Ë
    ÜÓ^jÞ]  ã                   óÆ  — d dl mZmZ d dlZd dlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZmZmZ dd
lmZmZ ddlmZ ddlmZmZmZ ddlmZmZ ddlmZ ddl m!Z!  G d„ dejD                  «      Z# G d„ dejD                  «      Z$	 	 d/dejD                  dejJ                  dejJ                  dejJ                  dejJ                  dz  de&dz  de&dee   fd„Z' G d„ dejD                  «      Z( G d „ d!ejD                  «      Z) G d"„ d#e«      Z* G d$„ d%ejD                  «      Z+e G d&„ d'e«      «       Z,e G d(„ d)e,«      «       Z- ed*¬+«       G d,„ d-e,«      «       Z.g d.¢Z/y)0é    )ÚCallableÚIterableN)Únné   )Úinitialization)ÚACT2FN)Úcreate_bidirectional_mask)ÚGradientCheckpointingLayer)ÚBaseModelOutputÚBaseModelOutputWithPoolingÚImageClassifierOutput)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedModel)ÚUnpack)ÚTransformersKwargsÚauto_docstringÚ	torch_int)Úcan_return_tupleÚmerge_with_config_defaults)Úcapture_outputsé   )ÚVivitConfigc                   ó`   ‡ — e Zd ZdZdefˆ fd„Zdej                  dej                  fd„Zˆ xZ	S )ÚVivitTubeletEmbeddingsay  
    This class turns `pixel_values` of shape `(batch_size, num_frames, num_channels, height, width)` into the initial
    `hidden_states` (tubelet embeddings) of shape `(batch_size, seq_length, hidden_size)` to be consumed by a
    Transformer encoder.

    The seq_length equals (num_frames // tubelet_size[0]) * (height // tubelet_size[1]) * (width // tubelet_size[2]).
    Úconfigc                 óN  •— t         ‰| �  «        |j                  }|j                  }t	        |t
        «      r|n||f}|j                  |d   z  |d   |d   z  z  |d   |d   z  z  | _        || _        t        j                  |j                  |j                  ||¬«      | _        y )Nr   r   é   )Úkernel_sizeÚstride)ÚsuperÚ__init__Útubelet_sizeÚ
image_sizeÚ
isinstancer   Ú
num_framesÚnum_patchesr   ÚConv3dÚnum_channelsÚhidden_sizeÚ
projection)Úselfr   r"   r#   Ú	__class__s       €ús/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/vivit/modeling_vivit.pyr!   zVivitTubeletEmbeddings.__init__0   s¯   ø€ Ü‰ÑÔØ×*Ñ*ˆØ×&Ñ&ˆ
Ü#-¨j¼(Ô#C‘ZÈ*ÐV`ÐIaˆ
ð ×Ñ ,¨q¡/Ñ1Ø˜!‰} ¨Q¡Ñ/ñ1à˜!‰} ¨Q¡Ñ/ñ1ð 	Ôð
 %ˆŒÜŸ)™)Ø×Ñ ×!3Ñ!3ÀÐVbô
ˆ�ó    Úpixel_valuesÚreturnc                 ó†   — |j                  dd«      }| j                  |«      j                  d«      j                  dd«      S )Nr   r   )Ú	transposer*   Úflatten)r+   r/   s     r-   ÚforwardzVivitTubeletEmbeddings.forward@   s;   € à#×-Ñ-¨a°Ó3ˆØ�‰˜|Ó,×4Ñ4°QÓ7×AÑAÀ!ÀQÓGÐGr.   )
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   ÚtorchÚTensorr4   Ú__classcell__©r,   s   @r-   r   r   '   s2   ø„ ñð
˜{õ 
ð H E§L¡Lð H°U·\±\÷ Hr.   r   c                   ó¦   ‡ — e Zd ZdZdefˆ fd„Zdej                  dededej                  fd„Z	dd	ej                  d
e
dej                  fd„Zˆ xZS )ÚVivitEmbeddingszY
    Construct the CLS token, position and tubelet patch embeddings for video input.
    r   c                 óþ  •— t         ‰| �  «        t        j                  t	        j
                  dd|j                  «      «      | _        t        |«      | _	        | j                  j                  }t        j                  t	        j
                  d|dz   |j                  «      «      | _        t        j                  |j                  «      | _        |j                  dd  | _        | j                  j"                  | _        y )Nr   )r    r!   r   Ú	Parameterr9   Úzerosr)   Ú	cls_tokenr   Úpatch_embeddingsr&   Úposition_embeddingsÚDropoutÚhidden_dropout_probÚdropoutr"   Ú
patch_sizer#   )r+   r   r&   r,   s      €r-   r!   zVivitEmbeddings.__init__K   sµ   ø€ Ü‰ÑÔäŸ™¤e§k¡k°!°Q¸×8JÑ8JÓ&KÓLˆŒÜ 6°vÓ >ˆÔØ×+Ñ+×7Ñ7ˆÜ#%§<¡<´·±¸A¸{ÈQ¹ÐPV×PbÑPbÓ0cÓ#dˆÔ Ü—z‘z &×"<Ñ"<Ó=ˆŒà ×-Ñ-¨a¨bÐ1ˆŒØ×/Ñ/×:Ñ:ˆ�r.   Ú
embeddingsÚheightÚwidthr0   c                 óº  — | j                   j                  }| j                  j                  d   dz
  }t        j
                  j                  «       s||k(  r||k(  r| j                  S | j                  dd…dd…f   }| j                  dd…dd…f   }|j                  d   }|| j                  d   z  }	|| j                  d   z  }
t        |dz  «      }|j                  d|||«      }|j                  dddd«      }t        j                  j                  ||	|
fdd	¬
«      }|j                  dddd«      j                  dd|«      }t	        j                  ||fd¬«      S )a   
        This method allows to interpolate the pre-trained position encodings, to be able to use the model on higher resolution
        images. This method is also adapted to support torch.jit tracing.

        Adapted from:
        - https://github.com/facebookresearch/dino/blob/de9ee3df6cf39fac952ab558447af1fa1365362a/vision_transformer.py#L174-L194, and
        - https://github.com/facebookresearch/dinov2/blob/e1277af2ba9496fbadf7aec6eba56e8d882d1e35/dinov2/models/vision_transformer.py#L179-L211
        r   Néÿÿÿÿr   g      à?r   r   ÚbicubicF)ÚsizeÚmodeÚalign_corners©Údim)rC   r&   rD   Úshaper9   ÚjitÚ
is_tracingrH   r   ÚreshapeÚpermuter   Ú
functionalÚinterpolateÚviewÚcat)r+   rI   rJ   rK   r&   Únum_positionsÚclass_pos_embedÚpatch_pos_embedrS   Ú
new_heightÚ	new_widthÚsqrt_num_positionss               r-   Úinterpolate_pos_encodingz(VivitEmbeddings.interpolate_pos_encodingW   sf  € ð ×+Ñ+×7Ñ7ˆØ×0Ñ0×6Ñ6°qÑ9¸AÑ=ˆô �y‰y×#Ñ#Ô%¨+¸Ò*FÈ6ÐUZÊ?Ø×+Ñ+Ð+à×2Ñ2²1°b°q°b°5Ñ9ˆØ×2Ñ2²1°a±b°5Ñ9ˆà×Ñ˜rÑ"ˆà˜tŸ™¨qÑ1Ñ1ˆ
Ø˜TŸ_™_¨QÑ/Ñ/ˆ	ä& }°cÑ'9Ó:ÐØ)×1Ñ1°!Ð5GÐI[Ð]`ÓaˆØ)×1Ñ1°!°Q¸¸1Ó=ˆäŸ-™-×3Ñ3ØØ˜iÐ(ØØð	 4ó 
ˆð *×1Ñ1°!°Q¸¸1Ó=×BÑBÀ1ÀbÈ#ÓNˆä�y‰y˜/¨?Ð;ÀÔCÐCr.   r/   rc   c                 óÖ  — |j                   \  }}}}}| j                  |«      }| j                  j                  |dd«      }	t	        j
                  |	|fd¬«      }|r|| j                  |||«      z   }ne|| j                  d   k7  s|| j                  d   k7  r2t        d|› d|› d| j                  d   › d| j                  d   › d�	«      ‚|| j                  z   }| j                  |«      }|S )	NrM   r   rR   r   zInput image size (Ú*z) doesn't match model (z).)rT   rC   rB   Úexpandr9   r\   rc   r#   Ú
ValueErrorrD   rG   )
r+   r/   rc   Ú
batch_sizer%   r(   rJ   rK   rI   Ú
cls_tokenss
             r-   r4   zVivitEmbeddings.forward~   s  € Ø>J×>PÑ>PÑ;ˆ
�J ¨f°eØ×*Ñ*¨<Ó8ˆ
ð —^‘^×*Ñ*¨:°r¸2Ó>ˆ
Ü—Y‘Y 
¨JÐ7¸QÔ?ˆ
á#Ø# d×&CÑ&CÀJÐPVÐX]Ó&^Ñ^‰Jà˜Ÿ™¨Ñ+Ò+¨u¸¿¹ÈÑ8JÒ/JÜ Ø(¨¨°°%°ð 9ØŸ™¨Ñ+Ð,¨A¨d¯o©o¸aÑ.@Ð-AÀðEóð ð $ d×&>Ñ&>Ñ>ˆJà—\‘\ *Ó-ˆ
àÐr.   )F)r5   r6   r7   r8   r   r!   r9   r:   Úintrc   Úboolr4   r;   r<   s   @r-   r>   r>   F   sl   ø„ ñð
;˜{õ 
;ð%D°5·<±<ð %DÈð %DÐUXð %DÐ]b×]iÑ]ió %DñN E§L¡Lð ÈDð Ð]b×]iÑ]i÷ r.   r>   ÚmoduleÚqueryÚkeyÚvalueÚattention_maskÚscalingrG   Úkwargsc                 óì  — |€|j                  d«      dz  }t        j                  ||j                  dd«      «      |z  }|�||z   }t        j
                  j                  |dt        j                  ¬«      j                  |j                  «      }t        j
                  j                  ||| j                  ¬«      }t        j                  ||«      }	|	j                  dd«      j                  «       }	|	|fS )NrM   ç      à¿r   r   )rS   Údtype)ÚpÚtrainingr   )rO   r9   Úmatmulr2   r   rY   ÚsoftmaxÚfloat32Útoru   rG   rw   Ú
contiguous)
rl   rm   rn   ro   rp   rq   rG   rr   Úattn_weightsÚattn_outputs
             r-   Úeager_attention_forwardr   •   sÓ   € ð €Ø—*‘*˜R“. DÑ(ˆô —<‘<  s§}¡}°Q¸Ó':Ó;¸gÑE€LàÐ!Ø# nÑ4ˆä—=‘=×(Ñ(¨¸2ÄUÇ]Á]Ð(ÓS×VÑVÐW\×WbÑWbÓc€LÜ—=‘=×(Ñ(¨¸È6Ï?É?Ð(Ó[€Lä—,‘,˜|¨UÓ3€KØ×'Ñ'¨¨1Ó-×8Ñ8Ó:€Kà˜Ð$Ð$r.   c                   ó¦   ‡ — e Zd Zdefˆ fd„Z	 d	dej                  dej                  dz  dee   de	ej                  ej                  f   fd„Z
ˆ xZS )
ÚVivitAttentionr   c                 ó(  •— t         ‰| �  «        || _        |j                  | _        t	        |d|j
                  |j                  z  «      | _        |j                  | _        | j                  dz  | _	        d| _
        t        j                  |j
                  |j                  | j                  z  |j                  ¬«      | _        t        j                  |j
                  |j                  | j                  z  |j                  ¬«      | _        t        j                  |j
                  |j                  | j                  z  |j                  ¬«      | _        t        j                  |j                  | j                  z  |j
                  d¬«      | _        y )NÚhead_dimrt   F)ÚbiasT)r    r!   r   Únum_attention_headsÚgetattrr)   rƒ   Úattention_probs_dropout_probÚattention_dropoutrq   Ú	is_causalr   ÚLinearÚqkv_biasÚq_projÚk_projÚv_projÚo_proj©r+   r   r,   s     €r-   r!   zVivitAttention.__init__²   s,  ø€ Ü‰ÑÔØˆŒØ#)×#=Ñ#=ˆÔ Ü ¨
°F×4FÑ4FÈ&×JdÑJdÑ4dÓeˆŒØ!'×!DÑ!DˆÔØ—}‘} dÑ*ˆŒØˆŒä—i‘i × 2Ñ 2°F×4NÑ4NÐQU×Q^ÑQ^Ñ4^Ðek×etÑetÔuˆŒÜ—i‘i × 2Ñ 2°F×4NÑ4NÐQU×Q^ÑQ^Ñ4^Ðek×etÑetÔuˆŒÜ—i‘i × 2Ñ 2°F×4NÑ4NÐQU×Q^ÑQ^Ñ4^Ðek×etÑetÔuˆŒÜ—i‘i × :Ñ :¸T¿]¹]Ñ JÈF×L^ÑL^ÐeiÔjˆ�r.   NÚhidden_statesrp   rr   r0   c                 óš  — |j                   d d }g |¢d‘| j                  ‘­}| j                  |«      j                  |«      j	                  dd«      }| j                  |«      j                  |«      j	                  dd«      }| j                  |«      j                  |«      j	                  dd«      }t        j                  | j                  j                  t        «      }	 |	| ||||f| j                  sdn| j                  | j                  dœ|¤Ž\  }
} |
j                  g |¢d‘­Ž j!                  «       }
| j#                  |
«      }
|
|fS )NrM   r   r   ç        )rG   rq   )rT   rƒ   rŒ   r[   r2   r�   rŽ   r   Úget_interfacer   Ú_attn_implementationr   rw   rˆ   rq   rW   r|   r�   )r+   r‘   rp   rr   Úinput_shapeÚhidden_shapeÚquery_statesÚ
key_statesÚvalue_statesÚattention_interfacer~   r}   s               r-   r4   zVivitAttention.forwardÀ   sK  € ð $×)Ñ)¨#¨2Ð.ˆØ8˜Ð8 bÐ8¨$¯-©-Ñ8ˆà—{‘{ =Ó1×6Ñ6°|ÓD×NÑNÈqÐRSÓTˆØ—[‘[ Ó/×4Ñ4°\ÓB×LÑLÈQÐPQÓRˆ
Ø—{‘{ =Ó1×6Ñ6°|ÓD×NÑNÈqÐRSÓTˆä(?×(MÑ(MØ�K‰K×,Ñ,Ô.Eó)
Ðñ %8ØØØØØð	%
ð  $Ÿ}š}‘C°$×2HÑ2HØ—L‘Lñ	%
ð ñ	%
Ñ!ˆ�\ð *�k×)Ñ)Ð;¨;Ð;¸Ò;×FÑFÓHˆØ—k‘k +Ó.ˆà˜LÐ(Ð(r.   ©N)r5   r6   r7   r   r!   r9   r:   r   r   Útupler4   r;   r<   s   @r-   r�   r�   ±   sf   ø„ ðk˜{õ kð" /3ñ)à—|‘|ð)ð Ÿ™ tÑ+ð)ð Ð+Ñ,ð	)ð
 
ˆu�|‰|˜UŸ\™\Ð)Ñ	*÷)r.   r�   c                   ó\   ‡ — e Zd Zdefˆ fd„Zdej                  dej                  fd„Zˆ xZS )ÚVivitMLPr   c                 ó  •— t         ‰| �  «        || _        t        |j                     | _        t        j                  |j                  |j                  «      | _
        t        j                  |j                  |j                  «      | _        y rœ   )r    r!   r   r   Ú
hidden_actÚactivation_fnr   rŠ   r)   Úintermediate_sizeÚfc1Úfc2r�   s     €r-   r!   zVivitMLP.__init__ã   sd   ø€ Ü‰ÑÔØˆŒÜ# F×$5Ñ$5Ñ6ˆÔÜ—9‘9˜V×/Ñ/°×1IÑ1IÓJˆŒÜ—9‘9˜V×5Ñ5°v×7IÑ7IÓJˆ�r.   r‘   r0   c                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S rœ   )r¤   r¢   r¥   )r+   r‘   s     r-   r4   zVivitMLP.forwardê   s4   € ØŸ™ Ó/ˆØ×*Ñ*¨=Ó9ˆØŸ™ Ó/ˆàÐr.   ©	r5   r6   r7   r   r!   r9   r:   r4   r;   r<   s   @r-   rŸ   rŸ   â   s,   ø„ ðK˜{õ Kð U§\¡\ð °e·l±l÷ r.   rŸ   c            	       óˆ   ‡ — e Zd Zdefˆ fd„Z	 d	dej                  dej                  dz  dee   dej                  fd„Z	ˆ xZ
S )
Ú
VivitLayerr   c                 ój  •— t         ‰| �  «        t        |«      | _        t	        j
                  |j                  |j                  ¬«      | _        t	        j
                  |j                  |j                  ¬«      | _	        t        |«      | _        t	        j                  |j                  «      | _        y )N©Úeps)r    r!   r�   Ú	attentionr   Ú	LayerNormr)   Úlayer_norm_epsÚlayernorm_beforeÚlayernorm_afterrŸ   ÚmlprE   rF   rG   r�   s     €r-   r!   zVivitLayer.__init__ó   sz   ø€ Ü‰ÑÔÜ'¨Ó/ˆŒÜ "§¡¨V×-?Ñ-?ÀV×EZÑEZÔ [ˆÔÜ!Ÿ|™|¨F×,>Ñ,>ÀF×DYÑDYÔZˆÔÜ˜FÓ#ˆŒÜ—z‘z &×"<Ñ"<Ó=ˆ�r.   Nr‘   rp   rr   r0   c                 óú   — |}| j                  |«      } | j                  ||fi |¤Ž\  }}| j                  |«      }||z   }|}| j                  |«      }| j	                  |«      }| j                  |«      }||z   }|S rœ   )r°   r­   rG   r±   r²   )r+   r‘   rp   rr   ÚresidualÚ_s         r-   r4   zVivitLayer.forwardû   s�   € ð !ˆØ×-Ñ-¨mÓ<ˆØ)˜4Ÿ>™>¨-¸ÑRÈ6ÑRÑˆ�qØŸ™ ]Ó3ˆØ%¨Ñ0ˆð !ˆØ×,Ñ,¨]Ó;ˆØŸ™ Ó/ˆØŸ™ ]Ó3ˆØ%¨Ñ0ˆàÐr.   rœ   )r5   r6   r7   r   r!   r9   r:   r   r   r4   r;   r<   s   @r-   r©   r©   ò   sV   ø„ ð>˜{õ >ð /3ñà—|‘|ðð Ÿ™ tÑ+ðð Ð+Ñ,ð	ð
 
�‰÷r.   r©   c                   ó\   ‡ — e Zd Zdefˆ fd„Zdej                  dej                  fd„Zˆ xZS )ÚVivitPoolerr   c                 ó°   •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        |j                     | _	        y rœ   )
r    r!   r   rŠ   r)   Úpooler_output_sizeÚdenser   Ú
pooler_actÚ
activationr�   s     €r-   r!   zVivitPooler.__init__  s>   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3LÑ3LÓMˆŒ
Ü  ×!2Ñ!2Ñ3ˆ�r.   r‘   r0   c                 ó\   — |d d …df   }| j                  |«      }| j                  |«      }|S )Nr   )rº   r¼   )r+   r‘   Úfirst_token_tensorÚpooled_outputs       r-   r4   zVivitPooler.forward  s6   € ð +ª1¨a¨4Ñ0ÐØŸ
™
Ð#5Ó6ˆØŸ™¨Ó6ˆØÐr.   r§   r<   s   @r-   r·   r·     s*   ø„ ð4˜{õ 4ð
 U§\¡\ð °e·l±l÷ r.   r·   c                   óŒ   ‡ — e Zd ZU eed<   dZdZdZdZddgZ	dZ
dZdZdZdZeedœZd	Z ej(                  «       ˆ fd
„«       Zˆ xZS )ÚVivitPreTrainedModelr   Úvivitr/   )ÚvideoTr>   r©   )r‘   Ú
attentionsrC   c                 óÂ   •— t         ‰| �  |«       t        |t        «      r?t	        j
                  |j                  «       t	        j
                  |j                  «       yy)zInitialize the weightsN)r    Ú_init_weightsr$   r>   ÚinitÚzeros_rB   rD   )r+   rl   r,   s     €r-   rÆ   z"VivitPreTrainedModel._init_weights4  sE   ø€ ô 	‰Ñ˜fÔ%Ü�fœoÔ.Ü�K‰K˜×(Ñ(Ô)Ü�K‰K˜×2Ñ2Õ3ð /r.   )r5   r6   r7   r   Ú__annotations__Úbase_model_prefixÚmain_input_nameÚinput_modalitiesÚsupports_gradient_checkpointingÚ_no_split_modulesÚ_supports_sdpaÚ_supports_flash_attnÚ_supports_flex_attnÚ_supports_attention_backendÚ_can_compile_fullgraphr©   r�   Ú_can_record_outputsÚ_input_embed_layerr9   Úno_gradrÆ   r;   r<   s   @r-   rÁ   rÁ   !  sx   ø… àÓØÐØ$€OØ!ÐØ&*Ð#Ø*¨LÐ9ÐØ€NØÐØÐØ"&ÐØ!Ðà#Ø$ñÐð ,Ðà€U‡]�]ƒ_ó4ó ô4r.   rÁ   c                   ó´   ‡ — e Zd Zddedefˆ fd„Ze ed¬«      e	 	 	 dde	j                  dz  ded	e	j                  dz  d
ee   def
d„«       «       «       Zˆ xZS )Ú
VivitModelr   Úadd_pooling_layerc                 óŒ  •— t         ‰| �  |«       || _        t        |«      | _        t        j                  t        |j                  «      D �cg c]  }t        |«      ‘Œ c}«      | _
        t        j                  |j                  |j                  ¬«      | _        |rt        |«      nd| _        | j#                  «        yc c}w )zv
        add_pooling_layer (bool, *optional*, defaults to `True`):
            Whether to add a pooling layer
        r«   N)r    r!   r   r>   rI   r   Ú
ModuleListÚrangeÚnum_hidden_layersr©   Úlayersr®   r)   r¯   Ú	layernormr·   ÚpoolerÚ	post_init)r+   r   rÙ   rµ   r,   s       €r-   r!   zVivitModel.__init__?  sŽ   ø€ ô
 	‰Ñ˜Ô ØˆŒÜ)¨&Ó1ˆŒÜ—m‘mÄÀv×G_ÑG_ÓA`Ö$a¸A¤Z°Õ%7Ò$aÓbˆŒÜŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒÙ->”k &Ô)ÀDˆŒà�‰Õùò	 %bs   ÁCF)Útie_last_hidden_statesNr/   rc   rp   rr   r0   c                 ó  — | j                  ||¬«      }t        | j                  ||¬«      }|}| j                  D ]  } |||fi |¤Ž}Œ | j	                  |«      }| j
                  �| j                  |«      nd}	t        ||	¬«      S )a  
        Examples:

        ```python
        >>> import av
        >>> import numpy as np

        >>> from transformers import VivitImageProcessor, VivitModel
        >>> from huggingface_hub import hf_hub_download

        >>> np.random.seed(0)


        >>> def read_video_pyav(container, indices):
        ...     '''
        ...     Decode the video with PyAV decoder.
        ...     Args:
        ...         container (`av.container.input.InputContainer`): PyAV container.
        ...         indices (`list[int]`): List of frame indices to decode.
        ...     Returns:
        ...         result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3).
        ...     '''
        ...     frames = []
        ...     container.seek(0)
        ...     start_index = indices[0]
        ...     end_index = indices[-1]
        ...     for i, frame in enumerate(container.decode(video=0)):
        ...         if i > end_index:
        ...             break
        ...         if i >= start_index and i in indices:
        ...             frames.append(frame)
        ...     return np.stack([x.to_ndarray(format="rgb24") for x in frames])


        >>> def sample_frame_indices(clip_len, frame_sample_rate, seg_len):
        ...     '''
        ...     Sample a given number of frame indices from the video.
        ...     Args:
        ...         clip_len (`int`): Total number of frames to sample.
        ...         frame_sample_rate (`int`): Sample every n-th frame.
        ...         seg_len (`int`): Maximum allowed index of sample's last frame.
        ...     Returns:
        ...         indices (`list[int]`): List of sampled frame indices
        ...     '''
        ...     converted_len = int(clip_len * frame_sample_rate)
        ...     end_idx = np.random.randint(converted_len, seg_len)
        ...     start_idx = end_idx - converted_len
        ...     indices = np.linspace(start_idx, end_idx, num=clip_len)
        ...     indices = np.clip(indices, start_idx, end_idx - 1).astype(np.int64)
        ...     return indices


        >>> # video clip consists of 300 frames (10 seconds at 30 FPS)
        >>> file_path = hf_hub_download(
        ...     repo_id="nielsr/video-demo", filename="eating_spaghetti.mp4", repo_type="dataset"
        ... )
        >>> container = av.open(file_path)

        >>> # sample 32 frames
        >>> indices = sample_frame_indices(clip_len=32, frame_sample_rate=1, seg_len=container.streams.video[0].frames)
        >>> video = read_video_pyav(container=container, indices=indices)

        >>> image_processor = VivitImageProcessor.from_pretrained("google/vivit-b-16x2-kinetics400")
        >>> model = VivitModel.from_pretrained("google/vivit-b-16x2-kinetics400")

        >>> # prepare video for the model
        >>> inputs = image_processor(list(video), return_tensors="pt")

        >>> # forward pass
        >>> outputs = model(**inputs)
        >>> last_hidden_states = outputs.last_hidden_state
        >>> list(last_hidden_states.shape)
        [1, 3137, 768]
        ```)rc   )r   Úinputs_embedsrp   N)Úlast_hidden_stateÚpooler_output)rI   r	   r   rÞ   rß   rà   r   )
r+   r/   rc   rp   rr   Úembedding_outputr‘   ÚlayerÚsequence_outputr¿   s
             r-   r4   zVivitModel.forwardM  s“   € ðj  Ÿ?™?¨<ÐRj˜?ÓkÐÜ2Ø—;‘;Ø*Ø)ô
ˆð
 )ˆØ—[‘[ò 	KˆEÙ! -°ÑJÀ6ÑJ‰Mð	KàŸ.™.¨Ó7ˆØ8<¿¹Ð8O˜Ÿ™ OÔ4ÐUYˆä)¸OÐ[hÔiÐir.   )T)NFN)r5   r6   r7   r   rk   r!   r   r   r   r9   ÚFloatTensorr:   r   r   r   r4   r;   r<   s   @r-   rØ   rØ   =  s¢   ø„ ñ˜{ð ¸tõ ð  Ù¨EÔ2Øð 26Ø).Ø.2ñ	^jà×'Ñ'¨$Ñ.ð^jð #'ð^jð Ÿ™ tÑ+ð	^jð
 Ð+Ñ,ð^jð 
$ò^jó ó 3ó  ô^jr.   rØ   aý  
        ViViT Transformer model with a video classification head on top (a linear layer on top of the final hidden state of the
    [CLS] token) e.g. for Kinetics-400.

        <Tip>

            Note that it's possible to fine-tune ViT 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>
    )Úcustom_introc                   ó–   ‡ — e Zd Zdefˆ fd„Zee	 	 	 d
dej                  dz  dej                  dz  de
dee   def
d	„«       «       Zˆ xZS )ÚVivitForVideoClassificationr   c                 ó.  •— t         ‰| �  |«       |j                  | _        t        |d¬«      | _        |j                  dkD  r*t        j                  |j                  |j                  «      nt        j                  «       | _	        | j                  «        y )NF)rÙ   r   )r    r!   Ú
num_labelsrØ   rÂ   r   rŠ   r)   ÚIdentityÚ
classifierrá   r�   s     €r-   r!   z$VivitForVideoClassification.__init__À  ss   ø€ Ü‰Ñ˜Ô à ×+Ñ+ˆŒÜ ¸%Ô@ˆŒ
ð OU×N_ÑN_ÐbcÒNcœ"Ÿ)™) F×$6Ñ$6¸×8IÑ8IÔJÔik×itÑitÓivˆŒð 	�‰Õr.   Nr/   Úlabelsrc   rr   r0   c                 ó  —  | j                   |fd|i|¤Ž}|j                  }| j                  |dd…ddd…f   «      }d}|� | j                  ||| j                  fi |¤Ž}t        |||j                  |j                  ¬«      S )a  
        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
        >>> import av
        >>> import numpy as np
        >>> import torch

        >>> from transformers import VivitImageProcessor, VivitForVideoClassification
        >>> from huggingface_hub import hf_hub_download

        >>> np.random.seed(0)


        >>> def read_video_pyav(container, indices):
        ...     '''
        ...     Decode the video with PyAV decoder.
        ...     Args:
        ...         container (`av.container.input.InputContainer`): PyAV container.
        ...         indices (`list[int]`): List of frame indices to decode.
        ...     Returns:
        ...         result (np.ndarray): np array of decoded frames of shape (num_frames, height, width, 3).
        ...     '''
        ...     frames = []
        ...     container.seek(0)
        ...     start_index = indices[0]
        ...     end_index = indices[-1]
        ...     for i, frame in enumerate(container.decode(video=0)):
        ...         if i > end_index:
        ...             break
        ...         if i >= start_index and i in indices:
        ...             frames.append(frame)
        ...     return np.stack([x.to_ndarray(format="rgb24") for x in frames])


        >>> def sample_frame_indices(clip_len, frame_sample_rate, seg_len):
        ...     '''
        ...     Sample a given number of frame indices from the video.
        ...     Args:
        ...         clip_len (`int`): Total number of frames to sample.
        ...         frame_sample_rate (`int`): Sample every n-th frame.
        ...         seg_len (`int`): Maximum allowed index of sample's last frame.
        ...     Returns:
        ...         indices (`list[int]`): List of sampled frame indices
        ...     '''
        ...     converted_len = int(clip_len * frame_sample_rate)
        ...     end_idx = np.random.randint(converted_len, seg_len)
        ...     start_idx = end_idx - converted_len
        ...     indices = np.linspace(start_idx, end_idx, num=clip_len)
        ...     indices = np.clip(indices, start_idx, end_idx - 1).astype(np.int64)
        ...     return indices


        >>> # video clip consists of 300 frames (10 seconds at 30 FPS)
        >>> file_path = hf_hub_download(
        ...     repo_id="nielsr/video-demo", filename="eating_spaghetti.mp4", repo_type="dataset"
        ... )
        >>> container = av.open(file_path)

        >>> # sample 32 frames
        >>> indices = sample_frame_indices(clip_len=32, frame_sample_rate=4, seg_len=container.streams.video[0].frames)
        >>> video = read_video_pyav(container=container, indices=indices)

        >>> image_processor = VivitImageProcessor.from_pretrained("google/vivit-b-16x2-kinetics400")
        >>> model = VivitForVideoClassification.from_pretrained("google/vivit-b-16x2-kinetics400")

        >>> inputs = image_processor(list(video), return_tensors="pt")

        >>> with torch.no_grad():
        ...     outputs = model(**inputs)
        ...     logits = outputs.logits

        >>> # model predicts one of the 400 Kinetics-400 classes
        >>> predicted_label = logits.argmax(-1).item()
        >>> print(model.config.id2label[predicted_label])
        LABEL_116
        ```rc   Nr   )ÚlossÚlogitsr‘   rÄ   )rÂ   rå   rñ   Úloss_functionr   r   r‘   rÄ   )	r+   r/   rò   rc   rr   Úoutputsré   rõ   rô   s	            r-   r4   z#VivitForVideoClassification.forwardÌ  s�   € ðx $. 4§:¡:Øñ$
Ø3Kð$
ØOUñ$
ˆð "×3Ñ3ˆØ—‘ ²°A²q°Ñ!9Ó:ˆàˆØÐØ%�4×%Ñ% f¨f°d·k±kÑLÀVÑLˆDä$ØØØ!×/Ñ/Ø×)Ñ)ô	
ð 	
r.   )NNF)r5   r6   r7   r   r!   r   r   r9   rê   Ú
LongTensorrk   r   r   r   r4   r;   r<   s   @r-   rí   rí   ±  s‰   ø„ ð
˜{õ 
ð Øð 26Ø*.Ø).ñ	i
à×'Ñ'¨$Ñ.ði
ð × Ñ  4Ñ'ði
ð #'ð	i
ð
 Ð+Ñ,ði
ð 
òi
ó ó ôi
r.   rí   )rØ   rÁ   rí   )Nr“   )0Úcollections.abcr   r   r9   r   Ú r   rÇ   Úactivationsr   Úmasking_utilsr	   Úmodeling_layersr
   Úmodeling_outputsr   r   r   Úmodeling_utilsr   r   Úprocessing_utilsr   Úutilsr   r   r   Úutils.genericr   r   Úutils.output_capturingr   Úconfiguration_vivitr   ÚModuler   r>   r:   Úfloatr   r�   rŸ   r©   r·   rÁ   rØ   rí   Ú__all__© r.   r-   ú<module>r	     s‡  ð÷* /ã Ý å &Ý !Ý 6Ý 9ß bÑ bß FÝ &ß BÑ Bß IÝ 5Ý ,ôH˜RŸY™Yô Hô>L�b—i‘iô Lðj !Øñ%Ø�I‰Ið%à�<‰<ð%ð 
�‰ð%ð �<‰<ð	%ð
 —L‘L 4Ñ'ð%ð �T‰\ð%ð ð%ð Ð'Ñ(ó%ô8.)�R—Y‘Yô .)ôbˆr�y‰yô ô Ð+ô ô@�"—)‘)ô ð ô4˜?ó 4ó ð4ð6 ôpjÐ%ó pjó ðpjñf ðôôx
Ð"6ó x
óðx
òv P�r.   