[Fix]: handle zero-one specialization in PiecewiseBackend#24
Merged
jiahy0825 merged 1 commit intoApr 23, 2026
Merged
Conversation
When the first call uses batch size 0 or 1, PyTorch Dynamo specializes that dimension as a static constant, leaving sym_shape_indices empty and causing an AssertionError on subsequent calls with different shapes. - Raise ValueError early in _mark_dynamic_shapes when dim_size <= 1 so users get a clear, actionable error instead of a cryptic assert - Replace the hard assert in PiecewiseBackend.__call__ with a fallback to compiled_graph_for_general_shape + info log as a safety net - Add test_mlp_batch1_first_call_raises regression test
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.
When the first call uses batch size 0 or 1, PyTorch Dynamo specializes that dimension as a static constant, leaving sym_shape_indices empty and causing an AssertionError on subsequent calls with different shapes.
🗂️ PR Category
📝 Description
Problem
From: ISSUE-23
When the first call to a @magi_compile-decorated module uses batch size 0 or 1, PyTorch Dynamo's zero-one specialization treats that dimension as a static constant rather than a symbolic value. This causes sym_shape_indices to be empty in PiecewiseBackend, resulting in AssertionError: No symbolic shape indices found on subsequent calls with different shapes.
Changes
Raise early with a clear message: Upgrade the existing magi_logger.warning in _mark_dynamic_shapes to a ValueError, so users get an actionable error at the point of compilation rather than a cryptic assert deep in the piecewise backend.
Add fallback safety net: Replace the hard assert in PiecewiseBackend.call with a graceful fallback to compiled_graph_for_general_shape plus an info log, covering any other scenario where sym_shape_indices might be empty.
Add regression test: test_mlp_batch1_first_call_raises verifies that calling with batch=1 as the first invocation raises ValueError with a clear message.
Test
All 5 tests in tests/model_tests/test_mlp_infer.py pass, including the new regression test.