-
Notifications
You must be signed in to change notification settings - Fork 315
Expand file tree
/
Copy pathtest_memory.py
More file actions
285 lines (213 loc) · 8.73 KB
/
Copy pathtest_memory.py
File metadata and controls
285 lines (213 loc) · 8.73 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
try:
from cuda.bindings import driver
except ImportError:
from cuda import cuda as driver
import ctypes
import pytest
from cuda.core.experimental import Buffer, Device, DeviceMemoryResource, MemoryResource
from cuda.core.experimental._memory import DLDeviceType
from cuda.core.experimental._utils.cuda_utils import handle_return
class DummyDeviceMemoryResource(MemoryResource):
def __init__(self, device):
self.device = device
def allocate(self, size, stream=None) -> Buffer:
ptr = handle_return(driver.cuMemAlloc(size))
return Buffer.from_handle(ptr=ptr, size=size, mr=self)
def deallocate(self, ptr, size, stream=None):
handle_return(driver.cuMemFree(ptr))
@property
def is_device_accessible(self) -> bool:
return True
@property
def is_host_accessible(self) -> bool:
return False
@property
def device_id(self) -> int:
return 0
class DummyHostMemoryResource(MemoryResource):
def __init__(self):
pass
def allocate(self, size, stream=None) -> Buffer:
# Allocate a ctypes buffer of size `size`
ptr = (ctypes.c_byte * size)()
return Buffer.from_handle(ptr=ptr, size=size, mr=self)
def deallocate(self, ptr, size, stream=None):
# the memory is deallocated per the ctypes deallocation at garbage collection time
pass
@property
def is_device_accessible(self) -> bool:
return False
@property
def is_host_accessible(self) -> bool:
return True
@property
def device_id(self) -> int:
raise RuntimeError("the pinned memory resource is not bound to any GPU")
class DummyUnifiedMemoryResource(MemoryResource):
def __init__(self, device):
self.device = device
def allocate(self, size, stream=None) -> Buffer:
ptr = handle_return(driver.cuMemAllocManaged(size, driver.CUmemAttach_flags.CU_MEM_ATTACH_GLOBAL.value))
return Buffer.from_handle(ptr=ptr, size=size, mr=self)
def deallocate(self, ptr, size, stream=None):
handle_return(driver.cuMemFree(ptr))
@property
def is_device_accessible(self) -> bool:
return True
@property
def is_host_accessible(self) -> bool:
return True
@property
def device_id(self) -> int:
return 0
class DummyPinnedMemoryResource(MemoryResource):
def __init__(self, device):
self.device = device
def allocate(self, size, stream=None) -> Buffer:
ptr = handle_return(driver.cuMemAllocHost(size))
return Buffer.from_handle(ptr=ptr, size=size, mr=self)
def deallocate(self, ptr, size, stream=None):
handle_return(driver.cuMemFreeHost(ptr))
@property
def is_device_accessible(self) -> bool:
return True
@property
def is_host_accessible(self) -> bool:
return True
@property
def device_id(self) -> int:
raise RuntimeError("the pinned memory resource is not bound to any GPU")
class NullMemoryResource(DummyHostMemoryResource):
@property
def is_host_accessible(self) -> bool:
return False
def buffer_initialization(dummy_mr: MemoryResource):
buffer = dummy_mr.allocate(size=1024)
assert buffer.handle != 0
assert buffer.size == 1024
assert buffer.memory_resource == dummy_mr
assert buffer.is_device_accessible == dummy_mr.is_device_accessible
assert buffer.is_host_accessible == dummy_mr.is_host_accessible
buffer.close()
def test_buffer_initialization():
device = Device()
device.set_current()
buffer_initialization(DummyDeviceMemoryResource(device))
buffer_initialization(DummyHostMemoryResource())
buffer_initialization(DummyUnifiedMemoryResource(device))
buffer_initialization(DummyPinnedMemoryResource(device))
def buffer_copy_to(dummy_mr: MemoryResource, device: Device, check=False):
src_buffer = dummy_mr.allocate(size=1024)
dst_buffer = dummy_mr.allocate(size=1024)
stream = device.create_stream()
if check:
src_ptr = ctypes.cast(src_buffer.handle, ctypes.POINTER(ctypes.c_byte))
for i in range(1024):
src_ptr[i] = ctypes.c_byte(i)
src_buffer.copy_to(dst_buffer, stream=stream)
device.sync()
if check:
dst_ptr = ctypes.cast(dst_buffer.handle, ctypes.POINTER(ctypes.c_byte))
for i in range(10):
assert dst_ptr[i] == src_ptr[i]
dst_buffer.close()
src_buffer.close()
def test_buffer_copy_to():
device = Device()
device.set_current()
buffer_copy_to(DummyDeviceMemoryResource(device), device)
buffer_copy_to(DummyUnifiedMemoryResource(device), device)
buffer_copy_to(DummyPinnedMemoryResource(device), device, check=True)
def buffer_copy_from(dummy_mr: MemoryResource, device, check=False):
src_buffer = dummy_mr.allocate(size=1024)
dst_buffer = dummy_mr.allocate(size=1024)
stream = device.create_stream()
if check:
src_ptr = ctypes.cast(src_buffer.handle, ctypes.POINTER(ctypes.c_byte))
for i in range(1024):
src_ptr[i] = ctypes.c_byte(i)
dst_buffer.copy_from(src_buffer, stream=stream)
device.sync()
if check:
dst_ptr = ctypes.cast(dst_buffer.handle, ctypes.POINTER(ctypes.c_byte))
for i in range(10):
assert dst_ptr[i] == src_ptr[i]
dst_buffer.close()
src_buffer.close()
def test_buffer_copy_from():
device = Device()
device.set_current()
buffer_copy_from(DummyDeviceMemoryResource(device), device)
buffer_copy_from(DummyUnifiedMemoryResource(device), device)
buffer_copy_from(DummyPinnedMemoryResource(device), device, check=True)
def buffer_close(dummy_mr: MemoryResource):
buffer = dummy_mr.allocate(size=1024)
buffer.close()
assert buffer.handle == 0
assert buffer.memory_resource is None
def test_buffer_close():
device = Device()
device.set_current()
buffer_close(DummyDeviceMemoryResource(device))
buffer_close(DummyHostMemoryResource())
buffer_close(DummyUnifiedMemoryResource(device))
buffer_close(DummyPinnedMemoryResource(device))
def test_buffer_dunder_dlpack():
device = Device()
device.set_current()
dummy_mr = DummyDeviceMemoryResource(device)
buffer = dummy_mr.allocate(size=1024)
capsule = buffer.__dlpack__()
assert "dltensor" in repr(capsule)
capsule = buffer.__dlpack__(max_version=(1, 0))
assert "dltensor" in repr(capsule)
with pytest.raises(BufferError, match=r"^Sorry, not supported: dl_device other than None$"):
buffer.__dlpack__(dl_device=[])
with pytest.raises(BufferError, match=r"^Sorry, not supported: copy=True$"):
buffer.__dlpack__(copy=True)
with pytest.raises(BufferError, match=r"^Expected max_version Tuple\[int, int\], got \[\]$"):
buffer.__dlpack__(max_version=[])
with pytest.raises(BufferError, match=r"^Expected max_version Tuple\[int, int\], got \(9, 8, 7\)$"):
buffer.__dlpack__(max_version=(9, 8, 7))
@pytest.mark.parametrize(
("DummyMR", "expected"),
[
(DummyDeviceMemoryResource, (DLDeviceType.kDLCUDA, 0)),
(DummyHostMemoryResource, (DLDeviceType.kDLCPU, 0)),
(DummyUnifiedMemoryResource, (DLDeviceType.kDLCUDAHost, 0)),
(DummyPinnedMemoryResource, (DLDeviceType.kDLCUDAHost, 0)),
],
)
def test_buffer_dunder_dlpack_device_success(DummyMR, expected):
device = Device()
device.set_current()
dummy_mr = DummyMR() if DummyMR is DummyHostMemoryResource else DummyMR(device)
buffer = dummy_mr.allocate(size=1024)
assert buffer.__dlpack_device__() == expected
def test_buffer_dunder_dlpack_device_failure():
dummy_mr = NullMemoryResource()
buffer = dummy_mr.allocate(size=1024)
with pytest.raises(BufferError, match=r"^buffer is neither device-accessible nor host-accessible$"):
buffer.__dlpack_device__()
def test_device_memory_resource_initialization():
"""Test that DeviceMemoryResource can be initialized successfully.
This test verifies that the DeviceMemoryResource initializes properly,
including the release threshold configuration for performance optimization.
"""
device = Device()
if not device.properties.memory_pools_supported:
pytest.skip("memory pools not supported")
device.set_current()
# This should succeed and configure the memory pool release threshold
mr = DeviceMemoryResource(device.device_id)
# Verify basic properties
assert mr.device_id == device.device_id
assert mr.is_device_accessible is True
assert mr.is_host_accessible is False
# Test allocation/deallocation works
buffer = mr.allocate(1024)
assert buffer.size == 1024
assert buffer.device_id == device.device_id
buffer.close()