Repository: facebookresearch/fairseq

Issue:
Wav2Vec Pretrain error [TypeError: ConformerEncoder.build_encoder_layer() takes 2 positional arguments but 3 were given]
```
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/hydra/_internal/utils.py", line 198, in run_and_report
    return func()
  File "/usr/local/lib/python3.10/dist-packages/hydra/_internal/utils.py", line 347, in <lambda>
    lambda: hydra.run(
  File "/usr/local/lib/python3.10/dist-packages/hydra/_internal/hydra.py", line 107, in run
    return run_job(
  File "/usr/local/lib/python3.10/dist-packages/hydra/core/utils.py", line 129, in run_job
    ret.return_value = task_function(task_cfg)
  File "/content/fairseq/fairseq_cli/hydra_train.py", line 27, in hydra_main
    _hydra_main(cfg)
  File "/content/fairseq/fairseq_cli/hydra_train.py", line 56, in _hydra_main
    distributed_utils.call_main(cfg, pre_main, **kwargs)
  File "/content/fairseq/fairseq/distributed/utils.py", line 404, in call_main
    main(cfg, **kwargs)
  File "/content/fairseq/fairseq_cli/train.py", line 96, in main
    model = task.build_model(cfg.model)
  File "/content/fairseq/fairseq/tasks/audio_pretraining.py", line 224, in build_model
    model = super().build_model(model_cfg, from_checkpoint)
  File "/content/fairseq/fairseq/tasks/fairseq_task.py", line 355, in build_model
    model = models.build_model(cfg, self, from_checkpoint)
  File "/content/fairseq/fairseq/models/__init__.py", line 106, in build_model
    return model.build_model(cfg, task)
  File "/content/fairseq/fairseq/models/wav2vec/wav2vec2.py", line 427, in build_model
    return cls(cfg)
  File "/content/fairseq/fairseq/models/wav2vec/wav2vec2.py", line 407, in __init__
    self.encoder = encoder_cls(cfg)
  File "/content/fairseq/fairseq/models/wav2vec/wav2vec2.py", line 1186, in __init__
    super().__init__(args)
  File "/content/fairseq/fairseq/models/wav2vec/wav2vec2.py", line 1053, in __init__
    [self.build_encoder_layer(args, ii) for ii in range(args.encoder_layers)]
  File "/content/fairseq/fairseq/models/wav2vec/wav2vec2.py", line 1053, in <listcomp>
    [self.build_encoder_layer(args, ii) for ii in range(args.encoder_layers)]
TypeError: ConformerEncoder.build_encoder_layer() takes 2 positional arguments but 3 were given
```
This is the config:

```
# @package _group_

common:
  fp16: true
  log_format: json
  log_interval: 200

checkpoint:
  save_interval_updates: 25000
  keep_interval_updates: 1
  no_epoch_checkpoints: true

task:
  _name: audio_pretraining
  data: ???
  max_sample_size: 250000
  min_sample_size: 32000
  normalize: true

dataset:
  num_workers: 6
  max_tokens: 1400000
  skip_invalid_size_inputs_valid_test: true

distributed_training:
  distributed_world_size: 1
  ddp_backend: legacy_ddp

criterion:
  _name: wav2vec
  infonce: true
  log_keys: ["prob_perplexity","code_perplexity","temp"]
  loss_weights: [0.1, 10]

optimization:
  max_update: 400000
  lr: [0.0005]

optimizer:
  _name: adam
  adam_betas: (0.9,0.98)
  adam_eps: 1e-06
  weight_decay: 0.01

lr_scheduler:
  _name: polynomial_decay
  warmup_updates: 32000

model:
  _name: wav2vec2
  quantize_targets: true
  final_dim: 256
  encoder_layerdrop: 0.05
  dropout_input: 0.1
  dropout_features: 0.1
  feature_grad_mult: 1.0
  encoder_embed_dim: 768
  layer_type: conformer
  attn_type: espnet
  pos_enc_type: rel_pos
```

I am trying to pretrain wav2vec2-conformer, earlier it worked fine, but now it's throwing this error.
Any suggestions here?

Thanks and Regards


Discussion:

Resolving patch:
diff --git a/fairseq/models/wav2vec/wav2vec2.py b/fairseq/models/wav2vec/wav2vec2.py
--- a/fairseq/models/wav2vec/wav2vec2.py
+++ b/fairseq/models/wav2vec/wav2vec2.py
@@ -942,7 +942,7 @@ def make_conv_pos(e, k, g):
 
 
 class TransformerEncoder(nn.Module):
-    def build_encoder_layer(self, args: Wav2Vec2Config, layer_idx: int):
+    def build_encoder_layer(self, args: Wav2Vec2Config, **kwargs):
         if args.layer_type == "transformer":
             layer = TransformerSentenceEncoderLayer(
                 embedding_dim=self.embedding_dim,
@@ -972,7 +972,7 @@ def build_encoder_layer(self, args: Wav2Vec2Config, layer_idx: int):
                 use_adp = True
             else:
                 adp_trf_idx = list(range(*[int(g) for g in args.adp_trf_idx.split(":")]))
-                if layer_idx in adp_trf_idx:
+                if kwargs.get("layer_idx", None) in adp_trf_idx:
                     use_adp = True
             if use_adp:
                 layer = TransformerSentenceEncoderWithAdapterLayer(
@@ -1050,7 +1050,7 @@ def make_conv_block(e, k, g, l):
             )
 
         self.layers = nn.ModuleList(
-            [self.build_encoder_layer(args, ii) for ii in range(args.encoder_layers)]
+            [self.build_encoder_layer(args, layer_idx=ii) for ii in range(args.encoder_layers)]
         )
         self.layer_norm_first = args.layer_norm_first
         self.layer_norm = LayerNorm(self.embedding_dim)


Test patch: