
    Αi$                         S SK r S SKrS SKrS SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJrJr  / rSr " S S	5      r\" 5       rS
 rSS jr      SS jrg)    N)_legacy_C_ops)core)check_variable_and_dtype)Variable)LayerHelperin_dynamic_modemodel_parallel_rngc                   `    \ rS rSrSrS rS rS rS rS r	\
R                  \4S j5       rS	rg
)RNGStatesTracker"   z
Tracker the RNG states.
c                 0    0 U l         [        5       U l        g Nstates_setseeds_selfs    j/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distributed/fleet/layers/mpu/random.py__init__RNGStatesTracker.__init__'   s    e    c                 0    0 U l         [        5       U l        g r   r   r   s    r   resetRNGStatesTracker.reset,   s    er   c                    X R                   ;   a  [        SU S35      eU R                   R                  U5        XR                  ;   a  [        SU S35      e[        R
                  R                  SS9n[        R
                  R                  5       U R                  U'   [        R                  " U5        [        R
                  R                  USS9  g )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!   RNGStatesTracker.add0   s    ;;uTF/:;;<<vdV?;<<%<<t<L#__HHJTD%%&:d%Kr   c                 *   0 n[         R                  R                  SS9nU R                   HE  n[         R                  R	                  U R                  U   SS9  [         R                  " 5       X'   MG     [         R                  R	                  USS9  U$ )NTr   )r"   r#   r$   r   r'   r   statesr)   r(   s       r   get_states_tracker#RNGStatesTracker.get_states_tracker<   s|    %<<t<LLLDOO))$,,t*<)M!//1FL	 !
 	%%&:d%Kr   c                 N   [         R                  R                  SS9nU Hd  nX0R                  ;  a  [	        SU S35      e[         R                  R                  U R                  U   SS9  [         R
                  " X   5        Mf     [         R                  R                  USS9  g )NTr   r   z does not exists)r"   r#   r$   r   r    r'   r,   s       r   set_states_tracker#RNGStatesTracker.set_states_trackerG   s    %<<t<LD<<' 6$/?!@AAOO))$,,t*<)M  .  	%%&:d%Kr   c              #     #    XR                   ;  a  [        SU S35      e[        R                  R	                  SS9n[        R                  R                  U R                   U   SS9   S v   [        R                  R	                  SS9U R                   U'   [        R                  R                  USS9  g ! [        R                  R	                  SS9U R                   U'   [        R                  R                  USS9  f = f7f)Nr   z does not existTr   )r   r    r"   r#   r$   r'   )r   r(   r)   s      r   	rng_stateRNGStatesTracker.rng_stateS   s     ||#vdV?;<<%<<t<L%%dll4&8D%I	P!'!>!>!>!NDLLOO))*>$)O "(!>!>!>!NDLLOO))*>$)Os    A'D*B7 .A	D7A
DD)r   r   N)__name__
__module____qualname____firstlineno____doc__r   r   r!   r.   r1   
contextlibcontextmanagerMODEL_PARALLEL_RNGr4   __static_attributes__ r   r   r   r   "   sA    

L	
L / 	P 	Pr   r   c                      [         $ r   )RNG_STATE_TRACKERr?   r   r   get_rng_state_trackerrB   c   s    r   c                    SSK Jn  UR                  5       nUR                  5       nUR	                  5       nUR                  5       nUR                  5       nU (       a  U nU S-   X6-  -   U-   nO-[        R                  R                  SS5      nUS-   X6-  -   U-   n[        R                  5         [        R                  [        U5        [        R                  " U5        g )Nr   )fleet   i'  )paddle.distributedrD   get_hybrid_communicate_groupget_model_parallel_rankget_model_parallel_world_sizeget_stage_idget_pipe_parallel_world_sizenprandomrandintrA   r   r!   r=   r"   r&   )	r&   rD   hcgmp_rankmp_sizepp_rankpp_sizeglobal_seed
local_seeds	            r   model_parallel_random_seedrV   g   s    (

,
,
.C))+G//1G G..0GAX 11G;
ii''51 1_w'887B
,j9
KKr   c                    Uc+  [         R                  R                  R                  XX$XV5      $ [	        U[
        [        [        45      (       d  [        S5      e[	        U[        [
        45      (       a  US:X  a  U $ SUs=::  a  S::  d  O   [        S5      5       eUS;   d   [        S5      5       eUb   [        S5      5       eUS:X  a  S	OUn[        5       (       a)  [        R                  " U S
USU(       + SSSSSU5      u  pxU$ [	        U[        5      (       a)  UR                  S/:w  d  [        SUR                   35      e[        S0 [        5       D6n	[        U S/ SQS5        U	R!                  [         R"                  S9n
U	R%                  SSU
0S9  U	R!                  U R&                  S9nU	R!                  [(        R*                  R,                  R.                  SS9nU	R%                  SU /U
S.U/U/S.UU(       + US.S9  U$ )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. ]]

z4p argument should be a number(int|float) or Variabler   rE   z!p argument should between 0 and 1)downscale_in_inferupscale_in_trainzBmode argument should be 'downscale_in_infer' or 'upscale_in_train'z1unsupported axis when using random seed generatorrX   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)rd   stop_gradient)XSeed)re   Mask)r[   r\   r^   )rf   inputsrg   attrs)r_   )r"   nn
functionalr_   
isinstancefloatintr   	TypeErrorr    r   r   shaper   localsr   "create_variable_for_type_inferenceint32	append_oprd   r   VarDescVarTypeUINT8)r`   paxisrng_nametrainingmoder(   outmaskhelperr&   s              r   r_   r_      s.   N yy##++A$$MMa%h/00NOO !c5\""qAv;Q;G
#FGG;== zL@ = < ; <
 !%(< <$ 	
 !))L$
	 
a""177qc>YZ[ZaZaYbc  3&(3 s5y	
 88v||8Lfudm<77agg7F88,,&&,,D 9 
 	d+ ED62 !'<*.	 	 		
 
r   r   )g      ?NNTrY   N)r;   numpyrL   r"   r   paddle.baser   paddle.base.data_feederr   paddle.common_ops_importr   paddle.frameworkr   r   __all__r=   r   rA   rB   rV   r_   r?   r   r   <module>r      sf          < - 9
) ;P ;P| %& 4 
			Kr   