Python class
Buffer
Bufferβ
class max.driver.Buffer(self, dtype: max.dtype.DType, shape: collections.abc.Sequence[int], device: max.driver.Device | None = None, pinned: bool = False)
class max.driver.Buffer(self, dtype: max.dtype.DType, shape: collections.abc.Sequence[int], stream: max.driver.DeviceStream, pinned: bool = False)
class max.driver.Buffer(self, shape: ndarray[writable=False], device: max.driver.Device)
Bases: object
Device-resident buffer representation.
Allocates memory onto a given device with the provided shape and dtype. Buffers can be sliced to provide strided views of the underlying memory, but any buffers input into model execution must be contiguous.
Supports numpy-style slicing but does not currently support setting items across multiple indices.
from max import driver
from max.dtype import DType
cpu_buffer = driver.Buffer(shape=[2, 3], dtype=DType.float32)
# Create a buffer on GPU
gpu = driver.Accelerator()
gpu_buffer = driver.Buffer(shape=[2, 3], dtype=DType.float32, device=gpu)-
Parameters:
-
- dtype (DType) β Data type of buffer elements.
- shape (Sequence[int]) β Tuple of positive, non-zero integers denoting the buffer shape.
- device (Device, optional) β Device to allocate buffer onto. Defaults to the CPU.
- pinned (bool, optional) β If True, memory is page-locked (pinned). Defaults to False.
- stream (DeviceStream, optional) β Stream to associate the buffer with.
contiguous()β
contiguous()
Creates a contiguous copy of the parent buffer.
copy()β
copy(self, stream: max.driver.DeviceStream) β max.driver.Buffer
copy(self, device: max.driver.Device | None = None) β max.driver.Buffer
Overloaded function.
-
copy(self, stream: max.driver.DeviceStream) -> max.driver.BufferCreates a deep copy on the device associated with the stream.
- Args:
- stream (DeviceStream): The stream to associate the new buffer with.
- Returns:
- Buffer: A new buffer that is a copy of this buffer.
-
copy(self, device: max.driver.Device | None = None) -> max.driver.BufferCreates a deep copy on an optionally given device.
If device is None (default), a copy is created on the same device.
from max import driver from max.dtype import DType β cpu_buffer = driver.Buffer(shape=[2, 3], dtype=DType.bfloat16, device=driver.CPU()) cpu_copy = cpu_buffer.copy() β # Copy to GPU gpu = driver.Accelerator() gpu_copy = cpu_buffer.copy(device=gpu)- Args:
- device (Device, optional): The device to create the copy on.
- Defaults to None (same device).
- Returns:
- Buffer: A new buffer that is a copy of this buffer.
deviceβ
property device
Device on which tensor is resident.
dtypeβ
property dtype
DType of constituent elements in tensor.
element_sizeβ
property element_size
Return the size of the element type in bytes.
from_dlpack()β
from_dlpack(*, copy=None)
Create a buffer from an object implementing the dlpack protocol.
This usually does not result in a copy, and the producer of the object retains ownership of the underlying memory.
from_numpy()β
from_numpy()
Creates a buffer from a provided numpy array on the host device.
The underlying data is not copied unless the array is noncontiguous. If it is, a contiguous copy will be returned.
inplace_copy_from()β
inplace_copy_from(src)
Copy the contents of another buffer into this one.
These buffers may be on different devices. Requires that both buffers are contiguous and have same size.
is_contiguousβ
property is_contiguous
Whether or not buffer is contiguously allocated in memory.
Returns false if the buffer is a non-contiguous slice.
Currently, we consider certain situations that are contiguous as non-contiguous for the purposes of our engine, such as when a buffer has negative steps.
is_hostβ
property is_host
Whether or not buffer is host-resident.
Returns false for GPU buffers, true for CPU buffers.
from max import driver
from max.dtype import DType
cpu_buffer = driver.Buffer(shape=[2, 3], dtype=DType.bfloat16, device=driver.CPU())
print(cpu_buffer.is_host)item()β
item(self) β Any
Returns the scalar value at a given location.
Currently implemented only for zero-rank buffers. The return type is converted to a Python built-in type.
mmap()β
mmap(dtype, shape, mode='copyonwrite', offset=0)
num_elementsβ
property num_elements
Returns the number of elements in this buffer.
Rank-0 buffers have 1 element by convention.
pinnedβ
property pinned
Whether or not the underlying memory is pinned (page-locked).
rankβ
property rank
Buffer rank.
scalarβ
scalar = <nanobind.nb_func object>
shapeβ
property shape
Shape of buffer.
streamβ
property stream
Stream to which tensor is bound.
to()β
to(self, device: max.driver.Device) β max.driver.Buffer
to(self, stream: max.driver.DeviceStream) β max.driver.Buffer
to(self, devices: collections.abc.Sequence[max.driver.Device]) β list[max.driver.Buffer]
to(self, streams: collections.abc.Sequence[max.driver.DeviceStream]) β list[max.driver.Buffer]
Overloaded function.
-
to(self, device: max.driver.Device) -> max.driver.BufferReturns a buffer thatβs guaranteed to be on the given device.
The buffer is only copied if the requested device is different from the device upon which the buffer is already resident.
-
to(self, stream: max.driver.DeviceStream) -> max.driver.BufferReturns a buffer thatβs guaranteed to be on the given device and associated with the given stream.
The buffer is only copied if the requested device is different from the device upon which the buffer is already resident. If the destination stream is on the same device, then a new reference to the same buffer is returned.
-
to(self, devices: collections.abc.Sequence[max.driver.Device]) -> list[max.driver.Buffer]Returns a list of buffers that are guaranteed to be on the given devices.
The buffers are only copied if the requested devices are different from the device upon which the buffer is already resident.
-
to(self, streams: collections.abc.Sequence[max.driver.DeviceStream]) -> list[max.driver.Buffer]Returns a list of buffers that are guaranteed to be on the given streams.
The buffers are only copied if the requested streams are different from the stream upon which the buffer is already resident.
to_numpy()β
to_numpy()
Converts the buffer to a numpy array.
If the buffer is not on the host, a copy will be issued.
view()β
view(dtype, shape=None)
Return a new buffer with the given type and shape that shares the underlying memory.
If the shape is not given, it will be deduced if possible, or a ValueError is raised.
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!