Repository navigation
[precompile] Carry an eager backend's graph without re-tracing it at load - #194300
Closed
bobrenjc93 wants to merge 3 commits into
Closed
bobrenjc93 wants to merge 3 commits into
bobrenjc93 wants to merge 3 commits into
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/194300
Note: Links to docs will display an error until the docs builds have been completed. ❌ 23 New Failures, 2 Unrelated Failures, 1 Unclassified FailureAs of commit 96918ae with merge base 1b4e490 ( NEW FAILURES - The following jobs have failed:
UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:
FLAKY - The following job failed but was likely due to flakiness present on trunk:
BROKEN TRUNK - The following job failed but was present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This was referenced Aug 20, 2026
Closed
Closed
Contributor
Author
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
This was referenced Aug 21, 2026
This was referenced Aug 22, 2026
Closed
Contributor
Author
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
1 similar comment
Contributor
Author
|
@bobrenjc93 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
Contributor
Author
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.
Stack from ghstack (oldest at bottom):
An eager-backend subgraph ships as a pickled
GraphModule, andGraphModule.__reduce__deliberately serializes only the generated source,recovering the Graph at load by symbolically re-tracing that source. For an fx
graph that round trip is the documented BC guarantee. For a DYNAMO graph it is
lossy, and the interesting half of the loss is silent.
Three failure classes, all measured on
backend="eager", tracer="dynamo":A HOP explodes.
torch.cond,torch._higher_order_ops.while_loop,torch.vmapandnested_compile_regionall reject a Proxy operand, soprecompile.loadraisesAssertionError/TraceErrorfrom_deserialize_graph_module.An in-graph context manager is silently deleted.
_enter_autocast/_exit_autocastandtorch._C._set_grad_enabledtake no Proxy at all, sothe retrace EXECUTES them and records nothing: the graph goes 9 nodes to 7 and
the artifact serves fp32 where capture chose bf16, or returns a tensor carrying
a
SinBackward0from a region the user wrote underno_grad.The retrace's side effects escape onto the loader. Because
_set_grad_enabled(True)actually runs,precompile.load()called insidetorch.no_grad()returns with grad enabled.Only eager is affected: an inductor capture renders its subgraphs to source and
never goes through this pickle. Nor is it precompile-specific -- the same graph
through plain
CompilePackage+DiskDynamoStore+record_eager_backendfails the same way. And
backend="inductor"is not a workaround for such amodel: AOTAutogradCache's
check_node_saferefuses it at capture. The eagerpath has no equivalent gate, so it accepts a graph it cannot reload.
The fix is to stop re-deriving the Graph. Nothing reads
.graphon atop-level Dynamo graph --
_install_codesinstalls the value as a moduleglobal and calls it -- so
_SourceGraphModulerebuilds the module by exec'ingthe same generated source and simply has no Graph. Nested HOP bodies DO need
one, because some eager HOP implementations run their body through
fx.Interpreter; those go throughtorch.fx._graph_pickler.GraphPickler,which serializes a graph node by node with no retrace.
Serializing everything with
GraphPicklerwas the obvious alternative and isworse: it fails 8 tests in test_precompile.py (the
no_dispatch()deepcopy inrecord_artifact), grows the artifact 16-28%, and raisesRuntimeError: Cannot access data pointerat capture on a stocknn.MultiheadAttention, whosenode.meta["example_value"]reaches a metastorage through a weakref. The hybrid is size-neutral -- 90,589 chars against
the baseline's 90,645 on a 6-block net -- because the top-level graph is carried
as exactly the source the old reduction carried.
Two of the three methods added to
EagerCacheArtifactand_SourceGraphModulelook optional and are not. Without_SourceGraphModule.__reduce__, an artifact that is re-serialized after beingloaded once raises
PicklingErroron the exec'd forward (4 errors intest_package.py). Without
__deepcopy__, deepcopy falls through to__reduce__and rebuilds fake tensors under theno_dispatch()thatPrecompileContext.record_artifactcopies inside, which asserts inmeta_utils -- so the INSTALLED serving mode fails for every HOP graph, and only
installed-mode tests catch it.
Read-BC is automatic: bytes pickled by the old code carry the old recipe.
torch._dynamo.aot_compile'sforce_autograd_cachepath hands over aGraphModuleSerializableCallablerather than a bound forward and keeps theold lossy reduction; precompile never reaches it, and the pass-through branch
says so.
Test Plan:
test_aot_compile.py has 3 pre-existing failures in this environment
(test_aot_compile_with_aoti*,
fatal error: cuda.h: No such file or directory),identical with and without this change.
The new tests are a matrix over cond / while_loop / vmap / autocast /
checkpoint, each in both serving modes, asserting equality with
torch.compile(backend="eager")-- which for autocast is a dtype assertion,since
assertEqualcompares dtype. 10 of the 12 fail without this change;checkpoint is the exception and is there for the other half of the fix, since it
is the construct whose body is run through
fx.Interpreterand so the one thatproves a nested body must keep a real Graph. The grad-mode pair is separate
because it fails in two directions: the served output's
requires_grad, andtorch.is_grad_enabled()in the caller afterload().Also checked by hand, all serving with matching numerics: MultiheadAttention,
TransformerEncoderLayer, SDPA, flex_attention, a module with a buffer, a
closed-over global tensor, unbacked
.item(), a custom autograd.Function,dynamic shapes (including a shape never captured), and a training capture.
Authored with an AI assistant.
Differential Revision: D116865484