class_docstr: FairSeq Hubert encoder module, used for loading pretrained weight and finetuning
  
  Args:
      input_size: input dim
      hubert_url: url to Hubert pretrained model
      hubert_dir_path: directory to download the Wav2Vec2.0 pretrained model.
      output_size: dimension of attention
      normalize_before: whether to use layer_norm before the first block
      freeze_finetune_updates: steps that freeze all layers except output layer
          before tuning the whole model (nessasary to prevent overfit).
      dropout_rate: dropout rate
      activation_dropout: dropout rate in activation function
      attention_dropout: dropout rate in attention
  Hubert specific Args:
      Please refer to:
      https://github.com/pytorch/fairseq/blob/master/fairseq/models/hubert/hubert.py
code: def __init__(
      self,
      input_size: int,
      hubert_url: str = "./",
      hubert_dir_path: str = "./",
      output_size: int = 256,
      normalize_before: bool = False,
      freeze_finetune_updates: int = 0,
      dropout_rate: float = 0.0,
      activation_dropout: float = 0.1,
      attention_dropout: float = 0.0,
      mask_length: int = 10,
      mask_prob: float = 0.75,
      mask_selection: str = "static",
      mask_other: int = 0,
      apply_mask: bool = True,
      mask_channel_length: int = 64,
      mask_channel_prob: float = 0.5,
      mask_channel_other: int = 0,
      mask_channel_selection: str = "static",
      layerdrop: float = 0.1,
      feature_grad_mult: float = 0.0,
  ):
      assert check_argument_types()
      super().__init__()
      self.apply_mask = apply_mask
      try:
          import fairseq
          from fairseq.models.hubert.hubert import HubertModel
      except Exception as e:
          print("Error: FairSeq is not properly installed.")
          print(
              "Please install FairSeq: cd ${MAIN_ROOT}/tools && make fairseq.done"
          )
          raise e
  
      arg_overrides = {
          "dropout": dropout_rate,
          "activation_dropout": activation_dropout,
          "attention_dropout": attention_dropout,
          "mask_length": mask_length,
          "mask_prob": mask_prob,
          "mask_selection": mask_selection,
          "mask_other": mask_other,
          "mask_channel_length": mask_channel_length,
          "mask_channel_prob": mask_channel_prob,
          "mask_channel_selection": mask_channel_selection,
          "mask_channel_other": mask_channel_other,
          "encoder_layerdrop": layerdrop,
          "feature_grad_mult": feature_grad_mult,
          "data": hubert_dir_path,
      }
  
      if hubert_url == "espnet":
          self.hubert_model_path = hubert_dir_path
          s = torch.load(
              self.hubert_model_path,
              map_location=torch.device("cpu"),
          )
  
          if all("encoder.encoder" in k for k in s):
              try:
                  state = {
                      k.replace("encoder.encoder.", ""): v
                      for k, v in s.items()
                      if "label_embs_concat" not in k
                  }
              except Exception as e:
                  raise e
  
          config_file = os.path.join(
              "/".join(self.hubert_model_path.split("/")[:-1]),
              "config.yaml",
          )
          config_file = Path(config_file)
  
          with config_file.open("r", encoding="utf-8") as f:
              self.pretrained_cfg = yaml.safe_load(f)
  
          model = FairseqHubertPretrainEncoder(
              input_size=self.pretrained_cfg["input_size"],
              hubert_dict=self.pretrained_cfg["hubert_dict"],
              **self.pretrained_cfg["encoder_conf"],
          )
          model = model.encoder
  
          d = self.pretrained_cfg["encoder_conf"]["output_size"]
          self.pretrained_params = copy.deepcopy(state)
  
      else:
          self.hubert_model_path = download_hubert(
              hubert_url, hubert_dir_path
          )
  
          (
              models,
              self.pretrained_cfg,
              task,
          ) = fairseq.checkpoint_utils.load_model_ensemble_and_task(
              [self.hubert_model_path],
              arg_overrides=arg_overrides,
              strict=False,
          )
          model = models[0]
  
          d = self.pretrained_cfg.model.encoder_embed_dim
          self.pretrained_params = copy.deepcopy(model.state_dict())
  
      self._output_size = output_size
  
      if not isinstance(model, HubertModel):
          try:
              model = model.hubert_encoder.hubert_model
          except Exception as e:
              print(
                  "Error: pretrained models should be within: "
                  "'HubertModel, Hubertctc' classes, etc."
              )
              raise e
  
      self.encoders = model
  
      self.normalize_before = normalize_before
      if self.normalize_before:
          self.after_norm = LayerNorm(output_size)
  
      if output_size and output_size != d:
          self.output_layer = torch.nn.Sequential(
              torch.nn.Linear(d, output_size),
          )
      else:
          self.output_layer = None
  
      self.freeze_finetune_updates = freeze_finetune_updates
      self.register_buffer("num_updates", torch.LongTensor([0]))
  
imports: import contextlib
  import copy
  import logging
  import os
  from pathlib import Path
  from typing import List, Optional, Tuple
  import torch
  import yaml
  from filelock import FileLock
  from typeguard import check_argument_types
  from espnet2.asr.encoder.abs_encoder import AbsEncoder
  from espnet.nets.pytorch_backend.nets_utils import make_pad_mask
  from espnet.nets.pytorch_backend.transformer.layer_norm import LayerNorm
label_desc: Use image node_id 1 to create a new FairseqHubertEncoder object from inherited base classes: AbsEncoder with example: obj = FairseqHubertEncoder(input_size, hubert_url, hubert_dir_path, output_size, normalize_before, freeze_finetune_updates, dropout_rate, activation_dropout, attention_dropout, mask_length, mask_prob, mask_selection, mask_other, apply_mask, mask_channel_length, mask_channel_prob, mask_channel_other, mask_channel_selection, layerdrop, feature_grad_mult)