
    x-j$                         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	          Z e            Zd
 ZddZ	 	 	 	 	 	 ddZdS )    N)_legacy_C_ops)core)check_variable_and_dtype)Variable)LayerHelperin_dynamic_modemodel_parallel_rngc                   T    e Zd ZdZd Zd Zd Zd Zd Ze	j
        efd            ZdS )	RNGStatesTrackerz!
    Tracker the RNG states.
    c                 :    i | _         t                      | _        d S Nstates_setseeds_selfs    j/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/paddle/distributed/fleet/layers/mpu/random.py__init__zRNGStatesTracker.__init__'   s    ee    c                 :    i | _         t                      | _        d S r   r   r   s    r   resetzRNGStatesTracker.reset,   s    eer   c                    || j         v rt          d| d          | j                             |           || j        v rt          d| d          t          j                            d          }t          j                                        | j        |<   t	          j        |           t          j        	                    |d           d S )Nzseed z already existsstate T	use_index)
r   
ValueErroraddr   paddleincubateget_rng_stateregister_rng_state_as_indexseedset_rng_state)r   namer#   orig_rng_state_indexs       r   r   zRNGStatesTracker.add0   s    4;:T:::;;;4<;d;;;<<<%<<t<LL#_HHJJTD%%&:d%KKKKKr   c                 $   i }t           j                            d          }| j        D ]D}t           j                            | j        |         d           t          j                    ||<   Et           j                            |d           |S )NTr   )r   r    r!   r   r$   r   statesr&   r%   s       r   get_states_trackerz#RNGStatesTracker.get_states_tracker<   s    %<<t<LLL 	2 	2DO))$,t*<)MMM!/11F4LL%%&:d%KKKr   c                 V   t           j                            d          }|D ]d}|| j        vrt	          d| d          t           j                            | j        |         d           t          j        ||                    et           j                            |d           d S )NTr   r   z does not exists)r   r    r!   r   r   r$   r(   s       r   set_states_trackerz#RNGStatesTracker.set_states_trackerG   s    %<<t<LL 	/ 	/D4<'' !@$!@!@!@AAAO))$,t*<)MMM ....%%&:d%KKKKKr   c              #     K   || j         vrt          d| d          t          j                            d          }t          j                            | j         |         d           	 d V  t          j                            d          | j         |<   t          j                            |d           d S # t          j                            d          | j         |<   t          j                            |d           w xY w)Nr   z does not existTr   )r   r   r   r    r!   r$   )r   r%   r&   s      r   	rng_statezRNGStatesTracker.rng_stateS   s      t|##;d;;;<<<%<<t<LL%%dl4&8D%III	PEEE!'!>!>!>!N!NDLO))*>$)OOOOO "(!>!>!>!N!NDLO))*>$)OOOOs   ,B; ;ADN)__name__
__module____qualname____doc__r   r   r   r*   r,   
contextlibcontextmanagerMODEL_PARALLEL_RNGr.    r   r   r   r   "   s           
  
L 
L 
L	 	 	
L 
L 
L / 	P 	P 	P 	P 	P 	Pr   r   c                      t           S r   )RNG_STATE_TRACKERr6   r   r   get_rng_state_trackerr9   c   s    r   c                    ddl m} |                                }|                                }|                                }|                                }|                                }| r| }| dz   ||z  z   |z   }n.t          j        	                    dd          }|dz   ||z  z   |z   }t                                           t                              t          |           t          j        |           d S )Nr   )fleet   i'  )paddle.distributedr;   get_hybrid_communicate_groupget_model_parallel_rankget_model_parallel_world_sizeget_stage_idget_pipe_parallel_world_sizenprandomrandintr8   r   r   r5   r   r#   )	r#   r;   hcgmp_rankmp_sizepp_rankpp_sizeglobal_seed
local_seeds	            r   model_parallel_random_seedrM   g   s    ((((((

,
,
.
.C))++G//11G  G..00G CAX' 11G;

i''511 1_w'887B
,j999
Kr         ?Tupscale_in_trainc                 F   |)t           j        j                            | |||||          S t	          |t
          t          t          f          st          d          t	          |t          t
          f          r|dk    r| S d|cxk    rdk    sn J t          d                      |dv sJ t          d                      |J t          d                      |d	k    rd
n|}t                      r$t          j        | d|d| ddddd|          \  }}|S t	          |t                    r#|j        dgk    st          d|j                   t          di t                      }	t          | dg dd           |	                    t           j                  }
|	                    dd|
i           |	                    | j                  }|	                    t(          j        j        j        d          }|	                    d| g|
d|g|g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_inferrO   zBmode argument should be 'downscale_in_infer' or 'upscale_in_train'z1unsupported axis when using random seed generatorrQ   downgrade_in_inferdropout_probis_testfix_seedFr#   dropout_implementationzGRequired p.shape == [1] if type(p) is Variable, but received p.shape = dropoutx)float16float32float64)dtypeOut)typeoutputsT)r\   stop_gradient)XSeed)r]   Mask)rS   rT   rV   )r^   inputsr_   attrs)rW   )r   nn
functionalrW   
isinstancefloatintr   	TypeErrorr   r   r   shaper   localsr   "create_variable_for_type_inferenceint32	append_opr\   r   VarDescVarTypeUINT8)rX   paxisrng_nametrainingmoder%   outmaskhelperr#   s              r   rW   rW      s   N y#++Aq$$MMMa%h/00 PNOOO !c5\"" qAvv;;;;Q;;;;;
#FGG;;;====zL@ @=== <<; <<<
 !%(< < <$ 	
  ,!)L$
 
	T 
a"" 	17qc>>cZ[Zacc   33&((33 s555y	
 	
 	
 88v|8LLfudm<<<77ag7FF88,&,D 9 
 
 	d++ ED622 !'<*. 	 	 		
 		
 		
 
r   r   )rN   NNTrO   N)r3   numpyrC   r   r   paddle.baser   paddle.base.data_feederr   paddle.common_ops_importr   paddle.frameworkr   r   __all__r5   r   r8   r9   rM   rW   r6   r   r   <module>r      s0                              < < < < < < - - - - - - 9 9 9 9 9 9 9 9
) ;P ;P ;P ;P ;P ;P ;P ;P| %$&&      4 
			K K K K K Kr   