o
    + i38                     @  s   d dl mZ d dlZd dlm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 er>dd
lmZ g ZG dd deZG dd deZG dd deZdS )    )annotationsN)TYPE_CHECKING)_C_ops   )core	frameworkunique_name)_current_expected_placein_dygraph_modein_pir_mode   )Initializercalculate_gain)_NonLinearityc                      s>   e Zd ZdZ						dd fddZ	dd ddZ  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 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.

    TNr   relufan_inuniformboolfloat | Noneseedintnegative_slopefloatnonlinearityr   modestrreturnNonec                   s   |dusJ |dusJ t    || _|| _|| _|| _|| _|| _| jdvr1td| j d| jdkr?| jdurAtddS dS )zConstructor for MSRAInitializerN)r   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)selfr   r   r   r   r   r   	__class__ i/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/nn/initializer/kaiming.pyr"   H   s(   


zMSRAInitializer.__init__varpaddle.Tensorblockpaddle.pir.Block | Nonepaddle.Tensor | Nonec              
   C  s  t |tjr| rJ d| |}t |tjtjjtjj	j
fs#J t |tjtjjfs/J | |\}}| jdkrE| jdu rB|n| j}| jdkrL|}| jdkrV|jj| _|j}|t	jjjksj|t	jjjkr| jst	jjj}|jtdd|jdg|j|t	jjjd	d
}n|t	j j!t	j j"fv r| jst	j j#}|}n|}|}t$ r| jrt%| j&| j'}	|	t()dt*|  }
t+,|j||
 |
| jt- }nt%| j&| j'}	|	t()t*| }t- }t+.|jd|| j||}|t	jjjks|t	jjjt	j j!t	j j"fv r| jst+/||}|0| dS |0| dS t1 ru| jr?t%| j&| j'}	|	t()dt*|  }
t+,|j||
 |
| jt- }nt%| j&| j'}	|	t()t*| }t- }t+.|jd|| j||}|t	j j!t	j j"fv rs| jsst+/||S |S | jrt%| j&| j'}	|	t()dt*|  }
|j2di d|i|jt3||
 |
| jddd}n%t%| j&| j'}	|	t()t*| }|j2dd|i|jt3|d|| jddd}|t	jjjks|t	jjjkr| js|j2dd|id|i|j|dd ||_4|S )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    Z
masra_inittmpF)nameshapedtypetypeZpersistableg      @        Zuniform_randomZOut)r6   r7   minmaxr   T)r8   inputsoutputsattrsstop_gradientZgaussian_random)r6   r7   meanstdr   )r8   r=   r>   r?   castX)Zin_dtype	out_dtype)r8   r<   r=   r>   )5
isinstancer   ZEagerParamBaseZis_distZ_check_blockVariablepaddleZpirValuer   ZParameterMetaZBlockZ_compute_fansr(   r$   r%   programZrandom_seedr7   ZVarDescZVarTypeZFP16ZBF16r#   ZFP32Z
create_varr   generatejoinr5   r6   ZDENSE_TENSORZDataTypeZFLOAT16ZBFLOAT16ZFLOAT32r
   r   r'   r&   mathsqrtr   r   r   r	   ZgaussianrB   Z_share_underline_tensor_tor   Z	append_opr   op)r*   r/   r1   Zf_inZf_outr   Zorigin_dtyperD   Zout_varZgainlimitrA   ZplaceZvar_tmprN   r-   r-   r.   forwardf   s  







	

	
zMSRAInitializer.forward)TNr   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )N)r/   r0   r1   r2   r   r3   )__name__
__module____qualname____doc__r"   rP   __classcell__r-   r-   r+   r.   r   &   s    #r   c                      ,   e Zd ZdZ				dd fddZ  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 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)

    Nr9   r   r   r   r   r   r   r   r   r   r   c                      t  jd|d|||d d S )NFr   r   r   r   r   r   r   r!   r"   r*   r   r   r   r   r+   r-   r.   r"   <     
zKaimingNormal.__init__Nr9   r   r   
r   r   r   r   r   r   r   r   r   r   rQ   rR   rS   rT   r"   rU   r-   r-   r+   r.   rW     s    'rW   c                      rV   )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 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)

    Nr9   r   r   r   r   r   r   r   r   r   r   c                   rX   )NTr   rY   rZ   r[   r+   r-   r.   r"   r  r\   zKaimingUniform.__init__r]   r^   r_   r-   r-   r+   r.   r`   M  s    &r`   )
__future__r   rL   typingr   rG   r   baser   r   r   Zbase.frameworkr	   r
   r   Zinitializerr   r   r   __all__r   rW   r`   r-   r-   r-   r.   <module>   s    q7