
    Vh                     P   d dl Z d dlZd dlmZmZ d dlmZmZmZ d dl	m
Z
 d dlmZ d dlmZ g dZde j                   fd	Z e       Z e
d
      e G d d                    Z e
d
      	 ddedee   deeegef      deeee   f   fd       Z e
d
      dededefd       Zy)    N)	dataclassfield)AnyCallableOptional)compatibility)Graph)Node)get_source_partitionscheck_subgraphs_connectedSourcePartitionreturnc                  |   t        j                  t              } t        j                  j                  dd      j                         }| j                  |       t        j                         }t        j                  d      }|j                  |       |j                  |       | j                  |       d| _        | S )NPYTORCH_MATCHER_LOGLEVELWARNINGz%(filename)s > %(message)sF)logging	getLogger__name__osenvirongetuppersetLevelStreamHandler	FormattersetFormatter
addHandler	propagate)loggerlevelconsole	formatters       Z/home/dcms/DCMS/lib/python3.12/site-packages/torch/fx/passes/utils/source_matcher_utils.py_init_loggerr$      s    x(FJJNN5yAGGIE
OOE##%G!!">?I#U
gFM    F)is_backward_compatiblec                       e Zd ZU ee   ed<   eed<    ee      Zee   ed<    ee      Z	ee   ed<    ee      Z
ee   ed<   y)r   nodessource)default_factoryinput_nodesoutput_nodesparamsN)r   
__module____qualname__listr
   __annotations__r   r   r+   r,   r-    r%   r#   r   r   !   sT     : K $D9Kd9  %T:L$t*: t4FDJ4r%   r   graphwanted_sources	filter_fnc                    i }| j                   D ]  }|j                  j                  dd      x}q|j                  j                  dd      x}	 R|\  }}|j                  d      d   }	|	|v r5|j	                  |	i       }
|
j	                  |g       }|j                  |       |j                  j                  dd      x}|d   }|d   |v s|j	                  |d   i       }
|
j	                  |d   g       }|j                  |        dt        t           d	t        d
t        fd}i }|rYi }|j                         D ]B  \  }}|j                         D ci c]  \  }}t        t        ||            r|| }}}|||<   D |}|j                         D ]-  \  }}|j                         D cg c]  } |||       c}||<   / |S c c}}w c c}w )a  
    Args:
        graph: The graph we want to partition
        wanted_sources: List of sources of nodes that were decomposed from this
            source. This can be a function (ex. torch.nn.functional.linear) or a
            leaf module type (ex. torch.nn.Linear).

    Returns:
        Dictionary mapping sources that were given to a list of SourcePartitions
        that correspond to the list of nodes that were decomposed from the given
        source.
    source_fn_stackNtorch_fn.   r   r(   module_typer   c           	         t               }t               }t               }| D ]  }|j                  D ]9  }t        |t              s|| vs|j                  dk7  s)|j                  |       ; |j                  dk(  r|j                  |       l|j                  j                         D ]  }|| vs|j                  |         t        | |t        |      t        |      t        |            S )Nget_attr)
setargs
isinstancer
   opadduserskeysr   r0   )r(   r<   r+   r,   r-   nodeargusers           r#   make_partitionz-get_source_partitions.<locals>.make_partitione   s    eu 	+Dyy )c4(S-=#&&JBVOOC() ww*$

4 

) +u$ $$T*+	+ L
 	
r%   )r(   metar   split
setdefaultappendr0   r
   typer   itemsallmapvalues)r3   r4   r5   modulesrF   source_fn_str8   node_fqn	source_fnsource_fn_namediff_modules	partitionrI   retfiltered_modulestpname_to_partitionnamefiltered_name_to_partitionkvs                        r#   r   r   6   s
   $ 24G ' !IIMM*;TBBLK		j$77HQ #+Hi&__S1!4N/&11."E(33HbA	  & IIMM*;TBBLO$R(I|~-&11)A,C(33IaL"E	  &3'6
d4j 
t 
 
4 35C %,]]_ 	>!B! (9'>'>'@*#D)s9i01 i*& *
 $>R 	> # L1@A
K9.A.KAL J* Ls   "G,G	subgraph1	subgraph2c                     t        | j                        D ]2  }|j                  j                         D ]  }||j                  v s  y 4 y)z
    Given two subgraphs A and B (in the form of a list of nodes), checks if
    A has nodes connecting to at least one node in B -- aka there exists a node
    in B that uses a node in A (not the other way around).
    TF)reversedr(   rD   rE   )rb   rc   rF   rH   s       r#   r   r      sJ     ) JJOO% 	Dy&	 r%   )N)r   r   dataclassesr   r   typingr   r   r   torch.fx._compatibilityr   torch.fx.graphr	   torch.fx.noder
   __all__Loggerr$   r   r   r0   booldictr   r   r2   r%   r#   <module>ro      s    	 ( * * 1    Tgnn  
 e,
5 5  -5& e, 37ZZIZ $./Z 
#tO$
$%	Z -Zz e,+:	 -r%   