Ë
    T^(h¦ ã                   ó˜  — d dl mZ d dlmZmZmZmZ d dlZd dl	Z	d dl
mZ ddlmZ ddlmZmZmZmZ ddlmZ ddlmZmZmZ dd	lmZ dd
lmZmZmZmZm Z  ddl!m"Z"m#Z# ddl$m%Z%m&Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/ ddl0m1Z1  e-«       rd dl2m3Z3 ddl4m5Z5  e.jl                  e7«      Z8dZ9 G d„ dejt                  «      Z; G d„ dejt                  «      Z<de	jz                  de>de	jz                  fd„Z?	 dGdejt                  de	jz                  de	jz                  de	jz                  d ee	jz                     d!e@d"e@fd#„ZAd$„ ZBdHd%„ZC G d&„ d'ejt                  «      ZD G d(„ d)ejt                  «      ZE G d*„ d+ejt                  «      ZF G d,„ d-ejt                  «      ZGd.ZH e*d/eH«       G d0„ d1e&«      «       ZI G d2„ d3eI«      ZJd4ZK e*d/eH«       G d5„ d6eI«      «       ZL	 	 dId7ee>e>f   d8e@d9e>d ee	jš                     d:e>dejœ                  fd;„ZOd<ZP e*d/eH«       G d=„ d>eI«      «       ZQd?e	jz                  d@e>dAe>fdB„ZR e*dCeH«       G dD„ dEeIe«      «       ZSg dF¢ZTy)Jé    )Úpartial)ÚCallableÚOptionalÚTupleÚUnionNé   )ÚACT2FN)ÚCacheÚDynamicCacheÚEncoderDecoderCacheÚStaticCache)ÚGenerationMixin)ÚAttentionMaskConverterÚ_prepare_4d_attention_maskÚ#_prepare_4d_attention_mask_for_sdpa)ÚFlashAttentionKwargs)ÚBaseModelOutputÚBaseModelOutputWithPastÚ)BaseModelOutputWithPastAndCrossAttentionsÚSeq2SeqLMOutputÚSeq2SeqModelOutput)ÚROPE_INIT_FUNCTIONSÚdynamic_rope_update)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedModel)ÚUnpack)Úadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚcan_return_tupleÚis_torch_flex_attn_availableÚloggingÚreplace_return_docstringsé   )ÚMoonshineConfig)Ú	BlockMask)Úmake_flex_block_causal_maskr$   c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚMoonshineEncoderMLPc                 ó  •— t         ‰| �  «        || _        t        |   | _        t        j                  |j                  |j                  «      | _	        t        j                  |j                  |j                  «      | _
        y ©N©ÚsuperÚ__init__Úconfigr	   Úactivation_fnÚnnÚLinearÚhidden_sizeÚintermediate_sizeÚfc1Úfc2©Úselfr.   Ú
hidden_actÚ	__class__s      €ún/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/moonshine/modeling_moonshine.pyr-   zMoonshineEncoderMLP.__init__E   s^   ø€ Ü‰ÑÔØˆŒÜ# JÑ/ˆÔÜ—9‘9˜V×/Ñ/°×1IÑ1IÓJˆŒÜ—9‘9˜V×5Ñ5°v×7IÑ7IÓJˆ�ó    Úhidden_statesÚreturnc                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S r*   )r4   r/   r5   )r7   r<   s     r:   ÚforwardzMoonshineEncoderMLP.forwardL   s4   € ØŸ™ Ó/ˆØ×*Ñ*¨=Ó9ˆØŸ™ Ó/ˆØÐr;   ©Ú__name__Ú
__module__Ú__qualname__r-   ÚtorchÚTensorr?   Ú__classcell__©r9   s   @r:   r(   r(   D   s$   ø„ ôKð U§\¡\ð °e·l±l÷ r;   r(   c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚMoonshineDecoderMLPc                 ó  •— t         ‰| �  «        || _        t        |   | _        t        j                  |j                  |j                  dz  «      | _	        t        j                  |j                  |j                  «      | _
        y )Né   r+   r6   s      €r:   r-   zMoonshineDecoderMLP.__init__T   sc   ø€ Ü‰ÑÔØˆŒÜ# JÑ/ˆÔÜ—9‘9˜V×/Ñ/°×1IÑ1IÈAÑ1MÓNˆŒÜ—9‘9˜V×5Ñ5°v×7IÑ7IÓJˆ�r;   r<   r=   c                 óž   — | j                  |«      }|j                  dd¬«      \  }}| j                  |«      |z  }| j                  |«      }|S )NrK   éÿÿÿÿ©Údim)r4   Úchunkr/   r5   )r7   r<   Úgates      r:   r?   zMoonshineDecoderMLP.forward[   sS   € ØŸ™ Ó/ˆØ+×1Ñ1°!¸Ð1Ó<Ñˆ�tØ×*Ñ*¨4Ó0°=Ñ@ˆØŸ™ Ó/ˆØÐr;   r@   rG   s   @r:   rI   rI   S   s$   ø„ ôKð U§\¡\ð °e·l±l÷ r;   rI   r<   Ún_repr=   c                 óª   — | j                   \  }}}}|dk(  r| S | dd…dd…ddd…dd…f   j                  |||||«      } | j                  |||z  ||«      S )zÔ
    This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
    num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
    r#   N)ÚshapeÚexpandÚreshape)r<   rR   ÚbatchÚnum_key_value_headsÚslenÚhead_dims         r:   Ú	repeat_kvr[   c   so   € ð
 2?×1DÑ1DÑ.€EÐ  hØ�‚zØÐØ!¢!¢Q¨ªa²Ð"2Ñ3×:Ñ:¸5ÐBUÐW\Ð^bÐdlÓm€MØ× Ñ  Ð(;¸eÑ(CÀTÈ8ÓTÐTr;   ÚmoduleÚqueryÚkeyÚvalueÚattention_maskÚscalingÚdropoutc                 óT  — t        || j                  «      }t        || j                  «      }	t        j                  ||j	                  dd«      «      |z  }
|�#|d d …d d …d d …d |j
                  d   …f   }|
|z   }
t        j                  j                  |
dt        j                  ¬«      j                  |j                  «      }
t        j                  j                  |
|| j                  ¬«      }
t        j                  |
|	«      }|j	                  dd«      j                  «       }||
fS )NrK   r   éþÿÿÿrM   )rO   Údtype)ÚpÚtrainingr#   )r[   Únum_key_value_groupsrD   ÚmatmulÚ	transposerT   r0   Ú
functionalÚsoftmaxÚfloat32Útore   rb   rg   Ú
contiguous)r\   r]   r^   r_   r`   ra   rb   ÚkwargsÚ
key_statesÚvalue_statesÚattn_weightsÚcausal_maskÚattn_outputs                r:   Úeager_attention_forwardrv   o   s  € ô ˜3 × ;Ñ ;Ó<€JÜ˜U F×$?Ñ$?Ó@€Lä—<‘<  z×';Ñ';¸A¸qÓ'AÓBÀWÑL€LØÐ!Ø$¢Qªª1Ð.D°
×0@Ñ0@ÀÑ0DÐ.DÐ%DÑEˆØ# kÑ1ˆä—=‘=×(Ñ(¨¸2ÄUÇ]Á]Ð(ÓS×VÑVÐW\×WbÑWbÓc€LÜ—=‘=×(Ñ(¨¸È6Ï?É?Ð(Ó[€LÜ—,‘,˜|¨\Ó:€KØ×'Ñ'¨¨1Ó-×8Ñ8Ó:€Kà˜Ð$Ð$r;   c                 ó|   — | dddd…f   }| dddd…f   }t        j                  | |fd¬«      j                  d«      S )	z*Rotates half the hidden dims of the input..r   NrK   r#   rM   rN   rd   )rD   ÚstackÚflatten)ÚxÚx1Úx2s      r:   Úrotate_halfr}   ‰   sJ   € à	
ˆ3���1�ˆ9‰€BØ	
ˆ3���1�ˆ9‰€BÜ�;‰;˜˜˜R�y bÔ)×1Ñ1°"Ó5Ð5r;   c                 ó  — |j                  |«      }|j                  |«      }|dd|j                  d   dz  …f   j                  dd¬«      }|dd|j                  d   dz  …f   j                  dd¬«      }|j                  d   }| dd|…f   | d|d…f   }}|dd|…f   |d|d…f   }
}	||z  t        |«      |z  z   }|	|z  t        |	«      |z  z   }t	        j
                  ||gd¬«      }t	        j
                  ||
gd¬«      }||fS )aÛ  Applies Rotary Position Embedding to the query and key tensors.

    Args:
        q (`torch.Tensor`): The query tensor.
        k (`torch.Tensor`): The key tensor.
        cos (`torch.Tensor`): The cosine part of the rotary embedding.
        sin (`torch.Tensor`): The sine part of the rotary embedding.
        position_ids (`torch.Tensor`, *optional*):
            Deprecated and unused.
        unsqueeze_dim (`int`, *optional*, defaults to 1):
            The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
            sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
            that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
            k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
            cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
            the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
    Returns:
        `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
    .NrM   rK   rN   )Ú	unsqueezerT   Úrepeat_interleaver}   rD   Úcat)ÚqÚkÚcosÚsinÚposition_idsÚunsqueeze_dimÚ
rotary_dimÚq_rotÚq_passÚk_rotÚk_passÚq_embedÚk_embeds                r:   Úapply_rotary_pos_embr�   �   sD  € ð( �-‰-˜Ó
&€CØ
�-‰-˜Ó
&€Cð ˆcÐ'�S—Y‘Y˜r‘] aÑ'Ð'Ð'Ñ
(×
:Ñ
:¸1À"Ð
:Ó
E€CØ
ˆcÐ'�S—Y‘Y˜r‘] aÑ'Ð'Ð'Ñ
(×
:Ñ
:¸1À"Ð
:Ó
E€Cð —‘˜2‘€JØ�c˜;˜J˜;Ð&Ñ'¨¨3°
±Ð+;Ñ)<ˆ6€EØ�c˜;˜J˜;Ð&Ñ'¨¨3°
±Ð+;Ñ)<ˆ6€Eð �s‰{œ{¨5Ó1°CÑ7Ñ8€GØ�s‰{œ{¨5Ó1°CÑ7Ñ8€Gô �i‰i˜ &Ð)¨rÔ2€GÜ�i‰i˜ &Ð)¨rÔ2€GØ�GÐÐr;   c                   ól  ‡ — e Zd ZdZdededededef
ˆ fd„Z	 	 	 	 	 ddej                  d	e
eej                  ej                  f      d
e
ej                     de
e   de
ej                     de
ej                     dee   deej                  e
ej                     e
eej                        f   fd„Zˆ xZS )ÚMoonshineAttentionz=Multi-headed attention from 'Attention Is All You Need' paperr.   Ú	layer_idxÚ	is_causalÚnum_attention_headsrX   c                 ó8  •— t         ‰| �  «        |j                  ||dœ«       || _        || _        t        |d|j                  |j                  z  «      | _        |j                  |j                  z  | _
        | j                  dz  | _        |j                  | _        || _        t        j                  |j                  |j                  | j                  z  |j                   ¬«      | _        t        j                  |j                  |j                  | j                  z  |j                   ¬«      | _        t        j                  |j                  |j                  | j                  z  |j                   ¬«      | _        t        j                  |j                  | j                  z  |j                  d¬«      | _        | j                  j*                  �C| j                  j*                  }|| j                  |z   dz
  |z  z  }|| j                  z
  | _        y d| _        y )N)r”   rX   rZ   g      à¿©ÚbiasFr#   r   )r,   r-   Úupdater.   r’   Úgetattrr2   r”   rZ   rX   rh   ra   Úattention_dropoutr“   r0   r1   Úattention_biasÚq_projÚk_projÚv_projÚo_projÚpad_head_dim_to_multiple_ofÚhead_dim_padding)	r7   r.   r’   r“   r”   rX   Útarget_multipleÚtarget_head_dimr9   s	           €r:   r-   zMoonshineAttention.__init__½   s²  ø€ ô 	‰ÑÔØ�‰Ð.AÐZmÑnÔoØˆŒØ"ˆŒÜ ¨
°F×4FÑ4FÈ&×JdÑJdÑ4dÓeˆŒØ$*×$>Ñ$>À&×B\ÑB\Ñ$\ˆÔ!Ø—}‘} dÑ*ˆŒØ!'×!9Ñ!9ˆÔØ"ˆŒä—i‘iØ×Ñ × :Ñ :¸T¿]¹]Ñ JÐQW×QfÑQfô
ˆŒô —i‘iØ×Ñ × :Ñ :¸T¿]¹]Ñ JÐQW×QfÑQfô
ˆŒô —i‘iØ×Ñ × :Ñ :¸T¿]¹]Ñ JÐQW×QfÑQfô
ˆŒô —i‘i × :Ñ :¸T¿]¹]Ñ JÈF×L^ÑL^ÐejÔkˆŒð �;‰;×2Ñ2Ð>Ø"Ÿk™k×EÑEˆOØ-°$·-±-À/Ñ2QÐTUÑ2UÐZiÑ1iÑjˆOØ$3°d·m±mÑ$CˆDÕ!à$%ˆDÕ!r;   r<   Úposition_embeddingsr`   Úpast_key_valueÚcache_positionÚkey_value_statesrp   r=   c                 óÀ  — |j                   d d \  }}	| j                  |«      j                  ||	| j                  j                  | j
                  «      j                  dd«      }
|d u}|�Y|j                  j                  | j                  «      }|r&d|j                  | j                  <   |j                  }n|j                  }|�|n|}|r7|r5r3|j                  | j                     }|j                  | j                     }nÉ| j                  |«      j                  |d| j                  j                  | j
                  «      j                  dd«      }| j                  |«      j                  |d| j                  j                  | j
                  «      j                  dd«      }|r%|�#|j!                  ||| j                  d|i«      \  }}|s?|\  }}t#        |
|||«      \  }
}|�'|||dœ}|j!                  ||| j                  |«      \  }}t$        }| j                  j&                  dk7  r^| j                  j&                  dk(  r(|j                  d	d
«      rt(        j+                  d«       nt,        | j                  j&                     }| j.                  r	|€|	dkD  rdnd
}| j0                  dkD  r¢t2        j4                  j6                  j9                  |
d| j0                  f«      }
t2        j4                  j6                  j9                  |d| j0                  f«      }t2        j4                  j6                  j9                  |d| j0                  f«      } || |
|||f| j:                  sdn| j<                  | j>                  |dœ|¤Ž\  }}| j0                  dkD  r|dd | j0                   …f   }|jA                  ||	d«      jC                  «       }| jE                  |«      }||fS )NrM   r#   rK   Tr¦   )r…   r„   r¦   ÚeagerÚsdpaÚoutput_attentionsFzã`torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to eager attention. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.r   ç        )rb   ra   r“   .)#rT   rœ   Úviewr.   rX   rZ   rj   Ú
is_updatedÚgetr’   Úcross_attention_cacheÚself_attention_cacheÚ	key_cacheÚvalue_cacher�   rž   r˜   r�   rv   Ú_attn_implementationÚloggerÚwarning_oncer   r“   r¡   rD   r0   rk   Úpadrg   rš   ra   rV   ro   rŸ   )r7   r<   r¤   r`   r¥   r¦   r§   rp   ÚbszÚq_lenÚquery_statesÚis_cross_attentionr®   Úcurrent_statesrq   rr   r„   r…   Úcache_kwargsÚattention_interfacer“   ru   rs   s                          r:   r?   zMoonshineAttention.forwardâ   s¬  € ð #×(Ñ(¨¨"Ð-‰
ˆˆUð �K‰K˜Ó&×+Ñ+¨C°¸¿¹×8WÑ8WÐY]×YfÑYfÓg×qÑqÐrsÐuvÓwð 	ð .°TÐ9ÐØÐ%Ø'×2Ñ2×6Ñ6°t·~±~ÓFˆJÙ!à<@�×)Ñ)¨$¯.©.Ñ9Ø!/×!EÑ!E‘à!/×!DÑ!D�ð .>Ð-IÑ)È}ˆÙ¡.±ZØ'×1Ñ1°$·.±.ÑAˆJØ)×5Ñ5°d·n±nÑE‰Lð —‘˜NÓ+ß‘�c˜2˜tŸ{™{×>Ñ>ÀÇÁÓNß‘˜1˜a“ð ð —‘˜NÓ+ß‘�c˜2˜tŸ{™{×>Ñ>ÀÇÁÓNß‘˜1˜a“ð ñ
 " nÐ&@Ø+9×+@Ñ+@Ø ¨d¯n©nÐ?OÐQ_Ð>`ó,Ñ(�
˜Lñ "Ø*‰HˆC�Ü';¸LÈ*ÐVYÐ[^Ó'_Ñ$ˆL˜*àÐ)Ø'*°3È.ÑY�Ø+9×+@Ñ+@Ø ¨d¯n©n¸ló,Ñ(�
˜Lô )@ÐØ�;‰;×+Ñ+¨wÒ6Ø�{‰{×/Ñ/°6Ò9¸f¿j¹jÐI\Ð^cÔ>dÜ×#Ñ#ðLõô
 '>¸d¿k¹k×>^Ñ>^Ñ&_Ð#à ŸNšN¨~Ð/EÈ%ÐRSÊ)‘DÐY^ˆ	à× Ñ  1Ò$Ü Ÿ8™8×.Ñ.×2Ñ2°<À!ÀT×EZÑEZÐA[Ó\ˆLÜŸ™×,Ñ,×0Ñ0°¸aÀ×AVÑAVÐ=WÓXˆJÜ Ÿ8™8×.Ñ.×2Ñ2°<À!ÀT×EZÑEZÐA[Ó\ˆLá$7ØØØØØð
%
ð  $Ÿ}š}‘C°$×2HÑ2HØ—L‘LØñ
%
ð ñ
%
Ñ!ˆ�\ð × Ñ  1Ò$Ø% cÐ+C¨d×.CÑ.CÐ-CÐ+CÐ&CÑDˆKà!×)Ñ)¨#¨u°bÓ9×DÑDÓFˆØ—k‘k +Ó.ˆØ˜LÐ(Ð(r;   )NNNNN)rA   rB   rC   Ú__doc__r$   ÚintÚboolr-   rD   rE   r   r   r
   Ú
LongTensorr   r   r?   rF   rG   s   @r:   r‘   r‘   º   s  ø„ ÙGð#&àð#&ð ð#&ð ð	#&ð
 !ð#&ð !õ#&ðP LPØ15Ø*.Ø59Ø37ñ[)à—|‘|ð[)ð & e¨E¯L©L¸%¿,¹,Ð,FÑ&GÑHð[)ð ! §¡Ñ.ð	[)ð
 ! ™ð[)ð ! ×!1Ñ!1Ñ2ð[)ð # 5§<¡<Ñ0ð[)ð Ð-Ñ.ð[)ð 
ˆu�|‰|˜X e§l¡lÑ3°X¸eÀEÇLÁLÑ>QÑ5RÐRÑ	S÷[)r;   r‘   c                   ó^   ‡ — e Zd Zddefˆ fd„Z ej                  «       ed„ «       «       Zˆ xZ	S )ÚMoonshineRotaryEmbeddingr.   c                 óê  •— t         ‰| �  «        t        |d«      rG|j                  �;|j                  j	                  d|j                  j	                  d«      «      | _        nd| _        |j                  | _        |j                  | _        || _	        t        | j
                     | _        | j                  | j                  |«      \  }| _        | j                  d|d¬«       | j                  | _        y )NÚrope_scalingÚ	rope_typeÚtypeÚdefaultÚinv_freqF)Ú
persistent)r,   r-   ÚhasattrrÆ   r¯   rÇ   Úmax_position_embeddingsÚmax_seq_len_cachedÚoriginal_max_seq_lenr.   r   Úrope_init_fnÚattention_scalingÚregister_bufferrÊ   Úoriginal_inv_freq)r7   r.   ÚdevicerÊ   r9   s       €r:   r-   z!MoonshineRotaryEmbedding.__init__A  sÉ   ø€ Ü‰ÑÔä�6˜>Ô*¨v×/BÑ/BÐ/NØ#×0Ñ0×4Ñ4°[À&×BUÑBU×BYÑBYÐZ`ÓBaÓbˆD�Nà&ˆDŒNØ"(×"@Ñ"@ˆÔØ$*×$BÑ$BˆÔ!àˆŒÜ/°·±Ñ?ˆÔà+/×+<Ñ+<¸T¿[¹[È&Ó+QÑ(ˆ�$Ô(Ø×Ñ˜Z¨¸eÐÔDØ!%§¡ˆÕr;   c                 ób  — | j                   d d d …d f   j                  «       j                  |j                  d   dd«      j	                  |j
                  «      }|d d …d d d …f   j                  «       }t        |j
                  j                  t        «      r/|j
                  j                  dk7  r|j
                  j                  nd}t        j                  |d¬«      5  |j                  «       |j                  «       z  j                  dd«      }t        j                  ||fd¬	«      }|j                  «       | j                  z  }|j                  «       | j                  z  }	d d d «       j	                  |j                   ¬
«      	j	                  |j                   ¬
«      fS # 1 sw Y   ŒAxY w)Nr   rM   r#   ÚmpsÚcpuF)Údevice_typeÚenabledrK   rN   ©re   )rÊ   ÚfloatrU   rT   rn   rÔ   Ú
isinstancerÈ   ÚstrrD   Úautocastrj   r�   r„   rÑ   r…   re   )
r7   rz   r†   Úinv_freq_expandedÚposition_ids_expandedrØ   ÚfreqsÚembr„   r…   s
             r:   r?   z MoonshineRotaryEmbedding.forwardR  sV  € ð !ŸM™M¨$²°4¨-Ñ8×>Ñ>Ó@×GÑGÈ×HZÑHZÐ[\ÑH]Ð_aÐcdÓe×hÑhÐij×iqÑiqÓrÐØ ,ªQ°²a¨ZÑ 8× >Ñ >Ó @Ðä'1°!·(±(·-±-ÄÔ'EÈ!Ï(É(Ï-É-Ð[`ÒJ`�a—h‘h—m’mÐfkˆÜ�^‰^¨¸UÔCñ 	5Ø&×,Ñ,Ó.Ð1F×1LÑ1LÓ1NÑN×YÑYÐZ[Ð]^Ó_ˆEÜ—)‘)˜U E˜N°Ô3ˆCØ—'‘'“)˜d×4Ñ4Ñ4ˆCØ—'‘'“)˜d×4Ñ4Ñ4ˆC÷		5ð �v‰v˜AŸG™GˆvÓ$ c§f¡f°1·7±7 fÓ&;Ð;Ð;÷	5ð 	5ús   Ã BF%Æ%F.r*   )
rA   rB   rC   r$   r-   rD   Úno_gradr   r?   rF   rG   s   @r:   rÄ   rÄ   @  s3   ø„ ñ/˜õ /ð" €U‡]�]ƒ_Øñ<ó ó ô<r;   rÄ   c                   óp  ‡ — e Zd Zdedefˆ fd„Z	 	 	 	 	 	 	 ddej                  deej                     deej                     dee
   dee   d	ee   d
eej                     deeej                  ej                  f      dee   deej                  eeej                  ej                  f      f   fd„Zˆ xZS )ÚMoonshineEncoderLayerr.   r’   c                 ód  •— t         ‰| �  «        |j                  | _        t        ||d|j                  |j
                  ¬«      | _        t        ||j                  «      | _	        t        j                  |j                  d¬«      | _        t        j                  |j                  d¬«      | _        y )NF©r.   r’   r“   r”   rX   r–   )r,   r-   r2   r‘   Úencoder_num_attention_headsÚencoder_num_key_value_headsÚ	self_attnr(   Úencoder_hidden_actÚmlpr0   Ú	LayerNormÚinput_layernormÚpost_attention_layernorm©r7   r.   r’   r9   s      €r:   r-   zMoonshineEncoderLayer.__init__c  s‹   ø€ Ü‰ÑÔØ!×-Ñ-ˆÔä+ØØØØ &× BÑ BØ &× BÑ Bô
ˆŒô ' v¨v×/HÑ/HÓIˆŒÜ!Ÿ|™|¨F×,>Ñ,>ÀUÔKˆÔÜ(*¯©°V×5GÑ5GÈeÔ(TˆÕ%r;   r<   r`   r†   r¥   r«   Ú	use_cacher¦   r¤   rp   r=   c	                 óÚ   — |}
| j                  |«      } | j                  d||||||||dœ|	¤Ž\  }}|
|z   }|}
| j                  |«      }| j                  |«      }|
|z   }|f}|r||fz  }|S )N©r<   r`   r†   r¥   r«   rñ   r¦   r¤   © )rî   rê   rï   rì   )r7   r<   r`   r†   r¥   r«   rñ   r¦   r¤   rp   ÚresidualÚself_attn_weightsÚoutputss                r:   r?   zMoonshineEncoderLayer.forwards  s±   € ð !ˆà×,Ñ,¨]Ó;ˆð ,:¨4¯>©>ð 
,
Ø'Ø)Ø%Ø)Ø/ØØ)Ø 3ñ
,
ð ñ
,
Ñ(ˆÐ(ð ! =Ñ0ˆð !ˆØ×5Ñ5°mÓDˆØŸ™ Ó/ˆØ  =Ñ0ˆà Ð"ˆÙØÐ)Ð+Ñ+ˆGàˆr;   )NNNFFNN)rA   rB   rC   r$   rÀ   r-   rD   rE   r   rÂ   r
   rÁ   r   r   r   ÚFloatTensorr?   rF   rG   s   @r:   rå   rå   b  s  ø„ ðU˜ð U¸3õ Uð& 26Ø37Ø*.Ø,1Ø$)Ø59ØKOñ(à—|‘|ð(ð ! §¡Ñ.ð(ð ˜u×/Ñ/Ñ0ð	(ð
 ! ™ð(ð $ D™>ð(ð ˜D‘>ð(ð ! ×!1Ñ!1Ñ2ð(ð & e¨E¯L©L¸%¿,¹,Ð,FÑ&GÑHð(ð Ð-Ñ.ð(ð 
ˆu× Ñ  (¨5°×1BÑ1BÀE×DUÑDUÐ1UÑ+VÑ"WÐWÑ	X÷(r;   rå   c                    ó  ‡ — e Zd Zddedee   fˆ fd„Z	 	 	 	 	 	 	 	 	 	 	 ddej                  deej                     deej                     deej                     deej                     d	eej                     d
ee
   dee   dee   deej                     deeej                  ej                  f      deeej                  ej                  f      deej                  eeej                  ej                  f      f   fd„Zˆ xZS )ÚMoonshineDecoderLayerr.   r’   c                 ó  •— t         ‰| �  «        |j                  | _        t        ||d|j                  |j
                  ¬«      | _        t        ||d|j                  |j
                  ¬«      | _        t        ||j                  «      | _
        t        j                  |j                  d¬«      | _        t        j                  |j                  d¬«      | _        t        j                  |j                  d¬«      | _        y )NTrç   Fr–   )r,   r-   r2   r‘   Údecoder_num_attention_headsÚdecoder_num_key_value_headsrê   Úencoder_attnrI   Údecoder_hidden_actrì   r0   rí   rî   rï   Úfinal_layernormrð   s      €r:   r-   zMoonshineDecoderLayer.__init__Ÿ  sÎ   ø€ Ü‰ÑÔØ!×-Ñ-ˆÔä+ØØØØ &× BÑ BØ &× BÑ Bô
ˆŒô /ØØØØ &× BÑ BØ &× BÑ Bô
ˆÔô ' v¨v×/HÑ/HÓIˆŒÜ!Ÿ|™|¨F×,>Ñ,>ÀUÔKˆÔÜ(*¯©°V×5GÑ5GÈeÔ(TˆÔ%Ü!Ÿ|™|¨F×,>Ñ,>ÀUÔKˆÕr;   r<   r`   Úencoder_hidden_statesÚencoder_attention_maskr†   Úencoder_position_idsr¥   r«   rñ   r¦   r¤   Úencoder_position_embeddingsr=   c                 óH  — |}| j                  |«      } | j                  d||||||	|
|dœ|¤Ž\  }}||z   }d }|�2|}| j                  |«      }| j                  ||||||	¬«      \  }}||z   }|}| j	                  |«      }| j                  |«      }||z   }|f}|r|||fz  }|S )Nró   )r<   r§   r`   r¥   r«   rñ   rô   )rî   rê   rï   rþ   r   rì   )r7   r<   r`   r  r  r†   r  r¥   r«   rñ   r¦   r¤   r  rp   rõ   rö   Úcross_attn_weightsr÷   s                     r:   r?   zMoonshineDecoderLayer.forward·  s  € ð  !ˆà×,Ñ,¨]Ó;ˆð ,:¨4¯>©>ð 
,
Ø'Ø)Ø%Ø)Ø/ØØ)Ø 3ñ
,
ð ñ
,
Ñ(ˆÐ(ð ! =Ñ0ˆð "ÐØ Ð,Ø$ˆHØ ×9Ñ9¸-ÓHˆMØ04×0AÑ0AØ+Ø!6Ø5Ø-Ø"3Ø#ð 1Bó 1Ñ-ˆMÐ-ð % }Ñ4ˆMð !ˆØ×,Ñ,¨]Ó;ˆØŸ™ Ó/ˆØ  =Ñ0ˆà Ð"ˆáØÐ)Ð+=Ð>Ñ>ˆGàˆr;   r*   )NNNNNNFFNNN)rA   rB   rC   r$   r   rÀ   r-   rD   rE   rÂ   r
   rÁ   r   rø   r?   rF   rG   s   @r:   rú   rú   ž  sj  ø„ ñL˜ð L¸8ÀC¹=õ Lð6 26Ø8<Ø9=Ø37Ø;?Ø*.Ø,1Ø$)Ø59ØKOØSWñ<à—|‘|ð<ð ! §¡Ñ.ð<ð  (¨¯©Ñ5ð	<ð
 !)¨¯©Ñ 6ð<ð ˜u×/Ñ/Ñ0ð<ð ' u×'7Ñ'7Ñ8ð<ð ! ™ð<ð $ D™>ð<ð ˜D‘>ð<ð ! ×!1Ñ!1Ñ2ð<ð & e¨E¯L©L¸%¿,¹,Ð,FÑ&GÑHð<ð &.¨e°E·L±LÀ%Ç,Á,Ð4NÑ.OÑ%Pð<ð 
ˆu× Ñ  (¨5°×1BÑ1BÀE×DUÑDUÐ1UÑ+VÑ"WÐWÑ	X÷<r;   rú   aN  
    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

    This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
    Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
    and behavior.

    Parameters:
        config ([`MoonshineConfig`]):
            Model configuration class with all the parameters of the model. Initializing with a config file does not
            load the weights associated with the model, only the configuration. Check out the
            [`~PreTrainedModel.from_pretrained`] method to load the model weights.
zWThe bare Moonshine Model outputting raw hidden-states without any specific head on top.c                   óZ   — e Zd ZeZdZdZdZddgZdZ	dZ
dZdZd„ Zdej                  fd„Zy	)
ÚMoonshinePreTrainedModelÚmodelÚinput_valuesTrå   rú   c                 ó6  — | j                   j                  }t        |t        j                  t        j
                  f«      rY|j                  j                  j                  d|¬«       |j                  �%|j                  j                  j                  «        y y t        |t        j                  «      rf|j                  j                  j                  d|¬«       |j                  �2|j                  j                  |j                     j                  «        y y y )Nr¬   )ÚmeanÚstd)r.   Úinitializer_rangerÜ   r0   r1   ÚConv1dÚweightÚdataÚnormal_r—   Úzero_Ú	EmbeddingÚpadding_idx)r7   r\   r  s      r:   Ú_init_weightsz&MoonshinePreTrainedModel._init_weights  sÌ   € Ø�k‰k×+Ñ+ˆÜ�fœrŸy™y¬"¯)©)Ð4Ô5Ø�M‰M×Ñ×&Ñ&¨C°SÐ&Ô9Ø�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ä˜¤§¡Ô-Ø�M‰M×Ñ×&Ñ&¨C°SÐ&Ô9Ø×!Ñ!Ð-Ø—‘×"Ñ" 6×#5Ñ#5Ñ6×<Ñ<Õ>ð .ð .r;   Úinput_lengthsc                 ó~   — t        |dz
  dz  dz   «      }t        |dz
  dz  dz   «      }t        |dz
  dz  dz   «      }|S )zH
        Computes the output length of the convolutional layers
        é   é@   r#   é   r   rK   )rÀ   )r7   r  Úoutput_conv1_lengthÚoutput_conv2_lengthÚoutput_conv3_lengths        r:   Ú _get_feat_extract_output_lengthsz9MoonshinePreTrainedModel._get_feat_extract_output_lengths!  sZ   € ô " =°3Ñ#6¸"Ñ"<¸qÑ"@ÓAÐÜ!Ð#6¸Ñ#:¸aÑ"?À!Ñ"CÓDÐÜ!Ð#6¸Ñ#:¸aÑ"?À!Ñ"CÓDÐà"Ð"r;   N)rA   rB   rC   r$   Úconfig_classÚbase_model_prefixÚmain_input_nameÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_supports_flash_attn_2Ú_supports_sdpaÚ_supports_cache_classÚ_supports_static_cacher  rD   rÂ   r  rô   r;   r:   r  r    sT   „ ð
 #€LØÐØ$€OØ&*Ð#Ø0Ð2IÐJÐØ!ÐØ€NØ ÐØ!Ðò	?ð#¸e×>NÑ>Nô #r;   r  c                   óæ   ‡ — e Zd ZdZdZdefˆ fd„Zdej                  fd„Z	dej                  fd„Z
e	 	 	 	 ddeej                     d	eej                     d
ee   dee   dee   defd„«       Zˆ xZS )ÚMoonshineEncoderz£
    Transformer encoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MoonshineEncoderLayer`]

    Args:
        config: MoonshineConfig
    r
  r.   c           	      ób  •— t         ‰| �  |«       || _        |j                  }t	        j
                  d|ddd¬«      | _        t	        j
                  |d|z  dd¬	«      | _        t	        j
                  d|z  |dd¬	«      | _        t	        j                  d|d
¬«      | _
        t        |¬«      | _        t	        j                  t        |j                  «      D �cg c]  }t!        ||«      ‘Œ c}«      | _        t	        j$                  |d¬«      | _        d| _        | j+                  «        y c c}w )Nr#   r  r  F)Úkernel_sizeÚstrider—   rK   r  r   )r,  r-  gñhãˆµøä>)Ú
num_groupsÚnum_channelsÚeps©r.   r–   )r,   r-   r.   r2   r0   r  Úconv1Úconv2Úconv3Ú	GroupNormÚ	groupnormrÄ   Ú
rotary_embÚ
ModuleListÚrangeÚencoder_num_hidden_layersrå   Úlayersrí   Ú
layer_normÚgradient_checkpointingÚ	post_init)r7   r.   Ú	embed_dimÚidxr9   s       €r:   r-   zMoonshineEncoder.__init__6  sì   ø€ Ü‰Ñ˜Ô ØˆŒØ×&Ñ&ˆ	ä—Y‘Y˜q )¸ÀRÈeÔTˆŒ
Ü—Y‘Y˜y¨!¨i©-ÀQÈqÔQˆŒ
Ü—Y‘Y˜q 9™}¨iÀQÈqÔQˆŒ
ÜŸ™°ÀÐPTÔUˆŒä2¸&ÔAˆŒä—m‘mÜ;@À×AaÑAaÓ;bÖc°CÔ" 6¨3Õ/Òcó
ˆŒô Ÿ,™, y°uÔ=ˆŒà&+ˆÔ#Ø�‰Õùò ds   ÃD,r=   c                 ó   — | j                   S r*   ©r2  ©r7   s    r:   Úget_input_embeddingsz%MoonshineEncoder.get_input_embeddingsJ  s   € Ø�z‰zÐr;   r_   c                 ó   — || _         y r*   rB  ©r7   r_   s     r:   Úset_input_embeddingsz%MoonshineEncoder.set_input_embeddingsM  s	   € Øˆ�
r;   r`   r«   Úoutput_hidden_statesÚflash_attn_kwargsc                 óH  — |�|n| j                   j                  }|�|n| j                   j                  }|€t        d«      ‚|j	                  d«      }t
        j                  j                  | j                  |«      «      }| j                  |«      }t
        j                  j                  | j                  |«      «      }t
        j                  j                  | j                  |«      «      }|j                  ddd«      }|�ª| j                  |j                  d   «      }d}|ddd|…f   dd|…f   }| j                   j                   d	k(  r|d
k(  j#                  «       r|nd}nH| j                   j                   dk(  r|st%        ||j&                  «      }nt)        ||j&                  «      }t+        j,                  d|j                  d   |j.                  ¬«      j	                  d«      }	| j1                  ||	«      }
|rdnd}|rdnd}| j2                  D ]e  }|r||fz  }| j4                  r0| j6                  r$| j9                  |j:                  |||	d|dd|
«	      }n ||f||	||
dœ|¤Ž}|d   }|sŒ]||d   fz  }Œg | j=                  |«      }|r||fz  }t?        |||¬«      S )a  
        Args:
            input_values (`torch.FloatTensor` of shape `(batch_size, audio_length)`):
                Float values of the raw speech waveform. Raw speech waveform can be
                obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a
                `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into
                `input_values`, the [`AutoFeatureExtractor`] should be used for padding
                and conversion into a tensor of type `torch.FloatTensor`.
            attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
                Mask to avoid performing attention on padding indices in `input_values`. Mask values selected in `[0, 1]`:
                - 1 for tokens that are **not masked**,
                - 0 for tokens that are **masked**.
                [What are attention masks?](../glossary#attention-mask)
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
                tensors for more detail.
            output_hidden_states (`bool`, *optional*):
                Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
                more detail.
            return_dict (`bool`, *optional*):
                Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
        NzYou must specify input_values.r#   r   rK   rM   é€  .Úflash_attention_2r¬   rª   ©rÔ   rô   F)r`   r†   r«   r¤   ©Úlast_hidden_stater<   Ú
attentions) r.   r«   rH  Ú
ValueErrorr   r0   rk   Útanhr2  r6  Úgelur3  r4  Úpermuter  rT   r´   Úanyr   re   r   rD   ÚarangerÔ   r7  r;  r=  rg   Ú_gradient_checkpointing_funcÚ__call__r<  r   )r7   r
  r`   r«   rH  rI  r<   Úmask_lenÚdownsample_strider†   r¤   Úall_hidden_statesÚall_self_attnsÚencoder_layerÚlayer_outputss                  r:   r?   zMoonshineEncoder.forwardP  sÎ  € ð> 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð ÐÜÐ=Ó>Ð>ð $×-Ñ-¨aÓ0ˆÜŸ™×*Ñ*¨4¯:©:°lÓ+CÓDˆØŸ™ }Ó5ˆÜŸ™×*Ñ*¨4¯:©:°mÓ+DÓEˆÜŸ™×*Ñ*¨4¯:©:°mÓ+DÓEˆØ%×-Ñ-¨a°°AÓ6ˆð Ð%Ø×<Ñ<¸^×=QÑ=QÐRTÑ=UÓVˆHØ *ÐØ+¨CÑ1DÐ3DÐ1DÐ,DÑEÀcÈ9ÈHÈ9ÀnÑUˆNØ�{‰{×/Ñ/Ð3FÒFØ4BÀcÑ4I×3NÑ3NÔ3P¡ÐVZ‘ð —‘×1Ñ1°VÒ;ÑDUä!DÀ^ÐUb×UhÑUhÓ!i‘ô "<¸NÈM×L_ÑL_Ó!`�ä—|‘| A }×':Ñ':¸1Ñ'=Àm×FZÑFZÔ[×eÑeÐfgÓhˆð #Ÿo™o¨m¸\ÓJÐñ #7™B¸DÐÙ0™°dˆà!Ÿ[™[ò 	6ˆMÙ#Ø! mÐ%5Ñ5Ð!à×*Ò*¨t¯}ª}Ø $× AÑ AØ!×*Ñ*Ø!Ø"Ø ØØ%ØØØ'ó
!‘ñ !.Ø!ð!à#1Ø!-Ø&7Ø(;ñ!ð (ñ!�ð *¨!Ñ,ˆMâ Ø =°Ñ#3Ð"5Ñ5‘ð;	6ð> Ÿ™¨Ó6ˆñ  Ø -Ð!1Ñ1Ðä&Ø+Ø+Ø%ô
ð 	
r;   )NNNN)rA   rB   rC   r¿   r"  r$   r-   r0   ÚModulerD  rG  r   r   rD   rø   rE   rÁ   r   r   r   r?   rF   rG   s   @r:   r*  r*  ,  sÁ   ø„ ñð %€Oð˜õ ð( b§i¡ió ð¨"¯)©)ó ð ð 59Ø15Ø,0Ø/3ñp
à˜u×0Ñ0Ñ1ðp
ð ! §¡Ñ.ðp
ð $ D™>ð	p
ð
 ' t™nðp
ð $Ð$8Ñ9ðp
ð 
!òp
ó ôp
r;   r*  a$  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
            it.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            If `past_key_values` is used, optionally only the last `input_ids` have to be input (see
            `past_key_values`).

            If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
            and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
            information on the default strategy.

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.
        position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.n_positions - 1]`.

            [What are position IDs?](../glossary#position-ids)
        past_key_values (`Cache`, *optional*):
            Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
            blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
            returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.

            It is a [`~cache_utils.Cache`] instance. For more details, see our [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache).

            If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
            have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
            of shape `(batch_size, sequence_length)`.
        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        use_cache (`bool`, *optional*):
            If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
            `past_key_values`).
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
        cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
            Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`,
            this tensor is not affected by padding. It is used to update the cache in the correct position and to infer
            the complete sequence length.
c                   ód  ‡ — e Zd ZdZdZdefˆ fd„Zd„ Zd„ Ze	 e
e«      	 	 	 	 	 	 	 	 	 	 	 ddeej                     deej                     deej                     d	ee   d
eej"                     dee   dee   dee   deej                     deej"                     deej                     dee   deeef   fd„«       «       Z	 ddej                  dej                  dej                  d	edef
d„Zedej                  dededej8                  dej:                  dej                  defd„«       Zˆ xZS )ÚMoonshineDecoderz£
    Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MoonshineDecoderLayer`]

    Args:
        config: MoonshineConfig
    Ú	input_idsr.   c           	      ó  •— t         ‰| �  |«       |j                  | _        |j                  | _        t        j                  |j                  |j                  | j                  «      | _        t        j                  t        |j                  «      D �cg c]  }t        ||«      ‘Œ c}«      | _        t        j                  |j                  d¬«      | _        t!        |¬«      | _        d| _        | j'                  «        y c c}w )NFr–   r1  )r,   r-   Úpad_token_idr  Ú
vocab_sizer0   r  r2   Úembed_tokensr8  r9  Údecoder_num_hidden_layersrú   r;  rí   ÚnormrÄ   r7  r=  r>  )r7   r.   r@  r9   s      €r:   r-   zMoonshineDecoder.__init__  sÄ   ø€ Ü‰Ñ˜Ô Ø!×.Ñ.ˆÔØ ×+Ñ+ˆŒäŸL™L¨×):Ñ):¸F×<NÑ<NÐPT×P`ÑP`ÓaˆÔÜ—m‘mÜ;@À×AaÑAaÓ;bÖc°CÔ" 6¨3Õ/Òcó
ˆŒô —L‘L ×!3Ñ!3¸%Ô@ˆŒ	Ü2¸&ÔAˆŒØ&+ˆÔ#ð 	�‰Õùò ds   ÂDc                 ó   — | j                   S r*   ©rf  rC  s    r:   rD  z%MoonshineDecoder.get_input_embeddings%  s   € Ø× Ñ Ð r;   c                 ó   — || _         y r*   rj  rF  s     r:   rG  z%MoonshineDecoder.set_input_embeddings(  s
   € Ø!ˆÕr;   r`   r†   Úpast_key_valuesÚinputs_embedsrñ   r«   rH  r¦   r  r  rI  r=   c                 ó²  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|du |duz  rt	        d«      ‚| j
                  r%| j                  r|rt        j                  d«       d}|€| j                  |«      }|r"|€ t        «       }t        «       }t        ||«      }|	€F|�|j                  «       nd}t        j                  |||j                  d   z   |j                   ¬«      }	|€|	j#                  d«      }| j%                  |||	||«      }|}| j'                  ||«      }|rdnd}|rdnd}|r|
�dnd}|�·|
j                  d	   }d
}|ddd|…f   dd|…f   }| j                   j(                  dk(  r|dk(  j+                  «       r|nd}nd| j                   j(                  dk(  r'|s%t-        ||j.                  |j                  d	   «      }n$t1        ||j.                  |j                  d	   «      }| j2                  D ]�  }|r||fz  }| j
                  r;| j                  r/| j5                  t7        |j8                  fi |¤Ž|||
|||||	|«
      }n ||f|||
|||||	|dœ	|¤Ž}|d   }|sŒm||d   fz  }|
€Œy||d   fz  }Œƒ | j;                  |«      }|r||fz  }t=        ||r|nd|||¬«      S )aÒ  
        Args:
            encoder_hidden_states (`torch.FloatTensor` of shape `(batch_size, encoder_sequence_length, hidden_size)`, *optional*):
                Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention
                of the decoder.
            encoder_attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
                Mask to avoid performing attention on padding indices in `encoder_hidden_states`. Mask values selected in `[0, 1]`:
                - 1 for tokens that are **not masked**,
                - 0 for tokens that are **masked**.
                [What are attention masks?](../glossary#attention-mask)
        Nz:You must specify exactly one of input_ids or inputs_embedszX`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`.Fr   r#   rM  rô   rd   rK  .rL  r¬   rª   )	r`   r  r  r†   r¥   r«   rñ   r¦   r¤   rK   )rO  rl  r<   rP  Úcross_attentions)r.   r«   rH  rñ   rQ  r=  rg   rµ   r¶   rf  r   r   Úget_seq_lengthrD   rV  rT   rÔ   r   Ú_update_causal_maskr7  r´   rU  r   re   r   r;  rW  r   rX  rh  r   )r7   rb  r`   r†   rl  rm  rñ   r«   rH  r¦   r  r  rI  r±   r°   Úpast_seen_tokensrt   r<   r¤   r[  r\  Úall_cross_attentionsrY  rZ  Údecoder_layerr^  s                             r:   r?   zMoonshineDecoder.forward+  s˜  € ð8 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð "+Ð!6‘I¸D¿K¹K×<QÑ<Qˆ	à˜Ð -°tÐ";Ò<ÜÐYÓZÐZà×&Ò&¨4¯=ª=¹YÜ×ÑØjôð ˆIàÐ Ø ×-Ñ-¨iÓ8ˆMá˜Ð0Ü#/£>Ð Ü$0£NÐ!Ü1Ð2FÐH]Ó^ˆOàÐ!ØCRÐC^˜×=Ñ=Ô?ÐdeÐÜ"Ÿ\™\Ø Ð"2°]×5HÑ5HÈÑ5KÑ"KÐTa×ThÑThôˆNð ÐØ)×3Ñ3°AÓ6ˆLà×.Ñ.Ø˜M¨>¸?ÐL]ó
ˆð &ˆð #Ÿo™o¨m¸\ÓJÐñ #7™B¸DÐÙ0™°dˆÙ&7Ð<QÐ<]™rÐdhÐð "Ð-Ø,×2Ñ2°2Ñ6ˆHØ *ÐØ%;¸CÑATÐCTÐATÐ<TÑ%UÐVYÐ[dÐ\dÐ[dÐVdÑ%eÐ"Ø�{‰{×/Ñ/Ð3FÒFØDZÐ^aÑDa×CfÑCfÔChÑ)?ÐnrÑ&ð —‘×1Ñ1°VÒ;ÑDUä)LØ*¨M×,?Ñ,?À×ATÑATÐUWÑAXó*Ñ&ô
 *DØ*¨M×,?Ñ,?À×ATÑATÐUWÑAXó*Ð&ð "Ÿ[™[ò &	@ˆMÙ#Ø! mÐ%5Ñ5Ð!à×*Ò*¨t¯}ª}Ø $× AÑ AÜ˜M×2Ñ2ÑHÐ6GÑHØ!ØØ)Ø Ø#Ø%ØØ"Ø'ó!‘ñ !.Ø!ð!à#.Ø+AØ*?Ø!-Ø#2Ø&7Ø'Ø#1Ø(;ñ!ð (ñ!�ð *¨!Ñ,ˆMâ Ø =°Ñ#3Ð"5Ñ5�à(Ñ4Ø(¨]¸1Ñ-=Ð,?Ñ?Ñ(ðM&	@ðP Ÿ	™	 -Ó0ˆñ  Ø -Ð!1Ñ1Ðä8Ø+Ù/8™O¸dØ+Ø%Ø1ô
ð 	
r;   Úinput_tensorc           
      óÆ  — | j                   j                  dk(  r|�|dk(  j                  «       r|S y | j                   j                  dk(  r7t        |t        j
                  «      rt        |«      }t        |t        «      r|S |�|j                  «       nd}t        |t        «      }| j                   j                  dk(  r(|s&|s$t        j                  |||| j                  ¬«      ry |j                  |j                  }	}|j                  d   }
|r|j!                  «       }n1t        |t        j
                  «      r|j                  d   n||
z   dz   }| j#                  ||
|||	||j                  d   ¬	«      }| j                   j                  dk(  rQ|�O|j                  j$                  d
v r7|s5t	        j&                  |«      j(                  }t        j*                  ||«      }|S )NrL  r¬   Úflex_attentionr   rª   )rm  Úpast_key_values_lengthÚis_trainingr#   rM   )Úsequence_lengthÚtarget_lengthre   rÔ   r¦   Ú
batch_size)ÚcudaÚxpu)r.   r´   rU  rÜ   rD   rE   r&   r%   rp  r   r   Ú_ignore_causal_mask_sdparg   re   rÔ   rT   Úget_max_cache_shapeÚ5_prepare_4d_causal_attention_mask_with_cache_positionrÈ   ÚfinfoÚminÚ_unmask_unattended)r7   r`   ru  r¦   rl  r«   rr  Úusing_static_cachere   rÔ   rz  r{  rt   Ú	min_dtypes                 r:   rq  z$MoonshineDecoder._update_causal_mask¿  sÖ  € ð �;‰;×+Ñ+Ð/BÒBØÐ)¨~ÀÑ/D×.IÑ.IÔ.KØ%Ð%ØØ�;‰;×+Ñ+Ð/?Ò?Ü˜.¬%¯,©,Ô7Ü!<¸^Ó!L�Ü˜.¬)Ô4Ø%Ð%ð
 @OÐ?Z˜?×9Ñ9Ô;Ð`aÐÜ'¨¼ÓEÐð �;‰;×+Ñ+¨vÒ5Ñ>PÑYjÜ%×>Ñ>ØØ*Ø'7Ø ŸM™Mõ	ð à$×*Ñ*¨L×,?Ñ,?ˆvˆØ&×,Ñ,¨QÑ/ˆÙØ+×?Ñ?ÓA‰Mô ˜n¬e¯l©lÔ;ð ×$Ñ$ RÒ(à%¨Ñ7¸!Ñ;ð ð ×PÑPØØ+Ø'ØØØ)Ø#×)Ñ)¨!Ñ,ð Qó 
ˆð �K‰K×,Ñ,°Ò6ØÐ*Ø×%Ñ%×*Ñ*¨oÑ=Ù%ô
 Ÿ™ EÓ*×.Ñ.ˆIÜ0×CÑCÀKÐQZÓ[ˆKàÐr;   rz  r{  re   rÔ   r|  c                 ó˜  — | �| j                  «       dk(  r| }|S t        j                  |«      j                  }	t        j                  ||f|	||¬«      }|dk7  rt        j
                  |d¬«      }|t        j                  ||¬«      |j                  dd«      kD  z  }|dddd…dd…f   j                  |ddd«      }| �Œ|j                  «       }| j                  d   }
|dd…dd…dd…d|
…f   | dd…dddd…f   j                  |j                  «      z   }|dk(  }|dd…dd…dd…d|
…f   j                  ||	«      |dd…dd…dd…d|
…f<   |S )	a°  
        Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape
        `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.

        Args:
            attention_mask (`torch.Tensor`):
                A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape
                `(batch_size, 1, query_length, key_value_length)`.
            sequence_length (`int`):
                The sequence length being processed.
            target_length (`int`):
                The target length: when generating with static cache, the mask should be as long as the static cache,
                to account for the 0 padding, the part of the cache that is not filled yet.
            dtype (`torch.dtype`):
                The dtype to use for the 4D attention mask.
            device (`torch.device`):
                The device to place the 4D attention mask on.
            cache_position (`torch.Tensor`):
                Indices depicting the position of the input sequence tokens in the sequence.
            batch_size (`torch.Tensor`):
                Batch size.
        Né   )Ú
fill_valuere   rÔ   r#   )ÚdiagonalrM  rM   r   )rO   rD   r‚  rƒ  ÚfullÚtriurV  rV   rU   ÚclonerT   rn   rÔ   Úmasked_fill)r`   rz  r{  re   rÔ   r¦   r|  rp   rt   r†  Úmask_lengthÚpadding_masks               r:   r�  zFMoonshineDecoder._prepare_4d_causal_attention_mask_with_cache_position  sy  € ðB Ð%¨.×*<Ñ*<Ó*>À!Ò*Cà(ˆKð* Ðô' Ÿ™ EÓ*×.Ñ.ˆIÜŸ*™*Ø  -Ð0¸YÈeÐ\bôˆKð  !Ò#Ü#Ÿj™j¨¸qÔA�Øœ5Ÿ<™<¨¸fÔEÈ×H^ÑH^Ð_aÐcdÓHeÑeÑeˆKØ% d¨D²!²QÐ&6Ñ7×>Ñ>¸zÈ1ÈbÐRTÓUˆKØÐ)Ø)×/Ñ/Ó1�Ø,×2Ñ2°2Ñ6�Ø*ª1ªa²°L°[°LÐ+@ÑAÀNÒSTÐVZÐ\`ÒbcÐScÑDd×DgÑDgØ×&Ñ&óEñ  �ð  ,¨qÑ0�Ø5@ÂÂAÂqÈ,È;È,ÐAVÑ5W×5cÑ5cØ  )ó6�šAšq¢! \ k \Ð1Ñ2ð Ðr;   )NNNNNNNNNNN)F) rA   rB   rC   r¿   r"  r$   r-   rD  rG  r   r   ÚMOONSHINE_INPUTS_DOCSTRINGr   rD   rÂ   rE   r
   rø   rÁ   r   r   r   r   r   r?   rq  ÚstaticmethodrÀ   re   rÔ   r�  rF   rG   s   @r:   ra  ra    s  ø„ ñ
ð "€Oð˜õ ò !ò"ð Ù*Ð+EÓFð 15Ø15Ø37Ø+/Ø59Ø$(Ø,0Ø/3Ø59Ø=AØ9=ñP
à˜E×,Ñ,Ñ-ðP
ð ! §¡Ñ.ðP
ð ˜u×/Ñ/Ñ0ð	P
ð
 " %™ðP
ð   × 1Ñ 1Ñ2ðP
ð ˜D‘>ðP
ð $ D™>ðP
ð ' t™nðP
ð ! ×!1Ñ!1Ñ2ðP
ð  (¨×(9Ñ(9Ñ:ðP
ð !)¨¯©Ñ 6ðP
ð $Ð$8Ñ9ðP
ð 
ˆuÐ-Ð-Ñ	.òP
ó Gó ðP
ðp #(ñDàŸ™ðDð —l‘lðDð Ÿ™ð	Dð
 ðDð  óDðL ð7ØŸ™ð7àð7ð ð7ð �{‰{ð	7ð
 —‘ð7ð Ÿ™ð7ð ò7ó ô7r;   ra  rT   Ú	mask_probr�  Ú	min_masksc                 óà  ‡‡‡‡‡— | \  }Š‰dk  rt        d«      ‚‰‰kD  rt        d‰› d‰› d�«      ‚t        j                  j                  d«      j	                  «       Šˆˆˆˆˆfd„}|�-|j                  «       j                  d«      j                  «       nt        |«      D �cg c]  }‰‘Œ c}}t        j                  |‰ft        ¬	«      }	g }
 |‰«      }|d
k(  r|	S |D ]¯  } ||«      }t        j                  j                  t        j                  |‰dz
  z
  «      |d¬«      }t        |«      d
k(  r‰dz
  }n|d
   }t        j                  |t        j                  ||z
  t        j                   ¬	«      |z  g«      }|
j#                  |«       Œ± t        j$                  |
«      }
t        j&                  |
dd…dd…df   ||‰f«      }
|
j)                  ||‰z  «      }
t        j                  ‰«      dddd…f   }t        j&                  |||‰f«      j)                  ||‰z  «      }|
|z   }
|
j+                  «       ‰dz
  kD  r‰dz
  |
|
‰dz
  kD  <   t        j,                  |	|
dd«       |	S c c}w )af  
    Computes random mask spans for a given shape. Used to implement [SpecAugment: A Simple Data Augmentation Method for
    ASR](https://arxiv.org/abs/1904.08779). Note that this method is not optimized to run on TPU and should be run on
    CPU as part of the preprocessing during training.

    Args:
        shape: The shape for which to compute masks. This should be of a tuple of size 2 where
               the first element is the batch size and the second element is the length of the axis to span.
        mask_prob:  The percentage of the whole axis (between 0 and 1) which will be masked. The number of
                    independently generated mask spans of length `mask_length` is computed by
                    `mask_prob*shape[1]/mask_length`. Note that due to overlaps, `mask_prob` is an upper bound and the
                    actual percentage will be smaller.
        mask_length: size of the mask
        min_masks: minimum number of masked spans
        attention_mask: A (right-padded) attention mask which independently shortens the feature axis of
                        each batch dimension.
    r#   z&`mask_length` has to be bigger than 0.zO`mask_length` has to be smaller than `sequence_length`, but got `mask_length`: z and `sequence_length`: ú`c                 óœ   •— t        ‰| z  ‰z  ‰z   «      }t        |‰«      }|‰z  ‰kD  r‰‰z  }| ‰dz
  z
  |k  rt        | ‰dz
  z
  d«      }|S )z;Given input length, compute how many spans should be maskedr#   r   )rÀ   Úmax)Úinput_lengthÚnum_masked_spanÚepsilonr�  r“  r”  rz  s     €€€€€r:   Úcompute_num_masked_spanz6_compute_mask_indices.<locals>.compute_num_masked_spanf  so   ø€ ä˜i¨,Ñ6¸ÑDÀwÑNÓOˆÜ˜o¨yÓ9ˆð ˜[Ñ(¨?Ò:Ø-°Ñ<ˆOð ˜;¨™?Ñ+¨oÒ=Ü! ,°+À±/Ñ"BÀAÓFˆOàÐr;   NrM   rÚ   r   F)Úreplace)rQ  ÚnpÚrandomÚrandÚitemÚdetachÚsumÚtolistr9  ÚzerosrÁ   ÚchoicerV  ÚlenÚconcatenateÚonesÚint32ÚappendÚarrayÚbroadcast_torV   r˜  Úput_along_axis)rT   r“  r�  r`   r”  r|  rœ  Ú_r  Úspec_aug_maskÚspec_aug_mask_idxsÚmax_num_masked_spanr™  rš  Úspec_aug_mask_idxÚdummy_mask_idxÚoffsetsr›  rz  s    `` `            @@r:   Ú_compute_mask_indicesr¶  @  s­  ü€ ð0 #(Ñ€J�à�Q‚ÜÐAÓBÐBà�_Ò$ÜØ]Ð^iÐ]jØ& Ð&7°qð:ó
ð 	
ô �i‰i�n‰n˜QÓ×$Ñ$Ó&€G÷ð ð$ Ð%ð 	×ÑÓ×#Ñ# BÓ'×.Ñ.Ô0ä',¨ZÓ'8Ö9 !ŠoÒ9ð ô —H‘H˜j¨/Ð:Ä$ÔG€MØÐá1°/ÓBÐà˜aÒØÐà%ò 5ˆá1°,Ó?ˆô ŸI™I×,Ñ,Ü�I‰I�l k°A¡oÑ6Ó7¸ÐRWð -ó 
Ðô Ð Ó! QÒ&ð -¨qÑ0‰Nà.¨qÑ1ˆNäŸN™NØ¤§¡Ð(;¸oÑ(MÔUW×U]ÑU]Ô ^ÐaoÑ oÐpó
Ðð 	×!Ñ!Ð"3Õ4ð/5ô2 Ÿ™Ð"4Ó5Ðô Ÿ™Øš1ša ˜:Ñ&¨Ð5HÈ+Ð(VóÐð ,×3Ñ3°JÐ@SÐVaÑ@aÓbÐô �i‰i˜Ó$ T¨4² ]Ñ3€GÜ�o‰o˜g¨
Ð4GÈÐ'UÓV×^Ñ^ØÐ'¨+Ñ5ó€Gð ,¨gÑ5Ðð ×ÑÓ /°AÑ"5Ò5ØGVÐYZÑGZÐÐ-°À!Ñ0CÑCÑDô ×Ñ�mÐ%7¸¸BÔ?àÐùòw :s   Â$	I+a�  
    Args:
        input_values (`torch.FloatTensor` of shape `(batch_size, audio_length)`):
            Float values of the raw speech waveform. Raw speech waveform can be
            obtained by loading a `.flac` or `.wav` audio file into an array of type `List[float]` or a
            `numpy.ndarray`, *e.g.* via the soundfile library (`pip install soundfile`). To prepare the array into
            `input_values`, the [`AutoFeatureExtractor`] should be used for padding
            and conversion into a tensor of type `torch.FloatTensor`.
        attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding indices in `input_values`. Mask values selected in `[0, 1]`:
            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.
            [What are attention masks?](../glossary#attention-mask)
        decoder_input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
            it.

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            [What are input IDs?](../glossary#input-ids)
        decoder_attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)

            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
            [`PreTrainedTokenizer.__call__`] for details.

            If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
            `past_key_values`).

            If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
            and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
            information on the default strategy.

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.
        encoder_outputs (`tuple(tuple(torch.FloatTensor)`, *optional*):
            Tuple consists of (`last_hidden_state`, *optional*: `hidden_states`, *optional*: `attentions`)
            `last_hidden_state` of shape `(batch_size, sequence_length, hidden_size)`, *optional*) is a sequence of
            hidden-states at the output of the last layer of the encoder. Used in the cross-attention of the decoder.
        past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
            Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
            blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
            returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.

            Two formats are allowed:
            - a [`~cache_utils.Cache`] instance, see our
            [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache);
            - Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
            shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
            cache format.

            The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
            legacy cache format will be returned.

            If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don't
            have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `decoder_input_ids`
            of shape `(batch_size, sequence_length)`.
        decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `decoder_input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `decoder_input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        decoder_position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.n_positions - 1]`.

            [What are position IDs?](../glossary#position-ids)
        use_cache (`bool`, *optional*):
            If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
            `past_key_values`).
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
        cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
            Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `decoder_position_ids`,
            this tensor is not affected by padding. It is used to update the cache in the correct position and to infer
            the complete sequence length.
c                   ó8  ‡ — e Zd Zdefˆ fd„Zd„ Zd„ Zd„ Zd„ Zd„ Z		 dde
j                  d	ee
j                     fd
„Ze ee«       eee¬«      	 	 	 	 	 	 	 	 	 	 	 	 ddee
j                     d	ee
j                     dee
j                     dee
j                     deeee
j                           deeeee
j                     f      deee
j                        deee
j                        dee   dee   dee   dee
j                     defd„«       «       «       Zˆ xZS )ÚMoonshineModelr.   c                 ó„   •— t         ‰| �  |«       t        |«      | _        t	        |«      | _        | j                  «        y r*   )r,   r-   r*  Úencoderra  Údecoderr>  ©r7   r.   r9   s     €r:   r-   zMoonshineModel.__init__  s2   ø€ Ü‰Ñ˜Ô ä'¨Ó/ˆŒÜ'¨Ó/ˆŒà�‰Õr;   c                 ó.   — | j                   j                  S r*   ©r»  rf  rC  s    r:   rD  z#MoonshineModel.get_input_embeddings  s   € Ø�|‰|×(Ñ(Ð(r;   c                 ó&   — || j                   _        y r*   r¾  rF  s     r:   rG  z#MoonshineModel.set_input_embeddings!  s   € Ø$)ˆ�‰Õ!r;   c                 ó   — | j                   S r*   )rº  rC  s    r:   Úget_encoderzMoonshineModel.get_encoder$  ó   € Ø�|‰|Ðr;   c                 ó   — | j                   S r*   )r»  rC  s    r:   Úget_decoderzMoonshineModel.get_decoder'  rÂ  r;   c                 ó8   — | j                   j                  «        y)z«
        Calling this function will disable the gradient computation for the Moonshine encoder so that its parameters will
        not be updated during training.
        N)rº  Ú_freeze_parametersrC  s    r:   Úfreeze_encoderzMoonshineModel.freeze_encoder*  s   € ð
 	�‰×'Ñ'Õ)r;   Úinput_featuresr`   c                 ó2  — t        | j                  dd«      s|S |j                  «       \  }}}| j                  j                  dkD  r©| j                  r�t        ||f| j                  j                  | j                  j                  || j                  j                  ¬«      }t        j                  ||j                  t        j                  ¬«      }|dd…df   j                  d|d«      }d||<   | j                  j                  dkD  rŽ| j                  r‚t        ||f| j                  j                  | j                  j                  | j                  j                  ¬«      }t        j                  ||j                  t        j                  ¬«      }d||<   |S )	zš
        Masks extracted features along time axis and/or along feature axis according to
        [SpecAugment](https://arxiv.org/abs/1904.08779).
        Úapply_spec_augmentTr   )r“  r�  r`   r”  )rÔ   re   NrM   )r“  r�  r”  )r™   r.   ÚsizeÚmask_time_probrg   r¶  Úmask_time_lengthÚmask_time_min_masksrD   ÚtensorrÔ   rÁ   rU   Úmask_feature_probÚmask_feature_lengthÚmask_feature_min_masks)r7   rÈ  r`   r|  r2   rz  Úmask_time_indicesÚmask_feature_indicess           r:   Ú_mask_input_featuresz#MoonshineModel._mask_input_features1  s[  € ô �t—{‘{Ð$8¸$Ô?Ø!Ð!ð 4B×3FÑ3FÓ3HÑ0ˆ
�K à�;‰;×%Ñ%¨Ò)¨d¯mªmä 5Ø˜_Ð-ØŸ+™+×4Ñ4Ø ŸK™K×8Ñ8Ø-ØŸ+™+×9Ñ9ô!Ðô !&§¡Ð->À~×G\ÑG\Ôdi×dnÑdnÔ oÐØ 1²!°T°'Ñ :× AÑ AÀ"ÀkÐSUÓ VÐØ01ˆNÐ,Ñ-à�;‰;×(Ñ(¨1Ò,°·²ä#8Ø˜[Ð)ØŸ+™+×7Ñ7Ø ŸK™K×;Ñ;ØŸ+™+×<Ñ<ô	$Ð ô $)§<¡<Ð0DÈ^×MbÑMbÔjo×jtÑjtÔ#uÐ Ø34ˆNÐ/Ñ0àÐr;   ©Úoutput_typer   r
  Údecoder_input_idsÚdecoder_attention_maskÚencoder_outputsrl  Údecoder_inputs_embedsÚdecoder_position_idsrñ   r«   rH  r¦   r=   c                 ón  — |
�|
n| j                   j                  }
|�|n| j                   j                  }|	�|	n| j                   j                  }	|€| j	                  |||
|¬«      }nGt        |t        «      s7t        |d   t        |«      dkD  r|d   ndt        |«      dkD  r|d   nd¬«      }| j                  ||||j                  ||||	|
||¬«      }t        |j                  |j                  |j                  |j                  |j                  |j                  |j                  |j                  ¬«      S )	a~  
        Returns:

        Example:

        ```python
        >>> import torch
        >>> from transformers import AutoFeatureExtractor, MoonshineModel
        >>> from datasets import load_dataset

        >>> model = MoonshineModel.from_pretrained("UsefulSensors/moonshine-tiny")
        >>> feature_extractor = AutoFeatureExtractor.from_pretrained("UsefulSensors/moonshine-tiny")
        >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")
        >>> inputs = feature_extractor(ds[0]["audio"]["array"], return_tensors="pt")
        >>> input_values = inputs.input_values
        >>> decoder_input_ids = torch.tensor([[1, 1]]) * model.config.decoder_start_token_id
        >>> last_hidden_state = model(input_values, decoder_input_ids=decoder_input_ids).last_hidden_state
        >>> list(last_hidden_state.shape)
        [1, 2, 288]
        ```N)r`   r«   rH  r   r#   rK   rN  )rb  r`   r  r  rl  rm  r†   rñ   r«   rH  r¦   )rO  rl  Údecoder_hidden_statesÚdecoder_attentionsro  Úencoder_last_hidden_stater  Úencoder_attentions)r.   r«   rH  rñ   rº  rÜ   r   r§  r»  rO  r   rl  r<   rP  ro  )r7   r
  r`   rØ  rÙ  rÚ  rl  rÛ  rÜ  rñ   r«   rH  r¦   Údecoder_outputss                 r:   r?   zMoonshineModel.forward\  s[  € ðL 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð "+Ð!6‘I¸D¿K¹K×<QÑ<Qˆ	àÐ"Ø/3¯|©|ØØ-Ø"3Ø%9ð	 0<ó 0‰Oô ˜O¬_Ô=Ü-Ø"1°!Ñ"4Ü47¸Ó4HÈ1Ò4L˜o¨aÒ0ÐRVÜ14°_Ó1EÈÒ1I˜?¨1Ò-ÈtôˆOð FJÇ\Á\Ø'Ø1Ø#1Ø"1×"CÑ"CØ+Ø/Ø-ØØ/Ø!5Ø)ð FRó F
ˆô "Ø-×?Ñ?Ø+×;Ñ;Ø"1×"?Ñ"?Ø.×9Ñ9Ø,×=Ñ=Ø&5×&GÑ&GØ"1×"?Ñ"?Ø.×9Ñ9ô	
ð 		
r;   r*   )NNNNNNNNNNNN)rA   rB   rC   r$   r-   rD  rG  rÁ  rÄ  rÇ  rD   rø   r   rÂ   rÕ  r   r   Ú MOONSHINE_MODEL_INPUTS_DOCSTRINGr"   r   Ú_CONFIG_FOR_DOCr   r   r   rÁ   r?   rF   rG   s   @r:   r¸  r¸    sÒ  ø„ ð
˜õ ò)ò*òòò*ð 6:ñ)à×)Ñ)ð)ð ! ×!1Ñ!1Ñ2ó)ðV Ù*Ð+KÓLÙÐ+=ÈOÔ\ð 59Ø59Ø8<Ø=AØEIØZ^ØDHØBFØ$(Ø,0Ø/3Ø59ñP
à˜u×0Ñ0Ñ1ðP
ð ! ×!1Ñ!1Ñ2ðP
ð $ E×$4Ñ$4Ñ5ð	P
ð
 !)¨×)9Ñ)9Ñ :ðP
ð " %¨¨e×.?Ñ.?Ñ(@Ñ"AÑBðP
ð " %Ð(;¸UÀ5×CTÑCTÑ=UÐ(UÑ"VÑWðP
ð  (¨¨e×.?Ñ.?Ñ(@ÑAðP
ð ' u¨U×-=Ñ-=Ñ'>Ñ?ðP
ð ˜D‘>ðP
ð $ D™>ðP
ð ' t™nðP
ð ! ×!1Ñ!1Ñ2ðP
ð 
òP
ó ]ó Mó ôP
r;   r¸  rb  rd  Údecoder_start_token_idc                 óÖ   — | j                  | j                  «      }| dd…dd…f   j                  «       |dd…dd…f<   ||dd…df<   |€t        d«      ‚|j	                  |dk(  |«       |S )z1
    Shift input ids one token to the right.
    NrM   r#   r   z1self.model.config.pad_token_id has to be defined.iœÿÿÿ)Ú	new_zerosrT   r�  rQ  Úmasked_fill_)rb  rd  rå  Úshifted_input_idss       r:   Úshift_tokens_rightrê  ²  s}   € ð "×+Ñ+¨I¯O©OÓ<ÐØ(ª¨C¨R¨C¨Ñ0×6Ñ6Ó8Ð’a˜™�eÑØ4Ð’a˜�dÑàÐÜÐLÓMÐMà×"Ñ"Ð#4¸Ñ#<¸lÔKàÐr;   z`The Moonshine Model with a language modeling head. Can be used for automatic speech recognition.c                    ó6  ‡ — e Zd ZdgZdefˆ fd„Zd„ Zd„ Zd„ Zd„ Z	de
j                  fd	„Ze ee«       eee¬
«      	 	 	 	 	 	 	 	 	 	 	 	 	 ddeej*                     deej,                     deej,                     deej,                     deeeej*                           deeeeej*                     f      deeej*                        deeej,                        dee   dee   dee   deej,                     deej,                     defd„«       «       «       Zˆ xZS )Ú!MoonshineForConditionalGenerationzproj_out.weightr.   c                 óÆ   •— t         ‰| �  |«       t        |«      | _        t	        j
                  |j                  |j                  d¬«      | _        | j                  «        y )NFr–   )
r,   r-   r¸  r	  r0   r1   r2   re  Úproj_outr>  r¼  s     €r:   r-   z*MoonshineForConditionalGeneration.__init__É  sH   ø€ Ü‰Ñ˜Ô Ü# FÓ+ˆŒ
ÜŸ	™	 &×"4Ñ"4°f×6GÑ6GÈeÔTˆŒð 	�‰Õr;   c                 ó6   — | j                   j                  «       S r*   )r	  rÁ  rC  s    r:   rÁ  z-MoonshineForConditionalGeneration.get_encoderÑ  ó   € Ø�z‰z×%Ñ%Ó'Ð'r;   c                 ó6   — | j                   j                  «       S r*   )r	  rÄ  rC  s    r:   rÄ  z-MoonshineForConditionalGeneration.get_decoderÔ  rð  r;   c                 ó   — | j                   S r*   ©rî  rC  s    r:   Úget_output_embeddingsz7MoonshineForConditionalGeneration.get_output_embeddings×  s   € Ø�}‰}Ðr;   c                 ó   — || _         y r*   ró  )r7   Únew_embeddingss     r:   Úset_output_embeddingsz7MoonshineForConditionalGeneration.set_output_embeddingsÚ  s	   € Ø&ˆ�r;   r=   c                 ó6   — | j                   j                  «       S r*   )r	  rD  rC  s    r:   rD  z6MoonshineForConditionalGeneration.get_input_embeddingsÝ  s   € Ø�z‰z×.Ñ.Ó0Ð0r;   rÖ  r
  r`   rØ  rÙ  rÚ  rl  rÛ  rÜ  rñ   r«   rH  r¦   Úlabelsc                 óô  — |�9|€7|€5t        || j                  j                  | j                  j                  «      }| j	                  |||||||||	|
||¬«      }| j                  |j                  «      }d}|�(| j                  ||| j                  j                  ¬«      }t        |||j                  |j                  |j                  |j                  |j                  |j                  |j                   ¬«	      S )aK  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the language modeling loss. Indices should either be in `[0, ..., config.vocab_size]`
            or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is
            only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

        Returns:

        Example:

        ```python
        >>> import torch
        >>> from transformers import AutoProcessor, MoonshineForConditionalGeneration
        >>> from datasets import load_dataset

        >>> processor = AutoProcessor.from_pretrained("UsefulSensors/moonshine-tiny")
        >>> model = MoonshineForConditionalGeneration.from_pretrained("UsefulSensors/moonshine-tiny")

        >>> ds = load_dataset("hf-internal-testing/librispeech_asr_dummy", "clean", split="validation")

        >>> inputs = processor(ds[0]["audio"]["array"], return_tensors="pt")
        >>> input_values = inputs.input_values

        >>> generated_ids = model.generate(input_values, max_new_tokens=100)

        >>> transcription = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
        >>> transcription
        'Mr. Quilter is the apostle of the middle classes, and we are glad to welcome his gospel.'
        ```N)r`   rØ  rÚ  rÙ  rl  rÛ  rÜ  rñ   r«   rH  r¦   )Úlogitsrù  re  )	Úlossrû  rl  rÞ  rß  ro  rà  r  rá  )rê  r.   rd  rå  r	  rî  rO  Úloss_functionre  r   rl  rÞ  rß  ro  rà  r  rá  )r7   r
  r`   rØ  rÙ  rÚ  rl  rÛ  rÜ  rñ   r«   rH  r¦   rù  r÷   rû  rü  s                    r:   r?   z)MoonshineForConditionalGeneration.forwardà  s
  € ðb ÐØ Ð(Ð-BÐ-JÜ$6Ø˜DŸK™K×4Ñ4°d·k±k×6XÑ6Xó%Ð!ð '+§j¡jØØ)Ø/Ø+Ø#9Ø+Ø"7Ø!5ØØ/Ø!5Ø)ð '1ó '
ˆð —‘˜w×8Ñ8Ó9ˆàˆØÐØ×%Ñ%¨V¸FÈtÏ{É{×OeÑOeÐ%ÓfˆDäØØØ#×3Ñ3Ø")×"?Ñ"?Ø&×9Ñ9Ø$×5Ñ5Ø&-×&GÑ&GØ")×"?Ñ"?Ø&×9Ñ9ô

ð 
	
r;   )NNNNNNNNNNNNN)rA   rB   rC   Ú_tied_weights_keysr$   r-   rÁ  rÄ  rô  r÷  r0   r_  rD  r   r   rã  r"   r   rä  r   rD   rø   rÂ   r   r   r   rÁ   r?   rF   rG   s   @r:   rì  rì  Â  sÒ  ø„ ð
 ,Ð,Ðð˜õ ò(ò(òò'ð1 b§i¡ió 1ð Ù*Ð+KÓLÙ¨?ÈÔYð 59Ø59Ø8<Ø=AØEIØZ^ØDHØBFØ$(Ø,0Ø/3Ø59Ø-1ñR
à˜u×0Ñ0Ñ1ðR
ð ! ×!1Ñ!1Ñ2ðR
ð $ E×$4Ñ$4Ñ5ð	R
ð
 !)¨×)9Ñ)9Ñ :ðR
ð " %¨¨e×.?Ñ.?Ñ(@Ñ"AÑBðR
ð " %Ð(;¸UÀ5×CTÑCTÑ=UÐ(UÑ"VÑWðR
ð  (¨¨e×.?Ñ.?Ñ(@ÑAðR
ð ' u¨U×-=Ñ-=Ñ'>Ñ?ðR
ð ˜D‘>ðR
ð $ D™>ðR
ð ' t™nðR
ð ! ×!1Ñ!1Ñ2ðR
ð ˜×)Ñ)Ñ*ðR
ð 
òR
ó Zó Mó ôR
r;   rì  )r¸  r  rì  )r¬   )Nr#   )Nr   )UÚ	functoolsr   Útypingr   r   r   r   Únumpyrž  rD   Útorch.nnr0   Úactivationsr	   Úcache_utilsr
   r   r   r   Ú
generationr   Úmodeling_attn_mask_utilsr   r   r   Úmodeling_flash_attention_utilsr   Úmodeling_outputsr   r   r   r   r   Úmodeling_rope_utilsr   r   Úmodeling_utilsr   r   Úprocessing_utilsr   Úutilsr   r   r   r    r!   r"   Úconfiguration_moonshiner$   Ú!torch.nn.attention.flex_attentionr%   Úintegrations.flex_attentionr&   Ú
get_loggerrA   rµ   rä  r_  r(   rI   rE   rÀ   r[   rÛ   rv   r}   r�   r‘   rÄ   rå   rú   ÚMOONSHINE_START_DOCSTRINGr  r*  r‘  ra  rÂ   Úndarrayr¶  rã  r¸  rê  rì  Ú__all__rô   r;   r:   ú<module>r     sà  ðõ* ß 3Ó 3ã Û Ý å !ß PÓ PÝ )÷ñ õ
 C÷õ ÷ Lß FÝ &÷÷ õ 5ñ  Ô!Ý;åJð 
ˆ×	Ñ	˜HÓ	%€Ø#€ô˜"Ÿ)™)ô ô˜"Ÿ)™)ô ð 	U˜UŸ\™\ð 	U°#ð 	U¸%¿,¹,ó 	Uð& ñ%Ø�I‰Ið%à�<‰<ð%ð 
�‰ð%ð �<‰<ð	%ð
 ˜UŸ\™\Ñ*ð%ð ð%ð ó%ò46ó'ôTC)˜Ÿ™ô C)ôL<˜rŸy™yô <ôD9˜BŸI™Iô 9ôxU˜BŸI™Iô UðpÐ ñ" Ø]Øóô#˜ó #ó	ð#ôBU
Ð/ô U
ðp@Ð ñF Ø]ØóôrÐ/ó ró	ðrðr	 26ØñtØ��c�‰?ðtàðtð ðtð ˜U×-Ñ-Ñ.ð	tð
 ðtð ‡Z�ZótðnW$Ð  ñt Ø]ØóôZ
Ð-ó Z
ó	ðZ
ðz %§,¡,ð ¸cð Ð[^ó ñ  ØfØóôo
Ð(@À/ó o
ó	ðo
òd ^�r;   