o
    Ih"                     @   sD  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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dZ#dS )    N)Iterable)AnyCallableOptional)
QConfigAny)MatchAllNodePattern)GraphNode)type_before_parametrizations   )_is_observed_standalone_module)QuantizeHandler__all__c                    sj  t |tr|^}}|tu rt|dksJ dg }n|}g }t |tr*t|tr*dS ||kr0dS t |tr<t|j|kr>dS t |tr]t|t	j
jr]|jdkrQdS t |j |ks\dS n:t|r~|jdksk|j|urmdS |jtu r}|jd |d kr}dS nt |tr|jdks|j|krdS n|j|krdS |sdS t|t|jkrdS t fd	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   s$    | ]\}}t  ||d dV  qdS )r   )max_usesN)	_is_match).0node	arg_matchmodules X/var/www/vscode/kcb/lib/python3.10/site-packages/torch/ao/quantization/fx/match_utils.py	<genexpr>J   s
    
z_is_match.<locals>.<genexpr>)
isinstancetuplegetattrlentype
issubclassr   r
   userstorchnnModuleopr   targetcallableargsstrallzip)r   r   patternr   
self_matcharg_matchesr   r   r   r      sP   





r   graphr   patternsroot_node_getter_mappingstandalone_module_namesstandalone_module_classescustom_module_classesreturnc              	      s  |du rg }du rg du rg i }t  } fdd fddt| jD ]C}	|	j|vrk|	j|vrk| D ]2\}
}||
d}t||	|
rj|	j|vrjg }|
|	|	|| ||||}|	} |||||
|  nq8q(|dusrJ | jD ]}	|	jdkrt||	j	 |v r|	|	dt
|	|ddf||	j< qud	td
tttjjf ffdd}| jD ]"}	|	jdkr||	j	|st||	j	 r|	|	dt
|	|ddf||	j< q|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                    sN   t |tr| |||f||j< d S t |tsd S |D ]} | ||||| qd S N)r   r
   namer   )	last_node	match_mapnode_patternmatched_node_patternr/   match_valuen)#_recursive_record_node_in_match_mapr   r   rA   {   s   

z:_find_matches.<locals>._recursive_record_node_in_match_mapc                    s   t | trU| ^}}t|dk}g } ||||| | d tur2t||jD ]\}	}
 |	|
||| q%t|dkrL|rC|t| d S |t| d S ||d  d S || d S )Nr   r   )	r   r   r!   r    r.   r+   appendextendlist)r/   r   r;   r>   r<   sr+   is_single_argcurrent_node_pattern
subpatternarg)record_matchr   r   rJ      s   
z#_find_matches.<locals>.record_matchr   T)is_custom_modulenode_targetr   c                    s$   |d usJ | v pt ||   v S r9   )r"   )rL   r   )r6   r5   r   r   is_standalone_module   s   
z+_find_matches.<locals>.is_standalone_module)rM   )setreversednodesr:   itemsgetr   r(   r"   r)   r   r,   dictr%   r&   r'   r   )r2   r   r3   r4   r5   r6   r7   r<   all_matchedr   r/   quantize_handler_clsroot_node_getterr>   quantize_handlerr;   rM   r   )rA   rJ   r6   r5   r   _find_matchesP   sn   



$
	


rX   )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   rW   r   r   rD   r,   __annotations__r   _MatchResult_MatchResultWithQConfigmaxsizer   rS   r&   r'   r"   rX   r   r   r   r   <module>   sH   
	6





