Skip to content

feat(mega): add bf16 situ forward and backward - #24

Draft
yoyoyocmu wants to merge 1 commit into
mainfrom
codex/bf16-megamoe-situ-main
Draft

yoyoyocmu wants to merge 1 commit into
mainfrom
codex/bf16-megamoe-situ-main

Conversation

@yoyoyocmu

@yoyoyocmu yoyoyocmu commented Oct 27, 2026 •

Copy link
Copy Markdown

Kimi-K3.1's merged-LoRA expert path uses BF16 SiTU, but BF16 MegaMoE only implements SwiGLU/GeGLU. Add SiTU forward and input-gradient support to the existing MegaMoE kernels, with optional gate/up saturation and explicit FP32 rounding before BF16 stores.

This is a draft dependency for the trainer integration. It adds no environment variables and preserves the existing non-SiTU call signatures. SiTU requires post-down routing, unclamped activation, and fast_math=False; unsupported fused shared-expert and numerical-correction paths fail explicitly. The capability is exported from the compiled extension so an old binary cannot silently accept the new path.

Validation

  • Native extension builds; all 42 CPU CUDA-compatibility variants compile on current main (including eight new SiTU variants).
  • Trainer numerical matrix on two B300 GPUs: 17 cases pass on each rank. Covers odd/empty/all-masked inputs, per-expert and shared LoRA factors, full activation checkpointing and checkpoint-input offload. Forward/dX/router/factor cosines are strictly >0.99999 against the matching grouped BF16 reference; repeated forward is bitwise deterministic.
  • The same patch backported onto the trainer's pinned DeepGEMM revision passes EP=1 coverage and a fresh-cache audit: two modified kernels compile, then lengths 1, 37, 129, 0 and 7 add zero variants.
  • Both DeepEP V2 and MegaMoE complete 20 steps through the existing K3.1 shrink YAML, real packed checkpoint, rank-32 LoRA, full AC, CP=2/EP=2, 1K context. This is preliminary qualification, not the requested CP=8/EP=4 256K run.

Performance evidence and remaining gates

One B300, EP=1 primitive, 32,768 source tokens, H=3584/I=3072/E=128/top-8/rank-32, BF16 merged weights and SiTU beta=4/linear-beta=25; two warmups and five measured iterations. Excludes merge, router, latent/shared expert and cross-rank communication.

Measurement Initial SiTU implementation
Forward median 18.995 ms
Backward median 41.435 ms
Combined median 60.393 ms
Peak allocated GPU memory 31.101 GiB
Peak process RSS 2.730 GiB

NCU full capture with line information: native forward 15.296 ms, SM throughput 87.946%, tensor pipeline utilization 86.521%, achieved occupancy 23.041%, 128 registers/thread and 227,716 bytes shared memory/block. The wrapper also packs gate/up weights; trainer snapshots identify separate 2.82 GB forward and backward temporaries. The initial full trainer integration regresses warm latency and peak memory versus DeepEP at 1K; removing those temporaries is a separate optimization, and the performance gate is not passed.

CPU/fence review: SiTU adds compile-time-specialized device arithmetic and launch-argument validation, with no new tensor allocations, device-to-host reads, or synchronization in these native kernels. The trainer wrapper's existing routing-plan exchanges and packing remain visible in its traces.

Trace pictures, NCU reports, memory snapshots and the reproducible local harness are retained under profile/k31-megamoe-merged-lora-20261027/ in the development checkout. The completed artifact bundle is uploaded and verified. Pinned-memory counters in this image are inconsistent and are not accepted as peak-memory evidence. Backward source NCU capture is complete (51.02% long-scoreboard stalls). The isolated backward-copy optimization reports a 5.25 GiB allocation reduction and 6.8% combined primitive latency reduction at 32K tokens; its full-model qualification and final 256K run remain outstanding. A driver-level pinned-memory allocation/registration/free probe passes, but workload pinned peaks still need measurement.

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