code: def latency(
      args, model_path, forecaster, train_loader, test_loader, records
  ):
      """
      latency stage will record inference latency.
      """
  
      try:
          # load trained model
          forecaster.load(model_path)
      except:
          # if no ckpt can be used, then train a new one
          forecaster.fit(train_loader, epochs=1)
  
      latency, latency_onnx, latency_vino, latency_jit = [], [], [], []
      latency_trim_portion = 0.1
      latency_percentile = [50, 90, 95, 99]
  
      if args.quantize:
          import onnxruntime
  
          sess_options = onnxruntime.SessionOptions()
          if args.cores:
              sess_options.intra_op_num_threads = args.cores
              sess_options.inter_op_num_threads = args.cores
          forecaster.quantize(
              test_loader,
              framework=args.quantize_type,
              sess_options=sess_options,
              thread_num=args.cores if args.cores else None,
          )
          print("QUANTIZATION DONE")
  
      # predict
      if "torch" in args.inference_framework:
          import torch
  
          if args.model == "autoformer":
              for x, y, x_, y_ in test_loader:
                  st = time.time()
                  yhat = forecaster.predict(
                      (x.numpy(), y.numpy(), x_.numpy(), y_.numpy())
                  )
                  latency.append(time.time() - st)
          else:
              for x, y in test_loader:
                  st = time.time()
                  yhat = forecaster.predict(
                      x.numpy(), quantize=args.quantize
                  )
                  latency.append(time.time() - st)
          records["torch_latency"] = stats.trim_mean(
              latency, latency_trim_portion
          )
          records["torch_percentile_latency"] = np.percentile(
              latency, latency_percentile
          )
  
      # predict with onnx
      if "onnx" in args.inference_framework:
          if args.cores and not args.quantize:
              forecaster.build_onnx(thread_num=args.cores)
          for x, y in test_loader:
              st = time.time()
              yhat = forecaster.predict_with_onnx(
                  x.numpy(), quantize=args.quantize
              )
              latency_onnx.append(time.time() - st)
          records["onnx_latency"] = stats.trim_mean(
              latency_onnx, latency_trim_portion
          )
          records["onnx_percentile_latency"] = np.percentile(
              latency_onnx, latency_percentile
          )
  
      # predict with openvino
      if "openvino" in args.inference_framework:
          if args.cores and not args.quantize:
              forecaster.build_openvino(thread_num=args.cores)
          for x, y in test_loader:
              st = time.time()
              yhat = forecaster.predict_with_openvino(
                  x.numpy(), quantize=args.quantize
              )
              latency_vino.append(time.time() - st)
          records["openvino_latency"] = stats.trim_mean(
              latency_vino, latency_trim_portion
          )
          records["openvino_percentile_latency"] = np.percentile(
              latency_vino, latency_percentile
          )
  
      # predict with jit
      if "jit" in args.inference_framework:
          if args.cores:
              forecaster.build_jit(thread_num=args.cores)
          for x, y in test_loader:
              st = time.time()
              yhat = forecaster.predict_with_jit(
                  x.numpy(), quantize=args.quantize
              )
              latency_jit.append(time.time() - st)
          records["jit_latency"] = stats.trim_mean(
              latency_jit, latency_trim_portion
          )
          records["jit_percentile_latency"] = np.percentile(
              latency_jit, latency_percentile
          )
  
file_path: BigDL/python/chronos/src/bigdl/chronos/benchmark/benchmark_chronos.py
imports: from bigdl.chronos.benchmark import generate_forecaster, generate_data, get_CPU_info, check_nano_env
  import time
  import numpy
  import argparse
  import os
  from scipy import stats
  import psutil
  import subprocess
  from bigdl.nano.utils.common import invalidInputError
  from bigdl.nano.utils.common import spawn_new_process
label_desc: Use image node_id 3 for calling a global function with example usage: latency(args, model_path, forecaster, train_loader, test_loader, records) without return types