Skip to content

Fix ops.select output-shape/dtype inference under broadcasting - #23265

Merged
hertschuh merged 2 commits into
keras-team:masterfrom
winklemad:fix/ops-select-broadcast-shape
Jul 22, 2026
Merged

hertschuh merged 2 commits into
keras-team:masterfrom
winklemad:fix/ops-select-broadcast-shape

Conversation

@winklemad

@winklemad winklemad commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #23264.

Description

keras.ops.select broadcasts every array in condlist and choicelist (and default) to a common shape and promotes the dtype across all choices, but Select.compute_output_spec returned choicelist[0]'s shape and dtype:

def compute_output_spec(self, condlist, choicelist, default=0):
    first_element = choicelist[0]
    return KerasTensor(first_element.shape, dtype=first_element.dtype)

So the inferred shape is wrong whenever a condition, a later choice, or default is broader than the first choice (ops.select([x>0], [row], 0) with x=(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, and default, and resolve the dtype with dtypes.result_type across all choices and default.

Tests: extended test_select with a broadcasting-condition case and a broadcasting-default case (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:

  • I am a human, and not a bot.
  • I will be responsible for responding to review comments in a timely manner.
  • I will work with the maintainers to push this PR forward until submission.

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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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

Comment thread keras/src/ops/numpy.py
Comment on lines +9484 to +9497
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

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)

Comment on lines +7137 to +7143
# 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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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.

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

codecov-commenter commented Jul 16, 2026 •

Copy link
Copy Markdown

Codecov Report

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

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     
Flag Coverage Δ
keras 83.89% <100.00%> (-0.51%) ⬇️
keras-cpu 83.89% <100.00%> (+<0.01%) ⬆️
keras-gpu ?
keras-jax 57.88% <100.00%> (+<0.01%) ⬆️
keras-numpy 53.69% <100.00%> (+<0.01%) ⬆️
keras-openvino 59.53% <100.00%> (+<0.01%) ⬆️
keras-tensorflow 59.54% <100.00%> (-0.29%) ⬇️
keras-torch 58.68% <100.00%> (-0.39%) ⬇️
keras-tpu ?

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

☔ View full report in Codecov by Harness.
📢 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 hertschuh left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for fixing this!

@google-ml-butler google-ml-butler Bot added kokoro:force-run ready to pull Ready to be merged into the codebase labels Jul 22, 2026
@hertschuh hertschuh removed the stat:awaiting keras-eng Awaiting response from Keras engineer label Jul 22, 2026
@hertschuh
hertschuh merged commit 14ce04c into keras-team:master Jul 22, 2026
16 of 17 checks passed
andersendsa pushed a commit to andersendsa/keras that referenced this pull request Aug 6, 2026
…-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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready to pull Ready to be merged into the codebase size:S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

keras.ops.select infers the wrong output shape under broadcasting

5 participants