Skip to content

Fix TODO: enforce static scan dimension in associative_scan#22073

Open
MalyalaKarthik66 wants to merge 2 commits intokeras-team:masterfrom
MalyalaKarthik66:fix-associative-scan-static-dim
Open

Fix TODO: enforce static scan dimension in associative_scan#22073
MalyalaKarthik66 wants to merge 2 commits intokeras-team:masterfrom
MalyalaKarthik66:fix-associative-scan-static-dim

Conversation

@MalyalaKarthik66
Copy link
Contributor

@MalyalaKarthik66 MalyalaKarthik66 commented Jan 28, 2026

This PR resolves the TODO in associative_scan by enforcing that the scan dimension is statically known. Without this check, dynamic or unknown dimensions can trigger deep recursion and runtime failures that are difficult to debug.

The validation is added early to fail fast with a clear error message, improving robustness without changing the intended behavior of the operation.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @MalyalaKarthik66, 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 significantly improves the robustness and debuggability of the associative_scan function within the Keras TensorFlow backend. By introducing an explicit check for statically known scan dimensions, it proactively prevents potential runtime failures and deep recursion issues that could arise from dynamic or unknown input shapes. The changes ensure that invalid inputs are caught early with clear error messages, thereby enhancing the overall stability and user experience without altering the intended functional behavior of the operation.

Highlights

  • Static Dimension Enforcement: The associative_scan function now enforces that the scan dimension (axis) must be statically known. If a dynamic or unknown dimension is encountered, a ValueError is raised early.
  • Improved Error Handling: Enhanced error messages are provided when associative_scan detects non-statically known dimensions or mismatches in array input dimensions, making debugging easier.
  • TODO Resolution: A long-standing TODO comment regarding constant dimension checks in associative_scan has been addressed and removed.

🧠 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 PR correctly enforces a statically known scan dimension in associative_scan for the TensorFlow backend, which improves robustness and aligns its behavior with other backends. The new error messages are clear and helpful, following the API design guidelines.

However, this change will cause an existing test, TestCoreOpsDynamicShape.test_associative_scan, to fail. The second part of this test case relies on the old behavior of allowing a dynamic scan dimension.

I recommend updating the tests to reflect this new, stricter behavior. Specifically:

  1. Modify TestCoreOpsDynamicShape.test_associative_scan to remove the case that now fails.
  2. Add a new test case to test_associative_scan_invalid_arguments to explicitly check that using a dynamic scan dimension raises the new ValueError. I've added a suggestion for this.

Comment on lines 1468 to 1482
def test_associative_scan_invalid_arguments(self):
# varying dimension at scan axis
x = (np.array([1, 2]), np.array([3, 4]), np.array([5, 6, 7]))
with self.assertRaisesRegex(ValueError, " first dimension"):
with self.assertRaisesRegex(ValueError, "first dimension"):
core.associative_scan(lambda x, y: (x[0] + y[0], x[1] + y[1]), x)

# same error, symbolic
x = (
KerasTensor((None, 5)),
KerasTensor((None, 4)),
)
with self.assertRaisesRegex(ValueError, " first dimension"):
with self.assertRaisesRegex(ValueError, "first dimension"):
core.associative_scan(
lambda x, y: (x[0] + y[0], x[1] + y[1]), x, axis=1
)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

To ensure the new static dimension check is covered by tests, please add a case to verify that a ValueError is raised when the scan dimension is dynamic. This also helps document the intended behavior for future reference.

Additionally, please note that this PR will cause TestCoreOpsDynamicShape.test_associative_scan to fail. You'll need to remove the second part of that test which now validates incorrect behavior.

    def test_associative_scan_invalid_arguments(self):
        # varying dimension at scan axis
        x = (np.array([1, 2]), np.array([3, 4]), np.array([5, 6, 7]))
        with self.assertRaisesRegex(ValueError, "first dimension"):
            core.associative_scan(lambda x, y: (x[0] + y[0], x[1] + y[1]), x)

        # same error, symbolic
        x = (
            KerasTensor((None, 5)),
            KerasTensor((None, 4)),
        )
        with self.assertRaisesRegex(ValueError, "first dimension"):
            core.associative_scan(
                lambda x, y: (x[0] + y[0], x[1] + y[1]), x, axis=1
            )

        # dynamic dimension at scan axis
        x = (KerasTensor((5, None)), KerasTensor((5, None)))
        with self.assertRaisesRegex(ValueError, "statically known dimension"):
            core.associative_scan(
                lambda x, y: (x[0] + y[0], x[1] + y[1]), x, axis=1
            )

@codecov-commenter
Copy link

codecov-commenter commented Jan 28, 2026

Codecov Report

❌ Patch coverage is 63.63636% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.83%. Comparing base (c718248) to head (b66b5ed).

Files with missing lines Patch % Lines
keras/src/backend/tensorflow/core.py 75.00% 1 Missing and 1 partial ⚠️
keras/src/ops/core.py 33.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #22073      +/-   ##
==========================================
- Coverage   82.83%   82.83%   -0.01%     
==========================================
  Files         592      592              
  Lines       62991    62995       +4     
  Branches     9886     9889       +3     
==========================================
  Hits        52179    52179              
- Misses       8262     8264       +2     
- Partials     2550     2552       +2     
Flag Coverage Δ
keras 82.65% <63.63%> (-0.01%) ⬇️
keras-jax 62.51% <9.09%> (-0.01%) ⬇️
keras-numpy 56.62% <9.09%> (-0.01%) ⬇️
keras-openvino 37.46% <9.09%> (-0.01%) ⬇️
keras-tensorflow 63.77% <63.63%> (-0.01%) ⬇️
keras-torch 62.53% <9.09%> (-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.

@hertschuh
Copy link
Collaborator

Apparently #22072 will add support for dynamic scan dimensions

@MalyalaKarthik66 MalyalaKarthik66 force-pushed the fix-associative-scan-static-dim branch from 12cc3b4 to b66b5ed Compare February 2, 2026 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants