Skip to content

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

Open
maocheng23 wants to merge 28 commits into
sglang-milesfrom
feat/true_on_policy_qwen_dense
Open

[feat] Init true on policy with qwen_dense#2
maocheng23 wants to merge 28 commits into
sglang-milesfrom
feat/true_on_policy_qwen_dense

Conversation

@maocheng23

@maocheng23 maocheng23 commented Apr 28, 2026

Copy link
Copy Markdown
Owner

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, this PR): produces logprobs using deterministic kernels — TP-invariant matmul + fixed binary tree all-reduce + fp32 log-softmax.
  3. Megatron (parity target): 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.

In this PR (SGLang)

  • New python/sglang/srt/true_on_policy/ package:
    • schema.py — vendored shared identity, byte-identical with Megatron and Miles copies
    • contracts.pySGLangTrueOnPolicyContract with policy_for(server_args) factory; resolver is registry lookup, not branch
    • config.py — policy-aware helpers (is_true_on_policy_enabled, should_force_bfloat16_*, should_use_tp_invariant_*)
  • New python/sglang/srt/tp_invariant_ops/ package: deterministic K-block fixed-tree matmul + tree all-reduce Triton kernels
  • Forward-path consumers read typed runtime policy (no raw flag branching):
    • layers/{linear,layernorm,sampler,activation,communicator,logits_processor}.py
    • layers/rotary_embedding/{base,mrope}.py
    • layers/attention/vision.py
    • model_executor/{model_runner,cuda_graph_runner,forward_batch_info}.py
  • Model integration:
    • models/qwen3.py — dense, parity-validated path
    • models/{qwen3_moe,qwen2,qwen2_moe,sdar,sdar_moe,step3p5}.py — defensive helper-API alignment (these models are not yet declared true-on-policy ready; alignment keeps helper API consistent for future contract additions)
  • New --true-on-policy-contract CLI flag; --rl-on-policy-target demoted to legacy fallback (zero in-tree readers)
  • patch_prefill_only_deterministic_inference_for_cuda_graph — env/server_args mutation gutted

Validation

  • ✅ CPU unit tests green (test_on_policy_wiring.py, test_dense_deterministic_math.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.

Out of scope

  • Qwen3-MoE / Qwen3-Next contracts — additive after this stack lands
  • Alignment harness (layer-dump comparator, first-divergence comparator) — separate follow-up PR
  • Full retirement of legacy rl_on_policy_target field — zero remaining in-tree readers; cosmetic cleanup

Test plan

  • CPU unit tests pass in CI
  • GPU exact-zero E2E gate at TP=1
  • GPU exact-zero E2E gate at TP=2
  • grad_norm parity check (validates Miles _ReplicatedLossAllGatherLastDim fix)

🤖 Generated with Claude Code

ByronHsu and others added 20 commits May 8, 2026 22:45
…or --tokenizer-worker-num > 1 (sgl-project#24769)

Co-authored-by: maocheng23 <35615230+maocheng23@users.noreply.github.com>
Co-authored-by: lawrence-harmonic <185285563+lawrence-harmonic@users.noreply.github.com>
…a keys for prefix cache isolation (sgl-project#24770)

Co-authored-by: Charles Chen <pychen96@gmail.com>
…solution under shuffled CUDA_VISIBLE_DEVICES (sgl-project#24901)

Co-authored-by: Byron Hsu <byron@periodiclabs.ai>
Co-authored-by: Cursor <cursoragent@cursor.com>
…t to unpicklable server_args and large meta_info (sgl-project#24902)

Co-authored-by: Byron Hsu <byron@periodiclabs.ai>
Co-authored-by: Cursor <cursoragent@cursor.com>
…NCCL all-gather for cross-DP info sync (sgl-project#24903)

Co-authored-by: Byron Hsu <byron@periodiclabs.ai>
Co-authored-by: Cursor <cursoragent@cursor.com>
…ache() for in-place pause mode to avoid OOM (sgl-project#24905)

Co-authored-by: Byron Hsu <byron@periodiclabs.ai>
Co-authored-by: Cursor <cursoragent@cursor.com>
…t_len for absolute routing slice control (sgl-project#24904)

Co-authored-by: Byron Hsu <byron@periodiclabs.ai>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: zyzshishui <zyzshishui@gmail.com>
Co-authored-by: Yuzhen Zhou <82826991+zyzshishui@users.noreply.github.com>
…call id. (sgl-project#25196)

Co-authored-by: Jiajun Li <jiajun.li@MacBook-Pro-F7J34QJK2C-jiajunli.local>
Add deterministic TP kernels for true-on-policy alignment:
- matmul_tp_inv: TP-invariant persistent matmul via DeepGEMM/Triton
- tree_all_reduce_sum: fixed-order binary tree allreduce
- moe_sum_tree_reduce: deterministic per-token MoE expert combine
- tensor_model_parallel_tree_all_reduce in communication_op.py
- Unit tests in test/registered/core/test_tp_invariant_ops.py

Kernels are dormant unless explicitly enabled by later on-policy flags.

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>
Expand test_tp_invariant_ops.py from 15 to 55 tests covering all PR1
kernel invariance properties:

- Cross-TP invariance (bitwise torch.equal): TP=1 == TP=N for
  K={512,1024,2048,4096} in BF16/FP32/FP16, including torch.ops dispatch
- Determinism: same TP degree, two runs == bitwise identical for
  production K={3584,5120,4096} (Qwen3-4B/30B hidden sizes)
- MoE slot/EP invariance: permuting expert slot assignments gives
  bitwise identical results (FP32, BF16, with remote experts, H=7168 E=64)
- fp32_accum path: deterministic and correct output dtype
- Input validation: matmul rejects non-2D/dim mismatch, MoE rejects
  shape mismatches and non-power-of-two E
- Edge cases: K < BLOCK_K, K == BLOCK_K, K non-aligned, single expert,
  duplicate expert IDs, all-remote, odd tree count
- Distributed: multi-GPU tree all-reduce correctness + BF16 bitwise
  determinism (requires torchrun)

Documents the invariance contract: cross-TP equality holds when
K/(BLOCK_K * tp_size) is a power of two (isomorphic tree). For
non-isomorphic cases, the guarantee is determinism at fixed TP degree.

Made-with: Cursor
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>
Add runtime flags and deterministic path selection for true-on-policy mode:
- server_args: rl_on_policy_target (fsdp/fsdp_tp) flag and related args
- linear.py: TP-invariant row-linear and tree-allreduce path selection
- communicator.py: deterministic allreduce in gather/scatter paths
- cuda_graph_runner.py: CUDA graph guards for prefill-only deterministic mode
- model_runner.py: on-policy mode initialization
- on_policy_utils.py: shared utilities for on-policy path selection

Default serving path remains 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>
Make dense Qwen3 rollout numerically compatible with Megatron scoring:
- layernorm.py: RMSNorm forward_native with fp32 precision control
- logits_processor.py: CUDA graph-safe dtype handling for lm_head
- on_policy_utils.py: dense deterministic math helpers
- qwen2.py/qwen3.py: fp32_residual, override_orig_dtype, attention backend
- tp_invariant_ops: refinements for dense matmul paths
- Updated PR1/PR2 tests for compatibility

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>
maocheng23 and others added 7 commits May 17, 2026 16:02
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/ruff/black reformatting on files touched by the true-on-policy
substrate, contract, and Qwen3-dense wiring. 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>
…icy config

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 82b0b7e to e883df8 Compare May 17, 2026 23:14
@maocheng23
maocheng23 force-pushed the feat/true_on_policy_qwen_dense branch from ab1389b to 76c42c7 Compare May 18, 2026 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants