google-research / timesfm

TimesFM (Time Series Foundation Model) is a pretrained time-series foundation model developed by Google Research for time-series forecasting.
https://research.google/blog/a-decoder-only-foundation-model-for-time-series-forecasting/
Apache License 2.0
3.03k stars 227 forks source link

ImportError: cannot import name 'patched_decoder' from 'src' #13

Closed fahdmirza closed 1 month ago

fahdmirza commented 1 month ago

Hi, I am trying to get it installed on Ubuntu 22.04 and followed the steps to create conda environment in GPU, installed all the pre-reqs and then when trying to import the timesfm, it gives following error:

(tfm_env) ubuntu@ip-10-218-241-180:~/timesfm$ ls LICENSE init.py docs environment_cpu.yml pyproject.toml README.md datasets environment.yml experiments src (tfm_env) ubuntu@ip-10-218-241-180:~/timesfm$ ls -ltr total 52 -rw-rw-r-- 1 ubuntu ubuntu 231 May 11 06:28 environment_cpu.yml -rw-rw-r-- 1 ubuntu ubuntu 318 May 11 06:28 environment.yml drwxrwxr-x 2 ubuntu ubuntu 4096 May 11 06:28 docs drwxrwxr-x 2 ubuntu ubuntu 4096 May 11 06:28 datasets -rw-rw-r-- 1 ubuntu ubuntu 763 May 11 06:28 init.py -rw-rw-r-- 1 ubuntu ubuntu 6207 May 11 06:28 README.md -rw-rw-r-- 1 ubuntu ubuntu 11358 May 11 06:28 LICENSE -rw-rw-r-- 1 ubuntu ubuntu 696 May 11 06:28 pyproject.toml drwxrwxr-x 4 ubuntu ubuntu 4096 May 11 06:28 experiments drwxrwxr-x 4 ubuntu ubuntu 4096 May 11 06:33 src (tfm_env) ubuntu@ip-10-218-241-180:~/timesfm$ python Python 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:45:18) [GCC 12.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.

import timesfm Traceback (most recent call last): File "", line 1, in File "/home/ubuntu/timesfm/src/timesfm.py", line 38, in from src import patched_decoder ImportError: cannot import name 'patched_decoder' from 'src' (/home/ubuntu/.local/lib/python3.10/site-packages/src/init.py)

theafien commented 1 month ago

simple:

$ cd /timesfm/src/
$ python
>>> import timesfm
...

Or

cd /timesfm/
pip install -e .
fahdmirza commented 1 month ago

simple:

$ cd /timesfm/src/
$ python
>>> import timesfm
...

Or

cd /timesfm/
pip install -e .

Yes tried that already. Same error.

R3xpook commented 1 month ago

same exact issue here

nxfi777 commented 1 month ago

You must run the python file with the conda environment. You cannot import timesfm elsewhere because it is only installed to the conda environment.

conda activate tfm_env

fahdmirza commented 1 month ago

You must run the python file with the conda environment. You cannot import timesfm elsewhere because it is only installed to the conda environment.

conda activate tfm_env

The above issue is happening in conda too and without conda too.

nxfi777 commented 1 month ago

Okay I see, I had the same issue. I believe it goes away when you append the timesfm folder directly to the python code and run with conda. Add the following lines:

import sys
sys.path.append('/home/kasm-user/Documents/timesfm') # replace with path to timesfm dir
fahdmirza commented 1 month ago

Okay I see, I had the same issue. I believe it goes away when you append the timesfm folder directly to the python code and run with conda. Add the following lines:

import sys
sys.path.append('/home/kasm-user/Documents/timesfm') # The clone dir

(tfm_env) ubuntu@ip-10-218-241-180:~/timesfm$ python3 Python 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:45:18) [GCC 12.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.

import sys sys.path.append('/home/ubuntu/timesfm') import timesfm Traceback (most recent call last): File "", line 1, in File "/home/ubuntu/timesfm/src/timesfm.py", line 38, in from src import patched_decoder ImportError: cannot import name 'patched_decoder' from 'src' (/home/ubuntu/.local/lib/python3.10/site-packages/src/init.py)

KeyboardInterrupt

Still doesnt work.

fahdmirza commented 1 month ago

Sorry which first Python file?

nxfi777 commented 1 month ago

Sorry which first Python file?

remove what I said from timesfm.py. You should be importing timesfm into a python script outside of the folder. If you get the patched_decoder error, add the lines I said to the start.

R3xpook commented 1 month ago

For me I solve this error by just running the py file from shell Spyder and other IDE gives problems didn't try any other IDE still pretty weird

fahdmirza commented 1 month ago

Still doesn't work. I think I will wait for a stable and proper version

nxfi777 commented 1 month ago

Still doesn't work. I think I will wait for a stable and proper version

I think I found a fix-all. Navigate to the timesfm directory, then to the src folder and edit the timesfm.py code. Inject the following lines:

import sys
sys.path.append(path.dirname(path.abspath(__file__)))

So the final import structure for patched_decoder should look like this:

from praxis.layers import transformers

import sys
sys.path.append(path.dirname(path.abspath(__file__)))

import patched_decoder
SpirosM10 commented 1 month ago

from . import patched_decoder

This fixed it

fahdmirza commented 1 month ago

No it doesn't fix it. Still same error. This model should never have been released in this buggy state. At least do the basic testing before releasing it. Very disappointing. Not wasting any more time on this.

theafien commented 1 month ago

No it doesn't fix it. Still same error. This model should never have been released in this buggy state. At least do the basic testing before releasing it. Very disappointing. Not wasting any more time on this.

The model works for me. You probably don't know how to import the package correctly.

fahdmirza commented 1 month ago

No it doesn't fix it. Still same error. This model should never have been released in this buggy state. At least do the basic testing before releasing it. Very disappointing. Not wasting any more time on this.

The model works for me. You probably don't know how to import the package correctly.

No it doesn't work, and I know how to import stuff. Are you one of the developer? Anyway instead of judging me, would have been better if you have shown your whole code here which 'works'.

nxfi777 commented 1 month ago

Still doesn't work. I think I will wait for a stable and proper version

I think I found a fix-all. Navigate to the timesfm directory, then to the src folder and edit the timesfm.py code. Inject the following lines:


import sys

sys.path.append(path.dirname(path.abspath(__file__)))

So the final import structure for patched_decoder should look like this:


from praxis.layers import transformers

import sys

sys.path.append(path.dirname(path.abspath(__file__)))

import patched_decoder

This fixes the issue. Tested on multiple environments.