Skip to content

feat: add Keras 3 support for multi-backend (pytorch, jax)#5650

Open
rmarquis wants to merge 8 commits into
bentoml:mainfrom
rmarquis:main
Open

feat: add Keras 3 support for multi-backend (pytorch, jax)#5650
rmarquis wants to merge 8 commits into
bentoml:mainfrom
rmarquis:main

Conversation

@rmarquis

@rmarquis rmarquis commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

What does this PR address?

Fixes #5649

Before submitting:

rmarquis added 2 commits July 4, 2026 01:02
…ack)

Make TensorFlow a lazy dependency and branch save/load/runnable logic
based on keras.config.backend(). This allows bentoml.keras to work with
Keras models using the PyTorch backend without requiring TensorFlow.

- Detect active backend via keras.config.backend() (Keras 3) or default
  to tensorflow (Keras 2).
- Record backend-relevant versions in ModelContext (keras, torch, jax).
- Reject tf_signatures/tf_save_options when backend is not tensorflow.
- Add backend-aware device handling and tensor conversion in runnable.
- Preserve existing TensorFlow backend behavior.
- Add tests/integration/frameworks/test_keras_torch_backend.py covering
  save/load/runner inference for a Keras 3 model using the torch backend.
- Add a note to docs/source/reference/bentoml/frameworks/keras.rst about
  Keras 2/3 multi-backend support and TensorFlow-only options.
Copilot AI review requested due to automatic review settings July 3, 2026 23:07
@rmarquis
rmarquis requested a review from a team as a code owner July 3, 2026 23:07
@rmarquis
rmarquis requested review from parano and removed request for a team July 3, 2026 23:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends bentoml.keras to support Keras 3 multi-backend setups (TensorFlow / PyTorch / JAX) by making TensorFlow optional where possible and adding backend-specific runner behavior.

Changes:

  • Add backend detection and backend-specific runnable execution paths (TensorFlow vs. torch vs. other backends).
  • Adjust save/load behavior and recorded framework context for Keras 3 usage.
  • Add an integration test for saving/loading with the Keras torch backend and update reference docs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
tests/integration/frameworks/test_keras_torch_backend.py Adds an integration test covering save/load + runner execution under Keras torch backend.
src/bentoml/_internal/frameworks/keras.py Implements backend-aware loading and runnable execution, including torch/jax tensor conversion helpers.
docs/source/reference/bentoml/frameworks/keras.rst Documents multi-backend support and TensorFlow-only options.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/bentoml/_internal/frameworks/keras.py
Comment thread src/bentoml/_internal/frameworks/keras.py Outdated
Comment thread src/bentoml/_internal/frameworks/keras.py Outdated
Comment thread src/bentoml/_internal/frameworks/keras.py
Comment thread src/bentoml/_internal/frameworks/keras.py Outdated
rmarquis added 4 commits July 4, 2026 01:20
Wire _normalize_device_name() into load_model() and KerasRunnable so
TensorFlow-style device strings (e.g. '/device:CPU:0') are accepted
when the active Keras backend is PyTorch. Rename the previously-unused
_normalize_torch_device helper to _normalize_device_name and extend its
docstring.

Add test_keras_torch_load_with_tf_style_device to verify save/load and
runner inference with a TF-style device string under the torch backend.
Store the active Keras backend name in ModelContext.framework_versions
under the 'backend' key. In load_model(), compare it against the current
backend from keras.config.backend() and raise BentoMLException with a
helpful message when they differ. This prevents confusing failures when a
model saved with one backend is loaded under another.

Update the load_model docstring to accurately describe this behavior.

Add test_keras_torch_load_with_wrong_backend_raises to verify the
mismatch error.
Avoid IndexError when a TensorFlow-style GPU device name is requested
but tf.config.list_physical_devices('GPU') returns an empty list. Only
call set_memory_growth when at least one physical GPU is present.
The torch backend runnable only converted numpy arrays and torch.Tensor
inputs, leaving plain Python lists/tuples to pass through unchanged. Keras
with the torch backend can fail on raw sequences, so convert them with
torch.as_tensor() and move to the target device.

Add test_keras_torch_runner_accepts_list_input covering list and tuple
inputs through the runner.
@rmarquis rmarquis changed the title Add Keras 3 support for multi-backend feat: add Keras 3 support for multi-backend (pytorch, jax) Jul 4, 2026
The Keras 3 integration only handles tensorflow, torch, and jax in
_get_context. The save_model docstring and framework reference docs
still listed OpenVINO as a supported backend, so drop it to match the
actual implementation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: Add Keras 3 support for multi-backend (pytorch, jax)

2 participants