
    N j2)                        S SK r S SKJr  S SKJr  S SKrS SKJ	r	  S SK
Jr  S SKJr  S SKJr  S SKJrJrJr  S S	KJr  \	" S
S9S\S\4S j5       r\	" S
S9S\S\4S j5       r\	" S
S9 S S
S.S\S\S\S\\\\\   4      S\S\\\\S4   \\S4   4   4S jjj5       r\	" S
S9S\S\S\\S4   S\\S4   S\4
S j5       r\	" S
S9S\S\SS4S j5       r\	" S
S9  S!S\S\ \\\\   4      S\S\S\4
S jj5       r!g)"    N)SimpleQueue)Optional)compatibility)Graph)GraphModule)Node)legalize_graphNodeListNodeSet)lift_subgraph_as_moduleF)is_backward_compatiblenodesreturnc                 @   [         R                  U S5      n[        5       nU  HD  nUR                   H  nXA;   d  M
  X==   S-  ss'   M     X   S:X  d  M3  UR	                  U5        MF     / nUR                  5       (       dy  UR                  5       nUR                  U5        UR                   H1  nXA;   d  M
  X==   S-  ss'   X   S:X  d  M   UR	                  U5        M3     UR                  5       (       d  My  [        U 5      [        U5      :w  a  [        S5      eU$ )Nr      z@topological sorted nodes doesn't have same length as input nodes)dictfromkeysr   all_input_nodesputemptygetappenduserslenAssertionError)r   indegree_map
candidatesnodensorted_nodess         r/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/torch/fx/passes/utils/fuser_utils.py	topo_sortr"      s    ==*L$/MJ%%A "a'" & "NN4    L  ~~D!A 1$?a'NN1%	 	    5zS&&N
 	
     	partitionc                    ^ [        U 5      m/ nT H/  nUR                   H  nUT;  d  M  UR                  U5        M     M1     S[        S[        4U4S jjnU" U5      (       a  gg)N
root_nodesr   c                    > [        5       nU nU(       a\  UR                  5       nUR                  U5        UT;   a  gUR                   H  nXA;   a  M
  UR	                  U5        M     U(       a  M\  g)NTF)setpopaddr   r   )r&   visitedqueuecurrent	user_nodepartition_sets        r!   bfs_find_cycle*validate_partition.<locals>.bfs_find_cycle?   si     5
 %iikGKK -' $]]	'Y' + e r#   FT)r(   r   r   r
   bool)r$   outputsr   r.   r0   r/   s        @r!   validate_partitionr4   .   se    
 	NMGI-y) $ 8  4 gr#   always_return_tuplegmmodule_namepartition_lookup_tabler6   .c                  ^^^^ U H  nUR                   R                  U La  [        U SU R                  5        35      eUR                  (       a  [        U S35      eXPR                   R
                  ;  d  Mt  [        U SU R                  5        35      e   [        U5      (       d  [        S5      eTc  [        R                  U5      m[        5       m0 m0 mS[        S[        4UUUU4S jjnU H  nTR                  XV5      nUTU'   M     0 nU H%  nUR                   H  n	U	T;  d  M  TU   X'   M     M'     [        UR                  5       5      n
U(       a  TR                  U
5        O%TR                  [!        U
5      S:X  a  U
S	   OU
5        TR#                  5         [%        U TS
US9u  p[        TR'                  5       5      n[        UR'                  5       5      nXU4$ )a  
Fuse nodes in graph_module into a GraphModule.

Args:
    gm (GraphModule): target graph_module

    nodes (List[Node]): list of nodes in `gm` to fuse, where the node must be topologically sorted

    module_name: class name for the fused GraphModule

    partition_lookup_table (Optional[Dict[Node, None]]): optional dict of nodes to speed up lookup

    always_return_tuple (bool): whether to always return a tuple, even if there is only one output

Returns:
    fused_gm (GraphModule): fused graph module, where its node is a copy of `nodes` in `gm`

    original_inputs (Tuple[Node, ...]): input nodes to `nodes` in original `gm`

    original_outputs (Tuple[Node, ...]): consumer nodes of `nodes` in original `gm`

z* doesn't belong to passed in graph module z# has been removed from owning graphz is not found in graph module z*Invalid partition, found dependency cyclesxr   c                    > U R                   S:X  a   U T;   a  TU    $ U T;  aN  TR                  U R                  U R                  S9n[        R                  " U R
                  5      Ul        UTU '   TU    $ )Nget_attr)	type_expr)opplaceholdernametypecopymeta)r;   placeholder_nodenode_mapnode_to_placeholderr9   subgraphs     r!   remap_inputs)fuse_as_graphmodule.<locals>.remap_inputs   s|    44: && A;'''33AFFaff3M$(IIaff$5!%5""1%%r#   r   r    )	comp_name
class_name)graphowning_moduler   	_get_name_erased_find_nodes_lookup_tabler4   r   r   r   r   	node_copyr   tuplevaluesoutputr   lintr   keys)r7   r   r8   r9   r6   r   rI   new_nodeoutput_mappingr.   outsfused_gm_original_inputsoriginal_outputsrF   rG   rH   s      `           @@@r!   fuse_as_graphmoduler`   _   s   D ::##2- &B2<<>BRS  << D6)L!MNNxx888 &6r||~6FG   e$$IJJ %!%u!5wH 	  "$H& & & &* %%d9! 
 (*NI 66'/~$ $  &&()D 	3t9>Qt< MMO)
H{KH
 )..A.F.F.H(IO */~/B/B/D)E&666r#   sub_gmorig_inputsorig_outputsc                   ^  UR                   R                  nT R                  XA5        S[        [        S4   S[        S -  4U 4S jjnU" U5      nUc  [        S5      eT R                  R                  U5         T R                  R                  XBS S9nUR                  R                  5       nS S S 5        WR                  n	T R                  R                  U	5         [        U5      S:X  a6  [        WR                  S   [        5      (       d  US   R                  US	S
9  Oo[!        U5       HA  u  p["        R$                  R'                  U5      U
   R(                  nUR                  US	S
9  MC     [        S U 5       5      UR*                  S'   S S S 5        T $ ! , (       d  f       N= f! , (       d  f       T $ = f)Ntarget_nodes.r   c                 d   > [        TR                  R                  5       H  nX;   d  M
  Us  $    g N)reversedrN   r   )re   r   r7   s     r!   	last_nodeinsert_subgm.<locals>.last_node   s*    RXX^^,D# - r#   zlast_output_node is None)argskwargsr   r   T)propagate_metac              3   X   #    U  H   oR                   R                  S S5      v   M"     g7f)valN)rD   r   ).0orig_outputs     r!   	<genexpr>insert_subgm.<locals>.<genexpr>   s'      ,EQk  $$UD11\s   (*ro   )	__class____name__add_submodulerT   r   r   rN   inserting_aftercall_moduleoutput_nodenextinserting_beforer   
isinstancerk   replace_all_uses_with	enumeratetorchfxProxyr   rD   )r7   ra   rb   rc   submodule_nameri   last_output_nodemodule_nodery   	next_nodeirq   	proxy_outs   `            r!   insert_subgmr      s    %%..N^,dCi 0 TD[  %.l$;788 
	!	!"2	3hh**T + 
 ll..0	 
4   I		"	"9	-|!*[5E5Ea5H%*P*PO11+d1S"+L"9!HHNN;7:??	11)D1Q #:
 ', ,EQ, 'KU# 
. I) 
4	3 
.	- Is   :5FB5F.
F+.
F=c                 ^    [        U5       H  nU R                  R                  U5        M      g rg   )rh   rN   
erase_node)r7   r   r   s      r!   erase_nodesr     s$     
D!  r#   
partitionsprefixc           	      Z   [        U5       HN  u  pE[        [        U5      5      nU[        U5      -   n[	        U UUUUS9u  pn
[        XX5        [        X5        MP     [        R                  R                  R                  R                  U 5        U R                  R                  5         U $ )Nr5   )r~   r"   liststrr`   r   r   r   r   passestools_commonstable_topological_sortrN   rW   )r7   r   r   r6   partition_idr$   r    r   ra   rb   rc   s              r!   fuse_by_partitionsr     s     $-Z#8 i1#l"33,? 3-
)\ 	R;B% $9  
HHOO  88<HHMMOIr#   rg   )fused_F)"rC   r,   r   typingr   	_Optionaltorch.fxr   torch.fx._compatibilityr   torch.fx.graphr   torch.fx.graph_moduler   torch.fx.noder   torch.fx.passes.tools_commonr	   r
   r   torch.fx.passes.utilsr   r"   r2   r4   r   r   intrT   r`   r   r   r   r    r#   r!   <module>r      s     (  1   -  J J 9 e,X (  -> e,-( -t - --` e,
 EI	v7 !&v7v7v7 v7 &d43+?&@A	v7 v7 ;dCi(%c	*::;v7 -v7r e,))) tSy!) c	"	)
 ) -)X e,"K " "T " -" e,  %	T$	#./0  	
  -r#   