Skip to content

[torchlib] Implement adaptive_max_pool{1,2,3}d for dynamic shapes - #3219

Open
Qiming Bao (14H034160212) wants to merge 1 commit into
microsoft:mainfrom
14H034160212:torchlib-adaptive-max-pool-dynamic
Open

Qiming Bao (14H034160212) wants to merge 1 commit into
microsoft:mainfrom
14H034160212:torchlib-adaptive-max-pool-dynamic

Conversation

@14H034160212

@14H034160212 Qiming Bao (14H034160212) commented Nov 16, 2026 •

Copy link
Copy Markdown

Follow-up to #3201 for the max-pooling case raised in pytorch/pytorch#204405.

torchlib has only stubs for aten::adaptive_max_pool{1,2,3}d, so the exporter uses the torch decomposition. That decomposition computes the kernel size from the input size, which specializes it to the example input. With dynamic spatial dims the ONNX model keeps dynamic input axes but is wrong at every other size:

adaptive_max_pool2d(x, (2, 3)), exported at 6x6 PyTorch main
run at 12x12 (2, 3) (4, 6)
run at 8x9 (2, 3) (2, 4)

adaptive_max_pool1d does not export at all on main (No ONNX function found for aten.adaptive_max_pool2d).

Implementation

The prefix-sum approach from #3201 only works for averages, so max pooling selects each window with a mask. For one spatial axis of size n and output size o:

  • Window i is [floor(i*n/o), ceil((i+1)*n/o)). n comes from Shape at run time, so nothing depends on the example size. None keeps the axis (o = n).
  • x gets a new axis of length o, positions outside each window become -inf, and ReduceMax / ArgMax over the original axis give the values and the position of the first maximum. The intermediate tensor is o times the size of the input along that axis.
  • If a window holds only -inf, ArgMax can return a position before the window, so the position is clamped to the window start. That is what PyTorch reports in that case.

The spatial axes are pooled one at a time, innermost first. The positions found for inner axes are carried along with GatherElements and combined into PyTorch's flattened spatial index (h * W + w for 2d, (d * H + h) * W + w for 3d). Pooling the innermost axis first also reproduces PyTorch's tie breaking, which is the first maximum in row-major order within the window.

NaN handling follows ReduceMax / ArgMax rather than PyTorch, which propagates NaN.

Tests

  • New test_adaptive_max_pool_with_dynamic_spatial_dims (1d, 2d, 2d with 7x7 output, 3d) exports with every dim dynamic and runs at the example size and other sizes, including sizes smaller than the output. For each size it checks random inputs, inputs with many ties, and windows that hold only -inf. It requires values and indices to match PyTorch exactly. On main, 1d fails to export and 2d and 3d return wrong shapes. With this change all 4 pass.
  • OpInfo entries for nn.functional.adaptive_max_pool{1,2,3}d, with a wrangler for the positional return_indices. Samples with return_indices=False are skipped because the aten op always returns the indices. 6 passed, 66 subtests passed.
  • ruff check and ruff format (0.15.8) are clean.

This branch and #3201 merge without conflicts (checked with git merge-tree), so they can land in either order.

torchlib had no adaptive max pooling, so the exporter used the torch
decomposition, which specializes the kernel size to the example input.
With dynamic spatial dims the ONNX model then gives wrong shapes at other
sizes.

Pool one spatial axis at a time, innermost first. The window bounds come
from the run-time size, each window is selected with a mask, and
ReduceMax/ArgMax give the values and positions. The per-axis positions are
combined into PyTorch's flattened spatial index, with PyTorch's tie
breaking (first maximum in row-major order).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

1 participant