
    {-j(                    L   d dl mZ d dlZd dlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZ ddlmZmZ d6dZ	 	 	 d7d8dZ	 	 	 d7d8dZ	 	 	 d9d:d"Z	 	 	 d9d:d#Z	 	 d;d<d&Z 	 	 d;d=d)Z!	 	 	 	 d>d?d,Z"d@d.Z#dAd/Z$dAd0Z%	 dBdCd3Z&dAd4Z'	 dBdDd5Z(dS )E    )annotationsN   )in_dygraph_modein_pir_mode   ConstantDirac)calculate_gain)KaimingNormalKaimingUniform)NormalTruncatedNormal
OrthogonalUniform)XavierNormalXavierUniformvarpaddle.Tensorreturntuple[int, int]c                h   | j         }|rt          |          dk    rdx}}nt          |          dk    r|d         x}}npt          |          dk    r|d         }|d         }nLt          j        |dd                   }t	          |d         |z            }t	          |d         |z            }||fS )a  Compute the fan_in and the fan_out for layers

    This method computes the fan_in and the fan_out
    for neural network layers, if not specified. It is
    not possible to perfectly estimate fan_in and fan_out.
    This method will estimate it correctly for matrix multiply and
    convolutions.

    Args:
        var: variable for which fan_in and fan_out have to be computed.

    Returns:
        tuple of two integers (fan_in, fan_out).
    r   r   r   N)shapelennpprodint)r   r   fan_infan_outreceptive_field_sizes        N/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/nn/init.py_calculate_fan_in_and_fan_outr%       s     IE 7CJJ!OO	Uq 8#	Uqq(  "wuQRRy11U1X 4455eAh!5566G    r!   
leaky_relutensorafloatmodestrnonlinearitypaddle.Tensor | Nonec                <    t          |||          } ||           S )a  Modify tensor inplace using Kaiming uniform method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        a (float, optional): The negative slope of the rectifier used after this layer.
            Defaults to 0.
        mode (str, optional): Mode to compute the fan. Choose from ["fan_in", "fan_out"].
            When set to 'fan_in', the fan_in parameter is used for initialization.
            When set to 'fan_out', the out_features of trainable Tensor will be used.
            Default is 'fan_in'.
        nonlinearity (str, optional): Nonlinearity method name. Defaults to "leaky_relu".

    Returns:
        Tensor: Initialized tensor.
    negative_sloper-   r+   )r   r(   r)   r+   r-   inits        r$   kaiming_uniform_r4   C   s/    * |$  D 4<<r&   c                <    t          |||          } ||           S )a  Modify tensor inplace using Kaiming normal method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        a (float, optional): The negative slope of the rectifier used after this layer.
            Defaults to 0.
        mode (str, optional): Mode to compute the fan. Choose from ["fan_in", "fan_out"].
            When set to 'fan_in', the fan_in parameter is used for initialization.
            When set to 'fan_out', the out_features of trainable Tensor will be used.
            Default is 'fan_in'.
        nonlinearity (str, optional): Nonlinearity method name. Defaults to "leaky_relu".

    Returns:
        Tensor: Initialized tensor.
    r0   )r   r2   s        r$   kaiming_normal_r6   _   s&    * 4PPPD4<<r&         ?gainfloat | Noner"   c                <    t          |||          } ||           S )a  Modify tensor inplace using Xavier uniform method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        gain (float, optional): Scaling Tensor. Default is 1.0.
        fan_in (float|None, optional): fan_in for Xavier initialization, which is
                inferred from the Tensor. Default is None.
        fan_out (float|None, optional): fan_out for Xavier initialization, which is
                 inferred from the Tensor. Default is None.

    Returns:
        Tensor: Initialized tensor.
    r8   r!   r"   )r   r(   r8   r!   r"   r3   s        r$   xavier_uniform_r=   y   s1    &   D 4<<r&   c                <    t          |||          } ||           S )a  Modify tensor inplace using Xavier normal method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        gain (float, optional): Scaling Tensor. Default is 1.0.
        fan_in (float|None, optional): fan_in for Xavier initialization, which is
                inferred from the Tensor. Default is None.
        fan_out (float|None, optional): fan_out for Xavier initialization, which is
                 inferred from the Tensor. Default is None.

    Returns:
        Tensor: Initialized tensor.
    r;   )r   r<   s        r$   xavier_normal_r?      s1    &   D 4<<r&           bc                :    t          ||          } ||           S )a^  Modify tensor inplace using uniform method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        low (float, optional): Lower boundary of the uniform distribution. Default is :math:`-1.0`.
        high (float, optional): Upper boundary of the uniform distribution. Default is :math:`1.0`.

    Returns:
        Tensor: Initialized tensor.
    )lowhighr   )r(   r)   rA   r3   s       r$   uniform_rE      s$     qq!!!D4<<r&   meanstdc                :    t          ||          } ||           S )aL  Modify tensor inplace using normal method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        mean (float|complex, optional): mean of the normal distribution. Default is 0.0.
        std (float, optional): standard deviation of the normal distribution. Default is 1.0.

    Returns:
        Tensor: Initialized tensor.
    )rF   rG   )r   )r(   rF   rG   r3   s       r$   normal_rI      s$     t%%%D4<<r&                 @c                >    t          ||||          } ||           S )a  Modify tensor inplace using truncated normal method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        mean (float|complex, optional): mean of the normal distribution. Default is 0.0.
        std (float, optional): standard deviation of the normal distribution. Default is 1.0.
        a (float, optional): The minimum cutoff value. Default is -2.0.
        b (float, optional): The maximum cutoff value. Default is 2.0.

    Returns:
        Tensor: Initialized tensor.
    )rF   rG   r)   rA   )r   )r(   rF   rG   r)   rA   r3   s         r$   trunc_normal_rM      s(    & #a888D4<<r&   valc                8    t          |          } ||           S )zModify tensor inplace using constant method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        value (float32|float64, optional): constant value to initialize the parameter.

    Returns:
        Tensor: Initialized tensor.
    valuer   )r(   rN   r3   s      r$   	constant_rR      s"     #D4<<r&   c                8    t          d          } ||           S )zFill the input Tensor with the scalar value 1.

    Args:
        tensor (Tensor):  Paddle Tensor.

    Returns:
        Tensor: Initialized tensor.
    r7   rP   r   r(   r3   s     r$   ones_rU     "     #D4<<r&   c                8    t          d          } ||           S )zFill the input Tensor with the scalar value 0.

    Args:
        tensor (Tensor):  Paddle Tensor.

    Returns:
        Tensor: Initialized tensor.
    r@   rP   r   rT   s     r$   zeros_rX     rV   r&   groupsr    c                8    t          |          } ||           S )a4  Initialize the 3D/4D/5D Tensor with Dirac delta function.

    Args:
        tensor (Tensor):  Paddle Tensor.
        groups (int|None, optional): 0-dimension of the Tensor will be divided by groups,
            each group has the same value. Default: 1.
    Returns:
        Tensor: Initialized tensor.
    )rY   r
   )r(   rY   r3   s      r$   dirac_r[   #  s"     D4<<r&   c                   t          | j                  dk    r%t          dt          | j                   d          t                      rIt	          j        | j        d         | j        d         | j                  }|                    |            dS t                      r4t	          j        | j        d         | j        d         | j                  }|S t          d          )	zFill the 2-dimensional input Tensor with the identity matrix.

    Args:
        tensor (Tensor):  Paddle Tensor.
    Returns:
        Tensor: Initialized tensor.
    r   z+Only support 2 dimensional tensor, but got .r   r   )dtypeNz-Only support run in dygraph mode or PIR mode.)
r   r   AssertionErrorr   paddleeyer^   _share_underline_tensor_tor   NotImplementedError)r(   
new_tensors     r$   eye_re   5  s     6<AN#fl:K:KNNN
 
 	
  
ZLOV\!_FL
 
 

 	--f555t	 
ZLOV\!_FL
 
 

 !;
 
 	
r&   c                8    t          |          } ||           S )a  Fill the input Tensor with a (semi) orthogonal matrix.

    Args:
        tensor (Tensor):  Paddle Tensor.
        gain(float, optional): The multiplication coefficient for initialized tensor. Default: 1.0.
    Returns:
        Tensor: Initialized tensor.
    )r8   r   )r(   r8   r3   s      r$   orthogonal_rg   V  s"     4   D4<<r&   )r   r   r   r   )r   r!   r'   )
r(   r   r)   r*   r+   r,   r-   r,   r   r.   )r7   NN)
r(   r   r8   r*   r!   r9   r"   r9   r   r.   )r@   r7   )r(   r   r)   r*   rA   r*   r   r.   )r(   r   rF   r*   rG   r*   r   r.   )r@   r7   rJ   rK   )r(   r   rF   r*   rG   r*   r)   r*   rA   r*   r   r.   )r(   r   rN   r*   r   r.   )r(   r   r   r.   )r   )r(   r   rY   r    r   r.   )r(   r   r8   r*   r   r.   ))
__future__r   numpyr   r`   base.frameworkr   r   initializer.constantr	   initializer.diracr   initializer.initializerr   initializer.kaimingr   r   initializer.normalr   r   initializer.orthogonalr   initializer.uniformr   initializer.xavierr   r   r%   r4   r6   r=   r?   rE   rI   rM   rR   rU   rX   r[   re   rg    r&   r$   <module>rt      s   # " " " " "      9 9 9 9 9 9 9 9 * * * * * * $ $ $ $ $ $ 3 3 3 3 3 3 > > > > > > > > 7 7 7 7 7 7 7 7 . . . . . . ( ( ( ( ( ( ; ; ; ; ; ; ; ;       J $	    < $	    8  	    <  	    <     ,     ,     0   $       $     $
 
 
 
F       r&   