docstr: Obtain corresponding context manager from (multi) model, defaults to BaseContextManager().
  
  :param model: Any model of torch.nn.Module, including all models accelareted by
         InferenceOptimizer.trace/InferenceOptimizer.quantize.
  :param models: Any model of torch.nn.Module or list of torch.nn.Module, including all models
         accelareted by InferenceOptimizer.trace/InferenceOptimizer.quantize.
  :return: a context manager if there is no conflict between context managers,
           otherwise will report RuntimeError.
docstr_tok: ["Obtain","corresponding","context","manager","from","(","multi",")","model",",","defaults","to","BaseContextManager","(",")",".",":","param","model",":","Any","model","of","torch.nn.Module",",","including","all","models","accelareted","by","InferenceOptimizer.trace\/InferenceOptimizer.quantize",".",":","param","models",":","Any","model","of","torch.nn.Module","or","list","of","torch.nn.Module",",","including","all","models","accelareted","by","InferenceOptimizer.trace\/InferenceOptimizer.quantize",".",":","return",":","a","context","manager","if","there","is","no","conflict","between","context","managers",",","otherwise","will","report","RuntimeError","."]
code: def get_context(model: nn.Module, *models):
      """
      Obtain corresponding context manager from (multi) model, defaults to BaseContextManager().
  
      :param model: Any model of torch.nn.Module, including all models accelareted by
             InferenceOptimizer.trace/InferenceOptimizer.quantize.
      :param models: Any model of torch.nn.Module or list of torch.nn.Module, including all models
             accelareted by InferenceOptimizer.trace/InferenceOptimizer.quantize.
      :return: a context manager if there is no conflict between context managers,
               otherwise will report RuntimeError.
      """
  
      def obtain_manager(input_model):
          if hasattr(input_model, "_nano_context_manager"):
              _context_manager = input_model._nano_context_manager
          else:
              _context_manager = generate_context_manager(
                  accelerator=None, precision="fp32"
              )
          return _context_manager
  
      def join_manager(manager1, manager2):
          def is_bf16(x):
              return isinstance(x, AutocastContextManager)
  
          if (is_bf16(manager1) ^ is_bf16(manager2)) is True:
              warnings.warn(
                  "Only one of the context managers uses mixed precision, "
                  "and we will return the context manager with mixed precision."
              )
          manager = None
          if is_bf16(manager1) or is_bf16(manager2):
              manager = AutocastContextManager()
          else:
              manager = BaseContextManager()
          thread_num1 = manager1.thread_num
          thread_num2 = manager2.thread_num
          if thread_num1 != thread_num2:
              if thread_num1 is None or thread_num2 is None:
                  warnings.warn(
                      "One of the two models has thread control and the other "
                      "does not. The returned context manager will be dominated "
                      "by the non-None one."
                  )
              else:
                  warnings.warn(
                      "These context managers have different thread_num.  We will "
                      "set thread_num to the larger one."
                  )
          if thread_num1 is None:
              manager.thread_num = thread_num2
          elif thread_num2 is None:
              manager.thread_num = thread_num1
          else:
              manager.thread_num = max(thread_num1, thread_num2)
          return manager
  
      _context_manager = obtain_manager(model)
      if len(models) == 0:
          # only single model
          return _context_manager
      else:
          for model_ in models:
              if isinstance(model_, nn.Module):
                  new_manager = obtain_manager(model_)
                  _context_manager = join_manager(
                      _context_manager, new_manager
                  )
          return _context_manager
  
imports: import torch
  from torch import nn
  import time
  import sigfig
  import multiprocessing
  from typing import Dict, Callable, Tuple, Optional, List, Union, Sequence, Mapping
  from torch.utils.data import DataLoader
  from torchmetrics.metric import Metric
  from bigdl.nano.utils.common import AccelerationOption, available_acceleration_combination, latency_calculate_helper, torch_loader_latency_calculate_helper, format_optimize_result, BaseInferenceOptimizer
  from bigdl.nano.utils.common import invalidInputError
  from bigdl.nano.pytorch.amp import BF16Model
  from bigdl.nano.pytorch.low_precision.jit_int8_api import PytorchJITINT8Model
  from bigdl.nano.deps.openvino.openvino_api import PytorchOpenVINOModel
  from bigdl.nano.deps.ipex.ipex_api import PytorchIPEXJITModel, PytorchIPEXJITBF16Model, PytorchIPEXQuantizationModel, PytorchIPEXPUModel
  from bigdl.nano.deps.onnxruntime.onnxruntime_api import PytorchONNXRuntimeModel
  from bigdl.nano.deps.neural_compressor.inc_api import quantize
  from bigdl.nano.pytorch.model import AcceleratedLightningModule
  from bigdl.nano.utils.pytorch import get_forward_args, get_input_example
  from bigdl.nano.utils.pytorch import NanoMetric
  from bigdl.nano.utils.pytorch import RepeatDataset, remove_batch_dim_fn
  from bigdl.nano.utils.pytorch import transform_multiple_input_dataloader_to_inc_mode, automatic_add_label_in_dataloader
  from bigdl.nano.utils.pytorch import TORCH_VERSION_LESS_1_10
  from bigdl.nano.utils.pytorch import save_model, load_model
  from bigdl.nano.utils.common import schedule_processors
  from bigdl.nano.utils.common import EnvContext
  from bigdl.nano.pytorch.context_manager import generate_context_manager, BaseContextManager, AutocastContextManager
  from .multi_instance import _MultiInstanceModel, _multi_instance_helper
  import traceback
  import warnings
  import os
label_desc: Use image node_id 4 for calling the InferenceOptimizer obj's underlying member method code with example usage: obj.get_context(model) and returns: _context_manager, manager, _context_manager, _context_manager, isinstance