code: def load(
      name: str,
      model: torch.nn.Module | None = None,
      version: str | None = None,
      workflow_type: str = "train",
      model_file: str | None = None,
      load_ts_module: bool = False,
      bundle_dir: PathLike | None = None,
      source: str = DEFAULT_DOWNLOAD_SOURCE,
      repo: str | None = None,
      remove_prefix: str | None = "monai_",
      progress: bool = True,
      device: str | None = None,
      key_in_ckpt: str | None = None,
      config_files: Sequence[str] = (),
      workflow_name: str | BundleWorkflow | None = None,
      args_file: str | None = None,
      copy_model_args: dict | None = None,
      return_state_dict: bool = True,
      net_override: dict | None = None,
      net_name: str | None = None,
      **net_kwargs: Any,
  ) -> object | tuple[torch.nn.Module, dict, dict] | Any:
      """
      Load model weights or TorchScript module of a bundle.
  
      Args:
          name: bundle name. If `None` and `url` is `None`, it must be provided in `args_file`.
              for example:
              "spleen_ct_segmentation", "prostate_mri_anatomy" in model-zoo:
              https://github.com/Project-MONAI/model-zoo/releases/tag/hosting_storage_v1.
              "monai_brats_mri_segmentation" in ngc:
              https://catalog.ngc.nvidia.com/models?filters=&orderBy=scoreDESC&query=monai.
              "mednist_gan" in monaihosting:
              https://api.ngc.nvidia.com/v2/models/nvidia/monaihosting/mednist_gan/versions/0.2.0/files/mednist_gan_v0.2.0.zip
          model: a pytorch module to be updated. Default to None, using the "network_def" in the bundle.
          version: version name of the target bundle to download, like: "0.1.0". If `None`, will download
              the latest version. If `source` is "huggingface_hub", this argument is a Git revision id.
          workflow_type: specifies the workflow type: "train" or "training" for a training workflow,
              or "infer", "inference", "eval", "evaluation" for a inference workflow,
              other unsupported string will raise a ValueError.
              default to `train` for training workflow.
          model_file: the relative path of the model weights or TorchScript module within bundle.
              If `None`, "models/model.pt" or "models/model.ts" will be used.
          load_ts_module: a flag to specify if loading the TorchScript module.
          bundle_dir: directory the weights/TorchScript module will be loaded from.
              Default is `bundle` subfolder under `torch.hub.get_dir()`.
          source: storage location name. This argument is used when `model_file` is not existing locally and need to be
              downloaded first.
              In default, the value is achieved from the environment variable BUNDLE_DOWNLOAD_SRC, and
              it should be "ngc", "monaihosting", "github", or "huggingface_hub".
          repo: repo name. This argument is used when `url` is `None` and `source` is "github" or "huggingface_hub".
              If `source` is "github", it should be in the form of "repo_owner/repo_name/release_tag".
              If `source` is "huggingface_hub", it should be in the form of "repo_owner/repo_name".
          remove_prefix: This argument is used when `source` is "ngc". Currently, all ngc bundles
              have the ``monai_`` prefix, which is not existing in their model zoo contrasts. In order to
              maintain the consistency between these three sources, remove prefix is necessary.
              Therefore, if specified, downloaded folder name will remove the prefix.
          progress: whether to display a progress bar when downloading.
          device: target device of returned weights or module, if `None`, prefer to "cuda" if existing.
          key_in_ckpt: for nested checkpoint like `{"model": XXX, "optimizer": XXX, ...}`, specify the key of model
              weights. if not nested checkpoint, no need to set.
          config_files: extra filenames would be loaded. The argument only works when loading a TorchScript module,
              see `_extra_files` in `torch.jit.load` for more details.
          workflow_name: specified bundle workflow name, should be a string or class, default to "ConfigWorkflow".
          args_file: a JSON or YAML file to provide default values for all the args in "download" function.
          copy_model_args: other arguments for the `monai.networks.copy_model_state` function.
          return_state_dict: whether to return state dict, if True, return state_dict, else a corresponding network
              from `_workflow.network_def` will be instantiated and load the achieved weights.
          net_override: id-value pairs to override the parameters in the network of the bundle, default to `None`.
          net_name: if not `None`, a corresponding network will be instantiated and load the achieved weights.
              This argument only works when loading weights.
          net_kwargs: other arguments that are used to instantiate the network class defined by `net_name`.
  
      Returns:
          1. If `load_ts_module` is `False` and `model` is `None`,
              return model weights if can't find "network_def" in the bundle,
              else return an instantiated network that loaded the weights.
          2. If `load_ts_module` is `False` and `model` is not `None`,
              return an instantiated network that loaded the weights.
          3. If `load_ts_module` is `True`, return a triple that include a TorchScript module,
              the corresponding metadata dict, and extra files dict.
              please check `monai.data.load_net_with_metadata` for more details.
          4. If `return_state_dict` is True, return model weights, only used for compatibility
              when `model` and `net_name` are all `None`.
  
      """
      if return_state_dict and (
          model is not None or net_name is not None
      ):
          warnings.warn(
              "Incompatible values: model and net_name are all specified, return state dict instead."
          )
  
      bundle_dir_ = _process_bundle_dir(bundle_dir)
      net_override = {} if net_override is None else net_override
      copy_model_args = (
          {} if copy_model_args is None else copy_model_args
      )
  
      if device is None:
          device = "cuda:0" if is_available() else "cpu"
      if model_file is None:
          model_file = os.path.join(
              "models",
              "model.ts" if load_ts_module is True else "model.pt",
          )
      if source == "ngc":
          name = _add_ngc_prefix(name)
          if remove_prefix:
              name = _remove_ngc_prefix(name, prefix=remove_prefix)
      full_path = os.path.join(bundle_dir_, name, model_file)
      if not os.path.exists(full_path):
          download(
              name=name,
              version=version,
              bundle_dir=bundle_dir_,
              source=source,
              repo=repo,
              remove_prefix=remove_prefix,
              progress=progress,
              args_file=args_file,
          )
  
      # loading with `torch.jit.load`
      if load_ts_module is True:
          return load_net_with_metadata(
              full_path,
              map_location=torch.device(device),
              more_extra_files=config_files,
          )
      # loading with `torch.load`
      model_dict = torch.load(
          full_path, map_location=torch.device(device)
      )
  
      if not isinstance(model_dict, Mapping):
          warnings.warn(
              f"the state dictionary from {full_path} should be a dictionary but got {type(model_dict)}."
          )
          model_dict = get_state_dict(model_dict)
  
      if return_state_dict:
          return model_dict
  
      _workflow = None
      if model is None and net_name is None:
          bundle_config_file = (
              bundle_dir_ / name / "configs" / f"{workflow_type}.json"
          )
          if bundle_config_file.is_file():
              _net_override = {
                  f"network_def#{key}": value
                  for key, value in net_override.items()
              }
              _workflow = create_workflow(
                  workflow_name=workflow_name,
                  args_file=args_file,
                  config_file=str(bundle_config_file),
                  workflow_type=workflow_type,
                  **_net_override,
              )
          else:
              warnings.warn(
                  f"Cannot find the config file: {bundle_config_file}, return state dict instead."
              )
              return model_dict
          if _workflow is not None:
              if not hasattr(_workflow, "network_def"):
                  warnings.warn(
                      "No available network definition in the bundle, return state dict instead."
                  )
                  return model_dict
              else:
                  model = _workflow.network_def
      elif net_name is not None:
          net_kwargs["_target_"] = net_name
          configer = ConfigComponent(config=net_kwargs)
          model = configer.instantiate()  # type: ignore
  
      model.to(device)  # type: ignore
  
      copy_model_state(
          dst=model, src=model_dict if key_in_ckpt is None else model_dict[key_in_ckpt], **copy_model_args  # type: ignore
      )
  
      return model
  
imports: from __future__ import annotations
  import ast
  import json
  import os
  import re
  import warnings
  from collections.abc import Mapping, Sequence
  from pathlib import Path
  from pydoc import locate
  from shutil import copyfile
  from textwrap import dedent
  from typing import Any, Callable
  import torch
  from torch.cuda import is_available
  from monai.apps.mmars.mmars import _get_all_ngc_models
  from monai.apps.utils import _basename, download_url, extractall, get_logger
  from monai.bundle.config_item import ConfigComponent
  from monai.bundle.config_parser import ConfigParser
  from monai.bundle.utils import DEFAULT_INFERENCE, DEFAULT_METADATA
  from monai.bundle.workflows import BundleWorkflow, ConfigWorkflow
  from monai.config import IgniteInfo, PathLike
  from monai.data import load_net_with_metadata, save_net_with_metadata
  from monai.networks import convert_to_onnx, convert_to_torchscript, convert_to_trt, copy_model_state, get_state_dict, save_state
  from monai.utils import check_parent_dir, deprecated_arg, ensure_tuple, get_equivalent_dtype, min_version, optional_import, pprint_edges
label_desc: Use image node_id 18 for calling a global function with example usage: load(name, model, version, workflow_type, model_file, load_ts_module, bundle_dir, source, repo, remove_prefix, progress, device, key_in_ckpt, config_files, workflow_name, args_file, copy_model_args, return_state_dict, net_override, net_name) and returns: model, load_net_with_metadata, model_dict, model_dict, model_dict