
    Αiz                       S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKJrJ	r	J
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  S	S
KJr  \(       a  S SKJrJr  S SKJr  S SKJ r    " S S\
5      r!/ r"S r#S r$S r% " S S5      r&\&" 5       r'\RP                     SS j5       r) " S S\5      r* SS jr+S r,          SS jr-g)    )annotationsN)TYPE_CHECKINGAny	TypedDict)	framework)PyLayer)EagerParamBase)get_rng_state_tracker)corein_dynamic_mode   )logger)CallableSequence)NotRequired)
Sequentialc                  .    \ rS rSr% SrS\S'   S\S'   Srg)	_Ctx,      intsegmentszNotRequired[bool]preserve_rng_state N)__name__
__module____qualname____firstlineno__r   __annotations____static_attributes__r       l/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distributed/fleet/recompute/recompute.pyr   r   ,   s    #--r!   r   c                    [         R                  " U R                  5      n[        SU R                  U R
                  U R                  U R                  S.UD6nU R                  U5        U$ )N)shapedtype	trainablenamer   )	copydeepcopy__dict__r	   r$   r%   r&   r'   _share_buffer_to)paramstate	new_params      r"   _varbase_helpr/   4   s_    MM%..)E kkkk//ZZ	
 I 
9%r!   c                |   / nU  GH  n[        U[        R                  R                  5      (       dQ  [	        U5      [
        Ld,  [        US   [        R                  R                  5      (       d  UR                  U5        M~  [        U[        5      (       a  UR                  [        U5      5        M  [	        U5      [
        L a  / nU H  n[        U[        R                  R                  5      (       d   e[        U[        5      (       a  UR                  [        U5      5        M_  UR                  5       nUR                  Ul
        UR                  U5        M     UR                  [        U5      5        GMw  UR                  5       nUR                  Ul
        UR                  U5        GM     [        U5      $ )Nr   )
isinstancer   eagerTensortypetupleappendr	   r/   detachstop_gradient)inputsoutinp
detach_inpitmp_ixs          r"   detach_variabler@   A   sA   
C#tzz0011IU"*SVTZZ=N=N*O*O JJsOc>**JJ}S)*9J!!TZZ%6%67777a00%%mA&67HHJE*+//E'%%e,  JJuZ()JJL++

1= > :r!   c                   / nU  H  n[        U[        R                  5      (       a  UR                  UR                  5        M?  [        U5      [        L d  MS  U H?  n[        U[        R                  5      (       d  M$  UR                  UR                  5        MA     M     [        U5      (       a  [        R                  " S5        g g )Nz[Recompute]: None of the inputs to current recompute block need grad, therefore there is NO need to recompute this block in backward !)
r1   paddler3   r6   r8   r4   r5   allr   warning)r9   necessary_for_each_inputinput_r=   s       r"   check_recompute_necessaryrG   e   s    !ffmm,,$++F,@,@A&\U"a//,33AOOD 	  #$$O	
 %r!   c                  *    \ rS rSrS rS rS rS rSrg)CustomStatesManagerv   c                     SU l         SU l        g)__init__Ncustom_get_state_funccustom_set_state_func)selfs    r"   rL   CustomStatesManager.__init__y   s    %)"%)"r!   c                <    SnU R                   b   U5       eXl         g Nz=The custom_state_manager does not support duplicate settings.)rN   )rP   rN   
assert_msgs      r"   set_custom_get_state_func-CustomStatesManager.set_custom_get_state_func~   )    K 	 ))1=:=1%:"r!   c                <    SnU R                   b   U5       eXl         g rS   )rO   )rP   rO   rT   s      r"   set_custom_set_state_func-CustomStatesManager.set_custom_set_state_func   rW   r!   rM   N)	r   r   r   r   __doc__rL   rU   rY   r    r   r!   r"   rI   rI   v   s    *
;;r!   rI   c              #    #    [         R                  " 5       n[        5       R                  5       n[         R                  " U 5        [        5       R                  U5        [        R                  R                  5       n	[        R                  " 5       n
[        R                  R                  U5        [        R                  " U5        Ub  Uc   eUc   eU" 5       nU" U5         S v   [         R                  " U5        [        5       R                  U5        [        R                  R                  U	5        [        R                  " U
5        Ub	  U" W5        g g ! [         R                  " U5        [        5       R                  U5        [        R                  R                  U	5        [        R                  " U
5        Ub	  U" W5        f f = f7fN)rB   get_rng_stater
   get_states_trackerset_rng_stateset_states_trackernprandom	get_stategetstate	set_statesetstate)	rng_statetrackernumpy_staterandom_statecustom_staterN   rO   orig_rng_stateorig_rng_trackerorig_numpy_stateorig_random_stateorig_custom_states               r"   switch_rng_state_trackerrr      sf     ))+N,.AAC
#..w7yy**,)II$
OOL!$000$00013l+	5^,223CD
		,-)*#!"34 $ 	^,223CD
		,-)*#!"34 $s    C!G$E (A1GA2GGc                  4    \ rS rSr\S 5       r\S 5       rSrg)RecomputeFunction   c                \	   Xl         X l        X0l        Xpl        U R                  (       a  [        R
                  " 5       U l        [        5       R                  5       U l	        [        R                  R                  5       U l        [        R                  " 5       U l        U" 5       U l        X@l        XPl        [&        R(                  " 5       nUR*                  [,        R.                  R0                  :X  a  SOSU l        UR*                  [,        R.                  R4                  :X  a  SU l        ObUR*                  [,        R.                  R8                  [,        R.                  R0                  4;   a  SU l        O[;        SUR*                   35      eUR<                  S:X  a  SU l        O0UR<                  S;   a  SU l        O[;        S	UR<                   35      eURA                  5       u  U l!        U l"        [        RF                  " 5          U" U0 UD6n	S S S 5        / U l$        / U l%        [M        [O        U5      5       V
s/ s H  n
SPM     sn
U l(        / n[S        U5       GH  u  p[        RT                  " U5      (       a  XR                  ;   aK  [,        RV                  " 5       (       a  URY                  5       OUR[                  5       nUR]                  U5        UR_                  U5        U RJ                  R_                  U5        U RH                  R_                  S 5        M  [a        U5      [b        L Ga'  XR                  ;  d
   S
U 35       eU Vs/ s H  n[        RT                  " U5      PM     nn[e        U5      (       a  U Vs/ s H  oRf                  PM     nn[e        U5      (       d  [i        U5      (       a  [;        S5      eUR_                  U5        U RJ                  R_                  U5        SU RP                  U'   U RH                  R_                  S 5        GM  [i        U5      (       a  [;        S5      eU RH                  R_                  U5        GM  U RH                  R_                  U5        GM     U Rj                  " U6   W	$ ! , (       d  f       GN= fs  sn
f s  snf s  snf )NFTO2O1zunsupported amp level: float16bfloat16float32r{   zunsupported amp dtype: z;offload_indices should not contain tensor tuple in positionzJRecompute receive a tuple containing tensor holds different stop gradient.zHRecompute receive a tuple containing tensor and non-tensor at same time.)6run_functionr   offload_indiceskwargsrB   r^   fw_rng_stater
   r_   fwd_rng_state_trackerrb   rc   rd   fwd_numpy_statere   fwd_random_statefwd_custom_staterN   rO   r   _dygraph_tracer
_amp_levelr   AmpLevelO0is_fw_autocastrw   	amp_levelrx   
ValueError
_amp_dtype	amp_dtype_get_amp_op_listamp_white_listamp_black_listno_gradr9   tensor_indicesrangelenduplicate_tensor	enumerate	is_tensoris_compiled_with_cuda
pin_memorycpur+   r6   r4   r5   rC   r8   anysave_for_backward)ctxr}   r   r~   rN   rO   argsr   traceroutputs_tensor_inputsr=   argcpu_arga
is_tensorstensors_stop_gradients                     r"   forwardRecomputeFunction.forward   s    (!3-
 !!%335C%'::< % #%))"5"5"7C#)??#4C #8#:C (=%(=% **,&&$--*:*::E 	  0 00 CM4==#3#3T]]5E5E"FF CM6v7H7H6IJKK	)%CM"99&CM6v7H7H6IJKK171H1H1J.C.^^"D3F3G  
/4SY/?@/?!/?@oFA$$+++  5577 ( WWY 
 ,,S1$$S)""))!,

!!$'ce# 3 33 QRSQTU3 <??3af..q13
?z??FI,Jc__c),J455#-; ; )h  "((-&&--a0.2C((+JJ%%d+__$b  JJ%%c*

!!#&Q &T 	}-i   A$ @ -Ks   	R=R R$R)
Rc           
     *
   [         R                  R                  R                  5          [	        U R
                  5      nU R                  nU R                  nU R                  5       n[        U5       Hy  u  pgX`R                  ;   a9  XV   R                  [         R                  R                  R                  5       5      OXV   X''   X`R                  ;   d  Md  XV   R                  X'   l        M{     [        R                  " 5       nSUl        U R"                  (       a  [%        U R&                  U R(                  U R*                  U R,                  U R.                  U R0                  U R2                  5         [         R4                  R7                  U R8                  U R:                  U R<                  U R>                  U R@                  S9   [C        [E        U5      5      n	U RF                  " U	0 U RH                  D6n
S S S 5        S S S 5        O[         R4                  R7                  U R8                  U R:                  U R<                  U R>                  U R@                  S9   [C        [E        U5      5      n	U RF                  " U	0 U RH                  D6n
S S S 5        [K        W
[L        RN                  RP                  5      (       a  U
4n
[S        U
5      [S        U5      :X  d   e/ n/ n[U        [S        U
5      5       Hk  n[K        X   [L        RN                  RP                  5      (       d  M0  X   R                  (       a  ME  URW                  X   5        URW                  X   5        Mm     [S        U5      S:X  a  [Y        S5      e[         R4                  R7                  SS9   [         RZ                  R]                  X5        S S S 5        / n[        W	5       H  u  p~[K        U[L        RN                  RP                  5      (       a!  URW                  UR_                  5       5        MO  [a        U5      [D        L d  Mc  XG   (       d  Mn  [c        S U 5       5      (       a  URW                  S 5        M  URW                  [E        S U 5       5      5        M     [e        5       (       a  [E        U5      nO[	        U5      nUsS S S 5        $ ! , (       d  f       GN= f! , (       d  f       GNH= f! , (       d  f       GNZ= f! , (       d  f       GN@= f! , (       d  f       g = f)	NTenablecustom_white_listcustom_black_listlevelr%   r   zHnone of output has requires_grad=True, this recompute() is not necessaryF)r   c              3  8   #    U  H  oR                   v   M     g 7fr]   )r8   .0r=   s     r"   	<genexpr>-RecomputeFunction.backward.<locals>.<genexpr>  s     8Cq??Cs   c              3  @   #    U  H  oR                  5       v   M     g 7fr]   )
_grad_ivarr   s     r"   r   r     s     *G3a<<>>3s   )3rB   basedygraphguardlistr9   r   r   saved_tensorr   r~   tor   _current_expected_placer8   r   	_has_gradr   rr   r   r   r   r   r   rN   rO   amp	auto_castr   r   r   r   r   r@   r5   r}   r   r1   r   r2   r3   r   r   r6   RuntimeErrorautogradbackwardr   r4   rC   r   )r   r   r9   r   r   tensorsr=   idxr   detached_inputsr   forward_outputs_with_gradbackward_inputs_with_gradgradsr;   s                  r"   r   RecomputeFunction.backward#  s   [[  &&( #**%F //N"33&&(G#N3
 /// JMM--EEG !  +++07
0H0HFK- 4 ..0F#F %%,((11++,,,,1111 JJ(("11*-*<*<*-*<*<!mm!mm )  '6eFm&DO!..N3::NG ( ZZ))--&)&8&8&)&8&8---- *  '6eFm&DO!..N3::NG '4::#4#455"*w<3t9,,, )+%
 )+%3w<(wz4::+<+<==#J444-44WZ@-44TW= ) ,-2"^ 
 %%U%3((- 4
 E%o6c4::#4#455LL!12#Y%',<,A,A8C888T* U*G3*G%GH 7   eUQ )(H  ( F 43e )(s   B)TBT3AS1R<8S AT1S BT#T8A!T S29A4T1T<A6T<
SS
S	T 
S/	*T2
T	<T
Tr   N)r   r   r   r   staticmethodr   r   r    r   r!   r"   rt   rt      s-    j jX i ir!   rt   c                X  ^ ^^^^^^^^^^^^^^^^^^ T(       Ga%  [         R                  " 5       nUR                  S5      (       a  [         R                  " 5       mOSU;   a  [         R                  " 5       mOvSU;   a  [         R                  " 5       mOZUR                  S5      S   [         R                  R                  5       ;   a  [         R                  " U5      mO[        SU S35      e[        5       R                  5       m[        R                  R                  5       m[        R                  " 5       mT" 5       m[        R                   " 5       nUR"                  [$        R&                  R(                  :X  a  SOS	mUR"                  [$        R&                  R*                  :X  a  S
mODUR"                  [$        R&                  R,                  [$        R&                  R(                  4;   a  SmUR.                  S:X  a  SmOUR.                  S;   a  SmUR1                  5       u  mm " S S5      m[2        R4                  " 5       m/ mUU4S jnUUUUUUUU UUUUUUUUUU4S jn	[         R6                  R9                  X5         T " T0 TD6n
SSS5        U
$ ! , (       d  f       W
$ = f)zw
recompute without reentrant, that means use hook to implement the recompute function rather than re-entrant autograd.
zgpu:r   zxpu::r   z;Recompute with RNG preserve is not support current device: .FTrw   rx   ry   rz   r{   c                      \ rS rSrSrg)9_recompute_without_reentrant.<locals>.Intermediate_Holderi  r   N)r   r   r   r   r    r   r!   r"   Intermediate_Holderr     s    r!   r   c                `   > T" 5       nTR                  [        R                  " U5      5        U$ r]   )r6   weakrefref)r?   resr   holder_lists     r"   pack*_recompute_without_reentrant.<locals>.pack  s'    !#7;;s+,
r!   c           
       >^ Sm[        T5      S:X  Ga  UUU4S jnS nT(       a  [        TTTTTT	T
5         [        R                  " S5         [        R                  R                  TTTTTS9   [        R                  R                  X5         T" T0 TD6  S S S 5        S S S 5        S S S 5        S S S 5        Oy[        R                  " S5         [        R                  R                  TTTTTS9   [        R                  R                  X5         T" T0 TD6  S S S 5        S S S 5        S S S 5        U T;  a  [        S5      eTR                  U 5      $ ! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       Nc= f! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       N= f)Nr   c                @  > TS-  mTTS-
     " 5       c  g U c  S TTTS-
     " 5       '   g [        U S5      (       d  U R                  b  U TTTS-
     " 5       '   g U R                  5       (       a   [        R                  R                  U 5      nOp[        R                  R                  U R                  U R                  U R                  S-   [        R                  R                  R                  U R                  5      nU R                  U5        UTTTS-
     " 5       '   g )Nr   	main_gradcpy)hasattrgradis_distr   r2   r3   r%   r$   r'   VarDescVarTypeDENSE_TENSORpersistable_unsafe_share_buffer_to)inner_x
tmp_tensorr   storageunpack_counters     r"   
inner_pack@_recompute_without_reentrant.<locals>.unpack.<locals>.inner_pack  s   !#~124<?AEGK(:;=>7K00GLL4LAHGK(:;=>  ((%)ZZ%6%6w%?
%)ZZ%6%6#MM#MM#LL50 LL00==#//&
 33J?AKGK(:;=>r!   c                    [        S5      e)Nz*An unexpected backward called on a tensor!)	Exception)r   s    r"   inner_unpackB_recompute_without_reentrant.<locals>.unpack.<locals>.inner_unpack  s     LMMr!   Tr   zaNot supported to retrieve a tensor saved by autograd multiple times that is no need to recompute.)
r   rr   rB   set_grad_enabledr   r   r   saved_tensors_hooksr   pop)r?   r   r   r   r   r   r   r   r   rN   rO   functionfw_cuda_rng_statefwd_cuda_rng_state_trackerr   r   r   r   r   r   r   r   s      @r"   unpack,_recompute_without_reentrant.<locals>.unpack  s   w<14N ",)2'((-- ++D1JJ((-*8*8'' )  OO77" d-f-	 2 0 ++D1JJ((-*8*8'' )  OO77" d-f-	 2 Gs  {{1~5   21 @   21s   F	"E81 E'	E	E'"E8*F	"F<3 F+	FF+$F<
E$ E''
E51E88
F	F		
F
F($F++
F9	5F<<
G
N)rB   
get_device
startswithget_cuda_rng_stater^   splitdeviceget_all_custom_device_typer   r
   r_   rb   rc   rd   re   r   r   r   r   r   r   rw   rx   r   r   r   WeakKeyDictionaryr   r   )r   rN   rO   r   r   r   
cur_devicer   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s   ``````     @@@@@@@@@@@@@r"   _recompute_without_reentrantr     s     &&(
  (( & 9 9 ;j  & 4 4 6z! & 4 4 6S!!$}}779: !' 4 4Z @Mj\YZ[  "#668 	# ))--/!??,02&&(F$//4==3C3CCUNDMM,,,				t}}//1A1AB	B	I%				5	5	%+%<%<%>"NN  '')GK
N N N` 
	,	,T	:D+F+ 
; N 
;	: Ns   	J
J)c                   UR                  SS5      nUR                  SS5      n[        R                  c  [        R                  b   eSS jnSS jnO [        R                  n[        R                  n[	        5       (       d  SSKJn  U" U 5      " U0 UD6$ [        R                  " 5       R                  (       a>  [        U5      nUR                  [        UR                  5       5      5        [        U5        U(       Ga  UR                  S/ 5      n	/ n
[        U [        R                   R"                  5      (       a!  [$        R&                  " U R(                  5      nO[$        R&                  " U 5      nUR*                  " U0 UD6nUR-                  5         [/        UR0                  R                  5       UR2                  R                  5       5       H  u  pUR4                  UR6                  :X  a  U
R                  U5        M2  UR4                  UR8                  UR:                  4;   a  U
R=                  U5        Mk  UR4                  UR>                  :X  a!  U
R                  UR                  5       5        M  UR4                  UR@                  :X  a  [C        S	5      e[C        S
5      e   [D        RF                  " U UU	UU/U
Q76 $ [I        U UUU/UQ70 UD6$ )al  
recompute intermediate activations to save then memory.

Parameters:
    function(paddle.nn.Layer): layer of sequence of layers that describes part of forward pass of the model
          whose intermediate activations will be released to save memory in forward stage and will be recomputed
          in backward stage for gradient calculation.
    *args(Tensor): inputs to the function.
    **kwargs(Dict): Kwargs should only contain two kinds of key-value params, the one is part of function's key-value params,
                    and the other contains 'preserve_rng_state' and 'use_reentrant'. the key-value pair of preserve_rng_state,
                    which is used to indicate whether to save the forward rng. If it is True, then the last forward rng value
                    will be restored when the forward recalculation of backpropagation is performed, its default value is True.
                    the key-value pair of use_reentrant is used to indicate which implementation of recompute you will be used.
                    'use_reentrant=True' means to use the PyLayer implementation of recompute, 'use_reentrant=False' means to
                    use the Hook implementation of recompute, its default value is True.
Returns:
    Output of function on args.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:DISTRIBUTED, env:GPU)
        >>> import paddle
        >>> from paddle.distributed.fleet.utils import recompute
        >>> import random
        >>> paddle.seed(2023)
        >>> def get_fc_block(block_idx, input_size, is_last=False):
        ...     block_name = "block_" + str(block_idx)
        ...     block = paddle.nn.Sequential(
        ...         (block_name + "_fc_0", paddle.nn.Linear(input_size, input_size, bias_attr=False)),
        ...         (block_name + "_dropout", paddle.nn.Dropout(p=0.5)),
        ...         (block_name + "_relu_1", paddle.nn.ReLU()),
        ...         (block_name + "_fc_1", paddle.nn.Linear(input_size, input_size, bias_attr=False)),
        ...         (block_name + "_relu_2", paddle.nn.ReLU()),
        ...     )
        ...     if is_last:
        ...         block.add_sublayer(
        ...             block_name + "_fc_2",
        ...             paddle.nn.Linear(
        ...                 input_size, 1, bias_attr=False
        ...             )
        ...         )
        ...     else:
        ...         block.add_sublayer(
        ...             block_name + "_fc_2",
        ...             paddle.nn.Linear(input_size, input_size, bias_attr=False)
        ...         )
        ...     return block

        >>> class Naive_fc_net(paddle.nn.Layer):
        ...     def __init__(self, input_size=10,
        ...                 recompute_blocks=[1, 3],
        ...                 recompute_kwargs={}):
        ...         super().__init__()
        ...         self.recompute_blocks = recompute_blocks
        ...         self.recompute_kwargs = recompute_kwargs
        ...         self.runfunc0 = get_fc_block(0, input_size, is_last=False)
        ...         self.runfunc1 = get_fc_block(1, input_size, is_last=False)
        ...         self.runfunc2 = get_fc_block(2, input_size, is_last=False)
        ...         self.runfunc3 = get_fc_block(3, input_size, is_last=False)
        ...         self.runfunc4 = get_fc_block(4, input_size, is_last=True)
        ...         self.total_func = [self.runfunc0, self.runfunc1, self.runfunc2, self.runfunc3, self.runfunc4]
        ...     def forward(self, inputs):
        ...         nums = len(self.total_func)
        ...         for i in range(nums):
        ...             if i in self.recompute_blocks:
        ...                 inputs = recompute(self.total_func[i], inputs, **{"preserve_rng_state": True})
        ...             else:
        ...                 inputs = self.total_func[i](inputs)
        ...         return inputs

        >>> def run_model(cuda_state, recompute_block=[], recompute_kwargs={}):
        ...     gen = paddle.seed(10)
        ...     gen.manual_seed(10)
        ...     random.seed(10)
        ...     if cuda_state:
        ...         paddle.set_cuda_rng_state(cuda_state)
        ...     batch_size, input_size = 1, 10
        ...     model = Naive_fc_net(
        ...         input_size,
        ...         recompute_blocks=recompute_block,
        ...         recompute_kwargs=recompute_kwargs)
        ...     optimizer = paddle.optimizer.SGD(learning_rate=0.01, parameters=model.parameters())
        ...     loss_ = []
        ...     param_ = []
        ...     grad_ = []
        ...     for _ in range(5):
        ...         x = paddle.rand(shape=[batch_size, input_size], dtype="float32")
        ...         y_pred = model(x)
        ...         loss = y_pred.mean()
        ...         loss_.append(loss.item())
        ...         loss.backward()
        ...         optimizer.step()
        ...         param_.append(model.parameters()[9])
        ...         grad_.append(model.parameters()[3]._grad_ivar())
        ...         optimizer.clear_grad()
        ...     return loss_, param_, grad_

        >>> cuda_state = paddle.get_cuda_rng_state()
        >>> # without recompute
        >>> loss_ref, param_ref, grad_ref = run_model(
        ...     cuda_state, recompute_block=[]
        ... )

        >>> loss, param, grad = run_model(cuda_state, recompute_block=[1, 2])
        >>> print("normal_loss: {}, recompute_loss: {}".format(loss_ref, loss))
        >>> # The result of the recompute_loss should be the same as the normal_loss.
        normal_loss: [0.0018744759727269411, 0.0, 0.035971127450466156, 0.0, 0.0], recompute_loss: [0.0018744759727269411, 0.0, 0.035971127450466156, 0.0, 0.0]

r   Tuse_reentrantc                    g r]   r   r?   s    r"   <lambda>recompute.<locals>.<lambda>      tr!   c                    g r]   r   r  s    r"   r  r    r  r!   r   )	recomputer~   zCurrently, keyword-only arguments are not supported when you want to send kwargs(dict parameter) to function with use_reentrant=True.zUnknown parameter kind.r]   )%r   custom_state_managerrN   rO   r   *paddle.distributed.auto_parallel.interfacer  r   r   r   r   extendvaluesrG   r1   rB   nnLayerinspect	signaturer   bindapply_defaultszip	arguments
parameterskindVAR_POSITIONALPOSITIONAL_ONLYPOSITIONAL_OR_KEYWORDr6   VAR_KEYWORDKEYWORD_ONLYr   rt   applyr   )r   r   r   preserver  rN   rO   static_auto_recompute
check_argsr~   
input_args
dyfunc_sig
bound_argsr   r,   s                  r"   r  r  #  s}   ` zz.5H JJ5M119#99AAA 3 3 4 J J 4 J J	
 %X.???  ",,$Z
$v}}/0!*- **%6;
h		00 **8+;+;<J **84J__d5f5
!!#  ''):+@+@+G+G+I
JC zzU111!!#&%%++   !!#&u000!!#**,/u111  \  !!:;;#
& !&&!!
 
 	
 ,!!	

 
 
 	
r!   c                   U R                  SS5      nU R                  SS5      nS n[        U[        R                  R                  5      (       a  [        UR                  5       5      n[        U5      U-  nSn[        SXtS-
  -  U5       H"  n	X-   S-
  n[        U" XU5      /UQ7SU0UD6nM$     U" US-   [        U5      S-
  U5      " U6 $ )a  
recompute intermediate activations to save the memory for 'Sequential' models. use 'ctx' to transmit some context params, it is similar to 'recompute_hybrid' API.

Parameters:
    ctx(dict): include 'segments' and  'preserve_rng_state' keys, the key 'segments' (int, default 1), represents the number of chunks to create in the model,
               the key 'preserve_rng_state' (bool, optional, default=True) indicate whether to save the forward rng. If it is True, then the last forward rng value will be
               restored when the forward recalculation of backpropagation is performed.
    functions(paddle.nn.Sequential): layer of sequence of layers that describes part of forward pass of the model
          whose intermediate activations will be released to save memory in forward stage and will be recomputed
          in backward stage for gradient calculation.
    *args(Tensor): inputs(tuple) to the function.
    **kwargs(Dict): inputs(dict) to the function.

Returns:
    Output of function on args and kwargs.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env:DISTRIBUTED)
        >>> import paddle
        >>> from paddle.incubate.distributed.fleet import recompute_sequential
        >>> input = paddle.ones(shape=[8, 10])
        >>> model = paddle.nn.Sequential(paddle.nn.Linear(10, 10), paddle.nn.Linear(10, 2))
        >>> output = recompute_sequential({'segments' : 1}, model, input)

r   r   r   Tc                   ^ ^^ U UU4S jnU$ )Nc                J   > [        TTS-   5       H  nTU   " U 5      n M     U $ )Nr   )r   )inputr=   beginendfuncss     r"   do_run7recompute_sequential.<locals>._run_func.<locals>.do_run  s*    5#'*a +Lr!   r   )r'  r(  r)  r*  s   ``` r"   	_run_func'recompute_sequential.<locals>._run_func  s    	
 r!   r   )
getr1   rB   r  r   r   childrenr   r   r  )
r   	functionsr   r   r   r   r,  segment_sizer(  r'  s
             r"   recompute_sequentialr3    s    B wwz1%H!5t< )VYY1122++-.	y>X-L
Cq,Q,7F"Q&e),

  2
 	
 G S1Wc)nq0)<dCCr!   )NNN)T)
r   r   r1  z)Sequential | Sequence[Callable[..., Any]]r   r   r   r   returnr   ).
__future__r   
contextlibr(   r  rc   r   typingr   r   r   numpyrb   rB   r   paddle.autogradr   paddle.base.frameworkr	   =paddle.distributed.fleet.meta_parallel.parallel_layers.randomr
   paddle.frameworkr   r   utils.log_utilr   collections.abcr   r   typing_extensionsr   	paddle.nnr   r   __all__r/   r@   rG   rI   r	  contextmanagerrr   rt   r   r  r3  r   r!   r"   <module>rC     s    #      0 0    # 0 3 #2-$.y .
 
!H
"; ;. +,   !5 !5HX X~ 	Pfx
v:D	:D8:D :D 	:D
 	:Dr!   