
    {-j8                        d dl mZ d dlZd dlmZmZ ddlmZmZmZ ddl	m
Z
 ddlmZmZmZ dd	lmZ dd
lmZ g Z G d de          Z G d de          Z G d de          Z G d de          ZdS )    )annotationsN)_C_opspir   )core	frameworkunique_name)check_variable_and_dtype)_current_expected_placein_dygraph_modein_pir_mode   )Initializer)lazy_init_helperc                  4     e Zd ZdZ	 dd fdZ	 dddZ xZS )NormalInitializeraA  Implements the Random Normal(Gaussian) distribution initializer

    Args:
        loc (float|complex, optional): mean of the normal distribution. Default is 0.0.
        scale (float, optional): standard deviation of the normal distribution. Default is 1.0.
        seed (int, optional): random seed. Default is 0.

                  ?r   locfloatscaleseedintreturnNonec                j   |J |J |J t                                                       || _        || _        || _        t          | j        t                    rV| j        j        | j        j        k    r)t          d| j        j         d| j        j                   | j        j        | _        d S d S )NzVif mean is a complex number, its real part should equal imag part, but got real part: z != imag part: )
super__init___mean_std_dev_seed
isinstancecomplexrealimag
ValueError)selfr   r   r   	__class__s       \/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/nn/initializer/normal.pyr   zNormalInitializer.__init__+   s        

dj'** 	)z$*/11 \*.*/\ \JN*/\ \   DJJJ	) 	)    Nvarpaddle.Tensorblockpir.Block | Nonepaddle.Tensor | Nonec           
     <   t          |t          j                  r|                                r
J d            |                     |          }t          |t          j        t          j        f          sJ t          |dg dd           | j        dk    r|j	        j
        | _        t                      rWt                      }t          j        |j        | j        | j        | j        |j        |          }|                    |           dS t)                      rBt                      }t          j        |j        | j        | j        | j        |j        |          }|S |                    dd|i|j        |j        | j        | j        | j        dd	          }||_        |S )
a\  Initialize the input tensor with Normal distribution.

        Args:
            var(Tensor): Tensor that needs to be initialized.
            block(Block|None, optional): The block in which initialization ops
                   should be added. Used in static graph only, default None.

        Returns:
            The initialization op.
        zCCurrently, normal initializer not support lazy init for dist param.Out)uint16float16float32float64	complex64
complex128gaussian_randomr   N)shapedtypemeanstdr   Ttypeoutputsattrsstop_gradient)r"   r   EagerParamBaseis_dist_check_blockBlockr   r
   r!   programrandom_seedr   r   r   gaussianr9   r   r    r:   _share_underline_tensor_tor   	append_opop)r'   r+   r-   placeout_varrK   s         r)   forwardzNormalInitializer.forward=   s    sI455	Q:=++--	Q 	QP	Q 	Q 
 !!%((%)/39!=>>>>>    	
 	
 	
 :??2DJ %	+--Eo	

	 G ..s3334]] 	+--Eo	

	 G N& Y Y J= J  # !  B CFIr*   )r   r   r   )r   r   r   r   r   r   r   r   Nr+   r,   r-   r.   r   r/   __name__
__module____qualname____doc__r   rN   __classcell__r(   s   @r)   r   r   !   s{          AB) ) ) ) ) ) )& =AJ J J J J J J J Jr*   r   c                  (     e Zd ZdZ	 dd fdZ xZS )Normalam  The Random Normal (Gaussian) distribution initializer.

    Args:
        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.
        name(str|None, optional): The default value is None. Normally there is no need for user to set this
            property. For more information, please refer to :ref:`api_guide_Name`. Default: None.

    Returns:
        A parameter initialized by Random Normal (Gaussian) distribution.

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> data = paddle.ones(shape=[3, 1, 2], dtype='float32')
            >>> weight_attr = paddle.framework.ParamAttr(
            ...     name="linear_weight",
            ...     initializer=paddle.nn.initializer.Normal(mean=0.0, std=2.0))
            >>> bias_attr = paddle.framework.ParamAttr(
            ...     name="linear_bias",
            ...     initializer=paddle.nn.initializer.Normal(mean=0.0, std=2.0))
            >>> # doctest: +SKIP('name has been used')
            >>> linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr)
            >>> print(linear.weight)
            Parameter containing:
            Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[ 2.1973135 -2.2697184],
             [-1.9104223 -1.0541488]])
            >>> print(linear.bias)
            Parameter containing:
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [ 0.7885926  -0.74719954])
            >>> res = linear(data)
            >>> print(res)
            Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[[ 1.0754838 -4.071067 ]],
             [[ 1.0754838 -4.071067 ]],
             [[ 1.0754838 -4.071067 ]]])
    r   r   Nr;   r   r<   name
str | Noner   r   c                    |
J d            |
J d            t                                          ||d           d S )Nmean should not be Nonestd should not be Noner   )r   r   r   r   r   )r'   r;   r<   rZ   r(   s       r)   r   zNormal.__init__   sO     !: 8T155555r*   )r   r   N)r;   r   r<   r   rZ   r[   r   r   rR   rS   rT   rU   r   rV   rW   s   @r)   rY   rY      sT        ( (V GK6 6 6 6 6 6 6 6 6 6 6r*   rY   c                  <     e Zd ZdZ	 	 	 	 	 dd fdZ	 dddZ xZS )TruncatedNormalInitializera|  Implements the Random TruncatedNormal(Gaussian) distribution initializer

    Note:
        It is better to set `a <= mean <= b`.
        If `mean < a - 2*std` or `mean > b + 2*std`, the distribution of values may be incorrect.

    Args:
        loc (float, optional): Mean of the normal distribution. Default is :math:`0.0`.
        scale (float, optional): Standard deviation of the normal distribution. Default is :math:`1.0`.
        seed (int, optional): random seed. Default is 0.
        a (float, optional): The minimum cutoff value. Default is -2.0.
        b (float, optional): The maximum cutoff value. Default is 2.0.

    r   r   r                 @r   r   r   r   r   abr   r   c                    |J |J |J |J |J t                                                       || _        || _        || _        || _        || _        d S rO   )r   r   r   r    r!   _a_b)r'   r   r   r   re   rf   r(   s         r)   r   z#TruncatedNormalInitializer.__init__   su        }}}}}}

r*   Nr+   r,   r-   r.   r/   c                   |                      |          }t                      j        r3t          j        t
          j        j        j        t          j	        j
        f}n2t          j        t
          j        j        t
          j        j        j        f}t          ||          sJ t          |t          j        t          j        f          sJ | j        dk    r|j        j        | _        |j        t          j        j        j        t          j        j        j        fv rwt          j        j        j        }|                    t1          j        d                    d|j        dg                    |j        |t          j        j        j        d          }n	|j        }|}t=                      rt?          j         |j        | j!        | j"        | j        | j#        | j$        |tK                                }|j        t          j        j        j        t          j        j        j        fv r0t?          j&        ||j                  }|'                    |           n|'                    |           dS tQ                      rt?          j         |j        | j!        | j"        | j        | j#        | j$        |tK                                }|j        t          j        j        j        t          j        j        j        fv r/t?          j&        ||j                  }|'                    |           |S |)                    dd|i|j        || j!        | j"        | j        | j#        | j$        d	d
          }|j        t          j        j        j        t          j        j        j        fv r*|)                    dd|id|i|j        |j        d           ||_*        |S )ad  Initialize the input tensor with TruncatedNormal distribution.

        Args:
            var(Tensor): Tensor that needs to be initialized.
            block(Block|None, optional): The block in which initialization ops
                   should be added. Used in static graph only, default None.

        Returns:
            The initialization op
        r   .truncated_gaussian_randomtmpF)rZ   r9   r:   r>   persistableNr1   )r9   r:   r;   r<   r   re   rf   Tr=   castX)in_dtype	out_dtype)r>   inputsr?   r@   )+rD   r   stater   Variablepaddler   r   ParameterMetaeagerTensorValuer"   rE   r!   rF   rG   r:   VarDescVarTypeFP16BF16FP32
create_varr	   generatejoinrZ   r9   DENSE_TENSORr   r   rl   r   r    rh   ri   r   ro   rI   r   rJ   rK   )r'   r+   r-   expectedrr   rM   var_tmprK   s           r)   rN   z"TruncatedNormalInitializer.forward   s~    !!%((# 	"
-
!HH "
 
-H #x(((((%)/39!=>>>>>:??2DJ 9-2DL4H4MNNN,1I&& )HH938UKLL  i\)6! '  GG 	IG C	6	

'))	 	G y$)$)   !+gsy9922377772237774]] .	6	

'))	 	G y$)$)   !+gsy99223777N 0( Y& J= J  # !  B y$)$)   >"CL'.}39MM	      CFIr*   )r   r   r   rc   rd   )r   r   r   r   r   r   re   r   rf   r   r   r   rO   rP   rQ   rW   s   @r)   rb   rb      s         "       * =At t t t t t t t tr*   rb   c                  0     e Zd ZdZ	 	 	 	 	 dd fdZ xZS )TruncatedNormala  The truncated normal distribution (Gaussian distribution) initializer.

    Note:
        It is better to set `a <= mean <= b`.
        If `mean < a - 2*std` or `mean > b + 2*std`, the distribution of values may be incorrect.

    Args:
        mean (float, optional): Mean of the normal distribution. Default is :math:`0.0`.
        std (float, optional): Standard deviation of the normal distribution. Default is :math:`1.0`.
        a (float, optional): The minimum cutoff value. Default is -2.0.
        b (float, optional): The maximum cutoff value. Default is 2.0.
        name (str|None, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

    Returns:
        A parameter initialized by truncated normal distribution (Gaussian distribution).

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> data = paddle.ones(shape=[3, 1, 2], dtype='float32')
            >>> weight_attr = paddle.framework.ParamAttr(
            ...     name="linear_weight",
            ...     initializer=paddle.nn.initializer.TruncatedNormal(mean=0.0, std=2.0))
            >>> bias_attr = paddle.framework.ParamAttr(
            ...     name="linear_bias",
            ...     initializer=paddle.nn.initializer.TruncatedNormal(mean=0.0, std=2.0))
            >>> # doctest: +SKIP('name has been used')
            >>> linear = paddle.nn.Linear(2, 2, weight_attr=weight_attr, bias_attr=bias_attr)
            >>> print(linear.weight)
            Parameter containing:
            Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[-1.0981836  1.4140984],
             [ 3.1390522 -2.8266568]])
            >>> print(linear.bias)
            Parameter containing:
            Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [ -2.1546738  -1.6570673])
            >>> res = linear(data)
            >>> print(res)
            Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[[-0.11380529 -3.0696259 ]],
             [[-0.11380529 -3.0696259 ]],
             [[-0.11380529 -3.0696259 ]]])
    r   r   rc   rd   Nr;   r   r<   re   rf   rZ   r[   r   r   c                    |
J d            |
J d            |
J d            |
J d            t                                          ||d||           d S )Nr]   r^   za should not be Nonezb should not be Noner   )r   r   r   re   rf   r_   )r'   r;   r<   re   rf   rZ   r(   s         r)   r   zTruncatedNormal.__init__  ss     !: 8}}4}}}}}4}}}T1Q?????r*   )r   r   rc   rd   N)r;   r   r<   r   re   r   rf   r   rZ   r[   r   r   r`   rW   s   @r)   r   r   X  si        - -b @ @ @ @ @ @ @ @ @ @ @r*   r   )
__future__r   rv   r   r   baser   r   r	   base.data_feederr
   base.frameworkr   r   r   initializerr   	lazy_initr   __all__r   rY   rb   r    r*   r)   <module>r      s   # " " " " "          0 0 0 0 0 0 0 0 0 0 8 8 8 8 8 8         
 % $ $ $ $ $ ' ' ' ' ' '
f f f f f f f fR06 06 06 06 06 06 06 06fX X X X X X X Xv<@ <@ <@ <@ <@0 <@ <@ <@ <@ <@r*   