Lower adaptive pooling to output size 1 as a global reduce in 1D - #2787
Open
LeSingh1 wants to merge 1 commit into
Open
Lower adaptive pooling to output size 1 as a global reduce in 1D#2787LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
nn.AdaptiveAvgPool1d(1) / nn.AdaptiveMaxPool1d(1) over a flexible length,
i.e. global pooling across a variable-length sequence, aborts conversion with
File ".../sympy/core/expr.py", line 375, in __float__
TypeError: Cannot convert expression to float
_adaptive_pool1d always slices the input into per-output-element windows,
whose boundaries it computes in python from the pooled dimension. That
dimension is a sympy symbol for a flexible input, so the floor/ceil of the
window boundaries raises out of sympy.
_adaptive_pool2d already special-cases output size == (1, 1) into a global
reduce_mean / reduce_max, which needs no window boundaries and so works with
a symbolic shape; nn.AdaptiveAvgPool2d((1, 1)) converts today. Give the 1D
path the same treatment, and let the remaining output sizes report the same
clear ValueError that _adaptive_pool2d raises instead of surfacing a sympy
error from the middle of the converter.
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.
Problem
nn.AdaptiveAvgPool1d(1)over a flexible length — global pooling across a variable-length sequence, the standard head of a 1-D convnet — aborts conversion:_adaptive_pool1dalways slices the input into one window per output element, computing the window boundaries in python from the pooled dimension:For a flexible input
in_dimensionis a sympy symbol, sofloor/ceilraise out of sympy, from the middle of the converter with no indication of what the user did wrong.The 2-D equivalent has no such problem:
_adaptive_pool2dspecial-cases output size(1, 1)into a globalreduce_mean/reduce_max, which needs no window boundaries, sonn.AdaptiveAvgPool2d((1, 1))over a flexible input converts today. The 1-D path just never got the same treatment.Fix
Give
_adaptive_pool1dthe two cases_adaptive_pool2dalready has:ValueError_adaptive_pool2draises, instead of a sympyTypeError.Tests
TestAdaptiveAvgPool::test_adaptive_avg_pool1d_symbolic_input—nn.AdaptiveAvgPool1d(1)with aRangeDimlength. Fails with the sympyTypeErrorwithout the fix.TestAdaptiveAvgPool::test_adaptive_avg_pool1d_output_size_1— output size 1 with static shapes over rank 3 and rank 2 input, pinning the new global-reduce path against torch. The existingtest_adaptive_max_pool1dparametrizations only use output sizes 3 and 5, so output size 1 was uncovered.The symbolic test skips the torch.export frontends, which decompose
AdaptiveAvgPool1d(1)into ameanop before the converter sees it.Verification
618 passed, 2 skipped on macOS / Apple silicon, torch 2.12, across mlprogram + neuralnetwork and the TorchScript / TorchExport / ExecuTorch frontends.