abertsch72 / unlimiformer

Public repo for the NeurIPS 2023 paper "Unlimiformer: Long-Range Transformers with Unlimited Length Input"
MIT License
1.05k stars 77 forks source link

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

Closed yungsinatra0 closed 1 year ago

yungsinatra0 commented 1 year ago

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:

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!

SharkWipf commented 1 year ago

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.

yungsinatra0 commented 1 year ago

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

urialon commented 1 year ago

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

Does it work if you cd src?

Uri

yungsinatra0 commented 1 year ago

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.

urialon commented 1 year ago

What if you cp src/* . ?

yungsinatra0 commented 1 year ago

@urialon I have done that as well.

SharkWipf commented 1 year ago

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.

yungsinatra0 commented 1 year ago

@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?

SharkWipf commented 1 year ago

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.

yungsinatra0 commented 1 year ago

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

image

urialon commented 1 year ago

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?

yungsinatra0 commented 1 year ago

@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.

urialon commented 1 year ago

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

SharkWipf commented 1 year ago

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

yungsinatra0 commented 1 year ago

@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.

SharkWipf commented 1 year ago

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

yungsinatra0 commented 1 year ago

@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!

urialon commented 1 year ago

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

@SharkWipf - thank you for your help and involvement!