pub struct SafeTensors<'data> { /* private fields */ }
Expand description

A structure owning some metadata to lookup tensors on a shared data byte-buffer (not owned).

Implementations§

source§

impl<'data> SafeTensors<'data>

source

pub fn read_metadata<'in_data>( buffer: &'in_data [u8] ) -> Result<(usize, Metadata), SafeTensorError>where 'in_data: 'data,

Given a byte-buffer representing the whole safetensor file parses the header, and returns the size of the header + the parsed data.

source

pub fn deserialize<'in_data>( buffer: &'in_data [u8] ) -> Result<Self, SafeTensorError>where 'in_data: 'data,

Given a byte-buffer representing the whole safetensor file parses it and returns the Deserialized form (No Tensor allocation).

use safetensors::SafeTensors;
use memmap2::MmapOptions;
use std::fs::File;

let filename = "model.safetensors";
let file = File::open(filename).unwrap();
let buffer = unsafe { MmapOptions::new().map(&file).unwrap() };
let tensors = SafeTensors::deserialize(&buffer).unwrap();
let tensor = tensors
        .tensor("test")
        .unwrap();
source

pub fn tensors(&self) -> Vec<(String, TensorView<'_>)>

Allow the user to iterate over tensors within the SafeTensors. The tensors returned are merely views and the data is not owned by this structure.

source

pub fn tensor( &self, tensor_name: &str ) -> Result<TensorView<'_>, SafeTensorError>

Allow the user to get a specific tensor within the SafeTensors. The tensor returned is merely a view and the data is not owned by this structure.

source

pub fn names(&self) -> Vec<&String>

Return the names of the tensors within the SafeTensors. These are used as keys to access to the actual tensors, that can be retrieved using the tensor method.

source

pub fn len(&self) -> usize

Return how many tensors are currently stored within the SafeTensors.

source

pub fn is_empty(&self) -> bool

Indicate if the SafeTensors contains or not any tensor.

Trait Implementations§

source§

impl<'data> Debug for SafeTensors<'data>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'data> RefUnwindSafe for SafeTensors<'data>

§

impl<'data> Send for SafeTensors<'data>

§

impl<'data> Sync for SafeTensors<'data>

§

impl<'data> Unpin for SafeTensors<'data>

§

impl<'data> UnwindSafe for SafeTensors<'data>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.