
    Z jO                        S SK r S SKJr  S SKrS SKJr  S SKJrJrJ	r	J
r
Jr  S SKJr  S SKJr  SSKJr  SS	KJr  SS
KJrJr  SSKJr  SSKJr  SSKJrJrJr  SSKJr  SSK J!r!  SSK"J#r#J$r$J%r%J&r&  SSK'J(r(J)r)  SSK*J+r+J,r,J-r-J.r.  SSK/J0r0  SSK1J2r2   " S S\2\5      r3 " S S\05      r4\" SS9\ " S S\)5      5       5       r5\" SS9\ " S S\(5      5       5       r6 " S  S!\Rn                  5      r8 " S" S#\$5      r9 " S$ S%\#5      r: " S& S'\,5      r; " S( S)\+5      r< " S* S+\.5      r=\" S,S-9 " S. S/\=5      5       r> " S0 S1\-5      r?/ S2Qr@g)3    N)Callable)strict)	Tokenizerdecodersnormalizerspre_tokenizers
processors)Unigram)nn   )create_bidirectional_mask)BaseModelOutput)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack)TokenizersBackend)TransformersKwargsauto_docstringcan_return_tuple)merge_with_config_defaults)capture_outputs   )LlamaAttentionLlamaRotaryEmbeddingapply_rotary_pos_embeager_attention_forward)ParakeetCTCConfigParakeetEncoderConfig)ParakeetEncoderBlock ParakeetEncoderConvolutionModuleParakeetForCTCParakeetPreTrainedModel)ParakeetProcessor)T5Tokenizerc                   h    \ rS rSr        SS jr   SS\\\   -  S\S\S-  S\S\4
S	 jjr	S
r
g)LasrTokenizer,   Nc	           	      n   XPl         Ub~  U V
s/ s H  n
S[        U
5      ;   d  M  U
PM     nn
[        U5      S:  a$  U[        U5       Vs/ s H	  nSU S3PM     sn-  nOIUS:  a!  U[        U5      :w  a  [	        SU SU S35      eO![        U5       Vs/ s H	  nSU S3PM     nnUnUb  Xpl        Od[        U5      S4[        U5      S4[        U5      S4S	/U l        [        US-
  S
S
5       H$  nU R
                  R                  SU S3S45        M&     [        [        U R
                  SSS95      U l	        Ub%  [        R                  " U5      U R                  l        [        R                  " [        R                  " 5       [        R                   " SSSS9/5      U R                  l        [$        R                   " SSSS9U R                  l        [(        R*                  " SUUUUUS.U	D6  [,        R.                  " SS// SQSU R0                  4/S9U R                  l        g s  sn
f s  snf s  snf )Nz
<extra_id_   >r   zBoth extra_ids (z!) and additional_special_tokens (zm) are provided to LasrTokenizer. In this case the additional_special_tokens must include the extra_ids tokens        )   ▁g       r   F)unk_idbyte_fallbackr,   alwaysT)replacementprepend_schemesplit)	eos_token	unk_token	pad_token	extra_idsadditional_special_tokens$A</s>)r9   r:   z$Br:   )singlepairspecial_tokens )
_extra_idsstrlenrange
ValueError_vocab_scoresappendr   r
   
_tokenizerr   Precompiled
normalizerr   SequenceWhitespaceSplit	Metaspacepre_tokenizerr   decoderr   __init__r	   TemplateProcessingeos_token_idpost_processor)selfr4   r5   r6   _spm_precompiled_charsmapr7   r8   vocab
vocab_filekwargsxextra_tokensis                v/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/models/lasr/modular_lasr.pyrN   LasrTokenizer.__init__-   s_    $ %0'@['@!LTWXYTZDZA'@L[< 1$)yIY-ZIYA
1#Q.?IY-ZZ)Q9L0A#A &yk1RSlRm n   8=Y7GH7G!j1-7GLH(4% !& Y%Y%Y%	"D 9q="b1""))Zs!+<c*BC 2#""#
 %0)4)@)@AZ)[DOO&(6(?(?..0((U8[_`)
% #+"4"4W_gk"l"" 	
&?	
 	
 *4)F)F&>-**+*
&k \-Z Is   H(H(
H-H2	token_idsskip_special_tokensclean_up_tokenization_spacesgroup_tokensreturnc                     [        U[        5      (       a  U/nU(       a(  [        R                  " U5       Vs/ s H  ofS   PM	     nnU Vs/ s H  owU R                  :w  d  M  UPM     nn[
        R                  " U 4UUUS.UD6$ s  snf s  snf )Nr   )r\   r]   r^   )
isinstanceint	itertoolsgroupbypad_token_idr   _decode)rR   r\   r]   r^   r_   rV   token_grouptokens           rZ   rg   LasrTokenizer._decodez   s     i%%"I;D;L;LY;WX;WKQ;WIX )2P	ud>O>O5OU		P ((
 3)E	

 
 	
 Y Qs   BB#B)r?   rF   rD   )r:   z<unk>z<pad>Nd   NNN)FNT)__name__
__module____qualname____firstlineno__rN   rc   listboolr@   rg   __static_attributes__r>       rZ   r&   r&   ,   st     "&"&K
` %*48!
c?
 "
 '+Tk	

 
 

 
rs   r&   c                       \ rS rSrSrg)LasrProcessor   r>   Nrl   rm   rn   ro   rr   r>   rs   rZ   ru   ru      s    rs   ru   zgoogle/medasr)
checkpointc                   ,   \ rS rSr% SrSr\\S'   Sr\\S'   Sr	\\S'   S	r
\\S
'   S	r\\S'   Sr\\S'   Sr\\S'   Sr\\S'   Sr\\S'   Sr\\S'   Sr\\   \\S4   -  \S'   Sr\\   \\S4   -  \S'   Sr\\S'   Sr\S-  \S'   \" 5       r\" 5       rSrg) LasrEncoderConfig   a  
convolution_bias (`bool`, *optional*, defaults to `False`):
    Whether to use bias in convolutions of the conformer's convolution module.
conv_kernel_size (`int`, *optional*, defaults to 32):
    The kernel size of the convolution layers in the Conformer block.
subsampling_conv_channels (`int`, *optional*, defaults to 256):
    The number of channels in the subsampling convolution layers.
subsampling_conv_kernel_size (`int`, *optional*, defaults to 5):
    The kernel size of the subsampling convolution layers.
subsampling_conv_stride (`int`, *optional*, defaults to 2):
    The stride of the subsampling convolution layers.
dropout_positions (`float`, *optional*, defaults to 0.0):
    The dropout ratio for the positions in the input sequence.
feed_forward_residual_weights (`tuple[float, float]`, *optional*, defaults to `[1.5, 0.5]`):
    The residual weights for the feed forward layers.
conv_residual_weights (`tuple[float, float]`, *optional*, defaults to `[2.0, 1.0]`):
    The residual weights for the convolution layers.
batch_norm_momentum (`float`, *optional*, defaults to 0.01):
    The momentum for the batch normalization layers

Example:
    ```python
    >>> from transformers import LasrEncoderModel, LasrEncoderConfig

    >>> # Initializing a `LasrEncoder` configuration
    >>> configuration = LasrEncoderConfig()

    >>> # Initializing a model from the configuration
    >>> model = LasrEncoderModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```

This configuration class is based on the LasrEncoder architecture from Google Health AI. You can find more details
and pre-trained models at [google/medasr](https://huggingface.co/google/medasr).
   hidden_size   num_hidden_layersi   intermediate_sizeFattention_biasconvolution_bias    conv_kernel_size   subsampling_conv_kernel_size   num_mel_binsi'  max_position_embeddingsgư>layer_norm_eps)g      ?g      ?.feed_forward_residual_weights)g       @g      ?conv_residual_weightsg{Gz?batch_norm_momentumNrope_parametersr>   )rl   rm   rn   ro   __doc__r}   rc   __annotations__r   r   r   rq   r   r   r   r   r   r   floatr   rp   tupler   r   r   dictAttributeErrorsubsampling_factorscale_inputrr   r>   rs   rZ   rz   rz      s    $L Ks!s! ND "d"c() #)L##(S( NE EO!4;ucz1B#BO=G4;ucz)::G!%%#'OTD['') "Krs   rz   c                   F    \ rS rSr% SrSr\\S'   Sr\\S'   \	S 5       r
Srg	)
LasrCTCConfig   a  
ctc_loss_reduction (`str`, *optional*, defaults to `"mean"`):
    Specifies the reduction to apply to the output of `torch.nn.CTCLoss`. Only relevant when training an
    instance of [`LasrForCTC`].
ctc_zero_infinity (`bool`, *optional*, defaults to `True`):
    Whether to zero infinite losses and the associated gradients of `torch.nn.CTCLoss`. Infinite losses mainly
    occur when the inputs are too short to be aligned to the targets. Only relevant when training an instance
    of [`LasrForCTC`].

Example:
    ```python
    >>> from transformers import LasrForCTC, LasrCTCConfig
    >>> # Initializing a Lasr configuration
    >>> configuration = LasrCTCConfig()
    >>> # Initializing a model from the configuration
    >>> model = LasrForCTC(configuration)
    >>> # Accessing the model configuration
    >>> configuration = model.config
    ```
This configuration class is based on the Lasr CTC architecture from Google Health AI. You can find more details
and pre-trained models at [google/medasr](https://huggingface.co/google/medasr).
r|   
vocab_sizer   rf   c                 4    U R                   R                  S-  $ )Nr   )encoder_configsubsampling_conv_stride)rR   s    rZ   inputs_to_logits_ratio$LasrCTCConfig.inputs_to_logits_ratio   s    ""::A==rs   r>   N)rl   rm   rn   ro   r   r   rc   r   rf   propertyr   rr   r>   rs   rZ   r   r      s/    . JL#> >rs   r   c                   j   ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )LasrEncoderSubsampling   configc                 &  > [         TU ]  5         [        R                  " UR                  UR
                  5      U l        [        R                  " UR
                  UR
                  UR                  UR                  S9U l
        [        R                  " UR
                  UR                  UR                  UR                  S9U l        [        R                  " UR                  UR
                  5      U l        [        R                  " 5       U l        g )N)kernel_sizestride)superrN   r   Linearr   r}   dense_0Conv1dr   r   conv_0subsampling_conv_channelsconv_1dense_1ReLUact_fn)rR   r   	__class__s     rZ   rN   LasrEncoderSubsampling.__init__   s    yy!4!4f6H6HIii;;11	
 ii,,;;11	
 yy!A!A6CUCUVggirs   input_featuresr`   c                 ,   U R                  U R                  U5      5      nUR                  SS5      nU R                  U R                  U5      5      nU R                  U R	                  U5      5      nUR                  SS5      nU R                  U5      $ )Nr)   r   )r   r   	transposer   r   r   )rR   r   hidden_statess      rZ   forwardLasrEncoderSubsampling.forward  sz    DLL$@A%//15DKK$>?DKK$>?%//15||M**rs   )r   r   r   r   r   )rl   rm   rn   ro   rz   rN   torchTensorr   rr   __classcell__r   s   @rZ   r   r      s0     0  $+ell +u|| + +rs   r   c                       \ rS rSrSrg)LasrEncoderRotaryEmbeddingi  r>   Nrw   r>   rs   rZ   r   r     s    rs   r   c                      ^  \ rS rSrS\S\4U 4S jjr  SS\R                  S\	\R                  \R                  4   S-  S\R                  S-  S	\
\   S
\	\R                  \R                  4   4
S jjrSrU =r$ )LasrEncoderAttentioni  r   	layer_idxc                 2   > [         TU ]  X5        SU l        g )NF)r   rN   	is_causalrR   r   r   r   s      rZ   rN   LasrEncoderAttention.__init__  s    +rs   Nr   position_embeddingsattention_maskrV   r`   c                    UR                   S S n/ UQSPU R                  P7nU R                  U5      R                  U5      R	                  SS5      nU R                  U5      R                  U5      R	                  SS5      nU R                  U5      R                  U5      R	                  SS5      n	Uu  p[        XxX5      u  px[        R                  " U R                  R                  [        5      nU" U UUU	U4U R                  (       d  SOU R                  U R                  S.UD6u  pUR                   " / UQSP76 R#                  5       nU R%                  U5      nX4$ )Nr-   r)   r   r+   )dropoutscaling)shapehead_dimq_projviewr   k_projv_projr   r   get_interfacer   _attn_implementationr   trainingattention_dropoutr   reshape
contiguouso_proj)rR   r   r   r   rV   input_shapehidden_shapequery_states
key_statesvalue_statescossinattention_interfaceattn_outputattn_weightss                  rZ   r   LasrEncoderAttention.forward  s\    $))#2.88b8$--8{{=166|DNNqRST[[/44\BLLQPQR
{{=166|DNNqRST&#7RU#[ (?(M(MKK,,.E)
 %8	%
  $}}C$2H2HLL	%
 	%
! "));;;;FFHkk+.((rs   )r   NN)rl   rm   rn   ro   rz   rc   rN   r   r   r   r   r   r   rr   r   r   s   @rZ   r   r     s    0 S  IM.2	")||") #5<<#=>E") t+	")
 +,") 
u||U\\)	*") ")rs   r   c                   4   ^  \ rS rSrSS\4U 4S jjjrSrU =r$ )LasrEncoderConvolutionModulei>  r   c                    > [         TU ]  X5        SU l        [        R                  " UR
                  UR                  S9U l        g )Nsame)momentum)r   rN   paddingr   BatchNorm1dr}   r   norm)rR   r   module_configr   s      rZ   rN   %LasrEncoderConvolutionModule.__init__?  s5    /NN6#5#5@Z@Z[	rs   )r   r   N)rl   rm   rn   ro   rz   rN   rr   r   r   s   @rZ   r   r   >  s    \0 \ \rs   r   c                      ^  \ rS rSrS\S\4U 4S jjr  SS\R                  S\R                  S-  S\R                  S-  S	\	\
   S
\R                  4
S jjrSrU =r$ )LasrEncoderBlockiE  r   r   c                 >  > [         TU ]  X5        UR                  U l        UR                  U l        [        R
                  " UR                  UR                  SS9U l        [        R
                  " UR                  UR                  SS9U l	        [        R
                  " UR                  UR                  SS9U l
        [        R
                  " UR                  UR                  SS9U l        [        R
                  " UR                  UR                  SS9U l        g )NF)bias)r   rN   r   r   r   	LayerNormr}   r   norm_feed_forward1norm_self_att	norm_convnorm_feed_forward2norm_outr   s      rZ   rN   LasrEncoderBlock.__init__F  s    +-3-Q-Q*%+%A%A""$,,v/A/A6CXCX_d"e\\&*<*<f>S>SZ_`f&8&8&:O:OV[\"$,,v/A/A6CXCX_d"eV%7%79N9NUZ[rs   Nr   r   r   rV   r`   c                 &   UnU R                  U R                  U5      5      nU R                  S   U-  U R                  S   U-  -   nU R                  U5      nU R                  " SUUUS.UD6u  pxX-   nU R                  U R                  U5      US9n	U R                  S   U-  U R                  S   U	-  -   nUnU R                  U R                  U5      5      nU R                  S   U-  U R                  S   U-  -   nU R                  U5      nU$ )Nr   r)   )r   r   r   )r   r>   )feed_forward1r   r   r   	self_attnconvr   r   feed_forward2r   r   )
rR   r   r   r   rV   residualnormalized_hidden_statesr   _conv_outputs
             rZ   r   LasrEncoderBlock.forwardR  sD    !**4+B+B=+QR..q1H<t?a?abc?dgt?tt 	 $(#5#5m#D  
2) 3
 	
 &3ii} =ni]2215EHbHbcdHehsHss **4+B+B=+QR..q1H<t?a?abc?dgt?tt 	 m4rs   )r   r   r   r   r   r   r   r   )rl   rm   rn   ro   rz   rc   rN   r   r   r   r   r   rr   r   r   s   @rZ   r   r   E  s|    
\0 
\S 
\ /337	!||! t+! #\\D0	!
 +,! 
! !rs   r   c                   @    \ rS rSrSrS rS\R                  4S jrSr	g)LasrPreTrainedModeliv  Fc                 0    [         R                  " U5        g r   )r   _init_weights)rR   modules     rZ   r  !LasrPreTrainedModel._init_weightsz  s    %%f-rs   input_lengthsc                     [        U R                  [        5      (       a  U R                  R                  OU R                  nUR                  nUR
                  nSn[        U5       H  nX-
  U-  S-   nM     U$ )Nr   r)   )rb   r   r   r   r   r   rB   )rR   r  r   r   r   
num_layersr  s          rZ   _get_subsampling_output_length2LasrPreTrainedModel._get_subsampling_output_length}  sn    7A$++}7]7]33cgcncn$AA77
z"A*8VCaGM # rs   r>   N)
rl   rm   rn   ro   _supports_flex_attnr  r   r   r  rr   r>   rs   rZ   r  r  v  s    .	ELL 	rs   r  zh
    The LasrEncoder model, based on the Conformer architecture](https://arxiv.org/abs/2005.08100).
    )custom_introc                      ^  \ rS rSr% \\S'   SrS\4U 4S jjr\\	\
\ SS\R                  S\R                  S-  S\\   S	\4S
 jj5       5       5       5       rSrU =r$ )LasrEncoderi  r   encoderc           	        > [         TU ]  U5        SU l        UR                  U l        UR                  U l        UR
                  U l        [        U5      U l        [        U5      U l	        [        R                  " [        UR                  5       Vs/ s H  n[        X5      PM     sn5      U l        [        R                   " UR"                  UR$                  SS9U l        U R)                  5         g s  snf )NF)epsr   )r   rN   gradient_checkpointingr   dropout_positions	layerdropr   
subsamplerr   
rotary_embr   
ModuleListrB   r   r   layersr   r}   r   out_norm	post_initr   s      rZ   rN   LasrEncoder.__init__  s     &+#~~!'!9!9))084V<mmBGH`H`BabBaYf0Bab
 V%7%7V=R=RY^_	 cs   C3Nr   r   rV   r`   c                 L   U R                  U5      nU R                  U[        R                  " UR                  S   UR
                  S9R                  S5      5      u  pV[        R                  R                  X@R                  U R                  S9n[        R                  R                  XPR                  U R                  S9n[        R                  R                  X`R                  U R                  S9nUb  U R                  X$R                  S   S9n[        U R                  UUS9nU R                   HS  nSnU R                  (       a'  [        R                   " / 5      n	XR"                  :  a  SnU(       a  MF  U" U4UXV4S	.UD6nMU     U R%                  U5      n['        US
9$ )aR  
Example:

```python
>>> from transformers import AutoProcessor, LasrEncoder
>>> from datasets import load_dataset, Audio

>>> model_id = TODO
>>> processor = AutoProcessor.from_pretrained(model_id)
>>> encoder = ParakeetEncoder.from_pretrained(model_id)

>>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
>>> ds = ds.cast_column("audio", Audio(sampling_rate=processor.feature_extractor.sampling_rate))

>>> inputs = processor(ds[0]["audio"]["array"])
>>> encoder_outputs = encoder(**inputs)

>>> print(encoder_outputs.last_hidden_state.shape)
```
r)   )devicer   )pr   )target_length)r   inputs_embedsr   FT)r   r   )last_hidden_state)r  r  r   aranger   r"  	unsqueezer   
functionalr   r   r  _get_output_attention_maskr   r   r  randr  r  r   )
rR   r   r   rV   r   r   r   encoder_layerto_dropdropout_probabilitys
             rZ   r   LasrEncoder.forward  st   > 7??5<<(;(;A(>}G[G[\ffghi
 --m||VZVcVc-dmm##C+A+ADMM#Zmm##C+A+ADMM#Z%!<<^[n[nop[q<rN2;;')
 "[[MG}}&+jjn#&7"G7 -!!#1),
! 	! )  m4??rs   )r   r  r  r  r  r  r  r  r   )rl   rm   rn   ro   rz   r   base_model_prefixrN   r   r   r   r   r   r   r   r   r   r   rr   r   r   s   @rZ   r  r    s     !0 "  /3?@?@ t+?@ +,	?@
 
?@     ?@rs   r  c                   (   ^  \ rS rSrU 4S jrSrU =r$ )
LasrForCTCi  c                  8   > [        5       R                  " S0 U D6$ )a  
Example:

```python
>>> from transformers import AutoProcessor, LasrForCTC
>>> from datasets import load_dataset, Audio

>>> model_id = TODO
>>> processor = AutoProcessor.from_pretrained(model_id)
>>> model = LasrForCTC.from_pretrained(model_id)

>>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
>>> ds = ds.cast_column("audio", Audio(sampling_rate=processor.feature_extractor.sampling_rate))

>>> inputs = processor(ds[0]["audio"]["array"], text=ds[0]["text"])
>>> predicted_ids = model.generate(**inputs)
>>> transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)

>>> print(transcription)
```
r>   )r   generate)super_kwargsr   s    rZ   r4  LasrForCTC.generate  s    , w/,//rs   r>   )rl   rm   rn   ro   r4  rr   r   r   s   @rZ   r2  r2    s    0 0rs   r2  )r2  r  r  ru   rz   r   r&   )Ard   collections.abcr   r   huggingface_hub.dataclassesr   
tokenizersr   r   r   r   r	   tokenizers.modelsr
   r   masking_utilsr   modeling_outputsr   modeling_utilsr   r   processing_utilsr   tokenization_utils_tokenizersr   utilsr   r   r   utils.genericr   utils.output_capturingr   llama.modeling_llamar   r   r   r   parakeet.configuration_parakeetr   r   parakeet.modeling_parakeetr   r    r!   r"   parakeet.processing_parakeetr#   t5.tokenization_t5r$   r&   ru   rz   r   Moduler   r   r   r   r   r  r  r2  __all__r>   rs   rZ   <module>rJ     sZ    $  . S S %  6 / F & > I I 7 5 v v V  = ,d
K!2 d
N	% 	 ?+7#- 7#  ,7#t ?+>% >  ,>@+RYY +8 <!5 ;')> ')T\#C \.+ .b1 & 
X@% X@
X@v0 04rs   