[BUG]: Imported memory resources should take their location from the driver, not the caller

Aberta
#3,615 0 comentários 0 reações 0 responsáveis Ver no GitHub

Mantenedores costumam responder em até 1 dia

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
5/5
Tempo estimado
Mais de uma semana
Facilidade para iniciantes
8/100
Tipo de issue
Bug
Clareza
Razoavelmente clara
Status de atividade
Ativa
Stack de tecnologia
python
Domínio
devtools

Direção de pesquisa

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.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

bug cuda.core
Is this a duplicate?
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.pyx uses mr._dev_id as the owner (owner_id, owner_device_id=mr._dev_id), so peer_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_id and anything derived from it name the wrong GPU.
  • Buffer._reduce_helper in _buffer.pyx calls Device(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.

  1. Process 1: create DeviceMemoryResource(0, DeviceMemoryResourceOptions(ipc_enabled=True, max_size=1 << 21)) and send mr.allocation_handle to process 2 (for example with multiprocessing, spawn start method).
  2. Process 2: call DeviceMemoryResource.from_allocation_handle(1, handle).
  3. Observe that the import succeeds with device_id == 1, while cuMemPoolGetAttribute(..., CU_MEMPOOL_ATTR_LOCATION_ID) on the imported pool is expected to report device 0 in process 2's numbering.
  4. Repeat with process 2 under a reordered CUDA_VISIBLE_DEVICES (for example 1,0) and check which ordinal CU_MEMPOOL_ATTR_LOCATION_ID reports. 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 sends Device(0).uuid, as the multiprocessing reduction of a DeviceMemoryResource does. The reported ordinal must equal _device_from_uuid(uuid).device_id in process 2.
  5. Repeat with process 2 under CUDA_VISIBLE_DEVICES restricted to the non-owner device and record what CU_MEMPOOL_ATTR_LOCATION_TYPE and CU_MEMPOOL_ATTR_LOCATION_ID report. The header documents CU_MEM_LOCATION_TYPE_INVISIBLE with CU_DEVICE_INVALID for this case. Then grant the visible device access with cuMemPoolSetAccess, import an allocation with cuMemPoolImportPointer, and read and write it from the visible device. This determines whether a pool with an invisible owner is usable at all. Also record what cuPointerGetAttribute returns for CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL on 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. DeviceMemoryResource takes its device from CU_MEMPOOL_ATTR_LOCATION_ID, and PinnedMemoryResource keeps 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_id that disagrees with it raises ValueError, for both classes. This keeps the conflict check that #3530 adds for numa_id and adds the same check for device_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_id argument. It should compare the argument with the resource's device and raise on a mismatch, as #3530 does for numa_id.
  • device_id=None is accepted by DeviceMemoryResource.from_allocation_handle. The resource then takes its device from the driver. When the driver cannot report the location (a CUDA 12 build of cuda.core, or a driver older than 13.2), None raises, because nothing else determines the device.
  • Locality-domain pools take the device from localized.deviceId. For CU_MEM_LOCATION_TYPE_DEVICE_LOCALITY_DOMAIN, CUmemLocation is a union of id with localized.deviceId and localized.localityDomainId, and the documentation does not say what CU_MEMPOOL_ATTR_LOCATION_ID returns 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 as CU_MEMPOOL_ATTR_LOCALITY_DOMAIN_ID. A device with a single locality domain reports such a pool as CU_MEM_LOCATION_TYPE_DEVICE, so this needs hardware with more than one locality domain to verify.
  • Docs for both from_allocation_handle methods 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 DeviceMemoryResource through multiprocessing from the process that created the pool to a process under a reordered CUDA_VISIBLE_DEVICES, and asserts that the imported resource's device_id equals _device_from_uuid(uuid).device_id for 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 raises ValueError, like every other mismatch.
  • DeviceMemoryResource.device_id raises RuntimeError for such a resource, with a message that the owning device is not visible to this process. A sentinel value is not used: -1 is CU_DEVICE_CPU and means host in Buffer.device_id.
  • peer_accessible_by works without an owner. No device is skipped as the owner, and the Device.can_access_peer check is omitted because it needs the owner's ordinal. An error from cuMemPoolSetAccess propagates.
  • Buffer._reduce_helper uses the default device when the process has no current context, as it already does for a negative device_id.
  • Buffer.device_id and the DLPack device ask the driver per pointer. A buffer from such a resource uses CU_POINTER_ATTRIBUTE_DEVICE_ORDINAL, the path that buffers without a memory resource already take, instead of the resource's device_id. If step 5 shows that the driver returns no usable ordinal for such a pointer, Buffer.device_id raises RuntimeError like the resource.
  • Receiving through multiprocessing: _reconstruct_device_memory_resource passes device_id=None when no visible device has the exporter's UUID, instead of raising in _device_from_uuid. Without the driver query it keeps raising RuntimeError.
  • Sending on through multiprocessing: a process that imported such a pool cannot query the owner's UUID. When it sends the resource, or a Buffer from it, to a third process, the reduction sends None in place of the UUID. The receiver then imports with device_id=None and determines the device with its own driver query. A receiver without the driver query raises RuntimeError. 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.

Linguagem predominante
Cython
Estrelas
3.4k
Forks
353
Merge médio
2d 3h
PRs com merge (30d)
163

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de NVIDIA/cuda-python

Todas as issues de NVIDIA/cuda-python

Issues semelhantes

Mais issues de DevTools

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.