Skip to main content

MlxDevice

Struct MlxDevice 

Source
pub struct MlxDevice { /* private fields */ }
Expand description

Wraps a Metal device and its command queue.

§Thread Safety

MlxDevice is Send + Sync — you can share it across threads. The underlying Metal device and command queue are thread-safe on Apple Silicon.

Implementations§

Source§

impl MlxDevice

Source

pub fn new() -> Result<Self>

Initialize the Metal GPU device and create a command queue.

Returns Err(MlxError::DeviceNotFound) if no Metal device is available (e.g. running on a non-Apple-Silicon machine or in a headless Linux VM).

Source

pub fn command_encoder(&self) -> Result<CommandEncoder>

Create a CommandEncoder for batching GPU dispatches.

The encoder wraps a fresh Metal command buffer from the device’s command queue. Encode one or more kernel dispatches, then call CommandEncoder::commit_and_wait to submit and block until completion.

Source

pub fn alloc_buffer( &self, byte_len: usize, dtype: DType, shape: Vec<usize>, ) -> Result<MlxBuffer>

Allocate a new GPU buffer with StorageModeShared.

§Arguments
  • byte_len — Size of the buffer in bytes. Must be > 0.
  • dtype — Element data type for metadata tracking.
  • shape — Tensor dimensions for metadata tracking.
§Errors

Returns MlxError::InvalidArgument if byte_len is zero. Returns MlxError::BufferAllocationError if Metal cannot allocate.

Source

pub fn metal_device(&self) -> &DeviceRef

Borrow the underlying metal::Device for direct Metal API calls (e.g. kernel compilation in KernelRegistry).

Source

pub fn metal_queue(&self) -> &CommandQueue

Borrow the underlying metal::CommandQueue.

Source

pub fn name(&self) -> String

Human-readable name of the GPU (e.g. “Apple M2 Max”).

Trait Implementations§

Source§

impl Debug for MlxDevice

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.