Skip to content

squeeze.dims: keep listed dims whose size is not 1 - #146

Merged
gokulkrishna98 merged 3 commits into
apple:mainfrom
john-rocky:fix/squeeze-non-unit-dim
Nov 11, 2026
Merged

gokulkrishna98 merged 3 commits into
apple:mainfrom
john-rocky:fix/squeeze-non-unit-dim

Conversation

@john-rocky

Copy link
Copy Markdown
Contributor

Summary

x.squeeze(1) on a [1, 4, 8] tensor is a no-op in PyTorch, but the exported aten.squeeze.dims node still lists dim 1. replace_squeeze_dims passed every listed dim to coreai.shrink_dims, which rejects a dim of size 4 (ValueError: Operation creation failed). With torch 2.13.0, x.squeeze() decomposes to squeeze.dims over every dim and fails the same way. A listed dynamic dim, which PyTorch keeps, was shrunk too, and conversion failed (squeeze[0]: rank 2 vs 3).

The lowering now shrinks only the listed dims whose size is a static 1 in the exported input, and returns the input when there are none.

Testing

test_squeeze_dims_not_size_one runs torch.squeeze(x, [1]), torch.squeeze(x, [0, 1]) and x.squeeze() on [1, 4, 8], static and with dims 1 and 2 dynamic, against PyTorch. All six fail before the fix and pass after it.

uv run pytest tests/ops/test_ops.py -k squeeze   # 50 passed
uv run pytest tests/ -n 4                        # 6560 passed; 3 failed in tests/debugging/, as on main
uv run --extra dev ruff check .                  # All checks passed
uv run --extra dev ruff format --check .         # all files formatted

Fixes #143

PyTorch leaves a listed dim in place when its size is not 1, and
x.squeeze() exports as squeeze.dims over every dim. The lowering passed
all listed dims to shrink_dims, which rejects a static dim of size 4
("Operation creation failed") and drops a dynamic dim that PyTorch keeps
(rank mismatch). Only dims of static size 1 in the exported input are
shrunk now.
@gokulkrishna98
gokulkrishna98 self-requested a review November 9, 2026 21:53
@gokulkrishna98
gokulkrishna98 merged commit 7a9a7db into apple:main Nov 11, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

squeeze(dim) on a dimension whose size is not 1 fails to convert; PyTorch returns the input unchanged

2 participants