Ë
    ÛÓ^jÕç  ã                   óø  — d Z ddlmZ ddlmZ ddl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 ddlmZ ddlmZmZmZmZmZmZ ddlmZmZ ddl m!Z! ddl"m#Z# ddl$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* ddl+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1m2Z2m3Z3 ddl4m5Z5m6Z6m7Z7  e)jp                  e9«      Z:e'e G d„ de«      «       «       Z; e'd¬«      e G d„ de%«      «       «       Z< G d„ d ejz                  «      Z>	 dNd!ejz                  d"ej~                  d#ej~                  d$ej~                  d%ej~                  dz  d&e@d'e@fd(„ZA G d)„ d*ejz                  «      ZB G d+„ d,ejz                  «      ZC G d-„ d.e«      ZDe' G d/„ d0e«      «       ZE G d1„ d2ejz                  «      ZF G d3„ d4eE«      ZG G d5„ d6ejz                  «      ZH G d7„ d8ejz                  «      ZI G d9„ d:ejz                  «      ZJ G d;„ d<ejz                  «      ZK G d=„ d>ejz                  «      ZL G d?„ d@e«      ZM G dA„ dBejz                  «      ZN G dC„ dDejz                  «      ZO G dE„ dFeE«      ZP e'dG¬«       G dH„ dIeE«      «       ZQ e'dJ¬«       G dK„ dLeEe«      «       ZRg dM¢ZSy)OzPyTorch InstructBLIP model.é    )ÚCallable)Ú	dataclass)ÚAnyN)Únné   )Úinitialization)ÚACT2FN)ÚGenerationMixin)Úcreate_bidirectional_mask)ÚFlashAttentionKwargs)ÚGradientCheckpointingLayer)ÚBaseModelOutputÚ)BaseModelOutputWithPastAndCrossAttentionsÚBaseModelOutputWithPoolingÚ,BaseModelOutputWithPoolingAndCrossAttentionsÚCausalLMOutputWithPastÚSeq2SeqLMOutput)ÚALL_ATTENTION_FUNCTIONSÚPreTrainedModel)ÚUnpack)Úapply_chunking_to_forward)ÚModelOutputÚTransformersKwargsÚauto_docstringÚcan_return_tupleÚloggingÚ	torch_int)Úmerge_with_config_defaults)ÚOutputRecorderÚcapture_outputsé   )Ú	AutoModelÚAutoModelForCausalLMÚAutoModelForSeq2SeqLMé   )ÚInstructBlipConfigÚInstructBlipQFormerConfigÚInstructBlipVisionConfigc                   ó:   — e Zd ZU dZdZedz  ed<   dZedz  ed<   y)Ú'BaseModelOutputWithVisionQformerOutputszÝ
    vision_outputs (`BaseModelOutputWithPooling`):
        Outputs of the vision encoder.
    qformer_outputs (`BaseModelOutputWithPoolingAndCrossAttentions`):
        Outputs of the Q-Former (Querying Transformer).
    NÚvision_outputsÚqformer_outputs)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r+   r   Ú__annotations__r,   r   © ó    ú�/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/instructblip/modeling_instructblip.pyr*   r*   2   s)   … ñð 9=€NÐ.°Ñ5Ó<ØKO€OÐAÀDÑHÔOr3   r*   zQ
    Class defining the outputs of [`InstructBlipForConditionalGeneration`].
    )Úcustom_introc                   óÂ   — e Zd ZU dZdZeej                     dz  ed<   dZ	eej                     dz  ed<   dZ
edz  ed<   dZedz  ed<   dZeez  dz  ed<   dee   fd	„Zy)
Ú/InstructBlipForConditionalGenerationModelOutputaª  
    loss (`torch.FloatTensor`, *optional*, returned when `labels` is provided, `torch.FloatTensor` of shape `(1,)`):
        Language modeling loss from the language model.
    logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
        Prediction scores of the language modeling head of the language model.
    vision_outputs (`BaseModelOutputWithPooling`):
        Outputs of the vision encoder.
    qformer_outputs (`BaseModelOutputWithPoolingAndCrossAttentions`):
        Outputs of the Q-Former (Querying Transformer).
    language_model_outputs (`CausalLMOutputWithPast` or `Seq2SeqLMOutput`):
        Outputs of the language model.
    NÚlossÚlogitsr+   r,   Úlanguage_model_outputsÚreturnc                 óH   ‡ — t        ˆ fd„‰ j                  «       D «       «      S )Nc              3   ód   •K  — | ]'  }|d vr‰|   nt        ‰|«      j                  «       –— Œ) y­w)©r+   r,   r:   N)ÚgetattrÚto_tuple)Ú.0ÚkÚselfs     €r4   ú	<genexpr>zKInstructBlipForConditionalGenerationModelOutput.to_tuple.<locals>.<genexpr>\   sB   øè ø€ ò 
ð ð ÐWÑWð �ŠGä˜˜qÓ!×*Ñ*Ó,ó-ñ
ùs   ƒ-0)ÚtupleÚkeys©rC   s   `r4   r@   z8InstructBlipForConditionalGenerationModelOutput.to_tuple[   s%   ø€ Üó 
ð —Y‘Y“[ô	
ó 
ð 	
r3   )r-   r.   r/   r0   r8   rE   ÚtorchÚFloatTensorr1   r9   r+   r   r,   r   r:   r   r   r   r@   r2   r3   r4   r7   r7   @   sƒ   … ñð -1€Dˆ%�×!Ñ!Ñ
" TÑ
)Ó0Ø.2€FˆE�%×#Ñ#Ñ$ tÑ+Ó2Ø8<€NÐ.°Ñ5Ó<ØKO€OÐAÀDÑHÓOØNRÐÐ2°_ÑDÀtÑKÓRð
˜% ™*ô 
r3   r7   c                   ó¢   ‡ — e 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 )ÚInstructBlipVisionEmbeddingsÚconfigc                 óp  •— t         ‰| �  «        || _        |j                  | _        |j
                  | _        |j                  | _        t        j                  t        j                  dd| j                  «      «      | _        t        j                  d| j                  | j                  | j                  ¬«      | _        | j
                  | j                  z  dz  | _        | j                  dz   | _        t        j                  t        j                  d| j                  | j                  «      «      | _        y )Nr%   r   )Úin_channelsÚout_channelsÚkernel_sizeÚstrider!   )ÚsuperÚ__init__rL   Úhidden_sizeÚ	embed_dimÚ
image_sizeÚ
patch_sizer   Ú	ParameterrH   ÚrandnÚclass_embeddingÚConv2dÚpatch_embeddingÚnum_patchesÚnum_positionsÚposition_embedding©rC   rL   Ú	__class__s     €r4   rS   z%InstructBlipVisionEmbeddings.__init__f   sá   ø€ Ü‰ÑÔØˆŒØ×+Ñ+ˆŒØ ×+Ñ+ˆŒØ ×+Ñ+ˆŒä!Ÿ|™|¬E¯K©K¸¸1¸d¿n¹nÓ,MÓNˆÔä!Ÿy™yØ¨¯©ÀDÇOÁOÐ\`×\kÑ\kô 
ˆÔð !ŸO™O¨t¯©Ñ>À1ÑDˆÔØ!×-Ñ-°Ñ1ˆÔä"$§,¡,¬u¯{©{¸1¸d×>PÑ>PÐRV×R`ÑR`Ó/aÓ"bˆÕr3   Ú
embeddingsÚheightÚwidthr;   c                 ó¦  — |j                   d   dz
  }| 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
                  z  }	|| j
                  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éÿÿÿÿg      à?r   r   r!   ÚbicubicF)ÚsizeÚmodeÚalign_corners©Údim)Úshaper_   rH   ÚjitÚ
is_tracingrW   r   ÚreshapeÚpermuter   Ú
functionalÚinterpolateÚviewÚcat)rC   rb   rc   rd   r]   r^   Úclass_pos_embedÚpatch_pos_embedrl   Ú
new_heightÚ	new_widthÚsqrt_num_positionss               r4   Úinterpolate_pos_encodingz5InstructBlipVisionEmbeddings.interpolate_pos_encodingx   s`  € ð !×&Ñ& qÑ)¨AÑ-ˆØ×/Ñ/×5Ñ5°aÑ8¸1Ñ<ˆô �y‰y×#Ñ#Ô%¨+¸Ò*FÈ6ÐUZÊ?Ø×*Ñ*Ð*à×1Ñ1²!°R°a°R°%Ñ8ˆØ×1Ñ1²!°Q±R°%Ñ8ˆà×Ñ˜rÑ"ˆà˜tŸ™Ñ.ˆ
Ø˜TŸ_™_Ñ,ˆ	ä& }°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ÐCr3   Úpixel_valuesr{   c                 ó  — |j                   \  }}}}| j                  j                  j                  }| j                  |j	                  |¬«      «      }|j                  d«      j                  dd«      }| j                  j                  |dd«      j	                  |«      }	t        j                  |	|gd¬«      }
|r| j                  |
||«      }n| j                  }|
|d d …d |
j                  d«      …d d …f   j	                  |«      z   }
|
S )N)Údtyper!   r%   rf   rk   )rm   r\   Úweightr~   ÚtoÚflattenÚ	transposerZ   ÚexpandrH   ru   r{   r_   rh   )rC   r|   r{   Ú
batch_sizeÚ_rc   rd   Útarget_dtypeÚpatch_embedsÚclass_embedsrb   r_   s               r4   Úforwardz$InstructBlipVisionEmbeddings.forward    sü   € Ø'3×'9Ñ'9Ñ$ˆ
�A�v˜uØ×+Ñ+×2Ñ2×8Ñ8ˆØ×+Ñ+¨L¯O©OÀ,¨OÓ,OÓPˆØ#×+Ñ+¨AÓ.×8Ñ8¸¸AÓ>ˆØ×+Ñ+×2Ñ2°:¸qÀ"ÓE×HÑHÈÓVˆÜ—Y‘Y ¨lÐ;ÀÔCˆ
Ù#Ø!%×!>Ñ!>¸zÈ6ÐSXÓ!YÑà!%×!8Ñ!8ÐØÐ"4²QÐ8L¸*¿/¹/È!Ó:LÐ8LÊaÐ5OÑ"P×"SÑ"SÐT`Ó"aÑaˆ
ØÐr3   ©F)r-   r.   r/   r(   rS   rH   ÚTensorÚintr{   rI   Úboolr‰   Ú__classcell__©ra   s   @r4   rK   rK   e   sm   ø„ ðcÐ7õ cð$&D°5·<±<ð &DÈð &DÐUXð &DÐ]b×]iÑ]ió &DñP E×$5Ñ$5ð ÐQUð Ðbg×bnÑbn÷ r3   rK   ÚmoduleÚqueryÚkeyÚvalueÚattention_maskÚscalingÚdropoutc                 óp  — t        j                  ||j                  dd«      «      |z  }|�||z   }t        j                  j                  |d¬«      }t        j                  j                  ||| j                  ¬«      }t        j                  ||«      }	|	j                  dd«      j                  «       }	|	|fS )Nrf   éþÿÿÿrk   )ÚpÚtrainingr%   r!   )	rH   Úmatmulr‚   r   rr   Úsoftmaxr–   rš   Ú
contiguous)
r�   r‘   r’   r“   r”   r•   r–   ÚkwargsÚattn_weightsÚattn_outputs
             r4   Úeager_attention_forwardr¡   °   s¢   € ô —<‘<  s§}¡}°R¸Ó'<Ó=ÀÑG€LØÐ!Ø# nÑ4ˆä—=‘=×(Ñ(¨¸2Ð(Ó>€LÜ—=‘=×(Ñ(¨¸È6Ï?É?Ð(Ó[€Lä—,‘,˜|¨UÓ3€KØ×'Ñ'¨¨1Ó-×8Ñ8Ó:€Kà˜Ð$Ð$r3   c                   óÈ   ‡ — e Zd ZdZˆ fd„Zdej                  dedefd„Zdej                  de	ej                  ej                  d	z  e	ej                     d	z  f   fd
„Z
ˆ xZS )ÚInstructBlipAttentionz=Multi-headed attention from 'Attention Is All You Need' paperc                 óö  •— t         ‰| �  «        || _        |j                  | _        |j
                  | _        | j                  | j                  z  | _        | j                  | j                  z  | j                  k7  r&t        d| j                  › d| j                  › d�«      ‚| j                  dz  | _	        d| _
        |j                  | _        t        j                  | j                  d| j                  z  d¬«      | _        |j                  ret        j                   t#        j$                  | j                  «      «      }t        j                   t#        j$                  | j                  «      «      }nd }d }|�Qt#        j&                  |t#        j(                  |d¬«      |f«      }t        j                   |«      | j                  _        t        j                  | j                  | j                  «      | _        y )	Nz;embed_dim must be divisible by num_heads (got `embed_dim`: z and `num_heads`: z).ç      à¿Fr   )Úbias)Úrequires_grad)rR   rS   rL   rT   rU   Únum_attention_headsÚ	num_headsÚhead_dimÚ
ValueErrorÚscaleÚ	is_causalÚattention_dropoutr   ÚLinearÚqkvÚqkv_biasrX   rH   Úzerosru   Ú
zeros_liker¦   Ú
projection)rC   rL   Úq_biasÚv_biasr±   ra   s        €r4   rS   zInstructBlipAttention.__init__Ë   su  ø€ Ü‰ÑÔØˆŒØ×+Ñ+ˆŒØ×3Ñ3ˆŒØŸ™¨$¯.©.Ñ8ˆŒØ�=‰=˜4Ÿ>™>Ñ)¨T¯^©^Ò;ÜØMÈdÏnÉnÐM]ð ^Ø—N‘NÐ# 2ð'óð ð —]‘] DÑ(ˆŒ
ØˆŒØ!'×!9Ñ!9ˆÔô —9‘9˜TŸ^™^¨Q°·±Ñ-?ÀeÔLˆŒà�?Š?Ü—\‘\¤%§+¡+¨d¯n©nÓ"=Ó>ˆFÜ—\‘\¤%§+¡+¨d¯n©nÓ"=Ó>‰FàˆFØˆFàÐÜ—y‘y &¬%×*:Ñ*:¸6ÐQVÔ*WÐY_Ð!`ÓaˆHÜŸL™L¨Ó2ˆD�H‰HŒMäŸ)™) D§N¡N°D·N±NÓCˆ�r3   ÚtensorÚseq_lenÚbszc                 óŽ   — |j                  ||| j                  | j                  «      j                  dd«      j	                  «       S )Nr%   r!   )rt   r©   rª   r‚   r�   )rC   r·   r¸   r¹   s       r4   Ú_shapezInstructBlipAttention._shapeê   s7   € Ø�{‰{˜3 ¨¯©¸¿¹ÓG×QÑQÐRSÐUVÓW×bÑbÓdÐdr3   Úhidden_statesr;   Nc                 ó  — |j                  «       \  }}}| j                  |«      }|j                  ||d| j                  || j                  z  «      j	                  ddddd«      }|d   |d   |d   }	}}t        j                  | j                  j                  t        «      }
 |
| |||	fd| j                  sdn| j                  | j                  dœ|¤Ž\  }}|j                  ||d	«      j                  «       }| j                  |«      }||fS )
z#Input shape: Batch x Time x Channelr   r!   r   r%   é   Nç        )r”   r–   r•   rf   )rh   r°   rp   r©   rq   r   Úget_interfacerL   Ú_attn_implementationr¡   rš   r®   r¬   r�   r´   )rC   r¼   rž   r¹   Útgt_lenrU   Ú	mixed_qkvÚquery_statesÚ
key_statesÚvalue_statesÚattention_interfacer    rŸ   s                r4   r‰   zInstructBlipAttention.forwardí   s#  € ð #0×"4Ñ"4Ó"6ÑˆˆW�ià—H‘H˜]Ó+ˆ	à×%Ñ% c¨7°A°t·~±~ÀyÐTX×TbÑTbÑGbÓc×kÑkØˆq�!�Q˜ó
ˆ	ð 2;¸1±¸yÈ¹|ÈYÐWXÉ\ ,�jˆä(?×(MÑ(MØ�K‰K×,Ñ,Ô.Eó)
Ðñ %8ØØØØð		%
ð
  Ø#Ÿ}š}‘C°$×2HÑ2HØ—J‘Jñ	%
ð ñ	%
Ñ!ˆ�\ð "×)Ñ)¨#¨w¸Ó;×FÑFÓHˆØ—o‘o kÓ2ˆà˜LÐ(Ð(r3   )r-   r.   r/   r0   rS   rH   r‹   rŒ   r»   rE   r‰   rŽ   r�   s   @r4   r£   r£   È   sp   ø„ ÙGôDð>e˜UŸ\™\ð e°Cð e¸có eð")à—|‘|ð")ð 
ˆu�|‰|˜UŸ\™\¨DÑ0°%¸¿¹Ñ2EÈÑ2LÐLÑ	M÷	")r3   r£   c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚInstructBlipMLPc                 ó  •— t         ‰| �  «        || _        t        |j                     | _        t        j                  |j                  |j                  «      | _
        t        j                  |j                  |j                  «      | _        y ©N)rR   rS   rL   r	   Ú
hidden_actÚactivation_fnr   r¯   rT   Úintermediate_sizeÚfc1Úfc2r`   s     €r4   rS   zInstructBlipMLP.__init__  sd   ø€ Ü‰ÑÔØˆŒÜ# F×$5Ñ$5Ñ6ˆÔÜ—9‘9˜V×/Ñ/°×1IÑ1IÓJˆŒÜ—9‘9˜V×5Ñ5°v×7IÑ7IÓJˆ�r3   r¼   r;   c                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S rË   )rÏ   rÍ   rÐ   ©rC   r¼   s     r4   r‰   zInstructBlipMLP.forward  s4   € ØŸ™ Ó/ˆØ×*Ñ*¨=Ó9ˆØŸ™ Ó/ˆØÐr3   ©r-   r.   r/   rS   rH   r‹   r‰   rŽ   r�   s   @r4   rÉ   rÉ     s$   ø„ ôKð U§\¡\ð °e·l±l÷ r3   rÉ   c                   óp   ‡ — e Zd Zdefˆ fd„Zedej                  dee	   dej                  fd„«       Zˆ xZS )ÚInstructBlipEncoderLayerrL   c                 óD  •— t         ‰| �  «        |j                  | _        t	        |«      | _        t        j                  | j                  |j                  ¬«      | _	        t        |«      | _        t        j                  | j                  |j                  ¬«      | _        y ©N©Úeps)rR   rS   rT   rU   r£   Ú	self_attnr   Ú	LayerNormÚlayer_norm_epsÚlayer_norm1rÉ   ÚmlpÚlayer_norm2r`   s     €r4   rS   z!InstructBlipEncoderLayer.__init__$  sm   ø€ Ü‰ÑÔØ×+Ñ+ˆŒÜ.¨vÓ6ˆŒÜŸ<™<¨¯©¸F×<QÑ<QÔRˆÔÜ" 6Ó*ˆŒÜŸ<™<¨¯©¸F×<QÑ<QÔRˆÕr3   r¼   rž   r;   c                 ó¶   — |}| j                  |«      } | j                  dd|i|¤Ž\  }}||z   }|}| j                  |«      }| j                  |«      }||z   }|S )Nr¼   r2   )rÝ   rÚ   rß   rÞ   )rC   r¼   rž   Úresidualr…   s        r4   r‰   z InstructBlipEncoderLayer.forward,  s}   € ð !ˆà×(Ñ(¨Ó7ˆØ)˜4Ÿ>™>ñ 
Ø'ð
àñ
Ñˆ�qð &¨Ñ0ˆØ ˆØ×(Ñ(¨Ó7ˆØŸ™ Ó/ˆà%¨Ñ0ˆàÐr3   )r-   r.   r/   r&   rS   r   rH   r‹   r   r   rI   r‰   rŽ   r�   s   @r4   rÕ   rÕ   #  sR   ø„ ðSÐ1õ Sð ðà—|‘|ðð Ð+Ñ,ðð 
×	Ñ	ò	ó ôr3   rÕ   c                   óz   ‡ — e Zd ZU eed<   dZdZdZdZdZ	dZ
dZdZg d¢Z ej                  «       ˆ fd„«       Zˆ xZS )ÚInstructBlipPreTrainedModelrL   Úblip)ÚimageÚtextT)ÚInstructBlipQFormerEmbeddingsr£   rÕ   ÚInstructBlipQFormerLayerÚ%InstructBlipQFormerMultiHeadAttentionÚInstructBlipQFormerSelfOutputc                 ó:  •— t         ‰| �  |«       | j                  j                  }t	        |t
        «      rEt        j                  |j                  d|¬«       t        j                  |j                  d|¬«       yt	        |t        t        f«      r t        j                  |j                  «       yt	        |t        «      rZt        j                  |j                   t#        j$                  |j                   j&                  d   «      j)                  d«      «       yy)zInitialize the weightsr¿   )ÚmeanÚstdrf   ©r%   rf   N)rR   Ú_init_weightsrL   Úinitializer_rangeÚ
isinstancerK   ÚinitÚtrunc_normal_r_   rZ   Ú$InstructBlipForConditionalGenerationÚInstructBlipModelÚzeros_Úquery_tokensrç   Úcopy_Úposition_idsrH   Úarangerm   rƒ   )rC   r�   Úfactorra   s      €r4   rï   z)InstructBlipPreTrainedModel._init_weightsY  sÊ   ø€ ô 	‰Ñ˜fÔ%Ø—‘×.Ñ.ˆÜ�fÔ:Ô;Ü×Ñ˜v×8Ñ8¸sÈÕOÜ×Ñ˜v×5Ñ5¸CÀVÖLÜ˜Ô!EÔGXÐ YÔZÜ�K‰K˜×+Ñ+Õ,Ü˜Ô =Ô>Ü�J‰J�v×*Ñ*¬E¯L©L¸×9LÑ9L×9RÑ9RÐSUÑ9VÓ,W×,^Ñ,^Ð_fÓ,gÕhð ?r3   )r-   r.   r/   r&   r1   Úbase_model_prefixÚinput_modalitiesÚsupports_gradient_checkpointingÚ_supports_attention_backendÚ_supports_flash_attnÚ_supports_sdpaÚ_supports_flex_attnÚ_can_compile_fullgraphÚ_no_split_modulesrH   Úno_gradrï   rŽ   r�   s   @r4   rã   rã   C  s^   ø… àÓØÐØ(ÐØ&*Ð#Ø"&ÐØÐØ€NØÐà!ÐòÐð €U‡]�]ƒ_ó
ió ô
ir3   rã   c                   óN   ‡ — e Zd ZdZdefˆ fd„Zedee   de	e
z  fd„«       Zˆ xZS )ÚInstructBlipEncodera  
    Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a
    [`InstructBlipEncoderLayer`].

    Args:
        config (`InstructBlipConfig`):
            The corresponding vision configuration for the `InstructBlipEncoder`.
    rL   c                 óÐ   •— t         ‰| �  «        || _        t        j                  t        |j                  «      D �cg c]  }t        |«      ‘Œ c}«      | _        d| _	        y c c}w ©NF)
rR   rS   rL   r   Ú
ModuleListÚrangeÚnum_hidden_layersrÕ   ÚlayersÚgradient_checkpointing)rC   rL   r…   ra   s      €r4   rS   zInstructBlipEncoder.__init__r  sP   ø€ Ü‰ÑÔØˆŒÜ—m‘mÌuÐU[×UmÑUmÓOnÖ$oÈ!Ô%=¸fÕ%EÒ$oÓpˆŒØ&+ˆÕ#ùò %ps   ½A#rž   r;   c                 óR   — |}| j                   D ]  } ||fi |¤Ž}Œ t        |¬«      S )N©Úlast_hidden_state)r  r   )rC   Úinputs_embedsrž   r¼   Úencoder_layers        r4   r‰   zInstructBlipEncoder.forwardx  s>   € ð &ˆØ!Ÿ[™[ò 	ˆMÙ)Øñàñ‰Mð	ô °Ô?Ð?r3   )r-   r.   r/   r0   r&   rS   r   r   r   rE   r   r‰   rŽ   r�   s   @r4   r  r  h  sL   ø„ ñð,Ð1õ ,ð ð@ð Ð+Ñ,ð@ð 
�Ñ	 ò	@ó ô@r3   r  c                   ó¸   ‡ — e Zd ZU dZdZeed<   eedœZ	defˆ fd„Z
e ed¬«      e	 	 ddej                  dz  d	ed
ee   deez  fd„«       «       «       Zd„ Zˆ xZS )ÚInstructBlipVisionModelr|   )rå   rL   )r¼   Ú
attentionsc                 óö   •— t         ‰| �  |«       || _        |j                  }t	        |«      | _        t        |«      | _        t        j                  ||j                  ¬«      | _        | j                  «        y r×   )rR   rS   rL   rT   rK   rb   r  Úencoderr   rÛ   rÜ   Úpost_layernormÚ	post_init)rC   rL   rU   ra   s      €r4   rS   z InstructBlipVisionModel.__init__‘  s]   ø€ Ü‰Ñ˜Ô ØˆŒØ×&Ñ&ˆ	ä6°vÓ>ˆŒÜ*¨6Ó2ˆŒÜ Ÿl™l¨9¸&×:OÑ:OÔPˆÔà�‰Õr3   F)Útie_last_hidden_statesNr{   rž   r;   c                 óø   — |€t        d«      ‚| j                  ||¬«      } | j                  dd|i|¤Ž}|j                  }| j	                  |«      }|d d …dd d …f   }| j	                  |«      }t        ||¬«      S )Nz You have to specify pixel_values)r{   r  r   ©r  Úpooler_outputr2   )r«   rb   r  r  r  r   )rC   r|   r{   rž   r¼   Úencoder_outputsr  Úpooled_outputs           r4   r‰   zInstructBlipVisionModel.forwardœ  sœ   € ð ÐÜÐ?Ó@Ð@àŸ™¨ÐOg˜Óhˆà+7¨4¯<©<ñ ,
Ø'ð,
àñ,
ˆð
 ,×=Ñ=ÐØ ×/Ñ/Ð0AÓBÐà)ª!¨Q²¨'Ñ2ˆØ×+Ñ+¨MÓ:ˆä)Ø/Ø'ô
ð 	
r3   c                 ó   — | j                   S rË   )rb   rG   s    r4   Úget_input_embeddingsz,InstructBlipVisionModel.get_input_embeddingsº  s   € Ø�‰Ðr3   r	  )r-   r.   r/   Úmain_input_namerý   r(   r1   rÕ   r£   Ú_can_record_outputsrS   r   r    r   rH   rI   r�   r   r   rE   r   r‰   r"  rŽ   r�   s   @r4   r  r  ˆ  s¢   ø… Ø$€OØ!ÐØ$Ó$à1Ø+ñÐð
	Ð7õ 	ð  Ù¨EÔ2Øð 26Ø).ñ
à×'Ñ'¨$Ñ.ð
ð #'ð
ð Ð+Ñ,ð	
ð
 
Ð+Ñ	+ò
ó ó 3ó  ð
ö6r3   r  c                   ó:   ‡ — e Zd Zdˆ fd„	Z	 	 	 ddee   fd„Zˆ xZS )ré   c                 ó¨  •— t         ‰| �  «        || _        |j                  |j                  z  dk7  r0t        |d«      s$t        d|j                  |j                  fz  «      ‚|j                  | _        t        |j                  |j                  z  «      | _        | j                  | j                  z  | _	        | j                  dz  | _
        d| _        |j                  | _        t        j                  |j                  | j                  «      | _        |r_t        j                  |j"                  | j                  «      | _        t        j                  |j"                  | j                  «      | _        y t        j                  |j                  | j                  «      | _        t        j                  |j                  | j                  «      | _        y )Nr   Úembedding_sizezLThe hidden size (%d) is not a multiple of the number of attention heads (%d)r¥   F)rR   rS   rL   rT   r¨   Úhasattrr«   rŒ   Úattention_head_sizeÚall_head_sizer•   r­   Úattention_probs_dropout_probr®   r   r¯   r‘   Úencoder_hidden_sizer’   r“   ©rC   rL   Úis_cross_attentionra   s      €r4   rS   z.InstructBlipQFormerMultiHeadAttention.__init__¿  sj  ø€ Ü‰ÑÔØˆŒØ×Ñ × :Ñ :Ñ:¸aÒ?ÌÐPVÐXhÔHiÜØ^Ø×%Ñ% v×'AÑ'AÐBñCóð ð
 $*×#=Ñ#=ˆÔ Ü#& v×'9Ñ'9¸F×<VÑ<VÑ'VÓ#WˆÔ Ø!×5Ñ5¸×8PÑ8PÑPˆÔØ×/Ñ/°Ñ5ˆŒØˆŒØ!'×!DÑ!DˆÔä—Y‘Y˜v×1Ñ1°4×3EÑ3EÓFˆŒ
ÙÜ—y‘y ×!;Ñ!;¸T×=OÑ=OÓPˆDŒHÜŸ™ 6×#=Ñ#=¸t×?QÑ?QÓRˆD�Jä—y‘y ×!3Ñ!3°T×5GÑ5GÓHˆDŒHÜŸ™ 6×#5Ñ#5°t×7IÑ7IÓJˆD�Jr3   rž   c                 óì  — |d u}|j                   d d }g |¢d‘| j                  ‘­}|r|}	|}n|}	g |	j                   d d ¢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 |j                   d d ¢d‘­Ž j!                  «       }||fS )Nrf   r%   r!   r¿   )r–   r•   )rm   r)  r‘   rt   r‚   r’   r“   r   rÀ   rL   rÁ   r¡   rš   r®   r•   rp   r�   )rC   r¼   r”   Úencoder_hidden_statesÚencoder_attention_maskrž   r.  Úinput_shapeÚhidden_shapeÚcurrent_statesÚkv_shapeÚquery_layerÚ	key_layerÚvalue_layerrÇ   r    rŸ   s                    r4   r‰   z-InstructBlipQFormerMultiHeadAttention.forward×  sŽ  € ð 3¸$Ð>Ðà#×)Ñ)¨#¨2Ð.ˆØC˜ÐC bÐC¨$×*BÑ*BÑCˆáØ2ˆNØ3‰Nà*ˆNàM�^×)Ñ)¨#¨2Ð.ÐM°ÐM°D×4LÑ4LÑMˆØ—j‘j Ó/×4Ñ4°\ÓB×LÑLÈQÐPQÓRˆØ—H‘H˜^Ó,×1Ñ1°(Ó;×EÑEÀaÈÓKˆ	Ø—j‘j Ó0×5Ñ5°hÓ?×IÑIÈ!ÈQÓOˆä(?×(MÑ(MØ�K‰K×,Ñ,Ô.Eó)
Ðñ %8ØØØØØð	%
ð  $Ÿ}š}‘C°$×2HÑ2HØ—L‘Lñ	%
ð ñ	%
Ñ!ˆ�\ð *�k×)Ñ)ÐE¨;×+<Ñ+<¸R¸aÐ+@ÐEÀ"ÒE×PÑPÓRˆØ˜LÐ(Ð(r3   rŠ   ©NNN)r-   r.   r/   rS   r   r   r‰   rŽ   r�   s   @r4   ré   ré   ¾  s)   ø„ õKð6 Ø"Ø#ñ,)ð Ð+Ñ,÷,)r3   ré   c                   ón   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  dej
                  fd„Zˆ xZS )rê   c                 ó(  •— t         ‰| �  «        t        j                  |j                  |j                  «      | _        t        j                  |j                  |j                  ¬«      | _        t        j                  |j                  «      | _
        y r×   )rR   rS   r   r¯   rT   ÚdenserÛ   rÜ   ÚDropoutÚhidden_dropout_probr–   r`   s     €r4   rS   z&InstructBlipQFormerSelfOutput.__init__  s`   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3EÑ3EÓFˆŒ
ÜŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒÜ—z‘z &×"<Ñ"<Ó=ˆ�r3   r¼   Úinput_tensorr;   c                 ór   — | j                  |«      }| j                  |«      }| j                  ||z   «      }|S rË   ©r<  r–   rÛ   ©rC   r¼   r?  s      r4   r‰   z%InstructBlipQFormerSelfOutput.forward  ó7   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆØŸ™ }°|Ñ'CÓDˆØÐr3   rÓ   r�   s   @r4   rê   rê     ó1   ø„ ô>ð U§\¡\ð ÀÇÁð ÐRW×R^ÑR^÷ r3   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e   dej
                  fd	„Z	ˆ xZ
S )ÚInstructBlipQFormerAttentionc                 ód   •— t         ‰| �  «        t        ||«      | _        t	        |«      | _        y rË   )rR   rS   ré   Ú	attentionrê   Úoutputr-  s      €r4   rS   z%InstructBlipQFormerAttention.__init__  s)   ø€ Ü‰ÑÔÜ>¸vÐGYÓZˆŒÜ3°FÓ;ˆ�r3   Nr¼   r”   r0  r1  rž   r;   c                 ó^   —  | j                   d||||dœ|¤Ž\  }}| j                  ||«      }|S )N)r¼   r”   r0  r1  r2   )rH  rI  )	rC   r¼   r”   r0  r1  rž   r    r…   Úattention_outputs	            r4   r‰   z$InstructBlipQFormerAttention.forward  sL   € ð (˜Ÿ™ð 
Ø'Ø)Ø"7Ø#9ñ	
ð
 ñ
‰ˆ�Qð  Ÿ;™; {°MÓBÐØÐr3   rŠ   r9  )r-   r.   r/   rS   rH   r‹   rI   r   r   r‰   rŽ   r�   s   @r4   rF  rF    s�   ø„ õ<ð 48Ø:>Ø;?ñ à—|‘|ð ð ×)Ñ)¨DÑ0ð ð  %×0Ñ0°4Ñ7ð	 ð
 !&× 1Ñ 1°DÑ 8ð ð Ð+Ñ,ð ð 
�‰÷ r3   rF  c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚInstructBlipQFormerIntermediatec                 ó  •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        |j                  t        «      rt        |j                     | _        y |j                  | _        y rË   )rR   rS   r   r¯   rT   rÎ   r<  rñ   rÌ   Ústrr	   Úintermediate_act_fnr`   s     €r4   rS   z(InstructBlipQFormerIntermediate.__init__1  s]   ø€ Ü‰ÑÔÜ—Y‘Y˜v×1Ñ1°6×3KÑ3KÓLˆŒ
Ü�f×'Ñ'¬Ô-Ü'-¨f×.?Ñ.?Ñ'@ˆDÕ$à'-×'8Ñ'8ˆDÕ$r3   r¼   r;   c                 óJ   — | j                  |«      }| j                  |«      }|S rË   )r<  rP  rÒ   s     r4   r‰   z'InstructBlipQFormerIntermediate.forward9  s&   € ØŸ
™
 =Ó1ˆØ×0Ñ0°Ó?ˆØÐr3   rÓ   r�   s   @r4   rM  rM  0  s#   ø„ ô9ð U§\¡\ð °e·l±l÷ r3   rM  c                   ón   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  dej
                  fd„Zˆ xZS )ÚInstructBlipQFormerOutputc                 ó(  •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        j                  |j
                  |j                  ¬«      | _        t        j                  |j                  «      | _        y r×   )rR   rS   r   r¯   rÎ   rT   r<  rÛ   rÜ   r=  r>  r–   r`   s     €r4   rS   z"InstructBlipQFormerOutput.__init__A  s`   ø€ Ü‰ÑÔÜ—Y‘Y˜v×7Ñ7¸×9KÑ9KÓLˆŒ
ÜŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒÜ—z‘z &×"<Ñ"<Ó=ˆ�r3   r¼   r?  r;   c                 ór   — | j                  |«      }| j                  |«      }| j                  ||z   «      }|S rË   rA  rB  s      r4   r‰   z!InstructBlipQFormerOutput.forwardG  rC  r3   rÓ   r�   s   @r4   rS  rS  @  rD  r3   rS  c                   óF   ‡ — e Zd Zˆ fd„Z	 	 	 	 ddee   fd„Zd„ Zd„ Zˆ xZ	S )rè   c                 óf  •— t         ‰| �  «        |j                  | _        d| _        t	        |«      | _        || _        ||j                  z  dk(  rt	        |d¬«      | _        d| _	        nd| _	        t        |«      | _        t        |«      | _        t        |«      | _        t        |«      | _        y )Nr%   r   T)r.  F)rR   rS   Úchunk_size_feed_forwardÚseq_len_dimrF  rH  Ú	layer_idxÚcross_attention_frequencyÚcrossattentionÚhas_cross_attentionrM  ÚintermediaterS  rI  Úintermediate_queryÚoutput_query©rC   rL   rZ  ra   s      €r4   rS   z!InstructBlipQFormerLayer.__init__O  s�   ø€ Ü‰ÑÔØ'-×'EÑ'EˆÔ$ØˆÔÜ5°fÓ=ˆŒà"ˆŒà�v×7Ñ7Ñ7¸1Ò<Ü">¸vÐZ^Ô"_ˆDÔØ'+ˆDÕ$à',ˆDÔ$ä;¸FÓCˆÔÜ/°Ó7ˆŒä"AÀ&Ó"IˆÔÜ5°fÓ=ˆÕr3   rž   c           
      ób  —  | j                   |fd|i|¤Ž}|dkD  rè|d d …d |…d d …f   }| j                  r$|€t        d«      ‚ | j                  |f|||dœ|¤Ž}t	        | j
                  | j                  | j                  |«      }	|j                  d   |kD  rjt	        | j                  | j                  | j                  |d d …|d …d d …f   «      j                  |	j                  «      }
t        j                  |	|
gd¬«      }	|	S t	        | j                  | j                  | j                  |«      }	|	S )Nr”   r   z>encoder_hidden_states must be given for cross-attention layers)r”   r0  r1  r%   rk   )rH  r]  r«   r\  r   Úfeed_forward_chunk_queryrX  rY  rm   Úfeed_forward_chunkr€   ÚdevicerH   ru   )rC   r¼   r”   r0  r1  Úquery_lengthrž   rK  Úquery_attention_outputÚlayer_outputÚlayer_output_texts              r4   r‰   z InstructBlipQFormerLayer.forwardc  sp  € ð *˜4Ÿ>™>Øñ
à)ð
ð ñ
Ðð ˜!ÒØ%5²a¸¸,¸ÊÐ6IÑ%JÐ"à×'Ò'Ø(Ð0Ü$Ð%eÓfÐfØ)<¨×)<Ñ)<Ø*ð*à#1Ø*?Ø+Añ	*ð
 ñ*Ð&ô 5Ø×-Ñ-Ø×,Ñ,Ø× Ñ Ø&ó	ˆLð  ×%Ñ% aÑ(¨<Ò7Ü$=Ø×+Ñ+Ø×0Ñ0Ø×$Ñ$Ø$¢Q¨©²qÐ%8Ñ9ó	%÷
 ‘"�\×(Ñ(Ó)ð "ô  %Ÿy™y¨,Ð8IÐ)JÐPQÔR�ð Ðô 5Ø×'Ñ'Ø×,Ñ,Ø× Ñ Ø ó	ˆLð Ðr3   c                 óL   — | j                  |«      }| j                  ||«      }|S rË   )r^  rI  ©rC   rK  Úintermediate_outputrh  s       r4   rd  z+InstructBlipQFormerLayer.feed_forward_chunk˜  s,   € Ø"×/Ñ/Ð0@ÓAÐØ—{‘{Ð#6Ð8HÓIˆØÐr3   c                 óL   — | j                  |«      }| j                  ||«      }|S rË   )r_  r`  rk  s       r4   rc  z1InstructBlipQFormerLayer.feed_forward_chunk_query�  s.   € Ø"×5Ñ5Ð6FÓGÐØ×(Ñ(Ð)<Ð>NÓOˆØÐr3   ©NNNr   )
r-   r.   r/   rS   r   r   r‰   rd  rc  rŽ   r�   s   @r4   rè   rè   N  s6   ø„ ô>ð. Ø"Ø#Øñ3ð Ð+Ñ,ó3òjö
r3   rè   c                   óD   ‡ — e Zd Zˆ fd„Ze	 	 	 	 ddee   fd„«       Zˆ xZS )ÚInstructBlipQFormerEncoderc           	      óÒ   •— t         ‰| �  «        || _        t        j                  t        |j                  «      D �cg c]  }t        ||«      ‘Œ c}«      | _        d| _	        y c c}w r	  )
rR   rS   rL   r   r
  r  r  rè   Úlayerr  ra  s      €r4   rS   z#InstructBlipQFormerEncoder.__init__¥  sW   ø€ Ü‰ÑÔØˆŒÜ—]‘]ÜJOÐPV×PhÑPhÓJiÖj¸YÔ% f¨iÕ8Òjó
ˆŒ
ð ',ˆÕ#ùò ks   ½A$rž   c                 óœ   — t        | j                  j                  «      D ]  }| j                  |   } ||||f||dœ|¤Ž}Œ! t	        |¬«      S )N)r1  rf  r  )r  rL   r  rr  r   )	rC   r¼   r”   r0  r1  rf  rž   ÚiÚlayer_modules	            r4   r‰   z"InstructBlipQFormerEncoder.forward­  sk   € ô �t—{‘{×4Ñ4Ó5ò 
	ˆAØŸ:™: a™=ˆLá(ØØØ%ðð (>Ø)ñð ñ‰Mð
	ô 9Ø+ô
ð 	
r3   rn  )	r-   r.   r/   rS   r   r   r   r‰   rŽ   r�   s   @r4   rp  rp  ¤  s:   ø„ ô,ð ð Ø"Ø#Øñ
ð Ð+Ñ,ò
ó ô
r3   rp  c                   ó2   ‡ — e Zd ZdZˆ fd„Z	 	 	 	 dd„Zˆ xZS )rç   z;Construct the embeddings from word and position embeddings.c                 ó,  •— t         ‰| �  «        t        j                  |j                  |j
                  |j                  ¬«      | _        t        j                  |j                  |j
                  «      | _	        t        j                  |j
                  |j                  ¬«      | _        t        j                  |j                  «      | _        | j!                  dt#        j$                  |j                  «      j'                  d«      d¬«       || _        y )N)Úpadding_idxrØ   rù   rî   F)Ú
persistent)rR   rS   r   Ú	EmbeddingÚ
vocab_sizerT   Úpad_token_idÚword_embeddingsÚmax_position_embeddingsÚposition_embeddingsrÛ   rÜ   Ú	layernormr=  r>  r–   Úregister_bufferrH   rú   rƒ   rL   r`   s     €r4   rS   z&InstructBlipQFormerEmbeddings.__init__Ë  sË   ø€ Ü‰ÑÔÜ!Ÿ|™|¨F×,=Ñ,=¸v×?QÑ?QÐ_e×_rÑ_rÔsˆÔÜ#%§<¡<°×0NÑ0NÐPV×PbÑPbÓ#cˆÔ äŸ™ f×&8Ñ&8¸f×>SÑ>SÔTˆŒÜ—z‘z &×"<Ñ"<Ó=ˆŒð 	×ÑØœEŸL™L¨×)GÑ)GÓH×OÑOÐPWÓXÐejð 	ô 	
ð ˆ�r3   c                 óè  — |�|j                  «       d   }nd}|€&| j                  d d …|||z   …f   j                  «       }|�\| j                  |«      }| j	                  |j                  |j                  «      «      }||z   }|�t        j                  ||fd¬«      }n|}|j                  | j                  j                  j                  «      }| j                  |«      }| j                  |«      }|S )Nr%   r   rk   )rh   rù   Úcloner}  r  r€   re  rH   ru   r€  r   r~   r–   )rC   Ú	input_idsrù   Úquery_embedsÚpast_key_values_lengthÚ
seq_lengthrb   r  s           r4   r‰   z%InstructBlipQFormerEmbeddings.forwardÚ  sñ   € ð Ð Ø"Ÿ™Ó)¨!Ñ,‰JàˆJàÐØ×,Ñ,ªQÐ0FÈÐVlÑIlÐ0lÐ-lÑm×sÑsÓuˆLàÐ Ø×-Ñ-¨iÓ8ˆJà"&×":Ñ":¸<¿?¹?È:×K\ÑK\Ó;]Ó"^ÐØ#Ð&9Ñ9ˆJàÐ'Ü"ŸY™Y¨°jÐ'AÀqÔI‘
à%ˆJà—]‘] 4§>¡>×#8Ñ#8×#>Ñ#>Ó?ˆ
Ø—^‘^ JÓ/ˆ
Ø—\‘\ *Ó-ˆ
ØÐr3   rn  )r-   r.   r/   r0   rS   r‰   rŽ   r�   s   @r4   rç   rç   È  s   ø„ ÙEôð" ØØØ ÷r3   rç   c                   ó†  ‡ — e Zd ZdZdZdZdZdZe e	e
dd¬«      g e	e
dd¬«      gdœZdefˆ fd	„Zd
„ Zd„ Zeee	 	 	 	 	 ddej(                  dej*                  dz  dej(                  dz  dej,                  dz  dej*                  dz  dej*                  dz  dee   deej*                     ez  fd„«       «       «       Zˆ xZS )ÚInstructBlipQFormerModelz�
    Querying Transformer (Q-Former), used in InstructBLIP. Slightly modified from BLIP-2 as it also takes the
    instruction as input.
    Tr%   z
.attention)ÚindexÚ
layer_namez.crossattention)r¼   r  Úcross_attentionsrL   c                 ó’   •— t         ‰| �  |«       || _        t        |«      | _        t        |«      | _        | j                  «        y rË   )rR   rS   rL   rç   rb   rp  r  r  r`   s     €r4   rS   z!InstructBlipQFormerModel.__init__  s9   ø€ Ü‰Ñ˜Ô ØˆŒä7¸Ó?ˆŒä1°&Ó9ˆŒà�‰Õr3   c                 ó.   — | j                   j                  S rË   ©rb   r}  rG   s    r4   r"  z-InstructBlipQFormerModel.get_input_embeddings  s   € Ø�‰×.Ñ.Ð.r3   c                 ó&   — || j                   _        y rË   r�  )rC   r“   s     r4   Úset_input_embeddingsz-InstructBlipQFormerModel.set_input_embeddings  s   € Ø*/ˆ�‰Õ'r3   Nr„  r”   rù   r…  r0  r1  rž   r;   c                 óx  — |€|€t        d«      ‚|�|j                  d   nd}| j                  |||¬«      }	t        | j                  |	|¬«      }|�.|dkD  r|	dd…d|…dd…f   n|	}
t        | j                  |
||¬«      } | j
                  |	f||||dœ|¤Ž}|j                  }|dd…ddd…f   }t        ||¬	«      S )
a$  
        query_embeds (`torch.FloatTensor`  of shape `(batch_size, sequence_length, hidden_size)`):
            Hidden states to be used in the attention computation. If cross-attention,
            will be used for the query (i.e., key and value will use the encoder_hidden_states).
        Nz7You have to specify query_embeds when input_ids is Noner%   r   )r„  rù   r…  )rL   r  r”   )rL   r  r”   r0  )r”   r0  r1  rf  r  )r«   rm   rb   r   rL   r  r  r   )rC   r„  r”   rù   r…  r0  r1  rž   rf  Úembedding_outputÚquery_embedding_outputr  Úsequence_outputr   s                 r4   r‰   z InstructBlipQFormerModel.forward  s  € ð$ Ð Ð!5ÜÐVÓWÐWà0<Ð0H�|×)Ñ)¨!Ò,ÈaˆàŸ?™?ØØ%Ø%ð +ó 
Ðô 3Ø—;‘;Ø*Ø)ô
ˆð "Ð-ð O[Ð]^ÒN^Ð%5²a¸¸,¸ÊÐ6IÒ%JÐdtÐ"Ü%>Ø—{‘{Ø4Ø5Ø&;ô	&Ð"ð ,8¨4¯<©<Øð,
à)Ø"7Ø#9Ø%ñ,
ð ñ,
ˆð *×;Ñ;ˆØ'ª¨1ªa¨Ñ0ˆä;Ø-Ø'ô
ð 	
r3   )NNNNN)r-   r.   r/   r0   rÿ   r  r   r  rè   r   ré   r$  r'   rS   r"  r‘  r   r    r   rH   Ú
LongTensorrI   r‹   r   r   rE   r   r‰   rŽ   r�   s   @r4   r‰  r‰  ú  sB  ø„ ñð
 #'ÐØ€NØÐØÐð 2áÐ@ÈÐVbÔcð
ñ Ð@ÈÐVgÔhð
ñÐðÐ8õ ò/ò0ð  ØØð 48Ø04Ø,0Ø:>Ø;?ñ9
à×#Ñ#ð9
ð ×)Ñ)¨DÑ0ð9
ð ×&Ñ&¨Ñ-ð	9
ð
 —l‘l TÑ)ð9
ð  %×0Ñ0°4Ñ7ð9
ð !&× 1Ñ 1°DÑ 8ð9
ð Ð+Ñ,ð9
ð 
ˆu× Ñ Ñ	!Ð$PÑ	Pò9
ó ó ó  ô9
r3   r‰  z[
    InstructBLIP base Model consisting of language model, qformer and vision encoder.
    c                   ó”  ‡ — e Zd ZdZdgZdefˆ fd„Zd„ Zdej                  dej                  fd„Zee	 	 	 	 	 	 	 ddej                  d
ej                  dej                  d	z  dej                  d	z  dej                  d	z  dej                  d	z  dej                  d	z  dej                  d	z  dedee   deez  fd„«       «       Zˆ xZS )rõ   r|   r÷   rL   c                 ó  •— t         ‰| �  |«       t        |j                  «      | _        t        j                  t        j                  d|j                  |j                  j                  «      «      | _        t        |j                  «      | _        t        j                  |j                  j                  |j                   j                  «      | _        t%        j&                  |j                   «      | _        | j+                  «        y ©Nr%   )rR   rS   r  Úvision_configÚvision_modelr   rX   rH   r²   Únum_query_tokensÚqformer_configrT   r÷   r‰  Úqformerr¯   Útext_configÚlanguage_projectionr"   Úfrom_configÚlanguage_modelr  r`   s     €r4   rS   zInstructBlipModel.__init__g  s·   ø€ Ü‰Ñ˜Ô ä3°F×4HÑ4HÓIˆÔÜŸL™L¬¯©°Q¸×8OÑ8OÐQW×QfÑQf×QrÑQrÓ)sÓtˆÔÜ/°×0EÑ0EÓFˆŒä#%§9¡9¨V×-BÑ-B×-NÑ-NÐPV×PbÑPb×PnÑPnÓ#oˆÔ Ü'×3Ñ3°F×4FÑ4FÓGˆÔð 	�‰Õr3   c                 ó  — | j                   }t        |«      dkD  r:d|vr6t        j                  j	                  «       dkD  rt
        j                  d«       t        | j                  d«      rd| j                  j                  _
        yy©z­
        Some pre-processing hacks to make the model `accelerate` compatible. Check
        https://github.com/huggingface/transformers/pull/21707 for more details.
        r%   r¢  a   The `language_model` is not in the `hf_device_map` dictionary and you are running your script in a multi-GPU environment. this may lead to unexpected behavior when using `accelerate`. Please pass a `device_map` that contains `language_model` to remove this warning. Please refer to https://github.com/huggingface/blog/blob/main/accelerate-large-models.md for more details on creating a `device_map` for large models.Ú_hf_hookTN©Úhf_device_mapÚlenrH   ÚcudaÚdevice_countÚloggerÚwarningr(  r¢  r¥  Úio_same_device©rC   r§  s     r4   Ú_preprocess_acceleratez(InstructBlipModel._preprocess_acceleratet  óy   € ð
 ×*Ñ*ˆäˆ}Ó Ò!Ð&6¸mÑ&KÔPU×PZÑPZ×PgÑPgÓPiÐlmÒPmä�N‰NðMôô �4×&Ñ&¨
Ô3Ø:>ˆD×Ñ×(Ñ(Õ7ð 4r3   r„  r  c                 ój  — |€m| | j                  «       t        j                  | j                  j                  t        j
                  |j                  ¬«      «      k(  }|j                  d«      }n|| j                  j                  k(  }|j                  d«      j                  |j                  «      }|S ©zZ
        Obtains multimodal placeholder mask from `input_ids` or `inputs_embeds`.
        ©r~   re  rf   ©
r"  rH   r·   rL   Úimage_token_idÚlongre  ÚallÚ	unsqueezer€   ©rC   r„  r  Úspecial_image_masks       r4   Úget_placeholder_maskz&InstructBlipModel.get_placeholder_maskˆ  ó�   € ð ÐØ!.Ð2M°$×2KÑ2KÓ2MÜ—‘˜TŸ[™[×7Ñ7¼u¿z¹zÐR_×RfÑRfÔgó3ñ "Ðð "4×!7Ñ!7¸Ó!;Ñà!*¨d¯k©k×.HÑ.HÑ!HÐà/×9Ñ9¸"Ó=×@Ñ@À×AUÑAUÓVÐØ!Ð!r3   NÚqformer_input_idsÚqformer_attention_maskr”   Údecoder_input_idsÚdecoder_attention_maskr{   rž   r;   c
           	      ó`  —  | j                   d||	dœ|
¤Ž}|d   }t        j                  |j                  «       dd t        j                  |j
                  ¬«      }| j                  j                  |j                  d   dd«      }t        j                  |j                  «       dd t        j                  |j
                  ¬«      }|€t        j                  |«      }|j                  |j
                  «      }t        j                  ||gd¬«      } | j                  d|||||dœ|
¤Ž}|d   dd…d|j                  d«      …dd…f   }|€7 | j                  j                  «       |«      }|€t        j                  |«      }| j                  |«      }|j                  |j
                  |j                   «      }| j#                  ||¬	«      }|j%                  ||«      }| j&                  j(                  r | j                  d||d
œ|
¤Ž}n | j                  d||||dœ|
¤Ž}t+        |||¬«      S )aË  
        qformer_input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of input sequence tokens in the vocabulary of the Q-Former. Input tokens can optionally be provided
            to serve as text prompt, which the Q-Former model will encode.

            Indices can be obtained using [`InstructBlipProcessor`]. See [`InstructBlipProcessor.__call__`] for
            details.

            [What are input IDs?](../glossary#input-ids)
        qformer_attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)
        decoder_attention_mask (`torch.BoolTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
            Default behavior: generate a tensor that ignores pad tokens in `decoder_input_ids`. Causal mask will also
            be used by default.

            Only relevant in case an encoder-decoder language model (like T5) is used.
        )r|   r{   r   Nrf   r³  r%   rk   )r„  r”   r…  r0  r1  ©r  ©r  r”   )r  r”   r¿  rÀ  r>   r2   )r›  rH   Úonesrh   r¶  re  r÷   rƒ   rm   Ú	ones_liker€   ru   rž  r¢  r"  r   r~   r»  Úmasked_scatterrL   Úuse_decoder_only_language_modelr7   )rC   r|   r½  r¾  r„  r”   r¿  rÀ  r  r{   rž   r+   Úimage_embedsÚimage_attention_maskr÷   Úquery_attention_maskÚquery_outputsÚquery_outputÚlanguage_model_inputsrº  Úoutputss                        r4   r‰   zInstructBlipModel.forward—  sh  € ðP +˜×*Ñ*ð 
Ø%Ø%=ñ
ð ñ
ˆð
 & aÑ(ˆô  %Ÿz™z¨,×*;Ñ*;Ó*=¸c¸rÐ*BÌ%Ï*É*Ð]i×]pÑ]pÔqÐð ×(Ñ(×/Ñ/°×0BÑ0BÀ1Ñ0EÀrÈ2ÓNˆÜ$Ÿz™z¨,×*;Ñ*;Ó*=¸c¸rÐ*BÌ%Ï*É*Ð]i×]pÑ]pÔqÐØ!Ð)Ü%*§_¡_Ð5FÓ%GÐ"Ø!7×!:Ñ!:Ð;O×;VÑ;VÓ!WÐÜ!&§¡Ð,@ÐBXÐ+YÐ_`Ô!aÐØ$˜Ÿ™ð 
Ø'Ø1Ø%Ø".Ø#7ñ
ð ñ
ˆð % QÑ'ªÐ+A¨\×->Ñ->¸qÓ-AÐ+AÂ1Ð(DÑEˆàÐ ØF˜D×/Ñ/×DÑDÓFÀyÓQˆMØÐ%Ü!&§¡°Ó!;�ð !%× 8Ñ 8¸Ó FÐØ 5× 8Ñ 8¸×9MÑ9MÈ}×ObÑObÓ cÐØ!×6Ñ6°yÐP]Ð6Ó^ÐØ%×4Ñ4Ð5GÐI^Ó_ˆà�;‰;×6Ò6Ø)�d×)Ñ)ð Ø+Ø-ñð ñ‰Gð *�d×)Ñ)ð Ø+Ø-Ø"3Ø'=ñ	ð
 ñˆGô ?Ø)Ø)Ø#*ô
ð 	
r3   )NNNNNNF)r-   r.   r/   r#  Ú_keep_in_fp32_modulesr&   rS   r¯  rH   r–  rI   r»  r   r   r‹   r�   r   r   rE   r7   r‰   rŽ   r�   s   @r4   rõ   rõ   ^  sH  ø„ ð %€OØ+Ð,ÐðÐ1õ ò?ð("¨e×.>Ñ.>ð "Èu×O`ÑO`ó "ð Øð
 ;?Ø.2Ø26Ø59Ø:>Ø-1Ø).ñ_
à×'Ñ'ð_
ð !×,Ñ,ð_
ð !&× 0Ñ 0°4Ñ 7ð	_
ð
 ×$Ñ$ tÑ+ð_
ð ×(Ñ(¨4Ñ/ð_
ð !×+Ñ+¨dÑ2ð_
ð !&× 0Ñ 0°4Ñ 7ð_
ð —|‘| dÑ*ð_
ð #'ð_
ð Ð-Ñ.ð_
ð 
Ð@Ñ	@ò_
ó ó ô_
r3   rõ   aª  
    InstructBLIP Model for generating text given an image and an optional text prompt. The model consists of a vision
    encoder, Querying Transformer (Q-Former) and a language model.

    One can optionally pass `input_ids` to the model, which serve as a text prompt, to make the language model continue
    the prompt. Otherwise, the language model starts generating text from the [BOS] (beginning-of-sequence) token.
    c                   ó’  ‡ — e Zd ZU eed<   dZdZdgZdefˆ fd„Zd„ Z	de
j                  fd„Zdˆ fd
„	Zd„ Zd„ Zee	 	 ddej&                  dej(                  dej(                  d	z  ded	z  dee   deez  fd„«       «       Zdej(                  dej&                  fd„Zee	 	 	 	 	 	 	 	 ddej&                  dej&                  dej(                  d	z  dej&                  d	z  dej(                  d	z  dej(                  d	z  dej(                  d	z  dej&                  d	z  dej(                  d	z  dedee   deez  fd„«       «       Z ej<                  «       	 	 	 	 	 	 ddej&                  dej(                  d	z  dej(                  d	z  dej(                  d	z  dej(                  d	z  dej&                  d	z  dedej(                  fd„«       Zˆ xZ S )rô   rL   r|   Tr÷   c                 ó˜  •— t         ‰| �  |«       t        j                  |j                  «      | _        t        j                  t        j                  d|j                  |j                  j                  «      «      | _        t        j                  |j                  «      | _        t        j                   |j                  j                  |j"                  j                  «      | _        |j&                  r t)        j*                  |j"                  «      }nt-        j*                  |j"                  «      }|| _        | j1                  «        y r™  )rR   rS   r  Ú_from_configrš  r›  r   rX   rH   r²   rœ  r�  rT   r÷   r‰  rž  r¯   rŸ  r   rÇ  r#   r¡  r$   r¢  r  )rC   rL   r¢  ra   s      €r4   rS   z-InstructBlipForConditionalGeneration.__init__  sç   ø€ Ü‰Ñ˜Ô ä3×@Ñ@À×AUÑAUÓVˆÔäŸL™L¬¯©°Q¸×8OÑ8OÐQW×QfÑQf×QrÑQrÓ)sÓtˆÔÜ/×<Ñ<¸V×=RÑ=RÓSˆŒä#%§9¡9¨V×-BÑ-B×-NÑ-NÐPV×PbÑPb×PnÑPnÓ#oˆÔ à×1Ò1Ü1×=Ñ=¸f×>PÑ>PÓQ‰Nä2×>Ñ>¸v×?QÑ?QÓRˆNà,ˆÔð 	�‰Õr3   c                 ó:   — | j                   j                  |«       y rË   )r¢  Úset_output_embeddings)rC   Únew_embeddingss     r4   rÔ  z:InstructBlipForConditionalGeneration.set_output_embeddings  s   € Ø×Ñ×1Ñ1°.ÕAr3   r;   c                 ó6   — | j                   j                  «       S rË   )r¢  Úget_output_embeddingsrG   s    r4   r×  z:InstructBlipForConditionalGeneration.get_output_embeddings"  s   € Ø×"Ñ"×8Ñ8Ó:Ð:r3   Nc                 ó\   •— |€| j                   j                  «       S t        ‰| �  |¬«      S )N)Úmodality)r¢  Úget_encoderrR   )rC   rÙ  ra   s     €r4   rÚ  z0InstructBlipForConditionalGeneration.get_encoder%  s1   ø€ ØÐØ×&Ñ&×2Ñ2Ó4Ð4ä‘7Ñ&°Ð&Ó9Ð9r3   c                 ó6   — | j                   j                  «       S rË   )r¢  Úget_decoderrG   s    r4   rÜ  z0InstructBlipForConditionalGeneration.get_decoder+  s   € Ø×"Ñ"×.Ñ.Ó0Ð0r3   c                 ó  — | j                   }t        |«      dkD  r:d|vr6t        j                  j	                  «       dkD  rt
        j                  d«       t        | j                  d«      rd| j                  j                  _
        yyr¤  r¦  r®  s     r4   r¯  z;InstructBlipForConditionalGeneration._preprocess_accelerate/  r°  r3   r½  r¾  r{   rž   c           
      óø  —  | j                   d||ddœ|¤Ž}t        di |¤d|i¤Ž}|d   }t        j                  |j	                  «       dd t        j
                  |j                  ¬«      }| j                  j                  |j                  d   dd«      }	t        j                  |	j	                  «       dd t        j
                  |j                  ¬«      }
|€t        j                  |«      }|j                  |
j                  «      }t        j                  |
|gd¬	«      } | j                  d|||	||dd
œ|¤Ž}||_        |d   dd…d|	j	                  d«      …dd…f   }| j                  |«      }||_        |S )a  
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`):
            The tensors corresponding to the input images.
        qformer_input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of input sequence tokens in the vocabulary of the Q-Former. Input tokens can optionally be provided
            to serve as text prompt, which the Q-Former model will encode.

            Indices can be obtained using [`InstructBlipProcessor`]. See [`InstructBlipProcessor.__call__`] for
            details.

            [What are input IDs?](../glossary#input-ids)
        qformer_attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)
        T)r|   r{   Úreturn_dictr+   r   Nrf   r³  r%   rk   )r„  r”   r…  r0  r1  rß  r2   )r›  r*   rH   rÄ  rh   r¶  re  r÷   rƒ   rm   rÅ  r€   ru   rž  r,   r   r  )rC   r|   r½  r¾  r{   rž   r+   rÈ  rÉ  r÷   rÊ  r,   rÌ  Úimage_featuress                 r4   Úget_image_featuresz7InstructBlipForConditionalGeneration.get_image_featuresC  s�  € ð> 6G°T×5FÑ5Fð 6
Ø%Ø%=Øñ6
ð ñ	6
ˆô AÑqÀ>ÑqÐbpÒqˆØ% aÑ(ˆô  %Ÿz™z¨,×*;Ñ*;Ó*=¸c¸rÐ*BÌ%Ï*É*Ð]i×]pÑ]pÔqÐð ×(Ñ(×/Ñ/°×0BÑ0BÀ1Ñ0EÀrÈ2ÓNˆÜ$Ÿz™z¨,×*;Ñ*;Ó*=¸c¸rÐ*BÌ%Ï*É*Ð]i×]pÑ]pÔqÐØ!Ð)Ü%*§_¡_Ð5FÓ%GÐ"Ø!7×!:Ñ!:Ð;O×;VÑ;VÓ!WÐÜ!&§¡Ð,@ÐBXÐ+YÐ_`Ô!aÐØ&˜$Ÿ,™,ð 
Ø'Ø1Ø%Ø".Ø#7Øñ
ð ñ
ˆð *9ˆÔ&Ø& qÑ)ª!Ð-C¨|×/@Ñ/@ÀÓ/CÐ-CÂQÐ*FÑGˆð ×1Ñ1°,Ó?ˆØ'5ˆÔ$àÐr3   r„  r  c                 ój  — |€m| | j                  «       t        j                  | j                  j                  t        j
                  |j                  ¬«      «      k(  }|j                  d«      }n|| j                  j                  k(  }|j                  d«      j                  |j                  «      }|S r²  r´  r¹  s       r4   r»  z9InstructBlipForConditionalGeneration.get_placeholder_mask‡  r¼  r3   r”   r¿  rÀ  Úlabelsc           	      óÆ  — | j                  ||||
d¬«      }|j                  }|j                  }|j                  }|€ | j	                  «       |«      }|€t        j                  |«      }|j                  |j                  |j                  «      }| j                  ||¬«      }|j                  ||«      }| j                  j                  rS | j                  d||dœ|¤Ž}|d   }d}|	�j | j                  d||	| j                  j                   j"                  dœ|¤Ž}n5d|d<    | j                  d|||||	d	œ|¤Ž}|j$                  }|j&                  }t)        |||||¬
«      S )a•  
        qformer_input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
            Indices of input sequence tokens in the vocabulary of the Q-Former. Input tokens can optionally be provided
            to serve as text prompt, which the Q-Former model will encode.

            Indices can be obtained using [`InstructBlipProcessor`]. See [`InstructBlipProcessor.__call__`] for
            details.

            [What are input IDs?](../glossary#input-ids)
        qformer_attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:

            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.

            [What are attention masks?](../glossary#attention-mask)
        decoder_attention_mask (`torch.BoolTensor` of shape `(batch_size, target_sequence_length)`, *optional*):
            Default behavior: generate a tensor that ignores pad tokens in `decoder_input_ids`. Causal mask will also
            be used by default.

            Only relevant in case an encoder-decoder language model (like T5) is used.
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the language modeling loss. Indices should be in `[-100, 0, ..., config.vocab_size -
            1]`. All labels set to `-100` are ignored (masked), the loss is only computed for labels in `[0, ...,
            config.vocab_size]`

        Examples:

        ```python
        >>> from transformers import InstructBlipProcessor, InstructBlipForConditionalGeneration
        >>> import torch
        >>> from PIL import Image
        >>> import httpx
        >>> from io import BytesIO

        >>> model = InstructBlipForConditionalGeneration.from_pretrained("Salesforce/instructblip-vicuna-7b")
        >>> processor = InstructBlipProcessor.from_pretrained("Salesforce/instructblip-vicuna-7b")

        >>> device = "cuda" if torch.cuda.is_available() else "cpu"
        >>> model.to(device)  # doctest: +IGNORE_RESULT

        >>> url = "https://raw.githubusercontent.com/salesforce/LAVIS/main/docs/_static/Confusing-Pictures.jpg"
        >>> with httpx.stream("GET", url) as response:
        ...     image = Image.open(BytesIO(response.read())).convert("RGB")
        >>> prompt = "What is unusual about this image?"
        >>> inputs = processor(images=image, text=prompt, return_tensors="pt").to(device)

        >>> outputs = model.generate(
        ...     **inputs,
        ...     do_sample=False,
        ...     num_beams=5,
        ...     max_length=256,
        ...     min_length=1,
        ...     top_p=0.9,
        ...     repetition_penalty=1.5,
        ...     length_penalty=1.0,
        ...     temperature=1,
        ... )
        >>> generated_text = processor.batch_decode(outputs, skip_special_tokens=True)[0].strip()
        >>> print(generated_text)
        The unusual aspect of this image is that a man is ironing clothes on the back of a yellow SUV, which is parked in the middle of a busy city street. This is an unconventional approach to ironing clothes, as it requires the man to balance himself and his ironing equipment on top of the vehicle while navigating through traffic. Additionally, the presence of taxis and other vehicles in the scene further emphasizes the unusual nature of this situation.
        ```T©r½  r¾  r{   rß  NrÂ  rÃ  r   )r9   rã  r{  rß  )r  r”   r¿  rÀ  rã  )r8   r9   r+   r,   r:   r2   )rá  r  r,   r+   r"  rH   rÅ  r€   re  r~   r»  rÆ  rL   rÇ  r¢  Úloss_functionrŸ  r{  r8   r9   r7   )rC   r|   r½  r¾  r„  r”   r¿  rÀ  r  rã  r{   rž   rà  rÍ  r,   r+   rº  rÎ  r9   r8   s                       r4   r‰   z,InstructBlipForConditionalGeneration.forward–  s·  € ð^ CG×BYÑBYØØ/Ø#9Ø%=Øð CZó C
ˆð !/× <Ñ <ÐØ(×8Ñ8ˆØ'×6Ñ6ˆàÐ Ø7˜D×5Ñ5Ó7¸	ÓBˆMàÐ!Ü"Ÿ_™_¨YÓ7ˆNà 5× 8Ñ 8¸×9MÑ9MÈ}×ObÑObÓ cÐØ!×6Ñ6°yÐP]Ð6Ó^ÐØ%×4Ñ4Ð5GÐI^Ó_ˆà�;‰;×6Ò6Ø)�d×)Ñ)ð Ø+Ø-ñð ñˆGð
 ˜Q‘ZˆFØˆDØÐ!Ø)�t×)Ñ)ð Ø!¨&¸T¿[¹[×=TÑ=T×=_Ñ=_ñØciñ‘ð
 %)ˆF�=Ñ!Ø)�d×)Ñ)ð Ø+Ø-Ø"3Ø'=Øñð ñˆGð —<‘<ˆDØ—^‘^ˆFä>ØØØ)Ø+Ø#*ô
ð 	
r3   c                 óR  — t        | d«      r| j                  «        |j                  d   }	| j                  ||||d¬«      }
|
j                  }|€­|€•| j
                  j                  g| j
                  j                  z  }|| j
                  j                  j                  gz   }t        j                  |gt        j                  |j                  ¬«      }|j                  |	d«      } | j                  «       |«      }|€t        j                   |«      }|j#                  |j                  |j$                  «      }| j'                  ||¬«      }|j)                  ||«      }||dœ}| j*                  j
                  j,                  s||d	<    | j*                  j.                  d
i |¤|¤Ž}|S )a–  
        Overrides `generate` function to be able to use the model as a conditional generator.

        Args:
            pixel_values (`torch.FloatTensor` of shape (batch_size, num_channels, height, width)):
                Input images to be processed.
            qformer_input_ids (`torch.LongTensor` of shape (batch_size, sequence_length), *optional*):
                The sequence used as a prompt to be fed to the Q-Former module.
            qformer_attention_mask (`torch.LongTensor` of shape (batch_size, sequence_length), *optional*):
                Mask to avoid performing attention on padding token indices.
            input_ids (`torch.LongTensor` of shape (batch_size, sequence_length), *optional*):
                The sequence used as a prompt for the generation.
            attention_mask (`torch.LongTensor` of shape (batch_size, sequence_length), *optional*):
                Mask to avoid performing attention on padding token indices.
            inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
                Embedded representation of the inputs. Should be float, not int tokens.
            interpolate_pos_encoding (`bool`, *optional*, defaults to `False`):
                Whether to interpolate the positional encoding of the image embeddings.

        Returns:
            captions (list): A list of strings of length batch_size * num_captions.
        r§  r   Trå  r³  r%   rÂ  rÃ  r„  r2   )r(  r¯  rm   rá  r  rL   Úimage_token_indexrœ  rŸ  Úbos_token_idrH   r·   r¶  re  Úrepeatr"  rÅ  r€   r~   r»  rÆ  r¢  Úis_encoder_decoderÚgenerate)rC   r|   r½  r¾  r„  r”   r  r{   Úgenerate_kwargsr„   rà  rÍ  Úimage_tokensÚstart_tokensrº  ÚinputsrÎ  s                    r4   rì  z-InstructBlipForConditionalGeneration.generate  sœ  € ôD �4˜Ô)à×'Ñ'Ô)à!×'Ñ'¨Ñ*ˆ
ØBF×BYÑBYØØ/Ø#9Ø%=Øð CZó C
ˆð !/× <Ñ <ÐàÐ ØÐ Ø $§¡× =Ñ =Ð>ÀÇÁ×A]ÑA]Ñ]�Ø+¨t¯{©{×/FÑ/F×/SÑ/SÐ.TÑT�Ü!ŸL™L¨,¨¼u¿z¹zÐR^×ReÑReÔf�	Ø%×,Ñ,¨Z¸Ó;�	Ø7˜D×5Ñ5Ó7¸	ÓBˆMàÐ!Ü"Ÿ_™_¨YÓ7ˆNà 5× 8Ñ 8¸×9MÑ9MÈ}×ObÑObÓ cÐØ!×6Ñ6°yÐP]Ð6Ó^ÐØ%×4Ñ4Ð5GÐI^Ó_ˆà#0ÀNÑSˆØ×"Ñ"×)Ñ)×<Ò<Ø"+ˆF�;Ñà.�$×%Ñ%×.Ñ.ÑK°ÐK¸?ÑKˆàˆr3   rË   r	  )NNNNNNNF)NNNNNF)!r-   r.   r/   r&   r1   r#  r  rÏ  rS   rÔ  r   ÚModuler×  rÚ  rÜ  r¯  r   r   rH   rI   r–  r�   r   r   rE   r*   rá  r»  r7   r‰   r  rì  rŽ   r�   s   @r4   rô   rô   û  sé  ø… ð ÓØ$€Oà!ÐØ+Ð,ÐðÐ1õ ò(Bð; r§y¡yó ;õ:ò1ò?ð( Øð
 ;?Ø05ñ@à×'Ñ'ð@ð !×+Ñ+ð@ð !&× 0Ñ 0°4Ñ 7ð	@ð
 #'¨¡+ð@ð Ð+Ñ,ð@ð 
Ð8Ñ	8ò@ó ó ð@ðD"¨e×.>Ñ.>ð "Èu×O`ÑO`ó "ð Øð
 ;?Ø.2Ø26Ø59Ø:>Ø26Ø*.Ø).ñB
à×'Ñ'ðB
ð !×,Ñ,ðB
ð !&× 0Ñ 0°4Ñ 7ð	B
ð
 ×$Ñ$ tÑ+ðB
ð ×(Ñ(¨4Ñ/ðB
ð !×+Ñ+¨dÑ2ðB
ð !&× 0Ñ 0°4Ñ 7ðB
ð ×(Ñ(¨4Ñ/ðB
ð × Ñ  4Ñ'ðB
ð #'ðB
ð Ð+Ñ,ðB
ð 
Ð@Ñ	@òB
ó ó ðB
ðH €U‡]�]ƒ_ð 6:Ø:>Ø-1Ø26Ø26Ø).ñDà×'Ñ'ðDð !×+Ñ+¨dÑ2ðDð !&× 0Ñ 0°4Ñ 7ð	Dð
 ×#Ñ# dÑ*ðDð ×(Ñ(¨4Ñ/ðDð ×(Ñ(¨4Ñ/ðDð #'ðDð 
×	Ñ	òDó ôDr3   rô   )r‰  rã   rõ   rô   r  )r¿   )Tr0   Úcollections.abcr   Údataclassesr   Útypingr   rH   r   Ú r   rò   Úactivationsr	   Ú
generationr
   Úmasking_utilsr   Úmodeling_flash_attention_utilsr   Úmodeling_layersr   Úmodeling_outputsr   r   r   r   r   r   Úmodeling_utilsr   r   Úprocessing_utilsr   Úpytorch_utilsr   Úutilsr   r   r   r   r   r   Úutils.genericr   Úutils.output_capturingr   r    Úautor"   r#   r$   Úconfiguration_instructblipr&   r'   r(   Ú
get_loggerr-   r«  r*   r7   rñ  rK   r‹   Úfloatr¡   r£   rÉ   rÕ   rã   r  r  ré   rê   rF  rM  rS  rè   rp  rç   r‰  rõ   rô   Ú__all__r2   r3   r4   ú<module>r     sž  ðñ "å $Ý !Ý ã Ý å &Ý !Ý )Ý 6Ý BÝ 9÷÷ ÷ GÝ &Ý 6ß j× jÝ 7ß Eß IÑ Iß oÑ oð 
ˆ×	Ñ	˜HÓ	%€ð Ø
ô	PÐ.Hó 	Pó ó ð	Pñ ðôð
 ô
°kó 
ó óð
ô<G 2§9¡9ô Gðd ñ%Ø�I‰Ið%à�<‰<ð%ð 
�‰ð%ð �<‰<ð	%ð
 —L‘L 4Ñ'ð%ð ð%ð ó%ô0G)˜BŸI™Iô G)ôV�b—i‘iô ô Ð9ô ð@ ô i /ó  ió ð iôH@˜"Ÿ)™)ô @ô@3Ð9ô 3ôlE)¨B¯I©Iô E)ôR B§I¡Iô ô  2§9¡9ô  ô4 b§i¡iô ô  §	¡	ô ôRÐ9ô Rôl!
 §¡ô !
ôH/ B§I¡Iô /ôda
Ð:ô a
ñH ðôô
U
Ð3ó U
óð
U
ñp ðôô]Ð+FÈó ]óð]ò@�r3   