use std::sync::Arc;
use burn::prelude::Backend;
use crate::{
burner::module::ModuleInit,
data::pretrained::{
Construct,
LoadedResources,
PretrainedRef,
ResourceMap,
},
errors::{
BunsenError,
BunsenResult,
ConstraintError,
Rule,
},
kits::images::resnet::{
ResNet,
ResNetContractConfig,
pretrained::{
CHECKPOINT,
RESNET_KIT,
RESNET_PREFABS,
},
},
};
#[derive(Clone, Debug, Default)]
pub struct ResNetConstruct {
pub config: Option<ResNetContractConfig>,
}
impl ResNetConstruct {
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: &PretrainedRef,
) -> BunsenResult<ResNetContractConfig> {
if let Some(config) = &self.config {
return Ok(config.clone());
}
model
.prefab(&RESNET_PREFABS)
.map(|prefab| prefab.to_config())
.ok_or_else(|| {
BunsenError::from(ConstraintError::new(
"ResNetConstruct",
"config",
Rule::Missing,
))
.context(format!(
"{}: names no prefab to build from; a given checkpoint needs \
`with_config`",
model.id()
))
})
}
}
impl Construct for ResNetConstruct {
type Built<B: Backend> = ResNet<B>;
const KIT: &'static str = RESNET_KIT;
fn for_map(_map: &ResourceMap) -> BunsenResult<Self> {
Ok(Self::new())
}
fn construct<B: Backend>(
&self,
model: &PretrainedRef,
loaded: &LoadedResources,
device: &B::Device,
) -> BunsenResult<Arc<ResNet<B>>> {
let config = self.config_for(model)?;
let resnet: ResNet<B> = config.try_init(device)?;
Ok(Arc::new(
resnet.load_pytorch_weights(loaded.expect(CHECKPOINT)?)?,
))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::kits::images::resnet::pretrained::default_resnet_factory;
#[test]
fn test_the_hook_takes_the_prefab_or_an_explicit_config() {
use std::fs;
use crate::{
data::{
cache::BunsenDiskCacheOptions,
pretrained::{
Construct,
Deferred,
PretrainedCache,
PretrainedCacheOptions,
PretrainedRef,
ResourceMap,
},
},
errors::{
BunsenErrorKind,
testing::{
ErrorMatcher,
predicate,
value,
},
},
support::testing::{
CpuBackend,
default_device,
},
};
let dir = tempfile::tempdir().unwrap();
let cache = PretrainedCache::new(
PretrainedCacheOptions::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.hook.clone();
let named = named.model;
let resnet50 = RESNET_PREFABS.expect_lookup_prefab("resnet50").to_config();
assert_eq!(
format!("{:?}", hook.config_for(&named).unwrap()),
format!("{resnet50:?}")
);
let resnet18 = RESNET_PREFABS.expect_lookup_prefab("resnet18").to_config();
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 = PretrainedRef::from(ResourceMap::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 = PretrainedCache::new(
PretrainedCacheOptions::default()
.with_disk(
BunsenDiskCacheOptions::default()
.with_cache_dir(Some(dir.path().join("cache")))
.without_transfer_observers(),
)
.with_offline(true),
)
.unwrap();
let err =
Deferred::<ResNetConstruct>::from_map(ResourceMap::given("mine", CHECKPOINT, &file))
.unwrap()
.load::<CpuBackend>(&cache, &default_device());
missing_config(&[BunsenErrorKind::Policy]).assert_err(&err);
assert_eq!(<ResNetConstruct as Construct>::KIT, RESNET_KIT);
}
}