Ë
    S^(hN›  ã            	       ó„  — d Z ddlZddlmZ ddlmZmZmZ ddl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 dd
lmZ ddlmZmZ ddlmZmZmZmZmZmZmZm Z m!Z! ddl"m#Z# ddl$m%Z%  e«       r	ddl&m'Z'm(Z( nd„ Z(d„ Z' ejR                  e*«      Z+dZ,dZ-g d¢Z.dZ/dZ0e G d„ de«      «       Z1e G d„ de«      «       Z2e G d„ de«      «       Z3 G d„ de
jh                  «      Z5 G d„ d e
jh                  «      Z6 G d!„ d"e
jh                  «      Z7dGd#ejp                  d$e9d%e:d&ejp                  fd'„Z; G d(„ d)e
jh                  «      Z< G d*„ d+e
jh                  «      Z= G d,„ d-e
jh                  «      Z> G d.„ d/e
jh                  «      Z? G d0„ d1e
jh                  «      Z@ G d2„ d3e
jh                  «      ZA G d4„ d5e
jh                  «      ZB G d6„ d7e
jh                  «      ZC G d8„ d9e
jh                  «      ZD G d:„ d;e«      ZEd<ZFd=ZG ed>eF«       G d?„ d@eE«      «       ZH edAeF«       G dB„ dCeE«      «       ZI edDeF«       G dE„ dFeEe#«      «       ZJy)Hz1PyTorch Neighborhood Attention Transformer model.é    N)Ú	dataclass)ÚOptionalÚTupleÚUnion)Únn)ÚBCEWithLogitsLossÚCrossEntropyLossÚMSELossé   )ÚACT2FN)ÚBackboneOutput)ÚPreTrainedModel)Ú find_pruneable_heads_and_indicesÚprune_linear_layer)	ÚModelOutputÚOptionalDependencyNotAvailableÚadd_code_sample_docstringsÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚis_natten_availableÚloggingÚreplace_return_docstringsÚrequires_backends)ÚBackboneMixiné   )Ú	NatConfig)Ú
natten2davÚnatten2dqkrpbc                  ó   — t        «       ‚©N©r   ©ÚargsÚkwargss     úm/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/deprecated/nat/modeling_nat.pyr   r   1   ó   € Ü,Ó.Ð.ó    c                  ó   — t        «       ‚r    r!   r"   s     r%   r   r   4   r&   r'   r   zshi-labs/nat-mini-in1k-224)r   é   r)   i   z	tiger catc                   óÐ   — e Zd ZU dZdZeej                     ed<   dZ	ee
ej                  df      ed<   dZee
ej                  df      ed<   dZee
ej                  df      ed<   y)ÚNatEncoderOutputa  
    Nat encoder's outputs, with potential hidden states and attentions.

    Args:
        last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
            Sequence of hidden-states at the output of the last layer of the model.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
            shape `(batch_size, sequence_length, hidden_size)`.

            Hidden-states of the model at the output of each layer plus the initial embedding outputs.
        attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each stage) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`.

            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
            heads.
        reshaped_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
            shape `(batch_size, hidden_size, height, width)`.

            Hidden-states of the model at the output of each layer plus the initial embedding outputs reshaped to
            include the spatial dimensions.
    NÚlast_hidden_state.Úhidden_statesÚ
attentionsÚreshaped_hidden_states)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r,   r   ÚtorchÚFloatTensorÚ__annotations__r-   r   r.   r/   © r'   r%   r+   r+   I   s}   … ñð2 6:Ð�x × 1Ñ 1Ñ2Ó9Ø=A€M�8˜E %×"3Ñ"3°SÐ"8Ñ9Ñ:ÓAØ:>€J�˜˜u×0Ñ0°#Ð5Ñ6Ñ7Ó>ØFJÐ˜H U¨5×+<Ñ+<¸cÐ+AÑ%BÑCÔJr'   r+   c                   óø   — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eeej                  df      ed<   dZeeej                  df      ed<   dZeeej                  df      ed<   y)	ÚNatModelOutputaS  
    Nat model's outputs that also contains a pooling of the last hidden states.

    Args:
        last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`):
            Sequence of hidden-states at the output of the last layer of the model.
        pooler_output (`torch.FloatTensor` of shape `(batch_size, hidden_size)`, *optional*, returned when `add_pooling_layer=True` is passed):
            Average pooling of the last layer hidden-state.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
            shape `(batch_size, sequence_length, hidden_size)`.

            Hidden-states of the model at the output of each layer plus the initial embedding outputs.
        attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each stage) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`.

            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
            heads.
        reshaped_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
            shape `(batch_size, hidden_size, height, width)`.

            Hidden-states of the model at the output of each layer plus the initial embedding outputs reshaped to
            include the spatial dimensions.
    Nr,   Úpooler_output.r-   r.   r/   )r0   r1   r2   r3   r,   r   r4   r5   r6   r:   r-   r   r.   r/   r7   r'   r%   r9   r9   j   s’   … ñð6 6:Ð�x × 1Ñ 1Ñ2Ó9Ø15€M�8˜E×-Ñ-Ñ.Ó5Ø=A€M�8˜E %×"3Ñ"3°SÐ"8Ñ9Ñ:ÓAØ:>€J�˜˜u×0Ñ0°#Ð5Ñ6Ñ7Ó>ØFJÐ˜H U¨5×+<Ñ+<¸cÐ+AÑ%BÑCÔJr'   r9   c                   óø   — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eeej                  df      ed<   dZeeej                  df      ed<   dZeeej                  df      ed<   y)	ÚNatImageClassifierOutputa   
    Nat outputs for image classification.

    Args:
        loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
            Classification (or regression if config.num_labels==1) loss.
        logits (`torch.FloatTensor` of shape `(batch_size, config.num_labels)`):
            Classification (or regression if config.num_labels==1) scores (before SoftMax).
        hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
            shape `(batch_size, sequence_length, hidden_size)`.

            Hidden-states of the model at the output of each layer plus the initial embedding outputs.
        attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `torch.FloatTensor` (one for each stage) of shape `(batch_size, num_heads, sequence_length,
            sequence_length)`.

            Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
            heads.
        reshaped_hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each stage) of
            shape `(batch_size, hidden_size, height, width)`.

            Hidden-states of the model at the output of each layer plus the initial embedding outputs reshaped to
            include the spatial dimensions.
    NÚlossÚlogits.r-   r.   r/   )r0   r1   r2   r3   r=   r   r4   r5   r6   r>   r-   r   r.   r/   r7   r'   r%   r<   r<   Ž   s‘   … ñð6 )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø*.€FˆH�U×&Ñ&Ñ'Ó.Ø=A€M�8˜E %×"3Ñ"3°SÐ"8Ñ9Ñ:ÓAØ:>€J�˜˜u×0Ñ0°#Ð5Ñ6Ñ7Ó>ØFJÐ˜H U¨5×+<Ñ+<¸cÐ+AÑ%BÑCÔJr'   r<   c                   óf   ‡ — e Zd ZdZˆ fd„Zdeej                     deej                     fd„Z
ˆ xZS )ÚNatEmbeddingsz6
    Construct the patch and position embeddings.
    c                 óÒ   •— t         ‰| �  «        t        |«      | _        t	        j
                  |j                  «      | _        t	        j                  |j                  «      | _
        y r    )ÚsuperÚ__init__ÚNatPatchEmbeddingsÚpatch_embeddingsr   Ú	LayerNormÚ	embed_dimÚnormÚDropoutÚhidden_dropout_probÚdropout©ÚselfÚconfigÚ	__class__s     €r%   rC   zNatEmbeddings.__init__·   sG   ø€ Ü‰ÑÔä 2°6Ó :ˆÔä—L‘L ×!1Ñ!1Ó2ˆŒ	Ü—z‘z &×"<Ñ"<Ó=ˆ�r'   Úpixel_valuesÚreturnc                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S r    )rE   rH   rK   )rM   rP   Ú
embeddingss      r%   ÚforwardzNatEmbeddings.forward¿   s4   € Ø×*Ñ*¨<Ó8ˆ
Ø—Y‘Y˜zÓ*ˆ
à—\‘\ *Ó-ˆ
àÐr'   )r0   r1   r2   r3   rC   r   r4   r5   r   ÚTensorrT   Ú__classcell__©rO   s   @r%   r@   r@   ²   s4   ø„ ñô>ð H¨U×->Ñ->Ñ$?ð ÀEÈ%Ï,É,ÑDW÷ r'   r@   c                   ó`   ‡ — e Zd ZdZˆ fd„Zdeej                     dej                  fd„Z	ˆ xZ
S )rD   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, height, width, hidden_size)` to be consumed by a
    Transformer.
    c           
      óP  •— t         ‰| �  «        |j                  }|j                  |j                  }}|| _        |dk(  rnt        d«      ‚t        j                  t        j                  | j                  |dz  ddd¬«      t        j                  |dz  |ddd¬«      «      | _	        y )Nr   z2Dinat only supports patch size of 4 at the moment.é   ©é   r\   ©rZ   rZ   ©r   r   )Úkernel_sizeÚstrideÚpadding)
rB   rC   Ú
patch_sizeÚnum_channelsrG   Ú
ValueErrorr   Ú
SequentialÚConv2dÚ
projection)rM   rN   rb   rc   Úhidden_sizerO   s        €r%   rC   zNatPatchEmbeddings.__init__Ï   sš   ø€ Ü‰ÑÔØ×&Ñ&ˆ
Ø$*×$7Ñ$7¸×9IÑ9I�kˆØ(ˆÔà˜Š?Øô ÐQÓRÐRäŸ-™-Ü�I‰I�d×'Ñ'¨¸Ñ)9ÀvÐV\ÐflÔmÜ�I‰I�k QÑ&¨ÀÐPVÐ`fÔgó
ˆ�r'   rP   rQ   c                 ó¦   — |j                   \  }}}}|| j                  k7  rt        d«      ‚| j                  |«      }|j	                  dddd«      }|S )NzeMake sure that the channel dimension of the pixel values match with the one set in the configuration.r   rZ   r\   r   )Úshaperc   rd   rg   Úpermute)rM   rP   Ú_rc   ÚheightÚwidthrS   s          r%   rT   zNatPatchEmbeddings.forwardà   s`   € Ø)5×);Ñ);Ñ&ˆˆ<˜ Ø˜4×,Ñ,Ò,ÜØwóð ð —_‘_ \Ó2ˆ
Ø×'Ñ'¨¨1¨a°Ó3ˆ
àÐr'   )r0   r1   r2   r3   rC   r   r4   r5   rU   rT   rV   rW   s   @r%   rD   rD   È   s/   ø„ ñô
ð"	 H¨U×->Ñ->Ñ$?ð 	ÀEÇLÁL÷ 	r'   rD   c                   ó”   ‡ — e Zd ZdZej
                  fdedej                  ddfˆ fd„Zde	j                  de	j                  fd„Zˆ xZS )	ÚNatDownsamplerzâ
    Convolutional Downsampling Layer.

    Args:
        dim (`int`):
            Number of input channels.
        norm_layer (`nn.Module`, *optional*, defaults to `nn.LayerNorm`):
            Normalization layer class.
    ÚdimÚ
norm_layerrQ   Nc                 ó–   •— t         ‰| �  «        || _        t        j                  |d|z  dddd¬«      | _         |d|z  «      | _        y )NrZ   r[   r]   r^   F)r_   r`   ra   Úbias)rB   rC   rq   r   rf   Ú	reductionrH   )rM   rq   rr   rO   s      €r%   rC   zNatDownsampler.__init__÷   sE   ø€ Ü‰ÑÔØˆŒÜŸ™ 3¨¨C©¸VÈFÐ\bÐinÔoˆŒÙ˜q 3™wÓ'ˆ�	r'   Úinput_featurec                 ó’   — | j                  |j                  dddd«      «      j                  dddd«      }| j                  |«      }|S )Nr   r\   r   rZ   )ru   rk   rH   )rM   rv   s     r%   rT   zNatDownsampler.forwardý   sJ   € ØŸ™ }×'<Ñ'<¸QÀÀ1ÀaÓ'HÓI×QÑQÐRSÐUVÐXYÐ[\Ó]ˆØŸ	™	 -Ó0ˆØÐr'   )r0   r1   r2   r3   r   rF   ÚintÚModulerC   r4   rU   rT   rV   rW   s   @r%   rp   rp   ì   sJ   ø„ ñð :<¿¹ñ (˜Cð (¨R¯Y©Yð (È$õ (ð U§\¡\ð °e·l±l÷ r'   rp   ÚinputÚ	drop_probÚtrainingrQ   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 )aF  
    Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).

    Comment by Ross Wightman: This is the same as the DropConnect impl I created for EfficientNet, etc networks,
    however, the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
    See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for changing the
    layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use 'survival rate' as the
    argument.
    ç        r   r   )r   )ÚdtypeÚdevice)rj   Úndimr4   Úrandr   r€   Úfloor_Údiv)rz   r{   r|   Ú	keep_probrj   Úrandom_tensorÚoutputs          r%   Ú	drop_pathrˆ     s�   € ð �CÒ™xØˆØ�I‘€IØ�[‰[˜‰^Ð ¨¯
©
°Q©Ñ 7Ñ7€EØ¤§
¡
¨5¸¿¹ÈEÏLÉLÔ YÑY€MØ×ÑÔØ�Y‰Y�yÓ! MÑ1€FØ€Mr'   c                   óx   ‡ — e Zd ZdZd	dee   ddfˆ fd„Zdej                  dej                  fd„Z	de
fd„Zˆ xZS )
ÚNatDropPathzXDrop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).Nr{   rQ   c                 ó0   •— t         ‰| �  «        || _        y r    )rB   rC   r{   )rM   r{   rO   s     €r%   rC   zNatDropPath.__init__  s   ø€ Ü‰ÑÔØ"ˆ�r'   r-   c                 óD   — t        || j                  | j                  «      S r    )rˆ   r{   r|   ©rM   r-   s     r%   rT   zNatDropPath.forward  s   € Ü˜¨¯©¸¿¹ÓFÐFr'   c                 ó8   — dj                  | j                  «      S )Nzp={})Úformatr{   ©rM   s    r%   Ú
extra_reprzNatDropPath.extra_repr!  s   € Ø�}‰}˜TŸ^™^Ó,Ð,r'   r    )r0   r1   r2   r3   r   ÚfloatrC   r4   rU   rT   Ústrr‘   rV   rW   s   @r%   rŠ   rŠ     sG   ø„ Ùbñ# (¨5¡/ð #¸Tõ #ðG U§\¡\ð G°e·l±ló Gð-˜C÷ -r'   rŠ   c                   óp   ‡ — e Zd Zˆ fd„Zd„ Z	 ddej                  dee   de	ej                     fd„Z
ˆ xZS )ÚNeighborhoodAttentionc                 ó  •— t         ‰| �  «        ||z  dk7  rt        d|› d|› d�«      ‚|| _        t	        ||z  «      | _        | j                  | j
                  z  | _        || _        t        j                  t        j                  |d| j                  z  dz
  d| j                  z  dz
  «      «      | _        t        j                  | j                  | j                  |j                  ¬«      | _        t        j                  | j                  | j                  |j                  ¬«      | _        t        j                  | j                  | j                  |j                  ¬«      | _        t        j$                  |j&                  «      | _        y )Nr   zThe hidden size (z6) is not a multiple of the number of attention heads (ú)rZ   r   )rt   )rB   rC   rd   Únum_attention_headsrx   Úattention_head_sizeÚall_head_sizer_   r   Ú	Parameterr4   ÚzerosÚrpbÚLinearÚqkv_biasÚqueryÚkeyÚvaluerI   Úattention_probs_dropout_probrK   ©rM   rN   rq   Ú	num_headsr_   rO   s        €r%   rC   zNeighborhoodAttention.__init__&  s=  ø€ Ü‰ÑÔØ�‰?˜aÒÜØ# C 5Ð(^Ð_hÐ^iÐijÐkóð ð $-ˆÔ Ü#& s¨Y¡Ó#7ˆÔ Ø!×5Ñ5¸×8PÑ8PÑPˆÔØ&ˆÔô —<‘<¤§¡¨I¸¸D×<LÑ<LÑ8LÈqÑ8PÐTUÐX\×XhÑXhÑThÐklÑTlÓ nÓoˆŒä—Y‘Y˜t×1Ñ1°4×3EÑ3EÈFÏOÉOÔ\ˆŒ
Ü—9‘9˜T×/Ñ/°×1CÑ1CÈ&Ï/É/ÔZˆŒÜ—Y‘Y˜t×1Ñ1°4×3EÑ3EÈFÏOÉOÔ\ˆŒ
ä—z‘z &×"EÑ"EÓFˆ�r'   c                 ó¦   — |j                  «       d d | j                  | j                  fz   }|j                  |«      }|j	                  ddddd«      S )Néÿÿÿÿr   r\   r   rZ   r   )Úsizer˜   r™   Úviewrk   )rM   ÚxÚnew_x_shapes      r%   Útranspose_for_scoresz*NeighborhoodAttention.transpose_for_scores;  sN   € Ø—f‘f“h˜s �m t×'?Ñ'?À×AYÑAYÐ&ZÑZˆØ�F‰F�;ÓˆØ�y‰y˜˜A˜q ! QÓ'Ð'r'   r-   Úoutput_attentionsrQ   c                 ó¤  — | j                  | j                  |«      «      }| j                  | j                  |«      «      }| j                  | j                  |«      «      }|t	        j
                  | j                  «      z  }t        ||| j                  | j                  d«      }t        j                  j                  |d¬«      }| j                  |«      }t        ||| j                  d«      }|j                  ddddd«      j!                  «       }|j#                  «       d d | j$                  fz   }	|j'                  |	«      }|r||f}
|
S |f}
|
S )	Nr   r§   ©rq   r   rZ   r\   r   éþÿÿÿ)r¬   r    r¡   r¢   ÚmathÚsqrtr™   r   r�   r_   r   Ú
functionalÚsoftmaxrK   r   rk   Ú
contiguousr¨   rš   r©   )rM   r-   r­   Úquery_layerÚ	key_layerÚvalue_layerÚattention_scoresÚattention_probsÚcontext_layerÚnew_context_layer_shapeÚoutputss              r%   rT   zNeighborhoodAttention.forward@  sE  € ð
 ×/Ñ/°·
±
¸=Ó0IÓJˆØ×-Ñ-¨d¯h©h°}Ó.EÓFˆ	Ø×/Ñ/°·
±
¸=Ó0IÓJˆð
 "¤D§I¡I¨d×.FÑ.FÓ$GÑGˆô )¨°iÀÇÁÈ4×K[ÑK[Ð]^Ó_Ðô Ÿ-™-×/Ñ/Ð0@ÀbÐ/ÓIˆð Ÿ,™, Ó7ˆä" ?°KÀ×AQÑAQÐSTÓUˆØ%×-Ñ-¨a°°A°q¸!Ó<×GÑGÓIˆØ"/×"4Ñ"4Ó"6°s¸Ð";¸t×?QÑ?QÐ>SÑ"SÐØ%×*Ñ*Ð+BÓCˆá6G�= /Ð2ˆàˆð O\ÐM]ˆàˆr'   ©F)r0   r1   r2   rC   r¬   r4   rU   r   Úboolr   rT   rV   rW   s   @r%   r•   r•   %  sE   ø„ ôGò*(ð -2ñà—|‘|ðð $ D™>ðð 
ˆu�|‰|Ñ	÷	r'   r•   c                   ón   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  dej
                  fd„Zˆ xZS )ÚNeighborhoodAttentionOutputc                 ó    •— t         ‰| �  «        t        j                  ||«      | _        t        j
                  |j                  «      | _        y r    )rB   rC   r   rž   ÚdenserI   r£   rK   ©rM   rN   rq   rO   s      €r%   rC   z$NeighborhoodAttentionOutput.__init__c  s6   ø€ Ü‰ÑÔÜ—Y‘Y˜s CÓ(ˆŒ
Ü—z‘z &×"EÑ"EÓFˆ�r'   r-   Úinput_tensorrQ   c                 óJ   — | j                  |«      }| j                  |«      }|S r    ©rÃ   rK   )rM   r-   rÅ   s      r%   rT   z#NeighborhoodAttentionOutput.forwardh  s$   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆàÐr'   ©r0   r1   r2   rC   r4   rU   rT   rV   rW   s   @r%   rÁ   rÁ   b  s2   ø„ ôGð
 U§\¡\ð ÀÇÁð ÐRW×R^ÑR^÷ r'   rÁ   c                   óp   ‡ — e Zd Zˆ fd„Zd„ Z	 ddej                  dee   de	ej                     fd„Z
ˆ xZS )ÚNeighborhoodAttentionModulec                 óˆ   •— t         ‰| �  «        t        ||||«      | _        t	        ||«      | _        t        «       | _        y r    )rB   rC   r•   rM   rÁ   r‡   ÚsetÚpruned_headsr¤   s        €r%   rC   z$NeighborhoodAttentionModule.__init__p  s8   ø€ Ü‰ÑÔÜ)¨&°#°yÀ+ÓNˆŒ	Ü1°&¸#Ó>ˆŒÜ›EˆÕr'   c                 ó>  — t        |«      dk(  ry t        || j                  j                  | j                  j                  | j
                  «      \  }}t        | j                  j                  |«      | j                  _        t        | j                  j                  |«      | j                  _        t        | j                  j                  |«      | j                  _	        t        | j                  j                  |d¬«      | j                  _        | j                  j                  t        |«      z
  | j                  _        | j                  j                  | j                  j                  z  | j                  _        | j
                  j                  |«      | _        y )Nr   r   r¯   )Úlenr   rM   r˜   r™   rÍ   r   r    r¡   r¢   r‡   rÃ   rš   Úunion)rM   ÚheadsÚindexs      r%   Úprune_headsz'NeighborhoodAttentionModule.prune_headsv  s  € Üˆu‹:˜Š?ØÜ7Ø�4—9‘9×0Ñ0°$·)±)×2OÑ2OÐQU×QbÑQbó
‰ˆˆuô
 -¨T¯Y©Y¯_©_¸eÓDˆ�	‰	ŒÜ*¨4¯9©9¯=©=¸%Ó@ˆ�	‰	ŒÜ,¨T¯Y©Y¯_©_¸eÓDˆ�	‰	ŒÜ.¨t¯{©{×/@Ñ/@À%ÈQÔOˆ�‰Ôð )-¯	©	×(EÑ(EÌÈEË
Ñ(Rˆ�	‰	Ô%Ø"&§)¡)×"?Ñ"?À$Ç)Á)×B_ÑB_Ñ"_ˆ�	‰	ÔØ ×-Ñ-×3Ñ3°EÓ:ˆÕr'   r-   r­   rQ   c                 óf   — | j                  ||«      }| j                  |d   |«      }|f|dd  z   }|S ©Nr   r   )rM   r‡   )rM   r-   r­   Úself_outputsÚattention_outputr½   s         r%   rT   z#NeighborhoodAttentionModule.forwardˆ  sC   € ð
 —y‘y Ð0AÓBˆØŸ;™; |°A¡¸ÓFÐØ#Ð%¨°Q°RÐ(8Ñ8ˆØˆr'   r¾   )r0   r1   r2   rC   rÓ   r4   rU   r   r¿   r   rT   rV   rW   s   @r%   rÊ   rÊ   o  sD   ø„ ô"ò;ð* -2ñà—|‘|ðð $ D™>ðð 
ˆu�|‰|Ñ	÷	r'   rÊ   c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚNatIntermediatec                 ó  •— t         ‰| �  «        t        j                  |t	        |j
                  |z  «      «      | _        t        |j                  t        «      rt        |j                     | _        y |j                  | _        y r    )rB   rC   r   rž   rx   Ú	mlp_ratiorÃ   Ú
isinstanceÚ
hidden_actr“   r   Úintermediate_act_fnrÄ   s      €r%   rC   zNatIntermediate.__init__”  sa   ø€ Ü‰ÑÔÜ—Y‘Y˜s¤C¨×(8Ñ(8¸3Ñ(>Ó$?Ó@ˆŒ
Ü�f×'Ñ'¬Ô-Ü'-¨f×.?Ñ.?Ñ'@ˆDÕ$à'-×'8Ñ'8ˆDÕ$r'   r-   rQ   c                 óJ   — | j                  |«      }| j                  |«      }|S r    )rÃ   rÞ   r�   s     r%   rT   zNatIntermediate.forwardœ  s&   € ØŸ
™
 =Ó1ˆØ×0Ñ0°Ó?ˆØÐr'   rÈ   rW   s   @r%   rÙ   rÙ   “  s#   ø„ ô9ð U§\¡\ð °e·l±l÷ r'   rÙ   c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )Ú	NatOutputc                 óÌ   •— t         ‰| �  «        t        j                  t	        |j
                  |z  «      |«      | _        t        j                  |j                  «      | _	        y r    )
rB   rC   r   rž   rx   rÛ   rÃ   rI   rJ   rK   rÄ   s      €r%   rC   zNatOutput.__init__£  sF   ø€ Ü‰ÑÔÜ—Y‘Yœs 6×#3Ñ#3°cÑ#9Ó:¸CÓ@ˆŒ
Ü—z‘z &×"<Ñ"<Ó=ˆ�r'   r-   rQ   c                 óJ   — | j                  |«      }| j                  |«      }|S r    rÇ   r�   s     r%   rT   zNatOutput.forward¨  s$   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆØÐr'   rÈ   rW   s   @r%   rá   rá   ¢  s#   ø„ ô>ð
 U§\¡\ð °e·l±l÷ r'   rá   c            	       óŠ   ‡ — e Zd Zdˆ fd„	Zd„ Z	 ddej                  dee   de	ej                  ej                  f   fd„Z
ˆ xZS )	ÚNatLayerc                 óz  •— t         ‰| �  «        |j                  | _        |j                  | _        t	        j
                  ||j                  ¬«      | _        t        |||| j                  ¬«      | _	        |dkD  rt        |«      nt	        j                  «       | _        t	        j
                  ||j                  ¬«      | _        t        ||«      | _        t!        ||«      | _        |j$                  dkD  r?t	        j&                  |j$                  t)        j*                  d|f«      z  d¬«      | _        y d | _        y )N©Úeps)r_   r~   r   rZ   T)Úrequires_grad)rB   rC   Úchunk_size_feed_forwardr_   r   rF   Úlayer_norm_epsÚlayernorm_beforerÊ   Ú	attentionrŠ   ÚIdentityrˆ   Úlayernorm_afterrÙ   Úintermediaterá   r‡   Úlayer_scale_init_valuer›   r4   ÚonesÚlayer_scale_parameters)rM   rN   rq   r¥   Údrop_path_raterO   s        €r%   rC   zNatLayer.__init__¯  sÿ   ø€ Ü‰ÑÔØ'-×'EÑ'EˆÔ$Ø!×-Ñ-ˆÔÜ "§¡¨S°f×6KÑ6KÔ LˆÔÜ4°V¸SÀ)ÐY]×YiÑYiÔjˆŒØ8FÈÒ8Lœ ^Ô4ÔRT×R]ÑR]ÓR_ˆŒÜ!Ÿ|™|¨C°V×5JÑ5JÔKˆÔÜ+¨F°CÓ8ˆÔÜ ¨Ó,ˆŒð ×,Ñ,¨qÒ0ô �L‰L˜×6Ñ6¼¿¹ÀQÈÀHÓ9MÑMÐ]aÔbð 	Õ#ð ð 	Õ#r'   c                 óÎ   — | j                   }d}||k  s||k  rJdx}}t        d||z
  «      }t        d||z
  «      }	dd||||	f}t        j                  j	                  ||«      }||fS )N)r   r   r   r   r   r   r   )r_   Úmaxr   r³   Úpad)
rM   r-   rm   rn   Úwindow_sizeÚ
pad_valuesÚpad_lÚpad_tÚpad_rÚpad_bs
             r%   Ú	maybe_padzNatLayer.maybe_pad¿  s€   € Ø×&Ñ&ˆØ'ˆ
Ø�KÒ 5¨;Ò#6ØÐˆE�EÜ˜˜;¨Ñ.Ó/ˆEÜ˜˜;¨Ñ/Ó0ˆEØ˜Q  u¨e°UÐ;ˆJÜŸM™M×-Ñ-¨m¸ZÓHˆMØ˜jÐ(Ð(r'   r-   r­   rQ   c                 ó€  — |j                  «       \  }}}}|}| j                  |«      }| j                  |||«      \  }}|j                  \  }	}
}}	| j	                  ||¬«      }|d   }|d   dkD  xs |d   dkD  }|r|d d …d |…d |…d d …f   j                  «       }| j                  �| j                  d   |z  }|| j                  |«      z   }| j                  |«      }| j                  | j                  |«      «      }| j                  �| j                  d   |z  }|| j                  |«      z   }|r	||d   f}|S |f}|S )N)r­   r   r\   é   r   )r¨   rì   rþ   rj   rí   rµ   ró   rˆ   rï   r‡   rð   )rM   r-   r­   Ú
batch_sizerm   rn   ÚchannelsÚshortcutrù   rl   Ú
height_padÚ	width_padÚattention_outputsr×   Ú
was_paddedÚlayer_outputÚlayer_outputss                    r%   rT   zNatLayer.forwardÊ  s|  € ð
 /<×.@Ñ.@Ó.BÑ+ˆ
�F˜E 8Ø ˆà×-Ñ-¨mÓ<ˆà$(§N¡N°=À&È%Ó$PÑ!ˆ�zà&3×&9Ñ&9Ñ#ˆˆ:�y !à ŸN™N¨=ÐL]˜NÓ^Ðà,¨QÑ/Ðà ‘] QÑ&Ò;¨*°Q©-¸!Ñ*;ˆ
ÙØ/²°7°F°7¸F¸U¸FÂAÐ0EÑF×QÑQÓSÐà×&Ñ&Ð2Ø#×:Ñ:¸1Ñ=Ð@PÑPÐà  4§>¡>Ð2BÓ#CÑCˆà×+Ñ+¨MÓ:ˆØ—{‘{ 4×#4Ñ#4°\Ó#BÓCˆà×&Ñ&Ð2Ø×6Ñ6°qÑ9¸LÑHˆLà$ t§~¡~°lÓ'CÑCˆá@Q˜Ð'8¸Ñ';Ð<ˆØÐð YeÐWfˆØÐr'   )r~   r¾   )r0   r1   r2   rC   rþ   r4   rU   r   r¿   r   rT   rV   rW   s   @r%   rå   rå   ®  sM   ø„ õ
ò 	)ð -2ñ$à—|‘|ð$ð $ D™>ð$ð 
ˆu�|‰|˜UŸ\™\Ð)Ñ	*÷	$r'   rå   c                   ój   ‡ — e Zd Zˆ fd„Z	 ddej
                  dee   deej
                     fd„Z	ˆ xZ
S )ÚNatStagec                 ó4  •— t         ‰| �  «        || _        || _        t	        j
                  t        |«      D �cg c]  }t        |||||   ¬«      ‘Œ c}«      | _        |�% ||t        j                  ¬«      | _
        d| _        y d | _
        d| _        y c c}w )N)rN   rq   r¥   rô   )rq   rr   F)rB   rC   rN   rq   r   Ú
ModuleListÚrangerå   ÚlayersrF   Ú
downsampleÚpointing)	rM   rN   rq   Údepthr¥   rô   r  ÚirO   s	           €r%   rC   zNatStage.__init__ò  s˜   ø€ Ü‰ÑÔØˆŒØˆŒÜ—m‘mô ˜u›öð ô Ø!ØØ'Ø#1°!Ñ#4ö	òó

ˆŒð Ð!Ù(¨S¼R¿\¹\ÔJˆDŒOð ˆ�ð #ˆDŒOàˆ�ùò#s   ºBr-   r­   rQ   c                 óà   — |j                  «       \  }}}}t        | j                  «      D ]  \  }} |||«      }|d   }Œ |}	| j                  �| j                  |	«      }||	f}
|r|
dd  z  }
|
S rÕ   )r¨   Ú	enumerater  r  )rM   r-   r­   rl   rm   rn   r  Úlayer_moduler	  Ú!hidden_states_before_downsamplingÚstage_outputss              r%   rT   zNatStage.forward
  s”   € ð
 ,×0Ñ0Ó2Ñˆˆ6�5˜!Ü(¨¯©Ó5ò 	-‰OˆAˆ|Ù(¨Ð8IÓJˆMØ)¨!Ñ,‰Mð	-ð -:Ð)Ø�?‰?Ð&Ø ŸO™OÐ,MÓNˆMà&Ð(IÐJˆáØ˜]¨1¨2Ð.Ñ.ˆMØÐr'   r¾   )r0   r1   r2   rC   r4   rU   r   r¿   r   rT   rV   rW   s   @r%   r  r  ñ  s?   ø„ ôð6 -2ñà—|‘|ðð $ D™>ðð 
ˆu�|‰|Ñ	÷	r'   r  c                   ó~   ‡ — e Zd Zˆ fd„Z	 	 	 	 d	dej
                  dee   dee   dee   dee   dee	e
f   fd„Zˆ xZS )
Ú
NatEncoderc                 ó¤  •— t         ‰| �  «        t        |j                  «      | _        || _        t        j                  d|j                  t        |j                  «      «      D �cg c]  }|j                  «       ‘Œ }}t        j                  t        | j                  «      D �cg c]�  }t        |t        |j                   d|z  z  «      |j                  |   |j"                  |   |t        |j                  d | «      t        |j                  d |dz    «       || j                  dz
  k  rt$        nd ¬«      ‘Œ’ c}«      | _        y c c}w c c}w )Nr   rZ   r   )rN   rq   r  r¥   rô   r  )rB   rC   rÏ   ÚdepthsÚ
num_levelsrN   r4   Úlinspacerô   ÚsumÚitemr   r  r  r  rx   rG   r¥   rp   Úlevels)rM   rN   rª   ÚdprÚi_layerrO   s        €r%   rC   zNatEncoder.__init__   s  ø€ Ü‰ÑÔÜ˜fŸm™mÓ,ˆŒØˆŒÜ!&§¡°°6×3HÑ3HÌ#ÈfÏmÉmÓJ\Ó!]Ö^˜Aˆq�v‰v�xÐ^ˆÐ^Ü—m‘mô  % T§_¡_Ó5ö
ð ô Ø!Ü˜F×,Ñ,¨q°'©zÑ9Ó:Ø Ÿ-™-¨Ñ0Ø$×.Ñ.¨wÑ7Ø#&¤s¨6¯=©=¸¸'Ð+BÓ'CÄcÈ&Ï-É-ÐXeÐZaÐdeÑZeÐJfÓFgÐ#hØ29¸D¿O¹OÈaÑ<OÒ2O�~ÐVZöò
ó
ˆ�ùò _ùò
s   Á'EÂ&BEr-   r­   Úoutput_hidden_statesÚ(output_hidden_states_before_downsamplingÚreturn_dictrQ   c                 ó¾  — |rdnd }|rdnd }|rdnd }|r |j                  dddd«      }	||fz  }||	fz  }t        | j                  «      D ]l  \  }
} |||«      }|d   }|d   }|r#|r!|j                  dddd«      }	||fz  }||	fz  }n$|r"|s |j                  dddd«      }	||fz  }||	fz  }|sŒe||dd  z  }Œn |st        d„ |||fD «       «      S t	        ||||¬«      S )Nr7   r   r\   r   rZ   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wr    r7   )Ú.0Úvs     r%   ú	<genexpr>z%NatEncoder.forward.<locals>.<genexpr>Z  s   è ø€ Òm˜qÐ_`Ñ_lœÑmùs   ‚Š)r,   r-   r.   r/   )rk   r  r!  Útupler+   )rM   r-   r­   r$  r%  r&  Úall_hidden_statesÚall_reshaped_hidden_statesÚall_self_attentionsÚreshaped_hidden_stater  r  r	  r  s                 r%   rT   zNatEncoder.forward3  s]  € ñ #7™B¸DÐÙ+?¡RÀTÐ"Ù$5™b¸4Ðáà$1×$9Ñ$9¸!¸QÀÀ1Ó$EÐ!Ø -Ð!1Ñ1ÐØ&Ð+@Ð*BÑBÐ&ä(¨¯©Ó5ò 	9‰OˆAˆ|Ù(¨Ð8IÓJˆMà)¨!Ñ,ˆMØ0=¸aÑ0@Ð-á#Ñ(Pà(I×(QÑ(QÐRSÐUVÐXYÐ[\Ó(]Ð%Ø!Ð&GÐ%IÑIÐ!Ø*Ð/DÐ.FÑFÑ*Ù%Ñ.Và(5×(=Ñ(=¸aÀÀAÀqÓ(IÐ%Ø! mÐ%5Ñ5Ð!Ø*Ð/DÐ.FÑFÐ*â Ø# }°Q°RÐ'8Ñ8Ñ#ð%	9ñ( ÜÑm ]Ð4EÐGZÐ$[ÔmÓmÐmäØ+Ø+Ø*Ø#=ô	
ð 	
r'   )FFFT)r0   r1   r2   rC   r4   rU   r   r¿   r   r   r+   rT   rV   rW   s   @r%   r  r    st   ø„ ô
ð, -2Ø/4ØCHØ&*ñ.
à—|‘|ð.
ð $ D™>ð.
ð ' t™nð	.
ð
 3;¸4±.ð.
ð ˜d‘^ð.
ð 
ˆuÐ&Ð&Ñ	'÷.
r'   r  c                   ó"   — e Zd ZdZeZdZdZd„ Zy)ÚNatPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    ÚnatrP   c                 óú  — t        |t        j                  t        j                  f«      rm|j                  j
                  j                  d| j                  j                  ¬«       |j                  �%|j                  j
                  j                  «        yyt        |t        j                  «      rJ|j                  j
                  j                  «        |j                  j
                  j                  d«       yy)zInitialize the weightsr~   )ÚmeanÚstdNg      ð?)rÜ   r   rž   rf   ÚweightÚdataÚnormal_rN   Úinitializer_rangert   Úzero_rF   Úfill_)rM   Úmodules     r%   Ú_init_weightsz NatPreTrainedModel._init_weightsn  s¨   € ä�fœrŸy™y¬"¯)©)Ð4Ô5ð �M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ä˜¤§¡Ô-Ø�K‰K×Ñ×"Ñ"Ô$Ø�M‰M×Ñ×$Ñ$ SÕ)ð .r'   N)	r0   r1   r2   r3   r   Úconfig_classÚbase_model_prefixÚmain_input_namer>  r7   r'   r%   r2  r2  d  s   „ ñð
 €LØÐØ$€Oó
*r'   r2  aF  
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class. Use
    it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`NatConfig`]): 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 [`~PreTrainedModel.from_pretrained`] method to load the model weights.
a  
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ViTImageProcessor.__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.
z]The bare Nat Model transformer outputting raw hidden-states without any specific head on top.c                   ó¾   ‡ — e Zd Zdˆ fd„	Zd„ Zd„ Z ee«       ee	e
ede¬«      	 	 	 	 ddeej                     dee   dee   d	ee   d
eee
f   f
d„«       «       Zˆ xZS )ÚNatModelc                 óÖ  •— t         ‰| �  |«       t        | dg«       || _        t	        |j
                  «      | _        t        |j                  d| j                  dz
  z  z  «      | _	        t        |«      | _        t        |«      | _        t        j                  | j                  |j                   ¬«      | _        |rt        j$                  d«      nd | _        | j)                  «        y )NÚnattenrZ   r   rç   )rB   rC   r   rN   rÏ   r  r  rx   rG   Únum_featuresr@   rS   r  Úencoderr   rF   rë   Ú	layernormÚAdaptiveAvgPool1dÚpoolerÚ	post_init)rM   rN   Úadd_pooling_layerrO   s      €r%   rC   zNatModel.__init__�  s±   ø€ Ü‰Ñ˜Ô ä˜$  
Ô+àˆŒÜ˜fŸm™mÓ,ˆŒÜ × 0Ñ 0°1¸¿¹È1Ñ9LÑ3MÑ MÓNˆÔä'¨Ó/ˆŒÜ! &Ó)ˆŒäŸ™ d×&7Ñ&7¸V×=RÑ=RÔSˆŒÙ1B”b×*Ñ*¨1Ô-ÈˆŒð 	�‰Õr'   c                 ó.   — | j                   j                  S r    ©rS   rE   r�   s    r%   Úget_input_embeddingszNatModel.get_input_embeddings¯  ó   € Ø�‰×/Ñ/Ð/r'   c                 ó˜   — |j                  «       D ]7  \  }}| j                  j                  |   j                  j	                  |«       Œ9 y)z�
        Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
        class PreTrainedModel
        N)ÚitemsrG  Úlayerrí   rÓ   )rM   Úheads_to_prunerS  rÑ   s       r%   Ú_prune_headszNatModel._prune_heads²  sE   € ð
 +×0Ñ0Ó2ò 	C‰LˆE�5Ø�L‰L×Ñ˜uÑ%×/Ñ/×;Ñ;¸EÕBñ	Cr'   Úvision)Ú
checkpointÚoutput_typer?  ÚmodalityÚexpected_outputrP   r­   r$  r&  rQ   c                 óR  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|€t	        d«      ‚| j                  |«      }| j                  ||||¬«      }|d   }| j                  |«      }d }| j                  �G| j                  |j                  dd«      j                  dd«      «      }t        j                  |d«      }|s||f|dd  z   }	|	S t        |||j                  |j                  |j                  ¬«      S )Nz You have to specify pixel_values©r­   r$  r&  r   r   rZ   )r,   r:   r-   r.   r/   )rN   r­   r$  Úuse_return_dictrd   rS   rG  rH  rJ  ÚflattenÚ	transposer4   r9   r-   r.   r/   )
rM   rP   r­   r$  r&  Úembedding_outputÚencoder_outputsÚsequence_outputÚpooled_outputr‡   s
             r%   rT   zNatModel.forwardº  sA  € ð 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐÜÐ?Ó@Ð@àŸ?™?¨<Ó8ÐàŸ,™,ØØ/Ø!5Ø#ð	 'ó 
ˆð *¨!Ñ,ˆØŸ.™.¨Ó9ˆàˆØ�;‰;Ð"Ø ŸK™K¨×(?Ñ(?ÀÀ1Ó(E×(OÑ(OÐPQÐSTÓ(UÓVˆMÜ!ŸM™M¨-¸Ó;ˆMáØ% }Ð5¸ÈÈÐ8KÑKˆFàˆMäØ-Ø'Ø)×7Ñ7Ø&×1Ñ1Ø#2×#IÑ#Iô
ð 	
r'   )T)NNNN)r0   r1   r2   rC   rO  rU  r   ÚNAT_INPUTS_DOCSTRINGr   Ú_CHECKPOINT_FOR_DOCr9   Ú_CONFIG_FOR_DOCÚ_EXPECTED_OUTPUT_SHAPEr   r4   r5   r¿   r   r   rT   rV   rW   s   @r%   rC  rC  ˜  s©   ø„ õ
ò$0òCñ +Ð+?Ó@ÙØ&Ø"Ø$ØØ.ôð 59Ø,0Ø/3Ø&*ñ,
à˜u×0Ñ0Ñ1ð,
ð $ D™>ð,
ð ' t™nð	,
ð
 ˜d‘^ð,
ð 
ˆu�nÐ$Ñ	%ò,
óó Aô,
r'   rC  z¤
    Nat 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ˆ fd„Z ee«       eeee	e
¬«      	 	 	 	 	 d
deej                     deej                     dee   dee   dee   deeef   fd	„«       «       Zˆ xZS )ÚNatForImageClassificationc                 óX  •— t         ‰| �  |«       t        | dg«       |j                  | _        t	        |«      | _        |j                  dkD  r4t        j                  | j
                  j                  |j                  «      nt        j                  «       | _
        | j                  «        y )NrE  r   )rB   rC   r   Ú
num_labelsrC  r3  r   rž   rF  rî   Ú
classifierrK  rL   s     €r%   rC   z"NatForImageClassification.__init__ù  s„   ø€ Ü‰Ñ˜Ô ä˜$  
Ô+à ×+Ñ+ˆŒÜ˜FÓ#ˆŒð DJ×CTÑCTÐWXÒCXŒB�I‰I�d—h‘h×+Ñ+¨V×->Ñ->Ô?Ô^`×^iÑ^iÓ^kð 	Œð
 	�‰Õr'   )rW  rX  r?  rZ  rP   Úlabelsr­   r$  r&  rQ   c                 ó*  — |�|n| j                   j                  }| j                  ||||¬«      }|d   }| j                  |«      }d}	|��‡| j                   j                  €�| j
                  dk(  rd| j                   _        nl| j
                  dkD  rL|j                  t        j                  k(  s|j                  t        j                  k(  rd| j                   _        nd| j                   _        | j                   j                  dk(  rIt        «       }
| j
                  dk(  r& |
|j                  «       |j                  «       «      }	nŒ |
||«      }	n‚| j                   j                  dk(  r=t        «       }
 |
|j                  d| j
                  «      |j                  d«      «      }	n,| j                   j                  dk(  rt        «       }
 |
||«      }	|s|f|dd z   }|	�|	f|z   S |S t        |	||j                   |j"                  |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   Ú
regressionÚsingle_label_classificationÚmulti_label_classificationr§   rZ   )r=   r>   r-   r.   r/   )rN   r]  r3  rl  Úproblem_typerk  r   r4   Úlongrx   r
   Úsqueezer	   r©   r   r<   r-   r.   r/   )rM   rP   rm  r­   r$  r&  r½   rc  r>   r=   Úloss_fctr‡   s               r%   rT   z!NatForImageClassification.forward	  sÔ  € ð* &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà—(‘(ØØ/Ø!5Ø#ð	 ó 
ˆð   ™
ˆà—‘ Ó/ˆàˆØÑØ�{‰{×'Ñ'Ð/Ø—?‘? aÒ'Ø/;�D—K‘KÕ,Ø—_‘_ qÒ(¨f¯l©l¼e¿j¹jÒ.HÈFÏLÉLÔ\a×\eÑ\eÒLeØ/L�D—K‘KÕ,à/K�D—K‘KÔ,à�{‰{×'Ñ'¨<Ò7Ü"›9�Ø—?‘? aÒ'Ù# F§N¡NÓ$4°f·n±nÓ6FÓG‘Dá# F¨FÓ3‘DØ—‘×)Ñ)Ð-JÒJÜ+Ó-�Ù §¡¨B°·±Ó @À&Ç+Á+ÈbÃ/ÓR‘Ø—‘×)Ñ)Ð-IÒIÜ,Ó.�Ù ¨Ó/�áØ�Y ¨¨ Ñ,ˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEä'ØØØ!×/Ñ/Ø×)Ñ)Ø#*×#AÑ#Aô
ð 	
r'   )NNNNN)r0   r1   r2   rC   r   rd  r   Ú_IMAGE_CLASS_CHECKPOINTr<   rf  Ú_IMAGE_CLASS_EXPECTED_OUTPUTr   r4   r5   Ú
LongTensorr¿   r   r   rT   rV   rW   s   @r%   ri  ri  ñ  s´   ø„ ôñ  +Ð+?Ó@ÙØ*Ø,Ø$Ø4ô	ð 59Ø-1Ø,0Ø/3Ø&*ñ<
à˜u×0Ñ0Ñ1ð<
ð ˜×)Ñ)Ñ*ð<
ð $ D™>ð	<
ð
 ' t™nð<
ð ˜d‘^ð<
ð 
ˆuÐ.Ð.Ñ	/ò<
óó Aô<
r'   ri  zBNAT backbone, to be used with frameworks like DETR and MaskFormer.c                   óž   ‡ — e Zd Zˆ fd„Zd„ Z ee«       eee	¬«      	 	 	 d
de
j                  dee   dee   dee   def
d	„«       «       Zˆ xZS )ÚNatBackbonec           	      ó.  •— t         ‰| �  |«       t         ‰| �	  |«       t        | dg«       t	        |«      | _        t        |«      | _        |j                  gt        t        |j                  «      «      D �cg c]  }t        |j                  d|z  z  «      ‘Œ c}z   | _        i }t        | j                  | j                   «      D ]  \  }}t#        j$                  |«      ||<   Œ t#        j&                  |«      | _        | j+                  «        y c c}w )NrE  rZ   )rB   rC   Ú_init_backboner   r@   rS   r  rG  rG   r  rÏ   r  rx   rF  ÚzipÚout_featuresr  r   rF   Ú
ModuleDictÚhidden_states_normsrK  )rM   rN   r  r€  Ústagerc   rO   s         €r%   rC   zNatBackbone.__init__T  sñ   ø€ Ü‰Ñ˜Ô Ü‰Ñ˜vÔ&ä˜$  
Ô+ä'¨Ó/ˆŒÜ! &Ó)ˆŒØ#×-Ñ-Ð.ÔX]Ô^aÐbh×boÑboÓ^pÓXqÖ1rÐST´#°f×6FÑ6FÈÈAÉÑ6MÕ2NÒ1rÑrˆÔð !ÐÜ#& t×'8Ñ'8¸$¿-¹-Ó#Hò 	DÑˆE�<Ü)+¯©°lÓ)CÐ Ò&ð	Dä#%§=¡=Ð1DÓ#EˆÔ ð 	�‰Õùò 2ss   Á9"Dc                 ó.   — | j                   j                  S r    rN  r�   s    r%   rO  z NatBackbone.get_input_embeddingsg  rP  r'   )rX  r?  rP   r$  r­   r&  rQ   c                 ó  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }| j	                  |«      }| j                  ||ddd¬«      }|j                  }d}t        | j                  |«      D ]®  \  }	}
|	| j                  v sŒ|
j                  \  }}}}|
j                  dddd«      j                  «       }
|
j                  |||z  |«      }
 | j                  |	   |
«      }
|
j                  ||||«      }
|
j                  dddd«      j                  «       }
||
fz  }Œ° |s|f}|r||j                  fz  }|S t!        ||r|j                  nd|j"                  ¬	«      S )
aA  
        Returns:

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, AutoBackbone
        >>> import torch
        >>> from PIL import Image
        >>> import requests

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

        >>> processor = AutoImageProcessor.from_pretrained("shi-labs/nat-mini-in1k-224")
        >>> model = AutoBackbone.from_pretrained(
        ...     "shi-labs/nat-mini-in1k-224", out_features=["stage1", "stage2", "stage3", "stage4"]
        ... )

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

        >>> outputs = model(**inputs)

        >>> feature_maps = outputs.feature_maps
        >>> list(feature_maps[-1].shape)
        [1, 512, 7, 7]
        ```NT)r­   r$  r%  r&  r7   r   rZ   r\   r   )Úfeature_mapsr-   r.   )rN   r]  r$  r­   rS   rG  r/   r}  Ústage_namesr~  rj   rk   rµ   r©   r€  r-   r   r.   )rM   rP   r$  r­   r&  r`  r½   r-   r„  r�  Úhidden_stater  rc   rm   rn   r‡   s                   r%   rT   zNatBackbone.forwardj  sµ  € ðH &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐàŸ?™?¨<Ó8Ðà—,‘,ØØ/Ø!%Ø59Øð ó 
ˆð  ×6Ñ6ˆàˆÜ#& t×'7Ñ'7¸Ó#Gò 		0ÑˆE�<Ø˜×)Ñ)Ò)à:F×:LÑ:LÑ7�
˜L¨&°%Ø+×3Ñ3°A°q¸!¸QÓ?×JÑJÓL�Ø+×0Ñ0°¸VÀe¹^È\ÓZ�Ø>˜t×7Ñ7¸Ñ>¸|ÓL�Ø+×0Ñ0°¸VÀUÈLÓY�Ø+×3Ñ3°A°q¸!¸QÓ?×JÑJÓL�Ø  Ñ/‘ð		0ñ Ø"�_ˆFÙ#Ø˜7×0Ñ0Ð2Ñ2�ØˆMäØ%Ù3G˜'×/Ò/ÈTØ×)Ñ)ô
ð 	
r'   )NNN)r0   r1   r2   rC   rO  r   rd  r   r   rf  r4   rU   r   r¿   rT   rV   rW   s   @r%   rz  rz  O  sŠ   ø„ ô
ò&0ñ +Ð+?Ó@Ù¨>ÈÔXð 04Ø,0Ø&*ñJ
à—l‘lðJ
ð ' t™nðJ
ð $ D™>ð	J
ð
 ˜d‘^ðJ
ð 
òJ
ó Yó AôJ
r'   rz  )r~   F)Kr3   r±   Údataclassesr   Útypingr   r   r   r4   Útorch.utils.checkpointr   Útorch.nnr   r	   r
   Úactivationsr   Úmodeling_outputsr   Úmodeling_utilsr   Úpytorch_utilsr   r   Úutilsr   r   r   r   r   r   r   r   r   Úutils.backbone_utilsr   Úconfiguration_natr   Únatten.functionalr   r   Ú
get_loggerr0   Úloggerrf  re  rg  rv  rw  r+   r9   r<   ry   r@   rD   rp   rU   r’   r¿   rˆ   rŠ   r•   rÁ   rÊ   rÙ   rá   rå   r  r  r2  ÚNAT_START_DOCSTRINGrd  rC  ri  rz  r7   r'   r%   ú<module>r–     sŒ  ðñ 8ã Ý !ß )Ñ )ã Û Ý ß AÑ Aå "Ý /Ý .ß R÷
÷ 
õ 
õ 3Ý (ñ Ôß;Ð;ò/ò/ð 
ˆ×	Ñ	˜HÓ	%€ð €ð 3Ð Ú'Ð ð 7Ð Ø*Ð ð ôK�{ó Kó ðKð@ ô K�[ó  Kó ð KðF ô K˜{ó  Kó ð KôF�B—I‘Iô ô,!˜Ÿ™ô !ôH�R—Y‘Yô ñ.�U—\‘\ð ¨eð ÀTð ÐV[×VbÑVbó ô(-�"—)‘)ô -ô:˜BŸI™Iô :ôz
 "§)¡)ô 
ô! "§)¡)ô !ôH�b—i‘iô ô	�—	‘	ô 	ô@ˆr�y‰yô @ôF+ˆr�y‰yô +ô\B
�—‘ô B
ôJ*˜ô *ð.	Ð ðÐ ñ" ØcØóôR
Ð!ó R
ó	ðR
ñj ðð óôT
Ð 2ó T
óðT
ñn ØHØóôc
Ð$ mó c
ó	ñc
r'   