pub struct MlxBuffer { /* private fields */ }Expand description
A Metal GPU buffer annotated with element dtype and tensor shape.
On Apple Silicon the underlying memory is unified — contents_ptr() gives
direct CPU access without any copy or transfer.
§Thread Safety
MlxBuffer is Send + Sync because the inner metal::Buffer is.
Implementations§
Source§impl MlxBuffer
impl MlxBuffer
Sourcepub fn from_raw(inner: MetalBuffer, dtype: DType, shape: Vec<usize>) -> Self
pub fn from_raw(inner: MetalBuffer, dtype: DType, shape: Vec<usize>) -> Self
Create a new MlxBuffer wrapping an already-allocated Metal buffer.
§When to use
Use this to wrap Metal buffers obtained from external frameworks (e.g.
candle’s MetalStorage::buffer()) for zero-copy interop on Apple
Silicon unified memory. Both frameworks see the same physical memory.
§Safety contract
The caller must ensure that inner remains valid for the lifetime of
the returned MlxBuffer. If the buffer was obtained from another
framework, the caller must ensure that framework does not deallocate
the buffer while this MlxBuffer exists.
Sourcepub fn element_count(&self) -> usize
pub fn element_count(&self) -> usize
Number of elements (product of shape dimensions, or byte_len / dtype.size_of()).
Sourcepub fn contents_ptr(&self) -> *mut c_void
pub fn contents_ptr(&self) -> *mut c_void
Raw pointer to the buffer contents (CPU-accessible on Apple Silicon).
§Safety
The caller must ensure proper synchronization — do not read while a GPU command buffer that writes this buffer is in flight.
Sourcepub fn metal_buffer(&self) -> &MetalBuffer
pub fn metal_buffer(&self) -> &MetalBuffer
Reference to the underlying metal::Buffer for passing to the encoder.
Sourcepub fn as_slice<T: Pod>(&self) -> Result<&[T]>
pub fn as_slice<T: Pod>(&self) -> Result<&[T]>
View the buffer contents as a typed slice.
Returns an error if the buffer byte length is not an exact multiple of
size_of::<T>().
§Safety contract
The caller must ensure:
Tmatches the actual element type stored in the buffer.- No GPU command buffer that writes this buffer is currently in flight.