[feat] Init true on policy with qwen_dense - #29
Merged
Conversation
This was referenced Apr 28, 2026
yueming-yuan
reviewed
May 1, 2026
yueming-yuan
approved these changes
May 14, 2026
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>
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>
maocheng23
force-pushed
the
feat/true_on_policy_qwen_dense
branch
from
May 17, 2026 23:14
4a5418a to
86b2fd4
Compare
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
force-pushed
the
feat/true_on_policy_qwen_dense
branch
2 times, most recently
from
May 18, 2026 06:15
9546575 to
57258c8
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_v1defined 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:
SGLangSpecProviderlayer 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 differentiable —
SGLang*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)
megatron/core/true_on_policy/package:schema.py— vendored shared identity, byte-identical with SGLang and Miles copiescontracts.py—MegatronTrueOnPolicyContractwithpolicy_for(config)factory; resolver is registry lookup, not branchprovider.py—SGLangSpecProviderextendingBackendSpecProviderSGLangNorm,SGLangColumnParallelLinear,SGLangRowParallelLinear,SGLangCoreAttention,SGLangFinalRMSNorm,SGLangQKRMSNormmatmul.py—sglang_reference_matmulwith K-block fixed-tree partial-sum ordering matching SGLang's row-linear contract, delegating to gradient-aware Megatron primitives in backwardattention_fa3.py,rope.py,cp_layout.py,bias_dropout.py,runtime.py,sglang_backend.pyif self.config.use_sglang:branches in forward paths:transformer/attention.py— dtype boundaries via runtime policytransformer/transformer_layer.py— residual contract via runtime policytransformer/transformer_block.py— block-level true-on-policy behavior + Ulysses CP recompute fallbacktransformer/linear_cross_entropy.py— LM-head input cast via runtime policytensor_parallel/layers.py— deterministic row-parallel reduction via runtime policydistributed/distributed_data_parallel.py— Ulysses CP gradient scaling via runtime policymodels/gpt/gpt_layer_specs.py— spec provider selection from runtime policyuse_sglangboolean field onTransformerConfig; replaced bytrue_on_policy_contract: Optional[str]. Backward-compat warning removed.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
test_sglang_extension.py,test_tree_all_reduce.py,test_true_on_policy_logits.py)Out of scope
grouped_mlp_modulesslot reserved (currently falls back to standardGroupedMLP)BackendSpecProviderextension for hybrid attention (Qwen3-Next) — needed when that contract is addedTest plan
🤖 Generated with Claude Code