o
    "j!                     @   s   d dl Z d dlZd dl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mZ g ZdZG dd	 d	Ze Zd
d ZdddZdd Z						dddZdS )    N)_legacy_C_ops)core)check_variable_and_dtype)Variable)LayerHelperin_dynamic_modeZmodel_parallel_rngc                   @   sJ   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	j
efddZdS )RNGStatesTrackerz!
    Tracker the RNG states.
    c                 C      i | _ t | _d S Nstates_setseeds_self r   k/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/distributed/fleet/layers/mpu/random.py__init__'   s   zRNGStatesTracker.__init__c                 C   r	   r
   r   r   r   r   r   reset,   s   zRNGStatesTracker.resetc                 C   sn   || j v rtd| d| j | || jv r td| dt }t| t | j|< t| d S )Nzseed z already existsstate )r   
ValueErroraddr   paddleget_rng_stateseedset_rng_state)r   namer   orig_rng_stater   r   r   r   0   s   


zRNGStatesTracker.addc                 C   s"   i }| j D ]	}| j | ||< q|S r
   r   )r   statesr   r   r   r   get_states_tracker;   s   
z#RNGStatesTracker.get_states_trackerc                 C   s
   || _ d S r
   r   )r   r   r   r   r   set_states_trackerA   s   
z#RNGStatesTracker.set_states_trackerc              	   c   st    || j vrtd| dt }t| j |  zd V  W t | j |< t| d S t | j |< t| w )Nr   z does not exist)r   r   r   r   r   )r   r   r   r   r   r   	rng_stateD   s   
zRNGStatesTracker.rng_stateN)__name__
__module____qualname____doc__r   r   r   r    r!   
contextlibcontextmanagerMODEL_PARALLEL_RNGr"   r   r   r   r   r   "   s    r   c                   C   s   t S r
   )RNG_STATE_TRACKERr   r   r   r   get_rng_state_trackerT   s   r+   c           	      C   s   ddl m} | }| }| }| }| }| r)| }| d ||  | }ntj	dd}|d ||  | }t
  t
t| t| d S )Nr   )fleet   i'  )Zpaddle.distributedr,   Zget_hybrid_communicate_groupZget_model_parallel_rankZget_model_parallel_world_sizeZget_stage_idZget_pipe_parallel_world_sizenprandomrandintr*   r   r   r)   r   r   )	r   r,   ZhcgZmp_rankZmp_sizeZpp_rankZpp_sizeZglobal_seedZ
local_seedr   r   r   model_parallel_random_seedX   s   r1   c                 C   sR   | d ur| dks
J t di t }|jtjd}|jdd|id| ddd |S )	N r   dtypeOutT)Zdeterministicrng_nameZ	force_cpu)typeoutputsattrs)r   )r   locals"create_variable_for_type_inferencer   Zint32	append_op)r6   helperoutr   r   r   determinate_seedp   s   
r?         ?Tupscale_in_trainc                 C   s~  |du rt jj| |||||S t|tttfstdt|ttfr)|dkr)| S d|  kr3dks:n J t	d|dv sDJ t	d|du sNJ td|d	krTd
n|}t
 rmt| d|d| ddddd|\}}|S t|}	t|tr|jdgkstd|j tdi t }
t| dg dd |
j| jd}|
jtjjjdd}|
jd| g|	d|g|gd|| |dd |S )a
  
    Dropout is a regularization technique for reducing overfitting by preventing
    neuron co-adaption during training. The dropout operator randomly sets the
    outputs of some units to zero, while upscale others according to the given
    dropout probability.

    Args:
        x (Tensor): The input tensor. The data type is float32 or float64.
        p (float|int): Probability of setting units to zero. Default 0.5.
        axis (int|list|tuple): The axis along which the dropout is performed. Default None.
        rng_name (str): The random seed generator name, which used to obtain deterministic results.
        training (bool): A flag indicating whether it is in train phrase or not. Default True.
        mode(str): ['upscale_in_train'(default) | 'downscale_in_infer'].

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

                              - train: out = input * mask / ( 1.0 - dropout_prob )
                              - inference: out = input

                           2. downscale_in_infer, downscale the output at inference

                              - train: out = input * mask
                              - inference: out = input * (1.0 - dropout_prob)
        name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.

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


    Examples:
        We use ``p=0.5`` in the following description for simplicity.

        1. When ``axis=None`` , this is commonly used dropout, which dropout each element of x randomly.

        ..  code-block:: text

            Let's see a simple case when x is a 2d tensor with shape 2*3:
            [[1 2 3]
             [4 5 6]]
            we generate mask with the same shape as x, which is 2*3. The value of mask is
            sampled from a Bernoulli distribution randomly. For example, we may get such mask:
            [[0 1 0]
             [1 0 1]]
            So the output is obtained from elementwise multiply of x and mask:
            [[0 2 0]
             [4 0 6]]
            Using default setting, i.e. ``mode='upscale_in_train'`` ,
            if in training phase, the final upscale output is:
            [[0 4 0 ]
             [8 0 12]]
            if in test phase, the output is the same as input:
            [[1 2 3]
             [4 5 6]]
            we can also set ``mode='downscale_in_infer'`` , then
            if in training phase, the final output is:
            [[0 2 0]
             [4 0 6]]
            if in test phase, the scale output is:
            [[0.5 1.  1.5]
             [2.  2.5 3. ]]

    Nz4p argument should be a number(int|float) or Variabler   r-   z!p argument should between 0 and 1)downscale_in_inferrA   zBmode argument should be 'downscale_in_infer' or 'upscale_in_train'z/unsupport axis when using random seed generatorrB   Zdowngrade_in_inferdropout_probis_testZfix_seedFr   dropout_implementationzGRequired p.shape == [1] if type(p) is Variable, but received p.shape = dropoutx)Zfloat16Zfloat32Zfloat64r3   T)r4   Zstop_gradient)XZSeed)r5   Mask)rC   rD   rE   )r7   Zinputsr8   r9   )rF   )r   nnZ
functionalrF   
isinstancefloatintr   	TypeErrorr   r   r   r?   shaper   r:   r   r;   r4   r   ZVarDescZVarTypeZUINT8r<   )rG   pZaxisr6   Ztrainingmoder   r>   maskr   r=   r   r   r   rF   }   sj   G"



rF   r
   )r@   NNTrA   N)r'   numpyr.   r   r   Zpaddle.baser   Zpaddle.base.data_feederr   Zpaddle.common_ops_importr   Zpaddle.frameworkr   r   __all__r)   r   r*   r+   r1   r?   rF   r   r   r   r   <module>   s,   /
