|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import multiprocessing |
| 5 | +import pickle |
| 6 | +import re |
| 7 | + |
| 8 | +from cuda.core.experimental import Buffer, Device, DeviceMemoryResource, DeviceMemoryResourceOptions |
| 9 | +from cuda.core.experimental._utils.cuda_utils import CUDAError |
| 10 | + |
| 11 | +CHILD_TIMEOUT_SEC = 20 |
| 12 | +NBYTES = 64 |
| 13 | +POOL_SIZE = 2097152 |
| 14 | + |
| 15 | + |
| 16 | +class ChildErrorHarness: |
| 17 | + """Test harness for checking errors in child processes. Subclasses override |
| 18 | + PARENT_ACTION, CHILD_ACTION, and ASSERT (see below for examples).""" |
| 19 | + |
| 20 | + def test_main(self, ipc_device, ipc_memory_resource): |
| 21 | + """Parent process that checks child errors.""" |
| 22 | + # Attach fixtures to this object for convenience. These can be accessed |
| 23 | + # from PARENT_ACTION. |
| 24 | + self.device = ipc_device |
| 25 | + self.mr = ipc_memory_resource |
| 26 | + |
| 27 | + # Start a child process to generate error info. |
| 28 | + pipe = [multiprocessing.Queue() for _ in range(2)] |
| 29 | + process = multiprocessing.Process(target=self.child_main, args=(pipe, self.device, self.mr)) |
| 30 | + process.start() |
| 31 | + |
| 32 | + # Interact. |
| 33 | + self.PARENT_ACTION(pipe[0]) |
| 34 | + |
| 35 | + # Check the error. |
| 36 | + exc_type, exc_msg = pipe[1].get(timeout=CHILD_TIMEOUT_SEC) |
| 37 | + self.ASSERT(exc_type, exc_msg) |
| 38 | + |
| 39 | + # Wait for the child process. |
| 40 | + process.join(timeout=CHILD_TIMEOUT_SEC) |
| 41 | + assert process.exitcode == 0 |
| 42 | + |
| 43 | + def child_main(self, pipe, device, mr): |
| 44 | + """Child process that pushes IPC errors to a shared pipe for testing.""" |
| 45 | + self.device = device |
| 46 | + self.device.set_current() |
| 47 | + self.mr = mr |
| 48 | + try: |
| 49 | + self.CHILD_ACTION(pipe[0]) |
| 50 | + except Exception as e: |
| 51 | + exc_info = type(e), str(e) |
| 52 | + else: |
| 53 | + exc_info = None, None |
| 54 | + pipe[1].put(exc_info) |
| 55 | + |
| 56 | + |
| 57 | +class TestAllocFromImportedMr(ChildErrorHarness): |
| 58 | + """Error when attempting to allocate from an import memory resource.""" |
| 59 | + |
| 60 | + def PARENT_ACTION(self, queue): |
| 61 | + queue.put(self.mr) |
| 62 | + |
| 63 | + def CHILD_ACTION(self, queue): |
| 64 | + mr = queue.get(timeout=CHILD_TIMEOUT_SEC) |
| 65 | + mr.allocate(NBYTES) |
| 66 | + |
| 67 | + def ASSERT(self, exc_type, exc_msg): |
| 68 | + assert exc_type is TypeError |
| 69 | + assert exc_msg == "Cannot allocate from a mapped IPC-enabled memory resource" |
| 70 | + |
| 71 | + |
| 72 | +class TestImportWrongMR(ChildErrorHarness): |
| 73 | + """Error when importing a buffer from the wrong memory resource.""" |
| 74 | + |
| 75 | + def PARENT_ACTION(self, queue): |
| 76 | + options = DeviceMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True) |
| 77 | + mr2 = DeviceMemoryResource(self.device, options=options) |
| 78 | + buffer = mr2.allocate(NBYTES) |
| 79 | + queue.put([self.mr, buffer.get_ipc_descriptor()]) # Note: mr does not own this buffer |
| 80 | + |
| 81 | + def CHILD_ACTION(self, queue): |
| 82 | + mr, buffer_desc = queue.get(timeout=CHILD_TIMEOUT_SEC) |
| 83 | + Buffer.from_ipc_descriptor(mr, buffer_desc) |
| 84 | + |
| 85 | + def ASSERT(self, exc_type, exc_msg): |
| 86 | + assert exc_type is CUDAError |
| 87 | + assert "CUDA_ERROR_INVALID_VALUE" in exc_msg |
| 88 | + |
| 89 | + |
| 90 | +class TestExportImportedMR(ChildErrorHarness): |
| 91 | + """Error when exporting a memory resource that was imported.""" |
| 92 | + |
| 93 | + def PARENT_ACTION(self, queue): |
| 94 | + queue.put(self.mr) |
| 95 | + |
| 96 | + def CHILD_ACTION(self, queue): |
| 97 | + mr = queue.get(timeout=CHILD_TIMEOUT_SEC) |
| 98 | + mr.get_allocation_handle() |
| 99 | + |
| 100 | + def ASSERT(self, exc_type, exc_msg): |
| 101 | + assert exc_type is RuntimeError |
| 102 | + assert exc_msg == "Imported memory resource cannot be exported" |
| 103 | + |
| 104 | + |
| 105 | +class TestImportBuffer(ChildErrorHarness): |
| 106 | + """Error when using a buffer as a buffer descriptor.""" |
| 107 | + |
| 108 | + def PARENT_ACTION(self, queue): |
| 109 | + # Note: if the buffer is not attached to something to prolong its life, |
| 110 | + # CUDA_ERROR_INVALID_CONTEXT is raised from Buffer.__del__ |
| 111 | + self.buffer = self.mr.allocate(NBYTES) |
| 112 | + queue.put(self.buffer) |
| 113 | + |
| 114 | + def CHILD_ACTION(self, queue): |
| 115 | + buffer = queue.get(timeout=CHILD_TIMEOUT_SEC) |
| 116 | + Buffer.from_ipc_descriptor(self.mr, buffer) |
| 117 | + |
| 118 | + def ASSERT(self, exc_type, exc_msg): |
| 119 | + assert exc_type is TypeError |
| 120 | + assert exc_msg.startswith("Argument 'ipc_buffer' has incorrect type") |
| 121 | + |
| 122 | + |
| 123 | +class TestDanglingBuffer(ChildErrorHarness): |
| 124 | + """ |
| 125 | + Error when importing a buffer object without registering its memory |
| 126 | + resource. |
| 127 | + """ |
| 128 | + |
| 129 | + def PARENT_ACTION(self, queue): |
| 130 | + options = DeviceMemoryResourceOptions(max_size=POOL_SIZE, ipc_enabled=True) |
| 131 | + mr2 = DeviceMemoryResource(self.device, options=options) |
| 132 | + self.buffer = mr2.allocate(NBYTES) |
| 133 | + buffer_s = pickle.dumps(self.buffer) # noqa: S301 |
| 134 | + queue.put(buffer_s) # Note: mr2 not sent |
| 135 | + |
| 136 | + def CHILD_ACTION(self, queue): |
| 137 | + Device().set_current() |
| 138 | + buffer_s = queue.get(timeout=CHILD_TIMEOUT_SEC) |
| 139 | + pickle.loads(buffer_s) # noqa: S301 |
| 140 | + |
| 141 | + def ASSERT(self, exc_type, exc_msg): |
| 142 | + assert exc_type is RuntimeError |
| 143 | + assert re.match(r"Memory resource [a-z0-9-]+ was not found", exc_msg) |
0 commit comments