Skip to content

Conversation

acsweet
Copy link

@acsweet acsweet commented Aug 7, 2025

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:

  • Quantization is not yet implemented for the MLX backend
  • MLX does not currently support float8
  • MLX does not support matmul with integer data types
  • masking is handled separately from a _keras_mask attribute, as it cannot be added to MLX array objects

Any 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

  • I have added all the necessary unit tests for my change.
  • I have verified that my change does not break existing code and works with all backends (TensorFlow, JAX, and PyTorch).
  • My PR is based on the latest changes of the main branch (if unsure, rebase the code).
  • I have followed the Keras Hub Model contribution guidelines in making these changes.
  • I have followed the Keras Hub API design guidelines in making these changes.
  • I have signed the Contributor License Agreement.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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

  1. 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.

Copy link

@gemini-code-assist gemini-code-assist bot left a 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))

Choose a reason for hiding this comment

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

critical

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.

Suggested change
inputs = tf.convert_to_tensor(memoryview(inputs))
inputs = tf.convert_to_tensor(np.array(inputs))

Comment on lines +39 to +42
if keras.config.backend() == "mlx":
self.assertAllEqual(backend.get_keras_mask(outputs), mask)
else:
self.assertAllEqual(outputs._keras_mask, mask)

Choose a reason for hiding this comment

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

medium

To improve readability and reduce code duplication, you can determine the output mask based on the backend and then perform the assertion once.

Suggested change
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,
)

Comment on lines +119 to +126
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)

Choose a reason for hiding this comment

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

medium

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)

Comment on lines +136 to +143
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)

Choose a reason for hiding this comment

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

medium

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)

Comment on lines +96 to +103
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)

Choose a reason for hiding this comment

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

medium

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.

Suggested change
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

Choose a reason for hiding this comment

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

medium

This commented-out import appears to be a leftover and should be removed to keep the code clean.

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.

1 participant