|
| 1 | +import sys |
| 2 | +import pytest |
| 3 | + |
| 4 | +import numpy |
| 5 | +import dpnp |
| 6 | +from numpy.testing import assert_array_equal |
| 7 | +from .helper import ( |
| 8 | + get_all_dtypes, |
| 9 | +) |
| 10 | + |
| 11 | +device_oneAPI = 14 # DLDeviceType.kDLOneAPI |
| 12 | + |
| 13 | +class TestDLPack: |
| 14 | + # @pytest.mark.parametrize("max_version", [(0, 0), None, (1, 0), (100, 3)]) |
| 15 | + # def test_dunder_dlpack_refcount(self, max_version): |
| 16 | + # x = dpnp.arange(5) |
| 17 | + # y = x.__dlpack__(max_version=max_version) |
| 18 | + # print() |
| 19 | + # print(sys.getrefcount(x), sys.getrefcount(y)) |
| 20 | + # assert sys.getrefcount(x) == 2 |
| 21 | + # del y |
| 22 | + # print(sys.getrefcount(x)) |
| 23 | + # assert sys.getrefcount(x) == 2 |
| 24 | + |
| 25 | + @pytest.mark.parametrize("stream", [None, 1]) |
| 26 | + def test_stream(self, stream): |
| 27 | + x = dpnp.arange(5) |
| 28 | + x.__dlpack__(stream=stream) |
| 29 | + |
| 30 | + @pytest.mark.parametrize("copy", [True, None, False]) |
| 31 | + def test_copy(self, copy): |
| 32 | + x = dpnp.arange(5) |
| 33 | + x.__dlpack__(copy=copy) |
| 34 | + |
| 35 | + def test_wrong_copy(self): |
| 36 | + x = dpnp.arange(5) |
| 37 | + x.__dlpack__(copy=dpnp.array([1, 2, 3])) |
| 38 | + |
| 39 | + @pytest.mark.parametrize("xp", [dpnp, numpy]) |
| 40 | + @pytest.mark.parametrize("dt", get_all_dtypes(no_none=True)) |
| 41 | + def test_dtype_passthrough(self, xp, dt): |
| 42 | + x = xp.arange(5).astype(dt) |
| 43 | + y = xp.from_dlpack(x) |
| 44 | + |
| 45 | + assert y.dtype == x.dtype |
| 46 | + assert_array_equal(x, y) |
| 47 | + |
| 48 | + @pytest.mark.parametrize("xp", [dpnp, numpy]) |
| 49 | + def test_non_contiguous(self, xp): |
| 50 | + x = xp.arange(25).reshape((5, 5)) |
| 51 | + |
| 52 | + y1 = x[0] |
| 53 | + assert_array_equal(y1, xp.from_dlpack(y1)) |
| 54 | + |
| 55 | + y2 = x[:, 0] |
| 56 | + assert_array_equal(y2, xp.from_dlpack(y2)) |
| 57 | + |
| 58 | + y3 = x[1, :] |
| 59 | + assert_array_equal(y3, xp.from_dlpack(y3)) |
| 60 | + |
| 61 | + y4 = x[1] |
| 62 | + assert_array_equal(y4, xp.from_dlpack(y4)) |
| 63 | + |
| 64 | + y5 = xp.diagonal(x).copy() |
| 65 | + assert_array_equal(y5, xp.from_dlpack(y5)) |
| 66 | + |
| 67 | + def test_device(self): |
| 68 | + x = dpnp.arange(5) |
| 69 | + assert x.__dlpack_device__()[0] == device_oneAPI |
| 70 | + y = dpnp.from_dlpack(x) |
| 71 | + assert y.__dlpack_device__()[0] == device_oneAPI |
| 72 | + z = y[::2] |
| 73 | + assert z.__dlpack_device__()[0] == device_oneAPI |
| 74 | + |
| 75 | + # def dlpack_deleter_exception(self, max_version): |
| 76 | + # x = np.arange(5) |
| 77 | + # _ = x.__dlpack__(max_version=max_version) |
| 78 | + # raise RuntimeError |
| 79 | + |
| 80 | + # @pytest.mark.parametrize("max_version", [None, (1, 0)]) |
| 81 | + # def test_dlpack_destructor_exception(self, max_version): |
| 82 | + # with pytest.raises(RuntimeError): |
| 83 | + # self.dlpack_deleter_exception(max_version=max_version) |
| 84 | + |
| 85 | + # def test_readonly(self): |
| 86 | + # x = np.arange(5) |
| 87 | + # x.flags.writeable = False |
| 88 | + # # Raises without max_version |
| 89 | + # with pytest.raises(BufferError): |
| 90 | + # x.__dlpack__() |
| 91 | + |
| 92 | + # # But works fine if we try with version |
| 93 | + # y = np.from_dlpack(x) |
| 94 | + # assert not y.flags.writeable |
| 95 | + |
| 96 | + # def test_ndim0(self): |
| 97 | + # x = np.array(1.0) |
| 98 | + # y = np.from_dlpack(x) |
| 99 | + # assert_array_equal(x, y) |
| 100 | + |
| 101 | + # def test_size1dims_arrays(self): |
| 102 | + # x = np.ndarray(dtype='f8', shape=(10, 5, 1), strides=(8, 80, 4), |
| 103 | + # buffer=np.ones(1000, dtype=np.uint8), order='F') |
| 104 | + # y = np.from_dlpack(x) |
| 105 | + # assert_array_equal(x, y) |
| 106 | + |
| 107 | + # def test_copy(self): |
| 108 | + # x = np.arange(5) |
| 109 | + |
| 110 | + # y = np.from_dlpack(x) |
| 111 | + # assert np.may_share_memory(x, y) |
| 112 | + # y = np.from_dlpack(x, copy=False) |
| 113 | + # assert np.may_share_memory(x, y) |
| 114 | + # y = np.from_dlpack(x, copy=True) |
| 115 | + # assert not np.may_share_memory(x, y) |
| 116 | + |
| 117 | + # def test_device(self): |
| 118 | + # x = np.arange(5) |
| 119 | + # # requesting (1, 0), i.e. CPU device works in both calls: |
| 120 | + # x.__dlpack__(dl_device=(1, 0)) |
| 121 | + # np.from_dlpack(x, device="cpu") |
| 122 | + # np.from_dlpack(x, device=None) |
| 123 | + |
| 124 | + # with pytest.raises(ValueError): |
| 125 | + # x.__dlpack__(dl_device=(10, 0)) |
| 126 | + # with pytest.raises(ValueError): |
| 127 | + # np.from_dlpack(x, device="gpu") |
0 commit comments