Skip to content

ENH: testing: Add check_device parameter#835

Open
prady0t wants to merge 3 commits into
data-apis:mainfrom
prady0t:add-check-device
Open

ENH: testing: Add check_device parameter#835
prady0t wants to merge 3 commits into
data-apis:mainfrom
prady0t:add-check-device

Conversation

@prady0t

@prady0t prady0t commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Towards: #833

Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>
Comment thread tests/test_funcs.py Outdated
Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>
Comment thread tests/test_funcs.py Outdated
a = xp.asarray([0, 3, 6, 10], device=device)
b = xp.asarray([1, 2, 3, 10], device=device)
expected = xp.asarray([True, False, True, False])
expected = xp.asarray([True, False, True, False], device=device)

@prady0t prady0t Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to specify device here; otherwise, it fails in the assert_equal statement below with:

Actual: array_api_strict.Device('device1')
Desired: array_api_strict.Device('CPU_DEVICE')

The reason is that res.device is set by the pytest fixture (as device1) and expected.device is set by array_api_strict default unless specified.

Comment thread tests/test_funcs.py Outdated
res = isclose(a, b)
assert get_device(res) == device
assert_equal(res, xp.asarray([False, False, False, False, True]))
assert_equal(res, xp.asarray([False, False, False, False, True], device=device))

@prady0t prady0t Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reason as above.

@prady0t

prady0t commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

CC @j-bowhay

Comment thread tests/test_funcs.py Outdated

@j-bowhay j-bowhay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to have some tests :)

Signed-off-by: Pradyot Ranjan <99216956+pradyotRanjan@users.noreply.github.com>
@prady0t

prady0t commented Jul 7, 2026

Copy link
Copy Markdown
Contributor Author

Some irrelevant device checks have been removed as assert functions check for the device by default now. I've also added checks for non-default device.

@lucascolley lucascolley added enhancement New feature or request xpx.testing labels Jul 7, 2026
@lucascolley lucascolley added this to the 0.11.1 milestone Jul 7, 2026
@lucascolley lucascolley self-requested a review July 7, 2026 12:34
Comment thread tests/test_funcs.py
Comment on lines -214 to -221
def test_device(self, xp: ModuleType, device: Device):
x = xp.asarray([1, 2, 3, 4], device=device)
y = apply_where(x % 2 == 0, x, self.f1, self.f2)
assert get_device(y) == device
y = apply_where(x % 2 == 0, x, self.f1, fill_value=0)
assert get_device(y) == device
y = apply_where(x % 2 == 0, x, self.f1, fill_value=x)
assert get_device(y) == device

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I'm not convinced that these tests are redundant yet. The device picture gives us a non-default device where possible:

@pytest.fixture
def device(
library: Backend, xp: ModuleType
) -> Device: # numpydoc ignore=PR01,RT01,RT03
"""
Return a valid device for the backend.
Where possible, return a device that is not the default one.
"""
if library == Backend.ARRAY_API_STRICT:
return xp.Device("device1")
if library == Backend.TORCH:
return xp.device("meta")
if library == Backend.TORCH_GPU:
return xp.device("cpu")
return get_device(xp.empty(0))

IIUC dropping the tests removes that testing coverage of non-default devices. I don't think we should do that until we have an improved solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request xpx.testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants