Ë
    T^(há ã                   óÜ  — d Z ddlZddlZddlmZ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mZmZ dd
lmZ ddlmZ ddlmZmZmZ ddlmZ ddlmZm Z m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+  e%«       rddl,m-Z- ddl.m/Z/  e(j`                  e1«      Z2dZ3	 ddl4m5Z5 dZ3e2jm                  d«       dZ:dZ;dZ< G d„ dejz                  «      Z>e3se5Z> ej~                  e>«        G d„ dejz                  «      Z@ G d „ d!ejz                  «      ZA G d"„ d#ejz                  «      ZB G d$„ d%ejz                  «      ZC G d&„ d'ejz                  «      ZD G d(„ d)ejz                  «      ZE G d*„ d+ejz                  «      ZF G d,„ d-e«      ZG G d.„ d/eG«      ZH G d0„ d1ejz                  «      ZId2ZJ e#d3eJ«       G d4„ d5eGe«      «       ZKd5d-gZLy# e7$ r Y �Œe8$ r e2js                  d«       Y �Œ.w xY w)6zPyTorch Pop2Piano model.é    N)ÚOptionalÚTupleÚUnion)Únn)ÚCrossEntropyLoss)ÚGenerationConfigé   )ÚACT2FN)ÚCacheÚDynamicCacheÚEncoderDecoderCacheÚStaticCache)ÚGenerationMixin)ÚAttentionMaskConverter)ÚBaseModelOutputÚ)BaseModelOutputWithPastAndCrossAttentionsÚSeq2SeqLMOutput)ÚPreTrainedModel)ÚALL_LAYERNORM_LAYERSÚ find_pruneable_heads_and_indicesÚprune_linear_layer)Úadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚis_torch_flex_attn_availableÚis_torch_fx_proxyÚis_torchdynamo_compilingÚloggingÚreplace_return_docstringsé   )ÚPop2PianoConfig)Ú	BlockMask)Úmake_flex_block_causal_maskT)ÚFusedRMSNormFzVDiscovered apex.normalization.FusedRMSNorm - will use it instead of Pop2PianoLayerNormzIDiscovered apex but it failed to load, falling back to Pop2PianoLayerNormr    zsweetcocoa/pop2pianoa/  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. Pop2Piano is a model with relative position embeddings
            so you should be able to pad the inputs on both the right and the left. Indices can be obtained using
            [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for detail.
            [What are input IDs?](../glossary#input-ids) To know more on how to prepare `input_ids` for pretraining
            take a look a [Pop2Piano Training](./Pop2Piano#training).
        attention_mask (`torch.FloatTensor` 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)
        decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
            Indices of decoder input sequence tokens in the vocabulary. Indices can be obtained using
            [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details.
            [What are decoder input IDs?](../glossary#decoder-input-ids) Pop2Piano uses the `pad_token_id` as the
            starting token for `decoder_input_ids` generation. If `past_key_values` is used, optionally only the last
            `decoder_input_ids` have to be input (see `past_key_values`). To know more on how to prepare
        decoder_attention_mask (`torch.BoolTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
            Default behavior: generate a tensor that ignores pad tokens in `decoder_input_ids`. Causal mask will also
            be used by default.
        head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules in the encoder. Mask values selected in `[0,
            1]`:
            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.
        decoder_head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules in the decoder. Mask values selected in `[0,
            1]`:
            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.
        cross_attn_head_mask (`torch.Tensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
                Mask to nullify selected heads of the cross-attention modules in the decoder. Mask values selected in
                `[0, 1]`:
                - 1 indicates the head is **not masked**,
                - 0 indicates the head is **masked**.
        encoder_outputs (`tuple(tuple(torch.FloatTensor)`, *optional*):
            Tuple consists of (`last_hidden_state`, `optional`: *hidden_states*, `optional`: *attentions*)
            `last_hidden_state` of shape `(batch_size, sequence_length, hidden_size)` is a sequence of hidden states at
            the output of the last layer of the encoder. Used in the cross-attention of the decoder.
        past_key_values (`tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`):
            Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding.
            If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
            don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
            `decoder_input_ids` of shape `(batch_size, sequence_length)`.
        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Does the same task as `inputs_embeds`. If `inputs_embeds` is not present but `input_features` is present
            then `input_features` will be considered as `inputs_embeds`.
        decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, target_sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `decoder_input_ids` you can choose to directly pass an embedded
            representation. If `past_key_values` is used, optionally only the last `decoder_inputs_embeds` have to be
            input (see `past_key_values`). This is useful if you want more control over how to convert
            `decoder_input_ids` indices into associated vectors than the model's internal embedding lookup matrix. If
            `decoder_input_ids` and `decoder_inputs_embeds` are both unset, `decoder_inputs_embeds` takes the value of
            `inputs_embeds`.
        use_cache (`bool`, *optional*):
            If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
            `past_key_values`).
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
        cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
            Indices depicting the position of the input sequence tokens in the sequence. It is used to update the
            cache in the correct position and to infer the complete sequence length.
c                   ó&   ‡ — e Zd Zdˆ fd„	Zd„ Zˆ xZS )ÚPop2PianoLayerNormc                 óŠ   •— t         ‰| �  «        t        j                  t	        j
                  |«      «      | _        || _        y)zj
        Construct a layernorm module in the Pop2Piano style. No bias and no subtraction of mean.
        N)ÚsuperÚ__init__r   Ú	ParameterÚtorchÚonesÚweightÚvariance_epsilon)ÚselfÚhidden_sizeÚepsÚ	__class__s      €ún/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/pop2piano/modeling_pop2piano.pyr(   zPop2PianoLayerNorm.__init__�   s1   ø€ ô 	‰ÑÔÜ—l‘l¤5§:¡:¨kÓ#:Ó;ˆŒØ #ˆÕó    c                 óž  — |j                  t        j                  «      j                  d«      j	                  dd¬«      }|t        j
                  || j                  z   «      z  }| j                  j                  t        j                  t        j                  fv r%|j                  | j                  j                  «      }| j                  |z  S )Né   éÿÿÿÿT)Úkeepdim)Útor*   Úfloat32ÚpowÚmeanÚrsqrtr-   r,   ÚdtypeÚfloat16Úbfloat16)r.   Úhidden_statesÚvariances      r2   ÚforwardzPop2PianoLayerNorm.forward¥   s›   € ð !×#Ñ#¤E§M¡MÓ2×6Ñ6°qÓ9×>Ñ>¸rÈ4Ð>ÓPˆØ%¬¯©°H¸t×?TÑ?TÑ4TÓ(UÑUˆð �;‰;×Ñ¤§¡´·±Ð ?Ñ?Ø)×,Ñ,¨T¯[©[×->Ñ->Ó?ˆMà�{‰{˜]Ñ*Ð*r3   )g�íµ ÷Æ°>)Ú__name__Ú
__module__Ú__qualname__r(   rB   Ú__classcell__©r1   s   @r2   r%   r%   œ   s   ø„ õ$ö+r3   r%   c                   ó*   ‡ — e Zd Zdefˆ fd„Zd„ Zˆ xZS )ÚPop2PianoDenseActDenseÚconfigc                 ó^  •— t         ‰| �  «        t        j                  |j                  |j
                  d¬«      | _        t        j                  |j
                  |j                  d¬«      | _        t        j                  |j                  «      | _
        t        |j                     | _        y ©NF©Úbias)r'   r(   r   ÚLinearÚd_modelÚd_ffÚwiÚwoÚDropoutÚdropout_rateÚdropoutr
   Údense_act_fnÚact©r.   rJ   r1   s     €r2   r(   zPop2PianoDenseActDense.__init__½   sn   ø€ Ü‰ÑÔÜ—)‘)˜FŸN™N¨F¯K©K¸eÔDˆŒÜ—)‘)˜FŸK™K¨¯©¸eÔDˆŒÜ—z‘z &×"5Ñ"5Ó6ˆŒÜ˜&×-Ñ-Ñ.ˆ�r3   c                 ó  — | j                  |«      }| j                  |«      }| j                  |«      }t        | j                  j
                  t        j                  «      r�|j                  | j                  j
                  j                  k7  r`| j                  j
                  j                  t        j                  k7  r/|j                  | j                  j
                  j                  «      }| j	                  |«      }|S ©N)rR   rX   rV   Ú
isinstancerS   r,   r*   ÚTensorr=   Úint8r8   )r.   r@   s     r2   rB   zPop2PianoDenseActDense.forwardÄ   sª   € ØŸ™ Ó.ˆØŸ™ Ó/ˆØŸ™ ]Ó3ˆä�t—w‘w—~‘~¤u§|¡|Ô4Ø×#Ñ# t§w¡w§~¡~×';Ñ';Ò;Ø—‘—‘×$Ñ$¬¯
©
Ò2à)×,Ñ,¨T¯W©W¯^©^×-AÑ-AÓBˆMØŸ™ Ó.ˆØÐr3   ©rC   rD   rE   r    r(   rB   rF   rG   s   @r2   rI   rI   ¼   s   ø„ ð/˜õ /ör3   rI   c                   ó*   ‡ — e Zd Zdefˆ fd„Zd„ Zˆ xZS )ÚPop2PianoDenseGatedActDenserJ   c                 óÀ  •— t         ‰| �  «        t        j                  |j                  |j
                  d¬«      | _        t        j                  |j                  |j
                  d¬«      | _        t        j                  |j
                  |j                  d¬«      | _        t        j                  |j                  «      | _        t        |j                     | _        y rL   )r'   r(   r   rO   rP   rQ   Úwi_0Úwi_1rS   rT   rU   rV   r
   rW   rX   rY   s     €r2   r(   z$Pop2PianoDenseGatedActDense.__init__Ô   sŠ   ø€ Ü‰ÑÔÜ—I‘I˜fŸn™n¨f¯k©kÀÔFˆŒ	Ü—I‘I˜fŸn™n¨f¯k©kÀÔFˆŒ	Ü—)‘)˜FŸK™K¨¯©¸eÔDˆŒÜ—z‘z &×"5Ñ"5Ó6ˆŒÜ˜&×-Ñ-Ñ.ˆ�r3   c                 ó,  — | j                  | j                  |«      «      }| j                  |«      }||z  }| j                  |«      }t	        | j
                  j                  t        j                  «      r�|j                  | j
                  j                  j                  k7  r`| j
                  j                  j                  t        j                  k7  r/|j                  | j
                  j                  j                  «      }| j                  |«      }|S r[   )rX   rc   rd   rV   r\   rS   r,   r*   r]   r=   r^   r8   )r.   r@   Úhidden_geluÚhidden_linears       r2   rB   z#Pop2PianoDenseGatedActDense.forwardÜ   s¿   € Ø—h‘h˜tŸy™y¨Ó7Ó8ˆØŸ	™	 -Ó0ˆØ# mÑ3ˆØŸ™ ]Ó3ˆô �t—w‘w—~‘~¤u§|¡|Ô4Ø×#Ñ# t§w¡w§~¡~×';Ñ';Ò;Ø—‘—‘×$Ñ$¬¯
©
Ò2à)×,Ñ,¨T¯W©W¯^©^×-AÑ-AÓBˆMàŸ™ Ó.ˆØÐr3   r_   rG   s   @r2   ra   ra   Ó   s   ø„ ð/˜õ /ör3   ra   c                   ó*   ‡ — e Zd Zdefˆ fd„Zd„ Zˆ xZS )ÚPop2PianoLayerFFrJ   c                 ó  •— t         ‰| �  «        |j                  rt        |«      | _        nt        |«      | _        t        |j                  |j                  ¬«      | _	        t        j                  |j                  «      | _        y )N©r0   )r'   r(   Úis_gated_actra   ÚDenseReluDenserI   r%   rP   Úlayer_norm_epsilonÚ
layer_normr   rT   rU   rV   rY   s     €r2   r(   zPop2PianoLayerFF.__init__ò   s_   ø€ Ü‰ÑÔØ×ÒÜ"=¸fÓ"EˆDÕä"8¸Ó"@ˆDÔä,¨V¯^©^À×AZÑAZÔ[ˆŒÜ—z‘z &×"5Ñ"5Ó6ˆ�r3   c                 ór   — | j                  |«      }| j                  |«      }|| j                  |«      z   }|S r[   )ro   rm   rV   )r.   r@   Úforwarded_statess      r2   rB   zPop2PianoLayerFF.forwardü   s=   € ØŸ?™?¨=Ó9ÐØ×.Ñ.Ð/?Ó@ÐØ%¨¯©Ð5EÓ(FÑFˆØÐr3   r_   rG   s   @r2   ri   ri   ñ   s   ø„ ð7˜õ 7ör3   ri   c                   ón   ‡ — e Zd Z	 	 ddedee   fˆ fd„Zd„ Zed	d„«       Z	d
d„Z
	 	 	 	 	 	 	 	 	 dd„Zˆ xZS )ÚPop2PianoAttentionrJ   Ú	layer_idxc                 ó  •— t         ‰| �  «        |j                  | _        || _        |j                  | _        |j
                  | _        |j                  | _        |j                  | _        |j                  | _
        |j                  | _        | j                  | j                  z  | _        || _        |€9| j                  r-t        j!                  d| j"                  j$                  › d�«       t'        j(                  | j                  | j                  d¬«      | _        t'        j(                  | j                  | j                  d¬«      | _        t'        j(                  | j                  | j                  d¬«      | _        t'        j(                  | j                  | j                  d¬«      | _        | j                  r/t'        j2                  | j                  | j                  «      | _        t7        «       | _        d| _        y )NzInstantiating a decoder z³ without passing `layer_idx` is not recommended and will to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` when creating this class.FrM   )r'   r(   Ú
is_decoderÚhas_relative_attention_biasÚrelative_attention_num_bucketsÚrelative_attention_max_distancerP   Úd_kvÚkey_value_proj_dimÚ	num_headsÚn_headsrU   rV   Ú	inner_dimrt   ÚloggerÚwarning_oncer1   rC   r   rO   ÚqÚkÚvÚoÚ	EmbeddingÚrelative_attention_biasÚsetÚpruned_headsÚgradient_checkpointing©r.   rJ   rw   rt   r1   s       €r2   r(   zPop2PianoAttention.__init__  ss  ø€ ô 	‰ÑÔØ ×+Ñ+ˆŒØ+FˆÔ(Ø.4×.SÑ.SˆÔ+Ø/5×/UÑ/UˆÔ,Ø—~‘~ˆŒØ"(§+¡+ˆÔØ×'Ñ'ˆŒØ×*Ñ*ˆŒØŸ™¨×(?Ñ(?Ñ?ˆŒØ"ˆŒØÐ §¢Ü×ÑØ*¨4¯>©>×+BÑ+BÐ*Cð D,ð ,ôô —‘˜4Ÿ<™<¨¯©¸eÔDˆŒÜ—‘˜4Ÿ<™<¨¯©¸eÔDˆŒÜ—‘˜4Ÿ<™<¨¯©¸eÔDˆŒÜ—‘˜4Ÿ>™>¨4¯<©<¸eÔDˆŒà×+Ò+Ü+-¯<©<¸×8[Ñ8[Ð]a×]iÑ]iÓ+jˆDÔ(Ü›EˆÔØ&+ˆÕ#r3   c                 ó  — t        |«      dk(  ry t        || j                  | j                  | j                  «      \  }}t        | j                  |«      | _        t        | j                  |«      | _        t        | j                  |«      | _        t        | j                  |d¬«      | _	        | j                  t        |«      z
  | _        | j                  | j                  z  | _
        | j                  j                  |«      | _        y )Nr   r   ©Údim)Úlenr   r}   r{   rˆ   r   r�   r‚   rƒ   r„   r~   Úunion)r.   ÚheadsÚindexs      r2   Úprune_headszPop2PianoAttention.prune_heads(  sÆ   € Üˆu‹:˜Š?ØÜ7Ø�4—<‘< ×!8Ñ!8¸$×:KÑ:Kó
‰ˆˆuô $ D§F¡F¨EÓ2ˆŒÜ# D§F¡F¨EÓ2ˆŒÜ# D§F¡F¨EÓ2ˆŒÜ# D§F¡F¨E°qÔ9ˆŒà—|‘|¤c¨%£jÑ0ˆŒØ×0Ñ0°4·<±<Ñ?ˆŒØ ×-Ñ-×3Ñ3°EÓ:ˆÕr3   c                 óT  — d}|rC|dz  }|| dkD  j                  t        j                  «      |z  z  }t        j                  | «      } n*t        j                  | t        j
                  | «      «       } |dz  }| |k  }|t        j                  | j                  «       |z  «      t        j                  ||z  «      z  ||z
  z  j                  t        j                  «      z   }t        j                  |t        j                  ||dz
  «      «      }|t        j                  || |«      z  }|S )aÒ  
        Adapted from Mesh Tensorflow:
        https://github.com/tensorflow/mesh/blob/0cb87fe07da627bf0b7e60475d59f95ed6b5be3d/mesh_tensorflow/transformer/transformer_layers.py#L593

        Translate relative position to a bucket number for relative attention. The relative position is defined as
        memory_position - query_position, i.e. the distance in tokens from the attending position to the attended-to
        position. If bidirectional=False, then positive relative positions are invalid. We use smaller buckets for
        small absolute relative_position and larger buckets for larger absolute relative_positions. All relative
        positions >=max_distance map to the same bucket. All relative positions <=-max_distance map to the same bucket.
        This should allow for more graceful generalization to longer sequences than the model has been trained on

        Args:
            relative_position: an int32 Tensor
            bidirectional: a boolean - whether the attention is bidirectional
            num_buckets: an integer
            max_distance: an integer

        Returns:
            a Tensor with the same shape as relative_position, containing int32 values in the range [0, num_buckets)
        r   r5   r   )r8   r*   ÚlongÚabsÚminÚ
zeros_likeÚlogÚfloatÚmathÚ	full_likeÚwhere)Úrelative_positionÚbidirectionalÚnum_bucketsÚmax_distanceÚrelative_bucketsÚ	max_exactÚis_smallÚrelative_position_if_larges           r2   Ú_relative_position_bucketz,Pop2PianoAttention._relative_position_bucket8  s(  € ð, ÐÙØ˜AÑˆKØÐ!2°QÑ!6× :Ñ :¼5¿:¹:Ó FÈÑ TÑTÐÜ %§	¡	Ð*;Ó <Ñä!&§¡Ð+<¼e×>NÑ>NÐO`Ó>aÓ!bÐ bÐð   1Ñ$ˆ	Ø$ yÑ0ˆð &/Ü�I‰IÐ'×-Ñ-Ó/°)Ñ;Ó<Ü�h‰h�| iÑ/Ó0ñ1à˜YÑ&ñ(÷ ‰"ŒU�Z‰Z‹.ñ	&Ð"ô
 &+§Y¡YØ&¬¯©Ð8RÐT_ÐbcÑTcÓ(dó&
Ð"ð 	œEŸK™K¨Ð2CÐE_Ó`Ñ`ÐØÐr3   c                 ó  — |€ | j                   j                  j                  }|€.t        j                  |t        j
                  |¬«      dd…df   }n|dd…df   j                  |«      }t        j                  |t        j
                  |¬«      ddd…f   }||z
  }| j                  || j                   | j                  | j                  ¬«      }| j                  |«      }	|	j                  g d¢«      j                  d«      }	|	S )z%Compute binned relative position biasN)r=   Údevice)rž   rŸ   r    )r5   r   r   r   )r†   r,   r§   r*   Úaranger”   r8   r¥   rv   rx   ry   ÚpermuteÚ	unsqueeze)
r.   Úquery_lengthÚ
key_lengthr§   Úcache_positionÚcontext_positionÚmemory_positionr�   Úrelative_position_bucketÚvaluess
             r2   Úcompute_biaszPop2PianoAttention.compute_biash  s÷   € àˆ>Ø×1Ñ1×8Ñ8×?Ñ?ˆFØÐ!Ü$Ÿ|™|¨LÄÇ
Á
ÐSYÔZÒ[\Ð^bÐ[bÑcÑà-ªa°¨gÑ6×9Ñ9¸&ÓAÐÜŸ,™, z¼¿¹ÈFÔSÐTXÒZ[ÐT[Ñ\ˆØ+Ð.>Ñ>ÐØ#'×#AÑ#AØØ#Ÿ™Ð.Ø×;Ñ;Ø×=Ñ=ð	 $Bó $
Ð ð ×-Ñ-Ð.FÓGˆØ—‘¢	Ó*×4Ñ4°QÓ7ˆØˆr3   c                 ó  — |j                   dd \  }}|du}| j                  |«      }|j                  |d| j                  | j                  «      j                  dd«      }|�@|j                  j                  | j                  «      }|r|j                  }n|j                  }|r|n|}|r7|�5r3j                  | j                     }|j                  | j                     }nØ| j                  |«      }| j                  |«      }|j                  |d| j                  | j                  «      j                  dd«      }|j                  |d| j                  | j                  «      j                  dd«      }|�D|s|
nd}
j                  ||| j                  d|
i«      \  }}|rd|j                  | j                  <   t!        j"                  ||j                  dd«      «      }|€×|j                   d   }|�|n|
d   dz   }| j$                  sZt!        j&                  d| j                  ||f|j(                  |j*                  ¬	«      }| j,                  rE| j.                  r9d|_        n1| j3                  |||j(                  |
¬
«      }|dd…dd…| d…dd…f   }|�#|dd…dd…dd…d|j                   d   …f   }||z   }| j4                  rRt!        j6                  |j                   d   «      }d|t9        | j4                  «      <   |dd…|j;                  «       f   }n|}||z  }t<        j>                  jA                  |jC                  «       d¬«      jE                  |«      }t<        j>                  jG                  || jF                  | j.                  ¬«      }|�||z  }t!        j"                  ||«      }|j                  dd«      jI                  «       }|j                  |d| jJ                  «      }| jM                  |«      }|||f}|	r||fz   }|S )z€
        Self-attention (if key_value_states is None) or attention over source sentence (provided by key_value_states).
        Nr5   r6   r   r­   Tr	   éþÿÿÿ)r§   r=   )r§   r­   r   rŒ   )ÚpÚtraining)'Úshaper�   Úviewr}   r{   Ú	transposeÚ
is_updatedÚgetrt   Úcross_attention_cacheÚself_attention_cacheÚ	key_cacheÚvalue_cacher‚   rƒ   Úupdater*   Úmatmulrw   Úzerosr§   r=   r‰   r¶   Úrequires_gradr²   rˆ   r+   ÚlistÚboolr   Ú
functionalÚsoftmaxr™   Útype_asrV   Ú
contiguousr~   r„   )r.   r@   ÚmaskÚkey_value_statesÚposition_biasÚpast_key_valueÚlayer_head_maskr«   Ú	use_cacheÚoutput_attentionsr­   Ú
batch_sizeÚ
seq_lengthÚis_cross_attentionÚquery_statesrº   Úcurr_past_key_valueÚcurrent_statesÚ
key_statesÚvalue_statesÚscoresr¬   Úreal_seq_lengthÚcausal_maskÚposition_bias_maskedÚattn_weightsÚattn_outputÚoutputss                               r2   rB   zPop2PianoAttention.forward|  sã  € ð$ "/×!4Ñ!4°R°aÐ!8Ñˆ
�Jð .°TÐ9Ðà—v‘v˜mÓ,ˆØ#×(Ñ(¨°R¸¿¹Àt×G^ÑG^Ó_×iÑiÐjkÐmnÓoˆàÐ%Ø'×2Ñ2×6Ñ6°t·~±~ÓFˆJÙ!à&4×&JÑ&JÑ#à&4×&IÑ&IÐ#á-?Ñ)À]ˆÙ .Ð"<Áà,×6Ñ6°t·~±~ÑFˆJØ.×:Ñ:¸4¿>¹>ÑJ‰LàŸ™ Ó/ˆJØŸ6™6 .Ó1ˆLØ#Ÿ™¨°R¸¿¹Àt×G^ÑG^Ó_×iÑiÐjkÐmnÓoˆJØ'×,Ñ,¨Z¸¸T¿\¹\È4×KbÑKbÓc×mÑmÐnoÐqrÓsˆLàÐ)á7I¡Èt�Ø+>×+EÑ+EØ ¨d¯n©nÐ?OÐQ_Ð>`ó,Ñ(�
˜Lñ &Ø@D�N×-Ñ-¨d¯n©nÑ=ô —‘˜l¨J×,@Ñ,@ÀÀAÓ,FÓGˆàÐ Ø#×)Ñ)¨"Ñ-ˆJà.:Ð.F™lÈNÐ[]ÑL^ÐabÑLbˆOØ×3Ò3Ü %§¡Ø˜Ÿ™ j°*Ð=ÀfÇmÁmÐ[a×[gÑ[gô!�ð ×.Ò.°4·=²=Ø26�MÕ/à $× 1Ñ 1Ø# Z¸¿¹ÐVdð !2ó !�ð !.ªa²°Z°K±LÂ!Ð.CÑ D�àÐØ"¢1¢aªÐ,B¨j×.>Ñ.>¸rÑ.BÐ,BÐ#BÑC�Ø -°Ñ ;�à×ÒÜ—:‘:˜m×1Ñ1°!Ñ4Ó5ˆDØ,-ˆD”�d×'Ñ'Ó(Ñ)Ø#0²°D·I±I³K°Ñ#@Ñ à#0Ð àÐ&Ñ&ˆô —}‘}×,Ñ,¨V¯\©\«^ÀÐ,ÓD×LÑLÈVÓTˆÜ—}‘}×,Ñ,¨\¸T¿\¹\ÐTX×TaÑTaÐ,Óbˆð Ð&Ø'¨/Ñ9ˆLä—l‘l <°Ó>ˆà!×+Ñ+¨A¨qÓ1×<Ñ<Ó>ˆØ!×&Ñ& z°2°t·~±~ÓFˆØ—f‘f˜[Ó)ˆà °Ð>ˆáØ  Ñ/ˆGØˆr3   ©FN)Té    é€   )NN)	NNNNNNFFN)rC   rD   rE   r    r   Úintr(   r’   Ústaticmethodr¥   r²   rB   rF   rG   s   @r2   rs   rs     si   ø„ ð %*Ø#'ñ	!,àð!,ð ˜C‘=õ	!,òF;ð  ò- ó ð- ó^ð. ØØØØØØØØ÷ir3   rs   c                   óB   ‡ — e Zd Zddee   fˆ fd„Z	 	 	 	 	 	 	 dd„Zˆ xZS )ÚPop2PianoLayerSelfAttentionrt   c                 óÜ   •— t         ‰| �  «        t        |||¬«      | _        t	        |j
                  |j                  ¬«      | _        t        j                  |j                  «      | _        y )N©rw   rt   rk   )r'   r(   rs   ÚSelfAttentionr%   rP   rn   ro   r   rT   rU   rV   rŠ   s       €r2   r(   z$Pop2PianoLayerSelfAttention.__init__ê  sT   ø€ Ü‰ÑÔÜ/ØÐ0KÐW`ô
ˆÔô -¨V¯^©^À×AZÑAZÔ[ˆŒÜ—z‘z &×"5Ñ"5Ó6ˆ�r3   c	           
      óš   — | j                  |«      }	| j                  |	|||||||¬«      }
|| j                  |
d   «      z   }|f|
dd  z   }|S )N)rÊ   rÌ   rÎ   rÍ   rÏ   rÐ   r­   r   r   )ro   ré   rV   )r.   r@   Úattention_maskrÌ   rÎ   rÍ   rÏ   rÐ   r­   Únormed_hidden_statesÚattention_outputrß   s               r2   rB   z#Pop2PianoLayerSelfAttention.forwardò  sv   € ð  $Ÿ™¨}Ó=ÐØ×-Ñ-Ø ØØ'Ø+Ø)ØØ/Ø)ð .ó 	
Ðð &¨¯©Ð5EÀaÑ5HÓ(IÑIˆØ Ð"Ð%5°a°bÐ%9Ñ9ˆØˆr3   rà   )NNNNFFN©rC   rD   rE   r   rã   r(   rB   rF   rG   s   @r2   ræ   ræ   é  s0   ø„ ñ7ÈXÐVYÉ]õ 7ð ØØØØØØ÷r3   ræ   c                   óD   ‡ — e Zd Zddee   fˆ fd„Z	 	 	 	 	 	 	 	 dd„Zˆ xZS )ÚPop2PianoLayerCrossAttentionrt   c                 óÜ   •— t         ‰| �  «        t        |d|¬«      | _        t	        |j
                  |j                  ¬«      | _        t        j                  |j                  «      | _        y )NFrè   rk   )r'   r(   rs   ÚEncDecAttentionr%   rP   rn   ro   r   rT   rU   rV   )r.   rJ   rt   r1   s      €r2   r(   z%Pop2PianoLayerCrossAttention.__init__  sO   ø€ Ü‰ÑÔÜ1°&ÐV[ÐgpÔqˆÔÜ,¨V¯^©^À×AZÑAZÔ[ˆŒÜ—z‘z &×"5Ñ"5Ó6ˆ�r3   c                 óž   — | j                  |«      }| j                  |||||||||	|
¬«
      }|| j                  |d   «      z   }|f|dd  z   }|S )N)	rÊ   rË   rÌ   rÎ   rÍ   rÏ   r«   rÐ   r­   r   r   )ro   rò   rV   )r.   r@   rË   rë   rÌ   rÎ   rÍ   rÏ   r«   rÐ   r­   rì   rí   Úlayer_outputrß   s                  r2   rB   z$Pop2PianoLayerCrossAttention.forward  s{   € ð  $Ÿ™¨}Ó=ÐØ×/Ñ/Ø ØØ-Ø'Ø+Ø)ØØ%Ø/Ø)ð 0ó 
Ðð % t§|¡|Ð4DÀQÑ4GÓ'HÑHˆØ�/Ð$4°Q°RÐ$8Ñ8ˆØˆr3   r[   )NNNNFNFNrî   rG   s   @r2   rð   rð     s2   ø„ ñ7¨(°3©-õ 7ð ØØØØØØØ÷r3   rð   c                   óL   ‡ — e Zd Zddee   fˆ fd„Z	 	 	 	 	 	 	 	 	 	 	 	 dd„Zˆ xZS )ÚPop2PianoBlockrt   c                 óp  •— t         ‰| �  «        |j                  | _        t        j                  «       | _        | j
                  j                  t        |||¬«      «       | j                  r&| j
                  j                  t        ||¬«      «       | j
                  j                  t        |«      «       y )Nrè   )rt   )
r'   r(   rv   r   Ú
ModuleListÚlayerÚappendræ   rð   ri   rŠ   s       €r2   r(   zPop2PianoBlock.__init__6  s†   ø€ Ü‰ÑÔØ ×+Ñ+ˆŒÜ—]‘]“_ˆŒ
Ø�
‰
×ÑÜ'ØÐ4OÐ[dôô	
ð
 �?Š?Ø�J‰J×ÑÔ:¸6ÈYÔWÔXà�
‰
×ÑÔ*¨6Ó2Õ3r3   c                 ó¸  —  | j                   d   |||||	|
||¬«      }|d d \  }}	|dd  }|j                  t        j                  k(  r¢t        j                  t        j
                  |«      j                  «       t        j                  |j                  «      j                  dz
  t        j                  |j                  «      j                  «      }t        j                  || |¬«      }| j                  xr |d u}|ró | j                   d   ||||||	|d   dz   |
|¬«	      }|d d \  }}	|j                  t        j                  k(  r¢t        j                  t        j
                  |«      j                  «       t        j                  |j                  «      j                  dz
  t        j                  |j                  «      j                  «      }t        j                  || |¬«      }||dd  z   } | j                   d   |«      }|j                  t        j                  k(  r¢t        j                  t        j
                  |«      j                  «       t        j                  |j                  «      j                  dz
  t        j                  |j                  «      j                  «      }t        j                  || |¬«      }|f}|
r||	fz   |z   }|S ||z   }|S )	Nr   )rë   rÌ   rÎ   rÍ   rÏ   rÐ   r­   r5   iè  )r–   Úmaxr   r6   )rË   rë   rÌ   rÎ   rÍ   r«   rÏ   rÐ   )rù   r=   r*   r>   rœ   ÚisinfÚanyÚfinforü   Úclamprv   )r.   r@   rë   rÌ   Úencoder_hidden_statesÚencoder_attention_maskÚencoder_decoder_position_biasrÎ   Úcross_attn_layer_head_maskrÍ   rÏ   rÐ   Úreturn_dictr­   Úself_attention_outputsÚattention_outputsÚclamp_valueÚdo_cross_attentionÚcross_attention_outputsrß   s                       r2   rB   zPop2PianoBlock.forwardD  s   € ð  "/ §¡¨A¡ØØ)Ø'Ø+Ø)ØØ/Ø)ô	"
Ðð )?¸rÀÐ(BÑ%ˆ�~Ø2°1°2Ð6Ðð ×Ñ¤%§-¡-Ò/ÜŸ+™+Ü—‘˜MÓ*×.Ñ.Ó0Ü—‘˜M×/Ñ/Ó0×4Ñ4°tÑ;Ü—‘˜M×/Ñ/Ó0×4Ñ4óˆKô
 "ŸK™K¨¸K¸<È[ÔYˆMà!Ÿ_™_ÒRÐ1FÈdÐ1RÐÙØ&3 d§j¡j°¡mØØ!6Ø5Ø;Ø :Ø-Ø+¨BÑ/°!Ñ3Ø#Ø"3ô
'Ð#ð -DÀBÀQÐ,GÑ)ˆM˜>ð ×"Ñ"¤e§m¡mÒ3Ü#Ÿk™kÜ—K‘K Ó.×2Ñ2Ó4Ü—K‘K × 3Ñ 3Ó4×8Ñ8¸4Ñ?Ü—K‘K × 3Ñ 3Ó4×8Ñ8ó�ô
 !&§¡¨MÀ¸|ÐQ\Ô ]�ð !2Ð4KÈAÈBÐ4OÑ OÐð '˜Ÿ
™
 2™ }Ó5ˆð ×Ñ¤%§-¡-Ò/ÜŸ+™+Ü—‘˜MÓ*×.Ñ.Ó0Ü—‘˜M×/Ñ/Ó0×4Ñ4°tÑ;Ü—‘˜M×/Ñ/Ó0×4Ñ4óˆKô
 "ŸK™K¨¸K¸<È[ÔYˆMà Ð"ˆáØ Ð 1Ñ1Ð4EÑEˆGð ˆð Ð 1Ñ1ˆGàˆr3   rà   )NNNNNNNNFFTNrî   rG   s   @r2   rö   rö   5  s@   ø„ ñ4ÈXÐVYÉ]õ 4ð" ØØ"Ø#Ø&*ØØ#'ØØØØØ÷Tr3   rö   c                   ó@   — e Zd ZdZeZdZdZdZdZ	dZ
dgZdgZd„ Zd„ Zy	)
ÚPop2PianoPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    ÚtransformerFTrö   rS   c                 ó  — | j                   j                  }t        |t        «      r)|j                  j
                  j                  |dz  «       yt        |t        «      r5|j                  j                  j
                  j                  d|dz  ¬«       yt        |t        «      r�|j                  j                  j
                  j                  d|dz  ¬«       t        |d«      rL| j                   j                  s5|j                  j                  j
                  j                  d|dz  ¬«       yyyt        |t        «      �rM|j                   j                  j
                  j                  d|| j                   j"                  dz  z  ¬«       t        |j                   d«      rD|j                   j$                  �.|j                   j$                  j
                  j'                  «        |j(                  j                  j
                  j                  d|| j                   j*                  dz  z  ¬«       t        |j(                  d«      rF|j(                  j$                  �/|j(                  j$                  j
                  j'                  «        yyyt        |t,        «      �rò|j.                  j                  j
                  j                  d|| j                   j"                  dz  z  ¬«       t        |j.                  d«      rD|j.                  j$                  �.|j.                  j$                  j
                  j'                  «        |j0                  j                  j
                  j                  d|| j                   j"                  dz  z  ¬«       t        |j0                  d«      rD|j0                  j$                  �.|j0                  j$                  j
                  j'                  «        |j(                  j                  j
                  j                  d|| j                   j*                  dz  z  ¬«       t        |j(                  d«      rF|j(                  j$                  �/|j(                  j$                  j
                  j'                  «        yyyt        |t2        «      �ri| j                   j"                  }| j                   j4                  }| j                   j6                  }|j8                  j                  j
                  j                  d|||z  dz  z  ¬«       |j:                  j                  j
                  j                  d||dz  z  ¬«       |j<                  j                  j
                  j                  d||dz  z  ¬«       |j>                  j                  j
                  j                  d|||z  dz  z  ¬«       |j@                  r8|jB                  j                  j
                  j                  d||dz  z  ¬«       yyy)zInitialize the weightsç      ð?ç        )r;   ÚstdÚlm_headç      à¿rN   N)"rJ   Úinitializer_factorr\   r%   r,   ÚdataÚfill_ÚPop2PianoConcatEmbeddingToMelÚ	embeddingÚnormal_Ú!Pop2PianoForConditionalGenerationÚsharedÚhasattrÚtie_word_embeddingsr  rI   rR   rP   rN   Úzero_rS   rQ   ra   rc   rd   rs   rz   r|   r�   r‚   rƒ   r„   rw   r†   )r.   ÚmoduleÚfactorrP   r{   r}   s         r2   Ú_init_weightsz&Pop2PianoPreTrainedModel._init_weightsª  sq  € à—‘×/Ñ/ˆÜ�fÔ0Ô1Ø�M‰M×Ñ×$Ñ$ V¨c¡\Õ2Ü˜Ô =Ô>Ø×Ñ×#Ñ#×(Ñ(×0Ñ0°c¸vÈ¹|Ð0ÕLÜ˜Ô AÔBð �M‰M× Ñ ×%Ñ%×-Ñ-°3¸FÀS¹LÐ-ÔIÜ�v˜yÔ)°$·+±+×2QÒ2QØ—‘×%Ñ%×*Ñ*×2Ñ2¸ÀÈ#ÁÐ2ÕNð 3RÐ)ä˜Ô 6Õ7ð �I‰I×Ñ×!Ñ!×)Ñ)¨s¸À4Ç;Á;×CVÑCVÐ[_ÑB_Ñ8`Ð)ÔaÜ�v—y‘y &Ô)¨f¯i©i¯n©nÐ.HØ—	‘	—‘×#Ñ#×)Ñ)Ô+Ø�I‰I×Ñ×!Ñ!×)Ñ)¨s¸À4Ç;Á;×CSÑCSÐX\ÑB\Ñ8]Ð)Ô^Ü�v—y‘y &Ô)¨f¯i©i¯n©nÐ.HØ—	‘	—‘×#Ñ#×)Ñ)Õ+ð /IÐ)ä˜Ô ;Õ<Ø�K‰K×Ñ×#Ñ#×+Ñ+°¸&ÀTÇ[Á[×EXÑEXÐ]aÑDaÑ:bÐ+ÔcÜ�v—{‘{ FÔ+°·±×0@Ñ0@Ð0LØ—‘× Ñ ×%Ñ%×+Ñ+Ô-Ø�K‰K×Ñ×#Ñ#×+Ñ+°¸&ÀTÇ[Á[×EXÑEXÐ]aÑDaÑ:bÐ+ÔcÜ�v—{‘{ FÔ+°·±×0@Ñ0@Ð0LØ—‘× Ñ ×%Ñ%×+Ñ+Ô-Ø�I‰I×Ñ×!Ñ!×)Ñ)¨s¸À4Ç;Á;×CSÑCSÐX\ÑB\Ñ8]Ð)Ô^Ü�v—y‘y &Ô)¨f¯i©i¯n©nÐ.HØ—	‘	—‘×#Ñ#×)Ñ)Õ+ð /IÐ)ä˜Ô 2Õ3ð —k‘k×)Ñ)ˆGØ!%§¡×!1Ñ!1ÐØ—k‘k×+Ñ+ˆGØ�H‰H�O‰O× Ñ ×(Ñ(¨c°vÀ'ÐL^ÑB^ÐcgÑAgÑ7hÐ(ÔiØ�H‰H�O‰O× Ñ ×(Ñ(¨c°vÀÈ$ÁÑ7OÐ(ÔPØ�H‰H�O‰O× Ñ ×(Ñ(¨c°vÀÈ$ÁÑ7OÐ(ÔPØ�H‰H�O‰O× Ñ ×(Ñ(¨c°vÀ'ÐL^ÑB^ÐcgÑAgÑ7hÐ(ÔiØ×1Ò1Ø×.Ñ.×5Ñ5×:Ñ:×BÑBÈÐQWÐ\cÐhlÑ[lÑQmÐBÕnð 2ð 4r3   c                 óÜ  — | j                   j                  }| j                   j                  }|€t        d«      ‚t	        |«      rGt        j                  |j                  d d dz   |«      }t        j                  ||dd d…f   gd¬«      }n>|j                  |j                  «      }|dd d…f   j                  «       |ddd …f<   ||d<   |€t        d«      ‚|j                  |d	k(  |«       |S )
Nzoself.model.config.decoder_start_token_id has to be defined. In Pop2Piano it is usually set to the pad_token_id.r6   )r   .rŒ   r   ).r   z1self.model.config.pad_token_id has to be defined.éœÿÿÿ)rJ   Údecoder_start_token_idÚpad_token_idÚ
ValueErrorr   r*   Úfullr·   ÚcatÚ	new_zerosÚcloneÚmasked_fill_)r.   Ú	input_idsr$  r%  Úshifted_input_idss        r2   Ú_shift_rightz%Pop2PianoPreTrainedModel._shift_rightØ  s   € Ø!%§¡×!CÑ!CÐØ—{‘{×/Ñ/ˆà!Ð)Üð Bóð ô
 ˜YÔ'ä %§
¡
¨9¯?©?¸3¸BÐ+?À$Ñ+FÐH^Ó _ÐÜ %§	¡	Ð+<¸iÈÈSÈbÈSÈÑ>QÐ*RÐXZÔ [Ñà )× 3Ñ 3°I·O±OÓ DÐØ)2°3¸¸¸°8Ñ)<×)BÑ)BÓ)DÐ˜c 1¡2˜gÑ&Ø(>Ð˜fÑ%àÐÜÐPÓQÐQà×&Ñ&Ð'8¸DÑ'@À,ÔOà Ð r3   N)rC   rD   rE   Ú__doc__r    Úconfig_classÚbase_model_prefixÚis_parallelizableÚsupports_gradient_checkpointingÚ_supports_cache_classÚ_supports_static_cacheÚ_no_split_modulesÚ_keep_in_fp32_modulesr!  r.  © r3   r2   r  r  ›  sI   „ ñð
 #€LØ%ÐØÐØ&*Ð#Ø ÐØ"ÐØ)Ð*ÐØ!˜FÐò,oó\!r3   r  c                   ó(  ‡ — e Zd Zdˆ fd„	Zd„ Zd„ Z	 	 	 	 	 	 	 	 	 	 	 	 	 dd„Z	 ddej                  dej                  dej                  de	d	e
f
d
„Zedej                  dededej                  dej                  dej                  defd„«       Zˆ xZS )ÚPop2PianoStackc                 óâ  •— t         ‰| �  |«       || _        |j                  | _        t	        j
                  t        |j                  «      D �cg c]  }t        |t        |dk(  «      |¬«      ‘Œ c}«      | _
        t        |j                  |j                  ¬«      | _        t	        j                  |j                   «      | _        | j%                  «        d| _        d | _        d| _        y c c}w )Nr   rè   rk   F)r'   r(   Úembed_tokensrv   r   rø   ÚrangeÚ
num_layersrö   rÅ   Úblockr%   rP   rn   Úfinal_layer_normrT   rU   rV   Ú	post_initÚmodel_parallelÚ
device_mapr‰   )r.   rJ   r<  Úir1   s       €r2   r(   zPop2PianoStack.__init__õ  sÃ   ø€ Ü‰Ñ˜Ô à(ˆÔØ ×+Ñ+ˆŒä—]‘]ô ˜v×0Ñ0Ó1öàô ˜vÄ4ÈÈQÉÃ<Ð[\Ö]òó
ˆŒ
ô !3°6·>±>Àv×G`ÑG`Ô aˆÔÜ—z‘z &×"5Ñ"5Ó6ˆŒð 	�‰Ôà#ˆÔØˆŒØ&+ˆÕ#ùòs   Á!C,c                 ó   — | j                   S r[   ©r<  ©r.   s    r2   Úget_input_embeddingsz#Pop2PianoStack.get_input_embeddings  s   € Ø× Ñ Ð r3   c                 ó   — || _         y r[   rF  ©r.   Únew_embeddingss     r2   Úset_input_embeddingsz#Pop2PianoStack.set_input_embeddings  s
   € Ø*ˆÕr3   c                 ó¦
  — |	�|	n| j                   j                  }	|
�|
n| j                   j                  }
|�|n| j                   j                  }|�|n| j                   j                  }|�$|�"| j
                  rdnd}t        d|› d|› d�«      ‚|�&|j                  «       }|j                  d|d   «      }n8|�|j                  «       d d }n"| j
                  rdnd}t        d|› d|› d	�«      ‚| j                  r%| j                  r|	rt        j                  d
«       d}	|€(| j                  €t        d«      ‚| j                  |«      }|\  }}|	du r| j
                  st        d| › d�«      ‚d}d}| j
                  r—|	s|�“t        |t        «      r't        |t         «      sd}t!        |t#        «       «      }njt        |t         «      s-d}t        j                  d«       t!        j$                  |«      }n-|€+t!        t#        «       t#        «       «      }n| j
                  sd }|�|j'                  «       nd}|€%t)        j*                  |||z   |j,                  ¬«      }|€1t/        «       s'||z   }t)        j0                  |||j,                  ¬«      }| j                   j
                  r$| j3                  ||||�|j4                  nd |
«      }nX|d d …d d d d …f   }|j7                  |j8                  ¬«      }d|z
  t)        j:                  |j8                  «      j<                  z  }| j
                  rO|�M|j                  «       \  }}}||f}|€!t)        j0                  ||j,                  ¬«      }| j?                  |«      }nd }| jA                  || j                   jB                  «      }| jA                  || j                   jB                  «      }|rdnd }|
rdnd }|
r| j
                  rdnd }d }d } | jE                  |«      }!tG        | jH                  «      D ]Ã  \  }"}#||"   }$||"   }%|r||!fz   }| j                  r4| j                  r(| jK                  |#jL                  |!||||| |$|%d |	|
|«      }&n |#|!||||| |$|%||	|
|¬«      }&|	du r|&d d dz   |&dd  z   }&|&d d \  }!}'|&d   }| j
                  r|�	|&|
rdnd   } |
sŒ¥||&d   fz   }| j
                  sŒ»||&d   fz   }ŒÅ | jO                  |!«      }!| jE                  |!«      }!|r||!fz   }|	r'nd }(|r|j4                  }(|r|jQ                  «       }(|stS        d„ |!|(|||fD «       «      S tU        |!|(|||¬«      S )NÚdecoder_Ú zYou cannot specify both zinput_ids and zinputs_embeds at the same timer6   zYou have to specify either zinput_ids or Úinputs_embedszZ`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...Fz<You have to initialize the model with valid token embeddingsTz)`use_cache` can only be set to `True` if z is used as a decoderzìPassing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.48.0. You should pass an instance of `EncoderDecoderCache` instead, e.g. `past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`.r   ©r§   )r=   r  r8  )rë   rÌ   r  r  r  rÎ   r  rÍ   rÏ   rÐ   r­   r   r[   r5   é   r	   é   c              3   ó$   K  — | ]  }|�|–— Œ
 y ­wr[   r8  )Ú.0rƒ   s     r2   ú	<genexpr>z)Pop2PianoStack.forward.<locals>.<genexpr>Õ  s   è ø€ ò 
àð �=ô ñ
ùs   ‚)Úlast_hidden_stateÚpast_key_valuesr@   Ú
attentionsÚcross_attentions)+rJ   rÏ   rÐ   Úoutput_hidden_statesÚuse_return_dictrv   r&  Úsizer¸   r‰   r¶   r   r€   r<  r\   r   r   r   Úfrom_legacy_cacheÚget_seq_lengthr*   r¨   r§   r   r+   Ú_update_causal_maskr½   r8   r=   rÿ   r–   Úinvert_attention_maskÚget_head_maskr>  rV   Ú	enumerater?  Ú_gradient_checkpointing_funcrB   r@  Úto_legacy_cacheÚtupler   ))r.   r,  rë   r  r  rP  Ú	head_maskÚcross_attn_head_maskrX  rÏ   rÐ   r[  r  r­   Úerr_msg_prefixÚinput_shaperÑ   rÒ   Úreturn_legacy_cacheÚreturn_self_attention_cacheÚpast_key_values_lengthÚmask_seq_lengthrÛ   Úencoder_batch_sizeÚencoder_sequence_lengthÚ_Úencoder_hidden_shapeÚencoder_extended_attention_maskÚall_hidden_statesÚall_attentionsÚall_cross_attentionsrÌ   r  r@   rD  Úlayer_modulerÎ   r  Úlayer_outputsÚnext_decoder_cacheÚ
next_caches)                                            r2   rB   zPop2PianoStack.forward  s  € ð  "+Ð!6‘I¸D¿K¹K×<QÑ<Qˆ	Ø1BÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐ  ]Ð%>Ø+/¯?ª?™ZÀˆNÜØ*¨>Ð*:¸.ÈÐHXÐXvÐwóð ð Ð"Ø#Ÿ.™.Ó*ˆKØ!Ÿ™ r¨;°r©?Ó;‰IØÐ&Ø'×,Ñ,Ó.¨s°Ð3‰Kà+/¯?ª?™ZÀˆNÜÐ:¸>Ð:JÈ-ÐXfÐWgÐgtÐuÓvÐvà×&Ò&¨4¯=ª=ÙÜ×#Ñ#Øpôð "�	àÐ Ø× Ñ Ð(Ü Ð!_Ó`Ð`Ø ×-Ñ-¨iÓ8ˆMà!,Ñˆ
�Jà˜ÑØ—?’?Ü Ð#LÈTÈFÐRgÐ!hÓiÐið $ÐØ&+Ð#Ø�?Š?¡	¨_Ð-HÜ˜/¬5Ô1¼*À_ÔViÔ:jØ.2Ð+Ü"5°oÄ|Ã~Ó"V‘Ü Ô1DÔEØ&*Ð#Ü×#Ñ#ð`ôô
 #6×"GÑ"GÈÓ"X‘Ø Ð(Ü"5´l³nÄlÃnÓ"U‘Ø—’ð #ˆOàETÐE` ×!?Ñ!?Ô!AÐfgÐØÐ!Ü"Ÿ\™\Ø&Ð(>ÀÑ(KÐTa×ThÑThôˆNð Ð!Ô*BÔ*Dà4°zÑAˆOÜ"ŸZ™Z¨
°OÈM×L`ÑL`ÔaˆNà�;‰;×!Ò!Ø×2Ñ2ØØØØ8GÐ8S�×4Ò4ÐY]Ø!ó‰Kð )ª¨D°$ºÐ)9Ñ:ˆKØ%Ÿ.™.¨}×/BÑ/B˜.ÓCˆKØ Ñ,´·±¸M×<OÑ<OÓ0P×0TÑ0TÑTˆKð �?Š?Ð4Ð@Ø=R×=WÑ=WÓ=YÑ:ÐÐ 7¸Ø$6Ð8OÐ#PÐ Ø%Ð-Ü).¯©Ð4HÐQ^×QeÑQeÔ)fÐ&Ø.2×.HÑ.HÐI_Ó.`Ñ+à.2Ð+ð ×&Ñ& y°$·+±+×2HÑ2HÓIˆ	Ø#×1Ñ1Ð2FÈÏÉ×H^ÑH^Ó_ÐÙ"6™B¸DÐÙ0™°dˆÙ&7¸D¿OºO™rÐRVÐØˆØ(,Ð%àŸ™ ]Ó3ˆä(¨¯©Ó4ò 7	V‰OˆAˆ|Ø'¨™lˆOØ)=¸aÑ)@Ð&Ù#Ø$5¸Ð8HÑ$HÐ!à×*Ò*¨t¯}ª}Ø $× AÑ AØ ×(Ñ(Ø!ØØ!Ø)Ø3Ø1Ø#Ø.ØØØ%Ø"ó!‘ñ  !-Ø!Ø#.Ø"/Ø*?Ø+JØ2OØ$3Ø/IØ#2Ø'Ø&7Ø#1ô!�ð" ˜EÑ!Ø -¨b¨qÐ 1°GÑ ;¸mÈAÈBÐ>OÑ O�à0=¸b¸qÐ0AÑ-ˆMÐ-ð
 *¨!Ñ,ˆMØ�ŠÐ#8Ð#DØ0=ÑCT¹aÐZ[Ñ0\Ð-â Ø!/°=ÀÑ3CÐ2EÑ!E�Ø—?“?Ø+?À=ÐQRÑCSÐBUÑ+UÑ(ðo7	Vðr ×-Ñ-¨mÓ<ˆØŸ™ ]Ó3ˆñ  Ø 1°]Ð4DÑ DÐá+4Ñ'¸$ˆ
Ù&Ø(×=Ñ=ˆJÙØ(×8Ñ8Ó:ˆJáÜñ 
ð "ØØ%Ø"Ø(ðô
ó 
ð 
ô 9Ø+Ø&Ø+Ø%Ø1ô
ð 	
r3   rë   Úinput_tensorr­   rX  rÐ   c           
      óÆ  — | j                   j                  dk(  r|�|dk(  j                  «       r|S y | j                   j                  dk(  r7t        |t        j
                  «      rt        |«      }t        |t        «      r|S |�|j                  «       nd}t        |t        «      }| j                   j                  dk(  r(|s&|s$t        j                  |||| j                  ¬«      ry |j                  |j                  }	}|j                  d   }
|r|j!                  «       }n1t        |t        j
                  «      r|j                  d   n||
z   dz   }| j#                  ||
|||	||j                  d   ¬	«      }| j                   j                  dk(  rQ|�O|j                  j$                  d
v r7|s5t	        j&                  |«      j(                  }t        j*                  ||«      }|S )NÚflash_attention_2r  Úflex_attentionr   Úsdpa)rP  rm  Úis_trainingr   r6   )Úsequence_lengthÚtarget_lengthr=   r§   r­   rÑ   )ÚcudaÚxpu)rJ   Ú_attn_implementationrþ   r\   r*   r]   r"   r!   r_  r   r   Ú_ignore_causal_mask_sdpar¶   r=   r§   r·   Úget_max_cache_shapeÚ5_prepare_4d_causal_attention_mask_with_cache_positionÚtyperÿ   r–   Ú_unmask_unattended)r.   rë   r{  r­   rX  rÐ   Úpast_seen_tokensÚusing_static_cacher=   r§   r�  r‚  rÛ   Ú	min_dtypes                 r2   r`  z"Pop2PianoStack._update_causal_maské  sÖ  € ð �;‰;×+Ñ+Ð/BÒBØÐ)¨~ÀÑ/D×.IÑ.IÔ.KØ%Ð%ØØ�;‰;×+Ñ+Ð/?Ò?Ü˜.¬%¯,©,Ô7Ü!<¸^Ó!L�Ü˜.¬)Ô4Ø%Ð%ð
 @OÐ?Z˜?×9Ñ9Ô;Ð`aÐÜ'¨¼ÓEÐð �;‰;×+Ñ+¨vÒ5Ñ>PÑYjÜ%×>Ñ>ØØ*Ø'7Ø ŸM™Mõ	ð à$×*Ñ*¨L×,?Ñ,?ˆvˆØ&×,Ñ,¨QÑ/ˆÙØ+×?Ñ?ÓA‰Mô ˜n¬e¯l©lÔ;ð ×$Ñ$ RÒ(à%¨Ñ7¸!Ñ;ð ð ×PÑPØØ+Ø'ØØØ)Ø#×)Ñ)¨!Ñ,ð Qó 
ˆð �K‰K×,Ñ,°Ò6ØÐ*Ø×%Ñ%×*Ñ*¨oÑ=Ù%ô
 Ÿ™ EÓ*×.Ñ.ˆIÜ0×CÑCÀKÐQZÓ[ˆKàÐr3   r�  r‚  r=   r§   rÑ   c                 ó˜  — | �| j                  «       dk(  r| }|S t        j                  |«      j                  }	t        j                  ||f|	||¬«      }|dk7  rt        j
                  |d¬«      }|t        j                  ||¬«      |j                  dd«      kD  z  }|dddd…dd…f   j                  |ddd«      }| �Œ|j                  «       }| j                  d   }
|dd…dd…dd…d|
…f   | dd…dddd…f   j                  |j                  «      z   }|dk(  }|dd…dd…dd…d|
…f   j                  ||	«      |dd…dd…dd…d|
…f<   |S )	a°  
        Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape
        `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.

        Args:
            attention_mask (`torch.Tensor`):
                A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape
                `(batch_size, 1, query_length, key_value_length)`.
            sequence_length (`int`):
                The sequence length being processed.
            target_length (`int`):
                The target length: when generating with static cache, the mask should be as long as the static cache,
                to account for the 0 padding, the part of the cache that is not filled yet.
            dtype (`torch.dtype`):
                The dtype to use for the 4D attention mask.
            device (`torch.device`):
                The device to place the 4D attention mask on.
            cache_position (`torch.Tensor`):
                Indices depicting the position of the input sequence tokens in the sequence.
            batch_size (`torch.Tensor`):
                Batch size.
        NrR  )Ú
fill_valuer=   r§   r   )ÚdiagonalrQ  r6   r   )r�   r*   rÿ   r–   r'  Útriur¨   ÚreshapeÚexpandr*  r·   r8   r§   Úmasked_fill)rë   r�  r‚  r=   r§   r­   rÑ   ÚkwargsrÛ   r�  Úmask_lengthÚpadding_masks               r2   rˆ  zDPop2PianoStack._prepare_4d_causal_attention_mask_with_cache_position/  sy  € ðD Ð%¨.×*<Ñ*<Ó*>À!Ò*Cà(ˆKð* Ðô' Ÿ™ EÓ*×.Ñ.ˆIÜŸ*™*Ø  -Ð0¸YÈeÐ\bôˆKð  !Ò#Ü#Ÿj™j¨¸qÔA�Øœ5Ÿ<™<¨¸fÔEÈ×H^ÑH^Ð_aÐcdÓHeÑeÑeˆKØ% d¨D²!²QÐ&6Ñ7×>Ñ>¸zÈ1ÈbÐRTÓUˆKØÐ)Ø)×/Ñ/Ó1�Ø,×2Ñ2°2Ñ6�Ø*ª1ªa²°L°[°LÐ+@ÑAÀNÒSTÐVZÐ\`ÒbcÐScÑDd×DgÑDgØ×&Ñ&óEñ  �ð  ,¨qÑ0�Ø5@ÂÂAÂqÈ,È;È,ÐAVÑ5W×5cÑ5cØ  )ó6�šAšq¢! \ k \Ð1Ñ2ð Ðr3   r[   )NNNNNNNNNNNNN)F)rC   rD   rE   r(   rH  rL  rB   r*   r]   r   rÅ   r`  rä   rã   r=   r§   rˆ  rF   rG   s   @r2   r:  r:  ó  sõ   ø„ õ,ò.!ò+ð
 ØØ"Ø#ØØØ!ØØØØ!ØØóS
ðx #(ñDàŸ™ðDð —l‘lðDð Ÿ™ð	Dð
 ðDð  óDðL ð7ØŸ™ð7àð7ð ð7ð �{‰{ð	7ð
 —‘ð7ð Ÿ™ð7ð ò7ó ô7r3   r:  c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )r  z'Embedding Matrix for `composer` tokens.c                 ó‚   •— t         ‰| �  «        t        j                  |j                  |j
                  ¬«      | _        y )N)Únum_embeddingsÚembedding_dim)r'   r(   r   r…   Úcomposer_vocab_sizerP   r  rY   s     €r2   r(   z&Pop2PianoConcatEmbeddingToMel.__init__n  s-   ø€ Ü‰ÑÔÜŸ™°V×5OÑ5OÐ_e×_mÑ_mÔnˆ�r3   c                 ó‚   — ||z
  }| j                  |«      j                  d«      }t        j                  ||gd¬«      }|S )Nr   rŒ   )r  rª   r*   r(  )r.   ÚfeatureÚindex_valueÚembedding_offsetÚindex_shiftedÚcomposer_embeddingrP  s          r2   rB   z%Pop2PianoConcatEmbeddingToMel.forwardr  sC   € Ø#Ð&6Ñ6ˆØ!Ÿ^™^¨MÓ:×DÑDÀQÓGÐÜŸ	™	Ð#5°wÐ"?ÀQÔGˆØÐr3   )rC   rD   rE   r/  r(   rB   rF   rG   s   @r2   r  r  k  s   ø„ Ù1ôoör3   r  aB  
    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

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

    Parameters:
        config ([`Pop2PianoConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
z7Pop2Piano Model with a `language modeling` head on top.c            +       ód  ‡ — e Zd Zg d¢Zdefˆ fd„Zd„ Zd„ Zd„ Zd„ Z	d„ Z
d	„ Z	 d%d
ej                  dededeej                     fd„Z ee«       eee¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d&deej.                     deej                     deej.                     deej0                     deej                     deej                     deej2                     deeeej2                           deeeej2                           deej                     d
eej                     deej                     deej.                     dee   dee   dee   dee   deej.                     d eeej                     ef   f&d!„«       «       Z ej<                  «       	 	 	 d'ˆ fd"„	«       Zdej2                  fd#„Z d$„ Z!ˆ xZ"S )(r  )zencoder.embed_tokens.weightzdecoder.embed_tokens.weightzlm_head.weightrJ   c                 ó|  •— t         ‰| �  |«       || _        |j                  | _        t        j                  |j                  |j                  «      | _        t        |«      | _
        t        j                  |«      }d|_        d|_        d|_        t!        || j                  «      | _        t        j                  |«      }d|_        d|_        |j$                  |_        t!        || j                  «      | _        t        j*                  |j                  |j                  d¬«      | _        | j/                  «        y )NFTrM   )r'   r(   rJ   rP   Ú	model_dimr   r…   Ú
vocab_sizer  r  Úmel_conditionerÚcopyÚdeepcopyrv   rÏ   Úis_encoder_decoderr:  ÚencoderÚnum_decoder_layersr>  ÚdecoderrO   r  rA  )r.   rJ   Úencoder_configÚdecoder_configr1   s       €r2   r(   z*Pop2PianoForConditionalGeneration.__init__�  së   ø€ Ü‰Ñ˜Ô ØˆŒØŸ™ˆŒä—l‘l 6×#4Ñ#4°f·n±nÓEˆŒä<¸VÓDˆÔäŸ™ vÓ.ˆØ$)ˆÔ!Ø#(ˆÔ Ø,1ˆÔ)ä% n°d·k±kÓBˆŒäŸ™ vÓ.ˆØ$(ˆÔ!Ø,1ˆÔ)Ø$*×$=Ñ$=ˆÔ!Ü% n°d·k±kÓBˆŒä—y‘y §¡°×1BÑ1BÈÔOˆŒð 	�‰Õr3   c                 ó   — | j                   S r[   )r  rG  s    r2   rH  z6Pop2PianoForConditionalGeneration.get_input_embeddings¨  s   € Ø�{‰{Ðr3   c                 ó~   — || _         | j                  j                  |«       | j                  j                  |«       y r[   )r  r«  rL  r­  rJ  s     r2   rL  z6Pop2PianoForConditionalGeneration.set_input_embeddings«  s-   € Ø$ˆŒØ�‰×)Ñ)¨.Ô9Ø�‰×)Ñ)¨.Õ9r3   c                 ó   — || _         y r[   ©r  rJ  s     r2   Úset_output_embeddingsz7Pop2PianoForConditionalGeneration.set_output_embeddings°  s	   € Ø%ˆ�r3   c                 ó   — | j                   S r[   r³  rG  s    r2   Úget_output_embeddingsz7Pop2PianoForConditionalGeneration.get_output_embeddings³  ó   € Ø�|‰|Ðr3   c                 ó   — | j                   S r[   )r«  rG  s    r2   Úget_encoderz-Pop2PianoForConditionalGeneration.get_encoder¶  r·  r3   c                 ó   — | j                   S r[   )r­  rG  s    r2   Úget_decoderz-Pop2PianoForConditionalGeneration.get_decoder¹  r·  r3   Úinput_featuresÚcomposerÚgeneration_configrë   c                 ó  — |j                   }||j                  «       vr(t        dt        |j                  «       «      › d|› �«      ‚||   }t	        j
                  || j                  ¬«      }|j                  |j                  d   «      }t        |j                  «       «      }| j                  |||¬«      }|�Od||dd…df   j                  «        <   t	        j                  |dd…df   j                  dd	«      |gd	¬
«      }||fS |dfS )aã  
        This method is used to concatenate mel conditioner tokens at the front of the input_features in order to
        control the type of MIDI token generated by the model.

        Args:
            input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
                input features extracted from the feature extractor.
            composer (`str`):
                composer token which determines the type of MIDI tokens to be generated.
            generation_config (`~generation.GenerationConfig`):
                The generation is used to get the composer-feature_token pair.
            attention_mask (``, *optional*):
                For batched generation `input_features` are padded to have the same shape across all examples.
                `attention_mask` helps to determine which areas were padded and which were not.
                - 1 for tokens that are **not padded**,
                - 0 for tokens that are **padded**.
        zPlease choose a composer from z. Composer received - rQ  r   )rž  rŸ  r   Nr  r6   r   )Úaxis)Úcomposer_to_feature_tokenÚkeysr&  rÄ   r*   Útensorr§   Úrepeatr·   r–   r±   r§  rÅ   Úconcatenater¸   )r.   r¼  r½  r¾  rë   rÁ  Úcomposer_valuer   s           r2   Úget_mel_conditioner_outputsz=Pop2PianoForConditionalGeneration.get_mel_conditioner_outputs¼  s.  € ð0 %6×$OÑ$OÐ!ØÐ4×9Ñ9Ó;Ñ;ÜØ0´Ð6O×6TÑ6TÓ6VÓ1WÐ0XÐXnÐowÐnxÐyóð ð 3°8Ñ<ˆÜŸ™ n¸T¿[¹[ÔIˆØ'×.Ñ.¨~×/CÑ/CÀAÑ/FÓGˆäÐ8×?Ñ?ÓAÓBÐà×-Ñ-Ø"Ø&Ø-ð .ó 
ˆð
 Ð%Ø;>ˆN˜Nª1¨a¨4Ñ0×5Ñ5Ó7Ð7Ñ8ô #×.Ñ.°ºqÀ!¸tÑ0D×0IÑ0IÈ"ÈaÓ0PÐR`Ð/aÐhiÔjˆNØ! >Ð1Ð1à˜tÐ#Ð#r3   )Úoutput_typer0  r,  Údecoder_input_idsÚdecoder_attention_maskrg  Údecoder_head_maskrh  Úencoder_outputsrX  rP  Údecoder_inputs_embedsÚlabelsrÏ   rÐ   r[  r  r­   Úreturnc                 ó¾  — |�|n| j                   j                  }|�|n| j                   j                  }|
�|�t        d«      ‚|�|
€|}
|€| j	                  |||
||||¬«      }nI|rGt        |t        «      s7t        |d   t        |«      dkD  r|d   ndt        |«      dkD  r|d   nd¬«      }|d   }|�|€|€| j                  |«      }| j                  ||||	|||||||||¬«      }|d   }| j                   j                  r|| j                  d	z  z  }| j                  |«      }d}|�Ct        d
¬«      } ||j                  d|j                  d«      «      |j                  d«      «      }|s|f|dd z   |z   }|�|f|z   S |S t!        |||j"                  |j$                  |j&                  |j(                  |j*                  |j$                  |j&                  ¬«	      S )a}  
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the sequence classification/regression loss. Indices should be in `[-100, 0, ...,
            config.vocab_size - 1]`. All labels set to `-100` are ignored (masked), the loss is only computed for
            labels in `[0, ..., config.vocab_size]`
        Returns:
        NzSBoth `inputs_embeds` and `input_features` received! Please provide only one of them)r,  rë   rP  rg  rÐ   r[  r  r   r   r5   )rW  r@   rY  )r,  rë   rP  rX  r  r  rg  rh  rÏ   rÐ   r[  r  r­   r  r#  )Úignore_indexr6   )	ÚlossÚlogitsrX  Údecoder_hidden_statesÚdecoder_attentionsrZ  Úencoder_last_hidden_stater  Úencoder_attentions)rJ   rÏ   r\  r&  r«  r\   r   rŽ   r.  r­  r  r¥  r  r   r¸   r]  r   rX  r@   rY  rZ  rW  )r.   r,  rë   rÉ  rÊ  rg  rË  rh  rÌ  rX  rP  r¼  rÍ  rÎ  rÏ   rÐ   r[  r  r­   r@   Údecoder_outputsÚsequence_outputÚ	lm_logitsrÒ  Úloss_fctÚoutputs                             r2   rB   z)Pop2PianoForConditionalGeneration.forwardí  s=  € ð< "+Ð!6‘I¸D¿K¹K×<QÑ<Qˆ	Ø%0Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐ$¨Ð)CÜÐrÓsÐsØÐ'¨MÐ,AØ*ˆMð Ð"à"Ÿl™lØ#Ø-Ø+Ø#Ø"3Ø%9Ø'ð +ó ‰Oñ ¤¨O¼_Ô!MÜ-Ø"1°!Ñ"4Ü47¸Ó4HÈ1Ò4L˜o¨aÒ0ÐRVÜ14°_Ó1EÈÒ1I˜?¨1Ò-ÈtôˆOð (¨Ñ*ˆàÐÐ"3Ð";Ð@UÐ@]à $× 1Ñ 1°&Ó 9Ðð Ÿ,™,Ø'Ø1Ø/Ø+Ø"/Ø#1Ø'Ø!5ØØ/Ø!5Ø#Ø)ð 'ó 
ˆð  *¨!Ñ,ˆà�;‰;×*Ò*ð .°·±ÀÑ1EÑFˆOà—L‘L Ó1ˆ	àˆØÐÜ'°TÔ:ˆHÙ˜IŸN™N¨2¨y¯~©~¸bÓ/AÓBÀFÇKÁKÐPRÃOÓTˆDáØ�\ O°A°BÐ$7Ñ7¸/ÑIˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEäØØØ+×;Ñ;Ø"1×"?Ñ"?Ø.×9Ñ9Ø,×=Ñ=Ø&5×&GÑ&GØ"1×"?Ñ"?Ø.×9Ñ9ô

ð 
	
r3   c                 ó”  •— |€| j                   } |j                  d	i |¤Ž t        |d«      st        d«      ‚t	        |j
                  «      | j                  j                  k7  r9t        d| j                  j                  › dt	        |j
                  «      › d�«      ‚| j                  ||||¬«      \  }}t        ‰| �(  d	d|||dœ|¤ŽS )
aŽ  
        Generates token ids for midi outputs.

        <Tip warning={true}>

        Most generation-controlling parameters are set in `generation_config` which, if not passed, will be set to the
        model's default generation configuration. You can override any `generation_config` by passing the corresponding
        parameters to generate(), e.g. `.generate(inputs, num_beams=4, do_sample=True)`. For an overview of generation
        strategies and code examples, check out the [following guide](./generation_strategies).

        </Tip>

        Parameters:
            input_features (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
                This is the featurized version of audio generated by `Pop2PianoFeatureExtractor`.
            attention_mask:
                For batched generation `input_features` are padded to have the same shape across all examples.
                `attention_mask` helps to determine which areas were padded and which were not.
                - 1 for tokens that are **not padded**,
                - 0 for tokens that are **padded**.
            composer (`str`, *optional*, defaults to `"composer1"`):
                This value is passed to `Pop2PianoConcatEmbeddingToMel` to generate different embeddings for each
                `"composer"`. Please make sure that the composet value is present in `composer_to_feature_token` in
                `generation_config`. For an example please see
                https://huggingface.co/sweetcocoa/pop2piano/blob/main/generation_config.json .
            generation_config (`~generation.GenerationConfig`, *optional*):
                The generation configuration to be used as base parametrization for the generation call. `**kwargs`
                passed to generate matching the attributes of `generation_config` will override them. If
                `generation_config` is not provided, the default will be used, which had the following loading
                priority: 1) from the `generation_config.json` model file, if it exists; 2) from the model
                configuration. Please note that unspecified parameters will inherit [`~generation.GenerationConfig`]'s
                default values, whose documentation should be checked to parameterize generation.
            kwargs:
                Ad hoc parametrization of `generate_config` and/or additional model-specific kwargs that will be
                forwarded to the `forward` function of the model. If the model is an encoder-decoder model, encoder
                specific kwargs should not be prefixed and decoder specific kwargs should be prefixed with *decoder_*.
        Return:
            [`~utils.ModelOutput`] or `torch.LongTensor`: A [`~utils.ModelOutput`] (if `return_dict_in_generate=True`
            or when `config.return_dict_in_generate=True`) or a `torch.FloatTensor`.
                Since Pop2Piano is an encoder-decoder model (`model.config.is_encoder_decoder=True`), the possible
                [`~utils.ModelOutput`] types are:
                    - [`~generation.GenerateEncoderDecoderOutput`],
                    - [`~generation.GenerateBeamEncoderDecoderOutput`]
        NrÁ  z¢`composer_to_feature_token` was not found! Please refer to https://huggingface.co/sweetcocoa/pop2piano/blob/main/generation_config.jsonand parse a dict like that.ztconfig.composer_vocab_size must be same as the number of keys in generation_config.composer_to_feature_token! Found z vs ú.)r¼  rë   r½  r¾  )ÚinputsrP  rë   r¾  r8  )r¾  rÀ   r  r&  rŽ   rÁ  rJ   rœ  rÇ  r'   Úgenerate)r.   r¼  rë   r½  r¾  r•  r1   s         €r2   rà  z*Pop2PianoForConditionalGeneration.generate[  s  ø€ ðl Ð$Ø $× 6Ñ 6ÐØ Ð× Ñ Ñ* 6Ò*ô Ð(Ð*EÔFÜð.óð ô Ð ×:Ñ:Ó;¸t¿{¹{×?^Ñ?^Ò^ÜðàŸ™×8Ñ8Ð9¸¼cÐBS×BmÑBmÓ>nÐ=oÐopðróð ð *.×)IÑ)IØ)Ø)ØØ/ð	 *Jó *
Ñ&ˆ˜ô ‰wÑð 
ØØ(Ø)Ø/ñ	
ð
 ñ
ð 	
r3   c                 ó$   — | j                  |«      S r[   )r.  )r.   rÎ  s     r2   Ú%prepare_decoder_input_ids_from_labelszGPop2PianoForConditionalGeneration.prepare_decoder_input_ids_from_labelsµ  s   € Ø× Ñ  Ó(Ð(r3   c           	      óÒ  — |€t         j                  d«       |S d}|D ]Æ  }d}|D ]1  }||j                  d|j                  |j                  «      «      fz   }Œ3 |d   j
                  |d   j
                  k7  r,t        d|d   j
                  › d|d   j
                  › d�«      ‚t        |«      t        |«      k7  r$t        dt        |«      › dt        |«      › d�«      ‚||fz   }ŒÈ |S )	NzHYou might want to consider setting `use_cache=True` to speed up decodingr8  r   z%reordered_layer_past_states[0] shape z  and layer_past_states[0] shape z mismatchedz&length of reordered_layer_past_states z! and length of layer_past_states )r   ÚwarningÚindex_selectr8   r§   r·   r&  rŽ   )r.   rX  Úbeam_idxÚreordered_decoder_pastÚlayer_past_statesÚreordered_layer_past_statesÚlayer_past_states          r2   Ú_reorder_cachez0Pop2PianoForConditionalGeneration._reorder_cache¸  su  € ð Ð"Ü�N‰NÐeÔfØ"Ð"à!#ÐØ!0ò 	]Ðð +-Ð'Ø$5ò Ð à.IØ$×1Ñ1°!°X·[±[ÐAQ×AXÑAXÓ5YÓZðMñ /Ñ+ðð +¨1Ñ-×3Ñ3Ð7HÈÑ7K×7QÑ7QÒQÜ Ø;Ð<WÐXYÑ<Z×<`Ñ<`Ð;að  bBð  CTð  UVñ  CW÷  C]ñ  C]ð  B^ð  ^ið  jóð ô Ð.Ó/´3Ð7HÓ3IÒIÜ Ø<¼SÐA\Ó=]Ð<^Ð^ô  ADð  EVó  AWð  @Xð  Xcð  dóð ð &<Ð?ZÐ>\Ñ%\Ñ"ð'	]ð( &Ð%r3   r[   )NNNNNNNNNNNNNNNNNN)NÚ	composer1N)#rC   rD   rE   Ú_tied_weights_keysr    r(   rH  rL  r´  r¶  r¹  r»  r*   ÚFloatTensorÚstrr   r   rÇ  r   ÚPOP2PIANO_INPUTS_DOCSTRINGr   r   Ú_CONFIG_FOR_DOCÚ
LongTensorÚ
BoolTensorr]   r   rÅ   r   rB   Úno_gradrà  râ  rë  rF   rG   s   @r2   r  r  ‰  s¶  ø„ âiÐð˜õ ò6ò:ò
&òòòð 7;ñ/$à×)Ñ)ð/$ð ð/$ð ,ð	/$ð
 ! ×!2Ñ!2Ñ3ó/$ñb +Ð+EÓFÙ¨?ÈÔYð 15Ø6:Ø8<Ø=AØ15Ø9=Ø7;Ø@DØ@DØ59Ø6:Ø=AØ-1Ø$(Ø,0Ø/3Ø&*Ø59ñ'j
à˜E×,Ñ,Ñ-ðj
ð ! ×!2Ñ!2Ñ3ðj
ð $ E×$4Ñ$4Ñ5ð	j
ð
 !)¨×)9Ñ)9Ñ :ðj
ð ˜E×-Ñ-Ñ.ðj
ð $ E×$5Ñ$5Ñ6ðj
ð ' u§|¡|Ñ4ðj
ð " %¨¨e¯l©lÑ(;Ñ"<Ñ=ðj
ð " %¨¨e¯l©lÑ(;Ñ"<Ñ=ðj
ð   × 1Ñ 1Ñ2ðj
ð ! ×!2Ñ!2Ñ3ðj
ð  (¨×(9Ñ(9Ñ:ðj
ð ˜×)Ñ)Ñ*ðj
ð ˜D‘>ðj
ð  $ D™>ð!j
ð" ' t™nð#j
ð$ ˜d‘^ð%j
ð& ! ×!1Ñ!1Ñ2ð'j
ð( 
ˆu�U×&Ñ&Ñ'¨Ð8Ñ	9ò)j
ó Zó Gðj
ðX €U‡]�]ƒ_ð ØØôW
ó ðW
ðr)¸E¿L¹Ló )ö&r3   r  )Mr/  r¨  rš   Útypingr   r   r   r*   r   Útorch.nnr   Útransformers.generationr   Úactivationsr
   Úcache_utilsr   r   r   r   Ú
generationr   Úmodeling_attn_mask_utilsr   Úmodeling_outputsr   r   r   Úmodeling_utilsr   Úpytorch_utilsr   r   r   Úutilsr   r   r   r   r   r   r   Úconfiguration_pop2pianor    Ú!torch.nn.attention.flex_attentionr!   Úintegrations.flex_attentionr"   Ú
get_loggerrC   r   Ú_load_pop2piano_layer_normÚapex.normalizationr#   ÚinfoÚImportErrorÚ	Exceptionrä  rñ  Ú_CHECKPOINT_FOR_DOCrð  ÚModuler%   rú   rI   ra   ri   rs   ræ   rð   rö   r  r:  r  ÚPop2Piano_START_DOCSTRINGr  Ú__all__r8  r3   r2   ú<module>r     sö  ðñ ã Û ß )Ñ )ã Ý Ý %å 4å !ß PÓ PÝ )Ý >÷ñ õ
 .ß gÑ g÷÷ ñ õ 5ñ  Ô!Ý;åJð 
ˆ×	Ñ	˜HÓ	%€à!Ð ð	Ý/à!&Ðà
‡K�KÐhÔið $€Ø,Ð ðJÐ ô\+˜Ÿ™ô +ñ2 "Ø%Ðà Ð × Ñ Ð.Ô /ô˜RŸY™Yô ô. "§)¡)ô ô<�r—y‘yô ô&a˜Ÿ™ô aôJ! "§)¡)ô !ôJ# 2§9¡9ô #ôNc�R—Y‘Yô côLU!˜ô U!ôpuÐ-ô uôp B§I¡Iô ðÐ ñ  ÐSÐUnÓoôJ&Ð(@À/ó J&ó pðJ&ðZ
 /Ð0JÐ
K�øðk, ò 	âØò 	Ø
‡N�NÐ^Ô_Úð	ús   Â#G
 Ç
G+ÇG+Ç*G+