class_docstr: FairSeq Wav2Vec2 encoder module.
  
  Args:
      input_size: input dim
      output_size: dimension of attention
      w2v_url: url to Wav2Vec2.0 pretrained model
      w2v_dir_path: directory to download the Wav2Vec2.0 pretrained model.
      normalize_before: whether to use layer_norm before the first block
      finetune_last_n_layers: last n layers to be finetuned in Wav2Vec2.0
                              0 means to finetune every layer if freeze_w2v=False.
class_docstr_tok: ["FairSeq","Wav2Vec2","encoder","module",".","Args",":","input_size",":","input","dim","output_size",":","dimension","of","attention","w2v_url",":","url","to","Wav2Vec2.0","pretrained","model","w2v_dir_path",":","directory","to","download","the","Wav2Vec2.0","pretrained","model",".","normalize_before",":","whether","to","use","layer_norm","before","the","first","block","finetune_last_n_layers",":","last","n","layers","to","be","finetuned","in","Wav2Vec2.0","0","means","to","finetune","every","layer","if","freeze_w2v=False","."]
code: def __init__(
      self,
      input_size: int,
      w2v_url: str,
      w2v_dir_path: str = "./",
      output_size: int = 256,
      normalize_before: bool = False,
      freeze_finetune_updates: int = 0,
  ):
      assert check_argument_types()
      super().__init__()
  
      if w2v_url != "":
          try:
              import fairseq
              from fairseq.models.wav2vec.wav2vec2 import Wav2Vec2Model
          except Exception as e:
              print("Error: FairSeq is not properly installed.")
              print(
                  "Please install FairSeq: cd ${MAIN_ROOT}/tools && make fairseq.done"
              )
              raise e
  
      self.w2v_model_path = download_w2v(w2v_url, w2v_dir_path)
  
      self._output_size = output_size
  
      (
          models,
          _,
          _,
      ) = fairseq.checkpoint_utils.load_model_ensemble_and_task(
          [self.w2v_model_path],
          arg_overrides={"data": w2v_dir_path},
      )
      model = models[0]
  
      if not isinstance(model, Wav2Vec2Model):
          try:
              model = model.w2v_encoder.w2v_model
          except Exception as e:
              print(
                  "Error: pretrained models should be within: "
                  "'Wav2Vec2Model, Wav2VecCTC' classes, etc."
              )
              raise e
  
      self.encoders = model
  
      self.pretrained_params = copy.deepcopy(model.state_dict())
  
      self.normalize_before = normalize_before
      if self.normalize_before:
          self.after_norm = LayerNorm(output_size)
  
      if model.cfg.encoder_embed_dim != output_size:
          # TODO(xkc09): try LSTM
          self.output_layer = torch.nn.Sequential(
              torch.nn.Linear(model.cfg.encoder_embed_dim, output_size),
          )
      else:
          self.output_layer = None
  
      self.freeze_finetune_updates = freeze_finetune_updates
      self.register_buffer("num_updates", torch.LongTensor([0]))
  
file_path: espnet/espnet2/asr/encoder/wav2vec2_encoder.py
imports: import contextlib
  import copy
  import logging
  import os
  from typing import Optional, Tuple
  import torch
  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 FairSeqWav2Vec2Encoder object from inherited base classes: AbsEncoder with example: obj = FairSeqWav2Vec2Encoder(input_size, w2v_url, w2v_dir_path, output_size, normalize_before, freeze_finetune_updates)