[Fix] Scope deferred_runtime_asserts per piecewise sub-graph to prevent NameError#27
Merged
cennn merged 1 commit intoApr 27, 2026
Conversation
…nt NameError After piecewise splitting, sub-graphs share the same ShapeEnv whose deferred_runtime_asserts may reference backed SymInts (e.g. s90) that only exist as placeholders in another sub-graph. Inductor blindly emits these as Python runtime assertions, causing NameError at runtime. Add _scope_deferred_runtime_asserts context manager that temporarily narrows deferred_runtime_asserts to only reachable backed symbols before each standalone_compile call, then restores the original dict afterwards. Includes pytest that reproduces the bug (patch fix away → NameError) and verifies the fix (with fix → passes).
f778e70 to
7fc0725
Compare
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.
🗂️ PR Category
Problem
After piecewise splitting, each sub-graph is compiled via
standalone_compileindependently but shares the sameShapeEnv. When unbacked SymInts (frommark_unbacked) createEqconstraints linking multiple backed SymInts(e.g.
Eq(u0+u1+u2, s77)andEq(u0+u1+u2, s90)), Inductor'sGraphLoweringblindly copies the entiredeferred_runtime_assertsdict and emits each entry as a Python runtime assertion. A sub-graph that only hass90in its placeholders still inheritsEq(u0+u1+u2, s77), generatingif not (u0+u1+u2 == s77):wheres77is undefined →NameError.Fix
Add
_scope_deferred_runtime_assertscontext manager inpiecewise_compiler.py.Before each
standalone_compilecall, it:example_inputsdeferred_runtime_assertsto only those whose backed symbols are all reachableOnly applied when
dynamic_shapes="from_tracing_context"(Inductor-specific).Test
test_piecewise_deferred_assert_scope.py:test_without_fix_raises_nameerror: patches fix away → confirmsNameErrortest_with_fix_passes: runs with fix → passes with two different shapes