
    RЦi                         S SK r S SKrS SKrS SK Jr  S SKJr  S\4S jrS rSS jr	SS jr
SS	 jrS
 r   SS\R                  S\S\S\S\4
S jjr  SS\R                  S\S\S\4S jjrg)    N)nn)_calculate_fan_in_and_fan_outreturnc                     U b  [        U 5      S:H  $ [        [        S5      (       a/  [        R                  " 5       nUSL=(       a    UR                  S:H  $ g)zACheck if targeting meta device (explicit arg or context manager).Nmetaget_default_deviceF)strhasattrtorchr   type)devicedefault_devices     V/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/timm/layers/weight_init.pyis_meta_devicer      sQ    6{f$$u*++113T)Kn.A.AV.KK    c                    S nXSU-  -
  :  d  XSU-  -   :  a  [         R                  " SSS9  U" X1-
  U-  5      nU" XA-
  U-  5      nU R                  SU-  S-
  SU-  S-
  5        U R                  5         U R	                  U[
        R                  " S5      -  5        U R                  U5        U R                  X4S9  U $ )Nc                 h    S[         R                  " U [         R                  " S5      -  5      -   S-  $ )N      ?       @)matherfsqrt)xs    r   norm_cdf _trunc_normal_.<locals>.norm_cdf   s(    TXXa$))B-/00B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 	1s7{1s7{ 2 E!"	$ 	!(c!"A!(c!"A OOAEAIq1uqy) NN KKdiim#$
KK MMaMMr   c                 |    [         R                  " 5          [        XX#U5      sSSS5        $ ! , (       d  f       g= f)ah  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_r3   6   s#    . 
fCA6 
s   -
;c                     [         R                  " 5          [        U SSX45        U R                  U5      R	                  U5        SSS5        U $ ! , (       d  f       U $ = f)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   r1   r/   r%   r&   r2   s        r   trunc_normal_tf_r5   Q   sM    . 
vq#q,Cd# 
 M 
 Ms   /A
Ac                 F   [        U 5      u  pEUS:X  a  UnOUS:X  a  UnOUS:X  a  XE-   S-  nUW-  nUS:X  a"  [        U [        R                  " U5      S-  S9  g US:X  aB  [        R
                  " 5          U R                  [        R                  " U5      S9  S S S 5        g US	:X  aK  [        R                  " S
U-  5      n[        R
                  " 5          U R                  U* U5        S S S 5        g [        SU 35      e! , (       d  f       g = f! , (       d  f       g = f)Nfan_infan_outfan_avgr   truncated_normalg۶%?r*   normaluniform   zinvalid distribution )	r   r5   r   r   r   r1   normal_r#   
ValueError)	r(   scalemodedistributionr7   r8   denomvariancebounds	            r   variance_scaling_rG   n   s    3F;OFx				!Q&u}H))TYYx%8;M%MN		!]]_NNtyy2N3 _		"		!h,']]_OOUFE* _ 0?@@ _ _s   5$DD
D
D c                     [        U SSS9  g )Nr7   r:   )rB   rC   )rG   )r(   s    r   lecun_normal_rI      s    f8:LMr   modulename	init_bias	head_biasclassifier_namec                    [        U [        R                  [        R                  [        R                  [        R
                  45      (       a  UR                  U5      (       aT  [        R                  R                  U R                  5        [        R                  R                  U R                  U5        g [        R                  R                  U R                  SS9  [        U [        R                  5      (       a9  U R                  b+  [        R                  R                  U R                  U5        g g g [        U S5      (       a  U R                  5         g g )N{Gz?r;   init_weights)
isinstancer   LinearConv1dConv2dConv3d
startswithinitzeros_weight	constant_biasr3   r
   rQ   )rJ   rK   rL   rM   rN   s        r   init_weight_vitr]      s     &299biiBIIFGG???++GGNN6==)GGfkk95GG!!&--T!:&")),,1H!!&++y9 2I,		(	( 
)r   c                 P   [        U [        R                  5      (       a  UR                  U5      (       aT  [        R                  R                  U R                  5        [        R                  R                  U R                  U5        g [        R                  R                  U R                  5        U R                  bX  SU;   a(  [        R                  R                  U R                  SS9O([        R                  R                  U R                  5        g g [        U [        R                  5      (       aM  [        U R                  5        U R                  b*  [        R                  R                  U R                  5        g g [        U S5      (       a  U R                  5         g g )Nmlpgư>r;   rQ   )rR   r   rS   rW   rX   rY   rZ   r[   r\   xavier_uniform_r?   rU   rI   r
   rQ   )rJ   rK   rM   rN   s       r   init_weight_jaxra      s    &"))$$???++GGNN6==)GGfkk95GG##FMM2{{&:?4-6RWW^^\b\g\gMh '	FBII	&	&fmm$;;"GGNN6;;' #		(	( 
)r   )        r   g       r   )r   r7   r<   )rP   rb   head)rb   rc   )r   r   r!   r   torch.nn.initr   boolr   r/   r3   r5   rG   rI   Moduler	   floatr]   ra    r   r   <module>ri      s        7d  F76:A2N  %		  	
 , %			  	r   