
    Vhz                    .   d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	m
Z
mZ d dlZe
rd dlmZ g dZdZd	Zd
ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ# G d de      Z$e G d d             Z%e G d  d!             Z& G d" d#      Z' G d$ d%      Z(y)&    )annotations)	dataclass)Enum)AnyCallableOptionalTYPE_CHECKINGUnionN)Pattern)BackendConfigBackendPatternConfigDTypeConfigDTypeWithConstraintsObservationTypeinput_dtypeoutput_dtypeweight_dtype
bias_dtype
is_dynamicnameconfigspatternpattern_complex_formatobservation_typedtype_configsroot_module
qat_module#reference_quantized_module_for_rootfused_modulefuser_methodroot_node_getterextra_inputs_getter#num_tensor_args_to_observation_typeinput_type_to_indexc                       e Zd ZdZdZ	 dZ	 dZy)r   zfAn enum that represents different ways of how an operator/operator pattern
    should be observed
    r         N)__name__
__module____qualname____doc__&OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT OUTPUT_SHARE_OBSERVER_WITH_INPUTINPUT_OUTPUT_NOT_OBSERVED     c/home/dcms/DCMS/lib/python3.12/site-packages/torch/ao/quantization/backend_config/backend_config.pyr   r   5   s.     ./*
 ()$
 !"r0   r   c                  t    e Zd ZU dZdZded<   dZded<   dZded<   dZded<   dZ	ded	<   dZ
d
ed<   dZded<   y)r   a  
    Config for specifying additional constraints for a given dtype, such as quantization
    value ranges, scale value ranges, and fixed quantization params, to be used in
    :class:`~torch.ao.quantization.backend_config.DTypeConfig`.

    The constraints currently supported are:

    * `quant_min_lower_bound` and `quant_max_upper_bound`: Lower and upper
      bounds for the minimum and maximum quantized values respectively. If
      the QConfig's `quant_min` and `quant_max` fall outside this range,
      then the QConfig will be ignored.

    * `scale_min_lower_bound` and `scale_max_upper_bound`: Lower and upper
      bounds for the minimum and maximum scale values respectively. If the
      QConfig's minimum scale value (currently exposed as `eps`) falls below
      the lower bound, then the QConfig will be ignored. Note that the upper
      bound is currently not enforced.

    * `scale_exact_match` and `zero_point_exact_match`: Exact match requirements
      for scale and zero point, to be used for operators with fixed quantization
      parameters such as sigmoid and tanh. If the observer specified in the QConfig
      is neither `FixedQParamsObserver` nor `FixedQParamsFakeQuantize`, or if
      the quantization parameters don't match, then the QConfig will be ignored.
    NOptional[torch.dtype]dtypezUnion[int, float, None]quant_min_lower_boundquant_max_upper_boundscale_min_lower_boundscale_max_upper_boundzOptional[float]scale_exact_matchzOptional[int]zero_point_exact_match)r(   r)   r*   r+   r4   __annotations__r5   r6   r7   r8   r9   r:   r/   r0   r1   r   r   L   sZ    2 $(E '5929592959295929)--,0M0r0   r   c                      e Zd ZU dZded<   ded<   ded<   ded<   ded	<   	 	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZedd       Zedd       Zedd       Z	e
dd       ZddZy
)r   aG  
    Config object that specifies the supported data types passed as arguments to
    quantize ops in the reference model spec, for input and output activations,
    weights, and biases.

    For example, consider the following reference model:

      quant1 - [dequant1 - fp32_linear - quant2] - dequant2

    The pattern in the square brackets refers to the reference pattern of
    statically quantized linear. Setting the input dtype as `torch.quint8`
    in the DTypeConfig means we pass in `torch.quint8` as the dtype argument
    to the first quantize op (quant1). Similarly, setting the output dtype as
    `torch.quint8` means we pass in `torch.quint8` as the dtype argument to
    the second quantize op (quant2).

    Note that the dtype here does not refer to the interface dtypes of the
    op. For example, the "input dtype" here is not the dtype of the input
    tensor passed to the quantized linear op. Though it can still be the
    same as the interface dtype, this is not always the case, e.g. the
    interface dtype is fp32 in dynamic quantization but the "input dtype"
    specified in the DTypeConfig would still be quint8. The semantics of
    dtypes here are the same as the semantics of the dtypes specified in
    the observers.

    These dtypes are matched against the ones specified in the user's
    QConfig. If there is a match, and the QConfig satisfies the constraints
    specified in the DTypeConfig (if any), then we will quantize the given
    pattern using this DTypeConfig. Otherwise, the QConfig is ignored and
    the pattern will not be quantized.

    Example usage::

        >>> # xdoctest: +SKIP(failing)
        >>> dtype_config1 = DTypeConfig(
        ...     input_dtype=torch.quint8,
        ...     output_dtype=torch.quint8,
        ...     weight_dtype=torch.qint8,
        ...     bias_dtype=torch.float)

        >>> dtype_config2 = DTypeConfig(
        ...     input_dtype=DTypeWithConstraints(
        ...         dtype=torch.quint8,
        ...         quant_min_lower_bound=0,
        ...         quant_max_upper_bound=255,
        ...     ),
        ...     output_dtype=DTypeWithConstraints(
        ...         dtype=torch.quint8,
        ...         quant_min_lower_bound=0,
        ...         quant_max_upper_bound=255,
        ...     ),
        ...     weight_dtype=DTypeWithConstraints(
        ...         dtype=torch.qint8,
        ...         quant_min_lower_bound=-128,
        ...         quant_max_upper_bound=127,
        ...     ),
        ...     bias_dtype=torch.float)

        >>> dtype_config1.input_dtype
        torch.quint8

        >>> dtype_config2.input_dtype
        torch.quint8

        >>> dtype_config2.input_dtype_with_constraints
        DTypeWithConstraints(dtype=torch.quint8, quant_min_lower_bound=0, quant_max_upper_bound=255, scale_min_lower_bound=None, scale_max_upper_bound=None)
    r   input_dtype_with_constraintsoutput_dtype_with_constraintsweight_dtype_with_constraintsr3   r   Optional[bool]r   Nc                   t        |t              r|| _        nt        |      | _        t        |t              r|| _        nt        |      | _        t        |t              r|| _        nt        |      | _        || _        || _        y )N)r4   )
isinstancer   r=   r>   r?   r   r   )selfr   r   r   r   r   s         r1   __init__zDTypeConfig.__init__   s|     k#780;D-0D;0WD-l$891=D.1E"2D. l$891=D.1E"2D. %$r0   c                .    | j                   j                  S N)r=   r4   rC   s    r1   r   zDTypeConfig.input_dtype   s    00666r0   c                .    | j                   j                  S rF   )r>   r4   rG   s    r1   r   zDTypeConfig.output_dtype       11777r0   c                .    | j                   j                  S rF   )r?   r4   rG   s    r1   r   zDTypeConfig.weight_dtype   rI   r0   c                   |j                  t        d      }|+t        |t        j                  t
        f      st        d      |j                  t        d      }|+t        |t        j                  t
        f      st        d      |j                  t        d      }|+t        |t        j                  t
        f      st        d      |j                  t        d      }|j                  t        d      } | |||||      S )at  
        Create a ``DTypeConfig`` from a dictionary with the following items (all optional):
            "input_dtype": torch.dtype or ``DTypeWithConstraints``
            "output_dtype": torch.dtype or ``DTypeWithConstraints``
            "weight_dtype": torch.dtype or ``DTypeWithConstraints``
            "bias_type": torch.dtype
            "is_dynamic": bool
        Nz@Expected input_dtype to be a torch.dtype or DTypeWithConstraintszAExpected output_dtype to be a torch.dtype or DTypeWithConstraintszAExpected weight_dtype to be a torch.dtype or DTypeWithConstraints)getINPUT_DTYPE_DICT_KEYrB   torchr4   r   
ValueErrorOUTPUT_DTYPE_DICT_KEYWEIGHT_DTYPE_DICT_KEYBIAS_DTYPE_DICT_KEYIS_DYNAMIC_DICT_KEY)clsdtype_config_dictr   r   r   r   r   s          r1   	from_dictzDTypeConfig.from_dict   s    (++,@$G":%++';<,
 R  ),,-BDI#J5;;(<=-
 S  ),,-BDI#J5;;(<=-
 S  '**+>E
&**+>E
;lJ
SSr0   c                @   i }| j                   | j                  |t        <   | j                  | j                  |t
        <   | j                  | j                  |t        <   | j                  | j                  |t        <   | j                  | j                  |t        <   |S )z
        Convert this ``DTypeConfig`` to a dictionary with the items described in
        :func:`~torch.ao.quantization.backend_config.DTypeConfig.from_dict`.
        )r   r=   rM   r   r>   rP   r   r?   rQ   r   rR   r   rS   )rC   rU   s     r1   to_dictzDTypeConfig.to_dict
  s    
 -/'6:6W6W23( 22 % ( 22 % ??&59__12??&59__12  r0   )NNNNN)
r   .Union[torch.dtype, DTypeWithConstraints, None]r   rY   r   rY   r   r3   r   r@   )returnr3   )rU   dict[str, Any]rZ   r   rZ   r[   )r(   r)   r*   r+   r;   rD   propertyr   r   r   classmethodrV   rX   r/   r0   r1   r   r   p   s    CJ #76#77#77%% GKGKGK,0%)%C% E% E	%
 *% #%< 7 7 8 8 8 8  T  TD!r0   r   c                  l    e Zd ZdZdddZd ZddZddZ	 	 	 	 ddZe	dd       Z
edd       Zdd	Zy
)r   ac  Config that defines the set of patterns that can be quantized on a given backend, and how reference
    quantized models can be produced from these patterns.

    A pattern in this context refers to a module, a functional, an operator, or a directed acyclic graph
    of the above. Each pattern supported on the target backend can be individually configured through
    :class:`~torch.ao.quantization.backend_config.BackendPatternConfig` in terms of:

    (1) The supported input/output activation, weight, and bias data types

    (2) How observers and quant/dequant ops are inserted in order to construct the reference pattern, and

    (3) (Optionally) Fusion, QAT, and reference module mappings.

    The format of the patterns is described in:
    https://github.com/pytorch/pytorch/blob/master/torch/ao/quantization/backend_config/README.md

    Example usage::

        import torch
        from torch.ao.quantization.backend_config import (
            BackendConfig,
            BackendPatternConfig,
            DTypeConfig,
            ObservationType,
        )

        weighted_int8_dtype_config = DTypeConfig(
            input_dtype=torch.quint8,
            output_dtype=torch.quint8,
            weight_dtype=torch.qint8,
            bias_dtype=torch.float)

        def fuse_conv2d_relu(is_qat, conv, relu):
            return torch.ao.nn.intrinsic.ConvReLU2d(conv, relu)

        # For quantizing Linear
        linear_config = BackendPatternConfig(torch.nn.Linear)             .set_observation_type(ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT)             .add_dtype_config(weighted_int8_dtype_config)             .set_root_module(torch.nn.Linear)             .set_qat_module(torch.ao.nn.qat.Linear)             .set_reference_quantized_module(torch.ao.nn.quantized.reference.Linear)

        # For fusing Conv2d + ReLU into ConvReLU2d
        conv_relu_config = BackendPatternConfig((torch.nn.Conv2d, torch.nn.ReLU))             .set_observation_type(ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT)             .add_dtype_config(weighted_int8_dtype_config)             .set_fused_module(torch.ao.nn.intrinsic.ConvReLU2d)             .set_fuser_method(fuse_conv2d_relu)

        # For quantizing ConvReLU2d
        fused_conv_relu_config = BackendPatternConfig(torch.ao.nn.intrinsic.ConvReLU2d)             .set_observation_type(ObservationType.OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT)             .add_dtype_config(weighted_int8_dtype_config)             .set_root_module(torch.nn.Conv2d)             .set_qat_module(torch.ao.nn.intrinsic.qat.ConvReLU2d)             .set_reference_quantized_module(torch.ao.nn.quantized.reference.Conv2d)

        backend_config = BackendConfig("my_backend")             .set_backend_pattern_config(linear_config)             .set_backend_pattern_config(conv_relu_config)             .set_backend_pattern_config(fused_conv_relu_config)

    c                     || _         i | _        y rF   )r   !_pattern_complex_format_to_configrC   r   s     r1   rD   zBackendConfig.__init__d  s    	
 WY.r0   c                "    d| j                    dS )NzBackendConfig())__dict__rG   s    r1   __repr__zBackendConfig.__repr__l  s    a00r0   c                    || _         | S )z5
        Set the name of the target backend.
        )r   rb   s     r1   set_namezBackendConfig.set_nameo  s     	r0   c                    t         j                  j                  j                  j                  j                  |      }|| j                  |<   | S )z
        Set the config for an pattern that can be run on the target backend.
        This overrides any existing config for the given pattern.
        )rN   aoquantizationbackend_configutils,_get_pattern_in_reversed_nested_tuple_formatra   )rC   configr   s      r1   set_backend_pattern_configz(BackendConfig.set_backend_pattern_configv  sH     "'!6!6!E!E!K!K!x!x"
 JP../EFr0   c                6    |D ]  }| j                  |        | S )z
        Set the configs for patterns that can be run on the target backend.
        This overrides any existing config for a given pattern if it was previously registered already.
        )rp   )rC   r   confs      r1   set_backend_pattern_configsz)BackendConfig.set_backend_pattern_configs  s&      	2D++D1	2r0   c                H    t        | j                  j                               S )zS
        Return a copy of the list of configs set in this `BackendConfig`.
        )listra   valuesrG   s    r1   r   zBackendConfig.configs  s    
 D::AACDDr0   c                F    | |j                  t        d            }|j                  t        g       D ]k  }t        |t              r|j                  |       %t        |t              r%|j                  t        j                  |             Zt        dt         d       |S )z
        Create a ``BackendConfig`` from a dictionary with the following items:

            "name": the name of the target backend

            "configs": a list of dictionaries that each represents a `BackendPatternConfig`

         zExpected backend_config_dict['z'] to be a dictionary)	rL   NAME_DICT_KEYCONFIGS_DICT_KEYrB   r   rp   dictrV   rO   )rT   backend_config_dictrr   ds       r1   rV   zBackendConfig.from_dict  s     &**="=>$(()92> 	A!12//2At$//0D0N0Nq0QR 45E4FF[\ 	 r0   c                    t         | j                  t        | j                  D cg c]  }|j	                          c}iS c c}w )z
        Convert this ``BackendConfig`` to a dictionary with the items described in
        :func:`~torch.ao.quantization.backend_config.BackendConfig.from_dict`.
        )ry   r   rz   r   rX   )rC   cs     r1   rX   zBackendConfig.to_dict  s4     499DLLAqqyy{A
 	
As   ?
N)rx   )r   str)r   r   rZ   r   )ro   r   rZ   r   )r   list[BackendPatternConfig]rZ   r   )rZ   r   )r|   r[   rZ   r   r\   )r(   r)   r*   r+   rD   rf   rh   rp   rs   r]   r   r^   rV   rX   r/   r0   r1   r   r   !  sa    ?BY1
	1			 E E  *
r0   r   c                      e Zd ZdZdddZd ZddZ	 	 	 	 ddZddZ	 	 	 	 ddZ		 	 	 	 dd	Z
dd
Z	 	 	 	 ddZ	 	 	 	 ddZ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   z
    Config object that specifies quantization behavior for a given operator pattern.
    For a detailed example usage, see :class:`~torch.ao.quantization.backend_config.BackendConfig`.
    Nc                    || _         t        j                  | _        g | _        d | _        d | _        d | _        d | _        d | _	        d | _
        d | _        i | _        i | _        d | _        y rF   )r   r   r,   r   r   r   r   reference_quantized_moduler   r    _root_node_getter_extra_inputs_getter$_num_tensor_args_to_observation_type_input_type_to_index_pattern_complex_formatrC   r   s     r1   rD   zBackendPatternConfig.__init__  sp    *1 / V V02<@;?KO'=A04 6:8<!PR146!:>$r0   c           	         | j                   j                         D ci c]D  \  }}t        |t        t        f      s|$t        |t        t        f      rt        |      dkD  r||F }}}d| dS c c}}w )Nr   zBackendPatternConfig(rd   )re   itemsrB   ru   r{   len)rC   kvdict_nonemptys       r1   rf   zBackendPatternConfig.__repr__  sv     ++-
1D$<0Q]q4,/CFQJ	 qD
 
 '}oQ77
s   A	A0c                B    | j                   t        d      || _        | S )a&  
        Set the pattern to configure.

        The pattern can be a float module, functional operator, pytorch operator, or a tuple
        combination of the above. Tuple patterns are treated as sequential patterns, and
        currently only tuples of 2 or 3 elements are supported.
        <Only one of 'pattern' or 'pattern_complex_format' can be set)r   rO   r   r   s     r1   set_patternz BackendPatternConfig.set_pattern  s.     ''3N  r0   c                    || _         | S )a  
        Set how observers should be inserted in the graph for this pattern.

        Observation type here refers to how observers (or quant-dequant ops) will be placed
        in the graph. This is used to produce the desired reference patterns understood by
        the backend. Weighted ops such as linear and conv require different observers
        (or quantization parameters passed to quantize ops in the reference model) for the
        input and the output.

        There are two observation types:

            `OUTPUT_USE_DIFFERENT_OBSERVER_AS_INPUT` (default): the output observer instance
            will be different from the input. This is the most common observation type.

            `OUTPUT_SHARE_OBSERVER_WITH_INPUT`: the output observer instance will be the
            same as the input. This is useful for operators like `cat`.

        Note: This will be renamed in the near future, since we will soon insert QuantDeQuantStubs
        with observers (and fake quantizes) attached instead of observers themselves.
        )r   )rC   r   s     r1   set_observation_typez)BackendPatternConfig.set_observation_type  s    . !1r0   c                <    | j                   j                  |       | S )z|
        Add a set of supported data types passed as arguments to quantize ops in the
        reference model spec.
        )r   append)rC   dtype_configs     r1   add_dtype_configz%BackendPatternConfig.add_dtype_config   s    
 	!!,/r0   c                    || _         | S )z
        Set the supported data types passed as arguments to quantize ops in the
        reference model spec, overriding all previously registered data types.
        )r   )rC   r   s     r1   set_dtype_configsz&BackendPatternConfig.set_dtype_configs  s     +r0   c                    || _         | S )a  
        Set the module that represents the root for this pattern.

        When we construct the reference quantized model during the convert phase,
        the root modules (e.g. torch.nn.Linear for torch.ao.nn.intrinsic.LinearReLU)
        will be swapped to the corresponding reference quantized modules (e.g.
        torch.ao.nn.reference.quantized.Linear). This allows custom backends to
        specify custom reference quantized module implementations to match the
        numerics of their lowered operators. Since this is a one-to-one mapping,
        both the root module and the reference quantized module must be specified
        in the same BackendPatternConfig in order for the conversion to take place.
        )r   )rC   r   s     r1   set_root_modulez$BackendPatternConfig.set_root_module  s     'r0   c                    || _         | S )zY
        Set the module that represents the QAT implementation for this pattern.
        )r   )rC   r   s     r1   set_qat_modulez#BackendPatternConfig.set_qat_module$  s     %r0   c                    || _         | S )z
        Set the module that represents the reference quantized implementation for
        this pattern's root module.

        For more detail, see :func:`~torch.ao.quantization.backend_config.BackendPatternConfig.set_root_module`.
        )r   )rC   r   s     r1   set_reference_quantized_modulez3BackendPatternConfig.set_reference_quantized_module+  s     +E'r0   c                    || _         | S )z[
        Set the module that represents the fused implementation for this pattern.
        )r   )rC   r   s     r1   set_fused_modulez%BackendPatternConfig.set_fused_module7  s     )r0   c                    || _         | S )a  
        Set the function that specifies how to fuse this BackendPatternConfig's pattern.

        The first argument of this function should be `is_qat`, and the rest of the arguments
        should be the items in the tuple pattern. The return value of this function should be
        the resulting fused module.

        For example, the fuser method for the pattern `(torch.nn.Linear, torch.nn.ReLU)` can be:

            def fuse_linear_relu(is_qat, linear, relu):
                return torch.ao.nn.intrinsic.LinearReLU(linear, relu)

        For a more complicated example, see https://gist.github.com/jerryzh168/8bea7180a8ba3c279f2c9b050f2a69a6.
        )r    )rC   r    s     r1   set_fuser_methodz%BackendPatternConfig.set_fuser_method@  s     )r0   c                    || _         | S rF   )r   )rC   r!   s     r1   _set_root_node_getterz*BackendPatternConfig._set_root_node_getterR  s    !1r0   c                    || _         | S rF   )r   )rC   r"   s     r1   _set_extra_inputs_getterz-BackendPatternConfig._set_extra_inputs_getterV       %8!r0   c                    || _         | S rF   )r   )rC   r#   s     r1   (_set_num_tensor_args_to_observation_typez=BackendPatternConfig._set_num_tensor_args_to_observation_type\  s     5X1r0   c                    || _         | S rF   )r   )rC   r$   s     r1   _set_input_type_to_indexz-BackendPatternConfig._set_input_type_to_indexb  r   r0   c                B    | j                   t        d      || _        | S )a  
        Set the pattern to configure, using the reversed nested tuple format.

        See the BackendConfig README for more detail:
        https://github.com/pytorch/pytorch/blob/master/torch/ao/quantization/backend_config/README.md#advanced-pattern-specification
        r   )r   rO   r   r   s     r1   _set_pattern_complex_formatz0BackendPatternConfig._set_pattern_complex_formath  s-     <<#N  (/$r0   c                   dd} |        }t         |v r|j                  |t                   t        |v r|j                  |t                  |j	                  t
        g       D ]  }|j                   ||              |j                  |j	                  t        d             |j                  |j	                  t        d             |j                  |j	                  t        d             |j                  |j	                  t        d             |j                  |j	                  t         d             |j#                  |j	                  t$        d             |j'                  |j	                  t(        d             |j+                  |j	                  t,        i              |j/                  |j	                  t0        i              t2        |v r|j5                  |t2                  |S )ax  
        Create a ``BackendPatternConfig`` from a dictionary with the following items:

            "pattern": the pattern being configured
            "observation_type": the :class:`~torch.ao.quantization.backend_config.ObservationType` that specifies how
            observers should be inserted for this pattern
            "dtype_configs": a list of dictionaries that represents :class:`~torch.ao.quantization.backend_config.DTypeConfig` s
            "root_module": a :class:`torch.nn.Module` that represents the root for this pattern
            "qat_module": a :class:`torch.nn.Module` that represents the QAT implementation for this pattern
            "reference_quantized_module": a :class:`torch.nn.Module` that represents the reference quantized
            implementation for this pattern's root module.
            "fused_module": a :class:`torch.nn.Module` that represents the fused implementation for this pattern
            "fuser_method": a function that specifies how to fuse the pattern for this pattern
            "pattern_complex_format": the pattern specified in the reversed nested tuple format (deprecated)

        c                    t        | t              r| S t        | t              rt        j                  |       S t	        dt
         dt        |        d      )zo
            Convert the given object into a ``DTypeConfig`` if possible, else throw an exception.
            z@Expected a list of DTypeConfigs in backend_pattern_config_dict["z	"], got '')rB   r   r{   rV   rO   DTYPE_CONFIGS_DICT_KEYtype)objs    r1   _get_dtype_configz9BackendPatternConfig.from_dict.<locals>._get_dtype_config  s]     #{+
#t$",,S1111G0H
SWX[S\R]]^` r0   N)r   r   rZ   r   )PATTERN_DICT_KEYr   OBSERVATION_TYPE_DICT_KEYr   rL   r   r   r   ROOT_MODULE_DICT_KEYr   QAT_MODULE_DICT_KEYr   #REFERENCE_QUANTIZED_MODULE_DICT_KEYr   FUSED_MODULE_DICT_KEYr   FUSER_METHOD_DICT_KEYr   ROOT_NODE_GETTER_DICT_KEYr   EXTRA_INPUTS_GETTER_DICT_KEYr   ,NUM_TENSOR_ARGS_TO_OBSERVATION_TYPE_DICT_KEYr   INPUT_TYPE_TO_INDEX_DICT_KEYPATTERN_COMPLEX_FORMAT_DICT_KEYr   )rT   backend_pattern_config_dictr   rr   r}   s        r1   rV   zBackendPatternConfig.from_dictv  s   *	 u::89IJK$(CC%%+,EF -001GL 	8A!!"3A"67	8'++,@$G	
 	7;;<OQUVW++'++,OQUV	
 	'++,A4H	
 	'++,A4H	
 	""'++,EtL	
 	%%'++,H$O	
 	55'++<b	

 	%%'++,H"M	
 +.II,,+,KL r0   c                f   t         | j                  t        | j                  D cg c]  }|j	                          c}i}| j
                  | j
                  |t        <   | j                  | j                  |t        <   | j                  | j                  |t        <   | j                  | j                  |t        <   | j                  | j                  |t        <   | j                  | j                  |t         <   | j"                  | j"                  |t$        <   | j&                  | j&                  |t(        <   t+        | j,                        dkD  r| j,                  |t.        <   t+        | j0                        dkD  r| j0                  |t2        <   | j4                  | j4                  |t6        <   |S c c}w )z
        Convert this ``BackendPatternConfig`` to a dictionary with the items described in
        :func:`~torch.ao.quantization.backend_config.BackendPatternConfig.from_dict`.
        r   )r   r   r   r   rX   r   r   r   r   r   r   r   r   r   r   r    r   r   r   r   r   r   r   r   r   r   r   r   )rC   r   r   s      r1   rX   zBackendPatternConfig.to_dict  s    &t'<'<"$:L:L$MQQYY[$M7
# <<#<@LL'(89'@D@P@P'(<=??&?C'(;<**6 // (3 (AEARAR'(=>(AEARAR'(=>!!- && () $$0 )) (, t889A= 99 (< t(()A- )) (, ''3 ,, (/ +*I %Ns   F.
rF   )r   zOptional[Pattern])r   r   rZ   r   )r   r   rZ   r   )r   r   rZ   r   )r   zlist[DTypeConfig]rZ   r   )r   type[torch.nn.Module]rZ   r   )r   r   rZ   r   )r   r   rZ   r   )r   r   rZ   r   )r    r   rZ   r   )r!   r   rZ   r   )r"   r   rZ   r   )r#   zdict[int, ObservationType]rZ   r   )r$   zdict[str, int]rZ   r   )r   r[   rZ   r   r\   )r(   r)   r*   r+   rD   rf   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r^   rV   rX   r/   r0   r1   r   r     s   
?"	8 /	4.	0	$
*?
	
1	$#+	3M	#1	 I*8I	I IV++r0   r   ))
__future__r   dataclassesr   enumr   typingr   r   r   r	   r
   rN   torch.ao.quantization.utilsr   __all__rM   rP   rQ   rR   rS   ry   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r/   r0   r1   <module>r      s    " !  @ @  3 % & & " "     ": . ( $ " &K #& & . 4 /T ,4 
d .  1  1  1F m! m! m!`Q
 Q
hx+ x+r0   