
    8h                        % S SK r S SKrS SKJr  S SKJr  S SKJrJrJ	r	J
r
  S SKrS SKJr  S SKJr  S SKJrJrJr  / SQr\R*                  R,                  \R*                  R.                  R0                  1\R*                  R2                  \R*                  R.                  R4                  1\R*                  R6                  \R*                  R.                  R8                  1\R*                  R:                  \R*                  R.                  R<                  \R*                  R.                  R>                  1\R*                  R@                  \R*                  R.                  RB                  1\R*                  RD                  \R*                  R.                  RF                  \R*                  R.                  RH                  1\RJ                  \RJ                  \RL                  S	S
1\RN                  \RN                  \RP                  SS1/q)\*\+   \,S'   S r-\-" 5       q.S\*\+   4S jr/S"S jr0S\\   4S jr1S r2S\*\   4S jr3  S#S\Rh                  Rj                  S\*\   S\	\\/\64      4S jjr7S\Rh                  Rj                  S\Rh                  R                  S\8S\9\:\R*                  Rv                  \Rh                  R                  4   4S jr<S\Rh                  Rj                  S\*\9\:\R*                  Rv                  \Rh                  R                  4      4S jr=S\
\\Rh                  Rj                  4   S \SS4S! jr>g)$    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                  P    0 n [          H  nU H  n[        U5      X'   M     M     U $ N)r   list)_DICTvaluesvs      `/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/ao/quantization/pt2e/graph_utils.py_create_equivalent_types_dictr   %   s-    E#AF|EH  $ L    returnc                      [         $ r   )r    r   r   r   r   0   s    r   c                 8    U c  [        S5      eU q[        5       qg)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                 H    S nU  H  nUb  [        X5      (       d    gUnM     g)NFT)r   )r'   prev_partition	partitions      r   _partitions_sequentialr+   A   s6    N	%.G/
 /
 "   r   c                 P    U /nU [         ;   a  UR                  [         U    5        U$ r   )r%   extend)partition_typematching_typess     r   _get_matching_typesr0   L   s-    $%N//4^DEr   partition_typesc                     [        5       nU  H0  n[        U5      n[        U5      n[        X-  5      S:  a    gX-  nM2     g)Nr   FT)setr0   len)r1   partition_types_setr.   r/   matching_types_sets        r   _valid_type_sequencer7   S   sK    %),^< 0"781<1 * r   gm	filter_fnc                    [        U5      (       d  [        SU S35      e[        5       nU H\  n[        U5      n[	        U R
                  Xc5      n[        [        R                  R                  UR                  5       5      5      XE'   M^     [        UR                  5       5      n[        R                  " U6 n	U	 V
s/ s H  n
[        U
5      (       d  M  U
PM     nn
U$ s  sn
f )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0''88bc
 	
 AL),^<*288^O
+/OO))**;*;*=>,
( * !!1!8!8!:;!))+@A +*I!), 	*  
 s   <CCgraph_modulenode	arg_indexc                 .   UR                   U   n[        U[        R                  R                  5      (       d   eUR
                  S:X  d   e[        UR                  [        5      (       d   eU R                  UR                  5      nUR                  XA4$ )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1>>Z'''k((#....**;+=+=>Iy..r   c                    / nU R                   R                   H  nUR                  S:w  a  M  UR                  [        R
                  R                  R                  L a6  UR                  [        XS5      5        UR                  [        XS5      5        UR                  [        R
                  R                  R                  L d  M  UR                  [        XS5      5        M     U$ )ac  
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     !""((77o%;;%))00555#**>,a+PQ#**>,a+PQ;;%))00999#**>,a+PQ ) #"r   modelnode_opc                    [        U [        [        R                  R                  45      (       d   S[        U 5       35       e[        U [        5      (       a  U R                  OU nU/nU(       a  UR                  S5      nUR                  R                   H  nUR                  S;   a  M  U" U5        M     [        U5       VVs/ s H  u  pgnUPM
     nnnUR                  U5        U(       a  M  ggs  snnf )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    
 eouxx/C/CDEE 
7U}EE  *%AA		uBDE
$yy|(..44Dww33DM	 5 $@@T#U#
#Ua #U 	  #
 	,- %#
s   C4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   