
    Vh                     t   U d dl Z d dlZd dlmZ d dlmZ d dlmZ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mZmZ g dZej*                  j,                  ej*                  j.                  j0                  hej*                  j2                  ej*                  j.                  j4                  hej*                  j6                  ej*                  j.                  j8                  hej*                  j:                  ej*                  j.                  j<                  ej*                  j.                  j>                  hej*                  j@                  ej*                  j.                  jB                  hej*                  jD                  ej*                  j.                  jF                  ej*                  j.                  jH                  hejJ                  ejJ                  ejL                  d	d
hejN                  ejN                  ejP                  ddhga)e*e+   e,d<   d Z- e-       a.de*e+   fdZ/d"dZ0dee   fdZ1d Z2de*e   fdZ3	 	 d#dejh                  jj                  de*e   de	eege6f      fdZ7dejh                  jj                  dejh                  j                  de8de9e:ej*                  jv                  ejh                  j                  f   fdZ<dejh                  jj                  de*e9e:ej*                  jv                  ejh                  j                  f      fdZ=de
eejh                  jj                  f   d eddfd!Z>y)$    N)OrderedDict)Sequence)AnyCallableOptionalUnion)ExportedProgram)Node)check_subgraphs_connectedget_source_partitionsSourcePartition)find_sequential_partitionsget_equivalent_typesupdate_equivalent_types_dictbfs_trace_with_node_processaddadd_mulmul__EQUIVALENT_TYPESc                  J    i } t         D ]  }|D ]  }t        |      | |<     | S N)r   list)_DICTvaluesvs      V/home/dcms/DCMS/lib/python3.12/site-packages/torch/ao/quantization/pt2e/graph_utils.py_create_equivalent_types_dictr   %   s9    E# $ 	$AF|E!H	$$ L    returnc                      t         S r   )r    r   r   r   r   0   s    r   c                 6    | t        d      | at               ay)zHelp function for user who wants to customize the _EQUIVALENT_TYPES and _EQUIVALENT_TYPES_DICT.
    When customized_equivalent_types passes in,
    re-generate _EQUIVALENT_TYPES and _EQUIVALENT_TYPES_DICT.
    Nz.customized_equivalent_types should not be None)
ValueErrorr   r   _EQUIVALENT_TYPES_DICT)customized_equivalent_typess    r   r   r   4   s'    
 #*IJJ 4:<r   
partitionsc                 :    d }| D ]  }|t        ||      s y|} y)NFT)r   )r'   prev_partition	partitions      r   _partitions_sequentialr+   A   s8    N #	%.GI/
 "# r   c                 L    | g}| t         v r|j                  t         |           |S r   )r%   extend)partition_typematching_typess     r   _get_matching_typesr0   L   s-    $%N//4^DEr   partition_typesc                     t               }| D ]0  }t        |      }t        |      }t        ||z        dkD  r y||z  }2 y)Nr   FT)setr0   len)r1   partition_types_setr.   r/   matching_types_sets        r   _valid_type_sequencer7   S   sV    %) 2,^< 0"%7781<112 r   gm	filter_fnc                    t        |      st        d| d      t               }|D ]]  }t        |      }t	        | j
                  ||      }t        t        j                  j                  |j                                     ||<   _ t        |j                               }t        j                  | }	|	D 
cg c]  }
t        |
      r|
 }}
|S c c}
w )NzInvalid partition types: z*. Each type in the sequence must be unique)r7   r$   r   r0   r   graphr   	itertoolschainfrom_iterabler   productr+   )r8   r1   include_functional_equivalentr9   typed_partitionsr.   types_to_matchr'   typed_partitions_listfusion_candidates	candidatefused_partitionss               r   r   r   ^   s      0''88bc
 	
 AL) 
,^<*288^YO
+/OO))**;*;*=>,
(
 !!1!8!8!:;!))+@A +!), 	 
 s   6Cgraph_modulenode	arg_indexc                    |j                   |   }t        |t        j                  j                        sJ |j
                  dk(  sJ t        |j                  t              sJ | j                  |j                        }|j                  ||fS )Nget_attr)	args
isinstancetorchfxr
   optargetstrget_submodule)rG   rH   rI   submod_node	submodules        r   _get_submodulerV   {   s{     ))I&Kk588==111>>Z'''k((#...**;+=+=>Iy$..r   c                    g }| j                   j                  D ]  }|j                  dk7  r|j                  t        j
                  j                  j                  u r8|j                  t        | |d             |j                  t        | |d             |j                  t        j
                  j                  j                  u s|j                  t        | |d              |S )a{  
    Returns a list of submodules used for control flow operations
    (torch.ops.higher_order.cond/map) that are in the given toplevel graph (does not look
    into submodules). Specifically, the returned value is a list containing a
    tuple of (name of the submodule that's stored in the graph module, the
    submodule itself, and the fx node that uses this submodule).
    call_function      r   )r;   nodesrP   rQ   rN   opshigher_ordercondappendrV   map_impl)rG   control_flow_submodulesrH   s      r   _get_control_flow_submodulesrb      s     !""(( R77o%;;%))00555#**>,a+PQ#**>,a+PQ;;%))00999#**>,a+PQR #"r   modelnode_opc                    t        | t        t        j                  j                  f      sJ dt        |               t        | t              r| j                  n| }|g}|rv|j                  d      }|j                  j                  D ]  }|j                  dv r ||        t        |      D cg c]  \  }}}|
 }}}|j                  |       |ruyyc c}}w )z9Traverse the graph module and apply node_op to each node.z-Expected GraphModule or ExportedProgram, got r   )outputplaceholderN)rM   r	   rN   rO   GraphModuletyperG   popr;   r[   rP   rb   r-   )	rc   rd   r8   queuecurrent_graph_modulerH   _rU   ra   s	            r   r   r      s    
 !5!56 E	6tE{mDE   *%A		uBDE
$yy|(..44 	Dww33DM		 $@@T#U#
9a #
 #
 	,- #
s   2Cr   )TN)?r<   operatorcollectionsr   collections.abcr   typingr   r   r   r   rN   torch.exportr	   torch.fxr
   *torch.fx.passes.utils.source_matcher_utilsr   r   r   __all__nnConv1d
functionalconv1dConv2dconv2dAdaptiveAvgPool2dadaptive_avg_pool2dReLUrelurelu_BatchNorm2d
batch_normHardtanhhardtanh	hardtanh_r   iaddr   imulr   r   r3   __annotations__r   r%   r   r   r+   r0   r7   rO   rh   boolr   inttuplerR   ModulerV   rb   r   r"   r   r   <module>r      s     # $ 1 1  (   XX__ehh))001
XX__ehh))001
XX!4!4!H!HI
XX]]EHH'',,ehh.A.A.G.GH
XX588..99:
XX++44ehh6I6I6S6ST
YYhmmUF;
YYhmmUF;	  49 	 78 d3i 
=x'@ $s)  #'26	#Y $./	:	/((&&	/.3hhmm	/HK	/
3./	/#((&&#	%UXX__ehhmm3
45#0.%(("6"667.BJ.	.r   