Repository navigation
Incompatibility between clip and torch.vmap #350
Description
Activity
that is a shame!
array-api-compat/array_api_compat/common/_aliases.py
Lines 438 to 439 in be9eff7
# Return a scalar for 0-D return out[()] For most things,
[()]works to get NumPy scalars for NumPy, and as a no-op for other arrays. But withvmapit seems the no-op assumption breaks down.In the long-run, I think this calls for an
as_scalar_if_numpyhelper to deal with the pesky NumPy special-case in a safe way. Not sure if there is a simpler workaround in the short term. WDYT @ev-br ?Reacted by Tim DawsonIn array-api-compat specifically, I think it's a matter of a simple refactoring to move the
[()]out ofcommon/and into "numpy/" and other specific folders.EDIT: one other offender is common/sign.
Good shout!
It would be great to understand better the origin of this error.
If I replaceout[()] = xwithout = torch.clone(x)at, it immediately fails a few lines below atout[()] = x out[ia] = a[ia] While this all can be worked around, it begs the question what is the deeper reason behind all this. Surely it cannot be that indexing operations are incompatible with
vmap?Ah, OK, This seems to be the answer: https://discuss.pytorch.org/t/vmap-inplace-arithmetic-error/196556/2
you can’t have a write to an existing tensor
So indeed mutating indexing operations are incompatible with torch.vmap. Which means
array_api_compat.torch.clipshould be rewritten in terms oftorch.clampinstead of emulating it as it does ATM.Reacted by Tim Dawson- added 2 commits that reference this issue
on Sep 8, 2025 A tentative fix is in #353, but I'm not sure it's entirely correct because of promotion rules. Thoughts?
I stumbled into an edge case when trying to apply
torch.vmapto some code I had rewritten to utilize array-api-compat. So far everything seems to work just fine, with the exception ofclip. Here's a minimal example:Which raises the following error:
I totally understand if full support for
torch.vmapis out of scope, but figured it might be worth raising the issue in case there's something which requires fixing.