Ë
    ÜÓ^jëd ã                   óÖ  — d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	Z	ddl
mZ ddl
mZ ddlmZ dd	l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 ddl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, ddl-m.Z. ddl/m0Z0 ddl1m2Z2m3Z3  e,jh                  e5«      Z6 e+d¬«      e G d„ de«      «       «       Z7 e+d¬«      e G d„ de«      «       «       Z8 e+d¬«      e G d „ d!e«      «       «       Z9e+e G d"„ d#e«      «       «       Z: G d$„ d%ejv                  «      Z< G d&„ d'ejv                  «      Z= G d(„ d)ejv                  «      Z> G d*„ d+ejv                  «      Z?d,„ Z@dNd-„ZA G d.„ d/ejv                  «      ZBd0e	j†                  d1eDd2e	j†                  fd3„ZE	 dOd4ejv                  d5e	j†                  d6e	j†                  d7e	j†                  d8e	j†                  dz  d9eFd:eFd;e(e*   fd<„ZG G d=„ d>ejv                  «      ZH G d?„ d@e«      ZIe+ G dA„ dBe&«      «       ZJ G dC„ dDeJe«      ZKe+ G dE„ dFeJ«      «       ZL e+dG¬«       G dH„ dIeJe«      «       ZM e+dJ¬«       G dK„ dLeJe«      «       ZNg dM¢ZOy)PzPyTorch Moshi model.é    N)ÚCallable)Ú	dataclass)ÚAnyÚOptional)ÚCrossEntropyLossé   )Úinitialization)ÚACT2FN)ÚCacheÚDynamicCache)ÚGenerationConfigÚGenerationMixin)Úcreate_causal_mask)ÚGradientCheckpointingLayer)ÚBaseModelOutputWithPastÚCausalLMOutputWithPastÚModelOutputÚSeq2SeqLMOutput)ÚROPE_INIT_FUNCTIONSÚdynamic_rope_update)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedModel)ÚUnpack)ÚTransformersKwargsÚauto_docstringÚlogging)Úmaybe_autocasté   )Ú	AutoModelé   )ÚMoshiConfigÚMoshiDepthConfigzK
    Outputs of [`MoshiForConditionalConditionalGeneration.generate`].
    )Úcustom_introc                   ó²  — e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ
ej                  dz  ed<   dZeej                     dz  ed<   dZeej                     dz  ed<   dZej                  dz  ed<   dZeeej                        dz  ed	<   dZeeej                        dz  ed
<   dZedz  ed<   dZej                  dz  ed<   y)Ú(MoshiConditionalGenerationGenerateOutputaÞ  
    audio_sequences (`torch.LongTensor` of shape `(batch_size*num_return_sequences, 1, sequence_length)`, *optional*):
        The generated audio waveforms.
    sequences (`torch.LongTensor` of shape `(batch_size*num_return_sequences, sequence_length)`):
        The generated text sequences. The second dimension (sequence_length) is either equal to `max_length` or shorter
        if all batches finished early due to the `eos_token_id`.
    sequences_scores (`torch.FloatTensor` of shape `(batch_size*num_return_sequences)`, *optional*, returned when `output_scores=True`):
        Final beam scores of the generated `sequences`.
    scores (`tuple(torch.FloatTensor)` *optional*, returned when `output_scores=True`):
        Beam transition scores for each vocabulary token at each generation step. Beam transition scores consisting
        of log probabilities of tokens conditioned on log softmax of previously generated tokens in this beam.
        Tuple of `torch.FloatTensor` with up to `max_new_tokens` elements (one element for each generated token),
        with each tensor of shape `(batch_size*num_beams, config.vocab_size)`.
    logits (`tuple(torch.FloatTensor)` *optional*, returned when `output_logits=True`):
        Unprocessed prediction scores of the language modeling head (scores for each vocabulary token before SoftMax)
        at each generation step. Tuple of `torch.FloatTensor` with up to `max_new_tokens` elements (one element for
        each generated token), with each tensor of shape `(batch_size, config.vocab_size)`.
    beam_indices (`torch.LongTensor`, *optional*, returned when `output_scores=True`):
        Beam indices of generated token id at each generation step. `torch.LongTensor` of shape
        `(batch_size*num_return_sequences, sequence_length)`.
    attentions (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `output_attentions=True`):
        Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) of
        `torch.FloatTensor` of shape `(batch_size*num_beams, num_heads, generated_length, sequence_length)`.
    hidden_states (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `output_hidden_states=True`):
        Tuple (one element for each generated token) of tuples (one element for each layer of the decoder) of
        `torch.FloatTensor` of shape `(batch_size*num_beams*num_return_sequences, generated_length, hidden_size)`.
    past_key_values (`Cache`, *optional*, returned when `use_cache=True`):
        Contains the model cache, used to speed up decoding. Different models have a different cache format, check
        the model's documentation. Usually, a [`~cache_utils.Cache`] instance.
    audio_codes (`torch.LongTensor` of shape `(batch_size*num_return_sequences, num_codeooks, sequence_length)`, *optional*):
        The generated audio codes. Returned if `return_audio_codes=True`. Intermediate audio "tokens" which transforms to `audio_sequences` once passed through the audio decoder.
    NÚaudio_sequencesÚ	sequencesÚsequences_scoresÚscoresÚlogitsÚbeam_indicesÚ
attentionsÚhidden_statesÚpast_key_valuesÚaudio_codes)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r&   ÚtorchÚTensorÚ__annotations__r'   Ú
LongTensorr(   ÚFloatTensorr)   Útupler*   r+   r,   r-   r.   r   r/   © ó    ús/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/moshi/modeling_moshi.pyr%   r%   ,   sò   … ñðB ,0€O�U—\‘\ DÑ(Ó/Ø)-€Iˆu×Ñ $Ñ&Ó-Ø15Ð�e×'Ñ'¨$Ñ.Ó5Ø.2€FˆE�%×#Ñ#Ñ$ tÑ+Ó2Ø.2€FˆE�%×#Ñ#Ñ$ tÑ+Ó2Ø,0€L�%×"Ñ" TÑ)Ó0Ø9=€J��e˜E×-Ñ-Ñ.Ñ/°$Ñ6Ó=Ø<@€M�5˜˜u×0Ñ0Ñ1Ñ2°TÑ9Ó@Ø$(€O�U˜T‘\Ó(Ø+/€K�×!Ñ! DÑ(Ô/r;   r%   z%
    `MoshiForCausalLM` outputs.
    c                   ó  — e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	ej                  dz  ed<   dZ
edz  ed<   dZeej                  df   dz  ed<   dZeej                  df   dz  ed	<   y)
ÚMoshiCausalLMOutputWithPasta%  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
        Language modeling loss (for next-token prediction).
    logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
        Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
    past_key_values (`Cache`, *optional*, returned when `use_cache=True` is passed or when `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).

        Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see
        `past_key_values` input) to speed up sequential decoding.
    NÚlossr*   Úlast_hidden_stater.   .r-   r,   )r0   r1   r2   r3   r?   r4   r8   r6   r*   r@   r.   r   r-   r9   r,   r:   r;   r<   r>   r>   `   s•   … ñ
ð &*€Dˆ%×
Ñ
˜dÑ
"Ó)Ø'+€FˆE×Ñ Ñ$Ó+Ø26Ð�u×(Ñ(¨4Ñ/Ó6Ø$(€O�U˜T‘\Ó(Ø:>€M�5˜×*Ñ*¨CÐ/Ñ0°4Ñ7Ó>Ø7;€J��e×'Ñ'¨Ð,Ñ-°Ñ4Ô;r;   r>   z2
    `MoshiForConditionalGeneration` outputs.
    c                   óÊ  — e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	ej                  dz  ed<   dZ
edz  ed<   dZeej                  df   dz  ed<   dZeej                  df   dz  ed	<   dZej                  dz  ed
<   dZej                  dz  ed<   dZedz  ed<   dZeej                  df   dz  ed<   dZeej                  df   dz  ed<   y)Ú(MoshiConditionalGenerationOutputWithPasta'  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `text_labels` is provided):
        Text language modeling loss (for next-token prediction).
    logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
        Prediction scores of the text language modeling head (scores for each vocabulary token before SoftMax).
    past_key_values (`Cache`, *optional*, returned when `use_cache=True` is passed or when `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).

        Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see
        `past_key_values` input) to speed up sequential decoding.
    depth_loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `audio_labels` is provided):
        Audio language modeling loss (for next-token prediction).
    audio_logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
        Prediction scores of the audio language modeling heads.
    depth_past_key_values (`Cache`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
        Past key-values of the depth decoder.
    depth_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Hidden states of the depth decoder
    depth_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
        Depth decoder's Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
        heads.
    Nr?   r*   r@   r.   .r-   r,   Ú
depth_lossÚaudio_logitsÚdepth_past_key_valuesÚdepth_hidden_statesÚdepth_attentions)r0   r1   r2   r3   r?   r4   r8   r6   r*   r@   r.   r   r-   r9   r,   rC   rD   rE   rF   rG   r:   r;   r<   rB   rB   {   s  … ñð. &*€Dˆ%×
Ñ
˜dÑ
"Ó)Ø'+€FˆE×Ñ Ñ$Ó+Ø26Ð�u×(Ñ(¨4Ñ/Ó6Ø$(€O�U˜T‘\Ó(Ø:>€M�5˜×*Ñ*¨CÐ/Ñ0°4Ñ7Ó>Ø7;€J��e×'Ñ'¨Ð,Ñ-°Ñ4Ó;Ø+/€J�×!Ñ! DÑ(Ó/Ø-1€L�%×#Ñ# dÑ*Ó1Ø*.Ð˜5 4™<Ó.Ø@DÐ˜˜u×0Ñ0°#Ð5Ñ6¸Ñ=ÓDØ=AÐ�e˜E×-Ñ-¨sÐ2Ñ3°dÑ:ÔAr;   rB   c                   ó²   — e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ
ej                  dz  ed<   dZej                  dz  ed<   y)ÚMoshiUnconditionalInputa)  
    input_ids (`torch.Tensor `of shape `(batch_size, sequence_length), *optional*):
        The sequence used as a text prompt for the generation.
    user_audio_codes (`torch.Tensor `of shape `(batch_size, num_codebooks, sequence_length), *optional*):
        The audio codes used as audio user prompt for the generation. Has priority over `user_input_values` and represents the audio "tokens" of `user_input_values` once passed through the audio encoder.
    moshi_audio_codes (`torch.Tensor `of shape `(batch_size, num_codebooks, sequence_length), *optional*):
        The audio codes used as audio Moshi prompt for the generation. Has priority over `moshi_input_values` and represents the audio "tokens" of `moshi_input_values` once passed through the audio encoder.
    attention_mask (`torch.LongTensor`)  of shape `(batch_size, sequence_length)`, *optional*):
        Attention 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**.
    NÚ	input_idsÚuser_audio_codesÚmoshi_audio_codesÚattention_mask)r0   r1   r2   r3   rJ   r4   r7   r6   rK   r5   rL   rM   r:   r;   r<   rI   rI   ¦   s[   … ñ
ð *.€Iˆu×Ñ $Ñ&Ó-Ø,0Ð�e—l‘l TÑ)Ó0Ø-1Ð�u—|‘| dÑ*Ó1Ø.2€N�E×$Ñ$ tÑ+Ô2r;   rI   c                   ó<   ‡ — e Zd Zddedefˆ fd„Zd„ Zd„ Zd„ Zˆ xZ	S )ÚMoshiRMSNormÚdimÚepsc                 óŠ   •— t         ‰| �  «        || _        t        j                  t        j                  |«      «      | _        y ©N)ÚsuperÚ__init__rQ   ÚnnÚ	Parameterr4   ÚonesÚweight)ÚselfrP   rQ   Ú	__class__s      €r<   rU   zMoshiRMSNorm.__init__½   s-   ø€ Ü‰ÑÔØˆŒÜ—l‘l¤5§:¡:¨c£?Ó3ˆ�r;   c                 óŒ   — |t        j                  |j                  d«      j                  dd¬«      | j                  z   «      z  S )Nr   éÿÿÿÿT)Úkeepdim)r4   ÚrsqrtÚpowÚmeanrQ   )rZ   Úxs     r<   Ú_normzMoshiRMSNorm._normÂ   s4   € Ø”5—;‘;˜qŸu™u Q›xŸ}™}¨R¸˜}Ó>ÀÇÁÑIÓJÑJÐJr;   c                 óœ   — | j                  |j                  «       «      }|| j                  j                  «       z  }|j                  |«      S rS   )rc   ÚfloatrY   Útype_as)rZ   rb   Úoutputs      r<   ÚforwardzMoshiRMSNorm.forwardÆ   s<   € Ø—‘˜AŸG™G›IÓ&ˆØ˜$Ÿ+™+×+Ñ+Ó-Ñ-ˆØ�~‰~˜aÓ Ð r;   c                 ó^   — t        | j                  j                  «      › d| j                  › �S )Nz, eps=)r9   rY   ÚshaperQ   ©rZ   s    r<   Ú
extra_reprzMoshiRMSNorm.extra_reprË   s'   € Ü˜Ÿ™×)Ñ)Ó*Ð+¨6°$·(±(°Ð<Ð<r;   )g�íµ ÷Æ°>)
r0   r1   r2   Úintre   rU   rc   rh   rl   Ú__classcell__©r[   s   @r<   rO   rO   ¼   s&   ø„ ñ4˜Cð 4 eõ 4ò
Kò!ö
=r;   rO   c                   ó&   ‡ — e Zd Zˆ fd„Zdd„Zˆ xZS )ÚMoshiFlexibleLinearc                 ó€   •— t         ‰| �  «        t        j                  t	        j
                  |||«      «      | _        y rS   )rT   rU   rV   rW   r4   ÚrandnrY   )rZ   Ú
input_sizeÚoutput_sizeÚ
num_layersr[   s       €r<   rU   zMoshiFlexibleLinear.__init__Ð   s+   ø€ Ü‰ÑÔä—l‘l¤5§;¡;¨z¸;È
Ó#SÓTˆ�r;   c                 ó  — |�!t        j                  | j                  d|«      n| j                  }|j                  dd«      ddd…dd…dd…f   }t        j                  |dd…dd…ddd…f   |«      }|j                  d«      S )aå  
        `MoshiFlexibleLinear` creates one linear layer per codebook. There's multiple ways to use it.
        In the default case, `sequence_length=num_layers`, so each element of the sequence will be matmul to the weights corresponding to its index on the sequence.

        For more advanced cases, one can specify which codebook's layer(s) to use with `layer_idx`.
        If `layer_idx` indicates a single integer, all of the element of the sequence will be matmul to this single codebook's layer.
        But if `layer_idx` is a tensor of shape `(seq_length,)`, it will matmul each i-th element of the input sequence to the corresponding layer `weight[i]`.


        Args:
            x (`torch.FloatTensor): input to the layer of shape `(batch, num_layers, embed_dim)` or of shape `(batch, seq_length, embed_dim)`
            layer_idx (`torch.Tensor`, *optional*):
                Can be used to specify which codebook's layers(s) to use.
                If it's a tensor of shape `(seq_length,)`, will matmul each element of the sequence to the corresponding weights.
                But if `layer_idx` is a tensor of shape `(seq_length,)`, it will matmul each i-th element of the input sequence to the corresponding layer `weight[i]`.
        Nr   r    r   )r4   Úindex_selectrY   Ú	transposeÚmatmulÚsqueeze)rZ   rb   Ú	layer_idxÚselected_weightss       r<   rh   zMoshiFlexibleLinear.forwardÕ   s   € ð( MVÐLaœ5×-Ñ-¨d¯k©k¸1¸iÔHÐgk×grÑgrÐð ,×5Ñ5°a¸Ó;¸DÂ!ÂQÊ¸MÑJÐô �L‰L˜š1ša ¢q˜=Ñ)Ð+;Ó<ˆð �y‰y˜‹|Ðr;   rS   ©r0   r1   r2   rU   rh   rn   ro   s   @r<   rq   rq   Ï   s   ø„ ôU÷
r;   rq   c                   ó(   ‡ — e Zd Zdˆ fd„	Zdd„Zˆ xZS )ÚMoshiLinearc                 ó–   •— t         ‰| �  «        || _        |st        j                  ||d¬«      | _        y t        |||¬«      | _        y )NF©Úbias)rv   )rT   rU   Úuse_flexible_linearrV   ÚLinearÚlinearrq   )rZ   Ú	input_dimÚ
output_dimÚnum_codebooksr„   r[   s        €r<   rU   zMoshiLinear.__init__÷   s>   ø€ Ü‰ÑÔà#6ˆÔ á"ÜŸ)™) I¨zÀÔFˆD�Kä-¨i¸ÐP]Ô^ˆD�Kr;   c                 ó`   — | j                   r| j                  ||«      S | j                  |«      S rS   )r„   r†   )rZ   rb   r|   s      r<   rh   zMoshiLinear.forward  s*   € Ø×#Ò#Ø—;‘;˜q )Ó,Ð,à—;‘;˜q“>Ð!r;   ©FrS   r~   ro   s   @r<   r€   r€   ö   s   ø„ õ_÷"r;   r€   c                   óÄ   ‡ — e Zd ZU ej                  ed<   ddefˆ fd„Ze	 	 	 ddedz  de	d   de
dz  ded	ef   fd
„«       Z ej                  «       ed„ «       «       Zˆ xZS )ÚMoshiRotaryEmbeddingÚinv_freqNÚconfigc                 ó®  •— t         ‰| �  «        |j                  | _        |j                  | _        || _        | j
                  j                  d   | _        | j                  }| j                  dk7  rt        | j                     } || j
                  |«      \  }| _
        | j                  d|d¬«       | j                  d|j                  «       d¬«       y )NÚ	rope_typeÚdefaultrŽ   F)Ú
persistentÚoriginal_inv_freq)rT   rU   Úmax_position_embeddingsÚmax_seq_len_cachedÚoriginal_max_seq_lenr�   Úrope_parametersr‘   Úcompute_default_rope_parametersr   Úattention_scalingÚregister_bufferÚclone)rZ   r�   ÚdeviceÚrope_init_fnrŽ   r[   s        €r<   rU   zMoshiRotaryEmbedding.__init__  s¶   ø€ Ü‰ÑÔØ"(×"@Ñ"@ˆÔØ$*×$BÑ$BˆÔ!àˆŒàŸ™×4Ñ4°[ÑAˆŒØ!%×!EÑ!EˆØ�>‰>˜YÒ&Ü.¨t¯~©~Ñ>ˆLÙ+7¸¿¹ÀVÓ+LÑ(ˆ�$Ô(à×Ñ˜Z¨¸eÐÔDØ×ÑÐ0°(·.±.Ó2BÈuÐÕUr;   r�   ztorch.deviceÚseq_lenÚreturnztorch.Tensorc                 ó  — | j                   d   }t        | dd«      xs | j                  | j                  z  }d}d|t	        j
                  d|dt        j                  ¬«      j                  |t        j                  ¬«      |z  z  z  }||fS )	a¨  
        Computes the inverse frequencies according to the original RoPE implementation
        Args:
            config ([`~transformers.PreTrainedConfig`]):
                The model configuration.
            device (`torch.device`):
                The device to use for initialization of the inverse frequencies.
            seq_len (`int`, *optional*):
                The current sequence length. Unused for this type of RoPE.
        Returns:
            Tuple of (`torch.Tensor`, `float`), containing the inverse frequencies for the RoPE embeddings and the
            post-processing scaling factor applied to the computed cos/sin (unused in this type of RoPE).
        Ú
rope_thetaÚhead_dimNg      ð?r   r   ©Údtype©r�   r¥   )	r˜   ÚgetattrÚhidden_sizeÚnum_attention_headsr4   ÚarangeÚint64Útore   )r�   r�   rŸ   ÚbaserP   Úattention_factorrŽ   s          r<   r™   z4MoshiRotaryEmbedding.compute_default_rope_parameters  s�   € ð& ×%Ñ% lÑ3ˆÜ�f˜j¨$Ó/Òc°6×3EÑ3EÈ×IcÑIcÑ3cˆàÐð Ø”U—\‘\ ! S¨!´5·;±;Ô?×BÑBÈ&ÔX]×XcÑXcÐBÓdÐgjÑjÑkñ
ˆð Ð)Ð)Ð)r;   c                 óN  — | 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        |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   r]   r    ÚmpsÚcpuF)Údevice_typeÚenabledr   ©rP   r¤   )rŽ   re   Úexpandrj   r¬   r�   Ú
isinstanceÚtypeÚstrr   ry   r4   ÚcatÚcosrš   Úsinr¥   )
rZ   rb   Úposition_idsÚinv_freq_expandedÚposition_ids_expandedr²   ÚfreqsÚembrº   r»   s
             r<   rh   zMoshiRotaryEmbedding.forward:  sR  € ð !Ÿ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$rS   )NNN)r0   r1   r2   r4   r5   r6   r!   rU   Ústaticmethodr   rm   r9   re   r™   Úno_gradr   rh   rn   ro   s   @r<   r�   r�   	  s˜   ø… Ø�l‰lÓñV˜{õ Vð  à%)Ø+/Ø"ñ*Ø˜dÑ"ð*à˜Ñ(ð*ð �t‘ð*ð 
ˆ~˜uÐ$Ñ	%ò	*ó ð*ð: €U‡]�]ƒ_Øñ<ó ó ô<r;   r�   c                 óš   — | dd| j                   d   dz  …f   }| d| j                   d   dz  d…f   }t        j                  | |fd¬«      S )z*Rotates half the hidden dims of the input..Nr]   r   r´   )rj   r4   r¹   )rb   Úx1Úx2s      r<   Úrotate_halfrÆ   K  sZ   € à	
ˆ3Ð"�!—'‘'˜"‘+ Ñ"Ð"Ð"Ñ	#€BØ	
ˆ3�—‘˜‘˜qÑ Ñ"Ð"Ñ	#€BÜ�9‰9�r�c˜2�Y BÔ'Ð'r;   c                 óž   — |j                  |«      }|j                  |«      }| |z  t        | «      |z  z   }||z  t        |«      |z  z   }||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.
        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.
    )Ú	unsqueezerÆ   )ÚqÚkrº   r»   Úunsqueeze_dimÚq_embedÚk_embeds          r<   Úapply_rotary_pos_embrÎ   S  sY   € ð$ �-‰-˜Ó
&€CØ
�-‰-˜Ó
&€CØ�3‰wœ; q›>¨CÑ/Ñ0€GØ�3‰wœ; q›>¨CÑ/Ñ0€GØ�GÐÐr;   c                   ód   ‡ — e Zd Zdˆ fd„	Zddej
                  dedz  dej
                  fd„Zˆ xZS )	ÚMoshiGatingMLPc                 óv  •— t         ‰| �  «        t        |j                     | _        |j
                  }|j                  }|r|j                  nd}|dk(  r>t        j                  ||d¬«      | _
        t        j                  |dz  |d¬«      | _        y t        |||«      | _
        t        |dz  ||«      | _        y )Nr    Fr‚   r   )rT   rU   r
   Ú
hidden_actÚactivation_fnÚffn_dimr¨   r‰   rV   r…   Úfc1Úfc2rq   )rZ   r�   r„   rÔ   r¨   rv   r[   s         €r<   rU   zMoshiGatingMLP.__init__m  s›   ø€ Ü‰ÑÔä# F×$5Ñ$5Ñ6ˆÔØ—.‘.ˆØ×(Ñ(ˆÙ-@�V×)Ò)Àaˆ
Ø˜Š?Ü—y‘y ¨g¸EÔBˆDŒHÜ—y‘y ¨A¡¨{ÀÔGˆD�Hä*¨;¸ÀÓLˆDŒHÜ*¨7°a©<¸ÀjÓQˆD�Hr;   Nr-   r|   r    c                 ó.  — |€| j                  |«      n| j                  ||«      }|j                  \  }}}|j                  ||dd«      }| j                  |ddd d …f   «      |ddd d …f   z  }|€| j	                  |«      }|S | j	                  ||«      }|S )Nr   r]   .r   r    )rÕ   rj   ÚviewrÓ   rÖ   )rZ   r-   r|   Ú
batch_sizeÚsequence_lengthÚ_s         r<   rh   zMoshiGatingMLP.forward{  s°   € Ø3<Ð3D˜Ÿ™ Ô/È$Ï(É(ÐS`ÐbkÓJlˆà)6×)<Ñ)<Ñ&ˆ
�O QØ%×*Ñ*¨:°ÈÈ2ÓNˆØ×*Ñ*¨=¸¸aÂ¸Ñ+CÓDÀ}ÐUXÐZ[Ò]^ÐU^ÑG_Ñ_ˆØ3<Ð3D˜Ÿ™ Ó/ˆØÐð KOÏ(É(ÐS`ÐbkÓJlˆØÐr;   r‹   rS   )	r0   r1   r2   rU   r4   r5   rm   rh   rn   ro   s   @r<   rÐ   rÐ   l  s2   ø„ õRñ U§\¡\ð ¸cÀD¹jð ÐTY×T`ÑT`÷ r;   rÐ   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)rj   rµ   Úreshape)r-   rÜ   ÚbatchÚnum_key_value_headsÚslenr£   s         r<   Ú	repeat_kvrâ   †  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ÚvaluerM   ÚscalingÚdropoutÚkwargsc                 ó  — t        || j                  «      }t        || j                  «      }	t        j                  ||j	                  dd«      «      |z  }
|�|
|z   }
t
        j                  j                  |
dt        j                  ¬«      j                  |j                  «      }
t
        j                  j                  |
|| j                  ¬«      }
t        j                  |
|	«      }|j	                  dd«      j                  «       }||
fS )Nr   r   r]   )rP   r¥   )ÚpÚtrainingr    )râ   Únum_key_value_groupsr4   rz   ry   rV   Ú
functionalÚsoftmaxÚfloat32r¬   r¥   rè   rì   Ú
contiguous)rã   rä   rå   ræ   rM   rç   rè   ré   Ú
key_statesÚvalue_statesÚattn_weightsÚattn_outputs               r<   Úeager_attention_forwardrö   “  sà   € ô ˜3 × ;Ñ ;Ó<€JÜ˜U F×$?Ñ$?Ó@€Lä—<‘<  z×';Ñ';¸A¸qÓ'AÓBÀWÑL€LØÐ!Ø# nÑ4ˆä—=‘=×(Ñ(¨¸2ÄUÇ]Á]Ð(ÓS×VÑVÐW\×WbÑWbÓc€LÜ—=‘=×(Ñ(¨¸È6Ï?É?Ð(Ó[€LÜ—,‘,˜|¨\Ó:€KØ×'Ñ'¨¨1Ó-×8Ñ8Ó:€Kà˜Ð$Ð$r;   c                   ó  ‡ — e Zd ZdZddededz  fˆ fd„Z	 	 	 	 ddej                  dej                  dz  dej                  dz  d	e
dz  d
ej                  dz  dee   deej                  ej                  f   fd„Zˆ xZS )ÚMoshiAttentionz=Multi-headed attention from 'Attention Is All You Need' paperNr�   r|   c                 ó(  •— t         ‰| �  «        || _        || _        |j                  | _        |j
                  | _        |j                  | _        |j                  | _        |j                  | _	        | j                  | j                  z  | _
        d| _        dt        j                  | j                  «      z  | _        | j
                  | j                  z  dk7  r&t        d| j
                  › d| j                  › d�«      ‚t!        | j
                  | j                  | j                  z  |j"                  |«      | _        t!        | j
                  | j                  | j                  z  |j"                  |«      | _        t!        | j
                  | j                  | j                  z  |j"                  |«      | _        t!        | j                  | j                  z  | j
                  |j"                  |«      | _        d | _        |rt/        |«      | _        y y )NTr    r   z?hidden_size must be divisible by num_heads (got `hidden_size`: z and `num_heads`: z).)rT   rU   r�   r|   Úattention_dropoutr¨   r©   Ú	num_headsr£   rà   rí   Ú	is_causalÚmathÚsqrtrç   Ú
ValueErrorr€   r‰   Úq_projÚk_projÚv_projÚo_projÚ
rotary_embr�   ©rZ   r�   r|   r„   Úuse_roper[   s        €r<   rU   zMoshiAttention.__init__¯  sÀ  ø€ Ü‰ÑÔØˆŒØ"ˆŒà!'×!9Ñ!9ˆÔØ!×-Ñ-ˆÔØ×3Ñ3ˆŒØŸ™ˆŒØ#)×#=Ñ#=ˆÔ Ø$(§N¡N°d×6NÑ6NÑ$NˆÔ!ØˆŒØœ4Ÿ9™9 T§]¡]Ó3Ñ3ˆŒà×Ñ˜dŸn™nÑ,°Ò1ÜØQÐRV×RbÑRbÐQcØ$ T§^¡^Ð$4°Bð8óð ô
 "Ø×Ñ˜dŸn™n¨t¯}©}Ñ<¸f×>RÑ>RÐTgó
ˆŒô "Ø×Ñ˜d×6Ñ6¸¿¹ÑFÈ×H\ÑH\Ð^qó
ˆŒô "Ø×Ñ˜d×6Ñ6¸¿¹ÑFÈ×H\ÑH\Ð^qó
ˆŒô "Ø�N‰N˜TŸ]™]Ñ*¨D×,<Ñ,<¸f×>RÑ>RÐTgó
ˆŒð
 ˆŒÙÜ2°6Ó:ˆD�Oð r;   r-   rM   r¼   r.   Úcodebook_idxré   r    c                 óJ  — |j                   d d }g |¢d‘| j                  ‘­}| j                  ||«      j                  |«      j	                  dd«      }	| j                  ||«      j                  |«      j	                  dd«      }
| j                  ||«      j                  |«      j	                  dd«      }| j                  �&| j                  ||«      \  }}t        |	|
||«      \  }	}
|� |j                  |
|| j                  «      \  }
}t        j                  | j                  j                  t        «      } || |	|
||f| j                   sdn| j"                  | j$                  dœ|¤Ž\  }} |j&                  g |¢d‘­Ž j)                  «       }| j+                  ||«      }||fS )Nr]   r    r   ç        )rè   rç   )rj   r£   r   rØ   ry   r  r  r  rÎ   Úupdater|   r   Úget_interfacer�   Ú_attn_implementationrö   rì   rú   rç   rÞ   rñ   r  )rZ   r-   rM   r¼   r.   r  ré   Úinput_shapeÚhidden_shapeÚquery_statesrò   ró   rº   r»   Úattention_interfacerõ   rô   s                    r<   rh   zMoshiAttention.forwardÕ  s±  € ð $×)Ñ)¨#¨2Ð.ˆØ8˜Ð8 bÐ8¨$¯-©-Ñ8ˆà—{‘{ =°,Ó?×DÑDÀ\ÓR×\Ñ\Ð]^Ð`aÓbˆØ—[‘[ °Ó=×BÑBÀ<ÓP×ZÑZÐ[\Ð^_Ó`ˆ
Ø—{‘{ =°,Ó?×DÑDÀ\ÓR×\Ñ\Ð]^Ð`aÓbˆà�?‰?Ð&Ø—‘ |°\ÓB‰HˆC�Ü';¸LÈ*ÐVYÐ[^Ó'_Ñ$ˆL˜*àÐ&Ø'6×'=Ñ'=¸jÈ,ÐX\×XfÑXfÓ'gÑ$ˆJ˜ä(?×(MÑ(MØ�K‰K×,Ñ,Ô.Eó)
Ðñ %8ØØØØØð	%
ð  $Ÿ}š}‘C°$×2HÑ2HØ—L‘Lñ	%
ð ñ	%
Ñ!ˆ�\ð *�k×)Ñ)Ð;¨;Ð;¸Ò;×FÑFÓHˆØ—k‘k +¨|Ó<ˆØ˜LÐ(Ð(r;   )NFT)NNNN)r0   r1   r2   r3   r!   rm   rU   r4   r5   r7   r   r   r   r9   rh   rn   ro   s   @r<   rø   rø   ¬  s²   ø„ ÙGñ$;˜{ð $;°s¸T±zõ $;ðR /3Ø04Ø(,Ø,0ñ()à—|‘|ð()ð Ÿ™ tÑ+ð()ð ×&Ñ&¨Ñ-ð	()ð
  ™ð()ð —l‘l TÑ)ð()ð Ð+Ñ,ð()ð 
ˆu�|‰|˜UŸ\™\Ð)Ñ	*÷()r;   rø   c                   ó.  ‡ — e Zd Zddededefˆ fd„Z	 	 	 	 	 	 ddej                  dej                  dz  dej                  dz  d	e
dz  d
edz  dedz  dej                  dz  deej                  eej                  ej                  f   dz  f   fd„Zˆ xZS )ÚMoshiDecoderLayerr�   r|   r„   c                 óx  •— t         ‰| �  «        |j                  | _        || _        t	        ||||¬«      | _        t        ||«      | _        t        | j                  |j                  ¬«      | _
        t        | j                  |j                  ¬«      | _        |j                  | _        |j                  | _        y )N)r�   r|   r„   r  ©rQ   )rT   rU   r¨   r„   rø   Ú	self_attnrÐ   ÚmlprO   Úrms_norm_epsÚinput_layernormÚpost_attention_layernormÚsliding_windowr  r  s        €r<   rU   zMoshiDecoderLayer.__init__  sž   ø€ Ü‰ÑÔØ!×-Ñ-ˆÔØ#6ˆÔ ä'Ø YÐDWÐbjô
ˆŒô " &Ð*=Ó>ˆŒÜ+¨D×,<Ñ,<À&×BUÑBUÔVˆÔÜ(4°T×5EÑ5EÈ6×K^ÑK^Ô(_ˆÔ%Ø$×3Ñ3ˆÔà$*×$?Ñ$?ˆÕ!r;   Nr-   rM   r¼   r.   Úoutput_attentionsÚ	use_cacher  r    c                 ó  — |}	| j                  |«      } | j                  d|||||||dœ|¤Ž\  }}
|	|z   }|}	| j                  |«      }| j                  s| j	                  |«      n| j	                  ||«      }|	|z   }|f}|r||
fz  }|S )N)r-   rM   r¼   r.   r  r  r  r:   )r  r  r  r„   r  )rZ   r-   rM   r¼   r.   r  r  r  ré   ÚresidualÚself_attn_weightsÚoutputss               r<   rh   zMoshiDecoderLayer.forward  sÊ   € ð !ˆà×,Ñ,¨]Ó;ˆð ,:¨4¯>©>ð 	,
Ø'Ø)Ø%Ø+Ø/ØØ%ñ	,
ð ñ	,
Ñ(ˆÐ(ð ! =Ñ0ˆð !ˆØ×5Ñ5°mÓDˆà+/×+CÒ+CˆD�H‰H�]Ô#ÈÏÉÐR_ÐamÓInð 	ð ! =Ñ0ˆà Ð"ˆáØÐ)Ð+Ñ+ˆGàˆr;   )T)NNNFFN)r0   r1   r2   r!   rm   ÚboolrU   r4   r5   r7   r   r9   r8   rh   rn   ro   s   @r<   r  r    sâ   ø„ ñ@˜{ð @°sð @ÐQUõ @ð& /3Ø04Ø(,Ø).Ø!&Ø,0ñ)à—|‘|ð)ð Ÿ™ tÑ+ð)ð ×&Ñ&¨Ñ-ð	)ð
  ™ð)ð   $™;ð)ð ˜$‘;ð)ð —l‘l TÑ)ð)ð 
ˆu× Ñ  %¨×(9Ñ(9¸5×;LÑ;LÐ(LÑ"MÐPTÑ"TÐTÑ	U÷)r;   r  c                   óz   ‡ — e Zd ZU eed<   dZdZdZddgZdZ	dZ
dZdZdZ ej                  «       ˆ fd„«       Zˆ xZS )	ÚMoshiPreTrainedModelr�   Úmodel©ÚaudioÚtextTr  ÚMimiTransformerLayerrJ   c                 ó„   •— t         ‰| �  |«       t        |t        «      r t	        j
                  |j                  «       y y rS   )rT   Ú_init_weightsr¶   rq   ÚinitÚnormal_rY   )rZ   rã   r[   s     €r<   r*  z"MoshiPreTrainedModel._init_weightsM  s0   ø€ ä‰Ñ˜fÔ%Ü�fÔ1Ô2Ü�L‰L˜Ÿ™Õ'ð 3r;   )r0   r1   r2   r!   r6   Úbase_model_prefixÚinput_modalitiesÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_supports_flash_attnÚ_supports_sdpaÚ_supports_flex_attnÚ_supports_attention_backendÚmain_input_namer4   rÂ   r*  rn   ro   s   @r<   r#  r#  ?  s]   ø… àÓØÐØ(ÐØ&*Ð#Ø,Ð.DÐEÐØÐØ€NØÐØ"&Ðà!€Oà€U‡]�]ƒ_ó(ó ô(r;   r#  c                   óD  ‡ — e Zd ZU dZeed<   defˆ fd„Z	 	 	 	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  de
dz  d	ej                  dz  d
edz  dedz  dedz  dedz  dej                  dz  dej                  dz  deez  fd„Zˆ xZS )ÚMoshiDepthDecoderz¥
    Transformer depth decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`MoshiTransformerLayer`]

    Args:
        config: MoshiConfig
    r�   c                 ó  •— t         ‰| �  |«       t        j                  |j                  dz   |j
                  «      | _        t        j                  t        |j                  dz
  «      D �cg c]/  }t        j                  |j                  dz   |j
                  «      ‘Œ1 c}«      | _        t        |j                  |j
                  |j                  «      | _        t        j                  t        |j                  «      D �cg c]  }t!        ||dd¬«      ‘Œ c}«      | _        t        |j
                  |j                  |j                  «      | _        |j&                  | _        d| _        || _        | j-                  «        y c c}w c c}w )Nr    TF)r„   r  )rT   rU   rV   Ú	EmbeddingÚ
vocab_sizer¨   Útext_embed_tokensÚ
ModuleListÚranger‰   Úaudio_vocab_sizeÚembed_tokensrq   rt   Úinput_projectionsÚnum_hidden_layersr  ÚlayersÚlm_headsr  Úgradient_checkpointingr�   Ú	post_init)rZ   r�   rÛ   r|   r[   s       €r<   rU   zMoshiDepthDecoder.__init__^  s?  ø€ Ü‰Ñ˜Ô ä!#§¡¨f×.?Ñ.?À!Ñ.CÀV×EWÑEWÓ!XˆÔô ŸM™MÜTYÐZ`×ZnÑZnÐqrÑZrÓTsÖtÈqŒR�\‰\˜&×1Ñ1°AÑ5°v×7IÑ7IÕJÒtó
ˆÔô "5°V×5FÑ5FÈ×HZÑHZÐ\b×\pÑ\pÓ!qˆÔä—m‘mô "' v×'?Ñ'?Ó!@öàô " &¨)ÈÐX]Ö^òó
ˆŒô ,¨F×,>Ñ,>À×@WÑ@WÐY_×YmÑYmÓnˆŒØ$*×$?Ñ$?ˆÔ!Ø&+ˆÔ#ØˆŒà�‰Õùò# uùòs   Á,4FÄF	NrJ   r@   rM   r.   Úinputs_embedsr  r  Úoutput_hidden_statesÚreturn_dictr¼   Úlabelsr    c           
      óò  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|	�|	n| j                   j                  }	| j
                  r%| j                  r|rt        j                  d«       d}|r|€t        | j                   ¬«      }|€dn|j                  «       }t        j                  |j                  d   |j                  ¬«      |z   }|
€|j                  d«      }
|€�g }|D ]r  }|j!                  «       }|dk(  r)|j#                  | j%                  |dd…|gf   «      «       ŒA|j#                   | j&                  |dz
     |dd…||z
  gf   «      «       Œt t        j(                  |d¬«      }|| j+                  ||«      z  }d}|�t-        | j                   ||||
¬	«      }|rd
nd}|rd
nd}|}| j.                  D ]*  }|r||fz  } ||||
||||¬«      }|d   }|sŒ"||d   fz  }Œ, |r||fz  }| j1                  ||«      }d}|�š|j3                  «       }t5        «       }|j7                  || j                   j8                  k(  d«      j;                  d«      }|j=                  |j                  «      } ||j;                  d| j                   j8                  «      |«      }|	st?        d„ |||||fD «       «      S tA        |||||¬«      S )a  
        Args:
            input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
                Indices of input sequence tokens. The first element of the sequence must the text token associated to the audio codebooks.
                The rest of the elements must be flatten audio codebooks.
            last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
                Sequence of hidden-states at the output of the last layer of the main decoder. Used to contextualize `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://huggingface.co/papers/1910.13461) for more
                information on the default strategy.

                - 1 indicates the head is **not masked**,
                - 0 indicates the head is **masked**.
            past_key_values (`Cache`, *optional*):
                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 the inputs 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.
            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)
            labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
                Labels for computing the masked 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]`.
        NúX`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`.F©r�   r   r    ©r�   r´   ©r�   rF  rM   r.   r¼   r:   )rM   r¼   r.   r  r  r  iœÿÿÿr]   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrS   r:   ©Ú.0Úvs     r<   ú	<genexpr>z,MoshiDepthDecoder.forward.<locals>.<genexpr>  s   è ø€ ò ØÐabÑan”ñùó   ‚Š)r?   r*   r.   r-   r,   )!r�   r  rG  r  rH  rD  rì   ÚloggerÚwarning_oncer   Úget_seq_lengthr4   rª   rj   r�   rÈ   ÚitemÚappendr;  r?  r¹   r@  r   rB  rC  re   r   Úmasked_fillr>  rÞ   r¬   r9   r   )rZ   rJ   r@   rM   r.   rF  r  r  rG  rH  r¼   rI  ré   Úpast_seen_tokensr  Úposition_idxÚcausal_maskÚall_hidden_statesÚall_self_attnsr-   Údecoder_layerÚlayer_outputsr*   r?   Úloss_fcts                            r<   rh   zMoshiDepthDecoder.forwardx  sD  € ðR 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð "+Ð!6‘I¸D¿K¹K×<QÑ<Qˆ	à%0Ð%<‘kÀ$Ç+Á+×BYÑBYˆà×&Ò&¨4¯=ª=¹YÜ×ÑØjôð ˆIá˜Ð0Ü*°$·+±+Ô>ˆOà /Ð 7™1¸_×=[Ñ=[Ó=]ÐÜ—|‘| I§O¡O°AÑ$6¸y×?OÑ?OÔPÐScÑcˆàÐØ'×1Ñ1°!Ó4ˆLð Ð ØˆMØ ,ò �Ø+×0Ñ0Ó2�Ø 1Ò$Ø!×(Ñ(¨×)?Ñ)?À	Ê!ÈlÈ^ÐJ[Ñ@\Ó)]Õ^à!×(Ñ(Ø=˜×)Ñ)¨<¸!Ñ+;Ñ=¸iÊÈLÐ[kÑLkÐKlÐHlÑ>mÓnõðô "ŸI™I m¸Ô;ˆMà˜×/Ñ/Ð0AÀ<ÓPÑPˆàˆØÐ%Ü,Ø—{‘{Ø+Ø-Ø /Ø)ôˆKñ #7™B¸DÐÙ0™°dˆØ%ˆØ!Ÿ[™[ò 	6ˆMÙ#Ø! mÐ%5Ñ5Ð!á)ØØ*Ø)Ø /Ø"3Ø#Ø)ôˆMð *¨!Ñ,ˆMâ Ø =°Ñ#3Ð"5Ñ5‘ð#	6ñ(  Ø -Ð!1Ñ1Ðà—‘˜}¨lÓ;ˆàˆØÐà—\‘\“^ˆFÜ'Ó)ˆHà×'Ñ'¨°$·+±+×2NÑ2NÑ(NÐPTÓU×]Ñ]Ð^`ÓaˆFØ—Y‘Y˜vŸ}™}Ó-ˆFÙ˜FŸN™N¨2¨t¯{©{×/KÑ/KÓLÈfÓUˆDáÜñ Ø  &¨/Ð;LÈnÐ]ôó ð ô &ØØØ+Ø)Ø%ô
ð 	
r;   )NNNNNNNNNNN)r0   r1   r2   r3   r"   r6   rU   r4   r7   Ú
BoolTensorr   r8   r!  r9   r   rh   rn   ro   s   @r<   r7  r7  T  s$  ø… ñð ÓðÐ/õ ð8 .2Ø59Ø26Ø(,Ø26Ø!%Ø)-Ø,0Ø#'Ø04Ø*.ñl
à×#Ñ# dÑ*ðl
ð !×+Ñ+¨dÑ2ðl
ð ×(Ñ(¨4Ñ/ð	l
ð
  ™ðl
ð ×(Ñ(¨4Ñ/ðl
ð ˜$‘;ðl
ð   $™;ðl
ð # T™kðl
ð ˜D‘[ðl
ð ×&Ñ&¨Ñ-ðl
ð × Ñ  4Ñ'ðl
ð 
Ð(Ñ	(÷l
r;   r7  c                   óþ   ‡ — e Zd Zdefˆ fd„Ze	 	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  de	dz  dej                  dz  d	edz  d
edz  dedz  dedz  deez  fd„«       Zˆ xZS )Ú
MoshiModelr�   c           
      óô  •— t         ‰| �  |«       |j                  | _        |j                  | _        t        j                  |j                  dz   |j                  | j                  «      | _        t        j                  t        |j                  «      D �cg c]  }t        ||d¬«      ‘Œ c}«      | _        t        |j                  |j                  ¬«      | _        d| _        | j%                  «        y c c}w )Nr    F)r„   r  )rT   rU   Úpad_token_idÚpadding_idxr:  rV   r9  r¨   r?  r<  r=  rA  r  rB  rO   r  ÚnormrD  rE  )rZ   r�   r|   r[   s      €r<   rU   zMoshiModel.__init__)  sÉ   ø€ Ü‰Ñ˜Ô Ø!×.Ñ.ˆÔØ ×+Ñ+ˆŒäŸL™L¨×):Ñ):¸QÑ)>À×@RÑ@RÐTX×TdÑTdÓeˆÔÜ—m‘mô "' v×'?Ñ'?Ó!@öàô " &¨)ÈÖOòó
ˆŒô ! ×!3Ñ!3¸×9LÑ9LÔMˆŒ	Ø&+ˆÔ#ð 	�‰Õùòs   ÂC5NrJ   rM   r¼   r.   rF  r  r  rG  rH  r    c
           	      ó†  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|	�|	n| j                   j                  }	| j
                  r%| j                  r|rt        j                  d«       d}|€| j                  |«      }|€V|�|j                  «       nd}t        j                  |j                  d   |j                  ¬«      |z   }|j                  d«      }d }|�t!        | j                   ||||¬«      }|}|r|€t#        | j                   ¬«      }|rdnd }|rdnd }| j$                  D ])  }|r||fz  } |||||||¬	«      }|d   }|sŒ!||d   fz  }Œ+ | j'                  |«      }|r||fz  }|	st)        d
„ ||||fD «       «      S t+        ||||¬«      S )NrK  Fr   r    rM  rN  rL  r:   )rM   r¼   r.   r  r  c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrS   r:   rP  s     r<   rS  z%MoshiModel.forward.<locals>.<genexpr>‹  s   è ø€ ò ØÐbcÑbo”ñùrT  )r@   r.   r-   r,   )r�   r  rG  r  rH  rD  rì   rU  rV  r?  rW  r4   rª   rj   r�   rÈ   r   r   rB  ri  r9   r   )rZ   rJ   rM   r¼   r.   rF  r  r  rG  rH  ré   r[  r]  r-   r^  r_  r`  ra  s                     r<   rh   zMoshiModel.forward;  s  € ð 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð "+Ð!6‘I¸D¿K¹K×<QÑ<Qˆ	Ø%0Ð%<‘kÀ$Ç+Á+×BYÑBYˆà×&Ò&¨4¯=ª=¹YÜ×ÑØjôð ˆIàÐ Ø ×-Ñ-¨iÓ8ˆMàÐØCRÐC^˜×=Ñ=Ô?ÐdeÐÜ Ÿ<™<¨×(;Ñ(;¸AÑ(>À}×G[ÑG[Ô\Ð_oÑoˆLØ'×1Ñ1°!Ó4ˆLàˆØÐ%Ü,Ø—{‘{Ø+Ø-Ø /Ø)ôˆKð &ˆá˜Ð0Ü*°$·+±+Ô>ˆOñ #7™B¸DÐÙ0™°dˆà!Ÿ[™[ò 	6ˆMÙ#Ø! mÐ%5Ñ5Ð!á)ØØ*Ø)Ø /Ø"3Ø#ôˆMð *¨!Ñ,ˆMâ Ø =°Ñ#3Ð"5Ñ5‘ð!	6ð$ Ÿ	™	 -Ó0ˆñ  Ø -Ð!1Ñ1ÐáÜñ Ø)¨?Ð<MÈ~Ð^ôó ð ô 'Ø+Ø+Ø+Ø%ô	
ð 	
r;   )	NNNNNNNNN)r0   r1   r2   r!   rU   r   r4   r7   r5   r   r8   r!  r9   r   rh   rn   ro   s   @r<   re  re  '  sò   ø„ ð˜{õ ð$ ð .2Ø.2Ø04Ø(,Ø26Ø!%Ø)-Ø,0Ø#'ñW
à×#Ñ# dÑ*ðW
ð Ÿ™ tÑ+ðW
ð ×&Ñ&¨Ñ-ð	W
ð
  ™ðW
ð ×(Ñ(¨4Ñ/ðW
ð ˜$‘;ðW
ð   $™;ðW
ð # T™kðW
ð ˜D‘[ðW
ð 
Ð(Ñ	(òW
ó ôW
r;   re  zc
    The Moshi decoder model with a text language modelling head on top. Only usable for text.
    c                   ó<  ‡ — e Zd ZdZˆ fd„Ze	 	 	 	 	 	 	 	 	 	 	 ddej                  dz  dej                  dz  dej                  dz  de	dz  dej                  dz  d	edz  d
edz  dedz  dedz  dej                  dz  deej                  z  deez  fd„«       Zˆ xZS )ÚMoshiForCausalLM)r'  c                 óè   •— t         ‰| �  |«       t        |«      | _        |j                  | _        t        j                  |j                  |j                  d¬«      | _        | j                  «        y )NFr‚   )
rT   rU   re  r$  r:  rV   r…   r¨   Úlm_headrE  )rZ   r�   r[   s     €r<   rU   zMoshiForCausalLM.__init__Ÿ  sU   ø€ Ü‰Ñ˜Ô Ü Ó'ˆŒ
Ø ×+Ñ+ˆŒÜ—y‘y ×!3Ñ!3°V×5FÑ5FÈUÔSˆŒð 	�‰Õr;   NrJ   rM   r¼   r.   rF  r  r  rG  rH  rI  Úlogits_to_keepr    c                 ó\  — |�|n| j                   j                  }|�|n| j                   j                  }|	�|	n| j                   j                  }	| j	                  |||||||||	¬«	      }|d   }t        |t        «      rt        | d«      n|}| j                  |dd…|dd…f   «      }d}|
�½|j                  «       }|ddd…dd…f   j                  «       }|
ddd…f   j                  «       }|j                  d| j                   j                  «      }|j                  d«      }|j                  |j                  «      } | j                  ||fd| j                   j                  i|¤Ž}|	s||f|dd z   }|�|f|z   S |S t!        ||||j"                  |j$                  |j&                  ¬«      S )	a!  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the masked 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]`.

        Example:

        ```python
        >>> from transformers import AutoTokenizer, MoshiForCausalLM

        >>> model = MoshiForCausalLM.from_pretrained("kmhf/hf-moshiko")
        >>> tokenizer = AutoTokenizer.from_pretrained("kmhf/hf-moshiko")

        >>> prompt = "What is your favorite condiment?"
        >>> inputs = tokenizer(prompt, return_tensors="pt")

        >>> # Generate
        >>> generate_ids = model.generate(inputs.input_ids, max_length=30)
        >>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
        "What is your favorite condiment?"
        ```N)	rJ   rM   r¼   r.   rF  r  r  rG  rH  r   .r]   r    r:  )r?   r*   r@   r.   r-   r,   )r�   r  rG  rH  r$  r¶   rm   Úslicero  re   rñ   rØ   r:  r¬   r�   Úloss_functionr>   r.   r-   r,   )rZ   rJ   rM   r¼   r.   rF  r  r  rG  rH  rI  rp  ré   r   r-   Úslice_indicesr*   r?   Úshift_logitsÚshift_labelsrg   s                        r<   rh   zMoshiForCausalLM.forward¨  sð  € ðL 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×BYÑBYˆð —*‘*ØØ)Ø%Ø+Ø'ØØ/Ø!5Ø#ð ó 

ˆð   ™
ˆä8BÀ>ÔSVÔ8Wœ˜~˜o¨tÔ4Ð]kˆØ—‘˜mªA¨}ºaÐ,?Ñ@ÓAˆàˆØÐà—\‘\“^ˆFà! # s¨ sªA +Ñ.×9Ñ9Ó;ˆLØ! # q¡r '™?×5Ñ5Ó7ˆLà'×,Ñ,¨R°·±×1GÑ1GÓHˆLØ'×,Ñ,¨RÓ0ˆLØ'Ÿ?™?¨<×+>Ñ+>Ó?ˆLØ%�4×%Ñ%ØØñð  Ÿ;™;×1Ñ1ðð ñ	ˆDñ àØðð ˜˜�ñˆFð (,Ð'7�D�7˜VÑ#ÐC¸VÐCä*ØØØ+Ø#×3Ñ3Ø!×/Ñ/Ø×)Ñ)ô
ð 	
r;   )NNNNNNNNNNr   )r0   r1   r2   r.  rU   r   r4   r7   r5   r   r8   r!  rm   r9   r>   rh   rn   ro   s   @r<   rm  rm  –  s#  ø„ ð !Ðôð ð .2Ø.2Ø04Ø(,Ø26Ø!%Ø)-Ø,0Ø#'Ø*.Ø-.ñ]
à×#Ñ# dÑ*ð]
ð Ÿ™ tÑ+ð]
ð ×&Ñ&¨Ñ-ð	]
ð
  ™ð]
ð ×(Ñ(¨4Ñ/ð]
ð ˜$‘;ð]
ð   $™;ð]
ð # T™kð]
ð ˜D‘[ð]
ð × Ñ  4Ñ'ð]
ð ˜eŸl™lÑ*ð]
ð 
Ð,Ñ	,ò]
ó ô]
r;   rm  zz
    The original Moshi model with an audio encoder, a Moshi depth decoder and a Moshi decoder, for speech-to-speech.
    c            !       óN  ‡ — e Zd ZU eed<   dZdZdZdZdZ	dZ
dZdefˆ fd„Zd„ Ze	 	 	 	 	 	 	 	 	 	 	 	 	 	 d4dej                   dz  dej"                  dz  d	ej$                  dz  d
ej&                  dz  dej$                  dz  dej&                  dz  dedz  dej$                  dz  dej                   dz  dej                   dz  dedz  dedz  dedz  dedz  deez  fd„«       Zdej                   dedeeef   dej                   fd„Z	 	 	 	 	 	 	 	 	 	 d5dej                   dz  d	ej$                  dz  d
ej&                  dz  dej$                  dz  dej&                  dz  dej$                  dz  dej&                  dz  dedz  dedefd„Z ej>                  «       	 	 	 	 	 	 	 	 	 d6dej                   dz  d	ej$                  dz  d
ej&                  dz  dej$                  dz  dej&                  dz  dej$                  dz  dedz  dedz  dedz  dej                   fˆ fd„«       Z 	 	 	 	 	 	 	 	 	 	 	 d7d ej$                  dz  fˆ fd!„Z!	 	 d8d"e"d#eeef   d$ed%e#deeef   f
ˆ fd&„Z$d'„ Z%d(„ Z&d)„ Z'd*„ Z(d+„ Z)d,„ Z*e+d-„ «       Z,	 d9dej                   d.e#d/e#d0e#dz  fd1„Z-d:d2„Z.	 	 	 	 	 	 	 d;d3„Z/ˆ xZ0S )<ÚMoshiForConditionalGenerationr�   r%  rJ   Tc           	      óò  •— t         ‰| �  |«       t        j                  t	        d|j
                  z  «      D �cg c]/  }t        j                  |j                  dz   |j                  «      ‘Œ1 c}«      | _	        t        j                  |j                  «      | _        t        |«      | _        t         j#                  |j$                  «      | _        |j
                  | _        | j)                  «        y c c}w )Nr   r    )rT   rU   rV   r<  r=  r‰   r9  r>  r¨   r?  r   Úfrom_configÚaudio_encoder_configÚaudio_encoderrm  Údecoderr7  Ú_from_configÚdepth_decoder_configÚdepth_decoderrE  )rZ   r�   rÛ   r[   s      €r<   rU   z&MoshiForConditionalGeneration.__init__  s»   ø€ Ü‰Ñ˜Ô äŸM™MÜTYÐZ[Ð^d×^rÑ^rÑZrÓTsÖtÈqŒR�\‰\˜&×1Ñ1°AÑ5°v×7IÑ7IÕJÒtó
ˆÔô '×2Ñ2°6×3NÑ3NÓOˆÔÜ'¨Ó/ˆŒä.×;Ñ;¸F×<WÑ<WÓXˆÔà#×1Ñ1ˆÔØ�‰Õùò us   º4C4c                 ó   — | j                   S rS   )r€  rk   s    r<   Úget_depth_decoderz/MoshiForConditionalGeneration.get_depth_decoder&  s   € Ø×!Ñ!Ð!r;   NrM   Úuser_input_valuesrK   Úmoshi_input_valuesrL   r.   rF  Útext_labelsÚaudio_labelsr  r  rG  rH  r    c                 óÀ  ‡ ‡— |�|n‰ j                   j                  }|j                  «       D ��ci c]%  \  }}|j                  d«      r|t	        d«         |“Œ' }}}|j                  «       D ��ci c]&  \  }}|j                  d«      sŒ|t	        d«      d |“Œ( }}}|j                  «       D ��ci c]%  \  }}|j                  d«      r|t	        d«      d |“Œ' }}}|€þ|�.|€, ‰ j
                  j                  |fd‰ j                  i|¤Žd   }|�.|€, ‰ j
                  j                  |fd‰ j                  i|¤Žd   }t        j                  ||gd¬«      Š|€‰€t        d	«      ‚|�%‰ j                  j                  j                  |«      }‰�Mt        ˆˆ fd
„t        ‰j                   d   «      D «       «      }|€|n||j#                  |j$                  «      z   } ‰ j                  d||||||d|	dœ|¤Ž}|j&                  }d}|j(                  }|	�ý|
�û‰ j+                  |
‰ j                   j,                  ‰ j                   j,                  |
j                   d   dz   ¬«      d   }
|	j/                  dd«      }	|
j1                  dd«      j3                  d|
j                   d   «      }
t        j                  |	|
gd¬«      }|dd…dd…f   }|j/                  dd|j                   d   «      } ‰ j4                  d||||
dœ|¤Ž}||j(                  z  }|s'|j7                  «       }|�||j7                  «       z  }|S t9        |j(                  |j:                  ||j<                  |j>                  |j@                  |€dn|j(                  |€dn|j:                  |€dn|j<                  |€dn|j>                  |€d¬«      S |j@                  ¬«      S c c}}w c c}}w c c}}w )a¡  
        user_input_values (`torch.Tensor `of shape `(batch_size, 1, audio_sequence_length), *optional*):
            The audio waveforms used as audio user prompt for the generation.
        user_audio_codes (`torch.Tensor `of shape `(batch_size, num_codebooks, sequence_length), *optional*):
            The audio codes used as audio user prompt for the generation. Has priority over `user_input_values` and represents the audio "tokens" of `user_input_values` once passed through the audio encoder.
        moshi_input_values (`torch.Tensor `of shape `(batch_size, 1, audio_sequence_length), *optional*):
            The audio waveforms used as audio Moshi prompt for the generation.
        moshi_audio_codes (`torch.Tensor `of shape `(batch_size, num_codebooks, sequence_length), *optional*):
            The audio codes used as audio Moshi prompt for the generation. Has priority over `moshi_input_values` and represents the audio "tokens" of `moshi_input_values` once passed through the audio encoder.
        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. If `past_key_values` is used, optionally only the last `inputs_embeds` have to be
            input (see `past_key_values`). 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.

            If `input_ids` and `inputs_embeds` are both unset, `inputs_embeds` takes the value
            of `inputs_embeds`.
        text_labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for text language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
            `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
            are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]`
        audio_labels (`torch.LongTensor` of shape `(batch_size, num_codebooks, sequence_length)`, *optional*):
            Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set
            `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100`
            are ignored (masked), the loss is only computed for labels in `[0, ..., config.audio_vocab_size]`

        Examples:
        ```python
        >>> from transformers import MoshiForConditionalGeneration
        >>> import torch

        >>> model = MoshiForConditionalGeneration.from_pretrained("kmhf/hf-moshiko")
        >>> inputs = moshi.get_unconditional_inputs()

        >>> logits = model(**inputs, ).logits
        >>> logits.shape  # (bsz, seq_len, text_vocab_size)
        torch.Size([1, 1, 32000])
        ```NÚaudio_encoder_Údecoder_Údepth_decoder_Únum_quantizersr   r    r´   z`You must provide at least one of `input_ids`, `inputs_embeds`, `input_values` and `audio_codes`.c              3   óV   •K  — | ]   } ‰j                   |   ‰d d …|f   «      –— Œ" y ­wrS   ©r?  ©rQ  Úcodebookr/   rZ   s     €€r<   rS  z8MoshiForConditionalGeneration.forward.<locals>.<genexpr>‹  ó2   øè ø€ ò *ØNVÐ/�D×%Ñ% hÑ/°ºA¸x¸KÑ0H×Iñ*ùó   ƒ&)T)rM   rF  r  rG  r  r.   rH  rI  r]   ©Úbos_token_idrg  Ú
max_lengthr   )r@   rJ   rM   rI  )r?   r*   r@   r.   r-   r,   rC   rD   rE   rF   rG   r:   )!r�   rH  ÚitemsÚ
startswithÚlenr|  Úencoder‰   r4   r¹   rÿ   r}  r$  r?  Úsumr=  rj   r¬   r�   r@   r?   Úbuild_delay_pattern_maskr>  rØ   ry   rÞ   r€  Úto_tuplerB   r*   r.   r-   r,   )rZ   rJ   rM   rƒ  rK   r„  rL   r.   rF  r…  r†  r  r  rG  rH  ré   Úargumentræ   Úkwargs_audio_encoderÚkwargs_decoderÚkwargs_depth_decoderÚaudio_inputs_embedsÚdecoder_outputsÚdecoder_last_hidden_stateÚdepth_decoder_outputsÚ
final_lossÚdepth_input_idsr   r/   s   `                           @r<   rh   z%MoshiForConditionalGeneration.forward)  s¡  ù€ ðr &1Ð%<‘kÀ$Ç+Á+×BYÑBYˆð $*§<¡<£>÷ 
á�˜%Ø×"Ñ"Ð#3Ô4ð ”SÐ)Ó*Ñ+¨UÑ2ð 
Ðñ  
ð GMÇlÁlÃn÷
Ù3B°8¸UÐX`×XkÑXkÐlvÕXwˆH”S˜“_Ð&Ð'¨Ñ.ð
ˆñ 
ð $*§<¡<£>÷ 
á�˜%Ø×"Ñ"Ð#3Ô4ð ”SÐ)Ó*Ð,Ð-¨uÑ4ð 
Ðñ  
ð Ð Ø Ð,Ð1AÐ1IØ#< 4×#5Ñ#5×#<Ñ#<Ø%ñ$Ø6:×6HÑ6Hð$ØL`ñ$àñ$Ð ð "Ð-Ð2CÐ2KØ$= D×$6Ñ$6×$=Ñ$=Ø&ñ%Ø7;×7IÑ7Ið%ØMañ%àñ%Ð!ô  Ÿ)™)Ð%6Ð8HÐ$IÈqÔQˆKàÐ  [Ð%8Ü Øvóð ð Ð$Ø $§¡× 2Ñ 2× ?Ñ ?À	Ó J�àÐ&Ü&)ô *ÜZ_Ð`k×`qÑ`qÐrsÑ`tÓZuô*ó 'Ð#ð
 %Ð,ñ (à,¨}×/?Ñ/?Ð@S×@ZÑ@ZÓ/[Ñ[ð ð '˜$Ÿ,™,ð 

Ø)Ø'Ø/Ø!5ØØ+ØØñ

ð ñ

ˆð %4×$EÑ$EÐ!à $ÐØ$×)Ñ)ˆ
ØÐ" |Ð'?ð  ×8Ñ8ØØ!Ÿ[™[×9Ñ9Ø!Ÿ[™[×9Ñ9Ø'×-Ñ-¨bÑ1°AÑ5ð	 9ó ð
 ñˆLð &×*Ñ*¨2¨qÓ1ˆKð (×1Ñ1°!°QÓ7×?Ñ?ÀÀL×DVÑDVÐWXÑDYÓZˆLä#Ÿi™i¨°lÐ(CÈÔKˆOà-ªa°°"°¨fÑ5ˆOð )B×(FÑ(FÀrÈ1ÐNg×NmÑNmÐnpÑNqÓ(rÐ%à$6 D×$6Ñ$6ð %Ø";Ø)Ø-Ø#ñ	%ð
 'ñ%Ð!ð Ð/×4Ñ4Ñ4ˆJáØ%×.Ñ.Ó0ˆGØ$Ð0ØÐ0×9Ñ9Ó;Ñ;�ØˆNä7Ø ×%Ñ%Ø"×)Ñ)Ø7Ø+×;Ñ;Ø)×7Ñ7Ø&×1Ñ1Ø4Ð<‘tÐBW×B\ÑB\Ø!6Ð!>™ÐDY×D`ÑD`Ø*9Ð*A¡$À×GfÑGfØ(7Ð(?¡À_×EbÑEbØ%4Ð%<˜Tô
ð 	
ð CR×B\ÑB\ô
ð 	
ùóQ 
ùó
ùó 
s   °*OÁ0OÂ
OÂ1*OÚgeneration_configré   c                 ó¼  — |j                   }|j                  }t        j                  |j                  t        j
                  |j                  ¬«      }|€|S |d uxr$ t        j                  ||«      j                  «       }|d u xs% t        j                  ||«      j                  «        }||z  }	|j                  |«      j                  «       }
|
|	z  ||	 z  z   }|S )N©r¥   r�   )
rg  Úeos_token_idr4   rX   rj   Úlongr�   ÚisinÚanyÚne)rZ   rJ   r¦  ré   rg  r©  Údefault_attention_maskÚis_pad_token_in_inputsÚ&is_pad_token_not_equal_to_eos_token_idÚcan_infer_attention_maskÚattention_mask_from_paddingrM   s               r<   Ú&_prepare_attention_mask_for_generationzDMoshiForConditionalGeneration._prepare_attention_mask_for_generationÚ  sè   € ð )×5Ñ5ˆØ(×5Ñ5ˆä!&§¡¨I¯O©OÄ5Ç:Á:ÐV_×VfÑVfÔ!gÐØÐØ)Ð)à".°dÐ":Ò!iÄÇ
Á
È9ÐVbÓ@c×@gÑ@gÓ@iÐØ2>À$Ð2Fò 2ÌEÏJÉJØ˜,óM
ç
‰#‹%ðLÐ.ð $:Ð<bÑ#bÐ Ø&/§l¡l°<Ó&@×&EÑ&EÓ&GÐ#ð (Ð*BÑBÐE[Ð_wÐ^wÑEwÑwð 	ð Ðr;   Úapply_delay_pattern_maskÚconcat_unconditional_inputsc                 óž  ‡ ‡— d }d }|€|€|€|€|€|€t        d«      ‚|�|	r\|�,|€*‰ j                  j                  |‰ j                  ¬«      d   }|�,|€*‰ j                  j                  |‰ j                  ¬«      d   }|€¯|
r­‰ j	                  |j
                  d   ¬«      }t        j                  |j                  |gd¬«      }t        j                  |j                  |gd¬«      }t        j                  |j                  |gd¬«      }|�#t        j                  |j                  |gd¬«      }|�|	rœ|	rL|�J‰ j                  |‰ j                  j                  ‰ j                  j                  |j                  ¬«      \  }}|	rL|�J‰ j                  |‰ j                  j                  ‰ j                  j                  |j                  ¬«      \  }}|€õd }|�G|�Et        j                  ||gd¬«      Št!        ˆˆ fd	„t#        ‰j
                  d   «      D «       «      }n_|�.|Št!        ˆˆ fd
„t#        ‰j
                  d   «      D «       «      }n/|�-|Št!        ˆˆ fd„t#        ‰j
                  d   «      D «       «      }|�%‰ j$                  j&                  j)                  |«      }|�"|€|n||j+                  |j,                  «      z   }|||||||fS )Nz”You must provide at least one of `input_ids`, `user_input_values`, `moshi_input_values`, `user_audio_codes`, `moshi_audio_codes` or `inputs_embeds`.©r‹  r   )Únum_samplesr   r´   r    r’  c              3   óV   •K  — | ]   } ‰j                   |   ‰d d …|f   «      –— Œ" y ­wrS   r�  rŽ  s     €€r<   rS  zVMoshiForConditionalGeneration._prepare_inputs_embeds_for_generation.<locals>.<genexpr>5  r�  r‘  c              3   óV   •K  — | ]   } ‰j                   |   ‰d d …|f   «      –— Œ" y ­wrS   r�  rŽ  s     €€r<   rS  zVMoshiForConditionalGeneration._prepare_inputs_embeds_for_generation.<locals>.<genexpr>:  r�  r‘  c              3   óp   •K  — | ]-  } ‰j                   |   ‰d d …|‰j                  z   f   «      –— Œ/ y ­wrS   )r?  r‰   rŽ  s     €€r<   rS  zVMoshiForConditionalGeneration._prepare_inputs_embeds_for_generation.<locals>.<genexpr>?  s@   øè ø€ ò *à ð 0�D×%Ñ% hÑ/°ºA¸xÈ$×J\ÑJ\Ñ?\Ð<\Ñ0]×^ñ*ùs   ƒ36)rÿ   r|  r˜  r‰   Úget_unconditional_inputsrj   r4   r¹   rL   rK   rJ   rM   rš  r�   r>  r”  r™  r=  r}  r$  r?  r¬   r�   )rZ   rJ   rƒ  rK   r„  rL   rF  rM   r¦  r´  rµ  Úuser_delay_pattern_maskÚmoshi_delay_pattern_maskÚunconditional_inputsr   r/   s   `              @r<   Ú%_prepare_inputs_embeds_for_generationzCMoshiForConditionalGeneration._prepare_inputs_embeds_for_generationó  sJ  ù€ ð #'ÐØ#'Ð ð Ð!ØÐ!Ø!Ð)Ø Ð(Ø"Ð*Ø!Ð)äð góð ð
 Ð Ñ$<Ø Ð,Ð1AÐ1IØ#'×#5Ñ#5×#<Ñ#<Ð=NÐ_c×_qÑ_qÐ#<Ó#rÐstÑ#uÐ à!Ð-Ð2CÐ2KØ$(×$6Ñ$6×$=Ñ$=Ð>PÐae×asÑasÐ$=Ó$tÐuvÑ$wÐ!àÐ Ñ%@Ø#'×#@Ñ#@ÐM]×McÑMcÐdeÑMfÐ#@Ó#gÐ Ü %§	¡	Ð+?×+QÑ+QÐSdÐ*eÐklÔ mÐÜ$Ÿy™yÐ*>×*OÑ*OÐQaÐ)bÐhiÔjÐÜŸ	™	Ð#7×#AÑ#AÀ9Ð"MÐSTÔUˆIØÐ)Ü!&§¡Ð,@×,OÑ,OÐQ_Ð+`ÐfgÔ!h�àÐ Ñ$<Ù'Ð,<Ð,HØ<@×<YÑ<YØ$Ø!%§¡×!=Ñ!=Ø!%§¡×!=Ñ!=Ø0×;Ñ;ð	 =Zó =Ñ9Ð Ð"9ñ (Ð,=Ð,IØ>B×>[Ñ>[Ø%Ø!%§¡×!=Ñ!=Ø!%§¡×!=Ñ!=Ø0×;Ñ;ð	 ?\ó ?Ñ;Ð!Ð#;ð Ð Ø"&ÐØÐ+Ð0AÐ0MÜ#Ÿi™iÐ):Ð<LÐ(MÐSTÔU�Ü&)ô *ÜZ_Ð`k×`qÑ`qÐrsÑ`tÓZuô*ó 'Ñ#ð #Ð.Ø/�Ü&)ô *ÜZ_Ð`k×`qÑ`qÐrsÑ`tÓZuô*ó 'Ñ#ð "Ð-Ø.�Ü&)ô *ä$)¨+×*;Ñ*;¸AÑ*>Ó$?ô*ó 'Ð#ð
 Ð$Ø $§¡× 2Ñ 2× ?Ñ ?À	Ó J�à"Ð.ð %Ð,ñ (à,¨}×/?Ñ/?Ð@S×@ZÑ@ZÓ/[Ñ[ð ð ØØØØ#Ø$Øð
ð 	
r;   Úreturn_audio_waveformsÚreturn_audio_codesc
                 ó|  •— t        | d«      �r't        | j                  d«      �si | j                  _        d| j                  v r| j                  | j                  _        n�| j                  j                  «       D �cg c]	  }|dvsŒ|‘Œ c}d   }| j                  j	                  «       D ��ci c]+  \  }}|j                  d«      r|t        d«      d |dv r|n|“Œ- c}}| j                  _        | j                  j	                  «       D ��ci c]  \  }}|j                  d«      rŒ||“Œ c}}| _        |
D �ch c]  }|j                  d«      sŒ|’Œ }}|D �ci c]   }|t        d«      d |
j                  |«      “Œ" }} | j                  |
j                  dd«      fi |
¤Ž\  }}
| j                  |||||||	¬	«      \  }}}}	|€|n|}|	r|j                  d
   dz   n|j                  d
   }|
j                  d«      du xr |j                  du}|
j                  d«      du xr |j                  du}| j                  ||||€dnd||¬«      }t        |d«      r|j                  }n | j                   dz   | j                   dz   ddœ}|j#                  |«       |}|
j                  dd«      }|€| j%                  |||
¬«      }| j'                  ||||||||d|	¬«
      \  }}}}}}}t)        j*                  |j                  d   dt-        | j.                  j0                  | j.                  j2                  j4                  z  «      f| j6                  | j8                  ¬«      }| j:                  j=                  || j                   ¬«      d   }|�|n|
j                  d«      |
d<   |�|n|
j                  d«      |
d<   t)        j>                  |tA        |jB                  |jD                  «      d¬«      | _#        |jB                  dkD  xs |jH                  }|jB                  dkD  xs |jJ                  }tM        ‰)| �œ  d#||||||||dœ|
¤Ž}|s|s|r|jH                  s|jP                  S |S |s|} n|jP                  } |jD                  dkD  r"t)        j>                  ||jD                  d¬«      }|jB                  dkD  �r|jR                  dd…d|j                  d
    …f   }!| jF                  dd…dd…|j                  d
   d…f   }"|!dd…dd
…f   jU                  d«      jW                  d
| j                   d
«      }#t)        jX                  |"d|#¬«      }"t)        j>                  ||jD                  d¬«      }t)        jZ                  ||"fd¬«      | _#        t)        j\                  | j^                  d|!dd…d
f   ¬«      | _/        | j^                  ja                  d
d| j^                  j                  d
   «      }$ | j                  jN                  d#|$| dd…d
d…f   ja                  d
d«      d œ|¤Ž}%|%dd…dd…f   jU                  d«      }%t)        jZ                  | jF                  |%gd¬«      | _#        | jc                  | jF                  |«      }&|| j.                  jd                  k7  }'|&|'   jg                  |'j                  d   | j                   d
«      }&d}(|r%| j:                  ji                  |&«      jj                  }(|r|&nd}&|jH                  rtm        d#|(|&d!œ|¤ŽS tm        |(| |&¬"«      S c c}w c c}}w c c}}w c c}w c c}w )$as  
        Generates sequences of text token ids and audio tokens ids.

        Parameters:
            input_ids (`torch.Tensor `of shape `(batch_size, sequence_length), *optional*):
                The sequence used as a text prompt for the generation.
            user_input_values (`torch.Tensor `of shape `(batch_size, 1, audio_sequence_length), *optional*):
                The audio waveforms used as audio user prompt for the generation.
            user_audio_codes (`torch.Tensor `of shape `(batch_size, num_codebooks, sequence_length), *optional*):
                The audio codes used as audio user prompt for the generation. Has priority over `user_input_values` and represents the audio "tokens" of `user_input_values` once passed through the audio encoder.
            moshi_input_values (`torch.Tensor `of shape `(batch_size, 1, audio_sequence_length), *optional*):
                The audio waveforms used as audio Moshi prompt for the generation.
            moshi_audio_codes (`torch.Tensor `of shape `(batch_size, num_codebooks, sequence_length), *optional*):
                The audio codes used as audio Moshi prompt for the generation. Has priority over `moshi_input_values` and represents the audio "tokens" of `moshi_input_values` once passed through the audio encoder.
            inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
                Optionally, instead of passing `input_ids` and the audio inputs you can choose to directly pass an embedded representation. This
                is useful if you want more control over how to convert the inputs into associated vectors than the
                model's internal embedding lookup matrix.
            return_audio_waveforms (`bool`, *optional*, defaults to `True`):
                If `False`, won't generate the audio waveforms.
            return_audio_codes (`bool`, *optional*):
                If `True`, will also returns the generated audio codes, i.e the intermediate audio "tokens" which transforms to `audio_sequences` once passed through the audio decoder.
            concat_unconditional_inputs (`bool`, *optional*, defaults to `True`):
                If `False`, won't concatenate initial audio and text tokens.
            kwargs (`dict[str, Any]`, *optional*):
                Remaining dictionary of keyword arguments that are passed to the `generate` method. Refers to the
                original [`generate` docstrings](https://huggingface.co/docs/transformers/main/en/main_classes/text_generation#transformers.GenerationMixin.generate)
                for more information on how to use them.
                Note that keywords with a *depth_* prefix will be input for the `generate` method of the
                depth decoder. Otherwise, the latter will use its default generation config.
        Return:
            [`MoshiConditionalGenerationGenerateOutput`]
        Úhf_device_mapÚ )r±   Údiskr   r€  NrŠ  r¦  )rJ   rƒ  rK   r„  rL   rF  rµ  r]   r    r”  Ú
min_lengthrF  rJ   )r¦  Úhas_default_max_lengthÚhas_default_min_lengthÚmodel_input_nameÚinputs_tensorÚinput_ids_lengthr  Ústatic)rÇ  r”  Úcache_implementationrM   )rJ   r¦  ré   T)
rJ   rƒ  rK   r„  rL   rF  rM   r¦  r´  rµ  r¨  r·  r½  r¾  r´   )rF  rJ   r¦  Úblank_user_audio_codesrŸ  Úreturn_dict_in_generateÚoutput_scoresrM   )rP   Úindexr   ©r@   rJ   )r&   r/   )r&   r'   r/   r:   )7Úhasattrr€  rÄ  Úvaluesr•  r–  r—  ÚpopÚ_prepare_generation_configÚ"_check_and_maybe_initialize_inputsrj   Úgetr”  rÇ  Ú_prepare_generated_lengthr  r‰   r
  r³  rÀ  r4   Úzerosrm   r�   Úsampling_rater{  Ú
frame_rater¥   r�   r|  r˜  Úrepeat_interleaveÚmaxÚ	num_beamsÚnum_return_sequencesÚgenerated_audio_codesrÐ  rÑ  rT   Úgenerater'   r+   rÈ   rµ   Úgatherr¹   rx   r@   rØ   r´  r>  rÞ   ÚdecodeÚaudio_valuesr%   )*rZ   rJ   rƒ  rK   r„  rL   rF  rÁ  rÂ  rµ  ré   ÚdÚmain_devicerå   ræ   rœ  Údepth_decoder_kwargs_keysrŸ  r¦  ÚinputsrÌ  rÈ  rÉ  Údepth_decoder_generation_configrM   r½  r¾  Úblank_input_valuesrÏ  rÐ  rÑ  r   Úoutput_text_idsr+   râ  Úexpanded_beam_indicesr@   Úlast_generated_audio_codesÚoutput_audio_codesÚmaskÚoutput_valuesr[   s*                                            €r<   rã  z&MoshiForConditionalGeneration.generateX  sþ  ø€ ô` �4˜Õ)´'¸$×:LÑ:LÈoÕ2^Ø/1ˆD×ÑÔ,Ø�T×'Ñ'Ñ'Ø37×3EÑ3E�×"Ñ"Õ0à*.×*<Ñ*<×*CÑ*CÓ*EÖb QÈÐRaÒIašqÒbÐcdÑe�ð '+×&8Ñ&8×&>Ñ&>Ó&@÷4á"˜˜UØ—~‘~ oÔ6ð œ˜OÓ,Ð.Ð/ÀÈÑ@X±Ð^cÑcó4�×"Ñ"Ô0ð .2×-?Ñ-?×-EÑ-EÓ-G÷"Ù)˜s EÈsÏ~É~Ð^mÕOn��U‘
ó"ˆDÔð ?EÖ$n°(È×H[ÑH[Ð\lÕHm¢XÐ$nÐ!Ð$nàTmö 
ØHPˆH”SÐ)Ó*Ð,Ð-¨v¯z©z¸(Ó/CÑCð 
Ðð  
ð
 %D D×$CÑ$CÀFÇJÁJÐObÐdhÓDiÑ$tÐmsÑ$tÑ!Ð˜6ð ×3Ñ3Ø#Ø"3Ø!1Ø#5Ø"3Ø+Ø,Gð 4ó ñ 	Tˆ	Ð#Ð%6Ð8Sð #,Ð"3‘¸ˆá3N˜6Ÿ<™<¨Ñ+¨aÒ/ÐTZ×T`ÑT`ÐacÑTdÐØ!'§¡¨LÓ!9¸TÐ!AÒ!nÐFW×FbÑFbÐjnÐFnÐØ!'§¡¨LÓ!9¸TÐ!AÒ!nÐFW×FbÑFbÐjnÐFnÐØ ×:Ñ:Ø/Ø#9Ø#9Ø09Ð0A™_À{Ø Ø-ð ;ó 
Ðô Ð$Ð&<Ô=Ø.?×.TÑ.TÑ+ð #×0Ñ0°1Ñ4Ø"×0Ñ0°1Ñ4Ø(0ñ/Ð+ð 	(×.Ñ.Ð/CÔDØ>ÐàŸ™Ð$4°dÓ;ˆØÐ!Ø!×HÑHØ#Ø"3Øð Ió ˆNð ×6Ñ6ØØ/Ø-Ø1Ø/Ø'Ø)Ø/Ø%)Ø(Cð 7ó 
ñ	
ØØØØØ#Ø$Øô #Ÿ[™[Ø× Ñ  Ñ# Q¬¨D¯K©K×,EÑ,EÈÏÉ×HhÑHh×HsÑHsÑ,sÓ(tÐuØ—*‘*Ø—;‘;ô
Ðð
 "&×!3Ñ!3×!:Ñ!:Ð;MÐ^b×^pÑ^pÐ!:Ó!qÐrsÑ!tÐð (?Ð'JÑ#ÐPV×PZÑPZÐ[tÓPuð 	Ð(Ñ)ð
 (Ð3ñ %à—‘Ð6Ó7ð 	Ð)Ñ*ô &+×%<Ñ%<ØœsÐ#4×#>Ñ#>Ð@Q×@fÑ@fÓgÐmnô&
ˆÔ"ð #4×"=Ñ"=ÀÑ"AÒ"nÐEV×EnÑEnÐØ)×3Ñ3°aÑ7ÒZÐ;L×;ZÑ;ZˆÜ‘'Ñ"ð 

Ø'ØØ/Ø#9Ø!5Ø$;Ø'Ø)ñ

ð ñ

ˆñ &Ñ.@Ù&Ð/@×/XÒ/XØ×(Ñ(Ð(ØˆNñ 'Ø%‰Oà%×/Ñ/ˆOà×1Ñ1°AÒ5Ü',×'>Ñ'>Ø(Ð*;×*PÑ*PÐVWô(Ð$ð ×&Ñ&¨Ó*ð #×/Ñ/²Ð3QÐ6G×6MÑ6MÈbÑ6QÐ5QÐ3QÐ0QÑRˆLà$(×$>Ñ$>ºqÂ!ÐEV×E\ÑE\Ð]_ÑE`ÑEbÐ?bÑ$cÐ!ð %1²°C°R°C°Ñ$8×$BÑ$BÀ1Ó$E×$LÑ$LÈRÐQU×QcÑQcÐegÓ$hÐ!Ü$)§L¡LÐ1FÈAÐUjÔ$kÐ!ô !&× 7Ñ 7Ø!Ð#4×#IÑ#IÈqô!Ðô */¯©Ð4EÐG\Ð3]ÐcdÔ)eˆDÔ&ô &+×%7Ñ%7¸×8NÑ8NÐTUÐ]iÒjkÐmoÐjoÑ]pÔ%qˆDÔ"ð !×2Ñ2×7Ñ7¸¸A¸t×?UÑ?U×?[Ñ?[Ð\^Ñ?_Ó`Ðà%@ T×%7Ñ%7×%@Ñ%@ð &
Ø/Ø%¢a¨© fÑ-×2Ñ2°2°qÓ9ñ&
ð #ñ&
Ð"ð &@ÂÀ1Á2ÀÑ%F×%PÑ%PÐQRÓ%SÐ"ä%*§Y¡Y°×0JÑ0JÐLfÐ/gÐmnÔ%oˆÔ"ð "×:Ñ:¸4×;UÑ;UÐWoÓpÐð (¨4¯;©;×+GÑ+GÑGˆà/°Ñ5×=Ñ=¸d¿j¹jÈ¹mÈT×M_ÑM_ÐacÓdÐàˆÙ!Ø ×.Ñ.×5Ñ5Ø"óç‰lð ñ 4FÑ/È4Ðà×4Ò4Ü;ð Ø -Ð;MñØQXñð ô 8Ø)°_ÐRdô
ð 	
ùòK cùó4ùó"ùò %oùò 
s0   Á=	\#Â\#Â.0\(Ä\.Ä(\.Ä:\4Å\4Å%\9rÏ  c                 ó  •— t        ‰| �  |f||||||||	|
||dœ|¤Ž}|d   ��a|j                  d«      }|j                  dd|j                  d   «      }|j                  d«      } | j
                  j                  d||j                  dd«      dœ|
¤Ž}|d d …dd …f   j                  d«      }| j                  t        j                  | j                  |j                  | j                  j                  «      gd¬«      |«      d d …d d …dd …f   }| j                  t        j                  | j                  |gd¬«      |	«      | _        | j                  || j                  d d …d d …dd …f   |¬	«      \  }}}}}}}d |d<   ||d
<   |S )N)r.   rM   rF  r¼   r  rp  r½  r¾  rŸ  Úis_first_iterationrÏ  rJ   r@   r]   r    rÓ  r   r´   )rL   rK   rF  r:   )rT   Úprepare_inputs_for_generationrÖ  rØ   rj   r€  rã  rÈ   r´  r4   r¹   râ  r¬   r�   rÀ  )rZ   rJ   r.   rM   rF  r¼   r  rp  r½  r¾  rŸ  rô  rÏ  ré   Úmodel_inputsr@   râ  rK   rÛ   r[   s                      €r<   rõ  z;MoshiForConditionalGeneration.prepare_inputs_for_generationV  sÛ  ø€ ô$ ‘wÑ<Øð
à+Ø)Ø'Ø%ØØ)Ø$;Ø%=Ø!5Ø1Ø#9ñ
ð ñ
ˆð& ˜Ñ$Ñ0Ø &§
¡
Ð+>Ó ?Ðà 1× 6Ñ 6°r¸1Ð>O×>UÑ>UÐVXÑ>YÓ ZÐà$×(Ñ(¨Ó5ˆIà$? D×$6Ñ$6×$?Ñ$?ð %Ø"3Ø#Ÿ.™.¨¨QÓ/ñ%ð 'ñ%Ð!ð %:º!¸Q¹R¸%Ñ$@×$JÑ$JÈ1Ó$MÐ!à#×<Ñ<Ü—	‘	Ø×/Ñ/Ð1G×1JÑ1JÈ4×KeÑKe×KlÑKlÓ1mÐnÐtuôð (ó	 ò
 ’�B‘Cˆiñ Ðð *.×)FÑ)FÜ—	‘	˜4×5Ñ5Ð7LÐMÐSTÔUÐWoó*ˆDÔ&ð /3×.XÑ.XØ¨T×-GÑ-GÊÊ1ÈbÉcÈ	Ñ-RÐeuð /Yó /Ñ+ˆM˜1˜a  A q¨!ð )-ˆL˜Ñ%Ø,9ˆL˜Ñ)àÐr;   r   Úmodel_kwargsÚis_encoder_decoderÚnum_new_tokensc                 ó”   •— t         ‰| �  ||||«      }|j                  d«      d d …dd …f   j                  «       }||d<   || _        |S )Nr@   r]   )rT   Ú#_update_model_kwargs_for_generationrÙ  rœ   r@   )rZ   r   r÷  rø  rù  r@   r[   s         €r<   rû  zAMoshiForConditionalGeneration._update_model_kwargs_for_generationž  s]   ø€ ô ‘wÑBØ�\Ð#5°~ó
ˆð $ŸK™KÐ(;Ó<ºQÀÁ¸VÑD×JÑJÓLÐØ,=ˆÐ(Ñ)à!2ˆÔØÐr;   c                 ó6   — | j                   j                  «       S rS   )r}  Úget_input_embeddingsrk   s    r<   rý  z2MoshiForConditionalGeneration.get_input_embeddings²  s   € Ø�|‰|×0Ñ0Ó2Ð2r;   c                 ó:   — | j                   j                  |«       y rS   )r}  Úset_input_embeddings)rZ   ræ   s     r<   rÿ  z2MoshiForConditionalGeneration.set_input_embeddingsµ  s   € Ø�‰×)Ñ)¨%Õ0r;   c                 ó6   — | j                   j                  «       S rS   )r}  Úget_output_embeddingsrk   s    r<   r  z3MoshiForConditionalGeneration.get_output_embeddings¸  s   € Ø�|‰|×1Ñ1Ó3Ð3r;   c                 ó:   — | j                   j                  |«       y rS   )r}  Úset_output_embeddings)rZ   Únew_embeddingss     r<   r  z3MoshiForConditionalGeneration.set_output_embeddings»  s   € Ø�‰×*Ñ*¨>Õ:r;   c                 ór   — | j                   j                  «       D ]	  }d|_        Œ d| j                   _        y)z3
        Freeze the audio encoder weights.
        FN)r|  Ú
parametersÚrequires_gradÚ_requires_grad©rZ   Úparams     r<   Úfreeze_audio_encoderz2MoshiForConditionalGeneration.freeze_audio_encoder¾  ó8   € ð ×'Ñ'×2Ñ2Ó4ò 	(ˆEØ"'ˆEÕð	(à,1ˆ×ÑÕ)r;   c                 ór   — | j                   j                  «       D ]	  }d|_        Œ d| j                   _        y)z3
        Freeze the depth encoder weights.
        FN)r€  r  r  r  r	  s     r<   Úfreeze_depth_decoderz2MoshiForConditionalGeneration.freeze_depth_decoderÆ  r  r;   c                 ój   — | j                   d   }|dd|…f   }t        j                  |dk(  | |«      } | S )z®Apply a delay pattern mask to the decoder input ids, only preserving predictions where
        the mask is set to -1, and otherwise setting to the value detailed in the mask.r]   .N)rj   r4   Úwhere)rJ   Údecoder_pad_token_maskrŸ   s      r<   r´  z6MoshiForConditionalGeneration.apply_delay_pattern_maskÎ  sD   € ð
 —/‘/ "Ñ%ˆØ!7¸¸X¸g¸X¸Ñ!FÐÜ—K‘KÐ 6¸"Ñ <¸iÐI_Ó`ˆ	ØÐr;   r“  rg  r”  c                 ó�  — |j                   \  }}}|�|n| j                  j                  }t        j                  |||ft        j
                  |j                  ¬«      dz  }t        ||dz
  «      }	|dd…dd|	…f   |dd…dd|	…f<   |dd…dd…d|	…f   |dd…dd…d|	dz   …f<   ||dd…dd…df<   ||dd…ddf<   |}
|dd|	…f   }||
fS )aI  Build a delayed pattern mask to the input_ids. Each codebook, except the first one, is offset by
        one, giving a delayed pattern mask at the start of sequence and end of sequence. Take the example where there
        are 4 codebooks and a max sequence length of 6, we have the delayed pattern mask of shape `(codebooks,
        seq_len)`:
        - [-1, -1, -1, -1, -1,  P]
        - [ B, -1, -1, -1, -1, -1]
        - [ B, -1, -1, -1, -1, -1]
        - [ B, -1, -1, -1, -1, -1]
        where B is the beginning-of-sentence token, P is the special padding token id and -1 indicates that the token is valid for prediction. If we include
        a prompt (input ids), the -1 positions indicate where new tokens should be predicted. Otherwise, the
        mask is set to the value in the prompt:
        - [ a0, a1, -1, -1, -1,  P]
        - [ B,  b0, b1, -1, -1, -1]
        - [ B,  c0, c1, -1, -1, -1]
        - [ B,  d0, d1, -1, -1, -1]
        where a-d indicate the codebook channel and 0/1 indicates the temporality. Now, we only override the -1
        tokens in our prediction.
        Nr¨  r]   r    r   .)rj   r¦  r”  r4   rX   rª  r�   Úmin)rZ   rJ   r“  rg  r”  Úbszr‰   rŸ   Úinput_ids_shiftedÚseq_len_to_keepÚpattern_masks              r<   rš  z6MoshiForConditionalGeneration.build_delay_pattern_maskØ  s  € ð* '0§o¡oÑ#ˆˆ]˜Gà#-Ð#9‘Z¸t×?UÑ?U×?`Ñ?`ˆ
ä�J‰J˜˜]¨JÐ7¼u¿z¹zÐR[×RbÑRbÔcÐfhÑhð 	ô
 ˜g z°A¡~Ó6ˆØ4=ºaÀÐDTÀ_ÐDTÐ>TÑ4UÐš!˜QÐ 0 Ð 0Ð0Ñ1ð =FÂaÈÉÐM]ÈoÐM]ÐF]Ñ<^Ðš!˜Q™R  _°qÑ%8Ð!8Ð8Ñ9ð '3Ðš!˜Q™R ˜(Ñ#Ø&2Ðš!˜Q ˜(Ñ#ð )ˆà% cÐ+;¨OÐ+;Ð&;Ñ<ˆ	Ø˜,Ð&Ð&r;   c                 óf  — t        j                  |df| j                  t         j                  ¬«      | j                  j
                  z  }t        j                  || j                  df| j                  t         j                  ¬«      | j                  j                  z  }t        j                  || j                  df| j                  t         j                  ¬«      | j                  j                  z  }t        j                  |df| j                  t         j                  ¬«      }t        ||||¬«      S )aà  
        Helper function to get null inputs for unconditional generation, enabling the model to be used without the
        feature extractor or tokenizer.

        Args:
            num_samples (int, *optional*):
                Number of audio samples to unconditionally generate.
            max_new_tokens (int, *optional*):
                Number of tokens to generate for each sample. More tokens means longer audio samples, at the expense of
                longer inference (since more audio tokens need to be generated per sample).

        Example:
        ```python
        >>> from transformers import MoshiForConditionalGeneration

        >>> model = MoshiForConditionalGeneration.from_pretrained("kmhf/hf-moshiko-pytorch-bf16")

        >>> # get the unconditional (or 'null') inputs for the model
        >>> unconditional_inputs = model.get_unconditional_inputs(num_samples=1)
        >>> audio_samples = model.generate(**unconditional_inputs, max_new_tokens=256)
        ```r    r¦   )rJ   rK   rL   rM   )
r4   rX   r�   r«   r�   r:  r‰   r>  rª  rI   )rZ   r¸  rJ   rK   rL   rM   s         r<   r¼  z6MoshiForConditionalGeneration.get_unconditional_inputs  sò   € ô. —J‘J ¨QÐ/¸¿¹Ì5Ï;É;ÔWÐZ^×ZeÑZe×ZpÑZpÑpˆ	ä�J‰J˜ T×%7Ñ%7¸Ð;ÀDÇKÁKÔW\×WbÑWbÔcØ�k‰k×*Ñ*ñ+ð 	ô
 �J‰J˜ T×%7Ñ%7¸Ð;ÀDÇKÁKÔW\×WbÑWbÔcØ�k‰k×*Ñ*ñ+ð 	ô Ÿ™ [°!Ð$4¸T¿[¹[ÔPU×PZÑPZÔ[ˆä&ØØ-Ø/Ø)ô	
ð 	
r;   c                 óž  — |€|n|}|€|n|}	|€|n|}
|	d uxs
 |
d uxs |d u}|xr |d uxr |d u  }|r|	€t        d«      ‚|r|
€t        d«      ‚|r|€t        d«      ‚|s7| j                  «       }|j                  }|j                  }|j                  }d}n±|	j
                  d   }|
j
                  d   }|j
                  d   }| j                  j                  j                  | j                  j                  z  }|€t        j                  ||z  «      n|}|€t        j                  ||z  «      n|}||k7  s||k7  rt        d«      ‚||||fS )Na  No user audio inputs have been passed alongside the other inputs. Make sure either `user_input_values` or `user_audio_codes` is passed or use `MoshiForConditionalGeneration.get_unconditional_inputs`. Check the `MoshiForConditionalGeneration` docstrings for more information.a  No Moshi audio inputs have been passed alongside the other inputs. Make sure either `moshi_input_values` or `moshi_audio_codes` is passed or use `MoshiForConditionalGeneration.get_unconditional_inputs`. Check the `MoshiForConditionalGeneration` docstrings for more information.zúNo `input_ids` or `inputs_embeds` have been passed alongside the other inputs. Make sure `input_ids` is passed or use `MoshiForConditionalGeneration.get_unconditional_inputs`. Check the `MoshiForConditionalGeneration` docstrings for more information.Fr]   r    zõAt least one of the 3 inputs of `MoshiForConditionalGeneration` doesn't have the same sequence length as the others.Make sure that they all have the same sequence length. Check the `MoshiForConditionalGeneration` docstrings for more information.)rÿ   r¼  rJ   rK   rL   rj   r�   r{  rÝ  rÜ  rý   Úceil)rZ   rJ   rƒ  rK   r„  rL   rF  rµ  rê  Ú
user_inputÚmoshi_inputÚone_input_has_been_passedr¿  Úuser_seq_lengthÚmoshi_seq_lengthÚtokens_seq_lengthÚratios                    r<   rØ  z@MoshiForConditionalGeneration._check_and_maybe_initialize_inputs.  sÎ  € ð ,Ð3‘¸ˆØ):Ð)BÑ%ÐHYˆ
Ø+=Ð+EÑ'ÐK]ˆà%/°tÐ%;Ò$qÀÐTXÐAXÒ$qÐ^dÐlpÐ^pÐ!ð 'Bò '
Ø Ð%Ò;¨)°tÐ*;ðG
Ð#ñ
 %¨*Ð*<Üð eóð ñ '¨KÐ,?Üð hóð ñ '¨F¨NÜð Móð ñ +à#'×#@Ñ#@Ó#BÐ Ø,×6Ñ6ˆIØ3×DÑDÐØ 4× FÑ FÐð +0Ñ'ð )×.Ñ.¨rÑ2ˆOØ*×0Ñ0°Ñ4ÐØ &§¡¨Q¡Ðà—K‘K×4Ñ4×?Ñ?À$Ç+Á+×B[ÑB[Ñ[ˆEØFWÐF_œtŸy™yÐ)9¸EÑ)AÔBÐeuÐØDTÐD\œdŸi™i¨¸%Ñ(?Ô@ÐbqˆOà Ð$4Ò4Ð8IÈ_Ò8\Ü ðXóð ð
 Ð*Ð,=Ð?ZÐZÐZr;   )NNNNNNNNNNNNNN)
NNNNNNNNFF)	NNNNNNTNT)NNNNTNNNNFN)Fr    rS   ©r    )NNNNNNN)1r0   r1   r2   r!   r6   Úoutput_modalitiesr5  r/  r1  r2  r3  r4  rU   r‚  r   r4   r7   rc  r8   r5   r   r!  r9   r   rh   r   Údictr¸   r   r³  rÀ  rÂ   rã  rõ  r   rm   rû  rý  rÿ  r  r  r  r  rÁ   r´  rš  r¼  rØ  rn   ro   s   @r<   rx  rx  	  sÓ  ø… ð ÓØ)ÐØ!€OØ&*Ð#ØÐØ€NØÐØ"&Ðð˜{õ ò"ð ð .2Ø26Ø6:Ø04Ø7;Ø15Ø(,Ø26Ø/3Ø04Ø!%Ø)-Ø,0Ø#'ñn
à×#Ñ# dÑ*ðn
ð ×(Ñ(¨4Ñ/ðn
ð !×,Ñ,¨tÑ3ð	n
ð
  Ÿ,™,¨Ñ-ðn
ð "×-Ñ-°Ñ4ðn
ð !Ÿ<™<¨$Ñ.ðn
ð  ™ðn
ð ×(Ñ(¨4Ñ/ðn
ð ×%Ñ%¨Ñ,ðn
ð ×&Ñ&¨Ñ-ðn
ð ˜$‘;ðn
ð   $™;ðn
ð # T™kðn
ð ˜D‘[ðn
ð" 
�Ñ	 ò#n
ó ðn
ð`à×#Ñ#ðð ,ðð �S˜#�X‘ð	ð
 
×	Ñ	óð6 .2Ø6:Ø04Ø7;Ø15Ø26Ø.2Ø59Ø).Ø,1ñc
à×#Ñ# dÑ*ðc
ð !×,Ñ,¨tÑ3ðc
ð  Ÿ,™,¨Ñ-ð	c
ð
 "×-Ñ-°Ñ4ðc
ð !Ÿ<™<¨$Ñ.ðc
ð ×(Ñ(¨4Ñ/ðc
ð Ÿ™ tÑ+ðc
ð ,¨dÑ2ðc
ð #'ðc
ð &*óc
ðJ €U‡]�]ƒ_ð .2Ø6:Ø04Ø7;Ø15Ø26Ø.2Ø*.Ø37ñ{
à×#Ñ# dÑ*ð{
ð !×,Ñ,¨tÑ3ð{
ð  Ÿ,™,¨Ñ-ð	{
ð
 "×-Ñ-°Ñ4ð{
ð !Ÿ<™<¨$Ñ.ð{
ð ×(Ñ(¨4Ñ/ð{
ð !% t¡ð{
ð ! 4™Kð{
ð &*¨D¡[ð{
ð 
×	Ñ	ô{
ó ð{
ð@ ØØØØØØ $Ø!%Ø!Ø Ø;?ñFð !&× 1Ñ 1°DÑ 8õFðX $)Øñàðð ˜3 ˜8‘nðð !ð	ð
 ðð 
ˆc�3ˆh‰õò(3ò1ò4ò;ò2ò2ð ñó ðð koñ+'Ø×)Ñ)ð+'Ø9<ð+'ØLOð+'Ø]`ÐcgÑ]gó+'óZ'
ðV ØØØØØØ$(÷;[r;   rx  )rm  rx  re  r#  r"  )r	  )Pr3   rý   Úcollections.abcr   Údataclassesr   Útypingr   r   r4   Útorch.nnrV   r   rÅ  r	   r+  Úactivationsr
   Úcache_utilsr   r   Ú
generationr   r   Úmasking_utilsr   Úmodeling_layersr   Úmodeling_outputsr   r   r   r   Úmodeling_rope_utilsr   r   Úmodeling_utilsr   r   Úprocessing_utilsr   Úutilsr   r   r   Úutils.genericr   Úauto.modeling_autor   Úconfiguration_moshir!   r"   Ú
get_loggerr0   rU  r%   r>   rB   rI   ÚModulerO   rq   r€   r�   rÆ   rÎ   rÐ   r5   rm   râ   re   rö   rø   r  r#  r7  re  rm  rx  Ú__all__r:   r;   r<   ú<module>r9     sà  ðñ ã Ý $Ý !ß  ã Ý Ý %å &Ý !ß .ß ;Ý /Ý 9ß mÓ mß Kß FÝ &ß @Ñ @Ý +Ý *ß >ð 
ˆ×	Ñ	˜HÓ	%€ñ ðôð
 ô+0¨{ó +0ó óð+0ñ\ ðôð
 ô< +ó <ó óð<ñ* ðôð
 ô"B¨{ó "Bó óð"BðJ Ø
ô3˜kó 3ó ó ð3ô(=�2—9‘9ô =ô&$˜"Ÿ)™)ô $ôN"�"—)‘)ô "ô&><˜2Ÿ9™9ô ><òD(óô2�R—Y‘Yô ð4	U˜UŸ\™\ð 	U°#ð 	U¸%¿,¹,ó 	Uð( ñ%Ø�I‰Ið%à�<‰<ð%ð 
�‰ð%ð �<‰<ð	%ð
 —L‘L 4Ñ'ð%ð ð%ð ð%ð Ð'Ñ(ó%ô2Q)�R—Y‘Yô Q)ôl:Ð2ô :ðz ô(˜?ó (ó ð(ô(P
Ð,¨oô P
ðf ôk
Ð%ó k
ó ðk
ñ\ ðôô
k
Ð+¨_ó k
óð
k
ñ\ ðôô
[[Ð$8¸/ó [[óð
[[ò| f�r;   