fairseq: Dataclass error while importing Fairseq in Python 3.11

🐛 Bug

FaValueError: mutable default <class ‘fairseq.dataclass.configs.CommonConfig’> for field common is not allowed: use default_factory

To Reproduce

Steps to reproduce the behavior (always include the command you ran): 1, pip install fairseq==0.12.2 2. from fairseq import *

Trace:

Traceback (most recent call last): File “<stdin>”, line 1, in <module> File “/home/a1bharat/fairseq/fairseq/init.py”, line 20, in <module> from fairseq.distributed import utils as distributed_utils File “/home/a1bharat/fairseq/fairseq/distributed/init.py”, line 7, in <module> from .legacy_distributed_data_parallel import LegacyDistributedDataParallel File “/home/a1bharat/fairseq/fairseq/distributed/legacy_distributed_data_parallel.py”, line 23, in <module> from fairseq.distributed import utils File “/home/a1bharat/fairseq/fairseq/distributed/utils.py”, line 22, in <module> from fairseq.dataclass.configs import DistributedTrainingConfig, FairseqConfig File “/home/a1bharat/fairseq/fairseq/dataclass/init.py”, line 6, in <module> from .configs import FairseqDataclass File “/home/a1bharat/fairseq/fairseq/dataclass/configs.py”, line 906, in <module> @dataclass ^^^^^^^^^ File “/usr/lib/python3.11/dataclasses.py”, line 1220, in dataclass return wrap(cls) ^^^^^^^^^ File “/usr/lib/python3.11/dataclasses.py”, line 1210, in wrap return _process_class(cls, init, repr, eq, order, unsafe_hash, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “/usr/lib/python3.11/dataclasses.py”, line 958, in _process_class cls_fields.append(_get_field(cls, name, type, kw_only)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File “/usr/lib/python3.11/dataclasses.py”, line 815, in _get_field raise ValueError(f’mutable default {type(f.default)} for field ’ ValueError: mutable default <class ‘fairseq.dataclass.configs.CommonConfig’> for field common is not allowed: use default_factory

Code sample

Environment

  • fairseq Version ( main):
  • PyTorch Version (e.g., 1.0): 1.13.1
  • OS (e.g., Linux): Ubuntu
  • How you installed fairseq (pip, source): pip install successful
  • Build command you used (if compiling from source): git clone followed by pip install ./
  • Python version: 3.11
  • CUDA/cuDNN version: 11.6
  • GPU models and configuration: NVIDIA
  • Any other relevant information:

Additional context

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 22
  • Comments: 23

Commits related to this issue

Most upvoted comments

The official fix is too slow, I made a fix version myself. You can install it using the following command. v0.12.3. I conducted a simple test and found no abnormalities.

pip install git+https://github.com/One-sixth/fairseq.git

I have this same issue running Python 3.11.3, and the config.py modifications suggested did not resolve the issue. Looks like the issues migrated to the hydra module, based on the traceback. Please advise. Traceback below.

fairseq_error_traceback

This issue still exists on the official release. I’m using Python 3.11.

I have encountered these problems, and here is my solution: Windows11 environment

pip install git+https://github.com/liyaodev/fairseq.git

or, download fairseq-0.12.3.1-cp311-cp311-linux_x86_64.whl

pip install./fairseq-0.12.3.1-cp311-cp311-linux_x86_64.whl

WSL2(Ubuntu22.04) environment(my local use)

pip install git+https://github.com/liyaodev/fairseq.git

or, download fairseq-0.12.3.1-cp311-cp311-linux_x86_64.whl

pip install./fairseq-0.12.3.1-cp311-cp311-linux_x86_64.whl

there’s a little tip, best use hydra-core==1.3.2 omegaconf==2.3.0

I created another conda environment with python 3.9, and didn’t face with this error.

Same error with Python 3.11.6 😥

@EmreOzkose Yes, Pytorch2.0 supports Python 3.11

Alternately, you can use my fork of fairseq which supports Python 3.11, Knowledge Distillation, Adapters a few more interesting fixes.

getting the same ValeError iwth Python 3.11.4. Any suggestions?

Experiencing this with 3.11.2.

https://github.com/facebookresearch/fairseq/blob/176cd934982212a4f75e0669ee81b834ee71dbb0/fairseq/dataclass/configs.py#L1129

I assume it wants something like this:

@dataclass
class FairseqConfig(FairseqDataclass):
-     common: CommonConfig = CommonConfig()
+     common: CommonConfig = field(default_factory=CommonConfig)