
    `iS&                        d dl Z d dlmZ d dlmZ d dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZmZmZ d d	lmZ  e	d
          dedefd            Z e	d
          dedefd            Z e	d
          	 d!d
ddedededeeeee         f                  dedeeeedf         eedf         f         fd            Z e	d
          dededeedf         deedf         def
d            Z e	d
          dededdfd            Z e	d
          	 	 d"dede eeee         f                  dededef
d             Z!dS )#    N)SimpleQueue)Optional)compatibility)Graph)GraphModule)Node)legalize_graphNodeListNodeSet)lift_subgraph_as_moduleF)is_backward_compatiblenodesreturnc                 X   t                               | d          }t                      }| D ]A}|j        D ]}||v r||xx         dz  cc<   ||         dk    r|                    |           Bg }|                                s||                                }|                    |           |j        D ]7}||v r1||xx         dz  cc<   ||         dk    r|                    |           8|                                |t          |           t          |          k    s
J d            |S )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         u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/fx/passes/utils/fuser_utils.py	topo_sortr!      sf    ==**L$/MMJ ! !% 	( 	(AL  T"""a'"""""NN4   L   &~~D!!! 	& 	&AL  Q1$?a''NN1%%%    & u::\*****J +**     	partitionc                     t          |           g }D ]%}|j        D ]}|vr|                    |           &dt          dt          ffd} ||          rdS dS )N
root_nodesr   c                     t                      }| }|rU|                                }|                    |           |v rdS |j        D ]}||v r|                    |           |UdS )NTF)setpopaddr   r   )r%   visitedqueuecurrent	user_nodepartition_sets        r    bfs_find_cyclez*validate_partition.<locals>.bfs_find_cycle>   s     55
 % 
	(iikkGKK   -'' t$] ( (	''Y''''  
	( ur"   FT)r'   r   r   r
   bool)r#   outputsr   r-   r/   r.   s        @r    validate_partitionr2   -   s    
 	NNMG * * 	* 	*I--y)))	*8       4 ~g u4r"   always_return_tuplegmmodule_namepartition_lookup_tabler4   .c                   |D ]t}|j         j        | u s!J | d|                                              |j        rJ | d            || j         j        v s!J | d|                                              ut          |          s
J d            t                              |          t                      i i dt          dt          ffd}|D ]}
                    ||          }||<   i }|D ]}|j        D ]}	|	vr|         ||<   t          |                                          }
|r                    |
           n0                    t          |
          d	k    r|
d
         n|
                                            t#          | d|          \  }}t                                                    }t          |                                          }|||fS )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 cyclesNxr   c                     | j         dk    r	 | v r|          S | vrD                    | j        | j                  }t	          j        | j                  |_        || <   |          S )Nget_attr)	type_expr)opplaceholdernametypecopymeta)r9   placeholder_nodenode_mapnode_to_placeholderr7   subgraphs     r    remap_inputsz)fuse_as_graphmodule.<locals>.remap_inputs   s    4: &&& A;''''33AFaf3MM$(Iaf$5$5!%5""1%%r"   r   r    )	comp_name
class_name)graphowning_module	_get_name_erased_find_nodes_lookup_tabler2   r   r   r   r   	node_copyr   tuplevaluesoutputr   lintr   keys)r5   r   r6   r7   r4   r   rG   new_nodeoutput_mappingr-   outsfused_gm_original_inputsoriginal_outputsrD   rE   rF   s      `           @@@r    fuse_as_graphmoduler]   ^   s   D  
 
z'2---OOr||~~OO .-- <MMD!M!M!MMMrx8888CC2<<>>CC 9888
 e$$RR&RRR$ %!%u!5!5wwH 	  "$H& & & & & & & & & & &*  " "%%dL99! (*N 6 6 	6 	6I 666'/~t$	6 &&(())D = 	3t99>>Qt<<< MMOOO)
H{  KHa
 )..A.F.F.H.H(I(IO */~/B/B/D/D)E)E_&666r"   sub_gmorig_inputsorig_outputsc                 F   |j         j        }|                     ||           | j                            ||d           }|j                                        }t          |          dk    r>t          |j        d         t                    s|d         
                    |d           nwt          |          D ]F\  }}t          j                            |          |         j        }	|
                    |	d           Gt          d |D                       |j        d<   | S )N)argskwargsr   r   T)propagate_metac              3   L   K   | ]}|j                             d d          V   dS )valN)rB   r   ).0orig_outputs     r    	<genexpr>zinsert_subgm.<locals>.<genexpr>   sF       (
 (
2=K  --(
 (
 (
 (
 (
 (
r"   rf   )	__class____name__add_submodulerK   call_moduleoutput_noder   
isinstancerb   rQ   replace_all_uses_with	enumeratetorchfxProxyr   rB   )
r5   r^   r_   r`   submodule_namemodule_nodern   irh   	proxy_outs
             r    insert_subgmry      s/    %.N^V,,, (&&~KPT&UUK,**,,K
<Aj1A!1De&L&LQ--k$-OOOO'55 	N 	NNA{{33A6;I--i-MMMM"' (
 (
AM(
 (
 (
 #
 #
 Ir"   c                 ^    t          |          D ]}| j                            |           d S N)reversedrK   
erase_node)r5   r   r   s      r    erase_nodesr~      s<      " "
D!!!!" "r"   fused_
partitionsprefixc                    t          |          D ]m\  }}t          t          |                    }|t          |          z   }t	          | ||||          \  }}	}
t          | ||	|
           t          | |           nt          |            | S )Nr3   )rq   r!   liststrr]   ry   r~   r	   )r5   r   r   r4   partition_idr#   r   ru   r^   r_   r`   s              r    fuse_by_partitionsr      s     $-Z#8#8 & &i i11#l"3"33,? 3-
 -
 -
)\ 	Rl;;;B%%%% 2Ir"   r{   )r   F)"rA   r+   r   typingr   	_Optionaltorch.fxrr   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!   r0   r2   r   r   intrQ   r]   ry   r~   r   r    r"   r    <module>r      s#          ( ( ( ( ( (  1 1 1 1 1 1             - - - - - -       J J J J J J J J J J 9 9 9 9 9 9 e,,,X (    -,< e,,,-( -t - - - -,-` e,,,
 EI	r7 !&r7 r7 r7r7r7 r7 &d43+?&@A	r7 r7 ;dCi(%c	*::;r7 r7 r7 -,r7j e,,, tSy! c	"	
    -,: e,,,"K " "T " " " -," e,,,  %	 T$	#./0  	
    -,  r"   