Skip to content

[Inductor] Clear _original_gm in CompiledFxGraph.prepare_for_serialization - #182048

Closed
bobrenjc93 wants to merge 4 commits into
gh/bobrenjc93/887/basefrom
gh/bobrenjc93/887/head
Closed

bobrenjc93 wants to merge 4 commits into
gh/bobrenjc93/887/basefrom
gh/bobrenjc93/887/head

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented Apr 30, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

CompiledFxGraph stores _original_gm (a deepcopy of the pre-compiled FX
graph) when wrap_inductor_compiled_regions is enabled. When the compiled
graph contains higher-order ops with lifted buffers
(e.g. flex_attention with BlockMask mask_mod_other_buffers), serializing
_original_gm causes a crash during deserialization. The crash path:

pickle.loads(AOTCompiledArtifact data)
→ CompiledFxGraph._original_gm is reconstructed
→ GraphModule.reduce → reduce_graph_module
→ _deserialize_graph_module → KeepModules().trace()
→ flex_attention.call(... mask_mod_other_buffers=(Proxy,) ...)
→ validate_subgraph_args_types
→ AssertionError: Proxy is not Tensor/int/SymInt

This affects the AOTAutograd disk cache and torchtitan's graph_trainer
precompile path when FlexAttention uses a causal BlockMask (which adds
mask_mod closure tensors to mask_mod_other_buffers).

The fix: clear _original_gm in prepare_for_serialization() since we don't need
the original gm at runtime. NB: this is for the standalone compile node in the larger
fx graph. We will still have an "outer gm", it's just the inner standalone compile
nodes won't have the original gms, but that's fine, we just need the callable.

Authored with Claude

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

…ation

CompiledFxGraph stores _original_gm (a deepcopy of the pre-compiled FX
graph) when wrap_inductor_compiled_regions is enabled. This field exists
solely for FakeTensor shape inference via inductor_compiled_code_fake —
it is never needed at runtime for actual execution.

When the compiled graph contains higher-order ops with lifted buffers
(e.g. flex_attention with BlockMask mask_mod_other_buffers), serializing
_original_gm causes a crash during deserialization. The crash path:

  pickle.loads(AOTCompiledArtifact data)
  → CompiledFxGraph._original_gm is reconstructed
  → GraphModule.__reduce__ → reduce_graph_module
  → _deserialize_graph_module → KeepModules().trace()
  → flex_attention.__call__(... mask_mod_other_buffers=(Proxy,) ...)
  → validate_subgraph_args_types
  → AssertionError: Proxy is not Tensor/int/SymInt

This affects the AOTAutograd disk cache and torchtitan's graph_trainer
precompile path when FlexAttention uses a causal BlockMask (which adds
mask_mod closure tensors to mask_mod_other_buffers).

The fix: clear _original_gm in prepare_for_serialization(). The
inductor_compiled_code_fake handler already handles this case — it
raises a clear RuntimeError("... Recompile to restore") when
original_gm is None.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Apr 30, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/182048

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 42 Pending

As of commit 9718aee with merge base 00c683a (image):

NEW FAILURE - The following job has failed:

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot

pytorch-bot Bot commented Apr 30, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

…or_serialization"

CompiledFxGraph stores _original_gm (a deepcopy of the pre-compiled FX
graph) when wrap_inductor_compiled_regions is enabled. This field exists
solely for FakeTensor shape inference via inductor_compiled_code_fake —
it is never needed at runtime for actual execution.

When the compiled graph contains higher-order ops with lifted buffers
(e.g. flex_attention with BlockMask mask_mod_other_buffers), serializing
_original_gm causes a crash during deserialization. The crash path:

  pickle.loads(AOTCompiledArtifact data)
  → CompiledFxGraph._original_gm is reconstructed
  → GraphModule.__reduce__ → reduce_graph_module
  → _deserialize_graph_module → KeepModules().trace()
  → flex_attention.__call__(... mask_mod_other_buffers=(Proxy,) ...)
  → validate_subgraph_args_types
  → AssertionError: Proxy is not Tensor/int/SymInt

This affects the AOTAutograd disk cache and torchtitan's graph_trainer
precompile path when FlexAttention uses a causal BlockMask (which adds
mask_mod closure tensors to mask_mod_other_buffers).

The fix: clear _original_gm in prepare_for_serialization(). The
inductor_compiled_code_fake handler already handles this case — it
raises a clear RuntimeError("... Recompile to restore") when
original_gm is None.

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo

[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Apr 30, 2026
…ation

CompiledFxGraph stores _original_gm (a deepcopy of the pre-compiled FX
graph) when wrap_inductor_compiled_regions is enabled. This field exists
solely for FakeTensor shape inference via inductor_compiled_code_fake —
it is never needed at runtime for actual execution.

When the compiled graph contains higher-order ops with lifted buffers
(e.g. flex_attention with BlockMask mask_mod_other_buffers), serializing
_original_gm causes a crash during deserialization. The crash path:

  pickle.loads(AOTCompiledArtifact data)
  → CompiledFxGraph._original_gm is reconstructed
  → GraphModule.__reduce__ → reduce_graph_module
  → _deserialize_graph_module → KeepModules().trace()
  → flex_attention.__call__(... mask_mod_other_buffers=(Proxy,) ...)
  → validate_subgraph_args_types
  → AssertionError: Proxy is not Tensor/int/SymInt

This affects the AOTAutograd disk cache and torchtitan's graph_trainer
precompile path when FlexAttention uses a causal BlockMask (which adds
mask_mod closure tensors to mask_mod_other_buffers).

The fix: clear _original_gm in prepare_for_serialization(). The
inductor_compiled_code_fake handler already handles this case — it
raises a clear RuntimeError("... Recompile to restore") when
original_gm is None.

ghstack-source-id: c3f1a84
Pull Request resolved: #182048
@bobrenjc93
bobrenjc93 marked this pull request as ready for review April 30, 2026 17:20
@bobrenjc93 bobrenjc93 added ciflow/trunk Trigger trunk jobs on your pull request topic: not user facing topic category labels Apr 30, 2026
…or_serialization"


CompiledFxGraph stores _original_gm (a deepcopy of the pre-compiled FX
graph) when wrap_inductor_compiled_regions is enabled. This field exists
solely for FakeTensor shape inference via inductor_compiled_code_fake —
it is never needed at runtime for actual execution.

When the compiled graph contains higher-order ops with lifted buffers
(e.g. flex_attention with BlockMask mask_mod_other_buffers), serializing
_original_gm causes a crash during deserialization. The crash path:

  pickle.loads(AOTCompiledArtifact data)
  → CompiledFxGraph._original_gm is reconstructed
  → GraphModule.__reduce__ → reduce_graph_module
  → _deserialize_graph_module → KeepModules().trace()
  → flex_attention.__call__(... mask_mod_other_buffers=(Proxy,) ...)
  → validate_subgraph_args_types
  → AssertionError: Proxy is not Tensor/int/SymInt

This affects the AOTAutograd disk cache and torchtitan's graph_trainer
precompile path when FlexAttention uses a causal BlockMask (which adds
mask_mod closure tensors to mask_mod_other_buffers).

The fix: clear _original_gm in prepare_for_serialization() since we don't need the original gm at runtime

Authored with Claude

cc voznesenskym penguinwu EikanWang jgong5 Guobing-Chen XiaobingSuper zhuhaozhe blzheng wenzhe-nrv jiayisunx ipiszy kadeng muchulee8 amjames chauhang aakhundov coconutruben jataylo

[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request Apr 30, 2026
…ation

CompiledFxGraph stores _original_gm (a deepcopy of the pre-compiled FX
graph) when wrap_inductor_compiled_regions is enabled. This field exists
solely for FakeTensor shape inference via inductor_compiled_code_fake —
it is never needed at runtime for actual execution.

When the compiled graph contains higher-order ops with lifted buffers
(e.g. flex_attention with BlockMask mask_mod_other_buffers), serializing
_original_gm causes a crash during deserialization. The crash path:

  pickle.loads(AOTCompiledArtifact data)
  → CompiledFxGraph._original_gm is reconstructed
  → GraphModule.__reduce__ → reduce_graph_module
  → _deserialize_graph_module → KeepModules().trace()
  → flex_attention.__call__(... mask_mod_other_buffers=(Proxy,) ...)
  → validate_subgraph_args_types
  → AssertionError: Proxy is not Tensor/int/SymInt

This affects the AOTAutograd disk cache and torchtitan's graph_trainer
precompile path when FlexAttention uses a causal BlockMask (which adds
mask_mod closure tensors to mask_mod_other_buffers).

The fix: clear _original_gm in prepare_for_serialization(). The
inductor_compiled_code_fake handler already handles this case — it
raises a clear RuntimeError("... Recompile to restore") when
original_gm is None.

ghstack-source-id: 3acba3d
Pull Request resolved: #182048
self.recursively_apply_fns = None
self.compiled_fn_runner = None
# Note: _original_gm is already picklable (metadata stripped at creation)
self._original_gm = None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm, but someone who understand InductorOutputCode should review this

[ghstack-poisoned]
bobrenjc93 added a commit that referenced this pull request May 1, 2026
…ation

CompiledFxGraph stores _original_gm (a deepcopy of the pre-compiled FX
graph) when wrap_inductor_compiled_regions is enabled. This field exists
solely for FakeTensor shape inference via inductor_compiled_code_fake —
it is never needed at runtime for actual execution.

When the compiled graph contains higher-order ops with lifted buffers
(e.g. flex_attention with BlockMask mask_mod_other_buffers), serializing
_original_gm causes a crash during deserialization. The crash path:

  pickle.loads(AOTCompiledArtifact data)
  → CompiledFxGraph._original_gm is reconstructed
  → GraphModule.__reduce__ → reduce_graph_module
  → _deserialize_graph_module → KeepModules().trace()
  → flex_attention.__call__(... mask_mod_other_buffers=(Proxy,) ...)
  → validate_subgraph_args_types
  → AssertionError: Proxy is not Tensor/int/SymInt

This affects the AOTAutograd disk cache and torchtitan's graph_trainer
precompile path when FlexAttention uses a causal BlockMask (which adds
mask_mod closure tensors to mask_mod_other_buffers).

The fix: clear _original_gm in prepare_for_serialization(). The
inductor_compiled_code_fake handler already handles this case — it
raises a clear RuntimeError("... Recompile to restore") when
original_gm is None.

ghstack-source-id: df462db
Pull Request resolved: #182048
@bobrenjc93

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: trunk / linux-jammy-rocm-py3.10-mi355 / test (default, 1, 6, linux.rocm.gpu.gfx950.1)

Details for Dev Infra team Raised by workflow job

@bobrenjc93

Copy link
Copy Markdown
Contributor Author

Closing in favor of #182048

@bobrenjc93 bobrenjc93 closed this May 1, 2026
bobrenjc93 added a commit to pytorch/torchtitan that referenced this pull request May 3, 2026
…ts (#3178)

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0)
(oldest at bottom):
* __->__ #3178

The FlexAttention precompile tests were skipped with "numerics mismatch
— under investigation". The root cause was that
_run_steps_with_precompile
did not pass attention_masks to the model, while GraphTrainer's
post_dataloading_process generates a causal BlockMask for FlexAttention
models. This meant the two paths computed different attention patterns:
- GraphTrainer path: causal mask via BlockMask
- Precompile path: no mask (full attention)

Fix by adding _get_extra_kwargs() to the test base class, which
generates
the same BlockMask attention masks that post_dataloading_process
produces.
_run_steps_with_precompile now passes these masks and calls
maybe_register_blockmask_pytree_node() before tracing.

Depends on pytorch/pytorch#182048
@github-actions
github-actions Bot deleted the gh/bobrenjc93/887/head branch June 1, 2026 02:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request module: inductor topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants