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,
},
};
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct GeometryMismatch {
pub model: String,
pub expected: WhisperGeometry,
pub found: WhisperGeometry,
}
impl GeometryMismatch {
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)
}
}
#[derive(Clone, Debug)]
pub enum WhisperReader {
Pytorch(PytorchWhisperScanner),
#[cfg(feature = "store_safetensors")]
Safetensors(SafetensorsWhisperScanner),
}
impl Default for WhisperReader {
fn default() -> Self {
Self::Pytorch(PytorchWhisperScanner::new())
}
}
impl WhisperReader {
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"
))),
}
}
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)?)
}
}
}
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,
),
}
}
}
#[derive(Clone, Debug)]
pub struct WhisperConstruct {
pub reader: WhisperReader,
pub expected: Option<WhisperGeometry>,
}
impl Default for WhisperConstruct {
fn default() -> Self {
Self::new()
}
}
impl WhisperConstruct {
pub fn new() -> Self {
Self {
reader: WhisperReader::default(),
expected: None,
}
}
pub fn with_reader(
mut self,
reader: WhisperReader,
) -> Self {
self.reader = reader;
self
}
pub fn with_scanner(
self,
scanner: PytorchWhisperScanner,
) -> Self {
self.with_reader(WhisperReader::Pytorch(scanner))
}
pub fn with_expected(
mut self,
expected: Option<WhisperGeometry>,
) -> Self {
self.expected = expected;
self
}
pub fn expected_for(
&self,
model: &PretrainedRef,
) -> Option<WhisperGeometry> {
self.expected.or_else(|| {
model
.prefab(&WHISPER_PREFABS)
.map(|prefab| prefab.to_config().geometry())
})
}
pub fn scan(
&self,
model: &PretrainedRef,
loaded: &LoadedResources,
) -> BunsenResult<WhisperApiConfig> {
let cfg = self.reader.scan_cfg(loaded)?;
self.check(model, &cfg.geometry())?;
Ok(cfg)
}
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;
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))
}
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) => {
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)
}
}
}
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,
};
#[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));
}
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")));
}
}