a
    d                     @   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	   `/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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   s<   t    t| ||||W  d   S 1 s.0    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   r	   r	   r
   trunc_normal_+   s    
r#   c                 C   sL   t  0 t| dd|| | || W d   n1 s>0    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"   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 n|dkrt " | jt|d W d    n1 s0    Y  n\|d	krtd
| }t  | | | W d    n1 s0    Y  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   Zscalemodedistributionr%   r'   ZdenomZvarianceboundr	   r	   r
   variance_scaling_c   s$    
2
.r/   c                 C   s   t | ddd d S )Nr%   r(   )r,   r-   )r/   )r   r	   r	   r
   lecun_normal_|   s    r0   )r   r   r   r   )r   r   r   r   )r   r%   r&   )
r    r   r   Ztorch.nn.initr   r   r#   r$   r/   r0   r	   r	   r	   r
   <module>   s   #


