o
    #j>.                     @   s   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	 d	eZG d
d deZG dd deZdS )    N)_C_ops   )core	frameworkunique_name)_current_expected_placein_dygraph_modein_pir_mode   )Initializercalculate_gainc                       s6   e Zd ZdZ					d
 fdd	Z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 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.

    TNr   reluc                    sD   |dusJ |dusJ t    || _|| _|| _|| _|| _dS )zConstructor for MSRAInitializerN)super__init___uniform_fan_in_seed_negative_slope_nonlinearity)selfuniformfan_inseednegative_slopenonlinearity	__class__ ^/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/nn/initializer/kaiming.pyr   A   s   	

zMSRAInitializer.__init__c              
   C   s  |  |}t|tjtjjjfsJ t|tjtjjfsJ | 	|\}}| j
du r,|n| j
}| jdkr9|jj| _|jtjjjksL|jtjjjkrj| jsjtjjj}|jtdd|jdg|j|tjjjdd}n|jtjjtjjfv r| jstjj}|}n|j}|}t  r| jrt!| j"| j#}|t$%dt&|  }	t'(|j||	 |	| jt) }nt!| j"| j#}|t$%t&| }
t) }t'*|jd	|
| j||}|jtjjjks|jtjjjkr| jst'+||j}|,| dS |,| dS t- rS| jrt!| j"| j#}|t$%dt&|  }	t'(|j||	 |	| jt) }nt!| j"| j#}|t$%t&| }
t) }t'*|jd	|
| j||}|jtjjtjjfv rQ| jsQt'+||jS |S | jrt!| j"| j#}|t$%dt&|  }	|j.d
i d|i|jt/||	 |	| jddd}n%t!| j"| j#}|t$%t&| }
|j.dd|i|jt/|d	|
| jddd}|jtjjjks|jtjjjkr| js|j.dd|id|i|j|jd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   .Z
masra_inittmpF)nameshapedtypetypeZpersistableg      @        Zuniform_randomZOut)r#   r$   minmaxr   T)r%   inputsoutputsattrsstop_gradientZgaussian_random)r#   r$   meanstdr   )r%   r*   r+   r,   castX)Zin_dtype	out_dtype)r%   r)   r*   r+   )1Z_check_block
isinstancer   VariablepaddleZpirr   ZParameterMetaZBlockZ_compute_fansr   r   programZrandom_seedr$   ZVarDescZVarTypeZFP16ZBF16r   ZFP32Z
create_varr   generatejoinr"   r#   Z
LOD_TENSORZDataTypeZFLOAT16ZBFLOAT16ZFLOAT32r   r   r   r   mathsqrtfloatr   r   r   Zgaussianr/   Z_share_underline_tensor_tor	   Z	append_opintop)r   varblockZf_inZf_outr   r1   Zout_varZgainlimitr.   ZplaceZvar_tmpr<   r   r   r   forwardS   s   




	

	zMSRAInitializer.forward)TNr   r   r   )N)__name__
__module____qualname____doc__r   r@   __classcell__r   r   r   r   r       s    "r   c                       "   e Zd ZdZ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 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)

    Nr&   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        
zKaimingNormal.__init__Nr&   r   rA   rB   rC   rD   r   rE   r   r   r   r   rG      s    $rG   c                       rF   )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)

    Nr&   r   c                    rH   )NTr   rI   rJ   rK   r   r   r   r   =  rL   zKaimingUniform.__init__rM   rN   r   r   r   r   rO     s    #rO   )r8   r4   r   baser   r   r   Zbase.frameworkr   r   r	   Zinitializerr   r   __all__r   rG   rO   r   r   r   r   <module>   s    K/