Python class
DevicePinnedBuffer
DevicePinnedBuffer
class max.driver.DevicePinnedBuffer(self, dtype: max.dtype.DType, shape: collections.abc.Sequence[int], device: max.driver.Device)
class max.driver.DevicePinnedBuffer(self, dtype: max.dtype.DType, shape: collections.abc.Sequence[int], stream: max.driver.DeviceStream)
Bases: Buffer
Creates a pinned host memory allocation tied to the given device.
Device-pinned memory allocations can provide faster DMA speeds and allow properly asynchronous copies between the device and the host.
Since device-pinned buffers can be used for asynchronous copies they don’t perform automatic synchronizations in operations like to_numpy, so synchronization should be handled manually to ensure GPU tasks writing to the buffer are complete before reading it on the host.
from max.driver import DevicePinnedBuffer, Accelerator
from max.dtype import DType
import numpy as np
device = Accelerator()
buffer = DevicePinnedBuffer(
dtype=DType.float32, shape=[1024], device=device
)
# Fill with data and transfer to GPU
np_data = buffer.to_numpy()
np_data[:] = np.arange(1024, dtype=np.float32)
gpu_buffer = buffer.to(device)-
Parameters:
-
- dtype (DType) – Data type of buffer elements.
- shape (Sequence[int]) – Tuple of positive, non-zero integers denoting the buffer shape.
- device (Device) – GPU/Accelerator device to associate buffer with. Must not be CPU.
- stream (DeviceStream, optional) – Stream to associate the buffer with.
-
Raises:
-
ValueError – If is a CPU device.
zeros
zeros = <nanobind.nb_func object>
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!