Skip to content

Commit c180b0e

Browse files
committed
ENH: add a "no_x64" device which only supports 32-bit ints and floats
This mimics the JAX's default (and the name follows the JAX `jax_enable_x64` flag name).
1 parent 9490c92 commit c180b0e

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

array_api_strict/_devices.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
from typing import Final
22

33
from ._dtypes import (
4-
DType, float32, float64, complex64, complex128, int64,
4+
DType, float32, float64, complex64, complex128, int64, uint64, int32,
55
_all_dtypes, _boolean_dtypes, _signed_integer_dtypes,
66
_unsigned_integer_dtypes, _integer_dtypes, _real_floating_dtypes,
77
_complex_floating_dtypes, _numeric_dtypes
88
)
99

10-
_ALL_DEVICE_NAMES = ("CPU_DEVICE", "device1", "device2", "no_float64")
10+
_ALL_DEVICE_NAMES = ("CPU_DEVICE", "device1", "device2", "no_float64", "no_x64")
1111

1212
class Device:
1313
_device: Final[str]
@@ -32,8 +32,11 @@ def __hash__(self) -> int:
3232

3333
CPU_DEVICE = Device()
3434
NO_FLOAT64_DEVICE = Device("no_float64")
35+
NO_X64_DEVICE = Device("no_x64")
3536

36-
ALL_DEVICES = (CPU_DEVICE, Device("device1"), Device("device2"), NO_FLOAT64_DEVICE)
37+
ALL_DEVICES = (
38+
CPU_DEVICE, Device("device1"), Device("device2"), NO_FLOAT64_DEVICE, NO_X64_DEVICE
39+
)
3740

3841

3942
def check_device(device: Device | None) -> None:
@@ -55,6 +58,14 @@ def get_default_dtypes(device: Device | None = None) -> dict[str, DType]:
5558
"integral": int64,
5659
"indexing": int64,
5760
}
61+
elif device == NO_X64_DEVICE:
62+
# mimic JAX default: no float64, no int64
63+
return {
64+
"real floating": float32,
65+
"complex floating": complex64,
66+
"integral": int32,
67+
"indexing": int32,
68+
}
5869
elif device == Device('device2'):
5970
# mimic a torch CPU device: support float64 but default to float32
6071
return {
@@ -77,6 +88,9 @@ def device_supports_dtype(device: Device | None, dtype: DType |None) -> bool:
7788
# Device("no_float64") supports all dtypes except float64 and complex128
7889
if device == NO_FLOAT64_DEVICE:
7990
return dtype not in (float64, complex128)
91+
if device == NO_X64_DEVICE:
92+
# "no_x64" only supports 32-bit ints and floats
93+
return dtype not in (float64, complex128, int64, uint64)
8094

8195
# All other devices support all dtypes
8296
return True

array_api_strict/tests/test_creation_functions.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
zeros,
2323
zeros_like,
2424
)
25-
from .._dtypes import float32, float64, complex64, bool as xp_bool
25+
from .._dtypes import float32, float64, complex64, int32, int64, bool as xp_bool
2626
from .._array_object import Array
2727
from .._devices import CPU_DEVICE, ALL_DEVICES, Device
2828
from .._info import __array_namespace_info__
@@ -368,6 +368,17 @@ def test_asarray_device_2():
368368
assert y.dtype == float64
369369

370370

371+
def test_asarray_no_x64_device():
372+
x = asarray(3, device=Device("no_x64"))
373+
assert x.dtype == int32
374+
375+
with pytest.raises(ValueError):
376+
asarray(3, device=Device("no_x64"), dtype=int64)
377+
378+
y = zeros_like(ones(3, dtype=int32), device=Device("no_x64"))
379+
assert y.dtype == int32
380+
381+
371382
@pytest.mark.parametrize("api_version", ['2021.12', '2022.12', '2023.12'])
372383
def from_dlpack_2023_12(api_version):
373384
if api_version != '2022.12':

0 commit comments

Comments
 (0)