docstr: Restores model instance (weights and configuration) into .nemo file
  
  Args:
      restore_path: path to .nemo file from which model should be instantiated
      override_config_path: path to a yaml config that will override the internal
          config file or an OmegaConf / DictConfig object representing the model config.
      map_location: Optional torch.device() to map the instantiated model to a device.
          By default (None), it will select a GPU if available, falling back to CPU otherwise.
      strict: Passed to load_state_dict. By default True
      return_config: If set to true, will return just the underlying config of the restored
          model as an OmegaConf DictConfig object without instantiating the model.
  
  Example:
      ```
      model = nemo.collections.asr.models.EncDecCTCModel.restore_from('asr.nemo')
      assert isinstance(model, nemo.collections.asr.models.EncDecCTCModel)
      ```
  
  Returns:
      An instance of type cls or its underlying config (if return_config is set).
code: def load_config_and_state_dict(
      self,
      calling_cls,
      restore_path: str,
      override_config_path: Optional[Union[OmegaConf, str]] = None,
      map_location: Optional[torch.device] = None,
      strict: bool = True,
      return_config: bool = False,
      trainer: Trainer = None,
  ):
      """
      Restores model instance (weights and configuration) into .nemo file
  
      Args:
          restore_path: path to .nemo file from which model should be instantiated
          override_config_path: path to a yaml config that will override the internal
              config file or an OmegaConf / DictConfig object representing the model config.
          map_location: Optional torch.device() to map the instantiated model to a device.
              By default (None), it will select a GPU if available, falling back to CPU otherwise.
          strict: Passed to load_state_dict. By default True
          return_config: If set to true, will return just the underlying config of the restored
              model as an OmegaConf DictConfig object without instantiating the model.
  
      Example:
          ```
          model = nemo.collections.asr.models.EncDecCTCModel.restore_from('asr.nemo')
          assert isinstance(model, nemo.collections.asr.models.EncDecCTCModel)
          ```
  
      Returns:
          An instance of type cls or its underlying config (if return_config is set).
      """
      # Get path where the command is executed - the artifacts will be "retrieved" there
      # (original .nemo behavior)
      cwd = os.getcwd()
  
      if map_location is None:
          if torch.cuda.is_available():
              map_location = torch.device("cuda")
          else:
              map_location = torch.device("cpu")
  
      app_state = AppState()
      with tempfile.TemporaryDirectory() as tmpdir:
          try:
              # Check if self.model_extracted_dir is set, and is a valid path
              if (
                  self.model_extracted_dir is not None
                  and os.path.isdir(self.model_extracted_dir)
              ):
                  # Log that NeMo will use the provided `model_extracted_dir`
                  logging.info(
                      f"Restoration will occur within pre-extracted directory : "
                      f"`{self.model_extracted_dir}`."
                  )
  
                  # Override `tmpdir` above with the pre-extracted `model_extracted_dir`
                  tmpdir = self.model_extracted_dir
  
              else:
                  # Extract the nemo file into the temporary directory
                  self._unpack_nemo_file(
                      path2file=restore_path,
                      out_folder=tmpdir,
                      extract_config_only=return_config is True,
                  )
  
              # Change current working directory to
              os.chdir(tmpdir)
              if override_config_path is None:
                  config_yaml = self.model_config_yaml
              else:
                  # can be str path or OmegaConf / DictConfig object
                  config_yaml = override_config_path
              if not isinstance(config_yaml, (OmegaConf, DictConfig)):
                  conf = OmegaConf.load(config_yaml)
              else:
                  conf = config_yaml
                  if override_config_path is not None:
                      # Resolve the override config
                      conf = OmegaConf.to_container(conf, resolve=True)
                      conf = OmegaConf.create(conf)
              # If override is top level config, extract just `model` from it
              if "model" in conf:
                  conf = conf.model
  
              if return_config:
                  instance = conf
                  return instance
              else:
                  if (
                      app_state.model_parallel_size is not None
                      and app_state.model_parallel_size > 1
                  ):
                      model_weights = (
                          self._inject_model_parallel_rank_for_ckpt(
                              tmpdir, self.model_weights_ckpt
                          )
                      )
                  else:
                      model_weights = os.path.join(
                          tmpdir, self.model_weights_ckpt
                      )
              OmegaConf.set_struct(conf, True)
              os.chdir(cwd)
              # get the class
              calling_cls._set_model_restore_state(
                  is_being_restored=True, folder=tmpdir
              )
              instance = calling_cls.from_config_dict(
                  config=conf, trainer=trainer
              )
              instance = instance.to(map_location)
              # add load_state_dict override
              if (
                  app_state.model_parallel_size is not None
                  and app_state.model_parallel_size > 1
              ):
                  model_weights = (
                      self._inject_model_parallel_rank_for_ckpt(
                          tmpdir, self.model_weights_ckpt
                      )
                  )
              state_dict = self._load_state_dict_from_disk(
                  model_weights, map_location=map_location
              )
          finally:
              os.chdir(cwd)
  
      return (conf, instance, state_dict)
  
imports: from __future__ import annotations
  import os
  import shutil
  import tarfile
  import tempfile
  import uuid
  from typing import Optional, Set, Union
  import torch
  from omegaconf import DictConfig, OmegaConf
  from omegaconf.omegaconf import open_dict
  from pytorch_lightning.trainer.trainer import Trainer
  from nemo.core import classes
  from nemo.utils import logging, model_utils
  from nemo.utils.app_state import AppState
  from nemo.utils.get_rank import is_global_rank_zero
  from nemo.utils.model_utils import inject_model_parallel_rank
label_desc: Use image node_id 3 for calling the SaveRestoreConnector obj's underlying member method code with example usage: obj.load_config_and_state_dict(calling_cls, restore_path, override_config_path, map_location, strict, return_config, trainer) and returns: conf,  instance,  state_dict, instance