+
    –°³j÷` ã                  ó   € ^ RI Ht ^ RIt^ RIt^ RIt^ RIt^ RIt^ RIHt ^ RI	H
t
 ^ RIHtHt ^ RIt^ RIHt ^ RIHt ^RIHtHtHt ^R	IHt ]'       d	   ^R
IHtHt ^RIHtHtHtHt ^RI H!t!H"t"H#t#H$t$ ^RI%H&t& ]PN                  ! ](4      t)]PT                  PW                  ](R4      t,]'       d   ^ RI-H.t. R R lt/R R lt0R R lt1R R lt2R R lt3]
 ! R R4      4       t4R R lt5R R lt6R  R! lt7ReR" R# llt8R$ t9R% R& lt:RfR' lt;R( R) lt<R* R+ lt=R, R- lt>R. t?R/ t@R0 tAR1 R2 ltBR3 R4 ltCR5 R6 ltDR7 R8 ltER9 R: ltFR; R< ltGR= R> ltHR? R@ ltIRA RB ltJRC RD ltKRE RF ltLRG RH ltMRI RJ ltN]
 ! RK RL4      4       tORM RN ltPRO RP ltQRQ RR ltRRS RT ltSRU RV ltTRW RX ltURY RZ ltVR[ R\ ltWR] R^ ltXR_ tYR` tZRa Rb lt[Rc Rd lt\R# )gé    )ÚannotationsN)Údefaultdict)Ú	dataclass)ÚAnyÚTYPE_CHECKING)Útrace_structured)Ú
OrderedSet)ÚconfigÚconfig_commsÚir)ÚWeakDep)ÚIRNodeÚ	Operation)Úestimate_peak_memory_allocfreeÚFreeableInputBufferÚget_freeable_input_bufÚSNodeMemory)Úcontains_collectiveÚcontains_waitÚis_fallback_opÚis_wait)ÚVÚoverlap)ÚBaseSchedulerNodec               ó   € V ^8„  d   QhRR/# )é   Úsnodesúlist[BaseSchedulerNode]© )Úformats   "ÚV/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torch/_inductor/comms.pyÚ__annotate__r"   *   s   € ÷ !Kñ !KØ#ñ!Kó    c           	     óà  € V  Uu. uF  qP                  4       NK  	  pp^ RIHp ^ RIHp VP                  4       pV! 4       p\        V4       Uu. uF  p. NK  	  ppVP                  VVV4       \        V U	u. uF  p	\        V	4      NK  	  up	4      p
\        V
4      ^8w  dS   \        P                  RV U	u. uF  p	\        V	4      NK  	  up	4       \        V 4       F  w  r±W+,          Vn        K  	  R# \        P                  ! \        P                   ! V4      ^ R7      P"                  P%                  4       p\        V 4       F  w  r±WË,          Vn        K  	  R# u upi u upi u up	i u up	i )r   N)Ú_get_default_groupzeDifferent ranks have different numbers of scheduler nodes (%s), skipping runtime estimation alignment)Údim)Úget_estimated_runtimeÚtorch.distributedÚdistributedÚ"torch.distributed.distributed_c10dr%   Úget_world_sizeÚrangeÚall_gather_objectr	   ÚlenÚlogÚwarningÚ	enumerateÚoverride_estimated_runtimeÚtorchÚmedianÚtensorÚvaluesÚtolist)r   ÚsnodeÚruntime_estimationsÚdistr%   Ú
world_sizeÚpgÚ_Úgathered_runtime_estimationsÚeÚlengthsÚidxÚmedian_runtime_estimationss   &            r!   Ú6align_runtime_estimations_across_all_distributed_ranksrC   *   sI  € ñ GMÓMÁf¸U×6Ñ6Ö8ÁfÐÐMå$ÝEà×$Ñ$Ó&€JÙ	Ó	€BÜCHÈÔCTÓ6UÑCT¸a³rÑCTÐ Ð6UØ×ÑØ$ØØ
ôô Ñ*FÓGÑ*F Qœ#˜až&Ñ*FÑGÓH€GÜ
ˆ7ƒ|�qÔÜ�‰ð4á9Ó:Ñ9˜ŒS�ŽVÑ9Ñ:ô	
ô
 $ FÖ+‰JˆCØ/BÕ/GˆEÖ,ñ ,áä!&§¢Ü�ŠÐ1Ó2¸ô"ç�f�V‰V‹Xð ô   Ö'‰
ˆØ+EÕ+JˆÖ(ó (ùò; Nùò 7Vùò Hùò
 ;s   …EÁE!Á8E&Â7E+
c               ó    € V ^8„  d   QhRRRR/# ©r   r   r   Úreturnr   )r    s   "r!   r"   r"   N   s   € ÷ ñ Ð.ð Ð3Jñ r#   c                ó    € \        V RRRR7      # )z/
Greedily schedules waits as late as possible.
FT©Úraise_commsÚ
sink_waitsÚreorder_for_overlap©Ú_schedule_for_comm©r   s   &r!   rJ   rJ   N   s   € ô Ø˜E¨dÈôð r#   c               ó    € V ^8„  d   QhRRRR/# rE   r   )r    s   "r!   r"   r"   W   s   € ÷ ñ Ð/ð Ð4Kñ r#   c                ó    € \        V RRRR7      # )z0
Greedily schedules comms as early as possible.
TFrH   rL   rN   s   &r!   rI   rI   W   s   € ô Ø˜D¨UÈôð r#   c               ó    € V ^8„  d   QhRRRR/# rE   r   )r    s   "r!   r"   r"   `   s   € ÷ ñ Ø#ðàñr#   c                ó    € \        V RRRR7      # )a˜  
This achieves the following overall scheduling procedure:
    Step 1: Given that we've currently scheduled comm N, we now schedule all compute nodes
        that are required for comm N + 1 but do not depend on comm N, to run at the same time with comm N.
    Step 2: If all those compute nodes are sufficient to overlap comm N, we're done.
        Otherwise, we now need to look elsewhere to find compute that overlaps with comm N.
        We prioritize compute nodes that are needed sooner.
    Step 3: We schedule the compute nodes dependent on comm N and required for comm N + 1.
    Step 4: We schedule comm N + 1.
    Repeat this for subsequent comm nodes.
TrH   rL   rN   s   &r!   Úreorder_compute_for_overlaprS   `   s   € ô Ø˜D¨TÀtôð r#   c               ó    € V ^8„  d   QhRRRR/# rE   r   )r    s   "r!   r"   r"   s   s   € ÷ #ñ #Ø#ð#àñ#r#   c                ó    € \        V 4      w  rV# )a+  
Reorders communication ops relative to computation ops to improve communication-compute overlapping and hide comm
latency.  Stops moving a particular op if it reaches a point that would have increased the peak memory footprint.

Currently, follows these heuristics (subject to change or tune):
- never reorders collectives relative to one another, for SPMD safety
- has an option for per-collective prefetch limit, but does not enable it by default
- limits the total number of reorder steps to some factor of the graph size to prevent worst-case quadratic
  performance

Prerequisite: sink_comms_and_waits - ensure comm and wait nodes are scheduled as late as possible, respecting data
dependencies.  That allows reorder_communication_preserving_peak_memory to take a best case peak-memory snapshot,
and then monotonically improve latency by moving collectives backward in time.

Peak memory impact is computed in an iterative fashion.  First, memory use at each timestep is computed, and global
peak memory is computed as a max over timesteps.  Then, when swapping any two adjacent nodes, only the curr-memory
for the earlier of the nodes after the swap is affected.  This enables checking step by step whether a swap is
peak-memory-safe, and bailing out if not.  Example:

0   n0      C0
1   n1      C0 + Allocs(n1) - Frees(n1)
2   n2      C0 + Allocs(n1) - Frees(n1) + Allocs(n2) - Frees(n2)

0   n0      C0
1   n2      C0 + Allocs(n2) - Frees(n2)    <-- After moving n2 to Time 1, only time1 memory changes
2   n1      C0 + Allocs(n2) - Frees(n2) + Allocs(n1) - Frees(n1)

)Ú6_reorder_communication_preserving_peak_memory_internal)r   Úreordered_snodesÚ
node_statss   &  r!   Ú,reorder_communication_preserving_peak_memoryrY   s   s   € ô@ 	?¸vÓFñ !Ðð Ðr#   c                  óª   € ] tR t^™t$ RtRtR]R&   ^ tR]R&   ^ tR]R&   Rt	R]R	&   Rt
R
]R&   RtR
]R&   RtR
]R&   RtR
]R&   RtR]R&   ]R 4       tRtR# )ÚReorderInfoz=
Debug info describing how an individual snode was reordered
ÚNoneÚstrÚlimiting_factorÚintÚmovesÚgroupedÚ Úgrouped_infoÚfloatÚ	comm_timeÚ	comp_timeÚinitial_exposedÚfinal_exposedÚoverlap_infoc                	ó<   € V P                   V P                  ,
          # ©N©rg   rh   ©Úselfs   &r!   ÚimprovementÚReorderInfo.improvement©   ó   € à×#Ñ# d×&8Ñ&8Õ8Ð8r#   r   Nç      ð¿)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r^   Ú__annotations__r`   ra   rc   re   rf   rg   rh   ri   Úpropertyro   Ú__static_attributes__r   r#   r!   r[   r[   ™   st   ‡ ñð "€O�SÓ!Ø€Eˆ3ƒNØ€GˆSÓØ€L�#ÓØ€IˆuÓØ€IˆuÓØ!€O�UÓ!Ø€M�5ÓØ€L�#Óàñ9ó ô9r#   r[   c               ó    € V ^8„  d   QhRRRR/# )r   ÚnodezIRNode | Operation | NonerF   Úboolr   )r    s   "r!   r"   r"   ®   s   € ÷ ñ Ð0ð °Tñ r#   c                óÎ   € V f   R# \        V \        P                  P                  P                  P
                  4      '       d   R# \        V RR 4      ;p'       d
   RV9   d   R# R# )NFTÚpython_kernel_nameÚextern_kernels)r   r3   ÚopsÚatenÚ#_scaled_dot_product_flash_attentionÚdefaultÚgetattr)r|   r   s   & r!   Úis_gemm_liker†   ®   s]   € Ø‚|ÙäØÜ�	‰	�‰×:Ñ:×BÑB÷ò ñ ô & dÐ,@À$ÓGÐGÐÖGØ
Ð0Ô
0ÙÙr#   c               ó    € V ^8„  d   QhRRRR/# )r   r8   r   rF   r}   r   )r    s   "r!   r"   r"   ¿   s   € ÷ (ñ (Ð/ð (°Dñ (r#   c                óê   € ^ RI Hp \        W4      '       dI   \        ;QJ d&    R V P                   4       F  '       g   K   R# 	  R# ! R V P                   4       4      # \        V P                  4      # )r   ©ÚGroupedSchedulerNodec              3  ó8   "  € T F  p\        V4      x € K  	  R # 5irk   )Úcontains_gemm_like)Ú.0Úxs   & r!   Ú	<genexpr>Ú%contains_gemm_like.<locals>.<genexpr>Ã   s   é € Ð?±,¨QÔ% a×(Ð(³,ùó   ‚TF)Útorch._inductor.schedulerrŠ   Ú
isinstanceÚanyr   r†   r|   )r8   rŠ   s   & r!   rŒ   rŒ   ¿   sP   € Ý>ä�%×.Ò.ß‹sÑ?°%·,²,Ó?�sŒsÐ?ŠsÐ?ˆsÑ?°%·,²,Ó?Ó?Ð?ä˜EŸJ™JÓ'Ð'r#   c               ó   € V ^8„  d   QhRR/# )r   r8   r   r   )r    s   "r!   r"   r"   È   s   € ÷ ñ Ð$5ñ r#   c                ó¢   € ^ RI Hp \        W4      '       d0   V P                  '       d   V P                   F  pV! V4       K  	  R# V! V 4       R# )r   r‰   N)r’   rŠ   r“   Útemp_groupingr   )r8   ÚfnrŠ   Ú_snodes   &&  r!   Ú_temp_group_visit_leavesrš   È   s9   € Ý>ä�%×.Ò.°5×3F×3FÐ3FØ—l”lˆFÙˆvŽJó #ñ 	ˆ5Ž	r#   c               ó   € V ^8„  d   QhRR/# )r   rF   r]   r   )r    s   "r!   r"   r"   Ò   s   € ÷ ñ ¨3ñ r#   c                óÔ   € R pV P                    FU  pV'       d
   VR,          pW#P                  4       ,          pV'       g   K4  V\        V P                  4       4       ,          pKW  	  V# )rb   r=   )r   Úget_nameÚlistÚget_buffer_names)r8   Ú	with_bufsÚretÚns   &&  r!   Ú_group_namer£   Ò   sV   € Ø
€CØ�\Œ\ˆßØ�3�JˆCØ�z‰z‹|Õˆß‰9Ø”d˜5×1Ñ1Ó3Ó4Ð5Õ6ŠCñ ð €Jr#   c                óJ   € \        V \        4      ;'       d    V P                  # rk   )r“   r   Úis_fake)Úds   &r!   Ú_is_fake_depr§   Ý   s   € Ü�aœÓ!×/Ð/ a§i¡iÐ/r#   c               ó    € V ^8„  d   QhRRRR/# )r   Úgnsr   rF   r]   r   )r    s   "r!   r"   r"   á   s   € ÷ 3ñ 3Ð-ð 3°#ñ 3r#   c                óh   € R P                  V  Uu. uF  qP                  4       NK  	  up4      # u upi )Ú~)Újoinr�   )r©   Úgns   & r!   Ú_group_namesr®   á   s'   € Ø�8‰8©SÓ1©S r—[‘[–]©SÑ1Ó2Ð2ùÒ1s   �/c                óN  € Vf2   \        \        P                  P                  P	                  4       4      pVf(   \        \        P                  P                  4       4      p\        W4      p\        WV4      w  rErg\        \        W4      4      pRVR&   \        V4      p	VVVVVV	3# )z*Initialize memory tracking data structuresN)r   r   )r	   r   ÚgraphÚgraph_inputsÚkeysÚget_output_namesr   r   ÚdictÚzipÚ_build_candidate_buffer_map)
r   r±   Úgraph_outputsÚname_to_freeable_input_bufÚpeak_memoryÚsnodes_curr_memoryÚsnodes_allocfreeÚbuf_to_snode_last_useÚ_curr_memoryÚcandidate_buffer_maps
   &&&       r!   Ú_initialize_memory_trackingr¿   å   s¬   € àÒÜ!¤!§'¡'×"6Ñ"6×";Ñ";Ó"=Ó>ˆØÒÜ"¤1§7¡7×#;Ñ#;Ó#=Ó>ˆä!7¸Ó!MÐä&Ø°ó	
ñ M€KÐ%5ô
 œ˜FÓ7Ó8€Là€L�Ñô 7Ð7LÓMÐð 	ØØØØ"Øðð r#   c               ó    € V ^8„  d   QhRRRR/# )r   r   r   rF   z~tuple[dict[BaseSchedulerNode, BaseSchedulerNode | None], dict[BaseSchedulerNode, BaseSchedulerNode | None], BaseSchedulerNode]r   )r    s   "r!   r"   r"     s   € ÷ ñ Ø#ððñr#   c                óÔ   € / p/ p\        V 4       FI  w  r4V^ 8”  d   W^,
          ,          MRW&   V\        V 4      ^,
          8  d   W^,           ,          MRW$&   KK  	  V ^ ,          pWV3# )z/Create double-linked list structure from snodesN)r1   r.   )r   Ú_prevÚ_nextÚir8   Ú_heads   &     r!   Ú_initialize_double_linked_listrÆ     sh   € ð €EØ€EÜ˜fÖ%‰ˆØ()¨A¬�v !�e–}°4ˆ‰Ø()¬C°«K¸!­OÔ(;�v !�e–}Àˆ‹ñ &ð �1�I€EØ˜ÐÐr#   c               ó    € V ^8„  d   QhRRRR/# )r   r¼   r´   rF   ú#dict[BaseSchedulerNode, OrderedSet]r   )r    s   "r!   r"   r"     s   € ÷ (ñ (Øð(à(ñ(r#   c                ó°   € \        \        4      pV  F7  pVP                  P                   F  pW,          P	                  V4       K  	  K9  	  \        V4      # )ae  
Build inverted index: node -> set of buffers where node appears in successors.

This optimization reduces buffer iteration from O(total_buffers) to O(buffers_per_node).
Since buffer successors are immutable during reordering, this map doesn't need updates.

Returns:
    dict mapping each node to the set of buffers that have this node in their successors
)r   r	   Ú
mpi_bufferÚ
succ_nodesÚaddr´   )r¼   Únode_to_candidate_bufsÚbufÚ	succ_nodes   &   r!   r¶   r¶     sT   € ô CNÜóCÐó %ˆàŸ™×2Ô2ˆIØ"Õ-×1Ñ1°#Ö6ó 3ñ %ô
 Ð&Ó'Ð'r#   c               ó    € V ^8„  d   QhRRRR/# )r   r   r   rF   z(dict[BaseSchedulerNode, OrderedSet[str]]r   )r    s   "r!   r"   r"   ,  s   € ÷ ñ Ø#ðà-ñr#   c           	     ól   € V  Uu/ uF#  q\        R VP                  4        4       4      bK%  	  up# u upi )zÒ
Pre-compute output name sets for all nodes.

This optimization avoids creating OrderedSet objects repeatedly during
exposed time calculations.

Returns:
    dict mapping each node to a set of its output names
c              3  ó@   "  € T F  qP                  4       x € K  	  R # 5irk   ©r�   ©r�   Úos   & r!   r�   Ú/_precompute_node_output_sets.<locals>.<genexpr>9  s   é € ÐDÑ0C¨1Ÿ*™*Ÿ,˜,Ó0Cùó   ‚)r	   Úget_outputs)r   r8   s   & r!   Ú_precompute_node_output_setsrÙ   ,  s?   € ñ SYóÙRXÈŒzÑD°×0AÑ0AÔ0CÓDÓDÒDÑRXñð ùò s   …)1c                ód   € \        V 4      '       d   \        P                  # \        P                  # rk   )r   r   Ú*reorder_sink_runtime_estimations_comm_multÚ.reorder_sink_runtime_estimations_non_comm_mult©r8   s   &r!   Ú_op_runtime_estimate_multrÞ   =  s'   € ô ˜5×!Ò!Ü×FÑFÐFä×FÑFÐFr#   c                óV   € \        V P                  RR4      ;p'       d
   RV9   d   R# R# )z¾
Filtering out ops that contain Collective and Wait inside and considered as Collectives.
See contains_collective function.
If the op contains Wait inside - consider as Synchronous compute.
r   Nz-torch.ops._dtensor.shard_dim_alltoall.defaultFT)r…   r|   )r8   r   s   & r!   Úis_async_collectiverà   F  s-   € ô % U§Z¡ZÐ1EÀtÓLÐLÐÖLØ:Ð>PÔPÙár#   c                ó"   € \        V \        4      # rk   )r   rà   rÝ   s   &r!   Úcontains_async_collectiverâ   S  s   € Ü˜uÔ&9Ó:Ð:r#   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   ÚheadzBaseSchedulerNode | NoneÚtailÚ	next_dictú1dict[BaseSchedulerNode, BaseSchedulerNode | None]rF   r   r   )r    s   "r!   r"   r"   W  s1   € ÷ ñ Ø
"ðà
"ðð Aðð ñ	r#   c                ó\   € . pT p Ve   VP                  V4       WA8X  d    V# W$,          pK)  )a!  
Traverse doubly-linked list from head to tail and return nodes as a list.

Args:
    head: Starting node of the segment
    tail: Ending node of the segment (inclusive)
    next_dict: Dictionary mapping each node to its next node

Returns:
    List of nodes from head to tail (inclusive)
)Úappend)rä   rå   ræ   r¡   r¢   s   &&&  r!   Ú_group_nodes_from_linked_listrê   W  s7   € ð  €CØ€AØ
ØŠ=Ø�J‰J�qŒMØŒ9Øà€Jð �LŠr#   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r   Úcollective_snoder   Ú
wait_snodeÚnode_output_setsú'dict[BaseSchedulerNode, frozenset[str]]Únode_dep_setsrF   r}   r   )r    s   "r!   r"   r"   r  s:   € ÷ .ñ .Ø'ð.à!ð.ð >ð.ð ;ð	.ð
 
ñ.r#   c                óD   € W ,          pW1,          p\        WT,          4      # )zf
Check if a wait node corresponds to a given collective node.
Uses pre-computed sets for O(1) lookup.
)r}   )rì   rí   rî   rð   Úcollective_outsÚ
unmet_depss   &&&&  r!   Ú!_is_corresponding_collective_waitrô   r  s"   € ð 'Õ8€OØÕ*€JÜ�
Õ,Ó-Ð-r#   c               ó0   € V ^8„  d   QhRRRRRRRRR	RR
R/# )r   rì   r   ræ   rç   Úruntimesúdict[BaseSchedulerNode, float]rî   rï   rð   rF   útuple[float, float, str]r   )r    s   "r!   r"   r"   �  sD   € ÷ 9.ñ 9.Ø'ð9.à@ð9.ð -ð9.ð >ð	9.ð
 ;ð9.ð ñ9.r#   c                ó:  aa€ SV ,          pRoW0,          pRp. pW,          p	V	eó   WI,          p
W¦,          '       d   VRV	P                  4        R2,          pMÀ\        V	4      '       d.   \        V	4      '       g   M�VP                  V	4       W,          p	Kt  \	        V	4      '       d+   RpV F  p\        W	W44      '       g   K  Rp M	  V'       d   MGSpR VV3R llp\        Wž4       SpVR	V	P                  4        R
Wý,
           R2,          pW,          p	Kö  VSV3# )zË
Calculate exposed communication time by iterating directly over linked list.
Avoids O(N) list construction for each call.

The collective_snode is the starting point, iteration continues via next_dict.
ç        rb   z->W[Ú]FTc               ó    € V ^8„  d   QhRRRR/# ©r   r™   r   rF   r\   r   )r    s   "r!   r"   Ú6_coll_exposed_communication_time.<locals>.__annotate__°  ó   € ÷ 	*ñ 	*Ð$5ð 	*¸$ñ 	*r#   c                ó(   <€ SSV ,          ,          oR # rk   r   ©r™   rf   rö   s   &€€r!   Úaccumulate_timeÚ9_coll_exposed_communication_time.<locals>.accumulate_time°  ó   ø€ à˜ &Õ)Õ)ŠIr#   Ú+Ú[)r�   r   râ   ré   r   rô   rš   )rì   ræ   rö   rî   rð   re   rò   ri   Úcollectives_foundr8   ró   Úhas_wait_for_collectives_foundÚ_collÚcomp_time_beforer  Úcomp_time_afterrf   s   &&f&&           @r!   Ú _coll_exposed_communication_timer  �  s2  ù€ ð Ð)Õ*€IØ€IØ&Õ8€OØ€LØ13ÐàÕ'€EØ
Ò
Ø"Õ)ˆ
à×'Ô'Ø˜d 5§>¡>Ó#3Ð"4°AÐ6Õ6ˆLØä˜u×%Ò%Ü,¨U×3Ò3Øà!×(Ñ(¨Ô/Ø!Õ(�ÙÜ˜×ÒØ-2Ð*Û*�Ü4Ø$Ð-=÷ô ð 6:Ð2Ùñ +÷ .Øà$Ð÷	*ñ 	*ô 	! Ô8Ø#ˆØ˜!˜EŸN™NÓ,Ð-¨Q¨Õ/QÐ.RÐRSÐTÕTˆàÕ Šà�i Ð-Ð-r#   c               ó4   € V ^8„  d   QhRRRRRRRRRR	R
R	RR/# )r   rí   r   rä   Ú	prev_dictrç   rö   r÷   rî   rï   rð   rF   rø   r   )r    s   "r!   r"   r"   ½  sO   € ÷ 9.ñ 9.Ø!ð9.à
ð9.ð Að9.ð -ð	9.ð
 >ð9.ð ;ð9.ð ñ9.r#   c                ó&  aa€ RpRoRp. pW ,          p	V	eø   \        V	4      '       d   VP                  V	4       \        V	4      '       d~   \        W�WE4      '       d&   SV	,          pVRV	P	                  4        R2,          pM�\        V	4      '       g   RoW),          p	K‹  V F  p
\        WšWE4      '       g   K  Ro M	  W),          p	K´  SpR VV3R llp\        Wœ4       SpVRV	P	                  4        RWÛ,
           R2,          pW),          p	Kû  VSV3# )	zâ
Calculate exposed communication time for a wait operation by iterating
directly over linked list backwards. Avoids O(N) list construction.

Iterates from wait_snode backwards using prev_dict to find corresponding collective.
rú   rb   z->C[rû   c               ó    € V ^8„  d   QhRRRR/# rý   r   )r    s   "r!   r"   Ú6_wait_exposed_communication_time.<locals>.__annotate__ì  rÿ   r#   c                ó(   <€ SSV ,          ,          oR # rk   r   r  s   &€€r!   r  Ú9_wait_exposed_communication_time.<locals>.accumulate_timeì  r  r#   r  r  )r   ré   r   rô   r�   râ   rš   )rí   rä   r  rö   rî   rð   re   ri   Úwaits_foundr8   Úwr
  r  r  rf   s   &&&f&&        @r!   Ú _wait_exposed_communication_timer  ½  s0  ù€ ð €IØ€IØ€LØ+-€KàÕ!€EØ
Ò
Ü˜×ÒØ×Ñ˜uÔ%Ü˜u×%Ò%Ü0ØÐ#3÷ò ð % U�O�	Ø $ u§~¡~Ó'7Ð&8¸Ð :Õ:�Øä,¨U×3Ò3Ø�	Ø!Õ(�Ùã$�AÜ8ØÐ"2÷ô ð %(˜	Ùñ %ð "Õ(�Ùà$Ð÷	*ñ 	*ô 	! Ô8Ø#ˆØ˜!˜EŸN™NÓ,Ð-¨Q¨Õ/QÐ.RÐRSÐTÕTˆàÕ Šà�i Ð-Ð-r#   c               ó4   € V ^8„  d   QhRRRRRRRRRRRRR	R/# ©
r   Ú	candidater   Ú
group_headÚ
group_tailr  rç   ræ   rä   rF   r   )r    s   "r!   r"   r"   ù  óP   € ÷ .ñ .Ø ð.à!ð.ð "ð.ð Að	.ð
 Að.ð ð.ð ñ.r#   c                ó†   € W0,          pV'       d   WV&   WcV&   WB,          pV'       d   WV&   WtV &   W#V &   WV&   WP8X  d   V# V# )aQ  
Swap positions of candidate and group in doubly-linked list.

Transforms:
candidate_prev -> candidate -> group_head...group_tail -> group_tail_next
Into:
candidate_prev -> group_head...group_tail -> candidate -> group_tail_next

Args:
    candidate: Node to swap with group
    group_head: First node of group
    group_tail: Last node of group
    prev_dict: Dictionary mapping nodes to their previous nodes
    next_dict: Dictionary mapping nodes to their next nodes
    head: Current head of the linked list

Returns:
    New head of the linked list (may change if candidate was the head)
r   )r  r  r  r  ræ   rä   Úcandidate_prevÚgroup_tail_nexts   &&&&&&  r!   Ú _perform_double_linked_list_swapr   ù  sa   € ð8 Õ)€NßØ$.�.Ñ!Ø*ˆjÑð  Õ+€OßØ%.�/Ñ"Ø*ˆiÑð &ˆiÑØ%ˆjÑð ÔØÐØ€Kr#   c               ó<   € V ^8„  d   QhRRRRRRRRRRR	R
RRRRRR/	# )r   r  r   r©   r   r  Úgroup_peak_memoryr_   Úcandidate_delta_memÚcandidate_allocfreer   Ú/group_n_to_bufs_after_swap_dealloc_by_candidater´   Úcurr_memoryrF   z(tuple[int, dict[BaseSchedulerNode, int]]r   )r    s   "r!   r"   r"   *  sm   € ÷ @.ñ @.Ø ð@.à	 ð@.ð "ð@.ð ð	@.ð
 ð@.ð %ð@.ð 6:ð@.ð ð@.ð .ñ@.r#   c                óÚ  € / p^ p	V'       g;   \        W4,
          Wr,          ^,          V,
          VP                  ,           4      p	W˜3# V) p
V Fd  pW{,          ^ ,          V
,           pWÈV&   \        Wœ4      p	VP                  V4      pVf   K?  V F  pW®P                  P                  ,          p
K!  	  Kf  	  Wr,          ^,          V
,           VP                  ,           pWøV &   \        WŸ4      p	W˜3# )aã  
Calculate potential peak memory after swapping candidate with group (reorder version).

Computes new memory levels for all affected nodes and returns the potential
peak memory along with cached post-allocation memory values for each node.

Args:
    candidate: Node being moved
    gns: Group nodes
    group_tail: Last node of group
    group_peak_memory: Current peak memory within the group
    candidate_delta_mem: Net memory change from candidate (alloc - free)
    candidate_allocfree: Candidate's allocation/free info
    group_n_to_bufs_after_swap_dealloc_by_candidate: Buffers whose deallocation moves to candidate
    curr_memory: Current memory state dict

Returns:
    Tuple of (potential_peak_memory, post_alloc_update_dict)
)ÚmaxÚ
size_allocÚgetrÊ   Ú	size_free)r  r©   r  r"  r#  r$  r%  r&  Ú_post_alloc_updateÚpotential_peakÚmem_after_reorder_deltar­   Úgn_post_alloc_memÚbufsrÎ   Úcandidate_mem_post_allocs   &&&&&&&&        r!   Ú(_calculate_potential_peak_memory_reorderr2  *  s   € ð> 8:ÐØ€Nß:äØÕ3ØÕ# AÕ&Ø!õ"à!×,Ñ,õ-ó
ˆð Ð1Ð1ð %8Ð#7ÐÛˆØ'�O¨AÕ.Ð1HÕHÐØ!2˜2ÑÜ˜^Ó?ˆà>×BÑBÀ2ÓFˆØÔÛ�à'¯>©>×+CÑ+CÕCÒ'ó ñ ð 	Õ Õ"Ø
!õ	"à
×
(Ñ
(õ	)ð ð
 %=�yÑ!Ü˜ÓB€NØÐ-Ð-r#   c               óD   € V ^8„  d   QhRRRRRRRRRR	R
RRRRRRRRRRR/# )r   r  r   r©   r   r  r#  r_   r$  r   r%  r´   Úpost_alloc_updateúdict[BaseSchedulerNode, int]r&  r¼   r»   rF   r\   r   )r    s   "r!   r"   r"   m  sƒ   € ÷ Jñ JØ ðJà	 ðJð "ðJð ð	Jð
 %ðJð 6:ðJð 4ðJð ðJð  ðJð ðJð 
ñJr#   c
                ó²  € V'       gn   V F-  p
Wz,          pV^ ,          V,
          V^,          V,
          3Wz&   K/  	  Wr,          ^,          VP                   ,           pWÄP                  ,
          pVV3Wp&   R# VP                  4        F  pV F  pWV&   K	  	  K  	  ^ pV Fo  pVV,          p\        R VV,           4       4      pVV,          pV	V,          ;P                  V,          un        VV	V,          P                  ,
          pVV3VV&   Kq  	  W`,          pW�,          ;P                  V,          un        WÉV ,          P                  ,
          pVV3Wp&   R# )a  
Update memory tracking structures after swap (reorder version).

Updates curr_memory, buf_to_snode_last_use, and snodes_allocfree dictionaries
to reflect the new memory state after swapping candidate with group.

Args:
    candidate: Node that was moved
    gns: Group nodes
    group_tail: Last node of group
    candidate_delta_mem: Net memory change from candidate (alloc - free)
    candidate_allocfree: Candidate's allocation/free info
    group_n_to_bufs_after_swap_dealloc_by_candidate: Buffers whose deallocation moves to candidate
    post_alloc_update: Cached post-allocation memory values
    curr_memory: Current memory state dict (mutated)
    buf_to_snode_last_use: Buffer to last-use node mapping (mutated)
    snodes_allocfree: Node allocation/free info dict (mutated)
Nc              3  óN   "  € T F  pVP                   P                  x € K  	  R # 5irk   ©rÊ   r+  ©r�   rÎ   s   & r!   r�   Ú=_update_memory_tracking_after_swap_reorder.<locals>.<genexpr>¦  s!   é € ð 2
áI�ð �N‰N×$Ö$ÛIùó   ‚#%)r)  r+  r6   Úsum)r  r©   r  r#  r$  r%  r4  r&  r¼   r»   r­   ÚcmÚ_candidate_post_alloc_memÚ_candidate_post_free_memr0  rÎ   Ú"size_free_to_move_to_candidate_sumr¢   Ú_gn_post_alloc_memÚsize_free_to_move_to_candidateÚgn_post_free_memÚcandidate_post_free_mems   &&&&&&&&&&            r!   Ú*_update_memory_tracking_after_swap_reorderrE  m  sy  € ÷< ;ÛˆBØ•ˆBà�1•Ð+Õ+Ø�1•Ð+Õ+ðˆK‹Oñ ð Õ# AÕ&Ð)<×)GÑ)GÕGð 	"ð &×(EÑ(EÕEð 	!ð &Ø$ð"
ˆÑñ 	ð @×FÑFÖHˆÛˆCØ)2 #Ó&ó ñ Ið /0Ð&ÛˆØ"3°AÕ"6ÐÜ.1ñ 2
àFÀqÖIó2
ó /
Ð&ð 	+Ð.LÕLÐ*à˜Õ×%Ò%Ð)GÕGÕ%Ø 2Ð5EÀaÕ5H×5RÑ5RÕ RÐØ,Ð.>Ð?ˆ�A‹ñ ð !2Õ <ÐØÕ×)Ò)Ð-OÕOÕ)à!°YÕ$?×$IÑ$IÕIð ð 	"Øð€KÓr#   c          
     ó,   € V ^8„  d   QhRRRRRRRRR	R
/# ©r   r  r   r©   r   r¼   r´   r¾   rÈ   rF   z8dict[BaseSchedulerNode, list[FreeableInputBuffer | Any]]r   )r    s   "r!   r"   r"   º  s:   € ÷ $;ñ $;Ø ð$;à	 ð$;ð  ð$;ð >ð	$;ð
 >ñ$;r#   c                óÎ   € \        \        4      pVP                  V \        4       4      p\        V4      pV F*  pW',          pW†9   g   K  WH,          P	                  V4       K,  	  V# )a^  
Find buffers whose last use will change after swapping candidate with group.

When we swap [candidate [group]] to [[group] candidate], some buffers that
were last used by a group node will now be last used by candidate instead.
This affects memory deallocation timing.

Args:
    candidate: The node being moved
    gns: Group nodes being swapped with candidate
    buf_to_snode_last_use: Mapping of buffers to their current last-use nodes
    candidate_buffer_map: Pre-computed map of node -> buffers using that node

Returns:
    Dict mapping group nodes to buffers that will change their last-use node
)r   rž   r*  r	   ré   )	r  r©   r¼   r¾   r%  Úcandidate_bufsÚgns_setrÎ   Úsnode_last_uses	   &&&&     r!   Ú#_find_buffers_with_changed_last_userL  º  sb   € ô0 	”DÓð 4ð *×-Ñ-¨i¼»ÓF€NÜ˜‹o€GãˆØ.Õ3ˆØÖ$Ø;ÕK×RÑRÐSVÖWñ ð
 ;Ð:r#   c               ó    € V ^8„  d   QhRRRR/# ©r   r  r   rF   ztuple[bool, str | None]r   )r    s   "r!   r"   r"   á  s   € ÷ ñ Ø ðàñr#   c                óÐ   € \         P                  '       g'   \        V 4      '       d   RRV P                  4        23# \         P                  '       g   \        V 4      '       d   R# R# )aA  
Check if a candidate node can be grouped with collective during reordering.

This pass processes collectives left to right, so we avoid grouping with
already-processed collectives based on configuration.

Args:
    candidate: Node to check for groupability

Returns:
    Tuple of (is_groupable, reason_if_not_groupable)
Fzcandidate contains_collective )FrŒ   ©TN)r   Ú(reorder_iterative_group_with_collectivesrâ   r�   Ú)reorder_iterative_use_runtime_estimationsrŒ   ©r  s   &r!   Ú_is_node_groupable_for_reorderrT  á  sa   € ô$ ×@×@Ð@Ü$ Y×/Ò/àØ0°×1CÑ1CÓ1EÐ0FÐGðð ô ×A×AÐAÜ˜i×(Ò(Ø.Ð.ØÐr#   c               ó8   € V ^8„  d   QhRRRRRRRRR	RR
RRRRR/# )r   Ústatsz$dict[BaseSchedulerNode, ReorderInfo]rä   r   ræ   rç   Úoriginal_snodes_numr_   r¹   r¸   r´   r·   úOrderedSet[str]rF   r   r   )r    s   "r!   r"   r"   ÿ  sc   € ÷ eñ eØ/ðeà
ðeð Aðeð ð	eð
 ðeð !%ðeð #ðeð ñer#   c                óÀ  a€ T pV Uu/ uF  qˆWx,          P                   bK  	  p	p\        V	 Uu. uF  q‰V,          NK  	  up4      p
\        V Uu. uF  q‡V,          P                  NK  	  up4      pRV
 RV R2o. ROpVP                  4        UUu. uF¢  w  r�\	        V4      VP
                  R,          VP                  R,          VP                  R,          VP                  R,          VP                   R,          VP                  VP                  VP                  VP                  VP                  .NK¤  	  ppp\        P                  P                  R4      '       d   ^ RIHp SV! VVR7      ,          oMHSR,          oS\#        V4      R	,           ,          oSR	P%                  \'        \"        V4      4      ,          o\)        VR
V4      p\+        V4      V8w  d   \-        RV R\+        V4       24      h\/        VWV4      w  p  pSRV 2,          oSRV 2,          o\0        P3                  S4       \5        RR V3R lR7       V# u upi u upi u upi u uppi )a�  
Format reordering statistics, log them, and return final node list.

Computes improvement metrics, creates a formatted table (using tabulate if
available), validates the reordered node count, recalculates peak memory,
and logs all information.

Args:
    stats: Per-node reordering statistics
    head: Head of the reordered linked list
    next_dict: Linked list next pointers
    original_snodes_num: Original number of nodes (for validation)
    peak_memory: Initial peak memory before reordering
    name_to_freeable_input_buf: Buffer memory tracking info
    graph_outputs: Graph output names

Returns:
    Final reordered list of scheduler nodes
zAreorder_communication_preserving_peak_memory improved overlap by z
 ns after z reorders.
ç     @�@Útabulate©r[  ©Úheadersú>Please `pip install tabulate` to nicely render overlap stats.
Ú
Nú	expected ú nodes, got z
 peak_memory_before:z
 peak_memory_after:Úartifactc                 ó   € R RRR/# )ÚnamerY   ÚencodingÚstringr   r   r#   r!   Ú<lambda>Ú2_format_and_log_reordering_stats.<locals>.<lambda>]  s   € ØÐBØ˜ñ
r#   c                 ó   <€ S # rk   r   )Úreorder_log_strs   €r!   rh  ri  a  s   ø€ ™?r#   ©Úmetadata_fnÚ
payload_fn)zCollective nodeúcomm_time(us)úcomp_time(us)úinitial exposed(us)úfinal exposed(us)úimprovement(us)úlimiting factorr`   ra   rc   ri   )ro   r<  r`   ÚitemsÚnode_summaryre   rf   rg   rh   r^   ra   rc   ri   Ú	importlibÚutilÚ	find_specr[  r]   r¬   Úmaprê   r.   ÚAssertionErrorr   Úoverlap_logÚinfor   )rV  rä   ræ   rW  r¹   r¸   r·   rX   r8   ro   Útotal_improvementÚtotal_movesr^  Ú	node_infoÚrowsr[  Ú
new_snodesÚnew_peak_memoryr=   rk  s   &&&&&&&            @r!   Ú _format_and_log_reordering_statsr„  ÿ  so  ø€ ð8 €JÙEOÓPÁZ¸E˜*Õ+×7Ñ7Ò7ÁZ€KÐPÜ¹[ÓI¹[°E¨×/Ð/¹[ÑIÓJÐÜ¹JÓG¹J°5 %Õ(×.Ô.¹JÑGÓH€Kð LÐL]ÐK^ð _Ø�˜lð	,ð ò€Gð6 !+× 0Ñ 0Ô 2ôñ !3ÑˆEô ˜ÓØ×Ñ #Õ%Ø×Ñ #Õ%Ø×%Ñ%¨Õ+Ø×#Ñ# cÕ)Ø×!Ñ! CÕ'Ø×%Ñ%Ø�O‰OØ×ÑØ×"Ñ"Ø×"Ñ"ó	
ñ !3ð 	ñ ô  ‡~�~×Ñ 
×+Ò+Ý%à™8ØØô
õ 	
‰ð
 	ØMõ	
ˆð 	œ3˜w›<¨$Õ.Õ.ˆØ˜4Ÿ9™9¤S¬¨d£^Ó4Õ4ˆä.¨t°T¸9ÓE€JÜ
ˆ:ƒÐ-Ô-ÜØÐ+Ð,¨L¼¸Z»Ð8IÐJó
ð 	
ô  >ØÐ.ó Ñ€O�Q˜˜1ð Ð/°¨}Ð=Õ=€OØÐ.¨Ð.?Ð@Õ@€Oä×Ñ�_Ô%ÜØñ
ô +õð ÐùòQ QùÚIùÚGùó&s   ˆI®IÁIÂB(Ic               ó    € V ^8„  d   QhRRRR/# )r   r   r   rF   zDtuple[list[BaseSchedulerNode], dict[BaseSchedulerNode, ReorderInfo]]r   )r    s   "r!   r"   r"   g  s    € ÷ Uñ UØ#ðUàIñUr#   c                ó$  € RpV  F  p\        V4      '       g   K  Rp M	  V'       g   V / 3# \        V 4      p\        \        P                  P
                  P                  4       4      p\        \        P                  P                  4       4      p\        WV4      w  pppp	p
pV  Uu/ uF  pV\        V4      \        V4      ,          bK!  	  ppV  Uu/ uF#  q"\        R VP                  4        4       4      bK%  	  ppV  Uu/ uF   pV\        R VP                   4       4      bK"  	  pp/ p^ p\        V 4      w  ppp\        P                   p^ pTp\        P"                  pRpVEeÜ   VV,          EeÐ   VV,          pV'       d   EM¼\%        V4      '       g   TpK9  Ve
   VV8¼  d   EMšV^,          p\'        4       ;pVV&   \)        VVWÍV4      w  pppVVn        VVn        VV,
          ;Vn        Vn        VVn        VV,          pTpTp / p!Rp"VV,          ^ ,          p#\        VV,          4      p$\        VV,          4      p%VEeø   \        P4                  '       d;   VP0                  \        P6                  ) VP*                  ,          8  d
   RVn        EM§\        P:                  '       g   \        V4      '       d
   RVn        EMwV$V%,
          p&V&'       g   Rp'MVV,          p(\=        V(V&,          4      p'V''       Ed9   \?        V4      w  p)p*V)'       dÕ   TpV$PA                  VV,          4       V%PA                  VV,          4       \        P4                  '       dN   \C        V4      '       d   \E        VVVVVV4      w  ppp+VV3V!V&   \%        V4      '       g   V"VV,          ,          p"\G        V#VV,          ^ ,          4      p#V;PH                  ^,          un$        VV,          pEK§  R	VPK                  4        R
VP                  4        U,u. uF  p,V,PK                  4       NK  	  up, RV* 2p-V-Vn        EM\        P4                  '       Ed§   \%        V4      '       Eg   VV,          p.V.^ 8”  Ed   \        V!4      ^ 8”  d÷   \G        ^ VP*                  VP,                  ,
          4      p/\G        ^ VP*                  VP,                  ,
          V.,
          4      p0V0V/,
          p1V!PM                  4        FC  w  p2p3\G        ^ V2V3,
          4      p4\G        ^ V2V3,
          V.,           4      p5V1V5V4,
          ,          p1KE  	  V1^ 8”  d   RV. RV1 2Vn        EMòV!PO                  4        F  w  p6w  p2p3V2V3V.,
          3V!V6&   K  	  M|V"^ 8”  dv   \)        VVWÍV4      w  ppp+\G        ^ VV,
          4      p/\G        ^ VV,
          V",           4      p0V0V/,
          p1V1^ 8”  d'   RVPK                  4        RV" RV1 RV RV 2
Vn        EMJ\Q        VV V4      p7VV,          p8V8PR                  V8PT                  ,
          p9\W        VV7W›4      p:\Y        VV7V V#V9V8V:V4      w  p;p<V;V,
          V\        PZ                  ,          8”  d   RV; RV 2Vn        MÄV;P\                  ^,          un.        V^,          p\_        VVV VVV4      p\)        VVWÍV4      w  pppVVn        VVn        VVn        VV,
          Vn        \a        VV7V V9V8V:V<VV	V4
       V'       d6   ^RI1H2p= V=! VV7\g        V74      \Q        VRV4      V
VVVVRV:4      pV'       d   MVV,          pEKü  TpEKà  \        Ph                  '       g   \Q        VRV4      p>V>V3# \k        VVVVVV
V4      p>V>V3# u upi u upi u upi u up,i )z|
Internal testing helper that also returns debug info.
Returns:
    - reordered snodes list
    - dict {snode: ReorderInfo}
FTc              3  ó@   "  € T F  qP                  4       x € K  	  R # 5irk   rÓ   rÔ   s   & r!   r�   ÚI_reorder_communication_preserving_peak_memory_internal.<locals>.<genexpr>Œ  ó   é € ÐCÑ/B¨!Ÿ™Ÿ˜Ó/Bùr×   c              3  ó`   "  € T F$  p\        V4      '       d   K  VP                  x € K&  	  R # 5irk   ©r§   re  ©r�   r¦   s   & r!   r�   rˆ  �  ó    é € ð 
Ù4�q¼LÈ¿OŒFˆA�FŽFÓ4ùó   ‚.š.Nrú   ú unexposed by runtime estimationszcollective orderingú$data dependency detected
 candidate:z(outs:z)
 non_group_reason:úcandidate has compute z,, group contains waits, total_exposed_delta z
candidate z is collective, group_runtime:z, exposed_delta:z c_comm_time:z c_comp_time:úpeak memory new:ú	 vs base:©Ú!_debug_iterative_memory_recomputerY   )6r   r.   r	   r   r°   r±   r²   r³   r¿   Úestimate_op_runtimerÞ   Ú	frozensetrØ   Úunmet_dependenciesrÆ   r   Ú(reorder_iterative_debug_limit_to_reorderÚ(reorder_iterative_debug_memory_recomputerâ   r[   r  re   rf   rg   rh   ri   rR  Ú)reorder_iterative_extra_comm_comp_overlapr^   Ú,reorder_iterative_unsafe_collectives_reorderr}   rT  Úupdater   r  r(  ra   r�   r6   ru  rê   r)  r+  rL  r2  Ú$reorder_iterative_peak_memory_budgetr`   r   rE  Úcomms_debugr•  r®   Úreorder_sink_verbose_loggingr„  )?r   Úhas_collectivesr8   rW  r±   r·   r¹   r½   r»   r¼   r¸   r¾   rö   rî   rð   rV  r  rÂ   rÃ   rÅ   Ú debug_num_collectives_to_reorderÚnum_processed_collectivesÚcurrÚ debug_iterative_memory_recomputeÚiterative_recompute_errorÚ
_next_currr}  re   rf   ri   r  r  r  Úgroup_waitsÚgroup_runtimer"  Úgroup_unmet_deps_namesÚgroup_output_namesÚdata_deps_namesÚdata_depÚcandidate_out_namesÚis_groupable_resultÚgrouping_reasonr=   rÕ   ÚmsgÚ	c_runtimeÚexposed_beforeÚexposed_afterÚexposed_deltaÚgw_comm_timeÚgw_comp_timeÚgw_exposed_beforeÚgw_exposed_afterÚgwr©   r$  r#  r%  r-  r,  r•  r‚  s?   &                                                              r!   rV   rV   g  sw  € ð €OÛˆÜ˜u×%Ô%Ø"ˆOÙñ ÷ Ø�rˆzÐä˜f›+Ðä$.¬q¯w©w×/CÑ/C×/HÑ/HÓ/JÓ$K€LÜ%/´·±×0HÑ0HÓ0JÓ%K€Mô 	$ F¸-ÓHñØØØØØ"Øñ
 ó0áˆEð 	Ô" 5Ó)Ô,EÀeÓ,LÕLÒLÙð ð 0ñ RXóAÙQWÈŒyÑC¨u×/@Ñ/@Ô/BÓCÓCÒCÑQWð ð Añ ó	>ñ ˆEð 	Œyñ 
Ø!×4Ò4ó
ó 
ò 	
ñ ð	 ð >ð 35€Eà€Kä8¸Ó@Ñ€Eˆ5�%ô 	×=Ñ=ð %ð &'ÐØ%*€Dä×=Ñ=ð %ð !&Ðà
Ó
˜u T�{Ó6Ø˜4•[ˆ
ß$Ùä(¨×.Ò.ØˆDÙà+Ò7Ø%Ð)IÔIáØ! QÕ&Ð!ä(›]Ð*ˆˆu�T‰{Ü-MØ�%˜°]ó.
Ñ*ˆ	�9˜lð #ˆŒØ"ˆŒØ4=À	Õ4IÐIˆÔ˜tÔ1Ø(ˆÔà˜$•Kˆ	Øˆ
Øˆ
ØˆØˆØ(¨Õ.¨qÕ1Ðô ",¨M¸$Õ,?Ó!@ÐÜ'Ð(8¸Õ(>Ó?ÐàÓ#Ü×E×EÐEØ×"Ñ"Ü×IÑIÐIØ—.‘.õ!ô!ð (J�Ô$Ùô !×M×MÐMÜ'¨	×2Ò2à'<�Ô$Ùð 5Ð7IÕIˆOß"Ø ‘ð '7°yÕ&AÐ#ÜÐ 3°oÕ EÓF�çˆxÜ7UØó8Ñ4Ð# _÷ 'Ø!*�Jð +×1Ñ1°-À	Õ2JÔKØ&×-Ñ-Ð.>¸yÕ.IÔJä#×M×MÐMÜ(¨×3Ò3Ü6VØ )Ø %Ø %Ø (Ø 0Ø -ó7Ñ3˜I y°!ð 6?À	Ð5I˜K¨	Ñ2Ü8¸×CÒCØ)¨X°iÕ-@Õ@˜Mä(+Ø)¨<¸	Õ+BÀ1Õ+Eó)Ð%ð —L’L AÕ%•LØ % iÕ 0�IÚð(Ø(1×(:Ñ(:Ó(<Ð'=¸VÐ[d×[pÑ[pÔ[rÓDsÑ[rÐVWÀQÇZÁZÆ\Ñ[rÑDsÐCtð u/Ø/>Ð.?ðAð ð
 ,/�DÔ(Ùä×E×EÑEä0°×;Ó;Ø (¨Õ 3�Ià  1•}¬¨[Ó)9¸AÔ)=ä),¨Q°·±ÀÇÁÕ0OÓ)P˜ä(+Ø˜tŸ~™~°·±Õ>ÀÕJó)˜ð )6¸Õ(F˜Ø:E×:LÑ:LÖ:NÑ6˜L¨,ä03°A°|ÀlÕ7RÓ0SÐ-ä/2Ø ! <°,Õ#>ÀÕ#Jó0Ð,ð *Ð-=Ð@QÕ-QÕQšMñ ;Oð )¨1Ô,à"8¸¸ð DNØN[È_ð!^ð !Ô0ñ "ð "-×!2Ñ!2Ö!4ñ! ñ %Ø ,Ø ,ð %1Ø$0°9Õ$<ð3" ¨B£ñ "5øð % qÔ(Ü2RØ% u¨hÈ-ó3Ñ/˜	 9¨aô *-¨Q°	¸IÕ0EÓ)F˜ä(+¨A¨y¸9Õ/DÀ}Õ/TÓ(U˜Ø(5¸Õ(F˜Ø(¨1Ô,à",¨Y×-?Ñ-?Ó-AÐ,Bð C2Ø2?°ð A2Ø2?°ÀÈiÈ[ÐXeÐfoÐepð!rð !Ô0ñ
 "ô ,IØ˜J¨ó,ˆCð 0@À	Õ/JÐà#×.Ñ.Ð1D×1NÑ1NÕNð  ô  4Ø˜sÐ$9óð <ô 9ØØØØ%Ø'Ø'ØCØ ó	ñ /ˆNÐ.ð  Õ,Ø¤× QÑ QÕQôRð ' ~Ð&6°iÀ¸}ÐMð Ô$ð Ø�JŠJ˜!�O�JØ˜1ÕˆKä4Ø˜: z°5¸%ÀóˆEô 2RØ�e˜X¸ó2Ñ.ˆI�y ,ð 'ˆDŒNØ&ˆDŒNØ ,ˆDÔØ!*¨YÕ!6ˆDÔä6ØØØØ#Ø#Ø?Ø"ØØ%Ø ô÷ 0õ Ká,MØØÜ  Ó%Ü1°%¸¸uÓEØ.Ø!ØØ Ø$ØBØCó-Ð)÷ -ØØ˜jÕ)‹IØ‹ä×4×4Ð4Ü2°5¸$ÀÓFˆ
Ø˜5Ð Ð ä1ØØØØØØ"Øó€Jð �uÐÐùòo	0ùòAùò>ùòp Ets   Â'%]>Ã)^Ä&^Ð^
c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )r   r   r   rI   r}   rJ   rK   rF   r   )r    s   "r!   r"   r"   ¿  sA   € ÷ [ñ [Ø#ð[àð[ð ð[ð ð	[ð
 ñ[r#   c                ó¬  aaaaaaaaaaaa€ / p/ o/ / / uooo\        V 4       F|  w  rVVP                  4        F  pWdV&   K	  	  VP                  4        F  pVSV&   K
  	  VSVP                  4       &   VP                  4       p	\        P
                  SV	&   ^ SV	&   VSV	&   K~  	  ^ p
V  Fª  pV'       dp   \        V4      '       d_   V
SVP                  4       &   VP                   F0  pSV,          P                  4       p\        SV,          V
4      SV&   K2  	  V
^,          p
Kz  V'       g   K„  \        V4      '       g   K—  ^SVP                  4       &   K¬  	   ! VVVV3R lR4      oV  Uu/ uF   pV\        R VP                   4       4      bK"  	  upo. o\        \        4      oV  Uu/ uF  qf\        V4      bK  	  upoSP                  4        FS  w  rm\        V4      ^ 8X  d   \         P"                  ! SS! V4      4       V F  pSV,          P%                  V4       K  	  KU  	  . oVVVVV3R loV3R loVVVV3R lpS'       dN   \         P&                  ! S4      P(                  pV'       d   \        V4      '       d   V! V4       KK  S! V4       KU  SP+                  4        F!  p\        V4      ^ 8w  g   K  \-        RS 24      h	  S# u upi u upi )a�  
Schedule `snodes` for various comm optimization objectives.

Args:
    snodes: the nodes to be scheduled.
    raise_comms: whether to greedily schedule collectives as early as possible
    sink_wait: whether to greedily schedule waits as late as possible
    reorder_compute_for_overlap: whether to reorder compute nodes to
        optimize for compute/communication overlapping.

Returns:
    The new schedule order.

Some notes on the synergy between different options:
    - `raise_comms` provides more overlapping opportunities for `reorder_compute_for_overlap`.
    - When both `raise_comms` and `sink_waits` is `True`, `raise_comms` is prioritized.
c                  ó6   <€ ] tR tRtR V VVV3R lltR tRtR# )Ú$_schedule_for_comm.<locals>.Runnablei   c               ó   € V ^8„  d   QhRR/# )r   rF   r\   r   )r    s   "r!   r"   Ú1_schedule_for_comm.<locals>.Runnable.__annotate__  s   € ÷ 	ñ 	 Tñ 	r#   c                	óÄ   <€ Wn         \        \        VP                  4       4      4      pSV,          P	                  4       pSV,          SV,          SV,          3V n        R # rk   )r8   ÚnextÚiterÚget_operation_namesr�   Úscore)rn   r8   re  Ú
fused_nameÚname_to_fused_nodeÚscores_0Úscores_1Úscores_2s   &&  €€€€r!   Ú__init__Ú-_schedule_for_comm.<locals>.Runnable.__init__  sT   ø€ ØŒJÜœ˜U×6Ñ6Ó8Ó9Ó:ˆDØ+¨DÕ1×:Ñ:Ó<ˆJà˜Õ$Ø˜Õ$Ø˜Õ$ðˆDŽJr#   c                	ó4   € V P                   VP                   8  # rk   ©rÅ  )rn   Úothers   &&r!   Ú__lt__Ú+_schedule_for_comm.<locals>.Runnable.__lt__  s   € Ø—:‘: §¡Ñ+Ð+r#   )rÅ  r8   N)rs   rt   ru   rv   rË  rÐ  rz   )rÇ  rÈ  rÉ  rÊ  s   €€€€r!   ÚRunnabler¾     s   ø† ÷	ó 	ö	,r#   rÒ  c              3  ó8   "  € T F  qP                   x € K  	  R # 5irk   ©re  )r�   Údeps   & r!   r�   Ú%_schedule_for_comm.<locals>.<genexpr>  s   é € ÐGÑ.F sŸ(ž(Ó.Fùr‘   c                ó  <€ SP                  V 4       V P                  4        Fa  pSV,           FQ  p SV ,          P                  V4       \        SV ,          4      ^ 8X  g   K4  \        P
                  ! SS! V 4      4       KS  	  Kc  	  R# )zE
Schedules `snode` and put all unblocked nodes onto the ready queue.
N)ré   rŸ   Úremover.   ÚheapqÚheappush)r8   Úbuf_namerÒ  Úbuffer_usersÚreadyÚ	scheduledró   s   & €€€€€r!   ÚscheduleÚ$_schedule_for_comm.<locals>.schedule  so   ø€ ð 	×Ñ˜ÔØ×.Ñ.Ö0ˆHØ% h×/Ð/�Ø˜5Õ!×(Ñ(¨Ô2Ü�z %Õ(Ó)¨QÖ.Ü—N’N 5©(°5«/Ö:ó 0ó 1r#   c                 óä   <€ S U u. uF?  p \        V P                  4      '       d   K   \        V P                  4      '       d   K=  V NKA  	  pp \        V4      ^ 8X  d   R# \	        VR R7      # u up i )zP
Return the next node in the ready queue that's neither a collective or
a wait.
Nc                ó   € V P                   # rk   rÎ  )rŽ   s   &r!   rh  ÚG_schedule_for_comm.<locals>.get_overlapping_candidate.<locals>.<lambda>6  s   € ¨Q¯WªWr#   )Úkey)r   r8   r   r.   Úmin)rŽ   Ú
candidatesrÝ  s     €r!   Úget_overlapping_candidateÚ5_schedule_for_comm.<locals>.get_overlapping_candidate*  sh   ø€ ñ ó
á�Ü& q§w¡w×/ô ä8EÀaÇgÁg×8N÷ ˆAÙð 	ð 
ô
 ˆz‹?˜aÔÙÜ�:Ñ#4Ô5Ð5ùò
s   †A-¨A-ÁA-c                ó0  <€ \        V 4      '       g   \        R4      hS! V 4       SV ,          pV^ 8”  dK   S! 4       ;pe@   SP                  V4       S! VP                  4       VSVP                  ,          ,          pKQ  \        P
                  ! S4       R# )z¨
Schedules collective node `snode`, along with one or more compute nodes
to overlap with it. The strategy is described in the comment of
`reorder_compute_for_overlap`.
z&expected snode to contain a collectiveN)r   r{  rØ  r8   rÙ  Úheapify)r8   Úcollective_costr  rç  rÝ  rß  Úsnode_to_costs   &  €€€€r!   Úschedule_collective_for_overlapÚ;_schedule_for_comm.<locals>.schedule_collective_for_overlap8  sz   ø€ ô # 5×)Ò)Ü Ð!IÓJÐJÙ�Œà'¨Õ.ˆà˜aÔÙ7Ó9Ð9�ÒFà�L‰L˜Ô#á�Y—_‘_Ô%à˜}¨Y¯_©_Õ=Õ=ŠOÜ�Š�eÖr#   z;Detected unscheduled nodes. Nodes with unmet dependencies: )r1   rŸ   rÄ  r�   ÚsysÚmaxsizer   Ú	ancestorsrå  r   r	   r˜  r   r–  ru  r.   rÙ  rÚ  rÌ   Úheappopr8   r6   r{  )r   rI   rJ   rK   Úbuf_name_to_snoderA   r8   rÛ  Úop_nameÚ	node_nameÚcomm_idxÚancestorÚanc_fused_nameÚdepsrÕ  rí  rÒ  rÜ  rç  rÇ  rÝ  rß  rÞ  rÈ  rÉ  rÊ  rì  ró   s   &&&&            @@@@@@@@@@@@r!   rM   rM   ¿  sœ  ÿû€ ðL ÐØÐØ#% r¨2Ð €Hˆh˜Ü Ö'‰
ˆØ×.Ñ.Ö0ˆHØ*/˜hÓ'ñ 1ð ×0Ñ0Ö2ˆGØ*/Ð˜wÓ'ñ 3à/4Ð˜5Ÿ>™>Ó+Ñ,à—N‘NÓ$ˆ	Ü!Ÿk™kˆ�ÑØˆ�ÑØ!ˆ�Óñ (ð €HÛˆßÔ.¨u×5Ò5Ø)1ˆH�U—^‘^Ó%Ñ&Ø!ŸOœO�Ø!3°HÕ!=×!FÑ!FÓ!H�Ü+.¨x¸Õ/GÈÓ+R�˜Ó(ñ ,ð ˜�MŠHß‰ZœM¨%×0Ô0Ø)*ˆH�U—^‘^Ó%Ó&ñ ÷,÷ ,ñ  ó<áˆEð 	ŒzÑG¨e×.FÒ.FÓGÓGÒGÙñ<€Jð
 €EÜ=HÌÓ=T€LÙDJÓKÁF¸5Ô/°Ó6Ò6ÁFÑK€Mà!×'Ñ'Ö)‰ˆÜˆt‹9˜Œ>Ü�NŠN˜5¡(¨5£/Ô2ÛˆCØ˜Õ×!Ñ! %Ö(ó ñ *ð €I÷	;ñ 	;õ6÷ð ÷, Ü—’˜eÓ$×*Ñ*ˆßÔ#6°u×#=Ò#=Ù+¨EÖ2á�UŽOà×!Ñ!Ö#ˆÜˆt‹9˜Ž>Ü ØMÈjÈ\ÐZóð ñ $ð
 ÐùòY<ùò Ls   Å)&KÆ'Kc               ó    € V ^8„  d   QhRRRR/# )r   Únodesr   rF   r   )r    s   "r!   r"   r"   ]  s   € ÷ ñ Ø"ðàñr#   c           
     ó¸  € \         P                  P                  4       '       g   V # V  Uu. uF  p\        V4      '       g   K  VNK  	  pp\	        ^\        V4      4       Fq  p\        \        WE,          P                  4       4      4      pWE^,
          ,          P                  4        F%  pWE,          P                  \        WvRR7      4       K'  	  Ks  	  V # u upi )z¼
Decide global ordering of comms, by just enforcing the ordering that's in the input graph
(might not be the same ordering as the eager mode program).
TODO: Come up with a better approach
T)Úmutating_bufr¥   )r3   r)   Úis_availabler   r,   r.   rÂ  rÃ  rŸ   Úadd_fake_depr   )rû  Úname_to_bufrÇ  r¢   Ú
comm_nodesrÄ   rý  rÎ   s   &&&     r!   Údecide_global_ordering_of_commsr  ]  s«   € ô ×Ñ×)Ñ)×+Ò+Øˆá"Ó=™U˜Ô&9¸!×&<—!�!™U€JÐ=ä�1”c˜*“oÖ&ˆäœD ¥×!?Ñ!?Ó!AÓBÓCˆØ !�eÕ$×5Ñ5Ö7ˆCØ�M×&Ñ&Ü˜ÀÔEöó 8ñ 'ð €Lùò >s   «CÁCc                  ó´   € ] tR tRt$ ^ tR]R&   RtR]R&   ^ tR]R&   RtR]R&   R	t	R]R
&   Rt
R]R&   RtR]R&   RtR]R&   RtR]R&   R	tR]R&   ]R 4       tRtR# )ÚSinkWaitInfoiu  r_   ra   rb   r]   rc   r`   Ú
moves_infor\   r^   rd   re   rf   rg   rh   ri   c                	ó<   € V P                   V P                  ,
          # rk   rl   rm   s   &r!   ro   ÚSinkWaitInfo.improvement‚  rq   r#   r   Nrr   )rs   rt   ru   rv   ra   rx   rc   r`   r  r^   re   rf   rg   rh   ri   ry   ro   rz   r   r#   r!   r  r  u  sw   ‡ à€GˆSÓØ€L�#ÓØ€Eˆ3ƒNØ€J�ÓØ!€O�SÓ!Ø€IˆuÓØ€IˆuÓØ!€O�UÓ!Ø€M�5ÓØ€L�#Óàñ9ó ô9r#   r  c               ó    € V ^8„  d   QhRRRR/# rN  r   )r    s   "r!   r"   r"   ‡  s   € ÷ (ñ (Ø ð(àñ(r#   c                ób  € \        V 4      '       d   RRV P                  4        23# \        V 4      '       d   RRV P                  4        23# \        P                  '       gM   \        V 4      '       d   RRV P                  4        23# \        V 4      '       d   RRV P                  4        23# R# )a0  
Check if a candidate node can be grouped during sink_waits pass.

Sink Waits traverses waits right to left, so we don't group with
processed waits on the right or with async collectives.

Args:
    candidate: Node to check for groupability

Returns:
    Tuple of (is_groupable, reason_if_not_groupable)
Fzcandidate contains wait ú$candidate contains_async_collective zcandidate contains collective zcandidate contains gemm_like rP  )r   r�   râ   r   Ú&sink_iterative_use_runtime_estimationsr   rŒ   rS  s   &r!   Ú!_is_node_groupable_for_sink_waitsr  ‡  sÊ   € ô" �Y×ÒØÐ0°×1CÑ1CÓ1EÐ0FÐGÐGÐGÜ  ×+Ò+àØ2°9×3EÑ3EÓ3GÐ2HÐIð
ð 	
ô
 ×>×>Ð>ô
 ˜y×)Ò)àØ0°×1CÑ1CÓ1EÐ0FÐGðð ô ˜i×(Ò(àØ/°	×0BÑ0BÓ0DÐ/EÐFðð ð Ðr#   c               ó@   € V ^8„  d   QhRRRRRRRRR	R
RRRRRR
RR
RR/
# )r   r  r   r©   r   r#  r_   r$  r   Ú7group_n_to_bufs_after_swap_dealloc_instead_of_candidater´   r4  r5  Úsize_free_delta_updater&  r»   rF   r\   r   )r    s   "r!   r"   r"   ²  sm   € ÷ 2
ñ 2
Ø ð2
à	 ð2
ð ð2
ð %ð	2
ð
 >Bð2
ð 4ð2
ð 9ð2
ð ð2
ð ð2
ð 
ñ2
r#   c	                óî  € V^ ,          p	Wy,          ^ ,          W‰,          P                   ,
          p
V'       g^   W£P                   ,           pVW³P                  ,
          3Wp&   V F-  pW|,          pV^ ,          V,           V^,          V,           3W|&   K/  	  R# V .VO FW  pW^,          pWŽ,          ;P                  VP                  V^ 4      ,          un        VWøV,          P                  ,
          3W~&   KY  	  R# )aâ  
Update memory tracking structures after swap (sink_waits version).

Updates curr_memory and snodes_allocfree dictionaries to reflect the new
memory state after swapping candidate with group.

Args:
    candidate: Node that was moved
    gns: Group nodes
    candidate_delta_mem: Net memory change from candidate (alloc - free)
    candidate_allocfree: Candidate's allocation/free info
    group_n_to_bufs_after_swap_dealloc_instead_of_candidate: Buffers whose deallocation moves from candidate to group
    post_alloc_update: Cached post-allocation memory values
    size_free_delta_update: Cached size-free delta values
    curr_memory: Current memory state dict (mutated)
    snodes_allocfree: Node allocation/free info dict (mutated)
N)r)  r+  r*  )r  r©   r#  r$  r  r4  r  r&  r»   r  Úpre_group_memÚcandidate_post_allocr­   r=  r¢   Ú
post_allocs   &&&&&&&&&       r!   Ú-_update_memory_tracking_after_swap_sink_waitsr  ²  sè   € ð8 �Q•€JØÕ+¨AÕ.Ð1AÕ1M×1XÑ1XÕX€MßBØ,×/MÑ/MÕMÐà Ø ×#@Ñ#@Õ@ð"
ˆÑó ˆBØ•ˆBà�1•Ð+Õ+Ø�1•Ð+Õ+ðˆK‹Oñ ñ 	àÐ˜#ÓˆØ&Õ)ˆ
ØÕ×%Ò%Ð)?×)CÑ)CÀAÀqÓ)IÕIÕ%àØ¨!Õ,×6Ñ6Õ6ð
ˆ‹ó r#   c               ó@   € V ^8„  d   QhRRRRRRRRRRR	R
RRRRRRRR/
# )r   r  r   r©   r   r  r"  r_   r#  r$  r   r  r´   r&  r»   rF   zFtuple[int, dict[BaseSchedulerNode, int], dict[BaseSchedulerNode, int]]r   )r    s   "r!   r"   r"   ç  s†   € ÷ CGñ CGØ ðCGà	 ðCGð "ðCGð ð	CGð
 ðCGð %ðCGð >BðCGð ðCGð ðCGð LñCGr#   c	                óp  € Wr,          ^ ,          W‚,          P                   ,
          p	/ p
/ p^ pV'       g'   \        W4,           W•P                   ,           4      pWÊV3# W•P                   ,           pWÚV &   Tp\        R \        P                  P                  VP                  4       4       4       4      pV) W°&   WN,           pV Fq  pVV,          ^ ,          V,           pVV
V&   \        VV4      p^ pVV9   d5   VV,          pV F   pVVP                  P                  ,          pK"  	  VVV&   VV,          pKs  	  WÊV3# )aO  
Calculate potential peak memory after swapping candidate with group (sink_waits version).

Computes new memory levels for all affected nodes and returns the potential
peak memory along with cached post-allocation and size-free delta values.

Args:
    candidate: Node being moved
    gns: Group nodes
    group_head: First node of group
    group_peak_memory: Current peak memory within the group
    candidate_delta_mem: Net memory change from candidate (alloc - free)
    candidate_allocfree: Candidate's allocation/free info
    group_n_to_bufs_after_swap_dealloc_instead_of_candidate: Buffers whose deallocation moves from candidate to group
    curr_memory: Current memory state dict
    snodes_allocfree: Allocation/free info for all nodes

Returns:
    Tuple of (potential_peak_memory, post_alloc_update_dict, size_free_delta_update_dict)
c              3  óN   "  € T F  pVP                   P                  x € K  	  R # 5irk   r8  r9  s   & r!   r�   Ú>_calculate_potential_peak_memory_sink_waits.<locals>.<genexpr>  s%   é € ð &ñ
ˆCð 	�‰× Ö ó
ùr;  )	r)  r(  r<  Ú	itertoolsÚchainÚfrom_iterabler6   rÊ   r+  )r  r©   r  r"  r#  r$  r  r&  r»   r  r,  Ú_size_free_delta_updater-  r  Úcandidate_size_free_to_moveÚ	delta_memr­   Úgn_post_allocÚgn_size_free_to_addr0  rÎ   s   &&&&&&&&&            r!   Ú+_calculate_potential_peak_memory_sink_waitsr!  ç  sT  € ð>  Õ+¨AÕ.Ð1AÕ1M×1XÑ1XÕX€Mà79ÐØ<>Ðà€NßBäØÕ3Ø×:Ñ:Õ:ó
ˆð Ð3JÐJÐJà(×+IÑ+IÕIÐØ$8�yÑ!Ø)€NÜ"%ñ &ä—?‘?×0Ñ0ØC×JÑJÓLô
ó&ó #Ðð +FÐ)EÐÑ&Ø#ÕA€IÛˆØ# B�¨Õ*¨YÕ6ˆØ!.Ð˜2ÑÜ˜^¨]Ó;ˆØÐØÐHÔHØJÈ2ÕNˆDÛ�Ø# s§~¡~×'?Ñ'?Õ?Ò#ñ à*=Ð# BÑ'ØÐ(Õ(Š	ñ ð Ð/FÐFÐFr#   c               ó4   € V ^8„  d   QhRRRRRRRRRRRRR	R/# r  r   )r    s   "r!   r"   r"   -  r  r#   c                ó†   € W1,          pV'       d   WV&   WcV &   W@,          pV'       d   W#V&   WtV&   WV&   WV &   W8X  d   V # V# )a…  
Swap positions of candidate and group in doubly-linked list (sink_waits version).

Transforms (moves candidate to the left):
group_head_prev -> group_head...group_tail -> candidate -> candidate_next
Into:
group_head_prev -> candidate -> group_head...group_tail -> candidate_next

Args:
    candidate: Node to swap with group
    group_head: First node of group
    group_tail: Last node of group
    prev_dict: Dictionary mapping nodes to their previous nodes
    next_dict: Dictionary mapping nodes to their next nodes
    head: Current head of the linked list

Returns:
    New head of the linked list (may change if group_head was the head)
r   )r  r  r  r  ræ   rä   Úgroup_head_prevÚcandidate_nexts   &&&&&&  r!   Ú+_perform_double_linked_list_swap_sink_waitsr&  -  sa   € ð8  Õ+€OßØ%.�/Ñ"Ø*ˆiÑð Õ)€NßØ$.�.Ñ!Ø*ˆjÑð &ˆjÑØ%ˆiÑð ÔØÐØ€Kr#   c               ó8   € V ^8„  d   QhRRRRRRRRR	RR
RRRRR/# )r   rV  z%dict[BaseSchedulerNode, SinkWaitInfo]rä   r   ræ   rç   rW  r_   r¹   r¸   r´   r·   rX  rF   r   r   )r    s   "r!   r"   r"   ^  sc   € ÷ Zñ ZØ0ðZà
ðZð AðZð ð	Zð
 ðZð !%ðZð #ðZð ñZr#   c                óÌ  a€ . ROpV P                  4        UU	u. uF­  w  r‰\        V4      V	P                  R,          V	P                  R,          V	P                  R,          V	P
                  R,          V	P                  R,          V	P                  V	P                  V	P                  V	P                  V	P                  V	P                  .NK¯  	  p
pp	Ro\        P                  P                  R4      '       d   ^ RIHp SV! V
VR7      ,          oMHSR,          oS\#        V4      R,           ,          oSRP%                  \'        \"        V
4      4      ,          o\(        P+                  S4       \-        VRV4      p\/        V4      V8w  d   \1        R	V R
\/        V4       24      h\3        WÅV4      w  p  pSRV 2,          oSRV 2,          o\5        RR V3R lR7       V# u up	pi )a�  
Format sink_waits statistics, log them, and return final node list.

Computes improvement metrics, creates a formatted table (using tabulate if
available), validates the reordered node count, recalculates peak memory,
and logs all information.

Args:
    stats: Per-node sink_waits statistics
    head: Head of the reordered linked list
    next_dict: Linked list next pointers
    original_snodes_num: Original number of nodes (for validation)
    peak_memory: Initial peak memory before reordering
    name_to_freeable_input_buf: Buffer memory tracking info
    graph_outputs: Graph output names

Returns:
    Final reordered list of scheduler nodes
rZ  rb   r[  r\  r]  r_  r`  Nra  rb  z*
 sink_waits_iterative peak_memory_before:z)
 sink_waits_iterative peak_memory_after:rc  c                 ó   € R RRR/# )re  Úsink_waits_iterative_inforf  rg  r   r   r#   r!   rh  Ú2_format_and_log_sink_waits_stats.<locals>.<lambda>²  s   € ØÐ/Ø˜ñ
r#   c                 ó   <€ S # rk   r   )Úlog_strs   €r!   rh  r+  ¶  s   ø€ ™7r#   rl  )z	Wait nodero  rp  rq  rr  rs  rt  ra   rc   r`   r  ri   )ru  rv  re   rf   rg   rh   ro   r^   ra   rc   r`   r  ri   rw  rx  ry  r[  r]   r¬   rz  r|  r}  rê   r.   r{  r   r   )rV  rä   ræ   rW  r¹   r¸   r·   r^  r8   r}  r�  r[  r‚  rƒ  r=   r-  s   &&&&&&&        @r!   Ú _format_and_log_sink_waits_statsr.  ^  sÔ  ø€ ò8€Gð: !Ÿ;™;œ=ôñ )‰KˆEô ˜ÓØ�N‰N˜SÕ Ø�N‰N˜SÕ Ø× Ñ  3Õ&Ø×Ñ Õ$Ø×Ñ˜sÕ"Ø× Ñ Ø�L‰LØ×ÑØ�J‰JØ�O‰OØ×Ñó	
ñ )ð 	ñ ð" €GÜ‡~�~×Ñ 
×+Ò+Ý%à‘8ØØô
õ 	
‰ð
 	ÐTÕTˆØ”3�w“< $Õ&Õ&ˆØ�4—9‘9œS¤ d›^Ó,Õ,ˆÜ×Ñ�WÔÜ.¨t°T¸9ÓE€JÜ
ˆ:ƒÐ-Ô-ÜØÐ+Ð,¨L¼¸Z»Ð8IÐJó
ð 	
ô  >Ø°ó Ñ€O�Q˜˜1ð Ð<¸[¸MÐJÕJ€GØÐ;¸OÐ;LÐMÕM€GÜØñ
ô #õð Ðùóas   ™B3G c          
     ó,   € V ^8„  d   QhRRRRRRRRR	R
/# rG  r   )r    s   "r!   r"   r"   »  sA   € ÷ 4Cñ 4CØ ð4Cà	 ð4Cð  ð4Cð >ð	4Cð
 >ñ4Cr#   c                ó  € \        \        4      pVP                  V \        4       4      pV F[  pW&,          pWp8w  d   K  VP                  P
                  pRp	V F  p
W¨9   g   K  T
p	K  	  V	f   KD  WI,          P                  V4       K]  	  V# )a}  
Find buffers whose last use will change after swapping in sink_waits pass.

When we swap [group] candidate to candidate [group], some buffers that
were last used by candidate will now be last used by a group node instead.
This is the opposite direction from the reorder version.

Args:
    candidate: The node being moved (currently last use)
    gns: Group nodes being swapped with candidate
    buf_to_snode_last_use: Mapping of buffers to their current last-use nodes
    candidate_buffer_map: Pre-computed map of node -> buffers using that node

Returns:
    Dict mapping group nodes to buffers that will change their last-use node
N)r   rž   r*  r	   rÊ   rË   ré   )r  r©   r¼   r¾   r  rI  rÎ   rK  rË   Úlast_succ_gnr­   s   &&&&       r!   Ú._find_buffers_with_changed_last_use_sink_waitsr2  »  s˜   € ô0 	”DÓð <ð *×-Ñ-¨i¼»ÓF€NãˆØ.Õ3ˆØÔ&Ùð —^‘^×.Ñ.ˆ
ØˆÛˆBØÖØ!’ñ ð ÒÙð 	@ÕM×TÑTØö	
ñ% ð, CÐBr#   c               ó    € V ^8„  d   QhRRRR/# )r   r   r   rF   zEtuple[list[BaseSchedulerNode], dict[BaseSchedulerNode, SinkWaitInfo]]r   )r    s   "r!   r"   r"   ò  s    € ÷ Nñ NØ#ðNàJñNr#   c                ó¢  € \        V 4      pV^ 8X  d   V / 3# \        \        P                  P                  P                  4       4      p\        \        P                  P                  4       4      p\        WV4      w  pppppp	\        V 4      w  r«p/ pV  Uu/ uF  pV\        V4      \        V4      ,          bK!  	  ppV  Uu/ uF#  qî\        R VP                  4        4       4      bK%  	  ppV  Uu/ uF   pV\        R VP                   4       4      bK"  	  ppV R,          p\        4       p\        P                  p\        P                   pRpVEe5   V
V,          Ee)   V
V,          pV'       d   EMVe   \        V4      V8¼  d   EMÿ\#        V4      '       d   VV9  g   TpKV  VP%                  V4       \'        4       ;pVV&   \)        VWÊVVV4      w  pppVV,
          ;Vn        Vn        VVn        VVn        VVn        VV,          pTpTp/ pRp VV,          ^ ,          p!\        VV,          4      p"\5        V4      p#VEe?   \        P6                  '       d;   VP,                  \        P8                  ) VP.                  ,          8  d
   RVn        EMîV"'       g   Rp$MVV,          p%\=        V%V",          4      p$\        P>                  '       g,   \A        V4      '       d   RVPC                  4        2Vn        EM‡T#;'       d    \5        V4      p&V$'       g
   V&'       EdD   \E        V4      w  p'p(V''       dÐ   TpV"PG                  VV,          4       T#;'       g    \5        V4      p#\        P6                  '       dL   \5        V4      '       d;   \I        VW¿VV4      w  ppp)VV3VV&   \A        V4      '       g   V VV,          ,          p \K        V!VV,          ^ ,          4      p!V;PL                  ^,          un&        VV,          pEKÇ  V$'       g:   \        PN                  '       g#   V&'       d   RVPC                  4        2Vn        EM<MR	VPC                  4        R
V( 2Vn        EM\        P6                  '       Edœ   \Q        VPR                  4      '       dX   \)        VVV
VVV4      w  ppp)\K        ^ VV,
          4      p*\K        ^ VV,
          V ,           4      p+V+V*8”  d   RV* RV+ 2Vn        EM•\A        V4      '       Eg   VV,          p,V,^ 8”  Ed   \        V4      ^ 8”  d÷   \K        ^ VP.                  VP0                  ,
          4      p*\K        ^ VP.                  VP0                  ,
          V,,
          4      p+V+V*,
          p-VPU                  4        FC  w  p.p/\K        ^ V.V/,
          4      p0\K        ^ V.V/,
          V,,           4      p1V-V1V0,
          ,          p-KE  	  V-^ 8”  d   RV, RV- 2Vn        EM—VPW                  4        F  w  p2w  p.p/V.V/V,,
          3VV2&   K  	  \Y        VVV4      p3VV,          p4V4PZ                  V4P\                  ,
          p5\_        VV3Wy4      p6\a        VV3VV!V5V4V6VV4	      w  p7p8p9V7V,
          V\        Pb                  ,          8”  d   RV7 RV 2Vn        MäV;Pd                  ^,          un2        V;Pf                  RVPC                  4        2,          un3        \i        VVVW«V4      p\)        VWÊVVV4      w  pppVVn        VVn        VV,
          Vn        VVn        \k        VV3V5V4V6V8V9VV4	       V'       d6   ^RI6H7p: V:! VV3\q        V34      \Y        VRV4      VVVVVRV64      pV'       d   MVV,          pEKC  TpEK9  \        Pr                  '       g   \Y        VRV4      p;V;V3# \u        VVVVVVV4      p;V;V3# u upi u upi u upi )r   c              3  ó@   "  € T F  qP                  4       x € K  	  R # 5irk   rÓ   rÔ   s   & r!   r�   Ú1_sink_waits_iterative_internal.<locals>.<genexpr>  r‰  r×   c              3  ó`   "  € T F$  p\        V4      '       d   K  VP                  x € K&  	  R # 5irk   r‹  rŒ  s   & r!   r�   r6    r�  rŽ  FNrú   r�  r
  z$collective ordering
 with candidate:r�  z
 non_group_reason:z"candidate is wait, exposed_before:z vs exposed_after:r‘  z2, group contains collectives, total_exposed_delta r’  r“  r  r”  Úsink_waits_iterativeéÿÿÿÿ);r.   r	   r   r°   r±   r²   r³   r¿   rÆ   r–  rÞ   r—  rØ   r˜  r   rš  Ú(sink_waits_iterative_debug_limit_to_sinkr   rÌ   r  r  rg   rh   re   rf   ri   r   r  Ú&sink_iterative_extra_comm_comp_overlapr^   r}   Ú*sink_waits_iterative_swap_with_collectivesrâ   r�   r  r�  r  r(  ra   Ú/sink_waits_iterative_unsafe_collectives_reorderr   r|   r6   ru  rê   r)  r+  r2  r!  Ú!sink_iterative_peak_memory_budgetr`   r  r&  r  rŸ  r•  r®   r   r.  )<r   rW  r±   r·   r¹   r½   r»   r¼   r¸   r¾   rÂ   rÃ   rÅ   rV  r8   rö   rî   rð   r¤  Úprocessed_waitsr¥  Údebug_num_sink_waits_to_reorderr¦  Ú
_prev_currr}  re   rf   ri   r  r  r  Úgroup_collsr©  r"  r«  Úgroup_contains_collectiver­  Úcandidate_dep_namesÚboth_contain_commsÚ_is_groupableÚgroupable_reasonr=   r³  r´  r²  rµ  Úgc_comm_timeÚgc_comp_timeÚgc_exposed_beforeÚgc_exposed_afterÚgcr©   r$  r#  r  r-  r,  r  r•  r‚  s<   &                                                           r!   Ú_sink_waits_iterative_internalrM  ò  s�  € ô ˜f›+ÐØ˜aÔØ�rˆzÐÜ$.¬q¯w©w×/CÑ/C×/HÑ/HÓ/JÓ$K€LÜ%/´·±×0HÑ0HÓ0JÓ%K€Mô 	$ F¸-ÓHñØØØØØ"Øô 9¸Ó@Ñ€E�%à35€Eñ ó0áˆEð 	Ô" 5Ó)Ô,EÀeÓ,LÕLÒLÙð ð 0ñ RXóAÙQWÈŒyÑC¨u×/@Ñ/@Ô/BÓCÓCÒCÑQWð ð Añ ó	>ñ ˆEð 	Œyñ 
Ø!×4Ò4ó
ó 
ò 	
ñ ð	 ð >ð &,¨B¥Z€Dä “l€Oä×=Ñ=ð %ô 	×=Ñ=ð $ð !&ÐØ
Ó
˜u T�{Ó6Ø˜4•[ˆ
ß$Ùà+Ò7Ü�OÓ$Ð(GÔGáä˜d×#Ò#¨°OÔ(CØˆDÙà×Ñ˜DÔ!Ü)›^Ð+ˆˆu�T‰{Ü-MØ�% Ð*:¸Mó.
Ñ*ˆ	�9˜lð 5>À	Õ4IÐIˆÔ˜tÔ1Ø"ˆŒØ"ˆŒØ(ˆÔà˜$•Kˆ	Øˆ
Øˆ
ØˆØˆØ(¨Õ.¨qÕ1Ðô (Ð(8¸Õ(>Ó?ÐÜ$7¸Ó$=Ð!àÓ#Ü×B×BÐBØ×"Ñ"Ü×FÑFÐFÈÏÉÕWôXð (J�Ô$Ù÷ &Ø ‘ð '4°IÕ&>Ð#ÜÐ 3Ð6HÕ HÓI�ô  ×J×JÐJÜ,¨Y×7Ò7à>¸y×?QÑ?QÓ?SÐ>TÐUð Ô(ñ ð ";÷ "ð "Ô?RØó@Ð÷ ×-Ð-Ü2SØó3Ñ/�Ð/÷ !Ø!*�Jð '×-Ñ-Ð.>¸yÕ.IÔJà1×SÐSÔ5HÈÓ5Sð .ô
 %×K×KÐKÜ/°	×:Ò:ä2RØ% uÐ8HÈ-ó3Ñ/˜	 9¨að 3<¸YÐ1G˜ IÑ.Ü8¸×CÒCØ)¨X°iÕ-@Õ@˜Mä(+Ø)¨<¸	Õ+BÀ1Õ+Eó)Ð%ð —L’L AÕ%•LØ % iÕ 0�IÚß!ä(×X×XÐXß.ð1Ø1:×1CÑ1CÓ1EÐ0FðHð Ô,ñ øð(Ø(1×(:Ñ(:Ó(<Ð'=Ø.Ð/?Ð.@ðBð Ô(ñ
 ä×B×BÑBÜ˜9Ÿ>™>×*Ò*ô /OØ!ØØØ Ø(Ø%ó/Ñ+�I˜y¨!ô &)¨¨I¸	Õ,AÓ%B�Nä$'¨¨9°yÕ+@À=Õ+PÓ$Q�Mð % ~Ô5ð/Ø/=Ð.>Ð>PÐQ^ÐP_ðað Ô,ñ ô 1°×;Ó;ð !)¨Õ 3�Ià  1•}¬¨[Ó)9¸AÔ)=ä),¨Q°·±ÀÇÁÕ0OÓ)P˜ä(+Ø˜tŸ~™~°·±Õ>ÀÕJó)˜ð )6¸Õ(F˜Ø:E×:LÑ:LÖ:NÑ6˜L¨,ä03°A°|ÀlÕ7RÓ0SÐ-ä/2Ø ! <°,Õ#>ÀÕ#Jó0Ð,ð *Ð-=Ð@QÕ-QÕQšMñ ;Oð )¨1Ô,à"8¸¸ð D8Ø8E°ð!Hð !Ô0ñ "ð "-×!2Ñ!2Ö!4ñ! ñ %Ø ,Ø ,ð %1Ø$0°9Õ$<ð3" ¨B£ñ "5ô ,IØ˜J¨ó,ˆCð 0@À	Õ/JÐà#×.Ñ.Ð1D×1NÑ1NÕNð  ô ?Ø˜sÐ$9óð Dô <ØØØØ%Ø'Ø'ØKØ Ø$ó
ñ HˆNÐ.Ð0Gð  Õ,Ø¤× NÑ NÕNôOð ' ~Ð&6°iÀ¸}ÐMð Ô$ð à�JŠJ˜!�O�JØ�OŠO  9×#5Ñ#5Ó#7Ð"8Ð9Õ9�Oä?Ø˜: z°5ÀóˆEô 2RØ�e HÐ.>Àó2Ñ.ˆI�y ,ð 'ˆDŒNØ&ˆDŒNØ!*¨YÕ!6ˆDÔØ ,ˆDÔä9ØØØ#Ø#ØGØ"Ø'ØØ ô
÷ 0ÝJá,MØØÜ  Ó%Ü1°%¸¸uÓEØ.Ø!ØØ Ø$Ø*ØKó-Ð)÷ -Øà˜jÕ)‹IØ‹ä×4×4Ð4Ü2°5¸$ÀÓFˆ
Ø˜5Ð Ð ä1ØØØØØØ"Øó€Jð �uÐÐùòs	0ùòAùò>s   Â%_Ã)_Ã0&_c               ó    € V ^8„  d   QhRRRR/# rE   r   )r    s   "r!   r"   r"   C  s   € ÷ 5ñ 5Ð!8ð 5Ð=Tñ 5r#   c                ó&   € \        V 4      ^ ,          # )a_  
Similarly to reorder_communication_preserving_peak_memory this pass will try to iteratively
push Wait nodes later, recomputing estimated peak memory before each swap,
and preventing peak memory regressions.

Pass will be applied to every Wait node. If there are immediate dependencies with next node,
pass will try to group them together and on the next step to swap the group with next candidate.

If _inductor.config_comms.sink_iterative_use_runtime_estimations is set True,
pass will stop reordering of Wait once corresponding Collective is unexposed,
based on runtime estimations.

inductor.config_comms.sink_iterative_peak_memory_budget allows to tune how much pass
can regress initial peak memory.
E.g.:
sink_iterative_peak_memory_budget == 0.0 - No regression of initial peak memory is allowed
sink_iterative_peak_memory_budget == 0.2 - Pass can improve comm-compute overlap, sacrificing
20% of initial peak memory value.

inductor.config_comms.sink_iterative_extra_comm_comp_overlap config allows to more aggressively
sink waits, stopping only when overlap_compute >= (1 + extra_comm_comp_overlap) * comm_time
)rM  rN   s   &r!   r8  r8  C  s   € ô. *¨&Ó1°!Õ4Ð4r#   c               ó    € V ^8„  d   QhRRRR/# )r   r8   r   rF   rd   r   )r    s   "r!   r"   r"   ]  s   € ÷ 
ñ 
Ð0ð 
°Uñ 
r#   c                óÔ   € \         P                  R8X  d   V P                  4       pV# \        \         P                  4      '       g   \	        R4      h\         P                  ! V 4      pV# )z3
Returns estimated op runtime in milliseconds (ms)
r„   z2expected config.estimate_op_runtime to be callable)r
   r–  r'   Úcallabler{  )r8   Úruntimes   & r!   r–  r–  ]  s[   € ô ×!Ñ! YÔ.Ø×-Ñ-Ó/ˆð
 €Nô œ×2Ñ2×3Ò3Ü Ð!UÓVÐVÜ×,Ò,¨UÓ3ˆØ€Nr#   c           
     ó�  € V P                  4       p\        V4      ^8X  Ed±   Rp\        V P                  \        P
                  \        P                  34      '       dŠ   RV P                  4        Uu. uF  q3P                  4       NK  	  up 2pRV P                   Uu. uF  qUP                  NK  	  up 2pRV P                  4        RV P                  P                   RV RV R2	pV P                  4        Uu. uF  qwP                  P                  4       NK  	  ppR	P                  V U	u. uFB  p	\        V	\        P                  4      '       d   R
V	P                   RV	P                    R2MRNKD  	  up	4      p
 V P                  P#                  4       pV P                  P&                  P(                   V V
 RV RV P+                  4       R R2# . pV F  pVP-                  \/        V4      4       K  	  V P&                  P(                   RRP                  V4       2# u upi u upi u upi u up	i   \$         d    Rp L®i ; i)é   rb   zouts:zins:Ú z (z)
 Ú(Ú)Ú,z (size=z	, stride=z.0fz ns)ú: z, )Ú	get_nodesr.   r“   r|   r   ÚExternKernelOutÚ_CollectiveKernelrØ   r�   r˜  re  r   Úget_output_specr¬   ÚLayoutÚsizeÚstrideÚmaybe_get_nameÚAttributeErrorÚ	__class__rs   r'   ré   rv  )r8   r   ÚdetailrÕ   Úouts_strr¦   Úins_strÚchildÚlayoutsÚlayoutÚout_tensor_inforõ  Ú	summariesÚchild_snodes   &             r!   rv  rv  j  s&  € Ø�_‰_Ó€FÜ
ˆ6ƒ{�aÕØˆÜ�e—j‘j¤2×#5Ñ#5´r×7KÑ7KÐ"L×MÒMØ°e×6GÑ6GÔ6IÓJÑ6I°§
¡
¦Ñ6IÑJÐKÐLˆHØ¨e×.FÒ.FÓGÑ.F¨ŸfœfÑ.FÑGÐHÐIˆGØ˜Ÿ™Ó)Ð*¨"¨U¯Z©Z×-JÑ-JÐ,KÈ4ÐPXÈzÐYZÐ[bÐZcÐcdÐeˆFØ=B¿_¹_Ô=NÓOÑ=N°E—:‘:×-Ñ-Ö/Ñ=NˆÐOØŸ(™(ñ
 &ó	ñ &�Fô ˜f¤b§i¡i×0Ò0ð ˜&Ÿ+™+˜ i°·±¨¸aÑ@àòñ &ñ	ó
ˆð	ØŸ
™
×1Ñ1Ó3ˆIð —*‘*×&Ñ&×/Ñ/Ð0°°¸Ð8IÈÈIÈ;ÐVXÐY^×YtÑYtÓYvÐwzÐX{Ð{ð  Að  	Að €IÛˆØ×Ñœ kÓ2Ö3ñ à�o‰o×&Ñ&Ð' r¨$¯)©)°IÓ*>Ð)?Ð@Ð@ùò/  KùÚGùâOùòøô ô 	àŠIð	ús+   Á1H!ÂH&Ã9"H+Ä,AH0Å<H5 È5IÉIc                ó�  € R pRpR p\        V 4       EF  w  rEVft   \        V4      '       d    V\        V4      ,          pVP                  pM.\	        VP                  4      '       d   MV\        V4      ,          pV! V\        V4       4       K}  \        V4      '       d4   V\        V4      ,          pVP                  pV! V\        V4       4       KÁ  \	        VP                  4      '       d   V! V\        V4       4       RpKó  V! VR\        V4       24       EK  	  \        P                  RVR,          R,           24       R# )rú   Nc                ó<   € \         P                  V R  RV 24       R# )z>6rZ  N)r|  Údebug)Ústepr±  s   &&r!   Ústep_logÚ#visualize_overlap.<locals>.step_log�  s   € Ü×Ñ˜T "˜I R¨ uÐ-Ö.r#   z| zEst. runtime (ms): iè  )r1   r   r–  r|   r   rv  r|  rp  )ÚorderÚtotal_est_runtimeÚcur_comm_noderr  rq  r8   s   &     r!   Úvisualize_overlaprw  ‰  s!  € ð  #ÐØ€Mò/ô ! ×'‰ˆØÒ Ü" 5×)Ò)Ø!Ô%8¸Ó%?Õ?Ð!Ø %§
¡
‘Ü˜Ÿ™×$Ò$ð à!Ô%8¸Ó%?Õ?Ð!Ù�Tœl¨5Ó1Ð2Ö4ä" 5×)Ò)Ø!Ô%8¸Ó%?Õ?Ð!Ø %§
¡
�Ù˜¤,¨uÓ"5Ð!6Ö8Ü˜Ÿ™×$Ò$Ù˜¤,¨uÓ"5Ð!6Ô8Ø $’á˜ ¤L°Ó$7Ð#8Ð9×:ñ- (ô. ×ÑÐ+Ð,=ÀÕ,DÀtÕ,KÐ+LÐMÖNr#   c               ó    € V ^8„  d   QhRRRR/# rE   r   )r    s   "r!   r"   r"   ­  s    € ÷ d=ñ d=Ð2ð d=Ð7Nñ d=r#   c           	     ó¢  aaaaaaaa€ \        V 4      ^8  d   V # V  Uu. uF  p\        V4      '       g   K  VNK  	  ppV  Uu. uF  p\        V4      '       g   K  VNK  	  ppV'       g   V'       g   V # V  Uu/ uF#  q\        R VP	                  4        4       4      bK%  	  ppV  Uu/ uF  q\        R VP
                   4       4      bK!  	  pp\        V 4      w  ooo\        V 4      w  oooopo^ p^ pR VVVVVVVV3R llp	V Fs  p
WZ,          pRpSV
,          pVeG   WM,          V,          '       d   M0\        V4      '       d   MV	! WÚRR7      '       g   MRpSV
,          pKJ  V'       g   Kj  V^,          pKu  	  \        V4       Fu  pWN,          pRpSV,          pVeI   VV,          V,          '       d   M1\        V4      '       d   MV	! VVRR7      '       g   MRpSV,          pKL  V'       g   Kl  V^,          pKw  	  V'       g	   V'       d    \        P                  R	VVSR
,          4       \        SRS4      # u upi u upi u upi u upi )zMove collectives earlier and waits later via adjacent swaps.
Each swap is safe: no collective reordering, no memory regression.c              3  ó@   "  € T F  qP                  4       x € K  	  R # 5irk   rÓ   rÔ   s   & r!   r�   Ú!simple_overlap.<locals>.<genexpr>¹  s   é € Ð;©? a—Z‘Z—\�\«?ùr×   c              3  ó8   "  € T F  qP                   x € K  	  R # 5irk   rÔ  rŒ  s   & r!   r�   r{  ½  s   é € Ð:Ñ%9 —V–VÓ%9ùr‘   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r  r   Ú
group_nodeÚ	sink_waitr}   rF   r   )r    s   "r!   r"   Ú$simple_overlap.<locals>.__annotate__Í  s6   € ÷ Pñ PØ$ðPà%ðPð ð	Pð
 
ñPr#   c               óà  <€ V.pSV ,          pVP                   VP                  ,
          p/ pV'       d3   \        WS
S4      p\        V VVSV,          ^ ,          VVVSS4	      w  r‰pM/\	        WS
S4      p\        V VVSV,          ^ ,          VVVS4      w  r‰VS8”  d   R# V'       d%   \        WVSSS4      o\        V VVVVV	VSS4	       R# \        WVSSS4      o\        V VVVVVV	SS
S4
       R# )r   FT)
r)  r+  r2  r!  rL  r2  r&  r  r   rE  )r  r~  r  Úgroup_nodesÚcandidate_afÚcandidate_deltaÚsize_free_cacheÚchanged_bufsr-  Úpost_alloc_cacheÚ_buf_last_useÚ_cand_buf_mapr½   rÅ   rÃ   rÂ   r¹   r»   s   &&$       €€€€€€€€r!   Úpeak_memory_safe_swapÚ-simple_overlap.<locals>.peak_memory_safe_swapÍ  sU  ø€ ð "�lˆØ'¨	Õ2ˆØ&×1Ñ1°L×4JÑ4JÕJˆØ8:ˆçÜIØ¨°}óˆLô <ØØØØ  Õ,¨QÕ/Ø#Ø Ø Ø Ø$ó
ñ >ˆN©oô ?Ø¨°}óˆLô 0XØØØØ˜ZÕ(¨Õ+ØØØØó	0Ñ,ˆNð ˜KÔ'ÙçÜ?Ø z°5¸%ÀóˆEô :ØØØØØØ ØØØ ô
ñ8 ô! 5Ø z°5¸%ÀóˆEô 7ØØØØØØØ ØØØ ôñ r#   FN)r  Tz;simple_overlap: moved %d collectives, %d waits (peak %d MB)i   )r.   râ   r   r—  rØ   r˜  rÆ   r¿   Úreversedr/   r}  rê   )r   ÚsÚcollectivesÚwaitsÚ
outputs_ofÚdeps_ofÚ	_freeableÚn_moved_collsÚn_moved_waitsrŠ  ÚcollÚ	coll_depsÚmovedÚpredÚwaitÚ	wait_outsÚsuccrˆ  r‰  r½   rÅ   rÃ   rÂ   r¹   r»   s   &                @@@@@@@@r!   Úsimple_overlaprœ  ­  sF  ÿ€ ô ˆ6ƒ{�Q„Øˆá$ÓE™f˜Ô(AÀ!×(D—1�1™f€KÐEÙÓ3™�1¤-°×"2�QˆQ™€EÐ3ßŸuØˆñ FLó;ÙEKÀŒ9Ñ;¨1¯=©=¬?Ó;Ó;Ò;ÁVð ð ;ñ
 EKó8ÙDJ¸qŒ9Ñ: Q×%9Ò%9Ó:Ó:Ò:ÁFð ð 8ô 9¸Ó@Ñ€Eˆ5�%ô 	$ FÓ+ñØØØØØØð €MØ€M÷P÷ Póh ˆØ•Mˆ	Øˆà�T�{ˆØÒØÕ )×+Ô+ØÜ(¨×.Ò.ØÙ(¨¸u×EÓEØàˆEØ˜•;ŠDç‰5Ø˜QÕŠMñ# ô* ˜–ˆØÕ$ˆ	Øˆà�T�{ˆØÒØ�t�}˜y×(Ô(ØÜ˜T×"Ò"ØÙ(¨¨t¸t×DÓDØàˆEØ˜•;ŠDç‰5Ø˜QÕŠMñ#  ÷& ŸÜ�‰ØIØØØ˜KÕ(ô		
ô )¨°°eÓ<Ð<ùò} FùÚ3ùò;ùò8s"   ŸH=·H=ÁIÁIÁ9)IÂ(%Ic               ó    € V ^8„  d   QhRRRR/# rE   r   )r    s   "r!   r"   r"   T	  s   € ÷ ñ Ø#ðàñr#   c                óô   € T p\         P                   Fa  p\        V\        4      '       d!   V\	        4       9   d   \	        4       V,          p\        V4      '       g   \        R V R24      hV! V4      pKc  	  V# )z3Invalid reorder_compute_and_comm_for_overlap pass: z is not callable)r
   Ú'reorder_for_compute_comm_overlap_passesr“   r]   ÚglobalsrR  r{  )r   rt  Úps   &  r!   Ú$reorder_compute_and_comm_for_overlapr¢  T	  sn   € ð €Eä×;Ô;ˆÜ�aœ×Ò !¤w£y¤.Ü“	˜!•ˆAÜ˜�{Š{Ü ØEÀaÀSÐHXÐYóð ñ �%“Šñ <ð €Lr#   )F)NN)]Ú
__future__r   rÙ  rw  r  Úloggingrï  Úcollectionsr   Údataclassesr   Útypingr   r   r3   Útorch._loggingr   Útorch.utils._ordered_setr	   rb   r
   r   r   Údependenciesr   r   r   Úmemoryr   r   r   r   Úutilsr   r   r   r   Úvirtualizedr   Ú	getLoggerrs   r/   Ú_loggingÚgetArtifactLoggerr|  r’   r   rC   rJ   rI   rS   rY   r[   r†   rŒ   rš   r£   r§   r®   r¿   rÆ   r¶   rÙ   rÞ   rà   râ   rê   rô   r  r  r   r2  rE  rL  rT  r„  rV   rM   r  r  r  r  r!  r&  r.  r2  rM  r8  r–  rv  rw  rœ  r¢  r   r#   r!   Ú<module>r±     s±  ðõ #ã Û Û Û Û 
Ý #Ý !ß %ã Ý +Ý /ç &Ñ &Ý !÷ ß%÷ó ÷ OÓ NÝ ð ×Ò˜Ó!€Ø�n‰n×.Ñ.¨x¸ÓC€çÝ;õ!KõHõõõ&#ðL ÷9ð 9ó ð9õ(õ"(õ÷ò0õ3ôõ<õ"(õ0ò"Gò
ò;õõ6.õ9.õx9.õx.õb@.õFJõZ$;õNõ<eõPUõp
[õ|ð0 ÷9ð 9ó ð9õ"(õV2
õjCGõL.õbZõz4CõnNõb
5õ4
òAò>!OõHd=÷Nr#   