
    Uhn                        d dl Zd dlmZmZmZmZmZmZm	Z	 d dl
Z
d dlmZ d dlmZmZmZ ddlmZ ddlmZmZmZ ddlmZmZ ddlmZmZ dd	lmZmZmZ d
dl m!Z!  ejD                  e#      Z$ G d dejJ                        Z& G d dejJ                        Z'e G d de             Z(	 d1dejJ                  de
jR                  de
jR                  de
jR                  dee
jR                     de*de*fdZ+ G d dejJ                        Z, G d dejJ                        Z- G d dejJ                        Z. G d  d!ejJ                        Z/ G d" d#ejJ                        Z0 G d$ d%ejJ                        Z1 G d& d'ejJ                        Z2 G d( d)ejJ                        Z3e G d* d+e(             Z4 ed,-       G d. d/e(             Z5g d0Z6y)2    N)CallableDictListOptionalSetTupleUnion)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FN)BaseModelOutputBaseModelOutputWithPoolingImageClassifierOutput)ALL_ATTENTION_FUNCTIONSPreTrainedModel) find_pruneable_heads_and_indicesprune_linear_layer)auto_docstringlogging	torch_int   )IJepaConfigc                   `     e Zd ZdZ fdZddej                  dedej                  fdZ xZ	S )IJepaPatchEmbeddingsz
    This class turns `pixel_values` of shape `(batch_size, num_channels, height, width)` into the initial
    `hidden_states` (patch embeddings) of shape `(batch_size, seq_length, hidden_size)` to be consumed by a
    Transformer.
    c                    t         |           |j                  |j                  }}|j                  |j
                  }}t        |t        j                  j                        r|n||f}t        |t        j                  j                        r|n||f}|d   |d   z  |d   |d   z  z  }|| _        || _        || _        || _
        t        j                  ||||      | _        y )Nr   r   )kernel_sizestride)super__init__
image_size
patch_sizenum_channelshidden_size
isinstancecollectionsabcIterablenum_patchesnnConv2d
projection)selfconfigr"   r#   r$   r%   r*   	__class__s          z/var/www/catia.catastroantioquia-mas.com/valormas/lib/python3.12/site-packages/transformers/models/ijepa/modeling_ijepa.pyr!   zIJepaPatchEmbeddings.__init__    s    !'!2!2F4E4EJ
$*$7$79K9Kk#-j+//:R:R#SZZdfpYq
#-j+//:R:R#SZZdfpYq
!!}
15*Q-:VW=:XY$$(&))L+:^hi    pixel_valuesinterpolate_pos_encodingreturnc                    |j                   \  }}}}|| j                  k7  rt        d| j                   d| d      |sV|| j                  d   k7  s|| j                  d   k7  r2t        d| d| d| j                  d    d| j                  d    d		      | j	                  |      j                  d
      j                  dd
      }|S )NzoMake sure that the channel dimension of the pixel values match with the one set in the configuration. Expected z	 but got .r   r   zInput image size (*z) doesn't match model (z).   )shaper$   
ValueErrorr"   r-   flatten	transpose)r.   r3   r4   
batch_sizer$   heightwidth
embeddingss           r1   forwardzIJepaPatchEmbeddings.forward/   s    2>2D2D/
L&%4,,,!../yaI  (++u8J/J (% 9+,Adooa.@-AE  __\2::1=GG1M
r2   F)
__name__
__module____qualname____doc__r!   torchTensorboolrB   __classcell__r0   s   @r1   r   r      s3    jELL D ]b]i]i r2   r   c            	            e Zd ZdZddededdf fdZdej                  de	d	e	dej                  fd
Z
	 	 ddej                  deej                     dedej                  fdZ xZS )IJepaEmbeddingszb
    Construct the CLS token, position and patch embeddings. Optionally, also the mask token.
    r/   use_mask_tokenr5   Nc                    t         |           |r4t        j                  t	        j
                  dd|j                              nd | _        t        |      | _	        | j                  j                  }t        j                  t	        j                  d||j                              | _        t        j                  |j                        | _        |j                   | _        || _        y )Nr   )r    r!   r+   	ParameterrH   zerosr%   
mask_tokenr   patch_embeddingsr*   randnposition_embeddingsDropouthidden_dropout_probdropoutr#   r/   )r.   r/   rO   r*   r0   s       r1   r!   zIJepaEmbeddings.__init__E   s    Q_",,u{{1a9K9K'LMei 4V <++77#%<<A{FL^L^0_#` zz&"<"<= ++r2   rA   r?   r@   c                 0   |j                   d   }| j                  j                   d   }t        j                  j	                         s||k(  r||k(  r| j                  S | j                  }|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|      }|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   g      ?r   r   r9   bicubicF)sizemodealign_corners)r:   rV   rH   jit
is_tracingr#   r   reshapepermuter+   
functionalinterpolateview)r.   rA   r?   r@   r*   num_positionspatch_pos_embeddim
new_height	new_widthsqrt_num_positionss              r1   r4   z(IJepaEmbeddings.interpolate_pos_encodingO   s#    !&&q)0066q9 yy##%+*F6UZ?+++22r"t.
T__,	&}c'9:)11!5GI[]`a)11!Q1=--33i(	 4 
 *11!Q1=BB1b#Nr2   r3   bool_masked_posr4   c                 x   |j                   \  }}}}| j                  ||      }|Z|j                   d   }	| j                  j                  ||	d      }
|j	                  d      j                  |
      }|d|z
  z  |
|z  z   }|r|| j                  |||      z   }n|| j                  z   }| j                  |      }|S )N)r4   r   r[         ?)	r:   rT   rS   expand	unsqueezetype_asr4   rV   rY   )r.   r3   rm   r4   r>   _r?   r@   rA   
seq_lengthmask_tokensmasks               r1   rB   zIJepaEmbeddings.forwardv   s     (4'9'9$
Avu**<Rj*k
&#))!,J//00ZLK",,R088ED#sTz2[45GGJ $#d&C&CJPVX]&^^J#d&>&>>J\\*-
r2   rC   NF)rD   rE   rF   rG   r   rJ   r!   rH   rI   intr4   r   
BoolTensorrB   rK   rL   s   @r1   rN   rN   @   s    { D T %5<< % %UX %]b]i]i %T 7;).	ll "%"2"23 #'	
 
r2   rN   c                       e Zd ZeZdZdZdZddgZdZ	dZ
deej                  ej                  ej                  f   ddfd	Zy)
IJepaPreTrainedModelijepar3   TrN   
IJepaLayermoduler5   Nc                 l   t        |t        j                  t        j                  f      rt        j                  j                  |j                  j                  j                  t        j                        d| j                  j                        j                  |j                  j                        |j                  _        |j                  %|j                  j                  j                          yyt        |t        j                         rJ|j                  j                  j                          |j                  j                  j#                  d       yt        |t$              rt        j                  j                  |j&                  j                  j                  t        j                        d| j                  j                        j                  |j&                  j                        |j&                  _        |j(                  %|j(                  j                  j                          yyy)zInitialize the weights        )meanstdNro   )r&   r+   Linearr,   inittrunc_normal_weightdatatorH   float32r/   initializer_rangedtypebiaszero_	LayerNormfill_rN   rV   rS   )r.   r~   s     r1   _init_weightsz"IJepaPreTrainedModel._init_weights   s   fryy"))45 "$!6!6""%%emm43DKKDaDa "7 "b$$% MM {{&  &&( '-KK""$MM$$S)0.0gg.C.C**//225==AKK11 /D / b++112	 &&+
   ,!!&&,,. - 1r2   )rD   rE   rF   r   config_classbase_model_prefixmain_input_namesupports_gradient_checkpointing_no_split_modules_supports_sdpa_supports_flash_attn_2r	   r+   r   r,   r   r    r2   r1   r{   r{      sZ    L$O&*#*L9N!/E"))RYY*L$M /RV /r2   r{   r~   querykeyvalueattention_maskscalingrY   c                    t        j                  ||j                  dd            |z  }t        j                  j                  |dt         j                        j                  |j                        }t        j                  j                  ||| j                        }|||z  }t        j                  ||      }	|	j                  dd      j                         }	|	|fS )Nr[   )ri   r   )ptrainingr   r9   )rH   matmulr=   r+   rd   softmaxr   r   r   rY   r   
contiguous)
r~   r   r   r   r   r   rY   kwargsattn_weightsattn_outputs
             r1   eager_attention_forwardr      s     <<s}}R'<=GL ==((2U]](SVVW\WbWbcL ==((6??([L !#n4,,|U3K''1-88:K$$r2   c            
            e Zd Zdeddf fdZdej                  dej                  fdZ	 d
deej                     de	de
eej                  ej                  f   eej                     f   fd	Z xZS )IJepaSelfAttentionr/   r5   Nc                 2   t         |           |j                  |j                  z  dk7  r2t	        |d      s&t        d|j                   d|j                   d      || _        |j                  | _        t        |j                  |j                  z        | _        | j                  | j                  z  | _	        |j                  | _        | j                  dz  | _        d| _        t        j                  |j                  | j                  |j                         | _        t        j                  |j                  | j                  |j                         | _        t        j                  |j                  | j                  |j                         | _        y )	Nr   embedding_sizezThe hidden size z4 is not a multiple of the number of attention heads r7   g      F)r   )r    r!   r%   num_attention_headshasattrr;   r/   rx   attention_head_sizeall_head_sizeattention_probs_dropout_probdropout_probr   	is_causalr+   r   qkv_biasr   r   r   r.   r/   r0   s     r1   r!   zIJepaSelfAttention.__init__   sF    : ::a?PVXhHi"6#5#5"6 7334A7 
 #)#=#= #&v'9'9F<V<V'V#W !558P8PP"??//5YYv1143E3EFOO\
99V//1C1C&//ZYYv1143E3EFOO\
r2   xc                     |j                         d d | j                  | j                  fz   }|j                  |      }|j	                  dddd      S )Nr[   r   r9   r   r   )r]   r   r   rf   rc   )r.   r   new_x_shapes      r1   transpose_for_scoresz'IJepaSelfAttention.transpose_for_scores   sL    ffhsmt'?'?AYAY&ZZFF;yyAq!$$r2   	head_maskoutput_attentionsc           
         | j                  | j                  |            }| j                  | j                  |            }| j                  | j                  |            }t        }| j
                  j                  dk7  rN| j
                  j                  dk(  r|rt        j                  d       nt        | j
                  j                     } || ||||| j                  | j                  | j                  sdn| j                        \  }}	|j                         d d | j                  fz   }
|j!                  |
      }|r||	f}|S |f}|S )Neagersdpaz`torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to eager attention. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.r   )r   r   rY   r   )r   r   r   r   r   r/   _attn_implementationloggerwarning_oncer   r   r   r   r   r]   r   rb   )r.   hidden_statesr   r   	key_layervalue_layerquery_layerattention_interfacecontext_layerattention_probsnew_context_layer_shapeoutputss               r1   rB   zIJepaSelfAttention.forward   s=    --dhh}.EF	//

=0IJ//

=0IJ(?;;++w6{{//69>O##L
 '>dkk>^>^&_#)<nnLL#}}C$2C2C	*
& #0"4"4"6s";t?Q?Q>S"S%--.EF6G=/2 O\M]r2   rw   )rD   rE   rF   r   r!   rH   rI   r   r   rJ   r	   r   rB   rK   rL   s   @r1   r   r      s    ]{ ]t ](%ell %u|| % bg!(0(>!Z^!	uU\\5<</0%2EE	F!r2   r   c                   |     e Zd ZdZdeddf fdZdej                  dej                  dej                  fdZ xZ	S )	IJepaSelfOutputz
    The residual connection is defined in IJepaLayer instead of here (as is the case with other models), due to the
    layernorm applied before each block.
    r/   r5   Nc                     t         |           t        j                  |j                  |j                        | _        t        j                  |j                        | _        y N)	r    r!   r+   r   r%   denserW   rX   rY   r   s     r1   r!   zIJepaSelfOutput.__init__  sB    YYv1163E3EF
zz&"<"<=r2   r   input_tensorc                 J    | j                  |      }| j                  |      }|S r   r   rY   r.   r   r   s      r1   rB   zIJepaSelfOutput.forward  s$    

=1]3r2   )
rD   rE   rF   rG   r   r!   rH   rI   rB   rK   rL   s   @r1   r   r     sD    
>{ >t >
U\\  RWR^R^ r2   r   c                        e Zd Zdeddf fdZdee   ddfdZ	 	 ddej                  de
ej                     d	edeeej                  ej                  f   eej                     f   fd
Z xZS )IJepaAttentionr/   r5   Nc                     t         |           t        |      | _        t	        |      | _        t               | _        y r   )r    r!   r   	attentionr   outputsetpruned_headsr   s     r1   r!   zIJepaAttention.__init__!  s0    +F3%f-Er2   headsc                 >   t        |      dk(  ry t        || j                  j                  | j                  j                  | j
                        \  }}t        | j                  j                  |      | j                  _        t        | j                  j                  |      | j                  _        t        | j                  j                  |      | j                  _	        t        | j                  j                  |d      | j                  _        | j                  j                  t        |      z
  | j                  _        | j                  j                  | j                  j                  z  | j                  _        | j
                  j                  |      | _        y )Nr   r   ri   )lenr   r   r   r   r   r   r   r   r   r   r   r   union)r.   r   indexs      r1   prune_headszIJepaAttention.prune_heads'  s   u:?74>>55t~~7Y7Y[_[l[l
u
  2$..2F2FN/0B0BEJ1$..2F2FN.t{{/@/@%QO .2^^-O-ORUV[R\-\*'+~~'I'IDNNLnLn'n$ --33E:r2   r   r   r   c                 h    | j                  |||      }| j                  |d   |      }|f|dd  z   }|S )Nr   r   )r   r   )r.   r   r   r   self_outputsattention_outputr   s          r1   rB   zIJepaAttention.forward9  sE     ~~mY@QR;;|AF#%QR(88r2   rw   )rD   rE   rF   r   r!   r   rx   r   rH   rI   r   rJ   r	   r   rB   rK   rL   s   @r1   r   r      s    "{ "t ";S ;d ;* -1"'	|| ELL)  	
 
uU\\5<</0%2EE	Fr2   r   c                   `     e Zd Zdeddf fdZdej                  dej                  fdZ xZS )IJepaIntermediater/   r5   Nc                    t         |           t        j                  |j                  |j
                        | _        t        |j                  t              rt        |j                     | _        y |j                  | _        y r   )r    r!   r+   r   r%   intermediate_sizer   r&   
hidden_actstrr   intermediate_act_fnr   s     r1   r!   zIJepaIntermediate.__init__H  s]    YYv1163K3KL
f''-'-f.?.?'@D$'-'8'8D$r2   r   c                 J    | j                  |      }| j                  |      }|S r   )r   r   )r.   r   s     r1   rB   zIJepaIntermediate.forwardP  s&    

=100?r2   	rD   rE   rF   r   r!   rH   rI   rB   rK   rL   s   @r1   r   r   G  s1    9{ 9t 9U\\ ell r2   r   c                   x     e Zd Zdeddf fdZdej                  dej                  dej                  fdZ xZS )IJepaOutputr/   r5   Nc                     t         |           t        j                  |j                  |j
                        | _        t        j                  |j                        | _	        y r   )
r    r!   r+   r   r   r%   r   rW   rX   rY   r   s     r1   r!   zIJepaOutput.__init__X  sB    YYv779K9KL
zz&"<"<=r2   r   r   c                 T    | j                  |      }| j                  |      }||z   }|S r   r   r   s      r1   rB   zIJepaOutput.forward]  s.    

=1]3%4r2   r   rL   s   @r1   r   r   W  s?    >{ >t >
U\\  RWR^R^ r2   r   c                        e Zd ZdZdeddf fdZ	 	 d
dej                  deej                     de	de
eej                  ej                  f   eej                     f   fd	Z xZS )r}   z?This corresponds to the Block class in the timm implementation.r/   r5   Nc                 r   t         |           |j                  | _        d| _        t	        |      | _        t        |      | _        t        |      | _	        t        j                  |j                  |j                        | _        t        j                  |j                  |j                        | _        y )Nr   eps)r    r!   chunk_size_feed_forwardseq_len_dimr   r   r   intermediater   r   r+   r   r%   layer_norm_epslayernorm_beforelayernorm_afterr   s     r1   r!   zIJepaLayer.__init__i  s    '-'E'E$'/-f5!&) "V-?-?VEZEZ [!||F,>,>FDYDYZr2   r   r   r   c                     | j                  | j                  |      ||      }|d   }|dd  }||z   }| j                  |      }| j                  |      }| j	                  ||      }|f|z   }|S )N)r   r   r   )r   r   r   r   r   )r.   r   r   r   self_attention_outputsr   r   layer_outputs           r1   rB   zIJepaLayer.forwards  s     "&!!-0/ "0 "

 2!4(, )=8 ++M:((6 {{<?/G+r2   rw   )rD   rE   rF   rG   r   r!   rH   rI   r   rJ   r	   r   rB   rK   rL   s   @r1   r}   r}   f  s    I[{ [t [ -1"'	|| ELL)  	
 
uU\\5<</0%2EE	Fr2   r}   c                        e Zd Zdeddf fdZ	 	 	 	 ddej                  deej                     deded	ede	e
ef   fd
Z xZS )IJepaEncoderr/   r5   Nc                     t         |           || _        t        j                  t        |j                        D cg c]  }t        |       c}      | _        d| _	        y c c}w rw   )
r    r!   r/   r+   
ModuleListrangenum_hidden_layersr}   layergradient_checkpointing)r.   r/   rs   r0   s      r1   r!   zIJepaEncoder.__init__  sN    ]]fF^F^@_#`1Jv$6#`a
&+# $as   A#r   r   r   output_hidden_statesreturn_dictc                 t   |rdnd }|rdnd }t        | j                        D ]h  \  }}	|r||fz   }|||   nd }
| j                  r+| j                  r| j	                  |	j
                  ||
|      }n
 |	||
|      }|d   }|s`||d   fz   }j |r||fz   }|st        d |||fD              S t        |||      S )Nr   r   r   c              3   &   K   | ]	  }||  y wr   r   ).0vs     r1   	<genexpr>z'IJepaEncoder.forward.<locals>.<genexpr>  s     mq_`_lms   )last_hidden_stater   
attentions)	enumerater  r  r   _gradient_checkpointing_func__call__tupler   )r.   r   r   r   r  r  all_hidden_statesall_self_attentionsilayer_modulelayer_head_masklayer_outputss               r1   rB   zIJepaEncoder.forward  s     #7BD$5b4(4 	POA|#$58H$H!.7.CilO**t}} $ A A ))!#%	! !-]OM^ _)!,M &9]1=M<O&O#'	P*   1]4D Dm]4EGZ$[mmm++*
 	
r2   )NFFT)rD   rE   rF   r   r!   rH   rI   r   rJ   r	   r  r   rB   rK   rL   s   @r1   r   r     sz    ,{ ,t , -1"'%* )
||)
 ELL))
  	)

 #)
 )
 
uo%	&)
r2   r   c                   *     e Zd Zdef fdZd Z xZS )IJepaPoolerr/   c                     t         |           t        j                  |j                  |j
                        | _        t        |j                     | _	        y r   )
r    r!   r+   r   r%   pooler_output_sizer   r   
pooler_act
activationr   s     r1   r!   zIJepaPooler.__init__  s>    YYv1163L3LM
 !2!23r2   c                 \    |d d df   }| j                  |      }| j                  |      }|S )Nr   )r   r  )r.   r   first_token_tensorpooled_outputs       r1   rB   zIJepaPooler.forward  s6     +1a40

#566r2   )rD   rE   rF   r   r!   rB   rK   rL   s   @r1   r  r    s    4{ 4
r2   r  c                       e Zd Zddededef fdZdefdZdee	e
e	   f   ddfd	Ze	 	 	 	 	 	 	 dd
eej                     deej                      deej                     dee   dee   dee   dee   deeef   fd       Z xZS )
IJepaModelr/   add_pooling_layerrO   c                    t         |   |       || _        t        ||      | _        t        |      | _        t        j                  |j                  |j                        | _        |rt        |      nd| _        | j                          y)z
        add_pooling_layer (bool, *optional*, defaults to `True`):
            Whether to add a pooling layer
        use_mask_token (`bool`, *optional*, defaults to `False`):
            Whether to use a mask token for masked image modeling.
        )rO   r   N)r    r!   r/   rN   rA   r   encoderr+   r   r%   r   	layernormr  pooler	post_init)r.   r/   r%  rO   r0   s       r1   r!   zIJepaModel.__init__  sm     	 )&P#F+f&8&8f>S>ST->k&)D 	r2   r5   c                 .    | j                   j                  S r   )rA   rT   )r.   s    r1   get_input_embeddingszIJepaModel.get_input_embeddings  s    ///r2   heads_to_pruneNc                     |j                         D ]7  \  }}| j                  j                  |   j                  j	                  |       9 y)z
        Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
        class PreTrainedModel
        N)itemsr'  r  r   r   )r.   r-  r  r   s       r1   _prune_headszIJepaModel._prune_heads  sE    
 +002 	CLE5LLu%//;;EB	Cr2   r3   rm   r   r   r  r4   r  c                    ||n| j                   j                  }||n| j                   j                  }||n| j                   j                  }|t	        d      | j                  || j                   j                        }| j                  j                  j                  j                  j                  }|j                  |k7  r|j                  |      }| j                  |||      }	| j                  |	||||      }
|
d   }| j                  |      }| j                  | j                  |      nd}|s|||fn|f}||
dd z   S t!        |||
j"                  |
j$                        S )z
        bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`, *optional*):
            Boolean masked positions. Indicates which patches are masked (1) and which aren't (0).
        Nz You have to specify pixel_values)rm   r4   )r   r   r  r  r   r   )r  pooler_outputr   r  )r/   r   r  use_return_dictr;   get_head_maskr  rA   rT   r-   r   r   r   r'  r(  r)  r   r   r  )r.   r3   rm   r   r   r  r4   r  expected_dtypeembedding_outputencoder_outputssequence_outputr"  head_outputss                 r1   rB   zIJepaModel.forward  s    2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B]?@@ &&y$++2O2OP	 99DDKKQQ/'??>:L??/Tl + 
 ,,/!5# ' 
 *!,..98<8OO4UY?L?XO];_n^pL/!""555)-')77&11	
 	
r2   )FFNNNNNNN)rD   rE   rF   r   rJ   r!   r   r,  r   rx   r   r0  r   r   rH   rI   ry   r	   r   r   rB   rK   rL   s   @r1   r$  r$    s    { t ]a $0&: 0C4T#Y+? CD C  046:,0,0/337&*;
u||,;
 "%"2"23;
 ELL)	;

 $D>;
 'tn;
 #+4.;
 d^;
 
u00	1;
 ;
r2   r$  a  
    IJepa Model transformer with an image classification head on top (a linear layer on top of the final hidden states)
    e.g. for ImageNet.

    <Tip>

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

    </Tip>
    )custom_introc                        e Zd Zdeddf fdZe	 	 	 	 	 	 	 ddeej                     deej                     deej                     dee	   d	ee	   d
ee	   dee	   de
eef   fd       Z xZS )IJepaForImageClassificationr/   r5   Nc                 .   t         |   |       |j                  | _        t        |d      | _        |j                  dkD  r*t        j                  |j                  |j                        nt        j                         | _	        | j                          y )NF)r%  r   )r    r!   
num_labelsr$  r|   r+   r   r%   Identity
classifierr*  r   s     r1   r!   z$IJepaForImageClassification.__init__?  ss      ++%@
 OUN_N_bcNc"))F$6$68I8IJikititiv 	r2   r3   r   labelsr   r  r4   r  c                 n   ||n| j                   j                  }| j                  ||||||      }|d   }	| j                  |	j	                  d            }
d}||j                  |
j                        }| j                   j                  | j                  dk(  rd| j                   _        nl| j                  dkD  rL|j                  t        j                  k(  s|j                  t        j                  k(  rd| j                   _        nd| j                   _        | j                   j                  dk(  rIt               }| j                  dk(  r& ||
j                         |j                               }n ||
|      }n| j                   j                  dk(  r=t               } ||
j!                  d	| j                        |j!                  d	            }n,| j                   j                  dk(  rt#               } ||
|      }|s|
f|dd z   }||f|z   S |S t%        ||
|j&                  |j(                  
      S )a  
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        N)r   r   r  r4   r  r   r   r   
regressionsingle_label_classificationmulti_label_classificationr[   )losslogitsr   r  )r/   r3  r|   rA  r   r   deviceproblem_typer?  r   rH   longrx   r   squeezer   rf   r
   r   r   r  )r.   r3   r   rB  r   r  r4   r  r   r8  rH  rG  loss_fctr   s                 r1   rB   z#IJepaForImageClassification.forwardK  s   " &1%<k$++B]B]**/!5%=#  
 "!*!5!5!!5!<=YYv}}-F{{''/??a'/;DKK,__q(fllejj.HFLL\a\e\eLe/LDKK,/KDKK,{{''<7"9??a'#FNN$4fnn6FGD#FF3D))-JJ+-B @&++b/R))-II,./Y,F)-)9TGf$EvE$!//))	
 	
r2   r:  )rD   rE   rF   r   r!   r   r   rH   rI   rJ   r	   r  r   rB   rK   rL   s   @r1   r=  r=  0  s    
{ 
t 
  04,0)-,0/337&*A
u||,A
 ELL)A
 &	A

 $D>A
 'tnA
 #+4.A
 d^A
 
u++	,A
 A
r2   r=  )r{   r$  r=  )r   )7collections.abcr'   typingr   r   r   r   r   r   r	   rH   torch.nnr+   r
   r   r   activationsr   modeling_outputsr   r   r   modeling_utilsr   r   pytorch_utilsr   r   utilsr   r   r   configuration_ijepar   
get_loggerrD   r   Moduler   rN   r{   rI   floatr   r   r   r   r   r   r}   r   r  r$  r=  __all__r   r2   r1   <module>r[     s    D D D   A A ! b b F Q 7 7 , 
		H	%$299 $NNbii Nb /? / /N %II%<<% 
% <<	%
 U\\*% % %<; ;|bii $$RYY $N		  ")) ' 'T0
299 0
f"))  Z
% Z
 Z
z O
"6 O
O
d Pr2   