docstr: Build onnx model to speed up inference and reduce latency.
  The method is Not required to call before predict_with_onnx,
  evaluate_with_onnx or export_onnx_file.
  It is recommended to use when you want to:
  
  | 1. Strictly control the thread to be used during inferencing.
  | 2. Alleviate the cold start problem when you call predict_with_onnx
       for the first time.
  
  :param thread_num: int, the num of thread limit. The value is set to 1 by
         default where no limit is set. Besides, the environment variable
         `OMP_NUM_THREADS` is suggested to be same as `thread_num`.
  :param sess_options: an onnxruntime.SessionOptions instance, if you set this
         other than None, a new onnxruntime session will be built on this setting
         and ignore other settings you assigned(e.g. thread_num...).
  
  Example:
      >>> # to pre build onnx sess
      >>> automodel.build_onnx(thread_num=2)  # build onnx runtime sess for two threads
      >>> pred = automodel.predict_with_onnx(data)
      >>> # ------------------------------------------------------
      >>> # directly call onnx related method is also supported
      >>> # default to build onnx runtime sess for single thread
      >>> pred = automodel.predict_with_onnx(data)
docstr_tok: ["Build","onnx","model","to","speed","up","inference","and","reduce","latency",".","The","method","is","Not","required","to","call","before","predict_with_onnx",",","evaluate_with_onnx","or","export_onnx_file",".","It","is","recommended","to","use","when","you","want","to",":","|","1",".","Strictly","control","the","thread","to","be","used","during","inferencing",".","|","2",".","Alleviate","the","cold","start","problem","when","you","call","predict_with_onnx","for","the","first","time",".",":","param","thread_num",":","int",",","the","num","of","thread","limit",".","The","value","is","set","to","1","by","default","where","no","limit","is","set",".","Besides",",","the","environment","variable","`","OMP_NUM_THREADS","`","is","suggested","to","be","same","as","`","thread_num","`",".",":","param","sess_options",":","an","onnxruntime.SessionOptions","instance",",","if","you","set","this","other","than","None",",","a","new","onnxruntime","session","will","be","built","on","this","setting","and","ignore","other","settings","you","assigned","(","e.g",".","thread_num","...",")",".","Example",":",">",">",">","#","to","pre","build","onnx","sess",">",">",">","automodel.build_onnx","(","thread_num=2",")","#","build","onnx","runtime","sess","for","two","threads",">",">",">","pred","=","automodel.predict_with_onnx","(","data",")",">",">",">","#","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--","--",">",">",">","#","directly","call","onnx","related","method","is","also","supported",">",">",">","#","default","to","build","onnx","runtime","sess","for","single","thread",">",">",">","pred","=","automodel.predict_with_onnx","(","data",")"]
code: def build_onnx(self, thread_num=1, sess_options=None):
      """
      Build onnx model to speed up inference and reduce latency.
      The method is Not required to call before predict_with_onnx,
      evaluate_with_onnx or export_onnx_file.
      It is recommended to use when you want to:
  
      | 1. Strictly control the thread to be used during inferencing.
      | 2. Alleviate the cold start problem when you call predict_with_onnx
           for the first time.
  
      :param thread_num: int, the num of thread limit. The value is set to 1 by
             default where no limit is set. Besides, the environment variable
             `OMP_NUM_THREADS` is suggested to be same as `thread_num`.
      :param sess_options: an onnxruntime.SessionOptions instance, if you set this
             other than None, a new onnxruntime session will be built on this setting
             and ignore other settings you assigned(e.g. thread_num...).
  
      Example:
          >>> # to pre build onnx sess
          >>> automodel.build_onnx(thread_num=2)  # build onnx runtime sess for two threads
          >>> pred = automodel.predict_with_onnx(data)
          >>> # ------------------------------------------------------
          >>> # directly call onnx related method is also supported
          >>> # default to build onnx runtime sess for single thread
          >>> pred = automodel.predict_with_onnx(data)
      """
      from bigdl.nano.utils.common import invalidInputError
  
      if self.backend.startswith("keras"):
          invalidInputError(
              False, "Currenctly, keras not support onnx method."
          )
  
      import onnxruntime
  
      if sess_options is not None and not isinstance(
          sess_options, onnxruntime.SessionOptions
      ):
          invalidInputError(
              False,
              "sess_options should be an onnxruntime.SessionOptions instance"
              f", but found {type(sess_options)}",
          )
      if self.distributed:
          invalidInputError(
              False,
              "build_onnx has not been supported for distributed "
              "forecaster. You can call .to_local() to transform the "
              "forecaster to a non-distributed version.",
          )
      try:
          OMP_NUM_THREADS = os.getenv("OMP_NUM_THREADS")
      except KeyError:
          OMP_NUM_THREADS = 0
      if OMP_NUM_THREADS != str(thread_num):
          warnings.warn(
              "The environment variable OMP_NUM_THREADS is suggested to be same "
              f"as thread_num.You can use 'export OMP_NUM_THREADS={thread_num}'."
          )
  
      import torch
  
      dummy_input = torch.rand(
          1,
          self.best_config["past_seq_len"],
          self.best_config["input_feature_num"],
      )
      self.best_model._build_onnx(
          dummy_input,
          dirname=None,
          thread_num=thread_num,
          sess_options=None,
      )
  
file_path: BigDL/python/chronos/src/bigdl/chronos/autots/model/base_automodel.py
imports: import os
  import json
  from bigdl.chronos.autots.utils import recalculate_n_sampling
  import warnings
label_desc: Use image node_id 9 for calling the BaseAutomodel obj's underlying member method code with example usage: obj.build_onnx(thread_num, sess_options) without return types