U
    yh#                     @   sD  U d dl Z d dlZd dlmZmZ d dlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ d d
lmZ d dlmZmZmZmZmZmZmZmZ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eeeej"j#f eeef eeef eee  eee  eee  eeef dddZ$dS )    N)GraphNode)Pattern   )QuantizeHandler   )
QConfigAny)MatchAllNode)_is_observed_standalone_module)type_before_parametrizations)	AnyDictListCallableOptionalTupleTypeSetIterable__all__c                    sv  t |tr4|^}}|tkr<t|dks.tdg }n|}g }t |trTt|trTdS ||kr`dS t |trxt|j	|kr|dS t |trt|t
jjr|jdkrdS t |j |ksdS n|t|r|jdks|j|k	rdS |jtkr|jd |d krdS n:t |tr&|jdks |j|kr6dS n|j|kr6dS |s@dS t|t|jkrXdS t fd	d
t|j|D S )z, Matches a node in fx against a pattern
    r   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 V/var/www/html/venv/lib/python3.8/site-packages/torch/ao/quantization/fx/match_utils.py	<genexpr>R   s     z_is_match.<locals>.<genexpr>)
isinstancetuplegetattrlenAssertionError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   &   sD    


r   )graphr   patternsroot_node_getter_mappingstandalone_module_namesstandalone_module_classescustom_module_classesreturnc              	      s  |dkrg }dkrg dkr$g i }t  } fdd fddt| jD ]}	|	j|krP|	j|krP| D ]d\}
}||
d}t||	|
rp|	j|krpg }|
|	|	|| ||||}|	} |||||
|  qPqpqP|dk	st| jD ]<}	|	jdkrt	||	j
 |kr|	|	dt|	|ddf||	j< qttttjjf d	fd
d}| jD ]L}	|	jdkrP||	j
|s~t||	j
 rP|	|	dt|	|ddf||	j< qP|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                    sL   t |tr| |||f||j< n*t |ts,d S |D ]} | ||||| q0d S N)r    r   namer   )	last_node	match_mapZnode_patternmatched_node_patternr1   Zmatch_valuen)#_recursive_record_node_in_match_mapr   r   r?   ~   s    
   
z:_find_matches.<locals>._recursive_record_node_in_match_mapc                    s   t | tr| ^}}t|dk}g } ||||| | d tk	rdt||jD ]\}	}
 |	|
||| qJt|dkr|r|t| q|t| q||d  n
|| d S )Nr   r   )	r    r!   r#   r"   r0   r-   appendextendlist)r1   r   r;   r=   r<   sr-   Zis_single_argZcurrent_node_pattern
subpatternarg)record_matchr   r   rF      s4    
z#_find_matches.<locals>.record_matchr   T)Zis_custom_moduleZnode_targetr   c                    s$   |d k	st | kp"t||   kS r9   )r$   r%   rG   )r6   r5   r   r   is_standalone_module   s    z+_find_matches.<locals>.is_standalone_module)rH   )setreversedZnodesr:   itemsgetr   r$   r*   r%   r+   r   r.   r   r'   r(   r)   r
   )r2   r   r3   r4   r5   r6   r7   r<   Zall_matchedr   r1   Zquantize_handler_clsZroot_node_getterr=   quantize_handlerr;   rH   r   )r?   rF   r6   r5   r   _find_matchesT   sz    )	

   "

  rN   )NNN)%sysr'   Ztorch.fx.graphr   r   Ztorch.ao.quantization.utilsr   rM   r   Zqconfigr   utilsr	   Zgraph_moduler
   Ztorch.nn.utils.parametrizer   typingr   r   r   r   r   r   r   r   r   r   r.   __annotations__Z_MatchResultZ_MatchResultWithQConfigmaxsizer   r(   r)   rN   r   r   r   r   <module>   s:    ,3   




 
