docstr: This is the accelerated model for pytorch and ipex/jit.
  All the external API is based on InferenceOptimizer, so what we have here is
  basically internal APIs and subject to change.
  
  This PytorchIPEXQuantizationModel will serve for int8 and ipex>1.9 models.
  :param model: the model(nn.module) to be transform if from_load is False
         the accelerated model if from_load is True.
  :param calib_data: calibration data is required for static quantization.
  :param q_config: describes how to quantize a layer or a part of the network
         by providing settings (observer classes) for activations and weights
         respectively. Note that QConfig needs to contain observer classes
         (like MinMaxObserver) or a callable that returns instances on
         invocation, not the concrete observer instances themselves.
         Quantization preparation function will instantiate observers multiple
         times for each of the layers. For more details, please refer
         https://pytorch.org/docs/1.13/generated/torch.quantization.qconfig.
         QConfig.html#torch.quantization.qconfig.QConfig .
  :param input_sample: torch tensor indicate the data sample to be used
         for tracing.
  :param channels_last: if set model and data to be channels-last mode.
  :param thread_num: the thread num allocated for this model.
  :param from_load: this will only be set by _load method.
  :param inplace: whether to perform inplace optimization. Default: ``False``.
  :param jit_strict: Whether recording your mutable container types.
  :param example_kwarg_inputs: keyword arguments of example inputs that will
         be passed to ``torch.jit.trace``. Default to ``None``. Either this
         argument or ``input_sample`` should be specified when ``use_jit`` is
         ``True`` and torch > 2.0, otherwise will be ignored.
  :param enable_onednn: Whether to use PyTorch JIT graph fuser based on oneDNN
         Graph API, which provides a flexible API for aggressive fusion. Default to
         ``False``. For more details, please refer https://github.com/
         pytorch/pytorch/tree/master/torch/csrc/jit/codegen/
         onednn#pytorch---onednn-graph-api-bridge.
docstr_tok: ["This","is","the","accelerated","model","for","pytorch","and","ipex\/jit",".","All","the","external","API","is","based","on","InferenceOptimizer",",","so","what","we","have","here","is","basically","internal","APIs","and","subject","to","change",".","This","PytorchIPEXQuantizationModel","will","serve","for","int8","and","ipex",">","1.9","models",".",":","param","model",":","the","model","(","nn.module",")","to","be","transform","if","from_load","is","False","the","accelerated","model","if","from_load","is","True",".",":","param","calib_data",":","calibration","data","is","required","for","static","quantization",".",":","param","q_config",":","describes","how","to","quantize","a","layer","or","a","part","of","the","network","by","providing","settings","(","observer","classes",")","for","activations","and","weights","respectively",".","Note","that","QConfig","needs","to","contain","observer","classes","(","like","MinMaxObserver",")","or","a","callable","that","returns","instances","on","invocation",",","not","the","concrete","observer","instances","themselves",".","Quantization","preparation","function","will","instantiate","observers","multiple","times","for","each","of","the","layers",".","For","more","details",",","please","refer","https",":","\/\/pytorch.org\/docs\/1.13\/generated\/torch.quantization.qconfig",".","QConfig.html","#","torch.quantization.qconfig.QConfig",".",":","param","input_sample",":","torch","tensor","indicate","the","data","sample","to","be","used","for","tracing",".",":","param","channels_last",":","if","set","model","and","data","to","be","channels-last","mode",".",":","param","thread_num",":","the","thread","num","allocated","for","this","model",".",":","param","from_load",":","this","will","only","be","set","by","_load","method",".",":","param","inplace",":","whether","to","perform","inplace","optimization",".","Default",":","``","False","``",".",":","param","jit_strict",":","Whether","recording","your","mutable","container","types",".",":","param","example_kwarg_inputs",":","keyword","arguments","of","example","inputs","that","will","be","passed","to","``","torch.jit.trace","``",".","Default","to","``","None","``",".","Either","this","argument","or","``","input_sample","``","should","be","specified","when","``","use_jit","``","is","``","True","``","and","torch",">","2.0",",","otherwise","will","be","ignored",".",":","param","enable_onednn",":","Whether","to","use","PyTorch","JIT","graph","fuser","based","on","oneDNN","Graph","API",",","which","provides","a","flexible","API","for","aggressive","fusion",".","Default","to","``","False","``",".","For","more","details",",","please","refer","https",":","\/\/github.com\/","pytorch\/pytorch\/tree\/master\/torch\/csrc\/jit\/codegen\/","onednn","#","pytorch","--","-onednn-graph-api-bridge","."]
code: def __init__(
      self,
      model: torch.nn.Module,
      calib_data,
      q_config=None,
      input_sample=None,
      channels_last=None,
      thread_num=None,
      from_load=False,
      inplace=False,
      jit_strict=True,
      example_kwarg_inputs=None,
      enable_onednn=False,
  ):
      """
      This is the accelerated model for pytorch and ipex/jit.
      All the external API is based on InferenceOptimizer, so what we have here is
      basically internal APIs and subject to change.
  
      This PytorchIPEXQuantizationModel will serve for int8 and ipex>1.9 models.
      :param model: the model(nn.module) to be transform if from_load is False
             the accelerated model if from_load is True.
      :param calib_data: calibration data is required for static quantization.
      :param q_config: describes how to quantize a layer or a part of the network
             by providing settings (observer classes) for activations and weights
             respectively. Note that QConfig needs to contain observer classes
             (like MinMaxObserver) or a callable that returns instances on
             invocation, not the concrete observer instances themselves.
             Quantization preparation function will instantiate observers multiple
             times for each of the layers. For more details, please refer
             https://pytorch.org/docs/1.13/generated/torch.quantization.qconfig.
             QConfig.html#torch.quantization.qconfig.QConfig .
      :param input_sample: torch tensor indicate the data sample to be used
             for tracing.
      :param channels_last: if set model and data to be channels-last mode.
      :param thread_num: the thread num allocated for this model.
      :param from_load: this will only be set by _load method.
      :param inplace: whether to perform inplace optimization. Default: ``False``.
      :param jit_strict: Whether recording your mutable container types.
      :param example_kwarg_inputs: keyword arguments of example inputs that will
             be passed to ``torch.jit.trace``. Default to ``None``. Either this
             argument or ``input_sample`` should be specified when ``use_jit`` is
             ``True`` and torch > 2.0, otherwise will be ignored.
      :param enable_onednn: Whether to use PyTorch JIT graph fuser based on oneDNN
             Graph API, which provides a flexible API for aggressive fusion. Default to
             ``False``. For more details, please refer https://github.com/
             pytorch/pytorch/tree/master/torch/csrc/jit/codegen/
             onednn#pytorch---onednn-graph-api-bridge.
      """
      super().__init__(model)
      if from_load:
          self.channels_last = channels_last
          self.jit_strict = jit_strict
          self.enable_onednn = enable_onednn
          self._nano_context_manager = generate_context_manager(
              accelerator="jit",
              precision="int8",
              thread_num=thread_num,
              enable_onednn=enable_onednn,
          )
          return
      self.channels_last = channels_last
      self.original_state_dict = model.state_dict()
      self.jit_strict = jit_strict
      self.enable_onednn = enable_onednn
      self.original_model = model
      if self.channels_last:
          self.model = self.model.to(memory_format=torch.channels_last)
      self._nano_context_manager = generate_context_manager(
          accelerator="jit",
          precision="int8",
          thread_num=thread_num,
          enable_onednn=enable_onednn,
      )
      self.thread_num = thread_num
      if q_config is None:
          # default qconfig
          self.q_config = ipex.quantization.default_static_qconfig
      else:
          self.q_config = q_config
  
      if input_sample is None:
          input_sample = next(iter(calib_data))
          # data transform : remove y
          if (
              isinstance(input_sample, (tuple, list))
              and len(input_sample) > 1
          ):
              input_sample = input_sample[0]
              if self.channels_last:
                  if isinstance(input_sample, torch.Tensor):
                      input_sample = input_sample.to(
                          memory_format=torch.channels_last
                      )
                  else:
                      input_sample = tuple(
                          map(
                              lambda x: x.to(
                                  memory_format=torch.channels_last
                              ),
                              input_sample,
                          )
                      )
  
      # prepare model
      self.model = prepare(
          self.model,
          self.q_config,
          example_inputs=input_sample,
          inplace=inplace,
      )
  
      # calibration model
      for x in calib_data:
          # data transform of calib_data
          if isinstance(x, (tuple, list)) and len(x) > 1:
              x = x[0]
          if isinstance(x, Sequence):
              self.model(*x)
          else:
              self.model(x)
  
      # convert to static quantized model
      self.model = convert(self.model)
      with torch.no_grad():
          self.model = jit_convert(
              self.model,
              input_sample,
              jit_method="trace",
              jit_strict=jit_strict,
              example_kwarg_inputs=example_kwarg_inputs,
          )
          self.model = torch.jit.freeze(self.model)
      # patch attributes from original model
      patch_attrs_from_model_to_object(self.original_model, self)
  
file_path: BigDL/python/nano/src/bigdl/nano/deps/ipex/ipex_quantization_model.py
imports: from collections.abc import Sequence
  import intel_extension_for_pytorch
  from intel_extension_for_pytorch.quantization import prepare, convert
  from bigdl.nano.pytorch.model import AcceleratedLightningModule
  from bigdl.nano.pytorch.context_manager import generate_context_manager
  from bigdl.nano.utils.pytorch import patch_attrs_from_model_to_object, jit_convert
  from bigdl.nano.utils.common import compare_version
  import operator
  import torch
label_desc: Use image node_id 1 to create a new PytorchIPEXQuantizationModel object from inherited base classes: AcceleratedLightningModule with example: obj = PytorchIPEXQuantizationModel(model, calib_data, q_config, input_sample, channels_last, thread_num, from_load, inplace, jit_strict, example_kwarg_inputs, enable_onednn)