Skip to content

Raise on implicit device transfer in __setitem__ #207

@ogrisel

Description

@ogrisel

Consider the following two arrays:

>>> import array_api_strict as xp
>>> a = xp.ones(10, device=xp.Device("device2"))
>>> b = 2 * xp.ones(10)

As expected, it's not possible to run operations that combine two such arrays because they live on different devices:

>>> a + b
Traceback (most recent call last):
  Cell In[23], line 1
    a + b
  File ~/miniforge3/envs/dev/lib/python3.13/site-packages/array_api_strict/_array_object.py:545 in __add__
    self._check_type_device(other)
  File ~/miniforge3/envs/dev/lib/python3.13/site-packages/array_api_strict/_array_object.py:241 in _check_type_device
    raise ValueError(f"Arrays from two different devices ({self.device} and {other.device}) can not be combined.")
ValueError: Arrays from two different devices (array_api_strict.Device('device2') and array_api_strict.Device('CPU_DEVICE')) can not be combined.

However, doing assignment works without raising an exception despite the fact that this causes an implicit data transfer between devices:

>>> b[:5] = a[:5]
>>> b
Array([1., 1., 1., 1., 1., 2., 2., 2., 2.,
       2.], dtype=array_api_strict.float64)

Shouldn't the __setitem__ call raise in this case?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions