
    qi!              
       x   d dl mZ d dlmZmZ d dlZd dlmZ d dlmc m	Z
 d dlmZmZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d
dlmZ ej4                  j6                  Zdeeee   f   dee eef      fdZ!eeee eef   f   Z"ee e"e"f   e e"e"e"e"f   f   Z#de$e e#e%f      fdZ&dede#dedee   de'f
dZ(y)    )Callable)AnyUnionN)FakeQuantizeBaseObserverBase)get_native_backend_config)!_get_pattern_to_quantize_handlers)getattr_from_fqn)GraphModule)Node   )NSNodeTargetType base_name_to_sets_of_related_opsreturnc                 $   t               }| j                         D ]r  }t        |      }t        t	        |            D ]N  }t        |t	        |            D ]4  }|j                  ||   ||   f       |j                  ||   ||   f       6 P t |S )N)setvalueslistrangelenadd)r   type_a_related_to_bss_listidx_0idx_1s         b/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/torch/ao/ns/fx/pattern_utils.pyget_type_a_related_to_br      s     KN%-446 Ha3v;' 	HEuc&k2 H#''u(FG#''u(FGH	HH     c            	         g } t        t                     }d}|D ]  }t        |t              rFt	        |      dk(  r8t        |d   t              r%t	        |d         dk(  r|d   |d   d   |d   d   f}t        |t              r| j                  ||f       t        t        fD ]0  }t        |t              r|g|}n||f}| j                  ||f       2  d}dt        j                  ft        j                  t        j                  df|ft        j                  t        j                  f|ft        j                   t        j"                  f|ft        j$                  t        j&                  f|ft        j(                  t        j                  t        j                  f|ft        j(                  t        j                   t        j"                  f|ft        j(                  t        j$                  t        j&                  f|fg}|D ]U  }| j                  |       | j                  t        g|d   |d   f       | j                  t        g|d   |d   f       W | S )a  
    Set of potential fusions, in reverse order.  The order is reversed
    to match how fusion patterns are defined in quantization code.

    Fusion format:
    ((fusion_op_0, fusion_op_1), base_op_idx)

    Where base_op_idx is the idx of the op we should use to match other related
    ops. Note: base_op_idx is specified in non-reverse order, i.e. a base_op_idx
    of 0 represents the first op in regular (non-reverse) order, 1 represents the
    second op, etc.
    r      r   to
dequantize)r	   r   
isinstancetupler   appendr   r   torchfloat16FrelulinearnnBatchNorm1dConv1dBatchNorm2dConv2dBatchNorm3dConv3dReLU)	resultsall_quant_patternsdefault_base_op_idxquant_patternclsnew_patternfp16_em_base_op_idxpatterns_to_addps	            r   get_reversed_fusionsr=   4   s0    /1G ;;T;VW+ ?
 }e,M"a'=+U3M!$%* +1-}Q/?/BMRSDTUVDWXM
 mU+NNM+>?@ !"23 	?C-/"3]3"M2NNK)<=>	?/?F 
 EMM"AFFAHHlC	
 .."))	$&9:
.."))	$&9:
.."))	$&9:
''2>>299	-/BC
''2>>299	-/BC
''2>>299	-/BCO   :q-!-qt45)1AaD11Q489:
 Nr   end_nodereversed_fusiongm
seen_nodesc                 B   | }t        t        |            D ]  }||v r y||   }|j                  dk(  rt        |t               xr t        |t
               }|rX|j                  |k7  r yt        |j                        dkD  r-t        |j                  d   t              r|j                  d   } y y|j                  dk(  rt        |t
              }|rt        |j                  t              s!t        dt        |j                               t        ||j                        }	t        |t
              s yt        |	|      s yt        |j                        dkD  r.t        |j                  d   t              r|j                  d   }v y y|j                  dk(  rt        |t              xr t        |      dk(  }
t        |t              }|s|
r|r|j                  |k7  rn yt        |t              st        dt        |             |j                  |d   k7  r yt        |j                        dk  r y|j                  d	   |d	   k7  r yt        |j                        dkD  r.t        |j                  d   t              r|j                  d   } y y y y
)zZ
    Returns true if a pattern ending with `end_node` matches
    the fusion pattern.
    Fcall_functionr   call_modulezExpected str, got call_methodr!   zExpected tuple, got r   T)r   r   opr$   strtypetargetargsr   AssertionErrorr
   r%   )r>   r?   r@   rA   cur_node
fusion_idxcur_fusion_elfusion_el_is_funfusion_el_is_mod
target_mod!fusion_el_is_meth_with_second_argfusion_el_is_meth_without_argss               r    end_node_matches_reversed_fusionrT      sX    HC01 B
z!'
3;;/)$.}c$B B  }d33   ??m3 x}}%)jq9I4.P'}}Q/H [[M))->!(//37(+=d8??>S=T)UVV-b(//B
!-6 !*m< x}}%)jq9I4.P'}}Q/H [[M)=%0LS5G15L . .8s-K*-1R1-7$%mU;,243F2GH   -*::$X]]+a/$!q)]1-==$x}}%)jq9I4.P'}}Q/H EBH r   ))collections.abcr   typingr   r   r'   torch.nnr,   torch.nn.functional
functionalr)   torch.ao.quantizationr   r   $torch.ao.quantization.backend_configr   )torch.ao.quantization.fx.quantize_handlerr	   torch.ao.quantization.utilsr
   torch.fxr   torch.fx.graphr   ns_typesr   ops	quantizedtoqdictrG   r   r%   r   NSFusionElTypeNSFusionTyper   intr=   boolrT    r   r   <module>rj      s$   $      @ J W 8    & ii&*34D0E+E&F!11	23& 	S 	..
()	....
HIKPd5s):#;< PfOO!O 	O D		O
 
Or   