
    %Vji                     f   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        j        hej        j        ej        j        j        hej        j        ej        j        j        hej        j        ej        j        j        ej        j        j        hej        j         ej        j        j!        hej        j"        ej        j        j#        ej        j        j$        hej%        ej%        ej&        d	d
hej'        ej'        ej(        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j4        j5        de*e         de	eege6f                  fdZ7dej4        j5        dej4        j        de8de9e:ej        j;        ej4        j        f         fdZ<dej4        j5        de*e9e:ej        j;        ej4        j        f                  fdZ=d e
eej4        j5        f         d!eddfd"Z>dS )%    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                  L    i } t           D ]}|D ]}t          |          | |<   | S N)r   list)_DICTvaluesvs      `/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/ao/quantization/pt2e/graph_utils.py_create_equivalent_types_dictr   %   s@    E# $ $ 	$ 	$AF||E!HH	$L    returnc                      t           S r   )r    r   r   r   r   0   s    r   c                 H    | t          d          | at                      adS )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   s3    
 #*IJJJ 4:<<r   
partitionsc                 B    d }| D ]}|t          ||          s dS |}dS )NFT)r   )r'   prev_partition	partitions      r   _partitions_sequentialr+   A   sF    N # #	%.GI/
 /
% 55"4r   c                 ^    | g}| t           v r |                    t           |                     |S r   )r%   extend)partition_typematching_typess     r   _get_matching_typesr0   L   s6    $%N///4^DEEEr   partition_typesc                     t                      }| D ]>}t          |          }t          |          }t          ||z            dk    r dS ||z  }?dS )Nr   FT)setr0   len)r1   partition_types_setr.   r/   matching_types_sets        r   _valid_type_sequencer7   S   sk    %%) 2 2,^<< 00"%77881<<55114r   Tgm	filter_fnc                    t          |          st          d| d          t                      }|D ]h}t          |          }t	          | j        ||          }t          t          j        	                    |
                                                    ||<   it          |
                                          }t          j        | }	d |	D             }
|
S )NzInvalid partition types: z*. Each type in the sequence must be uniquec                 0    g | ]}t          |          |S r"   )r+   ).0	candidates     r   
<listcomp>z.find_sequential_partitions.<locals>.<listcomp>s   s6       !),,  r   )r7   r$   r   r0   r   graphr   	itertoolschainfrom_iterabler   product)r8   r1   include_functional_equivalentr9   typed_partitionsr.   types_to_matchr'   typed_partitions_listfusion_candidatesfused_partitionss              r   r   r   ^   s      00 
cccc
 
 	
 AL) 
 
,^<<*28^YOO
+/O))**;*;*=*=>>,
 ,
(( !!1!8!8!:!:;;!)+@A *  
 r   graph_modulenode	arg_indexc                     |j         |         }t          |t          j        j                  sJ |j        dk    sJ t          |j        t                    sJ |                     |j                  }|j        ||fS )Nget_attr)	args
isinstancetorchfxr
   optargetstrget_submodule)rJ   rK   rL   submod_node	submodules        r   _get_submodulerY   {   s|     )I&Kk58=11111>Z''''k(#.....**;+=>>Iy$..r   c                    g }| j         j        D ]}|j        dk    r|j        t          j        j        j        u rH|                    t          | |d                     |                    t          | |d                     |j        t          j        j        j
        u r$|                    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?   nodesrS   rT   rQ   opshigher_ordercondappendrY   map_impl)rJ   control_flow_submodulesrK   s      r   _get_control_flow_submodulesre      s     !"( R R7o%%;%)0555#**>,a+P+PQQQ#**>,a+P+PQQQ;%)0999#**>,a+P+PQQQ""r   modelnode_opc                    t          | t          t          j        j        f          sJ dt          |                        t          | t                    r| j        n| }|g}|rk|                    d          }|j        j	        D ]}|j
        dv r ||           d t          |          D             }|                    |           |idS dS )z9Traverse the graph module and apply node_op to each node.z-Expected GraphModule or ExportedProgram, got r   )outputplaceholderc                     g | ]\  }}}|	S r"   r"   )r<   _rX   s      r   r>   z/bfs_trace_with_node_process.<locals>.<listcomp>   s.     #
 #
 #
9a #
 #
 #
r   N)rP   r	   rQ   rR   GraphModuletyperJ   popr?   r^   rS   re   r-   )rf   rg   r8   queuecurrent_graph_modulerK   rd   s          r   r   r      s   
 eoux/CDEE  EUEE E  *%AA	L		uBDE
 .$yy||(.4 	 	Dw333GDMMMM#
 #
#?@T#U#U#
 #
 #
 	,---  . . . . .r   r   )TN)?r@   operatorcollectionsr   collections.abcr   typingr   r   r   r   rQ   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   rR   rm   boolr   inttuplerU   ModulerY   re   r   r"   r   r   <module>r      sx         # # # # # # $ $ $ $ $ $ 1 1 1 1 1 1 1 1 1 1 1 1  ( ( ( ( ( (                  X_eh)01
X_eh)01
X!4!HI
X]EH',eh.A.GH
X58.9:
X+4eh6I6ST
YhmUF;
YhmUF;	  49 	 	 	   7688 d3i    
= 
= 
= 
=x'@      $s)     #'26	 #Y $./	   :	/(&	/.3hm	/HK	/
3./	/ 	/ 	/ 	/#(&#	%UX_ehm3
45# # # #0.%("667.BJ.	. . . . . .r   