Ë
    ÛÓ^jTØ  ã                   ó  — d Z ddlmZ ddlmZ ddlmZ ddlZddlm	c 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 ddl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%m&Z& ddl'm(Z( ddl)m*Z*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1m2Z2  e&jf                  e4«      Z5 e$d¬«      e G d„ de«      «       «       Z6 e$d¬«      e G d„ de«      «       «       Z7	 	 	 	 dBd„Z8dCd „Z9 G d!„ d"e	jt                  «      Z; G d#„ d$e	jx                  «      Z= G d%„ d&e	j|                  «      Z? G d'„ d(ej                  j|                  «      Z@d)„ ZAdDd*„ZB G d+„ d,e	j|                  «      ZC	 dEd-e	j|                  d.ejˆ                  d/ejˆ                  d0ejˆ                  d1ejˆ                  dz  d2eEd3eEfd4„ZF G d5„ d6e	j|                  «      ZG G d7„ d8e«      ZH G d9„ d:e«      ZIe$ G d;„ d<e«      «       ZJe$ G d=„ d>eJ«      «       ZK G d?„ d@eJe«      ZLg dA¢ZMy)FzPyTorch Idefics model.é    )ÚCallable)Ú	dataclass)ÚAnyN)Únné   )Úinitialization)ÚACT2FN)ÚCacheÚDynamicCache)ÚGenerationMixin)Úcreate_causal_mask)ÚGradientCheckpointingLayer)ÚModelOutput)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedConfigÚPreTrainedModel)ÚUnpack)ÚTransformersKwargsÚauto_docstringÚcan_return_tupleÚlogging)Úmerge_with_config_defaults)ÚOutputRecorderÚcapture_outputsé   )ÚIdeficsConfig)ÚIdeficsPerceiverResampler)ÚIdeficsVisionEmbeddingsÚIdeficsVisionTransformerz{
    Base class for Idefics model's outputs that may also contain a past key/values (to speed up sequential decoding).
    )Úcustom_introc                   óØ   — e Zd ZU dZdZej                  dz  ed<   dZe	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ej                     dz  ed<   y)ÚIdeficsBaseModelOutputWithPasta…  
    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 model.

        If `past_key_values` is used only the last hidden-state of the sequences of shape `(batch_size, 1,
        hidden_size)` is output.
    image_hidden_states (`tuple(torch.FloatTensor)`, *optional*):
        Tuple of `torch.FloatTensor` (one for the output of the image embeddings, `(batch_size, num_images,
        sequence_length, hidden_size)`.

        image_hidden_states of the model produced by the vision encoder, and optionally by the perceiver
    NÚlast_hidden_stateÚpast_key_valuesÚhidden_statesÚ
attentionsÚimage_hidden_states)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   ÚtorchÚFloatTensorÚ__annotations__r$   r
   r%   Útupler&   r'   © ó    úw/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/idefics/modeling_idefics.pyr"   r"   1   s|   … ñð 37Ð�u×(Ñ(¨4Ñ/Ó6Ø$(€O�U˜T‘\Ó(Ø59€M�5˜×*Ñ*Ñ+¨dÑ2Ó9Ø26€J��e×'Ñ'Ñ(¨4Ñ/Ó6Ø;?Ð˜˜u×0Ñ0Ñ1°DÑ8Ô?r1   r"   zS
    Base class for Idefics causal language model (or autoregressive) 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
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ej                     dz  ed<   y)	ÚIdeficsCausalLMOutputWithPastae  
    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.
    image_hidden_states (`tuple(torch.FloatTensor)`, *optional*):
        Tuple of `torch.FloatTensor` (one for the output of the image embeddings, `(batch_size, num_images,
        sequence_length, hidden_size)`.

        image_hidden_states of the model produced by the vision encoder, and optionally by the perceiver
    NÚlossÚlogitsr$   r%   r&   r'   )r(   r)   r*   r+   r5   r,   r-   r.   r6   r$   r
   r%   r/   r&   r'   r0   r1   r2   r4   r4   L   s�   … ñð" &*€Dˆ%×
Ñ
˜dÑ
"Ó)Ø'+€FˆE×Ñ Ñ$Ó+Ø$(€O�U˜T‘\Ó(Ø59€M�5˜×*Ñ*Ñ+¨dÑ2Ó9Ø26€J��e×'Ñ'Ñ(¨4Ñ/Ó6Ø;?Ð˜˜u×0Ñ0Ñ1°DÑ8Ô?r1   r4   c                 ó  — t        j                  | j                  d   «      j                  dd«      j	                  d|«      j                  d«      j                  | j                  «      }| j                  d|«      } |j                  d«      |d<   |j                  d«      |d<   |j                  d«      |d<   |j                  d«      |d<   d|v r|d   }|j                  d|«      |d<   |�|j                  d|«      |d	<   |d   �|d   j                  d|«      |d<   |d   �|d   j                  d|«      |d<   | |fS |d   �|d   j                  d|«      |d<   | |fS |d   �|d   j                  d|«      |d<   | |fS )
Nr   éÿÿÿÿr   Úpixel_valuesÚimage_encoder_embeddingsÚperceiver_embeddingsÚimage_attention_maskÚtoken_type_idsÚattention_mask)	r,   ÚarangeÚshapeÚviewÚrepeatÚtoÚdeviceÚindex_selectÚget)Ú	input_idsÚexpand_sizeÚis_encoder_decoderr>   Úencoder_outputsÚmodel_kwargsÚexpanded_return_idxr=   s           r2   Úexpand_inputs_for_generationrM   l   sð  € ô 	�‰�Y—_‘_ QÑ'Ó(×-Ñ-¨b°!Ó4×;Ñ;¸A¸{ÓK×PÑPÐQSÓT×WÑWÐXa×XhÑXhÓið ð ×&Ñ& qÐ*=Ó>€IØ#/×#3Ñ#3°NÓ#C€L�Ñ Ø/;×/?Ñ/?Ð@ZÓ/[€LÐ+Ñ,Ø+7×+;Ñ+;Ð<RÓ+S€LÐ'Ñ(Ø+7×+;Ñ+;Ð<RÓ+S€LÐ'Ñ(à˜<Ñ'Ø%Ð&6Ñ7ˆØ)7×)DÑ)DÀQÐH[Ó)\ˆÐ%Ñ&àÐ!Ø)7×)DÑ)DÀQÐH[Ó)\ˆÐ%Ñ&àÐ*Ñ+Ð7Ø/;Ð<RÑ/S×/`Ñ/`ØÐ"ó0
ˆÐ+Ñ,ð �NÑ#Ð/Ø'3°NÑ'C×'PÑ'PÐQRÐTgÓ'hˆ�^Ñ$ð �lÐ"Ð"ð 
Ð0Ñ	1Ð	=Ø3?Ð@ZÑ3[×3hÑ3hØÐ"ó4
ˆÐ/Ñ0ð �lÐ"Ð"ð 
Ð,Ñ	-Ð	9Ø/;Ð<RÑ/S×/`Ñ/`ØÐ"ó0
ˆÐ+Ñ,ð �lÐ"Ð"r1   c                 ó2  ‡— t         j                  t         j                  t         j                  dœ}|D �cg c]  }||   ‘Œ	 }}| j	                  «       D ];  Š|r&t        ˆfd„|D «       «      r‰j                  d«       Œ+‰j                  d«       Œ= | S c c}w )N)Ú	LayerNormÚLinearÚ	Embeddingc              3   ó6   •K  — | ]  }t        ‰|«      –— Œ y ­w©N)Ú
isinstance)Ú.0ÚtÚmodules     €r2   ú	<genexpr>zfreeze_model.<locals>.<genexpr>¡   s   øè ø€ Ò$]¸q¤Z°¸×%:Ñ$]ùs   ƒTF)r   rO   rP   rQ   ÚmodulesÚanyÚrequires_grad_)ÚmodelÚmodule_exceptionsÚmappingÚmÚmodule_exceptions_mappedrW   s        @r2   Úfreeze_modelra   ™   s‰   ø€ ä—\‘\Ü—)‘)Ü—\‘\ñ€Gð
 5FÖF¨q ¨£
ÐFÐÐFØ—-‘-“/ò )ˆÙ¤Ó$]ÐD\Ô$]Ô!]Ø×!Ñ! $Õ'à×!Ñ! %Õ(ð	)ð
 €Lùò  Gs   ¶Bc                   óN   ‡ — e Zd ZdZ	 	 	 	 ddedz  ddfˆ fd„Zd„ Zdefd„Zˆ xZ	S )	ÚIdeficsDecoupledEmbeddinga¿  
    Implements a decoupling of parameters to allow freezing (or not) a subset of the embeddings. In practise, the
    regular `weight` can be trained or frozen (i.e. `partially_freeze=True`), and if `num_additional_embeddings` > 0,
    then it will create `num_additional_embeddings` additional parameters that are always trained. If
    `num_additional_embeddings=0`, then the module defaults back to the regular behavior of `nn.Embedding`.
    NÚpartially_freezeÚreturnc           	      óB  •— |�||kD  rt        d|› d|› �«      ‚t        ‰	| �  d|||||dœ|¤Ž || _        || _        || _        || _        |r| j                  j                  d«       | j
                  dkD  r)t        j                  | j
                  |||¬«      | _        yy)	a)  
        Args:
            num_embeddings (`int`):
                Size of the dictionary of embeddings
            num_additional_embeddings (`int`):
                Number of additional embeddings. Only useful when you `partially_freeze=True`.
            embedding_dim (`int`):
                The size of each embedding vector
            partially_freeze: (`bool`, *optional*, defaults to `False`):
                If `True`, the regular `weight` will be frozen. `additional_weight` is never frozen.
            padding_idx (`int`, *optional*):
                The padding index (needs to be less than num_embeddings)

        Note: there are a lot of other parameters to initialize a standard `nn.Embedding` such as `padding_idx`,
        `max_norm` or `norm_type`. We are not supporting these.
        Nz/padding_idx must be within num_embeddings. Got z and )Únum_embeddingsÚembedding_dimrD   ÚdtypeÚpadding_idxFr   )rg   rh   rD   ri   r0   )Ú
ValueErrorÚsuperÚ__init__rg   rj   Únum_additional_embeddingsrd   Úweightr[   r   rQ   Úadditional_embedding)
Úselfrg   rn   rh   rd   rD   ri   rj   ÚkwargsÚ	__class__s
            €r2   rm   z"IdeficsDecoupledEmbedding.__init__±   sÆ   ø€ ð6 Ð" {°^Ò'CÜÐNÈ{ÈmÐ[`ÐaoÐ`pÐqÓrÐrÜ‰Ñð 	
Ø)Ø'ØØØ#ñ	
ð ò	
ð -ˆÔØ&ˆÔØ)BˆÔ&Ø 0ˆÔáØ�K‰K×&Ñ& uÔ-à×)Ñ)¨AÒ-Ü(*¯©Ø#×=Ñ=Ø+ØØô	)ˆDÕ%ð .r1   c                 ób  — | j                   dk(  r t        j                  || j                  «      S |j	                  «       }t        j                  || j                  k\  «      }||   }| j                  || j                  z
  «      }d||<   t        j                  || j                  «      }|||<   |S )aû  
        we have 2 embeddings, with different indices - one pretrained self.weight and another
        self.additional_embedding.weight that is being trained.

        in order to make a lookup of the input ids, we:
        1. find out the indices of the entries belonging to the 2nd embedding
        2. extract those values while subtracting the size of the first embedding (num_embeddings), since the 2nd
           embedding starts from 0 and not num_embeddings
        3. perform the 2nd embedding lookup
        4. now we handle the 1st embedding, we overwrite indices belonging to the 2nd embedding with a padding index
        5. perform the 1st embedding lookup
        6. now we overwrite the values in the 1st embedding lookup with the values of the 2nd embedding lookup

        note: for the 1st embedding lookup we could have looked up only the low indices and not do the padding, but
        then we have to create a new tensor and populate it with 2 tensors that are spread out across various indices -
        i.e. not a simple concat - I haven't benchmarked the complex case if it's any faster, given that seqlens are
        usually relatively short it's probably not faster or if faster not by much - but might be a good idea to
        measure.

        r   )	rn   ÚFÚ	embeddingro   Úcloner,   Úwhererg   rp   )rq   rG   Úadditional_vocab_indicesÚinput_ids_additional_vocabÚadditional_embeddingsÚfull_vectors         r2   Úforwardz!IdeficsDecoupledEmbedding.forwardæ   s®   € ð* ×)Ñ)¨QÒ.Ü—;‘;˜y¨$¯+©+Ó6Ð6ð —O‘OÓ%ˆ	Ü#(§;¡;¨y¸D×<OÑ<OÑ/OÓ#PÐ Ø%.Ð/GÑ%HÐ"Ø $× 9Ñ 9Ð:TÐW[×WjÑWjÑ:jÓ kÐð /0ˆ	Ð*Ñ+Ü—k‘k )¨T¯[©[Ó9ˆð 1FˆÐ,Ñ-àÐr1   c                 ón   — d| j                   › d| j                  › d| j                  › d| j                  › �S )Nznum_embeddings=z, num_additional_embeddings=z, embedding_dim=ú, partially_freeze=)rg   rn   rh   rd   ©rq   s    r2   Ú
extra_reprz$IdeficsDecoupledEmbedding.extra_repr  sq   € Ø  ×!4Ñ!4Ð 5Ð5QÐRV×RpÑRpÐQqð  rBð  CG÷  CUñ  CUð  BVð  Við  jn÷  jñ  jð  i@ð  Að  	Ar1   )FNNN)
r(   r)   r*   r+   Úboolrm   r}   Ústrr�   Ú__classcell__©rs   s   @r2   rc   rc   ¨   sH   ø„ ñð ).ØØØñ3ð
  ™+ð3ð 
õ3òj%ðNA˜C÷ Ar1   rc   c                   óŒ   ‡ — e Zd ZdZ	 	 	 	 	 ddedededededdfˆ fd	„Zd
ej                  dej                  fd„Z	de
fd„Zˆ xZS )ÚIdeficsDecoupledLinearaÄ  
    Implements a decoupling of parameters to allow freezing (or not) a subset of the parameters. In practise, the
    regular `weight` can be trained or frozen (i.e. `partially_freeze=True`), and if `out_additional_features` > 0,
    then it will create `out_additional_features * in_features` additional parameters that are always trained. If
    `out_additional_features=0`, then the module defaults back to the regular behavior of `nn.Linear`.
    NÚin_featuresÚout_featuresÚout_additional_featuresÚbiasrd   re   c                 ó"  •— t         ‰| �  |||||«       || _        || _        || _        || _        |r8| j                  j                  d«       |r| j                  j                  d«       |dkD  r t        j                  |||||¬«      | _        yy)aG  
        out_additional_features: int. Number of additional trainable dimensions. Only makes sense when
        `partially_freeze=True`. partially_freeze: bool. If True, the regular `weight` will be frozen and extra
        parameters (if any) will be trainable. If False, default to the regular behavior of nn.Linear.
        Fr   )rˆ   r‰   r‹   rD   ri   N)rl   rm   rŠ   rd   rˆ   r‰   ro   r[   r‹   r   rP   Úadditional_fc)	rq   rˆ   r‰   rŠ   r‹   rd   rD   ri   rs   s	           €r2   rm   zIdeficsDecoupledLinear.__init__  s‘   ø€ ô 	‰Ñ˜ l°D¸&À%ÔHØ'>ˆÔ$Ø 0ˆÔà&ˆÔØ(ˆÔáØ�K‰K×&Ñ& uÔ-ÙØ—	‘	×(Ñ(¨Ô/à" QÒ&Ü!#§¡Ø'Ø4ØØØô"ˆDÕð 'r1   Úinputc                 óÌ   — t        j                  || j                  | j                  «      }| j                  dkD  r)| j                  |«      }t        j                  ||fd«      }|S )Nr   r8   )ru   Úlinearro   r‹   rŠ   r�   r,   Úcat)rq   rŽ   ÚoutputÚadditional_featuress       r2   r}   zIdeficsDecoupledLinear.forward>  sV   € Ü—‘˜% §¡¨d¯i©iÓ8ˆà×'Ñ'¨!Ò+Ø"&×"4Ñ"4°UÓ";ÐÜ—Y‘Y Ð(;Ð<¸bÓAˆFàˆr1   c           
      óŒ   — d| j                   › d| j                  › d| j                  › d| j                  du› d| j                  › �
S )z=Overwriting `nn.Linear.extra_repr` to include new parameters.zin_features=z, out_features=z, out_additional_features=z, bias=Nr   ©rˆ   r‰   rŠ   r‹   rd   r€   s    r2   r�   z!IdeficsDecoupledLinear.extra_reprG  s˜   € à˜d×.Ñ.Ð/¨¸t×?PÑ?PÐ>QÐQkÐlp÷  mIñ  mIð  lJð  JQð  RV÷  R[ñ  R[ð  cgð  Rgð  Qhð  h{ð  |@÷  |Qñ  |Qð  {Rð  Sð  	Sr1   )r   TTNN)r(   r)   r*   r+   Úintr‚   rm   r,   ÚTensorr}   rƒ   r�   r„   r…   s   @r2   r‡   r‡     s‚   ø„ ñð ()ØØ!%ØØñ"àð"ð ð"ð "%ð	"ð
 ð"ð ð"ð 
õ"ðH˜UŸ\™\ð ¨e¯l©ló ðS˜C÷ Sr1   r‡   c                   ó,   ‡ — e Zd Zdˆ fd„	Zd„ Zd„ Zˆ xZS )ÚIdeficsRMSNormc                 óŠ   •— t         ‰| �  «        t        j                  t	        j
                  |«      «      | _        || _        y)z=
        IdeficsRMSNorm is equivalent to T5LayerNorm
        N)rl   rm   r   Ú	Parameterr,   Úonesro   Úvariance_epsilon)rq   Úhidden_sizeÚepsrs   s      €r2   rm   zIdeficsRMSNorm.__init__N  s1   ø€ ô 	‰ÑÔÜ—l‘l¤5§:¡:¨kÓ#:Ó;ˆŒØ #ˆÕr1   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é   r8   T)Úkeepdim)rC   r,   Úfloat32ÚpowÚmeanÚrsqrtr�   ro   ri   Úfloat16Úbfloat16)rq   r%   Úvariances      r2   r}   zIdeficsRMSNorm.forwardV  s™   € Ø ×#Ñ#¤E§M¡MÓ2×6Ñ6°qÓ9×>Ñ>¸rÈ4Ð>ÓPˆØ%¬¯©°H¸t×?TÑ?TÑ4TÓ(UÑUˆð �;‰;×Ñ¤§¡´·±Ð ?Ñ?Ø)×,Ñ,¨T¯[©[×->Ñ->Ó?ˆMà�{‰{˜]Ñ*Ð*r1   c                 ó^   — t        | j                  j                  «      › d| j                  › �S )Nz, eps=)r/   ro   r@   r�   r€   s    r2   r�   zIdeficsRMSNorm.extra_repr`  s*   € Ü˜Ÿ™×)Ñ)Ó*Ð+¨6°$×2GÑ2GÐ1HÐIÐIr1   )g�íµ ÷Æ°>)r(   r)   r*   rm   r}   r�   r„   r…   s   @r2   r™   r™   M  s   ø„ õ$ò+öJr1   r™   c                   ó.   ‡ — e Zd Zdˆ fd„	Zd„ Zdd„Zˆ xZS )ÚIdeficsEmbeddingc                 óÂ  •— t         ‰| �  «        || _        || _        || _        d| j                  t        j                  d| j                  dt
        j                  ¬«      j                  |t
        j                  ¬«      | j                  z  z  z  }| j                  d|d¬«       | j                  || j                  j                  t        j                  «       ¬	«       y )
Nç      ð?r   r¡   ©ri   ©rD   ri   Úinv_freqF©Ú
persistent©Úseq_lenrD   ri   )rl   rm   ÚdimÚmax_position_embeddingsÚbaser,   r?   Úint64rC   ÚfloatÚregister_bufferÚ_set_cos_sin_cacher±   rD   Úget_default_dtype)rq   r¶   r·   r¸   rD   r±   rs   s         €r2   rm   zIdeficsEmbedding.__init__f  s¿   ø€ Ü‰ÑÔàˆŒØ'>ˆÔ$ØˆŒ	ØØ�I‰IÜ—‘˜Q §¡¨!´5·;±;Ô?×BÑBÈ&ÔX]×XcÑXcÐBÓdÐgk×goÑgoÑoñqñ
ˆð 	×Ñ˜Z¨¸eÐÔDð 	×ÑØ+°D·M±M×4HÑ4HÔPU×PgÑPgÓPið 	 õ 	
r1   c                 óÜ  — || _         t        j                  | j                   |t        j                  ¬«      j	                  | j
                  «      }t        j                  d|| j
                  «      }t        j                  ||fd¬«      }| j                  d|j                  «       j                  |«      d¬«       | j                  d|j                  «       j                  |«      d¬«       y )	Nr°   úi,j->ijr8   ©r¶   Ú
cos_cachedFr²   Ú
sin_cached)Úmax_seq_len_cachedr,   r?   r¹   Útype_asr±   Úeinsumr‘   r»   ÚcosrC   Úsin)rq   rµ   rD   ri   rV   ÚfreqsÚembs          r2   r¼   z#IdeficsEmbedding._set_cos_sin_cachew  sª   € Ø")ˆÔÜ�L‰L˜×0Ñ0¸ÄuÇ{Á{ÔS×[Ñ[Ð\`×\iÑ\iÓjˆä—‘˜Y¨¨4¯=©=Ó9ˆä�i‰i˜ ˜¨BÔ/ˆØ×Ñ˜\¨3¯7©7«9¯<©<¸Ó+>È5ÐÔQØ×Ñ˜\¨3¯7©7«9¯<©<¸Ó+>È5ÐÕQr1   c                 ó  — || j                   kD  r(| j                  ||j                  |j                  ¬«       | j                  d | j                  |j                  ¬«      | j                  d | j                  |j                  ¬«      fS )Nr´   r¯   )rÃ   r¼   rD   ri   rÁ   rC   rÂ   )rq   Úxrµ   s      r2   r}   zIdeficsEmbedding.forward�  sy   € à�T×,Ñ,Ò,Ø×#Ñ#¨G¸A¿H¹HÈAÏGÉGÐ#ÔTð �O‰O˜H˜WÐ%×(Ñ(¨q¯w©wÐ(Ó7Ø�O‰O˜H˜WÐ%×(Ñ(¨q¯w©wÐ(Ó7ð
ð 	
r1   )i   i'  NrS   )r(   r)   r*   rm   r¼   r}   r„   r…   s   @r2   r¬   r¬   e  s   ø„ õ
ò"R÷
r1   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..Nr8   r¡   rÀ   )r@   r,   r‘   )rË   Úx1Úx2s      r2   Úrotate_halfrÏ   Œ  sZ   € à	
ˆ3Ð"�!—'‘'˜"‘+ Ñ"Ð"Ð"Ñ	#€BØ	
ˆ3�—‘˜‘˜qÑ Ñ"Ð"Ñ	#€BÜ�9‰9�r�c˜2�Y BÔ'Ð'r1   c                 óª   — ||   j                  |«      }||   j                  |«      }| |z  t        | «      |z  z   }||z  t        |«      |z  z   }||fS )an  Applies Rotary Position Embedding to the query and key tensors.

    Args:
        q (`torch.Tensor`): The query tensor.
        k (`torch.Tensor`): The key tensor.
        cos (`torch.Tensor`): The cosine part of the rotary embedding.
        sin (`torch.Tensor`): The sine part of the rotary embedding.
        position_ids (`torch.Tensor`):
            The position indices of the tokens corresponding to the query and key tensors. For example, this can be
            used to pass offsetted position ids when working with a KV-cache.
        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Ç   Úposition_idsÚunsqueeze_dimÚq_embedÚk_embeds           r2   Úapply_rotary_pos_embrØ   “  sg   € ð* ˆlÑ
×
%Ñ
% mÓ
4€CØ
ˆlÑ
×
%Ñ
% mÓ
4€CØ�3‰wœ; q›>¨CÑ/Ñ0€GØ�3‰wœ; q›>¨CÑ/Ñ0€GØ�GÐÐr1   c                   ó2   ‡ — e Zd Zdededefˆ fd„Zd„ Zˆ xZS )Ú
IdeficsMLPrž   Úintermediate_sizeÚ
hidden_actc                 óì   •— t         ‰| �  «        t        j                  ||d¬«      | _        t        j                  ||d¬«      | _        t        j                  ||d¬«      | _        t        |   | _        y )NF©r‹   )	rl   rm   r   rP   Ú	gate_projÚ	down_projÚup_projr	   Úact_fn)rq   rž   rÛ   rÜ   rs   s       €r2   rm   zIdeficsMLP.__init__±  s[   ø€ ô 	‰ÑÔÜŸ™ ;Ð0AÈÔNˆŒÜŸ™Ð#4°kÈÔNˆŒÜ—y‘y Ð.?ÀeÔLˆŒÜ˜ZÑ(ˆ�r1   c                 ó„   — | j                  | j                  | j                  |«      «      | j                  |«      z  «      S rS   )rà   râ   rß   rá   )rq   rË   s     r2   r}   zIdeficsMLP.forward½  s0   € Ø�~‰~˜dŸk™k¨$¯.©.¸Ó*;Ó<¸t¿|¹|ÈA»ÑNÓOÐOr1   )r(   r)   r*   r–   rƒ   rm   r}   r„   r…   s   @r2   rÚ   rÚ   °  s*   ø„ ð
)àð
)ð ð
)ð õ	
)öPr1   rÚ   rW   ÚqueryÚkeyÚvaluer>   ÚscalingÚdropoutc                 óÀ  — 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 )Nr8   éþÿÿÿ)r¶   ri   ©ÚpÚtrainingr   r¡   )r,   ÚmatmulÚ	transposer   Ú
functionalÚsoftmaxr£   rC   ri   rè   rí   Ú
contiguous)
rW   rä   rå   ræ   r>   rç   rè   rr   Úattn_weightsÚattn_outputs
             r2   Úeager_attention_forwardrõ   Â  sº   € ô —<‘<  s§}¡}°R¸Ó'<Ó=ÀÑG€LØÐ!Ø# nÑ4ˆä—=‘=×(Ñ(¨¸2ÄUÇ]Á]Ð(ÓS×VÑVÐW\×WbÑWbÓc€LÜ—=‘=×(Ñ(¨¸È6Ï?É?Ð(Ó[€Lä—,‘,˜|¨UÓ3€KØ×'Ñ'¨¨1Ó-×8Ñ8Ó:€Kà˜Ð$Ð$r1   c                   óN  ‡ — e Zd ZdZ	 	 	 	 	 ddedededededz  ded	edz  fˆ fd
„Zde	j                  dedefd„Z	 	 	 	 dde	j                  de	j                  dz  de	j                  dz  de	j                  dz  dedz  dee   dee	j                  e	j                  f   fd„Zˆ xZS )ÚIdeficsAttentionz=Multi-headed attention from 'Attention Is All You Need' paperNrž   Ú	num_headsrè   Úis_cross_attentionÚconfigÚqk_layer_normsÚ	layer_idxc                 óò  •— t         ‰	| �  «        || _        || _        || _        ||z  | _        || _        d| _        | j
                  dz  | _        || _	        |€-t        j                  d| j                  j                  › d�«       | j
                  |z  | j                  k7  rt        d| j                  › d|› d�«      ‚|| _        t!        t"        j$                  d«      st        d	«      ‚| j                  rÁt!        |j&                  d
«      s| j                  n|j&                  j(                  }t#        j*                  | j                  || j
                  z  d¬«      | _        t#        j*                  ||| j
                  z  d¬«      | _        t#        j*                  ||| j
                  z  d¬«      | _        nœt#        j*                  | j                  || j
                  z  d¬«      | _        t#        j*                  | j                  || j
                  z  d¬«      | _        t#        j*                  | j                  || j
                  z  d¬«      | _        t#        j*                  || j
                  z  |d¬«      | _        t5        | j
                  «      | _        || _        | j8                  rMt;        | j
                  |j<                  ¬«      | _        t;        | j
                  |j<                  ¬«      | _         y y )NTg      à¿zInstantiating z¹ without passing a `layer_idx` is not recommended and will lead to errors during the forward call if caching is used. Please make sure to provide a `layer_idx` when creating this class.z?hidden_size must be divisible by num_heads (got `hidden_size`: z and `num_heads`: z).Úscaled_dot_product_attentionz)this model requires pytorch 2.0 or higherÚ	embed_dimFrÞ   ©rŸ   )!rl   rm   rú   rž   rø   Úhead_dimrè   Ú	is_causalrç   rü   ÚloggerÚwarning_oncers   r(   rk   rù   Úhasattrr   rð   Úvision_configrÿ   rP   Úq_projÚk_projÚv_projÚo_projr¬   Ú
rotary_embrû   r™   Úrms_norm_epsÚq_layer_normÚk_layer_norm)
rq   rž   rø   rè   rù   rú   rû   rü   Úkv_input_dimrs   s
            €r2   rm   zIdeficsAttention.__init__Ý  sŒ  ø€ ô 	‰ÑÔØˆŒØ&ˆÔØ"ˆŒØ# yÑ0ˆŒØˆŒØˆŒØ—}‘} dÑ*ˆŒà"ˆŒØÐÜ×ÑØ  §¡×!8Ñ!8Ð 9ð :,ð ,ôð �M‰M˜IÑ%¨$×*:Ñ*:Ò:ÜØQÐRV×RbÑRbÐQcØ$ Y K¨rð3óð ð
 #5ˆÔä”r—}‘}Ð&DÔEÜÐHÓIÐIà×"Ò"ä(/°×0DÑ0DÀkÔ(R�× Ò ÐX^×XlÑXl×XvÑXvð ô Ÿ)™)Ø× Ñ Ø˜DŸM™MÑ)ØôˆDŒKô
 Ÿ)™) L°)¸d¿m¹mÑ2KÐRWÔXˆDŒKÜŸ)™)ØØ˜DŸM™MÑ)ØôˆD�Kô Ÿ)™)Ø× Ñ Ø˜DŸM™MÑ)ØôˆDŒKô
 Ÿ)™)Ø× Ñ Ø˜DŸM™MÑ)ØôˆDŒKô
 Ÿ)™)Ø× Ñ Ø˜DŸM™MÑ)ØôˆDŒKô
 —i‘iØ˜Ÿ™Ñ%ØØô
ˆŒô
 +¨4¯=©=Ó9ˆŒà,ˆÔØ×ÒÜ .¨t¯}©}À&×BUÑBUÔ VˆDÔÜ .¨t¯}©}À&×BUÑBUÔ VˆDÕð r1   Útensorrµ   Úbszc                 óŽ   — |j                  ||| j                  | j                  «      j                  dd«      j	                  «       S )Nr   r¡   )rA   rø   r  rï   rò   )rq   r  rµ   r  s       r2   Ú_shapezIdeficsAttention._shape.  s7   € Ø�{‰{˜3 ¨¯©¸¿¹ÓG×QÑQÐRSÐUVÓW×bÑbÓdÐdr1   r%   Úkey_value_statesr>   rÔ   r$   rr   re   c                 óÄ  — | j                   xs |d u}|j                  «       \  }}	}
| j                  |«      j                  ||	| j                  | j
                  «      j                  dd«      }|s�| j                  |«      j                  ||	| j                  | j
                  «      j                  dd«      }| j                  |«      j                  ||	| j                  | j
                  «      j                  dd«      }n¢|j                  «       \  }
}}
| j                  |«      j                  ||| j                  | j
                  «      j                  dd«      }| j                  |«      j                  ||| j                  | j
                  «      j                  dd«      }|j                  d   }|�||j                  «       z  }|s2| j                  |t        ||	«      ¬«      \  }}t        |||||«      \  }}|� |j                  ||| j                  «      \  }}| j                   r"| j#                  |«      }| j%                  |«      }t'        j(                  | j*                  j,                  t.        «      } || ||||f| j0                  sdn| j2                  | j4                  dœ|¤Ž\  }}|j7                  ||	d«      j9                  «       }| j;                  |«      }||fS )Nr   r¡   rê   )rµ   ç        )rè   rç   r8   )rù   Úsizer  rA   rø   r  rï   r  r	  r@   Úget_seq_lengthr  ÚmaxrØ   Úupdaterü   rû   r  r  r   Úget_interfacerú   Ú_attn_implementationrõ   rí   rè   rç   Úreshaperò   r
  )rq   r%   r  r>   rÔ   r$   rr   rù   r  Úq_lenÚ_Úquery_statesÚ
key_statesÚvalue_statesÚkv_lenÚ
kv_seq_lenrÆ   rÇ   Úattention_interfacerô   ró   s                        r2   r}   zIdeficsAttention.forward1  s¿  € ð "×4Ñ4ÒTÐ8HÐPTÐ8TÐà%×*Ñ*Ó,‰ˆˆU�Aà—{‘{ =Ó1×6Ñ6°s¸EÀ4Ç>Á>ÐSW×S`ÑS`Óa×kÑkÐlmÐopÓqˆÙ!ØŸ™ ]Ó3×8Ñ8¸¸eÀTÇ^Á^ÐUY×UbÑUbÓc×mÑmÐnoÐqrÓsˆJØŸ;™; }Ó5×:Ñ:¸3ÀÀtÇ~Á~ÐW[×WdÑWdÓe×oÑoÐpqÐstÓu‰Là+×0Ñ0Ó2‰LˆAˆv�qØŸ™Ð%5Ó6×;Ñ;¸CÀÈÏÉÐY]×YfÑYfÓg×qÑqÐrsÐuvÓwˆJà—‘Ð,Ó-×2Ñ2°3¸ÀÇÁÐPT×P]ÑP]Ó^×hÑhÐijÐlmÓnð ð  ×%Ñ% bÑ)ˆ
ØÐ&Ø˜/×8Ñ8Ó:Ñ:ˆJá!Ø—‘ |¼SÀÈUÓ=S�ÓT‰HˆC�Ü';¸LÈ*ÐVYÐ[^Ð`lÓ'mÑ$ˆL˜*ð Ð&Ø'6×'=Ñ'=¸jÈ,ÐX\×XfÑXfÓ'gÑ$ˆJ˜à×ÒØ×,Ñ,¨\Ó:ˆLØ×*Ñ*¨:Ó6ˆJä(?×(MÑ(MØ�K‰K×,Ñ,Ô.Eó)
Ðñ %8ØØØØØð	%
ð  $Ÿ}š}‘C°$·,±,Ø—L‘Lñ	%
ð ñ	%
Ñ!ˆ�\ð "×)Ñ)¨#¨u°bÓ9×DÑDÓFˆØ—k‘k +Ó.ˆà˜LÐ(Ð(r1   )r  FNFN)NNNN)r(   r)   r*   r+   r–   rº   r‚   r   rm   r,   r—   r  Ú
LongTensorr
   r   r   r/   r}   r„   r…   s   @r2   r÷   r÷   Ú  s0  ø„ ÙGð Ø#(Ø*.Ø$Ø $ñOWàðOWð ðOWð ð	OWð
 !ðOWð ! 4Ñ'ðOWð ðOWð ˜‘:õOWðbe˜UŸ\™\ð e°Cð e¸có eð 15Ø.2Ø04Ø(,ñ;)à—|‘|ð;)ð  Ÿ,™,¨Ñ-ð;)ð Ÿ™ tÑ+ð	;)ð
 ×&Ñ&¨Ñ-ð;)ð  ™ð;)ð Ð+Ñ,ð;)ð 
ˆu�|‰|˜UŸ\™\Ð)Ñ	*÷;)r1   r÷   c                   óÊ   ‡ — e Zd Zddededz  fˆ fd„Ze	 	 	 ddej                  dej                  dz  dej                  dz  de
dz  d	ee   d
ej                  fd„«       Zˆ xZS )ÚIdeficsDecoderLayerNrú   rü   c                 óÆ  •— t         ‰| �  «        |j                  | _        t        | j                  |j                  |j
                  ||¬«      | _        t        | j                  |j                  |j                  ¬«      | _
        t        |j                  |j                  ¬«      | _        t        |j                  |j                  ¬«      | _        |j
                  | _        y )N)rž   rø   rè   rú   rü   ©rž   rÛ   rÜ   r   )rl   rm   rž   r÷   Únum_attention_headsrè   Ú	self_attnrÚ   rÛ   rÜ   Úmlpr™   r  Úinput_layernormÚpost_attention_layernorm©rq   rú   rü   rs   s      €r2   rm   zIdeficsDecoderLayer.__init__q  s³   ø€ Ü‰ÑÔØ!×-Ñ-ˆÔÜ)Ø×(Ñ(Ø×0Ñ0Ø—N‘NØØô
ˆŒô Ø×(Ñ(Ø$×6Ñ6Ø×(Ñ(ô
ˆŒô
  .¨f×.@Ñ.@Àf×FYÑFYÔZˆÔÜ(6°v×7IÑ7IÈv×ObÑObÔ(cˆÔ%Ø—~‘~ˆ�r1   r%   r>   rÔ   r$   rr   re   c                 ó”  — |}| j                  |«      } | j                  d||||dœ|¤Ž\  }}t        j                  j	                  || j                  | j
                  ¬«      }||z   }|}| j                  |«      }| j                  |«      }t        j                  j	                  || j                  | j
                  ¬«      }||z   }|S )N)r%   r>   rÔ   r$   rë   r0   )r.  r,  r   rð   rè   rí   r/  r-  )rq   r%   r>   rÔ   r$   rr   Úresidualr  s           r2   r}   zIdeficsDecoderLayer.forward„  sØ   € ð !ˆà×,Ñ,¨]Ó;ˆð *˜4Ÿ>™>ð 
Ø'Ø)Ø%Ø+ñ	
ð
 ñ
Ñˆ�qô Ÿ™×-Ñ-¨m¸t¿|¹|ÐVZ×VcÑVcÐ-ÓdˆØ  =Ñ0ˆð !ˆØ×5Ñ5°mÓDˆØŸ™ Ó/ˆÜŸ™×-Ñ-¨m¸t¿|¹|ÐVZ×VcÑVcÐ-ÓdˆØ  =Ñ0ˆàÐr1   rS   )NNN)r(   r)   r*   r   r–   rm   r   r,   r—   r&  r
   r   r   r-   r}   r„   r…   s   @r2   r(  r(  p  s›   ø„ ñ&˜}ð &¸¸t¹õ &ð& ð /3Ø04Ø(,ñà—|‘|ðð Ÿ™ tÑ+ðð ×&Ñ&¨Ñ-ð	ð
  ™ðð Ð+Ñ,ðð 
×	Ñ	òó ôr1   r(  c                   ó
  ‡ — e Zd Zddededz  fˆ fd„Ze	 	 	 	 	 ddej                  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
e   dej                  fd„«       Zˆ xZS )ÚIdeficsGatedCrossAttentionLayerNrú   rü   c           	      óª	  •— t         ‰| �  «        |j                  | _        t        | j                  |j                  d|j
                  ||j                  |¬«      | _        t        | j                  |j                  |j                  ¬«      | _        t        |j                  |j                  ¬«      | _        t        |j                  |j                  ¬«      | _        |j
                  | _        t#        j$                  «       | _        t#        j$                  «       | _        |j*                  dk(  �r|j,                  dk(  rtt#        j.                  t1        j2                  dd| j                  «      «      | _        t#        j.                  t1        j2                  dd| j                  «      «      | _        �nþ|j,                  dk(  r\t#        j.                  t1        j2                  d«      «      | _        t#        j.                  t1        j2                  d«      «      | _        �n“t9        d	|j,                  › d
�«      ‚|j*                  dk(  �r|j,                  dk(  rtt#        j.                  t1        j:                  dd| j                  «      «      | _        t#        j.                  t1        j:                  dd| j                  «      «      | _        �nç|j,                  dk(  r\t#        j.                  t1        j:                  d«      «      | _        t#        j.                  t1        j:                  d«      «      | _        �n|t9        d	|j,                  › d
�«      ‚|j*                  dv �r;|j,                  dk(  r�t#        j.                  t1        j<                  d|j>                  dd| j                  f¬«      «      | _        t#        j.                  t1        j<                  d|j>                  dd| j                  f¬«      «      | _        n¶|j,                  dk(  rut#        j.                  t1        j<                  d|j>                  d¬«      «      | _        t#        j.                  t1        j<                  d|j>                  d¬«      «      | _        n2t9        d	|j,                  › d
�«      ‚tA        d|j*                  › d�«      ‚tC        | d«      rtC        | d«      st9        d«      ‚y )NT)rž   rø   rù   rè   rú   rû   rü   r*  r   ÚzerosÚvectorr   rº   z Unknown value for `alpha_type` (ú)rœ   >   ÚnormalÚrandomÚgaussianr  )r¥   Ústdr  zAlpha initialization scheme z not yet implemented!Úalpha_cross_attnÚalpha_densez+Alpha parameters not initialized correctly!)"rl   rm   rž   r÷   r+  rè   rû   Ú
cross_attnrÚ   rÛ   rÜ   r-  r™   r  r.  r/  rú   r   ÚTanhÚact_cross_attnÚ	act_denseÚalpha_initializerÚ
alpha_typer›   r,   r6  r=  r>  rk   rœ   r9  Úalphas_initializer_rangeÚNotImplementedErrorr  r0  s      €r2   rm   z(IdeficsGatedCrossAttentionLayer.__init__§  sª  ø€ Ü‰ÑÔØ!×-Ñ-ˆÔÜ*Ø×(Ñ(Ø×0Ñ0Ø#Ø—N‘NØØ!×0Ñ0Øô
ˆŒô Ø×(Ñ(Ø$×6Ñ6Ø×(Ñ(ô
ˆŒô
  .¨f×.@Ñ.@Àf×FYÑFYÔZˆÔÜ(6°v×7IÑ7IÈv×ObÑObÔ(cˆÔ%Ø—n‘nˆŒä Ÿg™g›iˆÔÜŸ™›ˆŒà×#Ñ# wÓ.Ø× Ñ  HÒ,Ü(*¯©´U·[±[ÀÀAÀt×GWÑGWÓ5XÓ(Y�Ô%Ü#%§<¡<´·±¸A¸qÀ$×BRÑBRÓ0SÓ#T�Ö Ø×"Ñ" gÒ-Ü(*¯©´U·[±[À³^Ó(D�Ô%Ü#%§<¡<´·±¸A³Ó#?�Ö ä Ð#CÀF×DUÑDUÐCVÐVWÐ!XÓYÐYà×%Ñ%¨Ó/Ø× Ñ  HÒ,Ü(*¯©´U·Z±ZÀÀ1Àd×FVÑFVÓ5WÓ(X�Ô%Ü#%§<¡<´·
±
¸1¸aÀ×AQÑAQÓ0RÓ#S�Ö Ø×"Ñ" gÒ-Ü(*¯©´U·Z±ZÀ³]Ó(C�Ô%Ü#%§<¡<´·
±
¸1³Ó#>�Ö ä Ð#CÀF×DUÑDUÐCVÐVWÐ!XÓYÐYà×%Ñ%Ð)IÒIØ× Ñ  HÒ,Ü(*¯©Ü—L‘L c¨v×/NÑ/NÐVWÐYZÐ\`×\lÑ\lÐUmÔnó)�Ô%ô $&§<¡<Ü—L‘L c¨v×/NÑ/NÐVWÐYZÐ\`×\lÑ\lÐUmÔnó$�Õ ð ×"Ñ" gÒ-Ü(*¯©Ü—L‘L c¨v×/NÑ/NÐVWÔYó)�Ô%ô $&§<¡<´·±À#È6×KjÑKjÐrsÔ0uÓ#v�Õ ä Ð#CÀF×DUÑDUÐCVÐVWÐ!XÓYÐYô &Ð(DÀV×E]ÑE]ÐD^Ð^sÐ&tÓuÐuä˜Ð0Ô1´g¸dÀMÔ6RÜÐJÓKÐKð 7Sr1   r%   r>   r'   r<   Úcross_attention_gater$   rr   re   c                 óŽ  — |€t        d«      ‚|€t        d«      ‚|�t        d«      ‚|}| j                  |«      } | j                  d	|||dœ|¤Ž\  }}	t        j
                  j                  || j                  | j                  ¬«      }|j                  |dk(  dd…dd…df   d«      }|| j                  | j                  «      |z  z   }|}| j                  |«      }| j                  |«      }t        j
                  j                  || j                  | j                  ¬«      }|| j                  | j                  «      |z  z   }|S )
a  
        image_hidden_states (`torch.FloatTensor`):
            Input to the layer of shape `(batch, seq_len, embed_dim)`
        image_attention_mask (`torch.FloatTensor`, *optional*):
            image attention mask of size
            `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
        cross_attention_gate (`torch.FloatTensor`, *optional*):
            gate of size `(batch, seq_len)` used to zero-out cross-attention output for tokens attending no images.
        Nzt`image_hidden_states` is required for Idefics cross attention module which are visual features to be conditioned on.z‹`cross_attention_gate` is required for Idefics cross attention module to zero-out the cross-attention hidden_states attending to no images.zMPast key value states are not implemented for Idefics cross attention module.)r%   r  r>   rë   r   r  r0   )rk   rF  r.  r?  r   rð   rè   rú   rí   Úmasked_fillrA  r=  r/  r-  rB  r>  )
rq   r%   r>   r'   r<   rG  r$   rr   r2  r  s
             r2   r}   z'IdeficsGatedCrossAttentionLayer.forwardé  sm  € ð( Ð&Üð#óð ð
  Ð'Üð ^óð ð Ð&Ü%Ð&uÓvÐvà ˆà×,Ñ,¨]Ó;ˆð +˜4Ÿ?™?ð 
Ø'Ø0Ø/ñ
ð ñ	
Ñˆ�qô Ÿ™×-Ñ-¨m¸t¿{¹{ÐUY×UbÑUbÐ-Ócˆà%×1Ñ1Ð3GÈ1Ñ3LÊaÒQRÐTXÈjÑ2YÐ[^Ó_ˆØ  4×#6Ñ#6°t×7LÑ7LÓ#MÐP]Ñ#]Ñ]ˆð !ˆØ×5Ñ5°mÓDˆØŸ™ Ó/ˆÜŸ™×-Ñ-¨m¸t¿{¹{ÐUY×UbÑUbÐ-ÓcˆØ  4§>¡>°$×2BÑ2BÓ#CÀmÑ#SÑSˆàÐr1   rS   )NNNNN)r(   r)   r*   r   r–   rm   r   r,   r—   r
   r   r   r-   r}   r„   r…   s   @r2   r4  r4  ¦  sÌ   ø„ ñ@L˜}ð @L¸¸t¹õ @LðD ð /3Ø37Ø48Ø48Ø(,ñ8à—|‘|ð8ð Ÿ™ tÑ+ð8ð #Ÿ\™\¨DÑ0ð	8ð
 $Ÿl™l¨TÑ1ð8ð $Ÿl™l¨TÑ1ð8ð  ™ð8ð Ð+Ñ,ð8ð 
×	Ñ	ò8ó ô8r1   r4  c                   ó’   ‡ — e Zd ZU eed<   dZdZdZg d¢ZdZ	dZ
dZdZe eedd¬	«      d
œZ ej$                  «       ˆ fd„«       Zˆ xZS )ÚIdeficsPreTrainedModelrú   r\   )ÚimageÚtextT)r(  r4  ÚIdeficsVisionEncoderLayerFr   r,  )ÚindexÚ
layer_name)r%   r&   c                 óx  •— t         ‰| �  |«       t        |t        «      ryt	        j
                  |j                  «       t	        j                  |j                  t        j                  |j                  j                  d   «      j                  d«      «       y t        |t        «      �r6| j                  j                  dk(  r?t	        j                   |j"                  «       t	        j                   |j$                  «       y | j                  j                  dk(  r?t	        j&                  |j"                  «       t	        j&                  |j$                  «       y | j                  j                  dv rmt	        j
                  |j"                  d| j                  j(                  ¬«       t	        j
                  |j$                  d| j                  j(                  ¬«       y y t        |t*        «      r t	        j
                  |j,                  «       y t        |t.        «      �rd|j0                  t        j                  d	|j2                  d
«      |j2                  z  z  z  }t	        j                  |j4                  |«       t        j                  |j6                  «      j9                  |«      }t        j:                  d||«      }t        j<                  ||fd¬«      }t	        j                  |j>                  |jA                  «       «       t	        j                  |jB                  |jE                  «       «       y y )Nr8   )r   r8   r6  rœ   >   r9  r:  r;  r  )r¥   r<  r®   r   r¡   r¿   rÀ   )#rl   Ú_init_weightsrT   r   ÚinitÚnormal_Úclass_embeddingÚcopy_rÔ   r,   r?   r@   Úexpandr4  rú   rC  Úzeros_r=  r>  Úones_rE  r   Úlatentsr¬   r¸   r¶   r±   r·   rÄ   rÅ   r‘   rÁ   rÆ   rÂ   rÇ   )rq   rW   r±   rV   rÈ   rÉ   rs   s         €r2   rR  z$IdeficsPreTrainedModel._init_weights7  s  ø€ ô
 	‰Ñ˜fÔ%Ü�fÔ5Ô6Ü�L‰L˜×/Ñ/Ô0Ü�J‰J�v×*Ñ*¬E¯L©L¸×9LÑ9L×9RÑ9RÐSUÑ9VÓ,W×,^Ñ,^Ð_fÓ,gÕhÜ˜Ô ?Õ@Ø�{‰{×,Ñ,°Ò7Ü—‘˜F×3Ñ3Ô4Ü—‘˜F×.Ñ.Õ/Ø—‘×.Ñ.°&Ò8Ü—
‘
˜6×2Ñ2Ô3Ü—
‘
˜6×-Ñ-Õ.Ø—‘×.Ñ.Ð2RÑRÜ—‘˜V×4Ñ4¸3ÀDÇKÁK×DhÑDhÕiÜ—‘˜V×/Ñ/°c¸t¿{¹{×?cÑ?cÖdð Sô ˜Ô 9Ô:Ü�L‰L˜Ÿ™Õ(Ü˜Ô 0Õ1Ø˜fŸk™k¬e¯l©l¸1¸f¿j¹jÈ!Ó.LÈvÏzÉzÑ.YÑZÑ[ˆHÜ�J‰J�v—‘¨Ô1Ü—‘˜V×;Ñ;Ó<×DÑDÀXÓNˆAÜ—L‘L ¨A¨xÓ8ˆEä—)‘)˜U E˜N°Ô3ˆCÜ�J‰J�v×(Ñ(¨#¯'©'«)Ô4Ü�J‰J�v×(Ñ(¨#¯'©'«)Õ4ð 2r1   )r(   r)   r*   r   r.   Úbase_model_prefixÚinput_modalitiesÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_supports_sdpaÚ_supports_flash_attnÚ_can_compile_fullgraphÚ_supports_attention_backendr(  r   r÷   Ú_can_record_outputsr,   Úno_gradrR  r„   r…   s   @r2   rK  rK  %  sn   ø… àÓØÐØ(ÐØ&*Ð#ÚoÐØ€Nà ÐØ"ÐØ"&Ðð -Ù$Ð%5¸QÈ;ÔWñÐð
 €U‡]�]ƒ_ó5ó ô5r1   rK  c                   ó   ‡ — e Zd ZdZdefˆ fd„Zdd„Zdd„Zdd„Ze	e
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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e   deez  fd„«       «       «       Zˆ xZS )ÚIdeficsModelzŸ
    Transformer decoder consisting of `config.num_hidden_layers` layers. Each layer is a [`IdeficsDecoderLayer`]

    Args:
        config: IdeficsConfig
    rú   c           	      ó   •— t         ‰| �  |«       || _        |j                  | _        |j
                  | _        t        |j
                  |j                  |j                  |j                  | j                  ¬«      | _
        |j                  j                  | _        |j                  | _        |j                  | j                  _        t        |j                  «      | _        |j                   r]|j"                  }t%        ||j                  j&                  |j(                  |j*                  |j,                  |j.                  «      | _        t3        j4                  t7        |j8                  «      D �cg c]  }t;        ||¬«      ‘Œ c}«      | _        |j>                  | _        |j8                  | j>                  z  }t3        j4                  t7        |«      D �cg c]  }tA        ||¬«      ‘Œ c}«      | _!        d| _"        tG        |j                  |jH                  ¬«      | _%        | jM                  «        | jO                  |«       y c c}w c c}w )N)rg   rn   rh   rd   rj   )rü   Fr   )(rl   rm   rú   Úpad_token_idrj   Ú
vocab_sizerc   Úadditional_vocab_sizerž   Úfreeze_text_layersÚembed_tokensr  Ú
image_sizer  r   Úvision_modelÚuse_resamplerÚperceiver_configr   rÿ   Úresampler_depthÚresampler_n_headsÚresampler_head_dimÚresampler_n_latentsÚperceiver_resamplerr   Ú
ModuleListÚrangeÚnum_hidden_layersr(  ÚlayersÚcross_layer_intervalr4  Úgated_cross_attn_layersÚgradient_checkpointingr™   r  ÚnormÚ	post_initÚfreeze_relevant_params)rq   rú   rp  ÚiÚnum_cross_layersrs   s        €r2   rm   zIdeficsModel.__init__`  sá  ø€ Ü‰Ñ˜Ô ØˆŒØ!×.Ñ.ˆÔØ ×+Ñ+ˆŒä5Ø!×,Ñ,Ø&,×&BÑ&BØ ×,Ñ,Ø#×6Ñ6Ø×(Ñ(ô
ˆÔð !×.Ñ.×9Ñ9ˆŒØ#×1Ñ1ˆÔà28×2MÑ2Mˆ×ÑÔ/Ü4°V×5IÑ5IÓJˆÔð ×ÒØ%×6Ñ6ÐÜ'@ØØ×$Ñ$×.Ñ.Ø ×0Ñ0Ø ×2Ñ2Ø ×3Ñ3Ø ×4Ñ4ó(ˆDÔ$ô —m‘mÜ?DÀV×E]ÑE]Ó?^Ö_¸!Ô  °1Ö5Ò_ó
ˆŒð %+×$?Ñ$?ˆÔ!Ø!×3Ñ3°t×7PÑ7PÑPÐÜ')§}¡}ÜKPÐQaÓKbÖcÀaÔ,¨V¸qÖAÒcó(
ˆÔ$ð ',ˆÔ#ä" 6×#5Ñ#5¸6×;NÑ;NÔOˆŒ	ð 	�‰Ôà×#Ñ# FÕ+ùò `ùò ds   Å1IÇINc                 óÊ   — |€| j                   }|j                  r| j                  |j                  «       |j                  r"t	        | j
                  |j                  ¬«       y y ©N)r]   )rú   rk  Úfreeze_text_module_exceptionsÚfreeze_vision_layersra   rn  Úfreeze_vision_module_exceptions)rq   rú   s     r2   r  z#IdeficsModel.freeze_relevant_params’  sQ   € Øˆ>Ø—[‘[ˆFà×$Ò$Ø×#Ñ# F×$HÑ$HÔIà×&Ò&Ü˜×*Ñ*¸f×>dÑ>dÖeð 'r1   c                 óX   — | j                   | j                  fD ]  }t        ||¬«       Œ y rƒ  )ry  r}  ra   )rq   r]   rW   s      r2   rk  zIdeficsModel.freeze_text_layersœ  s+   € Ø—{‘{ D§I¡IÐ.ò 	FˆFÜ˜Ð3DÖEñ	Fr1   c                 ó2   — t        | j                  |¬«       y rƒ  )ra   rn  )rq   r]   s     r2   r…  z!IdeficsModel.freeze_vision_layers   s   € Ü�T×&Ñ&Ð:KÖLr1   rG   r>   rÔ   r$   Úinputs_embedsr9   r:   r;   r<   Ú	use_cacheÚinterpolate_pos_encodingrr   re   c                 óæ  — |�|j                   n|j                   }|du |duz  rt        d«      ‚|€| j                  |«      }|
r|€t        | j                  ¬«      }|j
                  d   }|�|j                  «       nd}||z   }|�F|€D|j                  «       j                  d«      dz
  }|j                  |dk(  d«       |dd…| d…f   }n7|€5t        j                  ||j                   ¬«      |z   }|j                  d«      }t        d„ |||fD «       «      d	k7  rt        d
«      ‚|�~|j                  | j                  |¬«      }|j
                  dd	 \  }} |j!                  «       j"                  ||z  g|j
                  d	d ¢­Ž }| j%                  ||¬«      j&                  }nJ|�H|j)                  «       \  }}}}|j                  | j                  |¬«      }|j#                  ||z  ||«      }| j                  j*                  rN|€4| j-                  «      }|j)                  d«      |j)                  d	«      }}n|j)                  «       \  }}}}|}n0|€#j)                  d«      |j)                  d	«      }}nt        d«      ‚|j#                  |z  |«      } |	d   j/                  ddd|«      j0                  g |	j
                  dd	 ¢d‘­Ž }	t        j2                  |	dd…ddd…dd…f   j5                  «       t        j6                  d||j                  ¬«      t        j8                  |j                  «      j:                  «      }	|	dk(  j=                  d¬«      j                  | j                  |¬«      j?                  d¬«      }|€2t        j@                  ||ft        j4                  |j                   ¬«      }tC        | j                  ||||¬«      }|}tE        | jF                  «      D ]O  \  }}|| jH                  z  dk(  r+| jJ                  || jH                  z     } ||||f|	|ddœ|¤Ž} ||f|||dœ|¤Ž}ŒQ | jM                  |«      }|j#                  ||||«      }tO        |||¬«      S )ab  
        image_encoder_embeddings (`torch.FloatTensor`, *optional*):
            The output of the image encoder.
        perceiver_embeddings (`torch.FloatTensor`, *optional*):
            The output of the perceiver resampler.
        image_attention_mask (`torch.LongTensor`, *optional*):
            The attention mask for the image encoder.
        Nz:You must specify exactly one of input_ids or inputs_embeds)rú   r   r   r8   )rD   c              3   ó$   K  — | ]  }|d u –— Œ
 y ­wrS   r0   )rU   rË   s     r2   rX   z'IdeficsModel.forward.<locals>.<genexpr>Õ  s   è ø€ Òa˜Qˆq�DŒyÑaùs   ‚r¡   z_Exactly 1 of pixel_values, image_encoder_embeddings or perceiver_embeddings has to be not-None.)ri   rD   )r9   r‹  zBIf `perceiver_embeddings` are passed, use_resampler should be True).Nr  r°   rÀ   )rú   r‰  r>   r$   rÔ   )r<   rG  r$   )r>   rÔ   r$   )r#   r'   r$   )(rD   rk   rl  r   rú   r@   r  ÚlongÚcumsumÚmasked_fill_r,   r?   rÑ   ÚsumrC   ri   rò   rA   rn  r#   r  ro  ru  rW  r  rx   r‚   r  ÚfinfoÚminrZ   Úsqueezerœ   r   Ú	enumeratery  rz  r{  r}  r"   )rq   rG   r>   rÔ   r$   r‰  r9   r:   r;   r<   rŠ  r‹  rr   rD   Ú
seq_lengthÚpast_key_values_lengthÚseq_length_with_pastÚ
batch_sizeÚ
num_imagesr'   Úimage_seq_lenÚimage_hidden_sizerG  Úcausal_maskr%   ÚidxÚdecoder_layerÚcross_attn_blocks                               r2   r}   zIdeficsModel.forward£  sù  € ð4 &/Ð%:�×!Ò!À×@TÑ@Tˆà˜Ð -°tÐ";Ò<ÜÐYÓZÐZàÐ Ø ×-Ñ-¨iÓ8ˆMá˜Ð0Ü*°$·+±+Ô>ˆOà"×(Ñ(¨Ñ+ˆ
ØETÐE` ×!?Ñ!?Ô!AÐfgÐØ)Ð,BÑBÐàÐ%¨,Ð*>à)×.Ñ.Ó0×7Ñ7¸Ó;¸aÑ?ˆLØ×%Ñ% n¸Ñ&9¸1Ô=Ø'ª¨J¨;©<¨Ñ8‰LØÐ!Ü Ÿ<™<¨
¸=×;OÑ;OÔPÐSiÑiˆLØ'×1Ñ1°!Ó4ˆLäÑa <Ð1IÐK_Ð"`ÔaÓaÐefÒfÜØqóð ð Ð%Ø'Ÿ?™?°·±ÀF˜?ÓKˆLØ%1×%7Ñ%7¸¸Ð%;Ñ"ˆJ˜
Ø9˜<×2Ñ2Ó4×9Ñ9¸*ÀzÑ:QÐkÐT`×TfÑTfÐghÐgiÐTjÒkˆLð #'×"3Ñ"3Ø)ÐD\ð #4ó #çÑñ  ð &Ð1ØG_×GdÑGdÓGfÑDˆJ˜
 MÐ3DØ":×"=Ñ"=ÀDÇJÁJÐW]Ð"=Ó"^ÐØ"5×":Ñ":¸:È
Ñ;RÐTaÐctÓ"uÐà�;‰;×$Ò$Ø#Ð+Ø'+×'?Ñ'?Ð@SÓ'TÐ$Ø3G×3LÑ3LÈQÓ3OÐQe×QjÑQjÐklÓQmÐ0‘àK_×KdÑKdÓKfÑH�
˜J¨Ð7HØ"6ÑØ!Ð)Ø/B×/GÑ/GÈÓ/JÐL_×LdÑLdÐefÓLgÐ,‰MäÐaÓbÐbà1×6Ñ6°zÀ:ÐP]ÑC]Ð_pÓqÐðÐ  Ñ+ß‰V�B˜˜B Ó.ß‰Wð:à*×0Ñ0°°!Ð4ð:à68ò:ð 	ô
  %Ÿ{™{Ø ¢ Dª!ªQ Ñ/×4Ñ4Ó6Ü�L‰L˜ VÐ3F×3LÑ3LÔMÜ�K‰KÐ+×1Ñ1Ó2×6Ñ6ó 
Ðð " SÑ(×-Ñ-°"Ð-Ó5×8Ñ8¸t¿z¹zÐRXÐ8ÓY×aÑaÐfgÐaÓhð 	ð
 Ð!Ü"ŸZ™ZØÐ1Ð2¼%¿*¹*È]×MaÑMaôˆNô )Ø—;‘;Ø'Ø)Ø+Ø%ô
ˆð &ˆä"+¨D¯K©KÓ"8ò 	ÑˆC�à�T×.Ñ.Ñ.°!Ò3Ø#'×#?Ñ#?ÀÀt×G`ÑG`Ñ@`Ñ#aÐ Ù 0Ø!ØØ'ð!ð *>Ø)=Ø$(ñ!ð ñ!�ñ *Øðà*Ø)Ø /ñ	ð
 ñ‰Mð	ð, Ÿ	™	 -Ó0ˆØ1×6Ñ6°zÀ:È}Ð^oÓpÐä-Ø+Ø 3Ø+ô
ð 	
r1   rS   ©r0   )NNNNNNNNNNF)r(   r)   r*   r+   r   rm   r  rk  r…  r   r   r   r,   r&  r—   r
   r-   r‚   r   r   r/   r"   r}   r„   r…   s   @r2   rf  rf  W  si  ø„ ñð0,˜}õ 0,ódfóFóMð  ØØð .2Ø.2Ø04Ø(,Ø26Ø15Ø=AØ9=Ø48Ø!%Ø05ñS
à×#Ñ# dÑ*ðS
ð Ÿ™ tÑ+ðS
ð ×&Ñ&¨Ñ-ð	S
ð
  ™ðS
ð ×(Ñ(¨4Ñ/ðS
ð ×'Ñ'¨$Ñ.ðS
ð #(×"3Ñ"3°dÑ":ðS
ð $×/Ñ/°$Ñ6ðS
ð $Ÿl™l¨TÑ1ðS
ð ˜$‘;ðS
ð #'¨¡+ðS
ð Ð+Ñ,ðS
ð 
Ð/Ñ	/òS
ó ó ó  ôS
r1   rf  c            "       ó  ‡ — e Zd ZddiZdˆ fd„	Ze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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ej                  z  dee   deez  fd„«       «       Z	 	 	 	 	 	 	 	 dˆ fd„	Z	 ddedeeef   dedeeef   fˆ fd„Zˆ xZS )ÚIdeficsForVisionText2Textúlm_head.weightúmodel.embed_tokens.weightNc                 ó  •— t         ‰| �  |«       t        |«      | _        t	        |j
                  |j                  |j                  d|j                  ¬«      | _	        |j                  dkD  r
dddœ| _
        | j                  «        y )NFr•   r   r¥  z.model.embed_tokens.additional_embedding.weight)r¤  zlm_head.additional_fc.weight)rl   rm   rf  r\   r‡   rž   ri  rj  Úfreeze_lm_headÚlm_headÚ_tied_weights_keysr~  )rq   rú   rn  rs   s      €r2   rm   z"IdeficsForVisionText2Text.__init__?  s{   ø€ Ü‰Ñ˜Ô Ü! &Ó)ˆŒ
ä-Ø×*Ñ*Ø×*Ñ*Ø$*×$@Ñ$@ØØ#×2Ñ2ô
ˆŒð ×'Ñ'¨!Ò+à"=Ø0`ñ'ˆDÔ#ð 	�‰Õr1   rG   r>   rÔ   r$   r‰  r9   r:   r;   r<   ÚlabelsrŠ  r‹  Úlogits_to_keeprr   re   c                 óš  —  | j                   d|||||||||	||ddœ|¤Ž}|j                  }t        |t        «      rt	        | d«      n|}| j                  |dd…|dd…f   «      }d}|
�* | j                  d||
| j                  j                  dœ|¤Ž}t        |||j                  |j                  |j                  |j                  ¬«      S )aC  
        image_encoder_embeddings (`torch.FloatTensor`, *optional*):
            The output of the image encoder.
        perceiver_embeddings (`torch.FloatTensor`, *optional*):
            The output of the perceiver resampler.
        image_attention_mask (`torch.LongTensor`, *optional*):
            The attention mask for the image encoder.
        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 AutoProcessor, IdeficsForVisionText2Text

        >>> model = IdeficsForVisionText2Text.from_pretrained("HuggingFaceM4/idefics-9b")
        >>> processor = AutoProcessor.from_pretrained("HuggingFaceM4/idefics-9b")

        >>> dogs_image_url_1 = "https://huggingface.co/datasets/hf-internal-testing/fixtures_nlvr2/raw/main/image1.jpeg"
        >>> dogs_image_url_2 = "https://huggingface.co/datasets/hf-internal-testing/fixtures_nlvr2/raw/main/image2.jpeg"

        >>> prompts = [
        ...     [
        ...         "User:",
        ...         dogs_image_url_1,
        ...         "Describe this image.\nAssistant: An image of two dogs.\n",
        ...         "User:",
        ...         dogs_image_url_2,
        ...         "Describe this image.\nAssistant:",
        ...     ]
        ... ]
        >>> inputs = processor(prompts, return_tensors="pt")
        >>> generate_ids = model.generate(**inputs, max_new_tokens=6)
        >>> processor.batch_decode(generate_ids, skip_special_tokens=True)
        ```T)rG   r>   rÔ   r$   r‰  r9   r:   r;   r<   rŠ  r‹  Úreturn_dictN)r6   rª  ri  )r5   r6   r$   r%   r&   r'   r0   )r\   r#   rT   r–   Úslicer¨  Úloss_functionrú   ri  r4   r$   r%   r&   r'   )rq   rG   r>   rÔ   r$   r‰  r9   r:   r;   r<   rª  rŠ  r‹  r«  rr   Úoutputsr%   Úslice_indicesr6   r5   s                       r2   r}   z!IdeficsForVisionText2Text.forwardS  sõ   € ðp 3=°$·*±*ð 3
ØØ)Ø%Ø+Ø'Ø%Ø%=Ø!5Ø!5ØØ%=Øñ3
ð ñ3
ˆð   ×1Ñ1ˆä8BÀ>ÔSVÔ8Wœ˜~˜o¨tÔ4Ð]kˆØ—‘˜mªA¨}ºaÐ,?Ñ@ÓAˆàˆØÐØ%�4×%Ñ%Ðp¨V¸FÈtÏ{É{×OeÑOeÑpÐioÑpˆDä,ØØØ#×3Ñ3Ø!×/Ñ/Ø×)Ñ)Ø '× ;Ñ ;ô
ð 	
r1   c
           
      ó  •— i }|�"| j                   j                  r||d<   n||d<   n||d<   |
j                  dd«      |d<   t        ‰| �  |f|||||	|dœ|¤|
¤Ž}|�#|€!|d   j
                  d   }|d d …| d …f   |d	<   |S )
Nr;   r:   r9   r‹  F)r$   r>   r‰  rÔ   rŠ  r<   rG   r   r<   )rú   ro  Úpoprl   Úprepare_inputs_for_generationr@   )rq   rG   r>   rÔ   r‰  r$   r9   r'   r<   rŠ  rr   Úimages_kwargsÚmodel_inputsr–  rs   s                 €r2   r´  z7IdeficsForVisionText2Text.prepare_inputs_for_generation­  sÒ   ø€ ð ˆØÐ*Ø�{‰{×(Ò(Ø8K�Ð4Ò5à<O�Ð8Ò9à,8ˆM˜.Ñ)Ø4:·J±JÐ?YÐ[`Ó4aˆÐ0Ñ1ä‘wÑ<Øð

à+Ø)Ø'Ø%ØØ!5ñ

ð ð

ð ñ

ˆð  Ð+°Ð0EØ% kÑ2×8Ñ8¸Ñ;ˆJØ3GÊÈJÈ;É<ÈÑ3XˆLÐ/Ñ0àÐr1   r°  rK   rI   c                 óú   •— t        ‰| �  |||fi |¤Ž}d|v rT|d   }|d d …dd d …f   j                  d«      }|j                  dd«      r||d<   nt	        j
                  ||gd¬«      |d<   |j                  |d<   |S )Nr<   r8   r   rŠ  TrÀ   r'   )rl   Ú#_update_model_kwargs_for_generationrÑ   rF   r,   r‘   r'   )rq   r°  rK   rI   rr   r<   Ú	last_maskrs   s          €r2   r¸  z=IdeficsForVisionText2Text._update_model_kwargs_for_generationØ  sª   ø€ ô ‘wÑBØØØñ
ð ñ	
ˆð " \Ñ1Ø#/Ð0FÑ#GÐ Ø,ªQ°²A¨XÑ6×@Ñ@ÀÓCˆIØ×Ñ ¨TÔ2Ø7@�Ð3Ò4ä7<·y±yÐBVÐXaÐAbÐhiÔ7j�Ð3Ñ4ð /6×.IÑ.IˆÐ*Ñ+ØÐr1   rS   )NNNNNNNNNNNFr   )NNNNNNNN)F)r(   r)   r*   r©  rm   r   r   r,   r&  r—   r
   r-   r‚   r–   r   r   r/   r4   r}   r´  r   Údictrƒ   r   r¸  r„   r…   s   @r2   r£  r£  <  sä  ø„ Ø*Ð,GÐHÐõð( Øð .2Ø.2Ø04Ø(,Ø26Ø15Ø=AØ9=Ø48Ø*.Ø!%Ø05Ø-.ñV
à×#Ñ# dÑ*ðV
ð Ÿ™ tÑ+ðV
ð ×&Ñ&¨Ñ-ð	V
ð
  ™ðV
ð ×(Ñ(¨4Ñ/ðV
ð ×'Ñ'¨$Ñ.ðV
ð #(×"3Ñ"3°dÑ":ðV
ð $×/Ñ/°$Ñ6ðV
ð $Ÿl™l¨TÑ1ðV
ð × Ñ  4Ñ'ðV
ð ˜$‘;ðV
ð #'¨¡+ðV
ð ˜eŸl™lÑ*ðV
ð Ð+Ñ,ðV
ð  
Ð.Ñ	.ò!V
ó ó ðV
ðv ØØØØØ Ø!Øõ)ð^ $)ñ	àðð ˜3 ˜8‘nðð !ð	ð 
ˆc�3ˆh‰÷ñ r1   r£  )r£  rf  rK  )r   FNNr¡  )r   )r  )Nr+   Úcollections.abcr   Údataclassesr   Útypingr   r,   Útorch.nn.functionalr   rð   ru   Ú r   rS  Úactivationsr	   Úcache_utilsr
   r   Ú
generationr   Úmasking_utilsr   Úmodeling_layersr   Úmodeling_outputsr   Úmodeling_utilsr   r   r   Úprocessing_utilsr   Úutilsr   r   r   r   Úutils.genericr   Úutils.output_capturingr   r   Úconfiguration_ideficsr   Ú	perceiverr   Úvisionr   r   Ú
get_loggerr(   r  r"   r4   rM   ra   rQ   rc   rP   r‡   ÚModuler™   r¬   rÏ   rØ   rÚ   r—   rº   rõ   r÷   r(  r4  rK  rf  r£  Ú__all__r0   r1   r2   ú<module>rÑ     s:  ðñ& å $Ý !Ý ã ß Ð Ý å &Ý !ß .Ý )Ý /Ý 9Ý +ß XÑ XÝ &ß RÓ RÝ 7ß EÝ 0Ý 0ß Eð 
ˆ×	Ñ	˜HÓ	%€ñ ðôð
 ô@ [ó @ó óð@ñ* ðôð
 ô@ Kó @ó óð@ð8 ØØØó*#óZôfA §¡ô fAôR8S˜RŸY™Yô 8SôxJ�R—Y‘Yô Jô0$
�u—x‘x—‘ô $
òN(óô:P�—‘ô Pð2 ñ%Ø�I‰Ið%à�<‰<ð%ð 
�‰ð%ð �<‰<ð	%ð
 —L‘L 4Ñ'ð%ð ð%ð ó%ô0R)�r—y‘yô R)ôl3Ð4ô 3ôl|Ð&@ô |ð~ ô.5˜_ó .5ó ð.5ðb ôa
Ð)ó a
ó ða
ôHtÐ 6¸ô tòn R�r1   