
    N j&              
          S r SSKrSSKrSSKr\R                  R
                  R                  \R                  R
                  R                  1rS\R                  R                  S\4S jrS\R                  R                  S\\\\\R                  R                  \\R                  R$                  4      4   SS4S jrS\S	\\\R                  R                  \\R                  R$                  4      S\4S
 jrS\R                  R                  S	\\\R                  R                  \\R                  R$                  4      S\\   S\4S jrg)aZ  
DCE pass for unused extra outputs in HOP subgraphs.

When enable_side_effects_with_extra_outputs is True, HOPs like invoke_subgraph and
checkpoint (tag_activation_checkpoint)
return all intermediate tensors/symints as extra outputs to support side effects.
However, many of these extra outputs may not actually be used in the parent graph.

This pass removes unused extra outputs by:
1. Collecting all callers for each subgraph
2. Checking if each output is used by all callers
3. Removing unused outputs from the subgraph's output node
4. Updating the HOP call and getitem indices in all call sites

    Ngmreturnc                    [         R                  " [        5      n[        X5        U(       d  gSnUR	                  5        GH  nUS   u  pEn[        XE5      n[        U[        R                  R                  5      (       d  MB  [        S UR                  R                   5       5      nUR                  S   n	[        U	[        [        45      (       d  M  [        U	5      n
[!        5       n[#        U
5       H&  n[%        X5      (       d  M  UR'                  U5        M(     S[        U5      s=:  a  U
:  d  M  O  M  [)        XsU5      (       d  GM  SnGM     U$ )a  
Remove unused extra outputs from HOP calls in all submodules.

For each subgraph output, check if any caller has a getitem for that index
with users. If no caller uses it, remove the output.
If the user in caller is an output node, to simply the algorithm, we do not recursively check
if the caller's output is used further up in the call chain.

Args:
    gm: The GraphModule to optimize

Returns:
    True if any modifications were made, False otherwise
Fr   c              3   H   #    U  H  oR                   S :X  d  M  Uv   M     g7foutputNop.0ns     p/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/torch/_dynamo/dce_extra_outputs.py	<genexpr>(dce_hop_extra_outputs.<locals>.<genexpr>@   s     O&:ddh>N11&:   "	"T)collectionsdefaultdictlist_collect_all_subgraph_usagesvaluesgetattr
isinstancetorchfxGraphModulenextgraphnodesargstuplelensetrange_is_output_usedadd_dce_subgraph)r   subgraph_id_to_callersmodifiedcallers	parent_gmsubgraph_name_subgraphoutput_nodeoutput_argsnum_outputsused_indicesidxs                r   dce_hop_extra_outputsr3      s   $ 	%  !<!H)002&-aj#	!94(EHH$8$899Ohnn&:&:OO!&&q)+t}55+&!$ %Cs,,  % &
 s< .;..X==/ 32 O    r'   c                 0   U R                   R                   H  nUR                  S:X  d  M  UR                  [        ;   d  M+  UR
                  S   n[        U[        R                  R                  5      (       d  Me  UR                  S:X  d  Mw  UR                  n[        U[        5      (       d   e[        XS5      n[        U[        R                  R                  5      (       d  M  [        U5      nX   R                  XU45        [        XQ5        M     g)z9Recursively collect all HOP usages across the graph tree.call_functionr   get_attrN)r   r   r
   target_HOPS_WITH_EXTRA_OUTPUTSr   r   r   r   Nodestrr   r   idappendr   )r   r'   nodesubgraph_attrr+   r-   subgraph_ids          r   r   r   U   s     77o%$++9Q*Q IIaLM=%((--88!$$
2 - 4 4!-5555"2d;h(<(<=="$X,K*7>>D1 1R r4   
output_idxr)   c                    U H|  u  p#nUR                    Hf  nUR                  S:X  d  M  UR                  [        R                  :X  d  M5  UR
                  S   U :X  d  MJ  [        UR                   5      S:  d  Me      g   M~     g)zECheck if output_idx is used by ANY caller (has a getitem with users).r6      r   TF)usersr
   r8   operatorgetitemr   r!   )rA   r)   
_parent_gm_subgraph_namehop_nodeusers         r   r$   r$   n   se    
 18,
HNNDww/)dkkX=M=M.M99Q<:-#djj/A2E # 18
 r4   r-   r1   c           	        ^^ [        S U R                  R                   5       5      n[        UR                  S   5      nSnU(       a1  [        U5      S:  a"  TR                  S5        TR                  S5        [        T5      [        U5      :  d  [        T5      S:X  a  g0 n/ nSn[        [        U5      5       H'  n	U	T;   d  M  XU	'   UR                  XI   5        US-  nM)     U R                  R                  U5         U R                  R                  [        U5      5      n
SSS5        UR                  W
5        U R                  R                  U5        U GH  u  pn[        UR                  5       GHL  nUR                  S:X  d  M  UR                   ["        R$                  :X  d  M6  UR                  S   n	['        U	[(        5      (       d   eX;  aA  [        [        UR                  5      5      S:X  d   eUR                  R                  U5        M  Xi   nUR                  R                  U5         UR                  R+                  ["        R$                  UR                  S   U4S9nUR,                  R/                  5       Ul        SSS5        UR                  W5        UR                  R                  U5        GMO     S	UR,                  ;   d  GM  UR,                  S	   m['        T[        [        45      (       d   e[        UU4S
 j[        [        U5      5       5       5      nUUR,                  S	'   GM     U R                  R1                  5         U R3                  5         U Vs1 s H  nUS   iM
     sn H-  nUR                  R1                  5         UR3                  5         M/     g! , (       d  f       GN= f! , (       d  f       GN?= fs  snf )z
DCE a subgraph by removing unused output indices.

Updates the subgraph's output node, all getitem nodes in callers,
and example_value metadata on HOP nodes.
c              3   H   #    U  H  oR                   S :X  d  M  Uv   M     g7fr   r	   r   s     r   r    _dce_subgraph.<locals>.<genexpr>   s     K"6Q$$(:Jqq"6r   r   F   rC   Nr6   )r   example_valuec              3   >   >#    U  H  nUT;   d  M  TU   v   M     g 7f)N )r   old_idxold_exampler1   s     r   r   rM      s'       6Gl* %G$6s   
T)r   r   r   r   r   r!   r%   r#   r=   inserting_beforer   r    replace_all_uses_with
erase_noderD   r
   r8   rE   rF   r   intr6   metacopylint	recompile)r-   r)   r1   r.   old_outputsis_autograd_fwd
old_to_newnew_outputsnew_idxrR   new_output_noder*   r,   rI   rJ   new_getitemnew_examplecallerrS   s     `               @r   r&   r&   {   s#    K(.."6"6KKK{''*+K O 3{+q0 <C,,L0AQ0F "$JKG[)*l"")w{34qLG	 + 
	(	(	5"..//k0BC 
6%%o6NNk*")	h(Dww/)dkkX=M=M.M))A,!'3////,tDJJ/0A555OO..t4$-__55d;"+//"?"? ((		!g/F #@ #K (,yy~~'7K$ < **;7**40' ), hmm+"--8KkE4=9999  $S%56  K
 .9HMM/*A #*F NN.56gFfQig6	 7 _ 
6	5& <;0 7s   %N8;AO
4O8
O

O)__doc__r   rE   r   opshigher_orderinvoke_subgraphtag_activation_checkpointr9   r   r   boolr3   dictrW   r   r    r;   r:   r   r$   r"   r&   rQ   r4   r   <module>rl      sa      
 
II**	II44 3ehh22 3t 3lSS T%,,c588==@ABBS
 
S2

%,,c588==@AB
 

]hh""]%,,c588==@AB] c(] 
	]r4   