
    8h<&                        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\\\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\\\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                 8   [         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      :X  d   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)r   indegree_map
candidatesnodensorted_nodess         [/var/www/fran/franai/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%	 	    u:\** J*     	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-   r/   r.   s        @r    validate_partitionr3   -   se    
 	NMGI-y) $ 8  4 gr"   always_return_tuplegmmodule_namepartition_lookup_tabler5   .c                  ^^^^ U H  nUR                   R                  U L d   U SU R                  5        35       eUR                  (       a
   U S35       eXPR                   R                  ;   a  Ml   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_placeholderr8   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_module	_get_name_erased_find_nodes_lookup_tabler3   r   r   r   r   	node_copyr   tuplevaluesoutputr   lintr   keys)r6   r   r7   r8   r5   r   rH   new_nodeoutput_mappingr-   outsfused_gm_original_inputsoriginal_outputsrE   rF   rG   s      `           @@@r    fuse_as_graphmoduler_   ^   s   D zz''2- 	
f>r||~>NO	
- <<MD6)L!MMxx888 	
f22<<>2BC	
8  e$$R&RR$ %!%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U R                  XA5        U R                  R	                  XBS S9nUR                  R                  5       n[        U5      S:X  a7  [        UR                  S   [        5      (       d  US   R                  USS9  U $ [        U5       HA  u  px[        R                  R                  U5      U   R                  n	UR                  U	SS9  MC     [        S U 5       5      UR                   S'   U $ )N)argskwargsr   r   T)propagate_metac              3   X   #    U  H   oR                   R                  S S5      v   M"     g7f)valN)rC   r   ).0orig_outputs     r    	<genexpr>insert_subgm.<locals>.<genexpr>   s'      (
AM+  --s   (*rh   )	__class____name__add_submodulerM   call_moduleoutput_noder   
isinstancerd   rS   replace_all_uses_with	enumeratetorchfxProxyr   rC   )
r6   r`   ra   rb   submodule_namemodule_noderq   irj   	proxy_outs
             r    insert_subgmr|      s    %%..N^, ((&&~PT&UK,,**,K
<Aj1A1A!1De&L&LQ--k$-O I (5NA{3A6;;I--i-M 6
 #( (
AM(
 #
 Ir"   c                 ^    [        U5       H  nU R                  R                  U5        M      g N)reversedrM   
erase_node)r6   r   r   s      r    erase_nodesr      s$     
D!  r"   
partitionsprefixc           	          [        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     [        U 5        U $ )Nr4   )rt   r!   liststrr_   r|   r   r	   )r6   r   r   r5   partition_idr#   r   rx   r`   ra   rb   s              r    fuse_by_partitionsr      st     $-Z#8 i1#l"33,? 3-
)\ 	R;B% $9" 2Ir"   r~   )fused_F)!rB   r+   r   typingr   	_Optionaltorch.fxru   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!   r1   r3   r   r   rS   r_   r|   r   r   r    r"   r    <module>r      s     (  1   -  J J 9 e,X (  -< e,-( -t - --` e,
 ;?	r7 !&r7r7r7 r7 &d4:&67	r7 r7 ;dCi(%c	*::;r7 -r7j e, tSy! c	"	
  -: e,"K " "T " -" e,  %	T$*%&  	
  -r"   