o
    * i                     @  s   U d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ erQd dlmZmZmZ d dlmZ ed	 Zd
ed< ed Zd
ed< eeef Zd
ed< ddddZdS )    )annotations)TYPE_CHECKING)_C_ops)check_variable_and_dtype)LayerHelper)in_dynamic_or_pir_mode)Literal	TypeAliasUnion)Tensor)meansumnoner	   _ReduceModeStringLiteral)r         _ReduceModeNumberLiteral_ReduceModer   xr   	reductionreturnc                 C  s   t |trdddd| }|du rtdt r!t| |S t| ddd	gd
 d|i}t	di t
 }|jdd}||}|jd
d| id|i|d |S )a  Marks a tensor as being part of the loss calculation for IPU.

    This operator is used to handle on the (final) loss of a model so that
    it is used as the start of backpropagation.

    When `reduction` is `none`, return raw `Out`.

    When `reduction` is `mean`, return

    .. math::
        Out = MEAN(Out)

    When `reduction` is `sum`, return

    .. math::
        Out = SUM(Out)

    Parameters:
        x (Variable): The input tensor. The shapes is [N, *], where N is batch size and `*` means any number of
             additional dimensions. It's data type should be float32, float64 on CPU and float16, float32 on IPU.
        reduction(str|int, optional): Reduce the loss output. Supported string values are: 'sum', 'mean', 'none'
                            the corresponding int values are 0, 1, 2 respectively. The default value is "none".

    Returns:
        Variable: The loss ``Tensor`` with the specified reduction applied.

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> paddle.enable_static()
            >>> loss = paddle.static.data(name="loss", shape=[-1, 1], dtype="float32")
            >>> out = paddle.incubate.identity_loss(loss, reduction=1)
    r   r   r   )r   r   r   NzUnsupported reduction type.r   Zfloat32Zfloat64identity_lossr   )Zinput_param_nameXZOut)typeZinputsZoutputsattrs)r   )
isinstancestrgetlower	Exceptionr   r   r   r   r   localsZinput_dtypeZ"create_variable_for_type_inferenceZ	append_op)r   r   r   helperZdtypeout r#   c/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/incubate/nn/loss.pyr   $   s   
$
r   N)r   )r   r   r   r   r   r   )
__future__r   typingr   Zpaddler   Zpaddle.base.data_feederr   Zpaddle.base.layer_helperr   Zpaddle.frameworkr   r   r	   r
   r   r   __annotations__r   r   r   r#   r#   r#   r$   <module>   s   