
    Ng?                         d dl Z d dlZd dlZd dl mZ d dlm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_outc                    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    S/var/www/html/ai-engine/env/lib/python3.11/site-packages/timm/layers/weight_init.pynorm_cdfz _trunc_normal_.<locals>.norm_cdf   s)    TXa$)B--/000B66       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 	q1s7{q1s7{ 2 2 E!"	$ 	$ 	$ 	$ 	!d(c!""A!d(c!""A OOAEAIq1uqy))) NN KKdimm#$$$
KK MMaQM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)torchno_gradr#   r   r   r   r   r    s        r   trunc_normal_r*   +   s    . 
 7 7fdC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 subsquently 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'   r(   r#   r   r   r)   s        r   trunc_normal_tf_r,   F   s    . 
 $ $vq#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|           )Nr-   fan_outfan_avgr   truncated_normalg۶%?r   r.   uniform   zinvalid distribution )	r   r,   r	   r   r'   r(   normal_r   
ValueError)	r   scalemodedistributionr-   r0   denomvariancebounds	            r   variance_scaling_r>   c   s   3F;;OFGx						'!Q&u}H)))TYx%8%8;M%MNNNNNN		!	!]__ 	4 	4NNty22N333	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4 	4		"	"	!h,'']__ 	+ 	+OOUFE***	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ ???@@@s$   ?)B55B9<B92DDDc                 *    t          | dd           d S )Nr-   r2   )r9   r:   )r>   )r   s    r   lecun_normal_r@   |   s    f8:LMMMMMMr   {Gz?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 )NrA   r3   init_weights)
isinstancer   LinearConv1dConv2dConv3d
startswithinitzeros_weight	constant_biasr*   hasattrrI   )rC   rD   rE   rF   rG   s        r   init_weight_vitrV      s    &29biBIFGG 	???++ 	:GNN6=)))Gfk955555G!!&-T!:::&"),, :1H!!&+y99999: :1H1H		(	(  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ư>r3   rI   )rJ   r   rK   rO   rP   rQ   rR   rS   rT   xavier_uniform_r6   rM   r@   rU   rI   )rC   rD   rF   rG   s       r   init_weight_jaxrZ      sJ    &")$$ ???++ 	iGNN6=)))Gfk955555G##FM222{&:?4--666RW^^\b\gMhMh   '&	FBI	&	& fm$$$;"GNN6;''''' #"		(	(  r   )r$   r   r%   r   )r   r-   r.   )rA   r$   rB   )r$   rB   )r'   r	   r   r   torch.nn.initr   r#   r*   r,   r>   r@   ModulestrfloatrV   rZ    r   r   <module>r`      sK            7 7 7 7 7 7     F7 7 7 76   :A A A A2N N N  % 	  	
    , %	 	  	     r   