|
13 | 13 |
|
14 | 14 | import ctypes
|
15 | 15 |
|
| 16 | +import pytest |
| 17 | + |
16 | 18 | from cuda.core.experimental import Device
|
17 |
| -from cuda.core.experimental._memory import Buffer, MemoryResource |
| 19 | +from cuda.core.experimental._memory import Buffer, DLDeviceType, MemoryResource |
18 | 20 | from cuda.core.experimental._utils import handle_return
|
19 | 21 |
|
20 | 22 |
|
@@ -116,6 +118,12 @@ def device_id(self) -> int:
|
116 | 118 | raise RuntimeError("the pinned memory resource is not bound to any GPU")
|
117 | 119 |
|
118 | 120 |
|
| 121 | +class NullMemoryResource(DummyHostMemoryResource): |
| 122 | + @property |
| 123 | + def is_host_accessible(self) -> bool: |
| 124 | + return False |
| 125 | + |
| 126 | + |
119 | 127 | def buffer_initialization(dummy_mr: MemoryResource):
|
120 | 128 | buffer = dummy_mr.allocate(size=1024)
|
121 | 129 | assert buffer.handle != 0
|
@@ -211,3 +219,46 @@ def test_buffer_close():
|
211 | 219 | buffer_close(DummyHostMemoryResource())
|
212 | 220 | buffer_close(DummyUnifiedMemoryResource(device))
|
213 | 221 | buffer_close(DummyPinnedMemoryResource(device))
|
| 222 | + |
| 223 | + |
| 224 | +def test_buffer_dunder_dlpack(): |
| 225 | + device = Device() |
| 226 | + device.set_current() |
| 227 | + dummy_mr = DummyDeviceMemoryResource(device) |
| 228 | + buffer = dummy_mr.allocate(size=1024) |
| 229 | + capsule = buffer.__dlpack__() |
| 230 | + assert "dltensor" in repr(capsule) |
| 231 | + capsule = buffer.__dlpack__(max_version=(1, 0)) |
| 232 | + assert "dltensor" in repr(capsule) |
| 233 | + with pytest.raises(BufferError, match=r"^Sorry, not supported: dl_device other than None$"): |
| 234 | + buffer.__dlpack__(dl_device=[]) |
| 235 | + with pytest.raises(BufferError, match=r"^Sorry, not supported: copy=True$"): |
| 236 | + buffer.__dlpack__(copy=True) |
| 237 | + with pytest.raises(BufferError, match=r"^Expected max_version Tuple\[int, int\], got \[\]$"): |
| 238 | + buffer.__dlpack__(max_version=[]) |
| 239 | + with pytest.raises(BufferError, match=r"^Expected max_version Tuple\[int, int\], got \(9, 8, 7\)$"): |
| 240 | + buffer.__dlpack__(max_version=(9, 8, 7)) |
| 241 | + |
| 242 | + |
| 243 | +@pytest.mark.parametrize( |
| 244 | + ("DummyMR", "expected"), |
| 245 | + [ |
| 246 | + (DummyDeviceMemoryResource, (DLDeviceType.kDLCUDA, 0)), |
| 247 | + (DummyHostMemoryResource, (DLDeviceType.kDLCPU, 0)), |
| 248 | + (DummyUnifiedMemoryResource, (DLDeviceType.kDLCUDAHost, 0)), |
| 249 | + (DummyPinnedMemoryResource, (DLDeviceType.kDLCUDAHost, 0)), |
| 250 | + ], |
| 251 | +) |
| 252 | +def test_buffer_dunder_dlpack_device_success(DummyMR, expected): |
| 253 | + device = Device() |
| 254 | + device.set_current() |
| 255 | + dummy_mr = DummyMR() if DummyMR is DummyHostMemoryResource else DummyMR(device) |
| 256 | + buffer = dummy_mr.allocate(size=1024) |
| 257 | + assert buffer.__dlpack_device__() == expected |
| 258 | + |
| 259 | + |
| 260 | +def test_buffer_dunder_dlpack_device_failure(): |
| 261 | + dummy_mr = NullMemoryResource() |
| 262 | + buffer = dummy_mr.allocate(size=1024) |
| 263 | + with pytest.raises(BufferError, match=r"^buffer is neither device-accessible nor host-accessible$"): |
| 264 | + buffer.__dlpack_device__() |
0 commit comments