Skip to content

fix(profiling): replay extern ops with compiled regions forced eager - #119

Merged
jiahy0825 merged 3 commits into
mainfrom
fix/replay-without-nested-compile
Nov 28, 2026
Merged

jiahy0825 merged 3 commits into
mainfrom
fix/replay-without-nested-compile

Conversation

@cennn

@cennn cennn commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

🗂️ PR Category

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

📝 Description

The runtime estimator times an extern kernel by replaying its op in the middle of the Inductor compile. When the op's implementation contains a torch.compiled region, for example a custom attention op that runs torch.compile(flex_attention) on first use (the documented way to use flex_attention), that replay fails the whole piecewise compile:

torch._dynamo.exc.BackendCompilerFailed: backend='<magi_compiler.magi_backend.magi_backend.MagiBackend ...>' raised:
RuntimeError: ('Error: specified log level PT2_COMPILE, but the event %s is not logged to pt2_compile_events. ...', 'inductor_compile')
  • Cause. torch._dynamo.disable on the replay keeps Dynamo from tracing the replay frame, not from compiling a torch.compiled region called inside it. The replay therefore starts a new top-level Dynamo compile nested in the Inductor compile, and the exit of that compile resets the chromium event log, clearing the enclosing compile's pt2 compile events; _compile_fx_inner then fails when it logs inductor_compile. The replay itself returns normally, so the estimator's fallback on replay errors never sees it. A log that hits this also carries many Start event not in stack, ignoring warnings.
  • Fix. The replay also runs under torch.compiler.set_stance("force_eager"), so compiled regions inside the op run eagerly and the enclosing compile's events stay intact. The estimator is after eager kernel times anyway.
  • Scope. Only the estimator's replay changes: the stance is scoped to the replayed call, whose output is discarded, and the real forward still runs the op's compiled region. An op with a compiled region is now timed eagerly; where its eager path cannot run at that shape (an eager flex_attention materializes the full score matrix, which does not fit at long sequence lengths), the replay error is caught as before and the op keeps its analytical cost. Ops without a compiled region replay exactly as before.

test_replay_runs_compiled_regions_eagerly_inside_the_enclosing_compile replays a torch.compiled op inside a pt2 compile event and checks that the event survives; it fails without this change, with the event stack empty.

cenzhiyao added 2 commits November 27, 2026 17:20
…n has no os.memfd_create

A Python built against a glibc older than 2.27, as conda builds on its 2.17 sysroot, leaves out os.memfd_create
even though the running glibc and kernel provide it; per-host weight sharing then failed on its first region.
dynamo.disable alone lets a torch.compiled region inside the replayed op compile, and that compile resets the chromium event log on exit, clearing the enclosing Inductor compile pt2 events; _compile_fx_inner then fails with inductor_compile not logged to pt2_compile_events.
Base automatically changed from fix/memfd-without-os-binding to main November 28, 2026 04:31

@jiahy0825 jiahy0825 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@jiahy0825
jiahy0825 merged commit c1959c8 into main Nov 28, 2026
1 check passed
@jiahy0825
jiahy0825 deleted the fix/replay-without-nested-compile branch November 28, 2026 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants