Repository navigation
[torchlib] Implement adaptive_max_pool{1,2,3}d for dynamic shapes - #3219
Open
Qiming Bao (14H034160212) wants to merge 1 commit into
Open
Qiming Bao (14H034160212) wants to merge 1 commit into
Qiming Bao (14H034160212) wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 6x6mainadaptive_max_pool1ddoes not export at all onmain(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
nand output sizeo:iis[floor(i*n/o), ceil((i+1)*n/o)).ncomes fromShapeat run time, so nothing depends on the example size.Nonekeeps the axis (o = n).xgets a new axis of lengtho, positions outside each window become-inf, andReduceMax/ArgMaxover the original axis give the values and the position of the first maximum. The intermediate tensor isotimes the size of the input along that axis.-inf,ArgMaxcan 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
GatherElementsand combined into PyTorch's flattened spatial index (h * W + wfor 2d,(d * H + h) * W + wfor 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/ArgMaxrather than PyTorch, which propagates NaN.Tests
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. Onmain, 1d fails to export and 2d and 3d return wrong shapes. With this change all 4 pass.nn.functional.adaptive_max_pool{1,2,3}d, with a wrangler for the positionalreturn_indices. Samples withreturn_indices=Falseare skipped because the aten op always returns the indices. 6 passed, 66 subtests passed.ruff checkandruff 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.