Ë
    •\;j‚/  ã                   óž   — d dl Z d dlZd dlmZ ddlmZmZmZ ddlmZm	Z	m
Z
 ddlmZmZ g Z G d„ d	e«      Z G d
„ de«      Z G d„ de«      Zy)é    N)Ú_C_opsé   )ÚcoreÚ	frameworkÚunique_name)Ú_current_expected_placeÚin_dygraph_modeÚin_pir_modeé   )ÚInitializerÚcalculate_gainc                   ó6   ‡ — e Zd ZdZ	 	 	 	 	 dˆ fd„	Zdd„Zˆ xZS )ÚMSRAInitializera]  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 infered automaticly. 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.

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

    c                 óx   •— |€J ‚|€J ‚t         ‰| �  «        || _        || _        || _        || _        || _        y)zConstructor for MSRAInitializerN)ÚsuperÚ__init__Ú_uniformÚ_fan_inÚ_seedÚ_negative_slopeÚ_nonlinearity)ÚselfÚuniformÚfan_inÚseedÚnegative_slopeÚnonlinearityÚ	__class__s         €úfG:\00. PROJECTS\API\Inventory\templateJSON\kerjaOCR\Lib\site-packages\paddle/nn/initializer/kaiming.pyr   zMSRAInitializer.__init__A   sM   ø€ ð Ð"Ð"Ð"ØÐÐÐÜ‰ÑÔØˆŒØˆŒØˆŒ
Ø-ˆÔØ)ˆÕó    c                 ó„  — | j                  |«      }t        |t        j                  t        j
                  j                  j                  f«      sJ ‚t        |t        j                  t        j
                  j                  f«      sJ ‚| j                  |«      \  }}| j                  €|n| j                  }| j                  dk(  r|j                  j                  | _        |j                  t        j                  j                   j"                  k(  s=|j                  t        j                  j                   j$                  k(  r¢| j&                  s–t        j                  j                   j(                  }|j+                  t-        j.                  dj1                  d|j2                  dg«      «      |j4                  |t        j                  j                   j6                  d¬«      }nw|j                  t        j8                  j:                  t        j8                  j<                  fv r)| j&                  st        j8                  j>                  }|}n|j                  }|}tA        «       �r³| j&                  r|tC        | jD                  | jF                  «      }|tI        jJ                  dtM        |«      z  «      z  }	tO        jP                  |j4                  ||	 |	| j                  tS        «       «      }nytC        | jD                  | jF                  «      }|tI        jJ                  tM        |«      «      z  }
tS        «       }tO        jT                  |j4                  d	|
| j                  ||«      }|j                  t        j                  j                   j"                  k(  s=|j                  t        j                  j                   j$                  k(  r>| j&                  s2tO        jV                  ||j                  «      }|jY                  |«       y|jY                  |«       yt[        «       �ro| j&                  r|tC        | jD                  | jF                  «      }|tI        jJ                  dtM        |«      z  «      z  }	tO        jP                  |j4                  ||	 |	| j                  tS        «       «      }nytC        | jD                  | jF                  «      }|tI        jJ                  tM        |«      «      z  }
tS        «       }tO        jT                  |j4                  d	|
| j                  ||«      }|j                  t        j8                  j:                  t        j8                  j<                  fv r,| j&                  s tO        jV                  ||j                  «      S |S | j&                  r�tC        | jD                  | jF                  «      }|tI        jJ                  dtM        |«      z  «      z  }	|j]                  d
i d|i|j4                  t_        |«      |	 |	| j                  dœd¬«      }n{tC        | jD                  | jF                  «      }|tI        jJ                  tM        |«      «      z  }
|j]                  dd|i|j4                  t_        |«      d	|
| j                  dœd¬«      }|j                  t        j                  j                   j"                  k(  s=|j                  t        j                  j                   j$                  k(  r<| j&                  s0|j]                  dd|id|i|j                  |j                  dœ¬«       ||_0        |S )aW  Initialize the input tensor with MSRA initialization.

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

        Returns:
            The initialization op.
        Nr   Ú.Ú
masra_initÚtmpF)ÚnameÚshapeÚdtypeÚtypeÚpersistableg      @ç        Úuniform_randomÚOut)r&   r'   ÚminÚmaxr   T)r(   ÚinputsÚoutputsÚattrsÚstop_gradientÚgaussian_random)r&   r'   ÚmeanÚstdr   )r(   r0   r1   r2   ÚcastÚX)Úin_dtypeÚ	out_dtype)r(   r/   r0   r1   )1Ú_check_blockÚ
isinstancer   ÚVariableÚpaddleÚpirr   ÚParameterMetaÚBlockÚ_compute_fansr   r   ÚprogramÚrandom_seedr'   ÚVarDescÚVarTypeÚFP16ÚBF16r   ÚFP32Ú
create_varr   ÚgenerateÚjoinr%   r&   Ú
LOD_TENSORÚDataTypeÚFLOAT16ÚBFLOAT16ÚFLOAT32r	   r   r   r   ÚmathÚsqrtÚfloatr   r   r   Úgaussianr6   Ú_share_underline_tensor_tor
   Ú	append_opÚintÚop)r   ÚvarÚblockÚf_inÚf_outr   r9   Úout_varÚgainÚlimitr5   ÚplaceÚvar_tmprX   s                 r   ÚforwardzMSRAInitializer.forwardS   s|  € ð ×!Ñ! %Ó(ˆÜØ”)×$Ñ$¤f§j¡j§o¡o×&CÑ&CÐDô
ð 	
ð 
ô ˜%¤)§/¡/´6·:±:×3CÑ3CÐ!DÔEÐEÐEØ×(Ñ(¨Ó-‰ˆˆeð Ÿ™Ð-‘°4·<±<ˆà�:‰:˜Š?ØŸ™×2Ñ2ˆDŒJð �9‰9œŸ™×,Ñ,×1Ñ1Ò1Ø�I‰IœŸ™×-Ñ-×2Ñ2Ò2¸4¿=º=äŸ™×,Ñ,×1Ñ1ˆIØ×&Ñ&Ü ×)Ñ)Ø—H‘H˜l¨C¯H©H°eÐ<Ó=óð —i‘iØÜ—\‘\×)Ñ)×4Ñ4Ø!ð 'ó ‰Gð �I‰Iœ$Ÿ-™-×/Ñ/´·±×1GÑ1GÐHÑHØ—M’MäŸ™×-Ñ-ˆIØ‰GàŸ	™	ˆIØˆGäÕØ�}Š}Ü% d×&8Ñ&8¸$×:NÑ:NÓO�ØœtŸy™y¨¬u°V«}Ñ)<Ó=Ñ=�Ü Ÿ.™.Ø—I‘IØØ�FØØ—J‘JÜ+Ó-ó‘ô & d×&8Ñ&8¸$×:NÑ:NÓO�ØœTŸY™Y¤u¨V£}Ó5Ñ5�Ü/Ó1�Ü Ÿ/™/Ø—M‘M 3¨¨T¯Z©Z¸ÀEó�ð �y‰yœDŸL™L×0Ñ0×5Ñ5Ò5Ø—	‘	œTŸ\™\×1Ñ1×6Ñ6Ò6¸t¿}º}ä Ÿ+™+ g¨s¯y©yÓ9�Ø×2Ñ2°3Ô7ð ð ×2Ñ2°3Ô7ØÜ�]Ø�}Š}Ü% d×&8Ñ&8¸$×:NÑ:NÓO�ØœtŸy™y¨¬u°V«}Ñ)<Ó=Ñ=�Ü Ÿ.™.Ø—I‘IØØ�FØØ—J‘JÜ+Ó-ó‘ô & d×&8Ñ&8¸$×:NÑ:NÓO�ØœTŸY™Y¤u¨V£}Ó5Ñ5�Ü/Ó1�Ü Ÿ/™/Ø—M‘M 3¨¨T¯Z©Z¸ÀEó�ð
 —	‘	œdŸm™m×3Ñ3´T·]±]×5KÑ5KÐLÑLØŸšä—{‘{ 7¨C¯I©IÓ6Ð6àˆNà�}Š}Ü% d×&8Ñ&8¸$×:NÑ:NÓO�ØœtŸy™y¨¬u°V«}Ñ)<Ó=Ñ=�Ø—_‘_Ø)ØØ" GÐ,à!(§¡Ü!$ Y£Ø %˜vØ$Ø $§
¡
ñð #'ð %ó ‘ô & d×&8Ñ&8¸$×:NÑ:NÓO�ØœTŸY™Y¤u¨V£}Ó5Ñ5�Ø—_‘_Ø*Ø" GÐ,à!(§¡Ü!$ Y£Ø #Ø"Ø $§
¡
ñð #'ð %ó �ð �y‰yœDŸL™L×0Ñ0×5Ñ5Ò5Ø—	‘	œTŸ\™\×1Ñ1×6Ñ6Ò6¸t¿}º}à—‘ØØ ˜>Ø" C˜LØ'.§}¡}À3Ç9Á9ÑMð	  ô ð ˆCŒFØˆIr    )TNr   r   Úrelu)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   rb   Ú__classcell__©r   s   @r   r   r       s%   ø„ ñðD ØØØØõ*÷$Tr    r   c                   ó$   ‡ — e Zd ZdZdˆ fd„	Zˆ xZS )ÚKaimingNormala_  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 infered automaticly. 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.

    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         ‰| �  d|d||¬«       y )NFr   ©r   r   r   r   r   ©r   r   ©r   r   r   r   r   s       €r   r   zKaimingNormal.__init__  s$   ø€ Ü‰ÑØØØØ)Ø%ð 	õ 	
r    ©Nr*   rc   ©rd   re   rf   rg   r   rh   ri   s   @r   rk   rk   ê   s   ø„ ñ"÷H
ñ 
r    rk   c                   ó$   ‡ — e Zd ZdZdˆ fd„	Zˆ xZS )ÚKaimingUniformaY  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 infered automaticly. 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.

    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         ‰| �  d|d||¬«       y )NTr   rm   rn   ro   s       €r   r   zKaimingUniform.__init__=  s$   ø€ Ü‰ÑØØØØ)Ø%ð 	õ 	
r    rp   rq   ri   s   @r   rs   rs     s   ø„ ñ!÷F
ñ 
r    rs   )rQ   r=   r   Úbaser   r   r   Úbase.frameworkr   r	   r
   Úinitializerr   r   Ú__all__r   rk   rs   © r    r   Ú<module>rz      sR   ðó  ã Ý ç 0Ñ 0÷ñ ÷
 5à
€ôG�kô GôT,
�Oô ,
ô^+
�_õ +
r    