o
    + i!                     @  s   d dl mZ d dlZ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 )    )annotationsN)_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d fddZ	d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   lowfloathighseedintdiag_num	diag_stepdiag_valreturnNonec                   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)selfr   r   r   r   r   r   	__class__ i/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/nn/initializer/uniform.pyr   0   s    	

zUniformInitializer.__init__Nvarpaddle.Tensorblockpir.Block | Nonepaddle.Tensor | Nonec                 C  s  t |tjr| rJ d| |}t |tjtjfsJ t s*t|dg dd | j	dkr4|j
j| _	|jtjjjkrZt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rt||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 )a\  Initialize the input tensor with Uniform distribution.

        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, uniform initializer not support lazy init for dist param.ZOut)Zuint16Zfloat16Zfloat32Zfloat64Zuniform_randomr   .tmpF)nameshapedtypetypeZpersistableN)r1   r2   minmaxr   r   r   r   T)r3   inputsoutputsattrsZstop_gradientcastX)Zin_dtype	out_dtype)r3   r6   r7   r8   ))
isinstancer   ZEagerParamBaseZis_distZ_check_blockZBlockr   r   r	   r    programZrandom_seedr2   r   ZVarDescZVarTypeZFP16ZFP32Z
create_varr   generatejoinr0   r1   ZDENSE_TENSORr   uniformr   r   r
   r9   Z_share_underline_tensor_tor   ZDataTypeZFLOAT16ZFLOAT32Z	append_opr!   r"   r#   op)r$   r)   r+   r;   Zout_varZvar_tmprA   r'   r'   r(   forwardJ   s   








	
zUniformInitializer.forward)r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   )N)r)   r*   r+   r,   r   r-   )__name__
__module____qualname____doc__r   rB   __classcell__r'   r'   r%   r(   r       s    r   c                      s&   e Zd ZdZ	d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|None, 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   Nr   r   r   r0   
str | Noner   r   c                   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   r0   r%   r'   r(   r      s   
zUniform.__init__)r   r   N)r   r   r   r   r0   rI   r   r   )rC   rD   rE   rF   r   rG   r'   r'   r%   r(   rH      s    +rH   )
__future__r   Zpaddler   r   baser   r   r   Zbase.data_feederr	   Zbase.frameworkr
   r   r   Zinitializerr   __all__r   rH   r'   r'   r'   r(   <module>   s    