o
    + it                     @  s>  d dl mZ d dlZ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mZ ddlmZ erTd d	lmZ d d
lmZ d dlmZ d dlmZmZmZmZ g Z				dEdFddZ								dGdHd,d-Z				dIdJd0d1Z					2				dKdLd6d7Z	8	9	:		dMdNd@dAZ					dOdPdCdDZ dS )Q    )annotationsN)TYPE_CHECKING)_C_opsin_dynamic_mode)in_dygraph_modein_dynamic_or_pir_modein_pir_mode   )
check_typecheck_variable_and_dtype)LayerHelper)Sequence)Literal)Tensor)DataLayout1DDataLayout2DDataLayout3DDataLayoutND      -q=xr   pfloataxisintepsilonname
str | Nonereturnc           	      C  sp  t  r!tjdg|| jd}t| t|||dd}| t|| S t rFtjdg|| jd}t| t|||dd}tj	| t|||dS t
|dttfd t
|dtd t| d	g d
d t| jdkru|dkru|dkrutd| |t|d|d}tdi t }|j| jd}|jdd| id|i|d |jj|jd}tjdg||jd}tj	| t|||dS )aL  
    Normalize ``x`` along dimension ``axis`` using :math:`L_p` norm. This layer computes

    .. math::

        y = \frac{x}{ \max\left( \lvert \lvert x \rvert \rvert_p, epsilon\right) }

    .. math::
        \lvert \lvert x \rvert \rvert_p = \left( \sum_i {\lvert x_i \rvert^p}  \right)^{1/p}

    where, :math:`\sum_i{\lvert x_i \rvert^p}` is calculated along the ``axis`` dimension.


    Parameters:
        x (Tensor): The input tensor could be N-D tensor, and the input data type could be float32 or float64.
        p (float|int, optional): The exponent value in the norm formulation. Default: 2.
        axis (int, optional): The axis on which to apply normalization. If `axis < 0`, the dimension to normalization is `x.ndim + axis`. -1 is the last dimension.
        epsilon (float, optional): Small float added to denominator to avoid dividing by zero. Default is 1e-12.
        name (str|None, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        Tensor, the output has the same shape and data type with ``x``.

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> import paddle.nn.functional as F

            >>> paddle.disable_static()
            >>> x = paddle.arange(6, dtype="float32").reshape([2,3])
            >>> y = F.normalize(x)
            >>> print(y)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[0.        , 0.44721359, 0.89442718],
             [0.42426404, 0.56568539, 0.70710671]])

            >>> y = F.normalize(x, p=1.5)
            >>> print(y)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[0.        , 0.40862012, 0.81724024],
             [0.35684016, 0.47578689, 0.59473360]])

            >>> y = F.normalize(x, axis=0)
            >>> print(y)
            Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[0.        , 0.24253564, 0.37139067],
             [1.        , 0.97014254, 0.92847669]])

    r   )shapeZ
fill_valuedtypeTFr   r   	normalizer   r   )float16float32float64r   zAAxis must be 0 or -1 when x is a 1-D tensor, but received axis = )r   ZporderZkeepdimr   p_normr!   XZOuttypeinputsoutputsattrsN)r(   )r   paddlefullr!   r   r(   r   maximumr   divider
   r   r   lenr    
ValueErrorr   locals"create_variable_for_type_inference	append_opblockZ
create_var)	r   r   r   r   r   epsoutr/   helper r=   e/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/nn/functional/norm.pyr#   .   s<   ;r#   F?h㈵>NCHWrunning_meanrunning_varweightTensor | Nonebiastrainingboolmomentumdata_formatr   use_global_statsbool | Nonec                 C  s  t | jdksJ d|}|}g d}||vrtd| |d dkr&dnd}|	d	u r2| }	d
}n|	 }t rPt| ||||| ||||	|\}}}}}}|S t rkt| ||||| ||||	|\}}}}}}|S t| dg dd ||| |d
|	|d}| g|g|gd}|r|g|d< |r|g|d< t	di t
 }ddlm} || jdvr| jnd}|j|dd}|j|dd}|| j}|g|g|g|g|gd}|s|r|j| jdd}|g|d< |jd|||d ||S )aQ  
    Applies Batch Normalization as described in the paper Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift .

    nn.functional.batch_norm is used for nn.BatchNorm1D, nn.BatchNorm2D, nn.BatchNorm3D. Please use above API for BatchNorm.

    Parameters:
        x(Tensor): input value. It's data type should be float32, float64.
        running_mean(Tensor): running mean.
        running_var(Tensor): running variance.
        weight(Tensor, optional): The weight tensor of batch_norm. Default: None.
        bias(Tensor, optional): The bias tensor of batch_norm. Default: None.
        epsilon(float, optional): The small value added to the variance to prevent division by zero. Default: 1e-5.
        training(bool, optional): True means train mode which compute by batch data and track global mean and var during train period. False means inference mode which compute by global mean and var which calculated by train period. Default False.
        momentum(float, optional): The value used for the moving_mean and moving_var computation. Default: 0.9.
        data_format(str, optional): Specify the input data format, may be "NC", "NCL", "NCHW", "NCDHW", "NLC", "NHWC" or "NDHWC", where `N` is batch size, `C` is the number of the feature map, `D` is the depth of the feature, `H` is the height of the feature map, `W` is the width of the feature map, `L` is the length of the feature map. Default "NCHW".
        use_global_stats(bool|None, optional): Whether to use global mean and variance. If set to False, use the statistics of one mini-batch, if set to True, use the global statistics, if set to None, use global statistics in the test phase and use the statistics of one mini-batch in the training phase. Default: None.
        name(str|None, optional): Name for the BatchNorm, default is None. For more information, please refer to :ref:`api_guide_Name`..

    Returns:
        None

    Examples:
        .. code-block:: python

            >>> import paddle

            >>> x = paddle.arange(12, dtype="float32").reshape([2, 1, 2, 3])
            >>> print(x)
            Tensor(shape=[2, 1, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[[[0. , 1. , 2. ],
               [3. , 4. , 5. ]]],
             [[[6. , 7. , 8. ],
               [9. , 10., 11.]]]])
            >>> running_mean = paddle.to_tensor([0], dtype="float32")
            >>> running_variance = paddle.to_tensor([1], dtype="float32")
            >>> weight = paddle.to_tensor([2], dtype="float32")
            >>> bias = paddle.to_tensor([1], dtype="float32")

            >>> batch_norm_out = paddle.nn.functional.batch_norm(x, running_mean,
            ...                                             running_variance, weight, bias)
            >>> print(batch_norm_out)
            Tensor(shape=[2, 1, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[[[1.         , 2.99998999 , 4.99997997 ],
               [6.99996948 , 8.99995995 , 10.99994946]]],
             [[[12.99993896, 14.99992943, 16.99991989],
               [18.99990845, 20.99989891, 22.99988937]]]])

    r   zinput dim must be larger than 1)ZNCNCLrA   NCDHWNLCNHWCNDHWCz\data_format must be one of 'NC', 'NCL', 'NCHW', 'NCDHW', 'NLC', 'NHWC', 'NDHWC' but receive r   CrA   rP   NFinput)r$   uint16r%   r&   Z	BatchNorm)rI   r   Zis_testdata_layoutZfuse_with_relurK   trainable_statistics)r*   MeanVarianceScaleBias
batch_normr   convert_dtype)r$   rT   r%   Tr!   Zstop_gradient)YZMeanOutZVarianceOut	SavedMeanSavedVarianceZReserveSpacer+   )r[   )r4   r    r5   r   r   r[   r   Zbatch_norm_r   r   r6   paddle.base.data_feederr]   r!   r7   r8   append_activation)r   rB   rC   rD   rF   rG   rI   r   rJ   rK   r   mean_outvariance_outZtrue_data_formatrV   Zbatch_norm_out_t1t2t3Zt4r/   r-   r<   r]   param_dtype
saved_meansaved_variancer.   Zreserve_spacer=   r=   r>   r[      s   =



r[   normalized_shapeint | Sequence[int]c                 C  s  t | j}t|}t|tjr|g}nt|trt |}n	t|t s&tdt|}|| }	||k s=tj	
||	d |sUt|}
td|
 d |
dd  d t| t rct| ||||	}|S t| dg dd	 i }| g|d
< |rz|g|d< |r|g|d< ||	d}tdi t }ddlm} || jdkr| jnd}|j|dd}|j|dd}|| j}|jd||||d||	dd ||S )a"  
    nn.LayerNorm is recommended.
    For more information, please refer to :ref:`api_paddle_nn_LayerNorm` .

    Parameters:
        x(Tensor): Input Tensor. It's data type should be bfloat16, float16, float32, float64.
        normalized_shape(int|list|tuple): Input shape from an expected input of
            size :math:`[*, normalized_shape[0], normalized_shape[1], ..., normalized_shape[-1]]`.
            If it is a single integer, this module will normalize over the last dimension
            which is expected to be of that specific size.
        weight(Tensor, optional): The weight tensor of layer_norm. Default: None.
        bias(Tensor, optional): The bias tensor of layer_norm. Default: None.
        epsilon(float, optional): The small value added to the variance to prevent
            division by zero. Default: 1e-05.
        name(str, optional): Name for the LayerNorm, default is None. For more information, please refer to :ref:`api_guide_Name` .

    Returns:
        None

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> paddle.seed(2023)
            >>> x = paddle.rand((2, 2, 2, 3))
            >>> layer_norm_out = paddle.nn.functional.layer_norm(x, x.shape[1:])
            >>> print(layer_norm_out)
            Tensor(shape=[2, 2, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[[[ 0.87799639, -0.32706568, -1.23529339],
               [ 1.01540327, -0.66222906, -0.72354043]],
              [[ 1.24183702,  0.45458138, -0.33506915],
               [ 0.41468468,  1.26852870, -1.98983312]]],
             [[[ 0.02837803,  1.27684665, -0.90110683],
               [-0.94709367, -0.15110941, -1.16546965]],
              [[-0.82010198,  0.11218392, -0.86506516],
               [ 1.09489357,  0.19107464,  2.14656854]]]])

    z@`normalized_shape` should be int, list of ints or tuple of ints.NzGiven normalized_shape is z , expected input with shape [*, r   z, but got input shape rS   )rT   r$   r%   r&   Z	LayerNormr*   rY   rZ   )r   begin_norm_axis
layer_normr   r\   r$   r%   Tr^   r_   rW   rX   r+   )rp   )listr    r4   
isinstancenumbersIntegraltupler5   r0   utilsZis_same_shapestrr   r   rp   r   r   r6   rb   r]   r!   r7   r8   rc   )r   rm   rD   rF   r   r   Zinput_shapeZ
input_ndimZnormalized_ndimro   Zstr_normalized_shaper;   r-   r/   r<   r]   rj   rd   re   Zlayer_norm_outr=   r=   r>   rp   @  s   
/



	




rp   Tuse_input_statsr:   %Literal['NC', 'NCL', 'NCHW', 'NCDHW']c
                 C  s   t  rt| |||}
|
S t| dg dd |||d}|r*|r*| g|g|gd}nd| gi}tdi t }|j| jdd	}|j| jdd	}|| j}|g|g|gd
}|jd|||d |S )a  
    It is recommended to use :ref:`api_paddle_nn_InstanceNorm1D` , :ref:`api_paddle_nn_InstanceNorm2D` , :ref:`api_paddle_nn_InstanceNorm3D` to call this method internally.

    Parameters:
        x (Tensor): Input Tensor. It's data type should be float32, float64.
        running_mean (Tensor, optional): running mean. Default None. Obsolete (that is, no longer usable).
        running_var (Tensor, optional): running variance. Default None. Obsolete (that is, no longer usable).
        weight (Tensor, optional): The weight tensor of instance_norm. Default: None.
            If its value is None, this parameter will be initialized by one.
        bias (Tensor, optional): The bias tensor of instance_norm. Default: None.
            If its value is None, this parameter will be initialized by zero.
        eps (float, optional): A value added to the denominator for numerical stability. Default is 1e-5.
        momentum (float, optional): The value used for the moving_mean and moving_var computation. Default: 0.9.
        use_input_stats (bool, optional): Default True. Obsolete (that is, no longer usable).
        data_format (str, optional): Specify the input data format, may be "NC", "NCL", "NCHW" or "NCDHW". Default "NCHW".
        name (str|None, optional): Name for the InstanceNorm, default is None. For more information, please refer to :ref:`api_guide_Name`..

    Returns:
        None.

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> paddle.seed(2023)
            >>> x = paddle.rand((2, 2, 2, 3))
            >>> instance_norm_out = paddle.nn.functional.instance_norm(x)

            >>> print(instance_norm_out)
            Tensor(shape=[2, 2, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[[[ 1.25768495, -0.18054862, -1.26451230],
               [ 1.42167914, -0.58056390, -0.65373862]],
              [[ 0.95882601,  0.25075224, -0.45947552],
               [ 0.21486834,  0.98283297, -1.94780385]]],
             [[[ 0.40697321,  1.90885782, -0.71117985],
               [-0.76650119,  0.19105314, -1.02920341]],
              [[-1.06926346, -0.18710862, -1.11180890],
               [ 0.74275863, -0.11246002,  1.73788261]]]])

    rS   )r%   r&   r$   rT   ZInstanceNorm)r   rI   rJ   )r*   rY   rZ   r*   instance_normTr^   )r_   r`   ra   r+   N)r{   )	r   r   r{   r   r   r6   r7   r!   r8   )r   rB   rC   rD   rF   ry   rI   r:   rJ   r   r;   r/   r-   r<   rk   rl   Zinstance_norm_outr.   r=   r=   r>   r{     s@   5
r{   -C6?      ?      ?sizealphabetak*DataLayout1D | DataLayout2D | DataLayout3Dc              	   C  s  t  st| dddgd |dvrtd| | j}t|}|dk r*td| d	t|D ]\}	}
|
d
ksD|	d
krDtd|	 d|
 q.|d dkrMdnd}d
dlm} |dd |dd d}tj	t
| | dd}|sd
d
|d |d d g}|df}|d
 d|d |d t||d |d   g}d
d
d
d
|d |d d g}|ddf}n:|d |d d d
d
g}d|f}|d
 d|d t||d |d   |d g}|d |d d d
d
d
d
g}dd|f}|dkrtjjj||d}tjjj||dd}tj|dd}n&tj||d}tjjj||dd}tjjj||dd}ttj|dd|}tj|||d}t||}tj| ||d}|S )a	  
    Local Response Normalization performs a type of "lateral inhibition" by normalizing over local input regions.
    For more information, please refer to `ImageNet Classification with Deep Convolutional Neural Networks <https://papers.nips.cc/paper/4824-imagenet-classification-with-deep-convolutional-neural-networks.pdf>`_

    The formula is as follows:

    .. math::

        Output(i, x, y) = Input(i, x, y) / \left(k + \alpha \sum\limits^{\min(C-1, i + size/2)}_{j = \max(0, i - size/2)}(Input(j, x, y))^2\right)^{\beta}

    In the above equation:

    - :math:`size` : The number of channels to sum over.
    - :math:`k` : The offset (avoid being divided by 0).
    - :math:`\\alpha` : The scaling parameter.
    - :math:`\\beta` : The exponent parameter.


    Args:
        x (Tensor): The input 3-D/4-D/5-D tensor. The data type is float16 or float32.
        size (int): The number of channels to sum over.
        alpha (float, optional): The scaling parameter, positive. Default:1e-4
        beta (float, optional): The exponent, positive. Default:0.75
        k (float, optional): An offset, positive. Default: 1.0
        data_format (str, optional): Specify the data format of the input, and the data format of the output
            will be consistent with that of the input. An optional string from:
            If x is 3-D Tensor, the string could be `"NCL"` or `"NLC"` . When it is `"NCL"`,
            the data is stored in the order of: `[batch_size, input_channels, feature_length]`.
            If x is 4-D Tensor, the string could be  `"NCHW"`, `"NHWC"`. When it is `"NCHW"`,
            the data is stored in the order of: `[batch_size, input_channels, input_height, input_width]`.
            If x is 5-D Tensor, the string could be  `"NCDHW"`, `"NDHWC"` . When it is `"NCDHW"`,
            the data is stored in the order of: `[batch_size, input_channels, input_depth, input_height, input_width]`.
        name (str|None, optional): Name for the operation (optional, default is None). For more information,
            please refer to :ref:`api_guide_Name`.

    Returns:
        A tensor storing the transformation result with the same shape and data type as input.


    Examples:

        .. code-block:: python

            >>> import paddle

            >>> x = paddle.rand(shape=(3, 3, 112, 112), dtype="float32")
            >>> y = paddle.nn.functional.local_response_norm(x, size=5)
            >>> print(y.shape)
            [3, 3, 112, 112]

    r   r$   r%   local_response_norm)rM   rO   rA   rP   rN   rQ   zNdata_format should be in one of [NCL, NCHW, NCDHW, NLC, NHWC, NDHWC], but got r	   z4Expected 3D or higher dimensionality input, but got z dimensionsr   zCExpected every dim's size to be larger than 0, but the size of the z-th dim is r'   rR   TF)reducec                 S  s   | | S )Nr=   )r   yr=   r=   r>   <lambda>q  s    z%local_response_norm.<locals>.<lambda>r   N)r   r   )pad)Zkernel_sizeZstride)r    rN   )r   rJ   )scalerF   r"   )r   r   r5   r    r4   	enumerate	functoolsr   r0   Z	unsqueezemultiplyr   nnZ
functionalr   Z
avg_pool2dZsqueezeZreshapeZ
avg_pool3dr   powr3   )r   r   r   r   r   rJ   r   sizesdimiszZchannel_lastr   Z	sum_sizesdivZpad4d_shapeZpool2d_shapeZreshape_shapeZpad5d_shapeZpool3d_shaperesr=   r=   r>   r     s   <


r   
num_groupsc                 C  s   |dvr
t d| |d dkrdnd}t r!t| |||||S tdi t }|j| jdd	}|j| jdd	}	d
| i}
|durE||
d< |durM||
d< |j| jd}|jd|
|||	d|||dd |	|S )a	  
    nn.GroupNorm is recommended.
    For more information, please refer to :ref:`api_paddle_nn_GroupNorm` .

    Parameters:
        x(Tensor): Input Tensor with shape: attr:`(batch, num_features, *)`.
        num_groups(int): The number of groups that divided from channels.
        epsilon(float, optional): The small value added to the variance to prevent
            division by zero. Default: 1e-05.
        weight(Tensor, optional): The weight Tensor of group_norm, with shape: attr:`[num_channels]`.
            Default: None.
        bias(Tensor, optional): The bias Tensor of group_norm, with shape: attr:`[num_channels]`.
            Default: None.
        data_format(str, optional): Specify the input data format. Support "NCL", "NCHW", "NCDHW", "NLC", "NHWC" or "NDHWC". Default: "NCHW".
        name(str|None, optional): Name for the GroupNorm, default is None. For more information, please refer to :ref:`api_guide_Name`..

    Returns:
        Tensor, the output has the same shape with ``x``.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> paddle.seed(100)
            >>> x = paddle.arange(48, dtype="float32").reshape((2, 6, 2, 2))
            >>> group_norm_out = paddle.nn.functional.group_norm(x, num_groups=6)

            >>> print(group_norm_out)
            Tensor(shape=[2, 6, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
            [[[[-1.34163547, -0.44721183],
               [ 0.44721183,  1.34163547]],
              [[-1.34163547, -0.44721183],
               [ 0.44721183,  1.34163547]],
              [[-1.34163547, -0.44721183],
               [ 0.44721183,  1.34163547]],
              [[-1.34163547, -0.44721183],
               [ 0.44721183,  1.34163547]],
              [[-1.34163547, -0.44721183],
               [ 0.44721183,  1.34163547]],
              [[-1.34163547, -0.44721183],
               [ 0.44721183,  1.34163547]]],
             [[[-1.34163547, -0.44721183],
               [ 0.44721183,  1.34163547]],
              [[-1.34163547, -0.44721183],
               [ 0.44721183,  1.34163547]],
              [[-1.34163547, -0.44721183],
               [ 0.44721183,  1.34163547]],
              [[-1.34163547, -0.44721183],
               [ 0.44721183,  1.34163547]],
              [[-1.34163547, -0.44721183],
               [ 0.44721183,  1.34163547]],
              [[-1.34163547, -0.44721183],
               [ 0.44721183,  1.34163547]]]])
    )rM   rA   rN   rO   rP   rQ   zunsupported data layout:r   rR   rA   rP   
group_normTr^   r*   NrZ   rY   r)   rq   )r   groupsrU   r+   )r   )
r5   r   r   r   r   r6   r7   r!   r8   rc   )r   r   r   rD   rF   rJ   r   r<   rd   re   r-   Zgroup_norm_outr=   r=   r>   r     sP   ?	
r   )r   r   r   N)r   r   r   r   r   r   r   r   r   r   r   r   )NNFr?   r@   rA   NN)rB   r   rC   r   rD   rE   rF   rE   rG   rH   rI   r   r   r   rJ   r   rK   rL   r   r   r   r   )NNr@   N)r   r   rm   rn   rD   rE   rF   rE   r   r   r   r   r   r   )	NNNNTr?   r@   rA   N)r   r   rB   rE   rC   rE   rD   rE   rF   rE   ry   rH   rI   r   r:   r   rJ   rz   r   r   r   r   )r|   r}   r~   rA   N)r   r   r   r   r   r   r   r   r   r   rJ   r   r   r   r   r   )r@   NNrA   N)r   r   r   r   r   r   rD   rE   rF   rE   rJ   r   r   r   r   r   )!
__future__r   rt   typingr   r0   r   r   Zpaddle.base.frameworkr   r   r   Zbase.data_feederr
   r   Zbase.layer_helperr   collections.abcr   r   r   Zpaddle._typingr   r   r   r   __all__r#   r[   rp   r{   r   r   r=   r=   r=   r>   <module>   sr   d 6{c 