keras icon indicating copy to clipboard operation
keras copied to clipboard

Keras Import Error with torch backend in Keras 3.0

Open surajpandey353 opened this issue 2 years ago • 7 comments

Hi,

I was using previously keras_core. After it got merged with keras 3.0, I am getting an import error while importing keras with torch as keras backend. The torch version I am using is 1.13.0

Error: `

import os os.environ["KERAS_BACKEND"] = "torch" import keras Traceback (most recent call last): File "", line 1, in File "anaconda3/envs/e2enc/lib/python3.9/site-packages/keras/init.py", line 8, in from keras import _tf_keras File "anaconda3/envs/e2enc/lib/python3.9/site-packages/keras/_tf_keras/init.py", line 1, in from keras._tf_keras import keras File "anaconda3/envs/e2enc/lib/python3.9/site-packages/keras/_tf_keras/keras/init.py", line 8, in from keras import activations File "anaconda3/envs/e2enc/lib/python3.9/site-packages/keras/activations/init.py", line 8, in from keras.src.activations import deserialize File "anaconda3/envs/e2enc/lib/python3.9/site-packages/keras/src/init.py", line 1, in from keras.src import activations File "anaconda3/envs/e2enc/lib/python3.9/site-packages/keras/src/activations/init.py", line 3, in from keras.src.activations.activations import elu File "anaconda3/envs/e2enc/lib/python3.9/site-packages/keras/src/activations/activations.py", line 1, in from keras.src import backend File "anaconda3/envs/e2enc/lib/python3.9/site-packages/keras/src/backend/init.py", line 37, in from keras.src.backend.torch import * # noqa: F403 File "anaconda3/envs/e2enc/lib/python3.9/site-packages/keras/src/backend/torch/init.py", line 22, in from keras.src.backend.torch import random File "anaconda3/envs/e2enc/lib/python3.9/site-packages/keras/src/backend/torch/random.py", line 2, in import torch._dynamo as dynamo ModuleNotFoundError: No module named 'torch._dynamo'`

surajpandey353 avatar Dec 01 '23 09:12 surajpandey353

ModuleNotFoundError: No module named 'torch._dynamo'`

This module is only present in the latest PyTorch version, I believe.

@kiukchung @haifeng-jin could we make sure to avoid importing _dynamo if the module doesn't exist, as many people are still on PyTorch 1.13.0?

fchollet avatar Dec 01 '23 17:12 fchollet

@surajpandey353 how hard would it be to upgrade to torch-2.x?

@fchollet we could add a is_dynamo_available() function and use it to guard imports and functionalities that would trigger dynamo (e.g. setting jit_compile=True with the pytorch backend). We'd still want to decide up to which torch version we want to support since it'll be good to run keras unittests with the major versions of pytorch we "officially" support.

kiukchung avatar Dec 01 '23 21:12 kiukchung

I don't think this is the only issue for torch 1.x. Anything related to torch.compile in the codebase is only in torch 2.x.

Do we want to fully support PyTorch 1?

haifeng-jin avatar Dec 01 '23 22:12 haifeng-jin

We'd still want to decide up to which torch version we want to support since it'll be good to run keras unittests with the major versions of pytorch we "officially" support.

So is it not worth supporting 1.13? That is fine as well.

fchollet avatar Dec 02 '23 01:12 fchollet

@surajpandey353 how hard would it be to upgrade to torch-2.x?

Switching to Pytorch-2x solves this issue. But, just to be clear there is not support for Pytorch 1x versions as of now.

surajpandey353 avatar Dec 07 '23 09:12 surajpandey353

@surajpandey353 , Here is the compatibility matrix for pyTorch and Keras 3 image

sachinprasadhs avatar Dec 07 '23 22:12 sachinprasadhs

This issue is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you.

github-actions[bot] avatar Dec 22 '23 01:12 github-actions[bot]

This issue was closed because it has been inactive for 28 days. Please reopen if you'd like to work on this further.

github-actions[bot] avatar Apr 26 '24 01:04 github-actions[bot]

Are you satisfied with the resolution of your issue? Yes No

google-ml-butler[bot] avatar Apr 26 '24 01:04 google-ml-butler[bot]