Ë
    T^(hlø  ã            	       óÈ  — d Z ddl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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mZ ddl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!jP                  e)«      Z*dZ+dZ,g d¢Z-dZ.dZ/e G d„ de«      «       Z0e G d„ de«      «       Z1e G d„ de«      «       Z2e G d„ de«      «       Z3d„ Z4d„ Z5 G d„ dejl                  «      Z7 G d „ d!ejl                  «      Z8 G d"„ d#ejl                  «      Z9dMd$ejt                  d%e;d&e<d'ejt                  fd(„Z= G d)„ d*ejl                  «      Z> G d+„ d,ejl                  «      Z? G d-„ d.ejl                  «      Z@ G d/„ d0ejl                  «      ZA G d1„ d2ejl                  «      ZB G d3„ d4ejl                  «      ZC G d5„ d6ejl                  «      ZD G d7„ d8ejl                  «      ZE G d9„ d:ejl                  «      ZF G d;„ d<e«      ZGd=ZHd>ZI ed?eHd@«       G dA„ dBeG«      «       ZJ edCeH«       G dD„ dEeG«      «       ZK edFeH«       G dG„ dHeG«      «       ZL edIeH«       G dJ„ dKeGe%«      «       ZMg dL¢ZNy)NzPyTorch Swin Transformer model.é    N)Ú	dataclass)ÚOptionalÚTupleÚUnion)Únn)ÚBCEWithLogitsLossÚCrossEntropyLossÚMSELossé   )ÚACT2FN)ÚBackboneOutput)ÚPreTrainedModel)Ú find_pruneable_heads_and_indicesÚmeshgridÚprune_linear_layer)ÚModelOutputÚadd_code_sample_docstringsÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚloggingÚreplace_return_docstringsÚ	torch_int)ÚBackboneMixiné   )Ú
SwinConfigr   z&microsoft/swin-tiny-patch4-window7-224)r   é1   i   ztabby, tabby 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)ÚSwinEncoderOutputa€  
    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__Ú__doc__r   r   ÚtorchÚFloatTensorÚ__annotations__r    r   r!   r"   © ó    úd/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/swin/modeling_swin.pyr   r   >   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)	ÚSwinModelOutputaT  
    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.   _   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+   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<   ed	„ «       Zy)
ÚSwinMaskedImageModelingOutputaÇ  
    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                 óN   — t        j                  dt        «       | j                  S )Nzžlogits 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ÚFutureWarningr3   ©Úselfs    r,   Úlogitsz$SwinMaskedImageModelingOutput.logits¦   s%   € ä�‰ð]äô	
ð
 ×"Ñ"Ð"r+   )r#   r$   r%   r&   r2   r   r'   r(   r)   r3   r    r   r!   r"   Úpropertyr:   r*   r+   r,   r1   r1   ƒ   s£   … ñð6 )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø26€N�H˜U×.Ñ.Ñ/Ó6Ø=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ÓJàñ#ó ñ#r+   r1   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)	ÚSwinImageClassifierOutputa  
    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.
    Nr2   r:   .r    r!   r"   )r#   r$   r%   r&   r2   r   r'   r(   r)   r:   r    r   r!   r"   r*   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                 óÌ   — | j                   \  }}}}| j                  |||z  |||z  ||«      } | j                  dddddd«      j                  «       j                  d|||«      }|S )z2
    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   Ô   s}   € ð /<×.AÑ.AÑ+€J�˜˜|Ø!×&Ñ&Ø�F˜kÑ)¨;¸ÀÑ8LÈkÐ[gó€Mð ×#Ñ# A q¨!¨Q°°1Ó5×@Ñ@ÓB×GÑGÈÈKÐYdÐfrÓs€GØ€Nr+   c                 óÈ   — | j                   d   }| j                  d||z  ||z  |||«      } | j                  dddddd«      j                  «       j                  d|||«      } | S )z?
    Merges windows to produce higher resolution features.
    rB   r   r   r   r?   r@   rA   rC   )rN   rI   rK   rL   rM   s        r,   Úwindow_reverserQ   à   sn   € ð —=‘= Ñ$€LØ�l‰l˜2˜v¨Ñ4°e¸{Ñ6JÈKÐYdÐfrÓs€GØ�o‰o˜a  A q¨!¨QÓ/×:Ñ:Ó<×AÑAÀ"ÀfÈeÐUaÓb€GØ€Nr+   c            
       óÐ   ‡ — e Zd ZdZdˆ fd„	Zdej                  dededej                  fd„Z	 	 dde	ej                     d	e	ej                     d
edeej                     fd„Zˆ xZS )ÚSwinEmbeddingszW
    Construct the patch and position embeddings. Optionally, also the mask token.
    c                 ó~  •— t         ‰| �  «        t        |«      | _        | j                  j                  }| j                  j
                  | _        |r4t        j                  t        j                  dd|j                  «      «      nd | _        |j                  r=t        j                  t        j                  d|dz   |j                  «      «      | _        nd | _        t        j                  |j                  «      | _        t        j"                  |j$                  «      | _        |j(                  | _        || _        y )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)r9   rh   Úuse_mask_tokenrY   Ú	__class__s       €r,   rV   zSwinEmbeddings.__init__ï   sâ   ø€ Ü‰ÑÔä 3°FÓ ;ˆÔØ×+Ñ+×7Ñ7ˆØ×/Ñ/×9Ñ9ˆŒÙO]œ"Ÿ,™,¤u§{¡{°1°a¸×9IÑ9IÓ'JÔKÐcgˆŒà×)Ò)Ü')§|¡|´E·K±KÀÀ;ÐQRÁ?ÐTZ×TdÑTdÓ4eÓ'fˆDÕ$à'+ˆDÔ$ä—L‘L ×!1Ñ!1Ó2ˆŒ	Ü—z‘z &×"<Ñ"<Ó=ˆŒØ ×+Ñ+ˆŒØˆ�r+   Ú
embeddingsrK   rL   Úreturnc                 ó¦  — |j                   d   dz
  }| j                  j                   d   dz
  }t        j                  j	                  «       s||k(  r||k(  r| j                  S | j                  dd…dd…f   }| j                  dd…dd…f   }|j                   d   }|| j
                  z  }	|| j
                  z  }
t        |dz  «      }|j                  d|||«      }|j                  dddd«      }t        j                  j                  ||	|
fdd	¬
«      }|j                  dddd«      j                  dd|«      }t        j                  ||fd¬«      S )a   
        This method allows to interpolate the pre-trained position encodings, to be able to use the model on higher resolution
        images. This method is also adapted to support torch.jit tracing.

        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   ra   r'   ÚjitÚ
is_tracingrg   r   ÚreshaperF   r   Ú
functionalÚinterpolaterE   Úcat)r9   rk   rK   rL   rY   Únum_positionsÚclass_pos_embedÚpatch_pos_embedrt   Ú
new_heightÚ	new_widthÚsqrt_num_positionss               r,   Úinterpolate_pos_encodingz'SwinEmbeddings.interpolate_pos_encoding  s`  € ð !×&Ñ& qÑ)¨AÑ-ˆØ×0Ñ0×6Ñ6°qÑ9¸AÑ=ˆô �y‰y×#Ñ#Ô%¨+¸Ò*FÈ6ÐUZÊ?Ø×+Ñ+Ð+à×2Ñ2²1°b°q°b°5Ñ9ˆØ×2Ñ2²1°a±b°5Ñ9ˆà×Ñ˜rÑ"ˆà˜tŸ™Ñ.ˆ
Ø˜TŸ_™_Ñ,ˆ	ä& }°cÑ'9Ó:ÐØ)×1Ñ1°!Ð5GÐI[Ð]`ÓaˆØ)×1Ñ1°!°Q¸¸1Ó=ˆäŸ-™-×3Ñ3ØØ˜iÐ(ØØð	 4ó 
ˆð *×1Ñ1°!°Q¸¸1Ó=×BÑBÀ1ÀbÈ#ÓNˆä�y‰y˜/¨?Ð;ÀÔCÐCr+   Úpixel_valuesÚbool_masked_posr�   c                 óÂ  — |j                   \  }}}}| j                  |«      \  }}	| j                  |«      }|j                  «       \  }
}}|�K| j                  j                  |
|d«      }|j                  d«      j                  |«      }|d|z
  z  ||z  z   }| j                  �(|r|| j                  |||«      z   }n|| j                  z   }| j                  |«      }||	fS )NrB   ç      ð?)rD   rX   rc   rp   r_   ÚexpandÚ	unsqueezeÚtype_asra   r�   rf   )r9   r‚   rƒ   r�   Ú_rM   rK   rL   rk   Úoutput_dimensionsrJ   Úseq_lenÚmask_tokensÚmasks                 r,   ÚforwardzSwinEmbeddings.forward*  sô   € ð *6×);Ñ);Ñ&ˆˆ<˜ Ø(,×(=Ñ(=¸lÓ(KÑ%ˆ
Ð%Ø—Y‘Y˜zÓ*ˆ
Ø!+§¡Ó!2Ñˆ
�G˜QàÐ&ØŸ/™/×0Ñ0°¸WÀbÓIˆKà"×,Ñ,¨RÓ0×8Ñ8¸ÓEˆDØ# s¨T¡zÑ2°[À4Ñ5GÑGˆJà×#Ñ#Ð/Ù'Ø'¨$×*GÑ*GÈ
ÐTZÐ\aÓ*bÑb‘
à'¨$×*BÑ*BÑB�
à—\‘\ *Ó-ˆ
àÐ,Ð,Ð,r+   )F)NF)r#   r$   r%   r&   rV   r'   ÚTensorÚintr�   r   r(   Ú
BoolTensorÚboolr   rŽ   Ú__classcell__©rj   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                   óv   ‡ — e Zd ZdZˆ fd„Zd„ Zdeej                     de	ej                  e	e   f   fd„Zˆ xZS )rW   zì
    This class turns `pixel_values` of shape `(batch_size, num_channels, height, width)` into the initial
    `hidden_states` (patch embeddings) of shape `(batch_size, seq_length, hidden_size)` to be consumed by a
    Transformer.
    c                 ó  •— t         ‰| �  «        |j                  |j                  }}|j                  |j
                  }}t        |t        j                  j                  «      r|n||f}t        |t        j                  j                  «      r|n||f}|d   |d   z  |d   |d   z  z  }|| _        || _        || _        || _
        |d   |d   z  |d   |d   z  f| _        t        j                  ||||¬«      | _        y )Nr   r   )Úkernel_sizeÚstride)rU   rV   Ú
image_sizerg   rM   r^   Ú
isinstanceÚcollectionsÚabcÚIterablerY   rZ   r   ÚConv2dÚ
projection)r9   rh   r™   rg   rM   Úhidden_sizerY   rj   s          €r,   rV   zSwinPatchEmbeddings.__init__M  sû   ø€ Ü‰ÑÔØ!'×!2Ñ!2°F×4EÑ4E�Jˆ
Ø$*×$7Ñ$7¸×9IÑ9I�kˆÜ#-¨j¼+¿/¹/×:RÑ:RÔ#S‘ZÐZdÐfpÐYqˆ
Ü#-¨j¼+¿/¹/×:RÑ:RÔ#S‘ZÐZdÐfpÐYqˆ
Ø! !‘}¨
°1©Ñ5¸*ÀQ¹-È:ÐVWÉ=Ñ:XÑYˆØ$ˆŒØ$ˆŒØ(ˆÔØ&ˆÔØ$ Q™-¨:°a©=Ñ8¸*ÀQ¹-È:ÐVWÉ=Ñ:XÐYˆŒäŸ)™) L°+È:Ð^hÔiˆ�r+   c                 ón  — || j                   d   z  dk7  rDd| j                   d   || j                   d   z  z
  f}t        j                  j                  ||«      }|| j                   d   z  dk7  rFddd| j                   d   || j                   d   z  z
  f}t        j                  j                  ||«      }|S )Nr   r   )rg   r   rx   Úpad)r9   r‚   rK   rL   Ú
pad_valuess        r,   Ú	maybe_padzSwinPatchEmbeddings.maybe_pad\  s±   € Ø�4—?‘? 1Ñ%Ñ%¨Ò*Ø˜TŸ_™_¨QÑ/°%¸$¿/¹/È!Ñ:LÑ2LÑLÐMˆJÜŸ=™=×,Ñ,¨\¸:ÓFˆLØ�D—O‘O AÑ&Ñ&¨!Ò+Ø˜Q  4§?¡?°1Ñ#5¸ÀÇÁÐQRÑASÑ8SÑ#SÐTˆJÜŸ=™=×,Ñ,¨\¸:ÓFˆLØÐr+   r‚   rl   c                 óà   — |j                   \  }}}}| j                  |||«      }| j                  |«      }|j                   \  }}}}||f}|j                  d«      j	                  dd«      }||fS )Nr?   r   )rD   r¤   rŸ   ÚflattenÚ	transpose)r9   r‚   r‰   rM   rK   rL   rk   rŠ   s           r,   rŽ   zSwinPatchEmbeddings.forwarde  s}   € Ø)5×);Ñ);Ñ&ˆˆ<˜ à—~‘~ l°F¸EÓBˆØ—_‘_ \Ó2ˆ
Ø(×.Ñ.Ñˆˆ1ˆf�eØ# U˜OÐØ×'Ñ'¨Ó*×4Ñ4°Q¸Ó:ˆ
àÐ,Ð,Ð,r+   )r#   r$   r%   r&   rV   r¤   r   r'   r(   r   r�   r�   rŽ   r“   r”   s   @r,   rW   rW   F  sF   ø„ ñôjòð	- H¨U×->Ñ->Ñ$?ð 	-ÀEÈ%Ï,É,ÐX]Ð^aÑXbÐJbÑDc÷ 	-r+   rW   c            	       ó²   ‡ — e Zd ZdZej
                  fdee   dedej                  ddfˆ fd„Z	d„ Z
d	ej                  d
eeef   dej                  fd„Zˆ xZS )ÚSwinPatchMerginga'  
    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_resolutionrt   Ú
norm_layerrl   Nc                 ó¤   •— t         ‰| �  «        || _        || _        t	        j
                  d|z  d|z  d¬«      | _         |d|z  «      | _        y )Nr@   r?   F©Úbias)rU   rV   rª   rt   r   ÚLinearÚ	reductionrc   )r9   rª   rt   r«   rj   s       €r,   rV   zSwinPatchMerging.__init__~  sI   ø€ Ü‰ÑÔØ 0ˆÔØˆŒÜŸ™ 1 s¡7¨A°©G¸%Ô@ˆŒÙ˜q 3™wÓ'ˆ�	r+   c                 óŠ   — |dz  dk(  xs |dz  dk(  }|r.ddd|dz  d|dz  f}t         j                  j                  ||«      }|S )Nr?   r   r   )r   rx   r¢   )r9   rH   rK   rL   Ú
should_padr£   s         r,   r¤   zSwinPatchMerging.maybe_pad…  sU   € Ø˜q‘j A‘oÒ:¨5°1©9¸©>ˆ
ÙØ˜Q  5¨1¡9¨a°¸!±Ð<ˆJÜŸM™M×-Ñ-¨m¸ZÓHˆMàÐr+   rH   Úinput_dimensionsc                 óº  — |\  }}|j                   \  }}}|j                  ||||«      }| j                  |||«      }|d d …dd d…dd d…d d …f   }|d d …dd d…dd d…d d …f   }	|d d …dd d…dd d…d d …f   }
|d d …dd d…dd d…d d …f   }t        j                  ||	|
|gd«      }|j                  |dd|z  «      }| j                  |«      }| j                  |«      }|S )Nr   r?   r   rB   r@   )rD   rE   r¤   r'   rz   rc   r°   )r9   rH   r³   rK   rL   rJ   rt   rM   Úinput_feature_0Úinput_feature_1Úinput_feature_2Úinput_feature_3s               r,   rŽ   zSwinPatchMerging.forward�  s  € Ø(‰ˆ�à(5×(;Ñ(;Ñ%ˆ
�C˜à%×*Ñ*¨:°v¸uÀlÓSˆàŸ™ }°f¸eÓDˆà'ª¨1¨4¨a¨4°°°A°²qÐ(8Ñ9ˆà'ª¨1¨4¨a¨4°°°A°²qÐ(8Ñ9ˆà'ª¨1¨4¨a¨4°°°A°²qÐ(8Ñ9ˆà'ª¨1¨4¨a¨4°°°A°²qÐ(8Ñ9ˆäŸ	™	 ?°OÀ_ÐVeÐ"fÐhjÓkˆØ%×*Ñ*¨:°r¸1¸|Ñ;KÓLˆàŸ	™	 -Ó0ˆØŸ™ }Ó5ˆàÐr+   )r#   r$   r%   r&   r   rb   r   r�   ÚModulerV   r¤   r'   r�   rŽ   r“   r”   s   @r,   r©   r©   q  sr   ø„ ñ
ð XZ×WcÑWcñ (¨¨s©ð (¸#ð (È2Ï9É9ð (Ðhlõ (òð U§\¡\ð ÀUÈ3ÐPSÈ8Á_ð ÐY^×YeÑYe÷ r+   r©   ÚinputÚ	drop_probÚtrainingrl   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)rD   Úndimr'   ÚrandrÀ   rÁ   Úfloor_Údiv)rº   r»   r¼   Ú	keep_probrD   Ú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 )
ÚSwinDropPathzXDrop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).Nr»   rl   c                 ó0   •— t         ‰| �  «        || _        y ©N)rU   rV   r»   )r9   r»   rj   s     €r,   rV   zSwinDropPath.__init__À  s   ø€ Ü‰ÑÔØ"ˆ�r+   r    c                 óD   — t        || j                  | j                  «      S rÍ   )rÉ   r»   r¼   ©r9   r    s     r,   rŽ   zSwinDropPath.forwardÄ  s   € Ü˜¨¯©¸¿¹ÓFÐFr+   c                 ó8   — dj                  | j                  «      S )Nzp={})Úformatr»   r8   s    r,   Ú
extra_reprzSwinDropPath.extra_reprÇ  s   € Ø�}‰}˜TŸ^™^Ó,Ð,r+   rÍ   )r#   r$   r%   r&   r   ÚfloatrV   r'   r�   rŽ   ÚstrrÒ   r“   r”   s   @r,   rË   rË   ½  sG   ø„ Ùbñ# (¨5¡/ð #¸Tõ #ðG U§\¡\ð G°e·l±ló Gð-˜C÷ -r+   rË   c                   ó°   ‡ — e Zd Zˆ fd„Zd„ Z	 	 	 d	dej                  deej                     deej                     dee	   de
ej                     f
d„Zˆ xZS )
ÚSwinSelfAttentionc                 ó  •— t         ‰| �  «        ||z  dk7  rt        d|› d|› d�«      ‚|| _        t	        ||z  «      | _        | j                  | j
                  z  | _        t        |t        j                  j                  «      r|n||f| _        t        j                  t        j                  d| j                  d   z  dz
  d| j                  d   z  dz
  z  |«      «      | _        t        j"                  | j                  d   «      }t        j"                  | j                  d   «      }t        j$                  t'        ||gd¬«      «      }t        j(                  |d«      }|d d …d d …d f   |d d …d d d …f   z
  }	|	j+                  ddd«      j-                  «       }	|	d d …d d …dfxx   | j                  d   dz
  z  cc<   |	d d …d d …dfxx   | j                  d   dz
  z  cc<   |	d d …d d …dfxx   d| j                  d   z  dz
  z  cc<   |	j/                  d	«      }
| j1                  d
|
«       t        j2                  | j                  | j                  |j4                  ¬«      | _        t        j2                  | j                  | j                  |j4                  ¬«      | _        t        j2                  | j                  | j                  |j4                  ¬«      | _        t        j<                  |j>                  «      | _         y )Nr   zThe hidden size (z6) is not a multiple of the number of attention heads (ú)r?   r   Úij)ÚindexingrB   Úrelative_position_indexr­   )!rU   rV   Ú
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Úvaluerd   Úattention_probs_dropout_probrf   )r9   rh   rt   Ú	num_headsrI   Úcoords_hÚcoords_wÚcoordsÚcoords_flattenÚrelative_coordsrÛ   rj   s              €r,   rV   zSwinSelfAttention.__init__Ì  s¡  ø€ Ü‰ÑÔØ�‰?˜aÒÜØ# C 5Ð(^Ð_hÐ^iÐijÐkóð ð $-ˆÔ Ü#& s¨Y¡Ó#7ˆÔ Ø!×5Ñ5¸×8PÑ8PÑPˆÔä% k´;·?±?×3KÑ3KÔL‰KÐS^Ð`kÐRlð 	Ôô -/¯L©LÜ�K‰K˜˜T×-Ñ-¨aÑ0Ñ0°1Ñ4¸¸T×=MÑ=MÈaÑ=PÑ9PÐSTÑ9TÑUÐW`Óaó-
ˆÔ)ô
 —<‘< × 0Ñ 0°Ñ 3Ó4ˆÜ—<‘< × 0Ñ 0°Ñ 3Ó4ˆÜ—‘œX x°Ð&:ÀTÔJÓKˆÜŸ™ v¨qÓ1ˆØ(ªªA¨t¨Ñ4°~ÂaÈÊqÀjÑ7QÑQˆØ)×1Ñ1°!°Q¸Ó:×EÑEÓGˆØšš1˜a˜Ó  D×$4Ñ$4°QÑ$7¸!Ñ$;Ñ;Ó Øšš1˜a˜Ó  D×$4Ñ$4°QÑ$7¸!Ñ$;Ñ;Ó Øšš1˜a˜Ó  A¨×(8Ñ(8¸Ñ(;Ñ$;¸aÑ$?Ñ?Ó Ø"1×"5Ñ"5°bÓ"9ÐØ×ÑÐ6Ð8OÔPä—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«      S )NrB   r   r?   r   r   )rp   rÝ   rÞ   rE   rF   )r9   ÚxÚnew_x_shapes      r,   Útranspose_for_scoresz&SwinSelfAttention.transpose_for_scoresñ  sL   € Ø—f‘f“h˜s �m t×'?Ñ'?À×AYÑAYÐ&ZÑZˆØ�F‰F�;ÓˆØ�y‰y˜˜A˜q !Ó$Ð$r+   r    Úattention_maskÚ	head_maskÚoutput_attentionsrl   c                 ó  — |j                   \  }}}| j                  |«      }| j                  | j                  |«      «      }	| j                  | j	                  |«      «      }
| j                  |«      }t        j                  ||	j                  dd«      «      }|t        j                  | j                  «      z  }| j                  | j                  j                  d«         }|j                  | j                  d   | j                  d   z  | j                  d   | j                  d   z  d«      }|j                  ddd«      j!                  «       }||j#                  d«      z   }|�r|j                   d   }|j                  ||z  || j$                  ||«      }||j#                  d«      j#                  d«      z   }|j                  d| j$                  ||«      }t&        j(                  j+                  |d¬«      }| j-                  |«      }|�||z  }t        j                  ||
«      }|j                  dddd«      j!                  «       }|j/                  «       d d | j0                  fz   }|j                  |«      }|r||f}|S |f}|S )NrB   éþÿÿÿr   r   r?   rs   r   )rD   ræ   ró   rç   rè   r'   Úmatmulr§   ÚmathÚsqrtrÞ   rà   rÛ   rE   rI   rF   rG   r‡   rÝ   r   rx   Úsoftmaxrf   rp   rß   )r9   r    rô   rõ   rö   rJ   rt   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Ž   zSwinSelfAttention.forwardö  s’  € ð )6×(;Ñ(;Ñ%ˆ
�C˜Ø ŸJ™J }Ó5Ðà×-Ñ-¨d¯h©h°}Ó.EÓFˆ	Ø×/Ñ/°·
±
¸=Ó0IÓJˆØ×/Ñ/Ð0AÓBˆô !Ÿ<™<¨°Y×5HÑ5HÈÈRÓ5PÓQÐà+¬d¯i©i¸×8PÑ8PÓ.QÑQÐà!%×!BÑ!BÀ4×C_ÑC_×CdÑCdÐegÓChÑ!iÐØ!7×!<Ñ!<Ø×Ñ˜QÑ $×"2Ñ"2°1Ñ"5Ñ5°t×7GÑ7GÈÑ7JÈT×M]ÑM]Ð^_ÑM`Ñ7`Ðbdó"
Ðð "8×!?Ñ!?ÀÀ1ÀaÓ!H×!SÑ!SÓ!UÐØ+Ð.D×.NÑ.NÈqÓ.QÑQÐàÐ%à'×-Ñ-¨aÑ0ˆJØ/×4Ñ4Ø˜jÑ(¨*°d×6NÑ6NÐPSÐUXó Ðð  0°.×2JÑ2JÈ1Ó2M×2WÑ2WÐXYÓ2ZÑZÐØ/×4Ñ4°R¸×9QÑ9QÐSVÐX[Ó\Ðô Ÿ-™-×/Ñ/Ð0@ÀbÐ/ÓIˆð Ÿ,™, Ó7ˆð Ð Ø-°	Ñ9ˆOäŸ™ _°kÓBˆØ%×-Ñ-¨a°°A°qÓ9×DÑDÓFˆØ"/×"4Ñ"4Ó"6°s¸Ð";¸t×?QÑ?QÐ>SÑ"SÐØ%×*Ñ*Ð+BÓCˆá6G�= /Ð2ˆàˆð O\ÐM]ˆàˆr+   ©NNF)r#   r$   r%   rV   ró   r'   r�   r   r(   r’   r   rŽ   r“   r”   s   @r,   rÖ   rÖ   Ë  sv   ø„ ô#GòJ%ð 7;Ø15Ø,1ñ6à—|‘|ð6ð ! ×!2Ñ!2Ñ3ð6ð ˜E×-Ñ-Ñ.ð	6ð
 $ D™>ð6ð 
ˆu�|‰|Ñ	÷6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 )ÚSwinSelfOutputc                 ó    •— t         ‰| �  «        t        j                  ||«      | _        t        j
                  |j                  «      | _        y rÍ   )rU   rV   r   r¯   Údenserd   ré   rf   ©r9   rh   rt   rj   s      €r,   rV   zSwinSelfOutput.__init__0  s6   ø€ Ü‰ÑÔÜ—Y‘Y˜s CÓ(ˆŒ
Ü—z‘z &×"EÑ"EÓFˆ�r+   r    Úinput_tensorrl   c                 óJ   — | j                  |«      }| j                  |«      }|S rÍ   ©r  rf   )r9   r    r  s      r,   rŽ   zSwinSelfOutput.forward5  s$   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆàÐr+   ©r#   r$   r%   rV   r'   r�   rŽ   r“   r”   s   @r,   r
  r
  /  s2   ø„ ôGð
 U§\¡\ð ÀÇÁð ÐRW×R^ÑR^÷ r+   r
  c                   ó°   ‡ — e Zd Zˆ fd„Zd„ Z	 	 	 d	dej                  deej                     deej                     dee	   de
ej                     f
d„Zˆ xZS )
ÚSwinAttentionc                 óˆ   •— t         ‰| �  «        t        ||||«      | _        t	        ||«      | _        t        «       | _        y rÍ   )rU   rV   rÖ   r9   r
  rÈ   ÚsetÚpruned_heads)r9   rh   rt   rê   rI   rj   s        €r,   rV   zSwinAttention.__init__=  s8   ø€ Ü‰ÑÔÜ% f¨c°9¸kÓJˆŒ	Ü$ V¨SÓ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   rs   )Úlenr   r9   rÝ   rÞ   r  r   ræ   rç   rè   rÈ   r  rß   Úunion)r9   ÚheadsÚindexs      r,   Úprune_headszSwinAttention.prune_headsC  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ô   rõ   rö   rl   c                 ój   — | j                  ||||«      }| j                  |d   |«      }|f|dd  z   }|S )Nr   r   )r9   rÈ   )r9   r    rô   rõ   rö   Úself_outputsÚattention_outputr  s           r,   rŽ   zSwinAttention.forwardU  sG   € ð —y‘y °À	ÐK\Ó]ˆØŸ;™; |°A¡¸ÓFÐØ#Ð%¨°Q°RÐ(8Ñ8ˆØˆr+   r  )r#   r$   r%   rV   r  r'   r�   r   r(   r’   r   rŽ   r“   r”   s   @r,   r  r  <  st   ø„ ô"ò;ð* 7;Ø15Ø,1ñ
à—|‘|ð
ð ! ×!2Ñ!2Ñ3ð
ð ˜E×-Ñ-Ñ.ð	
ð
 $ 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 )ÚSwinIntermediatec                 ó  •— t         ‰| �  «        t        j                  |t	        |j
                  |z  «      «      | _        t        |j                  t        «      rt        |j                     | _        y |j                  | _        y rÍ   )rU   rV   r   r¯   r�   Ú	mlp_ratior  rš   Ú
hidden_actrÔ   r   Úintermediate_act_fnr  s      €r,   rV   zSwinIntermediate.__init__c  sa   ø€ Ü‰ÑÔÜ—Y‘Y˜s¤C¨×(8Ñ(8¸3Ñ(>Ó$?Ó@ˆŒ
Ü�f×'Ñ'¬Ô-Ü'-¨f×.?Ñ.?Ñ'@ˆDÕ$à'-×'8Ñ'8ˆDÕ$r+   r    rl   c                 óJ   — | j                  |«      }| j                  |«      }|S rÍ   )r  r%  rÏ   s     r,   rŽ   zSwinIntermediate.forwardk  s&   € ØŸ
™
 =Ó1ˆØ×0Ñ0°Ó?ˆØÐr+   r  r”   s   @r,   r!  r!  b  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 )Ú
SwinOutputc                 óÌ   •— t         ‰| �  «        t        j                  t	        |j
                  |z  «      |«      | _        t        j                  |j                  «      | _	        y rÍ   )
rU   rV   r   r¯   r�   r#  r  rd   re   rf   r  s      €r,   rV   zSwinOutput.__init__r  sF   ø€ Ü‰ÑÔÜ—Y‘Yœs 6×#3Ñ#3°cÑ#9Ó:¸CÓ@ˆŒ
Ü—z‘z &×"<Ñ"<Ó=ˆ�r+   r    rl   c                 óJ   — | j                  |«      }| j                  |«      }|S rÍ   r  rÏ   s     r,   rŽ   zSwinOutput.forwardw  s$   € ØŸ
™
 =Ó1ˆØŸ™ ]Ó3ˆØÐr+   r  r”   s   @r,   r(  r(  q  s#   ø„ ô>ð
 U§\¡\ð °e·l±l÷ r+   r(  c                   óÐ   ‡ — e Zd Zdˆ fd„	Zd„ Zd„ Zd„ Z	 	 	 ddej                  de	e
e
f   deej                     dee   d	ee   d
e	ej                  ej                  f   fd„Zˆ xZS )Ú	SwinLayerc                 óì  •— t         ‰| �  «        |j                  | _        || _        |j                  | _        || _        t        j                  ||j                  ¬«      | _	        t        |||| j                  ¬«      | _        |dkD  rt        |«      nt        j                  «       | _        t        j                  ||j                  ¬«      | _        t!        ||«      | _        t%        ||«      | _        y )N©Úeps)rI   r¾   )rU   rV   Úchunk_size_feed_forwardÚ
shift_sizerI   rª   r   rb   Úlayer_norm_epsÚlayernorm_beforer  Ú	attentionrË   ÚIdentityrÉ   Úlayernorm_afterr!  Úintermediater(  rÈ   )r9   rh   rt   rª   rê   Údrop_path_rater1  rj   s          €r,   rV   zSwinLayer.__init__~  s¿   ø€ Ü‰ÑÔØ'-×'EÑ'EˆÔ$Ø$ˆŒØ!×-Ñ-ˆÔØ 0ˆÔÜ "§¡¨S°f×6KÑ6KÔ LˆÔÜ& v¨s°IÈ4×K[ÑK[Ô\ˆŒØ9GÈ#Ò9Mœ nÔ5ÔSU×S^ÑS^ÓS`ˆŒÜ!Ÿ|™|¨C°V×5JÑ5JÔKˆÔÜ,¨V°SÓ9ˆÔÜ  ¨Ó-ˆ�r+   c                 ó  — t        |«      | j                  k  rgt        d«      | _        t        j
                  j                  «       r(t	        j                   t	        j                  |«      «      n
t        |«      | _        y y ©Nr   )ÚminrI   r   r1  r'   ru   rv   Útensor)r9   rª   s     r,   Úset_shift_and_window_sizez#SwinLayer.set_shift_and_window_size‹  s\   € ÜÐÓ  D×$4Ñ$4Ò4ä'¨›lˆDŒOä=B¿Y¹Y×=QÑ=QÔ=S”—	‘	œ%Ÿ,™,Ð'7Ó8Ô9ÔY\Ð]mÓYnð Õð 5r+   c           	      ó  — | j                   dkD  �rzt        j                  d||df||¬«      }t        d| j                   «      t        | j                   | j                    «      t        | j                    d «      f}t        d| j                   «      t        | j                   | j                    «      t        | j                    d «      f}d}|D ]  }	|D ]  }
||d d …|	|
d d …f<   |dz  }Œ Œ t        || j                  «      }|j                  d| j                  | j                  z  «      }|j                  d«      |j                  d«      z
  }|j                  |dk7  t        d«      «      j                  |dk(  t        d«      «      }|S d }|S )Nr   r   r¿   rB   r?   g      YÀr¾   )
r1  r'   r]   ÚslicerI   rO   rE   r‡   Úmasked_fillrÓ   )r9   rK   rL   rÀ   rÁ   Úimg_maskÚheight_slicesÚwidth_slicesÚcountÚheight_sliceÚwidth_sliceÚmask_windowsÚ	attn_masks                r,   Úget_attn_maskzSwinLayer.get_attn_mask“  s—  € Ø�?‰?˜QÓä—{‘{ A v¨u°aÐ#8ÀÈfÔUˆHä�a˜$×*Ñ*Ð*Ó+Ü�t×'Ñ'Ð'¨$¯/©/Ð)9Ó:Ü�t—‘Ð&¨Ó-ðˆMô �a˜$×*Ñ*Ð*Ó+Ü�t×'Ñ'Ð'¨$¯/©/Ð)9Ó:Ü�t—‘Ð&¨Ó-ðˆLð
 ˆEØ -ò �Ø#/ò �KØ@E�HšQ ¨kº1Ð<Ñ=Ø˜Q‘J‘Eñðô
 ,¨H°d×6FÑ6FÓGˆLØ'×,Ñ,¨R°×1AÑ1AÀD×DTÑDTÑ1TÓUˆLØ$×.Ñ.¨qÓ1°L×4JÑ4JÈ1Ó4MÑMˆIØ!×-Ñ-¨i¸1©n¼eÀF»mÓL×XÑXÐYbÐfgÑYgÔinÐorÓisÓtˆIð Ðð ˆIØÐr+   c                 óþ   — | j                   || j                   z  z
  | j                   z  }| j                   || j                   z  z
  | j                   z  }ddd|d|f}t        j                  j                  ||«      }||fS r:  )rI   r   rx   r¢   )r9   r    rK   rL   Ú	pad_rightÚ
pad_bottomr£   s          r,   r¤   zSwinLayer.maybe_pad¯  s�   € Ø×%Ñ%¨°×0@Ñ0@Ñ(@Ñ@ÀD×DTÑDTÑTˆ	Ø×&Ñ&¨°$×2BÑ2BÑ)BÑBÀd×FVÑFVÑVˆ
Ø˜˜A˜y¨!¨ZÐ8ˆ
ÜŸ™×)Ñ)¨-¸ÓDˆØ˜jÐ(Ð(r+   r    r³   rõ   rö   Úalways_partitionrl   c                 óÊ  — |s| j                  |«       n	 |\  }}|j                  «       \  }}	}
|}| j                  |«      }|j                  ||||
«      }| j	                  |||«      \  }}|j
                  \  }	}}}	| j                  dkD  r1t        j                  || j                   | j                   fd¬«      }n|}t        || j                  «      }|j                  d| j                  | j                  z  |
«      }| j                  |||j                  |j                  ¬«      }| j                  ||||¬«      }|d   }|j                  d| j                  | j                  |
«      }t        || j                  ||«      }| j                  dkD  r/t        j                  || j                  | j                  fd¬«      }n|}|d   dkD  xs |d   dkD  }|r|d d …d |…d |…d d …f   j!                  «       }|j                  |||z  |
«      }|| j#                  |«      z   }| j%                  |«      }| j'                  |«      }|| j)                  |«      z   }|r	||d	   f}|S |f}|S )
Nr   )r   r?   )ÚshiftsÚdimsrB   r¿   )rö   r   rA   r   )r=  rp   r3  rE   r¤   rD   r1  r'   ÚrollrO   rI   rI  rÀ   rÁ   r4  rQ   rG   rÉ   r6  r7  rÈ   )r9   r    r³   rõ   rö   rM  rK   rL   rJ   r‰   ÚchannelsÚshortcutr£   Ú
height_padÚ	width_padÚshifted_hidden_statesÚhidden_states_windowsrH  Úattention_outputsr  Úattention_windowsÚshifted_windowsÚ
was_paddedÚlayer_outputÚlayer_outputss                            r,   rŽ   zSwinLayer.forward¶  s£  € ñ  Ø×*Ñ*Ð+;Õ<àØ(‰ˆ�Ø"/×"4Ñ"4Ó"6Ñˆ
�A�xØ ˆà×-Ñ-¨mÓ<ˆà%×*Ñ*¨:°v¸uÀhÓOˆð %)§N¡N°=À&È%Ó$PÑ!ˆ�zà&3×&9Ñ&9Ñ#ˆˆ:�y !à�?‰?˜QÒÜ$)§J¡J¨}ÀtÇÁÐFVÐY]×YhÑYhÐXhÐEiÐpvÔ$wÑ!à$1Ð!ô !1Ð1FÈ×HXÑHXÓ YÐØ 5× :Ñ :¸2¸t×?OÑ?OÐRV×RbÑRbÑ?bÐdlÓ mÐØ×&Ñ&Ø˜	¨×)<Ñ)<ÐEZ×EaÑEað 'ó 
ˆ	ð !ŸN™NØ! 9¨iÐK\ð +ó 
Ðð -¨QÑ/Ðà,×1Ñ1°"°d×6FÑ6FÈ×HXÑHXÐZbÓcÐÜ(Ð):¸D×<LÑ<LÈjÐZcÓdˆð �?‰?˜QÒÜ %§
¡
¨?ÀDÇOÁOÐUY×UdÑUdÐCeÐlrÔ sÑà /Ðà ‘] QÑ&Ò;¨*°Q©-¸!Ñ*;ˆ
ÙØ 1²!°W°f°W¸f¸u¸fÂaÐ2GÑ H× SÑ SÓ UÐà-×2Ñ2°:¸vÈ¹~ÈxÓXÐà  4§>¡>Ð2CÓ#DÑDˆà×+Ñ+¨MÓ:ˆØ×(Ñ(¨Ó6ˆØ$ t§{¡{°<Ó'@Ñ@ˆá@Q˜Ð'8¸Ñ';Ð<ˆØÐð YeÐWfˆØÐr+   )r¾   r   ©NFF)r#   r$   r%   rV   r=  rI  r¤   r'   r�   r   r�   r   r(   r’   rŽ   r“   r”   s   @r,   r,  r,  }  s™   ø„ õ.òòò8)ð 26Ø,1Ø+0ñAà—|‘|ðAð    S ™/ðAð ˜E×-Ñ-Ñ.ð	Að
 $ D™>ðAð # 4™.ðAð 
ˆu�|‰|˜UŸ\™\Ð)Ñ	*÷Ar+   r,  c                   ó¤   ‡ — e Zd Zˆ fd„Z	 	 	 d	dej
                  deeef   deej                     dee
   dee
   deej
                     fd„Zˆ xZS )
Ú	SwinStagec                 óh  •— t         ‰	| �  «        || _        || _        t	        j
                  t        |«      D �cg c]-  }t        ||||||   |dz  dk(  rdn|j                  dz  ¬«      ‘Œ/ c}«      | _	        |�& |||t        j                  ¬«      | _        d| _        y d | _        d| _        y c c}w )Nr?   r   )rh   rt   rª   rê   r8  r1  )rt   r«   F)rU   rV   rh   rt   r   Ú
ModuleListÚranger,  rI   Úblocksrb   Ú
downsampleÚpointing)
r9   rh   rt   rª   Údepthrê   rÉ   re  Úirj   s
            €r,   rV   zSwinStage.__init__û  s·   ø€ Ü‰ÑÔØˆŒØˆŒÜ—m‘mô ˜u›ö
ð ô Ø!ØØ%5Ø'Ø#,¨Q¡<Ø%&¨¡U¨a¢Z™q°f×6HÑ6HÈAÑ6Möò
ó
ˆŒð Ð!Ù(Ð)9¸sÌrÏ|É|Ô\ˆDŒOð ˆ�ð #ˆDŒOàˆ�ùò'
s   º2B/r    r³   rõ   rö   rM  rl   c                 ó  — |\  }}t        | j                  «      D ]  \  }}	|�||   nd }
 |	|||
||«      }|d   }Œ! |}| j                  �)|dz   dz  |dz   dz  }}||||f}| j                  ||«      }n||||f}|||f}|r|dd  z  }|S )Nr   r   r?   )Ú	enumeraterd  re  )r9   r    r³   rõ   rö   rM  rK   rL   rh  Úlayer_moduleÚlayer_head_maskr]  Ú!hidden_states_before_downsamplingÚheight_downsampledÚwidth_downsampledrŠ   Ústage_outputss                    r,   rŽ   zSwinStage.forward  sè   € ð )‰ˆ�Ü(¨¯©Ó5ò 	-‰OˆAˆ|Ø.7Ð.C˜i¨šlÈˆOá(ØÐ/°ÐBSÐUeóˆMð *¨!Ñ,‰Mð	-ð -:Ð)Ø�?‰?Ð&Ø5;¸a±ZÀAÑ4EÈÐPQÉ	ÐVWÑGWÐ 1ÐØ!'¨Ð0BÐDUÐ VÐØ ŸO™OÐ,MÐO_Ó`‰Mà!'¨°¸Ð >Ðà&Ð(IÐK\Ð]ˆáØ˜]¨1¨2Ð.Ñ.ˆMØÐr+   r^  )r#   r$   r%   rV   r'   r�   r   r�   r   r(   r’   rŽ   r“   r”   s   @r,   r`  r`  ú  sz   ø„ ôð< 26Ø,1Ø+0ñà—|‘|ðð    S ™/ðð ˜E×-Ñ-Ñ.ð	ð
 $ D™>ðð # 4™.ðð 
ˆu�|‰|Ñ	÷r+   r`  c                   ó¸   ‡ — e Zd Zˆ fd„Z	 	 	 	 	 	 ddej
                  deeef   deej                     dee
   dee
   dee
   dee
   d	ee
   d
eeef   fd„Zˆ xZS )ÚSwinEncoderc                 óÜ  •— 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  «      |d   d|z  z  |d   d|z  z  f|j                  |   |j"                  |   |t        |j                  d | «      t        |j                  d |dz    «       || j                  dz
  k  rt$        nd ¬«      ‘Œ§ c}«      | _        d| _        y c c}w c c}w )Nr   r?   r   )rh   rt   rª   rg  rê   rÉ   re  F)rU   rV   r  ÚdepthsÚ
num_layersrh   r'   Úlinspacer8  rã   Úitemr   rb  rc  r`  r�   r^   rê   r©   ÚlayersÚgradient_checkpointing)r9   rh   rZ   rñ   ÚdprÚi_layerrj   s         €r,   rV   zSwinEncoder.__init__7  sJ  ø€ Ü‰ÑÔÜ˜fŸm™mÓ,ˆŒØˆŒÜ!&§¡°°6×3HÑ3HÌ#ÈfÏmÉmÓJ\Ó!]Ö^˜Aˆq�v‰v�xÐ^ˆÐ^Ü—m‘mô  % T§_¡_Ó5öð ô Ø!Ü˜F×,Ñ,¨q°'©zÑ9Ó:Ø&/°¡l°q¸'±zÑ&BÀIÈaÁLÐUVÐX_ÑU_ÑD`Ð%aØ Ÿ-™-¨Ñ0Ø$×.Ñ.¨wÑ7Ø!¤# f§m¡m°H°WÐ&=Ó">ÄÀVÇ]Á]ÐS`ÐU\Ð_`ÑU`ÐEaÓAbÐcØ4;¸d¿o¹oÐPQÑ>QÒ4QÕ/ÐX\öòó
ˆŒð ',ˆÕ#ùò! _ùòs   Á'E$Â&B*E)r    r³   rõ   rö   Úoutput_hidden_statesÚ(output_hidden_states_before_downsamplingrM  Úreturn_dictrl   c	           	      óZ  — |rdnd }	|rdnd }
|rdnd }|rE|j                   \  }}} |j                  |g|¢|‘­Ž }|j                  dddd«      }|	|fz  }	|
|fz  }
t        | j                  «      D �]  \  }}|�||   nd }| j
                  r-| j                  r!| j                  |j                  |||||«      }n ||||||«      }|d   }|d   }|d   }|d   |d   f}|rP|rN|j                   \  }}} |j                  |g|d   |d   f¢|‘­Ž }|j                  dddd«      }|	|fz  }	|
|fz  }
nI|rG|sE|j                   \  }}} |j                  |g|¢|‘­Ž }|j                  dddd«      }|	|fz  }	|
|fz  }
|s�Œ||dd  z  }�Œ |st        d„ ||	|fD «       «      S t        ||	||
¬	«      S )
Nr*   r   r   r   r?   rø   rB   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrÍ   r*   )Ú.0Úvs     r,   ú	<genexpr>z&SwinEncoder.forward.<locals>.<genexpr>‘  s   è ø€ Òm˜qÐ_`Ñ_lœÑmùs   ‚Š)r   r    r!   r"   )rD   rE   rF   rj  rx  ry  r¼   Ú_gradient_checkpointing_funcÚ__call__Útupler   )r9   r    r³   rõ   rö   r|  r}  rM  r~  Úall_hidden_statesÚall_reshaped_hidden_statesÚall_self_attentionsrJ   r‰   r    Úreshaped_hidden_staterh  rk  rl  r]  rm  rŠ   s                         r,   rŽ   zSwinEncoder.forwardM  s‚  € ñ #7™B¸DÐÙ+?¡RÀTÐ"Ù$5™b¸4ÐáØ)6×)<Ñ)<Ñ&ˆJ˜˜;à$6 M×$6Ñ$6°zÐ$bÐDTÐ$bÐVaÒ$bÐ!Ø$9×$AÑ$AÀ!ÀQÈÈ1Ó$MÐ!Ø -Ð!1Ñ1ÐØ&Ð+@Ð*BÑBÐ&ä(¨¯©Ó5ó *	9‰OˆAˆ|Ø.7Ð.C˜i¨šlÈˆOà×*Ò*¨t¯}ª}Ø $× AÑ AØ ×)Ñ)Ø!Ø$Ø#Ø%Ø$ó!‘ñ !-Ø!Ð#3°_ÐFWÐYió!�ð *¨!Ñ,ˆMØ0=¸aÑ0@Ð-Ø -¨aÑ 0Ðà 1°"Ñ 5Ð7HÈÑ7LÐMÐá#Ñ(PØ-N×-TÑ-TÑ*�
˜A˜{ð )OÐ(I×(NÑ(NØð)Ø"3°AÑ"6Ð8IÈ!Ñ8LÐ!Mð)ØOZò)Ð%ð )>×(EÑ(EÀaÈÈAÈqÓ(QÐ%Ø!Ð&GÐ%IÑIÐ!Ø*Ð/DÐ.FÑFÑ*Ù%Ñ.VØ-:×-@Ñ-@Ñ*�
˜A˜{à(:¨×(:Ñ(:¸:Ð(fÐHXÐ(fÐZeÒ(fÐ%Ø(=×(EÑ(EÀaÈÈAÈqÓ(QÐ%Ø! mÐ%5Ñ5Ð!Ø*Ð/DÐ.FÑFÐ*ã Ø# }°Q°RÐ'8Ñ8Ò#ðU*	9ñX ÜÑm ]Ð4EÐGZÐ$[ÔmÓmÐmä Ø+Ø+Ø*Ø#=ô	
ð 	
r+   )NFFFFT)r#   r$   r%   rV   r'   r�   r   r�   r   r(   r’   r   r   rŽ   r“   r”   s   @r,   rr  rr  6  s¹   ø„ ô,ð4 26Ø,1Ø/4ØCHØ+0Ø&*ñK
à—|‘|ðK
ð    S ™/ðK
ð ˜E×-Ñ-Ñ.ð	K
ð
 $ D™>ðK
ð ' t™nðK
ð 3;¸4±.ðK
ð # 4™.ðK
ð ˜d‘^ðK
ð 
ˆuÐ'Ð'Ñ	(÷K
r+   rr  c                   ó,   — e Zd ZdZeZdZdZdZdgZ	d„ Z
y)ÚSwinPreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    Úswinr‚   Tr`  c                 óH  — 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t        |t        «      rb|j                  �$|j                  j
                  j                  «        |j                  �%|j                  j
                  j                  «        yyt        |t         «      r%|j"                  j
                  j                  «        yy)zInitialize the weightsr¾   )ÚmeanÚstdNr…   )rš   r   r¯   rž   ÚweightÚdataÚnormal_rh   Úinitializer_ranger®   Úzero_rb   Úfill_rS   r_   ra   rÖ   rà   )r9   Úmodules     r,   Ú_init_weightsz!SwinPreTrainedModel._init_weights§  s"  € ä�fœrŸy™y¬"¯)©)Ð4Ô5ð �M‰M×Ñ×&Ñ&¨C°T·[±[×5RÑ5RÐ&ÔSØ�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ä˜¤§¡Ô-Ø�K‰K×Ñ×"Ñ"Ô$Ø�M‰M×Ñ×$Ñ$ SÕ)Ü˜¤Ô/Ø× Ñ Ð,Ø×!Ñ!×&Ñ&×,Ñ,Ô.Ø×)Ñ)Ð5Ø×*Ñ*×/Ñ/×5Ñ5Õ7ð 6ä˜Ô 1Ô2Ø×/Ñ/×4Ñ4×:Ñ:Õ<ð 3r+   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Œ  ›  s,   „ ñð
 €LØÐØ$€OØ&*Ð#Ø$˜Ðó=r+   rŒ  aG  
    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 ([`SwinConfig`]): 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.
        head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
            Mask to nullify selected heads of the self-attention modules. Mask values selected in `[0, 1]`:

            - 1 indicates the head is **not masked**,
            - 0 indicates the head is **masked**.

        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.
        interpolate_pos_encoding (`bool`, *optional*, defaults to `False`):
            Whether to interpolate the pre-trained position encodings.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
z^The bare Swin Model transformer outputting raw hidden-states without any specific head on top.a  
        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.
    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j                      deej                     d	ee   d
ee   dedee   deee
f   fd„«       «       Zˆ xZS )Ú	SwinModelc                 óê  •— t         ‰| �  |«       || _        t        |j                  «      | _        t        |j                  d| j
                  dz
  z  z  «      | _        t        ||¬«      | _
        t        || j                  j                  «      | _        t        j                  | j                  |j                   ¬«      | _        |rt        j$                  d«      nd | _        | j)                  «        y )Nr?   r   )ri   r.  )rU   rV   rh   r  rt  ru  r�   r^   Únum_featuresrS   rk   rr  r[   Úencoderr   rb   r2  Ú	layernormÚAdaptiveAvgPool1dÚpoolerÚ	post_init)r9   rh   Úadd_pooling_layerri   rj   s       €r,   rV   zSwinModel.__init__é  s³   ø€ Ü‰Ñ˜Ô ØˆŒÜ˜fŸm™mÓ,ˆŒÜ × 0Ñ 0°1¸¿¹È1Ñ9LÑ3MÑ MÓNˆÔä(¨ÀÔOˆŒÜ" 6¨4¯?©?×+EÑ+EÓFˆŒäŸ™ d×&7Ñ&7¸V×=RÑ=RÔSˆŒÙ1B”b×*Ñ*¨1Ô-ÈˆŒð 	�‰Õr+   c                 ó.   — | j                   j                  S rÍ   ©rk   rX   r8   s    r,   Úget_input_embeddingszSwinModel.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)Úitemsr¢  Úlayerr4  r  )r9   Úheads_to_pruner®  r  s       r,   Ú_prune_headszSwinModel._prune_headsû  sE   € ð
 +×0Ñ0Ó2ò 	C‰LˆE�5Ø�L‰L×Ñ˜uÑ%×/Ñ/×;Ñ;¸EÕBñ	Cr+   Úvision)Ú
checkpointÚoutput_typer™  ÚmodalityÚexpected_outputr‚   rƒ   rõ   rö   r|  r�   r~  rl   c                 ó   — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|€t	        d«      ‚| j                  |t        | j                   j                  «      «      }| j                  |||¬«      \  }}	| j                  ||	||||¬«      }
|
d   }| j                  |«      }d}| j                  �7| j                  |j                  dd«      «      }t        j                  |d«      }|s||f|
dd z   }|S t        |||
j                   |
j"                  |
j$                  ¬«      S )	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"   )rh   rö   r|  Úuse_return_dictrÜ   Úget_head_maskr  rt  rk   r¢  r£  r¥  r§   r'   r¦   r.   r    r!   r"   )r9   r‚   rƒ   rõ   rö   r|  r�   r~  Úembedding_outputr³   Úencoder_outputsÚsequence_outputÚpooled_outputrÈ   s                 r,   rŽ   zSwinModel.forward  sp  € ð, 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆàÐÜÐ?Ó@Ð@ð ×&Ñ& y´#°d·k±k×6HÑ6HÓ2IÓJˆ	à-1¯_©_Ø¨/ÐTlð .=ó .
Ñ*ÐÐ*ð Ÿ,™,ØØØØ/Ø!5Ø#ð 'ó 
ˆð *¨!Ñ,ˆØŸ.™.¨Ó9ˆàˆØ�;‰;Ð"Ø ŸK™K¨×(AÑ(AÀ!ÀQÓ(GÓHˆMÜ!ŸM™M¨-¸Ó;ˆMáØ% }Ð5¸ÈÈÐ8KÑKˆFàˆMäØ-Ø'Ø)×7Ñ7Ø&×1Ñ1Ø#2×#IÑ#Iô
ð 	
r+   )TF©NNNNNFN)r#   r$   r%   rV   rª  r°  r   ÚSWIN_INPUTS_DOCSTRINGr   Ú_CHECKPOINT_FOR_DOCr.   Ú_CONFIG_FOR_DOCÚ_EXPECTED_OUTPUT_SHAPEr   r'   r(   r‘   r’   r   r   rŽ   r“   r”   s   @r,   rŸ  rŸ  Þ  sæ   ø„ õò0òCñ +Ð+@ÓAÙØ&Ø#Ø$ØØ.ôð 59Ø6:Ø15Ø,0Ø/3Ø).Ø&*ñ>
à˜u×0Ñ0Ñ1ð>
ð " %×"2Ñ"2Ñ3ð>
ð ˜E×-Ñ-Ñ.ð	>
ð
 $ D™>ð>
ð ' t™nð>
ð #'ð>
ð ˜d‘^ð>
ð 
ˆu�oÐ%Ñ	&ò>
óó Bô>
r+   rŸ  aW  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>
    c                   óð   ‡ — e Zd Zˆ fd„Z ee«       eee¬«      	 	 	 	 	 	 	 dde	e
j                     de	e
j                     de	e
j                     de	e   de	e   ded	e	e   d
eeef   fd„«       «       Zˆ xZS )ÚSwinForMaskedImageModelingc                 óŠ  •— t         ‰| �  |«       t        |dd¬«      | _        t	        |j
                  d|j                  dz
  z  z  «      }t        j                  t        j                  ||j                  dz  |j                  z  d¬«      t        j                  |j                  «      «      | _        | j                  «        y )NFT)r§  ri   r?   r   )Úin_channelsÚout_channelsr—   )rU   rV   rŸ  r�  r�   r^   ru  r   Ú
Sequentialrž   Úencoder_striderM   ÚPixelShuffleÚdecoderr¦  )r9   rh   r¡  rj   s      €r,   rV   z#SwinForMaskedImageModeling.__init__Y  s    ø€ Ü‰Ñ˜Ô ä˜f¸ÈdÔSˆŒ	ä˜6×+Ñ+¨a°F×4EÑ4EÈÑ4IÑ.JÑJÓKˆÜ—}‘}Ü�I‰IØ(°v×7LÑ7LÈaÑ7OÐRX×ReÑReÑ7eÐstôô �O‰O˜F×1Ñ1Ó2ó	
ˆŒð 	�‰Õr+   )r³  r™  r‚   rƒ   rõ   rö   r|  r�   r~  rl   c           	      óÐ  — |�|n| j                   j                  }| j                  |||||||¬«      }|d   }	|	j                  dd«      }	|	j                  \  }
}}t        j                  |dz  «      x}}|	j                  |
|||«      }	| j                  |	«      }d}|��| j                   j                  | j                   j                  z  }|j                  d||«      }|j                  | j                   j                  d«      j                  | j                   j                  d«      j                  d«      j                  «       }t        j                  j!                  ||d¬	«      }||z  j#                  «       |j#                  «       d
z   z  | j                   j$                  z  }|s|f|dd z   }|�|f|z   S |S t'        |||j(                  |j*                  |j,                  ¬«      S )aI  
        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).

        Returns:

        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?   rn   rB   Únone)r°   gñhãˆµøä>)r2   r3   r    r!   r"   )rh   r·  r�  r§   rD   rú   Úfloorrw   rÊ  r™   rg   Úrepeat_interleaver‡   rG   r   rx   Úl1_lossrã   rM   r1   r    r!   r"   )r9   r‚   rƒ   rõ   rö   r|  r�   r~  r  r»  rJ   rM   Úsequence_lengthrK   rL   Úreconstructed_pixel_valuesÚmasked_im_lossrp   r�   Úreconstruction_lossrÈ   s                        r,   rŽ   z"SwinForMaskedImageModeling.forwardi  sè  € ðR &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà—)‘)ØØ+ØØ/Ø!5Ø%=Ø#ð ó 
ˆð " !™*ˆà)×3Ñ3°A°qÓ9ˆØ4C×4IÑ4IÑ1ˆ
�L /ÜŸ™ O°SÑ$8Ó9Ð9ˆ�Ø)×1Ñ1°*¸lÈFÐTYÓZˆð &*§\¡\°/Ó%BÐ"àˆØÑ&Ø—;‘;×)Ñ)¨T¯[©[×-CÑ-CÑCˆDØ-×5Ñ5°b¸$ÀÓEˆOà×1Ñ1°$·+±+×2HÑ2HÈ!ÓLß"Ñ" 4§;¡;×#9Ñ#9¸1Ó=ß‘˜1“ß‘“ð	 ô #%§-¡-×"7Ñ"7¸ÐF`ÐlrÐ"7Ó"sÐØ1°DÑ8×=Ñ=Ó?À4Ç8Á8Ã:ÐPTÑCTÑUÐX\×XcÑXc×XpÑXpÑpˆNáØ0Ð2°W¸Q¸R°[Ñ@ˆFØ3AÐ3M�^Ð%¨Ñ.ÐYÐSYÐYä,ØØ5Ø!×/Ñ/Ø×)Ñ)Ø#*×#AÑ#Aô
ð 	
r+   r½  )r#   r$   r%   rV   r   r¾  r   r1   rÀ  r   r'   r(   r‘   r’   r   r   rŽ   r“   r”   s   @r,   rÃ  rÃ  L  sÜ   ø„ ôñ  +Ð+@ÓAÙÐ+HÐWfÔgð 59Ø6:Ø15Ø,0Ø/3Ø).Ø&*ñT
à˜u×0Ñ0Ñ1ðT
ð " %×"2Ñ"2Ñ3ðT
ð ˜E×-Ñ-Ñ.ð	T
ð
 $ D™>ðT
ð ' t™nðT
ð #'ðT
ð ˜d‘^ðT
ð 
ˆuÐ3Ð3Ñ	4òT
ó hó BôT
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                   óô   ‡ — 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j                     dee   dee   ded	ee   d
eeef   fd„«       «       Zˆ xZS )ÚSwinForImageClassificationc                 ó>  •— t         ‰| �  |«       |j                  | _        t        |«      | _        |j                  dkD  r4t        j                  | j                  j                  |j                  «      nt        j                  «       | _	        | j                  «        y r:  )rU   rV   Ú
num_labelsrŸ  r�  r   r¯   r¡  r5  Ú
classifierr¦  )r9   rh   rj   s     €r,   rV   z#SwinForImageClassification.__init__Ò  sx   ø€ Ü‰Ñ˜Ô à ×+Ñ+ˆŒÜ˜fÓ%ˆŒ	ð EK×DUÑDUÐXYÒDYŒB�I‰I�d—i‘i×,Ñ,¨f×.?Ñ.?Ô@Ô_a×_jÑ_jÓ_lð 	Œð
 	�‰Õr+   )r²  r³  r™  rµ  r‚   rõ   Úlabelsrö   r|  r�   r~  rl   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).
        N)rõ   rö   r|  r�   r~  r   Ú
regressionÚsingle_label_classificationÚmulti_label_classificationrB   r?   )r2   r:   r    r!   r"   )rh   r·  r�  rØ  Úproblem_typer×  rÀ   r'   Úlongr�   r
   Úsqueezer	   rE   r   r=   r    r!   r"   )r9   r‚   rõ   rÙ  rö   r|  r�   r~  r  r¼  r:   r2   Úloss_fctrÈ   s                 r,   rŽ   z"SwinForImageClassification.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+   r½  )r#   r$   r%   rV   r   r¾  r   Ú_IMAGE_CLASS_CHECKPOINTr=   rÀ  Ú_IMAGE_CLASS_EXPECTED_OUTPUTr   r'   r(   Ú
LongTensorr’   r   r   rŽ   r“   r”   s   @r,   rÕ  rÕ  Â  sã   ø„ ô ñ +Ð+@ÓAÙØ*Ø-Ø$Ø4ô	ð 59Ø15Ø-1Ø,0Ø/3Ø).Ø&*ñ@
à˜u×0Ñ0Ñ1ð@
ð ˜E×-Ñ-Ñ.ð@
ð ˜×)Ñ)Ñ*ð	@
ð
 $ D™>ð@
ð ' t™nð@
ð #'ð@
ð ˜d‘^ð@
ð 
ˆuÐ/Ð/Ñ	0ò@
óó Bô@
r+   rÕ  zM
    Swin backbone, to be used with frameworks like DETR and MaskFormer.
    c                   ót   ‡ — e Zd Zdefˆ fd„Zd„ Z	 	 	 d
dej                  dee	   dee	   dee	   de
f
d	„Zˆ xZS )ÚSwinBackbonerh   c           	      ó>  •— t         ‰| �  |«       t         ‰| �	  |«       |j                  gt	        t        |j                  «      «      D �cg c]  }t        |j                  d|z  z  «      ‘Œ c}z   | _        t        |«      | _
        t        || j                  j                  «      | _        i }t        | j                  | j                   «      D ]  \  }}t#        j$                  |«      ||<   Œ t#        j&                  |«      | _        | j+                  «        y c c}w )Nr?   )rU   rV   Ú_init_backboner^   rc  r  rt  r�   r¡  rS   rk   rr  r[   r¢  ÚzipÚ_out_featuresrR  r   rb   Ú
ModuleDictÚhidden_states_normsr¦  )r9   rh   rh  rì  ÚstagerM   rj   s         €r,   rV   zSwinBackbone.__init__1  sñ   ø€ Ü‰Ñ˜Ô Ü‰Ñ˜vÔ&à#×-Ñ-Ð.ÔX]Ô^aÐbh×boÑboÓ^pÓXqÖ1rÐST´#°f×6FÑ6FÈÈAÉÑ6MÕ2NÒ1rÑrˆÔÜ(¨Ó0ˆŒÜ" 6¨4¯?©?×+EÑ+EÓFˆŒð !ÐÜ#& t×'9Ñ'9¸4¿=¹=Ó#Iò 	DÑˆE�<Ü)+¯©°lÓ)CÐ Ò&ð	Dä#%§=¡=Ð1DÓ#EˆÔ ð 	�‰Õùò 2ss   Á"Dc                 ó.   — | j                   j                  S rÍ   r©  r8   s    r,   rª  z!SwinBackbone.get_input_embeddingsB  r«  r+   r‚   r|  rö   r~  rl   c           
      ó   — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }| j	                  |«      \  }}| j                  ||d|d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 )
aK  
        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}  rM  r~  r*   r   r?   r   r   )Úfeature_mapsr    r!   )rh   r·  r|  rö   rk   r¢  r"   ré  Ústage_namesÚout_featuresrD   rF   rG   rE   rì  r    r   r!   )r9   r‚   r|  rö   r~  r¹  r³   r  r    rð  rí  Úhidden_staterJ   rM   rK   rL   rÈ   s                    r,   rŽ   zSwinBackbone.forwardE  sÄ  € ð@ &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà-1¯_©_¸\Ó-JÑ*ÐÐ*à—,‘,ØØØØ/Ø!%Ø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)r#   r$   r%   r   rV   rª  r'   r�   r   r’   r   rŽ   r“   r”   s   @r,   ræ  ræ  *  sj   ø„ ð˜zõ ò"0ð 04Ø,0Ø&*ñJ
à—l‘lðJ
ð ' t™nðJ
ð $ D™>ð	J
ð
 ˜d‘^ðJ
ð 
÷J
r+   ræ  )rÕ  rÃ  rŸ  rŒ  ræ  )r¾   F)Or&   Úcollections.abcr›   rú   r5   Údataclassesr   Útypingr   r   r   r'   Útorch.utils.checkpointr   Útorch.nnr   r	   r
   Úactivationsr   Úmodeling_outputsr   Úmodeling_utilsr   Úpytorch_utilsr   r   r   Úutilsr   r   r   r   r   r   r   Úutils.backbone_utilsr   Úconfiguration_swinr   Ú
get_loggerr#   ÚloggerrÀ  r¿  rÁ  râ  rã  r   r.   r1   r=   rO   rQ   r¹   rS   rW   r©   r�   rÓ   r’   rÉ   rË   rÖ   r
  r  r!  r(  r,  r`  rr  rŒ  ÚSWIN_START_DOCSTRINGr¾  rŸ  rÃ  rÕ  ræ  Ú__all__r*   r+   r,   ú<module>r     së  ðñ &ã Û Û Ý !ß )Ñ )ã Û Ý ß AÑ Aå !Ý .Ý -ß [Ñ [÷÷ ñ õ 2Ý *ð 
ˆ×	Ñ	˜HÓ	%€ð €ð ?Ð Ú%Ð ð CÐ Ø1Ð ð ôK˜ó Kó ðKð@ ô K�kó  Kó ð KðF ô)# Kó )#ó ð)#ðX ô K ó  Kó ð KòF	òôY-�R—Y‘Yô Y-ôx(-˜"Ÿ)™)ô (-ôV3�r—y‘yô 3ñn�U—\‘\ð ¨eð ÀTð ÐV[×VbÑVbó ô*-�2—9‘9ô -ôa˜Ÿ	™	ô aôH
�R—Y‘Yô 
ô#�B—I‘Iô #ôL�r—y‘yô ô	�—‘ô 	ôz�—	‘	ô zôz9�—	‘	ô 9ôxb
�"—)‘)ô b
ôJ=˜/ô =ð@	Ð ðÐ ñ0 ØdØðó	ôa
Ð#ó a
ó	ða
ñH ðð óôg
Ð!4ó g
óðg
ñT ðð óôV
Ð!4ó V
óðV
ñr ðð ó	ô_
Ð&¨ó _
óð_
òD�r+   