Skip to content

Support a symbolic batch dim in torch narrow and group_norm - #2808

Open
LeSingh1 wants to merge 1 commit into
apple:mainfrom
LeSingh1:torch-symbolic-shape-consts
Open

Support a symbolic batch dim in torch narrow and group_norm#2808
LeSingh1 wants to merge 1 commit into
apple:mainfrom
LeSingh1:torch-symbolic-shape-consts

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Both converters bake x.shape into a constant, so a symbolic dim aborts the conversion with ValueError: Cannot add const [is0, 2, 4] ... cannot have symbolic values.

model = torch.jit.trace(nn.GroupNorm(2, 4).eval(), torch.rand(2, 4, 8, 8))
ct.convert(model, inputs=[ct.TensorType(shape=(ct.RangeDim(1, 8), 4, 8, 8))])

narrow

end = list(x.shape), with only the narrowed dim overwritten. The end of every other dim is irrelevant — the slice covers them whole — so state end = 0 and mask them off instead. That drops the dependency on x.shape entirely. The negative-start handling is unchanged.

_group_norm_impl

The guard was if not any_symbolic(x.shape[2:]), i.e. it checked only the spatial dims, but the static branch also bakes in the batch dim via new_shape = [n, num_groups, c // num_groups]. A symbolic batch therefore took the static path and produced a constant holding a symbol. Widened the guard to the whole shape, and the dynamic path now reads the batch dim from mb.shape rather than from x.shape[0].

The existing test_groupnorm_dynamic makes height and width dynamic but pins the batch to 6, which is why only the spatial dims were covered.

Testing

TestNarrow::test_narrow_dynamic_batch and TestGroupNorm::test_groupnorm_dynamic_batch (ranks 2, 3 and 4, so both the presence and absence of spatial dims are covered). Both fail on main and pass here; existing TestNarrow and TestGroupNorm tests are unchanged and still pass.

I could only exercise the TorchScript frontend locally — this machine has a broken scikit-learn install that makes every TorchFrontend.TORCHEXPORT case error out, including the pre-existing test_groupnorm_dynamic, so those results are not mine to report.

Both converters materialize x.shape into a constant, which fails when any
dim is symbolic: "Cannot add const [is0, 2, 4] ... cannot have symbolic
values".

narrow built `end = list(x.shape)` and only overwrote the narrowed dim.
The end of every other dim is irrelevant, since the slice covers them
whole, so state `end = 0` and mask them off instead. That drops the
dependency on x.shape entirely. The negative-start handling is unchanged.

_group_norm_impl guarded with `any_symbolic(x.shape[2:])`, i.e. it checked
only the spatial dims, and then built `[n, num_groups, c // num_groups]`
from the batch dim regardless. A symbolic batch took the static path and
produced a constant holding a symbol. Widen the guard to the whole shape
and read the batch dim from mb.shape in the dynamic path.

The existing test_groupnorm_dynamic makes height and width dynamic but
pins the batch to 6, which is why only the spatial dims were covered.
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