+
    –°³jn– ã                   ó
  € ^ RI t ^ RIt^ RIt^ RIt^ RIt^ RIHtHt ^ RIH	t	H
t
 ^ RIHt ^ RIHt ^ RIHt ^ RIHt ^ RIHtHtHtHtHt ^ RIt^ RIHt ^ R	IHt ^ R
IH t H!t! ^ RI"H#t# ^ RI$H%t% ^RI&H't'H(t(H)t) ^RI*H+t+H,t,H-t-H.t. ^RI/H0t0H1t1H2t2 . RSOt3]Ph                  ! ]54      t6 ! R R]7]4      t8]8Pr                  t9]8Pt                  t:]8Pv                  t;]8Px                  t<]8Pz                  t=]8P|                  t>]8P~                  t?]8P€                  t@]8P‚                  tA]8P„                  tB]8P†                  tC]8Pˆ                  tD].! ^ 4      tE]9tF]:tG];tH]BtI]P”                  ! R4      tK ! R R]4      tL]R R l4       tMRTR  R! lltN ! R" R#]4      tORTR$ R% lltPRTR& R' lltQR( R) ltR ! R* R]O4      tS ! R+ R,]S4      tT ! R- R]S4      tU ! R. R]S4      tVR/ R0 ltWR1 R2 ltXRUR3 R4 lltYR5 R6 ltZR7 R8 lt[R9 R: lt\R; R< lt] ! R= R]O4      t^] ! R> R?4      4       t_ ! R@ RA]4      t` ! RB RC]^4      ta ! RD R]a4      tbRVRE ltcRWRF RG lltd ! RH R]a4      te ! RI R]a4      tf ! RJ R]a4      tg ! RK R]a4      thRL RM ltiRN RO ltjRP tkRQ RR ltlR# )Xé    N)ÚABCÚabstractmethod)ÚCounterÚdefaultdict)ÚCallable)Ú	dataclass)ÚEnum)Ú	lru_cache)ÚAnyÚcastÚLiteralÚ
NamedTupleÚProtocol)ÚOptimizedModule)Ú
FSDPModuleÚUnshardHandle)Ú_Loss)Úrecord_function)Úgenerate_rank_to_stage_mappingÚgenerate_stage_to_rank_mappingÚInferenceMode)Ú_split_tensorÚmerge_chunksÚsplit_args_kwargs_into_chunksÚTensorChunkSpec)Ú_PipelineStageBaseÚ	_RecvInfoÚPipelineStageÚPipelineScheduleSingleÚPipelineScheduleMultiÚSchedule1F1BÚScheduleGPipeÚScheduleInterleaved1F1BÚScheduleLoopedBFSÚScheduleInterleavedZeroBubbleÚScheduleZBVZeroBubbleÚScheduleDualPipeVc                   ól   a € ] tR t^6t o RtRtRtRtRtRt	Rt
RtR	tR
tRtRt]V 3R lR l4       tRtV tR# )Ú_ComputationTypeÚFÚIÚWÚUNSHARDÚRESHARDÚSEND_FÚRECV_FÚSEND_BÚRECV_BÚBÚOVERLAP_F_BÚREDUCE_GRADc                ó$   <€ V ^8„  d   QhRS[ RR/# )é   ÚactionÚreturnr)   ©Ústr)ÚformatÚ__classdict__s   "€Úg/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torch/distributed/pipelining/schedules.pyÚ__annotate__Ú_ComputationType.__annotate__F   s    ø€ ÷ Nñ N™ð NÐ!3ñ Nó    c                ó^   €  \        V 4      #   \         d   p\        R T  24      ThRp?ii ; i)úInvalid computation type N)r)   Ú
ValueErrorÚRuntimeError)r8   Úexcs   & r>   Úfrom_strÚ_ComputationType.from_strE   s:   € ð	NÜ# FÓ+Ð+øÜô 	NÜÐ!:¸6¸(ÐCÓDÈ#ÐMûð	Nús   ‚
 �,˜'§,© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__ÚFORWARDÚBACKWARD_INPUTÚBACKWARD_WEIGHTr-   r.   r/   r0   r1   r2   ÚFULL_BACKWARDr4   r5   ÚstaticmethodrG   Ú__static_attributes__Ú__classdictcell__©r=   s   @r>   r)   r)   6   sU   ø‡ € à€GØ€NØ€OØ€GØ€GØ€FØ€FØ€FØ€FØ€MØ€KØ€Kà÷Nó öNrA   r)   zK(\d+)(F|I|B|W|UNSHARD|RESHARD|REDUCE_GRAD|SEND_F|RECV_F|SEND_B|RECV_B)(\d*)c                   óx   a € ] tR t^mt o RtRtR tR t]V 3R lR l4       t	]
V 3R lR l4       tV 3R ltR	tV tR# )
Ú_ActionNc                ó"   € V P                  4       # ©N)Ú__repr__©Úselfs   &r>   Ú__str__Ú_Action.__str__s   s   € Ø�}‰}‹ÐrA   c                ó€  € V P                   eP   V P                    Uu. uF  p\        V4      NK  	  ppRRP                  V4       RV P                  P                   2# \        V P                  4      pW0P                  P                  ,          pV P                  e   V\        V P                  4      ,          pV# u upi )NÚ(Ú;Ú))Úsub_actionsÚreprÚjoinÚcomputation_typeÚvaluer;   Ústage_indexÚmicrobatch_index)r\   Ú
sub_actionÚsub_action_reprsÚrepr_strs   &   r>   rZ   Ú_Action.__repr__v   s«   € Ø×ÑÒ'àCG×CSÒCSÓTÑCS°Z¤ ZÖ 0ÑCSÐÐTØ�s—x‘xÐ 0Ó1Ð2°!°D×4IÑ4I×4OÑ4OÐ3PÐQÐQä˜4×+Ñ+Ó,ˆHà×-Ñ-×3Ñ3Õ3ˆHØ×$Ñ$Ò0ØœC × 5Ñ 5Ó6Õ6�ØˆOùò  Us   �B;c                ó    <€ V ^8„  d   QhRS[ /# )r7   r9   )Úbool)r<   r=   s   "€r>   r?   Ú_Action.__annotate__„   s   ø€ ÷ 
ñ 
™tñ 
rA   c                óR   € V P                   \        \        \        \        \
        39   # rY   )rf   rN   rQ   rO   rP   r4   r[   s   &r>   Úis_compute_opÚ_Action.is_compute_opƒ   s)   € à×$Ñ$ÜÜÜÜÜð)
ñ 
ð 	
rA   c                ó    <€ V ^8„  d   QhRS[ /# )r7   Úaction_stringr:   )r<   r=   s   "€r>   r?   rp   Ž   s   ø€ ÷ 3
ñ 3
¡ñ 3
rA   c                ó  € V P                  4       p V R8X  d   R# V P                  R4      '       dÎ   RV 9   dÇ   V P                  R4      pV ^V pV V^,           R p. pVP                  4       '       dS   VP                  R4       F=  p\        P                  VP                  4       4      pVf   K,  VP                  V4       K?  	  \	        R	\        P                  V4      RV'       d   \        V4      R7      # RR7      # \        P                  V 4      ;p'       d]   VP                  4       w  r‰p
\	        \        V4      \        P                  V	4      \        V
4      '       d   \        V
4      4      # R4      # V R8X  d   R# \        RV  R24      h)
z~
Reverse of __repr__

String should be formatted as [stage][action type][(microbatch)]
    e.g. `2F0`, `1UNSHARD`, `3SEND_F1`
Ú Nr`   rb   ra   )rh   rf   ri   rc   zInvalid action string: zD, should be formatted as [stage][action type][(microbatch)] e.g. 2F0éÿÿÿÿ)ÚstripÚ
startswithÚfindÚsplitrW   rG   Úappendr)   ÚtupleÚ_action_regexÚmatchÚgroupsÚintÚlenrE   )ru   Úbracket_endÚsub_partÚcomputation_type_partrc   Úsub_strrj   r€   rh   rf   ri   s   &          r>   rG   Ú_Action.from_str�   s–  € ð &×+Ñ+Ó-ˆØ˜BÔÙð ×#Ñ# C×(Ò(¨S°MÔ-Aà'×,Ñ,¨SÓ1ˆKØ$Ø�+ðˆHð %2Ø˜a•Ð!ð%Ð!ð
 ˆKØ�~‰~×ÒØ'Ÿ~™~¨cÖ2�GÜ!(×!1Ñ!1°'·-±-³/Ó!B�JØ!Ô-Ø#×*Ñ*¨:Ö6ñ  3ô ØÜ!1×!:Ñ!:Ð;PÓ!QØ!%ß2=œE +Ó.ô	ð ð DHô	ð ô "×'Ñ'¨Ó6Ð6ˆ5Ö6Ø>C¿l¹l»nÑ;ˆKÐ+;ÜÜ�KÓ Ü ×)Ñ)Ð*:Ó;Ü),Ð-=×)>Ò)>”Ð$Ó%óð ð EIóð ð
 ˜bÔ ÙÜØ% m _Ð4xÐyó
ð 	
rA   c                ót   <€ V ^8„  d   Qh/ S[ ;R&   S[;R&   S[ R,          ;R&   S[R,          R,          ;R&   # )r7   rh   rf   Nri   rc   )rW   .)r‚   r)   r~   )r<   r=   s   "€r>   r?   rp   m   sC   ø‡ ‚ ÙÑñ á&Ñ&ñ ñ ˜D•jÑ'ñ ñ �~Õ&¨Õ-Ñ4ò	 rA   rI   )rJ   rK   rL   rM   ri   rc   r]   rZ   Úpropertyrr   rR   rG   Ú__annotate_func__rS   rT   rU   s   @r>   rW   rW   m   sL   ø‡ € ð $(ÐØ04€Kòòð ÷
ó ð
ð ÷3
ó ð3
÷C ƒ rA   rW   c                ó0   € V ^8„  d   QhR\         R\        /# ©r7   r8   r9   )rW   r;   )r<   s   "r>   r?   r?   Å   s   € ÷ ñ ¬ð ´Cñ rA   c                 ó   € R \        V 4       2# )zPP:r:   )r8   s   &r>   Ú_get_profiler_function_namer�   Ä   s   € à”�V“�ÐÐrA   c                ó�   € V ^8„  d   QhR\         \        \        \        R,          ,          3,          R\        R,          R\        /# )r7   Úpipeline_orderNÚerror_step_numberr9   )Údictr‚   ÚlistrW   r;   )r<   s   "r>   r?   r?   É   s=   € ÷ <ñ <Üœœd¤7¨T¥>Õ2Ð2Õ3ð<ä˜T•zð<ô 	ñ<rA   c                ó  a€ \         P                  ! V 4      p V  FB  p\        \        W,          4      4       F!  pW,          V,          e   K  RW,          V&   K#  	  KD  	  \	        R V P                  4        4       4      p\        V4       Uu. uF=  pR\        V4      P                  \        \        V^,
          4      4      4      ,           NK?  	  pp\        V 4       Uu. uF  q`P                  VR.V,          4      NK  	  pp\        \        P                  ! VRR/ 4      p\        V 4      p	\        V	4       Uu. uF  pR\        V4      ,           NK  	  p
p\        V.VO5!   Uu. uF  p\	        R V 4       4      NK  	  upoR\        V^ ,          4      ^,           ,          RP                  V3R l\        V
4       4       4      ,           p\        WX4       UUu. uFe  w  rÞV R	2RP                  V3R
 l\        V4       4       4      ,           Ve(   \!        VP#                  4       ^,          4      V8X  d   RMR,           NKg  	  pppVR,           RP                  V4      ,           R,           pV# u upi u upi u upi u upi u uppi )zè
Formats the pipeline order in a timestep (row) x rank (column) grid of actions
and returns the formatted string.

If `error_step_number` is passed in, an additional label will be added to signify which step
that it is erroring on.
rw   c              3   ó8   "  € T F  p\        V4      x € K  	  R # 5irY   )rƒ   )Ú.0Úactionss   & r>   Ú	<genexpr>Ú)_format_pipeline_order.<locals>.<genexpr>à   s   é € ÐHÑ0G W”C˜—L�LÓ0Gùó   ‚zStep Ú	fillvalueúRank c              3   óT   "  € T F  qe   \        \        V4      4      M^ x € K   	  R # 5irY   )rƒ   r;   )r—   Úitems   & r>   r™   rš   ð   s    é € ÐFÁ#¸$Ò.ŒC”�D“	ŒN°AÔ5Ã#ùs   ‚&(Ú c              3   óD   <"  € T F  w  rVR SV,           2 x € K  	  R# 5i©Ú<NrI   )r—   ÚiÚlabelÚmax_lengthss   &  €r>   r™   rš   ô   s+   øé € ð <Ù5K©¨ˆ5��;˜q•>Ð"Ð"Ð
#Ô$Ó5Kùó   ƒ z: c              3   óV   <"  € T F  w  r\        V4      R SV,           2 x € K   	  R# 5ir¢   r:   )r—   r¤   rŸ   r¦   s   &  €r>   r™   rš   ú   s+   øé € ÐRÁ>¹¸”c˜$“i  +¨a¥.Ð!1Ð1Ð2Ô3Ã>ùs   ƒ&)z <-- ERROR HEREÚ
)ÚcopyÚdeepcopyÚrangerƒ   ÚmaxÚvaluesr;   ÚzfillÚsortedÚgetr”   Ú	itertoolsÚzip_longestÚzipre   Ú	enumerater‚   r|   )r‘   r’   Úrankr¤   Ú	num_stepsÚstep_labelsÚkeyÚrank_actionsÚtransposed_actionsÚ	num_ranksÚrank_labelsÚcolÚ
header_rowr¥   ÚrowÚformatted_rowsÚformatted_tabler¦   s   &&               @r>   Ú_format_pipeline_orderrÃ   É   sj  ø€ ô —]’] >Ó2€Nó ˆÜ”s˜>Õ/Ó0Ö1ˆAØÕ# AÕ&Ô.à*,�Õ$ QÓ'ó 2ñ ô ÑH°×0EÑ0EÔ0GÓHÓH€IäAFÀyÔAQóÙAQ¸Aˆ”#�a“&—,‘,œs¤3 y°1¥}Ó#5Ó6Ó7×7Ð7ÑAQð ð ô
 >DÀNÔ=SóÙ=S°c×Ñ˜3   yÕ 0Ö1Ñ=Sð ð ô
 œi×3Ò3°\ÐPÈRÑPÓQÐä�NÓ#€IÜ-2°9Ô-=Ó>Ñ-=¨�7œS ›V×#Ð#Ñ-=€KÐ>ô �{Ð8Ð%7Ô8óá8ˆCô 	ÑFÁ#ÓFÖFÙ8ñ€Kð
 œ˜K¨�NÓ+¨aÕ/Õ0°3·8±8ô <Ü5>¸{Ô5Kó<ó 4õ €Jô ˜kÔ>ô
ñ ?‰JˆEð ˆ'�ˆØ
�(‰(ÔRÄ9ÈSÄ>ÓRÓ
Rõ	Sð !Ò,Ü�E—K‘K“M !Õ$Ó%Ð):Ô:ñ ð ÷	
ð 	
ñ ?ð ñ 
ð ! 4Õ'¨$¯)©)°NÓ*CÕCÀdÕJ€OØÐùòIùòùò ?ùòùó
s    ÂAI1Ã"I6Ä:I;Å&J ÇA+Jc                   óT  a € ] tR tRt o R$V 3R lR lltR%R ltR tR tV 3R	 lR
 ltR%V 3R lR llt	]
R&V 3R lR ll4       t]
RRRRRRRRRRRRRR/V 3R lR ll4       tRRRRRRRRRR/V 3R lR lltR'V 3R lR lltR%R ltR%V 3R lR lltV 3R lR  ltV 3R! lR" ltR#tV tR# )(Ú_PipelineSchedulei  NTc                óþ   <€ V ^8„  d   QhRS[ RS[RS[P                  3,          R,          RS[S[R3,          R,          RS[S[S[3,          R,          RS[S[S[3,          S[S[,          ,          R,          RS[	/# )	r7   Ún_microbatchesÚloss_fn.NÚargs_chunk_specÚkwargs_chunk_specÚoutput_merge_specÚscale_grads)
r‚   r   ÚtorchÚTensorr~   r   r“   r;   r   ro   )r<   r=   s   "€r>   r?   Ú_PipelineSchedule.__annotate__	  sŽ   ø€ ÷  9ñ  9áð 9ñ ˜#™uŸ|™|Ð+Õ,¨tÕ3ð 9ñ ™°Ð3Õ4°tÕ;ð	 9ñ
  ¡¡_Ð 4Õ5¸Õ<ð 9ñ  ¡¡S �>©E±#­JÕ6¸Õ=ð 9ñ ñ 9rA   c                óØ   € Wn         W n        W`n        W0n        W@n        WPn         V P                  R JV n        . V n        \        P                  RV P                  P                  4       R # )NzUsing %s)Ú_n_microbatchesÚ_loss_fnrÌ   Ú_args_chunk_specÚ_kwargs_chunk_specÚ_output_merge_specÚ_has_backwardÚ_internal_lossesÚloggerÚinfoÚ	__class__rJ   )r\   rÇ   rÈ   rÉ   rÊ   rË   rÌ   s   &&&&&&&r>   Ú__init__Ú_PipelineSchedule.__init__	  se   € ð  .ÔØŒð 'Ôð !0Ôà"3ÔØ"3Ôð	ð "Ÿ]™]°$Ð6ˆÔð 57ˆÔÜ�‰�J §¡× 7Ñ 7Ö8rA   c                ó¶   € VP                   '       dG   V P                  e7   V P                  W#V,          V4      pV P                  P	                  V4       R # R # R # rY   )Úis_lastrÒ   Ú_compute_lossr×   r}   )r\   ÚstageÚoutputÚ
target_mbsÚmb_indexÚloss_kwargsÚlosss   &&&&&& r>   Ú_maybe_compute_lossÚ%_PipelineSchedule._maybe_compute_loss+  sH   € ð �=�=ˆ=˜TŸ]™]Ò6Ø×%Ñ% f¸Õ.BÀKÓPˆDØ×!Ñ!×(Ñ(¨Ö.ñ 7‰=rA   c                óH  € ^ Tu;8*  ;'       d    \        V P                  4      8  Mu pVP                  '       d*   V P                  e   V'       d   V P                  V,          # \        V P                  4      ^ 8w  d$   V'       g   \	        RV RV P                   24      hR# )r   NzLoss for microbatch z6 is not available. Available losses for microbatches: )rƒ   r×   rÞ   rÒ   rE   )r\   rà   rã   Úvalid_indexs   &&& r>   Ú_maybe_get_lossÚ!_PipelineSchedule._maybe_get_loss2  s‰   € Ø˜8×@Ô@¤c¨$×*?Ñ*?Ó&@Ô@ˆØ�=�=ˆ=˜TŸ]™]Ò6¿;Ø×(Ñ(¨Õ2Ð2Ü�×&Ñ&Ó'¨1Ô,·[ÜØ& x jð 16Ø6:×6KÑ6KÐ5LðNóð ñ
 rA   c                óæ  € \        V\        4      '       g   V.p\        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      pV'       d‚   Ve~   \        V P                  4      V P
                  8w  d/   \        RV P
                   R\        V P                  4       24      hVP                  4        VP                  V P                  4       V P                  P                  4        R# )z2
Update the losses to those in the internal state
c              3   ó8   "  € T F  qP                   x € K  	  R # 5irY   ©rÞ   ©r—   rà   s   & r>   r™   Ú3_PipelineSchedule._update_losses.<locals>.<genexpr>E  s   é € Ð!D¹V°E§-¦-»Vùr›   TFNú
Expecting z losses but got )	Ú
isinstancer”   Úanyrƒ   r×   rÑ   rE   ÚclearÚextend)r\   ÚstagesÚlossesÚcontains_last_stages   &&& r>   Ú_update_lossesÚ _PipelineSchedule._update_losses>  s¼   € ô
 ˜&¤$×'Ò'Ø�XˆFß!›cÑ!D¹VÓ!DŸcŸcšcÑ!D¹VÓ!DÓDÐ÷  6Ò#5Ü�4×(Ñ(Ó)¨T×-AÑ-AÔAÜ"Ø  ×!5Ñ!5Ð 6Ð6FÄsÈ4×K`ÑK`ÓGaÐFbÐcóð ð
 �L‰LŒNà�M‰M˜$×/Ñ/Ô0à×Ñ×#Ñ#Ö%rA   c                ó@   <€ V ^8„  d   QhRS[ S[,          RS[RS[RR/# )r7   rö   Úhas_backwardÚp2p_doner9   N)r”   r   ro   )r<   r=   s   "€r>   r?   rÏ   U  s?   ø€ ÷ N1ñ N1áÑ'Õ(ðN1ñ ðN1ñ ð	N1ð
 
ñN1rA   c                ó  € \         ;QJ d    R V 4       F  '       d   K   RM	  RM! R V 4       4      '       Ed   \        \        \        ,          V4      p\        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      pV'       dº   \        ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      '       d€   V FX  p\
        P                  ! VP                  V4      '       d   \        RVP                   R24      h\
        P                  Vn        KZ  	  \        P                  R\        V4      4       R	# R	p\        \        \        ,          V4       F  pVP                  W'R
7      pK  	  Tp	R	p
\!        \        \        \        ,          V4      4       F_  pVP#                  V	R7      p	V	f   \        R4      hV	P%                  4       ^8X  d   \
        P                  M\
        P&                  p
W¨n        Ka  	  \        P                  RV
'       d   V
P(                  MR\        V4      4       R	# V'       gA   . pV F"  pVP+                  VP-                  4       4       K$  	  \/        \1        V4      4       R	# R	# )uF  Run the P2P warm-up protocol for the given stages.

For ``PipelineStage`` instances this executes the forward/backward vote
protocol (which warms up 2-rank sub-communicators) and sets each
stage's ``_inference_mode``.  For other stage types it falls back to
the legacy ``_get_init_p2p_neighbors_ops`` + ``_batch_p2p`` path.

Args:
    stages: The pipeline stages owned by this rank.
    has_backward: Whether the schedule includes a backward pass.
    p2p_done: ``True`` if P2P neighbours have already been initialised
        (avoids redundant init on evalâ†”train mode switches).
c              3   óB   "  € T F  p\        V\        4      x € K  	  R # 5irY   )rò   r   rï   s   & r>   r™   Ú0_PipelineSchedule._warmup_p2p.<locals>.<genexpr>h  s   é € ÐD¹V°EŒz˜%¤×/Ð/»Vùs   ‚FTc              3   ó2  "  € T F�  pVP                   '       * ;'       d0    VP                  VP                  ^,
          4      '       * ;'       gA    VP                  '       * ;'       d(    VP                  VP                  ^,           4      '       * x € K�  	  R# 5i©é   N)Úis_firstÚ_is_same_rankrh   rÞ   ©r—   Ústs   & r>   r™   r   r  sx   é € ð !ñ $�Bð —[‘[”×MÐM¨×)9Ñ)9¸"¿.¹.È1Õ:LÓ)MÔ%M÷ Qð QØŸ
™
”N×OÐO¨2×+;Ñ+;¸B¿N¹NÈQÕ<NÓ+OÔ'OôQã#ùs   ‚BŸ.BÁBÁ'0Bc              3   óh   "  € T F(  p\         P                  ! VP                  4      R 8H  x € K*  	  R# 5i)ÚfakeN)ÚdistÚget_backendÚgroupr  s   & r>   r™   r   w  s'   é € ð &Ù?H¸”× Ò  §¡Ó*¨fÖ4»yùs   ‚02zStage zâ requires dynamic shape inference, which is not supported with a fake process group. Provide complete static metadata (inputs/outputs, plus input_grads/output_grads for DTensors with backward) to the PipelineStage constructor.zUFake process group detected; set inference_mode=static for %d stage(s) without votingN)Úreceived_acc)Úreceived_resultzP2P warm-up voting failedz1Rank determined inference_mode=%s for %d stage(s)ÚNone)Úallr   r”   r   ró   r   Úneeds_dynamicÚ
_user_metarE   rh   ÚSTATICÚ_inference_moderØ   Údebugrƒ   Ú_warmup_forward_voteÚreversedÚ_warmup_backward_resultrŸ   ÚDYNAMICrg   rõ   Ú_get_init_p2p_neighbors_opsÚ_wait_batch_p2pÚ
_batch_p2p)r\   rö   rü   rý   Ú	pp_stagesÚhas_cross_rankr  Úaccrà   ÚresultÚdetermined_modeÚall_opss   &&&&        r>   Ú_warmup_p2pÚ_PipelineSchedule._warmup_p2pU  s  € ÷& ‹3ÑD¹VÓD�3�3Š3ÑD¹VÓD×DÓDô œT¤-Õ0°&Ó9ˆIß ›Sñ !ñ $ó!ŸSŸSšSñ !ñ $ó!ó ˆN÷
 §#£#ñ &Ù?Hó&§#§#¢#ñ &Ù?Hó&÷ #ò #ó $�BÜ$×2Ò2°2·=±=À,×OÒOÜ*Ø$ R§^¡^Ð$4ð 5+ð +óð ô *7×)=Ñ)=�BÖ&ñ $ô —‘ð5ä˜“Kôñ
 Ø'+ˆCÜœd¤=Õ1°6Ö:�Ø×0Ñ0°Ð0ÓP’ñ ;à*-ˆFØ48ˆOÜ!¤$¤t¬MÕ':¸FÓ"CÖD�Ø×6Ñ6ÀvÐ6ÓN�Ø’>Ü&Ð'BÓCÐCð —{‘{“}¨Ô)ô "×(Ò(ä&×.Ñ.ð  ð
 )8Ö%ñ Eô �L‰LØCß)8�×%Ò%¸fÜ�F“ö÷
 Ø(*ˆGÛ�Ø—‘˜u×@Ñ@ÓBÖCñ  äœJ wÓ/Ö0ñ	 rA   c                óÜ   <€ V ^8„  d   QhRS[ S[,          RS[S[R3,          S[,          RS[S[S[3,          R,          RS[RS[RS[R	S[S[S[3,          R,          R
S[S[S[3,          /# )r7   rö   Úargs.ÚkwargsNÚtargetÚfwd_initializedÚbwd_initializedrä   r9   )r”   r   r~   r   r“   r;   ro   )r<   r=   s   "€r>   r?   rÏ   ±  s›   ø€ ÷ W0ñ W0áÑ'Õ(ðW0ñ ‘C˜�H�o¡Õ#ðW0ñ ‘S™#�X• Õ%ð	W0ñ
 ðW0ñ ðW0ñ ðW0ñ ™#™s˜(•^ dÕ*ðW0ñ 
‰t‘TˆzÕ	ñW0rA   c                ó´  a€ TpV'       d   V P                   V8w  d   RpRpV'       * p	V P                   ;'       d    V'       * p
V	'       g   V
'       g   WV3# V	'       d   V P                  WP                   V4       \        V Uu0 uFT  p\        P                  ! VP                  4      P
                  R8w  g   K4  \        P                  ! VP                  4      kKV  	  up4      pV'       d   V^ ,          P
                  MRoSei   \        ;QJ d    V3R lV 4       F  '       d   K   RM	  RM! V3R lV 4       4      '       g)   V Uu0 uF  qÝP
                  kK  	  pp\        RV 24      hV Uu. uF  p\        V\        4      '       g   K  VNK  	  ppV F  pVP                  4        K  	   \        P                  P                  VSR7      ;_uu_ 4        V	'       dM   RpV FB  pVP                  '       d   TMTpVP                  V P                  VVV P                   R7      pKD  	  RpV
'       dA   Rp\!        V4       F-  pVP#                  V P                  V P$                  VVVR	7      pK/  	  RpRRR4       V F  pVP'                  4        K  	  WV3# u upi u upi u upi   + '       g   i     L;; i  T F  pTP'                  4        K  	  i ; i)
u  Common stage initialization shared by Single and Multi schedules.

Handles mode-change detection (evalâ†”train), P2P warm-up, RNG forking,
forward / backward metadata inference, and FSDP cleanup.

Returns the updated ``(fwd_initialized, bwd_initialized)`` flags.
FÚcpuNc              3   ó@   <"  € T F  qP                   S8H  x € K  	  R # 5irY   )Útype)r—   ÚdeviceÚdevice_types   & €r>   r™   Ú:_PipelineSchedule._initialize_pp_stages.<locals>.<genexpr>Ý  s   øé € ð /
Ù5<¨6�K‰K˜;Ö&³Wùs   ƒTzOAll stages must have the same device type for RNG forking. Found device types: )Údevicesr0  )rü   )rÈ   r(  Úreceived_grad_metarä   )rÖ   r#  r”   rÍ   r/  r.  r  ÚAssertionErrorrò   r   Ú_pre_metadata_inference_backupÚrandomÚfork_rngr  Ú_prepare_forward_infrarÑ   r  Ú_prepare_backward_infrarÒ   Ú _post_metadata_inference_cleanup)r\   rö   r&  r'  r(  r)  r*  rä   rý   Ú	needs_fwdÚ	needs_bwdrà   r2  r/  Údevice_typesÚpipeline_stagesÚnext_stage_argsÚ
stage_argsÚprev_stage_grad_metar0  s   &&&&&&&&           @r>   Ú_initialize_pp_stagesÚ'_PipelineSchedule._initialize_pp_stages±  s˜  ø€ ð( #ˆß × 2Ñ 2°oÔ EØ#ˆOØ#ˆOà'Ô'ˆ	Ø×&Ñ&×>Ð>¨Ô+>ˆ	ç§Ø"Ð3Ð3çØ×Ñ˜V×%7Ñ%7¸ÔBô ñ $óá#�EÜ—<’< §¡Ó-×2Ñ2°eÑ;ô +”—’˜UŸ\™\Ö*Ù#ñó
ˆ÷ *1�g˜a•j—o’o°dˆàÒ"¯3«3ô /
Ù5<ó/
¯3¯3ª3ô /
Ù5<ó/
÷ ,
ò ,
ñ 7>Ó>±g¨FŸKœK±gˆLÐ>Ü ð'Ø'3 nð6óð ñ
  &ó
Ù%�e¬°E¼=×)I�EˆE™vð 	ð 
ó %ˆEØ×0Ñ0Ö2ñ %ð	9Ü—‘×&Ñ&¨wÀKÐ&×PÕPßØ+/�OÛ!'˜Ø-2¯^¯^¨^¡TÀ˜
Ø*/×*FÑ*FØ ×0Ñ0Ø&Ø"Ø)-×);Ñ);ð	 +Gó +šñ "(ð '+�OçØ04Ð(Ü!)¨&Ö!1˜Ø/4×/LÑ/LØ ×0Ñ0Ø$(§M¡MØ#)Ø/CØ(3ð 0Mó 0Ò,ñ "2ð '+�O÷/ Qó2 )�Ø×6Ñ6Ö8ñ )ð Ð/Ð/ùògùò ?ùò

÷ Q×Pûó2 )�Ø×6Ñ6Ö8ò )úsJ   Â1JÂ9$JÅ	JÅ1J$ÆJ$Æ0)J< ÇAJ)È6A J)É6J< Ê)J9	Ê4J< Ê<Kc                óš   <€ V ^8„  d   QhRS[ R,          RS[ R,          RS[ R,          RS[ R,          RS[RS[S[S[3,          R,          /# ©r7   Úarg_mbsNÚ	kwarg_mbsrâ   r÷   Úreturn_outputsrä   ©r”   ro   r“   r;   r   )r<   r=   s   "€r>   r?   rÏ     sd   ø€ ÷ "ñ "á˜•ð"ñ ˜$•;ð"ñ ˜4•Kð	"ñ
 �t•ð"ñ ð"ñ ™#™s˜(•^ dÕ*ñ"rA   c                ó   € \         h)aX  
Run one iteration of the pipeline schedule with list of microbatches.
Will go through all the microbatches according to the schedule
implementation.

Args:
    microbatches: list of microbatch args.
    return_outputs: whether to return the outputs from the last stage.
    loss_kwargs: extra keyword arguments forwarded to the loss function.
©ÚNotImplementedError)r\   rF  rG  râ   r÷   rH  rä   s   &&&&&&&r>   Ú_step_microbatchesÚ$_PipelineSchedule._step_microbatches
  s
   € ô( "Ð!rA   r(  r÷   rH  rä   rF  rG  râ   c          
      ój   <€ V ^8„  d   QhRS[ R,          RS[S[S[3,          R,          RS[RS[RS[/# )r7   r÷   Nrä   rF  rG  râ   )r”   r“   r;   r   )r<   r=   s   "€r>   r?   rÏ   !  sP   ø€ ÷ "ñ "ñ �t•ð	"ñ ™#™s˜(•^ dÕ*ð"ñ ð"ñ ð"ñ ñ"rA   c               ó   € \         h)z+Run one iteration of the pipeline schedule.rK  )
r\   r(  r÷   rH  rä   rF  rG  râ   r&  r'  s
   &$$$$$$$*,r>   ÚstepÚ_PipelineSchedule.step   s
   € ô "Ð!rA   c                ó@   <€ V ^8„  d   QhRS[ R,          RS[RS[RS[/# )r7   r÷   NrF  rG  râ   ©r”   r   )r<   r=   s   "€r>   r?   rÏ   0  s?   ø€ ÷ J7ñ J7ñ �t•ð	J7ñ
 ðJ7ñ ðJ7ñ ñJ7rA   c               ó„   € V P                   p RV n         V P                  ! VRVRVRVRVRV/VB W€n         #   Y€n         i ; i)aU  Run one forward-only iteration of the pipeline schedule.

``eval`` uses the same input contract as ``step``, but temporarily
disables backward execution. By default, ``args``, ``kwargs``, and
``target`` are full-batch values that the schedule splits into
microbatches. When the caller has already split inputs into
microbatches, pass them through ``arg_mbs``, ``kwarg_mbs``, and
``target_mbs`` instead.

Args:
    \*args (Any): Whole-batch positional root inputs when this rank owns
        the first pipeline stage. Do not pass positional inputs with
        pre-split inputs.
    target (Any, optional): Whole-batch target for loss computation.
        When passing pre-split inputs, pass targets through
        ``target_mbs`` instead. Default: ``None``.
    losses (list, optional): Mutable list populated with one loss per
        microbatch when this schedule owns the last stage and a
        ``loss_fn`` was configured. Default: ``None``.
    arg_mbs (list[tuple], optional): Pre-split positional inputs, one
        tuple per microbatch. Default: ``None``.
    kwarg_mbs (list[dict], optional): Pre-split keyword inputs, one
        dict per microbatch. Default: ``None``.
    target_mbs (list, optional): Pre-split targets, one entry per
        microbatch. Default: ``None``.
    \*\*kwargs (Any): Whole-batch keyword root inputs when this rank owns
        the first pipeline stage. Do not pass keyword inputs with
        pre-split inputs.

Returns:
    Any or None: The merged output from the last stage when this rank
        owns the last stage; otherwise ``None``.

Raises:
    TypeError: If a pre-split microbatch container has the wrong type.
    ValueError: If whole-batch and pre-split inputs are mixed, or if a
        pre-split container does not have one entry per microbatch.

Examples::

    >>> # xdoctest: +SKIP("requires a constructed distributed pipeline schedule")
    >>> output = schedule.eval(x, mask=mask)
    >>> arg_mbs = [(x0,), (x1,)]
    >>> kwarg_mbs = [{"mask": mask0}, {"mask": mask1}]
    >>> output = schedule.eval(
    ...     arg_mbs=arg_mbs,
    ...     kwarg_mbs=kwarg_mbs,
    ... )
Fr(  r÷   rF  rG  râ   )rÖ   rQ  )	r\   r(  r÷   rF  rG  râ   r&  r'  Úoriginal_has_backwards	   &$$$$$*, r>   ÚevalÚ_PipelineSchedule.eval0  sw   € ðx !%× 2Ñ 2Ðð	7Ø!&ˆDÔØ—9’9Øðàðð ðð  ð	ð
 $ðð &ðð ñð "7ÕøÐ!6Õús   Ž"7 ·?c                ó†   <€ V ^8„  d   QhRS[ R,          RS[ R,          RS[ R,          RS[ R,          RS[S[ S[ 3,          /# )r7   rF  NrG  râ   r÷   r9   )r”   r~   )r<   r=   s   "€r>   r?   rÏ   |  sV   ø€ ÷ $"ñ $"á˜•ð$"ñ ˜$•;ð$"ñ ˜4•Kð	$"ñ
 �t•ð$"ñ 
‰t‘TˆzÕ	ñ$"rA   c                ó   a € R V 3R llpVe   V! VR4       MR.S P                   ,          pVe   V! VR4       M/ .S P                   ,          pVe
   V! VR4       Ve.   \        V\        4      '       g   \        R\	        V4       24      hW3# )z
Pre-process/check inputs
c                ó$   € V ^8„  d   QhR\         /# )r7   Únamer:   )r<   s   "r>   r?   Ú5_PipelineSchedule._check_inputs.<locals>.__annotate__‡  s   € ÷ 	ñ 	¬#ñ 	rA   c           
      óè   <€ \        V \        4      '       g   \        V R \        V 4       24      h\	        V 4      SP
                  8w  d(   \        RSP
                   RV R\	        V 4       24      hR# )z must be a list but got a rñ   r    z	 but got N)rò   r”   Ú	TypeErrorr.  rƒ   rÑ   rD   )Úmbsr\  r\   s   &&€r>   Úcheck_type_and_lenÚ;_PipelineSchedule._check_inputs.<locals>.check_type_and_len‡  sp   ø€ Ü˜c¤4×(Ò(Ü 4 &Ð(BÄ4ÈÃ9À+Ð NÓOÐOÜ�3‹x˜4×/Ñ/Ô/Ü Ø  ×!5Ñ!5Ð 6°a¸°v¸YÄsÈ3ÃxÀjÐQóð ñ 0rA   rF  rG  râ   z losses must be a list but got a rI   )rÑ   rò   r”   r_  r.  )r\   rF  rG  râ   r÷   ra  s   f&&&& r>   Ú_check_inputsÚ_PipelineSchedule._check_inputs|  s–   ø€ ÷	ð 	ð ÒÙ˜w¨	Õ2à�d˜T×1Ñ1Õ1ˆGàÒ Ù˜y¨+Õ6à˜˜t×3Ñ3Õ3ˆIàÒ!Ù˜z¨<Ô8àÒÜ˜f¤d×+Ò+ÜÐ"BÄ4ÈÃ<À.Ð QÓRÐRàÐ!Ð!rA   c                ó<   € V P                   ! W3/ T;'       g    / B # rY   )rÒ   )r\   rá   r(  rä   s   &&&&r>   rß   Ú_PipelineSchedule._compute_loss¢  s   € Ø�}Š}˜VÑC°×0AÐ0A¸rÑCÐCrA   c                ó^   <€ V ^8„  d   QhRS[ S[R3,          RS[S[S[3,          R,          /# )r7   r&  .r'  N)r~   r   r“   r;   )r<   r=   s   "€r>   r?   rÏ   ¥  s8   ø€ ÷ Lñ Lá‘C˜�H�oðLñ ‘S™#�X• Õ%ñLrA   c                óÖ   € V'       g	   V'       d3   \        VVV P                  V P                  V P                  4      w  r4W43# R.V P                  ,          / .V P                  ,          3# )zR
Splits a full-batch input into chunks (i.e. microbatches) and returns
the chunks
rI   )r   rÑ   rÓ   rÔ   )r\   r&  r'  Ú
args_splitÚkwargs_splits   &&&  r>   Ú_split_inputsÚ_PipelineSchedule._split_inputs¥  si   € ÷ —6Ü'DØØØ×$Ñ$Ø×%Ñ%Ø×'Ñ'ó(Ñ$ˆJð Ð+Ð+ð �4˜$×.Ñ.Õ.°°°t×7KÑ7KÕ0KÐKÐKrA   c                ó²   <€ V ^8„  d   QhRS[ S[R3,          RS[S[S[3,          RS[RS[RS[RS[RS[ S[R	,          S[R	,          S[R	,          3,          /# )
r7   r&  .r'  r(  rF  rG  râ   r9   N)r~   r   r“   r;   r”   )r<   r=   s   "€r>   r?   rÏ   ¼  sy   ø€ ÷ 7.ñ 7.á‘C˜�H�oð7.ñ ‘S™#�X•ð7.ñ ð	7.ñ
 ð7.ñ ð7.ñ ð7.ñ 
‰t�d�{™D 4�K©°­Ð4Õ	5ñ7.rA   c           	     óÞ  € \         ;QJ d    R  WEV3 4       F  '       g   K   RM	  RM! R  WEV3 4       4      pV'       gB   V P                  W4      w  r‰Ve%   \        \        V\        V P
                  4      4      MRp
W‰V
3# V'       d   \        R4      hV'       d*   RP                  \        V4      4      p\        RV R24      hVe   \        R4      hV P                  WEV4      w  rE\        \        WERR	7      4       Fh  w  pw  rÞ\        V\        4      '       g   \        R
V R\        V4       24      h\        V\         4      '       d   KP  \        RV R\        V4       24      h	  WEV3# )c              3   ó(   "  € T F  qR Jx € K
  	  R # 5irY   rI   )r—   r`  s   & r>   r™   Ú;_PipelineSchedule._get_microbatch_inputs.<locals>.<genexpr>Å  s   é € ÐTÑ3S¨C 4�Ó3Sùs   ‚TFNzmWhen using pre-split inputs, pass pre-split positional inputs through arg_mbs=... instead of positional args.ú, z4Unexpected keyword arguments with pre-split inputs: z5. Pass pre-split keyword inputs through kwarg_mbs=...z`When using pre-split inputs, pass pre-split targets through target_mbs=... instead of target=...)Ústrictz.arg_mbs must be a list of tuples, but arg_mbs[z] is a z1kwarg_mbs must be a list of dicts, but kwarg_mbs[)ró   rk  r”   r   Ú_TARGET_CHUNK_SPECrÑ   rD   re   r°   rc  rµ   r´   rò   r~   r_  r.  r“   )r\   r&  r'  r(  rF  rG  râ   Ú	pre_splitri  rj  Útargets_splitÚnamesrã   Úarg_mbÚkwarg_mbs   &&&&&&&        r>   Ú_get_microbatch_inputsÚ(_PipelineSchedule._get_microbatch_inputs¼  s”  € ÷ “CÑT°GÈ
Ñ3SÓT—C—C’CÑT°GÈ
Ñ3SÓTÓTˆ	ßØ'+×'9Ñ'9¸$Ó'GÑ$ˆJð Ò%ô ”] 6Ô+=¸t×?SÑ?SÓTÔUàð ð
 ¨]Ð:Ð:çÜðBóð ÷
 Ø—I‘Iœf V›nÓ-ˆEÜØFÀuÀgð NFð Fóð ð
 ÒÜð7óð ð
 "×/Ñ/°ÀJÓOÑˆä,5Ü�¨4Ô0ö-
Ñ(ˆHÑ(�vô ˜f¤e×,Ò,ÜðØ'˜j¨´°V³¨~ð?óð ô
 ˜h¬×-Ô-Üð!Ø!) 
¨'´$°x³.Ð1AðCóð ñ-
ð  :Ð-Ð-rA   c                ó6   <€ V ^8„  d   QhRS[ S[,          RS[/# )r7   Úoutput_chunksr9   rT  )r<   r=   s   "€r>   r?   rÏ   õ  s   ø€ ÷ 
ñ 
©D±­Ið 
¹#ñ 
rA   c                ó.   € \        VV P                  4      # )z‹
Merge output chunks back to a batch state.
If output_merge_spec is None, the utility will merge output chunks by dimension 0 (batch dim).
)r   rÕ   )r\   r|  s   &&r>   Ú_merge_outputsÚ _PipelineSchedule._merge_outputsõ  s   € ô
 ØØ×#Ñ#ó
ð 	
rA   )rÓ   rÖ   r×   rÔ   rÒ   rÑ   rÕ   rÌ   ©NNNNTrY   ©NNNNTN)NNNN)rJ   rK   rL   rM   rÛ   ræ   rê   rù   r#  rB  r   rM  rQ  rW  rc  rß   rk  ry  r~  rS   rT   rU   s   @r>   rÅ   rÅ     s'  ø‡ € ÷ 9ò  9ôD/ò
ò&÷.N1ð N1÷xW0ò W0ðr ÷"ñ "ó ð"ð* ð"ð ð"ð #ð	"ð
 ð"ð .2ð"ð ð"ð ð"ð ÷"ñ "ó ð"ðJ7ð ðJ7ð #ð	J7ð
 ðJ7ð ðJ7ð ÷J7ò J7÷X$"ò $"ôLD÷Lò L÷.7.ð 7.÷r
ö 
rA   rÅ   c                óž   € V ^8„  d   QhR\         \        P                  ,          R\        R,          R\         \        P                  ,          /# ©r7   Úp2p_opsÚdescNr9   )r”   r
  ÚP2POpr;   ÚWork)r<   s   "r>   r?   r?      s6   € ÷ .+ñ .+œœTŸZ™ZÕ(ð .+´°dµ
ð .+ÄdÌ4Ï9É9Åoñ .+rA   c           
     óø  € \        V 4      ^ 8X  d   . # V'       d   V R2MRp\        P                  RW 4       / pV  F8  pVP                  VP                  P
                  . 4      P                  V4       K:  	  \        V4      ^8”  d:   . p\        VP                  4       4       F  w  rgV\        WqR7      ,          pK  	  V# V  Uu0 uF  qDP                  kK  	  ppV\        P                  08X  dd   V  Uu. uF@  pVP                  VP                  VP                  VP                  VP                  R7      NKB  	  p	pV	 U
u. uF
  qªf   K  V
NK  	  up
# V\        P                   08X  dd   V  Uu. uF@  pVP                  VP                  VP                  VP                  VP                  R7      NKB  	  ppV U
u. uF
  qªf   K  V
NK  	  up
# \        P"                  ! V 4      # u upi u upi u up
i u upi u up
i )a3  
Wrapper over batch_isend_irecv that avoids coalescing for homogeneous
batches (all-send or all-recv).  Coalescing serializes ops on a single
CUDA stream, which causes head-of-line blocking when independent P2P ops
could otherwise overlap.  Mixed batches still use batch_isend_irecv for
deadlock avoidance.
rq  rw   zbatch_p2p %s%s©r…  )r  ÚtagÚ	group_dst)r  rŠ  Ú	group_src)rƒ   rØ   r  Ú
setdefaultr  Ú
group_namer}   r°   Úitemsr  Úopr
  ÚisendÚtensorrŠ  Ú
group_peerÚirecvÚbatch_isend_irecv)r„  r…  Údesc_strÚops_by_groupÚpÚworksÚ_Ú	group_opsÚop_typesÚ
send_worksÚworkÚ
recv_workss   &&          r>   r  r     sÁ  € ô ˆ7ƒ|�qÔØˆ	ß"�$��r‰{¨€HÜ
‡L�LÐ! 8Ô5ð 13€LÛˆØ×Ñ §¡× 2Ñ 2°BÓ7×>Ñ>¸qÖAñ ä
ˆ<Ó˜1ÔØ!#ˆô # <×#5Ñ#5Ó#7Ö8‰LˆAØ”Z 	Ô5Õ5ŠEñ 9àˆá%Ó&™g˜—”™g€HÐ&Ø”D—J‘J�<Ôñ ó
á�ð �D‰D�—‘ §¡¨a¯e©e¸q¿|¹|ˆDÖLÙð 	ð 
ñ ",Ó@¡˜—�¡Ñ@Ð@Ø”D—J‘J�<Ôñ ó
á�ð �D‰D�—‘ §¡¨a¯e©e¸q¿|¹|ˆDÖLÙð 	ð 
ñ ",Ó@¡˜—�¡Ñ@Ð@ä×!Ò! 'Ó*Ð*ùò 'ùò
ùò Aùò
ùò As,   ÃG#Ã5AG(ÅG-ÅG-Å.AG2Æ:G7ÇG7c          	      óÀ   € V ^8„  d   QhR\         \        P                  ,          R\        R,          R\        \
        \         \        P                  ,          3,          /# rƒ  )r”   r
  r†  r;   r“   r‚   r‡  )r<   s   "r>   r?   r?   1  sC   € ÷ ñ Ü”$—*‘*ÕðÜ%(¨4¥Zðä	Œ#Œt”D—I‘I�Ð
ÕñrA   c                ó   € \        \        4      p/ p\        V 4      ^ 8X  d   V# V  F$  pW$P                  ,          P	                  V4       K&  	  \        VP                  4       4       F  w  rV\        WaR7      W5&   K  	  V# )zº
Sorts the list of P2P ops by the peer rank, and then calls
batch_isend_irecv. Return a dictionary of works by peer rank. This function
helps us avoid hangs in case of skip connections.
r‰  )r   r”   rƒ   Úpeerr}   r°   r�  r  )r„  r…  Úops_by_peerÚwork_by_peerr�  r¢  Úopss   &&     r>   Ú_sorted_batch_p2pr¦  1  sw   € ô 0;¼4Ó/@€KØ/1€LÜ
ˆ7ƒ|�qÔØÐó ˆØ—G‘GÕ×#Ñ# BÖ'ñ ô ˜K×-Ñ-Ó/Ö0‰	ˆÜ'¨Ô7ˆÓñ 1ð ÐrA   c                óN   € V ^8„  d   QhR\         \        P                  ,          /# )r7   rž  )r”   r
  r‡  )r<   s   "r>   r?   r?   L  s   € ÷ ñ œ$œtŸy™y�/ñ rA   c                ó8   € V  F  pVP                  4        K  	  R# )zP
Waits for a list of dist.Work (typically from _batch_p2p / _sorted_batch_p2p).
N)Úwait)rž  Úws   & r>   r  r  L  s   € ó ˆØ	�‰Žó rA   c                   ó”   a a€ ] tR tRt oRtRV3R lV 3R llltRR ltRRR	RR
RRRRRRRRR/V3R lR lltV3R lR ltRt	Vt
V ;t# )r   iT  ax  
Base class for single-stage schedules.
Implements the `step` method.
Derived classes should implement `_step_microbatches`.

Gradients are scaled by num_microbatches depending on the `scale_grads` argument, defaulting to True.  This setting
should match the configuration of your loss_fn, which may either average losses (scale_grads=True)
or sum losses (scale_grads=False).
NTc                óÜ   <€ V ^8„  d   QhRS[ RS[RS[R,          RS[S[R3,          R,          RS[S[S[3,          R,          RS[S[S[3,          S[S[,          ,          R,          R	S[/# ©
r7   rà   rÇ   rÈ   NrÉ   .rÊ   rË   rÌ   ©	r   r‚   r   r~   r   r“   r;   r   ro   )r<   r=   s   "€r>   r?   Ú#PipelineScheduleSingle.__annotate___  s‰   ø€ ÷ 
ñ 
á!ð
ñ ð
ñ ˜D•ð	
ñ
 ™°Ð3Õ4°tÕ;ð
ñ  ¡¡_Ð 4Õ5¸Õ<ð
ñ  ¡¡S �>©E±#­JÕ6¸Õ=ð
ñ ñ
rA   c           	     ó¦   <€ \         SV `  VVVVVVR 7       Wn        VP                  V n        RV n        RV n        V P                  4       V n        R# )©rÇ   rÈ   rÉ   rÊ   rË   rÌ   FN)	ÚsuperrÛ   Ú_stageÚ
num_stagesÚ_num_stagesÚ_stage_forward_initializedÚ_stage_backward_initializedÚ_get_pipeline_orderr‘   ©	r\   rà   rÇ   rÈ   rÉ   rÊ   rË   rÌ   rÚ   s	   &&&&&&&&€r>   rÛ   ÚPipelineScheduleSingle.__init___  sd   ø€ ô 	‰ÑØ)ØØ+Ø/Ø/Ø#ð 	ô 	
ð ŒØ ×+Ñ+ˆÔØ*/ˆÔ'Ø+0ˆÔ(ð ×$Ñ$Ó&ð 	ÖrA   c           
     óŽ   € V P                  V P                  .VVVV P                  V P                  VR 7      w  V n        V n        R# ©©rä   N)rB  r³  r¶  r·  )r\   r&  r'  r(  rä   s   &&&&&r>   Ú_initialize_stageÚ(PipelineScheduleSingle._initialize_stage|  sO   € ð ×&Ñ&Ø�[‰[ˆMØØØØ×+Ñ+Ø×,Ñ,Ø#ð 'ó 
ñ	
ØÔ+ØÖ,rA   r(  r÷   rH  rä   rF  rG  râ   c                óp   <€ V ^8„  d   QhRS[ R,          RS[RS[S[S[3,          R,          RS[RS[RS[/# ©r7   r÷   NrH  rä   rF  rG  râ   rI  )r<   r=   s   "€r>   r?   r¯  Š  sb   ø€ ÷ gñ gñ �t•ð	gñ
 ðgñ ™#™s˜(•^ dÕ*ðgñ ðgñ ðgñ ñgrA   c          	     óÒ  € V P                   '       d'   \        P                  ! 4       '       g   \        R4      hV P                   V P                  n        V P                  P                  4        V P                  VV	VVVV4      w  r«pV P                  V
VVVVVR7       V P                  P                  '       d.   V'       d&   V P                  V P                  P                  4      # R# )aß	  Run one training iteration of a single-stage pipeline schedule.

By default, ``args``, ``kwargs``, and ``target`` are full-batch values
that the schedule splits into microbatches. When the caller has already
split inputs into microbatches, pass them through ``arg_mbs``,
``kwarg_mbs``, and ``target_mbs`` instead.

Args:
    \*args (Any): Whole-batch positional inputs for the first pipeline
        stage. Do not pass positional inputs with pre-split inputs.
    target (Any, optional): Whole-batch target for loss computation.
        When passing pre-split inputs, pass targets through
        ``target_mbs`` instead. Default: ``None``.
    losses (list, optional): Mutable list populated with one loss per
        microbatch when this schedule owns the last stage and a
        ``loss_fn`` was configured. Default: ``None``.
    return_outputs (bool, optional): Whether to merge and return output
        chunks on the last stage. Default: ``True``.
    loss_kwargs (dict, optional): Extra keyword arguments forwarded to
        the configured ``loss_fn``. Default: ``None``.
    arg_mbs (list[tuple], optional): Pre-split positional inputs, one
        tuple per microbatch. Default: ``None``.
    kwarg_mbs (list[dict], optional): Pre-split keyword inputs, one
        dict per microbatch. Default: ``None``.
    target_mbs (list, optional): Pre-split targets, one entry per
        microbatch. Default: ``None``.
    \*\*kwargs (Any): Whole-batch keyword inputs for the first pipeline
        stage. Do not pass keyword inputs with pre-split inputs.

Returns:
    Any or None: The merged output from the last stage when this rank
        owns the last stage and ``return_outputs=True``; otherwise
        ``None``.

Raises:
    RuntimeError: If backward computation is enabled and ``step`` is
        called under ``torch.no_grad()``. Use :meth:`eval` for
        forward-only execution.
    TypeError: If a pre-split microbatch container has the wrong type.
    ValueError: If whole-batch and pre-split inputs are mixed, or if a
        pre-split container does not have one entry per microbatch.

Examples::

    >>> # xdoctest: +SKIP("requires a constructed distributed pipeline schedule")
    >>> output = schedule.step(x, mask=mask, target=target)
    >>> arg_mbs = [(x0,), (x1,)]
    >>> kwarg_mbs = [{"mask": mask0}, {"mask": mask1}]
    >>> target_mbs = [target0, target1]
    >>> output = schedule.step(
    ...     arg_mbs=arg_mbs,
    ...     kwarg_mbs=kwarg_mbs,
    ...     target_mbs=target_mbs,
    ... )
ú’step() requires gradients to be enabled for backward computation; it should not be used under torch.no_grad() context. Please call eval() instead.r½  N)rÖ   rÍ   Úis_grad_enabledrE   r³  rü   Úclear_runtime_statesry  rM  rÞ   r~  r|  )r\   r(  r÷   rH  rä   rF  rG  râ   r&  r'  ri  rj  ru  s   &$$$$$$$*,   r>   rQ  ÚPipelineScheduleSingle.stepŠ  s×   € ðF ××Ð¤e×&;Ò&;×&=Ò&=Üð.óð ð $(×#5Ñ#5ˆ�‰Ô ð 	�‰×(Ñ(Ô*à26×2MÑ2MØØØØØØó3
Ñ/ˆ
 -ð 	×ÑØØØØØØ#ð 	 ô 	
ð �;‰;××Ð§>Ø×&Ñ& t§{¡{×'@Ñ'@ÓAÐAárA   c                ób   <€ V ^8„  d   QhRS[ S[S[S[R,          ,          3,          R,          /# ©r7   r9   N©r“   r‚   r”   rW   )r<   r=   s   "€r>   r?   r¯  ó  s,   ø€ ÷ ñ ¡T©#©t±G¸dµNÕ/CÐ*CÕ%DÀtÕ%Kñ rA   c                ó   € R# )a\  
Returns the pipeline execution order as a schedule IR.

The returned IR is a dictionary mapping rank IDs to lists of actions.
Each action is either an _Action object representing computation to perform,
or None representing a deliberate idle step.

The None values are used to represent pipeline bubbles where a rank
must wait for dependencies from other ranks before proceeding. However
during execution, with  the _PipelineScheduleRuntime, these Nones are
skipped since the relevant communication (send/recv) will be scheduled and waited on.

Returns:
    A dictionary mapping rank -> list of actions
NrI   r[   s   &r>   r¸  Ú*PipelineScheduleSingle._get_pipeline_orderó  s   € ñ  rA   )rµ  r³  r·  r¶  r‘   r€  ©NN)rJ   rK   rL   rM   Ú__doc__rÛ   r¾  rQ  r¸  rS   rT   Ú__classcell__©rÚ   r=   s   @@r>   r   r   T  s‚   ù‡ € ñ÷
õ 
ô:
ðgð ðgð #ð	gð
  $ðgð .2ðgð ðgð ðgð ÷gò g÷R÷ ð rA   c                   ó:   a € ] tR tRt o RtRV 3R lR lltRtV tR# )Ú_ScheduleForwardOnlyi  zc
The forward-only schedule.
Will go through all the microbatches and perform only the forward pass
Nc                óš   <€ V ^8„  d   QhRS[ R,          RS[ R,          RS[ R,          RS[ R,          RS[RS[S[S[3,          R,          /# rE  rI  )r<   r=   s   "€r>   r?   Ú!_ScheduleForwardOnly.__annotate__  sd   ø€ ÷ ,"ñ ,"á˜•ð,"ñ ˜$•;ð,"ñ ˜4•Kð	,"ñ
 �t•ð,"ñ ð,"ñ ™#™s˜(•^ dÕ*ñ,"rA   c           	     óJ  € Vf   Ve   \        R4      hV P                  WW44      w  rVe
   V^ ,          MRpV P                  V^ ,          V^ ,          V4       . p\        V P                  4       EF  p	\        RV	 24      ;_uu_ 4        V P                  P                  V	4      p
\        V
RR7      pVP                  4        F  p\        V4       K  	  V P                  P                  W‘V	,          W),          4       V P                  P                  V	4      p
\        V
RR7      pVP                  VP                  4       4       RRR4       \        P                  RV P                  P                   V	4       EK  	  V F  p\        V4       K  	  R#   + '       g   i     LV; i)z,
Run one iteration of the pipeline schedule
Nz7Forward-only schedule does not support loss computationúForward Úfwd_recvr‰  Úfwd_sendú[%s] Forwarded microbatch %s)rE   rc  r¾  r¬   rÑ   r   r³  Úget_fwd_recv_opsr¦  r®   r  Úforward_one_chunkÚget_fwd_send_opsrõ   rØ   r  rh   )r\   rF  rG  râ   r÷   rH  rä   Úmaybe_first_targetÚfwd_sends_to_waitr¤   r¥  r™  rž  s   &&&&&&&      r>   rM  Ú'_ScheduleForwardOnly._step_microbatches  sZ  € ð Ò! VÒ%7ÜØIóð ð "×/Ñ/°ÀJÓWÑˆØ.8Ò.D˜Z¨ž]È$ÐØ×Ñ˜w q�z¨9°Q­<Ð9KÔLð 46Ðô �t×+Ñ+×,ˆAÜ  8¨A¨3 ×0Õ0Ø—k‘k×2Ñ2°1Ó5�Ü)¨#°JÔ?�Ø!ŸL™LžN�DÜ# DÖ)ñ +ð —‘×-Ñ-¨a¸µ¸Y½\ÔJà—k‘k×2Ñ2°1Ó5�Ü)¨#°JÔ?�Ø!×(Ñ(¨¯©«Ô8÷ 1ô �L‰LÐ7¸¿¹×9PÑ9PÐRS×Tñ -ó$ &ˆDÜ˜DÖ!ó &÷# 1×0ús   Â	B;FÆF"rI   r�  )rJ   rK   rL   rM   rÍ  rM  rS   rT   rU   s   @r>   rÑ  rÑ    s   ø‡ € ñ÷
,"÷ ,"ð ,"rA   rÑ  c                   óL   a € ] tR tRt o RtR	V 3R lR lltV 3R lR ltRtV tR# )
r"   i;  zR
The GPipe schedule.
Will go through all the microbatches in a fill-drain manner.
Nc                óš   <€ V ^8„  d   QhRS[ R,          RS[ R,          RS[ R,          RS[ R,          RS[RS[S[S[3,          R,          /# rE  rI  )r<   r=   s   "€r>   r?   ÚScheduleGPipe.__annotate__A  st   ø€ ÷ RYñ RYá˜•ðRYñ ˜$•;ðRYñ ˜4•Kð	RYñ
 �t•ðRYñ ðRYñ ™#™s˜(•^ dÕ*ñRYrA   c           
     óú  € V P                  WW44      w  rVe
   V^ ,          MRpV P                  V^ ,          V^ ,          Wv4       . p\        V P                  4       EF(  p	\	        RV	 24      ;_uu_ 4        V P
                  P                  V	4      p
\        V
RR7      pVP                  4        F  p\        V4       K  	  V P
                  P                  W‘V	,          W),          VR7      pV P
                  P                  V	4      p
\        V
RR7      pVP                  VP                  4       4       RRR4       \        P                  RV P
                  P                  V	4       V P!                  V P
                  XW9V4       EK+  	  V F  p\        V4       K  	  . p\        V P                  4       EF,  p	\	        RV	 24      ;_uu_ 4        V P
                  P#                  V	4      p
\        V
R	R7      pVP                  4        F  p\        V4       K  	  V P%                  V P
                  V	4      pV P
                  P'                  V	VW�P                  ^,
          8H  R
7       V P
                  P)                  V	4      p
\        V
RR7      pVP                  VP                  4       4       RRR4       \        P                  RV P
                  P                  V	4       EK/  	  V F  p\        V4       K  	  V P+                  V P
                  V4       V P
                  P-                  V P.                  '       d   V P                  4       R# ^4       R#   + '       g   i     EL,; i  + '       g   i     LÅ; i)a  
Run one iteration of the pipeline schedule with list of microbatches.
Will go through all the microbatches according to the GPipe schedule.

Args:
    microbatches: list of microbatch args.
    return_outputs: whether to return the outputs from the last stage.
NrÕ  rÖ  r‰  ©Úsave_forward_outputr×  rØ  z	Backward Úbwd_recv©rå   Úlast_backwardÚbwd_sendz[%s] Backwarded microbatch %s)rc  r¾  r¬   rÑ   r   r³  rÙ  r¦  r®   r  rÚ  rÛ  rõ   rØ   r  rh   ræ   Úget_bwd_recv_opsrê   Úbackward_one_chunkÚget_bwd_send_opsrù   Úperform_reduce_gradrÌ   )r\   rF  rG  râ   r÷   rH  rä   rÜ  rÝ  r¤   r¥  r™  rž  rá   Úbwd_sends_to_waitrå   s   &&&&&&&         r>   rM  Ú ScheduleGPipe._step_microbatchesA  sË  € ð" "×/Ñ/°ÀJÓWÑˆØ.8Ò.D˜Z¨ž]È$ÐØ×ÑØ�A�J˜	 !�Ð&8ô	
ð
 46Ðô �t×+Ñ+×,ˆAÜ  8¨A¨3 ×0Õ0Ø—k‘k×2Ñ2°1Ó5�Ü)¨#°JÔ?�Ø!ŸL™LžN�DÜ# DÖ)ñ +ð Ÿ™×6Ñ6Ø˜q•z 9¥<À^ð 7ó �ð —k‘k×2Ñ2°1Ó5�Ü)¨#°JÔ?�Ø!×(Ñ(¨¯©«Ô8÷ 1ô �L‰LÐ7¸¿¹×9PÑ9PÐRSÔTà×$Ñ$ T§[¡[°&¸*È×Uñ# -ó, &ˆDÜ˜DÖ!ñ &ð
 46ÐÜ�t×+Ñ+×,ˆAÜ  9¨Q¨C ×1Õ1Ø—k‘k×2Ñ2°1Ó5�Ü)¨#°JÔ?�Ø!ŸL™LžN�DÜ# DÖ)ñ +ð ×+Ñ+¨D¯K©K¸Ó;�Ø—‘×.Ñ.ØØØ"#×';Ñ';¸aÕ'?Ñ"?ð /ô ð —k‘k×2Ñ2°1Ó5�Ü)¨#°JÔ?�Ø!×(Ñ(¨¯©«Ô8÷ 2ô" �L‰LÐ8¸$¿+¹+×:QÑ:QÐST×Uñ% -ó* &ˆDÜ˜DÖ!ñ &ð 	×Ñ˜DŸK™K¨Ô0à�‰×'Ñ'À×@P×@PÐ@P¨×(<Ñ(<ÖXÐVWÖX÷m 1×0Ð0ú÷8 2×1ús   Á6B=MÇCM*ÍM'Í*M:c                ób   <€ V ^8„  d   QhRS[ S[S[S[R,          ,          3,          R,          /# rÈ  rÉ  )r<   r=   s   "€r>   r?   rá  •  s,   ø€ ÷ ñ ¡T©#©t±G¸dµNÕ/CÐ*CÕ%DÀtÕ%Kñ rA   c           	     ó&  € / pV P                   p\        V4       Fó  p. pTpVP                  R.V,          4       \        V P                  4       F-  pVP	                  \        V\        P                  V4      4       K/  	  ^V^,
          V,
          ,          pVP                  R.V,          4       \        V P                  4       F-  pVP	                  \        V\        P                  V4      4       K/  	  \        W@P                  4      W&   Kõ  	  V# )z‚
Returns the pipeline order for GPipe schedule.

See base method in PipelineScheduleSingle for details on the schedule IR format.
N)
rµ  r¬   rõ   rÑ   r}   rW   r)   rN   rQ   Ú_add_reduce_grad)r\   r‘   Úpp_group_sizer¶   r˜   Úwarmup_delayÚmb_idxÚbackward_delays   &       r>   r¸  Ú!ScheduleGPipe._get_pipeline_order•  sî   € ð ˆØ×(Ñ(ˆä˜-Ö(ˆDØ,.ˆGð  ˆLØ�N‰N˜D˜6 LÕ0Ô1ô   × 4Ñ 4Ö5�Ø—‘œw tÔ-=×-EÑ-EÀvÓNÖOñ 6ð  -°!Õ"3°dÕ":Õ;ˆNØ�N‰N˜D˜6 NÕ2Ô3ô   × 4Ñ 4Ö5�Ø—‘œw tÔ-=×-KÑ-KÈVÓTÖUñ 6ô $4°G×=QÑ=QÓ#RˆNÓ ñ' )ð* ÐrA   rI   r�  )	rJ   rK   rL   rM   rÍ  rM  r¸  rS   rT   rU   s   @r>   r"   r"   ;  s#   ø‡ € ñ÷
RYò RY÷hö rA   c                   ón   a a€ ] tR tRt oRtR
V3R lV 3R llltRV3R lR lltV3R lR ltR	tVt	V ;t
# )r!   i¶  zc
The 1F1B schedule.
Will perform one forward and one backward on the microbatches in steady state.
c                óÜ   <€ V ^8„  d   QhRS[ RS[RS[R,          RS[S[R3,          R,          RS[S[S[3,          R,          RS[S[S[3,          S[S[,          ,          R,          R	S[/# r­  r®  )r<   r=   s   "€r>   r?   ÚSchedule1F1B.__annotate__¼  s‰   ø€ ÷ ñ á!ðñ ðñ ˜D•ð	ñ
 ™°Ð3Õ4°tÕ;ðñ  ¡¡_Ð 4Õ5¸Õ<ðñ  ¡¡S �>©E±#­JÕ6¸Õ=ðñ ñrA   c           
     óŒ   <€ \         SV `  VVVVVVVR 7       W P                  8  d   \        RV RV P                   R24      hR# ))rà   rÇ   rÈ   rÉ   rÊ   rË   rÌ   zNumber of microbatches (z9) must be greater than or equal to the number of stages (z).N)r²  rÛ   rµ  rD   r¹  s	   &&&&&&&&€r>   rÛ   ÚSchedule1F1B.__init__¼  sl   ø€ ô 	‰ÑØØ)ØØ+Ø/Ø/Ø#ð 	ô 	
ð ×,Ñ,Ô,ÜØ*¨>Ð*:ð ;#Ø#'×#3Ñ#3Ð"4°Bð8óð ñ -rA   c                óš   <€ V ^8„  d   QhRS[ R,          RS[ R,          RS[ R,          RS[ R,          RS[RS[S[S[3,          R,          /# rE  rI  )r<   r=   s   "€r>   r?   rù  Õ  st   ø€ ÷ WYñ WYá˜•ðWYñ ˜$•;ðWYñ ˜4•Kð	WYñ
 �t•ðWYñ ðWYñ ™#™s˜(•^ dÕ*ñWYrA   c                óþ  € V P                  WW44      w  rVe
   V^ ,          MRpV P                  V^ ,          V^ ,          Wv4       \        V P                  V P                  V P
                  P                  ,
          4      p^ p	^ p
. p. p\        V4       FÅ  pV P
                  P                  V	4      p\        \        VRR7      4       V P
                  P                  V	W,          W),          VR7      p\        V4       V P
                  P                  V	4      pW˜^,
          8w  d   \        VRR7      pV P                  V P
                  WóW–4       V	^,          p	KÇ  	   V P
                  P                  V
4      p\        \        VV,           RR7      4       V P                  V P
                  V
4      pV P
                  P!                  V
VW P                  ^,
          8H  R7       V P
                  P#                  V
4      pV
^,          p
W�P                  8X  d   M§V P
                  P                  V	4      p\        \        VV,           RR7      4       V P
                  P                  V	W,          W),          VR7      pV P                  V P
                  WóW–4       V P
                  P                  V	4      pV	^,          p	EKb  \        VR	R7      pW P                  8  d½   V P
                  P                  V
4      p\        \        VR
R7      4       V P                  V P
                  V
4      pV P
                  P!                  V
VW P                  ^,
          8H  R7       \        V4       V P
                  P#                  V
4      p\        VR	R7      pV
^,          p
KÌ  \        V4       V P%                  V P
                  V4       V P
                  P'                  V P(                  '       d   V P                  4       R# ^4       R# )aN  
Run one iteration of the pipeline schedule with list of microbatches.
Will go through all the microbatches according to the 1F1B schedule.

Args:
    microbatches: list of microbatch args.
    return_outputs: whether to return the outputs from the last stage.
    loss_kwargs: extra keyword arguments forwarded to the loss function.
NrÖ  r‰  rã  r×  Úfwd_send_bwd_recvræ  Úbwd_send_fwd_recvrè  rå  )rc  r¾  ÚminrÑ   rµ  r³  rh   r¬   rÙ  r  r  rÚ  rÛ  ræ   ré  rê   rê  rë  rù   rì  rÌ   )r\   rF  rG  râ   r÷   rH  rä   rÜ  Úwarmup_chunksÚfwd_mb_indexÚbwd_mb_indexÚ	send_workÚ	fwd_sendsrš  Ú	fwd_recvsrá   Ú	bwd_recvsrå   Ú	bwd_sendss   &&&&&&&            r>   rM  ÚSchedule1F1B._step_microbatchesÕ  s‰  € ð$ "×/Ñ/°ÀJÓWÑˆØ.8Ò.D˜Z¨ž]È$ÐØ×ÑØ�A�J˜	 !�Ð&8ô	
ô Ø× Ñ Ø×Ñ˜tŸ{™{×6Ñ6Õ6ó
ˆð ˆØˆð &(ˆ	Øˆ	Ü�}Ö%ˆAàŸ™×4Ñ4°\ÓBˆIÜœJ y°zÔBÔCð —[‘[×2Ñ2ØØÕ%ØÕ'Ø$2ð	 3ó ˆFô ˜IÔ&ð Ÿ™×4Ñ4°\ÓBˆIØ¨qÕ0Ô0ä& y°zÔB�	ð
 ×$Ñ$Ø—‘˜V°ôð ˜AÕŠLñ? &ðH àŸ™×4Ñ4°\ÓBˆIô œJ y°9Õ'<ÐCVÔWÔXð ×'Ñ'¨¯©°\ÓBˆDØ�K‰K×*Ñ*ØØØ*×.BÑ.BÀQÕ.FÑFð +ô ð Ÿ™×4Ñ4°\ÓBˆIØ˜AÕˆLà×3Ñ3Ô3àð Ÿ™×4Ñ4°\ÓBˆIô œJ y°9Õ'<ÐCVÔWÔXð —[‘[×2Ñ2ØØÕ%ØÕ'Ø$2ð	 3ó ˆFð ×$Ñ$Ø—‘˜V°ôð
 Ÿ™×4Ñ4°\ÓBˆIØ˜AÕ‹Lô ˜y¨zÔ:ˆ	ð ×1Ñ1Ô1àŸ™×4Ñ4°\ÓBˆIÜœJ y°zÔBÔCð ×'Ñ'¨¯©°\ÓBˆDØ�K‰K×*Ñ*ØØØ*×.BÑ.BÀQÕ.FÑFð +ô ô ˜IÔ&ð Ÿ™×4Ñ4°\ÓBˆIÜ" 9°:Ô>ˆIØ˜AÕŠLô 	˜	Ô"ð 	×Ñ˜DŸK™K¨Ô0à�‰×'Ñ'À×@P×@PÐ@P¨×(<Ñ(<ÖXÐVWÖXrA   c                ób   <€ V ^8„  d   QhRS[ S[S[S[R,          ,          3,          R,          /# rÈ  rÉ  )r<   r=   s   "€r>   r?   rù  n  s/   ø€ ÷ Bñ B¡T©#©t±G¸dµNÕ/CÐ*CÕ%DÀtÕ%Kñ BrA   c           	     óV  € / pV P                   p\        V4       EF
  p. pVP                  R.V,          4       V^,
          V,
          p^ p\        V4       F/  pVP                  \	        V\
        P                  V4      4       TpK1  	  \        ^ ^V^,
          V,
          ,          4      pVP                  R.V,          4       ^ p	V P                  V,
          p
V
^ 8”  dr   V^,          pVP                  \	        V\
        P                  V4      4       V
^,          p
VP                  \	        V\
        P                  V	4      4       V	^,          p	Kx  V P                  V	,
          pV^ 8”  d¤   W#,
          ^ 8”  dY   VP                  R4       V^ 8”  d?   VP                  \	        V\
        P                  V	4      4       V	^,          p	V^,          pKj  Kl  VP                  \	        V\
        P                  V	4      4       V	^,          p	V^,          pKª  \        W@P                  4      W&   EK  	  V# )z�
Returns the pipeline order for 1F1B schedule.

See base method in PipelineScheduleSingle for details on the schedule IR format.
N)rµ  r¬   rõ   r}   rW   r)   rN   r­   rÑ   rQ   rñ  )r\   r‘   rò  r¶   r˜   Únum_forwardÚ
forward_mbr¤   Úwait_for_1f1bÚbackward_mbÚremaining_forwardÚremaining_backwards   &           r>   r¸  Ú Schedule1F1B._get_pipeline_ordern  sû  € ð ˆØ×(Ñ(ˆä˜-×(ˆDØ,.ˆGð �N‰N˜D˜6 D�=Ô)ð )¨1Õ,°Õ4ˆKØˆJÜ˜;Ö'�Ø—‘œw tÔ-=×-EÑ-EÀqÓIÔJØ’
ñ (ô
    1¨¸Õ(9¸DÕ(@Õ#AÓBˆMØ�N‰N˜D˜6 MÕ1Ô2ð ˆKØ $× 4Ñ 4°{Õ BÐà# aÔ'à˜a•�
Ø—‘œw tÔ-=×-EÑ-EÀzÓRÔSØ! QÕ&Ð!ð —‘Ü˜DÔ"2×"@Ñ"@À+ÓNôð ˜qÕ ’ð "&×!5Ñ!5¸Õ!CÐà$ qÔ(ð "Õ(¨AÔ-Ø—N‘N 4Ô(à)¨AÔ-ØŸ™Ü# DÔ*:×*HÑ*HÈ+ÓVôð $ qÕ(˜Ø*¨aÕ/Ò*ñ .ð —N‘NÜ Ô&6×&DÑ&DÀkÓRôð   1Õ$�KØ&¨!Õ+Ò&ä#3°G×=QÑ=QÓ#RˆNÔ ñq )ðr ÐrA   rI   r€  r�  )rJ   rK   rL   rM   rÍ  rÛ   rM  r¸  rS   rT   rÎ  rÏ  s   @@r>   r!   r!   ¶  s5   ù‡ € ñ÷
õ ÷2WYò WY÷rB÷ Bð BrA   c                ó0   € V ^8„  d   QhR\         R\        /# )r7   Úaction_typer9   )r)   ro   )r<   s   "r>   r?   r?   ³  s   € ÷ !ñ !Ô'7ð !¼Dñ !rA   c                 ó    € V \         \        39   # rY   )r,   r3   )r  s   &r>   Ú_requires_reduce_gradr  ³  s   € Øœ1œa˜&Ñ Ð rA   c                ó„   € V ^8„  d   QhR\         \        R,          ,          R\        R\         \        R,          ,          /# )r7   r˜   NrÇ   r9   ©r”   rW   r‚   )r<   s   "r>   r?   r?   ·  s6   € ÷ $ñ $Ü”'˜D•.Õ!ð$Ü36ð$ä	Œ'�D�.Õñ$rA   c           	     ó^  aa€ . p\        \        4      oVV3R lpV  FŒ  pVf   K	  VP                  V4       . pVP                  \        8X  d+   VP
                  e   VP
                   F  pV! We4       K  	  MV! WE4       V F#  pVP                  \        V\        R4      4       K%  	  KŽ  	  V# )zŸ
REDUCE_GRAD refers to joint across minibatches grad reduction.
reduce_grad frees memory and we want to schedule it just after the last "backward"-like stage.
c                 óÆ   <€ \        V P                  4      '       dE   V P                  pSV;;,          ^,          uu&   SV,          S8X  d   VP                  V4       R# R# R# r  )r  rf   rh   r}   )ÚaÚto_schedulerh   ÚcntrÇ   s   && €€r>   Ú_leaf_actionÚ&_add_reduce_grad.<locals>._leaf_actionÁ  sR   ø€ Ü  ×!3Ñ!3×4Ò4ØŸ-™-ˆKØ�× Õ!ÓØ�;Õ >Ô1Ø×"Ñ" ;Ö/ñ 2ñ 5rA   N)r   r‚   r}   rf   r4   rc   rW   r5   )	r˜   rÇ   Úactions_with_reduce_gradr  r  Úschedule_reduce_grad_stage_idxsrj   Ú	stage_idxr  s	   &f      @r>   rñ  rñ  ·  s¢   ù€ ð 68ÐÜ%¤cÓ*€Cö0ó ˆØŠ9ÙØ ×'Ñ'¨Ô*Ø57Ð'Ø×Ñ¤Ô,°·±Ò1JØŸmœm�
Ù˜ZÖIò ,ñ ˜Ô<ã8ˆIØ$×+Ñ+¬G°I¼{ÈDÓ,QÖRó 9ñ ð $Ð#rA   c                óv   € V ^8„  d   QhR\         \        R,          ,          R\        R\         \        ,          /# )r7   Úcompute_actionsNÚmax_active_stagesr9   r  )r<   s   "r>   r?   r?   Ø  s8   € ÷ Pñ PÜœ' D�.Õ)ðPäðPô 
Œ'…]ñPrA   c                ó   a
aa€ R R lp\        4       o
. oR V
V3R llpR V
V3R llp\        V 4       F{  w  rVVf   K  V! WVR 4      o\        \        V
3R lS4      4      p\        \        V3R	 lS
4      4      pV F  p	V! V	4       K  	  V F  p	V! V	4       K  	  SP	                  V4       K}  	  \        S
4       F  p	V! V	4       K  	  S# )
aA  Given a basic schedule involving only compute actions (F,B,W,OVERLAP_F_B), add UNSHARD/RESHARD actions for FSDP.

UNSHARD refers to fetching the full contents of an FSDP-sharded layer, requiring an all-gather operation.
RESHARD does the opposite, releasing memory (but doing no communication)

We abandon the "timestep lock"  during lowering

max_active_stages controls how many prefetches we allow. It should be measured in mb and tuneable but in practice
3 stages is probably the thing we want?
(to account for having one f and one b active, and something else prefetching?)
c                óv   € V ^8„  d   QhR\         R\        \        R,          ,          R\        \         ,          /# )r7   ÚcountÚnext_actionsNr9   )r‚   r”   rW   )r<   s   "r>   r?   Ú*_add_unshard_reshard.<locals>.__annotate__è  s/   € ÷ ñ ¤#ð ´T¼'ÀD½.Õ5Ið ÌdÔSVÍiñ rA   c                ó  € \        4       p. pV Fù  pVf   K	  VP                  \        8X  d€   VP                  er   VP                   FL  pVP                  V9  g   K  VP                  VP                  4       VP                  VP                  4       KN  	  \        V4      V 8¼  d    V# K�  VP                  V9  g   K°  VP                  VP                  4       VP                  VP                  4       \        V4      V 8X  g   Kø   V# 	  V# )zdRemove duplicates (same stage, different microbatch), find next 'count' stages that will do compute.)Úsetrf   r4   rc   rh   Úaddr}   rƒ   )r(  r)  ÚseenÚretr  rj   s   &&    r>   Únext_stage_indicesÚ0_add_unshard_reshard.<locals>.next_stage_indicesè  sÚ   € ä›ˆØˆãˆAØŒ}à×%Ñ%¬Ô4¸¿¹Ò9RØ&'§m¤m˜
Ø%×1Ñ1¸Ö=Ø ŸH™H Z×%;Ñ%;Ô<ØŸJ™J z×'=Ñ'=Ö>ñ '4ô ˜3“x 5Ô(Øð ˆ
ñ )ð —}‘}¨DÖ0ØŸ™ §¡Ô/ØŸ
™
 1§=¡=Ô1Ü˜s›8 uÖ,Ø!Øˆ
ñ# ð" ˆ
rA   c                ó$   € V ^8„  d   QhR\         /# ©r7   rh   ©r‚   )r<   s   "r>   r?   r*    ó   € ÷ Gñ Gœcñ GrA   c                 ój   <€ SP                  V 4       SP                  \        V \        R 4      4       R # rY   )r-  r}   rW   r-   ©rh   Úactive_stagesÚfsdp_aware_actionss   &€€r>   Ú_unshardÚ&_add_unshard_reshard.<locals>._unshard  s(   ø€ Ø×Ñ˜+Ô&Ø×!Ñ!¤'¨+´wÀÓ"EÖFrA   c                ó$   € V ^8„  d   QhR\         /# r3  r4  )r<   s   "r>   r?   r*    r5  rA   c                 ój   <€ SP                  V 4       SP                  \        V \        R 4      4       R # rY   )Úremover}   rW   r.   r7  s   &€€r>   Ú_reshardÚ&_add_unshard_reshard.<locals>._reshard  s(   ø€ Ø×Ñ˜[Ô)Ø×!Ñ!¤'¨+´wÀÓ"EÖFrA   Nc                 ó   <€ V S9  # rY   rI   )Úsr8  s   &€r>   Ú<lambda>Ú&_add_unshard_reshard.<locals>.<lambda>  s
   ø€  a¨}Ò&<rA   c                 ó   <€ V S9  # rY   rI   )rB  Únext_ns   &€r>   rC  rD    s	   ø€  a¨v¢orA   )r,  rµ   r”   Úfilterr}   )r$  r%  r0  r:  r?  r¤   r8   ÚfetchÚevictrà   r8  r9  rF  s   &&        @@@r>   Ú_add_unshard_reshardrJ  Ø  sÔ   ú€ õ ô0 "›e€MØ(*Ð÷Gñ G÷Gñ Gô ˜Ö/‰	ˆØŠ>Ùñ $Ð$5ÀqÀrÐ7JÓKˆä”VÔ<¸fÓEÓFˆä”VÔ5°}ÓEÓFˆó ˆEÙ�UŽOñ ãˆEÙ�UŽOñ à×!Ñ! &Ö)ñ/ 0ô4 �mÖ$ˆÙ�Žñ %ð ÐrA   c                ój   € V ^8„  d   QhR\         \        R,          ,          R\         \        ,          /# )r7   r$  Nr9   ©r”   rW   )r<   s   "r>   r?   r?   +  s)   € ÷ #ñ #Üœ' D�.Õ)ð#ä	Œ'…]ñ#rA   c                ó^  € . pV '       Ed"   V P                  ^ 4      pVf   K   \        V 4      '       d   V ^ ,          f   V P                  ^ 4       K/  \        V 4      ^ 8”  d
   V ^ ,          MRpVP                  \        8X  d˜   Ve”   VP                  \        8X  d   VP
                  VP
                  8X  dd   VP                  VP                  8X  dI   VP                  \        VP
                  \        VP                  4      4       V P                  ^ 4       EK  VP                  V4       EK*  V# )a)  Given a basic schedule involving only compute actions (F,I,W), merge adjacent I and W ops into B ops.
(note: I = BACKWARD_INPUT, W = BACKWARD_WEIGHT, B = FULL_BACKWARD)

B refers to running the whole backward (not separating grad_input and grad_weight), which can be more efficient
in some cases.
N)
Úpoprƒ   rf   rO   rP   rh   ri   r}   rW   rQ   )r$  Úmerged_actionsr8   Únext_actions   &   r>   Ú	_merge_bwrQ  +  sû   € ð €Nß
ˆ/Ø ×$Ñ$ QÓ'ˆØŠ>Ùô �/×"Ò" °qÕ'9Ò'AØ×Ñ Ö"ô -0°Ó,@À1Ô,D�o aÖ(È$ˆð ×#Ñ#¤~Ô5ØÒ'Ø×,Ñ,´Ô?Ø×"Ñ" k×&=Ñ&=Ô=Ø×'Ñ'¨;×+GÑ+GÔGà×!Ñ!Ü˜×*Ñ*¬M¸6×;RÑ;RÓSôð ×Ñ ×"à×!Ñ! &×)ØÐrA   c                óÜ   € V ^8„  d   QhR\         \        \        \        ,          3,          R\        \        .\        3,          R\        R\         \        \        \        ,          3,          /# )r7   r$  Ústage_to_rankr´  r9   ©r“   r‚   r”   rW   r   )r<   s   "r>   r?   r?   Q  s]   € ÷ tñ tÜœ#œt¤G�}Ð,Õ-ðtäœS˜E¤3˜JÕ'ðtô ðtô 
Œ#Œt”G�}Ð
Õñ	trA   c           
     ó˜  aaaaaa€ V  Uu/ uF  q3. bK  	  ppV  Uu/ uF  q3\        4       bK  	  upoR VV3R lloR V3R llpR V3R lloV '       Edê   Rp\        V 4       EFÁ  o\        V S,          4      ^ 8”  g$   \        RS: R	\        V S,          4      : 24      hV S,          ^ ,          pVe   VP                  e   VP                  pMV3p\
        ;QJ d!    VVV3R
 lV 4       F  '       d   K   RM	  RM! VVV3R
 lV 4       4      '       g   K¶  VeÔ   VS,          P                  V4       V Fµ  p	SS,          P                  V	4       S! V	4      '       g   K+  V! V	4      w  r«VS,          P                  V
4       SS,          P                  V
4       VS! VP                  4      ,          P                  V4       SS! VP                  4      ,          P                  V4       K·  	  V S,          P                  ^ 4       \        V S,          4      ^ 8X  d   V S RpEKÄ  	  V'       d   EKç  \        R4      hV# u upi u upi )zÙ
Transforms a compute-only schedule into a complete schedule with communication actions.

For actions with sub-actions (OVERLAP_F_B) we ensure that all the subactions have been
computed and the communication is ready
c                ó0   € V ^8„  d   QhR\         R\        /# r�   ©rW   ro   )r<   s   "r>   r?   Ú$_add_send_recv.<locals>.__annotate___  s   € ÷ 	ñ 	œ7ð 	¤tñ 	rA   c                 ó‚  <€ V P                   \        8X  dK   V P                  S^,
          8g  ;'       d-    S! V P                  ^,           4      S! V P                  4      8g  # V P                   \        \        39   dD   V P                  ^ 8g  ;'       d-    S! V P                  ^,
          4      S! V P                  4      8g  # R# )r  F)rf   r*   rh   rO   rQ   )r8   r´  rS  s   &€€r>   Ú
_has_commsÚ"_add_send_recv.<locals>._has_comms_  s¹   ø€ Ø×"Ñ"¤aÔ'Ø×%Ñ%¨°a­Ñ7÷ 3ð 3¹MØ×"Ñ" QÕ&ó=á˜v×1Ñ1Ó2ñ=3ð 3ð ×$Ñ$¬¼Ð(GÔGØ×%Ñ%¨Ñ*÷ 3ð 3©}Ø×"Ñ" QÕ&ó0á˜v×1Ñ1Ó2ñ03ð 3ñ rA   c                óR   € V ^8„  d   QhR\         R\        \         \         3,          /# r�   )rW   r~   )r<   s   "r>   r?   rX  j  s$   € ÷ 	ñ 	œ7ð 	¤u¬W´gÐ-=Õ'>ñ 	rA   c                 óH  <€ S! V 4      '       g   \        V  R 24      hV P                  pV P                  pV P                  p\	        Y\
        8X  d   \        M\        V4      pV\
        8X  d
   V^,           MV^,
          p\	        YR\
        8X  d   \        M\        V4      pWF3# )z is not a valid comm action)
r4  rh   rf   ri   rW   r*   r/   r1   r0   r2   )r8   r"  Úctyperô  ÚsendÚrecv_stage_idxÚrecvrZ  s   &      €r>   Ú
_get_commsÚ"_add_send_recv.<locals>._get_commsj  s‡   ø€ Ù˜&×!Ò!Ü  F 8Ð+FÐ!GÓHÐHØ×&Ñ&ˆ	Ø×'Ñ'ˆØ×(Ñ(ˆÜ�y´Q¬J¥&¼FÀFÓKˆØ*/´1¬*˜ Qž¸)Àa½-ˆÜ�~¼!´¥vÄÈÓPˆØˆzÐrA   c                ó`   € V ^8„  d   QhR\         R,          R\        \         ,          R\        /# )r7   r8   NÚprev_actionsr9   )rW   r,  ro   )r<   s   "r>   r?   rX  u  s*   € ÷ (ñ (¤7¨T¥>ð (ÄÄWÅð (ÔRVñ (rA   c                ó°  <€ V f   R# V P                   \        8X  ds   V P                  ^ 8w  db   \        V P                  \        V P
                  4      V9   d   R# \        V P                  ^,
          \        V P
                  4      V9   d   R# R# V P                   \        \        39   d­   V P                  S^,
          8w  d•   \        V P                  \        V P
                  4      V9   d   R# \        V P                  ^,           \        V P
                  4      V9   d   R# \        V P                  ^,           \        V P
                  4      V9   d   R# R# R# )zþWe don't put our own recv ops in the schedule, we let a sender on another rank put our recv ops in place.
This helps ensure a sane (non-hanging) ordering of sends and recvs.
But it also means we might not be able to schedule our next compute action yet.
TF)	rf   r*   rh   rW   r0   ri   rO   rQ   r2   )r8   re  r´  s   &&€r>   Ú_ready_to_scheduleÚ*_add_send_recv.<locals>._ready_to_scheduleu  s   ø€ ð
 Š>ÙØ×$Ñ$¬Ô)¨f×.@Ñ.@ÀAÔ.Eä˜×*Ñ*¬F°F×4KÑ4KÓLØô ñ ä˜×*Ñ*¨QÕ.´°6×3JÑ3JÓKØô ñ Ùà×#Ñ#¬¼Ð'FÔFØ×"Ñ" j°1¥nÔ4ô ˜×*Ñ*¬F°F×4KÑ4KÓLØô ñ ä˜×*Ñ*¨QÕ.´À×@WÑ@WÓXØô ñ ä˜×*Ñ*¨QÕ.´¸v×?VÑ?VÓWØô ñ ÙárA   Fúrank=z, len(compute_actions[rank])=c              3   óD   <"  € T F  pS! VSS,          4      x € K  	  R # 5irY   rI   )r—   r  rg  re  r¶   s   & €€€r>   r™   Ú!_add_send_recv.<locals>.<genexpr>¬  s!   øé € ÐVÉ+ÀQÑ)¨!¨\¸$Õ-?×@Ð@Ë+ùr§   Tz6Malformed compute schedule, can't schedule sends/recvs)
r,  r°   rƒ   r4  rc   r  r}   r-  rh   rN  )r$  rS  r´  r¶   Úcomm_actionsrb  Úprogressr8   Úall_actionsr  r_  ra  rZ  rg  re  s   &ff`        @@@r>   Ú_add_send_recvro  Q  s  ý€ ñ DSÓ-SÁ?¸4°BªhÁ?€LÐ-SÙETÓ,UÁ_¸T´3³5ª[Á_Ñ,U€L÷	ñ 	÷	ð 	÷(ð (÷T ˆ/Øˆä˜?×+ˆDÜ˜¨Õ-Ó.°Ô2Ü$¨¨¡wÐ.L´°_ÀTÕ5JÓ1KÑ0MÐ%NÓOÐOØ$ TÕ*¨1Õ-ˆFàÒ! f×&8Ñ&8Ò&DØ$×0Ñ0‘à%˜i�ç“3ÖVÉ+ÓV—3—3’3ÖVÉ+ÓV×VÒVÙð Ò!Ø˜TÕ"×)Ñ)¨&Ô1Û$�AØ  Õ&×*Ñ*¨1Ô-Ù! !—}”}Ù%/°£]™
˜ð % TÕ*×1Ñ1°$Ô7Ø$ TÕ*×.Ñ.¨tÔ4Ø$¡]°4×3CÑ3CÓ%DÕE×LÑLÈTÔRØ$¡]°4×3CÑ3CÓ%DÕE×IÑIÈ$ÖOñ %ð ˜DÕ!×%Ñ% aÔ(Ü�? 4Õ(Ó)¨QÔ.Ø# DÐ)Ø‹Hñ= ,÷> ŠxÜ ØHóð ð ÐùòS .TùÚ,Us
   ‹IœIc          	      óÐ   € V ^8„  d   QhR\         \        \        \        ,          3,          R\        \        .\        3,          R\         \        \        \        ,          3,          /# )r7   r˜   rS  r9   rT  )r<   s   "r>   r?   r?   È  sR   € ÷ [ñ [Ü”#”tœG•}Ð$Õ%ð[äœS˜E¤3˜JÕ'ð[ô 
Œ#Œt”G�}Ð
Õñ[rA   c           
     óÌ  aaa€ \         P                  o\         P                  p\         P                  o\         P                  pR VV3R llpR VV3R llp/ pV P                  4        EFï  w  rx. p	/ p
V EF©  pVP                  SV39   d*   VP                  VP                  VP                  3pWºV&   K@  VP                  SV39   dj   V! V4      pW}8  d\   V
P                  4        UUu. uF  w  rïV! V4      V8X  g   K  VNK  	  pppV F#  pV	P                  V
P                  V4      4       K%  	  VP                  e   VP                  MV3pV F¹  pVP                  \        8X  dD   VP                  SVP                  3pWÊ9   d#   V	P                  V
P                  V4      4       KY  K[  VP                  \        \        39   g   Kx  VP                  VVP                  3pWÊ9   g   K™  V	P                  V
P                  V4      4       K»  	  V	P                  V4       EK¬  	  V
'       d*   \        RV R\!        V
P#                  4       4       R24      hW–V&   EKò  	  V# u uppi )a_  
Defers RECV operations to reduce interference with unrelated compute ops,
while maintaining deadlock-safe ordering via rank-parity P2P ordering.

By default, the schedule places RECV ops as early as possible (ASAP) to overlap
P2P communication with computation. However, on some platforms (e.g., AMD ROCm),
a pending RECV can block unrelated compute ops that also use the communication
fabric (e.g., FSDP allgather inside a forward pass), creating pipeline bubbles.

This function defers each RECV to as late as possible, subject to:
  1. A RECV must appear before the compute op that consumes its data.
  2. Deadlock avoidance via rank-parity ordering (see pytorch/pytorch#172668):
     - When current rank > peer rank: SEND before RECV is safe, so deferred
       RECVs from that peer are NOT flushed before SENDs to that peer.
     - When current rank < peer rank: RECV before SEND is required, so
       deferred RECVs from that peer ARE flushed before SENDs to that peer.

This breaks circular waits: the lower-ranked side always posts RECV first,
providing the matching target for the higher-ranked side's SEND.
c                ó0   € V ^8„  d   QhR\         R\        /# r�   ©rW   r‚   )r<   s   "r>   r?   Ú%_defer_recv_ops.<locals>.__annotate__å  ó   € ÷ 9ñ 9¤ð 9¬Cñ 9rA   c                 óŠ   <€ V P                   S8X  d   S! V P                  ^,
          4      # S! V P                  ^,           4      # ©r  ©rf   rh   )r8   r0   rS  s   &€€r>   Ú_recv_peer_rankÚ(_defer_recv_ops.<locals>._recv_peer_rankå  ó=   ø€ Ø×"Ñ" fÔ,Ù  ×!3Ñ!3°aÕ!7Ó8Ð8á  ×!3Ñ!3°aÕ!7Ó8Ð8rA   c                ó0   € V ^8„  d   QhR\         R\        /# r�   rs  )r<   s   "r>   r?   rt  ë  ru  rA   c                 óŠ   <€ V P                   S8X  d   S! V P                  ^,           4      # S! V P                  ^,
          4      # rw  rx  )r8   r/   rS  s   &€€r>   Ú_send_peer_rankÚ(_defer_recv_ops.<locals>._send_peer_rankë  r{  rA   z!Malformed input schedule on rank z,: leftover RECV ops with no consumer found: z9. Every RECV must be consumed by a downstream compute op.)r)   r0   r2   r/   r1   r�  rf   rh   ri   r}   rN  rc   rN   rQ   rO   r4  r”   r®   )r˜   rS  r2   r1   ry  r~  r   r¶   Úaction_listÚnew_actionsÚdeferredr8   r¹   r¢  ÚkÚvÚto_flushÚ	consumersÚsubr0   r/   s   &f                 @@r>   Ú_defer_recv_opsrˆ  È  sK  ú€ ô0 ×$Ñ$€FÜ×$Ñ$€FÜ×$Ñ$€FÜ×$Ñ$€F÷9ñ 9÷9ñ 9ð (*€FØ$Ÿ]™]Ÿ_ÑˆØ%'ˆØLNˆä!ˆFØ×&Ñ&¨6°6Ð*:Ô:à×&Ñ&Ø×+Ñ+Ø×+Ñ+ð�ð
 !'˜‘Ùà×&Ñ&¨6°6Ð*:Ô:Ù& vÓ.�ð ”;à&.§n¡nÔ&6ô Ù&6™d˜a¹/È!Ó:LÐPTÑ:TŸ˜Ñ&6ð ñ  ó  (˜Ø#×*Ñ*¨8¯<©<¸Ó+<Ö=ñ  (ð
 '-×&8Ñ&8Ò&D�×"Ò"È6È)ð ó !�Ø×'Ñ'¬7Ô2ØŸ?™?¨F°C×4HÑ4HÐI�CØ”Ø#×*Ñ*¨8¯<©<¸Ó+<Ö=ñ 'à×)Ñ)¬m¼^Ð-LÖLØŸ?™?¨F°C×4HÑ4HÐI�CØ–Ø#×*Ñ*¨8¯<©<¸Ó+<Ö=ñ !ð ×Ñ˜v×&ñG "÷J Ü Ø3°D°6ð :+Ü+/°·±Ó0AÓ+BÐ*Cð DJðJóð ð #ˆtŒña -ðb €Mùó= s   Ã*I 
Ä I 
c                ó¼   € V ^8„  d   QhR\         \        \        \        R,          ,          3,          R\        R\        R\        R\         \        \        3,          /# )r7   r˜   Nrò  r´  Únum_microbatchesr9   rÉ  )r<   s   "r>   r?   r?   &  s\   € ÷ 'ñ 'Ü”#”tœG d�NÕ+Ð+Õ,ð'äð'ô ð'ô ð	'ô
 
Œ#Œsˆ(…^ñ'rA   c                 óh  a aa€ \        S 4      V8X  g   \        R V R\        S 4       24      h\        V4       F  pVS 9  g   K  \        RV 24      h	  \        V4       Uu/ uF>  pV\        \	        4       \
        \	        4       \        \	        4       \        \	        4       /bK@  	  upo/ oR V VV3R llpS  F„  p\        S V,          4       Fk  w  rxVf   K  \        V\        4      '       g   \        RV RV RV R24      hVP                  e   VP                   F  p	V! W”V4       K  	  Kb  V! W„V4       Km  	  K†  	  S Fç  p
\        SV
,          \        ,          4      p\        SV
,          \
        ,          4      p\        SV
,          \        ,          4      p\        SV
,          \        ,          4      pW³8X  g   \        R	V R
\         RV
 RV 24      hWÞ8X  g   \        RV
 RV RV 24      hWÍV,           ^,          ,           V8X  d   KÏ  \        RV
 RV RV RV RV 2
4      h	  S# u upi )z2Schedule has incorrect number of ranks - expected z	, actual z%Schedule is missing actions for rank c                ó<   € V ^8„  d   QhR\         R\        R\        /# )r7   r8   r¶   rQ  rs  )r<   s   "r>   r?   Ú(_validate_schedule.<locals>.__annotate__A  s!   € ÷ :ñ :¤ð :¬sð :¼#ñ :rA   c                óà  <€ V P                   pV P                  pV P                  pV\        8X  d&   SV,          \        ,          P	                  V4       EMaV\
        8X  dl   VSV,          \        ,          9  d/   RV RV RV RV R2	p\        S
VR7      pV RV 2p\        V4      hSV,          \
        ,          P	                  V4       MëV\        8X  dl   VSV,          \        ,          9  d/   RV RV RV RV R2	p\        S
VR7      pV RV 2p\        V4      hSV,          \        ,          P	                  V4       MuV\        8X  dk   VSV,          \        ,          9  d/   RV RV R	V RV R
2	p\        S
VR7      pV RV 2p\        V4      hSV,          \        ,          P	                  V4       VS9  d   VSV&   R# SV,          p	W8X  g   \        RV RV RV RV RV	 2
4      hR# )zPProcess a single action and update stage_actions and stage_index_to_rank_mappingr�   ú, step z": Running Full Backward for stage z, microbatch z without first running Forward©r’   z

Full pipeline schedule:
z#: Running Backward Input for stage z$: Running Backward Weight for stage z% without first running Backward Inputz: Stage z is assigned to both rank z
 and rank N)
rh   rf   ri   r*   r-  r3   rÃ   r4  r+   r,   )r8   r¶   rQ  Ús_idr^  Úmb_idÚ	error_msgÚformatted_scheduleÚfull_error_msgÚexisting_rankr˜   Ústage_actionsÚstage_index_to_rank_mappings   &&&       €€€r>   Ú_process_actionÚ+_validate_schedule.<locals>._process_actionA  sf  ø€ à×!Ñ!ˆØ×'Ñ'ˆØ×'Ñ'ˆà”AŒ:Ø˜$Õ¤Õ"×&Ñ& uÖ-Ø”aŒZØ˜M¨$Õ/´Õ2Ô2à˜D˜6 ¨¨Ð.PÐQUÐPVð W"Ø"' Ð(FðHð ô &<Ø¨tô&Ð"ð !�kÐ!>Ð?QÐ>RÐSð ô % ^Ó4Ð4Ø˜$Õ¤Õ"×&Ñ& uÕ-Ø”aŒZØ˜M¨$Õ/´Õ2Ô2à˜D˜6 ¨¨Ð.QÐRVÐQWð X"Ø"' Ð(FðHð ô &<Ø¨tô&Ð"ð !�kÐ!>Ð?QÐ>RÐSð ô % ^Ó4Ð4Ø˜$Õ¤Õ"×&Ñ& uÕ-Ø”aŒZØ˜M¨$Õ/´Õ2Ô2à˜D˜6 ¨¨Ð.RÐSWÐRXð Y"Ø"' Ð(MðOð ô &<Ø¨tô&Ð"ð !�kÐ!>Ð?QÐ>RÐSð ô % ^Ó4Ð4Ø˜$Õ¤Õ"×&Ñ& uÔ-àÐ2Ô2Ø04Ð'¨Ó-à7¸Õ=ˆMØÔ)Ü$Ø˜D˜6 ¨¨¨h°t°fÐ<VÐW[ÐV\Ð\fÐgtÐfuÐvóð ñ *rA   r�   r�  z: Got an invalid action: z, expected instance of _ActionzGot r    z microbatches for stage z, expected z(Invalid backward microbatches for stage z8: I and W must have equal counts,             but got I=z, W=z: expected z( total backwards,             but got B=z, I=)rƒ   r4  r¬   r*   r,  r3   r+   r,   rµ   rò   rW   rc   )r˜   rò  r´  rŠ  r¶   Ústage_idr™  rQ  r8   rj   r‘  Úf_mbÚb_mbÚi_mbÚw_mbr—  r˜  s   f&&&           @@r>   Ú_validate_scheduler   &  sp  ú€ ô �‹L˜MÔ)ÜØ@ÀÀÈyÔY\Ð]dÓYeÐXfÐgó
ð 	
ô �mÖ$ˆØ�wÖÜ Ð#HÈÈÐ!OÓPÐPñ %ô ˜jÔ)ó=ñ *ˆHð 	ÜŒs‹uÜŒs‹uÜŒs‹uÜŒs‹uð	
ò 	
ñ *ñ=€Mð #%Ð÷:ò :óx ˆÜ% g¨d¥mÖ4‰LˆDØŠ~ÙÜ˜f¤g×.Ò.Ü$Ø˜D˜6 ¨¨Ð.GÈÀxÐOmÐnóð ð
 ×!Ñ!Ò-à"(×"4Ô"4�JÙ# J°dÖ;ó #5ñ   ¨dÖ3ó 5ñ ó$ ˆÜ�= Õ&¤qÕ)Ó*ˆÜ�= Õ&¤qÕ)Ó*ˆÜ�= Õ&¤qÕ)Ó*ˆÜ�= Õ&¤qÕ)Ó*ˆàÔ(Ü Ø�t�f˜Aœa˜SÐ 8¸¸¸kÐJZÐI[Ð\óð ð ”Ü Ø:¸4¸&ð AØ�f˜D  ð(óð ð
 ˜t�¨Õ)Õ)Ð-=Ö=Ü Ø:¸4¸&ÀÐL\ÐK]ð ^Ø�f˜D   d¨4¨&ð2óð ñ% ð, 'Ð&ùò_=s   Á"AH/c                   óä   a a€ ] tR tRt oRtRV3R lV 3R llltRV3R lR lltV3R	 lR
 ltV3R lR ltR t	RV3R lR llt
RRRRRRRRRRRRRR/V3R lR lltRV3R lR lltRtVtV ;t# ) r    i¨  a@  
Base class for multi-stage schedules.
Implements the `step` method.

Gradients are scaled by num_microbatches depending on the `scale_grads` argument, defaulting to True.  This setting
should match the configuration of your loss_fn, which may either average losses (scale_grads=True)
or sum losses (scale_grads=False).
NTc                ó  <€ V ^8„  d   QhRS[ S[,          RS[RS[R,          RS[S[R3,          R,          RS[S[S[3,          R,          RS[S[S[3,          S[S[,          ,          R,          R	S[	R,          R
S[	RS[	/	# )r7   rö   rÇ   rÈ   NrÉ   .rÊ   rË   Úuse_full_backwardrÌ   Úbackward_requires_autograd©
r”   r   r‚   r   r~   r   r“   r;   r   ro   )r<   r=   s   "€r>   r?   Ú"PipelineScheduleMulti.__annotate__²  s§   ø€ ÷ 4ñ 4áÑ'Õ(ð4ñ ð4ñ ˜D•ð	4ñ
 ™°Ð3Õ4°tÕ;ð4ñ  ¡¡_Ð 4Õ5¸Õ<ð4ñ  ¡¡S �>©E±#­JÕ6¸Õ=ð4ñ   $�;ð4ñ ð4ñ %)ñ4rA   c
           	     ó   <a€ \         SV `  VVVVVVR 7       Wn        V^ ,          P                  V n        V^ ,          P
                  V n        V^ ,          P                  V n        \        V P                  V P                  4      V n
        V P                   F  p
V P                  V
n
        K  	  RV n        RV n        V P                  RJoV3R lV n        / V n        W�n        Ve   \"        P%                  R4       R# R# )r±  FNc                 ó0   <€ V P                   ;'       d    S# rY   rî   )rà   Úhas_losss   &€r>   rC  Ú0PipelineScheduleMulti.__init__.<locals>.<lambda>Ø  s   ø€ °%·-±-×2LÐ2LÀHÐ2LrA   zDeprecation warning: 'use_full_backward' is no longer supported. Simply stop passing it, and everything should still work fine.)r²  rÛ   Ú_stagesr´  rµ  Ú
group_sizerò  Ú
group_rankr¶   r   Ústage_index_to_group_rankÚ_stages_forward_initializedÚ_stages_backward_initializedrÒ   Ú_should_compute_lossr‘   Ú_backward_requires_autogradrØ   Úwarning)r\   rö   rÇ   rÈ   rÉ   rÊ   rË   r£  rÌ   r¤  rà   r©  rÚ   s   &&&&&&&&&& @€r>   rÛ   ÚPipelineScheduleMulti.__init__²  sý   ù€ ô 	‰ÑØ)ØØ+Ø/Ø/Ø#ð 	ô 	
ð ŒØ! !�9×/Ñ/ˆÔØ# A�Y×1Ñ1ˆÔØ˜1•I×(Ñ(ˆŒ	ä)GØ×Ñ × 0Ñ 0ó*
ˆÔ&ð —\”\ˆEØ.2×.LÑ.LˆEÖ+ñ "ð ,1ˆÔ(Ø,1ˆÔ)ð Ÿ™¨dÐ2ˆÜ$LˆÔ!ð @BˆÔð
 ,FÔ(àÒ(Ü�N‰NðQöñ )rA   c                ó4   <€ V ^8„  d   QhRS[ S[R3,          /# )r7   r&  .)r~   r   )r<   r=   s   "€r>   r?   r¦  è  s   ø€ ÷ :ñ :Ù™#˜s˜(•Oñ:rA   c           
     ój  € V P                   ;'       d    V P                  V P                  8g  pV P                   pV P                  V P                  VVVV P                   V P                  VR 7      w  V n         V n        V P                   '       d%   V'       d	   V'       d   V P                  4        R# R# R# r¼  )r¯  rÖ   r°  rB  r«  Ú&_validate_adjacent_stage_communication)r\   r&  r'  r(  rä   Úreinit_for_mode_switchÚforward_initialized_befores   &&&&&  r>   Ú_initialize_stagesÚ(PipelineScheduleMulti._initialize_stagesè  s´   € ð "&×!AÑ!A÷ "
ð "
Ø×Ñ $×"CÑ"CÑCð 	ð &*×%EÑ%EÐ"ð ×&Ñ&Ø�L‰LØØØØ×,Ñ,Ø×-Ñ-Ø#ð 'ó 
ñ	
ØÔ,ØÔ-ð ×+×+Ð+ß*×.Dà×7Ñ7Ö9ñ /Eñ ,rA   c                ó   <€ V ^8„  d   QhRR/# rÈ  rI   )r<   r=   s   "€r>   r?   r¦    s   ø€ ÷ 3ñ 3¸ñ 3rA   c                óJ  € R R lpV P                    EF   pVP                  pVP                  ^ ,           Uu0 uF7  p\        V\        4      '       g   K  VP
                  f   K+  VP
                  kK9  	  ppVP                  '       d   \        4       M	V^,
          0pV! VRVV4       VP                  P                  4        UUu0 uF  pV F  pVf   K	  VkK  	  K  	  p	ppVP                  '       d   \        4       M	V^,           0p
V! VRV	V
4       EK  	  R# u upi u uppi )zGValidate that stage communication follows adjacent-stage topology only.c          
      óx   € V ^8„  d   QhR\         R\        R\        \         ,          R\        \         ,          RR/# )r7   r"  Ú	directionÚactual_stage_indicesÚexpected_stage_indicesr9   N)r‚   r;   r,  )r<   s   "r>   r?   ÚRPipelineScheduleMulti._validate_adjacent_stage_communication.<locals>.__annotate__  sB   € ÷ 	ñ 	Üð	äð	ô #&¤c¥(ð	ô %(¬¥Hð		ð
 ñ	rA   c                 ó’   € W#,
          pV'       d7   \        R V  RV R\        V4       R\        V4       R\        V4       R24      hR# )zLPipelineScheduleMulti only supports adjacent-stage communication, but stage z has z stages z with non-adjacent stages z (allowed adjacent stages: z\). This commonly indicates skip connections, which are unsupported in this schedule runtime.N)rE   r°   )r"  r¿  rÀ  rÁ  Únon_adjacent_stage_indicess   &&&& r>   Ú_check_stage_indicesÚZPipelineScheduleMulti._validate_adjacent_stage_communication.<locals>._check_stage_indices  sq   € ð *>Õ)VÐ&ß)Ü"ð0Ø09¨{¸%À	¸{ð KÜ$Ð%9Ó:Ð;ð <+Ü+1Ð2LÓ+MÐ*Nð O1äÐ4Ó5Ð6ð 7-ð-ó	ð 	ñ *rA   Nzforward recvzforward send)r«  rh   Úargs_recv_inforò   r   Úsourcer  r,  Úact_send_infor®   rÞ   )r\   rÅ  rà   r"  rÙ   Úactual_fwd_recv_sourcesÚexpected_fwd_recv_sourcesÚdstsÚdstÚactual_fwd_send_destsÚexpected_fwd_send_destss   &          r>   r·  Ú<PipelineScheduleMulti._validate_adjacent_stage_communication  s  € õ	ð& —\•\ˆEØ×)Ñ)ˆIð "×0Ñ0°Ö3ó1á3�DÜ˜d¤I×.ô à37·;±;ô �—”Ù3ð $ð 1ð
 27··°¬¬ÀiÐRSÅmÀ_Ð%Ù ØØØ'Ø)ô	ð "×/Ñ/×6Ñ6Ô8ô/á8�DÛ�CØ÷ �áñ Ù8ð "ñ /ð 05¯}¯}¨}¤c¤eÀ9ÈqÅ=À/Ð#Ù ØØØ%Ø'÷	ó1 "ùò1ùó/s   ¸DÁDÁ%DÃDÃ
Dc                óX   <€ V ^8„  d   QhRS[ S[S[S[R,          ,          3,          RR/# )r7   r˜   Nr9   rÉ  )r<   r=   s   "€r>   r?   r¦  6  s4   ø€ ÷ Mñ MÙ™C¡¡g°¥nÕ!5Ð5Õ6ðMà	ñMrA   c                ó°   € \        VV P                  V P                  V P                  4      V n        V P
                   F  pV P                  Vn        K  	  R# )zM
Allocates the stage index to rank mapping which is needed for communication
N)r   rò  rµ  rÑ   r®  r«  )r\   r˜   rà   s   && r>   Ú_validate_and_set_stage_mappingÚ5PipelineScheduleMulti._validate_and_set_stage_mapping6  sM   € ô *<ØØ×ÑØ×ÑØ× Ñ ó	*
ˆÔ&ð —\”\ˆEØ.2×.LÑ.LˆEÖ+ó "rA   c                ó  € \        VRRR7      ;_uu_ 4       p\        P                  ! V4      pV P                   F%  pVP	                  V P                  V,          4       K'  	  RRR4       R#   + '       g   i     R# ; i)úQDump a CSV representation of the schedule into a file with the provided filename.rª  rw   ©ÚnewlineN)ÚopenÚcsvÚwriterr‘   Úwriterow)r\   ÚfilenameÚcsvfilerÛ  r¶   s   &&   r>   Ú	_dump_csvÚPipelineScheduleMulti._dump_csvE  sW   € ä�(˜C¨×,Õ,°Ü—Z’Z Ó(ˆFØ×+Ô+�Ø—‘ × 3Ñ 3°DÕ 9Ö:ñ ,÷ -×,×,Ò,ús   —AA-Á-A>	c                ó4   <€ V ^8„  d   QhRS[ RS[R,          /# ©r7   rÝ  r<   ©Úcompute_onlyÚcompute_comms©r;   r   )r<   r=   s   "€r>   r?   r¦  L  s)   ø€ ÷ Bñ BáðBñ Ð7Õ8ñBrA   c           	     óŠ  € VR8w  d   \        RV 24      h\        VRR7      ;_uu_ 4       p\        P                  ! V4      p\	        V4       F7  w  rVV Uu. uF  p\
        P                  V4      NK  	  upV P                  V&   K9  	  RRR4       V P                  V P                  4       R# u upi   + '       g   i     L2; i)zßLoad a CSV representation of the schedule from a file with the provided filename.
This API will most likely get renamed/refactored so is marked as internal for now.

format must be "compute_only" for PipelineScheduleMulti.
rä  z#format must be "compute_only", got rw   r×  N)	r4  rÙ  rÚ  Úreaderrµ   rW   rG   r‘   rÓ  )r\   rÝ  r<   rÞ  rè  r¶   rÀ   rB  s   &&&     r>   Ú	_load_csvÚPipelineScheduleMulti._load_csvL  s¡   € ð �^Ô#Ü Ð#FÀvÀhÐ!OÓPÐPÜ�( B×'Õ'¨7Ü—Z’Z Ó(ˆFÜ& vÖ.‘	�ÙJMÓ,NÉ#ÀQ¬W×-=Ñ-=¸aÖ-@É#Ñ,N�×#Ñ# DÓ)ñ /÷ (ð 	×,Ñ,¨T×-@Ñ-@ÖAùò	 -O÷ (×'ús   «+B2ÁB-
Á4B2Â-B2Â2C	r(  r÷   rH  rä   rF  rG  râ   c                óp   <€ V ^8„  d   QhRS[ R,          RS[RS[S[S[3,          R,          RS[RS[RS[/# rÁ  rI  )r<   r=   s   "€r>   r?   r¦  a  sb   ø€ ÷ pñ pñ �t•ð	pñ
 ðpñ ™#™s˜(•^ dÕ*ðpñ ðpñ ðpñ ñprA   c          	     ó   € V P                   '       d9   V P                  '       d'   \        P                  ! 4       '       g   \	        R4      hV P
                   F  p
V P                   V
n        K  	  V P
                   F  p
V
P                  4        K  	  V P                  VV	VVVV4      w  r¼pV P                  VVVVVVR7       V P
                   F<  p
V
P                  '       g   K  V'       g   K!  V P                  V
P                  4      u # 	  R# )a
  Run one training iteration of a multi-stage pipeline schedule.

By default, ``args``, ``kwargs``, and ``target`` are full-batch values
that the schedule splits into microbatches. When the caller has already
split inputs into microbatches, pass them through ``arg_mbs``,
``kwarg_mbs``, and ``target_mbs`` instead.

Args:
    \*args (Any): Whole-batch positional root inputs when this rank owns
        the first pipeline stage. Do not pass positional inputs with
        pre-split inputs.
    target (Any, optional): Whole-batch target for loss computation.
        When passing pre-split inputs, pass targets through
        ``target_mbs`` instead. Default: ``None``.
    losses (list, optional): Mutable list populated with one loss per
        microbatch when this schedule owns the last stage and a
        ``loss_fn`` was configured. Default: ``None``.
    return_outputs (bool, optional): Whether to merge and return output
        chunks on the last stage. Default: ``True``.
    loss_kwargs (dict, optional): Extra keyword arguments forwarded to
        the configured ``loss_fn``. Default: ``None``.
    arg_mbs (list[tuple], optional): Pre-split positional inputs, one
        tuple per microbatch. Default: ``None``.
    kwarg_mbs (list[dict], optional): Pre-split keyword inputs, one
        dict per microbatch. Default: ``None``.
    target_mbs (list, optional): Pre-split targets, one entry per
        microbatch. Default: ``None``.
    \*\*kwargs (Any): Whole-batch keyword root inputs when this rank owns
        the first pipeline stage. Do not pass keyword inputs with
        pre-split inputs.

Returns:
    Any or None: The merged output from the last stage when this rank
        owns the last stage and ``return_outputs=True``; otherwise
        ``None``.

Raises:
    RuntimeError: If backward computation is enabled and ``step`` is
        called under ``torch.no_grad()``. Use :meth:`eval` for
        forward-only execution.
    TypeError: If a pre-split microbatch container has the wrong type.
    ValueError: If whole-batch and pre-split inputs are mixed, or if a
        pre-split container does not have one entry per microbatch.

Examples::

    >>> # xdoctest: +SKIP("requires a constructed distributed pipeline schedule")
    >>> output = schedule.step(x, mask=mask, target=target)
    >>> arg_mbs = [(x0,), (x1,)]
    >>> kwarg_mbs = [{"mask": mask0}, {"mask": mask1}]
    >>> target_mbs = [target0, target1]
    >>> output = schedule.step(
    ...     arg_mbs=arg_mbs,
    ...     kwarg_mbs=kwarg_mbs,
    ...     target_mbs=target_mbs,
    ... )
rÃ  r½  N)rÖ   r²  rÍ   rÄ  rE   r«  rü   rÅ  ry  rM  rÞ   r~  r|  )r\   r(  r÷   rH  rä   rF  rG  râ   r&  r'  rà   ri  rj  ru  s   &$$$$$$$*,    r>   rQ  ÚPipelineScheduleMulti.stepa  s  € ðL ××ÐØ×0×0Ð0Ü×)Ò)×+Ò+äð.óð ð —\”\ˆEØ!%×!3Ñ!3ˆEÖñ "ð —\”\ˆEØ×&Ñ&Ö(ñ "ð 37×2MÑ2MØØØØØØó3
Ñ/ˆ
 -ð 	×ÑØØØØØØ#ð 	 ô 	
ð —\”\ˆEØ�}�}Š}§¡Ø×*Ñ*¨5×+>Ñ+>Ó?Ò?ñ "ñ rA   c                óš   <€ V ^8„  d   QhRS[ R,          RS[ R,          RS[ R,          RS[ R,          RS[RS[S[S[3,          R,          /# rE  rI  )r<   r=   s   "€r>   r?   r¦  Ó  sl   ø€ ÷ E2ñ E2á˜•ðE2ñ ˜$•;ðE2ñ ˜4•Kð	E2ñ
 �t•ðE2ñ ðE2ñ ™#™s˜(•^ dÕ*ñE2rA   c           
     ó\  € V P                  WW44      w  rVe
   V^ ,          MRpV P                  V^ ,          V^ ,          Wv4       V P                   Uu/ uF  qˆP                  VbK  	  p	p\	        4       p
\	        4       pV	 Fu  pV^ 8”  d*   V
P                  V P                  V^,
          ,          4       WÀP                  ^,
          8  g   KL  VP                  V P                  V^,           ,          4       Kw  	  \        4       p\        V P                  V P                  ,          4       EFe  w  rï . pVEey   VP                  pVP                  pVP                  pVf   \        R4      hV\        P                   8X  dc   Wœ,          pVP#                  VVV,          VV,          VR7      pV P%                  VVVVV4       VP'                  VP)                  V4      4       EMÎV\        P*                  8X  d´   Wœ,          pV P-                  VV4      pWÜ;;,          ^,          uu&   WÜ,          V P.                  8H  pV P0                  '       d   V P.                  M^pVP3                  VVRVR7       V'       d   VP1                  V4       VP'                  VP5                  V4      4       EMV\        P6                  8X  dQ   Wœ,          pV P-                  VV4      pVP3                  VVRRR7       VP'                  VP5                  V4      4       M¡V\        P8                  8X  d   Wœ,          pWÜ;;,          ^,          uu&   WÜ,          V P.                  8H  pV P0                  '       d   V P.                  M^pVP;                  VVR7       V'       d   VP1                  V4       M\=        RV 24      hV
 Fä  pV P                  V,          pRpV\?        V4      8  d
   VV,          pVf   K7  VP                  pVP                  pVP                  pVf   \        R4      hV\        P                   8X  dB   V^,           V	9   d2   Wœ^,           ,          pVP'                  VPA                  V4      4       K¾  KÀ  V\*        \6        \8        39   d   KØ  \=        RV 24      h	  V FÛ  pV P                  V,          pRpV\?        V4      8  d
   VV,          pVf   K7  VP                  pVP                  pVP                  pVf   \        R4      hV\         \8        39   d   K}  V\6        \*        39   dB   V^,
          V	9   d2   Wœ^,
          ,          pVP'                  VPC                  V4      4       KÍ  KÏ  \=        RV 24      h	  \E        \G        V4      4       EKh  	  V PU                  V P                  V4       R# u upi   \H         dk   p\J        PM                  R	T P                  T PN                  PP                  TTT4       \J        PM                  R
\S        T P                  TR7      4       ThRp?ii ; i)úÌ
Operate on the microbatches for looped schedules (multiple stages on each rank).

TODO: Does not use sorted_batch_isend_irecv(). As a result, this schedule does
not support models with skip connections.
NzCAll currently supported action types require valid microbatch_indexrã  T©rå   Úfull_backwardrç  F©rç  zUnknown computation type zi[Rank %s] pipeline schedule %s caught the following exception '%s' at time_step %s when running action %sz%sr�  )+rc  rº  r«  rh   r,  r-  r®  rµ  r   rµ   r‘   r¶   rf   ri   r4  r)   rN   rÚ  ræ   rõ   rÛ  rQ   rê   rÑ   rÌ   rê  rë  rO   rP   Úbackward_weight_one_chunkrD   rƒ   rÙ  ré  r  r  Ú	ExceptionrØ   ÚerrorrÚ   rJ   rÃ   rù   )r\   rF  rG  râ   r÷   rH  rä   rÜ  rà   Ústage_index_to_stageÚall_prev_ranksÚall_next_ranksrh   Úbackward_counterÚ	time_stepr8   r¥  rf   rã   rá   rå   rç  Úgrad_scale_factorÚ	prev_rankÚprev_rank_opsÚprev_rank_actionÚ	next_rankÚnext_rank_opsÚnext_rank_actionÚes   &&&&&&&                       r>   rM  Ú(PipelineScheduleMulti._step_microbatchesÓ  sü  € ð "×/Ñ/°ÀJÓWÑˆØ.8Ò.D˜Z¨ž]È$ÐØ×ÑØ�A�J˜	 !�Ð&8ô	
ð 37·,²,ó?
Ù2>¨×Ñ˜uÒ$±,ð 	ð ?
ô $'£5ˆÜ#&£5ˆÛ/ˆKà˜QŒØ×"Ñ" 4×#AÑ#AÀ+ÐPQÅ/Õ#RÔSØ×-Ñ-°Õ1Ö1Ø×"Ñ" 4×#AÑ#AÀ+ÐPQÅ/Õ#RÖSñ 0ô *1«ÐÜ!*¨4×+>Ñ+>¸t¿y¹yÕ+I×!JÑˆIð[Ø(*�ØÓ%Ø'-×'>Ñ'>Ð$Ø%×6Ñ6�HØ"(×"4Ñ"4�KØÒ'Ü,Øaóð ð (Ô+;×+CÑ+CÔCà 4Õ A˜Ø!&×!8Ñ!8Ø$Ø# HÕ-Ø% hÕ/Ø0>ð	 "9ó "˜ð ×0Ñ0Ø! 6¨:°xÀôð Ÿ
™
 5×#9Ñ#9¸(Ó#CÖDØ)Ô-=×-KÑ-KÔKà 4Õ A˜Ø#×3Ñ3°E¸8ÓD˜Ø(×5¸Õ:Ó5à,Õ9¸T×=QÑ=QÑQð &ð 59×4D×4DÐ4D˜D×0Ò0È!ð *ð ×0Ñ0Ø$Ø!%Ø*.Ø*7ð	 1ô ÷ )Ø!×-Ñ-Ð.?Ô@àŸ
™
 5×#9Ñ#9¸(Ó#CÖDØ)Ô-=×-LÑ-LÔLà 4Õ A˜Ø#×3Ñ3°E¸8ÓD˜Ø×0Ñ0Ø$Ø!%Ø*/Ø*/ð	 1ô ð Ÿ
™
 5×#9Ñ#9¸(Ó#CÕDØ)Ô-=×-MÑ-MÔMà 4Õ A˜Ø(×5¸Õ:Ó5à,Õ9¸T×=QÑ=QÑQð &ð 59×4D×4DÐ4D˜D×0Ò0È!ð *ð ×7Ñ7Ø$Ø*7ð 8ô ÷ )Ø!×-Ñ-Ð.?Ô@øä(Ð+DÐEUÐDVÐ)WÓXÐXó "0�IØ$(×$7Ñ$7¸	Õ$B�MØ'+Ð$Ø ¤3 }Ó#5Ô5Ø+8¸Õ+CÐ(Ø'Ô3Ø+;×+LÑ+LÐ(Ø#3×#DÑ#D˜Ø&6×&BÑ&B˜Ø#Ò+Ü"0Ø eó#ð ð ,Ô/?×/GÑ/GÔGà*¨Q�Ð2FÔFð )=È1½_Õ(M Ø #§
¡
¨5×+AÑ+AÀ(Ó+KÖ Lñ	  Gð
 .Ü)Ü*Ü+ð2ô ñ !ä",Ø";Ð<LÐ;MÐ Nó#ð ñ; "0ó@ "0�IØ$(×$7Ñ$7¸	Õ$B�MØ'+Ð$Ø ¤3 }Ó#5Ô5Ø+8¸Õ+CÐ(Ø'Ô3Ø+;×+LÑ+LÐ(Ø#3×#DÑ#D˜Ø&6×&BÑ&B˜Ø (Ò 4Ü"0Ø eó#ð ð ,´¼Ð/IÔIá Ø-´.Ä-Ð1PÔPà*¨Q�Ð2FÔFð )=È1½_Õ(M Ø #§
¡
¨5×+AÑ+AÀ(Ó+KÖ Lñ	  Gô #-Ø";Ð<LÐ;MÐ Nó#ð ñ3 "0ô<  ¤
¨3£×0ñW "Kð| 	×Ñ˜DŸL™L¨&Ö1ùò]?
øôx ô Ü—‘ð(à—I‘IØ—N‘N×+Ñ+ØØØôô —‘ØÜ*Ø×+Ñ+¸yôôð �ûð!ús<   ÁV1Ä0EV6É6DV6ÎAV6Ï(C%V6ÓB<V6Ö6X+×A%X&Ø&X+)
r²  rµ  r±  r«  r°  r¯  r‘   rò  r¶   r®  )NNNNNTTrÌ  ©rä  r�  )rJ   rK   rL   rM   rÍ  rÛ   rº  r·  rÓ  rß  ré  rQ  rM  rS   rT   rÎ  rÏ  s   @@r>   r    r    ¨  s³   ù‡ € ñ÷4õ 4÷l:ò :÷23ð 3÷jMð Mò;÷Bò Bð*pð ðpð #ð	pð
  $ðpð .2ðpð ðpð ðpð ÷pò p÷dE2÷ E2ò E2rA   c                   ó@   a € ] tR tRt o RtRtRtRtRtV 3R lt	Rt
V tR# )Ú_PipelineContexti›	  z=Context passed to custom functions during pipeline execution.Nc                ó®   <€ V ^8„  d   Qh/ S[ ;R&   S[S[,          R,          ;R&   S[S[,          R,          ;R&   S[R,          ;R&   S[R,          ;R&   # )r7   Úschedule_refNrF  rG  râ   r÷   )rÅ   r”   r~   r“   )r<   r=   s   "€r>   r?   Ú_PipelineContext.__annotate__›	  s`   ø‡ ‚ ñ $Ñ#ñ	 ñ
 ‘%�[˜4ÕÑ&ñ ñ ‘D�z˜DÕ Ñ'ñ ñ �t•Ñ"ñ ñ �4�KÑò rA   rI   )rJ   rK   rL   rM   rÍ  rF  rG  râ   r÷   r‹   rS   rT   rU   s   @r>   r  r  ›	  s%   ø‡ € áGð #'€GØ#'€IØ"€JØ€F÷ ƒ rA   r  c                   ó2   a € ] tR tRt o V 3R lR ltRtV tR# )Ú_CustomFunctionProtocoli¦	  c                ó*   <€ V ^8„  d   QhRS[ RS[RR/# )r7   r8   Úctxr9   N)rW   r  )r<   r=   s   "€r>   r?   Ú$_CustomFunctionProtocol.__annotate__§	  s   ø€ ×KÑK™wÐKÑ-=ÐKÀ$ÑKrA   c                ó   € R # rY   rI   )r\   r8   r  s   &&&r>   Ú__call__Ú _CustomFunctionProtocol.__call__§	  s   € ÉrA   rI   N)rJ   rK   rL   rM   r  rS   rT   rU   s   @r>   r  r  ¦	  s   ø‡ € ßKÖKrA   r  c                   óÄ   a a€ ] tR tRt oRtV 3R ltV3R lR ltRV3R lV 3R llltRV3R lV 3R	 llltRV3R
 lR llt	R t
V3R lR ltRV3R lR lltRtVtV ;t# )Ú_PipelineScheduleRuntimeiª	  a  
Provides a simple runtime that requires a 'schedule IR' including specified communication operations.

Can be instantiated directly by creating _PipelineScheduleRuntime and calling load_csv, or can be
subclassed and the subclass can be responsible for creating a schedule IR.
c                óä   <€ VP                  R R4      V n        \        SV `  ! V/ VB  / V n        \        4       V n        / V n        / V n        \        \        4      V n        \        4       V n        R# )Údefer_pp_recvFN)rN  Ú_defer_pp_recvr²  rÛ   Ú_comp_type_to_function_mapr   rú  Úbwd_recv_opsÚfwd_recv_opsr   r”   Úunshard_opsr,  Úunsharded_stages)r\   r&  r'  rÚ   s   &*,€r>   rÛ   Ú!_PipelineScheduleRuntime.__init__²	  sh   ø€ Ø$*§J¡J¨ÀÓ$FˆÔÜ‰Ò˜$Ð) &Ò)àLNˆÔ'ä.5«iˆÔð EGˆÔØDFˆÔô <GÄtÓ;LˆÔÜ #£ˆÖrA   c                ó*   <€ V ^8„  d   QhRS[ RS[RR/# )r7   rf   Úcustom_functionr9   N)r)   r  )r<   r=   s   "€r>   r?   Ú%_PipelineScheduleRuntime.__annotate__Â	  s.   ø€ ÷ %Lñ %Lá*ð%Lñ 1ð%Lð 
ñ	%LrA   c           	     óê   € V\         \        \        \        \        \
        \        \        39  d   \        RV R24      hWP                  9   d   \        P                  RV4       W P                  V&   R# )aN  
Register a custom function to be executed for a specific computation type.

Args:
    computation_type: The computation type for which to register the custom function
    custom_function: The function to execute when this computation type is encountered.
        Must have signature: (action: _Action, ctx: _PipelineContext) -> None
rC   zŒ. Only FORWARD, FULL_BACKWARD,                 BACKWARD_INPUT, BACKWARD_WEIGHT, OVERLAP_F_B, UNSHARD, RESHARD and REDUCE_GRAD are supported.zTComputation type %s is already registered. Overwriting the existing custom function.N)rN   rQ   rO   rP   r4   r-   r.   r5   rD   r  rØ   r³  )r\   rf   r  s   &&&r>   Úregister_custom_functionÚ1_PipelineScheduleRuntime.register_custom_functionÂ	  s�   € ð ÜÜÜÜÜÜÜÜð	$
ô 	
ô Ø+Ð,<Ð+=ð >nð oóð ð ×>Ñ>Ô>Ü�N‰Nð<à ôð =L×'Ñ'Ð(8Ó9rA   c                óh   <€ V ^8„  d   QhRS[ S[S[S[R,          ,          3,          RS[R,          /# )r7   r˜   Nr<   rã  )r“   r‚   r”   rW   r   )r<   r=   s   "€r>   r?   r   é	  s<   ø€ ÷ :Gñ :Gá‘c™4¡¨$¥Õ/Ð/Õ0ð:Gñ Ð7Õ8ñ:GrA   c                óh  <a € \         SS `  V4       / S n        VR8X  dg   V F^  p. S P                  V&   W,           F@  pVf   \        R\	        V4       24      hS P                  V,          P                  V4       KB  	  K`  	  R# VR8X  Ed   VP                  4        FF  w  r5\        V4       F2  w  rdVf   K  VP                  '       d   K  \        RV RV RV R24      h	  KH  	  V FU  p\        W,          4      S P                  V&   \        S P                  V,          S P                  4      S P                  V&   KW  	  \        S P                  V 3R	 lS P                  R
7      S n        S P                  '       d#   \!        S P                  V 3R lR7      S n        R# R# \#        RV: R24      h)zÑ
Given an in-memory representation for a simple compute-only schedule, lower it to a complex schedule including
communication actions.  Stores the schedule in self, and must be called before running step_mo()
rå  Nz$Expected action to be not None, got rä  z?Expected compute-only schedule but found communication action 'z
' at rank z, position ze. Communication actions (e.g. SEND_F, RECV_F, etc.) should not be present when format='compute_only'.c                 ó*   <€ SP                   V ,          # rY   ©r®  ©rB  r\   s   &€r>   rC  ÚG_PipelineScheduleRuntime._prepare_schedule_with_comms.<locals>.<lambda>
  s   ø€ ¨×(FÑ(FÀqÖ(IrA   )rS  r´  c                 ó*   <€ SP                   V ,          # rY   r'  r(  s   &€r>   rC  r)   
  s   ø€ ¨D×,JÑ,JÈ1Ö,MrA   )rS  úformat=ú is not implemented)r²  rÓ  Úpipeline_order_with_commsr4  r.  r}   r�  rµ   rr   rD   rJ  rñ  rÑ   ro  rµ  r  rˆ  rL  )r\   r˜   r<   r¶   r8   r€  r¤   rÚ   s   f&&    €r>   Ú_prepare_schedule_with_commsÚ5_PipelineScheduleRuntime._prepare_schedule_with_commsé	  s±  ù€ ô 	‰Ñ/°Ô8àCEˆÔ&Ø�_Ô$Û�Ø79�×.Ñ.¨tÑ4Ø%Ÿm˜m�FØ’~Ü,ØBÄ4ÈÃ<À.ÐQóð ð ×2Ñ2°4Õ8×?Ñ?ÀÖGó ,ó  ð �~Õ%à%,§]¡]¦_Ñ!�Ü!*¨;Ö!7‘I�AØÔ)Ø%×3×3Ò3Ü",ð#$Ø$* 8¨:°d°V¸;ÀqÀcð JTð!Uó#ð ó "8ñ &5ó  �Ü7KØ•Mó8�×.Ñ.¨tÑ4ô 8HØ×2Ñ2°4Õ8Ø×(Ñ(ó8�×.Ñ.¨tÓ4ñ	  ô .<Ø×.Ñ.ÜIØ×+Ñ+ô.ˆDÔ*ð ×"×"Ð"Ü1@Ø×2Ñ2Ü"Mô2�Ö.ñ #ô &¨¨©	Ð1DÐ&EÓFÐFrA   c                ó4   <€ V ^8„  d   QhRS[ RS[R,          /# râ  ræ  )r<   r=   s   "€r>   r?   r   %
  s)   ø€ ÷ Gñ GáðGñ Ð7Õ8ñGrA   c           	     óÔ  <€ VR8X  d-   \         S	V `  V4       V P                  V P                  4       R# VR8X  d†   / p\	        VRR7      ;_uu_ 4       p\
        P                  ! V4      p\        V4       F,  w  rgV Uu. uF  p\        P                  V4      NK  	  upW6&   K.  	  V P                  W2R7       RRR4       R# \        RV: R24      hu upi   + '       g   i     R# ; i)	zñLoads a csv in simple format and then lowers it to include communication actions

format must be either "compute_only" or "compute_comms".  If compute_only, the lowering passes
will automatically be run to generate a compute_comms schedule.
rä  rå  rw   r×  )r<   Nr+  r,  )r²  ré  r.  r‘   rÙ  rÚ  rè  rµ   rW   rG   rL  )
r\   rÝ  r<   r˜   rÞ  rè  r¶   rÀ   rB  rÚ   s
   &&&      €r>   ré  Ú"_PipelineScheduleRuntime._load_csv%
  sÌ   ø€ ð �^Ô#ä‰GÑ˜hÔ'à×-Ñ-¨d×.AÑ.AÖBØ�Ô&ØˆGÜ�h¨×+Õ+¨wÜŸš GÓ,�Ü!*¨6Ö!2‘I�DÙBEÓ$FÁ#¸Q¤W×%5Ñ%5°aÖ%8Á#Ñ$F�G“Mñ "3à×1Ñ1°'Ð1ÔI÷	 ,Ñ+ô &¨¨©	Ð1DÐ&EÓFÐFùò %G÷ ,×+Ð+ús   Á+CÁ>C
ÂCÃCÃC'	c                ó4   <€ V ^8„  d   QhRS[ RS[R,          /# râ  ræ  )r<   r=   s   "€r>   r?   r   >
  s)   ø€ ÷ Jñ JáðJñ Ð7Õ8ñJrA   c                ó†  € VR8X  d†   V P                   f   \        R4      h\        VRRR7      ;_uu_ 4       p\        P                  ! V4      pV P                    F%  pVP                  V P                   V,          4       K'  	  RRR4       R# VR8X  d†   V P                  f   \        R4      h\        VRRR7      ;_uu_ 4       p\        P                  ! V4      pV P                   F%  pVP                  V P                  V,          4       K'  	  RRR4       R# R#   + '       g   i     R# ; i  + '       g   i     R# ; i)	rÖ  rä  Nz'Compute only schedule must be availablerª  rw   r×  rå  z6Must initialize compute_comms schedule before dump_csv)r‘   r4  rÙ  rÚ  rÛ  rÜ  r-  )r\   rÝ  r<   rÞ  rÛ  r¶   s   &&&   r>   rß  Ú"_PipelineScheduleRuntime._dump_csv>
  s  € ð �^Ô#Ø×"Ñ"Ò*Ü$Ð%NÓOÐOÜ�h ¨R×0Õ0°GÜŸš GÓ,�Ø ×/Ô/�DØ—O‘O D×$7Ñ$7¸Õ$=Ö>ñ 0÷ 1Ñ0ð �Ô&Ø×-Ñ-Ò5Ü$ØLóð ô �h ¨R×0Õ0°GÜŸš GÓ,�Ø ×:Ô:�DØ—O‘O D×$BÑ$BÀ4Õ$HÖIñ ;÷ 1Ñ0ñ '÷	 1×0Ð0ú÷ 1×0Ð0ús   ·ADÃAD/ÄD,	Ä/E 	c                óN   a € \        S P                  V 3R  lS P                  4      # )c                 ó*   <€ SP                   V ,          # rY   r'  r(  s   &€r>   rC  Ú4_PipelineScheduleRuntime._simulate.<locals>.<lambda>X
  s   ø€ �d×4Ñ4°QÖ7rA   )Ú_simulate_comms_computer-  rµ  r[   s   fr>   Ú	_simulateÚ"_PipelineScheduleRuntime._simulateU
  s%   ø€ Ü&Ø×*Ñ*Ü7Ø×Ñó
ð 	
rA   c                ó    <€ V ^8„  d   QhRS[ /# )r7   rà   )r   )r<   r=   s   "€r>   r?   r   \
  s   ø€ ÷ Vñ VÑ'9ñ VrA   c                óh  € \        VP                  \        4      pV'       d�   VP                  pW0P                  9   dS   V P                  V,           F  pVP                  4        K  	  V P                  V V P                  P                  V4       W0P                  9  d   \        RV: 24      hR# R# )zQIf an unshard is active for `stage_idx`, wait() it and mark `stage_idx` unshared.z*Attempted to compute on sharded stage_idx=N)	rò   Úsubmodr   rh   r  r©  r  r-  r4  )r\   rà   Ústage_uses_fsdpr"  r�  s   &&   r>   Ú_assert_unshardedÚ*_PipelineScheduleRuntime._assert_unsharded\
  s˜   € ä$ U§\¡\´:Ó>ˆßØ×)Ñ)ˆIØ×,Ñ,Ô,Ø×*Ñ*¨9×5Ð5�BØ—G‘G–Iñ 6à×$Ñ$ YÐ/Ø×%Ñ%×)Ñ)¨)Ô4Ø× 5Ñ 5Ô5Ü$Ð'RÈ	Á|Ð%TÓUÐUñ 6ñ rA   c                óš   <€ V ^8„  d   QhRS[ R,          RS[ R,          RS[ R,          RS[ R,          RS[RS[S[S[3,          R,          /# rE  rI  )r<   r=   s   "€r>   r?   r   i
  sl   ø€ ÷ ~2ñ ~2á˜•ð~2ñ ˜$•;ð~2ñ ˜4•Kð	~2ñ
 �t•ð~2ñ ð~2ñ ™#™s˜(•^ dÕ*ñ~2rA   c           
     ó<  a aaaaaaa€ S P                  SSSV4      w  ooSe
   S^ ,          MRpS P                  S^ ,          S^ ,          VS4       S P                   Uu/ uF  qˆP                  VbK  	  upoS P                  f   \        R4      h. oR VVVVV VVV3R llp	S P                  P                  4        \        S P                  S P                  ,          4       Fâ  w  r«\        P                  RV
V4        \        \        V4      4      ;_uu_ 4        VP                  S P                  9   d4   \!        S SSSV4      pS P                  VP                  ,          ! W¼4       MRVP                  \"        8X  d6   VP$                  f   \        R4      hVP$                   F  pV	! V4       K  	  MV	! V4       RRR4       Kä  	  S'       d   \-        SP/                  4       4       K#  \1        S P2                  4      ^ 8w  d   \        R	4      hS P5                  S P                  V4       R# u upi   + '       g   i     EKc  ; i  \&         dI   p\        P)                  RT
T4       \        P)                  \+        S P                  T
R7      4       ThRp?ii ; i)
rð  NzLMust call _prepare_schedule_with_comms() before calling _step_microbatches()c                ó(   € V ^8„  d   QhR\         RR/# )r7   r8   r9   N)rW   )r<   s   "r>   r?   ÚA_PipelineScheduleRuntime._step_microbatches.<locals>.__annotate__Œ
  s"   € ÷ g	Iñ g	I¤Gð g	I°ñ g	IrA   c                 ó´  <€ V P                   pV P                  e   V P                  MRpV^ 8¼  g'   V\        \        \        39   g   \        RV : R24      hV P                  pSV,          p\        VP                  \        4      pV^,           S9   pV^,
          S9   pV\        8X  d,   SP                  \        VP                  V4      4      4       R # V\        8X  d,   SP                  \        VP                  V4      4      4       R # V\         8X  dQ   W23SP"                  9   d   \        RV: RV: R24      h\        VP%                  V4      4      SP"                  W23&   R # V\&        8X  dQ   W23SP(                  9   d   \        RV: RV: R24      h\        VP+                  V4      4      SP(                  W23&   R # V\        8X  dº   V'       d°   VSP,                  9  d   VSP.                  9  g   \        RV: R24      hVP                  P1                  4        F]  p\        V\        4      '       g   K  \3        \4        VP7                  R	R
7      4      p	SP.                  V,          P                  V	4       K_  	  R # R # V\        8X  d³   V'       d©   VSP,                  9  d   \        RV: R24      hVSP.                  9   d   \        RV: R24      hVP                  P1                  4        F+  p\        V\        4      '       g   K  VP9                  4        K-  	  SP,                  P;                  V4       R # R # V\<        8X  dÕ   SP?                  V4       VP@                  '       gP   V'       gH   W23SP"                  9  d   \        RV : R24      h\C        SP"                  PE                  W234      4       VPG                  VSV,          SV,          SR7      p
SPI                  WJSVS4       V'       d"   SV^,           ,          PK                  W¢4       R # R # V\L        8X  Ed   SP?                  V4       VPN                  '       gP   V'       gH   W23SP(                  9  d   \        RV : R24      h\C        SP(                  PE                  W234      4       SPQ                  WB4      pSPR                  V;;,          ^,          uu&   SPR                  V,          SPT                  8H  pVPW                  VVR	VR7       V'       d2   SV^,
          ,          PY                  VP[                  V4      V4       R # R # V\\        8X  dÔ   SP?                  V4       VPN                  '       gP   V'       gH   W23SP(                  9  d   \        RV : R24      h\C        SP(                  PE                  W234      4       SPQ                  WB4      pVPW                  VVRRR7       V'       d2   SV^,
          ,          PY                  VP[                  V4      V4       R # R # V\^        8X  df   SP?                  V4       SPR                  V;;,          ^,          uu&   SPR                  V,          SPT                  8H  pVPa                  VVR7       R # V\        8X  d4   SPb                  '       d   SPT                  M^pVPe                  V4       R # \g        RV : R24      h)Nzaction=z missing mb_indexzRecv twice for stage_idx=z
 mb_index=z without executing forwardz without executing backwardzUnsharding the same stage_idx=z twiceT)Úasync_opzResharding stage_idx=z without unshardingz before finishing unshardzComputing action=z before receiving inputrã  z Attempted to run compute action=rñ  Fró  z is unknown or unsupportedrx   )4rf   ri   r-   r.   r5   r4  rh   rò   r>  r   r/   r}   r  rÛ  r1   rë  r0   r  rÙ  r2   r  ré  r  r  Úmodulesr   r   ÚunshardÚreshardr>  rN   r@  r  r  rN  rÚ  ræ   Úset_local_fwd_inputrQ   rÞ   rê   rú  rÑ   rê  Úset_local_bwd_inputÚget_local_bwd_outputrO   rP   rô  rÌ   rì  rD   )r8   Ú	comp_typerã   r"  rà   r?  Úis_next_stage_on_this_rankÚis_prev_stage_on_this_rankÚ	submoduleÚhandlerá   rå   rç  rü  rF  rG  rä   rH  r\   Úsend_opsr÷  râ   s   &             €€€€€€€€r>   Ú_perform_actionÚD_PipelineScheduleRuntime._step_microbatches.<locals>._perform_actionŒ
  sP  ø€ Ø×/Ñ/ˆIà+1×+BÑ+BÒ+N�×'Ò'ÐTVð ð ˜A”ØäÜÜðôô %¨¨¡yÐ0AÐ%BÓCÐCØ×*Ñ*ˆIØ(¨Õ3ˆEÜ(¨¯©´zÓBˆOà)2°Q­Ð:NÑ)NÐ&Ø)2°Q­Ð:NÑ)NÐ&ð œFÔ"Ø—‘¤
¨5×+AÑ+AÀ(Ó+KÓ LÖMØœfÔ$Ø—‘¤
¨5×+AÑ+AÀ(Ó+KÓ LÖMØœfÔ$ØÐ(¨D×,=Ñ,=Ô=Ü(Ø4¨)©°[°x±kÐA[Ð\óð ô <FØ×*Ñ*¨8Ó4ó<�×!Ñ! 9Ð"7Ó8ð œfÔ$ØÐ(¨D×,=Ñ,=Ô=Ü(Ø4¨)©°[°x±kÐA\Ð]óð ô <FØ×*Ñ*¨8Ó4ó<�×!Ñ! 9Ð"7Ó8ð œgÔ%ß"à!¨×)>Ñ)>Ô>Ø%¨T×-=Ñ-=Ô=ä,Ð/NÀIÁ<ÈvÐ-VÓWÐWØ%*§\¡\×%9Ñ%9Ö%;˜	Ü)¨)´Z×@Ò@Ù$Ü!%¤m°Y×5FÑ5FÐPTÐ5FÓ5UÓ!V˜Ø×(Ñ(¨Õ3×:Ñ:¸6ÖBó	 &<ñ #ð œgÔ%ß"Ø ¨×(=Ñ(=Ô=Ü,Ø4¨)©Ð5HÐIóð ð ! D×$4Ñ$4Ô4Ü,Ø4¨)©Ð5NÐOóð ð &+§\¡\×%9Ñ%9Ö%;˜	Ü)¨)´Z×@Ò@Ù$Ø!×)Ñ)Ö+ñ &<ð ×)Ñ)×0Ñ0°Ö;ñ #ð œgÔ%Ø×&Ñ& uÔ-ð ŸŸ˜ç6à!Ð,°D×4EÑ4EÔEÜ,Ø0¨©	Ð1HÐIóð ô $ D×$5Ñ$5×$9Ñ$9¸9Ð:OÓ$PÔQà×0Ñ0ØØ˜HÕ%Ø˜hÕ'Ø(6ð	 1ó �ð ×(Ñ(Ø :¨x¸ô÷ .Ø(¨°Q­Õ7×KÑKØöñ .ð
 œmÕ+Ø×&Ñ& uÔ-ð ŸŸ˜ç6à!Ð,°D×4EÑ4EÔEÜ,Ø?¸±yÐ@WÐXóð ô $ D×$5Ñ$5×$9Ñ$9¸9Ð:OÓ$PÔQØ×+Ñ+¨EÓ<�Ø×%Ñ% i×0°AÕ5Ó0Ø $× 5Ñ 5°iÕ @ÀD×DXÑDXÑ X�Ø×(Ñ(ØØØ"&Ø"/ð	 )ô ÷ .Ø(¨°Q­Õ7×KÑKØ×2Ñ2°8Ó<¸höñ .ð œnÔ,Ø×&Ñ& uÔ-à—}—}�}×-GØ!Ð,°D×4EÑ4EÔEÜ,Ø?¸±yÐ@WÐXóð ô $ D×$5Ñ$5×$9Ñ$9¸9Ð:OÓ$PÔQØ×+Ñ+¨EÓ<�Ø×(Ñ(ØØØ"'Ø"'ð	 )ô ÷ .Ø(¨°Q­Õ7×KÑKØ×2Ñ2°8Ó<¸höñ .ð œoÔ-Ø×&Ñ& uÔ-Ø×%Ñ% i×0°AÕ5Ó0Ø $× 5Ñ 5°iÕ @ÀD×DXÑDXÑ X�Ø×/Ñ/ØØ"/ð 0ö ð œkÔ)Ø<@×<L×<LÐ<L D×$8Ò$8ÐRSÐ!Ø×)Ñ)Ð*;Ö<ä  G F¡9Ð,FÐ!GÓHÐHrA   z8_PipelineScheduleRuntime running time_step %d, action %szsub_actions must be setz\_PipelineScheduleRuntime caught exception at step %s when running action %s.  Full Schedule:r�  zUnused unshard operations)rc  rº  r«  rh   r-  r4  rú  rô   rµ   r¶   rØ   r  r   r�   rf   r  r  r4   rc   rõ  rö  rÃ   r  rN  rƒ   r  rù   )r\   rF  rG  râ   r÷   rH  rä   rÜ  rà   rT  rû  r8   r  Úsub_ar  rS  r÷  s   ffff&ff        @@r>   rM  Ú+_PipelineScheduleRuntime._step_microbatchesi
  sZ  ÿ€ ð "×/Ñ/°¸ÀJÐPVÓWÑˆ�Ø.8Ò.D˜Z¨ž]È$ÐØ×ÑØ�A�J˜	 !�Ð&8¸+ô	
ð 37·,²,ó?
Ù2>¨×Ñ˜uÒ$±,ñ?
Ðð ×)Ñ)Ò1Ü Ø^óð ð
 +-ˆ÷g	I÷ g	IðT 	×Ñ×#Ñ#Ô%Ü!*¨4×+IÑ+IÈ$Ï)É)Õ+TÖ!UÑˆIÜ�L‰LØJØØôð
 Ü$Ô%@ÀÓ%H×IÕIØ×.Ñ.°$×2QÑ2QÔQÜ.Ø Ø#Ø%Ø&Ø"ó˜ð ×7Ñ7¸×8OÑ8OÖPØ"õð  ×0Ñ0´KÔ?Ø!×-Ñ-Ò5Ü"0Ð1JÓ"KÐKØ%+×%7Ô%7˜EÙ+¨EÖ2ò &8ñ (¨Ô/÷% JÑIñ "V÷R Ü˜HŸL™L›NÖ+äˆt×ÑÓ  AÔ%Ü Ð!<Ó=Ð=ð 	×Ñ˜DŸL™L¨&Ö1ùòO?
÷| J×IÑIûô& ô Ü—‘ØrØØôô
 —‘Ü*Ø×6Ñ6Ø*3ôôð �ûðús>   ÁH.Ã?IÄB!H3Æ<IÈ3IÈ>IÉIÉJÉAJÊJ)r  r  rú  r  r  r-  r  r  r  )rå  r�  )rJ   rK   rL   rM   rÍ  rÛ   r"  r.  ré  rß  r:  r@  rM  rS   rT   rÎ  rÏ  s   @@r>   r  r  ª	  sd   ù‡ € ñõ&÷ %Lð %L÷N:Gõ :G÷xGõ G÷2Jò Jò.
÷Vð V÷~2÷ ~2ò ~2rA   r  c                   óL   a a€ ] tR tRt oRtRV3R lV 3R llltR tRtVtV ;t	# )r$   ij  aM  
Breadth-First Pipeline Parallelism.
See https://arxiv.org/abs/2211.05953 for details.
Similar to Interleaved 1F1B, Looped BFS supports multiple stages per rank.
What is different is that when microbatches are ready for multiple local
stages, Loops BFS will prioritizes the earlier stage, running all available
microbatches at once.
c                ó¶   <€ V ^8„  d   QhRS[ S[,          RS[RS[S[,          R,          RS[S[S[3,          S[S[,          ,          R,          RS[	RS[	RS[	/# )	r7   rö   rÇ   rÈ   NrË   rÌ   r¤  r  )
r”   r   r‚   r   r   r“   r;   r   r~   ro   )r<   r=   s   "€r>   r?   ÚScheduleLoopedBFS.__annotate__t  sw   ø€ ÷ ?ñ ?áÑ'Õ(ð?ñ ð?ñ ™EÕ! DÕ(ð	?ñ
  ¡¡S �>©E±#­JÕ6¸Õ=ð?ñ ð?ñ %)ð?ñ ñ?rA   c           
     óî   <€ \         S
V `  VVVVVVVR 7       / V n        \        V P                  4       F"  pV P                  V4      p	W�P                  V&   K$  	  V P                  V P                  4       R# ))rö   rÇ   rÈ   rË   rÌ   r¤  r  N)r²  rÛ   r‘   r¬   rò  Ú!_calculate_single_rank_operationsr.  )r\   rö   rÇ   rÈ   rË   rÌ   r¤  r  r¶   Úrank_opsrÚ   s   &&&&&&&&  €r>   rÛ   ÚScheduleLoopedBFS.__init__t  s�   ø€ ô 	‰ÑØØ)ØØ/Ø#Ø'AØ'ð 	ô 	
ð @BˆÔä˜$×,Ñ,Ö-ˆDØ×=Ñ=¸dÓCˆHØ(0×Ñ Ó%ñ .ð
 	×)Ñ)¨$×*=Ñ*=Ö>rA   c           	     ó2  a€ \        V P                  4      p\        WP                  V,          V P                  4      p\        V4       Uu. uF  pR NK  	  ppV F1  oVP	                  V3R l\        V P
                  4       4       4       K3  	  ^V P                  ^,
          V,
          ,          pVP	                  R .V,          4       \        V4       F:  oVP	                  V3R l\        \        V P
                  4      4       4       4       K<  	  V# u upi )Nc              3   óZ   <"  € T F   p\        S\        P                  V4      x € K"  	  R # 5irY   )rW   r)   rN   ©r—   rã   rh   s   & €r>   r™   ÚFScheduleLoopedBFS._calculate_single_rank_operations.<locals>.<genexpr>Ÿ  s+   øé € ð á ;�Hô ˜Ô%5×%=Ñ%=¸x×HÐHÛ ;ùó   ƒ(+c              3   óZ   <"  € T F   p\        S\        P                  V4      x € K"  	  R # 5irY   )rW   r)   rQ   ra  s   & €r>   r™   rb  ª  s+   øé € ð á E�Hô ˜Ô%5×%CÑ%CÀX×NÐNÛ Eùrc  )rƒ   r«  r¬   rò  rõ   rÑ   r  )r\   r¶   Ún_local_stagesÚstage_indicesrš  r]  Úpost_warmup_opsrh   s   &&     @r>   r\  Ú3ScheduleLoopedBFS._calculate_single_rank_operations”  sí   ø€ Ü˜TŸ\™\Ó*ˆÜØ×$Ñ$ ~Õ5°t×7IÑ7Ió
ˆô 9>¸d¼Ó)D¹°1«$¹ˆÐ)Dã(ˆKØ�O‰Oô ä % d×&:Ñ&:Ô ;óö ñ )ð ˜t×1Ñ1°AÕ5¸Õ<Õ=ˆØ�‰˜˜ Õ0Ô1ä# MÖ2ˆKØ�O‰Oô ä (¬¨t×/CÑ/CÓ)DÔ Eóö ñ 3ð
 ˆùò% *Es   ÁD)r‘   )NNTTF©
rJ   rK   rL   rM   rÍ  rÛ   r\  rS   rT   rÎ  rÏ  s   @@r>   r$   r$   j  s   ù‡ € ñ÷?õ ?÷@ò rA   c
                 óf  € \        \        4      p
\        \        4      p\        \        4      p\        V4       Uu. uF  pR NK  	  ppW,          ^V^,
          V,
          ,          ,           W%,           ,
          pV	'       d   W,
          ^,
          pW#,           V,           p. p^ pV	'       d   \        M\        p\        V4       EFt  pVV8  ds   V! V4      pV
V,          ;p^,           V
V&   VP                  \        V\        P                  V4      4       VV^,
          8X  d   VP                  R .V,          4       K{  K}  VTu;8:  d   W#,           8  Ed   M EMV! V4      pV
V,          ;p^,           V
V&   VP                  \        V\        P                  V4      4       V! V4      pVV,          ;p^,           VV&   VP                  \        VVV4      4       VP                  V4       V	'       dl   VV,
          V8¼  d[   V! VV,          4      pVV,          ;p^,           VV&   VP                  \        V\        P                  V4      4       V^,          pEK˜  EK›  EKž  V	'       g   VP                  R 4       V! V4      pVV,          ;p^,           VV&   VP                  \        VVV4      4       VP                  V4       V	'       g   EK  VV,
          V8¼  g   EK  V! VV,          4      pVV,          ;p^,           VV&   VP                  \        V\        P                  V4      4       V^,          pEKw  	  V	'       dj   V\        V4      8  dZ   V! VV,          4      pVV,          ;p^,           VV&   VP                  \        V\        P                  V4      4       V^,          pKq  V# u upi rY   )r   r‚   r¬   rO   rQ   r}   rW   r)   rN   rõ   rP   rƒ   )re  rò  Ú
warmup_opsÚfwd_bwd_opsÚcooldown_opsr¶   Úforward_stage_indexÚbackward_stage_indexÚnum_1f1b_microbatchesÚenable_zero_bubbleÚfwd_stage_mb_indexÚbwd_stage_mb_indexÚweight_stage_mb_indexrš  r]  rg  Ú	total_opsÚbackward_op_idsÚweight_op_countÚFULL_BACKWARD_OR_BACKWARD_INPUTr�  Úfwd_stage_indexrã   r  Úbwd_stage_indexr  Úweight_stage_indexÚweight_mb_indexs   &&&&&&&&&&                  r>   Ú_get_1f1b_rank_opsr}  ±  s­  € ô *5´SÓ)9ÐÜ)4´SÓ)9ÐÜ,7¼Ó,<Ðô 5:¸$´KÓ%@±K¨q£d±K€HÐ%@ð 	Õ&¨¨m¸aÕ.?À$Õ.FÕ)GÕGØ	Õ	õ€O÷ Ø'Õ.°Õ2ˆàÕ(¨<Õ7€Ià€OØ€O÷ -�´-ð $ô �I×ˆà�
Œ?Ù1°"Ó5ˆOð /¨Õ?Ð?�Øõ3Ð˜Ñ/ð �O‰OÜ˜Ô)9×)AÑ)AÀ8ÓLôð �Z !•^Ô#à—‘  ¨Õ 8Ö9ñ $ð ˜2Ö8 
Õ 8×8Ñ8Ù1°"Ó5ˆOà 2°?Õ CÐC�Øõ3Ð˜Ñ/ð �O‰OÜ˜Ô)9×)AÑ)AÀ<ÓPôñ 3°2Ó6ˆOà 2°?Õ CÐC�Øõ3Ð˜Ñ/ð �O‰OÜ˜Ð)HÈ,ÓWôð ×"Ñ" 2Ô&ç! b¨:¥oÐ9NÔ&NÙ%9Ø# OÕ4ó&Ð"ð (=Ð=OÕ'PÐP�OØõ=Ð%Ð&8Ñ9ð —‘ÜØ*Ü(×8Ñ8Ø'óôð   1Õ$“ò 'OÒ!÷& &Ø—‘ Ô%á2°2Ó6ˆOà 2°?Õ CÐC�Øõ3Ð˜Ñ/ð �O‰OÜ˜Ð)HÈ,ÓWôð ×"Ñ" 2Ô&ç!Ò! b¨:¥oÐ9N×&NÙ%9Ø# OÕ4ó&Ð"ð (=Ð=OÕ'PÐP�OØõ=Ð%Ð&8Ñ9ð —‘ÜØ*Ü(×8Ñ8Ø'óôð   1Õ$“ñ[ ÷^  ´3°Ó3GÔ!GÙ1°/À/Õ2RÓSÐà4Ð5GÕHÐHˆOØõ5ÐÐ0Ñ1ð 	�‰ÜØ"Ô$4×$DÑ$DÀoóô	
ð
 	˜1ÕŠà€Oùòg &As   »N.c                ól   € V ^8„  d   QhR\         R\         R\         R\         R\         R\         R\         /# )r7   r¶   re  Úmicrobatches_per_roundrò  rÇ   Úmultiply_factorr9   r4  )r<   s   "r>   r?   r?   :  sN   € ÷ <ñ <Ü
ð<äð<ô  ð<ô ð	<ô
 ð<ô ð<ô 	ñ<rA   c                ó€   € V^,
          V,          pWeV^,
          V ,
          ,          ,           p\        WtV,          4      # )zF
Calculate the number of warmup operations for interleaved schedules.
)r   )r¶   re  r  rò  rÇ   r€  Úwarmups_ops_last_stagerk  s   &&&&&&  r>   Ú_get_warmup_opsrƒ  :  s=   € ð -¨qÕ0Ð4JÕJÐà'¸mÈaÕ>OÐSWÕ=WÕ*XÕX€Jäˆz¨NÕ:Ó;Ð;rA   c                   óX   a a€ ] tR tRt oRtRV3R lV 3R llltV3R lR ltRtVtV ;t	# )	r#   iM  aQ  
The Interleaved 1F1B schedule.
See https://arxiv.org/pdf/2104.04473 for details.
Will perform one forward and one backward on the microbatches in steady
state and supports multiple stages per rank. When microbatches are ready for
multiple local stages, Interleaved 1F1B prioritizes the earlier microbatch
(also called "depth first").

This schedule is mostly similar to the original paper.
It differs by being relaxing the requirement of num_microbatch % pp_size == 0.
Using the flex_pp schedule, we will have num_rounds = max(1, n_microbatches // pp_group_size) and
it works as long as n_microbatches % num_rounds is 0. As a few examples, support

1. pp_group_size = 4, n_microbatches = 10. We will have num_rounds = 2 and n_microbatches % 2 is 0.
2. pp_group_size = 4, n_microbatches = 3. We will have num_rounds = 1 and n_microbatches % 1 is 0.
c                óø   <€ V ^8„  d   QhRS[ S[,          RS[RS[R,          RS[S[R3,          R,          RS[S[S[3,          R,          RS[S[S[3,          S[S[,          ,          R,          R	S[	R
S[	RS[	/	# ©r7   rö   rÇ   rÈ   NrÉ   .rÊ   rË   rÌ   r¤  r  r¥  )r<   r=   s   "€r>   r?   Ú$ScheduleInterleaved1F1B.__annotate___  s£   ø€ ÷ +?ñ +?áÑ'Õ(ð+?ñ ð+?ñ ˜D•ð	+?ñ
 ™°Ð3Õ4°tÕ;ð+?ñ  ¡¡_Ð 4Õ5¸Õ<ð+?ñ  ¡¡S �>©E±#­JÕ6¸Õ=ð+?ñ ð+?ñ %)ð+?ñ ñ+?rA   c
                óH  <€ V^ ,          P                   V n        \        SV `  VVVVVVVVV	R7	       \	        V4      V n        V^ ,          P                  V n        \        ^W P                  ,          4      V n	        W P                  ,          V n
        W P                  ,          ^ 8w  d   \        RV P                   RV R24      h/ V n        \        V P                  4       F"  p
V P                  V
4      pW°P                  V
&   K$  	  V P                  V P                  4       R# )r   ©	rö   rÇ   rÈ   rÉ   rÊ   rË   rÌ   r¤  r  z_Interleaved 1F1B requires the number of microbatches to be a multiple of the number of rounds (ú), but got Ú.N)r¬  rò  r²  rÛ   rƒ   re  r­  r¶   r­   Únumber_of_roundsr  rD   r‘   r¬   r\  r.  ©r\   rö   rÇ   rÈ   rÉ   rÊ   rË   rÌ   r¤  r  r¶   r]  rÚ   s   &&&&&&&&&&  €r>   rÛ   Ú ScheduleInterleaved1F1B.__init___  s#  ø€ ð $ A�Y×1Ñ1ˆÔÜ‰ÑØØ)ØØ+Ø/Ø/Ø#Ø'AØ'ð 	ô 
	
ô " &›kˆÔØ˜1•I×(Ñ(ˆŒ	Ü # A ~×9KÑ9KÕ'KÓ LˆÔØ&4×8MÑ8MÕ&MˆÔ#Ø×1Ñ1Õ1°QÔ6Üð5Ø59×5JÑ5JÐ4Kð LØ)Ð*¨!ð-óð ð @BˆÔÜ˜$×,Ñ,Ö-ˆDØ×=Ñ=¸dÓCˆHØ(0×Ñ Ó%ñ .ð
 	×)Ñ)¨$×*=Ñ*=Ö>rA   c                ó>   <€ V ^8„  d   QhRS[ S[R,          ,          /# rÈ  rL  )r<   r=   s   "€r>   r?   r‡  Œ  s   ø€ ÷ 2
ñ 2
¹¹gÈ½nÕ9Mñ 2
rA   c           
     ó”  a aa€ \        SS P                  S P                  S P                  S P                  ^R7      oS P                  S P                  ,          pVS,
          pW#,
          pSV,           V,           p\
        P                  RSSVVV4       VV 3R lpVV V3R lp\        S P                  S P                  SVVSVV4      # )r7   ©r€  ú=rank %s, warmup_ops %s, 1f1b %s, cooldown_ops %s total_ops %sc                 ó€   <€ V SP                   ,          SP                  ,          pVSP                  ,          S,           # rY   ©r  re  rò  ©rQ  Úlocal_indexr¶   r\   s   & €€r>   rn  ÚVScheduleInterleaved1F1B._calculate_single_rank_operations.<locals>.forward_stage_index§  ó4   ø€ à 4×#>Ñ#>Õ>À$×BUÑBUÕUˆKØ $×"4Ñ"4Õ4¸Õ<Ð<rA   c                 ó¾   <€ SP                   ^,
          V S,
          SP                  ,          SP                   ,          ,
          pVSP                  ,          S,           # rw  ©re  r  rò  ©rQ  r–  r¶   r\   rk  s   & €€€r>   ro  ÚWScheduleInterleaved1F1B._calculate_single_rank_operations.<locals>.backward_stage_index¬  óW   ø€ à×#Ñ#Øõà˜:Õ%¨$×*EÑ*EÕEØ×%Ñ%õ&õ&ð ð   $×"4Ñ"4Õ4¸Õ<Ð<rA   ©rƒ  re  r  rò  rÑ   rØ   r  r}  )	r\   r¶   Úmicrobatch_opsrl  rm  ru  rn  ro  rk  s	   ff      @r>   r\  Ú9ScheduleInterleaved1F1B._calculate_single_rank_operationsŒ  sË   ú€ Ü$ØØ×ÑØ×'Ñ'Ø×ÑØ× Ñ Øô
ˆ
ð ×,Ñ,¨t×/CÑ/CÕCˆà$ zÕ1ˆà%Õ3ˆà Õ,¨|Õ;ˆ	ä�‰ØKØØØØØô	
ö	=÷
	=ô "Ø×ÑØ×ÑØØØØØØ ó	
ð 		
rA   ©r  re  rŒ  r‘   rò  r¶   ©NNNNTTFri  rÏ  s   @@r>   r#   r#   M  s$   ù‡ € ñ÷"+?õ +?÷Z2
÷ 2
ð 2
rA   c                   ó^   a a€ ] tR tRt oRtR	V3R lV 3R llltV3R lR ltR tRtVt	V ;t
# )
r%   iÁ  a[  
The Interleaved Zero Bubble schedule.
See https://arxiv.org/pdf/2401.10241 for details.
Will perform one forward and one backward on inputs for the microbatches in steady
state and supports multiple stages per rank. Uses the backward for weights to fill in
the pipeline bubble.

In particular this is implementing the ZB1P schedule in the paper.
c                óø   <€ V ^8„  d   QhRS[ S[,          RS[RS[R,          RS[S[R3,          R,          RS[S[S[3,          R,          RS[S[S[3,          S[S[,          ,          R,          R	S[	R
S[	RS[	/	# r†  r¥  )r<   r=   s   "€r>   r?   Ú*ScheduleInterleavedZeroBubble.__annotate__Ì  s£   ø€ ÷ 4?ñ 4?áÑ'Õ(ð4?ñ ð4?ñ ˜D•ð	4?ñ
 ™°Ð3Õ4°tÕ;ð4?ñ  ¡¡_Ð 4Õ5¸Õ<ð4?ñ  ¡¡S �>©E±#­JÕ6¸Õ=ð4?ñ ð4?ñ %)ð4?ñ ñ4?rA   c
                óè  <€ \        WP                  P                  4       V^ ,          P                  V n        \
        SV `  VVVVVVVVV	R7	       \        V4      V n        V^ ,          P                  V n
        \        ^W P                  ,          4      V n        W P                  ,          V n        W P                  ,          ^ 8w  d   \        RV P                   RV R24      h/ V n        \!        V P                  4       F"  p
V P#                  V
4      pW°P                  V
&   K$  	  V P%                  V P                  V P                  ,          4      V n        V P'                  V P                  4       R# )r   r‰  zZZero bubble requires the number of microbatches to be a multiple of the number of rounds (rŠ  r‹  N)Ú"_check_torch_compile_compatibilityrÚ   rJ   r¬  rò  r²  rÛ   rƒ   re  r­  r¶   r­   rŒ  r  rD   r‘   r¬   r\  Ú_add_bubbles_to_actionsr.  r�  s   &&&&&&&&&&  €r>   rÛ   Ú&ScheduleInterleavedZeroBubble.__init__Ì  s^  ø€ ô 	+¨6·>±>×3JÑ3JÔKØ# A�Y×1Ñ1ˆÔÜ‰ÑØØ)ØØ+Ø/Ø/Ø#Ø'AØ'ð 	ô 
	
ô " &›kˆÔØ˜1•I×(Ñ(ˆŒ	Ü # A ~×9KÑ9KÕ'KÓ LˆÔØ&4×8MÑ8MÕ&MˆÔ#Ø×1Ñ1Õ1°QÔ6Üð5Ø59×5JÑ5JÐ4Kð LØ)Ð*¨!ð-óð ð @BˆÔÜ˜$×,Ñ,Ö-ˆDØ×=Ñ=¸dÓCˆHØ(0×Ñ Ó%ñ .ð #×:Ñ:Ø×Ñ $×"4Ñ"4Õ4ó
ˆÔð
 	×)Ñ)¨$×*=Ñ*=Ö>rA   c                ó>   <€ V ^8„  d   QhRS[ S[R,          ,          /# rÈ  rL  )r<   r=   s   "€r>   r?   r¥    s   ø€ ÷ 7
ñ 7
¹¹gÈ½nÕ9Mñ 7
rA   c                óž  a aa	€ \        SS P                  S P                  S P                  S P                  ^R7      o	S P                  S P                  ,          pVS	,
          pW#,
          pS	V,           V,           p\
        P                  RSS	VVV4       VV 3R lpVV V	3R lpSp\        S P                  S P                  S	VVSVVVRR7
      # )r  r‘  r’  c                 ó€   <€ V SP                   ,          SP                  ,          pVSP                  ,          S,           # rY   r”  r•  s   & €€r>   rn  Ú\ScheduleInterleavedZeroBubble._calculate_single_rank_operations.<locals>.forward_stage_index  r˜  rA   c                 ó¾   <€ SP                   ^,
          V S,
          SP                  ,          SP                   ,          ,
          pVSP                  ,          S,           # rw  rš  r›  s   & €€€r>   ro  Ú]ScheduleInterleavedZeroBubble._calculate_single_rank_operations.<locals>.backward_stage_index#  r�  rA   T)rq  rž  )
r\   r¶   rŸ  rl  rm  ru  rn  ro  rp  rk  s
   ff       @r>   r\  Ú?ScheduleInterleavedZeroBubble._calculate_single_rank_operations  s×   ú€ Ü$ØØ×ÑØ×'Ñ'Ø×ÑØ× Ñ Øô
ˆ
ð ×,Ñ,¨t×/CÑ/CÕCˆà$ zÕ1ˆà%Õ3ˆà Õ,¨|Õ;ˆ	ä�‰ØKØØØØØô	
ö	=÷
	=ð !%Ðä!Ø×ÑØ×ÑØØØØØØ Ø!Ø#ô
ð 	
rA   c                óœ  € V P                   pR  p\        4       p/ p/ p/ p^ p\        V P                  4       F  p	. WY&   ^ Wi&   ^ Wy&   K  	   Rp
\        4       p\        V P                  4       EF   p	Wi,          pV\	        W),          4      8¼  d   K$  Rp
W),          V,          e¿   W),          V,          pVf   \        R\        V4       24      hVw  rïppV! WïVW4      '       gR   WY,          P                  W),          V,          4       Ve   VP                  WïV34       Wi;;,          ^,          uu&   KÈ  WY,          P                  R4       Wy;;,          ^,          uu&   Kõ  Wi;;,          ^,          uu&   WY,          P                  R4       EK#  	  VP                  V4       V
'       g   EKc   T^ 8”  d   \        P                  RTT4       T# )c                 óò   € V\         P                  8X  d   V ^ 8w  d   V ^,
          W3V9  d   R# R# V\         P                  8X  d2   W^,
          8X  d   V \         P                  V3V9  # V ^,           W3V9  # R# )r   TF)r)   rN   rQ   )rà   r�  Ú
microbatchÚnum_stages_globalÚseen_opss   &&&&&r>   Úneed_bubbleÚJScheduleInterleavedZeroBubble._add_bubbles_to_actions.<locals>.need_bubble>  s}   € ØÔ%×-Ñ-Ô-Ø˜A”: 5¨1¥9¨bÐ"=ÀXÔ"MÙñ
 ð	 Ô'×5Ñ5Ô5Ø°Õ1Ô1Ø!Ô#3×#;Ñ#;¸ZÐHÐPXÑXÐXØ �	 2Ð2¸(ÑBÐBÙrA   TFNz)Expected temp_action to be not None, got z?Non zero bubbles added: total_bubbles_added=%s bubbles_added=%s)r‘   r,  r¬   rò  rƒ   r4  r.  r}   r-  ÚupdaterØ   r³  )r\   r´  r˜   r¶  rµ  r   Únext_pointerÚbubbles_addedÚtotal_bubbles_addedr¶   Úshould_stopÚtemp_seen_opsÚ	timestampÚtemp_actionrh   r�  r³  rš  s   &&                r>   r¨  Ú5ScheduleInterleavedZeroBubble._add_bubbles_to_actions;  s·  € Ø×%Ñ%ˆò	ô <?»5ˆØ24ˆØ')ˆØ(*ˆØÐä˜$×,Ñ,Ö-ˆDØˆF‰LØ!"ˆLÑØ"#ˆMÓñ .ð
 ØˆKäDGÃEˆMä˜d×0Ñ0×1�Ø(Õ.�	Ø¤ G¥MÓ 2Ô2Ùà#�à•= Õ+Ò7Ø")¥-°	Õ":�KØ"Ò*Ü,ØGÌÈ[ÓHYÐGZÐ[óð ð 6AÑ2�K Z°Ù&Ø#¨Ð5F÷ò ð �×+Ñ+¨G­M¸)Õ,DÔEØ%Ò1Ø)×-Ñ-¨{À
Ð.KÔLØ$×*¨aÕ/Õ*à�×+Ñ+¨DÔ1Ø%×+¨qÕ0Õ+à ×&¨!Õ+Ó&Ø•L×'Ñ'¨×-ñ5 2ð8 �O‰O˜MÔ*ßŠ{Øà Ô"Ü�N‰NØQØ#Øôð
 ˆrA   r¡  r¢  )rJ   rK   rL   rM   rÍ  rÛ   r\  r¨  rS   rT   rÎ  rÏ  s   @@r>   r%   r%   Á  s,   ù‡ € ñ÷4?õ 4?÷l7
ð 7
÷rCò CrA   c                   óX   a a€ ] tR tRt oRtRV3R lV 3R llltV3R lR ltRtVtV ;t	# )	r&   i�  a{  
The Zero Bubble schedule (ZBV variant).
See https://arxiv.org/pdf/2401.10241 Section 6 for details.

This schedules requires exactly two stages per rank.

This schedule will perform one forward and one backward on inputs for the microbatches in steady
state and supports multiple stages per rank. Uses backward with respect to weights to fill in
the pipeline bubble.

This ZB-V schedule would have the "zero bubble" property only if time forward == time backward input == time backward weights.
In practice, this is not likely true for real models so alternatively
a greedy scheduler could be implemented for unequal/unbalanced time.
c                óø   <€ V ^8„  d   QhRS[ S[,          RS[RS[R,          RS[S[R3,          R,          RS[S[S[3,          R,          RS[S[S[3,          S[S[,          ,          R,          R	S[	R
S[	RS[	/	# r†  r¥  )r<   r=   s   "€r>   r?   Ú"ScheduleZBVZeroBubble.__annotate__‘  s£   ø€ ÷ 3?ñ 3?áÑ'Õ(ð3?ñ ð3?ñ ˜D•ð	3?ñ
 ™°Ð3Õ4°tÕ;ð3?ñ  ¡¡_Ð 4Õ5¸Õ<ð3?ñ  ¡¡S �>©E±#­JÕ6¸Õ=ð3?ñ ð3?ñ %)ð3?ñ ñ3?rA   c
                óÊ  <€ \        WP                  P                  4       V^ ,          P                  V n        \
        SV `  VVVVVVVVV	R7	       \        V P                  V P                  RR7      V n	        V P                   F  p
V P                  V
n	        K  	  \        V4      V n        V P                  ^8w  d   \        RV P                   R24      hV^ ,          P                  V n        V^ ,          P                   V n        / V n        \%        V P                  4       F"  pV P'                  V4      pWÀP"                  V&   K$  	  V P)                  V P"                  4       R# )r   r‰  r„  ©Ústyleú0ZBV requires exactly 2 stages per rank, but got r‹  N©r§  rÚ   rJ   r¬  rò  r²  rÛ   r   rµ  r®  r«  rƒ   re  rD   r­  r¶   r´  r‘   r¬   r\  r.  ©r\   rö   rÇ   rÈ   rÉ   rÊ   rË   rÌ   r¤  r  rà   r¶   r]  rÚ   s   &&&&&&&&&&   €r>   rÛ   ÚScheduleZBVZeroBubble.__init__‘  sO  ø€ ô 	+¨6·>±>×3JÑ3JÔKØ# A�Y×1Ñ1ˆÔÜ‰ÑØØ)ØØ+Ø/Ø/Ø#Ø'AØ'ð 	ô 
	
ô *HØ×Ñ × 0Ñ 0¸ô*
ˆÔ&ð —\”\ˆEØ.2×.LÑ.LˆEÖ+ñ "ô " &›kˆÔØ×Ñ !Ô#ÜØBØ×&Ñ&Ð' qð*óð ð
 ˜1•I×(Ñ(ˆŒ	Ø  �)×.Ñ.ˆŒð
 @BˆÔÜ˜$×,Ñ,Ö-ˆDØ×=Ñ=¸dÓCˆHØ(0×Ñ Ó%ñ .ð
 	×)Ñ)¨$×*=Ñ*=Ö>rA   c                ó>   <€ V ^8„  d   QhRS[ S[R,          ,          /# rÈ  rL  )r<   r=   s   "€r>   r?   rÃ  Æ  s    ø€ ÷ zñ z¹¹gÈ½nÕ9Mñ zrA   c           	     ó4	  € \        ^V P                  ,          ^,
          V P                  4      p\        V4       Uu. uF  pRNK  	  ppR	w  rVrx^V P                  V,
          ,          ^,
          p	Tp
V P                  ^,
          V,
          p\        V	4       F-  pVP                  \        V
\        VR7      4       V^,          pK/  	  Tp\        V4       FW  pVP                  \        V\        VR7      4       V^,          pVP                  \        V
\        VR7      4       V^,          pKY  	  V P                  V,
          p\        V4       Fx  pVP                  \        V\        VR7      4       V^,          pVP                  \        V\        VR7      4       VP                  \        V\        VR7      4       V^,          pKz  	  We8  g   WR8  dó   WR8  d+   VP                  \        V
\        VR7      4       V^,          pVP                  \        V
\        VR7      4       VP                  \        V
\        VR7      4       V^,          pVP                  \        V\        VR7      4       V^,          pVP                  \        V\        VR7      4       VP                  \        V\        VR7      4       V^,          pKþ  YxrþTp\        V4       FW  pVP                  \        V
\        VR7      4       V^,          pVP                  \        V\        VR7      4       V^,          pKY  	  V P                  V,
          p\        V4       FW  pVP                  \        V
\        VR7      4       V^,          pVP                  \        V
\        VR7      4       V^,          pKY  	  Wø8  d-   VP                  \        V\        VR7      4       V^,          pK2  Wç8  d-   VP                  \        V
\        VR7      4       V^,          pK2  Wç8X  d   Wu8X  g   \        RV RV RV 24      hWø8X  d   W†8X  g   \        RV RV RV 24      hV Uu. uF4  pVe,   VP                  e   VP                  V P                  8  d   TMRNK6  	  ppV# u upi u upi )
r7   N)rf   ri   z0Expected w0_cnt == b0_cnt == f0_cnt, got w0_cnt=z	, b0_cnt=z	, f0_cnt=z0Expected w1_cnt == b1_cnt == f1_cnt, got w1_cnt=z	, b1_cnt=z	, f1_cnt=)r   r   r   r   )r­   rò  rÑ   r¬   r´  r}   rW   r*   r+   r,   r4  ri   )r\   r¶   Ún_microrš  r]  Úf0_cntÚf1_cntÚb0_cntÚb1_cntÚ	warmup_n1Ústage_id_chunk0Ústage_id_chunk1Ú	warmup_n2Ú	warmup_n3Úw0_cntÚw1_cntÚcooldown_n1Úcooldown_n2r8   s   &&                 r>   r\  Ú7ScheduleZBVZeroBubble._calculate_single_rank_operationsÆ  sL  € ô �a˜$×,Ñ,Õ,¨qÕ0°$×2FÑ2FÓGˆÜ8=¸d¼Ó)D¹°1«$¹ˆÐ)Dð *4Ñ&ˆ˜à˜×+Ñ+¨dÕ2Õ3°aÕ7ˆ	ØˆØŸ/™/¨AÕ-°Õ4ˆä�yÖ!ˆAØ�O‰OÜ˜¼!ÈfÔUôð �a�KŠFñ	 "ð
 ˆ	Ü�yÖ!ˆAØ�O‰OÜ˜¼!ÈfÔUôð �a�KˆFØ�O‰OÜ˜¼!ÈfÔUôð �a�KŠFñ "ð ×&Ñ&¨Õ-ˆ	Ü�yÖ!ˆAØ�O‰OÜ˜¼!ÈfÔUôð �a�KˆFØ�O‰OÜ˜¼!ÈfÔUôð �O‰OÜ˜¼!ÈfÔUôð �a�KŠFñ "ð Œo Ô!1ØÔØ—‘ÜØ'¼!Èfôôð
 ˜!•�Ø�O‰OÜ˜¼!ÈfÔUôð �O‰OÜ˜¼!ÈfÔUôð �a�KˆFà�O‰OÜ˜¼!ÈfÔUôð �a�KˆFØ�O‰OÜ˜¼!ÈfÔUôð �O‰OÜ˜¼!ÈfÔUôð �a�KŠFà�ØˆÜ�{Ö#ˆAØ�O‰OÜ˜¼!ÈfÔUôð �a�KˆFØ�O‰OÜ˜¼!ÈfÔUôð �a�KŠFñ $ð ×(Ñ(¨4Õ/ˆÜ�{Ö#ˆAØ�O‰OÜ˜¼!ÈfÔUôð �a�KˆFØ�O‰OÜ˜¼!ÈfÔUôð �a�KŠFñ $ð ŒoØ�O‰OÜ˜¼!ÈfÔUôð �a�KŠFØŒoØ�O‰OÜ˜¼!ÈfÔUôð �a�KŠFàÔ  VÔ%5Ü ØBÀ6À(È)ÐTZÐS[Ð[dÐekÐdlÐmóð ð Ô  VÔ%5Ü ØBÀ6À(È)ÐTZÐS[Ð[dÐekÐdlÐmóð ñ #ó	
ñ #�ð Ò%Ø×+Ñ+Ò7Ø×+Ñ+¨d×.BÑ.BÔBñ ð ò	ñ #ð 	ð 	
ð ˆùòm *EùòX	
s   ¼RÑ:R©re  r´  r‘   rò  r¶   r®  r¢  ri  rÏ  s   @@r>   r&   r&   �  s'   ù‡ € ñ÷3?õ 3?÷jz÷ zð zrA   c                   óX   a a€ ] tR tRt oRtRV3R lV 3R llltV3R lR ltRtVtV ;t	# )	r'   iC  zâ
The DualPipeV schedule. A more efficient schedule variant based on the
DualPipe schedule introduced by DeepSeek in https://arxiv.org/pdf/2412.19437

Based on the open sourced code from https://github.com/deepseek-ai/DualPipe
c                óø   <€ V ^8„  d   QhRS[ S[,          RS[RS[R,          RS[S[R3,          R,          RS[S[S[3,          R,          RS[S[S[3,          S[S[,          ,          R,          R	S[	R
S[	RS[	/	# r†  r¥  )r<   r=   s   "€r>   r?   ÚScheduleDualPipeV.__annotate__K  s£   ø€ ÷ 8?ñ 8?áÑ'Õ(ð8?ñ ð8?ñ ˜D•ð	8?ñ
 ™°Ð3Õ4°tÕ;ð8?ñ  ¡¡_Ð 4Õ5¸Õ<ð8?ñ  ¡¡S �>©E±#­JÕ6¸Õ=ð8?ñ ð8?ñ %)ð8?ñ ñ8?rA   c
                ó"  <€ \        WP                  P                  4       V^ ,          P                  V n        \
        SV `  VVVVVVVVV	R7	       \        V P                  V P                  RR7      V n	        V P                   F  p
V P                  V
n	        K  	  \        V4      V n        V P                  ^8w  d   \        RV P                   R24      hW P                  8  d   \        RV RV P                   R24      hV^ ,          P                  V n        V^ ,          P                   V n        / V n        \%        V P                  4       F"  pV P'                  V4      pWÀP"                  V&   K$  	  V P)                  V P"                  4       R	# )
r   r‰  r„  rÅ  rÇ  r‹  zDDualPipeV requires at least as many microbatches as stages, but got z microbatches and z stages.NrÈ  rÉ  s   &&&&&&&&&&   €r>   rÛ   ÚScheduleDualPipeV.__init__K  s‰  ø€ ô 	+¨6·>±>×3JÑ3JÔKØ# A�Y×1Ñ1ˆÔÜ‰ÑØØ)ØØ+Ø/Ø/Ø#Ø'AØ'ð 	ô 
	
ô *HØ×Ñ × 0Ñ 0¸ô*
ˆÔ&ð —\”\ˆEØ.2×.LÑ.LˆEÖ+ñ "ô " &›kˆÔØ×Ñ !Ô#ÜØBØ×&Ñ&Ð' qð*óð ð ×,Ñ,Ô,ÜØVØ!Ð"Ð"4°T×5EÑ5EÐ4FÀhðPóð ð
 ˜1•I×(Ñ(ˆŒ	Ø  �)×.Ñ.ˆŒð
 @BˆÔÜ˜$×,Ñ,Ö-ˆDØ×=Ñ=¸dÓCˆHØ(0×Ñ Ó%ñ .ð
 	×)Ñ)¨$×*=Ñ*=Ö>rA   c                ó>   <€ V ^8„  d   QhRS[ S[R,          ,          /# rÈ  rL  )r<   r=   s   "€r>   r?   rß  …  s    ø€ ÷ ^ñ ^¹¹gÈ½nÕ9Mñ ^rA   c                ó€  aaa€ . p/ o. oV P                   pV P                  p\        W3^,          RR7      pWQ,          w  rgR V3R lloR VV3R llpR VVV3R llp	R	 VV3R
 llp
W1,
          ^,
          ^,          p\        V4       F  pV	! W&\        4       K  	  V^,           p\        V4       F  pV	! W&\        4       V	! W'\        4       K  	  W1,
          ^,
          p\        V4       F%  pV	! W'\
        4       V
! V4       V	! W'\        4       K'  	  WC^,          ,
          V,           ^,           p\        V4       FH  pV^ 8X  d)   W^,
          8X  d   V	! W&\        4       V	! W'\        4       MV! VVVR7       V! VVVR7       KJ  	  W1,
          ^,
          p\        V4       F  pV	! W'\        4       V! VVVR7       K  	  V^,           pRp\        V4       Fy  pVV^,          8X  d   V^,          ^8X  d   RpV'       d   \
        M\        pV	! W'V4       VV^,          8X  d   V^,          ^ 8X  d   RpV'       d   \
        M\        pV	! W&V4       K{  	  W1,
          ^,
          p\        V4       F(  pV
! V4       V'       d   \
        M\        pV	! W&V4       K*  	  V^,           p\        V4       F  pV
! V4       K  	  V# )r7   r„  rÅ  c                ó$   € V ^8„  d   QhR\         /# r3  r4  )r<   s   "r>   r?   ÚIScheduleDualPipeV._calculate_single_rank_operations.<locals>.__annotate__”  s   € ÷ 	Cñ 	C´3ñ 	CrA   c                ó˜   <€ V \         3pV \        3pSP                  V^ 4      ^,           SV&   SP                  V^ 4      ^,           SV&   R# )zbHelper method to increment BACKWARD_INPUT and BACKWARD_WEIGHT counters when FULL_BACKWARD is used.N)rO   rP   r±   )rh   Ú	input_keyÚ
weight_keyÚcounterss   &  €r>   Úincrement_backward_countsÚVScheduleDualPipeV._calculate_single_rank_operations.<locals>.increment_backward_counts”  sK   ø€ à$¤nÐ5ˆIØ%¤Ð7ˆJØ"*§,¡,¨y¸!Ó"<¸qÕ"@ˆH�YÑØ#+§<¡<°
¸AÓ#>ÀÕ#BˆH�ZÓ rA   c                ó<   € V ^8„  d   QhR\         R\        R\        /# )r7   r˜   Úforward_stageÚbackward_stage)r”   r‚   )r<   s   "r>   r?   rå  ›  s&   € ÷ 	6ñ 	6Üð	6äð	6ô  ñ	6rA   c                ó  <€ V\         3pV\        3pSP                  V^ 4      pSP                  V^ 4      p\        V\         V4      \        V\        V4      3pV P                  \        R\        RV4      4       V^,           SV&   S	! V4       R# )zYHelper method to add an overlapped forward+backward action which tracks microbatch index.Nrx   )rN   rO   r±   rW   rQ   r}   r4   )
r˜   rí  rî  Úforward_keyÚbackward_keyr  r  rc   ré  rê  s
   &&&     €€r>   Úadd_overlap_f_bÚLScheduleDualPipeV._calculate_single_rank_operations.<locals>.add_overlap_f_b›  s‹   ø€ ð )¬'Ð2ˆKØ*¬NÐ;ˆLà!Ÿ™ k°1Ó5ˆJØ"Ÿ,™, |°QÓ7ˆKô ˜¤w°
Ó;Ü˜¬°{ÓCðˆKð �N‰Nœ7 2¤{°D¸+ÓFÔGð %/°¥NˆH�[Ñ!Ù% nÖ5rA   c                ó<   € V ^8„  d   QhR\         R\        R\        /# )r7   r˜   rh   rf   )r”   r‚   r)   )r<   s   "r>   r?   rå  ²  s&   € ÷ 	-ñ 	-Üð	-äð	-ô /ñ	-rA   c                 ó
  <€ V\         8w  d   W3MV\        3pSP                  V^ 4      pV P                  \	        WV4      4       V\         8X  d   S! V4       R# V\        8X  d   SP                  W34       V^,           SV&   R# )r   N)rQ   rO   r±   r}   rW   )r˜   rh   rf   r¹   rã   ré  rê  Úweight_queues   &&&  €€€r>   Ú
add_actionÚGScheduleDualPipeV._calculate_single_rank_operations.<locals>.add_action²  s�   ø€ ð $¤}Ô4ð Ñ/à!¤>Ð2ð ð
  —|‘| C¨Ó+ˆHØ�N‰Nœ7 ;À(ÓKÔLð  ¤=Ô0Ù)¨+Ö6ð $¤~Ô5à ×'Ñ'¨Ð(?Ô@Ø (¨1¥�˜“rA   c                ó$   € V ^8„  d   QhR\         /# )r7   r˜   )r”   )r<   s   "r>   r?   rå  Ê  s   € ÷ 	Cñ 	C´$ñ 	CrA   c                óÊ   <€ S'       g   R# SP                  ^ 4      w  rV P                  \        V\        V4      4       V\        3pSP	                  V^ 4      ^,           SV&   R# )z4Helper method to add a weight action from the queue.N)rN  r}   rW   rP   r±   )r˜   Úactual_stage_indexr|  rè  ré  rö  s   &   €€r>   Úadd_weight_action_if_pendingÚYScheduleDualPipeV._calculate_single_rank_operations.<locals>.add_weight_action_if_pendingÊ  s`   ø€ çÙà2>×2BÑ2BÀ1Ó2EÑ/ÐØ�N‰NÜØ&Ü#Ø#óôð -¬oÐ>ˆJØ#+§<¡<°
¸AÓ#>ÀÕ#BˆH�ZÓ rA   )rí  rî  FT)rò  rÑ   r   r¬   rN   rO   rQ   )r\   r¶   r˜   r¼   Ú
num_chunksÚrank_to_stagesÚstage0_indexÚstage1_indexrò  r÷  rü  Ústep_1rš  Ústep_2Ústep_3Ústep_4r¤   Ústep_5Ústep_6Ú	enable_zbrN  Ústep_7Ústep_8ré  rê  rö  s   &&                     @@@r>   r\  Ú3ScheduleDualPipeV._calculate_single_rank_operations…  s�  ú€ Ø(*ˆð ð 	ð ˆà×&Ñ&ˆ	Ø×)Ñ)ˆ
ä7Ø 1•}¨Cô
ˆð &4Õ%9Ñ"ˆ÷	Cð 	C÷	6ñ 	6÷.	-ò 	-÷0	Cñ 	Cð$ Õ" QÕ&¨!Õ+ˆÜ�v–ˆAÙ�w¬gÖ6ñ ð ˜•ˆÜ�v–ˆAÙ�w¬gÔ6Ù�w¬gÖ6ñ ð
 Õ! AÕ%ˆÜ�v–ˆAÙ�w¬nÔ=Ù(¨Ô1Ù�w¬gÖ6ñ ð ¨!�mÕ+¨dÕ2°QÕ6ˆÜ�v–ˆAØ�AŒv˜$¨a¥-Ô/á˜7´'Ô:Ù˜7´-Õ@áØØ".Ø#/õñ
 ØØ*Ø+÷ñ ð$ Õ! AÕ%ˆÜ�v–ˆAÙ�w¬mÔ<ÙØØ*Ø+÷ñ ð ˜•ˆØˆ	Ü�v–ˆAØ�F˜a•KÔ D¨1¥H°¤MØ �	ß*3�¼ˆIÙ�w¨iÔ8Ø�F˜a•KÔ D¨1¥H°¤MØ �	ß*3�¼ˆIÙ�w¨iÖ8ñ ð Õ! AÕ%ˆÜ�v–ˆAÙ(¨Ô1ß*3�¼ˆIÙ�w¨iÖ8ñ ð ˜•ˆÜ�v–ˆAÙ(¨Ö1ñ ð ˆrA   rÜ  r¢  ri  rÏ  s   @@r>   r'   r'   C  s'   ù‡ € ñ÷8?õ 8?÷t^÷ ^ð ^rA   c                ó$   € V ^8„  d   QhR\         /# )r7   Úschedule_namer:   )r<   s   "r>   r?   r?   &  s   € ÷ Añ A¤cñ ArA   c                óT  € R\         R\        R\        R\        R\        R\
        R\        R\        R	\        /	pV Uu/ uF  q"P                  4       VbK  	  ppV P                  4       pWC9  d)   \        R
V  R\        VP                  4       4       24      hWV,          ,          # u upi )z†
Maps a schedule name (case insensitive) to its corresponding class object.

Args:
    schedule_name (str): The name of the schedule.
Ú1F1BÚInterleaved1F1BÚGPipeÚ	LoopedBFSÚInterleavedZeroBubbler   r    ÚZBVZeroBubbleÚ	DualPipeVzUnknown schedule name 'z'. The valid options are )r!   r#   r"   r$   r%   r   r    r&   r'   ÚlowerrD   r”   Úkeys)r  Úschedule_maprƒ  Úlowercase_keysÚlowercase_schedule_names   &    r>   Úget_schedule_classr  &  s¶   € ð 	”ØÔ2Ø”ØÔ&ØÔ!>Ø Ô"8ØÔ!6ØÔ.ØÔ&ð
€Lñ -9Ó9©L q—g‘g“i ’l©L€NÐ9Ø+×1Ñ1Ó3ÐØÔ4ÜØ% m _Ð4MÌdÐS_×SdÑSdÓSfÓNgÐMhÐió
ð 	
ð Ð'>Õ?Õ@Ð@ùò :s   ½B%c                óT   € V ^8„  d   QhR\         \        .\        3,          R\        /# )r7   rS  r´  )r   r‚   )r<   s   "r>   r?   r?   A  s+   € ÷ Bñ BÜ#+¬S¨E´3¨JÕ#7ðBÜEHñBrA   c           	     óÖ  aaa
a€ \        V 4       UUu/ uF!  pY0V,           Uu. uF
  qDf   K  VNK  	  upbK#  	  p pp\        V 4       Uu/ uF  q3. bK  	  upoS Uu/ uF  q3\        4       bK  	  upo
R V
V3R llpR V
VV3R llpV '       EdÅ   Rp\        V 4       Fh  p\        W,          4      ^ 8X  d   K  W,          ^ ,          pV! V4      '       d(   Ve	   V! W84       W,          P                  ^ 4       RpK_  V! VR4       Kj  	  \        V RR7       F  p	\        W	,          4      ^ 8X  g   K  W	 K  	  \        V 4       F�  p\        W,          4      ^ 8X  d   K  SV,          R,          e   K/  W,          ^ ,          pV! V4      '       g   KN  Ve%   VSV,          R&   S
V,          P	                  V4       W,          P                  ^ 4       K�  	  \        V RR7       F  p	\        W	,          4      ^ 8X  g   K  W	 K  	  V'       d   EK…  \        R	\        S4      4       V  F"  p\        R
V: RW,          ^ ,           24       K$  	  \        R4      hS# u upi u uppi u upi u upi )a¦  This function dry-run simulates the actions in the schedule from the perspective of all ranks, and flags
any deadlocks caused by missing or misordered communications.  It also simulates any bubbles in time where a rank
can not execute any action due to waiting for unmet dependencies.  The total number of simulator steps can be used
as a metric for unit tests involving IR optimization passes as reordering and merging of IR can reduce the number
of simulated steps.

The simulation is not high-fidelity and does not model overlapping of compute and communication, or cuda streams.
Future work may be to enhance this and model the compute time, comms overlap, and even memory.
Nc                ó>   € V ^8„  d   QhR\         R\        R,          /# )r7   r¶   r8   N)r‚   rW   )r<   s   "r>   r?   Ú-_simulate_comms_compute.<locals>.__annotate__W  s   € ÷ -ñ -œcð -¬7°T­>ñ -rA   c                 ót   <€ SV ,          P                  V4       Ve   SV ,          P                  V4       R # R # rY   )r}   r-  )r¶   r8   Ú_prev_ops_rankÚ	_schedules   &&€€r>   Úadd_to_scheduleÚ0_simulate_comms_compute.<locals>.add_to_scheduleW  s3   ø€ Ø�$�×Ñ˜vÔ&ØÒØ˜4Õ ×$Ñ$ VÖ,ñ rA   c                ó>   € V ^8„  d   QhR\         R,          R\        /# )r7   r8   Nr9   rW  )r<   s   "r>   r?   r  \  s"   € ÷ 8Bñ 8B¤7¨T¥>ð 8B´dñ 8BrA   c                 óº  <€ V f   R# V P                   pSS
! V4      ,          pV P                  \        8X  du   V P                   ^ 8X  d   R# \        V P                   \        V P
                  4      V9   d   R# \        V P                   ^,
          \        V P
                  4      V9   d   R# R# V P                  \        \        39   d¯   V P                   S	^,
          8X  d   R# \        V P                   \        V P
                  4      V9   d   R# \        V P                   ^,           \        V P
                  4      V9   d   R# \        V P                   ^,           \        V P
                  4      V9   d   R# R# V P                  \        8X  d   R# V P                  \        8X  d*   \        V P                   \        V P
                  4      pW29   # V P                  \        8X  d7   V^,
          p\        V\        V P
                  4      pVSS
! V4      ,          9   # V P                  \        8X  d[   \        V P                   \        V P
                  4      p\        V P                   \        V P
                  4      pWb9   ;'       g    Wr9   # V P                  \        8X  d7   V^,           p\        V\        V P
                  4      pVSS
! V4      ,          9   # \        RV  24      h)NTFzUnsupported action type )rh   rf   r*   rW   r0   ri   rO   rQ   r2   rP   r/   r1   rD   )r8   r"  Úprev_opsÚ
expected_fÚpeer_stage_idxÚexpected_sendÚ
expected_bÚexpected_bwr!  r´  rS  s   &       €€€r>   rg  Ú3_simulate_comms_compute.<locals>._ready_to_schedule\  sa  ø€ ØŠ>Ùà×&Ñ&ˆ	Ø!¡-°	Ó":Õ;ˆØ×"Ñ"¤aÔ'Ø×!Ñ! QÔ&Ùä˜×*Ñ*¬F°F×4KÑ4KÓLÐPXÔXáä˜×*Ñ*¨QÕ.´°6×3JÑ3JÓKÈxÔWáÙØ×$Ñ$¬¼Ð(GÔGØ×!Ñ! Z°!¥^Ô3ÙÜ�v×)Ñ)¬6°6×3JÑ3JÓKÈxÔWÙä˜×*Ñ*¨QÕ.´À×@WÑ@WÓXØôñ ä˜×*Ñ*¨QÕ.´¸v×?VÑ?VÓWØôñ ÙØ×$Ñ$¬Ô7ÙØ×$Ñ$¬Ô.Ü  ×!3Ñ!3´Q¸×8OÑ8OÓPˆJØÑ)Ð)Ø×$Ñ$¬Ô.Ø&¨�]ˆNÜ# N´F¸F×<SÑ<SÓTˆMØ  N±=ÀÓ3PÕ$QÑQÐQØ×$Ñ$¬Ô.Ü Ø×"Ñ"¤N°F×4KÑ4KóˆJô "Ø×"Ñ"¤M°6×3JÑ3JóˆKð Ñ)×DÐD¨[Ñ-DÐDØ×$Ñ$¬Ô.Ø&¨�]ˆNÜ# N´F¸F×<SÑ<SÓTˆMØ  N±=ÀÓ3PÕ$QÑQÐQäÐ7¸°xÐ@ÓAÐArA   FT)ÚreversezWIP comms schedule:
ri  z next action= zSchedule is not progressingrx   )r°   r,  rƒ   rN  r-  ÚprintrÃ   rD   )r‘   rS  r´  r¶   r  r#  rg  rm  r8   r¤   r!  r"  s   &ff       @@r>   r9  r9  A  sP  û€ ô ˜>Ô*ôá*ˆDð 	¨Ö.Ó@Ñ.�Q�qˆqÑ.Ñ@Ò@Ù*ð ñ ô
 $ NÔ3ó2Ù3�TˆbŠÑ3ñ2€Iñ HQÓ.QÁy¸t´S³Uª{ÁyÑ.Q€N÷-ñ -÷
8Bò 8B÷t ˆ.ØˆÜ˜>Ö*ˆDÜ�>Õ'Ó(¨AÔ-Ùà#Õ)¨!Õ,ˆFÙ! &×)Ò)ØÒ%Ù# DÔ1ØÕ$×(Ñ(¨Ô+Ø’á  dÖ+ñ +ô ˜°×5ˆAÜ�>Õ$Ó%¨Ö*Ø"Ò%ñ 6ô ˜>Ö*ˆDÜ�>Õ'Ó(¨AÔ-Ùà˜�˜rÕ"Ò.Ùà#Õ)¨!Õ,ˆFÙ! &×)Ô)ØÒ%Ø*0�I˜d•O BÑ'Ø" 4Õ(×,Ñ,¨VÔ4ØÕ$×(Ñ(¨Ö+ñ +ô ˜°×5ˆAÜ�>Õ$Ó%¨Ö*Ø"Ò%ñ 6÷ ŠxÜÐ)Ô+AÀ)Ó+LÔMÛ&�Ü˜˜™˜~¨nÕ.BÀ1Õ.EÐ-FÐGÖHñ 'äÐ:Ó;Ð;àÐùòk Aùóùò2ùò /Rs&   “I£I­I³IÁ
I!ÁI&ÉIc                ó”  € . p\        V 4       Fm  p\        W,          4       FU  w  rEVf   K  TP                  R\        V4      RVP                  \
        \        \        39   d   RMRRRRVR	VR
VR^/4       KW  	  Ko  	  ^ RIp\        VR4      ;_uu_ 4       pVP                  RV/V4       RRR4       R#   + '       g   i     R# ; i)a×  
This function dumps a schedule IR into a chrometrace format so it can be visualized.

It is currently very basic and only serves as a graphical alternative to dumping the schedule IR as text.

As future work we may extend this to include more accurate heuristics for durations, or let users input durations,
add 'flow events' to let the UI show the connection between sends and recvs, and model cuda streams for comm/compute
as separate streams on the chrometrace view.
Nr\  ÚcatÚcomputationÚcommunicationÚphÚXÚpidÚtidÚtsÚdurrª  ÚtraceEvents)r°   rµ   r}   r;   rf   r*   r3   r,   ÚjsonrÙ  Údump)ÚschedulerÝ  Úeventsr¶   Útimestepr8   r;  Úfs   &&      r>   Ú_dump_chrometracerA  Æ  s¼   € ð €FÜ�xÖ ˆÜ )¨(­.Ö 9ÑˆHØŠ~ÙØ�M‰MàœC ›KØà!×2Ñ2´q¼!¼Q°iÔ?ñ &à,à˜#Ø˜4Ø˜4Ø˜(Ø˜1ðöó !:ñ !ó& ä	ˆh˜×	Ô	 Ø�	‰	�= &Ð)¨1Ô-÷ 
×	×	Ò	ús   ÂB6Â6C	c                óF   € V ^8„  d   QhR\         \        ,          R\        /# )r7   rö   r  )r”   r   r;   )r<   s   "r>   r?   r?   ê  s"   € ÷ ñ ÜÔ#Õ$ðÜ58ñrA   c           
     ó:  € V  F”  p\        VP                  \        P                  P                  4      '       g   K9  VP                  P                  4        F=  p\        V\        4      '       g   K  \        RV R\        V4      P                   24      h	  K–  	  R# )zâ
Check if the schedule is compatible with torch.compile.

Args:
    stages: List of pipeline stages to check
    schedule_name: Name of the schedule for error message

Raises:
    RuntimeError: If any stage uses torch.compile
zThe ze schedule is not supported with stage modules that have used torch.compile. Found OptimizedModule in N)
rò   r>  rÍ   ÚnnÚModulerH  r   rE   r.  rJ   )rö   r  rà   Úmodules   &&  r>   r§  r§  ê  s}   € ó ˆÜ˜%Ÿ,™,¬¯©¯©×8Ò8Ùà—l‘l×*Ñ*Ö,ˆFÜ˜&¤/×2Ô2Ü"Ø˜=˜/ð *0ä04°V³×0EÑ0EÐ/FðHóð ó -ó	 rA   )
r  r   r    r!   r"   r#   r$   r%   r&   r'   rY   )é   )r   F)r7   )mrª   rÚ  r²   ÚloggingÚreÚabcr   r   Úcollectionsr   r   Úcollections.abcr   Údataclassesr   Úenumr	   Ú	functoolsr
   Útypingr   r   r   r   r   rÍ   Útorch.distributedÚdistributedr
  Útorch._dynamor   Útorch.distributed.fsdpr   r   Útorch.nn.modules.lossr   Útorch.profilerr   Ú_utilsr   r   r   r³  r   r   r   r   rà   r   r   r   Ú__all__Ú	getLoggerrJ   rØ   r;   r)   rN   rO   rP   r-   r.   r/   r0   r1   r2   rQ   r4   r5   rs  r*   r+   r,   r3   Úcompiler   rW   r�   rÃ   rÅ   r  r¦  r  r   rÑ  r"   r!   r  rñ  rJ  rQ  ro  rˆ  r   r    r  r  r  r$   r}  rƒ  r#   r%   r&   r'   r  r9  rA  r§  rI   rA   r>   Ú<module>r[     s°  ðó Û 
Û Û Û 	ß #ß ,Ý $Ý !Ý Ý ß ;Õ ;ã Ý  Ý )ß <Ý 'Ý *÷ñ ÷
ó ÷ @Ñ ?ò€ð 
×	Ò	˜8Ó	$€ôN�s˜Dô Nð. ×
"Ñ
"€Ø!×0Ñ0€Ø"×2Ñ2€Ø
×
"Ñ
"€Ø
×
"Ñ
"€Ø	×	 Ñ	 €Ø	×	 Ñ	 €Ø	×	 Ñ	 €Ø	×	 Ñ	 €Ø ×.Ñ.€Ø×*Ñ*€Ø×*Ñ*€ñ % QÓ'Ð ð €Ø€Ø€Ø€ð —
’
ØRó€ô
T
ˆjô T
ðn ôó ð÷<ô~u
˜ô u
÷p.+÷bõ6ôoÐ.ô oôd2"Ð1ô 2"ôjxÐ*ô xôvzÐ)ô zõz!õ$÷BPõf#õLtõn[õ|'ôDp2Ð-ô p2ðf ÷ð ó ðôL˜hô Lô}2Ð4ô }2ô@DÐ0ô DôNF÷R<ô&q
Ð6ô q
ôh}Ð$<ô }ô@Ð4ô ôD`Ð0ô `õFAõ6BòJ!.÷HrA   