class_docstr: Base class to help with generalized loading of pretrained weights.
  
  Subclasses should override `_from_config()` if you want to instantiate them with
  `from_pretrained_module()`.
class_docstr_tok: ["Base","class","to","help","with","generalized","loading","of","pretrained","weights",".","Subclasses","should","override","`","_from_config","(",")","`","if","you","want","to","instantiate","them","with","`","from_pretrained_module","(",")","`","."]
docstr: Returns the relevant part of the `state_dict`.
docstr_tok: ["Returns","the","relevant","part","of","the","`","state_dict","`","."]
code: def _get_relevant_submodule_state(
      cls,
      state_dict: StateDictType,
      relevant_module: Optional[Union[str, List[str]]] = None,
  ) -> StateDictType:
      """
      Returns the relevant part of the `state_dict`.
      """
      relevant_modules: Optional[List[str]] = None
      if relevant_module:
          relevant_modules = (
              [relevant_module]
              if isinstance(relevant_module, str)
              else relevant_module
          )
      elif isinstance(cls._pretrained_relevant_module, str):
          relevant_modules = [cls._pretrained_relevant_module]
      elif isinstance(cls._pretrained_relevant_module, list):
          relevant_modules = cls._pretrained_relevant_module
  
      if relevant_modules:
          found = False
          for module_name in relevant_modules:
              relevant_keys = set(
                  [
                      key
                      for key in state_dict.keys()
                      if key.startswith(module_name + ".")
                  ]
              )
              if relevant_keys:
                  # Only keep elements of state dict that correspond to the relevant module.
                  state_dict = {
                      key.replace(module_name + ".", "", 1): value
                      for key, value in state_dict.items()
                      if key in relevant_keys
                  }
                  found = True
                  break
  
          if not found:
              warnings.warn(
                  f"{relevant_modules} was not found at top level of state_dict!",
                  UserWarning,
              )
  
      return state_dict
  
imports: import logging
  import os
  from os import PathLike
  from typing import TYPE_CHECKING, Optional, Dict, Union, List, Any, TypeVar, Type
  import re
  import warnings
  import torch
  import torch.distributed
  from allennlp.common.util import is_distributed, is_global_primary
  from allennlp.nn.parallel import ShardedModuleMixin
  from allennlp.nn.module import Module
  from allennlp.nn.util import StateDictType, read_state_dict, _check_incompatible_keys
label_desc: Use image node_id 3 for calling the TransformerModule obj's underlying member method code with example usage: obj._get_relevant_submodule_state(cls, state_dict, relevant_module) and returns: state_dict