Skip to content

[BUG]: Buffer.from_handle does not respect custom MemoryResource deallocation #1619

@stiepan

Description

@stiepan

Is this a duplicate?

Type of Bug

Silent Failure

Component

cuda.core

Describe the bug

Custom MemoryResource.deallocate is not called when Buffer instances created with Buffer.from_handle(mr=mr : MemoryResource) are destroyed.

Up to (incl.) the most recent 0.5.* release, Buffer instances created from_handle with memory resource instance specified, took ownership of the resource. I.e. when the Buffer instance was destroyed, the MemoryResource got a corresponding deallocate call. This no longer seem to be a case with the cuda-core built from the top-of main branch.

How to Reproduce

from cuda.core import MemoryResource, Buffer
import numpy as np


class DummyResource(MemoryResource):
    def __init__(self):
        self.active_allocs = {}

    def allocate(self, size, stream=None, logger=None) -> Buffer:
        a = np.zeros(size, dtype=np.int8)
        ptr = a.ctypes.data
        self.active_allocs[ptr] = a
        return Buffer.from_handle(ptr=ptr, size=size, mr=self)

    def deallocate(self, ptr, size, stream=None):
        del self.active_allocs[ptr]

    @property
    def is_device_accessible(self) -> bool:
        return True

    @property
    def is_host_accessible(self) -> bool:
        return False

    @property
    def device_id(self) -> int:
        raise RuntimeError("the resource is not bound to any GPU")


resource = DummyResource()
buffer = resource.allocate(10)
assert int(buffer.handle) in resource.active_allocs
assert len(resource.active_allocs) == 1
del buffer
assert len(resource.active_allocs) == 0  # in cuda-core <=0.5.* this calls resource.deallocate, but not top-of-tree cuda-core

Expected behavior

Expected behaviour is to keep what cuda-core did so far - allow user specify custom deallcation via MemoryResource deallocate method. That ownership semantics established some usage pattern for custom memory resources built on top of cuda-core. Changing Buffer.from_handle to always act as non-owining wrapper can lead to hidden bugs/leaks in code that relies on the pattern.

Operating System

No response

nvidia-smi output

No response

Metadata

Metadata

Assignees

Labels

P0High priority - Must do!bugSomething isn't workingcuda.coreEverything related to the cuda.core module

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions