Accept an unflatten size read from the input shape - #2799
Open
LeSingh1 wants to merge 1 commit into
Open
Conversation
torch.unflatten(x, 0, (x.shape[0], 1)) over a flexible dimension aborts
conversion with
ValueError: Cannot add const [<Var object ...>, <Var object ...>]
A size read off the input comes from a prim::ListConstruct whose elements are
only known at run time, so _get_bindings binds it to a python list of scalar
Vars rather than to a single const Var. The converter already assembles the
target shape dynamically, but handed that python list straight to mb.concat,
which tried to turn it into a const.
Stack the elements into the rank 1 shape tensor concat expects. A size that
is already const is unaffected: it still binds to a single Var and takes the
existing path.
test_unflatten could not reach this because it builds nn.Unflatten from a
python list fixed at construction time, so its sizes are always const.
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.
Problem
torch.unflattenwith a size read off the input aborts conversion once that dimension is flexible:A size read off the input comes from a
prim::ListConstructwhose elements are only known at run time, so_get_bindingsbinds it to a python list of scalar Vars rather than to a single const Var. The converter already assembles the target shape dynamically —— but hands that python list straight to
mb.concat, which tries to turn it into a const and fails.Fix
Stack the list elements into the rank 1 shape tensor
concatexpects. A size that is already const is untouched: it still binds to a single Var and takes the existing path, so nothing about the static case changes.Test
TestUnflatten::test_unflatten_size_read_from_input_shapeunflattens a flexible leading dimension into(n, 1)and(1, n)withnread fromx.shape, and compares against torch. Both splits keep the product equal to the split dimension for every size theRangeDimadmits, so the model is valid across the whole range rather than only at the sizes the test happens to feed.All 4 parametrizations fail with
Cannot add const [...]without the fix and pass with it.The existing
test_unflattencannot reach this path: it buildsnn.Unflattenfrom a python list fixed at construction time, so its unflattened size is always a compile-time constant even in itsdynamic=Truecases.TorchScript only —
torch.exportresolves the size symbolically instead of emitting a list, so it never produces this binding.Related
There is a long-open PR #2050 that attacks the same symptom from the other end, by making
_array_constructitself emit a concat when aListConstructdepends on graph inputs. This change is deliberately narrower — it only teachesunflattento accept the list form the current_array_constructalready produces, and would simply become a no-op if #2050 were to land.Verification
passes on macOS / Apple silicon, torch 2.12, with the pre-existing xfails/skips unchanged.