class_docstr: Mixin class for adding basic quantization logic to quantized modules
class_docstr_tok: ["Mixin","class","for","adding","basic","quantization","logic","to","quantized","modules"]
docstr: Helper function for __init__ of quantized module
  
  Create input and weight quantizer based on quant_desc passed by kwargs, or default of the class.
  
  Args:
      quant_desc_input: An instance of :class:`QuantDescriptor <lightseq.training.pytorch_quantization.tensor_quant.QuantDescriptor>`
      quant_desc_weight: An instance of :class:`QuantDescriptor <lightseq.training.pytorch_quantization.tensor_quant.QuantDescriptor>`
      num_layers: An integer. Default None. If not None, create a list of quantizers.
docstr_tok: ["Helper","function","for","__init__","of","quantized","module","Create","input","and","weight","quantizer","based","on","quant_desc","passed","by","kwargs",",","or","default","of","the","class",".","Args",":","quant_desc_input",":","An","instance","of",":","class",":","`","QuantDescriptor","<","lightseq.training.pytorch_quantization.tensor_quant.QuantDescriptor",">","`","quant_desc_weight",":","An","instance","of",":","class",":","`","QuantDescriptor","<","lightseq.training.pytorch_quantization.tensor_quant.QuantDescriptor",">","`","num_layers",":","An","integer",".","Default","None",".","If","not","None",",","create","a","list","of","quantizers","."]
code: def init_quantizer(
      self, quant_desc_input, quant_desc_weight, num_layers=None
  ):
      """Helper function for __init__ of quantized module
  
      Create input and weight quantizer based on quant_desc passed by kwargs, or default of the class.
  
      Args:
          quant_desc_input: An instance of :class:`QuantDescriptor <lightseq.training.pytorch_quantization.tensor_quant.QuantDescriptor>`
          quant_desc_weight: An instance of :class:`QuantDescriptor <lightseq.training.pytorch_quantization.tensor_quant.QuantDescriptor>`
          num_layers: An integer. Default None. If not None, create a list of quantizers.
      """
      if not inspect.stack()[1].function == "__init__":
          raise TypeError(
              "{} should be only called by __init__ of quantized module.".format(
                  __name__
              )
          )
      self._fake_quant = True
      if (not quant_desc_input.fake_quant) or (
          not quant_desc_weight.fake_quant
      ):
          raise ValueError("Only fake quantization is supported!")
  
      logger.info(
          "Input is %squantized to %d bits in %s with axis %s!",
          "" if not quant_desc_input.fake_quant else "fake ",
          quant_desc_input.num_bits,
          self.__class__.__name__,
          quant_desc_input.axis,
      )
      logger.info(
          "Weight is %squantized to %d bits in %s with axis %s!",
          "" if not quant_desc_weight.fake_quant else "fake ",
          quant_desc_weight.num_bits,
          self.__class__.__name__,
          quant_desc_weight.axis,
      )
  
      if num_layers is None:
          self._input_quantizer = TensorQuantizer(quant_desc_input)
          self._weight_quantizer = TensorQuantizer(quant_desc_weight)
      else:
          self._input_quantizers = nn.ModuleList(
              [
                  TensorQuantizer(quant_desc_input)
                  for _ in range(num_layers)
              ]
          )
          self._weight_quantizers = nn.ModuleList(
              [
                  TensorQuantizer(quant_desc_weight)
                  for _ in range(num_layers)
              ]
          )
  
file_path: lightseq/lightseq/training/pytorch_quantization/nn/modules/_utils.py
imports: import copy
  import inspect
  import logging
  from torch import nn
  from lightseq.training.pytorch_quantization.nn import TensorQuantizer
  from lightseq.training.pytorch_quantization.tensor_quant import QuantDescriptor, QUANT_DESC_8BIT_PER_TENSOR
label_desc: Use image node_id 3 for calling the QuantMixin obj's underlying member method code with example usage: obj.init_quantizer(quant_desc_input, quant_desc_weight, num_layers) without return types