-
Notifications
You must be signed in to change notification settings - Fork 296
[MLX backend] Support for MLX backend across layers
tests
#2351
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
base: master
Are you sure you want to change the base?
Conversation
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.
Summary of Changes
Hello @acsweet, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
I've initiated the integration of the MLX backend into Keras Hub, with this pull request specifically focusing on enabling MLX support for tests within the keras_hub/src/layers directory. This involves addressing several known limitations of the MLX backend, such as its current lack of support for quantization, float8, and integer matrix multiplication, as well as specific handling for Keras masks.
Highlights
- Initial MLX Backend Integration: I've begun adding support for the MLX backend to Keras Hub, starting with modifications to tests in keras_hub/src/layers to ensure compatibility.
- MLX-Specific Test Adjustments: I've implemented conditional logic in various tests to account for MLX's current limitations, including skipping quantization tests, adapting mask propagation, and adjusting data types for image processing tests where MLX doesn't support integer matmul.
- New MLX Utility Function: I've introduced an is_mlx_array utility function to properly identify MLX array objects within the Keras backend.
- Dependency Updates: I've updated requirements.txt to include necessary MLX-related dependencies like pybind11, cmake, and mlx.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
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.
Code Review
This pull request introduces support for the MLX backend in Keras Hub, focusing on the layers
tests. The changes are generally good, with appropriate conditional logic and skips for MLX-specific limitations. My review includes several suggestions to refactor duplicated code in the tests for better maintainability. I also found a critical issue in tensor_utils.py
where an MLX array is incorrectly converted to a TensorFlow tensor, which will likely cause a runtime error. Additionally, there's a minor cleanup suggestion to remove a commented-out import.
# mlx array to tf tensor currently only supports flat arrays | ||
array_shape = inputs.shape | ||
inputs = inputs.flatten() | ||
inputs = tf.convert_to_tensor(memoryview(inputs)) |
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.
Using memoryview(inputs)
will raise a TypeError
because MLX arrays do not support the buffer protocol. You should convert the MLX array to a NumPy array before passing it to tf.convert_to_tensor
.
inputs = tf.convert_to_tensor(memoryview(inputs)) | |
inputs = tf.convert_to_tensor(np.array(inputs)) |
if keras.config.backend() == "mlx": | ||
self.assertAllEqual(backend.get_keras_mask(outputs), mask) | ||
else: | ||
self.assertAllEqual(outputs._keras_mask, mask) |
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.
To improve readability and reduce code duplication, you can determine the output mask based on the backend and then perform the assertion once.
if keras.config.backend() == "mlx": | |
self.assertAllEqual(backend.get_keras_mask(outputs), mask) | |
else: | |
self.assertAllEqual(outputs._keras_mask, mask) | |
self.assertAllEqual( | |
backend.get_keras_mask(outputs) | |
if keras.config.backend() == "mlx" | |
else outputs._keras_mask, | |
mask, | |
) |
if keras.config.backend() == "mlx": | ||
backend.set_keras_mask(decoder_sequence, mask) | ||
outputs = decoder(decoder_sequence, encoder_sequence) | ||
self.assertAllEqual(backend.get_keras_mask(outputs), mask) | ||
else: | ||
decoder_sequence._keras_mask = mask | ||
outputs = decoder(decoder_sequence, encoder_sequence) | ||
self.assertAllEqual(outputs._keras_mask, mask) |
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 block contains duplicated code. You can refactor it by setting the mask first, then calling the decoder, and finally asserting the output mask. This will make the code more concise and easier to maintain.
if keras.config.backend() == "mlx":
backend.set_keras_mask(decoder_sequence, mask)
else:
decoder_sequence._keras_mask = mask
outputs = decoder(decoder_sequence, encoder_sequence)
output_mask = (
backend.get_keras_mask(outputs)
if keras.config.backend() == "mlx"
else outputs._keras_mask
)
self.assertAllEqual(output_mask, mask)
if keras.config.backend() == "mlx": | ||
backend.set_keras_mask(decoder_sequence, mask) | ||
outputs = decoder(decoder_sequence) | ||
self.assertAllEqual(backend.get_keras_mask(outputs), mask) | ||
else: | ||
decoder_sequence._keras_mask = mask | ||
outputs = decoder(decoder_sequence) | ||
self.assertAllEqual(outputs._keras_mask, mask) |
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 block has duplicated code, similar to the previous test. Refactoring it will improve code quality and maintainability.
if keras.config.backend() == "mlx":
backend.set_keras_mask(decoder_sequence, mask)
else:
decoder_sequence._keras_mask = mask
outputs = decoder(decoder_sequence)
output_mask = (
backend.get_keras_mask(outputs)
if keras.config.backend() == "mlx"
else outputs._keras_mask
)
self.assertAllEqual(output_mask, mask)
if keras.config.backend() == "mlx": | ||
backend.set_keras_mask(inputs, mask) | ||
outputs = encoder(inputs) | ||
self.assertAllEqual(backend.get_keras_mask(outputs), mask) | ||
else: | ||
inputs._keras_mask = mask | ||
outputs = encoder(inputs) | ||
self.assertAllEqual(outputs._keras_mask, mask) |
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 block contains duplicated code. You can refactor it to improve readability and maintainability by setting the mask first, then calling the encoder, and finally asserting the output mask.
if keras.config.backend() == "mlx": | |
backend.set_keras_mask(inputs, mask) | |
outputs = encoder(inputs) | |
self.assertAllEqual(backend.get_keras_mask(outputs), mask) | |
else: | |
inputs._keras_mask = mask | |
outputs = encoder(inputs) | |
self.assertAllEqual(outputs._keras_mask, mask) | |
if keras.config.backend() == "mlx": | |
backend.set_keras_mask(inputs, mask) | |
else: | |
inputs._keras_mask = mask | |
outputs = encoder(inputs) | |
output_mask = ( | |
backend.get_keras_mask(outputs) | |
if keras.config.backend() == "mlx" | |
else outputs._keras_mask | |
) | |
self.assertAllEqual(output_mask, mask) |
@@ -10,12 +10,14 @@ | |||
from keras import ops | |||
from keras import tree | |||
|
|||
# from keras.src.trainers.data_adapters import is_mlx_array |
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.
Description of the change
This PR is the first (of more to come) to add support for the MLX backend of Keras to work with Keras Hub (see keras-team/keras#19571).
The focus of this PR is tests in
keras_hub/src/layers
, with the following notes:_keras_mask
attribute, as it cannot be added to MLXarray
objectsAny feedback on changes and implementations here would be appreciated. Will modify as needed!
Note that I have not included MLX in the GitHub CI yet. There are still a few issues failing tests on the Linux environment, so might need to wait until MLX is available via Keras nightly, also open to any other suggestions.
Checklist