
    ^jܯ                     V   d Z ddlZddlZddlm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 dd	lmZmZ dd
lmZ ddlmZmZmZ ddlmZ  ej4                  e      Z ed      e G d de                    Zd Zd Z G d dej@                        Z! G d dej@                        Z" G d dej@                        Z# G d dej@                        Z$ G d dej@                        Z% G d dej@                        Z& G d  d!ej@                        Z' G d" d#ej@                        Z( G d$ d%ej@                        Z) G d& d'ej@                        Z* G d( d)ej@                        Z+ G d* d+e      Z, G d, d-ej@                        Z-e G d. d/e             Z.e G d0 d1e.             Z/ G d2 d3ej@                        Z0 G d4 d5ej@                        Z1 G d6 d7ej@                        Z2 G d8 d9ej@                        Z3 G d: d;ej@                        Z4 ed<       G d= d>e.             Z5g d?Z6y)@z"PyTorch Swin2SR Transformer model.    N)	dataclass)nn   )initialization)ACT2FN)GradientCheckpointingLayer)BaseModelOutputImageSuperResolutionOutput)PreTrainedModel)ModelOutputauto_docstringlogging   )Swin2SRConfigzQ
    Swin2SR encoder's outputs, with potential hidden states and attentions.
    )custom_introc                       e Zd ZU dZej
                  dz  ed<   dZeej
                     dz  ed<   dZ	eej
                     dz  ed<   y)Swin2SREncoderOutputNlast_hidden_statehidden_states
attentions)
__name__
__module____qualname__r   torchFloatTensor__annotations__r   tupler        w/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/swin2sr/modeling_swin2sr.pyr   r   #   sN     37u((4/659M5**+d2926Je''(4/6r   r   c                     | j                   \  }}}}| j                  |||z  |||z  ||      } | j                  dd      j                         j                  d|||      }|S )z2
    Partitions the given input into windows.
       r   shapeview	transpose
contiguous)input_featurewindow_size
batch_sizeheightwidthnum_channelswindowss          r    window_partitionr0   0   st     /<.A.A+J|!&&Fk);8Lk[gM %%a+668==b+{\hiGNr   c                     | j                   d   }| j                  d||z  ||z  |||      } | j                  dd      j                         j                  d|||      } | S )z?
    Merges windows to produce higher resolution features.
    r#   r"   r   r$   )r/   r*   r,   r-   r.   s        r    window_reverser2   =   sg     ==$Lll2v4e{6JKYdfrsG1%00277FE<XGNr   c                   f     e Zd ZdZ fdZdej                  dz  deej                     fdZ	 xZ
S )Swin2SREmbeddingsz?
    Construct the patch and optional position embeddings.
    c                 x   t         |           t        |      | _        | j                  j                  }|j
                  r=t        j                  t        j                  d|dz   |j                              | _        nd | _        t        j                  |j                        | _        |j                  | _        y )Nr   )super__init__Swin2SRPatchEmbeddingspatch_embeddingsnum_patchesuse_absolute_embeddingsr   	Parameterr   zeros	embed_dimposition_embeddingsDropouthidden_dropout_probdropoutr*   )selfconfigr:   	__class__s      r    r7   zSwin2SREmbeddings.__init__L   s     6v >++77))')||EKK;QR?TZTdTd4e'fD$'+D$zz&"<"<=!--r   pixel_valuesNreturnc                     | j                  |      \  }}| j                  || j                  z   }| j                  |      }||fS N)r9   r?   rB   )rC   rF   
embeddingsoutput_dimensionss       r    forwardzSwin2SREmbeddings.forwardZ   sN    (,(=(=l(K%
%##/#d&>&>>J\\*-
,,,r   )r   r   r   __doc__r7   r   r   r   TensorrL   __classcell__rE   s   @r    r4   r4   G   s4    .-E$5$5$< -u||AT -r   r4   c                   n     e Zd Zd fd	Zdej
                  dz  deej                  ee   f   fdZ	 xZ
S )r8   c                    t         |           |j                  }|j                  |j                  }}t        |t        j                  j                        r|n||f}t        |t        j                  j                        r|n||f}|d   |d   z  |d   |d   z  g}|| _	        |d   |d   z  | _
        t        j                  ||j                  ||      | _        |r%t        j                  |j                        | _        y d | _        y )Nr   r   )kernel_sizestride)r6   r7   r>   
image_size
patch_size
isinstancecollectionsabcIterablepatches_resolutionr:   r   Conv2d
projection	LayerNorm	layernorm)rC   rD   normalize_patchesr.   rU   rV   r[   rE   s          r    r7   zSwin2SRPatchEmbeddings.__init__f   s    ''!'!2!2F4E4EJ
#-j+//:R:R#SZZdfpYq
#-j+//:R:R#SZZdfpYq
(mz!}<jmzZ[}>\]"4-a03Ea3HH))L&2B2BPZcmn;Lf&6&67RVr   rJ   NrG   c                     | j                  |      }|j                  \  }}}}||f}|j                  d      j                  dd      }| j                  | j	                  |      }||fS )Nr"   r   )r]   r%   flattenr'   r_   )rC   rJ   _r,   r-   rK   s         r    rL   zSwin2SRPatchEmbeddings.forwardt   so    __Z0
(..1fe#UO''*44Q:
>>%
3J,,,r   )T)r   r   r   r7   r   r   r   rN   intrL   rO   rP   s   @r    r8   r8   e   s<    W	-%"3"3d": 	-uU\\SXY\S]E]?^ 	-r   r8   c                   (     e Zd ZdZ fdZd Z xZS )Swin2SRPatchUnEmbeddingszImage to Patch Unembeddingc                 D    t         |           |j                  | _        y rI   )r6   r7   r>   )rC   rD   rE   s     r    r7   z!Swin2SRPatchUnEmbeddings.__init__   s    ))r   c                     |j                   \  }}}|j                  dd      j                  || j                  |d   |d         }|S )Nr   r"   r   )r%   r'   r&   r>   )rC   rJ   x_sizer+   height_widthr.   s         r    rL   z Swin2SRPatchUnEmbeddings.forward   sO    1;1A1A.
L,))!Q/44ZQWXYQZ\bcd\ef
r   r   r   r   rM   r7   rL   rO   rP   s   @r    rf   rf      s    %*
r   rf   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 )Swin2SRPatchMerginga'  
    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_resolutiondim
norm_layerrG   Nc                     t         |           || _        || _        t	        j
                  d|z  d|z  d      | _         |d|z        | _        y )N   r"   Fbias)r6   r7   rn   ro   r   Linear	reductionnorm)rC   rn   ro   rp   rE   s       r    r7   zSwin2SRPatchMerging.__init__   sI     01s7AG%@q3w'	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   
functionalpad)rC   r)   r,   r-   
should_pad
pad_valuess         r    	maybe_padzSwin2SRPatchMerging.maybe_pad   sU    qjAo:519>
Q519a!<JMM--mZHMr   r)   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   r#   rr   )r%   r&   r}   r   catrv   rw   )rC   r)   r~   r,   r-   r+   ro   r.   input_feature_0input_feature_1input_feature_2input_feature_3s               r    rL   zSwin2SRPatchMerging.forward   s   ((5(;(;%
C%**:vulS}feD'14a4Aq(89'14a4Aq(89'14a4Aq(89'14a4Aq(89		?O_Ve"fhjk%**:r1|;KL}5		-0r   )r   r   r   rM   r   r^   r   rd   Moduler7   r}   r   rN   rL   rO   rP   s   @r    rm   rm      sr    
 XZWcWc (s (# (299 (hl (U\\ U3PS8_ Y^YeYe r   rm   c            
            e Zd Zddgf fd	Z	 	 d
dej
                  dej                  dz  dedz  deej
                     fdZ	d	 Z
 xZS )Swin2SRSelfAttentionr   c           
         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t        j"                  |ddf      z              | _        t        j&                  t        j(                  ddd	
      t        j*                  d	      t        j(                  d|d
            | _        | j/                         \  }}| j1                  d|d       | j1                  d|d       t        j(                  | j                  | j                  |j2                  
      | _        t        j(                  | j                  | j                  d
      | _        t        j(                  | j                  | j                  |j2                  
      | _        t        j:                  |j<                        | _        y )Nr   zThe hidden size (z6) is not a multiple of the number of attention heads ()
   r   r"   i   Trs   inplaceFrelative_coords_table
persistentrelative_position_index) r6   r7   
ValueErrornum_attention_headsrd   attention_head_sizeall_head_sizerW   rX   rY   rZ   r*   pretrained_window_sizer   r<   r   logoneslogit_scale
Sequentialru   ReLUcontinuous_position_bias_mlpcreate_coords_table_and_indexregister_bufferqkv_biasquerykeyvaluer@   attention_probs_dropout_probrB   )	rC   rD   ro   	num_headsr*   r   r   r   rE   s	           r    r7   zSwin2SRSelfAttention.__init__   s   ?a#C5(^_h^iijk  $- #&sY#7 !558P8PP%k;??3K3KLKS^`kRl 	 '=#<<		"uzz9aQRBS7T2T(UV,.MMIIa4("''$*?3PY`eAf-
) :>9[9[9]6646KX]^68O\abYYt1143E3EFOO\
99T//1C1C%PYYt1143E3EFOO\
zz&"E"EFr   Nr   attention_maskoutput_attentionsrG   c                 R   |j                   \  }}}| j                  |      j                  |d| j                  | j                        j                  dd      }| j                  |      j                  |d| j                  | j                        j                  dd      }| j                  |      j                  |d| j                  | j                        j                  dd      }	t        j                  j                  |d      t        j                  j                  |d      j                  dd      z  }
t        j                  | j                  t        j                  d            j!                         }|
|z  }
| j#                  | j$                        j                  d| j                        }|| j&                  j                  d         j                  | j(                  d   | j(                  d   z  | j(                  d   | j(                  d   z  d      }|j+                  ddd      j-                         }d	t        j.                  |      z  }|
|j1                  d      z   }
||j                   d   }|
j                  ||z  || j                  ||      |j1                  d      j1                  d      z   }
|
|j1                  d      j1                  d      z   }
|
j                  d| j                  ||      }
t        j                  j3                  |
d      }| j5                  |      }t        j6                  ||	      }|j+                  dddd
      j-                         }|j9                         d d | j:                  fz   }|j                  |      }|r||f}|S |f}|S )Nr#   r   r"   )ro   g      Y@)maxr      r   )r%   r   r&   r   r   r'   r   r   r   ry   	normalizer   clampr   mathr   expr   r   r   r*   permuter(   sigmoid	unsqueezesoftmaxrB   matmulsizer   )rC   r   r   r   r+   ro   r.   query_layer	key_layervalue_layerattention_scoresr   relative_position_bias_tablerelative_position_bias
mask_shapeattention_probscontext_layernew_context_layer_shapeoutputss                      r    rL   zSwin2SRSelfAttention.forward   sx    )6(;(;%
CJJ}%T*b$":":D<T<TUYq!_ 	 HH]#T*b$":":D<T<TUYq!_ 	 JJ}%T*b$":":D<T<TUYq!_ 	 ==22;B2G"--JaJa2 Kb K

)B
 kk$"2"28LMQQS+k9'+'H'HIcIc'd'i'i(((
$ ">d>Z>Z>_>_`b>c!d!i!iQ$"2"21"55t7G7G7JTM]M]^_M`7`bd"
 "8!?!?1a!H!S!S!U!#emm4J&K!K+.D.N.Nq.QQ%'--a0J/44j(*d6N6NPSUX ((+55a8 9  0.2J2J12M2W2WXY2ZZ/44R9Q9QSVX[\ --//0@b/I ,,7 _kB%--aAq9DDF"/"4"4"6s";t?Q?Q>S"S%**+BC6G=/2 O\M]r   c                 8   t        j                  | j                  d   dz
   | j                  d   t         j                        j	                         }t        j                  | j                  d   dz
   | j                  d   t         j                        j	                         }t        j
                  t        j                  ||gd            j                  ddd      j                         j                  d      }| j                  d   dkD  rO|d d d d d d dfxx   | j                  d   dz
  z  cc<   |d d d d d d dfxx   | j                  d   dz
  z  cc<   n`| j                  d   dkD  rN|d d d d d d dfxx   | j                  d   dz
  z  cc<   |d d d d d d dfxx   | j                  d   dz
  z  cc<   |dz  }t        j                  |      t        j                  t        j                  |      dz         z  t        j                  d      z  }|j                  t!        | j"                  j%                               j&                        }t        j                  | j                  d         }t        j                  | j                  d         }t        j
                  t        j                  ||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	      }	||	fS )
Nr   r   dtypeij)indexingr"      g      ?r#   )r   aranger*   int64floatstackmeshgridr   r(   r   r   signlog2absr   tonextr   
parametersr   rb   sum)
rC   relative_coords_hrelative_coords_wr   coords_hcoords_wcoordscoords_flattenrelative_coordsr   s
             r    r   z2Swin2SRSelfAttention.create_coords_table_and_index(  s?   !LL4+;+;A+>+B)CTEUEUVWEX`e`k`klrrt!LL4+;+;A+>+B)CTEUEUVWEX`e`k`klrrtKK(9;L'MX\]^WQ1Z\Yq\	 	 &&q)A-!!Q1*-1L1LQ1ORS1SS-!!Q1*-1L1LQ1ORS1SS-a 1$!!Q1*-1A1A!1Dq1HH-!!Q1*-1A1A!1Dq1HH-"JJ,-

599EZ;[^a;a0bbeienenopeqq 	 !6 8 8d>_>_>j>j>l9m9s9s t << 0 0 34<< 0 0 34U^^Xx,@4PQvq1(At4~aqj7QQ)11!Q:EEG1a D$4$4Q$7!$;; 1a D$4$4Q$7!$;; 1a A(8(8(;$;a$?? "1"5"5b"9$&===r   NF)r   r   r   r7   r   rN   r   boolr   rL   r   rO   rP   s   @r    r   r      sm    TUWXSY G@ 48).	B||B ))D0B  $;	B
 
u||	BH#>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 )Swin2SRSelfOutputc                     t         |           t        j                  ||      | _        t        j
                  |j                        | _        y rI   )r6   r7   r   ru   denser@   r   rB   rC   rD   ro   rE   s      r    r7   zSwin2SRSelfOutput.__init__P  s6    YYsC(
zz&"E"EFr   r   input_tensorrG   c                 J    | j                  |      }| j                  |      }|S rI   r   rB   )rC   r   r   s      r    rL   zSwin2SRSelfOutput.forwardU  s$    

=1]3r   r   r   r   r7   r   rN   rL   rO   rP   s   @r    r   r   O  s2    G
U\\  RWR^R^ r   r   c            
            e Zd Zd fd	Z	 	 d	dej
                  dej                  dz  dedz  deej
                     fdZ	 xZ
S )
Swin2SRAttentionc           
          t         |           t        ||||t        |t        j
                  j                        r|n||f      | _        t        ||      | _	        y )NrD   ro   r   r*   r   )
r6   r7   r   rW   rX   rY   rZ   rC   r   output)rC   rD   ro   r   r*   r   rE   s         r    r7   zSwin2SRAttention.__init__^  sY    (#0+//2J2JK $:(*@A
	 (4r   Nr   r   r   rG   c                 h    | j                  |||      }| j                  |d   |      }|f|dd  z   }|S Nr   r   )rC   r   )rC   r   r   r   self_outputsattention_outputr   s          r    rL   zSwin2SRAttention.forwardk  sE     yy@QR;;|AF#%QR(88r   r   r   )r   r   r   r7   r   rN   r   r   r   rL   rO   rP   s   @r    r   r   ]  sW    5  48).		||	 ))D0	  $;		
 
u||		r   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )Swin2SRIntermediatec                    t         |           t        j                  |t	        |j
                  |z              | _        t        |j                  t              rt        |j                     | _        y |j                  | _        y rI   )r6   r7   r   ru   rd   	mlp_ratior   rW   
hidden_actstrr   intermediate_act_fnr   s      r    r7   zSwin2SRIntermediate.__init__y  sa    YYsC(8(83(>$?@
f''-'-f.?.?'@D$'-'8'8D$r   r   rG   c                 J    | j                  |      }| j                  |      }|S rI   )r   r   rC   r   s     r    rL   zSwin2SRIntermediate.forward  s&    

=100?r   r   rP   s   @r    r   r   x  s#    9U\\ ell r   r   c                   V     e Zd Z fdZdej
                  dej
                  fdZ xZS )Swin2SROutputc                     t         |           t        j                  t	        |j
                  |z        |      | _        t        j                  |j                        | _	        y rI   )
r6   r7   r   ru   rd   r   r   r@   rA   rB   r   s      r    r7   zSwin2SROutput.__init__  sF    YYs6#3#3c#9:C@
zz&"<"<=r   r   rG   c                 J    | j                  |      }| j                  |      }|S rI   r   r   s     r    rL   zSwin2SROutput.forward  s$    

=1]3r   r   rP   s   @r    r   r     s#    >
U\\ ell r   r   c                   r     e Zd ZdZd	deddf fdZdej                  dej                  fdZde	fdZ
 xZS )
Swin2SRDropPathzStochastic depth (DropPath) per sample, for residual blocks.

    Identity when ``drop_prob`` is 0 or outside training. See `Deep Networks with Stochastic Depth
    <https://arxiv.org/abs/1603.09382>`_.
    	drop_probrG   Nc                 0    t         |           || _        y rI   )r6   r7   r   )rC   r   rE   s     r    r7   zSwin2SRDropPath.__init__  s    "r   r   c                 P   | j                   dk(  s| j                  s|S d| j                   z
  }|j                  d   fd|j                  dz
  z  z   }t	        j
                  ||j                  |j                        }t	        j                  ||z         }|j                  |      |z  S )N        r   r   )r   )r   device)
r   trainingr%   ndimr   randr   r   floordiv)rC   r   	keep_probr%   random_tensors        r    rL   zSwin2SRDropPath.forward  s    >>S   &	$$Q')DM4F4F4J,KK

50C0CML`L`aMI$=>  +m;;r   c                      d| j                    S )Nzp=)r   rC   s    r    
extra_reprzSwin2SRDropPath.extra_repr  s    DNN#$$r   )r   )r   r   r   rM   r   r7   r   rN   rL   r   r
  rO   rP   s   @r    r   r     sB    #% #$ #<U\\ <ell <%C %r   r   c                        e Zd Z	 d fd	Zdeeeef   eeef   f   fdZd Zd Z	 dde	j                  deeef   ded	z  dee	j                  e	j                  f   fd
Z xZS )Swin2SRLayerc           
      n   t         	|           || _        | j                  |j                  |j                  f||f      \  }}|d   | _        |d   | _        t        |||| j                  t        |t        j                  j                        r|n||f      | _        t        j                  ||j                        | _        |dkD  rt!        |      nt        j"                         | _        t'        ||      | _        t+        ||      | _        t        j                  ||j                        | _        y )Nr   r   epsr   )r6   r7   rn   _compute_window_shiftr*   
shift_sizer   rW   rX   rY   rZ   	attentionr   r^   layer_norm_epslayernorm_beforer   Identity	drop_pathr   intermediater   r   layernorm_after)
rC   rD   ro   rn   r   drop_path_rater  r   r*   rE   s
            r    r7   zSwin2SRLayer.__init__  s    	 0"&"<"<!3!34z:6N#
Z 'q>$Q-)((0+//2J2JK $:(*@A
 !#Sf6K6K L<JS<P8VXVaVaVc/<#FC0!||CV5J5JKr   rG   c                     t        | j                  |      D cg c]  \  }}t        ||       }}}t        | j                  ||      D cg c]  \  }}}||k  rdn| }}}}||fS c c}}w c c}}}w Nr   )ziprn   min)rC   target_window_sizetarget_shift_sizerwr*   sr  s           r    r  z"Swin2SRLayer._compute_window_shift  sy    -01F1FHZ-[\TQs1ay\\8;D<Q<QS^`q8rssWQ116aq(s
sJ&& ]ss   A*A0c                    | j                   dk  ryt        j                  |      }t        j                  |      }||| j                  z
  k\  j	                         ||| j                   z
  k\  j	                         z   }||| j                  z
  k\  j	                         ||| j                   z
  k\  j	                         z   }|dddddf   dz  |dddddf   z   j                  |      }t        || j                        }	|	j                  d| j                  | j                  z        }	|	j                  d      |	j                  d      z
  }
|
j                  |
dk7  d      j                  |
dk(  d      }
|
S )	u  Build the cyclic-shift attention mask for shifted-window MSA; returns None when shift_size is 0.

        Each (h, w) position belongs to one of 9 cyclic-shift regions (3 along each axis), encoded
        as ``h_region * 3 + w_region``. Regions per axis:
        - 0: indices ``[0, axis - window_size)``
        - 1: indices ``[axis - window_size, axis - shift_size)``
        - 2: indices ``[axis - shift_size, axis)``
        Implementation note: a single arithmetic pass on `torch.arange` (two comparisons +
        broadcast add) replaces the original 9-iteration nested-Python-loop slice-assignment —
        fully vectorised, no per-cell host-side scatter, no GPU↔host sync.
        r   Nr   r#   r   r"   g      Yr   )
r  r   r   r*   longr   r0   r&   r   masked_fill)rC   r,   r-   r   h_idxw_idxh_regionw_regionimg_maskmask_windows	attn_masks              r    get_attn_maskzSwin2SRLayer.get_attn_mask  se    ??aV$U#Vd&6&666<<>%6TXTcTcKcBcAiAiAkkUT%5%555;;=%RVRaRaJaAa@g@g@iiT1dD01A5tQPTAT8UUYYZ_`'$2B2BC#((T-=-=@P@P-PQ **1-0F0Fq0II	)))q.&AMMi[\n^ab	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  )r*   r   ry   rz   )rC   r   r,   r-   	pad_right
pad_bottomr|   s          r    r}   zSwin2SRLayer.maybe_pad  s    %%0@0@(@@DDTDTT	&&$2B2B)BBdFVFVV
Ay!Z8
))-Dj((r   r   r~   r   Nc                    |\  }}|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                        }| 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!                  |      }|	| j#                  |      z   }| j%                  |      }| j'                  |      }|| j#                  | j)                  |            z   }|r	||d	   f}|S |f}|S )
Nr   )r   r"   )shiftsdimsr#   r   )r   r      r   )r   r&   r}   r%   r  r   rollr0   r*   r-  r   r   r   r  r2   r(   r  r  r  r   r  )rC   r   r~   r   r,   r-   r+   rc   channelsshortcutr|   
height_pad	width_padshifted_hidden_stateshidden_states_windowsr,  attention_outputsr   attention_windowsshifted_windows
was_paddedlayer_outputlayer_outputss                          r    rL   zSwin2SRLayer.forward  s    )"/"4"4"6
Ax  &**:vuhO$(NN=&%$P!z&3&9&9#:y!??Q$)JJ}tFVY]YhYhXhEipv$w!$1! !11FHXHX Y 5 : :2t?O?ORVRbRb?bdl m&&z9MDWDW&X	 !%:%A%ABI NN+@)_pNq,Q/,11"d6F6FHXHXZbc():D<L<LjZcd ??Q %

?DOOUYUdUdCelr s /]Q&;*Q-!*;
 1!WfWfufa2G H S S U-22:v~xX--.?@ 4>>-#@@((7{{<0$t~~d6J6J<6X'YY@Q'8';< YeWfr   )r   r   r   F)r   r   r   r7   r   rd   r  r-  r}   r   rN   r   rL   rO   rP   s   @r    r  r    s    qrL2'eTYZ]_bZbTcejknpsksetTtNu '
2) */	5||5  S/5  $;	5
 
u||U\\)	*5r   r  c            
       ~     e Zd ZdZd	 fd	Z	 d
dej                  deeef   de	dz  deej                     fdZ
 xZS )Swin2SRStagezh
    This corresponds to the Residual Swin Transformer Block (RSTB) in the original implementation.
    c                    t         	|           || _        || _        t	        j
                  t        |      D cg c]*  }t        |||||dz  dk(  rdn|j                  dz  |      , c}      | _	        |j                  dk(  rt	        j                  ||ddd      | _        n|j                  dk(  rt	        j                  t	        j                  ||dz  ddd      t	        j                  d	d
      t	        j                  |dz  |dz  ddd      t	        j                  d	d
      t	        j                  |dz  |ddd            | _        t        |d      | _        t#        |      | _        y c c}w )Nr"   r   )rD   ro   rn   r   r  r   1convr   r   3convrr   皙?Tnegative_sloper   F)r`   )r6   r7   rD   ro   r   
ModuleListranger  r*   layersresi_connectionr\   convr   	LeakyReLUr8   patch_embedrf   patch_unembed)
rC   rD   ro   rn   depthr   r  r   irE   s
            r    r7   zSwin2SRStage.__init__*  sI   mm u
  !%5'%&UaZqf6H6HA6M+A

 !!W,		#sAq!4DI##w.		#saxAq1C>		#(C1HaA6C>		#(CAq1DI 2&ER5f=7
s   /E.r   r~   r   NrG   c                    |}|\  }}t        | j                        D ]  \  }} ||||      }	|	d   } ||||f}
| j                  ||      }| j                  |      }| j	                  |      \  }}||z   }||
f}|r|	dd  z  }|S r   )	enumeraterM  rR  rO  rQ  )rC   r   r~   r   residualr,   r-   rT  layer_modulerA  rK   rc   stage_outputss                r    rL   zSwin2SRStage.forwardL  s     !((5 	-OA|(8HJ[\M)!,M	-
 $UFE:**=:JK		-0++M:q%0&(9:]12..Mr   r   rB  )r   r   r   rM   r7   r   rN   r   rd   r   rL   rO   rP   s   @r    rD  rD  %  sW     >L */	||  S/  $;	
 
u||	r   rD  c                   |     e Zd Z fdZ	 	 	 d
dej
                  deeef   dedz  dedz  dedz  dee	z  fd	Z
 xZS )Swin2SREncoderc                 z   t         |           t        |j                        | _        || _        t        j                  d|j                  t        |j                        d      D cg c]  }|j                          }}t        j                  t        | j                        D cg c]r  }t        ||j                  |d   |d   f|j                  |   |j                   |   |t        |j                  d |       t        |j                  d |dz           d      t c}      | _        d| _        y c c}w c c}w )Nr   cpu)r   r   )rD   ro   rn   rS  r   r  r   F)r6   r7   lendepths
num_stagesrD   r   linspacer  r   itemr   rK  rL  rD  r>   r   stagesgradient_checkpointing)rC   rD   	grid_sizexdpr	stage_idxrE   s         r    r7   zSwin2SREncoder.__init__j  s   fmm,!&63H3H#fmmJ\ej!klAqvvxllmm "'t!7  !((&/lIaL%A --	2$..y9!#fmmJY&?"@3v}}UdW`cdWdGeCfg+,
 ',#! ms   )D3(A7D8r   r~   r   Noutput_hidden_statesreturn_dictrG   c                     d}|rdnd }|rdnd }|r||fz  }t        | j                        D ]<  \  }	}
 |
|||      }|d   }|d   }|d   |d   f}||fz  }|r||fz  }|s5||dd  z  }> |st        d |||fD              S t        |||      S )	Nr   r   r   r   r#   r"   c              3   &   K   | ]	  }||  y wrI   r   ).0vs     r    	<genexpr>z)Swin2SREncoder.forward.<locals>.<genexpr>  s     mq_`_lms   r   r   r   )rV  rc  r   r   )rC   r   r~   r   ri  rj  all_input_dimensionsall_hidden_statesall_self_attentionsrT  stage_modulerA  rK   s                r    rL   zSwin2SREncoder.forward  s      ""6BD$5b4-!11(5 	9OA|(8HJ[\M)!,M -a 0 1" 57H7LM %5$77 #!m%55! #}QR'88#	9 m]4EGZ$[mmm#++*
 	
r   )FFT)r   r   r   r7   r   rN   r   rd   r   r   rL   rO   rP   s   @r    r[  r[  i  sp    ,4 */,1#'%
||%
  S/%
  $;	%

 #Tk%
 D[%
 
%	%%
r   r[  c                   b     e Zd ZU eed<   dZdZdZdZ e	j                          fd       Z xZS )Swin2SRPreTrainedModelrD   swin2srrF   )imageTc                    t         |   |       t        |t        j                  t        j
                  f      rbt        j                  |j                  | j                  j                         |j                   t        j                  |j                         yyt        |t              rt        j                  |j                  t!        j"                  d             |j%                         \  }}t        j&                  |j(                  |       t        j&                  |j*                  |       yt        |t,              r|j                  j.                  dk(  rC|j                  j0                  dk(  r*t3        j4                  g d      j7                  dddd      }nt3        j8                  dddd      }t        j&                  |j:                  |       yy)zInitialize the weights)stdNr   r   gw#?g8EGr?gB`"?r   )r6   _init_weightsrW   r   ru   r\   inittrunc_normal_weightrD   initializer_rangert   zeros_r   	constant_r   r   r   r   copy_r   r   Swin2SRModelr.   num_channels_outr   tensorr&   r=   mean)rC   moduler   r   r  rE   s        r    r|  z$Swin2SRPreTrainedModel._init_weights  sE    	f%fryy"))45v}}$++2O2OP{{&FKK( ' 45NN6--txx|<=C=a=a=c:!#:JJv335JKJJv557NO-}}))Q.6==3Q3QUV3V||$<=BB1aAN{{1aA.JJv{{D) .r   )r   r   r   r   r   base_model_prefixmain_input_nameinput_modalitiessupports_gradient_checkpointingr   no_gradr|  rO   rP   s   @r    rv  rv    s:    !$O!&*#U]]_* *r   rv  c                        e Zd Z fdZd Zd Ze	 	 	 ddej                  de	dz  de	dz  de	dz  d	e
ez  f
d
       Z xZS )r  c                 $   t         |   |       || _        |j                  dk(  r9|j                  dk(  r*t        j                  g d      j                  dddd      }nt        j                  dddd      }| j                  d|d       |j                  | _
        t        j                  |j                  |j                  ddd      | _        t        |      | _        t#        || j                   j$                  j&                        | _        t        j*                  |j                  |j,                        | _        t1        |      | _        t        j                  |j                  |j                  ddd      | _        | j7                          y )	Nr   r{  r   r  Fr   )re  r  )r6   r7   rD   r.   r  r   r  r&   r=   r   	img_ranger   r\   r>   first_convolutionr4   rJ   r[  r9   r[   encoderr^   r  r_   rf   rR  conv_after_body	post_init)rC   rD   r  rE   s      r    r7   zSwin2SRModel.__init__  s8    !#(?(?1(D<< 89>>q!QJD;;q!Q*DVTe<))!#6+>+>@P@PRSUVXY!Z+F3%f8X8X8k8klf&6&6F<Q<QR5f=!yy)9)96;K;KQPQSTU 	r   c                 .    | j                   j                  S rI   )rJ   r9   r	  s    r    get_input_embeddingsz!Swin2SRModel.get_input_embeddings  s    ///r   c                 X   |j                         \  }}}}| j                  j                  }|||z  z
  |z  }|||z  z
  |z  }t        j                  j                  |d|d|fd      }| j                  j                  |j                  |j                        }||z
  | j                  z  }|S )Nr   reflect)r   r   )r   rD   r*   r   ry   rz   r  r   r   r   r  )	rC   rF   rc   r,   r-   r*   modulo_pad_heightmodulo_pad_widthr  s	            r    pad_and_normalizezSwin2SRModel.pad_and_normalize  s    *//11fe kk--(6K+??;N'%+*==L}}((;KQPa7bdmn yy||<#6#6l>P>P|Q$t+t~~=r   NrF   r   ri  rj  rG   c                 2   ||n| j                   j                  }||n| j                   j                  }||n| j                   j                  }|j                  \  }}}}| j                  |      }| j                  |      }	| j                  |	      \  }
}| j                  |
||||      }|d   }| j                  |      }| j                  |||f      }| j                  |      |	z   }|s|f|dd  z   }|S t        ||j                  |j                        S )Nr   ri  rj  r   r   rp  )rD   r   ri  rj  r%   r  r  rJ   r  r_   rR  r  r	   r   r   )rC   rF   r   ri  rj  kwargsrc   r,   r-   rJ   embedding_outputr~   encoder_outputssequence_outputr   s                  r    rL   zSwin2SRModel.forward  sB    2C1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++BYBY*001fe --l;++L9
-1__Z-H**,,/!5# ' 
 *!,..9,,_vuoN..?*L%'/!"*==FM-)77&11
 	
r   )NNN)r   r   r   r7   r  r  r   r   r   r   r   r	   rL   rO   rP   s   @r    r  r    sx    .0  *.,0#'-
''-
  $;-
 #Tk	-

 D[-
 
	 -
 -
r   r  c                   (     e Zd ZdZ fdZd Z xZS )UpsamplezUpsample module.

    Args:
        scale (`int`):
            Scale factor. Supported scales: 2^n and 3.
        num_features (`int`):
            Channel number of intermediate features.
    c                    t         |           || _        ||dz
  z  dk(  rt        t	        t        j                  |                  D ]Y  }| j                  d| t        j                  |d|z  ddd             | j                  d| t        j                  d             [ y |dk(  r<t        j                  |d|z  ddd      | _        t        j                  d      | _        y t        d	| d
      )Nr   r   convolution_rr   r   pixelshuffle_r"   	   zScale z/ is not supported. Supported scales: 2^n and 3.)r6   r7   scalerL  rd   r   r   
add_moduler   r\   PixelShuffleconvolutionpixelshuffler   )rC   r  num_featuresrT  rE   s       r    r7   zUpsample.__init__+  s    
UQYA%3tyy/01 I,qc 2BIIlAP\L\^_abde4fg-s 3R__Q5GHI aZ!yyq<7GAqQD " 2DveW,[\]]r   c                 ~   | j                   | j                   dz
  z  dk(  rlt        t        t        j                  | j                                     D ]6  } | j                  d|       |      } | j                  d|       |      }8 |S | j                   dk(  r"| j                  |      }| j                  |      }|S )Nr   r   r  r  r   )r  rL  rd   r   r   __getattr__r  r  )rC   hidden_staterT  s      r    rL   zUpsample.forward:  s    JJ$**q.)a/3tyy456 SCt//,qc0BCLQDt//-s0CD\RS 	 ZZ1_++L9L,,\:Lr   rk   rP   s   @r    r  r  !  s    ^
r   r  c                   (     e Zd ZdZ fdZd Z xZS )UpsampleOneStepa  UpsampleOneStep module (the difference with Upsample is that it always only has 1conv + 1pixelshuffle)

    Used in lightweight SR to save parameters.

    Args:
        scale (int):
            Scale factor. Supported scales: 2^n and 3.
        in_channels (int):
            Channel number of intermediate features.
        out_channels (int):
            Channel number of output features.
    c                     t         |           t        j                  ||dz  |z  ddd      | _        t        j
                  |      | _        y )Nr"   r   r   )r6   r7   r   r\   rO  r  pixel_shuffle)rC   r  in_channelsout_channelsrE   s       r    r7   zUpsampleOneStep.__init__U  s@    IIkE1H+DaAN	__U3r   c                 J    | j                  |      }| j                  |      }|S rI   )rO  r  )rC   rf  s     r    rL   zUpsampleOneStep.forward[  s$    IIaLq!r   rk   rP   s   @r    r  r  G  s    4r   r  c                   $     e Zd Z fdZd Z xZS )PixelShuffleUpsamplerc                 .   t         |           t        j                  |j                  |ddd      | _        t        j                  d      | _        t        |j                  |      | _
        t        j                  ||j                  ddd      | _        y Nr   r   Tr   )r6   r7   r   r\   r>   conv_before_upsamplerP  
activationr  upscaleupsampler  final_convolutionrC   rD   r  rE   s      r    r7   zPixelShuffleUpsampler.__init__c  ss    $&IIf.>.>aQRTU$V!,,t4 >!#<9P9PRSUVXY!Zr   c                     | j                  |      }| j                  |      }| j                  |      }| j                  |      }|S rI   )r  r  r  r  )rC   r  rf  s      r    rL   zPixelShuffleUpsampler.forwardj  sC    %%o6OOAMM!""1%r   r   r   r   r7   rL   rO   rP   s   @r    r  r  b  s    [r   r  c                   $     e Zd Z fdZd Z xZS )NearestConvUpsamplerc                    t         |           |j                  dk7  rt        d      t	        j
                  |j                  |ddd      | _        t	        j                  d      | _	        t	        j
                  ||ddd      | _
        t	        j
                  ||ddd      | _        t	        j
                  ||ddd      | _        t	        j
                  ||j                  ddd      | _        t	        j                  dd      | _        y )	Nrr   zNThe nearest+conv upsampler only supports an upscale factor of 4 at the moment.r   r   Tr   rH  rI  )r6   r7   r  r   r   r\   r>   r  rP  r  conv_up1conv_up2conv_hrr  r  lrelur  s      r    r7   zNearestConvUpsampler.__init__t  s    >>Qmnn$&IIf.>.>aQRTU$V!,,t4		,aAF		,aAFyy|Q1E!#<9P9PRSUVXY!Z\\dC
r   c           	         | j                  |      }| j                  |      }| j                  | j                  t        j
                  j                  j                  |dd                  }| j                  | j                  t        j
                  j                  j                  |dd                  }| j                  | j                  | j                  |                  }|S )Nr"   nearest)scale_factormode)r  r  r  r  r   r   ry   interpolater  r  r  )rC   r  reconstructions      r    rL   zNearestConvUpsampler.forward  s    33OD///:**MM%((--99/XY`i9jk
 **MM%((--99/XY`i9jk
 //

4<<;X0YZr   r  rP   s   @r    r  r  s  s    D
r   r  c                   $     e Zd Z fdZd Z xZS )PixelShuffleAuxUpsamplerc           	      |   t         |           |j                  | _        t        j                  |j
                  |ddd      | _        t        j                  |j                  |ddd      | _        t        j                  d      | _
        t        j                  ||j
                  ddd      | _        t        j                  t        j                  d|ddd      t        j                  d            | _        t        |j                  |      | _        t        j                  ||j                   ddd      | _        y r  )r6   r7   r  r   r\   r.   conv_bicubicr>   r  rP  r  conv_auxr   conv_after_auxr  r  r  r  r  s      r    r7   z!PixelShuffleAuxUpsampler.__init__  s    ~~IIf&9&9<AqQ$&IIf.>.>aQRTU$V!,,t4		,0C0CQ1M mmBIIaq!Q,OQSQ]Q]fjQkl >!#<9P9PRSUVXY!Zr   c                    | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }| j                  |      d d d d d || j                  z  d || j                  z  f   |d d d d d || j                  z  d || j                  z  f   z   }| j                  |      }||fS rI   )r  r  r  r  r  r  r  r  )rC   r  bicubicr,   r-   auxr  s          r    rL   z PixelShuffleAuxUpsampler.forward  s    ##G,33OD///:mmO,--c2MM/*1a1H6DLL3H1HJ`ETXT`T`L`J`+`aa3ft||335Kut||7K5KKLM 	 //@s""r   r  rP   s   @r    r  r    s    
[#r   r  zm
    Swin2SR Model transformer with an upsampler head on top for image super resolution and restoration.
    c                        e Zd Z fdZe	 	 	 	 	 d
dej                  dz  dej                  dz  dedz  dedz  dedz  de	e
z  fd	       Z xZS )Swin2SRForImageSuperResolutionc                 V   t         |   |       t        |      | _        |j                  | _        |j
                  | _        d}| j                  dk(  rt        ||      | _        n| j                  dk(  rt        ||      | _        n| j                  dk(  r1t        |j
                  |j                  |j                        | _        nS| j                  dk(  rt        ||      | _        n2t        j                  |j                  |j                  ddd      | _        | j!                          y )N@   r  pixelshuffle_auxpixelshuffledirectnearest+convr   r   )r6   r7   r  rw  	upsamplerr  r  r  r  r  r>   r  r  r   r\   r  r  r  s      r    r7   z'Swin2SRForImageSuperResolution.__init__  s     #F+))~~ >>^+1&,GDM^^114V\JDM^^33+FNNF<L<LfNeNefDM^^~-0FDM &(YYv/?/?AXAXZ[]^`a%bD" 	r   NrF   labelsr   ri  rj  rG   c                    ||n| j                   j                  }d}|t        d      |j                  dd \  }}	| j                   j                  dk(  r?t
        j                  j                  ||| j                  z  |	| j                  z  fdd      }
| j                  ||||      }|d	   }| j                  d
v r| j                  |      }nk| j                  dk(  rH| j                  |
||	      \  }}|| j                  j                  z  | j                  j                  z   }n|| j                  |      z   }|| j                  j                  z  | j                  j                  z   }|ddddd|| j                  z  d|	| j                  z  f   }|s|f|dd z   }||f|z   S |S t        |||j                  |j                         S )a  
        Example:
         ```python
         >>> import torch
         >>> import numpy as np
         >>> from PIL import Image
         >>> import httpx
        >>> from io import BytesIO

         >>> from transformers import AutoImageProcessor, Swin2SRForImageSuperResolution

         >>> processor = AutoImageProcessor.from_pretrained("caidas/swin2SR-classical-sr-x2-64")
         >>> model = Swin2SRForImageSuperResolution.from_pretrained("caidas/swin2SR-classical-sr-x2-64")

         >>> url = "https://huggingface.co/spaces/jjourney1125/swin2sr/resolve/main/samples/butterfly.jpg"
         >>> with httpx.stream("GET", url) as response:
         ...     image = Image.open(BytesIO(response.read()))
         >>> # prepare image for the model
         >>> inputs = processor(image, return_tensors="pt")

         >>> # forward pass
         >>> with torch.no_grad():
         ...     outputs = model(**inputs)

         >>> output = outputs.reconstruction.data.squeeze().float().cpu().clamp_(0, 1).numpy()
         >>> output = np.moveaxis(output, source=0, destination=-1)
         >>> output = (output * 255.0).round().astype(np.uint8)  # float32 to uint8
         >>> # you can visualize `output` with `Image.fromarray`
         ```Nz'Training is not supported at the momentr"   r  r  F)r   r  align_cornersr  r   )r  r  r  r   )lossr  r   r   )rD   rj  NotImplementedErrorr%   r  r   ry   r  r  rw  r  r  r  r  r
   r   r   )rC   rF   r  r   ri  rj  r  r  r,   r-   r  r   r  r  r  r   s                   r    rL   z&Swin2SRForImageSuperResolution.forward  s   N &1%<k$++BYBY%&OPP$**12.;;  $66mm//t||+UT\\-AB#	 0 G ,,/!5#	  
 "!*>>SS!]]?;N^^11"&--&RW"XNC...1B1BBC)D,B,B?,SSN'$,,*@*@@4<<CTCTT'1.E0E.EG]QUQ]Q]I]G](]^$&4F)-)9TGf$EvE))!//))	
 	
r   )NNNNN)r   r   r   r7   r   r   r   
LongTensorr   r   r
   rL   rO   rP   s   @r    r  r    s    4  26*.)-,0#'S
''$.S
   4'S
  $;	S

 #TkS
 D[S
 
+	+S
 S
r   r  )r  r  rv  )7rM   collections.abcrX   r   dataclassesr   r   r    r   r}  activationsr   modeling_layersr   modeling_outputsr	   r
   modeling_utilsr   utilsr   r   r   configuration_swin2srr   
get_loggerr   loggerr   r0   r2   r   r4   r8   rf   rm   r   r   r   r   r   r   r  rD  r[  rv  r  r  r  r  r  r  r  __all__r   r   r    <module>r     s   )   !   & ! 9 K - 9 9 0 
		H	% 
 7; 7 7	-		 -<-RYY -6ryy 3")) 3nE>299 E>R
		 
ryy 6"))  	BII 	%bii %2t299 tnA- AH<
RYY <
~ *_ * *8 X
) X
 X
v#ryy #Lbii 6BII "299 6#ryy #8 
o
%; o

o
d Wr   