bunsen 0.41.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! # Constructing a Whisper pretrained

use std::{
    error::Error,
    fmt,
    sync::Arc,
};

use burn::tensor::Device;

#[cfg(feature = "store_safetensors")]
use crate::data::pretrained::SafetensorsCheckpoint;
#[cfg(not(feature = "store_safetensors"))]
use crate::kits::speech::whisper::Whisper;
#[cfg(feature = "store_safetensors")]
use crate::kits::speech::whisper::{
    Whisper,
    pretrained::SafetensorsWhisperScanner,
};
use crate::{
    data::pretrained::{
        Construct,
        Fuse,
        GIVEN_NAMESPACE,
        LoadedResources,
        PretrainedCache,
        PretrainedRef,
        ResourceMap,
        SAFETENSORS,
    },
    errors::{
        BunsenError,
        BunsenErrorKind,
        BunsenResult,
        ResultContext,
    },
    kits::{
        speech::whisper::{
            WhisperApiConfig,
            WhisperGeometry,
            driver::WhisperBundle,
            pretrained::{
                CHECKPOINT,
                PytorchWhisperScanner,
                VOCABULARY,
                WHISPER_KIT,
                WHISPER_PREFABS,
                WhisperVocabulary,
            },
        },
        tokens::TiktokenRanks,
    },
};

/// A checkpoint scanned to a geometry other than the one its model
/// promises: the files at a name are not the model the name claims.
///
/// The cause of the [`InvalidResource`](BunsenErrorKind::InvalidResource)
/// error [`WhisperConstruct::scan`] (and so
/// [`plan`](Construct::plan)) returns for it. A caller that wants to tell
/// this apart from a checkpoint it could not read at all finds it with
/// [`BunsenError::find`]:
///
/// ```
/// # #[cfg(all(feature = "store_pytorch", feature = "cache"))] {
/// use bunsen::{
///     errors::{
///         BunsenError,
///         BunsenErrorKind,
///     },
///     kits::speech::whisper::{
///         WhisperGeometry,
///         pretrained::GeometryMismatch,
///     },
/// };
///
/// let e = BunsenError::from(GeometryMismatch::new(
///     "well-known:openai/tiny",
///     WhisperGeometry::openai(80, 51865, 384, 4, 4),
///     WhisperGeometry::openai(80, 51865, 512, 6, 6),
/// ));
/// assert_eq!(e.kind(), BunsenErrorKind::InvalidResource);
/// let mismatch = e.find::<GeometryMismatch>().unwrap();
/// assert_eq!(mismatch.model, "well-known:openai/tiny");
/// # }
/// ```
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GeometryMismatch {
    /// The model's id, as [`PretrainedRef::id`] gives it.
    pub model: String,
    /// The geometry the model promises.
    pub expected: WhisperGeometry,
    /// The geometry the checkpoint scanned to.
    pub found: WhisperGeometry,
}

impl GeometryMismatch {
    /// `model`'s checkpoint scanned to `found`, not the promised `expected`.
    pub fn new(
        model: impl Into<String>,
        expected: WhisperGeometry,
        found: WhisperGeometry,
    ) -> Self {
        Self {
            model: model.into(),
            expected,
            found,
        }
    }
}

impl fmt::Display for GeometryMismatch {
    fn fmt(
        &self,
        f: &mut fmt::Formatter<'_>,
    ) -> fmt::Result {
        write!(
            f,
            "{}: the checkpoint's geometry is {:?}, not the promised {:?}",
            self.model, self.found, self.expected
        )
    }
}

impl Error for GeometryMismatch {}

impl From<GeometryMismatch> for BunsenError {
    #[track_caller]
    fn from(error: GeometryMismatch) -> Self {
        BunsenError::from_cause(BunsenErrorKind::InvalidResource, error)
    }
}

/// How a Whisper checkpoint is read: by the layout its resource's `kind`
/// names.
///
/// Chosen by the checkpoint resource's `kind` when the hook is made for a
/// map: `OpenAI`'s `.pt` through the `PyTorch` scanner, `transformers`'
/// `model.safetensors`, one file or shards, through the safetensors one.
/// Both scan to the same [`WhisperApiConfig`] and load the same
/// [`Whisper`] model; only the files' names and layout differ.
#[derive(Clone, Debug)]
pub enum WhisperReader {
    /// `OpenAI`'s `.pt`: a `PyTorch` state dict under `model_state_dict`,
    /// with upstream's names. The reader for a checkpoint of no declared
    /// kind, and of any kind starting `pytorch`.
    Pytorch(PytorchWhisperScanner),

    /// `transformers`' `model.safetensors`, one file or shards with an
    /// index, with its names: what a Hugging Face repo serves. The reader
    /// for any kind starting `safetensors`.
    #[cfg(feature = "store_safetensors")]
    Safetensors(SafetensorsWhisperScanner),
}

impl Default for WhisperReader {
    /// Upstream's `.pt` scanner.
    fn default() -> Self {
        Self::Pytorch(PytorchWhisperScanner::new())
    }
}

impl WhisperReader {
    /// The reader for a checkpoint of `kind`: `None` and `pytorch…` are
    /// upstream's `.pt`; `safetensors…` is `transformers`' file, with the
    /// `store_safetensors` feature; anything else has no reader here.
    ///
    /// # Errors
    /// [`Unsupported`](BunsenErrorKind::Unsupported) naming the kind, and the
    /// feature when it is the one missing.
    pub fn for_kind(kind: Option<&str>) -> BunsenResult<Self> {
        match kind {
            None => Ok(Self::default()),
            Some(kind) if kind.starts_with("pytorch") => Ok(Self::default()),
            #[cfg(feature = "store_safetensors")]
            Some(kind) if kind.starts_with(SAFETENSORS) => {
                Ok(Self::Safetensors(SafetensorsWhisperScanner::new()))
            }
            #[cfg(not(feature = "store_safetensors"))]
            Some(kind) if kind.starts_with(SAFETENSORS) => Err(BunsenError::unsupported(format!(
                "no reader for a {kind:?} checkpoint without the `store_safetensors` feature"
            ))),
            Some(kind) => Err(BunsenError::unsupported(format!(
                "no reader for a {kind:?} checkpoint; the Whisper kit reads PyTorch and safetensors checkpoints"
            ))),
        }
    }

    /// Scans the checkpoint in `loaded`, under [`CHECKPOINT`], for its
    /// config without loading its weights.
    ///
    /// # Errors
    /// [`Lookup`](BunsenErrorKind::Lookup) when `loaded` has no checkpoint;
    /// the scanner's for files that are not a checkpoint of its layout
    /// ([`InvalidResource`](BunsenErrorKind::InvalidResource), for the most
    /// part).
    pub fn scan_cfg(
        &self,
        loaded: &LoadedResources,
    ) -> BunsenResult<WhisperApiConfig> {
        match self {
            Self::Pytorch(scanner) => Ok(scanner.scan_cfg(loaded.expect(CHECKPOINT)?)?.1),
            #[cfg(feature = "store_safetensors")]
            Self::Safetensors(scanner) => {
                scanner.scan_cfg(&SafetensorsCheckpoint::from_loaded(loaded, CHECKPOINT)?)
            }
        }
    }

    /// Loads the checkpoint in `loaded` into a model at the precision it
    /// ships in.
    ///
    /// # Errors
    /// As [`scan_cfg`](Self::scan_cfg); the scanner's.
    pub fn load(
        &self,
        loaded: &LoadedResources,
        device: &Device,
    ) -> BunsenResult<(Whisper, WhisperApiConfig)> {
        match self {
            Self::Pytorch(scanner) => scanner.load::<_>(loaded.expect(CHECKPOINT)?, device),
            #[cfg(feature = "store_safetensors")]
            Self::Safetensors(scanner) => scanner.load(
                &SafetensorsCheckpoint::from_loaded(loaded, CHECKPOINT)?,
                device,
            ),
        }
    }
}

/// How a Whisper pretrained is built: the reader for its checkpoint, and
/// the geometry the checkpoint must have.
///
/// The [`Construct`] hook for the Whisper kit: from a resolved model to a
/// [`WhisperBundle`]. [`plan`](Construct::plan) scans the checkpoint,
/// checks it against the geometry the model promises, and applies the
/// vocabulary rule, all before anything but the checkpoint is brought
/// local; [`construct`](Construct::construct) reads the checkpoint through
/// the reader and the vocabulary through the rank parser, and checks that
/// they agree. [`scan`](Self::scan) is the read-only half, for a listing
/// that wants the geometry without the weights. The checkpoint is the
/// map's [family](ResourceMap::family) under [`CHECKPOINT`]: one resource,
/// or an index and its shards.
///
/// [`for_map`](Construct::for_map) chooses the hook by the checkpoint
/// resource's `kind`, through [`WhisperReader::for_kind`]: a `PyTorch`
/// row gets the `.pt` scanner, a Hugging Face row the safetensors one, and
/// a row of a kind neither reads is refused by name rather than misread. A
/// caller gets this with the deferred model the factory resolves and never
/// builds it.
#[derive(Clone, Debug)]
pub struct WhisperConstruct {
    /// How the checkpoint is read: which layout, and what the reader
    /// declares that the file does not record (the head size, the front
    /// end and the token layout).
    pub reader: WhisperReader,

    /// The geometry the checkpoint must scan to, overriding what a row's
    /// prefab promises. `None`, the default, expects the prefab's geometry
    /// when the model was named, and accepts whatever is found when it was
    /// given.
    pub expected: Option<WhisperGeometry>,
}

impl Default for WhisperConstruct {
    fn default() -> Self {
        Self::new()
    }
}

impl WhisperConstruct {
    /// Upstream's `.pt` scanner, expecting what the model promises.
    pub fn new() -> Self {
        Self {
            reader: WhisperReader::default(),
            expected: None,
        }
    }

    /// Sets the reader.
    pub fn with_reader(
        mut self,
        reader: WhisperReader,
    ) -> Self {
        self.reader = reader;
        self
    }

    /// Reads through `scanner`: a `.pt` with another head size, say.
    pub fn with_scanner(
        self,
        scanner: PytorchWhisperScanner,
    ) -> Self {
        self.with_reader(WhisperReader::Pytorch(scanner))
    }

    /// Sets the geometry the checkpoint must scan to, which wins over the
    /// one a row's prefab promises.
    pub fn with_expected(
        mut self,
        expected: Option<WhisperGeometry>,
    ) -> Self {
        self.expected = expected;
        self
    }

    /// The geometry `model` must scan to: the explicit one, else its
    /// prefab's when it was named and names one, else none.
    pub fn expected_for(
        &self,
        model: &PretrainedRef,
    ) -> Option<WhisperGeometry> {
        self.expected.or_else(|| {
            model
                .prefab(&WHISPER_PREFABS)
                .map(|prefab| prefab.to_config().geometry())
        })
    }

    /// Scans the checkpoint in `loaded` for its config without loading its
    /// weights, and checks it against the geometry `model` promises.
    ///
    /// # Errors
    /// As [`WhisperReader::scan_cfg`];
    /// [`InvalidResource`](BunsenErrorKind::InvalidResource), with a
    /// [`GeometryMismatch`] cause naming both geometries, when the files at
    /// that name are not the model it claims to be.
    pub fn scan(
        &self,
        model: &PretrainedRef,
        loaded: &LoadedResources,
    ) -> BunsenResult<WhisperApiConfig> {
        let cfg = self.reader.scan_cfg(loaded)?;
        self.check(model, &cfg.geometry())?;
        Ok(cfg)
    }

    /// Checks a scanned geometry against the one `model` promises.
    fn check(
        &self,
        model: &PretrainedRef,
        found: &WhisperGeometry,
    ) -> BunsenResult<()> {
        match self.expected_for(model) {
            Some(expected) if expected != *found => {
                Err(GeometryMismatch::new(model.id(), expected, *found).into())
            }
            _ => Ok(()),
        }
    }
}

impl Construct for WhisperConstruct {
    type Built = WhisperBundle;

    const KIT: &'static str = WHISPER_KIT;

    /// The reader the checkpoint's `kind` names, through
    /// [`WhisperReader::for_kind`], expecting what the model promises. The
    /// checkpoint is the map's family under [`CHECKPOINT`]: one resource,
    /// or an index and shards, whose first resource's kind speaks for
    /// them. A checkpoint of a kind with no reader is refused by name.
    fn for_map(map: &ResourceMap) -> BunsenResult<Self> {
        let family = map.family(CHECKPOINT);
        let Some(first) = family.resources.values().next() else {
            map.try_get(CHECKPOINT)?;
            unreachable!("an empty family is a missing key");
        };
        let reader = WhisperReader::for_kind(first.kind.as_deref()).context(&first.key)?;
        Ok(Self::new().with_reader(reader))
    }

    /// Scans the checkpoint, which comes local for it (every shard, when
    /// it is sharded), checks its geometry against the promised one, and
    /// settles the vocabulary: a map without one gets the one the
    /// checkpoint's layout selects; a map that declares one must declare
    /// that file, by name and digest, from wherever it serves it, unless
    /// the caller gave it, which is trusted.
    ///
    /// A vocabulary size that is not a Whisper layout is the checkpoint's
    /// fault, not the code's: it comes back as
    /// [`Policy`](BunsenErrorKind::Policy), under a frame naming the model.
    /// A declared vocabulary the checkpoint does not select is
    /// [`InvalidResource`](BunsenErrorKind::InvalidResource).
    fn plan(
        &self,
        model: &PretrainedRef,
        cache: &PretrainedCache,
    ) -> BunsenResult<ResourceMap> {
        let map = model.to_map();
        let checkpoint = map.family(CHECKPOINT);
        if checkpoint.is_empty() {
            map.try_get(CHECKPOINT)?;
        }
        let loaded = cache.load(WHISPER_KIT, &checkpoint)?;
        let cfg = self.scan(model, &loaded)?;

        let ids = *cfg
            .token_layout
            .policy_for_vocab(cfg.vocab_size)
            .as_policy()
            .with_context(|| model.id())?
            .ids();
        let rule = WhisperVocabulary::for_layout(&ids).map().to_map();
        match map.get(VOCABULARY).cloned() {
            None => map.fuse(rule, Fuse::Strict),
            Some(given) if given.namespace == GIVEN_NAMESPACE => Ok(map),
            Some(declared) => {
                // The same file, by name and digest: where a row's copy is
                // served from (a bundle, a mirror) is its own business.
                let selected = rule.try_get(VOCABULARY)?;
                if declared.file != selected.file || declared.sha256 != selected.sha256 {
                    return Err(BunsenError::invalid_resource(format!(
                        "{}: declares the vocabulary {} but the checkpoint's layout selects {}",
                        map.name, declared.file, selected.file
                    )));
                }
                Ok(map)
            }
        }
    }

    /// Reads the checkpoint into a model at the precision it ships in, and
    /// the vocabulary, when there is one, into ranks the layout agrees
    /// with.
    ///
    /// A layout the checkpoint and the vocabulary disagree on is their
    /// fault, not the code's: it comes back as
    /// [`Policy`](BunsenErrorKind::Policy), under a frame naming the model.
    fn construct(
        &self,
        model: &PretrainedRef,
        loaded: &LoadedResources,
        device: &Device,
    ) -> BunsenResult<Arc<WhisperBundle>> {
        let (whisper, cfg) = self.reader.load(loaded, device)?;
        let layout = cfg
            .token_layout
            .policy_for_vocab(cfg.vocab_size)
            .as_policy()
            .with_context(|| model.id())?;
        let mut bundle = WhisperBundle::new(whisper, layout);
        if let Some(part) = loaded.get(VOCABULARY) {
            bundle = bundle.with_ranks(TiktokenRanks::load(&part.path)?);
        }
        bundle.validate().as_policy().with_context(|| model.id())?;
        Ok(Arc::new(bundle))
    }
}

#[cfg(test)]
mod reader_tests {
    use super::*;
    use crate::{
        data::pretrained::Deferred,
        errors::testing::ErrorMatcher,
    };

    /// The checkpoint's `kind` chooses the reader when the hook is chosen,
    /// before its file is opened: an unlabeled or `PyTorch` one gets the
    /// `.pt` scanner, a safetensors one the safetensors scanner (with its
    /// feature; refused naming the feature without), and a kind neither
    /// reads is refused by name.
    #[test]
    fn test_the_kind_chooses_the_reader() {
        let mut map = ResourceMap::given("mine", CHECKPOINT, "/no/such/model.safetensors");
        let hook = WhisperConstruct::for_map(&map).unwrap();
        assert!(matches!(hook.reader, WhisperReader::Pytorch(_)));

        map.resources.get_mut(CHECKPOINT).unwrap().kind = Some("pytorch fp16".to_string());
        let hook = WhisperConstruct::for_map(&map).unwrap();
        assert!(matches!(hook.reader, WhisperReader::Pytorch(_)));

        map.resources.get_mut(CHECKPOINT).unwrap().kind = Some(SAFETENSORS.to_string());
        #[cfg(feature = "store_safetensors")]
        {
            let hook = WhisperConstruct::for_map(&map).unwrap();
            assert!(matches!(hook.reader, WhisperReader::Safetensors(_)));
            assert!(Deferred::<WhisperConstruct>::from_map(map.clone()).is_ok());
        }
        #[cfg(not(feature = "store_safetensors"))]
        {
            ErrorMatcher::kind(BunsenErrorKind::Unsupported)
                .message_contains("store_safetensors")
                .assert_err(&WhisperConstruct::for_map(&map));
        }

        // A sharded family under the key: its first resource speaks.
        let mut sharded = ResourceMap::new("sharded");
        for key in ["checkpoint.00001", "checkpoint.00002", "checkpoint.index"] {
            let mut r = crate::data::pretrained::Resource::given(key, format!("/no/such/{key}"));
            r.kind = Some(if key.ends_with("index") {
                crate::data::pretrained::SAFETENSORS_INDEX.to_string()
            } else {
                SAFETENSORS.to_string()
            });
            sharded.insert(r);
        }
        #[cfg(feature = "store_safetensors")]
        assert!(matches!(
            WhisperConstruct::for_map(&sharded).unwrap().reader,
            WhisperReader::Safetensors(_)
        ));
        #[cfg(not(feature = "store_safetensors"))]
        assert!(WhisperConstruct::for_map(&sharded).is_err());

        map.resources.get_mut(CHECKPOINT).unwrap().kind = Some("burnpack".to_string());
        ErrorMatcher::kind(BunsenErrorKind::Unsupported)
            .frame_contains("checkpoint")
            .display_contains("checkpoint: no reader for a \"burnpack\"")
            .assert_err(&WhisperConstruct::for_map(&map));
        ErrorMatcher::kind(BunsenErrorKind::Unsupported)
            .assert_err(&Deferred::<WhisperConstruct>::from_map(map));

        ErrorMatcher::kind(BunsenErrorKind::Lookup)
            .assert_err(&WhisperConstruct::for_map(&ResourceMap::new("empty")));
    }
}