
    Ph"                        d dl Z d dlmZ d dlmZmZ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mZmZ d dl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e
de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   fd       Z ed
      de
defd       Z ed
      de
dee   de
fd       Zy)    N)SimpleQueue)ListDictTuple)GraphModule)Graph)Node)NodeListNodeSetlegalize_graph)lift_subgraph_as_module)compatibilityF)is_backward_compatiblenodesreturnc                    | D ci c]  }|d }}t               }| D ]?  }|j                  D ]  }||v s||xx   dz  cc<    ||   dk(  s/|j                  |       A t               }|j	                         so|j                         }|j                  |       |j                  D ].  }||v s||xx   dz  cc<   ||   dk(  s|j                  |       0 |j	                         sot        |       t        |      k(  sJ d       |S c c}w )Nr      z@topological sorted nodes doesn't have same length as input nodes)	r   all_input_nodesputlistemptygetappenduserslen)r   nodeindegree_map
candidatesnsorted_nodess         lC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/fx/passes/utils/fuser_utils.py	topo_sortr"      s    *//D1HL/)mJ%%AL T"a'" & "NN4   "VL ~~D!AL Q1$?a'NN1%	 	   u:\**n,nn*/ 0s   
D	partitionc                     t        |       t               }D ])  }|j                  D ]  }|vs|j                  |        + dt        dt
        ffd} ||      ryy)N
root_nodesr   c                     t               }| }|rP|j                         }|j                  |       |v ry|j                  D ]  }||v r|j	                  |        |rPy)NTF)setpopaddr   r   )r%   visitedqueuecurrent	user_nodepartition_sets        r!   bfs_find_cyclez*validate_partition.<locals>.bfs_find_cycle;   si     5
 %iikGKK -' $]]	'Y' +      FT)r'   r   r   r   r
   bool)r#   outputsr   r-   r/   r.   s        @r!   validate_partitionr3   *   sd    
 	NMGI-y) $ 8  4 gr0   gmmodule_name.c                    D ]|  }|j                   j                  | u sJ | d| j                                 |j                  r
J | d       || j                   j                  v rdJ | d| j                                  t              sJ d       t               i i fd}D ]  }j                  ||      }||<    i }D ]   }|j                  D ]  }|vs|   ||<    " t        |j                               }j                  t        |      dk(  r|d   n|       j                          t        | d|	      \  }	}
t        j                               }t        |j                               }|	||fS )
a1  
    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

    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 cyclesc                     | j                   dk(  r	 | v r|    S | vrPj                  | j                  | j                        }t	        j                  | j
                        |_        || <   |    S )Nget_attr)	type_expr)opplaceholdernametypecopymeta)xplaceholder_nodenode_mapnode_to_placeholderr   subgraphs     r!   remap_inputsz)fuse_as_graphmodule.<locals>.remap_inputs   sz    44: : A;'''33AFFaff3M$(IIaff$5!%5""1%%r0   r   r    )	comp_name
class_name)graphowning_module	_get_name_erasedr   r3   r   	node_copyr   tuplevaluesoutputr   lintr   keys)r4   r   r5   r   rE   new_nodeoutput_mappingr-   outsfused_gm_original_inputsoriginal_outputsrB   rC   rD   s    `           @@@r!   fuse_as_graphmodulerZ   [   s   4 zz''2-r$7abdbnbnbpaq/rr-<<MD6)L!MMrxx~~%^$/MbllnM]'^^%  e$R&RR$wH,.!#H&* %%dL9! 
 (*NI%'/~t$ $  &&()D OOs4yA~DG48 MMO)"h"Q\]KHa )..A.F.F.H(IO */~/B/B/D)E_&666r0   sub_gmorig_inputsorig_outputsc                    |j                   j                  }| j                  ||       | j                  j	                  ||d       }t        |      dk(  r|d   j                  |d       | S t        |      D ]D  \  }}t        j                  j                  |      |   j                  }|j                  |d       F | S )N)argskwargsr   r   T)propagate_meta)	__class____name__add_submodulerI   call_moduler   replace_all_uses_with	enumeratetorchfxProxyr   )	r4   r[   r\   r]   submodule_namemodule_nodeiorig_output	proxy_outs	            r!   insert_subgmrp      s     %%..N^V, ((&& ' K
 <AQ--k$-O I	 (5NA{{3A6;;I--i-M 6 Ir0   c                 Z    t        |      D ]  }| j                  j                  |        y )N)reversedrI   
erase_node)r4   r   r   s      r!   erase_nodesrt      s$     
D!  r0   
partitionsc                     t        |      D ]I  \  }}t        |      }dt        |      z   }t        | ||      \  }}}t	        | |||       t        | |       K t        |        | S )Nfused_)rg   r"   strrZ   rp   rt   r   )	r4   ru   partition_idr   r    rk   r[   r\   r]   s	            r!   fuse_by_partitionsrz      sl    (4e '!C$55,?LR`,a)\Rl;B%  5 2Ir0   )r>   r+   r   typingr   r   r   torch.fxrh   torch.fx.graph_moduler   torch.fx.graphr   torch.fx.noder	   torch.fx.passes.tools_commonr
   r   r   torch.fx.passes.utilsr   torch.fx._compatibilityr   r"   r1   r3   rx   rZ   rp   rt   rz    r0   r!   <module>r      s|     $ $  -    J J 9 1e,X (  -8 e,-( -t - --` e,]7K ]7']7%(]7-2;dCi@PRWX\^aXaRb3b-c]7 -]7@ e,[ + E$PS)DT dijnpsjsdt  -* e,"K " " -" e,; DN {  -r0   