Skip to content

Fall back instead of baking a symbolic dim into softplus and nll_loss - #2812

Open
LeSingh1 wants to merge 1 commit into
apple:mainfrom
LeSingh1:torch-symbolic-dim-fallbacks
Open

Fall back instead of baking a symbolic dim into softplus and nll_loss#2812
LeSingh1 wants to merge 1 commit into
apple:mainfrom
LeSingh1:torch-symbolic-dim-fallbacks

Conversation

@LeSingh1

@LeSingh1 LeSingh1 commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Same class as #2808: both converters read a shape dim and use it where only a constant works, so a flexible input shape aborts the conversion.

softplus with beta != 1 on a rank 4 input builds per-channel alpha/beta for softplus_parametric out of x.shape[1]:

model = torch.jit.trace(nn.Softplus(beta=2).eval(), torch.rand(2, 3, 4, 5))
ct.convert(model, inputs=[ct.TensorType(shape=(2, ct.RangeDim(2, 10), 4, 5))])
# TypeError: Cannot convert symbols to int

Falls back to the general decomposition softplus(beta * x) / beta when the channel dim is dynamic — the same path every other rank already takes, and numerically identical.

nll_loss with reduction="mean" divided by np.float32(x.shape[0]) before summing, which raises TypeError: Cannot convert expression to float on a dynamic batch. reduce_mean over the batch axis is the same value and needs no constant, so the batch size is no longer read at all. "none" and "sum" never used it and are unchanged.

Testing

test_softplus_beta_dynamic_channel and test_nllloss_dynamic_batch (all three reductions). 4 of the 8 cases fail on main — the "none" and "sum" reductions pass either way and are there as controls. Existing test_softplus and test_nllloss are unchanged and still pass. Checked nll_loss numerically at a batch size different from the traced one.

This machine has a broken scikit-learn install that makes every TorchFrontend.TORCHEXPORT case error out, including pre-existing ones, so I could only exercise the TorchScript frontend locally.

Both converters read a shape dim and use it where only a constant works,
so a flexible input shape aborts the conversion.

softplus with beta != 1 on a rank 4 input builds per channel alpha and
beta for softplus_parametric out of x.shape[1]. A dynamic channel dim
raises "TypeError: Cannot convert symbols to int". Take the existing
general decomposition, softplus(beta * x) / beta, in that case; it is what
every other rank already uses and is numerically the same.

nll_loss with reduction="mean" divided by np.float32(x.shape[0]) before
summing, which raises "TypeError: Cannot convert expression to float" on a
dynamic batch. reduce_mean over the batch axis is the same value and needs
no constant, so the batch size is no longer read at all.
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