§
    �Åj@  ã                  ó    — d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	m
Z
mZ  G d„ d¦  «        Zdd„Zdd„Zdd„Z	 	 d d!d„Zi dœd"d„Zg d¢ZdS )#zM
A module containing commonly-used functionality to implement architectures.
é    )ÚannotationsN)ÚAnyÚLiteralÚMappingÚProtocolÚTypeVarc                  óR   — e Zd ZdZdd„Zedd	„¦   «         Zedd
„¦   «         Zdd„ZdS )ÚKeyConditionzE
    A condition that checks if a state dict has the given keys.
    ÚkindúLiteral['all', 'any']Úkeysútuple[str | KeyCondition, ...]c                ó"   — || _         || _        d S ©N)Ú_keysÚ_kind)Úselfr   r   s      úGC:\Projects\Historical\venv\Lib\site-packages\spandrel/util/__init__.pyÚ__init__zKeyCondition.__init__   s   € ð ˆŒ
Ø,0ˆŒ
ˆ
ˆ
ó    ústr | KeyConditionÚreturnc                 ó"   — t          d| ¦  «        S )NÚall©r
   ©r   s    r   Úhas_allzKeyCondition.has_all   ó   € å˜E 4Ñ(Ô(Ð(r   c                 ó"   — t          d| ¦  «        S )NÚanyr   r   s    r   Úhas_anyzKeyCondition.has_any   r   r   Ú
state_dictúMapping[str, object]Úboolc                ó¨   ‡‡— d	ˆfd„Š| j         dk    r t          ˆfd„| j        D ¦   «         ¦  «        S t          ˆfd„| j        D ¦   «         ¦  «        S )
NÚkeyr   r   r$   c                óL   •— t          | t          ¦  «        r | ‰¦  «        S | ‰v S r   )Ú
isinstancer
   )r&   r"   s    €r   Ú_detectz&KeyCondition.__call__.<locals>._detect!   s.   ø€ Ý˜#�|Ñ,Ô,ð 'Ø�s˜:‘”Ð&Ø˜*Ð$Ð$r   r   c              3  ó.   •K  — | ]} ‰|¦  «        V — Œd S r   © ©Ú.0r&   r)   s     €r   ú	<genexpr>z(KeyCondition.__call__.<locals>.<genexpr>'   ó+   øè è € Ð:Ð:¨�w�w˜s‘|”|Ð:Ð:Ð:Ð:Ð:Ð:r   c              3  ó.   •K  — | ]} ‰|¦  «        V — Œd S r   r+   r,   s     €r   r.   z(KeyCondition.__call__.<locals>.<genexpr>)   r/   r   )r&   r   r   r$   )r   r   r   r    )r   r"   r)   s    `@r   Ú__call__zKeyCondition.__call__    s{   øø€ ð	%ð 	%ð 	%ð 	%ð 	%ð 	%ð
 Œ:˜ÒÐÝÐ:Ð:Ð:Ð:¨t¬zÐ:Ñ:Ô:Ñ:Ô:Ð:åÐ:Ð:Ð:Ð:¨t¬zÐ:Ñ:Ô:Ñ:Ô:Ð:r   N)r   r   r   r   )r   r   r   r
   )r"   r#   r   r$   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ústaticmethodr   r!   r1   r+   r   r   r
   r
      s†   € € € € € ðð ð1ð 1ð 1ð 1ð ð)ð )ð )ñ „\ð)ð ð)ð )ð )ñ „\ð)ð	;ð 	;ð 	;ð 	;ð 	;ð 	;r   r
   r"   r#   Úkey_patternÚstrr   Úintc                óz   — t          d¦  «        D ]*}|                     t          |¦  «        ¦  «        | v r|c S Œ+dS )a  
    Returns the maximum index `i` such that `key_pattern.format(str(i))` is in `state`.

    If no such key is in state, then `-1` is returned.

    Example:
        get_first_seq_index(state, "body.{}.weight") -> -1
        get_first_seq_index(state, "body.{}.weight") -> 3
    éd   éÿÿÿÿ)ÚrangeÚformatr8   )r"   r7   Úis      r   Úget_first_seq_indexr@   ,   sK   € õ �3‰ZŒZð ð ˆØ×Ò�c !™fœfÑ%Ô%¨Ð3Ð3ØˆHˆHˆHð 4àˆ2r   Úseq_keyc                óv  — |dz   }t          ¦   «         }|                      ¦   «         D ]k}|                     |¦  «        rT|t          |¦  «        d…                              dd¬¦  «        d         }|                     t          |¦  «        ¦  «         Œlt          |¦  «        dk    rdS t          |¦  «        dz   S )zö
    Returns the length of a sequence in the state dict.

    The length is detected by finding the maximum index `i` such that
    `{seq_key}.{i}.{suffix}` is in `state` for some suffix.

    Example:
        get_seq_len(state, "body") -> 5
    ú.Né   )Úmaxsplitr   )Úsetr   Ú
startswithÚlenÚsplitÚaddr9   Úmax)r"   rA   Úprefixr   ÚkÚindexs         r   Úget_seq_lenrO   <   s¬   € ð �s‰]€Få‘U”U€DØ�_Š_ÑÔð !ð !ˆØ�<Š<˜ÑÔð 	!Ø•c˜&‘k”k�m�mÔ$×*Ò*¨3¸Ð*Ñ;Ô;¸AÔ>ˆEØ�HŠH•S˜‘Z”ZÑ Ô Ð øå
ˆ4�y„y�A‚~€~ØˆqÝˆt‰9Œ9�q‰=Ðr   ÚxÚinput_channelsútuple[int, int]c                óÄ   — dd„}|dddg}|D ]A}| |z  d	k    r6 || |z  ¦  «        r(t          t          j        | |z  ¦  «        ¦  «        |fc S ŒBt          d
| › d�¦  «        ‚)zŽ
    Returns a scale and number of output channels such that `scale**2 * out_nc = x`.

    This is commonly used for pixelshuffel layers.
    Únr9   r   r$   c                óp   — t          j        | ¦  «        t          t          j        | ¦  «        ¦  «        k    S r   )ÚmathÚsqrtr9   )rT   s    r   Ú	is_squarez0get_scale_and_output_channels.<locals>.is_squarec   s%   € ÝŒy˜‰|Œ|�s¥4¤9¨Q¡<¤<Ñ0Ô0Ò0Ð0r   é   é   rD   r   zwExpected output channels to be either 1, 3, or 4. Could not find a pair (scale, out_nc) such that `scale**2 * out_nc = ú`N)rT   r9   r   r$   )r9   rV   rW   ÚAssertionError)rP   rQ   rX   Ú
candidatesÚcs        r   Úget_scale_and_output_channelsr_   S   s©   € ð 1ð 1ð 1ð 1ð ! ! Q¨Ð*€JØð -ð -ˆØˆq‰5�AŠ:ˆ:˜)˜) A¨¡FÑ+Ô+ˆ:Ý•t”y  a¡Ñ(Ô(Ñ)Ô)¨1Ð,Ð,Ð,Ð,øå
ð	VØQRð	Vð 	Vð 	Vñô ð r   Úupsampleé@   Úupsample_keyÚ
default_nfc                óÄ   — d}|}t          ddd¦  «        D ]G}|› d|› d�}|| vr n8| |         }|j        }|d         }|t          j        |d         |z  ¦  «        z  }ŒH||fS )au  
    This will detect the upscale factor and number of features of a pixelshuffle module in the state dict.

    A pixelshuffle module is a sequence of alternating up convolutions and pixelshuffle.
    The class of this module is commonyl called `Upsample`.
    Examples of such modules can be found in most SISR architectures, such as SwinIR, HAT, RGT, and many more.
    rD   r   é
   é   rC   z.weight)r=   ÚshaperV   Úisqrt)	r"   rb   rc   ÚupscaleÚnum_featr?   r&   Útensorrg   s	            r   Úget_pixelshuffle_paramsrl   r   s�   € ð €GØ€Hå�1�b˜!‰_Œ_ð 	4ð 	4ˆØÐ*Ð* Ð*Ð*Ð*ˆØ�jÐ Ð ØˆEà˜C”ˆà!'¤ˆØ˜”8ˆØ•4”:˜e Aœh¨(Ñ2Ñ3Ô3Ñ3ˆˆà�HÐÐr   )Úextra_parametersrm   c                óh   ‡ ‡— dd„Š G d„ dt           ¦  «        }t          d|¬	¦  «        }dˆ ˆfd„}|S )zQ
    Stores the hyperparameters of a class in a `hyperparameters` attribute.
    Úspecúinspect.FullArgSpecr   údict[str, Any]c           	     óÀ   — i }| j         �| j         }| j        �Fi |¥t          t          | j        t          | j        ¦  «         d …         | j        ¦  «        ¦  «        ¥}|S r   )ÚkwonlydefaultsÚdefaultsÚdictÚzipÚargsrH   )ro   rt   s     r   Úget_arg_defaultsz/store_hyperparameters.<locals>.get_arg_defaults”   sl   € ØˆØÔÐ*ØÔ*ˆHàŒ=Ð$ðØðå•s˜4œ9¥c¨$¬-Ñ&8Ô&8Ð%8Ð%:Ð%:Ô;¸T¼]ÑKÔKÑLÔLðˆHð
 ˆr   c                  ó   — e Zd ZU ded<   dS )ú2store_hyperparameters.<locals>.WithHyperparametersrq   ÚhyperparametersN)r2   r3   r4   Ú__annotations__r+   r   r   ÚWithHyperparametersrz   ¡   s   € € € € € € Ø'Ð'Ð'Ñ'Ð'Ð'r   r}   ÚC)ÚboundÚclsútype[C]c                óN  •‡‡— | j         Št          j        ‰¦  «        } ‰|¦  «        Š|j        �t	          d¦  «        ‚|j        �t	          d¦  «        ‚|j        dgk    rt	          d| j        z   dz   ¦  «        ‚t          j	        ‰¦  «        dˆˆˆfd„¦   «         }|| _         | S )	NzPClass has *args, which is not allowed in combination with @store_hyperparameterszSClass has **kwargs, which is not allowed in combination with @store_hyperparametersr   z2@store_hyperparameters requires all arguments of `zW.__init__` after `self` to be keyword arguments. Use `def __init__(self, *, a, b, c):`.r~   c           
     óÊ   •— ‰                      ¦   «         D ]6\  }}||v r-||         |k    rt          d|› d|› d||         › �¦  «        ‚||= Œ7i ‰¥‰¥|¥| _         ‰| fi |¤Ž d S )NzExpected hyperparameter z to be z
, but got )ÚitemsÚ
ValueErrorr{   )r   ÚkwargsrM   Úvrt   rm   Úold_inits       €€€r   Únew_initz6store_hyperparameters.<locals>.inner.<locals>.new_init»   s¬   ø€ ð )×.Ò.Ñ0Ô0ð "ð "‘��1Ø˜�;�;Ø˜a”y A’~�~Ý(ØY°qÐYÐYÀÐYÐYÈfÐUVÌiÐYÐYñô ð ð ˜q˜	øà#MÐ&6Ð#M¸(Ð#MÀfÐ#MˆDÔ ØˆH�TÐ$Ð$˜VÐ$Ð$Ð$Ð$Ð$r   )r   r~   )
r   ÚinspectÚgetfullargspecÚvarargsÚUserWarningÚvarkwrw   r2   Ú	functoolsÚwraps)r€   ro   r‰   rt   rˆ   rm   rx   s      @@€€r   Úinnerz$store_hyperparameters.<locals>.inner¦   sî   øøø€ Ø”<ˆåÔ% hÑ/Ô/ˆØ#Ð# DÑ)Ô)ˆàŒ<Ð#ÝØbñô ð ð Œ:Ð!ÝØeñô ð ð Œ9˜˜Ò Ð ÝØDØ”,ñàkñlñô ð õ 
Œ˜Ñ	"Ô	"ð	%ð 	%ð 	%ð 	%ð 	%ð 	%ð 	%ñ 
#Ô	"ð	%ð  ˆŒØˆ
r   )ro   rp   r   rq   )r€   r�   r   r�   )r   r   )rm   r}   r~   r‘   rx   s   `   @r   Ústore_hyperparametersr’   �   s‚   øø€ ð
ð ð ð ð(ð (ð (ð (ð (�hñ (ô (ð (õ 	�Ð.Ð/Ñ/Ô/€Að$ð $ð $ð $ð $ð $ð $ðL €Lr   )r@   rl   r_   rO   r
   r’   )r"   r#   r7   r8   r   r9   )r"   r#   rA   r8   r   r9   )rP   r9   rQ   r9   r   rR   )r`   ra   )r"   r#   rb   r8   rc   r9   r   rR   )rm   r#   )r5   Ú
__future__r   r�   rŠ   rV   Útypingr   r   r   r   r   r
   r@   rO   r_   rl   r’   Ú__all__r+   r   r   ú<module>r–      s,  ððð ð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ø €€€Ø €€€Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;Ð ;ð;ð ;ð ;ð ;ð ;ñ ;ô ;ð ;ð>ð ð ð ð ð ð ð ð.ð ð ð ðB #Øðð ð ð ð ð: GIð =ð =ð =ð =ð =ð =ð@ð ð €€€r   