o
    #j                     @   sx   d dl mZmZ ddlmZmZmZ ddlmZ ddl	m
Z
mZmZ ddlmZ g ZG dd	 d	eZG d
d deZdS )    )_C_opspir   )core	frameworkunique_name)check_variable_and_dtype)_current_expected_placein_dygraph_modein_pir_mode   )Initializerc                       s.   e Zd ZdZ	d
 fdd	Zddd	Z  ZS )UniformInitializeraJ  Implements the random uniform distribution initializer

    Args:
        low (float, optional): Lower boundary of the uniform distribution. Default is :math:`-1.0`.
        high (float, optional): Upper boundary of the uniform distribution. Default is :math:`1.0`.
        seed (int, optional): Random seed. Default is 0.
        diag_num (int, optional): the number of diagonal elements to initialize.
            If set to 0, diagonal initialization will be not performed. Default is 0.
        diag_step (int, optional): Step size between two diagonal elements,
            which is generally the width of the square matrix. Default is 0.
        diag_val (float, optional): the value of the diagonal element to be initialized,
            default 1.0. It takes effect only if the diag_num is greater than 0. Default is :math:`1.0`.

                ?r   c                    s   |d usJ |d usJ ||ksJ |d usJ |d usJ |d us$J |d us*J |dks2|dkr<|dkr:|dks<J t    || _|| _|| _|| _|| _|| _d S )Nr   )super__init___low_high_seed	_diag_num
_diag_step	_diag_val)selflowhighseeddiag_num	diag_stepdiag_val	__class__ ^/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/nn/initializer/uniform.pyr   -   s    

zUniformInitializer.__init__Nc                 C   s  |  |}t|tjtjfsJ t st|dg dd | jdkr&|jj	| _|j
tjjjkrLtjjj}|jtdd|jdg|j|tjjjdd}n|j
}|}t rt|j|| j| j| jt }|j
tjjjkryt||j
}|| d	S || d	S t r|j
tjj krtjj!}n|j
}t|j|| j| j| jt }|j
tjj kr|j
tjj krt||j
S |S |j"di d|i|j|| j| j| j| j#| j$| j%d
dd}|j
tjjjkr|j"dd|id|i|j
|j
dd ||_&|S )aW  Initialize the input tensor with Uniform distribution.

        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
        ZOut)Zuint16Zfloat16Zfloat32Zfloat64Zuniform_randomr   .tmpF)nameshapedtypetypeZpersistableN)r'   r(   minmaxr   r   r   r   T)r)   inputsoutputsattrsZstop_gradientcastX)Zin_dtype	out_dtype)r)   r,   r-   r.   )'Z_check_block
isinstancer   ZBlockr   r
   r   r   programZrandom_seedr(   r   ZVarDescZVarTypeZFP16ZFP32Z
create_varr   generatejoinr&   r'   Z
LOD_TENSORr   uniformr   r   r	   r/   Z_share_underline_tensor_tor   ZDataTypeZFLOAT16ZFLOAT32Z	append_opr   r   r   op)r   varblockr1   Zout_varZvar_tmpr7   r"   r"   r#   forwardA   s   







	
zUniformInitializer.forward)r   r   r   r   r   r   )N)__name__
__module____qualname____doc__r   r:   __classcell__r"   r"   r    r#   r      s
    r   c                       s"   e Zd ZdZd fdd	Z  ZS )Uniforma  The uniform distribution initializer.

    Args:
        low (float, optional): Lower boundary of the uniform distribution. Default is :math:`-1.0`.
        high (float, optional): Upper boundary of the uniform distribution. Default is :math:`1.0`.
        name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.

    Returns:
        A parameter initialized by 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.Uniform(low=-0.5, high=0.5))
            >>> bias_attr = paddle.framework.ParamAttr(
            ...     name="linear_bias",
            ...     initializer=paddle.nn.initializer.Uniform(low=-0.5, high=0.5))
            >>> 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.48212373,  0.26492310],
             [ 0.17605734, -0.45379421]])

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

            >>> res = linear(data)
            >>> print(res)
            Tensor(shape=[3, 1, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
            [[[-0.41843393,  0.27575102]],
             [[-0.41843393,  0.27575102]],
             [[-0.41843393,  0.27575102]]])
    r   r   Nc                    sL   |d usJ d|d usJ d||ksJ dt  j||ddddd d S )Nzlow should not be Nonezhigh should not be Nonez%high should greater or equal than lowr   r   )r   r   r   r   r   r   )r   r   )r   r   r   r&   r    r"   r#   r      s   
zUniform.__init__)r   r   N)r;   r<   r=   r>   r   r?   r"   r"   r    r#   r@      s    *r@   N)Zpaddler   r   baser   r   r   Zbase.data_feederr   Zbase.frameworkr	   r
   r   Zinitializerr   __all__r   r@   r"   r"   r"   r#   <module>   s    