class_docstr: Trainer for BigDL-Nano pytorch.
  
  This Trainer extends PyTorch Lightning Trainer by adding
  various options to accelerate pytorch training.
class_docstr_tok: ["Trainer","for","BigDL-Nano","pytorch",".","This","Trainer","extends","PyTorch","Lightning","Trainer","by","adding","various","options","to","accelerate","pytorch","training","."]
docstr: Calibrate a Pytorch-Lightning model for post-training quantization.
  
  :param model:           A model to be quantized. Model type should be an instance of
                          nn.Module.
  :param precision:       Global precision of quantized model,
                          supported type: 'int8', 'bf16', 'fp16', defaults to 'int8'.
  :param accelerator:     Use accelerator 'None', 'onnxruntime', 'openvino', defaults to None.
                          None means staying in pytorch.
  :param calib_dataloader:    A torch.utils.data.dataloader.DataLoader object for calibration.
                              Required for static quantization.
                              It's also used as validation dataloader.
  :param metric:              A torchmetrics.metric.Metric object for evaluation.
  :param accuracy_criterion:  Tolerable accuracy drop, defaults to None meaning no
                              accuracy control.
                              accuracy_criterion = {'relative': 0.1, 'higher_is_better': True}
                              allows relative accuracy loss: 1%. accuracy_criterion =
                              {'absolute': 0.99, 'higher_is_better':False} means accuracy
                              must be smaller than 0.99.
  :param approach:    'static' or 'dynamic'.
                      'static': post_training_static_quant,
                      'dynamic': post_training_dynamic_quant.
                      Default: 'static'. OpenVINO supports static mode only.
  :param method:          Method to do quantization. When accelerator=None, supported
      methods: 'fx', 'eager', 'ipex', defaults to 'fx'. If you don't use ipex, suggest using
      'fx' which executes automatic optimizations like fusion. For more information, please
      refer to https://pytorch.org/docs/stable/quantization.html#eager-mode-quantization.
      When accelerator='onnxruntime', supported methods: 'qlinear', 'integer', defaults
      to 'qlinear'. Suggest 'qlinear' for lower accuracy drop if using static quantization.
      More details in https://onnxruntime.ai/docs/performance/quantization.html.
      This argument doesn't take effect for OpenVINO, don't change it for OpenVINO.
  :param conf:        A path to conf yaml file for quantization.
                      Default: None, using default config.
  :param tuning_strategy:    'bayesian', 'basic', 'mse', 'sigopt'. Default: 'bayesian'.
  :param timeout:     Tuning timeout (seconds). Default: None,  which means early stop.
                      Combine with max_trials field to decide when to exit.
  :param max_trials:  Max tune times. Default: None, which means no tuning.
                      Combine with timeout field to decide when to exit.
                      "timeout=0, max_trials=1" means it will try quantization only once and
                      return satisfying best model.
  :param input_sample:      An input example to convert pytorch model into ONNX/OpenVINO.
  :param thread_num: (optional) a int represents how many threads(cores) is needed for
                     inference, only valid for accelerator='onnxruntime'
                     or accelerator='openvino'.
  :param onnxruntime_session_options: The session option for onnxruntime, only valid when
                                      accelerator='onnxruntime', otherwise will be ignored.
  :param logging: whether to log detailed information of model conversion, only valid when
                  accelerator='openvino', otherwise will be ignored. default: True.
  :param **export_kwargs: will be passed to torch.onnx.export function.
  :return:            A accelerated Pytorch-Lightning Model if quantization is sucessful.
  
  .. warning::
       ``bigdl.nano.pytorch.Trainer.quantize`` will be deprecated in future release.
  
       Please use ``bigdl.nano.pytorch.InferenceOptimizer.quantize`` instead.
docstr_tok: ["Calibrate","a","Pytorch-Lightning","model","for","post-training","quantization",".",":","param","model",":","A","model","to","be","quantized",".","Model","type","should","be","an","instance","of","nn.Module",".",":","param","precision",":","Global","precision","of","quantized","model",",","supported","type",":","'int8","'",",","'bf16","'",",","'fp16","'",",","defaults","to","'int8","'",".",":","param","accelerator",":","Use","accelerator","'None","'",",","'onnxruntime","'",",","'openvino","'",",","defaults","to","None",".","None","means","staying","in","pytorch",".",":","param","calib_dataloader",":","A","torch.utils.data.dataloader.DataLoader","object","for","calibration",".","Required","for","static","quantization",".","It","'s","also","used","as","validation","dataloader",".",":","param","metric",":","A","torchmetrics.metric.Metric","object","for","evaluation",".",":","param","accuracy_criterion",":","Tolerable","accuracy","drop",",","defaults","to","None","meaning","no","accuracy","control",".","accuracy_criterion","=","{","'relative","'",":","0.1",",","'higher_is_better","'",":","True","}","allows","relative","accuracy","loss",":","1","%",".","accuracy_criterion","=","{","'absolute","'",":","0.99",",","'higher_is_better","'",":","False","}","means","accuracy","must","be","smaller","than","0.99.",":","param","approach",":","'static","'","or","'dynamic","'",".","'static","'",":","post_training_static_quant",",","'dynamic","'",":","post_training_dynamic_quant",".","Default",":","'static","'",".","OpenVINO","supports","static","mode","only",".",":","param","method",":","Method","to","do","quantization",".","When","accelerator=None",",","supported","methods",":","'fx","'",",","'eager","'",",","'ipex","'",",","defaults","to","'fx","'",".","If","you","do","n't","use","ipex",",","suggest","using","'fx","'","which","executes","automatic","optimizations","like","fusion",".","For","more","information",",","please","refer","to","https",":","\/\/pytorch.org\/docs\/stable\/quantization.html","#","eager-mode-quantization",".","When","accelerator='onnxruntime","'",",","supported","methods",":","'qlinear","'",",","'integer","'",",","defaults","to","'qlinear","'",".","Suggest","'qlinear","'","for","lower","accuracy","drop","if","using","static","quantization",".","More","details","in","https",":","\/\/onnxruntime.ai\/docs\/performance\/quantization.html",".","This","argument","does","n't","take","effect","for","OpenVINO",",","do","n't","change","it","for","OpenVINO",".",":","param","conf",":","A","path","to","conf","yaml","file","for","quantization",".","Default",":","None",",","using","default","config",".",":","param","tuning_strategy",":","'bayesian","'",",","'basic","'",",","'mse","'",",","'sigopt","'",".","Default",":","'bayesian","'",".",":","param","timeout",":","Tuning","timeout","(","seconds",")",".","Default",":","None",",","which","means","early","stop",".","Combine","with","max_trials","field","to","decide","when","to","exit",".",":","param","max_trials",":","Max","tune","times",".","Default",":","None",",","which","means","no","tuning",".","Combine","with","timeout","field","to","decide","when","to","exit",".","``","timeout=0",",","max_trials=1","''","means","it","will","try","quantization","only","once","and","return","satisfying","best","model",".",":","param","input_sample",":","An","input","example","to","convert","pytorch","model","into","ONNX\/OpenVINO",".",":","param","thread_num",":","(","optional",")","a","int","represents","how","many","threads","(","cores",")","is","needed","for","inference",",","only","valid","for","accelerator='onnxruntime'","or","accelerator='openvino","'",".",":","param","onnxruntime_session_options",":","The","session","option","for","onnxruntime",",","only","valid","when","accelerator='onnxruntime","'",",","otherwise","will","be","ignored",".",":","param","logging",":","whether","to","log","detailed","information","of","model","conversion",",","only","valid","when","accelerator='openvino","'",",","otherwise","will","be","ignored",".","default",":","True",".",":","param","*","*","export_kwargs",":","will","be","passed","to","torch.onnx.export","function",".",":","return",":","A","accelerated","Pytorch-Lightning","Model","if","quantization","is","sucessful",".","..","warning",":",":","``","bigdl.nano.pytorch.Trainer.quantize","``","will","be","deprecated","in","future","release",".","Please","use","``","bigdl.nano.pytorch.InferenceOptimizer.quantize","``","instead","."]
code: def quantize(
      model: nn.Module,
      precision: str = "int8",
      accelerator: str = None,
      use_ipex: bool = False,
      calib_dataloader: DataLoader = None,
      metric: Metric = None,
      accuracy_criterion: dict = None,
      approach: str = "static",
      method: str = None,
      conf: str = None,
      tuning_strategy: str = None,
      timeout: int = None,
      max_trials: int = None,
      input_sample=None,
      thread_num: int = None,
      onnxruntime_session_options=None,
      logging: bool = True,
      **export_kwargs,
  ):
      """
      Calibrate a Pytorch-Lightning model for post-training quantization.
  
      :param model:           A model to be quantized. Model type should be an instance of
                              nn.Module.
      :param precision:       Global precision of quantized model,
                              supported type: 'int8', 'bf16', 'fp16', defaults to 'int8'.
      :param accelerator:     Use accelerator 'None', 'onnxruntime', 'openvino', defaults to None.
                              None means staying in pytorch.
      :param calib_dataloader:    A torch.utils.data.dataloader.DataLoader object for calibration.
                                  Required for static quantization.
                                  It's also used as validation dataloader.
      :param metric:              A torchmetrics.metric.Metric object for evaluation.
      :param accuracy_criterion:  Tolerable accuracy drop, defaults to None meaning no
                                  accuracy control.
                                  accuracy_criterion = {'relative': 0.1, 'higher_is_better': True}
                                  allows relative accuracy loss: 1%. accuracy_criterion =
                                  {'absolute': 0.99, 'higher_is_better':False} means accuracy
                                  must be smaller than 0.99.
      :param approach:    'static' or 'dynamic'.
                          'static': post_training_static_quant,
                          'dynamic': post_training_dynamic_quant.
                          Default: 'static'. OpenVINO supports static mode only.
      :param method:          Method to do quantization. When accelerator=None, supported
          methods: 'fx', 'eager', 'ipex', defaults to 'fx'. If you don't use ipex, suggest using
          'fx' which executes automatic optimizations like fusion. For more information, please
          refer to https://pytorch.org/docs/stable/quantization.html#eager-mode-quantization.
          When accelerator='onnxruntime', supported methods: 'qlinear', 'integer', defaults
          to 'qlinear'. Suggest 'qlinear' for lower accuracy drop if using static quantization.
          More details in https://onnxruntime.ai/docs/performance/quantization.html.
          This argument doesn't take effect for OpenVINO, don't change it for OpenVINO.
      :param conf:        A path to conf yaml file for quantization.
                          Default: None, using default config.
      :param tuning_strategy:    'bayesian', 'basic', 'mse', 'sigopt'. Default: 'bayesian'.
      :param timeout:     Tuning timeout (seconds). Default: None,  which means early stop.
                          Combine with max_trials field to decide when to exit.
      :param max_trials:  Max tune times. Default: None, which means no tuning.
                          Combine with timeout field to decide when to exit.
                          "timeout=0, max_trials=1" means it will try quantization only once and
                          return satisfying best model.
      :param input_sample:      An input example to convert pytorch model into ONNX/OpenVINO.
      :param thread_num: (optional) a int represents how many threads(cores) is needed for
                         inference, only valid for accelerator='onnxruntime'
                         or accelerator='openvino'.
      :param onnxruntime_session_options: The session option for onnxruntime, only valid when
                                          accelerator='onnxruntime', otherwise will be ignored.
      :param logging: whether to log detailed information of model conversion, only valid when
                      accelerator='openvino', otherwise will be ignored. default: True.
      :param **export_kwargs: will be passed to torch.onnx.export function.
      :return:            A accelerated Pytorch-Lightning Model if quantization is sucessful.
  
      .. warning::
           ``bigdl.nano.pytorch.Trainer.quantize`` will be deprecated in future release.
  
           Please use ``bigdl.nano.pytorch.InferenceOptimizer.quantize`` instead.
      """
      return InferenceOptimizer.quantize(
          model=model,
          precision=precision,
          accelerator=accelerator,
          use_ipex=use_ipex,
          calib_dataloader=calib_dataloader,
          metric=metric,
          accuracy_criterion=accuracy_criterion,
          approach=approach,
          method=method,
          conf=conf,
          tuning_strategy=tuning_strategy,
          timeout=timeout,
          max_trials=max_trials,
          input_sample=input_sample,
          thread_num=thread_num,
          onnxruntime_session_options=onnxruntime_session_options,
          logging=logging,
          **export_kwargs,
      )
  
imports: import copy
  from logging import warning
  from typing import Any, List, Optional, Union
  import pytorch_lightning
  import torch
  from torch import nn
  from torch.nn.modules.loss import _Loss
  from torch.utils.data import DataLoader
  from torchmetrics.metric import Metric
  from torch.optim.lr_scheduler import _LRScheduler
  from bigdl.nano.pytorch import InferenceOptimizer
  from bigdl.nano.utils.pytorch import TORCH_VERSION_LESS_1_11, check_ccl
  from bigdl.nano.utils.pytorch import ChannelsLastCallback
  from bigdl.nano.utils.pytorch import save_model, load_model
  from bigdl.nano.pytorch.algorithms import SelectiveBackprop
  from bigdl.nano.pytorch.lightning import LightningModule
  from bigdl.nano.pytorch.strategies import IPEXStrategy, DDPSpawnStrategy, DDPSubprocessStrategy, DDPK8sStrategy
  from bigdl.nano.deps.automl.hpo_api import create_hpo_searcher, check_hpo_status
  from bigdl.nano.deps.ray.ray_api import create_ray_strategy
  from bigdl.nano.utils.common import invalidInputError
  from bigdl.nano.utils.common import _avx512_checker
  from bigdl.nano.utils.common import deprecated
label_desc: Use image node_id 7 for calling the Trainer obj's underlying member method code with example usage: obj.quantize(model, precision, accelerator, use_ipex, calib_dataloader, metric, accuracy_criterion, approach, method, conf, tuning_strategy, timeout, max_trials, input_sample, thread_num, onnxruntime_session_options, logging) and returns: InferenceOptimizer