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.
sourcepub fn names(&self) -> Vec<&String>
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.