
    `ieU                        d dl mZ d dlmZ d dlmZmZ d dlmZ d dl	m
Z
 d dlmZmZmZ g dZdZd	Zd
ZdZdZdZdZdZdZe G d d                      Z G d d          Z G d d          Z G d d          ZdS )    )annotations)	dataclass)AnyOptional)QConfigMapping)BackendConfig)_get_quant_type_to_str_quant_type_from_str	QuantType)ConvertCustomConfigFuseCustomConfigPrepareCustomConfigStandaloneModuleConfigEntrystandalone_module_namestandalone_module_class%float_to_observed_custom_module_class)observed_to_quantized_custom_module_classnon_traceable_module_namenon_traceable_module_classinput_quantized_idxsoutput_quantized_idxspreserved_attributesc                  8    e Zd ZU ded<   ded<   ded<   ded<   d	S )
r   Optional[QConfigMapping]qconfig_mappingtuple[Any, ...]example_inputsOptional[PrepareCustomConfig]prepare_custom_configOptional[BackendConfig]backend_configN)__name__
__module____qualname____annotations__     z/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/ao/quantization/fx/custom_config.pyr   r   $   sD          .---####8888++++++r'   r   c                      e Zd ZdZd*dZd Zd+dZd,dZej	        fd-dZ
d.dZd/dZd0d!Zd0d"Zd1d$Zed2d'            Zd3d(Zd)S )4r   a  
    Custom configuration for :func:`~torch.ao.quantization.quantize_fx.prepare_fx` and
    :func:`~torch.ao.quantization.quantize_fx.prepare_qat_fx`.

    Example usage::

        prepare_custom_config = PrepareCustomConfig()             .set_standalone_module_name("module1", qconfig_mapping, example_inputs,                 child_prepare_custom_config, backend_config)             .set_standalone_module_class(MyStandaloneModule, qconfig_mapping, example_inputs,                 child_prepare_custom_config, backend_config)             .set_float_to_observed_mapping(FloatCustomModule, ObservedCustomModule)             .set_non_traceable_module_names(["module2", "module3"])             .set_non_traceable_module_classes([NonTraceableModule1, NonTraceableModule2])             .set_input_quantized_indexes([0])             .set_output_quantized_indexes([0])             .set_preserved_attributes(["attr1", "attr2"])
    returnNonec                v    i | _         i | _        i | _        g | _        g | _        g | _        g | _        g | _        d S N)standalone_module_namesstandalone_module_classesfloat_to_observed_mappingnon_traceable_module_namesnon_traceable_module_classesinput_quantized_indexesoutput_quantized_indexesr   selfs    r(   __init__zPrepareCustomConfig.__init__B   sH    OQ$RT&LN&57'8:)24$35%/1!!!r'   c                T    d | j                                         D             }d| dS )Nc                @    i | ]\  }}t          |          d k    ||S r   len.0kvs      r(   
<dictcomp>z0PrepareCustomConfig.__repr__.<locals>.<dictcomp>M   )    NNN$!Q3q66A::A:::r'   zPrepareCustomConfig()__dict__itemsr6   dict_nonemptys     r(   __repr__zPrepareCustomConfig.__repr__L   4    NN$-*=*=*?*?NNN6m6666r'   module_namestrr   r   r   r   r   r   r!   r    c                :    t          ||||          | j        |<   | S )a|  
        Set the configuration for running a standalone module identified by ``module_name``.

        If ``qconfig_mapping`` is None, the parent ``qconfig_mapping`` will be used instead.
        If ``prepare_custom_config`` is None, an empty ``PrepareCustomConfig`` will be used.
        If ``backend_config`` is None, the parent ``backend_config`` will be used instead.
        )r   r.   )r6   rK   r   r   r   r!   s         r(   set_standalone_module_namez.PrepareCustomConfig.set_standalone_module_nameP   s-     5P^-BN5
 5
$[1 r'   module_classtypec                :    t          ||||          | j        |<   | S )a}  
        Set the configuration for running a standalone module identified by ``module_class``.

        If ``qconfig_mapping`` is None, the parent ``qconfig_mapping`` will be used instead.
        If ``prepare_custom_config`` is None, an empty ``PrepareCustomConfig`` will be used.
        If ``backend_config`` is None, the parent ``backend_config`` will be used instead.
        )r   r/   )r6   rO   r   r   r   r!   s         r(   set_standalone_module_classz/PrepareCustomConfig.set_standalone_module_classd   s-     8S^-BN8
 8
&|4 r'   float_classobserved_class
quant_typer   c                    |t           j        k    rt          d          || j        vr
i | j        |<   || j        |         |<   | S )a5  
        Set the mapping from a custom float module class to a custom observed module class.

        The observed module class must have a ``from_float`` class method that converts the float module class
        to the observed module class. This is currently only supported for static quantization.
        zQset_float_to_observed_mapping is currently only supported for static quantization)r   STATIC
ValueErrorr0   )r6   rS   rT   rU   s       r(   set_float_to_observed_mappingz1PrepareCustomConfig.set_float_to_observed_mappingx   s[     )))c   T;;;9;D*:6BP&z2;?r'   module_names	list[str]c                    || _         | S )zZ
        Set the modules that are not symbolically traceable, identified by name.
        )r1   )r6   rZ   s     r(   set_non_traceable_module_namesz2PrepareCustomConfig.set_non_traceable_module_names   s     +7'r'   module_classes
list[type]c                    || _         | S )z[
        Set the modules that are not symbolically traceable, identified by class.
        )r2   )r6   r^   s     r(    set_non_traceable_module_classesz4PrepareCustomConfig.set_non_traceable_module_classes   s     -;)r'   indexes	list[int]c                    || _         | S )z
        Set the indexes of the inputs of the graph that should be quantized.
        Inputs are otherwise assumed to be in fp32 by default instead.
        )r3   r6   rb   s     r(   set_input_quantized_indexesz/PrepareCustomConfig.set_input_quantized_indexes   s    
 (/$r'   c                    || _         | S )z
        Set the indexes of the outputs of the graph that should be quantized.
        Outputs are otherwise assumed to be in fp32 by default instead.
        )r4   re   s     r(   set_output_quantized_indexesz0PrepareCustomConfig.set_output_quantized_indexes   s    
 )0%r'   
attributesc                    || _         | S z
        Set the names of the attributes that will persist in the graph module even if they are not used in
        the model's ``forward`` method.
        r   r6   ri   s     r(   set_preserved_attributesz,PrepareCustomConfig.set_preserved_attributes       
 %/!r'   prepare_custom_config_dictdict[str, Any]c                   dd}dd	}dd} |             }|                     t          g           D ]T\  }}}}	}
 ||t                    } ||	t                    } ||
t                    }|                    |||||           U|                     t          g           D ]T\  }}}}	}
 ||t                    } ||	t                    } ||
t                    }|                    |||||           U|                     t
          i                                           D ]E\  }}t          |          }|                                D ]\  }}|                    |||           F|	                    |                     t          g                      |                    |                     t          g                      |                    |                     t          g                      |                    |                     t           g                      |                    |                     t$          g                      |S )a  
        Create a ``PrepareCustomConfig`` from a dictionary with the following items:

            "standalone_module_name": a list of (module_name, qconfig_mapping, example_inputs,
            child_prepare_custom_config, backend_config) tuples

            "standalone_module_class" a list of (module_class, qconfig_mapping, example_inputs,
            child_prepare_custom_config, backend_config) tuples

            "float_to_observed_custom_module_class": a nested dictionary mapping from quantization
            mode to an inner mapping from float module classes to observed module classes, e.g.
            {"static": {FloatCustomModule: ObservedCustomModule}}

            "non_traceable_module_name": a list of modules names that are not symbolically traceable
            "non_traceable_module_class": a list of module classes that are not symbolically traceable
            "input_quantized_idxs": a list of indexes of graph inputs that should be quantized
            "output_quantized_idxs": a list of indexes of graph outputs that should be quantized
            "preserved_attributes": a list of attributes that persist even if they are not used in ``forward``

        This function is primarily for backward compatibility and may be removed in the future.
        objr   dict_keyrL   r*   r   c                    t          | t                    s| | S t          | t                    rt          j        |           S t	          d| dt          |            d          )zn
            Convert the given object into a QConfigMapping if possible, else throw an exception.
            Nz7Expected QConfigMapping in prepare_custom_config_dict["	"], got '')
isinstancer   dict	from_dictrX   rP   rs   rt   s     r(   _get_qconfig_mappingz;PrepareCustomConfig.from_dict.<locals>._get_qconfig_mapping   st     #~.. #+
#t$$ 5%/444k8kk_cdg_h_hkkk  r'   r   c                    t          | t                    s| | S t          | t                    rt                              |           S t	          d| dt          |            d          )zs
            Convert the given object into a PrepareCustomConfig if possible, else throw an exception.
            Nz<Expected PrepareCustomConfig in prepare_custom_config_dict["rv   rw   )rx   r   ry   rz   rX   rP   r{   s     r(   _get_prepare_custom_configzAPrepareCustomConfig.from_dict.<locals>._get_prepare_custom_config   sy     #233 s{
#t$$ :*44S999pPXppdhildmdmppp  r'   r    c                    t          | t                    s| | S t          | t                    rt          j        |           S t	          d| dt          |            d          )zm
            Convert the given object into a BackendConfig if possible, else throw an exception.
            Nz6Expected BackendConfig in prepare_custom_config_dict["rv   rw   )rx   r   ry   rz   rX   rP   r{   s     r(   _get_backend_configz:PrepareCustomConfig.from_dict.<locals>._get_backend_config   st     #}-- 
#t$$ 4$.s333j(jj^bcf^g^gjjj  r'   )rs   r   rt   rL   r*   r   )rs   r   rt   rL   r*   r   )rs   r   rt   rL   r*   r    )getSTANDALONE_MODULE_NAME_DICT_KEYrN    STANDALONE_MODULE_CLASS_DICT_KEYrR   FLOAT_TO_OBSERVED_DICT_KEYrF   r
   rY   r]   "NON_TRACEABLE_MODULE_NAME_DICT_KEYra   #NON_TRACEABLE_MODULE_CLASS_DICT_KEYrf    INPUT_QUANTIZED_INDEXES_DICT_KEYrh   !OUTPUT_QUANTIZED_INDEXES_DICT_KEYrn   PRESERVED_ATTRIBUTES_DICT_KEY)clsrp   r|   r~   r   confrK   qconfig_dictr   _prepare_custom_config_dictbackend_config_dictr   r   r!   rO   quant_type_namecustom_module_mappingrU   rS   rT   s                       r(   rz   zPrepareCustomConfig.from_dict   s%   4
	 
	 
	 
		 	 	 	
	 
	 
	 
	 suu (++,KRPP	 	 
'22= O %?$>+-L% %! 10#%D N ++%    (++,LbQQ	 	 
'22> O %?$>+-M% %! 10#%E N ,,%    7Q6T6T&7
 7

%''	 	2O2 .o>>J/D/J/J/L/L  +^22    	++&**+MrRR	
 	
 	
 	--&**+NPRSS	
 	
 	
 	((&**+KRPP	
 	
 	
 	))&**+LbQQ	
 	
 	
 	%%&**+H"MM	
 	
 	
 r'   c                   dd}i }| j                                         D ]B\  }}t          |vr
g |t          <   |t                                        |||                     C| j                                        D ]B\  }}t
          |vr
g |t
          <   |t
                                        |||                     C| j                                        D ]5\  }}t          |vr
i |t          <   ||t                   t          |          <   6t          | j
                  dk    r| j
        |t          <   t          | j                  dk    r| j        |t          <   t          | j                  dk    r| j        |t          <   t          | j                  dk    r| j        |t"          <   t          | j                  dk    r| j        |t&          <   |S )z
        Convert this ``PrepareCustomConfig`` to a dictionary with the items described in
        :func:`~torch.ao.quantization.fx.custom_config.PrepareCustomConfig.from_dict`.
        keyr   er   c                    |j         r|j                                         nd }|j        r|j                                        nd }| ||j        ||j        fS r-   )r   to_dictr   r   r!   )r   r   r   rp   s       r(   _make_tuplez0PrepareCustomConfig.to_dict.<locals>._make_tupleF  sj    :;:KU1,44666QUL565LV'//111RV '  *  r'   r   )r   r   r   r   )r.   rF   r   appendr/   r   r0   r   r	   r<   r1   r   r2   r   r3   r   r4   r   r   r   )r6   r   drK   sm_config_entryrO   rU   r0   s           r(   r   zPrepareCustomConfig.to_dict@  s   	 	 	 	 ,0,H,N,N,P,P 	 	(K.a775712-.55K99    .2-K-Q-Q-S-S 	 	)L//q886823./66L/::    +1133	 	 
%)2202,-) ()*@*L*LMM t.//!33484SA01t011A55595VA12t+,,q00262NA./t,--11373PA/0t())A--/3/HA+,r'   Nr*   r+   )rK   rL   r   r   r   r   r   r   r!   r    r*   r   )rO   rP   r   r   r   r   r   r   r!   r    r*   r   )rS   rP   rT   rP   rU   r   r*   r   )rZ   r[   r*   r   )r^   r_   r*   r   )rb   rc   r*   r   )ri   r[   r*   r   )rp   rq   r*   r   r*   rq   )r"   r#   r$   __doc__r7   rI   rN   rR   r   rW   rY   r]   ra   rf   rh   rn   classmethodrz   r   r&   r'   r(   r   r   .   s'        &2 2 2 27 7 7   (   0 !* 0	    *                E E E [EN3 3 3 3 3 3r'   r   c                  ^    e Zd ZdZddZd Zej        fddZddZ	e
dd            ZddZdS )r   a;  
    Custom configuration for :func:`~torch.ao.quantization.quantize_fx.convert_fx`.

    Example usage::

        convert_custom_config = ConvertCustomConfig()             .set_observed_to_quantized_mapping(ObservedCustomModule, QuantizedCustomModule)             .set_preserved_attributes(["attr1", "attr2"])
    r*   r+   c                "    i | _         g | _        d S r-   )observed_to_quantized_mappingr   r5   s    r(   r7   zConvertCustomConfig.__init__  s    PR*/1!!!r'   c                T    d | j                                         D             }d| dS )Nc                @    i | ]\  }}t          |          d k    ||S r:   r;   r=   s      r(   rA   z0ConvertCustomConfig.__repr__.<locals>.<dictcomp>  rB   r'   zConvertCustomConfig(rC   rD   rG   s     r(   rI   zConvertCustomConfig.__repr__  rJ   r'   rT   rP   quantized_classrU   r   c                L    || j         vr
i | j         |<   || j         |         |<   | S )a  
        Set the mapping from a custom observed module class to a custom quantized module class.

        The quantized module class must have a ``from_observed`` class method that converts the observed module class
        to the quantized module class.
        )r   )r6   rT   r   rU   s       r(   !set_observed_to_quantized_mappingz5ConvertCustomConfig.set_observed_to_quantized_mapping  s6     T???=?D.z:IX*:6~Fr'   ri   r[   c                    || _         | S rk   rl   rm   s     r(   rn   z,ConvertCustomConfig.set_preserved_attributes  ro   r'   convert_custom_config_dictrq   c                \    |             }|                     t          i                                           D ]E\  }}t          |          }|                                D ]\  }}|                    |||           F|                    |                     t          g                      |S )a  
        Create a ``ConvertCustomConfig`` from a dictionary with the following items:

            "observed_to_quantized_custom_module_class": a nested dictionary mapping from quantization
            mode to an inner mapping from observed module classes to quantized module classes, e.g.::
            {
            "static": {FloatCustomModule: ObservedCustomModule},
            "dynamic": {FloatCustomModule: ObservedCustomModule},
            "weight_only": {FloatCustomModule: ObservedCustomModule}
            }
            "preserved_attributes": a list of attributes that persist even if they are not used in ``forward``

        This function is primarily for backward compatibility and may be removed in the future.
        )r   OBSERVED_TO_QUANTIZED_DICT_KEYrF   r
   r   rn   r   )r   r   r   r   r   rU   rT   r   s           r(   rz   zConvertCustomConfig.from_dict  s    $ suu6P6T6T*B7
 7

%''	 	2O2 .o>>J3H3N3N3P3P  /66"OZ    	%%&**+H"MM	
 	
 	
 r'   c                    i }| j                                         D ]5\  }}t          |vr
i |t          <   ||t                   t          |          <   6t	          | j                  dk    r| j        |t          <   |S )z
        Convert this ``ConvertCustomConfig`` to a dictionary with the items described in
        :func:`~torch.ao.quantization.fx.custom_config.ConvertCustomConfig.from_dict`.
        r   )r   rF   r   r	   r<   r   r   )r6   r   rU   r   s       r(   r   zConvertCustomConfig.to_dict  s    
  /5577	 	 
)-Q664601- ,-.DZ.P.PQQ t())A--/3/HA+,r'   Nr   )rT   rP   r   rP   rU   r   r*   r   )ri   r[   r*   r   )r   rq   r*   r   r   )r"   r#   r$   r   r7   rI   r   rW   r   rn   r   rz   r   r&   r'   r(   r   r   v  s         2 2 2 27 7 7 !* 0	    "       [>     r'   r   c                  H    e Zd ZdZddZd ZddZedd            ZddZ	dS )r   z
    Custom configuration for :func:`~torch.ao.quantization.quantize_fx.fuse_fx`.

    Example usage::

        fuse_custom_config = FuseCustomConfig().set_preserved_attributes(
            ["attr1", "attr2"]
        )
    r*   r+   c                    g | _         d S r-   rl   r5   s    r(   r7   zFuseCustomConfig.__init__  s    /1!!!r'   c                T    d | j                                         D             }d| dS )Nc                @    i | ]\  }}t          |          d k    ||S r:   r;   r=   s      r(   rA   z-FuseCustomConfig.__repr__.<locals>.<dictcomp>  rB   r'   zFuseCustomConfig(rC   rD   rG   s     r(   rI   zFuseCustomConfig.__repr__  s4    NN$-*=*=*?*?NNN3=3333r'   ri   r[   c                    || _         | S rk   rl   rm   s     r(   rn   z)FuseCustomConfig.set_preserved_attributes  ro   r'   fuse_custom_config_dictrq   c                v     |             }|                     |                    t          g                      |S )a/  
        Create a ``ConvertCustomConfig`` from a dictionary with the following items:

            "preserved_attributes": a list of attributes that persist even if they are not used in ``forward``

        This function is primarily for backward compatibility and may be removed in the future.
        )rn   r   r   )r   r   r   s      r(   rz   zFuseCustomConfig.from_dict  sB     suu%%#''(ErJJ	
 	
 	
 r'   c                X    i }t          | j                  dk    r| j        |t          <   |S )z
        Convert this ``FuseCustomConfig`` to a dictionary with the items described in
        :func:`~torch.ao.quantization.fx.custom_config.ConvertCustomConfig.from_dict`.
        r   )r<   r   r   )r6   r   s     r(   r   zFuseCustomConfig.to_dict  s2    
 t())A--/3/HA+,r'   Nr   )ri   r[   r*   r   )r   rq   r*   r   r   )
r"   r#   r$   r   r7   rI   rn   r   rz   r   r&   r'   r(   r   r     s         2 2 2 24 4 4       [     r'   r   N)
__future__r   dataclassesr   typingr   r   torch.ao.quantizationr   $torch.ao.quantization.backend_configr    torch.ao.quantization.quant_typer	   r
   r   __all__r   r   r   r   r   r   r   r   r   r   r   r   r   r&   r'   r(   <module>r      s   " " " " " " ! ! ! ! ! !                 0 0 0 0 0 0 > > > > > >            #; #<  D !L %@ "&B ##9  $; ! 6  , , , , , , , ,E E E E E E E EP
^ ^ ^ ^ ^ ^ ^ ^B2 2 2 2 2 2 2 2 2 2r'   