Skip to content

Fix TypeError in keras.ops.searchsorted by using shape instead of len #21472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

dhantule
Copy link
Contributor

@dhantule dhantule commented Jul 14, 2025

Replaces len(sorted_sequence) with sorted_sequence.shape[0] in the searchsorted function to prevent a TypeError when sorted_sequence is a symbolic Tensor, error reproduced in this gist.

Fixes: #21465

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.

Summary of Changes

Hello @dhantule, 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 resolves a TypeError in the keras.ops.searchsorted function, making it robust and compatible with symbolic Tensors. The core change involves updating how the size of the input sequence is determined, moving from a direct len() call to accessing the shape attribute, which is crucial for operations involving symbolic graph computations.

Highlights

  • Bug Fix: TypeError with Symbolic Tensors: I've addressed a TypeError that occurred in keras.ops.searchsorted when the sorted_sequence argument was a symbolic Tensor. The previous implementation used len(sorted_sequence) to determine the output type, which is not compatible with symbolic Tensors.
  • Compatibility with Symbolic Tensors: The fix ensures that searchsorted can now correctly handle symbolic Tensors by replacing the problematic len() call with sorted_sequence.shape[0], which is the appropriate way to get the size of the first dimension for a Tensor.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 is currently in preview and 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 to provide feedback.

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 fixes a TypeError in keras.ops.searchsorted for symbolic tensors by using sorted_sequence.shape[0] instead of len(sorted_sequence). The feedback focuses on improving the readability of the changed code by simplifying the ternary expression and removing redundant parentheses.

@codecov-commenter
Copy link

codecov-commenter commented Jul 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.80%. Comparing base (89d953e) to head (a3f9cd7).
Report is 4 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #21472   +/-   ##
=======================================
  Coverage   82.80%   82.80%           
=======================================
  Files         565      565           
  Lines       55505    55508    +3     
  Branches     8662     8662           
=======================================
+ Hits        45962    45965    +3     
  Misses       7429     7429           
  Partials     2114     2114           
Flag Coverage Δ
keras 82.61% <ø> (+<0.01%) ⬆️
keras-jax 63.39% <ø> (+<0.01%) ⬆️
keras-numpy 58.60% <ø> (+<0.01%) ⬆️
keras-openvino 33.70% <ø> (-0.01%) ⬇️
keras-tensorflow 63.85% <ø> (+<0.01%) ⬆️
keras-torch 63.51% <ø> (+<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.

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@@ -2134,7 +2134,7 @@ def searchsorted(sorted_sequence, values, side="left"):
f"sorted_sequence.shape={sorted_sequence.shape}"
)
out_type = (
"int32" if len(sorted_sequence) <= np.iinfo(np.int32).max else "int64"
"int32" if sorted_sequence.shape[0] <= np.iinfo(np.int32).max else "int64"
Copy link
Collaborator

Choose a reason for hiding this comment

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

You also need to check sorted_sequence.shape[0] is not None (it could be None for a symbolic tensor)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @fchollet, I have tested searchsorted with sorted_sequence as None and I am getting value error, traceback is shown here. Should I need to raise another value error ?

keras_sorted= keras.ops.searchsorted(None, [13, 12])
print("keras_sorted", keras_sorted)
Traceback (most recent call last):
  File "/Users/dhantule/Desktop/keras-test/searchsorted#21465.py", line 22, in <module>
    keras_sorted= keras.ops.searchsorted(None, [13, 12])
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dhantule/Desktop/dhantule-keras/keras/keras/src/ops/numpy.py", line 5329, in searchsorted
    sorted_sequence = backend.convert_to_tensor(sorted_sequence)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/dhantule/Desktop/dhantule-keras/keras/keras/src/backend/tensorflow/core.py", line 153, in convert_to_tensor
    return tf.convert_to_tensor(x, dtype=dtype)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/tensorflow/python/framework/constant_op.py", line 108, in convert_to_eager_tensor
    return ops.EagerTensor(value, ctx.device_name, dtype)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Attempt to convert a value (None) with an unsupported type (<class 'NoneType'>) to a Tensor.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh the sorted_sequence would not be None, simply, it's shape would have a None dimension.

I think this is one way to trigger it is:

keras_sorted= keras.ops.searchsorted(keras.KerasTensor((None,), dtype="float32"), [13, 12])

@iezepov
Copy link

iezepov commented Jul 16, 2025

Sorry, if I'm not familiar with the codebase enough, but would "backend/numpy" and "backend/torch" also need the same fix? Code looks identical there: https://github.com/search?q=repo%3Akeras-team%2Fkeras%20len(sorted_sequence)&type=code

@JyotinderSingh
Copy link
Collaborator

Sorry, if I'm not familiar with the codebase enough, but would "backend/numpy" and "backend/torch" also need the same fix? Code looks identical there: https://github.com/search?q=repo%3Akeras-team%2Fkeras%20len(sorted_sequence)&type=code

Yes, fixes need to be ported across backends to maintain consistent behavior

@JyotinderSingh
Copy link
Collaborator

@dhantule just checking in on the status of this fix, are you facing any blockers?

@dhantule
Copy link
Contributor Author

dhantule commented Aug 1, 2025

@dhantule just checking in on the status of this fix, are you facing any blockers?

Hi @JyotinderSingh, I'll port the fix across backends, just wanted to know about this
#21472 (comment)

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

Successfully merging this pull request may close these issues.

keras.ops.searchsorted doesn't work with symbolic tensors
7 participants