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: Initialize this module from a corresponding model on HuggingFace.
  
  !!! Note
      This method is only available for subclasses that implement `_from_config()`.
      Otherwise a `NotImplementedError` will be raised.
  
  # Parameters
  
  model_name : `str`
      The model identifier or path.
  
  load_weights : `bool`, optional (default = `True`)
      Whether to download and load the pretrained weights. If `False`, the
      weights are left uninitialized.
  
  weights_path : `Optional[Union[str, PathLike]]`, optional (default = `None`)
      When `load_weights` is `True`, this can be set to override the weights file.
      Otherwise the default weights from the pretrained model are used.
  
  auto_config_kwargs : `Optional[Dict[str, Any]]`, optional (default = `None`)
      Optional key-word arguments to pass to `transformers.AutoConfig.from_pretrained()`
      to load the pretrained model's configuration file.
  
  mapping : `Optional[Dict[str, str]]`, optional (default = `None`)
      Optional mapping that determines any differences in the submodule names
      between this module and the pretrained model from HuggingFace.
      If not given, the class's default is used: `cls._pretrained_mapping`.
  
  relevant_module : `Optional[str]`, optional (default = `None`)
      An optional submodule of the HuggingFace module to initialize weights from.
      This is only relevant when `load_weights` is `True`.
      If not given, the class's default is used: `cls._pretrained_relevant_module`.
  
  ignore : `Optional[List[str]]`, optional (default = `None`)
      An optional list of regular expressions that define which weights to ignore
      from a pretrained state_dict.
      This is only relevant when `load_weights` is `True`.
      If not specified, the class's default is used: `cls._pretrained_ignore`.
  
  allow_missing: `Optional[List[str]]`, optional (default = `None`)
      An optional list of regular expressions that specifies which weights are allowed to be missing
      from the pretrained state dictionary.
      This is only relevant when `load_weights` is `True`.
      If not specified, the class's default is used: `cls._pretrained_allow_missing`.
  
  strict : `bool`, optional (default = `True`)
      Whether to load the `state_dict` in "strict" model. This only applies
      when `load_weights` is `True`.
  
  **kwargs : `Any`
      Key word arguments to pass to `cls.from_config()` when instantiating the module.
docstr_tok: ["Initialize","this","module","from","a","corresponding","model","on","HuggingFace",".","!","!","!","Note","This","method","is","only","available","for","subclasses","that","implement","`","_from_config","(",")","`",".","Otherwise","a","`","NotImplementedError","`","will","be","raised",".","#","Parameters","model_name",":","`","str","`","The","model","identifier","or","path",".","load_weights",":","`","bool","`",",","optional","(","default","=","`","True","`",")","Whether","to","download","and","load","the","pretrained","weights",".","If","`","False","`",",","the","weights","are","left","uninitialized",".","weights_path",":","`","Optional","[","Union","[","str",",","PathLike","]","]","`",",","optional","(","default","=","`","None","`",")","When","`","load_weights","`","is","`","True","`",",","this","can","be","set","to","override","the","weights","file",".","Otherwise","the","default","weights","from","the","pretrained","model","are","used",".","auto_config_kwargs",":","`","Optional","[","Dict","[","str",",","Any","]","]","`",",","optional","(","default","=","`","None","`",")","Optional","key-word","arguments","to","pass","to","`","transformers.AutoConfig.from_pretrained","(",")","`","to","load","the","pretrained","model","'s","configuration","file",".","mapping",":","`","Optional","[","Dict","[","str",",","str","]","]","`",",","optional","(","default","=","`","None","`",")","Optional","mapping","that","determines","any","differences","in","the","submodule","names","between","this","module","and","the","pretrained","model","from","HuggingFace",".","If","not","given",",","the","class","'s","default","is","used",":","`","cls._pretrained_mapping","`",".","relevant_module",":","`","Optional","[","str","]","`",",","optional","(","default","=","`","None","`",")","An","optional","submodule","of","the","HuggingFace","module","to","initialize","weights","from",".","This","is","only","relevant","when","`","load_weights","`","is","`","True","`",".","If","not","given",",","the","class","'s","default","is","used",":","`","cls._pretrained_relevant_module","`",".","ignore",":","`","Optional","[","List","[","str","]","]","`",",","optional","(","default","=","`","None","`",")","An","optional","list","of","regular","expressions","that","define","which","weights","to","ignore","from","a","pretrained","state_dict",".","This","is","only","relevant","when","`","load_weights","`","is","`","True","`",".","If","not","specified",",","the","class","'s","default","is","used",":","`","cls._pretrained_ignore","`",".","allow_missing",":","`","Optional","[","List","[","str","]","]","`",",","optional","(","default","=","`","None","`",")","An","optional","list","of","regular","expressions","that","specifies","which","weights","are","allowed","to","be","missing","from","the","pretrained","state","dictionary",".","This","is","only","relevant","when","`","load_weights","`","is","`","True","`",".","If","not","specified",",","the","class","'s","default","is","used",":","`","cls._pretrained_allow_missing","`",".","strict",":","`","bool","`",",","optional","(","default","=","`","True","`",")","Whether","to","load","the","`","state_dict","`","in","``","strict","''","model",".","This","only","applies","when","`","load_weights","`","is","`","True","`",".","*","*","kwargs",":","`","Any","`","Key","word","arguments","to","pass","to","`","cls.from_config","(",")","`","when","instantiating","the","module","."]
code: def from_pretrained_module(
      cls: Type[_T],
      model_name: str,
      *,
      load_weights: bool = True,
      weights_path: Optional[Union[str, PathLike]] = None,
      auto_config_kwargs: Optional[Dict[str, Any]] = None,
      mapping: Optional[Dict[str, str]] = None,
      relevant_module: Optional[Union[str, List[str]]] = None,
      ignore: Optional[List[str]] = None,
      allow_missing: Optional[List[str]] = None,
      strict: bool = True,
      **kwargs,
  ) -> _T:
      """
      Initialize this module from a corresponding model on HuggingFace.
  
      !!! Note
          This method is only available for subclasses that implement `_from_config()`.
          Otherwise a `NotImplementedError` will be raised.
  
      # Parameters
  
      model_name : `str`
          The model identifier or path.
  
      load_weights : `bool`, optional (default = `True`)
          Whether to download and load the pretrained weights. If `False`, the
          weights are left uninitialized.
  
      weights_path : `Optional[Union[str, PathLike]]`, optional (default = `None`)
          When `load_weights` is `True`, this can be set to override the weights file.
          Otherwise the default weights from the pretrained model are used.
  
      auto_config_kwargs : `Optional[Dict[str, Any]]`, optional (default = `None`)
          Optional key-word arguments to pass to `transformers.AutoConfig.from_pretrained()`
          to load the pretrained model's configuration file.
  
      mapping : `Optional[Dict[str, str]]`, optional (default = `None`)
          Optional mapping that determines any differences in the submodule names
          between this module and the pretrained model from HuggingFace.
          If not given, the class's default is used: `cls._pretrained_mapping`.
  
      relevant_module : `Optional[str]`, optional (default = `None`)
          An optional submodule of the HuggingFace module to initialize weights from.
          This is only relevant when `load_weights` is `True`.
          If not given, the class's default is used: `cls._pretrained_relevant_module`.
  
      ignore : `Optional[List[str]]`, optional (default = `None`)
          An optional list of regular expressions that define which weights to ignore
          from a pretrained state_dict.
          This is only relevant when `load_weights` is `True`.
          If not specified, the class's default is used: `cls._pretrained_ignore`.
  
      allow_missing: `Optional[List[str]]`, optional (default = `None`)
          An optional list of regular expressions that specifies which weights are allowed to be missing
          from the pretrained state dictionary.
          This is only relevant when `load_weights` is `True`.
          If not specified, the class's default is used: `cls._pretrained_allow_missing`.
  
      strict : `bool`, optional (default = `True`)
          Whether to load the `state_dict` in "strict" model. This only applies
          when `load_weights` is `True`.
  
      **kwargs : `Any`
          Key word arguments to pass to `cls.from_config()` when instantiating the module.
      """  # noqa: E501
      from transformers import AutoConfig
  
      config = AutoConfig.from_pretrained(
          model_name, **(auto_config_kwargs or {})
      )
      model = cls._from_config(config, **kwargs)
  
      if load_weights:
          state_dict: Optional[StateDictType] = None
          if is_global_primary():
              # Load the pretrained HuggingFace state_dict.
              pretrained_state_dict = cls._get_pretrained_state_dict(
                  model_name,
                  weights_path=weights_path,
                  relevant_module=relevant_module,
                  ignore=ignore,
              )
              # Now map keys from the HuggingFace state_dict to the corresponding keys from
              # this class. This is called recursively on each submodule of the current module.
              state_dict = model._get_mapped_state_dict(
                  pretrained_state_dict, mapping=mapping
              )
  
          logger.info("Loading state_dict into module")
  
          missing_keys: List[str]
          unexpected_keys: List[str]
          if not is_distributed():
              assert state_dict is not None
              missing_keys, unexpected_keys = model.load_state_dict(
                  state_dict, strict=False
              )
          else:
              # We're in distributed training. `state_dict` is `None` for all process groups
              # except the global primary.
              # Syncronize here since non-primary process groups will have to wait for the primary
              # to load the state_dict into memory.
              dist.barrier()
              # Now load the state dict into the model.
              (
                  missing_keys,
                  unexpected_keys,
              ) = model.load_state_dict_distributed(
                  state_dict, strict=False
              )
  
          # Exclude any keys in `missing_keys` that match with the `allow_missing`
          # regular expressions.
          if allow_missing is None:
              allow_missing = cls._pretrained_allow_missing
          if allow_missing:
              missing_keys = [
                  k
                  for k in missing_keys
                  if not any(re.match(p, k) for p in allow_missing)
              ]
  
          _check_incompatible_keys(
              model, missing_keys, unexpected_keys, strict
          )
  
      return model
  
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 6 for calling the TransformerModule obj's underlying member method code with example usage: obj.from_pretrained_module(cls, model_name) and returns: model