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

feat: add scalar support to element-wise functions #862

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

kgryte
Copy link
Contributor

@kgryte kgryte commented Nov 25, 2024

This PR

  • progresses Python scalars in elementwise functions #807 in adding scalar support to element-wise functions. The changes are restricted to element-wise functions in order to simplify review.
  • updates the signatures for various dunder methods on the array object which did not indicate complex scalar support.
  • reformats and consolidates some of the notes included in affected function signatures to a dedicated "Notes" section, as done for special cases. This moves ancillary specification guidance to "below the fold". A subsequent PR will perform a similar clean-up for APIs outside of those affected by adding scalar support.

@kgryte kgryte added the API change Changes to existing functions or objects in the API. label Nov 25, 2024
@kgryte kgryte added this to the v2024 milestone Nov 25, 2024
@asmeurer
Copy link
Member

I think we need to update and reference this section for the specification on how scalars convert into arrays. https://data-apis.org/array-api/latest/API_specification/type_promotion.html#mixing-arrays-with-python-scalars

@asmeurer
Copy link
Member

And #841 if fixed will also potentially change how that text reads in relation to how real and complex scalar casting works.

@asmeurer
Copy link
Member

clip is already an elementwise function that supports scalars, but we should ensure the language is consistent.

@kgryte
Copy link
Contributor Author

kgryte commented Nov 28, 2024

In clip, which is slightly different in that the scalar arguments are optional, we already added guidance concerning differing data type kinds. However, we will still need to update to point to common documentation concerning scalar casting.

@ev-br
Copy link

ev-br commented Nov 28, 2024

Once python ints make the stage, I'd be ideal to explicitly spell the position on overflows. For both binary operators and elementwise functions. AFAICS, the spec only declares unspecified an attempt to cast a too-large python int to an integer dtype. However,

(Pdb) m32 = int(xp.finfo(xp.float32).max)
(Pdb) am32 = xp.asarray(m32, dtype=xp.float32)
(Pdb) am32
Array(3.4028235e+38, dtype=array_api_strict.float32)
(Pdb) am32 + m32
*** RuntimeWarning: overflow encountered in add
(Pdb) zzz = xp.asarray(m32*2, dtype=xp.float32)
*** RuntimeWarning: overflow encountered in cast

@kgryte kgryte added the Needs Review Pull request which needs review. label Dec 12, 2024
@asmeurer
Copy link
Member

Also regarding clip: #811

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API change Changes to existing functions or objects in the API. Needs Review Pull request which needs review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants