OFA icon indicating copy to clipboard operation
OFA copied to clipboard

Huggingface transformers inference: ModuleNotFoundError: No module named 'generate'

Open steve-marmalade opened this issue 1 year ago • 10 comments

When running the imports listed in transformers.md:

from PIL import Image
from torchvision import transforms
from transformers import OFATokenizer, OFAModel
from generate import sequence_generator

I get ModuleNotFoundError: No module named 'generate'

Where is generate supposed to come from? The implementations of sequence_generator.SequenceGenerator that I see in e.g. fairseq also don't have the same signature, so it's not clear how to proceed.

steve-marmalade avatar Aug 11 '22 22:08 steve-marmalade

Did you clone the latest code of the branch feature/add_transformers? Cuz in the new code we have added a directory called generate with sequence_generator inside.

JustinLin610 avatar Aug 12 '22 02:08 JustinLin610

I'll be really embarrassed if I've made a silly mistake here, but I don't see a directory called generate in that branch:

image

steve-marmalade avatar Aug 12 '22 02:08 steve-marmalade

Here: https://github.com/OFA-Sys/OFA/tree/feature/add_transformers/transformers/src/transformers/models/ofa/generate

faychu avatar Aug 12 '22 02:08 faychu

Excellent, thank you both for the quick help. I never would have found it otherwise.

I have gotten the import to work by adding /path/to/OFA/transformers/src/transformers/models/ofa to my python path.

Perhaps we should either:

  • add a sentence to transformers.md mentioning to add this directory to the python path
  • or add an __init__.py in the generate/ directory and update the import to be from transformers.models.ofa.generate import sequence_generator

What do you think?

steve-marmalade avatar Aug 12 '22 03:08 steve-marmalade

Good suggestion! Thanks for helping us improve our repo. I'll update the information to make things clear!

JustinLin610 avatar Aug 12 '22 03:08 JustinLin610

Hi! I'm still having this issue, even though I've downloaded the repo using git clone --single-branch --branch feature/add_transformers https://github.com/OFA-Sys/OFA.git and also have transformers/src/transformers/models/ofa/generate. I've followed the steps listed in transformers.md, but am still receiving the error:

    from generate import sequence_generator
ModuleNotFoundError: No module named 'generate'

@Steve-marmalade Could you share the steps you took to solve this issue?

Sultanax avatar Aug 23 '22 04:08 Sultanax

Hi @Sultanax, sure. I did two things:

  1. Added an empty __init__.py file in transformers/src/transformers/models/ofa/generate/__init__.py
  2. Changed the import to be from transformers.models.ofa.generate import sequence_generator

Note: you will need to pip install OFA/transformers/ again

steve-marmalade avatar Aug 23 '22 14:08 steve-marmalade

Thanks for the suggestions! I've completed those steps, but using from transformers.models.ofa.generate import sequence_generator still generates the same error:

    from transformers.models.ofa.generate import sequence_generator
ModuleNotFoundError: No module named 'transformers.models.ofa.generate'

I think the problem I'm mainly having is that when I run pip install OFA/transformers/ in the command line, I get the following error:

Hint: It looks like a path. File 'OFA/transformers/' does not exist.

I already have Huggingface transformers installed (version 4.21.1). I'm not sure exactly how to work around this. @JustinLin610

Sultanax avatar Aug 23 '22 17:08 Sultanax

Sorry for the problem pretty much. If you would like to directly import generate, you should cd to the directory of modeling_ofa.py. A better solution should be the previously mentioned one with __init__.py and reinstallation.

Seemingly your installation is not successful. How about cd into transformers with a setup.py, and uninstall your old transformers and then run pip install . to install ours?

JustinLin610 avatar Sep 01 '22 07:09 JustinLin610

Hi @Sultanax, sure. I did two things:

  1. Added an empty __init__.py file in transformers/src/transformers/models/ofa/generate/__init__.py
  2. Changed the import to be from transformers.models.ofa.generate import sequence_generator

Note: you will need to pip install OFA/transformers/ again

I have added the empty __init__.py to the directory generate and it all works now. If you guys are interested, you can try this notebook (link) and see if there are still any problems.

JustinLin610 avatar Sep 14 '22 03:09 JustinLin610