class_docstr: Configuration class for specifying global and module level quantization options for linear quantization
  algorithm implemented in :py:class:`LinearQuantizer`.
  
  Linear quantization algorithm simulates the effects of quantization during training, by quantizing
  and dequantizing the weights and/or activations during the model's forward pass. The forward and
  backward pass computations are conducted in ``float`` dtype, however, these ``float`` values follow
  the constraints imposed by ``int8`` and ``quint8`` dtypes. For more details, please refer to
  `Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference
  <https://arxiv.org/pdf/1712.05877.pdf>`_.
  
  For most applications, the only parameters that need to be set are ``quantization_scheme`` and
  ``milestones``.
  
  By default, ``quantization_scheme`` is set to :py:class:`QuantizationScheme.symmetric`, which means
  all weights are quantized with zero point as zero, and activations are quantized with zero point as zero for
  non-negative activations and 128 for all other activations. The weights are quantized using :py:class:`torch.qint8`
  and activations are quantized using :py:class:`torch.quint8`.
  
  Linear quantization algorithm inserts ``observers`` for each weight/activation tensor.
  These observers collect statistics of these tensors' values, for example, the minimum and maximum values they can
  take. These statistics are then used to compute the scale and zero point, which are in turn used for quantizing
  the weights/activations. By default, ``moving_average_min_max`` observer is used. For more details, please
  check `MinMaxObserver <https://pytorch.org/docs/stable/generated/torch.ao.quantization.observer.MinMaxObserver.html#torch.ao.quantization.observer.MinMaxObserver>`_.
  
  The ``milestones`` parameter controls the flow of the quantization algorithm.  The example below illustrates its
  usage in more detail:
  
  .. code-block:: python
  
          model = define_model()
  
          config = LinearQuantizerConfig(
              global_config=ModuleLinearQuantizerConfig(
                  quantization_scheme="symmetric",
                  milestones=[0, 100, 300, 200],
              )
          )
  
          quantizer = LinearQuantizer(model, config)
  
          # prepare the model to insert FakeQuantize layers for QAT
          model = quantizer.prepare()
  
          # use quantizer in your PyTorch training loop
          for inputs, labels in data:
              output = model(inputs)
              loss = loss_fn(output, labels)
              loss.backward()
              optimizer.step()
              quantizer.step()
  
          # In this example, from step 0 onwards, observers will collect statistics
          # of the values of weights/activations. However, between steps 0 and 100,
          # effects of quantization will not be simulated. At step 100, quantization
          # simulation will begin and at step 300, observer statistics collection will
          # stop. A batch norm layer computes mean and variance of input batch for normalizing
          # it during training, and collects running estimates of its computed mean and variance,
          # which are then used for normalization during evaluation. At step 200, batch norm
          # statistics collection is frozen, and the batch norm layers switch to evaluation
          # mode, thus more closely simulating the inference numerics during training time.
  
  Args:
      weight_dtype (:py:class:`torch.dtype`): The dtype to use for quantizing the weights. When dtype
          is set to :py:class:`torch.float32`, the weights corresponding to that layer are not quantized.
          Defaults to :py:class:`torch.qint8`.
      weight_observer (:py:class:`ObserverType`): Type of observer to use for quantizing weights. Defaults
          to ``moving_average_min_max``.
      weight_per_channel (:obj:`bool`): When ``True``, weights are quantized per channel; otherwise, per tensor.
      activation_dtype (:py:class:`torch.dtype`): The dtype to use for quantizing the activations. When dtype
          is set to :py:class:`torch.float32`, the activations corresponding to that layer are not quantized.
          Defaults to :py:class:`torch.quint8`.
      activation_observer (:py:class:`ObserverType`): Type of observer to use for quantizing activations. Allowed
          values are ``min_max`` and ``moving_average_min_max``. Defaults to ``moving_average_min_max``.
      quantization_scheme: (:py:class:`QuantizationScheme`): Type of quantization configuration to use. When
          this parameter is set to :py:class:`QuantizationScheme.symmetric`, all weights are
          quantized with zero point as zero, and activations are quantized with zero point as zero for
          non-negative activations and 128 for all other activations. When it is set to
          :py:class:`QuantizationScheme.affine`, zero point can be set anywhere in the range of values allowed
          for the quantized weight/activation. Defaults to :py:class:`QuantizationScheme.symmetric`.
      milestones (:obj:`list` of :obj:`int`): A list of four integers indicating milestones to use during
          quantization. The first milestone corresponds to enabling observers, the second to enabling fake
          quantization simulation, the third to disabling observers, and the last to freezing batch norm statistics.
          Defaults to ``None``, which means the ``step`` method of :py:class:`LinearQuantizer` will be a no-op and
          all observers and quantization simulation will be turned on from the first step, batch norm layers always
          operate in training mode, and mean and variance statistics collection is not frozen.
class_docstr_tok: ["Configuration","class","for","specifying","global","and","module","level","quantization","options","for","linear","quantization","algorithm","implemented","in",":","py",":","class",":","`","LinearQuantizer","`",".","Linear","quantization","algorithm","simulates","the","effects","of","quantization","during","training",",","by","quantizing","and","dequantizing","the","weights","and\/or","activations","during","the","model","'s","forward","pass",".","The","forward","and","backward","pass","computations","are","conducted","in","``","float","``","dtype",",","however",",","these","``","float","``","values","follow","the","constraints","imposed","by","``","int8","``","and","``","quint8","``","dtypes",".","For","more","details",",","please","refer","to","`","Quantization","and","Training","of","Neural","Networks","for","Efficient","Integer-Arithmetic-Only","Inference","<","https",":","\/\/arxiv.org\/pdf\/1712.05877.pdf",">","`","_",".","For","most","applications",",","the","only","parameters","that","need","to","be","set","are","``","quantization_scheme","``","and","``","milestones","``",".","By","default",",","``","quantization_scheme","``","is","set","to",":","py",":","class",":","`","QuantizationScheme.symmetric","`",",","which","means","all","weights","are","quantized","with","zero","point","as","zero",",","and","activations","are","quantized","with","zero","point","as","zero","for","non-negative","activations","and","128","for","all","other","activations",".","The","weights","are","quantized","using",":","py",":","class",":","`","torch.qint8","`","and","activations","are","quantized","using",":","py",":","class",":","`","torch.quint8","`",".","Linear","quantization","algorithm","inserts","``","observers","``","for","each","weight\/activation","tensor",".","These","observers","collect","statistics","of","these","tensors","'","values",",","for","example",",","the","minimum","and","maximum","values","they","can","take",".","These","statistics","are","then","used","to","compute","the","scale","and","zero","point",",","which","are","in","turn","used","for","quantizing","the","weights\/activations",".","By","default",",","``","moving_average_min_max","``","observer","is","used",".","For","more","details",",","please","check","`","MinMaxObserver","<","https",":","\/\/pytorch.org\/docs\/stable\/generated\/torch.ao.quantization.observer.MinMaxObserver.html","#","torch.ao.quantization.observer.MinMaxObserver",">","`","_",".","The","``","milestones","``","parameter","controls","the","flow","of","the","quantization","algorithm",".","The","example","below","illustrates","its","usage","in","more","detail",":","..","code-block",":",":","python","model","=","define_model","(",")","config","=","LinearQuantizerConfig","(","global_config=ModuleLinearQuantizerConfig","(","quantization_scheme=","''","symmetric","''",",","milestones=","[","0",",","100",",","300",",","200","]",",",")",")","quantizer","=","LinearQuantizer","(","model",",","config",")","#","prepare","the","model","to","insert","FakeQuantize","layers","for","QAT","model","=","quantizer.prepare","(",")","#","use","quantizer","in","your","PyTorch","training","loop","for","inputs",",","labels","in","data",":","output","=","model","(","inputs",")","loss","=","loss_fn","(","output",",","labels",")","loss.backward","(",")","optimizer.step","(",")","quantizer.step","(",")","#","In","this","example",",","from","step","0","onwards",",","observers","will","collect","statistics","#","of","the","values","of","weights\/activations",".","However",",","between","steps","0","and","100",",","#","effects","of","quantization","will","not","be","simulated",".","At","step","100",",","quantization","#","simulation","will","begin","and","at","step","300",",","observer","statistics","collection","will","#","stop",".","A","batch","norm","layer","computes","mean","and","variance","of","input","batch","for","normalizing","#","it","during","training",",","and","collects","running","estimates","of","its","computed","mean","and","variance",",","#","which","are","then","used","for","normalization","during","evaluation",".","At","step","200",",","batch","norm","#","statistics","collection","is","frozen",",","and","the","batch","norm","layers","switch","to","evaluation","#","mode",",","thus","more","closely","simulating","the","inference","numerics","during","training","time",".","Args",":","weight_dtype","(",":","py",":","class",":","`","torch.dtype","`",")",":","The","dtype","to","use","for","quantizing","the","weights",".","When","dtype","is","set","to",":","py",":","class",":","`","torch.float32","`",",","the","weights","corresponding","to","that","layer","are","not","quantized",".","Defaults","to",":","py",":","class",":","`","torch.qint8","`",".","weight_observer","(",":","py",":","class",":","`","ObserverType","`",")",":","Type","of","observer","to","use","for","quantizing","weights",".","Defaults","to","``","moving_average_min_max","``",".","weight_per_channel","(",":","obj",":","`","bool","`",")",":","When","``","True","``",",","weights","are","quantized","per","channel",";","otherwise",",","per","tensor",".","activation_dtype","(",":","py",":","class",":","`","torch.dtype","`",")",":","The","dtype","to","use","for","quantizing","the","activations",".","When","dtype","is","set","to",":","py",":","class",":","`","torch.float32","`",",","the","activations","corresponding","to","that","layer","are","not","quantized",".","Defaults","to",":","py",":","class",":","`","torch.quint8","`",".","activation_observer","(",":","py",":","class",":","`","ObserverType","`",")",":","Type","of","observer","to","use","for","quantizing","activations",".","Allowed","values","are","``","min_max","``","and","``","moving_average_min_max","``",".","Defaults","to","``","moving_average_min_max","``",".","quantization_scheme",":","(",":","py",":","class",":","`","QuantizationScheme","`",")",":","Type","of","quantization","configuration","to","use",".","When","this","parameter","is","set","to",":","py",":","class",":","`","QuantizationScheme.symmetric","`",",","all","weights","are","quantized","with","zero","point","as","zero",",","and","activations","are","quantized","with","zero","point","as","zero","for","non-negative","activations","and","128","for","all","other","activations",".","When","it","is","set","to",":","py",":","class",":","`","QuantizationScheme.affine","`",",","zero","point","can","be","set","anywhere","in","the","range","of","values","allowed","for","the","quantized","weight\/activation",".","Defaults","to",":","py",":","class",":","`","QuantizationScheme.symmetric","`",".","milestones","(",":","obj",":","`","list","`","of",":","obj",":","`","int","`",")",":","A","list","of","four","integers","indicating","milestones","to","use","during","quantization",".","The","first","milestone","corresponds","to","enabling","observers",",","the","second","to","enabling","fake","quantization","simulation",",","the","third","to","disabling","observers",",","and","the","last","to","freezing","batch","norm","statistics",".","Defaults","to","``","None","``",",","which","means","the","``","step","``","method","of",":","py",":","class",":","`","LinearQuantizer","`","will","be","a","no-op","and","all","observers","and","quantization","simulation","will","be","turned","on","from","the","first","step",",","batch","norm","layers","always","operate","in","training","mode",",","and","mean","and","variance","statistics","collection","is","not","frozen","."]
code: def __attrs_post_init__(self):
      if (
          self.weight_dtype == _torch.float32
          and self.activation_dtype != _torch.float32
      ):
          raise ValueError(
              f"Unsupported configuration: weight_dtype = {self.weight_dtype}, "
              f"activation_dtype = {self.activation_dtype}. When weights are not quantized,"
              f"activations cannot be quantized."
          )
  
imports: import logging
  from collections import OrderedDict
  from enum import Enum
  from enum import unique
  from typing import Any
  from typing import Callable
  from typing import Dict
  from typing import List
  from typing import NewType
  from typing import Optional
  from typing import Union
  import cattrs
  import torch
  import torch.ao.quantization
  from attr import define
  from attr import field
  from attrs import validators
  from coremltools.optimize.torch._utils.torch_utils import maybe_convert_str_to_dtype
  from coremltools.optimize.torch._utils.torch_utils import maybe_convert_str_to_mod_type
  from coremltools.optimize.torch.optimization_config import ModuleOptimizationConfig
  from coremltools.optimize.torch.optimization_config import OptimizationConfig
  from coremltools.optimize.torch.optimization_config import _structure_from_dict_hook_factory
label_desc: Use image node_id 1 for calling the ModuleLinearQuantizerConfig obj's underlying member method code with example usage: obj.__attrs_post_init__() without return types