Fix max_pool conversion for unbatched torch input#2723
Open
devin-lai wants to merge 1 commit into
Open
Conversation
PyTorch's MaxPool{1,2,3}d accepts unbatched (C, *spatial) input as
well as the batched (N, C, *spatial) form. MIL's max_pool requires
two leading (N, C) dims, so the converter has to bridge. Wrap the
input in expand_dims when its rank is below spatial_rank + 2, run
the pool, then squeeze the prepended dims back off, mirroring the
existing _adaptive_pool2d handling in the same file. The batched
path is unchanged.
Fixes apple#2148
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.
Summary
(C, *spatial)inputs when converting torch MaxPool1d/2d/3d ops.max_pool, then squeeze them back off after pooling.kernel_sizes, and leave the existing batched path unchanged.Details
PyTorch MaxPool1d, MaxPool2d, and MaxPool3d accept both batched
(N, C, *spatial)inputs and unbatched(C, *spatial)inputs. MILmax_poolexpects two leading(N, C)dimensions, so an unbatched MaxPool2d input such as(C, H, W)reaches MIL as a rank-3 tensor and its type inference only sees one spatial dimension. That causes the spatial-shape consistency check to fail before conversion completes.This change mirrors the existing adaptive-pooling handling: when the input rank is lower than
spatial_rank + 2, the converter inserts the missing leading dims withexpand_dims, appliesmax_pool, and removes those temporary dims withsqueeze. Batched inputs still go through the originalmax_poolpath and keep the original node name.Tests
python -m pytest -q --log-cli-level=CRITICAL --log-level=CRITICAL coremltools/converters/mil/frontend/torch/test/test_torch_ops.py::TestMaxPool::test_max_pool_unbatched_inputFixes #2148.