Inspiration

Burn is an open source deep learning library in Rust. Its max pooling supported dilation, where the pooling window reads cells spaced apart, but its average pooling did not. In issue #6148, antimora explained why that mattered: ONNX's AveragePool has had a dilations attribute since opset 19, and burn-onnx had nowhere to send it. Dilated 2D models lost their dilation on import and computed wrong results silently, and the upcoming 3D import could only refuse them.

What it does

tracel-ai/burn#6235 adds AvgPoolOptions::with_dilation, default 1, for 1D, 2D and 3D average pooling, with gradients, on the Flex (CPU) and CubeCL (GPU) backends. The meaning follows max pooling and the issue: window element k reads start + k * dilation, count_include_pad (whether padding cells count toward the average) counts the spaced out window steps that land in padding, and ceil mode (rounding the output size up) uses the dilated extent (kernel - 1) * dilation + 1. Existing code keeps its results.

How I built it

I traced the path max pooling's dilation takes through Burn and gave average pooling the same one: options type, tensor API, ModuleOps backend trait, autodiff (automatic gradients), the internal layers that record, merge and route operations, and the kernels, with dilation placed right after padding everywhere. In total, 24 files.

In the Flex kernel, the input index now steps by the dilation, and average pooling reuses max pooling's helper for the valid window range (renamed valid_kernel_range) instead of keeping its own undilated copy. padded_len_avgpool now counts the spaced out steps too.

Coverage: ONNX's averagepool_*_dilations* vectors, plus new 1D, 2D and 3D forward cases and 2D and 3D gradient cases, each with count_include_pad on and off. All pass on Flex and Metal, and all of them fail if the Flex kernels are forced back to dilation 1. cargo run-checks passed on macOS arm64.

Challenges I ran into

On CubeCL, 2D average pooling is delegated to CubeK, which had no dilation support. For dilated 2D pooling only, Burn now reshapes the input to depth 1 and runs its own 3D kernel:

let x = reshape(x, Shape::new([batch_size, channels, 1, in_h, in_w]));

The other challenge was ceil mode combined with count_include_pad. The ONNX reference counts window steps past the padding; Burn, like PyTorch, does not. Since that gap existed before my change, I wrote it up in the PR and stayed with Burn's rule.

Accomplishments that I'm proud of

A breaking change, flagged as feat(tensor)! with a changelog entry for struct literals and third party backends, that slots in beside max pooling without new concepts. Merged by laggui on 27 October 2026 after approval.

What I learned

A sibling feature is the best guide for a new one. And a fallback can be correct while still costing performance: laggui noted that mine copies multi channel inputs back to NCHW layout, undoing the NHWC optimization (two orders for storing image data in memory) and adding a full copy.

What's next

laggui followed up in CubeK with tracel-ai/cubek#974, merged the same day, which gives CubeK's own average pooling a dilation option. On the ONNX side, burn-onnx now has a Burn option it can map dilations onto, instead of dropping or rejecting it.

The full story, with all the code, is on my blog: https://ashokramsi.hashnode.dev/burn-avg-pool-dilation

Built With

Share this project:

Updates