Skip to content

Conversation

@Sikandar1310291
Copy link

Description

This PR adds a regression test for issue #21916, where splash_attention caused a ConcretizationTypeError when compiled with jax.jit because the mask became a Tracer.

Changes

  • Added keras/src/backend/jax/splash_attention_test.py

Testing

The test mocks a TPU environment (where Splash Attention is active) and confirms that dot_product_attention gracefully falls back to the native implementation instead of crashing when a Tracer mask is encountered.

Related Issues

Closes #21916

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @Sikandar1310291, 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!

This pull request introduces a crucial regression test to prevent a ConcretizationTypeError that previously occurred in JAX's splash_attention when a mask was transformed into a Tracer during JIT compilation. The test ensures that the dot_product_attention function gracefully defaults to a standard implementation under these specific conditions, thereby maintaining stability and preventing application crashes, particularly in simulated TPU environments. Additionally, documentation for the keras.device() context manager has been added.

Highlights

  • Regression Test Added: A new regression test has been introduced to address a ConcretizationTypeError that occurred in JAX's splash_attention.
  • Issue Resolution: The test specifically targets an issue where splash_attention failed when a mask became a Tracer during jax.jit compilation, as described in issue jax.errors.ConcretizationTypeError in Splash attention during model.generate #21916.
  • Fallback Verification: The test confirms that dot_product_attention correctly falls back to a native implementation, preventing crashes in scenarios where splash_attention would otherwise fail due to a Tracer mask.
  • Environment Mocking: The test employs mocking for a TPU environment and _can_use_flash_attention to accurately simulate the conditions under which the bug occurred and the fallback mechanism should activate.
  • Documentation Update: New documentation has been added for the keras.device() context manager, explaining how to control tensor device placement across different backends.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

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
Contributor

@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 adds a regression test for an issue where splash_attention in JAX would crash when JIT-compiled with a mask. The new test correctly mocks a TPU environment and verifies that the implementation now gracefully falls back to a native implementation, preventing the crash. The test is well-structured and effectively covers the reported issue. I've only found a minor style issue with trailing whitespaces in the new test file. Additionally, this PR includes an unrelated but useful documentation update for keras.device() in the training guide, which is also well-written.

Comment on lines 22 to 33

# We also need to mock _can_use_flash_attention to return True
# so we enter the block where the check happens.

with unittest.mock.patch("keras.src.backend.jax.nn._can_use_flash_attention", return_value=True):
# We mock jax.devices() to simulate TPU platform
# The actual device object needs a 'platform' attribute
mock_device = unittest.mock.Mock()
mock_device.platform = "tpu"

with unittest.mock.patch("jax.devices", return_value=[mock_device]):

Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There are some trailing whitespaces on lines 22, 25, 31, and 33 within this block. Please remove them to improve code style consistency.

@codecov-commenter
Copy link

codecov-commenter commented Jan 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.81%. Comparing base (a08f1ab) to head (cd500a5).
⚠️ Report is 9 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #22016      +/-   ##
==========================================
+ Coverage   82.73%   82.81%   +0.08%     
==========================================
  Files         592      592              
  Lines       62072    62142      +70     
  Branches     9723     9735      +12     
==========================================
+ Hits        51353    51466     +113     
+ Misses       8197     8138      -59     
- Partials     2522     2538      +16     
Flag Coverage Δ
keras 82.64% <ø> (+0.08%) ⬆️
keras-jax 62.54% <ø> (+0.06%) ⬆️
keras-numpy 56.56% <ø> (-0.01%) ⬇️
keras-openvino 37.41% <ø> (-0.01%) ⬇️
keras-tensorflow 63.70% <ø> (+<0.01%) ⬆️
keras-torch 62.46% <ø> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gbaned gbaned added this to PR Queue Jan 16, 2026
@github-project-automation github-project-automation bot moved this to Assigned Reviewer in PR Queue Jan 16, 2026
Copy link
Collaborator

@hertschuh hertschuh left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this.

"""

"""
## Controlling device placement
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, you're right that we're missing documentation for this feature.

Can you please add it as a docstring here: https://github.com/keras-team/keras/blob/master/keras/src/backend/__init__.py#L77

This would be a separate PR, so undo this file.

# We ensure it falls back gracefully instead of crashing.

# Mock is_tpu=True to trigger the Splash Attention path
# We can't actually run on TPU in CI, but we want to test the logic path
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually, we are running on TPU in CI: https://github.com/keras-team/keras/actions/workflows/tpu_tests.yml

We already have a unit test that cover splash attention:
https://github.com/keras-team/keras/blob/master/keras/src/ops/nn_test.py#L1328

Do we need to add something to it? If so, please add to this other file instead.

@Sikandar1310291
Copy link
Author

Thanks for the review!

✅ I've consolidated the splash attention regression test into
keras/src/ops/nn_test.py
and deleted the separate test file as requested.
✅ I verified that
guides/training_with_built_in_methods.py
is clean and didn't need any reverts in this branch.
✅ I also fixed the linting error (line too long) that caused the CI failure.
All checks should pass now. Ready for another look!

Comment on lines +1340 to +1356
# Mock is_tpu=True to trigger the Splash Attention path
# We can't actually run on TPU in CI, but we want to test the logic path
# up to the fallback check.

# We also need to mock _can_use_flash_attention to return True
# so we enter the block where the check happens.

with unittest.mock.patch(
"keras.src.backend.jax.nn._can_use_flash_attention",
return_value=True,
):
# We mock jax.devices() to simulate TPU platform
# The actual device object needs a 'platform' attribute
mock_device = unittest.mock.Mock()
mock_device.platform = "tpu"

with unittest.mock.patch("jax.devices", return_value=[mock_device]):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we should check this by mocking everything. It only gives me very weak confidence that it will actually behave correctly on TPU.

The good news is that we run tests on actual TPUs. Look at the test right below this one for an example of how to skip when you're not on TPU. Then, you can remove all the mocking and everything else should just work.

I can trigger the TPU tests for you once it's ready, just let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Assigned Reviewer

Development

Successfully merging this pull request may close these issues.

jax.errors.ConcretizationTypeError in Splash attention during model.generate

5 participants