Struct safetensors::tensor::SafeTensors
source · 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>
impl<'data> SafeTensors<'data>
sourcepub fn read_metadata<'in_data>(
buffer: &'in_data [u8]
) -> Result<(usize, Metadata), SafeTensorError>where
'in_data: 'data,
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.
sourcepub fn deserialize<'in_data>(
buffer: &'in_data [u8]
) -> Result<Self, SafeTensorError>where
'in_data: 'data,
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();
sourcepub fn tensors(&self) -> Vec<(String, TensorView<'_>)>
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.
sourcepub fn tensor(
&self,
tensor_name: &str
) -> Result<TensorView<'_>, SafeTensorError>
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.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more