
    ёi9<                        S SK Jr  S SKrS SKrS SKJr  SSKJrJrJr  SSK	J
r
  SSKJrJrJr  SS	KJr  / r " S
 S\5      r " S S\5      r " S S\5      rg)    )annotationsN)_C_ops   )core	frameworkunique_name)check_variable_and_dtype)_current_expected_placein_dygraph_modein_pir_mode   )Initializerc                  n   ^  \ rS rSrSr     S           SU 4S jjjr S     S	S jjrSrU =r$ )
XavierInitializer"   a  
This class implements the Xavier weight initializer from the paper
`Understanding the difficulty of training deep feedforward neural
networks <http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf>`_
by Xavier Glorot and Yoshua Bengio.

This initializer is designed to keep the scale of the gradients
approximately same in all the layers. In case of Uniform distribution,
the range is [-x, x], where

.. math::

    x = gain \times \sqrt{\\frac{6.0}{fan\_in + fan\_out}}

In case of Normal distribution, the mean is 0 and the standard deviation
is

.. math::

   gain \times \sqrt{\\frac{2.0}{fan\_in + fan\_out}}


Args:
    uniform (bool, optional): whether to use uniform ,if False use normal distribution. Default is True.
    fan_in (float|None, optional): fan_in for Xavier initialization. If None, it is
            inferred from the variable. Default is None.
    fan_out (float|None, optional): fan_out for Xavier initialization. If None, it is
             inferred from the variable. Default is None.
    seed (int, optional): Random seed. Default is 0.
    gain (float, optional): Scaling Tensor. Default is 1.0.

Note:
    It is recommended to set fan_in and fan_out to None for most cases.

c                r   > Uc   eUc   e[         TU ]  5         Xl        X l        X0l        X@l        XPl        g N)super__init___uniform_fan_in_fan_out_seed_gain)selfuniformfan_infan_outseedgain	__class__s         \/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/nn/initializer/xavier.pyr   XavierInitializer.__init__G   sA     """

    c                   U R                  U5      n[        U[        R                  [        R
                  R                  45      (       d   e[        U[        R
                  R                  R                  5      (       d  [        US/ SQS5        U R                  U5      u  p4U R                  c  UOU R                  nU R                  c  UOU R                  nU R                  S:X  a  UR                  R                  U l        [        U[        R                  5      (       a!  UR!                  5       (       a  UR"                  OUR$                  nUR&                  nU[        R(                  R*                  R,                  :X  d9  U[        R(                  R*                  R.                  :X  a  U R0                  (       d  [        R(                  R*                  R2                  n	UR5                  [6        R8                  " SR;                  SUR<                  S/5      5      UU	[        R(                  R*                  R>                  SS	9n
OjU[        R@                  RB                  [        R@                  RD                  4;   a.  U R0                  (       d  [        R@                  RF                  n	Un
OUn	Un
[I        5       (       Ga  U R0                  (       aj  SXV4;   a  S
nO1U RJ                  [L        RN                  " S[Q        XV-   5      -  5      -  n[R        RT                  " UU	U* UU R                  [W        5       5      n
OjSXV4;   a  S
nO1U RJ                  [L        RN                  " S[Q        XV-   5      -  5      -  n[W        5       n[R        RX                  " US
UU R                  U	U5      n
U[        R(                  R*                  R,                  :X  dl  U[        R(                  R*                  R.                  [        R@                  RB                  [        R@                  RD                  4;   a'  U R0                  (       d  [R        RZ                  " X5      n
[        U[        R                  5      (       a]  UR!                  5       (       aH  [        R\                  R^                  R`                  Rc                  XRd                  URf                  5      n
U
Ri                  U5        g[k        5       (       Gaa  U R0                  (       a}  SXV4;   a  S
nO1U RJ                  [L        RN                  " S[Q        XV-   5      -  5      -  n[        Rl                  RU                  U
R$                  U	U* UU R                  [W        5       5      n
OrSXV4;   a  S
nO1U RJ                  [L        RN                  " S[Q        XV-   5      -  5      -  n[R        RX                  " U
R$                  S
UU R                  U	[W        5       5      n
U[        R@                  RB                  [        R@                  RD                  4;   a'  U R0                  (       d  [R        RZ                  " X5      $ U
$ U R0                  (       al  SXV4;   a  S
nO1U RJ                  [L        RN                  " S[Q        XV-   5      -  5      -  nURo                  S0 SU
0U
R$                  U	U* UU R                  S.SS9nOsSXV4;   a  S
nO1U RJ                  [L        RN                  " S[Q        XV-   5      -  5      -  nURo                  SSU
0U
R$                  U
R&                  S
UU R                  S.SS9nU[        R(                  R*                  R,                  :X  d9  U[        R(                  R*                  R.                  :X  a4  U R0                  (       d#  URo                  SSU
0SU0U
R&                  US.S9  Xl8        U$ )a%  Initialize the input tensor with Xavier initialization.

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
Out)uint16float16float32float64xavier_initNr   .tmpF)nameshapedtypetypepersistableg        g      @g       @uniform_random)r/   r0   minmaxr   T)r1   inputsoutputsattrsstop_gradientgaussian_random)r/   r0   meanstdr   )r1   r7   r8   r9   castX)in_dtype	out_dtype)r1   r6   r7   r8   )9_check_block
isinstancer   Blockpaddlepirr   ParameterMetar	   _compute_fansr   r   r   programrandom_seedEagerParamBaseis_dist_local_shaper/   r0   VarDescVarTypeFP16BF16r   FP32
create_varr   generatejoinr.   DENSE_TENSORDataTypeFLOAT16BFLOAT16FLOAT32r   r   mathsqrtfloatr   r   r
   gaussianr=   distributedauto_parallelapidtensor_from_localprocess_mesh
placements_share_underline_tensor_tor   _pir_ops	append_opop)r   varblockf_inf_outr   r   out_var_shapeorigin_dtyper@   out_varlimitr<   placerg   s                  r"   forwardXavierInitializer.forwardX   s%    !!%(%)//6::3C3C!DEEEE#vzz<<==$;	 ((- -4<<==0%dmm::?22DJ 3	 8 899ckkmm  	 yy4<<//444DLL00555dmm,,11I&& ))HHmSXXu=> $\\))66! ' G T]]22DMM4J4JKKMM--IG$IG}}))E JJeF$455* E !..!FJJ+- ))C**tyyuV=M7N1N'OOC/1 //!JJ t||33888LL((--MM))MM**  ++g<#y7788S[[]] &&4488KK!1!13>> 
 ..s3]]}}))E JJeF$455* E !//11MMFJJ+- ))C**tyyuV=M7N1N'OOC //MMJJ+- !6!68N8N OO{{799N}}))E JJeF$455* E __)"G,!(!* %v$ $

 #' %  ))C**tyyuV=M7N1N'OOC__*"G,!(!( #" $

 #' %  t||33888 4 4 9 99$-->"CL$+MM%1	    FIr$   )r   r   r   r   r   )TNNr         ?)r   boolr   float | Noner   ru   r   intr    r\   returnNoner   )rh   zpaddle.Tensorri   zpaddle.pir.Block | Nonerw   zpaddle.Tensor | None)	__name__
__module____qualname____firstlineno____doc__r   rq   __static_attributes____classcell__r!   s   @r"   r   r   "   s    "L # $  	
   
 $ DHL L)@L	L Lr$   r   c                  N   ^  \ rS rSrSr    S         SU 4S jjjrSrU =r$ )XavierNormali'  ad  
This class implements the Xavier weight initializer from the paper
`Understanding the difficulty of training deep feedforward neural
networks <http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf>`_
by Xavier Glorot and Yoshua Bengio, using a normal distribution whose mean is :math:`0` and standard deviation is

.. math::

    gain \times \sqrt{\frac{2.0}{fan\_in + fan\_out}}.


Args:
    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.
    gain (float, optional): Scaling Tensor. Default is 1.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 Xavier weight, using a normal distribution.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> paddle.seed(1)
        >>> data = paddle.ones(shape=[3, 1, 2], dtype='float32')
        >>> weight_attr = paddle.framework.ParamAttr(
        ...     name="linear_weight",
        ...     initializer=paddle.nn.initializer.XavierNormal())
        >>> bias_attr = paddle.framework.ParamAttr(
        ...     name="linear_bias",
        ...     initializer=paddle.nn.initializer.XavierNormal())
        >>> 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,
        [[-0.21607460,  0.08382989],
         [ 0.29147008, -0.07049121]])

        >>> print(linear.bias)
        Parameter containing:
        Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False,
        [1.06076419, 0.87684733])

        >>> res = linear(data)
        >>> print(res)
        Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
        [[[1.13615966, 0.89018601]],
         [[1.13615966, 0.89018601]],
         [[1.13615966, 0.89018601]]])
c                &   > [         TU ]  SXSUS9  g )NFr   r   r   r   r   r    r   r   r   r   r   r    r.   r!   s        r"   r   XavierNormal.__init__^  s      	& 	 	
r$    NNrs   N
r   ru   r   ru   r    r\   r.   z
str | Nonerw   rx   ry   rz   r{   r|   r}   r   r~   r   r   s   @r"   r   r   '  sQ    4p  $ $	
	
 	
 		

 	
 
	
 	
r$   r   c                  N   ^  \ rS rSrSr    S         SU 4S jjjrSrU =r$ )XavierUniformij  a  
This class implements the Xavier weight initializer from the paper
`Understanding the difficulty of training deep feedforward neural
networks <http://proceedings.mlr.press/v9/glorot10a/glorot10a.pdf>`_
by Xavier Glorot and Yoshua Bengio.

This initializer is designed to keep the scale of the gradients
approximately same in all the layers. In case of Uniform distribution,
the range is :math:`[-x,x]`, where

.. math::

    x = gain \times \sqrt{\frac{6.0}{fan\_in + fan\_out}}.

Args:
    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.
    gain (float, optional): Scaling Tensor. Default is 1.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 Xavier weight, using a uniform distribution.

Examples:
    .. code-block:: python

        >>> import paddle
        >>> paddle.seed(1)
        >>> data = paddle.ones(shape=[3, 1, 2], dtype='float32')
        >>> weight_attr = paddle.framework.ParamAttr(
        ...     name="linear_weight",
        ...     initializer=paddle.nn.initializer.XavierUniform())
        >>> bias_attr = paddle.framework.ParamAttr(
        ...     name="linear_bias",
        ...     initializer=paddle.nn.initializer.XavierUniform())
        >>> 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.18095720,  0.64892638],
         [ 0.43125069, -1.11156428]])
        >>> print(linear.bias)
        Parameter containing:
        Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=False,
        [-0.27524316,  1.13808715])

        >>> res = linear(data)
        >>> print(res)
        Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
        [[[-1.02494967,  0.67544925]],
         [[-1.02494967,  0.67544925]],
         [[-1.02494967,  0.67544925]]])
c                &   > [         TU ]  SXSUS9  g )NTr   r   r   r   s        r"   r   XavierUniform.__init__  s      	qt 	 	
r$   r   r   r   r   r   s   @r"   r   r   j  sQ    6t  $ $	
	
 	
 		

 	
 
	
 	
r$   r   )
__future__r   rZ   rD   r   baser   r   r   base.data_feederr	   base.frameworkr
   r   r   initializerr   __all__r   r   r   r   r$   r"   <module>r      s^    #    0 0 8 
 %
B BJ@
$ @
FB
% B
r$   