bunsen 0.34.0

bunsen is a batteries included common library for burn
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! # Safetensors checkpoints
//!
//! A checkpoint in safetensors is one file, or several: `transformers`
//! saves a model as `model.safetensors` until it passes a shard-size
//! limit, then as `model-00001-of-0000N.safetensors` and so on with
//! `model.safetensors.index.json`, whose `weight_map` names the shard each
//! tensor is in. [`SafetensorsCheckpoint`] is either, from a map's
//! [family](super::LoadedResources::family) of resources under the kit's
//! checkpoint key: the one file, or the index and the shards it names.
//!
//! [`safetensors_header`] reads a file's header alone: every tensor's
//! name, element type and shape, without touching the data, which is how
//! a kit learns a checkpoint's geometry before loading it.
//! [`SafetensorsCheckpoint::load_into`] loads every shard into a module
//! through `burn-store`'s [`SafetensorsStore`], configured by the kit (its
//! name remaps, its adapter), and checks that every parameter of the
//! module was found in some shard. A safetensors file is contiguous and
//! row-major, so the `PyTorch` adapter's `Linear` transposition is right as
//! it stands; the strided-view repair `OpenAI`'s `.pt` files need does not
//! apply.

use std::{
    collections::{
        BTreeMap,
        BTreeSet,
    },
    io::Read,
    path::{
        Path,
        PathBuf,
    },
};

use burn::prelude::Backend;
use burn_store::{
    ModuleSnapshot,
    SafetensorsStore,
};
use serde::Deserialize;

use super::LoadedResources;
use crate::errors::{
    BunsenError,
    BunsenResult,
};

/// The longest header accepted, `safetensors`' own bound: 100 MB.
const MAX_HEADER_LEN: u64 = 100_000_000;

/// One tensor as a safetensors header describes it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SafetensorsEntry {
    /// The element type, as the file spells it: `F16`, `F32`, `BF16`.
    pub dtype: String,

    /// The shape.
    pub shape: Vec<usize>,
}

/// The header of a safetensors file: every tensor's name, element type
/// and shape, read without touching the data.
///
/// # Errors
/// [`BunsenError::External`] for a file that cannot be read;
/// [`BunsenError::Invalid`] for a file that is not safetensors: a header
/// length past the file or past 100 MB is refused before anything is
/// allocated for it.
pub fn safetensors_header(path: &Path) -> BunsenResult<BTreeMap<String, SafetensorsEntry>> {
    let mut file = std::fs::File::open(path).map_err(BunsenError::external)?;
    let mut len = [0u8; 8];
    file.read_exact(&mut len).map_err(BunsenError::external)?;
    let len = u64::from_le_bytes(len);
    let file_len = file.metadata().map_err(BunsenError::external)?.len();
    if len > MAX_HEADER_LEN || len.saturating_add(8) > file_len {
        return Err(BunsenError::Invalid(format!(
            "{}: not a safetensors file: a header of {len} bytes",
            path.display()
        )));
    }
    let len = usize::try_from(len).map_err(BunsenError::external)?;
    let mut header = vec![0u8; len];
    file.read_exact(&mut header)
        .map_err(BunsenError::external)?;
    let header: serde_json::Map<String, serde_json::Value> = serde_json::from_slice(&header)
        .map_err(|e| {
            BunsenError::Invalid(format!("{}: not a safetensors header: {e}", path.display()))
        })?;

    let mut entries = BTreeMap::new();
    for (name, value) in header {
        if name == "__metadata__" {
            continue;
        }
        let dtype = value
            .get("dtype")
            .and_then(|d| d.as_str())
            .ok_or_else(|| BunsenError::Invalid(format!("{}: {name}: no dtype", path.display())))?
            .to_string();
        let shape = value
            .get("shape")
            .and_then(|s| s.as_array())
            .ok_or_else(|| BunsenError::Invalid(format!("{}: {name}: no shape", path.display())))?
            .iter()
            .map(|d| {
                d.as_u64()
                    .and_then(|d| usize::try_from(d).ok())
                    .ok_or_else(|| {
                        BunsenError::Invalid(format!(
                            "{}: {name}: a shape that is not usize",
                            path.display()
                        ))
                    })
            })
            .collect::<BunsenResult<Vec<usize>>>()?;
        entries.insert(name, SafetensorsEntry { dtype, shape });
    }
    Ok(entries)
}

/// `model.safetensors.index.json`: the shard each tensor is in.
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq)]
pub struct SafetensorsIndex {
    /// What the writer recorded: `total_size`, say.
    #[serde(default)]
    pub metadata: BTreeMap<String, serde_json::Value>,

    /// Tensor name to shard file name.
    pub weight_map: BTreeMap<String, String>,
}

impl SafetensorsIndex {
    /// Reads an index file.
    ///
    /// # Errors
    /// [`BunsenError::External`] for a file that cannot be read;
    /// [`BunsenError::Invalid`] for one that is not an index.
    pub fn read(path: &Path) -> BunsenResult<Self> {
        let file = std::fs::File::open(path).map_err(BunsenError::external)?;
        serde_json::from_reader(std::io::BufReader::new(file)).map_err(|e| {
            BunsenError::Invalid(format!("{}: not a safetensors index: {e}", path.display()))
        })
    }

    /// The shard files the index names, each once, in name order.
    pub fn shards(&self) -> Vec<&str> {
        let set: BTreeSet<&str> = self.weight_map.values().map(String::as_str).collect();
        set.into_iter().collect()
    }
}

/// A checkpoint in safetensors: one file, or the shards of one.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SafetensorsCheckpoint {
    /// The files, in shard order; one for a checkpoint that is one file.
    pub shards: Vec<PathBuf>,
}

/// What a load found: the tensors applied, and the file tensors no
/// parameter took.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct SafetensorsApplied {
    /// The parameters applied, by the module's names, in name order.
    pub applied: Vec<String>,

    /// The tensors of the files no parameter took, by their file names.
    pub unused: Vec<String>,
}

impl SafetensorsCheckpoint {
    /// A checkpoint that is one file.
    pub fn single(path: impl Into<PathBuf>) -> Self {
        Self {
            shards: vec![path.into()],
        }
    }

    /// The checkpoint under `key` in `loaded`: the one file keyed `key`,
    /// or the index keyed `key.index` and the shards keyed `key.<n>`,
    /// which must be exactly the files the index names.
    ///
    /// # Errors
    /// [`BunsenError::ResourceNotFound`] when `loaded` has nothing under
    /// `key`, naming what it has; [`BunsenError::Invalid`] for a family
    /// with no index, or shards that are not the index's.
    pub fn from_loaded(
        loaded: &LoadedResources,
        key: &str,
    ) -> BunsenResult<Self> {
        if let Some(single) = loaded.get(key) {
            return Ok(Self::single(&single.path));
        }
        let family = loaded.family(key);
        if family.is_empty() {
            loaded.expect(key)?;
        }
        let index_key = format!("{key}.index");
        let Some((_, index)) = family.iter().find(|(k, _)| *k == index_key) else {
            return Err(BunsenError::Invalid(format!(
                "{}: {} shards under {key:?} and no {index_key:?}",
                loaded.map.name,
                family.len()
            )));
        };
        let index = SafetensorsIndex::read(&index.path)?;
        let named: Vec<&str> = index.shards();
        let mut by_file: BTreeMap<&str, &Path> = BTreeMap::new();
        for (k, part) in &family {
            if *k == index_key {
                continue;
            }
            let file = part
                .path
                .file_name()
                .and_then(|f| f.to_str())
                .unwrap_or_default();
            by_file.insert(file, &part.path);
        }
        let have: Vec<&str> = by_file.keys().copied().collect();
        if have != named {
            return Err(BunsenError::Invalid(format!(
                "{}: the index names shards {} but the map has {}",
                loaded.map.name,
                named.join(", "),
                if have.is_empty() {
                    "none".to_string()
                } else {
                    have.join(", ")
                }
            )));
        }
        Ok(Self {
            shards: named.iter().map(|f| by_file[f].to_path_buf()).collect(),
        })
    }

    /// The headers of every shard, merged: every tensor's name, element
    /// type and shape.
    ///
    /// # Errors
    /// As [`safetensors_header`]; [`BunsenError::Invalid`] for a tensor
    /// two shards both hold.
    pub fn headers(&self) -> BunsenResult<BTreeMap<String, SafetensorsEntry>> {
        let mut merged = BTreeMap::new();
        for shard in &self.shards {
            for (name, entry) in safetensors_header(shard)? {
                if merged.insert(name.clone(), entry).is_some() {
                    return Err(BunsenError::Invalid(format!(
                        "{}: {name} is in more than one shard",
                        shard.display()
                    )));
                }
            }
        }
        Ok(merged)
    }

    /// Loads every shard into `module`, each through a store `configure`
    /// sets up from the bare one (the kit's name remaps and adapter), and
    /// checks that every parameter of the module was found in some shard.
    ///
    /// # Errors
    /// [`BunsenError::External`] from the store: a file that cannot be
    /// read, a tensor whose shape does not fit its parameter;
    /// [`BunsenError::Invalid`] naming the parameters no shard held.
    pub fn load_into<B: Backend, M: ModuleSnapshot<B>>(
        &self,
        module: &mut M,
        configure: impl Fn(SafetensorsStore) -> SafetensorsStore,
    ) -> BunsenResult<SafetensorsApplied> {
        let mut applied = BTreeSet::new();
        let mut unused = BTreeSet::new();
        let mut all: Option<BTreeSet<String>> = None;
        for shard in &self.shards {
            let mut store =
                configure(SafetensorsStore::from_file(shard.clone()).allow_partial(true));
            let result = module
                .load_from(&mut store)
                .map_err(|e| BunsenError::External(format!("{}: {e}", shard.display())))?;
            if all.is_none() {
                let mut every: BTreeSet<String> = result.applied.iter().cloned().collect();
                every.extend(result.missing.iter().map(|(path, _)| path.clone()));
                every.extend(result.skipped.iter().cloned());
                all = Some(every);
            }
            applied.extend(result.applied.iter().cloned());
            unused.extend(result.unused.iter().cloned());
        }
        let never: Vec<String> = all
            .unwrap_or_default()
            .into_iter()
            .filter(|p| !applied.contains(p))
            .collect();
        if !never.is_empty() {
            return Err(BunsenError::Invalid(format!(
                "{}: the checkpoint lacks {} of the model's parameters: {}",
                self.shards
                    .first()
                    .map(|s| s.display().to_string())
                    .unwrap_or_default(),
                never.len(),
                never
                    .iter()
                    .take(8)
                    .map(|s| s.as_str())
                    .collect::<Vec<_>>()
                    .join(", ")
            )));
        }
        Ok(SafetensorsApplied {
            applied: applied.into_iter().collect(),
            unused: unused.into_iter().collect(),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Writes a safetensors file with `header` and `data`.
    fn write_safetensors(
        path: &Path,
        header: &str,
        data: &[u8],
    ) {
        let mut bytes = (header.len() as u64).to_le_bytes().to_vec();
        bytes.extend_from_slice(header.as_bytes());
        bytes.extend_from_slice(data);
        std::fs::write(path, bytes).unwrap();
    }

    /// The header is read without the data; a file that is not
    /// safetensors is refused, not allocated for.
    #[test]
    fn test_the_header_is_read_alone() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("w.safetensors");
        write_safetensors(
            &path,
            r#"{"__metadata__":{"format":"pt"},"weight":{"dtype":"F32","shape":[2,3],"data_offsets":[0,24]},"bias":{"dtype":"F16","shape":[2],"data_offsets":[24,28]}}"#,
            &[0u8; 28],
        );
        let header = safetensors_header(&path).unwrap();
        assert_eq!(header.keys().collect::<Vec<_>>(), ["bias", "weight"]);
        assert_eq!(
            header["weight"],
            SafetensorsEntry {
                dtype: "F32".to_string(),
                shape: vec![2, 3]
            }
        );
        assert_eq!(header["bias"].dtype, "F16");

        // "not a sa" as a little-endian length is enormous: refused, not
        // allocated for.
        let junk = dir.path().join("junk.safetensors");
        std::fs::write(&junk, b"not a safetensors file at all").unwrap();
        let err = safetensors_header(&junk).unwrap_err();
        assert!(
            matches!(&err, BunsenError::Invalid(m) if m.contains("not a safetensors file")),
            "{err}"
        );
    }

    /// An index names its shards once each; a checkpoint from loaded
    /// resources is the one file, or the index's shards in its order, and
    /// shards that are not the index's are refused.
    #[test]
    fn test_a_checkpoint_from_loaded_resources() {
        use crate::data::pretrained::{
            Provenance,
            ResolvedResource,
            ResourceMap,
        };

        let dir = tempfile::tempdir().unwrap();
        let index_path = dir.path().join("model.safetensors.index.json");
        std::fs::write(
            &index_path,
            r#"{"metadata":{"total_size":48},"weight_map":{"a":"model-00001-of-00002.safetensors","b":"model-00002-of-00002.safetensors","c":"model-00001-of-00002.safetensors"}}"#,
        )
        .unwrap();
        let index = SafetensorsIndex::read(&index_path).unwrap();
        assert_eq!(
            index.shards(),
            [
                "model-00001-of-00002.safetensors",
                "model-00002-of-00002.safetensors"
            ]
        );
        assert_eq!(index.metadata["total_size"], 48);

        let shard = |name: &str, header: &str| {
            let path = dir.path().join(name);
            write_safetensors(&path, header, &[0u8; 24]);
            path
        };
        let s1 = shard(
            "model-00001-of-00002.safetensors",
            r#"{"a":{"dtype":"F32","shape":[2,3],"data_offsets":[0,24]}}"#,
        );
        let s2 = shard(
            "model-00002-of-00002.safetensors",
            r#"{"b":{"dtype":"F32","shape":[6],"data_offsets":[0,24]}}"#,
        );
        let part = |path: &Path| ResolvedResource {
            path: path.to_path_buf(),
            provenance: Provenance::LocalDir,
        };
        let loaded = |parts: Vec<(&str, &Path)>| LoadedResources {
            map: ResourceMap::new("sharded"),
            parts: parts
                .into_iter()
                .map(|(k, p)| (k.to_string(), part(p)))
                .collect(),
        };

        let sharded = loaded(vec![
            ("checkpoint.index", &index_path),
            ("checkpoint.00002", &s2),
            ("checkpoint.00001", &s1),
            ("config", &index_path),
        ]);
        let checkpoint = SafetensorsCheckpoint::from_loaded(&sharded, "checkpoint").unwrap();
        assert_eq!(checkpoint.shards, [s1.clone(), s2.clone()]);
        let headers = checkpoint.headers().unwrap();
        assert_eq!(headers.keys().collect::<Vec<_>>(), ["a", "b"]);
        assert_eq!(headers["b"].shape, [6]);

        let single = loaded(vec![("checkpoint", &s1)]);
        assert_eq!(
            SafetensorsCheckpoint::from_loaded(&single, "checkpoint").unwrap(),
            SafetensorsCheckpoint::single(&s1)
        );

        let short = loaded(vec![
            ("checkpoint.index", &index_path),
            ("checkpoint.00001", &s1),
        ]);
        let err = SafetensorsCheckpoint::from_loaded(&short, "checkpoint").unwrap_err();
        assert!(
            matches!(&err, BunsenError::Invalid(m) if m.contains("the index names shards")),
            "{err}"
        );
        let no_index = loaded(vec![("checkpoint.00001", &s1)]);
        let err = SafetensorsCheckpoint::from_loaded(&no_index, "checkpoint").unwrap_err();
        assert!(
            matches!(&err, BunsenError::Invalid(m) if m.contains("no \"checkpoint.index\"")),
            "{err}"
        );
        let nothing = loaded(vec![("config", &index_path)]);
        assert!(matches!(
            SafetensorsCheckpoint::from_loaded(&nothing, "checkpoint"),
            Err(BunsenError::ResourceNotFound(_))
        ));
    }
}