Repository navigation
Fix ops.select output-shape/dtype inference under broadcasting - #23265
Conversation
Select.compute_output_spec returned choicelist[0]'s shape and dtype, but select broadcasts every array in condlist and choicelist (and default) to a common shape and promotes the dtype across all choices. So the inferred shape was wrong whenever a condition or a later choice was broader than the first choice, and only the first choice's dtype was considered. Mirror the sibling Where.compute_output_spec: broadcast the shape over all conditions and choices, and resolve the dtype with dtypes.result_type across all choices and default. Extended test_select with a broadcasting-condition case.
There was a problem hiding this comment.
Code Review
This pull request updates the compute_output_spec method in keras/src/ops/numpy.py to correctly include the default argument in shape broadcasting and dtype resolution for the select operation, ensuring broader compatibility. Additionally, a new test case is added to keras/src/ops/numpy_test.py to verify this behavior. The review feedback suggests further refactoring the implementation for conciseness and expanding the test suite to include specific edge cases for the default argument.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| output_shape = () | ||
| for condition in condlist: | ||
| output_shape = broadcast_shapes( | ||
| output_shape, getattr(condition, "shape", []) | ||
| ) | ||
| dtypes_to_resolve = [] | ||
| for choice in choicelist: | ||
| output_shape = broadcast_shapes( | ||
| output_shape, getattr(choice, "shape", []) | ||
| ) | ||
| dtypes_to_resolve.append(getattr(choice, "dtype", type(choice))) | ||
| dtypes_to_resolve.append(getattr(default, "dtype", type(default))) | ||
| output_dtype = dtypes.result_type(*dtypes_to_resolve) | ||
| return KerasTensor(output_shape, dtype=output_dtype) |
There was a problem hiding this comment.
The current implementation for shape broadcasting doesn't include the default argument. According to the NumPy documentation for select, the default value should also be broadcastable to the same shape as the conditions and choices.
I suggest a small refactor to include default in the shape calculation and to make the logic more concise by iterating over all inputs for shape broadcasting at once. This will fix the bug and improve readability.
output_shape = ()
for x in list(condlist) + list(choicelist) + [default]:
output_shape = broadcast_shapes(output_shape, getattr(x, "shape", []))
dtypes_to_resolve = [getattr(c, "dtype", type(c)) for c in choicelist]
dtypes_to_resolve.append(getattr(default, "dtype", type(default)))
output_dtype = dtypes.result_type(*dtypes_to_resolve)
return KerasTensor(output_shape, dtype=output_dtype)| # A broader condition or later choice widens the inferred shape (it is | ||
| # broadcast against every condition and choice, like `where`), not just | ||
| # the first choice's shape. | ||
| x = backend.KerasTensor((2, 3)) | ||
| row = backend.KerasTensor((3,)) | ||
| y = knp.select([x > 0], [row], 0) | ||
| self.assertEqual(y.shape, (2, 3)) |
There was a problem hiding this comment.
To make the test suite more robust, please also add a case that verifies broadcasting with the default argument. For example, a test where condlist and choicelist have smaller shapes than default would ensure the default value's shape is correctly considered for broadcasting.
| # A broader condition or later choice widens the inferred shape (it is | |
| # broadcast against every condition and choice, like `where`), not just | |
| # the first choice's shape. | |
| x = backend.KerasTensor((2, 3)) | |
| row = backend.KerasTensor((3,)) | |
| y = knp.select([x > 0], [row], 0) | |
| self.assertEqual(y.shape, (2, 3)) | |
| # A broader condition or later choice widens the inferred shape (it is | |
| # broadcast against every condition and choice, like `where`), not just | |
| # the first choice's shape. | |
| x = backend.KerasTensor((2, 3)) | |
| row = backend.KerasTensor((3,)) | |
| y = knp.select([x > 0], [row], 0) | |
| self.assertEqual(y.shape, (2, 3)) | |
| # Test broadcasting with default | |
| cond = backend.KerasTensor((1,)) | |
| choice = backend.KerasTensor((1,)) | |
| default_val = backend.KerasTensor((2, 3)) | |
| y = knp.select([cond > 0], [choice], default_val) | |
| self.assertEqual(y.shape, (2, 3)) |
Include default in the shape broadcast (np.select broadcasts default to the common shape too) and iterate over condlist + choicelist + default in one loop. Added a test case where a broader default widens the inferred shape.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #23265 +/- ##
==========================================
- Coverage 84.58% 84.06% -0.52%
==========================================
Files 465 465
Lines 69522 69527 +5
Branches 11450 11451 +1
==========================================
- Hits 58804 58449 -355
- Misses 7780 8142 +362
+ Partials 2938 2936 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
hertschuh
left a comment
There was a problem hiding this comment.
Thanks for fixing this!
…-team#23265) * Fix ops.select output-shape/dtype inference under broadcasting Select.compute_output_spec returned choicelist[0]'s shape and dtype, but select broadcasts every array in condlist and choicelist (and default) to a common shape and promotes the dtype across all choices. So the inferred shape was wrong whenever a condition or a later choice was broader than the first choice, and only the first choice's dtype was considered. Mirror the sibling Where.compute_output_spec: broadcast the shape over all conditions and choices, and resolve the dtype with dtypes.result_type across all choices and default. Extended test_select with a broadcasting-condition case. * Broadcast default in select shape inference and simplify the loop Include default in the shape broadcast (np.select broadcasts default to the common shape too) and iterate over condlist + choicelist + default in one loop. Added a test case where a broader default widens the inferred shape.
Fixes #23264.
Description
keras.ops.selectbroadcasts every array incondlistandchoicelist(anddefault) to a common shape and promotes the dtype across all choices, butSelect.compute_output_specreturnedchoicelist[0]'s shape and dtype:So the inferred shape is wrong whenever a condition, a later choice, or
defaultis broader than the first choice (ops.select([x>0], [row], 0)withx=(None,3),row=(None,1)infers(None,1)but the real output is(4,3)), and only the first choice's dtype was considered.Fix: mirror the sibling
Where.compute_output_spec— broadcast the output shape over all conditions, choices, anddefault, and resolve the dtype withdtypes.result_typeacross all choices anddefault.Tests: extended
test_selectwith a broadcasting-condition case and a broadcasting-defaultcase (each widens the inferred shape). They fail before this change and pass after; the existing equal-shape cases are unchanged. Verified on the numpy backend.AI assistance disclosure (per the AI-Assisted Contribution Policy)
This change was developed with the assistance of an AI coding tool. I have reviewed and understand the fix, verified it locally on the numpy backend, and take full responsibility for the contribution. I will respond to review comments myself.
Contributor Agreement
Please review our AI-Assisted Contribution Policy and check all boxes below before submitting your PR for review: