use std::collections::BTreeMap;
use burn::prelude::Backend;
use super::{
CacheStatus,
Construct,
Loaded,
PretrainedCache,
PretrainedRef,
ResourceMap,
};
use crate::errors::{
BunsenResult,
ResultContext,
};
#[derive(Clone, Debug)]
pub struct Deferred<H: Construct> {
pub model: PretrainedRef,
pub hook: H,
}
impl<H: Construct> Deferred<H> {
pub fn new(model: PretrainedRef) -> BunsenResult<Self> {
let hook = H::for_map(&model.to_map())?;
Ok(Self { model, hook })
}
pub fn from_map(map: ResourceMap) -> BunsenResult<Self> {
Self::new(PretrainedRef::from(map))
}
pub fn id(&self) -> String {
self.model.id()
}
pub fn to_map(&self) -> ResourceMap {
self.model.to_map()
}
pub fn with_overlay(
self,
map: ResourceMap,
) -> BunsenResult<Self> {
Ok(Self {
model: self.model.with_overlay(map)?,
hook: self.hook,
})
}
pub fn status(
&self,
cache: &PretrainedCache,
) -> BTreeMap<String, CacheStatus> {
cache.map_status(H::KIT, &self.to_map())
}
pub fn plan(
&self,
cache: &PretrainedCache,
) -> BunsenResult<ResourceMap> {
self.hook
.plan(&self.model, cache)
.with_context(|| format!("planning {} {:?}", H::KIT, self.id()))
.as_policy()
}
pub fn load<B: Backend>(
&self,
cache: &PretrainedCache,
device: &B::Device,
) -> BunsenResult<Loaded<H::Built<B>>> {
let load = || -> BunsenResult<Loaded<H::Built<B>>> {
let planned = self.hook.plan(&self.model, cache)?;
let resources = cache.load(H::KIT, &planned)?;
let handle = self.hook.construct::<B>(&self.model, &resources, device)?;
Ok(Loaded {
name: resources.map.name.clone(),
handle,
resources,
})
};
load()
.with_context(|| format!("loading {} {:?}", H::KIT, self.id()))
.as_policy()
}
}
#[cfg(test)]
mod tests {
use std::fs;
use super::*;
use crate::{
data::{
cache::BunsenDiskCacheOptions,
pretrained::{
Pretrained,
PretrainedCacheOptions,
Provenance,
Resource,
Source,
testing::CheckpointPath,
},
},
errors::{
BunsenErrorKind,
testing::ErrorMatcher,
},
support::testing::{
CpuBackend,
default_device,
},
};
fn offline_cache(dir: &std::path::Path) -> PretrainedCache {
PretrainedCache::new(
PretrainedCacheOptions::default()
.with_disk(
BunsenDiskCacheOptions::default()
.with_cache_dir(Some(dir.join("cache")))
.without_transfer_observers(),
)
.with_offline(true),
)
.unwrap()
}
fn named() -> PretrainedRef {
PretrainedRef::Named {
provider: "well-known".to_string(),
pretrained: Pretrained {
name: "a/small".to_string(),
aliases: Vec::new(),
description: "small weights".to_string(),
license: None,
origin: None,
prefab: Some("small".to_string()),
resources: ResourceMap::new("small").with_resource(Resource {
key: "checkpoint".to_string(),
file: "small.pt".to_string(),
sha256: None,
kind: Some("pytorch".to_string()),
namespace: "a".to_string(),
sources: vec![Source::Url("https://a.example/small.pt".to_string())],
}),
},
}
}
#[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 =
Deferred::<CheckpointPath>::from_map(ResourceMap::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_map());
let loaded = given.load::<CpuBackend>(&cache, &default_device()).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 = Deferred::<CheckpointPath>::new(named()).unwrap();
assert_eq!(named.id(), "well-known:a/small");
assert_eq!(named.status(&cache)["checkpoint"], CacheStatus::Remote);
ErrorMatcher::kind(BunsenErrorKind::Policy)
.frame_contains("loading kit \"well-known:a/small\"")
.message_contains("the cache is offline")
.assert_err(&named.load::<CpuBackend>(&cache, &default_device()));
}
#[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 = Deferred::<CheckpointPath>::new(named())
.unwrap()
.with_overlay(ResourceMap::given("mine", "checkpoint", &other))
.unwrap();
assert_eq!(model.id(), "well-known:a/small");
assert_eq!(model.to_map().get("checkpoint").unwrap().file, "other.pt");
let loaded = model.load::<CpuBackend>(&cache, &default_device()).unwrap();
assert_eq!(*loaded.handle, other);
assert_eq!(loaded.name, "well-known:a/small");
}
}