microsoft / LMOps

General technology for enabling AI capabilities w/ LLMs and MLLMs
https://aka.ms/GeneralAI
MIT License
3.73k stars 283 forks source link

Running structured prompting with Fairseq fails to start due to import error #4

Closed imrehg closed 1 year ago

imrehg commented 1 year ago

Hey, I'm following the guide in the readme to run the manyshots structured prompting example, but it doesn't seem to start up properly with an import error within fairseq.

I'm running in a local (rather than docker) environment, the relevant packages set up with conda, tried it with Python 3.8.15 an 3.9.15.

This is the error message I get:

Traceback (most recent call last):
  File "validate.py", line 9, in <module>
    import struprompting
  File "/project/gergely/LMOps/structured_prompting/fairseq-version/struprompting/__init__.py", line 1, in <module>
    import struprompting.models
  File "/project/gergely/LMOps/structured_prompting/fairseq-version/struprompting/models/__init__.py", line 3, in <module>
    from fairseq.models import import_models
  File "/project/gergely/LMOps/structured_prompting/fairseq-version/fairseq/fairseq/models/__init__.py", line 235, in <module>
    import_models(models_dir, "fairseq.models")
  File "/project/gergely/LMOps/structured_prompting/fairseq-version/fairseq/fairseq/models/__init__.py", line 217, in import_models
    importlib.import_module(namespace + "." + model_name)
  File "/opt/anaconda/envs/fairseq/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/project/gergely/LMOps/structured_prompting/fairseq-version/fairseq/fairseq/models/hubert/__init__.py", line 6, in <module>
    from .hubert import *  # noqa
  File "/project/gergely/LMOps/structured_prompting/fairseq-version/fairseq/fairseq/models/hubert/hubert.py", line 20, in <module>
    from fairseq.models.wav2vec.wav2vec2 import (
  File "/project/gergely/LMOps/structured_prompting/fairseq-version/fairseq/fairseq/models/wav2vec/__init__.py", line 6, in <module>
    from .wav2vec import *  # noqa
  File "/project/gergely/LMOps/structured_prompting/fairseq-version/fairseq/fairseq/models/wav2vec/wav2vec.py", line 25, in <module>
    from fairseq.tasks import FairseqTask
  File "/project/gergely/LMOps/structured_prompting/fairseq-version/fairseq/fairseq/tasks/__init__.py", line 15, in <module>
    from .fairseq_task import FairseqTask, LegacyFairseqTask  # noqa
  File "/project/gergely/LMOps/structured_prompting/fairseq-version/fairseq/fairseq/tasks/fairseq_task.py", line 13, in <module>
    from fairseq import metrics, search, tokenizer, utils
ImportError: cannot import name 'metrics' from 'fairseq' (unknown location)

Looks like this happens because when running the script, Python is trying to load the requested fairseq lib from the local folder within that folder where the whole example is (structured_prompting/fairseq-version/), and not using the version that was installed (with the README's pip install --user -e fairseq/ line). That is, the installed library and the local folder are clashing during script import time.

The script also cannot be run from another folder, as it wants to use the validate.py file,and thus the above clash cannot be resolved by just moving into another folder.

I managed to make this work by renaming the fairseq folder and reinstalling the module from the new, non-clashing name. Would this be the ultimate solution? Or am I missing something / doing something incorrectly so this import confusion happens?

YRdddream commented 1 year ago

Hi, it is recommended to run the script in the docker environment. Maybe you have installed other versions of fairseq before, which causes the conflict. I will check it soon.