Ë
    ÛÓ^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y)é    N)ÚSequenceé   )Úmel_filter_bankÚwindow_function)ÚSequenceFeatureExtractor)ÚBatchFeature)ÚPaddingStrategyÚ
TensorTypeÚloggingÚarrayÚ	dimensionÚsizeÚstepÚreturnc                 óÀ  — | j                   dk7  rt        d«      ‚|dk7  r|| j                   dz
  k7  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                  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	            ú/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/gemma4/feature_extraction_gemma4.pyÚ_unfoldr&      sÚ   € à‡z�z�Q‚ÜÐaÓbÐbØ�B‚˜9¨¯
©
°Q©Ò6ÜÐaÓbÐbà"'§+¡+Ñ€J�Ø! DÑ(¨TÑ1°AÑ5€Jà�Q‚Ü�x‰x˜ Q¨Ð-°U·[±[ÔAÐAà 
¨DÐ1€LØ—m‘m AÑ&¨¯©°aÑ(8¸4Ñ(?ÀÇÁÈqÑAQÐR€Nä�6‰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_maskNÚ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           
      ó  •— t        ‰| �  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                  «      j-                  t        j.                  «      | _        t3        j4                  «       5  t3        j6                  d«       t9        | j(                  dz  dz   |||| j:                  d d¬«      | _        d d d «       |�,t        j                  |«      j?                  dd|«      | _         nd | _         |�,t        j                  |«      j?                  dd|«      | _!        y d | _!        y # 1 sw Y   ŒtxY w)
N)r,   r-   r.   r/   g     @�@r   r   Úignorer   Úhtk)Únum_frequency_binsÚnum_mel_filtersr2   r3   r-   ÚnormÚ	mel_scale© )"ÚsuperÚ__init__r2   r3   r4   r5   r6   r7   r8   ÚintÚroundÚframe_lengthÚ
hop_lengthr   r   Úfloat64r9   ÚmathÚceilÚlog2Ú
fft_lengthr   ÚastypeÚfloat32ÚwindowÚwarningsÚcatch_warningsÚsimplefilterr   r-   Úmel_filtersÚreshaper:   r;   )Úselfr,   r-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   ÚkwargsrN   Ú	__class__s                      €r%   rE   z$Gemma4AudioFeatureExtractor.__init___   sÖ  ø€ ô( 	‰Ñð 	
Ø%Ø'Ø'Ø"7ñ		
ð
 ò	
ð +ˆÔØ*ˆÔØ&ˆÔØ&<ˆÔ#Ø*ˆÔØˆŒØ"4ˆÔÜ¤ m°oÑ&EÈÑ&NÓ OÓPˆÔÜœe M°MÑ$AÀFÑ$JÓKÓLˆŒÜŸ™ )´2·:±:Ô>ˆŒàœ$Ÿ)™)¤D§I¡I¨d×.?Ñ.?Ó$@ÓAÑAˆ
Ø×ÒØ˜!‰OˆJØ$ˆŒô & d×&7Ñ&7Ó8×?Ñ?ÄÇ
Á
ÓKˆŒô ×$Ñ$Ó&ñ 
	Ü×!Ñ! (Ô+Ü.Ø#'§?¡?°aÑ#7¸!Ñ#;Ø ,Ø+Ø+Ø"×0Ñ0ØØô ˆDÔ÷
	ð Ð#Ü "§¡¨Ó 6× >Ñ >¸qÀ!À\Ó RˆDÕà $ˆDÔàÐ%Ü"$§(¡(¨>Ó":×"BÑ"BÀ1ÀaÈÓ"VˆDÕà"&ˆDÕ÷)
	ð 
	ús   ÅAHÈH
ÚwaveformÚattention_maskr   c                 óª  — |j                   dk(  rt        j                  |d¬«      }| j                  dkD  rO|| j                  t        j                  j
                  |j                  Ž j                  |j                  «      z  z   }| j                  dk7  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kD  r€| j                  rS|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'                  || j(                  d¬«      }	t        j*                  |	«      }
t        j,                  |
| j.                  «      }t        j0                  || j2                  z   «      }| j4                  �|| j4                  z
  }| j6                  �|| j6                  z  }|j9                  d«      }|j                  d   }t        j:                  |«      | j                  z  |z   dz
  }||   j                  t<        «      }||fS )Ú r   r   )Úaxisç        ç      ð?r   )r   r   Úconstant)Úmode)rb   Úconstant_valuesr   )r   r   r   .Néþÿÿÿ)Únr^   )r   r   Úexpand_dimsr7   ÚrandomÚrandnr   rO   r   r8   rH   Úpadr&   rI   r4   r5   ÚconcatenaterQ   ÚfftÚrfftrN   ÚabsÚmatmulrU   Úlogr9   r:   r;   ÚsqueezeÚarangeÚbool)rW   rZ   r[   Ú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ˆHà�;‰;˜ÒØ $§+¡+´·	±	·±ÀÇÁÐ0P×0WÑ0WÐX`×XfÑXfÓ0gÑ"gÑgˆHà×"Ñ" cÒ)Ø $×"9Ñ"9Ñ9ˆHð ×$Ñ$¨Ñ)ˆÜ—6‘6˜( V¨h¸¨]Ð$;À*ÔMˆÜŸ™ °¸1°ÀJÐ`aÔbˆà $× 1Ñ 1°AÑ 5Ðô $ H¸ÐAVÐ]a×]lÑ]lÔmÐà×Ñ˜cÒ!Ø×*Ò*Ø!2°3¸¸¸°7Ñ!;¸sÀT×EUÑEUÑ?UÑ!V�Ø 1°#°q¸°t°)Ñ <¸t×?OÑ?OÐRcÐdgÐilÐjlÐilÐdlÑRmÑ?mÑ m�ÜŸ™¨¸Ð(GÈbÔQ‘à*¨3°±¨7Ñ3°d×6FÑ6FÐIZÐ[^Ð`cÐacÐ`cÐ[cÑIdÑ6dÑd‘à& s¨C¨R¨C xÑ0ˆFð ˜$Ÿ+™+Ñ%ˆÜ�v‰v�{‰{˜6 T§_¡_¸2ˆ{Ó>ˆäŸ™ ›ˆä—9‘9˜^¨T×-=Ñ-=Ó>ˆÜ—v‘v˜h¨¯©Ñ7Ó8ˆà×ÑÐ(Ø'¨$×*;Ñ*;Ñ;ˆLà×ÑÐ*Ø'¨$×*=Ñ*=Ñ=ˆLà&×.Ñ.¨qÓ1ˆØ(×.Ñ.¨qÑ1ˆô
 ŸI™I nÓ5¸¿¹ÑGÐJ_Ñ_ÐbcÑcÐØÐ/Ñ0×7Ñ7¼Ó=ˆØ Ð$Ð$r'   Ú
raw_speechÚpaddingÚ
max_lengthÚ
truncationÚpad_to_multiple_ofÚreturn_tensorsc                 óª  — t        |t        j                  «      xr t        |j                  «      dkD  }	t        |t
        «      xr# t        |d   t        j                  t
        f«      }
|	xs |
}|r.|D �cg c]"  }t        j                  |g«      j                  ‘Œ$ }}n1|s/t        |t        j                  «      st        j                  |«      }|st        j                  |g«      g}| j                  t        d|i«      |||||¬«      }g }g }t        |j                  |j                  «      D ]c  \  }}| j                  |j                  |«      \  }}|j                  |j                  t        j                   «      «       |j                  |«       Œe t        ||«      D ��cg c]  \  }}||d   z  ‘Œ }}}t        ||dœ|¬«      S c c}w c c}}w )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   r*   )rƒ   r„   r…   r†   r/   ).N)r*   r+   )Útensor_type)Ú
isinstancer   ÚndarrayÚlenr   r   ÚasarrayÚTri   r   Úzipr*   r[   r�   ÚappendrO   rP   )rW   r‚   rƒ   r„   r…   r†   r‡   r/   rX   Úis_batched_numpyÚis_batched_sequenceÚ
is_batchedÚrsÚbatched_speechÚprepared_speechÚprepared_speech_maskÚspeechr€   s                     r%   Ú__call__z$Gemma4AudioFeatureExtractor.__call__ã   s´  € ôF & j´"·*±*Ó=Ò[Ä#Àj×FVÑFVÓBWÐZ[ÑB[ÐÜ(¨´XÓ>ÒtÄ:ÈjÐYZÉmÔ^`×^hÑ^hÔjrÐ]sÓCtÐØ%Ò<Ð)<ˆ
áØ7AÖB°œ"Ÿ*™* b TÓ*×,Ó,ÐBˆJÑBÙ¤J¨z¼2¿:¹:Ô$FÜŸ™ JÓ/ˆJáÜŸ*™* j \Ó2Ð3ˆJàŸ™ÜÐ*¨JÐ7Ó8ØØ!Ø!Ø1Ø"7ð "ó 
ˆð ˆØ!ÐÜ × =Ñ =¸~×?\Ñ?\Ó]ò 	.‰LˆF�DØ×4Ñ4°V·X±X¸tÓD‰LˆF�DØ×"Ñ" 6§=¡=´·±Ó#<Ô=Ø ×'Ñ'¨Õ-ð	.ô
 ILÈOÐ]qÓHr×s¹¸À˜6 D¨¡OÓ3ÐsˆÑsäØ.ÐG[Ñ\Ø&ô
ð 	
ùò3 Cùó. ts   Á6'G
Æ%G)é€   i€>  r_   Tg      4@g      $@r_   g     @¿@r_   TFr_   r`   gü©ñÒMbP?NN)Úlongesti S Trš   NT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úmodel_input_namesrF   Úfloatrr   r   rE   r   r‹   Útupler�   ÚlistÚstrr	   r
   r   r™   Ú__classcell__)rY   s   @r%   r)   r)   1   s  ø„ ñ)ðV *Ð+@ÐAÐð  Ø#Ø"Ø&*Ø!%Ø#Ø"Ø%Ø Ø'+Ø#ØØ$'ØØ/3Ø15ñ#H'àðH'ð ðH'ð ð	H'ð
  $ðH'ð ðH'ð ðH'ð ðH'ð ðH'ð ðH'ð !%ðH'ð ðH'ð ðH'ð "ðH'ð ðH'ð  ˜u‘o¨Ñ,ð!H'ð" ! ™¨$Ñ.õ#H'ðT8%¨R¯Z©Zð 8%ÈÏÉð 8%ÐX]Ð^`×^hÑ^hÐjl×jtÑjtÐ^tÑXuó 8%ðz 1:Ø!(ØØ),Ø26Ø-1ñD
à—J‘J  e¡Ñ,¨t°B·J±JÑ/?Ñ?À$ÀtÈEÁ{ÑBSÑSðD
ð ˜‘˜oÑ-ðD
ð ˜$‘Jð	D
ð
 ðD
ð   $™JðD
ð ˜jÑ(¨4Ñ/ðD
ð  $ d™{ðD
ð 
÷D
r'   r)   )rK   rR   Úcollections.abcr   Únumpyr   Úaudio_utilsr   r   Ú!feature_extraction_sequence_utilsr   Úfeature_extraction_utilsr   Úutilsr	   r
   r   Ú
get_loggerrœ   Úloggerr‹   rF   r&   r)   Ú__all__rC   r'   r%   ú<module>r¯      s†   ðó Û Ý $ã ç ;Ý IÝ 4ß 9Ñ 9ð 
ˆ×	Ñ	˜HÓ	%€ð^�2—:‘:ð ^¨#ð ^°Sð ^Àð ^ÈÏ
É
ó ^ô&v
Ð":ô v
ðr )Ð
)�r'   