docstr: Extract the state dict(s) from a provided .nemo tarfile and save it to a directory.
  
  Args:
      restore_path: path to .nemo file from which state dict(s) should be extracted
      save_dir: directory in which the saved state dict(s) should be stored
      split_by_module: bool flag, which determins whether the output checkpoint should
          be for the entire Model, or the individual module's that comprise the Model
  
  Example:
      To convert the .nemo tarfile into a single Model level PyTorch checkpoint
      ::
      state_dict = nemo.collections.asr.models.EncDecCTCModel.extract_state_dict_from('asr.nemo', './asr_ckpts')
  
  
      To restore a model from a Model level checkpoint
      ::
      model = nemo.collections.asr.models.EncDecCTCModel(cfg)  # or any other method of restoration
      model.load_state_dict(torch.load("./asr_ckpts/model_weights.ckpt"))
  
  
      To convert the .nemo tarfile into multiple Module level PyTorch checkpoints
      ::
      state_dict = nemo.collections.asr.models.EncDecCTCModel.extract_state_dict_from('asr.nemo', './asr_ckpts', split_by_module=True)
  
  
      To restore a module from a Module level checkpoint
      ::
      model = nemo.collections.asr.models.EncDecCTCModel(cfg)  # or any other method of restoration
  
      # load the individual components
      model.preprocessor.load_state_dict(torch.load("./asr_ckpts/preprocessor.ckpt"))
      model.encoder.load_state_dict(torch.load("./asr_ckpts/encoder.ckpt"))
      model.decoder.load_state_dict(torch.load("./asr_ckpts/decoder.ckpt"))
  
  
  Returns:
      The state dict that was loaded from the original .nemo checkpoint
code: def extract_state_dict_from(
      self,
      restore_path: str,
      save_dir: str,
      split_by_module: bool = False,
  ):
      """
      Extract the state dict(s) from a provided .nemo tarfile and save it to a directory.
  
      Args:
          restore_path: path to .nemo file from which state dict(s) should be extracted
          save_dir: directory in which the saved state dict(s) should be stored
          split_by_module: bool flag, which determins whether the output checkpoint should
              be for the entire Model, or the individual module's that comprise the Model
  
      Example:
          To convert the .nemo tarfile into a single Model level PyTorch checkpoint
          ::
          state_dict = nemo.collections.asr.models.EncDecCTCModel.extract_state_dict_from('asr.nemo', './asr_ckpts')
  
  
          To restore a model from a Model level checkpoint
          ::
          model = nemo.collections.asr.models.EncDecCTCModel(cfg)  # or any other method of restoration
          model.load_state_dict(torch.load("./asr_ckpts/model_weights.ckpt"))
  
  
          To convert the .nemo tarfile into multiple Module level PyTorch checkpoints
          ::
          state_dict = nemo.collections.asr.models.EncDecCTCModel.extract_state_dict_from('asr.nemo', './asr_ckpts', split_by_module=True)
  
  
          To restore a module from a Module level checkpoint
          ::
          model = nemo.collections.asr.models.EncDecCTCModel(cfg)  # or any other method of restoration
  
          # load the individual components
          model.preprocessor.load_state_dict(torch.load("./asr_ckpts/preprocessor.ckpt"))
          model.encoder.load_state_dict(torch.load("./asr_ckpts/encoder.ckpt"))
          model.decoder.load_state_dict(torch.load("./asr_ckpts/decoder.ckpt"))
  
  
      Returns:
          The state dict that was loaded from the original .nemo checkpoint
      """
  
      cwd = os.getcwd()
  
      save_dir = os.path.abspath(save_dir)
      if not os.path.exists(save_dir):
          os.makedirs(save_dir, exist_ok=True)
  
      with tempfile.TemporaryDirectory() as tmpdir:
          try:
              self._unpack_nemo_file(
                  path2file=restore_path, out_folder=tmpdir
              )
              os.chdir(tmpdir)
              model_weights = os.path.join(
                  tmpdir, self.model_weights_ckpt
              )
              state_dict = self._load_state_dict_from_disk(
                  model_weights
              )
  
              if not split_by_module:
                  filepath = os.path.join(
                      save_dir, self.model_weights_ckpt
                  )
                  self._save_state_dict_to_disk(state_dict, filepath)
  
              else:
                  key_set = set(
                      [key.split(".")[0] for key in state_dict.keys()]
                  )
                  for primary_key in key_set:
                      inner_keys = [
                          key
                          for key in state_dict.keys()
                          if key.split(".")[0] == primary_key
                      ]
                      state_dict_subset = {
                          ".".join(
                              inner_key.split(".")[1:]
                          ): state_dict[inner_key]
                          for inner_key in inner_keys
                      }
                      filepath = os.path.join(
                          save_dir, f"{primary_key}.ckpt"
                      )
                      self._save_state_dict_to_disk(
                          state_dict_subset, filepath
                      )
  
              logging.info(
                  f"Checkpoints from {restore_path} were successfully extracted into {save_dir}."
              )
          finally:
              os.chdir(cwd)
  
      return 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 7 for calling the SaveRestoreConnector obj's underlying member method code with example usage: obj.extract_state_dict_from(restore_path, save_dir, split_by_module) and returns: state_dict