
     `i                      d Z ddlmZ ddlZddlZddlZddlmZ ddlm	Z	 ddl
mZ ddlmZmZ ddlZdd	lmZ dd
lmZmZmZmZmZmZ ddlmZ ddlmZmZmZm Z m!Z!m"Z" ddl#m$Z$  e!j%        e&          Z'dZ(dZ)g dZ*dZ+dZ,e	 G d de                      Z-e	 G d de                      Z.e	 G d de                      Z/e	 G d de                      Z0d`d Z1dad$Z2	 dbdcd.Z3 G d/ d0ej4        j5                  Z6 G d1 d2ej4        j5                  Z7 G d3 d4ej4        j5                  Z8 G d5 d6ej4        j5                  Z9 G d7 d8ej4        j5                  Z: G d9 d:ej4        j5                  Z; G d; d<ej4        j5                  Z< G d= d>ej4        j5                  Z= G d? d@ej4        j5                  Z> G dA dBej4        j5                  Z? G dC dDej4        j5                  Z@ G dE dFej4        j5                  ZA G dG dHe          ZBdIZCdJZDdddMZE G dN dOej4        j5                  ZFe G dP dQej4        j5                              ZG edReC           G dS dTeB                      ZH G dU dVej4        j5                  ZI G dW dXej4        j5                  ZJ edYeC           G dZ d[eB                      ZK ed\eC           G d] d^eBe                      ZLg d_ZMdS )ezTF 2.0 Swin Transformer model.    )annotationsN)Iterable)	dataclass)partial)AnyCallable   )ACT2FN)TFPreTrainedModelTFSequenceClassificationLossget_initializerkeraskeras_serializableunpack_inputs)
shape_list)ModelOutputadd_code_sample_docstringsadd_start_docstrings%add_start_docstrings_to_model_forwardloggingreplace_return_docstrings   )
SwinConfigr   z&microsoft/swin-tiny-patch4-window7-224)r   1   i   ztabby, tabby catc                  L    e Zd ZU dZdZded<   dZded<   dZded<   dZded<   dS )	TFSwinEncoderOutputaH  
    Swin encoder's outputs, with potential hidden states and attentions.

    Args:
        last_hidden_state (`tf.Tensor` 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(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `tf.Tensor` (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(tf.Tensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `tf.Tensor` (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(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `tf.Tensor` (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tf.Tensor | Nonelast_hidden_statetuple[tf.Tensor, ...] | Nonehidden_states
attentionsreshaped_hidden_states)	__name__
__module____qualname____doc__r   __annotations__r    r!   r"        }/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/swin/modeling_tf_swin.pyr   r   D   sd          2 +/....26M6666/3J3333;???????r)   r   c                  Z    e Zd ZU dZdZded<   dZded<   dZded<   dZded<   dZ	ded	<   dS )
TFSwinModelOutputa  
    Swin model's outputs that also contains a pooling of the last hidden states.

    Args:
        last_hidden_state (`tf.Tensor` of shape `(batch_size, sequence_length, hidden_size)`):
            Sequence of hidden-states at the output of the last layer of the model.
        pooler_output (`tf.Tensor` 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(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `tf.Tensor` (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(tf.Tensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `tf.Tensor` (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(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `tf.Tensor` (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   r   pooler_outputr   r    r!   r"   )
r#   r$   r%   r&   r   r'   r-   r    r!   r"   r(   r)   r*   r,   r,   e   su          6 +/....&*M****26M6666/3J3333;???????r)   r,   c                  p    e Zd ZU dZdZded<   dZded<   dZded<   dZded<   dZ	ded	<   e
d
             ZdS )TFSwinMaskedImageModelingOutputa  
    Swin masked image model outputs.

    Args:
        loss (`tf.Tensor` of shape `(1,)`, *optional*, returned when `bool_masked_pos` is provided):
            Masked image modeling (MLM) loss.
        reconstruction (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):
            Reconstructed pixel values.
        hidden_states (`tuple(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `tf.Tensor` (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(tf.Tensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `tf.Tensor` (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(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `tf.Tensor` (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   lossreconstructionr   r    r!   r"   c                D    t          j        dt                     | j        S )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*   logitsz&TFSwinMaskedImageModelingOutput.logits   s*    ]	
 	
 	

 ""r)   )r#   r$   r%   r&   r0   r'   r1   r    r!   r"   propertyr8   r(   r)   r*   r/   r/      s          6 "D!!!!'+N++++26M6666/3J3333;?????# # X# # #r)   r/   c                  Z    e Zd ZU dZdZded<   dZded<   dZded<   dZded<   dZ	ded	<   dS )
TFSwinImageClassifierOutputa  
    Swin outputs for image classification.

    Args:
        loss (`tf.Tensor` of shape `(1,)`, *optional*, returned when `labels` is provided):
            Classification (or regression if config.num_labels==1) loss.
        logits (`tf.Tensor` of shape `(batch_size, config.num_labels)`):
            Classification (or regression if config.num_labels==1) scores (before SoftMax).
        hidden_states (`tuple(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `tf.Tensor` (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(tf.Tensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
            Tuple of `tf.Tensor` (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(tf.Tensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
            Tuple of `tf.Tensor` (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   r0   r8   r   r    r!   r"   )
r#   r$   r%   r&   r0   r'   r8   r    r!   r"   r(   r)   r*   r;   r;      st          6 "D!!!!#F####26M6666/3J3333;???????r)   r;   input_feature	tf.Tensorwindow_sizeintreturnc           	         t          |           \  }}}}t          j        | |||z  |||z  ||f          } t          j        | d          }t          j        |d|||f          }|S )z2
    Partitions the given input into windows.
    r   r   r	            )r   tfreshape	transpose)r<   r>   
batch_sizeheightwidthnum_channelswindowss          r*   window_partitionrO      s|     /9.G.G+J|J	V{*K+9M{\hi M l=*<==Gj2{K"NOOGNr)   rN   rK   rL   c           	     h   t          j        |           d         }t          j        ||z  ||z  z  t           j                  }t           j                            ||          }t          j        | |||z  ||z  ||df          } t          j        | d          } t          j        | |||df          } | S )z?
    Merges windows to produce higher resolution features.
    r   rF   rB   )rG   shapecastint32mathfloordivrH   rI   )rN   r>   rK   rL   xyrJ   s          r*   window_reverserX      s     	!A
+";<bhGGA!!!Q''Jj*f3Uk5I;Xcegh G l7$677Gj:vub"ABBGNr)           FTinput	drop_probfloattrainingboolscale_by_keepc                   |dk    s|s| S d|z
  }t          |           }t          |          }|d         gdg|dz
  z  z   }t          j                            |          }t          j        ||k    dd          }|dk    r|r||z  }| |z  S )zb
    Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
    rY   r   r         ?)r   lenrG   randomuniformwhere)	rZ   r[   r]   r_   	keep_probinput_shapendimrQ   random_tensors	            r*   	drop_pathrj      s     CxIIU##K{D^sdQh//EI%%e,,MH]i7cBBM3="=  r)   c                  :     e Zd ZdZdd fd	ZddZ	 dddZ xZS )TFSwinEmbeddingszW
    Construct the patch and position embeddings. Optionally, also the mask token.
    Fconfigr   use_mask_tokenr^   r@   Nonec                    t                      j        di | t          |d          | _        | j        j        | _        | j        j        | _        |j        | _        || _        |j	        | _	        t          j                            dd          | _        t          j                            |j        d          | _        || _        d S )Npatch_embeddingsnamenormh㈵>)rs   epsilondropoutr(   )super__init__TFSwinPatchEmbeddingsrq   num_patches	grid_size
patch_grid	embed_dimrn   use_absolute_embeddingsr   layersLayerNormalizationrt   Dropouthidden_dropout_probrw   rm   )r7   rm   rn   kwargs	__class__s       r*   ry   zTFSwinEmbeddings.__init__  s    ""6""" 5fCU V V V0</9),'-'E$L333NN	|++F,FY+WWr)   rg   tf.TensorShapec                l   | j         r&|                     dd| j        fdd          | _        nd | _        | j        r.|                     d| j        dz   | j        fdd          | _        nd | _        | j        rd S d| _        t          | dd           Pt          j
        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | d	d           ]t          j
        | j        j                  5  | j                            d d | j        j        g           d d d            n# 1 swxY w Y   t          | d
d           St          j
        | j        j                  5  | j                            d            d d d            d S # 1 swxY w Y   d S d S )Nr   zeros
mask_tokenrQ   initializerrs   positional_embeddings)r   rs   Trq   rt   rw   )rn   
add_weightr~   r   r   r{   position_embeddingsbuiltgetattrrG   
name_scoperq   rs   buildrt   rm   rw   r7   rg   s     r*   r   zTFSwinEmbeddings.build  s    	#"ooQ4>4JX_frossDOO"DO' 	,'+D$q($.9wUl (7 ( (D$$ (,D$: 	F
4+T22>t49:: 2 2%++D1112 2 2 2 2 2 2 2 2 2 2 2 2 2 24&&2ty~.. E E	tT[-B CDDDE E E E E E E E E E E E E E E4D))5t|011 ) )""4((() ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) 65s6   0CCC(EE	E	?F''F+.F+Npixel_valuesr=   bool_masked_posbool | Noner]   !tuple[tf.Tensor, tuple[int, int]]c                   |                      ||          \  }}|                     ||          }t          |          \  }}}|nt          j        | j        |d          }	t          j        |	|d          }	t          j        |d          }
t          j        |
|	j                  }
|d|
z
  z  |	|
z  z   }| j	        
|| j	        z   }| 
                    ||          }||fS )Nr]   r   r   rF   ra   )rq   rt   r   rG   repeatr   expand_dimsrR   dtyper   rw   )r7   r   r   r]   
embeddingsoutput_dimensionsrJ   seq_len_mask_tokensmasks              r*   callzTFSwinEmbeddings.call6  s     )-(=(=lU](=(^(^%
%YYzHY==
!+J!7!7
GQ&)DOZCCK)K!<<K>/266D74!233D#sTz2[45GGJ#/#d&>>J\\*x\@@
,,,r)   F)rm   r   rn   r^   r@   ro   rg   r   r@   ro   )NF)r   r=   r   r   r]   r^   r@   r   )r#   r$   r%   r&   ry   r   r   __classcell__r   s   @r*   rl   rl   
  s               ) ) ) )6 ^c- - - - - - - - -r)   rl   c                  <     e Zd ZdZ fdZdd	ZdddZddZ xZS )rz   z#
    Image to Patch Embedding.
    c                8    t                      j        di | |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                            || j        | j        dd          | _        d S )Nr   r   valid
projection)filterskernel_sizestridespaddingrs   r(   )rx   ry   
image_size
patch_sizerM   r~   
isinstancecollectionsabcr   r{   r|   r   r   Conv2Dr   )	r7   rm   r   r   r   rM   hidden_sizer{   r   s	           r*   ry   zTFSwinPatchEmbeddings.__init__S  s%   ""6"""!'!2F4EJ
$*$79Ik#-j+/:R#S#SqZZZdfpYq
#-j+/:R#S#SqZZZdfpYq
!!}
15*Q-:VW=:XY$$(&$Q-:a=8*Q-:VW=:XY,--O . 
 
r)   r   r=   rK   r?   rL   r@   c                :   || j         d         z  dk    r9dddd| j         d         || j         d         z  z
  ff}t          j        ||          }|| j         d         z  dk    r9ddd| j         d         || j         d         z  z
  fdf}t          j        ||          }|S )Nr   r   r   r   )r   rG   pad)r7   r   rK   rL   
pad_valuess        r*   	maybe_padzTFSwinPatchEmbeddings.maybe_padh  s    4?1%%** &&1doa6H5SWSbcdSeKe6e2fgJ6,
;;LDOA&&!++ &1doa.@6DO\]L^C^.^*_aghJ6,
;;Lr)   Fr]   r^   r   c                   t          |          \  }}}}t          j                    r|| j        k    rt	          d          |                     |||          }t          j        |d          }|                     ||          }t          j        |d          }t          |          \  }}	}}||f}
t          j        |||	df          }t          j        |d          }||
fS )NzeMake sure that the channel dimension of the pixel values match with the one set in the configuration.r   rC   r	   r   r   r   r	   r   rC   rF   r   rC   r   )	r   rG   executing_eagerlyrM   
ValueErrorr   rI   r   rH   )r7   r   r]   r   rM   rK   rL   r   rJ   channelsr   s              r*   r   zTFSwinPatchEmbeddings.callq  s    )3L)A)A&<!! 	ld6G&G&Gw   ~~lFEBB |L,??__\H_EE
 \*l;;
.8.D.D+
Hfe#UOZ
Z2,FGG
\*i88
,,,r)   Nc                    | j         rd S d| _         t          | dd           \t          j        | j        j                  5  | j                            d d d | j        g           d d d            d S # 1 swxY w Y   d S d S )NTr   )r   r   rG   r   r   rs   r   rM   r   s     r*   r   zTFSwinPatchEmbeddings.build  s    : 	F
4t,,8t344 M M%%tT49J&KLLLM M M M M M M M M M M M M M M M M M 98s    $A11A58A5)r   r=   rK   r?   rL   r?   r@   r=   r   )r   r=   r]   r^   r@   r   N	r#   r$   r%   r&   ry   r   r   r   r   r   s   @r*   rz   rz   N  s         
 
 
 
 
*   - - - - -0M M M M M M M Mr)   rz   c                  B     e Zd ZdZ	 dd fdZddZdddZddZ xZS )TFSwinPatchMergingaB  
    Patch Merging Layer.

    Args:
        input_resolution (`tuple[int]`):
            Resolution of input feature.
        dim (`int`):
            Number of input channels.
        norm_layer (`keras.layer.Layer`, *optional*, defaults to `keras.layers.LayerNormalization`):
            Normalization layer class.
    Ninput_resolutiontuple[int, int]dimr?   
norm_layerCallable | Noner@   ro   c                     t                      j        d	i | || _        || _        t          j                            d|z  dd          | _        |(t          j                            dd          | _	        d S  |d          | _	        d S )
NrC   F	reduction)use_biasrs   ru   rt   rv   rs   rr   r(   )
rx   ry   r   r   r   r   Denser   r   rt   )r7   r   r   r   r   r   s        r*   ry   zTFSwinPatchMerging.__init__  s     	""6""" 0++AGe++VV7767RRDIII"
///DIIIr)   r<   r=   rK   rL   c                x    |dz  dk    p|dz  dk    }|r%dd|dz  fd|dz  fdf}t          j        ||          }|S )NrC   r   r   r   )rG   r   )r7   r<   rK   rL   
should_padr   s         r*   r   zTFSwinPatchMerging.maybe_pad  sY    qjAo:519>
 	> 1fqj/Auqy>6JJF=*==Mr)   Finput_dimensionsr]   r^   c                
   |\  }}t          |          \  }}}t          j        |||||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         }|d d dd ddd dd d f         }t          j        |	|
||gd          }t          j        ||dd|z  f          }|                     ||          }|                     ||          }|S )Nr   rC   r   rF   rD   r   )r   rG   rH   r   concatrt   r   )r7   r<   r   r]   rK   rL   rJ   r   rM   input_feature_0input_feature_1input_feature_2input_feature_3s                r*   r   zTFSwinPatchMerging.call  sZ   (&0&?&?#
A|
=:vul2[\\}feDD'14a4Aqqq(89'14a4Aqqq(89'14a4Aqqq(89'14a4Aqqq(89	?O_Ve"fhjkk
JA,<=
 
 		-(	CC}xHHr)   c                   | j         rd S d| _         t          | dd           [t          j        | j        j                  5  | j                            d d d| j        z  g           d d d            n# 1 swxY w Y   t          | dd           ^t          j        | j        j                  5  | j                            d d d| j        z  g           d d d            d S # 1 swxY w Y   d S d S )NTr   rD   rt   )	r   r   rG   r   r   rs   r   r   rt   r   s     r*   r   zTFSwinPatchMerging.build  s{   : 	F
4d++7t~233 A A$$dD!dh,%?@@@A A A A A A A A A A A A A A A4&&2ty~.. < <	tQ\ :;;;< < < < < < < < < < < < < < < < < < 32s$    &A22A69A6,&CC#&C#r   )r   r   r   r?   r   r   r@   ro   )r<   r=   rK   r?   rL   r?   r@   r=   r   )r<   r=   r   r   r]   r^   r@   r=   r   r   s   @r*   r   r     s        
 
 Z^0 0 0 0 0 0 0       6	< 	< 	< 	< 	< 	< 	< 	<r)   r   c                  0     e Zd ZdZdd fd
ZdddZ xZS )TFSwinDropPathzXDrop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).NTr[   float | Noner_   r^   r@   ro   c                V     t                      j        di | || _        || _        d S Nr(   )rx   ry   r[   r_   )r7   r[   r_   r   r   s       r*   ry   zTFSwinDropPath.__init__  s5    ""6""""*r)   FrZ   r=   r]   c                :    t          || j        || j                  S r   )rj   r[   r_   )r7   rZ   r]   s      r*   r   zTFSwinDropPath.call  s    $:LMMMr)   )NT)r[   r   r_   r^   r@   ro   r   )rZ   r=   r]   r^   r@   r=   r#   r$   r%   r&   ry   r   r   r   s   @r*   r   r     sj        bb+ + + + + + +
N N N N N N N N Nr)   r   c                  B     e Zd Zd fdZddZddZ	 	 	 	 dddZ xZS )TFSwinSelfAttentionrm   r   r   r?   	num_headsr@   ro   c                (    t                      j        d	i | ||z  dk    rt          d| d| d          || _        t	          ||z            | _        | j        | j        z  | _        |j        }t          |t          j
        j                  r|n||f| _        t          j                            | j        t          |j                  |j        d          | _        t          j                            | j        t          |j                  |j        d          | _        t          j                            | j        t          |j                  |j        d          | _        t          j                            |j                  | _        d S )
Nr   zThe hidden size (z6) is not a multiple of the number of attention heads ()query)kernel_initializerr   rs   keyvaluer(   )rx   ry   r   num_attention_headsr?   attention_head_sizeall_head_sizer>   r   r   r   r   r   r   r   r   initializer_rangeqkv_biasr   r   r   r   attention_probs_dropout_probrw   )r7   rm   r   r   r   r>   r   s         r*   ry   zTFSwinSelfAttention.__init__  s   ""6"""?akCkk_hkkk   $- #&sY#7#7 !58PP(%k;?3KLLlKKS^`kRl 	 \''.v/GHH_	 ( 
 

 <%%.v/GHH_	 & 
 
 \''.v/GHH_	 ( 
 

 |++F,OPPr)   rg   r   c                   |                      d| j        d         z  dz
  d| j        d         z  dz
  z  | j        fdd          | _        |                      | j        d         dz  | j        d         dz  fdt          j        d	          | _        t	          j        | j        d                   }t	          j        | j        d                   }t	          j        t	          j	        ||d
                    }t	          j
        |t          |          d         df          }|d d d d d f         |d d d d d f         z
  }t	          j        |d          }t	          j        |d          \  }}|| j        d         dz
  z  }|d| j        d         z  dz
  z  }|| j        d         dz
  z  }t	          j        ||gd          }| j                            t	          j        t	          j        |d          t          j                             | j        rd S d| _        t%          | dd           Xt	          j        | j        j                  5  | j                            d d | j        g           d d d            n# 1 swxY w Y   t%          | dd           Xt	          j        | j        j                  5  | j                            d d | j        g           d d d            n# 1 swxY w Y   t%          | dd           [t	          j        | j        j                  5  | j                            d d | j        g           d d d            d S # 1 swxY w Y   d S d S )NrC   r   r   r   relative_position_bias_tabler   Frelative_position_index)rQ   	trainabler   rs   ij)indexingrF   )r   rC   r   axisTr   r   r   )r   r>   r   r   rG   rS   r   rangestackmeshgridrH   r   rI   unstackassignrR   
reduce_sumr   r   r   r   rs   r   r   r   r   )	r7   rg   coords_hcoords_wcoordscoords_flattenrelative_coordsstack_0stack_1s	            r*   r   zTFSwinSelfAttention.build  s;   ,0OO)!,,q0Q9I!9L5Lq5PQTXTlm/ -< -
 -
)
 (,#A&!+T-=a-@A-EF(*	 (7 (
 (
$ 8D,Q/008D,Q/00"+h4HHHIIFZ-?-?-BB,GHH(AAAt4~aaaqqqj7QQ,	BB:oA>>>4#A&**1t'**Q..4#A&**(GW#5A>>>$++BGBM/XZ4[4[4[]_]e,f,fggg: 	F
4$''3tz// C C
  $d.@!ABBBC C C C C C C C C C C C C C C4%%1tx}-- A AdD,>?@@@A A A A A A A A A A A A A A A4$''3tz// C C
  $d.@!ABBBC C C C C C C C C C C C C C C C C C 43s6   #I;;I?I?5#K$$K(+K(#MMMrV   r=   c                    t          |          d d         | j        | j        gz   }t          j        ||          }t          j        |d          S )NrF   r   rC   r   r	   )r   r   r   rG   rH   rI   )r7   rV   new_x_shapes      r*   transpose_for_scoresz(TFSwinSelfAttention.transpose_for_scores6  sI     mmCRC(D,DdF^+__Jq+&&|A|,,,r)   NFr    attention_maskr   	head_maskoutput_attentionsr^   r]   tuple[tf.Tensor, ...]c                T   t          |          \  }}}|                     |          }	|                     |                     |                    }
|                     |                     |                    }|                     |	          }t          j        |t          j        |
d                    }|t          j	        | j
                  z  }t          j        | j        t          j        | j        d                    }t          j        || j        d         | j        d         z  | j        d         | j        d         z  df          }t          j        |d          }|t          j        |d          z   }|t          |          d         }t          j        |||z  || j        ||f          }t          j        |d          }t          j        |d          }||z   }t          j        |d| j        ||f          }t
          j                            |d          }|                     ||          }|||z  }t          j        ||          }t          j        |d	          }t          |          d d
         | j        gz   }t          j        ||          }|r||fn|f}|S )N)r   r   r	   rC   rF   r   r   rF   )rC   r   r   r   r   r  )r   r   r
  r   r   rG   matmulrI   rT   sqrtr   gatherr   rH   r   r>   r   r   nnsoftmaxrw   r   )r7   r    r  r  r  r]   rJ   r   r   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TFSwinSelfAttention.call;  s    (66
C JJ}55--dhh}.E.EFF	//

=0I0IJJ//0ABB 9[",y,2W2WXX+di8P.Q.QQ!#-rz$:VX]/^/^"
 "
 "$"a 4#3A#668H8KdN^_`Na8acef"
 "

 "$.Di!P!P+bn=SUV.W.WW%#N33A6J!z :#;ZIacfhk"l     ^NA>>N^NA>>N/.@!z*:RAY[^`c<dee %--(8r-BB ,,,JJ  -	9O	/;??]LAA",]";";CRC"@D
 #
 
=2IJJ6G]=/22mM]r)   rm   r   r   r?   r   r?   r@   ro   r   rV   r=   r@   r=   NNFF)r    r=   r  r   r  r   r  r^   r]   r^   r@   r  )r#   r$   r%   ry   r   r
  r   r   r   s   @r*   r   r     s        "Q "Q "Q "Q "Q "QH(C (C (C (CT- - - - ,0&*"'= = = = = = = = =r)   r   c                  2     e Zd Zd fdZdddZddZ xZS )TFSwinSelfOutputrm   r   r   r?   r@   ro   c                     t                      j        di | t          j                            |d          | _        t          j                            |j        d          | _        || _	        d S Ndenserr   rw   r(   )
rx   ry   r   r   r   r)  r   r   rw   r   r7   rm   r   r   r   s       r*   ry   zTFSwinSelfOutput.__init__|  sg    ""6"""\''''::
|++F,OV_+``r)   Fr    r=   input_tensorr]   r^   c                ^    |                      |          }|                     ||          }|S Nr   r)  rw   )r7   r    r+  r]   s       r*   r   zTFSwinSelfOutput.call  .    

=11]XFFr)   Nc                   | j         rd S d| _         t          | dd           Xt          j        | j        j                  5  | j                            d d | j        g           d d d            n# 1 swxY w Y   t          | dd           St          j        | j        j                  5  | j                            d            d d d            d S # 1 swxY w Y   d S d S )NTr)  rw   )	r   r   rG   r   r)  rs   r   r   rw   r   s     r*   r   zTFSwinSelfOutput.build  s[   : 	F
4$''3tz// 9 9
  $dh!78889 9 9 9 9 9 9 9 9 9 9 9 9 9 94D))5t|011 ) )""4((() ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) 65s$    #A//A36A3)CCCrm   r   r   r?   r@   ro   r   )r    r=   r+  r=   r]   r^   r@   r=   r   r#   r$   r%   ry   r   r   r   r   s   @r*   r&  r&  {  sj                 
	) 	) 	) 	) 	) 	) 	) 	)r)   r&  c                  @     e Zd Zd fdZd	 Z	 	 	 	 dddZddZ xZS )TFSwinAttentionrm   r   r   r?   r   r@   ro   c                     t                      j        di | t          |||d          | _        t	          ||d          | _        t                      | _        d S )Nr7   rr   outputr(   )rx   ry   r   r7   r&  self_outputsetpruned_heads)r7   rm   r   r   r   r   s        r*   ry   zTFSwinAttention.__init__  sa    ""6"""'YVLLL	+FChGGGEEr)   c                    t           )z
        Prunes heads of the model. See base class PreTrainedModel heads: dict of {layer_num: list of heads to prune in
        this layer}
        )NotImplementedError)r7   headss     r*   prune_headszTFSwinAttention.prune_heads  s
    
 "!r)   NFr    r=   r  r   r  r  r^   r]   c                    |                      |||||          }|                     |d         ||          }|f|dd          z   }|S )Nr   r   r   )r7   r7  )	r7   r    r  r  r  r]   self_outputsattention_outputr!  s	            r*   r   zTFSwinAttention.call  s\     yy	K\goypp++LO]U]+^^#%QRR(88r)   c                   | j         rd S d| _         t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           St          j        | j        j                  5  | j                            d            d d d            d S # 1 swxY w Y   d S d S )NTr7   r7  )r   r   rG   r   r7   rs   r   r7  r   s     r*   r   zTFSwinAttention.build  sR   : 	F
4&&2ty~.. & &	%%%& & & & & & & & & & & & & & &4--9t/455 - - &&t,,,- - - - - - - - - - - - - - - - - - :9$    A''A+.A+!C		CCr"  r$  )r    r=   r  r   r  r   r  r^   r]   r^   r@   r=   r   )r#   r$   r%   ry   r=  r   r   r   r   s   @r*   r4  r4    s        " " " " " "" " " ,0&*"'    	- 	- 	- 	- 	- 	- 	- 	-r)   r4  c                  0     e Zd Zd fdZdd
ZddZ xZS )TFSwinIntermediaterm   r   r   r?   r@   ro   c                :    t                      j        di | t          j                            t          |j        |z            d          | _        t          |j	        t                    rt          |j	                 | _        n|j	        | _        || _        d S )Nr)  rr   r(   )rx   ry   r   r   r   r?   	mlp_ratior)  r   
hidden_actstrr
   intermediate_act_fnr   r*  s       r*   ry   zTFSwinIntermediate.__init__  s    ""6"""\''F,<s,B(C(C''RR
f'-- 	9'-f.?'@D$$'-'8D$r)   r    r=   c                Z    |                      |          }|                     |          }|S r   )r)  rI  )r7   r    s     r*   r   zTFSwinIntermediate.call  s,    

=1100??r)   Nc                    | j         rd S d| _         t          | dd           [t          j        | j        j                  5  | j                            d d | j        g           d d d            d S # 1 swxY w Y   d S d S NTr)  )r   r   rG   r   r)  rs   r   r   r   s     r*   r   zTFSwinIntermediate.build  s    : 	F
4$''3tz// 9 9
  $dh!78889 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 43s    #A00A47A4r1  )r    r=   r@   r=   r   r2  r   s   @r*   rD  rD    se                
9 9 9 9 9 9 9 9r)   rD  c                  2     e Zd Zd fdZdddZddZ xZS )TFSwinOutputrm   r   r   r?   r@   ro   c                     t                      j        di | t          j                            |d          | _        t          j                            |j        d          | _        || _	        || _
        d S r(  )rx   ry   r   r   r   r)  r   r   rw   rm   r   r*  s       r*   ry   zTFSwinOutput.__init__  sj    ""6"""\''''::
|++F,F	RRr)   Fr    r=   r]   r^   c                ^    |                      |          }|                     ||          }|S r-  r.  )r7   r    r]   s      r*   r   zTFSwinOutput.call  r/  r)   Nc           	     2   | j         rd S d| _         t          | dd           ut          j        | j        j                  5  | j                            d d t          | j        j	        | j
        z            g           d d d            d S # 1 swxY w Y   d S d S rL  )r   r   rG   r   r)  rs   r   r?   rm   rF  r   r   s     r*   r   zTFSwinOutput.build  s    : 	F
4$''3tz// V V
  $c$+2G$(2R.S.S!TUUUV V V V V V V V V V V V V V V V V V 43s    =B

BBr1  r   )r    r=   r]   r^   r@   r=   r   r2  r   s   @r*   rN  rN    sr                 
V V V V V V V Vr)   rN  c                  N     e Zd Z	 	 dd  fdZd!dZd"dZ	 	 	 d#d$dZd%dZ xZS )&TFSwinLayerrY   r   r   r   r   r?   drop_path_rater\   
shift_sizer@   ro   c                    t                      j        di | |j        | _        t          j        |          }||j        k    r|n|j        | _        || j        k    rdn|| _        || _        t          j	        
                    |j        d          | _        t          |||d          | _        |dk    rt          |d          n t          j	                            dd          | _        t          j	        
                    |j        d	          | _        t'          ||d
          | _        t+          ||d          | _        || _        d S )Nr   layernorm_beforer   	attentionrr   rY   rj   linearlayernorm_afterintermediater6  r(   )rx   ry   chunk_size_feed_forwardrG   
reduce_minr>   rU  r   r   r   r   layer_norm_epsrW  r4  rX  r   
Activationrj   rZ  rD  r[  rN  swin_outputr   )
r7   rm   r   r   r   rT  rU  r   min_resr   s
            r*   ry   zTFSwinLayer.__init__  sO    	""6"""'-'E$- 011&-1C&C&C77I[&$*:::!!
 0 % ? ?H]dv ? w w(ikRRR ## ><<<<(((DD 	
  %|>>vG\ct>uu.vsPPP'(CCCr)   rK   rL   r>   r   c           
        t          j        ||f          }d| f| | f| dff}d| f| | f| dff}|dk    rd}|D ]}	|D ]}
t          j        |	d         |z  |	d         |z  dz             }t          j        |
d         |z  |
d         |z  dz             }t          j        t          j        t          j        ||          d          d          }t          |          dk    rBt          j        t          |          f|j                  |z  }t          j	        |||          }|dz  }t          j
        |d          }t          j
        |d          }t          ||          }t          j        |d||z  f          }t          j
        |d          t          j
        |d          z
  }t          j        |dk    d|          }t          j        |dk    d	|          }|S )
Nr   rF   r   r   )rF   rC   )r   rC   g      YrY   )rG   r   r   rH   r   r   rb   onesr   tensor_scatter_nd_updater   rO   re   )r7   rK   rL   r>   rU  img_maskheight_sliceswidth_slicescountheight_slicewidth_sliceheight_inds
width_indsindicesupdatesmask_windows	attn_masks                    r*   get_attn_maskzTFSwinLayer.get_attn_mask  s   8VUO,,k\*k\J;,G:+WYIZ[[L)[L:++F*VXHYZ >>E -  #/  K"$(<?V+C\RS_W]E]`aEa"b"bK!#+a.5*@+a.SXBX[\B\!]!]J j"+k:2V2V]_)`)`)`bijjG7||q(("$'3w<</"P"P"PSX"X#%#>xRY#Z#ZQJEE >(B//>(A..'+>>z,[;5N0OPPN<33bn\ST6U6UU	HY!^VY??	HY!^S)<<	r)   r    r=   tuple[tf.Tensor, tf.Tensor]c                    |||z  z
  |z  }|||z  z
  |z  }ddgd|gd|gddgg}t          j        ||          }t          j        |d          }||fS )Nr   r  )rG   r   rH   )r7   r    r>   rK   rL   	pad_right
pad_bottomr   s           r*   r   zTFSwinLayer.maybe_pad"  sw     !5;#66+E	!F[$88KG
!fq*o9~1vF
}j99Z
E22
j((r)   NFr   r  r  r^   r]   c                   t          j        |          }|| j        k    rdn| j        }|| j        k    r|n| j        }|\  }	}
t	          |          \  }}}|}|                     ||          }t          j        |||	|
|f          }|                     |||	|
          \  }}t	          |          \  }}}}|dk    rt          j        || | fd          }n|}t          ||          }t          j        |d||z  |f          }| 
                    ||||          }|                     |||||          }|d         }t          j        |d|||f          }t          ||||          }|dk    rt          j        |||fd          }n|}|d         dk    p|d	         dk    }|r|d d d |	d |
d d f         }t          j        |||	|
z  |f          }||                     ||          z   }|                     ||          }|                     |          }||                     ||          z   }|r
||d
         fn|f}|S )Nr   r   )r   rC   )shiftr   rF   )rK   rL   r>   rU  )r  r]   r	   rE   r   )rG   r]  r>   rU  r   rW  rH   r   rollrO   rq  rX  rX   rj   rZ  r[  r`  )r7   r    r   r  r  r]   ra  rU  r>   rK   rL   rJ   r   r   shortcutr   
height_pad	width_padshifted_hidden_stateshidden_states_windowsrp  attention_outputsr@  attention_windowsshifted_windows
was_paddedlayer_outputlayer_outputss                               r*   r   zTFSwinLayer.call,  s    - 011!T%555QQ4?
!(D,<!<!<gg$BR(",]";";
Ax --mh-OO
=:vuh2WXX$(NN=+vW\$]$]!z&0&?&?#:y!>>$&GM:+PZ{A[bh$i$i$i!!$1! !11F T T "
+@2{U`G`bjBk l l&&YKT^ ' 
 
	 !NN!9iK\go + 
 
 -Q/J'7"k;X`9abb():KU^__ >> "
J?W^d e e e /]Q&;*Q-!*;
 	I 1!!!WfWfufaaa2G HJ'8:vPU~W_:`aa 4>>2Ch>#W#WW++MH+MM((66$t'7'7x'7'X'XX@Qf'8';<<XdWfr)   c                   | j         rd S d| _         t          | dd           Xt          j        | j        j                  5  | j                            d d | j        g           d d d            n# 1 swxY w Y   t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           Pt          j        | j	        j                  5  | j	                            d            d d d            n# 1 swxY w Y   t          | dd           Xt          j        | j
        j                  5  | j
                            d d | j        g           d d d            n# 1 swxY w Y   t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           St          j        | j        j                  5  | j                            d            d d d            d S # 1 swxY w Y   d S d S )NTrW  rX  rj   rZ  r[  r`  )r   r   rG   r   rW  rs   r   r   rX  rj   rZ  r[  r`  r   s     r*   r   zTFSwinLayer.buildn  s   : 	F
4+T22>t49:: D D%++T4,BCCCD D D D D D D D D D D D D D D4d++7t~233 + +$$T***+ + + + + + + + + + + + + + +4d++7t~233 + +$$T***+ + + + + + + + + + + + + + +4*D11=t3899 C C$**D$+ABBBC C C C C C C C C C C C C C C4..:t0566 . .!''---. . . . . . . . . . . . . . .4--9t/455 - - &&t,,,- - - - - - - - - - - - - - - - - - :9sl    #A//A36A3)CCC
D11D58D5+#FF!FG;;G?G?5II!$I!)rY   r   )
r   r   r   r?   rT  r\   rU  r?   r@   ro   )
rK   r?   rL   r?   r>   r?   rU  r?   r@   r   )
r    r=   r>   r?   rK   r?   rL   r?   r@   rr  NFF)r    r=   r   r   r  r   r  r^   r]   r^   r@   r=   r   )	r#   r$   r%   ry   rq  r   r   r   r   r   s   @r*   rS  rS    s         !$      :   8) ) ) ) '+"'@ @ @ @ @D- - - - - - - -r)   rS  c                  8     e Zd Zd fdZ	 	 	 dd dZd!dZ xZS )"TFSwinStagerm   r   r   r?   r   r   depthr   rj   list[float]
downsampler   r@   ro   c                .    t                      j        di | | _        | _        fdt	          |          D             | _        |3 |t          t          j        j	        d          d          | _
        nd | _
        d| _        d S )Nc                t    g | ]4}t          |d z  dk    rdn	j        d z  |         d|           5S )rC   r   zblocks.)rm   r   r   r   rU  rT  rs   )rS  r>   ).0irm   r   rj   r   r   s     r*   
<listcomp>z(TFSwinStage.__init__.<locals>.<listcomp>  sq     
 
 
  !1#!"Q!11&2D2I(|"q]]  
 
 
r)   ru   )rv   r  )r   r   rs   Fr(   )rx   ry   rm   r   r   blocksr   r   r   r   r  pointing)
r7   rm   r   r   r  r   rj   r  r   r   s
    ``` ``  r*   ry   zTFSwinStage.__init__  s     	""6"""
 
 
 
 
 
 
 
 5\\
 
 
 !(j "5<#BDQQQ!	  DOO #DOr)   NFr    r=   r   r  r   r  r   r]   r^   r  c                6   |\  }}t          | j                  D ])\  }}	|||         nd }
 |	|||
||          }|d         }*| j        5|dz   dz  |dz   dz  }}||||f}|                     |d         ||          }n||||f}||f}|r||dd          z  }|S )Nr   r   r   rC   )	enumerater  r  )r7   r    r   r  r  r]   rK   rL   r  layer_modulelayer_head_maskr  height_downsampledwidth_downsampledr   stage_outputss                   r*   r   zTFSwinStage.call  s    )(55 	- 	-OA|.7.CillO(L/BS^f  M *!,MM?&5;aZA4EPQ	VWGW 1!'0BDU V OOM!,<>NYaObbMM!' >&(9: 	/]122..Mr)   c                   | j         rd S d| _         t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           P| j        D ]J}t          j        |j                  5  |                    d            d d d            n# 1 swxY w Y   Id S d S )NTr  r  )r   r   rG   r   r  rs   r   r  r7   rg   layers      r*   r   zTFSwinStage.build  sY   : 	F
4t,,8t344 , ,%%d+++, , , , , , , , , , , , , , ,44((4 & &]5:.. & &KK%%%& & & & & & & & & & & & & & & 54& &s$    A''A+.A+%CC	C	)rm   r   r   r?   r   r   r  r?   r   r?   rj   r  r  r   r@   ro   r  )r    r=   r   r   r  r   r  r   r]   r^   r@   r  r   r2  r   s   @r*   r  r    sv        & & & & & &X '+).    >
& 
& 
& 
& 
& 
& 
& 
&r)   r  c                  <     e Zd Zd fdZ	 	 	 	 	 dddZddZ xZS )TFSwinEncoderrm   r   r|   r   c           	     ~     t                      j        di | t          j                   _         _        t          t          j        ddt          j                            j
        z                                             fdt           j                  D              _        d _        d S )Nr   r   c                |   g | ]}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d|           S )rC   r   r   Nzlayers.)rm   r   r   r  r   rj   r  rs   )r  r?   r~   depthsr   sum
num_layersr   )r  i_layerrm   dprr|   r7   s     r*   r  z*TFSwinEncoder.__init__.<locals>.<listcomp>  s     
 
 
  (1g:566"+A,1g:">	!QRT[Q[@\!]mG, *73c&-"9::S}QX[\Q\}A]=^=^^_29DOa<O2O2O--VZ(w((	 	 	
 
 
r)   Fr(   )rx   ry   rb   r  r  rm   listrG   linspacer  rT  numpyr   r   gradient_checkpointing)r7   rm   r|   r   r  r   s   ``` @r*   ry   zTFSwinEncoder.__init__  s    ""6"""fm,,BK1c&-&8&899F<QQXXZZ[[
 
 
 
 
 
 
 !11
 
 
 ',###r)   NFTr    r=   r   r  r   r  r^   output_hidden_statesreturn_dictr]   r@   +tuple[tf.Tensor, ...] | TFSwinEncoderOutputc                   d}|rdnd }	|rdnd }
|rdnd }|rOt          |          \  }}}t          j        ||g||R           }t          j        |d          }|	|fz  }	|
|fz  }
t	          | j                  D ]\  }}|||         nd } ||||||          }|d         }|d         }|d         |d         f}||fz  }|rOt          |          \  }}}t          j        ||g||R           }t          j        |d          }|	|fz  }	|
|fz  }
|r||dd          z  }|st          d	 ||	|fD                       S t          ||	||

          S )Nr(   r   r   r   r   r  rF   rC   c              3     K   | ]}||V  	d S r   r(   )r  vs     r*   	<genexpr>z%TFSwinEncoder.call.<locals>.<genexpr>!  s(      mmq_`_l_l_l_l_lmmr)   )r   r    r!   r"   )r   rG   rH   rI   r  r   tupler   )r7   r    r   r  r  r  r  r]   all_input_dimensionsall_hidden_statesall_reshaped_hidden_statesall_self_attentionsrJ   r   r   reshaped_hidden_stater  r  r  r  r   s                        r*   r   zTFSwinEncoder.call  s     ""6@BBD+?%IRRT"$5?bb4 	C)3M)B)B&J;$&J}z>jL\>j^i>j>j$k$k!$&L1F$U$U!-!11&+@*BB&(55 	9 	9OA|.7.CillO(L/BS^f  M *!,M -a 0 1" 57H7LM %5$77 # G-7-F-F*
A{(*
=:BnP`BnbmBnBn(o(o%(*5JL(Y(Y%!m%55!*/D.FF*  9#}QRR'88# 	nmm]4EGZ$[mmmmmm"++*#=	
 
 
 	
r)   c                    | j         rd S d| _         t          | dd           P| j        D ]J}t          j        |j                  5  |                    d            d d d            n# 1 swxY w Y   Id S d S )NTr   )r   r   r   rG   r   rs   r   r  s      r*   r   zTFSwinEncoder.build*  s    : 	F
44((4 & &]5:.. & &KK%%%& & & & & & & & & & & & & & & 54& &s   A&&A*	-A*	)rm   r   r|   r   )NFFTF)r    r=   r   r   r  r   r  r^   r  r^   r  r^   r]   r^   r@   r  r   r2  r   s   @r*   r  r    s|        , , , , , ,2 '+"'%* 7
 7
 7
 7
 7
r& & & & & & & &r)   r  c                      e Zd ZdZeZdZdZdS )TFSwinPreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    swinr   N)r#   r$   r%   r&   r   config_classbase_model_prefixmain_input_namer(   r)   r*   r  r  4  s*         
 L$OOOr)   r  a`  
    This model is a Tensorflow
    [keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a
    regular Tensorflow Module and refer to the Tensorflow 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 (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See [`ViTImageProcessor.__call__`]
            for details.
        head_mask (`tf.Tensor` 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.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
r   rH  c                    | t           j                                        } |                                 }|dvrt	          dt          |           z             |S )z
    From tensorflow addons
    https://github.com/tensorflow/addons/blob/8cec33fcaaf1cf90aec7bdd55a0fcdbb251ce5c2/tensorflow_addons/utils/keras_utils.py#L71
    N>   channels_lastchannels_firstzWThe `data_format` argument must be one of "channels_first", "channels_last". Received: )r   backendimage_data_formatlowerr   rH  )r   data_formats     r*   normalize_data_formatr  a  sa    
 }//11++--K===ehklqhrhrr
 
 	
 r)   c                  P     e Zd ZdZej        dfd fdZddZddZd fdZ	 xZ
S )AdaptiveAveragePooling1Da|  
    Args:
    Average 1D Pooling with adaptive kernel size.
      output_size: An integer or tuple/list of a single integer, specifying pooled_features.
        The new size of output channels.
      data_format: A string,
        one of `channels_last` (default) or `channels_first`. The ordering of the dimensions in the inputs.
        `channels_last` corresponds to inputs with shape `(batch, steps, channels)` while `channels_first` corresponds
        to inputs with shape `(batch, channels, steps)`.
    Input shape:
      - If `data_format='channels_last'`: 3D tensor with shape `(batch, steps, channels)`.
      - If `data_format='channels_first'`: 3D tensor with shape `(batch, channels, steps)`.
    Output shape:
      - If `data_format='channels_last'`: 3D tensor with shape `(batch_size, pooled_steps, channels)`.
      - If `data_format='channels_first'`: 3D tensor with shape `(batch_size, channels, pooled_steps)`.

    Adapted from [tensorflow-addon's adaptive pooling.py](
        https://github.com/tensorflow/addons/blob/8cec33fcaaf1cf90aec7bdd55a0fcdbb251ce5c2/tensorflow_addons/layers/adaptive_pooling.py#L90-L120
    )
    Noutput_sizeint | Iterable[int]reduce_functionr   r  
str | Noner@   ro   c                    t          |          | _        || _        t          |t                    r|fnt          |          | _         t                      j        di | d S r   )	r  r  r  r   r?   r  r  rx   ry   )r7   r  r  r  r   r   s        r*   ry   z!AdaptiveAveragePooling1D.__init__  sf     1==.-7S-I-IaK>>uU`OaOa""6"""""r)   inputsr=   c                H   | j         d         }| j        dk    rEt          j        ||d          }t          j        |d          }|                     |d          }nDt          j        ||d          }t          j        |d          }|                     |d          }|S )Nr   r  r   r   rC   r	   )r  r  rG   splitr   r  )r7   r  argsbinssplitsout_vects         r*   r   zAdaptiveAveragePooling1D.call  s    "..Xfd333FXf1---F++F+;;HHXfd333FXf1---F++F+;;Hr)   rg   Iterable[int]r   c                "   t          j        |                                          }| j        dk    r/t          j        |d         | j        d         |d         g          }n.t          j        |d         |d         | j        d         g          }|S )Nr  r   rC   r   )rG   TensorShapeas_listr  r  )r7   rg   rQ   s      r*   compute_output_shapez-AdaptiveAveragePooling1D.compute_output_shape  s    n[1199;;..NKND4DQ4GUV#XYYEENKNKNDDTUVDW#XYYEr)   dict[str, Any]c                n    | j         | j        d}t                                                      }i ||S )N)r  r  )r  r  rx   
get_config)r7   rm   base_configr   s      r*   r  z#AdaptiveAveragePooling1D.get_config  sB    ++
 
 gg((**(+(((r)   )r  r  r  r   r  r  r@   ro   )r  r=   r@   ro   )rg   r  r@   r   )r@   r  )r#   r$   r%   r&   rG   reduce_meanry   r   r  r  r   r   s   @r*   r  r  p  s         0 %'N"&	
# 
# 
# 
# 
# 
# 
#
 
 
 
   ) ) ) ) ) ) ) ) ) )r)   r  c                  p     e Zd ZeZ	 d d! fd
Zd"dZd#dZd$dZe		 	 	 	 	 	 	 d%d&d            Z
d'dZ xZS )(TFSwinMainLayerTFrm   r   add_pooling_layerr^   rn   r@   ro   c                    t                      j        di | || _        t          |j                  | _        t          |j        d| j        dz
  z  z            | _        t          ||d          | _
        t          || j
        j        d          | _        t          j                            |j        d          | _        |rt'          d	
          nd | _        d S )NrC   r   r   )rn   rs   encoderrr   	layernormr   r   )r  r(   )rx   ry   rm   rb   r  r  r?   r~   num_featuresrl   r   r  r}   r  r   r   r   r^  r  r  poolerr7   rm   r  rn   r   r   s        r*   ry   zTFSwinMainLayer.__init__  s     	""6"""fm,, 0119L3M MNN*6.Wcddd$VT_-GiXXX88AV]h8iiDU_.4@@@@[_r)   rz   c                    | j         j        S r   )r   rq   r6   s    r*   get_input_embeddingsz$TFSwinMainLayer.get_input_embeddings  s    //r)   heads_to_prunedict[int, list]c                    |                                 D ]/\  }}| j        j        |         j                            |           0dS )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  r  rX  r=  )r7   r  r  r<  s       r*   _prune_headszTFSwinMainLayer._prune_heads  sU    
 +0022 	C 	CLE5Lu%/;;EBBBB	C 	Cr)   r  
Any | Noner  c                N    |t           d gt          | j        j                  z  S r   )r;  rb   rm   r  )r7   r  s     r*   get_head_maskzTFSwinMainLayer.get_head_mask  s(     %%vDK.////r)   Nr   r   r   r  r   r  r  r]   )TFSwinModelOutput | tuple[tf.Tensor, ...]c           	     V   ||n| j         j        }||n| j         j        }||n| j         j        }|t	          d          |                     |          }|                     |||          \  }}	|                     ||	|||||          }
|
d         }|                     ||          }d }| j	        ?t          |          \  }}}| 	                    |          }t          j        |||f          }|s||f|
dd          z   }|S t          |||
j        |
j        |
j                  S )N You have to specify pixel_values)r   r]   r  r  r  r  r]   r   r   r   )r   r-   r    r!   r"   )rm   r  r  use_return_dictr   r  r   r  r  r  r   rG   rH   r,   r    r!   r"   )r7   r   r   r  r  r  r  r]   embedding_outputr   encoder_outputssequence_outputpooled_outputrJ   r   r  r6  s                    r*   r   zTFSwinMainLayer.call  s    2C1N--TXT_Tq$8$D  $+Jj 	 &1%<kk$+B]?@@@ &&y11	-1__/H .= .
 .
** ,,/!5# ' 
 
 *!,..8.LL;"*4_*E*E'J< KK88MJ}z<6PQQM 	%}58KKFM -')7&1#2#I
 
 
 	
r)   c                   | j         rd S d| _         t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           [t          j        | j        j                  5  | j                            d d | j	        g           d d d            d S # 1 swxY w Y   d S d S )NTr   r  r  )
r   r   rG   r   r   rs   r   r  r  r  r   s     r*   r   zTFSwinMainLayer.build  s   : 	F
4t,,8t344 , ,%%d+++, , , , , , , , , , , , , , ,4D))5t|011 ) )""4((() ) ) ) ) ) ) ) ) ) ) ) ) ) )4d++7t~233 F F$$dD$2C%DEEEF F F F F F F F F F F F F F F F F F 87s6    A''A+.A+!CCC#D22D69D6TFrm   r   r  r^   rn   r^   r@   ro   )r@   rz   )r  r  )r  r  r@   r  NNNNNNFr   r   r   r   r  r   r  r   r  r   r  r   r]   r^   r@   r  r   )r#   r$   r%   r   r  ry   r  r  r  r   r   r   r   r   s   @r*   r  r    s        L Z_` ` ` ` ` ` `0 0 0 0C C C C0 0 0 0
  *.,0&*)-,0#':
 :
 :
 :
 ]:
xF F F F F F F Fr)   r  z^The bare Swin Model transformer outputting raw hidden-states without any specific head on top.c                       e Zd Z	 dd fd
Z ee           eeee	de
          e	 	 	 	 	 	 	 ddd                                    ZddZ xZS )TFSwinModelTFrm   r   r  r^   rn   r@   ro   c                v     t                      j        |fi | || _        t          |d          | _        d S )Nr  rr   )rx   ry   rm   r  r  r  s        r*   ry   zTFSwinModel.__init__"  sC     	**6***#F888			r)   vision)
checkpointoutput_typer  modalityexpected_outputNr   r   r   r  r  r   r  r  r]   r  c           	         ||n| j         j        }||n| j         j        }||n| j         j        }|t	          d          |                     |||||||          }|S )z
        bool_masked_pos (`tf.Tensor` of shape `(batch_size, num_patches)`, *optional*):
            Boolean masked positions. Indicates which patches are masked (1) and which aren't (0).
        Nr  )r   r   r  r  r  r  r]   )rm   r  r  r  r   r  )	r7   r   r   r  r  r  r  r]   swin_outputss	            r*   r   zTFSwinModel.call)  s    . 2C1N--TXT_Tq$8$D  $+Jj 	 &1%<kk$+B]?@@@yy%+/!5# ! 
 
 r)   c                    | j         rd S d| _         t          | dd           St          j        | j        j                  5  | j                            d            d d d            d S # 1 swxY w Y   d S d S )NTr  )r   r   rG   r   r  rs   r   r   s     r*   r   zTFSwinModel.buildU  s    : 	F
4&&2ty~.. & &	%%%& & & & & & & & & & & & & & & & & & 32s    A((A,/A,r  r  r  r  r   )r#   r$   r%   ry   r   SWIN_INPUTS_DOCSTRINGr   _CHECKPOINT_FOR_DOCr,   _CONFIG_FOR_DOC_EXPECTED_OUTPUT_SHAPEr   r   r   r   r   s   @r*   r   r     s         Z_9 9 9 9 9 9 9 +*+@AA&%$.    *.,0&*)-,0#'! ! ! ! ]  BA!F& & & & & & & &r)   r   c                  ,     e Zd ZdZd
 fdZdd	Z xZS )TFSwinPixelShufflez0TF layer implementation of torch.nn.PixelShuffleupscale_factorr?   r@   ro   c                     t                      j        di | t          |t                    r|dk     rt	          d|           || _        d S )NrC   z1upscale_factor must be an integer value >= 2 got r(   )rx   ry   r   r?   r   r  )r7   r  r   r   s      r*   ry   zTFSwinPixelShuffle.__init__a  sc    ""6""".#.. 	c.12D2DaQ_aabbb,r)   rV   r=   c                r   |}t          |          \  }}}}| j        dz  t          |z            t          j        fdt                    D             g          }t          j        |t          j        ||dg          d          }t          j        	                    || j        d          }|S )NrC   c                D    g | ]}t                    D ]
}||z  z   S r(   )r   )r  r  jblock_size_squaredoutput_depths      r*   r  z+TFSwinPixelShuffle.call.<locals>.<listcomp>q  s<    iiiQUZ[gUhUhiiPQa!(((iiiir)   r   rF   )paramsrm  
batch_dimsNHWC)
block_sizer  )
r   r  r?   rG   constantr   r  tiler  depth_to_space)	r7   rV   r    rJ   r   num_input_channelspermutationr  r  s	          @@r*   r   zTFSwinPixelShuffle.callg  s    /9-/H/H,
Aq,!0!3-0BBCC
 kiiiii%8J2K2Kiiij
 
 	V`bcUd@e@ertuuu,,]tGZhn,oor)   )r  r?   r@   ro   r#  r   r   s   @r*   r  r  ^  sW        ::- - - - - -       r)   r  c                  0     e Zd Zd
 fdZddZdd	Z xZS )TFSwinDecoderrm   r   c                     t                      j        di | t          j                            |j        dz  |j        z  ddd          | _        t          |j        d          | _	        || _
        d S )NrC   r   0)r   r   r   rs   1rr   r(   )rx   ry   r   r   r   encoder_striderM   conv2dr  pixel_shufflerm   )r7   rm   r   r   s      r*   ry   zTFSwinDecoder.__init__y  s    ""6"""l)))1,v/BBPQ[\cf * 
 
 00ECPPPr)   rV   r=   r@   c                    |}t          j        |d          }|                     |          }|                     |          }t          j        |d          }|S )Nr   r   )rG   rI   r&  r'  )r7   rV   r    s      r*   r   zTFSwinDecoder.call  sS    ]LAAM22**=99]LAAr)   Nc                   | j         rd S d| _         t          | dd           ^t          j        | j        j                  5  | j                            d d d | j        j        g           d d d            n# 1 swxY w Y   t          | dd           St          j        | j	        j                  5  | j	                            d            d d d            d S # 1 swxY w Y   d S d S )NTr&  r'  )
r   r   rG   r   r&  rs   r   rm   r   r'  r   s     r*   r   zTFSwinDecoder.build  st   : 	F
44((4t{/00 O O!!4tT[5L"MNNNO O O O O O O O O O O O O O O4$//;t1677 / /"((.../ / / / / / / / / / / / / / / / / / <;s$    )A55A9<A9/CCCrm   r   r#  r   r2  r   s   @r*   r!  r!  x  se                	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/r)   r!  z~Swin Model with a decoder on top for masked image modeling, as proposed in [SimMIM](https://huggingface.co/papers/2111.09886).c                       e Zd Zd fdZ ee           eee          e		 	 	 	 	 	 	 ddd                                    Z
ddZ xZS )TFSwinForMaskedImageModelingrm   r   c                    t                                          |           t          |ddd          | _        t	          |d          | _        d S )NFTr  )r  rn   rs   decoderrr   )rx   ry   r  r  r!  r.  r7   rm   r   s     r*   ry   z%TFSwinForMaskedImageModeling.__init__  sM       #FeTX_efff	$V)<<<r)   )r  r  NFr   r   r   r  r  r   r  r  r]   r^   r@   'tuple | TFSwinMaskedImageModelingOutputc           	     l   ||n| j         j        }|                     |||||||          }|d         }	t          j        |	d          }	t          |	          \  }
}}t          |dz            x}}t          j        |	|
|||f          }	|                     |	          }d}|U| j         j	        | j         j
        z  }t          j        |d||f          }t          j        || j         j
        d          }t          j        || j         j
        d          }t          j        |d          }t          j        |t          j                  }t          j                            t          j        |d	          t          j        |d	                    }t          j        |d          }t          j        ||z            }t          j        |          d
z   | j         j        z  }||z  }t          j        |d          }|s|f|dd         z   }||f|z   n|S t)          |||j        |j        |j                  S )aA  
        bool_masked_pos (`tf.Tensor` 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, TFSwinForMaskedImageModeling
        >>> import tensorflow as tf
        >>> 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-tiny-patch4-window7-224")
        >>> model = TFSwinForMaskedImageModeling.from_pretrained("microsoft/swin-tiny-patch4-window7-224")

        >>> num_patches = (model.config.image_size // model.config.patch_size) ** 2
        >>> pixel_values = image_processor(images=image, return_tensors="tf").pixel_values
        >>> # create random boolean mask of shape (batch_size, num_patches)
        >>> bool_masked_pos = tf.random.uniform((1, num_patches)) >= 0.5

        >>> 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, 224, 224]
        ```N)r   r  r  r  r  r]   r   r   g      ?rF   r   rC   )r   rC   r	   r   ru   r  )r0   r1   r    r!   r"   )rm   r  r  rG   rI   r   r?   rH   r.  r   r   r   r   rR   float32r   lossesmean_absolute_errorr   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_losssizer   reconstruction_loss
total_lossnum_masked_pixelsr6  s                          r*   r   z!TFSwinForMaskedImageModeling.call  sI   T &1%<kk$+B]))+/!5#  
 
 "!*,	BB4>4O4O1
L/_c1222*_z<QWY^6_`` &*\\/%B%B"&;)T[-CCD j2tT:JKKO9_dk.DaHHD9T4;#91==D>$**D74,,D"',"B"B\<887FF# #
 #%.1Da"H"H':T'ABBJ!#t!4!4t!;t{?W W'*;;NZ==N 	Z02WQRR[@F3A3M^%..SYY.5!/)#*#A
 
 
 	
r)   c                   | j         rd S d| _         t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           St          j        | j        j                  5  | j                            d            d d d            d S # 1 swxY w Y   d S d S )NTr  r.  )r   r   rG   r   r  rs   r   r.  r   s     r*   r   z"TFSwinForMaskedImageModeling.build  sP   : 	F
4&&2ty~.. & &	%%%& & & & & & & & & & & & & & &4D))5t|011 ) )""4((() ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) ) 65rB  r*  r  )r   r   r   r   r  r   r  r   r  r   r  r   r]   r^   r@   r0  r   )r#   r$   r%   ry   r   r
  r   r/   r  r   r   r   r   r   s   @r*   r,  r,    s        = = = = = = +*+@AA+JYhiii *.,0&*)-,0#'[
 [
 [
 [
 ] ji BA[
z	) 	) 	) 	) 	) 	) 	) 	)r)   r,  z
    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.
    c                       e Zd Zd fdZ ee           eeee	e
          e	 	 	 	 	 	 	 ddd                                    ZddZ xZS )TFSwinForImageClassificationrm   r   c                <   t                                          |           |j        | _        t          |d          | _        |j        dk    r&t
          j                            |j        d          n t
          j                            dd          | _	        d S )Nr  rr   r   
classifierrY  )
rx   ry   
num_labelsr  r  r   r   r   r_  r@  r/  s     r*   ry   z%TFSwinForImageClassification.__init__  s        +#F888	
  1$$ Lv0|DDD(((EE 	r)   )r  r  r  r  NFr   r   r  labelsr  r   r  r  r]   r^   r@   3tuple[tf.Tensor, ...] | TFSwinImageClassifierOutputc                D   ||n| j         j        }|                     ||||||          }|d         }	|                     |	|          }
|dn|                     ||
          }|s|
f|dd         z   }||f|z   n|S t          ||
|j        |j        |j                  S )a  
        labels (`tf.Tensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        Nr  r   r   rC   )r0   r8   r    r!   r"   )	rm   r  r  r@  hf_compute_lossr;   r    r!   r"   )r7   r   r  rB  r  r  r  r]   r!  r  r8   r0   r6  s                r*   r   z!TFSwinForImageClassification.call%  s    0 &1%<kk$+B]))/!5#  
 
  
BB~tt4+?+?+O+O 	FY,F)-)9TGf$$vE*!/)#*#A
 
 
 	
r)   c                   | j         rd S d| _         t          | dd           Pt          j        | j        j                  5  | j                            d            d d d            n# 1 swxY w Y   t          | dd           ut          | j        d          rbt          j        | j        j                  5  | j                            d d | j        j	        g           d d d            d S # 1 swxY w Y   d S d S d S )NTr  r@  rs   )
r   r   rG   r   r  rs   r   hasattrr@  r  r   s     r*   r   z"TFSwinForImageClassification.buildZ  s   : 	F
4&&2ty~.. & &	%%%& & & & & & & & & & & & & & &4t,,8t// P]4?#788 P PO))4ty7M*NOOOP P P P P P P P P P P P P P P P P P 98P Ps$    A''A+.A+6(C++C/2C/r*  r  )r   r   r  r   rB  r   r  r   r  r   r  r   r]   r^   r@   rC  r   )r#   r$   r%   ry   r   r
  r   _IMAGE_CLASS_CHECKPOINTr;   r  _IMAGE_CLASS_EXPECTED_OUTPUTr   r   r   r   r   s   @r*   r>  r>    s        
 
 
 
 
 
 +*+@AA*/$4	    *.&*#')-,0#'+
 +
 +
 +
 ]  BA+
Z
P 
P 
P 
P 
P 
P 
P 
Pr)   r>  )r>  r,  r   r  )r<   r=   r>   r?   r@   r=   )
rN   r=   r>   r?   rK   r?   rL   r?   r@   r=   )rY   FT)
rZ   r=   r[   r\   r]   r^   r_   r^   r@   r=   )r   rH  r@   rH  )Nr&   
__future__r   collections.abcr   rT   r3   r   dataclassesr   	functoolsr   typingr   r   
tensorflowrG   activations_tfr
   modeling_tf_utilsr   r   r   r   r   r   tf_utilsr   utilsr   r   r   r   r   r   configuration_swinr   
get_loggerr#   loggerr  r  r  rH  rI  r   r,   r/   r;   rO   rX   rj   r   Layerrl   rz   r   r   r   r&  r4  rD  rN  rS  r  r  r  SWIN_START_DOCSTRINGr
  r  r  r  r   r  r!  r,  r>  __all__r(   r)   r*   <module>rZ     s   % $ " " " " " "       $ $ $ $ $ $ ! ! ! ! ! !                           $ $ $ $ $ $                # " " " " "                + * * * * * 
	H	%	%  ? %  C 1  @ @ @ @ @+ @ @ @@  @  @  @  @  @  @  @  @F )# )# )# )# )#k )# )# )#X  @  @  @  @  @+  @  @  @F        ]a! ! ! ! !&A- A- A- A- A-u|) A- A- A-HAM AM AM AM AMEL. AM AM AMHF< F< F< F< F<+ F< F< F<R	N 	N 	N 	N 	NU\' 	N 	N 	NQ Q Q Q Q%,, Q Q Qh) ) ) ) )u|) ) ) )0$- $- $- $- $-el( $- $- $-N9 9 9 9 9+ 9 9 90V V V V V5<% V V V,[- [- [- [- [-%,$ [- [- [-|R& R& R& R& R&%,$ R& R& R&jV& V& V& V& V&EL& V& V& V&r% % % % %- % % %
  ,   <) <) <) <) <)u|1 <) <) <)~ jF jF jF jF jFel( jF jF jFZ d :& :& :& :& :&' :& :&	 :&z    +   4/ / / / /EL& / / /> ; 
q) q) q) q) q)#8 q) q) 
q)h   MP MP MP MP MP#8:V MP MP MP` s
r
rr)   