bunsen 0.36.1

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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//! # Pretrained providers
//!
//! A provider is a namespace over pretrained rows: the `provider` of
//! `provider:ref`. It names and looks up, and lists when it can. It knows
//! no hook, since the function that loads a kit's models is the kit's and
//! supplies one, and it does not know which resource of a row is the model.
//! A row may name the prefab it instantiates, so "what shape is this" is
//! answered by the row, and "what rows exist for this shape" is derived by
//! scanning the listing. One prefab, many rows, many providers.
//!
//! [`PretrainedProvider`] is the trait; a
//! [`PretrainedFactory`](super::PretrainedFactory) holds providers behind it
//! and dispatches a spec. The compiled-in rows sit behind one provider named
//! [`WELL_KNOWN`], a [`PretrainedTable`] of [`PretrainedGroup`]s: the `openai`
//! of `well-known:openai/tiny`. A group is a labelled set of rows with one
//! license and origin; a table's ref is `{group}/{name}`, and a bare name or
//! alias searches its groups in order. A hub that can answer a ref but not
//! enumerate what it has implements the trait with an empty listing and
//! declines bare names.

use alloc::{
    format,
    string::{
        String,
        ToString,
    },
    vec::Vec,
};
use core::fmt;

use serde::{
    Deserialize,
    Serialize,
};

#[cfg(feature = "cache")]
use super::PretrainedCache;
use super::{
    Pretrained,
    StaticPretrained,
};
use crate::errors::BunsenResult;

/// The name of the provider the compiled-in rows sit behind:
/// `well-known:openai/tiny`.
pub const WELL_KNOWN: &str = "well-known";

/// The name of the provider whose rows a build ships with:
/// `bundled:openai/base`, `bundled:silero/vad`. A kit registers one under
/// its `*-weights` feature, after [`WELL_KNOWN`], so a bare name's identity
/// does not change with the feature; the rows are the same files, served
/// from the bundle rather than fetched.
pub const BUNDLED: &str = "bundled";

/// A namespace of pretrained rows: the `provider` of `provider:ref`.
///
/// Object-safe by construction, so a factory holds any mix of these behind
/// `Arc<dyn PretrainedProvider>`. [`list`](Self::list) and
/// [`lookup`](Self::lookup) are separate so that a provider which cannot
/// enumerate what it has (a hub) can still answer a ref; such a provider
/// also says it does not [answer bare names](Self::answers_bare_names), so
/// a spec with no `provider:` never reaches it.
pub trait PretrainedProvider: Send + Sync + fmt::Debug {
    /// The namespace: the `provider` of `provider:ref`.
    fn name(&self) -> &str;

    /// A line for a listing.
    fn description(&self) -> &str;

    /// The license the rows are distributed under, when one covers them
    /// all.
    fn license(&self) -> Option<&str> {
        None
    }

    /// Where the table came from.
    fn origin(&self) -> Option<&str> {
        None
    }

    /// The rows this provider can enumerate, in listing order; each row's
    /// `name` is the ref it answers to. Empty for a provider that can look
    /// up but not list.
    fn list(&self) -> Vec<Pretrained>;

    /// The row `name` refers to, by its name or an alias.
    ///
    /// `Ok(None)` is "not here", and a factory moves on to the next
    /// provider. The default scans [`list`](Self::list).
    ///
    /// # Errors
    /// The provider's own: a hub that cannot be reached. Any error aborts
    /// the lookup; it is not "not here".
    fn lookup(
        &self,
        name: &str,
    ) -> BunsenResult<Option<Pretrained>> {
        Ok(self.list().into_iter().find(|p| p.matches(name)))
    }

    /// The row `name` refers to, with a cache to hand: what a factory
    /// asks when it resolves a spec to load. The default is
    /// [`lookup`](Self::lookup). A hub answers here instead, asking the
    /// hub what the repo holds once and keeping the answer in the cache
    /// under `kit`, so the same ref is answered offline after.
    ///
    /// # Errors
    /// The provider's own, as [`lookup`](Self::lookup).
    #[cfg(feature = "cache")]
    fn resolve(
        &self,
        name: &str,
        kit: &str,
        cache: &PretrainedCache,
    ) -> BunsenResult<Option<Pretrained>> {
        let _ = (kit, cache);
        self.lookup(name)
    }

    /// Whether a spec with no `provider:` is offered to this provider.
    ///
    /// `true` by default. A network provider says `false`, so that a bare
    /// name never reaches it; its refs are then only reachable qualified.
    fn answers_bare_names(&self) -> bool {
        true
    }

    /// The qualified id of a row: `provider:ref`.
    fn id(
        &self,
        pretrained: &Pretrained,
    ) -> String {
        format!("{}:{}", self.name(), pretrained.name)
    }

    /// Every qualified id, in listing order.
    fn ids(&self) -> Vec<String> {
        self.list().iter().map(|p| self.id(p)).collect()
    }

    /// The listed rows that instantiate `prefab`, in listing order.
    fn for_prefab(
        &self,
        prefab: &str,
    ) -> Vec<Pretrained> {
        self.list()
            .into_iter()
            .filter(|p| p.prefab.as_deref() == Some(prefab))
            .collect()
    }
}

/// A labelled set of pretrained rows, as a compiled-in table spells it: the
/// `openai` of `openai/tiny`.
#[derive(Debug)]
pub struct StaticPretrainedGroup<'a> {
    /// The label: the `group` in `group/name`.
    pub name: &'a str,

    /// A line for a listing.
    pub description: &'a str,

    /// The license the rows are distributed under.
    pub license: Option<&'a str>,

    /// Where the table came from.
    pub origin: Option<&'a str>,

    /// The rows, in listing order.
    pub items: &'a [&'a StaticPretrained<'a>],
}

impl<'a> StaticPretrainedGroup<'a> {
    /// The row `name` names, by its name or an alias.
    pub fn lookup(
        &self,
        name: &str,
    ) -> Option<&'a StaticPretrained<'a>> {
        self.items.iter().copied().find(|p| p.matches(name))
    }

    /// The ref of a row within a table: `group/name`.
    pub fn id(
        &self,
        pretrained: &StaticPretrained<'_>,
    ) -> String {
        format!("{}/{}", self.name, pretrained.name)
    }

    /// Every ref, in listing order.
    pub fn ids(&self) -> Vec<String> {
        self.items.iter().map(|p| self.id(p)).collect()
    }

    /// The rows that instantiate `prefab`, in listing order.
    pub fn for_prefab(
        &self,
        prefab: &str,
    ) -> Vec<&'a StaticPretrained<'a>> {
        self.items
            .iter()
            .copied()
            .filter(|p| p.prefab == Some(prefab))
            .collect()
    }

    /// The owned twin.
    ///
    /// # Panics
    /// If a row's maps share a key; a kit's tests pin that none do.
    pub fn to_group(&self) -> PretrainedGroup {
        PretrainedGroup {
            name: self.name.to_string(),
            description: self.description.to_string(),
            license: self.license.map(str::to_string),
            origin: self.origin.map(str::to_string),
            items: self.items.iter().map(|p| p.to_pretrained()).collect(),
        }
    }
}

impl From<&StaticPretrainedGroup<'_>> for PretrainedGroup {
    fn from(group: &StaticPretrainedGroup<'_>) -> Self {
        group.to_group()
    }
}

/// A labelled set of pretrained rows: the `openai` of `openai/tiny`.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PretrainedGroup {
    /// The label: the `group` in `group/name`.
    pub name: String,

    /// A line for a listing.
    pub description: String,

    /// The license the rows are distributed under.
    pub license: Option<String>,

    /// Where the table came from.
    pub origin: Option<String>,

    /// The rows, in listing order.
    pub items: Vec<Pretrained>,
}

impl PretrainedGroup {
    /// The row `name` names, by its name or an alias.
    pub fn lookup(
        &self,
        name: &str,
    ) -> Option<&Pretrained> {
        self.items.iter().find(|p| p.matches(name))
    }

    /// The ref of a row within a table: `group/name`.
    pub fn id(
        &self,
        pretrained: &Pretrained,
    ) -> String {
        format!("{}/{}", self.name, pretrained.name)
    }

    /// Every ref, in listing order.
    pub fn ids(&self) -> Vec<String> {
        self.items.iter().map(|p| self.id(p)).collect()
    }

    /// The rows that instantiate `prefab`, in listing order.
    pub fn for_prefab(
        &self,
        prefab: &str,
    ) -> Vec<&Pretrained> {
        self.items
            .iter()
            .filter(|p| p.prefab.as_deref() == Some(prefab))
            .collect()
    }

    /// A row as the table lists it: named by its ref, `group/name`, with
    /// its aliases as authored.
    fn qualified(
        &self,
        pretrained: &Pretrained,
    ) -> Pretrained {
        let mut row = pretrained.clone();
        row.name = self.id(pretrained);
        row
    }
}

/// A provider over groups, as a compiled-in table spells it.
#[derive(Debug)]
pub struct StaticPretrainedTable<'a> {
    /// The provider's name: [`WELL_KNOWN`] for the compiled-in rows.
    pub name: &'a str,

    /// A line for a listing.
    pub description: &'a str,

    /// The groups, in search order.
    pub groups: &'a [&'a StaticPretrainedGroup<'a>],
}

impl<'a> StaticPretrainedTable<'a> {
    /// The group called `name`.
    pub fn group(
        &self,
        name: &str,
    ) -> Option<&'a StaticPretrainedGroup<'a>> {
        self.groups.iter().copied().find(|g| g.name == name)
    }

    /// The owned twin: the provider.
    ///
    /// # Panics
    /// If a row's maps share a key; a kit's tests pin that none do.
    pub fn to_table(&self) -> PretrainedTable {
        PretrainedTable {
            name: self.name.to_string(),
            description: self.description.to_string(),
            groups: self.groups.iter().map(|g| g.to_group()).collect(),
        }
    }
}

impl From<&StaticPretrainedTable<'_>> for PretrainedTable {
    fn from(table: &StaticPretrainedTable<'_>) -> Self {
        table.to_table()
    }
}

/// A provider over groups: the compiled-in rows, and any table read from
/// a manifest. Its refs are `{group}/{name}`; a bare name or alias
/// searches the groups in order, first hit wins.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct PretrainedTable {
    /// The provider's name: [`WELL_KNOWN`] for the compiled-in rows.
    pub name: String,

    /// A line for a listing.
    pub description: String,

    /// The groups, in search order.
    pub groups: Vec<PretrainedGroup>,
}

impl PretrainedTable {
    /// The group called `name`.
    pub fn group(
        &self,
        name: &str,
    ) -> Option<&PretrainedGroup> {
        self.groups.iter().find(|g| g.name == name)
    }

    /// The row a ref names, with its group: `group/name` looks in that
    /// group, by name or alias; anything else searches the groups in order.
    pub fn get(
        &self,
        name: &str,
    ) -> Option<(&PretrainedGroup, &Pretrained)> {
        if let Some((group, rest)) = name.split_once('/')
            && let Some(group) = self.group(group)
        {
            return group.lookup(rest).map(|p| (group, p));
        }
        self.groups
            .iter()
            .find_map(|group| group.lookup(name).map(|p| (group, p)))
    }
}

impl PretrainedProvider for PretrainedTable {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        &self.description
    }

    fn list(&self) -> Vec<Pretrained> {
        self.groups
            .iter()
            .flat_map(|group| group.items.iter().map(|p| group.qualified(p)))
            .collect()
    }

    fn lookup(
        &self,
        name: &str,
    ) -> BunsenResult<Option<Pretrained>> {
        Ok(self.get(name).map(|(group, p)| group.qualified(p)))
    }
}

#[cfg(test)]
mod tests {
    use alloc::sync::Arc;

    use super::*;
    use crate::data::pretrained::{
        StaticBase,
        StaticResource,
        StaticResourceMap,
    };

    macro_rules! one_file {
        ($name:ident, $map:literal, $file:literal, $base:literal) => {
            static $name: StaticResourceMap<'static> = StaticResourceMap {
                name: $map,
                description: "one file",
                license: None,
                origin: None,
                namespace: "t",
                bases: &[StaticBase::Url($base)],
                resources: &[StaticResource {
                    key: "weights",
                    file: $file,
                    sha256: None,
                    kind: None,
                    sources: &[],
                }],
            };
        };
    }

    one_file!(A_SMALL_MAP, "a/small.pt", "small.pt", "https://a.example");
    one_file!(
        A_LARGE_V1_MAP,
        "a/large-v1.pt",
        "large-v1.pt",
        "https://a.example"
    );
    one_file!(
        A_LARGE_V2_MAP,
        "a/large-v2.pt",
        "large-v2.pt",
        "https://a.example"
    );
    one_file!(B_SMALL_MAP, "b/small.pt", "small.pt", "https://b.example");
    one_file!(B_TINY_MAP, "b/tiny.pt", "tiny.pt", "https://b.example");

    const fn entry(
        name: &'static str,
        aliases: &'static [&'static str],
        prefab: &'static str,
        maps: &'static [&'static StaticResourceMap<'static>],
    ) -> StaticPretrained<'static> {
        StaticPretrained {
            name,
            aliases,
            description: "an entry",
            license: None,
            origin: None,
            prefab: Some(prefab),
            maps,
        }
    }

    static A_SMALL: StaticPretrained<'static> = entry("small", &["s"], "small", &[&A_SMALL_MAP]);
    static A_LARGE_V1: StaticPretrained<'static> =
        entry("large-v1", &[], "large", &[&A_LARGE_V1_MAP]);
    static A_LARGE_V2: StaticPretrained<'static> =
        entry("large-v2", &["large"], "large", &[&A_LARGE_V2_MAP]);
    static B_SMALL: StaticPretrained<'static> = entry("small", &[], "small", &[&B_SMALL_MAP]);
    static B_TINY: StaticPretrained<'static> = entry("tiny", &[], "tiny", &[&B_TINY_MAP]);

    static A: StaticPretrainedGroup<'static> = StaticPretrainedGroup {
        name: "a",
        description: "group a",
        license: Some("MIT"),
        origin: Some("https://a.example"),
        items: &[&A_SMALL, &A_LARGE_V1, &A_LARGE_V2],
    };
    static B: StaticPretrainedGroup<'static> = StaticPretrainedGroup {
        name: "b",
        description: "group b",
        license: None,
        origin: None,
        items: &[&B_SMALL, &B_TINY],
    };
    /// The two groups as the well-known table.
    pub(crate) static TABLE: StaticPretrainedTable<'static> = StaticPretrainedTable {
        name: WELL_KNOWN,
        description: "the test rows",
        groups: &[&A, &B],
    };

    #[test]
    fn test_group_lookups_and_ids() {
        assert_eq!(A.lookup("s").map(|p| p.name), Some("small"));
        assert_eq!(A.lookup("large").map(|p| p.name), Some("large-v2"));
        assert!(A.lookup("tiny").is_none());
        assert_eq!(A.id(&A_SMALL), "a/small");
        assert_eq!(A.ids(), vec!["a/small", "a/large-v1", "a/large-v2"]);
        assert_eq!(
            A.for_prefab("large")
                .iter()
                .map(|p| p.name)
                .collect::<Vec<_>>(),
            vec!["large-v1", "large-v2"]
        );
        assert!(A.for_prefab("tiny").is_empty());
    }

    #[test]
    fn test_owned_group_matches_the_static_one() {
        let owned = A.to_group();
        assert_eq!(owned, PretrainedGroup::from(&A));
        assert_eq!(owned.name, "a");
        assert_eq!(owned.license.as_deref(), Some("MIT"));
        assert_eq!(owned.items.len(), 3);
        assert_eq!(
            owned.lookup("large").map(|p| p.name.as_str()),
            Some("large-v2")
        );
        assert_eq!(owned.ids(), A.ids());
        assert_eq!(owned.for_prefab("large").len(), 2);
        assert_eq!(owned.id(&owned.items[0]), "a/small");
        assert_eq!(owned.items[0].resources.keys(), ["weights"]);
        let json = serde_json::to_string(&owned).unwrap();
        assert_eq!(
            serde_json::from_str::<PretrainedGroup>(&json).unwrap(),
            owned
        );
    }

    /// A table lists its rows by ref, `group/name`, and answers a ref in one
    /// group or a bare name across them, first group first.
    #[test]
    fn test_the_table_lists_refs_and_looks_up() {
        let table = TABLE.to_table();
        assert_eq!(table, PretrainedTable::from(&TABLE));
        assert_eq!(table.name(), WELL_KNOWN);
        assert_eq!(table.description(), "the test rows");
        assert_eq!(table.license(), None, "a table's license is its groups'");
        assert!(table.answers_bare_names());

        let names: Vec<String> = table.list().into_iter().map(|p| p.name).collect();
        assert_eq!(
            names,
            ["a/small", "a/large-v1", "a/large-v2", "b/small", "b/tiny"]
        );
        assert_eq!(
            table.ids(),
            [
                "well-known:a/small",
                "well-known:a/large-v1",
                "well-known:a/large-v2",
                "well-known:b/small",
                "well-known:b/tiny",
            ]
        );

        let row = |name: &str| table.lookup(name).unwrap().map(|p| p.name);
        assert_eq!(row("a/small").as_deref(), Some("a/small"));
        assert_eq!(
            row("a/s").as_deref(),
            Some("a/small"),
            "an alias, in a group"
        );
        assert_eq!(row("a/large").as_deref(), Some("a/large-v2"));
        assert_eq!(row("b/small").as_deref(), Some("b/small"));
        assert_eq!(row("small").as_deref(), Some("a/small"), "first group wins");
        assert_eq!(row("tiny").as_deref(), Some("b/tiny"));
        assert_eq!(row("large").as_deref(), Some("a/large-v2"), "a bare alias");
        assert_eq!(row("c/small"), None, "no such group, and no such bare name");
        assert_eq!(row("a/tiny"), None, "the right group, no such row");
        assert_eq!(row("gigantic"), None);

        let listed = table.lookup("s").unwrap().unwrap();
        assert_eq!(listed.aliases, ["s"], "aliases stay as authored");
        assert_eq!(listed.resources.keys(), ["weights"]);
        assert_eq!(table.id(&listed), "well-known:a/small");

        assert_eq!(
            table
                .for_prefab("small")
                .iter()
                .map(|p| p.name.as_str())
                .collect::<Vec<_>>(),
            ["a/small", "b/small"]
        );
        assert_eq!(table.group("b").map(|g| g.items.len()), Some(2));
        assert_eq!(TABLE.group("b").map(|g| g.items.len()), Some(2));
        assert!(table.get("nope").is_none());

        let json = serde_json::to_string(&table).unwrap();
        assert_eq!(
            serde_json::from_str::<PretrainedTable>(&json).unwrap(),
            table
        );
    }

    /// A table is held as a trait object, which is how a factory holds
    /// every provider.
    #[test]
    fn test_a_table_behind_a_trait_object() {
        let provider: Arc<dyn PretrainedProvider> = Arc::new(TABLE.to_table());
        assert_eq!(provider.name(), "well-known");
        assert_eq!(provider.list().len(), 5);
        assert_eq!(
            provider
                .lookup("b/tiny")
                .unwrap()
                .map(|p| p.name)
                .as_deref(),
            Some("b/tiny")
        );
        assert!(format!("{provider:?}").contains("PretrainedTable"));
    }

    /// A provider that only lists gets `lookup`, `ids` and `for_prefab`
    /// for free.
    #[test]
    fn test_the_provided_lookup_scans_the_listing() {
        #[derive(Debug)]
        struct Flat;

        impl PretrainedProvider for Flat {
            fn name(&self) -> &str {
                "flat"
            }

            fn description(&self) -> &str {
                "two rows, no groups"
            }

            fn origin(&self) -> Option<&str> {
                Some("https://flat.example")
            }

            fn list(&self) -> Vec<Pretrained> {
                alloc::vec![A_SMALL.to_pretrained(), B_TINY.to_pretrained()]
            }
        }

        let flat = Flat;
        assert_eq!(flat.origin(), Some("https://flat.example"));
        assert_eq!(flat.ids(), ["flat:small", "flat:tiny"]);
        assert_eq!(
            flat.lookup("s").unwrap().map(|p| p.name).as_deref(),
            Some("small")
        );
        assert_eq!(flat.lookup("large").unwrap(), None);
        assert_eq!(flat.for_prefab("tiny").len(), 1);
    }
}