class_docstr: Hugging Face Transformers PostEncoder.
class_docstr_tok: ["Hugging","Face","Transformers","PostEncoder","."]
docstr: Initialize the module.
docstr_tok: ["Initialize","the","module","."]
code: def __init__(
      self,
      input_size: int,
      model_name_or_path: str,
      length_adaptor_n_layers: int = 0,
      lang_token_id: int = -1,
  ):
      """Initialize the module."""
      assert check_argument_types()
      super().__init__()
  
      if not is_transformers_available:
          raise ImportError(
              "`transformers` is not available. Please install it via `pip install"
              " transformers` or `cd /path/to/espnet/tools && . ./activate_python.sh"
              " && ./installers/install_transformers.sh`."
          )
  
      model = AutoModel.from_pretrained(model_name_or_path)
  
      if hasattr(model, "encoder"):
          self.transformer = model.encoder
      else:
          self.transformer = model
  
      self.lang_token_embed = None
  
      if hasattr(self.transformer, "embed_tokens"):
          if lang_token_id != -1:
              self.lang_token_embed = (
                  self.transformer.embed_tokens(
                      torch.tensor(lang_token_id)
                  )
                  .detach()
                  .cpu()
              )
          del self.transformer.embed_tokens
      if hasattr(self.transformer, "wte"):
          if lang_token_id != -1:
              self.lang_token_embed = (
                  self.transformer.wte(torch.tensor(lang_token_id))
                  .detach()
                  .cpu()
              )
          del self.transformer.wte
      if hasattr(self.transformer, "word_embedding"):
          if lang_token_id != -1:
              self.lang_token_embed = (
                  self.transformer.word_embedding(
                      torch.tensor(lang_token_id)
                  )
                  .detach()
                  .cpu()
              )
          del self.transformer.word_embedding
      if hasattr(model, "embeddings") and hasattr(
          model.embeddings, "word_embeddings"
      ):
          if lang_token_id != -1:
              self.lang_token_embed = (
                  model.embeddings.word_embeddings(
                      torch.tensor(lang_token_id)
                  )
                  .detach()
                  .cpu()
              )
  
      if self.lang_token_embed is not None and hasattr(
          self.transformer, "embed_scale"
      ):
          self.lang_token_embed *= self.transformer.embed_scale
  
      self.pretrained_params = copy.deepcopy(
          self.transformer.state_dict()
      )
  
      if (
          self.transformer.config.is_encoder_decoder
          or self.transformer.config.model_type in ["xlnet", "t5"]
      ):
          self.use_inputs_embeds = True
          self.extend_attention_mask = False
      elif self.transformer.config.model_type == "gpt2":
          self.use_inputs_embeds = True
          self.extend_attention_mask = True
      else:
          self.use_inputs_embeds = False
          self.extend_attention_mask = True
  
      self.linear_in = torch.nn.Linear(
          input_size, self.transformer.config.hidden_size
      )
  
      # Length Adaptor as in https://aclanthology.org/2021.acl-long.68.pdf
  
      if length_adaptor_n_layers > 0:
          length_adaptor_layers = []
          for _ in range(length_adaptor_n_layers):
              length_adaptor_layers.append(
                  torch.nn.Conv1d(input_size, input_size, 2, 2)
              )
              length_adaptor_layers.append(torch.nn.ReLU())
      else:
          length_adaptor_layers = [torch.nn.Identity()]
  
      self.length_adaptor = torch.nn.Sequential(*length_adaptor_layers)
      self.length_adaptor_ratio = 2**length_adaptor_n_layers
  
file_path: espnet/espnet2/asr/postencoder/hugging_face_transformers_postencoder.py
imports: import copy
  import logging
  from typing import Tuple
  import torch
  from typeguard import check_argument_types
  from espnet2.asr.postencoder.abs_postencoder import AbsPostEncoder
  from espnet.nets.pytorch_backend.nets_utils import make_pad_mask
  from espnet.nets.pytorch_backend.transformer.subsampling import TooShortUttError
label_desc: Use image node_id 1 to create a new HuggingFaceTransformersPostEncoder object from inherited base classes: AbsPostEncoder with example: obj = HuggingFaceTransformersPostEncoder(input_size, model_name_or_path, length_adaptor_n_layers, lang_token_id)