Skip to main content

SafetensorsFile

Struct SafetensorsFile 

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

A memory-mapped safetensors file ready for tensor extraction.

This struct owns the mmap and parsed header. Individual tensors can be loaded into Metal buffers on demand via load_tensor or all at once via load_all_tensors.

Implementations§

Source§

impl SafetensorsFile

Source

pub fn open(path: &Path) -> Result<Self>

Open and memory-map a safetensors file.

The file is mapped read-only. No tensor data is copied until load_tensor or load_all_tensors is called.

§Errors
  • MlxError::IoError if the file cannot be opened or mapped.
Source

pub fn tensor_names(&self) -> Result<Vec<String>>

List all tensor names in the file.

Source

pub fn load_tensor( &self, name: &str, device: &MlxDevice, ) -> Result<(DType, Vec<usize>, MlxBuffer)>

Load a single named tensor into a Metal buffer.

Returns the dtype, shape, and a Metal buffer containing the raw bytes.

§Errors
  • MlxError::SafetensorsError if the tensor name is not found.
  • MlxError::UnsupportedDtype if the tensor’s dtype is not supported.
  • MlxError::BufferAllocationError if Metal allocation fails.
Source

pub fn load_all_tensors( &self, device: &MlxDevice, ) -> Result<HashMap<String, (DType, Vec<usize>, MlxBuffer)>>

Load all tensors from the file into Metal buffers.

Returns a map from tensor name to (DType, shape, MlxBuffer).

§Errors

Returns the first error encountered during loading.

Trait Implementations§

Source§

impl Debug for SafetensorsFile

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.