use alloc::{
format,
vec::Vec,
};
use burn::{
config::Config,
nn::BatchNormConfig,
prelude::{
Module,
Tensor,
},
tensor::Device,
};
use crate::{
blocks::{
activation::BunsenActivationConfig,
norm::BunsenNormalizationConfig,
},
burner::module::{
ModuleInit,
ToStructureConfig,
},
errors::{
BunsenError,
BunsenResult,
ConstraintError,
Rule,
WithOkOrPanic,
},
kits::images::resnet::blocks::{
BottleneckPolicyConfig,
ResidualBlock,
ResidualBlockContractConfig,
ResidualBlockMeta,
ResidualBlockStructureConfig,
},
ops::{
conv::stride_div_output_resolution,
drop::DropBlockOptions,
},
support::validators::expect_probability,
};
#[derive(Config, Debug)]
pub struct LayerBlockContractConfig {
pub num_blocks: usize,
pub in_planes: usize,
pub out_planes: usize,
#[config(default = 1)]
pub dilation: usize,
#[config(default = "None")]
pub first_dilation: Option<usize>,
#[config(default = "false")]
pub downsample_input: bool,
#[config(default = "None")]
pub bottleneck_policy: Option<BottleneckPolicyConfig>,
#[config(default = "BunsenNormalizationConfig::Batch(BatchNormConfig::new(0))")]
pub normalization: BunsenNormalizationConfig,
#[config(default = "BunsenActivationConfig::Relu")]
pub activation: BunsenActivationConfig,
}
impl LayerBlockContractConfig {
pub fn to_block_contracts(&self) -> Vec<ResidualBlockContractConfig> {
let mut first_dilation = self.first_dilation;
let mut blocks = Vec::with_capacity(self.num_blocks);
for b in 0..self.num_blocks {
let downsample_input = b == 0 && self.downsample_input;
let in_planes = if b == 0 {
self.in_planes
} else {
self.out_planes
};
blocks.push(
ResidualBlockContractConfig::new(in_planes, self.out_planes)
.with_downsample_input(downsample_input)
.with_first_dilation(first_dilation)
.with_dilation(self.dilation)
.with_bottleneck_policy(self.bottleneck_policy.clone())
.with_normalization(self.normalization.clone())
.with_activation(self.activation.clone()),
);
first_dilation = Some(self.dilation);
}
blocks
}
}
impl ToStructureConfig for LayerBlockContractConfig {
type Structure = LayerBlockStructureConfig;
fn try_to_structure(&self) -> BunsenResult<LayerBlockStructureConfig> {
Ok(LayerBlockStructureConfig {
blocks: self
.to_block_contracts()
.iter()
.map(|cfg| cfg.try_to_structure())
.collect::<BunsenResult<_>>()?,
})
}
}
pub trait LayerBlockMeta {
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn in_planes(&self) -> usize;
fn out_planes(&self) -> usize;
fn stride(&self) -> usize;
fn output_resolution(
&self,
input_resolution: [usize; 2],
) -> [usize; 2] {
stride_div_output_resolution(input_resolution, self.stride())
}
}
#[derive(Config, Debug)]
pub struct LayerBlockStructureConfig {
pub blocks: Vec<ResidualBlockStructureConfig>,
}
impl From<Vec<ResidualBlockStructureConfig>> for LayerBlockStructureConfig {
fn from(blocks: Vec<ResidualBlockStructureConfig>) -> Self {
Self { blocks }
}
}
impl LayerBlockMeta for LayerBlockStructureConfig {
fn len(&self) -> usize {
self.blocks.len()
}
fn in_planes(&self) -> usize {
self.blocks[0].in_planes()
}
fn out_planes(&self) -> usize {
self.blocks[self.blocks.len() - 1].out_planes()
}
fn stride(&self) -> usize {
self.blocks
.iter()
.fold(1, |acc, block| acc * block.stride())
}
}
impl LayerBlockStructureConfig {
pub fn try_validate(&self) -> BunsenResult<()> {
if self.is_empty() {
return Err(
ConstraintError::zero_or_empty("LayerBlockStructureConfig", "blocks").into(),
);
}
for idx in 1..self.blocks.len() {
let prev = &self.blocks[idx - 1];
let curr = &self.blocks[idx];
if prev.out_planes() != curr.in_planes() {
return Err(BunsenError::from(ConstraintError::new(
"LayerBlockStructureConfig",
"blocks",
Rule::Chain {
index: idx,
out: format!("{} planes", prev.out_planes()),
r#in: format!("{} planes", curr.in_planes()),
},
))
.with_details(format!("{self:#?}")));
}
}
Ok(())
}
pub fn expect_valid(&self) {
self.try_validate().ok_or_panic()
}
pub fn map_blocks<F>(
self,
f: &mut F,
) -> Self
where
F: FnMut(usize, ResidualBlockStructureConfig) -> ResidualBlockStructureConfig,
{
Self {
blocks: self
.blocks
.into_iter()
.enumerate()
.map(|(idx, block)| f(idx, block))
.collect(),
}
}
pub fn with_drop_block<O>(
self,
options: O,
) -> Self
where
O: Into<Option<DropBlockOptions>>,
{
let options = options.into();
self.map_blocks(&mut |_, block| block.with_drop_block(options.clone()))
}
}
impl ModuleInit<LayerBlock> for LayerBlockStructureConfig {
fn try_init(
&self,
device: &Device,
) -> BunsenResult<LayerBlock> {
self.try_validate()?;
Ok(LayerBlock {
blocks: self
.blocks
.iter()
.map(|block| block.try_init(device))
.collect::<BunsenResult<Vec<ResidualBlock>>>()?,
})
}
}
#[derive(Module, Debug)]
pub struct LayerBlock {
pub blocks: Vec<ResidualBlock>,
}
impl LayerBlockMeta for LayerBlock {
fn len(&self) -> usize {
self.blocks.len()
}
fn in_planes(&self) -> usize {
self.blocks[0].in_planes()
}
fn out_planes(&self) -> usize {
self.blocks[self.blocks.len() - 1].out_planes()
}
fn stride(&self) -> usize {
self.blocks
.iter()
.fold(1, |acc, block| acc * block.stride())
}
}
impl LayerBlock {
pub fn debug_print(&self) {
println!("## LayerBlock: len={}", self.len());
for (idx, block) in self.blocks.iter().enumerate() {
println!("### block[{}]:", idx);
block.debug_print();
println!();
}
}
pub fn forward(
&self,
input: Tensor<4>,
) -> Tensor<4> {
#[cfg(debug_assertions)]
use crate::contracts::*;
#[cfg(debug_assertions)]
let [batch, out_height, out_width] = unpack_shape_contract!(
[
"batch",
"in_planes",
"in_height" = "out_height" * "stride",
"in_width" = "out_width" * "stride"
],
&input.dims(),
&["batch", "out_height", "out_width"],
&[("in_planes", self.in_planes()), ("stride", self.stride())],
);
let mut x = input;
for block in self.blocks.iter() {
x = block.forward(x);
}
#[cfg(debug_assertions)]
assert_shape_contract_periodically!(
["batch", "out_planes", "out_height", "out_width"],
&x.dims(),
&[
("batch", batch),
("out_planes", self.out_planes()),
("out_height", out_height),
("out_width", out_width)
],
);
x
}
pub fn map_blocks<F>(
self,
f: &mut F,
) -> Self
where
F: FnMut(usize, ResidualBlock) -> ResidualBlock,
{
Self {
blocks: self
.blocks
.into_iter()
.enumerate()
.map(|(idx, block)| f(idx, block))
.collect(),
}
}
pub fn with_drop_path_prob(
self,
prob: f64,
) -> Self {
let prob = expect_probability(prob);
self.map_blocks(&mut |_, block| block.with_drop_path_prob(prob))
}
pub fn with_drop_block<O>(
self,
options: O,
) -> Self
where
O: Into<Option<DropBlockOptions>>,
{
let options = options.into();
self.map_blocks(&mut |_, block| block.with_drop_block(options.clone()))
}
}
#[cfg(test)]
mod tests {
use alloc::vec;
use burn::tensor::Tolerance;
use serial_test::serial;
use super::*;
use crate::{
contracts::assert_shape_contract,
kits::images::resnet::blocks::BasicBlockConfig,
support::testing::{
DeviceMemoryGuard,
performance_device,
},
};
#[test]
fn test_layer_block_config_build() {
let num_blocks = 2;
let in_planes = 16;
let planes = 32;
let config: LayerBlockStructureConfig =
LayerBlockContractConfig::new(num_blocks, in_planes, planes)
.with_downsample_input(true)
.to_structure();
config.expect_valid();
assert_eq!(config.len(), 2);
assert_eq!(config.in_planes(), in_planes);
assert_eq!(config.out_planes(), planes);
assert_eq!(config.stride(), 2);
assert_eq!(config.output_resolution([12, 24]), [6, 12]);
let block1 = &config.blocks[0];
assert_eq!(block1.in_planes(), in_planes);
assert_eq!(block1.out_planes(), planes);
assert_eq!(block1.stride(), 2);
assert_eq!(block1.output_resolution([12, 24]), [6, 12]);
let block2 = &config.blocks[1];
assert_eq!(block2.in_planes(), planes);
assert_eq!(block2.out_planes(), planes);
assert_eq!(block2.stride(), 1);
assert_eq!(block2.output_resolution([12, 24]), [12, 24]);
}
#[test]
#[serial]
pub fn test_layer_block() {
type F = f32;
let device = performance_device();
let _memory = DeviceMemoryGuard::new(&device);
let a_planes = 16;
let b_planes = 32;
let c_planes = 64;
let config = LayerBlockStructureConfig::from(vec![
BasicBlockConfig::new(a_planes, b_planes)
.with_stride(2)
.into(),
BasicBlockConfig::new(b_planes, c_planes)
.with_stride(1)
.into(),
BasicBlockConfig::new(c_planes, c_planes)
.with_stride(1)
.into(),
]);
config.expect_valid();
assert_eq!(config.in_planes(), a_planes);
assert_eq!(config.out_planes(), c_planes);
assert_eq!(config.stride(), 2);
assert_eq!(config.output_resolution([20, 16]), [10, 8]);
let block: LayerBlock = config.init(&device);
assert_eq!(block.in_planes(), a_planes);
assert_eq!(block.out_planes(), c_planes);
assert_eq!(block.stride(), 2);
assert_eq!(block.output_resolution([20, 16]), [10, 8]);
let batch_size = 2;
let input = Tensor::ones([batch_size, a_planes, 20, 16], &device);
let output = block.forward(input.clone());
assert_shape_contract!(
["batch", "out_planes", "out_height", "out_width"],
&output.dims(),
&[
("batch", batch_size),
("out_planes", c_planes),
("out_height", 10),
("out_width", 8)
],
);
let mut expected = input;
for block in block.blocks.iter() {
expected = block.forward(expected);
}
output
.to_data_as::<F>()
.assert_approx_eq::<F>(&expected.to_data_as::<F>(), Tolerance::default());
}
fn assert_meta_agrees(
a: &impl LayerBlockMeta,
b: &impl LayerBlockMeta,
) {
assert_eq!(a.len(), b.len());
assert_eq!(a.is_empty(), b.is_empty());
assert_eq!(a.in_planes(), b.in_planes());
assert_eq!(a.out_planes(), b.out_planes());
assert_eq!(a.stride(), b.stride());
assert_eq!(a.output_resolution([8, 12]), b.output_resolution([8, 12]));
}
#[test]
#[serial]
fn test_policy_pathways_agree() {
let device = performance_device();
let _memory = DeviceMemoryGuard::new(&device);
let policy = LayerBlockContractConfig::new(2, 8, 32)
.with_downsample_input(true)
.with_bottleneck_policy(Some(BottleneckPolicyConfig::default()));
let structure = policy.to_structure();
assert_eq!(structure.len(), 2);
assert_eq!(structure.in_planes(), 8);
assert_eq!(structure.out_planes(), 32);
assert_eq!(structure.stride(), 2);
let lowered: LayerBlock = structure.init(&device);
let direct: LayerBlock = policy.init(&device);
assert_meta_agrees(&direct, &lowered);
assert_meta_agrees(&direct, &structure);
}
#[test]
fn test_structure_with_drop_block_reaches_every_block() {
let options = DropBlockOptions::default().with_drop_prob(0.1);
let structure = LayerBlockContractConfig::new(3, 8, 16)
.with_downsample_input(true)
.to_structure()
.with_drop_block(options.clone());
let drop_blocks: Vec<Option<DropBlockOptions>> = structure
.blocks
.iter()
.map(|block| match block {
ResidualBlockStructureConfig::Basic(config) => config.drop_block.clone(),
ResidualBlockStructureConfig::Bottleneck(config) => config.drop_block.clone(),
})
.collect();
assert_eq!(drop_blocks, vec![Some(options); 3]);
}
}