+
    –°³jd,  ã                   ó,  € ^ RI t ^ RIt^ RIHt ^ RIHtHt ^ RIHt  ^ RI	H
t
HtHtHtHtHtHtHtHtHt ] P,                  ! ]4      t]P3                  ] P4                  4       RR R llt ! R	 R
]4      tR R ltR#   ] d   t]! R4      ]hRtAii ; i)é    N)ÚIntEnum)ÚGraphÚis_submodule)ÚSACStats)
ÚlpDotÚ	LpIntegerÚ
LpMaximizeÚ
LpMinimizeÚ	LpProblemÚLpStatusÚlpSumÚ
LpVariableÚPULP_CBC_CMDÚvaluezBPlease install pulp package. See: https://github.com/coin-or/pulp.c                óö   € V ^8„  d   QhR\         R\        R\        R\        \        ,          R,          R\        \        ,          R,          R\
        \        \        \        3,          \        \        3,          /# )é   ÚgraphÚmemory_budgetÚ
world_sizeÚac_unitsNÚ
fsdp_unitsÚreturn)r   ÚfloatÚintÚlistÚstrÚtupleÚdict)Úformats   "Úa/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torch/distributed/_tools/sac_ilp.pyÚ__annotate__r!   "   ss   € ÷ y6ñ y6Üðy6äðy6ô ðy6ô ”3�i˜$Õð	y6ô
 ”S•	˜DÕ ðy6ô Œ4””U�
ÕœU¤CÐ'Õ(ñy6ó    c                ó  a a&€ \        S P                  4      p\        V 3R l\        V4       4       ^R7      p\        ^d^V,          4      pR%p\	        R\
        4      p	\        P                  ! R\        \        V4      4      ^ ^\        4      p
\        P                  ! R\        \        V4      4      ^ ^4      p\        P                  ! R\        \        V4      4      ^ 4      p\        P                  ! R\        \        V4      4      ^ 4      p\        P                  ! R\        \        V4      4      ^ 4      p\        P                  ! R	\        \        V4      4      ^ 4      p\        P                  ! R
\        \        V4      4      ^ 4      p\        R^ 4      pV'       dQ   \        V4      p\        V4       F6  o&S P                  S&,          R,          V9  g   K$  WšS&,          ^ 8H  ,          p	K8  	  V'       di   \        V4       FY  o&\        ;QJ d     V V&3R lV 4       F  '       g   K   RM	  RM! V V&3R lV 4       4      '       g   KG  WšS&,          ^ 8H  ,          p	K[  	  \        V4       F^  o&\        S&^,           V4       FD  pS P                  S&,          V,          ^8X  g   K$  WšS&,          V
V,          ,           ^8*  ,          p	KF  	  K`  	  \        V4       F7  o&S P                  S&,          R,          '       g   K%  WšS&,          ^ 8H  ,          p	K9  	  \        V4       EF¼  o&S P                  S&,          R,          '       Eg   S P                  S&,          R,          S P                  S&,          R,          8  dã   \        P                  RS P                  S&,          R,          4       \        P                  RS P                  S&,          R,          4       \        P                  RS P                  S&,          R,          4       \        P                  R4       \        P                  R4       S P                  S&,          R,          S P                  S&,          R&   S P                  S&,          R,          V,          pS P                  S&,          R,          V,          pWœS&,          VVS&,          ,          VV,
          V
S&,          ,          ,
          8H  ,          p	EK¿  	  \        V4       F®  o&WšS&,          VS&,          8¬  ,          p	S P                  S&,          R,          '       d   K>  S P                  S&,          R,          V,          pS P                  S&,          R,          V,          pW›S&,          VV,
          V,          V
S&,          ,          8¬  ,          p	K°  	  \        V4       FÕ  o&S P                  S&,          R,          V,          pS P                  S&,          R,          V,          pS P                  S&,          R,          p^ .V,          p\        V4       F3  pS P                  S P                   V,          ,          R,          p^VV&   K5  	  W�S&,          VV,           \#        VV4      ,
          8H  ,          p	K×  	  S P                  ^ ,          R,          V,          p\        V4       FR  o&S P                  S&,          R,          V,          pWžS&,          VS&,          VV,           V,          ,           8H  ,          p	KT  	  \        V4       F  o&V	VVS&,          8¬  ,          p	K  	  \        V4       F–  o&\        S P                  S&,          R,          4       Fl  pS P                  S&,          R,          V,          pS P                  S&,          R ,          V,          pWŸS&,          VVS&,          ,          V,           8¬  ,          p	Kn  	  K˜  	  \        V4       F›  o&S P                  S&,          R!,          p V	VS&,          WzS&,          ,          8*  ,          p	V	VS&,          V VS&,          ,          8*  ,          p	V	VS&,          V VS&,          ,          V^V
S&,          ,
          ,          ,
          8¬  ,          p	K�  	  V	VV8*  ,          p	V	\%        V4      ,          p	\'        R"^´^ R#7      p!V	P)                  V!4      p"V"^8w  d'   \        P+                  R$\,        V",          4       / ^ R&3# / p#\        V4       F^  o&\/        V
S&,          P0                  4      ^8X  g   K&  \/        VS&,          P0                  ^4      V#S P                  S&,          R,          &   K`  	  \/        \3        V	P4                  4      ^4      p$\/        VP0                  V,          4      p%V#V$V%3# )'aî  
MILP to decide which modules to AC and how much memory to discard.
The objective is to minimize recomputation time.
The constraint is to ensure peak memory is under budget.

Args:
    graph: graph representation of the model as a module submodule tree
        where each node is a submodule with memory & runtime stats
    memory_budget: memory budget in GiB
    world_size: number of GPUs. In the case of FSDP, world_size will be
        used to compute the amount of parameter and gradient memory on each rank
    ac_units: a list of user-specified AC units.
    fsdp_units: a list of FSDP units. AC units cannot be supermodules of FSDP units.

Returns:
    Dict[str, float]: the optimal SAC solution, mapping from module fqn to
        the percentage of activation memory to **discard**
    float: the recomputation time of the optimal SAC solution
    int: upper bound on the peak memory of the optimal SAC solution.
        note that value of -1 means that the ILP solver failed to find a solution.

c              3   óX   <"  € T F  pSP                   V,          R ,          x € K!  	  R# 5i)Úsac_runtimeN)Únodes)Ú.0Úir   s   & €r    Ú	<genexpr>Úsac_milp.<locals>.<genexpr>A   s#   øé € ÐAÑ0@¨1ˆ�‰�Q�˜×	&Ò	&Ó0@ùs   ƒ'*)ÚdefaultÚSACÚyÚrÚdÚaÚmÚrcpÚrctÚmax_mÚfqnc              3   ól   <"  € T F)  p\        VSP                  S,          R ,          4      x € K+  	  R# 5i)r5   N)r   r&   )r'   Ú	fsdp_unitr   r(   s   & €€r    r)   r*   f   s/   øé € ð á!+�Iô ˜Y¨¯©°A­°uÕ(=×>Ð>Û!+ùs   ƒ14TFÚis_leafÚ
sac_memoryÚact_fw_per_modulezFor module {%s}: z.activation memory from memory tracker is {%d},z-activation memory from SAC estimator is {%d}.z!Something is wrong. Please check!z&Overriding the latter with the former.Úact_grad_per_moduleÚ	act_totalÚpos_fw_post_orderÚindexÚparam_per_moduleÚ
grad_totalÚ
n_segmentsÚslopesÚ
interceptsr%   çš™™™™™©?©ÚgapRelÚ	timeLimitÚmsgú$Solver failed to find a solution: %si   @éÿÿÿÿ)Úlenr&   ÚmaxÚranger   r
   r   Úmatrixr   r   ÚsetÚanyÚ	ad_matrixÚloggerÚwarningÚ	name2nodeÚfw_post_orderr   r   r   ÚsolveÚerrorr   ÚroundÚvarValuer   Ú	objective)'r   r   r   r   r   Ú	num_nodesÚmax_runtimeÚMÚMEM_MULTIPLIERÚprobr-   r.   r/   r0   r1   r2   r3   r4   Úac_units_setÚjÚACM_iÚIA_iÚAG_iÚTA_iÚposÚcoeffÚpÚP_1ÚTG_iÚsÚslopeÚ	interceptÚACT_iÚsolverÚstatusÚac_decisionsÚrecomputation_timeÚpeak_memr(   s'   f&&&&                                 @r    Úsac_milprt   "   sP  ù€ ô: �E—K‘KÓ €IÜÜA´°iÔ0@ÓAÈ1ô€Kô 	ˆE�1�{•?Ó#€AØ€Nô �UœJÓ'€Dô 	×Ò˜#œt¤E¨)Ó$4Ó5°q¸!¼YÓG€Aä×Ò˜#œt¤E¨)Ó$4Ó5°q¸!Ó<€Aä×Ò˜#œt¤E¨)Ó$4Ó5°qÓ9€Aä×Ò˜#œt¤E¨)Ó$4Ó5°qÓ9€Aä×Ò˜#œt¤E¨)Ó$4Ó5°qÓ9€Aä
×
Ò
˜E¤4¬¨iÓ(8Ó#9¸1Ó
=€Cä
×
Ò
˜E¤4¬¨iÓ(8Ó#9¸1Ó
=€Cä�w Ó"€E÷ Ü˜8“}ˆÜ�yÖ!ˆAØ�{‰{˜1�~˜eÕ$¨LÖ8Ø˜!� ™	Õ!’ñ "÷
 Ü�yÖ!ˆAß‹sõ á!+ó�s�sŠsõ á!+ó÷ ô ð ˜!� ™	Õ!’ñ "ô �9ÖˆÜ�q˜1•u˜iÖ(ˆAØ�‰˜qÕ! !Õ$¨Ö)Ø˜!�˜q �t� qÑ(Õ(’ó )ñ ô �9ÖˆØ�;‰;�q�>˜)×$Ö$Ø�a•D˜A‘IÕŠDñ ô
 �9×ˆð —‘˜A•˜y×)Õ)¨u¯{©{¸1­~Øõ0
à�K‰K˜�NÐ.Õ/ô00ô �N‰NÐ.°·±¸Aµ¸uÕ0EÔFÜ�N‰NØ@Ø—‘˜A•Ð2Õ3ôô �N‰NØ?Ø—‘˜A•˜|Õ,ôô �N‰NÐ>Ô?Ü�N‰NÐCÔDØ+0¯;©;°q­>Ð:MÕ+NˆE�K‰K˜�N˜<Ñ(Ø—‘˜A•˜|Õ,¨~Õ=ˆØ�{‰{˜1�~Ð1Õ2°^ÕCˆØ�!•˜  !¥�¨°­¸¸!½Õ'<Õ<Ñ<Õ<‹ñ/ ô< �9ÖˆØ�!•˜˜!�‘ÕˆØ�;‰;�q�>˜)×$Ô$ÙØ—‘˜A•˜|Õ,¨~Õ=ˆØ�{‰{˜1�~Ð1Õ2°^ÕCˆØ�!•˜ �¨Õ.°°1µÕ5Ñ5Õ5Šñ ô �9ÖˆØ�{‰{˜1�~Ð3Õ4°~ÕEˆØ�{‰{˜1�~˜kÕ*¨^Õ;ˆà�k‰k˜!�nÐ0Õ1ˆØ��i•ˆÜ�s–ˆAØ—‘ × 3Ñ 3°AÕ 6Õ7¸Õ@ˆAØˆE�!‹Hñ ð 	�!•˜˜t�¤e¨E°1£oÕ5Ñ5Õ5Šñ ð �+‰+�a�.Ð+Õ
,¨~Õ
=€CÜ�9ÖˆØ�{‰{˜1�~˜lÕ+¨nÕ<ˆØ�!•˜˜!�  d¥
¨jÕ8Õ8Ñ8Õ8Šñ ô
 �9ÖˆØ�˜˜1�‘ÕŠñ ô �9ÖˆÜ�u—{‘{ 1•~ lÕ3Ö4ˆAØ—K‘K •N 8Õ,¨QÕ/ˆEØŸ™ A� |Õ4°QÕ7ˆIØ˜•F˜e a¨¥d�l¨YÕ6Ñ6Õ6ŠDó 5ñ ô �9ÖˆØ—‘˜A•˜}Õ-ˆØ��A•˜! �d�(Ñ"Õ"ˆØ��A•˜% # a¥&�.Ñ(Õ(ˆØ��A•˜% # a¥&�.¨1°°A°aµDµ­>Õ9Ñ9Õ9Šñ	 ð 	ˆE�]Ñ"Õ"€Dð 	ŒE�#‹JÕ€Dô  °¸!Ô<€FØ�Z‰Z˜Ó€Fð �„{Ü�‰Ð;¼XÀfÕ=MÔNØ�1�bˆyÐð €LÜ�9ÖˆÜ��1•—‘Ó 1Ö$Ü27¸¸!½¿¹ÀqÓ2IˆL˜Ÿ™ Q�¨Õ.Ó/ñ ô œu T§^¡^Ó4°aÓ8ÐÜ�U—^‘^ nÕ4Ó5€HàÐ+¨XÐ5Ð5r"   c                   ó   € ] tR t^Þt^ t^tRtR# )ÚSACDecision© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú	RECOMPUTEÚSAVEÚ__static_attributes__rw   r"   r    rv   rv   Þ   s   † Ø€IØ„Dr"   rv   c                óR   € V ^8„  d   QhR\         R\        R\        \        ,          /# )r   Ú	sac_statsr   r   )r   r   r   r   )r   s   "r    r!   r!   ã   s-   € ÷ F:ñ F:ÜðF:Ü(-ðF:ä	Œ#…YñF:r"   c                ó4  € ^ Tu;8:  d   ^8:  g   M \        RV R24      h\        V P                  4      p\        R\        4      p\
        P                  ! R\        \        V4      4      ^ ^\        4      pV P                  '       d?   V P                   F-  pW4V,          \        P                  P                  8H  ,          pK/  	  MK\        V P                  RR
 V P                  R,          4       F  w  rgW4V,          WG,          8H  ,          pK  	  V P                    F-  pW4V,          \        P"                  P                  8H  ,          pK/  	  V P$                   FO  w  r‰W‰8w  d   W4V,          WI,          8H  ,          pK%  W4V,          \        P                  P                  8H  ,          pKQ  	  \&        P(                  ! V\+        V P,                  4      ,          4      p
V\/        W@P,                  4      V
8*  ,          pV\/        W@P0                  4      ,          p\3        R^
^ R7      pVP5                  V4      pV^8w  d$   \6        P9                  R	\:        V,          4       . # \        V4       Uu. uF  p\=        WE,          P>                  4      NK   	  up# u upi )a  
This is adapted from --
https://github.com/facebookresearch/xformers/blob/c6c0ac31f1b08542a0bc27278c6ed10f825f6963/xformers/checkpoint.py#L375

Given the SACStats of a module, including list of operators, their memory, runtimes, and metadata,
decide via MILP an optimal set of operators to checkpoint under a given ``memory_budget``.

Args:
    sac_stats: the SACStats object of the module
    memory_budget: a float between zero and one

Returns:
    List[int]: the decision whether each operator should be saved (1) or recomptued (0).
z5`memory_budget` must be a float between 0 and 1. Got Ú.zSAC-per-moduleÚxN:é   NNrD   rE   rI   rJ   ) Ú
ValueErrorrK   Ú
func_namesr   r	   r   rN   r   rM   r   Úforce_store_randomÚrand_opsrv   r}   r   ÚzipÚview_like_opsr|   Úinplace_opsÚmathÚceilÚsumÚmemoryr   Úruntimesr   rV   rR   rW   r   rX   rY   )r€   r   Únum_opsr_   rƒ   r(   Úi1Úi2ÚopÚ	op_parentÚ
max_memoryro   rp   s   &&           r    Ú+get_optimal_checkpointing_policy_per_moduler—   ã   s  € ð" �Ö# !Ö#ÜØCÀMÀ?ÐRSÐTó
ð 	
ô �)×&Ñ&Ó'€Gô Ð%¤zÓ2€Dô 	×Ò˜#œt¤E¨'£NÓ3°Q¸¼9ÓE€Að
 ×#×#Ð#Ø×#Ô#ˆAØ�a•DœK×,Ñ,×2Ñ2Ñ2Õ2ŠDò $ô ˜)×,Ñ,¨S¨bÐ1°9×3EÑ3EÀbÕ3IÖJ‰FˆBØ�b•E˜Q�U‘NÕ"ŠDñ Kð ×$Ô$ˆØ�!•œ×-Ñ-×3Ñ3Ñ3Õ3Šñ %ð #×.Ô.‰ˆØŒ?Ø�b•E˜Q�\Ñ)Õ)ŠDà�b•Eœ[×-Ñ-×3Ñ3Ñ3Õ3ŠDñ	 /ô —’˜=¬3¨y×/?Ñ/?Ó+@Õ@ÓA€JØŒE�!×%Ñ%Ó&¨*Ñ4Õ4€Dð
 	ŒE�!×'Ñ'Ó(Õ(€Dô  °¸Ô;€FØ�Z‰Z˜Ó€Fð �„{Ü�‰Ð;¼XÀfÕ=MÔNØˆ	ô +0°¬.Ó9©. QŒE�!•$—-‘-Ö ©.Ñ9Ð9ùÒ9s   É.$J)r„   NN)ÚloggingrŒ   Úenumr   Ú"torch.distributed._tools.ilp_utilsr   r   Ú&torch.distributed._tools.sac_estimatorr   Úpulpr   r   r	   r
   r   r   r   r   r   r   ÚImportErrorÚerrÚ	getLoggerrx   rR   ÚsetLevelÚINFOrt   rv   r—   rw   r"   r    Ú<module>r¢      s‹   ðÛ Û Ý ç BÝ ;ð÷÷ ÷ ð$ 
×	Ò	˜8Ó	$€ð ‡��—‘Ô ÷y6ôx�'ô ÷
F:øð[ ô Ù
ØLóàðûðús   žA> Á>BÂ	BÂB