Repository navigation
Expand file tree
/
Copy pathtest_dlpack.py
More file actions
112 lines (96 loc) · 3.53 KB
/
Copy pathtest_dlpack.py
File metadata and controls
112 lines (96 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
from enum import Enum
from hypothesis import given, strategies as st
from . import _array_module as xp
from . import pytest_helpers as ph
from . import hypothesis_helpers as hh
# dlpack Enum values,
# https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__dlpack_device__.html
class DLPackDeviceEnum(Enum):
CPU = 1
CUDA = 2
CPU_PINNED = 3
OPENCL = 4
VULKAN = 7
METAL = 8
VPI = 9
ROCM = 10
CUDA_MANAGED = 13
ONE_API = 14
@given(dtype=hh.all_dtypes, data=st.data())
def test_dlpack_device(dtype, data):
"""Test the array object __dlpack_device__ method."""
# TODO: 1. generate inputs on non-default devices
x = xp.empty(3, dtype=dtype)
device_type, device_id = x.__dlpack_device__()
assert DLPackDeviceEnum(int(device_type))
assert isinstance(device_id, int)
@given(
x=hh.arrays(dtype=hh.all_dtypes, shape=hh.shapes(min_dims=1, max_side=2)),
copy_kw=hh.kwargs(
copy=st.booleans() | st.none()
),
max_version_kw=hh.kwargs(
max_version=st.tuples(
st.integers(min_value=0, max_value=2),
st.integers(min_value=0, max_value=0)
)
),
dl_device_kw=hh.kwargs(
dl_device=st.tuples( # XXX: the 2023.12 standard only mandates ... kDLCPU ?
st.just(DLPackDeviceEnum.CPU.value),
st.just(0)
)
),
data=st.data()
)
def test_dunder_dlpack(x, copy_kw, max_version_kw, dl_device_kw, data):
repro_snippet = ph.format_snippet(
f"x.__dlpack__ with {copy_kw = }, {max_version_kw = } and {dl_device_kw = }"
)
try:
x.__dlpack__(**copy_kw, **max_version_kw, **dl_device_kw)
# apparently, we cannot do anything with the DLPack capsule from python
except BufferError as exc:
copy = copy_kw["copy"] if copy_kw else None
dl_device = dl_device_kw["dl_device"] if dl_device_kw else None
cross_device = dl_device is not None and dl_device != x.__dlpack_device__()
if copy is False and cross_device:
# a no-copy cross-device transfer must be refused with a BufferError
return
ph.add_note(exc, repro_snippet)
raise
except Exception as exc:
ph.add_note(exc, repro_snippet)
raise
@given(
dtype_device_pair=hh.device_dtype_pairs,
copy_kw=hh.kwargs(copy=st.booleans()),
data=st.data()
)
def test_from_dlpack(copy_kw, data, dtype_device_pair):
# TODO: 1. test copy; 2. generate inputs on non-default devices;
# 3. test for copy=False cross-device transfers
# 4. test 0D arrays / numpy scalars (the latter do not support dlpack ATM)
dtype, device = dtype_device_pair
x = data.draw(hh.arrays(dtype=dtype, shape=hh.shapes(min_dims=1, max_side=2)))
copy = copy_kw["copy"] if copy_kw else None
# XXX there is no way to tell if a no-copy cross-device transfer is meant to succeed
tgt_device = x.device if copy is False else device
if data.draw(st.booleans()):
tgt_device_kw = {"device": tgt_device}
else:
tgt_device_kw = {}
tgt_device = tgt_device_kw.get("device")
repro_snippet = ph.format_snippet(
f"y = from_dlpack({x!r}, **tgt_device_kw, **copy_kw) with {tgt_device_kw=} and {copy_kw=}"
)
try:
y = xp.from_dlpack(x, **tgt_device_kw, **copy_kw)
if tgt_device is None:
assert y.device == x.device
assert xp.all(y == x)
else:
assert y.device == tgt_device
except Exception as exc:
ph.add_note(exc, repro_snippet)
raise