An ONNX model with dilated average pooling could load into Burn and give wrong numbers without a single error. The 2D import just dropped the dilation.

antimora opened issue #6148 to fix that at the source, and I added the missing option in tracel-ai/burn#6235. The trick was not inventing anything new.

Two quick definitions

Burn is a Rust framework for building and training neural networks. Pooling shrinks a grid of numbers (a tensor) by sliding a small window over it and keeping one value per spot: the max, or the average.

Dilation puts gaps in that window. A 2 by 2 window with dilation 2 reads four cells spaced two apart, like jumping two squares at a time on a chessboard, so it spans a 3 by 3 area. ONNX, the common format for sharing trained models, has supported dilation on AveragePool since opset 19. Burn's max pooling had it. Average pooling didn't.

The plan: follow the neighbour

Max pooling's dilation already travelled a known route through Burn: the options type, the tensor API, the backend trait, autodiff (automatic gradients), a few internal layers that record, merge and route operations, then the CPU (Flex) and GPU (CubeCL) kernels. I walked average pooling down that exact route and put dilation right after padding in every signature, where max pooling has it. That came to 24 files.

The issue made this much easier, because antimora had already written the rules down: element k of the window reads start + k * dilation, when padding cells are counted in the average, the spread out steps that land in padding count too, and ceil mode uses the spread out size (kernel - 1) * dilation + 1.

For users it's one new builder method, default 1, so existing code behaves the same (trimmed from a test):

AvgPoolOptions::new([2, 2]) .with_stride([1, 1]) .with_dilation([2, 2]) 

In the kernels, the core change is that each window step now jumps by the dilation, and the "which steps are inside the input" helper is now shared with max pooling instead of copied.

One wrinkle on the GPU

Burn's GPU 2D average pooling calls CubeK, a separate library without dilation. Burn's own 3D kernel got dilation in this PR, so a dilated 2D pool now runs as a 3D pool with depth 1. Without dilation, nothing changes and CubeK still does the work.

Heads up, it's a breaking change: struct literals of AvgPoolOptions must now include dilation: [1; N], and backends outside Burn get one more argument. The title says feat(tensor)! and the changelog explains it.

Tests

I reused ONNX's own test cases where they existed, such as test_averagepool_2d_dilations, and added forward tests for 1D, 2D and 3D plus gradient tests for 2D and 3D. Green on Flex and Metal. As a sanity check, pinning the Flex kernels to dilation 1 again turns every new test red.

Review

laggui approved with one note: the GPU fallback copies multi channel inputs into another memory layout, which undoes an optimization and costs a full tensor copy. laggui called it non blocking and opened tracel-ai/cubek#974 so CubeK's average pooling can take a dilation natively. The merge came from laggui on 27 October 2026.

The lesson

When the feature you need already exists one module over, its route through the code is your map. Copy the route, argument order included, and the change reads like it was always there.

What's your move when a feature exists for one operation but not its sibling: copy the path, or refactor them into one?

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