
    qi7U                        d dl mZ d dlmZ d dl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y)    )annotations)	dataclass)Any)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                  6    e Zd ZU ded<   ded<   ded<   ded<   y	)
r   QConfigMapping | Noneqconfig_mappingtuple[Any, ...]example_inputsPrepareCustomConfig | Noneprepare_custom_configBackendConfig | Nonebackend_configN)__name__
__module____qualname____annotations__     l/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/torch/ao/quantization/fx/custom_config.pyr   r   $   s     +*##55((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dd	Zdd
ZddZe	 	 	 	 dd       ZddZy)r   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"])
    c                t    i | _         i | _        i | _        g | _        g | _        g | _        g | _        g | _        y 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   sB    OQ$RT&LN&57'8:)24$35%/1!r&   c                    | j                   j                         D ci c]  \  }}t        |      dkD  s|| }}}d| dS c c}}w )Nr   zPrepareCustomConfig()__dict__itemslenr3   kvdict_nonemptys       r'   __repr__zPrepareCustomConfig.__repr__L   I    *.--*=*=*?N$!Q3q6A:ANN%m_A66 O
   AA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+   )r3   module_namer   r   r   r    s         r'   set_standalone_module_namez.PrepareCustomConfig.set_standalone_module_nameP   s+     5P^-BN5
$$[1 r&   c                <    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,   )r3   module_classr   r   r   r    s         r'   set_standalone_module_classz/PrepareCustomConfig.set_standalone_module_classd   s+     8S^-BN8
&&|4 r&   c                    |t         j                  k7  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
ValueErrorr-   )r3   float_classobserved_class
quant_types       r'   set_float_to_observed_mappingz1PrepareCustomConfig.set_float_to_observed_mappingx   s\     )))c  T;;;9;D**:6BP&&z2;?r&   c                    || _         | S )zZ
        Set the modules that are not symbolically traceable, identified by name.
        )r.   )r3   module_namess     r'   set_non_traceable_module_namesz2PrepareCustomConfig.set_non_traceable_module_names   s     +7'r&   c                    || _         | S )z[
        Set the modules that are not symbolically traceable, identified by class.
        )r/   )r3   module_classess     r'    set_non_traceable_module_classesz4PrepareCustomConfig.set_non_traceable_module_classes   s     -;)r&   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.
        )r0   r3   indexess     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.
        )r1   rV   s     r'   set_output_quantized_indexesz0PrepareCustomConfig.set_output_quantized_indexes   s    
 )0%r&   c                    || _         | 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   r3   
attributess     r'   set_preserved_attributesz,PrepareCustomConfig.set_preserved_attributes       
 %/!r&   c                   dd}	 	 	 	 	 	 dd}dd} |        }|j                  t        g       D ]D  \  }}}}	}
 ||t              } ||	t              } ||
t              }|j                  |||||       F |j                  t        g       D ]D  \  }}}}	}
 ||t              } ||	t              } ||
t              }|j	                  |||||       F |j                  t
        i       j                         D ];  \  }}t        |      }|j                         D ]  \  }}|j                  |||        = |j                  |j                  t        g              |j                  |j                  t        g              |j                  |j                  t        g              |j                  |j                  t         g              |j#                  |j                  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.
        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.
            z7Expected QConfigMapping in prepare_custom_config_dict["	"], got '')
isinstancer   dict	from_dictrJ   typeobjdict_keys     r'   _get_qconfig_mappingz;PrepareCustomConfig.from_dict.<locals>._get_qconfig_mapping   s[     #~.#+
#t$%//44J8*T^_cdg_h^iijk r&   c                    t        | t              s| | S t        | t              rt        j                  |       S t	        d| dt        |        d      )zs
            Convert the given object into a PrepareCustomConfig if possible, else throw an exception.
            z<Expected PrepareCustomConfig in prepare_custom_config_dict["rd   re   )rf   r   rg   rh   rJ   ri   rj   s     r'   _get_prepare_custom_configzAPrepareCustomConfig.from_dict.<locals>._get_prepare_custom_config   s]     #23s{
#t$*44S99OPXzYcdhildmcnnop 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.
            z6Expected BackendConfig in prepare_custom_config_dict["rd   re   )rf   r   rg   rh   rJ   ri   rj   s     r'   _get_backend_configz:PrepareCustomConfig.from_dict.<locals>._get_backend_config   s[     #}-
#t$$..s33I(S]^bcf^g]hhij r&   )rk   r   rl   strreturnr   )rk   r   rl   rr   rs   r   )rk   r   rl   rr   rs   r   )getSTANDALONE_MODULE_NAME_DICT_KEYrD    STANDALONE_MODULE_CLASS_DICT_KEYrG   FLOAT_TO_OBSERVED_DICT_KEYr9   r	   rN   rQ   "NON_TRACEABLE_MODULE_NAME_DICT_KEYrT   #NON_TRACEABLE_MODULE_CLASS_DICT_KEYrX    INPUT_QUANTIZED_INDEXES_DICT_KEYrZ   !OUTPUT_QUANTIZED_INDEXES_DICT_KEYr`   PRESERVED_ATTRIBUTES_DICT_KEY)clsprepare_custom_config_dictrm   ro   rq   confrC   qconfig_dictr   _prepare_custom_config_dictbackend_config_dictr   r   r    rF   quant_type_namecustom_module_mappingrM   rK   rL   s                       r'   rh   zPrepareCustomConfig.from_dict   sV   4
			 #	'	
	 u (++,KRP	 
'2=O %?+-L%! 1#%DN ++%!	: (++,LbQ	 
'2>O %?+-M%! 1#%EN ,,%!	. 7Q6T6T&7

%'	2O2 .o>J/D/J/J/L +^22		 	++&**+MrR	
 	--&**+NPRS	
 	((&**+KRP	
 	))&**+LbQ	
 	%%&**+H"M	
 r&   c                   dd}i }| j                   j                         D ]5  \  }}t        |vr	g |t        <   |t           j                   |||             7 | j                  j                         D ]5  \  }}t
        |vr	g |t
        <   |t
           j                   |||             7 | j                  j                         D ]+  \  }}t        |vr	i |t        <   ||t           t        |      <   - t        | j                        dkD  r| j                  |t        <   t        | j                        dkD  r| j                  |t        <   t        | j                        dkD  r| j                  |t        <   t        | j                         dkD  r| j                   |t"        <   t        | j$                        dkD  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`.
        c                    |j                   r|j                   j                         nd }|j                  r|j                  j                         nd }| ||j                  ||j                  fS r*   )r   to_dictr   r   r    )keyer   r~   s       r'   _make_tuplez0PrepareCustomConfig.to_dict.<locals>._make_tupleF  sj    :;:K:K1,,446QUL565L5L''//1RV '   *   r&   r   )r   r   r   r   )r+   r9   ru   appendr,   rv   r-   rw   r   r:   r.   rx   r/   ry   r0   rz   r1   r{   r   r|   )r3   r   drC   sm_config_entryrF   rM   r-   s           r'   r   zPrepareCustomConfig.to_dict@  s   	 ,0,H,H,N,N,P 	(K.a75712-.55K9	 .2-K-K-Q-Q-S 	)L//q86823./66L/:	 ++113	 
%)202,-) ()*@*LM	 t../!3484S4SA01t001A5595V5VA12t++,q0262N2NA./t,,-1373P3PA/0t(()A-/3/H/HA+,r&   Nrs   None)rC   rr   r   r   r   r   r   r   r    r   rs   r   )rF   ri   r   r   r   r   r   r   r    r   rs   r   )rK   ri   rL   ri   rM   r
   rs   r   )rP   	list[str]rs   r   )rS   z
list[type]rs   r   )rW   z	list[int]rs   r   )r_   r   rs   r   )r~   dict[str, Any]rs   r   rs   r   )r!   r"   r#   __doc__r4   r?   rD   rG   r
   rI   rN   rQ   rT   rX   rZ   r`   classmethodrh   r   r%   r&   r'   r   r   .   s,   &27 / (	
  : - 
( / (	
  : - 
0 !* 0 0	  	
 
*%	(	 E)7E	E EN3r&   r   c                  v    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y)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"])
    c                     i | _         g | _        y r*   )observed_to_quantized_mappingr   r2   s    r'   r4   zConvertCustomConfig.__init__  s    PR*/1!r&   c                    | j                   j                         D ci c]  \  }}t        |      dkD  s|| }}}d| dS c c}}w )Nr   zConvertCustomConfig(r6   r7   r;   s       r'   r?   zConvertCustomConfig.__repr__  r@   rA   c                d    || 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   )r3   rL   quantized_classrM   s       r'   !set_observed_to_quantized_mappingz5ConvertCustomConfig.set_observed_to_quantized_mapping  s<     T???=?D..z:IX**:6~Fr&   c                    || _         | S r\   r]   r^   s     r'   r`   z,ConvertCustomConfig.set_preserved_attributes  ra   r&   c                "    |        }|j                  t        i       j                         D ];  \  }}t        |      }|j                         D ]  \  }}|j	                  |||        = |j                  |j                  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.
        )rt   OBSERVED_TO_QUANTIZED_DICT_KEYr9   r	   r   r`   r|   )r}   convert_custom_config_dictr   r   r   rM   rL   r   s           r'   rh   zConvertCustomConfig.from_dict  s    $ u6P6T6T*B7

%'	2O2 .o>J3H3N3N3P /66"OZ		 	%%&**+H"M	
 r&   c                    i }| j                   j                         D ]+  \  }}t        |vr	i |t        <   ||t           t        |      <   - t	        | j
                        dkD  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   r9   r   r   r:   r   r|   )r3   r   rM   r   s       r'   r   zConvertCustomConfig.to_dict  s    
  //557	 
)-Q64601- ,-.DZ.PQ	 t(()A-/3/H/HA+,r&   Nr   )rL   ri   r   ri   rM   r
   rs   r   )r_   r   rs   r   )r   r   rs   r   r   )r!   r"   r#   r   r4   r?   r
   rI   r   r`   r   rh   r   r%   r&   r'   r   r   v  su    27 !* 0 0	  	
 
" )7	 >r&   r   c                  @    e Zd ZdZddZd Zd	dZed
d       ZddZ	y)r   z
    Custom configuration for :func:`~torch.ao.quantization.quantize_fx.fuse_fx`.

    Example usage::

        fuse_custom_config = FuseCustomConfig().set_preserved_attributes(
            ["attr1", "attr2"]
        )
    c                    g | _         y r*   r]   r2   s    r'   r4   zFuseCustomConfig.__init__  s
    /1!r&   c                    | j                   j                         D ci c]  \  }}t        |      dkD  s|| }}}d| dS c c}}w )Nr   zFuseCustomConfig(r6   r7   r;   s       r'   r?   zFuseCustomConfig.__repr__  sI    *.--*=*=*?N$!Q3q6A:ANN"=/33 OrA   c                    || _         | S r\   r]   r^   s     r'   r`   z)FuseCustomConfig.set_preserved_attributes  ra   r&   c                ^     |        }|j                  |j                  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.
        )r`   rt   r|   )r}   fuse_custom_config_dictr   s      r'   rh   zFuseCustomConfig.from_dict  s1     u%%#''(ErJ	
 r&   c                `    i }t        | j                        dkD  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|   )r3   r   s     r'   r   zFuseCustomConfig.to_dict  s2    
 t(()A-/3/H/HA+,r&   Nr   )r_   r   rs   r   )r   r   rs   r   r   )
r!   r"   r#   r   r4   r?   r`   r   rh   r   r%   r&   r'   r   r     s/    24  r&   r   N)
__future__r   dataclassesr   typingr   torch.ao.quantizationr   $torch.ao.quantization.backend_configr    torch.ao.quantization.quant_typer   r	   r
   __all__ru   rv   rw   r   rx   ry   rz   r{   r|   r   r   r   r   r%   r&   r'   <module>r      s    " !  0 >  #; #<  D !L %@ "&B ##9  $; ! 6  ) ) )E EP
^ ^B2 2r&   