array-api-compat icon indicating copy to clipboard operation
array-api-compat copied to clipboard

several `torch` binary functions don't accept scalars for one argument

Open mdhaber opened this issue 9 months ago • 5 comments

Per the 2024.12 standard, these should work, but don't:

from array_api_compat import torch as xp
xp.minimum(xp.asarray(1), 2)  # TypeError: minimum(): argument 'other' (position 2) must be Tensor, not int
xp.minimum(2, xp.asarray(1))  # TypeError: minimum(): argument 'input' (position 1) must be Tensor, not int
xp.copysign(xp.asarray(1.), 2)  # OK
xp.copysign(2, xp.asarray(1.))
# TypeError: copysign() received an invalid combination of arguments - got (int, Tensor), but expected one of:
#  * (Tensor input, Tensor other, *, Tensor out = None)
#  * (Tensor input, Number other, *, Tensor out = None)

Looks like maximum also has trouble. Haven't checked the rest of the functions that now accept scalars, but I assume others may have trouble.

mdhaber avatar Mar 14 '25 02:03 mdhaber

Would be great to report these to pytorch itself. Meanwhile we'll need a workaround in -compat indeed.

ev-br avatar Mar 14 '25 09:03 ev-br

Maybe once we figure out the extent of the problem (e.g. other functions beyond maximum), we can report it.

Or I've suggested before that we can probably work out a more streamlined reporting mechanism than to file the same issue in multiple places, customized for each affected backend. It might help if we can just ping someone from the libraries in these sorts of issues, and they can just add a link in a tracking issue in their project or something. Do you know anyone on the Torch side who is interested in array API compatibility who we might ping here?

mdhaber avatar Mar 14 '25 17:03 mdhaber

cross-ref https://github.com/data-apis/array-api-compat/pull/272 and https://github.com/data-apis/array-api-tests/pull/348

The bottom line seems to be that that in torch accepting scalars is more of an exception than a rule. Not sure who to ping, sadly, and a conclusion from my offline discussions with several pytorch devs is that it's on us to report these sorts of issues. A pytorch issue will probably not be closed, but whether someone will open it for us.... I doubt it.

ev-br avatar Mar 15 '25 12:03 ev-br

Testing shows 16 missing kernels, all xfailed for now: https://github.com/data-apis/array-api-compat/blob/1.11.2/torch-xfails.txt#L139

ev-br avatar Mar 21 '25 12:03 ev-br

This is marked as "blocked by upstream", but couldn't array_api_compat convert the scalar argument to a tensor of the result dtype before passing it to torch?

mdhaber avatar Nov 04 '25 20:11 mdhaber