Skip to content

Update default strides in AvgPool1dConfig, AvgPool2dConfig, MaxPool1dConfig, and MaxPool2dConfig to match kernel sizes #3338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/burn-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ burn-autodiff = { path = "../burn-autodiff", version = "0.18.0" }
burn-dataset = { path = "../burn-dataset", version = "0.18.0", features = [
"fake",
] }
rstest = { workspace = true }

[package.metadata.docs.rs]
features = ["doc"]
Expand Down
20 changes: 17 additions & 3 deletions crates/burn-core/src/nn/pool/avg_pool1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct AvgPool1dConfig {
/// The size of the kernel.
pub kernel_size: usize,
/// The stride.
#[config(default = "1")]
#[config(default = "kernel_size")]
pub stride: usize,
/// The padding configuration.
///
Expand Down Expand Up @@ -114,6 +114,7 @@ impl AvgPool1d {
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;

#[test]
#[should_panic = "Same padding with an even kernel size is not supported"]
Expand All @@ -128,8 +129,21 @@ mod tests {
let layer = config.init();

assert_eq!(
alloc::format!("{}", layer),
"AvgPool1d {kernel_size: 3, stride: 1, padding: Valid, count_include_pad: true}"
alloc::format!("{layer}"),
"AvgPool1d {kernel_size: 3, stride: 3, padding: Valid, count_include_pad: true}"
);
}

#[rstest]
#[case(1)]
#[case(2)]
fn default_strides_match_kernel_size(#[case] kernel_size: usize) {
let config = AvgPool1dConfig::new(kernel_size);

assert_eq!(
config.stride, kernel_size,
"Expected stride ({:?}) to match kernel size ({:?}) in default AvgPool1dConfig::new constructor",
config.stride, config.kernel_size
);
}
}
20 changes: 17 additions & 3 deletions crates/burn-core/src/nn/pool/avg_pool2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct AvgPool2dConfig {
/// The size of the kernel.
pub kernel_size: [usize; 2],
/// The strides.
#[config(default = "[1, 1]")]
#[config(default = "kernel_size")]
pub strides: [usize; 2],
/// The padding configuration.
///
Expand Down Expand Up @@ -114,6 +114,7 @@ impl AvgPool2d {
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;

#[test]
#[should_panic = "Same padding with an even kernel size is not supported"]
Expand All @@ -129,8 +130,21 @@ mod tests {
let layer = config.init();

assert_eq!(
alloc::format!("{}", layer),
"AvgPool2d {kernel_size: [3, 3], stride: [1, 1], padding: Valid, count_include_pad: true}"
alloc::format!("{layer}"),
"AvgPool2d {kernel_size: [3, 3], stride: [3, 3], padding: Valid, count_include_pad: true}"
);
}

#[rstest]
#[case([2, 2])]
#[case([1, 2])]
fn default_strides_match_kernel_size(#[case] kernel_size: [usize; 2]) {
let config = AvgPool2dConfig::new(kernel_size);

assert_eq!(
config.strides, kernel_size,
"Expected strides ({:?}) to match kernel size ({:?}) in default AvgPool2dConfig::new constructor",
config.strides, config.kernel_size
);
}
}
20 changes: 17 additions & 3 deletions crates/burn-core/src/nn/pool/max_pool1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct MaxPool1dConfig {
/// The size of the kernel.
pub kernel_size: usize,
/// The stride.
#[config(default = "1")]
#[config(default = "kernel_size")]
pub stride: usize,
/// The padding configuration.
///
Expand Down Expand Up @@ -100,6 +100,7 @@ impl MaxPool1d {
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;

#[test]
#[should_panic = "Same padding with an even kernel size is not supported"]
Expand All @@ -115,8 +116,21 @@ mod tests {
let layer = config.init();

assert_eq!(
alloc::format!("{}", layer),
"MaxPool1d {kernel_size: 3, stride: 1, padding: Valid, dilation: 1}"
alloc::format!("{layer}"),
"MaxPool1d {kernel_size: 3, stride: 3, padding: Valid, dilation: 1}"
);
}

#[rstest]
#[case(1)]
#[case(2)]
fn default_strides_match_kernel_size(#[case] kernel_size: usize) {
let config = MaxPool1dConfig::new(kernel_size);

assert_eq!(
config.stride, kernel_size,
"Expected stride ({:?}) to match kernel size ({:?}) in default MaxPool1dConfig::new constructor",
config.stride, config.kernel_size
);
}
}
18 changes: 16 additions & 2 deletions crates/burn-core/src/nn/pool/max_pool2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct MaxPool2dConfig {
/// The size of the kernel.
pub kernel_size: [usize; 2],
/// The strides.
#[config(default = "[1, 1]")]
#[config(default = "kernel_size")]
pub strides: [usize; 2],
/// The padding configuration.
///
Expand Down Expand Up @@ -100,6 +100,7 @@ impl MaxPool2d {
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;

#[test]
#[should_panic = "Same padding with an even kernel size is not supported"]
Expand All @@ -116,7 +117,20 @@ mod tests {

assert_eq!(
alloc::format!("{}", layer),
"MaxPool2d {kernel_size: [3, 3], stride: [1, 1], padding: Valid, dilation: [1, 1]}"
"MaxPool2d {kernel_size: [3, 3], stride: [3, 3], padding: Valid, dilation: [1, 1]}"
);
}

#[rstest]
#[case([2, 2])]
#[case([1, 2])]
fn default_strides_match_kernel_size(#[case] kernel_size: [usize; 2]) {
let config = MaxPool2dConfig::new(kernel_size);

assert_eq!(
config.strides, kernel_size,
"Expected strides ({:?}) to match kernel size ({:?}) in default MaxPool2dConfig::new constructor",
config.strides, config.kernel_size
);
}
}
1 change: 1 addition & 0 deletions crates/burn-no-std-tests/src/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl<B: Backend> ConvBlock<B> {
.with_padding(nn::PaddingConfig2d::Same)
.init(device);
let pool = nn::pool::MaxPool2dConfig::new(config.kernel_size)
.with_strides([1, 1])
.with_padding(nn::PaddingConfig2d::Same)
.init();
let activation = nn::Gelu::new();
Expand Down
Loading