Skip to content

Fix FFN depth-scaled initialization target - #5111

Merged
tianyu-l merged 4 commits into
pytorch:mainfrom
GodlyDonuts:fix/weight-init-fused-ffn
Oct 8, 2026
Merged

tianyu-l merged 4 commits into
pytorch:mainfrom
GodlyDonuts:fix/weight-init-fused-ffn

Conversation

@GodlyDonuts

@GodlyDonuts GodlyDonuts commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Depth-scale only the FFN output projection (w2), while keeping the gate/up projections (w1 and w3) on the regular initializer across the affected dense, shared-expert, and routed-expert paths.

The common FFN helpers now use w13_param_init and w2_param_init, which makes the initialization roles explicit and avoids carrying forward the old w2w3_param_init grouping.

This is an intentional change to initialization numerics for models that use depth-scaled output projections. It is not a regression introduced by the fused w13 representation; the older unfused implementation also depth-scaled w3. Existing checkpoints are unaffected, as are models such as Qwen3 that already initialize these projections uniformly.

depth_init remains model-specific. GPT-OSS uses a separate initialization scheme and is intentionally left for a follow-up rather than changing its numerics in this PR.

Changes

  • Use the regular initializer for both logical w13 projections (w1 and w3).
  • Apply the depth-scaled initializer only to w2.
  • Rename the helper arguments to w13_param_init and w2_param_init.
  • Fix the equivalent w3_EFD initialization in the affected routed-expert paths.
  • Update the corresponding dense and shared-expert model configurations.
  • Add regression coverage for the common helpers, a real Llama3 flavor, and routed experts.

Validation

The new routed-expert regression cases pass on this branch and fail against the original base, covering the initialization behavior this PR changes.

Passing:

  • formatting/lint checks
  • CPU unit tests
  • single-GPU unit tests
  • eight focused routed-expert regression cases

Because this changes initialization for from-scratch training, dense and MoE convergence validation is still pending.

@pytorch-bot pytorch-bot Bot added ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl labels Oct 7, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 7, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

@pytorch-bot

pytorch-bot Bot commented Oct 7, 2026

Copy link
Copy Markdown

The following ciflow label(s) have been added but CI has not been triggered yet because the workflows are awaiting approval:

  • ciflow/rl
  • ciflow/fake-pg

Once a maintainer approves the workflows (scroll to the bottom of the PR page), the corresponding CI jobs will be triggered automatically. Please ping one of the reviewers if you do not have access to approve and run workflows.

@tianyu-l

tianyu-l commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

@claude review this PR

@claude

claude Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @tianyu-l's task in 1m 21s —— View job


Review: Fix FFN depth-scaled initialization target

  • Read the PR diff
  • Check every caller of make_ffn_config / make_shared_expert_ffn_config, every w3_EFD init, and the fused w13 init paths
  • Post review

The change itself is correct. Every edited site now initializes the logical w3 slice of w13 (and w3_EFD) with the input-projection init, and keeps depth scaling on w2. I checked these paths:

  • w3_EFD for routed experts in DeepSeek V3, DeepSeek V4, Kimi K2.7 and Qwen3.5
  • dense FFNs in Llama3, DeepSeek V3/V4, Qwen3.5 and Muse Glimmer
  • shared experts in DeepSeek V3/V4
  • the hand-built fused paths in Qwen3.5 _shared_experts_config and DeepSeek V4 MTP

In DeepSeek V4 MTP and Qwen3.5, the depth_init local is still used for w2, so nothing is left dangling. Kimi K2.7 reuses deepseek_v3.build_mla_moe_layers, so its dense and shared FFNs are covered by the DSv3 edits. I'd still like to see the points below addressed before merge.

1. The PR description misstates where this behavior came from (please fix it)

The description says the fused w13 refactor caused this. It didn't. The pre-fusion Llama3 FeedForward.init_weights did the same thing on purpose:

nn.init.trunc_normal_(self.w1.weight, mean=0.0, std=0.02)
for linear in (self.w2, self.w3):
    nn.init.trunc_normal_(linear.weight, mean=0.0, std=init_std)

That is where the name w2w3_param_init comes from. This PR is not fixing a refactor regression. It changes the default init numerics for every depth-init model (Llama3, DSv3/V4, Kimi K2.7, Qwen3.5, Muse Glimmer). Training from scratch will diverge from current runs. Existing checkpoints are unaffected.

Under the repo's rules (Core Principle 5 and "Validating Numerics"), a computation change needs loss convergence evidence on a representative dataset (e.g. C4). At minimum that means before/after curves for Llama3 8B or the debug model, plus one MoE model such as DSv3 16B. The PR currently lists only the CPU unit test and CI. Please add the convergence results and update the description so it says this is an intentional numerics change.

2. The optional override has no in-tree user of its default

After this PR, every in-tree caller that passes a depth-scaled w2w3_param_init also passes w3_param_init=<linear init>. The remaining callers pass the same dict for both arguments: Qwen3, Kimi K3, the HF backend moe_replacement.py, and the tests. So the default w3_param_init = w2w3_param_init fallback exists only to keep the old, now-unwanted behavior. That leaves two problems:

  • w2w3_param_init is now misnamed, because in practice it only sets w2.
  • Any new model that copies the old call pattern silently gets the old init.

A cleaner API, following Principle 3 (reuse over duplication) and the config rule against None defaults for required fields:

  • Rename w2w3_param_init to w2_param_init (or out_param_init).
  • Initialize both gate and up slices from w1_param_init. Add an explicit w3_param_init only if a real caller needs a different init.

This touches about 15 call sites, mostly in tests (test_async_linear.py, test_tensor_parallel_feed_forward.py, test_fused_swiglu.py, test_lora.py, test_moe.py, test_tp_kv_heads_validation.py), plus Qwen3, Kimi K3 and moe_replacement.py. It's mechanical, and it removes the trap instead of documenting around it.

Fix this →

3. GPT-OSS is still inconsistent (could be a follow-up)

torchtitan/models/gpt_oss/flavors.py:149-161 applies depth_scaled_std to the whole routed-expert w13, both the gate and up slices. Under the rule this PR adopts (depth-scale only the projection that writes back into the residual stream), that is also wrong. If it's deliberately left out, please say so in the description or add a TODO. Principle 6 asks for all model variants to be audited.

4. Test coverage

  • test_make_ffn_config_uses_separate_w3_init only tests the helper's plumbing. It doesn't catch the actual regression, which is a flavor config that depth-scales w3. A small CPU test would: build a debug Llama3 (or DSv3) config, check that w13's two slices use the same param_init callables (or match in std), and check that w2 uses the depth-scaled one.
  • make_shared_expert_ffn_config has the same new code path and no test.
  • I couldn't run pytest tests/unit_tests/cpu/test_feed_forward.py here because the command was blocked by permissions, so please confirm it passes locally.

Nits

  • config_utils.py:276 and :301: if you keep the override, w3_param_init = w3_param_init or w2w3_param_init is simpler. Note that or also falls back on an empty dict {}, which is a falsy value.
  • The new make_ffn_config docstring is good. make_shared_expert_ffn_config should explain w1/w3/w2 the same way.

Summary

The fix does what it says. Before merge, it needs convergence evidence because it changes init numerics for every depth-init model, and a more accurate description of where the old behavior came from. I'd also recommend renaming w2w3_param_init instead of adding an override that every caller sets anyway, and handling or noting GPT-OSS.

@GodlyDonuts

Copy link
Copy Markdown
Contributor Author

oops, ill fix that real quick

@GodlyDonuts
GodlyDonuts force-pushed the fix/weight-init-fused-ffn branch from 882e1aa to 3ddb7ec Compare October 7, 2026 06:50
@GodlyDonuts
GodlyDonuts force-pushed the fix/weight-init-fused-ffn branch from d6668fe to 10503c1 Compare October 7, 2026 06:51
@GodlyDonuts

Copy link
Copy Markdown
Contributor Author

Cleaned up the API and added the missing tests :)

@GodlyDonuts

Copy link
Copy Markdown
Contributor Author

@claude review this PR

Comment on lines +272 to +273
``w1`` and ``w3`` are the gate/up projections and share
``w1_param_init``; ``w2`` is the residual output projection.

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.

should we rename to w13_param_init then?

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.

yeah thats probably a lot cleaner

@tianyu-l

tianyu-l commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

@claude review again

@claude

claude Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @tianyu-l's task in 1m 16s —— View job


Re-review: Fix FFN depth-scaled initialization target

  • Read the updated diff (git diff origin/main...HEAD)
  • Check that no w2w3_param_init callers or hand-built fused w13 init paths were missed
  • Check the new tests
  • Run CPU unit tests. Both pytest invocations were blocked by permissions here, so please confirm locally.

The code matches what my first review asked for. w2w3_param_init is now w2_param_init, the optional override is gone, and both w13 slices initialize from w1_param_init. I grepped the tree and found no remaining w2w3_param_init references. All hand-built fused paths now pass _LINEAR_INIT for both slices: Qwen3.5 _shared_experts_config (qwen3_5/flavors.py:145), DSv4 MTP (deepseek_v4/flavors.py:560) and HF moe_replacement.py:547. w3_EFD is no longer depth-scaled in DSv3, DSv4, Kimi K2.7 or Qwen3.5. The depth_init locals in Qwen3.5 and DSv4 MTP are still used for w2. Models that were already uniform (Qwen3, Kimi K3) only had the keyword renamed, so their behavior doesn't change.

1. Rename w1_param_init -> w13_param_init (agree with @tianyu-l)

w1_param_init now sets both gate and up, so the name is as misleading as w2w3_param_init was. Renaming it to w13_param_init matches the fused w13 attribute it configures. It's a mechanical rename across the same call sites this PR already touches, plus the two docstrings in config_utils.py.

Fix this →

2. Still open from the first review

  • Convergence evidence. This still changes default init numerics for every depth-init model (Llama3, DSv3/V4, Kimi K2.7, Qwen3.5, Muse Glimmer). The repo's "Validating Numerics" rule asks for loss curves on a representative dataset such as C4. That means at least one dense model (Llama3 debug or 8B) and one MoE model (e.g. DSv3 16B).
  • PR description is stale. It still describes the w3_param_init override, which no longer exists. It also still blames the fused w13 refactor. The pre-fusion Llama3 init_weights already depth-scaled w3 on purpose, so please describe this as an intentional numerics change.
  • GPT-OSS. gpt_oss/flavors.py:149-166 still applies depth_scaled_std to all of routed w13, both slices and their biases. Either fix it here or add a TODO and mention in the description that it's deliberately out of scope.

3. Tests

  • The new tests address the earlier gap. Both helpers are now covered, and test_llama3_depth_scales_only_ffn_output checks a real flavor config. Monkeypatching _LINEAR_INIT and _depth_init works because both are module globals that are looked up when _build_llama3_layers runs.
  • Routed experts have no equivalent check. A test that the DSv3 (or Qwen3.5) _depth_experts_init gives the same std to w1_EFD and w3_EFD would catch a regression in the four w3_EFD edits.
  • Lint nit: at test_feed_forward.py:~105, the monkeypatch.setattr(llama3_flavors, "_depth_init", lambda _layer_id: depth_init) call fits in 88 columns, so the formatter will probably collapse it onto one line. Please run pre-commit run --all-files.

Nit

  • The dense and shared helpers now tie w3 to w1, but make_routed_experts_config still takes separate w1_EFD and w3_EFD keys. That's fine as is, but the routed side can still diverge again if someone depth-scales only w3_EFD. If you want the two sides to match, you could drop w3_EFD and reuse w1_EFD. That can also be a follow-up.

Summary

The code changes are correct and the earlier API concerns are fixed. Before merge, it still needs the convergence evidence and an updated PR description, and the GPT-OSS case should be fixed or noted. I'd also take @tianyu-l's w13_param_init rename.

@GodlyDonuts

Copy link
Copy Markdown
Contributor Author

Renamed it to w13_param_init throughout and added routed-expert regression coverage. All eight focused cases pass (and fail on the original base), and formatting/lint is clean. Left GPT-OSS for a follow-up. This does change init numerics, so convergence validation is still pending.

w1_param_init=_LINEAR_INIT,
w2w3_param_init=_depth_init(layer_id),
w13_param_init=_LINEAR_INIT,
w2_param_init=_depth_init(layer_id),

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.

should we deprecate depth_init? I'm not sure if it's useful, do you have any idea?

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.

I would keep it, alteast for now and separate that decision from this fix. Removing depth scaling changes initialization numerics across several models and would need a closer look upon.

w1_param_init=_LINEAR_INIT,
w2w3_param_init=_LINEAR_INIT,
w13_param_init=_LINEAR_INIT,
w2_param_init=_LINEAR_INIT,

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.

e.g. qwen3 is not using depth init

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.

Yeah, Qwen3 already uses uniform initialization, so its behavior stays the exact same here. I would keep depth scaling model-specific for now, since removing it from the other models would need a closer look.

@GodlyDonuts

Copy link
Copy Markdown
Contributor Author

Fixed a CI error which was expecting exact SFT numerics from the old initialization.

@GodlyDonuts
GodlyDonuts requested a review from tianyu-l October 7, 2026 07:58
@tianyu-l

tianyu-l commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

@GodlyDonuts please wait until all CI finishes and push a single change to fix all the numerics

@GodlyDonuts

Copy link
Copy Markdown
Contributor Author

@GodlyDonuts please wait until all CI finishes and push a single change to fix all the numerics

Sorry! Won't happen again :)

@tianyu-l

tianyu-l commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

@GodlyDonuts
oh no worries -- could you update all the losses now?

@GodlyDonuts

Copy link
Copy Markdown
Contributor Author

Updated all five snapshots responsible for the six failing configurations.

@tianyu-l
tianyu-l merged commit 6a87591 into pytorch:main Oct 8, 2026
@acisseJZhong

acisseJZhong commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

looks like the real pg loss is not updated, therefore failing the CI. https://github.com/pytorch/torchtitan/actions/runs/37873979056/job/113638386726

codex tells me those are the missing updated files

tests/assets/losses/real_pg/a10g/sft.txt
tests/assets/losses/real_pg/a10g/llama3.txt
tests/assets/losses/real_pg/a10g/deepseek_v3.txt
tests/assets/losses/real_pg/a10g/qwen3.txt
tests/assets/losses/real_pg/a10g/gpt_oss.txt
tests/assets/losses/real_pg/a10g/muse_glimmer.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl 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