Skip to main content

MlxBuffer

Struct MlxBuffer 

Source
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

Source

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.

Source

pub fn dtype(&self) -> DType

Element data type.

Source

pub fn shape(&self) -> &[usize]

Tensor shape (dimensions).

Source

pub fn byte_len(&self) -> usize

Total byte length of the Metal buffer.

Source

pub fn element_count(&self) -> usize

Number of elements (product of shape dimensions, or byte_len / dtype.size_of()).

Source

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.

Source

pub fn metal_buffer(&self) -> &MetalBuffer

Reference to the underlying metal::Buffer for passing to the encoder.

Source

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:

  1. T matches the actual element type stored in the buffer.
  2. No GPU command buffer that writes this buffer is currently in flight.
Source

pub fn as_mut_slice<T: Pod>(&mut self) -> Result<&mut [T]>

View the buffer contents as a mutable typed slice.

Same safety contract as as_slice, plus: the caller must ensure exclusive access (no other references to this buffer’s memory exist).

Trait Implementations§

Source§

impl Debug for MlxBuffer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.