Skip to content

Make squeeze and slice_by_size const folding agree with their type inference - #2803

Open
LeSingh1 wants to merge 1 commit into
apple:mainfrom
LeSingh1:tensor-transformation-value-inference
Open

Make squeeze and slice_by_size const folding agree with their type inference#2803
LeSingh1 wants to merge 1 commit into
apple:mainfrom
LeSingh1:tensor-transformation-value-inference

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Three cases where an op's value_inference disagrees with its own type_inference, so const folding changes the shape the graph was built for.

squeeze folds without applying the axis rules type_inference uses, and slice_by_size folds a negative size differently from how it types it.

Both now fold to match their declared output type.

Each new test fails on main and passes here; existing tests in the file unchanged.

…ference

squeeze documents, and type_inference implements, PyTorch's rule that an
axis whose size is not 1 is ignored instead of raising. value_inference
handed the axes straight to np.squeeze, which raises. So squeezing a const
along a non-single dimension aborted the conversion, even though the same
program converts and runs when the input is not const.

squeeze's zero rank result was returned as self.x.val[0], which is only a
scalar when x is rank 1. For an all-ones shape of rank 2 or more this
returned an array, and building the op failed with "Types should have zero
rank ndarray input". Index the squeezed value instead.

slice_by_size's value_inference treated any size <= 0 as "the rest of the
dimension", while its type_inference (and the docstring) give that meaning
to -1 only. A size of 0 therefore produced a const whose value disagreed
with its own declared shape.
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.

1 participant