use alloc::{
string::String,
vec::Vec,
};
use burn::{
Tensor,
config::Config,
module::Module,
tensor::Device,
};
use crate::{
blocks::{
activation::BunsenActivationConfig,
conv::{
ConvBlock1d,
ConvBlock1dConfig,
ConvBlock1dMeta,
},
norm::BunsenNormalizationConfig,
},
burner::module::{
Indices,
ModuleInit,
Rekey,
},
errors::{
BunsenError,
BunsenResult,
ConstraintError,
ResultContext,
Rule,
},
};
pub trait ConvSeq1dMeta {
fn block_metas(&self) -> Vec<&dyn ConvBlock1dMeta>;
fn len(&self) -> usize {
self.block_metas().len()
}
fn is_empty(&self) -> bool {
self.block_metas().is_empty()
}
fn in_channels(&self) -> usize {
self.block_metas()
.first()
.expect("ConvSeq1d must have at least one block")
.in_channels()
}
fn out_channels(&self) -> usize {
self.block_metas()
.last()
.expect("ConvSeq1d must have at least one block")
.out_channels()
}
fn stride(&self) -> usize {
self.block_metas()
.iter()
.map(|meta| meta.stride()[0])
.product()
}
fn validate(&self) -> BunsenResult<()> {
let metas = self.block_metas();
if metas.is_empty() {
return Err(ConstraintError::zero_or_empty("ConvSeq1d", "blocks").into());
}
for (idx, pair) in metas.windows(2).enumerate() {
let prev = pair[0];
let next = pair[1];
if prev.out_channels() != next.in_channels() {
return Err(ConstraintError::new(
"ConvSeq1d",
"blocks",
Rule::Chain {
index: idx + 1,
out: format!("{} channels", prev.out_channels()),
r#in: format!("{} channels", next.in_channels()),
},
)
.into());
}
}
Ok(())
}
fn try_output_length(
&self,
in_length: usize,
) -> BunsenResult<usize> {
let mut length = in_length;
for (idx, meta) in self.block_metas().iter().enumerate() {
length = meta
.try_output_length(length)
.with_context(|| format!("ConvSeq1d block {idx}"))?;
}
Ok(length)
}
fn try_output_shape(
&self,
input_shape: [usize; 3],
) -> BunsenResult<[usize; 3]> {
let [batch_size, in_channels, in_length] = input_shape;
if in_channels != self.in_channels() {
return Err(BunsenError::illegal(format!(
"ConvSeq1d expected in_channels ({}), got ({in_channels})",
self.in_channels(),
)));
}
let out_length = self.try_output_length(in_length)?;
Ok([batch_size, self.out_channels(), out_length])
}
}
#[derive(Config, Debug)]
pub struct ConvSeq1dConfig {
pub blocks: Vec<ConvBlock1dConfig>,
}
impl ConvSeq1dMeta for ConvSeq1dConfig {
fn block_metas(&self) -> Vec<&dyn ConvBlock1dMeta> {
self.blocks
.iter()
.map(|config| config as &dyn ConvBlock1dMeta)
.collect()
}
}
impl ConvSeq1dConfig {
pub fn with_act<A: Into<Option<BunsenActivationConfig>>>(
self,
act: A,
) -> Self {
let act = act.into();
Self {
blocks: self
.blocks
.into_iter()
.map(|block| block.with_act(act.clone()))
.collect(),
}
}
pub fn with_norm<N: Into<Option<BunsenNormalizationConfig>>>(
self,
norm: N,
) -> Self {
let norm = norm.into();
Self {
blocks: self
.blocks
.into_iter()
.map(|block| block.with_norm(norm.clone()))
.collect(),
}
}
}
impl ModuleInit<ConvSeq1d> for ConvSeq1dConfig {
fn try_init(
&self,
device: &Device,
) -> BunsenResult<ConvSeq1d> {
let blocks = self
.blocks
.iter()
.map(|config| config.try_init(device))
.collect::<BunsenResult<Vec<_>>>()?;
ConvSeq1d::try_new(blocks)
}
}
#[derive(Module, Debug)]
pub struct ConvSeq1d {
pub blocks: Vec<ConvBlock1d>,
}
impl ConvSeq1dMeta for ConvSeq1d {
fn block_metas(&self) -> Vec<&dyn ConvBlock1dMeta> {
self.blocks
.iter()
.map(|block| block as &dyn ConvBlock1dMeta)
.collect()
}
}
impl ConvSeq1d {
pub fn try_new(blocks: Vec<ConvBlock1d>) -> BunsenResult<Self> {
let seq = Self { blocks };
seq.validate()?;
Ok(seq)
}
pub fn block_conv_path(
seq_path: &str,
index: usize,
) -> String {
format!("{seq_path}.blocks.{index}.conv")
}
pub fn one_based_rekey() -> Rekey {
Rekey::pass_rest().mount_indexed_with(
"",
"blocks.",
Indices::From(1),
Rekey::keep_rest().rename("", "conv."),
)
}
pub fn forward(
&self,
input: Tensor<3>,
) -> Tensor<3> {
let mut output = input;
for block in &self.blocks {
output = block.forward(output);
}
output
}
}
#[cfg(test)]
mod tests {
use burn::{
nn::{
PaddingConfig1d,
conv::Conv1dConfig,
},
tensor::Distribution,
};
use super::*;
use crate::{
errors::{
BunsenErrorKind,
testing::{
ErrorMatcher,
predicate,
},
},
support::testing::cpu_device,
};
fn block_config(
in_channels: usize,
out_channels: usize,
stride: usize,
) -> ConvBlock1dConfig {
ConvBlock1dConfig::new(
Conv1dConfig::new(in_channels, out_channels, 3)
.with_stride(stride)
.with_padding(PaddingConfig1d::Explicit(1, 1))
.with_bias(false),
)
.with_norm(None)
.with_act(Some(BunsenActivationConfig::Relu))
}
fn block(
in_channels: usize,
out_channels: usize,
stride: usize,
) -> ConvBlock1d {
block_config(in_channels, out_channels, stride).init(&cpu_device())
}
#[test]
fn test_validate_empty() {
let empty = ErrorMatcher::kind(BunsenErrorKind::Illegal)
.cause(predicate("empty blocks", |e: &ConstraintError| {
e.rule == Rule::ZeroOrEmpty
}));
empty.assert_err(&ConvSeq1d::try_new(vec![]));
empty.assert_err(&ConvSeq1dConfig::new(vec![]).validate());
}
#[test]
fn test_validate_channel_mismatch() {
let blocks = vec![block(2, 4, 1), block(8, 16, 1)];
let chain = ErrorMatcher::kind(BunsenErrorKind::Illegal).cause(predicate(
"a chain break at block 1",
|e: &ConstraintError| matches!(e.rule, Rule::Chain { index: 1, .. }),
));
chain.assert_err(&ConvSeq1d::try_new(blocks));
let result: BunsenResult<ConvSeq1d> =
ConvSeq1dConfig::new(vec![block_config(2, 4, 1), block_config(8, 16, 1)])
.try_init(&cpu_device());
chain.assert_err(&result);
}
#[test]
fn test_meta() {
let seq = ConvSeq1d::try_new(vec![block(2, 4, 2), block(4, 8, 2), block(8, 8, 1)]).unwrap();
assert_eq!(seq.len(), 3);
assert!(!seq.is_empty());
assert_eq!(seq.in_channels(), 2);
assert_eq!(seq.out_channels(), 8);
assert_eq!(seq.stride(), 4);
}
#[test]
fn test_config_meta_matches_module() {
let config = ConvSeq1dConfig::new(vec![block_config(2, 4, 2), block_config(4, 8, 2)]);
assert_eq!(config.len(), 2);
assert_eq!(config.in_channels(), 2);
assert_eq!(config.out_channels(), 8);
assert_eq!(config.stride(), 4);
assert_eq!(config.try_output_shape([1, 2, 16]).unwrap(), [1, 8, 4]);
let seq: ConvSeq1d = config.init(&cpu_device());
assert_eq!(seq.in_channels(), config.in_channels());
assert_eq!(seq.out_channels(), config.out_channels());
assert_eq!(seq.stride(), config.stride());
assert_eq!(
seq.try_output_shape([1, 2, 16]).unwrap(),
config.try_output_shape([1, 2, 16]).unwrap()
);
}
#[test]
fn test_output_length() {
let seq = ConvSeq1d::try_new(vec![block(2, 4, 2), block(4, 8, 2)]).unwrap();
assert_eq!(seq.try_output_length(16).unwrap(), 4);
assert_eq!(seq.try_output_length(12).unwrap(), 3);
assert_eq!(seq.try_output_length(6).unwrap(), 2);
}
#[test]
fn test_output_length_dilated() {
let device = cpu_device().autodiff();
let dilated = ConvBlock1dConfig::new(
Conv1dConfig::new(2, 4, 3)
.with_stride(1)
.with_dilation(2)
.with_padding(PaddingConfig1d::Valid)
.with_bias(false),
)
.with_norm(None)
.with_act(None)
.init(&device);
let seq = ConvSeq1d::try_new(vec![dilated]).unwrap();
assert_eq!(seq.try_output_length(10).unwrap(), 6);
assert_eq!(seq.try_output_shape([1, 2, 10]).unwrap(), [1, 4, 6]);
let input = Tensor::<3>::random([1, 2, 10], Distribution::Default, &device);
assert_eq!(seq.forward(input).dims(), [1, 4, 6]);
}
#[test]
fn test_output_shape_matches_forward() {
let device = cpu_device().autodiff();
let seq = ConvSeq1d::try_new(vec![block(2, 4, 2), block(4, 8, 2)]).unwrap();
let batch_size = 3;
let in_length = 16;
let input = Tensor::<3>::random([batch_size, 2, in_length], Distribution::Default, &device);
let predicted = seq.try_output_shape([batch_size, 2, in_length]).unwrap();
let actual = seq.forward(input).dims();
assert_eq!(predicted, [batch_size, 8, 4]);
assert_eq!(predicted, actual);
}
#[test]
fn test_output_shape_channel_mismatch() {
let seq = ConvSeq1d::try_new(vec![block(2, 4, 1)]).unwrap();
assert!(seq.try_output_shape([1, 3, 8]).is_err());
}
#[test]
fn test_forward_matches_sequential() {
let device = cpu_device().autodiff();
let blocks = vec![block(2, 4, 2), block(4, 8, 1)];
let seq = ConvSeq1d::try_new(blocks).unwrap();
let input = Tensor::<3>::random([2, 2, 8], Distribution::Default, &device);
let output = seq.forward(input.clone());
let expected = {
let mut x = input;
for b in &seq.blocks {
x = b.forward(x);
}
x
};
output.to_data().assert_eq(&expected.to_data(), true);
}
}