teeny_data/safetensors/mod.rs
1/*
2 * Copyright (c) 2026 Teenygrad.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Memory-mapped `.safetensors` file loading.
18
19use std::path::Path;
20
21use crate::error::Result;
22use memmap2::{Mmap, MmapOptions};
23use safetensors;
24use std::fs::File;
25
26/// A memory-mapped `.safetensors` file, ready to be deserialized without loading its full
27/// contents into memory up front.
28pub struct SafeTensors {
29 mmap: Mmap,
30}
31
32impl SafeTensors {
33 /// Opens and memory-maps the `.safetensors` file at `path`.
34 pub fn from_pretrained(path: &Path) -> Result<Self> {
35 let file = File::open(path)?;
36 let mmap = unsafe { MmapOptions::new().map(&file)? };
37 Ok(Self { mmap })
38 }
39
40 /// Deserializes the mapped file's tensor headers (tensor data is read lazily from the
41 /// memory-mapped region on access).
42 pub fn tensors(&self) -> Result<safetensors::SafeTensors<'_>> {
43 Ok(safetensors::SafeTensors::deserialize(&self.mmap)?)
44 }
45}