fairseq icon indicating copy to clipboard operation
fairseq copied to clipboard

Dataclass error while importing Fairseq in Python 3.11

Open usaraj opened this issue 2 years ago • 39 comments

🐛 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 "", line 1, in File "/home/a1bharat/fairseq/fairseq/init.py", line 20, in from fairseq.distributed import utils as distributed_utils File "/home/a1bharat/fairseq/fairseq/distributed/init.py", line 7, in from .legacy_distributed_data_parallel import LegacyDistributedDataParallel File "/home/a1bharat/fairseq/fairseq/distributed/legacy_distributed_data_parallel.py", line 23, in from fairseq.distributed import utils File "/home/a1bharat/fairseq/fairseq/distributed/utils.py", line 22, in from fairseq.dataclass.configs import DistributedTrainingConfig, FairseqConfig File "/home/a1bharat/fairseq/fairseq/dataclass/init.py", line 6, in from .configs import FairseqDataclass File "/home/a1bharat/fairseq/fairseq/dataclass/configs.py", line 906, in @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

usaraj avatar Mar 07 '23 05:03 usaraj

Is Pytorch supporting python 3.11?

VarunGumma avatar Mar 13 '23 06:03 VarunGumma

Hi, is there any progress on this issue?

EmreOzkose avatar Mar 22 '23 13:03 EmreOzkose

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

EmreOzkose avatar Mar 22 '23 13:03 EmreOzkose

@EmreOzkose Yes, Pytorch2.0 supports Python 3.11

VarunGumma avatar Mar 22 '23 13:03 VarunGumma

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)

Birch-san avatar Apr 02 '23 00:04 Birch-san

same problem

conan1024hao avatar Apr 07 '23 01:04 conan1024hao

Experiencing this with 3.11.2.

ashesfall avatar Apr 09 '23 16:04 ashesfall

I am also facing the same issue. any way to resolve this?

Muskan19577 avatar May 01 '23 14:05 Muskan19577

@Muskan19577 I had to move back to python 3.9

ashesfall avatar May 01 '23 15:05 ashesfall

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

thyripian avatar Jun 05 '23 00:06 thyripian

getting the same ValeError iwth Python 3.11.4. Any suggestions?

bit-scientist avatar Aug 09 '23 02:08 bit-scientist

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

One-sixth avatar Aug 11 '23 21:08 One-sixth

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

kabyanil avatar Sep 26 '23 10:09 kabyanil

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

Same error with Python 3.11.5

zhiqu22 avatar Oct 05 '23 05:10 zhiqu22

Same error with Python 3.11

MrGutarLed avatar Oct 08 '23 20:10 MrGutarLed

Same error with Python 3.11.6 😥

JingThree3 avatar Oct 18 '23 11:10 JingThree3

The changes to dataclasses are an official change that was introduced in python 3.11, meaning all 3.11.x and higher won't work. This will need to get changed in fairseq's codebase, and isnt dependent on the exact python 3.11.x version

aikow avatar Oct 18 '23 15:10 aikow

Python 3.11.4 (main, Jul  5 2023, 14:15:25) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import fairseq
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/users/nus/zyh/anaconda3/lib/python3.11/site-packages/fairseq/__init__.py", line 20, in <module>
    from fairseq.distributed import utils as distributed_utils
  File "/home/users/nus/zyh/anaconda3/lib/python3.11/site-packages/fairseq/distributed/__init__.py", line 7, in <module>
    from .fully_sharded_data_parallel import (
  File "/home/users/nus/zyh/anaconda3/lib/python3.11/site-packages/fairseq/distributed/fully_sharded_data_parallel.py", line 10, in <module>
    from fairseq.dataclass.configs import DistributedTrainingConfig
  File "/home/users/nus/zyh/anaconda3/lib/python3.11/site-packages/fairseq/dataclass/__init__.py", line 6, in <module>
    from .configs import FairseqDataclass
  File "/home/users/nus/zyh/anaconda3/lib/python3.11/site-packages/fairseq/dataclass/configs.py", line 1104, in <module>
    @dataclass
     ^^^^^^^^^
  File "/home/users/nus/zyh/anaconda3/lib/python3.11/dataclasses.py", line 1230, in dataclass
    return wrap(cls)
           ^^^^^^^^^
  File "/home/users/nus/zyh/anaconda3/lib/python3.11/dataclasses.py", line 1220, in wrap
    return _process_class(cls, init, repr, eq, order, unsafe_hash,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/users/nus/zyh/anaconda3/lib/python3.11/dataclasses.py", line 958, in _process_class
    cls_fields.append(_get_field(cls, name, type, kw_only))
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/users/nus/zyh/anaconda3/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

Y-H-Joe avatar Nov 24 '23 08:11 Y-H-Joe

I'm confused. Why is this still not fixed? Will it break backwards compatibility?

CircuitCM avatar Dec 15 '23 13:12 CircuitCM

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

liyaodev avatar Jan 10 '24 04:01 liyaodev

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

VarunGumma avatar Jan 10 '24 04:01 VarunGumma

!!! this issue needs attention.

Can't use python 3.11.2,

ValueError: mutable default <class 'fairseq.dataclass.configs.CommonConfig'> for field common is not allowed: use default_factory

vxnuaj avatar Feb 18 '24 02:02 vxnuaj

This same error is being output with Python 3.12.0.

4ka0 avatar Feb 23 '24 01:02 4ka0

Figured since it's April now, I'd throw in my two cents. Still not working.

Fmstrat avatar Apr 13 '24 14:04 Fmstrat

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)

Thanks, it work for me. I change all the error place like your suggestion.

yjyuanjin avatar Apr 15 '24 11:04 yjyuanjin

@liyaodev, could you please tell me what the exact name of the package you mentioned would be for Windows?

last-fisherman avatar Apr 15 '24 16:04 last-fisherman

@liyaodev, could you please tell me what the exact name of the package you mentioned would be for Windows?

Windows 11 Professional

liyaodev avatar Apr 16 '24 06:04 liyaodev

Not working in CentOS, Python 3.11

snoop2head avatar Apr 26 '24 23:04 snoop2head

So uh, still not working in Python 3.12 😅 Compile from source fixes tho

tavyscrolls avatar May 04 '24 20:05 tavyscrolls

problem still up and critical

ZeaMays14142 avatar May 22 '24 20:05 ZeaMays14142