Skip to content

[feat] Init true on policy with qwen_dense - #29

Merged
maocheng23 merged 21 commits into
miles-mainfrom
feat/true_on_policy_qwen_dense
May 18, 2026
Merged

[feat] Init true on policy with qwen_dense#29
maocheng23 merged 21 commits into
miles-mainfrom
feat/true_on_policy_qwen_dense

Conversation

@maocheng23

@maocheng23 maocheng23 commented Apr 28, 2026

Copy link
Copy Markdown

Summary

Initial framework introducing true-on-policy for Qwen3-dense across SGLang, Megatron, and Miles. This is one of three tightly-coupled PRs that must land together — they share a single contract identifier qwen3_dense_true_on_policy_v1 defined by a vendored schema in each repo.

Companion PRs (must land in lockstep):

Target

Bit-identical (exact-zero) logprob parity between the SGLang rollout engine and the Megatron trainer for every scored response token at TP=1, TP>1, PP>1, and Ulysses CP for Qwen3-4B (dense).

Design

Three-layer contract architecture:

  1. Miles (launcher): picks contract, validates parallel layout against typed model profile, renders SGLang/Megatron/env args from a typed kernel policy, drives PPO logprob comparison.
  2. SGLang (rollout numerical truth): produces logprobs using deterministic kernels — TP-invariant matmul + fixed binary tree all-reduce + fp32 log-softmax.
  3. Megatron (parity target, this PR): reproduces SGLang's numerics in a differentiable training forward via SGLangSpecProvider layer classes.

The contract object owns its own runtime policy — adding a new architecture (e.g. Qwen3-MoE) is one new contract object + one new model profile entry, not edits across three repos. Backward path is fully differentiableSGLang* layer classes inherit from standard Megatron layers and use the gradient-aware Megatron primitives (linear_with_grad_accumulation_and_async_allreduce).

In this PR (Megatron)

  • New megatron/core/true_on_policy/ package:
    • schema.py — vendored shared identity, byte-identical with SGLang and Miles copies
    • contracts.pyMegatronTrueOnPolicyContract with policy_for(config) factory; resolver is registry lookup, not branch
    • provider.pySGLangSpecProvider extending BackendSpecProvider
    • Layer classes: SGLangNorm, SGLangColumnParallelLinear, SGLangRowParallelLinear, SGLangCoreAttention, SGLangFinalRMSNorm, SGLangQKRMSNorm
    • matmul.pysglang_reference_matmul with K-block fixed-tree partial-sum ordering matching SGLang's row-linear contract, delegating to gradient-aware Megatron primitives in backward
    • attention_fa3.py, rope.py, cp_layout.py, bias_dropout.py, runtime.py, sglang_backend.py
  • Forward-path leak collapse — zero if self.config.use_sglang: branches in forward paths:
    • transformer/attention.py — dtype boundaries via runtime policy
    • transformer/transformer_layer.py — residual contract via runtime policy
    • transformer/transformer_block.py — block-level true-on-policy behavior + Ulysses CP recompute fallback
    • transformer/linear_cross_entropy.py — LM-head input cast via runtime policy
    • tensor_parallel/layers.py — deterministic row-parallel reduction via runtime policy
    • distributed/distributed_data_parallel.py — Ulysses CP gradient scaling via runtime policy
    • models/gpt/gpt_layer_specs.py — spec provider selection from runtime policy
  • Phase 4 — retire use_sglang boolean field on TransformerConfig; replaced by true_on_policy_contract: Optional[str]. Backward-compat warning removed.
  • All 19 typed runtime-policy fields land — cast_attention_input_to_dense_math_dtype, use_sglang_residual_pair, use_sglang_final_norm, deterministic_row_parallel_reduce, defer_ulysses_cp_loss_scaling_to_grad_sum, etc.

Validation

  • ✅ CPU unit tests green (test_sglang_extension.py, test_tree_all_reduce.py, test_true_on_policy_logits.py)
  • 🔴 GPU exact-zero E2E gate not yet run at TP=1, TP>1, PP>1, CP. This is the next task on the stack before adopting Qwen3-MoE / Qwen3-Next.
  • 🟡 Backward smoke test for every training-path kernel — listed in the contract checklist but not yet executed; gating item before training runs.

Out of scope

  • Qwen3-MoE / Qwen3-Next contracts — additive after this stack lands; spec provider has grouped_mlp_modules slot reserved (currently falls back to standard GroupedMLP)
  • Alignment harness (layer-dump comparator, first-divergence comparator) — separate follow-up PR
  • BackendSpecProvider extension for hybrid attention (Qwen3-Next) — needed when that contract is added
  • Model-family cross-check at validator — recommended before contract [1/8] fix: misc compatibility fixes for PyTorch and TE #2

Test plan

  • CPU unit tests pass in CI
  • GPU exact-zero E2E gate at TP=1
  • GPU exact-zero E2E gate at TP=2
  • Backward smoke test for SGLang layer classes
  • Training smoke run with PPO loss (validates Miles bug fix in companion PR)

🤖 Generated with Claude Code

Comment thread miles_megatron_plugins/true_on_policy/__init__.py

@Zhichenzzz Zhichenzzz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Comment thread megatron/core/transformer/transformer_layer.py Outdated
Comment thread megatron/core/transformer/transformer_layer.py Outdated
Comment thread megatron/core/distributed/param_and_grad_buffer.py Outdated
maocheng23 and others added 15 commits May 17, 2026 16:03
Add a clean Megatron backend that calls SGLang-compatible math under a flag:
- sglang.py: SGLangLinear, SGLangRMSNorm, SGLangFlashAttention and related modules
- matmul_tp_inv.py: TP-invariant matmul dispatch for Megatron layers
- transformer_config.py: use_sglang config flag
- arguments.py: --use-sglang CLI arg
- layers.py: conditional SGLang backend selection in TP layers
- gpt_layer_specs.py: SGLang-compatible layer spec builder
- test_sglang_extension.py: import, config, and default-path-unchanged tests

Default training path remains unchanged when use_sglang is off.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Match SGLang's TP reduction order and full-vocab logprob contract:
- mappings.py: tree_all_reduce_sum for deterministic TP reduction
- layers.py: conditional tree allreduce in RowParallelLinear
- gpt_model.py: full-vocab logprob gather/truncate/log-softmax
- transformer_config.py: true_on_policy_logits config
- test_tree_all_reduce.py: TP tree-allreduce tests
- test_true_on_policy_logits.py: full-vocab gather/truncate tests

Default NCCL allreduce path unchanged when flags are off.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
maocheng23 and others added 5 commits May 17, 2026 16:03
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
isort/black reformatting on files touched by the true-on-policy
substrate, runtime contract, and Qwen3-dense parity path.
No semantic changes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
@maocheng23
maocheng23 force-pushed the feat/true_on_policy_qwen_dense branch from 4a5418a to 86b2fd4 Compare May 17, 2026 23:14
Co-authored-by: zju-stu-lizheng <lizheng.cs@zju.edu.cn>
Co-authored-by: zyxiyy02 <282300612+zyxiyy02@users.noreply.github.com>
Co-authored-by: Yi Zhang <1109276519@qq.com>
@maocheng23
maocheng23 force-pushed the feat/true_on_policy_qwen_dense branch 2 times, most recently from 9546575 to 57258c8 Compare May 18, 2026 06:15
@maocheng23
maocheng23 merged commit 23924a0 into miles-main May 18, 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.

3 participants