CoolFace
Apppublic

XaviXva/Video-LLaVA

sourceHugging Faceapache-2.0updated 3y agoView on Hugging Face
0likes
modeling_video.cpython-310.pyc439 linesDownload Raw Back to __pycache__
1o

2،8eγ�	@s�ddlZddlmZmZmZddlZddlmZddlm	Z	m3Z4ddlmZddlm
ZddlmZmZddlmZmZdd	lmZmZmZmZmZmZmZmZmZdd5lm Z m!Z!ddl"m#Z#m$Z$m%Z%Gd
d�dej&�Z'Gdd�dej&�Z(Gdd�de�Z)dZ*dZ+dZ,dZ-Gdd�dej&�Z.	d+dej/dej0dej1de2fdd�Z3Gdd �d ej&�Z4ed!e*�Gd"d#�d#e)��Z5Gd$d%�d%ej&�Z6ed&e*�Gd'd(�d(e)��Z7ee*�Gd)d*�d*e)��Z8dS),�N)�Optional�Tuple�Union��	rearrange)�6LoraConfig�get_peft_model)�nn)�7functional)�PreTrainedModel�add_start_docstrings)�BaseModelOutput�BaseModelOutputWithPooling)	�CLIPMLP�
CLIPAttention�CLIPTextEmbeddings�CLIPVisionEmbeddings�CLIPVisionModelWithProjection�CLIPTextModelWithProjection�_expand_mask�8CLIPOutput�	clip_loss)�%add_start_docstrings_to_model_forward�replace_return_docstrings�)�LanguageBindVideoConfig�CLIPVisionConfig�CLIPTextConfigcs*eZdZdZd�fdd�	Zdd�Z�ZS)�PatchDropoutz*9    https://arxiv.org/abs/2212.0079410    Tcs6t���d|krdksJ�J�||_||_dS)Nr��?)�super�__init__�prob�exclude_first_token)�selfr"r#��	__class__��d/scc_cephfs/yy/lb/LLaVA-Video-YY/llava/model/multimodal_encoder/languagebind/video/modeling_video.pyr!s1112zPatchDropout.__init__cCs4|jr|jdkr13|S|jr#|dd�dd�f|dd�dd�f}}ntj�tj|dd�dd�f�}|��d}|��d}t�|�}|d}d|j}t	dt14||��}	|dkrit�||�}15|16j|	dd�j
}nt�||�}17|18j|	dd�j
}|�d��d|d�}t|d�}|||f}|jr�tj||fdd�}|S)N�rr).N�������dimzb t n -> (b t) n)�trainingr"r#�torch�jit�annotate�Tensor�size�arange�max�int�randn�topk�indices�	unsqueeze�repeatr�cat)r$�x�B�T�19cls_tokens�batch�20num_tokens�
batch_indices�	keep_prob�num_patches_keep�rand�patch_indices_keepr'r'r(�forwards,, 212223zPatchDropout.forward)T)�__name__�24__module__�__qualname__�__doc__r!rG�
__classcell__r'r'r%r(rsrcsTeZdZdef�fdd�Z	ddejdejdejdeed	e	ej25f26d27d�Z�ZS)
�CLIPEncoderLayer�configcs�t���|j|_t|�|_tj|j|jd�|_	t28|�|_tj|j|jd�|_|j
|_
|j
r`|j|_t�t�d|j|j��|_tjj|j|jdd�|j|_t|�|_tj|j|jd�|_dSdS)N��epsr�࿩�std)r r!�hidden_size�	embed_dimr�	self_attnr	�	LayerNorm�layer_norm_eps�layer_norm1r�mlp�layer_norm2�
add_time_attn�29num_frames�t�	Parameterr.�zeros�temporal_embedding�init�normal_�
temporal_attn�temporal_layer_norm1�r$rNr%r'r(r!Bs30313233�zCLIPEncoderLayer.__init__F�
hidden_states�attention_mask�causal_attention_mask�output_attentions�returncCs|jrU|j\}}}|j}|dkr3|jd}t|d|d�}||jdd�d|�dd�f}t|d|d�}|}	t|d|d�}|�|�}|j||||d�\}}34|	t|d|d�}|}	|�|�}|j||||d�\}}35|	|}|}	|�	|�}|�36|�}|	|}|f}|r�||37f7}|S)aI38        Args:39            hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`40            attention_mask (`torch.FloatTensor`): attention mask of size41                `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.42                `(config.encoder_attention_heads,)`.43            output_attentions (`bool`, *optional*):44                Whether or not to return the attentions tensors of all attention layers. See `attentions` under45                returned tensors for more detail.46        rz(b t) n d -> (b n) t d)r^Nz(b n) t d -> (b t) n d)�n)rgrhrirj)r\�shaper^rrarerdrYrVr[rZ)r$rgrhrirj�btrl�dr^�residual�attn_weights�outputsr'r'r(rGVsF47 4849�50 5152�535455zCLIPEncoderLayer.forward�F)
rHrIrJrr!r.r1r�boolr�FloatTensorrGrLr'r'r%r(rMAs������rMc@s4eZdZdZeZdZdZdgZdd�Z	ddd	�Z56d57S)�CLIPPreTrainedModelz�58    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained59    models.60    �clipT�position_idscCs�|jj}t|t�r#|jjjjd|dd�|jjjjd|dd��nt|t	�rX|jj}t61jj|jd|j
d|d�t62jj|jj|jj|d�t63jj|jj|jj|d�n�t|t�r�|jj}|j
dd|jjd|}|j
d|}t64jj|jj|d�t65jj|jj|d�t66jj|jj|d�t67jj|jj|d�n�t|t�r�|jj}|jjdd|jjd|}d|jjd|}t68jj|jj|d�t69jj|jj|d�nZt|t��rt70jj|jj|jd|jjd�t71jj|jj|jd|jjd�n1t|t��rt72jj|jj|jjd|jjd�nt|t ��r2t73jj|jj|jjd|jjd�t|t74j!��rF|j"j�#�|jj�$d�t|t75j%��r[|j"du�r]|j"j�#�dSdSdS)	zInitialize the weightsr)g{�G�z�?)�meanrSrQrR�rN)&rN�initializer_factor�76isinstancer�token_embedding�weight�datarc�position_embeddingrr	rb�class_embeddingrU�patch_embedding�initializer_ranger�num_hidden_layers�q_proj�k_proj�v_proj�out_projrrT�fc1�fc2�LanguageBindVideo�text_projection�text_embed_dim�visual_projection�vision_embed_dimrrrW�bias�zero_�fill_�Linear)r$�module�factor�in_proj_std�out_proj_std�fc_stdr'r'r(�
_init_weights�sb77787980������z!CLIPPreTrainedModel._init_weightsFcCst|t�r81||_dSdS�N)r|�CLIPEncoder�gradient_checkpointing)r$r��valuer'r'r(�_set_gradient_checkpointing�s8283�z/CLIPPreTrainedModel._set_gradient_checkpointingNrs)rHrIrJrKr�config_class�base_model_prefix�supports_gradient_checkpointing�_keys_to_ignore_on_load_missingr�r�r'r'r'r(rv�s5rva=84    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the85    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads86    etc.)87 88    This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.89    Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage90    and behavior.91 92    Parameters:93        config ([`CLIPConfig`]): Model configuration class with all the parameters of the model.94            Initializing with a config file does not load the weights associated with the model, only the95            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.96a�97    Args:98        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):99            Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide100            it.101 102            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and103            [`PreTrainedTokenizer.__call__`] for details.104 105            [What are input IDs?](../glossary#input-ids)106        attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):107            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:108 109            - 1 for tokens that are **not masked**,110            - 0 for tokens that are **masked**.111 112            [What are attention masks?](../glossary#attention-mask)113        position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):114            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,115            config.max_position_embeddings - 1]`.116 117            [What are position IDs?](../glossary#position-ids)118        output_attentions (`bool`, *optional*):119            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned120            tensors for more detail.121        output_hidden_states (`bool`, *optional*):122            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for123            more detail.124        return_dict (`bool`, *optional*):125            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.126aE127    Args:128        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):129            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using130            [`AutoImageProcessor`]. See [`CLIPImageProcessor.__call__`] for details.131        output_attentions (`bool`, *optional*):132            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned133            tensors for more detail.134        output_hidden_states (`bool`, *optional*):135            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for136            more detail.137        return_dict (`bool`, *optional*):138            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.139a140    Args:141        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):142            Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide143            it.144 145            Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and146            [`PreTrainedTokenizer.__call__`] for details.147 148            [What are input IDs?](../glossary#input-ids)149        attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):150            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:151 152            - 1 for tokens that are **not masked**,153            - 0 for tokens that are **masked**.154 155            [What are attention masks?](../glossary#attention-mask)156        position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):157            Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,158            config.max_position_embeddings - 1]`.159 160            [What are position IDs?](../glossary#position-ids)161        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):162            Pixel values. Padding will be ignored by default should you provide it. Pixel values can be obtained using163            [`AutoImageProcessor`]. See [`CLIPImageProcessor.__call__`] for details.164        return_loss (`bool`, *optional*):165            Whether or not to return the contrastive loss.166        output_attentions (`bool`, *optional*):167            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned168            tensors for more detail.169        output_hidden_states (`bool`, *optional*):170            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for171            more detail.172        return_dict (`bool`, *optional*):173            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.174csteZdZdZdef�fdd�Z					ddeejdeejdee	d	ee	d175ee	de176eeffdd
�Z
�ZS)r�z�177    Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a178    [`CLIPEncoderLayer`].179 180    Args:181        config: CLIPConfig182    rNcs:t����|_t��fdd�t�j�D��|_d|_dS)Ncsg|]}t���qSr')rM)�.0�_�rNr'r(�183<listcomp>]sz(CLIPEncoder.__init__.<locals>.<listcomp>F)	r r!rNr	�184ModuleList�ranger��layersr�rfr%r�r(r!Zs185 186zCLIPEncoder.__init__Nrhrirj�output_hidden_states�return_dictrkcs�dur�n|jj�|dur|n|jj}|dur|n|jj}|r"dnd}�r(dnd}|}	t|j�D]9\}187}|r<||	f}|jrU|jrU�fdd�}tj	j188�189||�|	||�}
n||	||�d�}
|
d}	�rj||
df}q1|rr||	f}|s�tdd	�|	||fD��St|	||d190�S)a�191        Args:192            inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):193                Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation.194                This is useful if you want more control over how to convert `input_ids` indices into associated vectors195                than the model's internal embedding lookup matrix.196            attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):197                Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:198 199                - 1 for tokens that are **not masked**,200                - 0 for tokens that are **masked**.201 202                [What are attention masks?](../glossary#attention-mask)203            causal_attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):204                Causal mask for the text model. Mask values selected in `[0, 1]`:205 206                - 1 for tokens that are **not masked**,207                - 0 for tokens that are **masked**.208 209                [What are attention masks?](../glossary#attention-mask)210            output_attentions (`bool`, *optional*):211                Whether or not to return the attentions tensors of all attention layers. See `attentions` under212                returned tensors for more detail.213            output_hidden_states (`bool`, *optional*):214                Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors215                for more detail.216            return_dict (`bool`, *optional*):217                Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.218        Nr'cs��fdd�}|S)Ncs�g|���R�Sr�r')�inputs)r�rjr'r(�custom_forward�szJCLIPEncoder.forward.<locals>.create_custom_forward.<locals>.custom_forwardr')r�r��rj)r�r(�create_custom_forward�sz2CLIPEncoder.forward.<locals>.create_custom_forwardr�rrcss�|]	}|dur|VqdSr�r')r��vr'r'r(�	<genexpr>�s�z&CLIPEncoder.forward.<locals>.<genexpr>)�last_hidden_staterg�219attentions)
rNrjr��use_return_dict�	enumerater�r�r-r.�utils�220checkpoint�tupler
)r$�
inputs_embedsrhrirjr�r��encoder_states�all_attentionsrg�idx�
encoder_layerr��
layer_outputsr'r�r(rG`sF&�221���222�zCLIPEncoder.forward)NNNNN)rHrIrJrKrr!rr.r1rtrrr
rGrLr'r'r%r(r�Qs*	������223�r��input_ids_shape�dtype�device�past_key_values_lengthcCs�|\}}tj||ft�|�j|d�}tj|�d�|d�}|�||d�|�d�d�kd�|�|�}|dkrFtj	tj224||||d�|gdd�}|dddd�dd�f�|d|||�S)zB225    Make causal mask used for bi-directional self-attention.226    �r�r*rr�r�r�r+N)r.�full�finfo�minr3r2�masked_fill_�view�tor;r`�expand)r�r�r�r��bsz�tgt_len�mask�	mask_condr'r'r(�_make_causal_mask�s"227 (r�cs�eZdZdef�fdd�Zee�eeed�						dde	e228jde	e229jde	e230jd	e	ed231e	ede	ede
eeffd
d���Z�ZS)�CLIPTextTransformerrNcs@t���||_|j}t|�|_t|�|_tj	||j232d�|_dS�NrO)r r!rNrTr�233embeddingsr��encoderr	rWrX�final_layer_norm�r$rNrUr%r'r(r!�s234235236zCLIPTextTransformer.__init__��output_typer�N�	input_idsrhrxrjr�r�rkc
Cs |dur|n|jj}|dur|n|jj}|dur|n|jj}|dur&td��|��}|�d|d�}|j||d�}t||j	|j237d�}	|durLt||j	�}|j|||	|||d�}238|239d}|�
|�}|tj|jd|j240d�|jtj|j241d�jdd	�f}|s�||f|242d243d�St|||244j|245jd�S)�246        Returns:247 248        NzYou have to specify input_idsr*)r�rxr�)r�rhrirjr�r�rr�r+r�r��
pooler_outputrgr�)rNrjr�r��249ValueErrorr2r�r�r�r�r�rr�r�r.r3rmr�r5�argmaxrrgr�)
r$r�rhrxrjr�r��input_shapergri�encoder_outputsr��
pooled_outputr'r'r(rG�sF��	250���zCLIPTextTransformer.forward�NNNNNN)rHrIrJrr!r�CLIP_TEXT_INPUTS_DOCSTRINGrrrr.r1rtrrrGrLr'r'r%r(r��s2251�������252�r�z?The text model from CLIP without any head or projection on top.cs�eZdZeZdgZdef�fdd�Zdejfdd�Z	dd	�Z253ee�e
eed254�						ddeejd
eejdeejdeedeedeedeeeffdd���Z�ZS)�
CLIPTextModelrMrNc�"t��|�t|�|_|��dSr�)r r!r��255text_model�	post_initrfr%r'r(r! �256zCLIPTextModel.__init__rkcC�257|jjjSr��r�r�r}�r$r'r'r(�get_input_embeddings&�258z"CLIPTextModel.get_input_embeddingscCs||jj_dSr�r�)r$r�r'r'r(�set_input_embeddings)sz"CLIPTextModel.set_input_embeddingsr�Nr�rhrxrjr�r�cCs*|dur|n|jj}|j||||||d�S)aK259        Returns:260 261        Examples:262 263        ```python264        >>> from transformers import AutoTokenizer, CLIPTextModel265 266        >>> model = CLIPTextModel.from_pretrained("openai/clip-vit-base-patch32")267        >>> tokenizer = AutoTokenizer.from_pretrained("openai/clip-vit-base-patch32")268 269        >>> inputs = tokenizer(["a photo of a cat", "a photo of a dog"], padding=True, return_tensors="pt")270 271        >>> outputs = model(**inputs)272        >>> last_hidden_state = outputs.last_hidden_state273        >>> pooled_output = outputs.pooler_output  # pooled (EOS token) states274        ```N�r�rhrxrjr�r�)rNr�r�)r$r�rhrxrjr�r�r'r'r(rG,s�zCLIPTextModel.forwardr�)rHrIrJrr��_no_split_modulesr!r	�Moduler�r�rr�rrrr.r1rtrrrGrLr'r'r%r(r�s:275�������276�r�csxeZdZdef�fdd�Zee�eeed�				d
de	e277jde	ede	ed	e	ed278e
eeff279dd���Z�ZS)�CLIPVisionTransformerrNcs^t���||_|j}t|�|_t|j�|_t	j280||jd�|_t
|�|_t	j281||jd�|_dSr�)r r!rNrTrr�r�force_patch_dropout�
patch_dropoutr	rWrX�pre_layrnormr�r��post_layernormr�r%r'r(r!Us282283284zCLIPVisionTransformer.__init__r�N�pixel_valuesrjr�r�rkcs~|dur|n|jj}|dur|n|jj}|dur|n|jj}|dur&td��t|j�dkrH|j\}}}}}	}285}|||�|��||	|286|�}nt|j�dkr]|j\�}}}}t|d�}n	|j\�}}}d}|�	|�}
|�287|
�|�}
|�|
�}
|j|
|||d�}|d}|dd�ddd�f}|�
|�}|��|d	��d�}�fd288d�|jD�|_|s�||f|dd�St|||j|jd�S)
r�Nz You have to specify pixel_values��zb c t h w -> (b t) c h wr)r�rjr�r�rr*csg|]	}t|d�d��qS)z(b t) n c -> b t n c)�br)r��i�r=r'r(r��sz1CLIPVisionTransformer.forward.<locals>.<listcomp>r�)rNrjr�r�r��lenrm�reshaperr�r�r�r�r�ryrgrr�)r$r�rjr�r��b_new�pair_newr>�bs_new�channel_new�h_new�w_newr�rgr�r�r�r'r�r(rG`sJ
�289290�291�zCLIPVisionTransformer.forward�NNNN)rHrIrJrr!r�CLIP_VISION_INPUTS_DOCSTRINGrrrr.rurtrrrGrLr'r'r%r(r�Ts&292�����293�r�zAThe vision model from CLIP without any head or projection on top.cs�eZdZeZdZdef�fdd�Zdejfdd�Z	e294e�ee
ed�								ddeejd295eedeedeedeee
ff296d
d���Z�ZS)�CLIPVisionModelr�rNcr�r�)r r!r��vision_modelr�rfr%r'r(r!�r�zCLIPVisionModel.__init__rkcCr�r�)rr�r�r�r'r'r(r��r�z$CLIPVisionModel.get_input_embeddingsr�Nrjr�r�cCs&|dur|n|jj}|j||||d�S)a�297        Returns:298 299        Examples:300 301        ```python302        >>> from PIL import Image303        >>> import requests304        >>> from transformers import AutoProcessor, CLIPVisionModel305 306        >>> model = CLIPVisionModel.from_pretrained("openai/clip-vit-base-patch32")307        >>> processor = AutoProcessor.from_pretrained("openai/clip-vit-base-patch32")308 309        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"310        >>> image = Image.open(requests.get(url, stream=True).raw)311 312        >>> inputs = processor(images=image, return_tensors="pt")313 314        >>> outputs = model(**inputs)315        >>> last_hidden_state = outputs.last_hidden_state316        >>> pooled_output = outputs.pooler_output  # pooled CLS states317        ```N�r�rjr�r�)rNr�r)r$r�rjr�r�r'r'r(rG�s�zCLIPVisionModel.forwardr)rHrIrJrr��main_input_namer!r	r�r�rrrrrr.rurtrrrGrLr'r'r%r(r�s,318�����319�rcsZeZdZeZdef�fdd�Zdd�Zdd�Zee	�						dd	e320ejd321e322ejde323ejde324e
d
e325e
de326e
dejfdd��Zee�				dde327ejde328e
d
e329e
de330e
dejf331dd��Zee�eeed�								dd	e332ejde333ejd334e335ejde336ejde337e
de338e
d
e339e
de340e
deeeffdd���Z�ZS)r�rNcs�t��|�t|jt�stdt|j��d���t|jt�s(tdt|j��d���|j}|j}|j	|_	|j341|_342|j|_|j|_|j
|_
|j|_|j|_t|�|_t|�|_tj|j|j
dd�|_tj|j|j
dd�|_t�t�|jj��|_|��dS)NzKconfig.text_config is expected to be of type CLIPTextConfig but is of type �.zOconfig.vision_config is expected to be of type CLIPVisionConfig but is of type F)r�) r r!r|�text_configrr��type�
vision_configrr\�lora_r�343lora_alpha�lora_dropout�projection_dimrTr�r�r�r�r�rr	r�r�r�r_r.�tensorrN�logit_scale_init_value�logit_scaler�)r$rNrrr%r'r(r!�s8����344345zLanguageBindVideo.__init__cCs`|jdkrdS|jrgd�}ngd�}t|j|j||jdgd�}d|jj_t|jj|�|j_dS)Nr)ztemporal_attn.k_projztemporal_attn.v_projztemporal_attn.q_projztemporal_attn.out_projztemporal_mlp.fc1ztemporal_mlp.fc2)r�r�r�r��none)�rr�target_modulesrr��modules_to_saveF)	rr\rrrrr��is_gradient_checkpointingr)r$rrNr'r'r(�convert_to_lora346s347348�349z!LanguageBindVideo.convert_to_loracCs�|jdkr|jdkr|j|jg|_t|jt�r|j|jgn|j|j_|j��}|d}|j}|jjd|j	|jjd|j	g}d}|d|d|}||j350dkrUdS|d|d|_|jd|_|�
dt�|j��d��t�|j|j�}	|r�|d|�||d�}351}nd|}352}tt�t|���gd}|�d|d|dd��dddd�}tj||d	d353dd�}|�dddd��d|d|dd�d}|354dur�tj|355|gdd
�}
n|}
|
�|�|d<|	|_|j�|�dS)Nrr~rrx)rr*rzr*��bicubicTF)r2�mode�	antialias�
align_cornersr+)�num_mel_bins�
target_length�356image_sizer|r5rNr��357state_dictr��358patch_sizerm�num_patches�
num_positions�register_bufferr.r3r�r	�	EmbeddingrU�math�sqrtr�r��permute�F�interpolater;r��load_state_dict)r$�mr�old_pos_embed_state_dict�
old_pos_embedr��	grid_size�extra_tokens�new_seq_len�new_position_embedding�pos_emb_tok�pos_emb_img�
old_grid_size�
new_pos_embedr'r'r(�359resize_possD"360$361$�*zLanguageBindVideo.resize_posNr�rhrxrjr�r�rkc362Csh|dur|n|jj}|dur|n|jj}|dur|n|jj}|j||||||d�}|d}|�|�}	|	S)a�363        Returns:364            text_features (`torch.FloatTensor` of shape `(batch_size, output_dim`): The text embeddings obtained by365            applying the projection layer to the pooled output of [`CLIPTextModel`].366 367        Examples:368 369        ```python370        >>> from transformers import AutoTokenizer, CLIPModel371 372        >>> model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")373        >>> tokenizer = AutoTokenizer.from_pretrained("openai/clip-vit-base-patch32")374 375        >>> inputs = tokenizer(["a photo of a cat", "a photo of a dog"], padding=True, return_tensors="pt")376        >>> text_features = model.get_text_features(**inputs)377        ```Nr�r)rNrjr�r�r�r�)378r$r�rhrxrjr�r��text_outputsr��
text_featuresr'r'r(�get_text_featuresNs��	379z#LanguageBindVideo.get_text_featuresr�cCsd|dur|n|jj}|dur|n|jj}|dur|n|jj}|j||||d�}|d}|�|�}|S)aD380        Returns:381            image_features (`torch.FloatTensor` of shape `(batch_size, output_dim`): The image embeddings obtained by382            applying the projection layer to the pooled output of [`CLIPVisionModel`].383 384        Examples:385 386        ```python387        >>> from PIL import Image388        >>> import requests389        >>> from transformers import AutoProcessor, CLIPModel390 391        >>> model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")392        >>> processor = AutoProcessor.from_pretrained("openai/clip-vit-base-patch32")393 394        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"395        >>> image = Image.open(requests.get(url, stream=True).raw)396 397        >>> inputs = processor(images=image, return_tensors="pt")398 399        >>> image_features = model.get_image_features(**inputs)400        ```Nr	r)rNrjr�r�rr�)r$r�rjr�r��vision_outputsr��image_featuresr'r'r(�get_image_features}s��401z$LanguageBindVideo.get_image_featuresr��return_lossc		Cs&|dur|n|jj}|dur|n|jj}|dur|n|jj}|j||||d�}	|j||||||d�}402|	d}|�|�}|403d}|�|�}||jdddd�}||jdddd�}|j	�404�}
t�||�
��|
}|�
�}d}|rst|�}|s�|||||405|	f}|dur�|f|S|St||||||406|	d	�S)407a�408        Returns:409 410        Examples:411 412        ```python413        >>> from PIL import Image414        >>> import requests415        >>> from transformers import AutoProcessor, CLIPModel416 417        >>> model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")418        >>> processor = AutoProcessor.from_pretrained("openai/clip-vit-base-patch32")419 420        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"421        >>> image = Image.open(requests.get(url, stream=True).raw)422 423        >>> inputs = processor(424        ...     text=["a photo of a cat", "a photo of a dog"], images=image, return_tensors="pt", padding=True425        ... )426 427        >>> outputs = model(**inputs)428        >>> logits_per_image = outputs.logits_per_image  # this is the image-text similarity score429        >>> probs = logits_per_image.softmax(dim=1)  # we can take the softmax to get the label probabilities430        ```Nr	r�rrzr*T)�pr,�keepdim)�loss�logits_per_image�logits_per_text�text_embeds�image_embeds�text_model_output�vision_model_output)rNrjr�r�rr�r�r��normr�expr.�matmulr^rr)r$r�r�rhrxrBrjr�r�r?r<rIrHrrGrFrE�outputr'r'r(rG�sT&���	431432433�zLanguageBindVideo.forwardr�r)NNNNNNNN)rHrIrJrr�r!rr;rr�rr.r1rtrur>rrA�CLIP_INPUTS_DOCSTRINGrr�434LongTensorrrrGrLr'r'r%r(r��s�&0��������.������0435��������	�436 437�r�)r)9r*�typingrrrr.�einopsr�peftrrr	�torch.nnr438r-�transformersrr�transformers.modeling_outputsr
rZ&transformers.models.clip.modeling_cliprrrrrrrrr�transformers.utilsrr�configuration_videorrrr�rrMrv�CLIP_START_DOCSTRINGr�rrPr��Sizer�r�r5r�r�r�r�rr�r'r'r'r(�<module>sX,.gE &i����439�M�9R�6