
    3jj4                        S SK Jr  S SKrS SKJr  S SKrS SKJr  S SKJ	r	J
r
JrJr  SSKJr  SSKJrJrJr  S	S
KJr  / SQrSS jrSS jr      SS jrSS jr " S S\\   5      rSS/rg)    )annotationsN)Literal)override)KeyConditionget_pixelshuffle_paramsget_scale_and_output_channelsget_seq_len   )remove_common_prefix)ArchitectureImageModelDescriptor	StateDict   )GRL)table_wtable_shtable_svindex_windex_sh_a2windex_sh_w2aindex_sv_a2windex_sv_w2amask_wmask_sh_a2wmask_sh_w2amask_sv_a2wmask_sv_w2ac                    [        U S/5      n SU ;   aI  U R                  5        VVs0 s H  u  pUR                  S5      (       d  M  X_M!     n nn[        U S/5      n U $ s  snnf )Nzmodel.model_g.conv_first.weightzmodel_g.)r   items
startswith)
state_dictkvs      ]/home/wildlama/miniconda3/lib/python3.13/site-packages/spandrel/architectures/GRL/__init__.py_clean_up_checkpointr&   %   sf     &j8*=J #j0'1'7'7'9V'9tqQ\\*=Udad'9
V)*zlC
 Ws   AAc                   SU ;   a)  SU ;   a#  SnU S   R                   S   n[        U S5      u  pEOZSU ;   a!  Sn[        U S   R                   S   U5      u  pCO3SU ;   a  S	nU S   R                   S   nS
nOSnU S   R                   S   nSnX2U4$ )Nzconv_before_upsample.0.weightzupsample.up.0.weightpixelshufflezconv_last.weightr   zupsample.uppixelshuffledirectzconv_up1.weightnearest+conv    r   )shaper   r   )r"   in_channels	upsamplerout_channelsupscale_s         r%   _get_output_paramsr3   5   s     	(:5"j0"	!"45;;A>,ZG
	:	-(	 =-.44Q7!
 
j	("	!"45;;A>	!"45;;A>G++    c                    [        U S5      nUS:X  aN  SnSU ;   a  SU ;   a  SnUnOVSnUnOQSU ;   a  S	nU S   R                  S
   S-
  nO3SnU S   R                  S
   S-
  nOSnS
U-  nSU ;   a  SnOSU ;   a  S	nOSnX4U4$ )Nz"layers.0.blocks.0.attn.anchor.bodyr   Tz5layers.0.blocks.0.attn.anchor.body.0.reduction.weightz3layers.0.blocks.0.attn.anchor.body.0.reduction.biasavgpoolpatchmergingz+layers.0.blocks.0.attn.anchor.body.0.weightconv2d   separable_convz-layers.0.blocks.0.attn.anchor.body.0.0.weightF)r	   r-   )r"   default_down_factoranchor_body_lenanchor_one_stageanchor_proj_typeanchor_window_down_factors         r%   _get_anchor_paramsr@   S   s     "*.RSO!BjPD
R $- ,?)#1 ,?):jH'HIOOPQRUVV &  0JKQQRSTWXX & !$%$6!BjP-:jH'//HHHr4   c                *    [        U SSU-  -   -  5      $ )z7
Assuming that `a = b + b // d`, this will return `b`.
r   )round)ads     r%   _inv_div_addrE      s     a!a%i!!r4   c                  @   ^  \ rS rSrSU 4S jjr\SS j5       rSrU =r$ )GRLArch   c                   > [         TU ]  S[        R                  " [        R                  " SSSSS5      [        R                  " SSS	S
S5      [        R                  " SSSSS5      5      S9  g )Nr   conv_first.weightznorm_start.weightznorm_end.weightz=layers.0.blocks.0.attn.window_attn.attn_transform.logit_scalez>layers.0.blocks.0.attn.stripe_attn.attn_transform1.logit_scalezmodel.conv_first.weightzmodel.norm_start.weightzmodel.norm_end.weightzCmodel.layers.0.blocks.0.attn.window_attn.attn_transform.logit_scalezDmodel.layers.0.blocks.0.attn.stripe_attn.attn_transform1.logit_scaler   zmodel_g.norm_start.weightzmodel_g.norm_end.weightzEmodel_g.layers.0.blocks.0.attn.window_attn.attn_transform.logit_scalezFmodel_g.layers.0.blocks.0.attn.stripe_attn.attn_transform1.logit_scale)iddetect)super__init__r   has_anyhas_all)self	__class__s    r%   rN   GRLArch.__init__   s    ''$$''%ST $$--+YZ $$//-[\ 	 	
r4   c                   [        U5      nSnSnSS/nS S /nSnSnSnSn	Sn
SS/nSS/nSnSnUS	   R                  S
   nUS	   R                  S   n[        X5      u  nnnSU;   a0  US   R                  n[        U5      S:X  a  SnOUS   S
:X  a  SnOSnOSn[	        US5      nS/U-  nS/U-  nS/U-  n[        U5       HH  n[	        USU S35      UU'   USU S3   R                  S   UU'   USU S3   R                  S   UU'   MJ     SU;   a  SnSU;   nOSnSU;   n[        USS9u  nnnSU;   n US   R                  S   U-  n!SU;   a  S U;   a  S!U;   a  US   R                  S
   S
-   S-  nUS!   R                  n"[        [        R                  " [        U"6 [        U"6 -  5      5      n[        US    R                  S
   S
-   U5      [        US    R                  S   S
-   U5      /nOUS
:X  a  OUS":X  a  S#nS$S/nOS$nSS/nS%n[         H  n#U#U;   d  M  UU#	 M     [        SO0 S&U_S'U_S(U_S)U_S*U_S+U_S,U_S-U_S.U_S/U_S0U_S1U_S2U_S3U!_S4U_S5U_S6U_S7U_S8U_S9U_S:U _S;U_S<U	_S=U
_S>U_S?U_S@U_SAU_SBU_6n$SCn%[        U5      S:  a
  USD:  a  SEOSFn%[        U$UU US
:X  a  SGOSHU%U SI3SJU SKU 3SLUS    SMUS
    3/SS%UUUSN9
$ )PN@      Flinearg        g?r   nrJ   r   zconv_after_body.weightr9   1conv1x11conv3convlayers   r
   zlayers.z.blocksz5.blocks.0.attn.window_attn.attn_transform.logit_scalez6.blocks.0.attn.stripe_attn.attn_transform1.logit_scalez&layers.0.blocks.0.attn.qkv.body.weightz$layers.0.blocks.0.attn.qkv.body.biasr:   z&layers.0.blocks.0.attn.qkv.body.0.biasr+   )r;   z#layers.0.blocks.0.conv.cab.0.weightz layers.0.blocks.0.mlp.fc1.weightr   r   r   r*          Timg_sizer.   r0   	embed_dimr1   r/   depthsnum_heads_windownum_heads_stripewindow_sizestripe_sizestripe_groupsstripe_shift	mlp_ratioqkv_biasqkv_proj_typer>   r=   r?   out_proj_typelocal_connection	drop_rateattn_drop_ratedrop_path_ratepretrained_window_sizepretrained_stripe_size	conv_typeinit_methodeuclidean_distbase`   smalltinyRestorationSRdimwdfsx)architecturepurposetagssupports_halfsupports_bfloat16scaleinput_channelsoutput_channels )r&   r-   r3   lenr	   ranger@   intmathsqrtmaxminrE   _NON_PERSISTENT_BUFFERSr   r   )&rQ   r"   r`   re   rf   rg   rh   rl   rn   ro   rp   rq   rr   rt   ru   r.   ra   r0   r/   r1   conv_after_body_shapers   
depths_lenrb   rc   rd   irk   rj   r=   r>   r?   rm   ri   index_sh_a2w_shape
buffer_keymodelsize_tags&                                         r%   loadGRLArch.load   s   )*5
 "#Q+
 # 6>	 # #-.F-.F$ !45;;A>2399!<	+=j+V(i $z1$./G$H$N$N!()Q.$	&q)Q.&	#	I !X6
z!3+3+z"A#J'!G0DEF1I",!QR#eA#Q #-!RS#eA#Q # 4zA$M=KH,M?:MH  !"	
		
% AJNABHHKiW	 #j(*, &i066q9A=!CK .8-G-M-M(+		#12S:L5MMN)% z*003a79R z*003a79R	K !| ."$K#%r(K #%K#%r(K  2JZ'z* 2  

#
 &
  	

 
  
 
 .
 .
 $
 $
 (
 &
  
 
  (!
" .#
$ .%
& '@'
( ()
* .+
,  -
. */
0 *1
2 $:3
4 $:5
6  7
8 $9
: *;
@ v;?"+r/wvH#%,\Mt+S!K=#<"=>KN#1[^$45	  "&(
 	
r4   r   )returnNone)r"   r   r   zImageModelDescriptor[GRL])	__name__
__module____qualname____firstlineno__rN   r   r   __static_attributes____classcell__)rR   s   @r%   rG   rG      s    
8 A
 A
r4   rG   r   )r"   r   r   r   )r"   r   r.   r   )r"   r   r;   r   r   ztuple[bool, str, int])rC   r   rD   r   r   r   )
__future__r   r   typingr   torchtyping_extensionsr   spandrel.utilr   r   r   r	   __helpers.canonicalizer   __helpers.model_descriptorr   r   r   
__arch.grlr   r   r&   r3   r@   rE   rG   __all__r   r4   r%   <module>r      s    "    &  ; W W  " ,<*I*I03*I*IZ"_
l3 _
D e
r4   