o
    hd                     @   s  d Z ddlZddlZddlmZmZmZmZm	Z	 ddl
mZ ddlZddlmZ g dZedZedZed	 Zed
 Z	dPdedededeej def
ddZ	dPdedededeej def
ddZ	dPdedededededeej defddZdededefddZdedefddZ	dPdedee	eef  defdd Z	!	"	dQdedededeej def
d#d$Z	!	"	dQdedededeej def
d%d&Z	!	"	'	(	dRdedededededeej defd)d*Zdededefd+d,Zdedefd-d.Z dedefd/d0Z!dedefd1d2Z"dSded4edefd5d6Z#dede$eef fd7d8Z%	"	dTded9edeej defd:d;Z&	"	dTded9edeej defd<d=Z'ded>edefd?d@Z(		A	B	dUdeded>ededeej defdCdDZ)		A	B	dUdeded>ededeej defdEdFZ*	3	dVded9edeej defdGdHZ+	I	dWdedJededeej def
dKdLZ,dMeeef deeef fdNdOZ-e-eZ.e-eZ/e-eZ0e-e"Z1e-e#Z2e-e&Z3e-e'Z4e-e)Z5e-e*Z6e-e+Z7e-e,Z8dS )XzHThis file contains utilities for initializing neural network parameters.    N)CallableLiteralOptionalTypeVarUnion)	ParamSpec)Tensor)calculate_gainuniform_normal_trunc_normal_	constant_ones_zeros_eye_dirac_xavier_uniform_xavier_normal_kaiming_uniform_kaiming_normal_orthogonal_sparse_uniformnormalconstanteyediracxavier_uniformxavier_normalkaiming_uniformkaiming_normal
orthogonalsparse_R_P)linearconv1dconv2dconv3dconv_transpose1dconv_transpose2dconv_transpose3dsigmoidtanhrelu
leaky_reluselu)fan_infan_outtensorab	generatorreturnc                 C   <   t   | j|||dW  d    S 1 sw   Y  d S Nr6   )torchno_gradr
   r3   r4   r5   r6    r>   S/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torch/nn/init.py_no_grad_uniform_D   s   
$r@   meanstdc                 C   r8   r9   )r;   r<   r   r3   rA   rB   r6   r>   r>   r?   _no_grad_normal_K   s   
$rD   c           	      C   s   dt dt fdd}||d|  k s||d|  kr tjddd t D ||| | }||| | }| jd| d d| d |d	 |   | |t	d
  | 
| | j||d | W  d    S 1 skw   Y  d S )Nxr7   c                 S   s   dt | t d  d S )N      ?       @)matherfsqrt)rE   r>   r>   r?   norm_cdf^   s   z(_no_grad_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:   rG   )minmax)floatwarningswarnr;   r<   r
   Zerfinv_mul_rH   rJ   Zadd_Zclamp_)	r3   rA   rB   r4   r5   r6   rK   lur>   r>   r?   _no_grad_trunc_normal_U   s    	 
 
$rX   valc                 C   s6   t   | |W  d    S 1 sw   Y  d S N)r;   r<   Zfill_r3   rY   r>   r>   r?   _no_grad_fill_   s   
$r\   c                 C   s4   t   |  W  d    S 1 sw   Y  d S rZ   )r;   r<   zero_r3   r>   r>   r?   _no_grad_zero_   s   
$r_   nonlinearityparamc                 C   s   g d}| |v s| dkrdS | dkrdS | dkrt dS | dkrM|d	u r(d
}nt|ts2t|ts7t|tr:|}ntd| dt dd|d   S | dkrT	 dS td|  )a  Return the recommended gain value for the given nonlinearity function.

    The values are as follows:

    ================= ====================================================
    nonlinearity      gain
    ================= ====================================================
    Linear / Identity :math:`1`
    Conv{1,2,3}D      :math:`1`
    Sigmoid           :math:`1`
    Tanh              :math:`\frac{5}{3}`
    ReLU              :math:`\sqrt{2}`
    Leaky Relu        :math:`\sqrt{\frac{2}{1 + \text{negative\_slope}^2}}`
    SELU              :math:`\frac{3}{4}`
    ================= ====================================================

    .. warning::
        In order to implement `Self-Normalizing Neural Networks`_ ,
        you should use ``nonlinearity='linear'`` instead of ``nonlinearity='selu'``.
        This gives the initial weights a variance of ``1 / N``,
        which is necessary to induce a stable fixed point in the forward pass.
        In contrast, the default gain for ``SELU`` sacrifices the normalization
        effect for more stable gradient flow in rectangular layers.

    Args:
        nonlinearity: the non-linear function (`nn.functional` name)
        param: optional parameter for the non-linear function

    Examples:
        >>> gain = nn.init.calculate_gain(
        ...     "leaky_relu", 0.2
        ... )  # leaky_relu with negative_slope=0.2

    .. _Self-Normalizing Neural Networks: https://papers.nips.cc/paper/2017/hash/5d44ee6f2c3f71b73125876103c8f6c4-Abstract.html
    )r%   r&   r'   r(   r)   r*   r+   r,   rO   r-   g?r.   rG   r/   N{Gz?znegative_slope z not a valid numberrL   r0   g      ?zUnsupported nonlinearity )rH   rJ   
isinstanceboolintrR   
ValueError)r`   ra   Z
linear_fnsZnegative_sloper>   r>   r?   r	      s.   &	
r	           rF   c                 C   4   t j| rt jjt| f| |||dS t| |||S )a  Fill the input Tensor with values drawn from the uniform distribution.

    :math:`\mathcal{U}(a, b)`.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        a: the lower bound of the uniform distribution
        b: the upper bound of the uniform distribution
        generator: the torch Generator to sample from (default: None)

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.uniform_(w)
    r=   )r;   	overrideshas_torch_function_variadichandle_torch_functionr
   r@   r=   r>   r>   r?   r
      
   r
   c                 C   rh   )a  Fill the input Tensor with values drawn from the normal distribution.

    :math:`\mathcal{N}(\text{mean}, \text{std}^2)`.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        mean: the mean of the normal distribution
        std: the standard deviation of the normal distribution
        generator: the torch Generator to sample from (default: None)

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.normal_(w)
    rC   )r;   ri   rj   rk   r   rD   rC   r>   r>   r?   r      rl   r          rG   c                 C   s   t | |||||dS )a  Fill 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`.

    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
        generator: the torch Generator to sample from (default: None)

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.trunc_normal_(w)
    r:   )rX   )r3   rA   rB   r4   r5   r6   r>   r>   r?   r     s   r   c                 C   s,   t j| rt jjt| f| |dS t| |S )zFill the input Tensor with the value :math:`\text{val}`.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        val: the value to fill the tensor with

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.constant_(w, 0.3)
    r[   )r;   ri   rj   rk   r   r\   r[   r>   r>   r?   r   *  s
   

r   c                 C   s
   t | dS )zFill the input Tensor with the scalar value `1`.

    Args:
        tensor: an n-dimensional `torch.Tensor`

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.ones_(w)
    rF   )r\   r^   r>   r>   r?   r   <  s   

r   c                 C   s   t | S )zFill the input Tensor with the scalar value `0`.

    Args:
        tensor: an n-dimensional `torch.Tensor`

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.zeros_(w)
    )r_   r^   r>   r>   r?   r   I  s   
r   c                 C   sX   |   dkr
tdt  tj| j| | jd W d   | S 1 s%w   Y  | S )a=  Fill the 2-dimensional input `Tensor` with the identity matrix.

    Preserves the identity of the inputs in `Linear` layers, where as
    many inputs are preserved as possible.

    Args:
        tensor: a 2-dimensional `torch.Tensor`

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.eye_(w)
    rL   ,Only tensors with 2 dimensions are supported)outrequires_gradN)
ndimensionrf   r;   r<   r   shaperp   r^   r>   r>   r?   r   V  s   

r   rO   groupsc                 C   s<  |   }|dvrtd|  }|d | dkrtd|d | }t||d }t g |   t|D ]U}t|D ]N}|dkrSd| || | || dd f< q<|dkrnd| || | || dd | dd f< q<d| || | || dd | dd | dd f< q<q6W d	   | S 1 sw   Y  | S )
aF  Fill the {3, 4, 5}-dimensional input `Tensor` with the Dirac delta function.

    Preserves the identity of the inputs in `Convolutional`
    layers, where as many input channels are preserved as possible. In case
    of groups>1, each group of channels preserves identity

    Args:
        tensor: a {3, 4, 5}-dimensional `torch.Tensor`
        groups (int, optional): number of groups in the conv layer (default: 1)
    Examples:
        >>> w = torch.empty(3, 16, 5, 5)
        >>> nn.init.dirac_(w)
        >>> w = torch.empty(3, 24, 5, 5)
        >>> nn.init.dirac_(w, 3)
    )         z5Only tensors with 3, 4, or 5 dimensions are supportedr   z!dim 0 must be divisible by groupsrO   rt   rL   ru   N)rq   rf   sizerP   r;   r<   r]   range)r3   rs   
dimensionssizesZout_chans_per_grpZmin_dimgdr>   r>   r?   r   k  sL   
"


r   c                 C   sp   |   }|dk rtd| d}| d}d}|   dkr,| jdd  D ]}||9 }q%|| }|| }||fS )NrL   zNFan in and fan out can not be computed for tensor with fewer than 2 dimensionsrO   r   )dimrf   rw   rr   )r3   ry   Znum_input_fmapsZnum_output_fmapsZreceptive_field_sizesr1   r2   r>   r>   r?   _calculate_fan_in_and_fan_out  s   


r   gainc                 C   sD   t | \}}|tdt||   }td| }t| | ||S )a  Fill the input `Tensor` with values using a Xavier uniform distribution.

    The method is described in `Understanding the difficulty of training
    deep feedforward neural networks` - Glorot, X. & Bengio, Y. (2010).
    The resulting tensor will have values sampled from
    :math:`\mathcal{U}(-a, a)` where

    .. math::
        a = \text{gain} \times \sqrt{\frac{6}{\text{fan\_in} + \text{fan\_out}}}

    Also known as Glorot initialization.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        gain: an optional scaling factor
        generator: the torch Generator to sample from (default: None)

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.xavier_uniform_(w, gain=nn.init.calculate_gain("relu"))

    Note:
        Be aware that ``fan_in`` and ``fan_out`` are calculated assuming
        that the weight matrix is used in a transposed manner,
        (i.e., ``x @ w.T`` in ``Linear`` layers, where ``w.shape = [fan_out, fan_in]``).
        This is important for correct initialization.
        If you plan to use ``x @ w``, where ``w.shape = [fan_in, fan_out]``,
        pass in a transposed weight matrix, i.e. ``nn.init.xavier_uniform_(w.T, ...)``.
    rG         @)r   rH   rJ   rR   r@   )r3   r   r6   r1   r2   rB   r4   r>   r>   r?   r     s   "r   c                 C   s4   t | \}}|tdt||   }t| d||S )a  Fill the input `Tensor` with values using a Xavier normal distribution.

    The method is described in `Understanding the difficulty of training deep feedforward
    neural networks` - Glorot, X. & Bengio, Y. (2010). The resulting tensor
    will have values sampled from :math:`\mathcal{N}(0, \text{std}^2)` where

    .. math::
        \text{std} = \text{gain} \times \sqrt{\frac{2}{\text{fan\_in} + \text{fan\_out}}}

    Also known as Glorot initialization.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        gain: an optional scaling factor
        generator: the torch Generator to sample from (default: None)

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.xavier_normal_(w)

    Note:
        Be aware that ``fan_in`` and ``fan_out`` are calculated assuming
        that the weight matrix is used in a transposed manner,
        (i.e., ``x @ w.T`` in ``Linear`` layers, where ``w.shape = [fan_out, fan_in]``).
        This is important for correct initialization.
        If you plan to use ``x @ w``, where ``w.shape = [fan_in, fan_out]``,
        pass in a transposed weight matrix, i.e. ``nn.init.xavier_normal_(w.T, ...)``.
    rG   rg   )r   rH   rJ   rR   rD   )r3   r   r6   r1   r2   rB   r>   r>   r?   r     s   !r   modec                 C   sH   |  }ddg}||vrtd| d| t| \}}|dkr"|S |S )Nr1   r2   zMode z" not supported, please use one of )lowerrf   r   )r3   r   Zvalid_modesr1   r2   r>   r>   r?   _calculate_correct_fan  s   r   r1   r/   c           	   	   C   s   t j| rt jjt| f| ||||dS d| jv r td | S t| |}t	||}|t
| }t
d| }t   | j| ||dW  d   S 1 sPw   Y  dS )a  Fill the input `Tensor` with values using a Kaiming uniform distribution.

    The method is described in `Delving deep into rectifiers: Surpassing
    human-level performance on ImageNet classification` - He, K. et al. (2015).
    The resulting tensor will have values sampled from
    :math:`\mathcal{U}(-\text{bound}, \text{bound})` where

    .. math::
        \text{bound} = \text{gain} \times \sqrt{\frac{3}{\text{fan\_mode}}}

    Also known as He initialization.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        a: the negative slope of the rectifier used after this layer (only
            used with ``'leaky_relu'``)
        mode: either ``'fan_in'`` (default) or ``'fan_out'``. Choosing ``'fan_in'``
            preserves the magnitude of the variance of the weights in the
            forward pass. Choosing ``'fan_out'`` preserves the magnitudes in the
            backwards pass.
        nonlinearity: the non-linear function (`nn.functional` name),
            recommended to use only with ``'relu'`` or ``'leaky_relu'`` (default).
        generator: the torch Generator to sample from (default: None)

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.kaiming_uniform_(w, mode="fan_in", nonlinearity="relu")

    Note:
        Be aware that ``fan_in`` and ``fan_out`` are calculated assuming
        that the weight matrix is used in a transposed manner,
        (i.e., ``x @ w.T`` in ``Linear`` layers, where ``w.shape = [fan_out, fan_in]``).
        This is important for correct initialization.
        If you plan to use ``x @ w``, where ``w.shape = [fan_in, fan_out]``,
        pass in a transposed weight matrix, i.e. ``nn.init.kaiming_uniform_(w.T, ...)``.
    )r3   r4   r   r`   r6   r   ,Initializing zero-element tensors is a no-opr   r:   N)r;   ri   rj   rk   r   rr   rS   rT   r   r	   rH   rJ   r<   r
   )	r3   r4   r   r`   r6   fanr   rB   boundr>   r>   r?   r     s(   +





$r   c                 C   sv   d| j v rtd | S t| |}t||}|t| }t  | j	d||dW  d   S 1 s4w   Y  dS )a  Fill the input `Tensor` with values using a Kaiming normal distribution.

    The method is described in `Delving deep into rectifiers: Surpassing
    human-level performance on ImageNet classification` - He, K. et al. (2015).
    The resulting tensor will have values sampled from
    :math:`\mathcal{N}(0, \text{std}^2)` where

    .. math::
        \text{std} = \frac{\text{gain}}{\sqrt{\text{fan\_mode}}}

    Also known as He initialization.

    Args:
        tensor: an n-dimensional `torch.Tensor`
        a: the negative slope of the rectifier used after this layer (only
            used with ``'leaky_relu'``)
        mode: either ``'fan_in'`` (default) or ``'fan_out'``. Choosing ``'fan_in'``
            preserves the magnitude of the variance of the weights in the
            forward pass. Choosing ``'fan_out'`` preserves the magnitudes in the
            backwards pass.
        nonlinearity: the non-linear function (`nn.functional` name),
            recommended to use only with ``'relu'`` or ``'leaky_relu'`` (default).
        generator: the torch Generator to sample from (default: None)

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.kaiming_normal_(w, mode="fan_out", nonlinearity="relu")

    Note:
        Be aware that ``fan_in`` and ``fan_out`` are calculated assuming
        that the weight matrix is used in a transposed manner,
        (i.e., ``x @ w.T`` in ``Linear`` layers, where ``w.shape = [fan_out, fan_in]``).
        This is important for correct initialization.
        If you plan to use ``x @ w``, where ``w.shape = [fan_in, fan_out]``,
        pass in a transposed weight matrix, i.e. ``nn.init.kaiming_normal_(w.T, ...)``.
    r   r   r:   N)
rr   rS   rT   r   r	   rH   rJ   r;   r<   r   )r3   r4   r   r`   r6   r   r   rB   r>   r>   r?   r   P  s   
+



$r   c           
      C   s   |   dk r
td|  dkr| S | d}|  | }| ||fjdd|d}||k r2|  tj	|\}}t
|d}| }	||	9 }||k rP|  t  | || | | W d   | S 1 smw   Y  | S )a  Fill the input `Tensor` with a (semi) orthogonal matrix.

    Described in `Exact solutions to the nonlinear dynamics of learning in deep
    linear neural networks` - Saxe, A. et al. (2013). The input tensor must have
    at least 2 dimensions, and for tensors with more than 2 dimensions the
    trailing dimensions are flattened.

    Args:
        tensor: an n-dimensional `torch.Tensor`, where :math:`n \geq 2`
        gain: optional scaling factor
        generator: the torch Generator to sample from (default: None)

    Examples:
        >>> # xdoctest: +REQUIRES(env:TORCH_DOCTEST_LAPACK)
        >>> w = torch.empty(3, 5)
        >>> nn.init.orthogonal_(w)
    rL   z4Only tensors with 2 or more dimensions are supportedr   rO   r:   N)rq   rf   Znumelrw   Z	new_emptyr   Zt_r;   ZlinalgZqrZdiagsignr<   Zview_asZcopy_rU   )
r3   r   r6   rowscolsZ	flattenedqrr|   phr>   r>   r?   r     s,   


r   rb   sparsityc           
      C   s   |   dkr
td| j\}}tt|| }t ) | jd||d t	|D ]}t
|}|d| }	d| |	|f< q)W d   | S 1 sHw   Y  | S )a  Fill the 2D input `Tensor` as a sparse matrix.

    The non-zero elements will be drawn from the normal distribution
    :math:`\mathcal{N}(0, 0.01)`, as described in `Deep learning via
    Hessian-free optimization` - Martens, J. (2010).

    Args:
        tensor: an n-dimensional `torch.Tensor`
        sparsity: The fraction of elements in each column to be set to zero
        std: the standard deviation of the normal distribution used to generate
            the non-zero values
        generator: the torch Generator to sample from (default: None)

    Examples:
        >>> w = torch.empty(3, 5)
        >>> nn.init.sparse_(w, sparsity=0.1)
    rL   rn   r   r:   N)rq   rf   rr   re   rH   ceilr;   r<   r   rx   Zrandperm)
r3   r   rB   r6   r   r   Z	num_zerosZcol_idxZrow_indicesZzero_indicesr>   r>   r?   r     s   



r   methc                    sX    j d d dtjdtjdtf fdd}d d d	 d
|_|_ |S )Nargskwargsr7   c                     s,   t jd d dtdd  | i |S )Nz	`nn.init.z)` is now deprecated in favor of `nn.init.z`.rL   rM   )rS   rT   FutureWarning)r   r   r   new_nameZold_namer>   r?   deprecated_init  s   z(_make_deprecate.<locals>.deprecated_initz
    z_(...)

    .. warning::
        This method is now deprecated in favor of :func:`torch.nn.init.z"`.

    See :func:`~torch.nn.init.z` for details.)__name__r$   r   r   r#   __doc__)r   r   r>   r   r?   _make_deprecate  s   "
r   rZ   )rg   rF   N)rg   rF   rm   rG   N)rO   )rF   N)r   r1   r/   N)rO   N)rb   N)9r   rH   rS   typingr   r   r   Z	_Optionalr   r   Ztyping_extensionsr   r;   r   __all__r#   r$   Z_NonlinearityTypeZ_FanModerR   	Generatorr@   rD   rX   r\   r_   re   r	   r
   r   r   r   r   r   r   r   tupler   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r>   r>   r>   r?   <module>   s   


,
L


5
+
'
C
7
6
"'