-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathconftest.py
More file actions
232 lines (177 loc) · 7 KB
/
conftest.py
File metadata and controls
232 lines (177 loc) · 7 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
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
import multiprocessing
import os
import helpers
import pytest
try:
from cuda.bindings import driver
except ImportError:
from cuda import cuda as driver
import cuda.core
from cuda.core import (
Device,
DeviceMemoryResource,
DeviceMemoryResourceOptions,
ManagedMemoryResource,
ManagedMemoryResourceOptions,
PinnedMemoryResource,
PinnedMemoryResourceOptions,
_device,
)
from cuda.core._utils.cuda_utils import handle_return
def skip_if_pinned_memory_unsupported(device):
try:
if not device.properties.host_memory_pools_supported:
pytest.skip("Device does not support host mempool operations")
except AttributeError:
pytest.skip("PinnedMemoryResource requires CUDA 13.0 or later")
def skip_if_managed_memory_unsupported(device):
try:
if not device.properties.memory_pools_supported or not device.properties.concurrent_managed_access:
pytest.skip("Device does not support managed memory pool operations")
except AttributeError:
pytest.skip("ManagedMemoryResource requires CUDA 13.0 or later")
def create_managed_memory_resource_or_skip(*args, **kwargs):
try:
return ManagedMemoryResource(*args, **kwargs)
except RuntimeError as e:
if "requires CUDA 13.0" in str(e):
pytest.skip("ManagedMemoryResource requires CUDA 13.0 or later")
raise
@pytest.fixture(scope="session", autouse=True)
def session_setup():
# Always init CUDA.
handle_return(driver.cuInit(0))
# Never fork processes.
multiprocessing.set_start_method("spawn", force=True)
@pytest.fixture(scope="function")
def init_cuda():
# TODO: rename this to e.g. init_context
device = Device(0)
device.set_current()
# Set option to avoid spin-waiting on synchronization.
if int(os.environ.get("CUDA_CORE_TEST_BLOCKING_SYNC", 0)) != 0:
handle_return(
driver.cuDevicePrimaryCtxSetFlags(device.device_id, driver.CUctx_flags.CU_CTX_SCHED_BLOCKING_SYNC)
)
yield device
_ = _device_unset_current()
def _device_unset_current() -> bool:
"""Pop current CUDA context.
Returns True if context was popped, False it the stack was empty.
"""
ctx = handle_return(driver.cuCtxGetCurrent())
if int(ctx) == 0:
# no active context, do nothing
return False
handle_return(driver.cuCtxPopCurrent())
if hasattr(_device._tls, "devices"):
del _device._tls.devices
return True
@pytest.fixture(scope="function")
def deinit_cuda():
# TODO: rename this to e.g. deinit_context
yield
_ = _device_unset_current()
@pytest.fixture(scope="function")
def deinit_all_contexts_function():
def pop_all_contexts():
max_iters = 256
for _ in range(max_iters):
if _device_unset_current():
# context was popped, continue until stack is empty
continue
# no active context, we are ready
break
else:
raise RuntimeError(f"Number of iterations popping current CUDA contexts, exceded {max_iters}")
return pop_all_contexts
@pytest.fixture
def ipc_device():
"""Obtains a device suitable for IPC-enabled mempool tests, or skips."""
# Check if IPC is supported on this platform/device
device = Device(0)
device.set_current()
if not device.properties.memory_pools_supported:
pytest.skip("Device does not support mempool operations")
# Note: Linux specific. Once Windows support for IPC is implemented, this
# test should be updated.
if not device.properties.handle_type_posix_file_descriptor_supported:
pytest.skip("Device does not support IPC")
# Skip on WSL or if driver rejects IPC-enabled mempool creation on this platform/device
if helpers.IS_WSL or not helpers.supports_ipc_mempool(device):
pytest.skip("Driver rejects IPC-enabled mempool creation on this platform")
return device
@pytest.fixture(
params=[
pytest.param("device", id="DeviceMR"),
pytest.param("pinned", id="PinnedMR"),
]
)
def ipc_memory_resource(request, ipc_device):
"""Provides IPC-enabled memory resource (either Device or Pinned)."""
POOL_SIZE = 2097152
mr_type = request.param
if mr_type == "device":
options = DeviceMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True)
mr = DeviceMemoryResource(ipc_device, options=options)
else: # pinned
skip_if_pinned_memory_unsupported(ipc_device)
options = PinnedMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True)
mr = PinnedMemoryResource(options=options)
assert mr.is_ipc_enabled
return mr
@pytest.fixture
def mempool_device():
"""Obtains a device suitable for mempool tests, or skips."""
device = Device(0)
device.set_current()
if not device.properties.memory_pools_supported:
pytest.skip("Device does not support mempool operations")
return device
def _mempool_device_impl(num):
num_devices = len(cuda.core.Device.get_all_devices())
if num_devices < num:
pytest.skip(f"Test requires at least {num} GPUs")
devs = [Device(i) for i in range(num)]
for i in reversed(range(num)):
devs[i].set_current() # ends with device 0 current
if not all(devs[i].can_access_peer(j) for i in range(num) for j in range(num)):
pytest.skip("Test requires GPUs with peer access")
if not all(devs[i].properties.memory_pools_supported for i in range(num)):
pytest.skip("Device does not support mempool operations")
return devs
@pytest.fixture
def mempool_device_x2():
"""Fixture that provides two devices if available, otherwise skips test."""
yield _mempool_device_impl(2)
_device_unset_current()
@pytest.fixture
def mempool_device_x3():
"""Fixture that provides three devices if available, otherwise skips test."""
yield _mempool_device_impl(3)
_device_unset_current()
@pytest.fixture(
params=[
pytest.param((DeviceMemoryResource, DeviceMemoryResourceOptions), id="DeviceMR"),
pytest.param((PinnedMemoryResource, PinnedMemoryResourceOptions), id="PinnedMR"),
pytest.param((ManagedMemoryResource, ManagedMemoryResourceOptions), id="ManagedMR"),
]
)
def memory_resource_factory(request, init_cuda):
"""Parametrized fixture providing memory resource types.
Returns a 2-tuple of (MRClass, MROptionClass).
Usage:
def test_something(memory_resource_factory):
MRClass, MROptions = memory_resource_factory
device = Device()
if MRClass is DeviceMemoryResource:
mr = MRClass(device)
elif MRClass is PinnedMemoryResource:
mr = MRClass()
elif MRClass is ManagedMemoryResource:
mr = MRClass()
"""
return request.param
skipif_need_cuda_headers = pytest.mark.skipif(helpers.CUDA_INCLUDE_PATH is None, reason="need CUDA header")