
    sg"                        U d dl Z d dlmZmZmZmZmZmZmZm	Z	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e   ef   Ze	eee   ee   eef   Ze j>                  fdZ 	 	 	 ddedeeejB                  jD                  f   deeef   deeef   deee      deee
      deee      deeef   fdZ#y)    N)	AnyCallableDictIterableListOptionalSetTupleType)
QConfigAny)MatchAllNodePattern)GraphNode)type_before_parametrizations   )_is_observed_standalone_module)QuantizeHandler__all__c                     t        |t              r$|^}}|t        u rt        |      dk(  sJ 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                           |k(  sy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      W/var/www/html/venv/lib/python3.12/site-packages/torch/ao/quantization/fx/match_utils.py	<genexpr>z_is_match.<locals>.<genexpr>I   s+      D) 	'4Q77s   )
isinstancetuplegetattrlen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!#* 
[ w<1$V&VV$K
*d#
:|(LwdD!S_x%?*d#
:uxx(O77m#+GDKK,@AZO	*	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                  |
d      }t        ||	|
      s&|	j                  |vs5g } |
|	|	||        ||||      }|	} |||||
|          |J | 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_patternr5   match_valuen#_recursive_record_node_in_match_maps          r"   rJ   z:_find_matches.<locals>._recursive_record_node_in_match_mapz   sa     lD)$	,Il''( L(3! 3y!-A7Kr8   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&   r4   r1   appendextendlist)r5   r   rD   rG   rE   sr1   is_single_argcurrent_node_pattern
subpatternargrecord_matchs              r"   rT   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 ''-r8   r   T)is_custom_modulenode_targetr!   c                 8    |J | v xs t        ||          v S rB   )r(   )rV   r!   r=   r<   s     r"   is_standalone_modulez+_find_matches.<locals>.is_standalone_module   s7    """22 )GK()()	
r8   )rX   )setreversednodesrC   itemsgetr   r.   r(   r/   r   r2   r   r+   r,   r-   r   )r9   r!   r:   r;   r<   r=   r>   rE   all_matchedr   r5   quantize_handler_clsroot_node_getterrG   quantize_handlerrD   rX   rJ   rT   s       ``           @@r"   _find_matchesrb   O   s   > $ " ($&!&"$)+IEK&.4 % 99I%$));*F191A --#;#?#?#N WdG4)9S79( $6JIV';,g7G($ !%I7!!,,(	 )2  
GG}$WT[[)*.CC gE	$Idii 

# 
S%((//=Q8R 
  77m# g6-gdkk.BC gDI	$Idii  r8   )NNN)$systypingr   r   r   r   r   r   r	   r
   r   r+   torch.ao.quantization.qconfigr   torch.ao.quantization.utilsr   r   torch.fx.graphr   r   torch.nn.utils.parametrizer   graph_moduler   ra   r   r   r2   __annotations___MatchResult_MatchResultWithQConfigmaxsizer   r,   r-   rb    r8   r"   <module>ro      s5   
 R R R  4 = & C 8 - c  T4:x'8/IJ$t*hw'*D  03{{ .l 486:15TT#uxx&'T 7O+,T #7H#45	T
 &d3i0T  (T
3T $DI.T 
#|
Tr8   