use alloc::collections::BTreeMap;
use burn::tensor::Device;
use crate::{
data::{
builds::{
BuildRef,
ConstructInit,
Loaded,
},
resources::{
CacheStatus,
ResourceCache,
ResourceSet,
},
},
errors::{
BunsenResult,
ResultContext,
},
};
#[derive(Clone, Debug)]
pub struct DeferredBuild<H: ConstructInit> {
pub model: BuildRef,
pub config: H,
}
impl<H: ConstructInit> DeferredBuild<H> {
pub fn new(model: BuildRef) -> BunsenResult<Self> {
let config = H::for_resource_set(&model.to_resource_set())?;
Ok(Self { model, config })
}
pub fn from_resource_set(map: ResourceSet) -> BunsenResult<Self> {
Self::new(BuildRef::from(map))
}
pub fn id(&self) -> String {
self.model.id()
}
pub fn to_resource_set(&self) -> ResourceSet {
self.model.to_resource_set()
}
pub fn with_overlay(
self,
map: ResourceSet,
) -> BunsenResult<Self> {
Ok(Self {
model: self.model.with_overlay(map)?,
config: self.config,
})
}
pub fn status(
&self,
cache: &ResourceCache,
) -> BTreeMap<String, CacheStatus> {
cache.map_status(&self.to_resource_set())
}
pub fn plan(
&self,
cache: &ResourceCache,
) -> BunsenResult<ResourceSet> {
self.config
.plan(&self.model, cache)
.with_context(|| format!("planning {} {:?}", H::KIT, self.id()))
.as_policy()
}
pub fn load(
&self,
cache: &ResourceCache,
device: &Device,
policy: crate::data::formats::LoadPolicy,
) -> BunsenResult<Loaded<H::Construct>> {
let load = || -> BunsenResult<Loaded<H::Construct>> {
let planned = self.config.plan(&self.model, cache)?;
let resources = cache.load(&planned)?;
let handle = self
.config
.try_init(&self.model, &resources, device, policy)?;
let summary = self
.model
.summary()
.with_applied(H::applied_presets(&handle));
Ok(Loaded {
name: resources.map.name.clone(),
handle,
resources,
summary,
})
};
load()
.with_context(|| format!("loading {} {:?}", H::KIT, self.id()))
.as_policy()
}
}
#[cfg(test)]
mod tests {
use std::fs;
use super::*;
use crate::{
data::{
builds::testing::CheckpointPath,
cache::BunsenDiskCacheOptions,
catalog::{
CatalogName,
ResolvedBuild,
document::{
ResourceEntry,
ResourceSource,
},
},
resources::{
Provenance,
ResourceCacheOptions,
},
},
errors::{
BunsenErrorKind,
testing::ErrorMatcher,
},
support::testing::cpu_device,
};
fn offline_cache(dir: &std::path::Path) -> ResourceCache {
ResourceCache::new(
ResourceCacheOptions::default()
.with_disk(
BunsenDiskCacheOptions::default()
.with_cache_dir(Some(dir.join("cache")))
.without_transfer_observers(),
)
.with_offline(true),
)
.unwrap()
}
fn named() -> BuildRef {
let n = |name: &str| CatalogName::parse(name).unwrap();
let build = ResolvedBuild::new(n("a/small"), "small weights").with_resource(
"checkpoint",
n("a/small/checkpoint"),
ResourceEntry::new(
"small.pt",
"pytorch",
vec![ResourceSource::Url(
"https://a.example/small.pt".to_string(),
)],
),
);
BuildRef::named("catalog", build, Default::default())
}
#[test]
fn test_status_and_load() {
let dir = tempfile::tempdir().unwrap();
let cache = offline_cache(dir.path());
let file = dir.path().join("ckpt.pt");
fs::write(&file, b"x").unwrap();
let spec = file.to_str().unwrap();
let given = DeferredBuild::<CheckpointPath>::from_resource_set(ResourceSet::given(
spec,
"checkpoint",
&file,
))
.unwrap();
assert_eq!(given.id(), spec);
assert_eq!(given.status(&cache)["checkpoint"], CacheStatus::LocalDir);
assert_eq!(given.plan(&cache).unwrap(), given.to_resource_set());
let loaded = given
.load(
&cache,
&cpu_device(),
crate::data::formats::LoadPolicy::load_as_stored(),
)
.unwrap();
assert_eq!(*loaded.handle, file);
assert_eq!(loaded.name, spec);
assert_eq!(
loaded.resources.get("checkpoint").unwrap().provenance,
Provenance::LocalDir
);
assert!(format!("{given:?}").contains("CheckpointPath"));
let named = DeferredBuild::<CheckpointPath>::new(named()).unwrap();
assert_eq!(named.id(), "catalog:a/small");
assert_eq!(named.status(&cache)["checkpoint"], CacheStatus::Remote);
ErrorMatcher::kind(BunsenErrorKind::Policy)
.frame_contains("loading kit \"catalog:a/small\"")
.message_contains("the cache is offline")
.assert_err(&named.load(
&cache,
&cpu_device(),
crate::data::formats::LoadPolicy::load_as_stored(),
));
}
#[test]
fn test_with_overlay_keeps_the_hook() {
let dir = tempfile::tempdir().unwrap();
let cache = offline_cache(dir.path());
let other = dir.path().join("other.pt");
fs::write(&other, b"y").unwrap();
let model = DeferredBuild::<CheckpointPath>::new(named())
.unwrap()
.with_overlay(ResourceSet::given("mine", "checkpoint", &other))
.unwrap();
assert_eq!(model.id(), "catalog:a/small");
assert_eq!(
model.to_resource_set().get("checkpoint").unwrap().file,
"other.pt"
);
let loaded = model
.load(
&cache,
&cpu_device(),
crate::data::formats::LoadPolicy::load_as_stored(),
)
.unwrap();
assert_eq!(*loaded.handle, other);
assert_eq!(loaded.name, "catalog:a/small");
}
}