
    qi|                     h   U d dl Z d dlZd dlmZ d dlmZmZ d dl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*                  j0                  he	j&                  j2                  e	j&                  j*                  j4                  he	j&                  j6                  e	j&                  j*                  j8                  e	j&                  j*                  j:                  he	j&                  j<                  e	j&                  j*                  j>                  he	j&                  j@                  e	j&                  j*                  jB                  e	j&                  j*                  jD                  he	jF                  ejF                  ejH                  d	d
he	jJ                  ejJ                  ejL                  ddhga'e(e)   e*d<   d Z+ e+       a,de(e)   fdZ-d"dZ.dee   fdZ/d Z0de(e   fdZ1	 	 d#de	jd                  jf                  de(e   deege4f   dz  fdZ5de	jd                  jf                  de	jd                  j                  de6de7e8e	j&                  jr                  e	jd                  j                  f   fdZ:de	jd                  jf                  de(e7e8e	j&                  jr                  e	jd                  j                  f      fdZ;dee	jd                  jf                  z  d eddfd!Z<y)$    N)OrderedDict)CallableSequence)Any)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      l/home/ubuntu/crypto_trading_bot/.venv/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_typesr.   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)setr.   len)r/   partition_types_setr,   r-   matching_types_sets        r   _valid_type_sequencer5   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)r5   r"   r   r.   r
   graphr   	itertoolschainfrom_iterabler   productr)   )r6   r/   include_functional_equivalentr7   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t        dt        |             |j                  dk7  rt        d|j                         t        |j                  t              s!t        dt        |j                               | j                  |j                        }|j                  ||fS )Nz0Expected submod_node to be a torch.fx.Node, got get_attrz.Expected submod_node.op to be 'get_attr', got z?Expected submod_node.target to be a string attribute name, got )args
isinstancetorchfxr   AssertionErrortypeoptargetstrget_submodule)rE   rF   rG   submod_node	submodules        r   _get_submodulerV   {   s     ))I&Kk588==1>tK?P>QR
 	
 ~~#<[^^<LM
 	
 k((#.MdS^SeSeNfMgh
 	
 **;+=+=>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   )r9   nodesrP   rQ   rL   opshigher_ordercondappendrV   map_impl)rE   control_flow_submodulesrF   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t        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)rK   r   rL   rM   GraphModulerN   rO   rE   popr9   r[   rP   rb   r+   )	rc   rd   r6   queuecurrent_graph_modulerF   _rU   ra   s	            r   r   r      s    
 eouxx/C/CDE;DK=I
 	
  *%A		uBDE
$yy|(..44 	Dww33DM		 $@@T#U#
9a #
 #
 	,- #
s   6Cr   )TN)=r:   operatorcollectionsr   collections.abcr   r   typingr   rL   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   r1   __annotations__r   r#   r   r   r)   r.   r5   rM   rh   boolr   inttuplerR   ModulerV   rb   r   r    r   r   <module>r      s     # .   (   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)  #'/3	#Y %,	:/((&&/.3hhmm/HK/
3.//*#((&&#	%UXX__ehhmm3
45#0.UXX111.<D.	.r   