Skip to content

[Override] Make FusedSwiGLU checkpoints interoperable with stock FeedForward - #3638

Merged
fegin merged 10 commits into
mainfrom
gh/fegin/138/head
Jun 15, 2026
Merged

fegin merged 10 commits into
mainfrom
gh/fegin/138/head

Conversation

@fegin

@fegin fegin commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

FusedSwiGLU now checkpoints in the stock FeedForward layout (w1.weight/w3.weight) via register_state_dict_post_hook / register_load_state_dict_pre_hook, so its checkpoints interoperate with the non-fused module and the HF adapter (a native w13 key is still accepted). This is possible now that we removed the usage of get_model_state_dict/set_model_state_dict. Resolves #3569.

Adds CPU unit tests for both load directions and native-w13 back-compat.

fegin added 2 commits June 11, 2026 11:39
[ghstack-poisoned]
[ghstack-poisoned]
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jun 11, 2026

@tianyu-l tianyu-l 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.

Comment thread torchtitan/config/OVERRIDE.md Resolved
Comment thread torchtitan/overrides/fused_swiglu.py Resolved
Comment thread tests/unit_tests/test_fused_swiglu.py Resolved
fegin added a commit that referenced this pull request Jun 12, 2026
…ict APIs (#3623)

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

**Summary:**
Same as #2441 but with new
implementation to be compatiblewith the latest OptimizerContainer

**Verification:**
  | Check | Result |
  |---|---|
| Checkpoint compat, FSDP (old saves -> new loads, resume to step 100) |
PASS - bit-identical all 100
  steps |
| Checkpoint compat, PP=2 (old saves -> new loads) | PASS -
bit-identical |
| loss_compare, FSDP (old vs new, 100 steps, seed checkpoint) | PASS -
identical loss |
| CPU unit tests (lr_scheduler, optimizer_param_groups, state_dict_keys,
checkpoint) | 51 passed |
| Full unit suite | 411 passed (2 unrelated tokenizer-download failures)
|
  | Lint (ufmt, flake8, pydoclint) | pass |
saforem2 pushed a commit to saforem2/torchtitan that referenced this pull request Jun 12, 2026
…ict APIs (pytorch#3623)

Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.15.0)
(oldest at bottom):
* pytorch#3638
* __->__ pytorch#3623

**Summary:**
Same as pytorch#2441 but with new
implementation to be compatiblewith the latest OptimizerContainer

**Verification:**
  | Check | Result |
  |---|---|
| Checkpoint compat, FSDP (old saves -> new loads, resume to step 100) |
PASS - bit-identical all 100
  steps |
| Checkpoint compat, PP=2 (old saves -> new loads) | PASS -
bit-identical |
| loss_compare, FSDP (old vs new, 100 steps, seed checkpoint) | PASS -
identical loss |
| CPU unit tests (lr_scheduler, optimizer_param_groups, state_dict_keys,
checkpoint) | 51 passed |
| Full unit suite | 411 passed (2 unrelated tokenizer-download failures)
|
  | Lint (ufmt, flake8, pydoclint) | pass |
"""
w13 = state_dict.pop(f"{prefix}w13")
state_dict[f"{prefix}w1.weight"] = w13[:, 0].contiguous()
state_dict[f"{prefix}w3.weight"] = w13[:, 1].contiguous()

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.

This seems to conflict with ModelWrapper's caching behavior for model.state_dict() at init time.

The _split_w13_on_save hook's .contiguous() creates copies that are detached from the parameter storage. After optimizer steps update w13.weight in‑place, the cache holds stale values, causing DCP to save incorrect weights.

I have attempted to address this issue in PR #3656 . Could you please take a look and let me know if anything looks off?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Replied in your PR.

fegin added 2 commits June 15, 2026 11:26
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Jun 15, 2026
…Forward

FusedSwiGLU now checkpoints in the stock FeedForward layout (w1.weight/w3.weight) via register_state_dict_post_hook / register_load_state_dict_pre_hook, so its checkpoints interoperate with the non-fused module and the HF state-dict adapter (a native w13 key is still accepted). This is possible now that we removed the usage of get_model_state_dict/set_model_state_dict.

Also moves the override design doc to torchtitan/overrides/README.md for discoverability. Adds CPU unit tests for both load directions, native-w13 back-compat, and HF-adapter interop.

ghstack-source-id: 3efb0ec
Pull-Request: #3638
fegin added 2 commits June 15, 2026 11:44
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Jun 15, 2026
…Forward

FusedSwiGLU now checkpoints in the stock FeedForward layout (w1.weight/w3.weight) via register_state_dict_post_hook / register_load_state_dict_pre_hook, so its checkpoints interoperate with the non-fused module and the HF state-dict adapter (a native w13 key is still accepted). This is possible now that we removed the usage of get_model_state_dict/set_model_state_dict.

Also moves the override design doc to torchtitan/overrides/README.md for discoverability. Adds CPU unit tests for both load directions, native-w13 back-compat, and HF-adapter interop.

ghstack-source-id: cfe40dd
Pull-Request: #3638
fegin added 2 commits June 15, 2026 12:26
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Jun 15, 2026
…Forward

FusedSwiGLU now checkpoints in the stock FeedForward layout (w1.weight/w3.weight) via register_state_dict_post_hook / register_load_state_dict_pre_hook, so its checkpoints interoperate with the non-fused module and the HF state-dict adapter (a native w13 key is still accepted). This is possible now that we removed the usage of get_model_state_dict/set_model_state_dict.

Also moves the override design doc to torchtitan/overrides/README.md for discoverability. Adds CPU unit tests for both load directions, native-w13 back-compat, and HF-adapter interop.

ghstack-source-id: eac80aa
Pull-Request: #3638
@fegin

fegin commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

@sdmyzlp's #3656 has tackled the FuseQKV. So we won't do it in this PR.

fegin added 2 commits June 15, 2026 14:30
[ghstack-poisoned]
[ghstack-poisoned]
fegin added a commit that referenced this pull request Jun 15, 2026
…Forward

FusedSwiGLU now checkpoints in the stock FeedForward layout (w1.weight/w3.weight) via register_state_dict_post_hook / register_load_state_dict_pre_hook, so its checkpoints interoperate with the non-fused module and the HF state-dict adapter (a native w13 key is still accepted). This is possible now that we removed the usage of get_model_state_dict/set_model_state_dict.

Also moves the override design doc to torchtitan/overrides/README.md for discoverability. Adds CPU unit tests for both load directions, native-w13 back-compat, and HF-adapter interop.

ghstack-source-id: e069c42
Pull-Request: #3638
@fegin
fegin changed the base branch from gh/fegin/138/base to main June 15, 2026 22:12
fegin added a commit that referenced this pull request Jun 15, 2026
…3675)

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

**TLDR**
We need cached state_dict for the pinned memory copy. But state_dict
hooks can invalidate the cache. This PR fixes by using `copy_` for those
tensors that storages change.

**Summary**
ModelWrapper caches the flattened model state dict so async DCP staging
reuses its pinned host buffers (the stager keys them by source storage).
A module state_dict hook that emits a freshly allocated tensor (e.g.
splitting a fused parameter into the stock layout) is disconnected from
the parameter, so once training updates the parameter the cached copy
goes stale and the checkpoint saves init-time values.

state_dict() now refreshes only those hook-produced tensors in place, so
their storage stays stable (pinned buffers stay reusable) while values
stay current. Parameter-backed tensors share storage with the live
parameter and need no copy; models without such hooks return the cache
unchanged.

Adds CPU unit tests for stable storage plus in-place refresh.
@fegin
fegin merged commit a6815bb into main Jun 15, 2026
9 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

checkpoint interoperability issue between fused and non-fused Module definition

3 participants