docstr: Quantize the forecaster.
  
  :param input_data: Input data which is used for training. Support following formats:
  
         | 1. a numpy ndarray:
         | The shape is (num_samples, lookback, feature_dim) where lookback and feature_dim
         | should be the same as past_seq_len and input_feature_num.
         |
         | 2. TensorFlow tensor:
         | The shape is (num_samples, lookback, feature_dim) where lookback and feature_dim
         | should be the same as past_seq_len and input_feature_num.
         |
         | 3. an unbatched tf.data.Dataset. Should return a tuple of (inputs, targets).
         |
         | Input data will be used as calibration dataset for static quantization,
         | as well as be used for generating input_sample to calculate latency.
         | To avoid data leak during calibration, please use training dataset.
  
  :param target_data: Target data. It could be either Numpy array(s) or TensorFlow tensor(s)
         while the length should be consistent with `input_data`.
         If `input_data` is dataset, `target_data` will be ignored.
  :param metric: A str represent the metrics for tunning the quality of
         quantization. You may choose from "mse", "mae", "rmse", "r2", "mape", "smape".
  :param conf: A path to conf yaml file for quantization. Default to None,
         using default config.
  :param framework: A str represent the framework for quantization. You may choose from
         "tensorflow", "onnxrt_integerops", "onnxrt_qlinearops", "openvino".
         Default: 'tensorflow'.
  :param approach: str, 'static' or 'dynamic'. Default to 'static'.
         Only 'static' approach is supported now.
  :param tuning_strategy: str, 'bayesian', 'basic', 'mse' or 'sigopt'. Default to 'bayesian'.
  :param relative_drop: Float, tolerable ralative accuracy drop. Default to None,
         e.g. set to 0.1 means that we accept a 10% increase in the metrics error.
  :param absolute_drop: Float, tolerable ralative accuracy drop. Default to None,
         e.g. set to 5 means that we can only accept metrics smaller than 5.
  :param timeout: Tuning timeout (seconds). Default to 0, which means early stop.
         Combine with max_trials field to decide when to exit.
  :param max_trials: Max tune times. Default to 1. 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 sess_options: The session option for onnxruntime, only valid when
         framework contains 'onnxrt_integerops' or 'onnxrt_qlinearops',
         otherwise will be ignored.
  :param thread_num: int, the num of thread limit, only valid when framework contains
         'onnxrt_integerops' or 'onnxrt_qlinearops' or 'openvino'. The value is set to None
         by default where no limit is set.
docstr_tok: ["Quantize","the","forecaster",".",":","param","input_data",":","Input","data","which","is","used","for","training",".","Support","following","formats",":","|","1.","a","numpy","ndarray",":","|","The","shape","is","(","num_samples",",","lookback",",","feature_dim",")","where","lookback","and","feature_dim","|","should","be","the","same","as","past_seq_len","and","input_feature_num",".","|","|","2",".","TensorFlow","tensor",":","|","The","shape","is","(","num_samples",",","lookback",",","feature_dim",")","where","lookback","and","feature_dim","|","should","be","the","same","as","past_seq_len","and","input_feature_num",".","|","|","3.","an","unbatched","tf.data.Dataset",".","Should","return","a","tuple","of","(","inputs",",","targets",")",".","|","|","Input","data","will","be","used","as","calibration","dataset","for","static","quantization",",","|","as","well","as","be","used","for","generating","input_sample","to","calculate","latency",".","|","To","avoid","data","leak","during","calibration",",","please","use","training","dataset",".",":","param","target_data",":","Target","data",".","It","could","be","either","Numpy","array","(","s",")","or","TensorFlow","tensor","(","s",")","while","the","length","should","be","consistent","with","`","input_data","`",".","If","`","input_data","`","is","dataset",",","`","target_data","`","will","be","ignored",".",":","param","metric",":","A","str","represent","the","metrics","for","tunning","the","quality","of","quantization",".","You","may","choose","from","``","mse","''",",","``","mae","''",",","``","rmse","''",",","``","r2","''",",","``","mape","''",",","``","smape","''",".",":","param","conf",":","A","path","to","conf","yaml","file","for","quantization",".","Default","to","None",",","using","default","config",".",":","param","framework",":","A","str","represent","the","framework","for","quantization",".","You","may","choose","from","``","tensorflow","''",",","``","onnxrt_integerops","''",",","``","onnxrt_qlinearops","''",",","``","openvino","''",".","Default",":","'tensorflow","'",".",":","param","approach",":","str",",","'static","'","or","'dynamic","'",".","Default","to","'static","'",".","Only","'static","'","approach","is","supported","now",".",":","param","tuning_strategy",":","str",",","'bayesian","'",",","'basic","'",",","'mse","'","or","'sigopt","'",".","Default","to","'bayesian","'",".",":","param","relative_drop",":","Float",",","tolerable","ralative","accuracy","drop",".","Default","to","None",",","e.g",".","set","to","0.1","means","that","we","accept","a","10","%","increase","in","the","metrics","error",".",":","param","absolute_drop",":","Float",",","tolerable","ralative","accuracy","drop",".","Default","to","None",",","e.g",".","set","to","5","means","that","we","can","only","accept","metrics","smaller","than","5.",":","param","timeout",":","Tuning","timeout","(","seconds",")",".","Default","to","0",",","which","means","early","stop",".","Combine","with","max_trials","field","to","decide","when","to","exit",".",":","param","max_trials",":","Max","tune","times",".","Default","to","1",".","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","sess_options",":","The","session","option","for","onnxruntime",",","only","valid","when","framework","contains","'onnxrt_integerops","'","or","'onnxrt_qlinearops","'",",","otherwise","will","be","ignored",".",":","param","thread_num",":","int",",","the","num","of","thread","limit",",","only","valid","when","framework","contains","'onnxrt_integerops","'","or","'onnxrt_qlinearops","'","or","'openvino","'",".","The","value","is","set","to","None","by","default","where","no","limit","is","set","."]
code: def quantize(
      self,
      input_data=None,
      target_data=None,
      metric=None,
      conf=None,
      framework="tensorflow",
      approach="static",
      tuning_strategy="bayesian",
      relative_drop=None,
      absolute_drop=None,
      timeout=0,
      max_trials=1,
      sess_options=None,
      thread_num=None,
  ):
      """
      Quantize the forecaster.
  
      :param input_data: Input data which is used for training. Support following formats:
  
             | 1. a numpy ndarray:
             | The shape is (num_samples, lookback, feature_dim) where lookback and feature_dim
             | should be the same as past_seq_len and input_feature_num.
             |
             | 2. TensorFlow tensor:
             | The shape is (num_samples, lookback, feature_dim) where lookback and feature_dim
             | should be the same as past_seq_len and input_feature_num.
             |
             | 3. an unbatched tf.data.Dataset. Should return a tuple of (inputs, targets).
             |
             | Input data will be used as calibration dataset for static quantization,
             | as well as be used for generating input_sample to calculate latency.
             | To avoid data leak during calibration, please use training dataset.
  
      :param target_data: Target data. It could be either Numpy array(s) or TensorFlow tensor(s)
             while the length should be consistent with `input_data`.
             If `input_data` is dataset, `target_data` will be ignored.
      :param metric: A str represent the metrics for tunning the quality of
             quantization. You may choose from "mse", "mae", "rmse", "r2", "mape", "smape".
      :param conf: A path to conf yaml file for quantization. Default to None,
             using default config.
      :param framework: A str represent the framework for quantization. You may choose from
             "tensorflow", "onnxrt_integerops", "onnxrt_qlinearops", "openvino".
             Default: 'tensorflow'.
      :param approach: str, 'static' or 'dynamic'. Default to 'static'.
             Only 'static' approach is supported now.
      :param tuning_strategy: str, 'bayesian', 'basic', 'mse' or 'sigopt'. Default to 'bayesian'.
      :param relative_drop: Float, tolerable ralative accuracy drop. Default to None,
             e.g. set to 0.1 means that we accept a 10% increase in the metrics error.
      :param absolute_drop: Float, tolerable ralative accuracy drop. Default to None,
             e.g. set to 5 means that we can only accept metrics smaller than 5.
      :param timeout: Tuning timeout (seconds). Default to 0, which means early stop.
             Combine with max_trials field to decide when to exit.
      :param max_trials: Max tune times. Default to 1. 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 sess_options: The session option for onnxruntime, only valid when
             framework contains 'onnxrt_integerops' or 'onnxrt_qlinearops',
             otherwise will be ignored.
      :param thread_num: int, the num of thread limit, only valid when framework contains
             'onnxrt_integerops' or 'onnxrt_qlinearops' or 'openvino'. The value is set to None
             by default where no limit is set.
      """
      # check model support for quantization
      from bigdl.nano.utils.common import invalidInputError
      from bigdl.nano.tf.keras import InferenceOptimizer
  
      if not self.quantize_available:
          invalidInputError(
              False, "This model has not supported quantization."
          )
  
      # Distributed forecaster does not support quantization
      if self.distributed:
          invalidInputError(
              False,
              "quantization has not been supported for distributed "
              "forecaster. You can call .to_local() to transform the "
              "forecaster to a non-distributed version.",
          )
  
      try:
          metric = _str2metric(metric)
      except Exception:
          invalidInputError(
              False,
              "Unable to recognize the metric string you passed in.",
          )
  
      # init acc criterion
      accuracy_criterion = None
      if relative_drop and absolute_drop:
          invalidInputError(
              False,
              "Please unset either `relative_drop` or `absolute_drop`.",
          )
      if relative_drop:
          accuracy_criterion = {
              "relative": relative_drop,
              "higher_is_better": False,
          }
      if absolute_drop:
          accuracy_criterion = {
              "absolute": absolute_drop,
              "higher_is_better": False,
          }
  
      # quantize
      if "_" in framework:
          accelerator, method = framework.split("_")
      else:
          accelerator = framework
      if accelerator == "tensorflow":
          accelerator = None
          method = None
      elif accelerator == "openvino":
          method = None
      else:
          accelerator = "onnxruntime"
          method = method[:-3]
      input_spec = tf.TensorSpec(
          shape=(
              None,
              self.model_config["past_seq_len"],
              self.model_config["input_feature_num"],
          )
      )
      q_model = InferenceOptimizer.quantize(
          self.internal,
          x=input_data,
          y=target_data,
          input_spec=input_spec,
          precision="int8",
          accelerator=accelerator,
          method=method,
          metric=metric,
          conf=conf,
          approach=approach,
          tuning_strategy=tuning_strategy,
          accuracy_criterion=accuracy_criterion,
          timeout=timeout,
          max_trials=max_trials,
          onnxruntime_session_options=sess_options,
          thread_num=thread_num,
      )
      if accelerator == "onnxruntime":
          self.accelerated_model = q_model
          self.accelerate_method = "onnxruntime_int8"
      if accelerator == "openvino":
          self.accelerated_model = q_model
          self.accelerate_method = "openvino_int8"
      if accelerator is None:
          self.accelerated_model = q_model
          self.accelerate_method = "tensorflow_int8"
  
imports: import warnings
  import keras
  import tensorflow
  import numpy
  from bigdl.chronos.forecaster.abstract import Forecaster
  from bigdl.chronos.data import TSDataset
  from bigdl.chronos.metric.forecast_metrics import Evaluator
  from bigdl.nano.utils.common import invalidInputError
  from bigdl.chronos.forecaster.tf.utils import np_to_data_creator, tsdata_to_data_creator, np_to_xshards, xshard_to_np, np_to_tfdataset
label_desc: Use image node_id 3 for calling the BaseTF2Forecaster obj's underlying member method code with example usage: obj.quantize(input_data, target_data, metric, conf, framework, approach, tuning_strategy, relative_drop, absolute_drop, timeout, max_trials, sess_options, thread_num) without return types