Repository navigation
Initialize packed experts as the unpacked experts they stack - #787
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests.
... and 6 files with indirect coverage changes 🚀 New features to boost your workflow:
|
5 of 7 tasks
amazloumi
requested changes
Dec 6, 2026
amazloumi
left a comment
Member
There was a problem hiding this comment.
Thanks, Packed experts now start with exactly the unpacked experts' weights and down_w gets the residual std; non-packed models initialize bit-identically to main, and the tensor-parallel exception behaves as documented on GPU. just the test covering comment below and it will be good to go.
Merged
5 of 7 tasks
This was referenced Dec 6, 2026
amazloumi
approved these changes
Dec 6, 2026
amazloumi
left a comment
Member
There was a problem hiding this comment.
Approve. The sharded test now covers every packed weight, which settles the one point from the last review, and the head passes the full test gate merged with current main.
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
#779: packed experts skip the residual init scale on
down_w, and from one seed get other weights than unpacked expertsYasin filed the issue, with the analysis and a CPU reproduction.
init_weightspicked residual output projections by name (o_proj.weight,down_proj.weight), so a packed layer'sdown_wwas drawn atinit_std, sqrt(2 * n_layers) times theinit_std / sqrt(2 * n_layers)each unpackeddown_projgets (2x at 2 layers, 8x at 32). It also drewup_w,down_wandgate_wwhole and before the router, sincenamed_parameterslists theMoEMLP's own parameters before its submodules', so each MoE layer's router, experts and shared expert landed on other parts of the random stream than in the unpacked layout.init_weightsnow leaves a packed layer's expert weights out of its main pass and draws them right after the layer's last parameter, where the unpacked layout'sexpertscome: after the router and the shared expert. The new_init_packed_expertsdraws them one expert at a time, each projection in the order an unpacked expert holds them (gate_projunder SwiGLU, thenup_projanddown_proj), as that expert's(out, in)weight, stored transposed, withdown_wat the residual std. From one seed a packed model now starts with exactly the weights of the unpacked one: eachgate_w[e],up_w[e]anddown_w[e]equals the unpacked expert's weight transposed, and the routers, shared experts and all other parameters are equal. This holds for both routers, SwiGLU and standard experts, shared experts,moe_frequency > 1, MoT's per-modality MoE layers, pipeline stage modules, and meta-device builds that runto_emptyand theninit_weights_and_freqs.build_parallel_modeland the VLM build apply FSDP2 to the meta-device model before init, sinceinit_distributedgives every tensor-parallel mesh adp_sharddimension (of size 1 without data parallelism). Each packed weight is then a DTensor and cannot be written one expert at a time, so each is drawn whole at its std:down_wgets the residual std there too (main drew it atinit_std), and the router and shared expert still come first.ModuleListof MLPs, whosedown_proj.weightalready takes the residual scale, so MoMa has no packed layout to fix.docs/architecture/model.md(Weight initialization),docs/moe/capacity-and-dispatch.md(Packed experts) and themoe_packed_expertsrow indocs/configuration/config-sections.md.codebase-map.json: the two new helpers ininit.py, the new test class, and thetest_model.pycount (80 to 86).A decision you can reverse: the same weights from one seed. The issue's second half asks that packed experts get the unpacked experts' weights from one seed. This branch does it because it is cheap and exact: per expert, one
normal_per projection into a scratch tensor the size of one expert's projection, then a transposed copy, which gives bit-identical weights (checked on CPU). It changes the order of a packed layer's draws, so a packed run started from a seed gets other router, expert and shared-expert values than on main; the scale fix alone changes every packed run'sdown_wanyway, and resumed runs are not affected. To fix only the scale and keep main's draws instead, drop_init_packed_expertsand the reordering, and let main's loop draw a parameter whose name ends in.down_wat the residual std; the two layouts then start from the same distributions but not the same values, and #470's same-seed comparison has to copy weights from one layout into the other.Behavior a user will notice:
down_wsqrt(2 * n_layers) times smaller than on main, as unpacked runs always did.moe_packed_expertschanges the expert kernels, not the starting point. Muon gives packed MoE experts to AdamW, unpacked ones to Newton-Schulz #470's acceptance can compare the layouts directly.computed_expert_ffn_hidden_dimis), so drawing it expert by expert takes as many draws as drawing it whole. Under tensor parallelism each packed weight is drawn whole, as on main, only after the router and shared expert; DTensor's random ops advance the generator by a tensor's full size whatever the order, so parameters outside the MoE layers keep main's values there too (checked on CPU with gloo, not on a GPU). Runs resumed from a checkpoint are not affected.Testing
uv run ruff check .passesuv run ruff format --check .passesuv run pyright kempnerforge/passes (0 errors)uv run pytest tests/unit/ -v --timeout=60passes, apart from failures this CPU batch node causes, not the branch: at 5594867, 6645 passed, 14 skipped, 39 failed. 38 aretest_vlm_dataset.py(35) andtest_video_dataset.py(3), which need the Hugging Facegpt2files the offline test cache no longer holds (LocalEntryNotFoundError). The other istest_hooks.py::TestActivationStoreParallel::test_whole_block_outputs_are_gathered_to_the_full_tensor[dp_shard2_tp2], where a rank hitOSError: [Errno 39] Directory not emptywhile removing its temporary directory on netscratch. That case passes alone on this branch (twice in a row); on main 3899909 it failed once with the same error and then passed. Runs of the whole file hit the same error in one or two of itsdp_shard2_tp2cases (41 passed, 2 failed in the last). Those tests build a dense model, whose init this branch leaves bit-identical.uv run torchrun --nproc_per_node=4 -m pytest tests/distributed/ -v: not run, it needs 4 GPUs and this round ran on CPU only. No distributed code changed; the DTensor path that FSDP2 leaves before init is covered by a two-rank gloo unit test. Training never runs the per-expert draws on a GPU: a build without tensor parallelism initializes on CPU, and a build with it takes the whole-tensor DTensor path. That path was not run on CUDA.uv run pytest tests/e2e/ --e2e -v: not run, it needs GPUs. Only initial values change.New tests in
tests/unit/test_model.py::TestPackedExpertInit: the std of each packed weight against the unpacked layout's (SwiGLU, gelu, sigmoid router, shared expert, MoE every other layer), the same weights from one seed for the same five cases, a meta-device build, MoT, a meta model left on the meta device, and a two-rank FSDP2-sharded meta build, which checks that each packed weight is a DTensor drawn at its std (down_wat the residual std,gate_wandup_watinit_std).Failing first, on main bdaf4a4 with this branch's tests at c9833a3 (13 of the 14 fail):
test_down_w_gets_the_residual_std(5 cases):AssertionError: layers.0.mlp.down_w,assert 0.02008027955889702 == 0.005 ± 2.5e-04(8 layers, so the residual std is 0.02 / 4).test_one_seed_gives_both_layouts_the_same_weights(5 cases),test_a_meta_build_gets_the_same_weightsandtest_mot_experts_get_the_same_weights:AssertionError: layers.0.mlp.up_w[0](layers.1.mlp.up_w[0]with MoE every other layer,layers.0.mlp.image.up_w[0]for MoT), the first packed slice that differs from the unpacked expert.test_a_sharded_meta_build_draws_down_w_at_the_residual_std: a rank raisesAssertionError: layers.0.mlp.down_w.test_a_meta_model_stays_on_the_meta_devicepasses on main too. It checks that init leaves a meta model on the meta device; it runs the early return for meta weights but would still pass without it, since drawing into meta tensors does nothing.Also checked:
gate_wat the residual std on the DTensor path fails onlytest_a_sharded_meta_build_draws_down_w_at_the_residual_std(AssertionError: layers.0.mlp.gate_w); before c9833a3 it passed all 14 cases.-Wbuilds the docs, andmap_counts.pyreports only main's known mismatches.The full unit suite ran at 5594867. The first review round added two commits:
80283cf9says that every tensor-parallel build draws packed experts whole (docs/architecture/model.md, themoe_packed_expertsrow indocs/configuration/config-sections.md, and the meta-build test's docstring), andb28793d1seeds the ranks of the two-rank FSDP2 test, so it is deterministic. At80283cf9ruff, ruff format, Sphinx-Wandtest_model.pypass (180 passed, 3 skipped). Atb28793d1ruff, ruff format, pyright (0 errors), Sphinx-Wand the map counts pass, andtest_model.pywithtest_moe.pypass (328 passed, 3 skipped). A third commit,c9833a33, has the two-rank test check every packed weight's std,gate_wincluded, after the review found that it checked onlydown_wandup_w. Atc9833a33ruff and ruff format pass, andtest_model.pypasses (180 passed, 3 skipped). The full suite was not rerun, since these commits change wording, one test seed and one test.The head merges cleanly with main bdaf4a4, and with each open PR: #788 (
afab6c32, and its main merge89d601c8), #789 (db64875f), #781 (506fb678), #776 (d4e569bf), #709 (991b0b6a) and #648 (dce0ff8e). Each mergedcodebase-map.jsonparses and shows only main's known count mismatches. #788 atafab6c32, #781, #776 and #648 conflict with main on their own; merged with this head directly they do not conflict.Contribution terms
Closes #779