Skip to content

RFC: add scalar support to xp.searchsorted #967

Closed

Description

@ogrisel
import array_api_strict as xp

a = xp.arange(10, device=xp.Device("device1"))
xp.searchsorted(a, 42)

raises:

Traceback (most recent call last):
  Cell In[5], line 5
    xp.searchsorted(a, 42)
  File ~/miniforge3/envs/dev/lib/python3.13/site-packages/array_api_strict/_flags.py:395 in wrapper
    return func(*args, **kwargs)
  File ~/miniforge3/envs/dev/lib/python3.13/site-packages/array_api_strict/_searching_functions.py:78 in searchsorted
    if x1.dtype not in _real_numeric_dtypes or x2.dtype not in _real_numeric_dtypes:
AttributeError: 'int' object has no attribute 'dtype'

This is a bit annoying as it requires to instead write: xp.searchsorted(a, xp.asarray(42, device=a.device)) which feels unnecessarily verbose.

Even PyTorch accepts the following without complaining, for instance:

import array_api_compat.torch as xp

a = xp.arange(10, device="mps")
xp.searchsorted(a, 42)

However, the SPEC does not mention Python scalar support explicitly, so maybe it would need to be updated first?

https://data-apis.org/array-api/latest/API_specification/generated/array_api.searchsorted.html#searchsorted

Activity

  1. lucascolley commented on Aug 22, 2025

    @lucascolley
    Member

    I guess this is similar to #860

  2. lucascolley commented on Aug 22, 2025

    @lucascolley
    Member

    This was at least a conscious decision originally, #699 (comment)

  3. ogrisel commented on Aug 25, 2025

    @ogrisel
    Author

    requires that x2 be an array. CuPy and TensorFlow require that x2 be an array, while NumPy, PyTorch, and others allow x2 to be a scalar. In this PR, I've chosen to restrict x2 to be an array. For the scalar use case, downstream consumers can wrap scalar values as zero-dimensional arrays.

    Indeed, thanks for looking this up. Still, I think that calling searchsorted with a scalar as second argument is quite common, and it's easy to forget about passing the device argument when developing with numpy on a CPU and then get the code crash when running with another on GPUs.

    I think we could make the spec evolve and handle the discrepancy in scalar support via array_api_compat.

  4. lucascolley commented on Aug 25, 2025

    @lucascolley
    Member

    Yes, I think I am +1 because the usage feels similar to xp.where.

  5. kgryte commented on Aug 25, 2025

    @kgryte
    Contributor

    I'll go ahead and add this to the next array API workgroup meeting as a discussion topic.

  6. kgryte commented on Nov 13, 2025

    @kgryte
    Contributor

    I've opened #982 for adding scalar support to the second argument in searchsorted.

  7. changed the title [-]Make `xp.searchsorted` accept Python scalars for the second argument[/-] [+]RFC: add scalar support to `xp.searchsorted`[/+] on Nov 13, 2025
  8. added
    RFCRequest for comments. Feature requests and proposed changes.
    API changeChanges to existing functions or objects in the API.
    on Nov 13, 2025
  9. added this to the v2025 milestone on Nov 13, 2025
  10. moved this to Stage 3 in Proposalson Nov 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    API changeChanges to existing functions or objects in the API.RFCRequest for comments. Feature requests and proposed changes.

    Type

    No type

    Projects

    • Status
      Stage 3

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions