Ë
    T^(h©~ ã            	       ó¼  — d Z ddlZddlmZ ddlmZ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 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mZmZm Z  ddl!m"Z"m#Z#m$Z$  ejJ                  e&«      Z'e"Z(dHde
jR                  de
jT                  dee+   fd„Z,	 dIde
jZ                  de
jT                  de
j\                  de+fd„Z/dId„Z0dZ1dZ2dZ3dZ4e G d„ de«      «       Z5e G d„ de«      «       Z6 G d „ d!ejn                  «      Z8 G d"„ d#ejn                  «      Z9 G d$„ d%ejn                  «      Z: G d&„ d'ejn                  «      Z; G d(„ d)ejn                  «      Z< G d*„ d+ejn                  «      Z= G d,„ d-ejn                  «      Z> G d.„ d/ejn                  «      Z? G d0„ d1ejn                  «      Z@ G d2„ d3ejn                  «      ZA G d4„ d5ejn                  «      ZB G d6„ d7e«      ZC G d8„ d9eC«      ZD G d:„ d;eC«      ZE ed<e1«       G d=„ d>eCe«      «       ZF G d?„ d@ejn                  «      ZG edAe1«       G dB„ dCeC«      «       ZH edDe1«       G dE„ dFeCe«      «       ZIg dG¢ZJy)JzPyTorch KOSMOS-2 model.é    N)Ú	dataclass)ÚAnyÚListÚOptionalÚTupleÚUnion)Únn)ÚCrossEntropyLossé   )ÚACT2FN)ÚGenerationMixin)ÚBaseModelOutputÚ)BaseModelOutputWithPastAndCrossAttentionsÚBaseModelOutputWithPoolingÚ!CausalLMOutputWithCrossAttentions)ÚPreTrainedModel)ÚModelOutputÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚloggingÚreplace_return_docstringsÚ	torch_inté   )ÚKosmos2ConfigÚKosmos2TextConfigÚKosmos2VisionConfigÚmaskÚdtypeÚtgt_lenc                 ó2  — | j                  «       \  }}|�|n|}| dd…dddd…f   j                  |d||«      j                  |«      }d|z
  }|j                  |j                  t        j
                  «      t	        j                  |«      j                  «      S )z_
    Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
    Nr   ç      ð?)ÚsizeÚexpandÚtoÚmasked_fillÚtorchÚboolÚfinfoÚmin)r   r   r   ÚbszÚsrc_lenÚexpanded_maskÚinverted_masks          új/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/kosmos2/modeling_kosmos2.pyÚ_expand_maskr/   3   s‰   € ð —9‘9“;�L€CˆØ Ð,‰g°'€Gàš˜D $ªÐ)Ñ*×1Ñ1°#°q¸'À7ÓK×NÑNÈuÓU€Mà˜-Ñ'€Mà×$Ñ$ ]×%5Ñ%5´e·j±jÓ%AÄ5Ç;Á;ÈuÓCU×CYÑCYÓZÐZó    Úinput_ids_shapeÚdeviceÚpast_key_values_lengthc                 ó  — | \  }}t        j                  ||ft        j                  |«      j                  |¬«      }t        j                  |j                  d«      |¬«      }|j                  ||dz   j                  |j                  d«      d«      k  d«       |j                  |«      }|dkD  r0t        j                  t        j                  ||||¬«      |gd¬«      }|dddd…dd…f   j                  |d|||z   «      S )zB
    Make causal mask used for bi-directional self-attention.
    )r2   éÿÿÿÿr   r   ©r   r2   ©ÚdimN)r&   Úfullr(   r)   Úaranger"   Úmasked_fill_Úviewr$   ÚcatÚzerosr#   )r1   r   r2   r3   r*   r   r   Ú	mask_conds           r.   Ú_make_causal_maskr@   A   sá   € ð #�L€CˆÜ�:‰:�w Ð(¬%¯+©+°eÓ*<×*@Ñ*@ÈÔP€DÜ—‘˜TŸY™Y r›]°6Ô:€IØ×Ñ�i 9¨q¡=×"6Ñ"6°t·y±yÀ³}ÀaÓ"HÑHÈ!ÔLØ�7‰7�5‹>€Dà Ò!Ü�y‰yœ%Ÿ+™+ gÐ/EÈUÐ[aÔbÐdhÐiÐoqÔrˆØ��dšAšqÐ Ñ!×(Ñ(¨¨a°¸'ÐDZÑ:ZÓ[Ð[r0   c                 ó¾   — | j                  |«      j                  «       }t        j                  |d¬«      j	                  |«      |z   |z  }|j                  «       |z   S )a  
    Replace non-padding symbols with their position numbers. Position numbers begin at padding_idx+1. Padding symbols
    are ignored. This is modified from fairseq's `utils.make_positions`.

    Args:
        x: torch.Tensor x:

    Returns: torch.Tensor
    r   r7   )ÚneÚintr&   ÚcumsumÚtype_asÚlong)Ú	input_idsÚpadding_idxr3   r   Úincremental_indicess        r.   Ú"create_position_ids_from_input_idsrJ   S   sW   € ð �<‰<˜Ó$×(Ñ(Ó*€DÜ Ÿ<™<¨°!Ô4×<Ñ<¸TÓBÐE[Ñ[Ð_cÑcÐØ×#Ñ#Ó%¨Ñ3Ð3r0   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 ([`Kosmos2Config`]): 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:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`CLIPImageProcessor.__call__`] for details.
        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.
        interpolate_pos_encoding (`bool`, *optional*, defaults `False`):
            Whether to interpolate the pre-trained position encodings.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
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)
        image_embeds: (`torch.FloatTensor` of shape `(batch_size, latent_query_num, hidden_size)`, *optional*):
            Sequence of hidden-states at the output of `Kosmos2ImageToTextProjection`.
        image_embeds_position_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to indicate the location in a sequence to insert the image features . Mask values selected in `[0,
            1]`:

            - 1 for places where to put the image features,
            - 0 for places that are not for image features (i.e. for text tokens).

        encoder_hidden_states  (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if
            the model is configured as a decoder.
        encoder_attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used in
            the cross-attention if the model is configured as a decoder. Mask values selected in `[0, 1]`:

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

        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**.

        cross_attn_head_mask (`torch.Tensor` of shape `(decoder_layers, decoder_attention_heads)`, *optional*):
            Mask to nullify selected heads of the cross-attention modules. Mask values selected in `[0, 1]`:

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

        past_key_values (`tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`):
            Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding.

            If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
            don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
            `decoder_input_ids` of shape `(batch_size, sequence_length)`.
        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        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)
        use_cache (`bool`, *optional*):
            If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
            `past_key_values`).
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
aT  
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`CLIPImageProcessor.__call__`] for details.
        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)
        image_embeds_position_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to indicate the location in a sequence to insert the image features . Mask values selected in `[0,
            1]`:

            - 1 for places where to put the image features,
            - 0 for places that are not for image features (i.e. for text tokens).

        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)
        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**.
        past_key_values (`tuple(tuple(torch.FloatTensor))` of length `config.n_layers` with each tuple having 4 tensors of shape `(batch_size, num_heads, sequence_length - 1, embed_size_per_head)`):
            Contains precomputed key and value hidden states of the attention blocks. Can be used to speed up decoding.

            If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that
            don't have their past key value states given to this model) of shape `(batch_size, 1)` instead of all
            `decoder_input_ids` of shape `(batch_size, sequence_length)`.
        image_embeds: (`torch.FloatTensor` of shape `(batch_size, latent_query_num, hidden_size)`, *optional*):
            Sequence of hidden-states at the output of `Kosmos2ImageToTextProjection`.
        inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        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)
        use_cache (`bool`, *optional*):
            If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
            `past_key_values`).
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        interpolate_pos_encoding (`bool`, *optional*, defaults `False`):
            Whether to interpolate the pre-trained position encodings.
        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
e
ej                           ed<   dZee
ej                        ed<   dZee
ej                        ed<   dZeej                     ed<   dZee
ej                        ed<   dZeed	<   d
e
e   fd„Zy)ÚKosmos2ModelOutputaº
  
    Base class for text model's outputs that also contains a pooling of the last hidden states.

    Args:
        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.
        image_embeds (`torch.FloatTensor` of shape `(batch_size, latent_query_num, hidden_size)`, *optional*):
            Sequence of hidden-states at the output of `Kosmos2ImageToTextProjection`.
        projection_attentions (`tuple(torch.FloatTensor)`, *optional*):
            Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`.

            Attentions weights given by `Kosmos2ImageToTextProjection`, after the attention softmax, used to compute
            the weighted average in the self-attention heads.
        vision_model_output(`BaseModelOutputWithPooling`, *optional*):
            The output of the [`Kosmos2VisionModel`].
        past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
            Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
            `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and optionally if
            `config.is_encoder_decoder=True` 2 additional tensors of shape `(batch_size, num_heads,
            encoder_sequence_length, embed_size_per_head)`.

            Contains pre-computed hidden-states (key and values in the self-attention blocks and optionally if
            `config.is_encoder_decoder=True` in the cross-attention blocks) that can be used (see `past_key_values`
            input) to speed up sequential decoding.
    NÚlast_hidden_stateÚpast_key_valuesÚhidden_statesÚ
attentionsÚimage_embedsÚprojection_attentionsÚvision_model_outputÚreturnc                 óH   ‡ — t        ˆ fd„‰ j                  «       D «       «      S )Nc              3   ód   •K  — | ]'  }|d vr‰|   nt        ‰|«      j                  «       –— Œ) y­w©)Útext_model_outputrS   N©ÚgetattrÚto_tuple©Ú.0ÚkÚselfs     €r.   ú	<genexpr>z.Kosmos2ModelOutput.to_tuple.<locals>.<genexpr>A  ó=   øè ø€ ò 
àð Ð LÑLˆD�ŠGÔRYÐZ^Ð`aÓRb×RkÑRkÓRmÓmñ
ùó   ƒ-0©ÚtupleÚkeys©r_   s   `r.   r[   zKosmos2ModelOutput.to_tuple@  ó#   ø€ Üó 
à—Y‘Y“[ô
ó 
ð 	
r0   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rM   r   r&   ÚFloatTensorÚ__annotations__rN   r   rO   rP   rQ   rR   rS   r   r   r[   © r0   r.   rL   rL     s¿   … ñ$ðL 6:Ð�x × 1Ñ 1Ñ2Ó9ØAE€O�X˜e E¨%×*;Ñ*;Ñ$<Ñ=Ñ>ÓEØ8<€M�8˜E %×"3Ñ"3Ñ4Ñ5Ó<Ø59€J�˜˜u×0Ñ0Ñ1Ñ2Ó9Ø04€L�(˜5×,Ñ,Ñ-Ó4Ø@DÐ˜8 E¨%×*;Ñ*;Ñ$<Ñ=ÓDØ6:ÐÐ3Ó:ð
˜% ™*ô 
r0   rL   c                   óh  — 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ej                           ed<   dZeeej                        ed<   dZeeej                        ed<   dZeej                     ed<   dZeeej                        ed	<   dZeed
<   dee   fd„Zy)Ú*Kosmos2ForConditionalGenerationModelOutputaR  
    Model output class for `Kosmos2ForConditionalGeneration`.

    Args:
        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).
        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.
        image_embeds (`torch.FloatTensor` of shape `(batch_size, latent_query_num, hidden_size)`, *optional*):
            Sequence of hidden-states at the output of `Kosmos2ImageToTextProjection`.
        projection_attentions (`tuple(torch.FloatTensor)`, *optional*):
            Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`.

            Attentions weights given by `Kosmos2ImageToTextProjection`, after the attention softmax, used to compute
            the weighted average in the self-attention heads.
        vision_model_output(`BaseModelOutputWithPooling`, *optional*):
            The output of the [`Kosmos2VisionModel`].
        past_key_values (`tuple(tuple(torch.FloatTensor))`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`):
            Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape
            `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and optionally if
            `config.is_encoder_decoder=True` 2 additional tensors of shape `(batch_size, num_heads,
            encoder_sequence_length, embed_size_per_head)`.

            Contains pre-computed hidden-states (key and values in the self-attention blocks and optionally if
            `config.is_encoder_decoder=True` in the cross-attention blocks) that can be used (see `past_key_values`
            input) to speed up sequential decoding.
    NÚlossÚlogitsrN   rO   rP   rQ   rR   rS   rT   c                 óH   ‡ — t        ˆ fd„‰ j                  «       D «       «      S )Nc              3   ód   •K  — | ]'  }|d vr‰|   nt        ‰|«      j                  «       –— Œ) y­wrW   rY   r\   s     €r.   r`   zFKosmos2ForConditionalGenerationModelOutput.to_tuple.<locals>.<genexpr>{  ra   rb   rc   rf   s   `r.   r[   z3Kosmos2ForConditionalGenerationModelOutput.to_tuplez  rg   r0   )rh   ri   rj   rk   rq   r   r&   rl   rm   rr   rN   r   rO   rP   rQ   rR   rS   r   r   r[   rn   r0   r.   rp   rp   G  sÓ   … ñ&ðP )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø*.€FˆH�U×&Ñ&Ñ'Ó.ØAE€O�X˜e E¨%×*;Ñ*;Ñ$<Ñ=Ñ>ÓEØ8<€M�8˜E %×"3Ñ"3Ñ4Ñ5Ó<Ø59€J�˜˜u×0Ñ0Ñ1Ñ2Ó9Ø04€L�(˜5×,Ñ,Ñ-Ó4Ø@DÐ˜8 E¨%×*;Ñ*;Ñ$<Ñ=ÓDØ6:ÐÐ3Ó:ð
˜% ™*ô 
r0   rp   c                   óž   ‡ — e Zd Zdefˆ fd„Zdej                  dededej                  fd„Zd
dej                  dej                  fd	„Z
ˆ xZS )ÚKosmos2VisionEmbeddingsÚconfigc                 óÚ  •— t         ‰| �  «        || _        |j                  | _        |j
                  | _        |j                  | _        t        j                  t        j                  | j                  «      «      | _        t        j                  |j                  | j                  | j                  | j                  d¬«      | _        | j
                  | j                  z  dz  | _        | j                  dz   | _        t        j"                  | j                   | j                  «      | _        | j'                  dt        j(                  | j                   «      j+                  d«      d¬«       y )NF)Úin_channelsÚout_channelsÚkernel_sizeÚstrideÚbiasé   r   Úposition_ids)r   r5   ©Ú
persistent)ÚsuperÚ__init__rw   Úhidden_sizeÚ	embed_dimÚ
image_sizeÚ
patch_sizer	   Ú	Parameterr&   ÚrandnÚclass_embeddingÚConv2dÚnum_channelsÚpatch_embeddingÚnum_patchesÚnum_positionsÚ	EmbeddingÚposition_embeddingÚregister_bufferr:   r#   ©r_   rw   Ú	__class__s     €r.   rƒ   z Kosmos2VisionEmbeddings.__init__ƒ  s	  ø€ Ü‰ÑÔØˆŒØ×+Ñ+ˆŒØ ×+Ñ+ˆŒØ ×+Ñ+ˆŒä!Ÿ|™|¬E¯K©K¸¿¹Ó,GÓHˆÔä!Ÿy™yØ×+Ñ+ØŸ™ØŸ™Ø—?‘?Øô 
ˆÔð !ŸO™O¨t¯©Ñ>À1ÑDˆÔØ!×-Ñ-°Ñ1ˆÔÜ"$§,¡,¨t×/AÑ/AÀ4Ç>Á>Ó"RˆÔØ×Ñ˜^¬U¯\©\¸$×:LÑ:LÓ-M×-TÑ-TÐU\Ó-]ÐjoÐÕpr0   Ú
embeddingsÚheightÚwidthrT   c                 óÒ  — |j                   d   dz
  }| j                  j                  j                  d«      }|j                   d   dz
  }t        j
                  j                  «       s%||k(  r ||k(  r| j                  | j                  «      S |dd…dd…f   }|dd…dd…f   }|j                   d   }	|| j                  z  }
|| j                  z  }t        |dz  «      }|j                  d|||	«      }|j                  dddd«      }t        j                  j                  ||
|fdd	¬
«      }|j                  dddd«      j                  dd|	«      }t	        j                   ||fd¬«      S )a   
        This method allows to interpolate the pre-trained position encodings, to be able to use the model on higher resolution
        images. This method is also adapted to support torch.jit tracing.

        Adapted from:
        - https://github.com/facebookresearch/dino/blob/de9ee3df6cf39fac952ab558447af1fa1365362a/vision_transformer.py#L174-L194, and
        - https://github.com/facebookresearch/dinov2/blob/e1277af2ba9496fbadf7aec6eba56e8d882d1e35/dinov2/models/vision_transformer.py#L179-L211
        r   r   Nr5   g      à?r   r~   ÚbicubicF)r"   ÚmodeÚalign_cornersr7   )Úshaper‘   ÚweightÚ	unsqueezer&   ÚjitÚ
is_tracingr   r‡   r   ÚreshapeÚpermuter	   Ú
functionalÚinterpolater<   r=   )r_   r•   r–   r—   rŽ   r‘   r�   Úclass_pos_embedÚpatch_pos_embedr8   Ú
new_heightÚ	new_widthÚsqrt_num_positionss                r.   Úinterpolate_pos_encodingz0Kosmos2VisionEmbeddings.interpolate_pos_encoding™  sv  € ð !×&Ñ& qÑ)¨AÑ-ˆØ!×4Ñ4×;Ñ;×EÑEÀaÓHÐØ*×0Ñ0°Ñ3°aÑ7ˆô �y‰y×#Ñ#Ô%¨+¸Ò*FÈ6ÐUZÊ?Ø×*Ñ*¨4×+<Ñ+<Ó=Ð=à,ªQ°°°¨UÑ3ˆØ,ªQ°±¨UÑ3ˆà×Ñ˜rÑ"ˆà˜tŸ™Ñ.ˆ
Ø˜TŸ_™_Ñ,ˆ	ä& }°cÑ'9Ó:ÐØ)×1Ñ1°!Ð5GÐI[Ð]`ÓaˆØ)×1Ñ1°!°Q¸¸1Ó=ˆäŸ-™-×3Ñ3ØØ˜iÐ(ØØð	 4ó 
ˆð *×1Ñ1°!°Q¸¸1Ó=×BÑBÀ1ÀbÈ#ÓNˆä�y‰y˜/¨?Ð;ÀÔCÐCr0   Úpixel_valuesc                 ó`  — |j                   \  }}}}|sJ|| j                  k7  s|| j                  k7  r,t        d|› d|› d| j                  › d| j                  › d�	«      ‚| j                  j                  j
                  }| j                  |j                  |¬«      «      }|j                  d«      j                  dd«      }| j                  j                  |dd«      }	t        j                  |	|gd¬	«      }
|r|
| j                  |
||«      z   }
|
S |
| j                  | j                  «      z   }
|
S )
NzInput image size (Ú*z) doesn't match model (ú).©r   r~   r   r5   r7   )rœ   r†   Ú
ValueErrorr�   r�   r   r$   ÚflattenÚ	transposerŠ   r#   r&   r=   rª   r‘   r   )r_   r«   rª   Ú
batch_sizeÚ_r–   r—   Útarget_dtypeÚpatch_embedsÚclass_embedsr•   s              r.   ÚforwardzKosmos2VisionEmbeddings.forwardÂ  s6  € Ø'3×'9Ñ'9Ñ$ˆ
�A�v˜uÙ'¨V°t·±Ò-FÈ%ÐSW×SbÑSbÒJbÜØ$ V H¨A¨e¨WÐ4KÈDÏOÉOÐK\Ð\]Ð^b×^mÑ^mÐ]nÐnpÐqóð ð ×+Ñ+×2Ñ2×8Ñ8ˆØ×+Ñ+¨L¯O©OÀ,¨OÓ,OÓPˆØ#×+Ñ+¨AÓ.×8Ñ8¸¸AÓ>ˆà×+Ñ+×2Ñ2°:¸qÀ"ÓEˆÜ—Y‘Y ¨lÐ;ÀÔCˆ
Ù#Ø# d×&CÑ&CÀJÐPVÐX]Ó&^Ñ^ˆJð Ðð $ d×&=Ñ&=¸d×>OÑ>OÓ&PÑPˆJØÐr0   ©F)rh   ri   rj   r   rƒ   r&   ÚTensorrC   rª   rl   r¸   Ú__classcell__©r”   s   @r.   rv   rv   ‚  se   ø„ ðqÐ2õ qð,'D°5·<±<ð 'DÈð 'DÐUXð 'DÐ]b×]iÑ]ió 'DñR E×$5Ñ$5ð ÐZ_×ZfÑZf÷ r0   rv   c                   óô   ‡ — e Zd ZdZˆ fd„Zdej                  dedefd„Z	 	 	 ddej                  de	ej                     d	e	ej                     d
e	e
   deej                  e	ej                     f   f
d„Zˆ xZS )ÚKosmos2VisionAttentionú=Multi-headed attention from 'Attention Is All You Need' paperc                 ó
  •— t         ‰| �  «        || _        |j                  | _        |j
                  | _        | j                  | j                  z  | _        | j                  | j                  z  | j                  k7  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                  «      | _        y )Nú;embed_dim must be divisible by num_heads (got `embed_dim`: ú and `num_heads`: r®   ç      à¿)r‚   rƒ   rw   r„   r…   Únum_attention_headsÚ	num_headsÚhead_dimr°   ÚscaleÚattention_dropoutÚdropoutr	   ÚLinearÚk_projÚv_projÚq_projÚout_projr“   s     €r.   rƒ   zKosmos2VisionAttention.__init__Ù  s  ø€ Ü‰ÑÔØˆŒØ×+Ñ+ˆŒØ×3Ñ3ˆŒØŸ™¨$¯.©.Ñ8ˆŒØ�=‰=˜4Ÿ>™>Ñ)¨T¯^©^Ò;ÜØMÈdÏnÉnÐM]ð ^Ø—N‘NÐ# 2ð'óð ð —]‘] DÑ(ˆŒ
Ø×/Ñ/ˆŒä—i‘i §¡°·±Ó?ˆŒÜ—i‘i §¡°·±Ó?ˆŒÜ—i‘i §¡°·±Ó?ˆŒÜŸ	™	 $§.¡.°$·.±.ÓAˆ�r0   ÚtensorÚseq_lenr*   c                 óŽ   — |j                  ||| j                  | j                  «      j                  dd«      j	                  «       S )Nr   r~   )r<   rÅ   rÆ   r²   Ú
contiguous)r_   rÏ   rÐ   r*   s       r.   Ú_shapezKosmos2VisionAttention._shapeì  s7   € Ø�{‰{˜3 ¨¯©¸¿¹ÓG×QÑQÐRSÐUVÓW×bÑbÓdÐdr0   rO   Úattention_maskÚcausal_attention_maskÚoutput_attentionsrT   c                 ó”  — |j                  «       \  }}}| j                  |«      | j                  z  }| j                  | j	                  |«      d|«      }	| j                  | j                  |«      d|«      }
|| j                  z  d| j                  f} | j                  |||«      j                  |Ž } |	j                  |Ž }	 |
j                  |Ž }
|	j                  d«      }t        j                  ||	j                  dd«      «      }|j                  «       || j                  z  ||fk7  r/t        d|| j                  z  ||f› d|j                  «       › �«      ‚|�{|j                  «       |d||fk7  r#t        d|d||f› d|j                  «       › �«      ‚|j                  || j                  ||«      |z   }|j                  || j                  z  ||«      }|�{|j                  «       |d||fk7  r#t        d|d||f› d|j                  «       › �«      ‚|j                  || j                  ||«      |z   }|j                  || j                  z  ||«      }t        j                  j                  |d¬«      }|r?|j                  || j                  ||«      }|j                  || j                  z  ||«      }nd}t        j                  j!                  || j                   | j"                  ¬	«      }t        j                  ||
«      }|j                  «       || j                  z  || j                  fk7  r7t        d
|| j                  || j                  f› d|j                  «       › �«      ‚|j                  || j                  || j                  «      }|j                  dd«      }|j%                  |||«      }| j'                  |«      }||fS )ú#Input shape: Batch x Time x Channelr5   r   r~   z$Attention weights should be of size ú	, but is Nú!Attention mask should be of size r7   ©ÚpÚtrainingz `attn_output` should be of size )r"   rÍ   rÇ   rÓ   rË   rÌ   rÅ   rÆ   r<   r&   Úbmmr²   r°   r	   r£   ÚsoftmaxrÉ   rÝ   r¡   rÎ   )r_   rO   rÔ   rÕ   rÖ   r*   r   r…   Úquery_statesÚ
key_statesÚvalue_statesÚ
proj_shaper+   Úattn_weightsÚattn_weights_reshapedÚ
attn_probsÚattn_outputs                    r.   r¸   zKosmos2VisionAttention.forwardï  sÕ  € ð #0×"4Ñ"4Ó"6ÑˆˆW�ið —{‘{ =Ó1°D·J±JÑ>ˆØ—[‘[ §¡¨]Ó!;¸RÀÓEˆ
Ø—{‘{ 4§;¡;¨}Ó#=¸rÀ3ÓGˆà˜DŸN™NÑ*¨B°·±Ð>ˆ
ØC�t—{‘{ <°¸#Ó>×CÑCÀZÐPˆØ$�Z—_‘_ jÐ1ˆ
Ø(�|×(Ñ(¨*Ð5ˆà—/‘/ !Ó$ˆÜ—y‘y ¨z×/CÑ/CÀAÀqÓ/IÓJˆà×ÑÓ 3¨¯©Ñ#7¸À'Ð"JÒJÜØ6¸¸d¿n¹nÑ8LÈgÐW^Ð7_Ð6`ð aØ ×%Ñ%Ó'Ð(ð*óð ð !Ð,Ø$×)Ñ)Ó+°°Q¸ÀÐ/IÒIÜ Ø7¸¸aÀÈ'Ð8RÐ7Sð TØ-×2Ñ2Ó4Ð5ð7óð ð (×,Ñ,¨S°$·.±.À'È7ÓSÐVkÑkˆLØ'×,Ñ,¨S°4·>±>Ñ-AÀ7ÈGÓTˆLàÐ%Ø×"Ñ"Ó$¨¨a°¸'Ð(BÒBÜ Ø7¸¸aÀÈ'Ð8RÐ7SÐS\Ð]k×]pÑ]pÓ]rÐ\sÐtóð ð (×,Ñ,¨S°$·.±.À'È7ÓSÐVdÑdˆLØ'×,Ñ,¨S°4·>±>Ñ-AÀ7ÈGÓTˆLä—}‘}×,Ñ,¨\¸rÐ,ÓBˆáð
 %1×$5Ñ$5°c¸4¿>¹>È7ÐT[Ó$\Ð!Ø0×5Ñ5°c¸D¿N¹NÑ6JÈGÐU\Ó]‰Là$(Ð!ä—]‘]×*Ñ*¨<¸4¿<¹<ÐRV×R_ÑR_Ð*Ó`ˆ
ä—i‘i 
¨LÓ9ˆà×ÑÓ #¨¯©Ñ"6¸ÀÇÁÐ!OÒOÜØ2°C¸¿¹ÈÐRV×R_ÑR_Ð3`Ð2að bØ×$Ñ$Ó&Ð'ð)óð ð
 "×&Ñ& s¨D¯N©N¸GÀTÇ]Á]ÓSˆØ!×+Ñ+¨A¨qÓ1ˆØ!×)Ñ)¨#¨w¸	ÓBˆà—m‘m KÓ0ˆàÐ1Ð1Ð1r0   )NNF)rh   ri   rj   rk   rƒ   r&   rº   rC   rÓ   r   r'   r   r¸   r»   r¼   s   @r.   r¾   r¾   Ö  s¥   ø„ ÙGôBð&e˜UŸ\™\ð e°Cð e¸có eð 26Ø8<Ø,1ñL2à—|‘|ðL2ð ! §¡Ñ.ðL2ð  (¨¯©Ñ5ð	L2ð
 $ D™>ðL2ð 
ˆu�|‰|˜X e§l¡lÑ3Ð3Ñ	4÷L2r0   r¾   c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚKosmos2VisionMLPc                 ó  •— t         ‰| �  «        || _        t        |j                     | _        t        j                  |j                  |j                  «      | _
        t        j                  |j                  |j                  «      | _        y ©N)r‚   rƒ   rw   r   Ú
hidden_actÚactivation_fnr	   rÊ   r„   Úintermediate_sizeÚfc1Úfc2r“   s     €r.   rƒ   zKosmos2VisionMLP.__init__@  sd   ø€ Ü‰ÑÔØˆŒÜ# F×$5Ñ$5Ñ6ˆÔÜ—9‘9˜V×/Ñ/°×1IÑ1IÓJˆŒÜ—9‘9˜V×5Ñ5°v×7IÑ7IÓJˆ�r0   rO   rT   c                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S rë   )rï   rí   rð   ©r_   rO   s     r.   r¸   zKosmos2VisionMLP.forwardG  s4   € ØŸ™ Ó/ˆØ×*Ñ*¨=Ó9ˆØŸ™ Ó/ˆØÐr0   )rh   ri   rj   rƒ   r&   rº   r¸   r»   r¼   s   @r.   ré   ré   ?  s$   ø„ ôKð U§\¡\ð °e·l±l÷ r0   ré   c                   ó    ‡ — e Zd Zdefˆ fd„Z	 d	dej                  dej                  dej                  dee   de	ej                     f
d„Zˆ xZS )
ÚKosmos2VisionEncoderLayerrw   c                 óD  •— t         ‰| �  «        |j                  | _        t	        |«      | _        t        j                  | j                  |j                  ¬«      | _	        t        |«      | _        t        j                  | j                  |j                  ¬«      | _        y ©N©Úeps)r‚   rƒ   r„   r…   r¾   Ú	self_attnr	   Ú	LayerNormÚlayer_norm_epsÚlayer_norm1ré   ÚmlpÚlayer_norm2r“   s     €r.   rƒ   z"Kosmos2VisionEncoderLayer.__init__P  sm   ø€ Ü‰ÑÔØ×+Ñ+ˆŒÜ/°Ó7ˆŒÜŸ<™<¨¯©¸F×<QÑ<QÔRˆÔÜ# FÓ+ˆŒÜŸ<™<¨¯©¸F×<QÑ<QÔRˆÕr0   rO   rÔ   rÕ   rÖ   rT   c                 óÎ   — |}| j                  |«      }| j                  ||||¬«      \  }}||z   }|}| j                  |«      }| j                  |«      }||z   }|f}|r||fz  }|S )aI  
        Args:
            hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
            attention_mask (`torch.FloatTensor`): attention mask of size
                `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
                `(config.encoder_attention_heads,)`.
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors of all attention layers. See `attentions` under
                returned tensors for more detail.
        )rO   rÔ   rÕ   rÖ   )rü   rù   rþ   rý   )r_   rO   rÔ   rÕ   rÖ   Úresidualrä   Úoutputss           r.   r¸   z!Kosmos2VisionEncoderLayer.forwardX  s’   € ð" !ˆà×(Ñ(¨Ó7ˆØ&*§n¡nØ'Ø)Ø"7Ø/ð	 '5ó '
Ñ#ˆ�|ð ! =Ñ0ˆà ˆØ×(Ñ(¨Ó7ˆØŸ™ Ó/ˆØ  =Ñ0ˆà Ð"ˆáØ˜�Ñ&ˆGàˆr0   r¹   )rh   ri   rj   r   rƒ   r&   rº   r   r'   r   rl   r¸   r»   r¼   s   @r.   rô   rô   O  sg   ø„ ðSÐ2õ Sð -2ñ&à—|‘|ð&ð Ÿ™ð&ð  %Ÿ|™|ð	&ð
 $ D™>ð&ð 
ˆu× Ñ Ñ	!÷&r0   rô   c                   ó¤   ‡ — e Zd ZdZdefˆ fd„Z	 	 	 	 	 d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 )ÚKosmos2VisionEncoderz¿
    Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a
    [`Kosmos2VisionEncoderLayer`].

    Args:
        config: Kosmos2VisionConfig
    rw   c                 óÐ   •— t         ‰| �  «        || _        t        j                  t        |j                  «      D �cg c]  }t        |«      ‘Œ c}«      | _        d| _	        y c c}w )NF)
r‚   rƒ   rw   r	   Ú
ModuleListÚrangeÚnum_hidden_layersrô   ÚlayersÚgradient_checkpointing©r_   rw   r´   r”   s      €r.   rƒ   zKosmos2VisionEncoder.__init__‹  sQ   ø€ Ü‰ÑÔØˆŒÜ—m‘mÔPUÐV\×VnÑVnÓPoÖ$pÈ1Ô%>¸vÕ%FÒ$pÓqˆŒØ&+ˆÕ#ùò %qs   ½A#rÔ   rÕ   rÖ   Úoutput_hidden_statesÚreturn_dictrT   c                 ó  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|rdnd}|rdnd}|}	t	        | j
                  «      D ]b  \  }
}|r||	fz   }| j                  r,| j                  r | j                  |j                  |	|||«      }n ||	|||¬«      }|d   }	|sŒZ||d   fz   }Œd |r||	fz   }|st        d„ |	||fD «       «      S t        |	||¬«      S )aÕ  
        Args:
            inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
                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.
            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)
            causal_attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
                Causal mask for the text model. 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)
            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.
        Nrn   )rÖ   r   r   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrë   rn   ©r]   Úvs     r.   r`   z/Kosmos2VisionEncoder.forward.<locals>.<genexpr>Ý  s   è ø€ Òe˜qÐWXÑWdœÑeùó   ‚Š)rM   rO   rP   )rw   rÖ   r  Úuse_return_dictÚ	enumerater  r	  rÝ   Ú_gradient_checkpointing_funcÚ__call__rd   r   )r_   Úinputs_embedsrÔ   rÕ   rÖ   r  r  Úencoder_statesÚall_attentionsrO   ÚidxÚencoder_layerÚlayer_outputss                r.   r¸   zKosmos2VisionEncoder.forward‘  sH  € ðL 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆá3™¸ˆÙ0™°dˆà%ˆÜ"+¨D¯K©KÓ"8ò 	FÑˆC�Ù#Ø!/°=Ð2BÑ!B�Ø×*Ò*¨t¯}ª}Ø $× AÑ AØ!×*Ñ*Ø!Ø"Ø)Ø%ó!‘ñ !.Ø!Ø"Ø)Ø&7ô	!�ð *¨!Ñ,ˆMâ Ø!/°=ÀÑ3CÐ2EÑ!E‘ð-	Fñ0  Ø+¨}Ð.>Ñ>ˆNáÜÑe ]°NÀNÐ$SÔeÓeÐeÜØ+¸>ÐVdô
ð 	
r0   ©NNNNN)rh   ri   rj   rk   r   rƒ   r   r&   rº   r'   r   r   r   r¸   r»   r¼   s   @r.   r  r  ‚  s–   ø„ ñð,Ð2õ ,ð 26Ø8<Ø,0Ø/3Ø&*ñO
ð ! §¡Ñ.ðO
ð  (¨¯©Ñ5ð	O
ð
 $ D™>ðO
ð ' t™nðO
ð ˜d‘^ðO
ð 
ˆu�oÐ%Ñ	&÷O
r0   r  c                   ó†   ‡ — e Zd Zdefˆ fd„Z	 	 	 	 	 d
deej                     dee   dee   dedee   de	e
ef   fd	„Zˆ xZS )ÚKosmos2VisionTransformerrw   c                 ó   •— t         ‰| �  «        || _        |j                  }t	        |«      | _        t        j                  ||j                  ¬«      | _	        t        |«      | _        t        j                  ||j                  ¬«      | _        y rö   )r‚   rƒ   rw   r„   rv   r•   r	   rú   rû   Úpre_layrnormr  ÚencoderÚpost_layernorm)r_   rw   r…   r”   s      €r.   rƒ   z!Kosmos2VisionTransformer.__init__æ  sj   ø€ Ü‰ÑÔØˆŒØ×&Ñ&ˆ	ä1°&Ó9ˆŒÜŸL™L¨¸×8MÑ8MÔNˆÔÜ+¨FÓ3ˆŒÜ Ÿl™l¨9¸&×:OÑ:OÔPˆÕr0   r«   rÖ   r  rª   r  rT   c                 óÌ  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|€t	        d«      ‚| j                  ||¬«      }| j                  |«      }| j                  ||||¬«      }|d   }|d d …dd d …f   }	| j                  |	«      }	|s
||	f|dd  z   S t        ||	|j                  |j                  ¬«      S )Nz You have to specify pixel_values)rª   )r  rÖ   r  r  r   r   )rM   Úpooler_outputrO   rP   )rw   rÖ   r  r  r°   r•   r   r!  r"  r   rO   rP   )
r_   r«   rÖ   r  rª   r  rO   Úencoder_outputsrM   Úpooled_outputs
             r.   r¸   z Kosmos2VisionTransformer.forwardð  s  € ð 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐÜÐ?Ó@Ð@àŸ™¨ÐOg˜ÓhˆØ×)Ñ)¨-Ó8ˆàŸ,™,Ø'Ø/Ø!5Ø#ð	 'ó 
ˆð ,¨AÑ.ÐØ)ª!¨Q²¨'Ñ2ˆØ×+Ñ+¨MÓ:ˆáØ% }Ð5¸ÈÈÐ8KÑKÐKä)Ø/Ø'Ø)×7Ñ7Ø&×1Ñ1ô	
ð 	
r0   ©NNNFN)rh   ri   rj   r   rƒ   r   r&   rl   r'   r   r   r   r¸   r»   r¼   s   @r.   r  r  ä  s„   ø„ ðQÐ2õ Qð 59Ø,0Ø/3Ø).Ø&*ñ'
à˜u×0Ñ0Ñ1ð'
ð $ D™>ð'
ð ' t™nð	'
ð
 #'ð'
ð ˜d‘^ð'
ð 
ˆuÐ0Ð0Ñ	1÷'
r0   r  c                   ó  ‡ — e Zd ZdZddededee   fˆ fd„Zddededee   fd„Zeddededee   fd„«       Z	 e
j                  «       	 	 	 	 dd	ee
j                     d
ee
j                     dedee
j                     fd„«       Zd„ Zˆ xZS )Ú(Kosmos2TextSinusoidalPositionalEmbeddingzDThis module produces sinusoidal positional embeddings of any length.r�   Úembedding_dimrH   c                 óŒ   •— t         ‰| �  «        d| _        || _        || _        | j                  || j                  z   ||«       y )Nr~   )r‚   rƒ   Úoffsetr*  rH   Úmake_weights)r_   r�   r*  rH   r”   s       €r.   rƒ   z1Kosmos2TextSinusoidalPositionalEmbedding.__init__  s@   ø€ Ü‰ÑÔØˆŒØ*ˆÔØ&ˆÔØ×Ñ˜-¨$¯+©+Ñ5°}ÀkÕRr0   Únum_embeddingsc                 óà   — | j                  |||«      }t        | d«      r;|j                  | j                  j                  | j                  j
                  ¬«      }| j                  d|d¬«       y )NÚweightsr6   Fr€   )Úget_embeddingÚhasattrr$   r0  r   r2   r’   )r_   r.  r*  rH   Úemb_weightss        r.   r-  z5Kosmos2TextSinusoidalPositionalEmbedding.make_weights'  s[   € Ø×(Ñ(¨¸ÈÓTˆÜ�4˜Ô#à%Ÿ.™.¨t¯|©|×/AÑ/AÈ$Ï,É,×J]ÑJ]˜.Ó^ˆKà×Ñ˜Y¨ÀÐÕFr0   c                 óâ  — |dz  }t        j                  d«      |dz
  z  }t        j                  t        j                  |t        j
                  ¬«      j                  «       | z  «      }t        j                  | t        j
                  ¬«      j                  «       j                  d«      |j                  d«      z  }t        j                  t        j                  |«      t        j                  |«      gd¬«      j                  | d«      }|dz  dk(  r-t        j                  |t        j                  | d«      gd¬«      }|�	d||dd…f<   |j                  t        j                  «       «      S )	zÊ
        Build sinusoidal embeddings.

        This matches the implementation in tensor2tensor, but differs slightly from the description in Section 3.5 of
        "Attention Is All You Need".
        r~   i'  r   r¯   r   r7   r5   N)ÚmathÚlogr&   Úexpr:   Úint64Úfloatrž   r=   ÚsinÚcosr<   r>   r$   Úget_default_dtype)r.  r*  rH   Úhalf_dimÚembs        r.   r1  z6Kosmos2TextSinusoidalPositionalEmbedding.get_embedding/  s  € ð ! AÑ%ˆÜ�h‰h�u‹o ¨A¡Ñ.ˆÜ�i‰iœŸ™ X´U·[±[ÔA×GÑGÓIÈSÈDÑPÓQˆÜ�l‰l˜>´·±Ô=×CÑCÓE×OÑOÐPQÓRÐUX×UbÑUbÐcdÓUeÑeˆÜ�i‰iœŸ™ 3›¬¯©°3«Ð8¸aÔ@×EÑEÀnÐVXÓYˆØ˜1Ñ Ò!ä—)‘)˜S¤%§+¡+¨n¸aÓ"@ÐAÀqÔIˆCØÐ"Ø"#ˆC�šQ�Ñà�v‰v”e×-Ñ-Ó/Ó0Ð0r0   rG   r  r3   r   c                 óv  — |�F|j                  «       \  }}|€[t        || j                  |«      j                  |j                  «      }n*|j                  «       d d \  }}|€| j                  ||«      }| j                  dz   |z   |z   }|| j                  j                  d«      kD  r4| j                  || j                  z   | j                  | j                  «       | j                  j                  d|j                  d«      «      j                  ||| j                  j                  d   «      j                  «       S )Nr5   r   r   )r"   rJ   rH   r$   r2   Ú&create_position_ids_from_inputs_embedsr0  r-  r,  r*  Úindex_selectr<   rœ   Údetach)r_   rG   r  r3   r   r*   rÐ   Úmax_poss           r.   r¸   z0Kosmos2TextSinusoidalPositionalEmbedding.forwardE  s'  € ð Ð Ø$Ÿ>™>Ó+‰LˆC�ØÐ#äAØ˜t×/Ñ/Ð1Gó ç‘"�Y×%Ñ%Ó&ñ ð )×-Ñ-Ó/°°Ð4‰LˆC�ØÐ#Ø#×JÑJÈ=ÐZpÓq�ð ×"Ñ" QÑ&¨Ñ0Ð3IÑIˆØ�T—\‘\×&Ñ& qÓ)Ò)Ø×Ñ˜g¨¯©Ñ3°T×5GÑ5GÈ×IYÑIYÔZà�|‰|×(Ñ(¨¨L×,=Ñ,=¸bÓ,AÓB×GÑGÈÈWÐVZ×VbÑVb×VhÑVhÐikÑVlÓm×tÑtÓvÐvr0   c                 ó0  — |j                  «       dd }|d   }t        j                  | j                  dz   || j                  z   dz   t        j                  |j
                  ¬«      }|j                  d«      j                  |«      j                  «       |z   S )z×
        We are provided embeddings directly. We cannot infer which are padded so just generate sequential position ids.

        Args:
            inputs_embeds: torch.Tensor

        Returns: torch.Tensor
        Nr5   r   r6   r   )	r"   r&   r:   rH   rF   r2   rž   r#   rÒ   )r_   r  r3   Úinput_shapeÚsequence_lengthr   s         r.   r@  zOKosmos2TextSinusoidalPositionalEmbedding.create_position_ids_from_inputs_embedsa  s�   € ð $×(Ñ(Ó*¨3¨BÐ/ˆØ% a™.ˆä—|‘|Ø×Ñ˜qÑ  /°D×4DÑ4DÑ"DÀqÑ"HÔPU×PZÑPZÐcp×cwÑcwô
ˆð ×%Ñ% aÓ(×/Ñ/°Ó<×GÑGÓIÐLbÑbÐbr0   rë   )NNr   N)rh   ri   rj   rk   rC   r   rƒ   r-  Ústaticmethodr1  r&   Úno_gradrº   r¸   r@  r»   r¼   s   @r.   r)  r)    só   ø„ ÙNñS cð S¸#ð SÈHÐUXÉMõ SñG¨3ð G¸sð GÐQYÐZ]ÑQ^ó Gð ñ1 cð 1¸#ð 1ÈHÐUXÉMò 1ó ð1ð( €U‡]�]ƒ_ð -1Ø04Ø&'Ø/3ñwà˜EŸL™LÑ)ðwð   §¡Ñ-ðwð !$ð	wð
 ˜uŸ|™|Ñ,òwó ðwö6cr0   r)  c                   óŠ  ‡ — e Zd ZdZ	 	 	 	 d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
	 	 	 	 	 ddej                  deej                     deeej                        deej                     deej                     ded
eej                  eej                     eeej                        f   fd„Zˆ xZS )ÚKosmosTextAttentionr¿   r…   rÅ   rÉ   Ú
is_decoderÚadd_inner_attn_layernormr}   c                 óN  •— t         ‰| �  «        || _        || _        || _        ||z  | _        | j
                  |z  | j                  k7  rt        d| j                  › d|› d�«      ‚| j
                  dz  | _        || _        t        j                  |||¬«      | _        t        j                  |||¬«      | _        t        j                  |||¬«      | _        t        j                  |||¬«      | _        d | _        |r't        j                   ||j"                  ¬«      | _        y y )NrÁ   rÂ   r®   rÃ   )r}   r÷   )r‚   rƒ   r…   rÅ   rÉ   rÆ   r°   ÚscalingrK  r	   rÊ   rË   rÌ   rÍ   rÎ   Úinner_attn_lnrú   rû   )	r_   rw   r…   rÅ   rÉ   rK  rL  r}   r”   s	           €r.   rƒ   zKosmosTextAttention.__init__w  s  ø€ ô 	‰ÑÔØ"ˆŒØ"ˆŒØˆŒØ! YÑ.ˆŒà�M‰M˜IÑ%¨$¯.©.Ò8ÜØMÈdÏnÉnÐM]Ø$ Y K¨rð3óð ð —}‘} dÑ*ˆŒØ$ˆŒä—i‘i 	¨9¸4Ô@ˆŒÜ—i‘i 	¨9¸4Ô@ˆŒÜ—i‘i 	¨9¸4Ô@ˆŒÜŸ	™	 )¨Y¸TÔBˆŒð "ˆÔÙ#Ü!#§¡¨i¸V×=RÑ=RÔ!SˆDÕð $r0   Ú
projectionrT   c                 ó¤   — |j                  «       d d | j                  | j                  fz   }|j                  |«      j	                  dddd«      }|S )Nr5   r   r~   r   r   )r"   rÅ   rÆ   r<   r¢   )r_   rP  Únew_projection_shapeÚnew_projections       r.   rÓ   zKosmosTextAttention._shape™  sO   € Ø)Ÿ™Ó0°°"Ð5¸¿¹ÈÏÉÐ8WÑWÐà#Ÿ™Ð)=Ó>×FÑFÀqÈ!ÈQÐPQÓRˆØÐr0   rO   Úencoder_hidden_statesÚpast_key_valuerÔ   Úlayer_head_maskrÖ   c                 ó²  — |du}|j                   dd \  }}	|�|n|}
|r/|r-|d   j                   d   |
j                   d   k(  r|d   }|d   }n|| j                  | j                  |
«      «      }| j                  | j                  |
«      «      }|�:|s8t	        j
                  |d   |gd¬«      }t	        j
                  |d   |gd¬«      }| j                  | j                  |«      | j                  z  «      }t	        j                  ||j                  dd«      «      }| j                  r||f}|j                  d«      }|�?|j                  «       |d|	|fk7  r#t        d|d|	|f› d	|j                  «       › �«      ‚||z   }t        j                  j                  |d¬«      }|�||z  }t        j                  j!                  || j                   | j"                  ¬
«      }t	        j                  ||«      }|j%                  dddd«      j'                  «       j)                  ||	d«      }| j*                  �| j+                  |«      }| j-                  |«      }|||fS )rØ   Nr~   r   r   r7   r5   éþÿÿÿrÚ   rÙ   rÛ   r   )rœ   rÓ   rË   rÌ   r&   r=   rÍ   rN  Úmatmulr²   rK  r"   r°   r	   r£   rß   rÉ   rÝ   r¢   rÒ   r<   rO  rÎ   )r_   rO   rT  rU  rÔ   rV  rÖ   Úis_cross_attentionr³   Ú
seq_lengthÚcurrent_statesrá   râ   rà   rä   r+   Úcontext_statesrç   s                     r.   r¸   zKosmosTextAttention.forwardŸ  sj  € ð 3¸$Ð>ÐØ!.×!4Ñ!4°R°aÐ!8Ñˆ
�Jð 3HÐ2SÑ.ÐYfˆñ ¡.°^ÀAÑ5F×5LÑ5LÈQÑ5OÐSa×SgÑSgÐhiÑSjÒ5jà'¨Ñ*ˆJØ)¨!Ñ,‰LàŸ™ T§[¡[°Ó%@ÓAˆJØŸ;™; t§{¡{°>Ó'BÓCˆLØÐ)Ñ2Dä"ŸY™Y¨°qÑ(9¸:Ð'FÈAÔN�
Ü$Ÿy™y¨.¸Ñ*;¸\Ð)JÐPQÔR�à—{‘{ 4§;¡;¨}Ó#=ÀÇÁÑ#LÓMˆÜ—|‘| L°*×2FÑ2FÀrÈ2Ó2NÓOˆà�?Š?ð )¨,Ð7ˆNà—/‘/ !Ó$ˆàÐ%Ø×"Ñ"Ó$¨°Q¸
ÀGÐ(LÒLÜ Ø7¸ÀQÈ
ÐT[Ð8\Ð7]Ð]fÐgu×gzÑgzÓg|Ðf}Ð~óð ð (¨.Ñ8ˆLä—}‘}×,Ñ,¨\¸rÐ,ÓBˆð Ð&Ø'¨/Ñ9ˆLä—}‘}×,Ñ,¨\¸T¿\¹\ÐTX×TaÑTaÐ,Óbˆô Ÿ™ l°LÓAˆà'×/Ñ/°°1°a¸Ó;×FÑFÓH×MÑMÈjÐZdÐfhÓiˆà×ÑÐ)Ø!×/Ñ/°Ó?ˆNà—m‘m NÓ3ˆà˜L¨.Ð8Ð8r0   )ç        FFT)NNNNF)rh   ri   rj   rk   rC   r9  r'   rƒ   r&   rº   rÓ   r   r   r¸   r»   r¼   s   @r.   rJ  rJ  s  s.  ø„ ÙGð Ø Ø).Øñ Tð ð Tð ð	 Tð
 ð Tð ð Tð #'ð Tð õ TðD §¡ð °%·,±,ó ð 9=Ø8<Ø15Ø26Ø"'ñH9à—|‘|ðH9ð  (¨¯©Ñ5ðH9ð !  u§|¡|Ñ!4Ñ5ð	H9ð
 ! §¡Ñ.ðH9ð " %§,¡,Ñ/ðH9ð  ðH9ð 
ˆu�|‰|˜X e§l¡lÑ3°X¸eÀEÇLÁLÑ>QÑ5RÐRÑ	S÷H9r0   rJ  c                   ó*   ‡ — e Zd Zdefˆ fd„Zd„ Zˆ xZS )ÚKosmos2TextFFNrw   c                 ó²  •— t         ‰| �  «        |j                  | _        t        |j                     | _        |j                  | _        t        j                  |j                  |j                  «      | _        t        j                  |j                  |j                  «      | _        t        j                  |j                  |j                  ¬«      | _        y rö   )r‚   rƒ   rÉ   r   Úactivation_functionrí   Úactivation_dropoutr	   rÊ   r…   Úffn_dimrï   rð   rú   rû   Úffn_layernormr“   s     €r.   rƒ   zKosmos2TextFFN.__init__ë  s�   ø€ Ü‰ÑÔà—~‘~ˆŒÜ# F×$>Ñ$>Ñ?ˆÔØ"(×";Ñ";ˆÔä—9‘9˜V×-Ñ-¨v¯~©~Ó>ˆŒÜ—9‘9˜VŸ^™^¨V×-=Ñ-=Ó>ˆŒäŸ\™\¨&¯.©.¸f×>SÑ>SÔTˆÕr0   c                 ób  — | j                  | j                  |«      «      }t        j                  j	                  || j
                  | j                  ¬«      }| j                  |«      }| j                  |«      }t        j                  j	                  || j                  | j                  ¬«      }|S )NrÛ   )	rí   rï   r	   r£   rÉ   rc  rÝ   re  rð   rò   s     r.   r¸   zKosmos2TextFFN.forward÷  s�   € Ø×*Ñ*¨4¯8©8°MÓ+BÓCˆÜŸ™×-Ñ-¨m¸t×?VÑ?VÐae×anÑanÐ-ÓoˆØ×*Ñ*¨=Ó9ˆØŸ™ Ó/ˆÜŸ™×-Ñ-¨m¸t¿|¹|ÐVZ×VcÑVcÐ-ÓdˆàÐr0   )rh   ri   rj   r   rƒ   r¸   r»   r¼   s   @r.   r`  r`  ê  s   ø„ ð
UÐ0õ 
Uör0   r`  c                   ó~  ‡ — e Zd Zdefˆ 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j                     d	eeej                        d
ee	   dee	   deej                  eeej                  ej                  f      f   fd„Zˆ xZS )ÚKosmos2TextBlockrw   c                 óŽ  •— t         ‰| �  «        |j                  | _        t        || j                  |j                  |j
                  dd¬«      | _        |j                  | _        t        j                  | j                  |j                  ¬«      | _        |j                  rdt        || j                  |j                  |j
                  dd¬«      | _        t        j                  | j                  |j                  ¬«      | _        t        |«      | _        t        j                  | j                  |j                  ¬«      | _        y )NT)r…   rÅ   rÉ   rK  rL  r÷   F)r‚   rƒ   r…   rJ  Úattention_headsrÈ   rù   rÉ   r	   rú   rû   Úself_attn_layer_normÚadd_cross_attentionÚencoder_attnÚencoder_attn_layer_normr`  ÚffnÚfinal_layer_normr“   s     €r.   rƒ   zKosmos2TextBlock.__init__  só   ø€ Ü‰ÑÔØ×)Ñ)ˆŒä,ØØ—n‘nØ×,Ñ,Ø×,Ñ,ØØ%)ô
ˆŒð —~‘~ˆŒÜ$&§L¡L°·±ÀV×EZÑEZÔ$[ˆÔ!à×%Ò%Ü 3ØØŸ.™.Ø ×0Ñ0Ø×0Ñ0ØØ).ô!ˆDÔô ,.¯<©<¸¿¹ÈF×LaÑLaÔ+bˆDÔ(ä! &Ó)ˆŒÜ "§¡¨T¯^©^À×AVÑAVÔ WˆÕr0   rO   rÔ   rT  Úencoder_attention_maskrV  Úcross_attn_layer_head_maskrU  rÖ   Ú	use_cacherT   c
                 ó’  — |}
|�|d d nd }| j                  |«      }| j                  |||||¬«      \  }}}t        j                  j	                  || j                  | j
                  ¬«      }|
|z   }d }d }|�’t        | d«      st        d| › d�«      ‚|}
| j                  |«      }|�|dd  nd }| j                  ||||||¬«      \  }}}t        j                  j	                  || j                  | j
                  ¬«      }|
|z   }||z   }|}
| j                  |«      }| j                  |«      }|
|z   }|f}|r|||fz  }|	r||fz  }|S )	Nr~   )rO   rU  rÔ   rV  rÖ   rÛ   rm  z'If `encoder_hidden_states` are passed, z` has to be instantiated with cross-attention layers by setting `config.add_cross_attention=True`rX  )rO   rT  rÔ   rV  rU  rÖ   )rk  rù   r	   r£   rÉ   rÝ   r2  r°   rn  rm  rp  ro  )r_   rO   rÔ   rT  rq  rV  rr  rU  rÖ   rs  r   Úself_attn_past_key_valueÚself_attn_weightsÚpresent_key_valueÚcross_attn_present_key_valueÚcross_attn_weightsÚcross_attn_past_key_valuer  s                     r.   r¸   zKosmos2TextBlock.forward  s×  € ð !ˆð :HÐ9S >°"°1Ñ#5ÐY]Ð à×1Ñ1°-Ó@ˆð ?C¿n¹nØ'Ø3Ø)Ø+Ø/ð ?Mó ?
Ñ;ˆÐ(Ð*;ô Ÿ™×-Ñ-¨m¸t¿|¹|ÐVZ×VcÑVcÐ-ÓdˆØ  =Ñ0ˆð (,Ð$Ø!ÐØ Ð,Ü˜4 Ô0Ü Ø=¸d¸Vð DDð Dóð ð
 %ˆHà ×8Ñ8¸ÓGˆMð @NÐ?Y¨°r°sÑ(;Ð_cÐ%ØNR×N_ÑN_Ø+Ø&;Ø5Ø :Ø8Ø"3ð O`ó OÑKˆMÐ-Ð/Kô ŸM™M×1Ñ1°-À4Ç<Á<ÐZ^×ZgÑZgÐ1ÓhˆMØ$ }Ñ4ˆMð !2Ð4PÑ PÐð !ˆà×-Ñ-¨mÓ<ˆð Ÿ™ Ó/ˆØ  =Ñ0ˆà Ð"ˆáØÐ)Ð+=Ð>Ñ>ˆGáØÐ)Ð+Ñ+ˆGàˆr0   )NNNNNNFT)rh   ri   rj   r   rƒ   r&   rº   r   r   r'   rl   r¸   r»   r¼   s   @r.   rh  rh    s  ø„ ðXÐ0õ Xð@ 26Ø8<Ø9=Ø26Ø=AØ8<Ø,1Ø$(ñNà—|‘|ðNð ! §¡Ñ.ðNð  (¨¯©Ñ5ð	Nð
 !)¨¯©Ñ 6ðNð " %§,¡,Ñ/ðNð %-¨U¯\©\Ñ$:ðNð !  u§|¡|Ñ!4Ñ5ðNð $ D™>ðNð ˜D‘>ðNð 
ˆu× Ñ  (¨5°×1BÑ1BÀE×DUÑDUÐ1UÑ+VÑ"WÐWÑ	X÷Nr0   rh  c            #       ól  ‡ — e Zd ZdZdefˆ fd„Zd„ Z	 	 	 	 	 ddeej                     deej                     deej                     de
d	eej                     f
d
„Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 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j                     de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 )ÚKosmos2TextTransformerz–
    Transformer decoder consisting of `config.layers` layers. Each layer is a [`Kosmos2TextBlock`].

    Args:
        config: Kosmos2TextConfig
    rw   c                 ó®  •— t         ‰| �  «        || _        |j                  | _        |j                  | _        |j
                  rt        j                  |j                  «      nd| _	        t        j                  |j                  |j                  |j                  ¬«      | _        t        |j                   |j                  |j                  ¬«      | _        t        j$                  t'        |j(                  «      D �cg c]  }t+        |«      ‘Œ c}«      | _        t        j,                  |j                  |j.                  «      | _        d| _        y c c}w )Nr!   )rH   )r�   r*  rH   F)r‚   rƒ   rw   rÉ   Ú	layerdropÚscale_embeddingr5  Úsqrtr…   Úembed_scaler	   r�   Ú
vocab_sizeÚpad_token_idÚembed_tokensr)  Úmax_position_embeddingsÚembed_positionsr  r  r  rh  rú   rû   Ú
layer_normr	  r
  s      €r.   rƒ   zKosmos2TextTransformer.__init__x  s÷   ø€ Ü‰ÑÔØˆŒØ—~‘~ˆŒØ×)Ñ)ˆŒà:@×:PÒ:Pœ4Ÿ9™9 V×%5Ñ%5Ô6ÐVYˆÔÜŸL™L¨×):Ñ):¸F×<LÑ<LÐZ`×ZmÑZmÔnˆÔäGØ ×8Ñ8Ø ×*Ñ*Ø×+Ñ+ô 
ˆÔô —m‘mÄuÈVÏ]É]ÓG[Ö$\À!Ô%5°fÕ%=Ò$\Ó]ˆŒÜŸ,™, v×'7Ñ'7¸×9NÑ9NÓOˆŒà&+ˆÕ#ùò %]s   Ã=Ec                 óÞ   — d }|d   dkD  r#t        ||j                  |j                  |¬«      }|�=t        ||j                  |d   ¬«      j	                  |j                  «      }|€|n||z   }|S )Nr5   r   )r2   r3   ©r   )r@   r   r2   r/   r$   )r_   rÔ   rE  r  r3   Úcombined_attention_maskÚexpanded_attn_masks          r.   Ú_prepare_decoder_attention_maskz6Kosmos2TextTransformer._prepare_decoder_attention_maskŒ  s’   € ð #'ÐØ�r‰?˜QÒÜ&7ØØ×#Ñ#Ø$×+Ñ+Ø'=ô	'Ð#ð Ð%ä!-¨n¸m×>QÑ>QÐ[fÐgiÑ[jÔ!k×!nÑ!nØ×$Ñ$ó"Ðð '>Ð&EÑ"ÐK]Ð`wÑKwð $ð 'Ð&r0   r  rQ   Úimg_input_maskr3   r   c                 óÚ  — |€| j                  |«      }|�[|j                  |j                  «      j                  d|j	                  d«      «      ||j                  t
        j                  ¬«      <   || j                  z  }| j                  ||||¬«      }|j                  |j                  «      }||z   }t        j                  j                  || j                  | j                  ¬«      }|S )Nr5   r¯   )rG   r  r3   r   rÛ   )r„  r$   r2   r<   r"   r&   r'   r�  r†  r	   r£   rÉ   rÝ   )	r_   rG   r  rQ   r�  r3   r   Ú	positionsrO   s	            r.   Úforward_embeddingz(Kosmos2TextTransformer.forward_embedding£  sé   € ð Ð Ø ×-Ñ-¨iÓ8ˆMàÐ#ØAMÇÁÐQ^×QeÑQeÓAf×AkÑAkØ�L×%Ñ% bÓ)óBˆM˜.×+Ñ+´%·*±*Ð+Ó=Ñ>ð &¨×(8Ñ(8Ñ8ˆð ×(Ñ(ØØ'Ø#9Ø%ð	 )ó 
ˆ	ð —L‘L ×!5Ñ!5Ó6ˆ	à%¨	Ñ1ˆäŸ™×-Ñ-¨m¸t¿|¹|ÐVZ×VcÑVcÐ-ÓdˆàÐr0   rG   rÔ   Úimage_embeds_position_maskrT  rq  Ú	head_maskÚcross_attn_head_maskrN   rs  rÖ   r  r  rT   c                 ó¼  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|�|
�t        d«      ‚|�"|j                  }|j                  d|d   «      }n!|
�|
j                  «       d d }nt        d«      ‚|	�|	d   d   j                  d   nd}|dkD  rd }d }| j                  ||
||||¬«      }| j                  ||||«      }|�|�t        ||
j                  |d   ¬«      }t        j                  j                  || j                  | j                   ¬«      }| j"                  r%| j                   r|rt$        j'                  d	«       d
}|rdnd }|rdnd }|r|�dnd }|rdnd }t)        ||gddg«      D ]j  \  }}|€Œ	|j                  «       d   t+        | j,                  «      k7  sŒ3t        d|› dt+        | j,                  «      › d|j                  «       d   › d�«      ‚ t/        | j,                  «      D ]Ý  \  }}|r||fz  }| j                   r%t1        j2                  g «      }|| j4                  k  rŒ?|	�|	|   nd }| j"                  r?| j                   r3| j7                  |j8                  |||||�||   nd |�||   nd d ||«
      }n ||||||�||   nd |�||   nd |||¬«	      }|d   }|r|||rdnd   fz  }|sŒÉ||d   fz  }|€ŒÕ||d   fz  }Œß | j;                  |«      }|r||fz  }|st=        d„ |||||fD «       «      S t?        |||||¬«      S )NzDYou cannot specify both input_ids and inputs_embeds at the same timer5   z5You have to specify either input_ids or inputs_embedsr   r~   )rG   r  rQ   r�  r3   r   r‰  rÛ   zZ`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...Frn   r’  r“  zThe `z` should be specified for z layers, but it is for ú.)rÔ   rT  rq  rV  rr  rU  rÖ   rs  r   r   c              3   ó$   K  — | ]  }|�|–— Œ
 y ­wrë   rn   r  s     r.   r`   z1Kosmos2TextTransformer.forward.<locals>.<genexpr>T  s   è ø€ ò 
àð �=ô ñ
ùs   ‚)rM   rN   rO   rP   Úcross_attentions) rw   rÖ   r  rs  r  r°   rœ   r<   r"   r�  rŒ  r/   r   r	   r£   rÉ   rÝ   r	  ÚloggerÚwarning_onceÚzipÚlenr  r  r&   Úrandr~  r  r  r‡  rd   r   )r_   rG   rÔ   rQ   r‘  rT  rq  r’  r“  rN   r  r   rs  rÖ   r  r  rE  r3   rO   Úall_hidden_statesÚall_self_attnsÚall_cross_attentionsÚpresent_key_value_statesÚ	attn_maskÚ	mask_namer  Údecoder_layerÚdropout_probabilityrU  r  s                                 r.   r¸   zKosmos2TextTransformer.forwardÆ  sQ  € ð$ 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð "+Ð!6‘I¸D¿K¹K×<QÑ<Qˆ	Ø%0Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐ  ]Ð%>ÜÐcÓdÐdØÐ"Ø#Ÿ/™/ˆKØ!Ÿ™ r¨;°r©?Ó;‰IØÐ&Ø'×,Ñ,Ó.¨s°Ð3‰KäÐTÓUÐUð DSÐC^ °Ñ!3°AÑ!6×!<Ñ!<¸QÒ!?ÐdeÐð " AÒ%ØˆLØ)-Ð&à×.Ñ.ØØ'Ø%Ø5Ø#9Ø%ð /ó 
ˆð ×=Ñ=Ø˜K¨Ð8Nó
ˆð
 !Ð,Ð1GÐ1Sä%1Ð2HÈ-×J]ÑJ]ÐgrÐsuÑgvÔ%wÐ"äŸ™×-Ñ-¨m¸t¿|¹|ÐVZ×VcÑVcÐ-Ódˆà×&Ò&¨4¯=ª=ÙÜ×#Ñ#Øpôð "�	ñ #7™B¸DÐÙ0™°dˆÙ&7Ð<QÐ<]™rÐdhÐÙ)2¡2¸Ð ô %(¨Ð4HÐ(IÈKÐYoÐKpÓ$qò 	Ñ ˆI�yØÑ$Ø—>‘>Ó# AÑ&¬3¨t¯{©{Ó+;Ó<Ü$Ø 	˜{Ð*DÄSÈÏÉÓEUÐDVð WØ%ŸN™NÓ,¨QÑ/Ð0°ð3óð ð	ô #,¨D¯K©KÓ"8ò /	@ÑˆC�á#Ø! mÐ%5Ñ5Ð!Ø�}Š}Ü&+§j¡j°£nÐ#Ø&¨¯©Ò7Øà5DÐ5P˜_¨SÒ1ÐVZˆNà×*Ò*¨t¯}ª}Ø $× AÑ AØ!×*Ñ*Ø!Ø"Ø)Ø*Ø&/Ð&;�I˜c’NÀØ1EÐ1QÐ(¨Ò-ÐW[ØØ%Øó!‘ñ !.Ø!Ø#1Ø*?Ø+AØ7@Ð7L Y¨s¢^ÐRVà5IÐ5UÐ,¨SÒ1Ð[_à#1Ø&7Ø'ô!�ð *¨!Ñ,ˆMáØ(¨]Ñ@Q¹1ÐWXÑ-YÐ,[Ñ[Ð(â Ø =°Ñ#3Ð"5Ñ5�à(Ñ4Ø(¨]¸1Ñ-=Ð,?Ñ?Ñ(ð_/	@ðd Ÿ™¨Ó6ˆñ  Ø -Ð!1Ñ1ÐáÜñ 
ð "Ø,Ø%Ø"Ø(ðô
ó 
ð 
ô 9Ø+Ø4Ø+Ø%Ø1ô
ð 	
r0   )NNNr   N©NNNNNNNNNNNNNNN)rh   ri   rj   rk   r   rƒ   rŒ  r   r&   rº   rC   r�  r   rl   r'   r   r   r   r¸   r»   r¼   s   @r.   r|  r|  p  sï  ø„ ñð,Ð0õ ,ò('ð4 15Ø/3Ø15Ø&'Ø/3ñ!ð   §¡Ñ-ð!ð ˜uŸ|™|Ñ,ð	!ð
 ! §¡Ñ.ð!ð !$ð!ð ˜uŸ|™|Ñ,ó!ðJ -1Ø15Ø/3Ø=AØ8<Ø9=Ø,0Ø7;Ø=AØ04Ø/3Ø$(Ø,0Ø/3Ø&*ñ!_
à˜EŸL™LÑ)ð_
ð ! §¡Ñ.ð_
ð ˜uŸ|™|Ñ,ð	_
ð
 %-¨U¯\©\Ñ$:ð_
ð  (¨¯©Ñ5ð_
ð !)¨¯©Ñ 6ð_
ð ˜EŸL™LÑ)ð_
ð ' u§|¡|Ñ4ð_
ð " $ u×'8Ñ'8Ñ"9Ñ:ð_
ð   §¡Ñ-ð_
ð ˜uŸ|™|Ñ,ð_
ð ˜D‘>ð_
ð $ D™>ð_
ð ' t™nð_
ð  ˜d‘^ð!_
ð" 
ˆuÐ?Ð?Ñ	@÷#_
r0   r|  c                   ó&   — e Zd ZdZeZdZddgZd„ Zy)ÚKosmos2PreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    Trô   rh  c                 ó  — t        | t        «      r| j                  j                  }n6t        | t        t
        f«      r | j                  j                  j                  }t        | t        t        f«      r| j                  j                  }n6t        | t        t
        f«      r | j                  j                  j                  }t        |t        «      rÕt        j                  j                  |j                  d|j                   dz  z  ¬«       t        j                  j                  |j"                  j$                  |j                  j&                  |z  ¬«       t        j                  j                  |j(                  j$                  |j                  j&                  |z  ¬«       yt        |t*        «      �r'|j                   dz  d|j                  j,                  z  dz  z  z  }|j                   dz  |z  }t        j                  j                  |j.                  j$                  |¬«       t        j                  j                  |j0                  j$                  |¬«       t        j                  j                  |j2                  j$                  |¬«       t        j                  j                  |j4                  j$                  |¬«       |j.                  j6                  �.|j.                  j6                  j8                  j;                  «        |j0                  j6                  �.|j0                  j6                  j8                  j;                  «        |j2                  j6                  �.|j2                  j6                  j8                  j;                  «        |j4                  j6                  �/|j4                  j6                  j8                  j;                  «        yyt        |t<        «      �rL|j                  j>                  dz  d|j                  j,                  z  dz  z  z  }d|j                  j>                  z  dz  |z  }t        j                  j                  |j@                  j$                  |¬«       t        j                  j                  |jB                  j$                  |¬«       |j@                  j6                  �.|j@                  j6                  j8                  j;                  «        |jB                  j6                  �/|jB                  j6                  j8                  j;                  «        yyt        |tD        «      r»|jF                  j6                  j8                  j;                  «        |jF                  j$                  j8                  jI                  d«       |jJ                  j6                  j8                  j;                  «        |jJ                  j$                  j8                  jI                  d«       yt        |tL        «      r»|jN                  j6                  j8                  j;                  «        |jN                  j$                  j8                  jI                  d«       |jP                  j6                  j8                  j;                  «        |jP                  j$                  j8                  jI                  d«       yt        |tR        «      �ræt        j                  j                  |j.                  j$                  ¬«       t        j                  j                  |j0                  j$                  |¬«       t        j                  j                  |j2                  j$                  |¬«       t        j                  j                  |j4                  j$                  |¬«       |j.                  j6                  �.|j.                  j6                  j8                  j;                  «        |j0                  j6                  �.|j0                  j6                  j8                  j;                  «        |j2                  j6                  �.|j2                  j6                  j8                  j;                  «        |j4                  j6                  �/|j4                  j6                  j8                  j;                  «        yyt        |tT        «      rôt        j                  j                  |j@                  j$                  ¬«       t        j                  j                  |jB                  j$                  |¬«       |j@                  j6                  �.|j@                  j6                  j8                  j;                  «        |jB                  j6                  �/|jB                  j6                  j8                  j;                  «        yyt        |t        «      r{t        j                  j                  |jV                  j$                  ¬«       |jV                  j6                  �/|jV                  j6                  j8                  j;                  «        yyt        |tX        «      r{t        j                  j                  |jZ                  j$                  ¬«       |jZ                  j6                  �/|jZ                  j6                  j8                  j;                  «        yyt        |t\        «      rŽ|j^                  j$                  j8                  j                  d¬«       |j^                  j`                  �F|j^                  j$                  j8                  |j^                  j`                     j;                  «        yyy)zInitialize the weightsr^  rÃ   )ÚmeanÚstd)rª  r~   Nr!   )1Ú
isinstanceÚKosmos2VisionModelrw   Úinitializer_factorÚKosmos2ModelÚKosmos2ForConditionalGenerationÚvision_configÚKosmos2TextModelÚKosmos2TextForCausalLMÚinit_stdÚtext_configrv   r	   ÚinitÚnormal_rŠ   r…   r�   r�   Úinitializer_ranger‘   r¾   r  rÍ   rË   rÌ   rÎ   r}   ÚdataÚzero_ré   r„   rï   rð   rô   rü   Úfill_rþ   r  r   r"  rJ  r`  Úlm_headÚKosmos2ImageToTextProjectionÚdenser|  r„  rH   )r_   ÚmoduleÚfactorrª  Úin_proj_stdÚout_proj_stdÚfc_stds          r.   Ú_init_weightsz$Kosmos2PreTrainedModel._init_weightsr  sš  € ä�dÔ.Ô/Ø—[‘[×3Ñ3‰FÜ˜œ|Ô-LÐMÔNØ—[‘[×.Ñ.×AÑAˆFä�dÔ-Ô/EÐFÔGØ—+‘+×&Ñ&‰CÜ˜œ|Ô-LÐMÔNØ—+‘+×)Ñ)×2Ñ2ˆCä�fÔ5Ô6Ü�G‰G�O‰O˜F×2Ñ2¸À&×BRÑBRÐTXÑBXÐ[aÑBaˆOÔbÜ�G‰G�O‰O˜F×2Ñ2×9Ñ9¸v¿}¹}×?^Ñ?^ÐagÑ?gˆOÔhÜ�G‰G�O‰O˜F×5Ñ5×<Ñ<À&Ç-Á-×BaÑBaÐdjÑBjˆOÕkÜ˜Ô 6Õ7Ø!×+Ñ+¨TÑ1°q¸6¿=¹=×;ZÑ;ZÑ7ZÐ_cÑ6cÑdÐgmÑmˆKØ"×,Ñ,¨dÑ2°fÑ<ˆLÜ�G‰G�O‰O˜FŸM™M×0Ñ0°kˆOÔBÜ�G‰G�O‰O˜FŸM™M×0Ñ0°kˆOÔBÜ�G‰G�O‰O˜FŸM™M×0Ñ0°kˆOÔBÜ�G‰G�O‰O˜FŸO™O×2Ñ2¸ˆOÔEØ�}‰}×!Ñ!Ð-Ø—‘×"Ñ"×'Ñ'×-Ñ-Ô/Ø�}‰}×!Ñ!Ð-Ø—‘×"Ñ"×'Ñ'×-Ñ-Ô/Ø�}‰}×!Ñ!Ð-Ø—‘×"Ñ"×'Ñ'×-Ñ-Ô/Ø�‰×#Ñ#Ð/Ø—‘×$Ñ$×)Ñ)×/Ñ/Õ1ð 0ä˜Ô 0Õ1Ø!Ÿ=™=×4Ñ4°dÑ:ÀÀFÇMÁM×DcÑDcÑ@cÐhlÑ?lÑmÐpvÑvˆKØ˜&Ÿ-™-×3Ñ3Ñ3¸Ñ<¸vÑEˆFÜ�G‰G�O‰O˜FŸJ™J×-Ñ-°6ˆOÔ:Ü�G‰G�O‰O˜FŸJ™J×-Ñ-°;ˆOÔ?Ø�z‰z�‰Ð*Ø—
‘
—‘×$Ñ$×*Ñ*Ô,Ø�z‰z�‰Ð*Ø—
‘
—‘×$Ñ$×*Ñ*Õ,ð +ä˜Ô 9Ô:Ø×Ñ×#Ñ#×(Ñ(×.Ñ.Ô0Ø×Ñ×%Ñ%×*Ñ*×0Ñ0°Ô5Ø×Ñ×#Ñ#×(Ñ(×.Ñ.Ô0Ø×Ñ×%Ñ%×*Ñ*×0Ñ0°Õ5Ü˜Ô 8Ô9Ø×Ñ×$Ñ$×)Ñ)×/Ñ/Ô1Ø×Ñ×&Ñ&×+Ñ+×1Ñ1°#Ô6Ø×!Ñ!×&Ñ&×+Ñ+×1Ñ1Ô3Ø×!Ñ!×(Ñ(×-Ñ-×3Ñ3°CÕ8Ü˜Ô 3Õ4Ü�G‰G�O‰O˜FŸM™M×0Ñ0°cˆOÔ:Ü�G‰G�O‰O˜FŸM™M×0Ñ0°cˆOÔ:Ü�G‰G�O‰O˜FŸM™M×0Ñ0°cˆOÔ:Ü�G‰G�O‰O˜FŸO™O×2Ñ2¸ˆOÔ<Ø�}‰}×!Ñ!Ð-Ø—‘×"Ñ"×'Ñ'×-Ñ-Ô/Ø�}‰}×!Ñ!Ð-Ø—‘×"Ñ"×'Ñ'×-Ñ-Ô/Ø�}‰}×!Ñ!Ð-Ø—‘×"Ñ"×'Ñ'×-Ñ-Ô/Ø�‰×#Ñ#Ð/Ø—‘×$Ñ$×)Ñ)×/Ñ/Õ1ð 0ä˜¤Ô/Ü�G‰G�O‰O˜FŸJ™J×-Ñ-°3ˆOÔ7Ü�G‰G�O‰O˜FŸJ™J×-Ñ-°3ˆOÔ7Ø�z‰z�‰Ð*Ø—
‘
—‘×$Ñ$×*Ñ*Ô,Ø�z‰z�‰Ð*Ø—
‘
—‘×$Ñ$×*Ñ*Õ,ð +ä˜Ô 6Ô7Ü�G‰G�O‰O˜FŸN™N×1Ñ1°sˆOÔ;Ø�~‰~×"Ñ"Ð.Ø—‘×#Ñ#×(Ñ(×.Ñ.Õ0ð /ä˜Ô <Ô=Ü�G‰G�O‰O˜FŸL™L×/Ñ/°SˆOÔ9Ø�|‰|× Ñ Ð,Ø—‘×!Ñ!×&Ñ&×,Ñ,Õ.ð -ä˜Ô 6Ô7Ø×Ñ×&Ñ&×+Ñ+×3Ñ3¸À#Ð3ÔFØ×"Ñ"×.Ñ.Ð:Ø×#Ñ#×*Ñ*×/Ñ/°×0CÑ0C×0OÑ0OÑP×VÑVÕXð ;ð 8r0   N)	rh   ri   rj   rk   r   Úconfig_classÚsupports_gradient_checkpointingÚ_no_split_modulesrÃ  rn   r0   r.   r§  r§  h  s'   „ ñð
 !€LØ&*Ð#Ø4Ð6HÐIÐóQYr0   r§  c                   óÞ   ‡ — e Zd Ze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d
ee   deeef   fd„«       «       Zˆ xZS )r¬  r«   rw   c                 ód   •— t         ‰| �  |«       t        |«      | _        | j	                  «        y rë   )r‚   rƒ   r  ÚmodelÚ	post_initr“   s     €r.   rƒ   zKosmos2VisionModel.__init__Ë  s&   ø€ Ü‰Ñ˜Ô Ü-¨fÓ5ˆŒ
à�‰Õr0   rT   c                 óB   — | j                   j                  j                  S rë   )rÉ  r•   r�   rf   s    r.   Úget_input_embeddingsz'Kosmos2VisionModel.get_input_embeddingsÒ  s   € Ø�z‰z×$Ñ$×4Ñ4Ð4r0   ©Úoutput_typerÄ  rÖ   r  rª   r  c                 ó.   — | j                  |||||¬«      S )ú
        Returns:

        ©r«   rÖ   r  rª   r  ©rÉ  )r_   r«   rÖ   r  rª   r  s         r.   r¸   zKosmos2VisionModel.forwardÕ  s)   € ð �z‰zØ%Ø/Ø!5Ø%=Ø#ð ó 
ð 	
r0   r'  )rh   ri   rj   r   rÄ  Úmain_input_namerƒ   r	   ÚModulerÌ  r   ÚKOSMOS2_VISION_INPUTS_DOCSTRINGr   r   r   r&   rl   r'   r   r   r¸   r»   r¼   s   @r.   r¬  r¬  Æ  sÄ   ø„ Ø&€LØ$€OðÐ2õ ð5 b§i¡ió 5ñ +Ð+JÓKÙÐ+EÐTgÔhð 59Ø,0Ø/3Ø).Ø&*ñ
à˜u×0Ñ0Ñ1ð
ð $ D™>ð
ð ' t™nð	
ð
 #'ð
ð ˜d‘^ð
ð 
ˆuÐ0Ð0Ñ	1ò
ó ió Lô
r0   r¬  c            %       ó,  ‡ — e Zd ZeZdefˆ fd„Zdej                  f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j                      deej                      de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±  rw   c                 ód   •— t         ‰| �  |«       t        |«      | _        | j	                  «        y rë   )r‚   rƒ   r|  rÉ  rÊ  r“   s     €r.   rƒ   zKosmos2TextModel.__init__ï  s&   ø€ Ü‰Ñ˜Ô Ü+¨FÓ3ˆŒ
à�‰Õr0   rT   c                 ó.   — | j                   j                  S rë   ©rÉ  r„  rf   s    r.   rÌ  z%Kosmos2TextModel.get_input_embeddingsõ  ó   € Ø�z‰z×&Ñ&Ð&r0   c                 ó&   — || j                   _        y rë   rÙ  ©r_   Úvalues     r.   Úset_input_embeddingsz%Kosmos2TextModel.set_input_embeddingsø  ó   € Ø"'ˆ�
‰
Õr0   rÍ  rG   rÔ   rQ   r‘  rT  rq  r’  r“  rN   r  r   rs  rÖ   r  r  c                 óB   — | j                  |||||||||	|
|||||¬«      S )rÐ  ©rG   rÔ   rQ   r‘  rT  rq  r’  r“  rN   r  r   rs  rÖ   r  r  rÒ  )r_   rG   rÔ   rQ   r‘  rT  rq  r’  r“  rN   r  r   rs  rÖ   r  r  s                   r.   r¸   zKosmos2TextModel.forwardû  sG   € ð0 �z‰zØØ)Ø%Ø'AØ"7Ø#9ØØ!5Ø+Ø'Ø%ØØ/Ø!5Ø#ð ó 
ð 	
r0   r¥  )rh   ri   rj   r   rÄ  rƒ   r	   rÔ  rÌ  rÞ  r   ÚKOSMOS2_TEXT_INPUTS_DOCSTRINGr   r   r   r&   rº   r   rl   r'   r   r   r¸   r»   r¼   s   @r.   r±  r±  ì  s©  ø„ Ø$€LðÐ0õ ð' b§i¡ió 'ò(ñ +Ð+HÓIÙÐ+TÐctÔuð -1Ø15Ø/3Ø=AØ8<Ø9=Ø,0Ø7;Ø=AØ04Ø/3Ø$(Ø,0Ø/3Ø&*ñ!&
à˜EŸL™LÑ)ð&
ð ! §¡Ñ.ð&
ð ˜uŸ|™|Ñ,ð	&
ð
 %-¨U¯\©\Ñ$:ð&
ð  (¨¯©Ñ5ð&
ð !)¨¯©Ñ 6ð&
ð ˜EŸL™LÑ)ð&
ð ' u§|¡|Ñ4ð&
ð " $ u×'8Ñ'8Ñ"9Ñ:ð&
ð   §¡Ñ-ð&
ð ˜uŸ|™|Ñ,ð&
ð ˜D‘>ð&
ð $ D™>ð&
ð ' t™nð&
ð  ˜d‘^ð!&
ð" 
ˆuÐ?Ð?Ñ	@ò#&
ó vó Jô&
r0   r±  z‰
    The text model from KOSMOS-2 with a language modeling head on top (linear layer with weights tied to the input
    embeddings).
    c            '       ó   ‡ — e Zd ZeZdgZdefˆ fd„Zdej                  fd„Z	d„ Z
dej                  f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j&                     deej&                     de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	 	 	 	 	 	 dˆ fd„	Zed„ «       Zˆ xZS )r²  zlm_head.weightrw   c                 óÆ   •— t         ‰| �  |«       t        |«      | _        t	        j
                  |j                  |j                  d¬«      | _        | j                  «        y )NF)Úin_featuresÚout_featuresr}   )
r‚   rƒ   r|  rÉ  r	   rÊ   r…   r‚  r»  rÊ  r“   s     €r.   rƒ   zKosmos2TextForCausalLM.__init__1  sI   ø€ Ü‰Ñ˜Ô ä+¨FÓ3ˆŒ
Ü—y‘y¨V×-=Ñ-=ÈF×L]ÑL]ÐdiÔjˆŒð 	�‰Õr0   rT   c                 ó.   — | j                   j                  S rë   rÙ  rf   s    r.   rÌ  z+Kosmos2TextForCausalLM.get_input_embeddings:  rÚ  r0   c                 ó&   — || j                   _        y rë   rÙ  rÜ  s     r.   rÞ  z+Kosmos2TextForCausalLM.set_input_embeddings=  rß  r0   c                 ó   — | j                   S rë   ©r»  rf   s    r.   Úget_output_embeddingsz,Kosmos2TextForCausalLM.get_output_embeddings@  s   € Ø�|‰|Ðr0   c                 ó   — || _         y rë   rê  ©r_   Únew_embeddingss     r.   Úset_output_embeddingsz,Kosmos2TextForCausalLM.set_output_embeddingsC  s	   € Ø%ˆ�r0   rÍ  rG   rÔ   rQ   r‘  rT  rq  r’  r“  rN   r  r   Úlabelsrs  rÖ   r  r  c                 ó   — |�|n| j                   j                  }|�|rt        j                  d«       d}| j	                  |||||||||	|
|||||¬«      }| j                  |d   «      }d}|�”|j                  |j                  «      }|ddd…dd…f   j                  «       }|ddd…f   j                  «       }|j                  \  }}}t        «       } ||j                  ||z  |«      |j                  ||z  «      «      }|s|f|dd z   }|�|f|z   S |S t        |||j                  |j                  |j                  |j                   ¬	«      S )
aÓ  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in
            `[-100, 0, ..., config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are
            ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`

        Returns:

        NzJThe `use_cache` argument is changed to `False` since `labels` is provided.Frá  r   .r5   r   )rq   rr   rN   rO   rP   r—  )rw   r  r˜  ÚwarningrÉ  r»  r$   r2   rÒ   rœ   r
   r<   r   rN   rO   rP   r—  )r_   rG   rÔ   rQ   r‘  rT  rq  r’  r“  rN   r  r   rð  rs  rÖ   r  r  r  Ú	lm_logitsrq   Úshift_logitsÚshift_labelsr³   r[  r‚  Úloss_fctÚoutputs                              r.   r¸   zKosmos2TextForCausalLM.forwardF  s�  € ð< &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐÙÜ—‘ÐkÔlØˆIà—*‘*ØØ)Ø%Ø'AØ"7Ø#9ØØ!5Ø+Ø'Ø%ØØ/Ø!5Ø#ð ó 
ˆð" —L‘L ¨¡Ó,ˆ	àˆØÐà—Y‘Y˜y×/Ñ/Ó0ˆFà$ S¨#¨2¨#ªq [Ñ1×<Ñ<Ó>ˆLØ! # q¡r '™?×5Ñ5Ó7ˆLØ1=×1CÑ1CÑ.ˆJ˜
 Jä'Ó)ˆHÙØ×!Ñ! *¨zÑ"9¸:ÓFÈ×HYÑHYÐZdÐgqÑZqÓHróˆDñ Ø�\ G¨A¨B KÑ/ˆFØ'+Ð'7�D�7˜VÑ#ÐC¸VÐCä0ØØØ#×3Ñ3Ø!×/Ñ/Ø×)Ñ)Ø$×5Ñ5ô
ð 	
r0   c                 ór  •— t        || j                  j                  d¬«      }	|�d }d }nt|�r|j                  «       \  }
}|j                  «       d   }t	        j
                  |t	        j                  |
||z
  ft        j                  |j                  ¬«      fd¬«      }t        ‰| �(  |f||||||	|dœ|¤Ž}|S )Nr   )rH   r3   r5   )r"   r   r2   r   r7   )rN   rÔ   rQ   r‘  rs  r   Úcache_position)rJ   rw   rƒ  r"   r&   r=   r>   r'   r2   r‚   Úprepare_inputs_for_generation)r_   rG   rQ   r‘  rN   rÔ   rs  rù  Úmodel_kwargsr   r³   rÐ   Úmask_lenÚmodel_inputsr”   s                 €r.   rú  z4Kosmos2TextForCausalLM.prepare_inputs_for_generation™  sà   ø€ ô :ØØŸ™×0Ñ0Ø#$ô
ˆð Ð&ØˆLØ)-Ñ&à'Ð3Ø"+§.¡.Ó"2ÑˆJ˜Ø1×6Ñ6Ó8¸Ñ<ˆHÜ).¯©à.Ü—K‘K j°'¸HÑ2DÐ%EÌUÏZÉZÐ`i×`pÑ`pÔqðð ô*Ð&ô ‘wÑ<Øð

à+Ø)Ø%Ø'AØØ%Ø)ñ

ð ñ

ˆð Ðr0   c                 óJ   ‡— d}| D ]  }|t        ˆfd„|D «       «      fz  }Œ |S )Nrn   c              3   ót   •K  — | ]/  }|j                  d ‰j                  |j                  «      «      –— Œ1 y­w)r   N)rA  r$   r2   )r]   Ú
past_stateÚbeam_idxs     €r.   r`   z8Kosmos2TextForCausalLM._reorder_cache.<locals>.<genexpr>Ð  s.   øè ø€ ÒnÐU_�j×-Ñ-¨a°·±¸Z×=NÑ=NÓ1O×PÑnùs   ƒ58)rd   )rN   r  Úreordered_pastÚ
layer_pasts    `  r.   Ú_reorder_cachez%Kosmos2TextForCausalLM._reorder_cacheÊ  s?   ø€ ð ˆØ)ò 	ˆJØÜÓnÐcmÔnÓnðñ ‰Nð	ð Ðr0   )NNNNNNNNNNNNNNNN)NNNNNN)rh   ri   rj   r   rÄ  Ú_tied_weights_keysrƒ   r	   rÔ  rÌ  rÞ  rë  rï  r   râ  r   r   r   r&   rº   r   rl   Ú
LongTensorr'   r   r   r¸   rú  rG  r  r»   r¼   s   @r.   r²  r²  &  s#  ø„ ð %€LØ*Ð+ÐðÐ0õ ð' b§i¡ió 'ò(ð r§y¡yó ò&ñ +Ð+HÓIÙÐ+LÐ[lÔmð -1Ø15Ø/3Ø=AØ8<Ø9=Ø,0Ø7;Ø=AØ04Ø/3Ø-1Ø$(Ø,0Ø/3Ø&*ñ#O
à˜EŸL™LÑ)ðO
ð ! §¡Ñ.ðO
ð ˜uŸ|™|Ñ,ð	O
ð
 %-¨U¯\©\Ñ$:ðO
ð  (¨¯©Ñ5ðO
ð !)¨¯©Ñ 6ðO
ð ˜EŸL™LÑ)ðO
ð ' u§|¡|Ñ4ðO
ð " $ u×'8Ñ'8Ñ"9Ñ:ðO
ð   §¡Ñ-ðO
ð ˜uŸ|™|Ñ,ðO
ð ˜×)Ñ)Ñ*ðO
ð ˜D‘>ðO
ð $ D™>ðO
ð  ' t™nð!O
ð" ˜d‘^ð#O
ð$ 
ˆuÐ7Ð7Ñ	8ò%O
ó nó JðO
ðh Ø#'ØØØØõ/ðb ñó ôr0   r²  c                   ó.   ‡ — e Zd ZdZdefˆ fd„Zd„ Zˆ xZS )r¼  zmThe layer that transforms the image model's output to part of the text model's input (namely, image features)rw   c                 óø  •— t         ‰| �  «        t        j                  |j                  j
                  |j                  j                  «      | _        t        j                  t        j                  |j                  |j                  j                  «      «      | _        t        |j                  |j                  j                  |j                  j                  |j                  j                   dd¬«      | _        y )NF)rÉ   rK  rL  )r‚   rƒ   r	   rÊ   r°  r„   r´  r…   r½  rˆ   r&   r‰   Úlatent_query_numÚlatent_queryrJ  rj  rÈ   Úx_attnr“   s     €r.   rƒ   z%Kosmos2ImageToTextProjection.__init__Ø  s®   ø€ Ü‰ÑÔÜ—Y‘Y˜v×3Ñ3×?Ñ?À×ASÑAS×A]ÑA]Ó^ˆŒ
ÜŸL™L¬¯©°V×5LÑ5LÈf×N`ÑN`×NjÑNjÓ)kÓlˆÔä)Ø×ÑØ×Ñ×(Ñ(Ø×Ñ×.Ñ.Ø×&Ñ&×8Ñ8ØØ%*ô
ˆ�r0   c                 ó  — | j                  |«      }| j                  j                  d«      j                  |j	                  d«      dd«      }t        j                  ||gd¬«      }| j                  ||d d d ¬«      \  }}}||fS )Nr   r5   r   r7   )rO   rT  rU  rÔ   rÖ   )r½  r
  rž   r#   r"   r&   r=   r  )r_   ÚfeaturesrO   r
  Úkey_value_statesrä   r´   s          r.   r¸   z$Kosmos2ImageToTextProjection.forwardæ  s‘   € ØŸ
™
 8Ó,ˆð ×(Ñ(×2Ñ2°1Ó5×<Ñ<¸]×=OÑ=OÐPQÓ=RÐTVÐXZÓ[ˆÜ Ÿ9™9 m°\Ð%BÈÔJÐà)-¯©Ø&Ø"2ØØØ"ð *5ó *
Ñ&ˆ�| Qð ˜lÐ*Ð*r0   )rh   ri   rj   rk   r   rƒ   r¸   r»   r¼   s   @r.   r¼  r¼  Õ  s   ø„ Ùwð
˜}õ 
ö+r0   r¼  z}
    KOSMOS-2 Model for generating text and image features. The model consists of a vision encoder and a language model.
    c            #       óö  ‡ — e Zd ZeZdZdefˆ fd„Zdej                  f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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dee   deeef   fd„«       «       Zˆ xZS )r®  r«   rw   c                 óÌ   •— t         ‰| �  |«       t        |j                  «      | _        t        |j                  «      | _        t        |«      | _	        | j                  «        y rë   )r‚   rƒ   r±  r´  Ú
text_modelr¬  r°  Úvision_modelr¼  Úimage_to_text_projectionrÊ  r“   s     €r.   rƒ   zKosmos2Model.__init__  sN   ø€ Ü‰Ñ˜Ô ä*¨6×+=Ñ+=Ó>ˆŒÜ.¨v×/CÑ/CÓDˆÔÜ(DÀVÓ(LˆÔ%ð 	�‰Õr0   rT   c                 óB   — | j                   j                  j                  S rë   ©r  rÉ  r„  rf   s    r.   rÌ  z!Kosmos2Model.get_input_embeddings  ó   € Ø�‰×$Ñ$×1Ñ1Ð1r0   c                 ó:   — || j                   j                  _        y rë   r  rÜ  s     r.   rÞ  z!Kosmos2Model.set_input_embeddings  ó   € Ø-2ˆ�‰×ÑÕ*r0   rÍ  rG   r‘  rÔ   r’  rN   rQ   r  r   rs  rÖ   r  rª   r  c                 ó�  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }d}d}|€€|€t	        d«      ‚| j                  |||||¬«      }| j
                  j                  j                  |d   «      }t        j                  j                  |d¬«      }| j                  |«      \  }}| j                  ||||||||	|
|||¬«      }|s||||fz   }t        d„ |D «       «      S t        |j                  |j                   |j"                  |j$                  |||¬	«      S )
a  
        Returns:

        Examples:

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

        >>> model = Kosmos2Model.from_pretrained("microsoft/kosmos-2-patch14-224")
        >>> processor = AutoProcessor.from_pretrained("microsoft/kosmos-2-patch14-224")

        >>> url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> text = (
        ...     "<grounding> An image of<phrase> a snowman</phrase><object><patch_index_0044><patch_index_0863>"
        ...     "</object> warming himself by<phrase> a fire</phrase><object><patch_index_0005><patch_index_0911>"
        ...     "</object>"
        ... )

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

        >>> last_hidden_state = model(
        ...     pixel_values=inputs["pixel_values"],
        ...     input_ids=inputs["input_ids"],
        ...     attention_mask=inputs["attention_mask"],
        ...     image_embeds_position_mask=inputs["image_embeds_position_mask"],
        ... ).last_hidden_state
        >>> list(last_hidden_state.shape)
        [1, 91, 2048]
        ```Nú<You have to specify either `pixel_values` or `image_embeds`.rÑ  r   r5   r7   )rG   rÔ   rQ   r‘  r’  rN   r  r   rs  rÖ   r  r  c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrë   rn   ©r]   r÷  s     r.   r`   z'Kosmos2Model.forward.<locals>.<genexpr>p  ó   è ø€ ÒL F¸Ñ9KœÑLùr  )rM   rN   rO   rP   rQ   rR   rS   )rw   rÖ   r  r  r°   r  rÉ  r"  r	   r£   Ú	normalizer  r  rd   rL   rM   rN   rO   rP   )r_   r«   rG   r‘  rÔ   r’  rN   rQ   r  r   rs  rÖ   r  rª   r  rS   rR   r  s                     r.   r¸   zKosmos2Model.forward  s‹  € ðh 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà"ÐØ $ÐØÐØÐ#Ü Ð!_Ó`Ð`à"&×"3Ñ"3Ø)Ø"3Ø%9Ø)AØ'ð #4ó #Ðð  ×,Ñ,×2Ñ2×AÑAÐBUÐVWÑBXÓYˆLäŸ=™=×2Ñ2°<ÀRÐ2ÓHˆLØ26×2OÑ2OÐP\Ó2]Ñ/ˆLÐ/à—/‘/ØØ)Ø%Ø'AØØ+Ø'Ø%ØØ/Ø!5Ø#ð "ó 
ˆñ Ø Ð/DÐFYÐ ZÑZˆGÜÑL¨gÔLÓLÐLä!Ø%×7Ñ7Ø#×3Ñ3Ø!×/Ñ/Ø×)Ñ)Ø%Ø"7Ø 3ô
ð 	
r0   )NNNNNNNNNNNNFN)rh   ri   rj   r   rÄ  rÓ  rƒ   r	   rÔ  rÌ  rÞ  r   ÚKOSMOS2_INPUTS_DOCSTRINGr   rL   Ú_CONFIG_FOR_DOCr   r&   rº   r   rl   r'   r   r   r¸   r»   r¼   s   @r.   r®  r®  ø  s   ø„ ð !€LØ$€Oð˜}õ ð2 b§i¡ió 2ò3ñ +Ð+CÓDÙÐ+=ÈOÔ\ð 04Ø,0Ø=AØ15Ø,0Ø=AØ/3Ø04Ø/3Ø$(Ø,0Ø/3Ø).Ø&*ñf
à˜uŸ|™|Ñ,ðf
ð ˜EŸL™LÑ)ðf
ð %-¨U¯\©\Ñ$:ð	f
ð
 ! §¡Ñ.ðf
ð ˜EŸL™LÑ)ðf
ð " $ u×'8Ñ'8Ñ"9Ñ:ðf
ð ˜uŸ|™|Ñ,ðf
ð   §¡Ñ-ðf
ð ˜uŸ|™|Ñ,ðf
ð ˜D‘>ðf
ð $ D™>ðf
ð ' t™nðf
ð #'ðf
ð ˜d‘^ðf
ð  
ˆuÐ(Ð(Ñ	)ò!f
ó ]ó Eôf
r0   r®  z�
    KOSMOS-2 Model for generating text and bounding boxes given an image. The model consists of a vision encoder and a
    language model.
    c            #       óæ  ‡ — e Zd ZeZdZdgZdefˆ fd„Zdej                  fd„Z
d„ Zdej                  f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ej.                        deej*                     deej*                     deej*                     deej0                     dee   dee   dee   dee   deeef   fd„«       «       Z	 	 	 	 	 ddeej*                     deej*                     deej*                     deej*                     deej*                     f
d„Zˆ xZS )r¯  r«   ztext_model.lm_head.weightrw   c                 óÌ   •— t         ‰| �  |«       t        |j                  «      | _        t        |j                  «      | _        t        |«      | _	        | j                  «        y rë   )r‚   rƒ   r²  r´  r  r¬  r°  r  r¼  r  rÊ  r“   s     €r.   rƒ   z(Kosmos2ForConditionalGeneration.__init__‰  sN   ø€ Ü‰Ñ˜Ô ä0°×1CÑ1CÓDˆŒÜ.¨v×/CÑ/CÓDˆÔä(DÀVÓ(LˆÔ%ð 	�‰Õr0   rT   c                 óB   — | j                   j                  j                  S rë   r  rf   s    r.   rÌ  z4Kosmos2ForConditionalGeneration.get_input_embeddings”  r  r0   c                 ó:   — || j                   j                  _        y rë   r  rÜ  s     r.   rÞ  z4Kosmos2ForConditionalGeneration.set_input_embeddings—  r  r0   c                 ó6   — | j                   j                  «       S rë   )r  rë  rf   s    r.   rë  z5Kosmos2ForConditionalGeneration.get_output_embeddingsš  s   € Ø�‰×4Ñ4Ó6Ð6r0   c                 ó:   — | j                   j                  |«       y rë   )r  rï  rí  s     r.   rï  z5Kosmos2ForConditionalGeneration.set_output_embeddings�  s   € Ø�‰×-Ñ-¨nÕ=r0   rÍ  rG   r‘  rÔ   r’  rN   rQ   r  r   rð  rs  rÖ   r  r  c                 ó¦  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }d}d}|€|€t	        d«      ‚| j                  ||||¬«      }| j
                  j                  j                  |d   «      }t        j                  j                  |d¬«      }| j                  |«      \  }}| j                  ||||||||	|
||||¬«      }|s||||fz   }t        d„ |D «       «      S t        |j                  |j                   |j"                  |j$                  |j&                  |||¬	«      S )
a	  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in
            `[-100, 0, ..., config.vocab_size]` (see `input_ids` docstring) Tokens with indices set to `-100` are
            ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`

        Returns:

        Examples:

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

        >>> model = Kosmos2ForConditionalGeneration.from_pretrained("microsoft/kosmos-2-patch14-224")
        >>> processor = AutoProcessor.from_pretrained("microsoft/kosmos-2-patch14-224")

        >>> url = "https://huggingface.co/microsoft/kosmos-2-patch14-224/resolve/main/snowman.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> prompt = "<grounding> An image of"

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

        >>> generated_ids = model.generate(
        ...     pixel_values=inputs["pixel_values"],
        ...     input_ids=inputs["input_ids"],
        ...     attention_mask=inputs["attention_mask"],
        ...     image_embeds=None,
        ...     image_embeds_position_mask=inputs["image_embeds_position_mask"],
        ...     use_cache=True,
        ...     max_new_tokens=64,
        ... )
        >>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
        >>> processed_text = processor.post_process_generation(generated_text, cleanup_and_extract=False)
        >>> processed_text
        '<grounding> An image of<phrase> a snowman</phrase><object><patch_index_0044><patch_index_0863></object> warming himself by<phrase> a fire</phrase><object><patch_index_0005><patch_index_0911></object>.'

        >>> caption, entities = processor.post_process_generation(generated_text)
        >>> caption
        'An image of a snowman warming himself by a fire.'

        >>> entities
        [('a snowman', (12, 21), [(0.390625, 0.046875, 0.984375, 0.828125)]), ('a fire', (41, 47), [(0.171875, 0.015625, 0.484375, 0.890625)])]
        ```Nr  )r«   rÖ   r  r  r   r5   r7   )rG   rÔ   rQ   r‘  r’  rN   r  r   rð  rs  rÖ   r  r  c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrë   rn   r  s     r.   r`   z:Kosmos2ForConditionalGeneration.forward.<locals>.<genexpr>  r  r  )rq   rr   rN   rO   rP   rQ   rR   rS   )rw   rÖ   r  r  r°   r  rÉ  r"  r	   r£   r  r  r  rd   rp   rq   rr   rN   rO   rP   )r_   r«   rG   r‘  rÔ   r’  rN   rQ   r  r   rð  rs  rÖ   r  r  rS   rR   Ú
lm_outputsr  s                      r.   r¸   z'Kosmos2ForConditionalGeneration.forward   s’  € ðB 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà"ÐØ $ÐØÐØÐ#Ü Ð!_Ó`Ð`à"&×"3Ñ"3Ø)Ø"3Ø%9Ø'ð	 #4ó #Ðð  ×,Ñ,×2Ñ2×AÑAÐBUÐVWÑBXÓYˆLäŸ=™=×2Ñ2°<ÀRÐ2ÓHˆLØ26×2OÑ2OÐP\Ó2]Ñ/ˆLÐ/à—_‘_ØØ)Ø%Ø'AØØ+Ø'Ø%ØØØ/Ø!5Ø#ð %ó 
ˆ
ñ  Ø  LÐ2GÐI\Ð#]Ñ]ˆGÜÑL¨gÔLÓLÐLä9Ø—‘Ø×$Ñ$Ø&×6Ñ6Ø$×2Ñ2Ø!×,Ñ,Ø%Ø"7Ø 3ô	
ð 		
r0   c                 ó~  — |j                  dd «      }|�|�t        d|› d�«      ‚|€|�|}|€n| j                  |«      }| j                  j                  j	                  |d   «      }t
        j                  j                  |d¬«      }| j                  |«      \  }}	 | j                  j                  d||||dœ|¤Ž}
|
S )	NÚinputsz
`inputs`: zp were passed alongside `pixel_values` which is not allowed.Make sure to either pass `inputs` or pixel_values=...r   r5   r7   )rG   rÔ   rQ   r‘  rn   )Úpopr°   r  rÉ  r"  r	   r£   r  r  r  Úgenerate)r_   r«   r‘  rG   rÔ   rQ   Úkwargsr+  rS   rR   r÷  s              r.   r-  z(Kosmos2ForConditionalGeneration.generate  só   € ð —‘˜H dÓ+ˆØÐ#¨Ð(:ÜØ˜V˜Hð %Hð Ióð ð Ð FÐ$6Ø!ˆLàÐØ"&×"3Ñ"3°LÓ"AÐà×,Ñ,×2Ñ2×AÑAÐBUÐVWÑBXÓYˆLäŸ=™=×2Ñ2°<ÀRÐ2ÓHˆLØ26×2OÑ2OÐP\Ó2]Ñ/ˆLÐ/à)�—‘×)Ñ)ð 
ØØ)Ø%Ø'Añ	
ð
 ñ
ˆð ˆr0   )NNNNNNNNNNNNNNr  )rh   ri   rj   r   rÄ  rÓ  r  rƒ   r	   rÔ  rÌ  rÞ  rë  rï  r   r  r   rp   r   r   r&   rº   r   rl   r  r'   r   r   r¸   r-  r»   r¼   s   @r.   r¯  r¯  }  s>  ø„ ð !€LØ$€OØ5Ð6Ðð	˜}õ 	ð2 b§i¡ió 2ò3ð7 r§y¡yó 7ò>ñ +Ð+CÓDÙÐ+UÐdsÔtð 04Ø,0Ø=AØ15Ø,0Ø=AØ/3Ø04Ø/3Ø-1Ø$(Ø,0Ø/3Ø&*ñt
à˜uŸ|™|Ñ,ðt
ð ˜EŸL™LÑ)ðt
ð %-¨U¯\©\Ñ$:ð	t
ð
 ! §¡Ñ.ðt
ð ˜EŸL™LÑ)ðt
ð " $ u×'8Ñ'8Ñ"9Ñ:ðt
ð ˜uŸ|™|Ñ,ðt
ð   §¡Ñ-ðt
ð ˜uŸ|™|Ñ,ðt
ð ˜×)Ñ)Ñ*ðt
ð ˜D‘>ðt
ð $ D™>ðt
ð ' t™nðt
ð ˜d‘^ðt
ð  
ˆuÐ@Ð@Ñ	Aò!t
ó uó Eðt
ðp 04Ø=AØ,0Ø15Ø/3ñ#à˜uŸ|™|Ñ,ð#ð %-¨U¯\©\Ñ$:ð#ð ˜EŸL™LÑ)ð	#ð
 ! §¡Ñ.ð#ð ˜uŸ|™|Ñ,÷#r0   r¯  )r¯  r®  r§  rë   )r   )Krk   r5  Údataclassesr   Útypingr   r   r   r   r   r&   Útorch.utils.checkpointr	   Útorch.nnr
   Úactivationsr   Ú
generationr   Úmodeling_outputsr   r   r   r   Úmodeling_utilsr   Úutilsr   r   r   r   r   r   Úconfiguration_kosmos2r   r   r   Ú
get_loggerrh   r˜  r   rº   r   rC   r/   ÚSizer2   r@   rJ   ÚKOSMOS2_START_DOCSTRINGrÕ  râ  r  rL   rp   rÔ  rv   r¾   ré   rô   r  r  r)  rJ  r`  rh  r|  r§  r¬  r±  r²  r¼  r®  r¯  Ú__all__rn   r0   r.   ú<module>r=     sÊ  ðñ ã Ý !ß 4Õ 4ã Û Ý Ý %å !Ý )÷ó õ .÷÷ ÷ YÑ Xð 
ˆ×	Ñ	˜HÓ	%€à€ñ[�u—|‘|ð [¨E¯K©Kð [À(È3Á-ó [ð jkñ\Ø—Z‘Zð\Ø(-¯©ð\Ø=B¿\¹\ð\Øcfó\ó$4ð Ð ð#Ð ð"J!Ð ðX>Ð ðB ô3
˜ó 3
ó ð3
ðl ô6
°ó 6
ó ð6
ôtP˜bŸi™iô Pôhe2˜RŸY™Yô e2ôR�r—y‘yô ô / §	¡	ô /ôf^
˜2Ÿ9™9ô ^
ôD3
˜rŸy™yô 3
ônUc¨r¯y©yô Ucôpt9˜"Ÿ)™)ô t9ôn�R—Y‘Yô ô.l�r—y‘yô lô^u
˜RŸY™Yô u
ôp[Y˜_ô [Yô|#
Ð/ô #
ôL7
Ð-ô 7
ñt ðð óôeÐ3°_ó eóðeôP + 2§9¡9ô  +ñF ðð ó	ô|
Ð)ó |
óð|
ñ~ ðð óôwÐ&<¸oó wóðwòt X�r0   