bunsen 0.40.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
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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
//! Hugging Face as a pretrained provider: `hf:org/repo`.

use alloc::{
    collections::BTreeSet,
    format,
    string::{
        String,
        ToString,
    },
    vec::Vec,
};

use serde::Deserialize;

#[cfg(feature = "cache")]
use super::PretrainedCache;
use super::{
    Pretrained,
    PretrainedProvider,
    Resource,
    ResourceMap,
    SAFETENSORS,
    SAFETENSORS_INDEX,
    Source,
    is_sha256_hex,
};
use crate::errors::{
    BunsenError,
    BunsenErrorKind,
    BunsenResult,
    ParseError,
};

/// The provider's default name: the `hf` of `hf:openai/whisper-tiny`, and
/// the namespace its files are cached under.
pub const HF: &str = "hf";

/// Where the hub is.
pub const HF_ORIGIN: &str = "https://huggingface.co";

/// The revision a ref resolves at unless another is named.
pub const HF_MAIN: &str = "main";

/// The file a `transformers` repo keeps its weights in, when they fit one.
pub const HF_SINGLE_FILE: &str = "model.safetensors";

/// The index of a sharded checkpoint: the shard each tensor is in.
pub const HF_INDEX_FILE: &str = "model.safetensors.index.json";

/// The model's config, as `transformers` writes it.
pub const HF_CONFIG_FILE: &str = "config.json";

/// The key `config.json` rides under.
pub const CONFIG: &str = "config";

/// The key the checkpoint is under unless the kit names another.
pub const DEFAULT_CHECKPOINT_KEY: &str = "checkpoint";

/// One entry of the hub's file listing, as its tree API spells it.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub struct HfTreeEntry {
    /// `file` or `directory`.
    #[serde(rename = "type")]
    pub kind: String,

    /// The path within the repo.
    pub path: String,

    /// The size in bytes.
    #[serde(default)]
    pub size: u64,

    /// The LFS pointer, for a file stored through LFS: its `oid` is the
    /// file's SHA-256.
    #[serde(default)]
    pub lfs: Option<HfLfs>,
}

/// The LFS part of a listing entry.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub struct HfLfs {
    /// The file's SHA-256 in hex, as the listing spells it (the hub's is
    /// lowercase); [`HfTreeEntry::sha256`] is the pin made from it.
    pub oid: String,

    /// The size in bytes.
    #[serde(default)]
    pub size: u64,
}

impl HfTreeEntry {
    /// The digest that pins this file, when LFS holds it: its oid, in
    /// lowercase hex, the one spelling of a digest the cache's pins, paths
    /// and checks use. An oid that is not 64 hex digits pins nothing.
    pub fn sha256(&self) -> Option<String> {
        let oid = self.lfs.as_ref()?.oid.to_ascii_lowercase();
        is_sha256_hex(&oid).then_some(oid)
    }
}

/// The shard number and count of `model-00001-of-00002.safetensors`.
fn shard_numbers(file: &str) -> Option<(usize, usize)> {
    let rest = file.strip_prefix("model-")?.strip_suffix(".safetensors")?;
    let (n, of) = rest.split_once("-of-")?;
    Some((n.parse().ok()?, of.parse().ok()?))
}

/// `; missing model-00003-of-00003.safetensors`: the shards of `1..=of`
/// that `shards` lacks, at most eight of them by name, or nothing when it
/// lacks none.
fn missing_shards(
    of: usize,
    shards: &[(usize, usize, &HfTreeEntry)],
) -> String {
    let present: BTreeSet<usize> = shards
        .iter()
        .map(|(n, _, _)| *n)
        .filter(|n| (1..=of).contains(n))
        .collect();
    let count = of - present.len();
    if count == 0 {
        return String::new();
    }
    let named: Vec<String> = (1..=of)
        .filter(|i| !present.contains(i))
        .take(8)
        .map(|i| format!("model-{i:05}-of-{of:05}.safetensors"))
        .collect();
    let more = if count > named.len() {
        format!(", and {} more", count - named.len())
    } else {
        String::new()
    };
    format!("; missing {}{more}", named.join(", "))
}

/// Hugging Face repos, by ref, as safetensors checkpoints: the `hf:`
/// provider.
///
/// `hf:{org}/{repo}` is a Hugging Face repo: `hf:openai/whisper-large-v3`
/// is <https://huggingface.co/openai/whisper-large-v3>. The provider
/// answers such a ref with a row whose map is the repo's safetensors
/// checkpoint, and it lists nothing: the hub is not enumerable, and a bare
/// name never reaches it
/// ([`answers_bare_names`](PretrainedProvider::answers_bare_names) is
/// `false`). A kit registers one in its default factory, after its own
/// tables and under its own checkpoint key
/// ([`with_checkpoint_key`](Self::with_checkpoint_key)); Whisper's does.
/// The kit's hook then reads the row with
/// [`SafetensorsCheckpoint`](super::SafetensorsCheckpoint).
///
/// # Resolving a ref
///
/// What a repo holds comes from the hub's file listing (its tree API, at
/// [`tree_url`](Self::tree_url)). The listing is fetched once through the
/// [`PretrainedCache`](super::PretrainedCache) and kept there, so a ref
/// resolved once resolves again offline. The listing names the files and
/// pins each LFS file by its SHA-256, so the checkpoint is
/// digest-addressed in the cache like a well-known row's:
/// `<cache>/pretrained/<kit>/hf/<sha256>/<file>`. Fetching the listing
/// needs the `fetch` feature, unless the cache has it already.
///
/// Resolving a ref without a cache is an error, and
/// [`lookup`](PretrainedProvider::lookup) always refuses: the provider
/// guesses nothing about a repo. A repo that is not there, or a gated one,
/// answers 401, and the error says so.
///
/// # Sharding
///
/// `transformers` saves a model as one `model.safetensors` until it passes
/// a shard-size limit, then as `model-00001-of-0000N.safetensors` and so
/// on, with `model.safetensors.index.json` naming the shard each tensor is
/// in. The row ([`row_from_listing`](Self::row_from_listing)) is one
/// resource, or the index and every shard, under the kit's checkpoint key:
/// `checkpoint`, or `checkpoint.index` and `checkpoint.00001`, and so on.
/// Each is labelled [`SAFETENSORS`] or [`SAFETENSORS_INDEX`] for the kit's
/// reader. A kit whose checkpoints may also come as one archive
/// ([`with_archive_checkpoint`](Self::with_archive_checkpoint): a `.nemo`)
/// gets that file instead from a repo with no safetensors checkpoint,
/// labelled as it asks. `config.json` rides along as `config` when the repo
/// has one.
/// Anything else in the repo (other frameworks' weights, tokenizer files)
/// is not a resource: a kit reads what it knows.
///
/// The shards must be the whole set their names count: every one of
/// `model-00001-of-0000N` to `model-0000N-of-0000N`, with the one `N`. A
/// listing that lacks any is refused, naming them, before anything is
/// fetched. The listing says nothing of what the index holds; that the
/// index names exactly these shards is checked when the checkpoint is read
/// ([`SafetensorsCheckpoint::from_loaded`](super::SafetensorsCheckpoint::from_loaded)).
///
/// # Revisions
///
/// The listing is at a revision: [`HF_MAIN`] unless another is named
/// ([`with_revision`](Self::with_revision)). A repo whose `main` moves is
/// seen again only when the cache is cleared or the revision is named.
/// [`with_origin`](Self::with_origin) points the provider at a mirror, and
/// [`with_name`](Self::with_name) lets two providers, at two revisions,
/// share a factory.
#[derive(Clone, Debug)]
pub struct HfProvider {
    /// The provider's name, and the namespace its files are cached under.
    pub name: String,

    /// Where the hub is: [`HF_ORIGIN`], or a mirror.
    pub origin: String,

    /// The revision every ref resolves at: a branch, a tag, or a commit.
    pub revision: String,

    /// The key the checkpoint goes under: the kit's.
    pub checkpoint_key: String,

    /// The other files a row carries when the repo has them, beyond the
    /// checkpoint and `config.json`: a kit's vocabulary, say.
    pub extra_files: Vec<HfExtraFile>,

    /// The one-file checkpoints a repo without a safetensors checkpoint may
    /// hold instead, in preference order: a `.nemo`, say.
    pub archive_checkpoints: Vec<HfArchiveCheckpoint>,
}

/// A one-file checkpoint an [`HfProvider`] row takes when the repo has no
/// safetensors checkpoint: the repo's one file ending in `extension`,
/// labelled `kind` for the kit's reader.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HfArchiveCheckpoint {
    /// The file name's ending: `.nemo`.
    pub extension: String,

    /// The resource's `kind`.
    pub kind: String,
}

/// A file an [`HfProvider`] row carries when the repo has it: under `key`,
/// labelled `kind` for the kit's reader.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct HfExtraFile {
    /// The resource key.
    pub key: String,

    /// The file's path in the repo: `tokenizer.json`.
    pub path: String,

    /// The resource's `kind`.
    pub kind: String,
}

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

impl HfProvider {
    /// `hf`, at [`HF_MAIN`], the checkpoint under `checkpoint`.
    pub fn new() -> Self {
        Self {
            name: HF.to_string(),
            origin: HF_ORIGIN.to_string(),
            revision: HF_MAIN.to_string(),
            checkpoint_key: DEFAULT_CHECKPOINT_KEY.to_string(),
            extra_files: Vec::new(),
            archive_checkpoints: Vec::new(),
        }
    }

    /// Repos at a mirror of the hub, which serves the same paths.
    pub fn with_origin(
        mut self,
        origin: impl Into<String>,
    ) -> Self {
        self.origin = origin.into();
        self
    }

    /// Another name: two of these in one factory, at two revisions, say.
    pub fn with_name(
        mut self,
        name: impl Into<String>,
    ) -> Self {
        self.name = name.into();
        self
    }

    /// Repos at `revision`.
    pub fn with_revision(
        mut self,
        revision: impl Into<String>,
    ) -> Self {
        self.revision = revision.into();
        self
    }

    /// The checkpoint under `key`.
    pub fn with_checkpoint_key(
        mut self,
        key: impl Into<String>,
    ) -> Self {
        self.checkpoint_key = key.into();
        self
    }

    /// Rows carry `path` under `key`, labelled `kind`, when the repo has
    /// it: `with_extra_file("vocabulary", "tokenizer.json", "tokenizer.json")`.
    pub fn with_extra_file(
        mut self,
        key: impl Into<String>,
        path: impl Into<String>,
        kind: impl Into<String>,
    ) -> Self {
        self.extra_files.push(HfExtraFile {
            key: key.into(),
            path: path.into(),
            kind: kind.into(),
        });
        self
    }

    /// A repo without a safetensors checkpoint may hold one file ending in
    /// `extension` instead, its checkpoint, labelled `kind`:
    /// `with_archive_checkpoint(".nemo", "nemo")`.
    pub fn with_archive_checkpoint(
        mut self,
        extension: impl Into<String>,
        kind: impl Into<String>,
    ) -> Self {
        self.archive_checkpoints.push(HfArchiveCheckpoint {
            extension: extension.into(),
            kind: kind.into(),
        });
        self
    }

    /// The URL `file` in `org/repo` is served at, at this provider's
    /// revision.
    pub fn url(
        &self,
        org: &str,
        repo: &str,
        file: &str,
    ) -> String {
        format!(
            "{}/{org}/{repo}/resolve/{}/{file}",
            self.origin, self.revision
        )
    }

    /// The URL of `org/repo`'s file listing at this provider's revision.
    pub fn tree_url(
        &self,
        org: &str,
        repo: &str,
    ) -> String {
        format!(
            "{}/api/models/{org}/{repo}/tree/{}",
            self.origin, self.revision
        )
    }

    /// The `org` and `repo` of a ref, which is exactly `org/repo`.
    ///
    /// # Errors
    /// [`Illegal`](BunsenErrorKind::Illegal), with a [`ParseError`], for
    /// anything else. A caller that took the ref from outside the program
    /// re-marks it [`as_policy`](BunsenError::as_policy), as
    /// [`PretrainedFactory::resolve`](super::PretrainedFactory::resolve)
    /// does.
    pub fn split<'a>(
        &self,
        name: &'a str,
    ) -> BunsenResult<(&'a str, &'a str)> {
        match name.split_once('/') {
            Some((org, repo)) if !org.is_empty() && !repo.is_empty() && !repo.contains('/') => {
                Ok((org, repo))
            }
            _ => Err(BunsenError::from_cause(
                BunsenErrorKind::Illegal,
                ParseError::new("Hugging Face ref")
                    .input(format!("{}:{name}", self.name))
                    .because("a ref is org/repo"),
            )),
        }
    }

    /// The listing resource for `org/repo`: the tree API's JSON, unpinned,
    /// under this provider's namespace.
    pub fn listing_resource(
        &self,
        org: &str,
        repo: &str,
    ) -> Resource {
        Resource {
            key: "listing".to_string(),
            file: format!("{org}--{repo}--{}.json", self.revision.replace('/', "-")),
            sha256: None,
            kind: Some("json".to_string()),
            namespace: self.name.clone(),
            sources: vec![Source::Url(self.tree_url(org, repo))],
        }
    }

    /// One resource of the row: `file` of `org/repo`, pinned when the
    /// listing pins it.
    fn resource(
        &self,
        org: &str,
        repo: &str,
        key: &str,
        entry: &HfTreeEntry,
        kind: &str,
    ) -> Resource {
        Resource {
            key: key.to_string(),
            file: entry.path.clone(),
            sha256: entry.sha256(),
            kind: Some(kind.to_string()),
            namespace: self.name.clone(),
            sources: vec![Source::Url(self.url(org, repo, &entry.path))],
        }
    }

    /// The row for `name` from the repo's file listing: the safetensors
    /// checkpoint, one file or the index and every shard, or else the first
    /// of the [`archive_checkpoints`](Self::archive_checkpoints) the repo
    /// holds, pinned by the hub's LFS digests; `config.json` when there is
    /// one, and each of the [`extra_files`](Self::extra_files) the repo has.
    ///
    /// # Errors
    /// As [`split`](Self::split) for a malformed ref.
    /// [`InvalidResource`](BunsenErrorKind::InvalidResource), under a frame
    /// naming the ref, when the listing has neither [`HF_SINGLE_FILE`] nor
    /// [`HF_INDEX_FILE`] with a complete set of shards, nor an archive
    /// checkpoint, naming what it has instead, and the shards a set of one
    /// `N` lacks: the repo is not the checkpoint a ref names. So too when it
    /// holds two files of an archive checkpoint's ending.
    pub fn row_from_listing(
        &self,
        name: &str,
        listing: &[HfTreeEntry],
    ) -> BunsenResult<Pretrained> {
        let (org, repo) = self.split(name)?;
        let files: Vec<&HfTreeEntry> = listing.iter().filter(|e| e.kind == "file").collect();
        let file = |path: &str| files.iter().copied().find(|e| e.path == path);
        let origin = format!("{}/{org}/{repo}", self.origin);
        let key = self.checkpoint_key.as_str();

        let mut resources = ResourceMap::new(name);
        resources.origin = Some(origin.clone());
        let what;
        if let Some(single) = file(HF_SINGLE_FILE) {
            resources.insert(self.resource(org, repo, key, single, SAFETENSORS));
            what = format!("{HF_SINGLE_FILE}, {}", megabytes(single.size));
        } else if let Some(index) = file(HF_INDEX_FILE) {
            let mut shards: Vec<(usize, usize, &HfTreeEntry)> = files
                .iter()
                .filter_map(|e| shard_numbers(&e.path).map(|(n, of)| (n, of, *e)))
                .collect();
            shards.sort_by_key(|(n, _, _)| *n);
            let of = shards.first().map(|(_, of, _)| *of).unwrap_or(0);
            let one_set = shards.iter().all(|(_, o, _)| *o == of);
            let complete = !shards.is_empty()
                && one_set
                && shards.len() == of
                && shards.iter().enumerate().all(|(i, (n, _, _))| *n == i + 1);
            if !complete {
                let has: Vec<&str> = shards.iter().map(|(_, _, e)| e.path.as_str()).collect();
                let mut message = format!(
                    "{HF_INDEX_FILE} with an incomplete set of shards: {}",
                    if has.is_empty() {
                        "none".to_string()
                    } else {
                        has.join(", ")
                    }
                );
                if one_set {
                    message.push_str(&missing_shards(of, &shards));
                }
                return Err(
                    BunsenError::invalid_resource(message).context(format!("{}:{name}", self.name))
                );
            }
            resources.insert(self.resource(
                org,
                repo,
                &format!("{key}.index"),
                index,
                SAFETENSORS_INDEX,
            ));
            let mut total = 0;
            for (n, _, entry) in &shards {
                total += entry.size;
                resources.insert(self.resource(
                    org,
                    repo,
                    &format!("{key}.{n:05}"),
                    entry,
                    SAFETENSORS,
                ));
            }
            what = format!("{} shards, {}", shards.len(), megabytes(total));
        } else if let Some((archive, kind)) = self.archive_checkpoint(name, &files)? {
            resources.insert(self.resource(org, repo, key, archive, kind));
            what = format!("{}, {}", archive.path, megabytes(archive.size));
        } else {
            let weights: Vec<&str> = files
                .iter()
                .map(|e| e.path.as_str())
                .filter(|p| {
                    p.ends_with(".safetensors")
                        || p.ends_with(".bin")
                        || p.ends_with(".h5")
                        || p.ends_with(".msgpack")
                        || p.ends_with(".pt")
                        || p.ends_with(".gguf")
                        || p.ends_with(".nemo")
                })
                .collect();
            let archives: String = self
                .archive_checkpoints
                .iter()
                .map(|a| format!(", or one *{}", a.extension))
                .collect();
            return Err(BunsenError::invalid_resource(format!(
                "no {HF_SINGLE_FILE} or {HF_INDEX_FILE}{archives} at {}; the repo has: {}",
                self.revision,
                if weights.is_empty() {
                    "no weights".to_string()
                } else {
                    weights.join(", ")
                }
            ))
            .context(format!("{}:{name}", self.name)));
        }
        let mut with: Vec<&str> = Vec::new();
        if let Some(config) = file(HF_CONFIG_FILE) {
            resources.insert(self.resource(org, repo, CONFIG, config, "json"));
            with.push(HF_CONFIG_FILE);
        }
        for extra in &self.extra_files {
            if let Some(entry) = file(&extra.path) {
                resources.insert(self.resource(org, repo, &extra.key, entry, &extra.kind));
                with.push(&extra.path);
            }
        }
        let with = if with.is_empty() {
            String::new()
        } else {
            format!(", with {}", with.join(" and "))
        };
        let description = format!(
            "{org}/{repo} on Hugging Face at {}: {what}{with}",
            self.revision
        );
        resources.description.clone_from(&description);
        Ok(Pretrained {
            name: name.to_string(),
            aliases: Vec::new(),
            description,
            license: None,
            origin: Some(origin),
            prefab: None,
            resources,
        })
    }

    /// The first of [`archive_checkpoints`](Self::archive_checkpoints) the
    /// repo holds, with its kind.
    ///
    /// # Errors
    /// [`InvalidResource`](BunsenErrorKind::InvalidResource), under a frame
    /// naming the ref, naming them, when the repo holds more than one file
    /// of that ending: which is the checkpoint is not the provider's guess.
    fn archive_checkpoint<'e>(
        &'e self,
        name: &str,
        files: &[&'e HfTreeEntry],
    ) -> BunsenResult<Option<(&'e HfTreeEntry, &'e str)>> {
        for archive in &self.archive_checkpoints {
            let found: Vec<&HfTreeEntry> = files
                .iter()
                .copied()
                .filter(|e| e.path.ends_with(&archive.extension))
                .collect();
            match found.as_slice() {
                [] => continue,
                [one] => return Ok(Some((one, archive.kind.as_str()))),
                many => {
                    let names: Vec<&str> = many.iter().map(|e| e.path.as_str()).collect();
                    return Err(BunsenError::invalid_resource(format!(
                        "{} {} files, and no way to tell the checkpoint: {}",
                        many.len(),
                        archive.extension,
                        names.join(", ")
                    ))
                    .context(format!("{}:{name}", self.name)));
                }
            }
        }
        Ok(None)
    }

    /// The repo's file listing, through the cache: fetched once under
    /// `kit`, read from there after.
    #[cfg(feature = "cache")]
    fn listing(
        &self,
        name: &str,
        kit: &str,
        cache: &PretrainedCache,
    ) -> BunsenResult<Vec<HfTreeEntry>> {
        use crate::errors::{
            LookupError,
            LookupProblem,
            ResultContext,
            sys_at,
        };

        let (org, repo) = self.split(name)?;
        let resource = self.listing_resource(org, repo);
        let resolved = cache.resolve(kit, &resource);
        // The hub answers 401 to an anonymous caller both for a repo that is
        // not there and for a gated one.
        let unauthorized = resolved
            .as_ref()
            .err()
            .and_then(|e| e.find::<LookupError>())
            .is_some_and(|l| l.problem == LookupProblem::Unauthorized);
        let resolved = resolved.with_context(|| {
            if unauthorized {
                format!(
                    "{}:{name}: the repo does not exist, or is gated (bunsen fetches anonymously, so a gated repo is unreachable)",
                    self.name
                )
            } else {
                format!("{}:{name}: file listing", self.name)
            }
        })?;
        let path = &resolved.path;
        let file = std::fs::File::open(path)
            .map_err(sys_at("open", path))
            .with_context(|| format!("{}:{name}: file listing", self.name))?;
        serde_json::from_reader(std::io::BufReader::new(file)).map_err(|e| {
            BunsenError::from_cause(
                BunsenErrorKind::InvalidResource,
                ParseError::new("Hugging Face file listing")
                    .at(path.display())
                    .with_source(e),
            )
            .with_details(format!(
                "the listing is cached unpinned; remove {} to fetch it again",
                path.display()
            ))
            .context(format!("{}:{name}: file listing", self.name))
        })
    }
}

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

    fn description(&self) -> &str {
        "Hugging Face repos by ref: `hf:org/repo` is its safetensors checkpoint at a revision, pinned by the hub's listing; lists nothing"
    }

    fn origin(&self) -> Option<&str> {
        Some(HF_ORIGIN)
    }

    /// Nothing: the hub is not enumerable.
    fn list(&self) -> Vec<Pretrained> {
        Vec::new()
    }

    /// An error: what a repo holds is the hub's to say, through a cache.
    ///
    /// # Errors
    /// As [`split`](Self::split) for a malformed ref;
    /// [`Policy`](BunsenErrorKind::Policy) otherwise, always: the request
    /// needs a cache.
    fn lookup(
        &self,
        name: &str,
    ) -> BunsenResult<Option<Pretrained>> {
        self.split(name)?;
        Err(BunsenError::policy(
            "a Hugging Face ref is resolved through a cache, which asks the hub what the repo holds",
        )
        .context(format!("{}:{name}", self.name)))
    }

    /// The row for `org/repo` from its file listing, fetched once into
    /// `cache` under `kit` and read from there after.
    ///
    /// # Errors
    /// As [`split`](Self::split) and
    /// [`row_from_listing`](Self::row_from_listing). For a listing that
    /// cannot be had, the cache's error under a frame naming the ref: a repo
    /// that is not there, or a gated one, answers 401, a
    /// [`Lookup`](BunsenErrorKind::Lookup) whose
    /// [`LookupError`](crate::errors::LookupError) problem is
    /// [`Unauthorized`](crate::errors::LookupProblem::Unauthorized), and an
    /// offline cache that never saw the repo is a
    /// [`Policy`](BunsenErrorKind::Policy) error. A cached listing that does
    /// not parse is
    /// [`InvalidResource`](BunsenErrorKind::InvalidResource), with a
    /// [`ParseError`].
    #[cfg(feature = "cache")]
    fn resolve(
        &self,
        name: &str,
        kit: &str,
        cache: &PretrainedCache,
    ) -> BunsenResult<Option<Pretrained>> {
        let listing = self.listing(name, kit, cache)?;
        self.row_from_listing(name, &listing).map(Some)
    }

    /// Never: a bare name means a row bunsen knows.
    fn answers_bare_names(&self) -> bool {
        false
    }
}

/// `151.1 MB` or `6.17 GB`.
fn megabytes(bytes: u64) -> String {
    if bytes >= 1_000_000_000 {
        format!("{:.2} GB", bytes as f64 / 1e9)
    } else {
        format!("{:.1} MB", bytes as f64 / 1e6)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::errors::testing::{
        ErrorMatcher,
        text,
    };

    fn entry(
        path: &str,
        size: u64,
        sha256: Option<&str>,
    ) -> HfTreeEntry {
        HfTreeEntry {
            kind: "file".to_string(),
            path: path.to_string(),
            size,
            lfs: sha256.map(|oid| HfLfs {
                oid: oid.to_string(),
                size,
            }),
        }
    }

    const TINY_SHA: &str = "7ebd0e69e78190ff5b1b2e6c7c0a6a9b1a4e2f0f0b3d2c1a9f8e7d6c5b4a3f2e";
    const SHARD_1: &str = "1111111111111111111111111111111111111111111111111111111111111111";
    const SHARD_2: &str = "2222222222222222222222222222222222222222222222222222222222222222";

    /// A single-file repo: the checkpoint pinned by its LFS digest, the
    /// config along, nothing else of the repo.
    #[test]
    fn test_a_single_file_repo_is_one_pinned_checkpoint() {
        let hf = HfProvider::new();
        let listing = vec![
            entry(".gitattributes", 1438, None),
            entry("config.json", 1983, None),
            entry("model.safetensors", 151_061_672, Some(TINY_SHA)),
            entry("pytorch_model.bin", 151_095_027, Some(SHARD_1)),
            entry("tokenizer.json", 2_480_466, None),
        ];
        let row = hf
            .row_from_listing("openai/whisper-tiny", &listing)
            .unwrap();
        assert_eq!(row.name, "openai/whisper-tiny");
        assert_eq!(hf.id(&row), "hf:openai/whisper-tiny");
        assert_eq!(
            row.origin.as_deref(),
            Some("https://huggingface.co/openai/whisper-tiny")
        );
        assert_eq!(
            row.description,
            "openai/whisper-tiny on Hugging Face at main: model.safetensors, 151.1 MB, with config.json"
        );
        assert_eq!(row.prefab, None);
        row.resources.validate().unwrap();
        assert_eq!(row.resources.keys(), ["checkpoint", "config"]);

        let checkpoint = row.resources.get("checkpoint").unwrap();
        assert_eq!(checkpoint.file, "model.safetensors");
        assert_eq!(checkpoint.sha256.as_deref(), Some(TINY_SHA));
        assert_eq!(checkpoint.kind.as_deref(), Some("safetensors"));
        assert_eq!(checkpoint.namespace, "hf");
        assert_eq!(
            checkpoint.sources,
            [Source::Url(
                "https://huggingface.co/openai/whisper-tiny/resolve/main/model.safetensors"
                    .to_string()
            )]
        );
        let config = row.resources.get("config").unwrap();
        assert_eq!(config.file, "config.json");
        assert_eq!(config.sha256, None);
        assert_eq!(config.kind.as_deref(), Some("json"));
    }

    /// An extra file rides along under its key and kind, pinned when the
    /// hub pins it; one the repo lacks is simply absent.
    #[test]
    fn test_extra_files_ride_along() {
        let hf = HfProvider::new()
            .with_extra_file("vocabulary", "tokenizer.json", "tokenizer.json")
            .with_extra_file("merges", "merges.txt", "text");
        let listing = vec![
            entry("config.json", 1983, None),
            entry("model.safetensors", 151_061_672, Some(TINY_SHA)),
            entry("tokenizer.json", 2_480_466, Some(SHARD_1)),
        ];
        let row = hf.row_from_listing("org/asr", &listing).unwrap();
        assert_eq!(row.resources.keys(), ["checkpoint", "config", "vocabulary"]);
        let vocab = row.resources.get("vocabulary").unwrap();
        assert_eq!(vocab.file, "tokenizer.json");
        assert_eq!(vocab.kind.as_deref(), Some("tokenizer.json"));
        assert_eq!(vocab.sha256.as_deref(), Some(SHARD_1));
        assert_eq!(
            row.description,
            "org/asr on Hugging Face at main: model.safetensors, 151.1 MB, with config.json and tokenizer.json"
        );
    }

    /// A sharded repo: the index and every shard, in shard order, under
    /// the checkpoint key; an incomplete set is refused.
    #[test]
    fn test_a_sharded_repo_is_the_index_and_its_shards() {
        let hf = HfProvider::new().with_checkpoint_key("weights");
        let listing = vec![
            entry(
                "model-00002-of-00002.safetensors",
                1_170_000_000,
                Some(SHARD_2),
            ),
            entry(
                "model-00001-of-00002.safetensors",
                5_000_000_000,
                Some(SHARD_1),
            ),
            entry("model.safetensors.index.json", 71_000, None),
        ];
        let row = hf
            .row_from_listing("ivrit-ai/whisper-large-v3", &listing)
            .unwrap();
        assert_eq!(
            row.resources.keys(),
            ["weights.00001", "weights.00002", "weights.index"]
        );
        assert_eq!(
            row.description,
            "ivrit-ai/whisper-large-v3 on Hugging Face at main: 2 shards, 6.17 GB"
        );
        let index = row.resources.get("weights.index").unwrap();
        assert_eq!(index.file, "model.safetensors.index.json");
        assert_eq!(index.kind.as_deref(), Some("safetensors-index"));
        assert_eq!(index.sha256, None);
        let first = row.resources.get("weights.00001").unwrap();
        assert_eq!(first.file, "model-00001-of-00002.safetensors");
        assert_eq!(first.sha256.as_deref(), Some(SHARD_1));
        assert_eq!(first.kind.as_deref(), Some("safetensors"));

        let incomplete = vec![
            entry("model-00002-of-00002.safetensors", 1, Some(SHARD_2)),
            entry("model.safetensors.index.json", 71_000, None),
        ];
        ErrorMatcher::kind(BunsenErrorKind::InvalidResource)
            .message_contains("incomplete set of shards")
            .frame_contains("hf:ivrit-ai/whisper-large-v3")
            .assert_err(&hf.row_from_listing("ivrit-ai/whisper-large-v3", &incomplete));
    }

    /// A set numbered from 1 with no gap is still incomplete when its last
    /// shards are missing: the `-of-N` total says how many there are, and
    /// the error names the ones the listing lacks.
    #[test]
    fn test_a_sharded_repo_missing_its_last_shards_is_refused() {
        let hf = HfProvider::new();
        let listing = vec![
            entry("model.safetensors.index.json", 71_000, None),
            entry("model-00001-of-00003.safetensors", 1, Some(SHARD_1)),
            entry("model-00002-of-00003.safetensors", 1, Some(SHARD_2)),
        ];
        ErrorMatcher::kind(BunsenErrorKind::InvalidResource)
            .message_contains("incomplete set of shards")
            .message_contains("missing model-00003-of-00003.safetensors")
            .assert_err(&hf.row_from_listing("org/repo", &listing));

        // A long tail is named eight shards at most, then counted.
        let listing = vec![
            entry("model.safetensors.index.json", 71_000, None),
            entry("model-00001-of-00012.safetensors", 1, Some(SHARD_1)),
        ];
        ErrorMatcher::kind(BunsenErrorKind::InvalidResource)
            .message_contains("missing model-00002-of-00012.safetensors, ")
            .message(text::ends_with(
                "model-00009-of-00012.safetensors, and 3 more",
            ))
            .assert_err(&hf.row_from_listing("org/repo", &listing));
    }

    /// A repo with weights in another format only is refused naming them;
    /// a malformed ref is refused before any listing is read; a lookup
    /// without a cache is refused.
    #[test]
    fn test_what_is_refused() {
        let hf = HfProvider::new();
        let listing = vec![
            entry("pytorch_model.bin", 10, Some(SHARD_1)),
            entry("tf_model.h5", 10, Some(SHARD_2)),
            entry("README.md", 10, None),
        ];
        ErrorMatcher::kind(BunsenErrorKind::InvalidResource)
            .message_contains("no model.safetensors")
            .message_contains("pytorch_model.bin, tf_model.h5")
            .assert_err(&hf.row_from_listing("some/repo", &listing));

        for bad in [
            "whisper-tiny",
            "openai/",
            "/whisper-tiny",
            "openai/whisper/tiny",
            "",
        ] {
            ErrorMatcher::kind(BunsenErrorKind::Illegal)
                .message_contains("org/repo")
                .has_cause::<ParseError>()
                .assert_err(&hf.row_from_listing(bad, &[]));
        }
        ErrorMatcher::kind(BunsenErrorKind::Policy)
            .message_contains("through a cache")
            .assert_err(&hf.lookup("openai/whisper-tiny"));
        assert!(hf.list().is_empty());
        assert!(hf.ids().is_empty());
        assert!(!hf.answers_bare_names());
        assert_eq!(hf.origin(), Some("https://huggingface.co"));
    }

    /// A repo with no safetensors checkpoint gives its one archive
    /// checkpoint, under the checkpoint key and the archive's kind; two of
    /// one ending are refused, naming them; a provider that asks for none
    /// refuses the repo, naming the archive among its weights.
    #[test]
    fn test_an_archive_checkpoint_stands_in() {
        let hf = HfProvider::new().with_archive_checkpoint(".nemo", "nemo");
        let listing = vec![
            entry(".gitattributes", 1438, None),
            entry("README.md", 10, None),
            entry(
                "diar_streaming_sortformer_4spk-v2.nemo",
                471_367_680,
                Some(TINY_SHA),
            ),
        ];
        let row = hf
            .row_from_listing("nvidia/diar_streaming_sortformer_4spk-v2", &listing)
            .unwrap();
        row.resources.validate().unwrap();
        assert_eq!(row.resources.keys(), ["checkpoint"]);
        let checkpoint = row.resources.get("checkpoint").unwrap();
        assert_eq!(checkpoint.file, "diar_streaming_sortformer_4spk-v2.nemo");
        assert_eq!(checkpoint.kind.as_deref(), Some("nemo"));
        assert_eq!(checkpoint.sha256.as_deref(), Some(TINY_SHA));
        assert!(
            row.description
                .ends_with(": diar_streaming_sortformer_4spk-v2.nemo, 471.4 MB"),
            "{}",
            row.description
        );

        // A safetensors checkpoint is preferred.
        let mut both = listing.clone();
        both.push(entry("model.safetensors", 10, Some(SHARD_1)));
        let row = hf.row_from_listing("org/repo", &both).unwrap();
        assert_eq!(
            row.resources.get("checkpoint").unwrap().kind.as_deref(),
            Some("safetensors")
        );

        let mut two = listing.clone();
        two.push(entry("other.nemo", 10, Some(SHARD_1)));
        ErrorMatcher::kind(BunsenErrorKind::InvalidResource)
            .message_contains("2 .nemo files")
            .message_contains("other.nemo")
            .frame_contains("hf:org/repo")
            .assert_err(&hf.row_from_listing("org/repo", &two));

        ErrorMatcher::kind(BunsenErrorKind::InvalidResource)
            .message_contains("the repo has: diar_streaming_sortformer_4spk-v2.nemo")
            .assert_err(&HfProvider::new().row_from_listing("org/repo", &listing));
        ErrorMatcher::kind(BunsenErrorKind::InvalidResource)
            .message_contains("or one *.nemo at main; the repo has: no weights")
            .assert_err(&hf.row_from_listing("org/repo", &listing[..2]));
    }

    /// The URLs: a file at a revision, and the listing.
    #[test]
    fn test_urls_carry_the_revision() {
        let hf = HfProvider::new().with_revision("06f233fe06e710322aca913c1bc4249a0d71fce1");
        assert_eq!(
            hf.url("openai", "whisper-large-v3", "model.safetensors"),
            "https://huggingface.co/openai/whisper-large-v3/resolve/06f233fe06e710322aca913c1bc4249a0d71fce1/model.safetensors"
        );
        assert_eq!(
            hf.tree_url("openai", "whisper-large-v3"),
            "https://huggingface.co/api/models/openai/whisper-large-v3/tree/06f233fe06e710322aca913c1bc4249a0d71fce1"
        );
        let listing = hf.listing_resource("openai", "whisper-large-v3");
        assert_eq!(
            listing.file,
            "openai--whisper-large-v3--06f233fe06e710322aca913c1bc4249a0d71fce1.json"
        );
        assert_eq!(listing.namespace, "hf");
        listing.validate().unwrap();

        let named = HfProvider::new().with_name("hf-dev").with_revision("dev");
        assert_eq!(named.name(), "hf-dev");
        assert_eq!(named.listing_resource("o", "r").namespace, "hf-dev");
    }

    /// The listing as the hub serves it: LFS files carry a digest, the
    /// rest do not.
    #[test]
    fn test_the_listing_parses() {
        let json = r#"[
          {"type":"file","oid":"abc","size":1983,"path":"config.json"},
          {"type":"file","oid":"def","size":151061672,"path":"model.safetensors",
           "lfs":{"oid":"7ebd0e69e78190ff5b1b2e6c7c0a6a9b1a4e2f0f0b3d2c1a9f8e7d6c5b4a3f2e","size":151061672,"pointerSize":135}},
          {"type":"directory","oid":"ghi","size":0,"path":"onnx"}
        ]"#;
        let listing: Vec<HfTreeEntry> = serde_json::from_str(json).unwrap();
        assert_eq!(listing.len(), 3);
        assert_eq!(listing[0].sha256(), None);
        assert_eq!(listing[1].sha256().as_deref(), Some(TINY_SHA));
        assert_eq!(listing[2].kind, "directory");
        let row = HfProvider::new()
            .row_from_listing("openai/whisper-tiny", &listing)
            .unwrap();
        assert_eq!(row.resources.keys(), ["checkpoint", "config"]);
    }

    /// A digest's hex case carries nothing, and the cache's pins, paths and
    /// checks are all lowercase: a listing that spells a digest in
    /// uppercase pins the file by the same digest, in lowercase.
    #[test]
    fn test_an_uppercase_digest_pins_in_lowercase() {
        let json = format!(
            r#"[{{"type":"file","size":3,"path":"model.safetensors","lfs":{{"oid":"{}","size":3}}}}]"#,
            TINY_SHA.to_uppercase()
        );
        let listing: Vec<HfTreeEntry> = serde_json::from_str(&json).unwrap();
        assert_eq!(listing[0].sha256().as_deref(), Some(TINY_SHA));
        let row = HfProvider::new()
            .row_from_listing("org/repo", &listing)
            .unwrap();
        row.resources.validate().unwrap();
        assert_eq!(
            row.resources.get("checkpoint").unwrap().sha256.as_deref(),
            Some(TINY_SHA)
        );
    }

    /// Through a cache, against a loopback hub: the listing is fetched
    /// once, the row is pinned by it, and an offline cache that has the
    /// listing resolves the ref again without the network; a hub that
    /// answers 401 is reported with the ref and what 401 means.
    #[cfg(all(feature = "cache", feature = "fetch"))]
    #[test]
    fn test_resolve_fetches_the_listing_once_and_keeps_it() {
        use crate::data::{
            cache::{
                BunsenDiskCacheOptions,
                testing::{
                    serve_once,
                    serve_status,
                },
            },
            pretrained::{
                CacheStatus,
                PretrainedCacheOptions,
            },
        };

        static LISTING: &[u8] = br#"[
          {"type":"file","oid":"abc","size":1983,"path":"config.json"},
          {"type":"file","oid":"def","size":3,"path":"model.safetensors",
           "lfs":{"oid":"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad","size":3,"pointerSize":135}}
        ]"#;
        let dir = tempfile::tempdir().unwrap();
        let cache_at = |offline: bool| {
            PretrainedCache::new(
                PretrainedCacheOptions::default()
                    .with_disk(
                        BunsenDiskCacheOptions::default()
                            .with_cache_dir(Some(dir.path().join("cache")))
                            .without_transfer_observers(),
                    )
                    .with_offline(offline),
            )
            .unwrap()
        };
        // The server answers one request, whatever the path: the listing.
        let origin_of = |url: String| url.rsplit_once('/').unwrap().0.to_string();
        let hf = HfProvider::new().with_origin(origin_of(serve_once("any", LISTING)));

        let online = cache_at(false);
        let row = hf.resolve("org/repo", "kit", &online).unwrap().unwrap();
        assert_eq!(row.name, "org/repo");
        let checkpoint = row.resources.get("checkpoint").unwrap();
        assert_eq!(
            checkpoint.sha256.as_deref(),
            Some("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
        );
        assert_eq!(
            checkpoint.sources,
            [Source::Url(format!(
                "{}/org/repo/resolve/main/model.safetensors",
                hf.origin
            ))]
        );
        assert_eq!(
            online.status("kit", &hf.listing_resource("org", "repo")),
            CacheStatus::Cached,
            "the listing stays in the cache"
        );

        // Offline, the same ref resolves from the cached listing; the
        // server took one request and is gone.
        let offline = cache_at(true);
        let again = hf.resolve("org/repo", "kit", &offline).unwrap().unwrap();
        assert_eq!(again, row);
        ErrorMatcher::kind(BunsenErrorKind::Policy)
            .display(text::starts_with("hf:org/other: file listing: "))
            .message_contains("the cache is offline")
            .assert_err(&hf.resolve("org/other", "kit", &offline));

        let gone = HfProvider::new().with_origin(origin_of(serve_status("any", 401)));
        ErrorMatcher::kind(BunsenErrorKind::Lookup)
            .display(text::starts_with(
                "hf:nobody/nothing: the repo does not exist, or is gated",
            ))
            .cause(crate::errors::testing::predicate(
                "an unauthorized lookup",
                |c: &crate::errors::LookupError| {
                    c.problem == crate::errors::LookupProblem::Unauthorized
                },
            ))
            .assert_err(&gone.resolve("nobody/nothing", "kit", &online));
    }
}