Skip to content

Stage separate gate/up weights in BF16 MegaMoE forward - #26

Draft
yoyoyocmu wants to merge 1 commit into
codex/bf16-megamoe-unpacked-weightsfrom
codex/bf16-megamoe-separate-forward
Draft

yoyoyocmu wants to merge 1 commit into
codex/bf16-megamoe-unpacked-weightsfrom
codex/bf16-megamoe-separate-forward

Conversation

@yoyoyocmu

@yoyoyocmu yoyoyocmu commented Oct 27, 2026 •

Copy link
Copy Markdown

Allow BF16 MegaMoE forward to read separate contiguous gate and up weights. Eight-row TMA loads reconstruct the existing interleave directly in shared memory, removing the caller's packing allocation and copies while preserving MMA and SiTU rounding. The optional trailing l1_up_weights argument is capability-gated and rejects fused shared experts for this layout.

Stacked on #25. Trainer integration PR: https://github.com/fw-ai/fireworks/pull/68548.

B300, EP=1, 32,768 tokens, H=3584/I=3072/E=128/top-8/rank-32. Both arms include the parent separate-weight backward and the capacity-tail correctness fix. Alternating A/B/B/A blocks use 20 warmups and 41 samples each (82 samples per arm); memory uses separate fresh processes.

Metric Forward packing Separate forward weights
Forward (ms) 19.358 13.946
Backward (ms) 37.820 37.481
Combined (ms) 57.308 51.397
GPU allocated peak (GiB) 25.851 25.851
GPU reserved peak (GiB) 26.510 25.904
Process RSS peak (GiB) 2.714 2.700
CUDA-registered host peak (bytes) 1,179,665 1,179,665

Primitive scope excludes base-weight merging, router/latent/shared experts and communication. Forward is 28.0% lower and combined time 10.3% lower; this is not an end-to-end speedup claim. CUDA host accounting has zero errors and excludes OS/RDMA pins. Two-iteration traces show 16 forward copy kernels (13.137 ms total) before and zero after; total kernels fall from 500 to 484. NCU after: SM throughput 80.2%, tensor cycles 78.3%, DRAM active 31.5%; replay durations are excluded from timings.

Measured CUDA trace

Validation: 58 CPU tests; 62 CUDA compile variants; 23 new-layout EP1 GPU cases and 69 EP2 cases per rank across packed/backward-only/both-separate layouts. Real shrink dimensions pass the matching BF16 SiTU oracle: forward bitwise equal, dx cosine 0.999992312, router cosine 1, all six factors >=0.999999916. Repeated forwards are bitwise deterministic. Empty/odd/all-masked, shared factors, AC and input-offload cases pass. Fresh-cache length audit: two initial modified variants, zero additions or changed cubins for lengths 1,37,129,0,7.

Reproduce correctness in the trainer environment with this native build:

cd train-firetitan-py
python -m pytest tests/kimi_k31/test_megamoe_merged_lora.py tests/test_megamoe_layout.py -q
# Check nvidia-smi and select two free GPUs first.
CUDA_VISIBLE_DEVICES=<free_gpu_ids> torchrun --standalone --nproc-per-node=2 -m pytest tests/kimi_k31/test_megamoe_situ_gpu.py -vv

Review: no new host/device synchronization or collectives. The wrapper adds a capability check and keyword argument and removes stack/copy work; trace launch counts confirm fewer GPU launches. Native TMA requests increase within the existing kernel, with unchanged barrier byte counts, SMEM layout and accumulation order. No new environment variables or public API/schema changes.

Fresh full-model qualification (existing real k31_shrink YAML, rank/alpha32, full AC, CP2/EP2, 64K tokens):

Metric DeepEP V2 Combined MegaMoE changes
Forward (ms) 1628.353 1574.848
Backward (ms) 3401.938 3408.174
Combined wall (ms) 5033.691 4980.876
GPU allocated peak (GiB) 90.515 79.590
GPU reserved peak (GiB) 99.332 85.078
Host RSS peak (GiB) 27.547 27.760
CUDA-registered host peak (bytes) 16,164,957 2,164,761
Successful updates 20 20
Loss at step 20 12.540501 12.538519

Timings use rank-0 medians over steps7–20; peaks are maximum over ranks. This is ~1% full-step improvement and ~12% lower allocated peak, not the requested 1.5–2× training gain. Host RSS is slightly higher. Final 256K/CP8/EP4 remains unrun. A fresh 1K control of this forward optimization completes 20 steps in each arm, 1.034→1.001 s median combined CUDA time and 44.212→43.892 GiB allocated peak; DeepEP at 43.644 GiB is still lower.

EP4 qualification also passes all 69 cases on each of four ranks, minimum recorded cosine 0.999993627. Full-model cross-backend expert gradients reach ~0.9999893 and do not meet a universal all-parameter 0.99999 gate; residual/attention adapters are lower still. Isolated matching kernel-oracle checks pass, and no stronger full-model parity claim is made.

Remaining qualification is explicit: the real existing k31_shrink YAML completes 20 updates at 1K/CP2/EP2, but 43.892 GiB allocated peak remains above the 43.644 GiB DeepEP baseline. Full-model AC comparisons also show projection-adapter gradient cosines below 0.99999 on both backends; isolated expert/kernel checks pass. Final 256K/CP8/EP4, and complete end-to-end performance/memory acceptance remain pending. Evidence bundle: profile/k31-megamoe-merged-lora-20261027/ (uploaded; link below).

Native compile-only check: python tests/compile_mega_moe_compat.py --output-dir /tmp/mega-forward-compile (62 variants; requires nvcc/CUTLASS, no GPU).

Actual model-input operator check and fresh 64K captures

Captured all three routed MoE boundaries from a real CP2/EP2 native-MXFP4 model backward (documents 1/65/257, H=I=512, E=128, top-8, shared rank/alpha32 factors), then replayed each with the unchanged matching grouped reference and exactly the captured upstream adjoint. All three forwards are bitwise identical; minimum input-gradient cosine is 0.9999907997, minimum factor-gradient cosine 0.9999959057, and router gradients exceed 0.99999999999999. Every operator meets the strict >0.99999 gate. The capture wrapper preserves production aliasing and does not unpack saved tensors a second time. It synchronizes CPU copies and is numerical evidence only.

This does not replace or hide the reported full-model gradient differences across backends/AC settings. Fresh final-code 64K CP2/EP2 runs also completed six successful updates with step-5/6 traces and memory snapshots on both ranks (8 artifacts). The 20-step timing/loss comparison above remains the performance result; profiled timings are separate. Local report: analysis/cp-model-moe-boundary-comparison.json; trace inventory: analysis/cp2-65536-final-code-trace-inventory.json. Final CP8/EP4 256K remains pending.

Uploaded evidence

The completed local test, benchmark, NCU, trace and memory-snapshot bundle is uploaded to gs://fireworks-fine-tuning-profiles/kimi-k31/yued/20261027-megamoe-merged-lora/ (browse artifacts). bundle/analysis/artifact-manifest.json maps local evidence paths to GCS objects; transfer checksums and remote object sizes were verified. The required final 256K CP=8/EP=4 run still needs eight free GPUs and is not included.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants