class_docstr: Base default handler to load torchscript or eager mode [state_dict] models
  Also, provides handle method per torch serve custom model specification
class_docstr_tok: ["Base","default","handler","to","load","torchscript","or","eager","mode","[","state_dict","]","models","Also",",","provides","handle","method","per","torch","serve","custom","model","specification"]
docstr: Initialize function loads the model.pt file and initialized the model object.
     First try to load torchscript else load eager mode state_dict based model.
  
  Args:
      context (context): It is a JSON Object containing information
      pertaining to the model artifacts parameters.
  
  Raises:
      RuntimeError: Raises the Runtime error when the model.py is missing
docstr_tok: ["Initialize","function","loads","the","model.pt","file","and","initialized","the","model","object",".","First","try","to","load","torchscript","else","load","eager","mode","state_dict","based","model",".","Args",":","context","(","context",")",":","It","is","a","JSON","Object","containing","information","pertaining","to","the","model","artifacts","parameters",".","Raises",":","RuntimeError",":","Raises","the","Runtime","error","when","the","model.py","is","missing"]
code: def initialize(self, context):
      """Initialize function loads the model.pt file and initialized the model object.
         First try to load torchscript else load eager mode state_dict based model.
  
      Args:
          context (context): It is a JSON Object containing information
          pertaining to the model artifacts parameters.
  
      Raises:
          RuntimeError: Raises the Runtime error when the model.py is missing
  
      """
  
      if context is not None and hasattr(context, "model_yaml_config"):
          self.model_yaml_config = context.model_yaml_config
  
      properties = context.system_properties
      if (
          torch.cuda.is_available()
          and properties.get("gpu_id") is not None
      ):
          self.map_location = "cuda"
          self.device = torch.device(
              self.map_location + ":" + str(properties.get("gpu_id"))
          )
      elif XLA_AVAILABLE:
          self.device = xm.xla_device()
      else:
          self.map_location = "cpu"
          self.device = torch.device(self.map_location)
  
      self.manifest = context.manifest
  
      model_dir = properties.get("model_dir")
      self.model_pt_path = None
      if "serializedFile" in self.manifest["model"]:
          serialized_file = self.manifest["model"]["serializedFile"]
          self.model_pt_path = os.path.join(model_dir, serialized_file)
      # model def file
      model_file = self.manifest["model"].get("modelFile", "")
  
      if model_file:
          logger.debug("Loading eager model")
          self.model = self._load_pickled_model(
              model_dir, model_file, self.model_pt_path
          )
          self.model.to(self.device)
          self.model.eval()
  
      # Convert your model by following instructions: https://pytorch.org/tutorials/intermediate/nvfuser_intro_tutorial.html
      # For TensorRT support follow instructions here: https://pytorch.org/TensorRT/getting_started/getting_started_with_python_api.html#getting-started-with-python-api
      elif self.model_pt_path.endswith(".pt"):
          self.model = self._load_torchscript_model(self.model_pt_path)
          self.model.eval()
  
      # Convert your model by following instructions: https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html
      elif self.model_pt_path.endswith(".onnx") and ONNX_AVAILABLE:
          self.model = setup_ort_session(
              self.model_pt_path, self.map_location
          )
          logger.info("Succesfully setup ort session")
  
      elif (
          self.model_pt_path.endswith(".so")
          and self._use_torch_export_aot_compile()
          and PT220_AVAILABLE
      ):
          # Set cuda device to the gpu_id of the backend worker
          # This is needed as the API for loading the exported model doesn't yet have a device id
          if (
              torch.cuda.is_available()
              and properties.get("gpu_id") is not None
          ):
              torch.cuda.set_device(self.device)
  
          self.model = self._load_torch_export_aot_compile(
              self.model_pt_path
          )
          logger.warning(
              "torch._export is an experimental feature! Succesfully loaded torch exported model."
          )
      else:
          raise RuntimeError("No model weights could be loaded")
  
      if (
          hasattr(self, "model_yaml_config")
          and "pt2" in self.model_yaml_config
      ):
          pt2_value = self.model_yaml_config["pt2"]
  
          if "export" in pt2_value:
              valid_backend = False
          else:
              # pt2_value can be the backend, passed as a str, or arbitrary kwargs, passed as a dict
              if isinstance(pt2_value, str):
                  compile_options = dict(backend=pt2_value)
              elif isinstance(pt2_value, dict):
                  compile_options = pt2_value
              else:
                  raise ValueError("pt2 should be str or dict")
  
              # if backend is not provided, compile will use its default, which is valid
              valid_backend = (
                  check_valid_pt2_backend(compile_options["backend"])
                  if "backend" in compile_options
                  else True
              )
      else:
          valid_backend = False
  
      # PT 2.0 support is opt in
      if PT2_AVAILABLE and valid_backend:
          compile_options_str = ", ".join(
              [f"{k} {v}" for k, v in compile_options.items()]
          )
          # Compilation will delay your model initialization
          try:
              self.model = torch.compile(
                  self.model,
                  **compile_options,
              )
              logger.info(f"Compiled model with {compile_options_str}")
          except Exception as e:
              logger.warning(
                  f"Compiling model model with {compile_options_str} has failed \n Proceeding without compilation"
              )
              logger.warning(e)
  
      elif IPEX_AVAILABLE:
          self.model = self.model.to(memory_format=torch.channels_last)
          self.model = ipex.optimize(self.model)
          logger.info(f"Compiled model with ipex")
  
      logger.debug(
          "Model file %s loaded successfully", self.model_pt_path
      )
  
      # Load class mapping for classifiers
      mapping_file_path = os.path.join(model_dir, "index_to_name.json")
      self.mapping = load_label_mapping(mapping_file_path)
  
      self.initialized = True
  
imports: import abc
  import importlib.util
  import logging
  import os
  import time
  import torch
  from pkg_resources import packaging
  from ts.handler_utils.timer import timed
  from ..utils.util import check_valid_pt2_backend, list_classes_from_module, load_label_mapping
label_desc: Use image node_id 2 for calling the BaseHandler obj's underlying member method code with example usage: obj.initialize(context) without return types