o
    "j                     @   sD   d dl mZ d dlmZ d dlmZ d dlmZmZ 	dd	d
Z	dS )    )_C_ops)core)default_main_program)LayerHelperin_dynamic_or_pir_mode      ?Tupscale_in_trainNc                    s  t |ttfr|dkr| | S |dk s|dkrtd dvr#tdd}t rKt jdkr2t j}t| |d||  |durA|nd|du\}}|S t	di t
 }	|	j| jd}|	jtjjjd	d
} fdd}
|
|	j|| |}|	jd| |dd|g|gd|d |S )aJ	  
    Fused Dropout and Add.

    Args:
        x (Tensor): The input tensor. The data type is bfloat16, float16, float32 or float64.
        y (Tensor): The input tensor. The data type is bfloat16, float16, float32 or float64.

        p (float|int, optional): Probability of setting units to zero. Default: 0.5.
        training (bool, optional): A flag indicating whether it is in train phrase or not. Default: True.
        mode(str, optional): ['upscale_in_train'(default) | 'downscale_in_infer'].

            1. upscale_in_train (default), upscale the output at training time

                - train: :math:`out = x \times \frac{mask}{(1.0 - dropout\_prob)} + y`
                - inference: :math:`out = x + y`

            2. downscale_in_infer, downscale the output at inference

                - train: :math:`out = input \times mask + y`
                - inference: :math:`out = input \times (1.0 - dropout\_prob) + y`

        name (str, optional): Name for the operation, Default: None. For more information, please refer to :ref:`api_guide_Name`.

    Returns:
        A Tensor representing the fused dropout and add, has same shape and data type as `x` .


    Examples:

        .. code-block:: python

            >>> # doctest: +REQUIRES(env:GPU)
            >>> import paddle
            >>> from paddle.incubate.nn.functional import fused_dropout_add

            >>> paddle.set_device('gpu')
            >>> paddle.seed(2023)
            >>> x = paddle.randn([4, 10], dtype="float32")
            >>> y = paddle.randn([4, 10], dtype="float32")
            >>> out = fused_dropout_add(x, y, p=0.5)
            >>> print(out)
            Tensor(shape=[4, 10], dtype=float32, place=Place(gpu:0), stop_gradient=True,
            [[-0.49133155,  0.53819323, -2.58393312,  0.06336236, -1.09908366,
               0.22085167,  2.19751787,  0.05034769,  0.53417486,  0.84864247],
             [ 0.78248203, -1.59652555, -0.14399840, -0.77985179, -0.17006736,
              -0.30991879, -0.36593807, -0.51025450,  1.46401680,  0.61627960],
             [ 4.50472546, -0.48472026,  0.60729283,  0.33509624, -0.25593102,
              -1.45173049,  1.06727099,  0.00440830, -0.77340341,  0.67393088],
             [ 1.29453969,  0.07568165,  0.71947742, -0.71768606, -2.57172823,
               1.89179027,  3.26482797,  1.10493207, -1.04569530, -1.04862499]])
    r      z!p argument should between 0 and 1)Zdownscale_in_inferr   zBmode argument should be 'downscale_in_infer' or 'upscale_in_train'Nfused_dropout_add)dtypeT)r   Zstop_gradientc                    sD   |d u s|dkr| j dkr| j }|| |d ur|nd|d ud}|S )Nr   )pis_testmodeseedZfix_seed)random_seed)progZdropout_probr   r   attrsr    p/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/incubate/nn/functional/fused_dropout_add.py	get_attrsl   s   z$fused_dropout_add.<locals>.get_attrs)xyZseed_tensor)outseed_offset)typeZinputsZoutputsr   )r
   )
isinstanceintfloat
ValueErrorr   r   r   r   r
   r   localsZ"create_variable_for_type_inferencer   r   ZVarDescZVarTypeZINT64Zmain_programZ	append_op)r   r   r   Ztrainingr   namer   r   r   helperr   r   r   r   r   r
      sL   6


r
   )r   Tr   N)
Zpaddler   Zpaddle.baser   Zpaddle.common_ops_importr   Zpaddle.frameworkr   r   r
   r   r   r   r   <module>   s   