Skip to content

perf: cache DeepGEMM FP8 weights within training steps - #2488

Open
ggboooy wants to merge 2 commits into
THUDM:mainfrom
ggboooy:perf/deepgemm-weight-cache
Open

ggboooy wants to merge 2 commits into
THUDM:mainfrom
ggboooy:perf/deepgemm-weight-cache

Conversation

@ggboooy

@ggboooy ggboooy commented Oct 21, 2026 •

Copy link
Copy Markdown

Problem

The DeepGEMM alignment forwards introduced in #2262 quantize the same BF16 weights again for each microbatch and activation-checkpoint recomputation, even while the weights are unchanged.

Change

Add --megatron-deepgemm-cache-weights (default: off) for the custom dense and grouped-MoE forward paths.

  • Quantize on first use within a training forward/backward schedule, then reuse FP8 weights and scales across microbatches and checkpoint recomputation.
  • Release the cache before optimizer updates, and on exceptional exits. Each logprob/value evaluation has an independent cache, so actor/reference/old-policy swaps do not retain old quantized weights.
  • Match checkpoint-created detached aliases by storage/layout/version. Ordinary in-place changes invalidate entries; optimizer writes through .data are handled by the schedule boundary.
  • Give cached MoE weights independent storage instead of aliasing the activation workspace.
  • Add English/Chinese documentation, a reproducible dense microbenchmark, and a 1-GPU test entry in the workflow template and generated workflow.

Activation quantization and backward rules are unchanged. The cache increases live GPU memory by approximately one byte per cached weight element plus scales.

Validation

Validated on patched upstream a4202e05 with an SM90 GPU, PyTorch 2.11.0+cu129, CUDA 12.9, and the deterministic SGLang/DeepGEMM stack (DeepGEMM b38a77cd).

PYTHONPATH=.:/path/to/Megatron-LM:/path/to/sglang/python \
  CUDA_VISIBLE_DEVICES=0 SGLANG_JIT_DEEPGEMM_PRECOMPILE=0 \
  python -m pytest tests/test_deepgemm_weight_cache.py
  • All 13 new cases pass. They cover disabled behavior, cache boundaries, mutations, detached aliases, nested scopes, failure cleanup, MoE workspace isolation, the UE8M0 quantize/requantize chain, and optimizer-boundary integration.
  • Real-GPU dense test: three microbatches plus three checkpoint recomputations reduce weight-quantizer calls from 6 to 1. Outputs, dgrad, and wgrad are bitwise equal with the cache on/off.
  • Real-GPU MoE test: two schedules, including an empty expert group and a .data weight update, reduce quantizer calls from 12 to 8; outputs are bitwise equal.
  • The 84 original dense/MoE regression cases from feat(glm5): align Megatron DeepEP training with SGLang rollout #2262 also pass against the patched main sources: 97 total passed.
  • Repository pre-commit hooks, workflow generation, and git diff --check pass.
  • The CPU checkpoint harness was updated for the new scope dependency; all 24 checkpoint tests passed in GitHub CI, covering absent/off/on cache flags.

Dense forward microbenchmark

PYTHONPATH=.:/path/to/Megatron-LM:/path/to/sglang/python \
  CUDA_VISIBLE_DEVICES=0 python tools/benchmark_deepgemm_weight_cache.py

Shape M=128, N=4096, K=4096; eight microbatches per fresh cache scope; three warmups; median of seven synchronized wall-clock measurements, alternating execution order.

Cache Median per 8-forward sequence Weight quantizations
Off 1.264 ms 8
On 1.020 ms 1

This is approximately 19.3% lower latency for this single-layer forward workload, with bitwise-equal output. Both paths measured 19,419,136 bytes of incremental peak allocated memory in this single-layer fixture; a full model retains multiple layers' cached weights and can use more memory. This does not claim end-to-end training speedup or a full-model peak-memory result.

Full-model Megatron training-step benchmark

Measured on 2026-10-22 (UTC+8), using PR commit 9639e597. This exercises the full model's production train_one_step: forward/loss, backward and checkpoint recomputation, gradient collectives, gradient processing, and a real distributed Adam update.

Workload and controls

  • Full 40-layer Qwen3.5-35B-A3B, 8 SM90 GPUs (runtime-reported 139.81 GiB each); TP=1, PP=1, CP=1, EP=8, expert TP=1.
  • Existing Qwen alignment adapter enabled identically for both settings: 100 TE linears, 90 GDN projections, and 40 grouped-MoE layers. DeepGEMM block-FP8 forward, BF16 backward, batch-invariant DeepGEMM enabled.
  • Fixed rollout replay via --load-debug-rollout-data; 32 samples (16 captured examples repeated twice), lengths 3,073–3,091, each with 1,024 response tokens. 98,624 non-padding input tokens per optimizer step, including prompt and response.
  • Global batch 32, dynamic token budget 4,096 per GPU, 4 microbatches per rank, full activation checkpointing with one layer per recompute unit.
  • Distributed Adam, learning rate 1e-6, betas (0.9, 0.98), weight decay 0.1, dropout zero, seed 1234. Recorded rewards are zero, so the nonzero updates in this fixture come from the entropy term (entropy_coef=0.001); this is a performance workload, not a reward-learning/convergence claim.
  • Four independent runs in off → on → on → off order, each reloading the same initial model and optimizer state. Six real training steps per run; discard the first two as warmup. Eight measured steps per setting, with no measured-step outlier removal.
  • The A/B configuration changes only --megatron-deepgemm-cache-weights. The same quantizer-call instrumentation is used on both sides.

Results

Step time is synchronized wall time around train_one_step, taking the maximum over the eight ranks. Throughput is 98,624 input tokens divided by median step time. Memory is the maximum per-rank PyTorch peak allocated memory over measured steps, not total device memory including NCCL/NVSHMEM allocations.

Metric Cache off Cache on Difference
Median training-step time 10.840 s 9.534 s 12.05% lower
Mean training-step time 10.836 s 9.575 s
Global input-token throughput 9,098 tokens/s 10,344 tokens/s 13.69% higher
Peak allocated GPU memory 93.22 GiB 98.39 GiB +5.17 GiB/GPU
Weight-quantizer calls per step, summed over 8 ranks 176,000 22,000 87.5% fewer

Per-run medians, in execution order: off1 10.755 s, on1 9.586 s, on2 9.429 s, off2 10.886 s. All measured steps are included; ranges are 10.551–11.078 s off and 9.399–10.011 s on.

All four runs completed successfully (24 optimizer steps, 192 rank-step records). Initial sampled-parameter hashes matched across runs on every rank; all gradient norms were finite and nonzero, and post-training sampled parameters changed on every rank. The replay fixture SHA-256 is 869d8cd353c18f380831df40b4e8a3bb9afabab559505d69646957d10ff11f73.

Scope: model loading, online SGLang generation, rollout preprocessing and model backup/offload are outside this timing. The Qwen adapter does not enable the full ordered-MoE alignment path, and independent repeats of the same setting also produced non-bitwise-identical training trajectories. This experiment establishes a full-model training-step performance/memory tradeoff; the exact output/gradient parity evidence remains the focused tests above.

Review

Follow-up to #2262. cc @zhuzilin for the DeepGEMM alignment/cache lifetime review.

Prepared with AI assistance; the validation above was executed on the stated environment.

Exercise missing, disabled, and enabled cache flags and verify the scope
ends before optimizer updates, including invalid-gradient steps.

This branch has not been deployed

No deployments
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.

1 participant