
    ij                     :   d dl mZ d dlmZ d dlmZ d dlZd dlZd dl	m
Z
 d dlm
c 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#m$Z$ ddl%m&Z&m'Z' ddl(m)Z) ddl*m+Z+m,Z,m-Z- e e#d       G d de!                    Z.e e#d       G d de!                    Z/ee# G d de!                    Z0 G d de
jb                        Z2 G d de
jb                        Z3	 dCde
jb                  d ejh                  d!ejh                  d"ejh                  d#ejh                  dz  d$e5d%e5fd&Z6 G d' d(e
jb                        Z7 G d) d*e
jb                        Z8 G d+ d,e      Z9e# G d- d.e             Z: G d/ d0e
jb                        Z; G d1 d2e:      Z< G d3 d4e:      Z= e#d5       G d6 d7e:             Z> G d8 d9e
jb                        Z? e#d:       G d; d<e:             Z@e# G d= d>e:             ZA e#d?       G d@ dAe:             ZBg dBZCy)D    )Callable)	dataclass)AnyN   )initialization)ACT2FN)create_bidirectional_mask)GradientCheckpointingLayer)BaseModelOutputBaseModelOutputWithPoolingImageClassifierOutput)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack)ModelOutputTransformersKwargsauto_docstringtorch_compilable_check)can_return_tuplemerge_with_config_defaults)capture_outputs   )Siglip2ConfigSiglip2TextConfigSiglip2VisionConfigz}
    Base class for vision model's outputs that also contains image embeddings of the pooling of the last hidden states.
    )custom_introc                       e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	e
ej                  df   dz  ed<   dZe
ej                  df   dz  ed<   y)Siglip2VisionOutputz
    image_embeds (`torch.FloatTensor` of shape `(batch_size, output_dim)` *optional* returned when model is initialized with `with_projection=True`):
        The image embeddings obtained by applying the projection layer to the pooler_output.
    Nimage_embedslast_hidden_state.hidden_states
attentions)__name__
__module____qualname____doc__r   torchFloatTensor__annotations__r    r!   tupler"        }/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/transformers/models/siglip2/modeling_siglip2.pyr   r   ,   sr    
 .2L%##d*126u((4/6:>M5**C/047>7;Je'',-4;r,   r   ze
    Base class for text model's outputs that also contains a pooling of the last hidden states.
    c                       e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	e
ej                  df   dz  ed<   dZe
ej                  df   dz  ed<   y)Siglip2TextOutputz
    text_embeds (`torch.FloatTensor` of shape `(batch_size, output_dim)` *optional* returned when model is initialized with `with_projection=True`):
        The text embeddings obtained by applying the projection layer to the pooler_output.
    Ntext_embedsr    .r!   r"   )r#   r$   r%   r&   r0   r'   r(   r)   r    r!   r*   r"   r+   r,   r-   r/   r/   >   sr    
 -1K""T)026u((4/6:>M5**C/047>7;Je'',-4;r,   r/   c                      e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	ej                  dz  ed<   dZ
ej                  dz  ed<   dZej                  dz  ed<   dZeed<   dZeed	<   d
ee   fdZy)Siglip2Outputa  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `return_loss` is `True`):
        Contrastive loss for image-text similarity.
    logits_per_image (`torch.FloatTensor` of shape `(image_batch_size, text_batch_size)`):
        The scaled dot product scores between `image_embeds` and `text_embeds`. This represents the image-text
        similarity scores.
    logits_per_text (`torch.FloatTensor` of shape `(text_batch_size, image_batch_size)`):
        The scaled dot product scores between `text_embeds` and `image_embeds`. This represents the text-image
        similarity scores.
    text_embeds (`torch.FloatTensor` of shape `(batch_size, output_dim`):
        The text embeddings obtained by applying the projection layer to the pooled output of [`Siglip2TextModel`].
    image_embeds (`torch.FloatTensor` of shape `(batch_size, output_dim`):
        The image embeddings obtained by applying the projection layer to the pooled output of [`Siglip2VisionModel`].
    text_model_output (`BaseModelOutputWithPooling`):
        The output of the [`Siglip2TextModel`].
    vision_model_output (`BaseModelOutputWithPooling`):
        The output of the [`Siglip2VisionModel`].
    Nlosslogits_per_imagelogits_per_textr0   r   text_model_outputvision_model_outputreturnc                 B    t        d | j                         D              S )Nc              3   `   K   | ]&  }t        |t              r|j                         n| ( y wN)
isinstancer   to_tuple).0vs     r-   	<genexpr>z)Siglip2Output.to_tuple.<locals>.<genexpr>o   s$     ^1Z;%?QZZ\QF^s   ,.)r*   valuesselfs    r-   r=   zSiglip2Output.to_tuplen   s    ^PTP[P[P]^^^r,   )r#   r$   r%   r&   r3   r'   r(   r)   r4   r5   r0   r   r6   r   r7   r*   r   r=   r+   r,   r-   r2   r2   P   s    & &*D%

d
")15e''$.504OU&&-4,0K""T)0-1L%##d*148186:3:_%* _r,   r2   c            	            e Zd Zdef fdZedej                  dej                  de	dej                  fd       Z
dej                  dej                  dej                  fd	Z xZS )
Siglip2VisionEmbeddingsconfigc                    t         |           || _        |j                  | _        |j
                  | _        t        j                  |j                  | j
                  z  | j
                  z  | j                        | _	        |j                  | _
        t        | j                  dz        | _        t        j                  | j                  | j                        | _        y )N)in_featuresout_featuresg      ?)super__init__rF   hidden_size	embed_dim
patch_sizennLinearnum_channelspatch_embeddingnum_patchesintposition_embedding_size	Embeddingposition_embeddingrC   rF   	__class__s     r-   rK   z Siglip2VisionEmbeddings.__init__s   s    ++ ++!yy++doo=O 

 "--'*4+;+;S+@'A$"$,,t/?/?"Pr,   positional_embeddingsspatial_shapes
max_lengthr8   c                    |j                   d   }| j                   d   }| j                  }t        j                  |||f| j                  |      }| j                  ddd      j                  d      } | j                  j                  dk(  r| j                  t        j                        } t        |      D ]  }||   j                         \  }}	t        |	dkD  d       t        |dkD  d       t        ||	z  |k  d	       t        j                  | ||	fd
dd      }
|
j                  |||	z        j!                  dd      }
|
j                  |      }
|
||d||	z  f<   |
d   ||||	z  df<    |S )ac  
        Resize positional embeddings to image-specific size and pad to a fixed size.

        Args:
            positional_embeddings (`torch.Tensor`):
                Position embeddings of shape (height, width, embed_dim)
            spatial_shapes (`torch.LongTensor`):
                Spatial shapes of shape (batch_size, 2) to resize the positional embeddings to
            max_length (`int`):
                Maximum length of the positional embeddings to pad resized positional embeddings to

        Returns:
            `torch.Tensor`: Embeddings of shape (batch_size, max_length, embed_dim)
        r   devicedtype   r   cpuz8Width of resized positional embeddings must be positive.z9Height of resized positional embeddings must be positive.z0Resized positional embeddings exceed max_length.bilinearFT)sizemodealign_corners	antialiasN)shapera   r'   emptyr`   permute	unsqueezetypetofloat32rangetolistr   Finterpolatereshape	transpose)rZ   r[   r\   
batch_sizerM   source_dtyperesulted_positional_embeddingsiheightwidthresized_embeddingss              r-   resize_positional_embeddingsz4Siglip2VisionEmbeddings.resize_positional_embeddings   s   ( $))!,
)//3	,22).Y/(//*
& !6 = =aA F P PQR S !'',,5$9$<$<U]]$K!z" 	XA*1-446MFE"EAI0jk"FQJ1lm"FUNz#ACuv!"%e_#" "4!;!;IvPU~!V!`!`abde!f "4!6!6|!DBT*1.>.>+>?BTUVBW*1fun.>+>?+	X. .-r,   pixel_valuesc                 J   | j                   j                  j                  }| j                  |j                  |            }| j                  j                  j                  | j                  | j                  d      }| j                  |||j                  d         }||z   }|S )aH  
        Args:
            pixel_values (`torch.FloatTensor`):
                Pixel values of shape (batch_size, max_num_patches, num_channels * patch_size * patch_size)
            spatial_shapes (`list[tuple[int, int]]`):
                Spatial shapes of shape (batch_size, 2) to resize the positional embeddings to
        )ra   r^   r   )r\   )	rR   weightra   rn   rW   rt   rU   r}   ri   )rC   r~   r[   target_dtypepatch_embedsrZ   resized_positional_embeddings
embeddingss           r-   forwardzSiglip2VisionEmbeddings.forward   s     ++2288++LOO,O,OP !% 7 7 > > F F(($*F*F!
 )-(I(I!>l>P>PQR>S )J )
%
 "$AA
r,   )r#   r$   r%   r   rK   staticmethodr'   Tensor
LongTensorrT   r}   r(   r   __classcell__rY   s   @r-   rE   rE   r   s    Q2 Q ;.$||;.((;. ;. 
	;. ;.zE$5$5 uGWGW \a\h\h r,   rE   c            	            e Zd Zdef fdZ	 	 	 d	dej                  dz  dej                  dz  dej                  dz  dej                  fdZ	 xZ
S )
Siglip2TextEmbeddingsrF   c                 N   t         |           |j                  }t        j                  |j
                  |      | _        t        j                  |j                  |      | _        | j                  dt        j                  |j                        j                  d      d       y )Nposition_idsr   r^   F)
persistent)rJ   rK   rL   rO   rV   
vocab_sizetoken_embeddingmax_position_embeddingsrW   register_bufferr'   arangeexpandrC   rF   rM   rY   s      r-   rK   zSiglip2TextEmbeddings.__init__   s    &&	!||F,=,=yI"$,,v/M/My"Y 	ELL)G)GHOOPWXej 	 	
r,   N	input_idsr   inputs_embedsr8   c                 8   ||j                   d   n|j                   d   }| j                  j                  j                   d   }||kD  rt        d| d|       || j                  d d d |f   }|| j                  |      }| j                  |      }||z   }|S )Nr^   r   zRSequence length must be less than max_position_embeddings (got `sequence length`: z and max_position_embeddings: )ri   rW   r   
ValueErrorr   r   )rC   r   r   r   
seq_lengthmax_position_embeddingposition_embeddingsr   s           r-   r   zSiglip2TextEmbeddings.forward   s     -6,AY__R(}GZGZ[]G^
!%!8!8!?!?!E!Ea!H..d,<=S<TV 
 ,,Q^<L  00;M"55lC"%88
r,   NNN)r#   r$   r%   r   rK   r'   r   r(   r   r   r   r   s   @r-   r   r      sk    

0 

 .20426	##d* &&- ((4/	
 
r,   r   modulequerykeyvalueattention_maskscalingdropoutc                    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 )Nr^   r   )dimra   )ptrainingr   rb   )r'   matmulru   rO   
functionalsoftmaxro   rn   ra   r   r   
contiguous)
r   r   r   r   r   r   r   kwargsattn_weightsattn_outputs
             r-   eager_attention_forwardr     s     <<s}}R'<=GL!#n4==((2U]](SVVW\WbWbcL==((6??([L,,|U3K''1-88:K$$r,   c            
            e Zd ZdZ fdZ	 ddej                  dej                  dz  deej                  ej                  dz  f   fdZ xZ	S )	Siglip2Attentionz=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  | _	        |j                  | _        d| _        t        j                  | j                  | j                        | _        t        j                  | j                  | j                        | _        t        j                  | j                  | j                        | _        t        j                  | j                  | j                        | _        y )Nz;embed_dim must be divisible by num_heads (got `embed_dim`: z and `num_heads`: z).      F)rJ   rK   rF   rL   rM   num_attention_heads	num_headshead_dimr   scaleattention_dropoutr   	is_causalrO   rP   k_projv_projq_projout_projrX   s     r-   rK   zSiglip2Attention.__init__  s   ++33$..8==4>>)T^^;MdnnM] ^NN#2'  ]]D(
//ii?ii?ii?		$..$..Ar,   Nr!   r   r8   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              }	 |	| ||||| j                  | j                  | j                  sdn| j                        \  }
} |
j                   g |d j#                         }
| j%                  |
      }
|
|fS )z#Input shape: Batch x Time x ChannelNr^   r   rb           )r   r   r   )ri   r   r   viewru   r   r   r   get_interfacerF   _attn_implementationr   r   r   r   r   rt   r   r   )rC   r!   r   r   input_shapehidden_shapequerieskeysrA   attention_interfacer   r   s               r-   r   zSiglip2Attention.forward0  s<    $))#2.88b8$--8++m,11,?II!QO{{=)..|<FFq!L]+00>HHAN(?(M(MKK,,.E)
 %8nnJJ#}}C$,,	%
!\ *k));;;;FFHmmK0L((r,   r;   )
r#   r$   r%   r&   rK   r'   r   r*   r   r   r   s   @r-   r   r     sV    GB. /3!)||!) t+!)
 
u||U\\D00	1!)r,   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )
Siglip2MLPc                    t         |           || _        t        |j                     | _        t        j                  |j                  |j                        | _
        t        j                  |j                  |j                        | _        y r;   )rJ   rK   rF   r   
hidden_actactivation_fnrO   rP   rL   intermediate_sizefc1fc2rX   s     r-   rK   zSiglip2MLP.__init__U  sd    #F$5$5699V//1I1IJ99V55v7I7IJr,   r!   r8   c                 l    | j                  |      }| j                  |      }| j                  |      }|S r;   )r   r   r   )rC   r!   s     r-   r   zSiglip2MLP.forward\  s4    /**=9/r,   )r#   r$   r%   rK   r'   r   r   r   r   s   @r-   r   r   T  s$    KU\\ ell r,   r   c            	            e Zd Zdeez  f fdZedej                  dej                  de	e
   dej                  fd       Z xZS )Siglip2EncoderLayerrF   c                 D   t         |           |j                  | _        t	        j
                  | j                  |j                        | _        t        |      | _	        t	        j
                  | j                  |j                        | _
        t        |      | _        y Neps)rJ   rK   rL   rM   rO   	LayerNormlayer_norm_epslayer_norm1r   	self_attnlayer_norm2r   mlprX   s     r-   rK   zSiglip2EncoderLayer.__init__d  sm    ++<<F<Q<QR)&1<<F<Q<QRf%r,   r!   r   r   r8   c                     |}| j                  |      } | j                  d||d|\  }}||z   }|}| j                  |      }| j                  |      }||z   }|S )N)r!   r   r+   )r   r   r   r   )rC   r!   r   r   residual_s         r-   r   zSiglip2EncoderLayer.forwardl  s     !((7)4>> 
')
 
q
 !=0 ((7/ =0r,   )r#   r$   r%   r   r   rK   r   r'   r   r   r   r(   r   r   r   s   @r-   r   r   c  sd    &25FF & ||  +,	
 
		 r,   r   c                   t    e Zd ZU eed<   dZdZdZg dZdZ	dZ
dZdZeedZ ej"                         d        Zy	)
Siglip2PreTrainedModelrF   siglip2)imagetextT)r   rE   r   $Siglip2MultiheadAttentionPoolingHeadF)r!   r"   c                    t        |t              rt        | j                  t              r | j                  j                  j
                  n| j                  j
                  }t        j                  |j                  j                  dt        j                  |      z         t        |d      rZt        j                  |j                  t        j                   |j                  j"                  d         j%                  d             yyt        |t&        j(                        r t        j*                  |j                         yt        |t,              rIt        j.                  |j0                  j                         t        j.                  |j2                  j                         t        j.                  |j4                  j                         t        j.                  |j6                  j                         t        j8                  |j0                  j:                         t        j8                  |j2                  j:                         t        j8                  |j4                  j:                         t        j8                  |j6                  j:                         yt        |t<              rt        j.                  |j>                  j                         t        j.                  |j@                  j                         t        j                  |j>                  j:                  d       t        j                  |j@                  j:                  d       yt        |tB              rrt        j.                  |jD                         t        j.                  |jF                  jH                         t        j8                  |jF                  jJ                         yt        |tL              r?t        j8                  |jN                         t        j8                  |jP                         yt        |tR              rdt        j                  |jT                  j                  | j                  j                  j
                  dz  | j                  jV                  z         yt        |t&        jX                  t&        jZ                  f      rLt        j\                  |j                         |j:                   t        j8                  |j:                         yyt        |t&        j^                        r?t        j8                  |j:                         t        j`                  |j                         yt        |tb              rZt        j                  |j                  t        j                   |j                  j"                  d         j%                  d             yy)	zInitialize the weightsr   )stdr   r^   r   gư>r   N)2r<   rE   rF   r   vision_configrL   initnormal_rW   r   npsqrthasattrcopy_r   r'   r   ri   r   rO   rV   default_flax_embed_init_r   xavier_uniform_r   r   r   r   zeros_biasr   r   r   r   probe	attentionin_proj_weightin_proj_biasSiglip2Modellogit_scale
logit_biasSiglip2ForImageClassification
classifierinitializer_factorrP   Conv2dlecun_normal_r   ones_r   )rC   r   r{   s      r-   _init_weightsz$Siglip2PreTrainedModel._init_weights  s    f56 dkk=9 ))55[[,, 
 LL2299q2775>?QRv~.

6..V=P=P=V=VWY=Z0[0b0bcj0kl /-))&--8 01  !5!56  !5!56  !5!56  !7!78KK**+KK**+KK**+KK,,-
+  !2!23  !2!23LLd3LLd3 DE  .  !1!1!@!@AKK((556-KK**+KK))* =>LL!!((KK--994?$++B`B`` BII 67v}}-{{&FKK( '-KK$JJv}}% 56JJv**ELL9L9L9R9RSU9V,W,^,^_f,gh 7r,   N)r#   r$   r%   r   r)   base_model_prefixinput_modalitiessupports_gradient_checkpointing_no_split_modules_supports_flash_attn_supports_sdpa_supports_flex_attn_supports_attention_backendr   r   _can_record_outputsr'   no_gradr  r+   r,   r-   r   r     si    !(&*# !N"& -&
 U]]_/i /ir,   r   c                   j     e Zd ZdZdef fdZe	 d	dej                  dz  de	e
   defd       Z xZS )
Siglip2Encoderz
    Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a
    [`Siglip2EncoderLayer`].

    Args:
        config: Siglip2Config
    rF   c                     t         |           || _        t        j                  t        |j                        D cg c]  }t        |       c}      | _        d| _	        y c c}w )NF)
rJ   rK   rF   rO   
ModuleListrp   num_hidden_layersr   layersgradient_checkpointing)rC   rF   r   rY   s      r-   rK   zSiglip2Encoder.__init__  sP    mm%PVPhPhJi$jQ%8%@$jk&+# %ks   A#Nr   r   r8   c                 T    |}| j                   D ]  } |||fi |} t        |      S )N)r    )r  r   )rC   r   r   r   r!   encoder_layers         r-   r   zSiglip2Encoder.forward  sC     &![[ 	M) M	 ??r,   r;   )r#   r$   r%   r&   r   rK   r   r'   r   r   r   r   r   r   r   s   @r-   r  r    s_    ,} ,  /3@ t+@ +,	@
 
@ @r,   r  c                        e Zd ZdZdef fdZe ed      ede	j                  de	j                  de	j                  d	ee   d
ef
d                     Z xZS )Siglip2VisionTransformerrR   rF   c                 l   t         |   |       || _        |j                  }t	        |      | _        t        |      | _        t        j                  ||j                        | _        t        |d      sdn|j                  | _        | j                  rt        |      | _        | j#                          y )Nr   vision_use_headT)rJ   rK   rF   rL   rE   r   r  encoderrO   r   r   post_layernormr   r  use_headr   head	post_initr   s      r-   rK   z!Siglip2VisionTransformer.__init__  s     &&	1&9%f- ll9&:O:OP$+F4E$FFLbLb==<VDDIr,   Ftie_last_hidden_statesr~   r   r[   r   r8   c                    | j                  ||      }t        | j                  ||      } | j                  d||d|}|j                  }| j                  |      }| j                  r| j                  ||      nd}	t        ||	      S )z
        spatial_shapes (`torch.LongTensor` of shape `(batch_size, 2)`):
            Tensor containing the spatial dimensions (height, width) of the input images.
        rF   r   r   r   r   Nr    pooler_outputr+   )	r   r	   rF   r  r    r  r  r  r   )
rC   r~   r   r[   r   r!   encoder_attention_maskencoder_outputsr    r'  s
             r-   r   z Siglip2VisionTransformer.forward  s     nE!:;;')"
 ,84<< ,
'1,
 ,
 ,== //0ABHL		"3^D[_)/'
 	
r,   )r#   r$   r%   _input_embed_layerr   rK   r   r   r   r'   r(   r   r   r   r   r   r   r   r   s   @r-   r  r    s    *2   E2!
''!
 !
 ((	!

 +,!
 
$!
  3  !
r,   r  c                        e Zd ZdZdef fdZee	 	 	 ddej                  dz  dej                  dz  dej                  dz  de
e   d	ef
d
              Z xZS )Siglip2TextTransformerr   rF   c                 @   t         |   |       || _        |j                  }t	        |      | _        t        |      | _        t        j                  ||j                        | _        t        j                  ||j                        | _        | j                          y r   )rJ   rK   rF   rL   r   r   r  r  rO   r   r   final_layer_normrP   projection_sizer  r   r   s      r-   rK   zSiglip2TextTransformer.__init__-  su     &&	/7%f- "YF<Q<Q RIIi)?)?@	r,   Nr   r   r   r   r8   c                 t   |t        d      |j                         }|j                  d|d         }| j                  ||      }t	        | j
                  ||      } | j                  d||d|}|j                  }| j                  |      }|d d dd d f   }	| j                  |	      }	t        ||	      S )NzYou have to specify input_idsr^   )r   r   r$  r%  r&  r+   )r   re   r   r   r	   rF   r  r    r.  r  r   )
rC   r   r   r   r   r   r!   r)  r    pooled_outputs
             r-   r   zSiglip2TextTransformer.forward8  s     <==nn&NN2{27	),W 3;;')
 ,84<< ,
'),
 ,
 ,== 112CD *!R(3		-0)/'
 	
r,   r   )r#   r$   r%   r*  r   rK   r   r   r'   r   r   r   r   r   r   r   s   @r-   r,  r,  *  s    *	0 	  *..2,0	&
<<$&&
 t+&
 llT)	&

 +,&
 
$&
  &
r,   r,  zL
    The text model from Siglip2 without any head or projection on top.
    c                        e Zd ZU eed<   dZdef fdZdej                  fdZ	d Z
e ed      e	 	 	 dd
ej                  d	z  dej                  d	z  dej                  d	z  dee   def
d                     Z xZS )Siglip2TextModelrF   )r   c                 d    t         |   |       t        |      | _        | j	                          y r;   )rJ   rK   r,  
text_modelr   rX   s     r-   rK   zSiglip2TextModel.__init__l  s&     08r,   r8   c                 B    | j                   j                  j                  S r;   r5  r   r   rB   s    r-   get_input_embeddingsz%Siglip2TextModel.get_input_embeddingsr      ))999r,   c                 :    || j                   j                  _        y r;   r7  rC   r   s     r-   set_input_embeddingsz%Siglip2TextModel.set_input_embeddingsu      5:""2r,   Fr!  Nr   r   r   r   c                 .     | j                   d|||d|S )a  
        Examples:

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

        >>> model = Siglip2TextModel.from_pretrained("google/siglip2-base-patch16-224")
        >>> tokenizer = AutoTokenizer.from_pretrained("google/siglip2-base-patch16-224")

        >>> # important: make sure to set padding="max_length" as that's how the model was trained
        >>> inputs = tokenizer(["a photo of a cat", "a photo of a dog"], padding="max_length", return_tensors="pt")

        >>> outputs = model(**inputs)
        >>> last_hidden_state = outputs.last_hidden_state
        >>> pooled_output = outputs.pooler_output  # pooled (EOS token) states
        ```r   r   r   r+   r5  rC   r   r   r   r   s        r-   r   zSiglip2TextModel.forwardx  s/    6 t 
)%
 	
 	
r,   r   )r#   r$   r%   r   r)   r  rK   rO   Moduler8  r<  r   r   r   r'   r   r   r   r   r   r   r   s   @r-   r3  r3  c  s      0 :bii :;  E2 *..2,0	
<<$&
 t+
 llT)	

 +,
 
$
  3  
r,   r3  c                        e Zd ZdZdef fdZd	dej                  dej                  dz  dej                  fdZ xZ	S )
r   zMultihead Attention Pooling.rF   c                    t         |           t        j                  t	        j
                  dd|j                              | _        t        j                  j                  |j                  |j                  d      | _
        t        j                  |j                  |j                        | _        t        |      | _        || _        |j                  | _        y )Nr   T)batch_firstr   )rJ   rK   rO   	Parameterr'   randnrL   r   MultiheadAttentionr   r   r   r   	layernormr   r   rF   r   rX   s     r-   rK   z-Siglip2MultiheadAttentionPoolingHead.__init__  s    \\%++aF4F4F"GH
44V5G5GIcIcqu4vf&8&8f>S>STf%33r,   Nhidden_stater   r8   c                    |j                   d   }| j                  j                  |dd      }||j                   d   |j                   d   }}t        | j                  |||      }||j                  d| j
                  |d      }|j                  d||      }|j                  t        j                  k(  rht        j                  |t        j                  d|j                  |j                        t        j                  |j                        j                        }| j                  ||||      d   }|}| j!                  |      }|| j#                  |      z   }|d d df   S )Nr   r   )rF   r   r   encoder_hidden_statesr^   r   r_   )	attn_mask)ri   r   repeatr	   rF   r   rt   ra   r'   boolwheretensorr`   finfominr   rI  r   )rC   rJ  r   rv   r   
target_len
source_lenr   s           r-   r   z,Siglip2MultiheadAttentionPoolingHead.forward  sF   !''*


!!*a3%%*[[^\5G5G5J
J6{{#-&2	N )!/!6!6q$..*VW!X!/!7!7J
!S "''5::5%*[[&S1F1FekkZEKK044&N ~~e\<Sa~bcde~~l3$((<"88AqD!!r,   r;   )
r#   r$   r%   r&   r   rK   r'   r   r   r   r   s   @r-   r   r     sB    &	42 	4"ELL "%,,QUBU "afamam "r,   r   zN
    The vision model from Siglip2 without any head or projection on top.
    c                        e Zd ZU eed<   dZdZdef fdZdej                  fdZ
eedej                  dej                  dej                   d	ee   def
d
              Z xZS )Siglip2VisionModelrF   r~   r   c                 d    t         |   |       t        |      | _        | j	                          y r;   )rJ   rK   r  vision_modelr   rX   s     r-   rK   zSiglip2VisionModel.__init__  s)     4V< 	r,   r8   c                 B    | j                   j                  j                  S r;   rZ  r   rR   rB   s    r-   r8  z'Siglip2VisionModel.get_input_embeddings        ++;;;r,   pixel_attention_maskr[   r   c                 .     | j                   d|||d|S )a  
        pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*):
            Mask to avoid performing attention on padding pixel indices.
        spatial_shapes (`torch.LongTensor` of shape `(batch_size, 2)`):
            Tensor containing the spatial dimensions (height, width) of the input images.

        Examples:

        ```python
        >>> from PIL import Image
        >>> import httpx
        >>> from io import BytesIO
        >>> from transformers import AutoProcessor, Siglip2VisionModel

        >>> model = Siglip2VisionModel.from_pretrained("google/siglip2-base-patch16-224")
        >>> processor = AutoProcessor.from_pretrained("google/siglip2-base-patch16-224")

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

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

        >>> outputs = model(**inputs)
        >>> last_hidden_state = outputs.last_hidden_state
        >>> pooled_output = outputs.pooler_output  # pooled features
        ```r~   r   r[   r+   rZ  rC   r~   r^  r[   r   s        r-   r   zSiglip2VisionModel.forward  s2    H !t   
%/)
 	
 	
r,   )r#   r$   r%   r   r)   main_input_namer  rK   rO   rB  r8  r   r   r'   r(   r   r   r   r   r   r   r   r   s   @r-   rW  rW    s      $O!2 <bii < '
'''
 $ll'
 ((	'

 +,'
 
$'
  '
r,   rW  c                       e Zd ZU eed<   def fdZdej                  fdZdej                  fdZ	e
e	 	 ddej                  d	ej                  dz  d
ej                  dz  dee   deez  f
d              Ze
e	 	 	 ddej&                  dz  dej                  dz  dej(                  dz  dee   deez  f
d              Ze
e	 	 	 	 	 	 	 d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z  dee   defd              Z xZS )r   rF   c                    t         |   |       t        |j                  t              s"t        dt        |j                         d      t        |j                  t              s"t        dt        |j                         d      |j                  }|j                  }t        j                  |      }t        j                  |      }|j                  | _        |j                  | _        t        j                  t!        j"                  d            | _        t        j                  t!        j"                  d            | _        | j)                          y )NzNconfig.text_config is expected to be of type Siglip2TextConfig but is of type .zRconfig.vision_config is expected to be of type Siglip2VisionConfig but is of type r   )rJ   rK   r<   text_configr   	TypeErrorrm   r   r   r3  _from_configrW  r5  rZ  rO   rF  r'   rG  r   r   r   )rC   rF   rg  r   r5  rZ  rY   s         r-   rK   zSiglip2Model.__init__  s    &,,.?@++,-Q0 
 &..0CD--./q2 
 ((,, &22;?
)66}E %//(55<<A7,,u{{1~6 	r,   r8   c                 B    | j                   j                  j                  S r;   r7  rB   s    r-   r8  z!Siglip2Model.get_input_embeddings/  r9  r,   r   c                 :    || j                   j                  _        y r;   r7  r;  s     r-   r<  z!Siglip2Model.set_input_embeddings2  r=  r,   Nr   r   r   r   c                 .     | j                   d|||d|S )ao  
        Examples:

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

        >>> model = AutoModel.from_pretrained("google/siglip2-base-patch16-224")
        >>> tokenizer = AutoTokenizer.from_pretrained("google/siglip2-base-patch16-224")

        >>> # important: make sure to set padding="max_length" as that's how the model was trained
        >>> inputs = tokenizer(["a photo of a cat", "a photo of a dog"], padding="max_length", return_tensors="pt")
        >>> with torch.no_grad():
        ...     text_features = model.get_text_features(**inputs)
        ```r?  r+   r@  rA  s        r-   get_text_featureszSiglip2Model.get_text_features5  s/    0 t 
)%
 	
 	
r,   r~   r^  r[   c                 .     | j                   d|||d|S )a  
        pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*):
            Mask to avoid performing attention on padding pixel indices.
        spatial_shapes (`torch.LongTensor` of shape `(batch_size, 2)`):
            Tensor containing the spatial dimensions (height, width) of the input images.

        Examples:

        ```python
        >>> import torch
        >>> from transformers import AutoProcessor, AutoModel
        >>> from transformers.image_utils import load_image

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = load_image(url)

        >>> model = AutoModel.from_pretrained("google/siglip2-base-patch16-224")
        >>> processor = AutoProcessor.from_pretrained("google/siglip2-base-patch16-224")

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

        >>> with torch.no_grad():
        ...     image_features = model.get_image_features(**inputs)
        ```
        r`  r+   ra  rb  s        r-   get_image_featureszSiglip2Model.get_image_featuresT  s2    D !t   
%/)
 	
 	
r,   return_lossc           	          | j                   d|||d|}	 | j                  d|||d|}
|	j                  }|
j                  }||j                  ddd      z  }||j                  ddd      z  }t	        j
                  ||j                         j                  |j                              }| j                  j                  |j                        | j                  j                  |j                        }}||j                         z  |z   }|j                         }d}|rt	        j                  |j                  d      |j                  	      }t	        j                  |       d|z  z   }t        j                  j                   j#                  ||z        }t	        j$                  |d
       }|j'                         }t)        ||||||
|	      S )ae  
        pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*):
            Mask to avoid performing attention on padding pixel indices.
        spatial_shapes (`torch.LongTensor` of shape `(batch_size, 2)`):
            Tensor containing the spatial dimensions (height, width) of the input images.
        return_loss (`bool`, *optional*):
            Whether or not to return the contrastive loss.

        Examples:

        ```python
        >>> from PIL import Image
        >>> import httpx
        >>> from io import BytesIO
        >>> from transformers import AutoProcessor, AutoModel
        >>> import torch

        >>> model = AutoModel.from_pretrained("google/siglip2-base-patch16-224")
        >>> processor = AutoProcessor.from_pretrained("google/siglip2-base-patch16-224")

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

        >>> texts = ["a photo of 2 cats", "a photo of 2 dogs"]
        >>> # important: we pass `padding=max_length` since the model was trained with this
        >>> inputs = processor(text=texts, images=image, padding="max_length", return_tensors="pt")

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

        >>> logits_per_image = outputs.logits_per_image
        >>> probs = torch.sigmoid(logits_per_image) # these are the probabilities
        >>> print(f"{probs[0][0]:.1%} that image 0 is '{texts[0]}'")
        31.9% that image 0 is 'a photo of 2 cats'
        ```
        r`  r?  rb   r^   T)r   r   keepdimNr   )r`   r   )r3   r4   r5   r0   r   r6   r7   r+   )rZ  r5  r'  normr'   r   trn   r`   r   r   expeyere   	ones_likerO   r   
logsigmoidsummeanr2   )rC   r   r~   r^  r[   r   r   rp  r   vision_outputstext_outputsr   r0   r5   r   r   r4   r3   rw  m1_diag1logliknlls                         r-   r   zSiglip2Model.forward~  s   d 6GT5F5F 6
%/)6
 	6
 4C4?? 4
)%4
 	4
 &33"00 $l&7&7!T&7&RR!K$4$4qb$$4$OO  ,,{LNN4D4G4GHZHZ4[\"&"2"2"5"5k6H6H"I4??K]K]^i^p^pKqZ)KOO,==
J*,,.))O003O<R<RSC881s7BHXX((33H4NOF99V,,C88:D-+#%* .
 	
r,   )NNr   )NNNNNNN)r#   r$   r%   r   r)   rK   rO   rB  r8  r<  r   r   r'   r   r   r   r*   r   rm  r(   r   ro  rO  r2   r   r   r   s   @r-   r   r     s   } @:bii :;")) ;  /3,0	
<<
 t+
 llT)	

 +,
 
+	+
  
:  264826	%
''$.%
 $llT1%
 ((4/	%

 +,%
 
+	+%
  %
P  .2154826.204#'^
##d*^
 ''$.^
 $llT1	^

 ((4/^
 t+^
 &&-^
 D[^
 +,^
 
^
  ^
r,   r   z
    Siglip2 vision encoder with an image classification head on top (a linear layer on top of the pooled final hidden states of
    the patch tokens) e.g. for ImageNet.
    c                       e Zd ZdZdZdeddf fdZdej                  fdZ	dej                  fd	Z
ee	 	 	 	 d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fd              Z xZS )r   r~   rX  rF   r8   Nc                 ~   t         |   |       |j                  | _        t        j	                  |j
                        }|j                  | _        |j                  dkD  r4t        j                  |j
                  j                  |j                        nt        j                         | _        | j                          y )Nr   )rJ   rK   
num_labelsrW  ri  r   rZ  rO   rP   rL   Identityr   r   )rC   rF   rZ  rY   s      r-   rK   z&Siglip2ForImageClassification.__init__  s      ++ *66v7K7KL(55 OUN_N_bcNcBIIf**668I8IJikititiv 	
 	r,   c                 B    | j                   j                  j                  S r;   r\  rB   s    r-   r8  z2Siglip2ForImageClassification.get_input_embeddings  r]  r,   r   c                 :    || j                   j                  _        y r;   r\  r;  s     r-   r<  z2Siglip2ForImageClassification.set_input_embeddings   s    7<$$4r,   r^  r[   labelsr   c                     | j                   |f||d|}|j                  }|Q|d   j                  |j                        }t	        j
                  ||z  d      t	        j
                  |d      z  }nt	        j                  |d      }| j                  |      }	d}
|| j                  ||	| j                        }
t        |
|	|j                  |j                        S )a  
        pixel_attention_mask (`torch.Tensor` of shape `(batch_size, image_size, image_size)`, *optional*):
            Mask to avoid performing attention on padding pixel indices.
        spatial_shapes (`torch.LongTensor` of shape `(batch_size, 2)`):
            Tensor containing the spatial dimensions (height, width) of the input images.
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).

        Examples:

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

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

        >>> # note: we are loading a `Siglip2Model` from the hub here,
        >>> # so the head will be randomly initialized, hence the predictions will be random if seed is not set above.
        >>> image_processor = AutoImageProcessor.from_pretrained("google/siglip2-base-patch16-224")
        >>> model = Siglip2ForImageClassification.from_pretrained("google/siglip2-base-patch16-224")

        >>> inputs = image_processor(images=image, return_tensors="pt")
        >>> outputs = model(**inputs)
        >>> logits = outputs.logits
        >>> # model predicts one of the two classes
        >>> predicted_class_idx = logits.argmax(-1).item()
        >>> print("Predicted class:", model.config.id2label[predicted_class_idx])
        Predicted class: LABEL_1
        ```
        )r   r[   N).Nr   rs  )r3   logitsr!   r"   )rZ  r    rn   r`   r'   rz  r{  r   loss_functionrF   r   r!   r"   )rC   r~   r^  r[   r  r   outputssequence_output	pool_maskr  r3   s              r-   r   z%Siglip2ForImageClassification.forward  s    ` /@d.?.?/
/)/
 	/
 "33  +,Y7::?;Q;QRI#ii)(CKeiiXaghNiiO#jja@O 1%%ffdkkBD$!//))	
 	
r,   )NNNN)r#   r$   r%   rc  r  r   rK   rO   rB  r8  r<  r   r   r'   r   r   r   r   r   r   r   r   s   @r-   r   r     s     %O!}  $<bii <=")) =  -14826&*J
llT)J
 $llT1J
 ((4/	J

 t#J
 +,J
 
J
  J
r,   r   )r   r   r3  rW  r   )r   )Dcollections.abcr   dataclassesr   typingr   numpyr   r'   torch.nnrO   torch.nn.functionalr   rr    r   r   activationsr   masking_utilsr	   modeling_layersr
   modeling_outputsr   r   r   modeling_utilsr   r   processing_utilsr   utilsr   r   r   r   utils.genericr   r   utils.output_capturingr   configuration_siglip2r   r   r   r   r/   r2   rB  rE   r   r   floatr   r   r   r   r   r  r  r,  r3  r   rW  r   r   __all__r+   r,   r-   <module>r     s  , % !       & ! 6 9 b b F & \ \ I 5 X X 
	<+ 	< 	< 
	< 	< 	< _K _  _@ebii eP%BII %^ %II%<<% 
% <<	%
 LL4'% % %.8)ryy 8)v 4 D Gi_ Gi GiT@RYY @D5
5 5
p6
3 6
r 
0
- 0

0
f,"299 ,"^ 
9
/ 9

9
x R
) R
 R
j h
$: h
h
Vr,   