Ë
    S^(h` ã                   ót  — d Z ddlZddlmZ ddlmZmZmZmZ ddl	Z	ddl
Z	ddl	mZ ddlmZ ddlmZmZmZmZ dd	lmZ dd
lmZmZmZ ddlmZmZmZmZmZ ddlm Z m!Z!m"Z"  ejF                  e$«      Z%dZ&dZ'dZ(dZ)dZ*dZ+e G d„ de«      «       Z,e G d„ de«      «       Z-e G d„ de«      «       Z.de	j^                  de	j^                  fd„Z0de	j^                  de	j^                  fd„Z1de"d e2fd!„Z3dSd"ee2ef   d#e4fd$„Z5 G d%„ d&ejl                  «      Z7 G d'„ d(ejp                  «      Z9 G d)„ d*ejl                  «      Z: G d+„ d,ejl                  «      Z; G d-„ d.ejl                  «      Z< G d/„ d0ejl                  «      Z= G d1„ d2ejl                  «      Z> G d3„ d4ejl                  «      Z? G d5„ d6ejl                  «      Z@ G d7„ d8ejl                  «      ZA G d9„ d:ejl                  «      ZBd;eAiZC G d<„ d=ejl                  «      ZD G d>„ d?ejl                  «      ZE G d@„ dAejl                  «      ZF G dB„ dCejl                  «      ZG G dD„ dEejl                  «      ZH G dF„ dGejl                  «      ZI G dH„ dIe«      ZJ edJe(«       G dK„ dLeJ«      «       ZK edMe(«       G dN„ dOeJ«      «       ZL ee(«       G dP„ dQeJ«      «       ZMg dR¢ZNy)TzPyTorch ALIGN model.é    N)Ú	dataclass)ÚAnyÚOptionalÚTupleÚUnion)Únné   )ÚACT2FN)ÚBaseModelOutputWithNoAttentionÚ)BaseModelOutputWithPastAndCrossAttentionsÚ,BaseModelOutputWithPoolingAndCrossAttentionsÚ(BaseModelOutputWithPoolingAndNoAttention)ÚPreTrainedModel)Úapply_chunking_to_forwardÚ find_pruneable_heads_and_indicesÚprune_linear_layer)ÚModelOutputÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚloggingÚreplace_return_docstringsé   )ÚAlignConfigÚAlignTextConfigÚAlignVisionConfigzkakaobrain/align-baser   a>  
    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

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

    Parameters:
        config ([`AlignConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
a¾
  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
            it.

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

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

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

            [What are attention masks?](../glossary#attention-mask)
        position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.max_position_embeddings - 1]`.

            [What are position IDs?](../glossary#position-ids)
        token_type_ids (`torch.LongTensor` of shape `({0})`, *optional*):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,
            1]`:

            - 0 corresponds to a *sentence A* token,
            - 1 corresponds to a *sentence B* token.

            [What are token type IDs?](../glossary#token-type-ids)
        head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules. Mask values selected in `[0, 1]`:

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.

        inputs_embeds (`torch.FloatTensor` of shape `({0}, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
aƒ  
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using
            [`AutoImageProcessor`]. See [`EfficientNetImageProcessor.__call__`] for details.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
aW  
    Args:
       input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
            it.

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

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

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

            [What are attention masks?](../glossary#attention-mask)
        position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
            config.max_position_embeddings - 1]`.

            [What are position IDs?](../glossary#position-ids)
        token_type_ids (`torch.LongTensor` of shape `({0})`, *optional*):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,
            1]`:

            - 0 corresponds to a *sentence A* token,
            - 1 corresponds to a *sentence B* token.

            [What are token type IDs?](../glossary#token-type-ids)
        head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules. Mask values selected in `[0, 1]`:

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.

        inputs_embeds (`torch.FloatTensor` of shape `({0}, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using
            [`AutoImageProcessor`]. See [`EfficientNetImageProcessor.__call__`] for details.
        return_loss (`bool`, *optional*):
            Whether or not to return the contrastive loss.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
c                   ó�   — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eeej                        ed<   y)ÚAlignVisionModelOutputa+  
    Base class for vision model's outputs that also contains image embeddings of the pooling of the last hidden states.

    Args:
        image_embeds (`torch.FloatTensor` of shape `(batch_size, output_dim)` *optional* returned when model is initialized with `with_projection=True`):
            The image embeddings obtained by applying the projection layer to the pooler_output.
        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.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
            one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.

            Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
    NÚimage_embedsÚlast_hidden_stateÚhidden_states)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ÚtorchÚFloatTensorÚ__annotations__r   r    r   © ó    úf/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/align/modeling_align.pyr   r   ·   sN   … ñð 15€L�(˜5×,Ñ,Ñ-Ó4Ø59Ð�x × 1Ñ 1Ñ2Ó9Ø8<€M�8˜E %×"3Ñ"3Ñ4Ñ5Ô<r)   r   c                   ó¾   — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eeej                        ed<   dZeeej                        ed<   y)ÚAlignTextModelOutputaÃ  
    Base class for text model's outputs that also contains a pooling of the last hidden states.

    Args:
        text_embeds (`torch.FloatTensor` of shape `(batch_size, output_dim)` *optional* returned when model is initialized with `with_projection=True`):
            The text embeddings obtained by applying the projection layer to the pooler_output.
        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.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
            one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.

            Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
        attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`.

            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
            heads.
    NÚtext_embedsr   r    Ú
attentions)r!   r"   r#   r$   r-   r   r%   r&   r'   r   r    r   r.   r(   r)   r*   r,   r,   Í   sh   … ñð* 04€K�˜%×+Ñ+Ñ,Ó3Ø59Ð�x × 1Ñ 1Ñ2Ó9Ø8<€M�8˜E %×"3Ñ"3Ñ4Ñ5Ó<Ø59€J�˜˜u×0Ñ0Ñ1Ñ2Ô9r)   r,   c                   ó  — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eej                     ed<   dZeej                     ed<   dZeej                     ed<   dZeed<   dZeed	<   d
ee   fd„Zy)ÚAlignOutputaÊ  
    Args:
        loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `return_loss` is `True`):
            Contrastive loss for image-text similarity.
        logits_per_image:(`torch.FloatTensor` of shape `(image_batch_size, text_batch_size)`):
            The scaled dot product scores between `image_embeds` and `text_embeds`. This represents the image-text
            similarity scores.
        logits_per_text:(`torch.FloatTensor` of shape `(text_batch_size, image_batch_size)`):
            The scaled dot product scores between `text_embeds` and `image_embeds`. This represents the text-image
            similarity scores.
        text_embeds(`torch.FloatTensor` of shape `(batch_size, output_dim`):
            The text embeddings obtained by applying the projection layer to the pooled output of [`AlignTextModel`].
        image_embeds(`torch.FloatTensor` of shape `(batch_size, output_dim`):
            The output of [`AlignVisionModel`].
        text_model_output(`BaseModelOutputWithPoolingAndCrossAttentions`):
            The output of the [`AlignTextModel`].
        vision_model_output(`BaseModelOutputWithPoolingAndNoAttention`):
            The output of the [`AlignVisionModel`].
    NÚlossÚlogits_per_imageÚlogits_per_textr-   r   Útext_model_outputÚvision_model_outputÚreturnc                 óH   ‡ — t        ˆ fd„‰ j                  «       D «       «      S )Nc              3   ód   •K  — | ]'  }|d vr‰|   nt        ‰|«      j                  «       –— Œ) y­w))r4   r5   N)ÚgetattrÚto_tuple)Ú.0ÚkÚselfs     €r*   ú	<genexpr>z'AlignOutput.to_tuple.<locals>.<genexpr>	  s=   øè ø€ ò 
àð Ð LÑLˆD�ŠGÔRYÐZ^Ð`aÓRb×RkÑRkÓRmÓmñ
ùs   ƒ-0)ÚtupleÚkeys©r=   s   `r*   r:   zAlignOutput.to_tuple  s#   ø€ Üó 
à—Y‘Y“[ô
ó 
ð 	
r)   )r!   r"   r#   r$   r1   r   r%   r&   r'   r2   r3   r-   r   r4   r   r5   r   r   r   r:   r(   r)   r*   r0   r0   ê   s›   … ñð( )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø48Ð�h˜u×0Ñ0Ñ1Ó8Ø37€O�X˜e×/Ñ/Ñ0Ó7Ø/3€K�˜%×+Ñ+Ñ,Ó3Ø04€L�(˜5×,Ñ,Ñ-Ó4ØFJÐÐCÓJØDHÐÐAÓHð
˜% ™*ô 
r)   r0   Úlogitsr6   c                 ó–   — t         j                  j                  | t        j                  t        | «      | j                  ¬«      d¬«      S )N©Údevicegš™™™™™¹?)Úlabel_smoothing)r   Ú
functionalÚcross_entropyr%   ÚarangeÚlenrE   )rB   s    r*   Úcontrastive_lossrK     s5   € Ü�=‰=×&Ñ& v¬u¯|©|¼CÀ»KÐPV×P]ÑP]Ô/^ÐpsÐ&ÓtÐtr)   Ú
similarityc                 óZ   — t        | «      }t        | j                  «       «      }||z   dz  S )Ng       @)rK   Út)rL   Úcaption_lossÚ
image_losss      r*   Ú
align_lossrQ     s,   € Ü# JÓ/€LÜ! *§,¡,£.Ó1€JØ˜:Ñ%¨Ñ,Ð,r)   ÚconfigÚnum_channelsc                 óª   — | j                   }|| j                  z  }t        |t        ||dz  z   «      |z  |z  «      }|d|z  k  r||z  }t        |«      S )z<
    Round number of filters based on depth multiplier.
    é   gÍÌÌÌÌÌì?)Údepth_divisorÚwidth_coefficientÚmaxÚint)rR   rS   ÚdivisorÚnew_dims       r*   Úround_filtersr\     sf   € ð ×"Ñ"€GØ�F×,Ñ,Ñ,€LÜ�'œ3˜|¨g¸©kÑ9Ó:¸gÑEÈÑOÓP€Gð ��|Ñ#Ò#Ø�7Ñˆäˆw‹<Ðr)   Úkernel_sizeÚadjustc                 ó¢   — t        | t        «      r| | f} | d   dz  | d   dz  f}|r|d   dz
  |d   |d   dz
  |d   fS |d   |d   |d   |d   fS )aJ  
    Utility function to get the tuple padding value for the depthwise convolution.

    Args:
        kernel_size (`int` or `tuple`):
            Kernel size of the convolution layers.
        adjust (`bool`, *optional*, defaults to `True`):
            Adjusts padding value to apply to right and bottom sides of the input.
    r   rU   r   )Ú
isinstancerY   )r]   r^   Úcorrects      r*   Úcorrect_padrb   ,  s}   € ô �+œsÔ#Ø" KÐ0ˆà˜1‰~ Ñ" K°¡N°aÑ$7Ð8€GÙØ˜‘
˜Q‘ ¨¡
¨G°A©J¸©N¸GÀA¹JÐGÐGà˜‘
˜G A™J¨°©
°G¸A±JÐ?Ð?r)   c                   ó`   ‡ — e Zd ZdZdefˆ fd„Zdej                  dej                  fd„Zˆ xZ	S )ÚAlignVisionEmbeddingszL
    A module that corresponds to the stem module of the original work.
    rR   c                 óˆ  •— t         ‰| �  «        t        |d«      | _        t	        j
                  d¬«      | _        t	        j                  |j                  | j                  dddd¬«      | _	        t	        j                  | j                  |j                  |j                  ¬	«      | _        t        |j                     | _        y )
Né    )r   r   r   r   ©Úpaddingr	   rU   ÚvalidF©r]   Ústriderh   Úbias)ÚepsÚmomentum)ÚsuperÚ__init__r\   Úout_dimr   Ú	ZeroPad2drh   ÚConv2drS   ÚconvolutionÚBatchNorm2dÚbatch_norm_epsÚbatch_norm_momentumÚ	batchnormr
   Ú
hidden_actÚ
activation©r=   rR   Ú	__class__s     €r*   rp   zAlignVisionEmbeddings.__init__F  s�   ø€ Ü‰ÑÔä$ V¨RÓ0ˆŒÜ—|‘|¨LÔ9ˆŒÜŸ9™9Ø×Ñ §¡¸1ÀQÐPWÐ^cô
ˆÔô Ÿ™¨¯©¸&×:OÑ:OÐZ`×ZtÑZtÔuˆŒÜ  ×!2Ñ!2Ñ3ˆ�r)   Úpixel_valuesr6   c                 óŽ   — | j                  |«      }| j                  |«      }| j                  |«      }| j                  |«      }|S ©N)rh   rt   rx   rz   )r=   r}   Úfeaturess      r*   ÚforwardzAlignVisionEmbeddings.forwardQ  sA   € Ø—<‘< Ó-ˆØ×#Ñ# HÓ-ˆØ—>‘> (Ó+ˆØ—?‘? 8Ó,ˆàˆr)   )
r!   r"   r#   r$   r   rp   r%   ÚTensorr�   Ú__classcell__©r|   s   @r*   rd   rd   A  s0   ø„ ñð	4Ð0õ 	4ð E§L¡Lð °U·\±\÷ r)   rd   c                   ó.   ‡ — e Zd Z	 	 	 	 	 	 	 dˆ fd„	Zˆ xZS )ÚAlignVisionDepthwiseConv2dc	                 ó@   •— ||z  }	t         ‰
| �  ||	|||||||¬«	       y )N)	Úin_channelsÚout_channelsr]   rk   rh   ÚdilationÚgroupsrl   Úpadding_mode)ro   rp   )r=   rˆ   Údepth_multiplierr]   rk   rh   rŠ   rl   rŒ   r‰   r|   s             €r*   rp   z#AlignVisionDepthwiseConv2d.__init__\  s=   ø€ ð #Ð%5Ñ5ˆÜ‰ÑØ#Ø%Ø#ØØØØØØ%ð 	õ 
	
r)   )r   r	   r   r   r   TÚzeros)r!   r"   r#   rp   rƒ   r„   s   @r*   r†   r†   [  s$   ø„ ð ØØØØØØ÷
ñ 
r)   r†   c                   ól   ‡ — e Zd ZdZdedededefˆ fd„Zdej                  dej                  fd	„Z
ˆ xZS )
ÚAlignVisionExpansionLayerz_
    This corresponds to the expansion phase of each block in the original implementation.
    rR   Úin_dimrq   rk   c                 óÜ   •— t         ‰| �  «        t        j                  ||ddd¬«      | _        t        j
                  ||j                  ¬«      | _        t        |j                     | _
        y )Nr   ÚsameF©rˆ   r‰   r]   rh   rl   )Únum_featuresrm   )ro   rp   r   rs   Úexpand_convru   rv   Ú	expand_bnr
   ry   Ú
expand_act)r=   rR   r‘   rq   rk   r|   s        €r*   rp   z"AlignVisionExpansionLayer.__init__{  sZ   ø€ Ü‰ÑÔÜŸ9™9ØØ ØØØô
ˆÔô Ÿ™°WÀ&×BWÑBWÔXˆŒÜ  ×!2Ñ!2Ñ3ˆ�r)   r    r6   c                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S r   )r–   r—   r˜   ©r=   r    s     r*   r�   z!AlignVisionExpansionLayer.forward‡  s4   € à×(Ñ(¨Ó7ˆØŸ™ }Ó5ˆØŸ™¨Ó6ˆàÐr)   )r!   r"   r#   r$   r   rY   rp   r%   r&   r‚   r�   rƒ   r„   s   @r*   r�   r�   v  sH   ø„ ñð
4Ð0ð 
4¸#ð 
4Èð 
4ÐUXõ 
4ð U×%6Ñ%6ð ¸5¿<¹<÷ r)   r�   c            
       óp   ‡ — e Zd ZdZdededededef
ˆ fd„Zdej                  d	ej                  fd
„Zˆ xZS )ÚAlignVisionDepthwiseLayerzk
    This corresponds to the depthwise convolution phase of each block in the original implementation.
    rR   r‘   rk   r]   Úadjust_paddingc                 ób  •— t         ‰| �  «        || _        | j                  dk(  rdnd}t        ||¬«      }t	        j
                  |¬«      | _        t        ||||d¬«      | _        t	        j                  ||j                  |j                  ¬«      | _        t        |j                     | _        y )	NrU   ri   r“   )r^   rg   Frj   ©r•   rm   rn   )ro   rp   rk   rb   r   rr   Údepthwise_conv_padr†   Údepthwise_convru   rv   rw   Údepthwise_normr
   ry   Údepthwise_act)	r=   rR   r‘   rk   r]   r�   Úconv_padrh   r|   s	           €r*   rp   z"AlignVisionDepthwiseLayer.__init__–  sš   ø€ ô 	‰ÑÔØˆŒØ"Ÿk™k¨QÒ.‘7°FˆÜ˜k°.ÔAˆä"$§,¡,°wÔ"?ˆÔÜ8Ø °FÀHÐSXô
ˆÔô !Ÿn™nØ V×%:Ñ%:ÀV×E_ÑE_ô
ˆÔô $ F×$5Ñ$5Ñ6ˆÕr)   r    r6   c                 ó¬   — | j                   dk(  r| j                  |«      }| j                  |«      }| j                  |«      }| j	                  |«      }|S )NrU   )rk   r    r¡   r¢   r£   rš   s     r*   r�   z!AlignVisionDepthwiseLayer.forward¬  sT   € à�;‰;˜!ÒØ ×3Ñ3°MÓBˆMà×+Ñ+¨MÓ:ˆØ×+Ñ+¨MÓ:ˆØ×*Ñ*¨=Ó9ˆàÐr)   ©r!   r"   r#   r$   r   rY   Úboolrp   r%   r&   r‚   r�   rƒ   r„   s   @r*   rœ   rœ   ‘  sZ   ø„ ñð7à!ð7ð ð7ð ð	7ð
 ð7ð õ7ð,	 U×%6Ñ%6ð 	¸5¿<¹<÷ 	r)   rœ   c            	       ón   ‡ — e Zd ZdZd
dedededefˆ fd„Zdej                  dej                  fd	„Zˆ xZS )ÚAlignVisionSqueezeExciteLayerzl
    This corresponds to the Squeeze and Excitement phase of each block in the original implementation.
    rR   r‘   Ú
expand_dimÚexpandc                 óæ  •— t         ‰| �  «        |r|n|| _        t        dt	        ||j
                  z  «      «      | _        t        j                  d¬«      | _	        t        j                  | j                  | j                  dd¬«      | _        t        j                  | j                  | j                  dd¬«      | _        t        |j                     | _        t        j                   «       | _        y )Nr   )Úoutput_sizer“   )rˆ   r‰   r]   rh   )ro   rp   ÚdimrX   rY   Úsqueeze_expansion_ratioÚdim_ser   ÚAdaptiveAvgPool2dÚsqueezers   Úreducer«   r
   ry   Ú
act_reduceÚSigmoidÚ
act_expand)r=   rR   r‘   rª   r«   r|   s        €r*   rp   z&AlignVisionSqueezeExciteLayer.__init__¾  s´   ø€ Ü‰ÑÔÙ!'‘:¨VˆŒÜ˜!œS ¨&×*HÑ*HÑ!HÓIÓJˆŒä×+Ñ+¸Ô:ˆŒÜ—i‘iØŸ™ØŸ™ØØô	
ˆŒô —i‘iØŸ™ØŸ™ØØô	
ˆŒô ! ×!2Ñ!2Ñ3ˆŒÜŸ*™*›,ˆ�r)   r    r6   c                 óà   — |}| j                  |«      }| j                  |«      }| j                  |«      }| j                  |«      }| j	                  |«      }t        j                  ||«      }|S r   )r²   r³   r´   r«   r¶   r%   Úmul)r=   r    Úinputss      r*   r�   z%AlignVisionSqueezeExciteLayer.forwardÓ  sc   € ØˆØŸ™ ]Ó3ˆØŸ™ MÓ2ˆØŸ™¨Ó6ˆàŸ™ MÓ2ˆØŸ™¨Ó6ˆÜŸ	™	 &¨-Ó8ˆàÐr)   )Fr¦   r„   s   @r*   r©   r©   ¹  sH   ø„ ññ'Ð0ð '¸#ð 'È3ð 'ÐX\õ 'ð*
 U×%6Ñ%6ð 
¸5¿<¹<÷ 
r)   r©   c                   óŒ   ‡ — e Zd ZdZdedededededefˆ fd„Zd	e	j                  d
e	j                  de	j                  fd„Zˆ xZS )ÚAlignVisionFinalBlockLayerz[
    This corresponds to the final phase of each block in the original implementation.
    rR   r‘   rq   rk   Ú	drop_rateÚid_skipc                 ó  •— t         ‰| �  «        |dk(  xr | | _        t        j                  ||ddd¬«      | _        t        j                  ||j                  |j                  ¬«      | _	        t        j                  |¬«      | _        y )Nr   r“   Fr”   rŸ   )Úp)ro   rp   Úapply_dropoutr   rs   Úproject_convru   rv   rw   Ú
project_bnÚDropoutÚdropout)r=   rR   r‘   rq   rk   r¼   r½   r|   s          €r*   rp   z#AlignVisionFinalBlockLayer.__init__å  sz   ø€ ô 	‰ÑÔØ# q™[Ò8°¨[ˆÔÜŸI™IØØ ØØØô
ˆÔô Ÿ.™.Ø  f×&;Ñ&;Àf×F`ÑF`ô
ˆŒô —z‘z IÔ.ˆ�r)   Ú
embeddingsr    r6   c                 óŽ   — | j                  |«      }| j                  |«      }| j                  r| j                  |«      }||z   }|S r   )rÁ   rÂ   rÀ   rÄ   )r=   rÅ   r    s      r*   r�   z"AlignVisionFinalBlockLayer.forwardö  sG   € Ø×)Ñ)¨-Ó8ˆØŸ™¨Ó6ˆà×ÒØ ŸL™L¨Ó7ˆMØ)¨JÑ6ˆMàÐr)   ©r!   r"   r#   r$   r   rY   Úfloatr§   rp   r%   r&   r‚   r�   rƒ   r„   s   @r*   r»   r»   à  sj   ø„ ñð/Ø'ð/Ø14ð/Ø?Bð/ØLOð/Ø\að/Ølpõ/ð" %×"3Ñ"3ð ÀE×DUÑDUð ÐZ_×ZfÑZf÷ r)   r»   c                   ó€   ‡ — e Zd ZdZdededededededed	ed
efˆ fd„Zde	j                  de	j                  fd„Zˆ xZS )ÚAlignVisionBlocka�  
    This corresponds to the block module of original the EfficientNet vision encoder implementation.

    Args:
        config ([`AlignVisionConfig`]):
            Model configuration class.
        in_dim (`int`):
            Number of input channels.
        out_dim (`int`):
            Number of output channels.
        stride (`int`):
            Stride size to be used in convolution layers.
        expand_ratio (`int`):
            Expand ratio to set the output dimensions for the expansion and squeeze-excite layers.
        kernel_size (`int`):
            Kernel size for the depthwise convolution layer.
        drop_rate (`float`):
            Dropout rate to be used in the final phase of each block.
        id_skip (`bool`):
            Whether to apply dropout and sum the final hidden states with the input embeddings during the final phase
            of each block. Set to `True` for the first block of each stage.
        adjust_padding (`bool`):
            Whether to apply padding to only right and bottom side of the input kernel before the depthwise convolution
            operation, set to `True` for inputs with odd input sizes.
    rR   r‘   rq   rk   Úexpand_ratior]   r¼   r½   r�   c
                 ót  •— t         ‰| �  «        || _        | j                  dk7  rdnd| _        ||z  }
| j                  rt	        |||
|¬«      | _        t        || j                  r|
n||||	¬«      | _        t        |||
| j                  ¬«      | _	        t        || j                  r|
n|||||¬«      | _        y )Nr   TF)rR   r‘   rq   rk   )rR   r‘   rk   r]   r�   )rR   r‘   rª   r«   )rR   r‘   rq   rk   r¼   r½   )ro   rp   rË   r«   r�   Ú	expansionrœ   r¡   r©   Úsqueeze_exciter»   Ú
projection)r=   rR   r‘   rq   rk   rË   r]   r¼   r½   r�   Úexpand_in_dimr|   s              €r*   rp   zAlignVisionBlock.__init__  s¾   ø€ ô 	‰ÑÔØ(ˆÔØ"×/Ñ/°1Ò4‘d¸%ˆŒØ Ñ-ˆà�;Š;Ü6Ø f°mÈFôˆDŒNô 8ØØ$(§K¢K‘=°VØØ#Ø)ô
ˆÔô <Ø &°]È4Ï;É;ô
ˆÔô 5ØØ$(§K¢K‘=°VØØØØô
ˆ�r)   r    r6   c                 ó²   — |}| j                   dk7  r| j                  |«      }| j                  |«      }| j                  |«      }| j	                  ||«      }|S )Nr   )rË   rÍ   r¡   rÎ   rÏ   )r=   r    rÅ   s      r*   r�   zAlignVisionBlock.forwardE  s[   € Ø"ˆ
à×Ñ Ò!Ø ŸN™N¨=Ó9ˆMØ×+Ñ+¨MÓ:ˆð ×+Ñ+¨MÓ:ˆØŸ™¨
°MÓBˆØÐr)   rÇ   r„   s   @r*   rÊ   rÊ     sƒ   ø„ ñð4'
à!ð'
ð ð'
ð ð	'
ð
 ð'
ð ð'
ð ð'
ð ð'
ð ð'
ð õ'
ðR
 U×%6Ñ%6ð 
¸5¿<¹<÷ 
r)   rÊ   c            	       óf   ‡ — e Zd ZdZdefˆ fd„Z	 	 d	dej                  dee	   dee	   de
fd„Zˆ xZS )
ÚAlignVisionEncoderz·
    Forward propogates the embeddings through each vision encoder (EfficientNet) block.

    Args:
        config ([`AlignVisionConfig`]):
            Model configuration class.
    rR   c                 óê  •‡ ‡— t         ‰‰ �  «        |j                  ‰ _        ˆ fd„Št        |j                  «      }t        ˆfd„|j                  D «       «      }d}g }t        |«      D ]í  }t        ||j                  |   «      }t        ||j                  |   «      }|j                  |   }	|j                  |   }
|j                  |   }t         ‰|j                  |   «      «      D ]k  }|dk(  rdnd}|dkD  rdn|	}	|dkD  r|n|}||j                  v rdnd}|j                  |z  |z  }t        ||||	|
||||¬«	      }|j!                  |«       |dz  }Œm Œï t#        j$                  |«      ‰ _        y )Nc                 óZ   •— t        t        j                  ‰j                  | z  «      «      S r   )rY   ÚmathÚceilÚdepth_coefficient)Úrepeatsr=   s    €r*   Úround_repeatsz2AlignVisionEncoder.__init__.<locals>.round_repeats_  s"   ø€ ä”t—y‘y ×!7Ñ!7¸'Ñ!AÓBÓCÐCr)   c              3   ó.   •K  — | ]  } ‰|«      –— Œ y ­wr   r(   )r;   ÚnrÚ   s     €r*   r>   z.AlignVisionEncoder.__init__.<locals>.<genexpr>d  s   øè ø€ ÒL¨a™ q×)ÑLùs   ƒr   TFr   )	rR   r‘   rq   rk   r]   rË   r¼   r½   r�   )ro   rp   rØ   rJ   rˆ   ÚsumÚnum_block_repeatsÚranger\   r‰   ÚstridesÚkernel_sizesÚexpand_ratiosÚdepthwise_paddingÚdrop_connect_raterÊ   Úappendr   Ú
ModuleListÚblocks)r=   rR   Únum_base_blocksÚ
num_blocksÚcurr_block_numrç   Úir‘   rq   rk   r]   rË   Újr½   r�   r¼   ÚblockrÚ   r|   s   `                @€r*   rp   zAlignVisionEncoder.__init__[  sˆ  ú€ Ü‰ÑÔØ!'×!9Ñ!9ˆÔô	Dô ˜f×0Ñ0Ó1ˆÜÓL°6×3KÑ3KÔLÓLˆ
àˆØˆÜ�Ó'ò 	$ˆAÜ" 6¨6×+=Ñ+=¸aÑ+@ÓAˆFÜ# F¨F×,?Ñ,?ÀÑ,BÓCˆGØ—^‘^ AÑ&ˆFØ ×-Ñ-¨aÑ0ˆKØ!×/Ñ/°Ñ2ˆLä™=¨×)AÑ)AÀ!Ñ)DÓEÓFò $�Ø"# q¢&™$¨e�Ø !še™¨�Ø$%¨¢E™¨v�Ø*8¸F×<TÑ<TÑ*T¡ÐZ^�Ø"×4Ñ4°~ÑEÈ
ÑR�	ä(Ø!Ø!Ø#Ø!Ø +Ø!-Ø'Ø#Ø#1ô
�ð —‘˜eÔ$Ø !Ñ#‘ñ'$ð	$ô8 —m‘m FÓ+ˆ�r)   r    Úoutput_hidden_statesÚreturn_dictr6   c                 óš   — |r|fnd }| j                   D ]  } ||«      }|sŒ||fz  }Œ |st        d„ ||fD «       «      S t        ||¬«      S )Nc              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wr   r(   ©r;   Úvs     r*   r>   z-AlignVisionEncoder.forward.<locals>.<genexpr>”  s   è ø€ ÒX˜qÈ!É-œÑXùs   ‚Š)r   r    )rç   r?   r   )r=   r    rî   rï   Úall_hidden_statesrí   s         r*   r�   zAlignVisionEncoder.forward†  so   € ñ 1E˜]Ñ,È$Ðà—[‘[ò 	6ˆEÙ! -Ó0ˆMÚ#Ø! mÐ%5Ñ5Ñ!ð	6ñ
 ÜÑX ]Ð4EÐ$FÔXÓXÐXä-Ø+Ø+ô
ð 	
r)   )FT)r!   r"   r#   r$   r   rp   r%   r&   r   r§   r   r�   rƒ   r„   s   @r*   rÓ   rÓ   R  sW   ø„ ñð),Ð0õ ),ð\ 05Ø&*ñ	
à×(Ñ(ð
ð ' t™nð
ð ˜d‘^ð	
ð
 
2÷
r)   rÓ   c                   óÊ   ‡ — e Zd ZdZˆ fd„Z	 	 	 	 	 d
deej                     deej                     deej                     deej                     de	dej                  fd	„Zˆ xZS )ÚAlignTextEmbeddingszGConstruct the embeddings from word, position and token_type embeddings.c                 ó>  •— t         ‰| �  «        t        j                  |j                  |j
                  |j                  ¬«      | _        t        j                  |j                  |j
                  «      | _	        t        j                  |j                  |j
                  «      | _        t        j                  |j
                  |j                  ¬«      | _        t        j                  |j                  «      | _        t#        |dd«      | _        | j'                  dt)        j*                  |j                  «      j-                  d«      d¬«       | j'                  d	t)        j.                  | j0                  j3                  «       t(        j4                  ¬
«      d¬«       y )N)Úpadding_idx©rm   Úposition_embedding_typeÚabsoluteÚposition_ids)r   éÿÿÿÿF)Ú
persistentÚtoken_type_ids©Údtype)ro   rp   r   Ú	EmbeddingÚ
vocab_sizeÚhidden_sizeÚpad_token_idÚword_embeddingsÚmax_position_embeddingsÚposition_embeddingsÚtype_vocab_sizeÚtoken_type_embeddingsÚ	LayerNormÚlayer_norm_epsrÃ   Úhidden_dropout_probrÄ   r9   rú   Úregister_bufferr%   rI   r«   rŽ   rü   ÚsizeÚlongr{   s     €r*   rp   zAlignTextEmbeddings.__init__   s/  ø€ Ü‰ÑÔÜ!Ÿ|™|¨F×,=Ñ,=¸v×?QÑ?QÐ_e×_rÑ_rÔsˆÔÜ#%§<¡<°×0NÑ0NÐPV×PbÑPbÓ#cˆÔ Ü%'§\¡\°&×2HÑ2HÈ&×J\ÑJ\Ó%]ˆÔ"ô Ÿ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒÜ—z‘z &×"<Ñ"<Ó=ˆŒä'.¨vÐ7PÐR\Ó']ˆÔ$Ø×ÑØœEŸL™L¨×)GÑ)GÓH×OÑOÐPWÓXÐejð 	ô 	
ð 	×ÑØœeŸk™k¨$×*;Ñ*;×*@Ñ*@Ó*BÌ%Ï*É*ÔUÐbgð 	õ 	
r)   Ú	input_idsrÿ   rü   Úinputs_embedsÚpast_key_values_lengthr6   c                 óZ  — |�|j                  «       }n|j                  «       d d }|d   }|€| j                  d d …|||z   …f   }|€st        | d«      r-| j                  d d …d |…f   }|j	                  |d   |«      }	|	}n:t        j                  |t
        j                  | j                  j                  ¬«      }|€| j                  |«      }| j                  |«      }
||
z   }| j                  dk(  r| j                  |«      }||z  }| j                  |«      }| j                  |«      }|S )Nrý   r   rÿ   r   ©r  rE   rû   )r  rü   Úhasattrrÿ   r«   r%   rŽ   r  rE   r  r
  rú   r  r  rÄ   )r=   r  rÿ   rü   r  r  Úinput_shapeÚ
seq_lengthÚbuffered_token_type_idsÚ buffered_token_type_ids_expandedr
  rÅ   r  s                r*   r�   zAlignTextEmbeddings.forward³  sH  € ð Ð Ø#Ÿ.™.Ó*‰Kà'×,Ñ,Ó.¨s°Ð3ˆKà  ‘^ˆ
àÐØ×,Ñ,ªQÐ0FÈÐVlÑIlÐ0lÐ-lÑmˆLð
 Ð!Ü�tÐ-Ô.Ø*.×*=Ñ*=ºaÀÀ*À¸nÑ*MÐ'Ø3J×3QÑ3QÐR]Ð^_ÑR`ÐblÓ3mÐ0Ø!A‘ä!&§¡¨[ÄÇ
Á
ÐSW×SdÑSd×SkÑSkÔ!l�àÐ Ø ×0Ñ0°Ó;ˆMØ $× :Ñ :¸>Ó JÐà"Ð%:Ñ:ˆ
Ø×'Ñ'¨:Ò5Ø"&×":Ñ":¸<Ó"HÐØÐ-Ñ-ˆJØ—^‘^ JÓ/ˆ
Ø—\‘\ *Ó-ˆ
ØÐr)   )NNNNr   )r!   r"   r#   r$   rp   r   r%   Ú
LongTensorr&   rY   r‚   r�   rƒ   r„   s   @r*   rö   rö   �  s‹   ø„ ÙQô
ð* 15Ø59Ø37Ø59Ø&'ñ'à˜E×,Ñ,Ñ-ð'ð ! ×!1Ñ!1Ñ2ð'ð ˜u×/Ñ/Ñ0ð	'ð
   × 1Ñ 1Ñ2ð'ð !$ð'ð 
�‰÷'r)   rö   c                   óP  ‡ — e Zd Zdˆ fd„	Zdej
                  dej
                  fd„Z	 	 	 	 	 	 ddej
                  deej                     deej                     deej                     d	eej                     d
ee	e	ej                           dee
   de	ej
                     fd„Zˆ xZS )ÚAlignTextSelfAttentionc                 óâ  •— t         ‰| �  «        |j                  |j                  z  dk7  r2t	        |d«      s&t        d|j                  › d|j                  › d�«      ‚|j                  | _        t        |j                  |j                  z  «      | _        | j                  | j                  z  | _        t        j                  |j                  | j                  «      | _        t        j                  |j                  | j                  «      | _        t        j                  |j                  | j                  «      | _        t        j                  |j                  «      | _        |xs t#        |dd«      | _        | j$                  dk(  s| j$                  d	k(  rF|j&                  | _        t        j(                  d
|j&                  z  dz
  | j                  «      | _        |j,                  | _        y )Nr   Úembedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads (ú)rú   rû   Úrelative_keyÚrelative_key_queryrU   r   )ro   rp   r  Únum_attention_headsr  Ú
ValueErrorrY   Úattention_head_sizeÚall_head_sizer   ÚLinearÚqueryÚkeyÚvaluerÃ   Úattention_probs_dropout_probrÄ   r9   rú   r  r  Údistance_embeddingÚ
is_decoder©r=   rR   rú   r|   s      €r*   rp   zAlignTextSelfAttention.__init__ß  s�  ø€ Ü‰ÑÔØ×Ñ × :Ñ :Ñ:¸aÒ?ÌÐPVÐXhÔHiÜØ# F×$6Ñ$6Ð#7ð 8Ø ×4Ñ4Ð5°Qð8óð ð
 $*×#=Ñ#=ˆÔ Ü#& v×'9Ñ'9¸F×<VÑ<VÑ'VÓ#WˆÔ Ø!×5Ñ5¸×8PÑ8PÑPˆÔä—Y‘Y˜v×1Ñ1°4×3EÑ3EÓFˆŒ
Ü—9‘9˜V×/Ñ/°×1CÑ1CÓDˆŒÜ—Y‘Y˜v×1Ñ1°4×3EÑ3EÓFˆŒ
ä—z‘z &×"EÑ"EÓFˆŒØ'>ò (
Ä'ØÐ-¨zóC
ˆÔ$ð ×'Ñ'¨>Ò9¸T×=YÑ=YÐ]qÒ=qØ+1×+IÑ+IˆDÔ(Ü&(§l¡l°1°v×7UÑ7UÑ3UÐXYÑ3YÐ[_×[sÑ[sÓ&tˆDÔ#à ×+Ñ+ˆ�r)   Úxr6   c                 ó¤   — |j                  «       d d | j                  | j                  fz   }|j                  |«      }|j	                  dddd«      S )Nrý   r   rU   r   r	   )r  r#  r%  ÚviewÚpermute)r=   r/  Únew_x_shapes      r*   Útranspose_for_scoresz+AlignTextSelfAttention.transpose_for_scoresù  sL   € Ø—f‘f“h˜s �m t×'?Ñ'?À×AYÑAYÐ&ZÑZˆØ�F‰F�;ÓˆØ�y‰y˜˜A˜q !Ó$Ð$r)   r    Úattention_maskÚ	head_maskÚencoder_hidden_statesÚencoder_attention_maskÚpast_key_valueÚoutput_attentionsc                 ó$  — | j                  |«      }|d u}	|	r|�|d   }
|d   }|}�n |	rC| j                  | j                  |«      «      }
| j                  | j                  |«      «      }|}n»|�y| j                  | j                  |«      «      }
| j                  | j                  |«      «      }t	        j
                  |d   |
gd¬«      }
t	        j
                  |d   |gd¬«      }n@| j                  | j                  |«      «      }
| j                  | j                  |«      «      }| j                  |«      }|d u}| j                  r|
|f}t	        j                  ||
j                  dd«      «      }| j                  dk(  s| j                  dk(  �r—|j                  d   |
j                  d   }}|rDt	        j                  |dz
  t        j                  |j                  ¬	«      j                  dd«      }n@t	        j                  |t        j                  |j                  ¬	«      j                  dd«      }t	        j                  |t        j                  |j                  ¬	«      j                  dd«      }||z
  }| j!                  || j"                  z   dz
  «      }|j%                  |j&                  ¬
«      }| j                  dk(  rt	        j(                  d||«      }||z   }nE| j                  dk(  r6t	        j(                  d||«      }t	        j(                  d|
|«      }||z   |z   }|t+        j,                  | j.                  «      z  }|�||z   }t0        j2                  j5                  |d¬«      }| j7                  |«      }|�||z  }t	        j                  ||«      }|j9                  dddd«      j;                  «       }|j=                  «       d d | j>                  fz   }|j                  |«      }|r||fn|f}| j                  r||fz   }|S )Nr   r   rU   ©r®   rý   éþÿÿÿr!  r"  r  r   zbhld,lrd->bhlrzbhrd,lrd->bhlrr	   ) r(  r4  r)  r*  r%   Úcatr-  ÚmatmulÚ	transposerú   ÚshapeÚtensorr  rE   r1  rI   r,  r  Útor  ÚeinsumrÖ   Úsqrtr%  r   rG   ÚsoftmaxrÄ   r2  Ú
contiguousr  r&  )r=   r    r5  r6  r7  r8  r9  r:  Úmixed_query_layerÚis_cross_attentionÚ	key_layerÚvalue_layerÚquery_layerÚ	use_cacheÚattention_scoresÚquery_lengthÚ
key_lengthÚposition_ids_lÚposition_ids_rÚdistanceÚpositional_embeddingÚrelative_position_scoresÚrelative_position_scores_queryÚrelative_position_scores_keyÚattention_probsÚcontext_layerÚnew_context_layer_shapeÚoutputss                               r*   r�   zAlignTextSelfAttention.forwardþ  sç  € ð !ŸJ™J }Ó5Ðð
 3¸$Ð>Ðá .Ð"<à& qÑ)ˆIØ(¨Ñ+ˆKØ3ŠNÙØ×1Ñ1°$·(±(Ð;PÓ2QÓRˆIØ×3Ñ3°D·J±JÐ?TÓ4UÓVˆKØ3‰NØÐ'Ø×1Ñ1°$·(±(¸=Ó2IÓJˆIØ×3Ñ3°D·J±J¸}Ó4MÓNˆKÜŸ	™	 >°!Ñ#4°iÐ"@ÀaÔHˆIÜŸ)™) ^°AÑ%6¸Ð$DÈ!ÔL‰Kà×1Ñ1°$·(±(¸=Ó2IÓJˆIØ×3Ñ3°D·J±J¸}Ó4MÓNˆKà×/Ñ/Ð0AÓBˆà"¨$Ð.ˆ	Ø�?Š?ð (¨Ð5ˆNô !Ÿ<™<¨°Y×5HÑ5HÈÈRÓ5PÓQÐà×'Ñ'¨>Ò9¸T×=YÑ=YÐ]qÓ=qØ'2×'8Ñ'8¸Ñ';¸Y¿_¹_ÈQÑ=O˜*ˆLÙÜ!&§¡¨j¸1©nÄEÇJÁJÐWd×WkÑWkÔ!l×!qÑ!qØ˜ó"‘ô "'§¡¨lÄ%Ç*Á*ÐUb×UiÑUiÔ!j×!oÑ!oÐprÐtuÓ!v�Ü"Ÿ\™\¨*¼E¿J¹JÈ}×OcÑOcÔd×iÑiÐjkÐmoÓpˆNØ%¨Ñ6ˆHà#'×#:Ñ#:¸8Àd×FbÑFbÑ;bÐefÑ;fÓ#gÐ Ø#7×#:Ñ#:À×ARÑARÐ#:Ó#SÐ à×+Ñ+¨~Ò=Ü+0¯<©<Ð8HÈ+ÐWkÓ+lÐ(Ø#3Ð6NÑ#NÑ Ø×-Ñ-Ð1EÒEÜ16·±Ð>NÐP[Ð]qÓ1rÐ.Ü/4¯|©|Ð<LÈiÐYmÓ/nÐ,Ø#3Ð6TÑ#TÐWsÑ#sÐ à+¬d¯i©i¸×8PÑ8PÓ.QÑQÐØÐ%à/°.Ñ@Ðô Ÿ-™-×/Ñ/Ð0@ÀbÐ/ÓIˆð Ÿ,™, Ó7ˆð Ð Ø-°	Ñ9ˆOäŸ™ _°kÓBˆà%×-Ñ-¨a°°A°qÓ9×DÑDÓFˆØ"/×"4Ñ"4Ó"6°s¸Ð";¸t×?QÑ?QÐ>SÑ"SÐØ%×*Ñ*Ð+BÓCˆá6G�= /Ñ2ÈmÐM]ˆà�?Š?Ø Ð 1Ñ1ˆGØˆr)   r   ©NNNNNF)r!   r"   r#   rp   r%   r‚   r4  r   r&   r   r§   r�   rƒ   r„   s   @r*   r  r  Þ  så   ø„ õ,ð4% e§l¡lð %°u·|±|ó %ð 7;Ø15Ø=AØ>BØDHØ,1ñcà—|‘|ðcð ! ×!2Ñ!2Ñ3ðcð ˜E×-Ñ-Ñ.ð	cð
  (¨×(9Ñ(9Ñ:ðcð !)¨×):Ñ):Ñ ;ðcð !  u¨U×->Ñ->Ñ'?Ñ!@ÑAðcð $ D™>ðcð 
ˆu�|‰|Ñ	÷cr)   r  c                   ón   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  dej
                  fd„Zˆ xZS )ÚAlignTextSelfOutputc                 ó(  •— t         ‰| �  «        t        j                  |j                  |j                  «      | _        t        j                  |j                  |j                  ¬«      | _        t        j                  |j                  «      | _
        y ©Nrù   )ro   rp   r   r'  r  Údenser  r  rÃ   r  rÄ   r{   s     €r*   rp   zAlignTextSelfOutput.__init__f  s`   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3EÑ3EÓFˆŒ
ÜŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒÜ—z‘z &×"<Ñ"<Ó=ˆ�r)   r    Úinput_tensorr6   c                 ór   — | j                  |«      }| j                  |«      }| j                  ||z   «      }|S r   ©ra  rÄ   r  ©r=   r    rb  s      r*   r�   zAlignTextSelfOutput.forwardl  ó7   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆØŸ™ }°|Ñ'CÓDˆØÐr)   ©r!   r"   r#   rp   r%   r‚   r�   rƒ   r„   s   @r*   r^  r^  e  ó1   ø„ ô>ð U§\¡\ð ÀÇÁð ÐRW×R^ÑR^÷ r)   r^  Úeagerc                   ó  ‡ — e Zd Zdˆ fd„	Zd„ Z	 	 	 	 	 	 ddej                  deej                     deej                     deej                     deej                     dee	e	ej                           d	ee
   d
e	ej                     fd„Zˆ xZS )ÚAlignTextAttentionc                 óž   •— t         ‰| �  «        t        |j                     ||¬«      | _        t        |«      | _        t        «       | _        y )N©rú   )	ro   rp   Ú!ALIGN_TEXT_SELF_ATTENTION_CLASSESÚ_attn_implementationr=   r^  ÚoutputÚsetÚpruned_headsr.  s      €r*   rp   zAlignTextAttention.__init__z  sC   ø€ Ü‰ÑÔÜ5°f×6QÑ6QÑRØÐ,Cô
ˆŒ	ô *¨&Ó1ˆŒÜ›EˆÕr)   c                 ó>  — t        |«      dk(  ry t        || j                  j                  | j                  j                  | j
                  «      \  }}t        | j                  j                  |«      | j                  _        t        | j                  j                  |«      | j                  _        t        | j                  j                  |«      | j                  _	        t        | j                  j                  |d¬«      | j                  _        | j                  j                  t        |«      z
  | j                  _        | j                  j                  | j                  j                  z  | j                  _        | j
                  j                  |«      | _        y )Nr   r   r<  )rJ   r   r=   r#  r%  rr  r   r(  r)  r*  rp  ra  r&  Úunion)r=   ÚheadsÚindexs      r*   Úprune_headszAlignTextAttention.prune_heads‚  s  € Üˆu‹:˜Š?ØÜ7Ø�4—9‘9×0Ñ0°$·)±)×2OÑ2OÐQU×QbÑQbó
‰ˆˆuô
 -¨T¯Y©Y¯_©_¸eÓDˆ�	‰	ŒÜ*¨4¯9©9¯=©=¸%Ó@ˆ�	‰	ŒÜ,¨T¯Y©Y¯_©_¸eÓDˆ�	‰	ŒÜ.¨t¯{©{×/@Ñ/@À%ÈQÔOˆ�‰Ôð )-¯	©	×(EÑ(EÌÈEË
Ñ(Rˆ�	‰	Ô%Ø"&§)¡)×"?Ñ"?À$Ç)Á)×B_ÑB_Ñ"_ˆ�	‰	ÔØ ×-Ñ-×3Ñ3°EÓ:ˆÕr)   r    r5  r6  r7  r8  r9  r:  r6   c           	      óp   — | j                  |||||||«      }| j                  |d   |«      }	|	f|dd  z   }
|
S )Nr   r   )r=   rp  )r=   r    r5  r6  r7  r8  r9  r:  Úself_outputsÚattention_outputr[  s              r*   r�   zAlignTextAttention.forward”  sW   € ð —y‘yØØØØ!Ø"ØØó
ˆð  Ÿ;™; |°A¡¸ÓFÐØ#Ð%¨°Q°RÐ(8Ñ8ˆØˆr)   r   r\  )r!   r"   r#   rp   rw  r%   r‚   r   r&   r   r§   r�   rƒ   r„   s   @r*   rk  rk  y  sÆ   ø„ õ"ò;ð* 7;Ø15Ø=AØ>BØDHØ,1ñà—|‘|ðð ! ×!2Ñ!2Ñ3ðð ˜E×-Ñ-Ñ.ð	ð
  (¨×(9Ñ(9Ñ:ðð !)¨×):Ñ):Ñ ;ðð !  u¨U×->Ñ->Ñ'?Ñ!@ÑAðð $ D™>ðð 
ˆu�|‰|Ñ	÷r)   rk  c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚAlignTextIntermediatec                 ó  •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        |j                  t        «      rt        |j                     | _        y |j                  | _        y r   )ro   rp   r   r'  r  Úintermediate_sizera  r`   ry   Ústrr
   Úintermediate_act_fnr{   s     €r*   rp   zAlignTextIntermediate.__init__®  s]   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3KÑ3KÓLˆŒ
Ü�f×'Ñ'¬Ô-Ü'-¨f×.?Ñ.?Ñ'@ˆDÕ$à'-×'8Ñ'8ˆDÕ$r)   r    r6   c                 óJ   — | j                  |«      }| j                  |«      }|S r   )ra  r€  rš   s     r*   r�   zAlignTextIntermediate.forward¶  s&   € ØŸ
™
 =Ó1ˆØ×0Ñ0°Ó?ˆØÐr)   rg  r„   s   @r*   r|  r|  ­  s#   ø„ ô9ð U§\¡\ð °e·l±l÷ r)   r|  c                   ón   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  dej
                  fd„Zˆ xZS )ÚAlignTextOutputc                 ó(  •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        j                  |j
                  |j                  ¬«      | _        t        j                  |j                  «      | _        y r`  )ro   rp   r   r'  r~  r  ra  r  r  rÃ   r  rÄ   r{   s     €r*   rp   zAlignTextOutput.__init__¾  s`   ø€ Ü‰ÑÔÜ—Y‘Y˜v×7Ñ7¸×9KÑ9KÓLˆŒ
ÜŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒÜ—z‘z &×"<Ñ"<Ó=ˆ�r)   r    rb  r6   c                 ór   — | j                  |«      }| j                  |«      }| j                  ||z   «      }|S r   rd  re  s      r*   r�   zAlignTextOutput.forwardÄ  rf  r)   rg  r„   s   @r*   rƒ  rƒ  ½  rh  r)   rƒ  c                   ó  ‡ — e Zd Zˆ fd„Z	 	 	 	 	 	 ddej
                  deej                     deej                     deej                     deej                     deeeej                           dee	   d	eej
                     fd
„Z
d„ Zˆ xZS )ÚAlignTextLayerc                 óf  •— t         ‰| �  «        |j                  | _        d| _        t	        |«      | _        |j                  | _        |j                  | _        | j                  r,| j                  st        | › d�«      ‚t	        |d¬«      | _	        t        |«      | _        t        |«      | _        y )Nr   z> should be used as a decoder model if cross attention is addedrû   rm  )ro   rp   Úchunk_size_feed_forwardÚseq_len_dimrk  Ú	attentionr-  Úadd_cross_attentionr$  Úcrossattentionr|  Úintermediaterƒ  rp  r{   s     €r*   rp   zAlignTextLayer.__init__Í  s—   ø€ Ü‰ÑÔØ'-×'EÑ'EˆÔ$ØˆÔÜ+¨FÓ3ˆŒØ ×+Ñ+ˆŒØ#)×#=Ñ#=ˆÔ Ø×#Ò#Ø—?’?Ü  D 6Ð)gÐ!hÓiÐiÜ"4°VÐU_Ô"`ˆDÔÜ1°&Ó9ˆÔÜ% fÓ-ˆ�r)   r    r5  r6  r7  r8  r9  r:  r6   c           	      óÒ  — |�|d d nd }| j                  |||||¬«      }	|	d   }
| j                  r|	dd }|	d   }n|	dd  }d }| j                  rT|�Rt        | d«      st        d| › d�«      ‚|�|d	d  nd }| j	                  |
||||||«      }|d   }
||dd z   }|d   }|z   }t        | j                  | j                  | j                  |
«      }|f|z   }| j                  r|fz   }|S )
NrU   )r:  r9  r   r   rý   r�  z'If `encoder_hidden_states` are passed, z` has to be instantiated with cross-attention layers by setting `config.add_cross_attention=True`r=  )	r‹  r-  r  r$  r�  r   Úfeed_forward_chunkr‰  rŠ  )r=   r    r5  r6  r7  r8  r9  r:  Úself_attn_past_key_valueÚself_attention_outputsrz  r[  Úpresent_key_valueÚcross_attn_present_key_valueÚcross_attn_past_key_valueÚcross_attention_outputsÚlayer_outputs                    r*   r�   zAlignTextLayer.forwardÛ  s}  € ð :HÐ9S >°"°1Ñ#5ÐY]Ð Ø!%§¡ØØØØ/Ø3ð "0ó "
Ðð 2°!Ñ4Ðð �?Š?Ø,¨Q¨rÐ2ˆGØ 6°rÑ :Ñà,¨Q¨RÐ0ˆGà'+Ð$Ø�?Š?Ð4Ð@Ü˜4Ð!1Ô2Ü Ø=¸d¸Vð DDð Dóð ð @NÐ?Y¨°r°sÑ(;Ð_cÐ%Ø&*×&9Ñ&9Ø ØØØ%Ø&Ø)Ø!ó'Ð#ð  7°qÑ9ÐØÐ 7¸¸"Ð =Ñ=ˆGð ,CÀ2Ñ+FÐ(Ø 1Ð4PÑ PÐä0Ø×#Ñ# T×%AÑ%AÀ4×CSÑCSÐUeó
ˆð  �/ GÑ+ˆð �?Š?ØÐ!2Ð 4Ñ4ˆGàˆr)   c                 óL   — | j                  |«      }| j                  ||«      }|S r   )rŽ  rp  )r=   rz  Úintermediate_outputr—  s       r*   r�  z!AlignTextLayer.feed_forward_chunk  s,   € Ø"×/Ñ/Ð0@ÓAÐØ—{‘{Ð#6Ð8HÓIˆØÐr)   r\  )r!   r"   r#   rp   r%   r‚   r   r&   r   r§   r�   r�  rƒ   r„   s   @r*   r‡  r‡  Ì  sÇ   ø„ ô.ð" 7;Ø15Ø=AØ>BØDHØ,1ñ?à—|‘|ð?ð ! ×!2Ñ!2Ñ3ð?ð ˜E×-Ñ-Ñ.ð	?ð
  (¨×(9Ñ(9Ñ:ð?ð !)¨×):Ñ):Ñ ;ð?ð !  u¨U×->Ñ->Ñ'?Ñ!@ÑAð?ð $ D™>ð?ð 
ˆu�|‰|Ñ	ó?öBr)   r‡  c                   óD  ‡ — e Zd Zˆ fd„Z	 	 	 	 	 	 	 	 	 ddej
                  deej                     deej                     deej                     deej                     deeeej                           dee	   d	ee	   d
ee	   dee	   de
eej
                     ef   fd„Zˆ xZS )ÚAlignTextEncoderc                 óÐ   •— t         ‰| �  «        || _        t        j                  t        |j                  «      D �cg c]  }t        |«      ‘Œ c}«      | _        d| _	        y c c}w )NF)
ro   rp   rR   r   ræ   rß   Únum_hidden_layersr‡  ÚlayerÚgradient_checkpointing)r=   rR   Ú_r|   s      €r*   rp   zAlignTextEncoder.__init__$  sN   ø€ Ü‰ÑÔØˆŒÜ—]‘]ÄEÈ&×JbÑJbÓDcÖ#d¸q¤N°6Õ$:Ò#dÓeˆŒ
Ø&+ˆÕ#ùò $es   ½A#r    r5  r6  r7  r8  Úpast_key_valuesrM  r:  rî   rï   r6   c                 óš  — |	rdnd }|rdnd }|r| j                   j                  rdnd }| j                  r%| j                  r|rt        j                  d«       d}|rdnd }t        | j                  «      D ]¤  \  }}|	r||fz   }|�||   nd }|�||   nd }| j                  r/| j                  r#| j                  |j                  |||||||«      }n ||||||||«      }|d   }|r	||d   fz  }|sŒ|||d   fz   }| j                   j                  sŒœ||d   fz   }Œ¦ |	r||fz   }|
st        d„ |||||fD «       «      S t        |||||¬	«      S )
Nr(   zZ`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...Fr   rý   r   rU   c              3   ó$   K  — | ]  }|�|–— Œ
 y ­wr   r(   rò   s     r*   r>   z+AlignTextEncoder.forward.<locals>.<genexpr>l  s   è ø€ ò 
àð �=ô ñ
ùs   ‚)r   r¡  r    r.   Úcross_attentions)rR   rŒ  rŸ  ÚtrainingÚloggerÚwarning_onceÚ	enumeraterž  Ú_gradient_checkpointing_funcÚ__call__r?   r   )r=   r    r5  r6  r7  r8  r¡  rM  r:  rî   rï   rô   Úall_self_attentionsÚall_cross_attentionsÚnext_decoder_cacherë   Úlayer_moduleÚlayer_head_maskr9  Úlayer_outputss                       r*   r�   zAlignTextEncoder.forward*  sÎ  € ñ #7™B¸DÐÙ$5™b¸4ÐÙ%6¸4¿;¹;×;ZÒ;Z™rÐ`dÐà×&Ò&¨4¯=ª=ÙÜ×#Ñ#Øpôð "�	á#,™R°$ÐÜ(¨¯©Ó4ò #	V‰OˆAˆ|Ù#Ø$5¸Ð8HÑ$HÐ!à.7Ð.C˜i¨šlÈˆOØ3BÐ3N˜_¨QÒ/ÐTXˆNà×*Ò*¨t¯}ª}Ø $× AÑ AØ ×)Ñ)Ø!Ø"Ø#Ø)Ø*Ø"Ø%ó	!‘ñ !-Ø!Ø"Ø#Ø)Ø*Ø"Ø%ó!�ð *¨!Ñ,ˆMÙØ" }°RÑ'8Ð&:Ñ:Ð"Ú Ø&9¸]È1Ñ=MÐ<OÑ&OÐ#Ø—;‘;×2Ó2Ø+?À=ÐQRÑCSÐBUÑ+UÑ(ðG#	VñJ  Ø 1°]Ð4DÑ DÐáÜñ 
ð "Ø&Ø%Ø'Ø(ðô
ó 
ð 
ô 9Ø+Ø.Ø+Ø*Ø1ô
ð 	
r)   )	NNNNNNFFT)r!   r"   r#   rp   r%   r‚   r   r&   r   r§   r   r   r�   rƒ   r„   s   @r*   r›  r›  #  s  ø„ ô,ð 7;Ø15Ø=AØ>BØEIØ$(Ø,1Ø/4Ø&*ñS
à—|‘|ðS
ð ! ×!2Ñ!2Ñ3ðS
ð ˜E×-Ñ-Ñ.ð	S
ð
  (¨×(9Ñ(9Ñ:ðS
ð !)¨×):Ñ):Ñ ;ðS
ð " %¨¨e×.?Ñ.?Ñ(@Ñ"AÑBðS
ð ˜D‘>ðS
ð $ D™>ðS
ð ' t™nðS
ð ˜d‘^ðS
ð 
ˆu�U—\‘\Ñ"Ð$MÐMÑ	N÷S
r)   r›  c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚAlignTextPoolerc                 ó²   •— t         ‰| �  «        t        j                  |j                  |j                  «      | _        t        j                  «       | _        y r   )ro   rp   r   r'  r  ra  ÚTanhrz   r{   s     €r*   rp   zAlignTextPooler.__init__‚  s9   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3EÑ3EÓFˆŒ
ÜŸ'™'›)ˆ�r)   r    r6   c                 ó\   — |d d …df   }| j                  |«      }| j                  |«      }|S )Nr   )ra  rz   )r=   r    Úfirst_token_tensorÚpooled_outputs       r*   r�   zAlignTextPooler.forward‡  s6   € ð +ª1¨a¨4Ñ0ÐØŸ
™
Ð#5Ó6ˆØŸ™¨Ó6ˆØÐr)   rg  r„   s   @r*   r²  r²  �  s#   ø„ ô$ð
 U§\¡\ð °e·l±l÷ r)   r²  c                   ó"   — e Zd ZdZeZdZdZd„ Zy)ÚAlignPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    ÚalignTc                 ó&  — t        |t        j                  t        j                  f«      rn|j                  j
                  j                  d| j                  j                  ¬«       |j                  ��;|j                  j
                  j                  «        �nt        |t        «      rst        j                  j                  |j                  j                  «       |j                  j                  j
                  j                  «        d|j                  _        n’t        |t        j                   «      rx|j                  j
                  j                  d| j                  j                  ¬«       |j"                  �1|j                  j
                  |j"                     j                  «        t        |t        j$                  «      rJ|j                  j
                  j                  «        |j                  j
                  j'                  d«       yy)zInitialize the weightsg        )ÚmeanÚstdNTg      ð?)r`   r   r'  rs   ÚweightÚdataÚnormal_rR   Úinitializer_rangerl   Úzero_Ú
AlignModelÚinitÚxavier_uniform_Útext_projectionÚ_is_hf_initializedr  rø   r  Úfill_)r=   Úmodules     r*   Ú_init_weightsz"AlignPreTrainedModel._init_weightsš  sW  € ä�fœrŸy™y¬"¯)©)Ð4Ô5Ø�M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ�{‰{Ñ&Ø—‘× Ñ ×&Ñ&Ö(Ü˜¤
Ô+Ü�G‰G×#Ñ# F×$:Ñ$:×$AÑ$AÔBØ×"Ñ"×'Ñ'×,Ñ,×2Ñ2Ô4Ø8<ˆF×"Ñ"Õ5Ü˜¤§¡Ô-Ø�M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ×!Ñ!Ð-Ø—‘×"Ñ" 6×#5Ñ#5Ñ6×<Ñ<Ô>Ü�fœbŸl™lÔ+Ø�K‰K×Ñ×"Ñ"Ô$Ø�M‰M×Ñ×$Ñ$ SÕ)ð ,r)   N)	r!   r"   r#   r$   r   Úconfig_classÚbase_model_prefixÚsupports_gradient_checkpointingrÊ  r(   r)   r*   r¹  r¹  �  s   „ ñð
 €LØÐØ&*Ð#ó*r)   r¹  z@The text model from ALIGN without any head or projection on top.c                   ól  ‡ — e Zd ZeZdgZddedefˆ fd„Zd„ Zd„ Z	 e
e«       eee¬«      	 	 	 	 	 	 	 	 	 ddeej                      d	eej                      d
eej                      deej                      deej                      deej                      dee   dee   dee   deeef   fd„«       «       Zˆ xZS )ÚAlignTextModelrö   rR   Úadd_pooling_layerc                 óº   •— t         ‰| �  |«       || _        t        |«      | _        t        |«      | _        |rt        |«      nd | _        | j                  «        y r   )
ro   rp   rR   rö   rÅ   r›  Úencoderr²  ÚpoolerÚ	post_init)r=   rR   rÐ  r|   s      €r*   rp   zAlignTextModel.__init__µ  sK   ø€ Ü‰Ñ˜Ô ØˆŒä-¨fÓ5ˆŒÜ'¨Ó/ˆŒá1B”o fÔ-ÈˆŒð 	�‰Õr)   c                 ó.   — | j                   j                  S r   ©rÅ   r  rA   s    r*   Úget_input_embeddingsz#AlignTextModel.get_input_embeddingsÁ  s   € Ø�‰×.Ñ.Ð.r)   c                 ó&   — || j                   _        y r   rÖ  )r=   r*  s     r*   Úset_input_embeddingsz#AlignTextModel.set_input_embeddingsÄ  s   € Ø*/ˆ�‰Õ'r)   ©Úoutput_typerË  r  r5  rÿ   rü   r6  r  r:  rî   rï   r6   c
                 ó$  — |�|n| j                   j                  }|�|n| j                   j                  }|	�|	n| j                   j                  }	|�|�t	        d«      ‚|�#| j                  ||«       |j                  «       }
n!|�|j                  «       dd }
nt	        d«      ‚|
\  }}|�|j                  n|j                  }|€t        j                  ||f|¬«      }|€pt        | j                  d«      r4| j                  j                  dd…d|…f   }|j                  ||«      }|}n&t        j                  |
t        j                  |¬«      }| j!                  ||
«      }| j#                  || j                   j$                  «      }| j                  ||||¬«      }| j'                  ||||||	¬	«      }|d
   }| j(                  �| j)                  |«      nd}|	s
||f|dd z   S t+        |||j,                  |j.                  |j0                  ¬«      S )a?  
        Returns:

        Examples:

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

        >>> model = AlignTextModel.from_pretrained("kakaobrain/align-base")
        >>> tokenizer = AutoTokenizer.from_pretrained("kakaobrain/align-base")

        >>> inputs = tokenizer(["a photo of a cat", "a photo of a dog"], padding=True, return_tensors="pt")

        >>> outputs = model(**inputs)
        >>> last_hidden_state = outputs.last_hidden_state
        >>> pooled_output = outputs.pooler_output  # pooled (EOS token) states
        ```NzDYou cannot specify both input_ids and inputs_embeds at the same timerý   z5You have to specify either input_ids or inputs_embedsrD   rÿ   r  )r  rü   rÿ   r  )r5  r6  r:  rî   rï   r   r   )r   Úpooler_outputr    r.   r¤  )rR   r:  rî   Úuse_return_dictr$  Ú%warn_if_padding_and_no_attention_maskr  rE   r%   Úonesr  rÅ   rÿ   r«   rŽ   r  Úget_extended_attention_maskÚget_head_maskr�  rÒ  rÓ  r   r    r.   r¤  )r=   r  r5  rÿ   rü   r6  r  r:  rî   rï   r  Ú
batch_sizer  rE   r  r  Úextended_attention_maskÚembedding_outputÚencoder_outputsÚsequence_outputr·  s                        r*   r�   zAlignTextModel.forwardÇ  s<  € ð> 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐ  ]Ð%>ÜÐcÓdÐdØÐ"Ø×6Ñ6°yÀ.ÔQØ#Ÿ.™.Ó*‰KØÐ&Ø'×,Ñ,Ó.¨s°Ð3‰KäÐTÓUÐUà!,Ñˆ
�JØ%.Ð%:�×!Ò!À×@TÑ@TˆàÐ!Ü"ŸZ™Z¨*°jÐ)AÈ6ÔRˆNàÐ!Ü�t—‘Ð(8Ô9Ø*.¯/©/×*HÑ*HÊÈKÈZÈKÈÑ*XÐ'Ø3J×3QÑ3QÐR\Ð^hÓ3iÐ0Ø!A‘ä!&§¡¨[ÄÇ
Á
ÐSYÔ!Z�ð 15×0PÑ0PÐQ_ÐalÓ0mÐð ×&Ñ& y°$·+±+×2OÑ2OÓPˆ	àŸ?™?ØØ%Ø)Ø'ð	 +ó 
Ðð Ÿ,™,ØØ2ØØ/Ø!5Ø#ð 'ó 
ˆð *¨!Ñ,ˆØ8<¿¹Ð8O˜Ÿ™ OÔ4ÐUYˆáØ# ]Ð3°oÀaÀbÐ6IÑIÐIä;Ø-Ø'Ø)×7Ñ7Ø&×1Ñ1Ø,×=Ñ=ô
ð 	
r)   ©T©	NNNNNNNNN)r!   r"   r#   r   rË  Ú_no_split_modulesr§   rp   r×  rÙ  r   ÚALIGN_TEXT_INPUTS_DOCSTRINGr   r   r   r%   r‚   r   r   r�   rƒ   r„   s   @r*   rÏ  rÏ  ­  s5  ø„ ð
 #€LØ.Ð/Ðñ
˜ð 
À4õ 
ò/ò0ñ +Ð+FÓGÙÐ+WÐfuÔvð -1Ø15Ø15Ø/3Ø,0Ø04Ø,0Ø/3Ø&*ñ`
à˜EŸL™LÑ)ð`
ð ! §¡Ñ.ð`
ð ! §¡Ñ.ð	`
ð
 ˜uŸ|™|Ñ,ð`
ð ˜EŸL™LÑ)ð`
ð   §¡Ñ-ð`
ð $ D™>ð`
ð ' t™nð`
ð ˜d‘^ð`
ð 
ˆuÐBÐBÑ	Cò`
ó wó Hô`
r)   rÏ  zBThe vision model from ALIGN without any head or projection on top.c                   óÐ   ‡ — e Zd ZeZdZdZdefˆ fd„Zdej                  fd„Z
 ee«       eee¬«      	 	 	 ddeej"                     dee   d	ee   deeef   fd
„«       «       Zˆ xZS )ÚAlignVisionModelr}   FrR   c                 óš  •— t         ‰| �  |«       || _        t        |«      | _        t        |«      | _        |j                  dk(  r't        j                  |j                  d¬«      | _        nN|j                  dk(  r't        j                  |j                  d¬«      | _        nt        d|j                  › �«      ‚| j                  «        y )Nr¼  T)Ú	ceil_moderX   z2config.pooling must be one of ['mean', 'max'] got )ro   rp   rR   rd   rÅ   rÓ   rÒ  Úpooling_typer   Ú	AvgPool2dÚ
hidden_dimrÓ  Ú	MaxPool2dr$  ÚpoolingrÔ  r{   s     €r*   rp   zAlignVisionModel.__init__5  s¤   ø€ Ü‰Ñ˜Ô ØˆŒÜ/°Ó7ˆŒÜ)¨&Ó1ˆŒð ×Ñ &Ò(ÜŸ,™, v×'8Ñ'8ÀDÔIˆD�KØ× Ñ  EÒ)ÜŸ,™, v×'8Ñ'8ÀDÔIˆD�KäÐQÐRX×R`ÑR`ÐQaÐbÓcÐcð 	�‰Õr)   r6   c                 óB   — | j                   j                  j                  S r   )Úvision_modelrÅ   rt   rA   s    r*   r×  z%AlignVisionModel.get_input_embeddingsF  s   € Ø× Ñ ×+Ñ+×7Ñ7Ð7r)   rÚ  rî   rï   c                 ó~  — |�|n| j                   j                  }|�|n| j                   j                  }|€t        d«      ‚| j	                  |«      }| j                  |||¬«      }|d   }| j                  |«      }|j                  |j                  dd «      }|s
||f|dd z   S t        |||j                  ¬«      S )aÞ  
        Returns:

        Examples:

        ```python
        >>> from PIL import Image
        >>> import requests
        >>> from transformers import AutoProcessor, AlignVisionModel

        >>> model = AlignVisionModel.from_pretrained("kakaobrain/align-base")
        >>> processor = AutoProcessor.from_pretrained("kakaobrain/align-base")

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> inputs = processor(images=image, return_tensors="pt")

        >>> outputs = model(**inputs)
        >>> last_hidden_state = outputs.last_hidden_state
        >>> pooled_output = outputs.pooler_output  # pooled CLS states
        ```Nz You have to specify pixel_values)rî   rï   r   rU   r   )r   rÝ  r    )rR   rî   rÞ  r$  rÅ   rÒ  rÓ  ÚreshaperA  r   r    )r=   r}   rî   rï   rå  ræ  r   r·  s           r*   r�   zAlignVisionModel.forwardI  sâ   € ð> %9Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐÜÐ?Ó@Ð@àŸ?™?¨<Ó8ÐØŸ,™,ØØ!5Ø#ð 'ó 
ˆð ,¨AÑ.ÐØŸ™Ð$5Ó6ˆà%×-Ñ-¨m×.AÑ.AÀ"À1Ð.EÓFˆáØ% }Ð5¸ÈÈÐ8KÑKÐKä7Ø/Ø'Ø)×7Ñ7ô
ð 	
r)   ©NNN)r!   r"   r#   r   rË  Úmain_input_namerÍ  rp   r   ÚModuler×  r   ÚALIGN_VISION_INPUTS_DOCSTRINGr   r   r   r%   r&   r§   r   r   r�   rƒ   r„   s   @r*   rí  rí  ,  s®   ø„ ð
 %€LØ$€OØ&+Ð#ðÐ0õ ð"8 b§i¡ió 8ñ +Ð+HÓIÙÐ+SÐbsÔtð 59Ø/3Ø&*ñ	7
à˜u×0Ñ0Ñ1ð7
ð ' t™nð7
ð ˜d‘^ð	7
ð
 
ˆuÐ>Ð>Ñ	?ò7
ó uó Jô7
r)   rí  c                   ó  ‡ — e Zd ZeZdefˆ fd„Z ee«      	 	 	 	 	 	 	 	 	 ddee	j                     dee	j                     dee	j                     dee	j                     dee	j                     dee	j                     d	ee   d
ee   dee   de	j                  fd„«       Z ee«      	 	 	 ddee	j                     d
ee   dee   de	j                  fd„«       Z ee«       eee¬«      	 	 	 	 	 	 	 	 	 	 	 ddee	j&                     dee	j                     dee	j                     dee	j                     dee	j                     dee	j                     dee	j                     dee   d	ee   d
ee   dee   deeef   fd„«       «       Zˆ xZS )rÃ  rR   c                 óÈ  •— t         ‰| �  |«       t        |j                  t        «      s"t        dt        |j                  «      › d�«      ‚t        |j                  t        «      s"t        dt        |j                  «      › d�«      ‚|j                  }|j                  }|j                  | _	        |j                  | _        t        |«      | _        t        |«      | _        t!        j"                  | j                  | j                  «      | _        t!        j&                  t)        j*                  | j,                  j.                  «      «      | _        | j3                  «        y )NzLconfig.text_config is expected to be of type AlignTextConfig but is of type ú.zPconfig.vision_config is expected to be of type AlignVisionConfig but is of type )ro   rp   r`   Útext_configr   Ú	TypeErrorÚtypeÚvision_configr   Úprojection_dimr  Útext_embed_dimrÏ  Ú
text_modelrí  rö  r   r'  rÆ  Ú	Parameterr%   rB  rR   Útemperature_init_valueÚtemperaturerÔ  )r=   rR   r   r  r|   s       €r*   rp   zAlignModel.__init__‰  s#  ø€ Ü‰Ñ˜Ô ä˜&×,Ñ,¬oÔ>ÜðÜ˜×+Ñ+Ó,Ð-¨Qð0óð ô
 ˜&×.Ñ.Ô0AÔBÜðÜ˜×-Ñ-Ó.Ð/¨qð2óð ð
 ×(Ñ(ˆØ×,Ñ,ˆà$×3Ñ3ˆÔØ)×5Ñ5ˆÔä(¨Ó5ˆŒÜ,¨]Ó;ˆÔä!Ÿy™y¨×)<Ñ)<¸d×>QÑ>QÓRˆÔÜŸ<™<¬¯©°T·[±[×5WÑ5WÓ(XÓYˆÔð 	�‰Õr)   r  r5  rÿ   rü   r6  r  r:  rî   rï   r6   c
                 ó  — |�|n| j                   j                  }|�|n| j                   j                  }|	�|	n| j                   j                  }	| j	                  |||||||||	¬«	      }
|
d   dd…ddd…f   }| j                  |«      }|S )a�  
        Returns:
            text_features (`torch.FloatTensor` of shape `(batch_size, output_dim`): The text embeddings obtained by
            applying the projection layer to the pooled output of [`AlignTextModel`].

        Examples:

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

        >>> model = AlignModel.from_pretrained("kakaobrain/align-base")
        >>> tokenizer = AutoTokenizer.from_pretrained("kakaobrain/align-base")

        >>> inputs = tokenizer(["a photo of a cat", "a photo of a dog"], padding=True, return_tensors="pt")
        >>> text_features = model.get_text_features(**inputs)
        ```N©	r  r5  rÿ   rü   r6  r  r:  rî   rï   r   )rR   r:  rî   rÞ  r  rÆ  )r=   r  r5  rÿ   rü   r6  r  r:  rî   rï   Útext_outputsr   Útext_featuress                r*   Úget_text_featureszAlignModel.get_text_features§  s®   € ð< 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà—‘ØØ)Ø)Ø%ØØ'Ø/Ø!5Ø#ð 'ó 

ˆð )¨™OªA¨q²!¨GÑ4ÐØ×,Ñ,Ð->Ó?ˆàÐr)   r}   c                 ó    — |�|n| j                   j                  }|�|n| j                   j                  }| j                  |||¬«      }|d   }|S )a9  
        Returns:
            image_features (`torch.FloatTensor` of shape `(batch_size, output_dim`): The image embeddings obtained by
            applying the projection layer to the pooled output of [`AlignVisionModel`].

        Examples:

        ```python
        >>> from PIL import Image
        >>> import requests
        >>> from transformers import AutoProcessor, AlignModel

        >>> model = AlignModel.from_pretrained("kakaobrain/align-base")
        >>> processor = AutoProcessor.from_pretrained("kakaobrain/align-base")

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> inputs = processor(images=image, return_tensors="pt")

        >>> image_features = model.get_image_features(**inputs)
        ```©r}   rî   rï   r   )rR   rî   rÞ  rö  )r=   r}   rî   rï   Úvision_outputsÚimage_featuress         r*   Úget_image_featureszAlignModel.get_image_featuresÜ  sf   € ð> %9Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà×*Ñ*Ø%Ø!5Ø#ð +ó 
ˆð (¨Ñ*ˆàÐr)   rÚ  Úreturn_lossc                 óŒ  — |	�|	n| j                   j                  }	|
�|
n| j                   j                  }
|�|n| j                   j                  }| j	                  ||
|¬«      }| j                  |||||||	|
|¬«	      }|d   }|d   dd…ddd…f   }| j                  |«      }||j                  ddd¬	«      z  }||j                  ddd¬	«      z  }t        j                  ||j                  «       «      | j                  z  }|j                  «       }d}|rt        |«      }|s||||||f}|�|f|z   S |S t        |||||||¬
«      S )a}  
        Returns:

        Examples:

        ```python
        >>> from PIL import Image
        >>> import requests
        >>> from transformers import AutoProcessor, AlignModel

        >>> model = AlignModel.from_pretrained("kakaobrain/align-base")
        >>> processor = AutoProcessor.from_pretrained("kakaobrain/align-base")

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> inputs = processor(
        ...     images=image, text=["a photo of a cat", "a photo of a dog"], return_tensors="pt", padding=True
        ... )

        >>> outputs = model(**inputs)
        >>> logits_per_image = outputs.logits_per_image  # this is the image-text similarity score
        >>> probs = logits_per_image.softmax(dim=1)  # we can take the softmax to get the label probabilities
        ```Nr  r  r   r   rU   rý   T)r¿   r®   Úkeepdim)r1   r2   r3   r-   r   r4   r5   )rR   r:  rî   rÞ  rö  r  rÆ  Únormr%   r?  rN   r	  rQ   r0   )r=   r  r}   r5  rÿ   rü   r6  r  r  r:  rî   rï   r  r  r   r-   r3   r2   r1   rp  s                       r*   r�   zAlignModel.forward	  s   € ðR 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà×*Ñ*Ø%Ø!5Ø#ð +ó 
ˆð —‘ØØ)Ø)Ø%ØØ'Ø/Ø!5Ø#ð 'ó 

ˆð & aÑ(ˆØ" 1‘o¢a¨ªA gÑ.ˆØ×*Ñ*¨;Ó7ˆð $ l×&7Ñ&7¸!ÀÈTÐ&7Ó&RÑRˆØ! K×$4Ñ$4°q¸bÈ$Ð$4Ó$OÑOˆô  Ÿ,™, {°L·N±NÓ4DÓEÈ×HXÑHXÑXˆØ*×,Ñ,Ó.ÐàˆÙÜ˜oÓ.ˆDáØ&¨¸ÀlÐT`ÐbpÐqˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEäØØ-Ø+Ø#Ø%Ø*Ø .ô
ð 	
r)   ré  rù  )NNNNNNNNNNN)r!   r"   r#   r   rË  rp   r   rë  r   r%   r‚   r§   r&   r  rü  r  ÚALIGN_INPUTS_DOCSTRINGr   r0   r  r   r   r�   rƒ   r„   s   @r*   rÃ  rÃ  …  sŒ  ø„ à€Lð˜{õ ñ< +Ð+FÓGð -1Ø15Ø15Ø/3Ø,0Ø04Ø,0Ø/3Ø&*ñ2à˜EŸL™LÑ)ð2ð ! §¡Ñ.ð2ð ! §¡Ñ.ð	2ð
 ˜uŸ|™|Ñ,ð2ð ˜EŸL™LÑ)ð2ð   §¡Ñ-ð2ð $ D™>ð2ð ' t™nð2ð ˜d‘^ð2ð 
×	Ñ	ò2ó Hð2ñh +Ð+HÓIð 59Ø/3Ø&*ñ	*à˜u×0Ñ0Ñ1ð*ð ' t™nð*ð ˜d‘^ð	*ð
 
×	Ñ	ò*ó Jð*ñX +Ð+AÓBÙ¨;À[ÔQð 15Ø48Ø15Ø15Ø/3Ø,0Ø04Ø&*Ø,0Ø/3Ø&*ñ[
à˜E×,Ñ,Ñ-ð[
ð ˜u×0Ñ0Ñ1ð[
ð ! §¡Ñ.ð	[
ð
 ! §¡Ñ.ð[
ð ˜uŸ|™|Ñ,ð[
ð ˜EŸL™LÑ)ð[
ð   §¡Ñ-ð[
ð ˜d‘^ð[
ð $ D™>ð[
ð ' t™nð[
ð ˜d‘^ð[
ð 
ˆu�kÐ!Ñ	"ò[
ó Ró Cô[
r)   rÃ  )r¹  rÏ  rí  rÃ  rè  )Or$   rÖ   Údataclassesr   Útypingr   r   r   r   r%   Útorch.utils.checkpointr   Úactivationsr
   Úmodeling_outputsr   r   r   r   Úmodeling_utilsr   Úpytorch_utilsr   r   r   Úutilsr   r   r   r   r   Úconfiguration_alignr   r   r   Ú
get_loggerr!   r¦  Ú_CHECKPOINT_FOR_DOCÚ_CONFIG_FOR_DOCÚALIGN_START_DOCSTRINGrë  rü  r  r   r,   r0   r‚   rK   rQ   rY   r\   r§   rb   rû  rd   rs   r†   r�   rœ   r©   r»   rÊ   rÓ   rö   r  r^  rn  rk  r|  rƒ  r‡  r›  r²  r¹  rÏ  rí  rÃ  Ú__all__r(   r)   r*   ú<module>r'     s  ðñ ã Ý !ß .Ó .ã Û Ý å !÷ó õ .ß lÑ l÷õ ÷ QÑ Pð 
ˆ×	Ñ	˜HÓ	%€à-Ð Ø€ðÐ ð0Ð ðd
!Ð ð5Ð ðp ô=˜[ó =ó ð=ð* ô:˜;ó :ó ð:ð8 ô!
�+ó !
ó ð!
ðLu˜UŸ\™\ð u¨e¯l©ló uð-˜5Ÿ<™<ð -¨E¯L©Ló -ðÐ+ð ¸3ó ñ @˜U 3¨ :Ñ.ð @¸ó @ô*˜BŸI™Iô ô4
 §¡ô 
ô6 §	¡	ô ô6$ §	¡	ô $ôP$ B§I¡Iô $ôN §¡ô ôBN�r—y‘yô NôbG
˜Ÿ™ô G
ôV=˜"Ÿ)™)ô =ôBC˜RŸY™Yô CôN˜"Ÿ)™)ô ð Ð#ð%Ð !ô0˜Ÿ™ô 0ôh˜BŸI™Iô ô �b—i‘iô ôS�R—Y‘Yô SônZ
�r—y‘yô Z
ô|�b—i‘iô ô*˜?ô *ñ: ØJØóôx
Ð)ó x
ó	ðx
ñv ØLØóôR
Ð+ó R
ó	ðR
ñj Ð+Ó,ô`
Ð%ó `
ó -ð`
òF W�r)   