-
Notifications
You must be signed in to change notification settings - Fork 201
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DOC] Document memory resource lifetime requirements when taking ownership of device buffers in Python #1492
Comments
For a cudf-specific example and discussion, see rapidsai/cudf#14229 |
You can't inspect the provenance after the fact, no, but you control what resource is used to construct the buffer in the first place, yes? A sketch of what I mean:
This enables you to have the effective equivalent of if |
We do, although we don't currently use what you propose (or a moral equivalent thereof) in cudf-python for (I presume) historical reasons. However, since we're continuing to advocate for rmm as a cross-library allocator, we need to make these requirements (I think) clearer in the docs than they are currently. |
On the C++ side, device_buffers store raw pointers for the memory resource that was used in their allocation. Consequently, it is unsafe to take ownership of a device_buffer in Python unless we controlled the provenance of the memory resource that was used in its allocation. The only way to do that is to pass the memory resource from Python into C++ and then use it when constructing the DeviceBuffer. Add discussion of this with some examples and a section on pitfalls if only relying on get_current_device_resource and set_current_device_resource. - Closes rapidsai#1492
On the C++ side, device_buffers store raw pointers for the memory resource that was used in their allocation. Consequently, it is unsafe to take ownership of a device_buffer in Python unless we controlled the provenance of the memory resource that was used in its allocation. The only way to do that is to pass the memory resource from Python into C++ and then use it when constructing the DeviceBuffer. Add discussion of this with some examples and a section on pitfalls if only relying on get_current_device_resource and set_current_device_resource. - Closes rapidsai#1492
On the C++ side, device_buffers store raw pointers for the memory resource that was used in their allocation. Consequently, it is unsafe to take ownership of a device_buffer in Python unless we controlled the provenance of the memory resource that was used in its allocation. The only way to do that is to pass the memory resource from Python into C++ and then use it when constructing the DeviceBuffer. Add discussion of this with some examples and a section on pitfalls if only relying on get_current_device_resource and set_current_device_resource. - Closes rapidsai#1492
…hip requirements in Python/C++ interfacing (#1552) On the C++ side, device_buffers store raw pointers for the memory resource that was used in their allocation. Consequently, it is unsafe to take ownership of a device_buffer in Python unless we controlled the provenance of the memory resource that was used in its allocation. The only way to do that is to pass the memory resource from Python into C++ and then use it when constructing the DeviceBuffer. Add discussion of this with some examples and a section on pitfalls if only relying on get_current_device_resource and set_current_device_resource. To allow Python users of `DeviceBuffer` objects to follow best practices, introduce explicit (defaulting to the current device resource) `mr` arguments in both `c_from_unique_ptr` and the `DeviceBuffer` constructor. - Closes #1492 Authors: - Lawrence Mitchell (https://github.com/wence-) Approvers: - Mark Harris (https://github.com/harrism) - Vyas Ramasubramani (https://github.com/vyasr) URL: #1552
An
rmm::device_buffer
holds a (effectively) raw pointer to the memory resource used for its allocation and deallocation. On the C++ side, it is the responsibility of the constructor adevice_buffer
to ensure that the memory resource lives at least as long as the buffer.When we expose a
device_buffer
to Python through the cythonDeviceBuffer
class (specifically, when we useDeviceBuffer.c_from_unique_ptr
which is used in many places in cudf), we have no way of inspecting the provenance of the memory resource used to allocate that buffer, so we have no way of keeping it alive.Right now, the way we handle this in the cython wrappers is to throw our hands up in the air, grab the
current_device_resource
, cross our fingers and hope.This has worked so far because usually no-one switches out a memory resource from under us, and generally speaking in cudf we're taking ownership of buffers that have ultimately been allocated on the python side, but it's not actually safe.
Unless we're willing to make the memory resource slot in a device buffer hold a smart pointer, and I believe there are good reasons why we do not do that at the moment, the best we can do is document the requirements on the Python side and then obey them when using RMM from cudf.
The text was updated successfully, but these errors were encountered: