
    qib                     >   d Z ddl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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$  ejJ                  e&      Z' G d dejP                        Z) G d dejP                        Z*	 	 dBdejP                  dejV                  dejV                  dejV                  dejV                  dz  de,dz  de,dee   fdZ- G d d ejP                        Z. G d! d"ejP                        Z/ G d# d$ejP                        Z0 G d% d&ejP                        Z1dCd'ejV                  d(e,d)e2d*ejV                  fd+Z3 G d, d-ejP                        Z4 G d. d/ejP                        Z5 G d0 d1ejP                        Z6 G d2 d3e      Z7 G d4 d5ejP                        Z8e G d6 d7e             Z9e G d8 d9e9             Z: ed:;       G d< d=e9             Z; ed>;       G d? d@ee9             Z<g dAZ=y)DzPyTorch DINOv2 model.    N)Callable)nn   )initialization)ACT2FN)BackboneMixin)GradientCheckpointingLayer)BackboneOutputBaseModelOutputBaseModelOutputWithPoolingImageClassifierOutput)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack)TransformersKwargsauto_docstringlogging	torch_int)can_return_tuplemerge_with_config_defaults)capture_outputs   )Dinov2Configc                        e Zd ZdZ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j                  dz  dej                  fdZ
 xZS )Dinov2EmbeddingszM
    Construct the CLS token, mask token, position and patch embeddings.
    configreturnNc                 z   t         |           t        j                  t	        j
                  dd|j                              | _        |j                  r8t        j                  t	        j                  d|j                              | _
        t        |      | _        | j                  j                  }t        j                  t	        j
                  d|dz   |j                              | _        t        j                  |j                         | _        |j$                  | _        |j                  | _        || _        y )Nr   )super__init__r   	Parametertorchrandnhidden_size	cls_tokenuse_mask_tokenzeros
mask_tokenDinov2PatchEmbeddingspatch_embeddingsnum_patchesposition_embeddingsDropouthidden_dropout_probdropout
patch_sizer   )selfr   r+   	__class__s      \/opt/pipecat/venv/lib/python3.12/site-packages/transformers/models/dinov2/modeling_dinov2.pyr    zDinov2Embeddings.__init__+   s    ekk!Q8J8J&KL   ll5;;q&:L:L+MNDO 5f =++77#%<<A{QPVPbPb0c#d zz&"<"<= ++$33    
embeddingsheightwidthc                    |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      }|j                  }t        j                  j                  |j                  t        j                        |	|
fdd	
      j                  |      }|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 and interpolation at torch.float32 precision.

        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   Ng      ?r   r      bicubicF)sizemodealign_cornersdtypedim)shaper,   r"   jit
is_tracingr0   r   reshapepermuter@   r   
functionalinterpolatetofloat32viewcat)r1   r5   r6   r7   r+   num_positionsclass_pos_embedpatch_pos_embedrB   
new_height	new_widthsqrt_num_positionstarget_dtypes                r3   interpolate_pos_encodingz)Dinov2Embeddings.interpolate_pos_encoding9   s    !&&q)A-0066q9A= yy##%+*F6UZ?+++221bqb59221ab59r"t.
T__,	&}c'9:)11!5GI[]`a)11!Q1=&,,--33u}}-i(	 4 

 "<"
  	 *11!Q1=BB1b#Nyy/?;CCr4   pixel_valuesbool_masked_posc                 D   |j                   \  }}}}| j                  j                  j                  j                  }| j                  |j                  |            }|d| j                  rXt        j                  |j                  d      | j                  j                  |j                        j                  d      |      }| j                  j                  |dd      }	t        j                  |	|fd      }|| j                  |||      z   }| j                  |      }|S )Nr?   r9   r   r   rA   )rC   r*   
projectionweightr@   rJ   r&   r"   where	unsqueezer(   r%   expandrM   rU   r/   )
r1   rV   rW   
batch_size_r6   r7   rT   r5   
cls_tokenss
             r3   forwardzDinov2Embeddings.forwarda   s    '3'9'9$
Avu,,77>>DD**<???+NO
&4+>+>))"-t/A/A*BRBR/S/]/]^_/`blJ
 ^^**:r2>
YY
J7Q?
  $"?"?
FTY"ZZ
\\*-
r4   N)__name__
__module____qualname____doc__r   r    r"   TensorintrU   ra   __classcell__r2   s   @r3   r   r   &   s}    |  &D5<< &D &DUX &D]b]i]i &DPELL 5<<RVCV bgbnbn r4   r   c                   Z     e Zd ZdZ fdZdej                  dej                  fdZ xZS )r)   z
    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)r   r    
image_sizer0   num_channelsr$   
isinstancecollectionsabcIterabler+   r   Conv2drY   )r1   r   ro   r0   rp   r$   r+   r2   s          r3   r    zDinov2PatchEmbeddings.__init__~   s    !'!2!2F4E4EJ
$*$7$79K9Kk#-j+//:R:R#SZZdfpYq
#-j+//:R:R#SZZdfpYq
!!}
15*Q-:VW=:XY$$(&))L+:^hir4   rV   r   c                     |j                   d   }|| j                  k7  rt        d| j                   d| d      | j                  |      j	                  d      j                  dd      }|S )Nr   zoMake sure that the channel dimension of the pixel values match with the one set in the configuration. Expected z	 but got .r:   )rC   rp   
ValueErrorrY   flatten	transpose)r1   rV   rp   r5   s       r3   ra   zDinov2PatchEmbeddings.forward   sz    #))!,4,,,!../yaI  __\2::1=GG1M
r4   )	rc   rd   re   rf   r    r"   rg   ra   ri   rj   s   @r3   r)   r)   w   s)    jELL U\\ r4   r)   modulequerykeyvalueattention_maskscalingr/   kwargsc                    ||j                  d      dz  }t        j                  ||j                  dd            |z  }|||z   }t        j
                  j                  |d      }t        j
                  j                  ||| j                        }t        j                  ||      }	|	j                  dd      j                         }	|	|fS )Nr9         r:   r   rA   )ptrainingr   )
r<   r"   matmulrz   r   rH   softmaxr/   r   
contiguous)
r{   r|   r}   r~   r   r   r/   r   attn_weightsattn_outputs
             r3   eager_attention_forwardr      s     **R.D( <<s}}Q':;gEL!#n4==((2(>L==((6??([L,,|U3K''1-88:K$$r4   c                   z     e Zd Zdef fdZdej                  deej                  ej                  f   fdZ xZ	S )Dinov2SelfAttentionr   c                 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 rw   r   Fbias)r   r    r$   num_attention_headshasattrrx   r   rh   attention_head_sizeall_head_sizeattention_probs_dropout_probdropout_probr   	is_causalr   Linearqkv_biasr|   r}   r~   r1   r   r2   s     r3   r    zDinov2SelfAttention.__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\
r4   hidden_statesr   c           
         |j                   d   }|d| j                  | j                  f} | j                  |      j                  | j                  dd      } | j                  |      j                  | j                  dd      } | j                  |      j                  | j                  dd      }t        j                  | j                  j                  t              } || |||d | j                  | j                  | j                  sdn| j                         \  }}	|j#                         d d | j$                  fz   }
|j'                  |
      }||	fS )Nr   r9   r   r:           )r   r   r/   )rC   r   r   r}   rL   rz   r~   r|   r   get_interfacer   _attn_implementationr   r   r   r   r   r<   r   rF   )r1   r   r^   	new_shape	key_layervalue_layerquery_layerattention_interfacecontext_layerattention_probsnew_context_layer_shapes              r3   ra   zDinov2SelfAttention.forward   sF   "((+
D$<$<d>V>VV	0DHH]+00)<FFq!L	4djj/44i@JJ1aP4djj/44i@JJ1aP(?(M(MKK,,.E)
 *=nnLL#}}C$2C2C	*
& #0"4"4"6s";t?Q?Q>S"S%--.EFo--r4   )
rc   rd   re   r   r    r"   rg   tuplera   ri   rj   s   @r3   r   r      s:    ]| ](.U\\ .eELL%,,<V6W .r4   r   c                   x     e Zd ZdZdef fdZdej                  dej                  dej                  fdZ xZ	S )Dinov2SelfOutputz
    The residual connection is defined in Dinov2Layer instead of here (as is the case with other models), due to the
    layernorm applied before each block.
    r   c                     t         |           t        j                  |j                  |j                        | _        t        j                  |j                        | _        y rb   )	r   r    r   r   r$   denser-   r.   r/   r   s     r3   r    zDinov2SelfOutput.__init__   sB    YYv1163E3EF
zz&"<"<=r4   r   input_tensorr   c                 J    | j                  |      }| j                  |      }|S rb   )r   r/   )r1   r   r   s      r3   ra   zDinov2SelfOutput.forward   s$    

=1]3r4   
rc   rd   re   rf   r   r    r"   rg   ra   ri   rj   s   @r3   r   r      s=    
>| >
U\\  RWR^R^ r4   r   c                   \     e Zd Zdef fdZdej                  dej                  fdZ xZS )Dinov2Attentionr   c                 b    t         |           t        |      | _        t	        |      | _        y rb   )r   r    r   	attentionr   outputr   s     r3   r    zDinov2Attention.__init__   s&    ,V4&v.r4   r   r   c                 R    | j                  |      \  }}| j                  ||      }|S rb   )r   r   )r1   r   self_attn_outputr_   r   s        r3   ra   zDinov2Attention.forward  s,    "nn];!-}=r4   )	rc   rd   re   r   r    r"   rg   ra   ri   rj   s   @r3   r   r      s*    /| /
U\\ ell r4   r   c                   X     e Zd Zd fdZdej
                  dej
                  fdZ xZS )Dinov2LayerScaler   c                     t         |           t        j                  |j                  t        j                  |j                        z        | _        y rb   )	r   r    r   r!   layerscale_valuer"   onesr$   lambda1r   s     r3   r    zDinov2LayerScale.__init__  s8    ||F$;$;ejjI[I[>\$\]r4   hidden_statec                      || j                   z  S rb   )r   r1   r   s     r3   ra   zDinov2LayerScale.forward  s    dll**r4   r   Nrc   rd   re   r    r"   rg   ra   ri   rj   s   @r3   r   r     s$    ^+ELL +U\\ +r4   r   input	drop_probr   r   c                    |dk(  s|s| S d|z
  }| j                   d   fd| j                  dz
  z  z   }|t        j                  || j                  | j
                        z   }|j                          | j                  |      |z  }|S )zc
    Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).

    r   r   r   )r   )r@   device)rC   ndimr"   randr@   r   floor_div)r   r   r   	keep_probrC   random_tensorr   s          r3   	drop_pathr     s    
 CxII[[^

Q 77E

5ELL YYMYYy!M1FMr4   c                   x     e Zd ZdZd	dedz  ddf fdZdej                  dej                  fdZde	fdZ
 xZS )
Dinov2DropPathzXDrop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).Nr   r   c                 0    t         |           || _        y rb   )r   r    r   )r1   r   r2   s     r3   r    zDinov2DropPath.__init__$  s    "r4   r   c                 D    t        || j                  | j                        S rb   )r   r   r   )r1   r   s     r3   ra   zDinov2DropPath.forward(  s    FFr4   c                      d| j                    S )Nzp=)r   r1   s    r3   
extra_reprzDinov2DropPath.extra_repr+  s    DNN#$$r4   rb   )rc   rd   re   rf   floatr    r"   rg   ra   strr   ri   rj   s   @r3   r   r   !  sG    b#%$, #$ #GU\\ Gell G%C %r4   r   c                   X     e Zd Zd fdZdej
                  dej
                  fdZ xZS )	Dinov2MLPr   c                 ~   t         |           |j                  x}}t        |j                  |j                  z        }t        j                  ||d      | _        t        |j                  t              rt        |j                     | _        n|j                  | _        t        j                  ||d      | _        y )NTr   )r   r    r$   rh   	mlp_ratior   r   fc1rq   
hidden_actr   r   
activationfc2r1   r   in_featuresout_featureshidden_featuresr2   s        r3   r    zDinov2MLP.__init__0  s    %+%7%77lf0063C3CCD99[/Ef''-$V%6%67DO$//DO99_lFr4   r   c                 l    | j                  |      }| j                  |      }| j                  |      }|S rb   )r   r   r   r   s     r3   ra   zDinov2MLP.forward;  s2    xx-|4xx-r4   r   r   rj   s   @r3   r   r   /  s$    	GELL U\\ r4   r   c                   X     e Zd Zd fdZdej
                  dej
                  fdZ xZS )Dinov2SwiGLUFFNr   c                 0   t         |           |j                  x}}t        |j                  |j                  z        }t        |dz  dz        dz   dz  dz  }t        j                  |d|z  d      | _        t        j                  ||d      | _        y )Nr:   r         Tr   )	r   r    r$   rh   r   r   r   
weights_inweights_outr   s        r3   r    zDinov2SwiGLUFFN.__init__C  s    %+%7%77lf0063C3CCD2Q67!;AAE))K_1D4P99_lNr4   r   c                     | j                  |      }|j                  dd      \  }}t        j                  j	                  |      |z  }| j                  |      S )Nr:   r9   rA   )r   chunkr   rH   silur   )r1   r   x1x2hiddens        r3   ra   zDinov2SwiGLUFFN.forwardL  sS    |4##A2#.B##B'",''r4   r   r   rj   s   @r3   r   r   B  s$    O(ELL (U\\ (r4   r   c                   d     e Zd ZdZdeddf fdZdej                  dej                  fdZ xZ	S )Dinov2LayerzCThis corresponds to the Block class in the original implementation.r   r   Nc                    t         |           t        j                  |j                  |j
                        | _        t        |      | _        t        |      | _
        |j                  dkD  rt        |j                        nt        j                         | _        t        j                  |j                  |j
                        | _        |j                   rt#        |      | _        nt'        |      | _        t        |      | _        y )Nepsr   )r   r    r   	LayerNormr$   layer_norm_epsnorm1r   r   r   layer_scale1drop_path_rater   Identityr   norm2use_swiglu_ffnr   mlpr   layer_scale2r   s     r3   r    zDinov2Layer.__init__V  s    \\&"4"4&:O:OP
(0,V4BHBWBWZ]B](=(=>cecncncp\\&"4"4&:O:OP
  &v.DH (DH,V4r4   r   c                 "   | j                  |      }| j                  |      }| j                  |      }| j                  |      |z   }| j	                  |      }| j                  |      }| j                  |      }| j                  |      |z   }|S rb   )r   r   r   r   r   r   r   )r1   r   hidden_states_normself_attention_outputlayer_outputs        r3   ra   zDinov2Layer.forwardf  s     "ZZ6 $/A B $ 1 12G H '<=M zz-0xx-((6 ~~l3mCr4   r   rj   s   @r3   r   r   S  s8    M5| 5 5 || 
r4   r   c                   N     e Zd Zdef fdZddej                  dedefdZ	 xZ
S )Dinov2Encoderr   c                     t         |           || _        t        j                  t        |j                        D cg c]  }t        |       c}      | _        d| _	        y c c}w )NF)
r   r    r   r   
ModuleListrangenum_hidden_layersr   layergradient_checkpointingr1   r   r_   r2   s      r3   r    zDinov2Encoder.__init__}  sN    ]]vG_G_A`#aAK$7#ab
&+# $bs   A#r   output_hidden_statesr   c                     |r|gnd }t        | j                        D ]!  \  }} ||      }|s|j                  |       # t        ||rt	        |            S d       S )N)last_hidden_stater   )	enumerater  appendr   r   )r1   r   r  all_hidden_statesilayer_modules         r3   ra   zDinov2Encoder.forward  so    /C]O(4 	8OA|(7M !((7	8
 +6G% 12
 	
MQ
 	
r4   )F)rc   rd   re   r   r    r"   rg   boolr   ra   ri   rj   s   @r3   r  r  |  s.    ,| ,

U\\ 

 

Zi 

r4   r  c                       e Zd ZU eed<   dZdZdZdZdgZ	dZ
dZdZdZdeiZ ej"                         dej&                  ej(                  z  ej*                  z  d	d
fd       Zy
)Dinov2PreTrainedModelr   dinov2rV   )imageTr   
attentionsr{   r   Nc                    t        |t        j                  t        j                  f      rct	        j
                  |j                  d| j                  j                         |j                   t	        j                  |j                         yyt        |t        j                        r?t	        j                  |j                         t	        j                  |j                         yt        |t              rt	        j
                  |j                  d| j                  j                         t	        j
                  |j                  d| j                  j                         | j                  j                   r t	        j                  |j"                         yyt        |t$              r5t	        j&                  |j(                  | j                  j*                         yy)zInitialize the weightsr   )meanstdN)rq   r   r   ru   inittrunc_normal_rZ   r   initializer_ranger   zeros_r   ones_r   r,   r%   r&   r(   r   	constant_r   r   )r1   r{   s     r3   _init_weightsz#Dinov2PreTrainedModel._init_weights  s'    fryy"))45v}}3DKK<Y<YZ{{&FKK( '-KK$JJv}}% 01v99IfIfgv//ct{{?\?\]{{))F--. * 01NN6>>4;;+G+GH 2r4   )rc   rd   re   r   __annotations__base_model_prefixmain_input_nameinput_modalitiessupports_gradient_checkpointing_no_split_modules_supports_sdpa_supports_flash_attn_supports_flex_attn_supports_attention_backendr   _can_record_outputsr"   no_gradr   r   ru   r   r%   r4   r3   r  r    s     $O!&*#&N"&) U]]_IBII		$9BLL$H IT I Ir4   r  c                        e Zd Zdef fdZdefdZe ed      e		 	 	 dde
j                  dz  d	e
j                  dz  d
edz  defd                     Z xZS )Dinov2Modelr   c                     t         |   |       || _        t        |      | _        t        |      | _        t        j                  |j                  |j                        | _        | j                          y )Nr   )r   r    r   r   r5   r  encoderr   r   r$   r   	layernorm	post_initr   s     r3   r    zDinov2Model.__init__  sY     *62$V,f&8&8f>S>ST 	r4   r   c                 .    | j                   j                  S rb   r5   r*   r   s    r3   get_input_embeddingsz Dinov2Model.get_input_embeddings      ///r4   F)tie_last_hidden_statesNrV   rW   r  c                    || j                   j                  }|t        d      | j                  ||      }| j	                  ||      }|j
                  }| j                  |      }|dddddf   }t        |||j                        S )z
        bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, sequence_length)`):
            Boolean masked positions. Indicates which patches are masked (1) and which aren't (0). Only relevant for
            pre-training.
        Nz You have to specify pixel_values)rW   r  r   )r  pooler_outputr   )	r   r  rx   r5   r6  r  r7  r   r   )	r1   rV   rW   r  r   embedding_outputencoder_outputssequence_outputpooled_outputs	            r3   ra   zDinov2Model.forward  s      '#';;#C#C ?@@??<?Y+/<<8H_s<+t);;..9'1a0)-')77
 	
r4   )NNN)rc   rd   re   r   r    r)   r;  r   r   r   r"   rg   r  r   ra   ri   rj   s   @r3   r4  r4    s    
| 
0&; 0  E2 -1/3,0	
llT)
 ,
 #Tk	
 
$
  3  
r4   r4  z
    Dinov2 Model transformer with an image classification head on top (a linear layer on top of the final hidden state
    of the [CLS] token) e.g. for ImageNet.
    )custom_introc                        e Zd Zdeddf fdZee	 	 d	dej                  dz  dej                  dz  de	e
   defd              Z xZS )
Dinov2ForImageClassificationr   r   Nc                 0   t         |   |       |j                  | _        t        |      | _        |j                  dkD  r-t        j                  |j                  dz  |j                        nt        j                         | _	        | j                          y )Nr   r:   )r   r    
num_labelsr4  r  r   r   r$   r   
classifierr8  r   s     r3   r    z%Dinov2ForImageClassification.__init__  sy      ++!&) EKDUDUXYDYBIIf((1,f.?.?@_a_j_j_l 	
 	r4   rV   labelsr   c                 h    | j                   |fi |}|j                  }|dddf   }|ddddf   }t        j                  ||j	                  d      gd      }| j                  |      }	d}
| | j                  ||	| j                  fi |}
t        |
|	|j                  |j                        S )a  
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        Nr   r   rA   )losslogitsr   r  )r  r  r"   rM   r  rJ  loss_functionr   r   r   r  )r1   rV   rK  r   outputsrC  r%   patch_tokenslinear_inputrN  rM  s              r3   ra   z$Dinov2ForImageClassification.forward  s     /:dkk,.Q&.Q!33#AqD)	&q!"u-yy)\->->1->-E!FAN.%4%%ffdkkLVLD$!//))	
 	
r4   )NN)rc   rd   re   r   r    r   r   r"   rg   r   r   r   ra   ri   rj   s   @r3   rG  rG    sx    |    -1&*
llT)
 t#
 +,	

 

  
r4   rG  zO
    Dinov2 backbone, to be used with frameworks like DETR and MaskFormer.
    c            
       z     e Zd Z fdZdefdZeee	 dde	j                  dedz  defd                     Z xZS )	Dinov2Backbonec                 X   t         |   |       t        |j                  dz         D cg c]  }|j                   c}| _        t        |      | _        t        |      | _	        t        j                  |j                  |j                        | _        | j                          y c c}w )Nr   r   )r   r    r	  r
  r$   num_featuresr   r5   r  r6  r   r   r   r7  r8  r  s      r3   r    zDinov2Backbone.__init__%  s     9>v?W?WZ[?[9\]AV//]*62$V,f&8&8f>S>ST 	 ^s   B'r   c                 .    | j                   j                  S rb   r:  r   s    r3   r;  z#Dinov2Backbone.get_input_embeddings1  r<  r4   NrV   r  c                    || j                   j                  }| j                  |      }| j                  |d      }|j                  }g }t        | j                  |      D ]  \  }}	|| j                  v s| j                   j                  r| j                  |	      }	| j                   j                  rn|	ddddf   }	|j                  \  }
}}}| j                   j                  }|	j                  |
||z  ||z  d      }	|	j                  dddd      j                         }	|j!                  |	        t#        t%        |      |r|	      S d	      S )
av  
        Examples:

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

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

        >>> processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base")
        >>> model = AutoBackbone.from_pretrained(
        ...     "facebook/dinov2-base", out_features=["stage2", "stage5", "stage8", "stage11"]
        ... )

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

        >>> outputs = model(**inputs)
        >>> feature_maps = outputs.feature_maps
        >>> list(feature_maps[-1].shape)
        [1, 768, 16, 16]
        ```NTr?  r   r9   r   r   r:   )feature_mapsr   )r   r  r5   r6  r   zipstage_namesr   apply_layernormr7  reshape_hidden_statesrC   r0   rF   rG   r   r  r
   r   )r1   rV   r  r   rA  r   r   rY  stager   r^   r_   r6   r7   r0   s                  r3   ra   zDinov2Backbone.forward4  sT   @  '#';;#C#C ??<8"&,,/?VZ,"[,,#&t'7'7#G 	2E<)));;..#'>>,#?L;;44#/12#6L 4@3E3E0J65!%!7!7J#/#7#7
FjDXZ_cmZmoq#rL#/#7#71a#C#N#N#PL##L1	2 |,+?-
 	
EI
 	
r4   rb   )rc   rd   re   r    r)   r;  r   r   r   r"   rg   r  r
   ra   ri   rj   s   @r3   rT  rT    s\    
0&; 0  NR6
!LL6
@Dt6
	6
    6
r4   rT  )rG  r4  r  rT  )Nr   )r   F)>rf   collections.abcrr   r   r"   r    r   r  activationsr   backbone_utilsr   modeling_layersr	   modeling_outputsr
   r   r   r   modeling_utilsr   r   processing_utilsr   utilsr   r   r   r   utils.genericr   r   utils.output_capturingr   configuration_dinov2r   
get_loggerrc   loggerModuler   r)   rg   r   r   r   r   r   r   r  r   r   r   r   r   r  r  r4  rG  rT  __all__r2  r4   r3   <module>ro     sC     $   & ! + 9 r r F & K K I 5 . 
		H	%Nryy NbBII P !%II%<<% 
% <<	%
 LL4'% T\% % '(%:/.")) /.fryy $	bii 	+ryy +U\\ e T V[VbVb  %RYY %		 &(bii ("&, &R
BII 
( IO I ID 0
' 0
 0
f /
#8 /
/
d 
I
]$9 I

I
X er4   