Support return_indices for max_pool2d in the torch frontend#2717
Open
adityasingh2400 wants to merge 1 commit into
Open
Support return_indices for max_pool2d in the torch frontend#2717adityasingh2400 wants to merge 1 commit into
adityasingh2400 wants to merge 1 commit into
Conversation
max_pool2d_with_indices bound its indices output to None, so any model that consumed the indices failed conversion with a missing-var error (issue apple#2456). Recover the indices by replaying the pooling windows with sliding_windows, taking reduce_argmax inside each window, and mapping the within-window position back to a flattened input coordinate. The pooled output's spatial size is used directly so the ceil_mode window-dropping rule matches PyTorch. reduce_argmax's first-occurrence tie-break matches PyTorch, and padded cells are filled with the lowest float so they are never selected. Adds a parametrized test covering kernel size, stride, padding, and ceil_mode for both the TorchScript and torch.export frontends.
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.
Fixes #2456.
Problem
max_pool2dwithreturn_indices=Truecould not be converted. The torch frontend bound the indices output toNone, so any model that read the indices failed withValueError: Torch var <n> not found in context:Fix
MIL has no max-pool-with-indices op, so the indices are recovered from the pooling windows. The input is padded so window extraction lines up with the pooled output, the windows are materialized with
sliding_windows(batch and channel are folded together to stay within Core ML's rank-5 limit),reduce_argmaxfinds the selected element inside each window, and the within-window position is mapped back to a flattened input coordinate, which is what PyTorch returns.A few details match PyTorch exactly:
ceil_moderule that drops a trailing window starting inside the bottom/right padding is honored.reduce_argmaxreturns the first occurrence on ties, matching PyTorch's tie-breaking.-inf.The values output is unchanged. 1D/3D
return_indicesand dynamic input shapes raise a clearNotImplementedErrorinstead of silently producing wrong results.Testing
Added
test_max_pool2d_return_indices, parametrized over kernel size, stride, padding, andceil_modefor both the TorchScript and torch.export frontends. Indices are compared against the torch reference on the fp32 path; a fp16 backend can rank two near-equal window elements differently from torch's fp32 argmax, so that combination is skipped for the index comparison. The existingmax_pooltests continue to pass.