use std::sync::Arc;
use burn::prelude::Backend;
use super::{
LoadedResources,
PretrainedCache,
PretrainedRef,
ResourceMap,
};
use crate::errors::BunsenResult;
pub trait Construct: Sized {
const KIT: &'static str;
type Built<B: Backend>;
fn for_map(map: &ResourceMap) -> BunsenResult<Self>;
fn plan(
&self,
model: &PretrainedRef,
_cache: &PretrainedCache,
) -> BunsenResult<ResourceMap> {
Ok(model.to_map())
}
fn construct<B: Backend>(
&self,
model: &PretrainedRef,
loaded: &LoadedResources,
device: &B::Device,
) -> BunsenResult<Arc<Self::Built<B>>>;
}
#[derive(Debug)]
pub struct Loaded<T> {
pub name: String,
pub handle: Arc<T>,
pub resources: LoadedResources,
}
impl<T> Clone for Loaded<T> {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
handle: Arc::clone(&self.handle),
resources: self.resources.clone(),
}
}
}
#[cfg(test)]
mod tests {
use std::{
fs,
path::PathBuf,
};
use super::*;
use crate::{
data::{
cache::BunsenDiskCacheOptions,
pretrained::{
Deferred,
Fuse,
PretrainedCacheOptions,
Provenance,
},
},
errors::BunsenError,
support::testing::{
CpuBackend,
default_device,
},
};
struct Paths {
vocabulary: Option<PathBuf>,
}
impl Construct for Paths {
type Built<B: Backend> = Vec<PathBuf>;
const KIT: &'static str = "paths";
fn for_map(_map: &ResourceMap) -> BunsenResult<Self> {
Ok(Paths { vocabulary: None })
}
fn plan(
&self,
model: &PretrainedRef,
_cache: &PretrainedCache,
) -> BunsenResult<ResourceMap> {
let map = model.to_map();
match &self.vocabulary {
Some(path) if map.get("vocabulary").is_none() => map.fuse(
ResourceMap::given("derived", "vocabulary", path),
Fuse::Strict,
),
_ => Ok(map),
}
}
fn construct<B: Backend>(
&self,
_model: &PretrainedRef,
loaded: &LoadedResources,
_device: &B::Device,
) -> BunsenResult<Arc<Vec<PathBuf>>> {
loaded.expect("checkpoint")?;
Ok(Arc::new(
loaded.iter().map(|(_, r)| r.path.clone()).collect(),
))
}
}
struct NeedsConfig;
impl Construct for NeedsConfig {
type Built<B: Backend> = ();
const KIT: &'static str = "paths";
fn for_map(_map: &ResourceMap) -> BunsenResult<Self> {
Ok(NeedsConfig)
}
fn construct<B: Backend>(
&self,
_model: &PretrainedRef,
loaded: &LoadedResources,
_device: &B::Device,
) -> BunsenResult<Arc<()>> {
loaded.expect("config")?;
Ok(Arc::new(()))
}
}
fn cache_in(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()
}
#[test]
fn test_load_plans_loads_and_constructs() {
let dir = tempfile::tempdir().unwrap();
let checkpoint = dir.path().join("tiny.pt");
let vocabulary = dir.path().join("gpt2.tiktoken");
fs::write(&checkpoint, b"x").unwrap();
fs::write(&vocabulary, b"y").unwrap();
let cache = cache_in(dir.path());
let hook = Paths {
vocabulary: Some(vocabulary.clone()),
};
let loaded = Deferred {
model: PretrainedRef::from(ResourceMap::given("mine", "checkpoint", &checkpoint)),
hook,
}
.load::<CpuBackend>(&cache, &default_device())
.unwrap();
assert_eq!(loaded.name, "mine");
assert_eq!(*loaded.handle, vec![checkpoint, vocabulary]);
assert_eq!(loaded.resources.keys(), ["checkpoint", "vocabulary"]);
for (_, part) in loaded.resources.iter() {
assert_eq!(part.provenance, Provenance::LocalDir);
}
assert!(!dir.path().join("cache").exists(), "nothing was written");
let again = loaded.clone();
assert_eq!(Arc::strong_count(&loaded.handle), 2);
assert!(Arc::ptr_eq(&loaded.handle, &again.handle));
}
#[test]
fn test_plan_leaves_a_complete_map_alone() {
let dir = tempfile::tempdir().unwrap();
let checkpoint = dir.path().join("tiny.pt");
let vocabulary = dir.path().join("mine.tiktoken");
fs::write(&checkpoint, b"x").unwrap();
fs::write(&vocabulary, b"y").unwrap();
let cache = cache_in(dir.path());
let model = PretrainedRef::from(
ResourceMap::given("mine", "checkpoint", &checkpoint)
.fuse(
ResourceMap::given("flags", "vocabulary", &vocabulary),
Fuse::Strict,
)
.unwrap(),
);
let hook = Paths {
vocabulary: Some(dir.path().join("never-read.tiktoken")),
};
let loaded = Deferred {
model: model.clone(),
hook,
}
.load::<CpuBackend>(&cache, &default_device())
.unwrap();
assert_eq!(*loaded.handle, vec![checkpoint.clone(), vocabulary.clone()]);
let no_rule = Deferred::<Paths>::new(model.clone()).unwrap();
assert!(no_rule.hook.vocabulary.is_none());
let loaded = no_rule
.load::<CpuBackend>(&cache, &default_device())
.unwrap();
assert_eq!(*loaded.handle, vec![checkpoint, vocabulary]);
assert_eq!(NeedsConfig.plan(&model, &cache).unwrap(), model.to_map());
}
#[test]
fn test_a_missing_part_is_named() {
let dir = tempfile::tempdir().unwrap();
let checkpoint = dir.path().join("tiny.pt");
fs::write(&checkpoint, b"x").unwrap();
let cache = cache_in(dir.path());
let err = Deferred::<NeedsConfig>::from_map(ResourceMap::given(
"mine",
"checkpoint",
&checkpoint,
))
.unwrap()
.load::<CpuBackend>(&cache, &default_device())
.unwrap_err();
assert!(
matches!(&err, BunsenError::ResourceNotFound(m) if m.contains("\"config\"") && m.contains("checkpoint")),
"{err}"
);
let err = Deferred::<Paths>::from_map(ResourceMap::given(
"mine",
"checkpoint",
dir.path().join("absent.pt"),
))
.unwrap()
.load::<CpuBackend>(&cache, &default_device())
.unwrap_err();
assert!(matches!(&err, BunsenError::ResourceNotFound(_)), "{err}");
}
}