Ë
    T^(h„\ ã                   óœ  — d Z ddl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mZmZmZ ddlmZ dd	lmZ dd
lmZmZmZmZmZ ddlmZ ddlmZ ddl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+m,Z,m-Z-  e%«       rddl.m/Z/ ddl0m1Z1  e(jd                  e3«      Z4dZ5 G d„ de
jl                  «      Z7	 ddl8m9Z9 e9Z7e4ju                  d«        ej|                  e7«        G d„ de
jl                  «      Z? G d„ de
jl                  «      Z@ G d„ de
jl                  «      ZA G d„ de
jl                  «      ZB G d „ d!e
jl                  «      ZC G d"„ d#e«      ZDd$ZEd%ZF e#d&eE«       G d'„ d(eD«      «       ZG G d)„ d*e
jl                  «      ZH G d+„ d,e
jl                  «      ZI G d-„ d.e
jl                  «      ZJ G d/„ d0e
jl                  «      ZK G d1„ d2e
jl                  «      ZL G d3„ d4e
jl                  «      ZMd5ZNd6ZOd7ZP e#d8eN«       G d9„ d:eD«      «       ZQ e#d;eN«       G d<„ d=eDe«      «       ZRg d>¢ZSy# e;$ r Y �Œae<$ r e4j{                  d«       Y �Œyw xY w)?zPix2Struct modeling fileé    N)ÚDictÚListÚOptionalÚTupleÚUnion)Únné   )ÚACT2FN)ÚCacheÚDynamicCacheÚEncoderDecoderCacheÚStaticCache)ÚGenerationMixin)ÚAttentionMaskConverter)ÚBaseModelOutputÚBaseModelOutputWithPoolingÚ!CausalLMOutputWithCrossAttentionsÚSeq2SeqLMOutputÚSeq2SeqModelOutput)ÚPreTrainedModel)ÚALL_LAYERNORM_LAYERS)	ÚDUMMY_INPUTSÚ
DUMMY_MASKÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚis_torch_flex_attn_availableÚis_torch_fx_proxyÚis_torchdynamo_compilingÚloggingÚreplace_return_docstringsé   )ÚPix2StructConfigÚPix2StructTextConfigÚPix2StructVisionConfig)Ú	BlockMask)Úmake_flex_block_causal_maskr"   c                   ó&   ‡ — e Zd Zdˆ fd„	Zd„ Zˆ xZS )ÚPix2StructLayerNormc                 óŠ   •— t         ‰| �  «        t        j                  t	        j
                  |«      «      | _        || _        y)zc
        Construct a layernorm module in the T5 style. No bias and no subtraction of mean.
        N)ÚsuperÚ__init__r   Ú	ParameterÚtorchÚonesÚweightÚvariance_epsilon)ÚselfÚhidden_sizeÚepsÚ	__class__s      €úp/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/pix2struct/modeling_pix2struct.pyr+   zPix2StructLayerNorm.__init__A   s1   ø€ ô 	‰ÑÔÜ—l‘l¤5§:¡:¨kÓ#:Ó;ˆŒØ #ˆÕó    c                 óž  — |j                  t        j                  «      j                  d«      j	                  dd¬«      }|t        j
                  || j                  z   «      z  }| j                  j                  t        j                  t        j                  fv r%|j                  | j                  j                  «      }| j                  |z  S )Né   éÿÿÿÿT)Úkeepdim)Útor-   Úfloat32ÚpowÚmeanÚrsqrtr0   r/   ÚdtypeÚfloat16Úbfloat16)r1   Úhidden_statesÚvariances      r5   ÚforwardzPix2StructLayerNorm.forwardI   s›   € ð !×#Ñ#¤E§M¡MÓ2×6Ñ6°qÓ9×>Ñ>¸rÈ4Ð>ÓPˆØ%¬¯©°H¸t×?TÑ?TÑ4TÓ(UÑUˆð �;‰;×Ñ¤§¡´·±Ð ?Ñ?Ø)×,Ñ,¨T¯[©[×->Ñ->Ó?ˆMà�{‰{˜]Ñ*Ð*r6   )g�íµ ÷Æ°>©Ú__name__Ú
__module__Ú__qualname__r+   rE   Ú__classcell__©r4   s   @r5   r(   r(   @   s   ø„ õ$ö+r6   r(   )ÚFusedRMSNormzWDiscovered apex.normalization.FusedRMSNorm - will use it instead of Pix2StructLayerNormzJDiscovered apex but it failed to load, falling back to Pix2StructLayerNormc                   ód   ‡ — e Zd ZdZdeddfˆ fd„Zdej                  dej                  fd„Zˆ xZ	S )ÚPix2StructVisionEmbeddingsa-  
    Construct the embeddings from patch. In `Pix2Struct` the input is different from classic Vision-transformer models.
    Here the input is a sequence of `seq_len` flattened patches that also combines padding patches (tokens). Each patch
    is represented by a vector of `hidden_size` values.
    ÚconfigÚreturnNc                 ó„  •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        j                  |j                  |j
                  «      | _	        t        j                  |j                  |j
                  «      | _
        t        j                  |j                  «      | _        y ©N)r*   r+   r   ÚLinearÚpatch_embed_hidden_sizer2   Úpatch_projectionÚ	EmbeddingÚseq_lenÚrow_embedderÚcolumn_embedderÚDropoutÚdropout_rateÚdropout©r1   rO   r4   s     €r5   r+   z#Pix2StructVisionEmbeddings.__init__p   s}   ø€ Ü‰ÑÔÜ "§	¡	¨&×*HÑ*HÈ&×J\ÑJ\Ó ]ˆÔäŸL™L¨¯©¸×9KÑ9KÓLˆÔÜ!Ÿ|™|¨F¯N©N¸F×<NÑ<NÓOˆÔä—z‘z &×"5Ñ"5Ó6ˆ�r6   Úflattened_patchesc                 ó"  — |d d …d d …df   j                  «       }|d d …d d …df   j                  «       }|d d …d d …dd …f   }| j                  |«      }| j                  |«      }| j                  |«      }||z   |z   }| j	                  |«      }|S )Nr   r!   r8   )ÚlongrU   rX   rY   r\   )r1   r^   Úrow_indicesÚcol_indicesÚ
embeddingsÚrow_embeddingsÚcol_embeddingss          r5   rE   z"Pix2StructVisionEmbeddings.forwardy   sŸ   € ð (ªª1¨a¨Ñ0×5Ñ5Ó7ˆØ'ªª1¨a¨Ñ0×5Ñ5Ó7ˆà-ªa²°A±B¨hÑ7Ðà×*Ñ*Ð+<Ó=ˆ
Ø×*Ñ*¨;Ó7ˆØ×-Ñ-¨kÓ:ˆð   .Ñ0°>ÑAˆ
à—\‘\ *Ó-ˆ
àÐr6   )
rG   rH   rI   Ú__doc__r"   r+   r-   ÚTensorrE   rJ   rK   s   @r5   rN   rN   i   s7   ø„ ñð7Ð/ð 7°Dõ 7ð¨¯©ð ¸%¿,¹,÷ r6   rN   c                   ó.   ‡ — e Zd Zˆ fd„Z	 	 	 	 dd„Zˆ xZS )ÚPix2StructVisionAttentionc                 ó|  •— t         ‰| �  «        |j                  | _        |j                  | _        |j
                  | _        |j                  | _        | j                  | j                  z  | _	        t        j                  | j                  | j                  d¬«      | _        t        j                  | j                  | j                  d¬«      | _        t        j                  | j                  | j                  d¬«      | _        t        j                  | j                  | j                  d¬«      | _        d| _        y ©NF©Úbias)r*   r+   r2   Úd_kvÚkey_value_proj_dimÚnum_attention_headsÚn_headsÚattention_dropoutr\   Ú	inner_dimr   rS   ÚqueryÚkeyÚvalueÚoutputÚgradient_checkpointingr]   s     €r5   r+   z"Pix2StructVisionAttention.__init__Ž   sÚ   ø€ Ü‰ÑÔØ!×-Ñ-ˆÔØ"(§+¡+ˆÔØ×1Ñ1ˆŒØ×/Ñ/ˆŒØŸ™¨×(?Ñ(?Ñ?ˆŒô —Y‘Y˜t×/Ñ/°·±ÀeÔLˆŒ
Ü—9‘9˜T×-Ñ-¨t¯~©~ÀEÔJˆŒÜ—Y‘Y˜t×/Ñ/°·±ÀeÔLˆŒ
Ü—i‘i §¡°×0@Ñ0@ÀuÔMˆŒà&+ˆÕ#r6   c                 ó6  ‡ ‡— |j                   dd \  Š}ˆˆ fd„} |‰ j                  |«      «      } |‰ j                  |«      «      }	 |‰ j                  |«      «      }
t	        j
                  ||	j                  dd«      «      }|�€t	        j                  d‰ j                  ||f|j                  |j                  ¬«      }‰ j                  r‰ j                  rd|_        |j                  «       dk(  r*||dd…dddd…f   j                  |j                  «      z   }nw|�||j                  |j                  «      z   }nVt!        «       sLt	        j"                  ‰|f|j                  |j                  ¬«      }||j                  |j                  «      z   }d|z
  }|j%                  |dk(  t	        j&                  |j                  «      j(                  «      }||z  }t	        j*                  |t	        j,                  t	        j&                  |j                  «      j(                  «      «      }t.        j0                  j3                  |dt        j4                  ¬	«      j7                  |«      }t.        j0                  j9                  |‰ j8                  ‰ j                  ¬
«      }|�||z  }t	        j
                  ||
«      }|j                  dd«      j;                  «       j=                  ‰d‰ j>                  «      }‰ jA                  |«      }|f|fz   }|r||fz   }|S )z&
        Self-attention block
        Nr8   c                 ó�   •— | j                  «       j                  ‰d‰j                  ‰j                  «      j	                  dd«      S )Ú
projectionr9   r!   r8   )Ú
contiguousÚviewrq   ro   Ú	transpose)ÚstatesÚ
batch_sizer1   s    €€r5   Úto_projection_shapez>Pix2StructVisionAttention.forward.<locals>.to_projection_shape®   s<   ø€ à×$Ñ$Ó&×+Ñ+¨J¸¸D¿L¹LÈ$×JaÑJaÓb×lÑlÐmnÐpqÓrÐrr6   r	   r!   ©Údevicer@   Tr9   )Údimr@   ©ÚpÚtraining)!Úshapert   ru   rv   r-   Úmatmulr~   Úzerosrq   rƒ   r@   rx   r‡   Úrequires_gradr„   r;   r   r.   Úmasked_fillÚfinfoÚminÚmaxÚtensorr   Ú
functionalÚsoftmaxr<   Útype_asr\   r|   r}   rs   rw   )r1   rC   Úattention_maskÚposition_biasÚlayer_head_maskÚoutput_attentionsÚ
seq_lengthr�   Úquery_statesÚ
key_statesÚvalue_statesÚscoresÚposition_bias_maskedÚattn_weightsÚattn_outputÚoutputsr€   s   `               @r5   rE   z!Pix2StructVisionAttention.forwardž   s»  ù€ ð "/×!4Ñ!4°R°aÐ!8Ñˆ
�Jõ	sñ +¨4¯:©:°mÓ+DÓEˆñ )¨¯©°-Ó)@ÓAˆ
Ù*¨4¯:©:°mÓ+DÓEˆô —‘˜l¨J×,@Ñ,@ÀÀAÓ,FÓGˆàÑ Ü!ŸK™KØ�D—L‘L *¨jÐ9À&Ç-Á-ÐW]×WcÑWcôˆMð ×*Ò*¨t¯}ª}Ø.2�Ô+à×!Ñ!Ó# qÒ(Ø -°ºqÀ$ÈÊaÐ?OÑ0P×0SÑ0SÐTa×ThÑThÓ0iÑ i‘ØÐ+à -°×0AÑ0AÀ-×BVÑBVÓ0WÑ W‘Ü-Ô/Ü!&§¡Ø Ð,°]×5IÑ5IÐQ^×QdÑQdô"�ð !.°×0AÑ0AÀ-×BVÑBVÓ0WÑ W�à Ñ-ˆMà,×8Ñ8¸È!Ñ9KÌUÏ[É[ÐY_×YeÑYeÓMf×MjÑMjÓkÐØÐ&Ñ&ˆÜ—‘˜6¤5§<¡<´·±¸F¿L¹LÓ0I×0MÑ0MÓ#NÓOˆô —}‘}×,Ñ,¨V¸Ä5Ç=Á=Ð,ÓQ×YÑYÐZ`Óaˆô —}‘}×,Ñ,¨\¸T¿\¹\ÐTX×TaÑTaÐ,Óbˆð Ð&Ø'¨/Ñ9ˆLä—l‘l <°Ó>ˆð "×+Ñ+¨A¨qÓ1×<Ñ<Ó>×CÑCÀJÐPRÐTX×TbÑTbÓcˆà—k‘k +Ó.ˆà�. MÐ#3Ñ3ˆáØ  Ñ/ˆGØˆr6   )NNNFrF   rK   s   @r5   ri   ri   �   s   ø„ ô,ð& ØØØ÷Mr6   ri   c                   ó*   ‡ — e Zd Zdefˆ fd„Zd„ Zˆ xZS )ÚPix2StructVisionMlprO   c                 óÀ  •— t         ‰| �  «        t        j                  |j                  |j
                  d¬«      | _        t        j                  |j                  |j
                  d¬«      | _        t        j                  |j
                  |j                  d¬«      | _        t        j                  |j                  «      | _        t        |j                     | _        y rk   ©r*   r+   r   rS   r2   Úd_ffÚwi_0Úwi_1ÚworZ   r[   r\   r
   Údense_act_fnÚactr]   s     €r5   r+   zPix2StructVisionMlp.__init__ð   ó�   ø€ Ü‰ÑÔÜ—I‘I˜f×0Ñ0°&·+±+ÀEÔJˆŒ	Ü—I‘I˜f×0Ñ0°&·+±+ÀEÔJˆŒ	Ü—)‘)˜FŸK™K¨×);Ñ);À%ÔHˆŒÜ—z‘z &×"5Ñ"5Ó6ˆŒÜ˜&×-Ñ-Ñ.ˆ�r6   c                 ó,  — | j                  | j                  |«      «      }| j                  |«      }||z  }| j                  |«      }t	        | j
                  j                  t        j                  «      r�|j                  | j
                  j                  j                  k7  r`| j
                  j                  j                  t        j                  k7  r/|j                  | j
                  j                  j                  «      }| j                  |«      }|S rR   ©rª   r¦   r§   r\   Ú
isinstancer¨   r/   r-   rg   r@   Úint8r;   ©r1   rC   Úhidden_geluÚhidden_linears       r5   rE   zPix2StructVisionMlp.forwardø   ó¿   € Ø—h‘h˜tŸy™y¨Ó7Ó8ˆØŸ	™	 -Ó0ˆØ# mÑ3ˆØŸ™ ]Ó3ˆô �t—w‘w—~‘~¤u§|¡|Ô4Ø×#Ñ# t§w¡w§~¡~×';Ñ';Ò;Ø—‘—‘×$Ñ$¬¯
©
Ò2à)×,Ñ,¨T¯W©W¯^©^×-AÑ-AÓBˆMàŸ™ Ó.ˆØÐr6   )rG   rH   rI   r$   r+   rE   rJ   rK   s   @r5   r¢   r¢   ï   s   ø„ ð/Ð5õ /ör6   r¢   c                   óê   ‡ — e Zd Zdeddfˆ fd„Z	 	 	 d
dej                  deej                     deej                     dede	e
ej                  ej                  f   e
ej                     f   f
d	„Zˆ xZS )ÚPix2StructVisionLayerrO   rP   Nc                 ó*  •— t         ‰| �  «        |j                  | _        d| _        t	        |«      | _        t        |«      | _        t        |j                  |j                  ¬«      | _        t        |j                  |j                  ¬«      | _        y )Nr!   ©r3   )r*   r+   Úchunk_size_feed_forwardÚseq_len_dimri   Ú	attentionr¢   Úmlpr(   r2   Úlayer_norm_epsÚpre_mlp_layer_normÚpre_attention_layer_normr]   s     €r5   r+   zPix2StructVisionLayer.__init__  ss   ø€ Ü‰ÑÔØ'-×'EÑ'EˆÔ$ØˆÔÜ2°6Ó:ˆŒÜ& vÓ.ˆŒÜ"5°f×6HÑ6HÈf×NcÑNcÔ"dˆÔÜ(;¸F×<NÑ<NÐTZ×TiÑTiÔ(jˆÕ%r6   rC   r”   Ú	head_maskr—   c                 óÊ   — |}| j                  |«      }| j                  ||||¬«      }|d   }|dd  }||z   }| j                  |«      }	| j                  |	«      |z   }	|	f|z   }|S )N)r”   r–   r—   r   r!   )r¾   rº   r½   r»   )
r1   rC   r”   r¿   r—   ÚresidualÚself_attention_outputsÚattention_outputr    Úlayer_outputs
             r5   rE   zPix2StructVisionLayer.forward  s–   € ð !ˆð ×5Ñ5°mÓDˆà!%§¡ØØ)Ø%Ø/ð	 "0ó "
Ðð 2°!Ñ4ÐØ(¨¨Ð,ˆð )¨8Ñ3ˆð ×.Ñ.¨}Ó=ˆØ—x‘x Ó-°Ñ=ˆà�/ GÑ+ˆàˆr6   )NNF)rG   rH   rI   r"   r+   r-   rg   r   Úboolr   r   rE   rJ   rK   s   @r5   rµ   rµ     s•   ø„ ðkÐ/ð k°Dõ kð 26Ø,0Ø"'ñà—|‘|ðð ! §¡Ñ.ðð ˜EŸL™LÑ)ð	ð
  ðð 
ˆu�U—\‘\ 5§<¡<Ð/Ñ0°%¸¿¹Ñ2EÐEÑ	F÷r6   rµ   c                   óª   ‡ — e Zd Zdeddfˆ fd„Z	 	 	 	 	 ddej                  deej                     deej                     ded	ed
ede	e
ef   fd„Zˆ xZS )ÚPix2StructVisionEncoderrO   rP   Nc                 óÐ   •— t         ‰| �  «        || _        t        j                  t        |j                  «      D �cg c]  }t        |«      ‘Œ c}«      | _        d| _	        y c c}w )NF)
r*   r+   rO   r   Ú
ModuleListÚrangeÚnum_hidden_layersrµ   Úlayerrx   )r1   rO   Ú_r4   s      €r5   r+   z Pix2StructVisionEncoder.__init__8  sP   ø€ Ü‰ÑÔØˆŒÜ—]‘]Ì5ÐQW×QiÑQiÓKjÖ#kÀaÔ$9¸&Õ$AÒ#kÓlˆŒ
Ø&+ˆÕ#ùò $ls   ½A#rC   r”   r¿   r—   Úoutput_hidden_statesÚreturn_dictc                 óx  — |rdnd }|rdnd }t        | j                  «      D ]j  \  }	}
|r||fz   }|�||	   nd }| j                  r,| j                  r | j	                  |
j
                  ||||«      }n |
||||«      }|d   }|sŒb||d   fz   }Œl |r||fz   }|st        d„ |||fD «       «      S t        |||¬«      S )N© r   r!   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrR   rÑ   ©Ú.0Úvs     r5   ú	<genexpr>z2Pix2StructVisionEncoder.forward.<locals>.<genexpr>d  s   è ø€ Òm˜qÐ_`Ñ_lœÑmùs   ‚Š©Úlast_hidden_staterC   Ú
attentions)Ú	enumeraterÌ   rx   r‡   Ú_gradient_checkpointing_funcÚ__call__Útupler   )r1   rC   r”   r¿   r—   rÎ   rÏ   Úall_hidden_statesÚall_self_attentionsÚiÚlayer_moduler–   Úlayer_outputss                r5   rE   zPix2StructVisionEncoder.forward>  s  € ñ #7™B¸DÐÙ$5™b¸4Ðä(¨¯©Ó4ò 	P‰OˆAˆ|Ù#Ø$5¸Ð8HÑ$HÐ!à.7Ð.C˜i¨šlÈˆOà×*Ò*¨t¯}ª}Ø $× AÑ AØ ×)Ñ)Ø!Ø"Ø#Ø%ó!‘ñ !-¨]¸NÈOÐ]nÓ o�à)¨!Ñ,ˆMâ Ø&9¸]È1Ñ=MÐ<OÑ&OÑ#ð)	Pñ,  Ø 1°]Ð4DÑ DÐáÜÑm ]Ð4EÐGZÐ$[ÔmÓmÐmÜØ+Ø+Ø*ô
ð 	
r6   )NNFFT)rG   rH   rI   r"   r+   r-   rg   r   rÅ   r   rÝ   r   rE   rJ   rK   s   @r5   rÇ   rÇ   7  s‘   ø„ ð,Ð/ð ,°Dõ ,ð 26Ø,0Ø"'Ø%*Ø ñ+
à—|‘|ð+
ð ! §¡Ñ.ð+
ð ˜EŸL™LÑ)ð	+
ð
  ð+
ð #ð+
ð ð+
ð 
ˆu�oÐ%Ñ	&÷+
r6   rÇ   c                   ó8   — e Zd ZdZeZdZdZed„ «       Z	d„ Z
d„ Zy)ÚPix2StructPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    TFc                 óv   — t        j                  t        «      }t        j                  t        «      }|||dœ}|S )N)Údecoder_input_idsÚ	input_idsÚdecoder_attention_mask)r-   r�   r   r   )r1   rç   Ú
input_maskÚdummy_inputss       r5   rê   z&Pix2StructPreTrainedModel.dummy_inputsv  s6   € ä—L‘L¤Ó.ˆ	Ü—\‘\¤*Ó-ˆ
à!*Ø"Ø&0ñ
ˆð
 Ðr6   c                 ó4  — | j                   j                  }t        |t        «      r)|j                  j
                  j                  |dz  «       yt        |t        «      �rVt        | j                   t        «      r | j                   j                  j                  n| j                   j                  }t        | j                   t        «      r | j                   j                  j                  n| j                   j                  }|j                  j                  j
                  j                  d||dz  z  ¬«       t        |j                  d«      rD|j                  j                  �.|j                  j                  j
                  j!                  «        |j"                  j                  j
                  j                  d||dz  z  ¬«       t        |j"                  d«      rD|j"                  j                  �.|j"                  j                  j
                  j!                  «        |j$                  j                  j
                  j                  d||dz  z  ¬«       t        |j$                  d«      rF|j$                  j                  �/|j$                  j                  j
                  j!                  «        yyyt        |t&        «      �rt        | j                   t        «      r | j                   j                  j                  n| j                   j                  }t        | j                   t        «      r | j                   j                  j(                  n| j                   j                  }t        | j                   t        «      r | j                   j                  j*                  n| j                   j*                  }|j,                  j                  j
                  j                  d|||z  dz  z  ¬«       |j.                  j                  j
                  j                  d||dz  z  ¬«       |j0                  j                  j
                  j                  d||dz  z  ¬«       |j2                  j                  j
                  j                  d|||z  dz  z  ¬«       |j4                  r8|j6                  j                  j
                  j                  d||dz  z  ¬«       yyt        |t8        j:                  «      r¼t        | j                   t        «      r | j                   j                  j                  n| j                   j                  }|j                  j
                  j                  d||dz  z  ¬«       |j<                  �2|j                  j
                  |j<                     j!                  «        yyt        |t>        «      rˆt        | j                   t        «      r | j                   j                  j                  n| j                   j                  }|j@                  j                  j
                  j                  d||dz  z  ¬«       yt        |t8        jB                  t8        jD                  f«      rËt8        jF                  jI                  |j                  j
                  jK                  tL        jN                  «      d| j                   jP                  ¬«      jK                  |j                  jR                  «      |j                  _        |j                  �%|j                  j
                  j!                  «        yyt        |t        «      r3|j                  �&|j                  j
                  j                  d«       yyt        |t8        j:                  «      rz|j                  j
                  j                  d| j                   jP                  ¬«       |j<                  �2|j                  j
                  |j<                     j!                  «        yyy)zInitialize the weightsç      ð?ç        g      à¿)r>   Ústdrm   N)*rO   Úinitializer_factorr®   r(   r/   ÚdataÚfill_Ú Pix2StructTextDenseGatedActDenser"   Útext_configr2   r¥   r¦   Únormal_Úhasattrrm   Úzero_r§   r¨   ÚPix2StructTextAttentionrn   Ú	num_headsrt   ru   rv   rw   Úhas_relative_attention_biasÚrelative_attention_biasr   rV   Úpadding_idxÚPix2StructTextModelÚlm_headrS   ÚConv2dÚinitÚtrunc_normal_r;   r-   r<   Úinitializer_ranger@   )r1   ÚmoduleÚfactorr2   r¥   ro   rq   s          r5   Ú_init_weightsz'Pix2StructPreTrainedModel._init_weights�  só  € à—‘×/Ñ/ˆÜ�fÔ1Ô2Ø�M‰M×Ñ×$Ñ$ V¨c¡\Õ2Ü˜Ô @ÕAô ˜dŸk™kÔ+;Ô<ð —‘×'Ñ'×3Ò3à—[‘[×,Ñ,ð ô
 4>¸d¿k¹kÔK[Ô3\�4—;‘;×*Ñ*×/Ò/Ðbf×bmÑbm×brÑbrˆDà�K‰K×Ñ×#Ñ#×+Ñ+°¸&À[ÐUYÑDYÑ:ZÐ+Ô[Ü�v—{‘{ FÔ+°·±×0@Ñ0@Ð0LØ—‘× Ñ ×%Ñ%×+Ñ+Ô-Ø�K‰K×Ñ×#Ñ#×+Ñ+°¸&À[ÐUYÑDYÑ:ZÐ+Ô[Ü�v—{‘{ FÔ+°·±×0@Ñ0@Ð0LØ—‘× Ñ ×%Ñ%×+Ñ+Ô-Ø�I‰I×Ñ×!Ñ!×)Ñ)¨s¸À4ÈDÁ.Ñ8QÐ)ÔRÜ�v—y‘y &Ô)¨f¯i©i¯n©nÐ.HØ—	‘	—‘×#Ñ#×)Ñ)Õ+ð /IÐ)ä˜Ô 7Õ8ô
 ˜dŸk™kÔ+;Ô<ð —‘×'Ñ'×3Ò3à—[‘[×,Ñ,ð ô 1;¸4¿;¹;ÔHXÔ0Y�—‘×'Ñ'×,Ò,Ð_c×_jÑ_j×_vÑ_vð ô
 ˜dŸk™kÔ+;Ô<ð —‘×'Ñ'×1Ò1à—[‘[×*Ñ*ð ð �L‰L×Ñ×$Ñ$×,Ñ,°#¸6ÀkÐTfÑFfÐkoÑEoÑ;pÐ,ÔqØ�J‰J×Ñ×"Ñ"×*Ñ*°¸À;ÐPTÑCTÑ9UÐ*ÔVØ�L‰L×Ñ×$Ñ$×,Ñ,°#¸6À[ÐRVÑEVÑ;WÐ,ÔXØ�M‰M× Ñ ×%Ñ%×-Ñ-°3¸FÀwÐQcÑGcÐhlÑFlÑ<mÐ-ÔnØ×1Ò1Ø×.Ñ.×5Ñ5×:Ñ:×BÑBÈÐQWÐ\gÐlpÑ[pÑQqÐBÕrð 2ä˜¤§¡Ô-ô ˜dŸk™kÔ+;Ô<ð —‘×'Ñ'×3Ò3à—[‘[×,Ñ,ð ð �M‰M×Ñ×&Ñ&¨C°VÀÐPTÑ?TÑ5UÐ&ÔVØ×!Ñ!Ð-Ø—‘×"Ñ" 6×#5Ñ#5Ñ6×<Ñ<Õ>ð .ä˜Ô 3Ô4ô ˜dŸk™kÔ+;Ô<ð —‘×'Ñ'×3Ò3à—[‘[×,Ñ,ð ð �N‰N×!Ñ!×&Ñ&×.Ñ.°C¸VÈÐX\ÑG\Ñ=]Ð.Õ^Ü˜¤§¡¬B¯I©IÐ 6Ô7ô "$§¡×!6Ñ!6Ø—‘×"Ñ"×%Ñ%¤e§m¡mÓ4¸3ÀDÇKÁK×DaÑDað "7ó "ç‰b�—‘×$Ñ$Ó%ð �M‰MÔð �{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ä˜Ô 3Ô4Ø�}‰}Ð(Ø—‘×"Ñ"×(Ñ(¨Õ-ð )ä˜¤§¡Ô-Ø�M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ×!Ñ!Ð-Ø—‘×"Ñ" 6×#5Ñ#5Ñ6×<Ñ<Õ>ð .ð .r6   c                 óÜ  — | j                   j                  }| j                   j                  }|€t        d«      ‚t	        |«      rGt        j                  |j                  d d dz   |«      }t        j                  ||dd d…f   gd¬«      }n>|j                  |j                  «      }|dd d…f   j                  «       |ddd …f<   ||d<   |€t        d«      ‚|j                  |d	k(  |«       |S )
Nzšself.model.config.decoder_start_token_id has to be defined. In Pix2Struct it is usually set to the pad_token_id. See Pix2Struct docs for more information.r9   )r!   .©r„   r!   ).r   z1self.model.config.pad_token_id has to be defined.éœÿÿÿ)rO   Údecoder_start_token_idÚpad_token_idÚ
ValueErrorr   r-   Úfullrˆ   ÚcatÚ	new_zerosÚcloneÚmasked_fill_)r1   rç   r  r	  Úshifted_input_idss        r5   Ú_shift_rightz&Pix2StructPreTrainedModel._shift_rightÑ  sÿ   € Ø!%§¡×!CÑ!CÐØ—{‘{×/Ñ/ˆà!Ð)Üð<óð ô ˜YÔ'ä %§
¡
¨9¯?©?¸3¸BÐ+?À$Ñ+FÐH^Ó _ÐÜ %§	¡	Ð+<¸iÈÈSÈbÈSÈÑ>QÐ*RÐXZÔ [Ñà )× 3Ñ 3°I·O±OÓ DÐØ)2°3¸¸¸°8Ñ)<×)BÑ)BÓ)DÐ˜c 1¡2˜gÑ&Ø(>Ð˜fÑ%àÐÜÐPÓQÐQà×&Ñ&Ð'8¸DÑ'@À,ÔOà Ð r6   N)rG   rH   rI   rf   r"   Úconfig_classÚ_supports_cache_classÚ_supports_static_cacheÚpropertyrê   r  r  rÑ   r6   r5   rä   rä   l  s8   „ ñð
 $€LØ ÐØ"Ðàñó ðòM?ó`!r6   rä   aL  
    This model is 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 ([`Pix2StructConfig`]): 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:
        flattened_patches (`torch.FloatTensor` of shape `(batch_size, sequence_length, num_channels x patch_height x patch_width)`):
            Flattened and padded pixel values. These values can be obtained using [`AutoImageProcessor`]. See
            [`Pix2StructVisionImageProcessor.__call__`] for details. Check the [original
            paper](https://arxiv.org/abs/2210.03347) (figure 5) for more details.

        attention_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding pixel values. Mask values selected in `[0, 1]`:

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

        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.
zjThe bare Pix2StructVision Model transformer outputting raw hidden-states without any specific head on top.c                   ó(  ‡ — e Zd ZeZdZdZdgZdefˆ fd„Z	d„ Z
deeee   f   dd	fd
„Z ee«       eee¬«      	 	 	 	 	 	 ddeej,                     deej,                     deej,                     dee   dee   dee   deeef   fd„«       «       Zˆ xZS )ÚPix2StructVisionModelr^   Trµ   rO   c                 óÞ   •— t         ‰| �  |«       || _        t        |«      | _        t        |«      | _        t        |j                  |j                  ¬«      | _
        | j                  «        y ©Nr·   )r*   r+   rO   rN   rc   rÇ   Úencoderr(   r2   r¼   Ú	layernormÚ	post_initr]   s     €r5   r+   zPix2StructVisionModel.__init__  sU   ø€ Ü‰Ñ˜Ô ØˆŒä4°VÓ<ˆŒÜ.¨vÓ6ˆŒä,¨V×-?Ñ-?ÀV×EZÑEZÔ[ˆŒð 	�‰Õr6   c                 ó.   — | j                   j                  S rR   )rc   rU   ©r1   s    r5   Úget_input_embeddingsz*Pix2StructVisionModel.get_input_embeddings)  s   € Ø�‰×/Ñ/Ð/r6   Úheads_to_prunerP   Nc                 ó˜   — |j                  «       D ]7  \  }}| j                  j                  |   j                  j	                  |«       Œ9 y)z�
        Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
        class PreTrainedModel
        N)Úitemsr  rÌ   rº   Úprune_heads)r1   r   rÌ   Úheadss       r5   Ú_prune_headsz"Pix2StructVisionModel._prune_heads,  sE   € ð
 +×0Ñ0Ó2ò 	C‰LˆE�5Ø�L‰L×Ñ˜uÑ%×/Ñ/×;Ñ;¸EÕBñ	Cr6   ©Úoutput_typer  r”   r¿   r—   rÎ   rÏ   c                 ó(  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|€t	        d«      ‚|€#|j                  d¬«      dk7  j                  «       }| j                  || j                   j                  «      }| j                  |«      }| j                  ||||||¬«      }|d   }	| j                  |	«      }	|s|	f}
|
|dd z   S t        |	|j                  |j                  ¬«      S )	a.  
        Returns:

        Example:

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

        >>> image_processor = AutoProcessor.from_pretrained("google/pix2struct-textcaps-base")
        >>> model = Pix2StructVisionModel.from_pretrained("google/pix2struct-textcaps-base")

        >>> url = "https://www.ilankelman.org/stopsigns/australia.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> inputs = image_processor(images=image, return_tensors="pt")
        >>> with torch.no_grad():
        ...     outputs = model(**inputs)

        >>> last_hidden_states = outputs.last_hidden_state
        >>> list(last_hidden_states.shape)
        [1, 2048, 768]
        ```
        Nz%You have to specify flattened_patchesr9   r  r   )r”   r¿   r—   rÎ   rÏ   r!   r×   )rO   r—   rÎ   Úuse_return_dictr
  ÚsumÚfloatÚget_head_maskrË   rc   r  r  r   rC   rÙ   )r1   r^   r”   r¿   r—   rÎ   rÏ   Úembedding_outputÚencoder_outputsÚsequence_outputÚhead_outputss              r5   rE   zPix2StructVisionModel.forward4  s8  € ðH 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐ$ÜÐDÓEÐEàÐ!à/×3Ñ3¸Ð3Ó;¸qÑ@×GÑGÓIˆNð ×&Ñ& y°$·+±+×2OÑ2OÓPˆ	àŸ?™?Ð+<Ó=ÐàŸ,™,ØØ)ØØ/Ø!5Ø#ð 'ó 
ˆð *¨!Ñ,ˆØŸ.™.¨Ó9ˆáØ+Ð-ˆLØ /°!°"Ð"5Ñ5Ð5äØ-Ø)×7Ñ7Ø&×1Ñ1ô
ð 	
r6   )NNNNNN)rG   rH   rI   r$   r  Úmain_input_nameÚsupports_gradient_checkpointingÚ_no_split_modulesr"   r+   r  r   Úintr   r%  r   Ú"PIX2STRUCT_VISION_INPUTS_DOCSTRINGr    r   Ú_CONFIG_FOR_DOCr   r-   rg   rÅ   r   r   rE   rJ   rK   s   @r5   r  r    s  ø„ ð
 *€LØ)€OØ&*Ð#Ø0Ð1Ðð
Ð/õ 
ò0ðC¨4°°T¸#±Y°Ñ+?ð CÀDó Cñ +Ð+MÓNÙÐ+EÐTcÔdð 59Ø15Ø,0Ø,0Ø/3Ø&*ñK
à# E§L¡LÑ1ðK
ð ! §¡Ñ.ðK
ð ˜EŸL™LÑ)ð	K
ð
 $ D™>ðK
ð ' t™nðK
ð ˜d‘^ðK
ð 
ˆuÐ0Ð0Ñ	1òK
ó eó OôK
r6   r  c                   ó*   ‡ — e Zd Zdefˆ fd„Zd„ Zˆ xZS )rò   rO   c                 óÀ  •— t         ‰| �  «        t        j                  |j                  |j
                  d¬«      | _        t        j                  |j                  |j
                  d¬«      | _        t        j                  |j
                  |j                  d¬«      | _        t        j                  |j                  «      | _        t        |j                     | _        y rk   r¤   r]   s     €r5   r+   z)Pix2StructTextDenseGatedActDense.__init__†  r«   r6   c                 ó,  — | j                  | j                  |«      «      }| j                  |«      }||z  }| j                  |«      }t	        | j
                  j                  t        j                  «      r�|j                  | j
                  j                  j                  k7  r`| j
                  j                  j                  t        j                  k7  r/|j                  | j
                  j                  j                  «      }| j                  |«      }|S rR   r­   r°   s       r5   rE   z(Pix2StructTextDenseGatedActDense.forwardŽ  r³   r6   ©rG   rH   rI   r#   r+   rE   rJ   rK   s   @r5   rò   rò   …  s   ø„ ð/Ð3õ /ör6   rò   c                   ó*   ‡ — e Zd Zdefˆ fd„Zd„ Zˆ xZS )ÚPix2StructTextLayerFFrO   c                 óÖ   •— t         ‰| �  «        t        |«      | _        t	        |j
                  |j                  ¬«      | _        t        j                  |j                  «      | _        y r  )r*   r+   rò   ÚDenseReluDenser(   r2   Úlayer_norm_epsilonÚ
layer_normr   rZ   r[   r\   r]   s     €r5   r+   zPix2StructTextLayerFF.__init__£  sK   ø€ Ü‰ÑÔÜ>¸vÓFˆÔä-¨f×.@Ñ.@Àf×F_ÑF_Ô`ˆŒÜ—z‘z &×"5Ñ"5Ó6ˆ�r6   c                 ór   — | j                  |«      }| j                  |«      }|| j                  |«      z   }|S rR   )r@  r>  r\   )r1   rC   Úforwarded_statess      r5   rE   zPix2StructTextLayerFF.forward«  s=   € ØŸ?™?¨=Ó9ÐØ×.Ñ.Ð/?Ó@ÐØ%¨¯©Ð5EÓ(FÑFˆØÐr6   r:  rK   s   @r5   r<  r<  ¢  s   ø„ ð7Ð3õ 7ör6   r<  c                   óf   ‡ — e Zd Z	 ddedee   fˆ fd„Zedd„«       Zd	d„Z		 	 	 	 	 	 	 	 	 d
d„Z
ˆ xZS )r÷   rO   Ú	layer_idxc                 óÎ  •— t         ‰| �  «        || _        |j                  | _        |j                  | _        |j
                  | _        |j                  | _        |j                  | _	        |j                  | _        | j                  | j                  z  | _        || _        |€-t        j                  d| j                   j"                  › d�«       t%        j&                  | j
                  | j
                  d¬«      | _        t%        j&                  | j
                  | j
                  d¬«      | _        t%        j&                  | j
                  | j
                  d¬«      | _        t%        j&                  | j
                  | j
                  d¬«      | _        | j                  r/t%        j0                  | j                  | j                  «      | _        t5        «       | _        d| _        y )NzInstantiating a decoder z³ without passing `layer_idx` is not recommended and will to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` when creating this class.Frl   )r*   r+   rù   Úrelative_attention_num_bucketsÚrelative_attention_max_distancer2   rn   ro   rø   rq   r[   r\   rs   rD  ÚloggerÚwarning_oncer4   rG   r   rS   rt   ru   rv   rw   rV   rú   ÚsetÚpruned_headsrx   ©r1   rO   rù   rD  r4   s       €r5   r+   z Pix2StructTextAttention.__init__³  ss  ø€ ô 	‰ÑÔØ+FˆÔ(Ø.4×.SÑ.SˆÔ+Ø/5×/UÑ/UˆÔ,Ø!×-Ñ-ˆÔØ"(§+¡+ˆÔØ×'Ñ'ˆŒØ×*Ñ*ˆŒØŸ™¨×(?Ñ(?Ñ?ˆŒØ"ˆŒØÐÜ×ÑØ*¨4¯>©>×+BÑ+BÐ*Cð D,ð ,ôô —Y‘Y˜t×/Ñ/°×1AÑ1AÈÔNˆŒ
Ü—9‘9˜T×-Ñ-¨t×/?Ñ/?ÀeÔLˆŒÜ—Y‘Y˜t×/Ñ/°×1AÑ1AÈÔNˆŒ
Ü—i‘i × 0Ñ 0°$×2BÑ2BÈÔOˆŒà×+Ò+Ü+-¯<©<¸×8[Ñ8[Ð]a×]iÑ]iÓ+jˆDÔ(Ü›EˆÔØ&+ˆÕ#r6   c                 óT  — d}|rC|dz  }|| dkD  j                  t        j                  «      |z  z  }t        j                  | «      } n*t        j                  | t        j
                  | «      «       } |dz  }| |k  }|t        j                  | j                  «       |z  «      t        j                  ||z  «      z  ||z
  z  j                  t        j                  «      z   }t        j                  |t        j                  ||dz
  «      «      }|t        j                  || |«      z  }|S )aÒ  
        Adapted from Mesh Tensorflow:
        https://github.com/tensorflow/mesh/blob/0cb87fe07da627bf0b7e60475d59f95ed6b5be3d/mesh_tensorflow/transformer/transformer_layers.py#L593

        Translate relative position to a bucket number for relative attention. The relative position is defined as
        memory_position - query_position, i.e. the distance in tokens from the attending position to the attended-to
        position. If bidirectional=False, then positive relative positions are invalid. We use smaller buckets for
        small absolute relative_position and larger buckets for larger absolute relative_positions. All relative
        positions >=max_distance map to the same bucket. All relative positions <=-max_distance map to the same bucket.
        This should allow for more graceful generalization to longer sequences than the model has been trained on

        Args:
            relative_position: an int32 Tensor
            bidirectional: a boolean - whether the attention is bidirectional
            num_buckets: an integer
            max_distance: an integer

        Returns:
            a Tensor with the same shape as relative_position, containing int32 values in the range [0, num_buckets)
        r   r8   r!   )r;   r-   r`   ÚabsrŽ   Ú
zeros_likeÚlogr+  ÚmathÚ	full_likeÚwhere)Úrelative_positionÚbidirectionalÚnum_bucketsÚmax_distanceÚrelative_bucketsÚ	max_exactÚis_smallÚrelative_position_if_larges           r5   Ú_relative_position_bucketz1Pix2StructTextAttention._relative_position_bucketÒ  s(  € ð. ÐÙØ˜AÑˆKØÐ!2°QÑ!6× :Ñ :¼5¿:¹:Ó FÈÑ TÑTÐÜ %§	¡	Ð*;Ó <Ñä!&§¡Ð+<¼e×>NÑ>NÐO`Ó>aÓ!bÐ bÐð   1Ñ$ˆ	Ø$ yÑ0ˆð &/Ü�I‰IÐ'×-Ñ-Ó/°)Ñ;Ó<Ü�h‰h�| iÑ/Ó0ñ1à˜YÑ&ñ(÷ ‰"ŒU�Z‰Z‹.ñ	&Ð"ô
 &+§Y¡YØ&¬¯©Ð8RÐT_ÐbcÑTcÓ(dó&
Ð"ð 	œEŸK™K¨Ð2CÐE_Ó`Ñ`ÐØÐr6   c                 óö  — |€ | j                   j                  j                  }|€.t        j                  |t        j
                  |¬«      dd…df   }n|dd…df   j                  |«      }t        j                  |t        j
                  |¬«      ddd…f   }||z
  }| j                  |d| j                  | j                  ¬«      }| j                  |«      }	|	j                  g d¢«      j                  d«      }	|	S )z%Compute binned relative position biasN)r@   rƒ   F)rU  rV  rW  )r8   r   r!   r   )rú   r/   rƒ   r-   Úaranger`   r;   r\  rF  rG  ÚpermuteÚ	unsqueeze)
r1   Úquery_lengthÚ
key_lengthrƒ   Úcache_positionÚcontext_positionÚmemory_positionrT  Úrelative_position_bucketÚvaluess
             r5   Úcompute_biasz$Pix2StructTextAttention.compute_bias  sð   € àˆ>Ø×1Ñ1×8Ñ8×?Ñ?ˆFØÐ!Ü$Ÿ|™|¨LÄÇ
Á
ÐSYÔZÒ[\Ð^bÐ[bÑcÑà-ªa°¨gÑ6×9Ñ9¸&ÓAÐÜŸ,™, z¼¿¹ÈFÔSÐTXÒZ[ÐT[Ñ\ˆØ+Ð.>Ñ>ÐØ#'×#AÑ#AØØØ×;Ñ;Ø×=Ñ=ð	 $Bó $
Ð ð ×-Ñ-Ð.FÓGˆØ—‘¢	Ó*×4Ñ4°QÓ7ˆØˆr6   c                 ó  — |j                   dd \  }}|du}| j                  |«      }|j                  |d| j                  | j                  «      j                  dd«      }|�@|j                  j                  | j                  «      }|r|j                  }n|j                  }|r|n|}|r7|r5r3j                  | j                     }|j                  | j                     }nØ| j                  |«      }| j                  |«      }|j                  |d| j                  | j                  «      j                  dd«      }|j                  |d| j                  | j                  «      j                  dd«      }|�D|s|
nd}
j                  ||| j                  d|
i«      \  }}|rd|j                  | j                  <   t!        j"                  ||j                  dd«      «      }|€×|j                   d   }|�|n|
d   dz   }| j$                  sZt!        j&                  d| j                  ||f|j(                  |j*                  ¬	«      }| j,                  rE| j.                  r9d|_        n1| j3                  |||j(                  |
¬
«      }|dd…dd…| d…dd…f   }|�#|dd…dd…dd…d|j                   d   …f   }||z   }| j4                  rRt!        j6                  |j                   d   «      }d|t9        | j4                  «      <   |dd…|j;                  «       f   }n|}||z  }t<        j>                  jA                  |jC                  «       d¬«      jE                  |«      }t<        j>                  jG                  || jF                  | j.                  ¬«      }|�||z  }t!        j"                  ||«      }|j                  dd«      jI                  «       }|j                  |d| jJ                  «      }| jM                  |«      }|||f}|	r||fz   }|S )z€
        Self-attention (if key_value_states is None) or attention over source sentence (provided by key_value_states).
        Nr8   r9   r!   rc  Tr	   éþÿÿÿr‚   )rƒ   rc  r   r  r…   )'rˆ   rt   r}   rq   ro   r~   Ú
is_updatedÚgetrD  Úcross_attention_cacheÚself_attention_cacheÚ	key_cacheÚvalue_cacheru   rv   Úupdater-   r‰   rù   rŠ   rƒ   r@   rx   r‡   r‹   rh  rK  r.   ÚlistrÅ   r   r‘   r’   r+  r“   r\   r|   rs   rw   )r1   rC   ÚmaskÚkey_value_statesr•   Úpast_key_valuer–   ra  Ú	use_cacher—   rc  r€   r˜   Úis_cross_attentionr™   rk  Úcurr_past_key_valueÚcurrent_statesrš   r›   rœ   rb  Úreal_seq_lengthÚcausal_maskr�   rž   rŸ   r    s                               r5   rE   zPix2StructTextAttention.forward  sà  € ð$ "/×!4Ñ!4°R°aÐ!8Ñˆ
�Jð .°TÐ9Ðà—z‘z -Ó0ˆØ#×(Ñ(¨°R¸¿¹Àt×G^ÑG^Ó_×iÑiÐjkÐmnÓoˆàÐ%Ø'×2Ñ2×6Ñ6°t·~±~ÓFˆJÙ!à&4×&JÑ&JÑ#à&4×&IÑ&IÐ#á-?Ñ)À]ˆÙ¡.±Zà,×6Ñ6°t·~±~ÑFˆJØ.×:Ñ:¸4¿>¹>ÑJ‰LàŸ™ .Ó1ˆJØŸ:™: nÓ5ˆLØ#Ÿ™¨°R¸¿¹Àt×G^ÑG^Ó_×iÑiÐjkÐmnÓoˆJØ'×,Ñ,¨Z¸¸T¿\¹\È4×KbÑKbÓc×mÑmÐnoÐqrÓsˆLàÐ)á7I¡Èt�Ø+>×+EÑ+EØ ¨d¯n©nÐ?OÐQ_Ð>`ó,Ñ(�
˜Lñ &Ø@D�N×-Ñ-¨d¯n©nÑ=ô —‘˜l¨J×,@Ñ,@ÀÀAÓ,FÓGˆàÐ Ø#×)Ñ)¨"Ñ-ˆJà.:Ð.F™lÈNÐ[]ÑL^ÐabÑLbˆOØ×3Ò3Ü %§¡Ø˜Ÿ™ j°*Ð=ÀfÇmÁmÐ[a×[gÑ[gô!�ð ×.Ò.°4·=²=Ø26�MÕ/à $× 1Ñ 1Ø# Z¸¿¹ÐVdð !2ó !�ð !.ªa²°Z°K±LÂ!Ð.CÑ D�àÐØ"¢1¢aªÐ,B¨j×.>Ñ.>¸rÑ.BÐ,BÐ#BÑC�Ø -°Ñ ;�à×ÒÜ—:‘:˜m×1Ñ1°!Ñ4Ó5ˆDØ,-ˆD”�d×'Ñ'Ó(Ñ)Ø#0²°D·I±I³K°Ñ#@Ñ à#0Ð àÐ&Ñ&ˆô —}‘}×,Ñ,¨V¯\©\«^ÀÐ,ÓD×LÑLÈVÓTˆÜ—}‘}×,Ñ,¨\¸T¿\¹\ÐTX×TaÑTaÐ,Óbˆð Ð&Ø'¨/Ñ9ˆLä—l‘l <°Ó>ˆà!×+Ñ+¨A¨qÓ1×<Ñ<Ó>ˆØ!×&Ñ& z°2°t·~±~ÓFˆØ—k‘k +Ó.ˆà °Ð>ˆáØ  Ñ/ˆGØˆr6   ©FN)Té    é€   )NN)	NNNNNNFFN)rG   rH   rI   r#   r   r4  r+   Ústaticmethodr\  rh  rE   rJ   rK   s   @r5   r÷   r÷   ²  s^   ø„ àjnñ,Ø*ð,ØZbÐcfÑZgõ,ð> ò- ó ð- ó`ð0 ØØØØØØØØ÷ir6   r÷   c                   óB   ‡ — e Zd Zddee   fˆ fd„Z	 	 	 	 	 	 	 dd„Zˆ xZS )Ú Pix2StructTextLayerSelfAttentionrD  c                 óÜ   •— t         ‰| �  «        t        |||¬«      | _        t	        |j
                  |j                  ¬«      | _        t        j                  |j                  «      | _        y )N©rù   rD  r·   ©r*   r+   r÷   rº   r(   r2   r?  r@  r   rZ   r[   r\   rL  s       €r5   r+   z)Pix2StructTextLayerSelfAttention.__init__‡  sU   ø€ Ü‰ÑÔÜ0ØÐ0KÐW`ô
ˆŒô .¨f×.@Ñ.@Àf×F_ÑF_Ô`ˆŒÜ—z‘z &×"5Ñ"5Ó6ˆ�r6   c	           
      óš   — | j                  |«      }	| j                  |	|||||||¬«      }
|| j                  |
d   «      z   }|f|
dd  z   }|S )N)rs  r•   r–   ru  rv  r—   rc  r   r!   ©r@  rº   r\   )r1   rC   r”   r•   r–   ru  rv  r—   rc  Únormed_hidden_statesrÃ   r    s               r5   rE   z(Pix2StructTextLayerSelfAttention.forward�  st   € ð  $Ÿ™¨}Ó=ÐØŸ>™>Ø ØØ'Ø+Ø)ØØ/Ø)ð *ó 	
Ðð &¨¯©Ð5EÀaÑ5HÓ(IÑIˆØ Ð"Ð%5°a°bÐ%9Ñ9ˆØˆr6   r|  )NNNNFFN©rG   rH   rI   r   r4  r+   rE   rJ   rK   s   @r5   r�  r�  †  s0   ø„ ñ7ÈXÐVYÉ]õ 7ð ØØØØØØ÷r6   r�  c                   óD   ‡ — e Zd Zddee   fˆ fd„Z	 	 	 	 	 	 	 	 dd„Zˆ xZS )Ú!Pix2StructTextLayerCrossAttentionrD  c                 óÜ   •— t         ‰| �  «        t        |d|¬«      | _        t	        |j
                  |j                  ¬«      | _        t        j                  |j                  «      | _        y )NFrƒ  r·   r„  )r1   rO   rD  r4   s      €r5   r+   z*Pix2StructTextLayerCrossAttention.__init__¬  sP   ø€ Ü‰ÑÔÜ0°ÐUZÐfoÔpˆŒÜ-¨f×.@Ñ.@Àf×F_ÑF_Ô`ˆŒÜ—z‘z &×"5Ñ"5Ó6ˆ�r6   c                 óž   — | j                  |«      }| j                  |||||||||	|
¬«
      }|| j                  |d   «      z   }|f|dd  z   }|S )N)	rs  rt  r•   r–   ru  rv  ra  r—   rc  r   r!   r†  )r1   rC   rt  r”   r•   r–   ru  rv  ra  r—   rc  r‡  rÃ   rÄ   r    s                  r5   rE   z)Pix2StructTextLayerCrossAttention.forward²  sy   € ð  $Ÿ™¨}Ó=ÐØŸ>™>Ø ØØ-Ø'Ø+Ø)ØØ%Ø/Ø)ð *ó 
Ðð % t§|¡|Ð4DÀQÑ4GÓ'HÑHˆØ�/Ð$4°Q°RÐ$8Ñ8ˆØˆr6   rR   )NNNNFNFNrˆ  rK   s   @r5   rŠ  rŠ  «  s2   ø„ ñ7¨(°3©-õ 7ð ØØØØØØØ÷r6   rŠ  c                   óL   ‡ — e Zd Zddee   fˆ fd„Z	 	 	 	 	 	 	 	 	 	 	 	 dd„Zˆ xZS )ÚPix2StructTextBlockrD  c                 óŒ   •— t         ‰| �  «        t        |||¬«      | _        t	        ||¬«      | _        t        |«      | _        y )Nrƒ  )rD  )r*   r+   r�  Úself_attentionrŠ  Úencoder_decoder_attentionr<  r»   rL  s       €r5   r+   zPix2StructTextBlock.__init__Ò  sH   ø€ Ü‰ÑÔä>ØØ(CØô
ˆÔô *KØØô*
ˆÔ&ô
 )¨Ó0ˆ�r6   c                 ó(  — | j                  |||||	|
||¬«      }|d d \  }}	|dd  }|j                  t        j                  k(  rht        j                  |«      j                  «       rEt        j                  |j                  «      j                  dz
  }t        j                  || |¬«      }|d u}|rµ| j                  ||||||	|d   dz   |
|¬«	      }|d d \  }}	|j                  t        j                  k(  rht        j                  |«      j                  «       rEt        j                  |j                  «      j                  dz
  }t        j                  || |¬«      }||dd  z   }| j                  |«      }|j                  t        j                  k(  rht        j                  |«      j                  «       rEt        j                  |j                  «      j                  dz
  }t        j                  || |¬«      }|f}|
r||	fz   |z   }|S ||z   }|S )N)r”   r•   r–   ru  rv  r—   rc  r8   iè  )rŽ   r�   r9   r!   )rt  r”   r•   r–   ru  ra  rv  r—   )r�  r@   r-   rA   ÚisinfÚanyr�   r�   Úclampr‘  r»   )r1   rC   r”   r•   Úencoder_hidden_statesÚencoder_attention_maskÚencoder_decoder_position_biasr–   Úcross_attn_layer_head_maskru  rv  r—   rÏ   rc  rÂ   Úattention_outputsÚclamp_valueÚdo_cross_attentionÚcross_attention_outputsr    s                       r5   rE   zPix2StructTextBlock.forwardâ  s  € ð  "&×!4Ñ!4ØØ)Ø'Ø+Ø)ØØ/Ø)ð "5ó 	"
Ðð )?¸rÀÐ(BÑ%ˆ�~Ø2°1°2Ð6Ðð ×Ñ¤%§-¡-Ò/´E·K±KÀÓ4N×4RÑ4RÔ4TÜŸ+™+ m×&9Ñ&9Ó:×>Ñ>ÀÑEˆKÜ!ŸK™K¨¸K¸<È[ÔYˆMà2¸$Ð>ÐÙØ&*×&DÑ&DØØ!6Ø5Ø;Ø :Ø-Ø+¨BÑ/°!Ñ3Ø#Ø"3ð 'Eó 
'Ð#ð -DÀBÀQÐ,GÑ)ˆM˜>ð ×"Ñ"¤e§m¡mÒ3¼¿¹ÀMÓ8R×8VÑ8VÔ8XÜ#Ÿk™k¨-×*=Ñ*=Ó>×BÑBÀTÑI�Ü %§¡¨MÀ¸|ÐQ\Ô ]�ð !2Ð4KÈAÈBÐ4OÑ OÐð Ÿ™ Ó/ˆð ×Ñ¤%§-¡-Ò/´E·K±KÀÓ4N×4RÑ4RÔ4TÜŸ+™+ m×&9Ñ&9Ó:×>Ñ>ÀÑEˆKÜ!ŸK™K¨¸K¸<È[ÔYˆMà Ð"ˆáØ Ð 1Ñ1Ð4EÑEˆGð ˆð Ð 1Ñ1ˆGàˆr6   r|  )NNNNNNNNFFTNrˆ  rK   s   @r5   rŽ  rŽ  Ñ  s@   ø„ ñ1ÈXÐVYÉ]õ 1ð& ØØ"Ø#Ø&*ØØ#'ØØØØØ÷Hr6   rŽ  aü  

    The Pix2Struct model was proposed in [Pix2Struct: Screenshot Parsing as Pretraining for Visual Language
    Understanding](https://arxiv.org/abs/2210.03347) by Kenton Lee, Mandar Joshi, Iulia Turc, Hexiang Hu, Fangyu Liu,
    Julian Eisenschlos, Urvashi Khandelwal, Peter Shaw, Ming-Wei Chang, Kristina Toutanova. It's an encoder decoder
    transformer pre-trained in a image-to-text setting.

    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 (Union[`Pix2StructConfig`, `Pix2StructTextConfig`]):
            Model configuration class with all the parameters of the model. Initializing with a config file does not
            load the weights associated with the model, only the configuration. Check out the
            [`~PreTrainedModel.from_pretrained`] method to load the model weights.
aÁ  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. Pix2StructText is a model with relative position
            embeddings so you should be able to pad the inputs on both the right and the left.

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

            [What are input IDs?](../glossary#input-ids)

            To know more on how to prepare `input_ids` for pretraining take a look a [Pix2StructText
            Training](./t5#training).
        attention_mask (`torch.FloatTensor` 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)
        decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
            Indices of decoder input sequence tokens in the vocabulary.

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

            [What are decoder input IDs?](../glossary#decoder-input-ids)

            Pix2StructText uses the `pad_token_id` as the starting token for `decoder_input_ids` generation. If
            `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
            `past_key_values`).

            To know more on how to prepare `decoder_input_ids` for pretraining take a look at [Pix2StructText
            Training](./t5#training).
        decoder_attention_mask (`torch.BoolTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
            Default behavior: generate a tensor that ignores pad tokens in `decoder_input_ids`. Causal mask will also
            be used by default.
        head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules in the encoder. Mask values selected in `[0,
            1]`:

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

        decoder_head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules in the decoder. 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 `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
                Mask to nullify selected heads of the cross-attention modules in the decoder. Mask values selected in
                `[0, 1]`:

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

        encoder_outputs (`tuple(tuple(torch.FloatTensor)`, *optional*):
            Tuple consists of (`last_hidden_state`, `optional`: *hidden_states*, `optional`: *attentions*)
            `last_hidden_state` of shape `(batch_size, sequence_length, hidden_size)` is a sequence of hidden states at
            the output of the last layer of the encoder. Used in the cross-attention of the decoder.
        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 layers. 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.
        decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, target_sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `decoder_input_ids` you can choose to directly pass an embedded
            representation. If `past_key_values` is used, optionally only the last `decoder_inputs_embeds` have to be
            input (see `past_key_values`). This is useful if you want more control over how to convert
            `decoder_input_ids` indices into associated vectors than the model's internal embedding lookup matrix.

            If `decoder_input_ids` and `decoder_inputs_embeds` are both unset, `decoder_inputs_embeds` takes the value
            of `inputs_embeds`.

        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.
        cache_position (`torch.LongTensor` of shape `(sequence_length)`, *optional*):
            Indices depicting the position of the input sequence tokens in the sequence. It is used to update the
            cache in the correct position and to infer the complete sequence length.
aœ  
    Args:
        flattened_patches (`torch.FloatTensor` of shape `(batch_size, seq_length, hidden_size)`):
            Flattened pixel patches. the `hidden_size` is obtained by the following formula: `hidden_size` =
            `num_channels` * `patch_size` * `patch_size`

            The process of flattening the pixel patches is done by `Pix2StructProcessor`.

        attention_mask (`torch.FloatTensor` 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)
        decoder_input_ids (`torch.LongTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
            Indices of decoder input sequence tokens in the vocabulary.

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

            [What are decoder input IDs?](../glossary#decoder-input-ids)

            Pix2StructText uses the `pad_token_id` as the starting token for `decoder_input_ids` generation. If
            `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
            `past_key_values`).

            To know more on how to prepare `decoder_input_ids` for pretraining take a look at [Pix2StructText
            Training](./t5#training).
        decoder_attention_mask (`torch.BoolTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
            Default behavior: generate a tensor that ignores pad tokens in `decoder_input_ids`. Causal mask will also
            be used by default.
        head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules in the encoder. Mask values selected in `[0,
            1]`:

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

        decoder_head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules in the decoder. 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 `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
                Mask to nullify selected heads of the cross-attention modules in the decoder. Mask values selected in
                `[0, 1]`:

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

        encoder_outputs (`tuple(tuple(torch.FloatTensor)`, *optional*):
            Tuple consists of (`last_hidden_state`, `optional`: *hidden_states*, `optional`: *attentions*)
            `last_hidden_state` of shape `(batch_size, sequence_length, hidden_size)` is a sequence of hidden states at
            the output of the last layer of the encoder. Used in the cross-attention of the decoder.
        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 layers. 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)`.
        decoder_inputs_embeds (`torch.FloatTensor` of shape `(batch_size, target_sequence_length, hidden_size)`, *optional*):
            Optionally, instead of passing `decoder_input_ids` you can choose to directly pass an embedded
            representation. If `past_key_values` is used, optionally only the last `decoder_inputs_embeds` have to be
            input (see `past_key_values`). This is useful if you want more control over how to convert
            `decoder_input_ids` indices into associated vectors than the model's internal embedding lookup matrix.

            If `decoder_input_ids` and `decoder_inputs_embeds` are both unset, `decoder_inputs_embeds` takes the value
            of `inputs_embeds`.
        labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Labels for computing the masked language modeling loss for the decoder.

        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.
z)The standalone text decoder of Pix2Structc            $       ó  ‡ — e Zd ZeZdgZdgZdZˆ fd„Zd„ Z	d„ Z
d„ Zd„ Zd	„ Z ee«       eee¬
«      	 	 	 	 	 	 	 	 	 	 	 	 	 	 d$deej*                     deej,                     deej,                     deej,                     deej*                     deej,                     deej.                     deeeej,                           dee   dee   dee   deej*                     dee   deej*                     deeej,                  df   ef   fd„«       «       Z	 d%dej.                  dej.                  dej.                  dedef
d„Zedej.                  deded ej@                  d!ejB                  dej.                  d"efd#„«       Z"ˆ xZ#S )&rü   rŽ  zlm_head.weightTc                 óV  •— t         ‰| �  |«       t        j                  |j                  |j
                  «      | _        t        j                  t        |j                  «      D �cg c]  }t        |t        |dk(  «      |¬«      ‘Œ c}«      | _        t        |j
                  |j                  ¬«      | _        t        j                   |j"                  «      | _        t        j&                  |j
                  |j                  d¬«      | _        | j+                  «        d| _        y c c}w )Nr   rƒ  r·   Frl   )r*   r+   r   rV   Ú
vocab_sizer2   Úembed_tokensrÉ   rÊ   Ú
num_layersrŽ  rÅ   rÌ   r(   r?  Úfinal_layer_normrZ   r[   r\   rS   rý   r  rx   )r1   rO   rà   r4   s      €r5   r+   zPix2StructTextModel.__init__  sá   ø€ Ü‰Ñ˜Ô ÜŸL™L¨×):Ñ):¸F×<NÑ<NÓOˆÔä—]‘]ô ˜v×0Ñ0Ó1öàô $ FÌÈQÐRSÉVËÐ`aÖbòó
ˆŒ
ô !4°F×4FÑ4FÈF×LeÑLeÔ fˆÔÜ—z‘z &×"5Ñ"5Ó6ˆŒä—y‘y ×!3Ñ!3°V×5FÑ5FÈUÔSˆŒð 	�‰ÔØ&+ˆÕ#ùòs   Á&!D&c           	      óÒ  — |€t         j                  d«       |S d}|D ]Æ  }d}|D ]1  }||j                  d|j                  |j                  «      «      fz   }Œ3 |d   j
                  |d   j
                  k7  r,t        d|d   j
                  › d|d   j
                  › d�«      ‚t        |«      t        |«      k7  r$t        dt        |«      › dt        |«      › d�«      ‚||fz   }ŒÈ |S )	NzHYou might want to consider setting `use_cache=True` to speed up decodingrÑ   r   z%reordered_layer_past_states[0] shape z  and layer_past_states[0] shape z mismatchedz&length of reordered_layer_past_states z! and length of layer_past_states )rH  ÚwarningÚindex_selectr;   rƒ   rˆ   r
  Úlen)r1   Úpast_key_valuesÚbeam_idxÚreordered_decoder_pastÚlayer_past_statesÚreordered_layer_past_statesÚlayer_past_states          r5   Ú_reorder_cachez"Pix2StructTextModel._reorder_cache  su  € ð Ð"Ü�N‰NÐeÔfØ"Ð"à!#ÐØ!0ò 	]Ðð +-Ð'Ø$5ò Ð à.IØ$×1Ñ1°!°X·[±[ÐAQ×AXÑAXÓ5YÓZðMñ /Ñ+ðð +¨1Ñ-×3Ñ3Ð7HÈÑ7K×7QÑ7QÒQÜ Ø;Ð<WÐXYÑ<Z×<`Ñ<`Ð;að  bBð  CTð  UVñ  CW÷  C]ñ  C]ð  B^ð  ^ið  jóð ô Ð.Ó/´3Ð7HÓ3IÒIÜ Ø<¼SÐA\Ó=]Ð<^Ð^ô  ADð  EVó  AWð  @Xð  Xcð  dóð ð &<Ð?ZÐ>\Ñ%\Ñ"ð'	]ð( &Ð%r6   c                 ó   — | j                   S rR   ©r¡  r  s    r5   r  z(Pix2StructTextModel.get_input_embeddings:  s   € Ø× Ñ Ð r6   c                 ó   — || _         y rR   r°  ©r1   Únew_embeddingss     r5   Úset_input_embeddingsz(Pix2StructTextModel.set_input_embeddings=  s
   € Ø*ˆÕr6   c                 ó   — | j                   S rR   ©rý   r  s    r5   Úget_output_embeddingsz)Pix2StructTextModel.get_output_embeddings@  ó   € Ø�|‰|Ðr6   c                 ó   — || _         y rR   r¶  r²  s     r5   Úset_output_embeddingsz)Pix2StructTextModel.set_output_embeddingsC  s	   € Ø%ˆ�r6   r&  rç   r”   r–  r—  Úinputs_embedsr¿   Úcross_attn_head_maskr¨  rv  r—   rÎ   ÚlabelsrÏ   rc  rP   .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«      ‚|€$| j                  €J d«       ‚| j                  |«      }|\  }}d}d}|	s|�’t        |t        «      r't        |t        «      sd}t        |t        «       «      }n[t        |t        «      s-d}t        j                  d«       t        j                  |«      }n|€t        t        «       t        «       «      }d	}|�|d	   }n|�|j!                  «       }|€%t#        j$                  |||z   |j&                  ¬
«      }|€9|�|j!                  «       |z   n|}t#        j(                  |||j&                  ¬
«      }| j                   j*                  r$| j-                  ||||�|j.                  nd|
«      }nX|dd…dddd…f   }|j1                  |j2                  ¬«      }d|z
  t#        j4                  |j2                  «      j6                  z  }|�M|j                  «       \  }}}||f}|€!t#        j(                  ||j&                  ¬
«      }| j9                  |«      }nd}| j;                  || j                   j<                  «      }| j;                  || j                   j<                  «      }|rdnd}|
rdnd}|
rdnd}d} d}!| j?                  |«      }"tA        | jB                  «      D ]Æ  \  }#}$||#   }%||#   }&|r||"fz   }| jD                  rM| jF                  rA|	rt        jI                  d«       d}	| jK                  |$jL                  |"|| |||!|%|&d|	|
|«      }'n |$|"|| |||!|%|&||	|
|¬«      }'|	du r|'dd dz   |'dd z   }'|'dd \  }"}(|'d   } |�	|'|
rdnd   }!|
sŒ²||'d   fz   }|€Œ¾||'d   fz   }ŒÈ | jO                  |"«      }"| j?                  |"«      }"| jQ                  |"«      })|r||"fz   }d}*|�…|j1                  |)j&                  «      }tS        jT                  dd¬«      }+ |+|)jW                  «       j                  d|)j                  d«      «      |jW                  «       j                  d«      «      }*|	r(nd},|r|j.                  },|r|jY                  «       },|st[        d„ |*|)|,|||fD «       «      S t]        |*|)|,|||¬«      S )aá  
        Returns:

        Example:

        ```python
        >>> from transformers import AutoProcessor, Pix2StructTextModel

        >>> processor = AutoProcessor.from_pretrained("google/pix2struct-textcaps-base")
        >>> model = Pix2StructTextModel.from_pretrained("google/pix2struct-textcaps-base")

        >>> inputs = processor(text="Hello, my dog is cute", return_tensors="pt")
        >>> outputs = model(**inputs)
        >>> loss = outputs.loss
        ```
        NzTYou cannot specify both decoder_input_ids and decoder_inputs_embeds at the same timer9   zEYou have to specify either decoder_input_ids or decoder_inputs_embedsz<You have to initialize the model with valid token embeddingsFTzìPassing a tuple of `past_key_values` is deprecated and will be removed in Transformers v4.48.0. You should pass an instance of `EncoderDecoderCache` instead, e.g. `past_key_values=EncoderDecoderCache.from_legacy_cache(past_key_values)`.r   ©rƒ   )r@   rì   rÑ   zZ`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...)r”   r•   r–  r—  r˜  r–   r™  ru  rv  r—   rc  r!   rR   r8   é   r	   é   r  r>   )Úignore_indexÚ	reductionc              3   ó$   K  — | ]  }|�|–— Œ
 y ­wrR   rÑ   rÓ   s     r5   rÖ   z.Pix2StructTextModel.forward.<locals>.<genexpr>  s   è ø€ ò àð �=ô ñùs   ‚)ÚlossÚlogitsr¨  rC   rÙ   Úcross_attentions)/rO   rv  r—   rÎ   r)  r
  Úsizer}   r¡  r®   r   r   r   rH  rI  Úfrom_legacy_cacheÚget_seq_lengthr-   r^  rƒ   r.   Ú
is_decoderÚ_update_causal_maskrn  r;   r@   r�   rŽ   Úinvert_attention_maskr,  r¢  r\   rÚ   rÌ   rx   r‡   r¥  rÛ   rE   r£  rý   r   ÚCrossEntropyLossr|   Úto_legacy_cacherÝ   r   )-r1   rç   r”   r–  r—  r»  r¿   r¼  r¨  rv  r—   rÎ   r½  rÏ   rc  ÚkwargsÚinput_shaper€   r˜   Úreturn_legacy_cacheÚreturn_self_attention_cacheÚpast_key_values_lengthÚmask_seq_lengthr{  Úencoder_batch_sizeÚencoder_sequence_lengthrÍ   Úencoder_hidden_shapeÚencoder_extended_attention_maskrÞ   Úall_attentionsÚall_cross_attentionsr•   r˜  rC   rà   rá   r–   r™  râ   Únext_decoder_cacherÆ  rÅ  Úloss_fctÚ
next_caches-                                                r5   rE   zPix2StructTextModel.forwardF  s  € ðH "+Ð!6‘I¸D¿K¹K×<QÑ<Qˆ	Ø1BÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐ  ]Ð%>ÜÐsÓtÐtØÐ"Ø#Ÿ.™.Ó*ˆKØ!Ÿ™ r¨;°r©?Ó;‰IØÐ&Ø'×,Ñ,Ó.¨s°Ð3‰KäÐdÓeÐeàÐ Ø×$Ñ$Ð0ÐpÐ2pÓpÐ0Ø ×-Ñ-¨iÓ8ˆMà!,Ñˆ
�Jð $ÐØ&+Ð#Ù˜Ð3Ü˜/¬5Ô1¼*À_ÔViÔ:jØ.2Ð+Ü"5°oÄ|Ã~Ó"V‘Ü Ô1DÔEØ&*Ð#Ü×#Ñ#ð`ôô
 #6×"GÑ"GÈÓ"X‘Ø Ð(Ü"5´l³nÄlÃnÓ"U�à!"ÐØÐ%Ø%3°AÑ%6Ñ"ØÐ(Ø%4×%CÑ%CÓ%EÐ"àÐ!Ü"Ÿ\™\Ø&Ð(>ÀÑ(KÐTa×ThÑThôˆNð Ð!ð BQÐA\�×.Ñ.Ó0°:Ò=Ðblð ô #ŸZ™Z¨
°OÈM×L`ÑL`ÔaˆNà�;‰;×!Ò!Ø×2Ñ2ØØØØ8GÐ8S�×4Ò4ÐY]Ø!ó‰Kð )ª¨D°$ºÐ)9Ñ:ˆKØ%Ÿ.™.¨}×/BÑ/B˜.ÓCˆKØ Ñ,´·±¸M×<OÑ<OÓ0P×0TÑ0TÑTˆKð !Ð,Ø=R×=WÑ=WÓ=YÑ:ÐÐ 7¸Ø$6Ð8OÐ#PÐ Ø%Ð-Ü).¯©Ð4HÐQ^×QeÑQeÔ)fÐ&Ø.2×.HÑ.HÐI_Ó.`Ñ+à.2Ð+ð ×&Ñ& y°$·+±+×2HÑ2HÓIˆ	Ø#×1Ñ1Ð2FÈÏÉ×H^ÑH^Ó_ÐÙ"6™B¸DÐÙ0™°dˆÙ&7™r¸dÐØˆØ(,Ð%àŸ™ ]Ó3ˆä(¨¯©Ó4ò <	V‰OˆAˆ|Ø'¨™lˆOØ)=¸aÑ)@Ð&Ù#Ø$5¸Ð8HÑ$HÐ!à×*Ò*¨t¯}ª}ÙÜ—N‘NØtôð !&�IØ $× AÑ AØ ×(Ñ(Ø!ØØ!Ø)Ø3Ø1Ø#Ø.ØØØ%Ø"ó!‘ñ  !-Ø!Ø#.Ø"/Ø*?Ø+JØ2OØ$3Ø/IØ#2Ø'Ø&7Ø#1ô!�ð" ˜EÑ!Ø -¨b¨qÐ 1°GÑ ;¸mÈAÈBÐ>OÑ O�à0=¸b¸qÐ0AÑ-ˆMÐ-ð
 *¨!Ñ,ˆMØ$Ð0Ø0=ÑCT¹aÐZ[Ñ0\Ð-â Ø!/°=ÀÑ3CÐ2EÑ!E�Ø(Ñ4Ø+?À=ÐQRÑCSÐBUÑ+UÑ(ðy<	Vð| ×-Ñ-¨mÓ<ˆØŸ™ ]Ó3ˆà—‘˜mÓ,ˆñ  Ø 1°]Ð4DÑ DÐàˆØÐà—Y‘Y˜vŸ}™}Ó-ˆFÜ×*Ñ*¸ÈÔOˆHá˜F×-Ñ-Ó/×4Ñ4°R¸¿¹ÀR»ÓIÈ6×K\ÑK\ÓK^×KcÑKcÐdfÓKgÓhˆDá+4Ñ'¸$ˆ
Ù&Ø(×=Ñ=ˆJÙØ(×8Ñ8Ó:ˆJáÜñ ð ØØØ%Ø"Ø(ðôó ð ô 1ØØØ&Ø+Ø%Ø1ô
ð 	
r6   Úinput_tensorc           
      óÆ  — | j                   j                  dk(  r|�|dk(  j                  «       r|S y | j                   j                  dk(  r7t        |t        j
                  «      rt        |«      }t        |t        «      r|S |�|j                  «       nd}t        |t        «      }| j                   j                  dk(  r(|s&|s$t        j                  |||| j                  ¬«      ry |j                  |j                  }	}|j                  d   }
|r|j!                  «       }n1t        |t        j
                  «      r|j                  d   n||
z   dz   }| j#                  ||
|||	||j                  d   ¬	«      }| j                   j                  dk(  rQ|�O|j                  j$                  d
v r7|s5t	        j&                  |«      j(                  }t        j*                  ||«      }|S )NÚflash_attention_2rí   Úflex_attentionr   Úsdpa)r»  rÔ  Úis_trainingr!   r9   )Úsequence_lengthÚtarget_lengthr@   rƒ   rc  r€   )ÚcudaÚxpu)rO   Ú_attn_implementationr”  r®   r-   rg   r&   r%   rÊ  r   r   Ú_ignore_causal_mask_sdpar‡   r@   rƒ   rˆ   Úget_max_cache_shapeÚ5_prepare_4d_causal_attention_mask_with_cache_positionÚtyper�   rŽ   Ú_unmask_unattended)r1   r”   rß  rc  r¨  r—   Úpast_seen_tokensÚusing_static_cacher@   rƒ   rå  ræ  r{  Ú	min_dtypes                 r5   rÌ  z'Pix2StructTextModel._update_causal_mask4  sÖ  € ð �;‰;×+Ñ+Ð/BÒBØÐ)¨~ÀÑ/D×.IÑ.IÔ.KØ%Ð%ØØ�;‰;×+Ñ+Ð/?Ò?Ü˜.¬%¯,©,Ô7Ü!<¸^Ó!L�Ü˜.¬)Ô4Ø%Ð%ð
 @OÐ?Z˜?×9Ñ9Ô;Ð`aÐÜ'¨¼ÓEÐð �;‰;×+Ñ+¨vÒ5Ñ>PÑYjÜ%×>Ñ>ØØ*Ø'7Ø ŸM™Mõ	ð à$×*Ñ*¨L×,?Ñ,?ˆvˆØ&×,Ñ,¨QÑ/ˆÙØ+×?Ñ?ÓA‰Mô ˜n¬e¯l©lÔ;ð ×$Ñ$ RÒ(à%¨Ñ7¸!Ñ;ð ð ×PÑPØØ+Ø'ØØØ)Ø#×)Ñ)¨!Ñ,ð Qó 
ˆð �K‰K×,Ñ,°Ò6ØÐ*Ø×%Ñ%×*Ñ*¨oÑ=Ù%ô
 Ÿ™ EÓ*×.Ñ.ˆIÜ0×CÑCÀKÐQZÓ[ˆKàÐr6   rå  ræ  r@   rƒ   r€   c                 ó˜  — | �| j                  «       dk(  r| }|S t        j                  |«      j                  }	t        j                  ||f|	||¬«      }|dk7  rt        j
                  |d¬«      }|t        j                  ||¬«      |j                  dd«      kD  z  }|dddd…dd…f   j                  |ddd«      }| �Œ|j                  «       }| j                  d   }
|dd…dd…dd…d|
…f   | dd…dddd…f   j                  |j                  «      z   }|dk(  }|dd…dd…dd…d|
…f   j                  ||	«      |dd…dd…dd…d|
…f<   |S )	a°  
        Creates a causal 4D mask of shape `(batch_size, 1, query_length, key_value_length)` from a 2D mask of shape
        `(batch_size, key_value_length)`, or if the input `attention_mask` is already 4D, do nothing.

        Args:
            attention_mask (`torch.Tensor`):
                A 2D attention mask of shape `(batch_size, key_value_length)` or a 4D attention mask of shape
                `(batch_size, 1, query_length, key_value_length)`.
            sequence_length (`int`):
                The sequence length being processed.
            target_length (`int`):
                The target length: when generating with static cache, the mask should be as long as the static cache,
                to account for the 0 padding, the part of the cache that is not filled yet.
            dtype (`torch.dtype`):
                The dtype to use for the 4D attention mask.
            device (`torch.device`):
                The device to place the 4D attention mask on.
            cache_position (`torch.Tensor`):
                Indices depicting the position of the input sequence tokens in the sequence.
            batch_size (`torch.Tensor`):
                Batch size.
        NrÀ  )Ú
fill_valuer@   rƒ   r!   )Údiagonalr¿  r9   r   )r„   r-   r�   rŽ   r  Útriur^  ÚreshapeÚexpandr  rˆ   r;   rƒ   rŒ   )r”   rå  ræ  r@   rƒ   rc  r€   rÐ  r{  rñ  Úmask_lengthÚpadding_masks               r5   rì  zIPix2StructTextModel._prepare_4d_causal_attention_mask_with_cache_positionz  sy  € ðD Ð%¨.×*<Ñ*<Ó*>À!Ò*Cà(ˆKð* Ðô' Ÿ™ EÓ*×.Ñ.ˆIÜŸ*™*Ø  -Ð0¸YÈeÐ\bôˆKð  !Ò#Ü#Ÿj™j¨¸qÔA�Øœ5Ÿ<™<¨¸fÔEÈ×H^ÑH^Ð_aÐcdÓHeÑeÑeˆKØ% d¨D²!²QÐ&6Ñ7×>Ñ>¸zÈ1ÈbÐRTÓUˆKØÐ)Ø)×/Ñ/Ó1�Ø,×2Ñ2°2Ñ6�Ø*ª1ªa²°L°[°LÐ+@ÑAÀNÒSTÐVZÐ\`ÒbcÐScÑDd×DgÑDgØ×&Ñ&óEñ  �ð  ,¨qÑ0�Ø5@ÂÂAÂqÈ,È;È,ÐAVÑ5W×5cÑ5cØ  )ó6�šAšq¢! \ k \Ð1Ñ2ð Ðr6   )NNNNNNNNNNNNNN)F)$rG   rH   rI   r#   r  r3  Ú_tied_weights_keysr2  r+   r®  r  r´  r·  rº  r   Ú PIX2STRUCT_TEXT_INPUTS_DOCSTRINGr    r   r6  r   r-   Ú
LongTensorÚFloatTensorrg   r   rÅ   r   rE   r   rÌ  r  r4  r@   rƒ   rì  rJ   rK   s   @r5   rü   rü   þ  s“  ø„ ð
 (€LØ.Ð/ÐØ*Ð+ÐØ&*Ð#ô,ò(&ò<!ò+òò&ñ +Ð+KÓLÙÐ+LÐ[jÔkð 15Ø6:Ø=AØ>BØ48Ø15Ø7;ØEIØ$(Ø,0Ø/3Ø-1Ø&*Ø59ñi
à˜E×,Ñ,Ñ-ði
ð ! ×!2Ñ!2Ñ3ði
ð  (¨×(9Ñ(9Ñ:ð	i
ð
 !)¨×):Ñ):Ñ ;ði
ð   × 0Ñ 0Ñ1ði
ð ˜E×-Ñ-Ñ.ði
ð ' u§|¡|Ñ4ði
ð " %¨¨e×.?Ñ.?Ñ(@Ñ"AÑBði
ð ˜D‘>ði
ð $ D™>ði
ð ' t™nði
ð ˜×)Ñ)Ñ*ði
ð ˜d‘^ði
ð ! ×!1Ñ!1Ñ2ði
ð" 
ˆu�U×&Ñ&¨Ð+Ñ,Ð.OÐOÑ	Pò#i
ó ló Mði
ðd #(ñDàŸ™ðDð —l‘lðDð Ÿ™ð	Dð
 ðDð  óDðL ð7ØŸ™ð7àð7ð ð7ð �{‰{ð	7ð
 —‘ð7ð Ÿ™ð7ð ò7ó ô7r6   rü   zhA conditional generation model with a language modeling head. Can be used for sequence generation tasks.c            '       óš  ‡ — e Zd ZeZdZdgZdefˆ fd„Zd„ Zd„ Z	de
j                  fd„Zd	„ Zd
„ Zd„ Z ee«       eee¬«      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddeej.                     deej.                     deej0                     deej2                     deej.                     deej.                     deej4                     deeeej.                           deeeej.                           deej0                     deej4                     dee   dee   dee   dee   deej0                     deeej.                     ef   f"d„«       «       Zˆ xZS )Ú"Pix2StructForConditionalGenerationr^   zdecoder.lm_head.weightrO   c                 óÎ   •— t         ‰| �  |«       t        |j                  «      | _        t        |j                  «      | _        |j                  | _        | j                  «        y rR   )
r*   r+   r  Úvision_configr  rü   ró   ÚdecoderÚis_vqar  r]   s     €r5   r+   z+Pix2StructForConditionalGeneration.__init__¿  sK   ø€ Ü‰Ñ˜Ô ä,¨V×-AÑ-AÓBˆŒÜ*¨6×+=Ñ+=Ó>ˆŒà—m‘mˆŒð 	�‰Õr6   c                 ó6   — | j                   j                  «       S rR   )r  r  r  s    r5   r  z7Pix2StructForConditionalGeneration.get_input_embeddingsÊ  s   € Ø�|‰|×0Ñ0Ó2Ð2r6   c                 ó:   — | j                   j                  |«       y rR   )r  r´  r²  s     r5   r´  z7Pix2StructForConditionalGeneration.set_input_embeddingsÍ  s   € Ø�‰×)Ñ)¨.Õ9r6   rP   c                 ó6   — | j                   j                  «       S rR   )r  r·  r  s    r5   r·  z8Pix2StructForConditionalGeneration.get_output_embeddingsÐ  s   € Ø�|‰|×1Ñ1Ó3Ð3r6   c                 ó:   — | j                   j                  |«       y rR   )r  rº  r²  s     r5   rº  z8Pix2StructForConditionalGeneration.set_output_embeddingsÓ  s   € Ø�‰×*Ñ*¨>Õ:r6   c                 ó   — | j                   S rR   )r  r  s    r5   Úget_decoderz.Pix2StructForConditionalGeneration.get_decoderÖ  r¸  r6   c                 ó   — | j                   S rR   )r  r  s    r5   Úget_encoderz.Pix2StructForConditionalGeneration.get_encoderÙ  r¸  r6   r&  r”   ræ   rè   r¿   Údecoder_head_maskr¼  r.  r¨  r½  Údecoder_inputs_embedsrv  r—   rÎ   rÏ   rc  c                 ó$  — |�|n| j                   j                  j                  }|�|n| j                   j                  }|€| j	                  ||||||¬«      }nI|rGt        |t        «      s7t        |d   t        |«      dkD  r|d   ndt        |«      dkD  r|d   nd¬«      }|d   }|
�U|€S|€Q| j                  |
«      }|�|n2|j                  | j                   j                  «      j                  «       }d|dd…df<   | j                  ||||	||||||||
||¬«      }|s||z   S t        |j                  |j                  |j                   |j"                  |j$                  |j&                  |j(                  |j"                  |j$                  ¬«	      S )	a¾  
        Returns:

        Example:

        Inference:

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

        >>> processor = AutoProcessor.from_pretrained("google/pix2struct-textcaps-base")
        >>> model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-textcaps-base")

        >>> url = "https://www.ilankelman.org/stopsigns/australia.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

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

        >>> # autoregressive generation
        >>> generated_ids = model.generate(**inputs, max_new_tokens=50)
        >>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
        >>> print(generated_text)
        A stop sign is on a street corner.

        >>> # conditional generation
        >>> text = "A picture of"
        >>> inputs = processor(text=text, images=image, return_tensors="pt", add_special_tokens=False)

        >>> generated_ids = model.generate(**inputs, max_new_tokens=50)
        >>> generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
        >>> print(generated_text)
        A picture of a stop sign with a red stop sign
        ```

        Training:

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

        >>> processor = AutoProcessor.from_pretrained("google/pix2struct-base")
        >>> model = Pix2StructForConditionalGeneration.from_pretrained("google/pix2struct-base")

        >>> url = "https://www.ilankelman.org/stopsigns/australia.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)
        >>> text = "A stop sign is on the street corner."

        >>> inputs = processor(images=image, return_tensors="pt")
        >>> labels = processor(text=text, return_tensors="pt").input_ids

        >>> # forward pass
        >>> outputs = model(**inputs, labels=labels)
        >>> loss = outputs.loss
        >>> print(f"{loss.item():.5f}")
        5.94282
        ```N)r^   r”   r¿   r—   rÎ   rÏ   r   r!   r8   r×   )rç   r”   r»  r¨  r–  r—  r¿   r¼  rv  r—   rÎ   r½  rÏ   rc  )	rÅ  rÆ  r¨  Údecoder_hidden_statesÚdecoder_attentionsrÇ  Úencoder_last_hidden_stater–  Úencoder_attentions)rO   ró   rv  r)  r  r®   r   r§  r  Úner	  r+  r  r   rÅ  rÆ  r¨  rC   rÙ   rÇ  rØ   )r1   r^   r”   ræ   rè   r¿   r  r¼  r.  r¨  r½  r  rv  r—   rÎ   rÏ   rc  rC   Údecoder_outputss                      r5   rE   z*Pix2StructForConditionalGeneration.forwardÜ  sÑ  € ð` "+Ð!6‘I¸D¿K¹K×<SÑ<S×<]Ñ<]ˆ	Ø%0Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆð Ð"Ø"Ÿl™lØ"3Ø-Ø#Ø"3Ø%9Ø'ð +ó ‰Oñ ¤¨O¼_Ô!MÜ-Ø"1°!Ñ"4Ü47¸Ó4HÈ1Ò4L˜o¨aÒ0ÐRVÜ14°_Ó1EÈÒ1I˜?¨1Ò-ÈtôˆOð (¨Ñ*ˆàÐÐ"3Ð";Ð@UÐ@]à $× 1Ñ 1°&Ó 9Ðð *Ð5ñ 'à&×)Ñ)¨$¯+©+×*BÑ*BÓC×IÑIÓKð #ð ,-Ð"¢1 a 4Ñ(ð Ÿ,™,Ø'Ø1Ø/Ø+Ø"/Ø#1Ø'Ø!5ØØ/Ø!5ØØ#Ø)ð 'ó 
ˆñ" Ø" _Ñ4Ð4äØ ×%Ñ%Ø"×)Ñ)Ø+×;Ñ;Ø"1×"?Ñ"?Ø.×9Ñ9Ø,×=Ñ=Ø&5×&GÑ&GØ"1×"?Ñ"?Ø.×9Ñ9ô

ð 
	
r6   )NNNNNNNNNNNNNNNN) rG   rH   rI   r"   r  r1  rú  r+   r  r´  r   ÚModuler·  rº  r	  r  r   ÚPIX2STRUCT_INPUTS_DOCSTRINGr    r   r6  r   r-   rý  rü  Ú
BoolTensorrg   r   rÅ   r   rE   rJ   rK   s   @r5   rÿ  rÿ  ¶  s!  ø„ ð
 $€LØ)€OØ2Ð3Ðð	Ð/õ 	ò3ò:ð4 r§y¡yó 4ò;òòñ +Ð+FÓGÙÐ+=ÈOÔ\ð :>Ø6:Ø8<Ø=AØ15Ø9=Ø7;ØEIØEIØ-1Ø8<Ø$(Ø,0Ø/3Ø&*Ø59ñ#N
à# E×$5Ñ$5Ñ6ðN
ð ! ×!2Ñ!2Ñ3ðN
ð $ E×$4Ñ$4Ñ5ð	N
ð
 !)¨×)9Ñ)9Ñ :ðN
ð ˜E×-Ñ-Ñ.ðN
ð $ E×$5Ñ$5Ñ6ðN
ð ' u§|¡|Ñ4ðN
ð " %¨¨e×.?Ñ.?Ñ(@Ñ"AÑBðN
ð " %¨¨e×.?Ñ.?Ñ(@Ñ"AÑBðN
ð ˜×)Ñ)Ñ*ðN
ð  (¨¯©Ñ5ðN
ð ˜D‘>ðN
ð $ D™>ðN
ð ' t™nðN
ð  ˜d‘^ð!N
ð" ! ×!1Ñ!1Ñ2ð#N
ð$ 
ˆu�U×&Ñ&Ñ'Ð);Ð;Ñ	<ò%N
ó ]ó HôN
r6   rÿ  )rä   rÿ  r  rü   )Trf   rQ  Útypingr   r   r   r   r   r-   Útorch.utils.checkpointr   Úactivationsr
   Úcache_utilsr   r   r   r   Ú
generationr   Úmodeling_attn_mask_utilsr   Úmodeling_outputsr   r   r   r   r   Úmodeling_utilsr   Úpytorch_utilsr   Úutilsr   r   r   r   r   r   r   r   r    Úconfiguration_pix2structr"   r#   r$   Ú!torch.nn.attention.flex_attentionr%   Úintegrations.flex_attentionr&   Ú
get_loggerrG   rH  r6  r  r(   Úapex.normalizationrL   ÚinfoÚImportErrorÚ	Exceptionr¥  ÚappendrN   ri   r¢   rµ   rÇ   rä   Ú!PIX2STRUCT_VISION_START_DOCSTRINGr5  r  rò   r<  r÷   r�  rŠ  rŽ  ÚPIX2STRUCT_START_DOCSTRINGrû  r  rü   rÿ  Ú__all__rÑ   r6   r5   ú<module>r.     sm  ðñ ã ß 5Õ 5ã Û Ý å !ß PÓ PÝ )Ý >÷õ õ .Ý 1÷
÷ 
õ 
÷ eÑ dñ  Ô!Ý;åJð 
ˆ×	Ñ	˜HÓ	%€ð %€ô+˜"Ÿ)™)ô +ð2	Ý/à&Ðà
‡K�KÐiÔjð Ð × Ñ Ð/Ô 0ô! §¡ô !ôH^ §	¡	ô ^ôD˜"Ÿ)™)ô ô:(˜BŸI™Iô (ôV2
˜bŸi™iô 2
ôj~! ô ~!ðB	%Ð !ð&Ð "ñ6 ØpØ%óôj
Ð5ó j
ó	ðj
ô\ r§y¡yô ô:˜BŸI™Iô ô P˜bŸi™iô Pôh! r§y¡yô !ôJ#¨¯	©	ô #ôLY˜"Ÿ)™)ô YðxÐ ð,`$Ð  ðDVÐ ñr Ø/ØóôqÐ3ó qó	ðqñh ØnØóôr
Ð)BÀOó r
ó	ðr
òj�øða8 ò 	âØò 	Ø
‡N�NÐ_Ô`Úð	ús   Â8H* È*IÈ2IÉ
I