You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Raise on implicit device transfer in __setitem__ #207
As expected, it's not possible to run operations that combine two such arrays because they live on different devices:
>>>a+bTraceback (mostrecentcalllast):
CellIn[23], line1a+bFile~/miniforge3/envs/dev/lib/python3.13/site-packages/array_api_strict/_array_object.py:545in__add__self._check_type_device(other)
File~/miniforge3/envs/dev/lib/python3.13/site-packages/array_api_strict/_array_object.py:241in_check_type_deviceraiseValueError(f"Arrays from two different devices ({self.device} and {other.device}) can not be combined.")
ValueError: Arraysfromtwodifferentdevices (array_api_strict.Device('device2') andarray_api_strict.Device('CPU_DEVICE')) cannotbecombined.
However, doing assignment works without raising an exception despite the fact that this causes an implicit data transfer between devices:
Note that the current behavior (no exception on cross-device __setitem__) of array-api-strict is aligned with PyTorch:
>>>importtorch>>>a=torch.ones(10, device="mps")
>>>b=2*torch.ones(10) # CPU>>>a+bTraceback (mostrecentcalllast):
CellIn[9], line1a+bRuntimeError: Expectedalltensorstobeonthesamedevice, butfoundatleasttwodevices, mps:0andcpu!
>>>b[:5] =a[:5] # implicit MPS to CPU transfer does not raise!>>>btensor([1., 1., 1., 1., 1., 2., 2., 2., 2., 2.])
Not sure if this lack of exception could be considered a "bug" of PyTorch with MPS.
However, DPNP on some hardware architectures do raise: ValueError: Execution queue is not compatible with allocation queues as seen in scikit-learn/scikit-learn#32460 (comment).
I asked Claude that did a source code / doc review, and __setitem__ is considered "data movement" operation (like .to(), .copy_(), .cpu(), .cuda(), ...) and therefore is not expected to raise in PyTorch (contrary to "compute" operations like +).
Handling devices is complex, and some frameworks have elaborate policies for handling device placement. Therefore this section only gives recommendations, rather than hard requirements:
...
Raise an exception if an operation involves arrays on different devices (i.e. avoid implicit data transfer between devices).
So I think it would make sense for array-api-strict to raise on cross-device __setitem__ if we consider assignment an "operation".
Currently the Array API spec has a general recommendation to avoid implicit transfers, however different libraries make specific choices (e.g. torch special-cases a CPU device), hence the spec itself cannot really force a specific choice on all compliant libraries.
For array-api-strict this means it should indeed raise an exception, because it only implements behaviors which are explicitly allowed by the spec, and rejects anything unspecified, hence implementation-defined.
data-apis/array-api#1005 updates the specifications of __setitem__ and __getitem__; for array-api-strict, we could either lump an update to #206, or have a separate PR just for __{get,set}item__ (the latter's preferred, I'd think).
we could either lump an update to #206, or have a separate PR just for __{get,set}item__ (the latter's preferred, I'd think).
Raising on cross-device __{get,set}item__ is actually not directly related to the dtype restrictions of a particular device so better implement that independently to ease the review and keep a neat changelog.
Raising on cross-device {get,set}item is actually not directly related to the dtype restrictions of a particular device so better implement that independently
Activity
ogrisel commentedon Apr 30, 2026
Note that the current behavior (no exception on cross-device
__setitem__) of array-api-strict is aligned with PyTorch:Not sure if this lack of exception could be considered a "bug" of PyTorch with MPS.
However, DPNP on some hardware architectures do raise:
ValueError: Execution queue is not compatible with allocation queuesas seen in scikit-learn/scikit-learn#32460 (comment).ogrisel commentedon Apr 30, 2026
I asked Claude that did a source code / doc review, and
__setitem__is considered "data movement" operation (like.to(),.copy_(),.cpu(),.cuda(), ...) and therefore is not expected to raise in PyTorch (contrary to "compute" operations like+).I also checked the array API spec and there is no mention of constraints related to device transfers: https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__setitem__.html#setitem.
So I am not sure if it's ok or not for an array library to raise in this case.
ogrisel commentedon Apr 30, 2026
Also from:
So I think it would make sense for array-api-strict to raise on cross-device
__setitem__if we consider assignment an "operation".ogrisel commentedon Apr 30, 2026
cc @betatim.
lucascolley commentedon May 2, 2026
would this fall under gh-168 Olivier?
ev-br commentedon May 2, 2026
We discussed this in the Array API community meeting last week, https://hackmd.io/zn5bvdZTQIeJmb3RW1B-8g?view#Meeting-minutes-30-April-2026, with a resolution to designate it as implementation-defined.
Currently the Array API spec has a general recommendation to avoid implicit transfers, however different libraries make specific choices (e.g. torch special-cases a CPU device), hence the spec itself cannot really force a specific choice on all compliant libraries.
For
array-api-strictthis means it should indeed raise an exception, because it only implements behaviors which are explicitly allowed by the spec, and rejects anything unspecified, hence implementation-defined.data-apis/array-api#1005 updates the specifications of
__setitem__and__getitem__; forarray-api-strict, we could either lump an update to #206, or have a separate PR just for__{get,set}item__(the latter's preferred, I'd think).ogrisel commentedon May 4, 2026
This is indeed related but raising on cross-device
__setitem__by default would be an even more direct way to achieve this.ogrisel commentedon May 4, 2026
Raising on cross-device
__{get,set}item__is actually not directly related to the dtype restrictions of a particular device so better implement that independently to ease the review and keep a neat changelog.ev-br commentedon May 9, 2026
Agreed. Fix in #209