class_docstr: This lobe enables the integration of fairseq pretrained wav2vec2.0 models.
  
  Source paper: https://arxiv.org/abs/2006.11477
  FairSeq >= 0.10.0 needs to be installed:
  https://fairseq.readthedocs.io/en/latest/
  
  The model can be used as a fixed features extractor or can be finetuned. It
  will download automatically the model if a url is given (e.g FairSeq
  repository from GitHub).
  
  Arguments
  ---------
  pretrained_path : str
      Path of the pretrained wav2vec2 model. It can be a url or a local path.
  save_path : str
      Path and filename of the downloaded model.
  input_norm : bool (default: None)
      If True, a layer_norm (affine) will be applied to the input waveform.
      By default, it is extracted from the checkpoint of the downloaded model
      in order to match the pretraining conditions. However, if this information
      is not given in the checkpoint, it has to be given manually.
  output_norm : bool (default: True)
      If True, a layer_norm (affine) will be applied to the output obtained
      from the wav2vec model.
  freeze : bool (default: True)
      If True, the model is frozen. If False, the model will be trained
      alongside with the rest of the pipeline.
  pretrain : bool (default: True)
      If True, the model is pretrained with the specified source.
      If False, the randomly-initialized model is instantiated.
  dropout : float (default: None)
      If different from None (0.0 to 1.0), it will override the given fairseq
      dropout rates. This is useful if the wav2vec2 model has been trained
      without dropout and one wants to reactivate it for downstream task
      fine-tuning (better performance observed).
  layer_drop : float (default: None)
      If different from None (0.0 to 1.0), it will override the given fairseq
      layer_drop rate. This is useful if the wav2vec2 model has been trained
      without layer_drop and one wants to reactivate it for downstream task
      fine-tuning.
  
  Example
  -------
  >>> inputs = torch.rand([10, 600])
  >>> model_url = "https://dl.fbaipublicfiles.com/fairseq/wav2vec/wav2vec_small.pt"
  >>> save_path = "models_checkpoints/wav2vec2.pt"
  >>> model = FairseqWav2Vec2(model_url, save_path)
  >>> outputs = model(inputs)
  >>> outputs.shape
  torch.Size([10, 100,  768])
class_docstr_tok: ["This","lobe","enables","the","integration","of","fairseq","pretrained","wav2vec2.0","models",".","Source","paper",":","https",":","\/\/arxiv.org\/abs\/2006.11477","FairSeq",">","=","0.10.0","needs","to","be","installed",":","https",":","\/\/fairseq.readthedocs.io\/en\/latest\/","The","model","can","be","used","as","a","fixed","features","extractor","or","can","be","finetuned",".","It","will","download","automatically","the","model","if","a","url","is","given","(","e.g","FairSeq","repository","from","GitHub",")",".","Arguments","--","--","--","--","-","pretrained_path",":","str","Path","of","the","pretrained","wav2vec2","model",".","It","can","be","a","url","or","a","local","path",".","save_path",":","str","Path","and","filename","of","the","downloaded","model",".","input_norm",":","bool","(","default",":","None",")","If","True",",","a","layer_norm","(","affine",")","will","be","applied","to","the","input","waveform",".","By","default",",","it","is","extracted","from","the","checkpoint","of","the","downloaded","model","in","order","to","match","the","pretraining","conditions",".","However",",","if","this","information","is","not","given","in","the","checkpoint",",","it","has","to","be","given","manually",".","output_norm",":","bool","(","default",":","True",")","If","True",",","a","layer_norm","(","affine",")","will","be","applied","to","the","output","obtained","from","the","wav2vec","model",".","freeze",":","bool","(","default",":","True",")","If","True",",","the","model","is","frozen",".","If","False",",","the","model","will","be","trained","alongside","with","the","rest","of","the","pipeline",".","pretrain",":","bool","(","default",":","True",")","If","True",",","the","model","is","pretrained","with","the","specified","source",".","If","False",",","the","randomly-initialized","model","is","instantiated",".","dropout",":","float","(","default",":","None",")","If","different","from","None","(","0.0","to","1.0",")",",","it","will","override","the","given","fairseq","dropout","rates",".","This","is","useful","if","the","wav2vec2","model","has","been","trained","without","dropout","and","one","wants","to","reactivate","it","for","downstream","task","fine-tuning","(","better","performance","observed",")",".","layer_drop",":","float","(","default",":","None",")","If","different","from","None","(","0.0","to","1.0",")",",","it","will","override","the","given","fairseq","layer_drop","rate",".","This","is","useful","if","the","wav2vec2","model","has","been","trained","without","layer_drop","and","one","wants","to","reactivate","it","for","downstream","task","fine-tuning",".","Example","--","--","--","-",">",">",">","inputs","=","torch.rand","(","[","10",",","600","]",")",">",">",">","model_url","=","``","https",":","\/\/dl.fbaipublicfiles.com\/fairseq\/wav2vec\/wav2vec_small.pt","''",">",">",">","save_path","=","``","models_checkpoints\/wav2vec2.pt","''",">",">",">","model","=","FairseqWav2Vec2","(","model_url",",","save_path",")",">",">",">","outputs","=","model","(","inputs",")",">",">",">","outputs.shape","torch.Size","(","[","10",",","100",",","768","]",")"]
code: def __init__(
      self,
      pretrained_path,
      save_path,
      input_norm=None,
      output_norm=False,
      freeze=False,
      freeze_feature_extractor=False,
      pretrain=True,
      dropout=None,
      layer_drop=None,
  ):
      super().__init__()
  
      # Download the pretrained wav2vec2 model. It can be local or online.
      download_file(pretrained_path, save_path)
  
      # During pretraining dropout might be set to 0. However, we might want
      # to apply dropout when fine-tuning on a downstream task. Hence we need
      # to modify the fairseq cfg to activate dropout (if requested).
      overrides = {}
      if not freeze and dropout is not None:
          overrides["model"] = {}
          if dropout is not None:
              overrides["model"]["dropout"] = dropout
              overrides["model"]["dropout_input"] = dropout
              overrides["model"]["attention_dropout"] = dropout
          if layer_drop is not None:
              overrides["model"]["layer_drop"] = layer_drop
  
      (
          model,
          cfg,
          task,
      ) = fairseq.checkpoint_utils.load_model_ensemble_and_task(
          [save_path], arg_overrides=overrides
      )
  
      # wav2vec pretrained models may need the input waveform to be normalized
      # Hence, we check if the model has be trained with or without it.
      # If the information isn't contained in the checkpoint IT HAS TO BE GIVEN
      # BY THE USER.
      if input_norm is None:
          if hasattr(cfg["task"], "normalize"):
              self.normalize = cfg["task"].normalize
          elif hasattr(cfg, "normalize"):
              self.normalize = cfg.normalize
          else:
              self.normalize = False
      else:
          self.normalize = input_norm
  
      model = model[0]
      self.model = model
      self.freeze = freeze
      self.output_norm = output_norm
      self.freeze_feature_extractor = freeze_feature_extractor
  
      if self.freeze:
          logger.warning(
              "speechbrain.lobes.models.fairseq_wav2vec - wav2vec 2.0 is frozen."
          )
          self.model.eval()
          # Freeze parameters
          for param in self.model.parameters():
              param.requires_grad = False
      else:
          self.model.train()
          if self.freeze_feature_extractor:
              logger.warning(
                  "speechbrain.lobes.models.fairseq_wav2vec - wav2vec 2.0 feature extractor is frozen."
              )
              self.model.feature_extractor.eval()
              for param in self.model.feature_extractor.parameters():
                  param.requires_grad = False
  
      # Randomly initialized layers if pretrain is False
      if not (pretrain):
          self.reset_layer(self.model)
  
      # Following the fairseq implementation of downstream training,
      # we remove some modules that are unnecessary.
      self.remove_pretraining_modules()
  
imports: import torch
  import logging
  import torch.nn.functional
  from torch import nn
  from speechbrain.utils.data_utils import download_file
  from speechbrain.dataio.dataio import length_to_mask
label_desc: Use image node_id 1 to create a new FairseqWav2Vec2 object from inherited base classes: nn with example: obj = FairseqWav2Vec2(pretrained_path, save_path, input_norm, output_norm, freeze, freeze_feature_extractor, pretrain, dropout, layer_drop)