use alloc::{
fmt,
sync::Arc,
};
use core::error::Error;
use burn::tensor::Device;
use crate::{
data::{
builds::{
BuildRef,
ConstructInit,
GEOMETRY,
},
catalog::presets::ResolvedPresetSet,
formats::SafetensorsCheckpoint,
resources::{
Fuse,
GIVEN_NAMESPACE,
LoadedResourceSet,
ResourceCache,
ResourceSet,
SAFETENSORS,
},
},
errors::{
BunsenError,
BunsenErrorKind,
BunsenResult,
ResultContext,
},
kits::{
speech::asr::whisper::{
Whisper,
WhisperApiConfig,
WhisperGeometry,
driver::{
WhisperConstruct,
WhisperPolicy,
},
pretrained::{
CHECKPOINT,
PytorchWhisperScanner,
SafetensorsWhisperScanner,
VOCABULARY,
WHISPER_KIT,
WhisperVocabulary,
},
},
support::{
model_config::resolve_ref_presets,
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),
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()),
Some(kind) if kind.starts_with(SAFETENSORS) => {
Ok(Self::Safetensors(SafetensorsWhisperScanner::new()))
}
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: &LoadedResourceSet,
) -> BunsenResult<WhisperApiConfig> {
match self {
Self::Pytorch(scanner) => Ok(scanner.scan_cfg(loaded.expect(CHECKPOINT)?)?.1),
Self::Safetensors(scanner) => {
scanner.scan_cfg(&SafetensorsCheckpoint::from_loaded(loaded, CHECKPOINT)?)
}
}
}
pub fn load(
&self,
loaded: &LoadedResourceSet,
device: &Device,
) -> BunsenResult<(Whisper, WhisperApiConfig)> {
match self {
Self::Pytorch(scanner) => scanner.load::<_>(loaded.expect(CHECKPOINT)?, device),
Self::Safetensors(scanner) => scanner.load(
&SafetensorsCheckpoint::from_loaded(loaded, CHECKPOINT)?,
device,
),
}
}
}
#[derive(Clone, Debug)]
pub struct WhisperConstructConfig {
pub reader: WhisperReader,
pub expected: Option<WhisperGeometry>,
}
impl Default for WhisperConstructConfig {
fn default() -> Self {
Self::new()
}
}
impl WhisperConstructConfig {
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: &BuildRef,
) -> BunsenResult<Option<WhisperGeometry>> {
if self.expected.is_some() {
return Ok(self.expected);
}
model.stated(GEOMETRY)
}
pub fn scan(
&self,
model: &BuildRef,
loaded: &LoadedResourceSet,
) -> BunsenResult<WhisperApiConfig> {
let cfg = self.reader.scan_cfg(loaded)?;
self.check(model, &cfg.geometry())?;
Ok(cfg)
}
fn check(
&self,
model: &BuildRef,
found: &WhisperGeometry,
) -> BunsenResult<()> {
match self.expected_for(model)? {
Some(expected) if expected != *found => {
Err(GeometryMismatch::new(model.id(), expected, *found).into())
}
_ => Ok(()),
}
}
}
pub const MULTILINGUAL_TEMPLATE_CLASS: &str = "openai/whisper/multilingual";
impl ConstructInit for WhisperConstructConfig {
type Construct = WhisperConstruct;
const KIT: &'static str = WHISPER_KIT;
const RESOLVES_PRESETS: bool = true;
fn applied_presets(
construct: &Self::Construct
) -> Option<crate::data::catalog::presets::PresetSelection> {
Some(construct.presets.clone())
}
fn for_resource_set(map: &ResourceSet) -> 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.format.as_deref()).context(&first.slot)?;
Ok(Self::new().with_reader(reader))
}
fn plan(
&self,
model: &BuildRef,
cache: &ResourceCache,
) -> BunsenResult<ResourceSet> {
let map = model.to_resource_set();
let checkpoint = map.family(CHECKPOINT);
if checkpoint.is_empty() {
map.try_get(CHECKPOINT)?;
}
let loaded = cache.load(&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).resource_set();
match map.get(VOCABULARY).cloned() {
None => map.fuse(rule, Fuse::Strict),
Some(given) if given.name == 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 try_init(
&self,
model: &BuildRef,
loaded: &LoadedResourceSet,
device: &Device,
load: crate::data::formats::LoadPolicy,
) -> BunsenResult<Arc<WhisperConstruct>> {
let (whisper, cfg) = self.reader.load(loaded, device)?;
let whisper = load.place(whisper, device, |_| true)?;
let layout = cfg
.token_layout
.policy_for_vocab(cfg.vocab_size)
.as_policy()
.with_context(|| model.id())?;
let family = if layout.ids().is_multilingual() {
crate::data::catalog::BuiltinCatalog::get()?
.template_presets(MULTILINGUAL_TEMPLATE_CLASS)?
} else {
ResolvedPresetSet::default()
};
let policy = resolve_ref_presets(&WhisperPolicy::default(), model, &family)?;
let mut bundle = WhisperConstruct::new(whisper, layout)
.with_policy(policy.config.clone(), policy.applied());
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 preset_tests {
use super::*;
use crate::kits::speech::asr::whisper::{
driver::{
WhisperStreamDriverConfig,
WhisperTask,
},
pretrained::default_whisper_factory,
};
fn policy(
spec: &str
) -> BunsenResult<crate::kits::support::model_config::ResolvedPresets<WhisperPolicy>> {
default_whisper_factory()?.resolve_presets(spec, &WhisperPolicy::default())
}
#[test]
fn test_the_task_axis() {
let small = policy("openai/small").unwrap();
assert_eq!(small.presets, ["transcribe"]);
assert_eq!(small.config, WhisperPolicy::default());
let translate = policy("openai/small[translate]{beam_size: 1}").unwrap();
assert_eq!(translate.presets, ["translate"]);
assert_eq!(translate.config.task, WhisperTask::Translate);
assert_eq!(translate.config.beam_size, 1);
for row in ["openai/tiny.en", "openai/large-v3-turbo"] {
assert!(policy(row).unwrap().presets.is_empty(), "{row}");
crate::errors::testing::ErrorMatcher::kind(crate::errors::BunsenErrorKind::Lookup)
.message_contains("no preset `translate`")
.assert_err(&policy(&format!("{row}[translate]")));
}
assert_eq!(policy("large").unwrap().presets, ["transcribe"]);
}
#[test]
fn test_with_policy() {
let policy = WhisperPolicy {
task: WhisperTask::Translate,
language: Some("de".to_string()),
timestamps: true,
beam_size: 3,
max_tokens: 100,
condition_on_previous_text: false,
fallback: false,
};
let base = WhisperStreamDriverConfig::new();
let c = base.clone().with_policy(&policy);
assert_eq!(c.task, WhisperTask::Translate);
assert_eq!(c.language.as_deref(), Some("de"));
assert!(c.timestamps);
assert_eq!(c.search.beam_size, 3);
assert_eq!(c.search.temperature, base.search.temperature);
assert_eq!(c.max_tokens, 100);
assert!(!c.condition_on_previous_text);
assert_eq!(
c.fallback,
crate::kits::speech::asr::whisper::WhisperFallbackConfig::new()
);
assert_eq!(
base.with_policy(&WhisperPolicy::default()).fallback,
crate::kits::speech::asr::whisper::WhisperFallbackConfig::upstream()
);
}
}
#[cfg(test)]
mod reader_tests {
use super::*;
use crate::{
data::builds::DeferredBuild,
errors::testing::ErrorMatcher,
};
#[test]
fn test_the_kind_chooses_the_reader() {
let mut map = ResourceSet::given("mine", CHECKPOINT, "/no/such/model.safetensors");
let hook = WhisperConstructConfig::for_resource_set(&map).unwrap();
assert!(matches!(hook.reader, WhisperReader::Pytorch(_)));
map.resources.get_mut(CHECKPOINT).unwrap().format = Some("pytorch fp16".to_string());
let hook = WhisperConstructConfig::for_resource_set(&map).unwrap();
assert!(matches!(hook.reader, WhisperReader::Pytorch(_)));
map.resources.get_mut(CHECKPOINT).unwrap().format = Some(SAFETENSORS.to_string());
{
let hook = WhisperConstructConfig::for_resource_set(&map).unwrap();
assert!(matches!(hook.reader, WhisperReader::Safetensors(_)));
assert!(
DeferredBuild::<WhisperConstructConfig>::from_resource_set(map.clone()).is_ok()
);
}
let mut sharded = ResourceSet::new("sharded");
for key in ["checkpoint.00001", "checkpoint.00002", "checkpoint.index"] {
let mut r = crate::data::resources::Resource::given(key, format!("/no/such/{key}"));
r.format = Some(if key.ends_with("index") {
crate::data::resources::SAFETENSORS_INDEX.to_string()
} else {
SAFETENSORS.to_string()
});
sharded.insert(r);
}
assert!(matches!(
WhisperConstructConfig::for_resource_set(&sharded)
.unwrap()
.reader,
WhisperReader::Safetensors(_)
));
map.resources.get_mut(CHECKPOINT).unwrap().format = Some("burnpack".to_string());
ErrorMatcher::kind(BunsenErrorKind::Unsupported)
.frame_contains("checkpoint")
.display_contains("checkpoint: no reader for a \"burnpack\"")
.assert_err(&WhisperConstructConfig::for_resource_set(&map));
ErrorMatcher::kind(BunsenErrorKind::Unsupported).assert_err(&DeferredBuild::<
WhisperConstructConfig,
>::from_resource_set(
map
));
ErrorMatcher::kind(BunsenErrorKind::Lookup).assert_err(
&WhisperConstructConfig::for_resource_set(&ResourceSet::new("empty")),
);
}
}