-
Notifications
You must be signed in to change notification settings - Fork 19.6k
Keras <> NNX integration #21252
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
Keras <> NNX integration #21252
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #21252 +/- ##
==========================================
- Coverage 82.87% 82.72% -0.16%
==========================================
Files 567 567
Lines 56073 56214 +141
Branches 8756 8786 +30
==========================================
+ Hits 46470 46501 +31
- Misses 7459 7556 +97
- Partials 2144 2157 +13
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
keras/src/random/random_test.py
Outdated
x = ops.ones(3) | ||
|
||
@jax.jit | ||
@nnx.jit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the integration prevent the use of jax.jit
with Keras layers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes! it would only work with nnx.jit for now ( They might be working on adding support for jax.jit)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added nnx as a opt in with this flag - os.environ["KERAS_NNX_ENABLED"]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left some final comments on the config and configuration part. All good on my end once fixed.
keras/src/backend/config.py
Outdated
|
||
if "KERAS_NNX_ENABLED" in os.environ: | ||
env_val = os.environ["KERAS_NNX_ENABLED"].lower() | ||
if env_val: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems bad, don't we want to check if val.lower() = "true" or "1" here? Otherwise KERAS_NNX_ENABLED=False will enable nnx.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
if self._value is not None: | ||
# If NNX is enabled, it's possible the variable was already | ||
# initialized by a concrete call. In this case, | ||
# _deferred_initialize becomes a no-op for this variable. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this comment is incorrect. It raises an error in this case, so it's not a no-op.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated the comment and also if nnx is enabled, I added a return instead of retuning an error.
b53714b
to
3635148
Compare
|
||
# Apply basic configs that don't cause circular import | ||
set_floatx(_floatx) | ||
_NNX_ENABLED = _nnx_enabled_config |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we literally call set_nnx_enabled(_nnx_enabled_config)
(just going for consistency)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this leads to circular import error
from keras.src.utils import dataset_utils
keras/src/utils/dataset_utils.py:9: in <module>
from keras.src import tree
keras/src/tree/__init__.py:1: in <module>
from keras.src.tree.tree_api import assert_same_paths
keras/src/tree/tree_api.py:8: in <module>
from keras.src.tree import optree_impl as tree_impl
keras/src/tree/optree_impl.py:4: in <module>
from keras.src.backend.config import backend
E ImportError: cannot import name 'backend' from partially initialized module 'keras.src.backend.config' (most likely due to a circular import) (/workspaces/keras/keras/src/backend/config.py)
So decided to revert it. 2 lines below backend is also set in the same way _BACKEND = _backend
- in a way it is still consistent?
c9383e2
into
keras-team:master
The PR integrates NNX into JAX backend!
The following snippet shows how you would enable the nnx backend
Demo colab here : https://colab.sandbox.google.com/drive/1mK-4qbce2HGRIkcb4v5n4niWGDezL_6n#scrollTo=m-ZH9Mpnphfz
Added a github workflow action for nnx backend. Note this will fail - because this needs a new release of flax to work.