
    ёi[9                        S SK Jr  S SKrS SKJr  S SKrS SKJr  SSKJrJ	r	J
r
  SSKJrJrJr  SS	KJrJr  \(       a  SS
KJr  / r " S S\5      r " S S\5      r " S S\5      rg)    )annotationsN)TYPE_CHECKING)_C_ops   )core	frameworkunique_name)_current_expected_placein_dygraph_modein_pir_mode   )Initializercalculate_gain)_NonLinearityc                  t   ^  \ 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$ )
MSRAInitializer&   a  Implements the MSRA initializer a.k.a. Kaiming Initializer

This class implements the weight initialization from the paper
`Delving Deep into Rectifiers: Surpassing Human-Level Performance on
ImageNet Classification <https://arxiv.org/abs/1502.01852>`_
by Kaiming He, Xiangyu Zhang, Shaoqing Ren and Jian Sun. This is a
robust initialization method that particularly considers the rectifier
nonlinearities. In case of Uniform distribution, the range is [-x, x], where

.. math::

    x = gain \times \sqrt{\frac{3}{fan\_in}}

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

.. math::

    \frac{gain}{\sqrt{{fan\_in}}}

Args:
    uniform (bool, optional): whether to use uniform or normal distribution. Default is True.
    fan_in (float32|None, optional): fan_in (in_features) of trainable Tensor, If None, it will be inferred automatically. If you don't want to use in_features of the Tensor, you can set the value of 'fan_in' smartly by yourself. Default is None.
    seed (int32, optional): random seed. Default is 0.
    negative_slope (float, optional): negative_slope (only used with leaky_relu). Default is 0.0.
    nonlinearity(str, optional): the non-linear function. Default is relu.
    mode(str, optional): the mode of initialization, can be 'fan_in' or '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'.

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

c                "  > Uc   eUc   e[         TU ]  5         Xl        X l        X0l        X@l        XPl        X`l        U R                  S;  a  [        SU R                   S35      eU R                  S:X  a  U R                  b  [        S5      egg)zConstructor for MSRAInitializerN)fan_infan_outzWThe mode of KaimingNormal/KaimingUniform should be 'fan_in' or 'fan_out', but received .r   zdThe mode of KaimingNormal/KaimingUniform is 'fan_out', but fan_in is set. Please set fan_in to None.)	super__init___uniform_fan_in_seed_negative_slope_nonlinearity_mode
ValueError)selfuniformr   seednegative_slopenonlinearitymode	__class__s          ]/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/nn/initializer/kaiming.pyr   MSRAInitializer.__init__H   s     """
-)
::22  $

|1.  ::"t||'?@  (@"    c                ,   [        U[        R                  5      (       a  UR                  5       (       a   S5       eU R	                  U5      n[        U[        R
                  [        R                  R                  [        R                  R                  R                  45      (       d   e[        U[        R                  [        R                  R                  45      (       d   eU R                  U5      u  p4U R                  S:X  a  U R                  c  UOU R                  nU R                  S:X  a  UnU R                  S:X  a  UR                   R"                  U l        UR$                  nU[        R&                  R(                  R*                  :X  d9  U[        R&                  R(                  R,                  :X  a  U R.                  (       d  [        R&                  R(                  R0                  nUR3                  [4        R6                  " SR9                  SUR:                  S/5      5      UR<                  U[        R&                  R(                  R>                  S	S
9nOjU[        R@                  RB                  [        R@                  RD                  4;   a.  U R.                  (       d  [        R@                  RF                  nUnOUnUn[I        5       (       Ga,  U R.                  (       a  [K        U RL                  U RN                  5      n	U	[P        RR                  " S[U        W5      -  5      -  n
[V        RX                  " UR<                  UU
* U
U R                  URZ                  R]                  5       (       a  URZ                  O	[_        5       5      nO[K        U RL                  U RN                  5      n	U	[P        RR                  " [U        W5      5      -  nURZ                  R]                  5       (       a  URZ                  O	[_        5       n[V        R`                  " UR<                  SXR                  X|5      nU[        R&                  R(                  R*                  :X  dl  U[        R&                  R(                  R,                  [        R@                  RB                  [        R@                  RD                  4;   a9  U R.                  (       d(  [V        Rb                  " X5      nURe                  U5        gURe                  U5        g[g        5       (       Gai  U R.                  (       a~  [K        U RL                  U RN                  5      n	U	[P        RR                  " S[U        W5      -  5      -  n
[V        RX                  " UR<                  UU
* U
U R                  [_        5       5      nOy[K        U RL                  U RN                  5      n	U	[P        RR                  " [U        W5      5      -  n[_        5       n[V        R`                  " UR<                  SXR                  X|5      nU[        R@                  RB                  [        R@                  RD                  4;   a'  U R.                  (       d  [V        Rb                  " X5      $ U$ U R.                  (       a  [K        U RL                  U RN                  5      n	U	[P        RR                  " S[U        W5      -  5      -  n
URi                  S0 SU0UR<                  [k        U5      U
* U
U R                  S.SS9nOy[K        U RL                  U RN                  5      n	U	[P        RR                  " [U        W5      5      -  nURi                  SSU0UR<                  [k        U5      SUU R                  S.SS9nU[        R&                  R(                  R*                  :X  d9  U[        R&                  R(                  R,                  :X  a4  U R.                  (       d#  URi                  SSU0SU0UR$                  US.S9  Xl6        U$ )a$  Initialize the input tensor with MSRA 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.
zDCurrently, kaiming initializer not support lazy init for dist param.r   Nr   r   r   
masra_inittmpF)nameshapedtypetypepersistableg      @        uniform_randomOut)r/   r0   minmaxr#   T)r1   inputsoutputsattrsstop_gradientgaussian_random)r/   r0   meanstdr#   )r1   r9   r:   r;   castX)in_dtype	out_dtype)r1   r8   r9   r:   )7
isinstancer   EagerParamBaseis_dist_check_blockVariablepaddlepirValuer   ParameterMetaBlock_compute_fansr   r   r   programrandom_seedr0   VarDescVarTypeFP16BF16r   FP32
create_varr	   generatejoinr.   r/   DENSE_TENSORDataTypeFLOAT16BFLOAT16FLOAT32r   r   r   r   mathsqrtfloatr   r"   place_typer
   gaussianr?   _share_underline_tensor_tor   	append_opintop)r!   varblockf_inf_outr   origin_dtyperB   out_vargainlimitr>   r`   var_tmprf   s                  r(   forwardMSRAInitializer.forwardf   s    sI4455#++--	
 S	
 

 !!%(""

  

--
 
 	
 
 %)//6::3C3C!DEEEE((- ::!!\\1Tt||F::"F::?22DJ yy4<<//444DLL00555dmm,,11I&& ))HHlCHHe<= ii\\))66! ' G T]]22DMM4J4JKKMM--IG$IG}}%d&8&8$:N:NOtyyuV})<== ..IIFJJyy(( II02	 &d&8&8$:N:NOTYYuV}55 yy(( II02 
 !//MM3ZZ t||33888LL((--MM))MM**  ++g<2237  2237]]}}%d&8&8$:N:NOtyyuV})<== ..IIFJJ+- &d&8&8$:N:NOTYYuV}55/1 //MM3ZZ
 !6!68N8N OO{{799N}}%d&8&8$:N:NOtyyuV})<==__)"G,!(!$Y %v$ $

 #' %  &d&8&8$:N:NOTYYuV}55__*"G,!(!$Y #" $

 #' %  t||33888 4 4 9 99$-->"CL$+MM%1	    FIr*   )r   r   r   r   r   r   )TNr   r   relur   )r"   boolr   float | Noner#   re   r$   r_   r%   r   r&   strreturnNone)N)rg   zpaddle.Tensorrh   zpaddle.pir.Block | Nonerv   zpaddle.Tensor | None)	__name__
__module____qualname____firstlineno____doc__r   rp   __static_attributes____classcell__r'   s   @r(   r   r   &   s    F # !&,  	
  $  
 > DHt t)@t	t tr*   r   c                  N   ^  \ rS rSrSr    S         SU 4S jjjrSrU =r$ )KaimingNormali  a  Implements the Kaiming Normal initializer

This class implements the weight initialization from the paper
`Delving Deep into Rectifiers: Surpassing Human-Level Performance on
ImageNet Classification <https://arxiv.org/abs/1502.01852>`_
by Kaiming He, Xiangyu Zhang, Shaoqing Ren and Jian Sun. This is a
robust initialization method that particularly considers the rectifier
nonlinearities.

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

.. math::

    \frac{gain}{\sqrt{{fan\_in}}}

Args:
    fan_in (float32|None, optional): fan_in (in_features) of trainable Tensor, If None, it will be inferred automatically. If you don't want to use in_features of the Tensor, you can set the value of 'fan_in' smartly by yourself. Default is None.
    negative_slope (float, optional): negative_slope (only used with leaky_relu). Default is 0.0.
    nonlinearity(str, optional): the non-linear function. Default is relu.
    mode(str, optional): the mode of initialization, can be 'fan_in' or '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'.

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

Examples:
    .. code-block:: python

        >>> import paddle
        >>> import paddle.nn as nn

        >>> linear = nn.Linear(2, 4, weight_attr=nn.initializer.KaimingNormal())
        >>> data = paddle.rand([30, 10, 2], dtype='float32')
        >>> res = linear(data)

c           	     *   > [         TU ]  SUSUUUS9  g )NFr   r"   r   r#   r$   r%   r&   r   r   r!   r   r$   r%   r&   r'   s        r(   r   KaimingNormal.__init__C  s)     	)% 	 	
r*    Nr3   rr   r   
r   rt   r$   r_   r%   ru   r&   ru   rv   rw   rx   ry   rz   r{   r|   r   r}   r~   r   s   @r(   r   r     sQ    #N  $ #"

 
 	

 
 

 
r*   r   c                  N   ^  \ rS rSrSr    S         SU 4S jjjrSrU =r$ )KaimingUniformiT  a  Implements the Kaiming Uniform initializer

This class implements the weight initialization from the paper
`Delving Deep into Rectifiers: Surpassing Human-Level Performance on
ImageNet Classification <https://arxiv.org/abs/1502.01852>`_
by Kaiming He, Xiangyu Zhang, Shaoqing Ren and Jian Sun. This is a
robust initialization method that particularly considers the rectifier
nonlinearities.

In case of Uniform distribution, the range is [-x, x], where

.. math::

    x = gain \times \sqrt{\frac{3}{fan\_in}}

Args:
    fan_in (float32|None, optional): fan_in (in_features) of trainable Tensor, If None, it will be inferred automatically. If you don't want to use in_features of the Tensor, you can set the value of 'fan_in' smartly by yourself. Default is None.
    negative_slope (float, optional): negative_slope (only used with leaky_relu). Default is 0.0.
    nonlinearity(str, optional): the non-linear function. Default is relu.
    mode(str, optional): the mode of initialization, can be 'fan_in' or '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'.

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

Examples:
    .. code-block:: python

        >>> import paddle
        >>> import paddle.nn as nn

        >>> linear = nn.Linear(2, 4, weight_attr=nn.initializer.KaimingUniform())
        >>> data = paddle.rand([30, 10, 2], dtype='float32')
        >>> res = linear(data)

c           	     *   > [         TU ]  SUSUUUS9  g )NTr   r   r   r   s        r(   r   KaimingUniform.__init__y  s)     	)% 	 	
r*   r   r   r   r   r   s   @r(   r   r   T  sQ    "L  $ #"

 
 	

 
 

 
r*   r   )
__future__r   r]   typingr   rH   r   baser   r   r	   base.frameworkr
   r   r   initializerr   r   r   __all__r   r   r   r   r*   r(   <module>r      s`    #      0 0 
 5*
tk tn4
O 4
n3
_ 3
r*   