§
    ‚ŠtjÀ  ã                   ó4  — d Z ddlmZ ddl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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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* ddl+m,Z,m-Z-m.Z.  e$j/        e0¦  «        Z1 e"d¬¦  «        e G d„ de¦  «        ¦   «         ¦   «         Z2 e"d¬¦  «        e G d„ de¦  «        ¦   «         ¦   «         Z3 G d„ d ej4        ¦  «        Z5	 dRd"ej4        d#ej6        d$ej6        d%ej6        d&ej6        dz  d'e7d(e7d)ee!         fd*„Z8 G d+„ d,ej4        ¦  «        Z9 G d-„ d.ej4        ¦  «        Z: G d/„ d0ej4        ¦  «        Z; G d1„ d2ej4        ¦  «        Z< G d3„ d4e¦  «        Z= G d5„ d6ej4        ¦  «        Z>e" G d7„ d8e¦  «        ¦   «         Z? e"d9¬¦  «         G d:„ d;e?¦  «        ¦   «         Z@d<ej6        d=eAd>ej6        fd?„ZB G d@„ dAej4        ¦  «        ZC G dB„ dCej4        ¦  «        ZD G dD„ dEej4        ¦  «        ZE e"dF¬¦  «         G dG„ dHe?¦  «        ¦   «         ZF G dI„ dJej4        ¦  «        ZG e"dK¬¦  «         G dL„ dMe?¦  «        ¦   «         ZH e"dN¬¦  «         G dO„ dPe?e¦  «        ¦   «         ZIg dQ¢ZJdS )SzPyTorch Idefics2 model.é    )ÚCallable)Ú	dataclassN)Únné   )Úinitialization)ÚACT2FN)ÚCacheÚDynamicCache)ÚGenerationMixin)Úcreate_bidirectional_mask)ÚFlashAttentionKwargs)ÚGradientCheckpointingLayer)ÚBaseModelOutputÚBaseModelOutputWithPoolingÚModelOutput)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedModel)ÚUnpack)ÚTransformersKwargsÚauto_docstringÚcan_return_tupleÚlogging)Úmerge_with_config_defaults)Úcapture_outputsé   )Ú	AutoModelé   )ÚIdefics2ConfigÚIdefics2PerceiverConfigÚIdefics2VisionConfigz|
    Base class for Idefics2 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<   dS )ÚIdefics2BaseModelOutputWithPastaƒ  
    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)   © ó    úl/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/idefics2/modeling_idefics2.pyr$   r$   *   s£   € € € € € € ð	ð 	ð 37Ð�uÔ(¨4Ñ/Ð6Ð6Ñ6Ø$(€O�U˜T‘\Ð(Ð(Ñ(Ø59€M�5˜Ô*Ô+¨dÑ2Ð9Ð9Ñ9Ø26€J��eÔ'Ô(¨4Ñ/Ð6Ð6Ñ6Ø;?Ð˜˜uÔ0Ô1°DÑ8Ð?Ð?Ñ?Ð?Ð?r3   r$   zT
    Base class for Idefics2 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<   dS )	ÚIdefics2CausalLMOutputWithPastae  
    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-   r7   r.   r/   r0   r8   r&   r	   r'   r1   r(   r)   r2   r3   r4   r6   r6   C   sº   € € € € € € ðð ð" &*€Dˆ%Ô
˜dÑ
"Ð)Ð)Ñ)Ø'+€FˆEÔ Ñ$Ð+Ð+Ñ+Ø$(€O�U˜T‘\Ð(Ð(Ñ(Ø59€M�5˜Ô*Ô+¨dÑ2Ð9Ð9Ñ9Ø26€J��eÔ'Ô(¨4Ñ/Ð6Ð6Ñ6Ø;?Ð˜˜uÔ0Ô1°DÑ8Ð?Ð?Ñ?Ð?Ð?r3   r6   c                   óZ   ‡ — e Zd ZdZdefˆ fd„Zdej        dej        dej	        fd„Z
ˆ xZS )ÚIdefics2VisionEmbeddingsaX  
    This is a modified version of `siglip.modelign_siglip.SiglipVisionEmbeddings` to enable images of variable
    resolution.

    The modifications are adapted from [Patch n' Pack: NaViT, a Vision Transformer for any Aspect Ratio and Resolution](https://huggingface.co/papers/2307.06304)
    which allows treating images in their native aspect ratio and without the need to resize them to the same
    fixed size. In particular, we start from the original pre-trained SigLIP model
    (which uses images of fixed-size square images) and adapt it by training on images of variable resolutions.
    Úconfigc                 óš  •— t          ¦   «                              ¦   «          |j        | _        |j        | _        |j        | _        t          j        |j        | j        | j        | j        d¬¦  «        | _	        | j        | j        z  | _
        | j
        dz  | _        | j        | _        t          j        | j        | j        ¦  «        | _        d S )NÚvalid)Úin_channelsÚout_channelsÚkernel_sizeÚstrideÚpaddingr   )ÚsuperÚ__init__Úhidden_sizeÚ	embed_dimÚ
image_sizeÚ
patch_sizer   ÚConv2dÚnum_channelsÚpatch_embeddingÚnum_patches_per_sideÚnum_patchesÚnum_positionsÚ	EmbeddingÚposition_embedding©Úselfr;   Ú	__class__s     €r4   rD   z!Idefics2VisionEmbeddings.__init__o   sµ   ø€ Ý‰Œ×ÒÑÔÐØÔ+ˆŒØ Ô+ˆŒØ Ô+ˆŒå!œyØÔ+ØœØœØ”?Øð 
ñ  
ô  
ˆÔð %)¤O°t´Ñ$FˆÔ!ØÔ4°aÑ7ˆÔØ!Ô-ˆÔÝ"$¤,¨tÔ/AÀ4Ä>Ñ"RÔ"RˆÔÐÐr3   Úpixel_valuesÚpatch_attention_maskÚreturnc                 óz  — |j         \  }}}}|                      |¦  «        }|                     d¦  «                             dd¦  «        }|| j        z  || j        z  }
}	t          j        d| j        z  dd| j        z  |j        ¬¦  «        }t          j	        ||	|
z  fd|j        ¬¦  «        }|d d …d d …df          
                    d¬¦  «        }|d d …dd d …f          
                    d¬¦  «        }d|z  }d|z  }|                     d¦  «        }|                     d¦  «        }t          j        ||j        t
          j        ¬¦  «        }t          j        ||j        t
          j        ¬¦  «        }|d d d …f         |d d …d f         z  }|d d d …f         |d d …d f         z  }t          j        |d	¬
¦  «        }t          j        |d	¬
¦  «        }|                     |j        ¦  «        }|                     |j        ¦  «        }t          j        ||d¬¦  «        }t          j        ||d¬¦  «        }|d d …d d …d f         | j        z  |d d …d d d …f         z   }|                     |d¦  «        }||                     |d¦  «                 ||                     |d¦  «        <   ||                      |¦  «        z   }|S )Nr   r   g      ð?)Údevicer   )ÚsizeÚ
fill_valuerX   ©Údim)rX   Údtypegé!çýÿï?)ÚmaxT)Úrightéÿÿÿÿ)ÚshaperK   ÚflattenÚ	transposerH   r.   ÚarangerL   rX   ÚfullÚsumrY   Úfloat32ÚclampÚtor]   Ú	bucketizeÚreshapeÚviewrP   )rR   rT   rU   Ú
batch_sizeÚ_Úmax_im_hÚmax_im_wÚpatch_embedsÚ
embeddingsÚmax_nb_patches_hÚmax_nb_patches_wÚ
boundariesÚposition_idsÚnb_patches_hÚnb_patches_wÚstep_hÚstep_wÚmax_patches_hÚmax_patches_wÚ	h_indicesÚ	w_indicesÚfractional_coords_hÚfractional_coords_wÚbucket_coords_hÚbucket_coords_wÚpos_idss                             r4   Úforwardz Idefics2VisionEmbeddings.forward‚   s  € Ø,8Ô,>Ñ)ˆ
�A�x à×+Ò+¨LÑ9Ô9ˆØ!×)Ò)¨!Ñ,Ô,×6Ò6°q¸!Ñ<Ô<ˆ
à-5¸¼Ñ-HÈ(ÐVZÔVeÑJeÐ*ÐÝ”\Ø�Ô)Ñ)¨3°°DÔ4MÑ0MÐVbÔVið
ñ 
ô 
ˆ
õ ”zØÐ.Ð1AÑAÐBÈqÐYeÔYlð
ñ 
ô 
ˆð ,¨A¨A¨A¨q¨q¨q°!¨GÔ4×8Ò8¸QÐ8Ñ?Ô?ˆØ+¨A¨A¨A¨q°!°!°!¨GÔ4×8Ò8¸QÐ8Ñ?Ô?ˆà�|Ñ#ˆØ�|Ñ#ˆà,×1Ò1°!Ñ4Ô4ˆØ,×1Ò1°!Ñ4Ô4ˆÝ”L °|Ô7JÕRWÔR_Ð`Ñ`Ô`ˆ	Ý”L °|Ô7JÕRWÔR_Ð`Ñ`Ô`ˆ	à'¨¨a¨a¨a¨Ô0°6¸!¸!¸!¸T¸'´?ÑBÐØ'¨¨a¨a¨a¨Ô0°6¸!¸!¸!¸T¸'´?ÑBÐå#œkÐ*=ÀJÐPÑPÔPÐÝ#œkÐ*=ÀJÐPÑPÔPÐà1×4Ò4°\Ô5GÑHÔHÐØ1×4Ò4°\Ô5GÑHÔHÐåœ/Ð*=¸zÐQUÐVÑVÔVˆÝœ/Ð*=¸zÐQUÐVÑVÔVˆà! ! ! ! Q Q Q¨ *Ô-°Ô0IÑIÈOÐ\]Ð\]Ð\]Ð_cÐefÐefÐefÐ\fÔLgÑgˆØ—/’/ *¨bÑ1Ô1ˆàBIÐJ^×JcÒJcÐdnÐprÑJsÔJsÔBtˆÐ)×.Ò.¨z¸2Ñ>Ô>Ñ?à $×"9Ò"9¸,Ñ"GÔ"GÑGˆ
ØÐr3   )r*   r+   r,   r-   r    rD   r.   r/   Ú
BoolTensorÚTensorr„   Ú__classcell__©rS   s   @r4   r:   r:   d   s‰   ø€ € € € € ðð ðSÐ3ð Sð Sð Sð Sð Sð Sð&+ EÔ$5ð +ÈUÔM]ð +ÐbgÔbnð +ð +ð +ð +ð +ð +ð +ð +r3   r:   ç        ÚmoduleÚqueryÚkeyÚvalueÚattention_maskÚscalingÚdropoutÚkwargsc                 ó4  — t          | d¦  «        r*t          || j        ¦  «        }t          || j        ¦  «        }t          j        ||                     dd¦  «        ¦  «        |z  }|�||z   }t          j                             |dt          j	        ¬¦  «         
                    |j        ¦  «        }t          j                             ||| j        ¬¦  «        }t          j        ||¦  «        }	|	                     dd¦  «                             ¦   «         }	|	|fS )NÚnum_key_value_groupsr   r   r`   )r\   r]   )ÚpÚtrainingr   )ÚhasattrÚ	repeat_kvr“   r.   Úmatmulrc   r   Ú
functionalÚsoftmaxrg   ri   r]   r�   r•   Ú
contiguous)
rŠ   r‹   rŒ   r�   rŽ   r�   r�   r‘   Úattn_weightsÚattn_outputs
             r4   Úeager_attention_forwardrž   °   sú   € õ ˆvÐ-Ñ.Ô.ð >Ý˜˜VÔ8Ñ9Ô9ˆÝ˜% Ô!<Ñ=Ô=ˆå”<  s§}¢}°Q¸Ñ':Ô':Ñ;Ô;¸gÑE€LØÐ!Ø# nÑ4ˆå”=×(Ò(¨¸2ÅUÄ]Ð(ÑSÔS×VÒVÐW\ÔWbÑcÔc€LÝ”=×(Ò(¨¸È6Ì?Ð(Ñ[Ô[€LÝ”,˜|¨UÑ3Ô3€KØ×'Ò'¨¨1Ñ-Ô-×8Ò8Ñ:Ô:€Kà˜Ð$Ð$r3   c            
       ó~   ‡ — e Zd ZdZˆ fd„Z	 ddej        dej        dz  deej        ej        dz  f         fd„Zˆ xZ	S )	ÚIdefics2VisionAttentionz=Multi-headed attention from 'Attention Is All You Need' paperc                 ó‚  •— t          ¦   «                              ¦   «          || _        |j        | _        |j        | _        | j        | j        z  | _        | j        | j        z  | j        k    r t          d| j        › d| j        › d�¦  «        ‚| j        dz  | _	        |j
        | _        t          j        | j        | j        ¦  «        | _        t          j        | j        | j        ¦  «        | _        t          j        | j        | j        ¦  «        | _        t          j        | j        | j        ¦  «        | _        d| _        d S )Nz;embed_dim must be divisible by num_heads (got `embed_dim`: z and `num_heads`: z).ç      à¿F)rC   rD   r;   rE   rF   Únum_attention_headsÚ	num_headsÚhead_dimÚ
ValueErrorÚscaleÚattention_dropoutr�   r   ÚLinearÚk_projÚv_projÚq_projÚout_projÚ	is_causalrQ   s     €r4   rD   z Idefics2VisionAttention.__init__Ï   s  ø€ Ý‰Œ×ÒÑÔÐØˆŒØÔ+ˆŒØÔ3ˆŒØœ¨$¬.Ñ8ˆŒØŒ=˜4œ>Ñ)¨T¬^Ò;Ð;Ýð'ÈdÌnð 'ð 'Ø”Nð'ð 'ð 'ñô ð ð ”] DÑ(ˆŒ
ØÔ/ˆŒå”i ¤°´Ñ?Ô?ˆŒÝ”i ¤°´Ñ?Ô?ˆŒÝ”i ¤°´Ñ?Ô?ˆŒÝœ	 $¤.°$´.ÑAÔAˆŒð ˆŒˆˆr3   Nr'   rŽ   rV   c           
      ó¼  — |j         dd…         }g |¢d‘| j        ‘R }|                      |¦  «                             |¦  «                             dd¦  «        }|                      |¦  «                             |¦  «                             dd¦  «        }|                      |¦  «                             |¦  «                             dd¦  «        }t          j        | j	        j
        t          ¦  «        }	 |	| ||||| j        | j        | j        sdn| j        ¬¦  «        \  }
} |
j        g |¢d‘R Ž                      ¦   «         }
|                      |
¦  «        }
|
|fS )z#Input shape: Batch x Time x ChannelNr`   r   r   r‰   ©r®   r�   r�   )ra   r¥   r¬   rl   rc   rª   r«   r   Úget_interfacer;   Ú_attn_implementationrž   r®   r§   r•   r�   rk   r›   r­   )rR   r'   rŽ   r‘   Úinput_shapeÚhidden_shapeÚqueriesÚkeysÚvaluesÚattention_interfacer�   rœ   s               r4   r„   zIdefics2VisionAttention.forwardå   sg  € ð $Ô)¨#¨2¨#Ô.ˆà8˜Ð8 bÐ8¨$¬-Ð8Ð8ˆØ—+’+˜mÑ,Ô,×1Ò1°,Ñ?Ô?×IÒIÈ!ÈQÑOÔOˆØ�{Š{˜=Ñ)Ô)×.Ò.¨|Ñ<Ô<×FÒFÀqÈ!ÑLÔLˆØ—’˜]Ñ+Ô+×0Ò0°Ñ>Ô>×HÒHÈÈAÑNÔNˆå(?Ô(MØŒKÔ,Õ.Eñ)
ô )
Ðð %8Ð$7ØØØØØØ”nØ”JØ#œ}Ð>�C�C°$´,ð	%
ñ 	%
ô 	%
Ñ!ˆ�\ð *�kÔ)Ð;¨;Ð;¸Ð;Ð;Ð;×FÒFÑHÔHˆØ—m’m KÑ0Ô0ˆà˜LÐ(Ð(r3   ©N)
r*   r+   r,   r-   rD   r.   r†   r1   r„   r‡   rˆ   s   @r4   r    r    Ë   s”   ø€ € € € € ØGÐGðð ð ð ð ð2 /3ð!)ð !)à”|ð!)ð œ tÑ+ð!)ð
 
ˆuŒ|˜Uœ\¨DÑ0Ð0Ô	1ð!)ð !)ð !)ð !)ð !)ð !)ð !)ð !)r3   r    c                   óB   ‡ — e Zd Zˆ fd„Zdej        dej        fd„Zˆ xZS )ÚIdefics2VisionMLPc                 ó  •— t          ¦   «                              ¦   «          || _        t          |j                 | _        t          j        |j        |j	        ¦  «        | _
        t          j        |j	        |j        ¦  «        | _        d S r¹   )rC   rD   r;   r   Ú
hidden_actÚactivation_fnr   r©   rE   Úintermediate_sizeÚfc1Úfc2rQ   s     €r4   rD   zIdefics2VisionMLP.__init__  sf   ø€ Ý‰Œ×ÒÑÔÐØˆŒÝ# FÔ$5Ô6ˆÔÝ”9˜VÔ/°Ô1IÑJÔJˆŒÝ”9˜VÔ5°vÔ7IÑJÔJˆŒˆˆr3   r'   rV   c                 ó„   — |                       |¦  «        }|                      |¦  «        }|                      |¦  «        }|S r¹   )rÀ   r¾   rÁ   )rR   r'   s     r4   r„   zIdefics2VisionMLP.forward  s=   € ØŸš Ñ/Ô/ˆØ×*Ò*¨=Ñ9Ô9ˆØŸš Ñ/Ô/ˆØÐr3   )r*   r+   r,   rD   r.   r†   r„   r‡   rˆ   s   @r4   r»   r»   
  sc   ø€ € € € € ðKð Kð Kð Kð Kð U¤\ð °e´lð ð ð ð ð ð ð ð r3   r»   c                   ó6   ‡ — e Zd Zdedededefˆ fd„Zd„ Zˆ xZS )ÚIdefics2MLPrE   r¿   Úoutput_sizer½   c                 ó  •— t          ¦   «                              ¦   «          t          j        ||d¬¦  «        | _        t          j        ||d¬¦  «        | _        t          j        ||d¬¦  «        | _        t          |         | _        d S ©NF©Úbias)	rC   rD   r   r©   Ú	gate_projÚup_projÚ	down_projr   Úact_fn)rR   rE   r¿   rÅ   r½   rS   s        €r4   rD   zIdefics2MLP.__init__  sv   ø€ õ 	‰Œ×ÒÑÔÐÝœ ;Ð0AÈÐNÑNÔNˆŒÝ”y Ð.?ÀeÐLÑLÔLˆŒÝœÐ#4°kÈÐNÑNÔNˆŒÝ˜ZÔ(ˆŒˆˆr3   c                 ó¤   — |                       |                      |                      |¦  «        ¦  «        |                      |¦  «        z  ¦  «        S r¹   )rÌ   rÍ   rÊ   rË   )rR   Úxs     r4   r„   zIdefics2MLP.forward'  s;   € Ø�~Š~˜dŸkšk¨$¯.ª.¸Ñ*;Ô*;Ñ<Ô<¸t¿|º|ÈA¹¼ÑNÑOÔOÐOr3   )r*   r+   r,   ÚintÚstrrD   r„   r‡   rˆ   s   @r4   rÄ   rÄ     sy   ø€ € € € € ð)àð)ð ð)ð ð	)ð
 ð)ð )ð )ð )ð )ð )ðPð Pð Pð Pð Pð Pð Pr3   rÄ   c                   ó.   ‡ — e Zd ZdZdefˆ fd„Zd„ Zˆ xZS )Ú%Idefics2MultiheadAttentionPoolingHeadzMultihead Attention Pooling.r;   c                 ó°  •— t          ¦   «                              ¦   «          t          j        t	          j        dd|j        ¦  «        ¦  «        | _        t          j                             |j        |j	        d¬¦  «        | _
        t          j        |j        |j        ¬¦  «        | _        t          |j        |j        |j        |j        ¬¦  «        | _        d S )Nr   T)Úbatch_first©Úeps)rE   r¿   r½   rÅ   )rC   rD   r   Ú	Parameterr.   ÚrandnrE   ÚprobeÚMultiheadAttentionr£   Ú	attentionÚ	LayerNormÚlayer_norm_epsÚ	layernormrÄ   r¿   r½   ÚmlprQ   s     €r4   rD   z.Idefics2MultiheadAttentionPoolingHead.__init__/  s®   ø€ Ý‰Œ×ÒÑÔÐå”\¥%¤+¨a°°FÔ4FÑ"GÔ"GÑHÔHˆŒ
Ýœ×4Ò4°VÔ5GÈÔIcÐquÐ4ÑvÔvˆŒÝœ fÔ&8¸fÔ>SÐTÑTÔTˆŒåØÔ*Ø$Ô6ØÔ(ØÔ*ð	
ñ 
ô 
ˆŒˆˆr3   c                 ó  — |j         d         }| j                             |dd¦  «        }|                      |||¦  «        d         }|}|                      |¦  «        }||                      |¦  «        z   }|d d …df         S )Nr   r   )ra   rÚ   ÚrepeatrÜ   rß   rà   )rR   Úhidden_staterm   rÚ   Úresiduals        r4   r„   z-Idefics2MultiheadAttentionPoolingHead.forward=  s�   € Ø!Ô'¨Ô*ˆ
Ø”
×!Ò! *¨a°Ñ3Ô3ˆà—~’~ e¨\¸<ÑHÔHÈÔKˆàˆØ—~’~ lÑ3Ô3ˆØ $§(¢(¨<Ñ"8Ô"8Ñ8ˆà˜A˜A˜A˜q˜DÔ!Ð!r3   )r*   r+   r,   r-   r    rD   r„   r‡   rˆ   s   @r4   rÓ   rÓ   ,  sZ   ø€ € € € € Ø&Ð&ð
Ð3ð 
ð 
ð 
ð 
ð 
ð 
ð
"ð 
"ð 
"ð 
"ð 
"ð 
"ð 
"r3   rÓ   c            	       óv   ‡ — e Zd Zdefˆ fd„Zedej        dej        dee	         dej
        fd„¦   «         Zˆ xZS )ÚIdefics2EncoderLayerr;   c                 óD  •— t          ¦   «                              ¦   «          |j        | _        t	          |¦  «        | _        t          j        | j        |j        ¬¦  «        | _	        t          |¦  «        | _        t          j        | j        |j        ¬¦  «        | _        d S ©NrÖ   )rC   rD   rE   rF   r    Ú	self_attnr   rÝ   rÞ   Úlayer_norm1r»   rà   Úlayer_norm2rQ   s     €r4   rD   zIdefics2EncoderLayer.__init__K  s   ø€ Ý‰Œ×ÒÑÔÐØÔ+ˆŒÝ0°Ñ8Ô8ˆŒÝœ<¨¬¸FÔ<QÐRÑRÔRˆÔÝ$ VÑ,Ô,ˆŒÝœ<¨¬¸FÔ<QÐRÑRÔRˆÔÐÐr3   r'   rŽ   r‘   rV   c                 óÆ   — |}|                       |¦  «        } | j        d||dœ|¤Ž\  }}||z   }|}|                      |¦  «        }|                      |¦  «        }||z   }|S )N)r'   rŽ   r2   )rê   ré   rë   rà   )rR   r'   rŽ   r‘   rä   rn   s         r4   r„   zIdefics2EncoderLayer.forwardS  s“   € ð !ˆà×(Ò(¨Ñ7Ô7ˆØ)˜4œ>ð 
Ø'Ø)ð
ð 
ð ð
ð 
Ñˆ�qð
 ! =Ñ0ˆà ˆØ×(Ò(¨Ñ7Ô7ˆØŸš Ñ/Ô/ˆØ  =Ñ0ˆàÐr3   )r*   r+   r,   r    rD   r   r.   r†   r   r   r/   r„   r‡   rˆ   s   @r4   ræ   ræ   J  sŸ   ø€ € € € € ðSÐ3ð Sð Sð Sð Sð Sð Sð ðà”|ðð œðð Ð+Ô,ð	ð
 
Ô	ðð ð ñ „^ðð ð ð ð r3   ræ   c                   ól   ‡ — e Zd ZdZdefˆ fd„Ze	 d	dej        dz  de	e
         defd„¦   «         Zˆ xZS )
ÚIdefics2Encoderzµ
    Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a
    [`Idefics2EncoderLayer`].

    Args:
        config: Idefics2Config
    r;   c                 óÔ   •‡— t          ¦   «                              ¦   «          ‰| _        t          j        ˆfd„t          ‰j        ¦  «        D ¦   «         ¦  «        | _        d| _        d S )Nc                 ó.   •— g | ]}t          ‰¦  «        ‘ŒS r2   )ræ   )Ú.0rn   r;   s     €r4   ú
<listcomp>z,Idefics2Encoder.__init__.<locals>.<listcomp>z  s"   ø€ Ð$kÐ$kÐ$kÀaÕ%9¸&Ñ%AÔ%AÐ$kÐ$kÐ$kr3   F)	rC   rD   r;   r   Ú
ModuleListÚrangeÚnum_hidden_layersÚlayersÚgradient_checkpointingrQ   s    `€r4   rD   zIdefics2Encoder.__init__w  sa   øø€ Ý‰Œ×ÒÑÔÐØˆŒÝ”mÐ$kÐ$kÐ$kÐ$kÍ5ÐQWÔQiÑKjÔKjÐ$kÑ$kÔ$kÑlÔlˆŒØ&+ˆÔ#Ð#Ð#r3   NrŽ   r‘   rV   c                 óN   — |}| j         D ]} |||fi |¤Ž}Œt          |¬¦  «        S )N©r%   )rö   r   )rR   Úinputs_embedsrŽ   r‘   r'   Úencoder_layers         r4   r„   zIdefics2Encoder.forward~  sU   € ð &ˆØ!œ[ð 	ð 	ˆMØ)˜MØØðð ð ðð ˆMˆMõ °Ð?Ñ?Ô?Ð?r3   r¹   )r*   r+   r,   r-   r   rD   r   r.   r†   r   r   r   r„   r‡   rˆ   s   @r4   rî   rî   n  s¯   ø€ € € € € ðð ð,˜~ð ,ð ,ð ,ð ,ð ,ð ,ð ð /3ð@ð @ð œ tÑ+ð@ð Ð+Ô,ð	@ð
 
ð@ð @ð @ñ „^ð@ð @ð @ð @ð @r3   rî   c                   ó~   ‡ — e Zd ZU eed<   dZdZdZg d¢ZdgZ	dZ
dZdZdZ ej        ¦   «         ˆ fd„¦   «         Zˆ xZS )ÚIdefics2PreTrainedModelr;   Úmodel)ÚimageÚtextT)r    rÄ   ÚIdefics2PerceiverLayerÚIdefics2DecoderLayerr&   c                 ó
  •— t          ¦   «                              |¦  «         t          |t          ¦  «        rt	          j        |j        ¦  «         d S t          |t          ¦  «        rt	          j        |j	        ¦  «         d S d S r¹   )
rC   Ú_init_weightsÚ
isinstancerÓ   ÚinitÚnormal_rÚ   ÚIdefics2PerceiverResamplerÚones_Úlatents)rR   rŠ   rS   s     €r4   r  z%Idefics2PreTrainedModel._init_weightsž  s{   ø€ å‰Œ×Ò˜fÑ%Ô%Ð%Ý�fÕCÑDÔDð 	'ÝŒL˜œÑ&Ô&Ð&Ð&Ð&Ý˜Õ :Ñ;Ô;ð 	'ÝŒJ�v”~Ñ&Ô&Ð&Ð&Ð&ð	'ð 	'r3   )r*   r+   r,   r   r0   Úbase_model_prefixÚinput_modalitiesÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_skip_keys_device_placementÚ_supports_flash_attnÚ_supports_sdpaÚ_supports_flex_attnÚ_supports_attention_backendr.   Úno_gradr  r‡   rˆ   s   @r4   rý   rý   �  s–   ø€ € € € € € àÐÐÑØÐØ(ÐØ&*Ð#ØtÐtÐtÐØ#4Ð"5ÐØÐØ€NØÐà"&Ðà€U„]�_„_ð'ð 'ð 'ð 'ñ „_ð'ð 'ð 'ð 'ð 'r3   rý   zK
    Idefics2 vision encoder model that returnss raw image embeddings.
    c                   óÈ   ‡ — e Zd ZU eed<   dZeedœZdefˆ fd„Z	d„ Z
d„ Ze ed¬¦  «        e	 dd
ej        d	z  dee         deez  fd„¦   «         ¦   «         ¦   «         Zˆ xZS )ÚIdefics2VisionTransformerr;   ©rÿ   )r'   r(   c                 ó  •— t          ¦   «                              |¦  «         |j        }|| _        t	          |¦  «        | _        t          |¦  «        | _        t          j	        ||j
        ¬¦  «        | _        |                      ¦   «          d S rè   )rC   rD   rE   r;   r:   rr   rî   Úencoderr   rÝ   rÞ   Úpost_layernormÚ	post_init)rR   r;   rF   rS   s      €r4   rD   z"Idefics2VisionTransformer.__init__´  sx   ø€ Ý‰Œ×Ò˜Ñ Ô Ð ØÔ&ˆ	àˆŒÝ2°6Ñ:Ô:ˆŒÝ& vÑ.Ô.ˆŒÝ œl¨9¸&Ô:OÐPÑPÔPˆÔà�ŠÑÔÐÐÐr3   c                 ó   — | j         S r¹   ©rr   ©rR   s    r4   Úget_input_embeddingsz.Idefics2VisionTransformer.get_input_embeddings¿  s
   € ØŒÐr3   c                 ó   — || _         d S r¹   r  ©rR   r�   s     r4   Úset_input_embeddingsz.Idefics2VisionTransformer.set_input_embeddingsÂ  s   € ØˆŒˆˆr3   F)Útie_last_hidden_statesNrU   r‘   rV   c                 ó  — |                      d¦  «        }|€u| j        j        }t          j        ||                      d¦  «        |z  |                      d¦  «        |z  f¦  «        }|                     t          j        |j        ¬¦  «        }|                      ||¬¦  «        }| 	                    |d¦  «        }t          | j        ||¬¦  «        } | j        d||d	œ|¤Ž}|j        }|                      |¦  «        }t          |¬
¦  «        S )z·
        patch_attention_mask (`torch.BoolTensor` of shape `(batch_size, num_patches_height, num_patches_width)`, *optional*):
            The attention mask for the patches.
        r   Nr   r   ©r]   rX   ©rT   rU   r`   ©r;   rú   rŽ   )rú   rŽ   rù   r2   )rY   r;   rH   r.   Úonesri   ÚboolrX   rr   rl   r   r  r%   r  r   )	rR   rT   rU   r‘   rm   rH   r'   Úencoder_outputsr%   s	            r4   r„   z!Idefics2VisionTransformer.forwardÅ  s;  € ð "×&Ò& qÑ)Ô)ˆ
ØÐ'ØœÔ/ˆJÝ#(¤:àØ ×%Ò% aÑ(Ô(¨JÑ6Ø ×%Ò% aÑ(Ô(¨JÑ6ðñ$ô $Ð ð $8×#:Ò#:ÅÄÐT`ÔTgÐ#:Ñ#hÔ#hÐ àŸš°\ÐXl˜ÑmÔmˆà3×8Ò8¸ÀRÑHÔHÐÝ8Ø”;Ø'Ø/ð 
ñ  
ô  
Ðð ,8¨4¬<ð ,
Ø'Ø/ð,
ð ,
ð ð,
ð ,
ˆð ,Ô=ÐØ ×/Ò/Ð0AÑBÔBÐåÐ1BÐCÑCÔCÐCr3   r¹   )r*   r+   r,   r    r0   r  ræ   r    Ú_can_record_outputsrD   r  r"  r   r   r   r.   r…   r   r   r1   r   r„   r‡   rˆ   s   @r4   r  r  §  s  ø€ € € € € € ð !Ð Ð Ñ Ø!Ðà-Ø-ðð Ðð
	Ð3ð 	ð 	ð 	ð 	ð 	ð 	ðð ð ð ð  ð  ð  Ø€_¨EÐ2Ñ2Ô2Øð 9=ð(Dð (Dð $Ô.°Ñ5ð(Dð Ð+Ô,ð	(Dð
 
�Ñ	 ð(Dð (Dð (Dñ „^ñ 3Ô2ñ  Ôð(Dð (Dð (Dð (Dð (Dr3   r  r'   Ún_reprV   c                 ó¸   — | j         \  }}}}|dk    r| S | dd…dd…ddd…dd…f                              |||||¦  «        } |                      |||z  ||¦  «        S )zÔ
    This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
    num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
    r   N)ra   Úexpandrk   )r'   r,  ÚbatchÚnum_key_value_headsÚslenr¥   s         r4   r—   r—   ô  s„   € ð
 2?Ô1DÑ.€EÐ  hØ�‚z€zØÐØ! ! ! ! Q Q Q¨¨a¨a¨a°°°Ð"2Ô3×:Ò:¸5ÐBUÐW\Ð^bÐdlÑmÔm€MØ× Ò  Ð(;¸eÑ(CÀTÈ8ÑTÔTÐTr3   c                   óT   ‡ — e Zd Zd	deddfˆ fd„Zdej        dej        fd„Zd„ Zˆ xZ	S )
ÚIdefics2RMSNormç�íµ ÷Æ°>r×   rV   Nc                 ó¬   •— t          ¦   «                              ¦   «          t          j        t	          j        |¦  «        ¦  «        | _        || _        dS )z>
        Idefics2RMSNorm is equivalent to T5LayerNorm
        N)rC   rD   r   rØ   r.   r(  ÚweightÚvariance_epsilon)rR   rE   r×   rS   s      €r4   rD   zIdefics2RMSNorm.__init__  sD   ø€ õ 	‰Œ×ÒÑÔÐÝ”l¥5¤:¨kÑ#:Ô#:Ñ;Ô;ˆŒØ #ˆÔÐÐr3   r'   c                 ó  — |j         }|                     t          j        ¦  «        }|                     d¦  «                             dd¬¦  «        }|t          j        || j        z   ¦  «        z  }| j        |                     |¦  «        z  S )Nr   r`   T)Úkeepdim)	r]   ri   r.   rg   ÚpowÚmeanÚrsqrtr7  r6  )rR   r'   Úinput_dtypeÚvariances       r4   r„   zIdefics2RMSNorm.forward
  s|   € Ø#Ô)ˆØ%×(Ò(­¬Ñ7Ô7ˆØ ×$Ò$ QÑ'Ô'×,Ò,¨R¸Ð,Ñ>Ô>ˆØ%­¬°H¸tÔ?TÑ4TÑ(UÔ(UÑUˆØŒ{˜]×-Ò-¨kÑ:Ô:Ñ:Ð:r3   c                 óH   — t          | j        j        ¦  «        › d| j        › �S )Nz, eps=)r1   r6  ra   r7  r  s    r4   Ú
extra_reprzIdefics2RMSNorm.extra_repr  s&   € Ý˜œÔ)Ñ*Ô*ÐIÐI°$Ô2GÐIÐIÐIr3   )r4  )
r*   r+   r,   ÚfloatrD   r.   r†   r„   r@  r‡   rˆ   s   @r4   r3  r3    sŒ   ø€ € € € € ð$ð $¨ð $¸$ð $ð $ð $ð $ð $ð $ð; U¤\ð ;°e´lð ;ð ;ð ;ð ;ðJð Jð Jð Jð Jð Jð Jr3   r3  c                   óÌ   ‡ — e Zd Zddedz  ddfˆ fd„Z	 	 	 ddej        dej        dej        dz  dej        dz  d	edz  d
e	e
         deej        ej        dz  f         fd„Zˆ xZS )ÚIdefics2PerceiverAttentionNÚ	layer_idxrV   c                 ó   •— t          ¦   «                              ¦   «          || _        d| _        |j        | _        |j        | _        |j        | _        |j	        | _	        | j        | j	        z  | _
        |j        | _        | j        dz  | _        t          j        | j        | j        | j        z  d¬¦  «        | _        t          j        | j        | j	        | j        z  d¬¦  «        | _        t          j        | j        | j	        | j        z  d¬¦  «        | _        t          j        | j        | j        z  | j        d¬¦  «        | _        d| _        dS )ziPerceiver Cross-Attention Module --> let long-form inputs be `context`, resampled embeddings be `latents`Nr¢   FrÈ   )rC   rD   r;   rD  rE   Úresampler_n_headsr¤   Úresampler_head_dimr¥   r0  r“   r¨   r�   r   r©   r¬   rª   r«   Úo_projr®   ©rR   r;   rD  rS   s      €r4   rD   z#Idefics2PerceiverAttention.__init__  s  ø€ å‰Œ×ÒÑÔÐØˆŒØˆŒØ!Ô-ˆÔØÔ1ˆŒØÔ1ˆŒØ#)Ô#=ˆÔ Ø$(¤N°dÔ6NÑ$NˆÔ!Ø!'Ô!9ˆÔØ”} dÑ*ˆŒå”i Ô 0°$´.À4Ä=Ñ2PÐW\Ð]Ñ]Ô]ˆŒÝ”i Ô 0°$Ô2JÈTÌ]Ñ2ZÐafÐgÑgÔgˆŒÝ”i Ô 0°$Ô2JÈTÌ]Ñ2ZÐafÐgÑgÔgˆŒÝ”i ¤°´Ñ >ÀÔ@PÐW\Ð]Ñ]Ô]ˆŒàˆŒˆˆr3   r
  ÚcontextrŽ   rv   r&   r‘   c                 óÜ  — |                      ¦   «         \  }}}	||                      ¦   «         d         z   }
t          j        ||gd¬¦  «        }|                      |¦  «        }|                      |¦  «        }|                      |¦  «        }|                     ||| j        | j        ¦  «         	                    dd¦  «        }|                     ||
| j
        | j        ¦  «         	                    dd¦  «        }|                     ||
| j
        | j        ¦  «         	                    dd¦  «        }t          | d|¦  «        }|�|                     ||| j        ¦  «        \  }}t          j        | j        j        t$          ¦  «        } || ||||f| j        | j        | j        sdn| j        dœ|¤Ž\  }}|                     ||| j        | j        z  ¦  «        }|                      |¦  «        }||fS )	aÎ  
        Runs Perceiver Self-Attention, with special (context, latents) appended along the `seq` dimension!

        Args:
            latents (`torch.Tensor`): Tensor of shape [bsz, n_latents, embed_dim] representing fixed length latents to compress to.
            context (`torch.Tensor`): Tensor of shape [bsz, seq, embed_dim] representing long-form context to resample.
            attention_mask (`torch.Tensor`, *optional*): Tensor of shape [bsz, 1, seq, n_latents] representing attention mask.
            position_ids (`torch.LongTensor`, *optional*): Tensor of shape [bsz, seq] representing position indices of each input token.
            past_key_values (`Cache`, *optional*): Tuple of tensors containing cached key and value states.
            output_attentions (`bool`, *optional*, defaults to `False`): Whether to return attention weights.
            use_cache (`bool`, *optional*, defaults to `False`): Whether to use past_key_values for caching.
        r   éþÿÿÿr[   r   r&   Nr‰   r°   )rY   r.   Úconcatr¬   rª   r«   rl   r¤   r¥   rc   r0  ÚgetattrÚupdaterD  r   r±   r;   r²   rž   r®   r�   r•   r¨   rk   rH  )rR   r
  rJ  rŽ   rv   r&   r‘   ÚbszÚq_lenrn   Ú
kv_seq_lenr'   rµ   r¶   r·   r¸   r�   rœ   s                     r4   r„   z"Idefics2PerceiverAttention.forward*  sí  € ð*  Ÿš™œ‰ˆˆU�AØ˜WŸ\š\™^œ^¨AÔ.Ñ.ˆ
åœ g¨wÐ%7¸RÐ@Ñ@Ô@ˆà—+’+˜gÑ&Ô&ˆØ�{Š{˜=Ñ)Ô)ˆØ—’˜]Ñ+Ô+ˆà—,’,˜s E¨4¬>¸4¼=ÑIÔI×SÒSÐTUÐWXÑYÔYˆØ�yŠy˜˜j¨$Ô*BÀDÄMÑRÔR×\Ò\Ð]^Ð`aÑbÔbˆØ—’˜S *¨dÔ.FÈÌÑVÔV×`Ò`ÐabÐdeÑfÔfˆå! $Ð(9¸?ÑKÔKˆàÐ&Ø*×1Ò1°$¸ÀÄÑOÔO‰LˆD�&å(?Ô(MØŒKÔ,Õ.Eñ)
ô )
Ðð %8Ð$7ØØØØØð
%
ð ”nØ”LØ#œ}ÐH�C�C°$Ô2Hð
%
ð 
%
ð ð
%
ð 
%
Ñ!ˆ�\ð "×)Ò)¨#¨u°d´nÀtÄ}Ñ6TÑUÔUˆØ—k’k +Ñ.Ô.ˆà˜LÐ(Ð(r3   r¹   ©NNN)r*   r+   r,   rÐ   rD   r.   r†   Ú
LongTensorr	   r   r   r1   r„   r‡   rˆ   s   @r4   rC  rC    sì   ø€ € € € € ðð ¨#°©*ð Àð ð ð ð ð ð ð0 /3Ø04Ø(,ð:)ð :)à”ð:)ð ”ð:)ð œ tÑ+ð	:)ð
 Ô&¨Ñ-ð:)ð  ™ð:)ð Ð+Ô,ð:)ð 
ˆuŒ|˜Uœ\¨DÑ0Ð0Ô	1ð:)ð :)ð :)ð :)ð :)ð :)ð :)ð :)r3   rC  c                   ó    ‡ — e Zd Zdefˆ fd„Z	 	 	 ddej        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 )r  rD  c                 óò  •— t          ¦   «                              ¦   «          |j        | _        |j        | _        |j        | _        |j        | _        t          | j        | j        ¬¦  «        | _	        t          | j        | j        ¬¦  «        | _
        t          ||¬¦  «        | _        t          | j        | j        ¬¦  «        | _        t          |j        |j        dz  |j        |j        ¬¦  «        | _        d S )NrÖ   )rD  é   ©rE   r¿   rÅ   r½   )rC   rD   rE   Úresampler_n_latentsÚ	n_latentsÚresampler_depthÚdepthÚrms_norm_epsr3  Úinput_latents_normÚinput_context_normrC  ré   Úpost_attention_layernormrÄ   r½   rà   rI  s      €r4   rD   zIdefics2PerceiverLayer.__init__h  sß   ø€ Ý‰Œ×ÒÑÔÐØ!Ô-ˆÔØÔ3ˆŒØÔ+ˆŒ
Ø"Ô/ˆÔå"1°$Ô2BÈÔHYÐ"ZÑ"ZÔ"ZˆÔÝ"1°$Ô2BÈÔHYÐ"ZÑ"ZÔ"ZˆÔÝ3°FÀiÐPÑPÔPˆŒÝ(7¸Ô8HÈdÔN_Ð(`Ñ(`Ô(`ˆÔ%ÝØÔ*Ø$Ô0°1Ñ4ØÔ*ØÔ(ð	
ñ 
ô 
ˆŒˆˆr3   Nr
  rJ  rŽ   rv   r&   r‘   rV   c                 óò   — |}|                       |¦  «        }|                      |¦  «        } | j        d|||dœ|¤Ž\  }}||z   }|}|                      |¦  «        }|                      |¦  «        }||z   }|S )a�  
        Args:
            latents (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
            context (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
            attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
                `(batch, sequence_length)` where padding elements are indicated by 0.
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors of all attention layers. See `attentions` under
                returned tensors for more detail.
            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`).
            past_key_values (`Cache`, *optional*): cached past key and value projection states
        )r
  rJ  rŽ   r2   )r^  r_  ré   r`  rà   )	rR   r
  rJ  rŽ   rv   r&   r‘   rä   rn   s	            r4   r„   zIdefics2PerceiverLayer.forwardz  s§   € ð. ˆà×)Ò)¨'Ñ2Ô2ˆØ×)Ò)¨'Ñ2Ô2ˆà#�T”^ð 
ØØØ)ð
ð 
ð ð	
ð 
‰
ˆ�ð ˜WÑ$ˆØˆà×/Ò/°Ñ8Ô8ˆØ—(’(˜7Ñ#Ô#ˆØ˜WÑ$ˆàˆr3   rS  )r*   r+   r,   rÐ   rD   r.   r†   rT  r	   r   r   r/   r„   r‡   rˆ   s   @r4   r  r  g  sÌ   ø€ € € € € ð
¨#ð 
ð 
ð 
ð 
ð 
ð 
ð, /3Ø04Ø(,ð)ð )à”ð)ð ”ð)ð œ tÑ+ð	)ð
 Ô&¨Ñ-ð)ð  ™ð)ð Ð+Ô,ð)ð 
Ô	ð)ð )ð )ð )ð )ð )ð )ð )r3   r  zi
    Idefics2 perceiver resampler model that performs `depth` blocks of cross-attention with a fixed
    c            	       óŽ   ‡ — e Zd ZU eed<   dZdZdZdZdˆ fd„Z	e
dej        dej        d	ee         dej        fd
„¦   «         Zˆ xZS )r  r;   r  TrV   Nc                 ó  •‡— t          ¦   «                              ‰¦  «         ‰j        | _        ‰j        | _        ‰j        | _        ‰j        | _        ‰j        | _        t          j
        t          j        | j        | j        ¦  «        ¦  «        | _        t          j        ˆfd„t          | j        ¦  «        D ¦   «         ¦  «        | _        t#          | j        | j        ¬¦  «        | _        |                      ¦   «          d S )Nc                 ó0   •— g | ]}t          ‰|¦  «        ‘ŒS r2   )r  )rñ   Úidxr;   s     €r4   rò   z7Idefics2PerceiverResampler.__init__.<locals>.<listcomp>¾  s$   ø€ Ð$fÐ$fÐ$fÈSÕ%;¸FÀCÑ%HÔ%HÐ$fÐ$fÐ$fr3   rÖ   )rC   rD   rE   r½   rY  rZ  r[  r\  r]  r   rØ   r.   r(  r
  ró   rô   rö   r3  Únormr  rQ   s    `€r4   rD   z#Idefics2PerceiverResampler.__init__²  sÙ   øø€ Ý‰Œ×Ò˜Ñ Ô Ð Ø!Ô-ˆÔØ Ô+ˆŒØÔ3ˆŒØÔ+ˆŒ
Ø"Ô/ˆÔõ ”|¥E¤J¨t¬~¸tÔ?OÑ$PÔ$PÑQÔQˆŒõ ”mÐ$fÐ$fÐ$fÐ$fÕTYÐZ^ÔZdÑTeÔTeÐ$fÑ$fÔ$fÑgÔgˆŒÝ# DÔ$4¸$Ô:KÐLÑLÔLˆŒ	à�ŠÑÔÐÐÐr3   rJ  rŽ   r‘   c                 óø  — | j                              d¦  «                             |j        d         g| j                              ¦   «         ¢R ¦  «        }t          j        |                     d¦  «        |                     d¦  «        f|j        |j        ¬¦  «        }t          j	        ||gd¬¦  «        }t          | j        ||¬¦  «        }|}| j        D ]} |||f|ddœ|¤Ž}Œ|                      |¦  «        }|S )	zw
        context (`torch.FloatTensor` of shape `(batch, seq_len, embed_dim)`):
            Input to the layer.
        r   r   r%  r`   r[   r'  N)rŽ   rv   )r
  Ú	unsqueezer.  ra   rY   r.   r(  r]   rX   Úcatr   r;   rö   rf  )rR   rJ  rŽ   r‘   r
  Úlatent_attention_maskÚcompressed_contextÚperceiver_layers           r4   r„   z"Idefics2PerceiverResampler.forwardÃ  s0  € ð ”,×(Ò(¨Ñ+Ô+×2Ò2°G´MÀ!Ô4DÐ3[ÀtÄ|×GXÒGXÑGZÔGZÐ3[Ð3[Ñ\Ô\ˆå %¤
Ø× Ò  Ñ#Ô# W§\¢\°!¡_¤_Ð5¸^Ô=QÐZhÔZoð!
ñ !
ô !
Ðõ œ NÐ4IÐ#JÐPRÐSÑSÔSˆÝ2Ø”;Ø!Ø)ð
ñ 
ô 
ˆð %ÐØ#œ{ð 	ð 	ˆOØ!0 Ø"Øð"ð  .Ø!ð	"ð "ð
 ð"ð "ÐÐð "ŸYšYÐ'9Ñ:Ô:Ðà!Ð!r3   )rV   N)r*   r+   r,   r   r0   r  r  r  r  rD   r   r.   r†   r   r   r„   r‡   rˆ   s   @r4   r  r  ¦  s¶   ø€ € € € € € ð $Ð#Ð#Ñ#Ø!ÐØ€NØÐØÐðð ð ð ð ð ð" ð#"à”ð#"ð œð#"ð Ð+Ô,ð	#"ð
 
Œð#"ð #"ð #"ñ „^ð#"ð #"ð #"ð #"ð #"r3   r  c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚIdefics2Connectorc                 ó  •— t          ¦   «                              ¦   «          t          |j        j        |j        j        |j        j        |j        j        ¬¦  «        | _        t           
                    |j        ¦  «        | _        d S )NrX  )rC   rD   rÄ   Úvision_configrE   Útext_configr¿   r½   Úmodality_projectionr  Ú_from_configÚperceiver_configÚperceiver_resamplerrQ   s     €r4   rD   zIdefics2Connector.__init__ë  su   ø€ Ý‰Œ×ÒÑÔÐÝ#.ØÔ,Ô8Ø$Ô0ÔBØÔ*Ô6ØÔ)Ô4ð	$
ñ $
ô $
ˆÔ õ $>×#JÒ#JÈ6ÔKbÑ#cÔ#cˆÔ Ð Ð r3   c                 ó^   — |                       |¦  «        }|                      ||¬¦  «        }|S )N)rJ  rŽ   )rr  ru  )rR   r)   rŽ   s      r4   r„   zIdefics2Connector.forwardõ  s8   € Ø"×6Ò6Ð7JÑKÔKÐØ"×6Ò6Ð?RÐcqÐ6ÑrÔrÐØ"Ð"r3   )r*   r+   r,   rD   r„   r‡   rˆ   s   @r4   rn  rn  ê  sL   ø€ € € € € ðdð dð dð dð dð#ð #ð #ð #ð #ð #ð #r3   rn  z[
    Idefics2 model consisting of a SIGLIP vision encoder and Mistral language decoder
    c                   óà  ‡ — e Zd Zdefˆ fd„Zd„ Zd„ Zdej        dej	        dz  dej	        dz  fd	„Z
ee	 dd
ej        dej        dz  dee         deez  fd„¦   «         ¦   «         Ze ed¬¦  «        	 	 	 	 	 	 	 	 	 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dz  dee         deez  fd„¦   «         ¦   «         Zˆ xZS )ÚIdefics2Modelr;   c                 óº  •— t          ¦   «                              |¦  «         | j        j        j        | _        | j        j        j        | _        t                               |j	        ¦  «        | _
        t          |¦  «        | _        t          j        |j        ¦  «        | _        |j        j        | _        | j        j        | _        |                      ¦   «          d S r¹   )rC   rD   r;   rq  Úpad_token_idÚpadding_idxÚ
vocab_sizer  rs  rp  Úvision_modelrn  Ú	connectorr   Úfrom_configÚ
text_modelrt  rY  Úimage_seq_lenÚimage_token_idr  rQ   s     €r4   rD   zIdefics2Model.__init__  s¨   ø€ Ý‰Œ×Ò˜Ñ Ô Ð Øœ;Ô2Ô?ˆÔØœ+Ô1Ô<ˆŒå5×BÒBÀ6ÔCWÑXÔXˆÔÝ*¨6Ñ2Ô2ˆŒÝ#Ô/°Ô0BÑCÔCˆŒà#Ô4ÔHˆÔØ"œkÔ8ˆÔà�ŠÑÔÐÐÐr3   c                 ó4   — | j                              ¦   «         S r¹   )r€  r  r  s    r4   r  z"Idefics2Model.get_input_embeddings  s   € ØŒ×3Ò3Ñ5Ô5Ð5r3   c                 ó:   — | j                              |¦  «         d S r¹   )r€  r"  r!  s     r4   r"  z"Idefics2Model.set_input_embeddings  s   € ØŒ×,Ò,¨UÑ3Ô3Ð3Ð3Ð3r3   Ú	input_idsrú   Nr)   c                 óº  — |€e| |                       ¦   «         t          j        | j        j        t          j        |j        ¬¦  «        ¦  «        k    }|                     d¦  «        }n|| j        j        k    }|                     d¦  «         	                    |j        ¦  «        }| 	                    |j        |j
        ¦  «        }|                     ||¦  «        }|S )ar  
        This method aims at merging the token embeddings with the image hidden states into one single sequence of vectors that are fed to the transformer LM.
        The merging happens as follows:
        - The text token sequence is: `tok_1 tok_2 tok_3 <fake_token_around_image> <image> <image> ... <image> <fake_token_around_image> tok_4`.
        - We get the image hidden states for the image through the vision encoder (and potentially the perceiver), and that hidden state is then projected into the text embedding space.
        We thus have a sequence of image hidden states of size (1, image_seq_len, hidden_dim), where 1 is for batch_size of 1 image and hidden_dim is the hidden_dim of the LM transformer.
        - The merging happens so that we obtain the following sequence: `vector_tok_1 vector_tok_2 vector_tok_3 vector_fake_tok_around_image {sequence of image_seq_len image hidden states} vector_fake_toke_around_image vector_tok_4`. That sequence is fed to the LM.
        - To fit the format of that sequence, `input_ids`, `inputs_embeds`, `attention_mask` are all 3 adapted to insert the image hidden states.
        Nr%  r`   )r  r.   Útensorr;   r‚  ÚlongrX   Úallrh  ri   r]   Úmasked_scatter)rR   r…  rú   r)   Úspecial_image_masks        r4   Úinputs_mergerzIdefics2Model.inputs_merger  sÖ   € ð ÐØ!.Ð2M°$×2KÒ2KÑ2MÔ2MÝ”˜Tœ[Ô7½u¼zÐR_ÔRfÐgÑgÔgñ3ô 3ò "Ðð "4×!7Ò!7¸Ñ!;Ô!;ÐÐà!*¨d¬kÔ.HÒ!HÐà/×9Ò9¸"Ñ=Ô=×@Ò@ÀÔAUÑVÔVÐØ1×4Ò4°]Ô5IÈ=ÔK^Ñ_Ô_ÐØ%×4Ò4Ð5GÐI\Ñ]Ô]ˆØÐr3   rT   Úpixel_attention_maskr‘   rV   c                 óT  — |j         \  }}}}}|                     | j        ¬¦  «        } |j        ||z  g|j         dd…         ¢R Ž }|j         dd…                              ¦   «         }	|dk                         d¬¦  «        |	k    }
||
                              ¦   «         }|€ct          j        | 	                    d¦  «        | 	                    d¦  «        | 	                    d	¦  «        ft          j
        |j        ¬
¦  «        }n8 |j        ||z  g|j         dd…         ¢R Ž }||
                              ¦   «         }| j        j        j        }|                     d||¬¦  «        }|                     d||¬¦  «        }|                     d¬¦  «        ||z  k     
                    ¦   «         } | j        d||dœ|¤Ž}|j        }|                      ||                     | 	                    d¦  «        d¦  «        ¬¦  «        }|                     d|j         d         ¦  «        |_        |S )á4  
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`):
            The tensors corresponding to the input images.
        pixel_attention_mask (`torch.LongTensor`, *optional*):
            The attention mask indicating padded regions in the image.
        )r]   r   Nr   r‰   )r`   rL  éýÿÿÿr[   r   r   )rY   r]   rX   )Ú	dimensionrY   Ústep)r`   rL  r&  r`   )rŽ   r2   )ra   ri   r]   rl   Únumelrf   r›   r.   r(  rY   r)  rX   r;   rp  rH   Úunfoldr}  r%   r~  Úpooler_output)rR   rT   r�  r‘   rm   Ú
num_imagesrJ   ÚheightÚwidthÚnb_values_per_imageÚreal_images_indsrH   Úpatches_subgridrU   Úimage_outputsr)   Úimage_featuress                    r4   Úget_image_featuresz Idefics2Model.get_image_features1  sr  € ð ?KÔ>PÑ;ˆ
�J ¨f°eØ#—’¨T¬Z�Ñ8Ô8ˆØ(�|Ô(¨°jÑ)@ÐZÀ<ÔCUÐVWÐVXÐVXÔCYÐZÐZÐZˆð +Ô0°°°Ô4×:Ò:Ñ<Ô<ÐØ(¨CÒ/×4Ò4¸Ð4ÑFÔFÐJ]Ò]ÐØ#Ð$4Ô5×@Ò@ÑBÔBˆð  Ð'Ý#(¤:Ø"×'Ò'¨Ñ*Ô*¨L×,=Ò,=¸aÑ,@Ô,@À,×BSÒBSÐTUÑBVÔBVÐWÝ”jØ#Ô*ð$ñ $ô $Ð Ð ð $=Ð#7Ô#<¸ZÈ*Ñ=TÐ#vÐWkÔWqÐrsÐrtÐrtÔWuÐ#vÐ#vÐ#vÐ Ø#7Ð8HÔ#I×#TÒ#TÑ#VÔ#VÐ à”[Ô.Ô9ˆ
Ø.×5Ò5ÀÈ
ÐYcÐ5ÑdÔdˆØ)×0Ò0¸1À:ÐT^Ð0Ñ_Ô_ˆØ /× 3Ò 3¸Ð 3Ñ AÔ AÀZÐR\ÑE\Ò \×bÒbÑdÔdÐà)˜Ô)ð 
Ø%Ð<Pð
ð 
ØTZð
ð 
ˆð ,Ô=Ðð ŸšØÐ0D×0IÒ0IÈ,×J[ÒJ[Ð\]ÑJ^ÔJ^Ð`bÑ0cÔ0cð (ñ 
ô 
ˆð '5×&9Ò&9¸"¸nÔ>RÐSUÔ>VÑ&WÔ&WˆÔ#àÐr3   aÙ  
        Inputs fed to the model can have an arbitrary number of images. To account for this, pixel_values fed to
        the model have image padding -> (batch_size, max_num_images, 3, max_heights, max_widths) where
        max_num_images is the maximum number of images among the batch_size samples in the batch.

        Padding images are not needed beyond padding the pixel_values at the entrance of the model.
        For efficiency, we only pass through the vision_model's forward the real images by
        discarding the padding images i.e. pixel_values of size (image_batch_size, 3, height, width) where
        image_batch_size would be 7 when num_images_per_sample=[1, 3, 1, 2] and max_num_images would be 3.
        r!   rŽ   rv   r&   Ú	use_cachec
           	      ó   — |	�|	n| j         j        }	| j        r*| j        j        r|	rt
                               d¦  «         d}	|�|j        \  }}n|�|j        \  }}}nt          d¦  «        ‚|	r|€t          | j         ¬¦  «        }|€" | j         
                    ¦   «         |¦  «        }|�|�t          d¦  «        ‚|� | j        ||fi |
¤Žj        }n#|�!|                     | j        |j        ¬¦  «        }|�|                      |||¬¦  «        }d	|
d
<    | j        d|||||	dœ|
¤Ž}t#          |j        |j        |j        |j        |¬¦  «        S )a•  
        pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*):
            Mask to avoid performing attention on padding pixel indices.
        image_hidden_states (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`):
            The hidden states of the image encoder after modality projection and perceiver resampling.
        NzZ`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...Fz5You have to specify either input_ids or inputs_embeds)r;   zMYou cannot specify both pixel_values and image_hidden_states at the same timer%  )r…  rú   r)   TÚreturn_dict)rú   rŽ   rv   r&   rŸ  )r%   r&   r'   r(   r)   r2   )r;   rŸ  r•   r€  r÷   ÚloggerÚwarning_oncera   r¦   r
   r  rž  r•  ri   r]   rX   rŒ  r$   r%   r&   r'   r(   )rR   r…  rŽ   rv   r&   rú   rT   r�  r)   rŸ  r‘   rm   Ú
seq_lengthrn   Úoutputss                  r4   r„   zIdefics2Model.forwardf  sã  € ðB "+Ð!6�I�I¸D¼KÔ<Qˆ	ØŒ=ð 	˜Tœ_ÔCð 	È	ð 	Ý×ÒØlñô ð ð ˆIð Ð Ø%.¤_Ñ"ˆJ˜
˜
ØÐ&Ø(5Ô(;Ñ%ˆJ˜
 A AåÐTÑUÔUÐUàð 	?˜Ð0Ý*°$´+Ð>Ñ>Ô>ˆOàÐ ØB˜DœO×@Ò@ÑBÔBÀ9ÑMÔMˆMð Ð#Ð(;Ð(GÝÐlÑmÔmÐmØÐ%Ø"9 $Ô"9¸,ÐH\Ð"gÐ"gÐ`fÐ"gÐ"gÔ"uÐÐØ Ð,Ø"5×"8Ò"8¸t¼zÐR[ÔRbÐ"8Ñ"cÔ"cÐàÐ*ð !×.Ò.Ø#Ø+Ø$7ð /ñ ô ˆMð !%ˆˆ}ÑØ!�$”/ð 
Ø'Ø)Ø%Ø+Øð
ð 
ð ð
ð 
ˆõ /Ø%Ô7Ø#Ô3Ø!Ô/ØÔ)Ø 3ð
ñ 
ô 
ð 	
r3   r¹   )	NNNNNNNNN)r*   r+   r,   r   rD   r  r"  r.   rT  r†   rŒ  r   r   r/   r   r   r1   r   rž  r	   r…   r)  r   r$   r„   r‡   rˆ   s   @r4   rx  rx  û  sG  ø€ € € € € ð˜~ð ð ð ð ð ð ð6ð 6ð 6ð4ð 4ð 4ðàÔ#ðð ”| dÑ*ðð #œ\¨DÑ0ð	ð ð ð ð8 Øð 9=ð1ð 1àÔ'ð1ð $Ô.°Ñ5ð1ð Ð+Ô,ð	1ð
 
Ð+Ñ	+ð1ð 1ð 1ñ „^ñ Ôð1ðf Ø€^ð	ðñ ô ð .2Ø.2Ø04Ø(,Ø26Ø15Ø8<Ø8<Ø!%ðJ
ð J
àÔ# dÑ*ðJ
ð œ tÑ+ðJ
ð Ô&¨Ñ-ð	J
ð
  ™ðJ
ð Ô(¨4Ñ/ðJ
ð Ô'¨$Ñ.ðJ
ð $Ô.°Ñ5ðJ
ð #Ô.°Ñ5ðJ
ð ˜$‘;ðJ
ð Ð-Ô.ðJ
ð 
Ð0Ñ	0ðJ
ð J
ð J
ñô ñ ÔðJ
ð J
ð J
ð J
ð J
r3   rx  zˆ
    The Idefics2 Model with a language modeling head. It is made up a SigLIP vision encoder, with a language modeling head on top.
    c                   óÊ  ‡ — e Zd ZddiZˆ fd„Zd„ Zd„ Ze	 ddej	        dej
        dz  d	ee         d
eez  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dz  deej        z  d	ee         d
eez  fd„¦   «         ¦   «         Z	 	 	 	 	 	 	 	 	 dˆ fd„	Zˆ xZS )Ú Idefics2ForConditionalGenerationzlm_head.weightz$model.text_model.embed_tokens.weightc                 ó>  •— t          ¦   «                              |¦  «         t          |¦  «        | _        | j        j        | _        t          j        |j        j	        |j        j
        d¬¦  «        | _        |j        j
        | _
        |                      ¦   «          d S rÇ   )rC   rD   rx  rþ   r;   r‚  r   r©   rq  rE   r|  Úlm_headr  rQ   s     €r4   rD   z)Idefics2ForConditionalGeneration.__init__È  s�   ø€ Ý‰Œ×Ò˜Ñ Ô Ð Ý" 6Ñ*Ô*ˆŒ
Ø"œkÔ8ˆÔå”y Ô!3Ô!?ÀÔASÔA^ÐejÐkÑkÔkˆŒØ Ô,Ô7ˆŒð 	�ŠÑÔÐÐÐr3   c                 ó>   — | j         j                             ¦   «         S r¹   )rþ   r€  r  r  s    r4   r  z5Idefics2ForConditionalGeneration.get_input_embeddingsÓ  s   € ØŒzÔ$×9Ò9Ñ;Ô;Ð;r3   c                 óD   — | j         j                             |¦  «         d S r¹   )rþ   r€  r"  r!  s     r4   r"  z5Idefics2ForConditionalGeneration.set_input_embeddingsÖ  s!   € ØŒ
Ô×2Ò2°5Ñ9Ô9Ð9Ð9Ð9r3   NrT   r�  r‘   rV   c                 ó,   —  | j         j        d||dœ|¤ŽS )r�  )rT   r�  r2   )rþ   rž  )rR   rT   r�  r‘   s       r4   rž  z3Idefics2ForConditionalGeneration.get_image_featuresÙ  s5   € ð -ˆtŒzÔ,ð 
Ø%Ð<Pð
ð 
ØTZð
ð 
ð 	
r3   r   r…  rŽ   rv   r&   rú   r)   ÚlabelsrŸ  Úlogits_to_keepc                 ón  —  | j         d|||||||||
dœ	|¤Ž}|d         }t          |t          ¦  «        rt          | d¦  «        n|}|                      |dd…|dd…f         ¦  «        }d}|	�  | j        d||	| j        j        j        dœ|¤Ž}t          |||j
        |j        |j        |j        ¬¦  «        S )aô  
        pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*):
            Mask to avoid performing attention on padding pixel indices.
        image_hidden_states (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`):
            The hidden states of the image encoder after modality projection and perceiver resampling.
        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 `model.image_token_id` (where `model` is your instance of `Idefics2ForConditionalGeneration`).
            Tokens with indices set to `model.image_token_id` are ignored (masked), the loss is only
            computed for the tokens with labels in `[0, ..., config.vocab_size]`.

        Example:

        ```python
        >>> import torch
        >>> from PIL import Image
        >>> from io import BytesIO

        >>> from transformers import AutoProcessor, AutoModelForImageTextToText
        >>> from transformers.image_utils import load_image

        >>> # Note that passing the image urls (instead of the actual pil images) to the processor is also possible
        >>> image1 = load_image("https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg")
        >>> image2 = load_image("https://cdn.britannica.com/59/94459-050-DBA42467/Skyline-Chicago.jpg")
        >>> image3 = load_image("https://cdn.britannica.com/68/170868-050-8DDE8263/Golden-Gate-Bridge-San-Francisco.jpg")

        >>> processor = AutoProcessor.from_pretrained("HuggingFaceM4/idefics2-8b-base")
        >>> model = AutoModelForImageTextToText.from_pretrained("HuggingFaceM4/idefics2-8b-base", device_map="auto")

        >>> BAD_WORDS_IDS = processor.tokenizer(["<image>", "<fake_token_around_image>"], add_special_tokens=False).input_ids
        >>> EOS_WORDS_IDS = [processor.tokenizer.eos_token_id]

        >>> # Create inputs
        >>> prompts = [
        ...   "<image>In this image, we can see the city of New York, and more specifically the Statue of Liberty.<image>In this image,",
        ...   "In which city is that bridge located?<image>",
        ... ]
        >>> images = [[image1, image2], [image3]]
        >>> inputs = processor(images=images, text=prompts, padding=True, return_tensors="pt").to("cuda")

        >>> # Generate
        >>> generated_ids = model.generate(**inputs, bad_words_ids=BAD_WORDS_IDS, max_new_tokens=20)
        >>> generated_texts = processor.batch_decode(generated_ids, skip_special_tokens=True)

        >>> print(generated_texts)
        ['In this image, we can see the city of New York, and more specifically the Statue of Liberty. In this image, we can see the city of New York, and more specifically the Statue of Liberty.\n\n', 'In which city is that bridge located?\n\nThe bridge is located in the city of Pittsburgh, Pennsylvania.\n\n\nThe bridge is']
        ```)	r…  rŽ   rv   r&   rú   rT   r�  r)   rŸ  r   N)r8   r­  r|  )r7   r8   r&   r'   r(   r)   r2   )rþ   r  rÐ   Úslicer©  Úloss_functionr;   rq  r|  r6   r&   r'   r(   r)   )rR   r…  rŽ   rv   r&   rú   rT   r�  r)   r­  rŸ  r®  r‘   r¥  r'   Úslice_indicesr8   r7   s                     r4   r„   z(Idefics2ForConditionalGeneration.forwardê  s  € ðD �$”*ð 
ØØ)Ø%Ø+Ø'Ø%Ø!5Ø 3Øð
ð 
ð ð
ð 
ˆð   œ
ˆå8BÀ>ÕSVÑ8WÔ8WÐk�˜~˜o¨tÑ4Ô4Ð4Ð]kˆØ—’˜m¨A¨A¨A¨}¸a¸a¸aÐ,?Ô@ÑAÔAˆàˆØÐØ%�4Ô%ð Ø f¸¼Ô9PÔ9[ðð Ø_eðð ˆDõ .ØØØ#Ô3Ø!Ô/ØÔ)Ø 'Ô ;ð
ñ 
ô 
ð 	
r3   Fc                 óp   •—  t          ¦   «         j        |f||||||||	|
dœ	|¤Ž}|€|
r|	s
d |d<   d |d<   |S )N)	r&   rŽ   rú   rT   r�  r)   r®  Úis_first_iterationrŸ  rT   r�  )rC   Úprepare_inputs_for_generation)rR   r…  r&   rŽ   rú   rT   r�  r)   r®  r´  rŸ  r‘   Úmodel_inputsrS   s                €r4   rµ  z>Idefics2ForConditionalGeneration.prepare_inputs_for_generationM  s}   ø€ ð" =•u‘w”wÔ<Øð
à+Ø)Ø'Ø%Ø!5Ø 3Ø)Ø1Øð
ð 
ð ð
ð 
ˆð Ð*¨yÐ*ÐASÐ*Ø+/ˆL˜Ñ(Ø37ˆLÐ/Ñ0àÐr3   r¹   )NNNNNNNNNNr   )	NNNNNNNFF)r*   r+   r,   Ú_tied_weights_keysrD   r  r"  r   r.   r/   rT  r   r   r1   r   rž  r   r†   r	   r…   r)  rÐ   r6   r„   rµ  r‡   rˆ   s   @r4   r§  r§  À  sF  ø€ € € € € ð +Ð,RÐSÐð	ð 	ð 	ð 	ð 	ð<ð <ð <ð:ð :ð :ð ð 9=ð
ð 
àÔ'ð
ð $Ô.°Ñ5ð
ð Ð+Ô,ð	
ð
 
Ð+Ñ	+ð
ð 
ð 
ñ „^ð
ð  Øð .2Ø.2Ø04Ø(,Ø26Ø15Ø8<Ø8<Ø*.Ø!%Ø-.ð_
ð _
àÔ# dÑ*ð_
ð œ tÑ+ð_
ð Ô&¨Ñ-ð	_
ð
  ™ð_
ð Ô(¨4Ñ/ð_
ð Ô'¨$Ñ.ð_
ð $Ô.°Ñ5ð_
ð #Ô.°Ñ5ð_
ð Ô  4Ñ'ð_
ð ˜$‘;ð_
ð ˜eœlÑ*ð_
ð Ð+Ô,ð_
ð 
Ð/Ñ	/ð_
ð _
ð _
ñ „^ñ Ôð_
ðH ØØØØ!Ø ØØ Øð#ð #ð #ð #ð #ð #ð #ð #ð #ð #r3   r§  )r§  rý   rx  )r‰   )Kr-   Úcollections.abcr   Údataclassesr   r.   r   Ú r   r  Úactivationsr   Úcache_utilsr	   r
   Ú
generationr   Úmasking_utilsr   Úmodeling_flash_attention_utilsr   Úmodeling_layersr   Úmodeling_outputsr   r   r   Úmodeling_utilsr   r   Úprocessing_utilsr   Úutilsr   r   r   r   Úutils.genericr   Úutils.output_capturingr   Úautor   Úconfiguration_idefics2r   r   r    Ú
get_loggerr*   r¢  r$   r6   ÚModuler:   r†   rA  rž   r    r»   rÄ   rÓ   ræ   rî   rý   r  rÐ   r—   r3  rC  r  r  rn  rx  r§  Ú__all__r2   r3   r4   ú<module>rÌ     sÜ  ðð Ð à $Ð $Ð $Ð $Ð $Ð $Ø !Ð !Ð !Ð !Ð !Ð !à €€€Ø Ð Ð Ð Ð Ð à &Ð &Ð &Ð &Ð &Ð &Ø !Ð !Ð !Ð !Ð !Ð !Ø .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ø )Ð )Ð )Ð )Ð )Ð )Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø BÐ BÐ BÐ BÐ BÐ BØ 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XÐ XØ FÐ FÐ FÐ FÐ FÐ FÐ FÐ FØ &Ð &Ð &Ð &Ð &Ð &Ø RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RÐ RØ 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø Ð Ð Ð Ð Ð Ø aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ aÐ að 
ˆÔ	˜HÑ	%Ô	%€ð €ððñ ô ð
 ð@ð @ð @ð @ð @ kñ @ô @ñ „ñô ð@ð& €ððñ ô ð
 ð@ð @ð @ð @ð @ [ñ @ô @ñ „ñô ð@ð4Ið Ið Ið Ið I˜rœyñ Iô Ið Iðf ð%ð %ØŒIð%àŒ<ð%ð 
Œð%ð Œ<ð	%ð
 ”L 4Ñ'ð%ð ð%ð ð%ð Ð'Ô(ð%ð %ð %ð %ð6;)ð ;)ð ;)ð ;)ð ;)˜bœiñ ;)ô ;)ð ;)ð~ð ð ð ð ˜œ	ñ ô ð ðPð Pð Pð Pð P�"”)ñ Pô Pð Pð&"ð "ð "ð "ð "¨B¬Iñ "ô "ð "ð< ð  ð  ð  ð  Ð5ñ  ô  ð  ðH@ð @ð @ð @ð @�b”iñ @ô @ð @ðD ð'ð 'ð 'ð 'ð '˜oñ 'ô 'ñ „ð'ð, €ððñ ô ð
DDð DDð DDð DDð DDÐ 7ñ DDô DDñô ð
DDðP	U˜Uœ\ð 	U°#ð 	U¸%¼,ð 	Uð 	Uð 	Uð 	UðJð Jð Jð Jð J�b”iñ Jô Jð Jð(O)ð O)ð O)ð O)ð O) ¤ñ O)ô O)ð O)ðd<ð <ð <ð <ð <˜RœYñ <ô <ð <ð~ €ððñ ô ð
<"ð <"ð <"ð <"ð <"Ð!8ñ <"ô <"ñô ð
<"ð~#ð #ð #ð #ð #˜œ	ñ #ô #ð #ð" €ððñ ô ð
}
ð }
ð }
ð }
ð }
Ð+ñ }
ô }
ñô ð
}
ð@ €ððñ ô ð
kð kð kð kð kÐ'>Àñ kô kñô ð
kð\ [Ð
ZÐ
Z€€€r3   