§
    9²¸j¤6  ã            
       óØ   — d dl Z d dlZd dlmZ d dlZddlmZmZ ddl	m
Z
 ddlmZ ddlmZmZmZ  ej        e¦  «        Zdej        d	ed
ededej        f
d„Z G d„ de
¦  «        ZdgZdS )é    N)ÚSequenceé   )Úmel_filter_bankÚwindow_function)ÚSequenceFeatureExtractor)ÚBatchFeature)ÚPaddingStrategyÚ
TensorTypeÚloggingÚarrayÚ	dimensionÚsizeÚstepÚreturnc                 ó˜  — | j         dk    rt          d¦  «        ‚|dk    r|| j         dz
  k    rt          d¦  «        ‚| j        \  }}||z
  |z  dz   }|dk    rt          j        |d|f| j        ¬¦  «        S |||f}| j        d         | j        d         |z  | j        d         f}t          j        j         	                    | ||¬¦  «        S )	zNA basic NumPy equivalent of PyTorch's unfold for 2D arrays along the last dim.é   zFThis unfold implementation currently supports 2D arrays (batch, time).éÿÿÿÿé   zFThis unfold implementation only supports unfolding the last dimension.r   ©Údtype)ÚshapeÚstrides)
ÚndimÚ
ValueErrorr   ÚnpÚzerosr   r   ÚlibÚstride_tricksÚ
as_strided)	r   r   r   r   Ú
batch_sizeÚoriginal_lengthÚ
num_framesÚoutput_shapeÚoutput_stridess	            ú„/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/transformers/models/gemma4/feature_extraction_gemma4.pyÚ_unfoldr&      sÝ   € à„z�Q‚€ÝÐaÑbÔbÐbØ�B‚€˜9¨¬
°Q©Ò6Ð6ÝÐaÑbÔbÐbà"'¤+Ñ€J�Ø! DÑ(¨TÑ1°AÑ5€Jà�Q‚€ÝŒx˜ Q¨Ð-°U´[ÐAÑAÔAÐAà 
¨DÐ1€LØ”m AÔ&¨¬°aÔ(8¸4Ñ(?ÀÄÈqÔAQÐR€NåŒ6Ô×*Ò*¨5¸ÈnÐ*Ñ]Ô]Ð]ó    c            "       ó´  ‡ — e Zd ZdZddgZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d-dededededededededededededededee         dz  dee         dz  f ˆ fd„Z	d e
j        d!e
j        d"ee
j        e
j        f         fd#„Z	 	 	 	 	 	 d.d&e
j        ee         z  ee
j                 z  eee                  z  d'eez  ez  d(edz  d)ed*edz  d+eez  dz  dedz  d"efd,„Zˆ xZS )/ÚGemma4AudioFeatureExtractoraR
  An audio feature extractor Universal Speech Models https://huggingface.co/papers/2303.01037.

    Args:
        feature_size (`int`, *optional*, defaults to 128):
            The feature dimension of the extracted features.
        sampling_rate (`int`, *optional*, defaults to 16000):
            The sampling rate at which the audio files should be digitalized expressed in hertz (Hz).
        padding_value (`float`, *optional*, defaults to 0.0):
            Padding value used to pad the audio. Should correspond to silences.
        return_attention_mask (`bool`, *optional*, defaults to `True`):
            Whether to return the attention mask for the generated MEL spectrograms.
        frame_length_ms (`float`, *optional*, defaults to 20.0):
            The length of a frame in milliseconds.
        hop_length_ms (`float`, *optional*, defaults to 10.0):
            Length of the overlapping windows for the STFT used to obtain the Mel Frequency coefficients.
        min_frequency (`float`, *optional*, defaults to 0.0):
            The minimum frequency (in Hz) for the Mel filterbank.
        max_frequency (`float`, *optional*, defaults to 8000.0):
            The maximum frequency (in Hz) for the Mel filterbank.
        preemphasis (`float`, *optional*, defaults to 0.0):
            The preemphasis coefficient.
        preemphasis_htk_flavor (`bool`, *optional*, defaults to `True`):
            Whether to use HTK-style preemphasis.
        fft_overdrive (`bool`, *optional*, defaults to `False`):
            Whether to use FFT overdrive.
        dither (`float`, *optional*, defaults to 0.0):
            Adds dithering. In other words, adds a small Gaussian noise to each frame.
            E.g. use 0.0001 to add dithering with a normal distribution centered
            around 0.0 with standard deviation 0.0001 (assuming [-1,+1] range of raw_speech).
            The value 0.0 means no dithering.
            Dithering has similar effect as `spectrogram(mel_floor=...)`. It reduces
            the high log_mel_fbank values for signals with hard-zero sections,
            when VAD cutoff is present in the signal.
        input_scale_factor (`float`, *optional*, defaults to 1.0):
            Scaling factor applied to the input waveform.
        mel_floor (`float`, *optional*, defaults to 0.001):
            Minimum value for Mel spectrograms to avoid log(0).
        per_bin_mean (`Optional[Sequence[float]]`, *optional*):
            Mean values for per-bin normalization.
        per_bin_stddev (`Optional[Sequence[float]]`, *optional*):
            Standard deviation values for per-bin normalization.
    Úinput_featuresÚinput_features_maské€   é€>  ç        Tç      4@ç      $@ç     @¿@Fç      ð?çü©ñÒMbP?NÚfeature_sizeÚsampling_rateÚpadding_valueÚreturn_attention_maskÚframe_length_msÚhop_length_msÚmin_frequencyÚmax_frequencyÚpreemphasisÚpreemphasis_htk_flavorÚfft_overdriveÚditherÚinput_scale_factorÚ	mel_floorÚper_bin_meanÚper_bin_stddevc           
      ó0  •—  t          ¦   «         j        d	||||dœ|¤Ž || _        || _        |	| _        |
| _        || _        || _        || _        t          t          ||z  dz  ¦  «        ¦  «        | _        t          t          ||z  dz  ¦  «        ¦  «        | _        t          j        |t          j        ¬¦  «        | _        dt#          j        t#          j        | j        ¦  «        ¦  «        z  }| j        r|dz  }|| _        t+          | j        ¦  «                             t          j        ¦  «        | _        t3          j        ¦   «         5  t3          j        d¦  «         t9          | j        dz  dz   |||| j        d d¬¦  «        | _        d d d ¦  «         n# 1 swxY w Y   |�/t          j        |¦  «                             dd|¦  «        | _         nd | _         |�0t          j        |¦  «                             dd|¦  «        | _!        d S d | _!        d S )
N)r4   r5   r6   r7   g     @�@r   r   Úignorer   Úhtk)Únum_frequency_binsÚnum_mel_filtersr:   r;   r5   ÚnormÚ	mel_scale© )"ÚsuperÚ__init__r:   r;   r<   r=   r>   r?   r@   ÚintÚroundÚframe_lengthÚ
hop_lengthr   r   Úfloat64rA   ÚmathÚceilÚlog2Ú
fft_lengthr   ÚastypeÚfloat32ÚwindowÚwarningsÚcatch_warningsÚsimplefilterr   r5   Úmel_filtersÚreshaperB   rC   )Úselfr4   r5   r6   r7   r8   r9   r:   r;   r<   r=   r>   r?   r@   rA   rB   rC   ÚkwargsrV   Ú	__class__s                      €r%   rM   z$Gemma4AudioFeatureExtractor.__init___   s[  ø€ ð( 	�‰ŒÔð 	
Ø%Ø'Ø'Ø"7ð		
ð 	
ð
 ð	
ð 	
ð 	
ð +ˆÔØ*ˆÔØ&ˆÔØ&<ˆÔ#Ø*ˆÔØˆŒØ"4ˆÔÝ¥ m°oÑ&EÈÑ&NÑ OÔ OÑPÔPˆÔÝ�e M°MÑ$AÀFÑ$JÑKÔKÑLÔLˆŒÝœ )µ2´:Ð>Ñ>Ô>ˆŒà�$œ)¥D¤I¨dÔ.?Ñ$@Ô$@ÑAÔAÑAˆ
ØÔð 	Ø˜!‰OˆJØ$ˆŒõ & dÔ&7Ñ8Ô8×?Ò?ÅÄ
ÑKÔKˆŒõ Ô$Ñ&Ô&ð 
	ð 
	ÝÔ! (Ñ+Ô+Ð+Ý.Ø#'¤?°aÑ#7¸!Ñ#;Ø ,Ø+Ø+Ø"Ô0ØØð ñ  ô  ˆDÔð
	ð 
	ð 
	ñ 
	ô 
	ð 
	ð 
	ð 
	ð 
	ð 
	ð 
	øøøð 
	ð 
	ð 
	ð 
	ð Ð#Ý "¤¨Ñ 6Ô 6× >Ò >¸qÀ!À\Ñ RÔ RˆDÔÐà $ˆDÔàÐ%Ý"$¤(¨>Ñ":Ô":×"BÒ"BÀ1ÀaÈÑ"VÔ"VˆDÔÐÐà"&ˆDÔÐÐs   ÅA FÆFÆ!FÚwaveformÚattention_maskr   c                 óè  — |j         dk    rt          j        |d¬¦  «        }| j        dk    r;|| j        t          j        j        |j        Ž                      |j        ¦  «        z  z   }| j	        dk    r
|| j	        z  }| j
        dz  }t          j        |d|dffd¬	¦  «        }t          j        ||dfdd¬
¦  «        }| j
        dz   }t          |d|| j        ¬¦  «        }| j        dk    rz| j        rQ|dd d…f         d| j        z
  z  }|ddd…f         | j        |dd d…f         z  z
  }t          j        ||gd¬¦  «        }n.|ddd …f         | j        |dd d…f         z  z
  }n|dd d…f         }|| j        z  }t          j                             || j        d¬¦  «        }	t          j        |	¦  «        }
t          j        |
| j        ¦  «        }t          j        || j        z   ¦  «        }| j        �
|| j        z
  }| j        �
|| j        z  }|                     d¦  «        }|j        d         }t          j        |¦  «        | j        z  |z   dz
  }||                              t<          ¦  «        }||fS )Nr   r   )Úaxisr.   r2   r   )r   r   Úconstant)Úmode)rg   Úconstant_valuesr   )r   r   r   .éþÿÿÿ)Únre   )r   r   Úexpand_dimsr?   ÚrandomÚrandnr   rW   r   r@   rP   Úpadr&   rQ   r<   r=   ÚconcatenaterY   ÚfftÚrfftrV   ÚabsÚmatmulr]   ÚlogrA   rB   rC   ÚsqueezeÚarangeÚbool)r_   rb   rc   Úpad_leftÚframe_size_for_unfoldÚframes_to_processÚfirst_in_frameÚrest_in_frameÚframesÚstftÚmagnitude_specÚmel_specÚlog_mel_specÚmel_spectrogramÚnum_mel_framesÚframe_end_indicesÚmasks                    r%   Ú_extract_spectrogramz0Gemma4AudioFeatureExtractor._extract_spectrogram©   s±  € ØŒ=˜AÒÐÝ”~ h°QÐ7Ñ7Ô7ˆHàŒ;˜ÒÐØ $¤+µ´	´ÀÄÐ0P×0WÒ0WÐX`ÔXfÑ0gÔ0gÑ"gÑgˆHàÔ" cÒ)Ð)Ø $Ô"9Ñ9ˆHð Ô$¨Ñ)ˆÝ”6˜( V¨h¸¨]Ð$;À*ÐMÑMÔMˆÝœ °¸1°ÀJÐ`aÐbÑbÔbˆà $Ô 1°AÑ 5Ðõ $ H¸ÐAVÐ]aÔ]lÐmÑmÔmÐàÔ˜cÒ!Ð!ØÔ*ð eØ!2°3¸¸¸°7Ô!;¸sÀTÔEUÑ?UÑ!V�Ø 1°#°q¸°t°)Ô <¸tÔ?OÐRcÐdgÐilÐjlÐilÐdlÔRmÑ?mÑ m�Ýœ¨¸Ð(GÈbÐQÑQÔQ��à*¨3°°°¨7Ô3°dÔ6FÐIZÐ[^Ð`cÐacÐ`cÐ[cÔIdÑ6dÑd��à& s¨C¨R¨C xÔ0ˆFð ˜$œ+Ñ%ˆÝŒv�{Š{˜6 T¤_¸2ˆ{Ñ>Ô>ˆåœ ™œˆå”9˜^¨TÔ-=Ñ>Ô>ˆÝ”v˜h¨¬Ñ7Ñ8Ô8ˆàÔÐ(Ø'¨$Ô*;Ñ;ˆLàÔÐ*Ø'¨$Ô*=Ñ=ˆLà&×.Ò.¨qÑ1Ô1ˆØ(Ô.¨qÔ1ˆõ
 œI nÑ5Ô5¸¼ÑGÐJ_Ñ_ÐbcÑcÐØÐ/Ô0×7Ò7½Ñ=Ô=ˆØ Ð$Ð$r'   Úlongesté S Ú
raw_speechÚpaddingÚ
max_lengthÚ
truncationÚpad_to_multiple_ofÚreturn_tensorsc                 óV  — t          |t          j        ¦  «        ot          |j        ¦  «        dk    }	t          |t
          ¦  «        o&t          |d         t          j        t
          f¦  «        }
|	p|
}|rd„ |D ¦   «         }n0|s.t          |t          j        ¦  «        st          j        |¦  «        }|st          j        |g¦  «        g}|                      t          d|i¦  «        |||||¬¦  «        }g }g }t          |j
        |j        ¦  «        D ]j\  }}|                      |j        |¦  «        \  }}|                     |                     t          j        ¦  «        ¦  «         |                     |¦  «         Œkd„ t          ||¦  «        D ¦   «         }t          ||dœ|¬¦  «        S )	a  Creates a batch of MEL spectrograms from the provided raw speech.

        This implementation uses a different algorithm for windowing and preemphasis compared to the built-in
        `transformers.audio_utils.spectrogram()` function that _will_ result in different outputs. Consider this
        carefully when selecting an audio feature extractor, especially with pre-trained models.

        Args:
            raw_speech:
                The audio for which MEL spectrograms are created.
            padding (`Union[bool, str, PaddingStrategy]`, *optional*, defaults to `"longest"`):
                The padding strategy to use for batches of audio with different lengths.
            max_length (`int`, *optional*, defaults to 480000):
                If provided, defines the maximum length of the audio to allow. Audio longer than this will be
                truncated if `truncation=True`.
            truncation (`bool`, *optional*, defaults to `True`):
                Whether or not to truncate audio above `max_length`.
            pad_to_multiple_of (`int`, *optional*, defaults to 128):
                When padding, pad to a multiple of this value. The default value is defined for optimal TPU support.
            return_tensors (`Union[str, TensorType]`, *optional*, defaults to `None`):
                The type of tensors to return (e.g., NumPy, or Torch).
            return_attention_mask (`bool`, *optional*, defaults to `True`):
                Whether to return the attention mask for the generated MEL spectrograms.
        r   r   c                 óB   — g | ]}t          j        |g¦  «        j        ‘ŒS rK   )r   ÚasarrayÚT)Ú.0Úrss     r%   ú
<listcomp>z8Gemma4AudioFeatureExtractor.__call__.<locals>.<listcomp>
  s'   € ÐBÐBÐB°�"œ* b TÑ*Ô*Ô,ÐBÐBÐBr'   r*   )rŠ   r‹   rŒ   r�   r7   c                 ó*   — g | ]\  }}||d          z  ‘ŒS )).NrK   )r“   Úspeechr…   s      r%   r•   z8Gemma4AudioFeatureExtractor.__call__.<locals>.<listcomp>!  s%   € ÐsÐsÐs¹¸À˜6 D¨¤OÑ3ÐsÐsÐsr'   )r*   r+   )Útensor_type)Ú
isinstancer   ÚndarrayÚlenr   r   r‘   rn   r   Úzipr*   rc   r†   r’   ÚappendrW   rX   )r_   r‰   rŠ   r‹   rŒ   r�   rŽ   r7   r`   Úis_batched_numpyÚis_batched_sequenceÚ
is_batchedÚbatched_speechÚprepared_speechÚprepared_speech_maskr—   r…   s                    r%   Ú__call__z$Gemma4AudioFeatureExtractor.__call__â   sÉ  € õF & jµ"´*Ñ=Ô=Ð[Å#ÀjÔFVÑBWÔBWÐZ[ÒB[ÐÝ(¨µXÑ>Ô>ÐtÅ:ÈjÐYZÌmÕ^`Ô^hÕjrÐ]sÑCtÔCtÐØ%Ð<Ð)<ˆ
àð 	0ØBÐB°zÐBÑBÔBˆJˆJØð 	0¥J¨z½2¼:Ñ$FÔ$Fð 	0Ýœ JÑ/Ô/ˆJàð 	4Ýœ* j \Ñ2Ô2Ð3ˆJàŸšÝÐ*¨JÐ7Ñ8Ô8ØØ!Ø!Ø1Ø"7ð "ñ 
ô 
ˆð ˆØ!ÐÝ Ô =¸~Ô?\Ñ]Ô]ð 	.ð 	.‰LˆF�DØ×4Ò4°V´X¸tÑDÔD‰LˆF�DØ×"Ò" 6§=¢=µ´Ñ#<Ô#<Ñ=Ô=Ð=Ø ×'Ò'¨Ñ-Ô-Ð-Ð-àsÐsÍÈOÐ]qÑHrÔHrÐsÑsÔsˆåØ.ÐG[Ð\Ð\Ø&ð
ñ 
ô 
ð 	
r'   )r,   r-   r.   Tr/   r0   r.   r1   r.   TFr.   r2   r3   NN)r‡   rˆ   Tr,   NT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesrN   Úfloatrw   r   rM   r   rš   Útupler†   ÚlistÚstrr	   r
   r   r¤   Ú__classcell__)ra   s   @r%   r)   r)   1   sf  ø€ € € € € ð)ð )ðV *Ð+@ÐAÐð  Ø#Ø"Ø&*Ø!%Ø#Ø"Ø%Ø Ø'+Ø#ØØ$'ØØ/3Ø15ð#H'ð H'àðH'ð ðH'ð ð	H'ð
  $ðH'ð ðH'ð ðH'ð ðH'ð ðH'ð ðH'ð !%ðH'ð ðH'ð ðH'ð "ðH'ð ðH'ð  ˜u”o¨Ñ,ð!H'ð" ! œ¨$Ñ.ð#H'ð H'ð H'ð H'ð H'ð H'ðT7%¨R¬Zð 7%ÈÌð 7%ÐX]Ð^`Ô^hÐjlÔjtÐ^tÔXuð 7%ð 7%ð 7%ð 7%ðx 1:Ø!(ØØ),Ø26Ø-1ðD
ð D
à”J  e¤Ñ,¨t°B´JÔ/?Ñ?À$ÀtÈEÄ{ÔBSÑSðD
ð ˜‘˜oÑ-ðD
ð ˜$‘Jð	D
ð
 ðD
ð   $™JðD
ð ˜jÑ(¨4Ñ/ðD
ð  $ d™{ðD
ð 
ðD
ð D
ð D
ð D
ð D
ð D
ð D
ð D
r'   r)   )rS   rZ   Úcollections.abcr   Únumpyr   Úaudio_utilsr   r   Ú!feature_extraction_sequence_utilsr   Úfeature_extraction_utilsr   Úutilsr	   r
   r   Ú
get_loggerr¥   Úloggerrš   rN   r&   r)   Ú__all__rK   r'   r%   ú<module>r¸      s+  ðð €€€Ø €€€Ø $Ð $Ð $Ð $Ð $Ð $à Ð Ð Ð à ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø IÐ IÐ IÐ IÐ IÐ IØ 4Ð 4Ð 4Ð 4Ð 4Ð 4Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9ð 
ˆÔ	˜HÑ	%Ô	%€ð^�2”:ð ^¨#ð ^°Sð ^Àð ^ÈÌ
ð ^ð ^ð ^ð ^ð&u
ð u
ð u
ð u
ð u
Ð":ñ u
ô u
ð u
ðp )Ð
)€€€r'   