
    `i"                        U d dl Z d dlmZ d dl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j         j!        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#dS )    N)Iterable)AnyCallableOptional)
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dS ||k    rdS t          |t                    rt          |j                  |k    rdS t          |t                    rMt          |t          j
        j                  r.|j        dk    rdS t           |j                           |k    sdS nt          |          r>|j        dk    s	|j        |urdS |j        t          u r|j        d         |d         k    rdS n;t          |t"                    r|j        dk    s|j        |k    rdS n|j        |k    rdS |sdS t          |          t          |j                  k    rdS 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   D   K   | ]\  }}t          ||d           V  dS )r   )max_usesN)	_is_match).0node	arg_matchmoduless      x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/ao/quantization/fx/match_utils.py	<genexpr>z_is_match.<locals>.<genexpr>J   sL        D) 	'4Q777         )
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$$$K
*d## 
:|(L(L twtdD!! S__x%?%?u*d## 
:ux(O(O 7m##5+GDK,@AAZOO5 P	*		 
7o%%J)F)F5[G##y|wqz))u	J	$	$ 7m##t{j'@'@5 (A	
	"	"u t
;3ty>>))u    "49k::     r   graphr   patternsroot_node_getter_mappingstandalone_module_namesstandalone_module_classescustom_module_classesreturnc           
      h   |g }g g i }t                      }fdfdt          | j                  D ]}	|	j        |vr|	j        |vr||                                D ]g\  }
}|                    |
d          }t          ||	|
          r;|	j        |vr2g } |
|	|	||            ||||          }|	} |||||
|            nh|J | j        D ]G}	|	j        dk    r:t          ||	j	                           |v r|	|	dt          |	|d          f||	j        <   Hdt          dt          t          t          j        j        f         ffd	}| j        D ]V}	|	j        dk    rI ||	j	        |          st!          ||	j	                           r|	|	dt          |	|d
          f||	j        <   W|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        <   d S t          |t                    sd S |D ]} | |||||           d S N)r    r   namer   )	last_node	match_mapnode_patternmatched_node_patternr1   match_valuen#_recursive_record_node_in_match_maps          r   rE   z:_find_matches.<locals>._recursive_record_node_in_match_map{   s     lD)) 	$	,Il'((( L(33 	F!  33y!-A7K    r   c                    t          | t                    r| ^}}t          |          dk    }g } |||||           | d         t          ur*t	          ||j                  D ]\  }	}
 |	|
|||           t          |          dk    rJ|r$|                    t          |                     d S |                    t          |                     d S |                    |d                    d S |                    |           d S )Nr   r   )	r    r!   r#   r"   r0   r-   appendextendlist)r1   r   r?   rB   r@   sr-   is_single_argcurrent_node_pattern
subpatternargrecord_matchs              r   rO   z#_find_matches.<locals>.record_match   s7   gu%% 	.HAIINM/1 LD)-A9MMMqz(('*4';'; Y YOJ LS$8LiXXXX'((1,, ! L(//6J0K0KLLLLL(//5I0J0JKKKKK$++,@,CDDDDD ''-----r   r   T)is_custom_modulenode_targetr   c                 B    |J | v pt          ||                    v S r=   )r$   )rQ   r   r8   r7   s     r   is_standalone_modulez+_find_matches.<locals>.is_standalone_module   s:    """22 )GK())()	
r   )rS   )setreversednodesr>   itemsgetr   r*   r$   r+   r   r.   dictr'   r(   r)   r   )r4   r   r5   r6   r7   r8   r9   r@   all_matchedr   r1   quantize_handler_clsroot_node_getterrB   quantize_handlerr?   rS   rE   rO   s       ``           @@r   _find_matchesr^   P   s   > $ " ($&!&"$)+IEEK    &. . . . .4 %%  9I%%$);*F*F191A1A  --#;#?#?#N#N WdG44 )9S9S79( L$6JIVVV';';,g7G( ($ !%I77!!,,(	 	 	 E  
 
G}$$WT[)**.CCC gEEE	$Idi 
# 
S%(/=Q8R 
 
 
 
 
 
 
   7m##  g66 $-gdk.BCC $ gDIII	$Idi  r   )NNN)$syscollections.abcr   typingr   r   r   r'   torch.ao.quantization.qconfigr   torch.ao.quantization.utilsr   r	   torch.fx.graphr
   r   torch.nn.utils.parametrizer   graph_moduler   r]   r   r   rI   r.   __annotations__r!   _MatchResult_MatchResultWithQConfigmaxsizer   rY   r(   r)   r$   r^    r   r   <module>rl      s   




 $ $ $ $ $ $ * * * * * * * * * *  4 4 4 4 4 4 = = = = = = = = & & & & & & & & C C C C C C 8 8 8 8 8 8 - - - - - - c    T4:x'8/IJ$t*hw'*D  03{ . . . .l 486:15T TT#ux&'T 7O+,T #7H#45	T
 &d3i0T  (T
3T $DI.T 
#|
T T T T T Tr   