| Crates.io | ndarray-safetensors |
| lib.rs | ndarray-safetensors |
| version | 0.3.0 |
| created_at | 2024-12-21 04:14:13.486745+00 |
| updated_at | 2025-03-05 23:57:34.447557+00 |
| description | Serialize / deserialize ndarrays with safetensors. |
| homepage | https://github.com/linmx0130/ndarray_safetensors |
| repository | https://github.com/linmx0130/ndarray_safetensors |
| max_upload_size | |
| id | 1490741 |
| size | 40,991 |
Serialize / deserialize Rust ndarray with Safetensors.
See safetensors-testapp for more details. Here is an end-to-end demo to print all tensors from a safetensors file.
use ndarray_safetensors::parse_tensor_view_data;
let path_example = Path::new("data/rand.safetensors");
let file = File::open(path_example).unwrap();
let buffer = unsafe {MmapOptions::new().map(&file).unwrap()};
let tensors = safetensors::SafeTensors::deserialize(&buffer).unwrap();
for (name, tensor_view) in tensors.tensors() {
println!("Tensor: {}", name);
if tensor_view.dtype() == safetensors::Dtype::F32 {
let arr= parse_tensor_view_data::<f32>(&tensor_view).unwrap();
println!("{}", arr);
}
if tensor_view.dtype() == safetensors::Dtype::F64 {
let arr= parse_tensor_view_data::<f64>(&tensor_view).unwrap();
println!("{}", arr);
}
}
The following crate feature flags are available. They are configured in your Cargo.toml.
unsafe_copy: Allowing directly copying the bytes instead of parsing floats in serialization/deserialization for little-endian machines.x86_sse: Using SSE instructions to optimize some conversions, it could be useful for fp16.Copyright (c) 2024-2025 Mengxiao Lin. Check LICENSE file.