[BUG]: Imported memory resources should take their location from the driver, not the caller
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
Start with _DMR_init_imported in cuda_core/cuda/core/_memory/_device_memory_resource.pyx and _ipc.MP_check_import_location, then _PMR_check_numa_id from the open PR #3530. Done means the driver-reported location is authoritative and a conflicting device_id raises ValueError, verified by a two-GPU test. The issue requires multi-GPU hardware and a driver 13.2+ to confirm the numbering assumption before any fix.
由索引模型根据 Issue 内容生成。
描述
Is this a duplicate?
- I confirmed there appear to be no duplicate issues for this bug and that I agree to the Code of Conduct
Type of Bug
Silent Failure
Component
cuda.core
Describe the bug
When a memory pool is imported from an allocation handle, the pool's location is a property of the pool: it is fixed by the exporter's cuMemPoolCreate, and cuMemPoolImportFromShareableHandle takes no device or location argument. On a CUDA 13 build with a 13.2+ driver, the driver reports that location for an imported pool through CU_MEMPOOL_ATTR_LOCATION_TYPE and CU_MEMPOOL_ATTR_LOCATION_ID. cuda.core already queries both attributes on import (_ipc.MP_check_import_location), but the two resource classes use the result differently, and neither treats it as the only source of truth.
DeviceMemoryResource ignores the queried ID. _DMR_init_imported in cuda_core/cuda/core/_memory/_device_memory_resource.pyx uses only the location type (to reject a host pool, #3502) and then assigns the caller's value unconditionally:
(<DeviceMemoryResource>mr)._dev_id = dev_id
The docstring of from_allocation_handle states: "The pool's device is not checked against device_id." A caller that passes the device it intends to use rather than the pool's owner gets a resource that reports the wrong device. For example, a process whose primary device is B imports a pool that lives on device A with DeviceMemoryResource.from_allocation_handle(B, handle). The docstring's description of device_id ("the device ... for which the memory resource is created") reasonably invites this. The resulting resource reports device_id == B, and the code that treats _dev_id as the pool's owner acts on the wrong GPU:
_peer_access_utils.pyxusesmr._dev_idas the owner (owner_id,owner_device_id=mr._dev_id), sopeer_accessible_by = {B}is skipped as "owner always has access", although B is exactly the device that needs remote access granted.- The peer check
Device(mr._dev_id).can_access_peer(dev_id)asks about B instead of A. Buffer.device_idand anything derived from it name the wrong GPU.Buffer._reduce_helperin_buffer.pyxcallsDevice(mr.device_id).set_current()when the unpickling process has no current context, so it creates a context on the wrong GPU.
PinnedMemoryResource takes numa_id from the queried loc.id (_PMR_init_imported in _pinned_memory_resource.pyx), which is correct. #3530 (open, fixing #3508) adds an optional numa_id parameter to from_allocation_handle and, when the driver reports the node, raises ValueError if the caller's value disagrees (_PMR_check_numa_id).
The pickle paths are not affected by the device bug: _deep_reduce_device_memory_resource sends the owner's UUID (#3097) and the receiver maps it to its own ordinal.
The device check was deferred in #3502 ("A device ID mismatch on DeviceMemoryResource.from_allocation_handle is not checked. It needs a two-GPU machine to verify how the driver reports the pool's device in the importing process"), but no issue was filed for it. #3508 also describes the device path as already solved because from_allocation_handle "takes the device explicitly"; the caller-supplied device is the problem described here.
How to Reproduce
Requires two GPUs. I have not run this; the description above comes from reading the code.
- Process 1: create
DeviceMemoryResource(0, DeviceMemoryResourceOptions(ipc_enabled=True, max_size=1 << 21))and sendmr.allocation_handleto process 2 (for example withmultiprocessing, spawn start method). - Process 2: call
DeviceMemoryResource.from_allocation_handle(1, handle). - Observe that the import succeeds with
device_id == 1, whilecuMemPoolGetAttribute(..., CU_MEMPOOL_ATTR_LOCATION_ID)on the imported pool is expected to report device 0 in process 2's numbering. - Repeat with process 2 under a reordered
CUDA_VISIBLE_DEVICES(for example1,0) and check which ordinalCU_MEMPOOL_ATTR_LOCATION_IDreports. This verifies the assumption that the driver reports the owner in the importer's numbering. For an independent reference, process 1, which created the pool, also sendsDevice(0).uuid, as themultiprocessingreduction of aDeviceMemoryResourcedoes. The reported ordinal must equal_device_from_uuid(uuid).device_idin process 2. - Repeat with process 2 under
CUDA_VISIBLE_DEVICESrestricted to the non-owner device and record whatCU_MEMPOOL_ATTR_LOCATION_TYPEandCU_MEMPOOL_ATTR_LOCATION_IDreport. The header documentsCU_MEM_LOCATION_TYPE_INVISIBLEwithCU_DEVICE_INVALIDfor this case. Then grant the visible device access withcuMemPoolSetAccess, import an allocation withcuMemPoolImportPointer, and read and write it from the visible device. This determines whether a pool with an invisible owner is usable at all. Also record whatcuPointerGetAttributereturns forCU_POINTER_ATTRIBUTE_DEVICE_ORDINALon the imported pointer.
Expected behavior
Scope: the case where the driver reports the pool's location (CUDA 13 build, 13.2+ driver), for both DeviceMemoryResource and PinnedMemoryResource. The case without the query is tracked in #3508 / #3530 for pinned resources; for device resources the caller's device_id remains the only source there.
Prerequisite: the proposal assumes that CU_MEMPOOL_ATTR_LOCATION_ID on an imported device pool is a device ordinal in the importing process's numbering. This must be confirmed on a two-GPU machine (step 4 above) before the fix is merged. If the driver reports the ID in any other numbering, it cannot be used as device_id and the proposal needs rework. The check also matters for the multiprocessing path: _reconstruct_device_memory_resource passes the device it finds by UUID to from_allocation_handle, so with the conflict check below, a numbering mismatch would make every such transfer under a reordered CUDA_VISIBLE_DEVICES raise ValueError.
Proposal:
- The driver's value is authoritative.
DeviceMemoryResourcetakes its device fromCU_MEMPOOL_ATTR_LOCATION_ID, andPinnedMemoryResourcekeeps taking its node from it. - The caller's value must match the driver's value. When the driver reports the location, a
device_id/numa_idthat disagrees with it raisesValueError, for both classes. This keeps the conflict check that #3530 adds fornuma_idand adds the same check fordevice_id. The argument then has one meaning in every configuration: it names the pool's owner, and it is verified whenever the driver can verify it. Ignoring a conflicting value instead would let a caller's mistake pass on a 13.2+ driver and produce a wrong owner on an older driver or a CUDA 12 build, where the caller's value is the only source. - A registry hit applies the same check. Today a registry hit returns the registered resource with its original device and ignores a different
device_idargument. It should compare the argument with the resource's device and raise on a mismatch, as #3530 does fornuma_id. device_id=Noneis accepted byDeviceMemoryResource.from_allocation_handle. The resource then takes its device from the driver. When the driver cannot report the location (a CUDA 12 build ofcuda.core, or a driver older than 13.2),Noneraises, because nothing else determines the device.- Locality-domain pools take the device from
localized.deviceId. ForCU_MEM_LOCATION_TYPE_DEVICE_LOCALITY_DOMAIN,CUmemLocationis a union ofidwithlocalized.deviceIdandlocalized.localityDomainId, and the documentation does not say whatCU_MEMPOOL_ATTR_LOCATION_IDreturns for such a pool. The low byte,localized.deviceId, is the device ordinal whether the attribute returns the plain ordinal or the union's value. The locality domain is available separately asCU_MEMPOOL_ATTR_LOCALITY_DOMAIN_ID. A device with a single locality domain reports such a pool asCU_MEM_LOCATION_TYPE_DEVICE, so this needs hardware with more than one locality domain to verify. - Docs for both
from_allocation_handlemethods state that the argument must name the pool's owning device or node, not the device that will access the memory, and that it is checked against the pool when the driver reports the pool's location. - Tests include a permanent multi-GPU test, skipped on machines with fewer than two GPUs. It sends a
DeviceMemoryResourcethroughmultiprocessingfrom the process that created the pool to a process under a reorderedCUDA_VISIBLE_DEVICES, and asserts that the imported resource'sdevice_idequals_device_from_uuid(uuid).device_idfor the exporter's device UUID.
Pools whose owner is not visible:
An imported device pool whose device is not visible to the importer reports CU_MEM_LOCATION_TYPE_INVISIBLE with CU_DEVICE_INVALID. No ordinal in the importing process names that device. Today the import accepts the caller's ordinal, which is necessarily wrong. The header describes the type as "Location is not visible but device is accessible", so the memory is expected to be usable from a visible device that has been granted access. Step 5 above must confirm this. If it does:
- The import succeeds with
device_id=None. Any ordinal the caller passes conflicts with the pool and raisesValueError, like every other mismatch. DeviceMemoryResource.device_idraisesRuntimeErrorfor such a resource, with a message that the owning device is not visible to this process. A sentinel value is not used:-1isCU_DEVICE_CPUand means host inBuffer.device_id.peer_accessible_byworks without an owner. No device is skipped as the owner, and theDevice.can_access_peercheck is omitted because it needs the owner's ordinal. An error fromcuMemPoolSetAccesspropagates.Buffer._reduce_helperuses the default device when the process has no current context, as it already does for a negativedevice_id.Buffer.device_idand the DLPack device ask the driver per pointer. A buffer from such a resource usesCU_POINTER_ATTRIBUTE_DEVICE_ORDINAL, the path that buffers without a memory resource already take, instead of the resource'sdevice_id. If step 5 shows that the driver returns no usable ordinal for such a pointer,Buffer.device_idraisesRuntimeErrorlike the resource.- Receiving through
multiprocessing:_reconstruct_device_memory_resourcepassesdevice_id=Nonewhen no visible device has the exporter's UUID, instead of raising in_device_from_uuid. Without the driver query it keeps raisingRuntimeError. - Sending on through
multiprocessing: a process that imported such a pool cannot query the owner's UUID. When it sends the resource, or aBufferfrom it, to a third process, the reduction sendsNonein place of the UUID. The receiver then imports withdevice_id=Noneand determines the device with its own driver query. A receiver without the driver query raisesRuntimeError. The receiver may see the owning device even though the sender does not. Forwarding an imported pool through a third process is not covered by a test today, also not for a visible owner, so the fix adds one.
If step 5 shows that the memory is not usable, DeviceMemoryResource.from_allocation_handle raises RuntimeError at import for such a pool instead.
Callers that pass a non-owner device_id today get a ValueError on 13.2+ drivers. The error is raised without a release that warns first, as #3530 does for numa_id: the affected calls already report the wrong device and break peer access. The release note should call the change out.
- 主要语言
- Cython
- 星标
- 3.4k
- 派生
- 353
- 平均合并
- 2 天 3 小时
- 30 天内合并 PR
- 163
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
NVIDIA/cuda-python 的其他 Issue
-
[BUG]: the `__cuda_stream__` deprecation warning points to `Stream(obj)`, which always raises可能已有人在做 @Iamsohungrynow 于 5 天前认领。 未关闭bug cuda.core P1
难度 1/5 1 小时以内 新手友好度 86/100
NVIDIA/cuda-python#3505 · 3 条评论 · 1 个 reaction · 已指派 1 人 ·
维护者通常 1 天内回复
-
[BUG]: ProcessInfo.used_gpu_memory returns the NVML_VALUE_NOT_AVAILABLE sentinel as a byte count未关闭triage
难度 2/5 1-3 小时 新手友好度 80/100
NVIDIA/cuda-python#3468 · 3 条评论 ·
维护者通常 1 天内回复
-
triage
难度 2/5 1-3 小时 新手友好度 68/100
NVIDIA/cuda-python#3409 ·
维护者通常 1 天内回复
-
[BUG]: cleanup-pr-previews never removes previews of deleted PRs可能重新可做 @rwgk 于 48 天前认领,目前没有进行中的 PR。 未关闭
难度 2/5 1-3 小时 新手友好度 84/100
NVIDIA/cuda-python#2952 · 已指派 1 人 ·
维护者通常 1 天内回复
-
[BUG]: LocatedHeaderDir is mutable, so callers can poison the cached header-directory lookup可能已有人在做 @rwgk 于 62 天前认领。 未关闭triage
难度 2/5 1-3 小时 新手友好度 82/100
NVIDIA/cuda-python#2646 · 1 个 reaction ·
维护者通常 1 天内回复
查看 NVIDIA/cuda-python 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
area: ui-kit type: chore
难度 1/5 1 小时以内 新手友好度 82/100
schlessera/brain-kit#4598 ·
维护者通常 1 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 72/100
mloda-ai/mloda-plugin-template#206 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 62/100
维护者通常 1 天内回复
-
Beginner Enhancement
难度 2/5 1-3 小时 新手友好度 62/100
JSPrismarine/JSPrismarine#3740 ·
维护者通常 1 天内回复