Skip to content
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

RFC: add reciprocal for computing the element-wise reciprocal #790

Open
kgryte opened this issue Apr 18, 2024 · 2 comments · May be fixed by #802
Open

RFC: add reciprocal for computing the element-wise reciprocal #790

kgryte opened this issue Apr 18, 2024 · 2 comments · May be fixed by #802
Labels
API extension Adds new functions or objects to the API. Needs Discussion Needs further discussion. RFC Request for comments. Feature requests and proposed changes.
Projects
Milestone

Comments

@kgryte
Copy link
Contributor

kgryte commented Apr 18, 2024

This RFC proposes the addition of a new API in the array API specification for computing the element-wise reciprocal.

Overview

Based on array comparison data, the API is available across all major array libraries in the PyData ecosystem.

reciprocal was originally identified in #187 as a potential standardization candidate and has been mentioned by downstream libraries (see Intheon case study).

Prior art

Proposal

def reciprocal(x: array, /) -> array

Questions

  • While 1/x may be appear to be a trivial operation not warranting a dedicated API, there can be benefits should the API be mapped to dedicated hardware instructions. One can argue that this is best dealt with at the compiler level and end users should not be concerned with potential micro-optimizations. However, for larger datasets, there can be real-world benefits to explicit avoidance of division operations and array libraries may choose to avoid relying on compiler magic to implement polynomial approximations to the reciprocal. Given that this is implemented by all considered array libraries and straightforward to implement (similar to the standard already including negative and positive), are there any reasons to not include this API?
  • Given that the output data type will be a floating-point data type, are we okay limiting (i.e., via the use of "should") input dtypes to floating-point? Libraries would be free, of course, to explicitly support other dtypes (e.g., as in PyTorch and TensorFlow), but that would not be guaranteed to be portable.
@kgryte kgryte added RFC Request for comments. Feature requests and proposed changes. API extension Adds new functions or objects to the API. Needs Discussion Needs further discussion. labels Apr 18, 2024
@kgryte kgryte added this to the v2024 milestone Apr 18, 2024
@kgryte kgryte added this to Stage 0 in Proposals Apr 18, 2024
@rgommers
Copy link
Member

@oleksandr-pavlyk's comment that this is useful also to avoid dealing with moving a Python scalar to GPU/accelerator if it's written as 1/x and that's why it was added to dpctl seems like an additional reason to do this.

So +1 for adding this, with "should" for floating-point types only (as suggested). That way it's a straightforward signal that this is portable, without placing a new implementation burden on any known library.

@kgryte kgryte moved this from Stage 0 to Stage 1 in Proposals Apr 30, 2024
@kgryte kgryte linked a pull request May 2, 2024 that will close this issue
@kgryte
Copy link
Contributor Author

kgryte commented May 2, 2024

PR is up: #802

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API extension Adds new functions or objects to the API. Needs Discussion Needs further discussion. RFC Request for comments. Feature requests and proposed changes.
Projects
Proposals
Stage 1
Development

Successfully merging a pull request may close this issue.

2 participants