o
    Zh"                     @   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_moduleZcall_functionr   Zcall_methodc                 3   s$    | ]\}}t  ||d dV  qdS )r   )max_usesN)	_is_match).0nodeZ	arg_matchmodules S/var/www/auris/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
   ZuserstorchnnModuleopr   targetcallableargsstrallzip)r   r   patternr   Z
self_matchZ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_mapZnode_patternmatched_node_patternr+   Zmatch_valuen)#_recursive_record_node_in_match_mapr   r   r9   {   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   r5   r7   r6   sr'   Zis_single_argZcurrent_node_pattern
subpatternarg)record_matchr   r   r@      s   
z#_find_matches.<locals>.record_matchr   T)Zis_custom_modulenode_targetr   c                    s$   |d usJ | v pt ||   v S r3   )r   )rA   r   )r0   r/   r   r   is_standalone_module   s   
z+_find_matches.<locals>.is_standalone_module)rB   )setreversednodesr4   itemsgetr   r$   r   r%   r   r(   dictr!   r"   r#   r   )r,   r   r-   r.   r/   r0   r1   r6   Zall_matchedr   r+   Zquantize_handler_clsZroot_node_getterr7   quantize_handlerr5   rB   r   )r9   r@   r0   r/   r   _find_matchesP   sn   



$
	


rJ   )NNN)$syscollections.abcr   typingr   r   r   r!   Ztorch.ao.quantization.qconfigr   Ztorch.ao.quantization.utilsr   r   Ztorch.fx.graphr	   r
   Ztorch.nn.utils.parametrizer   Zgraph_moduler   rI   r   r   r<   r(   __annotations__r   Z_MatchResultZ_MatchResultWithQConfigmaxsizer   rH   r"   r#   r   rJ   r   r   r   r   <module>   sH   
	6





