o
    Zh%                     @   s   d dl mZ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 d dlmZ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 ddgZG dd deZG dd deZde	deeef fddZdS )    )ABCabstractmethod)AnyCallableUnionN)BackendConfig)get_fuser_method_new)_parent_nameNodePatternPattern)GraphNode)type_before_parametrizations   )FuseCustomConfig)MatchAllNodeDefaultFuseHandlerFuseHandlerc                   @   s|   e Zd ZdZedefddZededee	e
jjf deded	ee d
ededeeee
jjef f dedefddZdS )r   z*Base handler class for the fusion patternsnodec                 C      d S N selfr   r   r   T/var/www/auris/lib/python3.10/site-packages/torch/ao/quantization/fx/fuse_handler.py__init__   s   zFuseHandler.__init__load_argnamed_modulesfused_graph	root_nodeextra_inputsmatched_node_patternfuse_custom_configfuser_method_mappingis_qatreturnc
           
      C   r   r   r   )
r   r   r   r   r   r    r!   r"   r#   r$   r   r   r   fuse#   s   zFuseHandler.fuseN)__name__
__module____qualname____doc__r   r   r   r   dictstrtorchnnModuler   listr   r
   r   r   r   
Sequentialboolr&   r   r   r   r   r      s4    	
c                       sx   e Zd Zdef fddZdedeeej	j
f dededee d	ed
edeeeej	jef f dedefddZ  ZS )r   r   c                    s   t  | d S r   )superr   r   	__class__r   r   r   4   s   zDefaultFuseHandler.__init__r   r   r   r   r    r!   r"   r#   r$   r%   c
                    s   |j dks	J dt|j fdd|}
 fdd  |
}t|j\}}t||}||	g|
R  }t| || fdd|D }||}t|j}|	| t
||_|S )	Ncall_modulez.Expecting module node to be a call_module Nodec                    s   t | ttfr$| ^}}g }| | | fdd|D  t|S | }|jdkr0|j S |jdkrH|jtjj	j
krHtj }j|_|S |jdksR|jdkrU|jS tS )zGiven a node pattern, extract the corresponding modules
            e.g. input: (relu_node, (bn_node, conv_node))
                 output: (relu_module, (bn_module, conv_module))
            c                 3   s    | ]} |V  qd S r   r   ).0a)get_modulesr   r   	<genexpr>Q   s    z?DefaultFuseHandler.fuse.<locals>.get_modules.<locals>.<genexpr>r6   Zcall_functionZcall_method)
isinstancetupler0   appendextendoptargetr-   r.   Z
functionalreluZReLUZtrainingr   )patternnargsmodulesrA   )r9   r   root_moduler   r   r9   H   s    


z,DefaultFuseHandler.fuse.<locals>.get_modulesc                    s2   t | trtt | S t | tjjrt| S | S r   )r;   r<   mapr-   r.   r/   r   )m)get_matched_typesr   r   rI   c   s
   
z2DefaultFuseHandler.fuse.<locals>.get_matched_typesc                    s   g | ]} |qS r   r   )r7   input)r   r   r   
<listcomp>q   s    z+DefaultFuseHandler.fuse.<locals>.<listcomp>)r?   r,   r@   r	   r   setattrZ	node_copyr0   rD   r>   r<   )r   r   r   r   r   r    r!   r"   r#   r$   Zmatched_modulesZmatched_module_typesZmodule_parent_namemodule_namefuser_methodZfused_module
extra_argsr   rD   r   )rI   r9   r   r   rF   r   r&   7   s$   



zDefaultFuseHandler.fuse)r'   r(   r)   r   r   r   r+   r,   r-   r.   r/   r   r0   r   r
   r   r   r   r1   r2   r&   __classcell__r   r   r4   r   r   3   s.    	
backend_configr%   c                 C   s.   i }| j  D ]\}}|jd urt||< q|S r   )Z!_pattern_complex_format_to_configitemsrN   r   )rQ   Zfusion_pattern_to_fuse_handlersrB   configr   r   r   '_get_fusion_pattern_to_fuse_handler_clsy   s   
rT   )abcr   r   typingr   r   r   r-   Z$torch.ao.quantization.backend_configr   Z+torch.ao.quantization.fuser_method_mappingsr   Ztorch.ao.quantization.utilsr	   r
   r   Ztorch.fx.graphr   r   Ztorch.nn.utils.parametrizer   Zcustom_configr   Zmatch_utilsr   __all__r   r   r+   rT   r   r   r   r   <module>   s(   F
