o
    IñhÖ  ã                   @   sÒ   d dl Z d dlmZmZ d dlZd dlmZ d dlmZ g d¢Z	G dd„ deƒZ
G dd	„ d	eƒZd
edefdd„Zdeejjef dedefdd„ZG dd„ deƒZd
edefdd„Zdd„ ZG dd„ deƒZdS )é    N)ÚAnyÚUnion)ÚGraphModule)ÚGraph)ÚFusedGraphModuleÚObservedGraphModuleÚObservedStandaloneGraphModuleÚQuantizedGraphModulec                       óJ   e Zd Zdeejjeee	f f de
dee f‡ 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                    ó"   i | ]}t ˆ |ƒr|tˆ |ƒ“qS © ©ÚhasattrÚgetattr©Ú.0Úattr©r   r   úY/var/www/vscode/kcb/lib/python3.10/site-packages/torch/ao/quantization/fx/graph_module.pyÚ
<dictcomp>   ó    ý
ÿz-FusedGraphModule.__init__.<locals>.<dictcomp>)r   ÚsuperÚ__init__Úsetattr©Úselfr   r   r   Úpreserved_attrsr   ©Ú	__class__r   r   r      s   
þÿzFusedGraphModule.__init__c                 C   ó4   t j ¡ }t | j¡|_t|t | j¡t | j¡ƒS ©N)	ÚtorchÚnnÚModuleÚcopyÚdeepcopyÚ__dict__r   r   r   ©r   ÚmemoÚfake_modr   r   r   Ú__deepcopy__&   ó   


ýzFusedGraphModule.__deepcopy__©Ú__name__Ú
__module__Ú__qualname__r   r$   r%   r&   ÚdictÚstrr   r   Úsetr   r-   Ú__classcell__r   r   r    r   r      s    þýür   c                       r
   )r   r   r   r   c                    sP   h d£  |¡| _‡ fdd„| jD ƒ}tƒ  ˆ |¡ |D ]
}t| ||| ƒ qd S )N>
   Ú_is_qatÚ	_patternsÚ_qconfig_mappingÚ_node_name_to_scopeÚ_observed_node_namesÚ_node_name_to_qconfigÚ_prepare_custom_configÚ_activation_post_process_mapÚ _activation_post_process_indexesÚ"_equalization_node_name_to_qconfigc                    r   r   r   r   r   r   r   r   C   r   z0ObservedGraphModule.__init__.<locals>.<dictcomp>)Úunionr   r   r   r   r   r    r   r   r   1   s   õ
þÿzObservedGraphModule.__init__c                 C   r"   r#   )	r$   r%   r&   r'   r(   r)   r   r   r   r*   r   r   r   r-   O   r.   z ObservedGraphModule.__deepcopy__r/   r   r   r    r   r   0   s    þýür   ÚmoduleÚreturnc                 C   s   t | dƒo	d| jv S ©NÚmetaÚ_observed_graph_module_attrs)r   rE   ©rB   r   r   r   Ú_is_observed_moduleY   s   rH   ÚmodelÚ	attr_namec                 C   s(   t | dƒrd| jv rt| jd |ƒS d S rD   )r   rE   r   )rI   rJ   r   r   r   Ú_get_observed_graph_module_attr]   s   rK   c                       r
   )r   r   r   r   c                    s"   |  ddh¡}tƒ  |||¡ d S )NÚ'_standalone_module_input_quantized_idxsÚ(_standalone_module_output_quantized_idxs)rA   r   r   )r   r   r   r   r    r   r   r   f   s   þÿz&ObservedStandaloneGraphModule.__init__c                 C   r"   r#   )	r$   r%   r&   r'   r(   r)   r   r   r   r*   r   r   r   r-   t   r.   z*ObservedStandaloneGraphModule.__deepcopy__r/   r   r   r    r   r   e   s    þýür   c                 C   s   t | ƒo	| jd jS )NrF   )rH   rE   Úis_observed_standalone_modulerG   r   r   r   Ú_is_observed_standalone_module~   s   
þrO   c                 C   sD   t | ƒD ]}d|v rtt| |ƒtjjƒrt| |ƒ}|||| < qd S ©NÚ_packed_weight)ÚdirÚ
isinstancer   r$   Ú_CÚScriptObject)r   ÚdestinationÚprefixÚ	keep_varsrJ   Úpacked_weightr   r   r   Ú_save_packed_weight…   s   
ÿ
€ûrZ   c                       sZ   e Zd ZdZdeejjee	e
f f dedee	 f‡ 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   r   r   c                    sP   || _ ‡ fdd„| j D ƒ}tƒ  ˆ |¡ |D ]
}t| ||| ƒ q|  t¡ d S )Nc                    r   r   r   r   r   r   r   r   œ   r   z1QuantizedGraphModule.__init__.<locals>.<dictcomp>)r   r   r   r   Ú_register_state_dict_hookrZ   r   r    r   r   r   •   s   
þzQuantizedGraphModule.__init__c           
   	      st   g }|D ]}	|	  d¡r!t||	 tjjƒr!t| |	||	 ƒ | |	¡ q|D ]}	| |	¡ q$tƒ  	|||||||¡ d S rP   )
Ú
startswithrS   r$   rT   rU   r   ÚappendÚpopr   Ú_load_from_state_dict)
r   Ú
state_dictrW   Úlocal_metadataÚstrictÚmissing_keysÚunexpected_keysÚ
error_msgsÚattrs_to_poprJ   r    r   r   r_   ¦   s"   

€ùz*QuantizedGraphModule._load_from_state_dictc                 C   r"   r#   )	r$   r%   r&   r'   r(   r)   r	   r   r   r*   r   r   r   r-   Ä   r.   z!QuantizedGraphModule.__deepcopy__)r0   r1   r2   Ú__doc__r   r$   r%   r&   r3   r4   r   r   r5   r   r_   r-   r6   r   r   r    r   r	   Ž   s    þýür	   )r'   Útypingr   r   r$   Útorch.fxr   Útorch.fx.graphr   Ú__all__r   r   ÚboolrH   r%   r&   r4   rK   r   rO   rZ   r	   r   r   r   r   Ú<module>   s(   )ÿÿ
þ	