
    sg                        d dl Z d dlmZmZmZmZ d dlZd dlmZ d dl	m
Z
 g dZ G d de      Z G d d	e      Zd
edefdZdeej                   j"                  ef   dedefdZ G d de      Zd
edefdZd Z G d de      Zy)    N)AnyDictSetUnion)GraphModule)Graph)FusedGraphModuleObservedGraphModuleObservedStandaloneGraphModuleQuantizedGraphModulec                   t     e Zd Zdeej
                  j                  eee	f   f   de
dee   f fdZd Z xZS )r	   rootgraphpreserved_attr_namesc                     || _         | j                   D ci c]  }t        ||      r|t        ||       }}t        |   ||       |D ]  }t        | |||           y c c}w N)r   hasattrgetattrsuper__init__setattrselfr   r   r   attrpreserved_attrs	__class__s         X/var/www/html/venv/lib/python3.12/site-packages/torch/ao/quantization/fx/graph_module.pyr   zFusedGraphModule.__init__   s~     %9! 11
tT" '$%%
 

 	u%# 	7DD$ 56	7
s    A!c                    t         j                  j                         }t        j                  | j
                        |_        t        |t        j                  | j                        t        j                  | j                              S r   )	torchnnModulecopydeepcopy__dict__r	   r   r   r   memofake_mods      r   __deepcopy__zFusedGraphModule.__deepcopy__&   sY    88??$ MM$--8MM$**%MM$334
 	
    __name__
__module____qualname__r   r   r    r!   r   strr   r   r   r   r(   __classcell__r   s   @r   r	   r	      sG    7EHHOOT#s(^347 7 "#h	7&
r)   r	   c                   t     e Zd Zdeej
                  j                  eee	f   f   de
dee   f fdZd Z xZS )r
   r   r   r   c                     h dj                  |      | _        | j                  D ci c]  }t        ||      r|t        ||       }}t        |   ||       |D ]  }t        | |||           y c c}w )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_qconfig)unionr   r   r   r   r   r   r   s         r   r   zObservedGraphModule.__init__1   s    %
 %$
% 	! 11
tT" '$%%
 

 	u%# 	7DD$ 56	7
s    A2c                    t         j                  j                         }t        j                  | j
                        |_        t        |t        j                  | j                        t        j                  | j                              S r   )	r   r    r!   r"   r#   r$   r
   r   r   r%   s      r   r(   z ObservedGraphModule.__deepcopy__O   sY    88??$ MM$--8"MM$**%MM$334
 	
r)   r*   r0   s   @r   r
   r
   0   sG    7EHHOOT#s(^347 7 "#h	7<
r)   r
   modulereturnc                 :    t        | d      xr d| j                  v S Nmeta_observed_graph_module_attrs)r   rC   r?   s    r   _is_observed_modulerF   Y   s    66"T'E'TTr)   model	attr_namec                 j    t        | d      r'd| j                  v rt        | j                  d   |      S y rB   )r   rC   r   )rG   rH   s     r   _get_observed_graph_module_attrrJ   ]   s4     uf"@EJJ"Nuzz"@A9MMr)   c                   t     e Zd Zdeej
                  j                  eee	f   f   de
dee   f fdZd Z xZS )r   r   r   r   c                 N    |j                  ddh      }t        | 	  |||       y )N'_standalone_module_input_quantized_idxs(_standalone_module_output_quantized_idxs)r=   r   r   )r   r   r   r   r   s       r   r   z&ObservedStandaloneGraphModule.__init__f   s5      4999: 
 	u&:;r)   c                    t         j                  j                         }t        j                  | j
                        |_        t        |t        j                  | j                        t        j                  | j                              S r   )	r   r    r!   r"   r#   r$   r   r   r   r%   s      r   r(   z*ObservedStandaloneGraphModule.__deepcopy__t   sY    88??$ MM$--8,MM$**%MM$334
 	
r)   r*   r0   s   @r   r   r   e   sG    <EHHOOT#s(^34< < "#h	<
r)   r   c                 N    t        |       xr | j                  d   j                  S )NrD   )rF   rC   is_observed_standalone_modulerE   s    r   _is_observed_standalone_modulerR   ~   s(    F# 	VKK67UUr)   c                     t        |       D ]J  }d|v st        t        | |      t        j                  j
                        s7t        | |      }||||z   <   L y N_packed_weight)dir
isinstancer   r   _CScriptObject)r   destinationprefix	keep_varsrH   packed_weights         r   _save_packed_weightr^      sW    Y <	y(ZD)$ehh&;&;.
 $D)4M.;K*+<r)   c                        e Zd ZdZdeej                  j                  ee	e
f   f   dedee	   f fdZ fdZd Z x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                     || _         | j                   D ci c]  }t        ||      r|t        ||       }}t        |   ||       |D ]  }t        | |||           | j                  t               y c c}w r   )r   r   r   r   r   r   _register_state_dict_hookr^   r   s         r   r   zQuantizedGraphModule.__init__   s     %9! 11
tT" '$%%
 

 	u%# 	7DD$ 56	7&&':;
s    A6c           	      (   g }|D ]]  }	|	j                  d      st        ||	   t        j                  j                        s=t        | |	||	          |j                  |	       _ |D ]  }	|j                  |	        t        
| %  |||||||       y rT   )

startswithrW   r   rX   rY   r   appendpopr   _load_from_state_dict)r   
state_dictr[   local_metadatastrictmissing_keysunexpected_keys
error_msgsattrs_to_poprH   r   s             r   rf   z*QuantizedGraphModule._load_from_state_dict   s     # 	/I##$45*ZPYEZ\a\d\d\q\q:riI)>?##I.	/ & 	&INN9%	& 	%	
r)   c                    t         j                  j                         }t        j                  | j
                        |_        t        |t        j                  | j                        t        j                  | j                              S r   )	r   r    r!   r"   r#   r$   r   r   r   r%   s      r   r(   z!QuantizedGraphModule.__deepcopy__   sY    88??$ MM$--8#MM$**%MM$334
 	
r)   )r+   r,   r-   __doc__r   r   r    r!   r   r.   r   r   r   r   rf   r(   r/   r0   s   @r   r   r      sQ    <EHHOOT#s(^34< < "#h	<"
<
r)   r   )r"   typingr   r   r   r   r   torch.fxr   torch.fx.graphr   __all__r	   r
   boolrF   r    r!   r.   rJ   r   rR   r^   r    r)   r   <module>rv      s     ( (     
{ 
<&
+ &
RU U U+-.;>
$7 
23 4 <=
; =
r)   