Fix max_pool conversion for unbatched torch input
Summary
- Support unbatched
(C, *spatial)inputs when converting torch MaxPool1d/2d/3d ops. - Temporarily prepend missing leading dims before MIL
max_pool, then squeeze them back off after pooling. - Derive the pool spatial rank from
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. MIL max_pool expects 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 with expand_dims, applies max_pool, and removes those temporary dims with squeeze. Batched inputs still go through the original max_pool path 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_input
Fixes #2148.