#safetensors #tensor #serialization

ndarray-safetensors

Serialize / deserialize ndarrays with safetensors

5 unstable releases

0.3.0 Mar 5, 2025
0.2.2 Jan 9, 2025
0.2.1 Jan 5, 2025
0.2.0 Dec 22, 2024
0.1.0 Dec 21, 2024

#520 in Encoding

Download history 241/week @ 2024-12-17 18/week @ 2024-12-24 148/week @ 2024-12-31 171/week @ 2025-01-07 6/week @ 2025-01-14 3/week @ 2025-02-04 14/week @ 2025-02-11 2/week @ 2025-02-18 20/week @ 2025-02-25 137/week @ 2025-03-04 27/week @ 2025-03-11 37/week @ 2025-03-18 154/week @ 2025-03-25 46/week @ 2025-04-01

270 downloads per month
Used in e2k

MIT license

37KB
663 lines

ndarray-safetensors

Serialize / deserialize Rust ndarray with Safetensors.

Demo

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);
    }
}

Features

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.

Dependencies

~2–3MB
~63K SLoC