Skip to content

Commit 57a7a0b

Browse files
committed
fix(core): compare CUdeviceptr as int so the VMM grow fast path can run
`modify_allocation()` asks `cuMemAddressReserve` for the address immediately after the current range, then checks whether the driver granted that exact address before taking the fast path. `cuMemAddressReserve` returns `new_ptr` as a `CUdeviceptr`. That type defines `__int__` and `__repr__` but no `__eq__` or `__richcmp__`, so `new_ptr != <int>` falls back to identity comparison and is unconditionally true. The guard is therefore always taken and `_grow_allocation_fast_path` is unreachable: every grow runs the slow path, re-reserving the whole range and remapping it, so the buffer's base pointer changes even when a contiguous extension was available. Convert with `int()` before comparing. Reported as defect 2 in NVIDIA#2388. `test_vmm_allocator_grow_dispatches_to_fast_path` stubs `cuMemAddressReserve` to grant the requested address and asserts the dispatch reaches the fast path. It fails on the current code, which records "slow". `test_vmm_allocator_grow_allocation_fast_path` already exercised `_grow_allocation_fast_path` directly, so the helper itself is covered; what was missing was coverage of the dispatch decision that reaches it. Note this makes a previously unreachable code path live, so it is a behavior change rather than a pure cleanup: successful contiguous grows now preserve the base pointer instead of moving it. Signed-off-by: Aryan Putta <[email protected]>
1 parent 76bac4e commit 57a7a0b

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

‎cuda_core/cuda/core/_memory/_virtual_memory_resource.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ def modify_allocation(
265265
0,
266266
)
267267

268-
if res != driver.CUresult.CUDA_SUCCESS or new_ptr != (int(buf.handle) + aligned_prev_size):
268+
if res != driver.CUresult.CUDA_SUCCESS or int(new_ptr) != (int(buf.handle) + aligned_prev_size):
269269
# Check for specific errors that are not recoverable with the slow path
270270
if res in (
271271
driver.CUresult.CUDA_ERROR_INVALID_VALUE,

‎cuda_core/tests/test_memory.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,53 @@ def __init__(self, size):
10351035
assert ("set_access", new_ptr, aligned_additional, 1) in calls
10361036

10371037

1038+
@pytest.mark.agent_authored(model="claude-opus-4-8")
1039+
def test_vmm_allocator_grow_dispatches_to_fast_path(init_cuda, monkeypatch):
1040+
"""A contiguous reservation must dispatch to the fast path, not the slow path.
1041+
1042+
`test_vmm_allocator_grow_allocation_fast_path` covers the fast path itself by
1043+
calling it directly, so it stays green whether or not `modify_allocation` can
1044+
reach it. This covers the dispatch decision instead.
1045+
"""
1046+
device = Device()
1047+
if not device.properties.virtual_memory_management_supported:
1048+
pytest.skip("Virtual memory management is not supported on this device")
1049+
1050+
vmm_mr = VirtualMemoryResource(
1051+
device,
1052+
config=VirtualMemoryResourceOptions(handle_type="win32_kmt" if IS_WINDOWS else "posix_fd"),
1053+
)
1054+
try:
1055+
buffer = vmm_mr.allocate(2 * 1024 * 1024)
1056+
except NotImplementedError:
1057+
pytest.skip("handle_type not implemented on this platform")
1058+
1059+
# Grant the reservation at exactly the address the caller asked for, which is
1060+
# the contiguous-extension case the fast path exists to serve.
1061+
def fake_addr_reserve(size, alignment, addr, flags):
1062+
return (driver.CUresult.CUDA_SUCCESS, driver.CUdeviceptr(addr))
1063+
1064+
chosen = []
1065+
1066+
def record_fast(*_args, **_kwargs):
1067+
chosen.append("fast")
1068+
return buffer
1069+
1070+
def record_slow(*_args, **_kwargs):
1071+
chosen.append("slow")
1072+
return buffer
1073+
1074+
monkeypatch.setattr(driver, "cuMemAddressReserve", fake_addr_reserve)
1075+
monkeypatch.setattr(vmm_mr, "_grow_allocation_fast_path", record_fast)
1076+
monkeypatch.setattr(vmm_mr, "_grow_allocation_slow_path", record_slow)
1077+
1078+
try:
1079+
vmm_mr.modify_allocation(buffer, 4 * 1024 * 1024)
1080+
assert chosen == ["fast"]
1081+
finally:
1082+
buffer.close()
1083+
1084+
10381085
def test_vmm_allocator_rdma_unsupported_exception():
10391086
"""Test that VirtualMemoryResource throws an exception when RDMA is requested but device doesn't support it.
10401087

0 commit comments

Comments
 (0)