use alloc::sync::Arc;
use burn::tensor::Device;
use crate::{
burner::module::ModuleInit,
data::{
builds::{
BuildRef,
ConstructInit,
GEOMETRY,
},
resources::{
LoadedResourceSet,
ResourceSet,
},
},
errors::{
BunsenError,
BunsenResult,
ConstraintError,
Rule,
},
kits::images::resnet::{
ResNet,
ResNetContractConfig,
pretrained::{
CHECKPOINT,
RESNET_KIT,
},
},
};
#[derive(Clone, Debug, Default)]
pub struct ResNetConstructConfig {
pub config: Option<ResNetContractConfig>,
}
impl ResNetConstructConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_config(
mut self,
config: ResNetContractConfig,
) -> Self {
self.config = Some(config);
self
}
pub fn config_for(
&self,
model: &BuildRef,
) -> BunsenResult<ResNetContractConfig> {
if let Some(config) = &self.config {
return Ok(config.clone());
}
if let Some(config) = model.stated(GEOMETRY)? {
return Ok(config);
}
Err(BunsenError::from(ConstraintError::new(
"ResNetConstructConfig",
"config",
Rule::Missing,
))
.context(format!(
"{}: states no geometry to build from; a given checkpoint needs `with_config`",
model.id()
)))
}
}
impl ConstructInit for ResNetConstructConfig {
type Construct = ResNet;
const KIT: &'static str = RESNET_KIT;
fn for_resource_set(_map: &ResourceSet) -> BunsenResult<Self> {
Ok(Self::new())
}
fn try_init(
&self,
model: &BuildRef,
loaded: &LoadedResourceSet,
device: &Device,
load: crate::data::formats::LoadPolicy,
) -> BunsenResult<Arc<ResNet>> {
let config = self.config_for(model)?;
let resnet: ResNet = config.try_init(device)?;
let resnet = resnet.load_pytorch_weights(loaded.expect(CHECKPOINT)?)?;
Ok(Arc::new(load.place(resnet, device, |_| true)?))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
data::pretrained::Prefabs,
kits::images::resnet::{
ResNetContractConfig,
pretrained::default_resnet_factory,
},
};
#[test]
fn test_the_hook_takes_the_prefab_or_an_explicit_config() {
use std::fs;
use crate::{
data::{
builds::{
BuildRef,
ConstructInit,
DeferredBuild,
},
cache::BunsenDiskCacheOptions,
resources::{
ResourceCache,
ResourceCacheOptions,
ResourceSet,
},
},
errors::{
BunsenErrorKind,
testing::{
ErrorMatcher,
predicate,
value,
},
},
support::testing::cpu_device,
};
let dir = tempfile::tempdir().unwrap();
let cache = ResourceCache::new(
ResourceCacheOptions::default()
.with_disk(
crate::data::cache::BunsenDiskCacheOptions::default()
.with_cache_dir(Some(dir.path().join("cache")))
.without_transfer_observers(),
)
.with_offline(true),
)
.unwrap();
let factory = default_resnet_factory().unwrap();
let named = factory.resolve("resnet50", &cache).unwrap();
let hook = named.config.clone();
let named = named.model;
let resnet50 = ResNetContractConfig::expect_prefab("resnet50");
assert_eq!(
format!("{:?}", hook.config_for(&named).unwrap()),
format!("{resnet50:?}")
);
let resnet18 = ResNetContractConfig::expect_prefab("resnet18");
let explicit = hook.clone().with_config(resnet18.clone());
assert_eq!(
format!("{:?}", explicit.config_for(&named).unwrap()),
format!("{resnet18:?}"),
"an explicit config wins over the prefab"
);
let file = dir.path().join("ckpt.pth");
fs::write(&file, b"not a state dict").unwrap();
assert!(
factory.resolve(file.to_str().unwrap(), &cache).is_err(),
"a path is not a name the factory knows"
);
let given = BuildRef::from(ResourceSet::given("mine", CHECKPOINT, &file));
let missing_config = |kinds: &[BunsenErrorKind]| {
ErrorMatcher::new()
.with_kind_matching(value::one_of(kinds.to_vec()))
.cause(predicate("a missing config", |e: &ConstraintError| {
e.field == "config" && e.rule == Rule::Missing
}))
.frame_contains("with_config")
};
missing_config(&[BunsenErrorKind::Illegal]).assert_err(&hook.config_for(&given));
assert_eq!(
format!("{:?}", explicit.config_for(&given).unwrap()),
format!("{resnet18:?}")
);
let cache = ResourceCache::new(
ResourceCacheOptions::default()
.with_disk(
BunsenDiskCacheOptions::default()
.with_cache_dir(Some(dir.path().join("cache")))
.without_transfer_observers(),
)
.with_offline(true),
)
.unwrap();
let err = DeferredBuild::<ResNetConstructConfig>::from_resource_set(ResourceSet::given(
"mine", CHECKPOINT, &file,
))
.unwrap()
.load(
&cache,
&cpu_device(),
crate::data::formats::LoadPolicy::load_as_stored(),
);
missing_config(&[BunsenErrorKind::Policy]).assert_err(&err);
assert_eq!(<ResNetConstructConfig as ConstructInit>::KIT, RESNET_KIT);
}
}