Ë
    éÓ^jÛ  ã                   óÎ   — d dl Z d dlZd dlZd dl mZ d dlmZ defd„Zd„ Zdd„Z	dd„Z
dd	„Zd
„ Z	 	 	 ddej                  dedededef
d„Z	 	 ddej                  dededefd„Zy)é    N)Únn)Ú_calculate_fan_in_and_fan_outÚreturnc                 ó–   — | �t        | «      dk(  S t        t        d«      r)t        j                  «       }|duxr |j                  dk(  S y)zACheck if targeting meta device (explicit arg or context manager).NÚmetaÚget_default_deviceF)ÚstrÚhasattrÚtorchr   Útype)ÚdeviceÚdefault_devices     úb/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/timm/layers/weight_init.pyÚis_meta_devicer      sN   € àÐÜ�6‹{˜fÑ$Ð$äŒuÐ*Ô+Ü×1Ñ1Ó3ˆØ TÐ)ÒK¨n×.AÑ.AÀVÑ.KÐKØó    c                 ó�  — d„ }||d|z  z
  k  s||d|z  z   kD  rt        j                  dd¬«        |||z
  |z  «      } |||z
  |z  «      }| j                  d|z  dz
  d|z  dz
  «       | j                  «        | j	                  |t        j                  d«      z  «       | j                  |«       | j                  ||¬«       | S )Nc                 ód   — dt        j                  | t        j                  d«      z  «      z   dz  S )Nç      ð?ç       @)ÚmathÚerfÚsqrt)Úxs    r   Únorm_cdfz _trunc_normal_.<locals>.norm_cdf   s(   € à”T—X‘X˜a¤$§)¡)¨B£-Ñ/Ó0Ñ0°BÑ6Ð6r   é   zjmean is more than 2 std from [a, b] in nn.init.trunc_normal_. The distribution of values may be incorrect.)Ú
stacklevelé   r   )ÚminÚmax)	ÚwarningsÚwarnÚuniform_Úerfinv_Úmul_r   r   Úadd_Úclamp_)ÚtensorÚmeanÚstdÚaÚbr   ÚlÚus           r   Ú_trunc_normal_r.      sÏ   € ò7ð 	ˆq�1�s‘7‰{Ò  q¨1¨s©7¡{Ò 2Ü�‰ð Eà!"õ	$ñ 	�!�d‘(˜cÑ!Ó"€AÙ�!�d‘(˜cÑ!Ó"€Að ‡O�O�A˜‘E˜A‘I˜q 1™u q™yÔ)ð ‡N�NÔð ‡K�K�”d—i‘i “mÑ#Ô$Ø
‡K�K�Ôð ‡M�M�a˜Q€MÔØ€Mr   c                 ót   — t        j                  «       5  t        | ||||«      cddd«       S # 1 sw Y   yxY w)a°  Fills the input Tensor with values drawn from a truncated
    normal distribution. The values are effectively drawn from the
    normal distribution :math:`\mathcal{N}(\text{mean}, \text{std}^2)`
    with values outside :math:`[a, b]` redrawn until they are within
    the bounds. The method used for generating the random values works
    best when :math:`a \leq \text{mean} \leq b`.

    NOTE: this impl is similar to the PyTorch trunc_normal_, the bounds [a, b] are
    applied while sampling the normal with mean/std applied, therefore a, b args
    should be adjusted to match the range of mean, std args.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        mean: the mean of the normal distribution
        std: the standard deviation of the normal distribution
        a: the minimum cutoff value
        b: the maximum cutoff value
    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.trunc_normal_(w)
    N)r   Úno_gradr.   ©r'   r(   r)   r*   r+   s        r   Útrunc_normal_r2   6   s2   € ô. 
�‰‹ñ 7Ü˜f d¨C°°AÓ6÷7÷ 7ò 7ús   •.®7c                 ó¸   — t        j                  «       5  t        | dd||«       | j                  |«      j	                  |«       ddd«       | S # 1 sw Y   | S xY w)aÆ  Fills the input Tensor with values drawn from a truncated
    normal distribution. The values are effectively drawn from the
    normal distribution :math:`\mathcal{N}(\text{mean}, \text{std}^2)`
    with values outside :math:`[a, b]` redrawn until they are within
    the bounds. The method used for generating the random values works
    best when :math:`a \leq \text{mean} \leq b`.

    NOTE: this 'tf' variant behaves closer to Tensorflow / JAX impl where the
    bounds [a, b] are applied when sampling the normal distribution with mean=0, std=1.0
    and the result is subsequently scaled and shifted by the mean and std args.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        mean: the mean of the normal distribution
        std: the standard deviation of the normal distribution
        a: the minimum cutoff value
        b: the maximum cutoff value
    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.trunc_normal_(w)
    r   r   N)r   r0   r.   r$   r%   r1   s        r   Útrunc_normal_tf_r4   Q   sR   € ô. 
�‰‹ñ $Ü�v˜q # q¨!Ô,Ø�‰�CÓ×Ñ˜dÔ#÷$ð €M÷$ð €Mús   •0AÁAc                 ó,  — t        | «      \  }}|dk(  r|}n|dk(  r|}n|dk(  r||z   dz  }|z  }|dk(  r$t        | t        j                  |«      dz  ¬«       y |dk(  rCt	        j
                  «       5  | j                  t        j                  |«      ¬«       d d d «       y |d	k(  rIt        j                  d
|z  «      }t	        j
                  «       5  | j                  | |«       d d d «       y t        d|› �«      ‚# 1 sw Y   y xY w# 1 sw Y   y xY w)NÚfan_inÚfan_outÚfan_avgr   Útruncated_normalg�©Û¶ä%ì?©r)   ÚnormalÚuniformé   zinvalid distribution )	r   r4   r   r   r   r0   Únormal_r"   Ú
ValueError)	r'   ÚscaleÚmodeÚdistributionr6   r7   ÚdenomÚvarianceÚbounds	            r   Úvariance_scaling_rF   n   s  € Ü3°FÓ;�O€FˆGØˆxÒØ‰Ø	�Ò	Ø‰Ø	�Ò	Ø˜'Ñ! QÑ&ˆà�u‰}€HàÐ)Ò)ä˜¤T§Y¡Y¨xÓ%8Ð;MÑ%MÖNØ	˜Ò	!Ü�]‰]‹_ñ 	4Ø�N‰NœtŸy™y¨Ó2ˆNÔ3÷	4ð 	4à	˜Ò	"Ü—	‘	˜!˜h™,Ó'ˆÜ�]‰]‹_ñ 	+Ø�O‰O˜U˜F EÔ*÷	+ð 	+ô Ð0°°Ð?Ó@Ð@÷	4ð 	4ú÷	+ð 	+ús   Á3&C>ÃD
Ã>DÄ
Dc                 ó    — t        | dd¬«       y )Nr6   r9   )rA   rB   )rF   )r'   s    r   Úlecun_normal_rH   ‡   s   € Ü�f 8Ð:LÖMr   ÚmoduleÚnameÚ	init_biasÚ	head_biasÚclassifier_namec                 ó”  — t        | t        j                  t        j                  t        j                  t        j
                  f«      rã|j                  |«      rTt        j                  j                  | j                  «       t        j                  j                  | j                  |«       y t        j                  j                  | j                  d¬«       t        | t        j                  «      r8| j                  �+t        j                  j                  | j                  |«       y y y t        | d«      r| j                  «        y y )Nç{®Gáz”?r:   Úinit_weights)Ú
isinstancer   ÚLinearÚConv1dÚConv2dÚConv3dÚ
startswithÚinitÚzeros_ÚweightÚ	constant_Úbiasr2   r
   rP   )rI   rJ   rK   rL   rM   s        r   Úinit_weight_vitr\   ‹   sÍ   € ô �&œ2Ÿ9™9¤b§i¡i´·±¼B¿I¹IÐFÔGØ�?‰?˜?Ô+Ü�G‰G�N‰N˜6Ÿ=™=Ô)Ü�G‰G×Ñ˜fŸk™k¨9Õ5ä�G‰G×!Ñ! &§-¡-°TÐ!Ô:Ü˜&¤"§)¡)Ô,°·±Ð1HÜ—‘×!Ñ! &§+¡+¨yÕ9ð 2IÐ,ä	�˜Ô	(Ø×ÑÕð 
)r   c                 ó&  — t        | t        j                  «      rô|j                  |«      rTt        j                  j                  | j                  «       t        j                  j                  | j                  |«       y t        j                  j                  | j                  «       | j                  �Yd|v r+t        j                  j                  | j                  d¬«      n(t        j                  j                  | j                  «       y y t        | t        j                  «      rLt        | j                  «       | j                  �*t        j                  j                  | j                  «       y y t        | d«      r| j                  «        y y )NÚmlpg�íµ ÷Æ°>r:   rP   )rQ   r   rR   rV   rW   rX   rY   rZ   r[   Úxavier_uniform_r>   rT   rH   r
   rP   )rI   rJ   rL   rM   s       r   Úinit_weight_jaxr`   ž   sü   € ô �&œ"Ÿ)™)Ô$Ø�?‰?˜?Ô+Ü�G‰G�N‰N˜6Ÿ=™=Ô)Ü�G‰G×Ñ˜fŸk™k¨9Õ5ä�G‰G×#Ñ# F§M¡MÔ2Ø�{‰{Ð&Ø:?À4¹-”—‘—‘ §¡°�Ô6ÌRÏWÉWÏ^É^Ð\b×\gÑ\gÓMhùð 'ä	�FœBŸI™IÔ	&Ü�f—m‘mÔ$Ø�;‰;Ð"Ü�G‰G�N‰N˜6Ÿ;™;Õ'ð #ä	�˜Ô	(Ø×ÑÕð 
)r   )ç        r   g       Àr   )r   r6   r;   )rO   ra   Úhead)ra   rb   )r   r   r    r   Útorch.nn.initr   Úboolr   r.   r2   r4   rF   rH   ÚModuler	   Úfloatr\   r`   © r   r   ú<module>rh      s³   ðÛ Û Û Ý Ý 7ð˜dó ò óF7ó6ó:Aò2Nð  ØØ%ñØ—	‘	ðàðð ðð ð	ð
 óð, Ø%ñ	Ø—	‘	ðàðð ðð ô	r   