Skip to content

[graph_trainer] Fix precompile pickle failure with FlexAttention BlockMask - #3428

Merged
bobrenjc93 merged 1 commit into
mainfrom
fix-precompile-pickle-blockmask
Jun 9, 2026
Merged

bobrenjc93 merged 1 commit into
mainfrom
fix-precompile-pickle-blockmask

Conversation

@bobrenjc93

@bobrenjc93 bobrenjc93 commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fix TypeError: cannot pickle code objects when precompiling models with FlexAttention (e.g. Llama3-70B with attn_backend="flex").

Root cause

Two PRs combined to create this bug:

  1. [graph_trainer] Add CooR precompile support for DeepSeek V3 #2916 ("Add CooR precompile support for DeepSeek V3") hoisted BlockMask into extra_kwargs["attention_masks"] so FlexAttention masks flow through minimal_fx_tracer as kwargs. This was fine at the time because user_inputs_spec was not serialized.

  2. Support kwargs in minimal_fx_tracer #3262 ("Support kwargs in minimal_fx_tracer") added user_inputs_spec to PrecompiledFxTraceArtifact for optional runtime validation. This caused pickle.dumps(artifact) to serialize the TreeSpec, which now contained a _MaskModWrapper from the BlockMask pytree context. _MaskModWrapper holds the mask_mod closure (a local function from get_causal_mask_mod), which Python's pickle cannot serialize.

Neither PR broke things on its own — #2916 put BlockMask in the kwargs, and #3262 started serializing the kwargs' TreeSpec.

Fix

Remove user_inputs_spec from PrecompiledFxTraceArtifact. The mask_mod is already compiled into standalone Inductor HOPs (AOTCompiledArtifact) baked into serialized_gm by the regional inductor pass, so it does not need to be preserved in the artifact metadata. user_inputs_spec is only used for an optional runtime validation check in run_traced() (disabled by default), so omitting it is safe.

Test plan

  • New unit test test_artifact_pickle_with_blockmask_treespec — creates a TracedResult with an unpicklable BlockMask TreeSpec, verifies from_traced_result + pickle.dumps succeeds
  • pytest torchtitan/experiments/graph_trainer/tests/test_precompile.py — all 20 tests pass

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 26, 2026
@bobrenjc93
bobrenjc93 force-pushed the fix-precompile-pickle-blockmask branch from 343dc35 to c600142 Compare May 26, 2026 14:39
@bobrenjc93
bobrenjc93 requested a review from IvanKobzarev May 26, 2026 14:40
@bobrenjc93
bobrenjc93 marked this pull request as ready for review May 26, 2026 14:47
@bobrenjc93
bobrenjc93 force-pushed the fix-precompile-pickle-blockmask branch from c600142 to 54bec7d Compare May 26, 2026 14:48

@IvanKobzarev IvanKobzarev left a comment

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.

Thanks. Verified on single node runs with precompile

@SherlockNoMad SherlockNoMad added the ciflow/h100.8 Trigger H100.8 CI label Jun 2, 2026
@SherlockNoMad

Copy link
Copy Markdown
Contributor

can you check if the disabled precompile test in test_bitwise_numercis.py can pass now?

…kMask

When precompiling a model that uses FlexAttention (e.g. Llama3-70B with
attn_backend="flex"), `pickle.dumps(artifact)` fails with
`TypeError: cannot pickle code objects` at precompile.py:241.

Root cause: `PrecompiledFxTraceArtifact` serialized `user_inputs_spec`,
a pytree TreeSpec that captures the structure of the tracer's input args.
When those args include a `BlockMask`, the TreeSpec stores a
`_MaskModWrapper` in its context — this wrapper holds the mask_mod
function (a local closure from `get_causal_mask_mod`), which Python's
pickle cannot serialize.

The mask_mod is already compiled into standalone Inductor HOPs
(AOTCompiledArtifact) that are baked into `serialized_gm` by the
regional_inductor pass, so it does not need to be preserved in the
artifact metadata. The `user_inputs_spec` itself is only consumed by
an optional runtime validation check in `run_traced()` (disabled by
default), so omitting it from the serialized artifact is safe.

Fix: remove `user_inputs_spec` from `PrecompiledFxTraceArtifact` and
supply a dummy TreeSpec on deserialization.
@bobrenjc93
bobrenjc93 force-pushed the fix-precompile-pickle-blockmask branch from 54bec7d to b65d026 Compare June 9, 2026 20:44
@bobrenjc93
bobrenjc93 merged commit a977676 into main Jun 9, 2026
14 of 18 checks passed
wwwjn pushed a commit that referenced this pull request Jun 10, 2026
…kMask (#3428)

## Summary

Fix `TypeError: cannot pickle code objects` when precompiling models
with FlexAttention (e.g. Llama3-70B with `attn_backend="flex"`).

### Root cause

Two PRs combined to create this bug:

1. **#2916** ("Add CooR precompile support for DeepSeek V3") hoisted
`BlockMask` into `extra_kwargs["attention_masks"]` so FlexAttention
masks flow through `minimal_fx_tracer` as kwargs. This was fine at the
time because `user_inputs_spec` was not serialized.

2. **#3262** ("Support kwargs in minimal_fx_tracer") added
`user_inputs_spec` to `PrecompiledFxTraceArtifact` for optional runtime
validation. This caused `pickle.dumps(artifact)` to serialize the
`TreeSpec`, which now contained a `_MaskModWrapper` from the `BlockMask`
pytree context. `_MaskModWrapper` holds the `mask_mod` closure (a local
function from `get_causal_mask_mod`), which Python's pickle cannot
serialize.

Neither PR broke things on its own — #2916 put `BlockMask` in the
kwargs, and #3262 started serializing the kwargs' `TreeSpec`.

### Fix

Remove `user_inputs_spec` from `PrecompiledFxTraceArtifact`. The
`mask_mod` is already compiled into standalone Inductor HOPs
(`AOTCompiledArtifact`) baked into `serialized_gm` by the regional
inductor pass, so it does not need to be preserved in the artifact
metadata. `user_inputs_spec` is only used for an optional runtime
validation check in `run_traced()` (disabled by default), so omitting it
is safe.

## Test plan

- [x] New unit test `test_artifact_pickle_with_blockmask_treespec` —
creates a `TracedResult` with an unpicklable BlockMask `TreeSpec`,
verifies `from_traced_result` + `pickle.dumps` succeeds
- [x] `pytest
torchtitan/experiments/graph_trainer/tests/test_precompile.py` — all 20
tests pass
saforem2 pushed a commit to saforem2/torchtitan that referenced this pull request Jun 10, 2026
…kMask (pytorch#3428)

## Summary

Fix `TypeError: cannot pickle code objects` when precompiling models
with FlexAttention (e.g. Llama3-70B with `attn_backend="flex"`).

### Root cause

Two PRs combined to create this bug:

1. **pytorch#2916** ("Add CooR precompile support for DeepSeek V3") hoisted
`BlockMask` into `extra_kwargs["attention_masks"]` so FlexAttention
masks flow through `minimal_fx_tracer` as kwargs. This was fine at the
time because `user_inputs_spec` was not serialized.

2. **pytorch#3262** ("Support kwargs in minimal_fx_tracer") added
`user_inputs_spec` to `PrecompiledFxTraceArtifact` for optional runtime
validation. This caused `pickle.dumps(artifact)` to serialize the
`TreeSpec`, which now contained a `_MaskModWrapper` from the `BlockMask`
pytree context. `_MaskModWrapper` holds the `mask_mod` closure (a local
function from `get_causal_mask_mod`), which Python's pickle cannot
serialize.

Neither PR broke things on its own — pytorch#2916 put `BlockMask` in the
kwargs, and pytorch#3262 started serializing the kwargs' `TreeSpec`.

### Fix

Remove `user_inputs_spec` from `PrecompiledFxTraceArtifact`. The
`mask_mod` is already compiled into standalone Inductor HOPs
(`AOTCompiledArtifact`) baked into `serialized_gm` by the regional
inductor pass, so it does not need to be preserved in the artifact
metadata. `user_inputs_spec` is only used for an optional runtime
validation check in `run_traced()` (disabled by default), so omitting it
is safe.

## Test plan

- [x] New unit test `test_artifact_pickle_with_blockmask_treespec` —
creates a `TracedResult` with an unpicklable BlockMask `TreeSpec`,
verifies `from_traced_result` + `pickle.dumps` succeeds
- [x] `pytest
torchtitan/experiments/graph_trainer/tests/test_precompile.py` — all 20
tests pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100.8 Trigger H100.8 CI ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants