Ë
    T^(h ã                   óæ  — d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	m
Z
 ddlZddlmZ ddlmZmZ ddlmZmZ dd	lmZ dd
lmZmZmZmZmZmZ ddlmZ  ej<                  e«      Z dZ!dZ" G d„ dejF                  «      Z$e G d„ de«      «       Z%e G d„ de«      «       Z&e G d„ de«      «       Z'd„ Z( G d„ dejF                  «      Z) G d„ dejF                  «      Z* G d„ dejF                  «      Z+ G d„ dejF                  «      Z, G d „ d!ejF                  «      Z- G d"„ d#ejF                  «      Z. G d$„ d%ejF                  «      Z/ G d&„ d'ejF                  «      Z0 G d(„ d)ejF                  «      Z1 G d*„ d+ejF                  «      Z2 G d,„ d-ejF                  «      Z3 G d.„ d/ejF                  «      Z4 G d0„ d1ejF                  «      Z5 G d2„ d3ejF                  «      Z6 G d4„ d5ejF                  «      Z7 G d6„ d7ejF                  «      Z8 G d8„ d9ejF                  «      Z9 G d:„ d;e«      Z:d<Z;d=Z< ed>e;«       G d?„ d@e:«      «       Z= edAe;«       G dB„ dCe:«      «       Z> edDe;«       G dE„ dFe:«      «       Z?g dG¢Z@y)HzPyTorch LXMERT model.é    N)Ú	dataclass)ÚDictÚOptionalÚTupleÚUnion)Únn)ÚCrossEntropyLossÚSmoothL1Lossé   )ÚACT2FNÚgelu)ÚPreTrainedModel)ÚModelOutputÚadd_code_sample_docstringsÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚloggingÚreplace_return_docstringsé   )ÚLxmertConfigzunc-nlp/lxmert-base-uncasedr   c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚGeLUc                 ó"   •— t         ‰| �  «        y ©N)ÚsuperÚ__init__)ÚselfÚ	__class__s    €úh/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/lxmert/modeling_lxmert.pyr   zGeLU.__init__/   s   ø€ Ü‰ÑÕó    c                 ó   — t        |«      S r   )r   )r   Úxs     r   ÚforwardzGeLU.forward2   s   € Ü�A‹wˆr    ©Ú__name__Ú
__module__Ú__qualname__r   r#   Ú__classcell__©r   s   @r   r   r   .   s   ø„ ôör    r   c                   óp  — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eej                     ed<   dZeeej                        ed<   dZ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ej                        ed
<   y)ÚLxmertModelOutputak  
    Lxmert's outputs that contain the last hidden states, pooled outputs, and attention probabilities for the language,
    visual, and, cross-modality encoders. (note: the visual encoder in Lxmert is referred to as the "relation-ship"
    encoder")


    Args:
        language_output (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
            Sequence of hidden-states at the output of the last layer of the language encoder.
        vision_output (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
            Sequence of hidden-states at the output of the last layer of the visual encoder.
        pooled_output (`torch.FloatTensor` of shape `(batch_size, hidden_size)`):
            Last layer hidden-state of the first token of the sequence (classification, CLS, token) further processed
            by a Linear layer and a Tanh activation function. The Linear
        language_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 input features + one for the output of each cross-modality layer) of
            shape `(batch_size, sequence_length, hidden_size)`.
        vision_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 input features + one for the output of each cross-modality layer) of
            shape `(batch_size, sequence_length, hidden_size)`.
        language_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.
        vision_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.
        cross_encoder_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`. Attentions weights after the attention softmax, used to compute the weighted average in
            the self-attention heads.
    NÚlanguage_outputÚvision_outputÚpooled_outputÚlanguage_hidden_statesÚvision_hidden_statesÚlanguage_attentionsÚvision_attentionsÚcross_encoder_attentions)r%   r&   r'   Ú__doc__r,   r   ÚtorchÚFloatTensorÚ__annotations__r-   r.   r/   r   r0   r1   r2   r3   © r    r   r+   r+   6   sÐ   … ñ ðD 48€O�X˜e×/Ñ/Ñ0Ó7Ø15€M�8˜E×-Ñ-Ñ.Ó5Ø15€M�8˜E×-Ñ-Ñ.Ó5ØAEÐ˜H U¨5×+<Ñ+<Ñ%=Ñ>ÓEØ?CÐ˜( 5¨×):Ñ):Ñ#;Ñ<ÓCØ>BÐ˜ %¨×(9Ñ(9Ñ":Ñ;ÓBØ<@Ð�x  e×&7Ñ&7Ñ 8Ñ9Ó@ØCGÐ˜h u¨U×->Ñ->Ñ'?Ñ@ÔGr    r+   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j                        ed<   dZ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ej                        ed	<   y)
Ú LxmertForQuestionAnsweringOutputa¹	  
    Output type of [`LxmertForQuestionAnswering`].

    Args:
        loss (*optional*, returned when `labels` is provided, `torch.FloatTensor` of shape `(1,)`):
            Total loss as the sum of the masked language modeling loss and the next sequence prediction
            (classification) loss.k.
        question_answering_score (`torch.FloatTensor` of shape `(batch_size, n_qa_answers)`, *optional*):
            Prediction scores of question answering objective (classification).
        language_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 input features + one for the output of each cross-modality layer) of
            shape `(batch_size, sequence_length, hidden_size)`.
        vision_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 input features + one for the output of each cross-modality layer) of
            shape `(batch_size, sequence_length, hidden_size)`.
        language_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.
        vision_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.
        cross_encoder_attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`. Attentions weights after the attention softmax, used to compute the weighted average in
            the self-attention heads.
    NÚlossÚquestion_answering_scorer/   r0   r1   r2   r3   )r%   r&   r'   r4   r;   r   r5   r6   r7   r<   r/   r   r0   r1   r2   r3   r8   r    r   r:   r:   d   s»   … ñð: )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø<@Ð˜h u×'8Ñ'8Ñ9Ó@ØAEÐ˜H U¨5×+<Ñ+<Ñ%=Ñ>ÓEØ?CÐ˜( 5¨×):Ñ):Ñ#;Ñ<ÓCØ>BÐ˜ %¨×(9Ñ(9Ñ":Ñ;ÓBØ<@Ð�x  e×&7Ñ&7Ñ 8Ñ9Ó@ØCGÐ˜h u¨U×->Ñ->Ñ'?Ñ@ÔGr    r:   c                   ó˜  — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eej                     ed<   dZeej                     ed<   dZeeej                        ed<   dZ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ej                        ed<   y)ÚLxmertForPreTrainingOutputak  
    Output type of [`LxmertForPreTraining`].

    Args:
        loss (*optional*, returned when `labels` is provided, `torch.FloatTensor` of shape `(1,)`):
            Total loss as the sum of the masked language modeling loss and the next sequence prediction
            (classification) loss.
        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).
        cross_relationship_score (`torch.FloatTensor` of shape `(batch_size, 2)`):
            Prediction scores of the textual matching objective (classification) head (scores of True/False
            continuation before SoftMax).
        question_answering_score (`torch.FloatTensor` of shape `(batch_size, n_qa_answers)`):
            Prediction scores of question answering objective (classification).
        language_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 input features + one for the output of each cross-modality layer) of
            shape `(batch_size, sequence_length, hidden_size)`.
        vision_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 input features + one for the output of each cross-modality layer) of
            shape `(batch_size, sequence_length, hidden_size)`.
        language_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.
        vision_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.
        cross_encoder_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.

    Nr;   Úprediction_logitsÚcross_relationship_scorer<   r/   r0   r1   r2   r3   )r%   r&   r'   r4   r;   r   r5   r6   r7   r?   r@   r<   r/   r   r0   r1   r2   r3   r8   r    r   r>   r>   Œ   sè   … ñ!ðF )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø59Ð�x × 1Ñ 1Ñ2Ó9Ø<@Ð˜h u×'8Ñ'8Ñ9Ó@Ø<@Ð˜h u×'8Ñ'8Ñ9Ó@ØAEÐ˜H U¨5×+<Ñ+<Ñ%=Ñ>ÓEØ?CÐ˜( 5¨×):Ñ):Ñ#;Ñ<ÓCØ>BÐ˜ %¨×(9Ñ(9Ñ":Ñ;ÓBØ<@Ð�x  e×&7Ñ&7Ñ 8Ñ9Ó@ØCGÐ˜h u¨U×->Ñ->Ñ'?Ñ@ÔGr    r>   c           	      ó  — 	 ddl }ddl}ddl}t        j                  j                  |«      }t        j                  d|› �«       |j                  j                  |«      }g }g }	|D ]^  \  }
}t        j                  d|
› d|› �«       |j                  j                  ||
«      }|j                  |
«       |	j                  |«       Œ` t        ||	«      D �]’  \  }
}|
j                  d«      }
t!        d„ |
D «       «      r(t        j                  d	dj#                  |
«      › �«       ŒR| }|
D ]À  }|j%                  d
|«      r|j                  d|«      }n|g}|d   dk(  s|d   dk(  rt'        |d«      }nW|d   dk(  s|d   dk(  rt'        |d«      }n:|d   dk(  rt'        |d«      }n%|d   dk(  rt'        |d«      }n	 t'        ||d   «      }t+        |«      dk\  sŒ®t-        |d   «      }||   }ŒÂ dd dk(  rt'        |d«      }n|dk(  r|j/                  |«      }	 |j0                  |j0                  k(  sJ ‚	 t        j                  d|
› �«       t7        j8                  |«      |_        �Œ• | S # t        $ r t        j                  d«       ‚ w xY w# t(        $ r+ t        j                  d	dj#                  |
«      › �«       Y �Œ˜w xY w# t2        $ r1}|xj4                  |j0                  |j0                  fz  c_        ‚ d}~ww xY w)z'Load tf checkpoints in a pytorch model.r   Nz™Loading a TensorFlow model in PyTorch, requires TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.z&Converting TensorFlow checkpoint from zLoading TF weight z with shape ú/c              3   ó$   K  — | ]  }|d v –— Œ
 y­w))Úadam_vÚadam_mÚAdamWeightDecayOptimizerÚAdamWeightDecayOptimizer_1Úglobal_stepNr8   )Ú.0Úns     r   ú	<genexpr>z,load_tf_weights_in_lxmert.<locals>.<genexpr>Ù   s$   è ø€ ò 

ð ð ðôñ

ùs   ‚z	Skipping z[A-Za-z]+_\d+z_(\d+)ÚkernelÚgammaÚweightÚoutput_biasÚbetaÚbiasÚoutput_weightsÚsquadÚ
classifieré   r   iõÿÿÿÚ_embeddingszInitialize PyTorch weight )ÚreÚnumpyÚ
tensorflowÚImportErrorÚloggerÚerrorÚosÚpathÚabspathÚinfoÚtrainÚlist_variablesÚload_variableÚappendÚzipÚsplitÚanyÚjoinÚ	fullmatchÚgetattrÚAttributeErrorÚlenÚintÚ	transposeÚshapeÚAssertionErrorÚargsr5   Ú
from_numpyÚdata)ÚmodelÚconfigÚtf_checkpoint_pathrW   ÚnpÚtfÚtf_pathÚ	init_varsÚnamesÚarraysÚnamero   ÚarrayÚpointerÚm_nameÚscope_namesÚnumÚes                     r   Úload_tf_weights_in_lxmertr„   ¼   s  € ð
ÛãÛô �g‰g�o‰oÐ0Ó1€GÜ
‡K�KÐ8¸¸	ÐBÔCà—‘×'Ñ'¨Ó0€IØ€EØ€FØ ò ‰ˆˆeÜ�‰Ð(¨¨¨l¸5¸'ÐBÔCØ—‘×&Ñ& w°Ó5ˆØ�‰�TÔØ�‰�eÕð	ô ˜5 &Ó)ó 2/‰ˆˆeØ�z‰z˜#‹ˆô ñ 

ð ô

ô 

ô �K‰K˜) C§H¡H¨T£NÐ#3Ð4Ô5ØØˆØò 	'ˆFØ�|‰|Ð,¨fÔ5Ø Ÿh™h y°&Ó9‘à%˜h�Ø˜1‰~ Ò)¨[¸©^¸wÒ-FÜ! '¨8Ó4‘Ø˜Q‘ =Ò0°KÀ±NÀfÒ4LÜ! '¨6Ó2‘Ø˜Q‘Ð#3Ò3Ü! '¨8Ó4‘Ø˜Q‘ 7Ò*Ü! '¨<Ó8‘ðÜ% g¨{¸1©~Ó>�Gô �;Ó 1Ó$Ü˜+ a™.Ó)�Ø! #™,‘ð+	'ð, �#�$ˆ<˜=Ò(Ü˜g xÓ0‰GØ�xÒØ—L‘L Ó'ˆEð	Ø—=‘= E§K¡KÒ/Ð/Ñ/ô 	�‰Ð0°°Ð7Ô8Ü×'Ñ'¨Ó.ˆŽðe2/ðf €LøôK ò Ü�‰ðQô	
ð 	ðûôh &ò Ü—K‘K )¨C¯H©H°T«NÐ+;Ð <Ô=Úðûô ò 	Ø�FŠF�w—}‘} e§k¡kÐ2Ñ2�FØûð	ús5   ‚I+ Æ9JÈKÉ+ JÊ0KËKË	K?Ë,K:Ë:K?c                   ó*   ‡ — e Zd ZdZˆ fd„Zdd„Zˆ xZS )ÚLxmertEmbeddingszGConstruct the embeddings from word, position and token_type embeddings.c                 óÜ  •— t         ‰| �  «        t        j                  |j                  |j
                  d¬«      | _        t        j                  |j                  |j
                  d¬«      | _        t        j                  |j                  |j
                  d¬«      | _
        t        j                  |j
                  d¬«      | _        t        j                  |j                  «      | _        y )Nr   )Úpadding_idxçê-�™—q=©Úeps)r   r   r   Ú	EmbeddingÚ
vocab_sizeÚhidden_sizeÚword_embeddingsÚmax_position_embeddingsÚposition_embeddingsÚtype_vocab_sizeÚtoken_type_embeddingsÚ	LayerNormÚDropoutÚhidden_dropout_probÚdropout©r   ru   r   s     €r   r   zLxmertEmbeddings.__init__  s¥   ø€ Ü‰ÑÔÜ!Ÿ|™|¨F×,=Ñ,=¸v×?QÑ?QÐ_`ÔaˆÔÜ#%§<¡<°×0NÑ0NÐPV×PbÑPbÐpqÔ#rˆÔ Ü%'§\¡\°&×2HÑ2HÈ&×J\ÑJ\ÐjkÔ%lˆÔ"ô Ÿ™ f×&8Ñ&8¸eÔDˆŒÜ—z‘z &×"<Ñ"<Ó=ˆ�r    c                 óN  — |�|j                  «       }|j                  }n|j                  «       d d }|j                  }|d   }t        j                  |t        j                  |¬«      }|j                  d«      j                  |«      }|€:t        j                  |t        j                  | j                  j                  ¬«      }|€| j                  |«      }| j                  |«      }| j                  |«      }	||z   |	z   }
| j                  |
«      }
| j                  |
«      }
|
S )Néÿÿÿÿr   ©ÚdtypeÚdevicer   )Úsizer�   r5   ÚarangeÚlongÚ	unsqueezeÚexpandÚzerosÚposition_idsr�   r‘   r“   r”   r—   )r   Ú	input_idsÚtoken_type_idsÚinputs_embedsÚinput_shaper�   Ú
seq_lengthr¤   r‘   r“   Ú
embeddingss              r   r#   zLxmertEmbeddings.forward  s  € ØÐ Ø#Ÿ.™.Ó*ˆKØ×%Ñ%‰Fà'×,Ñ,Ó.¨s°Ð3ˆKØ"×)Ñ)ˆFØ  ‘^ˆ
ä—|‘| J´e·j±jÈÔPˆØ#×-Ñ-¨aÓ0×7Ñ7¸ÓDˆàÐ!Ü"Ÿ[™[¨¼E¿J¹JÈt×O`ÑO`×OgÑOgÔhˆNàÐ Ø ×0Ñ0°Ó;ˆMØ"×6Ñ6°|ÓDÐØ $× :Ñ :¸>Ó JÐà"Ð%8Ñ8Ð;PÑPˆ
Ø—^‘^ JÓ/ˆ
Ø—\‘\ *Ó-ˆ
ØÐr    ©NN)r%   r&   r'   r4   r   r#   r(   r)   s   @r   r†   r†     s   ø„ ÙQô	>÷r    r†   c                   ó.   ‡ — e Zd Zdˆ fd„	Zd„ Zdd„Zˆ xZS )ÚLxmertAttentionc                 ó¨  •— t         ‰| �  «        |j                  |j                  z  dk7  r&t	        d|j                  › d|j                  › d�«      ‚|j                  | _        t        |j                  |j                  z  «      | _        | j                  | j                  z  | _        |€|j                  }t        j                  |j                  | j                  «      | _
        t        j                  || j                  «      | _        t        j                  || j                  «      | _        t        j                  |j                  «      | _        y )Nr   zThe hidden size (z6) is not a multiple of the number of attention heads (ú))r   r   rŽ   Únum_attention_headsÚ
ValueErrorrm   Úattention_head_sizeÚ	head_sizer   ÚLinearÚqueryÚkeyÚvaluer•   Úattention_probs_dropout_probr—   )r   ru   Úctx_dimr   s      €r   r   zLxmertAttention.__init__4  s  ø€ Ü‰ÑÔØ×Ñ × :Ñ :Ñ:¸aÒ?ÜØ# F×$6Ñ$6Ð#7ð 8Ø ×4Ñ4Ð5°Qð8óð ð $*×#=Ñ#=ˆÔ Ü#& v×'9Ñ'9¸F×<VÑ<VÑ'VÓ#WˆÔ Ø×1Ñ1°D×4LÑ4LÑLˆŒð ˆ?Ø×(Ñ(ˆGÜ—Y‘Y˜v×1Ñ1°4·>±>ÓBˆŒ
Ü—9‘9˜W d§n¡nÓ5ˆŒÜ—Y‘Y˜w¨¯©Ó7ˆŒ
ä—z‘z &×"EÑ"EÓFˆ�r    c                 ó¤   — |j                  «       d d | j                  | j                  fz   }|j                  |«      }|j	                  dddd«      S )Nrš   r   rU   r   r   )rž   r°   r²   ÚviewÚpermute)r   r"   Únew_x_shapes      r   Útranspose_for_scoresz$LxmertAttention.transpose_for_scoresH  sT   € Ø—f‘f“h˜s �mØ×$Ñ$Ø×$Ñ$ð'
ñ 
ˆð �F‰F�;ÓˆØ�y‰y˜˜A˜q !Ó$Ð$r    c                 ó¾  — | j                  |«      }| j                  |«      }| j                  |«      }| j                  |«      }| j                  |«      }	| j                  |«      }
t	        j
                  ||	j                  dd«      «      }|t        j                  | j                  «      z  }|�||z   }t        j                  j                  |d¬«      }| j                  |«      }t	        j
                  ||
«      }|j                  dddd«      j                  «       }|j!                  «       d d | j"                  fz   }|j%                  |«      }|r||f}|S |f}|S )Nrš   éþÿÿÿ)Údimr   rU   r   r   )rµ   r¶   r·   r¾   r5   Úmatmulrn   ÚmathÚsqrtr²   r   Ú
functionalÚsoftmaxr—   r¼   Ú
contiguousrž   r³   r»   )r   Úhidden_statesÚcontextÚattention_maskÚoutput_attentionsÚmixed_query_layerÚmixed_key_layerÚmixed_value_layerÚquery_layerÚ	key_layerÚvalue_layerÚattention_scoresÚattention_probsÚcontext_layerÚnew_context_layer_shapeÚoutputss                   r   r#   zLxmertAttention.forwardP  s[  € Ø ŸJ™J }Ó5ÐØŸ(™( 7Ó+ˆØ ŸJ™J wÓ/Ðà×/Ñ/Ð0AÓBˆØ×-Ñ-¨oÓ>ˆ	Ø×/Ñ/Ð0AÓBˆô !Ÿ<™<¨°Y×5HÑ5HÈÈRÓ5PÓQÐØ+¬d¯i©i¸×8PÑ8PÓ.QÑQÐàÐ%Ø/°.Ñ@Ðô Ÿ-™-×/Ñ/Ð0@ÀbÐ/ÓIˆð Ÿ,™, Ó7ˆäŸ™ _°kÓBˆØ%×-Ñ-¨a°°A°qÓ9×DÑDÓFˆØ"/×"4Ñ"4Ó"6°s¸Ð";¸t¿~¹~Ð>OÑ"OÐØ%×*Ñ*Ð+BÓCˆá6G�= /Ð2ˆØˆð O\ÐM]ˆØˆr    r   ©NF)r%   r&   r'   r   r¾   r#   r(   r)   s   @r   r­   r­   3  s   ø„ õGò(%÷r    r­   c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚLxmertAttentionOutputc                 ó  •— t         ‰| �  «        t        j                  |j                  |j                  «      | _        t        j                  |j                  d¬«      | _        t        j                  |j                  «      | _	        y ©Nr‰   rŠ   )
r   r   r   r´   rŽ   Údenser”   r•   r–   r—   r˜   s     €r   r   zLxmertAttentionOutput.__init__q  sZ   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3EÑ3EÓFˆŒ
ÜŸ™ f×&8Ñ&8¸eÔDˆŒÜ—z‘z &×"<Ñ"<Ó=ˆ�r    c                 ór   — | j                  |«      }| j                  |«      }| j                  ||z   «      }|S r   ©rÜ   r—   r”   ©r   rÈ   Úinput_tensors      r   r#   zLxmertAttentionOutput.forwardw  ó7   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆØŸ™ }°|Ñ'CÓDˆØÐr    r$   r)   s   @r   rÙ   rÙ   p  ó   ø„ ô>ör    rÙ   c                   ó&   ‡ — e Zd Zˆ fd„Zdd„Zˆ xZS )ÚLxmertCrossAttentionLayerc                 ób   •— t         ‰| �  «        t        |«      | _        t	        |«      | _        y r   )r   r   r­   ÚattrÙ   Úoutputr˜   s     €r   r   z"LxmertCrossAttentionLayer.__init__  s&   ø€ Ü‰ÑÔÜ" 6Ó*ˆŒÜ+¨FÓ3ˆ�r    c                 ó~   — | j                  ||||¬«      }|r|d   }| j                  |d   |«      }|r|f}|S |f}|S ©N©rË   r   r   )ræ   rç   )	r   rà   Ú
ctx_tensorÚctx_att_maskrË   rç   rÓ   Úattention_outputrÖ   s	            r   r#   z!LxmertCrossAttentionLayer.forward„  s]   € Ø—‘˜,¨
°LÐTe�ÓfˆÙØ$ Q™iˆOØŸ;™; v¨a¡y°,Ó?ÐÙ9JÐ# _Ð5ˆØˆð RbÐPcˆØˆr    r×   r$   r)   s   @r   rä   rä   ~  s   ø„ ô4÷
r    rä   c                   ó&   ‡ — e Zd Zˆ fd„Zdd„Zˆ xZS )ÚLxmertSelfAttentionLayerc                 ób   •— t         ‰| �  «        t        |«      | _        t	        |«      | _        y r   )r   r   r­   r   rÙ   rç   r˜   s     €r   r   z!LxmertSelfAttentionLayer.__init__Ž  s&   ø€ Ü‰ÑÔÜ# FÓ+ˆŒ	Ü+¨FÓ3ˆ�r    c                 ó~   — | j                  ||||¬«      }|r|d   }| j                  |d   |«      }|r|f}|S |f}|S ré   )r   rç   )r   rà   rÊ   rË   rç   rÓ   rí   rÖ   s           r   r#   z LxmertSelfAttentionLayer.forward“  sg   € à—‘ØØØØ/ð	 ó 
ˆñ Ø$ Q™iˆOØŸ;™; v¨a¡y°,Ó?ÐÙ9JÐ# _Ð5ˆØˆð RbÐPcˆØˆr    ©Fr$   r)   s   @r   rï   rï   �  s   ø„ ô4÷
r    rï   c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚLxmertIntermediatec                 ó°   •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        |j                     | _	        y r   )
r   r   r   r´   rŽ   Úintermediate_sizerÜ   r   Ú
hidden_actÚintermediate_act_fnr˜   s     €r   r   zLxmertIntermediate.__init__£  s?   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3KÑ3KÓLˆŒ
Ü#)¨&×*;Ñ*;Ñ#<ˆÕ r    c                 óJ   — | j                  |«      }| j                  |«      }|S r   )rÜ   rø   ©r   rÈ   s     r   r#   zLxmertIntermediate.forward¨  s&   € ØŸ
™
 =Ó1ˆØ×0Ñ0°Ó?ˆØÐr    r$   r)   s   @r   rô   rô   ¢  s   ø„ ô=ö
r    rô   c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚLxmertOutputc                 ó  •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        j                  |j
                  d¬«      | _        t        j                  |j                  «      | _
        y rÛ   )r   r   r   r´   rö   rŽ   rÜ   r”   r•   r–   r—   r˜   s     €r   r   zLxmertOutput.__init__¯  sZ   ø€ Ü‰ÑÔÜ—Y‘Y˜v×7Ñ7¸×9KÑ9KÓLˆŒ
ÜŸ™ f×&8Ñ&8¸eÔDˆŒÜ—z‘z &×"<Ñ"<Ó=ˆ�r    c                 ór   — | j                  |«      }| j                  |«      }| j                  ||z   «      }|S r   rÞ   rß   s      r   r#   zLxmertOutput.forwardµ  rá   r    r$   r)   s   @r   rü   rü   ®  râ   r    rü   c                   ó&   ‡ — e Zd Zˆ fd„Zdd„Zˆ xZS )ÚLxmertLayerc                 ó‚   •— t         ‰| �  «        t        |«      | _        t	        |«      | _        t        |«      | _        y r   )r   r   rï   Ú	attentionrô   Úintermediaterü   rç   r˜   s     €r   r   zLxmertLayer.__init__½  s3   ø€ Ü‰ÑÔÜ1°&Ó9ˆŒÜ.¨vÓ6ˆÔÜ" 6Ó*ˆ�r    c                 ó�   — | j                  |||¬«      }|d   }| j                  |«      }| j                  ||«      }|f|dd  z   }|S )Nrê   r   r   )r  r  rç   )r   rÈ   rÊ   rË   rÖ   rí   Úintermediate_outputÚlayer_outputs           r   r#   zLxmertLayer.forwardÃ  s\   € Ø—.‘. °ÐRc�.ÓdˆØ" 1™:ÐØ"×/Ñ/Ð0@ÓAÐØ—{‘{Ð#6Ð8HÓIˆØ�/ G¨A¨B KÑ/ˆØˆr    r×   r$   r)   s   @r   r   r   ¼  s   ø„ ô+÷r    r   c                   ó>   ‡ — e Zd Zˆ fd„Z	 dd„Zd„ Zd„ Z	 dd„Zˆ xZS )ÚLxmertXLayerc                 ó  •— t         ‰| �  «        t        |«      | _        t	        |«      | _        t	        |«      | _        t        |«      | _        t        |«      | _
        t        |«      | _        t        |«      | _        y r   )r   r   rä   Úvisual_attentionrï   Úlang_self_attÚvisn_self_attrô   Ú
lang_interrü   Úlang_outputÚ
visn_interÚvisn_outputr˜   s     €r   r   zLxmertXLayer.__init__Í  sk   ø€ Ü‰ÑÔä 9¸&Ó AˆÔô 6°fÓ=ˆÔÜ5°fÓ=ˆÔô -¨VÓ4ˆŒÜ'¨Ó/ˆÔÜ,¨VÓ4ˆŒÜ'¨Ó/ˆÕr    c                 ó^   — | j                  ||||¬«      }| j                  |||d¬«      }||fS )N)rì   rË   F)r
  )r   Ú
lang_inputÚlang_attention_maskÚvisual_inputÚvisual_attention_maskÚoutput_x_attentionsÚlang_att_outputÚvisual_att_outputs           r   Ú	cross_attzLxmertXLayer.cross_attÜ  sW   € ð ×/Ñ/ØØØ.Ø1ð	 0ó 
ˆð !×1Ñ1ØØØ,Ø#ð	 2ó 
Ðð Ð 1Ð1Ð1r    c                 óf   — | j                  ||d¬«      }| j                  ||d¬«      }|d   |d   fS )NFrê   r   )r  r  )r   r  r  r  r  r  r  s          r   Úself_attzLxmertXLayer.self_attó  sK   € à×,Ñ,¨ZÐ9LÐ`eÐ,ÓfˆØ ×.Ñ.¨|Ð=RÐfkÐ.ÓlÐØ˜qÑ!Ð#4°QÑ#7Ð7Ð7r    c                 ó–   — | j                  |«      }| j                  |«      }| j                  ||«      }| j                  ||«      }||fS r   )r  r  r  r  )r   r  r  Úlang_inter_outputÚvisual_inter_outputr  Úvisual_outputs          r   Ú	output_fczLxmertXLayer.output_fcù  sS   € à ŸO™O¨JÓ7ÐØ"Ÿo™o¨lÓ;Ðð ×&Ñ&Ð'8¸*ÓEˆØ×(Ñ(Ð)<¸lÓKˆà˜MÐ)Ð)r    c                 ó¾   — | j                  |||||¬«      \  }}|dd  }| j                  |d   ||d   |«      \  }}| j                  ||«      \  }	}
|r|	|
|d   fS |	|
fS )N)r  r  r  r  r  r   r   )r  r  r   )r   Ú
lang_featsr  Úvisual_featsr  rË   r  r  rÓ   r  r  s              r   r#   zLxmertXLayer.forward  sª   € ð .2¯^©^Ø!Ø 3Ø%Ø"7Ø 1ð .<ó .
Ñ*ˆÐ*ð *¨!¨"Ð-ˆØ-1¯]©]Ø˜AÑØØ˜aÑ Ø!ó	.
Ñ*ˆÐ*ð &*§^¡^°OÐEVÓ%WÑ"ˆ�]ñ !ð	 ØØ Ñ"ðð	
ð ˜}Ð-ð	
r    rò   )	r%   r&   r'   r   r  r  r   r#   r(   r)   s   @r   r  r  Ì  s&   ø„ ô0ð* "ó2ò.8ò	*ð"  ÷ 
r    r  c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚLxmertVisualFeatureEncoderc                 óÆ  •— t         ‰| �  «        |j                  }|j                  }t	        j
                  ||j                  «      | _        t	        j                  |j                  d¬«      | _	        t	        j
                  ||j                  «      | _
        t	        j                  |j                  d¬«      | _        t	        j                  |j                  «      | _        y rÛ   )r   r   Úvisual_feat_dimÚvisual_pos_dimr   r´   rŽ   Úvisn_fcr”   Úvisn_layer_normÚbox_fcÚbox_layer_normr•   r–   r—   )r   ru   Úfeat_dimÚpos_dimr   s       €r   r   z#LxmertVisualFeatureEncoder.__init__(  s    ø€ Ü‰ÑÔØ×)Ñ)ˆØ×'Ñ'ˆô —y‘y ¨6×+=Ñ+=Ó>ˆŒÜ!Ÿ|™|¨F×,>Ñ,>ÀEÔJˆÔô —i‘i ¨×);Ñ);Ó<ˆŒÜ Ÿl™l¨6×+=Ñ+=À5ÔIˆÔä—z‘z &×"<Ñ"<Ó=ˆ�r    c                 óÀ   — | j                  |«      }| j                  |«      }| j                  |«      }| j                  |«      }||z   dz  }| j	                  |«      }|S ©NrU   )r)  r*  r+  r,  r—   )r   r#  Ú
visual_posr"   Úyrç   s         r   r#   z"LxmertVisualFeatureEncoder.forward7  s^   € Ø�L‰L˜Ó&ˆØ× Ñ  Ó#ˆØ�K‰K˜
Ó#ˆØ×Ñ Ó"ˆØ�a‘%˜1‘ˆà—‘˜fÓ%ˆØˆr    r$   r)   s   @r   r%  r%  '  s   ø„ ô>ör    r%  c                   ó*   ‡ — e Zd Zˆ fd„Z	 	 dd„Zˆ xZS )ÚLxmertEncoderc                 ól  •— t         ‰| �  «        t        |«      | _        || _        |j
                  | _        |j                  | _        |j                  | _
        t        j                  t        | j                  «      D �cg c]  }t        |«      ‘Œ c}«      | _        t        j                  t        | j                  «      D �cg c]  }t!        |«      ‘Œ c}«      | _        t        j                  t        | j                  «      D �cg c]  }t        |«      ‘Œ c}«      | _	        y c c}w c c}w c c}w r   )r   r   r%  r)  ru   Úl_layersÚnum_l_layersÚx_layersÚnum_x_layersÚr_layersÚnum_r_layersr   Ú
ModuleListÚranger   Úlayerr  )r   ru   Ú_r   s      €r   r   zLxmertEncoder.__init__C  sØ   ø€ Ü‰ÑÔô 2°&Ó9ˆŒØˆŒð #ŸO™OˆÔØ"ŸO™OˆÔØ"ŸO™OˆÔô —]‘]ÄÀt×GXÑGXÓAYÖ#Z¸A¤K°Õ$7Ò#ZÓ[ˆŒ
ÜŸ™ÄUÈ4×K\ÑK\ÓE]Ö&^À¤|°FÕ';Ò&^Ó_ˆŒÜŸ™ÄEÈ$×J[ÑJ[ÓD\Ö&]¸q¤{°6Õ':Ò&]Ó^ˆ�ùò $[ùÚ&^ùÚ&]s   Â D'ÃD,ÄD1c                 óR  — d}d}|s| j                   j                  rdnd }	|s| j                   j                  rdnd }
|s| j                   j                  rdnd }| j                  ||«      }| j                  D ]$  } ||||¬«      }|d   }||fz   }|
€Œ|
|d   fz   }
Œ& | j                  D ]$  } ||||¬«      }|d   }||fz   }|	€Œ|	|d   fz   }	Œ& | j
                  D ]/  } ||||||¬«      }|d d \  }}||fz   }||fz   }|€Œ'||d   fz   }Œ1 ||r|	nd f}||r|
nd f}|||r|fS d fS )Nr8   rê   r   r   rU   )ru   rË   r)  r>  r:  r8  )r   r"  r  r#  r1  r  rË   r0   r/   r2   r1   r3   Úlayer_moduleÚ	l_outputsÚ	v_outputsÚ	x_outputsÚvisual_encoder_outputsÚlang_encoder_outputss                     r   r#   zLxmertEncoder.forwardU  sÔ  € ð  "ÐØ!#ÐÙ"3°t·{±{×7TÒ7T™BÐZ^ÐÙ$5¸¿¹×9VÒ9V™bÐ\`ÐÙ):¸d¿k¹k×>[Ò>[¡2ÐaeÐ à—|‘| L°*Ó=ˆð !ŸJ™Jò 	LˆLÙ$ ZÐ1DÐXiÔjˆIØ" 1™ˆJØ%;¸z¸mÑ%KÐ"Ø"Ñ.Ø&9¸YÀq¹\¸OÑ&KÑ#ð	Lð !ŸM™Mò 	HˆLÙ$ \Ð3HÐ\mÔnˆIØ$ Q™<ˆLØ#7¸<¸/Ñ#IÐ Ø Ñ,Ø$5¸À1¹¸Ñ$GÑ!ð	Hð !ŸM™Mò 	VˆLÙ$ØØ#ØØ%Ø"3ôˆIð (1°°! }Ñ$ˆJ˜Ø#7¸<¸/Ñ#IÐ Ø%;¸z¸mÑ%KÐ"Ø'Ñ3Ø+CÀyÐQRÁ|ÀoÑ+UÑ(ð	Vð !Ù!2Ñ¸ð"
Ðð
 #Ù#4Ñ¸$ð 
Ðð
 #Ø Ù(9Ð$ð
ð 	
ð @Dð
ð 	
r    r«   r$   r)   s   @r   r4  r4  B  s   ø„ ô_ð0 #Ø÷;
r    r4  c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚLxmertPoolerc                 óº   •— t         t        | �  «        t        j                  |j
                  |j
                  «      | _        t        j                  «       | _        y r   )	r   rH  r   r   r´   rŽ   rÜ   ÚTanhÚ
activationr˜   s     €r   r   zLxmertPooler.__init__”  s;   ø€ ÜŒl˜DÑ*Ô,Ü—Y‘Y˜v×1Ñ1°6×3EÑ3EÓFˆŒ
ÜŸ'™'›)ˆ�r    c                 ó\   — |d d …df   }| j                  |«      }| j                  |«      }|S )Nr   )rÜ   rK  )r   rÈ   Úfirst_token_tensorr.   s       r   r#   zLxmertPooler.forward™  s6   € ð +ª1¨a¨4Ñ0ÐØŸ
™
Ð#5Ó6ˆØŸ™¨Ó6ˆØÐr    r$   r)   s   @r   rH  rH  “  s   ø„ ô$ö
r    rH  c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚLxmertPredictionHeadTransformc                 ó  •— t         t        | �  «        t        j                  |j
                  |j
                  «      | _        t        |j                     | _	        t        j                  |j
                  d¬«      | _
        y rÛ   )r   rO  r   r   r´   rŽ   rÜ   r   r÷   Útransform_act_fnr”   r˜   s     €r   r   z&LxmertPredictionHeadTransform.__init__£  sZ   ø€ ÜÔ+¨TÑ;Ô=Ü—Y‘Y˜v×1Ñ1°6×3EÑ3EÓFˆŒ
Ü & v×'8Ñ'8Ñ 9ˆÔÜŸ™ f×&8Ñ&8¸eÔDˆ�r    c                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S r   )rÜ   rQ  r”   rú   s     r   r#   z%LxmertPredictionHeadTransform.forward©  s4   € ØŸ
™
 =Ó1ˆØ×-Ñ-¨mÓ<ˆØŸ™ }Ó5ˆØÐr    r$   r)   s   @r   rO  rO  ¢  s   ø„ ôEör    rO  c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚLxmertLMPredictionHeadc                 óZ  •— t         t        | �  «        t        |«      | _        t        j                  |j                  d«      |j                  d«      d¬«      | _        || j                  _	        t        j                  t        j                  |j                  d«      «      «      | _        y )Nr   r   F©rQ   )r   rT  r   rO  Ú	transformr   r´   rž   ÚdecoderrN   Ú	Parameterr5   r£   rQ   ©r   ru   Úlxmert_model_embedding_weightsr   s      €r   r   zLxmertLMPredictionHead.__init__±  s‚   ø€ ÜÔ$ dÑ4Ô6Ü6°vÓ>ˆŒô —y‘yØ*×/Ñ/°Ó2Ø*×/Ñ/°Ó2Øô
ˆŒð
 =ˆ�‰ÔÜ—L‘L¤§¡Ð-K×-PÑ-PÐQRÓ-SÓ!TÓUˆ�	r    c                 ód   — | j                  |«      }| j                  |«      | j                  z   }|S r   )rW  rX  rQ   rú   s     r   r#   zLxmertLMPredictionHead.forward¿  s-   € ØŸ™ }Ó5ˆØŸ™ ]Ó3°d·i±iÑ?ˆØÐr    r$   r)   s   @r   rT  rT  °  s   ø„ ôVör    rT  c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚLxmertVisualAnswerHeadc           	      ó  •— t         ‰| �  «        |j                  }t        j                  t        j
                  ||dz  «      t        «       t        j                  |dz  d¬«      t        j
                  |dz  |«      «      | _        y )NrU   r‰   rŠ   )	r   r   rŽ   r   Ú
Sequentialr´   r   r”   Úlogit_fc)r   ru   Ú
num_labelsÚhid_dimr   s       €r   r   zLxmertVisualAnswerHead.__init__Æ  sd   ø€ Ü‰ÑÔØ×$Ñ$ˆÜŸ™Ü�I‰I�g˜w¨™{Ó+Ü‹FÜ�L‰L˜ 1™¨%Ô0Ü�I‰I�g ‘k :Ó.ó	
ˆ�r    c                 ó$   — | j                  |«      S r   )ra  rú   s     r   r#   zLxmertVisualAnswerHead.forwardÐ  s   € Ø�}‰}˜]Ó+Ð+r    r$   r)   s   @r   r^  r^  Å  s   ø„ ô
ö,r    r^  c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚLxmertVisualObjHeadc                 óì  •— t         ‰| �  «        t        |«      | _        i }|j                  rd|j
                  dœ|d<   |j                  rd|j                  dœ|d<   |j                  rd|j                  f|j                  dœ|d<   || _
        t        j                  | j                  D �ci c]3  }|t        j                  |j                  | j                  |   d   «      “Œ5 c}«      | _        y c c}w )N©rš   )ro   r‚   ÚobjÚattrrš   Úfeatr‚   )r   r   rO  rW  Úvisual_obj_lossÚnum_object_labelsÚvisual_attr_lossÚnum_attr_labelsÚvisual_feat_lossr'  Úvisual_lossesr   Ú
ModuleDictr´   rŽ   Údecoder_dict)r   ru   rq  r¶   r   s       €r   r   zLxmertVisualObjHead.__init__Õ  sâ   ø€ Ü‰ÑÔÜ6°vÓ>ˆŒàˆØ×!Ò!Ø-2¸6×;SÑ;SÑ#TˆM˜%Ñ Ø×"Ò"Ø.3¸F×<RÑ<RÑ$SˆM˜&Ñ!Ø×"Ò"à˜f×4Ñ4Ð5Ø×-Ñ-ñ%ˆM˜&Ñ!ð +ˆÔô ŸM™MØ[_×[mÑ[mÖnÐTWˆS”"—)‘)˜F×.Ñ.°×0BÑ0BÀ3Ñ0GÈÑ0NÓOÑOÒnó
ˆÕùÚns   Â,8C1c                 ó~   — | j                  |«      }i }| j                  D ]  } | j                  |   |«      ||<   Œ |S r   )rW  rq  rs  )r   rÈ   rç   r¶   s       r   r#   zLxmertVisualObjHead.forwardë  sL   € ØŸ™ }Ó5ˆØˆØ×%Ñ%ò 	@ˆCØ0˜$×+Ñ+¨CÑ0°Ó?ˆF�3ŠKð	@àˆr    r$   r)   s   @r   rf  rf  Ô  s   ø„ ô
ö,r    rf  c                   ó$   ‡ — e Zd Zˆ fd„Zd„ Zˆ xZS )ÚLxmertPreTrainingHeadsc                 ó–   •— t         t        | �  «        t        ||«      | _        t        j                  |j                  d«      | _        y r0  )	r   rv  r   rT  Úpredictionsr   r´   rŽ   Úseq_relationshiprZ  s      €r   r   zLxmertPreTrainingHeads.__init__ô  s:   ø€ ÜÔ$ dÑ4Ô6Ü1°&Ð:XÓYˆÔÜ "§	¡	¨&×*<Ñ*<¸aÓ @ˆÕr    c                 óN   — | j                  |«      }| j                  |«      }||fS r   )rx  ry  )r   Úsequence_outputr.   Úprediction_scoresÚseq_relationship_scores        r   r#   zLxmertPreTrainingHeads.forwardù  s0   € Ø ×,Ñ,¨_Ó=ÐØ!%×!6Ñ!6°}Ó!EÐØ Ð"8Ð8Ð8r    r$   r)   s   @r   rv  rv  ó  s   ø„ ôAö
9r    rv  c                   ó&   — e Zd ZdZeZeZdZdZ	d„ Z
y)ÚLxmertPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    ÚlxmertFc                 ól  — t        |t        j                  «      rm|j                  j                  j                  d| j                  j                  ¬«       |j                  �%|j                  j                  j                  «        yyt        |t        j                  «      rz|j                  j                  j                  d| j                  j                  ¬«       |j                  �2|j                  j                  |j                     j                  «        yyt        |t        j                  «      rJ|j                  j                  j                  «        |j                  j                  j                  d«       yt        |t        «      r%|j                  j                  j                  «        yy)zInitialize the weightsç        )ÚmeanÚstdNç      ð?)Ú
isinstancer   r´   rN   rs   Únormal_ru   Úinitializer_rangerQ   Úzero_rŒ   rˆ   r”   Úfill_rT  )r   Úmodules     r   Ú_init_weightsz#LxmertPreTrainedModel._init_weights
  s'  € ä�fœbŸi™iÔ(ð �M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ä˜¤§¡Ô-Ø�M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ×!Ñ!Ð-Ø—‘×"Ñ" 6×#5Ñ#5Ñ6×<Ñ<Õ>ð .ä˜¤§¡Ô-Ø�K‰K×Ñ×"Ñ"Ô$Ø�M‰M×Ñ×$Ñ$ SÕ)Ü˜Ô 6Ô7Ø�K‰K×Ñ×"Ñ"Õ$ð 8r    N)r%   r&   r'   r4   r   Úconfig_classr„   Úload_tf_weightsÚbase_model_prefixÚ!_supports_param_buffer_assignmentrŒ  r8   r    r   r  r  ÿ  s$   „ ñð
  €LØ/€OØ ÐØ(-Ð%ó%r    r  aR  

    The LXMERT model was proposed in [LXMERT: Learning Cross-Modality Encoder Representations from
    Transformers](https://arxiv.org/abs/1908.07490) by Hao Tan and Mohit Bansal. It's a vision and language transformer
    model, pretrained on a variety of multi-modal datasets comprising of GQA, VQAv2.0, MSCOCO captions, and Visual
    genome, using a combination of masked language modeling, region of interest feature regression, cross entropy loss
    for question answering attribute prediction, and object tag prediction.

    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 ([`LxmertConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
a  

    Args:
        input_ids (`torch.LongTensor` of shape `({0})`):
            Indices of input sequence tokens in the vocabulary.

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

            [What are input IDs?](../glossary#input-ids)
        visual_feats (`torch.FloatTensor` of shape `(batch_size, num_visual_features, visual_feat_dim)`):
            This input represents visual features. They ROI pooled object features from bounding boxes using a
            faster-RCNN model)

            These are currently not provided by the transformers library.
        visual_pos (`torch.FloatTensor` of shape `(batch_size, num_visual_features, visual_pos_dim)`):
            This input represents spacial features corresponding to their relative (via index) visual features. The
            pre-trained LXMERT model expects these spacial features to be normalized bounding boxes on a scale of 0 to
            1.

            These are currently not provided by the transformers library.
        attention_mask (`torch.FloatTensor` of shape `({0})`, *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)
        visual_attention_mask (`torch.FloatTensor` of shape `({0})`, *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)
        token_type_ids (`torch.LongTensor` of shape `({0})`, *optional*):
            Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0,
            1]`:

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

            [What are token type IDs?](../glossary#token-type-ids)
        inputs_embeds (`torch.FloatTensor` of shape `({0}, hidden_size)`, *optional*):
            Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
            is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
            model's internal embedding lookup matrix.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
z`The bare Lxmert Model transformer outputting raw hidden-states without any specific head on top.c                   ó°  ‡ — e Zd Zˆ fd„Zd„ Zd„ Z eej                  d«      «       e	e
ee¬«      	 	 	 	 	 	 	 	 	 	 ddeej                     deej                      deej                      d	eej                      d
eej                      deej                     deej                      dee   dee   dee   deeeej                      f   fd„«       «       Zˆ xZS )ÚLxmertModelc                 ó¤   •— t         ‰| �  |«       t        |«      | _        t	        |«      | _        t        |«      | _        | j                  «        y r   )	r   r   r†   rª   r4  ÚencoderrH  ÚpoolerÚ	post_initr˜   s     €r   r   zLxmertModel.__init__r  s>   ø€ Ü‰Ñ˜Ô Ü*¨6Ó2ˆŒÜ$ VÓ,ˆŒÜ" 6Ó*ˆŒà�‰Õr    c                 ó.   — | j                   j                  S r   ©rª   r�   ©r   s    r   Úget_input_embeddingsz LxmertModel.get_input_embeddingsz  s   € Ø�‰×.Ñ.Ð.r    c                 ó&   — || j                   _        y r   r˜  )r   Únew_embeddingss     r   Úset_input_embeddingsz LxmertModel.set_input_embeddings}  s   € Ø*8ˆ�‰Õ'r    úbatch_size, sequence_length©Ú
checkpointÚoutput_typer�  r¥   r#  r1  rÊ   r  r¦   r§   rË   Úoutput_hidden_statesÚreturn_dictÚreturnc           
      óH  — |�|n| j                   j                  }|	�|	n| j                   j                  }	|
�|
n| j                   j                  }
|�|�t	        d«      ‚|�#| j                  ||«       |j                  «       }n!|�|j                  «       d d }nt	        d«      ‚|€t	        d«      ‚|€t	        d«      ‚|�|j                  n|j                  }|€t        j                  ||¬«      }|€&t        j                  |t        j                  |¬«      }|j                  d«      j                  d	«      }|j                  | j                  ¬
«      }d|z
  t        j                  | j                  «      j                   z  }|�l|j                  d«      j                  d	«      }|j                  | j                  ¬
«      }d|z
  t        j                  | j                  «      j                   z  }nd }| j#                  |||«      }| j%                  ||||||¬«      }|d d	 \  }}|d   }|d   }d}|r|d   }|d   }|d	   }|||f}|	r||fnd}|d   }|d   }| j'                  |«      }|
s|||f|z   |z   S t)        ||||	r|nd |	r|nd |rnd |rnd |r¬«      S d ¬«      S )NzDYou cannot specify both input_ids and inputs_embeds at the same timerš   z5You have to specify either input_ids or inputs_embedsz`visual_feats` cannot be `None`z`visual_pos` cannot be `None`©r�   r›   r   rU   )rœ   r…  )r#  r1  r  rË   r   r8   )r.   r,   r-   r/   r0   r1   r2   r3   )ru   rË   r¢  Úuse_return_dictr±   Ú%warn_if_padding_and_no_attention_maskrž   r�   r5   Úonesr£   r    r¡   Útorœ   ÚfinfoÚminrª   r”  r•  r+   )r   r¥   r#  r1  rÊ   r  r¦   r§   rË   r¢  r£  r¨   r�   Úextended_attention_maskÚextended_visual_attention_maskÚembedding_outputÚencoder_outputsrE  rF  r0   r/   Úall_attentionsr1   r2   r3   rÈ   r  r  r.   s                                r   r#   zLxmertModel.forward€  s1  € ð& 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐ  ]Ð%>ÜÐcÓdÐdØÐ"Ø×6Ñ6°yÀ.ÔQØ#Ÿ.™.Ó*‰KØÐ&Ø'×,Ñ,Ó.¨s°Ð3‰KäÐTÓUÐUàÐÜÐ>Ó?Ð?ØÐÜÐ<Ó=Ð=à%.Ð%:�×!Ò!À×@TÑ@TˆàÐ!Ü"ŸZ™Z¨¸FÔCˆNØÐ!Ü"Ÿ[™[¨¼E¿J¹JÈvÔVˆNð #1×":Ñ":¸1Ó"=×"GÑ"GÈÓ"JÐð #:×"<Ñ"<À4Ç:Á:Ð"<Ó"NÐØ#&Ð)@Ñ#@ÄEÇKÁKÐPT×PZÑPZÓD[×D_ÑD_Ñ"_Ðð !Ð,Ø-B×-LÑ-LÈQÓ-O×-YÑ-YÐZ[Ó-\Ð*Ø-K×-NÑ-NÐUY×U_ÑU_Ð-NÓ-`Ð*Ø.1Ð4RÑ.RÔV[×VaÑVaÐbf×blÑblÓVm×VqÑVqÑ-qÑ*à-1Ð*ð  Ÿ?™?¨9°nÀmÓTÐð Ÿ,™,ØØ#Ø%Ø!Ø"@Ø/ð 'ó 
ˆð 8GÀrÈÐ7JÑ4ÐÐ 4Ø5°aÑ8ÐØ!5°aÑ!8ÐàˆÙØ"6°qÑ"9ÐØ 6°qÑ 9ÐØ'6°qÑ'9Ð$à#Ø!Ø(ðˆNñ K_Ð/Ð1EÑFÐdfˆà,¨RÑ0ˆØ,¨RÑ0ˆØŸ™ KÓ0ˆáØ °Ð>ÀÑNÐQ_Ñ_Ð_ä Ø'Ø'Ø'Ù=QÑ#9ÐW[Ù9MÑ!5ÐSWÙ7HÑ 3ÈdÙ3DÑ/È$ÙARÐ%=ô	
ð 		
ð Y]ô	
ð 		
r    )
NNNNNNNNNN)r%   r&   r'   r   rš  r�  r   ÚLXMERT_INPUTS_DOCSTRINGÚformatr   Ú_CHECKPOINT_FOR_DOCr+   Ú_CONFIG_FOR_DOCr   r5   Ú
LongTensorr6   Úboolr   r   r#   r(   r)   s   @r   r’  r’  m  sW  ø„ ô
ò/ò9ñ +Ð+B×+IÑ+IÐJgÓ+hÓiÙØ&Ø%Ø$ôð 15Ø48Ø26Ø6:Ø=AØ59Ø59Ø,0Ø/3Ø&*ñn
à˜E×,Ñ,Ñ-ðn
ð ˜u×0Ñ0Ñ1ðn
ð ˜U×.Ñ.Ñ/ð	n
ð
 ! ×!2Ñ!2Ñ3ðn
ð  (¨×(9Ñ(9Ñ:ðn
ð ! ×!1Ñ!1Ñ2ðn
ð   × 1Ñ 1Ñ2ðn
ð $ D™>ðn
ð ' t™nðn
ð ˜d‘^ðn
ð 
Ð  %¨×(9Ñ(9Ñ":Ð:Ñ	;òn
óó jôn
r    r’  z6Lxmert Model with a specified pretraining head on top.c            $       óÔ  ‡ — e Zd ZdgZˆ fd„Zd„ Z	 d dedee   dede	j                  fˆ fd„Zdefd	„Zd
„ Zd„ Zde	j                  fd„Zd„ Zd„ Z eej+                  d«      «       eee¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 	 d!deej4                     deej6                     deej6                     deej6                     deej6                     deej4                     deej6                     deej4                     deeeeej6                  ej6                  f   f      deej4                     deej>                     dee   dee   dee   de eeej6                     f   fd„«       «       Z!ˆ xZ"S )"ÚLxmertForPreTrainingzcls.predictions.decoder.weightc                 óh  •— t         ‰| �  |«       || _        |j                  | _        |j                  | _        |j
                  | _        |j                  | _        |j                  | _        |j                  | _        t        |«      | _
        t        || j                  j                  j                  j                  «      | _        | j                  rt!        |«      | _        | j                  rt%        || j                  «      | _        | j)                  «        t+        d¬«      t-        d¬«      t-        «       dœ| _        i }|j0                  rd|j2                  ddœ|d<   |j4                  rd|j6                  ddœ|d<   |j8                  rd	|j:                  f|j:                  d
dœ|d<   || _        y )NÚnone)Ú	reduction)Úl2Ú	visual_ceÚcerh  r¾  )ro   r‚   r;   ri  rj  rš   r½  rk  )r   r   ru   Únum_qa_labelsÚvisual_loss_normalizerÚtask_mask_lmÚtask_obj_predictÚtask_matchedÚtask_qar’  r€  rv  rª   r�   rN   Úclsrf  Úobj_predict_headr^  Úanswer_headr–  r
   r	   Ú	loss_fctsrl  rm  rn  ro  rp  r'  rq  )r   ru   rq  r   s      €r   r   zLxmertForPreTraining.__init__þ  s‰  ø€ Ü‰Ñ˜Ô àˆŒØ#×1Ñ1ˆÔØ&,×&CÑ&CˆÔ#ð #×/Ñ/ˆÔØ &× 7Ñ 7ˆÔØ"×/Ñ/ˆÔØ—~‘~ˆŒô " &Ó)ˆŒô *¨&°$·+±+×2HÑ2H×2XÑ2X×2_Ñ2_Ó`ˆŒØ× Ò Ü$7¸Ó$?ˆDÔ!Ø�<Š<Ü5°f¸d×>PÑ>PÓQˆDÔð 	�‰Ôô ¨Ô0Ü)°FÔ;Ü"Ó$ñ
ˆŒð ˆØ×!Ò!àØ×/Ñ/Ø#ñ$ˆM˜%Ñ ð
 ×"Ò"àØ×-Ñ-Ø#ñ%ˆM˜&Ñ!ð
 ×"Ò"à˜f×4Ñ4Ð5Ø×-Ñ-Øñ%ˆM˜&Ñ!ð
 +ˆÕr    c                 óž   — | j                   j                  j                  j                  | j                  j
                  j                  _        y r   )r€  rª   r�   rN   rÆ  rx  rX  r™  s    r   Ú_tie_weightsz!LxmertForPreTraining._tie_weights5  s0   € Ø.2¯k©k×.DÑ.D×.TÑ.T×.[Ñ.[ˆ�‰×Ñ×$Ñ$Õ+r    Únew_num_tokensÚpad_to_multiple_ofÚmean_resizingr¤  c                 ó¼   •— t         ‰| �  |||«      }| j                  | j                  j                  j
                  |«      | j                  j                  _        |S r   )r   Úresize_token_embeddingsÚ_resize_biasrÆ  rx  rQ   )r   rÌ  rÍ  rÎ  rœ  r   s        €r   rÐ  z,LxmertForPreTraining.resize_token_embeddings8  sP   ø€ ô ™Ñ8¸ÐI[Ð]jÓkˆØ$(×$5Ñ$5°d·h±h×6JÑ6J×6OÑ6OÐQ_Ó$`ˆ�‰×ÑÔ!ØÐr    c                 óÚ   — |j                   d   }||k  r|d | }n;t        j                  ||z
  |j                  ¬«      }t        j                  ||g«      }t        j                  |«      }|S )Nr   r¦  )ro   r5   r£   r�   Úcatr   rY  )r   rQ   rÌ  Úold_num_tokensÚnew_biasÚ
extra_biass         r   rÑ  z!LxmertForPreTraining._resize_bias@  sb   € ØŸ™ A™ˆØ˜^Ò+Ø˜O˜^Ð,‰HäŸ™ ^°nÑ%DÈTÏ[É[ÔYˆJÜ—y‘y $¨
Ð!3Ó4ˆHÜ—<‘< Ó)ˆØˆr    c                 ó‚   — | j                  «       }|�|€y| j                  |«      }|| j                  _        || _        |S ©aç  
        Build a resized question answering linear layer Module from a provided new linear layer. Increasing the size
        will add newly initialized weights. Reducing the size will remove weights from the end

        Args:
            num_labels (`int`, *optional*):
                New number of labels in the linear layer weight matrix. Increasing the size will add newly initialized
                weights at the end. Reducing the size will remove weights from the end. If not provided or `None`, just
                returns a pointer to the qa labels ``torch.nn.Linear``` module of the model without doing anything.

        Return:
            `torch.nn.Linear`: Pointer to the resized Linear layer or the old Linear layer
        N©Úget_qa_logit_layerÚ_resize_qa_labelsru   rÀ  ©r   rb  Úcur_qa_logit_layerÚnew_qa_logit_layers       r   Úresize_num_qa_labelsz)LxmertForPreTraining.resize_num_qa_labelsJ  óM   € ð "×4Ñ4Ó6ÐØÐÐ!3Ð!;ØØ!×3Ñ3°JÓ?ÐØ$.ˆ�‰Ô!Ø'ˆÔà!Ð!r    c                 óˆ   — | j                  «       }| j                  ||«      }| j                  |«       | j                  «       S r   ©rÚ  Ú_get_resized_qa_labelsÚ_set_qa_logit_layerrÜ  s       r   rÛ  z&LxmertForPreTraining._resize_qa_labelsb  óC   € Ø!×4Ñ4Ó6ÐØ!×8Ñ8Ð9KÈZÓXÐØ× Ñ Ð!3Ô4Ø×&Ñ&Ó(Ð(r    c                 óN   — t        | d«      r| j                  j                  d   S y)a  
        Returns the linear layer that produces question answering logits.

        Returns:
            `nn.Module`: A torch module mapping the question answering prediction hidden states or `None` if LXMERT
            does not have a visual answering head.
        rÈ  rš   N©ÚhasattrrÈ  ra  r™  s    r   rÚ  z'LxmertForPreTraining.get_qa_logit_layerh  s*   € ô �4˜Ô'Ø×#Ñ#×,Ñ,¨RÑ0Ð0ð (r    c                 ó6   — || j                   j                  d<   y ©Nrš   ©rÈ  ra  ©r   Úqa_logit_layers     r   rä  z(LxmertForPreTraining._set_qa_logit_layers  ó   € Ø(6ˆ×Ñ×!Ñ! "Ò%r    c                 óD  — |€|S |j                   j                  «       \  }}||k(  r|S t        |dd «      �t        j                  ||«      }nt        j                  ||d¬«      }|j                  |j                   j                  «       | j                  |«       t        ||«      }|j                   j                  d |…d d …f   |j                   j                  d |…d d …f<   t        |dd «      �0|j                  j                  d | |j                  j                  d | |S ©NrQ   FrV  ©rN   rž   rj   r   r´   rª  r�   rŒ  r¬  rs   rQ   ©r   rÝ  rb  Úcur_qa_labelsÚ
hidden_dimrÞ  Únum_labels_to_copys          r   rã  z+LxmertForPreTraining._get_resized_qa_labelsv  ó-  € ØÐØ%Ð%à$6×$=Ñ$=×$BÑ$BÓ$DÑ!ˆ�zØ˜JÒ&Ø%Ð%ô Ð% v¨tÓ4Ð@Ü!#§¡¨:°zÓ!BÑä!#§¡¨:°zÈÔ!NÐà×ÑÐ0×7Ñ7×>Ñ>Ô?ð 	×ÑÐ-Ô.ô ! °
Ó;ÐØAS×AZÑAZ×A_ÑA_Ð`sÐasÐ`sÒuvÐ`vÑAwÐ×!Ñ!×&Ñ&Ð':Ð(:Ð':ºAÐ'=Ñ>ÜÐ% v¨tÓ4Ð@Ø@R×@WÑ@W×@\Ñ@\Ð]pÐ^pÐ@qÐ×#Ñ#×(Ñ(Ð)<Ð*<Ð=à!Ð!r    rž  )r¡  r�  r¥   r#  r1  rÊ   r  r¦   r§   ÚlabelsÚ
obj_labelsÚmatched_labelÚansrË   r¢  r£  c                 ó¦  — d|v r+t        j                  dt        «       |j                  d«      }|�|n| j                  j
                  }|�|j                  n|j                  }| j                  ||||||||||¬«
      }|d   |d   |d   }}}| j                  ||«      \  }}| j                  r| j                  |«      }n|d   d   }|€|
€|	€|€dnt        j                  d|¬	«      }|�Z| j                  rN | j                  d
   |j                  d| j                  j                   «      |j                  d«      «      }||z  }|
�F| j"                  r: | j                  d
   |j                  dd«      |
j                  d«      «      }||z  }|	��| j$                  �rt        j                  d|j                  ¬	«      }| j'                  |«      }| j(                  j+                  «       D ]²  \  }}|	|   \  }} |d   }!|d   }"|d   }#| j,                  }$| j                  |"   }%||   }& |%|&j                  d|!«      |j                  |#«      «      }'|'j/                  «       dkD  r|'j1                  d«      }'|'| j                  d«      z  j1                  «       |$z  }'||'z  }Œ´ ||z  }|�P| j                  rD | j                  d
   |j                  d| j2                  «      |j                  d«      «      }(||(z  }|s|||f|dd z   })|�|f|)z   S |)S t5        |||||j6                  |j8                  |j:                  |j<                  |j>                  ¬«	      S )aV  
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the masked language modeling loss. 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]`
        obj_labels (`Dict[Str: Tuple[Torch.FloatTensor, Torch.FloatTensor]]`, *optional*):
            each key is named after each one of the visual losses and each element of the tuple is of the shape
            `(batch_size, num_features)` and `(batch_size, num_features, visual_feature_dim)` for each the label id and
            the label score respectively
        matched_label (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the whether or not the text input matches the image (classification) loss. Input
            should be a sequence pair (see `input_ids` docstring) Indices should be in `[0, 1]`:

            - 0 indicates that the sentence does not match the image,
            - 1 indicates that the sentence does match the image.
        ans (`Torch.Tensor` of shape `(batch_size)`, *optional*):
            a one hot representation hof the correct answer *optional*

        Returns:
        Úmasked_lm_labelszlThe `masked_lm_labels` argument is deprecated and will be removed in a future version, use `labels` instead.N©
r¥   r#  r1  r¦   rÊ   r  r§   r¢  rË   r£  r   r   rU   r‚  r¦  r¿  rš   r‚   r;   ro   r   )	r;   r?   r@   r<   r/   r0   r1   r2   r3   ) ÚwarningsÚwarnÚFutureWarningÚpopru   r§  r�   r€  rÆ  rÅ  rÈ  r5   ÚtensorrÂ  rÉ  r»   r�   rÄ  rÃ  rÇ  rq  ÚitemsrÁ  rÁ   rƒ  rÀ  r>   r/   r0   r1   r2   r3   )*r   r¥   r#  r1  rÊ   r  r¦   r§   r÷  rø  rù  rú  rË   r¢  r£  Úkwargsr�   Úlxmert_outputr  r  r.   Úlang_prediction_scoresr@   Úanswer_scoreÚ
total_lossÚmasked_lm_lossÚmatched_lossÚtotal_visual_lossÚvisual_prediction_scores_dictr¶   Úkey_infoÚlabelÚ	mask_confÚ
output_dimÚloss_fct_nameÚlabel_shaperN   Úvisual_loss_fctÚvisual_prediction_scoresÚvisual_lossÚanswer_lossrç   s*                                             r   r#   zLxmertForPreTraining.forward‘  s»  € ðR  Ñ'Ü�M‰Mðäôð
 —Z‘ZÐ 2Ó3ˆFà%0Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà%.Ð%:�×!Ò!À×@TÑ@TˆØŸ™ØØ%Ø!Ø)Ø)Ø"7Ø'Ø!5Ø/Ø#ð $ó 
ˆð ˜!ÑØ˜!ÑØ˜!Ñð %2�]ˆð
 <@¿8¹8ÀKÐQ^Ó;_Ñ8ÐÐ 8Ø�<Š<Ø×+Ñ+¨MÓ:‰Là(¨Ñ+¨AÑ.ˆLð � =Ð#8¸ZÐ=OÐTWÐT_ñ ä—‘˜c¨&Ô1ð 	ð
 Ð $×"3Ò"3Ø1˜TŸ^™^¨DÑ1Ø&×+Ñ+¨B°·±×0FÑ0FÓGØ—‘˜B“óˆNð ˜.Ñ(ˆJØÐ$¨×):Ò):Ø/˜4Ÿ>™>¨$Ñ/Ð0H×0MÑ0MÈbÐRSÓ0TÐVc×VhÑVhÐikÓVlÓmˆLØ˜,Ñ&ˆJØÑ! d×&;Ó&;Ü %§¡¨S¸×9IÑ9IÔ JÐØ,0×,AÑ,AÀ-Ó,PÐ)Ø!%×!3Ñ!3×!9Ñ!9Ó!;ò 1‘��XØ#-¨c¡?Ñ ��yØ% e™_�
Ø (¨Ñ 0�Ø& wÑ/�Ø×4Ñ4�Ø"&§.¡.°Ñ"?�Ø+HÈÑ+MÐ(Ù-Ø,×1Ñ1°"°jÓAØ—J‘J˜{Ó+ó�ð —?‘?Ó$ qÒ(Ø"-×"2Ñ"2°1Ó"5�KØ*¨Y¯^©^¸BÓ-?Ñ?×EÑEÓGÈ&ÑP�Ø! [Ñ0Ñ!ð1ð  Ð+Ñ+ˆJØˆ?˜tŸ|š|Ø.˜$Ÿ.™.¨Ñ.¨|×/@Ñ/@ÀÀT×EWÑEWÓ/XÐZ]×ZbÑZbÐceÓZfÓgˆKØ˜+Ñ%ˆJáà&Ø(Øðð ˜a˜bÐ!ñ	"ˆFð
 0:Ð/E�Z�M FÑ*ÐQÈ6ÐQä)ØØ4Ø%=Ø%1Ø#0×#GÑ#GØ!.×!CÑ!CØ -× AÑ AØ+×=Ñ=Ø%2×%KÑ%Kô

ð 
	
r    )NT)NNNNNNNNNNNNNN)#r%   r&   r'   Ú_tied_weights_keysr   rË  rm   r   r·  r   rŒ   rÐ  rÑ  rß  rÛ  ÚModulerÚ  rä  rã  r   r²  r³  r   r>   rµ  r5   r¶  r6   r   Ústrr   ÚTensorr   r#   r(   r)   s   @r   r¹  r¹  ÷  s;  ø„ ð
 ;Ð;Ðô5+òn\ð dhñØ!ðØ7?À±}ðØ\`ðà	�‰õð°ó ò"ò0)ð	1 B§I¡Ió 	1ò7ò"ñ6 +Ð+B×+IÑ+IÐJgÓ+hÓiÙÐ+EÐTcÔdð 15Ø48Ø26Ø6:Ø=AØ59Ø59Ø-1ØW[Ø48Ø&*Ø,0Ø/3Ø&*ñB
à˜E×,Ñ,Ñ-ðB
ð ˜u×0Ñ0Ñ1ðB
ð ˜U×.Ñ.Ñ/ð	B
ð
 ! ×!2Ñ!2Ñ3ðB
ð  (¨×(9Ñ(9Ñ:ðB
ð ! ×!1Ñ!1Ñ2ðB
ð   × 1Ñ 1Ñ2ðB
ð ˜×)Ñ)Ñ*ðB
ð ˜T # u¨U×->Ñ->À×@QÑ@QÐ-QÑ'RÐ"RÑSÑTðB
ð   × 0Ñ 0Ñ1ðB
ð �e—l‘lÑ#ðB
ð $ D™>ðB
ð ' t™nðB
ð ˜d‘^ðB
ð" 
Ð)¨5°×1BÑ1BÑ+CÐCÑ	Dò#B
ó eó jôB
r    r¹  zHLxmert Model with a visual-answering head on top for downstream QA tasksc                   óü  ‡ — e Zd Zˆ fd„Zd„ Zd„ Zdej                  fd„Zd„ Z	d„ Z
 eej                  d«      «       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   dee   dee   deeeej*                     f   fd„«       «       Zˆ xZS )ÚLxmertForQuestionAnsweringc                 ó
  •— t         ‰| �  |«       || _        |j                  | _        |j                  | _        t        |«      | _        t        || j                  «      | _        | j                  «        t        «       | _        y r   )r   r   ru   rÀ  rÁ  r’  r€  r^  rÈ  r–  r	   r;   r˜   s     €r   r   z#LxmertForQuestionAnswering.__init__  sn   ø€ Ü‰Ñ˜Ô àˆŒØ#×1Ñ1ˆÔØ&,×&CÑ&CˆÔ#ô " &Ó)ˆŒä1°&¸$×:LÑ:LÓMˆÔð 	�‰Ôô %Ó&ˆ�	r    c                 ó‚   — | j                  «       }|�|€y| j                  |«      }|| j                  _        || _        |S rØ  rÙ  rÜ  s       r   rß  z/LxmertForQuestionAnswering.resize_num_qa_labels0  rà  r    c                 óˆ   — | j                  «       }| j                  ||«      }| j                  |«       | j                  «       S r   râ  rÜ  s       r   rÛ  z,LxmertForQuestionAnswering._resize_qa_labelsH  rå  r    r¤  c                 óN   — t        | d«      r| j                  j                  d   S y)a  
        Returns the linear layer that produces question answering logits

        Returns:
            `nn.Module`: A torch module mapping the question answering prediction hidden states. `None`: A NoneType
            object if Lxmert does not have the visual answering head.
        rÈ  rš   Nrç  r™  s    r   rÚ  z-LxmertForQuestionAnswering.get_qa_logit_layerN  s*   € ô �4˜Ô'Ø×#Ñ#×,Ñ,¨RÑ0Ð0ð (r    c                 ó6   — || j                   j                  d<   y rê  rë  rì  s     r   rä  z.LxmertForQuestionAnswering._set_qa_logit_layerZ  rî  r    c                 óD  — |€|S |j                   j                  «       \  }}||k(  r|S t        |dd «      �t        j                  ||«      }nt        j                  ||d¬«      }|j                  |j                   j                  «       | j                  |«       t        ||«      }|j                   j                  d |…d d …f   |j                   j                  d |…d d …f<   t        |dd «      �0|j                  j                  d | |j                  j                  d | |S rð  rñ  rò  s          r   rã  z1LxmertForQuestionAnswering._get_resized_qa_labels]  rö  r    rž  rŸ  r¥   r#  r1  rÊ   r  r¦   r§   r÷  rË   r¢  r£  c                 óÈ  — |�|n| j                   j                  }| j                  ||||||||
|	|¬«
      }|d   }| j                  |«      }d}|�;| j	                  |j                  d| j                  «      |j                  d«      «      }|s|f|dd z   }|�|f|z   S |S t        |||j                  |j                  |j                  |j                  |j                  ¬«      S )z‰
        labels (`Torch.Tensor` of shape `(batch_size)`, *optional*):
            A one-hot representation of the correct answer
        Nrý  rU   rš   r   )r;   r<   r/   r0   r1   r2   r3   )ru   r§  r€  rÈ  r;   r»   rÀ  r:   r/   r0   r1   r2   r3   )r   r¥   r#  r1  rÊ   r  r¦   r§   r÷  rË   r¢  r£  r  r.   r  r;   rç   s                    r   r#   z"LxmertForQuestionAnswering.forwardx  s  € ð0 &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàŸ™ØØ%Ø!Ø)Ø)Ø"7Ø'Ø!5Ø/Ø#ð $ó 
ˆð & aÑ(ˆØ×'Ñ'¨Ó6ˆØˆØÐØ—9‘9˜\×.Ñ.¨r°4×3EÑ3EÓFÈÏÉÐTVËÓXˆDáØ"�_ }°Q°RÐ'8Ñ8ˆFØ'+Ð'7�D�7˜VÑ#ÐC¸VÐCä/ØØ%1Ø#0×#GÑ#GØ!.×!CÑ!CØ -× AÑ AØ+×=Ñ=Ø%2×%KÑ%Kô
ð 	
r    )NNNNNNNNNNN)r%   r&   r'   r   rß  rÛ  r   r  rÚ  rä  rã  r   r²  r³  r   r´  r:   rµ  r   r5   r¶  r6   r  r·  r   r   r#   r(   r)   s   @r   r  r    sz  ø„ ô
'ò&"ò0)ð
1 B§I¡Ió 
1ò7ò"ñ6 +Ð+B×+IÑ+IÐJgÓ+hÓiÙØ&Ø4Ø$ôð 15Ø48Ø26Ø6:Ø=AØ59Ø59Ø)-Ø,0Ø/3Ø&*ñ3
à˜E×,Ñ,Ñ-ð3
ð ˜u×0Ñ0Ñ1ð3
ð ˜U×.Ñ.Ñ/ð	3
ð
 ! ×!2Ñ!2Ñ3ð3
ð  (¨×(9Ñ(9Ñ:ð3
ð ! ×!1Ñ!1Ñ2ð3
ð   × 1Ñ 1Ñ2ð3
ð ˜Ÿ™Ñ&ð3
ð $ D™>ð3
ð ' t™nð3
ð ˜d‘^ð3
ð 
Ð/°°u×7HÑ7HÑ1IÐIÑ	Jò3
óó jô3
r    r  )r4  r¹  r  r’  r  r%  r  )Ar4   rÃ   r]   rþ  Údataclassesr   Útypingr   r   r   r   r5   r   Útorch.nnr	   r
   Úactivationsr   r   Úmodeling_utilsr   Úutilsr   r   r   r   r   r   Úconfiguration_lxmertr   Ú
get_loggerr%   r[   r´  rµ  r  r   r+   r:   r>   r„   r†   r­   rÙ   rä   rï   rô   rü   r   r  r%  r4  rH  rO  rT  r^  rf  rv  r  ÚLXMERT_START_DOCSTRINGr²  r’  r¹  r  Ú__all__r8   r    r   ú<module>r.     sŠ  ðñ ã Û 	Û Ý !ß /Ó /ã Ý ß 3ç 'Ý -÷÷ õ /ð 
ˆ×	Ñ	˜HÓ	%€à3Ð Ø €ôˆ2�9‰9ô ð ô*H˜ó *Hó ð*HðZ ô$H {ó $Hó ð$HðN ô,H ó ,Hó ð,Hò^Lô^%�r—y‘yô %ôP:�b—i‘iô :ôz˜BŸI™Iô ô §	¡	ô ô˜rŸy™yô ô*	˜Ÿ™ô 	ô�2—9‘9ô ô�"—)‘)ô ô X
�2—9‘9ô X
ôv §¡ô ô6N
�B—I‘Iô N
ôb�2—9‘9ô ô B§I¡Iô ô˜RŸY™Yô ô*,˜RŸY™Yô ,ô˜"Ÿ)™)ô ô>	9˜RŸY™Yô 	9ô%˜Oô %ð<Ð ð,7Ð ñt ØfØóôC
Ð'ó C
ó	ðC
ñL Ø@ØóôZ
Ð0ó Z
ó	ðZ
ñz ØRØóôU
Ð!6ó U
ó	ðU
òp�r    