§
    ‚Štjù
 ã            	       óž	  — d Z ddlZddlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlZddlZddlmZ dd	lmZmZmZ d
dlmZ d
dlmZ d
dlmZ d
dlmZ 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* ee+e,f         Z-e.ej/        ej/        dz  ej/        f         Z0ede0f         Z1edef         Z2 e'j3        e4¦  «        Z5 e&d¬¦  «        e G d„ de%¦  «        ¦   «         ¦   «         Z6 e&d¬¦  «        e G d„ de%¦  «        ¦   «         ¦   «         Z7 e&d¬¦  «        e G d„ de%¦  «        ¦   «         ¦   «         Z8 e&d¬¦  «        e G d „ d!e%¦  «        ¦   «         ¦   «         Z9 G d"„ d#ej:        ¦  «        Z; G d$„ d%ej:        ¦  «        Z< G d&„ d'ej:        ¦  «        Z= G d(„ d)ej:        ¦  «        Z> G d*„ d+ej:        ¦  «        Z? G d,„ d-ej:        ¦  «        Z@ G d.„ d/ej:        ¦  «        ZAe& G d0„ d1e!¦  «        ¦   «         ZB e&d2¬¦  «         G d3„ d4eB¦  «        ¦   «         ZC e&d5¬¦  «         G d6„ d7eB¦  «        ¦   «         ZD e&d8¬¦  «         G d9„ d:eB¦  «        ¦   «         ZE e&d;¬¦  «         G d<„ d=eB¦  «        ¦   «         ZF e&d>¬¦  «         G d?„ d@eB¦  «        ¦   «         ZG e&dA¬¦  «         G dB„ dCeB¦  «        ¦   «         ZH e&dD¬¦  «         G dE„ dFeB¦  «        ¦   «         ZI e&dG¬¦  «         G dH„ dIeB¦  «        ¦   «         ZJ	 	 	 	 d‹dK„ZK G dL„ dMej:        ejL        ¬N¦  «        ZM G dO„ dPeM¦  «        ZN G dQ„ dReM¦  «        ZO G dS„ dTeM¦  «        ZP G dU„ dVeM¦  «        ZQ G dW„ dXeM¦  «        ZRdYe-dZej/        d[ee+ej/        f         fd\„ZS G d]„ d^eM¦  «        ZTdŒd_ej/        d`e,dae,d[ej/        fdb„ZU G dc„ ddejV        ¦  «        ZW G de„ dfej:        ¦  «        ZXd�dj„ZYdŽdl„ZZ G dm„ dnej:        ejL        ¬N¦  «        Z[ G do„ dpe[¦  «        Z\dq„ Z] G dr„ dse[¦  «        Z^ G dt„ duej:        ¦  «        Z_ G dv„ dwe_¦  «        Z` G dx„ dyej:        ¦  «        Za G dz„ d{ej:        ¦  «        Zb G d|„ d}ej:        ¦  «        Zc G d~„ dej:        ¦  «        Zd G d€„ d�ej:        ¦  «        Ze G d‚„ dƒe_¦  «        Zf G d„„ d…e_¦  «        Zg G d†„ d‡e_¦  «        Zh G dˆ„ d‰e_¦  «        Zig dŠ¢ZjdS )�zPyTorch Perceiver model.é    N)ÚCallableÚMapping)Ú	dataclass)Úreduce)Ú__add__)ÚAnyÚOptional)Únn)ÚBCEWithLogitsLossÚCrossEntropyLossÚMSELossé   )Úinitialization)ÚACT2FN)Úcreate_bidirectional_mask)Ú"BaseModelOutputWithCrossAttentions)ÚPreTrainedModel)Úapply_chunking_to_forward)ÚModelOutputÚauto_docstringÚloggingÚ	torch_inté   )ÚPerceiverConfig.zw
    Base class for Perceiver base model's outputs, with potential hidden states, attentions and cross-attentions.
    )Úcustom_introc                   óÎ   — e Zd ZU dZdZej        dz  ed<   dZej        dz  ed<   dZ	e
ej                 dz  ed<   dZe
ej                 dz  ed<   dZe
ej                 dz  ed<   dS )ÚPerceiverModelOutputz£
    logits (`torch.FloatTensor` of shape `(batch_size, num_labels)`):
        Classification (or regression if config.num_labels==1) scores (before SoftMax).
    NÚlogitsÚlast_hidden_stateÚhidden_statesÚ
attentionsÚcross_attentions)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚtorchÚFloatTensorÚ__annotations__r   r    Útupler!   r"   © ó    ún/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/perceiver/modeling_perceiver.pyr   r   /   s§   € € € € € € ðð ð
 (,€FˆEÔ Ñ$Ð+Ð+Ñ+Ø26Ð�uÔ(¨4Ñ/Ð6Ð6Ñ6Ø59€M�5˜Ô*Ô+¨dÑ2Ð9Ð9Ñ9Ø26€J��eÔ'Ô(¨4Ñ/Ð6Ð6Ñ6Ø8<Ð�e˜EÔ-Ô.°Ñ5Ð<Ð<Ñ<Ð<Ð<r,   r   zT
    Base class for Perceiver decoder outputs, with potential cross-attentions.
    c                   ó\   — e Zd ZU dZdZej        dz  ed<   dZe	ej                 dz  ed<   dS )ÚPerceiverDecoderOutputzp
    logits (`torch.FloatTensor` of shape `(batch_size, num_labels)`):
        Output of the basic decoder.
    Nr   r"   )
r#   r$   r%   r&   r   r'   r(   r)   r"   r*   r+   r,   r-   r/   r/   B   sT   € € € € € € ðð ð
 (,€FˆEÔ Ñ$Ð+Ð+Ñ+Ø8<Ð�e˜EÔ-Ô.°Ñ5Ð<Ð<Ñ<Ð<Ð<r,   r/   zC
    Base class for Perceiver's masked language model outputs.
    c                   óÎ   — e Zd ZU dZdZej        dz  ed<   dZej        dz  ed<   dZ	e
ej                 dz  ed<   dZe
ej                 dz  ed<   dZe
ej                 dz  ed<   dS )ÚPerceiverMaskedLMOutputa[  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
        Masked language modeling (MLM) loss.
    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).
    NÚlossr   r    r!   r"   ©r#   r$   r%   r&   r2   r'   r(   r)   r   r    r*   r!   r"   r+   r,   r-   r1   r1   R   s¦   € € € € € € ðð ð &*€Dˆ%Ô
˜dÑ
"Ð)Ð)Ñ)Ø'+€FˆEÔ Ñ$Ð+Ð+Ñ+Ø59€M�5˜Ô*Ô+¨dÑ2Ð9Ð9Ñ9Ø26€J��eÔ'Ô(¨4Ñ/Ð6Ð6Ñ6Ø8<Ð�e˜EÔ-Ô.°Ñ5Ð<Ð<Ñ<Ð<Ð<r,   r1   zƒ
    Base class for Perceiver's outputs of sequence/image classification models, optical flow and multimodal
    autoencoding.
    c                   óÎ   — e Zd ZU dZdZej        dz  ed<   dZej        dz  ed<   dZ	e
ej                 dz  ed<   dZe
ej                 dz  ed<   dZe
ej                 dz  ed<   dS )ÚPerceiverClassifierOutputaO  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
        Classification (or regression if config.num_labels==1) loss.
    logits (`torch.FloatTensor` of shape `(batch_size, config.num_labels)`):
        Classification (or regression if config.num_labels==1) scores (before SoftMax).
    Nr2   r   r    r!   r"   r3   r+   r,   r-   r5   r5   g   s¦   € € € € € € ðð ð &*€Dˆ%Ô
˜dÑ
"Ð)Ð)Ñ)Ø'+€FˆEÔ Ñ$Ð+Ð+Ñ+Ø59€M�5˜Ô*Ô+¨dÑ2Ð9Ð9Ñ9Ø26€J��eÔ'Ô(¨4Ñ/Ð6Ð6Ñ6Ø8<Ð�e˜EÔ-Ô.°Ñ5Ð<Ð<Ñ<Ð<Ð<r,   r5   c                   ó.   ‡ — e Zd ZdZˆ fd„Zdefd„Zˆ xZS )ÚPerceiverEmbeddingsz Construct the latent embeddings.c                 ó´   •— t          ¦   «                              ¦   «          t          j        t	          j        |j        |j        ¦  «        ¦  «        | _        d S ©N)	ÚsuperÚ__init__r
   Ú	Parameterr'   ÚrandnÚnum_latentsÚ	d_latentsÚlatents©ÚselfÚconfigÚ	__class__s     €r-   r;   zPerceiverEmbeddings.__init__€   s@   ø€ Ý‰Œ×ÒÑÔÐÝ”|¥E¤K°Ô0BÀFÔDTÑ$UÔ$UÑVÔVˆŒˆˆr,   Ú
batch_sizec                 ó:   — | j                              |dd¦  «        S ©Néÿÿÿÿ)r@   Úexpand)rB   rE   s     r-   ÚforwardzPerceiverEmbeddings.forward„   s   € ØŒ|×"Ò" :¨r°2Ñ6Ô6Ð6r,   )r#   r$   r%   r&   r;   ÚintrJ   Ú__classcell__©rD   s   @r-   r7   r7   }   s^   ø€ € € € € Ø*Ð*ðWð Wð Wð Wð Wð7 #ð 7ð 7ð 7ð 7ð 7ð 7ð 7ð 7r,   r7   c                   ó¶   ‡ — e Zd ZdZ	 	 	 	 	 	 dˆ fd„	Zd„ Z	 	 	 	 ddej        dej        dz  d	ej        dz  d
ej        dz  de	dz  de
ej                 fd„Zˆ xZS )ÚPerceiverSelfAttentionz`Multi-headed {cross, self}-attention. Can be used both in the encoder as well as in the decoder.FNr   c                 ó   •— t          ¦   «                              ¦   «          || _        |€|}|€|}||z  dk    rt          d|› d|› d�¦  «        ‚||z  dk    rt          d|› d|› d�¦  «        ‚|| _        || _        | j        |z  | _        | j        |z  | _        t          j	        |¦  «        | _
        |rt          j	        |¦  «        nt          j        ¦   «         | _        t          j        ||¦  «        | _        t          j        ||¦  «        | _        t          j        ||¦  «        | _        t          j        |j        ¦  «        | _        d S )Nr   zqk_channels (z") must be divisible by num_heads (ú).zv_channels ()r:   r;   Ú	num_headsÚ
ValueErrorÚqk_channelsÚ
v_channelsÚqk_channels_per_headÚv_channels_per_headr
   Ú	LayerNormÚ
layernorm1ÚIdentityÚ
layernorm2ÚLinearÚqueryÚkeyÚvalueÚDropoutÚattention_probs_dropout_probÚdropout)	rB   rC   Úis_cross_attentionrT   rU   rR   Úq_dimÚkv_dimrD   s	           €r-   r;   zPerceiverSelfAttention.__init__‹   sW  ø€ õ 	‰Œ×ÒÑÔÐØ"ˆŒð ÐØˆKð ÐØ$ˆJØ˜Ñ" aÒ'Ð'ÝÐi¨[ÐiÐiÐ\eÐiÐiÐiÑjÔjÐjØ˜	Ñ! QÒ&Ð&ÝÐg¨JÐgÐgÐZcÐgÐgÐgÑhÔhÐhà&ˆÔØ$ˆŒØ$(Ô$4¸	Ñ$AˆÔ!Ø#'¤?°iÑ#?ˆÔ õ œ, uÑ-Ô-ˆŒØ2DÐW�"œ, vÑ.Ô.Ð.Í"Ì+É-Ì-ˆŒõ ”Y˜u kÑ2Ô2ˆŒ
Ý”9˜V [Ñ1Ô1ˆŒÝ”Y˜v zÑ2Ô2ˆŒ
å”z &Ô"EÑFÔFˆŒˆˆr,   c                 ó’   — |                      ¦   «         d d…         | j        |fz   } |j        |Ž }|                     dddd¦  «        S )NrH   r   é   r   r   )ÚsizerR   ÚviewÚpermute)rB   ÚxÚchannels_per_headÚnew_x_shapes       r-   Útranspose_for_scoresz+PerceiverSelfAttention.transpose_for_scores´   sJ   € Ø—f’f‘h”h˜s ˜s”m t¤~Ð7HÐ&IÑIˆØˆAŒF�KÐ ˆØ�yŠy˜˜A˜q !Ñ$Ô$Ð$r,   r    Úattention_maskÚinputsÚinputs_maskÚoutput_attentionsÚreturnc                 óü  — |                       |¦  «        }|                      |¦  «        }|d u}|                      |¦  «        }|r-|                      |¦  «        }|                      |¦  «        }	|}n*|                      |¦  «        }|                      |¦  «        }	|                      || j        ¦  «        }|                      || j        ¦  «        }|                      |	| j        ¦  «        }	t          j	        || 
                    dd¦  «        ¦  «        }
|j        \  }}}}|	j        \  }}}}| j        |z  }|
t          j        |¦  «        z  }
|�|
|z   }
 t          j        d¬¦  «        |
¦  «        }|                      |¦  «        }t          j	        ||	¦  «        }|                     dddd¦  «                             ¦   «         }|                     ¦   «         d d…         |fz   } |j        |Ž }|r||fn|f}|S )NrH   éþÿÿÿ©Údimr   rg   r   r   )rY   r[   r]   r^   r_   rn   rV   rW   r'   ÚmatmulÚ	transposeÚshaperR   ÚmathÚsqrtr
   ÚSoftmaxrb   rj   Ú
contiguousrh   ri   )rB   r    ro   rp   rq   rr   rc   ÚqueriesÚkeysÚvaluesÚattention_scoresrE   rR   Úseq_lenÚ
q_head_dimÚ_Ú
v_head_dimÚhiddensÚattention_probsÚcontext_layerÚnew_context_layer_shapeÚoutputss                         r-   rJ   zPerceiverSelfAttention.forward¹   s  € ð Ÿš¨Ñ6Ô6ˆØ—’ Ñ(Ô(ˆð $¨4Ð/ÐØ—*’*˜]Ñ+Ô+ˆàð 	/Ø—8’8˜FÑ#Ô#ˆDØ—Z’Z Ñ'Ô'ˆFØ(ˆNˆNà—8’8˜MÑ*Ô*ˆDØ—Z’Z Ñ.Ô.ˆFð ×+Ò+¨G°TÔ5NÑOÔOˆØ×(Ò(¨¨tÔ/HÑIÔIˆØ×*Ò*¨6°4Ô3KÑLÔLˆõ !œ<¨°·²ÀÀBÑ1GÔ1GÑHÔHÐà5<´]Ñ2ˆ
�I˜w¨
Ø$œlÑˆˆ1ˆa�Ø”. :Ñ-ˆà+­d¬i¸
Ñ.CÔ.CÑCÐàÐ%à/°.Ñ@Ðð -�"œ*¨Ð,Ñ,Ô,Ð-=Ñ>Ô>ˆð Ÿ,š, Ñ7Ô7ˆåœ _°fÑ=Ô=ˆà%×-Ò-¨a°°A°qÑ9Ô9×DÒDÑFÔFˆØ"/×"4Ò"4Ñ"6Ô"6°s¸°sÔ";¸w¸jÑ"HÐØ*˜Ô*Ð,CÐDˆà6GÐ]�= /Ð2Ð2ÈmÐM]ˆàˆr,   )FNNr   NN©NNNF)r#   r$   r%   r&   r;   rn   r'   ÚTensorr(   Úboolr*   rJ   rL   rM   s   @r-   rO   rO   ˆ   só   ø€ € € € € ØjÐjð
 !ØØØØØð'Gð 'Gð 'Gð 'Gð 'Gð 'GðR%ð %ð %ð 48Ø+/Ø04Ø).ð:ð :à”|ð:ð Ô)¨DÑ0ð:ð Ô! DÑ(ð	:ð
 Ô&¨Ñ-ð:ð   $™;ð:ð 
ˆuŒ|Ô	ð:ð :ð :ð :ð :ð :ð :ð :r,   rO   c                   óB   ‡ — e Zd Zˆ fd„Zdej        dej        fd„Zˆ xZS )ÚPerceiverSelfOutputc                 ó|   •— t          ¦   «                              ¦   «          t          j        ||¦  «        | _        d S r9   )r:   r;   r
   r\   Údense)rB   rC   Úinput_channelsÚoutput_channelsrD   s       €r-   r;   zPerceiverSelfOutput.__init__÷   s0   ø€ Ý‰Œ×ÒÑÔÐÝ”Y˜~¨Ñ?Ô?ˆŒ
ˆ
ˆ
r,   r    rs   c                 ó0   — |                       |¦  «        }|S r9   )r’   ©rB   r    s     r-   rJ   zPerceiverSelfOutput.forwardû   s   € ØŸ
š
 =Ñ1Ô1ˆØÐr,   )r#   r$   r%   r;   r'   r�   rJ   rL   rM   s   @r-   r�   r�   ö   sc   ø€ € € € € ð@ð @ð @ð @ð @ð U¤\ð °e´lð ð ð ð ð ð ð ð r,   r�   c                   ó²   ‡ — e Zd ZdZ	 	 	 	 	 	 	 dˆ fd„	Z	 	 	 	 ddej        dej        dz  d	ej        dz  d
ej        dz  dedz  de	ej                 fd„Z
ˆ xZS )ÚPerceiverAttentionz*Attention module, including a dense block.FNr   Tc	           	      ód  •— t          ¦   «                              ¦   «          |r6|€4|j        dk    r|}n.|j        dk    r|}n t          d|j        › d�¦  «        ‚|€|}|€|}t	          |||||||¬¦  «        | _        d }	|r|}	n|	€|}	t          || j        j        |	¬¦  «        | _        || _	        d S )NÚqÚkvzUnknown value z) for cross_attention_shape_for_attention.)rc   rT   rU   rR   rd   re   )r“   r”   )
r:   r;   Ú#cross_attention_shape_for_attentionrS   rO   rB   r�   rU   ÚoutputÚuse_query_residual)rB   rC   rc   rT   rU   rR   rd   re   rž   r”   rD   s             €r-   r;   zPerceiverAttention.__init__  s  ø€ õ 	‰Œ×ÒÑÔÐàð 	) +Ð"5ØÔ9¸SÒ@Ð@Ø#��ØÔ;¸tÒCÐCØ$��å ð; VÔ%Oð ;ð ;ð ;ñô ð ð
 Ð"Ø#�ØÐ!Ø(�
Ý*ØØ1Ø#Ø!ØØØð
ñ 
ô 
ˆŒ	ð ˆØð 	-Ø#ˆOˆOàÐ&Ø",�Ý)¨&ÀÄÔAUÐgvÐwÑwÔwˆŒØ"4ˆÔÐÐr,   r    ro   rp   rq   rr   rs   c                 ó¢   — |                       |||||¦  «        }|                      |d         ¦  «        }| j        r||z   }|f|dd …         z   }|S ©Nr   r   )rB   r�   rž   )	rB   r    ro   rp   rq   rr   Úself_outputsÚattention_outputr‹   s	            r-   rJ   zPerceiverAttention.forward2  sr   € ð —y’yØØØØØñ
ô 
ˆð  Ÿ;š; |°A¤Ñ7Ô7Ðð
 Ô"ð 	@Ø/°-Ñ?Ðà#Ð%¨°Q°R°RÔ(8Ñ8ˆØˆr,   )FNNr   NNTrŒ   )r#   r$   r%   r&   r;   r'   r�   r(   rŽ   r*   rJ   rL   rM   s   @r-   r˜   r˜      sá   ø€ € € € € Ø4Ð4ð
 !ØØØØØØð-5ð -5ð -5ð -5ð -5ð -5ðd 48Ø+/Ø04Ø).ðð à”|ðð Ô)¨DÑ0ðð Ô! DÑ(ð	ð
 Ô&¨Ñ-ðð   $™;ðð 
ˆuŒ|Ô	ðð ð ð ð ð ð ð r,   r˜   c                   óF   ‡ — e Zd ZdZˆ fd„Zdej        dej        fd„Zˆ xZS )ÚPerceiverMLPz5A Transformer-style dense module to follow attention.c                 ó8  •— t          ¦   «                              ¦   «          t          j        |||z  ¦  «        | _        t          |j        t          ¦  «        rt          |j                 | _	        n|j        | _	        t          j        ||z  |¦  «        | _
        d S r9   )r:   r;   r
   r\   Údense1Ú
isinstanceÚ
hidden_actÚstrr   Úintermediate_act_fnÚdense2)rB   rC   Ú
input_sizeÚwidening_factorrD   s       €r-   r;   zPerceiverMLP.__init__R  s€   ø€ Ý‰Œ×ÒÑÔÐÝ”i 
¨O¸jÑ,HÑIÔIˆŒÝ�fÔ'­Ñ-Ô-ð 	9Ý'-¨fÔ.?Ô'@ˆDÔ$Ð$à'-Ô'8ˆDÔ$Ý”i °*Ñ <¸jÑIÔIˆŒˆˆr,   r    rs   c                 ó„   — |                       |¦  «        }|                      |¦  «        }|                      |¦  «        }|S r9   )r¦   rª   r«   r–   s     r-   rJ   zPerceiverMLP.forward[  s=   € ØŸš MÑ2Ô2ˆØ×0Ò0°Ñ?Ô?ˆØŸš MÑ2Ô2ˆØÐr,   )	r#   r$   r%   r&   r;   r'   r�   rJ   rL   rM   s   @r-   r¤   r¤   O  si   ø€ € € € € Ø?Ð?ðJð Jð Jð Jð Jð U¤\ð °e´lð ð ð ð ð ð ð ð r,   r¤   c                   ó¶   ‡ — e Zd Z	 	 	 	 	 	 	 	 dˆ fd„	Z	 	 	 	 ddej        dej        dz  d	ej        dz  d
ej        dz  dedz  deej                 fd„Z	d„ Z
ˆ xZS )ÚPerceiverLayerFNr   é   Tc
           
      ó  •— t          ¦   «                              ¦   «          |j        | _        d| _        t	          ||||||||	¬¦  «        | _        t          j        |¦  «        | _        t          |||¬¦  «        | _
        d S )Nr   )rc   rT   rU   rR   rd   re   rž   )r¬   r­   )r:   r;   Úchunk_size_feed_forwardÚseq_len_dimr˜   Ú	attentionr
   rX   Ú	layernormr¤   Úmlp)rB   rC   rc   rT   rU   rR   rd   re   r­   rž   rD   s             €r-   r;   zPerceiverLayer.__init__c  s‰   ø€ õ 	‰Œ×ÒÑÔÐØ'-Ô'EˆÔ$ØˆÔÝ+ØØ1Ø#Ø!ØØØØ1ð	
ñ 	
ô 	
ˆŒõ œ eÑ,Ô,ˆŒÝ °5È/ÐZÑZÔZˆŒˆˆr,   r    ro   rp   rq   rr   rs   c                 ó´   — |                       |||||¦  «        }|d         }|dd …         }t          | j        | j        | j        |¦  «        }	|	|z   }	|	f|z   }|S r    )rµ   r   Úfeed_forward_chunkr³   r´   )
rB   r    ro   rp   rq   rr   Úattention_outputsr¢   r‹   Úlayer_outputs
             r-   rJ   zPerceiverLayer.forward  s‚   € ð !ŸNšNØØØØØñ
ô 
Ðð -¨QÔ/Ðà# A B BÔ'ˆå0ØÔ# TÔ%AÀ4ÔCSÐUeñ
ô 
ˆð $Ð&6Ñ6ˆà�/ GÑ+ˆàˆr,   c                 óZ   — |                       |¦  «        }|                      |¦  «        }|S r9   )r¶   r·   )rB   r¢   r»   s      r-   r¹   z!PerceiverLayer.feed_forward_chunkœ  s+   € Ø—~’~Ð&6Ñ7Ô7ˆØ—x’x Ñ-Ô-ˆØÐr,   )FNNr   NNr±   TrŒ   )r#   r$   r%   r;   r'   r�   r(   rŽ   r*   rJ   r¹   rL   rM   s   @r-   r°   r°   b  sò   ø€ € € € € ð !ØØØØØØØð[ð [ð [ð [ð [ð [ð> 48Ø+/Ø04Ø).ðð à”|ðð Ô)¨DÑ0ðð Ô! DÑ(ð	ð
 Ô&¨Ñ-ðð   $™;ðð 
ˆuŒ|Ô	ðð ð ð ð:ð ð ð ð ð ð r,   r°   c                   ó¬   ‡ — e Zd ZdZdˆ fd„	Z	 	 	 	 	 	 ddej        dej        dz  dej        dz  d	ej        dz  d
edz  dedz  dedz  de	e
z  fd„Zˆ xZS )ÚPerceiverEncoderz=The Perceiver Encoder: a scalable, fully attentional encoder.Nc                 ó�  •— t          ¦   «                              ¦   «          || _        |j        |j        z  dk    r t          d|j        › d|j        › d�¦  «        ‚|j        |j        z  dk    r t          d|j        › d|j        › d�¦  «        ‚t          |d|j        |j	        |j        |j        ||j
        |j        ¬¦	  «	        | _        g }t          |j        ¦  «        D ]L}t          |d|j        |j	        |j        |j        |j        |j        ¬	¦  «        }|                     |¦  «         ŒMt#          j        |¦  «        | _        d S )
Nr   znum_z_channels (z.) must be divisible by num_self_attend_heads (rQ   z/) must be divisible by num_cross_attend_heads (T©rc   rT   rU   rR   rd   re   r­   rž   F)rc   rT   rU   rR   rd   re   r­   )r:   r;   rC   r?   Únum_self_attention_headsrS   Únum_cross_attention_headsr°   rT   rU   Úcross_attention_widening_factorrž   Úcross_attentionÚrangeÚnum_self_attends_per_blockÚself_attention_widening_factorÚappendr
   Ú
ModuleListÚself_attends)rB   rC   re   Úself_attention_layersr…   ÚlayerrD   s         €r-   r;   zPerceiverEncoder.__init__¥  sª  ø€ Ý‰Œ×ÒÑÔÐØˆŒð Ô˜fÔ=Ñ=ÀÒBÐBÝðO 6Ô#3ð Oð OØ+1Ô+JðOð Oð Oñô ð ð Ô˜fÔ>Ñ>À!ÒCÐCÝðQ 6Ô#3ð Qð QØ,2Ô,LðQð Qð Qñô ð õ  .ØØ#ØÔ*ØÔ(ØÔ6ØÔ"ØØ"ÔBØ%Ô8ð
 
ñ 
 
ô 
 
ˆÔð !#ÐÝ�vÔ8Ñ9Ô9ð 	0ð 	0ˆAÝ"ØØ#(Ø"Ô.Ø!Ô,Ø Ô9ØÔ&ØÔ'Ø &Ô Eð	ñ 	ô 	ˆEð "×(Ò(¨Ñ/Ô/Ð/Ð/åœMÐ*?Ñ@Ô@ˆÔÐÐr,   FTr    ro   rp   rq   rr   Úoutput_hidden_statesÚreturn_dictrs   c                 ó¾  — |rdnd }|rdnd }	|rdnd }
|                       |||||¬¦  «        }|d         }|r|
|d         fz   }
t          | j        j        ¦  «        D ]P}t	          | j        ¦  «        D ]1\  }}|r||fz   } ||||¬¦  «        }|d         }|r|	|d         fz   }	Œ2|r||fz   }ŒQ|st          d„ |||	|
fD ¦   «         ¦  «        S t          |||	|
¬¦  «        S )Nr+   ©ro   rp   rq   rr   r   r   )ro   rr   c              3   ó   K  — | ]}|®|V — Œ	d S r9   r+   )Ú.0Úvs     r-   ú	<genexpr>z+PerceiverEncoder.forward.<locals>.<genexpr>  s0   è è € ð ð àØ�=ð à �=�=�=ðð r,   )r   r    r!   r"   )rÄ   rÅ   rC   Ú
num_blocksÚ	enumeraterÊ   r*   r   )rB   r    ro   rp   rq   rr   rÍ   rÎ   Úall_hidden_statesÚall_self_attentionsÚall_cross_attentionsÚlayer_outputsr…   ÚiÚlayer_modules                  r-   rJ   zPerceiverEncoder.forwardÔ  s¢  € ð #7Ð@˜B˜B¸DÐØ$5Ð?˜b˜b¸4ÐØ%6Ð@˜r˜r¸DÐð ×,Ò,ØØ)ØØ#Ø/ð -ñ 
ô 
ˆð & aÔ(ˆàð 	NØ#7¸=ÈÔ;KÐ:MÑ#MÐ õ �t”{Ô-Ñ.Ô.ð 	Ið 	IˆAÝ#,¨TÔ->Ñ#?Ô#?ð Tð T‘��<Ø'ð MØ(9¸]Ð<LÑ(LÐ%à , Ø!Ø#1Ø&7ð!ñ !ô !�ð !.¨aÔ 0�Ø$ð TØ*=ÀÈqÔAQÐ@SÑ*SÐ'øà#ð IØ$5¸Ð8HÑ$HÐ!øàð 	Ýð ð à'Ð):Ð<OÐQeÐfðñ ô ñ ô ð õ
 2Ø+Ø+Ø*Ø1ð	
ñ 
ô 
ð 	
r,   r9   )NNNFFT)r#   r$   r%   r&   r;   r'   r�   r(   rŽ   r*   r   rJ   rL   rM   s   @r-   r¾   r¾   ¢  sñ   ø€ € € € € ØGÐGð-Að -Að -Að -Að -Að -Aðd 48Ø+/Ø04Ø).Ø,1Ø#'ð9
ð 9
à”|ð9
ð Ô)¨DÑ0ð9
ð Ô! DÑ(ð	9
ð
 Ô&¨Ñ-ð9
ð   $™;ð9
ð # T™kð9
ð ˜D‘[ð9
ð 
Ð3Ñ	3ð9
ð 9
ð 9
ð 9
ð 9
ð 9
ð 9
ð 9
r,   r¾   c                   ó`   ‡ — e Zd ZU eed<   dZdZdZ ej	        ¦   «         ˆ fd„¦   «         Z
ˆ xZS )ÚPerceiverPreTrainedModelrC   Ú	perceiverrp   )Úimagec                 óä  •— t          ¦   «                              |¦  «         t          |d¦  «        r(t          j        |j        d| j        j        ¬¦  «         dS t          |d¦  «        r=t          |t          ¦  «        r(t          j        |j
        d| j        j        ¬¦  «         dS t          |t          j        ¦  «        r,|D ]+}t          j        ||         d| j        j        ¬¦  «         Œ*dS dS )zInitialize the weightsr@   ç        )ÚmeanÚstdÚposition_embeddingsN)r:   Ú_init_weightsÚhasattrÚinitÚnormal_r@   rC   Úinitializer_ranger§   Ú"PerceiverTrainablePositionEncodingrå   r
   ÚParameterDict)rB   ÚmoduleÚmodalityrD   s      €r-   ræ   z&PerceiverPreTrainedModel._init_weights  s  ø€ õ 	‰Œ×Ò˜fÑ%Ô%Ð%Ý�6˜9Ñ%Ô%ð 	\ÝŒL˜œ¨c°t´{Ô7TÐUÑUÔUÐUÐUÐUÝ�VÐ2Ñ3Ô3ð 	\½
À6ÕKmÑ8nÔ8nð 	\ÝŒL˜Ô3¸#À4Ä;ÔC`ÐaÑaÔaÐaÐaÐaÝ˜¥Ô 0Ñ1Ô1ð 	\Ø"ð \ð \�Ý”˜V HÔ-°C¸T¼[Ô=ZÐ[Ñ[Ô[Ð[Ð[ð	\ð 	\ð\ð \r,   )r#   r$   r%   r   r)   Úbase_model_prefixÚmain_input_nameÚinput_modalitiesr'   Úno_gradræ   rL   rM   s   @r-   rÞ   rÞ     sr   ø€ € € € € € àÐÐÑØ#ÐØ€OØ!Ðà€U„]�_„_ð	\ð 	\ð 	\ð 	\ñ „_ð	\ð 	\ð 	\ð 	\ð 	\r,   rÞ   a†  
    The Perceiver: a scalable, fully attentional architecture.

    <Tip>

        Note that it's possible to fine-tune Perceiver on higher resolution images than the ones it has been trained on, by
        setting `interpolate_pos_encoding` to `True` in the forward of the model. This will interpolate the pre-trained
        position embeddings to the higher resolution.

    </Tip>
    c                   óä   ‡ — e Zd Z	 	 	 dded         dedefˆ fd„Zd„ Zd„ Ze		 	 	 	 	 	 dd
e
j        de
j        dz  deee
j        f         dz  dedz  dedz  dededz  deez  fd„¦   «         Zˆ xZS )ÚPerceiverModelNÚdecoderÚPerceiverAbstractDecoderÚinput_preprocessorÚoutput_postprocessorc                 ó  •— t          ¦   «                              |¦  «         || _        || _        || _        t          |¦  «        | _        t          ||�|j        n|j	        ¬¦  «        | _
        || _        |                      ¦   «          dS )a®  
        decoder (`PerceiverDecoder`, *optional*):
            Decoder module that transforms latent representations into task predictions.
        input_preprocessor (`PreprocessorType`, *optional*):
            Preprocessor that encodes raw inputs into tensors for the model.
        output_postprocessor (`PostprocessorType`, *optional*):
            Postprocessor that transforms model outputs into final predictions.
        N)re   )r:   r;   rC   r÷   rø   r7   Ú
embeddingsr¾   Únum_channelsÚd_modelÚencoderrõ   Ú	post_init)rB   rC   rõ   r÷   rø   rD   s        €r-   r;   zPerceiverModel.__init__2  s’   ø€ õ 	‰Œ×Ò˜Ñ Ô Ð ØˆŒà"4ˆÔØ$8ˆÔ!Ý-¨fÑ5Ô5ˆŒÝ'ØÐ>PÐ>\Ð-Ô:Ð:ÐbhÔbpð
ñ 
ô 
ˆŒð ˆŒð 	�ŠÑÔÐÐÐr,   c                 ó   — | j         j        S r9   ©rú   r@   ©rB   s    r-   Úget_input_embeddingsz#PerceiverModel.get_input_embeddingsO  s   € ØŒÔ&Ð&r,   c                 ó   — || j         _        d S r9   r   )rB   r_   s     r-   Úset_input_embeddingsz#PerceiverModel.set_input_embeddingsR  s   € Ø"'ˆŒÔÐÐr,   Frp   ro   Úsubsampled_output_pointsrr   rÍ   Úinterpolate_pos_encodingrÎ   rs   c           	      ó„  — |�|n| j         j        }|�|n| j         j        }|�|n| j         j        }| j        �|                      ||¬¦  «        \  }}	}
ndd}	d}
|                     ¦   «         d         | j         j        k    r8t          d|                     ¦   «         d         › d| j         j        › d�¦  «        ‚|                     ¦   «         \  }}}|j        }|€t          j
        ||f|¬¦  «        }|                      |¬¦  «        }t          | j         ||¬	¦  «        }|                      |d|||||¬
¦  «        }|d         }d}| j        r³|�)|d         j        d         |d         j        d         ddœ}n|	}| j                             ||	|
|¬¦  «        }|                      ||||¬¦  «        }|j        }|r(|j        �!|r|j        |j        z   |_        n
||j        z   }| j        r|                      ||¬¦  «        }|s|�||f|dd…         z   S |f|dd…         z   S t)          |||j        |j        |j        ¬¦  «        S )a�  
        inputs (`torch.FloatTensor`):
            Inputs to the perceiver. Can be anything: images, text, audio, video, etc.
        subsampled_output_points (`dict[str, torch.Tensor]`, *optional*):
            Dictionary of tensors used as queries for the decoder. The decoder maps these queries to the latent
            representation of the model. Used for subsampled decoding, e.g. when only decoding certain image patches.

        Examples:

        ```python
        >>> from transformers import PerceiverConfig, PerceiverTokenizer, PerceiverImageProcessor, PerceiverModel
        >>> from transformers.models.perceiver.modeling_perceiver import (
        ...     PerceiverTextPreprocessor,
        ...     PerceiverImagePreprocessor,
        ...     PerceiverClassificationDecoder,
        ... )
        >>> import torch
        >>> import httpx
        >>> from io import BytesIO
        >>> from PIL import Image

        >>> # EXAMPLE 1: using the Perceiver to classify texts
        >>> # - we define a TextPreprocessor, which can be used to embed tokens
        >>> # - we define a ClassificationDecoder, which can be used to decode the
        >>> # final hidden states of the latents to classification logits
        >>> # using trainable position embeddings
        >>> config = PerceiverConfig()
        >>> preprocessor = PerceiverTextPreprocessor(config)
        >>> decoder = PerceiverClassificationDecoder(
        ...     config,
        ...     num_channels=config.d_latents,
        ...     trainable_position_encoding_kwargs=dict(num_channels=config.d_latents, index_dims=1),
        ...     use_query_residual=True,
        ... )
        >>> model = PerceiverModel(config, input_preprocessor=preprocessor, decoder=decoder)

        >>> # you can then do a forward pass as follows:
        >>> tokenizer = PerceiverTokenizer()
        >>> text = "hello world"
        >>> inputs = tokenizer(text, return_tensors="pt").input_ids

        >>> with torch.no_grad():
        ...     outputs = model(inputs=inputs)
        >>> logits = outputs.logits
        >>> list(logits.shape)
        [1, 2]

        >>> # to train, one can train the model using standard cross-entropy:
        >>> criterion = torch.nn.CrossEntropyLoss()

        >>> labels = torch.tensor([1])
        >>> loss = criterion(logits, labels)

        >>> # EXAMPLE 2: using the Perceiver to classify images
        >>> # - we define an ImagePreprocessor, which can be used to embed images
        >>> config = PerceiverConfig(image_size=224)
        >>> preprocessor = PerceiverImagePreprocessor(
        ...     config,
        ...     prep_type="conv1x1",
        ...     spatial_downsample=1,
        ...     out_channels=256,
        ...     position_encoding_type="trainable",
        ...     concat_or_add_pos="concat",
        ...     project_pos_dim=256,
        ...     trainable_position_encoding_kwargs=dict(
        ...         num_channels=256,
        ...         index_dims=config.image_size**2,
        ...     ),
        ... )

        >>> model = PerceiverModel(
        ...     config,
        ...     input_preprocessor=preprocessor,
        ...     decoder=PerceiverClassificationDecoder(
        ...         config,
        ...         num_channels=config.d_latents,
        ...         trainable_position_encoding_kwargs=dict(num_channels=config.d_latents, index_dims=1),
        ...         use_query_residual=True,
        ...     ),
        ... )

        >>> # you can then do a forward pass as follows:
        >>> image_processor = PerceiverImageProcessor()
        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> with httpx.stream("GET", url) as response:
        ...     image = Image.open(BytesIO(response.read()))
        >>> inputs = image_processor(image, return_tensors="pt").pixel_values

        >>> with torch.no_grad():
        ...     outputs = model(inputs=inputs)
        >>> logits = outputs.logits
        >>> list(logits.shape)
        [1, 2]

        >>> # to train, one can train the model using standard cross-entropy:
        >>> criterion = torch.nn.CrossEntropyLoss()

        >>> labels = torch.tensor([1])
        >>> loss = criterion(logits, labels)
        ```N)r  rH   zLast dimension of the inputs: z' doesn't correspond to config.d_model: z0. Make sure to set config.d_model appropriately.©Údevice)rE   )rC   Úinputs_embedsro   )ro   rp   rq   rr   rÍ   rÎ   r   Úaudiorà   r   ©r  rà   Úlabel©Úsubsampled_points)ÚzÚ
query_maskrr   )Úmodality_sizes)r   r   r    r!   r"   )rC   rr   rÍ   rÎ   r÷   rh   rü   rS   r	  r'   Úonesrú   r   rý   rõ   rz   Údecoder_queryr   r"   rø   r   r    r!   )rB   rp   ro   r  rr   rÍ   r  rÎ   Úkwargsr  Úinputs_without_posrE   Ú
seq_lengthr…   r	  Úembedding_outputÚencoder_outputsÚsequence_outputr   Úoutput_modality_sizesr  Údecoder_outputss                         r-   rJ   zPerceiverModel.forwardU  s"  € ð` 2CÐ1NÐ-Ð-ÐTXÔT_ÔTqÐà$8Ð$DÐ Ð È$Ì+ÔJjð 	ð &1Ð%<�k�kÀ$Ä+ÔBYˆàÔ"Ð.Ø9=×9PÒ9PØÐ1Ið :Qñ :ô :Ñ6ˆF�NÐ$6Ð$6ð "ˆNØ!%ÐØ�{Š{‰}Œ}˜RÔ  D¤KÔ$7Ò7Ð7Ý ð^°V·[²[±]´]À2Ô5Fð ^ð ^ØœÔ+ð^ð ^ð ^ñô ð ð
 %+§K¢K¡M¤MÑ!ˆ
�J Ø”ˆð Ð!Ý"œZ¨°ZÐ(@ÈÐPÑPÔPˆNàŸ?š?°j˜?ÑAÔAÐå2Ø”;Ø*Ø)ð
ñ 
ô 
ˆð Ÿ,š,ØØØØ&Ø/Ø!5Ø#ð 'ñ 
ô 
ˆð *¨!Ô,ˆàˆØŒ<ð 	aØ'Ð3à5°gÔ>ÔDÀQÔGØ5°gÔ>ÔDÀQÔGØð)ð )Ð%Ð%ð )7Ð%Ø œL×6Ò6Ø˜Ð(:ÐNfð 7ñ ô ˆMð #ŸlšlØØ!Ø)Ø"3ð	 +ñ ô ˆOð %Ô+ˆFð !ð Y _Ô%EÐ%QØð Yà'Ô8¸?Ô;[Ñ[ð $Ô4Ð4ð '6¸Ô8XÑ&X�OàÔ(ð aØ×2Ò2°6ÐJ_Ð2Ñ`Ô`�àð 	@ØÐ!Ø Ð0°?À1À2À2Ô3FÑFÐFà'Ð)¨O¸A¸B¸BÔ,?Ñ?Ð?å#ØØ-Ø)Ô7Ø&Ô1Ø,Ô=ð
ñ 
ô 
ð 	
r,   ©NNN)NNNNFN)r#   r$   r%   r	   ÚPreprocessorTypeÚPostprocessorTyper;   r  r  r   r'   r(   Údictr©   r�   rŽ   r*   r   rJ   rL   rM   s   @r-   rô   rô   $  sU  ø€ € € € € ð" 9=Ø/3Ø26ðð ð Ð4Ô5ðð -ð	ð
 0ðð ð ð ð ð ð:'ð 'ð 'ð(ð (ð (ð ð 48ØCGØ)-Ø,0Ø).Ø#'ðI
ð I
àÔ!ðI
ð Ô)¨DÑ0ðI
ð #' s¨E¬LÐ'8Ô"9¸DÑ"@ð	I
ð
   $™;ðI
ð # T™kðI
ð #'ðI
ð ˜D‘[ðI
ð 
Ð%Ñ	%ðI
ð I
ð I
ñ „^ðI
ð I
ð I
ð I
ð I
r,   rô   z@
    Example use of Perceiver for masked language modeling.
    c                   óÄ   ‡ — e Zd Zdefˆ fd„Ze	 	 	 	 	 	 	 ddej        dz  dej        dz  dedz  dedz  dej        dz  d	edz  d
ej        dz  de	e
z  fd„¦   «         Zˆ xZS )ÚPerceiverForMaskedLMrC   c                 óZ  •— t          ¦   «                              |¦  «         t          |¦  «        }|j        |j        dœ}t          ||t          ||j        |j        |j        d|j        ddd|¬¦
  «
        ¬¦  «        | _        t          |¦  «        | _
        |                      ¦   «          d S )N©rû   Ú
index_dimsé   é   F)	Úoutput_num_channelsÚoutput_index_dimsrû   rT   rU   rR   rž   Úfinal_projectÚ"trainable_position_encoding_kwargs©r÷   rõ   )r:   r;   ÚPerceiverTextPreprocessorrû   Úmax_position_embeddingsrô   ÚPerceiverBasicDecoderr?   rß   ÚPerceiverEmbeddingDecoderÚembedding_decoderrþ   )rB   rC   Útext_preprocessorÚ*trainable_position_encoding_kwargs_decoderrD   s       €r-   r;   zPerceiverForMaskedLM.__init__(  sÈ   ø€ Ý‰Œ×Ò˜Ñ Ô Ð å5°fÑ=Ô=Ðð .Ô:Ø Ô8ð6
ð 6
Ð2õ
 (ØØ0Ý)ØØ$*Ô$4Ø"(Ô"@Ø.Ô;Ø"Ø,Ô9ØØ#(Ø#Ø3]ðñ ô ð
ñ 
ô 
ˆŒõ  ";¸6Ñ!BÔ!BˆÔð 	�ŠÑÔÐÐÐr,   Nrp   ro   rr   rÍ   ÚlabelsrÎ   Ú	input_idsrs   c                 ó
  — |�|�t          d¦  «        ‚|€|�|}|�|n| j        j        }|                      |||||¬¦  «        }	|                      |r|	j        n|	d         | j        j        j        ¬¦  «        }
d}|�Kt          ¦   «         } ||
 	                    d| j        j
        ¦  «        | 	                    d¦  «        ¦  «        }|s|
f|	dd…         z   }|�|f|z   n|S t          ||
|	j        |	j        |	j        ¬¦  «        S )	až  
        inputs (`torch.FloatTensor`):
            Inputs to the perceiver. Can be anything: images, text, audio, video, etc.
        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]`

        Examples:

        ```python
        >>> from transformers import AutoTokenizer, PerceiverForMaskedLM
        >>> import torch

        >>> tokenizer = AutoTokenizer.from_pretrained("deepmind/language-perceiver")
        >>> model = PerceiverForMaskedLM.from_pretrained("deepmind/language-perceiver")

        >>> # training
        >>> text = "This is an incomplete sentence where some words are missing."
        >>> inputs = tokenizer(text, padding="max_length", return_tensors="pt")
        >>> # mask " missing."
        >>> inputs["input_ids"][0, 52:61] = tokenizer.mask_token_id
        >>> labels = tokenizer(text, padding="max_length", return_tensors="pt").input_ids

        >>> outputs = model(**inputs, labels=labels)
        >>> loss = outputs.loss
        >>> round(loss.item(), 2)
        19.87

        >>> logits = outputs.logits
        >>> list(logits.shape)
        [1, 2048, 262]

        >>> # inference
        >>> text = "This is an incomplete sentence where some words are missing."
        >>> encoding = tokenizer(text, padding="max_length", return_tensors="pt")

        >>> # mask bytes corresponding to " missing.". Note that the model performs much better if the masked span starts with a space.
        >>> encoding["input_ids"][0, 52:61] = tokenizer.mask_token_id

        >>> # forward pass
        >>> with torch.no_grad():
        ...     outputs = model(**encoding)
        >>> logits = outputs.logits
        >>> list(logits.shape)
        [1, 2048, 262]

        >>> masked_tokens_predictions = logits[0, 52:61].argmax(dim=-1).tolist()
        >>> tokenizer.decode(masked_tokens_predictions)
        ' missing.'
        ```Nú,You cannot use both `inputs` and `input_ids`©rp   ro   rr   rÍ   rÎ   r   )Úembedding_layerrH   rg   ©r2   r   r    r!   r"   )rS   rC   rÎ   rß   r1  r   r÷   rú   r   ri   Ú
vocab_sizer1   r    r!   r"   )rB   rp   ro   rr   rÍ   r4  rÎ   r5  r  r‹   r   Úmasked_lm_lossÚloss_fctr�   s                 r-   rJ   zPerceiverForMaskedLM.forwardG  sL  € ð~ Ð )Ð"7ÝÐKÑLÔLÐLØˆ^ 	Ð 5ØˆFà%0Ð%<�k�kÀ$Ä+ÔBYˆà—.’.ØØ)Ø/Ø!5Ø#ð !ñ 
ô 
ˆð ×'Ò'Ø)Ð9ˆGŒNˆN¨w°q¬zÈ4Ì>ÔKlÔKwð (ñ 
ô 
ˆð ˆØÐÝ'Ñ)Ô)ˆHØ%˜X f§k¢k°"°d´kÔ6LÑ&MÔ&MÈvÏ{Ê{Ð[]ÉÌÑ_Ô_ˆNàð 	ZØ�Y ¨¨¨¤Ñ,ˆFØ3AÐ3M�^Ð%¨Ñ.Ð.ÐSYÐYå&ØØØ!Ô/ØÔ)Ø$Ô5ð
ñ 
ô 
ð 	
r,   ©NNNNNNN)r#   r$   r%   r   r;   r   r'   r�   rŽ   r*   r1   rJ   rL   rM   s   @r-   r"  r"  "  s  ø€ € € € € ð˜ð ð ð ð ð ð ð> ð '+Ø.2Ø)-Ø,0Ø&*Ø#'Ø)-ð`
ð `
à”˜tÑ#ð`
ð œ tÑ+ð`
ð   $™;ð	`
ð
 # T™kð`
ð ”˜tÑ#ð`
ð ˜D‘[ð`
ð ”< $Ñ&ð`
ð 
Ð(Ñ	(ð`
ð `
ð `
ñ „^ð`
ð `
ð `
ð `
ð `
r,   r"  z;
    Example use of Perceiver for text classification.
    c                   ó¾   ‡ — e Zd Zˆ fd„Ze	 	 	 	 	 	 	 ddej        dz  dej        dz  dedz  dedz  dej        dz  dedz  d	ej        dz  d
ee	z  fd„¦   «         Z
ˆ xZS )Ú"PerceiverForSequenceClassificationc           
      ó  •— t          ¦   «                              |¦  «         |j        ddœ}|j        | _        t	          |t          |¦  «        t          ||j        |d¬¦  «        ¬¦  «        | _        |                      ¦   «          d S )Nr   r$  T©rû   r+  rž   r,  )	r:   r;   r?   Ú
num_labelsrô   r-  ÚPerceiverClassificationDecoderrß   rþ   )rB   rC   r3  rD   s      €r-   r;   z+PerceiverForSequenceClassification.__init__±  s”   ø€ Ý‰Œ×Ò˜Ñ Ô Ð àFLÔFVÐfgÐ5hÐ5hÐ2à Ô+ˆŒÝ'ØÝ8¸Ñ@Ô@Ý2ØØ#Ô-Ø3]Ø#'ð	ñ ô ð	
ñ 	
ô 	
ˆŒð 	�ŠÑÔÐÐÐr,   Nrp   ro   rr   rÍ   r4  rÎ   r5  rs   c                 óâ  — |�|�t          d¦  «        ‚|€|�|}|�|n| j        j        }|                      |||||¬¦  «        }	|r|	j        n|	d         }
d}|��Z| j        j        €f| j        dk    rd| j        _        nN| j        dk    r7|j        t          j	        k    s|j        t          j
        k    rd| j        _        nd| j        _        | j        j        dk    rWt          ¦   «         }| j        dk    r1 ||
                     ¦   «         |                     ¦   «         ¦  «        }nŽ ||
|¦  «        }n�| j        j        dk    rGt          ¦   «         } ||
                     d	| j        ¦  «        |                     d	¦  «        ¦  «        }n*| j        j        dk    rt          ¦   «         } ||
|¦  «        }|s|
f|	d
d…         z   }|�|f|z   n|S t!          ||
|	j        |	j        |	j        ¬¦  «        S )a   
        inputs (`torch.FloatTensor`):
            Inputs to the perceiver. Can be anything: images, text, audio, video, etc.
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the classification/regression loss. Indices should be in `[0, ..., config.num_labels -
            1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If `config.num_labels >
            1` a classification loss is computed (Cross-Entropy).

        Examples:

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

        >>> tokenizer = AutoTokenizer.from_pretrained("deepmind/language-perceiver")
        >>> model = PerceiverForSequenceClassification.from_pretrained("deepmind/language-perceiver")

        >>> text = "hello world"
        >>> inputs = tokenizer(text, return_tensors="pt").input_ids
        >>> outputs = model(inputs=inputs)
        >>> logits = outputs.logits
        >>> list(logits.shape)
        [1, 2]
        ```Nr7  r8  r   r   Ú
regressionÚsingle_label_classificationÚmulti_label_classificationrH   rg   r:  )rS   rC   rÎ   rß   r   Úproblem_typerC  Údtyper'   ÚlongrK   r   Úsqueezer   ri   r   r5   r    r!   r"   )rB   rp   ro   rr   rÍ   r4  rÎ   r5  r  r‹   r   r2   r=  r�   s                 r-   rJ   z*PerceiverForSequenceClassification.forwardÅ  s)  € ðF Ð )Ð"7ÝÐKÑLÔLÐLØˆ^ 	Ð 5ØˆFà%0Ð%<�k�kÀ$Ä+ÔBYˆà—.’.ØØ)Ø/Ø!5Ø#ð !ñ 
ô 
ˆð $/Ð>�”�°G¸A´JˆàˆØÑØŒ{Ô'Ð/Ø”? aÒ'Ð'Ø/;�D”KÔ,Ð,Ø”_ qÒ(Ð(¨f¬l½e¼jÒ.HÐ.HÈFÌLÕ\aÔ\eÒLeÐLeØ/L�D”KÔ,Ð,à/K�D”KÔ,àŒ{Ô'¨<Ò7Ð7Ý"™9œ9�Ø”? aÒ'Ð'Ø#˜8 F§N¢NÑ$4Ô$4°f·n²nÑ6FÔ6FÑGÔG�D�Dà#˜8 F¨FÑ3Ô3�D�DØ”Ô)Ð-JÒJÐJÝ+Ñ-Ô-�Ø�x §¢¨B°´Ñ @Ô @À&Ç+Â+ÈbÁ/Ä/ÑRÔR��Ø”Ô)Ð-IÒIÐIÝ,Ñ.Ô.�Ø�x ¨Ñ/Ô/�àð 	FØ�Y ¨¨¨¤Ñ,ˆFØ)-Ð)9�T�G˜fÑ$Ð$¸vÐEå(ØØØ!Ô/ØÔ)Ø$Ô5ð
ñ 
ô 
ð 	
r,   r>  ©r#   r$   r%   r;   r   r'   r�   rŽ   r*   r5   rJ   rL   rM   s   @r-   r@  r@  «  s  ø€ € € € € ðð ð ð ð ð( ð '+Ø.2Ø)-Ø,0Ø&*Ø#'Ø)-ðT
ð T
à”˜tÑ#ðT
ð œ tÑ+ðT
ð   $™;ð	T
ð
 # T™kðT
ð ”˜tÑ#ðT
ð ˜D‘[ðT
ð ”< $Ñ&ðT
ð 
Ð*Ñ	*ðT
ð T
ð T
ñ „^ðT
ð T
ð T
ð T
ð T
r,   r@  a¬  
        Example use of Perceiver for image classification, for tasks such as ImageNet.

    This model uses learned position embeddings. In other words, this model is not given any privileged information about
    the structure of images. As shown in the paper, this model can achieve a top-1 accuracy of 72.7 on ImageNet.

    [`PerceiverForImageClassificationLearned`] uses [`~models.perceiver.modeling_perceiver.PerceiverImagePreprocessor`]
    (with `prep_type="conv1x1"`) to preprocess the input images, and
    [`~models.perceiver.modeling_perceiver.PerceiverClassificationDecoder`] to decode the latent representation of
    [`PerceiverModel`] into classification logits.
    c                   óÄ   ‡ — e Zd Zˆ fd„Ze	 	 	 	 	 	 	 	 ddej        dz  dej        dz  dedz  dedz  dej        dz  d	ed
edz  dej        dz  dee	z  fd„¦   «         Z
ˆ xZS )Ú&PerceiverForImageClassificationLearnedc                 ó<  •— t          ¦   «                              |¦  «         d|j        dz  dœ}|j        ddœ}|j        | _        t          |t          |dddddd|¬¦  «        t          ||j        |d	¬
¦  «        ¬¦  «        | _        |  	                    ¦   «          d S )Nr&  rg   r$  r   Úconv1x1Ú	trainableÚconcat)Ú	prep_typeÚspatial_downsampleÚout_channelsÚposition_encoding_typeÚconcat_or_add_posÚproject_pos_dimr+  TrB  r,  )
r:   r;   Ú
image_sizer?   rC  rô   ÚPerceiverImagePreprocessorrD  rß   rþ   )rB   rC   Ú/trainable_position_encoding_kwargs_preprocessorr3  rD   s       €r-   r;   z/PerceiverForImageClassificationLearned.__init__+  sÍ   ø€ Ý‰Œ×Ò˜Ñ Ô Ð àKNÐ^dÔ^oÐqrÑ^rÐ:sÐ:sÐ7ØFLÔFVÐfgÐ5hÐ5hÐ2à Ô+ˆŒÝ'ØÝ9ØØ#Ø#$Ø Ø'2Ø"*Ø #Ø3bð	 ñ 	 ô 	 õ 3ØØ#Ô-Ø3]Ø#'ð	ñ ô ð
ñ 
ô 
ˆŒð* 	�ŠÑÔÐÐÐr,   NFrp   ro   rr   rÍ   r4  r  rÎ   Úpixel_valuesrs   c	                 óf  — |�|�t          d¦  «        ‚|€|�|}|�|n| j        j        }|                      ||||||¬¦  «        }
|r|
j        n|
d         }d}|�|                      ||| j        ¦  «        }|s|f|
dd…         z   }|�|f|z   n|S t          |||
j        |
j        |
j	        ¬¦  «        S )aQ  
        inputs (`torch.FloatTensor`):
            Inputs to the perceiver. Can be anything: images, text, audio, video, etc.
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, PerceiverForImageClassificationLearned
        >>> from PIL import Image
        >>> import httpx
        >>> from io import BytesIO

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> with httpx.stream("GET", url) as response:
        ...     image = Image.open(BytesIO(response.read()))

        >>> image_processor = AutoImageProcessor.from_pretrained("deepmind/vision-perceiver-learned")
        >>> model = PerceiverForImageClassificationLearned.from_pretrained("deepmind/vision-perceiver-learned")

        >>> inputs = image_processor(images=image, return_tensors="pt").pixel_values
        >>> outputs = model(inputs=inputs)
        >>> logits = outputs.logits
        >>> list(logits.shape)
        [1, 1000]

        >>> # model predicts one of the 1000 ImageNet classes
        >>> predicted_class_idx = logits.argmax(-1).item()
        >>> print("Predicted class:", model.config.id2label[predicted_class_idx])
        Predicted class: tabby, tabby cat
        ```Nú/You cannot use both `inputs` and `pixel_values`)rp   ro   rr   rÍ   r  rÎ   r   rg   r:  ©
rS   rC   rÎ   rß   r   Úloss_functionr5   r    r!   r"   )rB   rp   ro   rr   rÍ   r4  r  rÎ   r]  r  r‹   r   r2   r�   s                 r-   rJ   z.PerceiverForImageClassificationLearned.forwardI  s  € ð^ Ð ,Ð":ÝÐNÑOÔOÐOØˆ^ Ð 8Ø!ˆFà%0Ð%<�k�kÀ$Ä+ÔBYˆà—.’.ØØ)Ø/Ø!5Ø%=Ø#ð !ñ 
ô 
ˆð $/Ð>�”�°G¸A´JˆàˆØÐØ×%Ò% f¨f°d´kÑBÔBˆDàð 	FØ�Y ¨¨¨¤Ñ,ˆFØ)-Ð)9�T�G˜fÑ$Ð$¸vÐEå(ØØØ!Ô/ØÔ)Ø$Ô5ð
ñ 
ô 
ð 	
r,   )NNNNNFNNrM  rM   s   @r-   rO  rO    s  ø€ € € € € ðð ð ð ð ð< ð '+Ø.2Ø)-Ø,0Ø&*Ø).Ø#'Ø,0ðM
ð M
à”˜tÑ#ðM
ð œ tÑ+ðM
ð   $™;ð	M
ð
 # T™kðM
ð ”˜tÑ#ðM
ð #'ðM
ð ˜D‘[ðM
ð ”l TÑ)ðM
ð 
Ð*Ñ	*ðM
ð M
ð M
ñ „^ðM
ð M
ð M
ð M
ð M
r,   rO  a‘  
        Example use of Perceiver for image classification, for tasks such as ImageNet.

    This model uses fixed 2D Fourier position embeddings. As shown in the paper, this model can achieve a top-1 accuracy of
    79.0 on ImageNet, and 84.5 when pre-trained on a large-scale dataset (i.e. JFT).

    [`PerceiverForImageClassificationLearned`] uses [`~models.perceiver.modeling_perceiver.PerceiverImagePreprocessor`]
    (with `prep_type="pixels"`) to preprocess the input images, and
    [`~models.perceiver.modeling_perceiver.PerceiverClassificationDecoder`] to decode the latent representation of
    [`PerceiverModel`] into classification logits.
    c                   ó¾   ‡ — e Zd Zˆ fd„Ze	 	 	 	 	 	 	 ddej        dz  dej        dz  dedz  dedz  dej        dz  dedz  d	ej        dz  d
ee	z  fd„¦   «         Z
ˆ xZS )Ú&PerceiverForImageClassificationFourierc           
      ó(  •— t          ¦   «                              |¦  «         dddddœ}|j        ddœ}|j        | _        t	          |t          |dd|¬	¦  «        t          ||j        |d¬
¦  «        ¬¦  «        | _        |                      ¦   «          d S )NT©éà   rf  é@   F©Ú
concat_posÚmax_resolutionÚ	num_bandsÚ	sine_onlyr   r$  Úpixels)rT  rU  Ú fourier_position_encoding_kwargsrB  r,  ©	r:   r;   r?   rC  rô   r[  rD  rß   rþ   ©rB   rC   Ú-fourier_position_encoding_kwargs_preprocessorr3  rD   s       €r-   r;   z/PerceiverForImageClassificationFourier.__init__¨  sÈ   ø€ Ý‰Œ×Ò˜Ñ Ô Ð ð Ø(ØØð	9
ð 9
Ð5ð GMÔFVÐfgÐ5hÐ5hÐ2à Ô+ˆŒÝ'ØÝ9ØØ"Ø#$Ø1^ð	 ñ  ô  õ 3ØØ#Ô-Ø3]Ø#'ð	ñ ô ð
ñ 
ô 
ˆŒð" 	�ŠÑÔÐÐÐr,   Nrp   ro   rr   rÍ   r4  rÎ   r]  rs   c                 ód  — |�|�t          d¦  «        ‚|€|�|}|�|n| j        j        }|                      |||||¬¦  «        }	|r|	j        n|	d         }
d}|�|                      ||
| j        ¦  «        }|s|
f|	dd…         z   }|�|f|z   n|S t          ||
|	j        |	j        |	j	        ¬¦  «        S )aQ  
        inputs (`torch.FloatTensor`):
            Inputs to the perceiver. Can be anything: images, text, audio, video, etc.
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, PerceiverForImageClassificationFourier
        >>> from PIL import Image
        >>> import httpx
        >>> from io import BytesIO

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> with httpx.stream("GET", url) as response:
        ...     image = Image.open(BytesIO(response.read()))

        >>> image_processor = AutoImageProcessor.from_pretrained("deepmind/vision-perceiver-fourier")
        >>> model = PerceiverForImageClassificationFourier.from_pretrained("deepmind/vision-perceiver-fourier")

        >>> inputs = image_processor(images=image, return_tensors="pt").pixel_values
        >>> outputs = model(inputs=inputs)
        >>> logits = outputs.logits
        >>> list(logits.shape)
        [1, 1000]

        >>> # model predicts one of the 1000 ImageNet classes
        >>> predicted_class_idx = logits.argmax(-1).item()
        >>> print("Predicted class:", model.config.id2label[predicted_class_idx])
        Predicted class: tabby, tabby cat
        ```Nr_  r8  r   rg   r:  r`  ©rB   rp   ro   rr   rÍ   r4  rÎ   r]  r  r‹   r   r2   r�   s                r-   rJ   z.PerceiverForImageClassificationFourier.forwardÇ  ó  € ð\ Ð ,Ð":ÝÐNÑOÔOÐOØˆ^ Ð 8Ø!ˆFØ%0Ð%<�k�kÀ$Ä+ÔBYˆà—.’.ØØ)Ø/Ø!5Ø#ð !ñ 
ô 
ˆð $/Ð>�”�°G¸A´JˆàˆØÐØ×%Ò% f¨f°d´kÑBÔBˆDàð 	FØ�Y ¨¨¨¤Ñ,ˆFØ)-Ð)9�T�G˜fÑ$Ð$¸vÐEå(ØØØ!Ô/ØÔ)Ø$Ô5ð
ñ 
ô 
ð 	
r,   r>  rM  rM   s   @r-   rc  rc  š  s  ø€ € € € € ðð ð ð ð ð> ð '+Ø.2Ø)-Ø,0Ø&*Ø#'Ø,0ðJ
ð J
à”˜tÑ#ðJ
ð œ tÑ+ðJ
ð   $™;ð	J
ð
 # T™kðJ
ð ”˜tÑ#ðJ
ð ˜D‘[ðJ
ð ”l TÑ)ðJ
ð 
Ð*Ñ	*ðJ
ð J
ð J
ñ „^ðJ
ð J
ð J
ð J
ð J
r,   rc  aS  
        Example use of Perceiver for image classification, for tasks such as ImageNet.

    This model uses a 2D conv+maxpool preprocessing network. As shown in the paper, this model can achieve a top-1 accuracy
    of 82.1 on ImageNet.

    [`PerceiverForImageClassificationLearned`] uses [`~models.perceiver.modeling_perceiver.PerceiverImagePreprocessor`]
    (with `prep_type="conv"`) to preprocess the input images, and
    [`~models.perceiver.modeling_perceiver.PerceiverClassificationDecoder`] to decode the latent representation of
    [`PerceiverModel`] into classification logits.
    c                   ó¾   ‡ — e Zd Zˆ fd„Ze	 	 	 	 	 	 	 ddej        dz  dej        dz  dedz  dedz  dej        dz  dedz  d	ej        dz  d
ee	z  fd„¦   «         Z
ˆ xZS )Ú-PerceiverForImageClassificationConvProcessingc           
      ó*  •— t          ¦   «                              |¦  «         dddddœ}|j        ddœ}|j        | _        t	          |t          |ddd	|¬
¦  «        t          ||j        |d¬¦  «        ¬¦  «        | _        |                      ¦   «          d S )NT)é8   rx  rg  Frh  r   r$  ÚconvÚfourier)rT  rU  rW  rn  rB  r,  ro  rp  s       €r-   r;   z6PerceiverForImageClassificationConvProcessing.__init__#  sË   ø€ Ý‰Œ×Ò˜Ñ Ô Ð ð Ø&ØØð	9
ð 9
Ð5ð GMÔFVÐfgÐ5hÐ5hÐ2à Ô+ˆŒÝ'ØÝ9ØØ Ø#$Ø'0Ø1^ð ñ  ô  õ 3ØØ#Ô-Ø3]Ø#'ð	ñ ô ð
ñ 
ô 
ˆŒð$ 	�ŠÑÔÐÐÐr,   Nrp   ro   rr   rÍ   r4  rÎ   r]  rs   c                 ód  — |�|�t          d¦  «        ‚|€|�|}|�|n| j        j        }|                      |||||¬¦  «        }	|r|	j        n|	d         }
d}|�|                      ||
| j        ¦  «        }|s|
f|	dd…         z   }|�|f|z   n|S t          ||
|	j        |	j        |	j	        ¬¦  «        S )aY  
        inputs (`torch.FloatTensor`):
            Inputs to the perceiver. Can be anything: images, text, audio, video, etc.
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, PerceiverForImageClassificationConvProcessing
        >>> from PIL import Image
        >>> import httpx
        >>> from io import BytesIO

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> with httpx.stream("GET", url) as response:
        ...     image = Image.open(BytesIO(response.read()))

        >>> image_processor = AutoImageProcessor.from_pretrained("deepmind/vision-perceiver-conv")
        >>> model = PerceiverForImageClassificationConvProcessing.from_pretrained("deepmind/vision-perceiver-conv")

        >>> inputs = image_processor(images=image, return_tensors="pt").pixel_values
        >>> outputs = model(inputs=inputs)
        >>> logits = outputs.logits
        >>> list(logits.shape)
        [1, 1000]

        >>> # model predicts one of the 1000 ImageNet classes
        >>> predicted_class_idx = logits.argmax(-1).item()
        >>> print("Predicted class:", model.config.id2label[predicted_class_idx])
        Predicted class: tabby, tabby cat
        ```Nr_  r8  r   rg   r:  r`  rs  s                r-   rJ   z5PerceiverForImageClassificationConvProcessing.forwardC  rt  r,   r>  rM  rM   s   @r-   rv  rv    s  ø€ € € € € ðð ð ð ð ð@ ð '+Ø.2Ø)-Ø,0Ø&*Ø#'Ø,0ðJ
ð J
à”˜tÑ#ðJ
ð œ tÑ+ðJ
ð   $™;ð	J
ð
 # T™kðJ
ð ”˜tÑ#ðJ
ð ˜D‘[ðJ
ð ”l TÑ)ðJ
ð 
Ð*Ñ	*ðJ
ð J
ð J
ñ „^ðJ
ð J
ð J
ð J
ð J
r,   rv  a0  
        Example use of Perceiver for optical flow, for tasks such as Sintel and KITTI. [`PerceiverForOpticalFlow`] uses
    [`~models.perceiver.modeling_perceiver.PerceiverImagePreprocessor`] (with *prep_type="patches"*) to preprocess the
    input images, and [`~models.perceiver.modeling_perceiver.PerceiverOpticalFlowDecoder`] to decode the latent
    representation of [`PerceiverModel`].

    As input, one concatenates 2 subsequent frames along the channel dimension and extract a 3 x 3 patch around each pixel
    (leading to 3 x 3 x 3 x 2 = 54 values for each pixel). Fixed Fourier position encodings are used to encode the position
    of each pixel in the patch. Next, one applies the Perceiver encoder. To decode, one queries the latent representation
    using the same encoding used for the input.
    c                   ó¨   ‡ — e Zd Zˆ fd„Ze	 	 	 	 	 	 ddej        dz  dej        dz  dedz  dedz  dej        dz  dedz  d	ee	z  fd
„¦   «         Z
ˆ xZS )ÚPerceiverForOpticalFlowc                 ó<  •— t          ¦   «                              |¦  «         d|j        dddœ}d|j        dddœ}t          |ddddd	d
|¬¦  «        }t	          ||t          ||j        |j        ddd	d
|¬¦  «        ¬¦  «        | _        |                      ¦   «          d S )Nrg  FT©rk  rj  rl  ri  rh  Úpatchesr   é6   rg   rz  )rT  rU  Úconv_after_patchingÚconv_after_patching_in_channelsÚtemporal_downsamplerW  rn  ç      Y@)rû   Úoutput_image_shapeÚrescale_factorrž   r(  rW  rn  r,  )	r:   r;   Ú
train_sizer[  rô   ÚPerceiverOpticalFlowDecoderrû   rß   rþ   )rB   rC   rq  Ú(fourier_position_encoding_kwargs_decoderÚimage_preprocessorrD   s        €r-   r;   z PerceiverForOpticalFlow.__init__Ÿ  sî   ø€ Ý‰Œ×Ò˜Ñ Ô Ð ð Ø$Ô/ØØð	9
ð 9
Ð5ð Ø$Ô/ØØð	4
ð 4
Ð0õ 8ØØØ Ø $Ø,.Ø !Ø#,à-Zð

ñ 

ô 

Ðõ (ØØ1Ý/ØØ/Ô<Ø#)Ô#4Ø$à#(Ø$%ð (1Ø1Yðñ ô ð
ñ 
ô 
ˆŒð& 	�ŠÑÔÐÐÐr,   Nrp   ro   rr   rÍ   r4  rÎ   rs   c                 ó  — |�|n| j         j        }d}|�t          d¦  «        ‚|                      |||||¬¦  «        }	|r|	j        n|	d         }
|s|
f|	dd…         z   }|�|f|z   n|S t          ||
|	j        |	j        |	j        ¬¦  «        S )a7  
        inputs (`torch.FloatTensor`):
            Inputs to the perceiver. Can be anything: images, text, audio, video, etc.
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the optical flow loss. Indices should be in `[0, ..., config.num_labels - 1]`.

        Examples:

        ```python
        >>> from transformers import PerceiverForOpticalFlow
        >>> import torch

        >>> model = PerceiverForOpticalFlow.from_pretrained("deepmind/optical-flow-perceiver")

        >>> # in the Perceiver IO paper, the authors extract a 3 x 3 patch around each pixel,
        >>> # leading to 3 x 3 x 3 = 27 values for each pixel (as each pixel also has 3 color channels)
        >>> # patches have shape (batch_size, num_frames, num_channels, height, width)
        >>> # the authors train on resolutions of 368 x 496
        >>> patches = torch.randn(1, 2, 27, 368, 496)
        >>> outputs = model(inputs=patches)
        >>> logits = outputs.logits
        >>> list(logits.shape)
        [1, 368, 496, 2]
        ```Nz*Optical flow training is not yet supportedr8  r   rg   r:  ©	rC   rÎ   ÚNotImplementedErrorrß   r   r5   r    r!   r"   )rB   rp   ro   rr   rÍ   r4  rÎ   r  r2   r‹   r   r�   s               r-   rJ   zPerceiverForOpticalFlow.forwardÐ  sÒ   € ðF &1Ð%<�k�kÀ$Ä+ÔBYˆàˆØÐÝ%Ð&RÑSÔSÐSà—.’.ØØ)Ø/Ø!5Ø#ð !ñ 
ô 
ˆð $/Ð>�”�°G¸A´Jˆàð 	FØ�Y ¨¨¨¤Ñ,ˆFØ)-Ð)9�T�G˜fÑ$Ð$¸vÐEå(ØØØ!Ô/ØÔ)Ø$Ô5ð
ñ 
ô 
ð 	
r,   )NNNNNNrM  rM   s   @r-   r}  r}  ‘  sÝ   ø€ € € € € ð/ð /ð /ð /ð /ðb ð '+Ø.2Ø)-Ø,0Ø&*Ø#'ð;
ð ;
à”˜tÑ#ð;
ð œ tÑ+ð;
ð   $™;ð	;
ð
 # T™kð;
ð ”˜tÑ#ð;
ð ˜D‘[ð;
ð 
Ð*Ñ	*ð;
ð ;
ð ;
ñ „^ð;
ð ;
ð ;
ð ;
ð ;
r,   r}  a.  
        Example use of Perceiver for multimodal (video) autoencoding, for tasks such as Kinetics-700.

    [`PerceiverForMultimodalAutoencoding`] uses [`~models.perceiver.modeling_perceiver.PerceiverMultimodalPreprocessor`] to
    preprocess the 3 modalities: images, audio and class labels. This preprocessor uses modality-specific preprocessors to
    preprocess every modality separately, after which they are concatenated. Trainable position embeddings are used to pad
    each modality to the same number of channels to make concatenation along the time dimension possible. Next, one applies
    the Perceiver encoder.

    [`~models.perceiver.modeling_perceiver.PerceiverMultimodalDecoder`] is used to decode the latent representation of
    [`PerceiverModel`]. This decoder uses each modality-specific decoder to construct queries. The decoder queries are
    created based on the inputs after preprocessing. However, autoencoding an entire video in a single forward pass is
    computationally infeasible, hence one only uses parts of the decoder queries to do cross-attention with the latent
    representation. This is determined by the subsampled indices for each modality, which can be provided as additional
    input to the forward pass of [`PerceiverForMultimodalAutoencoding`].

    [`~models.perceiver.modeling_perceiver.PerceiverMultimodalDecoder`] also pads the decoder queries of the different
    modalities to the same number of channels, in order to concatenate them along the time dimension. Next, cross-attention
    is performed with the latent representation of [`PerceiverModel`].

    Finally, [`~models.perceiver.modeling_perceiver.PerceiverMultiModalPostprocessor`] is used to turn this tensor into an
    actual video. It first splits up the output into the different modalities, and then applies the respective
    postprocessor for each modality.

    Note that, by masking the classification label during evaluation (i.e. simply providing a tensor of zeros for the
    "label" modality), this auto-encoding model becomes a Kinetics 700 video classifier.
    c                   óÔ   ‡ — e Zd Zdefˆ fd„Ze	 	 	 	 	 	 	 ddej        dz  dej        dz  dee	ej        f         dz  de
dz  de
dz  d	ej        dz  d
e
dz  deez  fd„¦   «         Zˆ xZS )Ú"PerceiverForMultimodalAutoencodingrC   c                 óp  •— t          ¦   «                              |¦  «         |j        |j        z  }t	          dt          |dd|fdddœd|j        ¬¦  «        t          |dd	|j        |j        |j        fdddœddd
¬¦  «        t          |¦  «        dœddddœ¬¦  «        }t          |d|j        |j        dddd	|j        |j        |j        fdddœ¬¦  «        }t          |dt          |d||j        z  f|j        dddd|fdddœ¬¦  «        |t          |dddd|j        d
dœ¬¦  «        dœd |j        d¬¦  «        }t#          t%          ||j        ¬¦  «        t'          |j        d¬¦  «        t)          ||j        ¬¦  «        dœ¬¦  «        }t+          ||||¬¦  «        | _        |                      ¦   «          d S )Nr±   rz  éÀ   FTr  r€  )rW  rn  rT  Úsamples_per_patché    r   )rW  rn  rT  rU  r„  r  râ   ç      ð?)rà   r  r  )Úmin_padding_sizeÚ
modalitiesÚ
mask_probs)Úconcat_preprocessed_inputÚoutput_shaper(  rž   Úposition_encoding_onlyrW  rn  )r™  r)  r(  rž   r›  rW  rn  rR  r$  )r™  rž   r›  rW  r+  )r™  r—  Únum_outputsr(  rž   )Úin_channelsr   )r�  rV  )r—  )r÷   rõ   rø   )r:   r;   Ú
num_framesÚaudio_samples_per_frameÚPerceiverMultimodalPreprocessorÚPerceiverAudioPreprocessorr“  r[  rZ  ÚPerceiverOneHotPreprocessorÚ&PerceiverBasicVideoAutoencodingDecoderrš  r(  ÚPerceiverMultimodalDecoderr/  rD  Ú_label_trainable_num_channelsÚ PerceiverMultimodalPostprocessorÚPerceiverAudioPostprocessorÚ PerceiverProjectionPostprocessorÚ$PerceiverClassificationPostprocessorrô   rß   rþ   )rB   rC   Ún_audio_samplesr÷   Úimage_decoderrõ   rø   rD   s          €r-   r;   z+PerceiverForMultimodalAutoencoding.__init__-  s¡  ø€ Ý‰Œ×Ò˜Ñ Ô Ð à Ô+¨fÔ.LÑLˆå<Øå3ØØ+4à%(Ø+:Ð*<Ø%*Ø&*ð	6ð 6ð (Ø&,Ô&>ðñ ô õ 4ØØ+4à%'Ø+1Ô+<¸fÔ>OÐQWÔQbÐ*cØ%*Ø&*ð	6ð 6ð (Ø'(Ø()ðñ ô õ 5°VÑ<Ô<ð5ð ð8 "%¨s¸SÐAÐAð=
ñ 
ô 
ÐõB ?Øà&+ØÔ,Ø &Ô :Ø$Ø#'Ø#,àØ#)Ô#4°fÔ6GÈÔIZÐ"[Ø"Ø"ð	.ð .ð
ñ 
ô 
ˆõ" -Øà&+õ /Øà.3Ø'6¸&Ô:RÑ'RÐ&TØ(.Ô(BØ',Ø+/Ø+4à%(Ø+:Ð*<Ø%*Ø&*ð	6ð 6ðñ ô ð  'Ý7Øà.3Ø',Ø+/Ø+6à(.Ô(LØ&'ð8ð 8ðñ ô ð%ð ð> Ø &Ô :Ø$ðO(
ñ (
ô (
ˆõT  @å4°VÈÔIcÐdÑdÔdÝ9ÀfÔF`ÐopÐqÑqÔqÝ=¸fÐRXÔRlÐmÑmÔmðð ð 
ñ  
ô  
Ðõ (ØØ1ØØ!5ð	
ñ 
ô 
ˆŒð 	�ŠÑÔÐÐÐr,   Nrp   ro   r  rr   rÍ   r4  rÎ   rs   c                 ó  — |�|n| j         j        }d}	|�t          d¦  «        ‚|                      ||||||¬¦  «        }
|r|
j        n|
d         }|s|f|
dd…         z   }|	�|	f|z   n|S t          |	||
j        |
j        |
j        ¬¦  «        S )a+	  
        inputs (`torch.FloatTensor`):
            Inputs to the perceiver. Can be anything: images, text, audio, video, etc.
        subsampled_output_points (`dict[str, torch.Tensor]`, *optional*):
            Dictionary of tensors used as queries for the decoder. The decoder maps these queries to the latent
            representation of the model. Used for subsampled decoding, e.g. when only decoding certain image patches.
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).

        Examples:

        ```python
        >>> from transformers import PerceiverForMultimodalAutoencoding
        >>> import torch
        >>> import numpy as np

        >>> # create multimodal inputs
        >>> images = torch.randn((1, 16, 3, 224, 224))
        >>> audio = torch.randn((1, 30720, 1))
        >>> inputs = dict(image=images, audio=audio, label=torch.zeros((images.shape[0], 700)))

        >>> model = PerceiverForMultimodalAutoencoding.from_pretrained("deepmind/multimodal-perceiver")

        >>> # in the Perceiver IO paper, videos are auto-encoded in chunks
        >>> # each chunk subsamples different index dimensions of the image and audio modality decoder queries
        >>> nchunks = 128
        >>> image_chunk_size = np.prod((16, 224, 224)) // nchunks
        >>> audio_chunk_size = audio.shape[1] // model.config.samples_per_patch // nchunks
        >>> # process the first chunk
        >>> chunk_idx = 0
        >>> subsampling = {
        ...     "image": torch.arange(image_chunk_size * chunk_idx, image_chunk_size * (chunk_idx + 1)),
        ...     "audio": torch.arange(audio_chunk_size * chunk_idx, audio_chunk_size * (chunk_idx + 1)),
        ...     "label": None,
        ... }

        >>> outputs = model(inputs=inputs, subsampled_output_points=subsampling)
        >>> logits = outputs.logits
        >>> list(logits["audio"].shape)
        [1, 240]

        >>> list(logits["image"].shape)
        [1, 6272, 3]

        >>> list(logits["label"].shape)
        [1, 700]
        ```Nz5Multimodal autoencoding training is not yet supported)rp   ro   r  rr   rÍ   rÎ   r   rg   r:  r�  )rB   rp   ro   r  rr   rÍ   r4  rÎ   r  r2   r‹   r   r�   s                r-   rJ   z*PerceiverForMultimodalAutoencoding.forward   sÕ   € ðz &1Ð%<�k�kÀ$Ä+ÔBYˆàˆØÐÝ%Ð&]Ñ^Ô^Ð^à—.’.ØØ)Ø%=Ø/Ø!5Ø#ð !ñ 
ô 
ˆð $/Ð>�”�°G¸A´Jˆàð 	FØ�Y ¨¨¨¤Ñ,ˆFØ)-Ð)9�T�G˜fÑ$Ð$¸vÐEå(ØØØ!Ô/ØÔ)Ø$Ô5ð
ñ 
ô 
ð 	
r,   r>  )r#   r$   r%   r   r;   r   r'   r�   r   r©   rŽ   r*   r5   rJ   rL   rM   s   @r-   r�  r�    s  ø€ € € € € ð<q˜ð qð qð qð qð qð qðf ð '+Ø.2ØCGØ)-Ø,0Ø&*Ø#'ðV
ð V
à”˜tÑ#ðV
ð œ tÑ+ðV
ð #' s¨E¬LÐ'8Ô"9¸DÑ"@ð	V
ð
   $™;ðV
ð # T™kðV
ð ”˜tÑ#ðV
ð ˜D‘[ðV
ð 
Ð*Ñ	*ðV
ð V
ð V
ñ „^ðV
ð V
ð V
ð V
ð V
r,   r�  rH   c                 ó  — | dk    r|st          d¦  «        ‚t          di |¤Ž}n7| dk    r|st          d¦  «        ‚t          di |¤Ž}nt          d| › d�¦  «        ‚|dk    rt          j        ||¦  «        nt          j        ¦   «         }||fS )	zß
    Builds the position encoding.

    Args:
    - out_channels: refers to the number of channels of the position encodings.
    - project_pos_dim: if specified, will project the position encodings to this dimension.

    rR  z4Make sure to pass trainable_position_encoding_kwargsrz  z2Make sure to pass fourier_position_encoding_kwargsz Unknown position encoding type: ú.r   r+   )rS   rë   Ú PerceiverFourierPositionEncodingr
   r\   rZ   )rW  rV  rY  r+  rn  Úoutput_pos_encÚpositions_projections          r-   Úbuild_position_encodingr²  ý  sÍ   € ð   Ò,Ð,Ø1ð 	UÝÐSÑTÔTÐTÝ;ÐaÐaÐ>`ÐaÐaˆˆØ	 9Ò	,Ð	,à/ð 	SÝÐQÑRÔRÐRÝ9Ð]Ð]Ð<\Ð]Ð]ˆˆåÐUÐ<RÐUÐUÐUÑVÔVÐVð HWÐYZÒGZÐGZ�2œ9 \°?ÑCÔCÐCÕ`bÔ`kÑ`mÔ`mÐàÐ/Ð/Ð/r,   c                   ó†   — e Zd ZdZej        dd„¦   «         Zeej        d„ ¦   «         ¦   «         Zej        dd„¦   «         Z	dS )rö   zPerceiver abstract decoder.Nc                 ó   — t           ‚r9   ©rŽ  ©rB   rp   r  r  r  s        r-   r  z&PerceiverAbstractDecoder.decoder_query%  ó   € å!Ð!r,   c                 ó   — t           ‚r9   rµ  r  s    r-   Únum_query_channelsz+PerceiverAbstractDecoder.num_query_channels)  ó
   € õ "Ð!r,   c                 ó   — t           ‚r9   rµ  )rB   r]   r  r  s       r-   rJ   z PerceiverAbstractDecoder.forward.  r·  r,   r  r9   )
r#   r$   r%   r&   ÚabcÚabstractmethodr  Úpropertyr¹  rJ   r+   r,   r-   rö   rö   "  s‰   € € € € € Ø%Ð%àÔð"ð "ð "ñ Ôð"ð ØÔð"ð "ñ Ôñ „Xð"ð 	Ôð"ð "ð "ñ Ôð"ð "ð "r,   rö   )Ú	metaclassc            	       ót   ‡ — e Zd ZdZˆ fd„Zd
d„Z	 ddej        dej        dej        dz  dej        fd	„Z	ˆ xZ
S )ÚPerceiverProjectionDecoderz�
    Baseline projection decoder (no cross-attention).

    Args:
        config ([`PerceiverConfig`]):
            Model configuration.
    c                 ó�   •— t          ¦   «                              ¦   «          t          j        |j        |j        ¦  «        | _        d S r9   )r:   r;   r
   r\   r?   rC  Ú
classifierrA   s     €r-   r;   z#PerceiverProjectionDecoder.__init__<  s6   ø€ Ý‰Œ×ÒÑÔÐÝœ) FÔ$4°fÔ6GÑHÔHˆŒˆˆr,   Nc                 ó   — d S r9   r+   r¶  s        r-   r  z(PerceiverProjectionDecoder.decoder_query@  s   € Øˆtr,   r]   r  r  rs   c                 ó\   — t          j        |d¬¦  «        }|                      |¦  «        }|S )Nr   rv   )r'   rã   rÃ  )rB   r]   r  r  r   s        r-   rJ   z"PerceiverProjectionDecoder.forwardC  s.   € õ ŒJ�q˜aÐ Ñ Ô ˆà—’ Ñ#Ô#ˆØˆr,   r  r9   )r#   r$   r%   r&   r;   r  r'   r�   r(   rJ   rL   rM   s   @r-   rÁ  rÁ  3  s¬   ø€ € € € € ðð ðIð Ið Ið Ið Iðð ð ð ð aeðð Ø”\ðØ&+Ô&7ðØEJÔEVÐY]ÑE]ðà	Ô	ðð ð ð ð ð ð ð r,   rÁ  c                   ó2  ‡ — e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 dded	ed
edz  dedz  dedz  dedz  dedz  dedz  dedz  dedz  dedz  dedz  dedz  dedz  ddfˆ fd„Ze	defd„¦   «         Z
d d„Z	 	 d!dej        dej        dej        dz  dedz  def
d„Zˆ xZS )"r/  a†  
    Cross-attention-based decoder. This class can be used to decode the final hidden states of the latents using a
    cross-attention operation, in which the latents produce keys and values.

    The shape of the output of this class depends on how one defines the output queries (also called decoder queries).

    Args:
        config ([*PerceiverConfig*]):
            Model configuration.
        output_num_channels (`int`, *optional*):
            The number of channels in the output. Will only be used in case *final_project* is set to `True`.
        position_encoding_type (`str`, *optional*, defaults to "trainable"):
            The type of position encoding to use. Can be either "trainable", "fourier", or "none".
        output_index_dims (`int`, *optional*):
            The number of dimensions of the output queries. Ignored if 'position_encoding_type' == 'none'.
        num_channels (`int`, *optional*, defaults to 128):
            The number of channels of the decoder queries. Ignored if 'position_encoding_type' == 'none'.
        qk_channels (`int`, *optional*):
            The number of channels of the queries and keys in the cross-attention layer.
        v_channels (`int`, *optional*):
            The number of channels of the values in the cross-attention layer.
        num_heads (`int`, *optional*, defaults to 1):
            The number of attention heads in the cross-attention layer.
        widening_factor (`int`, *optional*, defaults to 1):
            The widening factor of the cross-attention layer.
        use_query_residual (`bool`, *optional*, defaults to `False`):
            Whether to use a residual connection between the query and the output of the cross-attention layer.
        concat_preprocessed_input (`bool`, *optional*, defaults to `False`):
            Whether to concatenate the preprocessed input to the query.
        final_project (`bool`, *optional*, defaults to `True`):
            Whether to project the output of the cross-attention layer to a target dimension.
        position_encoding_only (`bool`, *optional*, defaults to `False`):
            Whether to only use this class to define output queries.
    rR  Né€   r   FTrC   r(  rW  r)  rû   Úsubsampled_index_dimsrT   rU   rR   r­   rž   r™  r*  r›  rs   c                 óÒ  •— t          ¦   «                              ¦   «          || _        d | _        || _        || _        |dk    rt          dd|i|¤Ž\  | _        | _        || _        || _	        |€|}|| _
        || _        || _        || _        | j        sSt          |d|||	||j        |
|¬¦	  «	        | _        |rt#          j        ||¦  «        nt#          j        ¦   «         | _        d S d S )NÚnonerW  TrÀ   r+   )r:   r;   r(  Úoutput_position_encodingsrW  Úposition_encoding_kwargsr²  r±  r)  rû   rÈ  r™  r*  r›  r°   r?   Údecoding_cross_attentionr
   r\   rZ   Úfinal_layer)rB   rC   r(  rW  r)  rû   rÈ  rT   rU   rR   r­   rž   r™  r*  r›  rÌ  rD   s                   €r-   r;   zPerceiverBasicDecoder.__init__q  s<  ø€ õ& 	‰Œ×ÒÑÔÐà#6ˆÔ ð *.ˆÔ&Ø&<ˆÔ#Ø(@ˆÔ%Ø! VÒ+Ð+ÝH_ð Ið IØ'=ðIØAYðIð IÑEˆDÔ*¨DÔ,Eð "3ˆÔØ(ˆÔØ Ð(Ø$5Ð!Ø%:ˆÔ"Ø)BˆÔ&Ø*ˆÔØ&<ˆÔ#ð Ô*ð 	pÝ,:ØØ#'Ø'Ø%Ø#Ø"ØÔ'Ø /Ø#5ð
-ñ 
-ô 
-ˆDÔ)ð P]Ðo�rœy¨Ð7JÑKÔKÐKÕbdÔbmÑboÔboˆDÔÐÐð	pð 	pr,   c                 óÌ   — | j         dk    rt          d¦  «        ‚| j        r/d| j        v r| j        d         S | j                             ¦   «         S | j        r| j        S | j        S )NrÊ  z`You cannot calculate number of decoder query channels when position_encoding_type is set to nonerY  )	rW  rS   r›  rÌ  rË  Úoutput_sizer*  r(  rû   r  s    r-   r¹  z(PerceiverBasicDecoder.num_query_channelsª  s‚   € àÔ&¨&Ò0Ð0ÝØrñô ð ð Ô&ð 	@Ø  DÔ$AÐAÐAØÔ4Ð5FÔGÐGØÔ1×=Ò=Ñ?Ô?Ð?ØÔð 	,ØÔ+Ð+ØÔ Ð r,   c                 ó  — | j         dk    rt          d¦  «        ‚|��=t          j        || j        ¦  «        }t          j        |d¬¦  «        }|j        d         }dd|z  t          j        | j        |j        ¬¦  «        d d d …f         z  z   }t          j	        |d          ||j        d         |j        d         g¦  «        }| j         d	k    r|  
                    |¦  «        }n4| j         d
k    r)|  
                    | j        ||j        |j        |¬¦  «        }|                      |¦  «        }t          j        ||j        d         d|j        d         g¦  «        }n€|j        d         }|j        dd …         }	| j         d	k    r|  
                    |¦  «        }n.| j         d
k    r#|  
                    |	||j        |j        ¬¦  «        }|                      |¦  «        }| j        r)|€t          d¦  «        ‚t          j        ||gd¬¦  «        }|S )NrÊ  zOYou cannot construct decoder queries when position_encoding_type is set to noner   rv   r   rH   rg   r  rR  rz  )rE   r	  rJ  Úpos©r	  rJ  zMValue is required for inputs_without_pos if concat_preprocessed_input is True)rW  rS   r'   Úunravel_indexr)  Ústackrz   Útensorr	  Úbroadcast_torË  rJ  r±  Úreshaper™  Úcat)
rB   rp   r  r  r  ÚindicesrÒ  rE   Úpos_embr%  s
             r-   r  z#PerceiverBasicDecoder.decoder_query¸  s'  € ØÔ&¨&Ò0Ð0ÝÐnÑoÔoÐoØÑ(õ Ô)Ð*;¸TÔ=SÑTÔTˆGÝ”+˜g¨1Ð-Ñ-Ô-ˆCØœ aœˆJà�q˜3‘w¥¤¨dÔ.DÈSÌZÐ!XÑ!XÔ!XÐY]Ð_`Ð_`Ð_`ÐY`Ô!aÑaÑaˆCÝÔ$ S¨¤Y°¸S¼YÀq¼\È3Ì9ÐUVÌ<Ð0XÑYÔYˆCàÔ*¨kÒ9Ð9Ø×8Ò8¸ÑDÔD��ØÔ,°	Ò9Ð9Ø×8Ò8ØÔ*°zÈ&Ì-Ð_eÔ_kÐqtð 9ñ ô �ð
 ×/Ò/°Ñ8Ô8ˆGÝ”m G¨g¬m¸AÔ.>ÀÀGÄMÐRTÔDUÐ-VÑWÔWˆGˆGàœ aœˆJØœ a b bÔ)ˆJð Ô*¨kÒ9Ð9Ø×8Ò8¸ÑDÔD��ØÔ,°	Ò9Ð9Ø×8Ò8Ø 
°6´=ÈÌð 9ñ ô �ð
 ×/Ò/°Ñ8Ô8ˆGàÔ)ð 	GØ!Ð)Ý Ð!pÑqÔqÐqÝ”iÐ!3°WÐ =À2ÐFÑFÔFˆGàˆr,   r]   r  r  rr   c                 óº   — |rdnd }|                       |||d |¬¦  «        }|d         }|r||d         fz   }|                      |¦  «        }t          ||¬¦  «        S )Nr+   rÐ   r   r   ©r   r"   )rÍ  rÎ  r/   )	rB   r]   r  r  rr   r"   rÚ   r�   r   s	            r-   rJ   zPerceiverBasicDecoder.forwardè  s‹   € ð "3Ð<˜2˜2¸Ðà×5Ò5ØØ%ØØØ/ð 6ñ 
ô 
ˆð ˜qÔ!ˆàð 	FØ/°=ÀÔ3CÐ2EÑEÐà×!Ò! &Ñ)Ô)ˆå%¨VÐFVÐWÑWÔWÐWr,   )rR  NrÇ  NNNr   r   FFTFr  ©NF)r#   r$   r%   r&   r   rK   r©   rŽ   r;   r¾  r¹  r  r'   r�   r(   r/   rJ   rL   rM   s   @r-   r/  r/  M  sú  ø€ € € € € ð!ð !ðN .9à(,Ø#&Ø,0Ø"&Ø!%Ø !Ø&'Ø*/Ø16Ø%)Ø.3ð!7pð 7pàð7pð !ð7pð !$ d¡
ð	7pð  ™:ð7pð ˜D‘jð7pð  # T™zð7pð ˜4‘Zð7pð ˜$‘Jð7pð ˜‘:ð7pð ˜t™ð7pð ! 4™Kð7pð $(¨$¡;ð7pð ˜d‘{ð7pð  !% t¡ð!7pð$ 
ð%7pð 7pð 7pð 7pð 7pð 7pðr ð! Cð !ð !ð !ñ „Xð!ð.ð .ð .ð .ðh 04Ø).ðXð XàŒ|ðXð ÔðXð Ô%¨Ñ,ð	Xð
   $™;ðXð 
 ðXð Xð Xð Xð Xð Xð Xð Xr,   r/  c                   ó’   ‡ — e Zd ZdZˆ fd„Zedefd„¦   «         Zdd„Z	 	 dde	j
        d	e	j        d
e	j        dz  dedz  def
d„Zˆ xZS )rD  aŸ  
    Cross-attention based classification decoder. Light-weight wrapper of [`PerceiverBasicDecoder`] for logit output.
    Will turn the output of the Perceiver encoder which is of shape (batch_size, num_latents, d_latents) to a tensor of
    shape (batch_size, num_labels). The queries are of shape (batch_size, 1, num_labels).

    Args:
        config ([`PerceiverConfig`]):
            Model configuration.
    c                 ó”   •— t          ¦   «                              ¦   «          |j        | _        t          |f| j        ddœ|¤Ž| _        d S )Nr   )r(  r)  )r:   r;   rC  r/  rõ   )rB   rC   Údecoder_kwargsrD   s      €r-   r;   z'PerceiverClassificationDecoder.__init__  sW   ø€ Ý‰Œ×ÒÑÔÐà Ô+ˆŒÝ,Øð
à $¤Øð
ð 
ð ð	
ð 
ˆŒˆˆr,   rs   c                 ó   — | j         j        S r9   ©rõ   r¹  r  s    r-   r¹  z1PerceiverClassificationDecoder.num_query_channels  ó   € àŒ|Ô.Ð.r,   Nc                 ó>   — | j                              ||||¬¦  «        S )Nr  ©rõ   r  r¶  s        r-   r  z,PerceiverClassificationDecoder.decoder_query   s,   € ØŒ|×)Ò)Ø�NÐ$6ÐJ[ð *ñ 
ô 
ð 	
r,   Fr]   r  r  rr   c                 ó†   — |                       |||¬¦  «        }|j        d d …dd d …f         }t          ||j        ¬¦  «        S )N©rr   r   rÝ  )rõ   r   r/   r"   )rB   r]   r  r  rr   r  r   s          r-   rJ   z&PerceiverClassificationDecoder.forward%  sO   € ð Ÿ,š, u¨aÐCT˜,ÑUÔUˆð !Ô'¨¨¨¨1¨a¨a¨a¨Ô0ˆå%¨VÀoÔFfÐgÑgÔgÐgr,   r  rÞ  ©r#   r$   r%   r&   r;   r¾  rK   r¹  r  r'   r�   r(   rŽ   r/   rJ   rL   rM   s   @r-   rD  rD    sí   ø€ € € € € ðð ð	
ð 	
ð 	
ð 	
ð 	
ð ð/ Cð /ð /ð /ñ „Xð/ð
ð 
ð 
ð 
ð 04Ø).ðhð hàŒ|ðhð Ôðhð Ô%¨Ñ,ð	hð
   $™;ðhð 
 ðhð hð hð hð hð hð hð hr,   rD  c                   ó”   ‡ — e Zd ZdZdˆ fd„	Zedefd„¦   «         Zdd„Z	 	 dd
e	j
        de	j        de	j        dz  dedz  def
d„Zˆ xZS )r‰  z+Cross-attention based optical flow decoder.rg   r…  c                 óš   •— t          ¦   «                              ¦   «          || _        || _        || _        t          |fd|i|¤Ž| _        d S )Nr(  )r:   r;   r†  r(  r‡  r/  rõ   )rB   rC   r†  r(  r‡  rá  rD   s         €r-   r;   z$PerceiverOpticalFlowDecoder.__init__7  sS   ø€ Ý‰Œ×ÒÑÔÐà"4ˆÔØ#6ˆÔ Ø,ˆÔÝ,¨VÐoÐoÐI\ÐoÐ`nÐoÐoˆŒˆˆr,   rs   c                 ó   — | j         j        S r9   rã  r  s    r-   r¹  z.PerceiverOpticalFlowDecoder.num_query_channels?  rä  r,   Nc                 ó(   — |�t          d¦  «        ‚|S )Nz,FlowDecoder doesn't support subsampling yet.)rS   r¶  s        r-   r  z)PerceiverOpticalFlowDecoder.decoder_queryC  s   € ØÐ(ÝÐKÑLÔLÐLØˆr,   Fr]   r  r  rr   c                 ó
  — |                       |||¬¦  «        }|j        }|| j        z  }|                     |j        d         gt          | j        ¦  «        z   |j        d         gz   ¦  «        }t          ||j        ¬¦  «        S )Nrè  r   rH   rÝ  )	rõ   r   r‡  rØ  rz   Úlistr†  r/   r"   )rB   r]   r  r  rr   r  Úpredss          r-   rJ   z#PerceiverOpticalFlowDecoder.forwardH  sƒ   € ð Ÿ,š, u¨aÐCT˜,ÑUÔUˆØÔ&ˆà�Ô$Ñ$ˆØ—’˜uœ{¨1œ~Ð.µ°dÔ6MÑ1NÔ1NÑNÐRWÔR]Ð^`ÔRaÐQbÑbÑcÔcˆÝ%¨UÀ_ÔEeÐfÑfÔfÐfr,   )rg   r…  r  rÞ  ré  rM   s   @r-   r‰  r‰  4  sô   ø€ € € € € Ø5Ð5ðpð pð pð pð pð pð ð/ Cð /ð /ð /ñ „Xð/ðð ð ð ð 04Ø).ðgð gàŒ|ðgð Ôðgð Ô%¨Ñ,ð	gð
   $™;ðgð 
 ðgð gð gð gð gð gð gð gr,   r‰  c            	       ó¤   ‡ — e Zd ZdZdedee         deddfˆ fd„Ze	defd„¦   «         Z
dd	„Z	 dd
ej        dej        dej        dz  defd„Zˆ xZS )r£  aû  
    Cross-attention based video-autoencoding decoder. Light-weight wrapper of [*PerceiverBasicDecoder*] with video
    reshaping logic.

    Args:
        config ([*PerceiverConfig*]):
            Model configuration.
        output_shape (`list[int]`):
            Shape of the output as (batch_size, num_frames, height, width), excluding the channel dimension.
        position_encoding_type (`str`):
            The type of position encoding to use. Can be either "trainable", "fourier", or "none".
    rC   rš  rW  rs   Nc                 ó   •— t          ¦   «                              ¦   «          t          |¦  «        dk    rt          d|› d�¦  «        ‚|| _        |d         | _        t          |f| j        dd…         |dœ|¤Ž| _        d S )Nr±   z"Expected rank 4 output_shape, got r®  r(  r   )r)  rW  )r:   r;   ÚlenrS   rš  r(  r/  rõ   )rB   rC   rš  rW  rá  rD   s        €r-   r;   z/PerceiverBasicVideoAutoencodingDecoder.__init__e  sœ   ø€ õ 	‰Œ×ÒÑÔÐÝˆ|ÑÔ Ò!Ð!ÝÐQÀ,ÐQÐQÐQÑRÔRÐRà(ˆÔØ#1Ð2GÔ#HˆÔ å,Øð
à"Ô/°°!°Ô4Ø#9ð
ð 
ð ð	
ð 
ˆŒˆˆr,   c                 ó   — | j         j        S r9   rã  r  s    r-   r¹  z9PerceiverBasicVideoAutoencodingDecoder.num_query_channelsv  rä  r,   c                 ó>   — | j                              ||||¬¦  «        S )N)r  r  r  ræ  r¶  s        r-   r  z4PerceiverBasicVideoAutoencodingDecoder.decoder_queryz  s-   € ØŒ|×)Ò)ØØ)Ø1Ø/ð	 *ñ 
ô 
ð 	
r,   r]   r  r  c                 óº   — |                       ||¦  «        }|j        }t          j        || j        |j        d         gz   ¦  «        }t          ||j        ¬¦  «        S )NrH   rÝ  )rõ   r   r'   rØ  rš  rz   r/   r"   )rB   r]   r  r  r  r   s         r-   rJ   z.PerceiverBasicVideoAutoencodingDecoder.forward‚  sX   € ð Ÿ,š, u¨aÑ0Ô0ˆØ Ô'ˆå”˜v tÔ'8¸F¼LÈÔ<LÐ;MÑ'MÑNÔNˆÝ%¨VÀoÔFfÐgÑgÔgÐgr,   r  r9   )r#   r$   r%   r&   r   rï  rK   r©   r;   r¾  r¹  r  r'   r�   r(   r/   rJ   rL   rM   s   @r-   r£  r£  W  s  ø€ € € € € ðð ð
Ø%ð
Ø59¸#´Yð
ØX[ð
à	ð
ð 
ð 
ð 
ð 
ð 
ð" ð/ Cð /ð /ð /ñ „Xð/ð
ð 
ð 
ð 
ð aeðhð hØ”\ðhØ&+Ô&7ðhØEJÔEVÐY]ÑE]ðhà	ðhð hð hð hð hð hð hð hr,   r£  r  rp   rs   c                 óœ   — i }d}t          |                      ¦   «         ¦  «        D ]%}| |         }|dd…|||z   …f         }||z  }|||<   Œ&|S )a  
    Partitions a [B, N, C] tensor into tensors for each modality.

    Args:
        modality_sizes
            dict specifying the size of the modality
        inputs:
            input tensor

    Returns:
        dict mapping name of modality to its associated tensor.
    r   N)Úsortedr€   )r  rp   r‹   Úindexrî   rh   Úinps          r-   Úrestructurerû  Œ  sr   € ð €GØ€Eå˜>×.Ò.Ñ0Ô0Ñ1Ô1ð  ð  ˆØ˜hÔ'ˆØ�Q�Q�Q˜ ¨¡Ð,Ð,Ô-ˆØ�‰ˆØˆ�ÑÐØ€Nr,   c                   óì   ‡ — e Zd ZdZ	 	 ddedeeef         dedededz  d	eeef         dz  d
dfˆ fd„Z	e
d
efd„¦   «         Zdd„Z	 	 ddej        dej        dej        dz  dedz  d
ej        f
d„Zˆ xZS )r¤  a1  
    Multimodal decoding by composing uni-modal decoders. The *modalities* argument of the constructor is a dictionary
    mapping modality name to the decoder of that modality. That decoder will be used to construct queries for that
    modality. Modality-specific queries are padded with trainable modality-specific parameters, after which they are
    concatenated along the time dimension.

    Next, there is a shared cross attention operation across all modalities.

    Args:
        config ([*PerceiverConfig*]):
            Model configuration.
        modalities (`dict[str, PerceiverAbstractDecoder]`):
            Dictionary mapping modality name to the decoder of that modality.
        num_outputs (`int`):
            The number of outputs of the decoder.
        output_num_channels (`int`):
            The number of channels in the output.
        min_padding_size (`int`, *optional*, defaults to 2):
            The minimum padding size for all modalities. The final output will have num_channels equal to the maximum
            channels across all modalities plus min_padding_size.
        subsampled_index_dims (`dict[str, PerceiverAbstractDecoder]`, *optional*):
            Dictionary mapping modality name to the subsampled index dimensions to use for the decoder query of that
            modality.
    rg   NrC   r—  rœ  r(  r–  rÈ  rs   c                 ó\  •‡ — t          ¦   «                              ¦   «          t          j        |¦  «        ‰ _        |‰ _        |‰ _        |‰ _        |‰ _        t          |f|f|d‰ j
        dœ|¤Ž‰ _        t          j        ˆ fd„|                     ¦   «         D ¦   «         ¦  «        ‰ _        d S )NrÊ  )r)  r(  rW  rû   c           
      ó€   •— i | ]:\  }}|t          j        t          j        d ‰j        |j        z
  ¦  «        ¦  «        “Œ;S ©r   )r
   r<   r'   r=   r¹  )rÒ   rî   rõ   rB   s      €r-   ú
<dictcomp>z7PerceiverMultimodalDecoder.__init__.<locals>.<dictcomp>×  sQ   ø€ ð ð ð á%�H˜gð �"œ,¥u¤{°1°dÔ6MÐPWÔPjÑ6jÑ'kÔ'kÑlÔlðð ð r,   )r:   r;   r
   Ú
ModuleDictr—  rÈ  r–  r(  rœ  r/  r¹  rõ   rì   ÚitemsÚpadding)	rB   rC   r—  rœ  r(  r–  rÈ  rá  rD   s	   `       €r-   r;   z#PerceiverMultimodalDecoder.__init__¾  sÒ   øø€ õ 	‰Œ×ÒÑÔÐÝœ-¨
Ñ3Ô3ˆŒØ%:ˆÔ"Ø 0ˆÔØ#6ˆÔ Ø&ˆÔÝ,Øð
à*˜nØ 3Ø#)ØÔ0ð
ð 
ð ð
ð 
ˆŒõ Ô'ðð ð ð à)3×)9Ò)9Ñ);Ô);ðñ ô ñ
ô 
ˆŒˆˆr,   c                 óz   — t          d„ | j                             ¦   «         D ¦   «         ¦  «        }|| j        z   }|S )Nc              3   ó*   K  — | ]\  }}|j         V — Œd S r9   )r¹  )rÒ   r…   rõ   s      r-   rÔ   z@PerceiverMultimodalDecoder.num_query_channels.<locals>.<genexpr>ß  s)   è è € ÐdÐd¹j¸aÀ˜wÔ9ÐdÐdÐdÐdÐdÐdr,   ©Úmaxr—  r  r–  ©rB   Úmax_channel_sizeÚcommon_channel_sizes      r-   r¹  z-PerceiverMultimodalDecoder.num_query_channelsÝ  sA   € åÐdÐdÈDÌO×LaÒLaÑLcÔLcÐdÑdÔdÑdÔdÐØ.°Ô1FÑFÐØ"Ð"r,   c           
      ó®  ‡ ‡	‡
— t          ||¦  «        }|pi }i Š	‰ j                             ¦   «         D ]W\  }}d }|�|                     |d ¦  «        }|                     ||         d ||                     |d ¦  «        ¬¦  «        }|‰	|<   ŒXˆ fd„Š
t          j        ˆ	ˆ
fd„t          ‰ j                             ¦   «         ¦  «        D ¦   «         d¬¦  «        S )N)rp   r  r  r  c           	      ój  •— t          j        ||j        d         t          j        |j        dd…         ¦  «        |j        d         g¦  «        }‰j        |          }t          j        ||j        d         |j        d         ‰j        |j        d         z
  g¦  «        }t          j        ||gd¬¦  «        S )Nr   r   rH   rg   rv   )	r'   rØ  rz   ÚnpÚprodr  r×  r¹  rÙ  )rî   rk   rÒ  rB   s      €r-   Úembedz7PerceiverMultimodalDecoder.decoder_query.<locals>.embedú  s—   ø€ Ý”˜a !¤'¨!¤*­b¬g°a´g¸aÀ¸d´mÑ.DÔ.DÀaÄgÈbÄkÐ!RÑSÔSˆAØ”,˜xÔ(ˆCÝÔ$ S¨1¬7°1¬:°q´w¸q´zÀ4ÔCZÐ]^Ô]dÐefÔ]gÑCgÐ*hÑiÔiˆCÝ”9˜a ˜X¨1Ð-Ñ-Ô-Ð-r,   c                 ó4   •— g | ]} ‰|‰|         ¦  «        ‘ŒS r+   r+   )rÒ   rî   Údecoder_queriesr  s     €€r-   ú
<listcomp>z<PerceiverMultimodalDecoder.decoder_query.<locals>.<listcomp>	  s*   ø€ ÐgÐgÐg¸HˆUˆU�8˜_¨XÔ6Ñ7Ô7ÐgÐgÐgr,   r   rv   )	rû  r—  r  Úgetr  r'   rÙ  rø  r€   )rB   rp   r  r  r  rî   rõ   Úinput_without_posr]   r  r  s   `        @@r-   r  z(PerceiverMultimodalDecoder.decoder_queryã  s  øøø€ å˜^¨VÑ4Ô4ˆð .Ð3°ÐàˆØ!%¤×!6Ò!6Ñ!8Ô!8ð 	.ð 	.ÑˆH�gà $ÐØ!Ð-Ø$6×$:Ò$:¸8ÀTÑ$JÔ$JÐ!Ø×)Ò)Ø˜hÔ'Ø#Ø#4Ø"3×"7Ò"7¸À$Ñ"GÔ"Gð	 *ñ ô ˆEð ).ˆO˜HÑ%Ð%ð	.ð 	.ð 	.ð 	.ð 	.õ ŒyØgÐgÐgÐgÐgÍÈtÌ×OcÒOcÑOeÔOeÑHfÔHfÐgÑgÔgÐmnð
ñ 
ô 
ð 	
r,   Fr]   r  r  rr   c                 ó6   — |                       |||¬¦  «        }|S )Nrè  )rõ   )rB   r]   r  r  rr   r  s         r-   rJ   z"PerceiverMultimodalDecoder.forward	  s"   € ð Ÿ,š, u¨aÐCT˜,ÑUÔUˆàÐr,   )rg   N©NNrÞ  )r#   r$   r%   r&   r   r   r©   rö   rK   r;   r¾  r¹  r  r'   r�   r(   rŽ   rJ   rL   rM   s   @r-   r¤  r¤  ¤  sW  ø€ € € € € ðð ð> ()ØLPð
ð 
àð
ð ˜Ð6Ð6Ô7ð
ð ð	
ð
 !ð
ð  ™*ð
ð  $ CÐ)AÐ$AÔBÀTÑIð
ð 
ð
ð 
ð 
ð 
ð 
ð 
ð> ð# Cð #ð #ð #ñ „Xð#ð
 
ð  
ð  
ð  
ðL 04Ø).ð
ð 
àŒ|ð
ð Ôð
ð Ô%¨Ñ,ð	
ð
   $™;ð
ð 
Œð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
r,   r¤  ÚframesÚtemporal_block_sizeÚspatial_block_sizec           
      ó–  — t          | j        ¦  «        dk    r~| j        \  }}}}|                      ||||z  |||z  |¦  «        } |                      dddddd¦  «                             ¦   «         } |                      |||z  ||z  |dz  |z  ¦  «        } | S t          | j        ¦  «        dk    r�| j        \  }}}}}|                      |||z  ||||z  |||z  |¦  «        } |                      dddddddd¦  «                             ¦   «         } |                      |||z  ||z  ||z  ||dz  z  |z  ¦  «        } | S t          d	¦  «        ‚)
z¾
    Space to depth transform. Rearranges blocks of spatial data, into depth.

    This function assumes the channels to be first, but will place the channels last after transformation.
    r±   r   rg   r   é   r   é   é   zlFrames should be of rank 4 (batch, channels, height, width) or rank 5 (batch, time, channels, height, width))ró  rz   ri   rj   r~   rS   )r  r  r  rE   rû   ÚheightÚwidthÚtimes           r-   Úspace_to_depthr!  	  s³  € õ ˆ6Œ<ÑÔ˜AÒÐØ28´,Ñ/ˆ
�L &¨%à—’ØØØÐ(Ñ(ØØÐ'Ñ'Øñ
ô 
ˆð —’  1 a¨¨A¨qÑ1Ô1×<Ò<Ñ>Ô>ˆà—’ØØÐ(Ñ(ØÐ'Ñ'Ø Ñ" lÑ2ñ	
ô 
ˆð ˆÝ	ˆVŒ\Ñ	Ô	˜aÒ	Ð	Ø8>¼Ñ5ˆ
�D˜,¨°à—’ØØÐ'Ñ'ØØØÐ(Ñ(ØØÐ'Ñ'Øñ	
ô 	
ˆð —’  1 a¨¨A¨q°!°QÑ7Ô7×BÒBÑDÔDˆà—’ØØÐ'Ñ'ØÐ(Ñ(ØÐ'Ñ'ØÐ#5°qÑ#8Ñ9¸LÑHñ
ô 
ˆð ˆåð@ñ
ô 
ð 	
r,   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚConv2dSamePaddingz…
    Conv2d layer with padding="same" support. Source:
    https://gist.github.com/sumanmichael/4de9dee93f972d47c80c4ade8e149ea6
    c           
      óÂ   •—  t          ¦   «         j        |i |¤Ž t          j        t	          t
          d„ | j        d d d…         D ¦   «         ¦  «        ¦  «        | _        d S )Nc                 ó@   — g | ]}|d z  |d |d z  z  z
  z   dz
  |d z  f‘ŒS )rg   r   r+   )rÒ   Úks     r-   r  z.Conv2dSamePadding.__init__.<locals>.<listcomp>V	  s<   € ÐgÐgÐgÈ1˜a 1™f¨¨A°°a±©LÑ(8Ñ9¸AÑ=¸qÀA¹vÐFÐgÐgÐgr,   rH   )r:   r;   r
   Ú	ZeroPad2dr   r   Úkernel_sizeÚzero_pad_2d)rB   Úargsr  rD   s      €r-   r;   zConv2dSamePadding.__init__S	  sh   ø€ Ø�‰ŒÔ˜$Ð) &Ð)Ð)Ð)Ýœ<Ý•7ÐgÐgÐPTÔP`ÐaeÐaeÐceÐaeÔPfÐgÑgÔgÑhÔhñ
ô 
ˆÔÐÐr,   c                 ój   — |                       |                      |¦  «        | j        | j        ¦  «        S r9   )Ú_conv_forwardr)  ÚweightÚbias)rB   Úinputs     r-   rJ   zConv2dSamePadding.forwardY	  s,   € Ø×!Ò! $×"2Ò"2°5Ñ"9Ô"9¸4¼;ÈÌ	ÑRÔRÐRr,   )r#   r$   r%   r&   r;   rJ   rL   rM   s   @r-   r#  r#  M	  sX   ø€ € € € € ðð ð

ð 
ð 
ð 
ð 
ðSð Sð Sð Sð Sð Sð Sr,   r#  c            	       ób   ‡ — e Zd ZdZ	 	 	 	 ddededed	efˆ fd
„Zdej        dej        fd„Z	ˆ xZ
S )ÚConv2DDownsamplezBDownsamples 4x by applying a 2D convolution and doing max pooling.r   r   rg  TÚ
num_layersr�  rV  Úuse_batchnormc                 ó>  •— t          ¦   «                              ¦   «          t          ||ddd¬¦  «        | _        |rt	          j        |¬¦  «        nt	          j        ¦   «         | _        t	          j        ¦   «         | _	        t	          j
        dd¬¦  «        | _        dS )	a}  
        Constructs a Conv2DDownsample model.

        Args:
          in_channels (`int`, *optional*, defaults to 3):
            The number of input channels.
          out_channels (`int`, *optional*, defaults to 64):
            The number of conv output channels.
          use_batchnorm (`bool`, *optional*, defaults to `True`):
            Whether to use batchnorm.
        r  rg   F)r�  rV  r(  Ústrider.  )Únum_featuresr   )r(  r5  N)r:   r;   r#  ry  r
   ÚBatchNorm2drZ   Ú	batchnormÚReLUÚreluÚ	MaxPool2dÚmax_pool)rB   r2  r�  rV  r3  rD   s        €r-   r;   zConv2DDownsample.__init__`	  s‘   ø€ õ$ 	‰Œ×ÒÑÔÐå%Ø#°,ÈAÐVWÐ^cð
ñ 
ô 
ˆŒ	ð GTÐf�œ°\ÐBÑBÔBÐBÕY[ÔYdÑYfÔYfˆŒÝ”G‘I”IˆŒ	Ýœ°¸1Ð=Ñ=Ô=ˆŒˆˆr,   rp   rs   c                 ó®   — |                       |¦  «        }|                      |¦  «        }|                      |¦  «        }|                      |¦  «        }|S r9   )ry  r8  r:  r<  )rB   rp   Úouts      r-   rJ   zConv2DDownsample.forward{	  sI   € Ø�iŠi˜ÑÔˆØ�nŠn˜SÑ!Ô!ˆØ�iŠi˜‰nŒnˆØ�mŠm˜CÑ Ô ˆØˆ
r,   )r   r   rg  T)r#   r$   r%   r&   rK   rŽ   r;   r'   r�   rJ   rL   rM   s   @r-   r1  r1  ]	  s¢   ø€ € € € € ØLÐLð ØØØ"ð>ð >àð>ð ð>ð ð	>ð
 ð>ð >ð >ð >ð >ð >ð6˜eœlð ¨u¬|ð ð ð ð ð ð ð ð r,   r1  re  TFc           	      ó„  ‡ ‡‡— ‰ j         d         }dŠt          j        ˆˆˆ fd„|D ¦   «         ¦  «        }‰ ddd…dd…f         dd…dd…df         |ddd…dd…f         z  }t          j        |dt	          j        |j         dd…         ¦  «        g¦  «        }|r"t          j        t          j        |z  ¦  «        }nVt          j        t          j        t          j        |z  ¦  «        t          j	        t          j        |z  ¦  «        gd¬¦  «        }|r-t          j        ‰ | 
                    |dd¦  «        gd¬¦  «        }|S )a  
    Generate a Fourier frequency position encoding with linear spacing.

    Args:
      pos (`torch.LongTensor` of shape `(batch_size, sequence_length, dim)`):
        The Tensor containing the position of n points in d dimensional space.
      num_bands (`int`):
        The number of frequency bands (K) to use.
      max_resolution (`tuple[int]`, *optional*, defaults to (224, 224)):
        The maximum resolution (i.e. the number of pixels per dim). A tuple representing resolution for each dimension.
      concat_pos (`bool`, *optional*, defaults to `True`):
        Whether to concatenate the input position encoding to the Fourier features.
      sine_only (`bool`, *optional*, defaults to `False`):
        Whether to use a single phase (sin) or two (sin/cos) for each frequency band.

    Returns:
      `torch.FloatTensor` of shape `(batch_size, sequence_length, n_channels)`: The Fourier position embeddings. If
      `concat_pos` is `True` and `sine_only` is `False`, output dimensions are ordered as: [dim_1, dim_2, ..., dim_d,
      sin(pi*f_1*dim_1), ..., sin(pi*f_K*dim_1), ..., sin(pi*f_1*dim_d), ..., sin(pi*f_K*dim_d), cos(pi*f_1*dim_1),
      ..., cos(pi*f_K*dim_1), ..., cos(pi*f_1*dim_d), ..., cos(pi*f_K*dim_d)], where dim_i is pos[:, i] and f_k is the
      kth frequency band.
    r   r•  c                 óP   •— g | ]"}t          j        ‰|d z  ‰‰j        ¬¦  «        ‘Œ#S )rg   )ÚstartÚendÚstepsr	  )r'   Úlinspacer	  )rÒ   ÚresÚmin_freqrk  rÒ  s     €€€r-   r  z-generate_fourier_features.<locals>.<listcomp> 	  s3   ø€ ÐsÐsÐsÐ]`�Œ˜h¨C°!©G¸9ÈSÌZÐ	XÑ	XÔ	XÐsÐsÐsr,   NrH   r   rv   )rz   r'   rÕ  rØ  r  r  ÚsinÚpirÙ  ÚcosrI   )	rÒ  rk  rj  ri  rl  rE   Ú
freq_bandsÚper_pos_featuresrF  s	   ``      @r-   Úgenerate_fourier_featuresrL  ƒ	  sh  øøø€ ð0 ”˜1”€Jà€Hå”ØsÐsÐsÐsÐsÐsÐdrÐsÑsÔsñô €Jð ˜1˜a˜a˜a   ˜7”| A A A q q q¨$ JÔ/°*¸TÀ1À1À1ÀaÀaÀa¸ZÔ2HÑHÐÝ”}Ð%5¸½B¼GÐDTÔDZÐ[\Ð[]Ð[]ÔD^Ñ<_Ô<_Ð7`ÑaÔaÐàð 
å œ9¥R¤UÐ.>Ñ%?Ñ@Ô@ÐÐõ !œ9ÝŒY•r”uÐ/Ñ/Ñ0Ô0µ%´)½B¼EÐDTÑ<TÑ2UÔ2UÐVÐ\^ð
ñ 
ô 
Ðð ð aå œ9 cÐ+;×+BÒ+BÀ:ÈrÐSUÑ+VÔ+VÐ%WÐ]_Ð`Ñ`Ô`ÐØÐr,   ©g      ð¿r•  c                 óz   ‡‡— ˆfd„Šˆfd„| D ¦   «         }t          j        |ddiŽ}t          j        |d¬¦  «        S )aª  
    Generate an array of position indices for an N-D input array.

    Args:
      index_dims (`list[int]`):
        The shape of the index dimensions of the input array.
      output_range (`tuple[float]`, *optional*, defaults to `(-1.0, 1.0)`):
        The min and max values taken by each input index dimension.

    Returns:
      `torch.FloatTensor` of shape `(index_dims[0], index_dims[1], .., index_dims[-1], N)`.
    c                 ó`   •— t          j        ‰d         ‰d         | t           j        ¬¦  «        S )Nr   r   )rA  rB  rC  rJ  )r'   rD  Úfloat32)Ún_xels_per_dimÚoutput_ranges    €r-   Ú	_linspacez)build_linear_positions.<locals>._linspaceÅ	  s)   ø€ ÝŒ~ L°¤O¸Àa¼ÐP^ÕfkÔfsÐtÑtÔtÐtr,   c                 ó&   •— g | ]} ‰|¦  «        ‘ŒS r+   r+   )rÒ   rQ  rS  s     €r-   r  z*build_linear_positions.<locals>.<listcomp>È	  s#   ø€ ÐMÐMÐM°�)�)˜NÑ+Ô+ÐMÐMÐMr,   ÚindexingÚijrH   rv   )r'   ÚmeshgridrÕ  )r%  rR  Ú
dim_rangesÚarray_index_gridrS  s    `  @r-   Úbuild_linear_positionsrZ  ·	  si   øø€ ðuð uð uð uð uð NÐMÐMÐMÀ*ÐMÑMÔM€JÝ”~ zÐA¸DÐAÐAÐåŒ;Ð'¨RÐ0Ñ0Ô0Ð0r,   c                   óŽ   — e Zd ZdZeej        defd„¦   «         ¦   «         Zej        defd„¦   «         Z	ej        d„ ¦   «         Z
dS )Ú!PerceiverAbstractPositionEncodingz%Perceiver abstract position encoding.rs   c                 ó   — t           ‚r9   rµ  r  s    r-   Únum_dimensionsz0PerceiverAbstractPositionEncoding.num_dimensionsÑ	  rº  r,   c                 ó   — t           ‚r9   rµ  ©rB   r*  r  s      r-   rÐ  z-PerceiverAbstractPositionEncoding.output_sizeÖ	  r·  r,   c                 ó   — t           ‚r9   rµ  )rB   rE   rÒ  s      r-   rJ   z)PerceiverAbstractPositionEncoding.forwardÚ	  r·  r,   N)r#   r$   r%   r&   r¾  r¼  r½  rK   r^  rÐ  rJ   r+   r,   r-   r\  r\  Î	  s—   € € € € € Ø/Ð/àØÔð" ð "ð "ð "ñ Ôñ „Xð"ð 	Ôð"¨cð "ð "ð "ñ Ôð"ð 	Ôð"ð "ñ Ôð"ð "ð "r,   r\  c            	       ó®   ‡ — e Zd ZdZdˆ fd„	Zedefd„¦   «         Zdefd„Zde	j
        ded	ede	j
        fd
„Z	 ddedede	j        dz  de	j
        fd„Zˆ xZS )rë   zTrainable position encoding.rÇ  c                 óä   •— t          ¦   «                              ¦   «          || _        || _        t	          j        |¦  «        }t          j        t          j	        ||¦  «        ¦  «        | _
        d S r9   )r:   r;   Ú_num_channelsÚ_index_dimsr  r  r
   r<   r'   r=   rå   )rB   r%  rû   Ú	index_dimrD   s       €r-   r;   z+PerceiverTrainablePositionEncoding.__init__â	  s\   ø€ Ý‰Œ×ÒÑÔÐØ)ˆÔØ%ˆÔÝ”G˜JÑ'Ô'ˆ	Ý#%¤<µ´¸IÀ|Ñ0TÔ0TÑ#UÔ#UˆÔ Ð Ð r,   rs   c                 ób   — t          | j        t          ¦  «        rdS t          | j        ¦  «        S )Nr   )r§   re  rK   ró  r  s    r-   r^  z1PerceiverTrainablePositionEncoding.num_dimensionsé	  s-   € å�dÔ&­Ñ,Ô,ð 	Ø�1Ý�4Ô#Ñ$Ô$Ð$r,   c                 ó   — | j         S r9   )rd  r`  s      r-   rÐ  z.PerceiverTrainablePositionEncoding.output_sizeï	  s   € ØÔ!Ð!r,   rå   r  r  c                 óØ  — |j         d         }t          |dz  ¦  «        x}}t          j                             ¦   «         s||k    r||k    r|S |                     d||| j        ¦  «                             dddd¦  «        }t          j	         
                    |||fdd¬¦  «        }|                     d| j        d	¦  «                             ddd¦  «                             d¦  «        }|S )
Nr   g      à?r   r   rg   ÚbicubicF)rh   ÚmodeÚalign_cornersrH   )rz   r   r'   ÚjitÚ
is_tracingrØ  rd  rj   r
   Ú
functionalÚinterpolaterL  )rB   rå   r  r  Únum_positionsÚ
new_heightÚ	new_widths          r-   r  z;PerceiverTrainablePositionEncoding.interpolate_pos_encodingò	  s  € Ø+Ô1°!Ô4ˆÝ!*¨=¸#Ñ+=Ñ!>Ô!>Ð>ˆ
�Yõ Œy×#Ò#Ñ%Ô%ð 	'¨&°JÒ*>Ð*>À5ÈIÒCUÐCUØ&Ð&à1×9Ò9¸!¸ZÈÐTXÔTfÑgÔg×oÒoØˆq�!�Qñ
ô 
Ðõ !œm×7Ò7ØØ˜%�ØØð	 8ñ 
ô 
Ðð 2×9Ò9¸!¸TÔ=OÐQSÑTÔT×\Ò\Ð]^Ð`aÐcdÑeÔe×mÒmÐnoÑpÔpÐØ"Ð"r,   FNrE   r  r¬   c                 ó‚   — | j         }|r|\  }}|                      |||¦  «        }|�|                     |dd¦  «        }|S rG   )rå   r  rI   )rB   rE   r  r¬   rå   r  r  s          r-   rJ   z*PerceiverTrainablePositionEncoding.forward
  s^   € ð #Ô6Ðà#ð 	dØ&‰MˆF�EØ"&×"?Ò"?Ð@SÐU[Ð]bÑ"cÔ"cÐàÐ!Ø"5×"<Ò"<¸ZÈÈRÑ"PÔ"PÐØ"Ð"r,   )rÇ  )FN)r#   r$   r%   r&   r;   r¾  rK   r^  rÐ  r'   r�   r  rŽ   ÚSizerJ   rL   rM   s   @r-   rë   rë   ß	  s  ø€ € € € € Ø&Ð&ðVð Vð Vð Vð Vð Vð ð% ð %ð %ð %ñ „Xð%ð
"¨cð "ð "ð "ð "ð#¸E¼Lð #ÐRUð #Ð^að #ÐfkÔfrð #ð #ð #ð #ð, hlð#ð #Øð#Ø9=ð#ØSXÔS]Ð`dÑSdð#à	Œð#ð #ð #ð #ð #ð #ð #ð #r,   rë   c                 ó   — | €^t          |¦  «        } | d                              |f| j        z   ¦  «        } t          j        | |t          j        |¦  «        dg¦  «        } n-| j        d         t          |¦  «        k    rt          d¦  «        ‚| S )a  
    Checks or builds spatial position features (x, y, ...).

    Args:
      pos (`torch.FloatTensor`):
        None, or an array of position features. If None, position features are built. Otherwise, their size is checked.
      index_dims (`list[int]`):
        An iterable giving the spatial/index size of the data to be featurized.
      batch_size (`int`):
        The batch size of the data to be featurized.

    Returns:
        `torch.FloatTensor` of shape `(batch_size, prod(index_dims))` an array of position features.
    NrH   z5Spatial features have the wrong number of dimensions.)	rZ  rI   rz   r'   rØ  r  r  ró  rS   )rÒ  r%  rE   s      r-   Ú!_check_or_build_spatial_positionsrw  
  s‰   € ð €{Ý$ ZÑ0Ô0ˆð �$Œi×Ò 
˜}¨s¬yÑ8Ñ9Ô9ˆÝŒm˜C *­b¬g°jÑ.AÔ.AÀ2Ð!FÑGÔGˆˆð
 Œ9�RŒ=�C 
™OœOÒ+Ð+ÝÐTÑUÔUÐUØ€Jr,   c                   ó¤   ‡ — e Zd ZdZdˆ fd„	Zedefd„¦   «         Zd„ Z	 dd	e	e         d
ede
j        de
j        de
j        dz  de
j        fd„Zˆ xZS )r¯  z'Fourier (Sinusoidal) position encoding.TFc                 ó€   •— t          ¦   «                              ¦   «          || _        || _        || _        || _        d S r9   )r:   r;   rk  rj  ri  rl  )rB   rk  rj  ri  rl  rD   s        €r-   r;   z)PerceiverFourierPositionEncoding.__init__6
  s:   ø€ Ý‰Œ×ÒÑÔÐØ"ˆŒØ,ˆÔØ$ˆŒØ"ˆŒˆˆr,   rs   c                 ó*   — t          | j        ¦  «        S r9   )ró  rj  r  s    r-   r^  z/PerceiverFourierPositionEncoding.num_dimensions=
  s   € å�4Ô&Ñ'Ô'Ð'r,   c                 ó|   — t          | j        ¦  «        }| j        |z  }| j        s|dz  }| j        r
|| j        z  }|S )z4Returns size of positional encodings last dimension.rg   )ró  rj  rk  rl  ri  r^  )rB   Únum_dimsÚencoding_sizes      r-   rÐ  z,PerceiverFourierPositionEncoding.output_sizeA
  sO   € å�tÔ*Ñ+Ô+ˆØœ¨Ñ1ˆØŒ~ð 	Ø˜QÑˆMØŒ?ð 	1Ø˜TÔ0Ñ0ˆMàÐr,   Nr%  rE   r	  rJ  rÒ  c                 ó¢   — t          |||¦  «        }t          || j        | j        | j        | j        ¬¦  «                             ||¬¦  «        }|S )N)rk  rj  ri  rl  rÓ  )rw  rL  rk  rj  ri  rl  Úto)rB   r%  rE   r	  rJ  rÒ  Úfourier_pos_encs          r-   rJ   z(PerceiverFourierPositionEncoding.forwardL
  s_   € õ 0°°ZÀÑLÔLˆÝ3ØØ”nØÔ.Ø”Ø”nð
ñ 
ô 
÷ Š"�F %ˆ"Ñ
(Ô
(ð 	ð Ðr,   ©TFr9   )r#   r$   r%   r&   r;   r¾  rK   r^  rÐ  rï  r'   r	  rJ  r(   rJ   rL   rM   s   @r-   r¯  r¯  3
  sä   ø€ € € € € Ø1Ð1ð#ð #ð #ð #ð #ð #ð ð( ð (ð (ð (ñ „Xð(ð	ð 	ð 	ð" )-ðð à˜”Iðð ðð ”ð	ð
 Œ{ðð Ô Ñ%ðð 
Ô	ðð ð ð ð ð ð ð r,   r¯  c                   ó*   — e Zd Zedefd„¦   «         ZdS )ÚAbstractPreprocessorrs   c                 ó   — t          ¦   «         ‚)z$Returns size of preprocessor output.rµ  r  s    r-   rû   z!AbstractPreprocessor.num_channels`
  s   € õ "Ñ#Ô#Ð#r,   N)r#   r$   r%   r¾  rK   rû   r+   r,   r-   rƒ  rƒ  _
  s9   € € € € € Øð$˜cð $ð $ð $ñ „Xð$ð $ð $r,   rƒ  c            	       ó‚   ‡ — e Zd ZdZdeddfˆ fd„Zedefd„¦   «         Z	 	 	 dd	e	j
        d
e	j        dz  dedefd„Zˆ xZS )r-  a*  
    Text preprocessing for Perceiver Encoder. Can be used to embed `inputs` and add positional encodings.

    The dimensionality of the embeddings is determined by the `d_model` attribute of the configuration.

    Args:
        config ([`PerceiverConfig`]):
            Model configuration.
    rC   rs   Nc                 óè   •— t          ¦   «                              ¦   «          || _        t          j        |j        |j        ¬¦  «        | _        t          j        |j        |j        ¦  «        | _	        d S )N)Únum_embeddingsÚembedding_dim)
r:   r;   rC   r
   Ú	Embeddingr;  rü   rú   r.  rå   rA   s     €r-   r;   z"PerceiverTextPreprocessor.__init__q
  s^   ø€ Ý‰Œ×ÒÑÔÐØˆŒÝœ,°fÔ6GÐW]ÔWeÐfÑfÔfˆŒÝ#%¤<°Ô0NÐPVÔP^Ñ#_Ô#_ˆÔ Ð Ð r,   c                 ó   — | j         j        S r9   )rC   rü   r  s    r-   rû   z&PerceiverTextPreprocessor.num_channelsw
  s   € àŒ{Ô"Ð"r,   TFrp   rÒ  Únetwork_input_is_1dr  c                 ó¸   — |                       |¦  «        }|j        d         }t          j        d||j        ¬¦  «        }||                      |¦  «        z   }|d |fS )Nr   r   r  )rú   rz   r'   Úaranger	  rå   )	rB   rp   rÒ  r‹  r  Úembeddings_without_posr  Úposition_idsrú   s	            r-   rJ   z!PerceiverTextPreprocessor.forward{
  s_   € ð "&§¢°Ñ!8Ô!8Ðà”\ !”_ˆ
Ý”| A z¸&¼-ÐHÑHÔHˆØ+¨d×.FÒ.FÀ|Ñ.TÔ.TÑTˆ
à˜4Ð!7Ð7Ð7r,   ©NTF)r#   r$   r%   r&   r   r;   r¾  rK   rû   r'   Ú
LongTensorr�   rŽ   rJ   rL   rM   s   @r-   r-  r-  f
  sØ   ø€ € € € € ðð ð`˜ð `°4ð `ð `ð `ð `ð `ð `ð ð#˜cð #ð #ð #ñ „Xð#ð $(Ø$(Ø).ð8ð 8àÔ ð8ð Œ\˜DÑ ð8ð "ð	8ð
 #'ð8ð 8ð 8ð 8ð 8ð 8ð 8ð 8r,   r-  c                   ó^   ‡ — e Zd ZdZdeddfˆ fd„Zdej        dej        dej        fd„Zˆ xZ	S )	r0  z—
    Module to decode embeddings (for masked language modeling).

    Args:
        config ([`PerceiverConfig`]):
            Model configuration.
    rC   rs   Nc                 óÎ   •— t          ¦   «                              ¦   «          || _        |j        | _        t	          j        t          j        | j        ¦  «        ¦  «        | _        d S r9   )	r:   r;   rC   r;  r
   r<   r'   Úzerosr.  rA   s     €r-   r;   z"PerceiverEmbeddingDecoder.__init__”
  sK   ø€ Ý‰Œ×ÒÑÔÐØˆŒØ Ô+ˆŒÝ”L¥¤¨T¬_Ñ!=Ô!=Ñ>Ô>ˆŒ	ˆ	ˆ	r,   r    r9  c                 óì   — |j         \  }}}t          j        |                     d|g¦  «        |j                             dd¦  «        ¦  «        }|| j        z   }|                     ||| j        g¦  «        S )NrH   r   r   )rz   r'   rx   rØ  r-  ry   r.  r;  )rB   r    r9  rE   rƒ   rü   r�   s          r-   rJ   z!PerceiverEmbeddingDecoder.forwardš
  sp   € Ø'4Ô':Ñ$ˆ
�G˜Wå”˜m×3Ò3°R¸°MÑBÔBÀOÔDZ×DdÒDdÐefÐhiÑDjÔDjÑkÔkˆØ˜$œ)Ñ#ˆà�~Š~˜z¨7°D´OÐDÑEÔEÐEr,   )
r#   r$   r%   r&   r   r;   r'   r�   rJ   rL   rM   s   @r-   r0  r0  ‹
  s‘   ø€ € € € € ðð ð?˜ð ?°4ð ?ð ?ð ?ð ?ð ?ð ?ðF U¤\ð FÀEÄLð FÐUZÔUað Fð Fð Fð Fð Fð Fð Fð Fr,   r0  c            	       óŠ   ‡ — e Zd ZdZddeeef         defˆ fd„Z	 dde	j
        de	j
        dz  d	eee	j
        f         fd
„Zˆ xZS )r¦  a?  
    Multimodal postprocessing for Perceiver. Can be used to combine modality-specific postprocessors into a single
    postprocessor.

    Args:
          modalities (`Mapping[str, PostprocessorType]`):
            Dictionary mapping modality name to postprocessor class for that modality.
          input_is_dict (`bool`, *optional*, defaults to `False`):
            If True, input is assumed to be dictionary structured, and outputs keep the same dictionary shape. If
            False, input is a tensor which is sliced up during postprocessing by *modality_sizes*.
    Fr—  Úinput_is_dictc                 óˆ   •— t          ¦   «                              ¦   «          t          j        |¦  «        | _        || _        d S r9   )r:   r;   r
   r  r—  r—  )rB   r—  r—  rD   s      €r-   r;   z)PerceiverMultimodalPostprocessor.__init__°
  s8   ø€ Ý‰Œ×ÒÑÔÐÝœ-¨
Ñ3Ô3ˆŒØ*ˆÔÐÐr,   Nrp   rÒ  rs   c                 ó¨   ‡‡— | j         s"|€t          d¦  «        ‚t          |‰¬¦  «        Šˆˆfd„| j                             ¦   «         D ¦   «         }|S )Nz@Modality sizes should be specified if input is not a dictionary.)r  rp   c                 ó@   •— i | ]\  }}| |‰|         ‰d ¬¦  «        “ŒS )N)rÒ  r  r+   )rÒ   rî   Úpostprocessorrp   rÒ  s      €€r-   r   z<PerceiverMultimodalPostprocessor.forward.<locals>.<dictcomp>¾
  sE   ø€ ð 
ð 
ð 
á'�˜-ð �m�m F¨8Ô$4¸#ÈdÐSÑSÔSð
ð 
ð 
r,   )r—  rS   rû  r—  r  )rB   rp   rÒ  r  r‹   s    ``  r-   rJ   z(PerceiverMultimodalPostprocessor.forwardµ
  s{   øø€ ð Ô!ð 	OàÐ%Ý Ð!cÑdÔdÐdÝ °ÀvÐNÑNÔNˆFð
ð 
ð 
ð 
ð 
à+/¬?×+@Ò+@Ñ+BÔ+Bð
ñ 
ô 
ˆð ˆr,   )Fr  )r#   r$   r%   r&   r   r©   r  rŽ   r;   r'   r�   rJ   rL   rM   s   @r-   r¦  r¦  £
  s³   ø€ € € € € ð
ð 
ð+ð + 7¨3Ð0AÐ+AÔ#Bð +ÐSWð +ð +ð +ð +ð +ð +ð UYðð Ø”lðØ).¬¸Ñ)<ðà	��e”lÐ"Ô	#ðð ð ð ð ð ð ð r,   r¦  c                   ó\   ‡ — e Zd ZdZdededdfˆ fd„Zd	dej        dz  dej        fd„Z	ˆ xZ
S )
r©  a  
    Classification postprocessing for Perceiver. Can be used to convert the decoder output to classification logits.

    Args:
        config ([*PerceiverConfig*]):
            Model configuration.
        in_channels (`int`):
            Number of channels in the input.
    rC   r�  rs   Nc                 ó†   •— t          ¦   «                              ¦   «          t          j        ||j        ¦  «        | _        d S r9   )r:   r;   r
   r\   rC  rÃ  )rB   rC   r�  rD   s      €r-   r;   z-PerceiverClassificationPostprocessor.__init__Ð
  s3   ø€ Ý‰Œ×ÒÑÔÐÝœ) K°Ô1BÑCÔCˆŒˆˆr,   rÒ  c                 óJ   — |                       |¦  «        }|d d …dd d …f         S )Nr   ©rÃ  ©rB   rp   rÒ  r  r   s        r-   rJ   z,PerceiverClassificationPostprocessor.forwardÔ
  s+   € Ø—’ Ñ(Ô(ˆØ�a�a�a˜˜A˜A˜A�gŒÐr,   r  )r#   r$   r%   r&   r   rK   r;   r'   r�   rJ   rL   rM   s   @r-   r©  r©  Å
  s—   ø€ € € € € ðð ðD˜ð D¸Sð DÀTð Dð Dð Dð Dð Dð Dðð  5¤<°$Ñ#6ð ÐW\ÔWcð ð ð ð ð ð ð ð r,   r©  c            	       óp   ‡ — e Zd ZdZddedededdfˆ fd„Zdd	ej	        d
ej	        dz  dej	        fd„Z
ˆ xZS )r§  a™  
    Audio postprocessing for Perceiver. Can be used to convert the decoder output to audio features.

    Args:
        config ([*PerceiverConfig*]):
            Model configuration.
        in_channels (`int`):
            Number of channels in the input.
        postproc_type (`str`, *optional*, defaults to `"patches"`):
            Postprocessor type to use. Currently, only "patches" is supported.
    r€  rC   r�  Úpostproc_typers   Nc                 ó°   •— t          ¦   «                              ¦   «          |dk    rt          d¦  «        ‚t          j        ||j        ¦  «        | _        d S )Nr€  zInvalid postproc_type!)r:   r;   rS   r
   r\   r“  rÃ  )rB   rC   r�  r¢  rD   s       €r-   r;   z$PerceiverAudioPostprocessor.__init__æ
  sO   ø€ Ý‰Œ×ÒÑÔÐà˜IÒ%Ð%ÝÐ5Ñ6Ô6Ð6õ œ) K°Ô1IÑJÔJˆŒˆˆr,   rp   rÒ  c                 óp   — |                       |¦  «        }t          j        ||j        d         dg¦  «        S ©Nr   rH   )rÃ  r'   rØ  rz   r   s        r-   rJ   z#PerceiverAudioPostprocessor.forwardï
  s0   € Ø—’ Ñ(Ô(ˆÝŒ}˜V f¤l°1¤o°rÐ%:Ñ;Ô;Ð;r,   )r€  r  )r#   r$   r%   r&   r   rK   r©   r;   r'   r�   rJ   rL   rM   s   @r-   r§  r§  Ù
  s°   ø€ € € € € ð
ð 
ðKð K˜ð K¸Sð KÐQTð KÐeið Kð Kð Kð Kð Kð Kð<ð <˜eœlð <°´ÀÑ1Dð <ÐejÔeqð <ð <ð <ð <ð <ð <ð <ð <r,   r§  c                   ój   ‡ — e Zd ZdZdededdfˆ fd„Zd
dej        dej        dz  dej        fd	„Zˆ xZ	S )r¨  a'  
    Projection postprocessing for Perceiver. Can be used to project the channels of the decoder output to a lower
    dimension.

    Args:
        in_channels (`int`):
            Number of channels in the input.
        out_channels (`int`):
            Number of channels in the output.
    r�  rV  rs   Nc                 ó|   •— t          ¦   «                              ¦   «          t          j        ||¦  «        | _        d S r9   )r:   r;   r
   r\   rÃ  )rB   r�  rV  rD   s      €r-   r;   z)PerceiverProjectionPostprocessor.__init__   s0   ø€ Ý‰Œ×ÒÑÔÐÝœ) K°Ñ>Ô>ˆŒˆˆr,   rp   rÒ  c                 ó0   — |                       |¦  «        }|S r9   rŸ  r   s        r-   rJ   z(PerceiverProjectionPostprocessor.forward  s   € Ø—’ Ñ(Ô(ˆØˆr,   r  )
r#   r$   r%   r&   rK   r;   r'   r�   rJ   rL   rM   s   @r-   r¨  r¨  ô
  s—   ø€ € € € € ð	ð 	ð? Cð ?°sð ?¸tð ?ð ?ð ?ð ?ð ?ð ?ðð ˜eœlð °´ÀÑ1Dð ÐejÔeqð ð ð ð ð ð ð ð r,   r¨  c                   óÜ   ‡ — e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 d!dedededededededededefˆ fd„Zedefd„¦   «         Z		 d"de
j        dedefd„Z	 	 	 d#de
j        de
j        dz  dedefd „Zˆ xZS )$r[  a  
    Image preprocessing for Perceiver Encoder.

    Note: the *out_channels* argument refers to the output channels of a convolutional layer, if *prep_type* is set to
    "conv1x1" or "conv". If one adds absolute position embeddings, one must make sure the *num_channels* of the
    position encoding kwargs are set equal to the *out_channels*.

    Args:
        config ([*PerceiverConfig*]):
            Model configuration.
        prep_type (`str`, *optional*, defaults to `"conv"`):
            Preprocessing type. Can be "conv1x1", "conv", "patches", "pixels".
        spatial_downsample (`int`, *optional*, defaults to 4):
            Spatial downsampling factor.
        temporal_downsample (`int`, *optional*, defaults to 1):
            Temporal downsampling factor (only relevant in case a time dimension is present).
        position_encoding_type (`str`, *optional*, defaults to `"fourier"`):
            Position encoding type. Can be "fourier" or "trainable".
        in_channels (`int`, *optional*, defaults to 3):
            Number of channels in the input.
        out_channels (`int`, *optional*, defaults to 64):
            Number of channels in the output.
        conv_after_patching (`bool`, *optional*, defaults to `False`):
            Whether to apply a convolutional layer after patching.
        conv_after_patching_in_channels (`int`, *optional*, defaults to 54):
            Number of channels in the input of the convolutional layer after patching.
        conv2d_use_batchnorm (`bool`, *optional*, defaults to `True`):
            Whether to use batch normalization in the convolutional layer.
        concat_or_add_pos (`str`, *optional*, defaults to `"concat"`):
            How to concatenate the position encoding to the input. Can be "concat" or "add".
        project_pos_dim (`int`, *optional*, defaults to -1):
            Dimension of the position encoding to project to. If -1, no projection is applied.
        **position_encoding_kwargs (`Dict`, *optional*):
            Keyword arguments for the position encoding.
    ry  r±   r   rz  r   rg  Fr�  TrS  rH   rU  r„  rW  r�  rV  r‚  rƒ  Úconv2d_use_batchnormrX  rY  c                 ó:  •— t          ¦   «                              ¦   «          || _        |dvrt          d|› d�¦  «        ‚|dvrt          d|› d�¦  «        ‚|| _        || _        || _        || _        || _        || _	        || _
        || _        | j        dk    rjt          j        |d¦  «        }|t          j        |¦  «        k    }|r|d	k    rt          d
¦  «        ‚t!          |t#          |¦  «        ||
¬¦  «        | _        n?| j        dk    r4|d	k    rt          d¦  «        ‚t'          j        ||d||f¬¦  «        | _        || _        t/          d|||dœ|¤Ž\  | _        | _        |rt'          j        |	| j        ¦  «        nt'          j        ¦   «         | _        d S )N)ry  r€  rm  rQ  ú
Prep_type z is invalid©rS  ÚaddzInvalid value z for concat_or_add_pos.ry  r±   r   zYOnly powers of 4 expected for spatial and 1 expected for temporal downsampling with conv.)r�  r2  rV  r3  rQ  z$Conv1x1 does not downsample in time.©r   r   )r�  rV  r(  r5  ©rW  rV  rY  r+   )r:   r;   rC   rS   r�  rT  rU  r„  rW  rX  r‚  rV  r{   Úlogr  Úroundr1  rK   Úconvnetr
   ÚConv2dÚconvnet_1x1rY  r²  rå   r±  r\   rZ   Úconv_after_patches)rB   rC   rT  rU  r„  rW  r�  rV  r‚  rƒ  rª  rX  rY  rÌ  Úconvnet_num_layersÚconvnet_num_layers_is_intrD   s                   €r-   r;   z#PerceiverImagePreprocessor.__init__.  s	  ø€ õ  	‰Œ×ÒÑÔÐØˆŒàÐDÐDÐDÝÐ@¨)Ð@Ð@Ð@ÑAÔAÐAàÐ$5Ð5Ð5ÝÐXÐ.?ÐXÐXÐXÑYÔYÐYà&ˆÔØ"ˆŒØ"4ˆÔØ#6ˆÔ Ø&<ˆÔ#Ø!2ˆÔØ#6ˆÔ Ø(ˆÔàŒ>˜VÒ#Ð#å!%¤Ð*<¸aÑ!@Ô!@ÐØ(:½b¼hÐGYÑ>ZÔ>ZÒ(ZÐ%Ø,ð Ð0CÀqÒ0HÐ0HÝ Øoñô ð õ ,Ø'ÝÐ1Ñ2Ô2Ø)Ø2ð	ñ ô ˆDŒLˆLð Œ^˜yÒ(Ð(Ø" aÒ'Ð'Ý Ð!GÑHÔHÐHÝ!œyØ'Ø)Ø"à*Ð,>Ð?ð ñ  ô  ˆDÔð  /ˆÔÝ>Uð ?
Ø#9Ø%Ø+ð?
ð ?
ð 'ð	?
ð ?
Ñ;ˆÔ  $Ô";ð NaÐs�BŒIÐ5°tÔ7HÑIÔIÐIÕfhÔfqÑfsÔfsð 	ÔÐÐr,   rs   c                 ó   — | j         j        dk    }| j        dk    r| j        }n| j                              ¦   «         }| j        dk    r|S | j        s	| j        dv r| j        }ni| j        dk    r&| j        }|st          j
        || j        z  ¦  «        }n8| j        dk    r-| j        r| j        }n| j        | j        dz  z  }|r
|| j        z  }||z   S )Nrg   r   r®  )rQ  ry  rm  r€  )rå   r^  rY  rÐ  rX  r‚  rT  rV  r�  r{   ÚceilrU  r„  )rB   Úis_temporalÚpos_dimÚinp_dims       r-   rû   z'PerceiverImagePreprocessor.num_channelsx  s  € ð Ô.Ô=ÀÒAˆð Ô !Ò#Ð#ØÔ*ˆGˆGàÔ.×:Ò:Ñ<Ô<ˆGØÔ! UÒ*Ð*ØˆNð Ô#ð 	8 t¤~Ð9LÐ'LÐ'LØÔ'ˆGˆGØŒ^˜xÒ'Ð'ØÔ&ˆGØð GÝœ) G¨dÔ.EÑ$EÑFÔF�øØŒ^˜yÒ(Ð(ØÔ'ð 8ØÔ+��àÔ*¨TÔ-DÀaÑ-GÑG�Øð 8Ø˜tÔ7Ñ7�Gà˜Ñ Ð r,   rp   r‹  r  c                 óŽ  — |j         d         }|j         dd…         }|j         dd…         }t          j        |¦  «        }t          |j         ¦  «        dk    r|rt	          j        |||dg¦  «        }| j        dk    r|                      |||¦  «        }n.| j        dk    r#|                      |||j        |j	        ¬¦  «        }|  
                    |¦  «        }|s5|j         }	t	          j        |t          |	¦  «        dd…         dgz   ¦  «        }| j        d	k    rt	          j        ||gd¬
¦  «        }
n| j        dk    r||z   }
|
|fS )z�
        Construct the final input, including position encoding.

        This method expects the inputs to always have channels as last dimension.

        r   r   r   rH   rR  rz  rÓ  NrS  rv   r®  )rz   r  r  ró  r'   rØ  rW  rå   r	  rJ  r±  rï  rX  rÙ  )rB   rp   r‹  r  rE   r¬   r%  rÚ  Úpos_encÚshÚinputs_with_poss              r-   Ú_build_network_inputsz0PerceiverImagePreprocessor._build_network_inputs™  sg  € ð ”\ !”_ˆ
Ø”\ ! A #Ô&ˆ
Ø”\ ! B $Ô'ˆ
Ý”'˜*Ñ%Ô%ˆõ ˆvŒ|ÑÔ˜qÒ Ð Ð%8Ð Ý”] 6¨J¸ÀÐ+DÑEÔEˆFð Ô&¨+Ò5Ð5Ø×.Ò.¨zÐ;SÐU_Ñ`Ô`ˆGˆGØÔ(¨IÒ5Ð5Ø×.Ò.¨z¸:ÈfÌmÐciÔcoÐ.ÑpÔpˆGð ×+Ò+¨GÑ4Ô4ˆà"ð 	Cð ”ˆBÝ”m G­T°"©X¬X°c°r°c¬]¸b¸TÑ-AÑBÔBˆGØÔ! XÒ-Ð-Ý#œi¨°Ð(9¸rÐBÑBÔBˆOˆOØÔ# uÒ,Ð,Ø$ wÑ.ˆOØ Ð&Ð&r,   NrÒ  c                 óV  — | j         dk    r|                      |¦  «        }�n| j         dk    r|                      |¦  «        }nã| j         dk    rj|j        dk    r|d d | j        …d d | j        …f         }n²|j        dk    r*|d d …d d | j        …d d …d d | j        …d d | j        …f         }n}t          d¦  «        ‚| j         dk    rct          || j        | j        ¬¦  «        }|j        dk    r'|j        d	         d	k    r| 	                    d	¬
¦  «        }|  
                    |¦  «        }| j         dk    rX|j        dk    r|                     dddd	¦  «        }n4|j        dk    r|                     dd	ddd¦  «        }nt          d¦  «        ‚|                      |||¦  «        \  }}d }|||fS )Nry  rQ  rm  r±   r  z#Unsupported data format for pixels.r€  )r  r  r   rv   r   rg   r   z$Unsupported data format for conv1x1.)rT  r³  rµ  ÚndimrU  r„  rS   r!  rz   rL  r¶  rj   rÂ  ©rB   rp   rÒ  r‹  r  r  r  s          r-   rJ   z"PerceiverImagePreprocessor.forward¿  s  € ð Œ>˜VÒ#Ð#ð —\’\ &Ñ)Ô)ˆF‰FàŒ^˜yÒ(Ð(à×%Ò% fÑ-Ô-ˆFˆFàŒ^˜xÒ'Ð'àŒ{˜aÒÐØÐ :Ð : 4Ô#:Ð :Ð<VÐ<V¸tÔ?VÐ<VÐ VÔW��Ø” Ò!Ð!ØØ�A�AÐ2Ð2˜$Ô2Ð2°A°A°AÐ7QÐ7Q¸$Ô:QÐ7QÐSmÐSmÐVZÔVmÐSmÐmô��õ !Ð!FÑGÔGÐGàŒ^˜yÒ(Ð(õ $Ø¨DÔ,DÐY]ÔYpðñ ô ˆFð Œ{˜aÒÐ F¤L°¤O°qÒ$8Ð$8àŸš¨A˜Ñ.Ô.�ð ×,Ò,¨VÑ4Ô4ˆFàŒ>˜YÒ&Ð&àŒ{˜aÒÐØŸš¨¨1¨a°Ñ3Ô3��Ø” Ò!Ð!ØŸš¨¨1¨a°°AÑ6Ô6��å Ð!GÑHÔHÐHà%)×%?Ò%?ÀÐH[Ð]uÑ%vÔ%vÑ"ˆÐ"Øˆà�~Ð'9Ð9Ð9r,   )ry  r±   r   rz  r   rg  Fr�  TrS  rH   r�  r�  )r#   r$   r%   r&   rK   r©   rŽ   r;   r¾  rû   r'   r�   rÂ  rJ   rL   rM   s   @r-   r[  r[  	  sœ  ø€ € € € € ð"ð "ðN Ø"#Ø#$Ø&/ØØØ$)Ø/1Ø%)Ø!)Ø!ðH
ð H
ð  ð	H
ð
 !ðH
ð !$ðH
ð ðH
ð ðH
ð "ðH
ð *-ðH
ð #ðH
ð ðH
ð ðH
ð H
ð H
ð H
ð H
ð H
ðT ð!˜cð !ð !ð !ñ „Xð!ðB hmð$'ð $'Ø”lð$'Ø9=ð$'Ø`dð$'ð $'ð $'ð $'ðR $(Ø$(Ø).ð5:ð 5:à”ð5:ð Œ\˜DÑ ð5:ð "ð	5:ð
 #'ð5:ð 5:ð 5:ð 5:ð 5:ð 5:ð 5:ð 5:r,   r[  c                   óx   ‡ — e Zd ZdZdeddfˆ fd„Zedefd„¦   «         Zdde	j
        d	e	j
        dz  d
efd„Zˆ xZS )r¢  zÀ
    One-hot preprocessor for Perceiver Encoder. Can be used to add a dummy index dimension to the input.

    Args:
        config ([`PerceiverConfig`]):
            Model configuration.
    rC   rs   Nc                 óV   •— t          ¦   «                              ¦   «          || _        d S r9   )r:   r;   rC   rA   s     €r-   r;   z$PerceiverOneHotPreprocessor.__init__   s$   ø€ Ý‰Œ×ÒÑÔÐØ'-ˆŒˆˆr,   c                 ó   — | j         j        S r9   )rC   rC  r  s    r-   rû   z(PerceiverOneHotPreprocessor.num_channels  s   € àŒ{Ô%Ð%r,   Trp   rÒ  r‹  c                 ó*   — |d d …d d d …f         }|d |fS r9   r+   )rB   rp   rÒ  r‹  s       r-   rJ   z#PerceiverOneHotPreprocessor.forward  s)   € à˜˜˜˜4   ˜
Ô#ˆð �t˜VÐ#Ð#r,   )NT)r#   r$   r%   r&   r   r;   r¾  rK   rû   r'   r�   rŽ   rJ   rL   rM   s   @r-   r¢  r¢  ÷  s´   ø€ € € € € ðð ð.˜ð .°4ð .ð .ð .ð .ð .ð .ð ð&˜cð &ð &ð &ñ „Xð&ð$ð $˜eœlð $°´ÀÑ1Dð $Ðbfð $ð $ð $ð $ð $ð $ð $ð $r,   r¢  c            	       óž   ‡ — e Zd ZdZ	 	 	 	 	 	 dded	ed
edefˆ fd„Zedefd„¦   «         Zd„ Z		 	 	 dde
j        de
j        dz  dedefd„Zˆ xZS )r¡  a'  
    Audio preprocessing for Perceiver Encoder.

    Args:
        config ([*PerceiverConfig*]):
            Model configuration.
        prep_type (`str`, *optional*, defaults to `"patches"`):
            Preprocessor type to use. Only "patches" is supported.
        samples_per_patch (`int`, *optional*, defaults to 96):
            Number of samples per patch.
        position_encoding_type (`str`, *optional*, defaults to `"fourier"`):
            Type of position encoding to use. Can be "trainable" or "fourier".
        concat_or_add_pos (`str`, *optional*, defaults to `"concat"`):
            How to concatenate the position encoding to the input. Can be "concat" or "add".
        out_channels (`int`, *optional*, defaults to 64):
            Number of channels in the output.
        project_pos_dim (`int`, *optional*, defaults to -1):
            Dimension of the position encoding to project to. If -1, no projection is applied.
        **position_encoding_kwargs (`Dict`, *optional*):
            Keyword arguments for the position encoding.
    r€  é`   rz  rS  rg  rH   rT  r“  rW  rX  c                 ó(  •— t          ¦   «                              ¦   «          || _        |dk    rt          d|› d�¦  «        ‚|dvrt          d|› d�¦  «        ‚|| _        || _        || _        || _        t          d|||dœ|¤Ž\  | _	        | _
        d S )	Nr€  r¬  z# is invalid, can only be 'patches'.r­  zConcat_or_pos z+ is invalid, can only be 'concat' or 'add'.r°  r+   )r:   r;   rC   rS   r“  rW  rX  rY  r²  rå   r±  )
rB   rC   rT  r“  rW  rX  rV  rY  rÌ  rD   s
            €r-   r;   z#PerceiverAudioPreprocessor.__init__(  sÎ   ø€ õ 	‰Œ×ÒÑÔÐØˆŒà˜	Ò!Ð!ÝÐX¨)ÐXÐXÐXÑYÔYÐYàÐ$5Ð5Ð5ÝÐlÐ.?ÐlÐlÐlÑmÔmÐmà!2ˆÔØ&<ˆÔ#Ø!2ˆÔØ.ˆÔõ ?Vð ?
Ø#9Ø%Ø+ð?
ð ?
ð 'ð	?
ð ?
Ñ;ˆÔ  $Ô";Ð";Ð";r,   rs   c                 óˆ   — | j         dk    r| j         }n| j                             ¦   «         }| j        dk    r|S | j        |z   S )Nr   r®  )rY  rå   rÐ  rX  r“  )rB   r¼  s     r-   rû   z'PerceiverAudioPreprocessor.num_channelsI  sQ   € ð Ô !Ò#Ð#ØÔ*ˆGˆGàÔ.×:Ò:Ñ<Ô<ˆGØÔ! UÒ*Ð*ØˆNØÔ%¨Ñ/Ð/r,   c                 ór  — |j         d         }|j         dd…         }| j        dk    r|                      |¦  «        }n.| j        dk    r#|                      |||j        |j        ¬¦  «        }|                      |¦  «        }| j        dk    rt          j        ||gd¬¦  «        }n| j        d	k    r||z   }||fS )
z7Construct the final input, including position encoding.r   r   rH   rR  rz  rÓ  rS  rv   r®  )	rz   rW  rå   r	  rJ  r±  rX  r'   rÙ  )rB   rp   rE   r%  r¿  rÁ  s         r-   rÂ  z0PerceiverAudioPreprocessor._build_network_inputsT  sÓ   € à”\ !”_ˆ
Ø”\ ! B $Ô'ˆ
ð Ô&¨+Ò5Ð5Ø×.Ò.¨zÑ:Ô:ˆGˆGØÔ(¨IÒ5Ð5Ø×.Ò.¨z¸:ÈfÌmÐciÔcoÐ.ÑpÔpˆGð ×+Ò+¨GÑ4Ô4ˆàÔ! XÒ-Ð-Ý#œi¨°Ð(9¸rÐBÑBÔBˆOˆOØÔ# uÒ,Ð,Ø$ wÑ.ˆOà Ð&Ð&r,   NTFrp   rÒ  r‹  r  c                 ó�   — t          j        ||j        d         d| j        g¦  «        }|                      |¦  «        \  }}d }|||fS r¥  )r'   rØ  rz   r“  rÂ  rÅ  s          r-   rJ   z"PerceiverAudioPreprocessor.forwardi  sQ   € õ ”˜v¨¬°Q¬¸¸TÔ=SÐ'TÑUÔUˆà%)×%?Ò%?ÀÑ%GÔ%GÑ"ˆÐ"Øˆà�~Ð'9Ð9Ð9r,   )r€  rË  rz  rS  rg  rH   r�  )r#   r$   r%   r&   r©   rK   r;   r¾  rû   rÂ  r'   r�   rŽ   rJ   rL   rM   s   @r-   r¡  r¡    s  ø€ € € € € ðð ð2 #Ø!#Ø&/Ø!)ØØð
ð 
ð ð
ð ð	
ð
 !$ð
ð ð
ð 
ð 
ð 
ð 
ð 
ðB ð0˜cð 0ð 0ð 0ñ „Xð0ð'ð 'ð 'ð0 $(Ø$(Ø).ð:ð :à”ð:ð Œ\˜DÑ ð:ð "ð	:ð
 #'ð:ð :ð :ð :ð :ð :ð :ð :r,   r¡  c                   óÆ   ‡ — e Zd ZdZ	 	 ddeeef         deeef         dz  defˆ fd„Z	e
defd	„¦   «         Z	 	 	 ddeeej        f         dej        dz  dededef
d„Zˆ xZS )r   a©  
    Multimodal preprocessing for Perceiver Encoder.

    Inputs for each modality are preprocessed, then padded with trainable position embeddings to have the same number
    of channels.

    Args:
        modalities (`Mapping[str, PreprocessorType]`):
            Dict mapping modality name to preprocessor.
        mask_probs (`dict[str, float]`):
            Dict mapping modality name to masking probability of that modality.
        min_padding_size (`int`, *optional*, defaults to 2):
            The minimum padding size for all modalities. The final output will have num_channels equal to the maximum
            channels across all modalities plus min_padding_size.
    Nrg   r—  r˜  r–  c                 ó†  •‡ — t          ¦   «                              ¦   «          t          j        |¦  «        ‰ _        |‰ _        |�|ni ‰ _        t          j        ˆ fd„|                     ¦   «         D ¦   «         ¦  «        ‰ _	        t          j        ˆ fd„‰ j                             ¦   «         D ¦   «         ¦  «        ‰ _
        d S )Nc           
      ó€   •— i | ]:\  }}|t          j        t          j        d ‰j        |j        z
  ¦  «        ¦  «        “Œ;S rÿ  ©r
   r<   r'   r=   rû   )rÒ   rî   ÚpreprocessorrB   s      €r-   r   z<PerceiverMultimodalPreprocessor.__init__.<locals>.<dictcomp>”  sP   ø€ ð ð ð á*�H˜lð �"œ,¥u¤{°1°dÔ6GÈ,ÔJcÑ6cÑ'dÔ'dÑeÔeðð ð r,   c           	      óp   •— i | ]2\  }}|t          j        t          j        d ‰j        ¦  «        ¦  «        “Œ3S rÿ  rÓ  )rÒ   rî   r…   rB   s      €r-   r   z<PerceiverMultimodalPreprocessor.__init__.<locals>.<dictcomp>š  s9   ø€ ÐrÐrÐrÉ;È8ÐUVˆX•r”|¥E¤K°°4Ô3DÑ$EÔ$EÑFÔFÐrÐrÐrr,   )r:   r;   r
   r  r—  r–  r˜  rì   r  r  Úmask)rB   r—  r˜  r–  rD   s   `   €r-   r;   z(PerceiverMultimodalPreprocessor.__init__‰  sÌ   øø€ õ 	‰Œ×ÒÑÔÐÝœ-¨
Ñ3Ô3ˆŒØ 0ˆÔØ(2Ð(>˜*˜*ÀBˆŒÝÔ'ðð ð ð à.8×.>Ò.>Ñ.@Ô.@ðñ ô ñ
ô 
ˆŒõ Ô$ØrÐrÐrÐrÐZ^ÔZi×ZoÒZoÑZqÔZqÐrÑrÔrñ
ô 
ˆŒ	ˆ	ˆ	r,   rs   c                 óz   — t          d„ | j                             ¦   «         D ¦   «         ¦  «        }|| j        z   }|S )Nc              3   ó*   K  — | ]\  }}|j         V — Œd S r9   )rû   )rÒ   r…   Ú	processors      r-   rÔ   z?PerceiverMultimodalPreprocessor.num_channels.<locals>.<genexpr>Ÿ  s)   è è € ÐbÐb¹,¸!¸Y˜yÔ5ÐbÐbÐbÐbÐbÐbr,   r  r  s      r-   rû   z,PerceiverMultimodalPreprocessor.num_channels�  sA   € åÐbÐbÈ$Ì/×J_ÒJ_ÑJaÔJaÐbÑbÔbÑbÔbÐØ.°Ô1FÑFÐØ"Ð"r,   TFrp   rÒ  r‹  r  c                 óF  ‡— i Ši }i }| j                              ¦   «         D �]8\  }} |||         ||¬¦  «        \  }	}
||<   |	j        \  }}}| j        |                              |dd¦  «        }t          j        |||| j        |z
  g¦  «        }t          j        |	|gd¬¦  «        }|| j	        v r”| j
        |                              |dd¦  «        }| j	        |         }t          j        t          j        ||g|¦  «        ¦  «        }t          j        |d¬¦  «                             |j        ¦  «        }d|z
  |z  ||z  z   }|‰|<   |j        d         ||<   �Œ:ˆfd„t!          ‰                     ¦   «         ¦  «        D ¦   «         }t          j        |d¬¦  «        }|||fS )N)rÒ  r‹  rH   rg   rv   r   c                 ó    •— g | ]
}‰|         ‘ŒS r+   r+   )rÒ   r&  Úpaddeds     €r-   r  z;PerceiverMultimodalPreprocessor.forward.<locals>.<listcomp>É  s   ø€ Ð>Ð>Ð> 1�V˜A”YÐ>Ð>Ð>r,   )r—  r  rz   r  rI   r'   r×  rû   rÙ  r˜  rÖ  Ú	bernoulliÚfullÚ	unsqueezer  r	  rø  r€   )rB   rp   rÒ  r‹  r  r  r  rî   rÔ  r�   r…   rE   Únum_samplesrû   r¿  r  Úoutput_paddedÚ
mask_tokenÚ	mask_probrÖ  Ú	padded_lsÚfinal_inputsrÜ  s                         @r-   rJ   z'PerceiverMultimodalPreprocessor.forward£  sÛ  ø€ ð ˆØˆØÐØ&*¤o×&;Ò&;Ñ&=Ô&=ð 	>ñ 	>Ñ"ˆH�là6B°lØ�xÔ  cÐ?Rð7ñ 7ô 7Ñ3ˆF�AÐ)¨(Ñ3ð
 5;´LÑ1ˆJ˜ \Ø”l 8Ô,×3Ò3°JÀÀBÑGÔGˆGåÔ(ØØ˜[¨$Ô*;¸lÑ*JÐKñô ˆGõ "œI v¨wÐ&7¸QÐ?Ñ?Ô?ˆMð ˜4œ?Ð*Ð*Ø!œY xÔ0×7Ò7¸
ÀBÈÑKÔK�
Ø œO¨HÔ5�	Ý”¥u¤z°:¸{Ð2KÈYÑ'WÔ'WÑXÔX�Ý” t°Ð3Ñ3Ô3×6Ò6°zÔ7HÑIÔI�Ø!" T¡¨]Ñ :¸TÀJÑ=NÑ N�à,ˆF�8ÑØ'4Ô':¸1Ô'=ˆN˜8Ñ$Ñ$ð ?Ð>Ð>Ð>­¨v¯{ª{©}¬}Ñ(=Ô(=Ð>Ñ>Ô>ˆ	õ ”y °Ð2Ñ2Ô2ˆà˜^Ð-?Ð?Ð?r,   )Nrg   r�  )r#   r$   r%   r&   r   r©   r  ÚfloatrK   r;   r¾  rû   r'   r�   rŽ   ÚPreprocessorOutputTyperJ   rL   rM   s   @r-   r   r   x  s%  ø€ € € € € ðð ð& 26Ø !ð	
ð 
à˜CÐ!1Ð1Ô2ð
ð ˜C ˜JÔ'¨$Ñ.ð
ð ð	
ð 
ð 
ð 
ð 
ð 
ð( ð#˜cð #ð #ð #ñ „Xð#ð $(Ø$(Ø).ð+@ð +@à˜˜Uœ\Ð)Ô*ð+@ð Œ\˜DÑ ð+@ð "ð	+@ð
 #'ð+@ð 
 ð+@ð +@ð +@ð +@ð +@ð +@ð +@ð +@r,   r   )
rv  rc  rO  r"  r�  r}  r@  r°   rô   rÞ   )NrH   NNr¯  )re  TF)rM  )kr&   r¼  r{   Úcollections.abcr   r   Údataclassesr   Ú	functoolsr   Úoperatorr   Útypingr   r	   Únumpyr  r'   r
   Útorch.nnr   r   r   Ú r   rè   Úactivationsr   Úmasking_utilsr   Úmodeling_outputsr   Úmodeling_utilsr   Úpytorch_utilsr   Úutilsr   r   r   r   Úconfiguration_perceiverr   r©   rK   ÚModalitySizeTyper*   r�   rç  r  r  Ú
get_loggerr#   Úloggerr   r/   r1   r5   ÚModuler7   rO   r�   r˜   r¤   r°   r¾   rÞ   rô   r"  r@  rO  rc  rv  r}  r�  r²  ÚABCMetarö   rÁ  r/  rD  r‰  r£  rû  r¤  r!  r´  r#  r1  rL  rZ  r\  rë   rw  r¯  rƒ  r-  r0  r¦  r©  r§  r¨  r[  r¢  r¡  r   Ú__all__r+   r,   r-   ú<module>rý     sÜ  ðð Ð à 
€
€
€
Ø €€€Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ø !Ð !Ð !Ð !Ð !Ð !Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  à Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð Ø AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ AÐ Aà &Ð &Ð &Ð &Ð &Ð &Ø !Ð !Ð !Ð !Ð !Ð !Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø BÐ BÐ BÐ BÐ BÐ BØ -Ð -Ð -Ð -Ð -Ð -Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ø DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DØ 4Ð 4Ð 4Ð 4Ð 4Ð 4ð ˜3 ˜8Ô$Ð Ø˜uœ|¨U¬\¸DÑ-@À%Ä,ÐNÔOÐ Ø˜CÐ!7Ð7Ô8Ð Ø˜S #˜XÔ&Ð à	ˆÔ	˜HÑ	%Ô	%€ð €ððñ ô ð
 ð
=ð 
=ð 
=ð 
=ð 
=˜;ñ 
=ô 
=ñ „ñô ð
=ð €ððñ ô ð
 ð=ð =ð =ð =ð =˜[ñ =ô =ñ „ñô ð=ð €ððñ ô ð
 ð=ð =ð =ð =ð =˜kñ =ô =ñ „ñô ð=ð €ððñ ô ð ð=ð =ð =ð =ð = ñ =ô =ñ „ñô ð=ð7ð 7ð 7ð 7ð 7˜"œ)ñ 7ô 7ð 7ðkð kð kð kð k˜RœYñ kô kð kð\ð ð ð ð ˜"œ)ñ ô ð ðLð Lð Lð Lð L˜œñ Lô Lð Lð^ð ð ð ð �2”9ñ ô ð ð&=ð =ð =ð =ð =�R”Yñ =ô =ð =ð@k
ð k
ð k
ð k
ð k
�r”yñ k
ô k
ð k
ð\ ð\ð \ð \ð \ð \˜ñ \ô \ñ „ð\ð& €ð
ðñ ô ðn
ð n
ð n
ð n
ð n
Ð-ñ n
ô n
ñô ðn
ðb €ððñ ô ð
A
ð A
ð A
ð A
ð A
Ð3ñ A
ô A
ñô ð
A
ðH €ððñ ô ð
j
ð j
ð j
ð j
ð j
Ð)Añ j
ô j
ñô ð
j
ðZ €ð
ðñ ô ðm
ð m
ð m
ð m
ð m
Ð-Eñ m
ô m
ñô ðm
ð` €ð
ðñ ô ðk
ð k
ð k
ð k
ð k
Ð-Eñ k
ô k
ñô ðk
ð\ €ð
ðñ ô ðl
ð l
ð l
ð l
ð l
Ð4Lñ l
ô l
ñô ðl
ð^ €ð
ðñ ô ðn
ð n
ð n
ð n
ð n
Ð6ñ n
ô n
ñô ðn
ðb €ððñ ô ð:K
ð K
ð K
ð K
ð K
Ð)Añ K
ô K
ñ;ô ð:K
ðf ØØ'+Ø%)ð0ð 0ð 0ð 0ðJ"ð "ð "ð "ð "˜rœy°C´Kð "ñ "ô "ð "ð"ð ð ð ð Ð!9ñ ô ð ð4vXð vXð vXð vXð vXÐ4ñ vXô vXð vXðr+hð +hð +hð +hð +hÐ%=ñ +hô +hð +hð\ gð  gð  gð  gð  gÐ":ñ  gô  gð  gðF2hð 2hð 2hð 2hð 2hÐ-Eñ 2hô 2hð 2hðjÐ 0ð ¸%¼,ð È7ÐSVÐX]ÔXdÐSdÔKeð ð ð ð ð0kð kð kð kð kÐ!9ñ kô kð kð^7
ð 7
˜5œ<ð 7
¸cð 7
Ð[^ð 7
ÐglÔgsð 7
ð 7
ð 7
ð 7
ðtSð Sð Sð Sð S˜œ	ñ Sô Sð Sð #ð #ð #ð #ð #�r”yñ #ô #ð #ðL1ð 1ð 1ð 1ðh1ð 1ð 1ð 1ð."ð "ð "ð "ð "¨¬	¸S¼[ð "ñ "ô "ð "ð"3#ð 3#ð 3#ð 3#ð 3#Ð)Jñ 3#ô 3#ð 3#ðlð ð ð<)ð )ð )ð )ð )Ð'Hñ )ô )ð )ðX$ð $ð $ð $ð $˜2œ9ñ $ô $ð $ð"8ð "8ð "8ð "8ð "8Ð 4ñ "8ô "8ð "8ðJFð Fð Fð Fð F ¤	ñ Fô Fð Fð0ð ð ð ð  r¤yñ ô ð ðDð ð ð ð ¨2¬9ñ ô ð ð(<ð <ð <ð <ð < "¤)ñ <ô <ð <ð6ð ð ð ð  r¤yñ ô ð ð*k:ð k:ð k:ð k:ð k:Ð!5ñ k:ô k:ð k:ð\$ð $ð $ð $ð $Ð"6ñ $ô $ð $ð4d:ð d:ð d:ð d:ð d:Ð!5ñ d:ô d:ð d:ðNV@ð V@ð V@ð V@ð V@Ð&:ñ V@ô V@ð V@ðrð ð €€€r,   