Repository navigation
[graph_trainer] Fix precompile pickle failure with FlexAttention BlockMask - #3428
Merged
Merged
Conversation
bobrenjc93
force-pushed
the
fix-precompile-pickle-blockmask
branch
from
May 26, 2026 14:39
343dc35 to
c600142
Compare
bobrenjc93
marked this pull request as ready for review
May 26, 2026 14:47
bobrenjc93
requested review from
SherlockNoMad,
aditvenk,
sanketpurandare,
tianyu-l,
xmfan and
yiming0416
as code owners
May 26, 2026 14:47
bobrenjc93
force-pushed
the
fix-precompile-pickle-blockmask
branch
from
May 26, 2026 14:48
c600142 to
54bec7d
Compare
IvanKobzarev
approved these changes
May 29, 2026
IvanKobzarev
left a comment
Contributor
There was a problem hiding this comment.
Thanks. Verified on single node runs with precompile
SherlockNoMad
approved these changes
Jun 2, 2026
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
force-pushed
the
fix-precompile-pickle-blockmask
branch
from
June 9, 2026 20:44
54bec7d to
b65d026
Compare
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
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.
Summary
Fix
TypeError: cannot pickle code objectswhen precompiling models with FlexAttention (e.g. Llama3-70B withattn_backend="flex").Root cause
Two PRs combined to create this bug:
[graph_trainer] Add CooR precompile support for DeepSeek V3 #2916 ("Add CooR precompile support for DeepSeek V3") hoisted
BlockMaskintoextra_kwargs["attention_masks"]so FlexAttention masks flow throughminimal_fx_traceras kwargs. This was fine at the time becauseuser_inputs_specwas not serialized.Support kwargs in minimal_fx_tracer #3262 ("Support kwargs in minimal_fx_tracer") added
user_inputs_spectoPrecompiledFxTraceArtifactfor optional runtime validation. This causedpickle.dumps(artifact)to serialize theTreeSpec, which now contained a_MaskModWrapperfrom theBlockMaskpytree context._MaskModWrapperholds themask_modclosure (a local function fromget_causal_mask_mod), which Python's pickle cannot serialize.Neither PR broke things on its own — #2916 put
BlockMaskin the kwargs, and #3262 started serializing the kwargs'TreeSpec.Fix
Remove
user_inputs_specfromPrecompiledFxTraceArtifact. Themask_modis already compiled into standalone Inductor HOPs (AOTCompiledArtifact) baked intoserialized_gmby the regional inductor pass, so it does not need to be preserved in the artifact metadata.user_inputs_specis only used for an optional runtime validation check inrun_traced()(disabled by default), so omitting it is safe.Test plan
test_artifact_pickle_with_blockmask_treespec— creates aTracedResultwith an unpicklable BlockMaskTreeSpec, verifiesfrom_traced_result+pickle.dumpssucceedspytest torchtitan/experiments/graph_trainer/tests/test_precompile.py— all 20 tests pass