abertsch72/unlimiformer

ImportError: cannot import name 'Unlimiformer' from 'unlimiformer'

yungsinatra0 opened this issue · 18 comments

Hi, I've been trying to run inference using the model checkpoints on HF by following the code in inference-example.py.

I'm using Google Colab and I'm cloning the whole repository. However, when I try to run the code, I get the following error:

---------------------------------------------------------------------------

ImportError                               Traceback (most recent call last)

[<ipython-input-9-ea5aa8573cba>](https://localhost:8080/#) in <cell line: 1>()
----> 1 from unlimiformer import Unlimiformer
      2 from random_training_unlimiformer import RandomTrainingUnlimiformer
      3 from usage import UnlimiformerArguments, training_addin
      4 
      5 from transformers import BartForConditionalGeneration, AutoTokenizer

ImportError: cannot import name 'Unlimiformer' from 'unlimiformer' (unknown location)


---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.

To view examples of installing some common dependencies, click the
"Open Examples" button below.
---------------------------------------------------------------------------

I've tried multiple things but nothing seems to work:

  • Restarting runtime
  • Deleting runtime & creating a new one
  • Adding the directory to PATH
  • Changing to directory using %cd /content/unlimiformer/src
  • Moving the files to /content/

I'm not sure what I'm missing here. It seems to be such a minor issue but I'm out here pulling my hair out trying to figure why it isn't working. If someone can help me out I'd really appreciate it. Thanks!

You need the unlimiformer.py file (src/unlimiformer.py) to be in the same folder as your project (or have it installed in your Python env) for it to be able to import it.

What do you mean by having it in the same folder as my project?

Hi @yungsinatra0 ,
Thank you for your interest in our work!

Does it work if you cd src?

Uri

Hi @urialon!

No, it doesn't. I've tried cd src, and also using os.chdir() and both do not seem to solve the issue.

What if you cp src/* . ?

@urialon I have done that as well.

from unlimiformer import Unlimiformer

This line in the code:
from unlimiformer import Unlimiformer
Means Python looks for, in this case, a file called unlimiformer.py, in the same folder your script (not you yourself) is in.
As long as this file exists in the same folder as your script, and hasn't been modified, it should resolve this specific error.

@SharkWipf I'm running this in Google Colab (jupyter notebook), so in this case the script is in a cell that is being executed. Could this cause the issue?

I'm not too familiar with Colab/Jupyter so I can't say too much about that. But all the same, if you either ship unlimiformer.py with your project in the same folder, or copy it to the same folder as your script in the Jupyter notebook, it should work.

@SharkWipf that's the problem, technically it should be working, but for some reason it isn't, and I'm not sure why.

image

Hi @yungsinatra0 ,

Is this a problem only with this project, or does it happen if you create any python file and try to import classes from it?

@urialon I haven't tried creating a python file. I wanted to avoid that as I wanted to be able to see the code I'm running in each cell, but I'll give this option a try.

You can also just copy the entire content of the unlimiformer.py file into a new cell and run it

Ah, I think that's the problem, since you're running from what I assume is an "interactive" shell, cwd is not necessarily the import path.
Looking closer at your original error message, it says (unknown location) too.

Could you post the output of

import sys
print(sys.path)

? It should tell you exactly where Python is looking when importing packages I believe.

EDIT: Locally ran, interactive vs script:
image

@SharkWipf I did try that previously, here's the output from sys.path:

['/content', '/env/python', '/usr/lib/python310.zip', '/usr/lib/python3.10', '/usr/lib/python3.10/lib-dynload', '', '/usr/local/lib/python3.10/dist-packages', '/usr/lib/python3/dist-packages', '/usr/local/lib/python3.10/dist-packages/IPython/extensions', '/root/.ipython', '/content/unlimiformer/src/unlimiformer/src', '/content/unlimiformer/src/unlimiformer/src', '/content/unlimiformer/src']

(please ignore the weird paths, I'm not sure how I added them there)

I tried adding both /content/unlimiformer/src and /content/unlimiformer (you don't see the 2nd cause I deleted & restarted the runtime) and I'm still getting the same error.

Hmmm. I tried making a minimal Jupyter playbook on Colab, and it seems to import Unlimiformer fine when I run it (but then fails on the other modules, because I didn't install anything else)

image

@urialon @SharkWipf I've just tried it again and it works now... this is very weird. I hope I'm not going to have this issue anymore! Thanks for helping me out!

Great, let us know if you have any more questions @yungsinatra0 !

@SharkWipf - thank you for your help and involvement!