U
    yh                     @   s   d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	m
Z
 ddddgZG d	d deZG d
d deZe	edddZee jjef ee	dddZG dd deZe	edddZdd ZG dd deZdS )    N)GraphModule)Graph)UnionDictAnySetFusedGraphModuleObservedGraphModuleObservedStandaloneGraphModuleQuantizedGraphModulec                       sF   e Zd Zeejjeee	f f e
ee d fddZdd Z  ZS )r   rootgraphpreserved_attr_namesc                    sF   || _  fdd| j D }t  | |D ]}t| |||  q,d S )Nc                    s"   i | ]}t  |r|t |qS  hasattrgetattr.0attrr   r   W/var/www/html/venv/lib/python3.8/site-packages/torch/ao/quantization/fx/graph_module.py
<dictcomp>   s     
  z-FusedGraphModule.__init__.<locals>.<dictcomp>)r   super__init__setattrselfr   r   r   Zpreserved_attrsr   	__class__r   r   r      s
    zFusedGraphModule.__init__c                 C   s4   t j }t| j|_t|t| jt| jS N)	torchnnModulecopydeepcopy__dict__r   r   r   r   memoZfake_modr   r   r   __deepcopy__   s    
zFusedGraphModule.__deepcopy____name__
__module____qualname__r   r"   r#   r$   r   strr   r   r   r   r*   __classcell__r   r   r   r   r      s   .
c                       sF   e Zd Zeejjeee	f f e
ee d fddZdd Z  ZS )r	   r   c              
      s`   ddddddddd	d
h
 || _ fdd| jD }t  | |D ]}t| |||  qFd S )NZ_activation_post_process_mapZ _activation_post_process_indexesZ	_patternsZ_node_name_to_qconfigZ_prepare_custom_configZ"_equalization_node_name_to_qconfigZ_node_name_to_scopeZ_qconfig_mappingZ_is_qatZ_observed_node_namesc                    s"   i | ]}t  |r|t |qS r   r   r   r   r   r   r   -   s     
  z0ObservedGraphModule.__init__.<locals>.<dictcomp>)unionr   r   r   r   r   r   r   r   r   !   s"    
zObservedGraphModule.__init__c                 C   s4   t j }t| j|_t|t| jt| jS r!   )	r"   r#   r$   r%   r&   r'   r	   r   r   r(   r   r   r   r*   5   s    
z ObservedGraphModule.__deepcopy__r+   r   r   r   r   r	      s   .)modulereturnc                 C   s   t | dod| jkS Nmeta_observed_graph_module_attrs)r   r5   r2   r   r   r   _is_observed_module:   s    r8   )model	attr_namer3   c                 C   s(   t | dr$d| jkr$t| jd |S d S r4   )r   r5   r   )r9   r:   r   r   r   _get_observed_graph_module_attr=   s    r;   c                       sF   e Zd Zeejjeee	f f e
ee d fddZdd Z  ZS )r
   r   c                    s"   | ddh}t ||| d S )NZ'_standalone_module_input_quantized_idxsZ(_standalone_module_output_quantized_idxs)r1   r   r   )r   r   r   r   r   r   r   r   C   s
    z&ObservedStandaloneGraphModule.__init__c                 C   s4   t j }t| j|_t|t| jt| jS r!   )	r"   r#   r$   r%   r&   r'   r
   r   r   r(   r   r   r   r*   I   s    
z*ObservedStandaloneGraphModule.__deepcopy__r+   r   r   r   r   r
   B   s   .c                 C   s   t | o| jd jS )Nr6   )r8   r5   Zis_observed_standalone_moduler7   r   r   r   _is_observed_standalone_moduleN   s    r<   c                 C   sD   t | D ]6}d|krtt| |tjjrt| |}|||| < qd S NZ_packed_weight)dir
isinstancer   r"   _CScriptObject)r   ZdestinationprefixZ	keep_varsr:   Zpacked_weightr   r   r   _save_packed_weightQ   s    
rC   c                       sV   e Zd ZdZeejjee	e
f f eee	 d fddZ fddZdd Z  ZS )	r   a	   This class is created to make sure PackedParams
    (e.g. LinearPackedParams, Conv2dPackedParams) to appear in state_dict
    so that we can serialize and deserialize quantized graph module with
    torch.save(m.state_dict()) and m.load_state_dict(state_dict)
    r   c                    sP   || _  fdd| j D }t  | |D ]}t| |||  q,| t d S )Nc                    s"   i | ]}t  |r|t |qS r   r   r   r   r   r   r   `   s     
  z1QuantizedGraphModule.__init__.<locals>.<dictcomp>)r   r   r   r   Z_register_state_dict_hookrC   r   r   r   r   r   ^   s    zQuantizedGraphModule.__init__c           
   	      st   g }|D ]:}	|	 drt||	 tjjrt| |	||	  ||	 q|D ]}	||	 qHt 	||||||| d S r=   )

startswithr?   r"   r@   rA   r   appendpopr   _load_from_state_dict)
r   Z
state_dictrB   Zlocal_metadatastrictZmissing_keysZunexpected_keysZ
error_msgsZattrs_to_popr:   r   r   r   rG   f   s    z*QuantizedGraphModule._load_from_state_dictc                 C   s4   t j }t| j|_t|t| jt| jS r!   )	r"   r#   r$   r%   r&   r'   r   r   r   r(   r   r   r   r*   u   s    
z!QuantizedGraphModule.__deepcopy__)r,   r-   r.   __doc__r   r"   r#   r$   r   r/   r   r   r   r   rG   r*   r0   r   r   r   r   r   X   s   .)r"   r%   Ztorch.fxr   Ztorch.fx.graphr   typingr   r   r   r   __all__r   r	   boolr8   r#   r$   r/   r;   r
   r<   rC   r   r   r   r   r   <module>   s"   