Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ev-br
Copy link
Member

@ev-br ev-br commented Sep 8, 2025

Closes gh-350

Otherwise, the version which emulates "clip" fails with torch.vmap.


This patch is less innocuous than it looks, because it changes the promotion rules. Previously, min and max were not upcasting the result:

In [9]: xpn = array_namespace(np.arange(3))

In [10]: xpn.clip(xpn.arange(5, dtype=xpn.int8), 2, xpn.asarray(3.0))
Out[10]: array([2, 2, 2, 3, 3], dtype=int8)

and now they are:

In [11]: xpt = array_namespace(torch.ones(3))

In [12]: xpt.clip(xpt.arange(5, dtype=xpt.int8), 2, xpt.asarray(3.0))
Out[12]: tensor([2., 2., 2., 3., 3.])

OTOH, the wording in the spec, https://data-apis.org/array-api/draft/API_specification/generated/array_api.clip.html is

min ... should have the same data type as x.

and

If either min or max is an array having a different data type than x, behavior is unspecified and thus implementation-dependent.

Otherwise, the version which emulates "clip" fails with torch.vmap
(see data-apisgh-350)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incompatibility between clip and torch.vmap
1 participant