
    sgw                        d Z ddlZddlZddlmZmZ ddl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 ddlmZmZ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Z%dZ& G d de	jN                        Z( G d de	jN                        Z) G d de	jN                        Z* G d de	jN                        Z+ G d de	jN                        Z,ejZ                  fdZ. G d de	jN                        Z/ G d de	jN                        Z0 G d de	jN                        Z1 G d  d!e	jN                        Z2 G d" d#e	jN                        Z3 G d$ d%e	jN                        Z4 G d& d'e	jN                        Z5 G d( d)e      Z6 G d* d+e	jN                        Z7 e!d,e%       G d- d.e6             Z8d/Z9 ee8e9        ee8ee$0        G d1 d2e	jN                        Z: e!d3e%       G d4 d5e6             Z;d6Z< ee;e<        ee;ee$0       y)7zFlax DINOv2 model.    N)OptionalTuple)
FrozenDictfreezeunfreeze)dot_product_attention_weights)flatten_dictunflatten_dict   )FlaxBaseModelOutputFlaxBaseModelOutputWithPoolingFlaxSequenceClassifierOutput)ACT2FNFlaxPreTrainedModel append_replace_return_docstringsoverwrite_call_docstring)add_start_docstrings%add_start_docstrings_to_model_forward   )Dinov2Configa  

    This model inherits from [`FlaxPreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading, saving and converting weights from PyTorch models)

    This model is also a
    [flax.linen.Module](https://flax.readthedocs.io/en/latest/api_reference/flax.linen/module.html) subclass. Use it as
    a regular Flax linen Module and refer to the Flax documentation for all matter related to general usage and
    behavior.

    Finally, this model supports inherent JAX features such as:

    - [Just-In-Time (JIT) compilation](https://jax.readthedocs.io/en/latest/jax.html#just-in-time-compilation-jit)
    - [Automatic Differentiation](https://jax.readthedocs.io/en/latest/jax.html#automatic-differentiation)
    - [Vectorization](https://jax.readthedocs.io/en/latest/jax.html#vectorization-vmap)
    - [Parallelization](https://jax.readthedocs.io/en/latest/jax.html#parallelization-pmap)

    Parameters:
        config ([`Dinov2Config`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~FlaxPreTrainedModel.from_pretrained`] method to load the model weights.
        dtype (`jax.numpy.dtype`, *optional*, defaults to `jax.numpy.float32`):
            The data type of the computation. Can be one of `jax.numpy.float32`, `jax.numpy.float16` (on GPUs) and
            `jax.numpy.bfloat16` (on TPUs).

            This can be used to enable mixed-precision training or half-precision inference on GPUs or TPUs. If
            specified all the computation will be performed with the given `dtype`.

            **Note that this only specifies the dtype of the computation and does not influence the dtype of model
            parameters.**

            If you wish to change the dtype of the model parameters, see [`~FlaxPreTrainedModel.to_fp16`] and
            [`~FlaxPreTrainedModel.to_bf16`].
a
  
    Args:
        pixel_values (`numpy.ndarray` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`Dinov2ImageProcessor.__call__`]
            for details.

        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
c                   Z    e Zd ZU eed<   ej                  Zej                  ed<   d Zd Z	y)FlaxDinov2PatchEmbeddingsconfigdtypec                 r   | 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  }|| _        | j                   j                  | _        t        j                  | j                   j                  ||d| j                  t        j                  j                  j                  | j                   j                   dz  dd            | _        y )Nr   r   VALID   fan_intruncated_normal)kernel_sizestridespaddingr   kernel_init)r   
image_size
patch_size
isinstancecollectionsabcIterablenum_patchesnum_channelsnnConvhidden_sizer   jaxinitializersvariance_scalinginitializer_range
projection)selfr$   r%   r*   s       b/var/www/html/venv/lib/python3.12/site-packages/transformers/models/dinov2/modeling_flax_dinov2.pysetupzFlaxDinov2PatchEmbeddings.setup_   s    [[++
[[++
#-j+//:R:R#SZZdfpYq
#-j+//:R:R#SZZdfpYq
!!}
15*Q-:VW=:XY& KK44''KK##"**++<<--q0(<N	
    c                     |j                   d   }|| j                  k7  rt        d      | j                  |      }|j                   \  }}}}t	        j
                  ||d|f      S )NzeMake sure that the channel dimension of the pixel values match with the one set in the configuration.)shaper+   
ValueErrorr3   jnpreshape)r4   pixel_valuesr+   
embeddings
batch_size_channelss          r5   __call__z"FlaxDinov2PatchEmbeddings.__call__t   sl    #))"-4,,,w  __\2
%/%5%5"
Aq({{:
B'ABBr7   N
__name__
__module____qualname__r   __annotations__r<   float32r   r6   rC    r7   r5   r   r   [   s%    {{E399"
*Cr7   r   c                   f    e Zd ZU dZeed<   ej                  Zej                  ed<   d Z	d Z
ddZy)	FlaxDinov2Embeddingsz7Construct the CLS token, position and patch embeddings.r   r   c                 x   | j                  dt        j                  j                  j	                  | j
                  j                  dz  dd      dd| j
                  j                  f      | _        | j                  dt        j                  j                  j	                  | j
                  j                  dz  dd      d| j
                  j                  f      | _	        t        | j
                  | j                        | _        | j                  j                  }| j                  dt        j                  j                  j	                  | j
                  j                  dz  dd      d|dz   | j
                  j                  f      | _        t        j                  | j
                  j                   	      | _        y )
N	cls_tokenr   r   r   r   
mask_tokenr   position_embeddingsrate)paramr/   r,   r0   r1   r   r2   r.   rN   rO   r   r   patch_embeddingsr*   rQ   Dropouthidden_dropout_probdropout)r4   r*   s     r5   r6   zFlaxDinov2Embeddings.setup   sL   FF001N1NPQ1QS[]op4;;**+

 **FF001N1NPQ1QS[]op''(

 !:$++TZZ X++77#'::!FF001N1NPQ1QS[]opa!8!89$
 
 zzt{{'F'FGr7   c           	         |j                   d   dz
  }|j                   d   dz
  }||k(  r||k(  r|S |d d df   }|d d dd f   }	|j                   d   }
||j                  z  }||j                  z  }|dz   |dz   }}|	j                  dt        t	        j
                  |            t        t	        j
                  |            |
f      }	t        j                  |	d      }	|	j                  }t        j                  |t	        j
                  |      z        }t        j                  |t	        j
                  |      z        }t        j                  ||gt        j                        }t        j                  ddgt        j                        }t        j                  j                  |	j                  t        j                        |	j                   d   |	j                   d   ||fd||d	d
      }	|	j                  |      }	t        j                  |	d      j                  |j                   d   d|
f      }	t        j                  |t        j                   d d f   |	fd      S )Nr   r   r9   g?)r   r   r   r   rP           )r   r   bicubicF)r:   spatial_dimsscaletranslationmethod	antialiasr   r   r   r   axis)r:   r%   r=   intmathsqrtr<   	transposer   rI   arrayr/   imagescale_and_translateastypeconcatenatenewaxis)r4   r   hidden_statesheightwidthrQ   r*   num_positionsclass_pos_embedpatch_pos_embeddimhwtarget_dtypenew_height_rationew_width_ratior]   r^   s                     r5   interpolate_pos_encodingz-FlaxDinov2Embeddings.interpolate_pos_encoding   s"   #))!,q0+11!4q8-'FeO&&-ad3-ae4!!"%f'''V&&&CS)11DIIm,-s499]3K/LcR
 --F&,,;;v		-0H'HI++edii.F&FG		+_=S[[Qiic
#++>))77""3;;/"((+_-B-B1-Eq!L# 8 
 *00>--FNNP]PcPcdePfhjloOpqQ ?QXYZZr7   c                    |j                   d   }| j                  j                  j                  }|j                   d   |j                   d   }}| j                  |j	                  |            }t        j                  | j                  |d| j                  j                  f      }t        j                  ||fd      }|| j                  | j                  |||| j                        z   }| j                  ||      }|S )Nr   r   r   rb   deterministic)r:   rU   r3   r   rk   r<   broadcast_torN   r   r.   rl   rz   rQ   rX   )	r4   r>   r}   r@   rw   ro   rp   r?   
cls_tokenss	            r5   rC   zFlaxDinov2Embeddings.__call__   s    !''*
,,77==$**1-|/A/A!/D**<+>+>|+LM
%%dnnz1dkkF]F]6^_
__j*%=AF
$"?"?KKVUD4L4L#
 

 \\*M\J
r7   NT)rE   rF   rG   __doc__r   rH   r<   rI   r   r6   rz   rC   rJ   r7   r5   rL   rL      s/    A{{E399"H($[Lr7   rL   c                   f    e Zd ZU eed<   ej                  Zej                  ed<   d Zdde	de	fdZ
y)	FlaxDinov2SelfAttentionr   r   c           	         | j                   j                  | j                   j                  z  dk7  rt        d      t	        j
                  | j                   j                  | j                  t        j                  j                  j                  | j                   j                  dz  dd      | j                   j                        | _        t	        j
                  | j                   j                  | j                  t        j                  j                  j                  | j                   j                  dz  dd      | j                   j                        | _        t	        j
                  | j                   j                  | j                  t        j                  j                  j                  | j                   j                  dz  dd      | j                   j                        | _        y )Nr   z`config.hidden_size`: {self.config.hidden_size} has to be a multiple of `config.num_attention_heads`: {self.config.num_attention_heads}r   r   r   )modedistribution)r   r#   use_bias)r   r.   num_attention_headsr;   r,   Denser   r/   r0   r1   r2   qkv_biasquerykeyvaluer4   s    r5   r6   zFlaxDinov2SelfAttention.setup   sp   ;;""T[[%D%DDI5 
 XXKK##**++<<--q0xN` =  [[))

 88KK##**++<<--q0xN` =  [[))
 XXKK##**++<<--q0xN` =  [[))

r7   r}   output_attentionsc           
      H   | j                   j                  | j                   j                  z  }| j                  |      j	                  |j
                  d d | j                   j                  |fz         }| j                  |      j	                  |j
                  d d | j                   j                  |fz         }| j                  |      j	                  |j
                  d d | j                   j                  |fz         }d }|s*| j                   j                  dkD  r| j                  d      }t        |||| j                   j                  d|| j                  d       }	t        j                  d|	|      }
|
j	                  |
j
                  d d dz         }
|r|
|	f}|S |
f}|S )Nr   rZ   rX   T)dropout_rngdropout_ratebroadcast_dropoutr}   r   	precisionz...hqk,...khd->...qhd)r9   )r   r.   r   r   r=   r:   r   r   attention_probs_dropout_probmake_rngr   r   r<   einsum)r4   rn   r}   r   head_dimquery_statesvalue_states
key_statesr   attn_weightsattn_outputoutputss               r5   rC   z FlaxDinov2SelfAttention.__call__   s   ;;**dkk.M.MMzz-088#t{{'F'F&QQ
 zz-088#t{{'F'F&QQ
 XXm,44#t{{'F'F&QQ

 !I!IC!O--	2K4#AA"'**	
 jj!8,U!))+*;*;BQ*?%*GH1B;- JUr7   NTFrE   rF   rG   r   rH   r<   rI   r   r6   boolrC   rJ   r7   r5   r   r      s4    {{E399"
@ T  UY  r7   r   c                   b    e Zd ZU eed<   ej                  Zej                  ed<   d Zdde	fdZ
y)FlaxDinov2SelfOutputr   r   c                 X   t        j                  | j                  j                  t        j                   j
                  j                  | j                  j                  dz  dd      | j                        | _	        t        j                  | j                  j                        | _        y )Nr   r   r   r#   r   rR   )r,   r   r   r.   r/   r0   r1   r2   r   denserV   rW   rX   r   s    r5   r6   zFlaxDinov2SelfOutput.setup  ss    XXKK##++<<--q0(<N **

 zzt{{'F'FGr7   r}   c                 N    | j                  |      }| j                  ||      }|S )Nr|   )r   rX   )r4   rn   input_tensorr}   s       r5   rC   zFlaxDinov2SelfOutput.__call__(  s(    

=1]-Pr7   Nr   r   rJ   r7   r5   r   r     s,    {{E399"H4 r7   r   c                   b    e Zd ZU eed<   ej                  Zej                  ed<   d Zdde	fdZ
y)FlaxDinov2Attentionr   r   c                     t        | j                  | j                        | _        t	        | j                  | j                        | _        y NrP   )r   r   r   	attentionr   outputr   s    r5   r6   zFlaxDinov2Attention.setup3  s.    0DJJO*4;;djjIr7   r   c                 |    | j                  |||      }|d   }| j                  |||      }|f}|r	||d   fz  }|S )Nr}   r   r   r|   r   )r   r   )r4   rn   r}   r   attn_outputsr   r   s          r5   rC   zFlaxDinov2Attention.__call__7  sU    ~~m=du~v"1oKm\ "Q))Gr7   Nr   r   rJ   r7   r5   r   r   /  s,    {{E399"J
T 
r7   r   c                 4    t        j                  ||      |z  S N)r<   ones)r   r:   r]   r   s       r5   ones_with_scaler   D  s    88E5!E))r7   c                   Z    e Zd ZU eed<   ej                  Zej                  ed<   d Zd Z	y)FlaxDinov2LayerScaler   r   c                     | j                   j                  | j                  dt        j                  j
                  j                  | j                   j                  f      z  | _        | j                  | j                   j                  z  | _        y )Nlambda1)	r   layerscale_valuerT   r/   r,   r0   r   r.   r   r   s    r5   r6   zFlaxDinov2LayerScale.setupL  se    {{33djjFF$$[[$$&7
 

 ||dkk&B&BBr7   c                      | j                   |z  S r   )r   r4   rn   s     r5   rC   zFlaxDinov2LayerScale.__call__T  s    ||m++r7   NrD   rJ   r7   r5   r   r   H  s%    {{E399"C,r7   r   c                   b    e Zd ZU dZeed<   ej                  j                  dde	e
   fd       Zy)FlaxDinov2DropPathzXDrop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).rS   r}   c                 N   | j                   dk(  r|S d| j                   z
  }|r|S |j                  d   fd|j                  dz
  z  z   }| j                  d      }|t        j
                  j                  |||j                        z   }t        j                  |      }||z  |z  }|S )NrZ   g      ?r   )r   r   droppath)r:   r   )
rS   r:   ndimr   r/   randomuniformr   r<   floor)	r4   inputsr}   	keep_probr:   rngrandom_tensorbinary_tensorr   s	            r5   rC   zFlaxDinov2DropPath.__call__^  s    99M$))O	M\\!_&q)AAE--
+C%

(:(:3eSYS_S_(:(``MIIm4Mi'-7FMr7   Nr   )rE   rF   rG   r   floatrH   r,   modulecompactr   r   rC   rJ   r7   r5   r   r   Y  s1    b
KYYhtn  r7   r   c                   Z    e Zd ZU eed<   ej                  Zej                  ed<   d Zd Z	y)FlaxDinov2MLPr   r   c                    t        j                  | j                  j                  | j                  j                  z  t
        j                   j                  j                  | j                  j                  dz  dd      | j                        | _
        t        j                  | j                  j                  t
        j                   j                  j                  | j                  j                  dz  dd      | j                        | _        t        | j                  j                  t              r#t        | j                  j                     | _        y | j                  j                  | _        y )Nr   r   r   r   )r,   r   r   r.   	mlp_ratior/   r0   r1   r2   r   fc1fc2r&   
hidden_actstrr   actr   s    r5   r6   zFlaxDinov2MLP.setupr  s    88KK##dkk&;&;;++<<--q0(<N **
 88KK##++<<--q0(<N **
 dkk,,c2dkk445DH{{--DHr7   c                 l    | j                  |      }| j                  |      }| j                  |      }|S r   )r   r   r   r   s     r5   rC   zFlaxDinov2MLP.__call__  s2    ///r7   NrD   rJ   r7   r5   r   r   n  s$    {{E399".(r7   r   c                   Z    e Zd ZU eed<   ej                  Zej                  ed<   d Zd Z	y)FlaxDinov2SwiGLUFFNr   r   c                    t        | j                  j                  | j                  j                  z        }t        | j                  dz  dz        dz   dz  dz  }t        j                  d|z  t        j
                  j                  j                  | j                  j                  dz  dd      | j                        | _        t        j                  | j                  j                  t        j
                  j                  j                  | j                  j                  dz  dd      | j                        | _        y )Nr   r         r   r   r   )rd   r   r.   r   hidden_featuresr,   r   r/   r0   r1   r2   r   
weights_inweights_out)r4   r   s     r5   r6   zFlaxDinov2SwiGLUFFN.setup  s    dkk558M8MMNt33a7!;<q@QFJ((++<<--q0(<N **
 88KK##++<<--q0(<N **
r7   c                     | j                  |      }t        j                  |dd      \  }}t        j                  |      |z  }| j                  |      S )Nr   r9   rb   )r   r<   splitr,   silur   )r4   rn   x1x2hiddens        r5   rC   zFlaxDinov2SwiGLUFFN.__call__  sI    6=!"5Br!''r7   NrD   rJ   r7   r5   r   r     s$    {{E399"
&(r7   r   c                   f    e Zd ZU eed<   ej                  Zej                  ed<   d Zdde	de	fdZ
y)	FlaxDinov2Layerr   r   c                    t        j                  | j                  j                  | j                        | _        t        | j                  | j                        | _        t        | j                  | j                        | _	        t        | j                  j                        | _        t        j                  | j                  j                  | j                        | _        | j                  j                  r't        | j                  | j                        | _        n&t#        | j                  | j                        | _        t        | j                  | j                        | _        y )Nepsilonr   rP   )r,   	LayerNormr   layer_norm_epsr   norm1r   r   r   layer_scale1r   drop_path_rate	drop_pathnorm2use_swiglu_ffnr   mlpr   layer_scale2r   s    r5   r6   zFlaxDinov2Layer.setup  s    \\$++*D*DDJJW
,T[[

K0DJJO+DKK,F,FG\\$++*D*DDJJW
;;%%*4;;djjIDH$T[[

CDH0DJJOr7   r}   r   c                 D   | j                  | j                  |      ||      }|d   }| j                  |      }|dd  }| j                  |      |z   }| j	                  |      }| j                  |      }| j                  |      }| j                  |      |z   }|f|z   }|S )Nr   r   r   )r   r   r   r   r   r   r   )r4   rn   r}   r   self_attention_outputsattention_outputr   layer_outputs           r5   rC   zFlaxDinov2Layer.__call__  s    !%JJ}%'/ "0 "
 2!4,,-=>(, '78=H zz-0xx-((6 ~~l3mC/G+r7   Nr   r   rJ   r7   r5   r   r     s4    {{E399"PT UY r7   r   c            	       v    e Zd ZU eed<   ej                  Zej                  ed<   d Z	 	 	 	 d
de	de	de	de	fdZ
y	)FlaxDinov2LayerCollectionr   r   c           	          t        | j                  j                        D cg c]-  }t        | j                  t	        |      | j
                        / c}| _        y c c}w )N)namer   )ranger   num_hidden_layersr   r   r   layers)r4   is     r5   r6   zFlaxDinov2LayerCollection.setup  sE    QVW[WbWbWtWtQu
LMODKKc!fDJJG
 
s   2Ar}   r   output_hidden_statesreturn_dictc                     |rdnd }|rdnd }t        | j                        D ])  \  }}	|r||fz  } |	|||      }
|
d   }|s!||
d   fz  }+ |r||fz  }|f}|st        d |D              S t        |||      S )NrJ   r   r   r   c              3   &   K   | ]	  }||  y wr   rJ   ).0vs     r5   	<genexpr>z5FlaxDinov2LayerCollection.__call__.<locals>.<genexpr>  s     =qq}=s   )last_hidden_statern   
attentions)	enumerater   tupler   )r4   rn   r}   r   r   r  all_attentionsall_hidden_statesr   layerlayer_outputsr   s               r5   rC   z"FlaxDinov2LayerCollection.__call__  s      1d"6BD!$++. 		6HAu#!m%55!!-}`qrM)!,M =#3"55		6  -!11 "=G==="+;LYg
 	
r7   NTFFTr   rJ   r7   r5   r   r     sZ    {{E399"
 #"'%* 
 
  	

 #
 
r7   r   c            	       v    e Zd ZU eed<   ej                  Zej                  ed<   d Z	 	 	 	 d
de	de	de	de	fdZ
y	)FlaxDinov2Encoderr   r   c                 P    t        | j                  | j                        | _        y r   )r   r   r   r  r   s    r5   r6   zFlaxDinov2Encoder.setup  s    .t{{$**M
r7   r}   r   r   r  c                 .    | j                  |||||      S )Nr}   r   r   r  )r  )r4   rn   r}   r   r   r  s         r5   rC   zFlaxDinov2Encoder.__call__  s)     zz'/!5#  
 	
r7   Nr  r   rJ   r7   r5   r  r    s[    {{E399"N #"'%* 
 
  	

 #
 
r7   r  c                   t    e Zd ZU dZeZdZdZdZe	j                  ed<   ddej                  dfded	ed
ej                  def fdZddej&                  j(                  dededefdZ eej5                  d            	 	 	 	 	 	 ddedej&                  j(                  dedee   dee   dee   fd       Z xZS )FlaxDinov2PreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    dinov2r>   Nmodule_classr   Tr   seedr   _do_initc                      | j                   d||d|}|$d|j                  |j                  |j                  f}t        |   ||||||       y )Nr   r   r   )input_shaper  r   r  rJ   )r  r$   r+   super__init__)	r4   r   r  r  r   r  kwargsr   	__class__s	           r5   r  z"FlaxDinov2PreTrainedModel.__init__*  sc     #""H&HHf//1B1BFDWDWXK[tSXcklr7   r   r  paramsreturnc                    t        j                  || j                        }t        j                  j                  |      \  }}t        j                  j                  |      \  }}|||d}| j                  j                  ||d      d   }	|dt        t        |	            }	t        t        |            }| j                  D ]
  }
|	|
   ||
<    t               | _
        t        t        |            S |	S )NrP   )r"  rX   r   F)r  r"  )r<   zerosr   r/   r   r   r   initr	   r   _missing_keyssetr   r
   )r4   r   r  r"  r>   
params_rngr   droppath_rngrngsrandom_paramsmissing_keys              r5   init_weightsz&FlaxDinov2PreTrainedModel.init_weights8  s    yyDJJ?"%**"2"23"7
K$'JJ$4$4[$A!\$,W((|(OPXY(-)@AM!(6"23F#11 A&3K&@{#A!$D.011  r7   zbatch_size, sequence_lengthr   trainr   r   r  c           	         ||n| j                   j                  }||n| j                   j                  }||n| j                   j                  }t	        j
                  |d      }i }|,t        j                  j                  |      \  }}	||d<   |	|d<   | j                  j                  d|xs | j                  it	        j                  |t        j                        | ||||      S )Nra   rX   r   r"  rP   )r+  )r   r   r   r  r<   rg   r/   r   r   r   applyr"  rh   rI   )
r4   r>   r"  r   r/  r   r   r  r+  r*  s
             r5   rC   z"FlaxDinov2PreTrainedModel.__call__L  s     2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++BYBY}}\<@"(+

(8(8(E%K)DO+D{{  v,-IIl#++6I  ! 
 	
r7   r   )NNFNNN)rE   rF   rG   r   r   config_classbase_model_prefixmain_input_namer  r,   ModulerH   r<   rI   rd   r   r   r  r/   r   PRNGKeyr   r   r.  r   DINOV2_INPUTS_DOCSTRINGformatdictr   rC   __classcell__)r!  s   @r5   r  r    s(   
  L $O"L"))"
 ;;mm 	m
 yym m!

 2 2 ! !PZ !fp !( ++B+I+IJg+hi *.,0/3&* 
  
 ZZ''	 

  
 $D> 
 'tn 
 d^ 
 j 
r7   r  c            	       v    e Zd ZU eed<   ej                  Zej                  ed<   d Z	 	 	 	 d
de	de	de	de	fdZ
y	)FlaxDinov2Moduler   r   c                    t        | j                  | j                        | _        t	        | j                  | j                        | _        t        j                  | j                  j                  | j                        | _	        y )NrP   r   )
rL   r   r   r?   r  encoderr,   r   r   	layernormr   s    r5   r6   zFlaxDinov2Module.setupt  sQ    .t{{$**M(DJJGdkk.H.HPTPZPZ[r7   r}   r   r   r  c                     | j                  ||      }| j                  |||||      }|d   }| j                  |      }|d d dd d f   }	|s||	f}
|
|dd  z   S t        ||	|j                  |j
                        S )Nr|   r  r   r   )r  pooler_outputrn   r  )r?   r>  r?  r   rn   r  )r4   r>   r}   r   r   r  rn   encoder_outputssequence_outputpooled_outputhead_outputss              r5   rC   zFlaxDinov2Module.__call__y  s     MR,,'/!5# ' 
 *!,..9'1a0+];L/!""555--')77&11	
 	
r7   Nr  r   rJ   r7   r5   r<  r<  p  s[    {{E399"\ #"'%* 
 
  	

 #
 
r7   r<  z`The bare Dinov2 Model transformer outputting raw hidden-states without any specific head on top.c                       e Zd ZeZy)FlaxDinov2ModelN)rE   rF   rG   r<  r  rJ   r7   r5   rG  rG    s	    
 $Lr7   rG  ar  
    Returns:

    Examples:

    ```python
    >>> from transformers import AutoImageProcessor, FlaxDinov2Model
    >>> from PIL import Image
    >>> import requests

    >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
    >>> image = Image.open(requests.get(url, stream=True).raw)

    >>> image_processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base")
    >>> model = FlaxDinov2Model.from_pretrained("facebook/dinov2-base")

    >>> inputs = image_processor(images=image, return_tensors="np")
    >>> outputs = model(**inputs)
    >>> last_hidden_states = outputs.last_hidden_state
    ```
)output_typer2  c                   l    e Zd ZU eed<   ej                  Zej                  ed<   d Z	 	 	 	 	 dde	fdZ
y)&FlaxDinov2ForImageClassificationModuler   r   c           	      F   t        | j                  | j                        | _        t	        j
                  | j                  j                  | j                  t        j                  j                  j                  | j                  j                  dz  dd            | _        y )Nr  r   r   r   )r   r#   )r<  r   r   r  r,   r   
num_labelsr/   r0   r1   r2   
classifierr   s    r5   r6   z,FlaxDinov2ForImageClassificationModule.setup  sk    &dkkL((KK""**++<<--q0(<N
r7   Nr}   c                 f   ||n| j                   j                  }| j                  |||||      }|d   }|d d df   }|d d dd f   }	t        j                  ||	j                  d      gd      }
| j                  |
      }|s|f|dd  z   }|S t        ||j                  |j                        S )Nr  r   r   rb   r9   r   )logitsrn   r  )
r   use_return_dictr  r<   rl   meanrM  r   rn   r  )r4   r>   r}   r   r   r  r   rn   rN   patch_tokenslinear_inputrO  r   s                r5   rC   z/FlaxDinov2ForImageClassificationModule.__call__  s     &1%<k$++B]B]++'/!5#  
  
!!Q$'	$QU+	<3D3D!3D3L'MTVW.Y,FM+!//))
 	
r7   )NTNNNr   rJ   r7   r5   rJ  rJ    s?    {{E399"
 "!"
 "
r7   rJ  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.
    c                       e Zd ZeZy) FlaxDinov2ForImageClassificationN)rE   rF   rG   rJ  r  rJ   r7   r5   rU  rU    s	     :Lr7   rU  a  
    Returns:

    Example:

    ```python
    >>> from transformers import AutoImageProcessor, FlaxDinov2ForImageClassification
    >>> from PIL import Image
    >>> import jax
    >>> import requests

    >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
    >>> image = Image.open(requests.get(url, stream=True).raw)

    >>> image_processor = AutoImageProcessor.from_pretrained("facebook/dinov2-base-imagenet1k-1-layer")
    >>> model = FlaxDinov2ForImageClassification.from_pretrained("facebook/dinov2-base-imagenet1k-1-layer")

    >>> inputs = image_processor(images=image, return_tensors="np")
    >>> outputs = model(**inputs)
    >>> logits = outputs.logits

    >>> # model predicts one of the 1000 ImageNet classes
    >>> predicted_class_idx = jax.numpy.argmax(logits, axis=-1)
    >>> print("Predicted class:", model.config.id2label[predicted_class_idx.item()])
    ```
)=r   collections.abcr'   re   typingr   r   
flax.linenlinenr,   r/   	jax.numpynumpyr<   flax.core.frozen_dictr   r   r   flax.linen.attentionr   flax.traverse_utilr	   r
   modeling_flax_outputsr   r   r   modeling_flax_utilsr   r   r   r   utilsr   r   configuration_dinov2r   DINOV2_START_DOCSTRINGr7  r5  r   rL   r   r   r   rI   r   r   r   r   r   r   r   r  r  r<  rG  FLAX_VISION_MODEL_DOCSTRINGrJ  rU  $FLAX_VISION_CLASSIFICATION_DOCSTRINGrJ   r7   r5   <module>rf     s      "  
  > > > ; v v  Q .! F "!C		 !CHO299 OfDbii DP299 *")) * .1[[ *,299 ," *BII >(")) (<,bii ,`(
		 (
X
		 
0N
 3 N
b(
ryy (
V f$/ $	$ , *E F  !?l
0
RYY 0
f  :'@ ::( $6 9;_ `  $2N]ir7   