
    rhJy                         d Z ddlZddlZddlmZ ddlmZ ddl	Z	ddl
mZ ddlmZ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jL                        Z' G d dejL                        Z( G d dejL                        Z) G d dejL                        Z* G d dejL                        Z+ejX                  fdZ- G d dejL                        Z. G d dejL                        Z/ G d dejL                        Z0 G d  d!ejL                        Z1 G d" d#ejL                        Z2 G d$ d%ejL                        Z3 G d& d'ejL                        Z4 G d( d)e      Z5 G d* d+ejL                        Z6 e d,e$       G d- d.e5             Z7d/Z8 ee7e8        ee7ee#0        G d1 d2ejL                        Z9 e d3e$       G d4 d5e5             Z:d6Z; ee:e;        ee:ee#0       g d7Z<y)8zFlax DINOv2 model.    N)Optional)
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       /var/www/html/ai-insurance-compliance-backend/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*   
ValueErrorr2   jnpreshape)r3   pixel_valuesr*   
embeddings
batch_size_channelss          r4   __call__z"FlaxDinov2PatchEmbeddings.__call__t   sl    #))"-4,,,w  __\2
%/%5%5"
Aq({{:
B'ABBr6   N
__name__
__module____qualname__r   __annotations__r;   float32r   r5   rB    r6   r4   r   r   [   s%    {{E399"
*Cr6   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                    | j                  dt        j                  j                  j	                  | j
                  j                  dz  dd      dd| j
                  j                  f      | _        | j
                  j                  rn| 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+   r/   r0   r   r1   r-   rM   use_mask_tokenrN   r   r   patch_embeddingsr)   rP   Dropouthidden_dropout_probdropout)r3   r)   s     r4   r5   zFlaxDinov2Embeddings.setup   sY   FF001N1NPQ1QS[]op4;;**+

 ;;%%"jj##44T[[5R5RTU5UW_astDKK++,DO
 !:$++TZZ X++77#'::!FF001N1NPQ1QS[]opa!8!89$
 
 zzt{{'F'FGr6   c           	      6   |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                  |	|j                   d   ddf      }t        j                  ||j                   d   ddf      }t        j                   ||fd      S )Nr   r   r8   g?)r   r
   r   r   rO           )r   r
   bicubicF)r9   spatial_dimsscaletranslationmethod	antialiasr   r   r
   r   axis)r9   r$   r<   intmathsqrtr;   	transposer   rH   arrayr.   imagescale_and_translateastypetileconcatenate)r3   r   hidden_statesheightwidthrP   r)   num_positionsclass_pos_embedpatch_pos_embeddimhwtarget_dtypenew_height_rationew_width_ratior]   r^   patch_pos_embed_expandedclass_pos_embed_expandeds                       r4   interpolate_pos_encodingz-FlaxDinov2Embeddings.interpolate_pos_encoding   sc   #))!,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NPcPiPijkPlnpruOvw#&88Om>Q>QRS>TVWYZ=[#\ #&88Om>Q>QRS>TVWYZ=[#\  8:RSZ[\\r6   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)r9   rU   r2   r   rk   r;   broadcast_torM   r   r-   rm   r|   rP   rX   )	r3   r=   r   r?   rw   ro   rp   r>   
cls_tokenss	            r4   rB   zFlaxDinov2Embeddings.__call__   s    !''*
,,77==$**1-|/A/A!/D**<+>+>|+LM
%%dnnz1dkkF]F]6^_
__j*%=AF
$"?"?KKVUD4L4L#
 

 \\*M\J
r6   NT)rD   rE   rF   __doc__r   rG   r;   rH   r   r5   r|   rB   rI   r6   r4   rK   rK      s/    A{{E399"H*&]Pr6   rK   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.   r/   r0   r1   qkv_biasquerykeyvaluer3   s    r4   r5   zFlaxDinov2SelfAttention.setup   sp   ;;""T[[%D%DDI5 
 XXKK##**++<<--q0xN` =  [[))

 88KK##**++<<--q0xN` =  [[))
 XXKK##**++<<--q0xN` =  [[))

r6   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)r8   )r   r-   r   r   r<   r9   r   r   attention_probs_dropout_probmake_rngr   r   r;   einsum)r3   rn   r   r   head_dimquery_statesvalue_states
key_statesr   attn_weightsattn_outputoutputss               r4   rB   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r6   NTFrD   rE   rF   r   rG   r;   rH   r   r5   boolrB   rI   r6   r4   r   r      s4    {{E399"
@ T  UY  r6   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   rQ   )r+   r   r   r-   r.   r/   r0   r1   r   denserV   rW   rX   r   s    r4   r5   zFlaxDinov2SelfOutput.setup!  ss    XXKK##++<<--q0(<N **

 zzt{{'F'FGr6   r   c                 N    | j                  |      }| j                  ||      }|S )Nr~   )r   rX   )r3   rn   input_tensorr   s       r4   rB   zFlaxDinov2SelfOutput.__call__+  s(    

=1]-Pr6   Nr   r   rI   r6   r4   r   r     s,    {{E399"H4 r6   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 NrO   )r   r   r   	attentionr   outputr   s    r4   r5   zFlaxDinov2Attention.setup6  s.    0DJJO*4;;djjIr6   r   c                 |    | j                  |||      }|d   }| j                  |||      }|f}|r	||d   fz  }|S )Nr   r   r   r~   r   )r   r   )r3   rn   r   r   attn_outputsr   r   s          r4   rB   zFlaxDinov2Attention.__call__:  sU    ~~m=du~v"1oKm\ "Q))Gr6   Nr   r   rI   r6   r4   r   r   2  s,    {{E399"J
T 
r6   r   c                 4    t        j                  ||      |z  S N)r;   ones)r   r9   r]   r   s       r4   ones_with_scaler   G  s    88E5!E))r6   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_valuerS   r.   r+   r/   r   r-   r   r   s    r4   r5   zFlaxDinov2LayerScale.setupO  se    {{33djjFF$$[[$$&7
 

 ||dkk&B&BBr6   c                      | j                   |z  S r   )r   r3   rn   s     r4   rB   zFlaxDinov2LayerScale.__call__W  s    ||m++r6   NrC   rI   r6   r4   r   r   K  s%    {{E399"C,r6   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).rR   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)r9   r   )
rR   r9   ndimr   r.   randomuniformr   r;   floor)	r3   inputsr   	keep_probr9   rngrandom_tensorbinary_tensorr   s	            r4   rB   zFlaxDinov2DropPath.__call__a  s    99M$))O	M\\!_&q)AAE--
+C%

(:(:3eSYS_S_(:(``MIIm4Mi'-7FMr6   Nr   )rD   rE   rF   r   floatrG   r+   modulecompactr   r   rB   rI   r6   r4   r   r   \  s1    b
KYYhtn  r6   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.   r/   r0   r1   r   fc1fc2r%   
hidden_actstrr   actr   s    r4   r5   zFlaxDinov2MLP.setupu  s    88KK##dkk&;&;;++<<--q0(<N **
 88KK##++<<--q0(<N **
 dkk,,c2dkk445DH{{--DHr6   c                 l    | j                  |      }| j                  |      }| j                  |      }|S r   )r   r   r   r   s     r4   rB   zFlaxDinov2MLP.__call__  s2    ///r6   NrC   rI   r6   r4   r   r   q  s$    {{E399".(r6   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                 n   t        | j                  j                  | j                  j                  z        }t        |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   r+   r   r.   r/   r0   r1   r   
weights_inweights_out)r3   hidden_featuress     r4   r5   zFlaxDinov2SwiGLUFFN.setup  s    dkk558M8MMN2Q67!;AAE((++<<--q0(<N **
 88KK##++<<--q0(<N **
r6   c                     | j                  |      }t        j                  |dd      \  }}t        j                  |      |z  }| j                  |      S )Nr   r8   rb   )r   r;   splitr+   silur   )r3   rn   x1x2hiddens        r4   rB   zFlaxDinov2SwiGLUFFN.__call__  sI    6=!"5Br!''r6   NrC   rI   r6   r4   r   r     s$    {{E399"
&(r6   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   rO   )r+   	LayerNormr   layer_norm_epsr   norm1r   r   r   layer_scale1r   drop_path_rate	drop_pathnorm2use_swiglu_ffnr   mlpr   layer_scale2r   s    r4   r5   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r6   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   )r3   rn   r   r   self_attention_outputsattention_outputr   layer_outputs           r4   rB   zFlaxDinov2Layer.__call__  s    !%JJ}%'/ "0 "
 2!4,,-=>(, '78=H zz-0xx-((6 ~~l3mC/G+r6   Nr   r   rI   r6   r4   r   r     s4    {{E399"PT UY r6   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)r3   is     r4   r5   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 )NrI   r   r   r   c              3   &   K   | ]	  }||  y wr   rI   ).0vs     r4   	<genexpr>z5FlaxDinov2LayerCollection.__call__.<locals>.<genexpr>  s     =qq}=s   )last_hidden_statern   
attentions)	enumerater   tupler   )r3   rn   r   r   r  r  all_attentionsall_hidden_statesr  layerlayer_outputsr   s               r4   rB   z"FlaxDinov2LayerCollection.__call__  s      1d"6BD!$++. 		6HAu#!m%55!!-}`qrM)!,M =#3"55		6  -!11 "=G==="+;LYg
 	
r6   NTFFTr   rI   r6   r4   r   r     sZ    {{E399"
 #"'%* 
 
  	

 #
 
r6   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    r4   r5   zFlaxDinov2Encoder.setup  s    .t{{$**M
r6   r   r   r  r  c                 .    | j                  |||||      S )Nr   r   r  r  )r  )r3   rn   r   r   r  r  s         r4   rB   zFlaxDinov2Encoder.__call__  s)     zz'/!5#  
 	
r6   Nr  r   rI   r6   r4   r  r  
  s[    {{E399"N #"'%* 
 
  	

 #
 
r6   r  c                   z    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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  rI   )r  r#   r*   super__init__)	r3   r   r  r  r   r  kwargsr   	__class__s	           r4   r!  z"FlaxDinov2PreTrainedModel.__init__-  sc     #""H&HHf//1B1BFDWDWXK[tSXcklr6   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 )NrO   )r$  rX   r   F)r  r$  )r;   zerosr   r.   r   r   r   initr   r   _missing_keyssetr   r	   )r3   r   r  r$  r=   
params_rngr   droppath_rngrngsrandom_paramsmissing_keys              r4   init_weightsz&FlaxDinov2PreTrainedModel.init_weights;  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  r6   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$  rO   )r-  )r   r   r  r  r;   rg   r.   r   r   r   applyr$  rh   rH   )
r3   r=   r$  r   r1  r   r  r  r-  r,  s
             r4   rB   z"FlaxDinov2PreTrainedModel.__call__O  s     2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++BYBY}}\<@"(+

(8(8(E%K)DO+D{{  v,-IIl#++6I  ! 
 	
r6   r   )NNFNNN)rD   rE   rF   r   r   config_classbase_model_prefixmain_input_namer  r+   ModulerG   r;   rH   rd   r   r   r!  r.   r   PRNGKeyr  r   r0  r   DINOV2_INPUTS_DOCSTRINGformatr   dictrB   __classcell__)r#  s   @r4   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 
r6   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 )NrO   r   )
rK   r   r   r>   r  encoderr+   r   r   	layernormr   s    r4   r5   zFlaxDinov2Module.setupw  sQ    .t{{$**M(DJJGdkk.H.HPTPZPZ[r6   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@  rA  r   rn   r
  )r3   r=   r   r   r  r  rn   encoder_outputssequence_outputpooled_outputhead_outputss              r4   rB   zFlaxDinov2Module.__call__|  s     MR,,'/!5# ' 
 *!,..9'1a0+];L/!""555--')77&11	
 	
r6   Nr  r   rI   r6   r4   r>  r>  s  s[    {{E399"\ #"'%* 
 
  	

 #
 
r6   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)rD   rE   rF   r>  r  rI   r6   r4   rI  rI    s	    
 $Lr6   rI  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_typer4  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.   r/   r0   r1   
classifierr   s    r4   r5   z,FlaxDinov2ForImageClassificationModule.setup  sk    &dkkL((KK""**++<<--q0(<N
r6   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   r8   r   )logitsrn   r
  )
r   use_return_dictr  r;   rm   meanrO  r   rn   r
  )r3   r=   r   r   r  r  r   rn   rM   patch_tokenslinear_inputrQ  r   s                r4   rB   z/FlaxDinov2ForImageClassificationModule.__call__  s     &1%<k$++B]B]++'/!5#  
  
!!Q$'	$QU+	<3D3D!3D3L'MTVW.Y,FM+!//))
 	
r6   )NTNNNr   rI   r6   r4   rL  rL    s?    {{E399"
 "!"
 "
r6   rL  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)rD   rE   rF   rL  r  rI   r6   r4   rW  rW    s	     :Lr6   rW  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", from_pt=True)

    >>> 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()])
    ```
)rW  rI  r  )=r   collections.abcr&   re   typingr   
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_DOCSTRINGr9  r7  r   rK   r   r   r   rH   r   r   r   r   r   r   r   r  r  r>  rI  FLAX_VISION_MODEL_DOCSTRINGrL  rW  $FLAX_VISION_CLASSIFICATION_DOCSTRING__all__rI   r6   r4   <module>ri     s        
  > > > ; v v  Q .! F "!C		 !CHR299 Rl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
 _r6   