Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed keras_nlp import #1172

Closed
hatdropper1977 opened this issue Jul 20, 2023 · 6 comments
Closed

Failed keras_nlp import #1172

hatdropper1977 opened this issue Jul 20, 2023 · 6 comments
Labels
type:Bug Something isn't working

Comments

@hatdropper1977
Copy link

Describe the bug
I want to import keras_nlp with tensorflow. I do not want to import MXNET.

When I import keras_nlp I get an error.

To Reproduce

Install keras-nlp and tensorflow

sh-4.2$ source anaconda3/bin/activate
(base) sh-4.2$ pip freeze | grep tensor
(base) sh-4.2$ pip freeze | grep keras
(base) sh-4.2$ pip install -U keras-nlp tensorflow
Looking in indexes: https://pypi.org/simple, https://pip.repos.neuron.amazonaws.com
Collecting keras-nlp
  Downloading keras_nlp-0.6.0-py3-none-any.whl (576 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 576.5/576.5 kB 10.0 MB/s eta 0:00:00
Collecting tensorflow
  Downloading tensorflow-2.13.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (524.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 524.1/524.1 MB 1.5 MB/s eta 0:00:00
Collecting keras-core (from keras-nlp)
  Downloading keras_core-0.1.2-py3-none-any.whl (753 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 753.1/753.1 kB 22.0 MB/s eta 0:00:00
Collecting absl-py (from keras-nlp)
  Downloading absl_py-1.4.0-py3-none-any.whl (126 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 126.5/126.5 kB 28.7 MB/s eta 0:00:00

<SNIP>

Collecting oauthlib>=3.0.0 (from requests-oauthlib>=0.7.0->google-auth-oauthlib<1.1,>=0.5->tensorboard<2.14,>=2.13->tensorflow)
  Downloading oauthlib-3.2.2-py3-none-any.whl (151 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 151.7/151.7 kB 32.9 MB/s eta 0:00:00
Installing collected packages: namex, libclang, flatbuffers, dm-tree, termcolor, tensorflow-io-gcs-filesystem, tensorflow-hub, tensorflow-estimator, tensorboard-data-server, pyasn1-modules, opt-einsum, oauthlib, mdurl, markdown, keras, grpcio, gast, cachetools, astunparse, absl-py, requests-oauthlib, markdown-it-py, google-auth, rich, google-auth-oauthlib, tensorboard, keras-core, tensorflow, tensorflow-text, keras-nlp
Successfully installed absl-py-1.4.0 astunparse-1.6.3 cachetools-5.3.1 dm-tree-0.1.8 flatbuffers-23.5.26 gast-0.4.0 google-auth-2.22.0 google-auth-oauthlib-1.0.0 grpcio-1.56.2 keras-2.13.1 keras-core-0.1.2 keras-nlp-0.6.0 libclang-16.0.6 markdown-3.4.3 markdown-it-py-3.0.0 mdurl-0.1.2 namex-0.0.7 oauthlib-3.2.2 opt-einsum-3.3.0 pyasn1-modules-0.3.0 requests-oauthlib-1.3.1 rich-13.4.2 tensorboard-2.13.0 tensorboard-data-server-0.7.1 tensorflow-2.13.0 tensorflow-estimator-2.13.0 tensorflow-hub-0.14.0 tensorflow-io-gcs-filesystem-0.32.0 tensorflow-text-2.13.0 termcolor-2.3.0

When I attempt to import keras_nlp Python states an error about MXNET. I do not want MXNET, only tensorflow.

Python 3.10.9 | packaged by conda-forge | (main, Feb  2 2023, 20:20:04) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import keras_nlp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/__init__.py", line 8, in <module>
    from keras_nlp import layers
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/layers/__init__.py", line 8, in <module>
    from keras_nlp.src.layers.modeling.cached_multi_head_attention import CachedMultiHeadAttention
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/src/__init__.py", line 23, in <module>
    from keras_nlp.src import layers
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/src/layers/__init__.py", line 15, in <module>
    from keras_nlp.src.layers.modeling.cached_multi_head_attention import (
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/src/layers/modeling/cached_multi_head_attention.py", line 16, in <module>
    from keras_nlp.src.api_export import keras_nlp_export
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/src/api_export.py", line 17, in <module>
    from keras_nlp.src.backend import keras
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/src/backend/__init__.py", line 27, in <module>
    from keras_nlp.src.backend import config
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/src/backend/config.py", line 17, in <module>
    import keras_core
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_core/__init__.py", line 8, in <module>
    from keras_core import activations
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_core/activations/__init__.py", line 8, in <module>
    from keras_core.src.activations import deserialize
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_core/src/__init__.py", line 1, in <module>
    from keras_core.src import activations
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_core/src/activations/__init__.py", line 3, in <module>
    from keras_core.src.activations.activations import elu
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_core/src/activations/activations.py", line 1, in <module>
    from keras_core.src import backend
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_core/src/backend/__init__.py", line 48, in <module>
    raise ValueError(f"Unable to import backend : {backend()}")
ValueError: Unable to import backend : mxnet
>>> 

Expected behavior
I would like to import keras_nlp and tensorflow.

import keras_nlp
import tensorflow as tf
from tensorflow import keras

Additional context
I want to reproduce the code found in this collab session on my own server
https://colab.research.google.com/github/keras-team/keras-io/blob/master/guides/ipynb/keras_nlp/getting_started.ipynb

@hatdropper1977 hatdropper1977 added the type:Bug Something isn't working label Jul 20, 2023
@shivance
Copy link
Collaborator

shivance commented Jul 21, 2023

This is interesting.
Could you once try downgrade keras-core version to 0.1.0 ?

@mattdangerw
Copy link
Member

Ah interesting! Apologies, I think I misunderstood the previous issue.

The immediate reasons I can think of that this would happen...

  1. You have a keras.json in your home directory that has "backend": "mxnet" in it.
  2. You have a KERAS_BACKEND=mxnet environment variable set in your environment.

Can you check if either of those are the case? Very curious as to how this came about.

@hatdropper1977
Copy link
Author

Ah interesting! Apologies, I think I misunderstood the previous issue.

The immediate reasons I can think of that this would happen...

  1. You have a keras.json in your home directory that has "backend": "mxnet" in it.
  2. You have a KERAS_BACKEND=mxnet environment variable set in your environment.

Can you check if either of those are the case? Very curious as to how this came about.

@mattdangerw - I think you discovered it!

sh-4.2$ find . -name keras.json
./.keras/keras.json
find: ‘./SageMaker/lost+found’: Permission denied
sh-4.2$ cat keras.json
cat: keras.json: No such file or directory
sh-4.2$ cat ./.keras/keras.json
{
    "floatx": "float32",
    "epsilon": 1e-07,
    "backend": "mxnet",
    "image_data_format": "channels_first"
}
sh-4.2$ 

@hatdropper1977
Copy link
Author

This is interesting.
Could you once try downgrade keras-core version to 0.1.0 ?

@shivance - this failed. I think @mattdangerw found the issue.

(base) sh-4.2$ pip freeze | grep keras
(base) sh-4.2$ pip install -U keras-core==0.1.0 keras-nlp tensorflow
Looking in indexes: https://pypi.org/simple, https://pip.repos.neuron.amazonaws.com
Collecting keras-core==0.1.0
  Downloading keras_core-0.1.0-py3-none-any.whl (727 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 728.0/728.0 kB 14.8 MB/s eta 0:00:00
Collecting keras-nlp
  Downloading keras_nlp-0.6.0-py3-none-any.whl (576 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 576.5/576.5 kB 13.6 MB/s eta 0:00:00
Collecting tensorflow
  Downloading tensorflow-2.13.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (524.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 524.1/524.1 MB 1.5 MB/s eta 0:00:00
Collecting absl-py (from keras-core==0.1.0)
  Downloading absl_py-1.4.0-py3-none-any.whl (126 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 126.5/126.5 kB 29.2 MB/s eta 0:00:00
Requirement already satisfied: numpy in ./anaconda3/lib/python3.10/site-packages (from keras-core==0.1.0) (1.22.3)
Collecting rich (from keras-core==0.1.0)
  Downloading rich-13.4.2-py3-none-any.whl (239 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 239.4/239.4 kB 5.5 MB/s eta 0:00:00

Then I import Keras_nlp...

>>> import keras_nlp
2023-07-21 16:21:46.264556: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-07-21 16:21:48.077024: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/__init__.py", line 8, in <module>
    from keras_nlp import layers
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/layers/__init__.py", line 8, in <module>
    from keras_nlp.src.layers.modeling.cached_multi_head_attention import CachedMultiHeadAttention
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/src/__init__.py", line 23, in <module>
    from keras_nlp.src import layers
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/src/layers/__init__.py", line 15, in <module>
    from keras_nlp.src.layers.modeling.cached_multi_head_attention import (
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/src/layers/modeling/cached_multi_head_attention.py", line 16, in <module>
    from keras_nlp.src.api_export import keras_nlp_export
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/src/api_export.py", line 17, in <module>
    from keras_nlp.src.backend import keras
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/src/backend/__init__.py", line 27, in <module>
    from keras_nlp.src.backend import config
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_nlp/src/backend/config.py", line 17, in <module>
    import keras_core
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_core/__init__.py", line 8, in <module>
    from keras_core import activations
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_core/activations/__init__.py", line 8, in <module>
    from keras_core.src.activations import deserialize
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_core/src/__init__.py", line 1, in <module>
    from keras_core.src import activations
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_core/src/activations/__init__.py", line 3, in <module>
    from keras_core.src.activations.activations import elu
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_core/src/activations/activations.py", line 1, in <module>
    from keras_core.src import backend
  File "/home/ec2-user/anaconda3/lib/python3.10/site-packages/keras_core/src/backend/__init__.py", line 41, in <module>
    raise ValueError(f"Unable to import backend : {backend()}")
ValueError: Unable to import backend : mxnet

@mattdangerw
Copy link
Member

@hatdropper1977 glad you found it!

Had you done some work with https://github.com/awslabs/keras-apache-mxnet back with the old multi-backend Keras library?

@hatdropper1977
Copy link
Author

@hatdropper1977 glad you found it!

Had you done some work with https://github.com/awslabs/keras-apache-mxnet back with the old multi-backend Keras library?

I launched a clean Jupyter notebook from Amazon Web Services (AWS) Sagemaker. They had that file in my home directory by default, with backend set to MXNET.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants