§
    ·¯¸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dS )é    N)Únn)Ú_calculate_fan_in_and_fan_outÚreturnc                 óž   — | �t          | ¦  «        dk    S t          t          d¦  «        r"t          j        ¦   «         }|duo
|j        dk    S dS )zACheck if targeting meta device (explicit arg or context manager).NÚmetaÚget_default_deviceF)ÚstrÚhasattrÚtorchr   Útype)ÚdeviceÚdefault_devices     úg/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/timm/layers/weight_init.pyÚis_meta_devicer      sX   € àÐÝ�6‰{Œ{˜fÒ$Ð$å�uÐ*Ñ+Ô+ð LÝÔ1Ñ3Ô3ˆØ TÐ)ÐK¨nÔ.AÀVÒ.KÐKØˆ5ó    c                 óÄ  — d„ }||d|z  z
  k     s||d|z  z   k    rt          j        dd¬¦  «          |||z
  |z  ¦  «        } |||z
  |z  ¦  «        }|                      d|z  dz
  d|z  dz
  ¦  «         |                      ¦   «          |                      |t          j        d¦  «        z  ¦  «         |                      |¦  «         |                      ||¬¦  «         | S )Nc                 ó`   — 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˜a¥$¤)¨B¡-¤-Ñ/Ñ0Ô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ð 7ð 7ð 	ˆq�1�s‘7‰{ÒÐ  q¨1¨s©7¡{Ò 2Ð 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 ‘m”mÑ#Ñ$Ô$Ð$Ø
‡K‚K�ÑÔÐð ‡M‚M�a˜Q€MÑÔÐØ€Mr   ç        r   ç       Àr   c                 ó‚   — t          j        ¦   «         5  t          | ||||¦  «        cddd¦  «         S # 1 swxY w Y   dS )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_r4   6   s‡   € õ. 
Œ‰Œð 7ð 7Ý˜f d¨C°°AÑ6Ô6ð7ð 7ð 7ð 7ñ 7ô 7ð 7ð 7ð 7ð 7ð 7ð 7øøøð 7ð 7ð 7ð 7ð 7ð 7s   ”4´8»8c                 óÒ   — t          j        ¦   «         5  t          | dd||¦  «         |                      |¦  «                             |¦  «         ddd¦  «         n# 1 swxY w Y   | S )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   r2   r.   r$   r%   r3   s        r   Útrunc_normal_tf_r6   Q   s¢   € õ. 
Œ‰Œð $ð $Ý�v˜q # q¨!Ñ,Ô,Ð,Ø�Š�CÑÔ×Ò˜dÑ#Ô#Ð#ð$ð $ð $ñ $ô $ð $ð $ð $ð $ð $ð $øøøð $ð $ð $ð $ð €Ms   ”<AÁA Á#A Úfan_inÚnormalc                 ól  — t          | ¦  «        \  }}|dk    r|}n|dk    r|}n|dk    r||z   dz  }||z  }|dk    r(t          | t          j        |¦  «        dz  ¬¦  «         d S |dk    rVt	          j        ¦   «         5  |                      t          j        |¦  «        ¬¦  «         d d d ¦  «         d S # 1 swxY w Y   d S |d	k    r\t          j        d
|z  ¦  «        }t	          j        ¦   «         5  |                      | |¦  «         d d d ¦  «         d S # 1 swxY w Y   d S t          d|› �¦  «        ‚)Nr7   Úfan_outÚfan_avgr   Útruncated_normalg�©Û¶ä%ì?©r)   r8   Úuniformé   zinvalid distribution )	r   r6   r   r   r   r2   Únormal_r"   Ú
ValueError)	r'   ÚscaleÚmodeÚdistributionr7   r:   ÚdenomÚvarianceÚbounds	            r   Úvariance_scaling_rH   n   så  € Ý3°FÑ;Ô;�O€FˆGØˆxÒÐØˆˆØ	�Ò	Ð	ØˆˆØ	�Ò	Ð	Ø˜'Ñ! QÑ&ˆà�u‰}€HàÐ)Ò)Ð)å˜¥T¤Y¨xÑ%8Ô%8Ð;MÑ%MÐNÑNÔNÐNÐNÐNØ	˜Ò	!Ð	!ÝŒ]‰_Œ_ð 	4ð 	4Ø�NŠN�tœy¨Ñ2Ô2ˆNÑ3Ô3Ð3ð	4ð 	4ð 	4ñ 	4ô 	4ð 	4ð 	4ð 	4ð 	4ð 	4ð 	4ð 	4øøøð 	4ð 	4ð 	4ð 	4ð 	4ð 	4à	˜Ò	"Ð	"Ý”	˜!˜h™,Ñ'Ô'ˆÝŒ]‰_Œ_ð 	+ð 	+Ø�OŠO˜U˜F EÑ*Ô*Ð*ð	+ð 	+ð 	+ñ 	+ô 	+ð 	+ð 	+ð 	+ð 	+ð 	+ð 	+ð 	+øøøð 	+ð 	+ð 	+ð 	+ð 	+ð 	+õ Ð?°Ð?Ð?Ñ@Ô@Ð@s$   Á?)B5Â5B9Â<B9Ã2DÄDÄDc                 ó*   — t          | dd¬¦  «         d S )Nr7   r<   )rC   rD   )rH   )r'   s    r   Úlecun_normal_rJ   ‡   s   € Ý�f 8Ð:LÐMÑMÔMÐMÐMÐMr   ç{®Gáz”?ÚheadÚmoduleÚnameÚ	init_biasÚ	head_biasÚclassifier_namec                 ón  — t          | t          j        t          j        t          j        t          j        f¦  «        rÒ|                     |¦  «        rKt          j                             | j	        ¦  «         t          j         
                    | j        |¦  «         d S t          j                             | j	        d¬¦  «         t          | t          j        ¦  «        r.| j        �)t          j         
                    | j        |¦  «         d S d S d S t          | d¦  «        r|                      ¦   «          d S d S )NrK   r=   Úinit_weights)Ú
isinstancer   ÚLinearÚConv1dÚConv2dÚConv3dÚ
startswithÚinitÚzeros_ÚweightÚ	constant_Úbiasr4   r
   rS   )rM   rN   rO   rP   rQ   s        r   Úinit_weight_vitr_   ‹   s  € õ �&�2œ9¥b¤iµ´½B¼IÐFÑGÔGð 	Ø�?Š?˜?Ñ+Ô+ð 	:ÝŒG�NŠN˜6œ=Ñ)Ô)Ð)ÝŒG×Ò˜fœk¨9Ñ5Ô5Ð5Ð5Ð5åŒG×!Ò! &¤-°TÐ!Ñ:Ô:Ð:Ý˜&¥"¤)Ñ,Ô,ð :°´Ð1HÝ”×!Ò! &¤+¨yÑ9Ô9Ð9Ð9Ð9ð:ð :Ð1HÐ1Hå	�˜Ñ	(Ô	(ð Ø×ÒÑÔÐÐÐðð r   c                 óú  — t          | t          j        ¦  «        rÝ|                     |¦  «        rKt          j                             | j        ¦  «         t          j                             | j        |¦  «         d S t          j         	                    | j        ¦  «         | j        �Pd|v r&t          j         
                    | j        d¬¦  «        n#t          j                             | j        ¦  «         d S d S t          | t          j        ¦  «        rCt          | j        ¦  «         | j        �&t          j                             | j        ¦  «         d S d S t          | d¦  «        r|                      ¦   «          d S d S )NÚmlpg�íµ ÷Æ°>r=   rS   )rT   r   rU   rY   rZ   r[   r\   r]   r^   Úxavier_uniform_r@   rW   rJ   r
   rS   )rM   rN   rP   rQ   s       r   Úinit_weight_jaxrc   ž   sJ  € õ �&�"œ)Ñ$Ô$ð Ø�?Š?˜?Ñ+Ô+ð 	iÝŒG�NŠN˜6œ=Ñ)Ô)Ð)ÝŒG×Ò˜fœk¨9Ñ5Ô5Ð5Ð5Ð5åŒG×#Ò# F¤MÑ2Ô2Ð2ØŒ{Ð&Ø:?À4¸-¸-•”—’ ¤°�Ñ6Ô6Ð6ÍRÌWÏ^Ê^Ð\bÔ\gÑMhÔMhøè è ð 'Ð&å	�F�BœIÑ	&Ô	&ð Ý�f”mÑ$Ô$Ð$ØŒ;Ð"ÝŒG�NŠN˜6œ;Ñ'Ô'Ð'Ð'Ð'ð #Ð"å	�˜Ñ	(Ô	(ð Ø×ÒÑÔÐÐÐðð r   )r/   r   r0   r   )r   r7   r8   )rK   r/   rL   )r/   rL   )r   r   r    r   Útorch.nn.initr   Úboolr   r.   r4   r6   rH   rJ   ÚModuler	   Úfloatr_   rc   © r   r   ú<module>ri      sf  ðØ €€€Ø €€€Ø €€€Ø Ð Ð Ð Ð Ð Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7ð˜dð ð ð ð ð ð  ð  ðF7ð 7ð 7ð 7ð6ð ð ð ð:Að Að Að Að2Nð Nð Nð  ØØ%ðð Ø”	ðàðð ðð ð	ð
 ðð ð ð ð, Ø%ð	ð Ø”	ðàðð ðð ð	ð ð ð ð ð r   