Skip to content

Feat: Megatron-LM training backend with sparse weight sync#6

Closed
jsw-zorro wants to merge 3 commits into
Infini-AI-Lab:mainfrom
jsw-zorro:feat/megatron-weight-sync
Closed

Feat: Megatron-LM training backend with sparse weight sync#6
jsw-zorro wants to merge 3 commits into
Infini-AI-Lab:mainfrom
jsw-zorro:feat/megatron-weight-sync

Conversation

@jsw-zorro
Copy link
Copy Markdown

Adds Megatron-LM as a training backend with correct sparse (delta) weight
synchronization to RaaS under full parallelism (TP/PP/EP/ETP/VPP).

What

  • export_hf_named_params: streaming Megatron→HF per-tensor weight export
    (OOM-safe), delegating the gather + conversion to mbridge.
  • WeightManager HF-export mode: the transfer buffer holds HF-layout bytes,
    so the sparse delta is computed in HF space — correct under any
    parallelism. Removes the PP>1/EP>1 restrictions.
  • docker/Dockerfile.sglang: adds Transformer Engine + apex (the Megatron
    compiled deps).
  • Recipe examples/math/qwen3-8b-megatron-delta (Qwen3-8B, TP=4, delta).
  • Design doc docs/en/architecture/megatron-weight-sync.md.

Validation

  • Weight export bit-exact vs HF reference: Qwen3-0.6B (TP/PP combos) and
    Qwen3-30B-A3B MoE TP2/EP2/PP2 (18,867 tensors, 0 mismatch).
  • End-to-end RL: 100 steps, delta sparsity ~0.92, rising task reward.

jsw-zorro added 3 commits May 29, 2026 07:15
Add export_hf_named_params: a streaming generator that reconstructs the
global model from Megatron's TP/PP/EP/ETP/VPP layout and yields HF-named,
HF-layout CPU tensors one at a time (OOM-safe for large / MoE models).
The gather + mcore->HF conversion is delegated to mbridge's export_weights
(the same bridge the engine already uses to load/save); this module adds
the consumer concerns: CPU move, byte-bounded bucketing, and a
metadata-only path for transfer-buffer sizing.

This is the foundation for correct sparse weight sync under full Megatron
parallelism. The design (the "delta is computed in HF byte space"
invariant) is documented in docs/en/architecture/megatron-weight-sync.md.

The sglang Docker image gains the Megatron compiled deps it was missing:
Transformer Engine (fused LayerNorm + sequence parallelism) and apex
(optional fused LayerNorm/Adam). megatron-core / mbridge were already in
the base install.

Validated (exact bf16 match vs the HF reference checkpoint):
- Qwen3-0.6B     TP=2            310 tensors, 0 mismatch
- Qwen3-0.6B     PP=2            311 tensors, 0 mismatch
- Qwen3-0.6B     TP=2 PP=2       311 tensors, 0 mismatch
- Qwen3-30B-A3B  TP=2 EP=2 PP=2  18867 tensors, 0 mismatch
Replace the TP-only shard-direct weight transfer with the HF-export path:

- MegatronEngine.export_hf_named_params() / get_hf_weight_metadata() stream
  gathered HF tensors via mbridge (handles TP/PP/EP/ETP/VPP). The previous
  PP>1 / EP>1 NotImplementedError guards are removed.
- WeightManager gains "megatron_hf_meta" mode: the transfer buffer is sized
  for the full HF model and offload() streams HF tensors into the inactive
  half on the writer rank, while the gather collectives run on all ranks in
  lockstep. The sender receives megatron_metadata=None and runs the plain
  full/delta path used by FSDP. Because the buffer now holds HF-layout
  bytes, the sparse delta is computed in HF space and is correct under any
  parallelism — fixing the latent corruption where the delta was computed
  in mcore layout but applied by the receiver in HF layout.
- ppo_trainer wires the generator + HF metadata through.

The legacy CPU shard-reassembly in the sender agent is now unused for
Megatron (kept only for the deprecated megatron_metadata path).

Validated (buffer roundtrip == HF reference, bit-exact):
- Qwen3-0.6B TP=2       310 tensors, 0 mismatch, 1.19 GB
- Qwen3-0.6B TP=2 PP=2  311 tensors, 0 mismatch, 1.50 GB
Add examples/math/qwen3-8b-megatron-delta — the FSDP qwen3-8b-m2po-delta
recipe with the trainer engine switched to the Megatron backend
(backend: megatron, tensor_parallel_size: 4). Identical data, algorithm,
and weight-transfer path, so it doubles as a clean FSDP-vs-Megatron A/B.

End-to-end (4 RaaS + 4 trainer TP=4, delta TCP): 100 training
steps, weight_transfer/delta_sparsity ~0.92 (HF-space delta), and a rising
task reward (first-20 mean 0.55 -> later windows ~0.61-0.63).
@jsw-zorro jsw-zorro requested a review from haizhongzheng as a code owner May 29, 2026 07:21
@jsw-zorro jsw-zorro force-pushed the feat/megatron-weight-sync branch from d52b6ad to b6d0ce3 Compare May 29, 2026 07:22
@jsw-zorro jsw-zorro closed this May 30, 2026
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