
    fTh2              	       t   S r SSKrSSKrSSKrSSKJr  SSKJrJ	r	J
r
  SSKrSSKrSSKJr  SSKJr  SSKJr  SS	KJr  SS
KJrJrJr  SSKJrJrJrJr  SSKJr  SSKJ r   \RB                  " \"5      r#\ " S S\5      5       r$\ " S S\5      5       r%\ " S S\5      5       r&\ " S S\5      5       r'S r(S r) " S S\RT                  5      r+ " S S\RT                  5      r, " S S\RT                  5      r-SES\R\                  S \/S!\0S"\R\                  4S# jjr1 " S$ S%\RT                  5      r2 " S& S'\RT                  5      r3 " S( S)\RT                  5      r4 " S* S+\RT                  5      r5 " S, S-\RT                  5      r6 " S. S/\RT                  5      r7 " S0 S1\RT                  5      r8 " S2 S3\RT                  5      r9 " S4 S5\RT                  5      r:\ " S6 S7\5      5       r;\ " S8 S9\;5      5       r<\" S:S;9 " S< S=\;5      5       r=\" S>S;9 " S? S@\;5      5       r>\" SAS;9 " SB SC\;\5      5       r?/ SDQr@g)FzPyTorch Swin Transformer model.    N)	dataclass)OptionalTupleUnion)nn   )ACT2FN)BackboneOutput)PreTrainedModel) find_pruneable_heads_and_indicesmeshgridprune_linear_layer)ModelOutputauto_docstringlogging	torch_int)BackboneMixin   )
SwinConfigc                       \ rS rSr% SrSr\\R                     \	S'   Sr
\\\R                  S4      \	S'   Sr\\\R                  S4      \	S'   Sr\\\R                  S4      \	S'   S	rg)
SwinEncoderOutput*   a4  
Swin 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____firstlineno____doc__r   r   torchFloatTensor__annotations__r   r   r   r   __static_attributes__r       ^/var/www/auris/envauris/lib/python3.13/site-packages/transformers/models/swin/modeling_swin.pyr   r   *   s}    2 6:x 1 129=AM8E%"3"3S"89:A:>Ju00#567>FJHU5+<+<c+A%BCJr'   r   c                       \ rS rSr% SrSr\\R                     \	S'   Sr
\\R                     \	S'   Sr\\\R                  S4      \	S'   Sr\\\R                  S4      \	S'   Sr\\\R                  S4      \	S	'   S
rg)SwinModelOutputK   a   
Swin 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   r   )r   r   r    r!   r"   r   r   r#   r$   r%   r,   r   r   r   r   r&   r   r'   r(   r*   r*   K   s    6 6:x 1 12915M8E--.5=AM8E%"3"3S"89:A:>Ju00#567>FJHU5+<+<c+A%BCJr'   r*   c                      \ rS rSr% SrSr\\R                     \	S'   Sr
\\R                     \	S'   Sr\\\R                  S4      \	S'   Sr\\\R                  S4      \	S'   Sr\\\R                  S4      \	S	'   \S
 5       rSrg)SwinMaskedImageModelingOutputo   as  
Swin masked image model outputs.

Args:
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `bool_masked_pos` is provided):
        Masked image modeling (MLM) loss.
    reconstruction (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
        Reconstructed pixel values.
    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reconstruction.r   r   r   c                 P    [         R                  " S[        5        U R                  $ )Nzlogits attribute is deprecated and will be removed in version 5 of Transformers. Please use the reconstruction attribute to retrieve the final output instead.)warningswarnFutureWarningr1   selfs    r(   logits$SwinMaskedImageModelingOutput.logits   s%    ]	

 """r'   r   )r   r   r    r!   r"   r0   r   r#   r$   r%   r1   r   r   r   r   propertyr8   r&   r   r'   r(   r.   r.   o   s    6 )-D(5$$
%,26NHU../6=AM8E%"3"3S"89:A:>Ju00#567>FJHU5+<+<c+A%BCJ# #r'   r.   c                       \ rS rSr% SrSr\\R                     \	S'   Sr
\\R                     \	S'   Sr\\\R                  S4      \	S'   Sr\\\R                  S4      \	S'   Sr\\\R                  S4      \	S	'   S
rg)SwinImageClassifierOutput   a  
Swin 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.
Nr0   r8   .r   r   r   r   )r   r   r    r!   r"   r0   r   r#   r$   r%   r8   r   r   r   r   r&   r   r'   r(   r<   r<      s    6 )-D(5$$
%,*.FHU&&'.=AM8E%"3"3S"89:A:>Ju00#567>FJHU5+<+<c+A%BCJr'   r<   c                     U R                   u  p#pEU R                  X#U-  XU-  X5      n U R                  SSSSSS5      R                  5       R                  SXU5      nU$ )z*
Partitions the given input into windows.
r   r   r            shapeviewpermute
contiguous)input_featurewindow_size
batch_sizeheightwidthnum_channelswindowss          r(   window_partitionrO      so     /<.A.A+J!&&k);8LkM ##Aq!Q15@@BGGKfrsGNr'   c                     U R                   S   nU R                  SX!-  X1-  XU5      n U R                  SSSSSS5      R                  5       R                  SX#U5      n U $ )z7
Merges windows to produce higher resolution features.
rB   r   r   r   r?   r@   rA   rC   )rN   rI   rK   rL   rM   s        r(   window_reverserQ      se     ==$Lll2v4e6JKfrsGooaAq!Q/::<AA"fUabGNr'   c            
          ^  \ rS rSrSrSU 4S jjrS\R                  S\S\S\R                  4S jr	  SS	\
\R                     S
\
\R                     S\S\\R                     4S jjrSrU =r$ )SwinEmbeddings   zO
Construct the patch and position embeddings. Optionally, also the mask token.
c                   > [         TU ]  5         [        U5      U l        U R                  R                  nU R                  R
                  U l        U(       a6  [        R                  " [        R                  " SSUR                  5      5      OS U l        UR                  (       a?  [        R                  " [        R                  " SUS-   UR                  5      5      U l        OS U l        [        R                  " UR                  5      U l        [        R"                  " UR$                  5      U l        UR(                  U l        Xl        g )Nr   )super__init__SwinPatchEmbeddingspatch_embeddingsnum_patches	grid_size
patch_gridr   	Parameterr#   zeros	embed_dim
mask_tokenuse_absolute_embeddingsposition_embeddings	LayerNormnormDropouthidden_dropout_probdropout
patch_sizeconfig)r7   ri   use_mask_tokenrZ   	__class__s       r(   rW   SwinEmbeddings.__init__   s     3F ;++77//99O]",,u{{1a9I9I'JKcg))')||EKK;QR?TZTdTd4e'fD$'+D$LL!1!12	zz&"<"<= ++r'   
embeddingsrK   rL   returnc                    UR                   S   S-
  nU R                  R                   S   S-
  n[        R                  R	                  5       (       d  XE:X  a  X#:X  a  U R                  $ U R                  SS2SS24   nU R                  SS2SS24   nUR                   S   nX R
                  -  n	X0R
                  -  n
[        US-  5      nUR                  SXU5      nUR                  SSSS5      n[        R                  R                  UX4SS	S
9nUR                  SSSS5      R                  SSU5      n[        R                  " Xg4SS9$ )a  
This method allows to interpolate the pre-trained position encodings, to be able to use the model on higher resolution
images. This method is also adapted to support torch.jit tracing.

Adapted from:
- https://github.com/facebookresearch/dino/blob/de9ee3df6cf39fac952ab558447af1fa1365362a/vision_transformer.py#L174-L194, and
- https://github.com/facebookresearch/dinov2/blob/e1277af2ba9496fbadf7aec6eba56e8d882d1e35/dinov2/models/vision_transformer.py#L179-L211
r   NrB         ?r   r   r?   bicubicF)sizemodealign_cornersdim)rD   rb   r#   jit
is_tracingrh   r   reshaperF   r   
functionalinterpolaterE   cat)r7   rm   rK   rL   rZ   num_positionsclass_pos_embedpatch_pos_embedrv   
new_height	new_widthsqrt_num_positionss               r(   interpolate_pos_encoding'SwinEmbeddings.interpolate_pos_encoding   sS    !&&q)A-0066q9A= yy##%%+*F6?+++221bqb59221ab59r".
__,	&}c'9:)11!5G]`a)11!Q1=--33(	 4 
 *11!Q1=BB1b#Nyy/;CCr'   pixel_valuesbool_masked_posr   c                    UR                   u  pEpgU R                  U5      u  pU R                  U5      nUR                  5       u  pnUbI  U R                  R                  XS5      nUR                  S5      R                  U5      nUSU-
  -  X-  -   nU R                  b*  U(       a  XR                  XU5      -   nOXR                  -   nU R                  U5      nX4$ )NrB         ?)rD   rY   rd   rr   r`   expand	unsqueezetype_asrb   r   rg   )r7   r   r   r   _rM   rK   rL   rm   output_dimensionsrJ   seq_lenmask_tokensmasks                 r(   forwardSwinEmbeddings.forward  s     *6););&(,(=(=l(K%
YYz*
!+!2
Q&//00bIK",,R088ED#sTz2[5GGJ##/''*G*G
\a*bb
'*B*BB
\\*-
,,r'   )ri   rg   r`   rd   rY   r\   rh   rb   )F)NF)r   r   r    r!   r"   rW   r#   Tensorintr   r   r$   
BoolTensorboolr   r   r&   __classcell__rk   s   @r(   rS   rS      s    &&D5<< &D &DUX &D]b]i]i &DV 7;).	-u001- "%"2"23- #'	-
 
u||	- -r'   rS   c                      ^  \ rS rSrSrU 4S jrS rS\\R                     S\
\R                  \
\   4   4S jrSrU =r$ )	rX   i2  z
This class turns `pixel_values` of shape `(batch_size, num_channels, height, width)` into the initial
`hidden_states` (patch embeddings) of shape `(batch_size, seq_length, hidden_size)` to be consumed by a
Transformer.
c                   > [         TU ]  5         UR                  UR                  p2UR                  UR
                  pT[        U[        R                  R                  5      (       a  UOX"4n[        U[        R                  R                  5      (       a  UOX34nUS   US   -  US   US   -  -  nX l        X0l        X@l        X`l
        US   US   -  US   US   -  4U l        [        R                  " XEX3S9U l        g )Nr   r   )kernel_sizestride)rV   rW   
image_sizerh   rM   r_   
isinstancecollectionsabcIterablerZ   r[   r   Conv2d
projection)r7   ri   r   rh   rM   hidden_sizerZ   rk   s          r(   rW   SwinPatchEmbeddings.__init__9  s    !'!2!2F4E4EJ$*$7$79I9Ik#-j+//:R:R#S#SZZdYq
#-j+//:R:R#S#SZZdYq
!!}
15*Q-:VW=:XY$$(&$Q-:a=8*Q-:VW=:XY))L:ir'   c                 f   X0R                   S   -  S:w  aB  SU R                   S   X0R                   S   -  -
  4n[        R                  R                  X5      nX R                   S   -  S:w  aD  SSSU R                   S   X R                   S   -  -
  4n[        R                  R                  X5      nU$ )Nr   r   )rh   r   rz   pad)r7   r   rK   rL   
pad_valuess        r(   	maybe_padSwinPatchEmbeddings.maybe_padH  s    ??1%%*T__Q/%//!:L2LLMJ==,,\FLOOA&&!+Q4??1#5QRAS8S#STJ==,,\FLr'   r   rn   c                     UR                   u  p#pEU R                  XU5      nU R                  U5      nUR                   u    p$nXE4nUR                  S5      R	                  SS5      nXg4$ )Nr?   r   )rD   r   r   flatten	transpose)r7   r   r   rM   rK   rL   rm   r   s           r(   r   SwinPatchEmbeddings.forwardQ  sp    )5););&~~lEB__\2
(..1e#O''*44Q:
,,r'   )r[   r   rM   rZ   rh   r   )r   r   r    r!   r"   rW   r   r   r#   r$   r   r   r   r   r&   r   r   s   @r(   rX   rX   2  sK    j	-HU->->$? 	-E%,,X]^aXbJbDc 	- 	-r'   rX   c            	          ^  \ rS rSrSr\R                  4S\\   S\S\R                  SS4U 4S jjjr
S	 rS
\R                  S\\\4   S\R                  4S jrSrU =r$ )SwinPatchMergingi]  a  
Patch Merging Layer.

Args:
    input_resolution (`Tuple[int]`):
        Resolution of input feature.
    dim (`int`):
        Number of input channels.
    norm_layer (`nn.Module`, *optional*, defaults to `nn.LayerNorm`):
        Normalization layer class.
input_resolutionrv   
norm_layerrn   Nc                    > [         TU ]  5         Xl        X l        [        R
                  " SU-  SU-  SS9U l        U" SU-  5      U l        g )Nr@   r?   Fbias)rV   rW   r   rv   r   Linear	reductionrd   )r7   r   rv   r   rk   s       r(   rW   SwinPatchMerging.__init__j  sE     01s7AG%@q3w'	r'   c                     US-  S:H  =(       d    US-  S:H  nU(       a-  SSSUS-  SUS-  4n[         R                  R                  X5      nU$ )Nr?   r   r   )r   rz   r   )r7   rH   rK   rL   
should_padr   s         r(   r   SwinPatchMerging.maybe_padq  sS    qjAo:519>
Q519a!<JMM--mHMr'   rH   input_dimensionsc                    Uu  p4UR                   u  pVnUR                  XSXG5      nU R                  XU5      nUS S 2SS S2SS S2S S 24   nUS S 2SS S2SS S2S S 24   n	US S 2SS S2SS S2S S 24   n
US S 2SS S2SS S2S S 24   n[        R                  " XX/S5      nUR                  USSU-  5      nU R                  U5      nU R                  U5      nU$ )Nr   r?   r   rB   r@   )rD   rE   r   r#   r|   rd   r   )r7   rH   r   rK   rL   rJ   rv   rM   input_feature_0input_feature_1input_feature_2input_feature_3s               r(   r   SwinPatchMerging.forwardy  s   ((5(;(;%
%**:uS}eD'14a4Aq(89'14a4Aq(89'14a4Aq(89'14a4Aq(89		?_"fhjk%**:r1|;KL		-0}5r'   )rv   r   rd   r   )r   r   r    r!   r"   r   rc   r   r   ModulerW   r   r#   r   r   r&   r   r   s   @r(   r   r   ]  s|    
 XZWcWc (s (# (299 (hl ( (U\\ U3PS8_ Y^YeYe  r'   r   input	drop_probtrainingrn   c                    US:X  d  U(       d  U $ SU-
  nU R                   S   4SU R                  S-
  -  -   nU[        R                  " X@R                  U R
                  S9-   nUR                  5         U R                  U5      U-  nU$ )a*  
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)rD   ndimr#   randr   r   floor_div)r   r   r   	keep_probrD   random_tensoroutputs          r(   	drop_pathr     s     CxII[[^

Q 77E

5ELL YYMYYy!M1FMr'   c                      ^  \ rS rSrSrSS\\   SS4U 4S jjjrS\R                  S\R                  4S jr
S\4S	 jrS
rU =r$ )SwinDropPathi  zXDrop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).Nr   rn   c                 .   > [         TU ]  5         Xl        g N)rV   rW   r   )r7   r   rk   s     r(   rW   SwinDropPath.__init__  s    "r'   r   c                 B    [        XR                  U R                  5      $ r   )r   r   r   r7   r   s     r(   r   SwinDropPath.forward  s    FFr'   c                 8    SR                  U R                  5      $ )Nzp={})formatr   r6   s    r(   
extra_reprSwinDropPath.extra_repr  s    }}T^^,,r'   )r   r   )r   r   r    r!   r"   r   floatrW   r#   r   r   strr   r&   r   r   s   @r(   r   r     sQ    b#(5/ #T # #GU\\ Gell G-C - -r'   r   c                      ^  \ rS rSrU 4S jrS r   SS\R                  S\\R                     S\\R                     S\\
   S\\R                     4
S	 jjrS
rU =r$ )SwinSelfAttentioni  c                 
  > [         TU ]  5         X#-  S:w  a  [        SU SU S35      eX0l        [	        X#-  5      U l        U R                  U R
                  -  U l        [        U[        R                  R                  5      (       a  UOXD4U l        [        R                  " [        R                  " SU R                  S   -  S-
  SU R                  S   -  S-
  -  U5      5      U l        [        R"                  " U R                  S   5      n[        R"                  " U R                  S   5      n[        R$                  " ['        XV/SS95      n[        R(                  " US5      nUS S 2S S 2S 4   US S 2S S S 24   -
  n	U	R+                  SSS5      R-                  5       n	U	S S 2S S 2S4==   U R                  S   S-
  -  ss'   U	S S 2S S 2S4==   U R                  S   S-
  -  ss'   U	S S 2S S 2S4==   SU R                  S   -  S-
  -  ss'   U	R/                  S	5      n
U R1                  S
U
5        [        R2                  " U R                  U R                  UR4                  S9U l        [        R2                  " U R                  U R                  UR4                  S9U l        [        R2                  " U R                  U R                  UR4                  S9U l        [        R<                  " UR>                  5      U l         g )Nr   zThe hidden size (z6) is not a multiple of the number of attention heads ()r?   r   ij)indexingrB   relative_position_indexr   )!rV   rW   
ValueErrornum_attention_headsr   attention_head_sizeall_head_sizer   r   r   r   rI   r   r]   r#   r^   relative_position_bias_tablearangestackr   r   rF   rG   sumregister_bufferr   qkv_biasquerykeyvaluere   attention_probs_dropout_probrg   )r7   ri   rv   	num_headsrI   coords_hcoords_wcoordscoords_flattenrelative_coordsr   rk   s              r(   rW   SwinSelfAttention.__init__  s   ?a#C5(^_h^iijk  $- #&s#7 !558P8PP%k;??3K3KLLKS^Rl 	 -/LLKKT--a0014T=M=Ma=P9PST9TUW`a-
)
 << 0 0 34<< 0 0 34Xx&:TJKvq1(At4~aqj7QQ)11!Q:EEG1a D$4$4Q$7!$;; 1a D$4$4Q$7!$;; 1a A(8(8(;$;a$?? "1"5"5b"968OPYYt1143E3EFOO\
99T//1C1C&//ZYYt1143E3EFOO\
zz&"E"EFr'   c                     UR                  5       S S U R                  U R                  4-   nUR                  U5      nUR	                  SSSS5      $ )NrB   r   r?   r   r   )rr   r   r   rE   rF   )r7   xnew_x_shapes      r(   transpose_for_scores&SwinSelfAttention.transpose_for_scores  sL    ffhsmt'?'?AYAY&ZZFF;yyAq!$$r'   r   attention_mask	head_maskoutput_attentionsrn   c                    UR                   u  pVnU R                  U5      nU R                  U R                  U5      5      n	U R                  U R	                  U5      5      n
U R                  U5      n[
        R                  " XR                  SS5      5      nU[        R                  " U R                  5      -  nU R                  U R                  R                  S5         nUR                  U R                  S   U R                  S   -  U R                  S   U R                  S   -  S5      nUR                  SSS5      R!                  5       nXR#                  S5      -   nUbm  UR                   S   nUR                  X^-  XR$                  Xf5      nXR#                  S5      R#                  S5      -   nUR                  SU R$                  Xf5      n[&        R(                  R+                  USS9nU R-                  U5      nUb  X-  n[
        R                  " X5      nUR                  SSSS5      R!                  5       nUR/                  5       S S U R0                  4-   nUR                  U5      nU(       a  UU4nU$ U4nU$ )NrB   r   r   r?   ru   r   )rD   r   r  r   r   r#   matmulr   mathsqrtr   r   r   rE   rI   rF   rG   r   r   r   rz   softmaxrg   rr   r   )r7   r   r  r  r  rJ   rv   rM   mixed_query_layer	key_layervalue_layerquery_layerattention_scoresrelative_position_bias
mask_shapeattention_probscontext_layernew_context_layer_shapeoutputss                      r(   r   SwinSelfAttention.forward  s{    )6(;(;%
 JJ}5--dhh}.EF	//

=0IJ//0AB !<<5H5HR5PQ+dii8P8P.QQ!%!B!B4C_C_CdCdegCh!i!7!<!<Q$"2"21"55t7G7G7JTM]M]^_M`7`bd"
 "8!?!?1a!H!S!S!U+.N.Nq.QQ%'--a0J/44(*6N6NPS   02J2J12M2W2WXY2ZZ/44R9Q9QSV\ --//0@b/I ,,7  -9O_B%--aAq9DDF"/"4"4"6s";t?Q?Q>S"S%**+BC6G=/2 O\M]r'   )	r   r   rg   r   r   r   r   r   rI   NNF)r   r   r    r!   rW   r  r#   r   r   r$   r   r   r   r&   r   r   s   @r(   r   r     s{    #GJ% 7;15,16||6 !!2!236 E--.	6
 $D>6 
u||	6 6r'   r   c                   z   ^  \ rS rSrU 4S jrS\R                  S\R                  S\R                  4S jrSrU =r	$ )SwinSelfOutputi  c                    > [         TU ]  5         [        R                  " X"5      U l        [        R
                  " UR                  5      U l        g r   )rV   rW   r   r   densere   r   rg   r7   ri   rv   rk   s      r(   rW   SwinSelfOutput.__init__  s4    YYs(
zz&"E"EFr'   r   input_tensorrn   c                 J    U R                  U5      nU R                  U5      nU$ r   r  rg   )r7   r   r  s      r(   r   SwinSelfOutput.forward!  s$    

=1]3r'   r!  
r   r   r    r!   rW   r#   r   r   r&   r   r   s   @r(   r  r    s7    G
U\\  RWR^R^  r'   r  c                      ^  \ rS rSrU 4S jrS r   SS\R                  S\\R                     S\\R                     S\\
   S\\R                     4
S	 jjrS
rU =r$ )SwinAttentioni(  c                    > [         TU ]  5         [        XX45      U l        [	        X5      U l        [        5       U l        g r   )rV   rW   r   r7   r  r   setpruned_heads)r7   ri   rv   r   rI   rk   s        r(   rW   SwinAttention.__init__)  s2    %f9J	$V1Er'   c                 6   [        U5      S:X  a  g [        XR                  R                  U R                  R                  U R
                  5      u  p[        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l	        [        U R                  R                  USS9U R                  l        U R                  R                  [        U5      -
  U R                  l        U R                  R                  U R                  R                  -  U R                  l        U R
                  R                  U5      U l        g )Nr   r   ru   )lenr   r7   r   r   r(  r   r   r   r   r   r  r   union)r7   headsindexs      r(   prune_headsSwinAttention.prune_heads/  s   u:?79900$))2O2OQUQbQb

 -TYY__eD		*499==%@		,TYY__eD		.t{{/@/@%QO )-		(E(EE
(R		%"&))"?"?$))B_B_"_		 --33E:r'   r   r  r  r  rn   c                 f    U R                  XX45      nU R                  US   U5      nU4USS  -   nU$ )Nr   r   )r7   r   )r7   r   r  r  r  self_outputsattention_outputr  s           r(   r   SwinAttention.forwardA  sB     yy	];;|AF#%QR(88r'   )r   r(  r7   r  )r   r   r    r!   rW   r/  r#   r   r   r$   r   r   r   r&   r   r   s   @r(   r%  r%  (  sy    ";* 7;15,1
||
 !!2!23
 E--.	

 $D>
 
u||	
 
r'   r%  c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )SwinIntermediateiN  c                   > [         TU ]  5         [        R                  " U[	        UR
                  U-  5      5      U l        [        UR                  [        5      (       a  [        UR                     U l        g UR                  U l        g r   )rV   rW   r   r   r   	mlp_ratior  r   
hidden_actr   r	   intermediate_act_fnr  s      r(   rW   SwinIntermediate.__init__O  sd    YYsC(8(83(>$?@
f''--'-f.?.?'@D$'-'8'8D$r'   r   rn   c                 J    U R                  U5      nU R                  U5      nU$ r   r  r:  r   s     r(   r   SwinIntermediate.forwardW  s&    

=100?r'   r=  r#  r   s   @r(   r6  r6  N  s(    9U\\ ell  r'   r6  c                   b   ^  \ rS rSrU 4S jrS\R                  S\R                  4S jrSrU =r	$ )
SwinOutputi]  c                    > [         TU ]  5         [        R                  " [	        UR
                  U-  5      U5      U l        [        R                  " UR                  5      U l	        g r   )
rV   rW   r   r   r   r8  r  re   rf   rg   r  s      r(   rW   SwinOutput.__init__^  sF    YYs6#3#3c#9:C@
zz&"<"<=r'   r   rn   c                 J    U R                  U5      nU R                  U5      nU$ r   r!  r   s     r(   r   SwinOutput.forwardc  s$    

=1]3r'   r!  r#  r   s   @r(   r@  r@  ]  s(    >
U\\ ell  r'   r@  c                      ^  \ rS rSrSU 4S jjrS rS rS r   SS\R                  S\
\\4   S\\R                     S	\\   S
\\   S\
\R                  \R                  4   4S jjrSrU =r$ )	SwinLayerii  c                   > [         TU ]  5         UR                  U l        X`l        UR                  U l        X0l        [        R                  " X!R                  S9U l	        [        XX@R                  S9U l        US:  a  [        U5      O[        R                  " 5       U l        [        R                  " X!R                  S9U l        [!        X5      U l        [%        X5      U l        g )Neps)rI   r   )rV   rW   chunk_size_feed_forward
shift_sizerI   r   r   rc   layer_norm_epslayernorm_beforer%  	attentionr   Identityr   layernorm_afterr6  intermediater@  r   )r7   ri   rv   r   r   drop_path_raterK  rk   s          r(   rW   SwinLayer.__init__j  s    '-'E'E$$!-- 0 "S6K6K L&vIK[K[\9G#9Mn5SUS^S^S`!||C5J5JK,V9 -r'   c                    [        U5      U R                  ::  an  [        S5      U l        [        R
                  R                  5       (       a*  [        R                   " [        R                  " U5      5      O
[        U5      U l        g g Nr   )minrI   r   rK  r#   rw   rx   tensor)r7   r   s     r(   set_shift_and_window_size#SwinLayer.set_shift_and_window_sizew  s_     D$4$44'lDO=BYY=Q=Q=S=S		%,,'789Y\]mYn  5r'   c           	         U R                   S:  Gaw  [        R                  " SXS4X4S9n[        SU R                  * 5      [        U R                  * U R                   * 5      [        U R                   * S 5      4n[        SU R                  * 5      [        U R                  * U R                   * 5      [        U R                   * S 5      4nSnU H  n	U H  n
XS S 2XS S 24'   US-  nM     M     [        XPR                  5      nUR                  SU R                  U R                  -  5      nUR                  S5      UR                  S5      -
  nUR                  US:g  [        S5      5      R                  US:H  [        S5      5      nU$ S nU$ )Nr   r   r   rB   r?   g      Yr   )
rK  r#   r^   slicerI   rO   rE   r   masked_fillr   )r7   rK   rL   r   r   img_maskheight_sliceswidth_slicescountheight_slicewidth_slicemask_windows	attn_masks                r(   get_attn_maskSwinLayer.get_attn_mask  s   ??Q{{Ava#8UHa$***+t'''$//)9:t&-M a$***+t'''$//)9:t&-L
 E -#/K@EQ1<=QJE $0 !.
 ,H6F6FGL',,R1A1ADDTDT1TUL$..q1L4J4J14MMI!--i1neFmLXXYbfgYginoristI  Ir'   c                     U R                   X0R                   -  -
  U R                   -  nU R                   X R                   -  -
  U R                   -  nSSSUSU4n[        R                  R                  X5      nX4$ rU  )rI   r   rz   r   )r7   r   rK   rL   	pad_right
pad_bottomr   s          r(   r   SwinLayer.maybe_pad  sy    %%0@0@(@@DDTDTT	&&2B2B)BBdFVFVV
Ay!Z8
))-D((r'   r   r   r  r  always_partitionrn   c                    U(       d  U R                  U5        O Uu  pgUR                  5       u  pn
UnU R                  U5      nUR                  XXz5      nU R	                  XU5      u  pUR
                  u  ppU R                  S:  a.  [        R                  " XR                  * U R                  * 4SS9nOUn[        XR                  5      nUR                  SU R                  U R                  -  U
5      nU R                  XUR                  UR                  S9nU R                  UUX4S9nUS   nUR                  SU R                  U R                  U
5      n[        UU R                  X5      nU R                  S:  a-  [        R                  " UU R                  U R                  4SS9nOUnUS   S:  =(       d    US   S:  nU(       a  US S 2S U2S U2S S 24   R!                  5       nUR                  XU-  U
5      nXR#                  U5      -   nU R%                  U5      nU R'                  U5      nXR)                  U5      -   nU(       a	  UUS	   4nU$ U4nU$ )
Nr   )r   r?   )shiftsdimsrB   r   )r  r   rA   r   )rX  rr   rM  rE   r   rD   rK  r#   rollrO   rI   re  r   r   rN  rQ   rG   r   rP  rQ  r   )r7   r   r   r  r  rk  rK   rL   rJ   r   channelsshortcutr   
height_pad	width_padshifted_hidden_stateshidden_states_windowsrd  attention_outputsr3  attention_windowsshifted_windows
was_paddedlayer_outputlayer_outputss                            r(   r   SwinLayer.forward  s     **+;<("/"4"4"6
x --m<%**:uO %)NN=%$P!&3&9&9#y??Q$)JJ}FVY]YhYhXhEipv$w!$1! !11FHXHX Y 5 : :2t?O?ORVRbRb?bdl m&&)<)<EZEaEa ' 
	 !NN!9i + 
 -Q/,11"d6F6FHXHXZbc():D<L<Ljd ??Q %

?DOOUYUdUdCelr s /]Q&;*Q-!*;
 1!WfWfufa2G H S S U-22:~xX >>2C#DD++M:((6${{<'@@@Q'8';< YeWfr'   )
rN  rJ  r   r   rQ  rP  rM  r   rK  rI   )r   r   NFF)r   r   r    r!   rW   rX  re  r   r#   r   r   r   r   r$   r   r   r&   r   r   s   @r(   rF  rF  i  s    .8) 26,1+0A||A  S/A E--.	A
 $D>A #4.A 
u||U\\)	*A Ar'   rF  c                      ^  \ rS rSrU 4S jr   SS\R                  S\\\4   S\	\R                     S\	\   S\	\   S\\R                     4S	 jjrS
rU =r$ )	SwinStagei  c                 R  > [         T	U ]  5         Xl        X l        [        R
                  " [        U5       Vs/ s H+  n[        UUUUXh   US-  S:X  a  SOUR                  S-  S9PM-     sn5      U l	        Ub  U" X2[        R                  S9U l        OS U l        SU l        g s  snf )Nr?   r   )ri   rv   r   r   rR  rK  )rv   r   F)rV   rW   ri   rv   r   
ModuleListrangerF  rI   blocksrc   
downsamplepointing)
r7   ri   rv   r   depthr   r   r  irk   s
            r(   rW   SwinStage.__init__  s    mm u
 &A !%5'#,<%&UaZqf6H6HA6M &

 !()9r||\DO"DO'
s   2B$r   r   r  r  rk  rn   c                    Uu  pg[        U R                  5       H  u  pUb  X8   OS n
U	" XXU5      nUS   nM     UnU R                  b%  US-   S-  US-   S-  pXgX4nU R                  X5      nOXgXg4nXU4nU(       a  UWSS  -  nU$ )Nr   r   r?   )	enumerater  r  )r7   r   r   r  r  rk  rK   rL   r  layer_modulelayer_head_maskr{  !hidden_states_before_downsamplingheight_downsampledwidth_downsampledr   stage_outputss                    r(   r   SwinStage.forward  s     )(5OA.7.CilO(UeM *!,M  6 -:)??&5;aZA4EPQ	VWGW 1!'0B V OO,M`M!' >&K\]]12..Mr'   )r  ri   rv   r  r  r}  )r   r   r    r!   rW   r#   r   r   r   r   r$   r   r   r&   r   r   s   @r(   r  r    s    < 26,1+0||  S/ E--.	
 $D> #4. 
u||	 r'   r  c                      ^  \ rS rSrU 4S jr      SS\R                  S\\\4   S\	\R                     S\	\   S\	\   S\	\   S	\	\   S
\	\   S\\\4   4S jjrSrU =r$ )SwinEncoderi"  c                   > [         TU ]  5         [        UR                  5      U l        Xl        [        R                  " SUR                  [        UR                  5      SS9 Vs/ s H  o3R                  5       PM     nn[        R                  " [        U R                  5       Vs/ s H  n[        U[        UR                   SU-  -  5      US   SU-  -  US   SU-  -  4UR                  U   UR"                  U   U[        UR                  S U 5      [        UR                  S US-    5       XPR                  S-
  :  a  [$        OS S9PM     sn5      U l        SU l        g s  snf s  snf )Nr   cpu)r   r?   r   )ri   rv   r   r  r   r   r  F)rV   rW   r+  depths
num_layersri   r#   linspacerR  r   itemr   r  r  r  r   r_   r   r   layersgradient_checkpointing)r7   ri   r[   r   dpri_layerrk   s         r(   rW   SwinEncoder.__init__#  sQ   fmm,!&63H3H#fmmJ\ej!kl!kAvvx!klmm  %T__5  6G !F,,q'z9:&/lq'z&BIaLUVX_U_D`%a --0$..w7!#fmmHW&=">V]]S`U\_`U`EaAbc4;ooPQ>Q4Q/X\  6
 ',#! ms   &E&(B*E+r   r   r  r  output_hidden_states(output_hidden_states_before_downsamplingrk  return_dictrn   c	           	         U(       a  SOS n	U(       a  SOS n
U(       a  SOS nU(       aB  UR                   u  pnUR                  " U/UQUP76 nUR                  SSSS5      nX4-  n	X4-  n
[        U R                  5       GH3  u  nnUb  UU   OS nU R
                  (       a2  U R                  (       a!  U R                  UR                  UUUUU5      nO
U" XUXG5      nUS   nUS   nUS   nUS   US   4nU(       aS  U(       aL  UR                   u  pnUR                  " U/US   US   4QUP76 nUR                  SSSS5      nU	U4-  n	X4-  n
OPU(       aI  U(       dB  UR                   u  pnUR                  " U/UQUP76 nUR                  SSSS5      nX4-  n	X4-  n
U(       d  GM+  UUSS  -  nGM6     U(       d  [        S XU4 5       5      $ [        UU	UU
S	9$ )
Nr   r   r   r   r?   r  rB   c              3   .   #    U  H  oc  M  Uv   M     g 7fr   r   ).0vs     r(   	<genexpr>&SwinEncoder.forward.<locals>.<genexpr>}  s     m$[q$[s   	)r   r   r   r   )rD   rE   rF   r  r  r  r   _gradient_checkpointing_func__call__tupler   )r7   r   r   r  r  r  r  rk  r  all_hidden_statesall_reshaped_hidden_statesall_self_attentionsrJ   r   r   reshaped_hidden_stater  r  r  r{  r  r   s                         r(   r   SwinEncoder.forward9  s]    #7BD+?RT"$5b4)6)<)<&J;$1$6$6z$bDT$bVa$b!$9$A$A!Q1$M!!11&*BB&(5OA|.7.CilO**t}} $ A A ))!$#%$! !-!_FW! *!,M0=a0@- -a 0 1" 57H7LM#(P-N-T-T*
{ )J(N(N)"3A"68I!8L!M)OZ)% )>(E(EaAq(Q%!&G%II!*.FF*%.V-:-@-@*
{(5(:(::(fHX(fZe(f%(=(E(EaAq(Q%!%55!*.FF*  #}QR'88#U  6X m]GZ$[mmm ++*#=	
 	
r'   )ri   r  r  r  )NFFFFT)r   r   r    r!   rW   r#   r   r   r   r   r$   r   r   r   r   r&   r   r   s   @r(   r  r  "  s    ,4 26,1/4CH+0&*K
||K
  S/K
 E--.	K

 $D>K
 'tnK
 3;4.K
 #4.K
 d^K
 
u''	(K
 K
r'   r  c                   0    \ rS rSr\rSrSrSrS/r	S r
Srg)	SwinPreTrainedModeli  swinr   Tr  c                 p   [        U[        R                  [        R                  45      (       ak  UR                  R
                  R                  SU R                  R                  S9  UR                  b%  UR                  R
                  R                  5         gg[        U[        R                  5      (       aJ  UR                  R
                  R                  5         UR                  R
                  R                  S5        g[        U[        5      (       ad  UR                  b$  UR                  R
                  R                  5         UR                  b%  UR                  R
                  R                  5         gg[        U[         5      (       a%  UR"                  R
                  R                  5         gg)zInitialize the weightsr   )meanstdNr   )r   r   r   r   weightdatanormal_ri   initializer_ranger   zero_rc   fill_rS   r`   rb   r   r   )r7   modules     r(   _init_weights!SwinPreTrainedModel._init_weights  s.   fryy"))455 MM&&CT[[5R5R&S{{&  &&( '--KK""$MM$$S)//  ,!!&&,,.))5**//557 6 122//44::< 3r'   r   N)r   r   r    r!   r   config_classbase_model_prefixmain_input_namesupports_gradient_checkpointing_no_split_modulesr  r&   r   r'   r(   r  r    s%    L$O&*#$=r'   r  c                      ^  \ rS rSrSU 4S jjrS rS r\       SS\\	R                     S\\	R                     S\\	R                     S\\   S	\\   S
\S\\   S\\\4   4S jj5       rSrU =r$ )	SwinModeli  c                   > [         TU ]  U5        Xl        [        UR                  5      U l        [        UR                  SU R
                  S-
  -  -  5      U l        [        XS9U l
        [        XR                  R                  5      U l        [        R                  " U R                  UR                   S9U l        U(       a  [        R$                  " S5      OSU l        U R)                  5         g)z
add_pooling_layer (`bool`, *optional*, defaults to `True`):
    Whether or not to apply pooling layer.
use_mask_token (`bool`, *optional*, defaults to `False`):
    Whether or not to create and apply mask tokens in the embedding layer.
r?   r   )rj   rH  N)rV   rW   ri   r+  r  r  r   r_   num_featuresrS   rm   r  r\   encoderr   rc   rL  	layernormAdaptiveAvgPool1dpooler	post_init)r7   ri   add_pooling_layerrj   rk   s       r(   rW   SwinModel.__init__  s     	 fmm, 0 0119L3M MN(O"6??+E+EFd&7&7V=R=RS1Bb**1- 	r'   c                 .    U R                   R                  $ r   rm   rY   r6   s    r(   get_input_embeddingsSwinModel.get_input_embeddings      ///r'   c                     UR                  5        H7  u  p#U R                  R                  U   R                  R	                  U5        M9     g)z
Prunes heads of the model. heads_to_prune: dict of {layer_num: list of heads to prune in this layer} See base
class PreTrainedModel
N)itemsr  layerrN  r/  )r7   heads_to_pruner  r-  s       r(   _prune_headsSwinModel._prune_heads  s<    
 +002LELLu%//;;EB 3r'   r   r   r  r  r  r   r  rn   c           	         Ub  UOU R                   R                  nUb  UOU R                   R                  nUb  UOU R                   R                  nUc  [	        S5      eU R                  U[        U R                   R                  5      5      nU R                  XUS9u  pU R                  UU	UUUUS9n
U
S   nU R                  U5      nSnU R                  b8  U R                  UR                  SS5      5      n[        R                  " US5      nU(       d  X4U
SS -   nU$ [        UUU
R                   U
R"                  U
R$                  S9$ )	z
bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`, *optional*):
    Boolean masked positions. Indicates which patches are masked (1) and which aren't (0).
Nz You have to specify pixel_values)r   r   )r  r  r  r  r   r   r?   )r   r,   r   r   r   )ri   r  r  use_return_dictr   get_head_maskr+  r  rm   r  r  r  r   r#   r   r*   r   r   r   )r7   r   r   r  r  r  r   r  embedding_outputr   encoder_outputssequence_outputpooled_outputr   s                 r(   r   SwinModel.forward  si    2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B]?@@ &&y#dkk6H6H2IJ	-1__Tl .= .
* ,,/!5# ' 
 *!,..9;;" KK(A(A!Q(GHM!MM-;M%58KKFM-')77&11#2#I#I
 	
r'   )ri   rm   r  r  r  r  r  )TFNNNNNFN)r   r   r    r!   rW   r  r  r   r   r#   r$   r   r   r   r   r*   r   r&   r   r   s   @r(   r  r    s    *0C  596:15,0/3).&*>
u001>
 "%"2"23>
 E--.	>

 $D>>
 'tn>
 #'>
 d^>
 
uo%	&>
 >
r'   r  a\  
    Swin Model with a decoder on top for masked image modeling, as proposed in [SimMIM](https://arxiv.org/abs/2111.09886).

    <Tip>

    Note that we provide a script to pre-train this model on custom data in our [examples
    directory](https://github.com/huggingface/transformers/tree/main/examples/pytorch/image-pretraining).

    </Tip>
    )custom_introc                      ^  \ rS rSrU 4S jr\       SS\\R                     S\\R                     S\\R                     S\\
   S\\
   S\
S	\\
   S
\\\4   4S jj5       rSrU =r$ )SwinForMaskedImageModelingi  c                   > [         TU ]  U5        [        USSS9U l        [	        UR
                  SUR                  S-
  -  -  5      n[        R                  " [        R                  " X!R                  S-  UR                  -  SS9[        R                  " UR                  5      5      U l        U R                  5         g )NFT)r  rj   r?   r   )in_channelsout_channelsr   )rV   rW   r  r  r   r_   r  r   
Sequentialr   encoder_striderM   PixelShuffledecoderr  )r7   ri   r  rk   s      r(   rW   #SwinForMaskedImageModeling.__init__  s     fdS	6++aF4E4E4I.JJK}}II(7L7La7ORXReRe7est OOF112	
 	r'   r   r   r  r  r  r   r  rn   c           
         Ub  UOU R                   R                  nU R                  UUUUUUUS9nUS   n	U	R                  SS5      n	U	R                  u  pn[
        R                  " US-  5      =pU	R                  XX5      n	U R                  U	5      nSnUGb  U R                   R                  U R                   R                  -  nUR                  SUU5      nUR                  U R                   R                  S5      R                  U R                   R                  S5      R                  S5      R                  5       n[        R                  R!                  XSS	9nUU-  R#                  5       UR#                  5       S
-   -  U R                   R$                  -  nU(       d  U4USS -   nUb  U4U-   $ U$ ['        UUUR(                  UR*                  UR,                  S9$ )a  
bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`):
    Boolean masked positions. Indicates which patches are masked (1) and which aren't (0).

Examples:
```python
>>> from transformers import AutoImageProcessor, SwinForMaskedImageModeling
>>> 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)

>>> image_processor = AutoImageProcessor.from_pretrained("microsoft/swin-base-simmim-window6-192")
>>> model = SwinForMaskedImageModeling.from_pretrained("microsoft/swin-base-simmim-window6-192")

>>> num_patches = (model.config.image_size // model.config.patch_size) ** 2
>>> pixel_values = image_processor(images=image, return_tensors="pt").pixel_values
>>> # create random boolean mask of shape (batch_size, num_patches)
>>> bool_masked_pos = torch.randint(low=0, high=2, size=(1, num_patches)).bool()

>>> outputs = model(pixel_values, bool_masked_pos=bool_masked_pos)
>>> loss, reconstructed_pixel_values = outputs.loss, outputs.reconstruction
>>> list(reconstructed_pixel_values.shape)
[1, 3, 192, 192]
```N)r   r  r  r  r   r  r   r   r?   rp   rB   none)r   gh㈵>)r0   r1   r   r   r   )ri   r  r  r   rD   r	  floorry   r  r   rh   repeat_interleaver   rG   r   rz   l1_lossr   rM   r.   r   r   r   )r7   r   r   r  r  r  r   r  r  r  rJ   rM   sequence_lengthrK   rL   reconstructed_pixel_valuesmasked_im_lossrr   r   reconstruction_lossr   s                        r(   r   "SwinForMaskedImageModeling.forward$  s   L &1%<k$++B]B]))+/!5%=#  
 "!*)33Aq94C4I4I1
/OS$899)11*FZ &*\\/%B"&;;))T[[-C-CCD-55b$EO11$++2H2H!L""4;;#9#91=1	  #%--"7"7lr"7"s1D8==?488:PTCTUX\XcXcXpXppN02WQR[@F3A3M^%.YSYY,5!//))#*#A#A
 	
r'   )r  r  r  )r   r   r    r!   rW   r   r   r#   r$   r   r   r   r   r.   r   r&   r   r   s   @r(   r  r    s       596:15,0/3).&*R
u001R
 "%"2"23R
 E--.	R

 $D>R
 'tnR
 #'R
 d^R
 
u33	4R
 R
r'   r  a  
    Swin 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.

    <Tip>

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

    </Tip>
    c                      ^  \ rS rSrU 4S jr\       SS\\R                     S\\R                     S\\R                     S\\
   S\\
   S\
S	\\
   S
\\\4   4S jj5       rSrU =r$ )SwinForImageClassificationiz  c                 D  > [         TU ]  U5        UR                  U l        [        U5      U l        UR                  S:  a5  [
        R                  " U R                  R                  UR                  5      O[
        R                  " 5       U l	        U R                  5         g rU  )rV   rW   
num_labelsr  r  r   r   r  rO  
classifierr  )r7   ri   rk   s     r(   rW   #SwinForImageClassification.__init__  sx      ++f%	 EKDUDUXYDYBIIdii,,f.?.?@_a_j_j_l 	
 	r'   r   r  labelsr  r  r   r  rn   c           	      V   Ub  UOU R                   R                  nU R                  UUUUUUS9nUS   n	U R                  U	5      n
SnUb  U R	                  XXR                   S9nU(       d  U
4USS -   nUb  U4U-   $ U$ [        UU
UR                  UR                  UR                  S9$ )ab  
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
    Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
    config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
    `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
N)r  r  r  r   r  r   )r8   r  pooled_logitsri   r?   )r0   r8   r   r   r   )	ri   r  r  r  loss_functionr<   r   r   r   )r7   r   r  r  r  r  r   r  r  r  r8   r0   r   s                r(   r   "SwinForImageClassification.forward  s    " &1%<k$++B]B]))/!5%=#  
  
/%%VRXalal%mDY,F)-)9TGf$EvE(!//))#*#A#A
 	
r'   )r  r  r  r  )r   r   r    r!   rW   r   r   r#   r$   
LongTensorr   r   r   r<   r   r&   r   r   s   @r(   r  r  z  s      5915-1,0/3).&*-
u001-
 E--.-
 ))*	-

 $D>-
 'tn-
 #'-
 d^-
 
u//	0-
 -
r'   r  zM
    Swin backbone, to be used with frameworks like DETR and MaskFormer.
    c                      ^  \ rS rSrS\4U 4S jjrS r   SS\R                  S\	\
   S\	\
   S\	\
   S	\4
S
 jjrSrU =r$ )SwinBackbonei  ri   c           	      F  > [         TU ]  U5        [         TU ]	  U5        UR                  /[	        [        UR                  5      5       Vs/ s H  n[        UR                  SU-  -  5      PM      sn-   U l        [        U5      U l
        [        XR                  R                  5      U l        0 n[        U R                  U R                   5       H  u  pE["        R$                  " U5      X4'   M     ["        R&                  " U5      U l        U R+                  5         g s  snf )Nr?   )rV   rW   _init_backboner_   r  r+  r  r   r  rS   rm   r  r\   r  zip_out_featuresrp  r   rc   
ModuleDicthidden_states_normsr  )r7   ri   r  r  stagerM   rk   s         r(   rW   SwinBackbone.__init__  s     v&#--.X]^abhbobo^pXq1rXqST#f6F6FA6M2NXq1rr(0"6??+E+EF !#&t'9'94==#IE)+l)C& $J#%==1D#E  	 2ss   %Dc                 .    U R                   R                  $ r   r  r6   s    r(   r  !SwinBackbone.get_input_embeddings  r  r'   r   r  r  r  rn   c                 0   Ub  UOU R                   R                  nUb  UOU R                   R                  nUb  UOU R                   R                  nU R	                  U5      u  pVU R                  UUSUSSSSS9nUR                  nSn	[        U R                  U5       H  u  pXR                  ;   d  M  UR                  u  ppUR                  SSSS5      R                  5       nUR                  XU-  U5      nU R                  U
   " U5      nUR                  XX5      nUR                  SSSS5      R                  5       nX4-  n	M     U(       d  U	4nU(       a  UUR                  4-  nU$ [!        U	U(       a  UR                  OSUR"                  S	9$ )
a  
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(
...     "microsoft/swin-tiny-patch4-window7-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, 768, 7, 7]
```NT)r  r  r  r  rk  r  r   r   r?   r   r   )feature_mapsr   r   )ri   r  r  r  rm   r  r   r  stage_namesout_featuresrD   rF   rG   rE   r  r   r
   r   )r7   r   r  r  r  r  r   r  r   r  r  hidden_staterJ   rM   rK   rL   r   s                    r(   r   SwinBackbone.forward  s   @ &1%<k$++B]B]$8$D $++JjJj 	 2C1N-TXT_T_TqTq-1__\-J*,,/!%59!  	
  66#&t'7'7#GE))):F:L:L7
&+33Aq!Q?JJL+00e^\Z#77>|L+00UY+33Aq!Q?JJL/ $H "_F#70022M%3G'//T))
 	
r'   )rm   r  r  r  )NNN)r   r   r    r!   r   rW   r  r#   r   r   r   r
   r   r&   r   r   s   @r(   r  r    sp    z "0 04,0&*J
llJ
 'tnJ
 $D>	J

 d^J
 
J
 J
r'   r  )r  r  r  r  r  )r   F)Ar"   collections.abcr   r	  r3   dataclassesr   typingr   r   r   r#   torch.utils.checkpointr   activationsr	   modeling_outputsr
   modeling_utilsr   pytorch_utilsr   r   r   utilsr   r   r   r   utils.backbone_utilsr   configuration_swinr   
get_loggerr   loggerr   r*   r.   r<   rO   rQ   r   rS   rX   r   r   r   r   r   r   r   r  r%  r6  r@  rF  r  r  r  r  r  r  r  __all__r   r'   r(   <module>r$     s   &    ! ) )    ! . - [ [ D D 1 * 
		H	% K K K@  Kk  K  KF )#K )# )#X  K  K  KF	Y-RYY Y-x(-")) (-V3ryy 3nU\\ e T V[VbVb *-299 -a		 aH
RYY 
#BII #Lryy 	 	z		 zz9		 9xb
")) b
J =/ = =6 `
# `
 `
F 	d
!4 d
d
N =
!4 =
=
@ 
_
& _

_
Dr'   