
    qi#                        U d dl 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
mZ d dlmZmZ d dlmZ dd	lmZ dd
lmZ g Zee   ed<   eeee   edz  ef   Zeeee   edz  eef   Ze j8                  fdZ	 	 	 ddedeeej>                  j@                  f   deeef   deeef   dee   dz  dee!   dz  dee   dz  deeef   fdZ"y)    N)CallableIterable)Any)
QConfigAny)MatchAllNodePattern)GraphNode)type_before_parametrizations   )_is_observed_standalone_module)QuantizeHandler__all__c                     t        |t              r(|^}}|t        u r t        |      dk7  rt	        d      g }n|}g }t        |t
              rt        |t              ry||k(  ryt        |t              rt        |j                        |kD  ryt        |t
              rPt        |t        j                  j                        r,|j                  dk7  ryt         |j                           |k7  ryt!        |      rF|j                  dk7  s|j                  |ury|j                  t        u rU|j"                  d   |d   k7  r@yt        |t$              r|j                  dk7  s|j                  |k7  ry|j                  |k7  ry|syt        |      t        |j"                        k7  ryt'         fd	t)        |j"                  |      D              S )
z&Matches a node in fx against a pattern   z.Expecting getattr pattern to have two elementsTFcall_modulecall_functionr   call_methodc              3   B   K   | ]  \  }}t        ||d         yw)r   )max_usesN)	_is_match).0node	arg_matchmoduless      j/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/torch/ao/quantization/fx/match_utils.py	<genexpr>z_is_match.<locals>.<genexpr>K   s+      D) 	'4Q77s   )
isinstancetuplegetattrlenAssertionErrortype
issubclassr   r
   userstorchnnModuleopr   targetcallableargsstrallzip)r   r   patternr   
self_matcharg_matchess   `     r   r   r      s   '5!#* 
[ 7|q $%UVVK
*d#
:|(LwdD!S_x%?*d#
:uxx(O77m#'(<=K	*	77o%J)F[[G#yy|wqz)	J	$77m#t{{j'@	
	"
;3tyy>) "499k:      graphr   patternsroot_node_getter_mappingstandalone_module_namesstandalone_module_classescustom_module_classesreturnc           
         |g }g g i }t               }fdfdt        | j                        D ]  }	|	j                  |vs|	j                  |vs!|j	                         D ][  \  }
}|j                  |
      }t        ||	|
      s%|	j                  |vs4g } |
|	|	||        ||||      }|	} |||||
|          |t        d      | j                  D ]L  }	|	j                  dk(  st        ||	j                           |v s.|	|	dt        |	|d      f||	j                  <   N dt        d	t        t        t        j                  j                   f   ffd
}| j                  D ]]  }	|	j                  dk(  s ||	j                  |      st#        ||	j                           s?|	|	dt        |	|d      f||	j                  <   _ |S )a  
    Matches the nodes in the input graph to quantization patterns, and
    outputs the information needed to quantize them in future steps.

    Inputs:
      - graph: an fx.Graph object
      - modules: a mapping of fully qualified module name to instance,
          for example, {'foo': ModuleFoo, ...}
      - patterns: a mapping from a tuple of nodes in reverse order to
          uninitialized QuantizeHandler subclass.

    Outputs a map of
      node_name ->
        (node, matched_values, matched_pattern, QuantizeHandler instance,
         qconfig)

    For example, {
      'relu_1': (relu_1, [relu_1], torch.nn.functional.relu,
                 <CopyNodeQuantizeHandler instance>, QConfig(...)),
      ...
    }
    Nc           	          t        |t              r| |||f||j                  <   y t        |t              sy |D ]  } | |||||        y )N)r   r
   namer   )	last_node	match_mapnode_patternmatched_node_patternr0   match_valuen#_recursive_record_node_in_match_maps          r   rD   z:_find_matches.<locals>._recursive_record_node_in_match_map|   sa     lD)$	,Il''( L(3! 3y!-A7Kr3   c                    t        | t              r| ^}}t        |      dk(  }g } |||||       | d   t        ur*t	        ||j
                        D ]  \  }	}
 |	|
|||        t        |      dkD  r8|r|j                  t        |             y |j                  t        |             y |j                  |d          y |j                  |       y )Nr   r   )	r   r   r!   r    r/   r,   appendextendlist)r0   r   r>   rA   r?   sr,   is_single_argcurrent_node_pattern
subpatternargrecord_matchs              r   rN   z#_find_matches.<locals>.record_match   s    gu%HAINM/1 D)-A9Mqz('*4'; YOJ S$8LiXY'(1, !(//6J0KL(//5I0JK$++,@,CD ''-r3   modules must not be Noner   T)is_custom_modulenode_targetr   c                 J    |t        d      | v xs t        ||          v S )NrO   )r"   r#   )rQ   r   r8   r7   s     r   is_standalone_modulez+_find_matches.<locals>.is_standalone_module   s<    ? !;<<22 )GK()()	
r3   )rS   )setreversednodesr=   itemsgetr   r"   r)   r#   r*   r   r-   dictr&   r'   r(   r   )r4   r   r5   r6   r7   r8   r9   r?   all_matchedr   r0   quantize_handler_clsroot_node_getterrA   quantize_handlerr>   rS   rD   rN   s       ``           @@r   _find_matchesr^   Q   s   > $ " ($&!&"$)+IEK&.4 % 99I%$));*F191A --#;#?#?#H WdG4)9S79( $6JIV';,g7G($ !%I7!!,,(	 )2 788 
GG}$WT[[)*.CC gE	$Idii 

# 
S%((//=Q8R 
  77m# g6-gdkk.BC gDI	$Idii  r3   )NNN)#syscollections.abcr   r   typingr   r&   torch.ao.quantization.qconfigr   torch.ao.quantization.utilsr   r   torch.fx.graphr	   r
   torch.nn.utils.parametrizer   graph_moduler   r]   r   r   rH   r-   __annotations__r   _MatchResult_MatchResultWithQConfigmaxsizer   rY   r'   r(   r#   r^    r3   r   <module>rl      s0   
 .   4 = & C 8 - c  T4:w~FG$t*gnozA  03{{ /n 1537.2VV#uxx&'V 7O+,V #7H#45	V
 "#Y-V  $DzD0V  9t+V 
#|
Vr3   