U
    ¯Ê¹j�  ã                   @   sV   d dl Z d dlZd dlZd dlmZ dd„ Zdd	d
„Zddd„Zddd„Zdd„ Z	dS )é    N)Ú_calculate_fan_in_and_fan_outc                 C   sª   dd„ }||d|  k s(||d|  kr6t jddd� ||| | ƒ}||| | ƒ}|  d| d d| d ¡ |  ¡  |  |t d¡ ¡ |  |¡ | j||d� | S )	Nc                 S   s   dt  | t  d¡ ¡ d S )Nç      ð?ç       @)ÚmathÚerfÚsqrt)Úx© r	   úV/var/www/html/bwave/python/venv/lib/python3.8/site-packages/timm/layers/weight_init.pyÚnorm_cdf   s    z _trunc_normal_.<locals>.norm_cdfé   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_Zerfinv_Úmul_r   r   Úadd_Zclamp_)ÚtensorÚmeanÚstdÚaÚbr   ÚlÚur	   r	   r
   Ú_trunc_normal_   s     þ
r   ç        r   ç       Àr   c              
   C   s0   t  ¡ � t| ||||ƒW  5 Q R £ S Q R X 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   r	   r	   r
   Útrunc_normal_+   s    
r#   c              	   C   s8   t  ¡ �& t| dd||ƒ |  |¡ |¡ W 5 Q R X | 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   )r    r!   r   r   r   r"   r	   r	   r
   Útrunc_normal_tf_F   s    
r$   Úfan_inÚnormalc           	   	   C   sà   t | ƒ\}}|dkr|}n"|dkr(|}n|dkr<|| d }|| }|dkrdt| t |¡d d� nx|dkr”t ¡ � | jt |¡d� W 5 Q R X nH|d	krÎt d
| ¡}t ¡ � |  | |¡ W 5 Q R X ntd|› �ƒ‚d S )Nr%   Úfan_outZfan_avgr   Útruncated_normalg�©Û¶ä%ì?)r   r&   Úuniformé   zinvalid distribution )	r   r$   r   r   r    r!   Znormal_r   Ú
ValueError)	r   ÚscaleÚmodeÚdistributionr%   r'   ÚdenomZvarianceÚboundr	   r	   r
   Úvariance_scaling_c   s$    

r1   c                 C   s   t | ddd� d S )Nr%   r(   )r-   r.   )r1   )r   r	   r	   r
   Úlecun_normal_|   s    r2   )r   r   r   r   )r   r   r   r   )r   r%   r&   )
r    r   r   Ztorch.nn.initr   r   r#   r$   r1   r2   r	   r	   r	   r
   Ú<module>   s   #


