Skip to content

[Fix] Scope deferred_runtime_asserts per piecewise sub-graph to prevent NameError#27

Merged
cennn merged 1 commit into
SandAI-org:mainfrom
cennn:fix/unbacked-symint-symbolic-unification
Apr 27, 2026
Merged

[Fix] Scope deferred_runtime_asserts per piecewise sub-graph to prevent NameError#27
cennn merged 1 commit into
SandAI-org:mainfrom
cennn:fix/unbacked-symint-symbolic-unification

Conversation

@cennn
Copy link
Copy Markdown
Collaborator

@cennn cennn commented Apr 27, 2026

🗂️ PR Category

  • ✨ New Feature
  • 🚀 Optimization (performance, memory, etc.)
  • 💥 Breaking Change
  • 🐛 Bug Fix
  • 🛠️ Development / Refactoring
  • 📚 Documentation
  • 🧹 Chore (Dependencies, CI/CD, Configuration, etc.)
  • 🧪 Testing

Problem

After piecewise splitting, each sub-graph is compiled via standalone_compile independently but shares the same ShapeEnv. When unbacked SymInts (from mark_unbacked) create Eq constraints linking multiple backed SymInts
(e.g. Eq(u0+u1+u2, s77) and Eq(u0+u1+u2, s90)), Inductor's GraphLowering blindly copies the entire deferred_runtime_asserts dict and emits each entry as a Python runtime assertion. A sub-graph that only has s90 in its placeholders still inherits Eq(u0+u1+u2, s77), generating if not (u0+u1+u2 == s77): where s77 is undefined → NameError.

Fix

Add _scope_deferred_runtime_asserts context manager in piecewise_compiler.py.
Before each standalone_compile call, it:

  1. Collects all backed symbols reachable from the sub-graph's example_inputs
  2. Filters deferred_runtime_asserts to only those whose backed symbols are all reachable
  3. Restores the original dict after compilation

Only applied when dynamic_shapes="from_tracing_context" (Inductor-specific).

Test

test_piecewise_deferred_assert_scope.py:

  • test_without_fix_raises_nameerror: patches fix away → confirms NameError
  • test_with_fix_passes: runs with fix → passes with two different shapes

@cennn cennn changed the title Fix/unbacked symint symbolic unification [Fix] Scope deferred_runtime_asserts per piecewise sub-graph to prevent NameError Apr 27, 2026
…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).
@cennn cennn force-pushed the fix/unbacked-symint-symbolic-unification branch from f778e70 to 7fc0725 Compare April 27, 2026 05:59
Copy link
Copy Markdown
Collaborator

@wtr0504 wtr0504 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cennn cennn merged commit 2ea0ae6 into SandAI-org:main Apr 27, 2026
1 check passed
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.

2 participants