o
    + i#                     @  sT  d dl mZ d dlZddlmZmZ ddlmZ ddlm	Z	 ddl
mZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZ ddlmZmZ 	 		d@dAddZ	 		d@dAddZ			dBdCd!d"Z			dBdCd#d$Z	%	dDdEd'd(Z	%	dDdFd+d,Z	%		-	.dGdHd/d0ZdId2d3Z dJd4d5Z!dJd6d7Z"	dKdLd:d;Z#dJd<d=Z$	dKdMd>d?Z%dS )N    )annotationsN   )in_dygraph_modein_pir_mode   ConstantDirac)calculate_gain)KaimingNormalKaimingUniform)NormalTruncatedNormal
OrthogonalUniform)XavierNormalXavierUniformfan_in
leaky_relutensorpaddle.Tensorafloatmodestrnonlinearityreturnpaddle.Tensor | Nonec                 C     t |||d}|| S )a  Modify tensor inplace using Kaiming uniform method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        a (float, optional): The negative slope of the rectifier used after this layer.
            Defaults to 0.
        mode (str, optional): Mode to compute the fan. Choose from ["fan_in", "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'.
        nonlinearity (str, optional): Nonlinearity method name. Defaults to "leaky_relu".

    Returns:
        Tensor: Initialized tensor.
    Znegative_sloper   r   )r   r   r   r   r   init r%   Z/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/nn/init.pykaiming_uniform_   s   r'   c                 C  r!   )a  Modify tensor inplace using Kaiming normal method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        a (float, optional): The negative slope of the rectifier used after this layer.
            Defaults to 0.
        mode (str, optional): Mode to compute the fan. Choose from ["fan_in", "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'.
        nonlinearity (str, optional): Nonlinearity method name. Defaults to "leaky_relu".

    Returns:
        Tensor: Initialized tensor.
    r"   )r   r#   r%   r%   r&   kaiming_normal_:   s   r(         ?gainfloat | Nonefan_outc                 C     t |||d}|| S )a  Modify tensor inplace using Xavier uniform method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        gain (float, optional): Scaling Tensor. Default is 1.0.
        fan_in (float|None, optional): fan_in for Xavier initialization, which is
                inferred from the Tensor. Default is None.
        fan_out (float|None, optional): fan_out for Xavier initialization, which is
                 inferred from the Tensor. Default is None.

    Returns:
        Tensor: Initialized tensor.
    r*   r   r,   )r   r   r*   r   r,   r$   r%   r%   r&   xavier_uniform_T      r0   c                 C  r-   )a  Modify tensor inplace using Xavier normal method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        gain (float, optional): Scaling Tensor. Default is 1.0.
        fan_in (float|None, optional): fan_in for Xavier initialization, which is
                inferred from the Tensor. Default is None.
        fan_out (float|None, optional): fan_out for Xavier initialization, which is
                 inferred from the Tensor. Default is None.

    Returns:
        Tensor: Initialized tensor.
    r.   )r   r/   r%   r%   r&   xavier_normal_p   r1   r2           bc                 C     t ||d}|| S )a^  Modify tensor inplace using uniform method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        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`.

    Returns:
        Tensor: Initialized tensor.
    )lowhighr   )r   r   r4   r$   r%   r%   r&   uniform_      r8   meanstdc                 C  r5   )aL  Modify tensor inplace using normal method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        mean (float|complex, optional): mean of the normal distribution. Default is 0.0.
        std (float, optional): standard deviation of the normal distribution. Default is 1.0.

    Returns:
        Tensor: Initialized tensor.
    )r:   r;   )r   )r   r:   r;   r$   r%   r%   r&   normal_   r9   r<                 @c                 C  s   t ||||d}|| S )a  Modify tensor inplace using truncated normal method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        mean (float|complex, optional): mean of the normal distribution. Default is 0.0.
        std (float, optional): standard deviation of the normal distribution. Default is 1.0.
        a (float, optional): The minimum cutoff value. Default is -2.0.
        b (float, optional): The maximum cutoff value. Default is 2.0.

    Returns:
        Tensor: Initialized tensor.
    )r:   r;   r   r4   )r   )r   r:   r;   r   r4   r$   r%   r%   r&   trunc_normal_   s   r?   valc                 C     t |d}|| S )zModify tensor inplace using constant method.

    Args:
        tensor (Tensor):  Paddle Tensor.
        value (float32|float64, optional): constant value to initialize the parameter.

    Returns:
        Tensor: Initialized tensor.
    valuer   )r   r@   r$   r%   r%   r&   	constant_      
rD   c                 C     t dd}|| S )zFill the input Tensor with the scalar value 1.

    Args:
        tensor (Tensor):  Paddle Tensor.

    Returns:
        Tensor: Initialized tensor.
    r)   rB   r   r   r$   r%   r%   r&   ones_      
rH   c                 C  rF   )zFill the input Tensor with the scalar value 0.

    Args:
        tensor (Tensor):  Paddle Tensor.

    Returns:
        Tensor: Initialized tensor.
    r3   rB   r   rG   r%   r%   r&   zeros_   rI   rJ   groupsintc                 C  rA   )a4  Initialize the 3D/4D/5D Tensor with Dirac delta function.

    Args:
        tensor (Tensor):  Paddle Tensor.
        groups (int|None, optional): 0-dimension of the Tensor will be divided by groups,
            each group has the same value. Default: 1.
    Returns:
        Tensor: Initialized tensor.
    )rK   r	   )r   rK   r$   r%   r%   r&   dirac_   rE   rM   c                 C  s   t | jdkrtdt | j dt r+tj| jd | jd | jd}||  dS t r?tj| jd | jd | jd}|S t	d)	zFill the 2-dimensional input Tensor with the identity matrix.

    Args:
        tensor (Tensor):  Paddle Tensor.
    Returns:
        Tensor: Initialized tensor.
    r   z+Only support 2 dimensional tensor, but got .r   r   )dtypeNz-Only support run in dygraph mode or PIR mode.)
lenshapeAssertionErrorr   paddleeyerO   Z_share_underline_tensor_tor   NotImplementedError)r   Z
new_tensorr%   r%   r&   eye_  s$   
rV   c                 C  rA   )a  Fill the input Tensor with a (semi) orthogonal matrix.

    Args:
        tensor (Tensor):  Paddle Tensor.
        gain(float, optional): The multiplication coefficient for initialized tensor. Default: 1.0.
    Returns:
        Tensor: Initialized tensor.
    )r*   r   )r   r*   r$   r%   r%   r&   orthogonal_1  s   
rW   )r   r   r   )
r   r   r   r   r   r   r   r   r   r    )r)   NN)
r   r   r*   r   r   r+   r,   r+   r   r    )r3   r)   )r   r   r   r   r4   r   r   r    )r   r   r:   r   r;   r   r   r    )r3   r)   r=   r>   )r   r   r:   r   r;   r   r   r   r4   r   r   r    )r   r   r@   r   r   r    )r   r   r   r    )r   )r   r   rK   rL   r   r    )r   r   r*   r   r   r    )&
__future__r   rS   Zbase.frameworkr   r   Zinitializer.constantr   Zinitializer.diracr
   Zinitializer.initializerr   Zinitializer.kaimingr   r   Zinitializer.normalr   r   Zinitializer.orthogonalr   Zinitializer.uniformr   Zinitializer.xavierr   r   r'   r(   r0   r2   r8   r<   r?   rD   rH   rJ   rM   rV   rW   r%   r%   r%   r&   <module>   s\   



#