Skip to content

Skip MoMaFFN in the tensor-parallel plan, so no MoMa plan key goes unresolved - #777

Merged
amazloumi merged 4 commits into
mainfrom
fix/moma-ffn-skipped-in-tp-plan
Dec 6, 2026
Merged

amazloumi merged 4 commits into
mainfrom
fix/moma-ffn-skipped-in-tp-plan

Conversation

@Naeemkh

@Naeemkh Naeemkh commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Shard MoMa's expert groups under tensor parallelism #769, its interim part: under tensor parallelism, _build_mlp_tp_plan (kempnerforge/distributed/tensor_parallel.py) gave a MoMa block's mlp, a MoMaFFN, the dense MLP entries mlp.up_proj (column-parallel) and mlp.down_proj (row-parallel). A MoMaFFN keeps its experts under experts.<modality>.experts.<i> and has neither child, so for every MoMa block parallelize_module warned Parallelize plan key 'up_proj' could not be resolved: ... skipping this plan entry, the same for down_proj, and sharded nothing there.
  • _build_mlp_tp_plan now returns no entries for a MoMaFFN, as it already does for an MoEMLP. Its docstring says why: the experts sit in per-modality groups with no up_proj or down_proj of their own, and the groups stay whole on every TP rank. The new import adds no cycle: tensor_parallel.py already imports kempnerforge.model.moe, and loading that package runs kempnerforge/model/__init__.py, which imports transformer.py, which imports moma.py.
  • What a user notices: a MoMa run with tp > 1 no longer prints the warnings "Parallelize plan key 'up_proj' could not be resolved" and "Parallelize plan key 'down_proj' could not be resolved" at startup. parallelize_module issued both for every MoMa block, and Python shows each identical warning once, so every rank printed the pair once. Nothing else changes. The skipped entries matched nothing, so the same parameters are sharded as before (each block's four attention projections), and the model computes the same result.
  • Tests:
    • tests/unit/test_distributed.py: new TestBuildBlockTPPlan::test_moma_block_plan_has_only_the_attention_projections. A MoMaBlock's plan without SequenceParallel is exactly attention.q_proj, k_proj, v_proj and o_proj, and with SequenceParallel on it still has no mlp.* entry.
    • tests/unit/test_moma.py: new TestMoMaTensorParallel::test_the_attention_is_sharded_and_the_expert_groups_stay_whole, on 2 gloo CPU ranks with tp = 2 and tied embeddings (MoMa's only way to turn SequenceParallel off). apply_tensor_parallel on a 2-layer MoMa model raises no "could not be resolved" warning. The DTensor parameters are exactly the 8 attention projections (Q/K/V Shard(0), O Shard(1)), so the routers and every expert stay plain tensors. The logits and every parameter's full gradient match an unsharded copy. Before this, no unit test ran a MoMa model under tensor parallelism, although docs/configuration/validation-rules.md says it matches an unsharded model.
  • Docs: docs/distributed/tensor-parallelism.md (VLM architectures) now says that a MoMa block's plan holds only its attention projections. The page and docs/configuration/validation-rules.md already said that MoMa's expert groups stay whole on every TP rank, which is still exact.
  • codebase-map.json: an entry for tests/unit/test_moma.py, which had none; TestBuildBlockTPPlan(8), plus the two test_distributed.py classes the entry was missing (TestApplyTensorParallelSequenceParallel(3) and TestApplyFsdp2GuardedAttributes(3)), so its count is 45; and TestApplyTensorParallelSequenceParallel and TestMoMaTensorParallel in tensor_parallel.py's tested_by. kempnerforge/distributed/parallel.py now lists TestApplyFsdp2GuardedAttributes among its tests.

What remains of #769 is the full sharding of MoMa's expert groups:

  • Plan entries for every expert (gate_proj and up_proj column-parallel, down_proj row-parallel, with replicated input and output; the routers stay replicated), a model.computed_expert_ffn_hidden_dim % tp check beside the cross-attention one in JobConfig.validate, a parity test with Gumbel noise on, and the docs.
  • Two design calls. First, keep each expert's top-k_e choice identical on every TP rank: rely on the TP ranks' shared seed (seed + pp_rank, kempnerforge/distributed/setup.py:135) and pin it with a test, or draw the Gumbel noise once and broadcast it over the TP group. Second, run one all-reduce per expert (a row-parallel down_proj on each) or one per MoMaFFN (sum the partial expert outputs, then reduce once).

Testing

  • uv run ruff check . passes
  • uv run ruff format --check . passes
  • uv run pyright kempnerforge/ passes (0 errors)
  • uv run pytest tests/unit/ -v --timeout=60 passes at 67bd965 (b786c49 after it changes only a test and the map), apart from 48 failures this node also gives main, none in a file this branch touches (6568 passed, 14 skipped, 72 min): 38 in test_vlm_dataset.py (35) and test_video_dataset.py (3), which need the gpt2 tokenizer missing from this node's offline Hugging Face cache; 9 test_streaming_resume.py tests that ran out of time under load, 8 at the 60 s limit and 1 whose child process passed its own 50 s limit (test_streaming_resume.py's worker and multi-rank tests need more than the unit tier's 60 s on cluster CPU nodes #754, closed as not planned; the file passes alone at --timeout=300: 41 passed); and 1 test_convert_checkpoint.py case that hit the netscratch OSError: [Errno 39] Directory not empty while removing its temp directory (the file passes alone: 143 passed).
  • If distributed code changed: uv run torchrun --nproc_per_node=4 -m pytest tests/distributed/ -v: not run. tensor_parallel.py changed, but that suite needs GPUs and this branch was tested on CPU only. The new gloo test runs the changed plan on 2 CPU ranks instead.
  • If training loop / parallelism / optimizers changed: uv run pytest tests/e2e/ --e2e -v: not run, for the same reason (the e2e tests need GPUs).

Failing first, on main (d5befc5) with this branch's two test files:

  • test_moma_block_plan_has_only_the_attention_projections: AssertionError: assert {'attention.k...'mlp.up_proj'} == {'attention.k...ntion.v_proj'}, with 'mlp.up_proj' and 'mlp.down_proj' as the extra items
  • test_the_attention_is_sharded_and_the_expert_groups_stay_whole: AssertionError: ["Parallelize plan key 'up_proj' could not be resolved", "Parallelize plan key 'down_proj' could not be resolved", "Parallelize plan key 'up_proj' could not be resolved", "Parallelize plan key 'down_proj' could not be resolved"], raised on rank 0.
  • At b786c49, a mutant that skips MoMaFFN only without SequenceParallel fails the plan test: AssertionError: assert not ['mlp.up_proj', 'mlp.down_proj'].

Also checked:

Contribution terms

Part of #769: the full sharding of MoMa's expert groups, described above, remains.

@Naeemkh Naeemkh added the core Affecting core label Dec 5, 2026
@amazloumi
amazloumi self-requested a review December 5, 2026 16:50
@codecov

codecov Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
kempnerforge/distributed/tensor_parallel.py 97.61% <100.00%> (+0.02%) ⬆️

... and 6 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@amazloumi amazloumi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The plan now skips MoMaFFN like MoEMLP, the sharded parameters and their placements are unchanged from main at tp=2 and tp=4, the unresolved-key warnings are gone, and the new tests fail on main's code. just resolve the comment below and it will good to go.

@amazloumi amazloumi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approve. The docstring point is fixed, and on a merge with current main the sharded parameters and their placements still match main at tp=2 and tp=4, the unresolved-key warnings are gone, and the new tests pass and fail on main's code.

@amazloumi
amazloumi merged commit 681bbf8 into main Dec 6, 2026
15 checks passed
@amazloumi
amazloumi deleted the fix/moma-ffn-skipped-in-tp-plan branch December 6, 2026 01:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Affecting core

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants