Fix TODO: enforce static scan dimension in associative_scan#22073
Fix TODO: enforce static scan dimension in associative_scan#22073MalyalaKarthik66 wants to merge 2 commits intokeras-team:masterfrom
associative_scan#22073Conversation
Summary of ChangesHello @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 Highlights
🧠 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 AssistThe 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
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 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
|
There was a problem hiding this comment.
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:
- Modify
TestCoreOpsDynamicShape.test_associative_scanto remove the case that now fails. - Add a new test case to
test_associative_scan_invalid_argumentsto explicitly check that using a dynamic scan dimension raises the newValueError. I've added a suggestion for this.
| 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 | ||
| ) |
There was a problem hiding this comment.
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 Report❌ Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Apparently #22072 will add support for dynamic scan dimensions |
12cc3b4 to
b66b5ed
Compare
This PR resolves the TODO in
associative_scanby 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.