Skip to content

perf(megamoe): read separate BF16 gate and up weights in backward - #25

Draft
yoyoyocmu wants to merge 2 commits into
codex/bf16-megamoe-situ-mainfrom
codex/bf16-megamoe-unpacked-weights
Draft

yoyoyocmu wants to merge 2 commits into
codex/bf16-megamoe-situ-mainfrom
codex/bf16-megamoe-unpacked-weights

Conversation

@yoyoyocmu

@yoyoyocmu yoyoyocmu commented Oct 27, 2026 •

Copy link
Copy Markdown

Remove BF16 backward gate/up concatenation for merged-LoRA. An optional separate up-weight tensor supplies its TMA descriptor in the existing gate/up passes, preserving K accumulation order. Packed weights remain the default API.

Stacked on #24. Trainer companion: https://github.com/fw-ai/fireworks/pull/68491.

Primitive benchmark

B300, EP=1, 32,768 tokens, H=3584/I=3072, 128 experts/top-8, shared LoRA rank 32, BF16 SiTU beta=4/linear-beta=25. Timing: packed/separate/separate/packed blocks, each with 20 warmups and 41 measured iterations (82 samples per layout). Memory/RSS: separate fresh processes. CUDA host registrations: separate CUPTI callback runs. Excludes merge/router/latent/shared expert and inter-rank communication.

Measurement Packed backward Separate weights
Forward median 18.400 ms 18.789 ms
Backward median 41.399 ms 36.720 ms
Combined median 59.806 ms 55.459 ms
Peak GPU allocation 31.101 GiB 25.851 GiB
Peak GPU reservation 31.314 GiB 26.529 GiB
Peak process RSS 2.734 GiB 2.696 GiB
CUDA registered-host peak 1,179,665 bytes 1,179,665 bytes
Page-rounded registered-host peak 1,208,320 bytes 1,208,320 bytes

Backward improves 11.3%, combined 7.3%. Forward is unchanged in code but measured 2.1% slower; that observation is retained. The allocation reduction is exactly 5.25 GiB, the removed [128,6144,3584] BF16 copy. This does not establish an end-to-end trainer speedup. Host counters cover CUDA allocation/registration APIs, not arbitrary OS/RDMA pins; both runs reported zero accounting errors.

Validation

  • All 42 cases pass at EP=1 and on each EP=2 rank: both layouts, production linear-beta=25, shared/per-expert factors, uneven/empty/partially masked/all-masked inputs, AC off/on, and checkpoint-input offload. Repeated forwards are bitwise deterministic. Forward/input/router-score/all-factor gradient cosines strictly exceed 0.99999 against the matching BF16 grouped SiTU and EP owner-partial rounding reference. EP=2 minimum printed cosine: 0.999995800.
  • Real-dimension EP=1, 32K-token parity passes; minimum cosine 0.999992312 (input gradient), forward 1.0. No threshold or reference rounding was relaxed.
  • Fresh-cache audit: two initial modified-kernel variants; zero new variants or changed cubins at lengths 1,37,129,0,7.
  • Native CPU compilation covers 50 variants, including separate-weight SiTU/GeGLU/SwiGLU.
  • Local wheel deep_gemm-2.8.2+bc31d85-cp312-cp312-linux_x86_64.whl imports with both capability flags under Torch 2.11/CUDA 13.0. Production dependency-image packaging/qualification remains pending.

Trace and full-model evidence

Paired primitive traces and memory snapshots are captured. Correlation from CPU aten::cat to GPU launches identifies 16 backward copy kernels across two iterations (12.328 ms total) before, and zero after. Total GPU kernel count falls from 512 to 496. The rendered comparison is analysis/unpacked-trace-comparison.png in the local task bundle; it is included in the uploaded GCS bundle linked below.

Parent NCU: 86.52% forward tensor-pipeline utilization; backward long-scoreboard stalls 51.02% of average issue cycles, with gate/up activation loads among leading sites. This optimization removes the independently observed packing copy; it does not claim to remove kernel-internal stalls.

The real-checkpoint 20-step CP=2/EP=2, 1K-token rerun completed. Peak GPU allocation fell from the initial integration's 46.517 GiB to 44.213 GiB. DeepEP V2 baseline: 43.644 GiB, so the overall memory gate still fails. The new snapshot identifies a remaining 2.625 GiB forward interleave allocation at the recorded peak. The initial 64K run exposed shared-factor reductions reading unwritten capacity rows. The integration PR fixes this by clearing owned saved-x/grad-y tails before those reductions. All 46 cases pass at EP=1 and on both EP=2 ranks, including four poisoned-tail regressions that fail before the fix. The corrected real 64K comparison below completes all 20 updates. Primitive numbers above precede the correctness fix.

No new kernel launch, host scalar read, GPU fence, collective, environment variable, or checkpoint-format change. Existing dgrad accumulation order/barriers are preserved. Trainer overhead is a capability lookup plus contiguous views of existing tensors. Full-parameter backward retains its packed gradient storage. The long-context factor-gradient failure is fixed in the integration PR; the remaining gates below still apply.

Draft, not ready to merge: full-model CP/document-boundary and AC/offload matrix, full-model performance/memory gate, production dependency packaging, final 256K CP=8/EP=4 20-step comparison remain outstanding.

Compile: python tests/compile_mega_moe_compat.py --output-dir /tmp/megamoe-compat.

Numerical reproduction uses the trainer companion's train-firetitan-py/tests/kimi_k31/test_megamoe_situ_gpu.py at EP=1/2 with this build on PYTHONPATH.

Validated 64K full-model comparison

Existing k31_shrink YAML and real checkpoint; CP=2/EP=2, rank/alpha 32, full AC, fixed seeded synthetic next-token data. Both runs complete 20 optimizer updates. The MegaMoE run uses trainer 453c872aa9798e5ce7458fa223aadcff69d70725 and native bc31d854043e1c6c8d169509ae79bf66ee640c28, including the shared-factor capacity-tail fix and separate backward weights.

Measurement DeepEP V2 MegaMoE merged-LoRA
Forward median, CUDA 1628.353 ms 1594.431 ms
Backward median, CUDA 3401.938 ms 3419.437 ms
Combined median, wall 5033.691 ms 5014.404 ms
Peak GPU allocation 90.515 GiB 79.634 GiB
Peak GPU reservation 99.332 GiB 85.898 GiB
Peak process RSS 27.547 GiB 28.514 GiB
CUDA registered-host peak 16,164,957 bytes 2,164,761 bytes
Step-20 loss 12.540501 12.542526

Timings are rank-0 medians over steps 7–20; memory is maximum across ranks. Separate runs used free B300 GPU pairs on a shared host. Combined time is essentially unchanged; this is not the requested 1.5–2× speedup. GPU peak improves at 64K, while host RSS increases. CUDA host counters report zero accounting errors and exclude OS/RDMA pins. The earlier failed 64K run is retained as failure evidence and excluded from this table. Loss/latency/memory plot: analysis/cp2-65536-capacity-fixed-comparison.png in the local bundle. Final 256K CP=8/EP=4 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