
    ΑiCQ                         S SK r S SKJr  S SKrS SKJr  S SKJr  S SKJ	r	J
r
  S SKJr  SSKJr  SS	KJr  SS
KJrJrJrJrJr  S/r " S S5      rS rS rS rS rS rS rg)    N)defaultdict)DistributedContext)'get_distributed_operator_impl_container)Programcore)	Parameter   )OperatorDistAttr)BACKWARD_ONLY_DIST_OPS)__no_shape_var_type__is_backward_opis_forward_op
is_loss_opis_optimize_oplod_tensor_blocking_queuec                   F    \ rS rSrSrSS jrS rS rS rS r	S r
S	 rS
rg)Partitioner)   a  
warning:: Partitioner is experimental and subject to change.

Partitioner convert a program into another program.
Given a serial program which has been auto completed with shard annotation, the Partitioner
convert the serial program into a "distributed" program. The Partitioner will  modify the serial
program in following two ways, which is also the major difference between serial and distributed program:
    1. partition op: replace a serial op into its corresponding dist op inferred from the shard annotation
    2. partition var: if a var is sharded, modify the shape of var according to its shard annotation

Partitioner is supposed to be call by the auto parallel framework, and not supposed to be directly called by user.
c                     [        U[        5      (       d  [        S[        U5       S35      eXl        X l        [        [        5      U l        SU l	        0 U l
        g)a4  
Args:
    dist_context (DistributedContext): used to access the distributed_attr of var & op, every Partitioner object could maintain its own DistributedContext member, and partition program base on that shard scenario.
    rank_id (int): global rank id to which the partitioned distributed program belong.
z(dist_context be DistributedContext, got  here N)
isinstancer   	TypeErrortype_dist_context_rank_idr   dict_serial2dist_varname_mapping_dist_varname_suffix_forward_op_id2forward_op)selfdist_contextrank_ids      s/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distributed/auto_parallel/static/partitioner.py__init__Partitioner.__init__7   s`     ,(:;;:4;M:NeT  *,7-
) %'!)+&    c                 z   [        U[        5      (       d  [        S[        U5       S35      eU R	                  U5      (       d  [        S5      eU R                  R                  nU R                  Ul	        U R                  Ul        Uc  S nOU R                  X5      nXTl        U R                  X5      u  nnUUU4$ )Nz.main_program be paddle.framework.Program, got r   z3Not all vars or ops are annotated in main program !)r   r   r   r   _is_valid_annotated_programRuntimeErrorr   dist_op_contextr   varname_mappingr   r#   partition_startup_programdst_startup_programpartition_main_program)r!   serial_main_programserial_startup_programparams_gradsr+   partitioned_startup_progpartitioned_main_progpartitioned_params_gradss           r$   	partitionPartitioner.partitionJ   s     -99@FYAZ@[[`a 
 //0CDDE 
 ,,<<*.*K*K'"&-- ")'+$'+'E'E#($ /G+ ''(;J	
!$ "$$
 	
r'   c                    [        U[        5      (       d  [        S[        U5       S35      e[        R
                  R                  5       nUR                  R                  5       Ul        UR                  5       nUR                  5       n0 n0 nUR                  5        Hc  nUR                  (       d   eUR                  U R                  -   n	XUR                  '   [        U R                  XEUR                  U	5      n
XU	'   Me     UR                  5       R                   GH  nUR                   R#                  5       n[%        U5      S:X  d   SUR                    S35       eX|S      U;   d   SUS    S35       eUR                   R'                  5       nUR)                  UR                   5        UR+                  US   X|S      5        UR-                  S	XgUS         5        UR/                  5         UR                  S
   nUR                  UR                  5       :X  d   eUR                   U:X  d   eUR1                  US   5      nU R                  R3                  U5      n[5        5       nUR6                  Ul        UR9                  UR                  UR:                  5        UR=                  UR                  UR:                  5        U R                  R?                  UU5        GM     U$ )Nz.dist_context be paddle.framework.Program, got r   r	   z6initializer should output only ONE variable, but got []r   ztry to initialize [z ] which is not a persistable varshape) r   r   r   r   paddle	framework_name_generatorcloneglobal_block	list_varspersistablenamer   _partition_varr   opsdescoutput_arg_nameslen	append_op	copy_from_rename_output	_set_attr_sync_with_cppvar get_tensor_dist_attr_for_programr
   process_meshset_output_dims_mappingdims_mappingset_input_dims_mappingset_op_dist_attr_for_program)r!   r0   r1   r3   	ref_blocktarget_block	var2shapetemp_varname_maprN   new_nametarget_shapeopoutput_varsnew_op_descnew_op
output_varoutput_var_attrop_attrs                     r$   r-   %Partitioner.partition_startup_programr   s    07<<@F\A]@^^cd  $*#3#3#;#;#= "2288: 	!0 (446	/<<>	 *335C??"?xx$";";;H)1SXX&)""ISXXxL #/h 6 )557;;B''224K{#q( H	QRS( $N3y@ %k!n%55UV@ '++557K!!"''*&&A 0Q @ !!KN#CD '') "%%b)F;;+"2"2"4444;;+---%))+a.9J""CCJO  '(G#2#?#?G ++!=!= **!=!= ;;FGLG <J ('r'   c           	         [         R                  R                  5       nUR                  R	                  5       Ul        U R
                  R                  nX4l        [        U R
                  R                  R                  5       H  nUR                  U   nUS:X  a  UR                  S   nOPUR                  UR                  S9nUR                  UR                  :X  d   eUR                  UR                   5        Xtl        U R%                  Xg5        M     SUl        [        U R
                  R                  R                  5       H  nUR)                  U5      n	U	R*                   Hz  n
U
R-                  5        Hc  nU
R/                  U5      [0        R2                  R4                  :X  d  M2  U
R7                  U5      nU
R9                  XR)                  U5      5        Me     M|     M     / nU H  u  pUR:                  U R<                  S   ;   d   eU R?                  XS5      nUc  SnO1UR:                  U R<                  S   ;   d   eU R?                  XS5      nURA                  UU45        M     X=4$ )zG
1. partition variables
2. replace local op with corresponding dist op
r   )
parent_idxN)!r<   r=   r   r>   r?   r   r+   dst_main_programrangeblock_statenblockblocks_create_blockrd   idx_set_forward_block_idxforward_block_idx
work_blockpartition_blockcurrent_block_idxblockrE   	all_attrs	attr_typer   AttrTypeBLOCK_block_attr_idrL   rC   r   _get_dist_var_by_serial_varappend)r!   r0   params_and_gradsr4   r+   rk   rU   rV   block_idrq   r[   	attr_namerelative_idpartitioned_params_and_gradspgdist_pdist_gs                     r$   r/   "Partitioner.partition_main_program   s<    !' 0 0 8 8 ://557 	- ,,<<+@(++77>>?C+2237Iax4;;A>4BB(33  C   !}}(8(888833I4O4OP)5&  9 @ 34/ d00<<CCDH)//9Eii!#I||I.$--2E2EE&(&7&7	&B%'B'B;'O "0   E (*$$DA66T>>qAAAA55!F yvv!B!B1!EEEE99a )//0@A % %BBr'   c           	         U R                   R                  nSn[        UR                  5       H  u  pV[	        U5      (       d  M  Un  O   US:X  a  [        UR                  5      n[        [        UR                  5      5       HK  nXT::  d  M
  UR                  U   U R                  UR                  U   R                  R                  5       '   MM     Sn[        UR                  5       GH^  u  pVU R                   R                  U5      n[        U5      (       aV  [        UR                  US-
     5      (       d   [	        UR                  US-
     5      (       a  UR                  (       d  US-  nUR                  R                  5        H  n	U	U R                  UR                      ;  d  U	U R                  UR"                     ;  d  M?  XR$                  -   n
UR'                  U	5      (       a  [)        U R                   UUU	U
5        U
U R                  UR"                     U	'   M     UR                  R+                  5        H  nUU R                  UR                      ;  d  UU R                  UR"                     ;  d  M?  XR$                  -   n
UR'                  U5      (       a  [)        U R                   UUUU
5        U
U R                  UR"                     U'   M     [        U5      (       d  UR                  (       aJ  UR-                  U5      u  p[/        X`R                   5      nUR0                  " U R                   40 UDUD6  GM^  [        U5      (       a|  UR-                  U5      u  p[3        X`R                   U R                  5      nU R                   R                  R4                  U   nUR6                  " U R                   40 UDUDSU0D6  GM  [9        U5      (       aY  UR-                  U5      u  p[3        X`R                   U R                  5      nUR6                  " U R                   40 UDUDS0 0D6  GMS  [;        SU 35      e   g )Nr;   r   r	   grad_var_to_varzEpartitioner only support forward and backward, optimize ops, but got )r   r+   	enumeraterE   r   rH   rf   r    rF   original_idget_op_dist_attr_for_programr   r   is_recomputeinput_arg_namesr   rm   rk   r   has_varrD   rG   prepare_context_get_dist_op_forward_implementforward_get_dist_op_backward_implementr   backwardr   NotImplementedError)r!   rU   rV   r+   last_fwd_op_idxrk   r[   appended_grad_timesop_dist_attrserial_input_varnamenew_varnameserial_output_varnamekinputskoutputsdist_op_forward_impldist_op_backward_implr   dist_op_opt_impls                     r$   ro   Partitioner.partition_block   s8   ,,<< /GC"~~"% 0
 b !)--0OY]]+,C% MM#& ..MM#&++779 -   /GC--JJ2NLb!!immC!G455immC!G455#00'1,' )+(?(?(A$(<<!33 ,<<Y]]KL -/H/HH   !(()=>>& ..%(0' $ 55immD,) )B2 *,)A)A)C%)<<!33 -<<Y]]KL .0I0II   !(()>??& ..%(1' $ 55immD-) *D2 R  L$=$=$3$C$CB$G!'E**($ %,,&&*15=  ##$3$C$CB$G!(G**D,J,J)% &&66FF+  
 &..&&  )/:	  ##$3$C$CB$G!#B**D,J,J$  !))&&  )"-	 *[\^[_` E 0r'   c                    UR                  5       R                  nUR                  5       nU Vs/ s H  o@R                  R	                  U5      PM     nnU Vs/ s H4  nUR
                  [        ;  d  M  U R                  R                  U5      PM6     nn[        S U 5       5      n[        S U 5       5      n	U=(       a    U	$ s  snf s  snf )Nc              3   (   #    U  H  oS Lv   M
     g 7fN .0	dist_attrs     r$   	<genexpr>:Partitioner._is_valid_annotated_program.<locals>.<genexpr>u  s       
3@iT!=   c              3   (   #    U  H  oS Lv   M
     g 7fr   r   r   s     r$   r   r   x  s      !
3AiT!>r   )	r@   rE   rA   r   r   r   r   rO   all)
r!   programrE   vars_r[   op_dist_attrsrN   var_dist_attrsall_ops_annotatedall_vars_annotateds
             r$   r)   'Partitioner._is_valid_annotated_programh  s    ""$((!!#JM
JMB;;B?# 	 

 
 55 ED??D 	 
    
3@ 
 
 ! !
3A!
 
 !7%77!

s   $CC
5C
c                     UR                   R                  nUR                  U   nU R                  U   UR                     nUR                  U5      (       d   eUR                  U5      $ r   )rq   rk   ri   r   rC   r   rN   )r!   
serial_varr4   rz   	block_idxrV   dist_var_names          r$   rw   'Partitioner._get_dist_var_by_serial_var~  sk     $$((	,33I>99(COO
 ##M2222..r'   )r   r   r    r   r   N)r   )__name__
__module____qualname____firstlineno____doc__r%   r6   r-   r/   ro   r)   rw   __static_attributes__r   r'   r$   r   r   )   s1    ,&&
PA(F7Crxt8,	/r'   r   c                    U R                   nUR                  nUR                  R                   nU/ :X  a  U$ [        U5      [        U5      :X  d   SU SU S35       e/ n[	        [        U5      5       H}  nX&   S:X  d  X6   S:X  a  UR                  X&   5        M(  X&   XCU      -  S:X  d*   SX&    SXCU       SU R                   S	U S	U S	U 35       eUR                  X&   XCU      -  5        M     U$ )
Nzvariable shape [z] and dim_mapping [z] is NOT match !r;   r   z$un-event partition: var_shape[idx]=[z], mesh[z], z, )r:   rR   rP   rH   rf   rx   rC   )rN   r   	var_shapemappingmesh	new_shaperk   s          r$   _get_dist_shaper     sI   		I$$G!!''D"}y>S\) 
9+%8	AQR) IS^$>R7<2#5Y^,>D$66!; 6y~6Fht\_T`OaNbbefifnfneooqr{q||~  @D  E  EG  HO  GP  Q; Y^tCL/AAB % r'   c                 X   0 nUR                   US'   UR                  US'   UR                  US'   UR                  US'   UR                  US'   [        SUUR                  UUUR                  UR                  UR                  UR                  UR                  UR                  S.
UD6nU$ )N	trainableoptimize_attrregularizerdo_model_average	need_clip)
rq   r   rC   r:   dtype	lod_level
error_clipstop_gradientis_databelong_to_optimizerr   )r   r   r   r   r   r   r   r   r   r   r   r   r   )r"   src_var	dst_blockdst_varname	dst_shapecopied_kwargsparams          r$   _partition_parameterr     s    
 M!(!2!2M+%,%:%:M/"#*#6#6M- (/(@(@M$%!(!2!2M+ \\mm##%%++#77 E Lr'   c                     UR                  UR                  UUUR                  UR                  UR                  UR
                  UR                  UR                  UR                  S9
nU$ )N)
r   rC   r:   r   r   rB   r   r   r   r   )	
create_varr   r   r   rB   r   r   r   r   )r"   r   r   r   r   rN   s         r$   _partition_intermediate_varr     sh     

\\mm##''%%++#77  C Jr'   c                    UR                  U5      nUR                  [        ;   a,  [        USS5      nUR	                  UR                  UUSS9nSnOLU R                  U5      n	[        XY5      n[        U[        5      (       a  [        XX$U5      nO[        XX$U5      n[        R                  " U R                  U5      5      n	U	c   eU R                  Xy5        U$ )z&
partition include: split + replicate
rB   FT)r   rC   rB   r   N)rN   r   r   getattrr   rO   r   r   r   r   r   copydeepcopy set_tensor_dist_attr_for_program)
r"   	src_blockr   src_varnamer   r   persistnew_varrZ   r   s
             r$   rD   rD     s     mmK(G||,,'=%8&&	 ' 
  AA'J	&w:gy))*y|G 2y|G 55g>I    11'Er'   c                 L   UR                   nU R                  R                  5       UR                  ;   an  UR                  U R                  R                  5          nX$   nUR	                  U5      n[        UR                  5      nUR                  UR                  5      nU$ U R                  [        ;   aS  UR	                  U 5      n	U	R                  S:  d   e[        U	R                  5      R                  U	R                  5      nU$ [        S5      n
U
R                  S5      $ )Nr   default)r+   rF   r   grad_op_id_to_op_idr   r   	impl_typeget_implimpl_idxr   r   )backward_opr"   forward_op_id2forward_opr+   forward_op_id
forward_opforward_op_dist_attrdist_op_impl_containerdist_op_implr   dist_ops              r$   r   r     s$    #22O##%)L)LL';;((*
 .<
+HH 
 "I **"
 .66 ))
  11#@@M$$)))>""

(<((
) 	 5i@GAr'   c                     UR                  U 5      n[        UR                  5      nUR                  UR                  5      nU$ r   )r   r   r   r   r   )r   r"   r   r   r   s        r$   r   r     sD    99*EID *2293E3EFLr'   )r   collectionsr   r<   4paddle.distributed.auto_parallel.static.dist_contextr   8paddle.distributed.auto_parallel.static.operators.commonr   paddle.frameworkr   r   paddle.staticr   dist_attributer
   operators.commonr   utilsr   r   r   r   r   __varname_not_in_block__r   r   r   r   rD   r   r   r   r'   r$   <module>r      sj     #  + # , 4  88 ^/ ^/B.:&$NBr'   