From cbdc00a2599506e14b9837b65448d35a6ec9222f Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 9 Oct 2026 01:43:43 +0000 Subject: [PATCH 01/68] feat(qwen3-next): MoE route/combine contract with a batch-invariant grouped expert GEMM RFC #428 moe_route_combine_contract (CUDA). - Routing: FP32 router through the pinned vLLM batch-invariant GEMM, a fixed-order softmax, stable descending argsort (ties go to the lower expert id) and FP32 renormalisation of the ten selected weights. - Experts: per-route gate_up and down projections through vLLM 0.30.0's fused_moe Triton kernel with the batch-invariant tile passed explicitly (BLOCK 64/64/32, SPLIT_K=1); no routed-weight multiply and no sum inside the kernel, so every (token, route) row has one writer. Bitwise equal to the previous per-expert GEMM loop at TP4 and TP1 widths; fails closed if VLLM_TRITON_USE_TD is enabled. - Combine: ten ordered FP32 multiply-adds and one BF16 cast; no atomics. - Backward: per-expert pinned GEMMs into one dense gradient buffer; dx is summed in ascending expert order. - TP4 block: HF load/export for all 512 experts, replicated router and shared-expert gate, Megatron-style copy/reduce autograd boundaries moved into qwen3_next_tp.py. - Prior-art runner and plot comparing HF transformers, vLLM fused_moe (BI=0/1) and FlashInfer cutlass_fused_moe; TP4 real-weight gate script. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/ci.yml | 2 +- .github/workflows/qwen3-next-provider-gpu.yml | 11 +- rl_engine/integrations/qwen3_next_forward.py | 338 ++++++++++++++ rl_engine/integrations/qwen3_next_tp.py | 65 +++ .../integrations/qwen3_next_tp_blocks.py | 138 ++++++ rl_engine/testing/qwen3_next_moe_prior_art.py | 440 ++++++++++++++++++ rl_engine/testing/tensor_identity.py | 38 ++ scripts/plot_qwen3_next_moe_prior_art.py | 159 +++++++ scripts/qwen3_next_moe_prior_art.py | 87 ++++ scripts/qwen3_next_tp_moe_check.py | 207 ++++++++ tests/check_qwen3_next_forward.py | 238 ++++++++++ tests/test_qwen3_next_forward_contract.py | 62 +++ tests/test_qwen3_next_tp_blocks.py | 93 ++++ tests/test_tensor_identity.py | 41 ++ 14 files changed, 1915 insertions(+), 4 deletions(-) create mode 100644 rl_engine/integrations/qwen3_next_forward.py create mode 100644 rl_engine/integrations/qwen3_next_tp.py create mode 100644 rl_engine/integrations/qwen3_next_tp_blocks.py create mode 100644 rl_engine/testing/qwen3_next_moe_prior_art.py create mode 100644 rl_engine/testing/tensor_identity.py create mode 100644 scripts/plot_qwen3_next_moe_prior_art.py create mode 100644 scripts/qwen3_next_moe_prior_art.py create mode 100644 scripts/qwen3_next_tp_moe_check.py create mode 100644 tests/check_qwen3_next_forward.py create mode 100644 tests/test_qwen3_next_forward_contract.py create mode 100644 tests/test_qwen3_next_tp_blocks.py create mode 100644 tests/test_tensor_identity.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 814a4ae22..2fb92d641 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,7 +67,7 @@ jobs: run: | python -m pytest rl_engine/tests/test_dispatch.py -v PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python -m pytest tests/test_attention_correctness.py -q -rs - python -m pytest tests/test_forward_invariance.py tests/test_tolerance_contract.py tests/test_ws1_workload.py tests/test_gradient_invariance.py tests/test_elementwise_inventory.py tests/test_four_judgment_matrix.py tests/test_op_checks.py tests/test_operator_inputs.py tests/test_profiler.py tests/test_kv_consistency.py tests/test_ws1_qwen3_dense.py tests/test_ws1_chain_integration.py tests/test_qwen3_next_norm.py tests/test_gdn_state_contract.py -q + python -m pytest tests/test_forward_invariance.py tests/test_tolerance_contract.py tests/test_ws1_workload.py tests/test_gradient_invariance.py tests/test_elementwise_inventory.py tests/test_four_judgment_matrix.py tests/test_op_checks.py tests/test_operator_inputs.py tests/test_profiler.py tests/test_kv_consistency.py tests/test_ws1_qwen3_dense.py tests/test_ws1_chain_integration.py tests/test_qwen3_next_norm.py tests/test_gdn_state_contract.py tests/test_tensor_identity.py tests/test_qwen3_next_forward_contract.py tests/test_qwen3_next_tp_blocks.py -q - name: Run Cross-Configuration Contract Tests (CPU-safe) run: | diff --git a/.github/workflows/qwen3-next-provider-gpu.yml b/.github/workflows/qwen3-next-provider-gpu.yml index 146818019..691489402 100644 --- a/.github/workflows/qwen3-next-provider-gpu.yml +++ b/.github/workflows/qwen3-next-provider-gpu.yml @@ -1,6 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 -# RFC #428 Qwen3-Next provider comparisons: the C1/C3/C4 norm providers and the C6 -# GDN decode-step goldens, each checked against the real vLLM 0.30.0 provider. +# RFC #428 Qwen3-Next provider comparisons: the C1/C3/C4 norm providers, the C6 +# GDN decode-step goldens and the shared GEMM/MoE route-combine provider, each +# checked against the real vLLM 0.30.0 provider. # # These check_ files import vLLM, so the default pytest collection never runs them # (tests/test_framework_operator_integrations.py asserts vLLM is not imported). This @@ -19,6 +20,7 @@ on: branches: [main] paths: - "rl_engine/kernels/ops/**" + - "rl_engine/integrations/qwen3_next*" - "rl_engine/testing/qwen3_next*" - "tests/check_*qwen3_next*.py" - "tests/check_gdn_recurrent_golden.py" @@ -78,5 +80,8 @@ jobs: print("rl_engine._C:", loaded) PY - name: Check pinned providers in their own process + # The FP32 router needs IEEE Triton dots from process start. + env: + TRITON_F32_DEFAULT: ieee run: | - python3 -m pytest -q tests/check_qwen3_next_norm_providers.py tests/check_gdn_recurrent_golden.py + python3 -m pytest -q tests/check_qwen3_next_norm_providers.py tests/check_gdn_recurrent_golden.py tests/check_qwen3_next_forward.py diff --git a/rl_engine/integrations/qwen3_next_forward.py b/rl_engine/integrations/qwen3_next_forward.py new file mode 100644 index 000000000..b5a1e20e0 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_forward.py @@ -0,0 +1,338 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Explicit CUDA forward primitives for the Qwen3-Next shared TP4 profile. + +These functions do not install framework hooks or advertise engine capabilities. +The caller owns parameter sharding, TP collectives, positions and cache identity. +Weights remain BF16; only the router explicitly computes in FP32, as in VIME's +Qwen3-Next configuration. No CPU or alternate-kernel fallback is permitted. +""" + +from dataclasses import dataclass +from functools import lru_cache +from importlib.metadata import version + +import torch +import torch.nn.functional as F +from torch.autograd.function import once_differentiable + +FORWARD_PROVIDER_ID = "qwen3-next-shared-cuda-forward-v1" + + +@lru_cache(maxsize=1) +def _vllm_linear(): + if version("vllm") != "0.30.0": + raise RuntimeError("Shared Qwen3-Next linear requires pinned vLLM 0.30.0") + from vllm.model_executor.determinism.batch_invariant import linear_batch_invariant + + return linear_batch_invariant + + +# The batch-invariant tile vLLM itself selects under VLLM_BATCH_INVARIANT=1 +# (fused_moe.get_default_config), passed explicitly so no environment variable, +# tuned-config file or token count can change it. +_GROUPED_CONFIG = { + "BLOCK_SIZE_M": 64, + "BLOCK_SIZE_N": 64, + "BLOCK_SIZE_K": 32, + "GROUP_SIZE_M": 8, + "SPLIT_K": 1, +} + + +@lru_cache(maxsize=1) +def _vllm_grouped(): + if version("vllm") != "0.30.0": + raise RuntimeError("Shared Qwen3-Next experts require pinned vLLM 0.30.0") + from vllm.model_executor.layers.fused_moe.fused_moe import ( + _prepare_expert_assignment, + dispatch_fused_moe_kernel, + ) + from vllm.model_executor.layers.fused_moe.utils import resolve_moe_use_td + + return _prepare_expert_assignment, dispatch_fused_moe_kernel, resolve_moe_use_td + + +def grouped_route_linear(rows, weight, route_indices): + """Per-route ``rows @ weight[expert].T`` through vLLM's fused MoE Triton kernel. + + ``rows`` is [T, K] (one row per token, shared by its ten routes) or + [T*10, K] (one row per route); the result is [T, 10, N] in route order. + No routed weight is applied and nothing is summed: every output row has + exactly one writer, and its K loop has the fixed order of the fixed tile. + """ + prepare, dispatch, use_td = _vllm_grouped() + if use_td(): + raise RuntimeError("VLLM_TRITON_USE_TD changes the MoE kernel; strict mode requires 0") + import triton.language as tl + + tokens, top_k = route_indices.shape + out = rows.new_empty((tokens, top_k, weight.shape[1])) + if tokens == 0: + return out + indices = route_indices.to(torch.int32).contiguous() + sorted_ids, expert_ids, padded = prepare( + indices, _GROUPED_CONFIG, tokens, top_k, weight.shape[0], None, ignore_invalid_experts=True + ) + dispatch( + rows.contiguous(), + weight, + out, + None, + None, + None, + None, + sorted_ids, + expert_ids, + padded, + False, + top_k if rows.shape[0] == tokens else 1, + _GROUPED_CONFIG, + compute_type=tl.bfloat16, + use_fp8_w8a8=False, + use_int8_w8a8=False, + use_int8_w8a16=False, + use_int4_w4a16=False, + per_channel_quant=False, + ) + return out + + +def _cuda_tensors(*tensors): + if torch.version.hip is not None or any(not value.is_cuda for value in tensors): + raise ValueError("Shared Qwen3-Next forward requires CUDA tensors; no CPU fallback") + if any(value.device != tensors[0].device for value in tensors[1:]): + raise ValueError("Shared Qwen3-Next tensors must be on the same device") + + +class _SharedLinear(torch.autograd.Function): + @staticmethod + def forward(ctx, x, weight, bias): + ctx.save_for_backward(x, weight) + ctx.has_bias = bias is not None + if x.shape[0] == 0: + return x.new_empty((0, weight.shape[0])) + with torch.cuda.device(x.device): + return _vllm_linear()(x, weight, bias) + + @staticmethod + @once_differentiable + def backward(ctx, grad_output): + x, weight = ctx.saved_tensors + if x.shape[0] == 0: + return ( + torch.empty_like(x) if ctx.needs_input_grad[0] else None, + torch.zeros_like(weight) if ctx.needs_input_grad[1] else None, + weight.new_zeros(weight.shape[0]) if ctx.has_bias else None, + ) + grad = grad_output.contiguous() + with torch.cuda.device(x.device): + linear = _vllm_linear() + dx = linear(grad, weight.t()) if ctx.needs_input_grad[0] else None + dw = linear(grad.t(), x.t()) if ctx.needs_input_grad[1] else None + db = grad.sum(dim=0) if ctx.has_bias and ctx.needs_input_grad[2] else None + return dx, dw, db + + +def shared_linear(x, weight, bias=None): + """Pinned vLLM deterministic GEMM with an explicit first-order VJP. + + x is [..., K], weight is [N, K], output is [..., N]. Operands have one + common BF16 or FP32 dtype. Empty token dimensions preserve autograd. + Backward executes dX=dY@W and dW=dY.T@X through the same fixed provider. + """ + _cuda_tensors(x, weight, *(() if bias is None else (bias,))) + if x.ndim < 2 or weight.ndim != 2 or x.shape[-1] != weight.shape[1]: + raise ValueError("Shared linear requires x[..., K] and weight[N, K]") + if x.dtype not in (torch.bfloat16, torch.float32) or weight.dtype != x.dtype: + raise ValueError("Shared linear operands must have one common BF16 or FP32 dtype") + if x.dtype == torch.float32: + import triton + + if triton.knobs.language.fp32_default != "ieee": + raise RuntimeError( + "FP32 shared linear requires TRITON_F32_DEFAULT=ieee at process startup" + ) + if min(weight.shape) < 1: + raise ValueError("Shared linear weight dimensions must be positive") + if bias is not None and (bias.shape != (weight.shape[0],) or bias.dtype != x.dtype): + raise ValueError("Shared linear bias must be [N] with the input dtype") + out = _SharedLinear.apply(x.reshape(-1, x.shape[-1]), weight, bias) + return out.reshape(*x.shape[:-1], weight.shape[0]) + + +def shared_router(x, weight): + """Compute the 512-expert FP32 router from BF16 inputs/parameter storage.""" + _cuda_tensors(x, weight) + if x.dtype != torch.bfloat16 or weight.dtype != torch.bfloat16: + raise ValueError("Qwen3-Next router input and stored weight must be BF16") + if x.ndim != 2 or weight.shape != (512, x.shape[-1]): + raise ValueError("Qwen3-Next router requires x[T, H] and weight[512, H]") + return shared_linear(x.float(), weight.float()) + + +@dataclass(frozen=True) +class Routing: + """Descending probability routes; equal scores prefer smaller expert IDs.""" + + weights: torch.Tensor + indices: torch.Tensor + + +def fixed_order_row_sum(values): + """Sum the last dimension by a pairwise tree of elementwise adds. + + ``torch.sum`` picks its reduction order from the tensor shape, so the same + row summed inside an 8-row and a 32-row tensor can differ by one ulp unless + vLLM's batch-invariant aten overrides are installed. Qualification runs + execute with ``VLLM_BATCH_INVARIANT=0``, where those overrides are absent. + Elementwise adds have no such freedom: every row is summed in one fixed + tree whatever the row count. Odd widths are padded with exact zeros. + """ + while values.shape[-1] > 1: + if values.shape[-1] % 2: + values = torch.cat((values, values.new_zeros((*values.shape[:-1], 1))), dim=-1) + values = values[..., 0::2] + values[..., 1::2] + return values + + +def fixed_order_softmax(logits): + """Row softmax from a max subtraction, exp and one fixed-order row sum.""" + shifted = logits - torch.amax(logits, dim=-1, keepdim=True) + exponentials = torch.exp(shifted) + return exponentials / fixed_order_row_sum(exponentials) + + +def stable_top10_routes(router_logits): + """Select ten distinct experts and normalize their FP32 probabilities. + + The full 512-way softmax precedes selection, preserving Qwen's normalized + top-k arithmetic. Stable sort fixes the otherwise ambiguous tie boundary. + Indices are discrete; gradients flow through the selected probabilities. + Both row sums use the fixed-order tree, so a token's route weights do not + depend on how many other tokens share its forward pass. + """ + _cuda_tensors(router_logits) + if router_logits.ndim != 2 or router_logits.shape[1] != 512: + raise ValueError("Qwen3-Next routing requires logits[T, 512]") + if router_logits.dtype != torch.float32: + raise ValueError("Qwen3-Next router logits must be FP32") + if not torch.isfinite(router_logits).all(): + raise ValueError("Qwen3-Next router logits must be finite") + probabilities = fixed_order_softmax(router_logits) + indices = torch.argsort(router_logits, dim=-1, descending=True, stable=True)[:, :10] + selected = probabilities.gather(1, indices) + return Routing(selected / fixed_order_row_sum(selected), indices) + + +def combine_routes(expert_outputs, route_weights): + """Combine [T, 10, H] BF16 outputs by ten ordered FP32 multiply-adds. + + There are no atomic sums. Each route's product and each addition is a + separate eager operation; the single BF16 cast follows the final route. + """ + _cuda_tensors(expert_outputs, route_weights) + if expert_outputs.ndim != 3 or expert_outputs.shape[1] != 10: + raise ValueError("Qwen3-Next expert outputs must have shape [T, 10, H]") + if route_weights.shape != expert_outputs.shape[:2]: + raise ValueError("Route weights must have shape [T, 10]") + if expert_outputs.dtype != torch.bfloat16 or route_weights.dtype != torch.float32: + raise ValueError("Combine requires BF16 expert outputs and FP32 route weights") + output = expert_outputs[:, 0].float() * route_weights[:, 0, None] + for slot in range(1, 10): + output = output + expert_outputs[:, slot].float() * route_weights[:, slot, None] + return output.to(torch.bfloat16) + + +def _expert_activation(gate_up_rows): + """SwiGLU in FP32 from the BF16 [gate, up] projection; returns its parts too.""" + gate, up = gate_up_rows.float().chunk(2, dim=-1) + silu_gate = F.silu(gate) + return silu_gate * up, silu_gate, gate, up + + +class _RoutedExperts(torch.autograd.Function): + """Grouped expert forward; per-expert backward into one dense gradient buffer.""" + + @staticmethod + def forward(ctx, x, gate_up, down, route_indices): + ctx.save_for_backward(x, gate_up, down, route_indices) + with torch.cuda.device(x.device): + projected = grouped_route_linear(x, gate_up, route_indices) + activated = _expert_activation(projected)[0] + activated = activated.to(x.dtype).reshape(-1, activated.shape[-1]) + return grouped_route_linear(activated, down, route_indices) + + @staticmethod + @once_differentiable + def backward(ctx, grad_output): + x, gate_up, down, route_indices = ctx.saved_tensors + dx = torch.zeros_like(x) if ctx.needs_input_grad[0] else None + dgate_up = torch.zeros_like(gate_up) if ctx.needs_input_grad[1] else None + ddown = torch.zeros_like(down) if ctx.needs_input_grad[2] else None + top_k = route_indices.shape[1] + slots_by_expert = [[] for _ in range(gate_up.shape[0])] + for slot, expert in enumerate(route_indices.flatten().tolist()): + slots_by_expert[expert].append(slot) + grad_flat = grad_output.reshape(-1, x.shape[1]) + with torch.cuda.device(x.device): + linear = _vllm_linear() + projected = grouped_route_linear(x, gate_up, route_indices) + projected = projected.reshape(-1, projected.shape[-1]) + for expert, slots in enumerate(slots_by_expert): + if not slots: + continue + slots = torch.tensor(slots, dtype=torch.long, device=x.device) + tokens = slots // top_k + activated, silu_gate, gate32, up32 = _expert_activation( + projected.index_select(0, slots) + ) + activated = activated.to(x.dtype) + grad = grad_flat.index_select(0, slots) + if ddown is not None: + ddown[expert].copy_(linear(grad.t(), activated.t())) + if dx is None and dgate_up is None: + continue + rows = x.index_select(0, tokens) + dactivated = linear(grad, down[expert].t()).float() + sigmoid = gate32.sigmoid() + dgate = dactivated * up32 * (sigmoid + gate32 * sigmoid * (1 - sigmoid)) + dup = dactivated * silu_gate + dprojected = torch.cat((dgate, dup), dim=-1).to(x.dtype) + if dgate_up is not None: + dgate_up[expert].copy_(linear(dprojected.t(), rows.t())) + if dx is not None: + # Top-k has distinct experts, so tokens are unique within this + # launch. Ascending expert order fixes each token's sum order. + partial = linear(dprojected, gate_up[expert].t()) + dx.index_copy_(0, tokens, dx.index_select(0, tokens) + partial) + return dx, dgate_up, ddown, None + + +def shared_moe(x, router_weight, gate_up_weights, down_weights): + """Evaluate TP-local routed experts; caller reduces the returned output. + + Weights are [512, 2*I_local, H] (gate then up) and [512, H, I_local]. + All experts belong to EP=1. Each (token, route) output row has exactly one + writer and the ten routes are combined in route order. Shared-expert and + TP-reduction ownership remains with the model adapter. The forward has no + host synchronisation; the backward builds per-expert row lists on the host. + """ + _cuda_tensors(x, router_weight, gate_up_weights, down_weights) + if x.ndim != 2 or x.dtype != torch.bfloat16: + raise ValueError("Qwen3-Next MoE input must be BF16 [T, H]") + if gate_up_weights.ndim != 3 or down_weights.ndim != 3: + raise ValueError("Qwen3-Next expert weights must have three dimensions") + local_intermediate = down_weights.shape[-1] + if local_intermediate < 1: + raise ValueError("Expert intermediate dimension must be positive") + if gate_up_weights.shape != (512, 2 * local_intermediate, x.shape[1]): + raise ValueError("gate_up_weights must have shape [512, 2*I_local, H]") + if down_weights.shape != (512, x.shape[1], local_intermediate): + raise ValueError("down_weights must have shape [512, H, I_local]") + if gate_up_weights.dtype != x.dtype or down_weights.dtype != x.dtype: + raise ValueError("Qwen3-Next expert weights must remain BF16") + routes = stable_top10_routes(shared_router(x, router_weight)) + outputs = _RoutedExperts.apply(x, gate_up_weights, down_weights, routes.indices) + return combine_routes(outputs, routes.weights), routes diff --git a/rl_engine/integrations/qwen3_next_tp.py b/rl_engine/integrations/qwen3_next_tp.py new file mode 100644 index 000000000..884a2d043 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_tp.py @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Differentiable TP4 boundaries and parameter metadata shared by Qwen3-Next blocks. + +The forward copy and reduction follow Megatron's column/row-parallel autograd +pair. Reductions go through ``collective_for_group`` so a strict deployment +can pin the collective algorithm; nothing here selects one implicitly. +""" + +import torch +import torch.distributed as dist + + +def _reduce(value, group): + from rl_engine.distributed.collectives import collective_for_group + + collective = collective_for_group(group, min_size_bytes=value.numel() * value.element_size()) + return collective.all_reduce(value.contiguous()) + + +class _CopyToTP(torch.autograd.Function): + @staticmethod + def forward(ctx, value, group): + ctx.group = group + return value + + @staticmethod + def backward(ctx, gradient): + return _reduce(gradient, ctx.group), None + + +class _ReduceFromTP(torch.autograd.Function): + @staticmethod + def forward(ctx, value, group): + return _reduce(value, group) + + @staticmethod + def backward(ctx, gradient): + return gradient, None + + +def _rank(rank): + if isinstance(rank, bool) or not isinstance(rank, int) or rank not in range(4): + raise ValueError("TP4 rank must be an integer in [0, 4)") + + +def _weight(value, shape, name): + if value.dtype != torch.bfloat16 or value.shape != shape: + raise ValueError(f"{name} must be BF16 {shape}") + + +def _tp_group(group): + if not dist.is_initialized() or dist.get_world_size(group) != 4: + raise ValueError("Qwen3-Next blocks require an initialized four-rank TP group") + return dist.group.WORLD if group is None else group + + +def _parallel_parameter(parameter, dimension, *, stride=1, duplicate=False): + # MCore uses these attributes when computing the global optimizer gradient + # norm. A replicated copy updates normally but must not count twice. + parameter.tensor_model_parallel = True + parameter.partition_dim = dimension + parameter.partition_stride = stride + parameter.shared = duplicate diff --git a/rl_engine/integrations/qwen3_next_tp_blocks.py b/rl_engine/integrations/qwen3_next_tp_blocks.py new file mode 100644 index 000000000..1c80530d4 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_tp_blocks.py @@ -0,0 +1,138 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Official Qwen3-Next TP4 MoE block for shared adapters. + +The block owns the local arithmetic and differentiable TP boundaries. It does +not register an engine backend or advertise full-model acceptance. +""" + +import torch +import torch.distributed as dist +import torch.nn.functional as F +from torch import nn + +from rl_engine.integrations.qwen3_next_forward import shared_linear, shared_moe +from rl_engine.integrations.qwen3_next_tp import ( + _CopyToTP, + _parallel_parameter, + _rank, + _ReduceFromTP, + _tp_group, + _weight, +) +from rl_engine.testing.tensor_identity import tensor_bitwise_equal + + +def moe_hf_shapes(): + result = { + "gate.weight": (512, 2048), + "shared_expert_gate.weight": (1, 2048), + } + for prefix in ("shared_expert", *(f"experts.{expert}" for expert in range(512))): + result[f"{prefix}.gate_proj.weight"] = (512, 2048) + result[f"{prefix}.up_proj.weight"] = (512, 2048) + result[f"{prefix}.down_proj.weight"] = (2048, 512) + return result + + +def shard_moe_weight(name, value, rank): + """Shard one official HF tensor without materializing all expert weights.""" + _rank(rank) + shapes = moe_hf_shapes() + if name not in shapes: + raise ValueError(f"Unknown Qwen3-Next MoE weight: {name}") + _weight(value, shapes[name], name) + if name in ("gate.weight", "shared_expert_gate.weight"): + return value + return value.chunk(4, dim=1 if name.endswith("down_proj.weight") else 0)[rank].contiguous() + + +def assemble_moe_weight(name, values): + if len(values) != 4: + raise ValueError("Exactly four MoE weight shards are required") + shapes = moe_hf_shapes() + if name not in shapes: + raise ValueError(f"Unknown Qwen3-Next MoE weight: {name}") + if name in ("gate.weight", "shared_expert_gate.weight"): + for value in values: + _weight(value, shapes[name], name) + if any(not tensor_bitwise_equal(value, values[0]) for value in values[1:]): + raise ValueError(f"Replicated MoE weight differs across TP: {name}") + return values[0] + axis = 1 if name.endswith("down_proj.weight") else 0 + shape = list(shapes[name]) + shape[axis] //= 4 + for value in values: + _weight(value, tuple(shape), name) + return torch.cat(tuple(values), dim=axis) + + +class TP4MoE(nn.Module): + """All 512 experts at EP1 with intermediate width 128 per TP rank.""" + + def __init__(self, *, group, device): + super().__init__() + self.group = _tp_group(group) + self.rank = dist.get_rank(self.group) + factory = {"device": device, "dtype": torch.bfloat16} + self.gate = nn.Linear(2048, 512, bias=False, **factory) + self.experts = nn.Module() + self.experts.register_parameter( + "gate_up", nn.Parameter(torch.empty(512, 256, 2048, **factory)) + ) + self.experts.register_parameter( + "down", nn.Parameter(torch.empty(512, 2048, 128, **factory)) + ) + self.shared_expert = nn.Module() + self.shared_expert.gate_proj = nn.Linear(2048, 128, bias=False, **factory) + self.shared_expert.up_proj = nn.Linear(2048, 128, bias=False, **factory) + self.shared_expert.down_proj = nn.Linear(128, 2048, bias=False, **factory) + self.shared_expert_gate = nn.Linear(2048, 1, bias=False, **factory) + _parallel_parameter(self.experts.gate_up, 1, stride=2) + _parallel_parameter(self.experts.down, 2) + _parallel_parameter(self.shared_expert.gate_proj.weight, 0) + _parallel_parameter(self.shared_expert.up_proj.weight, 0) + _parallel_parameter(self.shared_expert.down_proj.weight, 1) + + def _hf_tensor(self, name): + if name.startswith("experts."): + _, expert, projection, _ = name.split(".") + expert = int(expert) + if projection == "down_proj": + return self.experts.down[expert] + offset = 0 if projection == "gate_proj" else 128 + return self.experts.gate_up[expert, offset : offset + 128] + obj = self + for part in name.split("."): + obj = getattr(obj, part) + return obj + + def load_hf_weights(self, weights): + if set(weights) != set(moe_hf_shapes()): + raise ValueError("MoE weight names do not match the official HF contract") + with torch.no_grad(): + for name in moe_hf_shapes(): + self._hf_tensor(name).copy_(shard_moe_weight(name, weights[name], self.rank)) + + def export_local_hf_weights(self): + return {name: self._hf_tensor(name).detach() for name in moe_hf_shapes()} + + def forward(self, hidden): + if hidden.ndim != 2 or hidden.shape[1] != 2048 or hidden.dtype != torch.bfloat16: + raise ValueError("MoE hidden must be BF16 [tokens, 2048]") + copied = _CopyToTP.apply(hidden, self.group) + routed, _ = shared_moe( + copied, + _CopyToTP.apply(self.gate.weight, self.group), + self.experts.gate_up, + self.experts.down, + ) + gate = shared_linear(copied, self.shared_expert.gate_proj.weight) + up = shared_linear(copied, self.shared_expert.up_proj.weight) + activated = (F.silu(gate.float()) * up.float()).to(hidden.dtype) + shared = shared_linear(activated, self.shared_expert.down_proj.weight) + shared_gate = shared_linear( + copied, _CopyToTP.apply(self.shared_expert_gate.weight, self.group) + ).sigmoid() + return _ReduceFromTP.apply(routed + shared * shared_gate, self.group) diff --git a/rl_engine/testing/qwen3_next_moe_prior_art.py b/rl_engine/testing/qwen3_next_moe_prior_art.py new file mode 100644 index 000000000..d8ad92234 --- /dev/null +++ b/rl_engine/testing/qwen3_next_moe_prior_art.py @@ -0,0 +1,440 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Prior-art comparison for the Qwen3-Next routed MoE (RFC #428 ``moe_route_combine_contract``). + +Every candidate computes the routed half of ``Qwen3NextSparseMoeBlock`` at the +official TP1 shape (H=2048, 512 experts, top-10, expert width 512): router +logits, top-10 selection with renormalised weights, the selected SwiGLU +experts and the weighted combine. The shared expert is left out because every +candidate evaluates it as the same dense MLP. + +For each candidate the report records + +* **routing batch invariance** - the expert ids and weights of a fixed block + of probe tokens computed alone, and embedded first and last in larger + batches, must be bitwise equal; +* **output batch invariance** - the same for the routed output; +* **dx / dW batch invariance** (candidates with a backward) - the probe rows' + input gradient must not change when unrelated rows join the batch, and the + expert weight gradient must not change when rows whose output gradient is + zero join it (mathematically neither can); +* **repeatability** - two identical calls are bitwise equal; +* **accuracy** - error against an FP64 evaluation of the HF formula with FP64 + routing, plus the fraction of tokens whose selected expert set differs; +* **performance** - median CUDA-event latency, candidates interleaved. + +A candidate that cannot be imported or launched in the pinned environment is +recorded as ``unavailable`` with the reason instead of failing the run. +""" + +from __future__ import annotations + +import os +import statistics +from contextlib import contextmanager +from typing import Any, Callable + +import torch +import torch.nn.functional as F + +HIDDEN = 2048 +EXPERTS = 512 +TOPK = 10 +WIDTH = 512 +PROBE = 8 +BI_SIZES = (16, 64, 256, 1024) +PERF_TOKENS = (1, 8, 64, 256, 1024, 4096) +BACKWARD_TOKENS = (64, 1024) + + +# --------------------------------------------------------------------------- # +# Measurement helpers +# --------------------------------------------------------------------------- # + + +def _sample_us(fn: Callable[[], Any]) -> float: + torch.cuda.synchronize() + start = torch.cuda.Event(enable_timing=True) + end = torch.cuda.Event(enable_timing=True) + start.record() + fn() + end.record() + torch.cuda.synchronize() + return start.elapsed_time(end) * 1000.0 + + +def time_interleaved(calls: dict[str, Callable[[], Any]], warmup: int = 5, iters: int = 30): + """Median latency per candidate; the order alternates so none always runs first.""" + keys = list(calls) + orders = (keys, list(reversed(keys))) + for i in range(warmup): + for key in orders[i % 2]: + calls[key]() + samples = {key: [] for key in keys} + for i in range(iters): + for key in orders[i % 2]: + samples[key].append(_sample_us(calls[key])) + return {key: statistics.median(values) for key, values in samples.items()} + + +def _bitwise(a: torch.Tensor, b: torch.Tensor) -> bool: + return a.shape == b.shape and a.dtype == b.dtype and torch.equal(a, b) + + +def _rel(value: torch.Tensor, reference: torch.Tensor) -> float: + reference = reference.double() + return float((value.double() - reference).norm() / reference.norm().clamp_min(1e-300)) + + +def _guard(key: str, factory: Callable[[], dict[str, Any]]) -> dict[str, Any]: + try: + return {"key": key, **factory()} + except Exception as exc: # noqa: BLE001 - record and continue + return {"key": key, "unavailable": f"{type(exc).__name__}: {str(exc)[:300]}"} + + +@contextmanager +def _env(name: str, value: str | None): + old = os.environ.get(name) + if value is None: + os.environ.pop(name, None) + else: + os.environ[name] = value + try: + yield + finally: + if old is None: + os.environ.pop(name, None) + else: + os.environ[name] = old + + +# --------------------------------------------------------------------------- # +# Inputs and the FP64 reference +# --------------------------------------------------------------------------- # + + +def make_weights(seed: int = 0) -> dict[str, torch.Tensor]: + g = torch.Generator(device="cuda").manual_seed(seed) + + def randn(*shape, scale): + return (torch.randn(shape, device="cuda", generator=g) * scale).to(torch.bfloat16) + + return { + "router": randn(EXPERTS, HIDDEN, scale=0.02), + "gate_up": randn(EXPERTS, 2 * WIDTH, HIDDEN, scale=0.02), + "down": randn(EXPERTS, HIDDEN, WIDTH, scale=0.02), + } + + +def make_tokens(count: int, seed: int) -> torch.Tensor: + g = torch.Generator(device="cuda").manual_seed(seed) + return torch.randn(count, HIDDEN, device="cuda", generator=g).to(torch.bfloat16) + + +def fp64_reference(x: torch.Tensor, w: dict[str, torch.Tensor]): + """HF formula in FP64 with FP64 routing; returns (output, expert ids).""" + x64 = x.double() + probs = (x64 @ w["router"].double().T).softmax(-1) + weights, ids = probs.topk(TOPK, dim=-1) + weights = weights / weights.sum(-1, keepdim=True) + out = torch.zeros_like(x64) + gate_up, down = w["gate_up"], w["down"] + for expert in ids.unique().tolist(): + token, slot = torch.where(ids == expert) + gate, up = (x64[token] @ gate_up[expert].double().T).chunk(2, dim=-1) + y = (F.silu(gate) * up) @ down[expert].double().T + out.index_add_(0, token, y * weights[token, slot, None]) + return out, ids + + +# --------------------------------------------------------------------------- # +# Candidates +# +# ``route(x) -> (ids [T,10], weights [T,10])`` exposes the routing decision; +# ``fwd(x) -> [T, H]`` is the routed output; ``graph(x) -> (y, leaves)`` (when +# the candidate is differentiable) returns the output and [x, gate_up, down]. +# --------------------------------------------------------------------------- # + + +def _rl_kernel(w): + from rl_engine.integrations import qwen3_next_forward as provider + + def route(x): + routes = provider.stable_top10_routes(provider.shared_router(x, w["router"])) + return routes.indices, routes.weights + + def fwd(x): + with torch.no_grad(): + return provider.shared_moe(x, w["router"], w["gate_up"], w["down"])[0] + + gate_up = w["gate_up"].detach().clone().requires_grad_(True) + down = w["down"].detach().clone().requires_grad_(True) + + def graph(x): + leaf = x.detach().clone().requires_grad_(True) + y, _ = provider.shared_moe(leaf, w["router"], gate_up, down) + return y, [leaf, gate_up, down] + + return { + "name": "rl-kernel shared_moe", + "source": "rl_engine.integrations.qwen3_next_forward.shared_moe", + "route": route, + "fwd": fwd, + "graph": graph, + } + + +def _hf_modules(w, device="cuda"): + from transformers import Qwen3NextConfig + from transformers.models.qwen3_next import modeling_qwen3_next as hf + + config = Qwen3NextConfig( + hidden_size=HIDDEN, + num_experts=EXPERTS, + num_experts_per_tok=TOPK, + moe_intermediate_size=WIDTH, + norm_topk_prob=True, + ) + router = hf.Qwen3NextTopKRouter(config).to(device=device, dtype=torch.bfloat16) + experts = hf.Qwen3NextExperts(config).to(device=device, dtype=torch.bfloat16) + with torch.no_grad(): + router.weight.copy_(w["router"]) + experts.gate_up_proj.copy_(w["gate_up"]) + experts.down_proj.copy_(w["down"]) + return router, experts + + +def _hf(w): + import transformers + + router, experts = _hf_modules(w) + + def route(x): + _, weights, ids = router(x) + return ids, weights + + def call(x, mod): + _, weights, ids = router(x) + return mod(x, ids, weights) + + def fwd(x): + with torch.no_grad(): + return call(x, experts) + + def graph(x): + leaf = x.detach().clone().requires_grad_(True) + return call(leaf, experts), [leaf, experts.gate_up_proj, experts.down_proj] + + return { + "name": "HF transformers Qwen3NextExperts (eager)", + "source": f"transformers {transformers.__version__} modeling_qwen3_next", + "route": route, + "fwd": fwd, + "graph": graph, + } + + +def _vllm(w, batch_invariant: bool): + import vllm + from vllm.model_executor.layers.fused_moe.fused_moe import fused_experts + from vllm.model_executor.layers.fused_moe.router.fused_topk_router import fused_topk + + flag = "1" if batch_invariant else "0" + linear = F.linear + if batch_invariant: + from vllm.model_executor.determinism.batch_invariant import linear_batch_invariant + + linear = linear_batch_invariant + + def route(x): + with _env("VLLM_BATCH_INVARIANT", flag): + # vLLM's Qwen3-Next gate is a BF16 ReplicatedLinear; batch-invariant + # mode replaces its aten GEMM with vLLM's persistent Triton matmul. + logits = linear(x, w["router"]) + weights, ids, _ = fused_topk(x, logits, TOPK, renormalize=True) + return ids.long(), weights + + def fwd(x): + with torch.no_grad(), _env("VLLM_BATCH_INVARIANT", flag): + ids, weights = route(x) + return fused_experts(x, w["gate_up"], w["down"], weights, ids.int()) + + return { + "name": f"vLLM fused_topk + fused_experts (Triton), VLLM_BATCH_INVARIANT={flag}", + "source": f"vllm {vllm.__version__} model_executor/layers/fused_moe", + "route": route, + "fwd": fwd, + } + + +def _flashinfer(w): + import flashinfer + from flashinfer.fused_moe import cutlass_fused_moe + + # CUTLASS SwiGLU takes fc1 as [up; gate] (the opposite half order of HF). + gate, up = w["gate_up"].chunk(2, dim=1) + fc1 = torch.cat((up, gate), dim=1).contiguous() + + def route(x): + probs = F.linear(x, w["router"]).float().softmax(-1) + weights, ids = probs.topk(TOPK, dim=-1) + return ids, weights / weights.sum(-1, keepdim=True) + + def fwd(x): + with torch.no_grad(): + ids, weights = route(x) + out = cutlass_fused_moe( + x, ids.int(), weights, fc1, w["down"], torch.bfloat16, quant_scales=[] + ) + return out[0] if isinstance(out, (list, tuple)) else out + + return { + "name": "FlashInfer cutlass_fused_moe (routes from torch.topk)", + "source": f"flashinfer {flashinfer.__version__} fused_moe.cutlass_fused_moe", + "route": route, + "fwd": fwd, + } + + +def candidates(w) -> list[dict[str, Any]]: + return [ + _guard("rl_kernel_cuda", lambda: _rl_kernel(w)), + _guard("hf_transformers", lambda: _hf(w)), + _guard("vllm_bi0", lambda: _vllm(w, False)), + _guard("vllm_bi1", lambda: _vllm(w, True)), + _guard("flashinfer_cutlass", lambda: _flashinfer(w)), + ] + + +# --------------------------------------------------------------------------- # +# Checks +# --------------------------------------------------------------------------- # + + +def _embedded(x: torch.Tensor, size: int): + """Probe rows first in a batch of ``size``, and the same probe rows last.""" + first = x[:size] + last = x[:size].clone() + last[size - PROBE :] = x[:PROBE] + return first, last + + +def _rows_bi(fn, x) -> dict[str, bool]: + alone = fn(x[:PROBE]) + result = {} + for size in BI_SIZES: + first, last = _embedded(x, size) + a, b = fn(first), fn(last) + if isinstance(alone, tuple): + ok = all( + _bitwise(p[:PROBE], q) and _bitwise(r[size - PROBE :], q) + for p, r, q in zip(a, b, alone) + ) + else: + ok = _bitwise(a[:PROBE], alone) and _bitwise(b[size - PROBE :], alone) + result[str(size)] = bool(ok) + return result + + +def _grad_bi(cand, x) -> dict[str, Any]: + """dx of the probe rows, and dW with zero-gradient rows appended.""" + g = torch.Generator(device="cuda").manual_seed(7) + dy = torch.randn(x.shape, device="cuda", generator=g).to(torch.bfloat16) + + def grads(rows, dy_rows): + y, leaves = cand["graph"](rows) + return torch.autograd.grad(y, leaves, dy_rows) + + alone = grads(x[:PROBE], dy[:PROBE]) + dx, dw = {}, {} + for size in BI_SIZES: + full = grads(x[:size], dy[:size]) + dx[str(size)] = bool(_bitwise(full[0][:PROBE], alone[0])) + padded = dy[:size].clone() + padded[PROBE:] = 0 + masked = grads(x[:size], padded) + dw[str(size)] = bool(all(_bitwise(m, a) for m, a in zip(masked[1:], alone[1:]))) + return {"dx_rows_bitwise": dx, "dweight_zero_rows_bitwise": dw} + + +def _accuracy(cand, x, w) -> dict[str, Any]: + ref, ref_ids = fp64_reference(x, w) + ids, _ = cand["route"](x) + same = ids.long().sort(-1).values == ref_ids.sort(-1).values + out = cand["fwd"](x) + return { + "rel_l2_vs_fp64": _rel(out, ref), + "max_abs_vs_fp64": float((out.double() - ref).abs().max()), + "tokens_with_different_expert_set": int((~same.all(-1)).sum()), + "tokens": int(x.shape[0]), + } + + +def _one(cand, x, w) -> dict[str, Any]: + if "unavailable" in cand: + return cand + out = {"key": cand["key"], "name": cand["name"], "source": cand["source"]} + try: + out["route_rows_bitwise"] = _rows_bi(cand["route"], x) + out["output_rows_bitwise"] = _rows_bi(cand["fwd"], x) + out["repeatable"] = bool(_bitwise(cand["fwd"](x[:256]), cand["fwd"](x[:256]))) + if "graph" in cand: + out.update(_grad_bi(cand, x)) + out["accuracy"] = _accuracy(cand, x[:256], w) + except Exception as exc: # noqa: BLE001 - a crash is a result, not a harness failure + out["failed"] = f"{type(exc).__name__}: {str(exc)[:300]}" + out["batch_invariant"] = bool( + "failed" not in out + and all(out["route_rows_bitwise"].values()) + and all(out["output_rows_bitwise"].values()) + and all(out.get("dx_rows_bitwise", {"": True}).values()) + and all(out.get("dweight_zero_rows_bitwise", {"": True}).values()) + ) + return out + + +def _latency(cands, tokens) -> dict[str, Any]: + ready = [c for c in cands if "unavailable" not in c] + forward = {} + for count in tokens: + x = make_tokens(count, seed=100 + count) + calls = {} + for cand in ready: + try: + cand["fwd"](x) + calls[cand["key"]] = lambda c=cand: c["fwd"](x) + except Exception: # noqa: BLE001 - recorded by the BI section + continue + forward[str(count)] = time_interleaved(calls) + backward = {} + for count in BACKWARD_TOKENS: + x = make_tokens(count, seed=200 + count) + dy = torch.randn_like(x) + calls = {} + for cand in ready: + if "graph" not in cand: + continue + + def step(c=cand): + y, leaves = c["graph"](x) + torch.autograd.grad(y, leaves, dy) + + calls[cand["key"]] = step + backward[str(count)] = time_interleaved(calls, warmup=2, iters=10) + return {"forward_us": forward, "forward_plus_backward_us": backward} + + +def moe_report() -> dict[str, Any]: + torch.manual_seed(0) + w = make_weights() + x = make_tokens(max(BI_SIZES), seed=1) + cands = candidates(w) + return { + "op": "qwen3_next_routed_moe", + "shape": {"hidden": HIDDEN, "experts": EXPERTS, "top_k": TOPK, "expert_width": WIDTH}, + "probe_rows": PROBE, + "candidates": [_one(c, x, w) for c in cands], + "latency": _latency(cands, PERF_TOKENS), + } diff --git a/rl_engine/testing/tensor_identity.py b/rl_engine/testing/tensor_identity.py new file mode 100644 index 000000000..5d05c0fe9 --- /dev/null +++ b/rl_engine/testing/tensor_identity.py @@ -0,0 +1,38 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Finite tensor identity in the original storage dtype, including signed zero.""" + +import torch + + +def tensor_bitwise_equal(actual: torch.Tensor, expected: torch.Tensor) -> bool: + """Compare logical values by bytes without widening or accepting NaN/Inf. + + Strides need not match. A cross-device comparison copies expected bytes to + the actual tensor's device; it never converts either tensor's dtype. + """ + if actual.shape != expected.shape or actual.dtype != expected.dtype: + return False + for value in (actual, expected): + if (value.is_floating_point() or value.is_complex()) and not bool( + torch.isfinite(value).all() + ): + return False + actual_bytes = actual.detach().contiguous().reshape(-1).view(torch.uint8) + expected_bytes = expected.detach().contiguous().reshape(-1).view(torch.uint8) + if actual_bytes.device != expected_bytes.device: + expected_bytes = expected_bytes.to(actual_bytes.device) + return bool(torch.equal(actual_bytes, expected_bytes)) + + +def assert_tensor_bitwise_equal( + actual: torch.Tensor, expected: torch.Tensor, *, name: str = "tensor" +) -> None: + """Require finite, same-dtype, same-shape raw-bit identity.""" + if not tensor_bitwise_equal(actual, expected): + raise AssertionError( + f"{name}: finite raw-bit identity failed; " + f"actual={tuple(actual.shape)}/{actual.dtype}, " + f"expected={tuple(expected.shape)}/{expected.dtype}" + ) diff --git a/scripts/plot_qwen3_next_moe_prior_art.py b/scripts/plot_qwen3_next_moe_prior_art.py new file mode 100644 index 000000000..17a966b61 --- /dev/null +++ b/scripts/plot_qwen3_next_moe_prior_art.py @@ -0,0 +1,159 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Render the routed-MoE prior-art report (``scripts/qwen3_next_moe_prior_art.py``) as a PNG. + +Needs only the report JSON and matplotlib, so it runs anywhere: + + python scripts/plot_qwen3_next_moe_prior_art.py \\ + docs/usage/evidence/qwen3-next-moe-route-b200/report.json + +writes ``figure.png`` next to the report. Three panels: which batch-invariance +checks each implementation passes, accuracy against an FP64 evaluation with +FP64 routing, and forward latency. RL-Kernel is blue, existing libraries orange. +""" + +from __future__ import annotations + +import argparse +import json +from pathlib import Path + +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt # noqa: E402 +from matplotlib.colors import ListedColormap # noqa: E402 + +RL_KERNEL = "#2a78d6" +INK = "#2b2b29" +MUTED = "#6f6e69" +GRID = "#e4e3dd" +SURFACE = "#fcfcfb" +PASS = "#1baf7a" +FAIL = "#d64545" +NA = "#e4e3dd" +EXISTING = ("#eb6834", "#b8501f", "#f29a62", "#8a3a15") + +LABELS = { + "rl_kernel_cuda": "RL-Kernel shared_moe", + "hf_transformers": "HF Qwen3NextExperts", + "vllm_bi0": "vLLM fused_moe (BI=0)", + "vllm_bi1": "vLLM fused_moe (BI=1)", + "flashinfer_cutlass": "FlashInfer cutlass_fused_moe", +} +CHECKS = ( + ("route_rows_bitwise", "routes"), + ("output_rows_bitwise", "output"), + ("dx_rows_bitwise", "dx"), + ("dweight_zero_rows_bitwise", "dW"), +) + + +def _colors(keys): + existing = iter(EXISTING) + return {k: RL_KERNEL if k == "rl_kernel_cuda" else next(existing) for k in keys} + + +def _style(ax): + ax.set_facecolor(SURFACE) + for side in ("top", "right"): + ax.spines[side].set_visible(False) + ax.tick_params(colors=INK, labelsize=8) + ax.grid(color=GRID, linewidth=0.6) + + +def plot(report: dict, out: Path) -> None: + cands = [c for c in report["candidates"] if "unavailable" not in c] + keys = [c["key"] for c in cands] + colors = _colors(keys) + sizes = list(next(iter(cands))["output_rows_bitwise"]) + fig, axes = plt.subplots(1, 3, figsize=(17, 4.6), gridspec_kw={"width_ratios": [1.5, 1, 1.4]}) + fig.patch.set_facecolor("white") + + # Panel 1: batch-invariance matrix; 1 = pass, 0 = fail, 0.5 = no backward. + ax = axes[0] + columns = [f"{label} {size}" for _, label in CHECKS for size in sizes] + grid = [] + for cand in cands: + row = [] + for field, _ in CHECKS: + values = cand.get(field) + row.extend(0.5 if values is None else float(values[s]) for s in sizes) + grid.append(row) + ax.imshow(grid, cmap=ListedColormap([FAIL, NA, PASS]), vmin=0, vmax=1, aspect="auto") + ax.set_xticks(range(len(columns)), columns, fontsize=7, rotation=90) + ax.set_yticks(range(len(cands)), [LABELS.get(k, k) for k in keys], fontsize=8) + for i, cand in enumerate(cands): + mark = "BI" if cand.get("batch_invariant") else "not BI" + ax.text(len(columns) - 0.4, i, f" {mark}", va="center", fontsize=8, color=INK) + ax.set_title( + f"Batch invariance: {report['probe_rows']} probe tokens alone vs. in a batch\n" + "(green bitwise equal, red differs, grey: no backward)", + fontsize=9, + color=INK, + ) + ax.tick_params(length=0) + + # Panel 2: accuracy. + ax = axes[1] + _style(ax) + rel = [c["accuracy"]["rel_l2_vs_fp64"] if "accuracy" in c else float("nan") for c in cands] + ax.barh(range(len(cands)), rel, color=[colors[k] for k in keys]) + for i, cand in enumerate(cands): + acc = cand.get("accuracy") + if acc: + ax.text( + rel[i], + i, + f" {rel[i]:.2e} ({acc['tokens_with_different_expert_set']}/{acc['tokens']}" + " tokens route differently)", + va="center", + fontsize=7, + color=MUTED, + ) + ax.set_yticks(range(len(cands)), [LABELS.get(k, k) for k in keys], fontsize=8) + ax.invert_yaxis() + ax.set_xlim(0, 2.2 * max(r for r in rel if r == r)) + ax.set_xlabel("relative L2 error vs. FP64 HF formula with FP64 routing", fontsize=8) + ax.set_title("Accuracy (256 tokens)", fontsize=9, color=INK) + + # Panel 3: forward latency. + ax = axes[2] + _style(ax) + forward = report["latency"]["forward_us"] + tokens = [int(t) for t in forward] + for key in keys: + ys = [forward[str(t)].get(key, float("nan")) / 1000.0 for t in tokens] + ax.plot(tokens, ys, marker="o", ms=3.5, color=colors[key], label=LABELS.get(key, key)) + ax.set_xscale("log", base=2) + ax.set_yscale("log") + ax.set_xlabel("tokens", fontsize=8) + ax.set_ylabel("forward latency (ms, median)", fontsize=8) + ax.set_title("Forward latency\n(H=2048, 512 experts, top-10, width 512)", fontsize=9) + ax.legend(fontsize=7, frameon=False) + + env = report["environment"] + fig.suptitle( + f"Qwen3-Next routed MoE prior art - {env['gpu']}, torch {env['torch']}, " + f"commit {report['rl_kernel_commit'][:7]}", + fontsize=10, + color=INK, + ) + fig.tight_layout() + fig.savefig(out, dpi=160) + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("report", type=Path) + args = parser.parse_args() + report = json.loads(args.report.read_text()) + out = args.report.with_name("figure.png") + plot(report, out) + print(f"wrote {out}") + + +if __name__ == "__main__": + main() diff --git a/scripts/qwen3_next_moe_prior_art.py b/scripts/qwen3_next_moe_prior_art.py new file mode 100644 index 000000000..398245eee --- /dev/null +++ b/scripts/qwen3_next_moe_prior_art.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Write the Qwen3-Next routed-MoE prior-art report (BI + accuracy + latency) as JSON. + +Run it on a clean tree with ``TRITON_F32_DEFAULT=ieee`` (the FP32 router needs +IEEE Triton dots from process start). The report records the commit, whether +the tracked tree was dirty and the version of every library compared. +``scripts/plot_qwen3_next_moe_prior_art.py`` turns the report into a figure. + + TRITON_F32_DEFAULT=ieee python scripts/qwen3_next_moe_prior_art.py \\ + --out docs/usage/evidence/qwen3-next-moe-route-b200/report.json +""" + +from __future__ import annotations + +import argparse +import importlib.metadata as md +import json +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) + +import torch # noqa: E402 + +from rl_engine.testing.qwen3_next_moe_prior_art import moe_report # noqa: E402 + +LIBRARIES = ( + "vllm", + "flashinfer-python", + "transformers", + "triton", + "sglang", + "megatron-core", + "transformer_engine", + "vime", +) + + +def _git(*args: str) -> str: + return subprocess.run( + ["git", *args], cwd=ROOT, check=True, capture_output=True, text=True + ).stdout.strip() + + +def _version(name: str) -> str | None: + try: + return md.version(name) + except md.PackageNotFoundError: + return None + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--out", type=Path, required=True) + args = parser.parse_args() + if not torch.cuda.is_available(): + raise SystemExit("needs a CUDA device") + torch.backends.cuda.matmul.allow_tf32 = False + torch.backends.cudnn.allow_tf32 = False + report = { + "kind": "qwen3_next_prior_art_report", + "rfc": 428, + "rl_kernel_commit": _git("rev-parse", "HEAD"), + "tracked_tree_dirty": bool(_git("status", "--porcelain", "--untracked-files=no")), + "environment": { + "gpu": torch.cuda.get_device_name(), + "torch": torch.__version__, + "cuda": torch.version.cuda, + "libraries": {name: _version(name) for name in LIBRARIES}, + }, + **moe_report(), + } + args.out.parent.mkdir(parents=True, exist_ok=True) + args.out.write_text(json.dumps(report, indent=2) + "\n") + print( + f"wrote {args.out} (commit {report['rl_kernel_commit'][:7]}, " + f"dirty={report['tracked_tree_dirty']})" + ) + + +if __name__ == "__main__": + main() diff --git a/scripts/qwen3_next_tp_moe_check.py b/scripts/qwen3_next_tp_moe_check.py new file mode 100644 index 000000000..817c07bcc --- /dev/null +++ b/scripts/qwen3_next_tp_moe_check.py @@ -0,0 +1,207 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""TP4 MoE gate on the official checkpoint's layer-0 weights; four GPUs, torchrun. + + TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 scripts/qwen3_next_tp_moe_check.py \\ + --checkpoint --output + +Each rank writes ``rank-.json``. The gate requires, bitwise: + +* the HF load -> local shard -> all-gather -> HF export round trip of all 512 experts; +* identical top-10 routes on every rank (the router is replicated); +* full batch == concatenated chunks, and reordered tokens == reordered output, + for 8, 64, 256 and 1024 tokens; +* the training forward (autograd on) == the no-grad forward; +* identical replicated router and shared-expert-gate gradients on every rank, + and a finite, nonzero gradient for every parameter. +""" + +import argparse +import json +import os +import subprocess +from collections.abc import Mapping +from contextlib import ExitStack +from datetime import timedelta +from importlib.metadata import version +from pathlib import Path + +import torch +import torch.distributed as dist +from safetensors import safe_open + +from rl_engine.integrations.qwen3_next_forward import ( + FORWARD_PROVIDER_ID, + shared_router, + stable_top10_routes, +) +from rl_engine.integrations.qwen3_next_tp_blocks import TP4MoE, assemble_moe_weight, moe_hf_shapes +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + +ROOT = Path(__file__).resolve().parents[1] +TOKEN_COUNTS = (8, 64, 256, 1024) + + +class CheckpointWeights(Mapping): + """Keep shard headers open while reading only the requested layer's tensors.""" + + def __init__(self, checkpoint, prefix, names, stack): + self.root, self.prefix, self.names, self.stack = checkpoint, prefix, tuple(names), stack + self.index = json.loads((checkpoint / "model.safetensors.index.json").read_text())[ + "weight_map" + ] + self.readers = {} + + def __iter__(self): + return iter(self.names) + + def __len__(self): + return len(self.names) + + def __getitem__(self, name): + if name not in self.names: + raise KeyError(name) + key = self.prefix + name + shard = self.index[key] + if shard not in self.readers: + self.readers[shard] = self.stack.enter_context( + safe_open(self.root / shard, framework="pt", device="cpu") + ) + return self.readers[shard].get_tensor(key) + + +def atomic_json(path, payload): + temporary = path.with_suffix(path.suffix + ".partial") + temporary.write_text(json.dumps(payload, indent=2) + "\n") + temporary.replace(path) + + +def gather(value): + peers = [torch.empty_like(value) for _ in range(4)] + dist.all_gather(peers, value.contiguous()) + return peers + + +def check_gradients(module): + result = {} + for name, parameter in module.named_parameters(): + grad = parameter.grad + if grad is None or not torch.isfinite(grad).all() or not grad.count_nonzero(): + raise AssertionError(f"Missing finite nonzero gradient: {name}") + result[name] = float(grad.float().abs().max()) + return result + + +def timed(fn): + start, end = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True) + start.record() + out = fn() + end.record() + torch.cuda.synchronize() + return out, start.elapsed_time(end) * 1000.0 + + +def moe_gate(checkpoint, generator): + module = TP4MoE(group=dist.group.WORLD, device="cuda") + with ExitStack() as stack: + weights = CheckpointWeights(checkpoint, "model.layers.0.mlp.", moe_hf_shapes(), stack) + module.load_hf_weights(weights) + for name, value in module.export_local_hf_weights().items(): + restored = assemble_moe_weight(name, [peer.cpu() for peer in gather(value)]) + exact(restored, weights[name], name=f"MoE HF {name}") + cases = ["hf-load-export-all512experts"] + forward_us = {} + with torch.no_grad(): + for length in TOKEN_COUNTS: + hidden = ( + torch.randn(length, 2048, device="cuda", dtype=torch.bfloat16, generator=generator) + * 0.1 + ) + # Every rank draws the same tokens from the same generator; verify it. + for peer in gather(hidden): + exact(peer, hidden, name="replicated input") + routes = stable_top10_routes(shared_router(hidden, module.gate.weight)) + for peer in gather(routes.indices): + exact(peer, routes.indices, name=f"routes across ranks {length}") + module(hidden) + whole, forward_us[str(length)] = timed(lambda: module(hidden)) + cut = min(65, length - 1) + exact( + torch.cat((module(hidden[:cut]), module(hidden[cut:]))), + whole, + name=f"MoE full/chunk {length}", + ) + order = torch.arange(length - 1, -1, -1, device="cuda") + exact(module(hidden[order]), whole[order], name=f"MoE reorder {length}") + cases.extend((f"routes-replicated-{length}", f"full-chunk-reorder-{length}")) + hidden = ( + torch.randn(16, 2048, device="cuda", dtype=torch.bfloat16, generator=generator) * 0.1 + ).requires_grad_() + with torch.no_grad(): + expected = module(hidden) + output = module(hidden) + exact(output, expected, name="MoE independent training forward") + output.float().square().mean().backward() + if ( + hidden.grad is None + or not torch.isfinite(hidden.grad).all() + or not hidden.grad.count_nonzero() + ): + raise AssertionError("MoE input gradient is missing") + gradients = check_gradients(module) + for parameter in (module.gate.weight, module.shared_expert_gate.weight): + for peer in gather(parameter.grad): + exact(peer, parameter.grad, name="MoE replicated router/gate gradient") + cases.extend(("training-forward", "all-parameter-gradient", "replicated-router-gradient")) + return {"cases": cases, "gradient_max_abs": gradients, "forward_us": forward_us} + + +def _git(*args): + return subprocess.run( + ["git", *args], cwd=ROOT, check=True, capture_output=True, text=True + ).stdout.strip() + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--checkpoint", required=True, type=Path) + parser.add_argument("--output", required=True, type=Path) + args = parser.parse_args() + rank = int(os.environ["LOCAL_RANK"]) + if int(os.environ["WORLD_SIZE"]) != 4 or torch.cuda.device_count() < 4: + raise RuntimeError("The TP4 MoE gate requires exactly four ranks on four GPUs") + torch.cuda.set_device(rank) + dist.init_process_group( + "nccl", timeout=timedelta(minutes=15), device_id=torch.device("cuda", rank) + ) + try: + generator = torch.Generator(device="cuda").manual_seed(1234) + moe = moe_gate(args.checkpoint, generator) + args.output.mkdir(parents=True, exist_ok=True) + atomic_json( + args.output / f"rank-{rank}.json", + { + "status": "passed", + "scope": "tp4_moe_block_layer0", + "rank": rank, + "rl_kernel_commit": _git("rev-parse", "HEAD"), + "tracked_tree_dirty": bool(_git("status", "--porcelain", "--untracked-files=no")), + "provider": FORWARD_PROVIDER_ID, + "environment": { + "gpu": torch.cuda.get_device_name(), + "torch": torch.__version__, + "vllm": version("vllm"), + "nccl": ".".join(map(str, torch.cuda.nccl.version())), + }, + "moe": moe, + }, + ) + dist.barrier() + finally: + dist.destroy_process_group() + + +if __name__ == "__main__": + main() diff --git a/tests/check_qwen3_next_forward.py b/tests/check_qwen3_next_forward.py new file mode 100644 index 000000000..ce4beb1f7 --- /dev/null +++ b/tests/check_qwen3_next_forward.py @@ -0,0 +1,238 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Mandatory shared CUDA GEMM and MoE checks; these do not claim model L2.""" + +import pytest +import torch +import torch.nn.functional as F + +from rl_engine.integrations.qwen3_next_forward import ( + combine_routes, + shared_linear, + shared_moe, + stable_top10_routes, +) +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +def random(shape, seed=801, *, grad=False, dtype=torch.bfloat16): + assert torch.cuda.is_available(), "Mandatory shared-forward CUDA gates cannot skip" + gen = torch.Generator(device="cuda").manual_seed(seed) + return ( + torch.randn(shape, generator=gen, device="cuda", dtype=dtype) + .mul_(0.02) + .requires_grad_(grad) + ) + + +@pytest.mark.parametrize( + "k,n", [(2048, 3072), (2048, 16), (1024, 2048), (2048, 256), (128, 2048), (2048, 37984)] +) +@torch.no_grad() +def test_qwen_linear_shapes_batch_chunk_and_reorder(k, n): + x, weight = random((17, k)), random((n, k), seed=802) + output = shared_linear(x, weight) + exact( + torch.cat( + [ + shared_linear(x[:1], weight), + shared_linear(x[1:4], weight), + shared_linear(x[4:], weight), + ] + ), + output, + ) + order = torch.arange(16, -1, -1, device="cuda") + exact(shared_linear(x[order], weight), output[order]) + + +def test_shared_linear_vjp_and_empty_tokens(): + x, weight, bias = ( + random((4, 2048), grad=True), + random((16, 2048), 802, grad=True), + random((16,), 803, grad=True), + ) + output = shared_linear(x, weight, bias) + grad = random(output.shape, 804) + output.backward(grad) + refs = [value.detach().double().requires_grad_() for value in (x, weight, bias)] + F.linear(*refs).backward(grad.double()) + for value, ref in zip((x, weight, bias), refs): + torch.testing.assert_close(value.grad.float(), ref.grad.float(), atol=0.002, rtol=0.016) + assert (value.grad.double() - ref.grad).norm() / ref.grad.norm() < 0.016 + with torch.no_grad(): + exact(output, shared_linear(x, weight, bias)) + empty = random((0, 2048), grad=True) + weight.grad = bias.grad = None + shared_linear(empty, weight, bias).sum().backward() + assert empty.grad.shape == empty.shape + assert torch.count_nonzero(weight.grad) == 0 and torch.count_nonzero(bias.grad) == 0 + + +def test_stable_route_tie_boundary_and_router_gradient(): + logits = random((4, 512), dtype=torch.float32) + logits.zero_().requires_grad_() + routes = stable_top10_routes(logits) + assert torch.equal(routes.indices, torch.arange(10, device="cuda").expand(4, 10)) + exact(routes.weights, torch.full((4, 10), 0.1, device="cuda")) + (routes.weights * torch.arange(10, device="cuda")).sum().backward() + assert torch.isfinite(logits.grad).all() and logits.grad.abs().max() > 0 + bad = logits.detach().clone() + bad[0, 0] = torch.nan + with pytest.raises(ValueError, match="finite"): + stable_top10_routes(bad) + + +def test_combine_has_fixed_route_order_and_differentiable_weights(): + outputs = random((4, 10, 2048), grad=True) + weights = random((4, 10), 802, dtype=torch.float32, grad=True) + actual = combine_routes(outputs, weights) + exact( + torch.cat([combine_routes(outputs[i : i + 1], weights[i : i + 1]) for i in range(4)]), + actual, + ) + actual.float().square().sum().backward() + assert weights.grad.abs().max() > 0 and outputs.grad.abs().max() > 0 + refs = [value.detach().double().requires_grad_() for value in (outputs, weights)] + expected = (refs[0] * refs[1].unsqueeze(-1)).sum(dim=1) + expected.backward((2 * actual.float()).double()) + for value, reference in zip((outputs, weights), refs): + torch.testing.assert_close( + value.grad.float(), reference.grad.float(), atol=0.002, rtol=0.016 + ) + + +def test_qwen_moe_actual_tp4_shapes_and_independent_training_forward(): + x = random((4, 2048), grad=True) + router = random((512, 2048), 802, grad=True) + gate_up = random((512, 256, 2048), 803, grad=True) + down = random((512, 2048, 128), 804, grad=True) + output, routes = shared_moe(x, router, gate_up, down) + with torch.no_grad(): + row_output, row_routes = shared_moe(x[:1], router, gate_up, down) + exact(row_output, output[:1]) + exact(row_routes.weights, routes.weights[:1]) + assert torch.equal(row_routes.indices, routes.indices[:1]) + output.float().square().sum().backward() + for value in (x, router, gate_up, down): + assert value.grad is not None and torch.isfinite(value.grad).all() + assert value.grad.abs().max() > 0 + active = routes.indices.unique() + inactive = torch.ones(512, dtype=torch.bool, device="cuda") + inactive[active] = False + assert torch.count_nonzero(gate_up.grad[inactive]) == 0 + assert torch.count_nonzero(down.grad[inactive]) == 0 + + +def test_moe_vjp_matches_independent_double_formula(): + # A small hidden width isolates the derivative; the previous gate covers + # the unmodified official TP4 widths and expert count. + x = random((2, 32)).mul_(10).requires_grad_() + router = random((512, 32), 802).mul_(5).requires_grad_() + gate_up = random((512, 16, 32), 803).mul_(10).requires_grad_() + down = random((512, 32, 8), 804).mul_(10).requires_grad_() + actual, routes = shared_moe(x, router, gate_up, down) + grad = random(actual.shape, 805) + actual.backward(grad) + xr, rr, gu, dr = [v.detach().double().requires_grad_() for v in (x, router, gate_up, down)] + probability = (xr @ rr.t()).softmax(-1) + selected = probability.gather(1, routes.indices) + selected = selected / selected.sum(-1, keepdim=True) + rows = [] + for token in range(2): + slots = [] + for slot, expert in enumerate(routes.indices[token].tolist()): + gate, up = F.linear(xr[token], gu[expert]).chunk(2) + slots.append(F.linear(F.silu(gate) * up, dr[expert]) * selected[token, slot]) + rows.append(torch.stack(slots).sum(0)) + torch.stack(rows).backward(grad.double()) + for value, reference in zip((x, router, gate_up, down), (xr, rr, gu, dr)): + assert torch.isfinite(value.grad).all() + # BF16 intermediates are rounded by the actual provider; this compares + # derivative correctness, never the raw-bit L1/L2 acceptance criterion. + torch.testing.assert_close( + value.grad.float(), reference.grad.float(), atol=0.00002, rtol=0.016 + ) + assert (value.grad.double() - reference.grad).norm() / reference.grad.norm() < 0.016 + + +@torch.no_grad() +def test_route_weights_are_batch_invariant(): + logits = random((32, 512), 805, dtype=torch.float32).mul_(100) + whole = stable_top10_routes(logits) + for count in (1, 8, 17): + part = stable_top10_routes(logits[:count]) + assert torch.equal(part.indices, whole.indices[:count]) + exact(part.weights, whole.weights[:count]) + moe_x, router = random((32, 2048), 806), random((512, 2048), 807) + gate_up, down = random((512, 256, 2048), 808), random((512, 2048, 128), 809) + whole_out, _ = shared_moe(moe_x, router, gate_up, down) + part_out, _ = shared_moe(moe_x[:8], router, gate_up, down) + exact(part_out, whole_out[:8]) + + +class _PerExpertReference(torch.autograd.Function): + """The previous provider: one pinned vLLM GEMM pair per expert, in expert order.""" + + @staticmethod + def forward(ctx, x, gate_up, down, route_indices): + from rl_engine.integrations.qwen3_next_forward import _vllm_linear + + output = x.new_empty((x.shape[0] * 10, x.shape[1])) + flat = route_indices.flatten() + for expert in flat.unique().tolist(): + slots = (flat == expert).nonzero().squeeze(1) + rows = x.index_select(0, slots // 10) + gate, up = _vllm_linear()(rows, gate_up[expert]).chunk(2, dim=-1) + activated = (F.silu(gate.float()) * up.float()).to(x.dtype) + output.index_copy_(0, slots, _vllm_linear()(activated, down[expert])) + return output.reshape(x.shape[0], 10, x.shape[1]) + + +@torch.no_grad() +@pytest.mark.parametrize("width", [128, 512]) +@pytest.mark.parametrize("tokens", [1, 7, 13, 300]) +def test_grouped_experts_are_bitwise_the_per_expert_gemms(width, tokens): + from rl_engine.integrations.qwen3_next_forward import _RoutedExperts, shared_router + + x, router = random((tokens, 2048), 811), random((512, 2048), 812) + gate_up, down = random((512, 2 * width, 2048), 813), random((512, 2048, width), 814) + indices = stable_top10_routes(shared_router(x, router)).indices + exact( + _RoutedExperts.apply(x, gate_up, down, indices), + _PerExpertReference.apply(x, gate_up, down, indices), + ) + + +@torch.no_grad() +@pytest.mark.parametrize("width", [128, 512]) +def test_grouped_projection_is_bitwise_the_per_expert_gemm(width): + # The backward recomputes [gate, up] with the grouped kernel; equality here + # leaves every gradient bitwise what the per-expert backward produced. + from rl_engine.integrations.qwen3_next_forward import ( + _vllm_linear, + grouped_route_linear, + shared_router, + ) + + x, router = random((300, 2048), 817), random((512, 2048), 818) + gate_up = random((512, 2 * width, 2048), 819) + indices = stable_top10_routes(shared_router(x, router)).indices + grouped = grouped_route_linear(x, gate_up, indices).reshape(-1, 2 * width) + flat = indices.flatten() + for expert in flat.unique().tolist(): + slots = (flat == expert).nonzero().squeeze(1) + exact(grouped[slots], _vllm_linear()(x[slots // 10], gate_up[expert]), name=f"e{expert}") + + +def test_grouped_experts_fail_closed_on_the_tensor_descriptor_path(monkeypatch): + from rl_engine.integrations import qwen3_next_forward as provider + + prepare, dispatch, _ = provider._vllm_grouped() + monkeypatch.setattr(provider, "_vllm_grouped", lambda: (prepare, dispatch, lambda: True)) + x = random((4, 2048), 815) + with pytest.raises(RuntimeError, match="VLLM_TRITON_USE_TD"): + provider.grouped_route_linear( + x, random((512, 256, 2048), 816), torch.zeros(4, 10, device="cuda", dtype=torch.long) + ) diff --git a/tests/test_qwen3_next_forward_contract.py b/tests/test_qwen3_next_forward_contract.py new file mode 100644 index 000000000..76d6df183 --- /dev/null +++ b/tests/test_qwen3_next_forward_contract.py @@ -0,0 +1,62 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""CPU checks of the strict shared-forward boundary, without a CPU fallback.""" + +import pytest +import torch + +from rl_engine.integrations import qwen3_next_forward as provider + + +@pytest.mark.parametrize( + "call", + [ + lambda: provider.shared_linear(torch.zeros(1, 8), torch.zeros(8, 8)), + lambda: provider.stable_top10_routes(torch.zeros(1, 512)), + lambda: provider.combine_routes(torch.zeros(1, 10, 8), torch.zeros(1, 10)), + ], +) +def test_required_cuda_primitives_reject_cpu(call): + with pytest.raises(ValueError, match="no CPU fallback"): + call() + + +def test_shared_linear_rejects_an_unpinned_vllm(monkeypatch): + provider._vllm_linear.cache_clear() + monkeypatch.setattr(provider, "version", lambda package: "0.30.1") + with pytest.raises(RuntimeError, match="pinned vLLM 0.30.0"): + provider._vllm_linear() + provider._vllm_linear.cache_clear() + + +def _rows(count, width, seed): + generator = torch.Generator().manual_seed(seed) + return torch.randn((count, width), generator=generator, dtype=torch.float32) * 4 + + +@pytest.mark.parametrize("width", [1, 2, 7, 10, 512]) +def test_fixed_order_row_sum_matches_sequential_double_sum(width): + values = _rows(5, width, 901) + actual = provider.fixed_order_row_sum(values) + assert actual.shape == (5, 1) + reference = values.double().sum(-1, keepdim=True).float() + torch.testing.assert_close(actual, reference, rtol=1e-5, atol=1e-4) + + +@pytest.mark.parametrize("width", [10, 512]) +def test_fixed_order_row_sum_is_independent_of_row_count(width): + values = _rows(32, width, 902) + whole = provider.fixed_order_row_sum(values) + for count in (1, 8, 17): + assert torch.equal(provider.fixed_order_row_sum(values[:count]), whole[:count]) + + +def test_fixed_order_softmax_rows_do_not_depend_on_batch(): + logits = _rows(32, 512, 903) + whole = provider.fixed_order_softmax(logits) + torch.testing.assert_close(whole.double().sum(-1), torch.ones(32, dtype=torch.float64)) + for count in (1, 8): + assert torch.equal(provider.fixed_order_softmax(logits[:count]), whole[:count]) + # The row sum is a fixed sequential chain, so each result is a pure function of + # its own row and cannot depend on how many rows share the call. diff --git a/tests/test_qwen3_next_tp_blocks.py b/tests/test_qwen3_next_tp_blocks.py new file mode 100644 index 000000000..f2ac9c822 --- /dev/null +++ b/tests/test_qwen3_next_tp_blocks.py @@ -0,0 +1,93 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""HF mappings, replica checks and official Qwen3-Next MoE dimensions.""" + +import pytest +import torch + +from rl_engine.integrations import qwen3_next_tp +from rl_engine.integrations import qwen3_next_tp_blocks as blocks +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +def pattern(shape): + return ( + torch.arange(torch.Size(shape).numel(), dtype=torch.int32) + .remainder(251) + .to(torch.bfloat16) + .reshape(shape) + ) + + +@pytest.mark.parametrize( + "name", + [ + "gate.weight", + "shared_expert_gate.weight", + "shared_expert.gate_proj.weight", + "shared_expert.up_proj.weight", + "shared_expert.down_proj.weight", + "experts.0.gate_proj.weight", + "experts.0.up_proj.weight", + "experts.511.down_proj.weight", + ], +) +def test_moe_hf_weight_roundtrip(name): + value = pattern(blocks.moe_hf_shapes()[name]) + shards = [blocks.shard_moe_weight(name, value, rank) for rank in range(4)] + exact(blocks.assemble_moe_weight(name, shards), value, name=name) + if name.endswith("down_proj.weight"): + assert shards[0].shape == (2048, 128) + elif name.endswith(("gate_proj.weight", "up_proj.weight")): + assert shards[0].shape == (128, 2048) + + +def test_moe_export_rejects_router_replica_drift(): + values = [pattern((512, 2048)) for _ in range(4)] + values[2][0, 0] = -1 + with pytest.raises(ValueError, match="Replicated MoE"): + blocks.assemble_moe_weight("gate.weight", values) + + +@pytest.mark.parametrize("rank", [-1, 4, True, 0.5]) +def test_invalid_shard_rank_rejected(rank): + with pytest.raises(ValueError, match="TP4 rank"): + blocks.shard_moe_weight("gate.weight", torch.empty(512, 2048, dtype=torch.bfloat16), rank) + + +def test_official_shared_expert_width_is_512_and_each_expert_has_three_weights(): + shapes = blocks.moe_hf_shapes() + assert len(shapes) == 512 * 3 + 5 + assert shapes["shared_expert.gate_proj.weight"] == (512, 2048) + assert shapes["shared_expert.down_proj.weight"] == (2048, 512) + + +def test_tp4_moe_rejects_a_missing_or_wrong_size_tp_group(monkeypatch): + monkeypatch.setattr(qwen3_next_tp.dist, "is_initialized", lambda: False) + with pytest.raises(ValueError, match="four-rank TP group"): + blocks.TP4MoE(group=None, device="meta") + monkeypatch.setattr(qwen3_next_tp.dist, "is_initialized", lambda: True) + monkeypatch.setattr(qwen3_next_tp.dist, "get_world_size", lambda group: 8) + with pytest.raises(ValueError, match="four-rank TP group"): + blocks.TP4MoE(group=None, device="meta") + + +def test_tp4_moe_parameter_ownership(monkeypatch): + group = object() + monkeypatch.setattr(qwen3_next_tp.dist, "is_initialized", lambda: True) + monkeypatch.setattr(qwen3_next_tp.dist, "get_world_size", lambda group: 4) + monkeypatch.setattr(blocks.dist, "get_rank", lambda group: 2) + module = blocks.TP4MoE(group=group, device="meta") + assert module.rank == 2 + assert module.experts.gate_up.shape == (512, 256, 2048) + assert module.experts.down.shape == (512, 2048, 128) + assert ( + module.experts.gate_up.partition_dim == 1 and module.experts.gate_up.partition_stride == 2 + ) + assert module.experts.down.partition_dim == 2 + assert module.shared_expert.down_proj.weight.partition_dim == 1 + # Router and shared-expert gate are replicated: no TP attributes, full shapes. + assert not getattr(module.gate.weight, "tensor_model_parallel", False) + assert not getattr(module.shared_expert_gate.weight, "tensor_model_parallel", False) + assert module.gate.weight.shape == (512, 2048) diff --git a/tests/test_tensor_identity.py b/tests/test_tensor_identity.py new file mode 100644 index 000000000..f85c0c4dd --- /dev/null +++ b/tests/test_tensor_identity.py @@ -0,0 +1,41 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""CPU regressions for the raw-bit identity used by Qwen3-Next acceptance checks.""" + +import pytest +import torch + +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal, tensor_bitwise_equal + + +@pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16, torch.float16]) +def test_signed_zero_is_not_identical(dtype): + positive = torch.tensor([0.0], dtype=dtype) + negative = torch.tensor([-0.0], dtype=dtype) + assert not tensor_bitwise_equal(positive, negative) + with pytest.raises(AssertionError, match="raw-bit"): + assert_tensor_bitwise_equal(positive, negative) + + +@pytest.mark.parametrize("value", [float("nan"), float("inf"), -float("inf")]) +def test_identical_nonfinite_values_are_rejected(value): + tensor = torch.tensor([value]) + assert not tensor_bitwise_equal(tensor, tensor) + + +def test_same_numbers_in_different_dtypes_are_rejected(): + assert not tensor_bitwise_equal(torch.ones(2), torch.ones(2, dtype=torch.bfloat16)) + + +def test_adjacent_bfloat16_values_are_rejected(): + value = torch.ones(2, dtype=torch.bfloat16) + changed = (value.view(torch.int16) + 1).view(torch.bfloat16) + assert not tensor_bitwise_equal(value, changed) + + +@pytest.mark.parametrize( + "value", [torch.tensor(1.0), torch.empty(0), torch.arange(12).reshape(3, 4).T] +) +def test_logical_identity_does_not_require_matching_strides(value): + assert_tensor_bitwise_equal(value, value.contiguous().clone()) From fce346d057a9b03d5b93ab77c52f034b47c3d398 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 9 Oct 2026 01:58:09 +0000 Subject: [PATCH 02/68] feat(qwen3-next): TP4 GDN, convolution and D=256 attention mixers Prep for RFC #428 full_model_chain; serves the gdn_state_bridge row. - GDN: packed recurrent prefill/decode through the pinned vLLM 0.30.0 provider with an explicit state boundary (qwen3_next_gdn.py), the depthwise causal convolution with cache handoff (qwen3_next_conv.py), their combined shared core (qwen3_next_provider.py) and the TP4 block with HF GQA-interleaved weight mapping (qwen3_next_tp_gdn.py), now on the shared TP boundaries in qwen3_next_tp.py. - Attention: the deterministic CUDA attention kernel accepts head dim 256 (ROCm stays 128) and validates backward inputs; shared_attention and TP4FullAttention with the KV head replicated on each rank pair, partial RoPE at absolute positions and explicit per-slot KV state. - Real-weight TP4 gates for GDN (layer 0) and attention (layer 3), sharing scripts/qwen3_next_gate_common.py with the MoE gate. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/ci.yml | 2 +- .github/workflows/qwen3-next-provider-gpu.yml | 11 +- .../cuda/attention/deterministic_attention.cu | 25 +- rl_engine/integrations/qwen3_next_conv.py | 133 ++++++++++ rl_engine/integrations/qwen3_next_forward.py | 26 ++ rl_engine/integrations/qwen3_next_gdn.py | 193 ++++++++++++++ rl_engine/integrations/qwen3_next_provider.py | 110 ++++++++ .../integrations/qwen3_next_tp_blocks.py | 241 +++++++++++++++++- rl_engine/integrations/qwen3_next_tp_gdn.py | 159 ++++++++++++ .../ops/cuda/attention/deterministic_attn.py | 12 +- scripts/qwen3_next_gate_common.py | 125 +++++++++ scripts/qwen3_next_tp_attention_check.py | 134 ++++++++++ scripts/qwen3_next_tp_gdn_check.py | 96 +++++++ scripts/qwen3_next_tp_moe_check.py | 127 +-------- tests/check_qwen3_next_attention.py | 110 ++++++++ tests/check_qwen3_next_conv_bridge.py | 96 +++++++ tests/check_qwen3_next_core_matrix.py | 97 +++++++ tests/check_qwen3_next_gdn_bridge.py | 112 ++++++++ tests/check_qwen3_next_gdn_sequence.py | 102 ++++++++ tests/check_qwen3_next_shared_core.py | 83 ++++++ tests/test_qwen3_next_forward_contract.py | 1 + tests/test_qwen3_next_tp_blocks.py | 71 +++++- tests/test_qwen3_next_tp_gdn.py | 65 +++++ 23 files changed, 1993 insertions(+), 138 deletions(-) create mode 100644 rl_engine/integrations/qwen3_next_conv.py create mode 100644 rl_engine/integrations/qwen3_next_gdn.py create mode 100644 rl_engine/integrations/qwen3_next_provider.py create mode 100644 rl_engine/integrations/qwen3_next_tp_gdn.py create mode 100644 scripts/qwen3_next_gate_common.py create mode 100644 scripts/qwen3_next_tp_attention_check.py create mode 100644 scripts/qwen3_next_tp_gdn_check.py create mode 100644 tests/check_qwen3_next_attention.py create mode 100644 tests/check_qwen3_next_conv_bridge.py create mode 100644 tests/check_qwen3_next_core_matrix.py create mode 100644 tests/check_qwen3_next_gdn_bridge.py create mode 100644 tests/check_qwen3_next_gdn_sequence.py create mode 100644 tests/check_qwen3_next_shared_core.py create mode 100644 tests/test_qwen3_next_tp_gdn.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2fb92d641..d2cef82de 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -67,7 +67,7 @@ jobs: run: | python -m pytest rl_engine/tests/test_dispatch.py -v PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python -m pytest tests/test_attention_correctness.py -q -rs - python -m pytest tests/test_forward_invariance.py tests/test_tolerance_contract.py tests/test_ws1_workload.py tests/test_gradient_invariance.py tests/test_elementwise_inventory.py tests/test_four_judgment_matrix.py tests/test_op_checks.py tests/test_operator_inputs.py tests/test_profiler.py tests/test_kv_consistency.py tests/test_ws1_qwen3_dense.py tests/test_ws1_chain_integration.py tests/test_qwen3_next_norm.py tests/test_gdn_state_contract.py tests/test_tensor_identity.py tests/test_qwen3_next_forward_contract.py tests/test_qwen3_next_tp_blocks.py -q + python -m pytest tests/test_forward_invariance.py tests/test_tolerance_contract.py tests/test_ws1_workload.py tests/test_gradient_invariance.py tests/test_elementwise_inventory.py tests/test_four_judgment_matrix.py tests/test_op_checks.py tests/test_operator_inputs.py tests/test_profiler.py tests/test_kv_consistency.py tests/test_ws1_qwen3_dense.py tests/test_ws1_chain_integration.py tests/test_qwen3_next_norm.py tests/test_gdn_state_contract.py tests/test_tensor_identity.py tests/test_qwen3_next_forward_contract.py tests/test_qwen3_next_tp_blocks.py tests/test_qwen3_next_tp_gdn.py -q - name: Run Cross-Configuration Contract Tests (CPU-safe) run: | diff --git a/.github/workflows/qwen3-next-provider-gpu.yml b/.github/workflows/qwen3-next-provider-gpu.yml index 691489402..c045484bd 100644 --- a/.github/workflows/qwen3-next-provider-gpu.yml +++ b/.github/workflows/qwen3-next-provider-gpu.yml @@ -1,7 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 # RFC #428 Qwen3-Next provider comparisons: the C1/C3/C4 norm providers, the C6 -# GDN decode-step goldens and the shared GEMM/MoE route-combine provider, each -# checked against the real vLLM 0.30.0 provider. +# GDN decode-step goldens, the shared GEMM/MoE route-combine provider and the TP4 +# GDN/convolution/attention mixers, each checked against the real vLLM 0.30.0 provider. # # These check_ files import vLLM, so the default pytest collection never runs them # (tests/test_framework_operator_integrations.py asserts vLLM is not imported). This @@ -20,6 +20,7 @@ on: branches: [main] paths: - "rl_engine/kernels/ops/**" + - "csrc/cuda/attention/**" - "rl_engine/integrations/qwen3_next*" - "rl_engine/testing/qwen3_next*" - "tests/check_*qwen3_next*.py" @@ -84,4 +85,8 @@ jobs: env: TRITON_F32_DEFAULT: ieee run: | - python3 -m pytest -q tests/check_qwen3_next_norm_providers.py tests/check_gdn_recurrent_golden.py tests/check_qwen3_next_forward.py + python3 -m pytest -q tests/check_qwen3_next_norm_providers.py tests/check_gdn_recurrent_golden.py \ + tests/check_qwen3_next_forward.py tests/check_qwen3_next_attention.py \ + tests/check_qwen3_next_gdn_bridge.py tests/check_qwen3_next_gdn_sequence.py \ + tests/check_qwen3_next_conv_bridge.py tests/check_qwen3_next_core_matrix.py \ + tests/check_qwen3_next_shared_core.py diff --git a/csrc/cuda/attention/deterministic_attention.cu b/csrc/cuda/attention/deterministic_attention.cu index 82ec824db..aeae9231c 100644 --- a/csrc/cuda/attention/deterministic_attention.cu +++ b/csrc/cuda/attention/deterministic_attention.cu @@ -17,7 +17,6 @@ namespace { -constexpr int64_t kDeterministicAttentionHeadDim = 128; constexpr int kSoftmaxThreads = 256; // --------------------------------------------------------------------------- @@ -220,15 +219,18 @@ void check_deterministic_attention_inputs( const int64_t Hkv = k.size(1); const int64_t Skv = k.size(2); - TORCH_CHECK(D == kDeterministicAttentionHeadDim, - "deterministic_attention: head dim D must be ", - kDeterministicAttentionHeadDim, - ", got ", - D); +#if defined(USE_ROCM) + TORCH_CHECK(D == 128, "deterministic_attention: ROCm head dim D must be 128, got ", D); +#else + TORCH_CHECK(D == 128 || D == 256, + "deterministic_attention: CUDA head dim D must be 128 or 256, got ", D); +#endif TORCH_CHECK(k.size(0) == B && v.size(0) == B, "deterministic_attention: batch size mismatch between q/k/v"); TORCH_CHECK(v.size(1) == Hkv && v.size(2) == Skv && k.size(3) == D && v.size(3) == D, "deterministic_attention: k/v shape mismatch"); + TORCH_CHECK(B > 0 && Hq > 0 && Hkv > 0, + "deterministic_attention: batch size and head counts must be positive"); TORCH_CHECK(Hq % Hkv == 0, "deterministic_attention: Hq (", Hq, @@ -573,6 +575,17 @@ std::vector deterministic_attention_backward( double scale, torch::optional key_padding_mask) { + check_deterministic_attention_inputs(q, k, v, key_padding_mask); + TORCH_CHECK(grad_output.device() == q.device() && grad_output.scalar_type() == q.scalar_type(), + "deterministic_attention_backward: grad_output must match q device and dtype"); + TORCH_CHECK(grad_output.sizes() == q.sizes(), + "deterministic_attention_backward: grad_output must have q shape"); + TORCH_CHECK(P.device() == q.device() && P.scalar_type() == at::kFloat, + "deterministic_attention_backward: P must be FP32 on the input device"); + TORCH_CHECK(P.dim() == 4 && P.size(0) == q.size(0) && P.size(1) == q.size(1) && + P.size(2) == q.size(2) && P.size(3) == k.size(2), + "deterministic_attention_backward: P must have shape [B, Hq, Sq, Skv]"); + const at::cuda::OptionalCUDAGuard device_guard(at::device_of(q)); auto dO = grad_output.contiguous(); diff --git a/rl_engine/integrations/qwen3_next_conv.py b/rl_engine/integrations/qwen3_next_conv.py new file mode 100644 index 000000000..39dd6ad1a --- /dev/null +++ b/rl_engine/integrations/qwen3_next_conv.py @@ -0,0 +1,133 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Differentiable, explicit-state bridge to the pinned vLLM causal-conv provider. + +Forward is the real vLLM update kernel for both prefill chunks and decode. +Backward recomputes the reference recurrence from training inputs. This module +does not install model hooks or advertise backend batch-invariance support. +""" + +from functools import lru_cache +from importlib.metadata import version + +import torch +from torch.autograd.function import once_differentiable + +from rl_engine.kernels.ops.pytorch.linear_attn.causal_conv1d import CausalConv1dUpdateOp +from rl_engine.kernels.ops.pytorch.linear_attn.gated_delta_rule import _validate_state_indices + + +@lru_cache(maxsize=1) +def _provider(): + if version("vllm") != "0.30.0": + raise RuntimeError("Qwen3-Next conv bridge requires the audited vLLM 0.30.0 provider") + from vllm.model_executor.layers.mamba.ops.causal_conv1d import causal_conv1d_update + + return causal_conv1d_update + + +def _lengths(x, state, weight, indices, cu_seqlens): + if x.ndim != 2 or not x.is_cuda or x.dtype != torch.bfloat16 or x.shape[1] == 0: + raise ValueError("x must be CUDA BF16 [tokens, dim] with positive dim") + if weight.shape != (x.shape[1], 4) or weight.dtype != x.dtype or weight.device != x.device: + raise ValueError("weight must be BF16 [dim, 4] on the input device") + if ( + state.ndim != 3 + or state.shape[1:] != (x.shape[1], 3) + or state.device != x.device + or state.dtype != x.dtype + ): + raise ValueError("conv state must be BF16 [blocks, dim, 3] on the input device") + if ( + cu_seqlens.ndim != 1 + or cu_seqlens.device.type != "cpu" + or cu_seqlens.dtype not in (torch.int32, torch.int64) + or cu_seqlens.numel() < 1 + ): + raise ValueError("cu_seqlens must be a nonempty CPU integer vector") + boundaries = cu_seqlens.tolist() + if boundaries[0] != 0 or boundaries[-1] != x.shape[0] or boundaries[-1] >= 2**31: + raise ValueError("cu_seqlens must cover all input tokens and fit int32") + lengths = [end - start for start, end in zip(boundaries, boundaries[1:])] + if any(length < 0 for length in lengths): + raise ValueError("cu_seqlens must be nondecreasing") + _validate_state_indices(indices, len(lengths), state.shape[0], x.device) + if bool((indices <= 0).any().item()): + raise ValueError("sequence cache indices must be positive; zero is reserved") + return lengths + + +class _CausalConvSequence(torch.autograd.Function): + @staticmethod + def forward(ctx, x, state, weight, indices, cu_seqlens): + lengths = _lengths(x, state, weight, indices, cu_seqlens) + ctx.save_for_backward(x, state, weight, indices, cu_seqlens) + new_state = state.contiguous().clone() + output = torch.zeros_like(x) + if x.shape[0]: + # The upstream varlen path subtracts (max_len - sequence_len) from + # its effective cache length, which is for speculative cache layouts. + # A width-1 ordinary cache must instead use the dense sequence path. + # Equal-length groups need neither padding nor a speculative cache. + boundaries = cu_seqlens.tolist() + for length in sorted(set(lengths) - {0}): + active = [i for i, size in enumerate(lengths) if size == length] + packed = torch.stack( + [x[boundaries[seq] : boundaries[seq + 1]].t() for seq in active] + ) + values = _provider()( + packed, + new_state, + weight.contiguous(), + activation="silu", + conv_state_indices=indices[active].to(torch.int32).contiguous(), + out=torch.empty_like(packed), + ) + for row, seq in enumerate(active): + output[boundaries[seq] : boundaries[seq + 1]] = values[row].t() + return output, new_state + + @staticmethod + @once_differentiable + def backward(ctx, grad_output, grad_state): + x, state, weight, indices, cu_seqlens = ctx.saved_tensors + with torch.enable_grad(): + inputs = [tensor.detach().requires_grad_(True) for tensor in (x, state, weight)] + x_ref, state_ref, weight_ref = inputs + pieces = [] + boundaries = cu_seqlens.tolist() + for seq, (start, end) in enumerate(zip(boundaries, boundaries[1:])): + for token in range(start, end): + out, state_ref = CausalConv1dUpdateOp()( + x_ref[token : token + 1], state_ref, weight_ref, indices[seq : seq + 1] + ) + pieces.append(out) + output = torch.cat(pieces) if pieces else x_ref[:0] + grads = torch.autograd.grad( + (output, state_ref), + inputs, + ( + torch.zeros_like(output) if grad_output is None else grad_output, + torch.zeros_like(state_ref) if grad_state is None else grad_state, + ), + allow_unused=True, + ) + return ( + *( + torch.zeros_like(value) if grad is None else grad + for value, grad in zip(inputs, grads) + ), + None, + None, + ) + + +def causal_conv_sequence(x, state, weight, indices, cu_seqlens): + """Return packed output and an independent cache; retain cache gradients. + + Qwen3-Next's width-four, bias-free SiLU convolution is the only contract. + All tensors except CPU ``cu_seqlens`` live on the input CUDA device. The + original cache and inputs are never mutated, including in inference mode. + """ + return _CausalConvSequence.apply(x, state, weight, indices, cu_seqlens) diff --git a/rl_engine/integrations/qwen3_next_forward.py b/rl_engine/integrations/qwen3_next_forward.py index b5a1e20e0..18c892261 100644 --- a/rl_engine/integrations/qwen3_next_forward.py +++ b/rl_engine/integrations/qwen3_next_forward.py @@ -336,3 +336,29 @@ def shared_moe(x, router_weight, gate_up_weights, down_weights): routes = stable_top10_routes(shared_router(x, router_weight)) outputs = _RoutedExperts.apply(x, gate_up_weights, down_weights, routes.indices) return combine_routes(outputs, routes.weights), routes + + +@lru_cache(maxsize=1) +def _attention_op(): + from rl_engine.kernels.ops.cuda.attention.deterministic_attn import DeterministicAttentionOp + + return DeterministicAttentionOp() + + +def shared_attention(q, k, v): + """Qwen TP4 GQA core: BF16 [B,4,Sq,256] over [B,1,Skv,256]. + + Queries must be the final Sq positions of the supplied KV prefix. This + covers full prefill, any contiguous prefill chunk, and one-token decode. + KV tensors can carry the prompt's gradient graph; no state is detached. + """ + _cuda_tensors(q, k, v) + if q.ndim != 4 or k.ndim != 4 or v.ndim != 4: + raise ValueError("Qwen3-Next attention requires four-dimensional Q/K/V") + if q.shape[1] != 4 or k.shape[1] != 1 or q.shape[-1] != 256: + raise ValueError("Qwen3-Next TP4 attention requires Q heads=4, KV heads=1, D=256") + if q.dtype != torch.bfloat16 or k.dtype != q.dtype or v.dtype != q.dtype: + raise ValueError("Qwen3-Next attention inputs must be BF16") + if q.shape[2] > k.shape[2]: + raise ValueError("Query tokens must fit within the supplied KV prefix") + return _attention_op()(q, k, v, causal=True, scale=1 / 16) diff --git a/rl_engine/integrations/qwen3_next_gdn.py b/rl_engine/integrations/qwen3_next_gdn.py new file mode 100644 index 000000000..e7d73c3c4 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_gdn.py @@ -0,0 +1,193 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Experimental training bridge for the pinned vLLM GDN decode provider. + +Forward uses the real packed decode kernel, with a private cache copy. Backward +recomputes the mathematical recurrence from the training inputs and preserves +initial-state gradients. This is a single operator bridge, not a model installer, +prefill/decode equivalence claim, or permission to bypass vLLM's BI guard. +""" + +from functools import lru_cache +from importlib.metadata import version + +import torch +from torch.autograd.function import once_differentiable + +from rl_engine.kernels.ops.pytorch.linear_attn.gated_delta_rule import ( + GatedDeltaRuleRecurrentStepOp, + _validate_state_indices, +) + + +@lru_cache(maxsize=1) +def _provider(): + if version("vllm") != "0.30.0": + raise RuntimeError("Qwen3-Next GDN bridge requires the audited vLLM 0.30.0 provider") + from vllm.third_party.flash_linear_attention.ops import ( + fused_recurrent_gated_delta_rule_packed_decode, + ) + + return fused_recurrent_gated_delta_rule_packed_decode + + +def _validate(qkv, a, b, A_log, dt_bias, state, indices, num_k_heads): + if not qkv.is_cuda or qkv.ndim != 2 or qkv.dtype != torch.bfloat16: + raise ValueError("qkv must be CUDA BF16 [B, D]") + if state.ndim != 4 or state.dtype != torch.float32 or state.shape[-2:] != (128, 128): + raise ValueError("state must be FP32 [blocks, HV, 128, 128]") + if isinstance(num_k_heads, bool) or not isinstance(num_k_heads, int) or num_k_heads <= 0: + raise ValueError("num_k_heads must be a positive integer") + batch, hv = qkv.shape[0], state.shape[1] + if hv <= 0 or hv % num_k_heads or qkv.shape[1] != 2 * num_k_heads * 128 + hv * 128: + raise ValueError("packed qkv width and state heads are inconsistent") + for name, tensor, shape, dtype in ( + ("a", a, (batch, hv), qkv.dtype), + ("b", b, (batch, hv), qkv.dtype), + ("A_log", A_log, (hv,), torch.float32), + ("dt_bias", dt_bias, (hv,), torch.float32), + ): + if tensor.shape != shape or tensor.dtype != dtype or tensor.device != qkv.device: + raise ValueError(f"{name} has an incompatible shape, dtype or device") + if state.device != qkv.device: + raise ValueError("state must share the qkv device") + _validate_state_indices(indices, batch, state.shape[0], qkv.device) + + +class _PackedDecode(torch.autograd.Function): + @staticmethod + def forward(ctx, qkv, a, b, A_log, dt_bias, state, indices, scale, num_k_heads): + _validate(qkv, a, b, A_log, dt_bias, state, indices, num_k_heads) + ctx.save_for_backward(qkv, a, b, A_log, dt_bias, state, indices) + ctx.scale, ctx.num_k_heads = float(scale), num_k_heads + new_state = state.contiguous().clone() + out = torch.zeros(qkv.shape[0], 1, state.shape[1], 128, device=qkv.device, dtype=qkv.dtype) + if qkv.shape[0]: + _provider()( + qkv.contiguous(), + a.contiguous(), + b.contiguous(), + A_log.contiguous(), + dt_bias.contiguous(), + float(scale), + new_state, + out, + indices.contiguous(), + use_qk_l2norm_in_kernel=True, + ) + return out, new_state + + @staticmethod + @once_differentiable + def backward(ctx, grad_out, grad_state): + *saved, indices = ctx.saved_tensors + with torch.enable_grad(): + inputs = [tensor.detach().requires_grad_(True) for tensor in saved] + out, state = GatedDeltaRuleRecurrentStepOp()( + *inputs, + indices, + scale=ctx.scale, + num_k_heads=ctx.num_k_heads, + ) + outputs_and_grads = [ + (value, torch.zeros_like(value) if grad is None else grad) + for value, grad in ((out, grad_out), (state, grad_state)) + if value.requires_grad + ] + grads = torch.autograd.grad( + tuple(value for value, _ in outputs_and_grads), + inputs, + tuple(grad for _, grad in outputs_and_grads), + allow_unused=True, + ) + grads = tuple( + torch.zeros_like(value) if grad is None else grad + for value, grad in zip(inputs, grads) + ) + + return (*grads, None, None, None) + + +def packed_decode_training_step( + qkv, a, b, A_log, dt_bias, state, indices, *, num_k_heads, scale=None +): + """One train-side token; pass returned state onward without detaching it. + + Supports first-order gradients only. FP32 recurrent state and Qwen3-Next + 128-wide heads are mandatory. This function never reads rollout tensors. + """ + return _PackedDecode.apply( + qkv, + a, + b, + A_log, + dt_bias, + state, + indices, + 128**-0.5 if scale is None else float(scale), + num_k_heads, + ) + + +def packed_recurrent_sequence( + qkv, a, b, A_log, dt_bias, state, indices, cu_seqlens, *, num_k_heads, scale=None +): + """Apply the decode provider to packed, variable-length sequences. + + ``cu_seqlens`` is CPU int32/int64 metadata delimiting contiguous sequences + in ``qkv[T, D]``. ``indices[B]`` maps each sequence to a distinct positive + cache slot; slot zero remains reserved by the decode provider. Returning + the entire FP32 state bank makes chunk continuation and sequence reordering + explicit. Callers must pass that state onward without detaching it for + training. No rollout cache is read and no batch-invariance capability is + registered. This intentionally slow recurrence is an integration reference, + not equivalence evidence for the upstream chunk-prefill kernel. + """ + if ( + not isinstance(cu_seqlens, torch.Tensor) + or cu_seqlens.device.type != "cpu" + or cu_seqlens.dtype not in (torch.int32, torch.int64) + or cu_seqlens.ndim != 1 + or cu_seqlens.numel() < 1 + ): + raise ValueError("cu_seqlens must be a nonempty CPU int32/int64 vector") + boundaries = cu_seqlens.tolist() + if qkv.ndim != 2 or boundaries[0] != 0 or boundaries[-1] != qkv.shape[0]: + raise ValueError("cu_seqlens must span all packed qkv tokens, starting at zero") + lengths = [end - start for start, end in zip(boundaries, boundaries[1:])] + if any(length < 0 for length in lengths): + raise ValueError("cu_seqlens must be nondecreasing") + if a.ndim != 2 or b.ndim != 2 or a.shape[0] != qkv.shape[0] or b.shape[0] != qkv.shape[0]: + raise ValueError("a and b must have one row per packed token") + _validate(qkv[:0], a[:0], b[:0], A_log, dt_bias, state, indices[:0], num_k_heads) + _validate_state_indices(indices, len(lengths), state.shape[0], qkv.device) + if bool((indices <= 0).any().item()): + raise ValueError("sequence cache indices must be positive; zero is reserved") + outputs, positions = [], [] + for position in range(max(lengths, default=0)): + active = [seq for seq, length in enumerate(lengths) if position < length] + rows = [boundaries[seq] + position for seq in active] + row_ids = torch.tensor(rows, device=qkv.device, dtype=torch.int64) + seq_ids = torch.tensor(active, device=qkv.device, dtype=torch.int64) + out, state = packed_decode_training_step( + qkv.index_select(0, row_ids), + a.index_select(0, row_ids), + b.index_select(0, row_ids), + A_log, + dt_bias, + state, + indices.index_select(0, seq_ids), + num_k_heads=num_k_heads, + scale=scale, + ) + outputs.append(out[:, 0]) + positions.extend(rows) + if not outputs: + return qkv.new_empty((0, state.shape[1], 128)), state + # Restore packed sequence order after the time-major recurrent traversal. + inverse = [0] * len(positions) + for source, destination in enumerate(positions): + inverse[destination] = source + order = torch.tensor(inverse, device=qkv.device, dtype=torch.int64) + return torch.cat(outputs).index_select(0, order), state diff --git a/rl_engine/integrations/qwen3_next_provider.py b/rl_engine/integrations/qwen3_next_provider.py new file mode 100644 index 000000000..ea568a020 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_provider.py @@ -0,0 +1,110 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Shared Qwen3-Next GDN core with explicit provider and state identities. + +This is the convolution/recurrence/gated-norm boundary, after the projections +and before the output projection. It is not a full attention or model adapter. +The same call can be made under inference_mode or with training autograd. +""" + +from dataclasses import asdict, dataclass + +import torch + +from rl_engine.integrations.qwen3_next_conv import causal_conv_sequence +from rl_engine.integrations.qwen3_next_gdn import packed_recurrent_sequence + + +@dataclass(frozen=True) +class GDNProviderConfig: + """The only implemented profile; other providers cannot silently substitute.""" + + version: str = "qwen3-next-gdn-core-v1" + convolution: str = "vllm-0.30.0-causal-conv-update-equal-length-groups" + recurrence: str = "vllm-0.30.0-packed-recurrent-decode" + gated_norm: str = "rl-engine-cuda-rmsnorm-gated" + recurrent_dtype: str = "float32" + activation_dtype: str = "bfloat16" + head_dim: int = 128 + + def validate(self): + if self != GDNProviderConfig(): + raise ValueError("Unsupported GDN provider configuration") + + def identity(self): + self.validate() + return asdict(self) + + +@dataclass(frozen=True) +class GDNState: + convolution: torch.Tensor + recurrent: torch.Tensor + + +def shared_gdn_core( + qkv, + a, + b, + z, + A_log, + dt_bias, + conv_weight, + norm_weight, + state, + indices, + cu_seqlens, + *, + config, + num_k_heads, + eps=1e-6, +): + """Independently recompute packed training inputs or execute rollout inputs. + + qkv is laid out as contiguous Q, K, V groups. z is [tokens, value_heads, 128]. + Both callers must explicitly supply the same GDNProviderConfig. State is + returned without detachment so a response loss can differentiate through + the prompt. No global vLLM batch-invariance capability is changed. + """ + if not isinstance(config, GDNProviderConfig): + raise ValueError("An explicit GDNProviderConfig is required") + config.validate() + if not isinstance(state, GDNState): + raise ValueError("An explicit convolution and recurrent GDNState is required") + if state.recurrent.ndim != 4: + raise ValueError("Recurrent state must have four dimensions") + if ( + z.shape != (qkv.shape[0], state.recurrent.shape[1], 128) + or z.dtype != torch.bfloat16 + or z.device != qkv.device + ): + raise ValueError("z must be BF16 [tokens, value_heads, 128] on the input device") + if ( + norm_weight.shape != (128,) + or norm_weight.dtype != torch.bfloat16 + or norm_weight.device != qkv.device + ): + raise ValueError("Gated norm weight must be BF16 [128] on the input device") + from rl_engine.kernels.ops.cuda.norm.rmsnorm import Qwen3NextRMSNormGatedCudaOp + + norm = Qwen3NextRMSNormGatedCudaOp() + + convolved, conv_state = causal_conv_sequence( + qkv, state.convolution, conv_weight, indices, cu_seqlens + ) + recurrent, recurrent_state = packed_recurrent_sequence( + convolved, + a, + b, + A_log, + dt_bias, + state.recurrent, + indices, + cu_seqlens, + num_k_heads=num_k_heads, + ) + normed = norm(recurrent.reshape(-1, 128), norm_weight, z.reshape(-1, 128), eps=eps).reshape_as( + z + ) + return normed, GDNState(conv_state, recurrent_state) diff --git a/rl_engine/integrations/qwen3_next_tp_blocks.py b/rl_engine/integrations/qwen3_next_tp_blocks.py index 1c80530d4..7a45c1ad2 100644 --- a/rl_engine/integrations/qwen3_next_tp_blocks.py +++ b/rl_engine/integrations/qwen3_next_tp_blocks.py @@ -1,18 +1,22 @@ # SPDX-License-Identifier: Apache-2.0 # Copyright (c) 2026 RL-Kernel Contributors -"""Official Qwen3-Next TP4 MoE block for shared adapters. +"""Official Qwen3-Next TP4 full-attention and MoE blocks for shared adapters. -The block owns the local arithmetic and differentiable TP boundaries. It does -not register an engine backend or advertise full-model acceptance. +These blocks own the local arithmetic and differentiable TP boundaries. They +do not register an engine backend or advertise full-model acceptance. """ +from collections.abc import Mapping, Sequence +from dataclasses import dataclass +from functools import lru_cache + import torch import torch.distributed as dist import torch.nn.functional as F from torch import nn -from rl_engine.integrations.qwen3_next_forward import shared_linear, shared_moe +from rl_engine.integrations.qwen3_next_forward import shared_attention, shared_linear, shared_moe from rl_engine.integrations.qwen3_next_tp import ( _CopyToTP, _parallel_parameter, @@ -23,6 +27,235 @@ ) from rl_engine.testing.tensor_identity import tensor_bitwise_equal +ATTENTION_HF_SHAPES = { + "q_proj.weight": (8192, 2048), + "k_proj.weight": (512, 2048), + "v_proj.weight": (512, 2048), + "o_proj.weight": (2048, 4096), + "q_norm.weight": (256,), + "k_norm.weight": (256,), +} + + +def shard_attention_weights(weights: Mapping[str, torch.Tensor], rank: int): + """Preserve Q/gate interleaving and replicate each KV head on a rank pair.""" + _rank(rank) + if set(weights) != set(ATTENTION_HF_SHAPES): + raise ValueError("Attention weight names do not match the official HF contract") + result = {} + for name, shape in ATTENTION_HF_SHAPES.items(): + value = weights[name] + _weight(value, shape, name) + if name.startswith(("q_norm.", "k_norm.")): + part = value + elif name in ("k_proj.weight", "v_proj.weight"): + part = value.chunk(2, dim=0)[rank // 2] + else: + part = value.chunk(4, dim=1 if name == "o_proj.weight" else 0)[rank] + result[name] = part.contiguous() + return result + + +def assemble_attention_weights(shards: Sequence[Mapping[str, torch.Tensor]]): + """Invert four local HF mappings and reject divergent replicated storage.""" + if len(shards) != 4 or any(set(shard) != set(ATTENTION_HF_SHAPES) for shard in shards): + raise ValueError("Exactly four complete attention HF shards are required") + result = {} + for name, shape in ATTENTION_HF_SHAPES.items(): + values = [shard[name] for shard in shards] + if name.startswith(("q_norm.", "k_norm.")): + for value in values: + _weight(value, shape, name) + if any(not tensor_bitwise_equal(value, values[0]) for value in values[1:]): + raise ValueError(f"Replicated attention norm differs: {name}") + result[name] = values[0] + elif name in ("k_proj.weight", "v_proj.weight"): + for value in values: + _weight(value, (256, 2048), name) + if not tensor_bitwise_equal(values[0], values[1]) or not tensor_bitwise_equal( + values[2], values[3] + ): + raise ValueError(f"Replicated KV head differs within its rank pair: {name}") + result[name] = torch.cat((values[0], values[2]), dim=0) + else: + axis = 1 if name == "o_proj.weight" else 0 + local_shape = list(shape) + local_shape[axis] //= 4 + for value in values: + _weight(value, tuple(local_shape), name) + result[name] = torch.cat(values, dim=axis) + return result + + +@lru_cache(maxsize=4) +def _kv_replica_groups(group): + ranks = dist.get_process_group_ranks(group) + # Every TP rank creates both groups in the same order. Only members use a + # group's collective, and the rank pairs follow the official KV layout. + return tuple( + dist.new_group(ranks[start : start + 2], use_local_synchronization=True) for start in (0, 2) + ) + + +def _norm_module(device): + module = nn.Module() + module.register_parameter( + "weight", nn.Parameter(torch.zeros(256, device=device, dtype=torch.bfloat16)) + ) + return module + + +@dataclass(frozen=True) +class KVState: + """Per-slot [1,1,length,256] KV tensors; length is the next absolute position.""" + + keys: tuple[torch.Tensor, ...] + values: tuple[torch.Tensor, ...] + + +def _packed_sequences(hidden, state, indices, cu_seqlens): + if ( + hidden.ndim != 2 + or hidden.shape[1] != 2048 + or hidden.dtype != torch.bfloat16 + or not hidden.is_cuda + ): + raise ValueError("Attention hidden must be CUDA BF16 [tokens, 2048]") + if hidden.shape[0] < 1: + raise ValueError("Attention requires at least one active token") + if ( + not isinstance(state, KVState) + or len(state.keys) < 2 + or len(state.keys) != len(state.values) + ): + raise ValueError("KVState requires a reserved slot and matching key/value slots") + if ( + indices.ndim != 1 + or indices.dtype not in (torch.int32, torch.int64) + or indices.device != hidden.device + ): + raise ValueError("State indices must be an integer vector on the input device") + if ( + cu_seqlens.device.type != "cpu" + or cu_seqlens.ndim != 1 + or cu_seqlens.dtype not in (torch.int32, torch.int64) + ): + raise ValueError("cu_seqlens must be a CPU integer vector") + slots, ends = indices.tolist(), cu_seqlens.tolist() + if len(ends) != len(slots) + 1 or ends[0] != 0 or ends[-1] != hidden.shape[0]: + raise ValueError("cu_seqlens must describe all packed tokens") + if any(left > right for left, right in zip(ends, ends[1:])): + raise ValueError("cu_seqlens must be nondecreasing") + if len(set(slots)) != len(slots) or any(slot <= 0 or slot >= len(state.keys) for slot in slots): + raise ValueError("Active state slots must be unique and exclude reserved slot zero") + for key, value in zip(state.keys, state.values): + if ( + key.ndim != 4 + or key.shape[:2] != (1, 1) + or key.shape[-1] != 256 + or key.shape != value.shape + ): + raise ValueError("KV slots must have matching [1,1,length,256] tensors") + if ( + key.dtype != hidden.dtype + or value.dtype != hidden.dtype + or key.device != hidden.device + or value.device != hidden.device + ): + raise ValueError("KV slots must have the input device and BF16 dtype") + return slots, ends + + +class TP4FullAttention(nn.Module): + """Four local query heads and one pair-replicated KV head, with explicit state.""" + + def __init__(self, *, group, device, eps=1e-6, rope_theta=10000000): + super().__init__() + self.group = _tp_group(group) + self.rank = dist.get_rank(self.group) + self.kv_group = _kv_replica_groups(self.group)[self.rank // 2] + if eps != 1e-6 or rope_theta != 10000000: + raise ValueError("Full attention requires official epsilon and RoPE theta") + self.eps = eps + factory = {"device": device, "dtype": torch.bfloat16} + self.q_proj = nn.Linear(2048, 2048, bias=False, **factory) + self.k_proj = nn.Linear(2048, 256, bias=False, **factory) + self.v_proj = nn.Linear(2048, 256, bias=False, **factory) + self.o_proj = nn.Linear(1024, 2048, bias=False, **factory) + self.q_norm, self.k_norm = _norm_module(device), _norm_module(device) + _parallel_parameter(self.q_proj.weight, 0) + _parallel_parameter(self.o_proj.weight, 1) + _parallel_parameter(self.k_proj.weight, 0, duplicate=self.rank % 2 == 1) + _parallel_parameter(self.v_proj.weight, 0, duplicate=self.rank % 2 == 1) + # Kept as a Python float, not a buffer: Megatron's Float16Module casts every + # floating-point buffer of the actor to BF16, and BF16-rounded inverse + # frequencies rotate keys differently from the rollout engine's FP32 ones. + self.rope_theta = float(rope_theta) + + def load_hf_weights(self, weights): + self.load_state_dict(shard_attention_weights(weights, self.rank), strict=True) + + def export_local_hf_weights(self): + return self.state_dict() + + def initial_state(self, blocks): + if isinstance(blocks, bool) or not isinstance(blocks, int) or blocks < 2: + raise ValueError("KV state requires a reserved slot and an active slot") + return KVState( + tuple(self.q_proj.weight.new_empty((1, 1, 0, 256)) for _ in range(blocks)), + tuple(self.q_proj.weight.new_empty((1, 1, 0, 256)) for _ in range(blocks)), + ) + + @staticmethod + def inverse_frequencies(rope_theta, device): + """FP32 inverse frequencies of the 64-wide rotary quarter, computed on demand.""" + exponents = torch.arange(0, 64, 2, dtype=torch.float32, device=device) / 64 + return 1.0 / (float(rope_theta) ** exponents) + + def _rotate(self, value, positions): + inv_freq = TP4FullAttention.inverse_frequencies(self.rope_theta, value.device) + angles = positions.float()[:, None] * inv_freq[None, :] + angles = torch.cat((angles, angles), dim=-1) + cos, sin = angles.cos().to(value.dtype)[:, None], angles.sin().to(value.dtype)[:, None] + rotary, tail = value[..., :64], value[..., 64:] + rotated = torch.cat((-rotary[..., 32:], rotary[..., :32]), dim=-1) + return torch.cat((rotary * cos + rotated * sin, tail), dim=-1) + + def forward(self, hidden, state, indices, cu_seqlens): + from rl_engine.kernels.ops.cuda.norm.rmsnorm import Qwen3NextRMSNormCudaOp + + slots, ends = _packed_sequences(hidden, state, indices, cu_seqlens) + copied = _CopyToTP.apply(hidden, self.group) + query, gate = shared_linear(copied, self.q_proj.weight).reshape(-1, 4, 512).chunk(2, dim=-1) + key = shared_linear(copied, _CopyToTP.apply(self.k_proj.weight, self.kv_group)).reshape( + -1, 1, 256 + ) + value = shared_linear(copied, _CopyToTP.apply(self.v_proj.weight, self.kv_group)).reshape( + -1, 1, 256 + ) + norm = Qwen3NextRMSNormCudaOp() + query = norm(query, _CopyToTP.apply(self.q_norm.weight, self.group), eps=self.eps) + key = norm(key, _CopyToTP.apply(self.k_norm.weight, self.group), eps=self.eps) + next_keys, next_values = list(state.keys), list(state.values) + outputs = [] + for slot, begin, end in zip(slots, ends[:-1], ends[1:]): + if begin == end: + continue + offset = state.keys[slot].shape[2] + if offset + end - begin > 262144: + raise ValueError("KV continuation exceeds the official maximum position") + positions = torch.arange(offset, offset + end - begin, device=hidden.device) + q = self._rotate(query[begin:end], positions).transpose(0, 1).unsqueeze(0) + k = self._rotate(key[begin:end], positions).transpose(0, 1).unsqueeze(0) + v = value[begin:end].transpose(0, 1).unsqueeze(0) + next_keys[slot] = torch.cat((state.keys[slot], k), dim=2) + next_values[slot] = torch.cat((state.values[slot], v), dim=2) + out = shared_attention(q, next_keys[slot], next_values[slot]) + outputs.append(out.squeeze(0).transpose(0, 1)) + attended = torch.cat(outputs, dim=0) * gate.sigmoid() + local = shared_linear(attended.reshape(-1, 1024), self.o_proj.weight) + return _ReduceFromTP.apply(local, self.group), KVState(tuple(next_keys), tuple(next_values)) + def moe_hf_shapes(): result = { diff --git a/rl_engine/integrations/qwen3_next_tp_gdn.py b/rl_engine/integrations/qwen3_next_tp_gdn.py new file mode 100644 index 000000000..a5b883d2b --- /dev/null +++ b/rl_engine/integrations/qwen3_next_tp_gdn.py @@ -0,0 +1,159 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""TP4 Qwen3-Next GDN projections, HF weight mapping and explicit recurrent state. + +The module is shared by engine adapters. It does not install a vLLM backend or +claim model-level acceptance. The process group must contain exactly four ranks. +""" + +from collections.abc import Mapping, Sequence + +import torch +import torch.distributed as dist +from torch import nn + +from rl_engine.integrations.qwen3_next_provider import GDNProviderConfig, GDNState, shared_gdn_core +from rl_engine.integrations.qwen3_next_tp import _CopyToTP, _rank, _ReduceFromTP +from rl_engine.testing.tensor_identity import tensor_bitwise_equal + +_GLOBAL_SHAPES = { + "in_proj_qkvz.weight": (12288, 2048), + "in_proj_ba.weight": (64, 2048), + "conv1d.weight": (8192, 1, 4), + "A_log": (32,), + "dt_bias": (32,), + "norm.weight": (128,), + "out_proj.weight": (2048, 4096), +} +_LOCAL_SHAPES = { + "in_proj_qkvz.weight": (3072, 2048), + "in_proj_ba.weight": (16, 2048), + "conv1d.weight": (2048, 1, 4), + "A_log": (8,), + "dt_bias": (8,), + "norm.weight": (128,), + "out_proj.weight": (2048, 1024), +} + + +def _validate_weights(weights, shapes): + if set(weights) != set(shapes): + raise ValueError("GDN weight names do not match the explicit HF contract") + device = weights["A_log"].device + for name, shape in shapes.items(): + value = weights[name] + if value.shape != shape or value.dtype != torch.bfloat16 or value.device != device: + raise ValueError(f"{name} must be BF16 {shape} on {device}") + + +def shard_gdn_weights(weights: Mapping[str, torch.Tensor], rank: int) -> dict[str, torch.Tensor]: + """Shard official HF GQA-interleaved projections and Q/K/V-segmented conv.""" + _rank(rank) + _validate_weights(weights, _GLOBAL_SHAPES) + result = {} + for name, value in weights.items(): + if name == "norm.weight": + shard = value + elif name == "out_proj.weight": + shard = value[:, rank * 1024 : (rank + 1) * 1024] + elif name == "conv1d.weight": + q, k, v = value.split((2048, 2048, 4096), dim=0) + shard = torch.cat([part.chunk(4, dim=0)[rank] for part in (q, k, v)]) + else: + shard = value.chunk(4, dim=0)[rank] + result[name] = shard.contiguous() + return result + + +def assemble_gdn_weights(shards: Sequence[Mapping[str, torch.Tensor]]) -> dict[str, torch.Tensor]: + """Inverse mapping; reject divergent replicated norm weights.""" + if len(shards) != 4: + raise ValueError("Exactly four ordered TP shards are required") + for shard in shards: + _validate_weights(shard, _LOCAL_SHAPES) + result = {} + for name in _GLOBAL_SHAPES: + values = [shard[name] for shard in shards] + if name == "norm.weight": + if any(not tensor_bitwise_equal(value, values[0]) for value in values[1:]): + raise ValueError("Replicated GDN norm weights differ across TP ranks") + result[name] = values[0] + elif name == "conv1d.weight": + segments = [value.split((512, 512, 1024), dim=0) for value in values] + result[name] = torch.cat( + [segments[rank][part] for part in range(3) for rank in range(4)] + ) + else: + result[name] = torch.cat(values, dim=1 if name == "out_proj.weight" else 0) + return result + + +class TP4GDN(nn.Module): + """Complete GDN projection/core/output block with differentiable TP semantics.""" + + def __init__(self, *, group, device, config: GDNProviderConfig, eps: float = 1e-6): + super().__init__() + if not dist.is_initialized() or dist.get_world_size(group) != 4: + raise ValueError("TP4GDN requires an initialized four-rank process group") + config.validate() + self.group = dist.group.WORLD if group is None else group + self.provider_config, self.eps = config, eps + self.rank = dist.get_rank(group) + factory = {"device": device, "dtype": torch.bfloat16} + self.in_proj_qkvz = nn.Linear(2048, 3072, bias=False, **factory) + self.in_proj_ba = nn.Linear(2048, 16, bias=False, **factory) + self.conv1d = nn.Conv1d(2048, 2048, 4, groups=2048, bias=False, **factory) + self.A_log = nn.Parameter(torch.zeros(8, **factory)) + self.dt_bias = nn.Parameter(torch.zeros(8, **factory)) + self.norm = nn.Module() + self.norm.register_parameter("weight", nn.Parameter(torch.ones(128, **factory))) + self.out_proj = nn.Linear(1024, 2048, bias=False, **factory) + + def load_hf_weights(self, weights: Mapping[str, torch.Tensor]): + self.load_state_dict(shard_gdn_weights(weights, self.rank), strict=True) + + def initial_state(self, blocks: int) -> GDNState: + if blocks < 2: + raise ValueError("State must include a reserved slot and at least one active slot") + return GDNState( + self.A_log.new_zeros((blocks, 2048, 3)), + torch.zeros(blocks, 8, 128, 128, dtype=torch.float32, device=self.A_log.device), + ) + + def forward(self, hidden, state, indices, cu_seqlens): + from rl_engine.integrations.qwen3_next_forward import shared_linear + + if hidden.ndim != 2 or hidden.shape[1] != 2048 or hidden.dtype != torch.bfloat16: + raise ValueError("GDN hidden states must be BF16 [tokens, 2048]") + copied = _CopyToTP.apply(hidden, self.group) + qkvz = shared_linear(copied, self.in_proj_qkvz.weight).reshape(-1, 4, 768) + ba = shared_linear(copied, self.in_proj_ba.weight).reshape(-1, 4, 4) + q, k, v, z = qkvz.split((128, 128, 256, 256), dim=-1) + b, a = ba.split(2, dim=-1) + tokens = hidden.shape[0] + packed = torch.cat( + [value.reshape(tokens, width) for value, width in ((q, 512), (k, 512), (v, 1024))], + dim=-1, + ) + # The norm weight is shared by heads on all four ranks; its gradient + # sums contributions from all local head partitions exactly once. + norm_weight = _CopyToTP.apply(self.norm.weight, self.group) + output, next_state = shared_gdn_core( + packed, + a.reshape(tokens, 8), + b.reshape(tokens, 8), + z.reshape(tokens, 8, 128), + self.A_log.float(), + self.dt_bias.float(), + self.conv1d.weight[:, 0], + norm_weight, + state, + indices, + cu_seqlens, + config=self.provider_config, + num_k_heads=4, + eps=self.eps, + ) + local = shared_linear(output.reshape(tokens, 1024), self.out_proj.weight) + return _ReduceFromTP.apply(local, self.group), next_state diff --git a/rl_engine/kernels/ops/cuda/attention/deterministic_attn.py b/rl_engine/kernels/ops/cuda/attention/deterministic_attn.py index 3a9084b18..5b5e636a4 100644 --- a/rl_engine/kernels/ops/cuda/attention/deterministic_attn.py +++ b/rl_engine/kernels/ops/cuda/attention/deterministic_attn.py @@ -27,8 +27,8 @@ from rl_engine.kernels.ops.base import _C, _EXT_AVAILABLE from rl_engine.utils.logger import logger -_HEAD_DIM = 128 _IS_ROCM = torch.version.hip is not None +_HEAD_DIMS = (128,) if _IS_ROCM else (128, 256) _GPU_PLATFORM = "ROCm" if _IS_ROCM else "CUDA" @@ -201,8 +201,10 @@ def _validate_inputs( f"k/v shape mismatch: k={tuple(k.shape)}, v={tuple(v.shape)}, " f"expected k/v [B={b}, Hkv, Skv, D={d}]" ) - if d != _HEAD_DIM: - raise ValueError(f"head dim D must be {_HEAD_DIM}, got {d}") + if d not in _HEAD_DIMS: + raise ValueError(f"head dim D must be one of {_HEAD_DIMS}, got {d}") + if b < 1 or hq < 1 or hkv < 1: + raise ValueError("batch size and query/key head counts must be positive") if hq % hkv != 0: raise ValueError(f"Hq={hq} not divisible by Hkv={hkv} (GQA group)") if q.dtype not in (torch.float16, torch.bfloat16): @@ -211,7 +213,11 @@ def _validate_inputs( raise ValueError("q, k, v must share the same dtype") if not (q.is_cuda and k.is_cuda and v.is_cuda): raise ValueError("q, k, v must be GPU tensors") + if not (q.device == k.device == v.device): + raise ValueError("q, k, v must be on the same device") if key_padding_mask is not None: + if key_padding_mask.device != q.device: + raise ValueError("key_padding_mask must be on the input device") if key_padding_mask.dtype != torch.bool: raise ValueError("key_padding_mask must be bool") if key_padding_mask.shape != (b, skv): diff --git a/scripts/qwen3_next_gate_common.py b/scripts/qwen3_next_gate_common.py new file mode 100644 index 000000000..00642c8de --- /dev/null +++ b/scripts/qwen3_next_gate_common.py @@ -0,0 +1,125 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Shared pieces of the Qwen3-Next TP4 real-weight gates (``scripts/qwen3_next_tp_*_check.py``). + +Each gate is launched as ``torchrun --nproc-per-node 4 scripts/.py +--checkpoint --output `` and writes one ``rank-.json``. +""" + +import argparse +import json +import os +import subprocess +from collections.abc import Mapping +from datetime import timedelta +from importlib.metadata import version +from pathlib import Path + +import torch +import torch.distributed as dist +from safetensors import safe_open + +ROOT = Path(__file__).resolve().parents[1] + + +class CheckpointWeights(Mapping): + """Keep shard headers open while reading only the requested layer's tensors.""" + + def __init__(self, checkpoint, prefix, names, stack): + self.root, self.prefix, self.names, self.stack = checkpoint, prefix, tuple(names), stack + self.index = json.loads((checkpoint / "model.safetensors.index.json").read_text())[ + "weight_map" + ] + self.readers = {} + + def __iter__(self): + return iter(self.names) + + def __len__(self): + return len(self.names) + + def __getitem__(self, name): + if name not in self.names: + raise KeyError(name) + key = self.prefix + name + shard = self.index[key] + if shard not in self.readers: + self.readers[shard] = self.stack.enter_context( + safe_open(self.root / shard, framework="pt", device="cpu") + ) + return self.readers[shard].get_tensor(key) + + +def gather(value): + peers = [torch.empty_like(value) for _ in range(4)] + dist.all_gather(peers, value.contiguous()) + return peers + + +def check_gradients(module): + result = {} + for name, parameter in module.named_parameters(): + grad = parameter.grad + if grad is None or not torch.isfinite(grad).all() or not grad.count_nonzero(): + raise AssertionError(f"Missing finite nonzero gradient: {name}") + result[name] = float(grad.float().abs().max()) + return result + + +def timed(fn): + start, end = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True) + start.record() + out = fn() + end.record() + torch.cuda.synchronize() + return out, start.elapsed_time(end) * 1000.0 + + +def _git(*args): + return subprocess.run( + ["git", *args], cwd=ROOT, check=True, capture_output=True, text=True + ).stdout.strip() + + +def provenance(): + from rl_engine.integrations.qwen3_next_forward import FORWARD_PROVIDER_ID + + return { + "rl_kernel_commit": _git("rev-parse", "HEAD"), + "tracked_tree_dirty": bool(_git("status", "--porcelain", "--untracked-files=no")), + "provider": FORWARD_PROVIDER_ID, + "environment": { + "gpu": torch.cuda.get_device_name(), + "torch": torch.__version__, + "vllm": version("vllm"), + "nccl": ".".join(map(str, torch.cuda.nccl.version())), + }, + } + + +def run_gate(description, scope, gate): + """Parse ``--checkpoint/--output``, run ``gate(checkpoint, generator)`` on four ranks.""" + parser = argparse.ArgumentParser(description=description) + parser.add_argument("--checkpoint", required=True, type=Path) + parser.add_argument("--output", required=True, type=Path) + args = parser.parse_args() + rank = int(os.environ["LOCAL_RANK"]) + if int(os.environ["WORLD_SIZE"]) != 4 or torch.cuda.device_count() < 4: + raise RuntimeError(f"The {scope} gate requires exactly four ranks on four GPUs") + torch.cuda.set_device(rank) + dist.init_process_group( + "nccl", timeout=timedelta(minutes=15), device_id=torch.device("cuda", rank) + ) + try: + generator = torch.Generator(device="cuda").manual_seed(1234) + result = gate(args.checkpoint, generator) + args.output.mkdir(parents=True, exist_ok=True) + path = args.output / f"rank-{rank}.json" + partial = path.with_suffix(".json.partial") + payload = {"status": "passed", "scope": scope, "rank": rank, **provenance(), **result} + partial.write_text(json.dumps(payload, indent=2) + "\n") + partial.replace(path) + dist.barrier() + finally: + dist.destroy_process_group() diff --git a/scripts/qwen3_next_tp_attention_check.py b/scripts/qwen3_next_tp_attention_check.py new file mode 100644 index 000000000..00ded482b --- /dev/null +++ b/scripts/qwen3_next_tp_attention_check.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""TP4 full-attention gate on the official checkpoint's layer-3 weights; four GPUs, torchrun. + + torchrun --nproc-per-node 4 scripts/qwen3_next_tp_attention_check.py \\ + --checkpoint --output + +Requires, bitwise: the HF load/export round trip with the KV head replicated on +each rank pair; full prefill == chunked prefill (output and KV continuation) for +8-1024 tokens; a variable-length batch of four == each sequence alone, in any +order; a sixteen-token decode == the prefill rows; the training recompute == the +no-grad forward; and identical KV-pair and QK-norm gradients across replicas. +""" + +from contextlib import ExitStack + +import torch +import torch.distributed as dist +from qwen3_next_gate_common import CheckpointWeights, check_gradients, gather, run_gate + +from rl_engine.integrations.qwen3_next_tp_blocks import ( + ATTENTION_HF_SHAPES, + TP4FullAttention, + assemble_attention_weights, +) +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +def attention_gate(checkpoint, generator): + module = TP4FullAttention(group=dist.group.WORLD, device="cuda") + with ExitStack() as stack: + weights = CheckpointWeights( + checkpoint, "model.layers.3.self_attn.", ATTENTION_HF_SHAPES, stack + ) + module.load_hf_weights(weights) + shards = [dict() for _ in range(4)] + for name, tensor in module.export_local_hf_weights().items(): + for rank, value in enumerate(gather(tensor)): + shards[rank][name] = value.cpu() + for name, tensor in assemble_attention_weights(shards).items(): + exact(tensor, weights[name], name=f"attention HF {name}") + del shards, weights + cases = ["hf-load-export"] + slots = torch.tensor([1], device="cuda", dtype=torch.int32) + with torch.no_grad(): + for length in (8, 64, 256, 1024): + hidden = ( + torch.randn(length, 2048, device="cuda", dtype=torch.bfloat16, generator=generator) + * 0.1 + ) + state = module.initial_state(2) + whole, expected = module(hidden, state, slots, torch.tensor([0, length])) + cut = min(65, length - 1) + first, state = module(hidden[:cut], state, slots, torch.tensor([0, cut])) + last, state = module(hidden[cut:], state, slots, torch.tensor([0, length - cut])) + exact(torch.cat((first, last)), whole, name=f"attention full/chunk {length}") + exact(state.keys[1], expected.keys[1], name="attention key continuation") + exact(state.values[1], expected.values[1], name="attention value continuation") + cases.append(f"full-chunk-{length}") + hidden = ( + torch.randn(88, 2048, device="cuda", dtype=torch.bfloat16, generator=generator) * 0.1 + ) + packed, state = module( + hidden, + module.initial_state(5), + torch.tensor([1, 2, 3, 4], device="cuda"), + torch.tensor([0, 8, 24, 48, 88]), + ) + order = [3, 1, 0, 2] + segments = [hidden[:8], hidden[8:24], hidden[24:48], hidden[48:]] + ends = [0] + for index in order: + ends.append(ends[-1] + segments[index].shape[0]) + reordered, _ = module( + torch.cat([segments[i] for i in order]), + module.initial_state(5), + torch.tensor([i + 1 for i in order], device="cuda"), + torch.tensor(ends), + ) + expected = packed.split((8, 16, 24, 40)) + exact( + reordered, + torch.cat([expected[i] for i in order]), + name="attention variable-length batch4 reorder", + ) + for index, segment in enumerate(segments): + single, _ = module( + segment, module.initial_state(2), slots, torch.tensor([0, segment.shape[0]]) + ) + exact(single, expected[index], name="attention batch4/single") + cases.append("batch4-varlen-reorder") + hidden = ( + torch.randn(80, 2048, device="cuda", dtype=torch.bfloat16, generator=generator) * 0.1 + ).requires_grad_() + with torch.no_grad(): + expected, _ = module(hidden, module.initial_state(2), slots, torch.tensor([0, 80])) + _, decode_state = module(hidden[:64], module.initial_state(2), slots, torch.tensor([0, 64])) + decoded = [] + for token in hidden[64:].split(1): + value, decode_state = module(token, decode_state, slots, torch.tensor([0, 1])) + decoded.append(value) + exact(torch.cat(decoded), expected[64:], name="attention sixteen-token decode") + _, state = module(hidden[:64], module.initial_state(2), slots, torch.tensor([0, 64])) + output, _ = module(hidden[64:], state, slots, torch.tensor([0, 16])) + exact(output, expected[64:], name="attention independent response recompute") + output.float().square().mean().backward() + if ( + hidden.grad is None + or not torch.isfinite(hidden.grad).all() + or not hidden.grad[:64].count_nonzero() + ): + raise AssertionError("Attention response gradient did not reach the prompt") + gradients = check_gradients(module) + for projection in (module.k_proj, module.v_proj): + peers = gather(projection.weight.grad) + exact(peers[0], peers[1], name="KV replica gradient pair0") + exact(peers[2], peers[3], name="KV replica gradient pair1") + for norm in (module.q_norm, module.k_norm): + for peer in gather(norm.weight.grad): + exact(peer, norm.weight.grad, name="QK norm replicated gradient") + cases.extend( + ("sixteen-token-decode", "prompt-gradient", "kv-pair-gradient", "qk-norm-gradient") + ) + return {"cases": cases, "gradient_max_abs": gradients} + + +if __name__ == "__main__": + run_gate( + __doc__, + "tp4_full_attention_layer3", + lambda checkpoint, generator: {"attention": attention_gate(checkpoint, generator)}, + ) diff --git a/scripts/qwen3_next_tp_gdn_check.py b/scripts/qwen3_next_tp_gdn_check.py new file mode 100644 index 000000000..e3aae3692 --- /dev/null +++ b/scripts/qwen3_next_tp_gdn_check.py @@ -0,0 +1,96 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""TP4 GDN gate on the official checkpoint's layer-0 weights; four GPUs, torchrun. + + torchrun --nproc-per-node 4 scripts/qwen3_next_tp_gdn_check.py \\ + --checkpoint --output + +A one-layer integration gate, not full-model evidence. Requires, bitwise: the +HF load/export round trip of the actual parameter storage; full prefill == +chunked prefill, output and convolution/recurrent state handoff, for 8-1024 +tokens; the training response recompute == the no-grad forward; a gradient that +reaches the prompt through the state; identical replicated norm gradients. +""" + +import json + +import torch +import torch.distributed as dist +from qwen3_next_gate_common import check_gradients, run_gate +from safetensors import safe_open + +from rl_engine.integrations.qwen3_next_provider import GDNProviderConfig +from rl_engine.integrations.qwen3_next_tp_gdn import _GLOBAL_SHAPES, TP4GDN, assemble_gdn_weights +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +def load_first_gdn(checkpoint): + index = json.loads((checkpoint / "model.safetensors.index.json").read_text())["weight_map"] + result = {} + for name in _GLOBAL_SHAPES: + key = f"model.layers.0.linear_attn.{name}" + with safe_open(checkpoint / index[key], framework="pt", device="cpu") as reader: + result[name] = reader.get_tensor(key) + return result + + +def gdn_gate(checkpoint, generator): + weights = load_first_gdn(checkpoint) + module = TP4GDN(group=dist.group.WORLD, device="cuda", config=GDNProviderConfig()) + module.load_hf_weights(weights) + # Validate actual parameter storage, not just the pure conversion helper. + shards = [dict() for _ in range(4)] + for name, parameter in module.named_parameters(): + gathered = [torch.empty_like(parameter) for _ in range(4)] + dist.all_gather(gathered, parameter.detach()) + for peer, value in enumerate(gathered): + shards[peer][name] = value.cpu() + for name, restored in assemble_gdn_weights(shards).items(): + exact(restored, weights[name], name=f"loaded weight {name}") + del shards, weights + cases = [] + indices = torch.tensor([1], device="cuda", dtype=torch.int32) + with torch.no_grad(): + for length in (8, 64, 256, 1024): + hidden = ( + torch.randn(length, 2048, device="cuda", dtype=torch.bfloat16, generator=generator) + * 0.1 + ) + initial = module.initial_state(2) + whole, expected_state = module(hidden, initial, indices, torch.tensor([0, length])) + cut = min(65, length - 1) + first, state = module(hidden[:cut], initial, indices, torch.tensor([0, cut])) + last, state = module(hidden[cut:], state, indices, torch.tensor([0, length - cut])) + exact(torch.cat((first, last)), whole, name=f"TP4 layer length={length}") + exact(state.convolution, expected_state.convolution, name="convolution handoff") + exact(state.recurrent, expected_state.recurrent, name="recurrent handoff") + cases.append(f"full-chunk-{length}") + hidden = ( + torch.randn(80, 2048, device="cuda", dtype=torch.bfloat16, generator=generator) * 0.1 + ).requires_grad_() + initial = module.initial_state(2) + with torch.no_grad(): + expected, _ = module(hidden, initial, indices, torch.tensor([0, 80])) + _, prompt_state = module(hidden[:64], initial, indices, torch.tensor([0, 64])) + actual, _ = module(hidden[64:], prompt_state, indices, torch.tensor([0, 16])) + exact(actual, expected[64:], name="independent training response") + actual.float().square().mean().backward() + if ( + hidden.grad is None + or not torch.isfinite(hidden.grad).all() + or not hidden.grad[:64].count_nonzero() + ): + raise AssertionError("Response gradient did not reach prompt input") + grad_stats = check_gradients(module) + norm_grads = [torch.empty_like(module.norm.weight.grad) for _ in range(4)] + dist.all_gather(norm_grads, module.norm.weight.grad) + for other in norm_grads: + exact(other, module.norm.weight.grad, name="replicated norm gradient") + cases.extend(("prompt-gradient", "replicated-norm-gradient", "hf-load-export")) + return {"cases": cases, "gradient_max_abs": grad_stats} + + +if __name__ == "__main__": + run_gate(__doc__, "tp4_gdn_layer0", gdn_gate) diff --git a/scripts/qwen3_next_tp_moe_check.py b/scripts/qwen3_next_tp_moe_check.py index 817c07bcc..450f7fd51 100644 --- a/scripts/qwen3_next_tp_moe_check.py +++ b/scripts/qwen3_next_tp_moe_check.py @@ -18,91 +18,19 @@ and a finite, nonzero gradient for every parameter. """ -import argparse -import json -import os -import subprocess -from collections.abc import Mapping from contextlib import ExitStack -from datetime import timedelta -from importlib.metadata import version -from pathlib import Path import torch import torch.distributed as dist -from safetensors import safe_open +from qwen3_next_gate_common import CheckpointWeights, check_gradients, gather, run_gate, timed -from rl_engine.integrations.qwen3_next_forward import ( - FORWARD_PROVIDER_ID, - shared_router, - stable_top10_routes, -) +from rl_engine.integrations.qwen3_next_forward import shared_router, stable_top10_routes from rl_engine.integrations.qwen3_next_tp_blocks import TP4MoE, assemble_moe_weight, moe_hf_shapes from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact -ROOT = Path(__file__).resolve().parents[1] TOKEN_COUNTS = (8, 64, 256, 1024) -class CheckpointWeights(Mapping): - """Keep shard headers open while reading only the requested layer's tensors.""" - - def __init__(self, checkpoint, prefix, names, stack): - self.root, self.prefix, self.names, self.stack = checkpoint, prefix, tuple(names), stack - self.index = json.loads((checkpoint / "model.safetensors.index.json").read_text())[ - "weight_map" - ] - self.readers = {} - - def __iter__(self): - return iter(self.names) - - def __len__(self): - return len(self.names) - - def __getitem__(self, name): - if name not in self.names: - raise KeyError(name) - key = self.prefix + name - shard = self.index[key] - if shard not in self.readers: - self.readers[shard] = self.stack.enter_context( - safe_open(self.root / shard, framework="pt", device="cpu") - ) - return self.readers[shard].get_tensor(key) - - -def atomic_json(path, payload): - temporary = path.with_suffix(path.suffix + ".partial") - temporary.write_text(json.dumps(payload, indent=2) + "\n") - temporary.replace(path) - - -def gather(value): - peers = [torch.empty_like(value) for _ in range(4)] - dist.all_gather(peers, value.contiguous()) - return peers - - -def check_gradients(module): - result = {} - for name, parameter in module.named_parameters(): - grad = parameter.grad - if grad is None or not torch.isfinite(grad).all() or not grad.count_nonzero(): - raise AssertionError(f"Missing finite nonzero gradient: {name}") - result[name] = float(grad.float().abs().max()) - return result - - -def timed(fn): - start, end = torch.cuda.Event(enable_timing=True), torch.cuda.Event(enable_timing=True) - start.record() - out = fn() - end.record() - torch.cuda.synchronize() - return out, start.elapsed_time(end) * 1000.0 - - def moe_gate(checkpoint, generator): module = TP4MoE(group=dist.group.WORLD, device="cuda") with ExitStack() as stack: @@ -158,50 +86,9 @@ def moe_gate(checkpoint, generator): return {"cases": cases, "gradient_max_abs": gradients, "forward_us": forward_us} -def _git(*args): - return subprocess.run( - ["git", *args], cwd=ROOT, check=True, capture_output=True, text=True - ).stdout.strip() - - -def main(): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--checkpoint", required=True, type=Path) - parser.add_argument("--output", required=True, type=Path) - args = parser.parse_args() - rank = int(os.environ["LOCAL_RANK"]) - if int(os.environ["WORLD_SIZE"]) != 4 or torch.cuda.device_count() < 4: - raise RuntimeError("The TP4 MoE gate requires exactly four ranks on four GPUs") - torch.cuda.set_device(rank) - dist.init_process_group( - "nccl", timeout=timedelta(minutes=15), device_id=torch.device("cuda", rank) - ) - try: - generator = torch.Generator(device="cuda").manual_seed(1234) - moe = moe_gate(args.checkpoint, generator) - args.output.mkdir(parents=True, exist_ok=True) - atomic_json( - args.output / f"rank-{rank}.json", - { - "status": "passed", - "scope": "tp4_moe_block_layer0", - "rank": rank, - "rl_kernel_commit": _git("rev-parse", "HEAD"), - "tracked_tree_dirty": bool(_git("status", "--porcelain", "--untracked-files=no")), - "provider": FORWARD_PROVIDER_ID, - "environment": { - "gpu": torch.cuda.get_device_name(), - "torch": torch.__version__, - "vllm": version("vllm"), - "nccl": ".".join(map(str, torch.cuda.nccl.version())), - }, - "moe": moe, - }, - ) - dist.barrier() - finally: - dist.destroy_process_group() - - if __name__ == "__main__": - main() + run_gate( + __doc__, + "tp4_moe_block_layer0", + lambda checkpoint, generator: {"moe": moe_gate(checkpoint, generator)}, + ) diff --git a/tests/check_qwen3_next_attention.py b/tests/check_qwen3_next_attention.py new file mode 100644 index 000000000..d184d5735 --- /dev/null +++ b/tests/check_qwen3_next_attention.py @@ -0,0 +1,110 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Required Qwen3-Next CUDA attention gates; unavailable CUDA is a failure.""" + +import pytest +import torch + +from rl_engine.kernels.ops.base import _C +from rl_engine.kernels.ops.cuda.attention.deterministic_attn import DeterministicAttentionOp +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +def inputs(tokens, *, batch=1, query_tokens=None, device="cuda", grad=False): + assert torch.cuda.is_available(), "Mandatory attention CUDA gate cannot skip" + gen = torch.Generator(device=device).manual_seed(920) + query_tokens = tokens if query_tokens is None else query_tokens + return tuple( + torch.randn(batch, heads, length, 256, device=device, dtype=torch.bfloat16, generator=gen) + .mul_(0.25) + .requires_grad_(grad) + for heads, length in ((4, query_tokens), (1, tokens), (1, tokens)) + ) + + +@pytest.mark.parametrize("tokens", [8, 64, 256, 1024]) +@torch.no_grad() +def test_d256_full_chunk_decode_and_batch_are_raw_bit_equal(tokens): + q, k, v = inputs(tokens, batch=4) + op = DeterministicAttentionOp() + whole, lse = op.forward_with_lse(q, k, v) + order = torch.tensor([3, 1, 0, 2], device=q.device) + exact(op(q[order], k[order], v[order]), whole[order]) + for row in range(4): + exact(op(q[row : row + 1], k[row : row + 1], v[row : row + 1]), whole[row : row + 1]) + starts = [0, 1, 7, tokens - 1, tokens] + starts = sorted(set(starts)) + for start, end in zip(starts[:-1], starts[1:]): + out, part_lse = op.forward_with_lse(q[:, :, start:end], k[:, :, :end], v[:, :, :end]) + exact(out, whole[:, :, start:end]) + exact(part_lse, lse[:, :, start:end]) + + +def test_d256_backward_with_cached_prefix_matches_float64_formula(): + q, k, v = inputs(19, query_tokens=3, grad=True) + op = DeterministicAttentionOp() + out = op(q, k, v) + grad = torch.linspace(-1, 1, out.numel(), device=q.device).reshape_as(out).to(out.dtype) + out.backward(grad) + qr, kr, vr = (x.detach().double().requires_grad_() for x in (q, k, v)) + scores = qr @ kr.repeat_interleave(4, dim=1).transpose(-1, -2) / 16 + causal = ( + torch.arange(19, device=q.device)[None, :] <= torch.arange(16, 19, device=q.device)[:, None] + ) + expected = scores.masked_fill(~causal, -torch.inf).softmax(-1) @ vr.repeat_interleave(4, dim=1) + expected.backward(grad.double()) + # Formula accuracy is separate from the raw-bit provider comparisons above. + for actual, reference in zip((q, k, v), (qr, kr, vr)): + assert torch.isfinite(actual.grad).all() and actual.grad.abs().max() > 0 + torch.testing.assert_close( + actual.grad.float(), reference.grad.float(), atol=0.002, rtol=0.016 + ) + assert k.grad[:, :, :16].abs().max() > 0 + assert v.grad[:, :, :16].abs().max() > 0 + + +def test_native_backward_rejects_malformed_probability_and_gradient_buffers(): + q, k, v = inputs(8) + out, _, probabilities = _C.deterministic_attention_forward(q, k, v, True, 1 / 16, None) + with pytest.raises(RuntimeError, match="P must be FP32"): + _C.deterministic_attention_backward( + out, q, k, v, probabilities.to(q.dtype), True, 1 / 16, None + ) + with pytest.raises(RuntimeError, match="grad_output must have q shape"): + _C.deterministic_attention_backward( + out[:, :, :1], q, k, v, probabilities, True, 1 / 16, None + ) + with pytest.raises(RuntimeError, match="P must have shape"): + _C.deterministic_attention_backward( + out, q, k, v, probabilities[:, :, :1], True, 1 / 16, None + ) + + +def test_d256_input_device_guard_and_nondefault_stream(): + assert torch.cuda.device_count() >= 2, "Mandatory cross-device gate requires allocated GPUs" + previous = torch.cuda.current_device() + try: + q, k, v = inputs(8, device="cuda:1", grad=True) + op = DeterministicAttentionOp() + torch.cuda.set_device(1) + expected = op(q, k, v) + expected.sum().backward() + gradients = [x.grad.clone() for x in (q, k, v)] + for x in (q, k, v): + x.grad = None + torch.cuda.synchronize(1) + stream = torch.cuda.Stream(device=1) + with torch.cuda.stream(stream): + torch.cuda.set_device(0) + actual = op(q, k, v) + actual.sum().backward() + assert torch.cuda.current_device() == 0 + stream.synchronize() + exact(actual, expected) + for x, expected_grad in zip((q, k, v), gradients): + exact(x.grad, expected_grad) + with pytest.raises(ValueError, match="same device"): + op(q, k.to("cuda:0"), v) + finally: + torch.cuda.set_device(previous) diff --git a/tests/check_qwen3_next_conv_bridge.py b/tests/check_qwen3_next_conv_bridge.py new file mode 100644 index 000000000..fb6778a71 --- /dev/null +++ b/tests/check_qwen3_next_conv_bridge.py @@ -0,0 +1,96 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""The convolution provider must agree across batch, chunk and cache layouts.""" + +import pytest +import torch + +from rl_engine.integrations.qwen3_next_conv import _provider, causal_conv_sequence +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +def inputs(lengths): + assert torch.cuda.is_available(), "Required provider tests cannot skip CUDA" + gen = torch.Generator(device="cuda").manual_seed(17) + + def rand(*shape): + return torch.randn(*shape, device="cuda", dtype=torch.bfloat16, generator=gen) + + return ( + rand(sum(lengths), 2048), + rand(len(lengths) + 1, 2048, 3), + rand(2048, 4), + torch.arange(1, len(lengths) + 1, device="cuda", dtype=torch.int32), + torch.tensor([0, *torch.tensor(lengths).cumsum(0).tolist()]), + ) + + +@torch.no_grad() +@pytest.mark.parametrize("lengths", [(1,), (64, 8, 0, 16), (1024, 256, 64, 8)]) +def test_varlen_matches_repeated_decode_and_preserves_inputs(lengths): + x, state, weight, indices, cu = inputs(lengths) + saved = [tensor.clone() for tensor in (x, state, weight)] + output, final_state = causal_conv_sequence(x, state, weight, indices, cu) + reference_state = state.clone() + reference_rows = [] + for seq in range(len(lengths)): + for token in range(cu[seq], cu[seq + 1]): + value = x[token : token + 1].clone() + reference_rows.append( + _provider()( + value, + reference_state, + weight, + activation="silu", + conv_state_indices=indices[seq : seq + 1], + ) + ) + exact(output, torch.cat(reference_rows)) + exact(final_state, reference_state) + for original, before in zip((x, state, weight), saved): + exact(original, before) + + +@torch.no_grad() +def test_chunked_and_reordered_sequences_match(): + x, state, weight, indices, cu = inputs((1024, 64)) + whole, whole_state = causal_conv_sequence(x, state, weight, indices, cu) + # Continue both sequences after 32 tokens, in the opposite batch order. + prompt = torch.cat((x[:32], x[1024:1056])) + first, next_state = causal_conv_sequence( + prompt, state, weight, indices, torch.tensor([0, 32, 64]) + ) + response = torch.cat((x[1056:], x[32:1024])) + second, next_state = causal_conv_sequence( + response, next_state, weight, indices.flip(0), torch.tensor([0, 32, 1024]) + ) + restored = torch.cat((first[:32], second[32:], first[32:], second[:32])) + exact(restored, whole) + exact(next_state, whole_state) + + +def test_response_gradient_reaches_prompt_and_convolution_weight(): + x, state, weight, indices, _ = inputs((8,)) + x.requires_grad_() + state.requires_grad_() + weight.requires_grad_() + _, prompt_state = causal_conv_sequence(x[:4], state, weight, indices, torch.tensor([0, 4])) + out, final_state = causal_conv_sequence( + x[4:], prompt_state, weight, indices, torch.tensor([0, 4]) + ) + (out.float().square().sum() + final_state.float().square().sum() * 0.01).backward() + for tensor in (x, weight): + assert tensor.grad is not None and torch.isfinite(tensor.grad).all() + assert tensor.grad.abs().max() > 0 + assert x.grad[:4].abs().max() > 0 + + +def test_empty_sequence_preserves_cache_and_gradients(): + x, state, weight, indices, cu = inputs((0,)) + state.requires_grad_() + out, next_state = causal_conv_sequence(x, state, weight, indices, cu) + assert out.shape == x.shape + exact(next_state, state) + next_state.float().sum().backward() + exact(state.grad, torch.ones_like(state)) diff --git a/tests/check_qwen3_next_core_matrix.py b/tests/check_qwen3_next_core_matrix.py new file mode 100644 index 000000000..8c299a87f --- /dev/null +++ b/tests/check_qwen3_next_core_matrix.py @@ -0,0 +1,97 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Mandatory TP4-shape GDN state tests; no full-model claim or optional skips.""" + +import pytest +import torch + +from rl_engine.integrations.qwen3_next_provider import GDNProviderConfig, GDNState, shared_gdn_core +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +def _inputs(lengths): + assert torch.cuda.is_available(), "Required shared-provider CUDA tests cannot skip" + generator = torch.Generator(device="cuda").manual_seed(20261002) + + def rand(*shape, dtype=torch.bfloat16): + return torch.randn(*shape, device="cuda", dtype=dtype, generator=generator) * 0.1 + + tokens = sum(lengths) + values = [rand(tokens, 2048), rand(tokens, 8), rand(tokens, 8), rand(tokens, 8, 128)] + weights = [rand(8, dtype=torch.float32), rand(8, dtype=torch.float32), rand(2048, 4), rand(128)] + state = GDNState( + rand(len(lengths) + 2, 2048, 3), + rand(len(lengths) + 2, 8, 128, 128, dtype=torch.float32), + ) + indices = torch.arange(1, len(lengths) + 1, device="cuda", dtype=torch.int32) + return values, weights, state, indices + + +def _run(values, weights, state, indices, lengths): + boundaries = torch.tensor([0, *torch.tensor(lengths).cumsum(0).tolist()], dtype=torch.int64) + return shared_gdn_core( + *values, *weights, state, indices, boundaries, config=GDNProviderConfig(), num_k_heads=4 + ) + + +@pytest.mark.parametrize("batch", [1, 4]) +@pytest.mark.parametrize("length", [8, 64, 256, 1024]) +@torch.no_grad() +def test_full_chunked_reordered_shared_core(batch, length): + lengths = [length] * batch + values, weights, initial, indices = _inputs(lengths) + whole, final = _run(values, weights, initial, indices, lengths) + for cut in (1, 63, 64, 65, 100, 528, 576): + cut = min(cut, length) + state = initial + pieces = [[] for _ in lengths] + for start, end in ((0, cut), (cut, length)): + # Reorder the response-bearing step, preserving sequence/cache identity. + order = list(range(batch)) if start == 0 else list(reversed(range(batch))) + rows = [row for seq in order for row in range(seq * length + start, seq * length + end)] + row_ids = torch.tensor(rows, device="cuda", dtype=torch.int64) + chunk = [value.index_select(0, row_ids) for value in values] + part, state = _run(chunk, weights, state, indices[order], [end - start] * batch) + for position, seq in enumerate(order): + pieces[seq].append(part[position * (end - start) : (position + 1) * (end - start)]) + exact(torch.cat([torch.cat(seq) for seq in pieces]), whole, name=f"output cut={cut}") + exact(state.convolution, final.convolution, name=f"conv state cut={cut}") + exact(state.recurrent, final.recurrent, name=f"recurrent state cut={cut}") + exact(final.convolution[0], initial.convolution[0], name="reserved conv slot") + exact(final.recurrent[0], initial.recurrent[0], name="reserved recurrent slot") + + +@torch.no_grad() +def test_variable_lengths_empty_and_mixed_step_match_independent_sequences(): + lengths = [65, 1, 0, 17] + values, weights, initial, indices = _inputs(lengths) + mixed, mixed_state = _run(values, weights, initial, indices, lengths) + start = 0 + for seq, length in enumerate(lengths): + end = start + length + output, state = _run( + [value[start:end] for value in values], + weights, + initial, + indices[seq : seq + 1], + [length], + ) + exact(output, mixed[start:end], name=f"sequence {seq}") + exact(state.convolution[seq + 1], mixed_state.convolution[seq + 1], name="conv state") + exact(state.recurrent[seq + 1], mixed_state.recurrent[seq + 1], name="recurrent state") + start = end + exact(mixed_state.recurrent[3], initial.recurrent[3], name="empty sequence state") + + +@pytest.mark.parametrize("batch", [1, 4]) +@torch.no_grad() +def test_shared_core_one_hundred_repetitions(batch): + lengths = [8] * batch + values, weights, initial, indices = _inputs(lengths) + expected, expected_state = _run(values, weights, initial, indices, lengths) + for repeat in range(100): + output, state = _run(values, weights, initial, indices, lengths) + exact(output, expected, name=f"output repeat={repeat}") + exact(state.convolution, expected_state.convolution, name="conv state") + exact(state.recurrent, expected_state.recurrent, name="recurrent state") diff --git a/tests/check_qwen3_next_gdn_bridge.py b/tests/check_qwen3_next_gdn_bridge.py new file mode 100644 index 000000000..9e44cc9e0 --- /dev/null +++ b/tests/check_qwen3_next_gdn_bridge.py @@ -0,0 +1,112 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Explicit subprocess provider tests; never import vLLM into ordinary pytest.""" + +import pytest +import torch + +from rl_engine.integrations.qwen3_next_gdn import _provider, packed_decode_training_step +from rl_engine.kernels.ops.pytorch.linear_attn import GatedDeltaRuleRecurrentStepOp +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal, tensor_bitwise_equal + +pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required") + + +def _inputs(seed=1): + gen = torch.Generator(device="cuda").manual_seed(seed) + + def rand(*shape, dtype=torch.bfloat16): + return torch.randn(*shape, device="cuda", dtype=dtype, generator=gen) + + return [ + rand(2, 512), + rand(2, 2), + rand(2, 2), + rand(2, dtype=torch.float32), + rand(2, dtype=torch.float32), + rand(3, 2, 128, 128, dtype=torch.float32) * 0.01, + ] + + +def test_bridge_forward_is_provider_exact_and_does_not_mutate_inputs(): + inputs = _inputs() + state_before = inputs[-1].clone() + indices = torch.tensor([1, 2], device="cuda", dtype=torch.int32) + out, state = packed_decode_training_step(*inputs, indices, num_k_heads=1) + expected = torch.empty_like(out) + expected_state = state_before.clone() + _provider()( + *inputs[:-1], 128**-0.5, expected_state, expected, indices, use_qk_l2norm_in_kernel=True + ) + assert_tensor_bitwise_equal(out, expected) + assert_tensor_bitwise_equal(state, expected_state) + assert tensor_bitwise_equal(inputs[-1], state_before) + + +def test_bridge_keeps_prompt_state_gradient_through_response(): + inputs = [x.requires_grad_() for x in _inputs()] + indices = torch.tensor([1, 2], device="cuda", dtype=torch.int32) + _, prompt_state = packed_decode_training_step(*inputs, indices, num_k_heads=1) + prompt_state.retain_grad() + response = _inputs(seed=2)[0].requires_grad_() + out, final_state = packed_decode_training_step( + response, *inputs[1:5], prompt_state, indices, num_k_heads=1 + ) + (out.float().square().sum() + final_state.square().sum() * 0.01).backward() + assert prompt_state.grad is not None and prompt_state.grad.abs().max() > 0 + for tensor in [*inputs, response]: + assert tensor.grad is not None + assert torch.isfinite(tensor.grad).all() + assert tensor.grad.abs().max() > 0 + + +def test_bridge_backward_matches_recomputed_recurrence_vjp(): + inputs = [x.requires_grad_() for x in _inputs()] + indices = torch.tensor([1, 2], device="cuda", dtype=torch.int32) + out, state = packed_decode_training_step(*inputs, indices, num_k_heads=1) + grad_out = torch.ones_like(out) * 0.125 + grad_state = torch.ones_like(state) * 0.001 + torch.autograd.backward((out, state), (grad_out, grad_state)) + refs = [x.detach().clone().requires_grad_() for x in inputs] + ref_out, ref_state = GatedDeltaRuleRecurrentStepOp()( + *refs, indices, scale=128**-0.5, num_k_heads=1 + ) + torch.autograd.backward((ref_out, ref_state), (grad_out, grad_state)) + for actual, ref in zip(inputs, refs): + assert tensor_bitwise_equal(actual.grad, ref.grad) + + +def test_initial_state_vjp_matches_provider_directional_difference(): + inputs = _inputs() + inputs[-1].requires_grad_() + indices = torch.tensor([1, 2], device="cuda", dtype=torch.int32) + _, output = packed_decode_training_step(*inputs, indices, num_k_heads=1) + direction = torch.ones_like(inputs[-1]) * 0.03125 + probe = torch.linspace(-0.01, 0.01, output.numel(), device="cuda").reshape_as(output) + (output.double() * probe.double()).sum().backward() + analytic = (inputs[-1].grad.double() * direction.double()).sum() + eps = 0.01 + with torch.no_grad(): + _, plus = packed_decode_training_step( + *inputs[:-1], inputs[-1] + eps * direction, indices, num_k_heads=1 + ) + _, minus = packed_decode_training_step( + *inputs[:-1], inputs[-1] - eps * direction, indices, num_k_heads=1 + ) + numerical = ((plus.double() - minus.double()) * probe.double()).sum() / (2 * eps) + torch.testing.assert_close(analytic, numerical, rtol=1e-3, atol=1e-4) + + +@pytest.mark.parametrize("empty", [False, True]) +def test_inactive_state_is_identity_in_backward(empty): + inputs = _inputs() + if empty: + inputs[:3] = [x[:0] for x in inputs[:3]] + inputs = [x.requires_grad_() for x in inputs] + indices = torch.zeros(inputs[0].shape[0], device="cuda", dtype=torch.int32) + out, state = packed_decode_training_step(*inputs, indices, num_k_heads=1) + (out.float().sum() + state.sum()).backward() + assert tensor_bitwise_equal(inputs[-1].grad, torch.ones_like(state)) + for tensor in inputs[:-1]: + assert torch.count_nonzero(tensor.grad) == 0 diff --git a/tests/check_qwen3_next_gdn_sequence.py b/tests/check_qwen3_next_gdn_sequence.py new file mode 100644 index 000000000..fef317872 --- /dev/null +++ b/tests/check_qwen3_next_gdn_sequence.py @@ -0,0 +1,102 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Packed decode recurrence boundary checks; no complete-model L2 claim.""" + +import pytest +import torch + +from rl_engine.integrations.qwen3_next_gdn import packed_recurrent_sequence +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +def inputs(lengths, seed=7): + assert torch.cuda.is_available(), "CUDA is required; this acceptance suite cannot skip" + generator = torch.Generator(device="cuda").manual_seed(seed) + + def rand(*shape, dtype=torch.bfloat16): + return torch.randn(*shape, device="cuda", dtype=dtype, generator=generator) + + tokens = sum(lengths) + # Actual TP4 shard: 16 / 4 key heads, 32 / 4 value heads, K=V=128. + tensors = [ + rand(tokens, 2048), + rand(tokens, 8), + rand(tokens, 8), + rand(8, dtype=torch.float32), + rand(8, dtype=torch.float32), + rand(len(lengths) + 1, 8, 128, 128, dtype=torch.float32) * 0.01, + ] + indices = torch.arange(1, len(lengths) + 1, device="cuda", dtype=torch.int32) + cu = torch.tensor([0, *torch.tensor(lengths).cumsum(0).tolist()], dtype=torch.int64) + return tensors, indices, cu + + +def run(tensors, indices, cu): + return packed_recurrent_sequence(*tensors, indices, cu, num_k_heads=4) + + +@torch.no_grad() +@pytest.mark.parametrize("lengths", [(8,), (64, 8, 0, 16), (1024, 256, 64, 8)]) +def test_packed_matches_independent_sequences_and_reordering(lengths): + tensors, indices, cu = inputs(lengths) + original_state = tensors[-1].clone() + output, state = run(tensors, indices, cu) + for seq, length in enumerate(lengths): + start, end = cu[seq : seq + 2].tolist() + one = [*(value[start:end] for value in tensors[:3]), *tensors[3:]] + expected, expected_state = run(one, indices[seq : seq + 1], torch.tensor([0, length])) + exact(output[start:end], expected) + exact(state[seq + 1], expected_state[seq + 1]) + permutation = list(reversed(range(len(lengths)))) + rows = torch.cat([torch.arange(cu[seq], cu[seq + 1], device="cuda") for seq in permutation]) + changed = [*(value.index_select(0, rows) for value in tensors[:3]), *tensors[3:]] + reordered_cu = torch.tensor([0, *torch.tensor([lengths[i] for i in permutation]).cumsum(0)]) + reordered, reordered_state = run(changed, indices[permutation], reordered_cu) + exact(reordered, output.index_select(0, rows)) + exact(reordered_state, state) + exact(tensors[-1], original_state) + + +@torch.no_grad() +def test_1024_token_chunk_continuation_is_bitwise_exact(): + tensors, indices, cu = inputs((1024,)) + whole, whole_state = run(tensors, indices, cu) + state, pieces = tensors[-1], [] + cuts = (0, 1, 8, 64, 256, 513, 1024) + for start, end in zip(cuts, cuts[1:]): + chunk = [*(value[start:end] for value in tensors[:3]), *tensors[3:5], state] + output, state = run(chunk, indices, torch.tensor([0, end - start])) + pieces.append(output) + exact(torch.cat(pieces), whole) + exact(state, whole_state) + + +def test_chunk_boundary_retains_prompt_and_initial_state_gradients(): + tensors, indices, cu = inputs((8,)) + tensors = [tensor.requires_grad_() for tensor in tensors] + prompt = [*(value[:4] for value in tensors[:3]), *tensors[3:]] + _, prompt_state = run(prompt, indices, torch.tensor([0, 4])) + prompt_state.retain_grad() + response = [*(value[4:] for value in tensors[:3]), *tensors[3:5], prompt_state] + out, state = run(response, indices, torch.tensor([0, 4])) + (out.float().square().sum() + state.square().sum() * 0.01).backward() + assert prompt_state.grad is not None and prompt_state.grad.abs().max() > 0 + for value in tensors: + assert value.grad is not None and torch.isfinite(value.grad).all() + assert value.grad.abs().max() > 0 + assert tensors[0].grad[:4].abs().max() > 0 + + +@pytest.mark.parametrize("bad", [[1, 8], [0, 9], [0, 6, 5, 8]]) +def test_bad_packing_is_rejected(bad): + tensors, indices, _ = inputs((8,)) + with pytest.raises(ValueError, match="cu_seqlens"): + run(tensors, indices, torch.tensor(bad)) + + +def test_reserved_and_duplicate_slots_are_rejected(): + tensors, indices, cu = inputs((4, 4)) + for bad in ([0, 1], [1, 1]): + with pytest.raises(ValueError): + run(tensors, torch.tensor(bad, device="cuda", dtype=torch.int32), cu) diff --git a/tests/check_qwen3_next_shared_core.py b/tests/check_qwen3_next_shared_core.py new file mode 100644 index 000000000..e65158d07 --- /dev/null +++ b/tests/check_qwen3_next_shared_core.py @@ -0,0 +1,83 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Combined provider contract; does not instantiate a complete Qwen model.""" + +from dataclasses import replace + +import pytest +import torch + +from rl_engine.integrations.qwen3_next_provider import GDNProviderConfig, GDNState, shared_gdn_core +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +def inputs(tokens, grad=False): + assert torch.cuda.is_available(), "Required CUDA provider suite cannot skip" + gen = torch.Generator(device="cuda").manual_seed(73) + + def rand(*shape, dtype=torch.bfloat16): + return torch.randn(*shape, device="cuda", dtype=dtype, generator=gen).requires_grad_(grad) + + args = [ + rand(tokens, 2048), + rand(tokens, 8), + rand(tokens, 8), + rand(tokens, 8, 128), + rand(8, dtype=torch.float32), + rand(8, dtype=torch.float32), + rand(2048, 4), + rand(128), + GDNState(rand(2, 2048, 3), rand(2, 8, 128, 128, dtype=torch.float32)), + ] + return args, torch.tensor([1], device="cuda", dtype=torch.int32) + + +@pytest.mark.parametrize("tokens", [8, 1024]) +@torch.no_grad() +def test_shared_core_chunk_and_decode_state_match(tokens): + args, indices = inputs(tokens) + config = GDNProviderConfig() + output, state = shared_gdn_core( + *args, indices, torch.tensor([0, tokens]), config=config, num_k_heads=4 + ) + pieces, next_state = [], args[-1] + for start, end in ((0, 3), (3, tokens - 1), (tokens - 1, tokens)): + chunk = [*(value[start:end] for value in args[:4]), *args[4:8], next_state] + part, next_state = shared_gdn_core( + *chunk, indices, torch.tensor([0, end - start]), config=config, num_k_heads=4 + ) + pieces.append(part) + exact(torch.cat(pieces), output) + exact(next_state.convolution, state.convolution) + exact(next_state.recurrent, state.recurrent) + + +def test_independent_training_forward_matches_inference_and_retains_gradients(): + args, indices = inputs(8, grad=True) + config = GDNProviderConfig() + with torch.no_grad(): + reference, _ = shared_gdn_core( + *args, indices, torch.tensor([0, 8]), config=config, num_k_heads=4 + ) + prompt = [*(value[:4] for value in args[:4]), *args[4:]] + _, prompt_state = shared_gdn_core( + *prompt, indices, torch.tensor([0, 4]), config=config, num_k_heads=4 + ) + prompt_state.recurrent.retain_grad() + response = [*(value[4:] for value in args[:4]), *args[4:8], prompt_state] + actual, _ = shared_gdn_core( + *response, indices, torch.tensor([0, 4]), config=config, num_k_heads=4 + ) + exact(actual, reference[4:]) + actual.float().square().sum().backward() + for value in args[:8]: + assert value.grad is not None and torch.isfinite(value.grad).all() + assert value.grad.abs().max() > 0 + assert args[0].grad[:4].abs().max() > 0 + assert prompt_state.recurrent.grad.abs().max() > 0 + + +def test_unimplemented_provider_profile_is_rejected(): + with pytest.raises(ValueError, match="Unsupported"): + replace(GDNProviderConfig(), recurrence="unverified-prefill").validate() diff --git a/tests/test_qwen3_next_forward_contract.py b/tests/test_qwen3_next_forward_contract.py index 76d6df183..56d681b21 100644 --- a/tests/test_qwen3_next_forward_contract.py +++ b/tests/test_qwen3_next_forward_contract.py @@ -15,6 +15,7 @@ lambda: provider.shared_linear(torch.zeros(1, 8), torch.zeros(8, 8)), lambda: provider.stable_top10_routes(torch.zeros(1, 512)), lambda: provider.combine_routes(torch.zeros(1, 10, 8), torch.zeros(1, 10)), + lambda: provider.shared_attention(*(torch.zeros(1, 4, 8, 256) for _ in range(3))), ], ) def test_required_cuda_primitives_reject_cpu(call): diff --git a/tests/test_qwen3_next_tp_blocks.py b/tests/test_qwen3_next_tp_blocks.py index f2ac9c822..f97ad07a2 100644 --- a/tests/test_qwen3_next_tp_blocks.py +++ b/tests/test_qwen3_next_tp_blocks.py @@ -1,7 +1,7 @@ # SPDX-License-Identifier: Apache-2.0 # Copyright (c) 2026 RL-Kernel Contributors -"""HF mappings, replica checks and official Qwen3-Next MoE dimensions.""" +"""HF mappings, KV replica ownership and official Qwen3-Next attention/MoE dimensions.""" import pytest import torch @@ -20,6 +20,32 @@ def pattern(shape): ) +@pytest.fixture(scope="module") +def attention_weights(): + return {name: pattern(shape) for name, shape in blocks.ATTENTION_HF_SHAPES.items()} + + +def test_attention_hf_roundtrip_and_kv_pair_layout(attention_weights): + shards = [blocks.shard_attention_weights(attention_weights, rank) for rank in range(4)] + for name, value in blocks.assemble_attention_weights(shards).items(): + exact(value, attention_weights[name], name=name) + for rank, shard in enumerate(shards): + exact( + shard["q_proj.weight"], + attention_weights["q_proj.weight"][rank * 2048 : (rank + 1) * 2048], + ) + begin = rank // 2 * 256 + exact(shard["k_proj.weight"], attention_weights["k_proj.weight"][begin : begin + 256]) + + +def test_attention_export_rejects_disagreeing_kv_replica(attention_weights): + shards = [blocks.shard_attention_weights(attention_weights, rank) for rank in range(4)] + shards[1]["k_proj.weight"] = shards[1]["k_proj.weight"].clone() + shards[1]["k_proj.weight"][0, 0] = -1 + with pytest.raises(ValueError, match="Replicated KV"): + blocks.assemble_attention_weights(shards) + + @pytest.mark.parametrize( "name", [ @@ -91,3 +117,46 @@ def test_tp4_moe_parameter_ownership(monkeypatch): assert not getattr(module.gate.weight, "tensor_model_parallel", False) assert not getattr(module.shared_expert_gate.weight, "tensor_model_parallel", False) assert module.gate.weight.shape == (512, 2048) + + +@pytest.fixture +def fake_tp(monkeypatch): + group = object() + monkeypatch.setattr(blocks.dist, "is_initialized", lambda: True) + monkeypatch.setattr(blocks.dist, "get_world_size", lambda group: 4) + monkeypatch.setattr(blocks, "_kv_replica_groups", lambda group: (object(), object())) + return group + + +@pytest.mark.parametrize("rank", [0, 1, 2, 3]) +def test_attention_parameter_ownership_and_actual_hf_storage( + fake_tp, monkeypatch, rank, attention_weights +): + monkeypatch.setattr(blocks.dist, "get_rank", lambda group: rank) + module = blocks.TP4FullAttention(group=fake_tp, device="cpu") + module.load_hf_weights(attention_weights) + expected = blocks.shard_attention_weights(attention_weights, rank) + for name, value in module.export_local_hf_weights().items(): + exact(value, expected[name], name=name) + assert module.q_proj.weight.tensor_model_parallel + assert module.o_proj.weight.partition_dim == 1 + assert module.k_proj.weight.shared == (rank % 2 == 1) + assert module.v_proj.weight.shared == (rank % 2 == 1) + assert not getattr(module.q_norm.weight, "tensor_model_parallel", False) + state = module.initial_state(3) + assert len(state.keys) == len(state.values) == 3 + assert all(value.shape == (1, 1, 0, 256) for value in state.keys) + + +def test_rope_uses_absolute_positions_and_preserves_unrotated_tail(fake_tp, monkeypatch): + monkeypatch.setattr(blocks.dist, "get_rank", lambda group: 0) + module = blocks.TP4FullAttention(group=fake_tp, device="cpu") + x = torch.randn(8, 4, 256, dtype=torch.bfloat16) + positions = torch.arange(61, 69) + whole = module._rotate(x, positions) + exact(whole[..., 64:], x[..., 64:]) + exact( + torch.cat([module._rotate(x[:3], positions[:3]), module._rotate(x[3:], positions[3:])]), + whole, + ) + assert not torch.equal(module._rotate(x, torch.arange(8)), whole) diff --git a/tests/test_qwen3_next_tp_gdn.py b/tests/test_qwen3_next_tp_gdn.py new file mode 100644 index 000000000..69d047983 --- /dev/null +++ b/tests/test_qwen3_next_tp_gdn.py @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Official HF interleaving and TP shard round trips, without a GPU dependency.""" + +import pytest +import torch + +from rl_engine.integrations.qwen3_next_tp_gdn import ( + _GLOBAL_SHAPES, + assemble_gdn_weights, + shard_gdn_weights, +) +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal + + +@pytest.fixture(scope="module") +def weights(): + return { + name: torch.arange(torch.Size(shape).numel(), dtype=torch.int32) + .remainder(251) + .to(torch.bfloat16) + .reshape(shape) + for name, shape in _GLOBAL_SHAPES.items() + } + + +def test_official_interleaving_survives_tp4_round_trip(weights): + shards = [shard_gdn_weights(weights, rank) for rank in range(4)] + restored = assemble_gdn_weights(shards) + for name, tensor in weights.items(): + assert_tensor_bitwise_equal(restored[name], tensor, name=name) + for rank, shard in enumerate(shards): + # Conv is Q/K/V segmented, unlike the GQA-interleaved qkvz projection. + assert_tensor_bitwise_equal( + shard["conv1d.weight"][:512], weights["conv1d.weight"][rank * 512 : (rank + 1) * 512] + ) + assert_tensor_bitwise_equal( + shard["conv1d.weight"][512:1024], + weights["conv1d.weight"][2048 + rank * 512 : 2048 + (rank + 1) * 512], + ) + assert_tensor_bitwise_equal( + shard["in_proj_qkvz.weight"], + weights["in_proj_qkvz.weight"][rank * 3072 : (rank + 1) * 3072], + ) + + +@pytest.mark.parametrize("rank", [-1, 4, True, 1.0]) +def test_invalid_tp_rank_is_rejected(weights, rank): + with pytest.raises(ValueError, match="TP4 rank"): + shard_gdn_weights(weights, rank) + + +def test_divergent_replicated_norm_is_not_silently_exported(weights): + shards = [shard_gdn_weights(weights, rank) for rank in range(4)] + shards[1]["norm.weight"] = shards[1]["norm.weight"].clone() + shards[1]["norm.weight"][0] = -1 + with pytest.raises(ValueError, match="Replicated"): + assemble_gdn_weights(shards) + + +def test_missing_hf_weight_is_rejected(weights): + incomplete = {name: value for name, value in weights.items() if name != "dt_bias"} + with pytest.raises(ValueError, match="weight names"): + shard_gdn_weights(incomplete, 0) From 362b80b5ae0feefcaafc7698b9894b671b126c24 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 9 Oct 2026 02:12:10 +0000 Subject: [PATCH 03/68] docs(qwen3-next): MoE route/combine contract, prior-art and TP4 evidence (B200, 2e950f3) Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/README.md | 1 + .../operators/qwen3-next-moe-route-combine.md | 137 ++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 docs/operators/qwen3-next-moe-route-combine.md diff --git a/docs/operators/README.md b/docs/operators/README.md index 0b93b3d90..be74cb6d0 100644 --- a/docs/operators/README.md +++ b/docs/operators/README.md @@ -34,4 +34,5 @@ Every operator page should include: - [Token Embedding](embedding.md) - [Qwen3-Next RMSNorm (zero-centred)](qwen3-next-rms-norm.md) - [Qwen3-Next Gated RMSNorm](qwen3-next-rms-norm-gated.md) +- [Qwen3-Next MoE route / combine](qwen3-next-moe-route-combine.md) - [Operator Doc Template](../contributing/operator-doc-template.md) diff --git a/docs/operators/qwen3-next-moe-route-combine.md b/docs/operators/qwen3-next-moe-route-combine.md new file mode 100644 index 000000000..ed11301a0 --- /dev/null +++ b/docs/operators/qwen3-next-moe-route-combine.md @@ -0,0 +1,137 @@ +# Qwen3-Next MoE route / combine contract + +## Summary + +Qwen3-Next's sparse MoE block routes every token to 10 of 512 experts (expert +width 512, TP4-local width 128) and adds a gated shared expert. RFC #428 row +`moe_route_combine_contract` asks for a routed MoE whose output for a token is a +pure function of that token: the same bits whatever else is in the batch, in +training replay and in rollout. This page states the contract and how +`rl_engine.integrations.qwen3_next_forward.shared_moe` meets it. + +Upstream references: `transformers` `Qwen3NextSparseMoeBlock` (5.17.0) and vLLM +0.30.0 `fused_topk` + `fused_experts`. + +## Entry Point + +```python +from rl_engine.integrations.qwen3_next_forward import shared_moe, stable_top10_routes +from rl_engine.integrations.qwen3_next_tp_blocks import TP4MoE + +routed, routes = shared_moe(x, router_weight, gate_up, down) # TP-local, not reduced +block = TP4MoE(group=tp_group, device="cuda") # routed + shared, reduced +block.load_hf_weights(hf_layer_mlp_weights) +y = block(hidden) +``` + +`TRITON_F32_DEFAULT=ieee` must be set before the process starts: the FP32 router +GEMM refuses to run on TF32 Triton dots. + +## Contract + +| Stage | Order and dtype | Atomics | +| --- | --- | --- | +| Router | BF16 `x`, `W` upcast to FP32; one pinned vLLM batch-invariant GEMM (IEEE FP32) | none | +| Softmax | max subtraction, `exp`, row sum by a fixed pairwise tree (`fixed_order_row_sum`) | none | +| Selection | `argsort(logits, descending, stable)[:, :10]`: score descending, equal scores to the lower expert id | none | +| Weights | the ten selected FP32 probabilities divided by their fixed-tree sum | none | +| Experts | per (token, route) row: `gate_up` GEMM (BF16 in, FP32 accumulate, BF16 out), SwiGLU in FP32, BF16 cast, `down` GEMM | none: one writer per row | +| Combine | `out = e0*w0`, then `out += e_k*w_k` for k = 1..9 in route order, FP32; one BF16 cast | none | +| Shared expert (TP4 block) | `routed + shared * sigmoid(gate)` in BF16, then one TP all-reduce | collective as configured | + +Payload dtype between stages: BF16 expert outputs, FP32 route weights, BF16 +block output. The expert GEMMs are vLLM 0.30.0's `fused_moe` Triton kernel with +the tile vLLM itself uses under `VLLM_BATCH_INVARIANT=1` +(`BLOCK_SIZE_M/N/K = 64/64/32`, `GROUP_SIZE_M = 8`, `SPLIT_K = 1`), passed +explicitly so no environment variable, tuned-config file or token count can +change it. The routed weight is *not* multiplied inside the kernel and the +kernel does not sum over routes; that stays in the combine above. + +Fail-closed checks: CUDA tensors only (no CPU fallback, no ROCm), BF16 weights of +the exact `[512, 2*I, H]` / `[512, H, I]` shapes, finite FP32 router logits, +vLLM exactly 0.30.0, `VLLM_TRITON_USE_TD` off (the tensor-descriptor path is a +different instruction stream), and IEEE Triton FP32. + +### Backward + +`_RoutedExperts.backward` recomputes the `[gate, up]` projection with the same +grouped kernel, then visits experts in ascending id. Each expert's `dgate_up` +and `ddown` slice is one pinned GEMM written once into a dense buffer; `dx` is +accumulated per token in ascending expert order (a token's ten experts are +distinct, so each launch has unique rows). Router-weight gradients flow through +the selected probabilities; the indices are discrete. + +## Reuse decision + +| Implementation | Routes BI | Output BI | Backward | Why not reused as is | +| --- | --- | --- | --- | --- | +| vLLM `fused_topk` + `fused_experts`, `VLLM_BATCH_INVARIANT=0` | no | no | none | tuned configs and the BF16 router GEMM depend on the token count | +| vLLM, `VLLM_BATCH_INVARIANT=1` | yes | yes | none | no VJP for training; BF16 router logits (Qwen3-Next and VIME route in FP32); routed weight applied in the expert GEMM epilogue on the BF16 output, then routes summed by `moe_sum` | +| HF `Qwen3NextExperts` (eager) | yes | yes | autograd | `dx`/`dW` change with batch size (cuBLAS shape heuristics); BF16 router; `index_add_` combine in BF16 | +| FlashInfer `cutlass_fused_moe` | (torch routing) | no | none | output changes with batch size; no VJP | +| SGLang, Megatron-core, Transformer Engine | not measured | not measured | - | not installable next to the pinned vLLM 0.30.0 / torch 2.13 runtime | + +What RL-Kernel reuses: vLLM's batch-invariant GEMM for the router and the +backward, and vLLM's `fused_moe` Triton kernel for the forward expert GEMMs, +which is batch-invariant per route at the fixed tile. What it implements: the +FP32 fixed-order routing, the stable tie rule, the atomic-free FP32 combine, the +deterministic backward and the TP4 boundaries. The grouped kernel's per-route +output is bitwise equal to the per-expert pinned GEMM loop it replaced +(`tests/check_qwen3_next_forward.py::test_grouped_*`), so this is a speed change +with no change in bits. + +## Results + +Measured on B200 with `scripts/qwen3_next_moe_prior_art.py` and the TP4 gate +`scripts/qwen3_next_tp_moe_check.py`; the reports, the figure and the exact +commands are in +[`docs/usage/evidence/qwen3-next-moe-route-b200/`](../usage/evidence/qwen3-next-moe-route-b200/README.md). + +![Qwen3-Next routed MoE prior art](../usage/evidence/qwen3-next-moe-route-b200/figure.png) + +TP1 shape (H=2048, 512 experts, top-10, width 512), random weights, BF16: + +| | RL-Kernel `shared_moe` | vLLM BI=1 | vLLM BI=0 | FlashInfer CUTLASS | HF eager | +| --- | --- | --- | --- | --- | --- | +| routes / output BI (16-1024 tokens) | **yes / yes** | yes / yes | no / no (256+) | no / no (256+) | yes / yes | +| dx / dW BI | **yes / yes** | no backward | no backward | no backward | no / no (1024) | +| rel. L2 error vs FP64 | **3.9e-3** | 7.0e-2 | 7.0e-2 | 7.0e-2 | 7.0e-2 | +| tokens routed unlike FP64 (of 256) | **0** | 11 | 11 | 11 | 11 | +| forward, 1 / 64 / 1024 / 4096 tokens (ms) | 1.38 / 1.71 / 2.05 / 3.05 | 0.37 / 0.66 / 0.88 / 1.06 | 0.36 / 0.65 / 0.87 / 1.06 | 0.34 / 0.67 / 0.85 / 1.08 | 2.0 / 61 / 89 / 89 | +| forward + backward, 64 / 1024 tokens (ms) | 112 / 166 | - | - | - | 878 / 1283 | + +* The accuracy gap is the router: every other candidate computes BF16 router + logits, and 11 of 256 tokens then select a different expert set than FP64 + routing does. RL-Kernel routes in FP32 and selects the same experts. +* RL-Kernel's forward is 2-4x slower than vLLM's. About 0.5 ms of it, at any + token count, is the FP32 IEEE router GEMM, which is part of the contract. + The expert GEMMs themselves run in vLLM's kernel. The per-expert GEMM loop + they replace (same bits) took 47 / 66 / 71 ms at 64 / 1024 / 4096 tokens in + a local B200 run of the same runner; that run is not part of this evidence. +* The backward is still a per-expert loop of pinned GEMMs (512 experts x 4 + GEMMs) and dominates a training step's MoE time. + +TP4 gate on the real layer-0 weights (`tp4-moe/rank-*.json`): all twelve cases +pass on all four ranks, including the round trip of all 512 experts, identical +routes on every rank, full == chunked and reordered batches at 8-1024 tokens, +and identical replicated router gradients. The TP4 block forward (routed + +shared expert + all-reduce) takes 1.9-2.4 ms at 8-1024 tokens. + +## Tests + +| File | Device | Covers | +| --- | --- | --- | +| `tests/test_qwen3_next_forward_contract.py` | CPU | CPU rejection, vLLM pin, fixed-order sum/softmax row independence | +| `tests/test_qwen3_next_tp_blocks.py` | CPU | HF shard/assemble round trips, replica drift rejection, TP4 parameter ownership | +| `tests/test_tensor_identity.py` | CPU | raw-bit identity (signed zero, NaN/Inf, dtype) | +| `tests/check_qwen3_next_forward.py` | CUDA + vLLM | GEMM batch/chunk/reorder, route ties, combine order, MoE VJP vs FP64, route and output batch invariance, grouped == per-expert bitwise, fail-closed TD path | +| `scripts/qwen3_next_tp_moe_check.py` | 4 x CUDA | real layer-0 weights: HF round trip, replicated routes, chunk/reorder, training forward, gradients | + +The `check_` file imports vLLM, so it runs in the `Qwen3-Next-provider-GPU` +workflow rather than the default collection. + +## Scope + +TP4 with EP1 (all 512 experts on every rank) on CUDA B200. ROCm, EP > 1 and +other TP sizes are separate claims. Model-level parity uses this block but is +the `full_model_chain` row. From bd13adc3a8267cbc0ed0011aeff1867e739f2867 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 9 Oct 2026 02:19:35 +0000 Subject: [PATCH 04/68] test(qwen3-next): prior-art runner for the TP4 D=256 attention core Compares RL-Kernel's deterministic attention with torch SDPA, vLLM FlashAttention-2 (num_splits auto and 1), vLLM's Triton unified attention and FlashInfer's ragged prefill: batch, chunked-prefill and decode invariance, backward repeatability, accuracy against FP64 and latency. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../testing/qwen3_next_attention_prior_art.py | 404 ++++++++++++++++++ .../plot_qwen3_next_attention_prior_art.py | 138 ++++++ scripts/qwen3_next_attention_prior_art.py | 87 ++++ 3 files changed, 629 insertions(+) create mode 100644 rl_engine/testing/qwen3_next_attention_prior_art.py create mode 100644 scripts/plot_qwen3_next_attention_prior_art.py create mode 100644 scripts/qwen3_next_attention_prior_art.py diff --git a/rl_engine/testing/qwen3_next_attention_prior_art.py b/rl_engine/testing/qwen3_next_attention_prior_art.py new file mode 100644 index 000000000..8a089b84a --- /dev/null +++ b/rl_engine/testing/qwen3_next_attention_prior_art.py @@ -0,0 +1,404 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Prior-art comparison for the Qwen3-Next TP4 full-attention core (D=256, GQA 4:1). + +Every candidate computes causal attention for one rank of Qwen3-Next's TP4 +layout: 4 query heads over 1 KV head, head dim 256, scale 1/16, queries aligned +to the end of the KV sequence. For each candidate the report records + +* **batch invariance** - a target sequence's output computed alone, and inside + a batch with three other sequences (placed first and last), must be bitwise + equal; for candidates with a backward, the same for its dq/dk/dv; +* **prefill/decode invariance** - the last 64 query rows, and the last single + row, computed against the full KV must equal the same rows of the full + prefill. This is the replay-vs-rollout boundary of RFC #428; +* **repeatability** - two identical calls are bitwise equal; +* **accuracy** - error against an FP64 evaluation; +* **performance** - median CUDA-event latency of prefill, decode and backward. + +A candidate that cannot be imported or launched in the pinned environment is +recorded as ``unavailable`` with the reason instead of failing the run. +""" + +from __future__ import annotations + +import statistics +from typing import Any, Callable + +import torch +import torch.nn.functional as F + +HQ, HKV, D = 4, 1, 256 +SCALE = 1.0 / 16 +TARGET = 1000 +COMPANIONS = (777, 1500, 64) +CHUNK = 64 +PREFILL_TOKENS = (512, 2048, 8192) +DECODE_KV = 8192 +BACKWARD_TOKENS = 2048 + + +# --------------------------------------------------------------------------- # +# Measurement helpers +# --------------------------------------------------------------------------- # + + +def _sample_us(fn: Callable[[], Any]) -> float: + torch.cuda.synchronize() + start = torch.cuda.Event(enable_timing=True) + end = torch.cuda.Event(enable_timing=True) + start.record() + fn() + end.record() + torch.cuda.synchronize() + return start.elapsed_time(end) * 1000.0 + + +def time_interleaved(calls: dict[str, Callable[[], Any]], warmup: int = 5, iters: int = 30): + keys = list(calls) + orders = (keys, list(reversed(keys))) + for i in range(warmup): + for key in orders[i % 2]: + calls[key]() + samples = {key: [] for key in keys} + for i in range(iters): + for key in orders[i % 2]: + samples[key].append(_sample_us(calls[key])) + return {key: statistics.median(values) for key, values in samples.items()} + + +def _bitwise(a: torch.Tensor, b: torch.Tensor) -> bool: + return a.shape == b.shape and a.dtype == b.dtype and torch.equal(a, b) + + +def _guard(key: str, factory: Callable[[], dict[str, Any]]) -> dict[str, Any]: + try: + return {"key": key, **factory()} + except Exception as exc: # noqa: BLE001 - record and continue + return {"key": key, "unavailable": f"{type(exc).__name__}: {str(exc)[:300]}"} + + +def make_sequence(length: int, seed: int, *, kv_length: int | None = None): + """q [Lq, HQ, D], k/v [Lk, HKV, D] in BF16 (token-major, as engines store them).""" + kv_length = length if kv_length is None else kv_length + g = torch.Generator(device="cuda").manual_seed(seed) + + def randn(*shape): + return torch.randn(shape, device="cuda", generator=g).to(torch.bfloat16) + + return randn(length, HQ, D), randn(kv_length, HKV, D), randn(kv_length, HKV, D) + + +def fp64_reference(q, k, v): + lq, lk = q.shape[0], k.shape[0] + qd, kd, vd = (t.double().transpose(0, 1) for t in (q, k, v)) + kd, vd = kd.expand(HQ, -1, -1), vd.expand(HQ, -1, -1) + scores = qd @ kd.transpose(1, 2) * SCALE + rows = torch.arange(lq, device=q.device)[:, None] + (lk - lq) + scores = scores.masked_fill(torch.arange(lk, device=q.device)[None, :] > rows, float("-inf")) + return (scores.softmax(-1) @ vd).transpose(0, 1) + + +# --------------------------------------------------------------------------- # +# Candidates +# +# ``run(seqs) -> outputs``: ``seqs`` is a list of (q, k, v) with queries aligned +# to the end of each KV sequence; every candidate computes the whole list in the +# way its engine batches it (one varlen launch, or one launch per sequence). +# ``grad(q, k, v, dy) -> (dq, dk, dv)`` when the candidate has a backward. +# --------------------------------------------------------------------------- # + + +def _rl_kernel(): + from rl_engine.integrations.qwen3_next_forward import shared_attention + + def one(q, k, v): + out = shared_attention(*(t.transpose(0, 1).unsqueeze(0) for t in (q, k, v))) + return out.squeeze(0).transpose(0, 1) + + def run(seqs): + with torch.no_grad(): + return [one(*s) for s in seqs] + + def grad(q, k, v, dy): + leaves = [t.detach().clone().requires_grad_(True) for t in (q, k, v)] + return torch.autograd.grad(one(*leaves), leaves, dy) + + return { + "name": "RL-Kernel deterministic attention (one launch per sequence)", + "source": "rl_engine.integrations.qwen3_next_forward.shared_attention", + "run": run, + "grad": grad, + } + + +def _sdpa(): + def one(q, k, v): + lq, lk = q.shape[0], k.shape[0] + qh, kh, vh = (t.transpose(0, 1).unsqueeze(0) for t in (q, k, v)) + if lq == lk: + out = F.scaled_dot_product_attention( + qh, kh, vh, is_causal=True, scale=SCALE, enable_gqa=True + ) + else: + rows = torch.arange(lq, device=q.device)[:, None] + (lk - lq) + mask = torch.arange(lk, device=q.device)[None, :] <= rows + out = F.scaled_dot_product_attention( + qh, kh, vh, attn_mask=mask, scale=SCALE, enable_gqa=True + ) + return out.squeeze(0).transpose(0, 1) + + def run(seqs): + with torch.no_grad(): + return [one(*s) for s in seqs] + + def grad(q, k, v, dy): + leaves = [t.detach().clone().requires_grad_(True) for t in (q, k, v)] + return torch.autograd.grad(one(*leaves), leaves, dy) + + return { + "name": "torch SDPA (default backend, one launch per sequence)", + "source": f"torch {torch.__version__} scaled_dot_product_attention", + "run": run, + "grad": grad, + } + + +def _cu(lengths): + out = [0] + for n in lengths: + out.append(out[-1] + n) + return torch.tensor(out, device="cuda", dtype=torch.int32) + + +def _split(out, lengths): + return list(out.split(list(lengths))) + + +def _vllm_fa(num_splits: int): + import vllm + from vllm.vllm_flash_attn import flash_attn_varlen_func + + def run(seqs): + lq = [s[0].shape[0] for s in seqs] + lk = [s[1].shape[0] for s in seqs] + q, k, v = (torch.cat([s[i] for s in seqs]) for i in range(3)) + out = flash_attn_varlen_func( + q, + k, + v, + max_seqlen_q=max(lq), + cu_seqlens_q=_cu(lq), + max_seqlen_k=max(lk), + cu_seqlens_k=_cu(lk), + softmax_scale=SCALE, + causal=True, + num_splits=num_splits, + fa_version=2, + ) + return _split(out, lq) + + return { + "name": f"vLLM FlashAttention-2 varlen, num_splits={num_splits}", + "source": f"vllm {vllm.__version__} vllm_flash_attn.flash_attn_varlen_func", + "run": run, + } + + +def _vllm_triton(): + import vllm + from vllm.v1.attention.ops.triton_unified_attention import unified_attention + + block = 16 + + def run(seqs): + lq = [s[0].shape[0] for s in seqs] + lk = [s[1].shape[0] for s in seqs] + blocks = [(n + block - 1) // block for n in lk] + k_cache = torch.zeros(sum(blocks), block, HKV, D, device="cuda", dtype=torch.bfloat16) + v_cache = torch.zeros_like(k_cache) + table = torch.zeros(len(seqs), max(blocks), device="cuda", dtype=torch.int32) + first = 0 + for i, (s, n) in enumerate(zip(seqs, blocks)): + pad = n * block - s[1].shape[0] + k_cache[first : first + n] = F.pad(s[1], (0, 0, 0, 0, 0, pad)).view(n, block, HKV, D) + v_cache[first : first + n] = F.pad(s[2], (0, 0, 0, 0, 0, pad)).view(n, block, HKV, D) + table[i, :n] = torch.arange(first, first + n, device="cuda") + first += n + q = torch.cat([s[0] for s in seqs]) + out = torch.empty_like(q) + unified_attention( + q, + k_cache, + v_cache, + out, + cu_seqlens_q=_cu(lq), + max_seqlen_q=max(lq), + seqused_k=torch.tensor(lk, device="cuda", dtype=torch.int32), + max_seqlen_k=max(lk), + softmax_scale=SCALE, + causal=True, + window_size=(-1, -1), + block_table=table, + softcap=0, + q_descale=None, + k_descale=None, + v_descale=None, + ) + return _split(out, lq) + + return { + # Without split-softmax buffers the 2D kernel runs for every batch, which + # is also the only kernel vLLM's batch-invariant mode allows. + "name": "vLLM Triton unified attention (2D kernel)", + "source": f"vllm {vllm.__version__} v1/attention/ops/triton_unified_attention", + "run": run, + } + + +def _flashinfer(): + import flashinfer + + workspace = torch.empty(256 << 20, device="cuda", dtype=torch.uint8) + wrapper = flashinfer.BatchPrefillWithRaggedKVCacheWrapper(workspace, "NHD") + + def run(seqs): + lq = [s[0].shape[0] for s in seqs] + lk = [s[1].shape[0] for s in seqs] + wrapper.plan( + _cu(lq), + _cu(lk), + HQ, + HKV, + D, + causal=True, + sm_scale=SCALE, + q_data_type=torch.bfloat16, + ) + q, k, v = (torch.cat([s[i] for s in seqs]) for i in range(3)) + return _split(wrapper.run(q, k, v), lq) + + return { + "name": "FlashInfer BatchPrefillWithRaggedKVCache", + "source": f"flashinfer {flashinfer.__version__}", + "run": run, + } + + +def candidates() -> list[dict[str, Any]]: + return [ + _guard("rl_kernel_cuda", _rl_kernel), + _guard("torch_sdpa", _sdpa), + _guard("vllm_fa2_auto", lambda: _vllm_fa(0)), + _guard("vllm_fa2_split1", lambda: _vllm_fa(1)), + _guard("vllm_triton_2d", _vllm_triton), + _guard("flashinfer", _flashinfer), + ] + + +# --------------------------------------------------------------------------- # +# Checks +# --------------------------------------------------------------------------- # + + +def _batch_bi(cand, target, others) -> dict[str, bool]: + alone = cand["run"]([target])[0] + first = cand["run"]([target, *others])[0] + last = cand["run"]([*others, target])[-1] + return {"first": _bitwise(first, alone), "last": _bitwise(last, alone)} + + +def _prefill_decode(cand, target) -> dict[str, bool]: + q, k, v = target + full = cand["run"]([target])[0] + result = {} + for rows in (CHUNK, 1): + part = cand["run"]([(q[-rows:], k, v)])[0] + result[f"last_{rows}"] = _bitwise(part, full[-rows:]) + return result + + +def _grad_bi(cand, target, others) -> dict[str, bool]: + g = torch.Generator(device="cuda").manual_seed(5) + dy = torch.randn(target[0].shape, device="cuda", generator=g).to(torch.bfloat16) + alone = cand["grad"](*target, dy) + # A backward is per sequence in every engine here; what can change is the + # kernel the shape selects, so compare against a second target placed after + # other sequences have been differentiated. + for other in others: + cand["grad"](*other, torch.ones_like(other[0])) + again = cand["grad"](*target, dy) + return {name: _bitwise(a, b) for name, a, b in zip(("dq", "dk", "dv"), again, alone)} + + +def _one(cand, target, others) -> dict[str, Any]: + if "unavailable" in cand: + return cand + out = {"key": cand["key"], "name": cand["name"], "source": cand["source"]} + try: + out["batch_bitwise"] = _batch_bi(cand, target, others) + out["prefill_decode_bitwise"] = _prefill_decode(cand, target) + out["repeatable"] = _bitwise(cand["run"]([target])[0], cand["run"]([target])[0]) + if "grad" in cand: + out["backward_repeat_bitwise"] = _grad_bi(cand, target, others) + ref = fp64_reference(*target) + got = cand["run"]([target])[0].double() + out["accuracy"] = { + "rel_l2_vs_fp64": float((got - ref).norm() / ref.norm()), + "max_abs_vs_fp64": float((got - ref).abs().max()), + } + except Exception as exc: # noqa: BLE001 - a crash is a result, not a harness failure + out["failed"] = f"{type(exc).__name__}: {str(exc)[:300]}" + out["batch_invariant"] = bool( + "failed" not in out + and all(out["batch_bitwise"].values()) + and all(out["prefill_decode_bitwise"].values()) + and all(out.get("backward_repeat_bitwise", {"": True}).values()) + ) + return out + + +def _latency(cands) -> dict[str, Any]: + ready = [c for c in cands if "unavailable" not in c] + + def calls_for(seqs): + calls = {} + for cand in ready: + try: + cand["run"](seqs) + except Exception: # noqa: BLE001 - recorded by the BI section + continue + calls[cand["key"]] = lambda c=cand: c["run"](seqs) + return calls + + prefill = { + str(n): time_interleaved(calls_for([make_sequence(n, 300 + n)])) for n in PREFILL_TOKENS + } + decode = time_interleaved(calls_for([make_sequence(1, 401, kv_length=DECODE_KV)])) + q, k, v = make_sequence(BACKWARD_TOKENS, 402) + dy = torch.randn_like(q) + backward = time_interleaved( + {c["key"]: (lambda c=c: c["grad"](q, k, v, dy)) for c in ready if "grad" in c}, + warmup=3, + iters=15, + ) + return { + "prefill_us": prefill, + f"decode_us_kv{DECODE_KV}": decode, + f"forward_plus_backward_us_{BACKWARD_TOKENS}": backward, + } + + +def attention_report() -> dict[str, Any]: + target = make_sequence(TARGET, 1) + others = [make_sequence(n, 10 + i) for i, n in enumerate(COMPANIONS)] + cands = candidates() + return { + "op": "qwen3_next_tp4_attention", + "shape": {"q_heads": HQ, "kv_heads": HKV, "head_dim": D, "scale": SCALE}, + "target_tokens": TARGET, + "companion_tokens": list(COMPANIONS), + "candidates": [_one(c, target, others) for c in cands], + "latency": _latency(cands), + } diff --git a/scripts/plot_qwen3_next_attention_prior_art.py b/scripts/plot_qwen3_next_attention_prior_art.py new file mode 100644 index 000000000..8343c5851 --- /dev/null +++ b/scripts/plot_qwen3_next_attention_prior_art.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Render the TP4 attention prior-art report (``scripts/qwen3_next_attention_prior_art.py``). + + python scripts/plot_qwen3_next_attention_prior_art.py \\ + docs/usage/evidence/qwen3-next-tp4-mixers-b200/attention/report.json + +writes ``figure.png`` next to the report: which invariance checks each +implementation passes, accuracy against FP64, and prefill/decode latency. +""" + +from __future__ import annotations + +import argparse +import json +from pathlib import Path + +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt # noqa: E402 +from matplotlib.colors import ListedColormap # noqa: E402 + +RL_KERNEL = "#2a78d6" +INK = "#2b2b29" +MUTED = "#6f6e69" +GRID = "#e4e3dd" +SURFACE = "#fcfcfb" +PASS = "#1baf7a" +FAIL = "#d64545" +NA = "#e4e3dd" +EXISTING = ("#eb6834", "#b8501f", "#f29a62", "#8a3a15", "#f6bd94", "#d9773f") + +LABELS = { + "rl_kernel_cuda": "RL-Kernel deterministic", + "torch_sdpa": "torch SDPA", + "vllm_fa2_auto": "vLLM FA2 (num_splits auto)", + "vllm_fa2_split1": "vLLM FA2 (num_splits=1)", + "vllm_triton_2d": "vLLM Triton unified (2D)", + "flashinfer": "FlashInfer ragged prefill", +} +COLUMNS = ( + ("batch_bitwise", "first", "batch: first"), + ("batch_bitwise", "last", "batch: last"), + ("prefill_decode_bitwise", "last_64", "chunk: last 64"), + ("prefill_decode_bitwise", "last_1", "decode: last 1"), + ("backward_repeat_bitwise", "dq", "bwd dq"), + ("backward_repeat_bitwise", "dk", "bwd dk"), + ("backward_repeat_bitwise", "dv", "bwd dv"), +) + + +def _style(ax): + ax.set_facecolor(SURFACE) + for side in ("top", "right"): + ax.spines[side].set_visible(False) + ax.tick_params(colors=INK, labelsize=8) + ax.grid(color=GRID, linewidth=0.6) + + +def plot(report: dict, out: Path) -> None: + cands = [c for c in report["candidates"] if "unavailable" not in c and "failed" not in c] + keys = [c["key"] for c in cands] + existing = iter(EXISTING) + colors = {k: RL_KERNEL if k == "rl_kernel_cuda" else next(existing) for k in keys} + labels = [LABELS.get(k, k) for k in keys] + fig, axes = plt.subplots(1, 3, figsize=(17, 4.6), gridspec_kw={"width_ratios": [1.3, 1, 1.4]}) + + ax = axes[0] + grid = [ + [0.5 if c.get(field) is None else float(c[field][item]) for field, item, _ in COLUMNS] + for c in cands + ] + ax.imshow(grid, cmap=ListedColormap([FAIL, NA, PASS]), vmin=0, vmax=1, aspect="auto") + ax.set_xticks(range(len(COLUMNS)), [c[2] for c in COLUMNS], fontsize=7, rotation=90) + ax.set_yticks(range(len(cands)), labels, fontsize=8) + for i, cand in enumerate(cands): + mark = "BI" if cand.get("batch_invariant") else "not BI" + ax.text(len(COLUMNS) - 0.4, i, f" {mark}", va="center", fontsize=8, color=INK) + ax.set_title( + f"Invariance: {report['target_tokens']}-token target alone vs. batched / chunked\n" + "(green bitwise equal, red differs, grey: no backward)", + fontsize=9, + color=INK, + ) + ax.tick_params(length=0) + + ax = axes[1] + _style(ax) + rel = [c["accuracy"]["rel_l2_vs_fp64"] for c in cands] + ax.barh(range(len(cands)), rel, color=[colors[k] for k in keys]) + for i, value in enumerate(rel): + ax.text(value, i, f" {value:.2e}", va="center", fontsize=7, color=MUTED) + ax.set_yticks(range(len(cands)), labels, fontsize=8) + ax.invert_yaxis() + ax.set_xlim(0, 1.35 * max(rel)) + ax.set_xlabel("relative L2 error vs. FP64", fontsize=8) + ax.set_title("Accuracy", fontsize=9, color=INK) + + ax = axes[2] + _style(ax) + prefill = report["latency"]["prefill_us"] + tokens = [int(t) for t in prefill] + for key in keys: + ys = [prefill[str(t)].get(key, float("nan")) / 1000.0 for t in tokens] + ax.plot(tokens, ys, marker="o", ms=3.5, color=colors[key], label=LABELS.get(key, key)) + ax.set_xscale("log", base=2) + ax.set_yscale("log") + ax.set_xlabel("prefill tokens", fontsize=8) + ax.set_ylabel("forward latency (ms, median)", fontsize=8) + ax.set_title("Prefill forward latency\n(4 Q heads, 1 KV head, D=256, causal)", fontsize=9) + ax.legend(fontsize=7, frameon=False) + + env = report["environment"] + fig.suptitle( + f"Qwen3-Next TP4 attention prior art - {env['gpu']}, torch {env['torch']}, " + f"commit {report['rl_kernel_commit'][:7]}", + fontsize=10, + color=INK, + ) + fig.tight_layout() + fig.savefig(out, dpi=160) + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("report", type=Path) + args = parser.parse_args() + report = json.loads(args.report.read_text()) + out = args.report.with_name("figure.png") + plot(report, out) + print(f"wrote {out}") + + +if __name__ == "__main__": + main() diff --git a/scripts/qwen3_next_attention_prior_art.py b/scripts/qwen3_next_attention_prior_art.py new file mode 100644 index 000000000..2ded9d0ca --- /dev/null +++ b/scripts/qwen3_next_attention_prior_art.py @@ -0,0 +1,87 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Write the Qwen3-Next TP4 attention prior-art report (BI + accuracy + latency) as JSON. + +Run it on a clean tree with ``rl_engine._C`` built in place. The report records +the commit, whether the tracked tree was dirty and the version of every library +compared. +``scripts/plot_qwen3_next_attention_prior_art.py`` turns the report into a figure. + + python scripts/qwen3_next_attention_prior_art.py \\ + --out docs/usage/evidence/qwen3-next-tp4-mixers-b200/attention/report.json +""" + +from __future__ import annotations + +import argparse +import importlib.metadata as md +import json +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) + +import torch # noqa: E402 + +from rl_engine.testing.qwen3_next_attention_prior_art import attention_report # noqa: E402 + +LIBRARIES = ( + "vllm", + "flashinfer-python", + "transformers", + "triton", + "sglang", + "megatron-core", + "transformer_engine", + "vime", +) + + +def _git(*args: str) -> str: + return subprocess.run( + ["git", *args], cwd=ROOT, check=True, capture_output=True, text=True + ).stdout.strip() + + +def _version(name: str) -> str | None: + try: + return md.version(name) + except md.PackageNotFoundError: + return None + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--out", type=Path, required=True) + args = parser.parse_args() + if not torch.cuda.is_available(): + raise SystemExit("needs a CUDA device") + torch.backends.cuda.matmul.allow_tf32 = False + torch.backends.cudnn.allow_tf32 = False + report = { + "kind": "qwen3_next_prior_art_report", + "rfc": 428, + "rl_kernel_commit": _git("rev-parse", "HEAD"), + "tracked_tree_dirty": bool(_git("status", "--porcelain", "--untracked-files=no")), + "environment": { + "gpu": torch.cuda.get_device_name(), + "torch": torch.__version__, + "cuda": torch.version.cuda, + "libraries": {name: _version(name) for name in LIBRARIES}, + }, + **attention_report(), + } + args.out.parent.mkdir(parents=True, exist_ok=True) + args.out.write_text(json.dumps(report, indent=2) + "\n") + print( + f"wrote {args.out} (commit {report['rl_kernel_commit'][:7]}, " + f"dirty={report['tracked_tree_dirty']})" + ) + + +if __name__ == "__main__": + main() From c318687e99b67f4b6e55c9a8ca35163f2eb2df9c Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 9 Oct 2026 02:31:53 +0000 Subject: [PATCH 05/68] docs(qwen3-next): TP4 mixer gates and D=256 attention prior-art evidence (B200, 9e6c4f2/6517077) Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/attention.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/operators/attention.md b/docs/operators/attention.md index 074bf9046..95537707f 100644 --- a/docs/operators/attention.md +++ b/docs/operators/attention.md @@ -265,7 +265,7 @@ for measured peak memory at representative shapes. ## Known Limitations -- First version: `D=128` only (Qwen3-8B alignment). +- Head dim: `D=128` (Qwen3-8B) on CUDA and ROCm; `D=256` (Qwen3-Next full attention) on CUDA only. - Supported dtypes: BF16, FP16. - Full materialization of scores/P limits practical sequence length. - `Hq` must be divisible by `Hkv` (raises `ValueError` otherwise). From 92e93a44d18e593038382648bf04dced4935232d Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 9 Oct 2026 02:52:30 +0000 Subject: [PATCH 06/68] feat(qwen3-next): full-model chain with first-drift localisation RFC #428 full_model_chain (CUDA). - Engine adapters: the native vLLM rollout (exact-prefill scheduler, paged KV, traced response rows), the VIME/Megatron replay and training actor, engine weight import/export and the logprob adapter, all on the shared TP4 providers. - Stage runners (preflight, layers, l2, train1, train2, train) with the validated acceptance contract (version 4) and scripts/qwen3_next_run.py, a launcher that works inside any four-GPU allocation: each stage gets a clean clone, an immutable run.json identified by a fresh execution_id, and refuses a predecessor that did not pass or pins another identity. Scheduler-independent helpers live in scripts/qwen3_next_runtime.py. - Sub-node digests: both engines observe the normed mixer input, the mixer output, the normed MLP input, the selected expert ids and the MoE output of every layer, plus the embedding; the first divergence names the layer and sub-node. - scripts/qwen3_next_chain_report.py writes a ChainGateReport-compatible report (schema qwen3-next-chain-v1, compatible with ws1-c10-c11-v5) with first_drift_family/layer/cell; scripts/qwen3_next_publish_evidence.py publishes a chain's records without site paths, hosts or users. - Native rollout caps raised to 64 sequences, 16384 batched tokens and a 4 GiB KV budget so the batch-invariance matrix runs under the same certified profile; the exact-prefill scheduler still fixes every chunk. - The operator-case registry is regenerated from collection by scripts/qwen3_next_operator_cases.py. - Packaging: transformers may be 5.13.1-5.17.0; a qwen3-next extra pins the audited runtime. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/ci.yml | 25 + .github/workflows/qwen3-next-provider-gpu.yml | 3 +- ci/run_qwen3_next_chain_gate.sh | 27 + ci/run_qwen3_next_operator_gates.sh | 10 + docs/usage/qwen3-next-chain.md | 88 ++ pyproject.toml | 10 +- rl_engine/integrations/qwen3_next_binary.py | 71 ++ .../integrations/qwen3_next_embedding.py | 57 + .../integrations/qwen3_next_engine_weights.py | 88 ++ rl_engine/integrations/qwen3_next_forward.py | 45 + rl_engine/integrations/qwen3_next_logprob.py | 89 ++ .../qwen3_next_native_recompute.py | 318 +++++ .../integrations/qwen3_next_native_rollout.py | 777 ++++++++++++ .../qwen3_next_native_training.py | 386 ++++++ .../integrations/qwen3_next_native_workers.py | 49 + rl_engine/integrations/qwen3_next_profile.py | 143 +++ rl_engine/integrations/qwen3_next_subnode.py | 109 ++ .../integrations/qwen3_next_tp_blocks.py | 7 +- rl_engine/integrations/qwen3_next_training.py | 285 +++++ rl_engine/integrations/qwen3_next_vime.py | 402 ++++++ .../integrations/qwen3_next_vime_runtime.py | 178 +++ rl_engine/integrations/qwen3_next_vllm.py | 205 ++++ .../integrations/qwen3_next_vllm_attention.py | 238 ++++ .../integrations/qwen3_next_vllm_model.py | 365 ++++++ .../integrations/qwen3_next_vllm_worker.py | 51 + .../kernels/gtest/qwen3_next_chain_report.py | 268 ++++ rl_engine/testing/pytest_case_manifest.py | 80 ++ rl_engine/testing/qwen3_next_binary.py | 89 ++ rl_engine/testing/qwen3_next_cases.py | 69 ++ .../testing/qwen3_next_export_schema.json | 10 + rl_engine/testing/qwen3_next_l2.py | 1047 ++++++++++++++++ rl_engine/testing/qwen3_next_layer_cases.json | 122 ++ .../testing/qwen3_next_operator_cases.json | 293 +++++ rl_engine/testing/qwen3_next_rl_fixture.py | 150 +++ scripts/qwen3_next_adapter_check.py | 321 +++++ scripts/qwen3_next_chain_report.py | 99 ++ scripts/qwen3_next_checkpoint.py | 312 +++++ scripts/qwen3_next_contract.py | 371 ++++++ scripts/qwen3_next_gate_common.py | 12 +- scripts/qwen3_next_l2.py | 742 ++++++++++++ scripts/qwen3_next_layer_contract.py | 123 ++ scripts/qwen3_next_layers.py | 342 ++++++ scripts/qwen3_next_operator_cases.py | 103 ++ scripts/qwen3_next_operator_gates.py | 151 +++ scripts/qwen3_next_optimizer_checkpoint.py | 307 +++++ scripts/qwen3_next_preflight.py | 252 ++++ scripts/qwen3_next_publish_evidence.py | 142 +++ scripts/qwen3_next_rollout_smoke.py | 73 ++ scripts/qwen3_next_run.py | 337 ++++++ scripts/qwen3_next_runtime.py | 772 ++++++++++++ scripts/qwen3_next_tp_head_check.py | 141 +++ scripts/qwen3_next_tp_probe.py | 54 + scripts/qwen3_next_training.py | 1077 +++++++++++++++++ scripts/qwen3_next_training_checkpoint.py | 602 +++++++++ scripts/qwen3_next_training_export.py | 503 ++++++++ setup.py | 9 +- tests/check_qwen3_next_batch_invariance.py | 453 +++++++ tests/check_qwen3_next_engine_metadata.py | 118 ++ tests/check_qwen3_next_forward.py | 55 +- tests/check_qwen3_next_multigpu.py | 46 + tests/check_qwen3_next_native_rollout.py | 318 +++++ tests/check_qwen3_next_paged_attention.py | 83 ++ tests/check_qwen3_next_vime_weight_hooks.py | 92 ++ tests/check_qwen3_next_vllm_model_contract.py | 281 +++++ tests/test_qwen3_next_binary_evidence.py | 68 ++ tests/test_qwen3_next_case_manifest.py | 106 ++ tests/test_qwen3_next_chain_report.py | 149 +++ tests/test_qwen3_next_checkpoint.py | 222 ++++ tests/test_qwen3_next_engine_weights.py | 81 ++ tests/test_qwen3_next_environment_identity.py | 41 + tests/test_qwen3_next_evidence_redaction.py | 105 ++ tests/test_qwen3_next_failure.py | 136 +++ tests/test_qwen3_next_forward_contract.py | 3 + tests/test_qwen3_next_imported_binary.py | 107 ++ ..._qwen3_next_installed_provider_identity.py | 45 + tests/test_qwen3_next_l2.py | 719 +++++++++++ tests/test_qwen3_next_layers.py | 135 +++ tests/test_qwen3_next_logprob_adapter.py | 43 + tests/test_qwen3_next_native_recompute.py | 232 ++++ tests/test_qwen3_next_operator_gates.py | 50 + tests/test_qwen3_next_profile.py | 149 +++ tests/test_qwen3_next_rl_fixture.py | 58 + tests/test_qwen3_next_rotary_dtype.py | 58 + tests/test_qwen3_next_run.py | 118 ++ tests/test_qwen3_next_storage.py | 334 +++++ tests/test_qwen3_next_subnode.py | 52 + tests/test_qwen3_next_training_audit.py | 137 +++ tests/test_qwen3_next_training_checkpoint.py | 539 +++++++++ tests/test_qwen3_next_training_closeout.py | 430 +++++++ tests/test_qwen3_next_training_export.py | 288 +++++ ...test_qwen3_next_vllm_attention_metadata.py | 75 ++ 91 files changed, 18919 insertions(+), 6 deletions(-) create mode 100755 ci/run_qwen3_next_chain_gate.sh create mode 100644 ci/run_qwen3_next_operator_gates.sh create mode 100644 docs/usage/qwen3-next-chain.md create mode 100644 rl_engine/integrations/qwen3_next_binary.py create mode 100644 rl_engine/integrations/qwen3_next_embedding.py create mode 100644 rl_engine/integrations/qwen3_next_engine_weights.py create mode 100644 rl_engine/integrations/qwen3_next_logprob.py create mode 100644 rl_engine/integrations/qwen3_next_native_recompute.py create mode 100644 rl_engine/integrations/qwen3_next_native_rollout.py create mode 100644 rl_engine/integrations/qwen3_next_native_training.py create mode 100644 rl_engine/integrations/qwen3_next_native_workers.py create mode 100644 rl_engine/integrations/qwen3_next_profile.py create mode 100644 rl_engine/integrations/qwen3_next_subnode.py create mode 100644 rl_engine/integrations/qwen3_next_training.py create mode 100644 rl_engine/integrations/qwen3_next_vime.py create mode 100644 rl_engine/integrations/qwen3_next_vime_runtime.py create mode 100644 rl_engine/integrations/qwen3_next_vllm.py create mode 100644 rl_engine/integrations/qwen3_next_vllm_attention.py create mode 100644 rl_engine/integrations/qwen3_next_vllm_model.py create mode 100644 rl_engine/integrations/qwen3_next_vllm_worker.py create mode 100644 rl_engine/kernels/gtest/qwen3_next_chain_report.py create mode 100644 rl_engine/testing/pytest_case_manifest.py create mode 100644 rl_engine/testing/qwen3_next_binary.py create mode 100644 rl_engine/testing/qwen3_next_cases.py create mode 100644 rl_engine/testing/qwen3_next_export_schema.json create mode 100644 rl_engine/testing/qwen3_next_l2.py create mode 100644 rl_engine/testing/qwen3_next_layer_cases.json create mode 100644 rl_engine/testing/qwen3_next_operator_cases.json create mode 100644 rl_engine/testing/qwen3_next_rl_fixture.py create mode 100644 scripts/qwen3_next_adapter_check.py create mode 100644 scripts/qwen3_next_chain_report.py create mode 100644 scripts/qwen3_next_checkpoint.py create mode 100644 scripts/qwen3_next_contract.py create mode 100644 scripts/qwen3_next_l2.py create mode 100644 scripts/qwen3_next_layer_contract.py create mode 100644 scripts/qwen3_next_layers.py create mode 100644 scripts/qwen3_next_operator_cases.py create mode 100644 scripts/qwen3_next_operator_gates.py create mode 100644 scripts/qwen3_next_optimizer_checkpoint.py create mode 100644 scripts/qwen3_next_preflight.py create mode 100644 scripts/qwen3_next_publish_evidence.py create mode 100644 scripts/qwen3_next_rollout_smoke.py create mode 100644 scripts/qwen3_next_run.py create mode 100644 scripts/qwen3_next_runtime.py create mode 100644 scripts/qwen3_next_tp_head_check.py create mode 100644 scripts/qwen3_next_tp_probe.py create mode 100644 scripts/qwen3_next_training.py create mode 100644 scripts/qwen3_next_training_checkpoint.py create mode 100644 scripts/qwen3_next_training_export.py create mode 100644 tests/check_qwen3_next_batch_invariance.py create mode 100644 tests/check_qwen3_next_engine_metadata.py create mode 100644 tests/check_qwen3_next_multigpu.py create mode 100644 tests/check_qwen3_next_native_rollout.py create mode 100644 tests/check_qwen3_next_paged_attention.py create mode 100644 tests/check_qwen3_next_vime_weight_hooks.py create mode 100644 tests/check_qwen3_next_vllm_model_contract.py create mode 100644 tests/test_qwen3_next_binary_evidence.py create mode 100644 tests/test_qwen3_next_case_manifest.py create mode 100644 tests/test_qwen3_next_chain_report.py create mode 100644 tests/test_qwen3_next_checkpoint.py create mode 100644 tests/test_qwen3_next_engine_weights.py create mode 100644 tests/test_qwen3_next_environment_identity.py create mode 100644 tests/test_qwen3_next_evidence_redaction.py create mode 100644 tests/test_qwen3_next_failure.py create mode 100644 tests/test_qwen3_next_imported_binary.py create mode 100644 tests/test_qwen3_next_installed_provider_identity.py create mode 100644 tests/test_qwen3_next_l2.py create mode 100644 tests/test_qwen3_next_layers.py create mode 100644 tests/test_qwen3_next_logprob_adapter.py create mode 100644 tests/test_qwen3_next_native_recompute.py create mode 100644 tests/test_qwen3_next_operator_gates.py create mode 100644 tests/test_qwen3_next_profile.py create mode 100644 tests/test_qwen3_next_rl_fixture.py create mode 100644 tests/test_qwen3_next_rotary_dtype.py create mode 100644 tests/test_qwen3_next_run.py create mode 100644 tests/test_qwen3_next_storage.py create mode 100644 tests/test_qwen3_next_subnode.py create mode 100644 tests/test_qwen3_next_training_audit.py create mode 100644 tests/test_qwen3_next_training_checkpoint.py create mode 100644 tests/test_qwen3_next_training_closeout.py create mode 100644 tests/test_qwen3_next_training_export.py create mode 100644 tests/test_qwen3_next_vllm_attention_metadata.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d2cef82de..f50c73cef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -68,6 +68,31 @@ jobs: python -m pytest rl_engine/tests/test_dispatch.py -v PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python -m pytest tests/test_attention_correctness.py -q -rs python -m pytest tests/test_forward_invariance.py tests/test_tolerance_contract.py tests/test_ws1_workload.py tests/test_gradient_invariance.py tests/test_elementwise_inventory.py tests/test_four_judgment_matrix.py tests/test_op_checks.py tests/test_operator_inputs.py tests/test_profiler.py tests/test_kv_consistency.py tests/test_ws1_qwen3_dense.py tests/test_ws1_chain_integration.py tests/test_qwen3_next_norm.py tests/test_gdn_state_contract.py tests/test_tensor_identity.py tests/test_qwen3_next_forward_contract.py tests/test_qwen3_next_tp_blocks.py tests/test_qwen3_next_tp_gdn.py -q + python -m pytest -q \ + tests/test_qwen3_next_binary_evidence.py \ + tests/test_qwen3_next_case_manifest.py \ + tests/test_qwen3_next_checkpoint.py \ + tests/test_qwen3_next_engine_weights.py \ + tests/test_qwen3_next_environment_identity.py \ + tests/test_qwen3_next_failure.py \ + tests/test_qwen3_next_imported_binary.py \ + tests/test_qwen3_next_installed_provider_identity.py \ + tests/test_qwen3_next_l2.py \ + tests/test_qwen3_next_layers.py \ + tests/test_qwen3_next_logprob_adapter.py \ + tests/test_qwen3_next_native_recompute.py \ + tests/test_qwen3_next_operator_gates.py \ + tests/test_qwen3_next_profile.py \ + tests/test_qwen3_next_rl_fixture.py \ + tests/test_qwen3_next_rotary_dtype.py \ + tests/test_qwen3_next_storage.py \ + tests/test_qwen3_next_training_audit.py \ + tests/test_qwen3_next_training_closeout.py \ + tests/test_qwen3_next_training_export.py \ + tests/test_qwen3_next_subnode.py \ + tests/test_qwen3_next_chain_report.py \ + tests/test_qwen3_next_run.py \ + tests/test_qwen3_next_evidence_redaction.py - name: Run Cross-Configuration Contract Tests (CPU-safe) run: | diff --git a/.github/workflows/qwen3-next-provider-gpu.yml b/.github/workflows/qwen3-next-provider-gpu.yml index c045484bd..ca5bf6f4d 100644 --- a/.github/workflows/qwen3-next-provider-gpu.yml +++ b/.github/workflows/qwen3-next-provider-gpu.yml @@ -89,4 +89,5 @@ jobs: tests/check_qwen3_next_forward.py tests/check_qwen3_next_attention.py \ tests/check_qwen3_next_gdn_bridge.py tests/check_qwen3_next_gdn_sequence.py \ tests/check_qwen3_next_conv_bridge.py tests/check_qwen3_next_core_matrix.py \ - tests/check_qwen3_next_shared_core.py + tests/check_qwen3_next_shared_core.py \ + tests/check_qwen3_next_paged_attention.py tests/check_qwen3_next_batch_invariance.py diff --git a/ci/run_qwen3_next_chain_gate.sh b/ci/run_qwen3_next_chain_gate.sh new file mode 100755 index 000000000..ac7d8cec0 --- /dev/null +++ b/ci/run_qwen3_next_chain_gate.sh @@ -0,0 +1,27 @@ +#!/usr/bin/env bash +# SPDX-License-Identifier: Apache-2.0 +# Qwen3-Next chain-report adapter checks; with a report path, also validate that report. +# bash ci/run_qwen3_next_chain_gate.sh [chain-report.json] +set -euo pipefail +ROOT="$(cd "$(dirname "$0")/.." && pwd)" +cd "$ROOT" +PY="${PY:-python3}" +export PYTHONPATH="$ROOT${PYTHONPATH:+:$PYTHONPATH}" +"$PY" -m pytest -q tests/test_qwen3_next_chain_report.py +if [ "$#" -ge 1 ]; then + "$PY" - "$1" <<'PY' +import dataclasses, json, sys + +from rl_engine.kernels.gtest.chain_gate import ChainGateReport +from rl_engine.kernels.gtest.qwen3_next_chain_report import COMPATIBLE_SCHEMA, SCHEMA_VERSION + +report = json.load(open(sys.argv[1])) +missing = {f.name for f in dataclasses.fields(ChainGateReport)} - set(report) +assert not missing, f"chain report lacks ChainGateReport fields: {sorted(missing)}" +assert report["schema_version"] == SCHEMA_VERSION, report["schema_version"] +assert report["compatible_schema"] == COMPATIBLE_SCHEMA, report["compatible_schema"] +print(f"passed={report['passed']} first_drift={report['first_drift']} " + f"family={report['first_drift_family']} cells={len(report['cells'])}") +sys.exit(0 if report["passed"] else 1) +PY +fi diff --git a/ci/run_qwen3_next_operator_gates.sh b/ci/run_qwen3_next_operator_gates.sh new file mode 100644 index 000000000..1086730a6 --- /dev/null +++ b/ci/run_qwen3_next_operator_gates.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +# Required operator acceptance and diagnostics use separate declared case sets. +set -euo pipefail +ROOT="$(cd "$(dirname "$0")/.." && pwd)" +cd "$ROOT" +PY="${PY:-python3}" +OUT="${QWEN3_NEXT_EVIDENCE_DIR:?Set QWEN3_NEXT_EVIDENCE_DIR to an artifact directory}" +export PYTHONPATH="$ROOT${PYTHONPATH:+:$PYTHONPATH}" +exec "$PY" scripts/qwen3_next_operator_gates.py --out "$OUT" \ + --suite "${QWEN3_NEXT_OPERATOR_SUITE:-all}" diff --git a/docs/usage/qwen3-next-chain.md b/docs/usage/qwen3-next-chain.md new file mode 100644 index 000000000..01087fda9 --- /dev/null +++ b/docs/usage/qwen3-next-chain.md @@ -0,0 +1,88 @@ +# Qwen3-Next full-model chain (RFC #428 `full_model_chain`) + +The chain checks, bitwise, that Qwen3-Next-80B-A3B-Instruct gives the same +response tokens, BF16 logits and FP32 selected logprobs in rollout (native vLLM +prefill and decode) and in replay (VIME/Megatron recompute), on the pinned +weights and again after two optimizer updates and a checkpoint reload. When a +case differs, the report names the first layer, the sub-node inside it and its +layer family. + +## Requirements + +* One node with four B200 GPUs, TP4 x EP1 (all 512 experts on every rank). +* The audited runtime: CUDA 13.0.88, torch 2.13.0+cu130, vLLM 0.30.0, Triton + 3.7.1, transformers 5.17.0 (`pip install -e ".[qwen3-next]"`), plus VIME and + its patched Megatron-LM in the same virtual environment for the replay and + training stages. The environment is fingerprinted at the start of every run; + installing anything into it between stages invalidates the chain. +* The official checkpoint `Qwen/Qwen3-Next-80B-A3B-Instruct`, revision + `9c7f2fbe84465e40164a94cc16cd30b6999b0cc7`; every shard is hashed before the + first model load. +* Host memory: 256 GB for preflight, 512 GB for `layers`/`l2`, 1.28 TB for the + training stages (`--memory` declares what the allocation grants), and shared + storage for the training checkpoints (about 1.1 TB each) and HF exports. + +## Running + +Inside an allocation with four GPUs, from a clean checkout of the commit to measure: + +```bash +python scripts/qwen3_next_run.py --stage all \ + --runs-root --checkpoint --cuda-home \ + --python /bin/python --until l2 +python scripts/qwen3_next_run.py --stage all --from train1 --until train2 \ + --previous /<...-l2-...> \ + --runs-root --checkpoint --cuda-home --python /bin/python +python scripts/qwen3_next_run.py --stage train --previous /<...-train2-...> \ + --runs-root --checkpoint --cuda-home --python /bin/python +``` + +Each stage gets its own run directory with a clean clone of the commit, an +immutable `run.json` (identified by a fresh `execution_id`) and, once it +passes, a validated `result.json`; a later stage refuses a predecessor that +did not pass or that pins a different source, environment, checkpoint, +provider or topology identity. + +| Stage | What it checks | Typical time | +| --- | --- | --- | +| `preflight` | environment and versions, the mandatory operator cases (`rl_engine/testing/qwen3_next_operator_cases.json`) and norm C3/C4 gates, multi-GPU device guards, a four-rank TP probe, checkpoint hashes, a vLLM rollout smoke | 15 min | +| `layers` | the real-weight TP4 gates (GDN, attention, MoE, GDN engine adapters, embedding/LM head) and the native scheduler on 16 cases; certifies the shared provider profile | 25 min | +| `l2` | 16 cases (prompt 8/64/256/1024 x batch 1/4 x full/chunked prefill, 16 response tokens): rollout vs replay, raw bits, plus every layer and sub-node digest | 30 min | +| `train1`, `train2` | one optimizer update each, checkpoint, HF export, L2 on the exported weights | 3-4 h each | +| `train` | reload, L2, continuous and resumed step-3 controls, checkpoint fault injection | 4 h | + +## Reports + +```bash +python scripts/qwen3_next_chain_report.py --last /<...-train-...> --out chain-report.json +bash ci/run_qwen3_next_chain_gate.sh chain-report.json +python scripts/qwen3_next_publish_evidence.py --last /<...-train-...> \ + --chain-report chain-report.json --out docs/usage/evidence/qwen3-next-chain- +``` + +`chain-report.json` has every field of the Qwen3-8B chain gate's report +(`rl_engine.kernels.gtest.chain_gate.ChainGateReport`, schema `ws1-c10-c11-v5`) +with `schema_version: qwen3-next-chain-v1`; each case at each weight version is +a cell, and rollout/replay logits and logprobs are compared with +`atol = rtol = 0`. A failing chain reports `first_drift` in the dense form +`layer23.router:rollout->recompute`, plus `first_drift_layer`, +`first_drift_family` (`embedding`, `norm`, `gdn`, `full_attention`, `moe` or +`lm_head_logprob`) and `first_drift_cell`. + +The sub-nodes are observed at the same five points of every decoder layer in +both engines (`rl_engine/integrations/qwen3_next_subnode.py`): the normed +mixer input, the mixer output, the normed MLP input, the ten selected expert +ids and the MoE output, plus the embedding once per model. + +`scripts/qwen3_next_publish_evidence.py` copies the stage records and the +report, replaces site paths, host and user names with placeholders, refuses to +publish if any survive, and writes `SHA256SUMS`. + +## Checks outside the default CPU suite + +`tests/check_qwen3_next_*.py` import vLLM and run in the self-hosted +`Qwen3-Next-provider-GPU` workflow and in `preflight`. +`tests/check_qwen3_next_vime_weight_hooks.py`, +`tests/test_qwen3_next_training_checkpoint.py` and +`tests/test_qwen3_next_vllm_attention_metadata.py` need the training +environment (VIME, Megatron) and run there. diff --git a/pyproject.toml b/pyproject.toml index e4940c22f..461b28495 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,7 @@ dependencies = [ "tabulate", "numpy", "accelerate", - "transformers==5.13.1", + "transformers>=5.13.1,<=5.17.0", ] [project.scripts] @@ -31,6 +31,14 @@ rl_kernel = "rl_engine.integrations.vllm_runtime:register_vllm_plugin" cuda = ["flashinfer-python>=0.6.0,<0.7", "nvidia-ml-py"] rocm = ["aiter"] vllm = ["vllm>=0.6.0"] +# The audited Qwen3-Next runtime (RFC #428): every provider check pins these. +qwen3-next = [ + "torch==2.13.0", + "transformers==5.17.0", + "vllm==0.30.0", + "triton==3.7.1", + "flashinfer-python==0.6.18.post1", +] drift-viewer = ["Pillow>=10", "PySide6>=6.6"] dev = ["pytest", "black", "isort", "ruff", "mypy", "pre-commit"] diff --git a/rl_engine/integrations/qwen3_next_binary.py b/rl_engine/integrations/qwen3_next_binary.py new file mode 100644 index 000000000..e883959af --- /dev/null +++ b/rl_engine/integrations/qwen3_next_binary.py @@ -0,0 +1,71 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Identity of the native provider actually imported by a model worker.""" + +import hashlib +import importlib +import importlib.machinery +import os +import re +from pathlib import Path + + +def validate_provider_identity(identity): + """Validate serialized execution evidence without importing GPU code.""" + if ( + not isinstance(identity, dict) + or set(identity) != {"module", "path", "sha256"} + or identity["module"] != "rl_engine._C" + or not isinstance(identity["path"], str) + or not Path(identity["path"]).is_absolute() + or not isinstance(identity["sha256"], str) + or re.fullmatch(r"[0-9a-f]{64}", identity["sha256"]) is None + ): + raise ValueError("Provider binary evidence must identify the actual native rl_engine._C") + return dict(identity) + + +def imported_provider_identity(): + """Really import the extension, hash its current bytes, and reject fallbacks. + + Call at worker initialization and on both sides of each case. No cached file + digest can hide a replacement made between cases or while a case executes. + """ + try: + module = importlib.import_module("rl_engine._C") + except (ImportError, OSError) as error: + raise RuntimeError( + "The required native rl_engine._C provider cannot be imported" + ) from error + spec = getattr(module, "__spec__", None) + filename = getattr(module, "__file__", None) + if ( + getattr(module, "__name__", None) != "rl_engine._C" + or not isinstance(getattr(spec, "loader", None), importlib.machinery.ExtensionFileLoader) + or not isinstance(filename, str) + or not isinstance(getattr(spec, "origin", None), str) + or not any(filename.endswith(suffix) for suffix in importlib.machinery.EXTENSION_SUFFIXES) + ): + raise RuntimeError( + "rl_engine._C is not an imported native extension; fallback is forbidden" + ) + path = Path(filename).resolve(strict=True) + if Path(spec.origin).resolve(strict=True) != path: + raise RuntimeError("Provider module file and import origin disagree") + digest = hashlib.sha256() + with path.open("rb") as stream: + before = os.fstat(stream.fileno()) + for chunk in iter(lambda: stream.read(8 * 1024 * 1024), b""): + digest.update(chunk) + after = os.fstat(stream.fileno()) + + # Detect replacement of the pathname as well as writes to the opened file. + def fingerprint(stat): + return (stat.st_dev, stat.st_ino, stat.st_size, stat.st_mtime_ns, stat.st_ctime_ns) + + if fingerprint(before) != fingerprint(after) or fingerprint(after) != fingerprint(path.stat()): + raise RuntimeError("Provider binary changed while its content identity was being recorded") + return validate_provider_identity( + {"module": "rl_engine._C", "path": str(path), "sha256": digest.hexdigest()} + ) diff --git a/rl_engine/integrations/qwen3_next_embedding.py b/rl_engine/integrations/qwen3_next_embedding.py new file mode 100644 index 000000000..a29ea5b1b --- /dev/null +++ b/rl_engine/integrations/qwen3_next_embedding.py @@ -0,0 +1,57 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Shared TP4 vocabulary embedding with a fixed-order, non-atomic VJP.""" + +import torch +import torch.distributed as dist + +from rl_engine.integrations.qwen3_next_tp import _reduce + + +class _Embedding(torch.autograd.Function): + @staticmethod + def forward(ctx, tokens, weight, rank, group): + flat = tokens.reshape(-1) + local = flat - rank * 37984 + valid = (local >= 0) & (local < 37984) + rows = local.clamp(0, 37983) + value = weight.index_select(0, rows) + value = torch.where(valid[:, None], value, torch.zeros_like(value)) + ctx.save_for_backward(local, valid) + ctx.weight_shape, ctx.weight_dtype = weight.shape, weight.dtype + return _reduce(value, group).reshape(*tokens.shape, 2048) + + @staticmethod + def backward(ctx, dy): + local, valid = ctx.saved_tensors + local = local[valid] + values = dy.reshape(-1, 2048)[valid] + gradient = torch.zeros(ctx.weight_shape, dtype=ctx.weight_dtype, device=dy.device) + order = torch.argsort(local, stable=True) + local, values = local[order], values[order] + unique, counts = torch.unique_consecutive(local, return_counts=True) + start = 0 + # Every row is written once. Pairwise FP32 additions use a fixed tree + # over source token order; there is no floating-point scatter/atomic. + for row, count in zip(unique.cpu().tolist(), counts.cpu().tolist(), strict=True): + part = values[start : start + count].float() + while part.shape[0] > 1: + pairs = part.shape[0] // 2 + merged = part[: 2 * pairs : 2] + part[1 : 2 * pairs : 2] + part = torch.cat((merged, part[-1:]), dim=0) if part.shape[0] % 2 else merged + gradient[row].copy_(part[0].to(ctx.weight_dtype)) + start += count + return None, gradient, None, None + + +def shared_embedding(tokens, weight, group): + if not dist.is_initialized() or group is None or dist.get_world_size(group) != 4: + raise ValueError("Shared Qwen embedding requires an explicit TP4 group") + if tokens.dtype != torch.int64 or tokens.device != weight.device: + raise ValueError("Embedding token IDs must be int64 on the weight device") + if weight.shape != (37984, 2048) or weight.dtype != torch.bfloat16: + raise ValueError("Each rank must own a BF16 [37984, 2048] embedding shard") + if weight.device.type != "cuda" or bool(((tokens < 0) | (tokens >= 151936)).any()): + raise ValueError("Embedding requires valid official vocabulary IDs on CUDA") + return _Embedding.apply(tokens, weight, dist.get_rank(group), group) diff --git a/rl_engine/integrations/qwen3_next_engine_weights.py b/rl_engine/integrations/qwen3_next_engine_weights.py new file mode 100644 index 000000000..a7a82e8ea --- /dev/null +++ b/rl_engine/integrations/qwen3_next_engine_weights.py @@ -0,0 +1,88 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Explicit TP metadata and convolution layout conversion at engine boundaries.""" + +import torch + + +def conv_hf_to_rank_major(weight): + """Arrange HF [Q(all ranks), K(all ranks), V(all ranks)] for a dim-0 TP split.""" + if weight.shape != (8192, 1, 4) or weight.dtype != torch.bfloat16: + raise ValueError("Qwen3-Next convolution must be BF16 [8192, 1, 4]") + q, k, v = weight.split((2048, 2048, 4096)) + return torch.cat([part.chunk(4)[rank] for rank in range(4) for part in (q, k, v)]) + + +def conv_rank_major_to_hf(weight): + """Undo VIME's rank-major TP gather before exporting the official HF layout.""" + if weight.shape != (8192, 1, 4) or weight.dtype != torch.bfloat16: + raise ValueError("Gathered Qwen3-Next convolution must be BF16 [8192, 1, 4]") + shards = [part.split((512, 512, 1024)) for part in weight.chunk(4)] + return torch.cat([shards[rank][segment] for segment in range(3) for rank in range(4)]) + + +def kv_hf_to_rank_major(weight): + if weight.shape != (512, 2048) or weight.dtype != torch.bfloat16: + raise ValueError("Official KV projection requires BF16 [512, 2048]") + first, second = weight.chunk(2) + return torch.cat((first, first, second, second)) + + +def kv_rank_major_to_hf(weight): + from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal + + if weight.shape != (1024, 2048) or weight.dtype != torch.bfloat16: + raise ValueError("Gathered pair-replicated KV requires BF16 [1024, 2048]") + parts = weight.chunk(4) + assert_tensor_bitwise_equal(parts[0], parts[1], name="KV replica pair 0") + assert_tensor_bitwise_equal(parts[2], parts[3], name="KV replica pair 1") + return torch.cat((parts[0], parts[2])) + + +def expert_hf_to_rank_major(gate, up): + if any(value.shape != (512, 2048) or value.dtype != torch.bfloat16 for value in (gate, up)): + raise ValueError("Official expert gate/up require BF16 [512, 2048]") + return torch.cat([value.chunk(4)[rank] for rank in range(4) for value in (gate, up)]) + + +def expert_rank_major_to_hf(weight): + if weight.shape != (1024, 2048) or weight.dtype != torch.bfloat16: + raise ValueError("Gathered fused expert requires BF16 [1024, 2048]") + ranks = [rank.chunk(2) for rank in weight.chunk(4)] + return tuple(torch.cat([ranks[rank][segment] for rank in range(4)]) for segment in range(2)) + + +def set_gdn_tp_metadata(module): + """Tell VIME/Megatron which weights are replicated and how TP gathers them. + + Convolution uses stride one because import/export hooks handle its unequal + Q/K/V segments. The shared norm's gradient is already reduced by TP4GDN. + """ + for name, parameter in module.named_parameters(): + replicated = name == "norm.weight" + parameter.tensor_model_parallel = not replicated + parameter.partition_dim = -1 if replicated else (1 if name == "out_proj.weight" else 0) + parameter.partition_stride = 1 + parameter.sequence_parallel = False + parameter.allreduce = True + + +def validate_training_topology(config, args): + for name, expected in ( + ("tensor_model_parallel_size", 4), + ("pipeline_model_parallel_size", 1), + ("context_parallel_size", 1), + ("expert_model_parallel_size", 1), + ): + if getattr(config, name, None) != expected: + raise ValueError(f"Shared Qwen3-Next requires {name}={expected}") + if getattr(config, "sequence_parallel", False) or getattr(args, "sequence_parallel", False): + raise ValueError("Shared Qwen3-Next requires sequence parallelism disabled") + if getattr(config, "fp32_residual_connection", False): + raise ValueError("Shared Qwen3-Next requires BF16 residual additions") + if any( + getattr(config, name, 0) + for name in ("attention_dropout", "hidden_dropout", "mtp_num_layers") + ): + raise ValueError("Shared Qwen3-Next requires dropout and MTP disabled") diff --git a/rl_engine/integrations/qwen3_next_forward.py b/rl_engine/integrations/qwen3_next_forward.py index 18c892261..872daed72 100644 --- a/rl_engine/integrations/qwen3_next_forward.py +++ b/rl_engine/integrations/qwen3_next_forward.py @@ -17,6 +17,8 @@ import torch.nn.functional as F from torch.autograd.function import once_differentiable +from rl_engine.kernels.logprob_contract import LogprobContract + FORWARD_PROVIDER_ID = "qwen3-next-shared-cuda-forward-v1" @@ -362,3 +364,46 @@ def shared_attention(q, k, v): if q.shape[2] > k.shape[2]: raise ValueError("Query tokens must fit within the supplied KV prefix") return _attention_op()(q, k, v, causal=True, scale=1 / 16) + + +def shared_logprob(local_logits, targets, *, contract, tp_group=None, with_entropy=True): + """Return FP32 (selected logprob, LSE, optional entropy) with 64 vocab tiles. + + The existing CUDA WS2 provider supplies forward and autograd. Native VIME + policy_loss requests entropy even with a zero regularization coefficient. + Both invocation modes therefore use its same entropy-capable path, keeping + selected-logprob forward/backward identical across training and rollout. + """ + _cuda_tensors(local_logits, targets) + if not isinstance(contract, LogprobContract): + raise ValueError("An explicit LogprobContract is required") + if contract.sharding.real_vocab_size != 151936 or contract.sharding.padded_vocab_size != 151936: + raise ValueError("Qwen3-Next logprob requires the full 151936-entry vocabulary") + if contract.sharding.cp_world_size != 1 or contract.sharding.tp_world_size not in (1, 4): + raise ValueError("Qwen3-Next logprob requires CP1 and TP4 (TP1 for component tests)") + if local_logits.dtype not in (torch.bfloat16, torch.float32): + raise ValueError("Qwen3-Next logits must be BF16 or FP32") + if targets.dtype not in (torch.int32, torch.int64): + raise ValueError("Qwen3-Next targets must have integer token IDs") + if not isinstance(with_entropy, bool): + raise ValueError("with_entropy must be a bool") + if not torch.isfinite(local_logits).all(): + raise ValueError("Qwen3-Next logits must be finite") + from rl_engine.kernels.ops.cuda.loss.vocab_parallel_logp import ( + CudaVocabParallelLogprobOp, + native_tile_stats_available, + ) + + if not native_tile_stats_available(): + raise RuntimeError("Qwen3-Next logprob requires rebuilt CUDA tile-stat kernels") + selected, lse, entropy = CudaVocabParallelLogprobOp().apply_with_entropy( + local_logits, + targets, + contract=contract, + tp_group=tp_group, + num_vocab_tiles=64, + validate=True, + with_entropy_grad=with_entropy, + deterministic=True, + ) + return selected, lse, entropy if with_entropy else None diff --git a/rl_engine/integrations/qwen3_next_logprob.py b/rl_engine/integrations/qwen3_next_logprob.py new file mode 100644 index 000000000..4848ec020 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_logprob.py @@ -0,0 +1,89 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Explicit shared Qwen TP4 logprob/entropy boundary for native VIME policy loss.""" + +import torch +import torch.distributed as dist + +from rl_engine.integrations.qwen3_next_forward import shared_logprob +from rl_engine.kernels.logprob_contract import ( + LogprobContract, + LogprobDType, + LogprobRole, + MaskSpec, + ReductionSpec, + ShardingSpec, +) + + +def qwen_logprob_contract(logits, *, rank, world_size, role=LogprobRole.TRAIN): + if world_size != 4 or type(rank) is not int or not 0 <= rank < 4: + raise ValueError("Full Qwen logprob execution requires TP4") + if logits.ndim != 2 or logits.shape[1] != 37984: + raise ValueError("Each Qwen TP4 rank must own 37984 vocabulary columns") + dtype = {torch.bfloat16: LogprobDType.BF16, torch.float32: LogprobDType.FP32}.get(logits.dtype) + if dtype is None: + raise ValueError("Qwen LM head produces BF16 logits, optionally promoted by VIME") + return LogprobContract( + role=role, + dtype=dtype, + mask=MaskSpec(num_tokens=logits.shape[0], active_mask=(True,) * logits.shape[0]), + sharding=ShardingSpec( + tp_rank=rank, + tp_world_size=4, + vocab_shard_bounds=tuple((index * 37984, (index + 1) * 37984) for index in range(4)), + real_vocab_size=151936, + padded_vocab_size=151936, + cp_rank=0, + cp_world_size=1, + ), + reduction=ReductionSpec(), + ) + + +def calculate_log_probs_and_entropy( + logits, + tokens, + tp_group, + with_entropy=False, + chunk_size=-1, + log_prob_keep_mask=None, + with_entropy_grad=True, +): + """VIME's numerical callback; its policy loss and response alignment stay native.""" + if log_prob_keep_mask is not None: + raise ValueError("Fixed Qwen eager profile does not support top-p replay") + if not dist.is_initialized() or tp_group is None: + raise ValueError("Shared Qwen logprob requires an explicit initialized TP4 group") + if logits.device.type != "cuda" or tokens.device != logits.device: + raise ValueError("Logits and target IDs must share a CUDA device") + if tokens.shape != (logits.shape[0],): + raise ValueError("Logprob target IDs must match the token rows") + contract = qwen_logprob_contract( + logits, rank=dist.get_rank(tp_group), world_size=dist.get_world_size(tp_group) + ) + # Keep one fixed numerical path even when VIME requests chunking or metrics + # only. Its chunk_size is a memory hint; this explicit provider owns tiling. + selected, _lse, entropy = shared_logprob( + logits, tokens, contract=contract, tp_group=tp_group, with_entropy=True + ) + if not with_entropy_grad: + entropy = entropy.detach() + return selected.unsqueeze(-1), entropy if with_entropy else None + + +def install_vime_logprob_provider(): + """Install explicitly in each worker, without modifying the shared VIME checkout.""" + from megatron.core import mpu + from vime.backends.megatron_utils import loss + + topology = (mpu.get_context_parallel_world_size(), mpu.get_tensor_model_parallel_world_size()) + if topology != (1, 4): + raise ValueError("The Qwen native policy adapter supports TP4/CP1 only") + original = loss.calculate_log_probs_and_entropy + if original is calculate_log_probs_and_entropy: + return + if original.__module__ != "vime.utils.ppo_utils": + raise ValueError("Refusing to replace an unknown VIME logprob provider") + loss.calculate_log_probs_and_entropy = calculate_log_probs_and_entropy diff --git a/rl_engine/integrations/qwen3_next_native_recompute.py b/rl_engine/integrations/qwen3_next_native_recompute.py new file mode 100644 index 000000000..a59e89dc7 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_native_recompute.py @@ -0,0 +1,318 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Independent native VIME full-model recomputation from frozen tokens only.""" + +import hashlib +import os +import sys +import traceback +from datetime import timedelta +from pathlib import Path +from uuid import uuid4 + +import torch +import torch.distributed as dist +import torch.multiprocessing as mp + +from rl_engine.integrations import qwen3_next_subnode as subnode +from rl_engine.testing.qwen3_next_l2 import ( + embedding_digest, + layer_digest, + load_tensor_artifact, + save_tensor_artifact, + validate_model_artifact, + validate_native_worker_traces, +) +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +def response_rows(value, request): + """Keep original BF16 values at P-1..P+14 for every packed sample.""" + batch = len(request["prompt_tokens"]) + length = len(request["consumed_input_tokens"][0]) + if value.dtype not in (torch.bfloat16, torch.int64) or value.numel() == 0: + raise ValueError("Response traces must preserve nonempty original BF16/int64 storage") + value = value.reshape(-1, value.shape[-1]) + if value.shape[0] < batch * length: + raise ValueError("Packed model output does not cover every frozen sample") + positions = torch.tensor(request["prediction_positions"], device=value.device) + if positions.shape != (16,) or positions[-1] != length - 1: + raise ValueError("Response positions must end before consuming the last target token") + return torch.stack([value[index * length + positions] for index in range(batch)]) + + +def _rank_main(rank, cases, staging): + os.environ.update(LOCAL_RANK=str(rank), RANK=str(rank), WORLD_SIZE="4") + torch.cuda.set_device(rank) + dist.init_process_group( + "nccl", + init_method=f"file://{staging}/rendezvous", + rank=rank, + world_size=4, + timeout=timedelta(minutes=30), + device_id=torch.device("cuda", rank), + ) + try: + from megatron.core import mpu + from megatron.core.packed_seq_params import PackedSeqParams + from qwen3_next_contract import atomic_json + + from rl_engine.integrations.qwen3_next_binary import imported_provider_identity + from rl_engine.integrations.qwen3_next_forward import shared_logprob + from rl_engine.integrations.qwen3_next_logprob import qwen_logprob_contract + from rl_engine.integrations.qwen3_next_vime_runtime import create_actor + + first = cases[0][0] + args, model, optimizer, scheduler = create_actor( + first["identity"]["checkpoint"], training=False + ) + if optimizer is not None or scheduler is not None or len(model) != 1: + raise ValueError("L2 recompute requires one native actor without an optimizer") + raw = model[0] + while hasattr(raw, "module"): + raw = raw.module + if len(raw.decoder.layers) != 48: + raise ValueError("Native recompute must execute all 48 decoder layers") + model[0].eval() + provider_binary = imported_provider_identity() + group = mpu.get_tensor_model_parallel_group() + for case_index, (request, _output) in enumerate(cases): + if request["identity"] != first["identity"] or request["role"] != "recompute": + raise ValueError("A native actor batch cannot mix model identities or roles") + if rank == 0: + atomic_json( + Path(staging).parent / "active-case.json", + { + "case_id": request["case_id"], + "phase": "native_recompute", + "request": str(Path(_output).with_suffix(".request.json")), + }, + ) + consumed = torch.tensor( + request["consumed_input_tokens"], device="cuda", dtype=torch.long + ) + batch, length = consumed.shape + if length != request["state_position"] + 1: + raise ValueError("Recompute recurrent-state position is inconsistent") + cu = torch.arange(batch + 1, device="cuda", dtype=torch.int32) * length + packed = PackedSeqParams( + cu_seqlens_q=cu, + cu_seqlens_kv=cu, + max_seqlen_q=length, + max_seqlen_kv=length, + qkv_format="thd", + ) + traces, handles = {}, [] + local_logits = [] + + def capture_output(index): + def hook(_module, _inputs, output): + hidden = output[0] if isinstance(output, tuple) else output + traces.setdefault(index, {})["output"] = ( + response_rows(hidden, request).detach().cpu() + ) + + return hook + + def capture_state(index): + def hook(state, indices, _cu): + slots = indices[:batch].long() + if (index + 1) % 4: + values = { + "convolution": state.convolution[slots], + "recurrent_state": state.recurrent[slots], + } + else: + chosen = slots.cpu().tolist() + values = { + "key": torch.cat([state.keys[i] for i in chosen]), + "value": torch.cat([state.values[i] for i in chosen]), + } + traces.setdefault(index, {})["states"] = { + key: value.detach().cpu() for key, value in values.items() + } + + return hook + + def capture_logits(_module, _inputs, output): + if local_logits: + raise ValueError( + "Native L2 recompute must run exactly one full forward per case" + ) + local_logits.append(response_rows(output[0], request).detach()) + + def capture_subnode(index, name, value): + nodes = traces.setdefault(index, {}).setdefault("subnodes", {}) + if name in nodes: + raise ValueError(f"Sub-node {name} of layer {index} was observed twice") + nodes[name] = response_rows(value, request).detach().cpu() + + subnode.install(raw.decoder.layers, subnode.vime_modules, capture_subnode) + embedding = raw.embedding.word_embeddings + + def capture_embedding(_name, value): + if "embedding" in traces.setdefault("model", {}): + raise ValueError("The embedding was observed twice in one forward") + traces["model"]["embedding"] = response_rows(value, request).detach().cpu() + + embedding.subnode_hook = capture_embedding + for index, layer in enumerate(raw.decoder.layers): + layer.self_attention.trace_state_hook = capture_state(index) + handles.append(layer.register_forward_hook(capture_output(index))) + handles.append(raw.output_layer.register_forward_hook(capture_logits)) + try: + with torch.no_grad(): + model[0]( + input_ids=consumed.reshape(1, -1), + position_ids=None, + attention_mask=None, + labels=None, + packed_seq_params=packed, + ) + if len(local_logits) != 1 or len(traces) != 49: + raise ValueError("Native model omitted an output or diagnostic hook") + local = local_logits[0].reshape(-1, 37984) + targets = torch.tensor( + request["response_tokens"], device="cuda", dtype=torch.long + ).reshape(-1) + selected, _lse, _entropy = shared_logprob( + local, + targets, + contract=qwen_logprob_contract(local, rank=rank, world_size=4), + tp_group=group, + with_entropy=True, + ) + peers = [torch.empty_like(local) for _ in range(4)] + dist.all_gather(peers, local.contiguous(), group=group) + record = { + "rank": rank, + "request_sha256": request["request_sha256"], + "role": "recompute", + "execution": request["execution"], + "provider_binary": provider_binary, + "layers": traces, + "local_logits": local.reshape(batch, 16, 37984).cpu(), + "responses": targets.reshape(batch, 16).cpu(), + "logprobs": selected.reshape(batch, 16).cpu(), + } + if rank == 0: + record["logits"] = torch.cat(peers, dim=-1).reshape(batch, 16, 151936).cpu() + save_tensor_artifact(Path(staging) / f"case-{case_index}-rank-{rank}.pt", record) + del peers, local + local_logits.clear() + finally: + for handle in handles: + handle.remove() + for layer in raw.decoder.layers: + layer.self_attention.trace_state_hook = None + subnode.remove(raw.decoder.layers, subnode.vime_modules) + embedding.subnode_hook = None + dist.barrier() + except BaseException: + # One rank must not take the others down by deadlock: tearing down the + # process group while its peers sit in a collective can block forever. + # Report, then exit so the spawning parent terminates + # the sibling ranks and the stage fails instead of running out its budget. + traceback.print_exc() + sys.stderr.flush() + os._exit(3) + finally: + dist.destroy_process_group() + + +def _assemble(request, ranks, records): + if len(ranks) != 4 or [item.get("rank") for item in ranks] != list(range(4)): + raise ValueError("Recompute artifacts must cover all four distinct TP ranks") + result = { + "metadata": { + key: request[key] + for key in ("schema_version", "case_id", "role", "identity", "request_sha256") + }, + "execution": request["execution"], + "prompt_tokens": torch.tensor(request["prompt_tokens"], dtype=torch.int64), + "response_tokens": torch.tensor(request["response_tokens"], dtype=torch.int64), + "prediction_positions": torch.tensor(request["prediction_positions"], dtype=torch.int64), + "logits": ranks[0]["logits"], + "logprobs": ranks[0]["logprobs"], + "layer_digests": [], + "embedding_digest": embedding_digest(ranks[0]["layers"]["model"]["embedding"]), + "debug_tensors": {}, + "worker_traces": records, + "provider_binary": ranks[0]["provider_binary"], + } + for peer in ranks[1:]: + exact( + peer["layers"]["model"]["embedding"], + ranks[0]["layers"]["model"]["embedding"], + name="TP replicated embedding", + ) + for peer in ranks: + if peer.get("provider_binary") != result["provider_binary"]: + raise ValueError("Recompute TP ranks loaded different CUDA provider binaries") + if peer["request_sha256"] != request["request_sha256"]: + raise ValueError("Rank result belongs to a different request") + exact(peer["logprobs"], result["logprobs"], name="TP selected logprob replica") + for layer in range(48): + hidden = ranks[0]["layers"][layer]["output"] + states = {} + for rank, peer in enumerate(ranks): + exact(peer["layers"][layer]["output"], hidden, name=f"TP layer {layer} replica") + states.update( + { + f"tp{rank}.{name}": value + for name, value in peer["layers"][layer]["states"].items() + } + ) + nodes = ranks[0]["layers"][layer]["subnodes"] + for peer in ranks[1:]: + for name in subnode.SUBNODES: + exact(peer["layers"][layer]["subnodes"][name], nodes[name], name=f"TP {name}") + nodes = {name: nodes[name] for name in subnode.SUBNODES} + result["layer_digests"].append( + layer_digest(layer, hidden, states, request=request, subnodes=nodes) + ) + if layer in request["trace_layers"]: + result["debug_tensors"][f"layer.{layer}.output"] = hidden + result["debug_tensors"].update( + {f"layer.{layer}.subnode.{name}": value for name, value in nodes.items()} + ) + result["debug_tensors"].update( + {f"layer.{layer}.{name}": value for name, value in states.items()} + ) + validate_model_artifact(result, request) + return result + + +def run_recompute_cases(cases): + cases = [(request, str(output)) for request, output in cases] + if not cases: + raise ValueError("Native recompute batch cannot be empty") + staging = Path(cases[0][1]).parent / f"native-recompute-{uuid4().hex}" + staging.mkdir(parents=True, exist_ok=False) + # Spawn preserves the current isolated-venv interpreter. Each rank loads + # the real model once, and all ranks exit before another GPU role starts. + mp.spawn(_rank_main, args=(cases, str(staging)), nprocs=4, join=True) + for index, (request, output) in enumerate(cases): + files = [staging / f"case-{index}-rank-{rank}.pt" for rank in range(4)] + records = [] + for rank, path in enumerate(files): + with path.open("rb") as stream: + digest = hashlib.file_digest(stream, "sha256").hexdigest() + records.append( + { + "rank": rank, + "path": str(path.absolute()), + "sha256": digest, + "request_sha256": request["request_sha256"], + } + ) + artifact = _assemble(request, [load_tensor_artifact(path) for path in files], records) + validate_native_worker_traces(artifact, request, artifact_root=staging.parent) + # Retain original per-rank hidden/state tensors for historical validation + # and first-divergence diagnosis after old HF exports have been retired. + save_tensor_artifact(Path(output), artifact) + + +def run_recompute_case(request, output): + run_recompute_cases([(request, output)]) diff --git a/rl_engine/integrations/qwen3_next_native_rollout.py b/rl_engine/integrations/qwen3_next_native_rollout.py new file mode 100644 index 000000000..912354597 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_native_rollout.py @@ -0,0 +1,777 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Actual vLLM scheduler rollout, with raw shared-provider L2 observations. + +Qualification leaves backend capabilities closed; certified execution requires +the source/environment-bound profile certificate. The scheduler audits actual decisions; +configuration hints alone are never reported as executed prefill chunks. +""" + +import gc +import hashlib +import os +from pathlib import Path + +import torch +from vllm.v1.core.sched.interface import PauseState +from vllm.v1.core.sched.scheduler import Scheduler +from vllm.v1.sample.logits_processor import AdapterLogitsProcessor + +from rl_engine.integrations import qwen3_next_subnode as subnode +from rl_engine.integrations.qwen3_next_binary import ( + imported_provider_identity, + validate_provider_identity, +) +from rl_engine.testing.qwen3_next_l2 import ( + embedding_digest, + json_digest, + layer_digest, + load_tensor_artifact, + save_tensor_artifact, + validate_debug_capture, + validate_model_artifact, +) +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + +NATIVE_ROLLOUT_PROFILE = { + # Caps, not schedules: ExactPrefillScheduler fixes every step's chunks. They + # are sized so up to 64 concurrent requests fit one certified profile. + "kv_cache_memory_bytes_per_gpu": 4 << 30, + "max_model_len": 1040, + "max_num_seqs": 64, + "max_num_batched_tokens": 16384, + "scheduler": "rl_engine.integrations.qwen3_next_native_rollout.ExactPrefillScheduler", + "async_scheduling": False, + "prefix_caching": False, + "weights_dtype": "bfloat16", + "recurrent_state_dtype": "float32", + "model_runner": "v1", +} + + +def capabilities(): + return { + "rollout": True, + "debug_layers": True, + "architecture_layers": 48, + "tp": 4, + "batched_cases": True, + "native_scheduler": True, + "scheduler_batch_invariance_verified": False, + "full_model_l2_verified": False, + } + + +def runtime_profile(config): + scheduler = config.scheduler_config.scheduler_cls + if isinstance(scheduler, type): + scheduler = scheduler.__module__ + "." + scheduler.__qualname__ + actual = { + "kv_cache_memory_bytes_per_gpu": config.cache_config.kv_cache_memory_bytes, + "max_model_len": config.model_config.max_model_len, + "max_num_seqs": config.scheduler_config.max_num_seqs, + "max_num_batched_tokens": config.scheduler_config.max_num_batched_tokens, + "scheduler": scheduler, + "async_scheduling": config.scheduler_config.async_scheduling, + "prefix_caching": config.cache_config.enable_prefix_caching, + "weights_dtype": str(config.model_config.dtype).removeprefix("torch."), + "recurrent_state_dtype": config.cache_config.mamba_ssm_cache_dtype, + "model_runner": "v2" if config.use_v2_model_runner else "v1", + } + if actual != NATIVE_ROLLOUT_PROFILE: + raise ValueError(f"Native runtime changed the explicit rollout profile: {actual}") + return actual + + +def validate_request(request): + if request.get("role") != "rollout" or request.get("schema_version") != 1: + raise ValueError("A version1 rollout request is required") + digest_input = {key: value for key, value in request.items() if key != "request_sha256"} + if json_digest(digest_input) != request.get("request_sha256"): + raise ValueError("Rollout request identity mismatch") + prompts, samples = request["prompt_tokens"], request["sample_ids"] + batch = len(prompts) + if ( + batch not in (1, 4) + or len(samples) != batch + or any(type(sample) is not int for sample in samples) + or len(set(samples)) != batch + ): + raise ValueError("Rollout requires one or four uniquely identified samples") + length = len(prompts[0]) + if length not in (8, 64, 256, 1024) or any(len(row) != length for row in prompts): + raise ValueError("Rollout prompts must use one official matrix length") + responses = request["response_tokens"] + for rows in (prompts, responses or []): + if any(type(token) is not int or not 0 <= token < 151936 for row in rows for token in row): + raise ValueError("Rollout tokens must be integers in the official vocabulary") + if responses is not None and ( + len(responses) != batch or any(len(row) != 16 for row in responses) + ): + raise ValueError("Frozen responses must have exactly sixteen tokens per sample") + chunks = request["prefill_chunks"] + if ( + not chunks + or any(type(value) is not int or value <= 0 for value in chunks) + or sum(chunks) != length + ): + raise ValueError("Exact positive prefill chunks must cover the prompt") + expected_execution = { + "rollout_mode": "autoregressive", + "prefill_chunks": chunks, + "response_mode": "generate" if responses is None else "teacher_forced", + } + if ( + request["execution"] != expected_execution + or request["response_length"] != 16 + or request["vocab_size"] != 151936 + or request["logits_dtype"] != "bfloat16" + or request["logprobs_dtype"] != "float32" + or request["prediction_positions"] != list(range(length - 1, length + 15)) + or request["consumed_response_tokens"] != 15 + or request["state_position"] != length + 14 + or request["required_layer_digests"] != list(range(48)) + or request["generation"] + != {"temperature": 0.0, "top_p": 1.0, "top_k": -1, "seed": 1234, "ignore_eos": True} + ): + raise ValueError("Rollout execution or response offsets differ from the pinned contract") + if responses is None: + if batch != 4 or chunks != [length] or request["consumed_input_tokens"] is not None: + raise ValueError("Only baseline B4 full prefill may define frozen responses") + elif request["consumed_input_tokens"] != [ + prompt + response[:-1] for prompt, response in zip(prompts, responses, strict=True) + ]: + raise ValueError("Consumed input must end before response token sixteen") + if any(type(layer) is not int or layer not in range(48) for layer in request["trace_layers"]): + raise ValueError("Debug layer indices must be in [0,48)") + identity = request["identity"] + if identity["architecture_layers"] != 48 or identity["topology"] != { + "tp": 4, + "pp": 1, + "cp": 1, + "ep": 1, + }: + raise ValueError("Rollout cannot change depth or topology") + if not Path(identity["checkpoint"]).is_absolute(): + raise ValueError("Checkpoint identity must use an absolute shared path") + if ( + identity["provider_config"].get("native_rollout_kv_cache_memory_bytes") + != NATIVE_ROLLOUT_PROFILE["kv_cache_memory_bytes_per_gpu"] + ): + raise ValueError("Provider identity must pin the native rollout cache budget") + return batch, length + + +def next_scheduled_tokens(computed, prompt_length, chunks): + boundary = 0 + for size in chunks: + if computed == boundary: + return size + boundary += size + if prompt_length <= computed < prompt_length + 15: + return 1 + raise ValueError("Native scheduler is not at an exact requested chunk/decode boundary") + + +class ExactPrefillScheduler(Scheduler): + """Use native cache allocation/scheduling while enforcing one frozen case at a time.""" + + def schedule(self, throttle_prefills=False): + active = [request for request in self.requests.values() if not request.is_finished()] + if not active: + return super().schedule(throttle_prefills) + descriptors = [ + (request.sampling_params.extra_args or {}).get("shared_l2") for request in active + ] + if any(item is None for item in descriptors): + raise ValueError("Exact Qwen scheduler accepts only identified L2 requests") + first = descriptors[0] + contract = ( + first["request_sha256"], + first["batch_size"], + first["prompt_length"], + first["prefill_chunks"], + ) + if any( + ( + item["request_sha256"], + item["batch_size"], + item["prompt_length"], + item["prefill_chunks"], + ) + != contract + for item in descriptors + ): + raise ValueError("Different L2 cases cannot share a scheduler batch") + if len(active) < first["batch_size"]: + # EngineCore may receive the four add-request messages separately. + # Its normal paused scheduler emits no work until all are admitted. + previous = self._pause_state + self._pause_state = PauseState.PAUSED_ALL + try: + result = super().schedule(throttle_prefills) + finally: + self._pause_state = previous + if result.total_num_scheduled_tokens: + raise RuntimeError("The native scheduler violated the input batch barrier") + return result + if len(active) != first["batch_size"] or len( + {item["sample_id"] for item in descriptors} + ) != len(active): + raise ValueError("L2 scheduler sample cardinality mismatch") + counts = {request.num_computed_tokens for request in active} + if len(counts) != 1: + raise RuntimeError("Native requests lost their synchronized exact-chunk boundary") + computed = counts.pop() + count = next_scheduled_tokens(computed, first["prompt_length"], first["prefill_chunks"]) + self.scheduler_config.long_prefill_token_threshold = count + result = super().schedule(throttle_prefills) + expected = {request.request_id: count for request in active} + if result.num_scheduled_tokens != expected or result.preempted_req_ids: + raise RuntimeError( + "exact_scheduler_mismatch: " + f"expected {expected}, actual {result.num_scheduled_tokens}" + ) + return result + + +class ForceFrozenTokens(AdapterLogitsProcessor): + """Sampling-only teacher forcing; raw model logits are observed beforehand.""" + + def is_argmax_invariant(self): + return False + + def new_req_logits_processor(self, params): + descriptor = (params.extra_args or {}).get("shared_l2") + if descriptor is None: + raise ValueError("L2 sampling requires explicit request metadata") + frozen = descriptor["response_tokens"] + if frozen is None: + return None + if len(frozen) != 16 or any( + type(token) is not int or not 0 <= token < 151936 for token in frozen + ): + raise ValueError("Teacher forcing requires sixteen frozen official token IDs") + + def force(output_ids, logits): + index = len(output_ids) + if index not in range(16): + raise ValueError("Teacher forcing exceeded the fixed response length") + selected = torch.full_like(logits, float("-inf")) + selected[frozen[index]] = 0 + return selected + + return force + + +def file_digest(path): + digest = hashlib.sha256() + with Path(path).open("rb") as stream: + for chunk in iter(lambda: stream.read(8 * 1024 * 1024), b""): + digest.update(chunk) + return digest.hexdigest() + + +class RolloutTrace: + """Worker-local observations of real engine buffers, keyed by immutable sample IDs.""" + + def __init__(self, worker, request, output_directory): + from rl_engine.integrations.qwen3_next_vllm_model import RLKernelQwen3NextForCausalLM + + self.batch, self.prompt_length = validate_request(request) + self.worker, self.request, self.directory = worker, request, Path(output_directory) + self.provider_binary = imported_provider_identity() + if self.provider_binary != worker.shared_provider_binary: + raise RuntimeError("Provider binary changed after native worker initialization") + self.rank = worker.rank + self.native_runtime_profile = runtime_profile(worker.vllm_config) + self.provider_execution_mode = worker.shared_provider_execution_mode + expected_mode = ( + "certified" + if request["identity"].get("scheduler_profile_certificate") + else "qualification" + ) + if self.provider_execution_mode != expected_mode: + raise ValueError("Worker provider certification differs from the immutable request") + self.model = worker.get_model() + if not isinstance(self.model, RLKernelQwen3NextForCausalLM) or worker.use_v2_model_runner: + raise ValueError( + "Trace requires the actual shared model and native V1 GPU model runner" + ) + self.row_by_sample = {sample: row for row, sample in enumerate(request["sample_ids"])} + self.request_rows, self.progress = {}, {} + self.current, self.schedule = [], [] + self.logits = torch.empty(self.batch, 16, 151936, dtype=torch.bfloat16) + self.local_logits = torch.empty(self.batch, 16, 37984, dtype=torch.bfloat16) + self.logprobs = torch.empty(self.batch, 16, dtype=torch.float32) + self.responses = torch.full((self.batch, 16), -1, dtype=torch.int64) + self.hidden = torch.empty(48, self.batch, 16, 2048, dtype=torch.bfloat16) + self.hidden_written = torch.zeros(48, self.batch, 16, dtype=torch.bool) + self.states = [ + ( + {"convolution": [None] * self.batch, "recurrent_state": [None] * self.batch} + if (layer + 1) % 4 + else {"key": [None] * self.batch, "value": [None] * self.batch} + ) + for layer in range(48) + ] + self.model.model.shared_forward_hook = self.begin_forward + self.model.model.shared_trace_hook = self.layer + self.model.shared_logits_hook = self.record_logits + self.subnodes = subnode.SubnodeBuffer(self.batch) + subnode.install(self.model.model.layers, subnode.vllm_modules, self.subnode) + self.model.model.subnode_hook = self.embedding + + def embedding(self, name, packed): + for row, begin, end, computed in self.current: + for offset in range(end - begin): + response = computed + offset - (self.prompt_length - 1) + if 0 <= response < 16: + self.subnodes.put_embedding(row, response, packed[begin + offset]) + + def subnode(self, layer_index, name, packed): + for row, begin, end, computed in self.current: + for offset in range(end - begin): + response = computed + offset - (self.prompt_length - 1) + if 0 <= response < 16: + self.subnodes.put(layer_index, name, row, response, packed[begin + offset]) + + def before_execute(self, scheduled): + for new in scheduled.scheduled_new_reqs: + descriptor = (new.sampling_params.extra_args or {}).get("shared_l2", {}) + if descriptor.get("request_sha256") != self.request["request_sha256"]: + raise ValueError("Scheduler request belongs to a different L2 case") + row = self.row_by_sample[descriptor["sample_id"]] + if ( + new.prompt_token_ids != self.request["prompt_tokens"][row] + or new.num_computed_tokens != 0 + ): + raise ValueError("Native prompt differs or prefix reuse was applied") + if new.req_id in self.request_rows or row in self.request_rows.values(): + raise ValueError("Native scheduler duplicated a sample") + self.request_rows[new.req_id], self.progress[new.req_id] = row, 0 + self.scheduled = scheduled + if not scheduled.total_num_scheduled_tokens: + return + if len(scheduled.num_scheduled_tokens) != self.batch or scheduled.preempted_req_ids: + raise RuntimeError("The actual native step did not preserve its required batch") + step = {} + for req_id, count in scheduled.num_scheduled_tokens.items(): + computed = self.progress[req_id] + expected = next_scheduled_tokens( + computed, self.prompt_length, self.request["prefill_chunks"] + ) + if count != expected: + raise RuntimeError( + "Observed worker tokens disagree with requested prefill/decode chunks" + ) + step[req_id] = (self.request_rows[req_id], computed, count) + self.step = step + + def begin_forward(self, input_ids, positions): + ids = list(self.worker.model_runner.input_batch.req_ids) + if set(ids) != set(self.step): + raise ValueError("Actual model input batch differs from SchedulerOutput") + self.current = [] + begin = 0 + for req_id in ids: + row, computed, count = self.step[req_id] + end = begin + count + if positions[begin:end].cpu().tolist() != list(range(computed, computed + count)): + raise ValueError("Native absolute positions differ from the scheduled offsets") + known = self.request["prompt_tokens"][row] + self.responses[row].tolist() + if input_ids[begin:end].cpu().tolist() != known[computed : computed + count]: + raise ValueError( + "Native engine consumed tokens other than the prompt/frozen response prefix" + ) + self.current.append((row, begin, end, computed)) + self.progress[req_id] = computed + count + begin = end + if begin != self.scheduled.total_num_scheduled_tokens: + raise ValueError("Packed model input does not cover actual scheduled tokens") + self.schedule.append( + { + "samples": [self.request["sample_ids"][row] for row, _, _, _ in self.current], + "computed_before": [computed for _, _, _, computed in self.current], + "token_counts": [end - begin for _, begin, end, _ in self.current], + } + ) + + def layer(self, layer_index, output, layer): + from vllm.forward_context import get_forward_context + from vllm.model_executor.layers.mamba.mamba_utils import is_conv_state_dim_first + + from rl_engine.integrations.qwen3_next_vllm import metadata_sequences + from rl_engine.integrations.qwen3_next_vllm_attention import SharedAttentionMetadata + + if output.dtype != torch.bfloat16 or output.shape[1:] != (2048,): + raise ValueError("Layer observation did not preserve original BF16 hidden states") + metadata = get_forward_context().attn_metadata + if not isinstance(metadata, dict): + raise ValueError("Native engine did not supply per-layer attention metadata") + for row, begin, end, computed in self.current: + for offset in range(end - begin): + response = computed + offset - (self.prompt_length - 1) + if 0 <= response < 16: + if self.hidden_written[layer_index, row, response]: + raise ValueError("A response hidden state was observed more than once") + self.hidden[layer_index, row, response].copy_( + output[begin + offset].detach().cpu() + ) + self.hidden_written[layer_index, row, response] = True + if not any( + computed + end - begin == self.prompt_length + 15 + for _, begin, end, computed in self.current + ): + return + if (layer_index + 1) % 4: + module = layer.linear_attn + meta = metadata[module.prefix] + indices, cu, _reset = metadata_sequences( + meta, self.scheduled.total_num_scheduled_tokens + ) + if cu.tolist() != [0] + [end for _, _, end, _ in self.current]: + raise ValueError("GDN metadata reordered sequences without matching model inputs") + conv, recurrent = module.kv_cache + if not is_conv_state_dim_first(): + conv = conv.transpose(-1, -2) + for (row, begin, end, computed), slot in zip( + self.current, indices.tolist(), strict=True + ): + if computed + end - begin == self.prompt_length + 15: + self.states[layer_index]["convolution"][row] = conv[slot].detach().cpu().clone() + self.states[layer_index]["recurrent_state"][row] = ( + recurrent[slot].detach().cpu().clone() + ) + else: + attention = layer.self_attn.attn + meta, cache = metadata[attention.layer_name], attention.kv_cache + if not isinstance(meta, SharedAttentionMetadata): + raise ValueError("Full attention did not use shared native paged-KV metadata") + if list(meta.query_start_loc) != [0] + [end for _, _, end, _ in self.current]: + raise ValueError("Full attention boundaries differ from actual packed input") + block_size = cache.shape[2] + for index, (row, begin, end, computed) in enumerate(self.current): + length = computed + end - begin + if length != self.prompt_length + 15: + continue + if meta.seq_lens[index] != length: + raise ValueError("Full attention prefix length differs from consumed inputs") + blocks = meta.block_table[index, : (length + block_size - 1) // block_size].long() + values = cache[blocks, 0].reshape(-1, 512)[:length] + self.states[layer_index]["key"][row] = ( + values[:, :256].reshape(1, length, 256).detach().cpu().clone() + ) + self.states[layer_index]["value"][row] = ( + values[:, 256:].reshape(1, length, 256).detach().cpu().clone() + ) + + def record_logits(self, hidden, local, full): + from rl_engine.integrations.qwen3_next_forward import shared_logprob + from rl_engine.integrations.qwen3_next_logprob import qwen_logprob_contract + from rl_engine.kernels.logprob_contract import LogprobRole + + if full.shape != (len(self.current), 151936) or local.shape != (len(self.current), 37984): + raise ValueError( + "Native sampling rows do not correspond one-to-one with active requests" + ) + eligible = [ + (index, row, computed + end - begin - self.prompt_length) + for index, (row, begin, end, computed) in enumerate(self.current) + if computed + end - begin >= self.prompt_length + ] + if not eligible: + return + rows = torch.tensor([index for index, _, _ in eligible], device=local.device) + targets = torch.tensor( + [ + ( + int(full[index].argmax()) + if self.request["response_tokens"] is None + else self.request["response_tokens"][row][position] + ) + for index, row, position in eligible + ], + device=local.device, + dtype=torch.int64, + ) + selected = local.index_select(0, rows) + contract = qwen_logprob_contract( + selected, rank=self.rank, world_size=4, role=LogprobRole.INFER + ) + logp, _, _ = shared_logprob( + selected, targets, contract=contract, tp_group=self.model.shared_tp_group + ) + for count, (index, row, position) in enumerate(eligible): + if position not in range(16) or self.responses[row, position] != -1: + raise ValueError("Sampling observation has a duplicate or invalid response offset") + self.logits[row, position].copy_(full[index].detach().cpu()) + self.local_logits[row, position].copy_(local[index].detach().cpu()) + self.logprobs[row, position].copy_(logp[count].detach().cpu()) + self.responses[row, position] = int(targets[count]) + + def finish(self): + if imported_provider_identity() != self.provider_binary: + raise RuntimeError("Provider binary changed during the rollout case") + if not self.hidden_written.all() or bool((self.responses < 0).any()): + raise RuntimeError("Rollout did not observe every response position in every layer") + for layer in range(48): + self.subnodes.layer(layer) + if len(self.schedule) != len(self.request["prefill_chunks"]) + 15: + raise RuntimeError("Actual scheduler step count differs from the exact chunk contract") + states = [] + for layer in self.states: + if any(value is None for values in layer.values() for value in values): + raise RuntimeError("A final convolution/recurrent/KV state was not observed") + states.append({name: torch.stack(values) for name, values in layer.items()}) + payload = { + "rank": self.rank, + "request_sha256": self.request["request_sha256"], + "logits": self.logits, + "local_logits": self.local_logits, + "logprobs": self.logprobs, + "responses": self.responses, + "hidden": self.hidden, + "states": states, + "subnodes": {name: self.subnodes.values[name] for name in subnode.SUBNODES}, + "embedding": self.subnodes.embedding_rows(), + "schedule": self.schedule, + "native_runtime_profile": self.native_runtime_profile, + "provider_execution_mode": self.provider_execution_mode, + "provider_binary": self.provider_binary, + } + self.directory.mkdir(parents=True, exist_ok=True) + path = self.directory / f"rank-{self.rank}.pt" + save_tensor_artifact(path, payload) + self.model.model.shared_forward_hook = self.model.model.shared_trace_hook = None + self.model.shared_logits_hook = None + subnode.remove(self.model.model.layers, subnode.vllm_modules) + self.model.model.subnode_hook = None + return { + "rank": self.rank, + "path": str(path), + "sha256": file_digest(path), + "request_sha256": self.request["request_sha256"], + } + + +def assemble_trace(request, records, sampled_responses): + if len(records) != 4 or sorted(record["rank"] for record in records) != list(range(4)): + raise ValueError("Rollout trace requires all four unique TP ranks") + shards = [] + for record in sorted(records, key=lambda value: value["rank"]): + if ( + record["request_sha256"] != request["request_sha256"] + or file_digest(record["path"]) != record["sha256"] + ): + raise ValueError("Worker trace identity/hash mismatch") + shard = load_tensor_artifact(record["path"]) + if shard["rank"] != record["rank"] or shard["request_sha256"] != request["request_sha256"]: + raise ValueError("Worker trace content identity mismatch") + shards.append(shard) + first = shards[0] + provider_binary = validate_provider_identity(first.get("provider_binary")) + for shard in shards[1:]: + if validate_provider_identity(shard.get("provider_binary")) != provider_binary: + raise ValueError("TP workers imported different provider binaries") + for shard in shards[1:]: + for name in ("logits", "logprobs", "responses", "hidden"): + exact(shard[name], first[name], name=f"TP replicated rollout {name}") + for name in subnode.SUBNODES: + exact(shard["subnodes"][name], first["subnodes"][name], name=f"TP replicated {name}") + exact(shard["embedding"], first["embedding"], name="TP replicated embedding") + if shard["schedule"] != first["schedule"]: + raise ValueError("TP workers observed different scheduler decisions") + if shard["native_runtime_profile"] != first["native_runtime_profile"]: + raise ValueError("TP workers use different native runtime profiles") + if shard["provider_execution_mode"] != first["provider_execution_mode"]: + raise ValueError("TP workers use different provider certification modes") + if first["native_runtime_profile"] != NATIVE_ROLLOUT_PROFILE: + raise ValueError("Worker artifacts do not identify the pinned native runtime") + expected_mode = ( + "certified" if request["identity"].get("scheduler_profile_certificate") else "qualification" + ) + if first["provider_execution_mode"] != expected_mode: + raise ValueError("Worker artifacts do not match the requested certification mode") + exact( + torch.cat([shard["local_logits"] for shard in shards], dim=-1), + first["logits"], + name="raw LM-head vocabulary gather", + ) + exact( + torch.tensor(sampled_responses, dtype=torch.int64), + first["responses"], + name="actual native sampled response", + ) + layers, debug = [], {} + for layer in range(48): + states = { + f"tp{rank}.{name}": value + for rank, shard in enumerate(shards) + for name, value in shard["states"][layer].items() + } + hidden = first["hidden"][layer] + nodes = {name: first["subnodes"][name][layer] for name in subnode.SUBNODES} + layers.append(layer_digest(layer, hidden, states, request=request, subnodes=nodes)) + if layer in request["trace_layers"]: + debug[f"layer.{layer}.output"] = hidden + debug.update({f"layer.{layer}.subnode.{name}": value for name, value in nodes.items()}) + debug.update({f"layer.{layer}.{name}": value for name, value in states.items()}) + artifact = { + "metadata": { + key: request[key] + for key in ("schema_version", "case_id", "role", "identity", "request_sha256") + }, + "execution": request["execution"], + "prompt_tokens": torch.tensor(request["prompt_tokens"], dtype=torch.int64), + "response_tokens": first["responses"], + "prediction_positions": torch.tensor(request["prediction_positions"], dtype=torch.int64), + "logits": first["logits"], + "logprobs": first["logprobs"], + "layer_digests": layers, + "embedding_digest": embedding_digest(first["embedding"]), + "debug_tensors": debug, + "native_scheduler_steps": first["schedule"], + "worker_traces": records, + "native_runtime_profile": first["native_runtime_profile"], + "provider_execution_mode": first["provider_execution_mode"], + "provider_binary": provider_binary, + } + validate_model_artifact(artifact, request) + validate_debug_capture(artifact, request) + return artifact + + +def _new_engine(identity): + from vllm import LLM + + from rl_engine.integrations.qwen3_next_profile import activate_for_identity + from rl_engine.integrations.qwen3_next_vllm_model import register_shared_model + + activate_for_identity(identity) + if os.environ.get("VLLM_USE_V2_MODEL_RUNNER") != "0": + raise RuntimeError( + "Set VLLM_USE_V2_MODEL_RUNNER=0 before importing the native rollout worker" + ) + if torch.cuda.device_count() != 4: + raise RuntimeError("Native rollout requires exactly four visible GPUs") + return LLM(**engine_arguments(identity, register_shared_model())) + + +def engine_arguments(identity, architecture): + """Keep explicit engine arguments independently checkable against pinned vLLM.""" + return dict( + model=identity["checkpoint"], + tokenizer=identity["checkpoint"], + skip_tokenizer_init=True, + trust_remote_code=False, + hf_overrides={"architectures": [architecture]}, + additional_config={"qwen3_next_shared_identity": identity, "gdn_prefill_backend": "triton"}, + tensor_parallel_size=4, + pipeline_parallel_size=1, + distributed_executor_backend="mp", + dtype="bfloat16", + seed=1234, + enforce_eager=True, + enable_prefix_caching=False, + enable_chunked_prefill=True, + max_model_len=1040, + max_num_seqs=NATIVE_ROLLOUT_PROFILE["max_num_seqs"], + max_num_batched_tokens=NATIVE_ROLLOUT_PROFILE["max_num_batched_tokens"], + long_prefill_token_threshold=1, + scheduler_cls="rl_engine.integrations.qwen3_next_native_rollout.ExactPrefillScheduler", + async_scheduling=False, + worker_cls="rl_engine.integrations.qwen3_next_vllm_worker.SharedQwenWorker", + logits_processors=["rl_engine.integrations.qwen3_next_native_rollout:ForceFrozenTokens"], + kv_cache_dtype="bfloat16", + mamba_cache_dtype="bfloat16", + mamba_ssm_cache_dtype="float32", + mamba_cache_mode="none", + generation_config="vllm", + gpu_memory_utilization=0.85, + kv_cache_memory_bytes=NATIVE_ROLLOUT_PROFILE["kv_cache_memory_bytes_per_gpu"], + attention_config={"backend": "CUSTOM"}, + kernel_config={ + "moe_backend": "triton", + "enable_flashinfer_autotune": False, + "enable_jit_warmup": False, + }, + compilation_config={"mode": 0, "cudagraph_mode": "NONE"}, + ) + + +def run_rollout_cases(cases): + from vllm import SamplingParams + + engine, identity = None, None + try: + for request, output in cases: + batch, length = validate_request(request) + if engine is None: + identity = request["identity"] + engine = _new_engine(identity) + elif request["identity"] != identity: + raise ValueError( + "One native rollout process cannot silently change weight/provider identity" + ) + output = Path(output).resolve() + records = engine.collective_rpc( + "configure_l2_trace", args=(request, str(output.with_suffix(".worker-traces"))) + ) + if len(records) != 4 or any( + record["request_sha256"] != request["request_sha256"] for record in records + ): + raise RuntimeError("All TP workers must acknowledge the exact trace request") + sampling = [] + for row, sample in enumerate(request["sample_ids"]): + descriptor = { + "request_sha256": request["request_sha256"], + "sample_id": sample, + "batch_size": batch, + "prompt_length": length, + "prefill_chunks": request["prefill_chunks"], + "response_tokens": ( + None + if request["response_tokens"] is None + else request["response_tokens"][row] + ), + } + sampling.append( + SamplingParams( + temperature=0, + top_p=1, + top_k=-1, + seed=1234, + ignore_eos=True, + max_tokens=16, + detokenize=False, + extra_args={"shared_l2": descriptor}, + ) + ) + generated = engine.generate( + [{"prompt_token_ids": row} for row in request["prompt_tokens"]], + sampling, + use_tqdm=False, + ) + if len(generated) != batch or any( + len(item.outputs) != 1 or item.prompt_token_ids != request["prompt_tokens"][row] + for row, item in enumerate(generated) + ): + raise RuntimeError( + "Native engine did not return one identified completion per prompt" + ) + responses = [list(item.outputs[0].token_ids) for item in generated] + if any(len(row) != 16 for row in responses): + raise RuntimeError("Native engine did not generate sixteen tokens") + records = engine.collective_rpc("collect_l2_trace") + save_tensor_artifact(output, assemble_trace(request, records, responses)) + finally: + if engine is not None: + engine.llm_engine.engine_core.shutdown() + del engine + gc.collect() + + +def run_rollout_case(request, output): + run_rollout_cases([(request, output)]) diff --git a/rl_engine/integrations/qwen3_next_native_training.py b/rl_engine/integrations/qwen3_next_native_training.py new file mode 100644 index 000000000..e252de3c7 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_native_training.py @@ -0,0 +1,386 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Native four-rank RL updates and restart controls, separated from rollout GPUs.""" + +import json +import os +import re +import sys +import traceback +from datetime import timedelta +from pathlib import Path +from uuid import uuid4 + +import torch +import torch.distributed as dist +import torch.multiprocessing as mp + +from rl_engine.testing.qwen3_next_l2 import load_tensor_artifact, validate_fixture +from rl_engine.testing.qwen3_next_rl_fixture import PolicyFixture + + +def policy_fixture(path): + frozen = load_tensor_artifact(path) + validate_fixture(frozen) + return PolicyFixture.freeze( + torch.cat((frozen["prompts"]["64"], frozen["responses"]["64"]), dim=1), + frozen["old_logprobs"]["64"], + baseline_identity=frozen["baseline_identity"], + ) + + +def active_parameter_groups(model): + """Every parameter tensor in the full actor must participate in the update. + + Routed experts use one fused parameter per layer: unselected expert rows + may be zero, while at least one selected row must carry a real gradient. + """ + from rl_engine.integrations.qwen3_next_training import named_trainable_parameters + + parameters = named_trainable_parameters(model) + validate_active_parameter_names(parameters) + return {name: (name,) for name in parameters} + + +def validate_active_parameter_names(parameters): + layers = { + int(match.group(1)) + for name in parameters + for match in [re.search(r"(?:^|\.)decoder\.layers\.(\d+)\.", name)] + if match + } + if layers != set(range(48)): + raise ValueError("Active parameters must cover exactly the full 48 decoder layers") + for layer in range(48): + for component in ("self_attention.", "mlp.", "pre_mlp_layernorm."): + prefix = f"decoder.layers.{layer}.{component}" + if not any(prefix in name for name in parameters): + raise ValueError(f"Full actor omits an active parameter path: {prefix}") + for component in ("embedding.", "output_layer.", "final_layernorm."): + if not any(component in name for name in parameters): + raise ValueError(f"Full actor omits {component}") + + +def policy_step(args, model, optimizer, scheduler, fixture, *, step, required_groups): + """Track consumed samples only after native loss/backward/update/scheduler succeed.""" + from rl_engine.integrations.qwen3_next_training import native_policy_update + + before, after = (step - 1) * 4, step * 4 + if args.consumed_train_samples != before or scheduler.num_steps != before: + raise ValueError("Native update starts from inconsistent sample/scheduler progress") + audit = native_policy_update( + args, model, optimizer, scheduler, fixture, step=step, required_groups=required_groups + ) + if scheduler.num_steps != after or args.consumed_train_samples != before: + raise ValueError("Native VIME scheduler/sample-counter behavior differs from pinned API") + # Pinned VIME train_one_step advances the scheduler but owns no consumed + # sample counter. The controller advances that counter after its success. + args.consumed_train_samples = after + audit["progress"] = { + "consumed_before": before, + "consumed_after": after, + "scheduler_before": before, + "scheduler_after": after, + "sample_counter_owner": "qwen3_next_native_training.policy_step", + } + return audit + + +def retire_prior_checkpoints(checkpoint, root, identity): + """Called only after all-rank publication/load verifies the successor bytes.""" + from qwen3_next_runtime import retire_directory + + checkpoint, root = Path(checkpoint), Path(root) + if ( + root.is_symlink() + or checkpoint.is_symlink() + or checkpoint.resolve().parent != root.resolve() + ): + raise ValueError("Checkpoint retention is restricted to this run's direct directory") + checkpoint, root = checkpoint.resolve(), root.resolve() + owner = root / "RUN_OWNER.json" + if owner.is_symlink() or json.loads(owner.read_text()) != identity: + raise ValueError("Checkpoint retention lacks a matching run owner") + successor = json.loads((checkpoint / "COMPLETE.json").read_text()) + if successor.get("identity") != identity or successor.get("status") != "complete": + raise ValueError("Checkpoint successor is not a published state for this run") + retired = [] + for path in root.iterdir(): + if ( + not path.is_dir() + or path == checkpoint + or path.suffix == ".partial" + or path.name.startswith(".") + ): + continue + marker = path / "COMPLETE.json" + if path.is_symlink() or marker.is_symlink(): + raise ValueError("Checkpoint retention cannot follow symlinks") + previous = json.loads(marker.read_text()) + if ( + previous.get("identity") != identity + or previous.get("status") != "complete" + or previous.get("step", -1) >= successor["step"] + ): + raise ValueError("Checkpoint retention found an unrelated or ambiguous successor") + retirement = retire_directory( + path, + root, + identity, + proof={ + "complete": previous, + "successor": str(checkpoint), + "successor_complete": successor, + }, + ) + retired.append({"path": str(path), "complete": previous, "retirement": retirement}) + return retired + + +def _rank_main(rank, request, staging): + from qwen3_next_contract import atomic_json + + os.environ.update(RANK=str(rank), LOCAL_RANK=str(rank), WORLD_SIZE="4") + torch.cuda.set_device(rank) + dist.init_process_group( + "nccl", + init_method=f"file://{staging}/rendezvous", + rank=rank, + world_size=4, + timeout=timedelta(minutes=60), + device_id=torch.device("cuda", rank), + ) + try: + from megatron.core.tensor_parallel.random import get_cuda_rng_tracker + from qwen3_next_training_checkpoint import ( + _rank_zero_call, + load_training_checkpoint, + save_training_checkpoint, + training_state_identity, + ) + from qwen3_next_training_export import export_training_weights + + from rl_engine.integrations.qwen3_next_vime_runtime import create_actor + from rl_engine.testing.qwen3_next_binary import verify_imported_provider + + fixture = policy_fixture(request["fixture"]) + manifest = request["manifest"] + identity = request["identity"] + provider_binary = verify_imported_provider( + request["provider_binary"], source_tree=identity["source_tree"] + ) + if identity["fixture_sha256"] != fixture.content_sha256: + raise ValueError("Native RL worker fixture identity changed") + args, model, optimizer, scheduler = create_actor(manifest["checkpoint"], training=True) + if optimizer is None or scheduler is None or len(model) != 1: + raise ValueError("Native RL worker requires the actual optimizer and scheduler") + tracker = get_cuda_rng_tracker() + groups = active_parameter_groups(model) + phase = request["phase"] + record = { + "rank": rank, + "phase": phase, + "identity": identity, + "pid": os.getpid(), + "provider_binary": provider_binary, + "native_entrypoints": [ + "vime.backends.megatron_utils.model.setup_model_and_optimizer", + "vime.backends.megatron_utils.model.train_one_step", + "vime.backends.megatron_utils.loss.policy_loss_function", + ], + } + restored = None + if request.get("checkpoint"): + restored = load_training_checkpoint( + args, + model, + optimizer, + scheduler, + fixture, + request["checkpoint"], + identity=identity, + tracker=tracker, + ) + record["restored"] = restored + expected_previous = { + "update1": 0, + "update2": 1, + "reload": 2, + "continuous": 0, + "resumed": 2, + }[phase] + actual_previous = restored["step"] if restored else 0 + recover_published_update = phase in ("update1", "update2") and actual_previous == ( + 1 if phase == "update1" else 2 + ) + if actual_previous != expected_previous and not recover_published_update: + raise ValueError("Native RL update resumed from the wrong complete step") + if phase in ("continuous", "resumed"): + record["controls"] = {} + for step in (1, 2, 3) if phase == "continuous" else (3,): + audit = policy_step( + args, model, optimizer, scheduler, fixture, step=step, required_groups=groups + ) + state = training_state_identity( + args, + model, + optimizer, + scheduler, + fixture, + step=step, + sample_position=step * 4, + identity=identity, + tracker=tracker, + ) + expected = request["expected_states"][str(step)][rank] if step < 3 else None + if expected is not None and state != expected: + differing = sorted( + key + for key in expected.keys() | state.keys() + if expected.get(key) != state.get(key) + ) + atomic_json( + Path(staging) / f"divergence-rank-{rank}.json", + { + "rank": rank, + "step": step, + "differing": differing, + "expected": expected, + "actual": state, + "update": audit, + }, + ) + raise ValueError( + f"Continuous control diverged from original update {step} " + f"on rank {rank}: {', '.join(differing)}" + ) + record["controls"][str(step)] = {"update": audit, "state": state} + verify_imported_provider( + request["provider_binary"], source_tree=identity["source_tree"] + ) + atomic_json(Path(staging) / f"rank-{rank}.json", record) + dist.barrier() + return + step = 1 if phase == "update1" else 2 + if recover_published_update: + record["update"] = restored["rank_evidence"]["update_evidence"] + if record["update"].get("step") != step: + raise ValueError("Published checkpoint omits the committed update evidence") + record["recovered_committed_update"] = True + checkpoint = request["checkpoint"] + elif phase != "reload": + record["update"] = policy_step( + args, model, optimizer, scheduler, fixture, step=step, required_groups=groups + ) + saved = save_training_checkpoint( + args, + model, + optimizer, + scheduler, + fixture, + request["checkpoint_root"], + step=step, + sample_position=step * 4, + identity=identity, + tracker=tracker, + export_bytes=request["export_bytes"], + update_evidence=record["update"], + ) + record["saved"] = saved + checkpoint = saved["checkpoint"] + else: + checkpoint = request["checkpoint"] + record["retired_checkpoints"] = _rank_zero_call( + lambda: retire_prior_checkpoints(checkpoint, request["checkpoint_root"], identity), + "retire previous complete checkpoints after verified successor", + ) + checkpoint_bytes = sum( + path.stat().st_size for path in Path(checkpoint).rglob("*") if path.is_file() + ) + # Export must reflect the reloaded BF16 actor in the reload phase. Never + # reuse the update2 export as purported evidence that loading worked. + record["export"] = export_training_weights( + args, + model, + request["export_root"], + step=step, + weight_version={"update1": "update-1", "update2": "update-2", "reload": "reload-2"}[ + phase + ], + identity=identity, + base_checkpoint=manifest["checkpoint"], + base_checkpoint_identity=manifest["checkpoint_identity"], + checkpoint_bytes=checkpoint_bytes, + retained_complete_bytes=checkpoint_bytes, + ) + record["state"] = training_state_identity( + args, + model, + optimizer, + scheduler, + fixture, + step=step, + sample_position=step * 4, + identity=identity, + tracker=tracker, + ) + verify_imported_provider(request["provider_binary"], source_tree=identity["source_tree"]) + atomic_json(Path(staging) / f"rank-{rank}.json", record) + dist.barrier() + except BaseException: + # One rank must not take the others down by deadlock: tearing down the + # process group while its peers sit in a collective can block forever. + # Report, then exit so the spawning parent terminates + # the sibling ranks and the stage fails instead of running out its budget. + traceback.print_exc() + sys.stderr.flush() + os._exit(3) + finally: + dist.destroy_process_group() + + +def run_training_phase(request, output): + """One fresh native actor lifetime, then all CUDA processes exit.""" + from qwen3_next_contract import atomic_json + + if request["phase"] not in ("update1", "update2", "reload", "continuous", "resumed"): + raise ValueError("Unknown native RL phase") + if torch.cuda.device_count() != 4: + raise ValueError("Native training requires exactly four visible GPUs") + identity, manifest = request["identity"], request["manifest"] + if ( + identity.get("topology") != {"tp": 4, "pp": 1, "cp": 1, "ep": 1} + or identity.get("architecture_layers") != 48 + or identity.get("checkpoint") != manifest.get("checkpoint") + or identity.get("checkpoint_identity") != manifest.get("checkpoint_identity") + ): + raise ValueError("Native training request changed base checkpoint/depth/topology") + staging = Path(output).parent / f"native-training-{uuid4().hex}" + staging.mkdir(parents=True, exist_ok=False) + mp.spawn(_rank_main, args=(request, str(staging)), nprocs=4, join=True) + records = [json.loads((staging / f"rank-{rank}.json").read_text()) for rank in range(4)] + for rank, record in enumerate(records): + if (record["rank"], record["phase"], record["identity"]) != ( + rank, + request["phase"], + request["identity"], + ): + raise ValueError("Native RL rank record identity mismatch") + if record.get("export") != records[0].get("export"): + raise ValueError("Native RL ranks disagree on the published HF export") + if record.get("provider_binary") != records[0].get("provider_binary"): + raise ValueError("Native RL ranks imported different CUDA provider binaries") + result = { + "schema_version": 1, + "status": "passed", + "phase": request["phase"], + "identity": request["identity"], + "ranks": records, + "export": records[0].get("export"), + "checkpoint": records[0].get("saved", {}).get("checkpoint", request.get("checkpoint")), + "all_rank_processes_exited": True, + "provider_binary": request["provider_binary"], + } + atomic_json(output, result) + return result diff --git a/rl_engine/integrations/qwen3_next_native_workers.py b/rl_engine/integrations/qwen3_next_native_workers.py new file mode 100644 index 000000000..f101e1133 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_native_workers.py @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Implemented native model entrypoints; availability is not acceptance evidence.""" + + +def capabilities(): + from rl_engine.integrations import qwen3_next_native_recompute as recompute + from rl_engine.integrations import qwen3_next_native_rollout as rollout + + ready = rollout.capabilities() + if any( + not callable(getattr(module, name, None)) + for module, names in ( + (rollout, ("run_rollout_case", "run_rollout_cases")), + (recompute, ("run_recompute_case", "run_recompute_cases")), + ) + for name in names + ): + raise RuntimeError("The native model worker implementation is incomplete") + return { + **ready, + "recompute": True, + "independent_processes_required": True, + "full_model_l2_verified": False, + "training_and_reload_verified": False, + } + + +def run_rollout_cases(cases): + from rl_engine.integrations.qwen3_next_native_rollout import run_rollout_cases as run + + # Preserve the lazy generator: baseline generation freezes response tokens + # before the subsequent teacher-forced cases are constructed. + return run(cases) + + +def run_rollout_case(request, output): + return run_rollout_cases([(request, output)]) + + +def run_recompute_cases(cases): + from rl_engine.integrations.qwen3_next_native_recompute import run_recompute_cases as run + + return run(cases) + + +def run_recompute_case(request, output): + return run_recompute_cases([(request, output)]) diff --git a/rl_engine/integrations/qwen3_next_profile.py b/rl_engine/integrations/qwen3_next_profile.py new file mode 100644 index 000000000..8a7ae2f63 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_profile.py @@ -0,0 +1,143 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Activate only the bounded CUSTOM profile after verifying native layer evidence. + +Capability is process-local and false by default. Qualification runs use BI=0; +strict runs use BI=1 plus the immutable predecessor certificate in their model +identity. Generic vLLM GDN_ATTN registration is never modified. +""" + +import json +import os +import subprocess +import sys +from pathlib import Path + +_CERTIFIED = None +ROOT = Path(__file__).resolve().parents[2] + + +def profile_is_certified(): + return _CERTIFIED is not None and os.environ.get("VLLM_BATCH_INVARIANT") == "1" + + +def validate_certified_weights(identity, certified): + """The base certificate is not a certificate for newly updated weight bytes.""" + from rl_engine.testing.qwen3_next_l2 import validate_weight_identity + + validate_weight_identity(identity) + if identity.get("weight_version", "baseline") == "baseline": + if any( + identity.get(key) != certified.get(key) for key in ("checkpoint", "checkpoint_identity") + ): + raise ValueError("Baseline weights differ from the certified official checkpoint") + return + if any( + ( + identity.get("base_checkpoint") != certified.get("checkpoint"), + identity.get("base_checkpoint_identity") != certified.get("checkpoint_identity"), + ) + ): + raise ValueError("Updated export refers to a different certified official base") + from qwen3_next_training_export import validate_training_export + + verified = validate_training_export(identity["checkpoint"], identity["checkpoint_identity"]) + if verified != identity["checkpoint_identity"]: + raise ValueError("Actual updated weight files differ from the pinned export identity") + training = verified["training_identity"] + fields = ( + "source_commit", + "source_tree", + "environment_sha256", + "provider_config", + "topology", + "checkpoint", + "checkpoint_identity", + ) + if any(training.get(key) != certified.get(key) for key in fields): + raise ValueError( + "Updated export training provenance differs from the certified base profile" + ) + + +def activate_for_identity(identity): + global _CERTIFIED + certificate = identity.get("scheduler_profile_certificate") + mode = os.environ.get("VLLM_BATCH_INVARIANT") + if certificate is None: + if mode != "0" or _CERTIFIED is not None: + raise ValueError("Uncertified shared providers require explicit qualification BI=0") + return "qualification" + if mode != "1": + raise ValueError("A certified shared profile must execute strict BI=1") + sys.path.insert(0, str(ROOT / "scripts")) + from qwen3_next_contract import validate_result + from qwen3_next_l2 import model_identity + from qwen3_next_runtime import file_digest, identity_digest, verify_environment + + record_key = identity_digest({"certificate": certificate, "identity": identity}) + if _CERTIFIED == record_key: + return "certified" + if _CERTIFIED is not None: + raise ValueError("A process cannot switch its certified shared provider identity") + path = Path(certificate["path"]) + if ( + not path.is_absolute() + or path.name != "result.json" + or file_digest(path) != certificate["sha256"] + ): + raise ValueError("Shared profile certificate path/content identity mismatch") + if os.environ.get("QWEN_SHARED_PROFILE_RESULT", str(path)) != str(path): + raise ValueError("Scheduler certificate environment disagrees with the worker request") + manifest = json.loads((path.parent / "run.json").read_text()) + result = json.loads(path.read_text()) + if manifest.get("stage") != "layers": + raise ValueError("Only completed native layer/scheduler evidence can certify CUSTOM") + certified = model_identity(manifest) + fields = ( + "source_commit", + "source_tree", + "environment_sha256", + "provider_config", + "topology", + "architecture_layers", + "weights_dtype", + "recurrent_state_dtype", + ) + if any(identity.get(key) != certified.get(key) for key in fields): + raise ValueError( + "Scheduler certificate belongs to another source/environment/provider profile" + ) + validate_certified_weights(identity, certified) + provider = identity["provider_config"] + if provider.get("profile") != "qwen3_next_tp4_shared_eager_v1": + raise ValueError("Only the explicit Qwen3-Next TP4 eager profile can be certified") + if ( + subprocess.check_output(["git", "rev-parse", "HEAD^{tree}"], cwd=ROOT, text=True).strip() + != identity["source_tree"] + ): + raise ValueError("Current source tree differs from the certified provider") + if subprocess.run(["git", "diff", "--quiet", "HEAD"], cwd=ROOT).returncode: + raise ValueError("Current provider source has uncommitted changes") + for name, expected in provider["sources"].items(): + if file_digest(ROOT / name) != expected: + raise ValueError(f"Certified provider source changed: {name}") + for name, expected in provider["runtime_env"].items(): + if os.environ.get(name) != expected: + raise ValueError(f"Certified runtime setting differs: {name}") + cuda = manifest["cuda_identity"] + if ( + os.environ.get("CUDA_HOME") != cuda["home"] + or file_digest(Path(cuda["home"]) / "bin/nvcc") != cuda["nvcc_sha256"] + ): + raise ValueError("CUDA toolkit differs from the certified environment") + verify_environment(manifest["environment"], require_venv=True) + validate_result(manifest, result, run_dir=path.parent) + _CERTIFIED = record_key + return "certified" + + +def activate_profile_from_environment(identity): + """Compatibility spelling; a path without its pinned request digest is insufficient.""" + return activate_for_identity(identity) diff --git a/rl_engine/integrations/qwen3_next_subnode.py b/rl_engine/integrations/qwen3_next_subnode.py new file mode 100644 index 000000000..cc9fc79cd --- /dev/null +++ b/rl_engine/integrations/qwen3_next_subnode.py @@ -0,0 +1,109 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Sub-node observations inside each Qwen3-Next decoder layer, for first-drift localisation. + +Both the rollout model (vLLM) and the replay model (VIME) call a module's +``subnode_hook(name, packed_rows)`` at the same five points of every decoder +layer, in this order: + +* ``input_layernorm`` - the normed mixer input; +* ``mixer`` - the GDN or full-attention output after the TP reduction; +* ``post_attention_layernorm`` - the normed MLP input; +* ``router`` - the ten selected expert ids per token; +* ``mlp`` - the MoE output after the TP reduction. + +The embedding output is observed once per model as ``embedding`` (on the +embedding module itself) so a drift there is not attributed to layer 0's norm. + +A trace owner binds the layer index, maps packed token rows to the 16 +response positions of each sample, and stores them in :class:`SubnodeBuffer`. +Observing copies values and never changes one. +""" + +from functools import partial + +import torch + +SUBNODES = ("input_layernorm", "mixer", "post_attention_layernorm", "router", "mlp") +EMBEDDING = "embedding" +LAYERS, RESPONSE_LENGTH, HIDDEN, TOP_K = 48, 16, 2048, 10 + + +def subnode_layout(name): + if name == "router": + return TOP_K, torch.int64 + if name in SUBNODES or name == EMBEDDING: + return HIDDEN, torch.bfloat16 + raise ValueError(f"Unknown Qwen3-Next sub-node: {name}") + + +class SubnodeBuffer: + """[48, B, 16, width] host copies of every sub-node, each written exactly once.""" + + def __init__(self, batch): + self.batch = batch + self.values, self.written = {}, {} + for name in SUBNODES: + width, dtype = subnode_layout(name) + self.values[name] = torch.zeros(LAYERS, batch, RESPONSE_LENGTH, width, dtype=dtype) + self.written[name] = torch.zeros(LAYERS, batch, RESPONSE_LENGTH, dtype=torch.bool) + self.embedding = torch.zeros(batch, RESPONSE_LENGTH, HIDDEN, dtype=torch.bfloat16) + self.embedding_written = torch.zeros(batch, RESPONSE_LENGTH, dtype=torch.bool) + + def put_embedding(self, row, response, value): + if value.dtype != torch.bfloat16 or value.shape != (HIDDEN,): + raise ValueError("The embedding must keep its original BF16 [2048] storage") + if self.embedding_written[row, response]: + raise ValueError("The embedding of one response position was observed twice") + self.embedding[row, response].copy_(value.detach().cpu()) + self.embedding_written[row, response] = True + + def embedding_rows(self): + if not bool(self.embedding_written.all()): + raise ValueError("The embedding was not observed at every response position") + return self.embedding + + def put(self, layer, name, row, response, value): + width, dtype = subnode_layout(name) + if value.dtype != dtype or value.shape != (width,): + raise ValueError(f"Sub-node {name} must keep its original {dtype} [{width}] storage") + if self.written[name][layer, row, response]: + raise ValueError(f"Sub-node {name} of layer {layer} was observed twice") + self.values[name][layer, row, response].copy_(value.detach().cpu()) + self.written[name][layer, row, response] = True + + def put_rows(self, layer, name, values): + """Store a complete [B, 16, width] block, as the replay model produces it.""" + for row in range(self.batch): + for response in range(RESPONSE_LENGTH): + self.put(layer, name, row, response, values[row, response]) + + def layer(self, layer): + missing = [name for name in SUBNODES if not bool(self.written[name][layer].all())] + if missing: + raise ValueError(f"Layer {layer} sub-nodes were not all observed: {missing}") + return {name: self.values[name][layer] for name in SUBNODES} + + +def install(layers, modules_of, record): + """Bind ``record(layer_index, name, packed_rows)`` to every hook-bearing module.""" + for index, layer in enumerate(layers): + for module in modules_of(layer): + module.subnode_hook = partial(record, index) + + +def remove(layers, modules_of): + for layer in layers: + for module in modules_of(layer): + module.subnode_hook = None + + +def vllm_modules(layer): + """vLLM ``SharedVLLMDecoderLayer``: the layer itself and its MoE.""" + return (layer, layer.mlp) + + +def vime_modules(layer): + """MCore layer with the shared VIME providers: mixer, pre-MLP norm and MoE.""" + return (layer.self_attention, layer.pre_mlp_layernorm, layer.mlp) diff --git a/rl_engine/integrations/qwen3_next_tp_blocks.py b/rl_engine/integrations/qwen3_next_tp_blocks.py index 7a45c1ad2..ccb04d978 100644 --- a/rl_engine/integrations/qwen3_next_tp_blocks.py +++ b/rl_engine/integrations/qwen3_next_tp_blocks.py @@ -304,6 +304,9 @@ def assemble_moe_weight(name, values): class TP4MoE(nn.Module): """All 512 experts at EP1 with intermediate width 128 per TP rank.""" + # Set by a trace owner (see qwen3_next_subnode); receives the route indices. + subnode_hook = None + def __init__(self, *, group, device): super().__init__() self.group = _tp_group(group) @@ -355,12 +358,14 @@ def forward(self, hidden): if hidden.ndim != 2 or hidden.shape[1] != 2048 or hidden.dtype != torch.bfloat16: raise ValueError("MoE hidden must be BF16 [tokens, 2048]") copied = _CopyToTP.apply(hidden, self.group) - routed, _ = shared_moe( + routed, routes = shared_moe( copied, _CopyToTP.apply(self.gate.weight, self.group), self.experts.gate_up, self.experts.down, ) + if self.subnode_hook is not None: + self.subnode_hook("router", routes.indices) gate = shared_linear(copied, self.shared_expert.gate_proj.weight) up = shared_linear(copied, self.shared_expert.up_proj.weight) activated = (F.silu(gate.float()) * up.float()).to(hidden.dtype) diff --git a/rl_engine/integrations/qwen3_next_training.py b/rl_engine/integrations/qwen3_next_training.py new file mode 100644 index 000000000..753242ad8 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_training.py @@ -0,0 +1,285 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Observe native VIME updates before its train_one_step clears gradients. + +This module does not implement an optimizer or loss. The native training entry +points remain responsible for both; this observer rejects missing evidence. +""" + +import hashlib +import math +import random +import struct +from contextlib import contextmanager + +import numpy as np +import torch + +from rl_engine.testing.qwen3_next_rl_fixture import require_policy_configuration + + +def tensor_digest(value, *, chunk_bytes=64 * 1024 * 1024): + """Hash original tensor bytes with bounded host memory, including dtype/shape.""" + if value.layout != torch.strided or value.device.type == "meta": + raise ValueError("Only materialized strided tensors have a state identity") + digest = hashlib.sha256() + digest.update(str((str(value.dtype), tuple(value.shape))).encode()) + flat = value.detach().contiguous().reshape(-1) + chunk_size = max(1, chunk_bytes // value.element_size()) + for start in range(0, flat.numel(), chunk_size): + part = flat[start : start + chunk_size].cpu() + if (part.is_floating_point() or part.is_complex()) and not torch.isfinite(part).all(): + raise ValueError("Nonfinite tensor in training state") + digest.update(part.view(torch.uint8).numpy().tobytes()) + return digest.hexdigest() + + +def state_digest(state): + """Canonical recursive identity for optimizer/scheduler/RNG state, without pickle.""" + digest = hashlib.sha256() + + def add(raw): + digest.update(struct.pack("!Q", len(raw))) + digest.update(raw) + + def visit(value): + add(type(value).__name__.encode()) + if isinstance(value, torch.Tensor): + add(tensor_digest(value).encode()) + elif isinstance(value, np.ndarray): + add(str((value.dtype.str, value.shape)).encode()) + if value.dtype.hasobject: + raise TypeError("Object arrays cannot identify recovery state") + add(value.tobytes(order="C")) + elif isinstance(value, dict): + if any(type(key) not in (str, int) for key in value): + raise TypeError("Recovery state keys must be strings or integers") + for key in sorted(value, key=lambda key: (type(key).__name__, str(key))): + visit(key) + visit(value[key]) + elif isinstance(value, (list, tuple)): + for item in value: + visit(item) + elif value is None or type(value) in (bool, int, str, bytes): + add(repr(value).encode()) + elif type(value) is float: + if not math.isfinite(value): + raise ValueError("Nonfinite scalar in training state") + add(struct.pack("!d", value)) + else: + raise TypeError(f"Unsupported recovery state type: {type(value)!r}") + + visit(state) + return digest.hexdigest() + + +def capture_rng_state(*, tracker=None): + return { + "python": random.getstate(), + "numpy": np.random.get_state(), + "torch_cpu": torch.get_rng_state().clone(), + "torch_cuda": [value.clone() for value in torch.cuda.get_rng_state_all()], + "megatron": ( + None + if tracker is None + else {name: value.clone() for name, value in tracker.get_states().items()} + ), + } + + +def restore_rng_state(state, *, tracker=None): + if len(state["torch_cuda"]) != torch.cuda.device_count(): + raise ValueError("RNG restore requires the original visible GPU topology") + if (state["megatron"] is None) != (tracker is None): + raise ValueError("Megatron RNG tracker must be restored explicitly") + random.setstate(state["python"]) + np.random.set_state(state["numpy"]) + torch.set_rng_state(state["torch_cpu"]) + torch.cuda.set_rng_state_all(state["torch_cuda"]) + if tracker is not None: + tracker.set_states({name: value.clone() for name, value in state["megatron"].items()}) + + +def named_trainable_parameters(model): + result = {} + seen = set() + for index, chunk in enumerate(model): + for name, parameter in chunk.named_parameters(): + if parameter.requires_grad and id(parameter) not in seen: + result[f"chunk{index}.{name}"] = parameter + seen.add(id(parameter)) + if not result: + raise ValueError("Native training model has no trainable parameters") + return result + + +def _any_rank_nonzero(flags): + """Combine per-rank nonzero flags across the tensor-parallel ranks (max).""" + import torch.distributed as dist + + if not dist.is_available() or not dist.is_initialized() or dist.get_world_size() == 1: + return flags + reduced = torch.tensor(flags, dtype=torch.int32, device=torch.cuda.current_device()) + dist.all_reduce(reduced, op=dist.ReduceOp.MAX) + return [bool(value) for value in reduced.tolist()] + + +def gradient_evidence(parameters, *, required_groups, reduce_flags=_any_rank_nonzero): + """Unselected experts may be zero; every declared active path must be nonzero. + + "Nonzero" is judged across the tensor-parallel group, not per rank: a + vocab-parallel shard such as the embedding of a rank whose vocabulary slice + contains none of the batch's tokens legitimately receives an all-zero + gradient while the full parameter does train. Every rank evaluates the same + parameter list in the same order, so the flag exchange is a fixed-order + collective that all ranks enter together. The per-rank value is kept as + ``nonzero_local``. + """ + if not required_groups or any(not prefixes for prefixes in required_groups.values()): + raise ValueError("Predeclare the active parameter groups before training") + matched = {group: [] for group in required_groups} + evidence = {} + names = [] + local_flags = [] + for name, parameter in parameters.items(): + gradient = getattr(parameter, "main_grad", None) + if gradient is None: + gradient = parameter.grad + if gradient is None: + evidence[name] = {"present": False, "nonzero": False, "nonzero_local": False} + continue + if gradient.shape != parameter.shape or not torch.isfinite(gradient).all(): + raise ValueError(f"Invalid/nonfinite gradient for {name}") + nonzero_local = bool(torch.count_nonzero(gradient)) + evidence[name] = { + "present": True, + "nonzero": nonzero_local, + "nonzero_local": nonzero_local, + "dtype": str(gradient.dtype), + } + names.append(name) + local_flags.append(nonzero_local) + group_flags = list(reduce_flags(local_flags)) if names else [] + if len(group_flags) != len(names): + raise ValueError("Nonzero-gradient flags did not come back one per parameter") + for name, nonzero in zip(names, group_flags, strict=True): + evidence[name]["nonzero"] = bool(nonzero) + for group, prefixes in required_groups.items(): + if nonzero and any(prefix in name for prefix in prefixes): + matched[group].append(name) + missing = [group for group, names_ in matched.items() if not names_] + if missing: + raise ValueError(f"Active paths have no finite nonzero gradient: {missing}") + return {"parameters": evidence, "active_groups": matched} + + +@contextmanager +def observe_native_optimizer_step(optimizer, model, *, required_groups, evidence): + """Wrap only step(), preserving the native loss, backward, clipping and update.""" + original = optimizer.step + had_own = "step" in vars(optimizer) + previous = vars(optimizer).get("step") + parameters = named_trainable_parameters(model) + calls = 0 + + def observed(*args, **kwargs): + nonlocal calls + calls += 1 + if calls != 1: + raise ValueError("One train_one_step call must perform exactly one optimizer step") + evidence["gradients"] = gradient_evidence(parameters, required_groups=required_groups) + before = {name: tensor_digest(value) for name, value in parameters.items()} + result = original(*args, **kwargs) + if not isinstance(result, tuple) or len(result) != 3 or not bool(result[0]): + raise ValueError("Native Megatron optimizer did not report a successful update") + norm = float(result[1]) + if not math.isfinite(norm) or norm <= 0: + raise ValueError("Native optimizer must report a finite nonzero gradient norm") + after = {name: tensor_digest(value) for name, value in parameters.items()} + changed = [name for name in before if before[name] != after[name]] + if not changed: + raise ValueError("Optimizer reported success without changing any model parameter") + evidence.update( + { + "grad_norm": norm, + "parameter_hashes_before": before, + "parameter_hashes_after": after, + "changed_parameters": changed, + } + ) + return result + + optimizer.step = observed + try: + yield + if calls != 1: + raise ValueError("Native training did not call the optimizer exactly once") + finally: + if had_own: + optimizer.step = previous + else: + del optimizer.step + + +def native_policy_update(args, model, optimizer, scheduler, fixture, *, step, required_groups): + """Execute the actual installed VIME train_one_step with its clipped policy loss.""" + require_policy_configuration(args) + fixture.validate() + if step not in (1, 2, 3): + raise ValueError("The fixed acceptance sequence uses updates 1, 2 and recovery control 3") + from megatron.core.distributed import finalize_model_grads + from megatron.core.utils import get_model_config + from vime.backends.megatron_utils import loss as native_loss + from vime.backends.megatron_utils.data import DataIterator + from vime.backends.megatron_utils.model import train_one_step + + if train_one_step.__module__ != "vime.backends.megatron_utils.model": + raise ValueError("Native VIME training entry point was replaced") + if native_loss.policy_loss_function.__module__ != "vime.backends.megatron_utils.loss": + raise ValueError("Native VIME policy loss was replaced") + for chunk in model: + chunk.train() + config = get_model_config(chunk) + if config.hidden_dropout != 0 or config.attention_dropout != 0: + raise ValueError("The fixed eager profile requires zero dropout") + config.grad_scale_func = optimizer.scale_loss + config.finalize_model_grads_func = finalize_model_grads + config.timers = None + batch = fixture.batch([0, 1, 2, 3], device=torch.cuda.current_device()) + batch["tokens"] = batch.pop("unconcat_tokens") + iterator = DataIterator(batch, [[0], [1], [2], [3]]) + original_policy_loss = native_loss.policy_loss_function + policy_calls = 0 + + def observed_loss(*loss_args, **loss_kwargs): + nonlocal policy_calls + policy_calls += 1 + return original_policy_loss(*loss_args, **loss_kwargs) + + evidence = {"step": step, "fixture": fixture.content_sha256, "sample_position": step * 4} + native_loss.policy_loss_function = observed_loss + try: + with observe_native_optimizer_step( + optimizer, model, required_groups=required_groups, evidence=evidence + ): + metrics, norm = train_one_step( + args, + rollout_id=0, + step_id=step - 1, + data_iterator=[iterator], + model=model, + optimizer=optimizer, + opt_param_scheduler=scheduler, + num_microbatches=4, + step_global_batch_size=4, + ) + finally: + native_loss.policy_loss_function = original_policy_loss + if policy_calls != 4 or iterator.offset != 4: + raise ValueError("Native policy loss must consume exactly the four fixed samples") + if not math.isfinite(float(norm)): + raise ValueError("Native training returned a nonfinite gradient norm") + evidence.update({"policy_loss_calls": policy_calls, "metrics": metrics}) + fixture.validate() + return evidence diff --git a/rl_engine/integrations/qwen3_next_vime.py b/rl_engine/integrations/qwen3_next_vime.py new file mode 100644 index 000000000..ff789dc8d --- /dev/null +++ b/rl_engine/integrations/qwen3_next_vime.py @@ -0,0 +1,402 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""VIME ModuleSpec and HF weight hooks for the explicit shared TP4 GDN provider. + + --spec rl_engine.integrations.qwen3_next_vime get_qwen3_next_shared_spec + +Installing this spec installs Qwen3-Next-only weight conversion hooks in this +training process. Other model families keep their existing conversion paths. +The decoder spec covers both attention families, routed/shared experts, and +decoder norms. Call install_vime_model_providers after constructing the GPT +model to replace its embedding, final norm and output layer as well. +""" + +import re + +import torch +from megatron.core import mpu +from megatron.core.tensor_parallel.layers import ( + set_defaults_if_not_set_tensor_model_parallel_attributes, +) +from megatron.core.transformer.identity_op import IdentityOp +from megatron.core.transformer.spec_utils import ModuleSpec +from torch import nn +from vime_plugins.models.hf_attention import HuggingfaceAttention + +from rl_engine.integrations.qwen3_next_engine_weights import ( + conv_hf_to_rank_major, + conv_rank_major_to_hf, + expert_hf_to_rank_major, + expert_rank_major_to_hf, + kv_hf_to_rank_major, + kv_rank_major_to_hf, + set_gdn_tp_metadata, + validate_training_topology, +) +from rl_engine.integrations.qwen3_next_provider import GDNProviderConfig +from rl_engine.integrations.qwen3_next_tp import _CopyToTP +from rl_engine.integrations.qwen3_next_tp_blocks import TP4FullAttention, TP4MoE +from rl_engine.integrations.qwen3_next_tp_gdn import TP4GDN + + +class SharedInputNorm(nn.Module): + def __init__(self, eps, device, weight=None): + super().__init__() + self.eps = eps + if weight is not None and not isinstance(weight, nn.Parameter): + raise ValueError("Shared decoder norm weight must be an existing Parameter") + if weight is not None and (weight.shape != (2048,) or weight.dtype != torch.bfloat16): + raise ValueError("Shared decoder norm requires a BF16 [2048] weight") + self.weight = ( + weight + if weight is not None + else nn.Parameter(torch.zeros(2048, dtype=torch.bfloat16, device=device)) + ) + self.weight.tensor_model_parallel = False + self.weight.partition_dim = -1 + self.weight.partition_stride = 1 + self.weight.sequence_parallel = False + + def forward(self, value): + from rl_engine.kernels.ops.cuda.norm.rmsnorm import Qwen3NextRMSNormCudaOp + + return Qwen3NextRMSNormCudaOp()(value, self.weight, eps=self.eps) + + +class SharedVIMELMHead(nn.Module): + """Megatron output-layer API with shared GEMM and the original weight name. + + A forward hook on this module observes BF16 logits before Float16Module + widens the model output for VIME's policy loss. + """ + + def __init__(self, weight, group): + super().__init__() + if not isinstance(weight, nn.Parameter): + raise ValueError("Shared TP4 LM head weight must be an existing Parameter") + if weight.shape != (37984, 2048) or weight.dtype != torch.bfloat16: + raise ValueError("Shared TP4 LM head requires BF16 [37984, 2048] weight") + if torch.distributed.get_world_size(group) != 4: + raise ValueError("Shared LM head requires TP4") + self.weight = weight + self.weight.tensor_model_parallel = True + self.weight.partition_dim = 0 + self.weight.partition_stride = 1 + self.group = group + self.gather_output = False + self.sequence_parallel = False + + def forward(self, input_, weight=None, runtime_gather_output=None): + from megatron.core.tensor_parallel.mappings import gather_from_tensor_model_parallel_region + + from rl_engine.integrations.qwen3_next_forward import shared_linear + + if self.sequence_parallel: + raise ValueError("Shared LM head requires sequence parallelism disabled") + selected = self.weight if weight is None else weight + if selected.shape != self.weight.shape or selected.dtype != torch.bfloat16: + raise ValueError("Shared LM head received an incompatible tied weight") + value = shared_linear(_CopyToTP.apply(input_, self.group), selected) + gather = self.gather_output if runtime_gather_output is None else runtime_gather_output + if gather: + value = gather_from_tensor_model_parallel_region(value, group=self.group) + return value, None + + +class SharedVIMEGDN(HuggingfaceAttention): + def __init__( + self, args, config, layer_number, cp_comm_type="p2p", pg_collection=None, name=None + ): + validate_training_topology(config, args) + super().__init__(args, config, layer_number, cp_comm_type, pg_collection) + if self.hf_config.hidden_size != 2048: + raise ValueError("Shared VIME GDN requires the pinned Qwen3-Next architecture") + group = ( + pg_collection.tp if pg_collection is not None else mpu.get_tensor_model_parallel_group() + ) + device = torch.device("cuda", torch.cuda.current_device()) + self.linear_attn = TP4GDN( + group=group, device=device, config=GDNProviderConfig(), eps=self.hf_config.rms_norm_eps + ) + set_gdn_tp_metadata(self.linear_attn) + self.input_layernorm = SharedInputNorm(self.hf_config.rms_norm_eps, device) + self.trace_state_hook = None + self.subnode_hook = None + + def hf_forward(self, hidden_states, packed_seq_params): + if hidden_states.ndim != 3 or hidden_states.shape[0] != 1: + raise ValueError("VIME packed GDN expects [1, tokens, 2048]") + if packed_seq_params is None: + raise ValueError("VIME GDN requires packed sequence boundaries") + cu = packed_seq_params.cu_seqlens_q.to(device="cpu") + hidden = self.input_layernorm(hidden_states[0]) + if self.subnode_hook is not None: + self.subnode_hook("input_layernorm", hidden) + indices = torch.arange(1, cu.numel(), device=hidden.device, dtype=torch.int32) + initial = self.linear_attn.initial_state(cu.numel()) + output, state = self.linear_attn(hidden, initial, indices, cu) + if self.trace_state_hook is not None: + self.trace_state_hook(state, indices, cu) + if self.subnode_hook is not None: + self.subnode_hook("mixer", output) + return output.unsqueeze(0) + + +class SharedVIMEFullAttention(HuggingfaceAttention): + def __init__( + self, args, config, layer_number, cp_comm_type="p2p", pg_collection=None, name=None + ): + validate_training_topology(config, args) + super().__init__(args, config, layer_number, cp_comm_type, pg_collection) + group = pg_collection.tp if pg_collection else mpu.get_tensor_model_parallel_group() + device = torch.device("cuda", torch.cuda.current_device()) + rope = getattr(self.hf_config, "rope_parameters", None) + if rope is None: + rope = {"rope_theta": self.hf_config.rope_theta, "partial_rotary_factor": 0.25} + if any( + ( + rope.get("partial_rotary_factor") != 0.25, + rope.get("rope_type", "default") != "default", + ) + ): + raise ValueError("Shared full attention requires default RoPE with rotary dimension 64") + self.self_attn = TP4FullAttention( + group=group, + device=device, + eps=self.hf_config.rms_norm_eps, + rope_theta=rope["rope_theta"], + ) + self.input_layernorm = SharedInputNorm(self.hf_config.rms_norm_eps, device) + self.trace_state_hook = None + self.subnode_hook = None + for parameter in self.parameters(): + set_defaults_if_not_set_tensor_model_parallel_attributes(parameter) + parameter.sequence_parallel = False + + def hf_forward(self, hidden_states, packed_seq_params): + if hidden_states.ndim != 3 or hidden_states.shape[0] != 1 or packed_seq_params is None: + raise ValueError("Shared VIME attention requires packed [1, tokens, 2048]") + hidden = self.input_layernorm(hidden_states[0]) + if self.subnode_hook is not None: + self.subnode_hook("input_layernorm", hidden) + cu = packed_seq_params.cu_seqlens_q.to(device="cpu") + indices = torch.arange(1, cu.numel(), device=hidden.device, dtype=torch.int32) + output, state = self.self_attn( + hidden, self.self_attn.initial_state(cu.numel()), indices, cu + ) + if self.trace_state_hook is not None: + self.trace_state_hook(state, indices, cu) + if self.subnode_hook is not None: + self.subnode_hook("mixer", output) + return output.unsqueeze(0) + + +class SharedVIMEMoE(TP4MoE): + def __init__(self, config, submodules=None): + validate_training_topology(config, config) + if config.expert_tensor_parallel_size != 4: + raise ValueError("Shared MoE requires expert tensor parallel size 4") + super().__init__( + group=mpu.get_tensor_model_parallel_group(), + device=torch.device("cuda", torch.cuda.current_device()), + ) + # Native VIME only recognizes stride-two FC1 names; our paired loader + # already packs complete per-rank gate/up blocks before the generic split. + self.experts.gate_up.partition_stride = 1 + for parameter in self.parameters(): + set_defaults_if_not_set_tensor_model_parallel_attributes(parameter) + parameter.sequence_parallel = False + + def forward(self, hidden_states, padding_mask=None): + if padding_mask is not None and bool(padding_mask.any()): + raise ValueError("Shared VIME MoE requires packed tokens without padding") + value = super().forward(hidden_states.reshape(-1, 2048)) + if self.subnode_hook is not None: + self.subnode_hook("mlp", value) + return value.reshape_as(hidden_states), None + + +class SharedDecoderNorm(SharedInputNorm): + def __init__(self, config, hidden_size, eps): + if hidden_size != 2048 or config.params_dtype != torch.bfloat16: + raise ValueError("Shared decoder norm requires the official BF16 architecture") + super().__init__(eps, torch.device("cuda", torch.cuda.current_device())) + # Set by a trace owner: this instance is a layer's pre-MLP norm. + self.subnode_hook = None + + def forward(self, value): + output = super().forward(value) + if self.subnode_hook is not None: + self.subnode_hook("post_attention_layernorm", output.reshape(-1, output.shape[-1])) + return output + + +class SharedVIMEEmbedding(nn.Module): + def __init__(self, weight, group): + super().__init__() + if weight.shape != (37984, 2048) or weight.dtype != torch.bfloat16: + raise ValueError("Shared embedding requires the official BF16 TP4 weight") + self.weight = weight + self.group = group + self.subnode_hook = None + + def forward(self, tokens): + from rl_engine.integrations.qwen3_next_embedding import shared_embedding + + output = shared_embedding(tokens, self.weight, self.group) + if self.subnode_hook is not None: + self.subnode_hook("embedding", output) + return output + + +def install_vime_model_providers(model): + """Replace outer GPT providers while retaining each original parameter object.""" + group = mpu.get_tensor_model_parallel_group() + validate_training_topology(model.config, model.config) + if len(model.decoder.layers) != 48: + raise ValueError("The complete provider requires all 48 decoder layers") + for layer in model.decoder.layers: + if not isinstance(layer.self_attention, (SharedVIMEGDN, SharedVIMEFullAttention)): + raise ValueError("Construct the model with get_qwen3_next_shared_spec first") + if not isinstance(layer.mlp, SharedVIMEMoE): + raise ValueError("Shared decoder MoE provider is missing") + model.embedding.word_embeddings = SharedVIMEEmbedding( + model.embedding.word_embeddings.weight, group + ) + previous = model.decoder.final_layernorm + model.decoder.final_layernorm = SharedInputNorm( + model.config.layernorm_epsilon, previous.weight.device, weight=previous.weight + ) + model.output_layer = SharedVIMELMHead(model.output_layer.weight, group) + return model + + +def install_vime_gdn_weight_hooks(): + """Install exact shared-model layouts in VIME's real load/export entrypoints.""" + import vime.backends.megatron_utils.hf_to_megatron as load_api + import vime.backends.megatron_utils.megatron_to_hf as export_api + from vime.backends.megatron_utils.hf_to_megatron.common import strip_mcore_wrappers + + original_load = load_api._LOADERS["qwen3_next"] + if getattr(original_load, "_rl_kernel_tp4_gdn", False): + return + original_export = export_api.convert_qwen3_next_to_hf + + def layer_parts(name): + match = re.fullmatch(r"decoder\.layers\.(\d+)\.(.+)", strip_mcore_wrappers(name)) + return match.groups() if match else (None, None) + + def direct_moe(rest): + return rest in { + "mlp.gate.weight", + "mlp.shared_expert_gate.weight", + "mlp.shared_expert.gate_proj.weight", + "mlp.shared_expert.up_proj.weight", + "mlp.shared_expert.down_proj.weight", + } + + def load(name, reader, config): + layer, rest = layer_parts(name) + prefix = f"model.layers.{layer}" + if rest == "mlp.experts.gate_up": + value = torch.empty((512, 1024, 2048), dtype=torch.bfloat16) + for expert in range(512): + gate, up = ( + reader.get_tensor(f"{prefix}.mlp.experts.{expert}.{part}_proj.weight") + for part in ("gate", "up") + ) + value[expert].copy_(expert_hf_to_rank_major(gate, up)) + return value + if rest == "mlp.experts.down": + value = torch.empty((512, 2048, 512), dtype=torch.bfloat16) + for expert in range(512): + weight = reader.get_tensor(f"{prefix}.mlp.experts.{expert}.down_proj.weight") + if weight.shape != (2048, 512) or weight.dtype != torch.bfloat16: + raise ValueError("Official expert down projection requires BF16 [2048, 512]") + value[expert].copy_(weight) + return value + if direct_moe(rest): + return reader.get_tensor(f"{prefix}.{rest}") + value = original_load(name, reader, config) + if name.endswith("self_attention.linear_attn.conv1d.weight"): + if mpu.get_tensor_model_parallel_world_size() != 4: + raise ValueError("The shared GDN weight loader requires TP4") + value = conv_hf_to_rank_major(value) + if rest in ( + "self_attention.self_attn.k_proj.weight", + "self_attention.self_attn.v_proj.weight", + ): + value = kv_hf_to_rank_major(value) + return value + + def export(args, name, value): + layer, rest = layer_parts(name) + prefix = f"model.layers.{layer}" + if rest == "mlp.experts.gate_up": + if value.shape != (512, 1024, 2048): + raise ValueError("Gathered routed gate/up shape is not the complete TP4 tensor") + return [ + (f"{prefix}.mlp.experts.{expert}.{part}_proj.weight", weight) + for expert in range(512) + for part, weight in zip(("gate", "up"), expert_rank_major_to_hf(value[expert])) + ] + if rest == "mlp.experts.down": + if value.shape != (512, 2048, 512) or value.dtype != torch.bfloat16: + raise ValueError("Gathered routed down shape is not the complete TP4 tensor") + return [ + (f"{prefix}.mlp.experts.{expert}.down_proj.weight", value[expert]) + for expert in range(512) + ] + if direct_moe(rest): + return [(f"{prefix}.{rest}", value)] + if name.endswith("self_attention.linear_attn.conv1d.weight"): + if mpu.get_tensor_model_parallel_world_size() != 4: + raise ValueError("The shared GDN weight exporter requires TP4") + value = conv_rank_major_to_hf(value) + if rest in ( + "self_attention.self_attn.k_proj.weight", + "self_attention.self_attn.v_proj.weight", + ): + value = kv_rank_major_to_hf(value) + return original_export(args, name, value) + + load._rl_kernel_tp4_gdn = True + load_api._LOADERS["qwen3_next"] = load + export_api.convert_qwen3_next_to_hf = export + + +def get_qwen3_next_shared_spec(args, config, vp_stage=None): + """Use the explicit provider for every decoder normalization and sublayer.""" + from vime_plugins.models.qwen3_next import Attention, get_qwen3_next_spec + + validate_training_topology(config, args) + if config.expert_tensor_parallel_size != 4: + raise ValueError("Shared model requires expert tensor parallel size 4") + if vp_stage is not None: + raise ValueError("The shared GDN profile does not use virtual pipeline stages") + spec = get_qwen3_next_spec(args, config, vp_stage) + replaced = 0 + for layer in spec.layer_specs: + if layer.submodules.self_attention.module is Attention: + layer.submodules.self_attention = ModuleSpec( + module=SharedVIMEGDN, params={"args": args} + ) + replaced += 1 + else: + layer.submodules.self_attention = ModuleSpec( + module=SharedVIMEFullAttention, params={"args": args} + ) + layer.submodules.input_layernorm = IdentityOp + layer.submodules.pre_mlp_layernorm = SharedDecoderNorm + layer.submodules.mlp = ModuleSpec(module=SharedVIMEMoE) + # The native TE sharded-state map refers to fused norm/projection names. + # Shared adapters retain explicit HF-compatible norm parameter names. + layer.submodules.sharded_state_dict_keys_map = {} + if replaced != 36: + raise ValueError(f"Pinned Qwen3-Next must contain 36 GDN layers, found {replaced}") + if len(spec.layer_specs) != 48: + raise ValueError("Pinned Qwen3-Next must contain all 48 decoder layers") + spec.layer_norm = SharedDecoderNorm + install_vime_gdn_weight_hooks() + return spec diff --git a/rl_engine/integrations/qwen3_next_vime_runtime.py b/rl_engine/integrations/qwen3_next_vime_runtime.py new file mode 100644 index 000000000..36df0bca7 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_vime_runtime.py @@ -0,0 +1,178 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Construct the native VIME/Megatron actor for the fixed Qwen TP4 profile.""" + +import copy +import os +import sys +from pathlib import Path + +from rl_engine.testing.qwen3_next_rl_fixture import ( + OPTIMIZER_SETTINGS, + POLICY_SETTINGS, + require_policy_configuration, +) + + +def model_provider(pre_process=True, post_process=True, vp_stage=None): + """Native GPTModel construction, with providers installed before DDP/optimizer.""" + from megatron.training.global_vars import get_args + from vime.backends.megatron_utils.model_provider import get_model_provider_func + + from rl_engine.integrations.qwen3_next_vime import install_vime_model_providers + + args = copy.copy(get_args()) + args.custom_model_provider_path = None + factory = get_model_provider_func(args, "actor") + model = factory(pre_process=pre_process, post_process=post_process, vp_stage=vp_stage) + install_vime_model_providers(model) + return model + + +def runtime_argv(checkpoint, *, training): + """Use native argument parsing/validation; no precision-policy overrides.""" + checkpoint = str(Path(checkpoint).absolute()) + values = { + "hf-checkpoint": checkpoint, + "load": checkpoint, + "actor-num-nodes": 1, + "actor-num-gpus-per-node": 4, + "num-gpus-per-node": 4, + "tensor-model-parallel-size": 4, + "pipeline-model-parallel-size": 1, + "context-parallel-size": 1, + "expert-model-parallel-size": 1, + "expert-tensor-parallel-size": 4, + "num-attention-heads": 16, + "num-query-groups": 2, + "kv-channels": 256, + "num-layers": 48, + "hidden-size": 2048, + "ffn-hidden-size": 5120, + "normalization": "RMSNorm", + "position-embedding-type": "rope", + "norm-epsilon": 1e-6, + "rotary-percent": 0.25, + "rotary-base": 10000000, + "vocab-size": 151936, + "make-vocab-size-divisible-by": 1, + "padded-vocab-size": 151936, + "moe-ffn-hidden-size": 512, + "moe-shared-expert-intermediate-size": 512, + "moe-router-score-function": "softmax", + "moe-router-dtype": "fp32", + "moe-router-topk": 10, + "num-experts": 512, + "moe-layer-freq": "[" + ",".join(["1"] * 48) + "]", + "moe-aux-loss-coeff": 0, + "moe-token-dispatcher-type": "alltoall", + "hidden-dropout": 0, + "attention-dropout": 0, + "seq-length": 1040, + "max-position-embeddings": 262144, + "global-batch-size": 4, + "micro-batch-size": 1, + "data-pad-size-multiplier": 1, + "num-rollout": 3 if training else 0, + "rollout-batch-size": 4, + "n-samples-per-prompt": 1, + "rollout-num-gpus": 0, + "advantage-estimator": "grpo", + "eps-clip": 0.2, + "eps-clip-high": 0.2, + "kl-coef": 0, + "kl-loss-coef": 0, + "entropy-coef": 0, + "optimizer": "adam", + "lr": 1e-6, + "lr-decay-style": "constant", + "adam-beta1": 0.9, + "adam-beta2": 0.98, + "adam-eps": 1e-8, + "weight-decay": 0.1, + "clip-grad": 1.0, + "optimizer-offload-fraction": 1.0, + "custom-model-provider-path": ( + "rl_engine.integrations.qwen3_next_vime_runtime.model_provider" + ), + } + switches = [ + "debug-train-only", + "disable-bias-linear", + "qk-layernorm", + "group-query-attention", + "use-gated-attention", + "apply-layernorm-1p", + "swiglu", + "untie-embeddings-and-output-weights", + "attention-output-gate", + "moe-shared-expert-gate", + "moe-grouped-gemm", + "use-rollout-logprobs", + "disable-compute-advantages-and-returns", + "optimizer-cpu-offload", + "use-precision-aware-optimizer", + # Megatron's fused weight-gradient accumulation needs APEX's + # fused_weight_gradient_mlp_cuda, which the pinned environment does not + # ship; the unfused path accumulates the same GEMM result into main_grad. + "no-gradient-accumulation-fusion", + ] + argv = ["qwen3-next-native-actor"] + for name, value in values.items(): + argv.extend((f"--{name}", str(value))) + argv.extend(f"--{name}" for name in switches) + argv.extend(("--spec", "rl_engine.integrations.qwen3_next_vime", "get_qwen3_next_shared_spec")) + return argv + + +def create_actor(checkpoint, *, training): + """Call real native setup/load after the caller initializes Torch distributed.""" + import torch + import torch.distributed as dist + from vime.backends.megatron_utils.initialize import init + from vime.utils.arguments import parse_args + + if sys.prefix == sys.base_prefix: + raise RuntimeError("Native VIME actor must actually run inside the isolated venv") + if not dist.is_initialized() or dist.get_world_size() != 4 or torch.cuda.device_count() != 4: + raise ValueError("Native VIME actor requires four initialized ranks and visible GPUs") + saved_argv = sys.argv + try: + sys.argv = runtime_argv(checkpoint, training=training) + args = parse_args() + finally: + sys.argv = saved_argv + precision = {name: getattr(args, name, None) for name in ("optimization_dtype", "reduce_dtype")} + for name, value in {**POLICY_SETTINGS, **OPTIMIZER_SETTINGS}.items(): + setattr(args, name, value) + args.rank, args.local_rank = dist.get_rank(), int(os.environ["LOCAL_RANK"]) + args.world_size = 4 + args.num_rollout = 3 if training else 0 + args.sequence_parallel = False + args.fp32_residual_connection = False + args.overlap_grad_reduce = False + args.overlap_param_gather = False + args.recompute_granularity = None + args.use_critic = False + require_policy_configuration(args) + if any(getattr(args, name, None) != value for name, value in precision.items()): + raise ValueError("Optimizer/reduction precision changed during profile setup") + init(args) + # vime's actor bootstrap creates a gloo group next to the NCCL one; the HF weight + # exporter (HfWeightIteratorDirect) gathers its parameter metadata over it. + # This runtime bypasses that bootstrap, so create the group here on every rank. + from vime.utils.distributed_utils import init_gloo_group + + init_gloo_group() + if (args.vocab_size, args.padded_vocab_size) != (151936, 151936): + raise ValueError("Tokenizer initialization changed the official LM-head vocabulary") + from vime.backends.megatron_utils.checkpoint import load_checkpoint + from vime.backends.megatron_utils.model import setup_model_and_optimizer + + from rl_engine.integrations.qwen3_next_logprob import install_vime_logprob_provider + + model, optimizer, scheduler = setup_model_and_optimizer(args, role="actor") + load_checkpoint(model, optimizer, scheduler, checkpointing_context={}) + install_vime_logprob_provider() + return args, model, optimizer, scheduler diff --git a/rl_engine/integrations/qwen3_next_vllm.py b/rl_engine/integrations/qwen3_next_vllm.py new file mode 100644 index 000000000..e5b03afdb --- /dev/null +++ b/rl_engine/integrations/qwen3_next_vllm.py @@ -0,0 +1,205 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Actual vLLM GDN layer/cache adapter for the shared TP4 provider. + +The CUSTOM backend deliberately does not advertise batch invariance. Registering +this adapter cannot bypass vLLM's strict capability check. Real metadata/step +acceptance must pass before a separately reviewed capability registration. +""" + +import torch +from torch import nn +from vllm.distributed import get_tp_group +from vllm.forward_context import get_forward_context +from vllm.model_executor.layers.mamba.gdn.qwen_gdn_linear_attn import QwenGatedDeltaNetAttention +from vllm.model_executor.layers.mamba.mamba_utils import is_conv_state_dim_first +from vllm.v1.attention.backend import AttentionCGSupport +from vllm.v1.attention.backends.gdn_attn import ( + GDNAttentionBackend, + GDNAttentionMetadata, + GDNAttentionMetadataBuilder, +) +from vllm.v1.attention.backends.registry import MambaAttentionBackendEnum, register_backend + +from rl_engine.integrations.qwen3_next_provider import GDNProviderConfig, GDNState +from rl_engine.integrations.qwen3_next_tp import _ReduceFromTP +from rl_engine.integrations.qwen3_next_tp_gdn import TP4GDN + + +class SharedGDNMetadataBuilder(GDNAttentionMetadataBuilder): + """Use vLLM's scheduler ordering and record resets even for fresh one-token prompts.""" + + _cudagraph_support = AttentionCGSupport.NEVER + + def build(self, common_prefix_len, common_attn_metadata, **kwargs): + if common_prefix_len: + raise ValueError("Shared GDN does not enable prefix reuse") + metadata = super().build(common_prefix_len, common_attn_metadata, **kwargs) + if metadata.num_spec_decodes or metadata.spec_sequence_masks is not None: + raise ValueError("Shared GDN does not enable speculative decoding") + metadata.has_initial_state = common_attn_metadata.compute_num_computed_tokens() > 0 + return metadata + + +class SharedGDNBackend(GDNAttentionBackend): + @staticmethod + def get_name(): + return "RL_KERNEL_QWEN3_NEXT_GDN" + + @staticmethod + def get_builder_cls(): + return SharedGDNMetadataBuilder + + @staticmethod + def supports_batch_invariance(): + from rl_engine.integrations.qwen3_next_profile import profile_is_certified + + return profile_is_certified() + + +def metadata_sequences(metadata, tokens): + """Extract active packed sequences, excluding zero-token padding without touching null state.""" + if not isinstance(metadata, GDNAttentionMetadata): + raise ValueError("Shared GDN requires actual vLLM GDNAttentionMetadata") + if metadata.num_spec_decodes or metadata.spec_sequence_masks is not None: + raise ValueError("Speculative GDN metadata is outside the shared profile") + cu = metadata.non_spec_query_start_loc + indices = metadata.non_spec_state_indices_tensor + if cu is None or indices is None: + raise ValueError("GDN metadata lacks packed sequence boundaries or state slots") + cu = cu.to(device="cpu", dtype=torch.int64) + if cu.ndim != 1 or cu.numel() != indices.numel() + 1: + raise ValueError("GDN metadata boundaries and slot counts disagree") + lengths = cu.diff() + if cu[0] != 0 or cu[-1] != tokens or bool((lengths < 0).any()): + raise ValueError("GDN metadata boundaries must cover exactly the actual tokens") + active = torch.nonzero(lengths > 0, as_tuple=True)[0] + chosen = indices.index_select(0, active.to(indices.device)) + if bool((chosen <= 0).any()) or chosen.unique().numel() != chosen.numel(): + raise ValueError("Active GDN requests require distinct positive state slots") + compact_cu = torch.cat((torch.zeros(1, dtype=torch.int64), lengths[active].cumsum(0))) + has_initial = metadata.has_initial_state + if has_initial is None or has_initial.numel() != indices.numel(): + raise ValueError("Shared GDN metadata must declare initial state for every request") + reset = chosen[ + ~has_initial.to(device=indices.device, dtype=torch.bool)[active.to(indices.device)] + ] + return chosen, compact_cu, reset + + +def _register_backend(): + path = "rl_engine.integrations.qwen3_next_vllm.SharedGDNBackend" + try: + current = MambaAttentionBackendEnum.CUSTOM.get_path() + except ValueError: + current = None + if current is not None and current != path: + raise RuntimeError("A different custom Mamba backend is already registered") + register_backend(MambaAttentionBackendEnum.CUSTOM, path, is_mamba=True) + + +class SharedQwenGDN(QwenGatedDeltaNetAttention): + """Preserve real vLLM HF weight loaders and bind shared forward to its owned cache.""" + + def __init__(self, config, vllm_config, prefix="", **kwargs): + parallel = vllm_config.parallel_config + if parallel.tensor_parallel_size != 4 or parallel.pipeline_parallel_size != 1: + raise ValueError("Shared vLLM GDN requires TP4 and PP1") + if getattr(parallel, "decode_context_parallel_size", 1) != 1: + raise ValueError("Shared vLLM GDN requires CP1") + if getattr(parallel, "prefill_context_parallel_size", 1) != 1: + raise ValueError("Shared vLLM GDN requires prefill CP1") + if vllm_config.model_config.dtype != torch.bfloat16: + raise ValueError("Shared vLLM GDN requires BF16 weights") + if vllm_config.quant_config is not None or vllm_config.speculative_config is not None: + raise ValueError("Shared GDN requires unquantized weights and no speculation") + if vllm_config.cache_config.enable_prefix_caching: + raise ValueError("Shared GDN requires prefix reuse disabled") + if vllm_config.cache_config.mamba_cache_mode != "none": + raise ValueError("Shared GDN requires the non-reusing mamba cache mode") + if kwargs.get("reduce_results", True) is not True: + raise ValueError("Shared GDN performs its own TP output reduction") + if not vllm_config.model_config.enforce_eager: + raise ValueError("Shared GDN currently requires eager execution") + if not kwargs.get("gqa_interleaved_layout", False) or config.hidden_size != 2048: + raise ValueError("Shared GDN requires Qwen3-Next interleaved TP4 projections") + _register_backend() + super().__init__(config, vllm_config, prefix, **kwargs) + if self.get_state_dtype() != (torch.bfloat16, torch.float32): + raise ValueError("Shared GDN requires BF16 convolution and FP32 recurrent state") + # HF stores A_log and dt_bias in BF16; upstream widens A_log at allocation. + # Keep the checkpoint weight identity, widening only at the recurrence call. + for name in ("A_log", "dt_bias"): + original = getattr(self, name) + parameter = nn.Parameter(original.to(dtype=torch.bfloat16), requires_grad=False) + parameter.weight_loader = original.weight_loader + setattr(self, name, parameter) + self.group = get_tp_group().device_group + self.provider_config = GDNProviderConfig() + self.eps = config.rms_norm_eps + + @property + def mamba_type(self): + return MambaAttentionBackendEnum.CUSTOM + + @torch.no_grad() + def forward(self, hidden_states): + """Match the installed vLLM layer contract: one input, one returned tensor.""" + output = torch.empty_like(hidden_states) + self._forward_into(hidden_states, output) + return output + + @torch.no_grad() + def _forward_into(self, hidden_states, output): + context = get_forward_context() + metadata = context.attn_metadata + metadata = metadata.get(self.prefix) if isinstance(metadata, dict) else None + if metadata is None: + # vLLM's pre-cache memory profiling has no request or persistent state. + from rl_engine.integrations.qwen3_next_forward import shared_linear + + qkvz = shared_linear(hidden_states, self.in_proj_qkvz.weight) + ba = shared_linear(hidden_states, self.in_proj_ba.weight) + zeros = qkvz.new_zeros((ba.shape[0], 1024)) + projected = shared_linear(zeros, self.out_proj.weight) + output.copy_(_ReduceFromTP.apply(projected, self.group)) + del qkvz, ba + return + tokens = metadata.num_actual_tokens + if tokens > hidden_states.shape[0] or output.shape != hidden_states.shape: + raise ValueError("vLLM GDN buffers do not cover the actual tokens") + indices, cu, reset = metadata_sequences(metadata, tokens) + output.zero_() + if not tokens: + return + conv = self.kv_cache[0] + if not is_conv_state_dim_first(): + conv = conv.transpose(-1, -2) + recurrent = self.kv_cache[1] + if reset.numel(): + state = GDNState(conv.clone(), recurrent.clone()) + state.convolution.index_fill_(0, reset.long(), 0) + state.recurrent.index_fill_(0, reset.long(), 0) + else: + state = GDNState(conv, recurrent) + value, next_state = TP4GDN.forward(self, hidden_states[:tokens], state, indices, cu) + trace_state_hook = getattr(self, "trace_state_hook", None) + if trace_state_hook is not None: + trace_state_hook(next_state, indices, cu) + output[:tokens].copy_(value) + conv.copy_(next_state.convolution) + recurrent.copy_(next_state.recurrent) + + +def register_shared_gdn_adapter(): + """Opt in to the actual GDN layer adapter, without claiming strict capability.""" + from vllm.model_executor.custom_op import op_registry_oot + + _register_backend() + existing = op_registry_oot.get("QwenGatedDeltaNetAttention") + if existing is SharedQwenGDN: + return + if existing is not None: + raise RuntimeError("A different out-of-tree Qwen GDN layer is already registered") + QwenGatedDeltaNetAttention.register_oot(SharedQwenGDN) diff --git a/rl_engine/integrations/qwen3_next_vllm_attention.py b/rl_engine/integrations/qwen3_next_vllm_attention.py new file mode 100644 index 000000000..b6d872b42 --- /dev/null +++ b/rl_engine/integrations/qwen3_next_vllm_attention.py @@ -0,0 +1,238 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Eager vLLM paged-KV adapter for the shared D=256 causal attention core. + +The scheduler owns physical blocks and slot mappings. This backend neither +reconstructs request IDs nor retains a separate Python KV cache. Capability +registration remains conservative until the real scheduler matrix passes. +""" + +from dataclasses import dataclass + +import torch +from vllm.v1.attention.backend import ( + AttentionBackend, + AttentionImpl, + AttentionMetadata, + AttentionMetadataBuilder, + AttentionType, +) + +from rl_engine.integrations.qwen3_next_forward import shared_attention + + +@dataclass +class SharedAttentionMetadata(AttentionMetadata): + query_start_loc: tuple[int, ...] + seq_lens: tuple[int, ...] + block_table: torch.Tensor + slot_mapping: torch.Tensor + num_actual_tokens: int + + +def validate_boundaries(starts, lengths, num_actual_tokens): + if len(starts) != len(lengths) + 1 or not starts or starts[0] != 0: + raise ValueError("Attention boundaries must start at zero and cover every request") + if starts[-1] != num_actual_tokens or num_actual_tokens < 0: + raise ValueError("Attention boundaries must exactly cover actual scheduled tokens") + for start, end, length in zip(starts[:-1], starts[1:], lengths, strict=True): + if end < start or length < end - start or length > 1040: + raise ValueError("Invalid Qwen eager query/prefix length") + + +class SharedAttentionBuilder(AttentionMetadataBuilder[SharedAttentionMetadata]): + def __init__(self, kv_cache_spec, layer_names, vllm_config, device): + super().__init__(kv_cache_spec, layer_names, vllm_config, device) + parallel = vllm_config.parallel_config + topology = ( + parallel.tensor_parallel_size, + parallel.pipeline_parallel_size, + parallel.decode_context_parallel_size, + parallel.prefill_context_parallel_size, + ) + if topology != (4, 1, 1, 1): + raise ValueError("Shared Qwen attention requires TP4/PP1/CP1") + if not vllm_config.model_config.enforce_eager or vllm_config.speculative_config is not None: + raise ValueError("Shared Qwen attention requires eager execution without speculation") + if vllm_config.cache_config.enable_prefix_caching: + raise ValueError("Prefix reuse is outside the fixed Qwen eager profile") + + def build(self, common_prefix_len, common_attn_metadata, fast_build=False): + if common_prefix_len != 0: + raise ValueError("Shared-prefix attention is outside the validated profile") + common = common_attn_metadata + if isinstance(common.causal, torch.Tensor): + causal = bool(common.causal.all()) + else: + causal = common.causal is True + if not causal: + raise ValueError("Only causal Qwen attention is supported") + # Use exact device lengths, not optimistic CPU bounds from spec decode. + starts = tuple(common.query_start_loc[: common.num_reqs + 1].cpu().tolist()) + lengths = tuple(common.seq_lens[: common.num_reqs].cpu().tolist()) + validate_boundaries(starts, lengths, common.num_actual_tokens) + return SharedAttentionMetadata( + starts, + lengths, + common.block_table_tensor[: common.num_reqs], + common.slot_mapping[: common.num_actual_tokens], + common.num_actual_tokens, + ) + + +class SharedAttentionImpl(AttentionImpl[SharedAttentionMetadata]): + def __init__( + self, + num_heads, + head_size, + scale, + num_kv_heads=None, + alibi_slopes=None, + sliding_window=None, + kv_cache_dtype="auto", + logits_soft_cap=None, + attn_type=AttentionType.DECODER, + kv_sharing_target_layer_name=None, + **kwargs, + ): + if (num_heads, head_size, num_kv_heads, scale) != (4, 256, 1, 1 / 16): + raise ValueError("Shared Qwen attention requires four Q heads and one D256 KV head") + unsupported = ( + alibi_slopes is not None, + sliding_window is not None, + logits_soft_cap not in (None, 0), + attn_type != AttentionType.DECODER, + kv_sharing_target_layer_name is not None, + kv_cache_dtype not in ("auto", "bfloat16"), + ) + if any(unsupported) or kwargs: + raise ValueError("Unsupported extension to the fixed Qwen attention profile") + self.num_heads, self.head_size, self.num_kv_heads = 4, 256, 1 + self.scale, self.kv_cache_dtype = scale, kv_cache_dtype + + def forward( + self, + layer, + query, + key, + value, + kv_cache, + attn_metadata, + output, + output_scale=None, + output_block_scale=None, + ): + if output_scale is not None or output_block_scale is not None: + raise ValueError("Quantized output is outside the BF16 eager profile") + if attn_metadata is None: + # vLLM allocation profiling has no requests or valid cache to read. + return output.zero_() + metadata = attn_metadata + n = metadata.num_actual_tokens + validate_boundaries(metadata.query_start_loc, metadata.seq_lens, n) + if any( + t.dtype != torch.bfloat16 or t.device != query.device + for t in (query, key, value, kv_cache, output) + ): + raise ValueError("Q/K/V/cache/output must be BF16 on the same CUDA device") + if query.device.type != "cuda": + raise ValueError("Shared Qwen attention executes on CUDA") + if query.shape[1:] != (4, 256) or key.shape[1:] != (1, 256) or value.shape != key.shape: + raise ValueError("Invalid projected Q/K/V shape") + # vLLM 0.30 materializes logical cache [blocks, KV heads, tokens, K+V]. + if kv_cache.ndim != 4 or kv_cache.shape[1] != 1 or kv_cache.shape[-1] != 512: + raise ValueError("Unexpected vLLM cache layout; no layout guessing is permitted") + if min(query.shape[0], key.shape[0], output.shape[0]) < n: + raise ValueError("Projected tensors do not cover every scheduled token") + slots = metadata.slot_mapping.to(device=query.device, dtype=torch.long) + if slots.shape != (n,) or slots.unique().numel() != n: + raise ValueError("Active KV write slots must be unique and match scheduled tokens") + block_size = kv_cache.shape[2] + if bool(((slots < 0) | (slots >= kv_cache.shape[0] * block_size)).any()): + raise ValueError("Active KV write slot is null or outside the allocated cache") + plans = [] + for row, (start, end, length) in enumerate( + zip( + metadata.query_start_loc[:-1], + metadata.query_start_loc[1:], + metadata.seq_lens, + strict=True, + ) + ): + if start == end: + continue + needed = (length + block_size - 1) // block_size + physical = metadata.block_table[row, :needed].to(device=query.device, dtype=torch.long) + if physical.numel() != needed or physical.unique().numel() != needed: + raise ValueError("Request KV blocks are incomplete or repeated") + if bool(((physical < 0) | (physical >= kv_cache.shape[0])).any()): + raise ValueError("Request KV block is outside the allocated cache") + logical = torch.arange(length, device=query.device) + physical_slots = physical[logical // block_size] * block_size + logical % block_size + if not torch.equal(physical_slots[-(end - start) :], slots[start:end]): + raise ValueError("Block table and write slots disagree about absolute positions") + plans.append((start, end, length, physical)) + if any(not torch.isfinite(tensor[:n]).all() for tensor in (query, key, value)): + raise ValueError("Projected attention inputs must be finite") + # Validate every request before mutating any scheduler-owned cache slot. + blocks, offsets = slots // block_size, slots % block_size + kv_cache[blocks, 0, offsets, :256] = key[:n, 0] + kv_cache[blocks, 0, offsets, 256:] = value[:n, 0] + output.zero_() + for start, end, length, physical in plans: + cached = kv_cache[physical, 0].reshape(-1, 512)[:length] + if not torch.isfinite(cached).all(): + raise ValueError("Request KV prefix contains nonfinite values") + k = cached[:, :256].reshape(1, 1, length, 256).contiguous() + v = cached[:, 256:].reshape(1, 1, length, 256).contiguous() + q = query[start:end].transpose(0, 1).unsqueeze(0).contiguous() + attended = shared_attention(q, k, v) + output[start:end].copy_(attended.squeeze(0).transpose(0, 1)) + return output + + +class SharedAttentionBackend(AttentionBackend): + supported_dtypes = [torch.bfloat16] + supported_kv_cache_dtypes = ["auto", "bfloat16"] + forward_includes_kv_cache_update = True + + @staticmethod + def get_name(): + # Attention.__init__ indexes AttentionBackendEnum with this value. + return "CUSTOM" + + @staticmethod + def get_impl_cls(): + return SharedAttentionImpl + + @staticmethod + def get_builder_cls(): + return SharedAttentionBuilder + + @classmethod + def get_supported_head_sizes(cls): + return [256] + + @classmethod + def supports_kv_connector(cls): + return False + + @classmethod + def supports_device_cpu_query_lens_mismatch(cls): + return False + + @classmethod + def supports_batch_invariance(cls): + from rl_engine.integrations.qwen3_next_profile import profile_is_certified + + return profile_is_certified() + + +def register_shared_attention_backend(): + from vllm.v1.attention.backends.registry import AttentionBackendEnum, register_backend + + register_backend( + AttentionBackendEnum.CUSTOM, + "rl_engine.integrations.qwen3_next_vllm_attention.SharedAttentionBackend", + ) diff --git a/rl_engine/integrations/qwen3_next_vllm_model.py b/rl_engine/integrations/qwen3_next_vllm_model.py new file mode 100644 index 000000000..445c6ca5f --- /dev/null +++ b/rl_engine/integrations/qwen3_next_vllm_model.py @@ -0,0 +1,365 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Opt-in vLLM model using shared Qwen3-Next arithmetic and native weight loaders. + +Construction preserves every native Parameter and loader. The existing modules +are bound to subclasses before weights load; no second copy of the 80B model is +allocated. Only the selected eager profile is supported. Registering it does not +declare scheduler batch invariance or completion of full-model L2 acceptance. +""" + +from importlib.metadata import version + +import torch +import torch.nn.functional as F +from vllm.distributed import get_tp_group +from vllm.model_executor.layers.fused_moe.unquantized_fused_moe_method import ( + UnquantizedFusedMoEMethod, +) +from vllm.model_executor.models.qwen3_next import ( + Qwen3NextAttention, + Qwen3NextDecoderLayer, + Qwen3NextForCausalLM, + Qwen3NextModel, + Qwen3NextSparseMoeBlock, +) +from vllm.v1.attention.backends.registry import AttentionBackendEnum + +from rl_engine.distributed.collectives import collective_for_group +from rl_engine.integrations.qwen3_next_forward import ( + FORWARD_PROVIDER_ID, + shared_linear, + shared_logprob, + shared_moe, +) +from rl_engine.integrations.qwen3_next_logprob import qwen_logprob_contract +from rl_engine.integrations.qwen3_next_tp import _ReduceFromTP +from rl_engine.integrations.qwen3_next_tp_blocks import TP4FullAttention +from rl_engine.kernels.logprob_contract import LogprobRole + +ARCHITECTURE = "RLKernelQwen3NextForCausalLM" + + +def profile_capabilities(): + return { + "provider": FORWARD_PROVIDER_ID, + "architecture": ARCHITECTURE, + "layers": 48, + "tensor_parallel_size": 4, + "scheduler_batch_invariance_verified": False, + "full_model_l2_verified": False, + } + + +def validate_profile(vllm_config): + if version("vllm") != "0.30.0": + raise ValueError("Shared model requires pinned vLLM 0.30.0") + model, parallel = vllm_config.model_config, vllm_config.parallel_config + config = model.hf_text_config + required = { + "hidden_size": 2048, + "num_hidden_layers": 48, + "num_attention_heads": 16, + "num_key_value_heads": 2, + "head_dim": 256, + "vocab_size": 151936, + "num_experts": 512, + "num_experts_per_tok": 10, + "moe_intermediate_size": 512, + "shared_expert_intermediate_size": 512, + "rms_norm_eps": 1e-6, + "max_position_embeddings": 262144, + } + for name, expected in required.items(): + if getattr(config, name, None) != expected: + raise ValueError(f"Shared model requires official {name}={expected}") + expected_types = [ + "full_attention" if index % 4 == 3 else "linear_attention" for index in range(48) + ] + if list(config.layer_types) != expected_types: + raise ValueError("Shared model requires the official 36 GDN / 12 full-attention layers") + rope = config.rope_parameters + if ( + rope.get("rope_theta") != 10000000 + or rope.get("partial_rotary_factor") != 0.25 + or rope.get("rope_type", "default") != "default" + ): + raise ValueError("Shared model requires default theta=1e7 and rotary dimension 64") + topology = tuple( + getattr(parallel, name, 1) + for name in ( + "tensor_parallel_size", + "pipeline_parallel_size", + "data_parallel_size", + "decode_context_parallel_size", + "prefill_context_parallel_size", + ) + ) + if topology != (4, 1, 1, 1, 1): + raise ValueError("Shared model requires TP4/PP1/DP1/CP1") + unsupported = ( + model.dtype != torch.bfloat16, + not model.enforce_eager, + vllm_config.quant_config is not None, + vllm_config.speculative_config is not None, + vllm_config.lora_config is not None, + vllm_config.cache_config.enable_prefix_caching, + parallel.enable_expert_parallel, + parallel.enable_eplb, + parallel.use_sequence_parallel_moe, + parallel.enable_dbo, + getattr(config, "attention_dropout", 0) != 0, + getattr(config, "mlp_only_layers", []), + getattr(config, "decoder_sparse_step", 1) != 1, + getattr(config, "layer_scale", False), + not getattr(config, "norm_topk_prob", True), + getattr(config, "hidden_act", "silu") != "silu", + getattr(config, "qkv_bias", False), + not getattr(config, "attn_output_gate", True), + not getattr(config, "is_causal", True), + getattr(config, "tie_word_embeddings", False), + ) + if any(unsupported): + raise ValueError("Unsupported extension to the fixed BF16 eager shared model profile") + # This selects the native plain-weight allocation, not the execution path. + # The shared MoE below owns all forward arithmetic and prevents repacking. + if vllm_config.kernel_config.moe_backend != "triton": + raise ValueError("Set kernel_config.moe_backend='triton' for plain native BF16 allocation") + if vllm_config.attention_config.backend != AttentionBackendEnum.CUSTOM: + raise ValueError("Set attention_config.backend='CUSTOM' for the shared paged-KV backend") + + +def _rms(value, module): + from rl_engine.kernels.ops.cuda.norm.rmsnorm import Qwen3NextRMSNormCudaOp + + return Qwen3NextRMSNormCudaOp()(value, module.weight, eps=1e-6) + + +def _require_weight(value, shape, name): + if value.dtype != torch.bfloat16 or value.shape != shape or not value.is_contiguous(): + raise ValueError(f"{name} must remain contiguous BF16 {shape}") + + +class SharedPlainBF16MoEMethod(UnquantizedFusedMoEMethod): + """Retain HF-loaded plain tensors; the shared model never calls native MoE.""" + + def process_weights_after_loading(self, layer): + _require_weight(layer.w13_weight, (512, 256, 2048), "routed gate/up") + _require_weight(layer.w2_weight, (512, 2048, 128), "routed down") + self.moe_quant_config = self.get_fused_moe_quant_config(layer) + + def apply(self, *args, **kwargs): + raise RuntimeError("Use SharedVLLMMoE; native MoE arithmetic is outside this provider") + + def apply_monolithic(self, *args, **kwargs): + raise RuntimeError("Use SharedVLLMMoE; native MoE arithmetic is outside this provider") + + +class SharedVLLMAttention(Qwen3NextAttention): + def forward(self, positions, hidden_states): + if positions.ndim != 1 or positions.shape[0] != hidden_states.shape[0]: + raise ValueError("Shared text attention requires one absolute position per token") + if bool(((positions < 0) | (positions >= 262144)).any()): + raise ValueError("Attention positions exceed the official RoPE range") + # Keep the three GEMM invocations identical to the training TP4 module; + # native qkv_proj owns one packed allocation and all three HF loaders. + q_weight, k_weight, v_weight = self.qkv_proj.weight.split((2048, 256, 256)) + query, gate = shared_linear(hidden_states, q_weight).reshape(-1, 4, 512).chunk(2, -1) + key = shared_linear(hidden_states, k_weight).reshape(-1, 1, 256) + value = shared_linear(hidden_states, v_weight) + query, key = _rms(query, self.q_norm), _rms(key, self.k_norm) + query = TP4FullAttention._rotate(self, query, positions).reshape(-1, 1024) + key = TP4FullAttention._rotate(self, key, positions).reshape(-1, 256) + attended = self.attn(query, key, value).reshape(-1, 4, 256) * gate.sigmoid() + local = shared_linear(attended.reshape(-1, 1024), self.o_proj.weight) + return _ReduceFromTP.apply(local, self.shared_tp_group) + + +class SharedVLLMMoE(Qwen3NextSparseMoeBlock): + subnode_hook = None + + def forward(self, hidden_states, already_sequence_parallel=False): + if already_sequence_parallel: + raise ValueError("Shared MoE does not enable sequence parallelism") + routed = self.experts.routed_experts + output, routes = shared_moe( + hidden_states, self.gate.weight, routed.w13_weight, routed.w2_weight + ) + if self.subnode_hook is not None: + self.subnode_hook("router", routes.indices) + gate_weight, up_weight = self.shared_expert.gate_up_proj.weight.chunk(2, dim=0) + gate = shared_linear(hidden_states, gate_weight) + up = shared_linear(hidden_states, up_weight) + activated = (F.silu(gate.float()) * up.float()).to(torch.bfloat16) + shared = shared_linear(activated, self.shared_expert.down_proj.weight) + shared_gate = shared_linear(hidden_states, self.shared_expert_gate.weight).sigmoid() + return _ReduceFromTP.apply(output + shared * shared_gate, self.shared_tp_group) + + +class SharedVLLMDecoderLayer(Qwen3NextDecoderLayer): + # Set by a trace owner to observe the sub-node outputs named in + # qwen3_next_subnode.SUBNODES; observing never changes a value. + subnode_hook = None + + def forward(self, hidden_states, residual, positions, **kwargs): + trace = self.subnode_hook + residual = hidden_states if residual is None else hidden_states + residual + hidden_states = _rms(residual, self.input_layernorm) + if trace is not None: + trace("input_layernorm", hidden_states) + if self.layer_type == "linear_attention": + hidden_states = self.linear_attn(hidden_states=hidden_states) + else: + hidden_states = self.self_attn(positions, hidden_states) + if trace is not None: + trace("mixer", hidden_states) + residual = residual + hidden_states + hidden_states = _rms(residual, self.post_attention_layernorm) + if trace is not None: + trace("post_attention_layernorm", hidden_states) + hidden_states = self.mlp(hidden_states) + if trace is not None: + trace("mlp", hidden_states) + return hidden_states, residual + + +class SharedVLLMModel(Qwen3NextModel): + subnode_hook = None + + def embed_input_ids(self, input_ids): + from rl_engine.integrations.qwen3_next_embedding import shared_embedding + + if input_ids.ndim != 1 or input_ids.dtype not in (torch.int32, torch.int64): + raise ValueError("Shared embedding requires a packed integer token vector") + return shared_embedding(input_ids.long(), self.embed_tokens.weight, self.shared_tp_group) + + def forward(self, input_ids, positions, intermediate_tensors=None, inputs_embeds=None): + if intermediate_tensors is not None or inputs_embeds is not None: + raise ValueError("Shared text-only PP1 profile takes token IDs directly") + if self.shared_forward_hook is not None: + self.shared_forward_hook(input_ids, positions) + hidden_states = self.embed_input_ids(input_ids) + if self.subnode_hook is not None: + self.subnode_hook("embedding", hidden_states) + residual = None + for index, layer in enumerate(self.layers): + hidden_states, residual = layer(hidden_states, residual, positions) + if self.shared_trace_hook is not None: + self.shared_trace_hook(index, hidden_states + residual, layer) + return _rms(hidden_states + residual, self.norm) + + +def _bind_native_model(model, group): + """Attach shared behavior without replacing parameters or their HF loaders.""" + from rl_engine.integrations.qwen3_next_vllm import SharedQwenGDN + from rl_engine.integrations.qwen3_next_vllm_attention import SharedAttentionBackend + + if len(model.layers) != 48 or (model.start_layer, model.end_layer) != (0, 48): + raise ValueError("The native model must own all 48 layers under PP1") + _require_weight(model.embed_tokens.weight, (37984, 2048), "embedding") + _require_weight(model.norm.weight, (2048,), "final norm") + for layer in model.layers: + layer.__class__ = SharedVLLMDecoderLayer + if layer.layer_type == "linear_attention": + if not isinstance(layer.linear_attn, SharedQwenGDN): + raise ValueError("Native GDN was not constructed with the shared cache adapter") + else: + attention = layer.self_attn + if attention.attn.attn_backend is not SharedAttentionBackend: + raise ValueError("Full attention must use the explicit shared paged-KV backend") + _require_weight(attention.qkv_proj.weight, (2560, 2048), "attention QKV/gate") + _require_weight(attention.o_proj.weight, (2048, 1024), "attention output") + attention.__class__ = SharedVLLMAttention + attention.shared_tp_group = group + # TP4FullAttention._rotate derives FP32 inverse frequencies from this + # float on every call, so both engines rotate with identical tables. + attention.rope_theta = 10000000.0 + moe = layer.mlp + if ( + not isinstance(moe, Qwen3NextSparseMoeBlock) + or moe.shared_expert is None + or moe.is_fused_shared_expert_enabled + or moe.replicate_shared_expert + ): + raise ValueError( + "Every layer requires all512 unfused TP experts plus a sharded shared expert" + ) + routed = moe.experts.routed_experts + if type(routed.quant_method) is not UnquantizedFusedMoEMethod: + raise ValueError("Shared MoE requires native unquantized BF16 parameter loaders") + _require_weight(routed.w13_weight, (512, 256, 2048), "routed gate/up") + _require_weight(routed.w2_weight, (512, 2048, 128), "routed down") + _require_weight(moe.shared_expert.gate_up_proj.weight, (256, 2048), "shared gate/up") + _require_weight(moe.shared_expert.down_proj.weight, (2048, 128), "shared down") + routed.quant_method.__class__ = SharedPlainBF16MoEMethod + moe.__class__ = SharedVLLMMoE + moe.shared_tp_group = group + model.__class__ = SharedVLLMModel + model.shared_tp_group, model.shared_trace_hook = group, None + model.shared_forward_hook = None + + +class RLKernelQwen3NextForCausalLM(Qwen3NextForCausalLM): + def __init__(self, *, vllm_config, prefix=""): + validate_profile(vllm_config) + from rl_engine.integrations.qwen3_next_vllm import register_shared_gdn_adapter + from rl_engine.integrations.qwen3_next_vllm_attention import ( + register_shared_attention_backend, + ) + + register_shared_gdn_adapter() + register_shared_attention_backend() + super().__init__(vllm_config=vllm_config, prefix=prefix) + self.shared_tp_group = get_tp_group().device_group + _bind_native_model(self.model, self.shared_tp_group) + _require_weight(self.lm_head.weight, (37984, 2048), "LM head") + self.shared_logits_hook = None + + def forward( + self, input_ids, positions, intermediate_tensors=None, inputs_embeds=None, **kwargs + ): + # Call the explicit eager method instead of the native compile wrapper. + return self.model.forward(input_ids, positions, intermediate_tensors, inputs_embeds) + + def compute_local_logits(self, hidden_states): + return shared_linear(hidden_states, self.lm_head.weight) + + def compute_logits(self, hidden_states): + local = self.compute_local_logits(hidden_states) + if not torch.isfinite(local).all(): + raise ValueError("Shared model produced non-finite logits") + source = local.t().contiguous() + collective = collective_for_group( + self.shared_tp_group, min_size_bytes=source.numel() * source.element_size() + ) + logits = collective.all_gather(source).t().contiguous() + if self.shared_logits_hook is not None: + self.shared_logits_hook(hidden_states, local, logits) + return logits + + def compute_selected_logprobs(self, hidden_states, targets): + """The L2/fixture path; native sampler logprobs are not provider evidence.""" + local = self.compute_local_logits(hidden_states) + contract = qwen_logprob_contract( + local, + rank=get_tp_group().rank_in_group, + world_size=4, + role=LogprobRole.INFER, + ) + return shared_logprob(local, targets, contract=contract, tp_group=self.shared_tp_group)[0] + + +def register_shared_model(): + """Call in every vLLM process before constructing the explicit architecture.""" + from vllm.model_executor.models import ModelRegistry + + path = "rl_engine.integrations.qwen3_next_vllm_model:RLKernelQwen3NextForCausalLM" + if ARCHITECTURE in ModelRegistry.get_supported_archs(): + if ModelRegistry.models[ARCHITECTURE].load_model_cls() is not RLKernelQwen3NextForCausalLM: + raise ValueError( + "A different model is already registered under the shared architecture" + ) + else: + ModelRegistry.register_model(ARCHITECTURE, path) + return ARCHITECTURE diff --git a/rl_engine/integrations/qwen3_next_vllm_worker.py b/rl_engine/integrations/qwen3_next_vllm_worker.py new file mode 100644 index 000000000..9b854daec --- /dev/null +++ b/rl_engine/integrations/qwen3_next_vllm_worker.py @@ -0,0 +1,51 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""vLLM spawned-worker registration for the explicit shared Qwen architecture.""" + +from vllm.v1.worker.gpu_worker import Worker + +from rl_engine.integrations.qwen3_next_binary import imported_provider_identity +from rl_engine.integrations.qwen3_next_vllm_model import register_shared_model + + +class SharedQwenWorker(Worker): + """Select through LLM(worker_cls=...) so spawn never relies on parent globals.""" + + def __init__(self, *args, **kwargs): + config = kwargs.get("vllm_config", args[0] if args else None) + identity = ( + config.additional_config.get("qwen3_next_shared_identity") + if config is not None + else None + ) + mode = None + if identity is not None: + from rl_engine.integrations.qwen3_next_profile import activate_for_identity + + mode = activate_for_identity(identity) + register_shared_model() + self.shared_provider_binary = imported_provider_identity() + super().__init__(*args, **kwargs) + self.shared_l2_trace = None + self.shared_provider_execution_mode = mode + + def configure_l2_trace(self, request, output_directory): + from rl_engine.integrations.qwen3_next_native_rollout import RolloutTrace + + if self.shared_l2_trace is not None: + raise RuntimeError("The previous rollout trace has not been collected") + self.shared_l2_trace = RolloutTrace(self, request, output_directory) + return {"rank": self.rank, "request_sha256": request["request_sha256"]} + + def collect_l2_trace(self): + if self.shared_l2_trace is None: + raise RuntimeError("No active rollout trace") + result = self.shared_l2_trace.finish() + self.shared_l2_trace = None + return result + + def execute_model(self, scheduler_output): + if self.shared_l2_trace is not None: + self.shared_l2_trace.before_execute(scheduler_output) + return super().execute_model(scheduler_output) diff --git a/rl_engine/kernels/gtest/qwen3_next_chain_report.py b/rl_engine/kernels/gtest/qwen3_next_chain_report.py new file mode 100644 index 000000000..3326dd0af --- /dev/null +++ b/rl_engine/kernels/gtest/qwen3_next_chain_report.py @@ -0,0 +1,268 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Qwen3-Next full-model chain as a WS1 chain-gate report. + +The Qwen3-Next chain runs as stages (``scripts/qwen3_next_run.py``): L2 on the +pinned weights, then L2 again after each of two optimizer updates and after a +reload. Each L2 stage writes a 16-case raw-bit matrix comparing rollout +(native vLLM prefill + decode) with replay (VIME recompute). This module turns +those matrices into the same report shape as the Qwen3-8B chain gate +(:class:`rl_engine.kernels.gtest.chain_gate.ChainGateReport`), built from the +same dataclasses, so the two models' reports can be read side by side. + +Every case of every weight version becomes one cell. Bitwise rollout/replay +equality of logits and selected logprobs becomes one +:class:`TensorComparisonDetail` each, with ``atol = rtol = 0``. The first +divergence names a decoder layer and a sub-node (see +``rl_engine.integrations.qwen3_next_subnode``), which this report also maps to +a layer family. +""" + +from __future__ import annotations + +from collections.abc import Mapping +from typing import Any + +import torch + +from rl_engine.kernels.gtest.chain_gate import CellOutput, ChainGateReport +from rl_engine.kernels.gtest.forward_invariance import TensorComparisonDetail +from rl_engine.kernels.gtest.tolerance import ( + BackendProvenance, + compute_logprob_aggregates, + default_clip_interval, + judge_logprob_aggregates, + resolve_comparison_roles, +) + +SCHEMA_VERSION = "qwen3-next-chain-v1" +COMPATIBLE_SCHEMA = "ws1-c10-c11-v5" +PROFILE = "qwen3_next_tp4_shared_eager_v1" +WEIGHT_VERSIONS = ("baseline", "update1", "update2", "reload") +FAMILIES = ("embedding", "norm", "gdn", "full_attention", "moe", "lm_head_logprob") +_TRAIN_INFER_KIND = "train_infer_logprob_parity" +DISCLAIMER = ( + "Qwen3-Next-80B-A3B-Instruct, TP4 x EP1, BF16, strict shared providers. Cells are the " + "16-case raw-bit L2 matrix (prompt 8/64/256/1024 x batch 1/4 x full/chunked prefill, 16 " + "response tokens) at each weight version; rollout is native vLLM prefill and decode, " + "replay is VIME recompute. Gradients are judged by the training stages (two updates, " + "reload and resumption), not by C11 gradient rows, so gradient fields are empty here." +) + + +def family_of(layer: int | None, node: str | None) -> str | None: + """Map a first divergence to the layer family that produced it.""" + if node is None: + return None + if node == "embedding": + return "embedding" + if node in ("logits", "logprobs", "response_tokens", "lm_head_logprob"): + return "lm_head_logprob" + if layer is None or layer not in range(48): + raise ValueError(f"Divergence node {node!r} needs a decoder layer, got {layer!r}") + if node in ("input_layernorm", "post_attention_layernorm"): + return "norm" + if node in ("router", "mlp"): + return "moe" + if node in ("mixer", "output", "state"): + return "full_attention" if (layer + 1) % 4 == 0 else "gdn" + raise ValueError(f"Unknown divergence node {node!r}") + + +def first_drift_of(record: Mapping[str, Any]) -> tuple[str | None, int | None, str | None]: + """``(dense first_drift, layer, family)`` of one L2 case record.""" + if record["passed"]: + return None, None, None + divergence = record.get("first_divergent_node") + if divergence is not None: + layer, node = divergence["layer"], divergence["node"] + label = "embedding" if node == "embedding" else f"layer{layer}.{node}" + else: + layer = None + difference = record.get("first_difference") or {} + node = difference.get("tensor", "lm_head_logprob") + label = node + return f"{label}:rollout->recompute", layer, family_of(layer, node) + + +def _detail(name, cell_id, rollout, recompute, roles) -> TensorComparisonDetail: + lhs, rhs = rollout.double(), recompute.double() + error = (lhs - rhs).abs() + return TensorComparisonDetail( + tensor_name=name, + config_pair=(f"{cell_id}/rollout", f"{cell_id}/recompute"), + shape=tuple(rollout.shape), + dtype=str(rollout.dtype).replace("torch.", ""), + max_abs_error=float(error.max()), + mean_abs_error=float(error.mean()), + max_rel_error=float((error / rhs.abs().clamp_min(1e-12)).max()), + atol=0.0, + rtol=0.0, + passed=bool(torch.equal(rollout, recompute)), + judgment="forward_invariance", + comparison_lhs_role=roles.comparison_lhs_role, + comparison_rhs_role=roles.comparison_rhs_role, + ) + + +def build_report( + matrices: Mapping[str, Mapping[str, Any]], + artifacts: Mapping[str, Mapping[str, Mapping[str, Any]]], + *, + contract: Mapping[str, Any], + measured_commit: str, + weight_source: str, + weight_hash: str, + device: str, + gpu_name: str | None, + compute_capability: str | None, +) -> dict[str, Any]: + """Build the chain report from L2 matrices and their loaded case artifacts. + + ``matrices[version]`` is an ``l2-matrix.json`` report; ``artifacts[version] + [case_id][role]`` is the loaded rollout/recompute artifact of that case. + """ + if not matrices or set(matrices) - set(WEIGHT_VERSIONS): + raise ValueError(f"Weight versions must be a subset of {WEIGHT_VERSIONS}") + roles = resolve_comparison_roles(contract, "forward_invariance") + train_roles = resolve_comparison_roles(contract, _TRAIN_INFER_KIND) + clip = default_clip_interval(contract) + cells, invariance, decode_prefill = {}, [], [] + lhs_all, rhs_all = [], [] + first = None + for version in WEIGHT_VERSIONS: + if version not in matrices: + continue + matrix = matrices[version] + for case_id in matrix["case_ids"]: + record = matrix["cases"][case_id] + rollout = artifacts[version][case_id]["rollout"] + recompute = artifacts[version][case_id]["recompute"] + cell_id = f"{version}/{case_id}" + drift, layer, family = first_drift_of(record) + if drift is not None and first is None: + first = (drift, layer, family, cell_id) + batch = rollout["logprobs"].shape[0] + cells[cell_id] = CellOutput( + cell_id=cell_id, + selected_logp={ + (f"sample{row}", position): rollout["logprobs"][row, position] + for row in range(batch) + for position in range(16) + }, + loss=None, + grads={}, + first_drift_node=drift, + node_digests={ + "embedding": rollout["embedding_digest"]["sha256"], + **{ + f"layer{item['layer']}": item["output_sha256"] + for item in rollout["layer_digests"] + }, + }, + requested_backend=PROFILE, + actual_backend=PROFILE, + ) + for name in ("logprobs", "logits"): + invariance.append(_detail(name, cell_id, rollout[name], recompute[name], roles)) + lhs_all.append(rollout["logprobs"].reshape(-1)) + rhs_all.append(recompute["logprobs"].reshape(-1)) + if case_id.endswith("-chunked"): + full = artifacts[version][case_id.replace("-chunked", "-full")]["rollout"] + verdict = judge_logprob_aggregates( + compute_logprob_aggregates( + rollout["logprobs"].reshape(-1), + full["logprobs"].reshape(-1), + torch.ones(rollout["logprobs"].numel(), dtype=torch.bool), + contract=contract, + report_kind=_TRAIN_INFER_KIND, + clip_interval=clip, + comparison_lhs_role=train_roles.comparison_lhs_role, + comparison_rhs_role=train_roles.comparison_rhs_role, + ), + contract, + execution_dtype="bf16", + ) + decode_prefill.append((cell_id, verdict)) + lhs, rhs = torch.cat(lhs_all), torch.cat(rhs_all) + train_infer = judge_logprob_aggregates( + compute_logprob_aggregates( + lhs, + rhs, + torch.ones(lhs.numel(), dtype=torch.bool), + contract=contract, + report_kind=_TRAIN_INFER_KIND, + clip_interval=clip, + comparison_lhs_role=train_roles.comparison_lhs_role, + comparison_rhs_role=train_roles.comparison_rhs_role, + ), + contract, + execution_dtype="bf16", + ) + passed = ( + first is None + and all(matrix["status"] == "passed" for matrix in matrices.values()) + and all(item.passed for item in invariance) + ) + report = ChainGateReport( + backend_profile=PROFILE, + workload_id="qwen3-next-l2-matrix-16", + fixture_hash=matrices[next(iter(matrices))]["fixture"]["content_sha256"], + config_fingerprint={"model": "Qwen3-Next-80B-A3B-Instruct", "tp": 4, "ep": 1, "pp": 1}, + weight_source=weight_source, + weight_hash=weight_hash, + seed=1234, + workload_seed=1234, + device=device, + gpu_name=gpu_name, + compute_capability=compute_capability, + backend_provenance=BackendProvenance( + backend_profile=PROFILE, + requested_backend=PROFILE, + actual_backend=PROFILE, + execution_dtype="bf16", + accumulation_dtype="fp32", + output_dtype="bf16", + reference_dtype="bf16", + candidate_tf32_enabled=False, + reference_tf32_enabled=False, + ), + runtime_backend_observations={}, + backward_runtime_observations={}, + cells=cells, + invariance=tuple(invariance), + gradient_invariance=(), + accuracy=(), + gradient_accuracy=(), + accuracy_aggregates=(), + train_infer=train_infer, + train_infer_bn=None, + decode_prefill=tuple(decode_prefill), + first_drift=None if first is None else first[0], + aggregates=train_infer, + gradient_scope="training_stage_reports", + required_grad_names=(), + all_parameter_gradients=False, + representative_case_ids=tuple(cells), + workflow_url=None, + c8_evidence_path="", + c8_source_commit=None, + passed=passed, + backward_executed="update1" in matrices, + train_infer_executed=True, + accuracy_executed=False, + gradient_accuracy_executed=False, + disclaimer=DISCLAIMER, + ) + payload = report.to_dict() + payload.update( + schema_version=SCHEMA_VERSION, + compatible_schema=COMPATIBLE_SCHEMA, + measured_commit=measured_commit, + weight_versions=[version for version in WEIGHT_VERSIONS if version in matrices], + first_drift_cell=None if first is None else first[3], + first_drift_layer=None if first is None else first[1], + first_drift_family=None if first is None else first[2], + ) + return payload diff --git a/rl_engine/testing/pytest_case_manifest.py b/rl_engine/testing/pytest_case_manifest.py new file mode 100644 index 000000000..7371727c7 --- /dev/null +++ b/rl_engine/testing/pytest_case_manifest.py @@ -0,0 +1,80 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Pytest plugin enforcing Qwen3-Next's declared mandatory case identities.""" + +import json +import time +from pathlib import Path + +from rl_engine.testing.qwen3_next_cases import execution_verdict, load_cases + + +def pytest_addoption(parser): + group = parser.getgroup("qwen3-next acceptance") + group.addoption("--case-manifest", required=True) + group.addoption("--case-suite", choices=("norm", "core", "all", "diagnostics"), required=True) + group.addoption("--case-report", required=True) + + +def pytest_configure(config): + plugin = CaseRecorder( + load_cases(Path(config.getoption("--case-manifest")), config.getoption("--case-suite")), + Path(config.getoption("--case-report")), + ) + config.pluginmanager.register(plugin, "qwen3-next-case-recorder") + + +class CaseRecorder: + def __init__(self, expected, output): + self.expected = expected + self.output = output + self.collected = [] + self.outcomes = {} + self.active = None + + def _progress(self, status="running"): + """Observable progress is separate from the final acceptance artifact.""" + self.output.parent.mkdir(parents=True, exist_ok=True) + target = self.output.with_suffix(".progress.json") + partial = target.with_suffix(".partial") + payload = json.dumps( + { + "status": status, + "active_case": self.active, + "observed_at": time.time(), + "collected": len(self.collected), + "required": len(self.expected), + "finished_calls": sum("call" in phases for phases in self.outcomes.values()), + "outcomes": self.outcomes, + }, + indent=2, + ) + partial.write_text(payload + "\n") + partial.replace(target) + + def pytest_collection_finish(self, session): + self.collected = [item.nodeid for item in session.items] + self._progress() + + def pytest_runtest_logstart(self, nodeid, location): + self.active = nodeid + self._progress() + + def pytest_runtest_logreport(self, report): + outcome = "skipped" if hasattr(report, "wasxfail") else report.outcome + self.outcomes.setdefault(report.nodeid, {})[report.when] = outcome + self._progress() + + def pytest_sessionfinish(self, session, exitstatus): + result = execution_verdict(self.expected, self.collected, self.outcomes) + result["pytest_exit_code"] = int(exitstatus) + result["passed"] = result["passed"] and exitstatus == 0 + self.output.parent.mkdir(parents=True, exist_ok=True) + partial = self.output.with_suffix(self.output.suffix + ".partial") + partial.write_text(json.dumps(result, indent=2) + "\n") + partial.replace(self.output) + self.active = None + self._progress("complete") + if not result["passed"] and exitstatus == 0: + session.exitstatus = 1 diff --git a/rl_engine/testing/qwen3_next_binary.py b/rl_engine/testing/qwen3_next_binary.py new file mode 100644 index 000000000..41ad8165b --- /dev/null +++ b/rl_engine/testing/qwen3_next_binary.py @@ -0,0 +1,89 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Bind each native model execution to its freshly built and archived CUDA binary.""" + +import hashlib +import json +import os +import shutil +from pathlib import Path + + +def _digest(path): + with Path(path).open("rb") as stream: + return hashlib.file_digest(stream, "sha256").hexdigest() + + +def archive_provider_binary(source, output, *, source_tree): + source, output = Path(source).absolute(), Path(output).absolute() + binaries = list((source / "rl_engine").glob("_C.cpython*.so")) + if len(binaries) != 1 or not source_tree: + raise ValueError("Expected one freshly built CUDA provider and its source tree") + binary = binaries[0] + digest = _digest(binary) + archive = output / "provider-binary.so" + temporary = archive.with_suffix(".partial") + shutil.copyfile(binary, temporary) + with temporary.open("rb") as stream: + os.fsync(stream.fileno()) + if _digest(temporary) != digest: + raise ValueError("CUDA provider changed while archiving its execution identity") + temporary.replace(archive) + payload = { + "schema_version": 1, + "source_tree": source_tree, + "actual": {"module": "rl_engine._C", "path": str(binary), "sha256": digest}, + "archive": {"path": str(archive), "sha256": digest}, + } + path = output / "provider-binary.json" + path.write_text(json.dumps(payload, indent=2) + "\n") + with path.open("rb") as stream: + os.fsync(stream.fileno()) + return {"path": str(path), "sha256": _digest(path)} + + +def validate_binary_record(record, *, source_tree=None, artifact_root=None): + """Check archived bytes, without depending on a subsequently rebuilt live file.""" + if not isinstance(record, dict) or set(record) != {"path", "sha256"}: + raise ValueError("Native execution requires a pinned CUDA binary evidence record") + + def checked(item): + path = Path(item["path"]) + if not path.is_absolute() or not path.is_file(): + raise ValueError("CUDA binary evidence requires an existing absolute path") + if artifact_root is not None and not path.resolve().is_relative_to( + Path(artifact_root).resolve() + ): + raise ValueError("CUDA binary evidence escaped its owning stage") + if _digest(path) != item["sha256"]: + raise ValueError("CUDA provider evidence content changed") + return path + + payload = json.loads(checked(record).read_text()) + if payload.get("schema_version") != 1 or not payload.get("source_tree"): + raise ValueError("Unsupported CUDA provider evidence") + if source_tree is not None and payload["source_tree"] != source_tree: + raise ValueError("CUDA provider belongs to another source tree") + actual = payload.get("actual", {}) + if set(actual) != {"module", "path", "sha256"} or actual["module"] != "rl_engine._C": + raise ValueError("The actual native CUDA extension identity is missing") + if not Path(actual["path"]).is_absolute() or not Path(actual["path"]).name.startswith( + "_C.cpython" + ): + raise ValueError("Provider identity does not name the actual native extension") + archive = payload.get("archive", {}) + if archive.get("sha256") != actual["sha256"]: + raise ValueError("Archived binary differs from the binary imported by workers") + checked(archive) + return actual + + +def verify_imported_provider(record, *, source_tree): + from rl_engine.integrations.qwen3_next_binary import imported_provider_identity + + expected = validate_binary_record(record, source_tree=source_tree) + actual = imported_provider_identity() + if actual != expected: + raise ValueError("Worker imported a different CUDA binary than its stage built") + return actual diff --git a/rl_engine/testing/qwen3_next_cases.py b/rl_engine/testing/qwen3_next_cases.py new file mode 100644 index 000000000..72e1794c7 --- /dev/null +++ b/rl_engine/testing/qwen3_next_cases.py @@ -0,0 +1,69 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Pinned operator case identities and complete execution verdicts.""" + +import hashlib +import json +from pathlib import Path + + +def load_cases(path: Path, suite: str) -> list[str]: + manifest = json.loads(path.read_text()) + if manifest.get("schema_version") != 1: + raise ValueError("Unsupported operator case manifest schema") + groups = manifest["suites"] + all_ids = [case for cases in groups.values() for case in cases] + if len(all_ids) != len(set(all_ids)): + raise ValueError("Operator case IDs must be unique across suites") + names = ("norm", "core") if suite == "all" else (suite,) + cases = sorted(case for name in names for case in groups[name]) + if not cases or any(not isinstance(case, str) or "::test_" not in case for case in cases): + raise ValueError("Operator suites must contain nonempty pytest case IDs") + return cases + + +def case_digest(cases: list[str]) -> str: + return hashlib.sha256(("\n".join(sorted(cases)) + "\n").encode()).hexdigest() + + +def execution_verdict(expected: list[str], collected: list[str], outcomes: dict) -> dict: + """Require the complete declared collection and successful setup/call/teardown.""" + missing = sorted(set(expected) - set(collected)) + unexpected = sorted(set(collected) - set(expected)) + duplicates = len(collected) != len(set(collected)) + counts = {"tests": len(expected), "failures": 0, "errors": 0, "skipped": 0} + incomplete = [] + for case in expected: + phases = outcomes.get(case, {}) + if any(value == "skipped" for value in phases.values()): + counts["skipped"] += 1 + elif phases.get("setup") == "failed" or phases.get("teardown") == "failed": + counts["errors"] += 1 + elif phases.get("call") == "failed": + counts["failures"] += 1 + elif phases != {"setup": "passed", "call": "passed", "teardown": "passed"}: + incomplete.append(case) + passed = not any( + ( + missing, + unexpected, + duplicates, + incomplete, + counts["failures"], + counts["errors"], + counts["skipped"], + ) + ) + return { + "passed": passed, + "expected_case_ids": expected, + "expected_case_ids_sha256": case_digest(expected), + "collected_case_ids": collected, + "missing": missing, + "unexpected": unexpected, + "duplicate_collection": duplicates, + "incomplete": incomplete, + "counts": counts, + "outcomes": outcomes, + } diff --git a/rl_engine/testing/qwen3_next_export_schema.json b/rl_engine/testing/qwen3_next_export_schema.json new file mode 100644 index 000000000..e1389c4f5 --- /dev/null +++ b/rl_engine/testing/qwen3_next_export_schema.json @@ -0,0 +1,10 @@ +{ + "schema_version": 1, + "checkpoint_revision": "9c7f2fbe84465e40164a94cc16cd30b6999b0cc7", + "tensor_count": 75944, + "weight_schema_sha256": "babe9f7e7cf398ad6a8607ee03bb6549139b26fcdd919bb65e9d22b059536361", + "weights_dtype": "bfloat16", + "architecture_layers": 48, + "encoding": "sha256 of sorted compact JSON mapping every HF tensor name to dtype and shape", + "scope": "Official safetensors header schema only; no numerical or full-model L2 acceptance" +} diff --git a/rl_engine/testing/qwen3_next_l2.py b/rl_engine/testing/qwen3_next_l2.py new file mode 100644 index 000000000..7f8778072 --- /dev/null +++ b/rl_engine/testing/qwen3_next_l2.py @@ -0,0 +1,1047 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Frozen full-model Qwen3-Next inputs and original-dtype L2 artifacts. + +Native workers receive only JSON requests (tokens, weights and provider identity). +They write a weights_only-safe torch artifact with metadata, prompt_tokens, +response_tokens, prediction_positions, BF16 logits [B,16,151936], FP32 logprobs +[B,16], and layer_digests for all 48 layers. Recurrent debug states describe the +same prediction positions: P-1 through P+14, before consuming response token 16. +No rollout hidden tensor or recurrent state is a recompute input. +""" + +import hashlib +import json +import os +from dataclasses import dataclass +from pathlib import Path + +import torch + +from rl_engine.integrations.qwen3_next_subnode import SUBNODES, subnode_layout +from rl_engine.testing.tensor_identity import tensor_bitwise_equal + +VOCAB_SIZE = 151936 +RESPONSE_LENGTH = 16 +PROMPT_LENGTHS = (8, 64, 256, 1024) +FROZEN_TEXTS = ( + "The capital of France is Paris. Explain the route from the library to the river. ", + "Compute seven plus five, then describe a careful way to check the answer. ", + "A robot observes a red cup beside a blue plate on a wooden kitchen table. ", + "Write a precise explanation of how rain reaches a mountain stream in autumn. ", +) +NATIVE_WORKER_MODULE = "rl_engine.integrations.qwen3_next_native_workers" + + +# The native rollout cache budget every certified profile pins (4 GiB per GPU). +NATIVE_KV_CACHE_BYTES = 4 << 30 + + +def json_digest(value): + return hashlib.sha256( + json.dumps(value, sort_keys=True, separators=(",", ":")).encode() + ).hexdigest() + + +def tensor_digest(value): + value = value.detach().cpu().contiguous() + digest = hashlib.sha256(str((str(value.dtype), tuple(value.shape))).encode()) + digest.update(value.reshape(-1).view(torch.uint8).numpy().tobytes()) + return digest.hexdigest() + + +@dataclass(frozen=True) +class L2Case: + prompt_length: int + batch_size: int + prefill: str + + @property + def case_id(self): + return f"p{self.prompt_length:04d}-b{self.batch_size}-{self.prefill}" + + @property + def chunks(self): + if self.prefill == "full": + return [self.prompt_length] + remaining = self.prompt_length - 1 + chunks = [1, min(63, remaining)] + remaining -= chunks[-1] + while remaining: + size = min(64, remaining) + chunks.append(size) + remaining -= size + return chunks + + +def case_matrix(): + return [ + L2Case(length, batch, prefill) + for length in PROMPT_LENGTHS + for batch in (1, 4) + for prefill in ("full", "chunked") + ] + + +def prompt_fixture(tokenizer, tokenizer_identity): + encoded = [tokenizer.encode(text, add_special_tokens=False) for text in FROZEN_TEXTS] + if any( + not row or any(type(token) is not int or not 0 <= token < VOCAB_SIZE for token in row) + for row in encoded + ): + raise ValueError("Pinned tokenizer produced an empty or out-of-range prompt") + prompts = {} + for length in PROMPT_LENGTHS: + prompts[str(length)] = torch.tensor( + [(row * ((length + len(row) - 1) // len(row)))[:length] for row in encoded], + dtype=torch.int64, + ) + if torch.unique(prompts[str(length)], dim=0).shape[0] != 4: + raise ValueError("The four pinned prompt samples must be distinct") + identity = { + "texts": list(FROZEN_TEXTS), + "tokenizer": tokenizer_identity, + "prompt_hashes": {key: tensor_digest(value) for key, value in prompts.items()}, + } + return { + "schema_version": 1, + "prompts": prompts, + "prompt_identity": identity, + "prompt_sha256": json_digest(identity), + } + + +def freeze_responses(prompts, baseline_artifacts, *, baseline_identity, baseline_records): + responses, old_logprobs = {}, {} + for length in PROMPT_LENGTHS: + artifact = baseline_artifacts[str(length)] + response = artifact["response_tokens"] + selected = artifact["logprobs"] + if response.shape != (4, RESPONSE_LENGTH) or response.dtype != torch.int64: + raise ValueError( + "Baseline generation must produce sixteen real tokens for four samples" + ) + if ( + selected.shape != (4, RESPONSE_LENGTH) + or selected.dtype != torch.float32 + or not torch.isfinite(selected).all() + ): + raise ValueError("Baseline logprobs must be finite FP32 [4,16]") + if bool(((response < 0) | (response >= VOCAB_SIZE)).any()): + raise ValueError("Baseline response token is outside the official vocabulary") + responses[str(length)] = response.detach().cpu().clone() + old_logprobs[str(length)] = selected.detach().cpu().clone() + fixture = { + **prompts, + "responses": responses, + "old_logprobs": old_logprobs, + "baseline_identity": baseline_identity, + "baseline_artifacts": baseline_records, + } + fixture["content_sha256"] = fixture_digest(fixture) + return fixture + + +def fixture_digest(fixture): + identity = { + "prompt_sha256": fixture["prompt_sha256"], + "baseline_identity": fixture["baseline_identity"], + "baseline_artifacts": fixture["baseline_artifacts"], + "responses": {key: tensor_digest(value) for key, value in fixture["responses"].items()}, + "old_logprobs": { + key: tensor_digest(value) for key, value in fixture["old_logprobs"].items() + }, + } + return json_digest(identity) + + +def validate_fixture(fixture): + expected_keys = {str(length) for length in PROMPT_LENGTHS} + if fixture.get("schema_version") != 1 or any( + set(fixture[name]) != expected_keys + for name in ("prompts", "responses", "old_logprobs", "baseline_artifacts") + ): + raise ValueError("Incomplete frozen L2 input matrix") + for record in fixture["baseline_artifacts"].values(): + if ( + set(record) != {"path", "sha256"} + or not Path(record["path"]).is_absolute() + or len(record["sha256"]) != 64 + ): + raise ValueError("Baseline generation artifact identity is incomplete") + for length in PROMPT_LENGTHS: + key = str(length) + prompt, response, logprobs = ( + fixture[name][key] for name in ("prompts", "responses", "old_logprobs") + ) + if ( + prompt.shape != (4, length) + or prompt.dtype != torch.int64 + or tensor_digest(prompt) != fixture["prompt_identity"]["prompt_hashes"][key] + ): + raise ValueError("Frozen prompt was changed") + if ( + response.shape != (4, RESPONSE_LENGTH) + or response.dtype != torch.int64 + or bool(((response < 0) | (response >= VOCAB_SIZE)).any()) + ): + raise ValueError("Invalid frozen response") + if ( + logprobs.shape != (4, RESPONSE_LENGTH) + or logprobs.dtype != torch.float32 + or not torch.isfinite(logprobs).all() + ): + raise ValueError("Invalid frozen baseline logprobs") + if ( + json_digest(fixture["prompt_identity"]) != fixture["prompt_sha256"] + or fixture_digest(fixture) != fixture["content_sha256"] + ): + raise ValueError("Frozen L2 fixture content identity mismatch") + + +def save_tensor_artifact(path, payload): + path = Path(path) + path.parent.mkdir(parents=True, exist_ok=True) + temporary = path.with_suffix(path.suffix + ".partial") + with temporary.open("wb") as stream: + torch.save(payload, stream) + stream.flush() + os.fsync(stream.fileno()) + temporary.replace(path) + directory = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY) + try: + os.fsync(directory) + finally: + os.close(directory) + + +def load_tensor_artifact(path): + return torch.load(path, map_location="cpu", weights_only=True) + + +def validate_weight_identity(identity): + """Bind mutable export bytes separately from the fixed official model identity. + + This is the JSON boundary check. The native loaders and profile activation also + call validate_training_export to rehash and inspect the actual exported files. + """ + if identity.get("provider_config", {}).get("profile") != "qwen3_next_tp4_shared_eager_v1": + return + version = identity.get("weight_version") + if not isinstance(version, str) or not version: + raise ValueError("Official shared model identity requires an explicit weight version") + actual = identity.get("checkpoint_identity", {}) + if version == "baseline": + if actual.get("kind") == "qwen3_next_native_hf_export": + raise ValueError("An updated native export cannot masquerade as the official baseline") + if any(key in identity for key in ("base_checkpoint", "base_checkpoint_identity")) and any( + ( + identity.get("base_checkpoint") != identity.get("checkpoint"), + identity.get("base_checkpoint_identity") != actual, + ) + ): + raise ValueError("Baseline identity cannot redirect its official base checkpoint") + return + if any(key not in identity for key in ("base_checkpoint", "base_checkpoint_identity")): + raise ValueError("Updated weights require a separately pinned official base identity") + if any( + ( + not Path(identity["checkpoint"]).is_absolute(), + not Path(identity["base_checkpoint"]).is_absolute(), + identity["checkpoint"] == identity["base_checkpoint"], + actual.get("schema_version") != 1, + actual.get("kind") != "qwen3_next_native_hf_export", + actual.get("status") != "complete", + actual.get("weight_version") != version, + actual.get("base_checkpoint") != identity["base_checkpoint"], + actual.get("base_checkpoint_identity") != identity["base_checkpoint_identity"], + actual.get("architecture_layers") != 48, + actual.get("weights_dtype") != "bfloat16", + type(actual.get("step")) is not int, + actual.get("step", 0) < 1, + ) + ): + raise ValueError("Updated checkpoint identity differs from its published native export") + training = actual.get("training_identity", {}) + fields = ("source_commit", "source_tree", "environment_sha256", "provider_config", "topology") + if any(training.get(key) != identity.get(key) for key in fields) or any( + ( + training.get("checkpoint") != identity["base_checkpoint"], + training.get("checkpoint_identity") != identity["base_checkpoint_identity"], + ) + ): + raise ValueError("Native export training provenance differs from the model/base identity") + + +def worker_request(case, fixture, *, role, identity, generate=False, trace_layers=None): + if role not in ("rollout", "recompute"): + raise ValueError("Unknown native model role") + validate_weight_identity(identity) + if generate and (role != "rollout" or case.batch_size != 4 or case.prefill != "full"): + raise ValueError("Only initial B4 full-prefill rollout may define frozen responses") + if not generate: + validate_fixture(fixture) + prompt = fixture["prompts"][str(case.prompt_length)][: case.batch_size] + request = { + "schema_version": 1, + "case_id": case.case_id, + "role": role, + "identity": identity, + "sample_ids": list(range(case.batch_size)), + "prompt_tokens": prompt.tolist(), + "response_tokens": ( + None + if generate + else fixture["responses"][str(case.prompt_length)][: case.batch_size].tolist() + ), + "response_length": RESPONSE_LENGTH, + "prediction_positions": list( + range(case.prompt_length - 1, case.prompt_length + RESPONSE_LENGTH - 1) + ), + "consumed_response_tokens": RESPONSE_LENGTH - 1, + "rollout_prefill_chunks": case.chunks, + "prefill_chunks": case.chunks if role == "rollout" else None, + "consumed_input_tokens": ( + None + if generate + else [ + left + right[:-1] + for left, right in zip( + prompt.tolist(), + fixture["responses"][str(case.prompt_length)][: case.batch_size].tolist(), + ) + ] + ), + "state_position": case.prompt_length + RESPONSE_LENGTH - 2, + "execution": ( + { + "rollout_mode": "autoregressive", + "prefill_chunks": case.chunks, + "response_mode": "generate" if generate else "teacher_forced", + } + if role == "rollout" + else { + "recompute_mode": "full_from_tokens", + "input_sequence_length": case.prompt_length + RESPONSE_LENGTH - 1, + } + ), + "fixture_sha256": fixture["prompt_sha256"] if generate else fixture["content_sha256"], + "logits_dtype": "bfloat16", + "logprobs_dtype": "float32", + "vocab_size": VOCAB_SIZE, + "trace_layers": list(trace_layers or []), + "required_layer_digests": list(range(48)), + "generation": { + "temperature": 0.0, + "top_p": 1.0, + "top_k": -1, + "seed": 1234, + "ignore_eos": True, + }, + } + request["request_sha256"] = json_digest(request) + return request + + +def layer_state_names(layer): + kinds = ("convolution", "recurrent_state") if (layer + 1) % 4 else ("key", "value") + return sorted(f"tp{rank}.{kind}" for rank in range(4) for kind in kinds) + + +def layer_digest(layer, output, states, *, request=None, subnodes=None): + """Hash each sample's real layer outputs, sub-nodes and all TP-owned state tensors.""" + if ( + layer not in range(48) + or output.ndim != 3 + or output.shape[1:] != (16, 2048) + or output.dtype != torch.bfloat16 + ): + raise ValueError( + "Layer diagnostics require original BF16 response hidden states [B,16,2048]" + ) + batch = output.shape[0] + if sorted(states) != layer_state_names(layer) or any( + not isinstance(value, torch.Tensor) or value.shape[0] != batch or not value.numel() + for value in states.values() + ): + raise ValueError("Actual convolution/recurrent or KV state tensors are required") + shapes = ( + native_state_shapes(request) + if request is not None + and request["identity"].get("provider_config", {}).get("profile") + == "qwen3_next_tp4_shared_eager_v1" + else None + ) + for name, value in states.items(): + if shapes is not None: + validate_native_state_storage(value, name.rsplit(".", 1)[-1], shapes) + if "recurrent_state" in name and value.dtype != torch.float32: + raise ValueError("Recurrent diagnostic state must preserve FP32") + if not torch.isfinite(value).all(): + raise ValueError("Nonfinite recurrent/KV diagnostic state") + if not torch.isfinite(output).all(): + raise ValueError("Nonfinite layer diagnostic output") + if subnodes is None or tuple(subnodes) != SUBNODES: + raise ValueError(f"Layer diagnostics require the sub-nodes {SUBNODES} in order") + for name, value in subnodes.items(): + width, dtype = subnode_layout(name) + if value.dtype != dtype or tuple(value.shape) != (batch, 16, width): + raise ValueError(f"Sub-node {name} must be original {dtype} [B,16,{width}]") + subnode_hashes = { + name: [tensor_digest(value[index]) for index in range(batch)] + for name, value in subnodes.items() + } + outputs = [tensor_digest(output[index]) for index in range(batch)] + state_hashes = [ + json_digest({name: tensor_digest(value[index]) for name, value in sorted(states.items())}) + for index in range(batch) + ] + return { + "layer": layer, + "sample_output_sha256": outputs, + "sample_state_sha256": state_hashes, + "output_sha256": json_digest({"layer": layer, "samples": outputs}), + "state_sha256": json_digest({"layer": layer, "samples": state_hashes}), + "state_names": sorted(states), + "sample_subnode_sha256": subnode_hashes, + "subnode_sha256": json_digest({"layer": layer, "subnodes": subnode_hashes}), + } + + +def embedding_digest(values): + """Per-sample digests of the [B, 16, 2048] BF16 embedding rows at the response positions.""" + if values.dtype != torch.bfloat16 or values.ndim != 3 or values.shape[1:] != (16, 2048): + raise ValueError("Embedding diagnostics require original BF16 [B,16,2048] rows") + samples = [tensor_digest(values[index]) for index in range(values.shape[0])] + return {"sample_sha256": samples, "sha256": json_digest({"embedding": samples})} + + +def first_divergent_node(reference, candidate): + """The first (layer, sub-node) whose sample digests differ, in execution order. + + Within a layer the sub-nodes are compared in SUBNODES order, then the layer + output and the recurrent/KV state. Returns ``{"layer", "node"}`` or None. + """ + batch = candidate["logits"].shape[0] + left, right = reference["embedding_digest"], candidate["embedding_digest"] + if left["sample_sha256"][:batch] != right["sample_sha256"]: + return {"layer": 0, "node": "embedding"} + for left, right in zip(reference["layer_digests"], candidate["layer_digests"]): + for node in SUBNODES: + if left["sample_subnode_sha256"][node][:batch] != right["sample_subnode_sha256"][node]: + return {"layer": right["layer"], "node": node} + for node, name in (("output", "sample_output_sha256"), ("state", "sample_state_sha256")): + if left[name][:batch] != right[name]: + return {"layer": right["layer"], "node": node} + return None + + +def first_divergent_layer(reference, candidate): + batch = candidate["logits"].shape[0] + for left, right in zip(reference["layer_digests"], candidate["layer_digests"]): + for name in ("sample_output_sha256", "sample_state_sha256"): + if left[name][:batch] != right[name]: + return right["layer"] + return None + + +def validate_model_artifact(artifact, request): + validate_weight_identity(request["identity"]) + batch = len(request["prompt_tokens"]) + metadata = artifact.get("metadata", {}) + if metadata != { + key: request[key] + for key in ("schema_version", "case_id", "role", "identity", "request_sha256") + }: + raise ValueError("Model artifact provenance differs from its independent worker request") + if artifact.get("execution") != request["execution"]: + raise ValueError("Artifact execution mode does not match its rollout/recompute contract") + profile = request["identity"].get("provider_config", {}) + if profile.get("profile") == "qwen3_next_tp4_shared_eager_v1" and request["role"] == "rollout": + validate_native_scheduler_execution(artifact, request) + expected = { + "logits": ((batch, RESPONSE_LENGTH, VOCAB_SIZE), torch.bfloat16), + "logprobs": ((batch, RESPONSE_LENGTH), torch.float32), + "response_tokens": ((batch, RESPONSE_LENGTH), torch.int64), + "prompt_tokens": ((batch, len(request["prompt_tokens"][0])), torch.int64), + "prediction_positions": ((RESPONSE_LENGTH,), torch.int64), + } + for name, (shape, dtype) in expected.items(): + tensor = artifact.get(name) + if ( + not isinstance(tensor, torch.Tensor) + or tensor.device.type != "cpu" + or tensor.dtype != dtype + or tuple(tensor.shape) != shape + ): + raise ValueError(f"{name}: expected CPU {dtype} {shape}, preserving original storage") + if tensor.is_floating_point() and not torch.isfinite(tensor).all(): + raise ValueError(f"{name}: nonfinite full-model artifact") + if not torch.equal( + artifact["prompt_tokens"], torch.tensor(request["prompt_tokens"], dtype=torch.int64) + ) or not torch.equal( + artifact["prediction_positions"], + torch.tensor(request["prediction_positions"], dtype=torch.int64), + ): + raise ValueError("Prompt or response prediction offsets do not match") + if request["response_tokens"] is not None and not torch.equal( + artifact["response_tokens"], torch.tensor(request["response_tokens"], dtype=torch.int64) + ): + raise ValueError("Worker changed frozen response tokens") + if bool( + ((artifact["response_tokens"] < 0) | (artifact["response_tokens"] >= VOCAB_SIZE)).any() + ): + raise ValueError("Generated token is outside the official vocabulary") + embedding = artifact.get("embedding_digest", {}) + samples = embedding.get("sample_sha256", []) + if len(samples) != batch or embedding.get("sha256") != json_digest({"embedding": samples}): + raise ValueError("The artifact must identify every sample's embedding rows") + layers = artifact.get("layer_digests", []) + if [layer.get("layer") for layer in layers] != list(range(48)): + raise ValueError("Full 48-layer diagnostic identities are required") + for layer in layers: + if layer.get("state_names") != layer_state_names(layer["layer"]): + raise ValueError("Per-layer state identities must cover every TP rank") + for name in ("output", "state"): + values = layer.get(f"sample_{name}_sha256", []) + if len(values) != batch: + raise ValueError("Every layer must identify every sample's output and state") + if any( + not isinstance(value, str) + or len(value) != 64 + or any(char not in "0123456789abcdef" for char in value) + for value in values + ): + raise ValueError("Invalid per-sample layer tensor digest") + if layer.get(f"{name}_sha256") != json_digest( + {"layer": layer["layer"], "samples": values} + ): + raise ValueError("Layer digest is not bound to its sample tensor identities") + subnodes = layer.get("sample_subnode_sha256", {}) + if tuple(subnodes) != SUBNODES or any( + len(values) != batch or any(not isinstance(v, str) or len(v) != 64 for v in values) + for values in subnodes.values() + ): + raise ValueError("Every layer must identify every sample's sub-node outputs") + if layer.get("subnode_sha256") != json_digest( + {"layer": layer["layer"], "subnodes": subnodes} + ): + raise ValueError("Layer sub-node digest is not bound to its sample identities") + for name, value in artifact.get("debug_tensors", {}).items(): + if ( + not isinstance(value, torch.Tensor) + or value.device.type != "cpu" + or not torch.isfinite(value).all() + ): + raise ValueError("Debug layer/state tensors must be finite CPU tensors") + if "recurrent_state" in name and value.dtype != torch.float32: + raise ValueError("Recurrent state diagnostics must preserve FP32 storage") + + +def validate_debug_capture(artifact, request): + tensors = artifact.get("debug_tensors", {}) + for layer in request["trace_layers"]: + record = artifact["layer_digests"][layer] + prefix = f"layer.{layer}." + names = [prefix + "output"] + [prefix + name for name in record["state_names"]] + names += [f"{prefix}subnode.{name}" for name in SUBNODES] + if any(name not in tensors for name in names): + raise ValueError("First-divergence capture is missing layer output or state tensors") + actual = layer_digest( + layer, + tensors[prefix + "output"], + {name: tensors[prefix + name] for name in record["state_names"]}, + request=request, + subnodes={name: tensors[f"{prefix}subnode.{name}"] for name in SUBNODES}, + ) + if actual != record: + raise ValueError("Saved first-divergence tensors differ from their layer identities") + + +def validate_native_scheduler_execution(artifact, request): + expected = { + "kv_cache_memory_bytes_per_gpu": NATIVE_KV_CACHE_BYTES, + "max_model_len": 1040, + "max_num_seqs": 64, + "max_num_batched_tokens": 16384, + "scheduler": "rl_engine.integrations.qwen3_next_native_rollout.ExactPrefillScheduler", + "async_scheduling": False, + "prefix_caching": False, + "weights_dtype": "bfloat16", + "recurrent_state_dtype": "float32", + "model_runner": "v1", + } + if artifact.get("native_runtime_profile") != expected: + raise ValueError("Native rollout runtime differs from its fixed scheduler/cache profile") + mode = ( + "certified" if "scheduler_profile_certificate" in request["identity"] else "qualification" + ) + if artifact.get("provider_execution_mode") != mode: + raise ValueError("Native rollout execution mode differs from its certificate identity") + if ( + request["identity"]["provider_config"].get("native_rollout_kv_cache_memory_bytes") + != NATIVE_KV_CACHE_BYTES + ): + raise ValueError("The submitted profile did not pin the native cache budget") + sizes = [*request["prefill_chunks"], *([1] * request["consumed_response_tokens"])] + steps = artifact.get("native_scheduler_steps", []) + if len(steps) != len(sizes): + raise ValueError("Native scheduler did not execute every requested prefill/decode boundary") + samples = request["sample_ids"] + consumed = 0 + for step, size in zip(steps, sizes): + if set(step) != {"samples", "computed_before", "token_counts"} or any( + ( + sorted(step["samples"]) != sorted(samples), + len(set(step["samples"])) != len(samples), + step["computed_before"] != [consumed] * len(samples), + step["token_counts"] != [size] * len(samples), + ) + ): + raise ValueError("Native scheduler executed a different sample/chunk/offset matrix") + consumed += size + + +def compare_artifacts(rollout, recompute): + comparisons = { + name: tensor_bitwise_equal(rollout[name], recompute[name]) + for name in ("response_tokens", "logits", "logprobs") + } + first_layer = first_divergent_layer(rollout, recompute) + first_node = first_divergent_node(rollout, recompute) + comparisons["layer_states"] = first_node is None + first_difference = None + for name, passed in comparisons.items(): + if passed: + continue + if name == "layer_states": + break + left, right = rollout[name], recompute[name] + if left.shape == right.shape and left.dtype == right.dtype: + width = left.element_size() + changed = ( + left.contiguous().reshape(-1).view(torch.uint8) + != right.contiguous().reshape(-1).view(torch.uint8) + ).nonzero() + if changed.numel(): + flat = int(changed[0, 0]) // width + first_difference = { + "tensor": name, + "flat_element": flat, + "byte_offset": int(changed[0, 0]), + } + break + return { + "passed": all(comparisons.values()), + "comparisons": comparisons, + "first_divergent_layer": first_layer, + "first_divergent_node": first_node, + "first_difference": first_difference, + } + + +def native_state_shapes(request): + """Complete canonical per-rank state after P prompt plus 15 response inputs. + + TP4GDN owns 2048 convolution channels with three prior samples and eight + 128x128 recurrent heads. Full attention owns one 256-wide KV head per rank; + the paired replicas remain independently recorded and compared. + """ + prompts = request["prompt_tokens"] + batch = len(prompts) + if batch not in (1, 4) or any(len(row) != len(prompts[0]) for row in prompts): + raise ValueError("Native state evidence requires the fixed equal-length batch1/4 matrix") + prompt_length = len(prompts[0]) + consumed = prompt_length + RESPONSE_LENGTH - 1 + if ( + prompt_length not in PROMPT_LENGTHS + or request.get("consumed_response_tokens") != RESPONSE_LENGTH - 1 + or request.get("state_position") != consumed - 1 + ): + raise ValueError("Native state position must consume the prompt and exactly 15 responses") + return { + "convolution": (batch, 2048, 3), + "recurrent_state": (batch, 8, 128, 128), + "key": (batch, 1, consumed, 256), + "value": (batch, 1, consumed, 256), + } + + +def validate_native_state_storage(value, name, expected_shapes): + """One dtype/shape/finite boundary shared by assembly and stored trace audits.""" + if not isinstance(value, torch.Tensor) or tuple(value.shape) != expected_shapes[name]: + raise ValueError( + f"Original worker must retain complete {name} state shape " + f"{expected_shapes[name]} after the consumed prompt/response prefix" + ) + dtype = torch.float32 if name == "recurrent_state" else torch.bfloat16 + if value.dtype != dtype or not torch.isfinite(value).all(): + raise ValueError("Original worker state violates BF16/FP32 finite storage") + + +def validate_native_worker_traces(artifact, request, *, artifact_root=None): + """Rehash four actual traces and reconstruct every layer and state digest.""" + profile = request["identity"].get("provider_config", {}).get("profile") + if profile != "qwen3_next_tp4_shared_eager_v1": + return + records = artifact.get("worker_traces", []) + if len(records) != 4 or sorted(record.get("rank", -1) for record in records) != [0, 1, 2, 3]: + raise ValueError("Native model needs one original trace from each TP rank") + binary = artifact.get("provider_binary", {}) + digest = binary.get("sha256", "") + if ( + binary.get("module") != "rl_engine._C" + or not isinstance(binary.get("path"), str) + or not Path(binary["path"]).is_absolute() + or not isinstance(digest, str) + or len(digest) != 64 + or any(character not in "0123456789abcdef" for character in digest) + ): + raise ValueError("Native model must identify the actual imported CUDA provider binary") + batch = len(request["sample_ids"]) + expected_state_shapes = native_state_shapes(request) + state_hashes = [[{} for _ in range(batch)] for _ in range(48)] + for record in records: + rank = record["rank"] + if record.get("request_sha256") != request["request_sha256"]: + raise ValueError("Worker trace request identity mismatch") + trace = load_tensor_artifact(checked_artifact_path(record, artifact_root=artifact_root)) + provenance = { + "rank": rank, + "request_sha256": request["request_sha256"], + "provider_binary": binary, + } + if request["role"] == "rollout": + provenance.update( + native_runtime_profile=artifact["native_runtime_profile"], + provider_execution_mode=artifact["provider_execution_mode"], + schedule=artifact["native_scheduler_steps"], + ) + else: + provenance.update(role="recompute", execution=request["execution"]) + if any(trace.get(key) != value for key, value in provenance.items()): + raise ValueError("Worker trace rank/runtime/scheduler provenance mismatch") + tensors = { + "logprobs": artifact["logprobs"], + "responses": artifact["response_tokens"], + "local_logits": artifact["logits"][..., rank * 37984 : (rank + 1) * 37984], + } + # Recompute keeps the gathered full logits on rank zero, and every rank + # retains its original local shard and independently selected logprobs. + if request["role"] == "rollout" or rank == 0: + tensors["logits"] = artifact["logits"] + for key, expected in tensors.items(): + if not isinstance(trace.get(key), torch.Tensor) or not tensor_bitwise_equal( + trace[key], expected + ): + raise ValueError(f"Original worker {key} bytes contradict the assembled artifact") + if request["role"] == "recompute": + layers = trace.get("layers", {}) + if not isinstance(layers, dict) or set(layers) != {*range(48), "model"}: + raise ValueError("Original recompute worker must retain all 48 layer traces") + outputs = [layers[layer].get("output") for layer in range(48)] + if any( + not isinstance(value, torch.Tensor) + or value.shape != (batch, 16, 2048) + or value.dtype != torch.bfloat16 + for value in outputs + ): + raise ValueError("Original recompute worker must retain all 48 BF16 layer outputs") + hidden = torch.stack(outputs) + states = [layers[layer].get("states", {}) for layer in range(48)] + subnodes = { + name: torch.stack([layers[layer]["subnodes"][name] for layer in range(48)]) + for name in SUBNODES + } + embedding = layers["model"]["embedding"] + else: + hidden = trace.get("hidden") + states = trace.get("states", []) + subnodes, embedding = trace.get("subnodes", {}), trace.get("embedding") + if embedding_digest(embedding) != artifact["embedding_digest"]: + raise ValueError("Original worker embedding rows contradict the assembled artifact") + for name in SUBNODES: + for layer in range(48): + if [ + tensor_digest(subnodes[name][layer, sample]) for sample in range(batch) + ] != artifact["layer_digests"][layer]["sample_subnode_sha256"][name]: + raise ValueError( + f"Original worker {name} rows contradict the assembled artifact" + ) + if ( + not isinstance(hidden, torch.Tensor) + or hidden.shape != (48, batch, 16, 2048) + or hidden.dtype != torch.bfloat16 + ): + raise ValueError("Original worker must retain all 48 BF16 layer outputs") + if not torch.isfinite(hidden).all() or len(states) != 48: + raise ValueError("Original worker has nonfinite outputs or missing layer states") + for layer in range(48): + expected_names = ( + ("key", "value") if (layer + 1) % 4 == 0 else ("convolution", "recurrent_state") + ) + if set(states[layer]) != set(expected_names): + raise ValueError("Worker trace state types differ from the official layer layout") + for sample in range(batch): + if ( + tensor_digest(hidden[layer, sample]) + != artifact["layer_digests"][layer]["sample_output_sha256"][sample] + ): + raise ValueError("Original worker layer output contradicts its declared digest") + for name, value in states[layer].items(): + validate_native_state_storage(value, name, expected_state_shapes) + for sample in range(batch): + state_hashes[layer][sample][f"tp{rank}.{name}"] = tensor_digest(value[sample]) + for layer in range(48): + if [json_digest(values) for values in state_hashes[layer]] != artifact["layer_digests"][ + layer + ]["sample_state_sha256"]: + raise ValueError("Original TP state bytes contradict the assembled layer digests") + + +def compare_batch_prefill(reference, candidate): + batch = candidate["logits"].shape[0] + compared = { + name: tensor_bitwise_equal(reference[name][:batch], candidate[name]) + for name in ("response_tokens", "logits", "logprobs") + } + compared["layer_states"] = first_divergent_node(reference, candidate) is None + return compared + + +def validate_matrix_report(report, *, expected_identity=None, artifact_root=None): + """A passed label alone cannot close the full-model matrix.""" + expected_ids = [case.case_id for case in case_matrix()] + required = { + "schema_version": 1, + "status": "passed", + "scope": "qwen3_next_full_model_l2", + "raw_bit_comparison": True, + "logits_dtype": "bfloat16", + "logprobs_dtype": "float32", + "response_length": 16, + "vocab_size": 151936, + "case_ids": expected_ids, + } + if any(report.get(key) != value for key, value in required.items()) or set( + report.get("cases", {}) + ) != set(expected_ids): + raise ValueError("Full-model L2 requires the complete sixteen-case raw-bit matrix") + identity = report.get("identity", {}) + if identity.get("architecture_layers") != 48 or identity.get("topology") != { + "tp": 4, + "pp": 1, + "cp": 1, + "ep": 1, + }: + raise ValueError("Full-model L2 cannot change model depth or topology") + processes = report.get("processes", {}) + if ( + set(processes) != {"rollout", "recompute"} + or any( + row.get("exit_code") != 0 + or row.get("gpu_release_verified") is not True + or type(row.get("pid")) is not int + or row["pid"] <= 0 + for row in processes.values() + ) + or processes["rollout"]["pid"] == processes["recompute"]["pid"] + ): + raise ValueError("Independent process completion and GPU release evidence are required") + checks = {"response_tokens", "logits", "logprobs", "layer_states"} + for case in report["cases"].values(): + compared = case.get("comparisons", {}) + if ( + case.get("passed") is not True + or set(compared) != checks + or any(value is not True for value in compared.values()) + ): + raise ValueError( + "Every response token, full logits, logprob and layer/state identity must pass" + ) + groups = [case.get("batch_prefill", {})] + if identity.get("weight_version") == "baseline": + groups.append(case.get("baseline_generation", {})) + for group in groups: + if set(group) != {"rollout", "recompute"} or any( + set(row) != checks or any(value is not True for value in row.values()) + for row in group.values() + ): + raise ValueError( + "Batch, chunk and actual baseline generation comparisons are mandatory" + ) + if set(case.get("artifacts", {})) != {"rollout", "recompute"}: + raise ValueError("Both independent full-vocabulary tensor artifacts are required") + _validate_report_artifacts( + report, + roles=("rollout", "recompute"), + expected_identity=expected_identity, + artifact_root=artifact_root, + ) + + +def checked_artifact_path(record, *, artifact_root=None): + """Rehash bytes before parsing; artifact paths cannot escape this attempt.""" + if not isinstance(record, dict) or not {"path", "sha256"} <= record.keys(): + raise ValueError("Tensor evidence requires a path and SHA256") + path = Path(record["path"]) + if not path.is_absolute(): + raise ValueError("Tensor artifact path must be absolute") + path = path.resolve(strict=True) + if artifact_root is not None and not path.is_relative_to(Path(artifact_root).resolve()): + raise ValueError("Tensor artifact escaped its current attempt") + with path.open("rb") as stream: + digest = hashlib.file_digest(stream, "sha256").hexdigest() + if digest != record["sha256"]: + raise ValueError("Tensor artifact content digest changed") + return path + + +def _validate_report_artifacts(report, *, roles, expected_identity, artifact_root): + identity = report["identity"] + if expected_identity is not None and identity != expected_identity: + raise ValueError("Model matrix source/environment/provider identity mismatch") + from rl_engine.testing.qwen3_next_binary import validate_binary_record + + binary = validate_binary_record( + report.get("provider_binary"), + source_tree=identity.get("source_tree"), + artifact_root=artifact_root, + ) + # Inputs may be the explicit frozen baseline shared by subsequent training + # runs. Their bytes are pinned separately; outputs must be in this attempt. + fixture = load_tensor_artifact(checked_artifact_path(report.get("fixture"))) + validate_fixture(fixture) + if fixture["content_sha256"] != report["fixture"].get("content_sha256"): + raise ValueError("Matrix frozen-input content identity mismatch") + baselines = {} + for length, record in fixture["baseline_artifacts"].items(): + baseline = load_tensor_artifact(checked_artifact_path(record)) + original_identity = baseline.get("metadata", {}).get("identity", {}) + if json_digest(original_identity) != fixture["baseline_identity"]: + raise ValueError("Baseline generation provenance changed") + if identity.get("weight_version") == "baseline" and original_identity != identity: + raise ValueError("Baseline weights/provider differ from the matrix") + baseline_request = worker_request( + L2Case(int(length), 4, "full"), + fixture, + role="rollout", + identity=original_identity, + generate=True, + ) + validate_model_artifact(baseline, baseline_request) + validate_native_worker_traces(baseline, baseline_request) + baselines[length] = baseline + worker_records = report.get("worker_results", {}) + if set(worker_records) != set(roles): + raise ValueError("Independent worker result artifacts are missing") + workers = {} + for role in roles: + worker = json.loads( + checked_artifact_path( + worker_records[role], + artifact_root=artifact_root, + ).read_text() + ) + if any( + worker.get(key) != value + for key, value in { + "status": "passed", + "role": role, + "identity": identity, + "fixture_sha256": fixture["content_sha256"], + "pid": report["processes"][role]["pid"], + "provider_binary": binary, + }.items() + ) or set(worker.get("cases", {})) != {case.case_id for case in case_matrix()}: + raise ValueError("Independent worker provenance or case inventory mismatch") + workers[role] = worker + for case in case_matrix(): + row = report["cases"][case.case_id] + outputs = {} + for role in roles: + record = row["artifacts"][role] + path = checked_artifact_path(record, artifact_root=artifact_root) + request = worker_request(case, fixture, role=role, identity=identity) + worker_record = workers[role]["cases"][case.case_id] + if worker_record != {**record, "request_sha256": request["request_sha256"]}: + raise ValueError("Worker and matrix refer to different model output artifacts") + outputs[role] = load_tensor_artifact(path) + if outputs[role].get("provider_binary") != binary: + raise ValueError("Model output used a different native CUDA provider") + validate_model_artifact(outputs[role], request) + validate_native_worker_traces(outputs[role], request, artifact_root=artifact_root) + reference_record = report["cases"][L2Case(case.prompt_length, 4, "full").case_id][ + "artifacts" + ][role] + reference = load_tensor_artifact( + checked_artifact_path(reference_record, artifact_root=artifact_root) + ) + actual = compare_batch_prefill(reference, outputs[role]) + if not all(actual.values()) or actual != row["batch_prefill"][role]: + raise ValueError("Batch/prefill tensor bytes contradict matrix acceptance") + if identity.get("weight_version") == "baseline": + actual = compare_batch_prefill(baselines[str(case.prompt_length)], outputs[role]) + if not all(actual.values()) or actual != row["baseline_generation"][role]: + raise ValueError("Generated baseline tensor bytes contradict matrix acceptance") + if len(roles) == 2: + actual = compare_artifacts(outputs["rollout"], outputs["recompute"]) + if not actual["passed"] or actual["comparisons"] != row["comparisons"]: + raise ValueError("Independent full-model tensor bytes contradict matrix acceptance") + + +def validate_scheduler_report(report, *, expected_identity=None, artifact_root=None): + expected_ids = [case.case_id for case in case_matrix()] + required = { + "schema_version": 1, + "status": "passed", + "raw_bit_comparison": True, + "scope": "qwen3_next_native_scheduler_only", + "case_ids": expected_ids, + "full_model_l2": "not_evaluated", + } + if any(report.get(key) != value for key, value in required.items()): + raise ValueError("Actual scheduler requires its complete native sixteen-case matrix") + if set(report.get("cases", {})) != set(expected_ids): + raise ValueError("Scheduler matrix omitted mandatory cases") + identity = report.get("identity", {}) + if identity.get("architecture_layers") != 48 or identity.get("topology") != { + "tp": 4, + "pp": 1, + "cp": 1, + "ep": 1, + }: + raise ValueError("Scheduler gate must execute the complete TP4 model") + processes = report.get("processes", {}) + process = processes.get("rollout", {}) + if set(processes) != {"rollout"} or any( + ( + process.get("exit_code") != 0, + process.get("gpu_release_verified") is not True, + type(process.get("pid")) is not int, + process.get("pid", 0) <= 0, + ) + ): + raise ValueError("Scheduler process completion and memory release evidence missing") + checks = {"response_tokens", "logits", "logprobs", "layer_states"} + for row in report["cases"].values(): + if row.get("passed") is not True or set(row.get("artifacts", {})) != {"rollout"}: + raise ValueError("Scheduler case failed or has no native tensor artifact") + for field in ("batch_prefill", "baseline_generation"): + group = row.get(field, {}) + values = group.get("rollout", {}) + if any( + ( + set(group) != {"rollout"}, + set(values) != checks, + not all(value is True for value in values.values()), + ) + ): + raise ValueError("Scheduler batch/prefill/raw-bit comparisons are incomplete") + _validate_report_artifacts( + report, + roles=("rollout",), + expected_identity=expected_identity, + artifact_root=artifact_root, + ) diff --git a/rl_engine/testing/qwen3_next_layer_cases.json b/rl_engine/testing/qwen3_next_layer_cases.json new file mode 100644 index 000000000..7610bd7c4 --- /dev/null +++ b/rl_engine/testing/qwen3_next_layer_cases.json @@ -0,0 +1,122 @@ +{ + "schema_version": 1, + "gates": { + "gdn": { + "script": "scripts/qwen3_next_tp_gdn_check.py", + "output_option": "--output", + "scope": "tp4_gdn_layer0", + "groups": { + "cases": [ + "full-chunk-8", + "full-chunk-64", + "full-chunk-256", + "full-chunk-1024", + "prompt-gradient", + "replicated-norm-gradient", + "hf-load-export" + ] + } + }, + "attention": { + "script": "scripts/qwen3_next_tp_attention_check.py", + "output_option": "--output", + "scope": "tp4_full_attention_layer3", + "groups": { + "attention.cases": [ + "hf-load-export", + "full-chunk-8", + "full-chunk-64", + "full-chunk-256", + "full-chunk-1024", + "batch4-varlen-reorder", + "sixteen-token-decode", + "prompt-gradient", + "kv-pair-gradient", + "qk-norm-gradient" + ] + } + }, + "moe": { + "script": "scripts/qwen3_next_tp_moe_check.py", + "output_option": "--output", + "scope": "tp4_moe_block_layer0", + "groups": { + "moe.cases": [ + "hf-load-export-all512experts", + "routes-replicated-8", + "full-chunk-reorder-8", + "routes-replicated-64", + "full-chunk-reorder-64", + "routes-replicated-256", + "full-chunk-reorder-256", + "routes-replicated-1024", + "full-chunk-reorder-1024", + "training-forward", + "all-parameter-gradient", + "replicated-router-gradient" + ] + } + }, + "adapters": { + "script": "scripts/qwen3_next_adapter_check.py", + "output_option": "--out", + "scope": "gdn_engine_adapter_component", + "groups": { + "cases": [ + "batch1-length8", + "batch1-length64", + "batch1-length256", + "batch1-length1024", + "batch4-length8", + "batch4-length64", + "batch4-length256", + "batch4-length1024", + "mixed-prefill-decode16-reorder" + ] + }, + "fields": { + "prompt_gradient": "passed", + "hf_conv_roundtrip": "passed", + "batch_invariance_capability_registered": false + } + }, + "heads": { + "script": "scripts/qwen3_next_tp_head_check.py", + "output_option": "--output", + "scope": "tp4_embedding_lm_head_only", + "groups": { + "cases": [ + "hf-embedding-roundtrip", + "hf-lm-head-roundtrip", + "replicated-norm-gradient", + "nonzero-finite-embedding-head-gradient", + "embedding-repeat-100", + "batch-invariant-prompt8", + "batch-invariant-prompt64", + "batch-invariant-prompt256", + "batch-invariant-prompt1024", + "logprob-original-and-promoted-logits", + "entropy-interface" + ] + } + } + }, + "scheduler_case_ids": [ + "p0008-b1-full", + "p0008-b1-chunked", + "p0008-b4-full", + "p0008-b4-chunked", + "p0064-b1-full", + "p0064-b1-chunked", + "p0064-b4-full", + "p0064-b4-chunked", + "p0256-b1-full", + "p0256-b1-chunked", + "p0256-b4-full", + "p0256-b4-chunked", + "p1024-b1-full", + "p1024-b1-chunked", + "p1024-b4-full", + "p1024-b4-chunked" + ] +} diff --git a/rl_engine/testing/qwen3_next_operator_cases.json b/rl_engine/testing/qwen3_next_operator_cases.json new file mode 100644 index 000000000..5da204ee9 --- /dev/null +++ b/rl_engine/testing/qwen3_next_operator_cases.json @@ -0,0 +1,293 @@ +{ + "schema_version": 1, + "description": "Mandatory norm/core contracts; alternative-provider approximate comparisons are diagnostics only. Generated by scripts/qwen3_next_operator_cases.py; case changes require review before a frozen run.", + "suites": { + "norm": [ + "tests/check_qwen3_next_norm_providers.py::test_custom_op_has_distinct_native_and_cuda_paths", + "tests/check_qwen3_next_norm_providers.py::test_gdn_decode_provider_env_defaults_are_recorded", + "tests/check_qwen3_next_norm_providers.py::test_our_gated_op_is_batch_invariant", + "tests/check_qwen3_next_norm_providers.py::test_vllm_gated_norm_is_batch_invariant[forward_cuda]", + "tests/check_qwen3_next_norm_providers.py::test_vllm_gated_norm_is_batch_invariant[forward_native]", + "tests/test_qwen3_next_norm.py::test_bad_weight_shape_raises", + "tests/test_qwen3_next_norm.py::test_batch_invariance_across_concurrency[16]", + "tests/test_qwen3_next_norm.py::test_batch_invariance_across_concurrency[1]", + "tests/test_qwen3_next_norm.py::test_batch_invariance_across_concurrency[2]", + "tests/test_qwen3_next_norm.py::test_batch_invariance_across_concurrency[32]", + "tests/test_qwen3_next_norm.py::test_batch_invariance_across_concurrency[48]", + "tests/test_qwen3_next_norm.py::test_batch_invariance_across_concurrency[64]", + "tests/test_qwen3_next_norm.py::test_batch_invariance_across_concurrency[8]", + "tests/test_qwen3_next_norm.py::test_batch_invariance_slice[128]", + "tests/test_qwen3_next_norm.py::test_batch_invariance_slice[2048]", + "tests/test_qwen3_next_norm.py::test_batch_invariance_with_padding", + "tests/test_qwen3_next_norm.py::test_call_matches_forward", + "tests/test_qwen3_next_norm.py::test_chunked_reduction_is_slice_invariant_on_device[128]", + "tests/test_qwen3_next_norm.py::test_chunked_reduction_is_slice_invariant_on_device[2048]", + "tests/test_qwen3_next_norm.py::test_cuda_default_offset_preserves_plain_convention", + "tests/test_qwen3_next_norm.py::test_cuda_gated_backward_matches_autograd_golden", + "tests/test_qwen3_next_norm.py::test_cuda_gated_batch_invariance[16]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_batch_invariance[1]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_batch_invariance[2]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_batch_invariance[32]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_batch_invariance[48]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_batch_invariance[512]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_batch_invariance[64]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_batch_invariance[8]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_dgate_keeps_signed_zero_weight[dtype0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_dgate_keeps_signed_zero_weight[dtype1]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_dgate_keeps_signed_zero_weight[dtype2]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_dweight_is_batch_invariant[1]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_dweight_is_batch_invariant[512]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_dweight_is_batch_invariant[64]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_dweight_is_batch_invariant[8]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_matches_golden_within_contract", + "tests/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[sigmoid-dtype0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[sigmoid-dtype1]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[silu-dtype0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[silu-dtype1]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rejects_bad_activation", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rejects_mismatched_gate", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-128-sigmoid-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-128-sigmoid-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-128-silu-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-128-silu-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-2048-sigmoid-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-2048-sigmoid-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-2048-silu-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-2048-silu-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-5120-sigmoid-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-5120-sigmoid-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-5120-silu-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-5120-silu-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp16-128-sigmoid-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp16-128-sigmoid-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp16-128-silu-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp16-128-silu-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp16-2048-sigmoid-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp16-2048-sigmoid-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp16-2048-silu-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp16-2048-silu-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp16-5120-sigmoid-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp16-5120-sigmoid-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp16-5120-silu-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp16-5120-silu-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp32-128-sigmoid-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp32-128-sigmoid-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp32-128-silu-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp32-128-silu-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp32-2048-sigmoid-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp32-2048-sigmoid-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp32-2048-silu-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp32-2048-silu-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp32-5120-sigmoid-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp32-5120-sigmoid-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp32-5120-silu-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[fp32-5120-silu-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_sigmoid_activation", + "tests/test_qwen3_next_norm.py::test_cuda_gated_sigmoid_backward_uses_the_sigmoid_derivative", + "tests/test_qwen3_next_norm.py::test_cuda_gated_signed_zero_weight_is_preserved[dtype0]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_signed_zero_weight_is_preserved[dtype1]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_signed_zero_weight_is_preserved[dtype2]", + "tests/test_qwen3_next_norm.py::test_cuda_gated_swish_is_an_alias_for_silu", + "tests/test_qwen3_next_norm.py::test_cuda_gated_unit_weight_is_plain_norm_times_silu", + "tests/test_qwen3_next_norm.py::test_cuda_gated_weight_offset_is_applied_in_fp32", + "tests/test_qwen3_next_norm.py::test_cuda_gated_wrapper_rejects_equal_numel_wrong_shape", + "tests/test_qwen3_next_norm.py::test_cuda_gated_zero_gate_zeroes_output", + "tests/test_qwen3_next_norm.py::test_cuda_offset_equals_explicit_fp32_weight", + "tests/test_qwen3_next_norm.py::test_cuda_offset_is_not_folded_through_bfloat16", + "tests/test_qwen3_next_norm.py::test_cuda_parameter_contributions_reproduce_backward[dtype0-shape0-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_parameter_contributions_reproduce_backward[dtype0-shape0-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_parameter_contributions_reproduce_backward[dtype0-shape1-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_parameter_contributions_reproduce_backward[dtype0-shape1-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_parameter_contributions_reproduce_backward[dtype1-shape0-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_parameter_contributions_reproduce_backward[dtype1-shape0-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_parameter_contributions_reproduce_backward[dtype1-shape1-0.0]", + "tests/test_qwen3_next_norm.py::test_cuda_parameter_contributions_reproduce_backward[dtype1-shape1-1.0]", + "tests/test_qwen3_next_norm.py::test_cuda_plain_signed_zero_is_preserved[dtype0]", + "tests/test_qwen3_next_norm.py::test_cuda_plain_signed_zero_is_preserved[dtype1]", + "tests/test_qwen3_next_norm.py::test_cuda_plain_signed_zero_is_preserved[dtype2]", + "tests/test_qwen3_next_norm.py::test_cuda_zero_centred_backward_is_offset_aware", + "tests/test_qwen3_next_norm.py::test_cuda_zero_centred_batch_invariance[16]", + "tests/test_qwen3_next_norm.py::test_cuda_zero_centred_batch_invariance[1]", + "tests/test_qwen3_next_norm.py::test_cuda_zero_centred_batch_invariance[2]", + "tests/test_qwen3_next_norm.py::test_cuda_zero_centred_batch_invariance[32]", + "tests/test_qwen3_next_norm.py::test_cuda_zero_centred_batch_invariance[48]", + "tests/test_qwen3_next_norm.py::test_cuda_zero_centred_batch_invariance[512]", + "tests/test_qwen3_next_norm.py::test_cuda_zero_centred_batch_invariance[64]", + "tests/test_qwen3_next_norm.py::test_cuda_zero_centred_batch_invariance[8]", + "tests/test_qwen3_next_norm.py::test_cuda_zero_centred_within_tolerance_of_golden", + "tests/test_qwen3_next_norm.py::test_deterministic_repeat", + "tests/test_qwen3_next_norm.py::test_dtype_paths[dtype0]", + "tests/test_qwen3_next_norm.py::test_dtype_paths[dtype1]", + "tests/test_qwen3_next_norm.py::test_dtype_paths[dtype2]", + "tests/test_qwen3_next_norm.py::test_eps_inside_sqrt", + "tests/test_qwen3_next_norm.py::test_gated_batch_invariance_slice", + "tests/test_qwen3_next_norm.py::test_gated_call_matches_forward[Qwen3NextRMSNormGatedHFOp]", + "tests/test_qwen3_next_norm.py::test_gated_call_matches_forward[Qwen3NextRMSNormGatedOp]", + "tests/test_qwen3_next_norm.py::test_gated_conventions_diverge_in_low_precision", + "tests/test_qwen3_next_norm.py::test_gated_cuda_op_rejects_bad_activation_at_construction", + "tests/test_qwen3_next_norm.py::test_gated_extension_handles_empty_batch_and_nondefault_stream", + "tests/test_qwen3_next_norm.py::test_gated_extension_rejects_invalid_tensor_contract[dy_dtype]", + "tests/test_qwen3_next_norm.py::test_gated_extension_rejects_invalid_tensor_contract[gate_dtype]", + "tests/test_qwen3_next_norm.py::test_gated_extension_rejects_invalid_tensor_contract[rstd_dtype]", + "tests/test_qwen3_next_norm.py::test_gated_extension_rejects_invalid_tensor_contract[weight_dtype]", + "tests/test_qwen3_next_norm.py::test_gated_extension_rejects_invalid_tensor_contract[weight_shape]", + "tests/test_qwen3_next_norm.py::test_gated_hf_witness_low_precision[dtype0]", + "tests/test_qwen3_next_norm.py::test_gated_hf_witness_low_precision[dtype1]", + "tests/test_qwen3_next_norm.py::test_gated_hf_witness_matches_hf_formula_fp32", + "tests/test_qwen3_next_norm.py::test_gated_shape_mismatch_raises", + "tests/test_qwen3_next_norm.py::test_gated_strict_low_precision[dtype0]", + "tests/test_qwen3_next_norm.py::test_gated_strict_low_precision[dtype1]", + "tests/test_qwen3_next_norm.py::test_gated_strict_matches_vllm_formula_fp32", + "tests/test_qwen3_next_norm.py::test_matches_upstream_formula_fp32[128]", + "tests/test_qwen3_next_norm.py::test_matches_upstream_formula_fp32[2048]", + "tests/test_qwen3_next_norm.py::test_matches_upstream_formula_low_precision[dtype0]", + "tests/test_qwen3_next_norm.py::test_matches_upstream_formula_low_precision[dtype1]", + "tests/test_qwen3_next_norm.py::test_offset_applied_in_fp32_not_folded_into_bf16", + "tests/test_qwen3_next_norm.py::test_op_batch_invariance_on_device", + "tests/test_qwen3_next_norm.py::test_plain_reference_is_unchanged_by_the_offset_plumbing", + "tests/test_qwen3_next_norm.py::test_weight_offset_is_one", + "tests/test_qwen3_next_norm.py::test_zero_centred_cuda_constructor_rejects_missing_extension", + "tests/test_qwen3_next_norm.py::test_zero_centred_op_inherits_the_plain_reference", + "tests/test_qwen3_next_norm.py::test_zero_weight_is_identity_scaling" + ], + "core": [ + "tests/check_qwen3_next_attention.py::test_d256_backward_with_cached_prefix_matches_float64_formula", + "tests/check_qwen3_next_attention.py::test_d256_full_chunk_decode_and_batch_are_raw_bit_equal[1024]", + "tests/check_qwen3_next_attention.py::test_d256_full_chunk_decode_and_batch_are_raw_bit_equal[256]", + "tests/check_qwen3_next_attention.py::test_d256_full_chunk_decode_and_batch_are_raw_bit_equal[64]", + "tests/check_qwen3_next_attention.py::test_d256_full_chunk_decode_and_batch_are_raw_bit_equal[8]", + "tests/check_qwen3_next_attention.py::test_d256_input_device_guard_and_nondefault_stream", + "tests/check_qwen3_next_attention.py::test_native_backward_rejects_malformed_probability_and_gradient_buffers", + "tests/check_qwen3_next_batch_invariance.py::test_causal_conv1d_update_is_batch_invariant[state_dtype0]", + "tests/check_qwen3_next_batch_invariance.py::test_causal_conv1d_update_is_batch_invariant[state_dtype1]", + "tests/check_qwen3_next_batch_invariance.py::test_chunk_metadata_partition_is_batch_invariant[0]", + "tests/check_qwen3_next_batch_invariance.py::test_chunk_metadata_partition_is_batch_invariant[1]", + "tests/check_qwen3_next_batch_invariance.py::test_chunk_metadata_partition_is_batch_invariant[2]", + "tests/check_qwen3_next_batch_invariance.py::test_packed_recurrent_decode_is_batch_invariant[state_dtype0]", + "tests/check_qwen3_next_batch_invariance.py::test_packed_recurrent_decode_is_batch_invariant[state_dtype1]", + "tests/check_qwen3_next_batch_invariance.py::test_target_inputs_do_not_depend_on_companions", + "tests/check_qwen3_next_batch_invariance.py::test_triton_chunked_prefill_is_batch_invariant", + "tests/check_qwen3_next_conv_bridge.py::test_chunked_and_reordered_sequences_match", + "tests/check_qwen3_next_conv_bridge.py::test_empty_sequence_preserves_cache_and_gradients", + "tests/check_qwen3_next_conv_bridge.py::test_response_gradient_reaches_prompt_and_convolution_weight", + "tests/check_qwen3_next_conv_bridge.py::test_varlen_matches_repeated_decode_and_preserves_inputs[lengths0]", + "tests/check_qwen3_next_conv_bridge.py::test_varlen_matches_repeated_decode_and_preserves_inputs[lengths1]", + "tests/check_qwen3_next_conv_bridge.py::test_varlen_matches_repeated_decode_and_preserves_inputs[lengths2]", + "tests/check_qwen3_next_core_matrix.py::test_full_chunked_reordered_shared_core[1024-1]", + "tests/check_qwen3_next_core_matrix.py::test_full_chunked_reordered_shared_core[1024-4]", + "tests/check_qwen3_next_core_matrix.py::test_full_chunked_reordered_shared_core[256-1]", + "tests/check_qwen3_next_core_matrix.py::test_full_chunked_reordered_shared_core[256-4]", + "tests/check_qwen3_next_core_matrix.py::test_full_chunked_reordered_shared_core[64-1]", + "tests/check_qwen3_next_core_matrix.py::test_full_chunked_reordered_shared_core[64-4]", + "tests/check_qwen3_next_core_matrix.py::test_full_chunked_reordered_shared_core[8-1]", + "tests/check_qwen3_next_core_matrix.py::test_full_chunked_reordered_shared_core[8-4]", + "tests/check_qwen3_next_core_matrix.py::test_shared_core_one_hundred_repetitions[1]", + "tests/check_qwen3_next_core_matrix.py::test_shared_core_one_hundred_repetitions[4]", + "tests/check_qwen3_next_core_matrix.py::test_variable_lengths_empty_and_mixed_step_match_independent_sequences", + "tests/check_qwen3_next_forward.py::test_combine_has_fixed_route_order_and_differentiable_weights", + "tests/check_qwen3_next_forward.py::test_full_vocabulary_logprob_entropy_modes_and_gradient", + "tests/check_qwen3_next_forward.py::test_grouped_experts_are_bitwise_the_per_expert_gemms[1-128]", + "tests/check_qwen3_next_forward.py::test_grouped_experts_are_bitwise_the_per_expert_gemms[1-512]", + "tests/check_qwen3_next_forward.py::test_grouped_experts_are_bitwise_the_per_expert_gemms[13-128]", + "tests/check_qwen3_next_forward.py::test_grouped_experts_are_bitwise_the_per_expert_gemms[13-512]", + "tests/check_qwen3_next_forward.py::test_grouped_experts_are_bitwise_the_per_expert_gemms[300-128]", + "tests/check_qwen3_next_forward.py::test_grouped_experts_are_bitwise_the_per_expert_gemms[300-512]", + "tests/check_qwen3_next_forward.py::test_grouped_experts_are_bitwise_the_per_expert_gemms[7-128]", + "tests/check_qwen3_next_forward.py::test_grouped_experts_are_bitwise_the_per_expert_gemms[7-512]", + "tests/check_qwen3_next_forward.py::test_grouped_experts_fail_closed_on_the_tensor_descriptor_path", + "tests/check_qwen3_next_forward.py::test_grouped_projection_is_bitwise_the_per_expert_gemm[128]", + "tests/check_qwen3_next_forward.py::test_grouped_projection_is_bitwise_the_per_expert_gemm[512]", + "tests/check_qwen3_next_forward.py::test_logprob_nan_rejected_before_comparison", + "tests/check_qwen3_next_forward.py::test_moe_vjp_matches_independent_double_formula", + "tests/check_qwen3_next_forward.py::test_qwen_linear_shapes_batch_chunk_and_reorder[1024-2048]", + "tests/check_qwen3_next_forward.py::test_qwen_linear_shapes_batch_chunk_and_reorder[128-2048]", + "tests/check_qwen3_next_forward.py::test_qwen_linear_shapes_batch_chunk_and_reorder[2048-16]", + "tests/check_qwen3_next_forward.py::test_qwen_linear_shapes_batch_chunk_and_reorder[2048-256]", + "tests/check_qwen3_next_forward.py::test_qwen_linear_shapes_batch_chunk_and_reorder[2048-3072]", + "tests/check_qwen3_next_forward.py::test_qwen_linear_shapes_batch_chunk_and_reorder[2048-37984]", + "tests/check_qwen3_next_forward.py::test_qwen_moe_actual_tp4_shapes_and_independent_training_forward", + "tests/check_qwen3_next_forward.py::test_route_weights_are_batch_invariant", + "tests/check_qwen3_next_forward.py::test_shared_linear_vjp_and_empty_tokens", + "tests/check_qwen3_next_forward.py::test_stable_route_tie_boundary_and_router_gradient", + "tests/check_qwen3_next_gdn_bridge.py::test_bridge_backward_matches_recomputed_recurrence_vjp", + "tests/check_qwen3_next_gdn_bridge.py::test_bridge_forward_is_provider_exact_and_does_not_mutate_inputs", + "tests/check_qwen3_next_gdn_bridge.py::test_bridge_keeps_prompt_state_gradient_through_response", + "tests/check_qwen3_next_gdn_bridge.py::test_inactive_state_is_identity_in_backward[False]", + "tests/check_qwen3_next_gdn_bridge.py::test_inactive_state_is_identity_in_backward[True]", + "tests/check_qwen3_next_gdn_bridge.py::test_initial_state_vjp_matches_provider_directional_difference", + "tests/check_qwen3_next_gdn_sequence.py::test_1024_token_chunk_continuation_is_bitwise_exact", + "tests/check_qwen3_next_gdn_sequence.py::test_bad_packing_is_rejected[bad0]", + "tests/check_qwen3_next_gdn_sequence.py::test_bad_packing_is_rejected[bad1]", + "tests/check_qwen3_next_gdn_sequence.py::test_bad_packing_is_rejected[bad2]", + "tests/check_qwen3_next_gdn_sequence.py::test_chunk_boundary_retains_prompt_and_initial_state_gradients", + "tests/check_qwen3_next_gdn_sequence.py::test_packed_matches_independent_sequences_and_reordering[lengths0]", + "tests/check_qwen3_next_gdn_sequence.py::test_packed_matches_independent_sequences_and_reordering[lengths1]", + "tests/check_qwen3_next_gdn_sequence.py::test_packed_matches_independent_sequences_and_reordering[lengths2]", + "tests/check_qwen3_next_gdn_sequence.py::test_reserved_and_duplicate_slots_are_rejected", + "tests/check_qwen3_next_paged_attention.py::test_paged_attention_full_chunk_reorder_and_cache_bits[1024-1]", + "tests/check_qwen3_next_paged_attention.py::test_paged_attention_full_chunk_reorder_and_cache_bits[1024-4]", + "tests/check_qwen3_next_paged_attention.py::test_paged_attention_full_chunk_reorder_and_cache_bits[65-1]", + "tests/check_qwen3_next_paged_attention.py::test_paged_attention_full_chunk_reorder_and_cache_bits[65-4]", + "tests/check_qwen3_next_paged_attention.py::test_paged_attention_full_chunk_reorder_and_cache_bits[8-1]", + "tests/check_qwen3_next_paged_attention.py::test_paged_attention_full_chunk_reorder_and_cache_bits[8-4]", + "tests/check_qwen3_next_paged_attention.py::test_paged_attention_invalid_indices_do_not_mutate_cache[bad_slots0]", + "tests/check_qwen3_next_paged_attention.py::test_paged_attention_invalid_indices_do_not_mutate_cache[bad_slots1]", + "tests/check_qwen3_next_paged_attention.py::test_paged_attention_invalid_indices_do_not_mutate_cache[bad_slots2]", + "tests/check_qwen3_next_paged_attention.py::test_paged_attention_invalid_indices_do_not_mutate_cache[bad_slots3]", + "tests/check_qwen3_next_shared_core.py::test_independent_training_forward_matches_inference_and_retains_gradients", + "tests/check_qwen3_next_shared_core.py::test_shared_core_chunk_and_decode_state_match[1024]", + "tests/check_qwen3_next_shared_core.py::test_shared_core_chunk_and_decode_state_match[8]", + "tests/check_qwen3_next_shared_core.py::test_unimplemented_provider_profile_is_rejected" + ], + "diagnostics": [ + "tests/check_gdn_recurrent_golden.py::test_bf16_state_rounding_stays_within_fixed_fixture_bound", + "tests/check_gdn_recurrent_golden.py::test_conv_dim_first_false_matches_the_transposed_layout", + "tests/check_gdn_recurrent_golden.py::test_conv_forward_fp32_and_call_entry_points", + "tests/check_gdn_recurrent_golden.py::test_conv_golden_is_batch_invariant[False-None-state_dtype0]", + "tests/check_gdn_recurrent_golden.py::test_conv_golden_is_batch_invariant[False-None-state_dtype1]", + "tests/check_gdn_recurrent_golden.py::test_conv_golden_is_batch_invariant[False-silu-state_dtype0]", + "tests/check_gdn_recurrent_golden.py::test_conv_golden_is_batch_invariant[False-silu-state_dtype1]", + "tests/check_gdn_recurrent_golden.py::test_conv_golden_is_batch_invariant[True-None-state_dtype0]", + "tests/check_gdn_recurrent_golden.py::test_conv_golden_is_batch_invariant[True-None-state_dtype1]", + "tests/check_gdn_recurrent_golden.py::test_conv_golden_is_batch_invariant[True-silu-state_dtype0]", + "tests/check_gdn_recurrent_golden.py::test_conv_golden_is_batch_invariant[True-silu-state_dtype1]", + "tests/check_gdn_recurrent_golden.py::test_conv_null_block_id_is_skipped", + "tests/check_gdn_recurrent_golden.py::test_conv_output_bf16_cache_matches_rounded_product_path[17]", + "tests/check_gdn_recurrent_golden.py::test_conv_output_bf16_cache_matches_rounded_product_path[1]", + "tests/check_gdn_recurrent_golden.py::test_conv_output_matches_provider_with_fp32_cache[17]", + "tests/check_gdn_recurrent_golden.py::test_conv_output_matches_provider_with_fp32_cache[1]", + "tests/check_gdn_recurrent_golden.py::test_conv_output_matches_provider_with_fp32_cache[4]", + "tests/check_gdn_recurrent_golden.py::test_conv_output_matches_provider_with_fp32_cache[64]", + "tests/check_gdn_recurrent_golden.py::test_conv_provider_preserves_bf16_product_cancellation", + "tests/check_gdn_recurrent_golden.py::test_conv_rejects_unknown_activation_and_bad_shapes", + "tests/check_gdn_recurrent_golden.py::test_conv_state_update_is_bitwise_exact[17]", + "tests/check_gdn_recurrent_golden.py::test_conv_state_update_is_bitwise_exact[1]", + "tests/check_gdn_recurrent_golden.py::test_conv_state_update_is_bitwise_exact[4]", + "tests/check_gdn_recurrent_golden.py::test_conv_state_update_is_bitwise_exact[64]", + "tests/check_gdn_recurrent_golden.py::test_golden_is_batch_invariant[state_dtype0]", + "tests/check_gdn_recurrent_golden.py::test_golden_is_batch_invariant[state_dtype1]", + "tests/check_gdn_recurrent_golden.py::test_golden_matches_packed_decode_provider[bf16-17]", + "tests/check_gdn_recurrent_golden.py::test_golden_matches_packed_decode_provider[bf16-1]", + "tests/check_gdn_recurrent_golden.py::test_golden_matches_packed_decode_provider[bf16-4]", + "tests/check_gdn_recurrent_golden.py::test_golden_matches_packed_decode_provider[bf16-64]", + "tests/check_gdn_recurrent_golden.py::test_golden_matches_packed_decode_provider[fp32-17]", + "tests/check_gdn_recurrent_golden.py::test_golden_matches_packed_decode_provider[fp32-1]", + "tests/check_gdn_recurrent_golden.py::test_golden_matches_packed_decode_provider[fp32-4]", + "tests/check_gdn_recurrent_golden.py::test_golden_matches_packed_decode_provider[fp32-64]", + "tests/check_gdn_recurrent_golden.py::test_null_block_id_is_skipped_by_both", + "tests/check_gdn_recurrent_golden.py::test_recurrent_call_matches_forward", + "tests/check_gdn_recurrent_golden.py::test_recurrent_forward_fp32_widens_the_state[state_dtype0]", + "tests/check_gdn_recurrent_golden.py::test_recurrent_forward_fp32_widens_the_state[state_dtype1]", + "tests/check_gdn_recurrent_golden.py::test_recurrent_without_in_kernel_l2norm_differs", + "tests/check_qwen3_next_batch_invariance.py::test_flashinfer_chunked_prefill_deviation_stays_rounding_scale", + "tests/check_qwen3_next_batch_invariance.py::test_triton_chunked_prefill_split_matches_one_pass[100]", + "tests/check_qwen3_next_batch_invariance.py::test_triton_chunked_prefill_split_matches_one_pass[128]", + "tests/check_qwen3_next_batch_invariance.py::test_triton_chunked_prefill_split_matches_one_pass[192]", + "tests/check_qwen3_next_batch_invariance.py::test_triton_chunked_prefill_split_matches_one_pass[64]", + "tests/check_qwen3_next_norm_providers.py::test_ours_tracks_each_vllm_path_within_bounds[forward_cuda]", + "tests/check_qwen3_next_norm_providers.py::test_ours_tracks_each_vllm_path_within_bounds[forward_native]", + "tests/check_qwen3_next_norm_providers.py::test_vllm_paths_disagree_only_within_bounds[dtype0-0.02-0.05]", + "tests/check_qwen3_next_norm_providers.py::test_vllm_paths_disagree_only_within_bounds[dtype1-1e-05-1.0]" + ] + } +} diff --git a/rl_engine/testing/qwen3_next_rl_fixture.py b/rl_engine/testing/qwen3_next_rl_fixture.py new file mode 100644 index 000000000..3d9cbaba8 --- /dev/null +++ b/rl_engine/testing/qwen3_next_rl_fixture.py @@ -0,0 +1,150 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Frozen inputs for real VIME policy-loss/optimizer recovery acceptance.""" + +import hashlib +import json +from dataclasses import dataclass + +import torch + +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal + +POLICY_SETTINGS = { + "loss_type": "policy_loss", + "advantage_estimator": "grpo", + "use_rollout_logprobs": True, + "disable_compute_advantages_and_returns": True, + "normalize_advantages": False, + "eps_clip": 0.2, + "eps_clip_high": 0.2, + "eps_clip_c": None, + "entropy_coef": 0.0, + "kl_coef": 0.0, + "kl_loss_coef": 0.0, + "opd_kl_coef": 0.0, + "use_kl_loss": False, + "use_tis": False, + "use_opsm": False, + "get_mismatch_metrics": False, + "custom_pg_loss_reducer_function_path": None, + "global_batch_size": 4, + "micro_batch_size": 1, + "use_dynamic_batch_size": False, + "rollout_temperature": 1.0, + "rollout_top_p": 1.0, + "rollout_top_k": -1, + "seed": 1234, +} +OPTIMIZER_SETTINGS = { + "optimizer": "adam", + "lr": 1e-6, + "lr_decay_style": "constant", + "weight_decay": 0.1, + "adam_beta1": 0.9, + "adam_beta2": 0.98, + "adam_eps": 1e-8, + "clip_grad": 1.0, + "optimizer_cpu_offload": True, + "optimizer_offload_fraction": 1.0, + "overlap_cpu_optimizer_d2h_h2d": False, + "use_precision_aware_optimizer": True, + "use_distributed_optimizer": True, + "use_stateless_adam": False, + "no_save_optim": False, + "no_save_rng": False, +} + + +def _tensor_digest(tensor): + value = tensor.detach().cpu().contiguous() + digest = hashlib.sha256() + digest.update(str((str(value.dtype), tuple(value.shape))).encode()) + digest.update(value.reshape(-1).view(torch.uint8).numpy().tobytes()) + return digest.hexdigest() + + +@dataclass(frozen=True) +class PolicyFixture: + tokens: torch.Tensor + old_logprobs: torch.Tensor + baseline_identity: str + content_sha256: str + + @classmethod + def freeze(cls, tokens, old_logprobs, *, baseline_identity): + if tokens.shape != (4, 80) or tokens.dtype != torch.int64: + raise ValueError("Policy fixture requires four int64 prompt64+response16 samples") + if old_logprobs.shape != (4, 16) or old_logprobs.dtype != torch.float32: + raise ValueError("Baseline selected logprobs must be FP32 [4, 16]") + if not torch.isfinite(old_logprobs).all() or old_logprobs.requires_grad: + raise ValueError("Baseline logprobs must be finite, frozen rollout values") + if not baseline_identity or not isinstance(baseline_identity, str): + raise ValueError("A verified baseline run/content identity is required") + if bool(((tokens < 0) | (tokens >= 151936)).any()): + raise ValueError("Token ID is outside the official Qwen3-Next vocabulary") + if torch.unique(tokens, dim=0).shape[0] != 4: + raise ValueError("The four policy samples must be distinct") + tokens = tokens.detach().cpu().clone() + old_logprobs = old_logprobs.detach().cpu().clone() + digest = cls._identity(tokens, old_logprobs, baseline_identity) + return cls(tokens, old_logprobs, baseline_identity, digest) + + @staticmethod + def _identity(tokens, old_logprobs, baseline_identity): + payload = { + "tokens": _tensor_digest(tokens), + "old_logprobs": _tensor_digest(old_logprobs), + "baseline": baseline_identity, + "advantages": [1.0, -0.5, 0.75, -0.25], + "response_mask": [1] * 16, + "sample_order": [[0], [1], [2], [3]], + "policy": POLICY_SETTINGS, + "optimizer": OPTIMIZER_SETTINGS, + } + return hashlib.sha256(json.dumps(payload, sort_keys=True).encode()).hexdigest() + + def validate(self): + if ( + self._identity(self.tokens, self.old_logprobs, self.baseline_identity) + != self.content_sha256 + ): + raise ValueError("Frozen policy fixture was modified") + + def verify_initial_recompute(self, logprobs): + self.validate() + assert_tensor_bitwise_equal( + logprobs.cpu(), self.old_logprobs, name="baseline policy logprobs" + ) + + def batch(self, sample_ids, *, device): + """Produce native VIME loss fields; no rollout hidden/state tensors are used.""" + self.validate() + if not sample_ids or any(type(i) is not int or i not in range(4) for i in sample_ids): + raise ValueError("Policy sample IDs must be nonempty integers in [0, 4)") + if len(set(sample_ids)) != len(sample_ids): + raise ValueError("A microbatch cannot duplicate policy samples") + advantages = [1.0, -0.5, 0.75, -0.25] + return { + "unconcat_tokens": [self.tokens[i].to(device=device) for i in sample_ids], + "rollout_log_probs": [self.old_logprobs[i].to(device=device) for i in sample_ids], + "advantages": [ + torch.full((16,), advantages[i], dtype=torch.float32, device=device) + for i in sample_ids + ], + "loss_masks": [torch.ones(16, dtype=torch.int32, device=device) for _ in sample_ids], + "rollout_mask_sums": torch.full( + (len(sample_ids),), 16, dtype=torch.float32, device=device + ), + "total_lengths": [80] * len(sample_ids), + "response_lengths": [16] * len(sample_ids), + "partition": list(sample_ids), + } + + +def require_policy_configuration(args): + """Reject drift rather than changing an already constructed training configuration.""" + for name, expected in {**POLICY_SETTINGS, **OPTIMIZER_SETTINGS}.items(): + if not hasattr(args, name) or getattr(args, name) != expected: + raise ValueError(f"Policy acceptance requires {name}={expected!r}") diff --git a/scripts/qwen3_next_adapter_check.py b/scripts/qwen3_next_adapter_check.py new file mode 100644 index 000000000..0705abf54 --- /dev/null +++ b/scripts/qwen3_next_adapter_check.py @@ -0,0 +1,321 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Four-rank real VIME/vLLM GDN adapter acceptance, scoped below complete-model L2.""" + +import argparse +import json +import os +from pathlib import Path +from types import SimpleNamespace + +import torch +import torch.distributed as dist + +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +def run(checkpoint, output): + from megatron.core import mpu + from megatron.core.packed_seq_params import PackedSeqParams + from megatron.core.transformer.spec_utils import build_module + from megatron.core.transformer.transformer_config import TransformerConfig + from vime.backends.megatron_utils import hf_to_megatron as load_api + from vime.backends.megatron_utils import megatron_to_hf as export_api + from vime.backends.megatron_utils.hf_to_megatron.common import ( + SafetensorReader, + shard_mcore_tensor, + ) + from vime.backends.megatron_utils.update_weight.common import all_gather_param + from vllm.config.vllm import set_current_vllm_config + from vllm.distributed import init_distributed_environment, initialize_model_parallel + from vllm.engine.arg_utils import EngineArgs + from vllm.forward_context import set_forward_context + from vllm.model_executor.layers.mamba.mamba_utils import is_conv_state_dim_first + from vllm.model_executor.model_loader.weight_utils import default_weight_loader + from vllm.v1.attention.backend import CommonAttentionMetadata + + from rl_engine.integrations.qwen3_next_vime import get_qwen3_next_shared_spec + from rl_engine.integrations.qwen3_next_vllm import SharedGDNMetadataBuilder, SharedQwenGDN + + rank, local_rank = int(os.environ["RANK"]), int(os.environ["LOCAL_RANK"]) + torch.cuda.set_device(local_rank) + dist.init_process_group("nccl") + if dist.get_world_size() != 4: + raise ValueError("GDN engine adapters require exactly four ranks") + mpu.initialize_model_parallel(tensor_model_parallel_size=4) + # vLLM 0.30.0's initialize_model_parallel reads get_current_vllm_config(), so the + # engine config must exist before the vLLM process groups are created. + vconfig = EngineArgs( + model=str(checkpoint), + skip_tokenizer_init=True, + dtype="bfloat16", + tensor_parallel_size=4, + max_model_len=2048, + max_num_seqs=4, + enforce_eager=True, + enable_prefix_caching=False, + mamba_cache_dtype="bfloat16", + mamba_ssm_cache_dtype="float32", + distributed_executor_backend="external_launcher", + additional_config={"gdn_prefill_backend": "triton"}, + ).create_engine_config() + with set_current_vllm_config(vconfig): + init_distributed_environment(4, rank, "env://", local_rank, "nccl") + initialize_model_parallel(4) + args = SimpleNamespace( + hf_checkpoint=str(checkpoint), + num_experts=512, + sequence_parallel=False, + hidden_size=2048, + num_attention_heads=16, + num_query_groups=2, + kv_channels=256, + ) + config = TransformerConfig( + num_layers=48, + hidden_size=2048, + num_attention_heads=16, + num_query_groups=2, + kv_channels=256, + ffn_hidden_size=5120, + num_moe_experts=512, + moe_ffn_hidden_size=512, + moe_router_topk=10, + moe_shared_expert_intermediate_size=512, + tensor_model_parallel_size=4, + expert_tensor_parallel_size=4, + pipeline_model_parallel_size=1, + context_parallel_size=1, + expert_model_parallel_size=1, + sequence_parallel=False, + attention_dropout=0, + hidden_dropout=0, + params_dtype=torch.bfloat16, + bf16=True, + normalization="RMSNorm", + add_bias_linear=False, + ) + spec = get_qwen3_next_shared_spec(args, config) + training = build_module( + spec.layer_specs[0].submodules.self_attention, config=config, layer_number=1 + ) + reader = SafetensorReader(checkpoint) + prefix = "module.module.decoder.layers.0.self_attention." + with torch.no_grad(): + for name, parameter in training.named_parameters(): + full_name = prefix + name + full = load_api._LOADERS["qwen3_next"](full_name, reader, training.hf_config) + local = shard_mcore_tensor(full_name, full, parameter) + if local.shape != parameter.shape: + raise ValueError(f"Incorrect training shard shape: {full_name}") + parameter.copy_(local.to(device=parameter.device)) + conv_name = prefix + "linear_attn.conv1d.weight" + conv = all_gather_param(conv_name, training.linear_attn.conv1d.weight) + converted = export_api.convert_qwen3_next_to_hf(args, conv_name, conv) + exact(converted[0][1].cpu(), reader.get_tensor("model.layers.0.linear_attn.conv1d.weight")) + + with set_current_vllm_config(vconfig): + previous_dtype = torch.get_default_dtype() + torch.set_default_dtype(torch.bfloat16) + try: + rollout = SharedQwenGDN( + vconfig.model_config.hf_text_config, + vconfig, + prefix="model.layers.0.linear_attn", + gqa_interleaved_layout=True, + ).cuda() + finally: + torch.set_default_dtype(previous_dtype) + for name, parameter in rollout.named_parameters(): + value = reader.get_tensor(f"model.layers.0.linear_attn.{name}") + getattr(parameter, "weight_loader", default_weight_loader)(parameter, value) + exact(parameter, dict(training.linear_attn.named_parameters())[name], name=name) + cache_spec = rollout.get_kv_cache_spec(vconfig) + builder = SharedGDNMetadataBuilder( + cache_spec, [rollout.prefix], vconfig, torch.device("cuda") + ) + + def initial_cache(): + # Deliberately dirty slots prove the metadata resets a new prompt. + return tuple( + torch.full((8, *shape), 0.125, dtype=dtype, device="cuda") + for shape, dtype in zip(rollout.get_state_shape(), rollout.get_state_dtype()) + ) + + def step(hidden, lengths, computed, slots): + cu = torch.tensor([0, *torch.tensor(lengths).cumsum(0).tolist()], dtype=torch.int32) + seq = torch.tensor(lengths, dtype=torch.int32) + torch.tensor( + computed, dtype=torch.int32 + ) + common = CommonAttentionMetadata( + query_start_loc=cu.cuda(), + query_start_loc_cpu=cu, + seq_lens=seq.cuda(), + num_reqs=len(lengths), + num_actual_tokens=sum(lengths), + max_query_len=max(lengths), + max_seq_len=int(seq.max()), + block_table_tensor=torch.tensor(slots, dtype=torch.int32, device="cuda")[:, None], + slot_mapping=torch.zeros(sum(lengths), dtype=torch.int64, device="cuda"), + ) + metadata = builder.build(0, common) + with set_forward_context({rollout.prefix: metadata}, vconfig): + return rollout(hidden_states=training.input_layernorm(hidden)) + + checked = [] + generator = torch.Generator(device="cuda").manual_seed(713) + for batch in (1, 4): + for length in (8, 64, 256, 1024): + hidden = torch.randn( + batch * length, 2048, generator=generator, device="cuda", dtype=torch.bfloat16 + ) + cu = torch.arange(batch + 1, dtype=torch.int32, device="cuda") * length + packed = PackedSeqParams( + qkv_format="thd", + cu_seqlens_q=cu, + cu_seqlens_kv=cu, + max_seqlen_q=length, + max_seqlen_kv=length, + ) + with torch.no_grad(): + train_out, _ = training(hidden.unsqueeze(1), None, packed_seq_params=packed) + rollout.kv_cache = initial_cache() + whole = step( + hidden.clone(), [length] * batch, [0] * batch, list(range(1, batch + 1)) + ) + exact(whole, train_out[:, 0], name=f"VIME/vLLM B{batch} L{length}") + final = tuple(state.clone() for state in rollout.kv_cache) + for cut in (1, 63, 64, 65, 100, 528, 576): + if cut >= length: + continue + rollout.kv_cache = initial_cache() + pieces = [[] for _ in range(batch)] + for lo, hi in ((0, cut), (cut, length)): + order = list(range(batch)) if lo == 0 else list(reversed(range(batch))) + rows = [ + row + for seq in order + for row in range(seq * length + lo, seq * length + hi) + ] + part = step( + hidden[rows], + [hi - lo] * batch, + [lo] * batch, + [seq + 1 for seq in order], + ) + for position, seq in enumerate(order): + pieces[seq].append( + part[position * (hi - lo) : (position + 1) * (hi - lo)] + ) + exact( + torch.cat([torch.cat(parts) for parts in pieces]), + whole, + name=f"chunk cut={cut}", + ) + for current, expected in zip(rollout.kv_cache, final): + exact(current, expected, name="owned rollout cache") + checked.append(f"batch{batch}-length{length}") + + # Exercise a fresh one-token prompt in the same real scheduler batch as + # prefill requests, then sixteen reordered decode steps on owned caches. + prompt_lengths = [1, 64, 256, 1024] + lengths = [length + 16 for length in prompt_lengths] + sequences = [ + torch.randn(length, 2048, generator=generator, device="cuda", dtype=torch.bfloat16) + for length in lengths + ] + complete = torch.cat(sequences) + cu = torch.tensor( + [0, *torch.tensor(lengths).cumsum(0).tolist()], device="cuda", dtype=torch.int32 + ) + packed = PackedSeqParams( + qkv_format="thd", + cu_seqlens_q=cu, + cu_seqlens_kv=cu, + max_seqlen_q=max(lengths), + max_seqlen_kv=max(lengths), + ) + with torch.no_grad(): + training_complete, _ = training(complete.unsqueeze(1), None, packed_seq_params=packed) + rollout.kv_cache = initial_cache() + whole = step(complete, lengths, [0] * 4, [1, 2, 3, 4]) + exact(whole, training_complete[:, 0], name="mixed VIME/vLLM complete") + final = tuple(state.clone() for state in rollout.kv_cache) + rollout.kv_cache = initial_cache() + first = step( + torch.cat([value[:length] for value, length in zip(sequences, prompt_lengths)]), + prompt_lengths, + [0] * 4, + [1, 2, 3, 4], + ) + pieces = [[part] for part in first.split(prompt_lengths)] + for position in range(16): + order = [3, 1, 0, 2] if position % 2 else [2, 0, 3, 1] + positions = [length + position for length in prompt_lengths] + outputs = step( + torch.cat( + [ + sequences[index][positions[index] : positions[index] + 1] + for index in order + ] + ), + [1] * 4, + [prompt_lengths[index] + position for index in order], + [index + 1 for index in order], + ) + for index, value in zip(order, outputs.split(1)): + pieces[index].append(value) + exact( + torch.cat([torch.cat(parts) for parts in pieces]), + whole, + name="sixteen decode steps", + ) + for current, expected in zip(rollout.kv_cache, final): + exact(current, expected, name="mixed owned rollout cache") + checked.append("mixed-prefill-decode16-reorder") + + hidden = torch.randn( + 8, 1, 2048, generator=generator, device="cuda", dtype=torch.bfloat16 + ).requires_grad_() + cu = torch.tensor([0, 8], device="cuda", dtype=torch.int32) + packed = PackedSeqParams( + qkv_format="thd", cu_seqlens_q=cu, cu_seqlens_kv=cu, max_seqlen_q=8, max_seqlen_kv=8 + ) + value, _ = training(hidden, None, packed_seq_params=packed) + value[4:].float().square().sum().backward() + if hidden.grad is None or not all( + (torch.isfinite(hidden.grad).all(), bool(hidden.grad[:4].abs().max() > 0)) + ): + raise AssertionError("Actual VIME response loss lost finite nonzero prompt gradients") + grads = [ + parameter.grad for parameter in training.parameters() if parameter.grad is not None + ] + if not grads or any(not torch.isfinite(grad).all() for grad in grads): + raise AssertionError("Actual VIME GDN parameter gradients must be finite") + from rl_engine.integrations.qwen3_next_binary import imported_provider_identity + + result = { + "status": "passed", + "scope": "gdn_engine_adapter_component", + "rank": rank, + "provider_binary": imported_provider_identity(), + "cases": checked, + "prompt_gradient": "passed", + "hf_conv_roundtrip": "passed", + "full_model_l2": "not_evaluated", + "batch_invariance_capability_registered": False, + "conv_dim_first": is_conv_state_dim_first(), + } + output.mkdir(parents=True, exist_ok=True) + (output / f"rank-{rank}.json").write_text(json.dumps(result, indent=2) + "\n") + dist.barrier() + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--checkpoint", type=Path, required=True) + parser.add_argument("--out", type=Path, required=True) + args = parser.parse_args() + run(args.checkpoint, args.out) diff --git a/scripts/qwen3_next_chain_report.py b/scripts/qwen3_next_chain_report.py new file mode 100644 index 000000000..df01bee54 --- /dev/null +++ b/scripts/qwen3_next_chain_report.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Write the WS1 chain-gate report for a finished Qwen3-Next chain. + + python scripts/qwen3_next_chain_report.py --last --out chain-report.json + +Follows ``previous`` from the given run back to the ``l2`` stage, reads every +passed stage's L2 matrix (``baseline`` from ``l2``, ``update1``/``update2`` +from ``train1``/``train2``, ``reload`` from ``train``) and the hash-checked case +artifacts it names, and writes ``rl_engine.kernels.gtest.qwen3_next_chain_report``'s +report. Exits 1 when the chain did not pass. +""" + +import argparse +import json +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) + +from rl_engine.kernels.gtest.qwen3_next_chain_report import build_report # noqa: E402 +from rl_engine.kernels.gtest.tolerance import load_contract # noqa: E402 +from rl_engine.testing.qwen3_next_l2 import ( # noqa: E402 + checked_artifact_path, + load_tensor_artifact, +) + +VERSION_OF_STAGE = {"l2": "baseline", "train1": "update1", "train2": "update2", "train": "reload"} + + +def chain_runs(last): + runs, run = [], Path(last).resolve() + while run is not None: + manifest = json.loads((run / "run.json").read_text()) + if manifest["stage"] in VERSION_OF_STAGE: + runs.append((manifest, run)) + if manifest["stage"] == "l2": + break + run = Path(manifest["previous"]) if manifest.get("previous") else None + if not runs or runs[-1][0]["stage"] != "l2": + raise SystemExit("The chain does not lead back to an l2 run") + commits = {manifest["source_commit"] for manifest, _ in runs} + if len(commits) != 1: + raise SystemExit(f"The chain mixes source commits: {sorted(commits)}") + return list(reversed(runs)) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--last", type=Path, required=True, help="last passed stage run") + parser.add_argument("--out", type=Path, required=True) + args = parser.parse_args() + matrices, artifacts = {}, {} + gpu_name = None + for manifest, run in chain_runs(args.last): + result = json.loads((run / "result.json").read_text()) + if result.get("status") != "passed": + raise SystemExit(f"{manifest['stage']} did not pass") + attempt = run / "attempts" / result["attempt"] + matrix = json.loads((run / result["evidence"]["l2_matrix"]["path"]).read_text()) + version = VERSION_OF_STAGE[manifest["stage"]] + matrices[version] = matrix + artifacts[version] = { + case_id: { + role: load_tensor_artifact(checked_artifact_path(record, artifact_root=attempt)) + for role, record in matrix["cases"][case_id]["artifacts"].items() + } + for case_id in matrix["case_ids"] + } + environment = attempt / "environment.json" + if gpu_name is None and environment.is_file(): + gpu_name = json.loads(environment.read_text()).get("gpu_name") + identity = manifest["checkpoint_identity"] + report = build_report( + matrices, + artifacts, + contract=load_contract(), + measured_commit=manifest["source_commit"], + weight_source=f"Qwen/Qwen3-Next-80B-A3B-Instruct@{identity['revision']}", + weight_hash=identity["expected_content_hash"], + device="cuda", + gpu_name=gpu_name, + compute_capability=None, + ) + args.out.parent.mkdir(parents=True, exist_ok=True) + args.out.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n") + print( + f"chain passed={report['passed']} first_drift={report['first_drift']} " + f"family={report['first_drift_family']} cells={len(report['cells'])}" + ) + if not report["passed"]: + raise SystemExit(1) + + +if __name__ == "__main__": + main() diff --git a/scripts/qwen3_next_checkpoint.py b/scripts/qwen3_next_checkpoint.py new file mode 100644 index 000000000..2d3e9036a --- /dev/null +++ b/scripts/qwen3_next_checkpoint.py @@ -0,0 +1,312 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Publish and recover complete all-rank checkpoints on the shared filesystem. + +Training owns serialization and barriers. This module never loads tensor files; +all four ranks must finish model, optimizer, RNG and progress before publishing. +""" + +import json +import os +import shutil +from pathlib import Path + +from qwen3_next_contract import atomic_json +from qwen3_next_runtime import file_digest + +ROLES = frozenset(("model", "optimizer", "rng", "progress")) + + +def _sync_directory(path): + fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY) + try: + os.fsync(fd) + finally: + os.close(fd) + + +def _file(root, relative): + relative = Path(relative) + if relative.is_absolute() or ".." in relative.parts: + raise ValueError("Checkpoint file must be relative to its checkpoint directory") + path = root / relative + if path.is_symlink() or not path.resolve().is_relative_to(root.resolve()) or not path.is_file(): + raise ValueError(f"Missing or external checkpoint file: {relative}") + return path + + +def write_rank_manifest(staging, *, rank, world_size, identity, step, sample_position, files): + staging = Path(staging) + if world_size != 4 or not 0 <= rank < world_size or step < 0 or sample_position < 0: + raise ValueError("Checkpoint requires four ranks and nonnegative progress") + if staging.suffix != ".partial" or (staging / "COMPLETE.json").exists(): + raise ValueError("Rank output requires an unpublished .partial checkpoint") + if set(files) != ROLES or any(not paths for paths in files.values()): + raise ValueError("Every rank must save model, optimizer, RNG and progress") + records = {} + seen = set() + for role, paths in files.items(): + records[role] = [] + for relative in paths: + if str(relative) in seen: + raise ValueError("A checkpoint file cannot fulfill multiple state roles") + seen.add(str(relative)) + path = _file(staging, relative) + with path.open("rb") as stream: + os.fsync(stream.fileno()) + records[role].append( + { + "path": str(relative), + "size": path.stat().st_size, + "sha256": file_digest(path), + } + ) + manifest = { + "schema_version": 1, + "rank": rank, + "world_size": world_size, + "identity": identity, + "step": step, + "sample_position": sample_position, + "files": records, + } + atomic_json(staging / f"rank-{rank}.manifest.json", manifest) + return manifest + + +def _validate_ranks(root, *, world_size, identity, verify_files=True): + if world_size != 4: + raise ValueError("Checkpoint topology must use four ranks") + manifests = [] + used_paths = set() + progress = None + for rank in range(world_size): + manifest = json.loads((root / f"rank-{rank}.manifest.json").read_text()) + if (manifest.get("schema_version"), manifest.get("rank"), manifest.get("world_size")) != ( + 1, + rank, + world_size, + ): + raise ValueError("Checkpoint rank topology mismatch") + if manifest.get("identity") != identity: + raise ValueError("Checkpoint model/provider/environment identity mismatch") + current = (manifest.get("step"), manifest.get("sample_position")) + if any( + not isinstance(value, int) or isinstance(value, bool) or value < 0 for value in current + ): + raise ValueError("Invalid checkpoint progress") + if progress is not None and current != progress: + raise ValueError("Checkpoint ranks have different progress") + progress = current + files = manifest.get("files", {}) + if set(files) != ROLES or any(not paths for paths in files.values()): + raise ValueError("Checkpoint omits required optimizer/RNG/model/progress state") + for records in files.values(): + for record in records: + if record["path"] in used_paths: + raise ValueError("Checkpoint ranks must own distinct state files") + used_paths.add(record["path"]) + path = _file(root, record["path"]) + if path.stat().st_size != record["size"]: + raise ValueError(f"Checkpoint state size mismatch: {record['path']}") + if verify_files and file_digest(path) != record["sha256"]: + raise ValueError(f"Checkpoint state checksum mismatch: {record['path']}") + manifests.append(manifest) + return manifests + + +def publish_checkpoint(staging, destination, *, world_size, identity): + staging, destination = Path(staging), Path(destination) + if ( + staging.suffix != ".partial" + or destination.suffix == ".partial" + or destination.name.startswith(".") + ): + raise ValueError("Publish from a .partial directory to a visible completed directory") + if staging.parent.resolve() != destination.parent.resolve() or destination.exists(): + raise ValueError("Checkpoint publication needs a new sibling destination") + manifests = _validate_ranks(staging, world_size=world_size, identity=identity) + complete = { + "schema_version": 1, + "status": "complete", + "world_size": world_size, + "identity": identity, + "step": manifests[0]["step"], + "sample_position": manifests[0]["sample_position"], + "rank_manifests": { + str(rank): file_digest(staging / f"rank-{rank}.manifest.json") + for rank in range(world_size) + }, + } + atomic_json(staging / "COMPLETE.json", complete) + # Sync nested directory entries as well as file contents before publication. + directories = sorted( + (path for path in staging.rglob("*") if path.is_dir()), + key=lambda path: len(path.parts), + reverse=True, + ) + for directory in directories: + _sync_directory(directory) + _sync_directory(staging) + staging.rename(destination) + _sync_directory(destination.parent) + atomic_json( + destination.parent / "LATEST.json", + { + "path": destination.name, + "step": complete["step"], + "complete_sha256": file_digest(destination / "COMPLETE.json"), + }, + ) + return complete + + +def validate_checkpoint(path, expected_identity, *, verify_files=True): + """Validate a published checkpoint's markers, manifests and files. + + ``verify_files=False`` checks every state file's presence and size but not its + content digest; callers use it where the content is verified anyway when the + files are read (the native restore compares every loaded tensor against the + saved digests), so a 1.1 TB checkpoint is not rehashed before each phase. + """ + path = Path(path) + if path.suffix == ".partial" or path.name.startswith("."): + raise ValueError("Unpublished checkpoint cannot be loaded") + complete = json.loads((path / "COMPLETE.json").read_text()) + if complete.get("schema_version") != 1 or complete.get("status") != "complete": + raise ValueError("Checkpoint has no supported completion marker") + if complete.get("identity") != expected_identity: + raise ValueError("Checkpoint model/provider/environment identity mismatch") + manifests = _validate_ranks( + path, + world_size=complete["world_size"], + identity=expected_identity, + verify_files=verify_files, + ) + if complete.get("rank_manifests") != { + str(rank): file_digest(path / f"rank-{rank}.manifest.json") for rank in range(4) + }: + raise ValueError("Checkpoint rank manifest checksum mismatch") + if (complete.get("step"), complete.get("sample_position")) != ( + manifests[0]["step"], + manifests[0]["sample_position"], + ): + raise ValueError("Completion marker progress mismatch") + return complete + + +def resume_interrupted_publications(root, expected_identity): + """Publish every fully written ``.partial`` checkpoint a killed job left unpublished. + + A preempted job can die after all four ranks wrote their files and manifests + but before rank 0 validated and renamed the directory. Such a directory is + publishable as is: publication rehashes every file against the manifests, + so an incomplete or damaged one is left for the normal retirement path. + Returns journal recovery records. + """ + root = Path(root) + records = [] + for staging in sorted(root.glob("*.partial")): + if not staging.is_dir() or staging.is_symlink() or (staging / "COMPLETE.json").exists(): + continue + marker = staging / "STAGING.json" + if not marker.is_file(): + continue + metadata = json.loads(marker.read_text()) + if metadata.get("identity") != expected_identity or metadata.get("status") != "staging": + continue + if not all((staging / f"rank-{rank}.manifest.json").is_file() for rank in range(4)): + continue + destination = staging.with_suffix("") + if destination.exists(): + continue + try: + complete = publish_checkpoint( + staging, destination, world_size=4, identity=expected_identity + ) + except (OSError, ValueError, KeyError) as exc: + records.append( + { + "reason": "interrupted_publication_not_resumable", + "staging": staging.name, + "error": str(exc)[:300], + } + ) + continue + records.append( + { + "reason": "interrupted_publication_resumed", + "checkpoint": destination.name, + "step": complete["step"], + } + ) + return records + + +def newest_complete_checkpoint(root, expected_identity, *, verify_files=True): + """Return a recovery record, retaining rejected and interrupted candidates.""" + root = Path(root) + candidates, rejected, interrupted = [], [], [] + for path in sorted(root.iterdir()): + if not path.is_dir(): + continue + if path.suffix == ".partial" or path.name.startswith("."): + interrupted.append(path.name) + continue + try: + complete = validate_checkpoint(path, expected_identity, verify_files=verify_files) + except (OSError, ValueError, KeyError) as exc: + rejected.append({"path": path.name, "reason": str(exc)}) + else: + candidates.append((complete["step"], path, complete)) + if not candidates and rejected: + raise ValueError(f"No valid published checkpoint remains: {rejected}") + chosen = max(candidates, key=lambda item: (item[0], item[1].name)) if candidates else None + return { + "checkpoint": str(chosen[1]) if chosen else None, + "step": chosen[0] if chosen else None, + "sample_position": chosen[2]["sample_position"] if chosen else None, + "rejected": rejected, + "interrupted": interrupted, + } + + +def require_checkpoint_disk_space( + root, *, checkpoint_bytes, export_bytes, retained_complete_bytes=0, headroom_bytes=16 * 1024**3 +): + """Budget last+next checkpoint and exports before serializing, without deletion. + + checkpoint_bytes includes model, optimizer/master weights, RNG and progress. + retained_complete_bytes discounts only already retained complete checkpoint + bytes; unfinished directories are never counted as available or reusable. + """ + if ( + any( + type(value) is not int or value < 0 + for value in (checkpoint_bytes, export_bytes, retained_complete_bytes, headroom_bytes) + ) + or checkpoint_bytes == 0 + ): + raise ValueError("Checkpoint disk estimates must be nonnegative integer byte counts") + retained = min(retained_complete_bytes, checkpoint_bytes) + required = 2 * checkpoint_bytes + export_bytes + headroom_bytes - retained + usage = shutil.disk_usage(root) + record = { + "scope": "checkpoint_disk_budget", + "checkpoint_bytes": checkpoint_bytes, + "last_plus_next_bytes": 2 * checkpoint_bytes, + "export_bytes": export_bytes, + "retained_complete_bytes": retained, + "headroom_bytes": headroom_bytes, + "required_free_bytes": required, + "actual_free_bytes": usage.free, + "status": "passed" if usage.free >= required else "insufficient_disk", + } + atomic_json(Path(root) / "disk-budget.json", record) + if usage.free < required: + raise RuntimeError( + f"insufficient_disk: need {required} free bytes for last+next checkpoint " + f"and exports, have {usage.free}" + ) + return record diff --git a/scripts/qwen3_next_contract.py b/scripts/qwen3_next_contract.py new file mode 100644 index 000000000..88d55dd6c --- /dev/null +++ b/scripts/qwen3_next_contract.py @@ -0,0 +1,371 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Versioned acceptance checks shared by the stage runners and the launcher.""" + +import hashlib +import json +import re +from pathlib import Path + +from qwen3_next_runtime import atomic_json, file_digest, identity_digest # noqa: F401 + +CONTRACT_VERSION = 4 +SCOPES = { + "components": "qwen3_next_components_only", + "diagnostics": "qwen3_next_operator_diagnostics", + "preflight": "four_gpu_infrastructure_and_operators_only", + "layers": "qwen3_next_layer_and_scheduler_gates", + "l2": "qwen3_next_full_model_l2", + "train1": "qwen3_next_training_update1", + "train2": "qwen3_next_training_update2", + "train": "qwen3_next_training_and_reload", +} +# Stages that evaluate the full model against updated weights (an L2 matrix each). +TRAIN_STAGES = ("train1", "train2", "train") +IDENTITY_FIELDS = ( + "contract_version", + "source_tree", + "environment_sha256", + "cuda_identity", + "checkpoint_identity", + "provider_config", + "topology", + "operator_requirements", + "layer_requirements", +) + + +def checkpoint_identity(checkpoint): + checkpoint = Path(checkpoint) + metadata = { + str(path.relative_to(checkpoint)): file_digest(path) + for path in sorted(checkpoint.iterdir()) + if path.is_file() and path.suffix != ".safetensors" + } + index = json.loads((checkpoint / "model.safetensors.index.json").read_text()) + reference = json.loads( + ( + Path(__file__).resolve().parents[1] / "rl_engine/testing/qwen3_next_norm_manifest.json" + ).read_text() + )["model_identity"] + snapshot = reference["weight_snapshot"] + if ( + checkpoint.name != reference["revision"] + or metadata.get(snapshot["index_file"]) != snapshot["index_sha256"] + ): + raise ValueError("Checkpoint metadata does not match the official pinned revision") + expected_shards = {item["filename"]: item for item in snapshot["shards"]} + if set(index["weight_map"].values()) != expected_shards.keys(): + raise ValueError("Checkpoint shard inventory differs from the official revision") + shards = {} + for filename, expected in sorted(expected_shards.items()): + stat = (checkpoint / filename).stat() + if stat.st_size != expected["size_bytes"]: + raise ValueError(f"Checkpoint shard size mismatch: {filename}") + shards[filename] = { + "size": stat.st_size, + "mtime_ns": stat.st_mtime_ns, + "ctime_ns": stat.st_ctime_ns, + "expected_sha256": expected["sha256"], + } + return { + "revision": checkpoint.name, + "metadata": metadata, + "shards": shards, + "expected_content_hash": snapshot["content_hash"], + "full_hash_verification": "required_before_model_load", + } + + +def operator_requirements(root, suite): + registry = json.loads( + (Path(root) / "rl_engine/testing/qwen3_next_operator_cases.json").read_text() + ) + if registry.get("schema_version") != 1: + raise ValueError("Unsupported mandatory operator case registry") + suites = registry["suites"] + cases = sorted(suites["norm"] + suites["core"] if suite == "all" else suites[suite]) + if not cases or len(cases) != len(set(cases)): + raise ValueError("Mandatory operator case registry is empty or duplicated") + gates = ( + sorted( + f"{op}-{gate}.json" + for op in ("qwen3_next_rms_norm", "rms_norm_gated") + for gate in ("forward", "gradient") + ) + if suite in ("all", "norm") + else [] + ) + return { + "suite": suite, + "case_ids": cases, + "case_ids_sha256": hashlib.sha256(("\n".join(cases) + "\n").encode()).hexdigest(), + "norm_gates": gates, + } + + +def latest_attempt(run_dir, scheduler_attempt=None): + values = [ + int(path.name) + for path in (Path(run_dir) / "attempts").glob("*") + if path.is_dir() and path.name.isdigit() + ] + if scheduler_attempt is not None: + if not str(scheduler_attempt).isdigit(): + raise ValueError("Invalid scheduler attempt identity") + values.append(int(scheduler_attempt)) + return str(max(values)) if values else None + + +def acceptance_identity(manifest): + return {field: manifest[field] for field in IDENTITY_FIELDS} + + +def result_envelope(manifest, attempt, *, evidence): + return { + **acceptance_identity(manifest), + "source_commit": manifest["source_commit"], + "run_id": manifest["run_id"], + "stage": manifest["stage"], + "suite": manifest.get("suite", "all"), + "execution_id": manifest["execution_id"], + "attempt": str(attempt), + "status": "diagnostic_completed" if manifest["stage"] == "diagnostics" else "passed", + "phase": "complete", + "scope": SCOPES[manifest["stage"]], + "full_model_l2": "not_evaluated", + "evidence": evidence, + } + + +def evidence_record(path, root): + return {"path": str(Path(path).relative_to(root)), "sha256": file_digest(path)} + + +def validate_partial_training_evidence(manifest, root, evidence, *, attempt_root): + """train1/train2: the progress record, its last phase's worker export and L2 matrix agree. + + A training phase's L2 runs on that phase's exported weights, so its matrix + identity is the run's model identity with the export as the checkpoint (the + pinned base recorded as ``base_checkpoint``), exactly as the stage built it. + """ + from qwen3_next_l2 import model_identity + from qwen3_next_training import STAGE_LAST_PHASE, read_record, stage_phases + + from rl_engine.testing.qwen3_next_l2 import validate_matrix_report + + stage = manifest["stage"] + progress = json.loads((root / evidence["updates"]["path"]).read_text()) + phases = list(stage_phases(stage)) + last = STAGE_LAST_PHASE[stage] + if ( + progress.get("status") != "passed" + or progress.get("scope") != SCOPES[stage] + or progress.get("identity") != model_identity(manifest) + or progress.get("phases") != phases + or any(phase not in progress.get("workers", {}) for phase in phases) + ): + raise ValueError("Training progress identity, scope or phases mismatch") + matrix_record = progress.get("l2_reports", {}).get(last, {}) + if matrix_record.get("sha256") != evidence["l2_matrix"]["sha256"]: + raise ValueError("Training progress does not reference the accepted L2 matrix") + worker = read_record(progress["workers"][last]) + export = worker.get("export") or {} + if ( + export.get("weight_version") is None + or export.get("base_checkpoint") != manifest["checkpoint"] + ): + raise ValueError("Training phase export does not derive from the pinned base checkpoint") + report = json.loads((root / evidence["l2_matrix"]["path"]).read_text()) + validate_matrix_report( + report, + expected_identity=model_identity({**manifest, **export}), + artifact_root=attempt_root, + ) + + +def validate_result(manifest, result, *, run_dir=None, attempt=None): + if manifest.get("contract_version") != CONTRACT_VERSION: + raise ValueError("Historical acceptance contract cannot authorize a new stage") + expected = { + **acceptance_identity(manifest), + "source_commit": manifest["source_commit"], + "run_id": manifest["run_id"], + "stage": manifest["stage"], + "suite": manifest.get("suite", "all"), + "status": "diagnostic_completed" if manifest["stage"] == "diagnostics" else "passed", + "phase": "complete", + "scope": SCOPES[manifest["stage"]], + } + for field, value in expected.items(): + if result.get(field) != value: + raise ValueError(f"Acceptance identity mismatch: {field}") + if not re.fullmatch(r"[0-9a-f]{32}", str(manifest.get("execution_id", ""))): + raise ValueError("Run has no launcher execution identity") + if result.get("execution_id") != manifest["execution_id"]: + raise ValueError("Acceptance execution identity mismatch") + if not str(result.get("attempt", "")).isdigit(): + raise ValueError("Acceptance has no attempt identity") + if run_dir is not None: + attempt = latest_attempt(run_dir, attempt) + if attempt is not None and result["attempt"] != str(attempt): + raise ValueError("Acceptance belongs to a stale attempt") + if manifest["stage"] in ("l2", *TRAIN_STAGES) and result.get("full_model_l2") != "passed": + raise ValueError("Full-model L2 evidence is required") + if ( + manifest["stage"] not in ("l2", *TRAIN_STAGES) + and result.get("full_model_l2") != "not_evaluated" + ): + raise ValueError("Component and layer acceptance cannot claim full-model L2") + if manifest["stage"] == "train" and result.get("training_and_reload") != "passed": + raise ValueError("Two updates and reload evidence is required") + evidence = result.get("evidence", {}) + required = {"operators", "environment"} + if manifest["stage"] == "preflight": + required |= {"device_guards", "tp4", "checkpoint", "rollout_smoke"} + if manifest["stage"] == "components" and manifest.get("suite") in ("norm", "all"): + required.add("device_guards") + if manifest["stage"] in ("l2", *TRAIN_STAGES): + required |= {"l2_matrix"} + if manifest["stage"] == "l2": + required.add("provider_binary") + if manifest["stage"] == "layers": + required |= {"layers", "checkpoint", "provider_binary"} + if manifest["stage"] == "train": + required |= {"updates", "reload"} + if manifest["stage"] in ("train1", "train2"): + required |= {"updates"} + if not required <= evidence.keys(): + raise ValueError(f"Incomplete acceptance evidence: {sorted(required - evidence.keys())}") + if run_dir is not None: + root = Path(run_dir).resolve() + attempt_root = root / "attempts" / result["attempt"] + for record in evidence.values(): + path = (root / record["path"]).resolve() + if not path.is_relative_to(attempt_root) or file_digest(path) != record["sha256"]: + raise ValueError("Acceptance evidence content/attempt mismatch") + operators = json.loads((root / evidence["operators"]["path"]).read_text()) + if ( + operators.get("status") != expected["status"] + or operators.get("git_commit") != manifest["source_commit"] + ): + raise ValueError("Operator evidence must match the completed source run") + requirement = manifest["operator_requirements"] + if manifest["stage"] == "diagnostics": + if ( + operators.get("diagnostic_only") is not True + or operators.get("case_ids_sha256") != requirement["case_ids_sha256"] + ): + raise ValueError("Diagnostic evidence case identity mismatch") + return identity_digest(expected) + case_ids = operators.get("mandatory_case_ids", []) + if ( + case_ids != requirement["case_ids"] + or operators.get("case_ids_sha256") != requirement["case_ids_sha256"] + ): + raise ValueError("Evidence differs from the pinned mandatory case registry") + gates = operators.get("norm_gates", {}) + if sorted(gates) != requirement["norm_gates"] or any( + gate.get("passed") is not True for gate in gates.values() + ): + raise ValueError("Norm C3/C4 evidence is missing or failed") + expected_digest = hashlib.sha256(("\n".join(sorted(case_ids)) + "\n").encode()).hexdigest() + counts = operators.get("provider_tests", {}) + if ( + not case_ids + or len(case_ids) != len(set(case_ids)) + or len(case_ids) != counts.get("tests") + or operators.get("case_ids_sha256") != expected_digest + ): + raise ValueError("Mandatory operator case identities do not match the evidence") + if not counts.get("tests") or any( + counts.get(key, 0) for key in ("skipped", "errors", "failures") + ): + raise ValueError("Required operator tests must pass without skips") + if manifest["stage"] in ("layers", "l2", *TRAIN_STAGES): + from qwen3_next_l2 import model_identity + + if manifest["stage"] == "layers": + from qwen3_next_layer_contract import validate_layer_report + + checkpoint = json.loads((root / evidence["checkpoint"]["path"]).read_text()) + if any( + checkpoint.get(key) != value + for key, value in { + "revision": manifest["checkpoint_identity"]["revision"], + "content_hash": manifest["checkpoint_identity"]["expected_content_hash"], + "shards_verified": len(manifest["checkpoint_identity"]["shards"]), + }.items() + ): + raise ValueError( + "Layer checkpoint verification differs from the pinned snapshot" + ) + report = json.loads((root / evidence["layers"]["path"]).read_text()) + validate_layer_report( + report, + manifest["layer_requirements"], + model_identity(manifest), + root=attempt_root, + ) + else: + from rl_engine.testing.qwen3_next_l2 import validate_matrix_report + + certificate = manifest.get("scheduler_profile_certificate", {}) + certificate_path = Path(certificate.get("path", "")) + if not certificate_path.is_absolute() or file_digest( + certificate_path + ) != certificate.get("sha256"): + raise ValueError("Strict L2 requires its pinned native scheduler certificate") + certified_manifest = json.loads((certificate_path.parent / "run.json").read_text()) + certified_result = json.loads(certificate_path.read_text()) + if certified_manifest.get("stage") != "layers" or acceptance_identity( + certified_manifest + ) != acceptance_identity(manifest): + raise ValueError("Strict L2 certificate belongs to another provider identity") + validate_result( + certified_manifest, certified_result, run_dir=certificate_path.parent + ) + if manifest["stage"] == "train": + from qwen3_next_training import validate_training_report + + if evidence["updates"]["sha256"] != evidence["reload"]["sha256"]: + raise ValueError( + "Update and reload evidence must name the same complete report" + ) + report = json.loads((root / evidence["updates"]["path"]).read_text()) + sections = { + "baseline", + "update1", + "update2", + "reload", + "continuous_step3", + "resumed_step3", + "checkpoint", + "fault_injection", + } + if ( + report.get("identity") != model_identity(manifest) + or set(report.get("required_sections", [])) != sections + ): + raise ValueError("Training report identity or required sections mismatch") + if ( + report.get("l2_reports", {}).get("reload", {}).get("sha256") + != evidence["l2_matrix"]["sha256"] + ): + raise ValueError( + "Training reload does not reference the accepted full L2 matrix" + ) + validate_training_report(report, verify_files=True) + elif manifest["stage"] in ("train1", "train2"): + validate_partial_training_evidence( + manifest, root, evidence, attempt_root=attempt_root + ) + else: + report = json.loads((root / evidence["l2_matrix"]["path"]).read_text()) + validate_matrix_report( + report, + expected_identity=model_identity(manifest), + artifact_root=attempt_root, + ) + return identity_digest(expected) diff --git a/scripts/qwen3_next_gate_common.py b/scripts/qwen3_next_gate_common.py index 00642c8de..8280e2d6c 100644 --- a/scripts/qwen3_next_gate_common.py +++ b/scripts/qwen3_next_gate_common.py @@ -117,7 +117,17 @@ def run_gate(description, scope, gate): args.output.mkdir(parents=True, exist_ok=True) path = args.output / f"rank-{rank}.json" partial = path.with_suffix(".json.partial") - payload = {"status": "passed", "scope": scope, "rank": rank, **provenance(), **result} + from rl_engine.integrations.qwen3_next_binary import imported_provider_identity + + payload = { + "status": "passed", + "scope": scope, + "full_model_l2": "not_evaluated", + "rank": rank, + "provider_binary": imported_provider_identity(), + **provenance(), + **result, + } partial.write_text(json.dumps(payload, indent=2) + "\n") partial.replace(path) dist.barrier() diff --git a/scripts/qwen3_next_l2.py b/scripts/qwen3_next_l2.py new file mode 100644 index 000000000..b0a191c45 --- /dev/null +++ b/scripts/qwen3_next_l2.py @@ -0,0 +1,742 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Independent native rollout/recompute processes; no numerical fallback.""" + +import argparse +import importlib +import json +import os +import shutil +import subprocess +import sys +import time +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) + +from qwen3_next_contract import ( # noqa: E402 + atomic_json, + checkpoint_identity, + evidence_record, + result_envelope, + validate_result, +) +from qwen3_next_runtime import ( # noqa: E402 + failure_fields, + file_digest, + memory_limits, + parse_memory_request, + subprocess_failure, + verify_checkpoint, + verify_environment, +) + +from rl_engine.testing.qwen3_next_binary import ( # noqa: E402 + archive_provider_binary, + validate_binary_record, + verify_imported_provider, +) +from rl_engine.testing.qwen3_next_l2 import ( # noqa: E402 + NATIVE_WORKER_MODULE, + PROMPT_LENGTHS, + L2Case, + case_matrix, + compare_artifacts, + compare_batch_prefill, + first_divergent_layer, + first_divergent_node, + json_digest, + load_tensor_artifact, + prompt_fixture, + save_tensor_artifact, + validate_debug_capture, + validate_fixture, + validate_matrix_report, + validate_model_artifact, + worker_request, +) + + +def check_ready(): + try: + module = importlib.import_module(NATIVE_WORKER_MODULE) + except ModuleNotFoundError as exc: + raise RuntimeError(f"native_workers_not_ready: {exc}") from exc + required = { + "rollout": True, + "recompute": True, + "debug_layers": True, + "architecture_layers": 48, + "tp": 4, + "batched_cases": True, + } + actual = module.capabilities() + if any(actual.get(key) != value for key, value in required.items()): + raise RuntimeError(f"native_workers_not_ready: required {required}, received {actual}") + if any( + not callable(getattr(module, name, None)) + for name in ( + "run_rollout_case", + "run_recompute_case", + "run_rollout_cases", + "run_recompute_cases", + ) + ): + raise RuntimeError("native_workers_not_ready: actual model worker entrypoints missing") + return module, actual + + +def model_identity(manifest): + identity = { + **( + {"scheduler_profile_certificate": manifest["scheduler_profile_certificate"]} + if "scheduler_profile_certificate" in manifest + else {} + ), + "source_commit": manifest["source_commit"], + "source_tree": manifest["source_tree"], + "environment_sha256": manifest["environment_sha256"], + "checkpoint": manifest["checkpoint"], + "weight_version": manifest.get("weight_version", "baseline"), + "checkpoint_identity": manifest["checkpoint_identity"], + "provider_config": manifest["provider_config"], + "topology": manifest["topology"], + "architecture_layers": 48, + "weights_dtype": "bfloat16", + "recurrent_state_dtype": "float32", + } + if manifest.get("weight_version", "baseline") != "baseline": + identity.update( + base_checkpoint=manifest["base_checkpoint"], + base_checkpoint_identity=manifest["base_checkpoint_identity"], + ) + from rl_engine.testing.qwen3_next_l2 import validate_weight_identity + + validate_weight_identity(identity) + return identity + + +def verify_before_model_load(manifest, output): + if manifest.get("weight_version", "baseline") != "baseline": + verified = verify_updated_weights(manifest) + else: + if checkpoint_identity(manifest["checkpoint"]) != manifest["checkpoint_identity"]: + raise RuntimeError("checkpoint_mismatch: official weight snapshot changed") + reference = json.loads( + (ROOT / "rl_engine/testing/qwen3_next_norm_manifest.json").read_text() + )["model_identity"] + verified = verify_checkpoint(Path(manifest["checkpoint"]), reference) + atomic_json(output / "weight-verification.json", verified) + return verified + + +def verify_updated_weights(manifest): + from qwen3_next_training_export import validate_training_export + + verified = validate_training_export(manifest["checkpoint"], manifest["checkpoint_identity"]) + expected = { + "weight_version": manifest["weight_version"], + "base_checkpoint": manifest["base_checkpoint"], + "base_checkpoint_identity": manifest["base_checkpoint_identity"], + } + if any(verified.get(key) != value for key, value in expected.items()): + raise RuntimeError("checkpoint_mismatch: updated weights/base/version provenance differs") + for key in ( + "source_commit", + "source_tree", + "environment_sha256", + "provider_config", + "topology", + ): + if verified["training_identity"].get(key) != manifest[key]: + raise RuntimeError(f"checkpoint_mismatch: updated weights changed {key}") + return verified + + +def run_one(module, role, request, output): + output.parent.mkdir(parents=True, exist_ok=True) + atomic_json(output.with_suffix(".request.json"), request) + getattr(module, f"run_{role}_case")(request, output) + if not output.is_file(): + raise RuntimeError("Native worker did not produce a full-vocabulary tensor artifact") + artifact = load_tensor_artifact(output) + validate_model_artifact(artifact, request) + validate_debug_capture(artifact, request) + return artifact + + +def worker_batch( + manifest, + output, + role, + fixture_path, + *, + debug_request=None, + native_worker=None, + provider_binary=None, +): + if manifest.get("weight_version", "baseline") != "baseline" and not fixture_path.is_file(): + raise RuntimeError("Updated weights must reuse the already frozen baseline input fixture") + module, capabilities = check_ready() if native_worker is None else native_worker + verify_environment(manifest["environment"], require_venv=True) + output.mkdir(parents=True, exist_ok=debug_request is not None) + verify_before_model_load(manifest, output) + identity = model_identity(manifest) + actual_binary = None + if provider_binary is not None: + actual_binary = verify_imported_provider( + provider_binary, source_tree=manifest["source_tree"] + ) + elif manifest["provider_config"].get("profile") == "qwen3_next_tp4_shared_eager_v1": + raise RuntimeError("Native worker requires the stage's pinned CUDA binary evidence") + if debug_request is not None: + request = json.loads(debug_request.read_text()) + if request["identity"] != identity or request["role"] != role: + raise ValueError("Debug request identity mismatch") + artifact = run_one(module, role, request, output / "debug.pt") + if not artifact.get("debug_tensors"): + raise RuntimeError("First divergence rerun did not save layer/recurrent-state tensors") + return + fixture = None + if fixture_path.is_file(): + fixture = load_tensor_artifact(fixture_path) + validate_fixture(fixture) + elif role != "rollout": + raise RuntimeError("Recompute requires the already frozen baseline token fixture") + records, pending = {}, [] + + def cases(): + nonlocal fixture + if fixture is None: + from transformers import AutoTokenizer + + from rl_engine.testing.qwen3_next_l2 import freeze_responses + + tokenizer = AutoTokenizer.from_pretrained( + manifest["checkpoint"], local_files_only=True, trust_remote_code=False + ) + prompts = prompt_fixture(tokenizer, manifest["checkpoint_identity"]["metadata"]) + baseline, baseline_records = {}, {} + for length in PROMPT_LENGTHS: + case = L2Case(length, 4, "full") + request = worker_request(case, prompts, role=role, identity=identity, generate=True) + path = output / "baseline" / f"{case.case_id}.pt" + path.parent.mkdir(parents=True, exist_ok=True) + atomic_json(path.with_suffix(".request.json"), request) + atomic_json( + output / "active-case.json", + { + "case_id": case.case_id, + "phase": "baseline_generation", + "request": str(path.with_suffix(".request.json")), + }, + ) + yield request, path + artifact = load_tensor_artifact(path) + validate_model_artifact(artifact, request) + if actual_binary is not None and artifact.get("provider_binary") != actual_binary: + raise ValueError("Baseline generation used a different CUDA provider") + baseline[str(length)] = artifact + baseline_records[str(length)] = {"path": str(path), "sha256": file_digest(path)} + fixture = freeze_responses( + prompts, + baseline, + baseline_identity=json_digest(identity), + baseline_records=baseline_records, + ) + validate_fixture(fixture) + save_tensor_artifact(fixture_path, fixture) + tokenizer_identity = manifest.get( + "base_checkpoint_identity", manifest["checkpoint_identity"] + ) + if fixture["prompt_identity"]["tokenizer"] != tokenizer_identity["metadata"]: + raise RuntimeError("Frozen input tokenizer identity mismatch") + for case in case_matrix(): + request = worker_request(case, fixture, role=role, identity=identity) + path = output / f"{case.case_id}.pt" + atomic_json(path.with_suffix(".request.json"), request) + atomic_json( + output / "active-case.json", + { + "case_id": case.case_id, + "phase": ( + "preparing_recompute_requests" if role == "recompute" else "frozen_inputs" + ), + "request": str(path.with_suffix(".request.json")), + }, + ) + # Recompute materializes this iterator before one four-rank model + # lifetime. Its artifacts therefore exist only after the batch returns. + # Rollout's baseline loop above stays lazy to freeze generated tokens. + pending.append((request, path)) + yield request, path + + getattr(module, f"run_{role}_cases")(cases()) + if [request["case_id"] for request, _ in pending] != [case.case_id for case in case_matrix()]: + raise RuntimeError("Native batch worker did not consume every required case") + for request, path in pending: + artifact = load_tensor_artifact(path) + validate_model_artifact(artifact, request) + if actual_binary is not None and artifact.get("provider_binary") != actual_binary: + raise ValueError("Native rank traces used a different CUDA provider") + records[request["case_id"]] = { + "path": str(path), + "sha256": file_digest(path), + "request_sha256": request["request_sha256"], + } + if manifest.get("weight_version", "baseline") != "baseline": + verify_updated_weights(manifest) + elif checkpoint_identity(manifest["checkpoint"]) != manifest["checkpoint_identity"]: + raise RuntimeError("checkpoint_mismatch: weights changed during model execution") + if provider_binary is not None: + verify_imported_provider(provider_binary, source_tree=manifest["source_tree"]) + atomic_json( + output / "worker-result.json", + { + "status": "passed", + "role": role, + "pid": os.getpid(), + "identity": identity, + "capabilities": capabilities, + "fixture_sha256": fixture["content_sha256"], + "cases": records, + "provider_binary": actual_binary, + }, + ) + + +def run_process(command, *, log_path, timeout=4 * 3600, cleanup_grace_seconds=10): + with Path(log_path).open("w") as log: + child = subprocess.Popen( + command, stdout=log, stderr=subprocess.STDOUT, start_new_session=True + ) + try: + code = child.wait(timeout=timeout) + except BaseException: + from qwen3_next_runtime import wait_owned_child + + # The leader may exit on TERM while workers in its group ignore it. + # Finish the bounded group cleanup even after the leader has exited. + wait_owned_child(child, lambda: True, grace_seconds=cleanup_grace_seconds) + raise + if code: + raise subprocess_failure(code, log_path) + return {"pid": child.pid, "exit_code": code, "log": str(log_path)} + + +def gpu_probe(output): + import torch + + if torch.cuda.device_count() != 4 or any( + "B200" not in torch.cuda.get_device_name(index) for index in range(4) + ): + raise RuntimeError("topology_mismatch: four allocated B200 GPUs required") + atomic_json( + output, {"uuids": [str(torch.cuda.get_device_properties(index).uuid) for index in range(4)]} + ) + + +def wait_gpu_release(uuids, *, timeout=30): + wanted = {uuid.lower().removeprefix("gpu-") for uuid in uuids} + deadline = time.monotonic() + timeout + while True: + output = subprocess.check_output( + [ + "nvidia-smi", + "--query-compute-apps=pid,gpu_uuid,used_gpu_memory", + "--format=csv,noheader,nounits", + ], + text=True, + timeout=10, + ) + remaining = [ + line + for line in output.splitlines() + if len(line.split(",")) == 3 + and line.split(",")[1].strip().lower().removeprefix("gpu-") in wanted + ] + if not remaining: + return + if time.monotonic() >= deadline: + raise RuntimeError(f"gpu_memory_not_released: {remaining}") + time.sleep(0.25) + + +def run_matrix(manifest_path, output, fixture_path): + """Return evidence after two independent processes; never mark training complete.""" + manifest = json.loads(Path(manifest_path).read_text()) + output = Path(output) + output.mkdir(parents=True, exist_ok=True) + check_ready() + verify_environment(manifest["environment"], require_venv=True) + memory = memory_limits() + requested = parse_memory_request(manifest["memory"]) + if ( + memory["effective_bytes"] is None + or memory["effective_bytes"] < requested + or memory["available_bytes"] is None + or memory["available_bytes"] < requested + ): + raise RuntimeError("insufficient_memory: L2 allocation cannot satisfy its pinned budget") + prebuilt = manifest.get("prebuilt_provider_binary") + if prebuilt is None: + run_process( + [manifest["python"], str(ROOT / "setup.py"), "build_ext", "--inplace"], + log_path=output / "build.log", + timeout=1800, + ) + else: + expected = validate_binary_record(prebuilt, source_tree=manifest["source_tree"]) + path = Path(expected["path"]) + if path.parent != ROOT / "rl_engine" or file_digest(path) != expected["sha256"]: + raise ValueError("Training's prebuilt CUDA provider changed before L2") + provider_binary = archive_provider_binary(ROOT, output, source_tree=manifest["source_tree"]) + probe_path = output / "gpu-probe.json" + run_process( + [manifest["python"], str(Path(__file__)), "--gpu-probe", "--output", str(probe_path)], + log_path=output / "gpu-probe.log", + timeout=180, + ) + uuids = json.loads(probe_path.read_text())["uuids"] + wait_gpu_release(uuids) + atomic_json( + output / "environment.json", + { + "environment": manifest["environment"], + "memory": memory, + "gpu_uuids": uuids, + "execution_gpus": 4, + }, + ) + if manifest.get("input_fixture"): + if ( + not manifest.get("input_fixture_sha256") + or file_digest(fixture_path) != manifest["input_fixture_sha256"] + ): + raise RuntimeError( + "input_fixture_mismatch: external baseline fixture must be pinned by file SHA256" + ) + processes = {} + for role in ("rollout", "recompute"): + command = [ + manifest["python"], + str(Path(__file__)), + "--worker", + role, + "--manifest", + str(manifest_path), + "--output", + str(output / role), + "--fixture", + str(fixture_path), + "--provider-binary", + json.dumps(provider_binary), + ] + processes[role] = run_process(command, log_path=output / f"{role}.log") + wait_gpu_release(uuids) + processes[role]["gpu_release_verified"] = True + worker_result = json.loads((output / role / "worker-result.json").read_text()) + if ( + worker_result.get("pid") != processes[role]["pid"] + or worker_result.get("status") != "passed" + or worker_result.get("identity") != model_identity(manifest) + or sorted(worker_result.get("cases", {})) + != sorted(case.case_id for case in case_matrix()) + ): + raise RuntimeError("Independent native worker did not finish the required matrix") + atomic_json(output / "processes.json", processes) + fixture = load_tensor_artifact(fixture_path) + validate_fixture(fixture) + records, first_failure = {}, None + baseline_outputs = {} + for length, record in fixture["baseline_artifacts"].items(): + if file_digest(record["path"]) != record["sha256"]: + raise RuntimeError("Baseline generation artifact was changed") + baseline_outputs[length] = load_tensor_artifact(record["path"]) + original_identity = baseline_outputs[length]["metadata"]["identity"] + if json_digest(original_identity) != fixture["baseline_identity"]: + raise RuntimeError("Baseline generation provenance was changed") + baseline_request = worker_request( + L2Case(int(length), 4, "full"), + fixture, + role="rollout", + identity=original_identity, + generate=True, + ) + validate_model_artifact(baseline_outputs[length], baseline_request) + identity = model_identity(manifest) + for case in case_matrix(): + artifacts = {} + paths = {} + for role in ("rollout", "recompute"): + path = output / role / f"{case.case_id}.pt" + artifact = load_tensor_artifact(path) + validate_model_artifact( + artifact, worker_request(case, fixture, role=role, identity=identity) + ) + artifacts[role], paths[role] = artifact, str(path) + paired = compare_artifacts(artifacts["rollout"], artifacts["recompute"]) + invariance = {} + for role in ("rollout", "recompute"): + reference = load_tensor_artifact( + output / role / f"{L2Case(case.prompt_length, 4, 'full').case_id}.pt" + ) + invariance[role] = compare_batch_prefill(reference, artifacts[role]) + baseline_checks = {} + if identity.get("weight_version") == "baseline": + baseline_checks = { + role: compare_batch_prefill(baseline_outputs[str(case.prompt_length)], artifact) + for role, artifact in artifacts.items() + } + passed = ( + paired["passed"] + and all(all(checks.values()) for checks in invariance.values()) + and all(all(checks.values()) for checks in baseline_checks.values()) + ) + records[case.case_id] = { + **paired, + "passed": passed, + "batch_prefill": invariance, + "baseline_generation": baseline_checks, + "artifacts": { + role: {"path": path, "sha256": file_digest(path)} for role, path in paths.items() + }, + } + if not passed and first_failure is None: + if not paired["passed"]: + first_failure = { + "case": case, + "paired": paired, + "contexts": [("rollout", case, False), ("recompute", case, False)], + } + elif baseline_checks and not all( + all(checks.values()) for checks in baseline_checks.values() + ): + failed_role = next( + role + for role in ("rollout", "recompute") + if not all(baseline_checks[role].values()) + ) + reference_case = L2Case(case.prompt_length, 4, "full") + reference = baseline_outputs[str(case.prompt_length)] + first_failure = { + "case": case, + "paired": { + **paired, + "first_divergent_layer": first_divergent_layer( + reference, artifacts[failed_role] + ), + "first_divergent_node": first_divergent_node( + reference, artifacts[failed_role] + ), + }, + "contexts": [("rollout", reference_case, True), (failed_role, case, False)], + } + else: + failed_role = next( + role for role in ("rollout", "recompute") if not all(invariance[role].values()) + ) + reference_case = L2Case(case.prompt_length, 4, "full") + reference = load_tensor_artifact( + output / failed_role / f"{reference_case.case_id}.pt" + ) + first_failure = { + "case": case, + "paired": { + **paired, + "first_divergent_layer": first_divergent_layer( + reference, artifacts[failed_role] + ), + "first_divergent_node": first_divergent_node( + reference, artifacts[failed_role] + ), + }, + "contexts": [(failed_role, reference_case, False), (failed_role, case, False)], + } + report = { + "schema_version": 1, + "status": "passed" if first_failure is None else "failed", + "scope": "qwen3_next_full_model_l2", + "identity": identity, + "fixture": { + "path": str(fixture_path), + "sha256": file_digest(fixture_path), + "content_sha256": fixture["content_sha256"], + }, + "case_ids": [case.case_id for case in case_matrix()], + "cases": records, + "processes": processes, + "worker_results": { + role: { + "path": str(output / role / "worker-result.json"), + "sha256": file_digest(output / role / "worker-result.json"), + } + for role in ("rollout", "recompute") + }, + "raw_bit_comparison": True, + "logits_dtype": "bfloat16", + "logprobs_dtype": "float32", + "response_length": 16, + "vocab_size": 151936, + "provider_binary": provider_binary, + } + atomic_json(output / "l2-matrix.json", report) + if first_failure: + case = first_failure["case"] + layer = first_failure["paired"]["first_divergent_layer"] + capture = { + "case_id": case.case_id, + "first_divergent_layer": layer, + "first_divergent_node": first_failure["paired"].get("first_divergent_node"), + "first_divergent_component": "lm_head_or_logprob" if layer is None else "decoder_layer", + "first_difference": first_failure["paired"]["first_difference"], + "captures": {}, + } + for label, (role, context_case, generate) in zip( + ("reference", "candidate"), first_failure["contexts"] + ): + directory = output / "first-divergence" / label + directory.mkdir(parents=True, exist_ok=True) + request = worker_request( + context_case, + fixture, + role=role, + identity=identity, + trace_layers=[47] if layer is None else [layer], + generate=generate, + ) + request_path = directory / "request.json" + atomic_json(request_path, request) + try: + command = [ + manifest["python"], + str(Path(__file__)), + "--worker", + role, + "--manifest", + str(manifest_path), + "--output", + str(directory), + "--fixture", + str(fixture_path), + "--debug-request", + str(request_path), + "--provider-binary", + json.dumps(provider_binary), + ] + run_process(command, log_path=directory / "worker.log") + wait_gpu_release(uuids) + except (RuntimeError, subprocess.SubprocessError) as exc: + capture["captures"][label] = {"status": "failed", "error": str(exc)} + break + capture["captures"][label] = { + "status": "saved", + "role": role, + "case_id": context_case.case_id, + "path": str(directory / "debug.pt"), + "sha256": file_digest(directory / "debug.pt"), + } + atomic_json(output / "first-divergence.json", capture) + if report["status"] == "passed": + validate_matrix_report(report, expected_identity=identity, artifact_root=output) + return report + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--manifest", type=Path) + parser.add_argument("--output", type=Path) + parser.add_argument("--fixture", type=Path) + parser.add_argument("--worker", choices=("rollout", "recompute")) + parser.add_argument("--debug-request", type=Path) + parser.add_argument("--provider-binary", type=json.loads) + parser.add_argument("--check-ready", action="store_true") + parser.add_argument("--gpu-probe", action="store_true") + args = parser.parse_args() + if args.check_ready: + try: + _, capabilities = check_ready() + except (RuntimeError, AttributeError) as exc: + print(json.dumps({"status": "not_ready", "error": str(exc)})) + return 2 + print(json.dumps({"status": "ready", "capabilities": capabilities})) + return 0 + if args.gpu_probe: + gpu_probe(args.output) + return 0 + if args.manifest is None or args.output is None: + parser.error("--manifest and --output are required") + manifest = json.loads(args.manifest.read_text()) + fixture_path = args.fixture or Path( + manifest.get("input_fixture", args.manifest.parent / "inputs/frozen.pt") + ) + if args.worker: + worker_batch( + manifest, + args.output, + args.worker, + fixture_path, + debug_request=args.debug_request, + provider_binary=args.provider_binary, + ) + return 0 + args.output.mkdir(parents=True, exist_ok=True) + try: + if manifest["stage"] != "l2": + raise RuntimeError("Standalone L2 runner cannot close training acceptance") + from qwen3_next_runtime import require_predecessor + + previous = Path(manifest["previous"]) + require_predecessor("l2", previous, Path(manifest["checkpoint"]), manifest) + predecessor = json.loads((previous / "run.json").read_text()) + previous_result = json.loads((previous / "result.json").read_text()) + validate_result(predecessor, previous_result, run_dir=previous) + report = run_matrix(args.manifest, args.output, fixture_path) + if report["status"] != "passed": + raise RuntimeError("Full-model raw-bit L2 failed; first divergence artifacts retained") + prior_operators = previous / previous_result["evidence"]["operators"]["path"] + shutil.copyfile(prior_operators, args.output / "operators.json") + atomic_json( + args.output / "predecessor-evidence.json", + { + "run_id": predecessor["run_id"], + "execution_id": predecessor["execution_id"], + "result_sha256": file_digest(previous / "result.json"), + }, + ) + evidence = { + name: evidence_record(args.output / filename, args.manifest.parent) + for name, filename in { + "operators": "operators.json", + "environment": "environment.json", + "l2_matrix": "l2-matrix.json", + "predecessor": "predecessor-evidence.json", + "provider_binary": "provider-binary.json", + }.items() + } + result = result_envelope(manifest, os.environ["QWEN3_NEXT_ATTEMPT"], evidence=evidence) + result["full_model_l2"] = "passed" + validate_result( + manifest, result, run_dir=args.manifest.parent, attempt=os.environ["QWEN3_NEXT_ATTEMPT"] + ) + atomic_json(args.output / "result.json", result) + except Exception as exc: + atomic_json( + args.output / "result.json", + { + "run_id": manifest["run_id"], + "attempt": os.environ["QWEN3_NEXT_ATTEMPT"], + "status": "failed", + "full_model_l2": "not_passed", + **failure_fields(exc), + }, + ) + raise + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/qwen3_next_layer_contract.py b/scripts/qwen3_next_layer_contract.py new file mode 100644 index 000000000..685e0117e --- /dev/null +++ b/scripts/qwen3_next_layer_contract.py @@ -0,0 +1,123 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Pinned layer component inventory and original tensor scheduler acceptance.""" + +import hashlib +import json +from pathlib import Path + +from qwen3_next_runtime import file_digest, identity_digest + + +def layer_requirements(root): + root = Path(root) + path = root / "rl_engine/testing/qwen3_next_layer_cases.json" + registry = json.loads(path.read_text()) + if registry.get("schema_version") != 1 or set(registry.get("gates", {})) != { + "gdn", + "attention", + "moe", + "adapters", + "heads", + }: + raise ValueError("Incomplete layer gate registry") + for gate in registry["gates"].values(): + gate["script_sha256"] = file_digest(root / gate["script"]) + for cases in gate["groups"].values(): + if not cases or len(cases) != len(set(cases)): + raise ValueError("Mandatory layer case list is empty or duplicated") + cases = sorted( + f"{name}/{group}/{case}" + for name, gate in registry["gates"].items() + for group, values in gate["groups"].items() + for case in values + ) + return { + **registry, + "registry_sha256": file_digest(path), + "case_ids_per_rank": cases, + "case_ids_sha256": hashlib.sha256(("\n".join(cases) + "\n").encode()).hexdigest(), + } + + +def validate_rank_result(row, requirement, rank): + if any( + row.get(name) != value + for name, value in { + "status": "passed", + "rank": rank, + "scope": requirement["scope"], + "full_model_l2": "not_evaluated", + **requirement.get("fields", {}), + }.items() + ): + raise ValueError("Layer rank evidence has a wrong identity, scope or status") + if any(row.get(name, 0) for name in ("skipped", "failures", "errors")): + raise ValueError("Layer rank evidence includes skipped or failed checks") + for field, expected in requirement["groups"].items(): + value = row + for key in field.split("."): + value = value.get(key, {}) if isinstance(value, dict) else None + if not isinstance(value, list) or sorted(value) != sorted(expected): + raise ValueError(f"Layer rank evidence changed mandatory cases: {field}") + + +def checked_record(record, *, root=None): + if not isinstance(record, dict) or not {"path", "sha256"} <= record.keys(): + raise ValueError("Missing artifact path/content digest") + path = Path(record["path"]) + if not path.is_absolute(): + if root is None: + raise ValueError("Artifact path must be absolute without an evidence root") + path = Path(root) / path + path = path.resolve(strict=True) + if root is not None and not path.is_relative_to(Path(root).resolve()): + raise ValueError("Artifact escaped its current attempt") + if file_digest(path) != record["sha256"]: + raise ValueError("Artifact content digest changed") + return path + + +def validate_layer_report(report, requirement, identity, *, root): + if any( + report.get(key) != value + for key, value in { + "schema_version": 1, + "status": "passed", + "phase": "complete", + "scope": "qwen3_next_layer_and_scheduler_gates", + "identity": identity, + "requirements_sha256": identity_digest(requirement), + "full_model_l2": "not_evaluated", + }.items() + ): + raise ValueError("Layer acceptance identity or completeness mismatch") + if set(report.get("gates", {})) != set(requirement["gates"]): + raise ValueError("Layer acceptance omitted a mandatory gate") + from rl_engine.testing.qwen3_next_binary import validate_binary_record + + binary = validate_binary_record( + report.get("provider_binary"), source_tree=identity["source_tree"], artifact_root=root + ) + for name, expected in requirement["gates"].items(): + gate = report["gates"][name] + if gate.get("exit_code") != 0 or gate.get("gpu_release_verified") is not True: + raise ValueError("Layer process failed or did not release GPU memory") + ranks = gate.get("ranks", {}) + if set(ranks) != {"0", "1", "2", "3"}: + raise ValueError("Layer acceptance requires each of the four TP ranks") + for rank, record in ranks.items(): + row = json.loads(checked_record(record, root=root).read_text()) + validate_rank_result(row, expected, int(rank)) + if row.get("provider_binary") != binary: + raise ValueError("Layer ranks did not import the archived CUDA provider") + scheduler_path = checked_record(report.get("scheduler"), root=root) + scheduler = json.loads(scheduler_path.read_text()) + from rl_engine.testing.qwen3_next_l2 import validate_scheduler_report + + validate_scheduler_report(scheduler, expected_identity=identity, artifact_root=root) + if scheduler.get("provider_binary") != report["provider_binary"]: + raise ValueError("Layer and scheduler gates used different CUDA providers") + if scheduler["case_ids"] != requirement["scheduler_case_ids"]: + raise ValueError("Scheduler evidence differs from the pinned layer registry") diff --git a/scripts/qwen3_next_layers.py b/scripts/qwen3_next_layers.py new file mode 100644 index 000000000..07a047011 --- /dev/null +++ b/scripts/qwen3_next_layers.py @@ -0,0 +1,342 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""TP4 layer gates and actual native scheduler acceptance, before independent L2.""" + +import argparse +import importlib +import json +import os +import shutil +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) + +from qwen3_next_contract import ( # noqa: E402 + atomic_json, + checkpoint_identity, + evidence_record, + result_envelope, + validate_result, +) +from qwen3_next_l2 import ( # noqa: E402 + model_identity, + run_process, + verify_before_model_load, + wait_gpu_release, + worker_batch, +) +from qwen3_next_layer_contract import ( # noqa: E402 + layer_requirements, + validate_layer_report, + validate_rank_result, +) +from qwen3_next_runtime import failure_fields # noqa: E402 +from qwen3_next_runtime import ( # noqa: E402 + file_digest, + identity_digest, + memory_limits, + parse_memory_request, + verify_environment, +) + +from rl_engine.testing.qwen3_next_binary import ( # noqa: E402 + archive_provider_binary, + validate_binary_record, +) +from rl_engine.testing.qwen3_next_l2 import ( # noqa: E402 + L2Case, + case_matrix, + compare_batch_prefill, + first_divergent_layer, + load_tensor_artifact, + validate_fixture, + validate_scheduler_report, +) + +SCHEDULER_MODULE = "rl_engine.integrations.qwen3_next_native_rollout" + + +def check_scheduler_ready(): + try: + module = importlib.import_module(SCHEDULER_MODULE) + actual = module.capabilities() + except (ModuleNotFoundError, AttributeError) as exc: + raise RuntimeError(f"native_scheduler_not_ready: {exc}") from exc + required = { + "rollout": True, + "debug_layers": True, + "architecture_layers": 48, + "tp": 4, + "batched_cases": True, + "native_scheduler": True, + } + if any(actual.get(key) != value for key, value in required.items()) or not callable( + getattr(module, "run_rollout_cases", None) + ): + raise RuntimeError(f"native_scheduler_not_ready: required {required}, received {actual}") + return module, actual + + +def scheduler_report(manifest, output, fixture_path, process, provider_binary): + fixture = load_tensor_artifact(fixture_path) + validate_fixture(fixture) + records = {} + for case in case_matrix(): + path = output / "rollout" / f"{case.case_id}.pt" + candidate = load_tensor_artifact(path) + reference = load_tensor_artifact( + output / "rollout" / f"{L2Case(case.prompt_length, 4, 'full').case_id}.pt" + ) + baseline = load_tensor_artifact( + fixture["baseline_artifacts"][str(case.prompt_length)]["path"] + ) + batch = compare_batch_prefill(reference, candidate) + generated = compare_batch_prefill(baseline, candidate) + records[case.case_id] = { + "passed": all(batch.values()) and all(generated.values()), + "batch_prefill": {"rollout": batch}, + "baseline_generation": {"rollout": generated}, + "artifacts": {"rollout": {"path": str(path), "sha256": file_digest(path)}}, + } + if not records[case.case_id]["passed"] and not (output / "first-divergence.json").exists(): + divergent = reference if not all(batch.values()) else baseline + atomic_json( + output / "first-divergence.json", + { + "case_id": case.case_id, + "first_divergent_layer": first_divergent_layer(divergent, candidate), + "candidate_artifact": str(path), + "reference_worker_traces": divergent.get("worker_traces", []), + "candidate_worker_traces": candidate.get("worker_traces", []), + "capture": "all_48_layers_and_owned_states_in_original_worker_traces", + }, + ) + worker_path = output / "rollout/worker-result.json" + report = { + "schema_version": 1, + "status": "passed" if all(row["passed"] for row in records.values()) else "failed", + "scope": "qwen3_next_native_scheduler_only", + "full_model_l2": "not_evaluated", + "raw_bit_comparison": True, + "identity": model_identity(manifest), + "case_ids": [case.case_id for case in case_matrix()], + "cases": records, + "fixture": { + "path": str(fixture_path), + "sha256": file_digest(fixture_path), + "content_sha256": fixture["content_sha256"], + }, + "processes": {"rollout": process}, + "worker_results": { + "rollout": {"path": str(worker_path), "sha256": file_digest(worker_path)} + }, + "provider_binary": provider_binary, + } + atomic_json(output / "scheduler-report.json", report) + validate_scheduler_report( + report, expected_identity=model_identity(manifest), artifact_root=output + ) + return report + + +def run_layers(manifest_path, output): + from qwen3_next_runtime import require_predecessor + + manifest = json.loads(manifest_path.read_text()) + if manifest["stage"] != "layers": + raise ValueError("Layer runner can only close the layers stage") + previous = Path(manifest["previous"]) + require_predecessor("layers", previous, Path(manifest["checkpoint"]), manifest) + verify_environment(manifest["environment"], require_venv=True) + requirement = layer_requirements(ROOT) + if requirement != manifest["layer_requirements"]: + raise ValueError("Layer registry differs from the submitted source snapshot") + memory = memory_limits() + requested = parse_memory_request(manifest["memory"]) + if any( + value is None or value < requested + for value in (memory["effective_bytes"], memory["available_bytes"]) + ): + raise RuntimeError("insufficient_memory: layer allocation cannot satisfy its pinned budget") + output.mkdir(parents=True, exist_ok=True) + report = { + "schema_version": 1, + "status": "incomplete", + "phase": "build", + "scope": "qwen3_next_layer_and_scheduler_gates", + "full_model_l2": "not_evaluated", + "identity": model_identity(manifest), + "requirements_sha256": identity_digest(requirement), + "gates": {}, + } + + def progress(phase): + report["phase"] = phase + atomic_json(output / "layers.json", report) + + run_process( + [manifest["python"], str(ROOT / "setup.py"), "build_ext", "--inplace", "--force"], + log_path=output / "build.log", + timeout=1800, + ) + provider_binary = archive_provider_binary(ROOT, output, source_tree=manifest["source_tree"]) + report["provider_binary"] = provider_binary + actual_binary = validate_binary_record(provider_binary, source_tree=manifest["source_tree"]) + progress("checkpoint") + verify_before_model_load(manifest, output) + probe = output / "gpu-probe.json" + run_process( + [ + manifest["python"], + str(ROOT / "scripts/qwen3_next_l2.py"), + "--gpu-probe", + "--output", + str(probe), + ], + log_path=output / "gpu-probe.log", + timeout=180, + ) + uuids = json.loads(probe.read_text())["uuids"] + wait_gpu_release(uuids) + atomic_json( + output / "environment.json", + { + "environment": manifest["environment"], + "memory": memory, + "gpu_uuids": uuids, + "execution_gpus": 4, + }, + ) + for name, expected in requirement["gates"].items(): + progress(name) + directory = output / name + process = run_process( + [ + manifest["python"], + "-m", + "torch.distributed.run", + "--standalone", + "--nproc_per_node=4", + str(ROOT / expected["script"]), + "--checkpoint", + manifest["checkpoint"], + expected["output_option"], + str(directory), + ], + log_path=output / f"{name}.log", + ) + wait_gpu_release(uuids) + ranks = {} + for rank in range(4): + path = directory / f"rank-{rank}.json" + validate_rank_result(json.loads(path.read_text()), expected, rank) + if json.loads(path.read_text()).get("provider_binary") != actual_binary: + raise ValueError("Layer rank used a different CUDA provider than its build") + ranks[str(rank)] = {"path": str(path), "sha256": file_digest(path)} + report["gates"][name] = {**process, "gpu_release_verified": True, "ranks": ranks} + progress(name + "_passed") + progress("native_scheduler") + check_scheduler_ready() + fixture = output / "inputs/frozen.pt" + process = run_process( + [ + manifest["python"], + str(Path(__file__)), + "--scheduler-worker", + "--manifest", + str(manifest_path), + "--output", + str(output / "rollout"), + "--fixture", + str(fixture), + "--provider-binary", + json.dumps(provider_binary), + ], + log_path=output / "scheduler.log", + ) + wait_gpu_release(uuids) + process["gpu_release_verified"] = True + scheduler_report(manifest, output, fixture, process, provider_binary) + report["scheduler"] = { + "path": str(output / "scheduler-report.json"), + "sha256": file_digest(output / "scheduler-report.json"), + } + if checkpoint_identity(manifest["checkpoint"]) != manifest["checkpoint_identity"]: + raise RuntimeError("checkpoint_mismatch: official snapshot changed during layer execution") + report.update(status="passed", phase="complete") + validate_layer_report(report, requirement, model_identity(manifest), root=output) + atomic_json(output / "layers.json", report) + previous_result = json.loads((previous / "result.json").read_text()) + shutil.copyfile( + previous / previous_result["evidence"]["operators"]["path"], output / "operators.json" + ) + names = { + "operators": "operators.json", + "environment": "environment.json", + "layers": "layers.json", + "checkpoint": "weight-verification.json", + "provider_binary": "provider-binary.json", + } + evidence = { + name: evidence_record(output / filename, manifest_path.parent) + for name, filename in names.items() + } + result = result_envelope(manifest, os.environ["QWEN3_NEXT_ATTEMPT"], evidence=evidence) + validate_result(manifest, result, run_dir=manifest_path.parent) + atomic_json(output / "result.json", result) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--manifest", type=Path) + parser.add_argument("--output", type=Path) + parser.add_argument("--fixture", type=Path) + parser.add_argument("--scheduler-worker", action="store_true") + parser.add_argument("--check-ready", action="store_true") + parser.add_argument("--provider-binary", type=json.loads) + args = parser.parse_args() + if args.check_ready: + _, capabilities = check_scheduler_ready() + print(json.dumps({"status": "ready", "capabilities": capabilities})) + return 0 + if args.manifest is None or args.output is None: + parser.error("--manifest and --output are required") + manifest = json.loads(args.manifest.read_text()) + if args.scheduler_worker: + worker_batch( + manifest, + args.output, + "rollout", + args.fixture, + native_worker=check_scheduler_ready(), + provider_binary=args.provider_binary, + ) + return 0 + args.output.mkdir(parents=True, exist_ok=True) + try: + run_layers(args.manifest, args.output) + except Exception as exc: + kind = str(exc).split(":", 1)[0] + atomic_json( + args.output / "result.json", + { + "run_id": manifest["run_id"], + "attempt": os.environ["QWEN3_NEXT_ATTEMPT"], + "status": "incomplete" if kind == "native_scheduler_not_ready" else "failed", + "phase": ( + "native_scheduler" if kind == "native_scheduler_not_ready" else "layer_gate" + ), + **failure_fields(exc), + "full_model_l2": "not_evaluated", + }, + ) + raise + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/qwen3_next_operator_cases.py b/scripts/qwen3_next_operator_cases.py new file mode 100644 index 000000000..caf139e4c --- /dev/null +++ b/scripts/qwen3_next_operator_cases.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Regenerate ``rl_engine/testing/qwen3_next_operator_cases.json`` from pytest collection. + + python scripts/qwen3_next_operator_cases.py # check, exit 1 if stale + python scripts/qwen3_next_operator_cases.py --write + +The registry pins the exact test IDs the preflight operator gate must run. The +``norm`` and ``core`` suites are mandatory and must pass without skips; +``diagnostics`` holds the approximate cross-provider comparisons, which are +recorded but never gate. Which file and test belongs to which suite is declared +below; the IDs themselves come from collection, so a parametrisation change is +a reviewed registry diff, not a silent change in what the gate runs. +""" + +import argparse +import json +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +REGISTRY = ROOT / "rl_engine/testing/qwen3_next_operator_cases.json" +NORM_FILES = ("tests/test_qwen3_next_norm.py", "tests/check_qwen3_next_norm_providers.py") +CORE_FILES = ( + "tests/check_qwen3_next_attention.py", + "tests/check_qwen3_next_batch_invariance.py", + "tests/check_qwen3_next_conv_bridge.py", + "tests/check_qwen3_next_core_matrix.py", + "tests/check_qwen3_next_forward.py", + "tests/check_qwen3_next_gdn_bridge.py", + "tests/check_qwen3_next_gdn_sequence.py", + "tests/check_qwen3_next_paged_attention.py", + "tests/check_qwen3_next_shared_core.py", +) +DIAGNOSTIC_FILES = ("tests/check_gdn_recurrent_golden.py",) +# Approximate comparisons against alternative providers, collected from the +# mandatory files above but recorded as diagnostics. +DIAGNOSTIC_TESTS = { + "tests/check_qwen3_next_norm_providers.py": ( + "test_ours_tracks_each_vllm_path_within_bounds", + "test_vllm_paths_disagree_only_within_bounds", + ), + "tests/check_qwen3_next_batch_invariance.py": ( + "test_flashinfer_chunked_prefill_deviation_stays_rounding_scale", + "test_triton_chunked_prefill_split_matches_one_pass", + ), +} +DESCRIPTION = ( + "Mandatory norm/core contracts; alternative-provider approximate comparisons are " + "diagnostics only. Generated by scripts/qwen3_next_operator_cases.py; case changes " + "require review before a frozen run." +) + + +def collect(files): + out = subprocess.run( + [sys.executable, "-m", "pytest", "--collect-only", "-q", "-p", "no:cacheprovider", *files], + cwd=ROOT, + capture_output=True, + text=True, + ) + ids = [line for line in out.stdout.splitlines() if "::test_" in line] + if out.returncode or not ids: + raise SystemExit(f"collection failed:\n{out.stdout[-2000:]}\n{out.stderr[-2000:]}") + return ids + + +def _diagnostic(case): + path, name = case.split("::", 1) + return name.split("[", 1)[0] in DIAGNOSTIC_TESTS.get(path, ()) + + +def registry(): + norm, core = collect(NORM_FILES), collect(CORE_FILES) + diagnostics = collect(DIAGNOSTIC_FILES) + [c for c in norm + core if _diagnostic(c)] + return { + "schema_version": 1, + "description": DESCRIPTION, + "suites": { + "norm": sorted(c for c in norm if not _diagnostic(c)), + "core": sorted(c for c in core if not _diagnostic(c)), + "diagnostics": sorted(diagnostics), + }, + } + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--write", action="store_true") + args = parser.parse_args() + text = json.dumps(registry(), indent=2) + "\n" + if args.write: + REGISTRY.write_text(text) + print(f"wrote {REGISTRY.relative_to(ROOT)}") + elif not REGISTRY.is_file() or REGISTRY.read_text() != text: + raise SystemExit(f"{REGISTRY.relative_to(ROOT)} is stale; rerun with --write") + + +if __name__ == "__main__": + main() diff --git a/scripts/qwen3_next_operator_gates.py b/scripts/qwen3_next_operator_gates.py new file mode 100644 index 000000000..d1c45d558 --- /dev/null +++ b/scripts/qwen3_next_operator_gates.py @@ -0,0 +1,151 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Run declared operator acceptance or explicitly separate diagnostic cases.""" + +import argparse +import importlib.metadata +import json +import os +import subprocess +import sys +from pathlib import Path + +from rl_engine.testing.qwen3_next_cases import case_digest, execution_verdict, load_cases + +ROOT = Path(__file__).resolve().parents[1] +MANIFEST = ROOT / "rl_engine/testing/qwen3_next_operator_cases.json" + + +def run_suite(out: Path, suite: str) -> dict: + out.mkdir(parents=True, exist_ok=True) + cases = load_cases(MANIFEST, suite) + diagnostic = suite == "diagnostics" + case_path = out / "case-results.json" + case_path.unlink(missing_ok=True) + env = dict(os.environ) + env["QWEN3_NEXT_ROUTING_EVIDENCE"] = str(out / "gdn-step-routing.json") + env["QWEN3_NEXT_FLASHINFER_EVIDENCE"] = str(out) + proc = subprocess.run( + [ + sys.executable, + "-m", + "pytest", + "-q", + "-p", + "no:cacheprovider", + "-p", + "rl_engine.testing.pytest_case_manifest", + "--case-manifest", + str(MANIFEST), + "--case-suite", + suite, + "--case-report", + str(case_path), + "--junitxml", + str(out / "providers.xml"), + *cases, + ], + cwd=ROOT, + env=env, + check=False, + ) + try: + case_report = json.loads(case_path.read_text()) if case_path.exists() else {} + except json.JSONDecodeError: + case_report = {} + verified = execution_verdict( + cases, case_report.get("collected_case_ids", []), case_report.get("outcomes", {}) + ) + passed = proc.returncode == 0 and case_report.get("passed") is True + passed = passed and verified["passed"] and case_report.get("expected_case_ids") == cases + passed = passed and case_report.get("expected_case_ids_sha256") == case_digest(cases) + reports = {} + if suite in ("norm", "all"): + for op in ("qwen3_next_rms_norm", "rms_norm_gated"): + for gate in ("forward", "gradient"): + name = f"{op}-{gate}.json" + with (out / name).open("w") as stream: + norm = subprocess.run( + [ + sys.executable, + f"scripts/check_{gate}_invariance.py", + "--manifest", + "rl_engine/testing/qwen3_next_norm_manifest.json", + "--op", + op, + "--candidate", + "cuda", + "--backend-profile", + "cuda_bf16", + "--hidden", + "2048", + "--head-dim", + "128", + "--json", + ], + cwd=ROOT, + env=env, + stdout=stream, + check=False, + ) + try: + report = json.loads((out / name).read_text()) + except json.JSONDecodeError: + report = {} + valid = all( + ( + norm.returncode == 0, + report.get("passed") is True, + report.get("workload", {}).get("scope") == "qwen3_next_norm_operators", + ) + ) + reports[name] = { + "passed": valid, + "exit_code": norm.returncode, + "workload": report.get("workload"), + } + passed = passed and valid + status = "passed" if passed else "failed" + if diagnostic: + status = "diagnostic_completed" if passed else "diagnostic_failed" + return { + "scope": "qwen3_next_operator_diagnostics" if diagnostic else "qwen3_next_operators_only", + "status": status, + "suite": suite, + "diagnostic_only": diagnostic, + "full_model_l2": "not_evaluated", + "git_commit": subprocess.check_output( + ["git", "rev-parse", "HEAD"], cwd=ROOT, text=True + ).strip(), + "git_dirty": bool( + subprocess.check_output(["git", "status", "--porcelain"], cwd=ROOT, text=True).strip() + ), + "versions": { + name: importlib.metadata.version(name) + for name in ("torch", "vllm", "transformers", "triton") + }, + "mandatory_case_ids": [] if diagnostic else cases, + "case_ids_sha256": case_digest(cases), + "pytest_exit_code": proc.returncode, + "provider_tests": verified["counts"], + "norm_gates": reports, + } + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--out", type=Path, required=True) + parser.add_argument("--suite", choices=("norm", "core", "all", "diagnostics"), default="all") + args = parser.parse_args() + result = run_suite(args.out.resolve(), args.suite) + partial = args.out / "summary.json.partial" + partial.write_text(json.dumps(result, indent=2) + "\n") + partial.replace(args.out / "summary.json") + print(json.dumps(result, indent=2)) + return 0 if result["status"] in ("passed", "diagnostic_completed") else 1 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/qwen3_next_optimizer_checkpoint.py b/scripts/qwen3_next_optimizer_checkpoint.py new file mode 100644 index 000000000..df6c1c07e --- /dev/null +++ b/scripts/qwen3_next_optimizer_checkpoint.py @@ -0,0 +1,307 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Bounded native DistributedOptimizer parameter-state I/O for fixed TP4/DP1. + +The installed native DP-zero saver packs entire gradient buckets. With CPU Adam +offload and no communication overlap, a bucket can contain most of the model. +This adapter uses the native parameter getters/setters and verifies their exact +DP1 range coverage, while copying at most 64 MiB into each serialized block. +It never implements an optimizer, changes dtype, or changes native state layout. +""" + +import json +from pathlib import Path + +import torch +from qwen3_next_contract import atomic_json + +from rl_engine.integrations.qwen3_next_training import named_trainable_parameters, state_digest + +PARAMETER_KEYS = ("param", "exp_avg", "exp_avg_sq") + + +def _hdo_param_generator(cpu_optimizer): + from megatron.core.optimizer.cpu_offloading.hybrid_optimizer import _param_generator + + return _param_generator(cpu_optimizer) + + +def verify_hdo_optimizer_master_aliases(model, leaves): + """CPU Adam must update the same FP32 master tensors the checkpoint getter exposes. + + The distributed optimizer keys its HybridDeviceOptimizer by the DP-sharded + parameter (``optimizer.param_groups[group][order]`` found through + ``model_param_group_index_map``), not by the model parameter, and the + checkpoint getter returns that shard's ``state["master_param"]``. The FP32 + copy CPU Adam steps (``param_to_fp32_param[shard]``) is a separate tensor + that must hold the same values, and the CPU optimizer's state entry must be + the very dict the HDO keeps for the shard. + """ + try: + from megatron.core.optimizer.cpu_offloading.hybrid_optimizer import HybridDeviceOptimizer + except ModuleNotFoundError: + return + + for record in native_parameter_layout(model, leaves).values(): + leaf = record["leaf"] + hdo = leaf.optimizer + if not isinstance(hdo, HybridDeviceOptimizer): + continue + orig = record["parameter"] + group_index, group_order = leaf.model_param_group_index_map[orig] + shard = hdo.param_groups[group_index]["params"][group_order] + master = leaf._get_main_param_and_optimizer_states(orig)["param"] + hdo_state = hdo.state.get(shard) + if not isinstance(hdo_state, dict) or hdo_state.get("master_param") is not master: + raise ValueError("HDO state master_param is not the checkpoint-visible master") + if shard not in hdo.param_to_fp32_param: + raise ValueError("Native HDO must keep an FP32 copy for each sharded parameter") + fp32_inner = hdo.param_to_fp32_param[shard] + if hdo.param_to_inner_param.get(shard) is not fp32_inner: + raise ValueError("HDO inner optimizer parameter is not the FP32 master copy") + if fp32_inner.dtype != torch.float32 or not torch.equal(master, fp32_inner): + raise ValueError("HDO FP32 master copy diverged from checkpoint-visible master weights") + linked = False + for cpu_optimizer in hdo.cpu_optimizers: + for param in _hdo_param_generator(cpu_optimizer): + if param is not fp32_inner: + continue + if cpu_optimizer.state.get(param) is not hdo_state: + raise ValueError("CPU Adam state is not aliased to the HDO parameter state") + linked = True + if not linked: + raise ValueError("CPU Adam does not own the FP32 master for a model parameter") + + +BLOCK_BYTES = 64 * 1024 * 1024 + + +def native_parameter_layout(model, leaves): + names = {id(value): name for name, value in named_trainable_parameters(model).items()} + records = {} + for leaf_index, leaf in enumerate(leaves): + covered = set() + for buffer_index, dtype_maps in enumerate(leaf.gbuf_ranges): + for dtype, buckets in dtype_maps.items(): + for bucket_index, bucket in enumerate(buckets): + intervals = [] + capacity = leaf.buffers[buffer_index].buckets[bucket_index].grad_data.numel() + for parameter, ranges in bucket["param_map"].items(): + name = names.get(id(parameter)) + if name is None or name in records or parameter in covered: + raise ValueError( + "Native optimizer ranges must partition model parameters" + ) + local, relative = ranges["gbuf_local"], ranges["param"] + if ( + relative.start != 0 + or relative.end != parameter.numel() + or local.end - local.start != parameter.numel() + or not 0 <= local.start < local.end <= capacity + ): + raise ValueError("Native DP1 ranges must cover each complete parameter") + if parameter not in leaf.model_param_group_index_map: + raise ValueError( + "Native bucket parameter is absent from optimizer groups" + ) + intervals.append((local.start, local.end)) + covered.add(parameter) + records[name] = { + "parameter": parameter, + "leaf": leaf, + "layout": { + "leaf": leaf_index, + "buffer": buffer_index, + "dtype": [str(item) for item in dtype], + "bucket": bucket_index, + "bucket_numel": capacity, + "group_order": list(leaf.model_param_group_index_map[parameter]), + "param_range": [relative.start, relative.end], + "local_range": [local.start, local.end], + }, + } + intervals.sort() + if any(right[0] < left[1] for left, right in zip(intervals, intervals[1:])): + raise ValueError("Native optimizer bucket ranges overlap") + if covered != leaf.model_param_group_index_map.keys(): + raise ValueError("Native bucket ranges do not cover all optimizer parameters") + if set(records) != set(names.values()): + raise ValueError("Native optimizer state does not cover all model parameters") + return dict(sorted(records.items())) + + +def native_parameter_states(record): + parameter, leaf = record["parameter"], record["leaf"] + states = leaf._get_main_param_and_optimizer_states(parameter) + if not set(PARAMETER_KEYS).issubset(states) or set(states) - set(PARAMETER_KEYS) - {"step"}: + raise ValueError("Native master weights or Adam moments are missing or unsupported") + for key in PARAMETER_KEYS: + value = states[key] + if ( + not isinstance(value, torch.Tensor) + or value.dtype != torch.float32 + or value.device.type != "cpu" + or value.numel() != parameter.numel() + or not value.is_contiguous() + ): + raise ValueError(f"Invalid fully offloaded native {key}") + if "step" in states: + step = states["step"] + if isinstance(step, torch.Tensor): + if step.device.type != "cpu" or step.numel() != 1: + raise ValueError("Native Adam step must be a CPU scalar") + elif type(step) not in (int, float): + raise ValueError("Native Adam step must be a numeric scalar") + return states + + +def parameter_state_records(model, leaves): + records, byte_count = {}, 0 + for name, record in native_parameter_layout(model, leaves).items(): + states = native_parameter_states(record) + records[name] = {key: state_digest(value) for key, value in states.items()} + byte_count += sum( + value.numel() * value.element_size() + for value in states.values() + if isinstance(value, torch.Tensor) + ) + return records, byte_count + + +def save_bounded_parameter_state(model, leaves, rank_dir, save_tensor_file): + rank_dir = Path(rank_dir) + directory = rank_dir / "optimizer-state" + directory.mkdir() + index = { + "schema_version": 1, + "scope": "native_dp1_parameter_state", + "block_bytes": BLOCK_BYTES, + "parameters": {}, + } + extras = {} + width = BLOCK_BYTES // (len(PARAMETER_KEYS) * 4) + for parameter_index, (name, record) in enumerate( + native_parameter_layout(model, leaves).items() + ): + states = native_parameter_states(record) + entry = { + "layout": record["layout"], + "shapes": {key: list(states[key].shape) for key in PARAMETER_KEYS}, + "blocks": [], + } + count = record["parameter"].numel() + for block_index, start in enumerate(range(0, count, width)): + end = min(start + width, count) + filename = f"optimizer-state/p{parameter_index:05d}-b{block_index:05d}.pt" + # Clone bounded views: torch.save otherwise serializes their entire + # underlying native storage, which can itself be a whole buffer. + block = {key: states[key].reshape(-1)[start:end].clone() for key in PARAMETER_KEYS} + save_tensor_file(rank_dir / filename, block) + del block + entry["blocks"].append({"path": filename, "start": start, "end": end}) + extras[name] = { + key: value.clone() if isinstance(value, torch.Tensor) else value + for key, value in states.items() + if key not in PARAMETER_KEYS + } + index["parameters"][name] = entry + save_tensor_file(rank_dir / "optimizer-extra.pt", extras) + atomic_json(rank_dir / "optimizer-index.json", index) + return index + + +def parameter_state_files(rank_dir, index=None): + rank_dir = Path(rank_dir) + if index is None: + index = json.loads((rank_dir / "optimizer-index.json").read_text()) + if (index.get("schema_version"), index.get("scope"), index.get("block_bytes")) != ( + 1, + "native_dp1_parameter_state", + BLOCK_BYTES, + ): + raise ValueError("Unsupported native bounded optimizer checkpoint") + files = ["optimizer-header.pt", "optimizer-index.json", "optimizer-extra.pt"] + for entry in index["parameters"].values(): + for block in entry["blocks"]: + path = Path(block["path"]) + if len(path.parts) != 2 or path.parts[0] != "optimizer-state" or path.suffix != ".pt": + raise ValueError("Optimizer block path is outside its rank state directory") + files.append(str(path)) + if len(files) != len(set(files)): + raise ValueError("Native optimizer blocks cannot alias another parameter's state") + return files + + +def load_bounded_parameter_state(model, leaves, rank_dir): + rank_dir = Path(rank_dir) + index = json.loads((rank_dir / "optimizer-index.json").read_text()) + parameter_state_files(rank_dir, index) + current = native_parameter_layout(model, leaves) + if current.keys() != index["parameters"].keys(): + raise ValueError("Saved native optimizer parameter inventory differs") + extras = torch.load(rank_dir / "optimizer-extra.pt", map_location="cpu", weights_only=True) + if extras.keys() != current.keys(): + raise ValueError("Saved native optimizer extra-state inventory differs") + width = BLOCK_BYTES // (len(PARAMETER_KEYS) * 4) + touched_leaves = [] + with torch.no_grad(): + for name, record in current.items(): + entry = index["parameters"][name] + states = native_parameter_states(record) + shapes = {key: list(states[key].shape) for key in PARAMETER_KEYS} + if entry["layout"] != record["layout"] or entry["shapes"] != shapes: + raise ValueError("Saved native optimizer layout or state shapes differ") + position = 0 + for block in entry["blocks"]: + start, end = block["start"], block["end"] + if ( + type(start) is not int + or type(end) is not int + or start != position + or not start < end <= min(start + width, record["parameter"].numel()) + ): + raise ValueError( + "Native optimizer blocks must cover each parameter exactly once" + ) + payload = torch.load( + rank_dir / block["path"], map_location="cpu", weights_only=True + ) + if payload.keys() != set(PARAMETER_KEYS): + raise ValueError("Native optimizer block omits masters or moments") + for key in PARAMETER_KEYS: + value = payload[key] + if ( + value.dtype != torch.float32 + or value.shape != (end - start,) + or not torch.isfinite(value).all() + ): + raise ValueError("Native optimizer block shape/dtype/finite check failed") + states[key].reshape(-1)[start:end].copy_(value) + del payload + position = end + if position != record["parameter"].numel(): + raise ValueError("Native optimizer blocks do not cover the complete parameter") + if extras[name].keys() != states.keys() - set(PARAMETER_KEYS): + raise ValueError("Native optimizer extra-state schema differs") + for key, value in extras[name].items(): + if isinstance(states[key], torch.Tensor): + if not isinstance(value, torch.Tensor) or value.dtype != states[key].dtype: + raise ValueError("Native Adam scalar dtype differs") + states[key].copy_(value) + else: + if type(value) is not type(states[key]): + raise ValueError("Native Adam scalar type differs") + states[key] = value + record["leaf"]._set_main_param_and_optimizer_states(record["parameter"], states) + if record["leaf"] not in touched_leaves: + touched_leaves.append(record["leaf"]) + try: + from megatron.core.optimizer.cpu_offloading.hybrid_optimizer import HybridDeviceOptimizer + except ModuleNotFoundError: + return + + for leaf in touched_leaves: + hdo = leaf.optimizer + if isinstance(hdo, HybridDeviceOptimizer): + hdo._sync_hdo_state_to_sub_optimizers() diff --git a/scripts/qwen3_next_preflight.py b/scripts/qwen3_next_preflight.py new file mode 100644 index 000000000..2b46c266c --- /dev/null +++ b/scripts/qwen3_next_preflight.py @@ -0,0 +1,252 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Required four-GPU infrastructure checks, never full-model L2 acceptance.""" + +import argparse +import importlib.metadata +import json +import os +import platform +import subprocess +import sys +from pathlib import Path + +from qwen3_next_contract import checkpoint_identity, evidence_record, result_envelope +from qwen3_next_runtime import ( + atomic_json, + failure_fields, + memory_limits, + parse_memory_request, + subprocess_failure, + verify_checkpoint, + verify_environment, +) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--manifest", type=Path, required=True) + parser.add_argument("--output", type=Path, required=True) + args = parser.parse_args() + manifest = json.loads(args.manifest.read_text()) + output = args.output + output.mkdir(parents=True, exist_ok=True) + progress = { + "run_id": manifest["run_id"], + "attempt": os.environ["QWEN3_NEXT_ATTEMPT"], + "status": "running", + "phase": "inventory", + "full_model_l2": "not_evaluated", + } + + def run(name, command, env=None): + progress["phase"] = name + atomic_json(output / "progress.json", progress) + print(f"PREFLIGHT {name}: {command}", flush=True) + log_path = output / f"{name}.log" + with log_path.open("w") as log: + completed = subprocess.run(command, stdout=log, stderr=subprocess.STDOUT, env=env) + if completed.returncode: + raise subprocess_failure(completed.returncode, log_path) + + try: + verify_environment(manifest["environment"]) + if checkpoint_identity(manifest["checkpoint"]) != manifest["checkpoint_identity"]: + raise RuntimeError("checkpoint_mismatch: pinned metadata or shard inventory changed") + import torch + + versions = { + name: importlib.metadata.version(name) + for name in ( + "torch", + "vllm", + "triton", + "transformers", + "flashinfer-python", + "safetensors", + ) + } + nvcc = subprocess.check_output( + [f"{manifest['cuda_home']}/bin/nvcc", "--version"], text=True + ) + inventory = { + "source_commit": manifest["source_commit"], + "environment_sha256": manifest["environment_sha256"], + "visible_gpu_ids": os.environ.get("CUDA_VISIBLE_DEVICES"), + "execution_gpus": 4, + "python": sys.version, + "executable": sys.executable, + "platform": platform.platform(), + "versions": versions, + "torch_cuda": torch.version.cuda, + "nvcc": nvcc, + "compiler": subprocess.check_output(["g++", "--version"], text=True), + "nvidia_smi": subprocess.check_output(["nvidia-smi"], text=True), + "gpu_topology": subprocess.check_output(["nvidia-smi", "topo", "-m"], text=True), + "visible_gpus": [ + torch.cuda.get_device_properties(i).__repr__() + for i in range(torch.cuda.device_count()) + ], + "memory": memory_limits(), + "memory_requested": manifest.get("memory"), + } + atomic_json(output / "environment.json", inventory) + run("environment-lock", [sys.executable, "-m", "pip", "freeze", "--all"]) + if torch.cuda.device_count() != 4 or any( + "B200" not in torch.cuda.get_device_name(i) for i in range(4) + ): + raise RuntimeError( + "topology_mismatch: requires exactly four B200 GPUs in the execution step" + ) + if "V13.0.88" not in nvcc or torch.version.cuda != "13.0": + raise RuntimeError( + "environment_mismatch: requires CUDA toolkit 13.0.88 and a CUDA 13.0 torch runtime" + ) + for name, required in { + "torch": "2.13.0", + "vllm": "0.30.0", + "triton": "3.7.1", + "transformers": "5.17.0", + }.items(): + if versions[name].split("+", 1)[0] != required: + raise RuntimeError( + f"environment_mismatch: unverified runtime version: {name}={versions[name]}" + ) + memory = inventory["memory"] + quota = memory["effective_bytes"] + # Compare against what THIS run requested, recorded by the submitter, so + # the budget is not a second constant that has to be kept in sync -- and + # so a trimmed allocation fails loudly instead of passing a stale bound. + if "memory" not in manifest: + raise RuntimeError("Manifest does not record the memory request") + requested = parse_memory_request(manifest["memory"]) + if quota is None or quota < requested: + raise RuntimeError( + f"insufficient_memory: node granted less memory than this run requested " + f"({manifest['memory']} = {requested} bytes): " + f"effective={quota}, cgroup={memory['cgroup_bytes']}, " + f"declared={memory['declared_bytes']}" + ) + # A grant is not availability on a shared node. Fail in seconds rather + # than being OOM-killed minutes into a 162 GB model load. + available = memory["available_bytes"] + if available is None or available < requested: + raise RuntimeError( + f"insufficient_memory: node does not currently have the requested memory free " + f"({manifest['memory']} = {requested} bytes): " + f"MemAvailable={available}. Co-tenants can hold host RAM that an " + "allocation nominally grants; use an exclusive allocation or wait " + "for the node to drain." + ) + run("build", [sys.executable, "setup.py", "build_ext", "--inplace"]) + env = { + **os.environ, + "PY": sys.executable, + "QWEN3_NEXT_EVIDENCE_DIR": str(output / "operators"), + "QWEN3_NEXT_OPERATOR_SUITE": manifest.get("suite", "all"), + } + run("operators", ["bash", "ci/run_qwen3_next_operator_gates.sh"], env) + if manifest["stage"] == "preflight" or manifest.get("suite") in ("norm", "all"): + run( + "device-guards", + [ + sys.executable, + "-m", + "pytest", + "-q", + "tests/check_qwen3_next_multigpu.py", + f"--junitxml={output}/device-guards.xml", + ], + ) + from xml.etree import ElementTree + + suites = list( + ElementTree.parse(output / "device-guards.xml").getroot().iter("testsuite") + ) + if not suites or any( + int(s.get(k, 0)) for s in suites for k in ("skipped", "errors", "failures") + ): + raise RuntimeError("Device guards must pass without skips") + checkpoint = None + if manifest["stage"] == "preflight": + run( + "tp4", + [ + sys.executable, + "-m", + "torch.distributed.run", + "--standalone", + "--nproc_per_node=4", + "scripts/qwen3_next_tp_probe.py", + "--output", + str(output / "tp4"), + ], + ) + ranks = [ + json.loads((output / "tp4" / f"rank-{rank}.json").read_text()) for rank in range(4) + ] + if any( + row.get("status") != "passed" + or row.get("rank") != rank + or row.get("world_size") != 4 + for rank, row in enumerate(ranks) + ): + raise RuntimeError("All four TP ranks must pass") + atomic_json(output / "tp4.json", {"status": "passed", "ranks": ranks}) + progress["phase"] = "checkpoint-integrity" + atomic_json(output / "progress.json", progress) + identity = json.loads( + Path("rl_engine/testing/qwen3_next_norm_manifest.json").read_text() + ) + checkpoint = verify_checkpoint(Path(manifest["checkpoint"]), identity["model_identity"]) + atomic_json(output / "checkpoint.json", checkpoint) + run( + "rollout-smoke", + [ + sys.executable, + "scripts/qwen3_next_rollout_smoke.py", + "--model", + manifest["checkpoint"], + "--output", + str(output / "rollout-smoke.json"), + "--tensor-parallel-size", + "4", + "--non-strict-smoke", + ], + ) + import rl_engine._C + from rl_engine.integrations.qwen3_next_gdn import _provider + + provider = _provider() + names = {"operators": "operators/summary.json", "environment": "environment.json"} + if manifest["stage"] == "preflight" or manifest.get("suite") in ("norm", "all"): + names["device_guards"] = "device-guards.xml" + if manifest["stage"] == "preflight": + names.update( + tp4="tp4.json", checkpoint="checkpoint.json", rollout_smoke="rollout-smoke.json" + ) + evidence = { + name: evidence_record(output / filename, args.manifest.parent) + for name, filename in names.items() + } + result = result_envelope(manifest, os.environ["QWEN3_NEXT_ATTEMPT"], evidence=evidence) + result.update( + checkpoint=checkpoint, + provider_paths={ + "norm": rl_engine._C.__file__, + "gdn": f"{provider.__module__}.{provider.__name__}", + }, + ) + atomic_json(output / "result.json", result) + except Exception as exc: + progress.update( + status="failed", attempt=os.environ["QWEN3_NEXT_ATTEMPT"], **failure_fields(exc) + ) + atomic_json(output / "result.json", progress) + raise + + +if __name__ == "__main__": + main() diff --git a/scripts/qwen3_next_publish_evidence.py b/scripts/qwen3_next_publish_evidence.py new file mode 100644 index 000000000..1cb6cb2e7 --- /dev/null +++ b/scripts/qwen3_next_publish_evidence.py @@ -0,0 +1,142 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Copy a finished Qwen3-Next chain's evidence into the repository, without site details. + + python scripts/qwen3_next_publish_evidence.py --last \\ + --chain-report chain-report.json --out docs/usage/evidence/qwen3-next-chain- + +For every stage run on the chain (preflight -> ... -> the given run) this copies +``run.json``, ``result.json`` and every evidence file the result names (JSON +and XML only; tensor artifacts stay with the run), plus the chain report. Each +absolute root is replaced by a placeholder - ````, ````, +````, ````, ````, ```` - and host and user names +by ```` / ````. The copy fails if any file still contains a +site-specific path or name afterwards. ``redaction.json`` records what was +replaced and ``SHA256SUMS`` the published bytes; ``measured-commit.txt`` names +the commit the stages ran. +""" + +import argparse +import getpass +import hashlib +import json +import re +import shutil +import socket +from pathlib import Path + +FORBIDDEN = ( + r"/data/", + r"/home/", + r"/raid/", + r"/scratch", + r"\bdgx-", + r"SLURM_", +) +TEXT_SUFFIXES = (".json", ".xml", ".txt", ".log") + + +def chain_runs(last): + runs, run = [], Path(last).resolve() + while run is not None: + manifest = json.loads((run / "run.json").read_text()) + runs.append((manifest, run)) + run = Path(manifest["previous"]) if manifest.get("previous") else None + if runs[-1][0]["stage"] != "preflight": + raise SystemExit("The chain does not lead back to a preflight run") + if len({manifest["source_commit"] for manifest, _ in runs}) != 1: + raise SystemExit("The chain mixes source commits") + return list(reversed(runs)) + + +def replacements(runs): + """Longest-first (path, placeholder) pairs for every root the chain recorded.""" + pairs = {} + for manifest, run in runs: + pairs[str(run)] = "" + pairs[str(run.parent)] = "" + pairs[manifest["checkpoint"]] = "" + pairs[manifest["cuda_home"]] = "" + pairs[manifest["python"]] = "" + python_root = Path(manifest["python"]).parents[1] + pairs[str(python_root)] = "" + if manifest.get("training_storage"): + pairs[manifest["training_storage"]] = "" + pairs[str(Path.home())] = "" + return sorted(pairs.items(), key=lambda item: -len(item[0])) + + +def redact(text, pairs, counts): + for value, placeholder in pairs: + if value and value in text: + counts[placeholder] = counts.get(placeholder, 0) + text.count(value) + text = text.replace(value, placeholder) + for pattern, placeholder in ( + (r"/[^\s\"']*/site-packages", ""), + (r"\bdgx-[A-Za-z0-9-]+", ""), + (rf"\b{re.escape(socket.gethostname())}\b", ""), + (rf"\b{re.escape(getpass.getuser())}\b", ""), + ): + text, count = re.subn(pattern, placeholder, text) + if count: + counts[placeholder] = counts.get(placeholder, 0) + count + return text + + +def leftovers(text): + return [pattern for pattern in FORBIDDEN if re.search(pattern, text)] + + +def publish(runs, chain_report, out): + out.mkdir(parents=True, exist_ok=False) + pairs, counts, files = replacements(runs), {}, [] + for manifest, run in runs: + stage_dir = out / manifest["stage"] + stage_dir.mkdir() + result = json.loads((run / "result.json").read_text()) + sources = {"run.json": run / "run.json", "result.json": run / "result.json"} + for name, record in result.get("evidence", {}).items(): + path = run / record["path"] + if path.suffix in TEXT_SUFFIXES and path.is_file(): + sources[f"{name}{path.suffix}"] = path + for name, source in sources.items(): + target = stage_dir / name + target.write_text(redact(source.read_text(), pairs, counts)) + files.append(target) + if chain_report is not None: + target = out / "chain-report.json" + target.write_text(redact(Path(chain_report).read_text(), pairs, counts)) + files.append(target) + (out / "measured-commit.txt").write_text(runs[0][0]["source_commit"] + "\n") + (out / "redaction.json").write_text( + json.dumps({"placeholders": dict(sorted(counts.items()))}, indent=2) + "\n" + ) + files += [out / "measured-commit.txt", out / "redaction.json"] + dirty = {str(f.relative_to(out)): leftovers(f.read_text()) for f in files} + dirty = {name: found for name, found in dirty.items() if found} + if dirty: + shutil.rmtree(out) + raise SystemExit(f"Site-specific text survived redaction: {dirty}") + sums = [ + f"{hashlib.sha256(f.read_bytes()).hexdigest()} {f.relative_to(out)}" for f in sorted(files) + ] + (out / "SHA256SUMS").write_text("\n".join(sums) + "\n") + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--last", type=Path, required=True) + parser.add_argument("--chain-report", type=Path) + parser.add_argument("--out", type=Path, required=True) + args = parser.parse_args() + runs = chain_runs(args.last) + if any(not (run / "result.json").is_file() for _, run in runs): + raise SystemExit("Every stage on the chain must have finished") + publish(runs, args.chain_report, args.out) + print(f"published {len(runs)} stages to {args.out}") + + +if __name__ == "__main__": + main() diff --git a/scripts/qwen3_next_rollout_smoke.py b/scripts/qwen3_next_rollout_smoke.py new file mode 100644 index 000000000..8ebb72c84 --- /dev/null +++ b/scripts/qwen3_next_rollout_smoke.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Real-checkpoint rollout diagnostic. This does not perform VIME or L2 validation.""" + +import argparse +import json +import os +from pathlib import Path + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--model", type=Path, required=True) + parser.add_argument("--output", type=Path, required=True) + parser.add_argument("--tensor-parallel-size", type=int, default=1) + parser.add_argument("--non-strict-smoke", action="store_true") + args = parser.parse_args() + config = json.loads((args.model / "config.json").read_text()) + if config.get("model_type") != "qwen3_next" or config.get("num_hidden_layers") != 48: + raise SystemExit("Expected the full Qwen3-Next checkpoint") + os.environ["VLLM_BATCH_INVARIANT"] = "0" if args.non_strict_smoke else "1" + os.environ["VLLM_PLUGINS"] = "" + from vllm import LLM, SamplingParams + + model = LLM( + model=str(args.model.resolve()), + dtype="bfloat16", + tensor_parallel_size=args.tensor_parallel_size, + gpu_memory_utilization=0.93, + max_model_len=512, + max_num_seqs=2, + enforce_eager=True, + enable_prefix_caching=False, + seed=1234, + mamba_cache_dtype="bfloat16", + mamba_ssm_cache_dtype="float32", + kernel_config={"moe_backend": "triton", "enable_flashinfer_autotune": False}, + ) + outputs = model.generate( + ["The capital of France is", "Calculate 7 plus 5. The result is"], + SamplingParams(temperature=0.0, top_p=1.0, max_tokens=8, logprobs=1), + ) + results = [] + for output in outputs: + response = output.outputs[0] + results.append( + { + "prompt_token_ids": output.prompt_token_ids, + "response_token_ids": list(response.token_ids), + "selected_logprobs": [ + row[token].logprob for row, token in zip(response.logprobs, response.token_ids) + ], + "text": response.text, + } + ) + payload = { + "scope": "rollout_smoke_only", + "full_model_l2": "not_evaluated", + "batch_invariant_requested": not args.non_strict_smoke, + "moe_backend": "triton", + "model": str(args.model.resolve()), + "tensor_parallel_size": args.tensor_parallel_size, + "results": results, + } + args.output.parent.mkdir(parents=True, exist_ok=True) + args.output.write_text(json.dumps(payload, indent=2) + "\n") + print("REAL_CHECKPOINT_ROLLOUT_SMOKE_COMPLETE", flush=True) + + +if __name__ == "__main__": + main() diff --git a/scripts/qwen3_next_run.py b/scripts/qwen3_next_run.py new file mode 100644 index 000000000..db8133d94 --- /dev/null +++ b/scripts/qwen3_next_run.py @@ -0,0 +1,337 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Run the Qwen3-Next full-model chain inside any allocation with four B200 GPUs. + + python scripts/qwen3_next_run.py --stage all \\ + --runs-root --checkpoint --cuda-home + +Stages run in order, each in its own run directory and its own clean clone of +the current commit; ``--from``/``--until`` select a contiguous part. The stages +are ``preflight`` (environment, operator gates, device guards, TP4 probe, +checkpoint hashes, rollout smoke), ``layers`` (per-layer and +scheduler gates, which certify the shared provider), ``l2`` (full-model +train/prefill/decode raw-bit matrix), then ``train1``, ``train2`` and ``train`` +(two optimizer updates, export, L2 on the updated weights, reload and +resumption). A single stage takes ``--previous `` of its predecessor. + +Every run directory holds ``run.json`` (the immutable manifest, identified by a +fresh ``execution_id``), ``attempts//`` (the stage's output) and, once the +stage passes, ``result.json``. The launcher refuses a dirty tree and never +talks to a batch scheduler; wrap it in whatever allocation command the cluster +uses. +""" + +import argparse +import json +import os +import signal +import subprocess +import sys +from datetime import datetime, timezone +from pathlib import Path +from uuid import uuid4 + +from qwen3_next_contract import ( + CONTRACT_VERSION, + checkpoint_identity, + operator_requirements, + validate_result, +) +from qwen3_next_layer_contract import layer_requirements +from qwen3_next_runtime import ( + STAGES, + atomic_json, + failure_fields, + file_digest, + interpreter_path, + probe_environment, + require_predecessor, + verify_environment, + wait_owned_child, +) + +ROOT = Path(__file__).resolve().parents[1] +ORDER = ("preflight", "layers", "l2", "train1", "train2", "train") +PROVIDER_SOURCES = ( + "rl_engine/integrations/qwen3_next_provider.py", + "rl_engine/integrations/qwen3_next_conv.py", + "rl_engine/integrations/qwen3_next_gdn.py", + "rl_engine/integrations/qwen3_next_engine_weights.py", + "rl_engine/integrations/qwen3_next_profile.py", + "rl_engine/integrations/qwen3_next_tp.py", + "rl_engine/integrations/qwen3_next_tp_gdn.py", + "rl_engine/integrations/qwen3_next_tp_blocks.py", + "rl_engine/integrations/qwen3_next_forward.py", + "rl_engine/integrations/qwen3_next_embedding.py", + "rl_engine/integrations/qwen3_next_logprob.py", + "rl_engine/integrations/qwen3_next_vime.py", + "rl_engine/integrations/qwen3_next_vllm.py", + "rl_engine/integrations/qwen3_next_vllm_attention.py", + "rl_engine/integrations/qwen3_next_vllm_model.py", + "rl_engine/kernels/ops/cuda/norm/rmsnorm.py", + "csrc/cuda/rmsnorm.cu", +) + + +def _git(*args, cwd=ROOT): + return subprocess.check_output(["git", *args], cwd=cwd, text=True).strip() + + +def provider_config(): + return { + "profile": "qwen3_next_tp4_shared_eager_v1", + "scope": "shared_full_forward_implementation", + "scheduler_batch_invariance_certified": False, + "sources": {name: file_digest(ROOT / name) for name in PROVIDER_SOURCES}, + "weights": "bfloat16", + "recurrent_state": "float32", + "mtp": False, + "prefix_reuse": False, + "dropout": False, + "dynamic_tuning": False, + "cuda_graph": False, + "sequence_parallel": False, + "fp32_residual_connection": False, + "vocab_size": 151936, + "padded_vocab_size": 151936, + "expert_tensor_parallel_size": 4, + # Large enough that the batch-invariance matrix (up to 64 concurrent + # requests) runs under the same certified provider identity. + "native_rollout_kv_cache_memory_bytes": 4 << 30, + "runtime_env": {"TRITON_F32_DEFAULT": "ieee", "VLLM_USE_V2_MODEL_RUNNER": "0"}, + } + + +def prepare(stage, previous, args): + """Create the run directory, its clean source clone and its immutable manifest.""" + if _git("status", "--porcelain", "--untracked-files=no"): + raise SystemExit("Commit the source before starting an immutable run") + commit = _git("rev-parse", "HEAD") + checkpoint = args.checkpoint.resolve(strict=True) + stamp = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ") + execution_id = uuid4().hex + run_id = f"{stamp}-{stage}-{commit[:8]}-{execution_id[:8]}" + run_dir = args.runs_root.resolve() / run_id + run_dir.mkdir(parents=True) + source = run_dir / "source" + subprocess.run( + ["git", "clone", "-q", "--shared", "--no-checkout", str(ROOT), str(source)], check=True + ) + subprocess.run(["git", "checkout", "-q", "--detach", commit], cwd=source, check=True) + python = interpreter_path(args.python) + manifest = { + "run_id": run_id, + "execution_id": execution_id, + "contract_version": CONTRACT_VERSION, + "suite": "all", + "execution_gpus": 4, + "source_tree": _git("rev-parse", "HEAD^{tree}"), + "stage": stage, + "source_commit": commit, + "source": str(source), + "python": python, + "cuda_home": str(args.cuda_home.resolve(strict=True)), + "checkpoint": str(checkpoint), + "memory": args.memory or STAGES[stage][0], + "previous": str(previous) if previous else None, + "status": "prepared", + "full_model_l2": "not_evaluated", + } + if args.training_storage_root and stage == "train1": + manifest["training_storage"] = str(args.training_storage_root.resolve(strict=True) / run_id) + environment = probe_environment(python) + manifest["environment"] = environment + manifest["environment_sha256"] = environment["sha256"] + manifest["cuda_identity"] = { + "home": manifest["cuda_home"], + "nvcc_sha256": file_digest(Path(manifest["cuda_home"]) / "bin/nvcc"), + } + manifest["checkpoint_identity"] = checkpoint_identity(checkpoint) + manifest["provider_config"] = provider_config() + manifest["operator_requirements"] = operator_requirements(ROOT, "all") + manifest["layer_requirements"] = layer_requirements(ROOT) + manifest["topology"] = {"tp": 4, "pp": 1, "cp": 1, "ep": 1} + require_predecessor(stage, previous, checkpoint, manifest) + if stage == "l2": + certificate = previous / "result.json" + manifest["scheduler_profile_certificate"] = { + "path": str(certificate), + "sha256": file_digest(certificate), + } + elif stage in ("train1", "train2", "train"): + manifest["scheduler_profile_certificate"] = json.loads((previous / "run.json").read_text())[ + "scheduler_profile_certificate" + ] + atomic_json(run_dir / "run.json", manifest) + return run_dir, manifest + + +def stage_environment(manifest, run_dir, attempt): + env = os.environ.copy() + env.pop("PYTHONHOME", None) + env.update( + CUDA_HOME=manifest["cuda_home"], + PYTHONUNBUFFERED="1", + VLLM_PLUGINS="", + MAX_JOBS="4", + KERNEL_ALIGN_DEV_RPATH="1", + QWEN3_NEXT_ATTEMPT=attempt, + QWEN3_NEXT_ALLOCATED_MEMORY=manifest["memory"], + # Four native ranks share the host. Left unpinned, each rank spins a + # thread per core on the ~50k small per-tensor loads of the HF import. + OMP_NUM_THREADS="16", + MKL_NUM_THREADS="16", + ) + # Layer gates qualify the providers without vLLM's aten overrides; the + # certified full-model stages run with them. + if manifest["stage"] in ("preflight", "layers"): + env["VLLM_BATCH_INVARIANT"] = "0" + else: + env["VLLM_BATCH_INVARIANT"] = "1" + env["QWEN_SHARED_PROFILE_RESULT"] = manifest["scheduler_profile_certificate"]["path"] + env.update(manifest["provider_config"]["runtime_env"]) + base_bin = Path(manifest["environment"]["identity"]["base_prefix"]) / "bin" + env["PATH"] = ( + f"{Path(manifest['python']).parent}:{manifest['cuda_home']}/bin:{base_bin}:{env['PATH']}" + ) + env["PYTHONPATH"] = manifest["source"] + env["TRITON_CACHE_DIR"] = str(run_dir / "cache/triton") + env["TORCH_EXTENSIONS_DIR"] = str(run_dir / "cache/torch") + return env + + +def execute(run_dir, manifest): + """Run one stage in its clone; on success write and validate ``result.json``.""" + attempt = "0" + output = run_dir / "attempts" / attempt + output.mkdir(parents=True) + status = { + "run_id": manifest["run_id"], + "execution_id": manifest["execution_id"], + "stage": manifest["stage"], + "attempt": attempt, + "status": "running", + "full_model_l2": "not_evaluated", + } + atomic_json(output / "status.json", status) + (run_dir / "RUNNING").touch() + received = None + + def stop(signum, frame): + nonlocal received + received = received or signum + + for sig in (signal.SIGUSR1, signal.SIGTERM, signal.SIGINT): + signal.signal(sig, stop) + child = None + try: + verify_environment(manifest["environment"], require_venv=manifest["stage"] != "preflight") + if manifest["environment"]["identity"]["build_tools"]["ninja"] is None: + raise RuntimeError("environment_mismatch: locked Ninja build tool is missing") + source = manifest["source"] + if _git("rev-parse", "HEAD^{tree}", cwd=source) != manifest["source_tree"] or _git( + "status", "--porcelain", "--untracked-files=no", cwd=source + ): + raise RuntimeError("source_mismatch: frozen tracked source changed") + if ( + file_digest(Path(manifest["cuda_home"]) / "bin/nvcc") + != manifest["cuda_identity"]["nvcc_sha256"] + ): + raise RuntimeError("environment_mismatch: CUDA toolkit changed") + atomic_json(output / "runtime-identity.json", manifest["environment"]) + command = [ + manifest["python"], + str(Path(source) / STAGES[manifest["stage"]][3]), + "--manifest", + str(run_dir / "run.json"), + "--output", + str(output), + ] + child = subprocess.Popen( + command, + cwd=source, + env=stage_environment(manifest, run_dir, attempt), + start_new_session=True, + ) + code = wait_owned_child(child, lambda: received is not None) + if received: + status.update(status="interrupted", signal=received, failure_kind="unknown") + elif code: + status.update( + status="failed", + exit_code=code, + **failure_fields(RuntimeError(f"Stage exited with code {code}")), + ) + failure = output / "result.json" + if failure.is_file(): + record = json.loads(failure.read_text()) + status.update({k: record[k] for k in ("failure_kind", "error") if k in record}) + else: + result = json.loads((output / "result.json").read_text()) + validate_result(manifest, result, run_dir=run_dir, attempt=attempt) + status.update(result) + atomic_json(run_dir / "result.json", result) + except Exception as exc: + status.update(status="interrupted" if received else "failed", **failure_fields(exc)) + raise + finally: + if child is not None and child.poll() is None: + wait_owned_child(child, lambda: True) + atomic_json(output / "status.json", status) + (run_dir / "RUNNING").unlink(missing_ok=True) + return status + + +def main(): + parser = argparse.ArgumentParser( + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter + ) + parser.add_argument("--stage", choices=(*ORDER, "all"), required=True) + parser.add_argument("--runs-root", type=Path, required=True) + parser.add_argument("--checkpoint", type=Path, required=True) + parser.add_argument("--cuda-home", type=Path, required=True) + parser.add_argument("--python", type=Path, default=Path(sys.executable)) + parser.add_argument("--previous", type=Path, help="passed run directory of the predecessor") + parser.add_argument( + "--from", + dest="first", + choices=ORDER, + default="preflight", + help="with --stage all: first stage to run (later than preflight needs --previous)", + ) + parser.add_argument( + "--until", choices=ORDER, default="train", help="with --stage all: last stage to run" + ) + parser.add_argument( + "--memory", + help="host memory this allocation grants (e.g. 1280G); default: the stage's budget", + ) + parser.add_argument( + "--training-storage-root", + type=Path, + help="shared directory for training checkpoints and exports (default: in the run)", + ) + args = parser.parse_args() + if args.stage == "all": + stages = ORDER[ORDER.index(args.first) : ORDER.index(args.until) + 1] + if not stages: + raise SystemExit("--from must not come after --until") + else: + stages = (args.stage,) + if (stages[0] == "preflight") != (args.previous is None): + raise SystemExit("Every stage after preflight needs --previous ") + previous = args.previous.resolve(strict=True) if args.previous else None + for stage in stages: + run_dir, manifest = prepare(stage, previous, args) + print(f"[qwen3-next] {stage}: {run_dir}", flush=True) + status = execute(run_dir, manifest) + print(f"[qwen3-next] {stage}: {status['status']}", flush=True) + if status["status"] != "passed": + raise SystemExit(1) + previous = run_dir + + +if __name__ == "__main__": + main() diff --git a/scripts/qwen3_next_runtime.py b/scripts/qwen3_next_runtime.py new file mode 100644 index 000000000..321597ac5 --- /dev/null +++ b/scripts/qwen3_next_runtime.py @@ -0,0 +1,772 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Scheduler-independent runtime for the Qwen3-Next stage runners. + +Environment identity, failure classification, memory preconditions, owned child +processes, checkpoint verification, stage ordering and crash-recoverable +training storage. Nothing here talks to a batch scheduler: a run is identified +by the ``execution_id`` that ``scripts/qwen3_next_run.py`` writes into +``run.json``, and any allocation that provides four GPUs can host it. +""" + +import errno +import hashlib +import importlib.metadata +import json +import os +import re +import shutil +import signal +import site +import subprocess +import sys +import sysconfig +import time +from pathlib import Path +from urllib.parse import unquote, urlparse + +# --------------------------------------------------------------------------- # +# Environment identity +# --------------------------------------------------------------------------- # + + +def file_digest(path): + with Path(path).open("rb") as stream: + return hashlib.file_digest(stream, "sha256").hexdigest() + + +def identity_digest(value): + encoded = json.dumps(value, sort_keys=True, separators=(",", ":")).encode() + return hashlib.sha256(encoded).hexdigest() + + +def interpreter_path(path): + path = Path(os.path.abspath(os.path.expanduser(str(path)))) + if not path.is_file() or not os.access(path, os.X_OK): + raise ValueError(f"Python interpreter is not executable: {path}") + return str(path) + + +def source_identity(path): + path = Path(path) + result = subprocess.run( + ["git", "-C", str(path), "rev-parse", "--show-toplevel"], + capture_output=True, + text=True, + check=False, + ) + if result.returncode: + raise ValueError(f"Editable Python source has no auditable Git identity: {path}") + root = result.stdout.strip() + tree = subprocess.check_output( + ["git", "-C", root, "rev-parse", "HEAD^{tree}"], text=True + ).strip() + patch = subprocess.check_output(["git", "-C", root, "diff", "--binary", "HEAD"]) + return {"path": root, "tree": tree, "patch_sha256": hashlib.sha256(patch).hexdigest()} + + +def installed_provider_sources(distributions): + """Pin provider code inside a wheel too; unchanged metadata is insufficient. + + Editable frameworks are separately identified by Git tree and patch. vLLM + is an installed wheel in the shared environment, and local patches to its + numerical kernels or actual scheduler must invalidate the environment lock. + No framework import or GPU initialization is needed for this inventory. + """ + prefixes = ( + "vllm/model_executor/determinism/", + "vllm/model_executor/layers/mamba/", + "vllm/model_executor/layers/fused_moe/", + "vllm/model_executor/models/qwen3_next.py", + "vllm/third_party/flash_linear_attention/", + "vllm/v1/attention/", + "vllm/v1/core/sched/", + "vllm/v1/worker/", + "vllm/v1/sample/", + "vllm/config/", + "vllm/compilation/", + "vllm/distributed/", + ) + records = {} + for dist in distributions: + if dist.metadata.get("Name", "").lower().replace("_", "-") != "vllm": + continue + files = dist.files + if not files: + raise ValueError("Installed vLLM has no auditable provider file inventory") + selected = sorted( + str(path) + for path in files + if str(path).endswith(".py") and str(path).startswith(prefixes) + ) + required = "vllm/model_executor/determinism/batch_invariant.py" + if required not in selected: + raise ValueError("Installed vLLM omits the pinned shared GEMM provider") + for name in selected: + path = Path(dist.locate_file(name)) + records[name] = {"path": str(path.absolute()), "sha256": file_digest(path)} + return records + + +def installation_roots(): + """The interpreter's site directories, independent of what has been imported. + + ``sys.path`` grows at runtime: importing setuptools appends its ``_vendor`` + directory, after which ``importlib.metadata.distributions()`` also lists the + vendored packages. An identity read through ``sys.path`` therefore depends on + import order, and a worker that imports the model before verifying would + disagree with the manifest written by a fresh interpreter. Enumerate the fixed + site roots instead; in a fresh interpreter they are exactly the ``site-packages`` + entries of ``sys.path``. + """ + roots = {Path(sysconfig.get_path("purelib")), Path(sysconfig.get_path("platlib"))} + roots.update(Path(p) for p in site.getsitepackages()) + if site.ENABLE_USER_SITE: + roots.add(Path(site.getusersitepackages())) + return sorted(root for root in roots if root.is_dir()) + + +def current_identity(): + distributions = [] + sources = {} + roots = installation_roots() + installed = list(importlib.metadata.distributions(path=[str(root) for root in roots])) + for dist in installed: + if dist.metadata.get("Name", "").lower().replace("_", "-") in {"rl-engine", "rl-kernel"}: + continue + direct = dist.read_text("direct_url.json") + if direct: + direct_value = json.loads(direct) + if direct_value.get("dir_info", {}).get("editable"): + location = Path(unquote(urlparse(direct_value["url"]).path)) + # This project is loaded from the frozen source snapshot instead. + if dist.metadata.get("Name", "").lower().replace("_", "-") != "rl-engine": + sources[str(location)] = source_identity(location) + distributions.append( + { + "name": dist.metadata.get("Name"), + "version": dist.version, + "metadata_sha256": identity_digest(dist.read_text("METADATA")), + "direct_url": direct, + } + ) + pth = {} + for root in roots: + for path in sorted(root.glob("*.pth")): + pth[str(path)] = file_digest(path) + for line in path.read_text().splitlines(): + location = Path(line) + if ( + line + and not line.startswith(("#", "import ")) + and location.is_absolute() + and location.is_dir() + ): + if (location / ".git").exists(): + sources[str(location)] = source_identity(location) + config = Path(sys.prefix) / "pyvenv.cfg" + ninja = next( + ( + Path(prefix) / "bin/ninja" + for prefix in (sys.prefix, sys.base_prefix) + if (Path(prefix) / "bin/ninja").is_file() + ), + None, + ) + build_tools = {"ninja": {"path": str(ninja), "sha256": file_digest(ninja)} if ninja else None} + identity = { + "executable": sys.executable, + "binary": str(Path(sys.executable).resolve()), + "binary_sha256": file_digest(sys.executable), + "prefix": sys.prefix, + "base_prefix": sys.base_prefix, + "version": sys.version, + "pyvenv_cfg_sha256": file_digest(config) if config.is_file() else None, + "distributions": sorted( + distributions, key=lambda item: (item["name"] or "", item["version"]) + ), + "build_tools": build_tools, + "pth": pth, + "sources": sources, + "installed_provider_sources": installed_provider_sources(installed), + } + return {"sha256": identity_digest(identity), "identity": identity} + + +def probe_environment(python): + command = [interpreter_path(python), str(Path(__file__).absolute()), "--json"] + env = os.environ.copy() + env.pop("PYTHONHOME", None) + env.pop("PYTHONPATH", None) + output = subprocess.check_output(command, text=True, env=env, timeout=180) + return json.loads(output) + + +def verify_environment(expected, *, require_venv=False): + actual = current_identity() + if actual != expected: + raise RuntimeError( + "environment_mismatch: interpreter, dependencies or editable source changed" + ) + if require_venv and sys.prefix == sys.base_prefix: + raise RuntimeError("environment_mismatch: training/recompute requires the isolated venv") + return actual + + +if __name__ == "__main__": + print(json.dumps(current_identity(), sort_keys=True)) + + +# --------------------------------------------------------------------------- # +# Failure classification +# --------------------------------------------------------------------------- # + + +FAILURE_KINDS = frozenset( + { + "environment_mismatch", + "source_mismatch", + "checkpoint_mismatch", + "topology_mismatch", + "insufficient_memory", + "insufficient_disk", + "out_of_memory", + "time_limit", + "unknown", + } +) + + +class ProcessFailure(RuntimeError): + def __init__(self, message, *, failure_kind): + super().__init__(message) + self.failure_kind = failure_kind + + +def failure_kind(error=None, *, text=""): + explicit = getattr(error, "failure_kind", None) + if explicit in FAILURE_KINDS: + return explicit + if isinstance(error, (TimeoutError, subprocess.TimeoutExpired)): + return "time_limit" + if isinstance(error, OSError) and error.errno == errno.ENOSPC: + return "insufficient_disk" + if type(error).__name__ == "OutOfMemoryError": + return "out_of_memory" + if isinstance(error, (ImportError, ModuleNotFoundError)): + return "environment_mismatch" + detail = f"{type(error).__name__}: {error}\n{text}" if error is not None else text + for kind in sorted(FAILURE_KINDS - {"unknown", "out_of_memory", "time_limit"}): + if re.search(rf"\b{kind}\s*:", detail): + return kind + if re.search(r"\bOutOfMemoryError\s*:|CUDA out of memory", detail, re.IGNORECASE): + return "out_of_memory" + if re.search(r"\bENOSPC\b|\[Errno 28\]|No space left on device", detail): + return "insufficient_disk" + if re.search(r"\b(?:ModuleNotFoundError|ImportError)\s*:", detail): + return "environment_mismatch" + if re.search(r"\b(?:TimeoutExpired|TimeoutError|time_limit)\s*:", detail): + return "time_limit" + # Signals, exit 137, MemoryError and a bare 'Killed' are not proof of CUDA OOM. + return "unknown" + + +def failure_fields(error): + return {"failure_kind": failure_kind(error), "error": f"{type(error).__name__}: {error}"} + + +def subprocess_failure(code, log_path): + with Path(log_path).open("rb") as stream: + stream.seek(0, 2) + stream.seek(max(0, stream.tell() - 65536)) + tail = stream.read().decode("utf-8", errors="replace") + return ProcessFailure( + f"Native model process failed with exit {code}; see {log_path}\n{tail}", + failure_kind=failure_kind(text=tail), + ) + + +# --------------------------------------------------------------------------- # +# Memory, checkpoint and atomic files +# --------------------------------------------------------------------------- # + + +def atomic_json(path, payload): + path = Path(path) + temporary = path.with_suffix(path.suffix + ".partial") + with temporary.open("w") as stream: + stream.write(json.dumps(payload, indent=2) + "\n") + stream.flush() + os.fsync(stream.fileno()) + temporary.replace(path) + fd = os.open(path.parent, os.O_RDONLY | os.O_DIRECTORY) + try: + os.fsync(fd) + finally: + os.close(fd) + + +def digest(path): + with path.open("rb") as stream: + return hashlib.file_digest(stream, "sha256").hexdigest() + + +_MEMORY_SUFFIXES = {"K": 1024, "M": 1024**2, "G": 1024**3, "T": 1024**4} + + +def parse_memory_request(text): + """A memory size (e.g. ``1280G``) in bytes; a bare number is MB.""" + text = str(text).strip() + if not text: + raise RuntimeError("Empty memory request") + scale = _MEMORY_SUFFIXES.get(text[-1].upper()) + digits = text[:-1] if scale else text + if not digits.isdigit(): + raise RuntimeError(f"Unparseable memory request: {text!r}") + return int(digits) * (scale if scale else 1024**2) + + +def available_memory_bytes(): + """MemAvailable, i.e. what the kernel thinks can be had without swapping.""" + for line in Path("/proc/meminfo").read_text().splitlines(): + if line.startswith("MemAvailable:"): + return int(line.split()[1]) * 1024 + return None + + +def memory_limits(environ=None): + """Every known memory ceiling, and what is free right now. + + ``effective_bytes`` is the smallest of the cgroup limit and the allocation + the launcher declares in ``QWEN3_NEXT_ALLOCATED_MEMORY`` (``--memory``). + It is None only when neither is known, which the stages treat as a hard + failure: host free memory alone is never taken as a grant. + ``available_bytes`` is a snapshot of MemAvailable; on a shared node a grant + is not an availability guarantee, so both are checked. + """ + environ = os.environ if environ is None else environ + records = {} + for line in Path("/proc/self/cgroup").read_text().splitlines(): + _, controllers, relative = line.split(":", 2) + if controllers == "": + root, name = Path("/sys/fs/cgroup"), "memory.max" + elif "memory" in controllers.split(","): + root, name = Path("/sys/fs/cgroup/memory"), "memory.limit_in_bytes" + else: + continue + current = root / relative.lstrip("/") + while current.is_relative_to(root): + path = current / name + if path.is_file(): + records[str(path)] = path.read_text().strip() + if current == root: + break + current = current.parent + finite = [int(value) for value in records.values() if value.isdigit() and int(value) < 2**60] + declared = environ.get("QWEN3_NEXT_ALLOCATED_MEMORY") + sources = { + "cgroup_bytes": min(finite) if finite else None, + "declared_bytes": parse_memory_request(declared) if declared else None, + } + known = [value for value in sources.values() if value is not None] + return { + "limits": records, + **sources, + "effective_bytes": min(known) if known else None, + "available_bytes": available_memory_bytes(), + } + + +def verify_checkpoint(checkpoint, identity): + if checkpoint.name != identity["revision"]: + raise RuntimeError("Checkpoint revision does not match the pinned manifest") + config = json.loads((checkpoint / "config.json").read_text()) + for key, value in identity["config_fingerprint"].items(): + if config.get(key) != value: + raise RuntimeError(f"Checkpoint architecture mismatch: {key}") + snapshot = identity["weight_snapshot"] + if digest(checkpoint / snapshot["index_file"]) != snapshot["index_sha256"]: + raise RuntimeError("Checkpoint index checksum mismatch") + for shard in snapshot["shards"]: + path = checkpoint / shard["filename"] + if path.stat().st_size != shard["size_bytes"] or digest(path) != shard["sha256"]: + raise RuntimeError(f"Checkpoint shard checksum mismatch: {path.name}") + return { + "model_id": identity["model_id"], + "revision": identity["revision"], + "content_hash": snapshot["content_hash"], + "shards_verified": len(snapshot["shards"]), + "metadata_sha256": {p.name: digest(p) for p in sorted(checkpoint.glob("*.json"))}, + } + + +# --------------------------------------------------------------------------- # +# Owned child processes +# --------------------------------------------------------------------------- # + + +def signal_group(child, signum): + try: + os.killpg(child.pid, signum) + except ProcessLookupError: + pass + + +def wait_owned_child(child, interrupted, *, grace_seconds=10): + deadline = None + while True: + if interrupted() and deadline is None: + signal_group(child, signal.SIGTERM) + deadline = time.monotonic() + grace_seconds + if deadline is not None and time.monotonic() >= deadline: + # Kill the group even if its leader exited, so grandchildren cannot linger. + signal_group(child, signal.SIGKILL) + return child.wait(timeout=10) + code = child.poll() + if code is not None and deadline is None: + return code + time.sleep(0.05) + + +# --------------------------------------------------------------------------- # +# Stage order +# --------------------------------------------------------------------------- # + +STAGES = { + "preflight": ("256G", "01:00:00", None, "scripts/qwen3_next_preflight.py"), + "layers": ("512G", "04:00:00", "preflight", "scripts/qwen3_next_layers.py"), + "l2": ("512G", "04:00:00", "layers", "scripts/qwen3_next_l2.py"), + # The training acceptance is split so no single job holds a node for a day: + # train1 = update1 + export + L2, train2 = update2 + export + L2 (continues + # train1's durable state), train = reload + L2 + continuous/resumed controls. + "train1": ("1280G", "06:00:00", "l2", "scripts/qwen3_next_training.py"), + "train2": ("1280G", "07:00:00", "train1", "scripts/qwen3_next_training.py"), + "train": ("1280G", "09:00:00", "train2", "scripts/qwen3_next_training.py"), +} + + +def require_predecessor(stage, previous, checkpoint, expected=None): + """The previous stage's run passed, finished, and pinned the same identity.""" + from qwen3_next_contract import acceptance_identity, checkpoint_identity, validate_result + + predecessor = STAGES[stage][2] + if predecessor is None: + return + if previous is None: + raise ValueError(f"{stage} requires a passed {predecessor} run") + manifest = json.loads((previous / "run.json").read_text()) + result = json.loads((previous / "result.json").read_text()) + if manifest["stage"] != predecessor or result.get("status") != "passed": + raise ValueError("predecessor stage has not passed") + if (previous / "RUNNING").exists(): + raise ValueError("predecessor run is still executing") + validate_result(manifest, result, run_dir=previous) + if manifest["checkpoint_identity"] != checkpoint_identity(checkpoint): + raise ValueError("predecessor checkpoint identity mismatch") + if expected is not None and acceptance_identity(manifest) != acceptance_identity(expected): + raise ValueError("predecessor source/environment/provider/topology identity mismatch") + + +# --------------------------------------------------------------------------- # +# Training storage +# --------------------------------------------------------------------------- # + + +def _boundary(name): + """CPU fault-injection seam; production deliberately performs no action.""" + + +def _sync(path): + descriptor = os.open(path, os.O_RDONLY | os.O_DIRECTORY) + try: + os.fsync(descriptor) + finally: + os.close(descriptor) + + +def _read(path): + if path.is_symlink() or not path.is_file(): + raise ValueError(f"Missing or external storage ownership record: {path}") + return json.loads(path.read_text()) + + +def _write(path, value): + if path.is_symlink() or path.with_suffix(path.suffix + ".partial").is_symlink(): + raise ValueError("Storage intent cannot follow a symlink") + atomic_json(path, value) + + +def _root_record(root, identity): + return { + "schema_version": 1, + "scope": "qwen3_next_owned_storage", + "root": str(root), + "identity": identity, + } + + +def owned_root(root, identity): + root = Path(root).absolute() + if root.is_symlink(): + raise ValueError("Training storage root cannot be a symlink") + root = root.resolve() + root.parent.mkdir(parents=True, exist_ok=True) + parent_owner = root.parent / f".{root.name}.owner.json" + expected = _root_record(root, identity) + if parent_owner.exists(): + if _read(parent_owner) != expected: + raise ValueError("Training storage belongs to another run") + else: + # Existing deployments can migrate only with their original owner. + # An interrupted new creation has no directory until parent proof lands. + if root.exists() and any(root.iterdir()): + if _read(root / "RUN_OWNER.json") != identity: + raise ValueError("Existing training files lack a matching run owner") + _write(parent_owner, expected) + _boundary("root_owner_persisted") + root.mkdir(exist_ok=True) + _sync(root.parent) + _boundary("root_created") + owner = root / "RUN_OWNER.json" + if owner.exists() and _read(owner) != identity: + raise ValueError("Internal training owner disagrees with durable parent proof") + _write(owner, identity) + return root + + +def require_owner(root, identity): + root = Path(root).absolute() + if root.is_symlink() or not root.is_dir(): + raise ValueError("Training storage must be a direct owned directory") + root = root.resolve() + if _read(root.parent / f".{root.name}.owner.json") != _root_record(root, identity): + raise ValueError("Training storage lacks matching durable parent ownership") + return root + + +def _child(root, name): + if not isinstance(name, str) or not name or Path(name).name != name or name in (".", ".."): + raise ValueError("Storage intent must name one direct child") + path = root / name + if path.is_symlink(): + raise ValueError("Storage intents cannot follow target symlinks") + return path + + +def create_staging(root, destination_name, metadata): + if metadata.get("status") != "staging": + raise ValueError("Staging creation needs an explicit unpublished status") + root = require_owner(root, metadata["identity"]) + destination = _child(root, destination_name) + if destination.name.startswith(".") or destination.suffix: + raise ValueError("Published storage names must remain visible and have no suffix") + staging = destination.with_suffix(".partial") + if destination.exists() or staging.exists(): + raise ValueError("A staging intent cannot adopt an existing directory") + intent = root / f".{destination.name}.staging.json" + value = { + "schema_version": 1, + "scope": "qwen3_next_staging_intent", + "identity": metadata["identity"], + "staging": staging.name, + "published": destination.name, + "metadata": metadata, + } + if intent.exists(): + raise ValueError("Staging names cannot reuse an earlier durable intent") + _write(intent, value) + _boundary("staging_intent_persisted") + staging.mkdir() + _sync(root) + _boundary("staging_created") + _write(staging / "STAGING.json", metadata) + _boundary("staging_marker_persisted") + return staging, destination + + +def _finish_retirement(root, intent, record, *, recovering=False): + target, tombstone = _child(root, record["target"]), _child(root, record["tombstone"]) + if target.exists() and tombstone.exists(): + raise ValueError("Retirement has conflicting original and tombstone directories") + if record["status"] == "retired": + if target.exists() or tombstone.exists(): + raise ValueError("Retired storage name was unexpectedly reused") + return record + if "successor" in record["proof"]: + successor = Path(record["proof"]["successor"]) + if successor.parent != root or successor.is_symlink() or successor == target: + raise ValueError("Retirement successor escaped its owning run") + if recovering: + from qwen3_next_checkpoint import validate_checkpoint + + if ( + validate_checkpoint(successor, record["identity"]) + != record["proof"]["successor_complete"] + ): + raise ValueError("Retirement successor no longer matches its verified publication") + if target.exists(): + marker, expected, optional = _retirement_marker(record) + marker = target / marker + if marker.exists() or marker.is_symlink() or not optional: + if _read(marker) != expected: + raise ValueError("Retirement target differs from its durable ownership proof") + target.rename(tombstone) + _sync(root) + _boundary("retirement_renamed") + if tombstone.exists(): + shutil.rmtree(tombstone) + _sync(root) + _boundary("retirement_removed") + record = {**record, "status": "retired"} + _write(intent, record) + _boundary("retirement_committed") + return record + + +def retire_directory(path, root, identity, *, proof): + root = require_owner(root, identity) + path = Path(path).absolute() + if path.is_symlink(): + raise ValueError("Retirement cannot follow a target symlink") + path = path.resolve() + if path.parent != root or path.name.startswith("."): + raise ValueError("Retirement target must be a visible direct child of this run") + target = _child(root, path.name) + intent = root / f".{target.name}.delete.json" + recovering = intent.exists() + if recovering: + record = _read(intent) + _validate_retirement(record, target.name, identity) + if record["proof"] != proof: + raise ValueError("Retirement retry changed its original ownership proof") + else: + if not target.is_dir(): + raise ValueError("Cannot authorize retirement of an unknown directory") + record = { + "schema_version": 1, + "scope": "qwen3_next_retirement", + "identity": identity, + "target": target.name, + "tombstone": f".{target.name}.deleting", + "status": "retiring", + "proof": proof, + } + _validate_retirement(record, target.name, identity) + _write(intent, record) + _boundary("retirement_intent_persisted") + return _finish_retirement(root, intent, record, recovering=recovering) + + +def _validate_retirement(record, target, identity): + if ( + record.get("schema_version") != 1 + or record.get("scope") != "qwen3_next_retirement" + or record.get("identity") != identity + or record.get("target") != target + or target.startswith(".") + or record.get("tombstone") != f".{target}.deleting" + or record.get("status") not in ("retiring", "retired") + or not isinstance(record.get("proof"), dict) + ): + raise ValueError("Retirement intent belongs to another target/run or is malformed") + _retirement_marker(record) + + +def _retirement_marker(record): + """Bind a destructive intent to one of the supported ownership proofs.""" + proof, identity = record["proof"], record["identity"] + if proof.get("scope") == "qwen3_next_staging_intent": + metadata = proof.get("metadata", {}) + published = proof.get("published") + if ( + proof.get("schema_version") != 1 + or proof.get("identity") != identity + or proof.get("staging") != record["target"] + or not isinstance(published, str) + or Path(published).name != published + or published.startswith(".") + or Path(published).suffix + or str(Path(published).with_suffix(".partial")) != record["target"] + or metadata.get("identity") != identity + or metadata.get("status") != "staging" + ): + raise ValueError("Staging retirement proof does not own its target") + return "STAGING.json", metadata, True + if proof.get("status") == "staging" and proof.get("identity") == identity: + if Path(record["target"]).suffix != ".partial": + raise ValueError("Legacy staging proof cannot authorize a published directory") + return "STAGING.json", proof, False + complete, successor = proof.get("complete", {}), proof.get("successor_complete", {}) + if ( + complete.get("identity") == identity + and complete.get("status") == "complete" + and successor.get("identity") == identity + and successor.get("status") == "complete" + and type(complete.get("step")) is int + and type(successor.get("step")) is int + and complete["step"] < successor["step"] + and isinstance(proof.get("successor"), str) + ): + return "COMPLETE.json", complete, False + exported = proof.get("export", {}) + if exported.get("training_identity") == identity and exported.get("status") == "complete": + return "EXPORT_COMPLETE.json", exported, False + raise ValueError("Retirement lacks a supported same-run ownership proof") + + +def recover_storage(root, identity): + root = require_owner(root, identity) + recovered = [] + for intent in sorted(root.glob(".*.delete.json")): + record = _read(intent) + target = record.get("target") + _child(root, target) + _validate_retirement(record, target, identity) + if intent.name != f".{target}.delete.json": + raise ValueError("Retirement filename contradicts its target") + if record["status"] == "retiring": + recovered.append(_finish_retirement(root, intent, record, recovering=True)) + else: + _finish_retirement(root, intent, record) + for intent in sorted(root.glob(".*.staging.json")): + record = _read(intent) + if ( + record.get("schema_version") != 1 + or record.get("scope") != "qwen3_next_staging_intent" + or record.get("identity") != identity + or record.get("metadata", {}).get("identity") != identity + or record.get("metadata", {}).get("status") != "staging" + ): + raise ValueError("Staging intent belongs to another run or is malformed") + staging, published = _child(root, record["staging"]), _child(root, record["published"]) + if ( + published.name.startswith(".") + or published.suffix + or staging != published.with_suffix(".partial") + or intent.name != f".{published.name}.staging.json" + ): + raise ValueError("Staging intent paths do not describe one publication") + if staging.exists() and published.exists(): + raise ValueError("Staging and published copies cannot coexist") + if staging.exists(): + marker = staging / "STAGING.json" + if marker.exists() and _read(marker) != record["metadata"]: + raise ValueError("Staging marker differs from its durable parent intent") + recovered.append(retire_directory(staging, root, identity, proof=record)) + # A marker from the earlier protocol can prove ownership, but an arbitrary + # directory with neither marker nor parent intent must never be deleted. + for staging in sorted(root.glob("*.partial")): + if not staging.is_dir(): + continue + metadata = _read(staging / "STAGING.json") + if metadata.get("identity") != identity or metadata.get("status") != "staging": + raise ValueError("Interrupted state belongs to another run or lacks a matching owner") + recovered.append(retire_directory(staging, root, identity, proof=metadata)) + return recovered + + +def training_storage_root(manifest, run_root): + """The run's checkpoint and export root: ``--training-storage`` or ``/training-state``.""" + shared = manifest.get("training_storage") + return Path(shared) if shared else Path(run_root) / "training-state" diff --git a/scripts/qwen3_next_tp_head_check.py b/scripts/qwen3_next_tp_head_check.py new file mode 100644 index 000000000..6e65b6292 --- /dev/null +++ b/scripts/qwen3_next_tp_head_check.py @@ -0,0 +1,141 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Real-weight TP4 embedding, final norm, LM-head and logprob component gate.""" + +import argparse +import json +import os +from datetime import timedelta +from pathlib import Path + +import torch +import torch.distributed as dist +from qwen3_next_contract import atomic_json +from safetensors import safe_open + +from rl_engine.integrations.qwen3_next_embedding import shared_embedding +from rl_engine.integrations.qwen3_next_forward import shared_linear, shared_logprob +from rl_engine.integrations.qwen3_next_logprob import qwen_logprob_contract +from rl_engine.integrations.qwen3_next_tp import _CopyToTP +from rl_engine.kernels.ops.cuda.norm.rmsnorm import Qwen3NextRMSNormCudaOp +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + +REQUIRED_CASES = [ + "hf-embedding-roundtrip", + "hf-lm-head-roundtrip", + "replicated-norm-gradient", + "nonzero-finite-embedding-head-gradient", + "embedding-repeat-100", + *[f"batch-invariant-prompt{length}" for length in (8, 64, 256, 1024)], + "logprob-original-and-promoted-logits", + "entropy-interface", +] + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--checkpoint", type=Path, required=True) + parser.add_argument("--output", type=Path, required=True) + args = parser.parse_args() + rank = int(os.environ["LOCAL_RANK"]) + if int(os.environ["WORLD_SIZE"]) != 4 or torch.cuda.device_count() != 4: + raise RuntimeError("Head acceptance requires four ranks and four visible GPUs") + torch.cuda.set_device(rank) + dist.init_process_group( + "nccl", timeout=timedelta(minutes=10), device_id=torch.device("cuda", rank) + ) + group = dist.group.WORLD + try: + index = json.loads((args.checkpoint / "model.safetensors.index.json").read_text())[ + "weight_map" + ] + + def load(name): + with safe_open(args.checkpoint / index[name], framework="pt", device="cpu") as reader: + value = reader.get_tensor(name) + if value.dtype != torch.bfloat16: + raise ValueError("Official component weight must be BF16") + return value + + weights = {} + for name in ("model.embed_tokens.weight", "lm_head.weight"): + original = load(name) + local = torch.nn.Parameter(original.chunk(4)[rank].cuda().contiguous()) + peers = [torch.empty_like(local) for _ in range(4)] + dist.all_gather(peers, local.detach()) + exact(torch.cat(peers).cpu(), original, name=f"loaded {name} HF roundtrip") + weights[name] = local + del original, peers + embedding, head = weights["model.embed_tokens.weight"], weights["lm_head.weight"] + norm = torch.nn.Parameter(load("model.norm.weight").cuda()) + norm_op = Qwen3NextRMSNormCudaOp() + + def forward(tokens): + hidden = shared_embedding(tokens, embedding, group) + normalized = norm_op(hidden, norm, eps=1e-6) + return shared_linear(_CopyToTP.apply(normalized, group), head) + + with torch.no_grad(): + for length in (8, 64, 256, 1024): + tokens = torch.arange(4 * (length + 16), device="cuda", dtype=torch.int64) + tokens = ((tokens * 2399 + 1) % 151936).reshape(4, length + 16) + batch = forward(tokens) + singleton = forward(tokens[:1]) + exact(batch[:1], singleton, name=f"embedding/norm/LM batch {length}") + repeated = torch.tensor([1, 37985, 75969, 113953, 1, 75969], device="cuda") + expected = shared_embedding(repeated, embedding, group) + for _ in range(100): + exact(shared_embedding(repeated, embedding, group), expected) + + tokens = torch.tensor([1, 37985, 75969, 113953] * 4, device="cuda") + logits = forward(tokens) + contract = qwen_logprob_contract(logits, rank=rank, world_size=4) + logp, lse, entropy = shared_logprob(logits, tokens, contract=contract, tp_group=group) + promoted = logits.float() + promoted_logp, _, _ = shared_logprob( + promoted, + tokens, + contract=qwen_logprob_contract(promoted, rank=rank, world_size=4), + tp_group=group, + ) + exact(logp, promoted_logp, name="native VIME loss input promotion") + without_entropy, _, no_entropy = shared_logprob( + logits, tokens, contract=contract, tp_group=group, with_entropy=False + ) + exact(logp, without_entropy, name="entropy request logprob identity") + if no_entropy is not None or not all(torch.isfinite(t).all() for t in (entropy, lse)): + raise AssertionError("Entropy/LSE interface is invalid") + (-logp.mean()).backward() + for name, parameter in (("embedding", embedding), ("lm_head", head), ("norm", norm)): + gradient = parameter.grad + valid = gradient is not None + valid = valid and bool(torch.isfinite(gradient).all() and gradient.count_nonzero()) + if not valid: + raise AssertionError(f"Missing finite nonzero {name} gradient") + peers = [torch.empty_like(norm.grad) for _ in range(4)] + dist.all_gather(peers, norm.grad) + for peer in peers: + exact(peer, norm.grad, name="replicated final norm gradient") + from rl_engine.integrations.qwen3_next_binary import imported_provider_identity + + args.output.mkdir(parents=True, exist_ok=True) + atomic_json( + args.output / f"rank-{rank}.json", + { + "status": "passed", + "scope": "tp4_embedding_lm_head_only", + "rank": rank, + "provider_binary": imported_provider_identity(), + "full_model_l2": "not_evaluated", + "cases": sorted(REQUIRED_CASES), + }, + ) + dist.barrier() + finally: + dist.destroy_process_group() + + +if __name__ == "__main__": + main() diff --git a/scripts/qwen3_next_tp_probe.py b/scripts/qwen3_next_tp_probe.py new file mode 100644 index 000000000..fd55d6d56 --- /dev/null +++ b/scripts/qwen3_next_tp_probe.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Four independent NCCL ranks must communicate exactly before model loading.""" + +import argparse +import os +from datetime import timedelta +from pathlib import Path + +import torch +import torch.distributed as dist +from qwen3_next_runtime import atomic_json + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--output", type=Path, required=True) + args = parser.parse_args() + rank = int(os.environ["LOCAL_RANK"]) + if int(os.environ["WORLD_SIZE"]) != 4: + raise RuntimeError("This probe requires TP=4") + torch.cuda.set_device(rank) + dist.init_process_group( + "nccl", timeout=timedelta(minutes=3), device_id=torch.device("cuda", rank) + ) + try: + value = torch.arange(1024, device="cuda", dtype=torch.int64) + rank + gathered = [torch.empty_like(value) for _ in range(4)] + dist.all_gather(gathered, value) + for peer, actual in enumerate(gathered): + assert torch.equal(actual, torch.arange(1024, device="cuda") + peer) + dist.all_reduce(value) + assert torch.equal(value, 4 * torch.arange(1024, device="cuda") + 6) + torch.cuda.synchronize() + args.output.mkdir(parents=True, exist_ok=True) + atomic_json( + args.output / f"rank-{rank}.json", + { + "status": "passed", + "rank": rank, + "world_size": 4, + "device": torch.cuda.get_device_name(), + "nccl": torch.cuda.nccl.version(), + }, + ) + dist.barrier() + finally: + dist.destroy_process_group() + + +if __name__ == "__main__": + main() diff --git a/scripts/qwen3_next_training.py b/scripts/qwen3_next_training.py new file mode 100644 index 000000000..21c52025a --- /dev/null +++ b/scripts/qwen3_next_training.py @@ -0,0 +1,1077 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Native RL update, full-model L2 and atomic restart acceptance on four GPUs.""" + +import argparse +import json +import math +import os +import re +import shutil +import signal +import subprocess +import sys +import time +from pathlib import Path +from uuid import uuid4 + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) + +from qwen3_next_checkpoint import ( # noqa: E402 + newest_complete_checkpoint, + publish_checkpoint, + resume_interrupted_publications, + validate_checkpoint, + write_rank_manifest, +) +from qwen3_next_contract import ( # noqa: E402 + SCOPES, + atomic_json, + evidence_record, + result_envelope, + validate_result, +) +from qwen3_next_l2 import check_ready as check_l2_ready # noqa: E402 +from qwen3_next_l2 import ( # noqa: E402 + model_identity, + run_matrix, + run_process, + verify_before_model_load, + wait_gpu_release, +) +from qwen3_next_runtime import failure_fields # noqa: E402 +from qwen3_next_runtime import ( # noqa: E402 + file_digest, + owned_root, + recover_storage, + retire_directory, + training_storage_root, + verify_environment, +) + +from rl_engine.integrations.qwen3_next_native_training import ( # noqa: E402 + policy_fixture, + run_training_phase, + validate_active_parameter_names, +) +from rl_engine.integrations.qwen3_next_training import state_digest # noqa: E402 +from rl_engine.testing.qwen3_next_binary import ( # noqa: E402 + archive_provider_binary, + validate_binary_record, +) +from rl_engine.testing.qwen3_next_l2 import ( # noqa: E402 + case_matrix, + checked_artifact_path, + compare_artifacts, + json_digest, + load_tensor_artifact, + validate_matrix_report, +) + +SECTIONS = ( + "baseline", + "update1", + "update2", + "reload", + "continuous_step3", + "resumed_step3", + "checkpoint", + "fault_injection", +) +FAULTS = ("interrupted_writer", "missing_rank", "hash_error", "unpublished_load") +PHASES = ("update1", "update2", "reload", "continuous", "resumed") +# Each training stage runs the phases up to its last one and leaves the durable +# state (checkpoints, journal) for the next stage to continue from. +STAGE_LAST_PHASE = {"train1": "update1", "train2": "update2", "train": "resumed"} + + +def stage_phases(stage): + """Phases a training stage executes, in order.""" + last = STAGE_LAST_PHASE[stage] + return PHASES[: PHASES.index(last) + 1] + + +def continued_training_storage(previous): + """Durable state a continuing stage resumes: the train1 run's, however many stages back. + + train2 continues train1's storage instead of creating its own, so the final + stage must follow the predecessor chain back to the run that owns the state. + """ + run = Path(previous) + for _ in range(len(STAGE_LAST_PHASE)): + manifest = json.loads((run / "run.json").read_text()) + if manifest.get("stage") == "train1": + return training_storage_root(manifest, run) + if manifest.get("stage") not in STAGE_LAST_PHASE or not manifest.get("previous"): + break + run = Path(manifest["previous"]) + raise ValueError("Training continuation does not lead back to a train1 run") + + +def continuation_identity_matches(journal_identity, model_identity_value, fixture_sha256): + """A later training stage continues a journal only for the same model/env/fixture.""" + expected = {**model_identity_value, "fixture_sha256": fixture_sha256} + actual = {k: v for k, v in journal_identity.items() if k not in ("run_id", "execution_id")} + return actual == expected + + +EVENTS = ( + "l2:baseline", + "worker:update1", + "l2:update1", + "worker:update2", + "l2:update2", + "worker:reload", + "l2:reload", + "worker:continuous", + "worker:resumed", +) + + +def record(path): + path = Path(path).absolute() + return {"path": str(path), "sha256": file_digest(path)} + + +def read_record(value): + path = Path(value["path"]) + if ( + not path.is_absolute() + or path.is_symlink() + or not path.is_file() + or file_digest(path) != value["sha256"] + ): + raise ValueError(f"Training evidence changed or is missing: {path}") + return json.loads(path.read_text()) + + +def require_digest(value): + if ( + not isinstance(value, str) + or len(value) != 64 + or any(char not in "0123456789abcdef" for char in value) + ): + raise ValueError("Training state contains an invalid original-byte digest") + + +def validate_state(state, *, rank, step, identity, parameters): + expected = { + "schema_version": 1, + "scope": "native_vime_training_state", + "rank": rank, + "identity": identity, + "step": step, + "sample_position": step * 4, + "consumed_train_samples": step * 4, + "fixture_sha256": identity["fixture_sha256"], + } + hash_fields = { + "model_sha256", + "optimizer_parameter_sha256", + "optimizer_header_sha256", + "scheduler_sha256", + "rng_sha256", + "state_sha256", + } + if set(state) != set(expected) | hash_fields or any( + state.get(key) != value for key, value in expected.items() + ): + raise ValueError("Native state omits or changes rank/step/sample/fixture identity") + if ( + state_digest({key: value for key, value in state.items() if key != "state_sha256"}) + != state["state_sha256"] + ): + raise ValueError("Native state digest does not bind the recorded recovery state") + if not isinstance(state["model_sha256"], list) or len(state["model_sha256"]) != 1: + raise ValueError("Native state requires the complete single PP1 model chunk") + for value in state["model_sha256"] + [ + state[key] for key in ("optimizer_header_sha256", "scheduler_sha256", "rng_sha256") + ]: + require_digest(value) + moments = state["optimizer_parameter_sha256"] + if set(moments) != set(parameters): + raise ValueError("Native optimizer state does not cover every trainable parameter") + for values in moments.values(): + if not {"param", "exp_avg", "exp_avg_sq"}.issubset(values): + raise ValueError("Native state omitted FP32 masters or Adam moments") + for value in values.values(): + require_digest(value) + + +def validate_journal(journal, identity, *, verify_files=True): + if journal.get("schema_version") != 1 or journal.get("identity") != identity: + raise ValueError("Training journal belongs to different source/environment/fixture") + events = journal.get("events", []) + if events != list(EVENTS[: len(events)]) or not events: + raise ValueError("Training journal did not preserve update/L2/reload/control order") + if set(journal.get("workers", {})) != { + event.split(":")[1] for event in events if event.startswith("worker:") + }: + raise ValueError("Training journal worker records disagree with durable phase order") + if set(journal.get("l2_reports", {})) != { + event.split(":")[1] for event in events if event.startswith("l2:") + }: + raise ValueError("Training journal L2 records disagree with durable phase order") + if verify_files: + for value in journal["workers"].values(): + read_record(value) + for value in journal["l2_reports"].values(): + validate_matrix_report(read_record(value)) + + +def repeat_reload_in_current_attempt(journal, attempt): + """Restore durable updates, but never pass with an older attempt's final reload.""" + if "reload" not in journal["workers"]: + return False + original = read_record(journal["workers"]["reload"]) + if original.get("attempt") == str(attempt): + return False + if not str(original.get("attempt", "")).isdigit(): + raise ValueError("Recovery source has no valid attempt identity") + cutoff = journal["events"].index("worker:reload") + retired = { + "reason": "repeat_final_reload_and_controls_in_current_attempt", + "from_attempt": original["attempt"], + "to_attempt": str(attempt), + "events": journal["events"][cutoff:], + "workers": { + phase: journal["workers"].pop(phase) + for phase in ("reload", "continuous", "resumed") + if phase in journal["workers"] + }, + "l2_report": journal["l2_reports"].pop("reload", None), + } + journal["events"] = journal["events"][:cutoff] + journal.setdefault("recovery", []).append(retired) + return True + + +def validate_update(audit, *, step, fixture): + if (audit.get("step"), audit.get("sample_position"), audit.get("fixture")) != ( + step, + step * 4, + fixture, + ): + raise ValueError("Native policy update progress/fixture mismatch") + norm = audit.get("grad_norm", float("nan")) + if audit.get("policy_loss_calls") != 4 or not math.isfinite(norm) or norm <= 0: + raise ValueError("Native policy update needs four losses and a finite nonzero norm") + before, after = ( + audit.get("parameter_hashes_before", {}), + audit.get("parameter_hashes_after", {}), + ) + if not before or before.keys() != after.keys(): + raise ValueError("Native update must hash every trainable parameter before and after") + validate_active_parameter_names(before) + for value in [*before.values(), *after.values()]: + require_digest(value) + if audit.get("progress") != { + "consumed_before": (step - 1) * 4, + "consumed_after": step * 4, + "scheduler_before": (step - 1) * 4, + "scheduler_after": step * 4, + "sample_counter_owner": "qwen3_next_native_training.policy_step", + }: + raise ValueError("Native update did not commit matching sample/scheduler progress") + changed = [name for name in before if before[name] != after[name]] + if not changed or sorted(changed) != sorted(audit.get("changed_parameters", [])): + raise ValueError("Native optimizer did not prove an actual parameter change") + gradients = audit.get("gradients", {}) + parameters, groups = gradients.get("parameters", {}), gradients.get("active_groups", {}) + if parameters.keys() != before.keys() or groups.keys() != before.keys(): + raise ValueError("Every active parameter tensor must have predeclared gradient evidence") + for name in before: + if ( + parameters[name].get("present") is not True + or parameters[name].get("nonzero") is not True + ): + raise ValueError(f"Active parameter tensor has no valid gradient: {name}") + if parameters[name].get("dtype") not in ("torch.float32", "torch.bfloat16"): + raise ValueError("Active gradient evidence omitted or changed its storage dtype") + if groups[name] != [name]: + raise ValueError("Active gradient group was substituted after execution") + metrics = audit.get("metrics", {}) + if not metrics or any( + type(value) not in (int, float) or not math.isfinite(value) for value in metrics.values() + ): + raise ValueError("Native policy loss must retain finite scalar training metrics") + return True + + +def validate_worker(value, *, phase, identity): + if ( + value.get("schema_version") != 1 + or value.get("status") != "passed" + or value.get("phase") != phase + or value.get("identity") != identity + or value.get("all_rank_processes_exited") is not True + or value.get("gpu_release_verified") is not True + or [rank.get("rank") for rank in value.get("ranks", [])] != list(range(4)) + ): + raise ValueError("Incomplete native four-rank worker evidence") + pids = [rank.get("pid") for rank in value["ranks"]] + if any(type(pid) is not int or pid <= 0 for pid in pids) or len(set(pids)) != 4: + raise ValueError("Native worker requires four distinct rank processes") + binary = validate_binary_record( + value.get("provider_binary"), source_tree=identity["source_tree"] + ) + for rank, row in enumerate(value["ranks"]): + if row.get("provider_binary") != binary: + raise ValueError("Native training rank differs from its archived CUDA provider") + if row.get("identity") != identity or row.get("phase") != phase: + raise ValueError("Native rank identity mismatch") + if row.get("native_entrypoints") != [ + "vime.backends.megatron_utils.model.setup_model_and_optimizer", + "vime.backends.megatron_utils.model.train_one_step", + "vime.backends.megatron_utils.loss.policy_loss_function", + ]: + raise ValueError("Worker did not identify the required native training entrypoints") + if phase in ("continuous", "resumed"): + controls = row.get("controls", {}) + if set(controls) != ({"1", "2", "3"} if phase == "continuous" else {"3"}): + raise ValueError("Native recovery controls omitted or added an update") + updates = [(int(step), control) for step, control in controls.items()] + elif phase == "reload": + restored = row.get("restored", {}) + audit = restored.get("rank_evidence", {}).get("update_evidence", {}) + updates = [(2, {"update": audit, "state": row.get("state", {})})] + else: + updates = [(1 if phase == "update1" else 2, row)] + for step, control in updates: + audit = control["update"] + validate_update(audit, step=step, fixture=identity["fixture_sha256"]) + validate_state( + control["state"], + rank=rank, + step=step, + identity=identity, + parameters=audit["parameter_hashes_after"], + ) + + +def validate_export_evidence(worker, identity): + from qwen3_next_training_export import validate_training_export_record + + evidence = worker["export_evidence"] + complete = read_record(evidence["complete"]) + checkpoint_identity = validate_training_export_record( + complete, worker["export"]["checkpoint_identity"] + ) + if checkpoint_identity != worker["export"]["checkpoint_identity"]: + raise ValueError("Archived export proof differs from the weights actually tested by L2") + if complete["training_identity"] != identity: + raise ValueError("Archived export belongs to another native training identity") + read_record(evidence["index"]) + # The original completion record binds the exact index bytes, including HF + # tensor-to-shard mapping. Historical proof never authorizes loading a file. + if complete["files"]["model.safetensors.index.json"]["sha256"] != evidence["index"]["sha256"]: + raise ValueError("Archived export index differs from its completion manifest") + audits = [ + ( + row["restored"]["rank_evidence"]["update_evidence"] + if worker["phase"] == "reload" + else row["update"] + ) + for row in worker["ranks"] + ] + if complete.get("model_identity_method") != "native_parameter_hashes_after" or complete[ + "model_rank_sha256" + ] != [state_digest(audit["parameter_hashes_after"]) for audit in audits]: + raise ValueError("HF export bytes do not belong to the actual optimizer-updated actor") + + +def archive_export(worker, directory, identity): + from qwen3_next_training_export import validate_training_export + + export = worker["export"] + root = Path(export["checkpoint"]) + validate_training_export(root, export["checkpoint_identity"]) + evidence = {} + for kind, name in ( + ("complete", "EXPORT_COMPLETE.json"), + ("index", "model.safetensors.index.json"), + ): + destination = Path(directory) / name + # Atomic JSON preserves the parsed content, while recording the index's + # original bytes is required for the completion manifest hash binding. + with destination.open("xb") as output, (root / name).open("rb") as source: + shutil.copyfileobj(source, output) + output.flush() + os.fsync(output.fileno()) + evidence[kind] = record(destination) + worker["export_evidence"] = evidence + validate_export_evidence(worker, identity) + + +def compare_reload_matrices(updated, reloaded): + """Reopen every original phase artifact; a saved passed boolean is insufficient.""" + ids = [case.case_id for case in case_matrix()] + if any( + matrix.get("case_ids") != ids or set(matrix.get("cases", {})) != set(ids) + for matrix in (updated, reloaded) + ): + raise ValueError("Reload comparison requires both complete sixteen-case matrices") + result = {"status": "passed", "raw_bit_comparison": True, "case_ids": ids, "cases": {}} + for case in ids: + result["cases"][case] = {} + for role in ("rollout", "recompute"): + left = load_tensor_artifact( + checked_artifact_path(updated["cases"][case]["artifacts"][role]) + ) + right = load_tensor_artifact( + checked_artifact_path(reloaded["cases"][case]["artifacts"][role]) + ) + compared = compare_artifacts(left, right) + if not compared["passed"]: + raise ValueError( + f"Reload changed original {role} tensor bits/state at {case}: {compared}" + ) + result["cases"][case][role] = compared["comparisons"] + return result + + +def validate_training_report(report, *, verify_files=True): + """No partial matrix, optimizer header or stale worker can close training.""" + if ( + report.get("schema_version") != 1 + or report.get("status") != "passed" + or report.get("scope") != "qwen3_next_training_and_reload" + or report.get("required_sections") != list(SECTIONS) + ): + raise ValueError("Incomplete/unsupported native training acceptance") + if not verify_files: + raise ValueError("Training acceptance requires verification of original evidence files") + if report.get("events") != list(EVENTS): + raise ValueError("Training acceptance omitted or reordered a required phase") + l2 = report.get("l2_reports", {}) + workers = report.get("workers", {}) + if set(l2) != {"baseline", "update1", "update2", "reload"} or set(workers) != { + "update1", + "update2", + "reload", + "continuous", + "resumed", + }: + raise ValueError("Training omitted an update, full L2 matrix or recovery control") + matrices = {} + versions = { + "baseline": "baseline", + "update1": "update-1", + "update2": "update-2", + "reload": "reload-2", + } + for phase, item in l2.items(): + matrices[phase] = matrix = read_record(item) + validate_matrix_report(matrix) + identity = matrix["identity"] + if identity.get("weight_version") != versions[phase]: + raise ValueError("Training phase used the wrong model weight version") + immutable = set(report["identity"]) - { + "checkpoint", + "checkpoint_identity", + "weight_version", + "base_checkpoint", + "base_checkpoint_identity", + } + if any(identity.get(key) != report["identity"][key] for key in immutable): + raise ValueError("Training phase changed source, environment, provider or topology") + if phase == "baseline": + if identity != report["identity"]: + raise ValueError("Training baseline must retain the exact original model identity") + elif (identity.get("base_checkpoint"), identity.get("base_checkpoint_identity")) != ( + report["identity"]["checkpoint"], + report["identity"]["checkpoint_identity"], + ): + raise ValueError("Updated actor export changed its official base checkpoint identity") + if matrix["fixture"]["content_sha256"] != report["l2_fixture_sha256"]: + raise ValueError("Training L2 changed its frozen input tokens") + identity = report["training_identity"] + if {key: identity.get(key) for key in report["identity"]} != report["identity"]: + raise ValueError("Training checkpoint is detached from the baseline model identity") + if report.get("reload_matches_update2") != compare_reload_matrices( + matrices["update2"], matrices["reload"] + ): + raise ValueError("Reload report does not match the reopened original phase artifacts") + phases = {} + for phase, item in workers.items(): + value = phases[phase] = read_record(item) + validate_worker(value, phase=phase, identity=identity) + if phase in ("update1", "update2", "reload"): + validate_export_evidence(value, identity) + export = value["export"] + if any(rank["export"] != export for rank in value["ranks"]): + raise ValueError("Native ranks disagree on exported weights") + for key in ("checkpoint", "checkpoint_identity", "weight_version"): + if export[key] != matrices[phase]["identity"][key]: + raise ValueError("L2 did not test the actual updated/reloaded actor export") + if phase in ("update1", "update2"): + step = 1 if phase == "update1" else 2 + for rank in value["ranks"]: + validate_update(rank["update"], step=step, fixture=identity["fixture_sha256"]) + for rank in range(4): + if ( + phases["update2"]["ranks"][rank]["update"]["parameter_hashes_before"] + != phases["update1"]["ranks"][rank]["update"]["parameter_hashes_after"] + ): + raise ValueError("Step2 did not start from the actual completed step1 parameters") + if phases["update2"]["ranks"][rank]["state"] != phases["reload"]["ranks"][rank]["state"]: + raise ValueError("Step2 model/optimizer/scheduler/RNG/progress changed on restart") + continuous = phases["continuous"]["ranks"][rank]["controls"] + resumed = phases["resumed"]["ranks"][rank]["controls"] + if set(continuous) != {"1", "2", "3"} or set(resumed) != {"3"}: + raise ValueError("Continuous/restarted controls omitted a required update") + for step in (1, 2, 3): + validate_update( + continuous[str(step)]["update"], step=step, fixture=identity["fixture_sha256"] + ) + if ( + step < 3 + and continuous[str(step)]["state"] + != phases[f"update{step}"]["ranks"][rank]["state"] + ): + raise ValueError("Continuous reference changed an earlier update") + validate_update(resumed["3"]["update"], step=3, fixture=identity["fixture_sha256"]) + if continuous["3"]["state"] != resumed["3"]["state"]: + raise ValueError("Continuous and restarted step3 states are not exactly equal") + checkpoint = report["checkpoint"] + complete = validate_checkpoint(checkpoint["path"], identity, verify_files=False) + if ( + complete["step"] != 2 + or file_digest(Path(checkpoint["path"]) / "COMPLETE.json") != checkpoint["sha256"] + ): + raise ValueError("Training acceptance requires the verified complete step2 checkpoint") + for rank in range(4): + progress = json.loads( + (Path(checkpoint["path"]) / f"rank-{rank}" / "progress.json").read_text() + ) + state = phases["update2"]["ranks"][rank]["state"] + for key in state.keys() - {"scope", "state_sha256"}: + if progress.get(key) != state[key]: + raise ValueError( + "Published checkpoint bytes contradict the step2 recovery identity" + ) + if progress.get("update_evidence") != phases["update2"]["ranks"][rank]["update"]: + raise ValueError("Published checkpoint detached the committed optimizer update") + validate_fault_evidence(read_record(report["fault_injection"]), identity) + return json_digest(report) + + +def validate_fault_evidence(faults, identity): + from qwen3_next_checkpoint import _validate_ranks + + if faults.get("identity") != identity or set(faults.get("cases", {})) != set(FAULTS): + raise ValueError("Checkpoint publication fault matrix is incomplete") + if any(value.get("passed") is not True for value in faults["cases"].values()): + raise ValueError("An interrupted/corrupted checkpoint was not rejected") + interrupted = faults["cases"]["interrupted_writer"] + if interrupted.get("exit_code") != -signal.SIGTERM: + raise ValueError("Interrupted writer did not record its actual termination") + checked_artifact_path(interrupted["writer_file"]) + for case in FAULTS: + path = Path(faults["cases"][case]["checkpoint"]) + if path.is_symlink() or path.suffix != ".partial" or not path.is_dir(): + raise ValueError("Fault evidence did not preserve its original unpublished directory") + try: + if case in ("missing_rank", "hash_error"): + _validate_ranks(path, world_size=4, identity=identity) + else: + validate_checkpoint(path, identity) + except (OSError, ValueError) as exc: + if case == "missing_rank" and not isinstance(exc, FileNotFoundError): + raise ValueError("Missing-rank fixture failed for an unrelated reason") from exc + if case == "hash_error" and not re.search("checksum mismatch|size mismatch", str(exc)): + raise ValueError("Corruption fixture failed for an unrelated reason") from exc + else: + raise ValueError(f"Fault fixture no longer reproduces its rejection: {case}") + + +def checkpoint_faults(output, identity): + """Kill a real writer and corrupt protocol files; never touch model checkpoints.""" + output = Path(output) + output.mkdir(parents=True, exist_ok=False) + partial = output / "interrupted.partial" + partial.mkdir() + code = ( + "import pathlib,sys,time; p=pathlib.Path(sys.argv[1]); " + "p.write_bytes(b'incomplete checkpoint'); time.sleep(300)" + ) + child = subprocess.Popen( + [sys.executable, "-c", code, str(partial / "writing")], start_new_session=True + ) + deadline = time.monotonic() + 10 + try: + while not (partial / "writing").exists(): + if child.poll() is not None or time.monotonic() > deadline: + raise RuntimeError("Fault injection writer did not start") + time.sleep(0.01) + os.killpg(child.pid, signal.SIGTERM) + code = child.wait(timeout=10) + finally: + if child.poll() is None: + os.killpg(child.pid, signal.SIGKILL) + child.wait(timeout=10) + recovery = newest_complete_checkpoint(output, identity) + cases = { + "interrupted_writer": { + "passed": code != 0 and recovery["checkpoint"] is None, + "exit_code": code, + "checkpoint": str(partial), + "writer_file": record(partial / "writing"), + } + } + try: + validate_checkpoint(partial, identity) + except (OSError, ValueError) as exc: + cases["unpublished_load"] = {"passed": True, "error": str(exc), "checkpoint": str(partial)} + partial = output / "missing.partial" + partial.mkdir() + for rank in range(4): + files = {} + for role in ("model", "optimizer", "rng", "progress"): + path = partial / f"rank-{rank}-{role}.bin" + path.write_bytes(f"{rank}:{role}".encode()) + files[role] = [path.name] + write_rank_manifest( + partial, + rank=rank, + world_size=4, + identity=identity, + step=2, + sample_position=8, + files=files, + ) + hidden = partial / "rank-3.manifest.json" + corrupted = output / "corrupted.partial" + shutil.copytree(partial, corrupted) + hidden.unlink() + try: + publish_checkpoint(partial, output / "missing", world_size=4, identity=identity) + except (OSError, ValueError) as exc: + cases["missing_rank"] = {"passed": True, "error": str(exc), "checkpoint": str(partial)} + (corrupted / "rank-2-optimizer.bin").write_bytes(b"corrupt optimizer bytes") + try: + publish_checkpoint(corrupted, output / "corrupted", world_size=4, identity=identity) + except (OSError, ValueError) as exc: + cases["hash_error"] = {"passed": True, "error": str(exc), "checkpoint": str(corrupted)} + result = { + "identity": identity, + "scope": "actual_checkpoint_publication_protocol", + "cases": cases, + } + if set(cases) != set(FAULTS) or not all(value["passed"] for value in cases.values()): + raise ValueError("Checkpoint fault injection accepted incomplete state") + validate_fault_evidence(result, identity) + atomic_json(output / "faults.json", result) + return record(output / "faults.json") + + +def _owned_remove(path, root, identity=None, *, proof=None): + path, root = Path(path), Path(root) + if path.is_symlink() or root.is_symlink() or not path.is_dir(): + raise ValueError("Cleanup target is not a directory owned by this training run") + path, root = path.resolve(), root.resolve() + if path.parent != root: + raise ValueError("Cleanup target is not a direct child of this training run") + if identity is None or proof is None: + raise ValueError("Cleanup requires explicit ownership and retirement proof") + return retire_directory(path, root, identity, proof=proof) + + +def remove_interrupted(root, identity): + return recover_storage(root, identity) + + +def retire_export(worker, root, identity): + validate_export_evidence(worker, identity) + path = Path(worker["export"]["checkpoint"]) + if not path.exists(): + return # A prior attempt may have retired it after durable L2 publication. + complete = path / "EXPORT_COMPLETE.json" + if ( + complete.is_symlink() + or json.loads(complete.read_text()) != worker["export"]["checkpoint_identity"] + ): + raise ValueError("Cleanup export is not the artifact whose complete L2 was retained") + _owned_remove( + path, + root, + identity, + proof={ + "export": worker["export"]["checkpoint_identity"], + "evidence": worker["export_evidence"], + }, + ) + + +def remove_uncommitted_exports(root, identity, journal): + """A crash after export publication cannot retain an unbounded extra copy.""" + from qwen3_next_training_export import validate_training_export_record + + keep = { + str(Path(read_record(item)["export"]["checkpoint"]).resolve()) + for phase, item in journal["workers"].items() + if phase in ("update1", "update2", "reload") and phase not in journal["l2_reports"] + } + removed = [] + for path in Path(root).iterdir(): + if ( + not path.is_dir() + or path.suffix == ".partial" + or path.name.startswith(".") + or str(path.resolve()) in keep + ): + continue + marker = path / "EXPORT_COMPLETE.json" + if path.is_symlink() or marker.is_symlink() or not marker.is_file(): + raise ValueError("Completed export cleanup has no safe ownership proof") + complete = json.loads(marker.read_text()) + validate_training_export_record(complete, complete) + if complete["training_identity"] != identity: + raise ValueError("Completed export belongs to another training run") + removed.append( + {"path": str(path.absolute()), "complete": complete, "action": "retire_unneeded_export"} + ) + _owned_remove(path, root, identity, proof={"export": complete}) + return removed + + +def execute(manifest_path, output): + from qwen3_next_runtime import require_predecessor + + manifest_path, output = Path(manifest_path).resolve(), Path(output).resolve() + manifest = json.loads(manifest_path.read_text()) + run_root = manifest_path.parent + stage = manifest["stage"] + if stage not in STAGE_LAST_PHASE: + raise ValueError("Native training runner requires a training stage") + previous = Path(manifest["previous"]) + require_predecessor(stage, previous, Path(manifest["checkpoint"]), manifest) + prior_result = json.loads((previous / "result.json").read_text()) + continuation = stage != "train1" + if continuation: + # Continue the predecessor training run's durable state under its own identity. + if manifest.get("training_storage"): + raise ValueError("A continuing training stage uses its predecessor's storage") + durable = continued_training_storage(previous) + journal_path = durable / "journal.json" + journal = json.loads(journal_path.read_text()) + baseline_path = Path(journal["l2_reports"]["baseline"]["path"]) + else: + baseline_path = previous / prior_result["evidence"]["l2_matrix"]["path"] + baseline = json.loads(baseline_path.read_text()) + validate_matrix_report(baseline, expected_identity=model_identity(manifest)) + fixture_path = Path(baseline["fixture"]["path"]) + if file_digest(fixture_path) != baseline["fixture"]["sha256"]: + raise ValueError("Baseline L2 fixture was changed") + fixture = policy_fixture(fixture_path) + if continuation: + identity = journal["identity"] + if not continuation_identity_matches( + identity, model_identity(manifest), fixture.content_sha256 + ): + raise ValueError("Training continuation identity differs from the journal") + if journal["l2_reports"]["baseline"] != record(baseline_path): + raise ValueError("Training continuation baseline L2 changed") + else: + identity = { + **model_identity(manifest), + "run_id": manifest["run_id"], + "execution_id": manifest["execution_id"], + "fixture_sha256": fixture.content_sha256, + } + durable = training_storage_root(manifest, run_root) + if not durable.is_absolute(): + raise ValueError("Training storage must be an absolute path") + durable.mkdir(parents=True, exist_ok=True) + checkpoint_root, export_root = durable / "checkpoints", durable / "exports" + owned_root(checkpoint_root, identity) + owned_root(export_root, identity) + journal_path = durable / "journal.json" + journal = { + "schema_version": 1, + "identity": identity, + "workers": {}, + "l2_reports": {"baseline": record(baseline_path)}, + "events": ["l2:baseline"], + "recovery": [], + } + if journal_path.exists(): + journal = json.loads(journal_path.read_text()) + validate_journal(journal, identity) + for phase, value in journal["workers"].items(): + worker = read_record(value) + validate_worker(worker, phase=phase, identity=identity) + if phase in ("update1", "update2", "reload"): + validate_export_evidence(worker, identity) + if repeat_reload_in_current_attempt(journal, os.environ["QWEN3_NEXT_ATTEMPT"]): + validate_journal(journal, identity, verify_files=False) + atomic_json(journal_path, journal) + verify_environment(manifest["environment"], require_venv=True) + check_l2_ready() + run_process( + [manifest["python"], str(ROOT / "setup.py"), "build_ext", "--inplace"], + log_path=output / "build.log", + timeout=1800, + ) + provider_binary = archive_provider_binary(ROOT, output, source_tree=manifest["source_tree"]) + probe = output / "gpu-probe.json" + run_process( + [ + manifest["python"], + str(ROOT / "scripts/qwen3_next_l2.py"), + "--gpu-probe", + "--output", + str(probe), + ], + log_path=output / "gpu-probe.log", + timeout=180, + ) + uuids = json.loads(probe.read_text())["uuids"] + wait_gpu_release(uuids) + # A preempted attempt may have written a whole checkpoint without publishing it: + # publish it (full validation) before interrupted state is retired. + journal["recovery"].extend(resume_interrupted_publications(checkpoint_root, identity)) + for root in (checkpoint_root, export_root): + journal["recovery"].extend(remove_interrupted(root, identity)) + journal["recovery"].extend(remove_uncommitted_exports(export_root, identity, journal)) + atomic_json(journal_path, journal) + export_bytes = sum( + value["size"] for value in manifest["checkpoint_identity"]["shards"].values() + ) + for phase in stage_phases(stage): + if phase not in journal["workers"]: + recovery = newest_complete_checkpoint(checkpoint_root, identity, verify_files=False) + checkpoint = None if phase == "continuous" else recovery["checkpoint"] + directory = output / f"{phase}-{uuid4().hex}" + directory.mkdir() + request = { + "manifest": manifest, + "phase": phase, + "identity": identity, + "fixture": str(fixture_path), + "checkpoint": checkpoint, + "checkpoint_root": str(checkpoint_root), + "export_root": str(export_root), + "export_bytes": export_bytes, + "provider_binary": provider_binary, + "expected_states": { + str(step): [ + rank["state"] + for rank in read_record(journal["workers"][f"update{step}"])["ranks"] + ] + for step in (1, 2) + if f"update{step}" in journal["workers"] + }, + } + request_path, worker_path = directory / "request.json", directory / "worker.json" + atomic_json(request_path, request) + process = run_process( + [ + manifest["python"], + str(Path(__file__)), + "--worker-request", + str(request_path), + "--output", + str(worker_path), + ], + log_path=directory / "worker.log", + timeout=8 * 3600, + ) + wait_gpu_release(uuids) + worker = json.loads(worker_path.read_text()) + worker.update( + { + "gpu_release_verified": True, + "process": process, + "attempt": os.environ["QWEN3_NEXT_ATTEMPT"], + } + ) + validate_worker(worker, phase=phase, identity=identity) + if phase in ("update1", "update2", "reload"): + archive_export(worker, directory, identity) + atomic_json(worker_path, worker) + journal["workers"][phase] = record(worker_path) + journal["events"].append(f"worker:{phase}") + validate_journal(journal, identity, verify_files=False) + atomic_json(journal_path, journal) + worker = read_record(journal["workers"][phase]) + if phase in ("update1", "update2", "reload") and phase not in journal["l2_reports"]: + directory = output / f"{phase}-l2-{uuid4().hex}" + directory.mkdir() + phase_manifest = { + **manifest, + **worker["export"], + "memory": "512G", + "input_fixture": str(fixture_path), + "input_fixture_sha256": file_digest(fixture_path), + "prebuilt_provider_binary": provider_binary, + } + phase_manifest_path = directory / "manifest.json" + atomic_json(phase_manifest_path, phase_manifest) + matrix = run_matrix(phase_manifest_path, directory, fixture_path) + validate_matrix_report(matrix, expected_identity=model_identity(phase_manifest)) + journal["l2_reports"][phase] = record(directory / "l2-matrix.json") + journal["events"].append(f"l2:{phase}") + validate_journal(journal, identity, verify_files=False) + atomic_json(journal_path, journal) + if phase == "reload": + journal["reload_matches_update2"] = compare_reload_matrices( + read_record(journal["l2_reports"]["update2"]), + read_record(journal["l2_reports"]["reload"]), + ) + atomic_json(journal_path, journal) + # Native workers retire the previous full checkpoint immediately after + # verified successor publication; export retirement waits for durable L2. + if phase in ("update1", "update2", "reload"): + retire_export(worker, export_root, identity) + if stage != "train": + _close_partial_training_stage( + manifest, output, run_root, previous, prior_result, journal, identity, baseline, uuids + ) + return + last = read_record(journal["workers"]["reload"])["checkpoint"] + report = { + "schema_version": 1, + "status": "passed", + "scope": "qwen3_next_training_and_reload", + "identity": model_identity(manifest), + "training_identity": identity, + "required_sections": list(SECTIONS), + "l2_fixture_sha256": baseline["fixture"]["content_sha256"], + "l2_reports": journal["l2_reports"], + "workers": journal["workers"], + "events": journal["events"], + "recovery": journal["recovery"], + "reload_matches_update2": journal["reload_matches_update2"], + "checkpoint": {"path": last, "sha256": file_digest(Path(last) / "COMPLETE.json")}, + "fault_injection": checkpoint_faults(output / "checkpoint-faults", identity), + "attempt": os.environ["QWEN3_NEXT_ATTEMPT"], + } + validate_training_report(report) + atomic_json(output / "training-report.json", report) + atomic_json(output / "reload.json", report) + shutil.copyfile(journal["l2_reports"]["reload"]["path"], output / "l2-matrix.json") + shutil.copyfile( + previous / prior_result["evidence"]["operators"]["path"], output / "operators.json" + ) + atomic_json( + output / "environment.json", + {"environment": manifest["environment"], "gpu_uuids": uuids, "execution_gpus": 4}, + ) + evidence = { + name: evidence_record(output / filename, run_root) + for name, filename in { + "updates": "training-report.json", + "reload": "reload.json", + "l2_matrix": "l2-matrix.json", + "operators": "operators.json", + "environment": "environment.json", + }.items() + } + result = result_envelope(manifest, os.environ["QWEN3_NEXT_ATTEMPT"], evidence=evidence) + result.update(full_model_l2="passed", training_and_reload="passed") + validate_result(manifest, result, run_dir=run_root, attempt=os.environ["QWEN3_NEXT_ATTEMPT"]) + atomic_json(output / "result.json", result) + + +def _close_partial_training_stage( + manifest, output, run_root, previous, prior_result, journal, identity, baseline, uuids +): + """Record a train1/train2 result: its phases, L2 matrix and the checkpoint left behind.""" + stage = manifest["stage"] + last_phase = STAGE_LAST_PHASE[stage] + checkpoint = read_record(journal["workers"][last_phase])["checkpoint"] + phases = list(stage_phases(stage)) + if any( + phase not in journal["workers"] or phase not in journal["l2_reports"] for phase in phases + ): + raise ValueError("Partial training stage closed before its phases completed") + progress = { + "schema_version": 1, + "status": "passed", + "scope": SCOPES[stage], + "identity": model_identity(manifest), + "training_identity": identity, + "phases": phases, + "l2_fixture_sha256": baseline["fixture"]["content_sha256"], + "l2_reports": journal["l2_reports"], + "workers": journal["workers"], + "events": journal["events"], + "recovery": journal["recovery"], + "checkpoint": { + "path": checkpoint, + "sha256": file_digest(Path(checkpoint) / "COMPLETE.json"), + }, + "attempt": os.environ["QWEN3_NEXT_ATTEMPT"], + } + atomic_json(output / "training-progress.json", progress) + shutil.copyfile(journal["l2_reports"][last_phase]["path"], output / "l2-matrix.json") + shutil.copyfile( + previous / prior_result["evidence"]["operators"]["path"], output / "operators.json" + ) + atomic_json( + output / "environment.json", + {"environment": manifest["environment"], "gpu_uuids": uuids, "execution_gpus": 4}, + ) + evidence = { + name: evidence_record(output / filename, run_root) + for name, filename in { + "updates": "training-progress.json", + "l2_matrix": "l2-matrix.json", + "operators": "operators.json", + "environment": "environment.json", + }.items() + } + result = result_envelope(manifest, os.environ["QWEN3_NEXT_ATTEMPT"], evidence=evidence) + result.update(full_model_l2="passed", training_phases=phases) + validate_result(manifest, result, run_dir=run_root, attempt=os.environ["QWEN3_NEXT_ATTEMPT"]) + atomic_json(output / "result.json", result) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--manifest", type=Path) + parser.add_argument("--output", type=Path) + parser.add_argument("--worker-request", type=Path) + parser.add_argument("--check-ready", action="store_true") + args = parser.parse_args() + if args.check_ready: + check_l2_ready() + from qwen3_next_training_checkpoint import save_training_checkpoint # noqa: F401 + from qwen3_next_training_export import export_training_weights # noqa: F401 + + print(json.dumps({"status": "ready", "native_training": True})) + return 0 + if args.output is None: + parser.error("--output is required") + if args.worker_request: + request = json.loads(args.worker_request.read_text()) + verify_environment(request["manifest"]["environment"], require_venv=True) + verify_before_model_load(request["manifest"], args.output.parent) + run_training_phase(request, args.output) + return 0 + if args.manifest is None: + parser.error("--manifest is required") + args.output.mkdir(parents=True, exist_ok=True) + try: + execute(args.manifest, args.output) + except Exception as exc: + atomic_json( + args.output / "result.json", + { + "run_id": json.loads(args.manifest.read_text())["run_id"], + "attempt": os.environ["QWEN3_NEXT_ATTEMPT"], + "status": "failed", + "training_and_reload": "not_passed", + **failure_fields(exc), + }, + ) + raise + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/scripts/qwen3_next_training_checkpoint.py b/scripts/qwen3_next_training_checkpoint.py new file mode 100644 index 000000000..75347aba4 --- /dev/null +++ b/scripts/qwen3_next_training_checkpoint.py @@ -0,0 +1,602 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Serialize the real native VIME/Megatron training state, one TP rank at a time. + +DistributedOptimizer.state_dict() deliberately omits master weights and Adam +moments. The bounded native parameter getter/setter adapter preserves all of +these states without allocating whole-bucket CPU packing copies. Publication +and recovery use the all-rank atomic protocol. +""" + +import gc +import json +import os +import uuid +from pathlib import Path + +import torch +import torch.distributed as dist +from qwen3_next_checkpoint import ( + newest_complete_checkpoint, + publish_checkpoint, + require_checkpoint_disk_space, + validate_checkpoint, + write_rank_manifest, +) +from qwen3_next_contract import atomic_json +from qwen3_next_optimizer_checkpoint import ( + load_bounded_parameter_state, + native_parameter_layout, + native_parameter_states, + parameter_state_files, +) +from qwen3_next_optimizer_checkpoint import parameter_state_records as _optimizer_parameter_records +from qwen3_next_optimizer_checkpoint import ( + save_bounded_parameter_state, + verify_hdo_optimizer_master_aliases, +) +from qwen3_next_runtime import create_staging, owned_root + +from rl_engine.integrations.qwen3_next_training import ( + capture_rng_state, + named_trainable_parameters, + restore_rng_state, + state_digest, +) +from rl_engine.testing.qwen3_next_rl_fixture import PolicyFixture, require_policy_configuration + +SCHEMA_VERSION = 1 +CLASS_TAG = "__qwen3_next_native_optimizer_class__" + + +def _native_leaves(args, model, optimizer, tracker): + """Reject an alternate optimizer/topology before any checkpoint work starts.""" + require_policy_configuration(args) + if tracker is None or not tracker.get_states(): + raise ValueError("Native checkpoint must include the initialized Megatron RNG tracker") + if type(getattr(args, "consumed_train_samples", None)) is not int: + raise ValueError("Native training sample progress must be explicit") + from megatron.core import parallel_state + from megatron.core.optimizer.cpu_offloading.hybrid_optimizer import HybridDeviceOptimizer + from megatron.core.optimizer.distrib_optimizer import DistributedOptimizer + from megatron.core.optimizer.optimizer import ChainedOptimizer + + if not dist.is_initialized() or dist.get_world_size() != 4: + raise ValueError("Native checkpoint requires an initialized four-rank process group") + topology = ( + parallel_state.get_tensor_model_parallel_world_size(), + parallel_state.get_pipeline_model_parallel_world_size(), + parallel_state.get_context_parallel_world_size(), + parallel_state.get_expert_model_parallel_world_size(), + parallel_state.get_data_parallel_world_size(), + ) + if topology != (4, 1, 1, 1, 1): + raise ValueError("Native checkpoint requires TP4 and PP/CP/EP/DP1") + if parallel_state.get_tensor_model_parallel_rank() != dist.get_rank(): + raise ValueError("Checkpoint rank order must equal tensor-parallel rank order") + leaves = ( + list(optimizer.chained_optimizers) + if isinstance(optimizer, ChainedOptimizer) + else [optimizer] + ) + if not leaves or any(not isinstance(leaf, DistributedOptimizer) for leaf in leaves): + raise TypeError("Checkpoint requires actual native DistributedOptimizer leaves") + for leaf in leaves: + if getattr(leaf, "is_stub_optimizer", False): + raise ValueError("Stub optimizers cannot provide complete training state") + if leaf.ddp_config.use_megatron_fsdp: + raise ValueError("The fixed TP4 checkpoint path does not use FSDP") + for group in (leaf.data_parallel_group, leaf.data_parallel_group_gloo): + if group is None or dist.get_world_size(group) != 1 or dist.get_rank(group) != 0: + raise ValueError("Serial native optimizer saving requires singleton DP groups") + native = leaf.optimizer + if not isinstance(native, HybridDeviceOptimizer): + raise TypeError("Native checkpoint requires Megatron CPU optimizer offload") + if ( + native.offload_fraction != 1.0 + or not native.param_update_in_fp32 + or native.overlap_cpu_optimizer_d2h_h2d + or native.gpu_optimizer is not None + ): + raise ValueError("Native optimizer must fully offload FP32 masters without overlap") + parameters = named_trainable_parameters(model) + if any(value.dtype != torch.bfloat16 or not value.is_cuda for value in parameters.values()): + raise ValueError("The complete native model must retain BF16 CUDA parameters") + return leaves + + +def _all_rank_call(action, label): + """Propagate a Python failure before another rank enters a later collective.""" + value, error = None, None + try: + value = action() + except Exception as exc: + error = f"{type(exc).__name__}: {exc}" + errors = [None] * 4 + dist.all_gather_object(errors, error) + if any(errors): + raise RuntimeError(f"{label} failed across ranks: {errors}") + return value + + +def _rank_zero_call(action, label): + value = _all_rank_call(lambda: action() if dist.get_rank() == 0 else None, label) + values = [value] + dist.broadcast_object_list(values, src=0) + return values[0] + + +def _serial_rank_call(action, label): + result = None + for owner in range(4): + dist.barrier() + current = _all_rank_call( + lambda: action() if dist.get_rank() == owner else None, f"{label} rank {owner}" + ) + if dist.get_rank() == owner: + result = current + gc.collect() + return result + + +def _native_class_allowlist(leaves): + allowed = {} + for leaf in leaves: + for name in ("cpu_optimizer_cls", "gpu_optimizer_cls"): + cls = getattr(leaf.optimizer, name) + if cls is not None: + key = f"{cls.__module__}.{cls.__qualname__}" + if not isinstance(cls, type) or (key in allowed and allowed[key] is not cls): + raise ValueError("Ambiguous native optimizer class identity") + allowed[key] = cls + return allowed + + +def _encode_header(value, allowed): + """Preserve native class-valued param-group defaults without unsafe unpickling.""" + if isinstance(value, type): + key = f"{value.__module__}.{value.__qualname__}" + if allowed.get(key) is not value: + raise TypeError(f"Unpinned class in native optimizer header: {key}") + return {CLASS_TAG: key} + if isinstance(value, dict): + if CLASS_TAG in value: + raise ValueError("Reserved native class tag in optimizer header") + return {key: _encode_header(item, allowed) for key, item in value.items()} + if isinstance(value, (list, tuple)): + return type(value)(_encode_header(item, allowed) for item in value) + if value is None or isinstance(value, torch.Tensor) or type(value) in (str, bool, int, float): + return value + raise TypeError(f"Unsupported native optimizer header value: {type(value)!r}") + + +def _decode_header(value, allowed): + if isinstance(value, dict): + if CLASS_TAG in value: + if set(value) != {CLASS_TAG} or value[CLASS_TAG] not in allowed: + raise ValueError( + "Saved optimizer class is absent from the running native optimizer" + ) + return allowed[value[CLASS_TAG]] + return {key: _decode_header(item, allowed) for key, item in value.items()} + if isinstance(value, (list, tuple)): + return type(value)(_decode_header(item, allowed) for item in value) + return value + + +def _model_states(model): + if not model: + raise ValueError("Cannot save an empty native model") + return [chunk.state_dict_for_save_checkpoint() for chunk in model] + + +def _state_signature(value): + if isinstance(value, torch.Tensor): + return {"dtype": str(value.dtype), "shape": list(value.shape)} + if isinstance(value, dict): + return {key: _state_signature(item) for key, item in value.items()} + if isinstance(value, (list, tuple)): + return type(value)(_state_signature(item) for item in value) + if value is None or type(value) in (str, bool, int, float): + return type(value).__name__ + raise TypeError(f"Unsupported native model state type: {type(value)!r}") + + +def _tensor_bytes(value, seen=None): + if seen is None: + seen = set() + if isinstance(value, torch.Tensor): + # A serialized view retains its underlying storage, including TP padding. + storage = value.untyped_storage() + key = (value.device, storage.data_ptr(), storage.nbytes()) + if key in seen: + return 0 + seen.add(key) + return storage.nbytes() + if isinstance(value, dict): + return sum(_tensor_bytes(item, seen) for item in value.values()) + if isinstance(value, (list, tuple)): + return sum(_tensor_bytes(item, seen) for item in value) + return 0 + + +def _encode_rng(state): + numpy_state = state["numpy"] + return { + **state, + "numpy": ( + numpy_state[0], + torch.from_numpy(numpy_state[1].copy()), + *numpy_state[2:], + ), + } + + +def _decode_rng(state): + numpy_state = state["numpy"] + if numpy_state[1].dtype != torch.uint32 or numpy_state[1].device.type != "cpu": + raise ValueError("Invalid saved NumPy RNG state") + return {**state, "numpy": (numpy_state[0], numpy_state[1].numpy().copy(), *numpy_state[2:])} + + +def _save_tensor_file(path, payload): + with Path(path).open("xb") as stream: + torch.save(payload, stream) + stream.flush() + os.fsync(stream.fileno()) + + +def _rank_files(rank, chunk_count, rank_dir): + return { + "model": [f"rank-{rank}/model-{index}.pt" for index in range(chunk_count)], + "optimizer": [f"rank-{rank}/{name}" for name in parameter_state_files(rank_dir)], + "rng": [f"rank-{rank}/rng.pt"], + "progress": [ + f"rank-{rank}/{name}" for name in ("progress.json", "scheduler.pt", "fixture.pt") + ], + } + + +def _validate_native_checkpoint(checkpoint, identity, chunk_count): + # Sizes and manifests only: _load_rank compares every loaded tensor's digest. + complete = validate_checkpoint(checkpoint, identity, verify_files=False) + for rank in range(4): + manifest = json.loads((checkpoint / f"rank-{rank}.manifest.json").read_text()) + files = { + role: sorted(record["path"] for record in records) + for role, records in manifest["files"].items() + } + expected = { + role: sorted(paths) + for role, paths in _rank_files(rank, chunk_count, checkpoint / f"rank-{rank}").items() + } + if files != expected: + raise ValueError( + "Native checkpoint manifest omits or substitutes a required state file" + ) + return complete + + +def _save_rank( + model, + optimizer, + scheduler, + fixture, + staging, + *, + leaves, + identity, + step, + sample_position, + tracker, + native_args, + update_evidence, +): + rank = dist.get_rank() + rank_dir = staging / f"rank-{rank}" + rank_dir.mkdir() + torch.cuda.synchronize() + rng = capture_rng_state(tracker=tracker) + states = _model_states(model) + model_hashes = [state_digest(state) for state in states] + native_records, _ = _optimizer_parameter_records(model, leaves) + header = _encode_header(optimizer.state_dict(), _native_class_allowlist(leaves)) + schedule = scheduler.state_dict() + for index, state in enumerate(states): + _save_tensor_file(rank_dir / f"model-{index}.pt", state) + del states + _save_tensor_file(rank_dir / "optimizer-header.pt", header) + save_bounded_parameter_state(model, leaves, rank_dir, _save_tensor_file) + verify_hdo_optimizer_master_aliases(model, leaves) + _save_tensor_file(rank_dir / "rng.pt", _encode_rng(rng)) + _save_tensor_file(rank_dir / "scheduler.pt", schedule) + _save_tensor_file( + rank_dir / "fixture.pt", + { + "tokens": fixture.tokens, + "old_logprobs": fixture.old_logprobs, + "baseline_identity": fixture.baseline_identity, + "content_sha256": fixture.content_sha256, + }, + ) + progress = { + "schema_version": SCHEMA_VERSION, + "scope": "native_vime_training_checkpoint", + "rank": rank, + "world_size": 4, + "identity": identity, + "step": step, + "sample_position": sample_position, + "consumed_train_samples": native_args.consumed_train_samples, + "update_evidence": update_evidence, + "fixture_sha256": fixture.content_sha256, + "model_sha256": model_hashes, + "optimizer_parameter_sha256": native_records, + "optimizer_header_sha256": state_digest(header), + "scheduler_sha256": state_digest(schedule), + "rng_sha256": state_digest(rng), + "serialization": "native_parameter_views_bounded_serial_tp_ranks", + } + atomic_json(rank_dir / "progress.json", progress) + if state_digest(capture_rng_state(tracker=tracker)) != progress["rng_sha256"]: + raise ValueError("Checkpoint serialization unexpectedly changed training RNG state") + write_rank_manifest( + staging, + rank=rank, + world_size=4, + identity=identity, + step=step, + sample_position=sample_position, + files=_rank_files(rank, len(model), rank_dir), + ) + return progress + + +def _load_rank( + model, + optimizer, + scheduler, + fixture, + checkpoint, + *, + leaves, + identity, + complete, + tracker, + native_args, +): + rank = dist.get_rank() + rank_dir = checkpoint / f"rank-{rank}" + progress = json.loads((rank_dir / "progress.json").read_text()) + expected = { + "schema_version": SCHEMA_VERSION, + "scope": "native_vime_training_checkpoint", + "rank": rank, + "world_size": 4, + "identity": identity, + "step": complete["step"], + "sample_position": complete["sample_position"], + "fixture_sha256": fixture.content_sha256, + "serialization": "native_parameter_views_bounded_serial_tp_ranks", + } + if any(progress.get(key) != value for key, value in expected.items()): + raise ValueError("Saved native training progress or fixture identity differs") + saved_fixture = PolicyFixture(**torch.load(rank_dir / "fixture.pt", weights_only=True)) + saved_fixture.validate() + if saved_fixture.content_sha256 != fixture.content_sha256: + raise ValueError("Recovery must reuse the frozen baseline policy samples/logprobs") + header = torch.load(rank_dir / "optimizer-header.pt", map_location="cpu", weights_only=True) + if state_digest(header) != progress["optimizer_header_sha256"]: + raise ValueError("Native optimizer header content mismatch") + # Native load_state_dict may initialize state with a dummy step. Load the + # saved BF16 model afterwards and restore RNG only after every other load. + optimizer.load_state_dict(_decode_header(header, _native_class_allowlist(leaves))) + load_bounded_parameter_state(model, leaves, rank_dir) + verify_hdo_optimizer_master_aliases(model, leaves) + if len(progress["model_sha256"]) != len(model): + raise ValueError("Saved model chunk count differs") + for index, chunk in enumerate(model): + saved = torch.load( + rank_dir / f"model-{index}.pt", map_location="cpu", weights_only=True, mmap=True + ) + current = chunk.state_dict_for_save_checkpoint() + if _state_signature(saved) != _state_signature(current): + raise ValueError("Model checkpoint would change tensor dtypes, shapes or state keys") + if state_digest(saved) != progress["model_sha256"][index]: + raise ValueError("Saved native model content mismatch") + del current + chunk.load_state_dict(saved, strict=True) + del saved + schedule = torch.load(rank_dir / "scheduler.pt", map_location="cpu", weights_only=True) + scheduler.load_state_dict(schedule) + torch.cuda.synchronize() + if [state_digest(value) for value in _model_states(model)] != progress["model_sha256"]: + raise ValueError("Native model reload changed original tensor bits") + restored, _ = _optimizer_parameter_records(model, leaves) + if restored != progress["optimizer_parameter_sha256"]: + raise ValueError("Native optimizer reload changed FP32 master weights or Adam moments") + current_header = _encode_header(optimizer.state_dict(), _native_class_allowlist(leaves)) + if state_digest(current_header) != progress["optimizer_header_sha256"]: + raise ValueError("Native optimizer reload changed optimizer metadata or step") + if state_digest(scheduler.state_dict()) != progress["scheduler_sha256"]: + raise ValueError("Native scheduler reload changed schedule progress") + if type(progress.get("consumed_train_samples")) is not int: + raise ValueError("Saved native consumed sample progress is absent") + native_args.consumed_train_samples = progress["consumed_train_samples"] + rng = _decode_rng(torch.load(rank_dir / "rng.pt", map_location="cpu", weights_only=True)) + if state_digest(rng) != progress["rng_sha256"]: + raise ValueError("Saved RNG content mismatch") + restore_rng_state(rng, tracker=tracker) + if state_digest(capture_rng_state(tracker=tracker)) != progress["rng_sha256"]: + raise ValueError("Native RNG restore changed original state") + return progress + + +def save_training_checkpoint( + args, + model, + optimizer, + scheduler, + fixture, + root, + *, + step, + sample_position, + identity, + tracker, + export_bytes, + update_evidence=None, +): + """Collectively save a complete native checkpoint; call on all four TP ranks.""" + if ( + type(step) is not int + or step not in (1, 2, 3) + or type(sample_position) is not int + or sample_position != step * 4 + ): + raise ValueError("Checkpoint progress must match complete four-sample native updates") + fixture.validate() + leaves = _all_rank_call( + lambda: _native_leaves(args, model, optimizer, tracker), "native topology" + ) + root = Path(root).absolute() + + def estimate(): + optimizer_bytes = sum( + value.numel() * value.element_size() + for record in native_parameter_layout(model, leaves).values() + for states in [native_parameter_states(record)] + for value in states.values() + if isinstance(value, torch.Tensor) + ) + # Four-rank allgather carries only byte counts, never parameter tensors. + model_bytes = sum(_tensor_bytes(state) for state in _model_states(model)) + # Native DP packing retains buffer padding, rather than only model numels. + return model_bytes + (optimizer_bytes * 105 // 100) + 64 * 1024**2 + + local_bytes = _all_rank_call(estimate, "checkpoint disk estimate") + estimates = [None] * 4 + dist.all_gather_object(estimates, local_bytes) + + def prepare(): + owned_root(root, identity) + recovery = newest_complete_checkpoint(root, identity, verify_files=False) + retained = 0 + if recovery["checkpoint"]: + retained = sum( + path.stat().st_size + for path in Path(recovery["checkpoint"]).rglob("*") + if path.is_file() + ) + require_checkpoint_disk_space( + root, + checkpoint_bytes=sum(estimates), + export_bytes=export_bytes, + retained_complete_bytes=retained, + ) + staging, destination = create_staging( + root, + f"step-{step:08d}-{uuid.uuid4().hex}", + { + "schema_version": 1, + "scope": "native_vime_training_checkpoint", + "status": "staging", + "identity": identity, + "step": step, + "sample_position": sample_position, + }, + ) + return {"staging": str(staging), "destination": str(destination), "recovery": recovery} + + paths = _rank_zero_call(prepare, "checkpoint disk budget and staging") + staging, destination = Path(paths["staging"]), Path(paths["destination"]) + _serial_rank_call( + lambda: _save_rank( + model, + optimizer, + scheduler, + fixture, + staging, + leaves=leaves, + identity=identity, + step=step, + sample_position=sample_position, + tracker=tracker, + native_args=args, + update_evidence=update_evidence, + ), + "native checkpoint save", + ) + complete = _rank_zero_call( + lambda: publish_checkpoint(staging, destination, world_size=4, identity=identity), + "checkpoint all-file validation and publication", + ) + return {"checkpoint": str(destination), "complete": complete, "recovery": paths["recovery"]} + + +def load_training_checkpoint( + args, model, optimizer, scheduler, fixture, checkpoint, *, identity, tracker +): + """Verify every rank's file before any load, then restore native state serially.""" + fixture.validate() + leaves = _all_rank_call( + lambda: _native_leaves(args, model, optimizer, tracker), "native topology" + ) + checkpoint = Path(checkpoint).resolve() + complete = _rank_zero_call( + lambda: _validate_native_checkpoint(checkpoint, identity, len(model)), + "checkpoint pre-load all-file validation", + ) + if complete["step"] not in (1, 2, 3) or complete["sample_position"] != complete["step"] * 4: + raise ValueError("Checkpoint progress differs from complete fixed native updates") + progress = _serial_rank_call( + lambda: _load_rank( + model, + optimizer, + scheduler, + fixture, + checkpoint, + leaves=leaves, + identity=identity, + complete=complete, + tracker=tracker, + native_args=args, + ), + "native checkpoint load", + ) + return { + "checkpoint": str(checkpoint), + "step": complete["step"], + "sample_position": complete["sample_position"], + "rank_evidence": progress, + } + + +def training_state_identity( + args, model, optimizer, scheduler, fixture, *, step, sample_position, identity, tracker +): + """Exact per-rank control/recovery state identity, with no PID or path fields.""" + fixture.validate() + if type(step) is not int or step not in (1, 2, 3) or sample_position != step * 4: + raise ValueError("State audit requires complete fixed native updates") + leaves = _native_leaves(args, model, optimizer, tracker) + torch.cuda.synchronize() + records, _ = _optimizer_parameter_records(model, leaves) + header = _encode_header(optimizer.state_dict(), _native_class_allowlist(leaves)) + result = { + "schema_version": SCHEMA_VERSION, + "scope": "native_vime_training_state", + "rank": dist.get_rank(), + "identity": identity, + "step": step, + "sample_position": sample_position, + "consumed_train_samples": args.consumed_train_samples, + "fixture_sha256": fixture.content_sha256, + "model_sha256": [state_digest(value) for value in _model_states(model)], + "optimizer_parameter_sha256": records, + "optimizer_header_sha256": state_digest(header), + "scheduler_sha256": state_digest(scheduler.state_dict()), + "rng_sha256": state_digest(capture_rng_state(tracker=tracker)), + } + return {**result, "state_sha256": state_digest(result)} diff --git a/scripts/qwen3_next_training_export.py b/scripts/qwen3_next_training_export.py new file mode 100644 index 000000000..60b43906a --- /dev/null +++ b/scripts/qwen3_next_training_export.py @@ -0,0 +1,503 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Publish fresh BF16 HF weights through the installed native VIME exporter. + +An export completion record preserves provenance after historical weights are +retired. Only validate_training_export, which rehashes every current file, can +authorize loading an export. A retained record alone cannot authorize a load. +""" + +import hashlib +import json +import math +import os +import re +import uuid +from pathlib import Path + +import torch +import torch.distributed as dist +from qwen3_next_checkpoint import require_checkpoint_disk_space +from qwen3_next_contract import atomic_json, checkpoint_identity +from qwen3_next_runtime import create_staging, file_digest, owned_root +from qwen3_next_training_checkpoint import _all_rank_call, _rank_zero_call + +from rl_engine.integrations.qwen3_next_training import ( + capture_rng_state, + named_trainable_parameters, + state_digest, + tensor_digest, +) + +COMPLETE_FILE = "EXPORT_COMPLETE.json" +INDEX_FILE = "model.safetensors.index.json" +KIND = "qwen3_next_native_hf_export" +# Official tensors the native trainer neither loads nor updates: the multi-token-prediction head. +# VIME's HF saver writes only Megatron-owned parameters, so these are carried from the pinned base. +PASSTHROUGH_PREFIXES = ("mtp.",) +PASSTHROUGH_SHARD = "model-base-passthrough-{idx:05d}-of-{total:05d}.safetensors" +IDENTITY_KEYS = ( + "source_commit", + "source_tree", + "environment_sha256", + "provider_config", + "topology", +) + + +def _json_digest(value): + return hashlib.sha256( + json.dumps(value, sort_keys=True, separators=(",", ":")).encode() + ).hexdigest() + + +def _reference(): + path = Path(__file__).resolve().parents[1] / "rl_engine/testing/qwen3_next_norm_manifest.json" + return json.loads(path.read_text())["model_identity"] + + +def _schema_reference(): + path = Path(__file__).resolve().parents[1] / "rl_engine/testing/qwen3_next_export_schema.json" + record = json.loads(path.read_text()) + if ( + record.get("schema_version") != 1 + or record.get("checkpoint_revision") != _reference()["revision"] + ): + raise ValueError("Pinned export schema must identify the official checkpoint revision") + return record + + +def _safe_file(root, name): + path = Path(name) + if len(path.parts) != 1 or path.is_absolute() or path.name in (".", ".."): + raise ValueError("HF export file must be directly inside its export directory") + candidate = root / path + if candidate.is_symlink() or not candidate.is_file(): + raise ValueError(f"Missing or external HF export file: {name}") + return candidate + + +def _weight_schema(root, *, check_finite): + from safetensors import safe_open + + root = Path(root) + index = json.loads((root / INDEX_FILE).read_text()) + weight_map = index["weight_map"] + if not weight_map or any(not isinstance(key, str) for key in weight_map): + raise ValueError("An HF export must contain the complete named model") + by_shard = {} + for name, shard in weight_map.items(): + if Path(shard).name != shard or not shard.endswith(".safetensors"): + raise ValueError("HF tensor index points outside safetensors shards") + by_shard.setdefault(shard, set()).add(name) + schema = {} + for shard, names in sorted(by_shard.items()): + with safe_open(root / shard, framework="pt", device="cpu") as reader: + if set(reader.keys()) != names: + raise ValueError("HF shard tensor inventory differs from its weight index") + for name in sorted(names): + view = reader.get_slice(name) + shape, dtype = view.get_shape(), view.get_dtype() + if dtype != "BF16" or not shape or any(value <= 0 for value in shape): + raise ValueError(f"HF export must preserve nonempty BF16 weights: {name}") + schema[name] = {"dtype": dtype, "shape": shape} + if check_finite: + # Keep verification memory bounded, including embedding/LM head. + row_size = max(1, math.prod(shape[1:])) + rows = max(1, (32 * 1024**2) // (row_size * 2)) + for start in range(0, shape[0], rows): + if not torch.isfinite(view[start : start + rows]).all(): + raise ValueError(f"Nonfinite exported model weight: {name}") + layers = { + int(match.group(1)) + for name in schema + if (match := re.match(r"model\.layers\.(\d+)\.", name)) + } + if layers != set(range(48)): + raise ValueError("HF export must retain all 48 real decoder layers") + if index.get("metadata", {}).get("total_size") != sum( + math.prod(record["shape"]) * 2 for record in schema.values() + ): + raise ValueError("HF tensor index total byte size differs from its tensors") + return schema + + +def validate_training_export_record(record, expected_identity): + """Validate a durable history record; this deliberately does not authorize loading.""" + if record != expected_identity: + raise ValueError("Published HF export identity differs from the requested weights") + if (record.get("schema_version"), record.get("kind"), record.get("status")) != ( + 1, + KIND, + "complete", + ): + raise ValueError("HF export has no supported completion record") + if (record.get("architecture_layers"), record.get("weights_dtype")) != (48, "bfloat16"): + raise ValueError("HF export architecture or precision differs") + expected_versions = {"update-1": 1, "update-2": 2, "reload-2": 2} + if ( + record.get("weight_version") not in expected_versions + or type(record.get("step")) is not int + or record.get("step") != expected_versions[record["weight_version"]] + ): + raise ValueError("HF export does not identify an accepted native update/reload phase") + training, base = record["training_identity"], record["base_checkpoint_identity"] + if ( + training.get("checkpoint") != record["base_checkpoint"] + or training.get("checkpoint_identity") != base + or any(key not in training for key in IDENTITY_KEYS) + ): + raise ValueError( + "Updated weights require explicit fixed baseline/source/environment provenance" + ) + if base.get("revision") != _reference()["revision"]: + raise ValueError("HF export base revision differs from the official pinned checkpoint") + hashes = record.get("model_rank_sha256") + if record.get("model_identity_method") != "native_parameter_hashes_after": + raise ValueError("HF export must bind directly to the native update parameter audit") + if not isinstance(hashes, list) or len(hashes) != 4: + raise ValueError("HF export requires four actual native model state hashes") + passthrough = record.get("base_passthrough") + if ( + not isinstance(passthrough, dict) + or passthrough.get("prefixes") != list(PASSTHROUGH_PREFIXES) + or type(passthrough.get("tensor_count")) is not int + or passthrough["tensor_count"] < 0 + or not isinstance(passthrough.get("shards"), dict) + ): + raise ValueError("HF export must state which untrained official tensors came from the base") + if any( + not isinstance(value, str) or not re.fullmatch("[0-9a-f]{64}", value) for value in hashes + ): + raise ValueError("HF export has an invalid native model state hash") + files = record.get("files", {}) + shards = {name: value for name, value in files.items() if name.endswith(".safetensors")} + if not shards or INDEX_FILE not in files or "config.json" not in files: + raise ValueError("HF export is missing model shards, index or configuration") + for name, item in files.items(): + if ( + Path(name).name != name + or type(item.get("size")) is not int + or item["size"] <= 0 + or not isinstance(item.get("sha256"), str) + or not re.fullmatch("[0-9a-f]{64}", item["sha256"]) + ): + raise ValueError("Invalid file identity in HF export record") + if record.get("weights_content_sha256") != _json_digest(shards): + raise ValueError("HF export shard content proof differs") + if record.get("metadata") != { + name: value["sha256"] + for name, value in files.items() + if not name.endswith(".safetensors") and name != "STAGING.json" + }: + raise ValueError("HF export metadata proof differs") + reference = _schema_reference() + if ( + record.get("weight_schema_sha256") != reference["weight_schema_sha256"] + or record.get("tensor_count") != reference["tensor_count"] + ): + raise ValueError("HF export omits the pinned complete official tensor schema proof") + return record + + +def validate_training_export(checkpoint, expected_identity): + """Rehash every current export file and exact full-model schema before loading.""" + checkpoint = Path(checkpoint) + if checkpoint.suffix == ".partial" or checkpoint.name.startswith("."): + raise ValueError("An unpublished HF export cannot be loaded") + record = json.loads((checkpoint / COMPLETE_FILE).read_text()) + validate_training_export_record(record, expected_identity) + present = {path.name for path in checkpoint.iterdir() if path.name != COMPLETE_FILE} + if present != record["files"].keys(): + raise ValueError("HF export file inventory changed after publication") + for name, expected in record["files"].items(): + path = _safe_file(checkpoint, name) + if path.stat().st_size != expected["size"] or file_digest(path) != expected["sha256"]: + raise ValueError(f"HF export file checksum mismatch: {name}") + base = Path(record["base_checkpoint"]) + if checkpoint_identity(base) != record["base_checkpoint_identity"]: + raise ValueError("HF export official base metadata or shard identity changed") + schema = _weight_schema(checkpoint, check_finite=True) + if ( + schema != _weight_schema(base, check_finite=False) + or _json_digest(schema) != record["weight_schema_sha256"] + or len(schema) != record["tensor_count"] + ): + raise ValueError("HF export differs from the complete official model tensor schema") + return record + + +def _carry_base_passthrough(staging, base_checkpoint): + """Copy the official tensors the native model does not own from the pinned base, byte for byte. + + Returns the passthrough record for publication. Any missing tensor outside + ``PASSTHROUGH_PREFIXES`` is a real omission by the saver and is rejected; the saver must + also not have written part of the passthrough head itself. + """ + from safetensors import safe_open + from safetensors.torch import save_file + + staging, base = Path(staging), Path(base_checkpoint) + index = json.loads((staging / INDEX_FILE).read_text()) + staged = index["weight_map"] + official = json.loads((base / INDEX_FILE).read_text())["weight_map"] + extra = sorted(set(staged) - set(official)) + if extra: + raise ValueError(f"Native HF saver wrote tensors outside the official schema: {extra[:5]}") + missing = sorted(set(official) - set(staged)) + foreign = [name for name in missing if not name.startswith(PASSTHROUGH_PREFIXES)] + if foreign: + raise ValueError(f"Native HF saver omitted trained official tensors: {foreign[:5]}") + if any(name.startswith(PASSTHROUGH_PREFIXES) for name in staged): + raise ValueError("Native HF saver wrote part of the base passthrough head itself") + record = {"prefixes": list(PASSTHROUGH_PREFIXES), "tensor_count": len(missing), "shards": {}} + if not missing: + return record + by_shard = {} + for name in missing: + by_shard.setdefault(official[name], []).append(name) + added = 0 + for idx, source in enumerate(sorted(by_shard), start=1): + if Path(source).name != source or not source.endswith(".safetensors"): + raise ValueError("Official tensor index points outside safetensors shards") + filename = PASSTHROUGH_SHARD.format(idx=idx, total=len(by_shard)) + if (staging / filename).exists(): + raise ValueError(f"Passthrough shard already present in the staging export: {filename}") + with safe_open(base / source, framework="pt", device="cpu") as reader: + tensors = {name: reader.get_tensor(name) for name in by_shard[source]} + for name, value in tensors.items(): + if ( + value.dtype != torch.bfloat16 + or value.numel() == 0 + or not torch.isfinite(value).all() + ): + raise ValueError(f"Official passthrough tensor is not finite BF16: {name}") + save_file(tensors, staging / filename, metadata={"format": "pt"}) + with safe_open(staging / filename, framework="pt", device="cpu") as reader: + if set(reader.keys()) != set(tensors) or any( + not torch.equal(reader.get_tensor(name), value) for name, value in tensors.items() + ): + raise ValueError( + f"Passthrough shard does not reproduce the base tensors: {filename}" + ) + for name, value in tensors.items(): + staged[name] = filename + added += value.numel() * value.element_size() + record["shards"][filename] = {"source": source, "tensor_count": len(tensors)} + metadata = index.setdefault("metadata", {}) + metadata["total_size"] = int(metadata.get("total_size", 0)) + added + atomic_json(staging / INDEX_FILE, index) + record["names_sha256"] = _json_digest(missing) + return record + + +def _publish_export( + staging, + destination, + *, + step, + weight_version, + identity, + base_checkpoint, + base_checkpoint_identity, + model_hashes, +): + staging, destination, base_checkpoint = Path(staging), Path(destination), Path(base_checkpoint) + if staging.suffix != ".partial" or staging.parent != destination.parent or destination.exists(): + raise ValueError("HF export publication requires a new sibling destination") + if checkpoint_identity(base_checkpoint) != base_checkpoint_identity: + raise ValueError("HF export official base identity changed") + passthrough = _carry_base_passthrough(staging, base_checkpoint) + schema = _weight_schema(staging, check_finite=True) + if schema != _weight_schema(base_checkpoint, check_finite=False): + raise ValueError("Native HF saver did not preserve the complete official tensor schema") + expected_assets = { + name: digest + for name, digest in base_checkpoint_identity["metadata"].items() + if name != INDEX_FILE + } + actual_assets = { + path.name: file_digest(path) + for path in staging.iterdir() + if path.name not in (INDEX_FILE, "STAGING.json") and path.suffix != ".safetensors" + } + if expected_assets != actual_assets: + raise ValueError("Native HF export changed tokenizer/configuration assets") + files = {} + for path in sorted(staging.iterdir()): + _safe_file(staging, path.name) + with path.open("rb") as stream: + os.fsync(stream.fileno()) + files[path.name] = {"size": path.stat().st_size, "sha256": file_digest(path)} + record = { + "schema_version": 1, + "kind": KIND, + "status": "complete", + "weight_version": weight_version, + "step": step, + "export_id": destination.name, + "base_checkpoint": str(base_checkpoint), + "base_checkpoint_identity": base_checkpoint_identity, + "training_identity": identity, + "architecture_layers": 48, + "weights_dtype": "bfloat16", + "model_rank_sha256": model_hashes, + "model_identity_method": "native_parameter_hashes_after", + "base_passthrough": passthrough, + "files": files, + "metadata": { + name: value["sha256"] + for name, value in files.items() + if not name.endswith(".safetensors") and name != "STAGING.json" + }, + "weight_schema_sha256": _json_digest(schema), + "tensor_count": len(schema), + "weights_content_sha256": _json_digest( + {name: value for name, value in files.items() if name.endswith(".safetensors")} + ), + } + validate_training_export_record(record, record) + atomic_json(staging / COMPLETE_FILE, record) + staging.rename(destination) + fd = os.open(destination.parent, os.O_RDONLY | os.O_DIRECTORY) + try: + os.fsync(fd) + finally: + os.close(fd) + return record + + +def export_training_weights( + args, + model, + root, + *, + step, + weight_version, + identity, + base_checkpoint, + base_checkpoint_identity, + checkpoint_bytes, + retained_complete_bytes=0, +): + """Collectively export the current native model; call on all four TP ranks.""" + from megatron.core import parallel_state + from megatron.core.tensor_parallel.random import get_cuda_rng_tracker + from vime.backends.megatron_utils.hf_checkpoint_saver import save_hf_model_to_path + + from rl_engine.integrations.qwen3_next_vime import install_vime_gdn_weight_hooks + + if ( + type(step) is not int + or {"update-1": 1, "update-2": 2, "reload-2": 2}.get(weight_version) != step + ): + raise ValueError("Native HF export requires an explicit accepted update/reload phase") + if ( + identity.get("checkpoint") != str(base_checkpoint) + or identity.get("checkpoint_identity") != base_checkpoint_identity + ): + raise ValueError("Native HF export provenance must retain the fixed official baseline") + if not dist.is_initialized() or dist.get_world_size() != 4: + raise ValueError("Native HF export requires four actual distributed ranks") + if ( + parallel_state.get_tensor_model_parallel_world_size(), + parallel_state.get_expert_tensor_parallel_world_size(), + parallel_state.get_pipeline_model_parallel_world_size(), + parallel_state.get_context_parallel_world_size(), + parallel_state.get_expert_model_parallel_world_size(), + ) != (4, 4, 1, 1, 1): + raise ValueError("Native HF export requires TP/ETP4 and PP/CP/EP1") + if Path(args.hf_checkpoint).resolve() != Path(base_checkpoint).resolve(): + raise ValueError("Native HF conversion metadata must come from the pinned official base") + if ( + getattr(args, "actor_num_nodes", None) != 1 + or getattr(args, "actor_num_gpus_per_node", None) != 4 + ): + raise ValueError("Native HF export requires one node writer for its four GPUs") + if not 0 < args.update_weight_buffer_size <= 4 * 1024**3: + raise ValueError("Native HF export must use a pinned bounded parameter gather size") + if save_hf_model_to_path.__module__ != "vime.backends.megatron_utils.hf_checkpoint_saver": + raise ValueError("Native VIME HF exporter was replaced") + install_vime_gdn_weight_hooks() + tracker = get_cuda_rng_tracker() + parameters = named_trainable_parameters(model) + if any(value.dtype != torch.bfloat16 for value in parameters.values()): + raise ValueError("Native HF export must retain BF16 model parameters") + torch.cuda.synchronize() + rng_before = state_digest(capture_rng_state(tracker=tracker)) + + def parameter_identity(): + return state_digest({name: tensor_digest(value) for name, value in parameters.items()}) + + model_before = _all_rank_call(parameter_identity, "native export model identity") + model_hashes = [None] * 4 + dist.all_gather_object(model_hashes, model_before) + root = Path(root).absolute() + + def prepare(): + owned_root(root, identity) + if checkpoint_identity(base_checkpoint) != base_checkpoint_identity: + raise ValueError("Native HF export official base identity changed before conversion") + export_bytes = sum(item["size"] for item in base_checkpoint_identity["shards"].values()) + require_checkpoint_disk_space( + root, + checkpoint_bytes=checkpoint_bytes, + export_bytes=export_bytes, + retained_complete_bytes=retained_complete_bytes, + ) + staging, destination = create_staging( + root, + f"{weight_version}-{uuid.uuid4().hex}", + { + "schema_version": 1, + "scope": KIND, + "status": "staging", + "identity": identity, + "step": step, + "weight_version": weight_version, + "base_checkpoint": str(base_checkpoint), + "base_checkpoint_identity": base_checkpoint_identity, + }, + ) + return {"staging": str(staging), "destination": str(destination)} + + paths = _rank_zero_call(prepare, "native HF export disk budget and staging") + save_hf_model_to_path( + args, + paths["staging"], + model, + model_name="qwen3_next", + quantization_config=None, + progress_desc=f"Native HF export {weight_version}", + ) + torch.cuda.synchronize() + + def verify_immutable(): + if ( + parameter_identity() != model_before + or state_digest(capture_rng_state(tracker=tracker)) != rng_before + ): + raise ValueError("Native HF export unexpectedly changed model parameters or RNG") + + _all_rank_call(verify_immutable, "native export model/RNG immutability") + record = _rank_zero_call( + lambda: _publish_export( + paths["staging"], + paths["destination"], + step=step, + weight_version=weight_version, + identity=identity, + base_checkpoint=base_checkpoint, + base_checkpoint_identity=base_checkpoint_identity, + model_hashes=model_hashes, + ), + "native HF all-file publication", + ) + return { + "checkpoint": paths["destination"], + "checkpoint_identity": record, + "weight_version": weight_version, + "base_checkpoint": str(base_checkpoint), + "base_checkpoint_identity": base_checkpoint_identity, + } diff --git a/setup.py b/setup.py index 9831c7022..59be67c20 100644 --- a/setup.py +++ b/setup.py @@ -472,7 +472,7 @@ def build_extension(self, ext): "tabulate", "numpy", "accelerate", - "transformers==5.13.1", + "transformers>=5.13.1,<=5.17.0", ], ext_modules=get_extensions(), cmdclass=get_cmdclass(), @@ -480,6 +480,13 @@ def build_extension(self, ext): "cuda": ["flashinfer"], "rocm": ["aiter"], "vllm": ["vllm>=0.6.0"], + "qwen3-next": [ + "torch==2.13.0", + "transformers==5.17.0", + "vllm==0.30.0", + "triton==3.7.1", + "flashinfer-python==0.6.18.post1", + ], "drift-viewer": ["Pillow>=10", "PySide6>=6.6"], }, entry_points={ diff --git a/tests/check_qwen3_next_batch_invariance.py b/tests/check_qwen3_next_batch_invariance.py new file mode 100644 index 000000000..7611da505 --- /dev/null +++ b/tests/check_qwen3_next_batch_invariance.py @@ -0,0 +1,453 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Which GDN components are batch-invariant, per component, measured. + +vLLM refuses strict mode for this model at engine init: + + VLLM batch_invariant mode is not supported for GDN_ATTN. + +The guard is a capability declaration -- `selector.py:235` raises when +`VLLM_BATCH_INVARIANT` is set and the mamba backend's +`supports_batch_invariance()` is False, which `gdn_attn` does not override. So the +question that decides whether full-model L2 is reachable is not "can the guard be +bypassed" (it must not be) but "are the components on the strict path actually +batch-invariant, and if not, which one is not". + +Measured on B200 (sm_100), vLLM 0.30.0, per component: + +========================================= ====================================== +component batch invariance +========================================= ====================================== +`causal_conv1d_update` bitwise (8 seeds x 4 positions, + fp32 and bf16 cache) +`fused_recurrent_..._packed_decode` bitwise (12 seeds x 4 positions, + fp32 and bf16 state) +FLA Triton `chunk_gated_delta_rule` bitwise w.r.t. batch composition +FlashInfer GDN prefill **not** bitwise +========================================= ====================================== + +FlashInfer is the *default* prefill backend on SM100 (`_resolve_gdn_prefill_backend` +returns "flashinfer" for compute capability 100 with `head_k_dim == 128` and CUDA +>= 13), so the default configuration is genuinely not batch-invariant and the +guard is correct rather than conservative. The deviation is rounding-scale, not +structural: adding a companion sequence moved 12149 of 262144 output elements, +`max|diff| = 2.44e-04` against a tensor of scale 7.18e-02 (3.4e-03 relative), and +left the final state within 5.7e-03 relative while almost no bit patterns matched +-- the signature of a pervasive small change in reduction order, consistent with +chunk tiling that depends on the batch's token count. + +**The actionable consequence**: pinning `gdn_prefill_backend="triton"` makes every +component measured here bitwise batch-invariant. That is the configuration a +full-model L2 attempt should use. It does not by itself justify flipping +`supports_batch_invariance()`, which is a statement about the whole backend -- +metadata construction, chunked-prefill splitting, CUDA graphs and the speculative +paths are outside this file's scope and remain unmeasured. + +These assertions therefore state what is true (the three invariant components) and +bound the deviation of the fourth. They deliberately do NOT assert that FlashInfer +*differs*: that would be asserting a defect persists, and would fail the day +upstream fixes it, which is the opposite of what we want to learn. + +Named ``check_`` rather than ``test_`` because it imports real vLLM; see +``tests/distributed/check_*.py``. Run via ``ci/run_qwen3_next_operator_gates.sh`` +or explicitly: + + CUDA_VISIBLE_DEVICES=1 pytest tests/check_qwen3_next_batch_invariance.py -v +""" + +from __future__ import annotations + +from functools import partial + +import pytest +import torch + +from rl_engine.testing.tensor_identity import tensor_bitwise_equal + +pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA is required") + +# Qwen3-Next-80B-A3B-Instruct GDN dimensions. +_H, _HV, _K, _V = 16, 32, 128, 128 +_SCALE = _K**-0.5 +_CONV_DIM, _CONV_WIDTH = 8192, 4 +_PACKED_DIM = _H * _K * 2 + _HV * _V + +#: Positions within the batch whose target sequence is re-run alone. +_POSITIONS = (0, 1, 31, 63) +_BATCH = 64 + + +def _randn(generator, *shape, dt=torch.bfloat16): + return torch.randn(*shape, device="cuda", dtype=dt, generator=generator) + + +def _l2_normalize(t: torch.Tensor) -> torch.Tensor: + """``t / sqrt(sum(t*t) + 1e-6)`` over the last dim. + + q/k arrive L2-normalized in the real path: prefill runs + ``fused_post_conv_prep(apply_l2norm=True)`` and then passes + ``use_qk_l2norm_in_kernel=False``. Feeding raw randn with that flag off leaves + them unnormalized and the delta-rule update diverges to NaN -- a property of + the inputs, not of the kernel. The division is by the norm cast to ``t``'s + dtype, which is what the measurements recorded in the execution doc used. + """ + norm = torch.sqrt((t.float() * t.float()).sum(-1, keepdim=True) + 1e-6) + return t / norm.to(t.dtype) + + +def _gen(seed: int) -> torch.Generator: + return torch.Generator(device="cuda").manual_seed(seed) + + +def _decay(hv: int, tokens: int, g: torch.Generator) -> torch.Tensor: + """``g = -exp(A_log) * softplus(a)``, which is always <= 0. + + Feeding a sign-free random tensor instead lets ``exp(g) > 1`` and the + recurrence diverges to NaN, which looks like a kernel fault and is not one. + """ + a_log = torch.randn(hv, device="cuda", dtype=torch.float32, generator=g) + a = torch.randn(1, tokens, hv, device="cuda", dtype=torch.float32, generator=g) + return -torch.exp(a_log) * torch.nn.functional.softplus(a) + + +# --------------------------------------------------------------------------- # +# 1. Causal conv1d state update -- bitwise +# --------------------------------------------------------------------------- # +@pytest.mark.parametrize("state_dtype", [torch.float32, torch.bfloat16]) +def test_causal_conv1d_update_is_batch_invariant(state_dtype): + pytest.importorskip("vllm") + from vllm.model_executor.layers.mamba.ops.causal_conv1d import causal_conv1d_update + + for seed in range(8): + g = _gen(seed) + x = torch.randn(_BATCH, _CONV_DIM, device="cuda", dtype=torch.bfloat16, generator=g) + # One cache line per sequence plus the null block. + state = torch.randn( + _BATCH + 2, _CONV_DIM, _CONV_WIDTH - 1, device="cuda", dtype=state_dtype, generator=g + ) + weight = torch.randn( + _CONV_DIM, _CONV_WIDTH, device="cuda", dtype=torch.bfloat16, generator=g + ) + bias = torch.randn(_CONV_DIM, device="cuda", dtype=torch.bfloat16, generator=g) + indices = torch.arange(1, _BATCH + 1, device="cuda", dtype=torch.int32) + + full_state, full_out = state.clone(), torch.empty_like(x) + causal_conv1d_update( + x, full_state, weight, bias, "silu", conv_state_indices=indices, out=full_out + ) + + for row in _POSITIONS: + lone_state = state.clone() + lone_out = torch.empty_like(x[row : row + 1]) + causal_conv1d_update( + x[row : row + 1], + lone_state, + weight, + bias, + "silu", + conv_state_indices=indices[row : row + 1].clone(), + out=lone_out, + ) + block = int(indices[row]) + assert tensor_bitwise_equal(lone_out[0], full_out[row]), f"out seed={seed} row={row}" + assert tensor_bitwise_equal(lone_state[block], full_state[block]), f"state row={row}" + + +# --------------------------------------------------------------------------- # +# 2. Packed recurrent decode -- bitwise. This is the rollout decode provider. +# --------------------------------------------------------------------------- # +@pytest.mark.parametrize("state_dtype", [torch.float32, torch.bfloat16]) +def test_packed_recurrent_decode_is_batch_invariant(state_dtype): + pytest.importorskip("vllm") + from vllm.third_party.flash_linear_attention.ops import ( + fused_recurrent_gated_delta_rule_packed_decode as decode, + ) + + def run(mixed, a, b, a_log, dt_bias, state, indices): + working = state.clone() + out = torch.empty(mixed.shape[0], 1, _HV, _V, device="cuda", dtype=mixed.dtype) + decode( + mixed, + a, + b, + a_log, + dt_bias, + _SCALE, + working, + out, + indices, + use_qk_l2norm_in_kernel=True, + ) + return out, working + + for seed in range(12): + g = _gen(seed) + mk = partial(_randn, g) + mixed, a, b = mk(_BATCH, _PACKED_DIM), mk(_BATCH, _HV), mk(_BATCH, _HV) + a_log, dt_bias = mk(_HV, dt=torch.float32), mk(_HV, dt=torch.float32) + state = mk(_BATCH + 2, _HV, _V, _K, dt=state_dtype) * 0.1 + indices = torch.arange(1, _BATCH + 1, device="cuda", dtype=torch.int32) + + full_out, full_state = run(mixed, a, b, a_log, dt_bias, state, indices) + for row in _POSITIONS: + lone_out, lone_state = run( + mixed[row : row + 1], + a[row : row + 1], + b[row : row + 1], + a_log, + dt_bias, + state, + indices[row : row + 1].clone(), + ) + block = int(indices[row]) + assert tensor_bitwise_equal(lone_out[0], full_out[row]), f"out seed={seed} row={row}" + assert tensor_bitwise_equal(lone_state[block], full_state[block]), f"state row={row}" + + +# --------------------------------------------------------------------------- # +# 3 & 4. Chunked prefill, per backend +# --------------------------------------------------------------------------- # +def _prefill_inputs(seqs, seed): + """Inputs for ``seqs`` packed into one varlen batch. + + Each sequence draws from its own generator (``seed + i``), so the first + sequence's tensors are identical whatever follows it. One generator shared + across the batch would make them depend on the total token count: the CUDA + RNG offset advances in rounded blocks, which hides that for short batches + and stops hiding it for longer ones, silently comparing different data. + """ + a_log = torch.randn(_HV, device="cuda", dtype=torch.float32, generator=_gen(seed + 10_000)) + parts = {name: [] for name in ("q", "k", "v", "g", "beta")} + for index, tokens in enumerate(seqs): + g = _gen(seed + index) + mk = partial(_randn, g) + parts["q"].append(_l2_normalize(mk(1, tokens, _HV, _K))) + parts["k"].append(_l2_normalize(mk(1, tokens, _HV, _K))) + parts["v"].append(mk(1, tokens, _HV, _V)) + a = torch.randn(1, tokens, _HV, device="cuda", dtype=torch.float32, generator=g) + parts["g"].append(-torch.exp(a_log) * torch.nn.functional.softplus(a)) + parts["beta"].append( + torch.rand(1, tokens, _HV, device="cuda", dtype=torch.float32, generator=g) + ) + cu = torch.tensor( + [0, *torch.cumsum(torch.tensor(seqs), 0).tolist()], device="cuda", dtype=torch.int32 + ) + return { + **{name: torch.cat(tensors, dim=1) for name, tensors in parts.items()}, + "cu_seqlens": cu, + "initial_state": torch.zeros(len(seqs), _HV, _V, _K, device="cuda", dtype=torch.float32), + } + + +#: The target sequence, then the companions appended after it. The long ones are +#: what a shared generator used to get wrong; see `_prefill_inputs`. +_COMPANIONS = ([], [64], [17, 95], [3], [1], [64, 64, 64, 64], [2048]) +_TARGET = 64 + + +def test_target_inputs_do_not_depend_on_companions(): + """Guards the comparisons below: they must compare the same target data.""" + alone = _prefill_inputs([_TARGET], seed=0) + for companions in _COMPANIONS: + batch = _prefill_inputs([_TARGET, *companions], seed=0) + for name in ("q", "k", "v", "g", "beta"): + assert tensor_bitwise_equal(batch[name][:, :_TARGET], alone[name]), (name, companions) + + +def test_triton_chunked_prefill_is_batch_invariant(): + """Supplementary native batch probe; shared full/chunk/decode uses packed recurrence.""" + pytest.importorskip("vllm") + from vllm.third_party.flash_linear_attention.ops import chunk_gated_delta_rule + + reference = None + for companions in _COMPANIONS: + inp = _prefill_inputs([_TARGET, *companions], seed=0) + out, final_state = chunk_gated_delta_rule( + inp["q"], + inp["k"], + inp["v"], + inp["g"], + inp["beta"], + scale=_SCALE, + initial_state=None, + output_final_state=True, + cu_seqlens=inp["cu_seqlens"], + use_qk_l2norm_in_kernel=False, + ) + target_out, target_state = out[0, :_TARGET], final_state[0] + assert not torch.isnan(target_out).any(), f"NaN with companions={companions}" + if reference is None: + reference = (target_out.clone(), target_state.clone()) + continue + assert tensor_bitwise_equal(target_out, reference[0]), f"out, companions={companions}" + assert tensor_bitwise_equal(target_state, reference[1]), f"state, companions={companions}" + + +def test_flashinfer_chunked_prefill_deviation_stays_rounding_scale(): + """The DEFAULT prefill backend on SM100, which is not batch-invariant. + + Only the magnitude is asserted. Asserting that it *differs* would be asserting + a defect persists, and would start failing the day upstream makes it + invariant -- which is the outcome we want. A structural regression (a + deviation well past rounding scale) still fails here. + """ + pytest.importorskip("vllm") + pytest.importorskip("flashinfer") + from vllm.model_executor.layers.mamba.gdn.qwen_gdn_linear_attn import fi_chunk_gated_delta_rule + + reference = None + for companions in _COMPANIONS: + inp = _prefill_inputs([_TARGET, *companions], seed=0) + out, final_state = fi_chunk_gated_delta_rule( + inp["q"], + inp["k"], + inp["v"], + inp["g"], + inp["beta"], + inp["initial_state"], + True, + cu_seqlens=inp["cu_seqlens"], + use_qk_l2norm_in_kernel=True, + ) + target_out, target_state = out[0, :_TARGET], final_state[0] + assert not torch.isnan(target_out).any(), f"NaN with companions={companions}" + if reference is None: + reference = (target_out.clone(), target_state.clone()) + continue + for name, got, want in ( + ("out", target_out, reference[0]), + ("state", target_state, reference[1]), + ): + scale = max(want.float().abs().max().item(), 1e-9) + relative = (got.float() - want.float()).abs().max().item() / scale + assert relative <= 5e-2, ( + f"{name} deviation {relative:.3e} relative with companions=" + f"{companions} is past rounding scale; measured baseline was " + "3.4e-03 (out) and 5.7e-03 (state)" + ) + + +# --------------------------------------------------------------------------- # +# 5. Metadata construction +# +# `GDNMetadataBuilder` derives the chunk partition from `cu_seqlens` via +# `prepare_chunk_indices` / `prepare_chunk_offsets`. Per-sequence chunk counts +# are `cdiv(own_length, FLA_CHUNK_SIZE)`, so a sequence's partition cannot depend +# on its companions -- only its sequence id and its offset into the flat chunk +# list shift. This is pure index arithmetic, so it is checked exactly, on CPU. +# --------------------------------------------------------------------------- # +def _chunk_metadata(seqs): + from vllm.third_party.flash_linear_attention.ops.index import ( + prepare_chunk_indices, + prepare_chunk_offsets, + ) + from vllm.third_party.flash_linear_attention.ops.utils import FLA_CHUNK_SIZE + + cu = torch.tensor([0, *torch.cumsum(torch.tensor(seqs), 0).tolist()], dtype=torch.int32) + return ( + prepare_chunk_indices(cu, FLA_CHUNK_SIZE), + prepare_chunk_offsets(cu, FLA_CHUNK_SIZE), + FLA_CHUNK_SIZE, + ) + + +@pytest.mark.parametrize("position", [0, 1, 2]) +def test_chunk_metadata_partition_is_batch_invariant(position): + """A sequence's own chunk partition must not move when the batch changes. + + The sequence id and the flat offset legitimately shift with position; what + must not change is how many chunks the sequence is cut into and which of its + own tokens each chunk covers. + """ + pytest.importorskip("vllm") + target = 150 # deliberately not a multiple of the chunk size + reference = None + for others in ([], [64], [17, 95], [3], [1, 1, 1]): + seqs = list(others) + seqs.insert(min(position, len(seqs)), target) + seq_id = min(position, len(others)) + + indices, offsets, chunk_size = _chunk_metadata(seqs) + mine = indices[indices[:, 0] == seq_id] + # Chunk ids within the sequence, and the flat span it occupies. + partition = ( + mine[:, 1].tolist(), + int(offsets[seq_id + 1] - offsets[seq_id]), + ) + expected_chunks = -(-target // chunk_size) # ceil div + assert partition[0] == list(range(expected_chunks)), f"others={others}" + assert partition[1] == expected_chunks, f"offset span, others={others}" + if reference is None: + reference = partition + else: + assert partition == reference, f"others={others}" + + +# --------------------------------------------------------------------------- # +# 6. Chunked-prefill splitting +# +# vLLM may process one prompt over several forward passes, handing the recurrent +# state between them. Whether that equals a single pass is the prefill analogue +# of the prefill-vs-decode question, and it is what decides L2 for prompts longer +# than `max_num_batched_tokens`. +# --------------------------------------------------------------------------- # +def _run_triton_prefill(inp, tokens, initial_state): + from vllm.third_party.flash_linear_attention.ops import chunk_gated_delta_rule + + lo, hi = tokens + cu = torch.tensor([0, hi - lo], device="cuda", dtype=torch.int32) + return chunk_gated_delta_rule( + inp["q"][:, lo:hi], + inp["k"][:, lo:hi], + inp["v"][:, lo:hi], + inp["g"][:, lo:hi], + inp["beta"][:, lo:hi], + scale=_SCALE, + initial_state=initial_state, + output_final_state=True, + cu_seqlens=cu, + use_qk_l2norm_in_kernel=False, + ) + + +@pytest.mark.parametrize("split", [128, 100, 64, 192]) +def test_triton_chunked_prefill_split_matches_one_pass(split): + """Splitting a prompt and handing over the state, versus one pass. + + `split=128` and `split=64` are multiples of FLA_CHUNK_SIZE; `split=100` and + `split=192` are not, which is the realistic case because an engine-step + boundary is set by the token budget, not by the kernel's chunk size. + """ + pytest.importorskip("vllm") + total = 256 + inp = _prefill_inputs([total], seed=0) + + zero = torch.zeros(1, _HV, _V, _K, device="cuda", dtype=torch.float32) + whole_out, whole_state = _run_triton_prefill(inp, (0, total), zero.clone()) + first_out, first_state = _run_triton_prefill(inp, (0, split), zero.clone()) + second_out, second_state = _run_triton_prefill(inp, (split, total), first_state) + + joined = torch.cat([first_out[0], second_out[0]], dim=0) + aligned = split % 64 == 0 + assert not torch.isnan(whole_out).any(), "degenerate inputs, not a kernel fault" + + out_rel = ( + (joined.float() - whole_out[0].float()).abs().max() + / max(whole_out[0].float().abs().max().item(), 1e-9) + ).item() + state_rel = ( + (second_state.float() - whole_state.float()).abs().max() + / max(whole_state.float().abs().max().item(), 1e-9) + ).item() + + if aligned: + # A chunk-aligned split must be bitwise: the kernel's own chunk + # boundaries already fall there, so no reduction order changes. + assert tensor_bitwise_equal(joined, whole_out[0]), f"split={split} out, rel={out_rel:.3e}" + assert tensor_bitwise_equal(second_state, whole_state), f"split={split} state" + else: + # An unaligned split re-cuts the sequence, so the reduction order moves. + # Bound it; do not assert equality, and do not assert inequality either. + assert out_rel <= 5e-2, f"split={split} out deviation {out_rel:.3e}" + assert state_rel <= 5e-2, f"split={split} state deviation {state_rel:.3e}" diff --git a/tests/check_qwen3_next_engine_metadata.py b/tests/check_qwen3_next_engine_metadata.py new file mode 100644 index 000000000..05c91d121 --- /dev/null +++ b/tests/check_qwen3_next_engine_metadata.py @@ -0,0 +1,118 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Real vLLM builder semantics; no GPU kernel or capability override is needed.""" + +from types import SimpleNamespace + +import pytest +import torch +from vllm.config import CompilationConfig +from vllm.config.compilation import CompilationMode, CUDAGraphMode +from vllm.v1.attention.backend import CommonAttentionMetadata +from vllm.v1.kv_cache_interface import MambaSpec + +from rl_engine.integrations.qwen3_next_vllm import ( + SharedGDNBackend, + SharedGDNMetadataBuilder, + metadata_sequences, +) + + +def build_metadata(lengths, computed, slots): + config = SimpleNamespace( + additional_config={"gdn_prefill_backend": "triton"}, + speculative_config=None, + compilation_config=CompilationConfig( + mode=CompilationMode.NONE, cudagraph_mode=CUDAGraphMode.NONE + ), + scheduler_config=SimpleNamespace(max_num_seqs=4), + parallel_config=SimpleNamespace(decode_context_parallel_size=1), + cache_config=SimpleNamespace(mamba_cache_mode="none"), + model_config=SimpleNamespace(hf_text_config=SimpleNamespace(linear_key_head_dim=128)), + ) + spec = MambaSpec( + block_size=64, + shapes=((3, 2048), (8, 128, 128)), + dtypes=(torch.bfloat16, torch.float32), + mamba_cache_mode="none", + ) + builder = SharedGDNMetadataBuilder( + spec, ["model.layers.0.linear_attn"], config, torch.device("cpu") + ) + cu = torch.tensor([0, *torch.tensor(lengths).cumsum(0).tolist()], dtype=torch.int32) + seq = torch.tensor(lengths, dtype=torch.int32) + torch.tensor(computed, dtype=torch.int32) + common = CommonAttentionMetadata( + query_start_loc=cu, + query_start_loc_cpu=cu, + seq_lens=seq, + num_reqs=len(lengths), + num_actual_tokens=sum(lengths), + max_query_len=max(lengths), + max_seq_len=int(seq.max()), + block_table_tensor=torch.tensor(slots, dtype=torch.int32)[:, None], + slot_mapping=torch.zeros(sum(lengths), dtype=torch.int64), + ) + return builder.build(0, common) + + +def test_fresh_single_token_prompt_resets_reused_slot(): + metadata = build_metadata([1], [0], [3]) + slots, cu, reset = metadata_sequences(metadata, 1) + assert slots.tolist() == [3] and cu.tolist() == [0, 1] and reset.tolist() == [3] + + +def test_mixed_step_resets_only_new_prefill_and_preserves_request_order(): + metadata = build_metadata([1, 8, 3], [64, 0, 7], [7, 3, 5]) + slots, cu, reset = metadata_sequences(metadata, 12) + assert slots.tolist() == [7, 3, 5] + assert cu.tolist() == [0, 1, 9, 12] + assert reset.tolist() == [3] + + +def test_null_padding_has_no_state_owner(): + metadata = build_metadata([1, 0], [64, 0], [7, 0]) + slots, cu, reset = metadata_sequences(metadata, 1) + assert slots.tolist() == [7] and cu.tolist() == [0, 1] and reset.numel() == 0 + + +@pytest.mark.parametrize("slots", [[0], [-1], [3, 3]]) +def test_active_null_negative_or_duplicate_slots_are_rejected(slots): + metadata = build_metadata([1] * len(slots), [2] * len(slots), slots) + with pytest.raises(ValueError, match="distinct positive"): + metadata_sequences(metadata, len(slots)) + + +def test_custom_backend_cannot_bypass_the_batch_invariance_guard(): + from vllm.v1.attention.backends.gdn_attn import GDNAttentionBackend + + assert SharedGDNBackend.supports_batch_invariance() is False + assert GDNAttentionBackend.supports_batch_invariance() is False + assert SharedGDNBackend.get_name() != GDNAttentionBackend.get_name() + + +def test_real_selector_rejects_the_unaccepted_custom_backend(monkeypatch): + from vllm import envs + from vllm.v1.attention.backends.registry import MambaAttentionBackendEnum, register_backend + from vllm.v1.attention.selector import _cached_get_mamba_attn_backend, get_mamba_attn_backend + + from rl_engine.integrations.qwen3_next_vllm import _register_backend + + try: + previous = MambaAttentionBackendEnum.CUSTOM.get_path() + except ValueError: + previous = None + monkeypatch.setattr(envs, "VLLM_BATCH_INVARIANT", True) + _cached_get_mamba_attn_backend.cache_clear() + try: + _register_backend() + with pytest.raises(RuntimeError, match="not supported for RL_KERNEL_QWEN3_NEXT_GDN"): + get_mamba_attn_backend(MambaAttentionBackendEnum.CUSTOM) + with pytest.raises(RuntimeError, match="not supported for GDN_ATTN"): + get_mamba_attn_backend(MambaAttentionBackendEnum.GDN_ATTN) + finally: + if previous is None: + MambaAttentionBackendEnum.CUSTOM.clear_override() + else: + register_backend(MambaAttentionBackendEnum.CUSTOM, previous, is_mamba=True) + _cached_get_mamba_attn_backend.cache_clear() diff --git a/tests/check_qwen3_next_forward.py b/tests/check_qwen3_next_forward.py index ce4beb1f7..5df5b9a9a 100644 --- a/tests/check_qwen3_next_forward.py +++ b/tests/check_qwen3_next_forward.py @@ -1,7 +1,9 @@ # SPDX-License-Identifier: Apache-2.0 # Copyright (c) 2026 RL-Kernel Contributors -"""Mandatory shared CUDA GEMM and MoE checks; these do not claim model L2.""" +"""Mandatory shared CUDA GEMM/MoE/logprob checks; these do not claim model L2.""" + +from dataclasses import replace import pytest import torch @@ -10,9 +12,18 @@ from rl_engine.integrations.qwen3_next_forward import ( combine_routes, shared_linear, + shared_logprob, shared_moe, stable_top10_routes, ) +from rl_engine.kernels.logprob_contract import ( + LogprobContract, + LogprobDType, + LogprobRole, + MaskSpec, + ReductionSpec, + ShardingSpec, +) from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact @@ -157,6 +168,48 @@ def test_moe_vjp_matches_independent_double_formula(): assert (value.grad.double() - reference.grad).norm() / reference.grad.norm() < 0.016 +def logprob_contract(tokens): + return LogprobContract( + role=LogprobRole.TRAIN, + dtype=LogprobDType.FP32, + mask=MaskSpec(tokens, (True,) * tokens), + sharding=ShardingSpec(0, 1, ((0, 151936),), 151936, 151936), + reduction=ReductionSpec(), + ) + + +def test_full_vocabulary_logprob_entropy_modes_and_gradient(): + logits = random((4, 151936), dtype=torch.float32, grad=True) + targets = torch.tensor([0, 37984, 75968, 151935], device="cuda") + contract = logprob_contract(4) + logp, lse, entropy = shared_logprob(logits, targets, contract=contract) + infer_logp, infer_lse, empty = shared_logprob( + logits, targets, contract=replace(contract, role=LogprobRole.INFER), with_entropy=False + ) + exact(logp, infer_logp) + exact(lse, infer_lse) + assert empty is None + row_logp, _, row_entropy = shared_logprob(logits[:1], targets[:1], contract=logprob_contract(1)) + exact(row_logp, logp[:1]) + exact(row_entropy, entropy[:1]) + (-logp.sum() + 0.03 * entropy.sum()).backward() + reference = logits.detach().double().requires_grad_() + log_softmax = reference.log_softmax(-1) + expected_logp = log_softmax.gather(1, targets[:, None]).squeeze(1) + expected_entropy = -(log_softmax.exp() * log_softmax).sum(-1) + (-expected_logp.sum() + 0.03 * expected_entropy.sum()).backward() + torch.testing.assert_close(logp.double(), expected_logp, atol=0.00002, rtol=0) + torch.testing.assert_close(entropy.double(), expected_entropy, atol=0.00002, rtol=0) + torch.testing.assert_close(logits.grad.double(), reference.grad, atol=0.000002, rtol=0) + + +def test_logprob_nan_rejected_before_comparison(): + logits = random((1, 151936), dtype=torch.float32) + logits[0, 0] = torch.nan + with pytest.raises(ValueError, match="finite"): + shared_logprob(logits, torch.tensor([0], device="cuda"), contract=logprob_contract(1)) + + @torch.no_grad() def test_route_weights_are_batch_invariant(): logits = random((32, 512), 805, dtype=torch.float32).mul_(100) diff --git a/tests/check_qwen3_next_multigpu.py b/tests/check_qwen3_next_multigpu.py new file mode 100644 index 000000000..ede9df2f7 --- /dev/null +++ b/tests/check_qwen3_next_multigpu.py @@ -0,0 +1,46 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Required allocation-only checks. Missing GPUs fail instead of skipping.""" + +import pytest +import torch + + +@pytest.mark.parametrize("device", [1, 2, 3]) +@pytest.mark.parametrize("gated", [False, True]) +def test_norm_guard_restores_current_device_and_uses_input_stream(device, gated): + from rl_engine import _C + + assert torch.cuda.device_count() == 4, "Four allocated GPUs are required" + torch.cuda.set_device(device) + x = torch.randn(32, 128, device=f"cuda:{device}", dtype=torch.bfloat16) + weight = torch.randn(128, device=x.device, dtype=x.dtype) + gate = torch.randn_like(x) + dy = torch.randn_like(x) + + def invoke(w=weight): + if gated: + out, rstd = _C.rmsnorm_gated_forward(x, w, gate, 1e-6) + dx = _C.rmsnorm_gated_backward_dx(dy, x, w, gate, rstd) + else: + out, rstd = _C.rmsnorm_forward(x, w, 1e-6, 1.0) + dx = _C.rmsnorm_backward_dx(dy, x, w, rstd, 1.0) + return out, rstd, dx + + expected = invoke() + stream = torch.cuda.Stream(device=device) + stream.wait_stream(torch.cuda.current_stream(device)) + # set_stream changes the current device: deliberately switch back before C++ entry. + torch.cuda.set_stream(stream) + torch.cuda.set_device(0) + actual = invoke() + assert torch.cuda.current_device() == 0 + stream.synchronize() + for got, want in zip(actual, expected): + assert got.device == x.device + assert torch.isfinite(got).all() + assert torch.equal(got.contiguous().view(torch.uint8), want.contiguous().view(torch.uint8)) + with pytest.raises(RuntimeError, match="device"): + invoke(weight.to("cuda:0")) + assert torch.cuda.current_device() == 0 diff --git a/tests/check_qwen3_next_native_rollout.py b/tests/check_qwen3_next_native_rollout.py new file mode 100644 index 000000000..05ba3cdad --- /dev/null +++ b/tests/check_qwen3_next_native_rollout.py @@ -0,0 +1,318 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Native scheduler/sampler plumbing tests; these are not GPU model evidence.""" + +from types import SimpleNamespace as Namespace + +import pytest +import torch + +from rl_engine.integrations import qwen3_next_native_rollout as rollout +from rl_engine.testing.qwen3_next_l2 import L2Case, json_digest + + +def request(batch=4, length=8, chunked=False, frozen=False): + chunks = L2Case(length, batch, "chunked" if chunked else "full").chunks + prompts = [[row + 1] * length for row in range(batch)] + responses = [list(range(16)) for _ in range(batch)] if frozen else None + result = { + "schema_version": 1, + "role": "rollout", + "case_id": "cpu-plumbing", + "identity": { + "architecture_layers": 48, + "topology": {"tp": 4, "pp": 1, "cp": 1, "ep": 1}, + "checkpoint": "/explicit/test-only-checkpoint", + "provider_config": {"native_rollout_kv_cache_memory_bytes": 4 << 30}, + }, + "prompt_tokens": prompts, + "response_tokens": responses, + "sample_ids": list(range(batch)), + "prefill_chunks": chunks, + "execution": { + "rollout_mode": "autoregressive", + "prefill_chunks": chunks, + "response_mode": "teacher_forced" if frozen else "generate", + }, + "response_length": 16, + "vocab_size": 151936, + "logits_dtype": "bfloat16", + "logprobs_dtype": "float32", + "prediction_positions": list(range(length - 1, length + 15)), + "state_position": length + 14, + "consumed_response_tokens": 15, + "required_layer_digests": list(range(48)), + "trace_layers": [], + "consumed_input_tokens": ( + None + if responses is None + else [left + right[:-1] for left, right in zip(prompts, responses)] + ), + "generation": { + "temperature": 0.0, + "top_p": 1.0, + "top_k": -1, + "seed": 1234, + "ignore_eos": True, + }, + } + result["request_sha256"] = json_digest(result) + return result + + +def seal(value): + value["request_sha256"] = json_digest( + {key: item for key, item in value.items() if key != "request_sha256"} + ) + return value + + +@pytest.mark.parametrize("length", [8, 64, 256, 1024]) +@pytest.mark.parametrize("batch", [1, 4]) +@pytest.mark.parametrize("chunked", [False, True]) +def test_exact_boundaries_cover_every_prefill_and_fifteen_decode_inputs(length, batch, chunked): + value = request(batch, length, chunked, frozen=True) + assert rollout.validate_request(value) == (batch, length) + computed = 0 + for count in value["prefill_chunks"] + [1] * 15: + assert rollout.next_scheduled_tokens(computed, length, value["prefill_chunks"]) == count + computed += count + assert computed == length + 15 + with pytest.raises(ValueError, match="exact requested"): + rollout.next_scheduled_tokens(computed, length, value["prefill_chunks"]) + + +@pytest.mark.parametrize( + "field,value", + [ + ("prediction_positions", list(range(8, 24))), + ("consumed_response_tokens", 16), + ("prefill_chunks", [7]), + ("logits_dtype", "float32"), + ("trace_layers", [48]), + ], +) +def test_worker_rejects_even_resigned_requests_that_change_the_contract(field, value): + item = request() + item[field] = value + with pytest.raises(ValueError): + rollout.validate_request(seal(item)) + + +def scheduled_request(sample, computed=0, chunks=(1, 63), batch=4): + descriptor = { + "request_sha256": "same-case", + "sample_id": sample, + "batch_size": batch, + "prompt_length": sum(chunks), + "prefill_chunks": list(chunks), + } + return Namespace( + request_id=str(sample), + sampling_params=Namespace(extra_args={"shared_l2": descriptor}), + num_computed_tokens=computed, + is_finished=lambda: False, + ) + + +def scheduler(requests): + value = object.__new__(rollout.ExactPrefillScheduler) + value.requests = {row.request_id: row for row in requests} + value.scheduler_config = Namespace(long_prefill_token_threshold=0) + value._pause_state = rollout.PauseState.UNPAUSED + return value + + +def test_native_scheduler_barrier_waits_for_all_four_requests(monkeypatch): + item = scheduler([scheduled_request(0)]) + calls = [] + + def native(self, throttle): + calls.append(self._pause_state) + return Namespace(total_num_scheduled_tokens=0) + + monkeypatch.setattr(rollout.Scheduler, "schedule", native) + item.schedule() + assert calls == [rollout.PauseState.PAUSED_ALL] + assert item._pause_state == rollout.PauseState.UNPAUSED + + +def test_native_scheduler_caps_real_decisions_and_rejects_partial_batches(monkeypatch): + item = scheduler([scheduled_request(rank, computed=1) for rank in range(4)]) + + def native(self, throttle): + assert self.scheduler_config.long_prefill_token_threshold == 63 + return Namespace( + num_scheduled_tokens={str(rank): 63 for rank in range(4)}, preempted_req_ids=set() + ) + + monkeypatch.setattr(rollout.Scheduler, "schedule", native) + assert item.schedule().num_scheduled_tokens == {str(rank): 63 for rank in range(4)} + monkeypatch.setattr( + rollout.Scheduler, + "schedule", + lambda self, throttle: Namespace(num_scheduled_tokens={"0": 63}, preempted_req_ids=set()), + ) + with pytest.raises(RuntimeError, match="exact_scheduler_mismatch"): + item.schedule() + + +def test_native_scheduler_rejects_desynchronized_request_offsets(): + item = scheduler([scheduled_request(rank, computed=rank) for rank in range(4)]) + with pytest.raises(RuntimeError, match="synchronized"): + item.schedule() + + +def test_native_logits_processor_forces_frozen_tokens_without_mutating_raw_logits(): + processor = rollout.ForceFrozenTokens(None, torch.device("cpu"), False) + assert processor.is_argmax_invariant() is False + original = torch.arange(151936, dtype=torch.float32) + frozen = list(range(16)) + apply = processor.new_req_logits_processor( + Namespace(extra_args={"shared_l2": {"response_tokens": frozen}}) + ) + forced = apply(frozen[:7], original) + assert int(forced.argmax()) == 7 + assert int(original.argmax()) == 151935 + assert forced.data_ptr() != original.data_ptr() + assert ( + processor.new_req_logits_processor( + Namespace(extra_args={"shared_l2": {"response_tokens": None}}) + ) + is None + ) + + +def test_runtime_profile_reads_actual_config_and_rejects_changed_cache_budget(): + expected = rollout.NATIVE_ROLLOUT_PROFILE + config = Namespace( + scheduler_config=Namespace( + scheduler_cls=rollout.ExactPrefillScheduler, + max_num_seqs=64, + max_num_batched_tokens=16384, + async_scheduling=False, + ), + model_config=Namespace(max_model_len=1040, dtype=torch.bfloat16), + cache_config=Namespace( + kv_cache_memory_bytes=4 << 30, + enable_prefix_caching=False, + mamba_ssm_cache_dtype="float32", + ), + use_v2_model_runner=False, + ) + assert rollout.runtime_profile(config) == expected + config.cache_config.kv_cache_memory_bytes *= 2 + with pytest.raises(ValueError, match="changed the explicit"): + rollout.runtime_profile(config) + + +def test_implementation_readiness_does_not_claim_scheduler_or_model_acceptance(): + capabilities = rollout.capabilities() + assert capabilities["rollout"] and capabilities["native_scheduler"] + assert not capabilities["scheduler_batch_invariance_verified"] + assert not capabilities["full_model_l2_verified"] + + +def test_engine_arguments_are_accepted_by_the_installed_native_api(monkeypatch): + import huggingface_hub.constants + from vllm.engine.arg_utils import EngineArgs + + monkeypatch.setattr(huggingface_hub.constants, "HF_HUB_OFFLINE", False) + value = EngineArgs(**rollout.engine_arguments(request()["identity"], "ExplicitTestModel")) + assert value.tensor_parallel_size == 4 + assert value.kv_cache_memory_bytes == 4 << 30 + assert value.scheduler_cls.endswith(".ExactPrefillScheduler") + assert value.attention_config.backend.name == "CUSTOM" + assert value.compilation_config.mode == 0 + + +def trace_for_cpu_bookkeeping(): + value = object.__new__(rollout.RolloutTrace) + value.request = request(frozen=True) + value.worker = Namespace(model_runner=Namespace(input_batch=Namespace(req_ids=["r2", "r0"]))) + value.step = {"r0": (0, 7, 1), "r2": (2, 7, 1)} + value.responses = torch.tensor(value.request["response_tokens"], dtype=torch.int64) + value.progress = {"r0": 7, "r2": 7} + value.scheduled = Namespace(total_num_scheduled_tokens=2) + value.schedule = [] + return value + + +def test_trace_uses_actual_native_request_order_and_absolute_positions(): + value = trace_for_cpu_bookkeeping() + value.begin_forward(torch.tensor([3, 1]), torch.tensor([7, 7])) + assert value.current == [(2, 0, 1, 7), (0, 1, 2, 7)] + assert value.progress == {"r0": 8, "r2": 8} + assert value.schedule == [ + {"samples": [2, 0], "computed_before": [7, 7], "token_counts": [1, 1]} + ] + value.step = {"r0": (0, 8, 1), "r2": (2, 8, 1)} + value.begin_forward(torch.tensor([0, 0]), torch.tensor([8, 8])) + assert value.progress == {"r0": 9, "r2": 9} + + +@pytest.mark.parametrize("tokens,positions", [([2, 1], [7, 7]), ([3, 1], [8, 7])]) +def test_trace_rejects_wrong_consumed_tokens_or_prediction_offsets(tokens, positions): + value = trace_for_cpu_bookkeeping() + with pytest.raises(ValueError): + value.begin_forward(torch.tensor(tokens), torch.tensor(positions)) + + +@pytest.mark.parametrize("field,replacement", [("sha256", "b" * 64), ("path", "/other/_C.so")]) +def test_rollout_rejects_provider_binary_changed_during_case(monkeypatch, field, replacement): + value = object.__new__(rollout.RolloutTrace) + value.provider_binary = {"module": "rl_engine._C", "path": "/actual/_C.so", "sha256": "a" * 64} + changed = {**value.provider_binary, field: replacement} + monkeypatch.setattr(rollout, "imported_provider_identity", lambda: changed) + # This must fail before examining numerical observations or publishing a trace. + with pytest.raises(RuntimeError, match="binary changed during"): + value.finish() + + +def test_rollout_rejects_provider_replaced_between_worker_initialization_and_case(monkeypatch): + provider = {"module": "rl_engine._C", "path": "/actual/_C.so", "sha256": "a" * 64} + worker = Namespace(shared_provider_binary=provider) + monkeypatch.setattr( + rollout, "imported_provider_identity", lambda: {**provider, "sha256": "b" * 64} + ) + with pytest.raises(RuntimeError, match="binary changed after"): + rollout.RolloutTrace(worker, request(frozen=True), "/unused-cpu-test") + + +@pytest.mark.parametrize("tamper", ["missing", "module", "path", "sha256"]) +def test_assemble_rejects_different_binary_in_one_rehashed_rank_trace(tmp_path, tamper): + value = request(frozen=True) + provider = {"module": "rl_engine._C", "path": "/actual/_C.so", "sha256": "a" * 64} + records = [] + for rank in range(4): + observed = dict(provider) + if rank == 3: + if tamper == "missing": + observed = None + else: + observed[tamper] = { + "module": "fallback", + "path": "/other/_C.so", + "sha256": "b" * 64, + }[tamper] + path = tmp_path / f"rank-{rank}.pt" + rollout.save_tensor_artifact( + path, + { + "rank": rank, + "request_sha256": value["request_sha256"], + "provider_binary": observed, + }, + ) + records.append( + { + "rank": rank, + "path": str(path), + "sha256": rollout.file_digest(path), + "request_sha256": value["request_sha256"], + } + ) + # File SHA is valid: per-rank provenance must still reject the rewritten identity. + with pytest.raises(ValueError, match="binary evidence|different provider binaries"): + rollout.assemble_trace(value, records, value["response_tokens"]) diff --git a/tests/check_qwen3_next_paged_attention.py b/tests/check_qwen3_next_paged_attention.py new file mode 100644 index 000000000..89d0dd148 --- /dev/null +++ b/tests/check_qwen3_next_paged_attention.py @@ -0,0 +1,83 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Paged-KV component tests; a real vLLM scheduler gate is still required.""" + +import pytest +import torch + +from rl_engine.integrations.qwen3_next_vllm_attention import ( + SharedAttentionImpl, + SharedAttentionMetadata, +) +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal + +pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA required") + + +@pytest.mark.parametrize("batch", [1, 4]) +@pytest.mark.parametrize("length", [8, 65, 1024]) +def test_paged_attention_full_chunk_reorder_and_cache_bits(batch, length): + torch.manual_seed(777) + q = torch.randn(batch, length, 4, 256, device="cuda", dtype=torch.bfloat16) + k = torch.randn(batch, length, 1, 256, device="cuda", dtype=torch.bfloat16) + v = torch.randn_like(k) + block = 64 + pages = (length + block - 1) // block + tables = torch.arange(1, batch * pages + 1, device="cuda").reshape(batch, pages) + full_cache = torch.zeros(batch * pages + 1, 1, block, 512, device="cuda", dtype=torch.bfloat16) + chunk_cache = full_cache.clone() + impl = SharedAttentionImpl(4, 256, 1 / 16, 1) + + def invoke(cache, start, end, order): + positions = torch.arange(start, end, device="cuda") + table = tables[order] + slots = (table[:, positions // block] * block + positions % block).reshape(-1) + metadata = SharedAttentionMetadata( + tuple(index * (end - start) for index in range(batch + 1)), + (end,) * batch, + table, + slots, + batch * (end - start), + ) + query = q[order, start:end].reshape(-1, 4, 256) + out = torch.empty_like(query) + impl.forward( + None, + query, + k[order, start:end].reshape(-1, 1, 256), + v[order, start:end].reshape(-1, 1, 256), + cache, + metadata, + out, + ) + return out.reshape(batch, end - start, 4, 256) + + expected = invoke(full_cache, 0, length, list(range(batch))) + actual = torch.empty_like(expected) + start = 0 + for end in sorted( + {1, length, *(point for point in (63, 64, 65, 100, 528, 576) if point < length)} + ): + order = list(reversed(range(batch))) if start else list(range(batch)) + result = invoke(chunk_cache, start, end, order) + actual[order, start:end] = result + start = end + assert_tensor_bitwise_equal(actual, expected, name="paged full/chunk/reorder outputs") + assert_tensor_bitwise_equal(chunk_cache, full_cache, name="complete scheduler-owned KV cache") + + +@pytest.mark.parametrize("bad_slots", [[-1, 1], [1, 1], [1, 99], [1, 0]]) +def test_paged_attention_invalid_indices_do_not_mutate_cache(bad_slots): + cache = torch.zeros(2, 1, 4, 512, device="cuda", dtype=torch.bfloat16) + original = cache.clone() + query = torch.zeros(2, 4, 256, device="cuda", dtype=torch.bfloat16) + key = torch.zeros(2, 1, 256, device="cuda", dtype=torch.bfloat16) + metadata = SharedAttentionMetadata( + (0, 2), (2,), torch.tensor([[0]], device="cuda"), torch.tensor(bad_slots, device="cuda"), 2 + ) + with pytest.raises(ValueError): + SharedAttentionImpl(4, 256, 1 / 16, 1).forward( + None, query, key, key, cache, metadata, query.clone() + ) + assert_tensor_bitwise_equal(cache, original) diff --git a/tests/check_qwen3_next_vime_weight_hooks.py b/tests/check_qwen3_next_vime_weight_hooks.py new file mode 100644 index 000000000..f8851eddc --- /dev/null +++ b/tests/check_qwen3_next_vime_weight_hooks.py @@ -0,0 +1,92 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""CPU checks through VIME's real parameter shard and HF export entrypoints.""" + +from types import SimpleNamespace + +import torch +from megatron.core import mpu +from vime.backends.megatron_utils import hf_to_megatron as load_api +from vime.backends.megatron_utils import megatron_to_hf as export_api +from vime.backends.megatron_utils.hf_to_megatron.common import shard_mcore_tensor + +from rl_engine.integrations.qwen3_next_vime import SharedInputNorm, install_vime_gdn_weight_hooks +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +class Reader(dict): + def get_tensor(self, key): + return self[key] + + +def test_vime_hf_load_shard_gather_export_roundtrip(monkeypatch): + monkeypatch.setitem(load_api._LOADERS, "qwen3_next", load_api._LOADERS["qwen3_next"]) + monkeypatch.setattr(export_api, "convert_qwen3_next_to_hf", export_api.convert_qwen3_next_to_hf) + monkeypatch.setattr(mpu, "get_tensor_model_parallel_world_size", lambda: 4) + install_vime_gdn_weight_hooks() + generator = torch.Generator().manual_seed(42) + weight = torch.randn((8192, 1, 4), generator=generator).bfloat16() + name = "module.module.decoder.layers.0.self_attention.linear_attn.conv1d.weight" + reader = Reader({"model.layers.0.linear_attn.conv1d.weight": weight}) + packed = load_api._LOADERS["qwen3_next"](name, reader, SimpleNamespace()) + parameter = torch.nn.Parameter(torch.empty(2048, 1, 4, dtype=torch.bfloat16)) + parameter.tensor_model_parallel = True + parameter.partition_dim = 0 + parameter.partition_stride = 1 + local = [] + for rank in range(4): + monkeypatch.setattr(mpu, "get_tensor_model_parallel_rank", lambda rank=rank: rank) + shard = shard_mcore_tensor(name, packed, parameter) + exact(shard, torch.cat([part.chunk(4)[rank] for part in weight.split((2048, 2048, 4096))])) + local.append(shard) + args = SimpleNamespace( + hidden_size=2048, num_attention_heads=16, num_query_groups=2, kv_channels=256 + ) + converted = export_api.convert_qwen3_next_to_hf(args, name, torch.cat(local)) + assert converted[0][0] == "model.layers.0.linear_attn.conv1d.weight" + exact(converted[0][1], weight) + + +def test_shared_norm_keeps_existing_parameter_name_and_identity(): + weight = torch.nn.Parameter(torch.zeros(2048, dtype=torch.bfloat16)) + norm = SharedInputNorm(1e-6, torch.device("cpu"), weight=weight) + assert dict(norm.named_parameters()) == {"weight": weight} + assert norm.weight is weight + assert norm.weight.tensor_model_parallel is False + + +def test_vime_kv_load_shard_export_uses_two_replica_pairs(monkeypatch): + monkeypatch.setattr(mpu, "get_tensor_model_parallel_world_size", lambda: 4) + install_vime_gdn_weight_hooks() + weight = torch.randn(512, 2048).bfloat16() + name = "module.module.decoder.layers.3.self_attention.self_attn.k_proj.weight" + reader = Reader({"model.layers.3.self_attn.k_proj.weight": weight}) + packed = load_api._LOADERS["qwen3_next"](name, reader, SimpleNamespace()) + parameter = torch.nn.Parameter(torch.empty(256, 2048, dtype=torch.bfloat16)) + parameter.tensor_model_parallel = True + parameter.partition_dim = 0 + parameter.partition_stride = 1 + shards = [] + for rank in range(4): + monkeypatch.setattr(mpu, "get_tensor_model_parallel_rank", lambda rank=rank: rank) + shard = shard_mcore_tensor(name, packed, parameter) + exact(shard, weight.chunk(2)[rank // 2]) + shards.append(shard) + args = SimpleNamespace( + hidden_size=2048, num_attention_heads=16, num_query_groups=2, kv_channels=256 + ) + outputs = export_api.convert_qwen3_next_to_hf(args, name, torch.cat(shards)) + assert outputs[0][0] == "model.layers.3.self_attn.k_proj.weight" + exact(outputs[0][1], weight) + + +def test_vime_shared_expert_direct_names_roundtrip(monkeypatch): + install_vime_gdn_weight_hooks() + weight = torch.randn(512, 2048).bfloat16() + hf_name = "model.layers.0.mlp.shared_expert.gate_proj.weight" + name = "module.module.decoder.layers.0.mlp.shared_expert.gate_proj.weight" + loaded = load_api._LOADERS["qwen3_next"](name, Reader({hf_name: weight}), SimpleNamespace()) + outputs = export_api.convert_qwen3_next_to_hf(SimpleNamespace(), name, loaded) + assert outputs[0][0] == hf_name + exact(outputs[0][1], weight) diff --git a/tests/check_qwen3_next_vllm_model_contract.py b/tests/check_qwen3_next_vllm_model_contract.py new file mode 100644 index 000000000..e5ece2808 --- /dev/null +++ b/tests/check_qwen3_next_vllm_model_contract.py @@ -0,0 +1,281 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Actual vLLM imports/loaders and constructor contracts, without GPU execution.""" + +from types import SimpleNamespace as Namespace + +import pytest +import torch +from vllm.model_executor import parameter +from vllm.model_executor.layers import linear + +from rl_engine.integrations import qwen3_next_vllm_model as adapter +from rl_engine.integrations.qwen3_next_tp_blocks import shard_attention_weights +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact + + +def profile(): + config = Namespace( + hidden_size=2048, + num_hidden_layers=48, + num_attention_heads=16, + num_key_value_heads=2, + head_dim=256, + vocab_size=151936, + num_experts=512, + num_experts_per_tok=10, + moe_intermediate_size=512, + shared_expert_intermediate_size=512, + rms_norm_eps=1e-6, + max_position_embeddings=262144, + layer_types=[ + "full_attention" if index % 4 == 3 else "linear_attention" for index in range(48) + ], + rope_parameters={ + "rope_theta": 10000000, + "partial_rotary_factor": 0.25, + "rope_type": "default", + }, + ) + return Namespace( + model_config=Namespace(dtype=torch.bfloat16, enforce_eager=True, hf_text_config=config), + parallel_config=Namespace( + tensor_parallel_size=4, + pipeline_parallel_size=1, + enable_expert_parallel=False, + enable_eplb=False, + use_sequence_parallel_moe=False, + enable_dbo=False, + ), + quant_config=None, + speculative_config=None, + lora_config=None, + cache_config=Namespace(enable_prefix_caching=False), + kernel_config=Namespace(moe_backend="triton"), + attention_config=Namespace(backend=adapter.AttentionBackendEnum.CUSTOM), + ) + + +def test_profile_keeps_engine_and_model_acceptance_closed(): + adapter.validate_profile(profile()) + assert adapter.profile_capabilities()["scheduler_batch_invariance_verified"] is False + assert adapter.profile_capabilities()["full_model_l2_verified"] is False + + +@pytest.mark.parametrize( + "scope,name,value", + [ + ("model_config", "dtype", torch.float32), + ("model_config", "enforce_eager", False), + ("parallel_config", "tensor_parallel_size", 2), + ("parallel_config", "enable_expert_parallel", True), + ("cache_config", "enable_prefix_caching", True), + ("kernel_config", "moe_backend", "auto"), + ("attention_config", "backend", None), + ], +) +def test_real_wrapper_constructor_rejects_drift_before_allocating_model(scope, name, value): + config = profile() + setattr(getattr(config, scope), name, value) + with pytest.raises(ValueError): + adapter.RLKernelQwen3NextForCausalLM(vllm_config=config) + + +@pytest.mark.parametrize("rank", range(4)) +def test_actual_native_qkv_loader_preserves_gate_layout_and_kv_replication(monkeypatch, rank): + for module in (linear, parameter): + monkeypatch.setattr(module, "get_tensor_model_parallel_rank", lambda: rank) + monkeypatch.setattr(module, "get_tensor_model_parallel_world_size", lambda: 4) + # Construct the real native layer, then use its unchanged HF shard loader. + layer = linear.QKVParallelLinear( + 2048, + 256, + 32, + 2, + bias=False, + params_dtype=torch.bfloat16, + ) + weights = {} + for name, rows in (("q_proj.weight", 8192), ("k_proj.weight", 512), ("v_proj.weight", 512)): + weights[name] = ( + torch.arange(rows, dtype=torch.int32) + .remainder(251) + .to(torch.bfloat16)[:, None] + .expand(rows, 2048) + .contiguous() + ) + weights.update( + { + "o_proj.weight": torch.zeros(2048, 4096, dtype=torch.bfloat16), + "q_norm.weight": torch.zeros(256, dtype=torch.bfloat16), + "k_norm.weight": torch.zeros(256, dtype=torch.bfloat16), + } + ) + expected = shard_attention_weights(weights, rank) + for key, shard in (("q_proj.weight", "q"), ("k_proj.weight", "k"), ("v_proj.weight", "v")): + layer.weight.weight_loader(layer.weight, weights[key], shard) + for actual, key in zip( + layer.weight.split((2048, 256, 256)), ("q_proj.weight", "k_proj.weight", "v_proj.weight") + ): + exact(actual, expected[key], name=f"native rank{rank} {key}") + + +def test_shared_moe_postload_preserves_parameter_identity_and_loader(monkeypatch): + # Meta tensors verify 512-expert allocation shapes without allocating an + # unnecessary CPU model. Real data movement is covered by the TP4 GPU gate. + gate = torch.nn.Parameter(torch.empty(512, 256, 2048, device="meta", dtype=torch.bfloat16)) + down = torch.nn.Parameter(torch.empty(512, 2048, 128, device="meta", dtype=torch.bfloat16)) + loader = object() + gate.weight_loader = down.weight_loader = loader + layer = Namespace(w13_weight=gate, w2_weight=down) + method = object.__new__(adapter.SharedPlainBF16MoEMethod) + monkeypatch.setattr( + adapter.SharedPlainBF16MoEMethod, + "get_fused_moe_quant_config", + lambda self, layer: "plain-bf16", + ) + method.process_weights_after_loading(layer) + assert layer.w13_weight is gate and layer.w2_weight is down + assert gate.weight_loader is loader and down.weight_loader is loader + assert method.moe_quant_config == "plain-bf16" + with pytest.raises(RuntimeError, match="native MoE arithmetic"): + method.apply() + + +def test_shared_model_rejects_rope_or_layer_layout_changes(): + config = profile() + config.model_config.hf_text_config.rope_parameters["rope_theta"] = 10000 + with pytest.raises(ValueError, match="rotary dimension"): + adapter.validate_profile(config) + config = profile() + config.model_config.hf_text_config.layer_types[0] = "full_attention" + with pytest.raises(ValueError, match="36 GDN"): + adapter.validate_profile(config) + + +@pytest.mark.parametrize( + "decoder_class", [adapter.Qwen3NextDecoderLayer, adapter.SharedVLLMDecoderLayer] +) +def test_real_decoder_invokes_gdn_with_single_input_and_consumes_returned_tensor( + monkeypatch, decoder_class +): + from rl_engine.integrations.qwen3_next_vllm import SharedQwenGDN + + class ResidualNorm(torch.nn.Module): + def forward(self, hidden, residual=None): + if residual is None: + return hidden + return hidden + residual, hidden + residual + + gdn = object.__new__(SharedQwenGDN) + torch.nn.Module.__init__(gdn) + calls = [] + + def into(self, hidden, output): + assert output is not hidden and output.data_ptr() != hidden.data_ptr() + assert output.dtype == hidden.dtype and output.shape == hidden.shape + output.copy_(hidden + 2) + calls.append(output) + + monkeypatch.setattr(SharedQwenGDN, "_forward_into", into) + monkeypatch.setattr(adapter, "_rms", lambda value, module: value) + decoder = object.__new__(decoder_class) + torch.nn.Module.__init__(decoder) + decoder.layer_type = "linear_attention" + decoder.layer_scale = decoder.use_attn_reduce_scatter_for_moe = False + decoder.linear_attn = gdn + decoder.input_layernorm = decoder.post_attention_layernorm = ResidualNorm() + decoder.mlp = torch.nn.Identity() + hidden = torch.ones((2, 2048), dtype=torch.bfloat16) + output, residual = decoder(hidden_states=hidden, residual=None, positions=torch.arange(2)) + assert len(calls) == 1 + exact(calls[0], torch.full_like(hidden, 3), name="public GDN return") + exact(output, torch.full_like(hidden, 4), name="decoder consumed GDN output") + exact(residual, torch.full_like(hidden, 4), name="decoder residual") + + +def test_binding_all48_layers_preserves_parameter_identity_and_native_loaders(): + from rl_engine.integrations.qwen3_next_vllm import SharedQwenGDN + from rl_engine.integrations.qwen3_next_vllm_attention import SharedAttentionBackend + + def instance(cls): + value = object.__new__(cls) + torch.nn.Module.__init__(value) + return value + + loader = object() + + def weight_module(shape): + module = torch.nn.Module() + module.weight = torch.nn.Parameter(torch.empty(shape, device="meta", dtype=torch.bfloat16)) + module.weight.weight_loader = loader + return module + + # The binding operation does not run a native constructor or GPU kernel. + # Meta tensors make its 48-layer ownership contract affordable to test. + model = instance(adapter.Qwen3NextModel) + model.start_layer, model.end_layer = 0, 48 + model.embed_tokens = weight_module((37984, 2048)) + model.norm = weight_module((2048,)) + layers = [] + for index in range(48): + layer = instance(adapter.Qwen3NextDecoderLayer) + layer.layer_type = "full_attention" if index % 4 == 3 else "linear_attention" + if layer.layer_type == "linear_attention": + layer.linear_attn = instance(SharedQwenGDN) + else: + layer.self_attn = instance(adapter.Qwen3NextAttention) + layer.self_attn.attn = Namespace(attn_backend=SharedAttentionBackend) + layer.self_attn.qkv_proj = weight_module((2560, 2048)) + layer.self_attn.o_proj = weight_module((2048, 1024)) + layer.mlp = instance(adapter.Qwen3NextSparseMoeBlock) + layer.mlp.is_fused_shared_expert_enabled = False + layer.mlp.replicate_shared_expert = False + layer.mlp.shared_expert = torch.nn.Module() + layer.mlp.shared_expert.gate_up_proj = weight_module((256, 2048)) + layer.mlp.shared_expert.down_proj = weight_module((2048, 128)) + routed = torch.nn.Module() + routed.quant_method = instance(adapter.UnquantizedFusedMoEMethod) + routed.w13_weight = weight_module((512, 256, 2048)).weight + routed.w2_weight = weight_module((512, 2048, 128)).weight + layer.mlp.experts = torch.nn.Module() + layer.mlp.experts.routed_experts = routed + layers.append(layer) + model.layers = torch.nn.ModuleList(layers) + before = {name: id(value) for name, value in model.named_parameters()} + adapter._bind_native_model(model, object()) + assert isinstance(model, adapter.SharedVLLMModel) + assert {name: id(value) for name, value in model.named_parameters()} == before + assert all(value.weight_loader is loader for value in model.parameters()) + assert all(isinstance(layer, adapter.SharedVLLMDecoderLayer) for layer in model.layers) + assert all(isinstance(layer.mlp, adapter.SharedVLLMMoE) for layer in model.layers) + + +def test_spawn_worker_registers_model_before_native_worker_initialization(monkeypatch): + from rl_engine.integrations import qwen3_next_vllm_worker as worker + + calls = [] + monkeypatch.setattr(worker, "register_shared_model", lambda: calls.append("registered")) + monkeypatch.setattr( + worker, + "imported_provider_identity", + lambda: calls.append("native-binary") or {"test": True}, + ) + monkeypatch.setattr( + worker.Worker, "__init__", lambda self, *args, **kwargs: calls.append("native-init") + ) + worker.SharedQwenWorker() + assert calls == ["registered", "native-binary", "native-init"] + + +def test_architecture_registration_is_idempotent_and_rejects_another_model(monkeypatch): + from vllm.model_executor.models import ModelRegistry + + monkeypatch.setattr(ModelRegistry, "models", dict(ModelRegistry.models)) + ModelRegistry.models.pop(adapter.ARCHITECTURE, None) + assert adapter.register_shared_model() == adapter.ARCHITECTURE + assert adapter.register_shared_model() == adapter.ARCHITECTURE + ModelRegistry.models[adapter.ARCHITECTURE] = Namespace(load_model_cls=lambda: torch.nn.Linear) + with pytest.raises(ValueError, match="different model"): + adapter.register_shared_model() diff --git a/tests/test_qwen3_next_binary_evidence.py b/tests/test_qwen3_next_binary_evidence.py new file mode 100644 index 000000000..a86976fe3 --- /dev/null +++ b/tests/test_qwen3_next_binary_evidence.py @@ -0,0 +1,68 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Loaded provider identity must match immutable build evidence, including on reload.""" + +import json +import sys +from types import SimpleNamespace + +import pytest + +from rl_engine.testing.qwen3_next_binary import ( + archive_provider_binary, + validate_binary_record, + verify_imported_provider, +) + + +@pytest.fixture +def archived(tmp_path): + source, output = tmp_path / "source", tmp_path / "attempt" + (source / "rl_engine").mkdir(parents=True) + output.mkdir() + binary = source / "rl_engine/_C.cpython-test.so" + binary.write_bytes(b"built CUDA extension") + return binary, archive_provider_binary(source, output, source_tree="original-tree") + + +def test_archive_survives_later_live_binary_rebuild(archived): + binary, record = archived + actual = validate_binary_record(record, source_tree="original-tree") + binary.write_bytes(b"later build") + assert validate_binary_record(record) == actual + with pytest.raises(ValueError, match="source tree"): + validate_binary_record(record, source_tree="different-tree") + + +def test_imported_binary_cannot_substitute_another_provider(archived, monkeypatch): + _, record = archived + expected = validate_binary_record(record) + loaded = dict(expected) + monkeypatch.setitem( + sys.modules, + "rl_engine.integrations.qwen3_next_binary", + SimpleNamespace(imported_provider_identity=lambda: loaded), + ) + assert verify_imported_provider(record, source_tree="original-tree") == expected + loaded["sha256"] = "0" * 64 + with pytest.raises(ValueError, match="different CUDA binary"): + verify_imported_provider(record, source_tree="original-tree") + + +def test_archived_binary_corruption_and_attempt_escape_are_rejected(archived, tmp_path): + from pathlib import Path + + _, record = archived + with pytest.raises(ValueError, match="escaped"): + validate_binary_record(record, artifact_root=tmp_path / "different-attempt") + metadata = json.loads(Path(record["path"]).read_text()) + Path(metadata["archive"]["path"]).write_bytes(b"corrupted original binary") + with pytest.raises(ValueError, match="content changed"): + validate_binary_record(record) + + +@pytest.mark.parametrize("record", [None, {}, {"path": "/tmp/unpinned"}]) +def test_missing_binary_record_cannot_close_native_acceptance(record): + with pytest.raises(ValueError, match="pinned CUDA binary"): + validate_binary_record(record) diff --git a/tests/test_qwen3_next_case_manifest.py b/tests/test_qwen3_next_case_manifest.py new file mode 100644 index 000000000..9834c76a7 --- /dev/null +++ b/tests/test_qwen3_next_case_manifest.py @@ -0,0 +1,106 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Mandatory case completeness must not be inferred from an aggregate count.""" + +import json +import subprocess +import sys +from pathlib import Path + +import pytest + +from rl_engine.testing.qwen3_next_cases import case_digest, execution_verdict, load_cases + +CASE = "tests/example.py::test_required" +SUCCESS = {"setup": "passed", "call": "passed", "teardown": "passed"} + + +@pytest.mark.parametrize( + "fault", ["missing", "extra", "duplicate", "skipped", "teardown", "incomplete"] +) +def test_declared_case_must_complete_all_phases(fault): + collected = [CASE] + outcomes = {CASE: dict(SUCCESS)} + if fault == "missing": + collected = [] + elif fault == "extra": + collected.append("tests/example.py::test_undeclared") + elif fault == "duplicate": + collected.append(CASE) + elif fault == "skipped": + outcomes[CASE]["call"] = "skipped" + elif fault == "teardown": + outcomes[CASE]["teardown"] = "failed" + else: + del outcomes[CASE]["call"] + assert execution_verdict([CASE], collected, outcomes)["passed"] is False + + +def test_complete_report_records_exact_identity(): + result = execution_verdict([CASE], [CASE], {CASE: SUCCESS}) + assert result["passed"] is True + assert result["counts"] == {"tests": 1, "failures": 0, "errors": 0, "skipped": 0} + assert result["expected_case_ids_sha256"] == case_digest([CASE]) + + +def test_diagnostics_cannot_enter_required_case_set(tmp_path): + path = tmp_path / "cases.json" + path.write_text( + json.dumps( + { + "schema_version": 1, + "suites": { + "norm": [CASE], + "core": ["tests/example.py::test_core"], + "diagnostics": ["tests/example.py::test_approximate"], + }, + } + ) + ) + assert load_cases(path, "all") == sorted([CASE, "tests/example.py::test_core"]) + + +@pytest.mark.parametrize("outcome", ["pass", "skip", "xfail", "deselected"]) +def test_plugin_enforces_real_pytest_outcomes(tmp_path, outcome): + test_file = tmp_path / "test_acceptance.py" + body = "pass" + if outcome == "skip": + body = "pytest.skip('missing CUDA')" + elif outcome == "xfail": + body = "pytest.xfail('unimplemented')" + test_file.write_text(f"import pytest\ndef test_required():\n {body}\n") + case = "test_acceptance.py::test_required" + manifest = tmp_path / "cases.json" + manifest.write_text(json.dumps({"schema_version": 1, "suites": {"core": [case]}})) + report_path = tmp_path / "report.json" + args = [ + sys.executable, + "-m", + "pytest", + "-q", + "-p", + "no:cacheprovider", + "-p", + "rl_engine.testing.pytest_case_manifest", + "--case-manifest", + str(manifest), + "--case-suite", + "core", + "--case-report", + str(report_path), + str(test_file), + ] + if outcome == "deselected": + args.extend(["-k", "not required"]) + # Use this source tree even when a different RL-Kernel checkout is installed. + import os + + env = dict(os.environ, PYTHONPATH=str(Path(__file__).resolve().parents[1])) + result = subprocess.run(args, cwd=tmp_path, env=env, capture_output=True, text=True) + assert result.returncode == ( + 0 if outcome == "pass" else (5 if outcome == "deselected" else 1) + ), (result.stdout + result.stderr) + report = json.loads(report_path.read_text()) + assert report["passed"] is (outcome == "pass") + assert report["expected_case_ids"] == [case] diff --git a/tests/test_qwen3_next_chain_report.py b/tests/test_qwen3_next_chain_report.py new file mode 100644 index 000000000..71b0eacde --- /dev/null +++ b/tests/test_qwen3_next_chain_report.py @@ -0,0 +1,149 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""The Qwen3-Next chain report keeps the WS1 chain-gate shape and attributes first drift.""" + +import dataclasses + +import pytest +import torch + +from rl_engine.kernels.gtest import qwen3_next_chain_report as chain +from rl_engine.kernels.gtest.chain_gate import ChainGateReport +from rl_engine.kernels.gtest.tolerance import load_contract + +CASES = ("p0008-b1-full", "p0008-b1-chunked") + + +def artifact(seed): + g = torch.Generator().manual_seed(seed) + return { + "logprobs": -torch.rand((1, 16), generator=g), + "logits": torch.randn((1, 16, 64), generator=g).to(torch.bfloat16), + "embedding_digest": {"sha256": "a" * 64, "sample_sha256": ["a" * 64]}, + "layer_digests": [{"layer": layer, "output_sha256": "b" * 64} for layer in range(48)], + } + + +def record(passed=True, node=None, layer=None, tensor=None): + return { + "passed": passed, + "first_divergent_node": None if node is None else {"layer": layer, "node": node}, + "first_difference": None if tensor is None else {"tensor": tensor}, + "artifacts": {}, + } + + +def inputs(records=None, recompute_seed=1): + matrices, artifacts = {}, {} + for version in ("baseline", "update1"): + matrices[version] = { + "status": "passed", + "case_ids": list(CASES), + "cases": {case: record() for case in CASES}, + "fixture": {"content_sha256": "f" * 64}, + } + artifacts[version] = { + case: {"rollout": artifact(1), "recompute": artifact(recompute_seed)} for case in CASES + } + for (version, case), value in (records or {}).items(): + matrices[version]["cases"][case] = value + matrices[version]["status"] = "failed" + return matrices, artifacts + + +def build(matrices, artifacts): + return chain.build_report( + matrices, + artifacts, + contract=load_contract(), + measured_commit="c" * 40, + weight_source="Qwen/Qwen3-Next-80B-A3B-Instruct@rev", + weight_hash="h" * 64, + device="cuda", + gpu_name="NVIDIA B200", + compute_capability="10.0", + ) + + +def test_report_is_a_chain_gate_report_superset_and_passes_when_bitwise(): + report = build(*inputs()) + fields = {field.name for field in dataclasses.fields(ChainGateReport)} + assert fields <= set(report) + assert report["schema_version"] == "qwen3-next-chain-v1" + assert report["compatible_schema"] == "ws1-c10-c11-v5" + assert report["passed"] and report["first_drift"] is None + assert report["first_drift_family"] is None + assert len(report["cells"]) == 4 and len(report["invariance"]) == 8 + assert all(item["atol"] == 0 and item["passed"] for item in report["invariance"]) + assert report["weight_versions"] == ["baseline", "update1"] + assert [entry["case_id"] for entry in report["decode_prefill"]] == [ + "baseline/p0008-b1-chunked", + "update1/p0008-b1-chunked", + ] + + +def test_a_single_bit_in_the_replay_fails_the_report(): + matrices, artifacts = inputs() + replay = artifacts["update1"]["p0008-b1-full"]["recompute"] + replay["logprobs"] = replay["logprobs"].clone() + replay["logprobs"][0, 3] = torch.nextafter(replay["logprobs"][0, 3], torch.tensor(0.0)) + report = build(matrices, artifacts) + assert not report["passed"] + failing = [item for item in report["invariance"] if not item["passed"]] + assert [item["config_pair"][0] for item in failing] == ["update1/p0008-b1-full/rollout"] + + +@pytest.mark.parametrize( + "node,layer,family,label", + [ + ("embedding", 0, "embedding", "embedding"), + ("input_layernorm", 0, "norm", "layer0.input_layernorm"), + ("post_attention_layernorm", 5, "norm", "layer5.post_attention_layernorm"), + ("mixer", 2, "gdn", "layer2.mixer"), + ("state", 7, "full_attention", "layer7.state"), + ("mixer", 47, "full_attention", "layer47.mixer"), + ("router", 9, "moe", "layer9.router"), + ("mlp", 10, "moe", "layer10.mlp"), + ], +) +def test_fabricated_drift_resolves_to_its_layer_family(node, layer, family, label): + failing = record(passed=False, node=node, layer=layer) + report = build(*inputs({("update1", "p0008-b1-chunked"): failing})) + assert not report["passed"] + assert report["first_drift"] == f"{label}:rollout->recompute" + assert report["first_drift_family"] == family + assert report["first_drift_layer"] == layer + assert report["first_drift_cell"] == "update1/p0008-b1-chunked" + + +def test_drift_after_the_last_layer_is_the_lm_head_or_logprob(): + failing = record(passed=False, tensor="logprobs") + report = build(*inputs({("baseline", "p0008-b1-full"): failing})) + assert report["first_drift"] == "logprobs:rollout->recompute" + assert report["first_drift_family"] == "lm_head_logprob" + assert report["first_drift_layer"] is None + + +def test_the_earliest_weight_version_and_case_names_the_first_drift(): + report = build( + *inputs( + { + ("update1", "p0008-b1-full"): record(False, "router", 3), + ("baseline", "p0008-b1-chunked"): record(False, "mixer", 11), + } + ) + ) + assert report["first_drift_cell"] == "baseline/p0008-b1-chunked" + assert report["first_drift_family"] == "full_attention" + + +def test_unknown_nodes_and_versions_are_rejected(): + with pytest.raises(ValueError, match="Unknown divergence node"): + chain.family_of(3, "gate") + with pytest.raises(ValueError, match="needs a decoder layer"): + chain.family_of(None, "mixer") + matrices, artifacts = inputs() + matrices["update3"] = matrices["update1"] + with pytest.raises(ValueError, match="subset"): + build(matrices, artifacts) diff --git a/tests/test_qwen3_next_checkpoint.py b/tests/test_qwen3_next_checkpoint.py new file mode 100644 index 000000000..54113ea66 --- /dev/null +++ b/tests/test_qwen3_next_checkpoint.py @@ -0,0 +1,222 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Crash boundaries for four-rank atomic checkpoint publication.""" + +import json +import sys +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) +from qwen3_next_checkpoint import ( # noqa: E402 + newest_complete_checkpoint, + publish_checkpoint, + resume_interrupted_publications, + validate_checkpoint, + write_rank_manifest, +) + +IDENTITY = {"provider": "explicit", "tp": 4, "environment": "pinned", "weight_revision": "official"} + + +def staged(root, step=1): + directory = root / f"step-{step}.partial" + directory.mkdir() + for rank in range(4): + rank_dir = directory / f"rank-{rank}" + rank_dir.mkdir() + files = {} + for role in ("model", "optimizer", "rng", "progress"): + path = rank_dir / f"{role}.json" + path.write_text(json.dumps({"rank": rank, "role": role, "step": step})) + files[role] = [str(path.relative_to(directory))] + write_rank_manifest( + directory, + rank=rank, + world_size=4, + identity=IDENTITY, + step=step, + sample_position=step * 4, + files=files, + ) + return directory + + +def test_complete_publication_and_recovery(tmp_path): + partial = staged(tmp_path) + destination = tmp_path / "step-1" + publish_checkpoint(partial, destination, world_size=4, identity=IDENTITY) + assert not partial.exists() + assert validate_checkpoint(destination, IDENTITY)["step"] == 1 + assert json.loads((tmp_path / "LATEST.json").read_text())["path"] == "step-1" + recovery = newest_complete_checkpoint(tmp_path, IDENTITY) + assert recovery["checkpoint"] == str(destination) + assert recovery["sample_position"] == 4 + + +def test_missing_rank_prevents_completion_marker(tmp_path): + partial = staged(tmp_path) + (partial / "rank-3.manifest.json").unlink() + with pytest.raises(FileNotFoundError): + publish_checkpoint(partial, tmp_path / "step-1", world_size=4, identity=IDENTITY) + assert not (partial / "COMPLETE.json").exists() + assert not (tmp_path / "step-1").exists() + + +def test_corruption_prevents_publication(tmp_path): + partial = staged(tmp_path) + (partial / "rank-2/optimizer.json").write_text("damaged") + with pytest.raises(ValueError, match="checksum|size mismatch"): + publish_checkpoint(partial, tmp_path / "step-1", world_size=4, identity=IDENTITY) + assert not (partial / "COMPLETE.json").exists() + + +def test_only_completed_checkpoints_can_load(tmp_path): + partial = staged(tmp_path) + with pytest.raises(ValueError, match="Unpublished"): + validate_checkpoint(partial, IDENTITY) + assert newest_complete_checkpoint(tmp_path, IDENTITY)["checkpoint"] is None + + +def test_corrupt_latest_falls_back_and_records_reason(tmp_path): + for step in (1, 2): + partial = staged(tmp_path, step) + publish_checkpoint(partial, tmp_path / f"step-{step}", world_size=4, identity=IDENTITY) + (tmp_path / "step-2/rank-0/rng.json").write_text("corrupt") + staged(tmp_path, 3) + recovery = newest_complete_checkpoint(tmp_path, IDENTITY) + assert recovery["step"] == 1 + assert recovery["rejected"][0]["path"] == "step-2" + assert recovery["interrupted"] == ["step-3.partial"] + + +def test_identity_mismatch_never_resumes_fresh_silently(tmp_path): + partial = staged(tmp_path) + publish_checkpoint(partial, tmp_path / "step-1", world_size=4, identity=IDENTITY) + with pytest.raises(ValueError, match="No valid"): + newest_complete_checkpoint(tmp_path, {**IDENTITY, "tp": 2}) + + +def test_rank_progress_mismatch_prevents_publication(tmp_path): + partial = staged(tmp_path) + path = partial / "rank-3.manifest.json" + payload = json.loads(path.read_text()) + payload["step"] = 2 + path.write_text(json.dumps(payload)) + with pytest.raises(ValueError, match="different progress"): + publish_checkpoint(partial, tmp_path / "step-1", world_size=4, identity=IDENTITY) + + +def test_rank_cannot_omit_optimizer_or_reuse_rng_as_model(tmp_path): + partial = tmp_path / "new.partial" + partial.mkdir() + (partial / "state").write_text("state") + with pytest.raises(ValueError, match="Every rank"): + write_rank_manifest( + partial, + rank=0, + world_size=4, + identity=IDENTITY, + step=1, + sample_position=4, + files={"model": ["state"]}, + ) + with pytest.raises(ValueError, match="multiple state roles"): + write_rank_manifest( + partial, + rank=0, + world_size=4, + identity=IDENTITY, + step=1, + sample_position=4, + files={role: ["state"] for role in ("model", "optimizer", "rng", "progress")}, + ) + + +def test_crash_after_marker_before_rename_stays_unloadable(tmp_path, monkeypatch): + partial = staged(tmp_path) + + def crash(*args, **kwargs): + raise OSError("simulated interrupted rename") + + monkeypatch.setattr(Path, "rename", crash) + with pytest.raises(OSError): + publish_checkpoint(partial, tmp_path / "step-1", world_size=4, identity=IDENTITY) + assert (partial / "COMPLETE.json").exists() + assert newest_complete_checkpoint(tmp_path, IDENTITY)["checkpoint"] is None + + +def test_published_directory_cannot_be_overwritten(tmp_path): + partial = staged(tmp_path) + destination = tmp_path / "step-1" + destination.mkdir() + with pytest.raises(ValueError, match="new sibling"): + publish_checkpoint(partial, destination, world_size=4, identity=IDENTITY) + + +def test_disk_budget_counts_last_next_exports_and_records_failure(tmp_path, monkeypatch): + from collections import namedtuple + + import qwen3_next_checkpoint as checkpoint + + usage = namedtuple("Usage", "total used free") + monkeypatch.setattr(checkpoint.shutil, "disk_usage", lambda path: usage(1000, 750, 250)) + with pytest.raises(RuntimeError, match="insufficient_disk"): + checkpoint.require_checkpoint_disk_space( + tmp_path, checkpoint_bytes=100, export_bytes=40, headroom_bytes=20 + ) + record = json.loads((tmp_path / "disk-budget.json").read_text()) + assert record["required_free_bytes"] == 260 and record["status"] == "insufficient_disk" + record = checkpoint.require_checkpoint_disk_space( + tmp_path, + checkpoint_bytes=100, + export_bytes=40, + retained_complete_bytes=100, + headroom_bytes=20, + ) + assert record["required_free_bytes"] == 160 and record["status"] == "passed" + + +def test_discovery_without_hashing_checks_sizes_and_defers_content_to_the_loader(tmp_path): + partial = staged(tmp_path) + publish_checkpoint(partial, tmp_path / "step-1", world_size=4, identity=IDENTITY) + target = tmp_path / "step-1/rank-0/rng.json" + original = target.read_text() + target.write_text("X" * len(original)) # same size, different bytes + fast = newest_complete_checkpoint(tmp_path, IDENTITY, verify_files=False) + assert fast["checkpoint"] == str(tmp_path / "step-1") and fast["rejected"] == [] + with pytest.raises(ValueError, match="checksum mismatch"): + newest_complete_checkpoint(tmp_path, IDENTITY) # hashed discovery still rejects it + target.write_text(original + "!") # size change is still caught without hashing + with pytest.raises(ValueError, match="size mismatch"): + newest_complete_checkpoint(tmp_path, IDENTITY, verify_files=False) + + +def test_interrupted_but_complete_partial_is_published_on_resume(tmp_path): + partial = staged(tmp_path, 1) # all four ranks and manifests written, never published + (partial / "STAGING.json").write_text(json.dumps({"identity": IDENTITY, "status": "staging"})) + records = resume_interrupted_publications(tmp_path, IDENTITY) + assert records == [ + {"reason": "interrupted_publication_resumed", "checkpoint": "step-1", "step": 1} + ] + assert not partial.exists() + assert newest_complete_checkpoint(tmp_path, IDENTITY)["checkpoint"] == str(tmp_path / "step-1") + + +def test_incomplete_or_corrupt_partial_is_not_resumed(tmp_path): + missing = staged(tmp_path, 1) + (missing / "rank-3.manifest.json").unlink() + corrupt = staged(tmp_path, 2) + (corrupt / "rank-0/rng.json").write_text("corrupt") + for partial in (missing, corrupt): + (partial / "STAGING.json").write_text( + json.dumps({"identity": IDENTITY, "status": "staging"}) + ) + records = resume_interrupted_publications(tmp_path, IDENTITY) + assert [r["reason"] for r in records] == ["interrupted_publication_not_resumable"] + assert records[0]["staging"] == "step-2.partial" + assert missing.exists() and corrupt.exists() + assert not (corrupt / "COMPLETE.json").exists() + assert newest_complete_checkpoint(tmp_path, IDENTITY)["checkpoint"] is None diff --git a/tests/test_qwen3_next_engine_weights.py b/tests/test_qwen3_next_engine_weights.py new file mode 100644 index 000000000..862d8a15e --- /dev/null +++ b/tests/test_qwen3_next_engine_weights.py @@ -0,0 +1,81 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Segmented convolution must survive the engines' actual rank-major TP layout.""" + +from types import SimpleNamespace + +import pytest +import torch + +from rl_engine.integrations.qwen3_next_engine_weights import ( + conv_hf_to_rank_major, + conv_rank_major_to_hf, + expert_hf_to_rank_major, + expert_rank_major_to_hf, + kv_hf_to_rank_major, + kv_rank_major_to_hf, + validate_training_topology, +) +from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal + + +def test_conv_rank_major_shards_preserve_q_k_v_segments(): + generator = torch.Generator().manual_seed(27) + weight = torch.randn((8192, 1, 4), generator=generator).bfloat16() + rank_major = conv_hf_to_rank_major(weight) + segments = weight.split((2048, 2048, 4096)) + for rank, shard in enumerate(rank_major.chunk(4)): + assert_tensor_bitwise_equal(shard, torch.cat([part.chunk(4)[rank] for part in segments])) + assert_tensor_bitwise_equal(conv_rank_major_to_hf(rank_major), weight) + + +@pytest.mark.parametrize( + "field", + [ + "tensor_model_parallel_size", + "context_parallel_size", + "pipeline_model_parallel_size", + "expert_model_parallel_size", + "sequence_parallel", + "fp32_residual_connection", + "hidden_dropout", + "mtp_num_layers", + ], +) +def test_training_adapter_rejects_unsupported_topology(field): + values = dict( + tensor_model_parallel_size=4, + context_parallel_size=1, + pipeline_model_parallel_size=1, + expert_model_parallel_size=1, + sequence_parallel=False, + fp32_residual_connection=False, + hidden_dropout=0, + mtp_num_layers=0, + ) + values[field] = 2 if field.endswith("size") else 1 + with pytest.raises(ValueError): + validate_training_topology( + SimpleNamespace(**values), SimpleNamespace(sequence_parallel=False) + ) + + +def test_kv_export_rejects_diverged_replica(): + weight = torch.randn(512, 2048).bfloat16() + packed = kv_hf_to_rank_major(weight) + assert_tensor_bitwise_equal(kv_rank_major_to_hf(packed), weight) + packed[256, 0] = packed[256, 0] + 1 + with pytest.raises(AssertionError, match="KV replica pair 0"): + kv_rank_major_to_hf(packed) + + +def test_expert_gather_restores_separate_hf_gate_and_up(): + gate, up = (torch.randn(512, 2048).bfloat16() for _ in range(2)) + packed = expert_hf_to_rank_major(gate, up) + for rank, shard in enumerate(packed.chunk(4)): + assert_tensor_bitwise_equal(shard[:128], gate.chunk(4)[rank]) + assert_tensor_bitwise_equal(shard[128:], up.chunk(4)[rank]) + restored = expert_rank_major_to_hf(packed) + assert_tensor_bitwise_equal(restored[0], gate) + assert_tensor_bitwise_equal(restored[1], up) diff --git a/tests/test_qwen3_next_environment_identity.py b/tests/test_qwen3_next_environment_identity.py new file mode 100644 index 000000000..35e4606ef --- /dev/null +++ b/tests/test_qwen3_next_environment_identity.py @@ -0,0 +1,41 @@ +# SPDX-License-Identifier: Apache-2.0 +"""The environment identity must not depend on what the process has imported.""" + +import sys +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) + +from qwen3_next_runtime import current_identity, installation_roots # noqa: E402 + + +def _fake_distribution(root: Path, name: str, version: str) -> None: + info = root / f"{name}-{version}.dist-info" + info.mkdir(parents=True) + (info / "METADATA").write_text(f"Metadata-Version: 2.1\nName: {name}\nVersion: {version}\n") + + +def test_identity_ignores_sys_path_entries_added_after_startup(tmp_path, monkeypatch): + before = current_identity() + extra = tmp_path / "late-site-packages" + _fake_distribution(extra, "late_arrival", "1.0") + monkeypatch.syspath_prepend(str(extra)) + after = current_identity() + assert after["sha256"] == before["sha256"] + assert all(item["name"] != "late_arrival" for item in after["identity"]["distributions"]) + + +def test_identity_survives_importing_setuptools(): + before = current_identity() + import setuptools # noqa: F401 # appends setuptools/_vendor to sys.path + + after = current_identity() + assert after["sha256"] == before["sha256"] + assert all(item["name"] != "typeguard" for item in after["identity"]["distributions"]) + + +def test_installation_roots_are_site_directories_only(): + roots = installation_roots() + assert roots + assert all(root.is_dir() for root in roots) + assert not any("_vendor" in str(root) for root in roots) diff --git a/tests/test_qwen3_next_evidence_redaction.py b/tests/test_qwen3_next_evidence_redaction.py new file mode 100644 index 000000000..5f045e8ab --- /dev/null +++ b/tests/test_qwen3_next_evidence_redaction.py @@ -0,0 +1,105 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Published Qwen3-Next evidence carries no site paths, hosts, users or scheduler details.""" + +import json +import re +import sys +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "scripts")) + +import qwen3_next_publish_evidence as publisher # noqa: E402 + +PATTERNS = ( + r"/data/", + r"/home/", + r"/raid/", + r"/scratch", + r"\bdgx-", + r"SLURM_", + r"\bsbatch\b", + r"\bsrun\b", + r"\bwrd\b", + r"\boverflow\b", + r"\bjob_id\b", +) + + +def published_files(): + roots = [ + *ROOT.glob("docs/usage/evidence/qwen3-next-*"), + *ROOT.glob("docs/operators/qwen3-next-*.md"), + *ROOT.glob("docs/usage/qwen3-next-*.md"), + ] + for root in roots: + paths = [root] if root.is_file() else root.rglob("*") + for path in paths: + if path.is_file() and path.suffix in (".md", ".json", ".txt", ".xml", ""): + yield path + + +@pytest.mark.parametrize("path", sorted(published_files()), ids=lambda p: str(p.relative_to(ROOT))) +def test_published_qwen3_next_evidence_is_redacted(path): + text = path.read_text(errors="replace") + assert not [pattern for pattern in PATTERNS if re.search(pattern, text)] + + +def chain(tmp_path): + runs_root = tmp_path / "data" / "runs" + checkpoint = tmp_path / "data" / "ckpt" / "rev" + previous = None + for stage in ("preflight", "layers"): + run = runs_root / f"20261009T000000Z-{stage}" + (run / "attempts" / "0").mkdir(parents=True) + manifest = { + "stage": stage, + "source_commit": "c" * 40, + "checkpoint": str(checkpoint), + "cuda_home": "/usr/local/cuda-13.0", + "python": str(tmp_path / "data" / "env" / "bin" / "python"), + "previous": str(previous) if previous else None, + } + (run / "run.json").write_text(json.dumps(manifest)) + evidence = run / "attempts" / "0" / "environment.json" + evidence.write_text( + json.dumps( + { + "checkpoint": str(checkpoint / "config.json"), + "executable": manifest["python"], + "host": "dgx-1a-02-03", + "site": "/usr/lib/python3/site-packages/vllm/__init__.py", + } + ) + ) + result = { + "status": "passed", + "evidence": {"environment": {"path": "attempts/0/environment.json"}}, + } + (run / "result.json").write_text(json.dumps(result)) + previous = run + return previous + + +def test_publisher_replaces_every_recorded_root(tmp_path): + out = tmp_path / "published" + publisher.publish(publisher.chain_runs(chain(tmp_path)), None, out) + text = (out / "layers" / "environment.json").read_text() + assert "/config.json" in text and "" in text + assert "" in text and "" in text and str(tmp_path) not in text + assert (out / "measured-commit.txt").read_text() == "c" * 40 + "\n" + sums = (out / "SHA256SUMS").read_text().splitlines() + assert any(line.endswith("preflight/run.json") for line in sums) + + +def test_publisher_refuses_text_that_survives_redaction(tmp_path): + last = chain(tmp_path) + leak = last / "attempts" / "0" / "environment.json" + leak.write_text(json.dumps({"note": "copied from /scratch/elsewhere"})) + with pytest.raises(SystemExit, match="survived redaction"): + publisher.publish(publisher.chain_runs(last), None, tmp_path / "published") + assert not (tmp_path / "published").exists() diff --git a/tests/test_qwen3_next_failure.py b/tests/test_qwen3_next_failure.py new file mode 100644 index 000000000..235f68606 --- /dev/null +++ b/tests/test_qwen3_next_failure.py @@ -0,0 +1,136 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Failure classification must retain evidence and never guess from signals.""" + +import errno +import json +import subprocess +import sys +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) +from qwen3_next_runtime import failure_kind, subprocess_failure # noqa: E402 + + +@pytest.mark.parametrize( + "text,expected", + [ + ("Killed", "unknown"), + ("exit 137", "unknown"), + ("MemoryError: allocation failed", "unknown"), + ("torch.OutOfMemoryError: allocation failed", "out_of_memory"), + ("RuntimeError: CUDA out of memory", "out_of_memory"), + ("OSError: [Errno 28] No space left on device", "insufficient_disk"), + ("RuntimeError: insufficient_disk: budget", "insufficient_disk"), + ("ModuleNotFoundError: No module named 'vime'", "environment_mismatch"), + ("RuntimeError: environment_mismatch: changed", "environment_mismatch"), + ("RuntimeError: source_mismatch: changed", "source_mismatch"), + ("RuntimeError: checkpoint_mismatch: changed", "checkpoint_mismatch"), + ("subprocess.TimeoutExpired: command exceeded timeout", "time_limit"), + ], +) +def test_tail_classification_is_explicit(tmp_path, text, expected): + log = tmp_path / "worker.log" + log.write_text(text) + failure = subprocess_failure(-9, log) + assert failure_kind(failure) == expected + assert text in str(failure) + assert str(log) in str(failure) + + +def test_exception_classification_and_actual_sigkill(tmp_path): + from qwen3_next_l2 import run_process + + assert failure_kind(OSError(errno.ENOSPC, "disk full")) == "insufficient_disk" + assert failure_kind(subprocess.TimeoutExpired("worker", 1)) == "time_limit" + with pytest.raises(RuntimeError) as caught: + run_process( + [sys.executable, "-c", "import os, signal; os.kill(os.getpid(), signal.SIGKILL)"], + log_path=tmp_path / "killed.log", + ) + assert failure_kind(caught.value) == "unknown" + with pytest.raises(subprocess.TimeoutExpired) as caught: + run_process( + [sys.executable, "-c", "import time; time.sleep(30)"], + log_path=tmp_path / "timeout.log", + timeout=0.02, + cleanup_grace_seconds=0.05, + ) + assert failure_kind(caught.value) == "time_limit" + + +def test_timeout_kills_ignoring_grandchild_even_after_leader_exits(tmp_path): + import os + import signal + import time + + from qwen3_next_l2 import run_process + + pid_file = tmp_path / "grandchild.pid" + grandchild = ( + "import os,pathlib,signal,time; " + "signal.signal(signal.SIGTERM, signal.SIG_IGN); " + f"pathlib.Path({str(pid_file)!r}).write_text(str(os.getpid())); time.sleep(60)" + ) + leader = ( + "import subprocess,sys,time; " + f"subprocess.Popen([sys.executable, '-c', {grandchild!r}]); time.sleep(60)" + ) + pid = None + try: + with pytest.raises(subprocess.TimeoutExpired) as caught: + run_process( + [sys.executable, "-c", leader], + log_path=tmp_path / "family.log", + timeout=0.5, + cleanup_grace_seconds=0.15, + ) + assert failure_kind(caught.value) == "time_limit" + pid = int(pid_file.read_text()) + stat = Path(f"/proc/{pid}/stat") + # An orphan may briefly remain as a zombie until init reaps it, but must + # exit once the kernel schedules its pending SIGKILL. We cannot waitpid + # an orphan that has already been adopted by init. + deadline = time.monotonic() + 2 + while stat.exists() and stat.read_text().split(") ", 1)[1].split()[0] != "Z": + if time.monotonic() >= deadline: + break + time.sleep(0.01) + assert not stat.exists() or stat.read_text().split(") ", 1)[1].split()[0] == "Z" + finally: + if pid is None and pid_file.exists(): + pid = int(pid_file.read_text()) + if pid is not None: + try: + os.kill(pid, signal.SIGKILL) + except ProcessLookupError: + pass + + +def test_training_failure_binds_run_attempt_and_error(tmp_path, monkeypatch): + import qwen3_next_training as training + + manifest = tmp_path / "run.json" + manifest.write_text(json.dumps({"run_id": "controlled-run"})) + output = tmp_path / "attempts/2" + monkeypatch.setenv("QWEN3_NEXT_ATTEMPT", "2") + monkeypatch.setattr( + sys, "argv", ["training", "--manifest", str(manifest), "--output", str(output)] + ) + + def fail(*args): + raise OSError(errno.ENOSPC, "controlled full disk") + + monkeypatch.setattr(training, "execute", fail) + with pytest.raises(OSError): + training.main() + result = json.loads((output / "result.json").read_text()) + assert (result["run_id"], result["attempt"], result["failure_kind"]) == ( + "controlled-run", + "2", + "insufficient_disk", + ) + assert "controlled full disk" in result["error"] diff --git a/tests/test_qwen3_next_forward_contract.py b/tests/test_qwen3_next_forward_contract.py index 56d681b21..62ea492f9 100644 --- a/tests/test_qwen3_next_forward_contract.py +++ b/tests/test_qwen3_next_forward_contract.py @@ -16,6 +16,9 @@ lambda: provider.stable_top10_routes(torch.zeros(1, 512)), lambda: provider.combine_routes(torch.zeros(1, 10, 8), torch.zeros(1, 10)), lambda: provider.shared_attention(*(torch.zeros(1, 4, 8, 256) for _ in range(3))), + lambda: provider.shared_logprob( + torch.zeros(1, 151936), torch.zeros(1, dtype=torch.long), contract=None + ), ], ) def test_required_cuda_primitives_reject_cpu(call): diff --git a/tests/test_qwen3_next_imported_binary.py b/tests/test_qwen3_next_imported_binary.py new file mode 100644 index 000000000..9ac370fc2 --- /dev/null +++ b/tests/test_qwen3_next_imported_binary.py @@ -0,0 +1,107 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""CPU integrity checks; stub loader metadata does not claim CUDA execution.""" + +import hashlib +import importlib.machinery +from types import SimpleNamespace as Namespace + +import pytest + +from rl_engine.integrations import qwen3_next_binary as binary + + +def extension_metadata(tmp_path, monkeypatch): + path = tmp_path / ("_C" + importlib.machinery.EXTENSION_SUFFIXES[0]) + path.write_bytes(b"controlled-native-loader-metadata") + loader = importlib.machinery.ExtensionFileLoader("rl_engine._C", str(path)) + module = Namespace( + __name__="rl_engine._C", + __file__=str(path), + __spec__=Namespace(loader=loader, origin=str(path)), + ) + imports = [] + + def import_module(name): + imports.append(name) + return module + + monkeypatch.setattr(binary.importlib, "import_module", import_module) + return path, module, imports + + +def test_import_is_required_and_content_is_rehashed_after_same_size_replacement( + tmp_path, monkeypatch +): + path, _module, imports = extension_metadata(tmp_path, monkeypatch) + first = binary.imported_provider_identity() + assert first == { + "module": "rl_engine._C", + "path": str(path.resolve()), + "sha256": hashlib.sha256(path.read_bytes()).hexdigest(), + } + path.write_bytes(b"X" * path.stat().st_size) + second = binary.imported_provider_identity() + assert first["sha256"] != second["sha256"] + assert imports == ["rl_engine._C", "rl_engine._C"] + + +def test_extension_import_failure_does_not_use_python_fallback(monkeypatch): + def unavailable(name): + raise ImportError("controlled missing extension") + + monkeypatch.setattr(binary.importlib, "import_module", unavailable) + with pytest.raises(RuntimeError, match="cannot be imported"): + binary.imported_provider_identity() + + +@pytest.mark.parametrize("drift", ["name", "loader", "suffix", "origin"]) +def test_native_identity_rejects_fallback_or_disagreeing_import_metadata( + tmp_path, monkeypatch, drift +): + path, module, _imports = extension_metadata(tmp_path, monkeypatch) + if drift == "name": + module.__name__ = "fallback" + elif drift == "loader": + module.__spec__.loader = importlib.machinery.SourceFileLoader("rl_engine._C", str(path)) + elif drift == "suffix": + module.__file__ = str(tmp_path / "fallback.py") + else: + other = tmp_path / "other.so" + other.write_bytes(path.read_bytes()) + module.__spec__.origin = str(other) + with pytest.raises(RuntimeError, match="native extension|import origin"): + binary.imported_provider_identity() + + +def test_file_write_during_hash_is_rejected(tmp_path, monkeypatch): + path, _module, _imports = extension_metadata(tmp_path, monkeypatch) + original = binary.os.fstat + calls = 0 + + def modified_fstat(fd): + nonlocal calls + calls += 1 + if calls == 2: + path.write_bytes(b"changed-during-read") + return original(fd) + + monkeypatch.setattr(binary.os, "fstat", modified_fstat) + with pytest.raises(RuntimeError, match="changed while"): + binary.imported_provider_identity() + + +@pytest.mark.parametrize( + "value", + [ + None, + {}, + {"module": "fallback", "path": "/a.so", "sha256": "a" * 64}, + {"module": "rl_engine._C", "path": "relative.so", "sha256": "a" * 64}, + {"module": "rl_engine._C", "path": "/a.so", "sha256": "unknown"}, + ], +) +def test_serialized_binary_identity_is_mandatory_and_complete(value): + with pytest.raises(ValueError, match="binary evidence"): + binary.validate_provider_identity(value) diff --git a/tests/test_qwen3_next_installed_provider_identity.py b/tests/test_qwen3_next_installed_provider_identity.py new file mode 100644 index 000000000..5b37860ce --- /dev/null +++ b/tests/test_qwen3_next_installed_provider_identity.py @@ -0,0 +1,45 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Wheel metadata alone cannot detect a locally patched numerical provider.""" + +import sys +from pathlib import Path +from types import SimpleNamespace + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) +from qwen3_next_runtime import installed_provider_sources # noqa: E402 + + +def test_same_version_provider_patch_changes_content_identity(tmp_path): + name = "vllm/model_executor/determinism/batch_invariant.py" + path = tmp_path / name + path.parent.mkdir(parents=True) + path.write_text("KERNEL = 'original'\n") + dist = SimpleNamespace( + metadata={"Name": "vllm"}, files=[name], locate_file=lambda name: tmp_path / name + ) + first = installed_provider_sources([dist]) + path.write_text("KERNEL = 'patched'\n") + second = installed_provider_sources([dist]) + assert first[name]["sha256"] != second[name]["sha256"] + assert first[name]["path"] == second[name]["path"] + + +@pytest.mark.parametrize("files", [None, [], ["vllm/unrelated.py"]]) +def test_missing_provider_inventory_is_rejected(files): + dist = SimpleNamespace(metadata={"Name": "vllm"}, files=files) + with pytest.raises(ValueError): + installed_provider_sources([dist]) + + +def test_provider_file_deleted_after_install_is_rejected(tmp_path): + dist = SimpleNamespace( + metadata={"Name": "vllm"}, + files=["vllm/model_executor/determinism/batch_invariant.py"], + locate_file=lambda name: tmp_path / name, + ) + with pytest.raises(FileNotFoundError): + installed_provider_sources([dist]) diff --git a/tests/test_qwen3_next_l2.py b/tests/test_qwen3_next_l2.py new file mode 100644 index 000000000..de4b29119 --- /dev/null +++ b/tests/test_qwen3_next_l2.py @@ -0,0 +1,719 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""CPU artifact and matrix contracts, never real-model acceptance evidence.""" + +import copy +import sys +from pathlib import Path + +import pytest +import torch + +from rl_engine.testing.qwen3_next_l2 import ( + PROMPT_LENGTHS, + RESPONSE_LENGTH, + VOCAB_SIZE, + L2Case, + case_matrix, + compare_artifacts, + compare_batch_prefill, + embedding_digest, + first_divergent_layer, + freeze_responses, + layer_digest, + layer_state_names, + load_tensor_artifact, + prompt_fixture, + save_tensor_artifact, + validate_fixture, + validate_model_artifact, + worker_request, +) + + +class UnitTokenizer: + def encode(self, text, *, add_special_tokens): + assert not add_special_tokens + return [ord(char) for char in text] + + +@pytest.fixture(scope="module") +def frozen(): + prompts = prompt_fixture(UnitTokenizer(), {"tokenizer.json": "test-tokenizer-hash"}) + baseline = { + str(length): { + "response_tokens": torch.arange(64, dtype=torch.int64).view(4, 16), + "logprobs": torch.full((4, 16), -1.0), + } + for length in PROMPT_LENGTHS + } + return freeze_responses( + prompts, + baseline, + baseline_identity="unit-test-only", + baseline_records={ + str(length): {"path": f"/unit-test-only/{length}.pt", "sha256": "1" * 64} + for length in PROMPT_LENGTHS + }, + ) + + +@pytest.fixture(scope="module") +def small_artifact(frozen): + request = worker_request( + L2Case(8, 1, "full"), frozen, role="rollout", identity={"test_only": True} + ) + return request, make_artifact(request) + + +def subnode_values(batch): + from rl_engine.integrations.qwen3_next_subnode import SUBNODES, subnode_layout + + return { + name: torch.zeros((batch, 16, subnode_layout(name)[0]), dtype=subnode_layout(name)[1]) + for name in SUBNODES + } + + +def make_artifact(request): + batch = len(request["prompt_tokens"]) + output = torch.zeros((batch, 16, 2048), dtype=torch.bfloat16) + + def states(layer): + return { + name: torch.ones( + (batch, 1, 2, 2), + dtype=torch.float32 if "recurrent_state" in name else torch.bfloat16, + ) + for name in layer_state_names(layer) + } + + return { + "metadata": { + key: request[key] + for key in ("schema_version", "case_id", "role", "identity", "request_sha256") + }, + "execution": request["execution"], + "prompt_tokens": torch.tensor(request["prompt_tokens"], dtype=torch.int64), + "response_tokens": torch.tensor(request["response_tokens"], dtype=torch.int64), + "prediction_positions": torch.tensor(request["prediction_positions"], dtype=torch.int64), + "logits": torch.zeros((batch, RESPONSE_LENGTH, VOCAB_SIZE), dtype=torch.bfloat16), + "logprobs": torch.full((batch, RESPONSE_LENGTH), -1.0, dtype=torch.float32), + "layer_digests": [ + layer_digest(layer, output, states(layer), subnodes=subnode_values(batch)) + for layer in range(48) + ], + "embedding_digest": embedding_digest(torch.zeros((batch, 16, 2048), dtype=torch.bfloat16)), + } + + +def test_exact_sixteen_case_matrix(): + cases = case_matrix() + assert len(cases) == 16 and len({case.case_id for case in cases}) == 16 + assert {case.prompt_length for case in cases} == {8, 64, 256, 1024} + assert {case.batch_size for case in cases} == {1, 4} + assert {case.prefill for case in cases} == {"full", "chunked"} + assert all( + sum(case.chunks) == case.prompt_length and all(size > 0 for size in case.chunks) + for case in cases + ) + assert L2Case(1024, 4, "chunked").chunks[:2] == [1, 63] + + +def test_offsets_and_batch_one_reuse_frozen_first_sample(frozen): + validate_fixture(frozen) + one = worker_request(L2Case(64, 1, "chunked"), frozen, role="recompute", identity={}) + four = worker_request(L2Case(64, 4, "full"), frozen, role="rollout", identity={}) + assert one["prompt_tokens"][0] == four["prompt_tokens"][0] + assert one["response_tokens"][0] == four["response_tokens"][0] + assert one["prediction_positions"] == list(range(63, 79)) + assert one["consumed_response_tokens"] == 15 + assert len(one["consumed_input_tokens"][0]) == 79 + assert one["state_position"] == 78 + assert one["execution"] == {"recompute_mode": "full_from_tokens", "input_sequence_length": 79} + assert one["prefill_chunks"] is None + assert one["rollout_prefill_chunks"] == [1, 63] + assert not any(key in one for key in ("hidden_states", "recurrent_state", "rollout_state")) + + +def test_frozen_tokens_and_old_logprobs_cannot_change(frozen): + for field in ("prompts", "responses", "old_logprobs"): + changed = copy.deepcopy(frozen) + changed[field]["64"][0, 0] += 1 + with pytest.raises(ValueError): + validate_fixture(changed) + + +def test_weights_only_tensor_serialization_roundtrip(tmp_path, frozen): + path = tmp_path / "input.pt" + save_tensor_artifact(path, frozen) + restored = load_tensor_artifact(path) + validate_fixture(restored) + assert restored["content_sha256"] == frozen["content_sha256"] + assert not path.with_suffix(".pt.partial").exists() + + +def test_valid_original_bf16_artifact(small_artifact): + request, artifact = small_artifact + validate_model_artifact(artifact, request) + report = compare_artifacts(artifact, artifact) + assert report["passed"] and report["first_divergent_layer"] is None + + +@pytest.mark.parametrize("field", ["logits", "logprobs"]) +def test_signed_zero_is_a_real_full_model_difference(small_artifact, field): + _, artifact = small_artifact + candidate = {**artifact, field: artifact[field].clone()} + reference = {**artifact, field: artifact[field].clone()} + reference[field].reshape(-1)[0] = 0.0 + candidate[field].reshape(-1)[0] = -0.0 + report = compare_artifacts(reference, candidate) + assert not report["passed"] + assert report["first_difference"]["tensor"] == field + assert report["first_difference"]["flat_element"] == 0 + + +@pytest.mark.parametrize("value", [float("nan"), float("inf"), float("-inf")]) +def test_nonfinite_logits_never_pass(small_artifact, value): + request, artifact = small_artifact + changed = {**artifact, "logits": artifact["logits"].clone()} + changed["logits"][0, 0, 0] = value + with pytest.raises(ValueError, match="nonfinite"): + validate_model_artifact(changed, request) + + +def test_widening_logits_or_truncating_vocab_is_rejected(small_artifact): + request, artifact = small_artifact + for logits in (artifact["logits"].float(), artifact["logits"][..., :1024]): + with pytest.raises(ValueError, match="original storage"): + validate_model_artifact({**artifact, "logits": logits}, request) + + +def test_wrong_response_offset_and_changed_tokens_are_rejected(small_artifact): + request, artifact = small_artifact + with pytest.raises(ValueError, match="offsets"): + validate_model_artifact( + {**artifact, "prediction_positions": artifact["prediction_positions"] + 1}, request + ) + with pytest.raises(ValueError, match="frozen response"): + validate_model_artifact( + {**artifact, "response_tokens": artifact["response_tokens"] + 1}, request + ) + + +def test_missing_layers_and_placeholder_hashes_are_rejected(small_artifact): + request, artifact = small_artifact + with pytest.raises(ValueError, match="48-layer"): + validate_model_artifact( + {**artifact, "layer_digests": artifact["layer_digests"][:-1]}, request + ) + changed = copy.deepcopy(artifact["layer_digests"]) + changed[0]["sample_output_sha256"] = [] + with pytest.raises(ValueError, match="Every layer"): + validate_model_artifact({**artifact, "layer_digests": changed}, request) + changed = copy.deepcopy(artifact["layer_digests"]) + changed[0]["output_sha256"] = "0" * 64 + with pytest.raises(ValueError, match="bound"): + validate_model_artifact({**artifact, "layer_digests": changed}, request) + + +def test_first_divergence_tracks_state_even_when_final_logits_match(small_artifact): + _, artifact = small_artifact + changed = copy.deepcopy(artifact["layer_digests"]) + changed[11]["sample_state_sha256"] = ["e" * 64] + candidate = {**artifact, "layer_digests": changed} + result = compare_artifacts(artifact, candidate) + assert not result["passed"] and result["first_divergent_layer"] == 11 + + +@pytest.mark.parametrize( + "node", ["input_layernorm", "mixer", "post_attention_layernorm", "router", "mlp"] +) +def test_first_divergent_node_names_the_sub_node(small_artifact, node): + from rl_engine.testing.qwen3_next_l2 import first_divergent_node + + _, artifact = small_artifact + changed = copy.deepcopy(artifact["layer_digests"]) + changed[23]["sample_subnode_sha256"][node] = ["e" * 64] + changed[30]["sample_output_sha256"] = ["e" * 64] + candidate = {**artifact, "layer_digests": changed} + assert first_divergent_node(artifact, candidate) == {"layer": 23, "node": node} + assert compare_artifacts(artifact, candidate)["first_divergent_node"]["node"] == node + + +def test_first_divergent_node_starts_at_the_embedding(small_artifact): + from rl_engine.testing.qwen3_next_l2 import first_divergent_node + + _, artifact = small_artifact + changed = copy.deepcopy(artifact["layer_digests"]) + changed[0]["sample_subnode_sha256"]["input_layernorm"] = ["e" * 64] + candidate = { + **artifact, + "layer_digests": changed, + "embedding_digest": {"sample_sha256": ["e" * 64], "sha256": "x"}, + } + assert first_divergent_node(artifact, candidate) == {"layer": 0, "node": "embedding"} + + +def test_first_divergent_node_falls_back_to_layer_output_and_state(small_artifact): + from rl_engine.testing.qwen3_next_l2 import first_divergent_node + + _, artifact = small_artifact + assert first_divergent_node(artifact, artifact) is None + changed = copy.deepcopy(artifact["layer_digests"]) + changed[5]["sample_state_sha256"] = ["e" * 64] + assert first_divergent_node(artifact, {**artifact, "layer_digests": changed}) == { + "layer": 5, + "node": "state", + } + + +def test_layer_digest_requires_every_sub_node_in_order(small_artifact): + request, artifact = small_artifact + output = torch.zeros((1, 16, 2048), dtype=torch.bfloat16) + states = { + name: torch.ones( + (1, 1, 2, 2), dtype=torch.float32 if "recurrent_state" in name else torch.bfloat16 + ) + for name in layer_state_names(0) + } + nodes = subnode_values(1) + with pytest.raises(ValueError, match="sub-nodes"): + layer_digest(0, output, states, subnodes=dict(reversed(list(nodes.items())))) + nodes["router"] = nodes["router"].int() + with pytest.raises(ValueError, match="router"): + layer_digest(0, output, states, subnodes=nodes) + + +def test_batch_invariance_selects_first_samples(frozen, small_artifact): + _, candidate = small_artifact + request = worker_request( + L2Case(8, 4, "full"), frozen, role="rollout", identity={"test_only": True} + ) + reference = make_artifact(request) + assert all(compare_batch_prefill(reference, candidate).values()) + reference["logits"][3, 0, 0] = 3.0 + assert all(compare_batch_prefill(reference, candidate).values()) + reference["logits"][0, 0, 0] = 3.0 + assert not compare_batch_prefill(reference, candidate)["logits"] + reference["layer_digests"][7]["sample_state_sha256"][0] = "f" * 64 + assert first_divergent_layer(reference, candidate) == 7 + + +def test_only_initial_rollout_can_generate_fixture(frozen): + for role, case in ( + ("recompute", L2Case(64, 4, "full")), + ("rollout", L2Case(64, 1, "full")), + ("rollout", L2Case(64, 4, "chunked")), + ): + with pytest.raises(ValueError, match="Only initial"): + worker_request(case, frozen, role=role, identity={}, generate=True) + + +def test_native_worker_absence_fails_readiness(monkeypatch): + sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) + import qwen3_next_l2 as runner + + def absent(name): + raise ModuleNotFoundError(name) + + monkeypatch.setattr(runner.importlib, "import_module", absent) + with pytest.raises(RuntimeError, match="native_workers_not_ready"): + runner.check_ready() + + +def test_gpu_release_never_cancels_an_unrelated_process(monkeypatch): + sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) + import qwen3_next_l2 as runner + + commands = [] + + def output(command, **kwargs): + commands.append(command) + return "99, GPU-owned, 10\\n123, GPU-other, 20\\n".replace("\\n", "\n") + + monkeypatch.setattr(runner.subprocess, "check_output", output) + with pytest.raises(RuntimeError, match="gpu_memory_not_released"): + runner.wait_gpu_release(["GPU-owned"], timeout=0) + runner.wait_gpu_release(["GPU-free"], timeout=0) + assert all(command[0] == "nvidia-smi" for command in commands) + + +def test_batch_callback_loads_once_and_freezes_before_recompute_cases(tmp_path, monkeypatch): + import json + from types import SimpleNamespace + + sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) + import qwen3_next_l2 as runner + + calls = [] + + def native_batch(cases): + calls.append([]) + for request, path in cases: + calls[-1].append(request) + responses = request["response_tokens"] + batch = len(request["prompt_tokens"]) + if responses is None: + responses = torch.arange(batch * 16).view(batch, 16).tolist() + save_tensor_artifact( + path, + { + "response_tokens": torch.tensor(responses), + "logprobs": torch.full((batch, 16), -1.0), + }, + ) + + def eager_recompute(cases): + # The actual recompute worker must collect requests before spawning its + # single actor lifetime; none of these outputs exist during collection. + requests = list(cases) + assert len(requests) == 16 + assert all(not path.exists() for _, path in requests) + native_batch(requests) + + module = SimpleNamespace(run_rollout_cases=native_batch, run_recompute_cases=eager_recompute) + monkeypatch.setattr(runner, "check_ready", lambda: (module, {"batched_cases": True})) + monkeypatch.setattr(runner, "verify_environment", lambda *a, **k: None) + monkeypatch.setattr(runner, "verify_before_model_load", lambda *a, **k: None) + monkeypatch.setattr(runner, "validate_model_artifact", lambda *a: None) + monkeypatch.setitem( + sys.modules, + "transformers", + SimpleNamespace( + AutoTokenizer=SimpleNamespace(from_pretrained=lambda *a, **k: UnitTokenizer()) + ), + ) + checkpoint = {"metadata": {"tokenizer": "pinned"}} + manifest = { + "source_commit": "unit", + "source_tree": "unit", + "environment_sha256": "unit", + "checkpoint": "/unit", + "checkpoint_identity": checkpoint, + "provider_config": {}, + "topology": {"tp": 4, "pp": 1, "cp": 1, "ep": 1}, + "environment": {}, + } + monkeypatch.setattr(runner, "checkpoint_identity", lambda path: checkpoint) + fixture = tmp_path / "inputs/frozen.pt" + runner.worker_batch(manifest, tmp_path / "rollout", "rollout", fixture) + assert len(calls) == 1 and len(calls[0]) == 20 + assert all(row["response_tokens"] is None for row in calls[0][:4]) + assert all(row["response_tokens"] is not None for row in calls[0][4:]) + frozen_inputs = load_tensor_artifact(fixture) + validate_fixture(frozen_inputs) + runner.worker_batch(manifest, tmp_path / "recompute", "recompute", fixture) + assert len(calls) == 2 and len(calls[1]) == 16 + assert all(row["response_tokens"] is not None for row in calls[1]) + result = json.loads((tmp_path / "recompute/worker-result.json").read_text()) + assert len(result["cases"]) == 16 + updated = { + **manifest, + "weight_version": "update-1", + "base_checkpoint": "/unit", + "base_checkpoint_identity": checkpoint, + "checkpoint": "/updated-unit", + "checkpoint_identity": {"metadata": {"tokenizer": "pinned", "index": "new"}}, + } + monkeypatch.setattr(runner, "verify_updated_weights", lambda manifest: {}) + runner.worker_batch(updated, tmp_path / "updated", "rollout", fixture) + assert len(calls) == 3 and len(calls[2]) == 16 + assert all(row["response_tokens"] is not None for row in calls[2]) + + +def test_passed_flag_cannot_replace_full_matrix(): + from rl_engine.testing.qwen3_next_l2 import validate_matrix_report + + with pytest.raises(ValueError, match="sixteen-case"): + validate_matrix_report({"status": "passed", "raw_bit_comparison": True}) + + +def test_updated_weights_require_existing_baseline_fixture_before_worker(tmp_path, monkeypatch): + import qwen3_next_l2 as runner + + def unexpected(): + raise AssertionError("A missing frozen fixture must fail before loading native workers") + + monkeypatch.setattr(runner, "check_ready", unexpected) + with pytest.raises(RuntimeError, match="already frozen baseline"): + runner.worker_batch( + {"weight_version": "update-1"}, tmp_path / "out", "rollout", tmp_path / "missing.pt" + ) + + +def test_updated_preload_binds_actual_export_to_baseline_source_and_version(tmp_path, monkeypatch): + import qwen3_next_l2 as runner + import qwen3_next_training_export as export + + identity = { + "source_commit": "source", + "source_tree": "tree", + "environment_sha256": "env", + "provider_config": {"provider": "strict"}, + "topology": {"tp": 4}, + } + record = { + "weight_version": "update-1", + "base_checkpoint": "/official", + "base_checkpoint_identity": {"revision": "pinned"}, + "training_identity": identity, + } + manifest = { + **identity, + **{ + key: record[key] + for key in ("weight_version", "base_checkpoint", "base_checkpoint_identity") + }, + "checkpoint": "/updated", + "checkpoint_identity": record, + } + calls = [] + + def deep_verify(path, expected): + calls.append((path, expected)) + return record + + monkeypatch.setattr(export, "validate_training_export", deep_verify) + assert runner.verify_before_model_load(manifest, tmp_path) == record + assert calls == [("/updated", record)] + manifest["source_commit"] = "different" + with pytest.raises(RuntimeError, match="changed source_commit"): + runner.verify_updated_weights(manifest) + + +def test_debug_capture_must_contain_matching_layer_state(small_artifact): + from rl_engine.testing.qwen3_next_l2 import validate_debug_capture + + request, artifact = small_artifact + request = {**request, "trace_layers": [2]} + with pytest.raises(ValueError, match="missing"): + validate_debug_capture(artifact, request) + capture = { + "layer.2.output": torch.zeros((1, 16, 2048), dtype=torch.bfloat16), + **{ + f"layer.2.{name}": torch.ones( + (1, 1, 2, 2), dtype=torch.float32 if "recurrent_state" in name else torch.bfloat16 + ) + for name in layer_state_names(2) + }, + **{f"layer.2.subnode.{name}": value for name, value in subnode_values(1).items()}, + } + validate_debug_capture({**artifact, "debug_tensors": capture}, request) + capture["layer.2.tp0.recurrent_state"].zero_() + with pytest.raises(ValueError, match="differ"): + validate_debug_capture({**artifact, "debug_tensors": capture}, request) + + +def test_model_artifact_hash_is_rechecked_before_deserialization(tmp_path): + from qwen3_next_runtime import file_digest + + from rl_engine.testing.qwen3_next_l2 import checked_artifact_path + + artifact = tmp_path / "output.pt" + save_tensor_artifact(artifact, {"logits": torch.zeros(2, dtype=torch.bfloat16)}) + record = {"path": str(artifact), "sha256": file_digest(artifact)} + checked_artifact_path(record, artifact_root=tmp_path) + save_tensor_artifact(artifact, {"logits": torch.ones(2, dtype=torch.bfloat16)}) + with pytest.raises(ValueError, match="digest changed"): + checked_artifact_path(record, artifact_root=tmp_path) + + +def scheduler_request_and_audit(frozen): + identity = { + "weight_version": "baseline", + "provider_config": { + "profile": "qwen3_next_tp4_shared_eager_v1", + "native_rollout_kv_cache_memory_bytes": 4 << 30, + }, + } + request = worker_request(L2Case(64, 4, "chunked"), frozen, role="rollout", identity=identity) + computed = 0 + steps = [] + for count in [*request["prefill_chunks"], *([1] * 15)]: + steps.append( + { + "samples": [3, 1, 0, 2], + "computed_before": [computed] * 4, + "token_counts": [count] * 4, + } + ) + computed += count + return request, { + "provider_execution_mode": "qualification", + "native_runtime_profile": { + "kv_cache_memory_bytes_per_gpu": 4 << 30, + "max_model_len": 1040, + "max_num_seqs": 64, + "max_num_batched_tokens": 16384, + "scheduler": "rl_engine.integrations.qwen3_next_native_rollout.ExactPrefillScheduler", + "async_scheduling": False, + "prefix_caching": False, + "weights_dtype": "bfloat16", + "recurrent_state_dtype": "float32", + "model_runner": "v1", + }, + "native_scheduler_steps": steps, + } + + +def test_actual_scheduler_must_follow_requested_chunks_and_offsets(frozen): + from rl_engine.testing.qwen3_next_l2 import validate_native_scheduler_execution + + request, artifact = scheduler_request_and_audit(frozen) + validate_native_scheduler_execution(artifact, request) + for key in ("token_counts", "computed_before", "samples"): + changed = copy.deepcopy(artifact) + changed["native_scheduler_steps"][0][key][0] += 1 + with pytest.raises(ValueError, match="sample/chunk/offset"): + validate_native_scheduler_execution(changed, request) + changed = {**artifact, "native_scheduler_steps": artifact["native_scheduler_steps"][:-1]} + with pytest.raises(ValueError, match="boundary"): + validate_native_scheduler_execution(changed, request) + + +def test_scheduler_cannot_shrink_cache_or_claim_certified_execution(frozen): + from rl_engine.testing.qwen3_next_l2 import validate_native_scheduler_execution + + request, artifact = scheduler_request_and_audit(frozen) + artifact["native_runtime_profile"]["kv_cache_memory_bytes_per_gpu"] //= 2 + with pytest.raises(ValueError, match="runtime"): + validate_native_scheduler_execution(artifact, request) + request, artifact = scheduler_request_and_audit(frozen) + artifact["provider_execution_mode"] = "certified" + with pytest.raises(ValueError, match="certificate identity"): + validate_native_scheduler_execution(artifact, request) + + +def test_layer_hashes_cannot_replace_four_original_worker_traces(frozen): + from rl_engine.testing.qwen3_next_l2 import validate_native_worker_traces + + request, artifact = scheduler_request_and_audit(frozen) + with pytest.raises(ValueError, match="each TP rank"): + validate_native_worker_traces(artifact, request) + + +def test_matrix_recomputes_tensor_comparisons_even_after_reports_are_rehashed(tmp_path, frozen): + import json + + from qwen3_next_runtime import file_digest + + from rl_engine.testing.qwen3_next_binary import archive_provider_binary, validate_binary_record + from rl_engine.testing.qwen3_next_l2 import json_digest, validate_matrix_report + + source = tmp_path / "source" + (source / "rl_engine").mkdir(parents=True) + (source / "rl_engine/_C.cpython-unit.so").write_bytes(b"unit native binary") + provider_binary = archive_provider_binary(source, tmp_path, source_tree="unit") + actual_binary = validate_binary_record(provider_binary) + + identity = { + "architecture_layers": 48, + "weight_version": "baseline", + "topology": {"tp": 4, "pp": 1, "cp": 1, "ep": 1}, + "test_only": True, + "source_tree": "unit", + } + + def record(path): + return {"path": str(path), "sha256": file_digest(path)} + + def compact_artifact(request): + result = make_artifact(request) + result["provider_binary"] = actual_binary + result["logits"] = torch.zeros((), dtype=torch.bfloat16).expand(result["logits"].shape) + return result + + baselines, baseline_records = {}, {} + for length in PROMPT_LENGTHS: + request = worker_request( + L2Case(length, 4, "full"), frozen, role="rollout", identity=identity, generate=True + ) + value = compact_artifact( + {**request, "response_tokens": frozen["responses"][str(length)].tolist()} + ) + path = tmp_path / f"baseline-{length}.pt" + save_tensor_artifact(path, value) + baselines[str(length)] = value + baseline_records[str(length)] = record(path) + fixture = freeze_responses( + frozen, + baselines, + baseline_identity=json_digest(identity), + baseline_records=baseline_records, + ) + fixture_path = tmp_path / "fixture.pt" + save_tensor_artifact(fixture_path, fixture) + checks = {name: True for name in ("response_tokens", "logits", "logprobs", "layer_states")} + processes = { + role: {"exit_code": 0, "pid": index + 1, "gpu_release_verified": True} + for index, role in enumerate(("rollout", "recompute")) + } + workers = { + role: { + "status": "passed", + "role": role, + "identity": identity, + "fixture_sha256": fixture["content_sha256"], + "pid": processes[role]["pid"], + "cases": {}, + "provider_binary": actual_binary, + } + for role in processes + } + cases = {} + for case in case_matrix(): + records = {} + for role in processes: + request = worker_request(case, fixture, role=role, identity=identity) + path = tmp_path / f"{role}-{case.case_id}.pt" + save_tensor_artifact(path, compact_artifact(request)) + records[role] = record(path) + workers[role]["cases"][case.case_id] = { + **records[role], + "request_sha256": request["request_sha256"], + } + cases[case.case_id] = { + "passed": True, + "comparisons": checks, + "batch_prefill": {role: checks for role in processes}, + "baseline_generation": {role: checks for role in processes}, + "artifacts": records, + } + worker_records = {} + for role in processes: + path = tmp_path / f"{role}-worker.json" + path.write_text(json.dumps(workers[role])) + worker_records[role] = record(path) + report = { + "schema_version": 1, + "status": "passed", + "scope": "qwen3_next_full_model_l2", + "raw_bit_comparison": True, + "logits_dtype": "bfloat16", + "logprobs_dtype": "float32", + "response_length": 16, + "vocab_size": 151936, + "identity": identity, + "case_ids": [case.case_id for case in case_matrix()], + "cases": cases, + "processes": processes, + "worker_results": worker_records, + "fixture": {**record(fixture_path), "content_sha256": fixture["content_sha256"]}, + "provider_binary": provider_binary, + } + validate_matrix_report(report, expected_identity=identity, artifact_root=tmp_path) + case_id = L2Case(8, 1, "full").case_id + path = Path(cases[case_id]["artifacts"]["recompute"]["path"]) + changed = load_tensor_artifact(path) + changed["logits"] = changed["logits"].clone() + changed["logits"][0, 0, 0] = -0.0 + save_tensor_artifact(path, changed) + cases[case_id]["artifacts"]["recompute"] = record(path) + workers["recompute"]["cases"][case_id].update(record(path)) + worker_path = Path(worker_records["recompute"]["path"]) + worker_path.write_text(json.dumps(workers["recompute"])) + report["worker_results"]["recompute"] = record(worker_path) + with pytest.raises(ValueError, match="tensor bytes contradict"): + validate_matrix_report(report, expected_identity=identity, artifact_root=tmp_path) diff --git a/tests/test_qwen3_next_layers.py b/tests/test_qwen3_next_layers.py new file mode 100644 index 000000000..7f9cec000 --- /dev/null +++ b/tests/test_qwen3_next_layers.py @@ -0,0 +1,135 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""CPU failure-injection tests; none constitutes native model evidence.""" + +import copy +import json +import sys +from pathlib import Path + +import pytest + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "scripts")) + +from qwen3_next_layer_contract import ( # noqa: E402 + checked_record, + layer_requirements, + validate_layer_report, + validate_rank_result, +) +from qwen3_next_runtime import STAGES # noqa: E402 +from qwen3_next_runtime import identity_digest # noqa: E402 + + +def rank_row(requirement, rank): + row = { + "status": "passed", + "rank": rank, + "scope": requirement["scope"], + "full_model_l2": "not_evaluated", + **requirement.get("fields", {}), + } + for path, cases in requirement["groups"].items(): + target = row + parts = path.split(".") + for part in parts[:-1]: + target = target.setdefault(part, {}) + target[parts[-1]] = list(cases) + return row + + +def test_layers_are_required_between_preflight_and_l2(): + assert STAGES["layers"][:3] == ("512G", "04:00:00", "preflight") + assert STAGES["l2"][2] == "layers" + + +@pytest.mark.parametrize("gate", ["gdn", "attention", "moe", "adapters", "heads"]) +def test_rank_gate_requires_complete_unduplicated_inventory(gate): + requirement = layer_requirements(ROOT)["gates"][gate] + row = rank_row(requirement, 2) + validate_rank_result(row, requirement, 2) + for field in ("rank", "scope", "status", "full_model_l2"): + altered = {**row, field: "changed"} + with pytest.raises(ValueError, match="identity"): + validate_rank_result(altered, requirement, 2) + for skipped in (1, 3): + with pytest.raises(ValueError, match="skipped"): + validate_rank_result({**row, "skipped": skipped}, requirement, 2) + for path in requirement["groups"]: + for duplicate in (False, True): + changed = copy.deepcopy(row) + value = changed + for part in path.split("."): + value = value[part] + value.append(value[0]) if duplicate else value.pop() + with pytest.raises(ValueError, match="mandatory"): + validate_rank_result(changed, requirement, 2) + + +def test_artifact_hash_and_attempt_escape_are_rejected(tmp_path): + from qwen3_next_runtime import file_digest + + attempt = tmp_path / "attempt" + attempt.mkdir() + path = attempt / "rank.json" + path.write_text("{}") + record = {"path": str(path), "sha256": file_digest(path)} + assert checked_record(record, root=attempt) == path + path.write_text('{"changed": true}') + with pytest.raises(ValueError, match="digest"): + checked_record(record, root=attempt) + record["sha256"] = file_digest(path) + with pytest.raises(ValueError, match="escaped"): + checked_record(record, root=attempt / "different") + + +def test_complete_component_flags_cannot_replace_native_scheduler(tmp_path): + from rl_engine.testing.qwen3_next_binary import archive_provider_binary, validate_binary_record + + source = tmp_path / "source" + (source / "rl_engine").mkdir(parents=True) + (source / "rl_engine/_C.cpython-unit.so").write_bytes(b"unit native binary") + provider_binary = archive_provider_binary(source, tmp_path, source_tree="unit") + actual_binary = validate_binary_record(provider_binary) + identity = {"source_tree": "unit"} + requirement = layer_requirements(ROOT) + report = { + "schema_version": 1, + "status": "passed", + "phase": "complete", + "scope": "qwen3_next_layer_and_scheduler_gates", + "identity": identity, + "requirements_sha256": identity_digest(requirement), + "full_model_l2": "not_evaluated", + "gates": {}, + "provider_binary": provider_binary, + } + from qwen3_next_runtime import file_digest + + for name, expected in requirement["gates"].items(): + records = {} + for rank in range(4): + path = tmp_path / f"{name}-{rank}.json" + path.write_text( + json.dumps({**rank_row(expected, rank), "provider_binary": actual_binary}) + ) + records[str(rank)] = {"path": str(path), "sha256": file_digest(path)} + report["gates"][name] = {"exit_code": 0, "gpu_release_verified": True, "ranks": records} + with pytest.raises(ValueError, match="artifact path"): + validate_layer_report(report, requirement, identity, root=tmp_path) + report["phase"] = "native_scheduler" + with pytest.raises(ValueError, match="completeness"): + validate_layer_report(report, requirement, identity, root=tmp_path) + + +def test_missing_native_scheduler_is_explicitly_incomplete(monkeypatch): + import qwen3_next_layers as runner + + def missing(_): + raise ModuleNotFoundError("native scheduler missing") + + monkeypatch.setattr(runner.importlib, "import_module", missing) + with pytest.raises(RuntimeError, match="native_scheduler_not_ready"): + runner.check_scheduler_ready() diff --git a/tests/test_qwen3_next_logprob_adapter.py b/tests/test_qwen3_next_logprob_adapter.py new file mode 100644 index 000000000..74236b7cd --- /dev/null +++ b/tests/test_qwen3_next_logprob_adapter.py @@ -0,0 +1,43 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +import pytest +import torch + +from rl_engine.integrations.qwen3_next_logprob import ( + calculate_log_probs_and_entropy, + qwen_logprob_contract, +) + + +def test_contract_pins_complete_tp4_vocab_and_original_dtype(): + for rank in range(4): + contract = qwen_logprob_contract( + torch.empty(2, 37984, dtype=torch.bfloat16), rank=rank, world_size=4 + ) + assert contract.sharding.vocab_shard_bounds[rank] == (rank * 37984, (rank + 1) * 37984) + assert contract.sharding.padded_vocab_size == 151936 + assert contract.sharding.cp_world_size == 1 + assert contract.dtype.value == "bf16" + + +@pytest.mark.parametrize( + "rank,world,width,dtype", + [ + (0, 1, 151936, torch.bfloat16), + (4, 4, 37984, torch.bfloat16), + (0, 4, 38016, torch.bfloat16), + (0, 4, 37984, torch.float16), + ], +) +def test_contract_rejects_profile_drift(rank, world, width, dtype): + with pytest.raises(ValueError): + qwen_logprob_contract(torch.empty(0, width, dtype=dtype), rank=rank, world_size=world) + + +def test_numerical_callback_has_no_fallback_for_mask_or_absent_group(): + logits, tokens = torch.empty(0, 37984), torch.empty(0, dtype=torch.long) + with pytest.raises(ValueError, match="top-p"): + calculate_log_probs_and_entropy(logits, tokens, None, log_prob_keep_mask=torch.empty(0)) + with pytest.raises(ValueError, match="explicit initialized"): + calculate_log_probs_and_entropy(logits, tokens, None) diff --git a/tests/test_qwen3_next_native_recompute.py b/tests/test_qwen3_next_native_recompute.py new file mode 100644 index 000000000..bde75df74 --- /dev/null +++ b/tests/test_qwen3_next_native_recompute.py @@ -0,0 +1,232 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +from pathlib import Path + +import pytest +import torch + +from rl_engine.integrations.qwen3_next_native_recompute import response_rows +from rl_engine.integrations.qwen3_next_subnode import SUBNODES, subnode_layout +from rl_engine.integrations.qwen3_next_vime_runtime import runtime_argv + + +@pytest.mark.parametrize("batch", [1, 4]) +def test_response_rows_start_at_last_prompt_and_do_not_consume_last_target(batch): + request = { + "prompt_tokens": [[1] * 8 for _ in range(batch)], + "consumed_input_tokens": [[1] * 23 for _ in range(batch)], + "prediction_positions": list(range(7, 23)), + } + hidden = torch.arange(batch * 23 * 2).reshape(batch * 23, 1, 2).to(torch.bfloat16) + selected = response_rows(hidden, request) + assert selected.dtype == torch.bfloat16 and selected.shape == (batch, 16, 2) + for index in range(batch): + assert torch.equal(selected[index], hidden[index * 23 + 7 : (index + 1) * 23, 0]) + with pytest.raises(ValueError, match="BF16"): + response_rows(hidden.float(), request) + request["prediction_positions"] = list(range(8, 24)) + with pytest.raises(ValueError, match="last target"): + response_rows(hidden, request) + + +def test_native_cli_has_full_model_vocabulary_topology_and_no_precision_override(): + argv = runtime_argv("/checkpoint", training=True) + for name, value in { + "num-layers": 48, + "tensor-model-parallel-size": 4, + "expert-tensor-parallel-size": 4, + "pipeline-model-parallel-size": 1, + "context-parallel-size": 1, + "padded-vocab-size": 151936, + }.items(): + assert argv[argv.index("--" + name) + 1] == str(value) + assert "--optimization-dtype" not in argv and "--reduce-dtype" not in argv + assert "--sequence-parallel" not in argv + assert "--use-rollout-logprobs" in argv + + +def test_recompute_batch_loads_once_and_keeps_verifiable_original_rank_traces( + tmp_path, monkeypatch +): + import copy + import hashlib + + from rl_engine.integrations import qwen3_next_native_recompute as recompute + from rl_engine.testing.qwen3_next_l2 import ( + load_tensor_artifact, + save_tensor_artifact, + validate_native_worker_traces, + ) + + request = { + "schema_version": 1, + "case_id": "cpu-contract-only", + "role": "recompute", + "identity": { + "provider_config": {"profile": "qwen3_next_tp4_shared_eager_v1"}, + "weight_version": "baseline", + }, + "request_sha256": "a" * 64, + "execution": {"recompute_mode": "full_from_tokens", "input_sequence_length": 23}, + "prompt_tokens": [[1] * 8], + "response_tokens": [[2] * 16], + "prediction_positions": list(range(7, 23)), + "sample_ids": [0], + "consumed_response_tokens": 15, + "state_position": 22, + "trace_layers": [], + } + second = {**request, "case_id": "second-cpu-case", "request_sha256": "b" * 64} + calls = [] + + def spawn(target, *, args, nprocs, join): + cases, staging = args + calls.append((target, nprocs, join)) + assert len(cases) == 2 + for index, (current, _output) in enumerate(cases): + for rank in range(4): + layers = {} + for layer in range(48): + names = ( + ("key", "value") + if (layer + 1) % 4 == 0 + else ("convolution", "recurrent_state") + ) + layers[layer] = { + "output": torch.zeros((1, 16, 2048), dtype=torch.bfloat16), + "subnodes": { + name: torch.zeros( + (1, 16, subnode_layout(name)[0]), dtype=subnode_layout(name)[1] + ) + for name in SUBNODES + }, + "states": { + name: torch.zeros( + (), + dtype=( + torch.float32 if name == "recurrent_state" else torch.bfloat16 + ), + ).expand( + { + "convolution": (1, 2048, 3), + "recurrent_state": (1, 8, 128, 128), + "key": (1, 1, 23, 256), + "value": (1, 1, 23, 256), + }[name] + ) + for name in names + }, + } + layers["model"] = {"embedding": torch.zeros((1, 16, 2048), dtype=torch.bfloat16)} + trace = { + "rank": rank, + "request_sha256": current["request_sha256"], + "role": "recompute", + "execution": current["execution"], + "provider_binary": { + "module": "rl_engine._C", + "path": "/cpu-test-only/_C.so", + "sha256": "c" * 64, + }, + "layers": layers, + "local_logits": torch.zeros((), dtype=torch.bfloat16).expand(1, 16, 37984), + "logprobs": torch.zeros((1, 16)), + "responses": torch.tensor(current["response_tokens"], dtype=torch.int64), + } + if rank == 0: + trace["logits"] = torch.zeros((), dtype=torch.bfloat16).expand(1, 16, 151936) + save_tensor_artifact(Path(staging) / f"case-{index}-rank-{rank}.pt", trace) + + monkeypatch.setattr(recompute.mp, "spawn", spawn) + outputs = [(request, tmp_path / "first.pt"), (second, tmp_path / "second.pt")] + recompute.run_recompute_cases(iter(outputs)) + assert calls == [(recompute._rank_main, 4, True)] + for current, path in outputs: + artifact = load_tensor_artifact(path) + assert len(artifact["worker_traces"]) == 4 + validate_native_worker_traces(artifact, current, artifact_root=tmp_path) + artifact = load_tensor_artifact(outputs[0][1]) + for record in artifact["worker_traces"]: + assert Path(record["path"]).is_file() + + missing_binary = copy.deepcopy(artifact) + missing_binary.pop("provider_binary") + with pytest.raises(ValueError, match="imported CUDA provider binary"): + validate_native_worker_traces(missing_binary, request, artifact_root=tmp_path) + + # Merely retaining files is insufficient: even after rehashing the record, + # original hidden/state/local-logit bytes must reproduce the declared result. + record = artifact["worker_traces"][1] + original = load_tensor_artifact(record["path"]) + for field, message in ( + ("state", "state bytes"), + ("hidden", "layer output"), + ("local_logits", "local_logits bytes"), + ("binary", "provenance mismatch"), + ("convolution_truncated", "complete convolution state shape"), + ("convolution_transposed", "complete convolution state shape"), + ("recurrent_truncated", "complete recurrent_state state shape"), + ("key_short", "complete key state shape"), + ("key_long", "complete key state shape"), + ("value_truncated", "complete value state shape"), + ): + trace = copy.deepcopy(original) + if field == "state": + value = trace["layers"][0]["states"]["recurrent_state"].clone() + value[0, 0, 0, 0] = -0.0 + trace["layers"][0]["states"]["recurrent_state"] = value + elif field == "convolution_truncated": + trace["layers"][0]["states"]["convolution"] = torch.zeros((1, 1), dtype=torch.bfloat16) + elif field == "convolution_transposed": + value = trace["layers"][0]["states"]["convolution"] + trace["layers"][0]["states"]["convolution"] = value.transpose(1, 2) + elif field == "recurrent_truncated": + value = trace["layers"][0]["states"]["recurrent_state"] + trace["layers"][0]["states"]["recurrent_state"] = value[:, :7] + elif field in ("key_short", "key_long"): + length = 22 if field == "key_short" else 24 + trace["layers"][3]["states"]["key"] = torch.zeros( + (1, 1, length, 256), dtype=torch.bfloat16 + ) + elif field == "value_truncated": + value = trace["layers"][3]["states"]["value"] + trace["layers"][3]["states"]["value"] = value[..., :255] + elif field == "hidden": + trace["layers"][0]["output"][0, 0, 0] = -0.0 + elif field == "binary": + trace["provider_binary"]["sha256"] = "d" * 64 + else: + trace["local_logits"] = trace["local_logits"].clone() + trace["local_logits"][0, 0, 0] = -0.0 + save_tensor_artifact(record["path"], trace) + with open(record["path"], "rb") as stream: + record["sha256"] = hashlib.file_digest(stream, "sha256").hexdigest() + with pytest.raises(ValueError, match=message): + validate_native_worker_traces(artifact, request, artifact_root=tmp_path) + if "complete " in message: + ranks = [load_tensor_artifact(item["path"]) for item in artifact["worker_traces"]] + with pytest.raises(ValueError, match=message): + recompute._assemble(request, ranks, artifact["worker_traces"]) + + +@pytest.mark.parametrize("batch", [1, 4]) +@pytest.mark.parametrize("prompt_length", [8, 64, 256, 1024]) +def test_complete_native_state_shapes_bind_every_consumed_token(batch, prompt_length): + from rl_engine.testing.qwen3_next_l2 import native_state_shapes + + request = { + "prompt_tokens": [[1] * prompt_length for _ in range(batch)], + "consumed_response_tokens": 15, + "state_position": prompt_length + 14, + } + assert native_state_shapes(request) == { + "convolution": (batch, 2048, 3), + "recurrent_state": (batch, 8, 128, 128), + "key": (batch, 1, prompt_length + 15, 256), + "value": (batch, 1, prompt_length + 15, 256), + } + with pytest.raises(ValueError, match="exactly 15 responses"): + native_state_shapes({**request, "state_position": prompt_length + 15}) + with pytest.raises(ValueError, match="exactly 15 responses"): + native_state_shapes({**request, "consumed_response_tokens": 16}) diff --git a/tests/test_qwen3_next_operator_gates.py b/tests/test_qwen3_next_operator_gates.py new file mode 100644 index 000000000..d991a7c43 --- /dev/null +++ b/tests/test_qwen3_next_operator_gates.py @@ -0,0 +1,50 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Runner failures cannot reuse stale evidence or promote diagnostics.""" + +import json +from types import SimpleNamespace + +import pytest + +from rl_engine.testing.qwen3_next_cases import execution_verdict +from scripts import qwen3_next_operator_gates as gates + +CASE = "tests/example.py::test_required" + + +@pytest.mark.parametrize("mode", ["passed", "missing", "forged", "exit_failed", "diagnostics"]) +def test_run_suite_requires_current_complete_evidence(tmp_path, monkeypatch, mode): + suite = "diagnostics" if mode == "diagnostics" else "core" + manifest = tmp_path / "manifest.json" + manifest.write_text(json.dumps({"schema_version": 1, "suites": {suite: [CASE]}})) + monkeypatch.setattr(gates, "MANIFEST", manifest) + monkeypatch.setattr(gates.importlib.metadata, "version", lambda _: "test-version") + monkeypatch.setattr(gates.subprocess, "check_output", lambda *args, **kwargs: "") + out = tmp_path / "output" + out.mkdir() + complete = execution_verdict( + [CASE], [CASE], {CASE: {"setup": "passed", "call": "passed", "teardown": "passed"}} + ) + # This must be removed before invoking pytest, even if pytest writes nothing. + (out / "case-results.json").write_text(json.dumps(complete)) + + def run(*args, **kwargs): + if mode != "missing": + report = dict(complete) + if mode == "forged": + report["outcomes"] = {} + (out / "case-results.json").write_text(json.dumps(report)) + return SimpleNamespace(returncode=1 if mode == "exit_failed" else 0) + + monkeypatch.setattr(gates.subprocess, "run", run) + result = gates.run_suite(out, suite) + if mode == "diagnostics": + assert result["status"] == "diagnostic_completed" + assert result["diagnostic_only"] is True + assert result["mandatory_case_ids"] == [] + else: + assert result["status"] == ("passed" if mode == "passed" else "failed") + assert result["mandatory_case_ids"] == [CASE] + assert result["full_model_l2"] == "not_evaluated" diff --git a/tests/test_qwen3_next_profile.py b/tests/test_qwen3_next_profile.py new file mode 100644 index 000000000..670c8e0b2 --- /dev/null +++ b/tests/test_qwen3_next_profile.py @@ -0,0 +1,149 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""A label or environment flag cannot certify the CUSTOM provider.""" + +import copy +import sys +from types import SimpleNamespace + +import pytest + +from rl_engine.integrations import qwen3_next_profile as profile + + +def test_qualification_never_advertises_batch_invariance(monkeypatch): + monkeypatch.setenv("VLLM_BATCH_INVARIANT", "0") + assert profile.activate_for_identity({}) == "qualification" + assert not profile.profile_is_certified() + + +@pytest.mark.parametrize("mode", ["1", "", "true"]) +def test_strict_flag_without_certificate_is_rejected(monkeypatch, mode): + monkeypatch.setenv("VLLM_BATCH_INVARIANT", mode) + with pytest.raises(ValueError, match="Uncertified"): + profile.activate_for_identity({}) + assert not profile.profile_is_certified() + + +def test_passed_label_without_matching_certificate_bytes_is_rejected(tmp_path, monkeypatch): + monkeypatch.setenv("VLLM_BATCH_INVARIANT", "1") + result = tmp_path / "result.json" + result.write_text('{"status": "passed"}') + identity = {"scheduler_profile_certificate": {"path": str(result), "sha256": "0" * 64}} + with pytest.raises(ValueError, match="content identity"): + profile.activate_for_identity(identity) + assert not profile.profile_is_certified() + + +@pytest.fixture +def updated_identity(): + certified = { + "checkpoint": "/official/revision", + "checkpoint_identity": {"revision": "official", "metadata": {"tokenizer": "pinned"}}, + "source_commit": "commit", + "source_tree": "tree", + "environment_sha256": "environment", + "provider_config": {"profile": "qwen3_next_tp4_shared_eager_v1"}, + "topology": {"tp": 4, "pp": 1, "cp": 1, "ep": 1}, + "weight_version": "baseline", + "architecture_layers": 48, + "weights_dtype": "bfloat16", + "recurrent_state_dtype": "float32", + } + record = { + "schema_version": 1, + "kind": "qwen3_next_native_hf_export", + "status": "complete", + "weight_version": "update1", + "step": 1, + "export_id": "unit-test-only", + "base_checkpoint": certified["checkpoint"], + "base_checkpoint_identity": certified["checkpoint_identity"], + "training_identity": copy.deepcopy(certified), + "architecture_layers": 48, + "weights_dtype": "bfloat16", + "weights_content_sha256": "new-weight-content", + } + identity = { + **copy.deepcopy(certified), + "weight_version": "update1", + "checkpoint": "/run/current-export", + "checkpoint_identity": record, + "base_checkpoint": certified["checkpoint"], + "base_checkpoint_identity": certified["checkpoint_identity"], + } + return identity, certified + + +def test_certificate_rehashes_actual_updated_export_not_old_base(updated_identity, monkeypatch): + identity, certified = updated_identity + calls = [] + + def check(path, expected): + calls.append((path, expected)) + return expected + + monkeypatch.setitem( + sys.modules, "qwen3_next_training_export", SimpleNamespace(validate_training_export=check) + ) + profile.validate_certified_weights(identity, certified) + assert calls == [(identity["checkpoint"], identity["checkpoint_identity"])] + assert calls[0][1] != certified["checkpoint_identity"] + assert not profile.profile_is_certified() # A JSON-boundary unit check cannot activate BI. + + +@pytest.mark.parametrize( + "field", ["base_checkpoint", "base_checkpoint_identity", "weight_version", "source_tree"] +) +def test_updated_identity_cannot_rebind_base_version_or_source(updated_identity, field): + identity, certified = updated_identity + identity[field] = "changed" + with pytest.raises(ValueError): + profile.validate_certified_weights(identity, certified) + + +def test_changed_actual_export_bytes_are_rejected(updated_identity, monkeypatch): + identity, certified = updated_identity + monkeypatch.setitem( + sys.modules, + "qwen3_next_training_export", + SimpleNamespace( + validate_training_export=lambda path, expected: { + **expected, + "weights_content_sha256": "different", + }, + ), + ) + with pytest.raises(ValueError, match="Actual updated weight files"): + profile.validate_certified_weights(identity, certified) + + +def test_updated_export_cannot_be_labeled_as_baseline(updated_identity): + identity, certified = updated_identity + identity["weight_version"] = "baseline" + with pytest.raises(ValueError, match="masquerade"): + profile.validate_certified_weights(identity, certified) + + +def test_historical_export_identity_checks_do_not_open_deleted_shards(updated_identity): + from rl_engine.testing.qwen3_next_l2 import validate_weight_identity + + identity, _ = updated_identity + validate_weight_identity(identity) + # No export files exist in this unit test; this verifies structural history + # only. Actual profile activation always invokes the deep file validator. + + +def test_updated_model_identity_carries_actual_and_base_checkpoint(updated_identity): + from qwen3_next_l2 import model_identity + + identity, _ = updated_identity + model = model_identity(identity) + assert model["checkpoint"] == "/run/current-export" + assert model["checkpoint_identity"]["weights_content_sha256"] == "new-weight-content" + assert model["base_checkpoint"] == "/official/revision" + assert model["base_checkpoint_identity"] == { + "revision": "official", + "metadata": {"tokenizer": "pinned"}, + } diff --git a/tests/test_qwen3_next_rl_fixture.py b/tests/test_qwen3_next_rl_fixture.py new file mode 100644 index 000000000..c33578f1f --- /dev/null +++ b/tests/test_qwen3_next_rl_fixture.py @@ -0,0 +1,58 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +from argparse import Namespace + +import pytest +import torch + +from rl_engine.testing.qwen3_next_rl_fixture import ( + OPTIMIZER_SETTINGS, + POLICY_SETTINGS, + PolicyFixture, + require_policy_configuration, +) + + +def fixture(): + return PolicyFixture.freeze( + torch.arange(320).reshape(4, 80), + torch.full((4, 16), -3.0), + baseline_identity="test-baseline", + ) + + +def test_policy_fixture_preserves_old_policy_and_native_loss_fields(): + value = fixture() + batch = value.batch([2], device="cpu") + assert batch["partition"] == [2] + assert batch["total_lengths"] == [80] + assert batch["response_lengths"] == [16] + assert torch.equal(batch["advantages"][0], torch.full((16,), 0.75)) + value.verify_initial_recompute(torch.full((4, 16), -3.0)) + with pytest.raises(AssertionError): + value.verify_initial_recompute(torch.full((4, 16), -3.01)) + + +def test_policy_fixture_rejects_mutation_and_unfrozen_baseline(): + value = fixture() + value.old_logprobs[0, 0] = -4 + with pytest.raises(ValueError, match="modified"): + value.batch([0], device="cpu") + with pytest.raises(ValueError, match="frozen"): + PolicyFixture.freeze( + torch.arange(320).reshape(4, 80), + torch.zeros(4, 16, requires_grad=True), + baseline_identity="run", + ) + + +@pytest.mark.parametrize( + "changed", ["loss_type", "use_rollout_logprobs", "use_stateless_adam", "optimizer_cpu_offload"] +) +def test_policy_configuration_cannot_fall_back_or_substitute_optimizer(changed): + settings = {**POLICY_SETTINGS, **OPTIMIZER_SETTINGS} + require_policy_configuration(Namespace(**settings)) + settings[changed] = "unexpected" + with pytest.raises(ValueError, match=changed): + require_policy_configuration(Namespace(**settings)) diff --git a/tests/test_qwen3_next_rotary_dtype.py b/tests/test_qwen3_next_rotary_dtype.py new file mode 100644 index 000000000..4a9848ed4 --- /dev/null +++ b/tests/test_qwen3_next_rotary_dtype.py @@ -0,0 +1,58 @@ +# SPDX-License-Identifier: Apache-2.0 +"""The rotary tables must not depend on how the owning module was cast.""" + +from types import SimpleNamespace + +import torch + +from rl_engine.integrations.qwen3_next_tp_blocks import TP4FullAttention + + +def _reference_rotate(value, positions, rope_theta=10000000.0): + inv_freq = 1.0 / (rope_theta ** (torch.arange(0, 64, 2, dtype=torch.float32) / 64)) + angles = positions.float()[:, None] * inv_freq[None, :] + angles = torch.cat((angles, angles), dim=-1) + cos, sin = angles.cos().to(value.dtype)[:, None], angles.sin().to(value.dtype)[:, None] + rotary, tail = value[..., :64], value[..., 64:] + rotated = torch.cat((-rotary[..., 32:], rotary[..., :32]), dim=-1) + return torch.cat((rotary * cos + rotated * sin, tail), dim=-1) + + +def test_inverse_frequencies_are_fp32_and_exact_at_dim_zero(): + inv_freq = TP4FullAttention.inverse_frequencies(10000000.0, torch.device("cpu")) + assert inv_freq.dtype == torch.float32 and inv_freq.shape == (32,) + assert inv_freq[0].item() == 1.0 + assert torch.equal( + inv_freq, 1.0 / (10000000.0 ** (torch.arange(0, 64, 2, dtype=torch.float32) / 64)) + ) + + +def test_rotate_matches_fp32_reference_for_any_module_dtype(): + generator = torch.Generator().manual_seed(7) + value = (torch.randn((23, 1, 256), generator=generator) * 1.5).to(torch.bfloat16) + positions = torch.arange(23) + module = SimpleNamespace(rope_theta=10000000.0) + actual = TP4FullAttention._rotate(module, value, positions) + assert torch.equal(actual, _reference_rotate(value, positions)) + + +def test_bf16_rounded_frequencies_would_change_rotated_keys(): + """A BF16-cast rotary table flips rotated dims 1..~47 at positions >= 1; keep it FP32.""" + generator = torch.Generator().manual_seed(8) + value = (torch.randn((23, 1, 256), generator=generator) * 1.5).to(torch.bfloat16) + positions = torch.arange(23) + exact = _reference_rotate(value, positions) + rounded_theta = SimpleNamespace(rope_theta=10000000.0) + inv_freq = TP4FullAttention.inverse_frequencies(rounded_theta.rope_theta, value.device) + rounded = inv_freq.to(torch.bfloat16).float() + angles = positions.float()[:, None] * rounded[None, :] + angles = torch.cat((angles, angles), dim=-1) + cos, sin = angles.cos().to(value.dtype)[:, None], angles.sin().to(value.dtype)[:, None] + rotary, tail = value[..., :64], value[..., 64:] + rotated = torch.cat((-rotary[..., 32:], rotary[..., :32]), dim=-1) + with_rounded = torch.cat((rotary * cos + rotated * sin, tail), dim=-1) + differing = (with_rounded != exact)[:, 0] + assert differing.any() + assert not differing[0].any() # position 0 has angle 0 + assert not differing[:, 64:].any() # the unrotated tail is untouched + assert not differing[:, 0].any() and not differing[:, 32].any() # dim 0 is exact in BF16 diff --git a/tests/test_qwen3_next_run.py b/tests/test_qwen3_next_run.py new file mode 100644 index 000000000..6a3fd3e82 --- /dev/null +++ b/tests/test_qwen3_next_run.py @@ -0,0 +1,118 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""The scheduler-independent Qwen3-Next chain launcher.""" + +import json +import sys +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) + +import qwen3_next_run as run # noqa: E402 + + +def manifest(tmp_path, stage="layers", script=None): + source = tmp_path / "source" + (source / "scripts").mkdir(parents=True, exist_ok=True) + if script is not None: + (source / run.STAGES[stage][3]).write_text(script) + return { + "run_id": "r", + "execution_id": "e" * 32, + "stage": stage, + "source": str(source), + "source_tree": "tree", + "python": sys.executable, + "cuda_home": str(tmp_path), + "cuda_identity": {"nvcc_sha256": "nvcc"}, + "memory": "512G", + "environment": {"identity": {"base_prefix": sys.prefix, "build_tools": {"ninja": "1"}}}, + "provider_config": {"runtime_env": {"TRITON_F32_DEFAULT": "ieee"}}, + "scheduler_profile_certificate": {"path": "/certificate"}, + } + + +@pytest.fixture +def frozen(monkeypatch): + monkeypatch.setattr(run, "verify_environment", lambda *args, **kwargs: None) + monkeypatch.setattr(run, "file_digest", lambda path: "nvcc") + monkeypatch.setattr(run, "_git", lambda *args, cwd=None: "tree" if "rev-parse" in args else "") + + +def test_prepare_refuses_a_dirty_tree(monkeypatch, tmp_path): + monkeypatch.setattr(run, "_git", lambda *args, cwd=None: " M scripts/qwen3_next_run.py") + with pytest.raises(SystemExit, match="Commit the source"): + run.prepare("preflight", None, None) + + +@pytest.mark.parametrize( + "stage,bi", [("preflight", "0"), ("layers", "0"), ("l2", "1"), ("train", "1")] +) +def test_stage_environment_pins_batch_invariance_and_the_declared_memory(tmp_path, stage, bi): + env = run.stage_environment(manifest(tmp_path, stage), tmp_path, "0") + assert env["VLLM_BATCH_INVARIANT"] == bi + assert env["QWEN3_NEXT_ALLOCATED_MEMORY"] == "512G" + assert env["QWEN3_NEXT_ATTEMPT"] == "0" + assert env["TRITON_F32_DEFAULT"] == "ieee" + assert env["PYTHONPATH"].endswith("source") + assert ("QWEN_SHARED_PROFILE_RESULT" in env) == (bi == "1") + + +def test_execute_validates_and_publishes_a_passed_result(tmp_path, frozen, monkeypatch): + script = ( + "import json, sys\n" + "out = sys.argv[sys.argv.index('--output') + 1]\n" + "json.dump({'status': 'passed', 'attempt': '0'}, open(out + '/result.json', 'w'))\n" + ) + current = manifest(tmp_path, script=script) + seen = [] + monkeypatch.setattr(run, "validate_result", lambda *args, **kwargs: seen.append(args)) + status = run.execute(tmp_path, current) + assert status["status"] == "passed" and len(seen) == 1 + assert json.loads((tmp_path / "result.json").read_text())["status"] == "passed" + assert not (tmp_path / "RUNNING").exists() + + +def test_execute_keeps_the_stage_failure_kind(tmp_path, frozen): + script = ( + "import json, sys\n" + "out = sys.argv[sys.argv.index('--output') + 1]\n" + "json.dump({'failure_kind': 'insufficient_disk', 'error': 'full'}," + " open(out + '/result.json', 'w'))\n" + "sys.exit(3)\n" + ) + status = run.execute(tmp_path, manifest(tmp_path, script=script)) + assert status["status"] == "failed" and status["exit_code"] == 3 + assert status["failure_kind"] == "insufficient_disk" + assert not (tmp_path / "result.json").exists() + assert not (tmp_path / "RUNNING").exists() + + +def test_execute_refuses_a_changed_source(tmp_path, monkeypatch, frozen): + monkeypatch.setattr(run, "_git", lambda *args, cwd=None: "other-tree") + with pytest.raises(RuntimeError, match="source_mismatch"): + run.execute(tmp_path, manifest(tmp_path, script="raise SystemExit(0)\n")) + + +@pytest.mark.parametrize( + "arguments,message", + [ + (["--stage", "all", "--from", "layers"], "needs --previous"), + (["--stage", "l2"], "needs --previous"), + (["--stage", "preflight", "--previous", "."], "needs --previous"), + (["--stage", "all", "--from", "train", "--until", "l2", "--previous", "."], "--from"), + ], +) +def test_stage_selection_requires_a_predecessor_exactly_after_preflight( + monkeypatch, tmp_path, arguments, message +): + monkeypatch.setattr( + sys, + "argv", + ["run", "--runs-root", str(tmp_path), "--checkpoint", ".", "--cuda-home", ".", *arguments], + ) + with pytest.raises(SystemExit, match=message): + run.main() diff --git a/tests/test_qwen3_next_storage.py b/tests/test_qwen3_next_storage.py new file mode 100644 index 000000000..024453a43 --- /dev/null +++ b/tests/test_qwen3_next_storage.py @@ -0,0 +1,334 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Faults at persistent filesystem boundaries, including an actual SIGKILL.""" + +import json +import os +import signal +import subprocess +import sys +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) +import qwen3_next_runtime as storage # noqa: E402 +from qwen3_next_checkpoint import ( # noqa: E402 + newest_complete_checkpoint, + publish_checkpoint, + validate_checkpoint, + write_rank_manifest, +) + +IDENTITY = {"run_id": "storage-fault-test"} + + +class Preempted(BaseException): + pass + + +def fail_at(monkeypatch, boundary): + def crash(name): + if name == boundary: + raise Preempted(name) + + monkeypatch.setattr(storage, "_boundary", crash) + + +def metadata(): + return {"identity": IDENTITY, "status": "staging", "step": 1} + + +def complete(root, name="step-2", step=2): + staging, destination = storage.create_staging(root, name, metadata()) + for rank in range(4): + files = {} + for role in ("model", "optimizer", "rng", "progress"): + relative = f"rank-{rank}-{role}.bin" + (staging / relative).write_bytes(f"{rank}:{role}".encode()) + files[role] = [relative] + write_rank_manifest( + staging, + rank=rank, + world_size=4, + identity=IDENTITY, + step=step, + sample_position=step * 4, + files=files, + ) + marker = publish_checkpoint(staging, destination, world_size=4, identity=IDENTITY) + return destination, marker + + +@pytest.mark.parametrize("boundary", ["root_owner_persisted", "root_created"]) +def test_root_creation_recovers_without_internal_owner(monkeypatch, tmp_path, boundary): + root = tmp_path / "checkpoints" + fail_at(monkeypatch, boundary) + with pytest.raises(Preempted): + storage.owned_root(root, IDENTITY) + monkeypatch.setattr(storage, "_boundary", lambda name: None) + storage.owned_root(root, IDENTITY) + assert json.loads((root / "RUN_OWNER.json").read_text()) == IDENTITY + storage.recover_storage(root, IDENTITY) + + +@pytest.mark.parametrize( + "boundary", ["staging_intent_persisted", "staging_created", "staging_marker_persisted"] +) +def test_staging_creation_recovers_every_window_without_loading_partial( + monkeypatch, tmp_path, boundary +): + root = storage.owned_root(tmp_path / "checkpoints", IDENTITY) + retained, marker = complete(root) + fail_at(monkeypatch, boundary) + with pytest.raises(Preempted): + storage.create_staging(root, "next-step", metadata()) + monkeypatch.setattr(storage, "_boundary", lambda name: None) + with pytest.raises((ValueError, FileNotFoundError)): + validate_checkpoint(root / "next-step.partial", IDENTITY) + storage.recover_storage(root, IDENTITY) + assert not (root / "next-step.partial").exists() + assert validate_checkpoint(retained, IDENTITY) == marker + assert newest_complete_checkpoint(root, IDENTITY)["checkpoint"] == str(retained) + assert storage.recover_storage(root, IDENTITY) == [] + + +@pytest.mark.parametrize( + "boundary", + [ + "retirement_intent_persisted", + "retirement_renamed", + "retirement_removed", + "retirement_committed", + ], +) +def test_retirement_resumes_each_window_and_preserves_successor(monkeypatch, tmp_path, boundary): + root = storage.owned_root(tmp_path / "checkpoints", IDENTITY) + prior, old_marker = complete(root, "step-1", 1) + retained, marker = complete(root) + proof = {"complete": old_marker, "successor": str(retained), "successor_complete": marker} + fail_at(monkeypatch, boundary) + with pytest.raises(Preempted): + storage.retire_directory(prior, root, IDENTITY, proof=proof) + monkeypatch.setattr(storage, "_boundary", lambda name: None) + with pytest.raises(ValueError, match="Unpublished"): + validate_checkpoint(root / ".step-1.deleting", IDENTITY) + storage.recover_storage(root, IDENTITY) + assert not prior.exists() and not (root / ".step-1.deleting").exists() + assert validate_checkpoint(retained, IDENTITY) == marker + assert storage.recover_storage(root, IDENTITY) == [] + + +def test_preemption_after_internal_marker_deleted_does_not_strand_cleanup(monkeypatch, tmp_path): + root = storage.owned_root(tmp_path / "exports", IDENTITY) + stage, _ = storage.create_staging(root, "update-1", metadata()) + (stage / "unfinished-weights").write_bytes(b"never load this") + remove = storage.shutil.rmtree + + def interrupted(path): + (path / "STAGING.json").unlink() + raise Preempted("during rmtree") + + monkeypatch.setattr(storage.shutil, "rmtree", interrupted) + with pytest.raises(Preempted): + storage.recover_storage(root, IDENTITY) + assert not stage.exists() + assert (root / ".update-1.partial.deleting" / "unfinished-weights").is_file() + monkeypatch.setattr(storage.shutil, "rmtree", remove) + storage.recover_storage(root, IDENTITY) + assert not (root / ".update-1.partial.deleting").exists() + + +def test_pending_retirement_rechecks_successor_before_deleting_last_good_state( + monkeypatch, tmp_path +): + root = storage.owned_root(tmp_path / "checkpoints", IDENTITY) + prior, marker = complete(root, "step-1", 1) + successor, next_marker = complete(root) + fail_at(monkeypatch, "retirement_intent_persisted") + with pytest.raises(Preempted): + storage.retire_directory( + prior, + root, + IDENTITY, + proof={ + "complete": marker, + "successor": str(successor), + "successor_complete": next_marker, + }, + ) + monkeypatch.setattr(storage, "_boundary", lambda name: None) + (successor / "rank-0-model.bin").write_bytes(b"corrupt") + with pytest.raises(ValueError, match="checksum|size mismatch"): + storage.recover_storage(root, IDENTITY) + assert validate_checkpoint(prior, IDENTITY) == marker + + +@pytest.mark.parametrize("foreign", ["missing_owner", "wrong_owner", "symlink"]) +def test_unrelated_partial_directories_are_never_deleted(tmp_path, foreign): + root = storage.owned_root(tmp_path / "exports", IDENTITY) + target = root / "unrelated.partial" + if foreign == "symlink": + outside = tmp_path / "outside" + outside.mkdir() + (outside / "STAGING.json").write_text(json.dumps(metadata())) + target.symlink_to(outside, target_is_directory=True) + else: + target.mkdir() + if foreign == "wrong_owner": + (target / "STAGING.json").write_text( + json.dumps({"status": "staging", "identity": {"run_id": "other"}}) + ) + with pytest.raises(ValueError): + storage.recover_storage(root, IDENTITY) + assert target.exists() + + +def test_real_sigkill_between_mkdir_and_internal_marker_is_recoverable(tmp_path): + root = tmp_path / "exports" + script = """ +import os, signal, sys +import qwen3_next_runtime as storage +identity = {'run_id': 'storage-fault-test'} +root = storage.owned_root(sys.argv[1], identity) +def stop(name): + if name == 'staging_created': + os.kill(os.getpid(), signal.SIGKILL) +storage._boundary = stop +storage.create_staging( + root, 'crashed-export', {'identity': identity, 'status': 'staging', 'step': 1} +) +""" + environment = {**os.environ, "PYTHONPATH": str(Path(storage.__file__).parent)} + child = subprocess.run([sys.executable, "-c", script, str(root)], env=environment, timeout=30) + assert child.returncode == -signal.SIGKILL + assert (root / "crashed-export.partial").is_dir() + assert not (root / "crashed-export.partial" / "STAGING.json").exists() + storage.owned_root(root, IDENTITY) + storage.recover_storage(root, IDENTITY) + assert not (root / "crashed-export.partial").exists() + + +@pytest.mark.parametrize("changed", ["identity", "target", "filename", "published", "status"]) +def test_malformed_parent_staging_intent_never_deletes_its_target(tmp_path, changed): + root = storage.owned_root(tmp_path / "exports", IDENTITY) + staging, _ = storage.create_staging(root, "unfinished", metadata()) + intent = root / ".unfinished.staging.json" + record = json.loads(intent.read_text()) + if changed == "identity": + record["identity"] = {"run_id": "another"} + elif changed == "target": + record["staging"] = "../outside.partial" + elif changed == "filename": + intent.rename(root / ".wrong-name.staging.json") + intent = root / ".wrong-name.staging.json" + elif changed == "published": + record["published"] = ".unfinished" + record["staging"] = ".unfinished.partial" + else: + record["metadata"]["status"] = "complete" + intent.write_text(json.dumps(record)) + with pytest.raises(ValueError): + storage.recover_storage(root, IDENTITY) + assert staging.is_dir() + + +def test_replaced_target_marker_stops_pending_retirement(monkeypatch, tmp_path): + root = storage.owned_root(tmp_path / "checkpoints", IDENTITY) + prior, previous = complete(root, "step-1", 1) + successor, current = complete(root) + fail_at(monkeypatch, "retirement_intent_persisted") + with pytest.raises(Preempted): + storage.retire_directory( + prior, + root, + IDENTITY, + proof={ + "complete": previous, + "successor": str(successor), + "successor_complete": current, + }, + ) + monkeypatch.setattr(storage, "_boundary", lambda name: None) + (prior / "COMPLETE.json").write_text(json.dumps({**previous, "step": 99})) + with pytest.raises(ValueError, match="differs from its durable ownership"): + storage.recover_storage(root, IDENTITY) + assert prior.is_dir() + + +@pytest.mark.parametrize("proof", [{}, {"status": "staging", "identity": IDENTITY}]) +def test_unsupported_proof_cannot_retire_unrelated_published_directory(tmp_path, proof): + root = storage.owned_root(tmp_path / "checkpoints", IDENTITY) + unrelated = root / "unrelated" + unrelated.mkdir() + with pytest.raises(ValueError): + storage.retire_directory(unrelated, root, IDENTITY, proof=proof) + assert unrelated.is_dir() + assert not (root / ".unrelated.delete.json").exists() + + +@pytest.mark.parametrize("record_name", ["parent_owner", "root_owner", "intent", "marker"]) +def test_interrupted_atomic_creation_record_does_not_strand_recovery( + monkeypatch, tmp_path, record_name +): + root = tmp_path / "exports" + atomic = storage.atomic_json + targets = { + "parent_owner": root.parent / ".exports.owner.json", + "root_owner": root / "RUN_OWNER.json", + "intent": root / ".unfinished.staging.json", + "marker": root / "unfinished.partial" / "STAGING.json", + } + + def interrupted(path, value): + if path == targets[record_name]: + path.with_suffix(path.suffix + ".partial").write_text('{"interrupted":') + raise Preempted("during atomic record write") + atomic(path, value) + + monkeypatch.setattr(storage, "atomic_json", interrupted) + with pytest.raises(Preempted): + storage.owned_root(root, IDENTITY) + storage.create_staging(root, "unfinished", metadata()) + monkeypatch.setattr(storage, "atomic_json", atomic) + storage.owned_root(root, IDENTITY) + storage.recover_storage(root, IDENTITY) + assert not (root / "unfinished.partial").exists() + assert storage.recover_storage(root, IDENTITY) == [] + + +@pytest.mark.parametrize("status", ["retiring", "retired"]) +def test_interrupted_atomic_retirement_record_is_recoverable(monkeypatch, tmp_path, status): + root = storage.owned_root(tmp_path / "exports", IDENTITY) + staging, _ = storage.create_staging(root, "unfinished", metadata()) + atomic = storage.atomic_json + + def interrupted(path, value): + if path.name.endswith(".delete.json") and value["status"] == status: + path.with_suffix(path.suffix + ".partial").write_text('{"interrupted":') + raise Preempted("during atomic retirement record") + atomic(path, value) + + monkeypatch.setattr(storage, "atomic_json", interrupted) + with pytest.raises(Preempted): + storage.recover_storage(root, IDENTITY) + monkeypatch.setattr(storage, "atomic_json", atomic) + storage.recover_storage(root, IDENTITY) + assert not staging.exists() + assert not (root / ".unfinished.partial.deleting").exists() + assert storage.recover_storage(root, IDENTITY) == [] + + +@pytest.mark.parametrize("name", ["foo.a", "foo.b", "foo.partial", ".hidden"]) +def test_published_names_cannot_alias_one_staging_directory(tmp_path, name): + root = storage.owned_root(tmp_path / "exports", IDENTITY) + with pytest.raises(ValueError, match="visible and have no suffix"): + storage.create_staging(root, name, metadata()) + assert list(root.iterdir()) == [root / "RUN_OWNER.json"] + + +def test_training_storage_root_is_the_declared_root_or_inside_the_run(tmp_path): + assert storage.training_storage_root({}, tmp_path) == tmp_path / "training-state" + shared = tmp_path / "shared" / "run-1" + assert storage.training_storage_root({"training_storage": str(shared)}, tmp_path) == shared diff --git a/tests/test_qwen3_next_subnode.py b/tests/test_qwen3_next_subnode.py new file mode 100644 index 000000000..672d18717 --- /dev/null +++ b/tests/test_qwen3_next_subnode.py @@ -0,0 +1,52 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""The sub-node recorder used for first-drift localisation inside a decoder layer.""" + +from types import SimpleNamespace + +import pytest +import torch + +from rl_engine.integrations import qwen3_next_subnode as subnode + + +def test_buffer_requires_each_sub_node_once_with_its_original_storage(): + buffer = subnode.SubnodeBuffer(batch=2) + row = torch.arange(2048, dtype=torch.float32).to(torch.bfloat16) + buffer.put(3, "mixer", 1, 15, row) + assert torch.equal(buffer.values["mixer"][3, 1, 15], row) + with pytest.raises(ValueError, match="twice"): + buffer.put(3, "mixer", 1, 15, row) + with pytest.raises(ValueError, match="int64"): + buffer.put(3, "router", 1, 15, torch.zeros(10, dtype=torch.int32)) + with pytest.raises(ValueError, match="bfloat16"): + buffer.put(4, "mlp", 0, 0, row.float()) + with pytest.raises(ValueError, match="not all observed"): + buffer.layer(3) + with pytest.raises(ValueError, match="Unknown"): + subnode.subnode_layout("gate") + + +def test_complete_layer_and_embedding_are_returned_in_order(): + buffer = subnode.SubnodeBuffer(batch=1) + for name in subnode.SUBNODES: + width, dtype = subnode.subnode_layout(name) + buffer.put_rows(0, name, torch.zeros(1, 16, width, dtype=dtype)) + assert tuple(buffer.layer(0)) == subnode.SUBNODES + with pytest.raises(ValueError, match="every response position"): + buffer.embedding_rows() + for response in range(16): + buffer.put_embedding(0, response, torch.zeros(2048, dtype=torch.bfloat16)) + assert buffer.embedding_rows().shape == (1, 16, 2048) + + +def test_install_binds_the_layer_index_and_remove_clears_every_hook(): + layers = [SimpleNamespace(mlp=SimpleNamespace()) for _ in range(3)] + seen = [] + subnode.install(layers, subnode.vllm_modules, lambda *args: seen.append(args)) + layers[2].subnode_hook("mixer", "rows") + layers[1].mlp.subnode_hook("router", "ids") + assert seen == [(2, "mixer", "rows"), (1, "router", "ids")] + subnode.remove(layers, subnode.vllm_modules) + assert all(layer.subnode_hook is None and layer.mlp.subnode_hook is None for layer in layers) diff --git a/tests/test_qwen3_next_training_audit.py b/tests/test_qwen3_next_training_audit.py new file mode 100644 index 000000000..b08508fa6 --- /dev/null +++ b/tests/test_qwen3_next_training_audit.py @@ -0,0 +1,137 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""CPU regression of observations; these are not native model training acceptance.""" + +import random + +import numpy as np +import pytest +import torch + +from rl_engine.integrations.qwen3_next_training import ( + capture_rng_state, + gradient_evidence, + observe_native_optimizer_step, + restore_rng_state, + state_digest, + tensor_digest, +) + + +def test_state_identity_preserves_bits_dtype_structure_and_key_order(): + assert tensor_digest(torch.tensor(0.0)) != tensor_digest(torch.tensor(-0.0)) + assert tensor_digest(torch.ones(2)) != tensor_digest(torch.ones(2, dtype=torch.bfloat16)) + assert state_digest({"a": [1], "b": 2}) == state_digest({"b": 2, "a": [1]}) + assert state_digest([1, 2]) != state_digest((1, 2)) + assert state_digest({1: "a"}) != state_digest({"1": "a"}) + value = torch.arange(12).reshape(3, 4).T + assert tensor_digest(value, chunk_bytes=3) == tensor_digest(value.contiguous()) + + +@pytest.mark.parametrize("value", [torch.tensor(float("nan")), float("inf"), object()]) +def test_state_identity_rejects_unknown_or_nonfinite_state(value): + with pytest.raises((ValueError, TypeError)): + state_digest(value) + + +def test_rng_restores_python_numpy_torch_exactly(): + state = capture_rng_state() + first = (random.random(), np.random.random(), torch.rand(4)) + restore_rng_state(state) + second = (random.random(), np.random.random(), torch.rand(4)) + assert first[:2] == second[:2] + assert torch.equal(first[2], second[2]) + + +def test_gradients_observe_main_grad_and_allow_inactive_expert(): + active, inactive = torch.nn.Parameter(torch.ones(2)), torch.nn.Parameter(torch.ones(2)) + active.main_grad = torch.ones(2) + inactive.grad = torch.zeros(2) + evidence = gradient_evidence( + {"layer.router": active, "layer.expert42": inactive}, + required_groups={"routing": ["router"]}, + ) + assert evidence["active_groups"] == {"routing": ["layer.router"]} + assert not evidence["parameters"]["layer.expert42"]["nonzero"] + with pytest.raises(ValueError, match="Active paths"): + gradient_evidence({"expert": inactive}, required_groups={"experts": ["expert"]}) + inactive.grad[0] = float("nan") + with pytest.raises(ValueError, match="nonfinite"): + gradient_evidence({"expert": inactive}, required_groups={"experts": ["expert"]}) + + +class _Optimizer: + """Test double only: exercise observer timing and cleanup, never acceptance.""" + + def __init__(self, parameter, change=True): + self.parameter, self.change = parameter, change + + def step(self): + if self.change: + with torch.no_grad(): + self.parameter.add_(1) + self.parameter.grad = None + return True, 1.0, 0 + + +def test_observer_captures_before_native_clear_and_restores_method(): + model = torch.nn.Linear(2, 1, bias=False) + model.weight.grad = torch.ones_like(model.weight) + optimizer, evidence = _Optimizer(model.weight), {} + with observe_native_optimizer_step( + optimizer, [model], required_groups={"output": ["weight"]}, evidence=evidence + ): + optimizer.step() + assert model.weight.grad is None + assert "step" not in vars(optimizer) + assert evidence["changed_parameters"] == ["chunk0.weight"] + assert evidence["gradients"]["parameters"]["chunk0.weight"]["nonzero"] + + +@pytest.mark.parametrize("mode", ["no_call", "no_change", "double_call"]) +def test_observer_rejects_incomplete_or_fake_update(mode): + model = torch.nn.Linear(2, 1, bias=False) + model.weight.grad = torch.ones_like(model.weight) + optimizer = _Optimizer(model.weight, change=mode != "no_change") + with pytest.raises(ValueError): + with observe_native_optimizer_step( + optimizer, [model], required_groups={"output": ["weight"]}, evidence={} + ): + if mode != "no_call": + optimizer.step() + if mode == "double_call": + optimizer.step() + assert "step" not in vars(optimizer) + + +def test_gradient_nonzero_is_judged_across_tensor_parallel_ranks(): + """A vocab-parallel shard with no tokens is all-zero locally yet trains as a whole.""" + embedding, router = torch.nn.Parameter(torch.ones(4, 2)), torch.nn.Parameter(torch.ones(2)) + embedding.main_grad = torch.zeros(4, 2) + router.main_grad = torch.ones(2) + groups = {"embedding": ["embedding"], "router": ["router"]} + + def other_rank_saw_tokens(flags): + return [True for _ in flags] + + evidence = gradient_evidence( + {"chunk0.embedding.word_embeddings.weight": embedding, "layer.router": router}, + required_groups=groups, + reduce_flags=other_rank_saw_tokens, + ) + record = evidence["parameters"]["chunk0.embedding.word_embeddings.weight"] + assert record["nonzero"] is True and record["nonzero_local"] is False + assert evidence["active_groups"]["embedding"] == ["chunk0.embedding.word_embeddings.weight"] + with pytest.raises(ValueError, match="Active paths.*embedding"): + gradient_evidence( + {"chunk0.embedding.word_embeddings.weight": embedding, "layer.router": router}, + required_groups=groups, + reduce_flags=lambda flags: list(flags), + ) + with pytest.raises(ValueError, match="one per parameter"): + gradient_evidence( + {"layer.router": router}, + required_groups={"router": ["router"]}, + reduce_flags=lambda flags: [], + ) diff --git a/tests/test_qwen3_next_training_checkpoint.py b/tests/test_qwen3_next_training_checkpoint.py new file mode 100644 index 000000000..884b4e42a --- /dev/null +++ b/tests/test_qwen3_next_training_checkpoint.py @@ -0,0 +1,539 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""CPU serialization/protocol regressions; these do not certify native GPU training.""" + +import json +import random +import sys +from datetime import timedelta +from pathlib import Path +from types import SimpleNamespace + +import numpy as np +import pytest +import torch +import torch.distributed as dist +import torch.multiprocessing as mp + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) +import qwen3_next_optimizer_checkpoint as optimizer_checkpoint # noqa: E402 +import qwen3_next_training_checkpoint as checkpoint # noqa: E402 +from qwen3_next_checkpoint import publish_checkpoint # noqa: E402 + +from rl_engine.integrations.qwen3_next_training import capture_rng_state, state_digest # noqa: E402 +from rl_engine.testing.qwen3_next_rl_fixture import PolicyFixture # noqa: E402 + +IDENTITY = {"source": "pinned", "provider": "strict", "checkpoint": "official", "tp": 4} + + +class ProtocolModel(torch.nn.Module): + def __init__(self): + super().__init__() + self.weight = torch.nn.Parameter(torch.arange(8, dtype=torch.bfloat16).reshape(2, 4)) + self.register_buffer("counter", torch.tensor(12, dtype=torch.int64)) + + def state_dict_for_save_checkpoint(self): + return self.state_dict() + + +class ProtocolOptimizer: + """Tiny CPU stand-in only for exercising serialization and corruption paths.""" + + def __init__(self, model): + self.model = model + self.calls = [] + self.model_param_group_index_map = {model.weight: (0, 0)} + self.gbuf_ranges = [ + { + (torch.bfloat16, torch.float32): [ + { + "param_map": { + model.weight: { + "param": SimpleNamespace(start=0, end=8), + "gbuf_local": SimpleNamespace(start=0, end=8), + } + } + } + ] + } + ] + self.buffers = [SimpleNamespace(buckets=[SimpleNamespace(grad_data=torch.empty(8))])] + self.optimizer = SimpleNamespace( + cpu_optimizer_cls=torch.optim.AdamW, gpu_optimizer_cls=None + ) + self.states = { + "param": model.weight.detach().float().clone(), + "exp_avg": torch.full_like(model.weight, 0.125, dtype=torch.float32), + "exp_avg_sq": torch.full_like(model.weight, 0.0625, dtype=torch.float32), + "step": torch.tensor(2.0), + } + self.step = 2 + self.corrupt_load = False + + def _get_main_param_and_optimizer_states(self, parameter): + assert parameter is self.model.weight + return self.states + + def state_dict(self): + return {"step": self.step, "cpu_optimizer_cls": torch.optim.AdamW} + + def load_state_dict(self, header): + self.calls.append("header") + assert header["cpu_optimizer_cls"] is torch.optim.AdamW + self.step = header["step"] + # Emulate native HybridDeviceOptimizer's RNG-consuming dummy initialization. + with torch.no_grad(): + self.model.weight.copy_(torch.randn_like(self.model.weight)) + + def save_parameter_state(self, path): + raise AssertionError("Whole-bucket packing must never be called") + + def load_parameter_state(self, path): + raise AssertionError("Whole-bucket unpacking must never be called") + + def _set_main_param_and_optimizer_states(self, parameter, states): + assert parameter is self.model.weight + self.calls.append("native_set") + assert states is self.states + if self.corrupt_load: + self.states["exp_avg"][0, 0] = -0.125 + + +class ProtocolScheduler: + def __init__(self): + self.num_steps = 8 + + def state_dict(self): + return {"num_steps": self.num_steps, "lr": 1e-6} + + def load_state_dict(self, state): + self.num_steps = state["num_steps"] + + +@pytest.fixture +def runtime(monkeypatch): + monkeypatch.setattr(torch.cuda, "synchronize", lambda: None) + monkeypatch.setattr(torch.cuda, "get_rng_state_all", lambda: []) + monkeypatch.setattr(torch.cuda, "set_rng_state_all", lambda values: None) + monkeypatch.setattr(torch.cuda, "device_count", lambda: 0) + monkeypatch.setattr(checkpoint.dist, "get_rank", lambda: 0) + model = ProtocolModel() + optimizer = ProtocolOptimizer(model) + scheduler = ProtocolScheduler() + fixture = PolicyFixture.freeze( + torch.arange(320, dtype=torch.int64).reshape(4, 80), + torch.full((4, 16), -2.0, dtype=torch.float32), + baseline_identity="real-worker-baseline-required-in-production", + ) + return [model], optimizer, scheduler, fixture + + +def save_rank(tmp_path, runtime): + model, optimizer, scheduler, fixture = runtime + staging = tmp_path / "step-2.partial" + staging.mkdir() + record = checkpoint._save_rank( + model, + optimizer, + scheduler, + fixture, + staging, + leaves=[optimizer], + identity=IDENTITY, + step=2, + sample_position=8, + tracker=None, + native_args=SimpleNamespace(consumed_train_samples=8), + update_evidence={"native_step": 2, "gradient_norm": 1.0}, + ) + return staging, record + + +def load_rank(staging, runtime): + model, optimizer, scheduler, fixture = runtime + return checkpoint._load_rank( + model, + optimizer, + scheduler, + fixture, + staging, + leaves=[optimizer], + identity=IDENTITY, + complete={"step": 2, "sample_position": 8}, + tracker=None, + native_args=SimpleNamespace(consumed_train_samples=0), + ) + + +def test_bounded_load_calls_hdo_sync(tmp_path, runtime, monkeypatch): + pytest.importorskip("megatron") + staging, _ = save_rank(tmp_path, runtime) + model, optimizer, _, _ = runtime + calls = [] + + class FakeHDO: + def _sync_hdo_state_to_sub_optimizers(self): + calls.append("sync") + + optimizer.optimizer = FakeHDO() + monkeypatch.setattr( + "megatron.core.optimizer.cpu_offloading.hybrid_optimizer.HybridDeviceOptimizer", + FakeHDO, + ) + optimizer_checkpoint.load_bounded_parameter_state(model, [optimizer], staging / "rank-0") + assert calls == ["sync"] + + +def test_load_rank_fails_when_hdo_sync_skipped(tmp_path, runtime, monkeypatch): + staging, _ = save_rank(tmp_path, runtime) + model, optimizer, scheduler, fixture = runtime + + def load_without_sync(model, leaves, rank_dir): + rank_dir = Path(rank_dir) + touched = [] + index = json.loads((rank_dir / "optimizer-index.json").read_text()) + current = optimizer_checkpoint.native_parameter_layout(model, leaves) + extras = torch.load(rank_dir / "optimizer-extra.pt", map_location="cpu", weights_only=True) + with torch.no_grad(): + for name, record in current.items(): + entry = index["parameters"][name] + states = optimizer_checkpoint.native_parameter_states(record) + for block in entry["blocks"]: + start, end = block["start"], block["end"] + payload = torch.load( + rank_dir / block["path"], map_location="cpu", weights_only=True + ) + for key in optimizer_checkpoint.PARAMETER_KEYS: + states[key].reshape(-1)[start:end].copy_(payload[key]) + for key, value in extras[name].items(): + if isinstance(states[key], torch.Tensor): + states[key].copy_(value) + else: + states[key] = value + record["leaf"]._set_main_param_and_optimizer_states(record["parameter"], states) + if record["leaf"] not in touched: + touched.append(record["leaf"]) + + monkeypatch.setattr(checkpoint, "load_bounded_parameter_state", load_without_sync) + monkeypatch.setattr( + checkpoint, + "verify_hdo_optimizer_master_aliases", + lambda model, leaves: (_ for _ in ()).throw( + ValueError("HDO FP32 master copy diverged from checkpoint-visible master weights") + ), + ) + with pytest.raises(ValueError, match="FP32 master copy diverged"): + checkpoint._load_rank( + model, + optimizer, + scheduler, + fixture, + staging, + leaves=[optimizer], + identity=IDENTITY, + complete={"step": 2, "sample_position": 8}, + tracker=None, + native_args=SimpleNamespace(consumed_train_samples=0), + ) + + +def test_rank_roundtrip_uses_parameter_state_and_restores_rng_last(tmp_path, runtime): + model, optimizer, scheduler, _ = runtime + original_rng = state_digest(capture_rng_state()) + original_model = state_digest(model[0].state_dict()) + staging, record = save_rank(tmp_path, runtime) + with torch.no_grad(): + model[0].weight.fill_(-3) + model[0].counter.fill_(42) + optimizer.states["param"].fill_(5) + optimizer.states["exp_avg"].fill_(0) + optimizer.step = 100 + scheduler.num_steps = 400 + random.random() + np.random.rand() + torch.rand(3) + assert load_rank(staging, runtime) == record + assert optimizer.calls == ["header", "native_set"] + assert optimizer.step == 2 and scheduler.num_steps == 8 + assert state_digest(model[0].state_dict()) == original_model + assert state_digest(capture_rng_state()) == original_rng + + +def test_all_small_state_files_are_weights_only_safe(tmp_path, runtime): + staging, _ = save_rank(tmp_path, runtime) + for path in (staging / "rank-0").glob("*.pt"): + assert torch.load(path, map_location="cpu", weights_only=True) is not None + + +def test_native_parameter_load_corruption_is_rejected(tmp_path, runtime): + staging, _ = save_rank(tmp_path, runtime) + runtime[1].corrupt_load = True + with pytest.raises(ValueError, match="master weights or Adam moments"): + load_rank(staging, runtime) + + +def test_header_only_optimizer_is_not_a_complete_checkpoint(runtime): + del runtime[1].states["exp_avg_sq"] + with pytest.raises(ValueError, match="Adam moments are missing"): + checkpoint._optimizer_parameter_records(runtime[0], [runtime[1]]) + + +def test_optimizer_must_cover_each_model_parameter_exactly_once(runtime): + model, optimizer, _, _ = runtime + with pytest.raises(ValueError, match="partition model parameters"): + checkpoint._optimizer_parameter_records(model, [optimizer, optimizer]) + optimizer.model_param_group_index_map = {} + with pytest.raises(ValueError, match="absent from optimizer groups"): + checkpoint._optimizer_parameter_records(model, [optimizer]) + + +def test_optimizer_master_dtype_is_never_silently_cast(runtime): + runtime[1].states["param"] = runtime[1].states["param"].bfloat16() + with pytest.raises(ValueError, match="Invalid fully offloaded native param"): + checkpoint._optimizer_parameter_records(runtime[0], [runtime[1]]) + + +def test_native_class_header_uses_only_running_optimizer_allowlist(runtime): + allowed = checkpoint._native_class_allowlist([runtime[1]]) + header = {"group": {"cls": torch.optim.AdamW, "options": (1, True)}} + encoded = checkpoint._encode_header(header, allowed) + assert checkpoint._decode_header(encoded, allowed) == header + with pytest.raises(TypeError, match="Unpinned class"): + checkpoint._encode_header({"cls": ProtocolOptimizer}, allowed) + with pytest.raises(ValueError, match="absent from the running"): + checkpoint._decode_header({checkpoint.CLASS_TAG: "untrusted.arbitrary.Class"}, allowed) + + +def test_saved_fixture_identity_cannot_change(tmp_path, runtime): + staging, _ = save_rank(tmp_path, runtime) + runtime[3].tokens[0, 0] += 1 + with pytest.raises(ValueError, match="Frozen policy fixture"): + checkpoint.load_training_checkpoint( + None, *runtime, staging, identity=IDENTITY, tracker=None + ) + + +def test_model_schema_rejects_dtype_change(tmp_path, runtime): + staging, record = save_rank(tmp_path, runtime) + path = staging / "rank-0/model-0.pt" + value = torch.load(path, weights_only=True) + value["weight"] = value["weight"].float() + torch.save(value, path) + record["model_sha256"][0] = state_digest(value) + (staging / "rank-0/progress.json").write_text(json.dumps(record)) + with pytest.raises(ValueError, match="dtypes, shapes or state keys"): + load_rank(staging, runtime) + + +def test_numpy_rng_roundtrip_preserves_all_bits(runtime): + state = capture_rng_state() + assert state_digest(checkpoint._decode_rng(checkpoint._encode_rng(state))) == state_digest( + state + ) + + +def test_shared_storage_disk_estimate_counts_padding_once(): + buffer = torch.arange(1024, dtype=torch.bfloat16) + assert checkpoint._tensor_bytes({"one": buffer[:8], "two": buffer[8:16]}) == 2048 + + +def test_bounded_blocks_do_not_serialize_underlying_bucket(tmp_path, runtime, monkeypatch): + import qwen3_next_optimizer_checkpoint as bounded + + monkeypatch.setattr(bounded, "BLOCK_BYTES", 48) + optimizer = runtime[1] + for key in bounded.PARAMETER_KEYS: + backing = torch.arange(1024 * 1024, dtype=torch.float32) + optimizer.states[key] = backing[100:108].view(2, 4) + staging, _ = save_rank(tmp_path, runtime) + blocks = sorted((staging / "rank-0/optimizer-state").glob("*.pt")) + assert len(blocks) == 2 + for path in blocks: + assert path.stat().st_size < 4096 + payload = torch.load(path, weights_only=True) + assert sum(value.numel() * value.element_size() for value in payload.values()) == 48 + load_rank(staging, runtime) + + +@pytest.mark.parametrize("change", ["gap", "overlap", "truncated"]) +def test_bounded_block_ranges_must_cover_parameter_once(tmp_path, runtime, monkeypatch, change): + import qwen3_next_optimizer_checkpoint as bounded + + monkeypatch.setattr(bounded, "BLOCK_BYTES", 48) + staging, _ = save_rank(tmp_path, runtime) + path = staging / "rank-0/optimizer-index.json" + index = json.loads(path.read_text()) + blocks = index["parameters"]["chunk0.weight"]["blocks"] + if change == "gap": + blocks[1]["start"] += 1 + elif change == "overlap": + blocks[1]["start"] -= 1 + else: + blocks.pop() + path.write_text(json.dumps(index)) + with pytest.raises(ValueError, match="cover each parameter exactly once|complete parameter"): + load_rank(staging, runtime) + + +def test_native_dp_range_cannot_describe_partial_parameter(runtime): + optimizer = runtime[1] + ranges = next(iter(optimizer.gbuf_ranges[0].values()))[0]["param_map"][runtime[0][0].weight] + ranges["param"].start = 1 + with pytest.raises(ValueError, match="cover each complete parameter"): + checkpoint._optimizer_parameter_records(runtime[0], [optimizer]) + + +def test_state_identity_includes_native_consumed_samples_and_rng(runtime, monkeypatch): + monkeypatch.setattr(checkpoint, "_native_leaves", lambda *args: [runtime[1]]) + args = SimpleNamespace(consumed_train_samples=8) + first = checkpoint.training_state_identity( + args, *runtime, step=2, sample_position=8, identity=IDENTITY, tracker=None + ) + args.consumed_train_samples = 12 + second = checkpoint.training_state_identity( + args, *runtime, step=2, sample_position=8, identity=IDENTITY, tracker=None + ) + assert first["state_sha256"] != second["state_sha256"] + args.consumed_train_samples = 8 + torch.rand(1) + third = checkpoint.training_state_identity( + args, *runtime, step=2, sample_position=8, identity=IDENTITY, tracker=None + ) + assert first["rng_sha256"] != third["rng_sha256"] + + +def test_native_required_files_are_bound_to_checked_manifest(tmp_path, runtime, monkeypatch): + model, optimizer, scheduler, fixture = runtime + staging = tmp_path / "all.partial" + staging.mkdir() + for rank in range(4): + monkeypatch.setattr(checkpoint.dist, "get_rank", lambda rank=rank: rank) + checkpoint._save_rank( + model, + optimizer, + scheduler, + fixture, + staging, + leaves=[optimizer], + identity=IDENTITY, + step=2, + sample_position=8, + tracker=None, + native_args=SimpleNamespace(consumed_train_samples=8), + update_evidence={"native_step": 2, "gradient_norm": 1.0}, + ) + complete = tmp_path / "all" + publish_checkpoint(staging, complete, world_size=4, identity=IDENTITY) + assert checkpoint._validate_native_checkpoint(complete, IDENTITY, 1)["step"] == 2 + with pytest.raises(ValueError, match="omits or substitutes"): + checkpoint._validate_native_checkpoint(complete, IDENTITY, 2) + (complete / "rank-3/optimizer-state/p00000-b00000.pt").write_bytes(b"corrupt") + monkeypatch.setattr(checkpoint.dist, "get_rank", lambda: 0) + monkeypatch.setattr(checkpoint, "_all_rank_call", lambda action, label: action()) + monkeypatch.setattr(checkpoint, "_rank_zero_call", lambda action, label: action()) + monkeypatch.setattr(checkpoint, "_native_leaves", lambda *args: [optimizer]) + optimizer.calls.clear() + with pytest.raises(ValueError, match="checksum|size mismatch"): + checkpoint.load_training_checkpoint( + None, *runtime, complete, identity=IDENTITY, tracker=None + ) + assert not optimizer.calls + + +def _serial_worker(rank, root, fail_owner): + dist.init_process_group( + "gloo", + rank=rank, + world_size=4, + init_method=f"file://{root}/init-{fail_owner}", + timeout=timedelta(seconds=30), + ) + try: + + def action(): + lock = Path(root) / f"lock-{fail_owner}" + with lock.open("x"): + path = Path(root) / f"visited-{fail_owner}" + with path.open("a") as stream: + stream.write(f"{rank}\n") + if rank == fail_owner: + raise ValueError("simulated native serialization failure") + lock.unlink() + return rank + + try: + result = checkpoint._serial_rank_call(action, "protocol test") + except RuntimeError as exc: + result = str(exc) + (Path(root) / f"result-{fail_owner}-{rank}.json").write_text(json.dumps(result)) + finally: + dist.destroy_process_group() + + +@pytest.mark.parametrize("fail_owner", [-1, 2]) +def test_real_four_process_serial_protocol_propagates_failures(tmp_path, fail_owner): + mp.spawn(_serial_worker, args=(str(tmp_path), fail_owner), nprocs=4, join=True) + visited = (tmp_path / f"visited-{fail_owner}").read_text().splitlines() + assert visited == (["0", "1", "2", "3"] if fail_owner == -1 else ["0", "1", "2"]) + for rank in range(4): + result = json.loads((tmp_path / f"result-{fail_owner}-{rank}.json").read_text()) + if fail_owner == -1: + assert result == rank + else: + assert "simulated native serialization failure" in result + + +def _wire_sharded_hdo(optimizer, monkeypatch, *, break_alias=False, diverge=False): + """Emulate Megatron: the HDO is keyed by the DP shard, the getter returns the shard's master.""" + pytest.importorskip("megatron") + model_weight = optimizer.model.weight + shard = torch.nn.Parameter(model_weight.detach().clone().reshape(-1)) + master = model_weight.detach().float().clone().reshape(-1) + fp32_inner = master.clone() + if diverge: + fp32_inner[0] += 1.0 + hdo_state = {"master_param": master, "exp_avg": torch.zeros(8), "exp_avg_sq": torch.zeros(8)} + + class FakeHDO: + pass + + hdo = FakeHDO() + hdo.param_groups = [{"params": [shard]}] + hdo.state = {shard: hdo_state} + hdo.param_to_fp32_param = {shard: fp32_inner} + hdo.param_to_inner_param = {shard: fp32_inner} + cpu_state = {fp32_inner: {} if break_alias else hdo_state} + hdo.cpu_optimizers = [SimpleNamespace(param_groups=[{"params": [fp32_inner]}], state=cpu_state)] + monkeypatch.setattr( + "megatron.core.optimizer.cpu_offloading.hybrid_optimizer.HybridDeviceOptimizer", FakeHDO + ) + monkeypatch.setattr( + optimizer_checkpoint, + "_hdo_param_generator", + lambda cpu_optimizer: [p for g in cpu_optimizer.param_groups for p in g["params"]], + ) + optimizer.optimizer = hdo + optimizer.states["param"] = master # the real getter returns state["master_param"] + return hdo + + +def test_hdo_alias_check_uses_the_sharded_parameter_not_the_model_parameter(runtime, monkeypatch): + model, optimizer, _, _ = runtime + hdo = _wire_sharded_hdo(optimizer, monkeypatch) + assert model[0].weight not in hdo.param_to_fp32_param # keyed by the shard, as in Megatron + optimizer_checkpoint.verify_hdo_optimizer_master_aliases(model, [optimizer]) + + +@pytest.mark.parametrize( + "kwargs, message", + [ + ({"break_alias": True}, "not aliased"), + ({"diverge": True}, "diverged"), + ], +) +def test_hdo_alias_check_rejects_broken_wiring(runtime, monkeypatch, kwargs, message): + model, optimizer, _, _ = runtime + _wire_sharded_hdo(optimizer, monkeypatch, **kwargs) + with pytest.raises(ValueError, match=message): + optimizer_checkpoint.verify_hdo_optimizer_master_aliases(model, [optimizer]) diff --git a/tests/test_qwen3_next_training_closeout.py b/tests/test_qwen3_next_training_closeout.py new file mode 100644 index 000000000..fbd59b087 --- /dev/null +++ b/tests/test_qwen3_next_training_closeout.py @@ -0,0 +1,430 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""CPU recovery/audit regressions; these never substitute for native GPU updates.""" + +import copy +import json +import sys +from pathlib import Path +from types import SimpleNamespace + +import pytest +import torch + +from rl_engine.integrations.qwen3_next_subnode import SUBNODES + + +def test_requeue_repeats_final_reload_without_reusing_stale_attempt(monkeypatch): + import qwen3_next_training as runner + + journal = { + "schema_version": 1, + "identity": {}, + "events": list(runner.EVENTS), + "recovery": [], + "workers": {phase: {"path": phase} for phase in runner.PHASES}, + "l2_reports": { + phase: {"path": phase} for phase in ("baseline", "update1", "update2", "reload") + }, + } + monkeypatch.setattr(runner, "read_record", lambda record: {"attempt": "0"}) + assert runner.repeat_reload_in_current_attempt(journal, "1") + runner.validate_journal(journal, {}, verify_files=False) + assert set(journal["workers"]) == {"update1", "update2"} + assert set(journal["l2_reports"]) == {"baseline", "update1", "update2"} + retired = journal["recovery"][0] + assert retired["from_attempt"] == "0" and retired["to_attempt"] == "1" + assert set(retired["workers"]) == {"reload", "continuous", "resumed"} + assert not runner.repeat_reload_in_current_attempt(journal, "1") + + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) +import qwen3_next_training as runner # noqa: E402 + +from rl_engine.integrations import qwen3_next_native_training as native # noqa: E402 +from rl_engine.integrations import qwen3_next_training as observer # noqa: E402 + + +def parameter_names(): + return [ + f"chunk0.decoder.layers.{layer}.{component}.weight" + for layer in range(48) + for component in ("self_attention", "mlp", "pre_mlp_layernorm") + ] + [ + f"chunk0.{component}.weight" + for component in ("embedding", "output_layer", "final_layernorm") + ] + + +def audit(step=1): + names = parameter_names() + before = {name: "1" * 64 for name in names} + after = {name: "2" * 64 for name in names} + return { + "step": step, + "sample_position": step * 4, + "fixture": "a" * 64, + "policy_loss_calls": 4, + "grad_norm": 0.25, + "metrics": {"loss": -0.25}, + "parameter_hashes_before": before, + "parameter_hashes_after": after, + "changed_parameters": names, + "gradients": { + "parameters": { + name: {"present": True, "nonzero": True, "dtype": "torch.float32"} for name in names + }, + "active_groups": {name: [name] for name in names}, + }, + "progress": { + "consumed_before": (step - 1) * 4, + "consumed_after": step * 4, + "scheduler_before": (step - 1) * 4, + "scheduler_after": step * 4, + "sample_counter_owner": "qwen3_next_native_training.policy_step", + }, + } + + +def state(): + value = { + "schema_version": 1, + "scope": "native_vime_training_state", + "rank": 0, + "identity": {"fixture_sha256": "a" * 64}, + "step": 1, + "sample_position": 4, + "consumed_train_samples": 4, + "fixture_sha256": "a" * 64, + "model_sha256": ["b" * 64], + "optimizer_parameter_sha256": { + name: {kind: "c" * 64 for kind in ("param", "exp_avg", "exp_avg_sq")} + for name in parameter_names() + }, + "optimizer_header_sha256": "d" * 64, + "scheduler_sha256": "e" * 64, + "rng_sha256": "f" * 64, + } + return seal_state(value) + + +def seal_state(value): + value["state_sha256"] = observer.state_digest( + {key: item for key, item in value.items() if key != "state_sha256"} + ) + return value + + +def validate_state(value): + runner.validate_state( + value, rank=0, step=1, identity={"fixture_sha256": "a" * 64}, parameters=parameter_names() + ) + + +def test_full_actor_update_and_recovery_state_contract(): + runner.validate_update(audit(), step=1, fixture="a" * 64) + validate_state(state()) + + +@pytest.mark.parametrize( + "damage", ["no_change", "zero_gradient", "missing_layer", "nan_loss", "bad_progress"] +) +def test_update_rejects_fake_or_incomplete_actor_evidence(damage): + value = audit() + name = parameter_names()[0] + if damage == "no_change": + value["parameter_hashes_after"] = dict(value["parameter_hashes_before"]) + value["changed_parameters"] = [] + elif damage == "zero_gradient": + value["gradients"]["parameters"][name]["nonzero"] = False + elif damage == "missing_layer": + for key in ("parameter_hashes_before", "parameter_hashes_after"): + value[key] = { + name: digest for name, digest in value[key].items() if ".layers.47." not in name + } + elif damage == "nan_loss": + value["metrics"]["loss"] = float("nan") + else: + value["progress"]["consumed_after"] = 0 + with pytest.raises(ValueError): + runner.validate_update(value, step=1, fixture="a" * 64) + + +@pytest.mark.parametrize("field", ["rng_sha256", "scheduler_sha256", "optimizer_header_sha256"]) +def test_state_rejects_tampering_even_when_comparison_peer_was_copied(field): + value = state() + value[field] = "0" * 64 + copied_peer = copy.deepcopy(value) + assert value == copied_peer + with pytest.raises(ValueError, match="digest"): + validate_state(value) + + +@pytest.mark.parametrize("damage", ["consumed", "missing_master", "missing_parameter"]) +def test_resigned_state_still_requires_complete_progress_and_optimizer(damage): + value = state() + if damage == "consumed": + value["consumed_train_samples"] = 0 + elif damage == "missing_master": + del value["optimizer_parameter_sha256"][parameter_names()[0]]["param"] + else: + del value["optimizer_parameter_sha256"][parameter_names()[0]] + with pytest.raises(ValueError): + validate_state(seal_state(value)) + + +@pytest.mark.parametrize("behavior", ["success", "native_failure", "missing_scheduler_step"]) +def test_controller_advances_samples_only_after_successful_native_scheduler_step( + monkeypatch, behavior +): + args, scheduler = SimpleNamespace(consumed_train_samples=4), SimpleNamespace(num_steps=4) + + def update(*unused, **kwargs): + if behavior == "native_failure": + raise RuntimeError("injected native optimizer failure") + if behavior == "success": + scheduler.num_steps += 4 + return audit(step=2) + + monkeypatch.setattr(observer, "native_policy_update", update) + + def invoke(): + return native.policy_step(args, [], None, scheduler, None, step=2, required_groups={}) + + if behavior == "success": + result = invoke() + assert args.consumed_train_samples == scheduler.num_steps == 8 + assert result["progress"]["consumed_before"] == 4 + else: + with pytest.raises((RuntimeError, ValueError)): + invoke() + assert args.consumed_train_samples == 4 + + +def test_journal_requires_l2_between_updates_and_before_reload_controls(): + journal = { + "schema_version": 1, + "identity": {}, + "events": list(runner.EVENTS[:3]), + "workers": {"update1": {}}, + "l2_reports": {"baseline": {}, "update1": {}}, + } + runner.validate_journal(journal, {}, verify_files=False) + journal["events"] = ["l2:baseline", "worker:update1", "worker:update2"] + with pytest.raises(ValueError, match="order"): + runner.validate_journal(journal, {}, verify_files=False) + + +@pytest.mark.parametrize("missing", ["update2", "reload", "continuous", "resumed"]) +def test_final_acceptance_rejects_missing_phases_before_reading_evidence(missing): + report = { + "schema_version": 1, + "status": "passed", + "scope": "qwen3_next_training_and_reload", + "required_sections": list(runner.SECTIONS), + "events": list(runner.EVENTS), + "l2_reports": dict.fromkeys(("baseline", "update1", "update2", "reload"), {}), + "workers": dict.fromkeys(runner.PHASES, {}), + } + del report["workers"][missing] + with pytest.raises(ValueError, match="omitted"): + runner.validate_training_report(report) + + +def test_cleanup_rejects_symlink_to_another_directory_in_same_run(tmp_path): + retained = tmp_path / "retained" + retained.mkdir() + (retained / "state").write_text("must survive") + alias = tmp_path / "alias" + alias.symlink_to(retained, target_is_directory=True) + with pytest.raises(ValueError, match="owned"): + runner._owned_remove(alias, tmp_path) + assert (retained / "state").read_text() == "must survive" + + +def test_interrupted_cleanup_requires_matching_run_identity(tmp_path): + root = tmp_path / "checkpoints" + identity = {"run_id": "this-run"} + runner.owned_root(root, identity) + own = root / "step-1.partial" + own.mkdir() + (own / "STAGING.json").write_text(json.dumps({"identity": identity, "status": "staging"})) + assert len(runner.remove_interrupted(root, identity)) == 1 + assert not own.exists() + foreign = root / "foreign.partial" + foreign.mkdir() + (foreign / "STAGING.json").write_text( + json.dumps({"identity": {"run_id": "other"}, "status": "staging"}) + ) + with pytest.raises(ValueError, match="another run"): + runner.remove_interrupted(root, identity) + assert foreign.exists() + + +def test_actual_fault_injection_kills_writer_and_rejects_partial_or_corrupt_protocol(tmp_path): + result = runner.checkpoint_faults(tmp_path / "faults", {"run_id": "cpu-protocol-only"}) + evidence = runner.read_record(result) + assert set(evidence["cases"]) == set(runner.FAULTS) + assert all(case["passed"] for case in evidence["cases"].values()) + assert evidence["cases"]["interrupted_writer"]["exit_code"] < 0 + # A passed label cannot survive repair/substitution of the rejected bytes. + corrupt = Path(evidence["cases"]["hash_error"]["checkpoint"]) / "rank-2-optimizer.bin" + corrupt.write_bytes(b"2:optimizer") + with pytest.raises(ValueError, match="no longer reproduces"): + runner.validate_fault_evidence(evidence, {"run_id": "cpu-protocol-only"}) + + +def test_checkpoint_retention_keeps_successor_and_only_removes_older_owned_state(tmp_path): + identity = {"run_id": "retention-unit-contract"} + runner.owned_root(tmp_path, identity) + checkpoints = [] + for step in (1, 2): + path = tmp_path / f"step-{step}" + path.mkdir() + (path / "COMPLETE.json").write_text( + json.dumps({"identity": identity, "status": "complete", "step": step}) + ) + checkpoints.append(path) + retired = native.retire_prior_checkpoints(checkpoints[1], tmp_path, identity) + assert retired[0]["complete"]["step"] == 1 + assert not checkpoints[0].exists() + assert checkpoints[1].is_dir() + + +def test_checkpoint_retention_cannot_delete_a_foreign_run(tmp_path): + identity = {"run_id": "this-run"} + runner.owned_root(tmp_path, identity) + successor, foreign = tmp_path / "step-2", tmp_path / "other-run" + for path, owner, step in ((successor, identity, 2), (foreign, {"run_id": "other"}, 1)): + path.mkdir() + (path / "COMPLETE.json").write_text( + json.dumps({"identity": owner, "status": "complete", "step": step}) + ) + with pytest.raises(ValueError, match="unrelated"): + native.retire_prior_checkpoints(successor, tmp_path, identity) + assert successor.exists() and foreign.exists() + + +@pytest.mark.parametrize("difference", [None, "signed_zero", "state"]) +def test_reload_comparison_reopens_original_tensors_and_rejects_cross_phase_drift( + tmp_path, difference +): + # Small tensors test the raw comparison plumbing. Full matrix shape and + # provenance remain the independent validate_matrix_report prerequisite. + payload = { + "response_tokens": torch.ones((1, 16), dtype=torch.int64), + "logits": torch.zeros((1, 16, 2), dtype=torch.bfloat16), + "logprobs": torch.zeros((1, 16), dtype=torch.float32), + "embedding_digest": {"sample_sha256": ["d" * 64], "sha256": "e" * 64}, + "layer_digests": [ + { + "layer": index, + "sample_output_sha256": ["a" * 64], + "sample_state_sha256": ["b" * 64], + "sample_subnode_sha256": {name: ["f" * 64] for name in SUBNODES}, + } + for index in range(48) + ], + } + left, right = tmp_path / "update.pt", tmp_path / "reload.pt" + torch.save(payload, left) + if difference == "signed_zero": + payload["logits"][0, 0, 0] = -0.0 + elif difference == "state": + payload["layer_digests"][3]["sample_state_sha256"] = ["c" * 64] + torch.save(payload, right) + ids = [case.case_id for case in runner.case_matrix()] + + def matrix(path): + return { + "case_ids": ids, + "cases": { + name: { + "artifacts": {role: runner.record(path) for role in ("rollout", "recompute")} + } + for name in ids + }, + } + + if difference is None: + result = runner.compare_reload_matrices(matrix(left), matrix(right)) + assert result["status"] == "passed" and len(result["cases"]) == 16 + else: + with pytest.raises(ValueError, match="original.*tensor bits/state"): + runner.compare_reload_matrices(matrix(left), matrix(right)) + + +def test_stage_phase_ranges_and_continuation_identity(): + assert runner.stage_phases("train1") == ("update1",) + assert runner.stage_phases("train2") == ("update1", "update2") + assert runner.stage_phases("train") == runner.PHASES + model = {"source_tree": "t", "environment_sha256": "e"} + journal_identity = {**model, "run_id": "r1", "execution_id": "1", "fixture_sha256": "f"} + assert runner.continuation_identity_matches(journal_identity, model, "f") + assert not runner.continuation_identity_matches(journal_identity, model, "g") + assert not runner.continuation_identity_matches( + journal_identity, {**model, "environment_sha256": "x"}, "f" + ) + + +def test_partial_training_evidence_uses_the_phase_export_identity(tmp_path, monkeypatch): + import qwen3_next_contract as contract + import qwen3_next_l2 + + from rl_engine.testing import qwen3_next_l2 as l2_testing + + manifest = {"stage": "train1", "checkpoint": "/official"} + export = {"checkpoint": "/export", "weight_version": "update-1", "base_checkpoint": "/official"} + monkeypatch.setattr(qwen3_next_l2, "model_identity", lambda m: {"ckpt": m["checkpoint"]}) + monkeypatch.setattr(runner, "read_record", lambda value: {"export": export}) + seen = [] + monkeypatch.setattr( + l2_testing, + "validate_matrix_report", + lambda report, expected_identity, artifact_root: seen.append(expected_identity), + ) + matrix = tmp_path / "l2-matrix.json" + matrix.write_text("{}") + progress = { + "status": "passed", + "scope": contract.SCOPES["train1"], + "identity": {"ckpt": "/official"}, + "phases": ["update1"], + "workers": {"update1": {"path": "w"}}, + "l2_reports": {"update1": {"sha256": "abc"}}, + } + path = tmp_path / "progress.json" + path.write_text(json.dumps(progress)) + evidence = {"updates": {"path": str(path)}, "l2_matrix": {"path": str(matrix), "sha256": "abc"}} + contract.validate_partial_training_evidence(manifest, tmp_path, evidence, attempt_root=tmp_path) + assert seen == [{"ckpt": "/export"}] # not the base model identity + with pytest.raises(ValueError, match="does not reference"): + contract.validate_partial_training_evidence( + manifest, + tmp_path, + {**evidence, "l2_matrix": {**evidence["l2_matrix"], "sha256": "x"}}, + attempt_root=tmp_path, + ) + export["base_checkpoint"] = "/elsewhere" + with pytest.raises(ValueError, match="pinned base"): + contract.validate_partial_training_evidence( + manifest, tmp_path, evidence, attempt_root=tmp_path + ) + + +def test_continued_storage_follows_the_chain_back_to_train1(tmp_path): + def run(name, stage, previous=None): + directory = tmp_path / name + directory.mkdir() + manifest = {"stage": stage, "previous": str(previous) if previous else None} + (directory / "run.json").write_text(json.dumps(manifest)) + return directory + + l2 = run("l2", "l2") + train1 = run("train1", "train1", l2) + train2 = run("train2", "train2", train1) + assert runner.continued_training_storage(train1) == train1 / "training-state" + assert runner.continued_training_storage(train2) == train1 / "training-state" + with pytest.raises(ValueError, match="back to a train1 run"): + runner.continued_training_storage(l2) diff --git a/tests/test_qwen3_next_training_export.py b/tests/test_qwen3_next_training_export.py new file mode 100644 index 000000000..18a6bad55 --- /dev/null +++ b/tests/test_qwen3_next_training_export.py @@ -0,0 +1,288 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Tiny CPU export-publication regressions, not full-model numerical acceptance.""" + +import copy +import json +import shutil +import sys +from pathlib import Path + +import pytest +import torch +from safetensors.torch import save_file + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts")) +import qwen3_next_training_export as export # noqa: E402 +from qwen3_next_runtime import file_digest # noqa: E402 + + +def write_weights(root, values, filename): + save_file(values, root / filename) + index = { + "metadata": { + "total_size": sum(value.numel() * value.element_size() for value in values.values()) + }, + "weight_map": {name: filename for name in values}, + } + (root / export.INDEX_FILE).write_text(json.dumps(index)) + + +@pytest.fixture +def staged(tmp_path, monkeypatch): + base = tmp_path / "unit-official" + base.mkdir() + (base / "config.json").write_text( + json.dumps({"model_type": "qwen3_next", "num_hidden_layers": 48}) + ) + (base / "tokenizer.json").write_text(json.dumps({"unit_fixture": "not a real tokenizer"})) + values = { + f"model.layers.{layer}.input_layernorm.weight": torch.ones(2, dtype=torch.bfloat16) + for layer in range(48) + } + values.update( + { + "model.embed_tokens.weight": torch.ones((4, 2), dtype=torch.bfloat16), + "model.norm.weight": torch.ones(2, dtype=torch.bfloat16), + "lm_head.weight": torch.ones((4, 2), dtype=torch.bfloat16), + } + ) + write_weights(base, values, "base.safetensors") + base_identity = { + "revision": base.name, + "metadata": { + path.name: file_digest(path) for path in base.iterdir() if path.suffix != ".safetensors" + }, + "shards": {"base.safetensors": {"size": (base / "base.safetensors").stat().st_size}}, + } + monkeypatch.setattr(export, "_reference", lambda: {"revision": base.name}) + schema_reference = { + "tensor_count": len(values), + "weight_schema_sha256": export._json_digest( + export._weight_schema(base, check_finite=False) + ), + } + monkeypatch.setattr(export, "_schema_reference", lambda: schema_reference) + + def identity(path): + assert Path(path) == base + return copy.deepcopy(base_identity) + + monkeypatch.setattr(export, "checkpoint_identity", identity) + staging, destination = tmp_path / "export.partial", tmp_path / "export-complete" + staging.mkdir() + for name in ("config.json", "tokenizer.json"): + shutil.copy2(base / name, staging / name) + write_weights( + staging, {name: value + 1 for name, value in values.items()}, "updated.safetensors" + ) + (staging / "STAGING.json").write_text(json.dumps({"scope": export.KIND, "status": "staging"})) + training = { + "source_commit": "unit-source", + "source_tree": "unit-tree", + "environment_sha256": "unit-env", + "provider_config": {"provider": "strict"}, + "topology": {"tp": 4, "pp": 1, "cp": 1, "ep": 1}, + "checkpoint": str(base), + "checkpoint_identity": base_identity, + } + return { + "staging": staging, + "destination": destination, + "step": 1, + "weight_version": "update-1", + "identity": training, + "base_checkpoint": base, + "base_checkpoint_identity": base_identity, + "model_hashes": [str(rank) * 64 for rank in range(4)], + } + + +def publish(staged): + return export._publish_export(**staged) + + +def test_atomic_native_export_identity_and_all_file_verification(staged): + record = publish(staged) + assert not staged["staging"].exists() + assert ( + record["metadata"][export.INDEX_FILE] + != staged["base_checkpoint_identity"]["metadata"][export.INDEX_FILE] + ) + assert export.validate_training_export(staged["destination"], record) == record + assert record["tensor_count"] == 51 + assert record["model_identity_method"] == "native_parameter_hashes_after" + + +def test_history_record_survives_weight_retirement_but_cannot_load(staged): + record = publish(staged) + (staged["destination"] / "updated.safetensors").unlink() + assert export.validate_training_export_record(record, record) == record + with pytest.raises(ValueError, match="file inventory changed"): + export.validate_training_export(staged["destination"], record) + + +def test_same_size_weight_corruption_is_rejected(staged): + record = publish(staged) + path = staged["destination"] / "updated.safetensors" + payload = bytearray(path.read_bytes()) + payload[-1] ^= 1 + path.write_bytes(payload) + with pytest.raises(ValueError, match="checksum"): + export.validate_training_export(staged["destination"], record) + + +def test_export_requires_completion_publication(staged): + with pytest.raises(ValueError, match="unpublished"): + export.validate_training_export(staged["staging"], {}) + + +@pytest.mark.parametrize("missing", ["model.layers.47.input_layernorm.weight", "lm_head.weight"]) +def test_reduced_model_or_missing_head_cannot_publish(staged, missing): + from safetensors.torch import load_file + + values = load_file(staged["staging"] / "updated.safetensors") + del values[missing] + write_weights(staged["staging"], values, "updated.safetensors") + with pytest.raises(ValueError, match="omitted trained official tensors"): + publish(staged) + assert not (staged["staging"] / export.COMPLETE_FILE).exists() + + +@pytest.mark.parametrize("mutation", ["dtype", "nonfinite", "shape"]) +def test_export_preserves_finite_original_weight_schema(staged, mutation): + from safetensors.torch import load_file + + values = load_file(staged["staging"] / "updated.safetensors") + key = "lm_head.weight" + if mutation == "dtype": + values[key] = values[key].float() + elif mutation == "nonfinite": + values[key][0, 0] = float("nan") + else: + values[key] = values[key][:2].contiguous() + write_weights(staged["staging"], values, "updated.safetensors") + with pytest.raises(ValueError, match="BF16|Nonfinite|complete official tensor schema"): + publish(staged) + + +def test_modified_tokenizer_cannot_publish(staged): + (staged["staging"] / "tokenizer.json").write_text("changed") + with pytest.raises(ValueError, match="tokenizer/configuration assets"): + publish(staged) + + +def test_different_requested_export_identity_is_rejected(staged): + record = publish(staged) + expected = {**record, "weight_version": "reload-2", "step": 2} + with pytest.raises(ValueError, match="requested weights"): + export.validate_training_export(staged["destination"], expected) + + +@pytest.mark.parametrize("field", ["weights_content_sha256", "metadata", "model_identity_method"]) +def test_self_consistent_expected_record_cannot_bypass_required_proofs(staged, field): + record = publish(staged) + record[field] = {} if field == "metadata" else "wrong" + with pytest.raises(ValueError, match="proof|parameter audit"): + export.validate_training_export_record(record, record) + + +def test_export_base_and_native_training_provenance_must_match(staged): + record = publish(staged) + record["training_identity"]["checkpoint"] = "/different-base" + with pytest.raises(ValueError, match="baseline/source/environment provenance"): + export.validate_training_export_record(record, record) + + +def test_no_unmanifested_file_or_symlink_is_accepted(staged): + record = publish(staged) + destination = staged["destination"] + (destination / "extra").write_text("unmanifested") + with pytest.raises(ValueError, match="file inventory changed"): + export.validate_training_export(destination, record) + (destination / "extra").unlink() + (destination / "tokenizer.json").unlink() + (destination / "tokenizer.json").symlink_to(staged["base_checkpoint"] / "tokenizer.json") + with pytest.raises(ValueError, match="external HF export"): + export.validate_training_export(destination, record) + + +def _add_mtp_head_to_base(staged, monkeypatch, n_tensors=3): + """Give the official base an untrained MTP head the native saver never writes.""" + from safetensors.torch import load_file + + base = staged["base_checkpoint"] + values = load_file(base / "base.safetensors") + mtp = { + f"mtp.layers.0.mlp.experts.{i}.down_proj.weight": torch.full( + (2, 2), float(i + 1), dtype=torch.bfloat16 + ) + for i in range(n_tensors) + } + values.update(mtp) + write_weights(base, values, "base.safetensors") + staged["base_checkpoint_identity"]["metadata"][export.INDEX_FILE] = file_digest( + base / export.INDEX_FILE + ) + staged["identity"]["checkpoint_identity"] = staged["base_checkpoint_identity"] + schema_reference = { + "tensor_count": len(values), + "weight_schema_sha256": export._json_digest( + export._weight_schema(base, check_finite=False) + ), + } + monkeypatch.setattr(export, "_schema_reference", lambda: schema_reference) + return mtp + + +def test_untrained_mtp_head_is_carried_from_the_base(staged, monkeypatch): + from safetensors.torch import load_file + + mtp = _add_mtp_head_to_base(staged, monkeypatch) + record = publish(staged) + passthrough = record["base_passthrough"] + assert passthrough["tensor_count"] == len(mtp) and passthrough["prefixes"] == ["mtp."] + (shard,) = passthrough["shards"] + assert passthrough["shards"][shard] == {"source": "base.safetensors", "tensor_count": len(mtp)} + index = json.loads((staged["destination"] / export.INDEX_FILE).read_text()) + assert {name: index["weight_map"][name] for name in mtp} == {name: shard for name in mtp} + carried = load_file(staged["destination"] / shard) + assert all(torch.equal(carried[name], value) for name, value in mtp.items()) + assert record["tensor_count"] == 51 + len(mtp) + assert export.validate_training_export(staged["destination"], record) == record + + +def test_passthrough_is_limited_to_the_mtp_head(staged, monkeypatch): + from safetensors.torch import load_file + + _add_mtp_head_to_base(staged, monkeypatch) + values = load_file(staged["staging"] / "updated.safetensors") + del values["model.norm.weight"] + write_weights(staged["staging"], values, "updated.safetensors") + with pytest.raises( + ValueError, match="omitted trained official tensors: \\['model.norm.weight'\\]" + ): + publish(staged) + assert not (staged["staging"] / export.COMPLETE_FILE).exists() + + +def test_saver_must_not_write_part_of_the_passthrough_head(staged, monkeypatch): + from safetensors.torch import load_file + + mtp = _add_mtp_head_to_base(staged, monkeypatch) + values = load_file(staged["staging"] / "updated.safetensors") + name = next(iter(mtp)) + values[name] = mtp[name] + write_weights(staged["staging"], values, "updated.safetensors") + with pytest.raises(ValueError, match="part of the base passthrough head"): + publish(staged) + + +def test_record_must_declare_the_base_passthrough(staged): + record = publish(staged) + assert record["base_passthrough"] == {"prefixes": ["mtp."], "tensor_count": 0, "shards": {}} + broken = copy.deepcopy(record) + del broken["base_passthrough"] + with pytest.raises(ValueError, match="came from the base"): + export.validate_training_export_record(broken, broken) diff --git a/tests/test_qwen3_next_vllm_attention_metadata.py b/tests/test_qwen3_next_vllm_attention_metadata.py new file mode 100644 index 000000000..02150f052 --- /dev/null +++ b/tests/test_qwen3_next_vllm_attention_metadata.py @@ -0,0 +1,75 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +from types import SimpleNamespace + +import pytest +import torch + +from rl_engine.integrations.qwen3_next_vllm_attention import ( + SharedAttentionBackend, + SharedAttentionBuilder, + SharedAttentionImpl, + validate_boundaries, +) + + +def _config(): + return SimpleNamespace( + parallel_config=SimpleNamespace( + tensor_parallel_size=4, + pipeline_parallel_size=1, + decode_context_parallel_size=1, + prefill_context_parallel_size=1, + ), + model_config=SimpleNamespace(enforce_eager=True), + cache_config=SimpleNamespace(enable_prefix_caching=False), + speculative_config=None, + ) + + +def test_real_builder_preserves_mixed_absolute_prefixes_and_ignores_optimistic_bounds(): + builder = SharedAttentionBuilder(None, ["layer3"], _config(), torch.device("cpu")) + metadata = builder.build( + 0, + SimpleNamespace( + num_reqs=3, + num_actual_tokens=5, + query_start_loc=torch.tensor([0, 1, 1, 5]), + seq_lens=torch.tensor([65, 0, 4]), + seq_lens_cpu_upper_bound=torch.tensor([100, 0, 999]), + block_table_tensor=torch.tensor([[3, 4], [0, 0], [8, 0]]), + slot_mapping=torch.tensor([128, 256, 257, 258, 259, -1]), + causal=True, + ), + ) + assert metadata.seq_lens == (65, 0, 4) + assert metadata.query_start_loc == (0, 1, 1, 5) + assert metadata.slot_mapping.numel() == 5 + assert not SharedAttentionBackend.supports_batch_invariance() + + +@pytest.mark.parametrize( + "starts,lengths,n", + [ + ((0, 2), (1,), 2), + ((0, 1), (1,), 2), + ((1, 2), (2,), 2), + ((0, 1), (1041,), 1), + ((0, 2, 1), (2, 1), 1), + ], +) +def test_invalid_scheduler_boundaries_fail(starts, lengths, n): + with pytest.raises(ValueError): + validate_boundaries(starts, lengths, n) + + +def test_empty_batch_and_profile_guards(): + validate_boundaries((0,), (), 0) + config = _config() + config.cache_config.enable_prefix_caching = True + with pytest.raises(ValueError, match="Prefix reuse"): + SharedAttentionBuilder(None, [], config, torch.device("cpu")) + with pytest.raises(ValueError, match="four Q heads"): + SharedAttentionImpl(8, 256, 1 / 16, 1) + SharedAttentionImpl(4, 256, 1 / 16, 1) From d6106c2ebcc844a5e9ea44717b6836a00f84bb13 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 9 Oct 2026 03:11:51 +0000 Subject: [PATCH 07/68] feat(qwen3-next): batch-invariance matrix for the full model RFC #428 batch_invariance_ci (CUDA). - Stage bi (after a passed l2, same certified profile): 97 pinned cells in rl_engine/testing/qwen3_next_bi_cases.py / qwen3_next_bi_manifest.json. concurrency (1-64 requests, target first or last), interference (companion content, lengths, arrival during prefill/decode, generation lengths), chunk boundaries (prompts 63-1024 around 64/128/528/1024, chunk 64/128, first chunk 1/63/65, compared with the replay recompute) and replay THD packing (1-7 neighbours, target first or last). A quick CI subset runs one cell per family plus 64 concurrent requests. - Native rollout: version 2 requests run one traced target next to untraced companions; the exact-prefill scheduler admits each request at its arrival step, pins the target's planned chunk at every step and fails on any preemption; the trace records only the target. - Replay: packed_layout() packs variable-length neighbours with the traced target in one THD batch. - bi-matrix.json names each failing cell's first divergent layer, sub-node and layer family; the chain report gains a batch_invariance section, and the evidence publisher takes the bi run with --also. - gtest: bitwise judgments compare raw bits (signed zero, NaN/Inf and dtype now count), not allclose with zero tolerance. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/ci.yml | 3 +- .github/workflows/qwen3-next-provider-gpu.yml | 3 +- docs/usage/qwen3-next-batch-invariance.md | 55 + .../qwen3_next_native_recompute.py | 53 +- .../integrations/qwen3_next_native_rollout.py | 305 +- rl_engine/kernels/gtest/forward_invariance.py | 6 +- .../kernels/gtest/qwen3_next_chain_report.py | 25 + rl_engine/testing/qwen3_next_bi_cases.py | 370 ++ rl_engine/testing/qwen3_next_bi_manifest.json | 3857 +++++++++++++++++ scripts/qwen3_next_batch_invariance.py | 337 ++ scripts/qwen3_next_chain_report.py | 12 + scripts/qwen3_next_contract.py | 17 + scripts/qwen3_next_publish_evidence.py | 14 + scripts/qwen3_next_run.py | 15 +- scripts/qwen3_next_runtime.py | 2 + tests/check_qwen3_next_bi_requests.py | 44 + tests/test_qwen3_next_bi_cases.py | 94 + tests/test_qwen3_next_chain_report.py | 38 + tests/test_tensor_identity.py | 37 + 19 files changed, 5251 insertions(+), 36 deletions(-) create mode 100644 docs/usage/qwen3-next-batch-invariance.md create mode 100644 rl_engine/testing/qwen3_next_bi_cases.py create mode 100644 rl_engine/testing/qwen3_next_bi_manifest.json create mode 100644 scripts/qwen3_next_batch_invariance.py create mode 100644 tests/check_qwen3_next_bi_requests.py create mode 100644 tests/test_qwen3_next_bi_cases.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f50c73cef..e6e1f2aa7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -92,7 +92,8 @@ jobs: tests/test_qwen3_next_subnode.py \ tests/test_qwen3_next_chain_report.py \ tests/test_qwen3_next_run.py \ - tests/test_qwen3_next_evidence_redaction.py + tests/test_qwen3_next_evidence_redaction.py \ + tests/test_qwen3_next_bi_cases.py - name: Run Cross-Configuration Contract Tests (CPU-safe) run: | diff --git a/.github/workflows/qwen3-next-provider-gpu.yml b/.github/workflows/qwen3-next-provider-gpu.yml index ca5bf6f4d..9b19dcf89 100644 --- a/.github/workflows/qwen3-next-provider-gpu.yml +++ b/.github/workflows/qwen3-next-provider-gpu.yml @@ -90,4 +90,5 @@ jobs: tests/check_qwen3_next_gdn_bridge.py tests/check_qwen3_next_gdn_sequence.py \ tests/check_qwen3_next_conv_bridge.py tests/check_qwen3_next_core_matrix.py \ tests/check_qwen3_next_shared_core.py \ - tests/check_qwen3_next_paged_attention.py tests/check_qwen3_next_batch_invariance.py + tests/check_qwen3_next_paged_attention.py tests/check_qwen3_next_batch_invariance.py \ + tests/check_qwen3_next_bi_requests.py diff --git a/docs/usage/qwen3-next-batch-invariance.md b/docs/usage/qwen3-next-batch-invariance.md new file mode 100644 index 000000000..603deccce --- /dev/null +++ b/docs/usage/qwen3-next-batch-invariance.md @@ -0,0 +1,55 @@ +# Qwen3-Next batch invariance (RFC #428 `batch_invariance_ci`) + +The `bi` stage checks that a Qwen3-Next-80B-A3B-Instruct sequence gets the +same bits whatever else the engines are doing: other requests in the same +vLLM step, how many there are, when they arrive, how its prompt is chunked, +and how the replay engine packs it with other sequences. It runs after a +passed `l2` stage of the [full-model chain](qwen3-next-chain.md), under the +same certified provider profile. + +```bash +python scripts/qwen3_next_run.py --stage bi --previous /<...-l2-...> \ + --runs-root --checkpoint --cuda-home \ + --python /bin/python [--bi-suite quick] +python scripts/qwen3_next_chain_report.py --last <...-train-...> --bi <...-bi-...> --out chain-report.json +``` + +## Cells + +Every cell has one *target* (16 generated tokens) and compares it bitwise - +response tokens, BF16 logits, FP32 logprobs, the embedding and every layer, +sub-node and recurrent/KV-state digest - with a reference. The cells are +generated by `rl_engine/testing/qwen3_next_bi_cases.py` and pinned by +`qwen3_next_bi_manifest.json`. + +| Family | Cells | The target runs | Compared with | +| --- | --- | --- | --- | +| `concurrency` | 13 | with 0, 1, 3, 7, 15, 31 or 63 companions (prompts 37-1000 tokens), first or last in the FCFS order | the target alone, same chunk plan | +| `interference` | 10 | with companions of the same text, very short or long prompts, mixed lengths, arriving during its prefill, during its decode or at its last decode step, generating 1 or 24 tokens; or itself arriving after them | the target alone, same chunk plan | +| `chunk` | 66 | alone, prompt 63/64/65, 127/128/129, 527/528/529, 1023/1024 tokens, chunk 64 or 128, first chunk 1, 63 or 65 | the replay engine's recompute of the same tokens (the train/rollout boundary) | +| `packing` | 8 | in the replay engine, packed into one THD batch with 1, 3 or 7 neighbours (16-1015 tokens), first or last | the replay of the target alone | + +`--bi-suite quick` runs five cells (one per family plus 64 concurrent +requests) for CI. Prompt 1025 is outside the pinned rollout profile +(`max_model_len` 1040 = 1024 + 16). + +## How a cell runs + +All requests of a cell are submitted together; the exact-prefill scheduler +admits a request to the waiting queue only at its `arrival_step`, counted in +scheduled steps, so arrivals are reproducible. Every prefilling request +shares one `long_prefill_token_threshold`: the target's next planned chunk +while the target prefills, the cell's chunk size otherwise. The scheduler +fails the cell if the target does not get exactly its planned tokens at +every step until its last input, or if any request is preempted. Only the +target is traced; companions generate greedily and are never recorded. +The rollout caps (64 sequences, 16384 tokens per step, 4 GiB of KV cache per +GPU) are part of the certified profile the `layers` stage qualifies. + +## Results + +`bi-matrix.json` lists every cell with its comparisons, the first divergent +layer and sub-node, and its layer family (`embedding`, `norm`, `gdn`, +`full_attention`, `moe` or `lm_head_logprob`). The chain report's +`batch_invariance` section summarises it per family; a failing cell fails +the chain report. diff --git a/rl_engine/integrations/qwen3_next_native_recompute.py b/rl_engine/integrations/qwen3_next_native_recompute.py index a59e89dc7..0624ffeff 100644 --- a/rl_engine/integrations/qwen3_next_native_recompute.py +++ b/rl_engine/integrations/qwen3_next_native_recompute.py @@ -27,19 +27,44 @@ from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal as exact +def packed_layout(request): + """``(sequences, traced)``: every packed sequence's tokens and which ones are traced. + + An L2 request packs its equal-length samples and traces all of them. A + batch-invariance packing request (``request["packing"]``) packs its target + with untraced companions of any length; only the target is traced. + """ + traced = request["consumed_input_tokens"] + packing = request.get("packing") + if packing is None: + return traced, list(range(len(traced))) + sequences, index = packing["sequences"], packing["target_index"] + if ( + len(traced) != 1 + or index not in range(len(sequences)) + or sequences[index] != traced[0] + or any(not row for row in sequences) + ): + raise ValueError("Packing request must contain its traced target exactly once") + return sequences, [index] + + def response_rows(value, request): - """Keep original BF16 values at P-1..P+14 for every packed sample.""" - batch = len(request["prompt_tokens"]) + """Keep original values at P-1..P+14 of every traced packed sample.""" + sequences, traced = packed_layout(request) + starts = [0] + for row in sequences: + starts.append(starts[-1] + len(row)) length = len(request["consumed_input_tokens"][0]) if value.dtype not in (torch.bfloat16, torch.int64) or value.numel() == 0: raise ValueError("Response traces must preserve nonempty original BF16/int64 storage") value = value.reshape(-1, value.shape[-1]) - if value.shape[0] < batch * length: + if value.shape[0] < starts[-1]: raise ValueError("Packed model output does not cover every frozen sample") positions = torch.tensor(request["prediction_positions"], device=value.device) if positions.shape != (16,) or positions[-1] != length - 1: raise ValueError("Response positions must end before consuming the last target token") - return torch.stack([value[index * length + positions] for index in range(batch)]) + return torch.stack([value[starts[index] + positions] for index in traced]) def _rank_main(rank, cases, staging): @@ -89,18 +114,22 @@ def _rank_main(rank, cases, staging): "request": str(Path(_output).with_suffix(".request.json")), }, ) - consumed = torch.tensor( - request["consumed_input_tokens"], device="cuda", dtype=torch.long - ) - batch, length = consumed.shape + sequences, traced_rows = packed_layout(request) + batch, length = len(traced_rows), len(request["consumed_input_tokens"][0]) if length != request["state_position"] + 1: raise ValueError("Recompute recurrent-state position is inconsistent") - cu = torch.arange(batch + 1, device="cuda", dtype=torch.int32) * length + consumed = torch.tensor( + [token for row in sequences for token in row], device="cuda", dtype=torch.long + ) + lengths = [len(row) for row in sequences] + cu = torch.tensor( + [0, *torch.tensor(lengths).cumsum(0).tolist()], device="cuda", dtype=torch.int32 + ) packed = PackedSeqParams( cu_seqlens_q=cu, cu_seqlens_kv=cu, - max_seqlen_q=length, - max_seqlen_kv=length, + max_seqlen_q=max(lengths), + max_seqlen_kv=max(lengths), qkv_format="thd", ) traces, handles = {}, [] @@ -117,7 +146,7 @@ def hook(_module, _inputs, output): def capture_state(index): def hook(state, indices, _cu): - slots = indices[:batch].long() + slots = indices[traced_rows].long() if (index + 1) % 4: values = { "convolution": state.convolution[slots], diff --git a/rl_engine/integrations/qwen3_next_native_rollout.py b/rl_engine/integrations/qwen3_next_native_rollout.py index 912354597..64012765b 100644 --- a/rl_engine/integrations/qwen3_next_native_rollout.py +++ b/rl_engine/integrations/qwen3_next_native_rollout.py @@ -85,22 +85,25 @@ def runtime_profile(config): def validate_request(request): - if request.get("role") != "rollout" or request.get("schema_version") != 1: - raise ValueError("A version1 rollout request is required") + if request.get("role") != "rollout" or request.get("schema_version") not in (1, 2): + raise ValueError("A version 1 (L2) or 2 (batch-invariance) rollout request is required") + # Version 2 is one traced target next to untraced companions. + bi = request["schema_version"] == 2 digest_input = {key: value for key, value in request.items() if key != "request_sha256"} if json_digest(digest_input) != request.get("request_sha256"): raise ValueError("Rollout request identity mismatch") prompts, samples = request["prompt_tokens"], request["sample_ids"] batch = len(prompts) if ( - batch not in (1, 4) + batch not in ((1,) if bi else (1, 4)) or len(samples) != batch or any(type(sample) is not int for sample in samples) or len(set(samples)) != batch ): raise ValueError("Rollout requires one or four uniquely identified samples") length = len(prompts[0]) - if length not in (8, 64, 256, 1024) or any(len(row) != length for row in prompts): + lengths = range(1, 1025) if bi else (8, 64, 256, 1024) + if length not in lengths or any(len(row) != length for row in prompts): raise ValueError("Rollout prompts must use one official matrix length") responses = request["response_tokens"] for rows in (prompts, responses or []): @@ -137,7 +140,9 @@ def validate_request(request): ): raise ValueError("Rollout execution or response offsets differ from the pinned contract") if responses is None: - if batch != 4 or chunks != [length] or request["consumed_input_tokens"] is not None: + if request["consumed_input_tokens"] is not None or ( + not bi and (batch != 4 or chunks != [length]) + ): raise ValueError("Only baseline B4 full prefill may define frozen responses") elif request["consumed_input_tokens"] != [ prompt + response[:-1] for prompt, response in zip(prompts, responses, strict=True) @@ -145,6 +150,10 @@ def validate_request(request): raise ValueError("Consumed input must end before response token sixteen") if any(type(layer) is not int or layer not in range(48) for layer in request["trace_layers"]): raise ValueError("Debug layer indices must be in [0,48)") + if bi: + validate_companions(request, length) + elif "companions" in request: + raise ValueError("Only a version 2 request may run companions") identity = request["identity"] if identity["architecture_layers"] != 48 or identity["topology"] != { "tp": 4, @@ -163,6 +172,37 @@ def validate_request(request): return batch, length +def validate_companions(request, length): + companions = request["companions"] + tokens, arrivals = request["companion_max_tokens"], request["arrival_steps"] + max_len = NATIVE_ROLLOUT_PROFILE["max_model_len"] + if ( + not isinstance(companions, list) + or len(companions) > NATIVE_ROLLOUT_PROFILE["max_num_seqs"] - 1 + or len(tokens) != len(companions) + or len(arrivals.get("companions", [])) != len(companions) + or type(arrivals.get("target")) is not int + or arrivals["target"] < 0 + or any(type(step) is not int or step < 0 for step in arrivals["companions"]) + or type(request["chunk_size"]) is not int + or request["chunk_size"] < 1 + or length + RESPONSE_LENGTH > max_len + ): + raise ValueError("Batch-invariance companions, arrivals or chunk size are malformed") + for prompt, count in zip(companions, tokens, strict=True): + if ( + not prompt + or type(count) is not int + or count < 1 + or len(prompt) + count > max_len + or any(type(token) is not int or not 0 <= token < 151936 for token in prompt) + ): + raise ValueError("A companion does not fit the pinned rollout profile") + + +RESPONSE_LENGTH = 16 + + def next_scheduled_tokens(computed, prompt_length, chunks): boundary = 0 for size in chunks: @@ -181,6 +221,12 @@ def schedule(self, throttle_prefills=False): active = [request for request in self.requests.values() if not request.is_finished()] if not active: return super().schedule(throttle_prefills) + if any( + (request.sampling_params.extra_args or {}).get("shared_bi_companion") is not None + or ((request.sampling_params.extra_args or {}).get("shared_l2") or {}).get("bi") + for request in active + ): + return self._schedule_batch_invariance(active, throttle_prefills) descriptors = [ (request.sampling_params.extra_args or {}).get("shared_l2") for request in active ] @@ -235,6 +281,82 @@ def schedule(self, throttle_prefills=False): ) return result + def _schedule_batch_invariance(self, active, throttle_prefills): + """One traced target keeps its exact chunk plan; companions fill the step. + + All requests of a cell are admitted before the first step (as for L2). + A request joins the waiting queue only at its ``arrival_step``, counted + in scheduled steps from the first one, so arrivals are reproducible + whatever the engine-core timing. Every prefilling request shares one + ``long_prefill_token_threshold``: the target's next planned chunk while + it prefills, the cell's chunk size otherwise. The target must get + exactly its planned token count at every step until it has consumed its + last input, and no request may be preempted. + """ + descriptors = {} + for request in self.requests.values(): + extra = request.sampling_params.extra_args or {} + descriptor = extra.get("shared_l2") or extra.get("shared_bi_companion") + if descriptor is None: + raise ValueError("Exact Qwen scheduler accepts only identified requests") + descriptors[request.request_id] = (request, descriptor, "shared_l2" in extra) + cells = {item[1]["request_sha256"] for item in descriptors.values()} + targets = [item for item in descriptors.values() if item[2]] + if len(cells) != 1 or len(targets) > 1: + raise ValueError("One batch-invariance cell runs exactly one traced target") + # Once the target has finished, the scheduler frees it; companions go on. + target, contract, _ = ( + targets[0] if targets else (None, *next(iter(descriptors.values()))[1:]) + ) + cell = contract["request_sha256"] + if getattr(self, "_bi_cell", None) != cell: + self._bi_cell, self._bi_step, self._bi_started = cell, 0, False + if not self._bi_started and len(descriptors) < contract["total_requests"]: + previous = self._pause_state + self._pause_state = PauseState.PAUSED_ALL + try: + result = super().schedule(throttle_prefills) + finally: + self._pause_state = previous + if result.total_num_scheduled_tokens: + raise RuntimeError("The native scheduler violated the cell admission barrier") + return result + self._bi_started = True + held = [ + r for r in self.waiting if descriptors[r.request_id][1]["arrival_step"] > self._bi_step + ] + if len(held) == len(self.waiting) and not self.running and held: + # Nothing can run before the next arrival: that arrival is now. + self._bi_step = min(descriptors[r.request_id][1]["arrival_step"] for r in held) + held = [r for r in held if descriptors[r.request_id][1]["arrival_step"] > self._bi_step] + self.waiting.remove_requests(held) + target_active = target is not None and not target.is_finished() and target not in held + count = None + if target_active: + count = next_scheduled_tokens( + target.num_computed_tokens, contract["prompt_length"], contract["prefill_chunks"] + ) + prefilling = target_active and target.num_computed_tokens < contract["prompt_length"] + self.scheduler_config.long_prefill_token_threshold = ( + count if prefilling else contract["chunk_size"] + ) + try: + result = super().schedule(throttle_prefills) + finally: + for request in held: + self.waiting.add_request(request) + if result.total_num_scheduled_tokens: + self._bi_step += 1 + if result.preempted_req_ids or ( + count is not None and result.num_scheduled_tokens.get(target.request_id) != count + ): + raise RuntimeError( + "exact_scheduler_mismatch: target expected " + f"{count}, actual {result.num_scheduled_tokens.get(target.request_id)}, " + f"preempted {sorted(result.preempted_req_ids)}" + ) + return result + class ForceFrozenTokens(AdapterLogitsProcessor): """Sampling-only teacher forcing; raw model logits are observed beforehand.""" @@ -243,6 +365,8 @@ def is_argmax_invariant(self): return False def new_req_logits_processor(self, params): + if (params.extra_args or {}).get("shared_bi_companion") is not None: + return None # companions generate greedily and are never traced descriptor = (params.extra_args or {}).get("shared_l2") if descriptor is None: raise ValueError("L2 sampling requires explicit request metadata") @@ -301,7 +425,11 @@ def __init__(self, worker, request, output_directory): ) self.row_by_sample = {sample: row for row, sample in enumerate(request["sample_ids"])} self.request_rows, self.progress = {}, {} - self.current, self.schedule = [], [] + # Batch-invariance requests run untraced companions next to the traced + # target; their tokens share every step but are never recorded. + self.companions = request.get("companions") + self.untraced = set() + self.current, self.current_index, self.packed, self.schedule = [], [], [], [] self.logits = torch.empty(self.batch, 16, 151936, dtype=torch.bfloat16) self.local_logits = torch.empty(self.batch, 16, 37984, dtype=torch.bfloat16) self.logprobs = torch.empty(self.batch, 16, dtype=torch.float32) @@ -339,7 +467,21 @@ def subnode(self, layer_index, name, packed): def before_execute(self, scheduled): for new in scheduled.scheduled_new_reqs: - descriptor = (new.sampling_params.extra_args or {}).get("shared_l2", {}) + extra = new.sampling_params.extra_args or {} + companion = extra.get("shared_bi_companion") + if companion is not None: + if ( + self.companions is None + or companion.get("request_sha256") != self.request["request_sha256"] + or new.prompt_token_ids != self.companions[companion["index"]] + or new.num_computed_tokens != 0 + or new.req_id in self.untraced + ): + raise ValueError("Untraced companion does not belong to this request") + self.untraced.add(new.req_id) + self.progress[new.req_id] = 0 + continue + descriptor = extra.get("shared_l2", {}) if descriptor.get("request_sha256") != self.request["request_sha256"]: raise ValueError("Scheduler request belongs to a different L2 case") row = self.row_by_sample[descriptor["sample_id"]] @@ -354,11 +496,29 @@ def before_execute(self, scheduled): self.scheduled = scheduled if not scheduled.total_num_scheduled_tokens: return - if len(scheduled.num_scheduled_tokens) != self.batch or scheduled.preempted_req_ids: + scheduled_ids = set(scheduled.num_scheduled_tokens) + traced = scheduled_ids & set(self.request_rows) + unfinished = { + req_id + for req_id in self.request_rows + if self.progress[req_id] < self.prompt_length + 15 + } + # A batch-invariance target may wait for its arrival step; once it has + # started it must take part in every step until its last input. + started = {req_id for req_id in unfinished if self.progress[req_id] > 0} + if ( + scheduled.preempted_req_ids + or scheduled_ids - traced - self.untraced + or (self.companions is None and len(traced) != self.batch) + or (self.companions is not None and not started <= traced <= unfinished) + ): raise RuntimeError("The actual native step did not preserve its required batch") step = {} for req_id, count in scheduled.num_scheduled_tokens.items(): computed = self.progress[req_id] + if req_id in self.untraced: + step[req_id] = (None, computed, count) + continue expected = next_scheduled_tokens( computed, self.prompt_length, self.request["prefill_chunks"] ) @@ -373,23 +533,31 @@ def begin_forward(self, input_ids, positions): ids = list(self.worker.model_runner.input_batch.req_ids) if set(ids) != set(self.step): raise ValueError("Actual model input batch differs from SchedulerOutput") - self.current = [] + self.current, self.current_index, self.packed = [], [], [] begin = 0 - for req_id in ids: + for index, req_id in enumerate(ids): row, computed, count = self.step[req_id] end = begin + count if positions[begin:end].cpu().tolist() != list(range(computed, computed + count)): raise ValueError("Native absolute positions differ from the scheduled offsets") + self.packed.append((begin, end, computed)) + if row is None: + self.progress[req_id] = computed + count + begin = end + continue known = self.request["prompt_tokens"][row] + self.responses[row].tolist() if input_ids[begin:end].cpu().tolist() != known[computed : computed + count]: raise ValueError( "Native engine consumed tokens other than the prompt/frozen response prefix" ) self.current.append((row, begin, end, computed)) + self.current_index.append(index) self.progress[req_id] = computed + count begin = end if begin != self.scheduled.total_num_scheduled_tokens: raise ValueError("Packed model input does not cover actual scheduled tokens") + if not self.current: + return self.schedule.append( { "samples": [self.request["sample_ids"][row] for row, _, _, _ in self.current], @@ -431,14 +599,16 @@ def layer(self, layer_index, output, layer): indices, cu, _reset = metadata_sequences( meta, self.scheduled.total_num_scheduled_tokens ) - if cu.tolist() != [0] + [end for _, _, end, _ in self.current]: + if cu.tolist() != [0] + [end for _, end, _ in self.packed]: raise ValueError("GDN metadata reordered sequences without matching model inputs") conv, recurrent = module.kv_cache if not is_conv_state_dim_first(): conv = conv.transpose(-1, -2) - for (row, begin, end, computed), slot in zip( - self.current, indices.tolist(), strict=True - ): + slots = indices.tolist() + if len(slots) != len(self.packed): + raise ValueError("GDN metadata does not cover every packed sequence") + for (row, begin, end, computed), index in zip(self.current, self.current_index): + slot = slots[index] if computed + end - begin == self.prompt_length + 15: self.states[layer_index]["convolution"][row] = conv[slot].detach().cpu().clone() self.states[layer_index]["recurrent_state"][row] = ( @@ -449,10 +619,10 @@ def layer(self, layer_index, output, layer): meta, cache = metadata[attention.layer_name], attention.kv_cache if not isinstance(meta, SharedAttentionMetadata): raise ValueError("Full attention did not use shared native paged-KV metadata") - if list(meta.query_start_loc) != [0] + [end for _, _, end, _ in self.current]: + if list(meta.query_start_loc) != [0] + [end for _, end, _ in self.packed]: raise ValueError("Full attention boundaries differ from actual packed input") block_size = cache.shape[2] - for index, (row, begin, end, computed) in enumerate(self.current): + for index, (row, begin, end, computed) in zip(self.current_index, self.current): length = computed + end - begin if length != self.prompt_length + 15: continue @@ -472,13 +642,13 @@ def record_logits(self, hidden, local, full): from rl_engine.integrations.qwen3_next_logprob import qwen_logprob_contract from rl_engine.kernels.logprob_contract import LogprobRole - if full.shape != (len(self.current), 151936) or local.shape != (len(self.current), 37984): + if full.shape != (len(self.packed), 151936) or local.shape != (len(self.packed), 37984): raise ValueError( "Native sampling rows do not correspond one-to-one with active requests" ) eligible = [ (index, row, computed + end - begin - self.prompt_length) - for index, (row, begin, end, computed) in enumerate(self.current) + for index, (row, begin, end, computed) in zip(self.current_index, self.current) if computed + end - begin >= self.prompt_length ] if not eligible: @@ -775,3 +945,102 @@ def run_rollout_cases(cases): def run_rollout_case(request, output): run_rollout_cases([(request, output)]) + + +def batch_invariance_descriptors(request): + """Scheduler descriptors for one version 2 request, in FCFS submission order.""" + common = { + "request_sha256": request["request_sha256"], + "total_requests": 1 + len(request["companions"]), + "chunk_size": request["chunk_size"], + } + responses = request["response_tokens"] + target = { + **common, + "bi": True, + "sample_id": request["sample_ids"][0], + "batch_size": 1, + "prompt_length": len(request["prompt_tokens"][0]), + "prefill_chunks": request["prefill_chunks"], + "response_tokens": None if responses is None else responses[0], + "arrival_step": request["arrival_steps"]["target"], + } + companions = [ + ( + prompt, + count, + { + **common, + "index": index, + "arrival_step": request["arrival_steps"]["companions"][index], + }, + ) + for index, (prompt, count) in enumerate( + zip(request["companions"], request["companion_max_tokens"], strict=True) + ) + ] + entries = [(request["prompt_tokens"][0], RESPONSE_LENGTH, target, "shared_l2")] + [ + (prompt, count, descriptor, "shared_bi_companion") + for prompt, count, descriptor in companions + ] + if request.get("target_last"): + entries = entries[1:] + entries[:1] + return entries + + +def run_batch_invariance_cases(cases): + """Run version 2 cells in one engine; each artifact describes the traced target only.""" + from vllm import SamplingParams + + engine, identity = None, None + try: + for request, output in cases: + validate_request(request) + if request["schema_version"] != 2: + raise ValueError("Batch-invariance cells use version 2 requests") + if engine is None: + identity = request["identity"] + engine = _new_engine(identity) + elif request["identity"] != identity: + raise ValueError("One native rollout process cannot change its identity") + output = Path(output).resolve() + records = engine.collective_rpc( + "configure_l2_trace", args=(request, str(output.with_suffix(".worker-traces"))) + ) + if len(records) != 4 or any( + record["request_sha256"] != request["request_sha256"] for record in records + ): + raise RuntimeError("All TP workers must acknowledge the exact trace request") + entries = batch_invariance_descriptors(request) + sampling = [ + SamplingParams( + temperature=0, + top_p=1, + top_k=-1, + seed=1234, + ignore_eos=True, + max_tokens=count, + detokenize=False, + extra_args={key: descriptor}, + ) + for _, count, descriptor, key in entries + ] + generated = engine.generate( + [{"prompt_token_ids": prompt} for prompt, _, _, _ in entries], + sampling, + use_tqdm=False, + ) + responses = None + for (prompt, count, _, key), item in zip(entries, generated, strict=True): + tokens = list(item.outputs[0].token_ids) + if item.prompt_token_ids != prompt or len(tokens) != count: + raise RuntimeError("Native engine did not complete every request of the cell") + if key == "shared_l2": + responses = [tokens] + records = engine.collective_rpc("collect_l2_trace") + save_tensor_artifact(output, assemble_trace(request, records, responses)) + finally: + if engine is not None: + engine.llm_engine.engine_core.shutdown() + del engine + gc.collect() diff --git a/rl_engine/kernels/gtest/forward_invariance.py b/rl_engine/kernels/gtest/forward_invariance.py index 04d6fed21..0937ce7ea 100644 --- a/rl_engine/kernels/gtest/forward_invariance.py +++ b/rl_engine/kernels/gtest/forward_invariance.py @@ -32,6 +32,7 @@ resolve_tolerance, validate_backend_provenance, ) +from rl_engine.testing.tensor_identity import tensor_bitwise_equal from rl_engine.testing.ws1_workload import ( LogicalBatch, PaddedBatch, @@ -349,7 +350,10 @@ def _compare_logical_tensors( rel_error = abs_error / canonical_fp32.abs().clamp_min(1e-12) max_rel = float(rel_error.max().item()) - passed = bool(torch.allclose(transformed_fp32, canonical_fp32, atol=atol, rtol=rtol)) + if spec.mode == "bitwise": + passed = tensor_bitwise_equal(transformed, canonical) + else: + passed = bool(torch.allclose(transformed_fp32, canonical_fp32, atol=atol, rtol=rtol)) return TensorComparisonDetail( tensor_name=tensor_name, diff --git a/rl_engine/kernels/gtest/qwen3_next_chain_report.py b/rl_engine/kernels/gtest/qwen3_next_chain_report.py index 3326dd0af..20fdbd621 100644 --- a/rl_engine/kernels/gtest/qwen3_next_chain_report.py +++ b/rl_engine/kernels/gtest/qwen3_next_chain_report.py @@ -117,11 +117,14 @@ def build_report( device: str, gpu_name: str | None, compute_capability: str | None, + bi_matrix: Mapping[str, Any] | None = None, ) -> dict[str, Any]: """Build the chain report from L2 matrices and their loaded case artifacts. ``matrices[version]`` is an ``l2-matrix.json`` report; ``artifacts[version] [case_id][role]`` is the loaded rollout/recompute artifact of that case. + ``bi_matrix`` (a ``bi-matrix.json`` from the batch-invariance stage) adds a + ``batch_invariance`` section and must pass for the report to pass. """ if not matrices or set(matrices) - set(WEIGHT_VERSIONS): raise ValueError(f"Weight versions must be a subset of {WEIGHT_VERSIONS}") @@ -204,6 +207,7 @@ def build_report( first is None and all(matrix["status"] == "passed" for matrix in matrices.values()) and all(item.passed for item in invariance) + and (bi_matrix is None or bi_matrix["status"] == "passed") ) report = ChainGateReport( backend_profile=PROFILE, @@ -264,5 +268,26 @@ def build_report( first_drift_cell=None if first is None else first[3], first_drift_layer=None if first is None else first[1], first_drift_family=None if first is None else first[2], + batch_invariance=None if bi_matrix is None else batch_invariance_section(bi_matrix), ) return payload + + +def batch_invariance_section(matrix: Mapping[str, Any]) -> dict[str, Any]: + """Per-family pass counts and the first failing cell of a ``bi-matrix.json``.""" + families: dict[str, dict[str, int]] = {} + for cell_id in matrix["cell_ids"]: + cell = matrix["cells"][cell_id] + counts = families.setdefault(cell["family"], {"cells": 0, "passed": 0}) + counts["cells"] += 1 + counts["passed"] += int(cell["passed"]) + first = matrix.get("first_failure") + return { + "suite": matrix["suite"], + "passed": matrix["status"] == "passed", + "families": families, + "first_failure": first, + "first_failure_family": ( + None if first is None else matrix["cells"][first]["first_drift_family"] + ), + } diff --git a/rl_engine/testing/qwen3_next_bi_cases.py b/rl_engine/testing/qwen3_next_bi_cases.py new file mode 100644 index 000000000..60004877b --- /dev/null +++ b/rl_engine/testing/qwen3_next_bi_cases.py @@ -0,0 +1,370 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Batch-invariance cells for the Qwen3-Next full model (RFC #428 ``batch_invariance_ci``). + +Every cell runs one *target* sequence through the native rollout, optionally +next to *companion* sequences, and compares the target bitwise - response +tokens, BF16 logits, FP32 logprobs, the embedding and every layer, sub-node +and recurrent/KV-state digest - with a *reference*: the same target, alone, +with the same prefill chunk plan. A cell passes only if nothing about the +target changed. Families: + +* ``concurrency`` - N = 1, 2, 4, 8, 16, 32, 64 requests in one engine, the + target first or last, all arriving together; +* ``interference`` - companion content, lengths and arrival (during the + target's prefill, during its decode, after it) varied around one target; +* ``chunk`` - the target alone across prefill chunk boundaries (prompt + lengths at and around 64, 128, 528 and 1024 tokens, chunk 64 or 128, first + chunk 1, 63 or 65), compared with the replay engine's full-sequence + recompute of the same tokens: the train/rollout boundary itself; +* ``packing`` - the replay engine packs the target with companions into one + THD sequence batch (offsets and padding varied) and the target must equal + its recompute alone. + +Prompts repeat the four pinned L2 texts to any length, so a cell is fully +determined by its parameters. :func:`cells` is pinned by +``qwen3_next_bi_manifest.json``. +""" + +from __future__ import annotations + +import hashlib +import json +from dataclasses import asdict, dataclass +from pathlib import Path + +from rl_engine.testing.qwen3_next_l2 import json_digest + +MANIFEST = Path(__file__).with_name("qwen3_next_bi_manifest.json") +MAX_MODEL_LEN = 1040 +RESPONSE_LENGTH = 16 +CONCURRENCY = (1, 2, 4, 8, 16, 32, 64) +CHUNK_PROMPTS = (63, 64, 65, 127, 128, 129, 527, 528, 529, 1023, 1024) +CHUNK_SIZES = (64, 128) +FIRST_CHUNKS = (1, 63, 65) + + +@dataclass(frozen=True) +class Sequence: + """One request: which pinned text it repeats, how long its prompt is, when it arrives.""" + + text: int + prompt_length: int + max_tokens: int = RESPONSE_LENGTH + arrival_step: int = 0 + offset: int = 0 + + +@dataclass(frozen=True) +class Cell: + cell_id: str + family: str + target: Sequence + first_chunk: int + chunk_size: int + companions: tuple[Sequence, ...] = () + target_last: bool = False + engine: str = "rollout" + + def chunk_plan(self): + """The target's exact prefill chunks: first chunk, then fixed chunks, then the rest.""" + remaining, plan = self.target.prompt_length, [] + size = min(self.first_chunk, remaining) + while remaining: + plan.append(size) + remaining -= size + size = min(self.chunk_size, remaining) + return plan + + def reference(self): + """The same target, alone, with the same chunk plan.""" + return Cell( + cell_id=f"ref-{self.target.text}-{self.target.prompt_length}" + f"-{self.target.offset}-{self.first_chunk}-{self.chunk_size}", + family="reference", + target=self.target, + first_chunk=self.first_chunk, + chunk_size=self.chunk_size, + engine=self.engine, + ) + + def to_dict(self): + return asdict(self) + + +def _companions(count, *, seed, lengths=(37, 256, 512, 1000), arrival=0, tokens=RESPONSE_LENGTH): + return tuple( + Sequence( + text=(seed + index) % 4, + prompt_length=lengths[(seed + index) % len(lengths)], + max_tokens=tokens, + arrival_step=arrival, + offset=7 * (index + 1), + ) + for index in range(count) + ) + + +def cells(): + out = [] + target = Sequence(text=0, prompt_length=256) + for n in CONCURRENCY: + for last in (False, True) if n > 1 else (False,): + out.append( + Cell( + cell_id=f"concurrency-n{n:02d}-{'last' if last else 'first'}", + family="concurrency", + target=target, + first_chunk=64, + chunk_size=64, + companions=_companions(n - 1, seed=n), + target_last=last, + ) + ) + interference = { + "same-text": _companions(3, seed=0, lengths=(256,)), + "short-companions": _companions(7, seed=1, lengths=(1, 2, 3, 5)), + "long-companions": _companions(3, seed=2, lengths=(1000, 1024)), + "mixed-lengths": _companions(15, seed=3, lengths=(8, 63, 129, 777, 1024)), + "arrive-in-prefill": _companions(4, seed=4, arrival=2), + "arrive-in-decode": _companions(4, seed=5, arrival=6), + # The 256-token target prefills in four steps and decodes in fifteen: step 18 + # is its last decode. + "arrive-at-last-decode": _companions(4, seed=6, arrival=18), + "long-generations": _companions(4, seed=7, tokens=24), + "single-token-generations": _companions(8, seed=8, tokens=1), + } + for name, companions in interference.items(): + out.append( + Cell( + cell_id=f"interference-{name}", + family="interference", + target=target, + first_chunk=64, + chunk_size=64, + companions=companions, + ) + ) + out.append( + Cell( + cell_id="interference-target-last-arrival", + family="interference", + target=Sequence(text=0, prompt_length=256, arrival_step=3), + first_chunk=64, + chunk_size=64, + companions=_companions(4, seed=9), + ) + ) + for prompt in CHUNK_PROMPTS: + for chunk in CHUNK_SIZES: + for first in FIRST_CHUNKS: + out.append( + Cell( + cell_id=f"chunk-p{prompt:04d}-c{chunk}-f{first:02d}", + family="chunk", + target=Sequence(text=prompt % 4, prompt_length=prompt), + first_chunk=first, + chunk_size=chunk, + ) + ) + packing = { + "two-sequences": _companions(1, seed=10), + "four-sequences": _companions(3, seed=11), + "eight-mixed": _companions(7, seed=12, lengths=(8, 63, 129, 777)), + "one-token-neighbours": _companions(3, seed=13, lengths=(1,)), + } + for name, companions in packing.items(): + for last in (False, True): + out.append( + Cell( + cell_id=f"packing-{name}-{'last' if last else 'first'}", + family="packing", + target=target, + first_chunk=target.prompt_length, + chunk_size=target.prompt_length, + companions=companions, + target_last=last, + engine="replay", + ) + ) + for cell in out: + validate(cell) + if len({cell.cell_id for cell in out}) != len(out): + raise ValueError("Batch-invariance cell IDs must be unique") + return out + + +def quick_cells(): + """A CI-sized subset: one cell per family, plus the largest concurrency.""" + keep = { + "concurrency-n04-last", + "concurrency-n64-first", + "interference-arrive-in-decode", + "chunk-p0129-c64-f63", + "packing-four-sequences-last", + } + return [cell for cell in cells() if cell.cell_id in keep] + + +def validate(cell): + sequences = (cell.target, *cell.companions) + if any( + s.prompt_length < 1 + or s.max_tokens < 1 + or s.prompt_length + s.max_tokens > MAX_MODEL_LEN + or s.text not in range(4) + or s.arrival_step < 0 + for s in sequences + ): + raise ValueError(f"{cell.cell_id}: a sequence does not fit the pinned rollout profile") + if cell.target.max_tokens != RESPONSE_LENGTH: + raise ValueError(f"{cell.cell_id}: the target always generates sixteen tokens") + if len(sequences) > 64: + raise ValueError(f"{cell.cell_id}: at most 64 concurrent requests are certified") + if cell.first_chunk < 1 or cell.chunk_size < 1: + raise ValueError(f"{cell.cell_id}: chunks must be positive") + if cell.engine not in ("rollout", "replay"): + raise ValueError(f"{cell.cell_id}: unknown engine {cell.engine}") + + +def prompt_tokens(encoded_texts, sequence): + """The pinned text ``sequence.text`` repeated, rotated by ``offset``, cut to length.""" + row = encoded_texts[sequence.text] + start = sequence.offset % len(row) + rotated = row[start:] + row[:start] + return (rotated * ((sequence.prompt_length + len(rotated) - 1) // len(rotated)))[ + : sequence.prompt_length + ] + + +def manifest_payload(): + entries = [cell.to_dict() for cell in cells()] + return { + "schema_version": 1, + "description": "Qwen3-Next batch-invariance cells; regenerate with " + "python -m rl_engine.testing.qwen3_next_bi_cases --write", + "cells": entries, + "cells_sha256": hashlib.sha256( + json.dumps(entries, sort_keys=True, separators=(",", ":")).encode() + ).hexdigest(), + } + + +def load_manifest(): + payload = json.loads(MANIFEST.read_text()) + if payload != json.loads(json.dumps(manifest_payload())): + raise ValueError("qwen3_next_bi_manifest.json differs from the generated cells") + return payload + + +if __name__ == "__main__": + import sys + + text = json.dumps(manifest_payload(), indent=2) + "\n" + if "--write" in sys.argv: + MANIFEST.write_text(text) + elif not MANIFEST.is_file() or MANIFEST.read_text() != text: + raise SystemExit("qwen3_next_bi_manifest.json is stale; rerun with --write") + + +# --------------------------------------------------------------------------- # +# Worker requests +# --------------------------------------------------------------------------- # + + +def _common(case_id, role, identity, prompt, responses, trace_layers): + length = len(prompt) + return { + "case_id": case_id, + "role": role, + "identity": identity, + "sample_ids": [0], + "prompt_tokens": [prompt], + "response_tokens": None if responses is None else [responses], + "response_length": RESPONSE_LENGTH, + "prediction_positions": list(range(length - 1, length + RESPONSE_LENGTH - 1)), + "consumed_response_tokens": RESPONSE_LENGTH - 1, + "consumed_input_tokens": None if responses is None else [prompt + responses[:-1]], + "state_position": length + RESPONSE_LENGTH - 2, + "logits_dtype": "bfloat16", + "logprobs_dtype": "float32", + "vocab_size": 151936, + "trace_layers": list(trace_layers or []), + "required_layer_digests": list(range(48)), + "generation": { + "temperature": 0.0, + "top_p": 1.0, + "top_k": -1, + "seed": 1234, + "ignore_eos": True, + }, + } + + +def rollout_request(cell, encoded_texts, identity, *, trace_layers=None): + """Version 2 rollout request: the generating target plus untraced companions.""" + if cell.engine != "rollout": + raise ValueError(f"{cell.cell_id} is not a rollout cell") + plan = cell.chunk_plan() + request = { + "schema_version": 2, + **_common( + cell.cell_id, + "rollout", + identity, + prompt_tokens(encoded_texts, cell.target), + None, + trace_layers, + ), + "rollout_prefill_chunks": plan, + "prefill_chunks": plan, + "execution": { + "rollout_mode": "autoregressive", + "prefill_chunks": plan, + "response_mode": "generate", + }, + "companions": [prompt_tokens(encoded_texts, s) for s in cell.companions], + "companion_max_tokens": [s.max_tokens for s in cell.companions], + "arrival_steps": { + "target": cell.target.arrival_step, + "companions": [s.arrival_step for s in cell.companions], + }, + "chunk_size": cell.chunk_size, + "target_last": cell.target_last, + } + request["request_sha256"] = json_digest(request) + return request + + +def replay_request(case_id, prompt, responses, identity, *, packing=None, trace_layers=None): + """Version 1 recompute request for one target; ``packing`` adds untraced neighbours.""" + if len(responses) != RESPONSE_LENGTH: + raise ValueError("Replay needs the target's sixteen response tokens") + request = { + "schema_version": 1, + **_common(case_id, "recompute", identity, prompt, responses, trace_layers), + "rollout_prefill_chunks": [len(prompt)], + "prefill_chunks": None, + "execution": { + "recompute_mode": "full_from_tokens", + "input_sequence_length": len(prompt) + RESPONSE_LENGTH - 1, + }, + } + if packing is not None: + request["packing"] = packing + request["request_sha256"] = json_digest(request) + return request + + +def packing_layout(cell, encoded_texts, target_tokens): + """The replay packing of one cell: neighbours are prompt plus continuation tokens.""" + neighbours = [ + prompt_tokens( + encoded_texts, + Sequence(s.text, s.prompt_length + s.max_tokens - 1, offset=s.offset), + ) + for s in cell.companions + ] + sequences = (neighbours + [target_tokens]) if cell.target_last else [target_tokens, *neighbours] + return {"sequences": sequences, "target_index": len(neighbours) if cell.target_last else 0} diff --git a/rl_engine/testing/qwen3_next_bi_manifest.json b/rl_engine/testing/qwen3_next_bi_manifest.json new file mode 100644 index 000000000..182f4d196 --- /dev/null +++ b/rl_engine/testing/qwen3_next_bi_manifest.json @@ -0,0 +1,3857 @@ +{ + "schema_version": 1, + "description": "Qwen3-Next batch-invariance cells; regenerate with python -m rl_engine.testing.qwen3_next_bi_cases --write", + "cells": [ + { + "cell_id": "concurrency-n01-first", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "concurrency-n02-first", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "concurrency-n02-last", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + } + ], + "target_last": true, + "engine": "rollout" + }, + { + "cell_id": "concurrency-n04-first", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "concurrency-n04-last", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + } + ], + "target_last": true, + "engine": "rollout" + }, + { + "cell_id": "concurrency-n08-first", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 49 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "concurrency-n08-last", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 49 + } + ], + "target_last": true, + "engine": "rollout" + }, + { + "cell_id": "concurrency-n16-first", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 49 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 56 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 63 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 70 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 77 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 84 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 91 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 98 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 105 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "concurrency-n16-last", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 49 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 56 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 63 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 70 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 77 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 84 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 91 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 98 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 105 + } + ], + "target_last": true, + "engine": "rollout" + }, + { + "cell_id": "concurrency-n32-first", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 49 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 56 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 63 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 70 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 77 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 84 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 91 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 98 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 105 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 112 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 119 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 126 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 133 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 140 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 147 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 154 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 161 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 168 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 175 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 182 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 189 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 196 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 203 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 210 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 217 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "concurrency-n32-last", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 49 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 56 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 63 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 70 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 77 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 84 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 91 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 98 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 105 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 112 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 119 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 126 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 133 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 140 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 147 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 154 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 161 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 168 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 175 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 182 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 189 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 196 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 203 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 210 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 217 + } + ], + "target_last": true, + "engine": "rollout" + }, + { + "cell_id": "concurrency-n64-first", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 49 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 56 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 63 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 70 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 77 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 84 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 91 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 98 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 105 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 112 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 119 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 126 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 133 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 140 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 147 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 154 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 161 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 168 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 175 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 182 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 189 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 196 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 203 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 210 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 217 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 224 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 231 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 238 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 245 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 252 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 259 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 266 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 273 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 280 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 287 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 294 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 301 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 308 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 315 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 322 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 329 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 336 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 343 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 350 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 357 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 364 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 371 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 378 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 385 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 392 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 399 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 406 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 413 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 420 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 427 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 434 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 441 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "concurrency-n64-last", + "family": "concurrency", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 49 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 56 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 63 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 70 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 77 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 84 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 91 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 98 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 105 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 112 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 119 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 126 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 133 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 140 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 147 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 154 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 161 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 168 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 175 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 182 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 189 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 196 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 203 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 210 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 217 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 224 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 231 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 238 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 245 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 252 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 259 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 266 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 273 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 280 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 287 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 294 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 301 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 308 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 315 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 322 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 329 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 336 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 343 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 350 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 357 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 364 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 371 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 378 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 385 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 392 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 399 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 406 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 413 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 420 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 427 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 434 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 441 + } + ], + "target_last": true, + "engine": "rollout" + }, + { + "cell_id": "interference-same-text", + "family": "interference", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "interference-short-companions", + "family": "interference", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 1, + "prompt_length": 2, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 2, + "prompt_length": 3, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 3, + "prompt_length": 5, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 0, + "prompt_length": 1, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 1, + "prompt_length": 2, + "max_tokens": 16, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 2, + "prompt_length": 3, + "max_tokens": 16, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 3, + "prompt_length": 5, + "max_tokens": 16, + "arrival_step": 0, + "offset": 49 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "interference-long-companions", + "family": "interference", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 2, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 3, + "prompt_length": 1024, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 0, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "interference-mixed-lengths", + "family": "interference", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 3, + "prompt_length": 777, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 0, + "prompt_length": 1024, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 1, + "prompt_length": 8, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 2, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 3, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 0, + "prompt_length": 777, + "max_tokens": 16, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 1, + "prompt_length": 1024, + "max_tokens": 16, + "arrival_step": 0, + "offset": 49 + }, + { + "text": 2, + "prompt_length": 8, + "max_tokens": 16, + "arrival_step": 0, + "offset": 56 + }, + { + "text": 3, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 63 + }, + { + "text": 0, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 70 + }, + { + "text": 1, + "prompt_length": 777, + "max_tokens": 16, + "arrival_step": 0, + "offset": 77 + }, + { + "text": 2, + "prompt_length": 1024, + "max_tokens": 16, + "arrival_step": 0, + "offset": 84 + }, + { + "text": 3, + "prompt_length": 8, + "max_tokens": 16, + "arrival_step": 0, + "offset": 91 + }, + { + "text": 0, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 98 + }, + { + "text": 1, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 105 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "interference-arrive-in-prefill", + "family": "interference", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 2, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 2, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 2, + "offset": 21 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 2, + "offset": 28 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "interference-arrive-in-decode", + "family": "interference", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 6, + "offset": 7 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 6, + "offset": 14 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 6, + "offset": 21 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 6, + "offset": 28 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "interference-arrive-at-last-decode", + "family": "interference", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 18, + "offset": 7 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 18, + "offset": 14 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 18, + "offset": 21 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 18, + "offset": 28 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "interference-long-generations", + "family": "interference", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 24, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 24, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 24, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 24, + "arrival_step": 0, + "offset": 28 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "interference-single-token-generations", + "family": "interference", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 0, + "prompt_length": 37, + "max_tokens": 1, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 1, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 1, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 1, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 1, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 1, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 1, + "arrival_step": 0, + "offset": 49 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 1, + "arrival_step": 0, + "offset": 56 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "interference-target-last-arrival", + "family": "interference", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 3, + "offset": 0 + }, + "first_chunk": 64, + "chunk_size": 64, + "companions": [ + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + } + ], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0063-c64-f01", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0063-c64-f63", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0063-c64-f65", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0063-c128-f01", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0063-c128-f63", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0063-c128-f65", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0064-c64-f01", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 64, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0064-c64-f63", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 64, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0064-c64-f65", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 64, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0064-c128-f01", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 64, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0064-c128-f63", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 64, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0064-c128-f65", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 64, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0065-c64-f01", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 65, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0065-c64-f63", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 65, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0065-c64-f65", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 65, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0065-c128-f01", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 65, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0065-c128-f63", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 65, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0065-c128-f65", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 65, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0127-c64-f01", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 127, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0127-c64-f63", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 127, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0127-c64-f65", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 127, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0127-c128-f01", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 127, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0127-c128-f63", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 127, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0127-c128-f65", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 127, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0128-c64-f01", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 128, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0128-c64-f63", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 128, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0128-c64-f65", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 128, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0128-c128-f01", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 128, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0128-c128-f63", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 128, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0128-c128-f65", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 128, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0129-c64-f01", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0129-c64-f63", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0129-c64-f65", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0129-c128-f01", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0129-c128-f63", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0129-c128-f65", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0527-c64-f01", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 527, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0527-c64-f63", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 527, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0527-c64-f65", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 527, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0527-c128-f01", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 527, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0527-c128-f63", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 527, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0527-c128-f65", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 527, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0528-c64-f01", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 528, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0528-c64-f63", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 528, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0528-c64-f65", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 528, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0528-c128-f01", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 528, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0528-c128-f63", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 528, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0528-c128-f65", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 528, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0529-c64-f01", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 529, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0529-c64-f63", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 529, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0529-c64-f65", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 529, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0529-c128-f01", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 529, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0529-c128-f63", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 529, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p0529-c128-f65", + "family": "chunk", + "target": { + "text": 1, + "prompt_length": 529, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p1023-c64-f01", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 1023, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p1023-c64-f63", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 1023, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p1023-c64-f65", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 1023, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p1023-c128-f01", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 1023, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p1023-c128-f63", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 1023, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p1023-c128-f65", + "family": "chunk", + "target": { + "text": 3, + "prompt_length": 1023, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p1024-c64-f01", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 1024, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p1024-c64-f63", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 1024, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p1024-c64-f65", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 1024, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 64, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p1024-c128-f01", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 1024, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 1, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p1024-c128-f63", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 1024, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 63, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "chunk-p1024-c128-f65", + "family": "chunk", + "target": { + "text": 0, + "prompt_length": 1024, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 65, + "chunk_size": 128, + "companions": [], + "target_last": false, + "engine": "rollout" + }, + { + "cell_id": "packing-two-sequences-first", + "family": "packing", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 256, + "chunk_size": 256, + "companions": [ + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + } + ], + "target_last": false, + "engine": "replay" + }, + { + "cell_id": "packing-two-sequences-last", + "family": "packing", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 256, + "chunk_size": 256, + "companions": [ + { + "text": 2, + "prompt_length": 512, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + } + ], + "target_last": true, + "engine": "replay" + }, + { + "cell_id": "packing-four-sequences-first", + "family": "packing", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 256, + "chunk_size": 256, + "companions": [ + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + } + ], + "target_last": false, + "engine": "replay" + }, + { + "cell_id": "packing-four-sequences-last", + "family": "packing", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 256, + "chunk_size": 256, + "companions": [ + { + "text": 3, + "prompt_length": 1000, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 0, + "prompt_length": 37, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 1, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + } + ], + "target_last": true, + "engine": "replay" + }, + { + "cell_id": "packing-eight-mixed-first", + "family": "packing", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 256, + "chunk_size": 256, + "companions": [ + { + "text": 0, + "prompt_length": 8, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 3, + "prompt_length": 777, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 0, + "prompt_length": 8, + "max_tokens": 16, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 1, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 2, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 49 + } + ], + "target_last": false, + "engine": "replay" + }, + { + "cell_id": "packing-eight-mixed-last", + "family": "packing", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 256, + "chunk_size": 256, + "companions": [ + { + "text": 0, + "prompt_length": 8, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 1, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 2, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + }, + { + "text": 3, + "prompt_length": 777, + "max_tokens": 16, + "arrival_step": 0, + "offset": 28 + }, + { + "text": 0, + "prompt_length": 8, + "max_tokens": 16, + "arrival_step": 0, + "offset": 35 + }, + { + "text": 1, + "prompt_length": 63, + "max_tokens": 16, + "arrival_step": 0, + "offset": 42 + }, + { + "text": 2, + "prompt_length": 129, + "max_tokens": 16, + "arrival_step": 0, + "offset": 49 + } + ], + "target_last": true, + "engine": "replay" + }, + { + "cell_id": "packing-one-token-neighbours-first", + "family": "packing", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 256, + "chunk_size": 256, + "companions": [ + { + "text": 1, + "prompt_length": 1, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 2, + "prompt_length": 1, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 3, + "prompt_length": 1, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + } + ], + "target_last": false, + "engine": "replay" + }, + { + "cell_id": "packing-one-token-neighbours-last", + "family": "packing", + "target": { + "text": 0, + "prompt_length": 256, + "max_tokens": 16, + "arrival_step": 0, + "offset": 0 + }, + "first_chunk": 256, + "chunk_size": 256, + "companions": [ + { + "text": 1, + "prompt_length": 1, + "max_tokens": 16, + "arrival_step": 0, + "offset": 7 + }, + { + "text": 2, + "prompt_length": 1, + "max_tokens": 16, + "arrival_step": 0, + "offset": 14 + }, + { + "text": 3, + "prompt_length": 1, + "max_tokens": 16, + "arrival_step": 0, + "offset": 21 + } + ], + "target_last": true, + "engine": "replay" + } + ], + "cells_sha256": "f4709588a5d1e4fdceffc74df74d8c5980f8321e484906324e507d5b55ebcfae" +} diff --git a/scripts/qwen3_next_batch_invariance.py b/scripts/qwen3_next_batch_invariance.py new file mode 100644 index 000000000..df9381dac --- /dev/null +++ b/scripts/qwen3_next_batch_invariance.py @@ -0,0 +1,337 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Stage ``bi``: the Qwen3-Next batch-invariance matrix (RFC #428 ``batch_invariance_ci``). + +Runs after a passed ``l2`` stage, under the same certified provider profile +(``VLLM_BATCH_INVARIANT=1``). The cells are ``rl_engine/testing/qwen3_next_bi_cases.py`` +(pinned by ``qwen3_next_bi_manifest.json``); ``bi_suite: quick`` in the manifest +runs the CI subset. Two independent worker processes, as in ``l2``: + +1. rollout: every rollout cell and its reference (the target alone, same chunk + plan) in one native vLLM engine; the target generates its sixteen tokens; +2. replay: for every ``chunk`` cell, the VIME recompute of the target's tokens; + for every ``packing`` cell, the target packed with its neighbours and alone. + +Then, bitwise on response tokens, logits, logprobs, the embedding and every +layer, sub-node and state digest: concurrency and interference cells against +their reference, chunk cells against their replay, packing cells against the +replay alone. ``bi-matrix.json`` records every cell, the first divergence and +its layer family. +""" + +import argparse +import json +import os +import shutil +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) +sys.path.insert(0, str(ROOT / "scripts")) + +from qwen3_next_contract import evidence_record, result_envelope, validate_result # noqa: E402 +from qwen3_next_l2 import ( # noqa: E402 + check_ready, + model_identity, + run_process, + verify_before_model_load, + wait_gpu_release, +) +from qwen3_next_runtime import ( # noqa: E402 + atomic_json, + failure_fields, + file_digest, + memory_limits, + parse_memory_request, + require_predecessor, + verify_environment, +) + +from rl_engine.kernels.gtest.qwen3_next_chain_report import family_of # noqa: E402 +from rl_engine.testing import qwen3_next_bi_cases as bi # noqa: E402 +from rl_engine.testing.qwen3_next_binary import ( # noqa: E402 + archive_provider_binary, + validate_binary_record, + verify_imported_provider, +) +from rl_engine.testing.qwen3_next_l2 import ( # noqa: E402 + FROZEN_TEXTS, + compare_artifacts, + compare_batch_prefill, + first_divergent_node, + load_tensor_artifact, + validate_model_artifact, +) + + +def suite_cells(manifest): + suite = manifest.get("bi_suite", "full") + if suite not in ("full", "quick"): + raise ValueError(f"Unknown batch-invariance suite {suite!r}") + bi.load_manifest() + return bi.quick_cells() if suite == "quick" else bi.cells() + + +def encoded_texts(manifest): + from transformers import AutoTokenizer + + tokenizer = AutoTokenizer.from_pretrained( + manifest["checkpoint"], local_files_only=True, trust_remote_code=False + ) + return [tokenizer.encode(text, add_special_tokens=False) for text in FROZEN_TEXTS] + + +def packing_source(cell): + """The rollout that defines a packing cell's target tokens: the target alone, 64/64.""" + return bi.Cell( + cell_id=cell.cell_id, + family=cell.family, + target=cell.target, + first_chunk=64, + chunk_size=64, + ).reference() + + +def rollout_requests(manifest, cells): + """References first, each once; then every rollout cell.""" + texts, identity = encoded_texts(manifest), model_identity(manifest) + requests, seen = [], set() + for cell in cells: + reference = packing_source(cell) if cell.engine == "replay" else cell.reference() + for item in (reference, cell): + if item.engine == "rollout" and item.cell_id not in seen: + seen.add(item.cell_id) + requests.append(bi.rollout_request(item, texts, identity)) + return requests + + +def replay_requests(manifest, cells, rollout_dir): + """Chunk-cell replays and packing cells (each with its unpacked replay).""" + texts, identity = encoded_texts(manifest), model_identity(manifest) + requests, seen = [], set() + for cell in cells: + if cell.family not in ("chunk", "packing"): + continue + source = cell.cell_id if cell.family == "chunk" else packing_source(cell).cell_id + artifact = load_tensor_artifact(rollout_dir / f"{source}.pt") + prompt = artifact["prompt_tokens"][0].tolist() + responses = artifact["response_tokens"][0].tolist() + alone = f"replay-{source}" + if alone not in seen: + seen.add(alone) + requests.append(bi.replay_request(alone, prompt, responses, identity)) + if cell.family == "packing": + layout = bi.packing_layout(cell, texts, prompt + responses[:-1]) + requests.append( + bi.replay_request(cell.cell_id, prompt, responses, identity, packing=layout) + ) + return requests + + +def worker(role, manifest_path, output, provider_binary): + """One independent process: run every request of ``role`` into ``output``.""" + manifest = json.loads(Path(manifest_path).read_text()) + module, _capabilities = check_ready() + verify_environment(manifest["environment"], require_venv=True) + output.mkdir(parents=True, exist_ok=True) + verify_before_model_load(manifest, output) + actual_binary = verify_imported_provider(provider_binary, source_tree=manifest["source_tree"]) + cells = suite_cells(manifest) + if role == "rollout": + from rl_engine.integrations.qwen3_next_native_rollout import run_batch_invariance_cases + + requests = rollout_requests(manifest, cells) + run = run_batch_invariance_cases + else: + requests = replay_requests(manifest, cells, output.parent / "rollout") + run = module.run_recompute_cases + pending = [] + for request in requests: + path = output / f"{request['case_id']}.pt" + atomic_json(path.with_suffix(".request.json"), request) + pending.append((request, path)) + run(pending) + records = {} + for request, path in pending: + artifact = load_tensor_artifact(path) + validate_model_artifact(artifact, request) + if artifact.get("provider_binary") != actual_binary: + raise ValueError("A batch-invariance case used a different CUDA provider") + records[request["case_id"]] = {"path": str(path), "sha256": file_digest(path)} + atomic_json( + output / "worker-result.json", + {"status": "passed", "role": role, "pid": os.getpid(), "cases": records}, + ) + + +def judge(cells, output): + rollout, replay = output / "rollout", output / "replay" + load = {} + + def artifact(directory, case_id): + key = (directory, case_id) + if key not in load: + load[key] = load_tensor_artifact(directory / f"{case_id}.pt") + return load[key] + + results = {} + for cell in cells: + if cell.family == "chunk": + lhs, rhs = artifact(rollout, cell.cell_id), artifact(replay, f"replay-{cell.cell_id}") + paired = compare_artifacts(lhs, rhs) + comparisons, against = paired["comparisons"], "replay" + elif cell.family == "packing": + source = packing_source(cell).cell_id + lhs, rhs = artifact(replay, f"replay-{source}"), artifact(replay, cell.cell_id) + comparisons, against = compare_batch_prefill(lhs, rhs), "replay alone" + else: + lhs = artifact(rollout, cell.reference().cell_id) + rhs = artifact(rollout, cell.cell_id) + comparisons, against = compare_batch_prefill(lhs, rhs), "reference alone" + node = first_divergent_node(lhs, rhs) + passed = all(comparisons.values()) + if node is None and not passed: + node = {"layer": None, "node": next(k for k, v in comparisons.items() if not v)} + results[cell.cell_id] = { + "family": cell.family, + "compared_with": against, + "passed": passed, + "comparisons": comparisons, + "first_divergent_node": None if passed else node, + "first_drift_family": (None if passed else family_of(node["layer"], node["node"])), + } + return results + + +def execute(manifest_path, output): + manifest = json.loads(Path(manifest_path).read_text()) + output.mkdir(parents=True, exist_ok=True) + previous = Path(manifest["previous"]) + require_predecessor("bi", previous, Path(manifest["checkpoint"]), manifest) + check_ready() + verify_environment(manifest["environment"], require_venv=True) + memory, requested = memory_limits(), parse_memory_request(manifest["memory"]) + if any( + value is None or value < requested + for value in (memory["effective_bytes"], memory["available_bytes"]) + ): + raise RuntimeError("insufficient_memory: batch-invariance stage budget unavailable") + run_process( + [manifest["python"], str(ROOT / "setup.py"), "build_ext", "--inplace"], + log_path=output / "build.log", + timeout=1800, + ) + provider_binary = archive_provider_binary(ROOT, output, source_tree=manifest["source_tree"]) + validate_binary_record(provider_binary, source_tree=manifest["source_tree"]) + probe = output / "gpu-probe.json" + run_process( + [ + manifest["python"], + str(ROOT / "scripts/qwen3_next_l2.py"), + "--gpu-probe", + "--output", + str(probe), + ], + log_path=output / "gpu-probe.log", + timeout=180, + ) + uuids = json.loads(probe.read_text())["uuids"] + wait_gpu_release(uuids) + atomic_json( + output / "environment.json", + {"environment": manifest["environment"], "memory": memory, "gpu_uuids": uuids}, + ) + for role in ("rollout", "replay"): + run_process( + [ + manifest["python"], + str(Path(__file__)), + "--worker", + role, + "--manifest", + str(manifest_path), + "--output", + str(output / role), + "--provider-binary", + json.dumps(provider_binary), + ], + log_path=output / f"{role}.log", + timeout=8 * 3600, + ) + wait_gpu_release(uuids) + cells = suite_cells(manifest) + results = judge(cells, output) + failed = [cell_id for cell_id, item in results.items() if not item["passed"]] + matrix = { + "schema_version": 1, + "scope": "qwen3_next_batch_invariance", + "status": "passed" if not failed else "failed", + "suite": manifest.get("bi_suite", "full"), + "identity": model_identity(manifest), + "cells_sha256": bi.manifest_payload()["cells_sha256"], + "cell_ids": [cell.cell_id for cell in cells], + "cells": results, + "failed": failed, + "first_failure": failed[0] if failed else None, + "provider_binary": provider_binary, + } + atomic_json(output / "bi-matrix.json", matrix) + prior = json.loads((previous / "result.json").read_text()) + shutil.copyfile(previous / prior["evidence"]["operators"]["path"], output / "operators.json") + evidence = { + name: evidence_record(output / filename, Path(manifest_path).parent) + for name, filename in { + "operators": "operators.json", + "environment": "environment.json", + "provider_binary": "provider-binary.json", + "bi_matrix": "bi-matrix.json", + }.items() + } + if failed: + raise RuntimeError(f"Batch invariance failed in {len(failed)} cells; first: {failed[0]}") + result = result_envelope(manifest, os.environ["QWEN3_NEXT_ATTEMPT"], evidence=evidence) + validate_result( + manifest, + result, + run_dir=Path(manifest_path).parent, + attempt=os.environ["QWEN3_NEXT_ATTEMPT"], + ) + atomic_json(output / "result.json", result) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--manifest", type=Path) + parser.add_argument("--output", type=Path) + parser.add_argument("--worker", choices=("rollout", "replay")) + parser.add_argument("--provider-binary") + parser.add_argument("--check-ready", action="store_true") + args = parser.parse_args() + if args.check_ready: + _, capabilities = check_ready() + print(json.dumps(capabilities)) + return + if args.worker: + worker(args.worker, args.manifest, args.output, json.loads(args.provider_binary)) + return + try: + execute(args.manifest, args.output) + except Exception as exc: + args.output.mkdir(parents=True, exist_ok=True) + atomic_json( + args.output / "result.json", + { + "status": "failed", + "attempt": os.environ.get("QWEN3_NEXT_ATTEMPT"), + "phase": "batch_invariance", + **failure_fields(exc), + }, + ) + raise + + +if __name__ == "__main__": + main() diff --git a/scripts/qwen3_next_chain_report.py b/scripts/qwen3_next_chain_report.py index df01bee54..6cf75b18f 100644 --- a/scripts/qwen3_next_chain_report.py +++ b/scripts/qwen3_next_chain_report.py @@ -52,6 +52,7 @@ def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--last", type=Path, required=True, help="last passed stage run") parser.add_argument("--out", type=Path, required=True) + parser.add_argument("--bi", type=Path, help="passed batch-invariance (bi) stage run") args = parser.parse_args() matrices, artifacts = {}, {} gpu_name = None @@ -74,6 +75,16 @@ def main(): if gpu_name is None and environment.is_file(): gpu_name = json.loads(environment.read_text()).get("gpu_name") identity = manifest["checkpoint_identity"] + bi_matrix = None + if args.bi is not None: + bi_manifest = json.loads((args.bi / "run.json").read_text()) + bi_result = json.loads((args.bi / "result.json").read_text()) + if ( + bi_manifest["stage"] != "bi" + or bi_manifest["source_commit"] != manifest["source_commit"] + ): + raise SystemExit("--bi must name a bi run on the same commit as the chain") + bi_matrix = json.loads((args.bi / bi_result["evidence"]["bi_matrix"]["path"]).read_text()) report = build_report( matrices, artifacts, @@ -84,6 +95,7 @@ def main(): device="cuda", gpu_name=gpu_name, compute_capability=None, + bi_matrix=bi_matrix, ) args.out.parent.mkdir(parents=True, exist_ok=True) args.out.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n") diff --git a/scripts/qwen3_next_contract.py b/scripts/qwen3_next_contract.py index 88d55dd6c..e0642b399 100644 --- a/scripts/qwen3_next_contract.py +++ b/scripts/qwen3_next_contract.py @@ -20,6 +20,7 @@ "train1": "qwen3_next_training_update1", "train2": "qwen3_next_training_update2", "train": "qwen3_next_training_and_reload", + "bi": "qwen3_next_batch_invariance", } # Stages that evaluate the full model against updated weights (an L2 matrix each). TRAIN_STAGES = ("train1", "train2", "train") @@ -232,6 +233,8 @@ def validate_result(manifest, result, *, run_dir=None, attempt=None): required.add("provider_binary") if manifest["stage"] == "layers": required |= {"layers", "checkpoint", "provider_binary"} + if manifest["stage"] == "bi": + required |= {"bi_matrix", "provider_binary"} if manifest["stage"] == "train": required |= {"updates", "reload"} if manifest["stage"] in ("train1", "train2"): @@ -283,6 +286,20 @@ def validate_result(manifest, result, *, run_dir=None, attempt=None): counts.get(key, 0) for key in ("skipped", "errors", "failures") ): raise ValueError("Required operator tests must pass without skips") + if manifest["stage"] == "bi": + from qwen3_next_l2 import model_identity + + from rl_engine.testing import qwen3_next_bi_cases as bi + + matrix = json.loads((root / evidence["bi_matrix"]["path"]).read_text()) + suite = bi.quick_cells() if manifest.get("bi_suite") == "quick" else bi.cells() + if ( + matrix.get("status") != "passed" + or matrix.get("identity") != model_identity(manifest) + or matrix.get("cell_ids") != [cell.cell_id for cell in suite] + or any(not matrix["cells"][cell.cell_id]["passed"] for cell in suite) + ): + raise ValueError("Batch-invariance matrix is incomplete or failed") if manifest["stage"] in ("layers", "l2", *TRAIN_STAGES): from qwen3_next_l2 import model_identity diff --git a/scripts/qwen3_next_publish_evidence.py b/scripts/qwen3_next_publish_evidence.py index 1cb6cb2e7..5ea7ec348 100644 --- a/scripts/qwen3_next_publish_evidence.py +++ b/scripts/qwen3_next_publish_evidence.py @@ -129,9 +129,23 @@ def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--last", type=Path, required=True) parser.add_argument("--chain-report", type=Path) + parser.add_argument( + "--also", + type=Path, + action="append", + default=[], + help="another finished run of the same commit, e.g. the bi stage (repeatable)", + ) parser.add_argument("--out", type=Path, required=True) args = parser.parse_args() runs = chain_runs(args.last) + for extra in args.also: + manifest = json.loads((extra / "run.json").read_text()) + if manifest["source_commit"] != runs[0][0]["source_commit"]: + raise SystemExit(f"{extra} ran another commit") + if manifest["stage"] in {item[0]["stage"] for item in runs}: + raise SystemExit(f"{extra} repeats the stage {manifest['stage']}") + runs.append((manifest, extra.resolve())) if any(not (run / "result.json").is_file() for _, run in runs): raise SystemExit("Every stage on the chain must have finished") publish(runs, args.chain_report, args.out) diff --git a/scripts/qwen3_next_run.py b/scripts/qwen3_next_run.py index db8133d94..75adefce6 100644 --- a/scripts/qwen3_next_run.py +++ b/scripts/qwen3_next_run.py @@ -14,7 +14,8 @@ scheduler gates, which certify the shared provider), ``l2`` (full-model train/prefill/decode raw-bit matrix), then ``train1``, ``train2`` and ``train`` (two optimizer updates, export, L2 on the updated weights, reload and -resumption). A single stage takes ``--previous `` of its predecessor. +resumption). A single stage takes ``--previous `` of its predecessor; +``bi`` (the batch-invariance matrix) runs after a passed ``l2``. Every run directory holds ``run.json`` (the immutable manifest, identified by a fresh ``execution_id``), ``attempts//`` (the stage's output) and, once the @@ -139,6 +140,8 @@ def prepare(stage, previous, args): "status": "prepared", "full_model_l2": "not_evaluated", } + if stage == "bi": + manifest["bi_suite"] = args.bi_suite if args.training_storage_root and stage == "train1": manifest["training_storage"] = str(args.training_storage_root.resolve(strict=True) / run_id) environment = probe_environment(python) @@ -160,7 +163,7 @@ def prepare(stage, previous, args): "path": str(certificate), "sha256": file_digest(certificate), } - elif stage in ("train1", "train2", "train"): + elif stage in ("train1", "train2", "train", "bi"): manifest["scheduler_profile_certificate"] = json.loads((previous / "run.json").read_text())[ "scheduler_profile_certificate" ] @@ -288,7 +291,7 @@ def main(): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter ) - parser.add_argument("--stage", choices=(*ORDER, "all"), required=True) + parser.add_argument("--stage", choices=(*ORDER, "bi", "all"), required=True) parser.add_argument("--runs-root", type=Path, required=True) parser.add_argument("--checkpoint", type=Path, required=True) parser.add_argument("--cuda-home", type=Path, required=True) @@ -308,6 +311,12 @@ def main(): "--memory", help="host memory this allocation grants (e.g. 1280G); default: the stage's budget", ) + parser.add_argument( + "--bi-suite", + choices=("full", "quick"), + default="full", + help="with --stage bi: the whole batch-invariance matrix or the CI subset", + ) parser.add_argument( "--training-storage-root", type=Path, diff --git a/scripts/qwen3_next_runtime.py b/scripts/qwen3_next_runtime.py index 321597ac5..e4985239b 100644 --- a/scripts/qwen3_next_runtime.py +++ b/scripts/qwen3_next_runtime.py @@ -443,6 +443,8 @@ def wait_owned_child(child, interrupted, *, grace_seconds=10): "train1": ("1280G", "06:00:00", "l2", "scripts/qwen3_next_training.py"), "train2": ("1280G", "07:00:00", "train1", "scripts/qwen3_next_training.py"), "train": ("1280G", "09:00:00", "train2", "scripts/qwen3_next_training.py"), + # Batch invariance branches off a passed l2, under the same certified profile. + "bi": ("512G", "06:00:00", "l2", "scripts/qwen3_next_batch_invariance.py"), } diff --git a/tests/check_qwen3_next_bi_requests.py b/tests/check_qwen3_next_bi_requests.py new file mode 100644 index 000000000..4eb2ccaee --- /dev/null +++ b/tests/check_qwen3_next_bi_requests.py @@ -0,0 +1,44 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Batch-invariance rollout requests against the native rollout contract (imports vLLM).""" + +import pytest + +from rl_engine.integrations import qwen3_next_native_rollout as rollout +from rl_engine.testing import qwen3_next_bi_cases as bi + +TEXTS = [list(range(1000 + 50 * i, 1000 + 50 * i + 17 + i)) for i in range(4)] +IDENTITY = { + "architecture_layers": 48, + "topology": {"tp": 4, "pp": 1, "cp": 1, "ep": 1}, + "checkpoint": "/checkpoint", + "provider_config": {"native_rollout_kv_cache_memory_bytes": 4 << 30}, +} + + +def test_every_rollout_cell_builds_a_valid_rollout_request(): + for cell in bi.cells(): + if cell.engine != "rollout": + continue + request = bi.rollout_request(cell, TEXTS, IDENTITY) + assert rollout.validate_request(request) == (1, cell.target.prompt_length) + entries = rollout.batch_invariance_descriptors(request) + assert len(entries) == 1 + len(cell.companions) + target = [entry for entry in entries if entry[3] == "shared_l2"] + assert len(target) == 1 and target[0][2]["prefill_chunks"] == cell.chunk_plan() + assert (entries[-1] if cell.target_last else entries[0])[3] == "shared_l2" + + +def test_requests_reject_companions_that_do_not_fit_the_profile(): + request = bi.rollout_request( + bi.Cell("y", "interference", bi.Sequence(0, 256), 64, 64, (bi.Sequence(1, 8),)), + TEXTS, + IDENTITY, + ) + request["companion_max_tokens"] = [2000] + request["request_sha256"] = bi.json_digest( + {k: v for k, v in request.items() if k != "request_sha256"} + ) + with pytest.raises(ValueError, match="does not fit"): + rollout.validate_request(request) diff --git a/tests/test_qwen3_next_bi_cases.py b/tests/test_qwen3_next_bi_cases.py new file mode 100644 index 000000000..df478f60d --- /dev/null +++ b/tests/test_qwen3_next_bi_cases.py @@ -0,0 +1,94 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Batch-invariance cells, their pinned manifest and the worker requests built from them.""" + +import collections + +import pytest + +from rl_engine.integrations.qwen3_next_native_recompute import packed_layout +from rl_engine.testing import qwen3_next_bi_cases as bi + +TEXTS = [list(range(1000 + 50 * i, 1000 + 50 * i + 17 + i)) for i in range(4)] +IDENTITY = { + "architecture_layers": 48, + "topology": {"tp": 4, "pp": 1, "cp": 1, "ep": 1}, + "checkpoint": "/checkpoint", + "provider_config": {"native_rollout_kv_cache_memory_bytes": 4 << 30}, +} + + +def test_manifest_pins_the_generated_cells(): + payload = bi.load_manifest() + assert len(payload["cells"]) == len(bi.cells()) == 97 + families = collections.Counter(cell.family for cell in bi.cells()) + assert families == {"chunk": 66, "concurrency": 13, "interference": 10, "packing": 8} + + +def test_concurrency_covers_one_to_sixty_four_requests(): + sizes = {1 + len(cell.companions) for cell in bi.cells() if cell.family == "concurrency"} + assert sizes == set(bi.CONCURRENCY) + + +@pytest.mark.parametrize( + "prompt,first,chunk,plan", + [ + (64, 1, 64, [1, 63]), + (65, 63, 64, [63, 2]), + (129, 65, 64, [65, 64]), + (1024, 1, 128, [1] + [128] * 7 + [127]), + (63, 65, 128, [63]), + ], +) +def test_chunk_plan_is_first_chunk_then_fixed_chunks(prompt, first, chunk, plan): + cell = bi.Cell("c", "chunk", bi.Sequence(0, prompt), first, chunk) + assert cell.chunk_plan() == plan and sum(plan) == prompt + + +def test_quick_subset_has_one_cell_per_family_and_the_largest_batch(): + quick = bi.quick_cells() + assert {cell.family for cell in quick} == {"concurrency", "interference", "chunk", "packing"} + assert max(1 + len(cell.companions) for cell in quick) == 64 + + +def test_reference_is_the_target_alone_with_the_same_plan(): + cell = next(c for c in bi.cells() if c.cell_id == "concurrency-n16-last") + reference = cell.reference() + assert reference.companions == () and reference.chunk_plan() == cell.chunk_plan() + assert reference.target == cell.target + + +def test_cells_reject_companions_that_do_not_fit_the_profile(): + cell = bi.Cell( + "x", "interference", bi.Sequence(0, 256), 64, 64, (bi.Sequence(1, 1030, max_tokens=16),) + ) + with pytest.raises(ValueError, match="does not fit"): + bi.validate(cell) + + +def test_prompts_repeat_and_rotate_the_pinned_text(): + tokens = bi.prompt_tokens(TEXTS, bi.Sequence(1, 40, offset=3)) + row = TEXTS[1] + assert len(tokens) == 40 and tokens[:5] == row[3:8] + assert tokens[len(row) - 3] == row[0] + + +@pytest.mark.parametrize("last", [False, True]) +def test_packing_traces_only_the_target(last): + cell = next( + c + for c in bi.cells() + if c.cell_id == f"packing-four-sequences-{'last' if last else 'first'}" + ) + target = list(range(271)) + layout = bi.packing_layout(cell, TEXTS, target) + request = bi.replay_request( + cell.cell_id, target[:256], target[256:] + [7], IDENTITY, packing=layout + ) + sequences, traced = packed_layout(request) + assert len(sequences) == 4 and traced == [3 if last else 0] + assert sequences[traced[0]] == target + layout["sequences"][traced[0]] = target[:-1] + with pytest.raises(ValueError, match="exactly once"): + packed_layout({**request, "packing": layout}) diff --git a/tests/test_qwen3_next_chain_report.py b/tests/test_qwen3_next_chain_report.py index 71b0eacde..e863091d8 100644 --- a/tests/test_qwen3_next_chain_report.py +++ b/tests/test_qwen3_next_chain_report.py @@ -147,3 +147,41 @@ def test_unknown_nodes_and_versions_are_rejected(): matrices["update3"] = matrices["update1"] with pytest.raises(ValueError, match="subset"): build(matrices, artifacts) + + +def test_batch_invariance_section_counts_families_and_gates_the_report(): + matrix = { + "suite": "quick", + "status": "failed", + "cell_ids": ["concurrency-n04-last", "chunk-p0129-c64-f63"], + "first_failure": "chunk-p0129-c64-f63", + "cells": { + "concurrency-n04-last": {"family": "concurrency", "passed": True}, + "chunk-p0129-c64-f63": { + "family": "chunk", + "passed": False, + "first_drift_family": "gdn", + }, + }, + } + matrices, artifacts = inputs() + report = chain.build_report( + matrices, + artifacts, + contract=load_contract(), + measured_commit="c" * 40, + weight_source="w", + weight_hash="h", + device="cuda", + gpu_name=None, + compute_capability=None, + bi_matrix=matrix, + ) + section = report["batch_invariance"] + assert not report["passed"] and not section["passed"] + assert section["families"] == { + "concurrency": {"cells": 1, "passed": 1}, + "chunk": {"cells": 1, "passed": 0}, + } + assert section["first_failure_family"] == "gdn" + assert build(matrices, artifacts)["batch_invariance"] is None diff --git a/tests/test_tensor_identity.py b/tests/test_tensor_identity.py index f85c0c4dd..c25d3d6d5 100644 --- a/tests/test_tensor_identity.py +++ b/tests/test_tensor_identity.py @@ -6,6 +6,8 @@ import pytest import torch +from rl_engine.kernels.gtest.forward_invariance import _compare_logical_tensors +from rl_engine.kernels.gtest.tolerance import load_contract from rl_engine.testing.tensor_identity import assert_tensor_bitwise_equal, tensor_bitwise_equal @@ -39,3 +41,38 @@ def test_adjacent_bfloat16_values_are_rejected(): ) def test_logical_identity_does_not_require_matching_strides(value): assert_tensor_bitwise_equal(value, value.contiguous().clone()) + + +@pytest.mark.parametrize("judgment", ["forward_invariance", "gradient_invariance"]) +@pytest.mark.parametrize("fault", ["signed_zero", "dtype", "nan"]) +def test_gtest_invariance_uses_original_bits(judgment, fault): + canonical = torch.zeros(1, dtype=torch.bfloat16) + changed = torch.tensor([-0.0], dtype=torch.bfloat16) + if fault == "dtype": + changed = canonical.float() + elif fault == "nan": + canonical = torch.tensor([float("nan")], dtype=torch.bfloat16) + changed = canonical.clone() + result = _compare_logical_tensors( + canonical, + changed, + judgment=judgment, + contract=load_contract(), + op_class="reduction", + dtype=torch.bfloat16, + backend_profile="cuda_bf16", + ) + assert result.passed is False + + +def test_accuracy_keeps_its_tolerance_semantics(): + result = _compare_logical_tensors( + torch.ones(1), + torch.tensor([1.0078125], dtype=torch.bfloat16), + judgment="forward_accuracy", + contract=load_contract(), + op_class="reduction", + dtype=torch.bfloat16, + backend_profile="cuda_bf16", + ) + assert result.passed is True From 02c576ca10bae15da9218bb9fd664c8e7f382308 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 9 Oct 2026 05:13:53 +0000 Subject: [PATCH 08/68] fix(qwen3-next): native state shapes for any prompt that fits the rollout profile Batch-invariance chunk cells use prompts such as 129 tokens; the state-shape check only admitted the four L2 lengths, which their own validators already pin. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- rl_engine/testing/qwen3_next_l2.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/rl_engine/testing/qwen3_next_l2.py b/rl_engine/testing/qwen3_next_l2.py index 7f8778072..15ab886f6 100644 --- a/rl_engine/testing/qwen3_next_l2.py +++ b/rl_engine/testing/qwen3_next_l2.py @@ -654,8 +654,10 @@ def native_state_shapes(request): raise ValueError("Native state evidence requires the fixed equal-length batch1/4 matrix") prompt_length = len(prompts[0]) consumed = prompt_length + RESPONSE_LENGTH - 1 + # L2 requests are pinned to PROMPT_LENGTHS by their own validators; batch-invariance + # cells use any prompt that fits the rollout profile (1024 + 16 = max_model_len). if ( - prompt_length not in PROMPT_LENGTHS + not 1 <= prompt_length <= 1024 or request.get("consumed_response_tokens") != RESPONSE_LENGTH - 1 or request.get("state_position") != consumed - 1 ): From f431bad5d0ed2c8ff1a07780aa8efe2906ccb114 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 9 Oct 2026 07:35:22 +0000 Subject: [PATCH 09/68] fix(qwen3-next): evidence publisher redacts every recorded site path Editable sources, base interpreters and build directories in package metadata now keep only their last path component; cgroup paths keep only their file name. The leftover check matches absolute paths only, so package paths such as vllm/.../cpu/data/ are not flagged, and any mention of the batch scheduler blocks publication. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- scripts/qwen3_next_publish_evidence.py | 22 +++++++++++++----- tests/test_qwen3_next_evidence_redaction.py | 25 ++++++++++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/scripts/qwen3_next_publish_evidence.py b/scripts/qwen3_next_publish_evidence.py index 1cb6cb2e7..41312eb53 100644 --- a/scripts/qwen3_next_publish_evidence.py +++ b/scripts/qwen3_next_publish_evidence.py @@ -27,13 +27,15 @@ import socket from pathlib import Path +# Absolute site paths only: "cpu/data/" inside a package path is not a site path. +_ABSOLUTE = r"(?-])" FORBIDDEN = ( - r"/data/", - r"/home/", - r"/raid/", - r"/scratch", + _ABSOLUTE + r"/data/", + _ABSOLUTE + r"/home/", + _ABSOLUTE + r"/raid/", + _ABSOLUTE + r"/scratch", r"\bdgx-", - r"SLURM_", + r"(?i)slurm", ) TEXT_SUFFIXES = (".json", ".xml", ".txt", ".log") @@ -74,14 +76,22 @@ def redact(text, pairs, counts): counts[placeholder] = counts.get(placeholder, 0) + text.count(value) text = text.replace(value, placeholder) for pattern, placeholder in ( + (r"/sys/fs/cgroup/[^\s\"']*/([^/\s\"']+)", r"/\1"), (r"/[^\s\"']*/site-packages", ""), + # Anything else under a site root (editable sources, base interpreters, + # build directories in package metadata) keeps only its last component. + ( + _ABSOLUTE + r"/(?:data|home|raid|scratch)/[^\s\"'\\]*?([^/\s\"'\\]+)(?=[\s\"'\\]|$)", + r"/\1", + ), (r"\bdgx-[A-Za-z0-9-]+", ""), (rf"\b{re.escape(socket.gethostname())}\b", ""), (rf"\b{re.escape(getpass.getuser())}\b", ""), ): text, count = re.subn(pattern, placeholder, text) if count: - counts[placeholder] = counts.get(placeholder, 0) + count + label = placeholder.split("/", 1)[0] + counts[label] = counts.get(label, 0) + count return text diff --git a/tests/test_qwen3_next_evidence_redaction.py b/tests/test_qwen3_next_evidence_redaction.py index 5f045e8ab..eb2a1ff56 100644 --- a/tests/test_qwen3_next_evidence_redaction.py +++ b/tests/test_qwen3_next_evidence_redaction.py @@ -16,12 +16,12 @@ import qwen3_next_publish_evidence as publisher # noqa: E402 PATTERNS = ( - r"/data/", - r"/home/", - r"/raid/", - r"/scratch", + r"(?-])/data/", + r"(?-])/home/", + r"(?-])/raid/", + r"(?-])/scratch", r"\bdgx-", - r"SLURM_", + r"(?i)slurm", r"\bsbatch\b", r"\bsrun\b", r"\bwrd\b", @@ -99,7 +99,20 @@ def test_publisher_replaces_every_recorded_root(tmp_path): def test_publisher_refuses_text_that_survives_redaction(tmp_path): last = chain(tmp_path) leak = last / "attempts" / "0" / "environment.json" - leak.write_text(json.dumps({"note": "copied from /scratch/elsewhere"})) + leak.write_text(json.dumps({"note": "SLURM_JOB_ID=123"})) with pytest.raises(SystemExit, match="survived redaction"): publisher.publish(publisher.chain_runs(last), None, tmp_path / "published") assert not (tmp_path / "published").exists() + + +def test_relative_package_paths_are_not_site_paths(tmp_path): + counts = {} + text = publisher.redact( + '"vllm/ec_connector/cpu/data/base.py" "file:///data/x/editable/vime" "/home/b/env/bin/py"' + ' "/sys/fs/cgroup/memory/system.slice/slurmd.service/memory.limit_in_bytes"', + [], + counts, + ) + assert "cpu/data/base.py" in text and "/vime" in text and "/py" in text + assert "/memory.limit_in_bytes" in text + assert not publisher.leftovers(text) From dc79953b823bcaaf5619e36605ea7650ebfc2814 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 9 Oct 2026 19:49:38 +0000 Subject: [PATCH 10/68] test(qwen3-next): measure Megatron-core/TE and SGLang in the MoE prior-art runner - Megatron-core 0.16 MoELayer with Transformer Engine grouped GEMM and fused permute, configured as VIME runs Qwen3-Next (FP32 softmax router, top-10, all-to-all dispatcher), including its backward. - SGLang 0.5.21 Triton fused_moe in default and deterministic-inference mode. sgl_kernel cannot load next to torch 2.13, so its two kernels on this path are replaced by SGLang's own Triton moe_sum_reduce and JIT moe_align_block_size; any other sgl_kernel call fails loudly. - --only and --merge combine reports from different Python environments. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- rl_engine/testing/qwen3_next_moe_prior_art.py | 264 +++++++++++++++++- scripts/plot_qwen3_next_moe_prior_art.py | 5 +- scripts/qwen3_next_moe_prior_art.py | 31 +- 3 files changed, 290 insertions(+), 10 deletions(-) diff --git a/rl_engine/testing/qwen3_next_moe_prior_art.py b/rl_engine/testing/qwen3_next_moe_prior_art.py index d8ad92234..90ed6dea3 100644 --- a/rl_engine/testing/qwen3_next_moe_prior_art.py +++ b/rl_engine/testing/qwen3_next_moe_prior_art.py @@ -298,6 +298,224 @@ def fwd(x): } +def _megatron_layer(w): + """Megatron-core MoELayer configured as VIME runs Qwen3-Next. + + VIME's ``scripts/models/qwen3-next-80B-A3B.sh``: + + Softmax router computed in FP32, top-10 of 512, all-to-all dispatcher, TE + grouped GEMM and TE fused permute, no auxiliary loss. Single process, TP1/EP1. + """ + import os + + import torch.distributed as dist + from megatron.core import parallel_state + from megatron.core.models.gpt.moe_module_specs import get_moe_module_spec + from megatron.core.tensor_parallel.random import model_parallel_cuda_manual_seed + from megatron.core.transformer.spec_utils import build_module + from megatron.core.transformer.transformer_config import TransformerConfig + + if not dist.is_initialized(): + os.environ.setdefault("MASTER_ADDR", "127.0.0.1") + os.environ.setdefault("MASTER_PORT", "29531") + device = torch.device("cuda", torch.cuda.current_device()) + dist.init_process_group("nccl", rank=0, world_size=1, device_id=device) + if not parallel_state.model_parallel_is_initialized(): + parallel_state.initialize_model_parallel(1, 1) + model_parallel_cuda_manual_seed(0) + config = TransformerConfig( + num_layers=1, + hidden_size=HIDDEN, + num_attention_heads=16, + ffn_hidden_size=WIDTH, + moe_ffn_hidden_size=WIDTH, + num_moe_experts=EXPERTS, + moe_router_topk=TOPK, + moe_router_score_function="softmax", + moe_router_dtype="fp32", + moe_token_dispatcher_type="alltoall", + moe_grouped_gemm=True, + moe_permute_fusion=True, + moe_aux_loss_coeff=0.0, + moe_router_load_balancing_type="none", + add_bias_linear=False, + gated_linear_unit=True, + activation_func=F.silu, + bf16=True, + params_dtype=torch.bfloat16, + ) + spec = get_moe_module_spec(use_te=True, num_experts=EXPERTS, moe_grouped_gemm=True) + layer = build_module(spec, config=config).cuda() + with torch.no_grad(): + layer.router.weight.copy_(w["router"]) + for expert in range(EXPERTS): + getattr(layer.experts.linear_fc1, f"weight{expert}").copy_(w["gate_up"][expert]) + getattr(layer.experts.linear_fc2, f"weight{expert}").copy_(w["down"][expert]) + return layer + + +def _megatron(w): + import megatron.core + import transformer_engine + + layer = _megatron_layer(w) + gate_up = [getattr(layer.experts.linear_fc1, f"weight{e}") for e in range(EXPERTS)] + down = [getattr(layer.experts.linear_fc2, f"weight{e}") for e in range(EXPERTS)] + + def route(x): + with torch.no_grad(): + probs, routing_map = layer.router(x) + weights, ids = probs.topk(TOPK, dim=-1) + if not bool(routing_map.gather(1, ids).all()): + raise RuntimeError("Megatron routing map disagrees with its routed probabilities") + return ids, weights + + def call(x): + return layer(x.unsqueeze(1))[0].squeeze(1) + + def fwd(x): + with torch.no_grad(): + return call(x) + + def grads(x, dy): + leaf = x.detach().clone().requires_grad_(True) + dx, *dw = torch.autograd.grad(call(leaf), [leaf, *gate_up, *down], dy) + return dx, torch.stack(dw[:EXPERTS]), torch.stack(dw[EXPERTS:]) + + return { + "name": "Megatron-core MoELayer + TE grouped GEMM (VIME's Qwen3-Next config)", + "source": f"megatron-core {megatron.core.__version__}, " + f"transformer-engine {transformer_engine.__version__}", + "route": route, + "fwd": fwd, + "grads": grads, + } + + +def _sglang_shims(): + """Make SGLang's Triton MoE path importable next to torch 2.13. + + ``sgl_kernel`` 0.3.21 is built against another libtorch ABI and cannot load + here. On CUDA, SGLang's Triton MoE calls two of its kernels; both are + replaced by SGLang's own implementations of the same operation: the Triton + ``moe_sum_reduce_triton`` and the JIT ``moe_align_block_size`` that SGLang + registers with the same signature. Every other ``sgl_kernel`` symbol only + has to import; calling one raises. + """ + import importlib.abc + import importlib.machinery + import sys + import types + + class Missing: + def __init__(self, name): + self.name = name + + def __getattr__(self, name): + if name.startswith("__"): + raise AttributeError(name) + return Missing(f"{self.name}.{name}") + + def __call__(self, *args, **kwargs): + raise RuntimeError(f"{self.name} called, but sgl_kernel cannot load next to torch 2.13") + + class Stub(types.ModuleType): + def __getattr__(self, name): + if name.startswith("__"): + raise AttributeError(name) + return Missing(f"{self.__name__}.{name}") + + class Finder(importlib.abc.MetaPathFinder, importlib.abc.Loader): + def find_spec(self, name, path=None, target=None): + if name in ("sgl_kernel", "gguf") or name.startswith("sgl_kernel."): + return importlib.machinery.ModuleSpec(name, self, is_package=True) + + def create_module(self, spec): + module = Stub(spec.name) + module.__path__ = [] + return module + + def exec_module(self, module): + pass + + if not any(type(f).__name__ == "Finder" for f in sys.meta_path): + sys.meta_path.insert(0, Finder()) + import sgl_kernel + import sglang.kernels.ops.moe as moe_ops + from sglang.kernels.ops.moe.fused_moe_triton_kernels import moe_sum_reduce_triton + from sglang.kernels.spec import KernelBackend + + sgl_kernel.moe_sum_reduce = moe_sum_reduce_triton + lookup = moe_ops.get_kernel + moe_ops.get_kernel = lambda op, backend: lookup( + op, KernelBackend.JIT if op == "moe.moe_align_block_size" else backend + ) + + +def _sglang(w, deterministic: bool): + import sglang + + _sglang_shims() + from sglang.srt.layers.moe.moe_runner.base import MoeRunnerConfig + from sglang.srt.layers.moe.moe_runner.triton_utils import fused_moe as fm + from sglang.srt.layers.moe.moe_runner.triton_utils import fused_moe_triton_config as fc + from sglang.srt.layers.moe.topk import StandardTopKOutput, fused_topk_torch_native + + class Bag: + def __init__(self, **values): + self.__dict__.update(values) + + def __getattr__(self, name): + return False + + # SGLang reads these switches from its published server config; publishing + # needs a full server (and sgl_kernel). Only the deterministic switch matters. + settings = Bag(deterministic=Bag(enable_deterministic_inference=deterministic), moe=Bag()) + from sglang.srt.batch_invariant_ops import batch_invariant_ops as bio + + def linear(x, weight): + # Deterministic mode: what enable_batch_invariant_mode() installs for aten::mm, + # called directly so the override does not leak into other candidates. + return bio.mm_batch_invariant(x, weight.t()) if deterministic else F.linear(x, weight) + + config = MoeRunnerConfig( + num_experts=EXPERTS, + num_local_experts=EXPERTS, + hidden_size=HIDDEN, + intermediate_size_per_partition=WIDTH, + top_k=TOPK, + params_dtype=torch.bfloat16, + inplace=False, + ) + + def set_mode(): + fm.get_exec = fc.get_exec = lambda: settings + fm.is_batch_invariant_mode_enabled = lambda: deterministic + # One process, no TP group: symmetric memory is disabled, the group unused. + fm.get_parallel = lambda: Bag(tp_group=None) + fm.is_allocation_symmetric = lambda: False + + def route(x): + set_mode() + logits = linear(x, w["router"]) + weights, ids = fused_topk_torch_native(x, logits, TOPK, renormalize=True)[:2] + return ids.long(), weights + + def fwd(x): + with torch.no_grad(): + ids, weights = route(x) + topk = StandardTopKOutput(weights, ids.int(), None) + return fm.fused_experts(x, w["gate_up"], w["down"], topk, config) + + mode = "deterministic inference" if deterministic else "default" + return { + "name": f"SGLang fused_moe (Triton), {mode}", + "source": f"sglang {sglang.__version__} moe_runner/triton_utils", + "route": route, + "fwd": fwd, + } + + def candidates(w) -> list[dict[str, Any]]: return [ _guard("rl_kernel_cuda", lambda: _rl_kernel(w)), @@ -305,6 +523,9 @@ def candidates(w) -> list[dict[str, Any]]: _guard("vllm_bi0", lambda: _vllm(w, False)), _guard("vllm_bi1", lambda: _vllm(w, True)), _guard("flashinfer_cutlass", lambda: _flashinfer(w)), + _guard("megatron_te", lambda: _megatron(w)), + _guard("sglang_triton", lambda: _sglang(w, False)), + _guard("sglang_deterministic", lambda: _sglang(w, True)), ] @@ -338,14 +559,25 @@ def _rows_bi(fn, x) -> dict[str, bool]: return result +def has_backward(cand) -> bool: + return "graph" in cand or "grads" in cand + + +def cand_grads(cand, rows, dy_rows): + """``(dx, d gate_up, d down)`` of one backward.""" + if "grads" in cand: + return cand["grads"](rows, dy_rows) + y, leaves = cand["graph"](rows) + return torch.autograd.grad(y, leaves, dy_rows) + + def _grad_bi(cand, x) -> dict[str, Any]: """dx of the probe rows, and dW with zero-gradient rows appended.""" g = torch.Generator(device="cuda").manual_seed(7) dy = torch.randn(x.shape, device="cuda", generator=g).to(torch.bfloat16) def grads(rows, dy_rows): - y, leaves = cand["graph"](rows) - return torch.autograd.grad(y, leaves, dy_rows) + return cand_grads(cand, rows, dy_rows) alone = grads(x[:PROBE], dy[:PROBE]) dx, dw = {}, {} @@ -380,7 +612,7 @@ def _one(cand, x, w) -> dict[str, Any]: out["route_rows_bitwise"] = _rows_bi(cand["route"], x) out["output_rows_bitwise"] = _rows_bi(cand["fwd"], x) out["repeatable"] = bool(_bitwise(cand["fwd"](x[:256]), cand["fwd"](x[:256]))) - if "graph" in cand: + if has_backward(cand): out.update(_grad_bi(cand, x)) out["accuracy"] = _accuracy(cand, x[:256], w) except Exception as exc: # noqa: BLE001 - a crash is a result, not a harness failure @@ -414,23 +646,39 @@ def _latency(cands, tokens) -> dict[str, Any]: dy = torch.randn_like(x) calls = {} for cand in ready: - if "graph" not in cand: + if not has_backward(cand): continue def step(c=cand): - y, leaves = c["graph"](x) - torch.autograd.grad(y, leaves, dy) + cand_grads(c, x, dy) calls[cand["key"]] = step backward[str(count)] = time_interleaved(calls, warmup=2, iters=10) return {"forward_us": forward, "forward_plus_backward_us": backward} -def moe_report() -> dict[str, Any]: +def moe_report(only=None) -> dict[str, Any]: + """The report for every candidate, or only for the keys in ``only``. + + Some candidates need another Python environment (SGLang's pinned torch ABI, + Megatron's training stack); run each environment with ``only`` and merge. + """ torch.manual_seed(0) w = make_weights() x = make_tokens(max(BI_SIZES), seed=1) - cands = candidates(w) + keys = [ + "rl_kernel_cuda", + "hf_transformers", + "vllm_bi0", + "vllm_bi1", + "flashinfer_cutlass", + "megatron_te", + "sglang_triton", + "sglang_deterministic", + ] + if only is not None and set(only) - set(keys): + raise ValueError(f"Unknown candidates: {sorted(set(only) - set(keys))}") + cands = [c for c in candidates(w) if only is None or c["key"] in only] return { "op": "qwen3_next_routed_moe", "shape": {"hidden": HIDDEN, "experts": EXPERTS, "top_k": TOPK, "expert_width": WIDTH}, diff --git a/scripts/plot_qwen3_next_moe_prior_art.py b/scripts/plot_qwen3_next_moe_prior_art.py index 17a966b61..f7e358b63 100644 --- a/scripts/plot_qwen3_next_moe_prior_art.py +++ b/scripts/plot_qwen3_next_moe_prior_art.py @@ -34,7 +34,7 @@ PASS = "#1baf7a" FAIL = "#d64545" NA = "#e4e3dd" -EXISTING = ("#eb6834", "#b8501f", "#f29a62", "#8a3a15") +EXISTING = ("#eb6834", "#b8501f", "#f29a62", "#8a3a15", "#f6bd94", "#d9773f", "#6b2a0e") LABELS = { "rl_kernel_cuda": "RL-Kernel shared_moe", @@ -42,6 +42,9 @@ "vllm_bi0": "vLLM fused_moe (BI=0)", "vllm_bi1": "vLLM fused_moe (BI=1)", "flashinfer_cutlass": "FlashInfer cutlass_fused_moe", + "megatron_te": "Megatron-core + TE (VIME config)", + "sglang_triton": "SGLang fused_moe (default)", + "sglang_deterministic": "SGLang fused_moe (deterministic)", } CHECKS = ( ("route_rows_bitwise", "routes"), diff --git a/scripts/qwen3_next_moe_prior_art.py b/scripts/qwen3_next_moe_prior_art.py index 398245eee..ae3a028b4 100644 --- a/scripts/qwen3_next_moe_prior_art.py +++ b/scripts/qwen3_next_moe_prior_art.py @@ -35,6 +35,8 @@ "transformers", "triton", "sglang", + "sgl-kernel", + "megatron_core", "megatron-core", "transformer_engine", "vime", @@ -54,9 +56,34 @@ def _version(name: str) -> str | None: return None +def merge(base: dict, extra: dict) -> dict: + """``base`` with ``extra``'s candidates and latencies added (same commit and GPU).""" + for field in ("rl_kernel_commit", "tracked_tree_dirty", "shape", "op"): + if base[field] != extra[field]: + raise SystemExit(f"Reports differ in {field}; they cannot be merged") + if base["environment"]["gpu"] != extra["environment"]["gpu"]: + raise SystemExit("Reports were measured on different GPUs") + keys = {c["key"] for c in base["candidates"]} + added = [c for c in extra["candidates"] if c["key"] not in keys] + merged = dict(base, candidates=base["candidates"] + added) + merged["latency"] = { + section: { + size: {**values, **extra["latency"][section].get(size, {})} + for size, values in sizes.items() + } + for section, sizes in base["latency"].items() + } + merged["environments"] = {"main": base["environment"], "extra": extra["environment"]} + return merged + + def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--out", type=Path, required=True) + parser.add_argument("--only", help="comma-separated candidate keys (default: all)") + parser.add_argument( + "--merge", type=Path, help="a report from another environment to merge into this one" + ) args = parser.parse_args() if not torch.cuda.is_available(): raise SystemExit("needs a CUDA device") @@ -73,8 +100,10 @@ def main() -> None: "cuda": torch.version.cuda, "libraries": {name: _version(name) for name in LIBRARIES}, }, - **moe_report(), + **moe_report(args.only.split(",") if args.only else None), } + if args.merge is not None: + report = merge(json.loads(args.merge.read_text()), report) args.out.parent.mkdir(parents=True, exist_ok=True) args.out.write_text(json.dumps(report, indent=2) + "\n") print( From 53dce6e9db4fd8941732579074db2ef74295f004 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 9 Oct 2026 23:49:57 +0000 Subject: [PATCH 11/68] docs(qwen3-next): MoE prior art with Megatron-core/TE and SGLang measured (B200, c58816b) Every library in the reuse table is now measured: Megatron-core 0.16 + TE 2.16 as VIME configures Qwen3-Next, and SGLang 0.5.21 in default and deterministic-inference mode. The TP4 gate evidence is unchanged (2e950f3, identical MoE code). Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../operators/qwen3-next-moe-route-combine.md | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/docs/operators/qwen3-next-moe-route-combine.md b/docs/operators/qwen3-next-moe-route-combine.md index ed11301a0..20ed1d6ba 100644 --- a/docs/operators/qwen3-next-moe-route-combine.md +++ b/docs/operators/qwen3-next-moe-route-combine.md @@ -69,7 +69,9 @@ the selected probabilities; the indices are discrete. | vLLM, `VLLM_BATCH_INVARIANT=1` | yes | yes | none | no VJP for training; BF16 router logits (Qwen3-Next and VIME route in FP32); routed weight applied in the expert GEMM epilogue on the BF16 output, then routes summed by `moe_sum` | | HF `Qwen3NextExperts` (eager) | yes | yes | autograd | `dx`/`dW` change with batch size (cuBLAS shape heuristics); BF16 router; `index_add_` combine in BF16 | | FlashInfer `cutlass_fused_moe` | (torch routing) | no | none | output changes with batch size; no VJP | -| SGLang, Megatron-core, Transformer Engine | not measured | not measured | - | not installable next to the pinned vLLM 0.30.0 / torch 2.13 runtime | +| Megatron-core 0.16 `MoELayer` + TE 2.16 grouped GEMM, as VIME configures Qwen3-Next | no (256+) | no (1024) | autograd, not BI | FP32 router routes like FP64, but the routing, output, dx and dW change with batch size | +| SGLang 0.5.21 Triton `fused_moe`, default | no (256+) | no (256+) | none | tuned configs and the BF16 router GEMM depend on the token count | +| SGLang, deterministic inference | yes | yes | none | inference only; BF16 router logits; its deterministic tile (64/64/32) is the one vLLM uses in batch-invariant mode | What RL-Kernel reuses: vLLM's batch-invariant GEMM for the router and the backward, and vLLM's `fused_moe` Triton kernel for the forward expert GEMMs, @@ -91,25 +93,32 @@ commands are in TP1 shape (H=2048, 512 experts, top-10, width 512), random weights, BF16: -| | RL-Kernel `shared_moe` | vLLM BI=1 | vLLM BI=0 | FlashInfer CUTLASS | HF eager | -| --- | --- | --- | --- | --- | --- | -| routes / output BI (16-1024 tokens) | **yes / yes** | yes / yes | no / no (256+) | no / no (256+) | yes / yes | -| dx / dW BI | **yes / yes** | no backward | no backward | no backward | no / no (1024) | -| rel. L2 error vs FP64 | **3.9e-3** | 7.0e-2 | 7.0e-2 | 7.0e-2 | 7.0e-2 | -| tokens routed unlike FP64 (of 256) | **0** | 11 | 11 | 11 | 11 | -| forward, 1 / 64 / 1024 / 4096 tokens (ms) | 1.38 / 1.71 / 2.05 / 3.05 | 0.37 / 0.66 / 0.88 / 1.06 | 0.36 / 0.65 / 0.87 / 1.06 | 0.34 / 0.67 / 0.85 / 1.08 | 2.0 / 61 / 89 / 89 | -| forward + backward, 64 / 1024 tokens (ms) | 112 / 166 | - | - | - | 878 / 1283 | - -* The accuracy gap is the router: every other candidate computes BF16 router - logits, and 11 of 256 tokens then select a different expert set than FP64 - routing does. RL-Kernel routes in FP32 and selects the same experts. +| | routes / output BI (16-1024 tokens) | dx / dW BI | rel. L2 vs FP64 | tokens routed unlike FP64 (of 256) | forward, 1 / 64 / 1024 / 4096 tokens (ms) | fwd + bwd, 64 / 1024 (ms) | +| --- | --- | --- | --- | --- | --- | --- | +| **RL-Kernel `shared_moe`** | **yes / yes** | **yes / yes** | **3.9e-3** | **0** | 1.39 / 1.69 / 2.04 / 3.05 | 114 / 169 | +| Megatron-core + TE (VIME config) | no / no | no / no (1024) | 4.6e-3 | 0 | 3.8 / 9.7 / 11.7 / 12.1 | 45 / 51 | +| vLLM BI=1 | yes / yes | no backward | 7.0e-2 | 11 | 0.39 / 0.66 / 0.87 / 1.05 | - | +| SGLang deterministic | yes / yes | no backward | 7.0e-2 | 11 | 0.35 / 0.66 / 0.84 / 1.37 | - | +| vLLM BI=0 | no / no | no backward | 7.0e-2 | 11 | 0.38 / 0.66 / 0.87 / 1.06 | - | +| SGLang default | no / no | no backward | 7.0e-2 | 11 | 0.34 / 0.61 / 0.83 / 1.35 | - | +| FlashInfer CUTLASS | no / no | no backward | 7.0e-2 | 11 | 0.37 / 0.72 / 0.90 / 1.12 | - | +| HF eager | yes / yes | no / no (1024) | 7.0e-2 | 11 | 2.1 / 62 / 89 / 87 | 877 / 1284 | + +* The accuracy gap is the router: every candidate with BF16 router logits sends + 11 of 256 tokens to a different expert set than FP64 routing does. RL-Kernel + and Megatron-core (VIME's training path) route in FP32 and select the same + experts as FP64. +* The only batch-invariant candidates are RL-Kernel and the inference-only + modes of vLLM and SGLang; of these, only RL-Kernel has a backward, so only it + can run the same forward on the training and the rollout side. * RL-Kernel's forward is 2-4x slower than vLLM's. About 0.5 ms of it, at any token count, is the FP32 IEEE router GEMM, which is part of the contract. The expert GEMMs themselves run in vLLM's kernel. The per-expert GEMM loop they replace (same bits) took 47 / 66 / 71 ms at 64 / 1024 / 4096 tokens in a local B200 run of the same runner; that run is not part of this evidence. * The backward is still a per-expert loop of pinned GEMMs (512 experts x 4 - GEMMs) and dominates a training step's MoE time. + GEMMs): 169 ms at 1024 tokens, against 51 ms for Megatron-core + TE, which is + not batch-invariant. It dominates a training step's MoE time. TP4 gate on the real layer-0 weights (`tp4-moe/rank-*.json`): all twelve cases pass on all four ranks, including the round trip of all 512 experts, identical From a97a6fe97a9084890517a6d11eab112ee571be19 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sat, 10 Oct 2026 00:51:36 +0000 Subject: [PATCH 12/68] test(qwen3-next): measure FlashAttention-4, Transformer Engine and Megatron-core attention Add three engines to the attention prior-art runner: - FlashAttention-4 (CuTe DSL) varlen as vendored by vLLM, forward and backward. - Transformer Engine DotProductAttention on THD-packed sequences, in inference mode (cuDNN fused) and in training mode (on SM100 TE has no fused head-dim-256 backward and falls back to its unfused path). - Megatron-core's local DotProductAttention, one launch per sequence. The backward check now compares the target's dq/dk/dv alone against the same sequence packed first and last, so engines with a packed backward are checked for batch invariance rather than repeatability. A prefill/decode crash is recorded per candidate without skipping its other checks. --only and --merge combine reports from different Python environments. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../testing/qwen3_next_attention_prior_art.py | 231 ++++++++++++++++-- .../plot_qwen3_next_attention_prior_art.py | 22 +- scripts/qwen3_next_attention_prior_art.py | 30 ++- 3 files changed, 261 insertions(+), 22 deletions(-) diff --git a/rl_engine/testing/qwen3_next_attention_prior_art.py b/rl_engine/testing/qwen3_next_attention_prior_art.py index 8a089b84a..68d98eab2 100644 --- a/rl_engine/testing/qwen3_next_attention_prior_art.py +++ b/rl_engine/testing/qwen3_next_attention_prior_art.py @@ -9,7 +9,8 @@ * **batch invariance** - a target sequence's output computed alone, and inside a batch with three other sequences (placed first and last), must be bitwise - equal; for candidates with a backward, the same for its dq/dk/dv; + equal; for candidates with a backward, the same for its dq/dk/dv, in one + packed launch where the engine has a packed backward; * **prefill/decode invariance** - the last 64 query rows, and the last single row, computed against the full KV must equal the same rows of the full prefill. This is the replay-vs-rollout boundary of RFC #428; @@ -286,6 +287,171 @@ def run(seqs): } +def _bottom_right_mask(lq, lk, device): + """True where query row i (aligned to the end of the KV sequence) may not attend.""" + rows = torch.arange(lq, device=device)[:, None] + (lk - lq) + return torch.arange(lk, device=device)[None, :] > rows + + +def _fa4(): + """FlashAttention-4 (CuTe DSL), as vendored by vLLM 0.30.0: varlen forward and backward.""" + import vllm + from vllm.vllm_flash_attn.cute import interface as fa4 + + def call(q, k, v, lq, lk): + out = fa4.flash_attn_varlen_func( + q, + k, + v, + cu_seqlens_q=_cu(lq), + cu_seqlens_k=_cu(lk), + max_seqlen_q=max(lq), + max_seqlen_k=max(lk), + softmax_scale=SCALE, + causal=True, + ) + return out[0] if isinstance(out, tuple) else out + + def run(seqs): + lq, lk = [s[0].shape[0] for s in seqs], [s[1].shape[0] for s in seqs] + q, k, v = (torch.cat([s[i] for s in seqs]) for i in range(3)) + with torch.no_grad(): + return _split(call(q, k, v, lq, lk), lq) + + def grad_batch(seqs, dys): + lq, lk = [s[0].shape[0] for s in seqs], [s[1].shape[0] for s in seqs] + leaves = [torch.cat([s[i] for s in seqs]).detach().requires_grad_(True) for i in range(3)] + dq, dk, dv = torch.autograd.grad(call(*leaves, lq, lk), leaves, torch.cat(dys)) + return list(zip(_split(dq, lq), _split(dk, lk), _split(dv, lk))) + + return { + "name": "FlashAttention-4 (CuTe DSL) varlen, vendored by vLLM", + "source": f"vllm {vllm.__version__} vllm_flash_attn/cute " + "(its head-dim-256 SM100 backward has no deterministic mode)", + "run": run, + "grad_batch": grad_batch, + } + + +def _te(training: bool): + """Transformer Engine DotProductAttention on THD-packed sequences. + + With cuDNN 9.20 on SM100, TE has a fused (cuDNN) head-dim-256 backend only for + inference; in training mode it falls back to its unfused PyTorch path. Both are + measured, the fused one forward-only. + """ + import transformer_engine + import transformer_engine.pytorch as te + + attention = te.DotProductAttention( + HQ, + D, + num_gqa_groups=HKV, + attention_dropout=0.0, + qkv_format="thd", + attn_mask_type="padding_causal_bottom_right", + softmax_scale=SCALE, + ) + attention.train(training) + + def call(q, k, v, lq, lk): + out = attention( + q, + k, + v, + cu_seqlens_q=_cu(lq), + cu_seqlens_kv=_cu(lk), + max_seqlen_q=max(lq), + max_seqlen_kv=max(lk), + ) + return out.view(q.shape[0], HQ, D) + + def run(seqs): + lq, lk = [s[0].shape[0] for s in seqs], [s[1].shape[0] for s in seqs] + q, k, v = (torch.cat([s[i] for s in seqs]) for i in range(3)) + with torch.no_grad(): + return _split(call(q, k, v, lq, lk), lq) + + def grad_batch(seqs, dys): + lq, lk = [s[0].shape[0] for s in seqs], [s[1].shape[0] for s in seqs] + leaves = [torch.cat([s[i] for s in seqs]).detach().requires_grad_(True) for i in range(3)] + dq, dk, dv = torch.autograd.grad(call(*leaves, lq, lk), leaves, torch.cat(dys)) + return list(zip(_split(dq, lq), _split(dk, lk), _split(dv, lk))) + + version = f"transformer-engine {transformer_engine.__version__}" + if not training: + return { + "name": "Transformer Engine DotProductAttention (THD), inference: cuDNN fused", + "source": f"{version} (no fused head-dim-256 backward on SM100)", + "run": run, + } + return { + "name": "Transformer Engine DotProductAttention (THD), training: unfused fallback", + "source": f"{version} (selected for head-dim-256 training on SM100)", + "run": run, + "grad_batch": grad_batch, + } + + +def _megatron_local(): + """Megatron-core's own (non-TE) DotProductAttention; it has no packed-sequence path.""" + import os + + import megatron.core + import torch.distributed as dist + from megatron.core import parallel_state + from megatron.core.tensor_parallel.random import model_parallel_cuda_manual_seed + from megatron.core.transformer.dot_product_attention import DotProductAttention + from megatron.core.transformer.enums import AttnMaskType + from megatron.core.transformer.transformer_config import TransformerConfig + + if not dist.is_initialized(): + os.environ.setdefault("MASTER_ADDR", "127.0.0.1") + os.environ.setdefault("MASTER_PORT", "29532") + device = torch.device("cuda", torch.cuda.current_device()) + dist.init_process_group("nccl", rank=0, world_size=1, device_id=device) + if not parallel_state.model_parallel_is_initialized(): + parallel_state.initialize_model_parallel(1, 1) + model_parallel_cuda_manual_seed(0) + config = TransformerConfig( + num_layers=1, + hidden_size=HQ * D, + num_attention_heads=HQ, + num_query_groups=HKV, + kv_channels=D, + attention_dropout=0.0, + bf16=True, + params_dtype=torch.bfloat16, + ) + attention = DotProductAttention( + config, + layer_number=1, + attn_mask_type=AttnMaskType.padding, + attention_type="self", + softmax_scale=SCALE, + ) + + def one(q, k, v): + mask = _bottom_right_mask(q.shape[0], k.shape[0], q.device)[None, None] + out = attention(q.unsqueeze(1), k.unsqueeze(1), v.unsqueeze(1), mask) + return out.view(q.shape[0], HQ, D) + + def run(seqs): + with torch.no_grad(): + return [one(*s) for s in seqs] + + def grad(q, k, v, dy): + leaves = [t.detach().clone().requires_grad_(True) for t in (q, k, v)] + return torch.autograd.grad(one(*leaves), leaves, dy) + + return { + "name": "Megatron-core DotProductAttention (local, one launch per sequence)", + "source": f"megatron-core {megatron.core.__version__}", + "run": run, + "grad": grad, + } + + def candidates() -> list[dict[str, Any]]: return [ _guard("rl_kernel_cuda", _rl_kernel), @@ -294,6 +460,10 @@ def candidates() -> list[dict[str, Any]]: _guard("vllm_fa2_split1", lambda: _vllm_fa(1)), _guard("vllm_triton_2d", _vllm_triton), _guard("flashinfer", _flashinfer), + _guard("fa4_cute", _fa4), + _guard("te_fused", lambda: _te(False)), + _guard("te_training", lambda: _te(True)), + _guard("megatron_local", _megatron_local), ] @@ -319,17 +489,33 @@ def _prefill_decode(cand, target) -> dict[str, bool]: return result +def has_backward(cand) -> bool: + return "grad" in cand or "grad_batch" in cand + + +def grad_batch(cand, seqs, dys): + """Each sequence's (dq, dk, dv): one packed launch where the engine has one.""" + if "grad_batch" in cand: + return cand["grad_batch"](seqs, dys) + return [cand["grad"](*s, dy) for s, dy in zip(seqs, dys)] + + def _grad_bi(cand, target, others) -> dict[str, bool]: + """The target's dq/dk/dv alone vs. packed first and last with the other sequences.""" g = torch.Generator(device="cuda").manual_seed(5) - dy = torch.randn(target[0].shape, device="cuda", generator=g).to(torch.bfloat16) - alone = cand["grad"](*target, dy) - # A backward is per sequence in every engine here; what can change is the - # kernel the shape selects, so compare against a second target placed after - # other sequences have been differentiated. - for other in others: - cand["grad"](*other, torch.ones_like(other[0])) - again = cand["grad"](*target, dy) - return {name: _bitwise(a, b) for name, a, b in zip(("dq", "dk", "dv"), again, alone)} + + def dy_for(q): + return torch.randn(q.shape, device="cuda", generator=g).to(torch.bfloat16) + + dy = dy_for(target[0]) + dys = [dy_for(o[0]) for o in others] + alone = grad_batch(cand, [target], [dy])[0] + first = grad_batch(cand, [target, *others], [dy, *dys])[0] + last = grad_batch(cand, [*others, target], [*dys, dy])[-1] + return { + name: _bitwise(f, a) and _bitwise(l_, a) + for name, f, l_, a in zip(("dq", "dk", "dv"), first, last, alone) + } def _one(cand, target, others) -> dict[str, Any]: @@ -338,10 +524,14 @@ def _one(cand, target, others) -> dict[str, Any]: out = {"key": cand["key"], "name": cand["name"], "source": cand["source"]} try: out["batch_bitwise"] = _batch_bi(cand, target, others) - out["prefill_decode_bitwise"] = _prefill_decode(cand, target) + try: + out["prefill_decode_bitwise"] = _prefill_decode(cand, target) + except Exception as exc: # noqa: BLE001 - recorded; the other checks still run + out["prefill_decode_failed"] = f"{type(exc).__name__}: {str(exc)[:300]}" + out["prefill_decode_bitwise"] = {"last_64": False, "last_1": False} out["repeatable"] = _bitwise(cand["run"]([target])[0], cand["run"]([target])[0]) - if "grad" in cand: - out["backward_repeat_bitwise"] = _grad_bi(cand, target, others) + if has_backward(cand): + out["backward_batch_bitwise"] = _grad_bi(cand, target, others) ref = fp64_reference(*target) got = cand["run"]([target])[0].double() out["accuracy"] = { @@ -354,7 +544,7 @@ def _one(cand, target, others) -> dict[str, Any]: "failed" not in out and all(out["batch_bitwise"].values()) and all(out["prefill_decode_bitwise"].values()) - and all(out.get("backward_repeat_bitwise", {"": True}).values()) + and all(out.get("backward_batch_bitwise", {"": True}).values()) ) return out @@ -379,7 +569,11 @@ def calls_for(seqs): q, k, v = make_sequence(BACKWARD_TOKENS, 402) dy = torch.randn_like(q) backward = time_interleaved( - {c["key"]: (lambda c=c: c["grad"](q, k, v, dy)) for c in ready if "grad" in c}, + { + c["key"]: (lambda c=c: grad_batch(c, [(q, k, v)], [dy])) + for c in ready + if has_backward(c) + }, warmup=3, iters=15, ) @@ -390,10 +584,13 @@ def calls_for(seqs): } -def attention_report() -> dict[str, Any]: +def attention_report(only=None) -> dict[str, Any]: + """Every candidate, or only the keys in ``only`` (run each Python environment separately).""" target = make_sequence(TARGET, 1) others = [make_sequence(n, 10 + i) for i, n in enumerate(COMPANIONS)] - cands = candidates() + cands = [c for c in candidates() if only is None or c["key"] in only] + if only is not None and set(only) - {c["key"] for c in cands}: + raise ValueError(f"Unknown candidates: {sorted(set(only) - {c['key'] for c in cands})}") return { "op": "qwen3_next_tp4_attention", "shape": {"q_heads": HQ, "kv_heads": HKV, "head_dim": D, "scale": SCALE}, diff --git a/scripts/plot_qwen3_next_attention_prior_art.py b/scripts/plot_qwen3_next_attention_prior_art.py index 8343c5851..fbfa94d8a 100644 --- a/scripts/plot_qwen3_next_attention_prior_art.py +++ b/scripts/plot_qwen3_next_attention_prior_art.py @@ -31,7 +31,17 @@ PASS = "#1baf7a" FAIL = "#d64545" NA = "#e4e3dd" -EXISTING = ("#eb6834", "#b8501f", "#f29a62", "#8a3a15", "#f6bd94", "#d9773f") +EXISTING = ( + "#eb6834", + "#b8501f", + "#f29a62", + "#8a3a15", + "#f6bd94", + "#d9773f", + "#6b2a0e", + "#c96a2c", + "#f0a878", +) LABELS = { "rl_kernel_cuda": "RL-Kernel deterministic", @@ -40,15 +50,19 @@ "vllm_fa2_split1": "vLLM FA2 (num_splits=1)", "vllm_triton_2d": "vLLM Triton unified (2D)", "flashinfer": "FlashInfer ragged prefill", + "fa4_cute": "FlashAttention-4 (CuTe)", + "te_fused": "TE, cuDNN fused (inference)", + "te_training": "TE, unfused (training)", + "megatron_local": "Megatron-core local", } COLUMNS = ( ("batch_bitwise", "first", "batch: first"), ("batch_bitwise", "last", "batch: last"), ("prefill_decode_bitwise", "last_64", "chunk: last 64"), ("prefill_decode_bitwise", "last_1", "decode: last 1"), - ("backward_repeat_bitwise", "dq", "bwd dq"), - ("backward_repeat_bitwise", "dk", "bwd dk"), - ("backward_repeat_bitwise", "dv", "bwd dv"), + ("backward_batch_bitwise", "dq", "bwd dq (packed)"), + ("backward_batch_bitwise", "dk", "bwd dk (packed)"), + ("backward_batch_bitwise", "dv", "bwd dv (packed)"), ) diff --git a/scripts/qwen3_next_attention_prior_art.py b/scripts/qwen3_next_attention_prior_art.py index 2ded9d0ca..c68219621 100644 --- a/scripts/qwen3_next_attention_prior_art.py +++ b/scripts/qwen3_next_attention_prior_art.py @@ -36,6 +36,7 @@ "triton", "sglang", "megatron-core", + "megatron_core", "transformer_engine", "vime", ) @@ -54,9 +55,34 @@ def _version(name: str) -> str | None: return None +def merge(base: dict, extra: dict) -> dict: + """``base`` with ``extra``'s candidates and latencies added (same commit and GPU).""" + for field in ("rl_kernel_commit", "tracked_tree_dirty", "shape", "op"): + if base[field] != extra[field]: + raise SystemExit(f"Reports differ in {field}; they cannot be merged") + if base["environment"]["gpu"] != extra["environment"]["gpu"]: + raise SystemExit("Reports were measured on different GPUs") + keys = {c["key"] for c in base["candidates"]} + added = [c for c in extra["candidates"] if c["key"] not in keys] + merged = dict(base, candidates=base["candidates"] + added) + merged["latency"] = { + section: { + size: {**values, **extra["latency"][section].get(size, {})} + for size, values in sizes.items() + } + for section, sizes in base["latency"].items() + } + merged["environments"] = {"main": base["environment"], "extra": extra["environment"]} + return merged + + def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--out", type=Path, required=True) + parser.add_argument("--only", help="comma-separated candidate keys (default: all)") + parser.add_argument( + "--merge", type=Path, help="a report from another process to merge into this one" + ) args = parser.parse_args() if not torch.cuda.is_available(): raise SystemExit("needs a CUDA device") @@ -73,8 +99,10 @@ def main() -> None: "cuda": torch.version.cuda, "libraries": {name: _version(name) for name in LIBRARIES}, }, - **attention_report(), + **attention_report(args.only.split(",") if args.only else None), } + if args.merge is not None: + report = merge(json.loads(args.merge.read_text()), report) args.out.parent.mkdir(parents=True, exist_ok=True) args.out.write_text(json.dumps(report, indent=2) + "\n") print( From 137d0fb1c9e57bd5252db8f85c351bca609300df Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sat, 10 Oct 2026 02:21:34 +0000 Subject: [PATCH 13/68] fix(qwen3-next): merge flat and per-size attention latency sections The decode and forward+backward latency sections are keyed by candidate directly, the prefill section by size first; merge them at any depth. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- scripts/qwen3_next_attention_prior_art.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/scripts/qwen3_next_attention_prior_art.py b/scripts/qwen3_next_attention_prior_art.py index c68219621..289f778bd 100644 --- a/scripts/qwen3_next_attention_prior_art.py +++ b/scripts/qwen3_next_attention_prior_art.py @@ -55,6 +55,17 @@ def _version(name: str) -> str | None: return None +def _merge_latency(base: dict, extra: dict) -> dict: + """Per-candidate latencies merged at any depth (prefill is keyed by size first).""" + merged = dict(base) + for key, value in extra.items(): + if isinstance(value, dict) and isinstance(base.get(key), dict): + merged[key] = _merge_latency(base[key], value) + else: + merged[key] = value + return merged + + def merge(base: dict, extra: dict) -> dict: """``base`` with ``extra``'s candidates and latencies added (same commit and GPU).""" for field in ("rl_kernel_commit", "tracked_tree_dirty", "shape", "op"): @@ -65,13 +76,7 @@ def merge(base: dict, extra: dict) -> dict: keys = {c["key"] for c in base["candidates"]} added = [c for c in extra["candidates"] if c["key"] not in keys] merged = dict(base, candidates=base["candidates"] + added) - merged["latency"] = { - section: { - size: {**values, **extra["latency"][section].get(size, {})} - for size, values in sizes.items() - } - for section, sizes in base["latency"].items() - } + merged["latency"] = _merge_latency(base["latency"], extra["latency"]) merged["environments"] = {"main": base["environment"], "extra": extra["environment"]} return merged From 7317e5a806deaff2c4210c85205d28d2a1e1e205 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sat, 10 Oct 2026 20:00:16 +0000 Subject: [PATCH 14/68] style(tools): mark the Qwen3-Next TP gate imports after sys.path setup as E402-exempt The gate scripts insert the repo root into sys.path before importing rl_engine and tools, which flake8 reports as E402. Mark those imports with noqa, as the other tools/ scripts do. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- tools/validation/models/qwen3_next_tp_moe_check.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tools/validation/models/qwen3_next_tp_moe_check.py b/tools/validation/models/qwen3_next_tp_moe_check.py index c9079b7df..775c9a84b 100644 --- a/tools/validation/models/qwen3_next_tp_moe_check.py +++ b/tools/validation/models/qwen3_next_tp_moe_check.py @@ -32,21 +32,23 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[3])) -import torch -import torch.distributed as dist -from safetensors import safe_open +import torch # noqa: E402 +import torch.distributed as dist # noqa: E402 +from safetensors import safe_open # noqa: E402 -from rl_engine.models.qwen3_next.qwen3_next_forward import ( +from rl_engine.models.qwen3_next.qwen3_next_forward import ( # noqa: E402 FORWARD_PROVIDER_ID, shared_router, stable_top10_routes, ) -from rl_engine.models.qwen3_next.qwen3_next_tp_blocks import ( +from rl_engine.models.qwen3_next.qwen3_next_tp_blocks import ( # noqa: E402 TP4MoE, assemble_moe_weight, moe_hf_shapes, ) -from rl_engine.validation.common.tensor_identity import assert_tensor_bitwise_equal as exact +from rl_engine.validation.common.tensor_identity import ( # noqa: E402 + assert_tensor_bitwise_equal as exact, +) ROOT = Path(__file__).resolve().parents[3] TOKEN_COUNTS = (8, 64, 256, 1024) From 5cacf980c77d1ee29ecb90a0f14838be5331bc97 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sat, 10 Oct 2026 20:00:17 +0000 Subject: [PATCH 15/68] style(tools): mark the Qwen3-Next TP gate imports after sys.path setup as E402-exempt The gate scripts insert the repo root into sys.path before importing rl_engine and tools, which flake8 reports as E402. Mark those imports with noqa, as the other tools/ scripts do. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../models/qwen3_next_tp_attention_check.py | 12 +++++++----- .../models/qwen3_next_tp_gdn_check.py | 18 +++++++++++------- .../models/qwen3_next_tp_moe_check.py | 17 +++++++++++------ 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/tools/validation/models/qwen3_next_tp_attention_check.py b/tools/validation/models/qwen3_next_tp_attention_check.py index 6a39585e2..a94487e35 100644 --- a/tools/validation/models/qwen3_next_tp_attention_check.py +++ b/tools/validation/models/qwen3_next_tp_attention_check.py @@ -20,16 +20,18 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[3])) -import torch -import torch.distributed as dist +import torch # noqa: E402 +import torch.distributed as dist # noqa: E402 -from rl_engine.models.qwen3_next.qwen3_next_tp_blocks import ( +from rl_engine.models.qwen3_next.qwen3_next_tp_blocks import ( # noqa: E402 ATTENTION_HF_SHAPES, TP4FullAttention, assemble_attention_weights, ) -from rl_engine.validation.common.tensor_identity import assert_tensor_bitwise_equal as exact -from tools.validation.models.qwen3_next_gate_common import ( +from rl_engine.validation.common.tensor_identity import ( # noqa: E402 + assert_tensor_bitwise_equal as exact, +) +from tools.validation.models.qwen3_next_gate_common import ( # noqa: E402 CheckpointWeights, check_gradients, gather, diff --git a/tools/validation/models/qwen3_next_tp_gdn_check.py b/tools/validation/models/qwen3_next_tp_gdn_check.py index 79a1fb29c..65fee3815 100644 --- a/tools/validation/models/qwen3_next_tp_gdn_check.py +++ b/tools/validation/models/qwen3_next_tp_gdn_check.py @@ -20,18 +20,22 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[3])) -import torch -import torch.distributed as dist -from safetensors import safe_open +import torch # noqa: E402 +import torch.distributed as dist # noqa: E402 +from safetensors import safe_open # noqa: E402 -from rl_engine.integrations.engines.train.vllm.qwen3_next_provider import GDNProviderConfig -from rl_engine.models.qwen3_next.qwen3_next_tp_gdn import ( +from rl_engine.integrations.engines.train.vllm.qwen3_next_provider import ( # noqa: E402 + GDNProviderConfig, +) +from rl_engine.models.qwen3_next.qwen3_next_tp_gdn import ( # noqa: E402 _GLOBAL_SHAPES, TP4GDN, assemble_gdn_weights, ) -from rl_engine.validation.common.tensor_identity import assert_tensor_bitwise_equal as exact -from tools.validation.models.qwen3_next_gate_common import check_gradients, run_gate +from rl_engine.validation.common.tensor_identity import ( # noqa: E402 + assert_tensor_bitwise_equal as exact, +) +from tools.validation.models.qwen3_next_gate_common import check_gradients, run_gate # noqa: E402 def load_first_gdn(checkpoint): diff --git a/tools/validation/models/qwen3_next_tp_moe_check.py b/tools/validation/models/qwen3_next_tp_moe_check.py index 80c88ec2f..c18b5b1fd 100644 --- a/tools/validation/models/qwen3_next_tp_moe_check.py +++ b/tools/validation/models/qwen3_next_tp_moe_check.py @@ -25,17 +25,22 @@ sys.path.insert(0, str(Path(__file__).resolve().parents[3])) -import torch -import torch.distributed as dist +import torch # noqa: E402 +import torch.distributed as dist # noqa: E402 -from rl_engine.models.qwen3_next.qwen3_next_forward import shared_router, stable_top10_routes -from rl_engine.models.qwen3_next.qwen3_next_tp_blocks import ( +from rl_engine.models.qwen3_next.qwen3_next_forward import ( # noqa: E402 + shared_router, + stable_top10_routes, +) +from rl_engine.models.qwen3_next.qwen3_next_tp_blocks import ( # noqa: E402 TP4MoE, assemble_moe_weight, moe_hf_shapes, ) -from rl_engine.validation.common.tensor_identity import assert_tensor_bitwise_equal as exact -from tools.validation.models.qwen3_next_gate_common import ( +from rl_engine.validation.common.tensor_identity import ( # noqa: E402 + assert_tensor_bitwise_equal as exact, +) +from tools.validation.models.qwen3_next_gate_common import ( # noqa: E402 CheckpointWeights, check_gradients, gather, From 52b97e3c788291f6611228aac3c46e4a232c8cb6 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 00:34:36 +0000 Subject: [PATCH 16/68] fix(qwen3-next): type annotations for mypy in the MoE prior-art runner Annotate the latency samples and accept the default-argument lambdas used as timed calls. No behaviour change. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- rl_engine/validation/models/qwen3_next_moe_prior_art.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rl_engine/validation/models/qwen3_next_moe_prior_art.py b/rl_engine/validation/models/qwen3_next_moe_prior_art.py index c0582726f..7c0cc33c9 100644 --- a/rl_engine/validation/models/qwen3_next_moe_prior_art.py +++ b/rl_engine/validation/models/qwen3_next_moe_prior_art.py @@ -64,14 +64,14 @@ def _sample_us(fn: Callable[[], Any]) -> float: return start.elapsed_time(end) * 1000.0 -def time_interleaved(calls: dict[str, Callable[[], Any]], warmup: int = 5, iters: int = 30): +def time_interleaved(calls: dict[str, Callable[..., Any]], warmup: int = 5, iters: int = 30): """Median latency per candidate; the order alternates so none always runs first.""" keys = list(calls) orders = (keys, list(reversed(keys))) for i in range(warmup): for key in orders[i % 2]: calls[key]() - samples = {key: [] for key in keys} + samples: dict[str, list[float]] = {key: [] for key in keys} for i in range(iters): for key in orders[i % 2]: samples[key].append(_sample_us(calls[key])) From 4d7d3d8dbe1d62dc28a0d7fc6d98e90da4a0c347 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 00:37:02 +0000 Subject: [PATCH 17/68] fix(qwen3-next): type annotations for mypy in the attention prior-art runner Annotate the latency samples and accept the default-argument lambdas used as timed calls. No behaviour change. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- rl_engine/validation/models/qwen3_next_attention_prior_art.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rl_engine/validation/models/qwen3_next_attention_prior_art.py b/rl_engine/validation/models/qwen3_next_attention_prior_art.py index 785040e46..db705b56f 100644 --- a/rl_engine/validation/models/qwen3_next_attention_prior_art.py +++ b/rl_engine/validation/models/qwen3_next_attention_prior_art.py @@ -56,13 +56,13 @@ def _sample_us(fn: Callable[[], Any]) -> float: return start.elapsed_time(end) * 1000.0 -def time_interleaved(calls: dict[str, Callable[[], Any]], warmup: int = 5, iters: int = 30): +def time_interleaved(calls: dict[str, Callable[..., Any]], warmup: int = 5, iters: int = 30): keys = list(calls) orders = (keys, list(reversed(keys))) for i in range(warmup): for key in orders[i % 2]: calls[key]() - samples = {key: [] for key in keys} + samples: dict[str, list[float]] = {key: [] for key in keys} for i in range(iters): for key in orders[i % 2]: samples[key].append(_sample_us(calls[key])) From b66a8d081fedada12f55f44c47d707dda4e649c9 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 01:33:38 +0000 Subject: [PATCH 18/68] build(pre-commit): leave the byte-exact Qwen3-Next chain evidence untouched The published chain evidence is a byte-exact copy covered by SHA256SUMS; a pytest JUnit XML without a final newline would otherwise be rewritten by end-of-file-fixer and break the checksum. Same treatment as reports/archive/. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .pre-commit-config.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3e37f8bd1..9ef274150 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,9 +4,10 @@ repos: hooks: - id: trailing-whitespace # Unified diffs include significant context markers and pinned hashes. - exclude: '(\.patch$|^reports/archive/)' + # Published Qwen3-Next chain evidence is byte-exact (SHA256SUMS). + exclude: '(\.patch$|^reports/archive/|^docs/usage/evidence/qwen3-next-chain-b200/)' - id: end-of-file-fixer - exclude: ^reports/archive/ + exclude: '^(reports/archive/|docs/usage/evidence/qwen3-next-chain-b200/)' - id: check-yaml exclude: 'mkdocs\.yaml$' - id: check-added-large-files From 848ea449831775f2edbe58f8b7b222f5e942591d Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 01:34:26 +0000 Subject: [PATCH 19/68] build(pre-commit): leave the byte-exact batch-invariance evidence untouched Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .pre-commit-config.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9ef274150..a38433e48 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,10 +4,10 @@ repos: hooks: - id: trailing-whitespace # Unified diffs include significant context markers and pinned hashes. - # Published Qwen3-Next chain evidence is byte-exact (SHA256SUMS). - exclude: '(\.patch$|^reports/archive/|^docs/usage/evidence/qwen3-next-chain-b200/)' + # Published Qwen3-Next chain and batch-invariance evidence is byte-exact (SHA256SUMS). + exclude: '(\.patch$|^reports/archive/|^docs/usage/evidence/qwen3-next-(chain|batch-invariance)-b200/)' - id: end-of-file-fixer - exclude: '^(reports/archive/|docs/usage/evidence/qwen3-next-chain-b200/)' + exclude: '^(reports/archive/|docs/usage/evidence/qwen3-next-(chain|batch-invariance)-b200/)' - id: check-yaml exclude: 'mkdocs\.yaml$' - id: check-added-large-files From 84a7bfeda1464de6fd6a32854933cbee10b83609 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 01:51:37 +0000 Subject: [PATCH 20/68] test(qwen3-next): pin the case-manifest subprocess rootdir to its temp directory Node ids are relative to pytest's rootdir. When the temp directory sits under another checkout, the subprocess picked that checkout as rootdir and the manifest's case id no longer matched. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- tests/models/qwen3_next/test_qwen3_next_case_manifest.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/models/qwen3_next/test_qwen3_next_case_manifest.py b/tests/models/qwen3_next/test_qwen3_next_case_manifest.py index d3897b648..7d78a236f 100644 --- a/tests/models/qwen3_next/test_qwen3_next_case_manifest.py +++ b/tests/models/qwen3_next/test_qwen3_next_case_manifest.py @@ -81,6 +81,9 @@ def test_plugin_enforces_real_pytest_outcomes(tmp_path, outcome): "-q", "-p", "no:cacheprovider", + # Node ids are relative to the rootdir; keep it here even under another checkout. + "--rootdir", + str(tmp_path), "-p", "rl_engine.validation.models.pytest_case_manifest", "--case-manifest", From cbb7b3d948b98f86cc275319fb4ae4df9fb7b4b6 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:34:53 +0000 Subject: [PATCH 21/68] docs(qwen3-next): figure for the full-model chain evidence tools/validation/models/plot_qwen3_next_chain.py draws, from the published evidence only, the rollout-vs-recompute raw-bit matrix (16 cases x 4 weight versions x tokens/logits/logprobs/layer digests) and the per-layer digest change between weight versions. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../models/plot_qwen3_next_chain.py | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 tools/validation/models/plot_qwen3_next_chain.py diff --git a/tools/validation/models/plot_qwen3_next_chain.py b/tools/validation/models/plot_qwen3_next_chain.py new file mode 100644 index 000000000..f3eab8718 --- /dev/null +++ b/tools/validation/models/plot_qwen3_next_chain.py @@ -0,0 +1,152 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Render a published Qwen3-Next full-model chain as a PNG. + +Needs only the published evidence directory and matplotlib: + + python tools/validation/models/plot_qwen3_next_chain.py \\ + docs/usage/evidence/qwen3-next-chain-b200 + +writes ``figure.png`` into that directory. Left: for each L2 case and weight +version, whether the vLLM rollout and the VIME recompute agree bit for bit on +response tokens, logits, logprobs and the per-layer digests. Right: for each +decoder layer, the share of cases whose layer digest changes from one weight +version to the next, which shows the updates reach every layer and the reload +reproduces update 2 exactly. +""" + +from __future__ import annotations + +import argparse +import json +from pathlib import Path + +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt # noqa: E402 +import numpy as np # noqa: E402 +from matplotlib.colors import ListedColormap # noqa: E402 + +INK = "#2b2b29" +MUTED = "#6f6e69" +SURFACE = "#fcfcfb" +PASS = "#1baf7a" +FAIL = "#d64545" +CHANGE = ("#e4e3dd", "#2a78d6") + +STAGES = (("l2", "baseline"), ("train1", "update 1"), ("train2", "update 2"), ("train", "reload")) +COMPARISONS = ( + ("response_tokens", "tokens"), + ("logits", "logits"), + ("logprobs", "logprobs"), + ("layer_states", "layer digests"), +) +STEPS = (("baseline", "update1"), ("update1", "update2"), ("update2", "reload")) + + +def _node_order(name): + return (0, 0) if name == "embedding" else (1, int(name.removeprefix("layer"))) + + +def plot(evidence: Path, out: Path) -> None: + matrices = { + label: json.loads((evidence / stage / "l2_matrix.json").read_text()) + for stage, label in STAGES + } + cases = matrices["baseline"]["case_ids"] + grid = np.array( + [ + [ + bool(matrices[label]["cases"][case]["comparisons"][key]) + for _, label in STAGES + for key, _ in COMPARISONS + ] + for case in cases + ] + ) + report = json.loads((evidence / "chain-report.json").read_text()) + cells = report["cells"] + nodes = sorted(cells[f"baseline/{cases[0]}"]["node_digests"], key=_node_order) + change = np.array( + [ + [ + np.mean( + [ + cells[f"{a}/{case}"]["node_digests"][node] + != cells[f"{b}/{case}"]["node_digests"][node] + for case in cases + ] + ) + for node in nodes + ] + for a, b in STEPS + ] + ) + + fig, (left, right) = plt.subplots( + 1, 2, figsize=(16, 6.2), gridspec_kw={"width_ratios": [1, 1.55]}, facecolor=SURFACE + ) + left.imshow(grid, cmap=ListedColormap([FAIL, PASS]), vmin=0, vmax=1, aspect="auto") + left.set_yticks(range(len(cases)), cases, fontsize=8, color=INK) + names = [name for _, name in COMPARISONS] + left.set_xticks(range(grid.shape[1]), names * len(STAGES), rotation=90, fontsize=8) + for i, (_, label) in enumerate(STAGES): + left.text(i * 4 + 1.5, -1.2, label, ha="center", fontsize=9, color=INK) + if i: + left.axvline(i * 4 - 0.5, color=SURFACE, linewidth=3) + left.set_title( + f"vLLM rollout vs VIME recompute, raw bits\n{int(grid.sum())}/{grid.size} equal", + fontsize=10, + color=INK, + pad=24, + ) + + right.imshow(change, cmap=ListedColormap(CHANGE), vmin=0, vmax=1, aspect="auto") + right.set_yticks(range(len(STEPS)), [f"{a} → {b}" for a, b in STEPS], fontsize=9, color=INK) + labels = ["emb" if n == "embedding" else n.removeprefix("layer") for n in nodes] + right.set_xticks(range(len(nodes)), labels, fontsize=6, color=MUTED) + right.set_xlabel("embedding and decoder layers 0-47", fontsize=9, color=MUTED) + for row in range(len(STEPS)): + right.text( + len(nodes) + 0.6, + row, + f"{change[row].mean():.0%} of digests change", + va="center", + fontsize=8, + color=INK, + ) + right.set_xlim(-0.5, len(nodes) + 9) + right.set_title( + "Per-layer digest change between weight versions (share of the 16 cases)", + fontsize=10, + color=INK, + ) + for axis in (left, right): + for spine in axis.spines.values(): + spine.set_visible(False) + commit = report["measured_commit"][:7] + gpu = f" on {report['gpu_name']}" if report.get("gpu_name") else "" + fig.suptitle( + f"Qwen3-Next-80B-A3B full-model chain, TP4{gpu}, commit {commit}: " + f"passed={report['passed']}, first drift={report['first_drift']}", + fontsize=11, + color=INK, + ) + fig.tight_layout() + fig.savefig(out, dpi=160) + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("evidence", type=Path) + args = parser.parse_args() + out = args.evidence / "figure.png" + plot(args.evidence, out) + print(f"wrote {out}") + + +if __name__ == "__main__": + main() From 9cc2fbc7d916c15ad1d8720f5377c9a86576cc5d Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:35:41 +0000 Subject: [PATCH 22/68] docs(qwen3-next): figure for the batch-invariance evidence tools/validation/models/plot_qwen3_next_batch_invariance.py draws, from the published evidence only, one panel per cell family (concurrency, chunk boundaries, interference, packing); a cell is green when tokens, logits, logprobs and every layer digest are bitwise equal to its reference. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../plot_qwen3_next_batch_invariance.py | 144 ++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 tools/validation/models/plot_qwen3_next_batch_invariance.py diff --git a/tools/validation/models/plot_qwen3_next_batch_invariance.py b/tools/validation/models/plot_qwen3_next_batch_invariance.py new file mode 100644 index 000000000..21a128a9f --- /dev/null +++ b/tools/validation/models/plot_qwen3_next_batch_invariance.py @@ -0,0 +1,144 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Render a published Qwen3-Next batch-invariance matrix as a PNG. + +Needs only the published evidence directory and matplotlib: + + python tools/validation/models/plot_qwen3_next_batch_invariance.py \\ + docs/usage/evidence/qwen3-next-batch-invariance-b200 + +writes ``figure.png`` into that directory: one panel per cell family. A cell is +green when the target request's response tokens, logits, logprobs and every +layer digest are bitwise equal to its reference, red otherwise. +""" + +from __future__ import annotations + +import argparse +import json +import re +from pathlib import Path + +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt # noqa: E402 +import numpy as np # noqa: E402 +from matplotlib.colors import ListedColormap # noqa: E402 + +INK = "#2b2b29" +MUTED = "#6f6e69" +SURFACE = "#fcfcfb" +NA = "#e4e3dd" +COLORS = ListedColormap([NA, "#d64545", "#1baf7a"]) # 0 absent, 1 differs, 2 bitwise equal + + +def _value(cell): + return 2 if cell["passed"] and all(cell["comparisons"].values()) else 1 + + +def _grid(axis, values, rows, cols, title, xlabel=None, ylabel=None): + axis.imshow(values, cmap=COLORS, vmin=0, vmax=2, aspect="auto") + axis.set_yticks(range(len(rows)), rows, fontsize=8, color=INK) + axis.set_xticks(range(len(cols)), cols, fontsize=8, color=INK, rotation=0) + axis.set_xticks(np.arange(-0.5, len(cols)), minor=True) + axis.set_yticks(np.arange(-0.5, len(rows)), minor=True) + axis.grid(which="minor", color=SURFACE, linewidth=2) + axis.tick_params(which="minor", length=0) + passed = int((values == 2).sum()) + total = int((values > 0).sum()) + axis.set_title(f"{title}: {passed}/{total} bitwise", fontsize=10, color=INK) + if xlabel: + axis.set_xlabel(xlabel, fontsize=9, color=MUTED) + if ylabel: + axis.set_ylabel(ylabel, fontsize=9, color=MUTED) + for spine in axis.spines.values(): + spine.set_visible(False) + + +def plot(evidence: Path, out: Path) -> None: + matrix = json.loads((evidence / "bi" / "bi_matrix.json").read_text()) + cells = matrix["cells"] + + conc = {} + for cid, cell in cells.items(): + m = re.fullmatch(r"concurrency-n(\d+)-(first|last)", cid) + if m: + conc[(int(m.group(1)), m.group(2))] = _value(cell) + ns = sorted({n for n, _ in conc}) + conc_grid = np.array([[conc.get((n, p), 0) for n in ns] for p in ("first", "last")]) + + chunk = {} + for cid, cell in cells.items(): + m = re.fullmatch(r"chunk-p(\d+)-c(\d+)-f(\d+)", cid) + if m: + chunk[(int(m.group(1)), int(m.group(2)), int(m.group(3)))] = _value(cell) + prompts = sorted({p for p, _, _ in chunk}) + plans = sorted({(c, f) for _, c, f in chunk}) + chunk_grid = np.array([[chunk.get((p, c, f), 0) for c, f in plans] for p in prompts]) + + def listed(prefix): + ids = [cid for cid in matrix["cell_ids"] if cid.startswith(prefix)] + return ids, np.array([[_value(cells[cid])] for cid in ids]) + + inter_ids, inter_grid = listed("interference-") + pack_ids, pack_grid = listed("packing-") + + fig = plt.figure(figsize=(16, 8.5), facecolor=SURFACE) + spec = fig.add_gridspec(2, 3, width_ratios=[1.5, 0.9, 0.9], height_ratios=[1, 2.6]) + _grid( + fig.add_subplot(spec[0, 0]), + conc_grid, + ["target first", "target last"], + [str(n) for n in ns], + "Concurrency (N requests, vs. the target alone)", + xlabel="N", + ) + _grid( + fig.add_subplot(spec[1, 0]), + chunk_grid, + [str(p) for p in prompts], + [f"c{c}\nfirst {f}" for c, f in plans], + "Chunk boundaries (vs. the replay engine)", + xlabel="chunk size / first chunk", + ylabel="prompt tokens", + ) + _grid( + fig.add_subplot(spec[:, 1]), + inter_grid, + [cid.removeprefix("interference-") for cid in inter_ids], + [""], + "Interference", + ) + _grid( + fig.add_subplot(spec[:, 2]), + pack_grid, + [cid.removeprefix("packing-") for cid in pack_ids], + [""], + "Packing", + ) + total = len(cells) + passed = sum(_value(cell) == 2 for cell in cells.values()) + fig.suptitle( + f"Qwen3-Next-80B-A3B batch invariance, TP4, suite {matrix.get('suite', '')}: " + f"{passed}/{total} cells bitwise (tokens, logits, logprobs, layer digests)", + fontsize=11, + color=INK, + ) + fig.tight_layout() + fig.savefig(out, dpi=160) + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("evidence", type=Path) + args = parser.parse_args() + out = args.evidence / "figure.png" + plot(args.evidence, out) + print(f"wrote {out}") + + +if __name__ == "__main__": + main() From bddabba77f7da8f160d15a50b5792a517a1efd1c Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 15:48:29 +0000 Subject: [PATCH 23/68] test(qwen3-next): digest every TP4 cross-rank point and compare across NCCL settings qwen3_next_logprob_parity.py records, on real weights, SHA-256 digests at the vocabulary-parallel embedding, the row-parallel outputs and input gradients of GDN layer 0, full-attention layer 3 and the layer-0 MoE block, the TP4 selected-token logprob/LSE/entropy and its logits gradient, the gathered sampling logits and the TP1 logprob over the same logits; plus a negative control reducing real o_proj partials with NCCL all_reduce. qwen3_next_logprob_parity_compare.py checks one digest per rank across NCCL algorithm, protocol and channel settings, and TP4 == TP1. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../models/qwen3_next_logprob_parity.py | 221 ++++++++++++++++++ .../qwen3_next_logprob_parity_compare.py | 86 +++++++ 2 files changed, 307 insertions(+) create mode 100644 tools/validation/models/qwen3_next_logprob_parity.py create mode 100644 tools/validation/models/qwen3_next_logprob_parity_compare.py diff --git a/tools/validation/models/qwen3_next_logprob_parity.py b/tools/validation/models/qwen3_next_logprob_parity.py new file mode 100644 index 000000000..17b0bd1d3 --- /dev/null +++ b/tools/validation/models/qwen3_next_logprob_parity.py @@ -0,0 +1,221 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Digest every cross-rank point of the Qwen3-Next TP4 path on real weights. + + torchrun --nproc-per-node 4 tools/validation/models/qwen3_next_logprob_parity.py \\ + --checkpoint --output + +Run it once per NCCL setting (``NCCL_ALGO``, ``NCCL_PROTO``, ``NCCL_MAX_NCHANNELS``) +and compare the reports with ``qwen3_next_logprob_parity_compare.py``. Each rank +writes ``rank-.json`` with SHA-256 digests of the raw bytes at every point +where ranks combine values: + +* the vocabulary-parallel embedding and its weight gradient; +* the row-parallel outputs and input gradients of GDN layer 0, full-attention + layer 3 and the MoE block of layer 0; +* the TP4 selected-token logprob, LSE and entropy (and the logits gradient), + the gathered sampling logits, and the same logprob computed on one rank over + the gathered full-vocabulary logits (TP1), which must be bitwise equal; +* a negative control: the attention ``o_proj`` row-parallel partials reduced by + RL-Kernel's deterministic collective and, separately, by ``torch.distributed`` + ``all_reduce`` on NCCL. +""" + +import hashlib +import os +import sys +from contextlib import ExitStack +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[3])) + +import torch # noqa: E402 +import torch.distributed as dist # noqa: E402 + +from rl_engine.contracts.operators.logprob import ( # noqa: E402 + LogprobContract, + LogprobDType, + LogprobRole, + MaskSpec, + ReductionSpec, + ShardingSpec, +) +from rl_engine.integrations.engines.train.vllm.qwen3_next_provider import ( # noqa: E402 + GDNProviderConfig, +) +from rl_engine.models.qwen3_next.qwen3_next_embedding import shared_embedding # noqa: E402 +from rl_engine.models.qwen3_next.qwen3_next_forward import ( # noqa: E402 + shared_linear, + shared_logprob, +) +from rl_engine.models.qwen3_next.qwen3_next_logprob import qwen_logprob_contract # noqa: E402 +from rl_engine.models.qwen3_next.qwen3_next_tp import _reduce # noqa: E402 +from rl_engine.models.qwen3_next.qwen3_next_tp_blocks import ( # noqa: E402 + ATTENTION_HF_SHAPES, + TP4FullAttention, + TP4MoE, + moe_hf_shapes, +) +from rl_engine.models.qwen3_next.qwen3_next_tp_gdn import TP4GDN # noqa: E402 +from tools.validation.models.qwen3_next_gate_common import CheckpointWeights, run_gate # noqa: E402 +from tools.validation.models.qwen3_next_tp_gdn_check import load_first_gdn # noqa: E402 + +TOKENS = 1024 +VOCAB, SHARD = 151936, 37984 +NCCL_SETTINGS = ("NCCL_ALGO", "NCCL_PROTO", "NCCL_MAX_NCHANNELS", "NCCL_MIN_NCHANNELS") + + +def digest(tensor): + return hashlib.sha256(tensor.detach().contiguous().view(torch.uint8).cpu().numpy()).hexdigest() + + +def _hidden(generator): + return torch.randn(TOKENS, 2048, device="cuda", dtype=torch.bfloat16, generator=generator) * 0.1 + + +def _full_vocab_contract(n, dtype): + return LogprobContract( + role=LogprobRole.TRAIN, + dtype=dtype, + mask=MaskSpec(num_tokens=n, active_mask=(True,) * n), + sharding=ShardingSpec( + tp_rank=0, + tp_world_size=1, + vocab_shard_bounds=((0, VOCAB),), + real_vocab_size=VOCAB, + padded_vocab_size=VOCAB, + cp_rank=0, + cp_world_size=1, + ), + reduction=ReductionSpec(), + ) + + +def _block(name, module, run, generator): + """Forward output and input-gradient digests of one TP4 block.""" + hidden = _hidden(generator).requires_grad_(True) + out = run(module, hidden) + dy = torch.randn(out.shape, device="cuda", dtype=out.dtype, generator=generator) + (grad,) = torch.autograd.grad(out, hidden, dy) + return {f"{name}.forward": digest(out), f"{name}.input_grad": digest(grad)} + + +def parity(checkpoint, generator): + rank = dist.get_rank() + group = dist.group.WORLD + points = {} + with ExitStack() as stack: + top = CheckpointWeights( + checkpoint, "", ("model.embed_tokens.weight", "lm_head.weight"), stack + ) + embedding = top["model.embed_tokens.weight"][rank * SHARD : (rank + 1) * SHARD] + embedding = embedding.cuda().requires_grad_(True) + lm_head_full = top["lm_head.weight"].cuda() + + tokens = torch.randint(0, VOCAB, (TOKENS,), device="cuda", generator=generator) + embedded = shared_embedding(tokens, embedding, group) + dy = torch.randn(embedded.shape, device="cuda", dtype=embedded.dtype, generator=generator) + (weight_grad,) = torch.autograd.grad(embedded, embedding, dy) + points["embedding.forward"] = digest(embedded) + points["embedding.weight_grad"] = digest(weight_grad) + del embedding, weight_grad + + gdn = TP4GDN(group=group, device="cuda", config=GDNProviderConfig()) + gdn.load_hf_weights(load_first_gdn(checkpoint)) + indices = torch.tensor([1], device="cuda", dtype=torch.int32) + bounds = torch.tensor([0, TOKENS]) + points.update( + _block( + "gdn_layer0", + gdn, + lambda m, h: m(h, m.initial_state(2), indices, bounds)[0], + generator, + ) + ) + del gdn + + attention = TP4FullAttention(group=group, device="cuda") + attention.load_hf_weights( + CheckpointWeights(checkpoint, "model.layers.3.self_attn.", ATTENTION_HF_SHAPES, stack) + ) + points.update( + _block( + "attention_layer3", + attention, + lambda m, h: m(h, m.initial_state(2), indices, bounds)[0], + generator, + ) + ) + # Negative control on a real row-parallel projection: the same local + # partials reduced by the deterministic collective and by NCCL all_reduce. + with torch.no_grad(): + local_in = _hidden(generator)[:, :1024].contiguous() + partial = shared_linear(local_in, attention.o_proj.weight) + points["o_proj_partials.deterministic"] = digest(_reduce(partial, group)) + nccl = partial.clone() + dist.all_reduce(nccl, group=group) + points["o_proj_partials.nccl_all_reduce"] = digest(nccl) + del attention + + moe = TP4MoE(group=group, device="cuda") + moe.load_hf_weights( + CheckpointWeights(checkpoint, "model.layers.0.mlp.", moe_hf_shapes(), stack) + ) + points.update(_block("moe_layer0", moe, lambda m, h: m(h), generator)) + del moe + + hidden = _hidden(generator) + targets = torch.randint(0, VOCAB, (TOKENS,), device="cuda", generator=generator) + lm_head = lm_head_full[rank * SHARD : (rank + 1) * SHARD].contiguous() + local = shared_linear(hidden, lm_head).requires_grad_(True) + contract = qwen_logprob_contract(local, rank=rank, world_size=4) + selected, lse, entropy = shared_logprob(local, targets, contract=contract, tp_group=group) + dsel = torch.randn(selected.shape, device="cuda", dtype=selected.dtype, generator=generator) + (logit_grad,) = torch.autograd.grad(selected, local, dsel) + points["logprob.tp4.selected"] = digest(selected) + points["logprob.tp4.lse"] = digest(lse) + points["logprob.tp4.entropy"] = digest(entropy) + points["logprob.tp4.logit_grad"] = digest(logit_grad) + + with torch.no_grad(): + peers = [torch.empty_like(local) for _ in range(4)] + dist.all_gather(peers, local.detach().contiguous()) + gathered = torch.cat(peers, dim=1) + points["sampling_logits.gathered"] = digest(gathered) + # TP1 sees the same logits as the four shards: only the cross-rank + # merge differs, which is what the vocabulary-parallel contract fixes. + dtype = {torch.bfloat16: LogprobDType.BF16, torch.float32: LogprobDType.FP32}[ + gathered.dtype + ] + tp1 = shared_logprob(gathered, targets, contract=_full_vocab_contract(TOKENS, dtype)) + full = shared_linear(hidden, lm_head_full) + points["logprob.tp1.selected"] = digest(tp1[0]) + points["logprob.tp1.lse"] = digest(tp1[1]) + points["logprob.tp1.entropy"] = digest(tp1[2]) + points["full_logits.matches_gathered"] = bool(torch.equal(full, gathered)) + + for peer_points in _gather_objects(points): + for key in points: + if key.startswith(("embedding.forward", "logprob.tp4.selected", "sampling_logits")): + if peer_points[key] != points[key]: + raise AssertionError(f"{key} differs between ranks") + for part in ("selected", "lse", "entropy"): + if points[f"logprob.tp4.{part}"] != points[f"logprob.tp1.{part}"]: + raise AssertionError(f"TP4 {part} differs from TP1") + return { + "tokens": TOKENS, + "nccl_settings": {name: os.environ.get(name) for name in NCCL_SETTINGS}, + "points": points, + } + + +def _gather_objects(value): + gathered = [None] * dist.get_world_size() + dist.all_gather_object(gathered, value) + return gathered + + +if __name__ == "__main__": + run_gate(__doc__, "tp4_distributed_logprob_parity", parity) diff --git a/tools/validation/models/qwen3_next_logprob_parity_compare.py b/tools/validation/models/qwen3_next_logprob_parity_compare.py new file mode 100644 index 000000000..8fc3e4698 --- /dev/null +++ b/tools/validation/models/qwen3_next_logprob_parity_compare.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Compare ``qwen3_next_logprob_parity.py`` runs made under different NCCL settings. + + python tools/validation/models/qwen3_next_logprob_parity_compare.py \\ + --runs ... --out report.json + +Every point except the raw-NCCL negative control must have one digest per rank +across all settings, and the TP4 logprob must equal TP1. The report records how +many distinct digests each point produced, per rank. +""" + +import argparse +import json +from pathlib import Path + +CONTROL = "o_proj_partials.nccl_all_reduce" + + +def load(run: Path): + ranks = sorted(run.glob("rank-*.json")) + if [p.name for p in ranks] != [f"rank-{r}.json" for r in range(4)]: + raise SystemExit(f"{run} does not hold four rank reports") + return [json.loads(p.read_text()) for p in ranks] + + +def compare(runs): + reports = {run.name: load(run) for run in runs} + first = next(iter(reports.values())) + commits = {r["rl_kernel_commit"] for rs in reports.values() for r in rs} + dirty = any(r["tracked_tree_dirty"] for rs in reports.values() for r in rs) + one_commit = len(commits) == 1 + points = sorted(k for k, v in first[0]["points"].items() if isinstance(v, str)) + table = {} + for point in points: + per_rank = [] + for rank in range(4): + per_rank.append(len({rs[rank]["points"][point] for rs in reports.values()})) + table[point] = {"distinct_digests_per_rank": per_rank} + deterministic = [p for p in points if p != CONTROL] + invariant = all(max(table[p]["distinct_digests_per_rank"]) == 1 for p in deterministic) + tp1 = all( + r["points"][f"logprob.tp4.{part}"] == r["points"][f"logprob.tp1.{part}"] + for rs in reports.values() + for r in rs + for part in ("selected", "lse", "entropy") + ) + return { + "kind": "qwen3_next_distributed_logprob_parity", + "rfc": 428, + "rl_kernel_commit": next(iter(commits)) if one_commit else sorted(commits), + "tracked_tree_dirty": dirty, + "environment": first[0]["environment"], + "tokens": first[0]["tokens"], + "settings": {name: rs[0]["nccl_settings"] for name, rs in reports.items()}, + "points": table, + "deterministic_points_invariant": invariant, + "tp4_equals_tp1": tp1, + "nccl_all_reduce_varies": max(table[CONTROL]["distinct_digests_per_rank"]) > 1, + "full_logits_match_gathered": all( + r["points"]["full_logits.matches_gathered"] for rs in reports.values() for r in rs + ), + "passed": invariant and tp1 and one_commit and not dirty, + } + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--runs", nargs="+", type=Path, required=True) + parser.add_argument("--out", type=Path, required=True) + args = parser.parse_args() + report = compare(args.runs) + args.out.parent.mkdir(parents=True, exist_ok=True) + args.out.write_text(json.dumps(report, indent=2) + "\n") + print( + f"passed={report['passed']} invariant={report['deterministic_points_invariant']} " + f"tp4==tp1={report['tp4_equals_tp1']} nccl_varies={report['nccl_all_reduce_varies']}" + ) + if not report["passed"]: + raise SystemExit(1) + + +if __name__ == "__main__": + main() From 99c244aa9ee13a67e3a4b47f3ab620727939ac00 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 19:10:42 +0000 Subject: [PATCH 24/68] test(qwen3-next): require TP4 == TP1 for selected logprob and LSE only The WS2 provider merges per-tile logprob statistics in global tile order, so selected logprob and LSE do not depend on the TP degree; entropy is summed per shard in rank order and is outside the selected-logprob contract. The run records whether entropy matches TP1 instead of failing on it. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- tools/validation/models/qwen3_next_logprob_parity.py | 11 +++++++++-- .../models/qwen3_next_logprob_parity_compare.py | 7 +++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/tools/validation/models/qwen3_next_logprob_parity.py b/tools/validation/models/qwen3_next_logprob_parity.py index 17b0bd1d3..58a3679bd 100644 --- a/tools/validation/models/qwen3_next_logprob_parity.py +++ b/tools/validation/models/qwen3_next_logprob_parity.py @@ -17,7 +17,8 @@ layer 3 and the MoE block of layer 0; * the TP4 selected-token logprob, LSE and entropy (and the logits gradient), the gathered sampling logits, and the same logprob computed on one rank over - the gathered full-vocabulary logits (TP1), which must be bitwise equal; + the gathered full-vocabulary logits (TP1); selected logprob and LSE must be + bitwise equal (entropy is merged in rank order and only recorded); * a negative control: the attention ``o_proj`` row-parallel partials reduced by RL-Kernel's deterministic collective and, separately, by ``torch.distributed`` ``all_reduce`` on NCCL. @@ -201,9 +202,15 @@ def parity(checkpoint, generator): if key.startswith(("embedding.forward", "logprob.tp4.selected", "sampling_logits")): if peer_points[key] != points[key]: raise AssertionError(f"{key} differs between ranks") - for part in ("selected", "lse", "entropy"): + # Selected logprob and LSE merge per-tile statistics in global tile order, so + # they cannot depend on the TP degree. Entropy is summed per shard in rank + # order (WS2 provider); it is recorded, not required, across TP degrees. + for part in ("selected", "lse"): if points[f"logprob.tp4.{part}"] != points[f"logprob.tp1.{part}"]: raise AssertionError(f"TP4 {part} differs from TP1") + points["entropy.tp4_equals_tp1"] = ( + points["logprob.tp4.entropy"] == points["logprob.tp1.entropy"] + ) return { "tokens": TOKENS, "nccl_settings": {name: os.environ.get(name) for name in NCCL_SETTINGS}, diff --git a/tools/validation/models/qwen3_next_logprob_parity_compare.py b/tools/validation/models/qwen3_next_logprob_parity_compare.py index 8fc3e4698..0d128d541 100644 --- a/tools/validation/models/qwen3_next_logprob_parity_compare.py +++ b/tools/validation/models/qwen3_next_logprob_parity_compare.py @@ -8,7 +8,7 @@ --runs ... --out report.json Every point except the raw-NCCL negative control must have one digest per rank -across all settings, and the TP4 logprob must equal TP1. The report records how +across all settings, and the TP4 selected logprob and LSE must equal TP1. The report records how many distinct digests each point produced, per rank. """ @@ -45,7 +45,7 @@ def compare(runs): r["points"][f"logprob.tp4.{part}"] == r["points"][f"logprob.tp1.{part}"] for rs in reports.values() for r in rs - for part in ("selected", "lse", "entropy") + for part in ("selected", "lse") ) return { "kind": "qwen3_next_distributed_logprob_parity", @@ -59,6 +59,9 @@ def compare(runs): "deterministic_points_invariant": invariant, "tp4_equals_tp1": tp1, "nccl_all_reduce_varies": max(table[CONTROL]["distinct_digests_per_rank"]) > 1, + "entropy_tp4_equals_tp1": all( + r["points"]["entropy.tp4_equals_tp1"] for rs in reports.values() for r in rs + ), "full_logits_match_gathered": all( r["points"]["full_logits.matches_gathered"] for rs in reports.values() for r in rs ), From c5cea58d7f8c3d0ff66dcaded9b91540054a38a6 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 20:09:49 +0000 Subject: [PATCH 25/68] test(qwen3-next): prior art for the vocabulary-parallel selected-token logprob Runs RL-Kernel's WS2 provider, vLLM's gather + log_softmax sampler path, Megatron-core vocab_parallel_cross_entropy and VIME's default logprob on the same official-LM-head logits at TP4 and TP1, and records TP-degree and NCCL setting invariance, batch invariance, error against FP64, pairwise differences and latency. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../models/qwen3_next_logprob_prior_art.py | 171 ++++++++++++++++++ .../models/qwen3_next_logprob_prior_art.py | 140 ++++++++++++++ 2 files changed, 311 insertions(+) create mode 100644 rl_engine/validation/models/qwen3_next_logprob_prior_art.py create mode 100644 tools/validation/models/qwen3_next_logprob_prior_art.py diff --git a/rl_engine/validation/models/qwen3_next_logprob_prior_art.py b/rl_engine/validation/models/qwen3_next_logprob_prior_art.py new file mode 100644 index 000000000..f6eb71a06 --- /dev/null +++ b/rl_engine/validation/models/qwen3_next_logprob_prior_art.py @@ -0,0 +1,171 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Prior art for the Qwen3-Next vocabulary-parallel selected-token logprob. + +Every candidate receives the same logits: the full-vocabulary product of the +official LM head with seeded hidden states, computed identically on every rank +and sliced into 37984-column shards for TP4. Only the cross-rank reduction +therefore differs between candidates and between TP degrees. + +Candidates: + +* ``rl_kernel_ws2`` -- RL-Kernel's provider (per-tile max/sum-exp, all-gather + transport, merge in global tile order); what both engines run here. +* ``vllm_gather`` -- vLLM's sampler path: all-gather the logits, then + ``log_softmax(dtype=float32)`` over the full vocabulary. +* ``megatron_ce`` -- Megatron-core ``vocab_parallel_cross_entropy`` (NCCL + all-reduce of max, then of target logit and sum-exp); logprob = -loss. +* ``vime_default`` -- VIME's own ``calculate_log_probs_and_entropy``. + +For each candidate a launch records a digest of the selected logprobs, whether +the first rows computed alone equal the same rows inside the full batch, the +maximum error against an FP64 log-softmax and a median latency. +""" + +from __future__ import annotations + +import hashlib +import statistics +from typing import Any, Callable + +import torch +import torch.distributed as dist + +VOCAB, SHARD = 151936, 37984 +ALONE_ROWS = 16 + + +def digest(tensor: torch.Tensor) -> str: + return hashlib.sha256(tensor.detach().contiguous().view(torch.uint8).cpu().numpy()).hexdigest() + + +def _rl_kernel(tp: int, group): + from rl_engine.contracts.operators.logprob import ( + LogprobContract, + LogprobDType, + LogprobRole, + MaskSpec, + ReductionSpec, + ShardingSpec, + ) + from rl_engine.models.qwen3_next.qwen3_next_forward import shared_logprob + + rank = dist.get_rank(group) if tp > 1 else 0 + + def run(local, targets): + n = local.shape[0] + contract = LogprobContract( + role=LogprobRole.TRAIN, + dtype=LogprobDType.BF16, + mask=MaskSpec(num_tokens=n, active_mask=(True,) * n), + sharding=ShardingSpec( + tp_rank=rank, + tp_world_size=tp, + vocab_shard_bounds=tuple( + (i * (VOCAB // tp), (i + 1) * (VOCAB // tp)) for i in range(tp) + ), + real_vocab_size=VOCAB, + padded_vocab_size=VOCAB, + cp_rank=0, + cp_world_size=1, + ), + reduction=ReductionSpec(), + ) + return shared_logprob( + local, + targets, + contract=contract, + tp_group=group if tp > 1 else None, + with_entropy=False, + )[0] + + return run + + +def _vllm_gather(tp: int, group): + from vllm.v1.sample.sampler import Sampler + + def run(local, targets): + full = local + if tp > 1: + peers = [torch.empty_like(local) for _ in range(tp)] + dist.all_gather(peers, local.contiguous(), group=group) + full = torch.cat(peers, dim=1) + logprobs = Sampler.compute_logprobs(full) + return logprobs.gather(1, targets[:, None]).squeeze(1) + + return run + + +def _megatron(tp: int, group): + from megatron.core.tensor_parallel import vocab_parallel_cross_entropy + + def run(local, targets): + loss = vocab_parallel_cross_entropy(local.float()[:, None, :], targets[:, None]) + return -loss.squeeze(1) + + return run + + +def _vime(tp: int, group): + from vime.utils.ppo_utils import calculate_log_probs_and_entropy + + def run(local, targets): + logprob, _ = calculate_log_probs_and_entropy(local.float(), targets, group) + return logprob.reshape(-1) + + return run + + +CANDIDATES: dict[str, Callable[[int, Any], Callable]] = { + "rl_kernel_ws2": _rl_kernel, + "vllm_gather": _vllm_gather, + "megatron_ce": _megatron, + "vime_default": _vime, +} + + +def _median_us(fn, iters=20): + samples = [] + for _ in range(iters): + start = torch.cuda.Event(enable_timing=True) + end = torch.cuda.Event(enable_timing=True) + start.record() + fn() + end.record() + torch.cuda.synchronize() + samples.append(start.elapsed_time(end) * 1000.0) + return statistics.median(samples) + + +def candidate_report(full_logits, targets, *, tp: int, group) -> dict[str, Any]: + """Run every candidate on this rank's shard of ``full_logits``.""" + rank = dist.get_rank(group) if tp > 1 else 0 + width = VOCAB // tp + local = full_logits[:, rank * width : (rank + 1) * width].contiguous() + reference = torch.log_softmax(full_logits.double(), dim=-1).gather(1, targets[:, None])[:, 0] + out, kept = {}, {} + for key, factory in CANDIDATES.items(): + try: + run = factory(tp, group) + with torch.no_grad(): + values = run(local, targets) + alone = run(local[:ALONE_ROWS].contiguous(), targets[:ALONE_ROWS]) + values = values.float() + kept[key] = values + out[key] = { + "selected_sha256": digest(values), + "batch_bitwise": bool(torch.equal(alone.float(), values[:ALONE_ROWS])), + "max_abs_vs_fp64": float((values.double() - reference).abs().max()), + "latency_us": _median_us(lambda r=run: r(local, targets)), + } + except Exception as exc: # noqa: BLE001 - recorded per candidate + out[key] = {"unavailable": f"{type(exc).__name__}: {str(exc)[:300]}"} + for key, values in kept.items(): + out[key]["max_abs_vs"] = { + other: float((values - peer).abs().max()) + for other, peer in kept.items() + if other != key + } + return out diff --git a/tools/validation/models/qwen3_next_logprob_prior_art.py b/tools/validation/models/qwen3_next_logprob_prior_art.py new file mode 100644 index 000000000..5122268bc --- /dev/null +++ b/tools/validation/models/qwen3_next_logprob_prior_art.py @@ -0,0 +1,140 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Run the vocabulary-parallel logprob prior art at one TP degree; merge launches. + + torchrun --nproc-per-node 4 tools/validation/models/qwen3_next_logprob_prior_art.py \\ + --checkpoint --output /tp4 + torchrun --nproc-per-node 1 tools/validation/models/qwen3_next_logprob_prior_art.py \\ + --checkpoint --output /tp1 + python tools/validation/models/qwen3_next_logprob_prior_art.py \\ + --merge /tp4 /tp4- /tp1 --out report.json + +The first ``--merge`` directory is the TP4 reference, the last the TP1 run; +any directories in between are TP4 runs under other NCCL settings. +""" + +import argparse +import json +import os +import sys +from contextlib import ExitStack +from datetime import timedelta +from pathlib import Path + +sys.path.insert(0, str(Path(__file__).resolve().parents[3])) + +import torch # noqa: E402 +import torch.distributed as dist # noqa: E402 + +from rl_engine.validation.models.qwen3_next_logprob_prior_art import ( # noqa: E402 + VOCAB, + candidate_report, + digest, +) +from tools.validation.models.qwen3_next_gate_common import ( # noqa: E402 + CheckpointWeights, + provenance, +) + +TOKENS = 1024 +NCCL_SETTINGS = ("NCCL_ALGO", "NCCL_PROTO", "NCCL_MAX_NCHANNELS") + + +def measure(checkpoint: Path, output: Path) -> None: + tp = int(os.environ["WORLD_SIZE"]) + rank = int(os.environ["LOCAL_RANK"]) + if tp not in (1, 4): + raise SystemExit("Run with one or four ranks") + torch.cuda.set_device(rank) + dist.init_process_group( + "nccl", timeout=timedelta(minutes=15), device_id=torch.device("cuda", rank) + ) + try: + from megatron.core import parallel_state + + parallel_state.initialize_model_parallel(tensor_model_parallel_size=tp) + group = parallel_state.get_tensor_model_parallel_group() + from rl_engine.models.qwen3_next.qwen3_next_forward import shared_linear + + generator = torch.Generator(device="cuda").manual_seed(4280) + with ExitStack() as stack: + head = CheckpointWeights(checkpoint, "", ("lm_head.weight",), stack)["lm_head.weight"] + hidden = torch.randn( + TOKENS, 2048, device="cuda", dtype=torch.bfloat16, generator=generator + ) + targets = torch.randint(0, VOCAB, (TOKENS,), device="cuda", generator=generator) + with torch.no_grad(): + full = shared_linear(hidden, head.cuda()) + report = { + "tp": tp, + "rank": rank, + "tokens": TOKENS, + "nccl_settings": {name: os.environ.get(name) for name in NCCL_SETTINGS}, + "full_logits_sha256": digest(full), + **provenance(), + "candidates": candidate_report(full, targets, tp=tp, group=group), + } + output.mkdir(parents=True, exist_ok=True) + (output / f"rank-{rank}.json").write_text(json.dumps(report, indent=2) + "\n") + dist.barrier() + finally: + dist.destroy_process_group() + + +def merge(runs: list[Path]) -> dict: + load = lambda run: json.loads((run / "rank-0.json").read_text()) # noqa: E731 + tp4, others, tp1 = load(runs[0]), [load(r) for r in runs[1:-1]], load(runs[-1]) + if tp4["tp"] != 4 or tp1["tp"] != 1 or any(o["tp"] != 4 for o in others): + raise SystemExit("Expected TP4 runs first and a TP1 run last") + if len({r["full_logits_sha256"] for r in (tp4, tp1, *others)}) != 1: + raise SystemExit("Launches did not see the same logits") + commits = {r["rl_kernel_commit"] for r in (tp4, tp1, *others)} + table = {} + for key, cand in tp4["candidates"].items(): + if "unavailable" in cand: + table[key] = cand + continue + table[key] = { + "tp4_equals_tp1": cand["selected_sha256"] + == tp1["candidates"].get(key, {}).get("selected_sha256"), + "nccl_setting_invariant": all( + o["candidates"][key].get("selected_sha256") == cand["selected_sha256"] + for o in others + ), + "batch_bitwise": cand["batch_bitwise"], + "max_abs_vs_fp64": cand["max_abs_vs_fp64"], + "max_abs_vs": cand["max_abs_vs"], + "latency_us_tp4": cand["latency_us"], + } + return { + "kind": "qwen3_next_logprob_prior_art", + "rfc": 428, + "rl_kernel_commit": next(iter(commits)) if len(commits) == 1 else sorted(commits), + "tracked_tree_dirty": any(r["tracked_tree_dirty"] for r in (tp4, tp1, *others)), + "environment": tp4["environment"], + "tokens": tp4["tokens"], + "nccl_settings": [r["nccl_settings"] for r in (tp4, *others)], + "candidates": table, + } + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--checkpoint", type=Path) + parser.add_argument("--output", type=Path) + parser.add_argument("--merge", nargs="+", type=Path) + parser.add_argument("--out", type=Path) + args = parser.parse_args() + if args.merge: + report = merge(args.merge) + args.out.write_text(json.dumps(report, indent=2) + "\n") + for key, row in report["candidates"].items(): + print(key, {k: v for k, v in row.items() if k != "max_abs_vs"}) + else: + measure(args.checkpoint, args.output) + + +if __name__ == "__main__": + main() From da566bbd851a1c0456f9ffe313fecd1ce8c06fee Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 22:42:37 +0000 Subject: [PATCH 26/68] test(qwen3-next): no raw torch.distributed reduction in Qwen3-Next forward paths NCCL changes its summation order with the algorithm, protocol and channel count. Scan the Qwen3-Next model, engine and VIME modules for raw reductions; all_gather (data movement) and an integer MAX of flags are allowed, and the TP reduction must go through RL-Kernel's fixed-order collective. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/ci.yml | 3 +- .../test_qwen3_next_tp_reductions.py | 93 +++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 tests/models/qwen3_next/test_qwen3_next_tp_reductions.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aac61850d..77b0693ec 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -101,7 +101,8 @@ jobs: tests/models/qwen3_next/test_qwen3_next_chain_report.py \ tests/models/qwen3_next/test_qwen3_next_run.py \ tests/models/qwen3_next/test_qwen3_next_evidence_redaction.py \ - tests/models/qwen3_next/test_qwen3_next_bi_cases.py + tests/models/qwen3_next/test_qwen3_next_bi_cases.py \ + tests/models/qwen3_next/test_qwen3_next_tp_reductions.py - name: Run Cross-Configuration Contract Tests (CPU-safe) run: | diff --git a/tests/models/qwen3_next/test_qwen3_next_tp_reductions.py b/tests/models/qwen3_next/test_qwen3_next_tp_reductions.py new file mode 100644 index 000000000..c43cbc480 --- /dev/null +++ b/tests/models/qwen3_next/test_qwen3_next_tp_reductions.py @@ -0,0 +1,93 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Every Qwen3-Next cross-rank sum goes through RL-Kernel's fixed-order collective. + +``torch.distributed`` reductions on NCCL change their summation order with the +NCCL algorithm, protocol and channel count, so a raw ``all_reduce`` in the +Qwen3-Next model or engine adapters would break replay/rollout logprob parity. +This scans those modules; data movement (``all_gather``) is allowed, and so is +an integer MAX of flags, whose result does not depend on order. +""" + +import ast +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[3] +MODULES = sorted( + { + *ROOT.glob("rl_engine/models/qwen3_next/*.py"), + *ROOT.glob("rl_engine/integrations/engines/rollout/vllm/qwen3_next_*.py"), + *ROOT.glob("rl_engine/integrations/engines/train/vllm/qwen3_next_*.py"), + *ROOT.glob("rl_engine/integrations/orchestrators/vime/providers/qwen3_next_*.py"), + *ROOT.glob("rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/*.py"), + ROOT / "rl_engine/integrations/common/qwen3_next_binary.py", + } +) +REDUCTIONS = { + "all_reduce", + "all_reduce_coalesced", + "reduce", + "reduce_scatter", + "reduce_scatter_tensor", + "_reduce_scatter_base", +} +# (module, function): why the reduction is exact regardless of order. +ALLOWED = { + ("qwen3_next_training.py", "_any_rank_nonzero"): "integer MAX of per-rank flags", +} + + +def _raw_reductions(path): + tree = ast.parse(path.read_text()) + found = [] + for function in ast.walk(tree): + if not isinstance(function, (ast.FunctionDef, ast.AsyncFunctionDef)): + continue + for node in ast.walk(function): + if ( + isinstance(node, ast.Call) + and isinstance(node.func, ast.Attribute) + and node.func.attr in REDUCTIONS + and isinstance(node.func.value, (ast.Name, ast.Attribute)) + and ast.unparse(node.func.value) in ("dist", "torch.distributed") + ): + found.append((path.name, function.name, node.lineno)) + return found + + +def test_scanned_modules_exist(): + assert len(MODULES) >= 20 + assert all(path.is_file() for path in MODULES) + + +def test_no_raw_nccl_reduction_in_qwen3_next_forward_paths(): + offenders = [ + f"{name}:{line} in {function}()" + for path in MODULES + for name, function, line in _raw_reductions(path) + if (name, function) not in ALLOWED + ] + assert not offenders, "raw torch.distributed reductions: " + ", ".join(offenders) + + +def test_allowed_reduction_is_an_integer_max(): + path = ROOT / "rl_engine/integrations/orchestrators/vime/providers/qwen3_next_training.py" + source = ast.get_source_segment( + path.read_text(), + next( + node + for node in ast.walk(ast.parse(path.read_text())) + if isinstance(node, ast.FunctionDef) and node.name == "_any_rank_nonzero" + ), + ) + assert "dtype=torch.int32" in source and "ReduceOp.MAX" in source + + +def test_tp_reduction_uses_the_fixed_order_collective(): + tree = ast.parse((ROOT / "rl_engine/models/qwen3_next/qwen3_next_tp.py").read_text()) + reduce = next( + n for n in ast.walk(tree) if isinstance(n, ast.FunctionDef) and n.name == "_reduce" + ) + calls = {ast.unparse(n.func) for n in ast.walk(reduce) if isinstance(n, ast.Call)} + assert "collective_for_group" in calls and "collective.all_reduce" in calls From f3a213d76274042e2ddeaa83e9c4381c0d3cf930 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 22:58:59 +0000 Subject: [PATCH 27/68] docs(qwen3-next): TP distributed logprob contract and evidence figure Document every TP4 cross-rank point of Qwen3-Next (embedding, row-parallel projections, MoE combine, column-parallel input gradients, the vocabulary- parallel selected logprob/LSE/entropy and the sampling logits): which ones move bytes with NCCL all-gather and which ones sum with RL-Kernel's fixed-order collective, with orders and dtypes, the reuse decision against vLLM, Megatron and VIME, and the known entropy TP-degree dependence. Add the figure script. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/README.md | 1 + .../qwen3-next-distributed-logprob.md | 106 +++++++++++++ .../plot_qwen3_next_distributed_logprob.py | 145 ++++++++++++++++++ 3 files changed, 252 insertions(+) create mode 100644 docs/operators/qwen3-next-distributed-logprob.md create mode 100644 tools/validation/models/plot_qwen3_next_distributed_logprob.py diff --git a/docs/operators/README.md b/docs/operators/README.md index be74cb6d0..047c61c57 100644 --- a/docs/operators/README.md +++ b/docs/operators/README.md @@ -35,4 +35,5 @@ Every operator page should include: - [Qwen3-Next RMSNorm (zero-centred)](qwen3-next-rms-norm.md) - [Qwen3-Next Gated RMSNorm](qwen3-next-rms-norm-gated.md) - [Qwen3-Next MoE route / combine](qwen3-next-moe-route-combine.md) +- [Qwen3-Next distributed logprob parity (TP)](qwen3-next-distributed-logprob.md) - [Operator Doc Template](../contributing/operator-doc-template.md) diff --git a/docs/operators/qwen3-next-distributed-logprob.md b/docs/operators/qwen3-next-distributed-logprob.md new file mode 100644 index 000000000..1e7d17ef7 --- /dev/null +++ b/docs/operators/qwen3-next-distributed-logprob.md @@ -0,0 +1,106 @@ +# Qwen3-Next distributed logprob parity (TP) + +## Summary + +RFC #428 row `distributed_logprob_parity` asks for a locked TP/EP reduction +order and an exactly equal selected-token logprob between VIME replay and vLLM +rollout. This page covers tensor parallelism (TP4, EP = 1, CP = 1) for +Qwen3-Next-80B-A3B-Instruct. Expert parallelism is a separate PR. + +Every place where TP ranks combine values either moves bytes (an NCCL +all-gather, which is exact whatever NCCL algorithm runs) or sums them with +RL-Kernel's fixed-order collective, whose order does not depend on NCCL at all. +No new collective or kernel is added; the row documents and proves the order. + +## Entry Point + +```python +from rl_engine.models.qwen3_next.qwen3_next_tp import _ReduceFromTP, _CopyToTP +from rl_engine.models.qwen3_next.qwen3_next_logprob import qwen_logprob_contract +from rl_engine.models.qwen3_next.qwen3_next_forward import shared_logprob + +contract = qwen_logprob_contract(local_logits, rank=rank, world_size=4) +selected, lse, entropy = shared_logprob(local_logits, targets, contract=contract, tp_group=tp_group) +``` + +vLLM rollout (`RLKernelQwen3NextForCausalLM.compute_selected_logprobs`) and VIME +replay (`calculate_log_probs_and_entropy`, installed by +`install_vime_logprob_provider`) both call `shared_logprob` on their own +37984-column LM-head shard. + +## Contract + +| Cross-rank point | Where | Operation | Order / dtype | +| --- | --- | --- | --- | +| Vocabulary-parallel embedding | `qwen3_next_embedding.shared_embedding` | each rank selects its rows, zeros elsewhere; sum | fixed-order collective, BF16; exactly one non-zero term per element | +| Embedding weight gradient | same, backward | stable sort by row, pairwise FP32 tree in token order | local, no collective | +| GDN `out_proj`, attention `o_proj`, MoE routed + shared output | `_ReduceFromTP` in `qwen3_next_tp_gdn`, `qwen3_next_tp_blocks`, vLLM adapters | row-parallel partial sums | fixed-order collective, BF16 | +| Input gradients of column-parallel projections | `_CopyToTP.backward` | sum of per-rank partial gradients | fixed-order collective | +| Selected-token logprob, LSE | `shared_logprob` (WS2 `CudaVocabParallelLogprobOp`) | per-rank FP32 max / sum-exp per tile, all-gather of tile statistics, merge in global tile order over a fixed `[tokens, 64]` grid | FP32; independent of the TP degree | +| Entropy | same | per-shard contributions, all-gather, sum in TP-rank order | FP32; depends on the TP degree (outside the selected-logprob contract) | +| Sampling logits | `compute_logits` (vLLM) | all-gather of the column shards | exact data movement | + +RL-Kernel's fixed-order collective (`rl_engine.distributed.algorithms.collectives`, +backend `cuda_ipc_fixed_tree`) stages every rank's input in a symmetric CUDA IPC +buffer and runs one reduction kernel that reads all peers and applies the same +balanced tree on every rank; NCCL is not involved in the arithmetic, so +`NCCL_ALGO`, `NCCL_PROTO` and the channel count cannot change the result. The +logprob all-gathers move per-tile statistics whose merge order is fixed by the +tile index, so changing the TP degree does not change selected logprobs or LSE +either. + +Fail-closed: `shared_logprob` rejects a vocabulary other than 151936, CP > 1, a +TP degree other than 1 or 4, non-finite logits and a missing contract; the +fixed-order collective checks on first use that every rank calls it with the +same shape and dtype, and supports only TP sizes 1, 2, 4 and 8. `tests/models/qwen3_next/test_qwen3_next_tp_reductions.py` fails +if a raw `torch.distributed` reduction appears in a Qwen3-Next model, engine or +VIME module (an integer MAX of per-rank flags is the one documented exception). + +## Reuse decision + +Measured with `tools/validation/models/qwen3_next_logprob_prior_art.py` on the +same official-LM-head logits (TP4 slices of one full-vocabulary product): + +| Implementation | TP4 == TP1 | NCCL-setting invariant | Batch invariant | Matches the others | +| --- | --- | --- | --- | --- | +| RL-Kernel WS2 (`shared_logprob`) | yes | yes | yes | — | +| vLLM sampler (all-gather + `log_softmax`) | yes | yes | yes | no | +| Megatron-core `vocab_parallel_cross_entropy` | no | see evidence | yes | no | +| VIME default `calculate_log_probs_and_entropy` | no | see evidence | yes | Megatron only | + +vLLM's sampler path is exact on the rollout side, but VIME's training side has +no counterpart that reproduces it: the Megatron/VIME paths reduce max and +sum-exp with NCCL all-reduce and differ from it. Using one provider for both +engines is the only measured way to equal bits, so both engines keep the WS2 +provider. Its cost is latency (see Results); a faster tile-stats kernel is +follow-up work and would keep the same merge order. + +## Results + +Evidence: [`docs/usage/evidence/qwen3-next-distributed-logprob-b200/`](../usage/evidence/qwen3-next-distributed-logprob-b200/README.md). + +- 13 NCCL settings (defaults; Ring and all-reduce Tree × LL / LL128 / Simple × + 1 channel / default channels), real Qwen3-Next weights, 1024 tokens, 4 × B200: + every point in the contract table has one digest per rank across all + settings, and the TP4 selected logprob and LSE equal TP1. The negative + control -- real attention `o_proj` partials summed with NCCL `all_reduce` -- + gives 5 different results over the same settings. +- The full-model chain (#543) shows the end-to-end consequence: selected + logprobs of vLLM rollout and VIME recompute are bitwise equal for 16 cases at + four weight versions. + +## Tests + +| File | Device | Covers | +| --- | --- | --- | +| `tests/models/qwen3_next/test_qwen3_next_tp_reductions.py` | CPU | no raw NCCL reduction in Qwen3-Next forward paths; the TP reduction uses the fixed-order collective | +| `tools/validation/models/qwen3_next_logprob_parity.py` + `..._compare.py` | 4 × CUDA | digests at every cross-rank point under each NCCL setting; TP4 == TP1 | +| `tools/validation/models/qwen3_next_logprob_prior_art.py` | 4 × CUDA | prior art above | +| `tests/distributed/tp/test_vocab_parallel_logp.py` (existing) | CUDA | WS2 TP = n == TP = 1 on synthetic shapes | + +## Known Limitations + +- TP4 with EP = 1 and CP = 1 only; expert parallelism is the second PR of this row. +- Entropy merges per-shard contributions in rank order and therefore changes + with the TP degree; rollout and replay run the same TP degree, so they still + agree, but entropy is not TP-degree invariant. diff --git a/tools/validation/models/plot_qwen3_next_distributed_logprob.py b/tools/validation/models/plot_qwen3_next_distributed_logprob.py new file mode 100644 index 000000000..1aad48766 --- /dev/null +++ b/tools/validation/models/plot_qwen3_next_distributed_logprob.py @@ -0,0 +1,145 @@ +#!/usr/bin/env python +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Render the Qwen3-Next distributed logprob parity evidence as a PNG. + + python tools/validation/models/plot_qwen3_next_distributed_logprob.py \\ + docs/usage/evidence/qwen3-next-distributed-logprob-b200 + +reads ``parity/runs//rank-0.json`` and ``prior-art/report.json`` and +writes ``figure.png``. Top: for every TP4 cross-rank point, whether its digest +under each NCCL setting equals the digest under NCCL defaults (the raw-NCCL +negative control is the last row). Bottom: the selected-token logprob prior art +-- invariance checks, the largest difference between implementations, latency. +""" + +from __future__ import annotations + +import argparse +import json +from pathlib import Path + +import matplotlib + +matplotlib.use("Agg") +import matplotlib.pyplot as plt # noqa: E402 +import numpy as np # noqa: E402 +from matplotlib.colors import ListedColormap # noqa: E402 + +INK = "#2b2b29" +MUTED = "#6f6e69" +SURFACE = "#fcfcfb" +RL_KERNEL = "#2a78d6" +EXISTING = ("#eb6834", "#b8501f", "#f29a62") +STATUS = ListedColormap(["#d64545", "#1baf7a"]) +CONTROL = "o_proj_partials.nccl_all_reduce" +LABELS = { + "rl_kernel_ws2": "RL-Kernel WS2\n(both engines here)", + "vllm_gather": "vLLM sampler\n(gather + log_softmax)", + "megatron_ce": "Megatron-core\nvocab_parallel_cross_entropy", + "vime_default": "VIME default\nlogprob", +} +CHECKS = ( + ("tp4_equals_tp1", "TP4 == TP1"), + ("nccl_setting_invariant", "NCCL setting\ninvariant"), + ("batch_bitwise", "batch\ninvariant"), +) + + +def _setting_label(name): + return name.replace("-cdefault", "").replace("-c1", "\n1 ch").replace("-", "\n") + + +def plot(evidence: Path, out: Path) -> None: + runs = sorted( + (evidence / "parity" / "runs").iterdir(), key=lambda p: (p.name != "default", p.name) + ) + digests = {run.name: json.loads((run / "rank-0.json").read_text())["points"] for run in runs} + points = [p for p, v in digests["default"].items() if isinstance(v, str) and p != CONTROL] + points = sorted(points) + [CONTROL] + grid = np.array( + [[digests[run.name][p] == digests["default"][p] for run in runs] for p in points] + ) + prior = json.loads((evidence / "prior-art" / "report.json").read_text())["candidates"] + keys = [k for k in LABELS if k in prior and "unavailable" not in prior[k]] + + fig = plt.figure(figsize=(17, 11), facecolor=SURFACE) + spec = fig.add_gridspec(2, 3, height_ratios=[1.35, 1], width_ratios=[1.1, 1, 1]) + top = fig.add_subplot(spec[0, :]) + top.imshow(grid, cmap=STATUS, vmin=0, vmax=1, aspect="auto") + top.set_yticks( + range(len(points)), + [ + p if p != CONTROL else "NEGATIVE CONTROL: o_proj partials, NCCL all_reduce" + for p in points + ], + fontsize=8, + color=INK, + ) + top.set_xticks(range(len(runs)), [_setting_label(r.name) for r in runs], fontsize=7, color=INK) + top.axhline(len(points) - 1.5, color=SURFACE, linewidth=4) + top.set_title( + "TP4 cross-rank points on real weights: digest under each NCCL setting == NCCL defaults " + "(green) or not (red)", + fontsize=10, + color=INK, + ) + + checks = fig.add_subplot(spec[1, 0]) + table = np.array([[bool(prior[k][c]) for c, _ in CHECKS] for k in keys]) + checks.imshow(table, cmap=STATUS, vmin=0, vmax=1, aspect="auto") + checks.set_yticks(range(len(keys)), [LABELS[k] for k in keys], fontsize=8, color=INK) + checks.set_xticks(range(len(CHECKS)), [label for _, label in CHECKS], fontsize=8, color=INK) + checks.set_title("Selected-token logprob: invariance (bitwise)", fontsize=10, color=INK) + + pairs = fig.add_subplot(spec[1, 1]) + delta = np.array( + [[0.0 if a == b else prior[a]["max_abs_vs"].get(b, np.nan) for b in keys] for a in keys] + ) + pairs.imshow(delta > 0, cmap=ListedColormap(["#1baf7a", "#f29a62"]), vmin=0, vmax=1) + for i in range(len(keys)): + for j in range(len(keys)): + text = "0" if delta[i, j] == 0 else f"{delta[i, j]:.1e}" + pairs.text(j, i, text, ha="center", va="center", fontsize=8, color=INK) + short = [LABELS[k].split("\n")[0] for k in keys] + pairs.set_xticks(range(len(keys)), short, fontsize=7, rotation=20, color=INK) + pairs.set_yticks(range(len(keys)), short, fontsize=7, color=INK) + pairs.set_title("Max |Δ logprob| between implementations", fontsize=10, color=INK) + + latency = fig.add_subplot(spec[1, 2]) + values = [prior[k]["latency_us_tp4"] / 1000.0 for k in keys] + colors = [RL_KERNEL if k == "rl_kernel_ws2" else EXISTING[i % 3] for i, k in enumerate(keys)] + latency.barh(range(len(keys)), values, color=colors) + latency.set_yticks(range(len(keys)), short, fontsize=8, color=INK) + latency.invert_yaxis() + for i, v in enumerate(values): + latency.text(v, i, f" {v:.2f} ms", va="center", fontsize=8, color=MUTED) + latency.set_xlabel("TP4 latency, 1024 tokens (ms, median)", fontsize=9, color=MUTED) + latency.set_title("Latency", fontsize=10, color=INK) + + for axis in fig.axes: + for spine in axis.spines.values(): + spine.set_visible(False) + env = json.loads((evidence / "prior-art" / "report.json").read_text())["environment"] + fig.suptitle( + f"Qwen3-Next TP4 distributed logprob parity - {env['gpu']}, NCCL {env['nccl']}, " + f"torch {env['torch']}", + fontsize=12, + color=INK, + ) + fig.tight_layout() + fig.savefig(out, dpi=150) + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("evidence", type=Path) + args = parser.parse_args() + out = args.evidence / "figure.png" + plot(args.evidence, out) + print(f"wrote {out}") + + +if __name__ == "__main__": + main() From 682c233a4c1f440b02bffdc13a2bfb601e365084 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 11 Oct 2026 23:01:31 +0000 Subject: [PATCH 28/68] test(qwen3-next): publisher for the distributed logprob parity evidence Copies the per-setting rank reports, the comparison report and the prior-art reports into the repository with the chain publisher's redaction rules, refuses anything a forbidden pattern still matches, and writes redaction.json and SHA256SUMS. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../qwen3_next_publish_parity_evidence.py | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 tools/validation/models/qwen3_next_publish_parity_evidence.py diff --git a/tools/validation/models/qwen3_next_publish_parity_evidence.py b/tools/validation/models/qwen3_next_publish_parity_evidence.py new file mode 100644 index 000000000..e2968c182 --- /dev/null +++ b/tools/validation/models/qwen3_next_publish_parity_evidence.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Copy distributed-logprob parity outputs into the repository without site details. + + python tools/validation/models/qwen3_next_publish_parity_evidence.py \\ + --parity --parity-report report.json \\ + --prior-art \\ + --out docs/usage/evidence/qwen3-next-distributed-logprob-b200 + +Writes ``parity/runs//rank-*.json``, ``parity/report.json``, +``prior-art/report.json`` and ``prior-art/runs//rank-0.json``, redacted +with the chain publisher's rules, plus ``redaction.json`` and ``SHA256SUMS``. +Refuses to publish anything a forbidden pattern still matches. +""" + +import argparse +import hashlib +import json +from pathlib import Path + +from qwen3_next_publish_evidence import leftovers, redact + + +def publish(parity: Path, parity_report: Path, prior_art: Path, out: Path) -> dict: + out.mkdir(parents=True, exist_ok=False) + sources = {Path("parity/report.json"): parity_report} + for run in sorted(p for p in parity.iterdir() if p.is_dir()): + for rank in sorted(run.glob("rank-*.json")): + sources[Path("parity/runs") / run.name / rank.name] = rank + sources[Path("prior-art/report.json")] = prior_art / "report.json" + for run in sorted(p for p in prior_art.iterdir() if p.is_dir()): + sources[Path("prior-art/runs") / run.name / "rank-0.json"] = run / "rank-0.json" + counts: dict[str, int] = {} + for relative, source in sources.items(): + text = redact(source.read_text(), [], counts) + found = leftovers(text) + if found: + raise SystemExit(f"{relative} still matches {found}") + target = out / relative + target.parent.mkdir(parents=True, exist_ok=True) + target.write_text(text) + (out / "redaction.json").write_text(json.dumps({"placeholders": counts}, indent=2) + "\n") + sums = [ + f"{hashlib.sha256(path.read_bytes()).hexdigest()} {path.relative_to(out)}" + for path in sorted(out.rglob("*")) + if path.is_file() + ] + (out / "SHA256SUMS").write_text("\n".join(sums) + "\n") + return counts + + +def main() -> None: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--parity", type=Path, required=True) + parser.add_argument("--parity-report", type=Path, required=True) + parser.add_argument("--prior-art", type=Path, required=True) + parser.add_argument("--out", type=Path, required=True) + args = parser.parse_args() + counts = publish(args.parity, args.parity_report, args.prior_art, args.out) + print(f"published to {args.out}; placeholders {counts}") + + +if __name__ == "__main__": + main() From 16171f5ed8a11fad7c2693d9b4ce759ae10f1259 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 12 Oct 2026 00:09:24 +0000 Subject: [PATCH 29/68] docs(qwen3-next): record the prior-art NCCL-setting results and latency Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/qwen3-next-distributed-logprob.md | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/operators/qwen3-next-distributed-logprob.md b/docs/operators/qwen3-next-distributed-logprob.md index 1e7d17ef7..5ba1a079a 100644 --- a/docs/operators/qwen3-next-distributed-logprob.md +++ b/docs/operators/qwen3-next-distributed-logprob.md @@ -59,14 +59,15 @@ VIME module (an integer MAX of per-rank flags is the one documented exception). ## Reuse decision Measured with `tools/validation/models/qwen3_next_logprob_prior_art.py` on the -same official-LM-head logits (TP4 slices of one full-vocabulary product): +same official-LM-head logits (TP4 slices of one full-vocabulary product), at TP1 +and at TP4 under the same 13 NCCL settings as the parity run: | Implementation | TP4 == TP1 | NCCL-setting invariant | Batch invariant | Matches the others | | --- | --- | --- | --- | --- | | RL-Kernel WS2 (`shared_logprob`) | yes | yes | yes | — | | vLLM sampler (all-gather + `log_softmax`) | yes | yes | yes | no | -| Megatron-core `vocab_parallel_cross_entropy` | no | see evidence | yes | no | -| VIME default `calculate_log_probs_and_entropy` | no | see evidence | yes | Megatron only | +| Megatron-core `vocab_parallel_cross_entropy` | no | no (2 results over 13 settings) | yes | no | +| VIME default `calculate_log_probs_and_entropy` | no | no (2 results over 13 settings) | yes | Megatron only | vLLM's sampler path is exact on the rollout side, but VIME's training side has no counterpart that reproduces it: the Megatron/VIME paths reduce max and @@ -75,6 +76,10 @@ engines is the only measured way to equal bits, so both engines keep the WS2 provider. Its cost is latency (see Results); a faster tile-stats kernel is follow-up work and would keep the same merge order. +All four agree with an FP64 log-softmax to within 8.1e-7 and the largest +difference between any two of them is 9.5e-7: the defaults are close, but not +equal, and only equal bits remove the replay/rollout difference the RFC targets. + ## Results Evidence: [`docs/usage/evidence/qwen3-next-distributed-logprob-b200/`](../usage/evidence/qwen3-next-distributed-logprob-b200/README.md). @@ -85,6 +90,8 @@ Evidence: [`docs/usage/evidence/qwen3-next-distributed-logprob-b200/`](../usage/ settings, and the TP4 selected logprob and LSE equal TP1. The negative control -- real attention `o_proj` partials summed with NCCL `all_reduce` -- gives 5 different results over the same settings. +- Latency at TP4 for 1024 tokens (median): RL-Kernel WS2 2.55 ms, vLLM + all-gather + log_softmax 1.51 ms, Megatron-core and VIME 0.57 ms. - The full-model chain (#543) shows the end-to-end consequence: selected logprobs of vLLM rollout and VIME recompute are bitwise equal for 16 cases at four weight versions. From 1106e461e30d724c700eeffa5df7550fc1389556 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 12 Oct 2026 10:56:42 +0000 Subject: [PATCH 30/68] refactor(qwen3-next): focus TP review on validation and externalize evidence Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- ci/scripts/run_qwen3_next_chain_gate.sh | 2 +- ci/scripts/run_qwen3_next_operator_gates.sh | 2 +- ci/scripts/run_ws1_gtest.sh | 2 +- .../qwen3-next-distributed-logprob.md | 31 +--- .../operators/qwen3-next-moe-route-combine.md | 6 +- docs/operators/qwen3-next-rms-norm-gated.md | 2 +- docs/operators/qwen3-next-rms-norm.md | 2 +- docs/usage/qwen3-next-batch-invariance.md | 9 +- docs/usage/qwen3-next-chain.md | 14 +- .../qwen3-next-batch-invariance-b200.md | 47 +++++ .../qwen3-next-distributed-logprob-b200.md | 47 +++++ rl_engine/config/workload.py | 4 +- .../rollout/vllm/qwen3_next_native_rollout.py | 2 +- .../qwen3_next/native_recompute.py | 2 +- .../experiments/qwen3_next/native_training.py | 11 +- .../vime/experiments/qwen3_next/profile.py | 4 +- .../vime/providers/qwen3_next_training.py | 4 +- .../vime/providers/qwen3_next_vime_runtime.py | 2 +- .../validation/models/pytest_case_manifest.py | 2 +- .../validation/models/qwen3_next/__init__.py | 2 + .../qwen3_next_attention_prior_art.py | 0 .../{ => qwen3_next}/qwen3_next_bi_cases.py | 4 +- .../qwen3_next_bi_manifest.json | 2 +- .../{ => qwen3_next}/qwen3_next_binary.py | 0 .../{ => qwen3_next}/qwen3_next_cases.py | 0 .../qwen3_next_chain_report.py | 3 +- .../qwen3_next_export_schema.json | 0 .../models/{ => qwen3_next}/qwen3_next_l2.py | 2 +- .../qwen3_next_layer_cases.json | 10 +- .../qwen3_next_moe_prior_art.py | 0 .../qwen3_next_norm_manifest.json | 0 .../qwen3_next_operator_cases.json | 2 +- .../{ => qwen3_next}/qwen3_next_rl_fixture.py | 0 .../{ => qwen3_next}/qwen3_next_workload.py | 0 .../models/qwen3_next_logprob_prior_art.py | 171 ------------------ .../qwen3_next/check_gdn_recurrent_golden.py | 2 +- .../check_qwen3_next_bi_requests.py | 2 +- .../check_qwen3_next_native_rollout.py | 2 +- .../qwen3_next/test_qwen3_next_bi_cases.py | 2 +- .../test_qwen3_next_binary_evidence.py | 2 +- .../test_qwen3_next_case_manifest.py | 6 +- .../test_qwen3_next_chain_report.py | 2 +- .../qwen3_next/test_qwen3_next_checkpoint.py | 2 +- .../test_qwen3_next_environment_identity.py | 2 +- .../test_qwen3_next_evidence_redaction.py | 3 +- .../qwen3_next/test_qwen3_next_failure.py | 2 +- ..._qwen3_next_installed_provider_identity.py | 2 +- tests/models/qwen3_next/test_qwen3_next_l2.py | 43 +++-- .../qwen3_next/test_qwen3_next_layers.py | 4 +- .../test_qwen3_next_native_recompute.py | 4 +- .../test_qwen3_next_operator_gates.py | 4 +- .../qwen3_next/test_qwen3_next_profile.py | 2 +- .../qwen3_next/test_qwen3_next_rl_fixture.py | 2 +- .../models/qwen3_next/test_qwen3_next_run.py | 8 +- .../qwen3_next/test_qwen3_next_storage.py | 2 +- .../test_qwen3_next_training_checkpoint.py | 4 +- .../test_qwen3_next_training_closeout.py | 4 +- .../test_qwen3_next_training_export.py | 2 +- .../qwen3_next/test_qwen3_next_workload.py | 6 +- .../plot_qwen3_next_distributed_logprob.py | 145 --------------- .../validation/models/qwen3_next/__init__.py | 2 + .../plot_qwen3_next_attention_prior_art.py | 2 +- .../plot_qwen3_next_batch_invariance.py | 4 +- .../{ => qwen3_next}/plot_qwen3_next_chain.py | 2 +- .../plot_qwen3_next_moe_prior_art.py | 2 +- .../plot_qwen3_next_norm_evidence.py | 5 +- .../qwen3_next_adapter_check.py | 0 .../qwen3_next_attention_prior_art.py | 9 +- .../qwen3_next_batch_invariance.py | 17 +- .../qwen3_next_chain_report.py | 12 +- .../{ => qwen3_next}/qwen3_next_checkpoint.py | 0 .../{ => qwen3_next}/qwen3_next_contract.py | 16 +- .../qwen3_next_gate_common.py | 2 +- .../models/{ => qwen3_next}/qwen3_next_l2.py | 14 +- .../qwen3_next_layer_contract.py | 6 +- .../{ => qwen3_next}/qwen3_next_layers.py | 8 +- .../qwen3_next_logprob_parity.py | 11 +- .../qwen3_next_logprob_parity_compare.py | 2 +- .../qwen3_next_moe_prior_art.py | 9 +- .../qwen3_next_norm_evidence.py | 6 +- .../qwen3_next_operator_cases.py | 14 +- .../qwen3_next_operator_gates.py | 12 +- .../qwen3_next_optimizer_checkpoint.py | 0 .../{ => qwen3_next}/qwen3_next_preflight.py | 8 +- .../qwen3_next_publish_evidence.py | 3 +- .../qwen3_next_publish_parity_evidence.py | 20 +- .../qwen3_next_rollout_smoke.py | 0 .../models/{ => qwen3_next}/qwen3_next_run.py | 4 +- .../{ => qwen3_next}/qwen3_next_runtime.py | 46 ++++- .../qwen3_next_tp_attention_check.py | 7 +- .../qwen3_next_tp_gdn_check.py | 9 +- .../qwen3_next_tp_head_check.py | 0 .../qwen3_next_tp_moe_check.py | 6 +- .../{ => qwen3_next}/qwen3_next_tp_probe.py | 0 .../{ => qwen3_next}/qwen3_next_training.py | 8 +- .../qwen3_next_training_checkpoint.py | 2 +- .../qwen3_next_training_export.py | 8 +- .../models/qwen3_next_logprob_prior_art.py | 140 -------------- .../operators/check_forward_invariance.py | 4 +- .../operators/check_gradient_invariance.py | 4 +- 100 files changed, 389 insertions(+), 685 deletions(-) create mode 100644 reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md create mode 100644 reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md create mode 100644 rl_engine/validation/models/qwen3_next/__init__.py rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_attention_prior_art.py (100%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_bi_cases.py (98%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_bi_manifest.json (99%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_binary.py (100%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_cases.py (100%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_chain_report.py (99%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_export_schema.json (100%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_l2.py (99%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_layer_cases.json (87%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_moe_prior_art.py (100%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_norm_manifest.json (100%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_operator_cases.json (99%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_rl_fixture.py (100%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_workload.py (100%) delete mode 100644 rl_engine/validation/models/qwen3_next_logprob_prior_art.py delete mode 100644 tools/validation/models/plot_qwen3_next_distributed_logprob.py create mode 100644 tools/validation/models/qwen3_next/__init__.py rename tools/validation/models/{ => qwen3_next}/plot_qwen3_next_attention_prior_art.py (98%) rename tools/validation/models/{ => qwen3_next}/plot_qwen3_next_batch_invariance.py (97%) rename tools/validation/models/{ => qwen3_next}/plot_qwen3_next_chain.py (98%) rename tools/validation/models/{ => qwen3_next}/plot_qwen3_next_moe_prior_art.py (98%) rename tools/validation/models/{ => qwen3_next}/plot_qwen3_next_norm_evidence.py (95%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_adapter_check.py (100%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_attention_prior_art.py (92%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_batch_invariance.py (94%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_chain_report.py (92%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_checkpoint.py (100%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_contract.py (96%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_gate_common.py (99%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_l2.py (98%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_layer_contract.py (94%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_layers.py (97%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_logprob_parity.py (96%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_logprob_parity_compare.py (97%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_moe_prior_art.py (92%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_norm_evidence.py (98%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_operator_cases.py (87%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_operator_gates.py (94%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_optimizer_checkpoint.py (100%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_preflight.py (97%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_publish_evidence.py (98%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_publish_parity_evidence.py (65%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_rollout_smoke.py (100%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_run.py (99%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_runtime.py (97%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_tp_attention_check.py (95%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_tp_gdn_check.py (95%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_tp_head_check.py (100%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_tp_moe_check.py (95%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_tp_probe.py (100%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_training.py (99%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_training_checkpoint.py (99%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_training_export.py (98%) delete mode 100644 tools/validation/models/qwen3_next_logprob_prior_art.py diff --git a/ci/scripts/run_qwen3_next_chain_gate.sh b/ci/scripts/run_qwen3_next_chain_gate.sh index 8c042ea64..8407c755e 100755 --- a/ci/scripts/run_qwen3_next_chain_gate.sh +++ b/ci/scripts/run_qwen3_next_chain_gate.sh @@ -13,7 +13,7 @@ if [ "$#" -ge 1 ]; then import dataclasses, json, sys from rl_engine.validation.models.chain_gate import ChainGateReport -from rl_engine.validation.models.qwen3_next_chain_report import COMPATIBLE_SCHEMA, SCHEMA_VERSION +from rl_engine.validation.models.qwen3_next.qwen3_next_chain_report import COMPATIBLE_SCHEMA, SCHEMA_VERSION report = json.load(open(sys.argv[1])) missing = {f.name for f in dataclasses.fields(ChainGateReport)} - set(report) diff --git a/ci/scripts/run_qwen3_next_operator_gates.sh b/ci/scripts/run_qwen3_next_operator_gates.sh index ceba42dd5..fc8f48f8d 100644 --- a/ci/scripts/run_qwen3_next_operator_gates.sh +++ b/ci/scripts/run_qwen3_next_operator_gates.sh @@ -6,5 +6,5 @@ cd "$ROOT" PY="${PY:-python3}" OUT="${QWEN3_NEXT_EVIDENCE_DIR:?Set QWEN3_NEXT_EVIDENCE_DIR to an artifact directory}" export PYTHONPATH="$ROOT${PYTHONPATH:+:$PYTHONPATH}" -exec "$PY" tools/validation/models/qwen3_next_operator_gates.py --out "$OUT" \ +exec "$PY" tools/validation/models/qwen3_next/qwen3_next_operator_gates.py --out "$OUT" \ --suite "${QWEN3_NEXT_OPERATOR_SUITE:-all}" diff --git a/ci/scripts/run_ws1_gtest.sh b/ci/scripts/run_ws1_gtest.sh index 1c5a2be53..23dbaabf4 100755 --- a/ci/scripts/run_ws1_gtest.sh +++ b/ci/scripts/run_ws1_gtest.sh @@ -78,7 +78,7 @@ print("[ws1-gtest] C8 gate passed") PY echo "[ws1-gtest] Qwen3-Next C3/C4 norms (operator scope)" -QWEN3_NEXT_NORM_MANIFEST=rl_engine/validation/models/qwen3_next_norm_manifest.json +QWEN3_NEXT_NORM_MANIFEST=rl_engine/validation/models/qwen3_next/qwen3_next_norm_manifest.json "$PY" tools/validation/operators/check_forward_invariance.py --manifest "$QWEN3_NEXT_NORM_MANIFEST" \ --op qwen3_next_rms_norm --candidate cuda --backend-profile cuda_bf16 --hidden 2048 --head-dim 128 "$PY" tools/validation/operators/check_gradient_invariance.py --manifest "$QWEN3_NEXT_NORM_MANIFEST" \ diff --git a/docs/operators/qwen3-next-distributed-logprob.md b/docs/operators/qwen3-next-distributed-logprob.md index 5ba1a079a..0c7d9ebfe 100644 --- a/docs/operators/qwen3-next-distributed-logprob.md +++ b/docs/operators/qwen3-next-distributed-logprob.md @@ -56,33 +56,9 @@ same shape and dtype, and supports only TP sizes 1, 2, 4 and 8. `tests/models/qw if a raw `torch.distributed` reduction appears in a Qwen3-Next model, engine or VIME module (an integer MAX of per-rank flags is the one documented exception). -## Reuse decision - -Measured with `tools/validation/models/qwen3_next_logprob_prior_art.py` on the -same official-LM-head logits (TP4 slices of one full-vocabulary product), at TP1 -and at TP4 under the same 13 NCCL settings as the parity run: - -| Implementation | TP4 == TP1 | NCCL-setting invariant | Batch invariant | Matches the others | -| --- | --- | --- | --- | --- | -| RL-Kernel WS2 (`shared_logprob`) | yes | yes | yes | — | -| vLLM sampler (all-gather + `log_softmax`) | yes | yes | yes | no | -| Megatron-core `vocab_parallel_cross_entropy` | no | no (2 results over 13 settings) | yes | no | -| VIME default `calculate_log_probs_and_entropy` | no | no (2 results over 13 settings) | yes | Megatron only | - -vLLM's sampler path is exact on the rollout side, but VIME's training side has -no counterpart that reproduces it: the Megatron/VIME paths reduce max and -sum-exp with NCCL all-reduce and differ from it. Using one provider for both -engines is the only measured way to equal bits, so both engines keep the WS2 -provider. Its cost is latency (see Results); a faster tile-stats kernel is -follow-up work and would keep the same merge order. - -All four agree with an FP64 log-softmax to within 8.1e-7 and the largest -difference between any two of them is 9.5e-7: the defaults are close, but not -equal, and only equal bits remove the replay/rollout difference the RFC targets. - ## Results -Evidence: [`docs/usage/evidence/qwen3-next-distributed-logprob-b200/`](../usage/evidence/qwen3-next-distributed-logprob-b200/README.md). +Evidence: [`reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md`](https://github.com/fusheng-ji/RL-Kernel/blob/feat/ws1-qwen3-next-distributed-logprob-tp/reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md). - 13 NCCL settings (defaults; Ring and all-reduce Tree × LL / LL128 / Simple × 1 channel / default channels), real Qwen3-Next weights, 1024 tokens, 4 × B200: @@ -90,8 +66,6 @@ Evidence: [`docs/usage/evidence/qwen3-next-distributed-logprob-b200/`](../usage/ settings, and the TP4 selected logprob and LSE equal TP1. The negative control -- real attention `o_proj` partials summed with NCCL `all_reduce` -- gives 5 different results over the same settings. -- Latency at TP4 for 1024 tokens (median): RL-Kernel WS2 2.55 ms, vLLM - all-gather + log_softmax 1.51 ms, Megatron-core and VIME 0.57 ms. - The full-model chain (#543) shows the end-to-end consequence: selected logprobs of vLLM rollout and VIME recompute are bitwise equal for 16 cases at four weight versions. @@ -101,8 +75,7 @@ Evidence: [`docs/usage/evidence/qwen3-next-distributed-logprob-b200/`](../usage/ | File | Device | Covers | | --- | --- | --- | | `tests/models/qwen3_next/test_qwen3_next_tp_reductions.py` | CPU | no raw NCCL reduction in Qwen3-Next forward paths; the TP reduction uses the fixed-order collective | -| `tools/validation/models/qwen3_next_logprob_parity.py` + `..._compare.py` | 4 × CUDA | digests at every cross-rank point under each NCCL setting; TP4 == TP1 | -| `tools/validation/models/qwen3_next_logprob_prior_art.py` | 4 × CUDA | prior art above | +| `tools/validation/models/qwen3_next/qwen3_next_logprob_parity.py` + `..._compare.py` | 4 × CUDA | digests at every cross-rank point under each NCCL setting; TP4 == TP1 | | `tests/distributed/tp/test_vocab_parallel_logp.py` (existing) | CUDA | WS2 TP = n == TP = 1 on synthetic shapes | ## Known Limitations diff --git a/docs/operators/qwen3-next-moe-route-combine.md b/docs/operators/qwen3-next-moe-route-combine.md index 07804b8bc..0236bfd06 100644 --- a/docs/operators/qwen3-next-moe-route-combine.md +++ b/docs/operators/qwen3-next-moe-route-combine.md @@ -84,8 +84,8 @@ with no change in bits. ## Results -Measured on B200 with `tools/validation/models/qwen3_next_moe_prior_art.py` and the TP4 gate -`tools/validation/models/qwen3_next_tp_moe_check.py`; the reports, the figure and the exact +Measured on B200 with `tools/validation/models/qwen3_next/qwen3_next_moe_prior_art.py` and the TP4 gate +`tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py`; the reports, the figure and the exact commands are in [`docs/usage/evidence/qwen3-next-moe-route-b200/`](../usage/evidence/qwen3-next-moe-route-b200/README.md). @@ -134,7 +134,7 @@ shared expert + all-reduce) takes 1.9-2.4 ms at 8-1024 tokens. | `tests/models/qwen3_next/test_qwen3_next_tp_blocks.py` | CPU | HF shard/assemble round trips, replica drift rejection, TP4 parameter ownership | | `tests/validation/common/test_tensor_identity.py` | CPU | raw-bit identity (signed zero, NaN/Inf, dtype) | | `tests/models/qwen3_next/check_qwen3_next_forward.py` | CUDA + vLLM | GEMM batch/chunk/reorder, route ties, combine order, MoE VJP vs FP64, route and output batch invariance, grouped == per-expert bitwise, fail-closed TD path | -| `tools/validation/models/qwen3_next_tp_moe_check.py` | 4 x CUDA | real layer-0 weights: HF round trip, replicated routes, chunk/reorder, training forward, gradients | +| `tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py` | 4 x CUDA | real layer-0 weights: HF round trip, replicated routes, chunk/reorder, training forward, gradients | The `check_` file imports vLLM, so it runs in the `Qwen3-Next-provider-GPU` workflow rather than the default collection. diff --git a/docs/operators/qwen3-next-rms-norm-gated.md b/docs/operators/qwen3-next-rms-norm-gated.md index 217fa45ed..f8cbaf3ec 100644 --- a/docs/operators/qwen3-next-rms-norm-gated.md +++ b/docs/operators/qwen3-next-rms-norm-gated.md @@ -124,7 +124,7 @@ python tools/validation/operators/check_operator.py --op rms_norm_gated --candid ![gated RMSNorm vs existing implementations on B200](../usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) [`report.json`](../usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by -`tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `822b085`, on an otherwise idle +`tools/validation/models/qwen3_next/qwen3_next_norm_evidence.py` from a clean tree at `822b085`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0). Head dim 128, BF16; the FP64 golden uses vLLM's convention. The same report re-measures the zero-centred op ([figure](../usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). diff --git a/docs/operators/qwen3-next-rms-norm.md b/docs/operators/qwen3-next-rms-norm.md index 2fbd8baa5..522fc1bbd 100644 --- a/docs/operators/qwen3-next-rms-norm.md +++ b/docs/operators/qwen3-next-rms-norm.md @@ -103,7 +103,7 @@ element and no extra memory traffic. ![zero-centred RMSNorm vs existing implementations on B200](../usage/evidence/qwen3-next-rms-norm-b200/figure.png) [`report.json`](../usage/evidence/qwen3-next-rms-norm-b200/report.json) was written by -`tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `3d0bae7`, on an otherwise idle +`tools/validation/models/qwen3_next/qwen3_next_norm_evidence.py` from a clean tree at `3d0bae7`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0, FlashInfer 0.6.18). Hidden 2048, BF16. diff --git a/docs/usage/qwen3-next-batch-invariance.md b/docs/usage/qwen3-next-batch-invariance.md index c3fea5dbb..56087bb1a 100644 --- a/docs/usage/qwen3-next-batch-invariance.md +++ b/docs/usage/qwen3-next-batch-invariance.md @@ -8,10 +8,10 @@ passed `l2` stage of the [full-model chain](qwen3-next-chain.md), under the same certified provider profile. ```bash -python tools/validation/models/qwen3_next_run.py --stage bi --previous /<...-l2-...> \ +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage bi --previous /<...-l2-...> \ --runs-root --checkpoint --cuda-home \ --python /bin/python [--bi-suite quick] -python tools/validation/models/qwen3_next_chain_report.py --last <...-train-...> --bi <...-bi-...> --out chain-report.json +python tools/validation/models/qwen3_next/qwen3_next_chain_report.py --last <...-train-...> --bi <...-bi-...> --out chain-report.json ``` ## Cells @@ -19,7 +19,7 @@ python tools/validation/models/qwen3_next_chain_report.py --last <...-train-...> Every cell has one *target* (16 generated tokens) and compares it bitwise - response tokens, BF16 logits, FP32 logprobs, the embedding and every layer, sub-node and recurrent/KV-state digest - with a reference. The cells are -generated by `rl_engine/validation/models/qwen3_next_bi_cases.py` and pinned by +generated by `rl_engine/validation/models/qwen3_next/qwen3_next_bi_cases.py` and pinned by `qwen3_next_bi_manifest.json`. | Family | Cells | The target runs | Compared with | @@ -53,3 +53,6 @@ layer and sub-node, and its layer family (`embedding`, `norm`, `gdn`, `full_attention`, `moe` or `lm_head_logprob`). The chain report's `batch_invariance` section summarises it per family; a failing cell fails the chain report. + +The [B200 result summary](https://github.com/fusheng-ji/RL-Kernel/blob/feat/ws1-qwen3-next-distributed-logprob-tp/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md) +links the raw evidence ZIP, tested commit and reproduction commands. diff --git a/docs/usage/qwen3-next-chain.md b/docs/usage/qwen3-next-chain.md index c5d5801aa..cb9d732a8 100644 --- a/docs/usage/qwen3-next-chain.md +++ b/docs/usage/qwen3-next-chain.md @@ -27,13 +27,13 @@ layer family. Inside an allocation with four GPUs, from a clean checkout of the commit to measure: ```bash -python tools/validation/models/qwen3_next_run.py --stage all \ +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage all \ --runs-root --checkpoint --cuda-home \ --python /bin/python --until l2 -python tools/validation/models/qwen3_next_run.py --stage all --from train1 --until train2 \ +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage all --from train1 --until train2 \ --previous /<...-l2-...> \ --runs-root --checkpoint --cuda-home --python /bin/python -python tools/validation/models/qwen3_next_run.py --stage train --previous /<...-train2-...> \ +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage train --previous /<...-train2-...> \ --runs-root --checkpoint --cuda-home --python /bin/python ``` @@ -45,7 +45,7 @@ provider or topology identity. | Stage | What it checks | Typical time | | --- | --- | --- | -| `preflight` | environment and versions, the mandatory operator cases (`rl_engine/validation/models/qwen3_next_operator_cases.json`) and norm C3/C4 gates, multi-GPU device guards, a four-rank TP probe, checkpoint hashes, a vLLM rollout smoke | 15 min | +| `preflight` | environment and versions, the mandatory operator cases (`rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json`) and norm C3/C4 gates, multi-GPU device guards, a four-rank TP probe, checkpoint hashes, a vLLM rollout smoke | 15 min | | `layers` | the real-weight TP4 gates (GDN, attention, MoE, GDN engine adapters, embedding/LM head) and the native scheduler on 16 cases; certifies the shared provider profile | 25 min | | `l2` | 16 cases (prompt 8/64/256/1024 x batch 1/4 x full/chunked prefill, 16 response tokens): rollout vs replay, raw bits, plus every layer and sub-node digest | 30 min | | `train1`, `train2` | one optimizer update each, checkpoint, HF export, L2 on the exported weights | 3-4 h each | @@ -54,9 +54,9 @@ provider or topology identity. ## Reports ```bash -python tools/validation/models/qwen3_next_chain_report.py --last /<...-train-...> --out chain-report.json +python tools/validation/models/qwen3_next/qwen3_next_chain_report.py --last /<...-train-...> --out chain-report.json bash ci/scripts/run_qwen3_next_chain_gate.sh chain-report.json -python tools/validation/models/qwen3_next_publish_evidence.py --last /<...-train-...> \ +python tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py --last /<...-train-...> \ --chain-report chain-report.json --out docs/usage/evidence/qwen3-next-chain- ``` @@ -74,7 +74,7 @@ both engines (`rl_engine/models/qwen3_next/qwen3_next_subnode.py`): the normed mixer input, the mixer output, the normed MLP input, the ten selected expert ids and the MoE output, plus the embedding once per model. -`tools/validation/models/qwen3_next_publish_evidence.py` copies the stage records and the +`tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py` copies the stage records and the report, replaces site paths, host and user names with placeholders, refuses to publish if any survive, and writes `SHA256SUMS`. diff --git a/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md b/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md new file mode 100644 index 000000000..07dfd8f32 --- /dev/null +++ b/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md @@ -0,0 +1,47 @@ +# Qwen3-Next batch invariance (B200) + +[Download raw results (ZIP)](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-pr549-evidence/qwen3-next-batch-invariance-b200.zip). + +ZIP SHA-256: `a6f7245c83d4e2fbbc949ba915abaf05a9b71d5fbeaaf241f846eab5addeb878`. + +Measured at commit `759755e` (`measured-commit.txt`) from clean clones, one +stage after another on one node with four B200 GPUs, in one environment +(`environment_sha256` `118384371a88…` in every `run.json`): + +```bash +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage all --until l2 \ + --runs-root --checkpoint --cuda-home --python /bin/python +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage bi --bi-suite full --previous /<...-l2-...> \ + --runs-root --checkpoint --cuda-home --python /bin/python +python tools/validation/models/qwen3_next/qwen3_next_chain_report.py --last <...-l2-...> --bi <...-bi-...> --out chain-report.json +python tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py --last <...-l2-...> --also <...-bi-...> \ + --chain-report chain-report.json --out artifacts/qwen3-next-batch-invariance-b200 +``` + +Commands use the current layout; at `759755e` the runners lived in `scripts/`. +The archive preserves the original evidence bytes and `SHA256SUMS`; the publisher +at `3025497` only added site redactions. Extract the ZIP before inspecting its JSON. + +| Stage | Result | +| --- | --- | +| `preflight` | passed: environment, mandatory operator cases and norm gates, device guards, TP4 probe, checkpoint hashes, rollout smoke | +| `layers` | passed: GDN, attention, MoE, GDN-adapter and embedding/head TP4 gates on real weights; native scheduler on 16 cases | +| `l2` | passed: 16/16 cases bitwise rollout vs replay, every layer, sub-node and state digest equal | +| `bi` (full) | passed: **97/97 cells bitwise** | + +| `bi` family | Cells | Passed | Compared with | +| --- | --- | --- | --- | +| concurrency (1-64 requests, target first or last) | 13 | 13 | the target alone, same chunk plan | +| interference (content, lengths, arrival, generation length) | 10 | 10 | the target alone, same chunk plan | +| chunk (prompts 63-1024, chunk 64/128, first chunk 1/63/65) | 66 | 66 | the replay engine's recompute | +| packing (replay THD batch with 1-7 neighbours) | 8 | 8 | the replay of the target alone | + +Each comparison is raw bits of the target's 16 response tokens, its BF16 +logits and FP32 logprobs over the full vocabulary, the embedding rows and, for +all 48 layers, the five sub-node outputs, the layer output and the +convolution/recurrent or KV state. `bi/bi_matrix.json` lists every cell; +`chain-report.json` is the chain-gate report with its `batch_invariance` +section. + +Site paths, host names and the scheduler are replaced by placeholders +(``, ``, ``, ``, `/name`, `/file`). diff --git a/reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md b/reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md new file mode 100644 index 000000000..004e4a6d6 --- /dev/null +++ b/reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md @@ -0,0 +1,47 @@ +# Qwen3-Next distributed logprob parity, TP4 (B200) + +[Download raw results (ZIP)](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-pr549-evidence/qwen3-next-distributed-logprob-b200.zip). + +ZIP SHA-256: `a48bf29d19e40d22178185de81dcfacdd9138ca03f5dc347d23eed4a4fbd2cd9`. + +One node with four B200 GPUs, torch 2.13.0+cu130, vLLM 0.30.0, NCCL 2.29.7, the +official `Qwen/Qwen3-Next-80B-A3B-Instruct` revision +`9c7f2fbe84465e40164a94cc16cd30b6999b0cc7`, 1024 tokens. Every run used a clean +clone with `rl_engine._C` built in place. + +| Directory | Measured at | Produced by | +| --- | --- | --- | +| `parity/runs//rank-{0..3}.json` | `04db726` | ` torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_logprob_parity.py --checkpoint --output parity/runs/` | +| `parity/report.json` | `04db726` | `python tools/validation/models/qwen3_next/qwen3_next_logprob_parity_compare.py --runs parity/runs/* --out parity/report.json` | + +The 13 NCCL settings are the defaults plus `NCCL_ALGO` Ring and all-reduce Tree +(`allreduce:tree`; NCCL 2.29 has no Tree all-gather) × `NCCL_PROTO` LL, LL128, +Simple × one channel (`NCCL_MAX_NCHANNELS=1`) or the default channel count. +The archive preserves the published JSON bytes and their `SHA256SUMS`, including +the historical prior-art results used by the separate benchmark PR. The TP +parity measurements were collected at `04db726`; prior-art data at `33495ad`. + +## Results + +- Every TP4 cross-rank point -- embedding forward and weight gradient; GDN + layer 0, full-attention layer 3 and MoE layer 0 forward and input gradient; + selected logprob, LSE, entropy and logits gradient; the gathered sampling + logits -- has one digest per rank across all 13 settings + (`parity/report.json`: `deterministic_points_invariant: true`). +- TP4 selected logprob and LSE equal the TP1 computation on the same logits + (`tp4_equals_tp1: true`); entropy does not (`entropy_tp4_equals_tp1: false`), + as documented. +- Negative control: the same real attention `o_proj` partials summed with NCCL + `all_reduce` give 5 different results across the 13 settings + (`nccl_all_reduce_varies: true`). + +Commands above use the current layout; at the measured commits, runners lived +directly under `tools/validation/models/`. To package a new run without adding raw JSON to the repository: + +```bash +python tools/validation/models/qwen3_next/qwen3_next_publish_parity_evidence.py \ + --parity parity/runs --parity-report parity/report.json \ + --out artifacts/qwen3-next-distributed-logprob-b200 +cd artifacts +zip -r qwen3-next-distributed-logprob-b200.zip qwen3-next-distributed-logprob-b200 +``` diff --git a/rl_engine/config/workload.py b/rl_engine/config/workload.py index 79f93208a..fbaf81f05 100644 --- a/rl_engine/config/workload.py +++ b/rl_engine/config/workload.py @@ -265,7 +265,9 @@ def load_manifest(path: str | Path | None = None) -> WS1Manifest: if not isinstance(raw, dict): raise WorkloadError("manifest root must be a JSON object") if raw.get("scope") == "qwen3_next_norm_operators": - from rl_engine.validation.models.qwen3_next_workload import validate_norm_manifest + from rl_engine.validation.models.qwen3_next.qwen3_next_workload import ( + validate_norm_manifest, + ) validate_norm_manifest(raw) else: diff --git a/rl_engine/integrations/engines/rollout/vllm/qwen3_next_native_rollout.py b/rl_engine/integrations/engines/rollout/vllm/qwen3_next_native_rollout.py index b827182a5..5fb04558f 100644 --- a/rl_engine/integrations/engines/rollout/vllm/qwen3_next_native_rollout.py +++ b/rl_engine/integrations/engines/rollout/vllm/qwen3_next_native_rollout.py @@ -24,7 +24,7 @@ ) from rl_engine.models.qwen3_next import qwen3_next_subnode as subnode from rl_engine.validation.common.tensor_identity import assert_tensor_bitwise_equal as exact -from rl_engine.validation.models.qwen3_next_l2 import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( embedding_digest, json_digest, layer_digest, diff --git a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py index 2bc4ede52..544788939 100644 --- a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py +++ b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py @@ -17,7 +17,7 @@ from rl_engine.models.qwen3_next import qwen3_next_subnode as subnode from rl_engine.validation.common.tensor_identity import assert_tensor_bitwise_equal as exact -from rl_engine.validation.models.qwen3_next_l2 import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( embedding_digest, layer_digest, load_tensor_artifact, diff --git a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_training.py b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_training.py index c3e2ae3fc..4ea41e570 100644 --- a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_training.py +++ b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_training.py @@ -16,8 +16,11 @@ import torch.distributed as dist import torch.multiprocessing as mp -from rl_engine.validation.models.qwen3_next_l2 import load_tensor_artifact, validate_fixture -from rl_engine.validation.models.qwen3_next_rl_fixture import PolicyFixture +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( + load_tensor_artifact, + validate_fixture, +) +from rl_engine.validation.models.qwen3_next.qwen3_next_rl_fixture import PolicyFixture def policy_fixture(path): @@ -168,7 +171,9 @@ def _rank_main(rank, request, staging): from rl_engine.integrations.orchestrators.vime.providers.qwen3_next_vime_runtime import ( create_actor, ) - from rl_engine.validation.models.qwen3_next_binary import verify_imported_provider + from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( + verify_imported_provider, + ) fixture = policy_fixture(request["fixture"]) manifest = request["manifest"] diff --git a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/profile.py b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/profile.py index 8e2e99fe0..87abbe803 100644 --- a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/profile.py +++ b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/profile.py @@ -24,7 +24,7 @@ def profile_is_certified(): def validate_certified_weights(identity, certified): """The base certificate is not a certificate for newly updated weight bytes.""" - from rl_engine.validation.models.qwen3_next_l2 import validate_weight_identity + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_weight_identity validate_weight_identity(identity) if identity.get("weight_version", "baseline") == "baseline": @@ -71,7 +71,7 @@ def activate_for_identity(identity): return "qualification" if mode != "1": raise ValueError("A certified shared profile must execute strict BI=1") - sys.path.insert(0, str(ROOT / "tools/validation/models")) + sys.path.insert(0, str(ROOT / "tools/validation/models/qwen3_next")) from qwen3_next_contract import validate_result from qwen3_next_l2 import model_identity from qwen3_next_runtime import file_digest, identity_digest, verify_environment diff --git a/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_training.py b/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_training.py index 8b60fc415..91292b9b8 100644 --- a/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_training.py +++ b/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_training.py @@ -16,7 +16,9 @@ import numpy as np import torch -from rl_engine.validation.models.qwen3_next_rl_fixture import require_policy_configuration +from rl_engine.validation.models.qwen3_next.qwen3_next_rl_fixture import ( + require_policy_configuration, +) def tensor_digest(value, *, chunk_bytes=64 * 1024 * 1024): diff --git a/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py b/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py index a7481150a..8888dc15b 100644 --- a/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py +++ b/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py @@ -8,7 +8,7 @@ import sys from pathlib import Path -from rl_engine.validation.models.qwen3_next_rl_fixture import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_rl_fixture import ( OPTIMIZER_SETTINGS, POLICY_SETTINGS, require_policy_configuration, diff --git a/rl_engine/validation/models/pytest_case_manifest.py b/rl_engine/validation/models/pytest_case_manifest.py index a25e41769..7cdb38a5c 100644 --- a/rl_engine/validation/models/pytest_case_manifest.py +++ b/rl_engine/validation/models/pytest_case_manifest.py @@ -7,7 +7,7 @@ import time from pathlib import Path -from rl_engine.validation.models.qwen3_next_cases import execution_verdict, load_cases +from rl_engine.validation.models.qwen3_next.qwen3_next_cases import execution_verdict, load_cases def pytest_addoption(parser): diff --git a/rl_engine/validation/models/qwen3_next/__init__.py b/rl_engine/validation/models/qwen3_next/__init__.py new file mode 100644 index 000000000..86cf4c9da --- /dev/null +++ b/rl_engine/validation/models/qwen3_next/__init__.py @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors diff --git a/rl_engine/validation/models/qwen3_next_attention_prior_art.py b/rl_engine/validation/models/qwen3_next/qwen3_next_attention_prior_art.py similarity index 100% rename from rl_engine/validation/models/qwen3_next_attention_prior_art.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_attention_prior_art.py diff --git a/rl_engine/validation/models/qwen3_next_bi_cases.py b/rl_engine/validation/models/qwen3_next/qwen3_next_bi_cases.py similarity index 98% rename from rl_engine/validation/models/qwen3_next_bi_cases.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_bi_cases.py index 1aaea196b..8df724891 100644 --- a/rl_engine/validation/models/qwen3_next_bi_cases.py +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_bi_cases.py @@ -34,7 +34,7 @@ from dataclasses import asdict, dataclass from pathlib import Path -from rl_engine.validation.models.qwen3_next_l2 import json_digest +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import json_digest MANIFEST = Path(__file__).with_name("qwen3_next_bi_manifest.json") MAX_MODEL_LEN = 1040 @@ -243,7 +243,7 @@ def manifest_payload(): return { "schema_version": 1, "description": "Qwen3-Next batch-invariance cells; regenerate with " - "python -m rl_engine.validation.models.qwen3_next_bi_cases --write", + "python -m rl_engine.validation.models.qwen3_next.qwen3_next_bi_cases --write", "cells": entries, "cells_sha256": hashlib.sha256( json.dumps(entries, sort_keys=True, separators=(",", ":")).encode() diff --git a/rl_engine/validation/models/qwen3_next_bi_manifest.json b/rl_engine/validation/models/qwen3_next/qwen3_next_bi_manifest.json similarity index 99% rename from rl_engine/validation/models/qwen3_next_bi_manifest.json rename to rl_engine/validation/models/qwen3_next/qwen3_next_bi_manifest.json index 63a4c3911..07bf6792b 100644 --- a/rl_engine/validation/models/qwen3_next_bi_manifest.json +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_bi_manifest.json @@ -1,6 +1,6 @@ { "schema_version": 1, - "description": "Qwen3-Next batch-invariance cells; regenerate with python -m rl_engine.validation.models.qwen3_next_bi_cases --write", + "description": "Qwen3-Next batch-invariance cells; regenerate with python -m rl_engine.validation.models.qwen3_next.qwen3_next_bi_cases --write", "cells": [ { "cell_id": "concurrency-n01-first", diff --git a/rl_engine/validation/models/qwen3_next_binary.py b/rl_engine/validation/models/qwen3_next/qwen3_next_binary.py similarity index 100% rename from rl_engine/validation/models/qwen3_next_binary.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_binary.py diff --git a/rl_engine/validation/models/qwen3_next_cases.py b/rl_engine/validation/models/qwen3_next/qwen3_next_cases.py similarity index 100% rename from rl_engine/validation/models/qwen3_next_cases.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_cases.py diff --git a/rl_engine/validation/models/qwen3_next_chain_report.py b/rl_engine/validation/models/qwen3_next/qwen3_next_chain_report.py similarity index 99% rename from rl_engine/validation/models/qwen3_next_chain_report.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_chain_report.py index e7e6d560d..2e69a11e7 100644 --- a/rl_engine/validation/models/qwen3_next_chain_report.py +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_chain_report.py @@ -3,7 +3,8 @@ """Qwen3-Next full-model chain as a WS1 chain-gate report. -The Qwen3-Next chain runs as stages (``tools/validation/models/qwen3_next_run.py``): L2 on the +The Qwen3-Next chain runs as stages +(``tools/validation/models/qwen3_next/qwen3_next_run.py``): L2 on the pinned weights, then L2 again after each of two optimizer updates and after a reload. Each L2 stage writes a 16-case raw-bit matrix comparing rollout (native vLLM prefill + decode) with replay (VIME recompute). This module turns diff --git a/rl_engine/validation/models/qwen3_next_export_schema.json b/rl_engine/validation/models/qwen3_next/qwen3_next_export_schema.json similarity index 100% rename from rl_engine/validation/models/qwen3_next_export_schema.json rename to rl_engine/validation/models/qwen3_next/qwen3_next_export_schema.json diff --git a/rl_engine/validation/models/qwen3_next_l2.py b/rl_engine/validation/models/qwen3_next/qwen3_next_l2.py similarity index 99% rename from rl_engine/validation/models/qwen3_next_l2.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_l2.py index 462200e28..5a30a6f84 100644 --- a/rl_engine/validation/models/qwen3_next_l2.py +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_l2.py @@ -909,7 +909,7 @@ def _validate_report_artifacts(report, *, roles, expected_identity, artifact_roo identity = report["identity"] if expected_identity is not None and identity != expected_identity: raise ValueError("Model matrix source/environment/provider identity mismatch") - from rl_engine.validation.models.qwen3_next_binary import validate_binary_record + from rl_engine.validation.models.qwen3_next.qwen3_next_binary import validate_binary_record binary = validate_binary_record( report.get("provider_binary"), diff --git a/rl_engine/validation/models/qwen3_next_layer_cases.json b/rl_engine/validation/models/qwen3_next/qwen3_next_layer_cases.json similarity index 87% rename from rl_engine/validation/models/qwen3_next_layer_cases.json rename to rl_engine/validation/models/qwen3_next/qwen3_next_layer_cases.json index 4e1af4c67..3914f4eeb 100644 --- a/rl_engine/validation/models/qwen3_next_layer_cases.json +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_layer_cases.json @@ -2,7 +2,7 @@ "schema_version": 1, "gates": { "gdn": { - "script": "tools/validation/models/qwen3_next_tp_gdn_check.py", + "script": "tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py", "output_option": "--output", "scope": "tp4_gdn_layer0", "groups": { @@ -18,7 +18,7 @@ } }, "attention": { - "script": "tools/validation/models/qwen3_next_tp_attention_check.py", + "script": "tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py", "output_option": "--output", "scope": "tp4_full_attention_layer3", "groups": { @@ -37,7 +37,7 @@ } }, "moe": { - "script": "tools/validation/models/qwen3_next_tp_moe_check.py", + "script": "tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py", "output_option": "--output", "scope": "tp4_moe_block_layer0", "groups": { @@ -58,7 +58,7 @@ } }, "adapters": { - "script": "tools/validation/models/qwen3_next_adapter_check.py", + "script": "tools/validation/models/qwen3_next/qwen3_next_adapter_check.py", "output_option": "--out", "scope": "gdn_engine_adapter_component", "groups": { @@ -81,7 +81,7 @@ } }, "heads": { - "script": "tools/validation/models/qwen3_next_tp_head_check.py", + "script": "tools/validation/models/qwen3_next/qwen3_next_tp_head_check.py", "output_option": "--output", "scope": "tp4_embedding_lm_head_only", "groups": { diff --git a/rl_engine/validation/models/qwen3_next_moe_prior_art.py b/rl_engine/validation/models/qwen3_next/qwen3_next_moe_prior_art.py similarity index 100% rename from rl_engine/validation/models/qwen3_next_moe_prior_art.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_moe_prior_art.py diff --git a/rl_engine/validation/models/qwen3_next_norm_manifest.json b/rl_engine/validation/models/qwen3_next/qwen3_next_norm_manifest.json similarity index 100% rename from rl_engine/validation/models/qwen3_next_norm_manifest.json rename to rl_engine/validation/models/qwen3_next/qwen3_next_norm_manifest.json diff --git a/rl_engine/validation/models/qwen3_next_operator_cases.json b/rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json similarity index 99% rename from rl_engine/validation/models/qwen3_next_operator_cases.json rename to rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json index 101ad30f1..1d8582f8e 100644 --- a/rl_engine/validation/models/qwen3_next_operator_cases.json +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json @@ -1,6 +1,6 @@ { "schema_version": 1, - "description": "Mandatory norm/core contracts; alternative-provider approximate comparisons are diagnostics only. Generated by tools/validation/models/qwen3_next_operator_cases.py; case changes require review before a frozen run.", + "description": "Mandatory norm/core contracts; alternative-provider approximate comparisons are diagnostics only. Generated by tools/validation/models/qwen3_next/qwen3_next_operator_cases.py; case changes require review before a frozen run.", "suites": { "norm": [ "tests/models/qwen3_next/check_qwen3_next_norm_providers.py::test_custom_op_has_distinct_native_and_cuda_paths", diff --git a/rl_engine/validation/models/qwen3_next_rl_fixture.py b/rl_engine/validation/models/qwen3_next/qwen3_next_rl_fixture.py similarity index 100% rename from rl_engine/validation/models/qwen3_next_rl_fixture.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_rl_fixture.py diff --git a/rl_engine/validation/models/qwen3_next_workload.py b/rl_engine/validation/models/qwen3_next/qwen3_next_workload.py similarity index 100% rename from rl_engine/validation/models/qwen3_next_workload.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_workload.py diff --git a/rl_engine/validation/models/qwen3_next_logprob_prior_art.py b/rl_engine/validation/models/qwen3_next_logprob_prior_art.py deleted file mode 100644 index f6eb71a06..000000000 --- a/rl_engine/validation/models/qwen3_next_logprob_prior_art.py +++ /dev/null @@ -1,171 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 -# Copyright (c) 2026 RL-Kernel Contributors - -"""Prior art for the Qwen3-Next vocabulary-parallel selected-token logprob. - -Every candidate receives the same logits: the full-vocabulary product of the -official LM head with seeded hidden states, computed identically on every rank -and sliced into 37984-column shards for TP4. Only the cross-rank reduction -therefore differs between candidates and between TP degrees. - -Candidates: - -* ``rl_kernel_ws2`` -- RL-Kernel's provider (per-tile max/sum-exp, all-gather - transport, merge in global tile order); what both engines run here. -* ``vllm_gather`` -- vLLM's sampler path: all-gather the logits, then - ``log_softmax(dtype=float32)`` over the full vocabulary. -* ``megatron_ce`` -- Megatron-core ``vocab_parallel_cross_entropy`` (NCCL - all-reduce of max, then of target logit and sum-exp); logprob = -loss. -* ``vime_default`` -- VIME's own ``calculate_log_probs_and_entropy``. - -For each candidate a launch records a digest of the selected logprobs, whether -the first rows computed alone equal the same rows inside the full batch, the -maximum error against an FP64 log-softmax and a median latency. -""" - -from __future__ import annotations - -import hashlib -import statistics -from typing import Any, Callable - -import torch -import torch.distributed as dist - -VOCAB, SHARD = 151936, 37984 -ALONE_ROWS = 16 - - -def digest(tensor: torch.Tensor) -> str: - return hashlib.sha256(tensor.detach().contiguous().view(torch.uint8).cpu().numpy()).hexdigest() - - -def _rl_kernel(tp: int, group): - from rl_engine.contracts.operators.logprob import ( - LogprobContract, - LogprobDType, - LogprobRole, - MaskSpec, - ReductionSpec, - ShardingSpec, - ) - from rl_engine.models.qwen3_next.qwen3_next_forward import shared_logprob - - rank = dist.get_rank(group) if tp > 1 else 0 - - def run(local, targets): - n = local.shape[0] - contract = LogprobContract( - role=LogprobRole.TRAIN, - dtype=LogprobDType.BF16, - mask=MaskSpec(num_tokens=n, active_mask=(True,) * n), - sharding=ShardingSpec( - tp_rank=rank, - tp_world_size=tp, - vocab_shard_bounds=tuple( - (i * (VOCAB // tp), (i + 1) * (VOCAB // tp)) for i in range(tp) - ), - real_vocab_size=VOCAB, - padded_vocab_size=VOCAB, - cp_rank=0, - cp_world_size=1, - ), - reduction=ReductionSpec(), - ) - return shared_logprob( - local, - targets, - contract=contract, - tp_group=group if tp > 1 else None, - with_entropy=False, - )[0] - - return run - - -def _vllm_gather(tp: int, group): - from vllm.v1.sample.sampler import Sampler - - def run(local, targets): - full = local - if tp > 1: - peers = [torch.empty_like(local) for _ in range(tp)] - dist.all_gather(peers, local.contiguous(), group=group) - full = torch.cat(peers, dim=1) - logprobs = Sampler.compute_logprobs(full) - return logprobs.gather(1, targets[:, None]).squeeze(1) - - return run - - -def _megatron(tp: int, group): - from megatron.core.tensor_parallel import vocab_parallel_cross_entropy - - def run(local, targets): - loss = vocab_parallel_cross_entropy(local.float()[:, None, :], targets[:, None]) - return -loss.squeeze(1) - - return run - - -def _vime(tp: int, group): - from vime.utils.ppo_utils import calculate_log_probs_and_entropy - - def run(local, targets): - logprob, _ = calculate_log_probs_and_entropy(local.float(), targets, group) - return logprob.reshape(-1) - - return run - - -CANDIDATES: dict[str, Callable[[int, Any], Callable]] = { - "rl_kernel_ws2": _rl_kernel, - "vllm_gather": _vllm_gather, - "megatron_ce": _megatron, - "vime_default": _vime, -} - - -def _median_us(fn, iters=20): - samples = [] - for _ in range(iters): - start = torch.cuda.Event(enable_timing=True) - end = torch.cuda.Event(enable_timing=True) - start.record() - fn() - end.record() - torch.cuda.synchronize() - samples.append(start.elapsed_time(end) * 1000.0) - return statistics.median(samples) - - -def candidate_report(full_logits, targets, *, tp: int, group) -> dict[str, Any]: - """Run every candidate on this rank's shard of ``full_logits``.""" - rank = dist.get_rank(group) if tp > 1 else 0 - width = VOCAB // tp - local = full_logits[:, rank * width : (rank + 1) * width].contiguous() - reference = torch.log_softmax(full_logits.double(), dim=-1).gather(1, targets[:, None])[:, 0] - out, kept = {}, {} - for key, factory in CANDIDATES.items(): - try: - run = factory(tp, group) - with torch.no_grad(): - values = run(local, targets) - alone = run(local[:ALONE_ROWS].contiguous(), targets[:ALONE_ROWS]) - values = values.float() - kept[key] = values - out[key] = { - "selected_sha256": digest(values), - "batch_bitwise": bool(torch.equal(alone.float(), values[:ALONE_ROWS])), - "max_abs_vs_fp64": float((values.double() - reference).abs().max()), - "latency_us": _median_us(lambda r=run: r(local, targets)), - } - except Exception as exc: # noqa: BLE001 - recorded per candidate - out[key] = {"unavailable": f"{type(exc).__name__}: {str(exc)[:300]}"} - for key, values in kept.items(): - out[key]["max_abs_vs"] = { - other: float((values - peer).abs().max()) - for other, peer in kept.items() - if other != key - } - return out diff --git a/tests/models/qwen3_next/check_gdn_recurrent_golden.py b/tests/models/qwen3_next/check_gdn_recurrent_golden.py index 043df421a..a83d40ddf 100644 --- a/tests/models/qwen3_next/check_gdn_recurrent_golden.py +++ b/tests/models/qwen3_next/check_gdn_recurrent_golden.py @@ -37,7 +37,7 @@ pytestmark = pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA is required") from rl_engine.reference.linear_attn import GatedDeltaRuleRecurrentStepOp # noqa: E402 -from rl_engine.validation.models.qwen3_next_workload import FINGERPRINT # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_workload import FINGERPRINT # noqa: E402 # Qwen3-Next-80B-A3B-Instruct dims, from the pinned checkpoint fingerprint. _H = FINGERPRINT["linear_num_key_heads"] diff --git a/tests/models/qwen3_next/check_qwen3_next_bi_requests.py b/tests/models/qwen3_next/check_qwen3_next_bi_requests.py index 567bbf242..952e96a9e 100644 --- a/tests/models/qwen3_next/check_qwen3_next_bi_requests.py +++ b/tests/models/qwen3_next/check_qwen3_next_bi_requests.py @@ -6,7 +6,7 @@ import pytest from rl_engine.integrations.engines.rollout.vllm import qwen3_next_native_rollout as rollout -from rl_engine.validation.models import qwen3_next_bi_cases as bi +from rl_engine.validation.models.qwen3_next import qwen3_next_bi_cases as bi TEXTS = [list(range(1000 + 50 * i, 1000 + 50 * i + 17 + i)) for i in range(4)] IDENTITY = { diff --git a/tests/models/qwen3_next/check_qwen3_next_native_rollout.py b/tests/models/qwen3_next/check_qwen3_next_native_rollout.py index 67e7cd08d..10f6f2c3f 100644 --- a/tests/models/qwen3_next/check_qwen3_next_native_rollout.py +++ b/tests/models/qwen3_next/check_qwen3_next_native_rollout.py @@ -9,7 +9,7 @@ import torch from rl_engine.integrations.engines.rollout.vllm import qwen3_next_native_rollout as rollout -from rl_engine.validation.models.qwen3_next_l2 import L2Case, json_digest +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import L2Case, json_digest def request(batch=4, length=8, chunked=False, frozen=False): diff --git a/tests/models/qwen3_next/test_qwen3_next_bi_cases.py b/tests/models/qwen3_next/test_qwen3_next_bi_cases.py index a2b7096e6..93ee28e13 100644 --- a/tests/models/qwen3_next/test_qwen3_next_bi_cases.py +++ b/tests/models/qwen3_next/test_qwen3_next_bi_cases.py @@ -10,7 +10,7 @@ from rl_engine.integrations.orchestrators.vime.experiments.qwen3_next.native_recompute import ( packed_layout, ) -from rl_engine.validation.models import qwen3_next_bi_cases as bi +from rl_engine.validation.models.qwen3_next import qwen3_next_bi_cases as bi TEXTS = [list(range(1000 + 50 * i, 1000 + 50 * i + 17 + i)) for i in range(4)] IDENTITY = { diff --git a/tests/models/qwen3_next/test_qwen3_next_binary_evidence.py b/tests/models/qwen3_next/test_qwen3_next_binary_evidence.py index 19cf4309c..c73e523fe 100644 --- a/tests/models/qwen3_next/test_qwen3_next_binary_evidence.py +++ b/tests/models/qwen3_next/test_qwen3_next_binary_evidence.py @@ -9,7 +9,7 @@ import pytest -from rl_engine.validation.models.qwen3_next_binary import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( archive_provider_binary, validate_binary_record, verify_imported_provider, diff --git a/tests/models/qwen3_next/test_qwen3_next_case_manifest.py b/tests/models/qwen3_next/test_qwen3_next_case_manifest.py index 7d78a236f..9e9e077c1 100644 --- a/tests/models/qwen3_next/test_qwen3_next_case_manifest.py +++ b/tests/models/qwen3_next/test_qwen3_next_case_manifest.py @@ -10,7 +10,11 @@ import pytest -from rl_engine.validation.models.qwen3_next_cases import case_digest, execution_verdict, load_cases +from rl_engine.validation.models.qwen3_next.qwen3_next_cases import ( + case_digest, + execution_verdict, + load_cases, +) CASE = "tests/example.py::test_required" SUCCESS = {"setup": "passed", "call": "passed", "teardown": "passed"} diff --git a/tests/models/qwen3_next/test_qwen3_next_chain_report.py b/tests/models/qwen3_next/test_qwen3_next_chain_report.py index 2101e8210..b92c74eae 100644 --- a/tests/models/qwen3_next/test_qwen3_next_chain_report.py +++ b/tests/models/qwen3_next/test_qwen3_next_chain_report.py @@ -9,8 +9,8 @@ import torch from rl_engine.contracts.numerical import load_contract -from rl_engine.validation.models import qwen3_next_chain_report as chain from rl_engine.validation.models.chain_gate import ChainGateReport +from rl_engine.validation.models.qwen3_next import qwen3_next_chain_report as chain CASES = ("p0008-b1-full", "p0008-b1-chunked") diff --git a/tests/models/qwen3_next/test_qwen3_next_checkpoint.py b/tests/models/qwen3_next/test_qwen3_next_checkpoint.py index 95672f6a1..682347a2c 100644 --- a/tests/models/qwen3_next/test_qwen3_next_checkpoint.py +++ b/tests/models/qwen3_next/test_qwen3_next_checkpoint.py @@ -9,7 +9,7 @@ import pytest -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) from qwen3_next_checkpoint import ( # noqa: E402 newest_complete_checkpoint, publish_checkpoint, diff --git a/tests/models/qwen3_next/test_qwen3_next_environment_identity.py b/tests/models/qwen3_next/test_qwen3_next_environment_identity.py index 26d3e7231..bb4233f3e 100644 --- a/tests/models/qwen3_next/test_qwen3_next_environment_identity.py +++ b/tests/models/qwen3_next/test_qwen3_next_environment_identity.py @@ -4,7 +4,7 @@ import sys from pathlib import Path -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) from qwen3_next_runtime import current_identity, installation_roots # noqa: E402 diff --git a/tests/models/qwen3_next/test_qwen3_next_evidence_redaction.py b/tests/models/qwen3_next/test_qwen3_next_evidence_redaction.py index 5ceb298e3..29ac2a731 100644 --- a/tests/models/qwen3_next/test_qwen3_next_evidence_redaction.py +++ b/tests/models/qwen3_next/test_qwen3_next_evidence_redaction.py @@ -11,7 +11,7 @@ import pytest ROOT = Path(__file__).resolve().parents[3] -sys.path.insert(0, str(ROOT / "tools/validation/models")) +sys.path.insert(0, str(ROOT / "tools/validation/models/qwen3_next")) import qwen3_next_publish_evidence as publisher # noqa: E402 @@ -33,6 +33,7 @@ def published_files(): roots = [ *ROOT.glob("docs/usage/evidence/qwen3-next-*"), + ROOT / "reports/experiments/qwen3_next", *ROOT.glob("docs/operators/qwen3-next-*.md"), *ROOT.glob("docs/usage/qwen3-next-*.md"), ] diff --git a/tests/models/qwen3_next/test_qwen3_next_failure.py b/tests/models/qwen3_next/test_qwen3_next_failure.py index 21de8b2c1..dcb6c3f0e 100644 --- a/tests/models/qwen3_next/test_qwen3_next_failure.py +++ b/tests/models/qwen3_next/test_qwen3_next_failure.py @@ -11,7 +11,7 @@ import pytest -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) from qwen3_next_runtime import failure_kind, subprocess_failure # noqa: E402 diff --git a/tests/models/qwen3_next/test_qwen3_next_installed_provider_identity.py b/tests/models/qwen3_next/test_qwen3_next_installed_provider_identity.py index 9bfb87677..9db266b82 100644 --- a/tests/models/qwen3_next/test_qwen3_next_installed_provider_identity.py +++ b/tests/models/qwen3_next/test_qwen3_next_installed_provider_identity.py @@ -9,7 +9,7 @@ import pytest -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) from qwen3_next_runtime import installed_provider_sources # noqa: E402 diff --git a/tests/models/qwen3_next/test_qwen3_next_l2.py b/tests/models/qwen3_next/test_qwen3_next_l2.py index 9c213ff3f..7d9c99ae9 100644 --- a/tests/models/qwen3_next/test_qwen3_next_l2.py +++ b/tests/models/qwen3_next/test_qwen3_next_l2.py @@ -10,7 +10,7 @@ import pytest import torch -from rl_engine.validation.models.qwen3_next_l2 import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( PROMPT_LENGTHS, RESPONSE_LENGTH, VOCAB_SIZE, @@ -231,7 +231,7 @@ def test_first_divergence_tracks_state_even_when_final_logits_match(small_artifa "node", ["input_layernorm", "mixer", "post_attention_layernorm", "router", "mlp"] ) def test_first_divergent_node_names_the_sub_node(small_artifact, node): - from rl_engine.validation.models.qwen3_next_l2 import first_divergent_node + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import first_divergent_node _, artifact = small_artifact changed = copy.deepcopy(artifact["layer_digests"]) @@ -243,7 +243,7 @@ def test_first_divergent_node_names_the_sub_node(small_artifact, node): def test_first_divergent_node_starts_at_the_embedding(small_artifact): - from rl_engine.validation.models.qwen3_next_l2 import first_divergent_node + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import first_divergent_node _, artifact = small_artifact changed = copy.deepcopy(artifact["layer_digests"]) @@ -257,7 +257,7 @@ def test_first_divergent_node_starts_at_the_embedding(small_artifact): def test_first_divergent_node_falls_back_to_layer_output_and_state(small_artifact): - from rl_engine.validation.models.qwen3_next_l2 import first_divergent_node + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import first_divergent_node _, artifact = small_artifact assert first_divergent_node(artifact, artifact) is None @@ -312,7 +312,9 @@ def test_only_initial_rollout_can_generate_fixture(frozen): def test_native_worker_absence_fails_readiness(monkeypatch): - sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) + sys.path.insert( + 0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next") + ) import qwen3_next_l2 as runner def absent(name): @@ -324,7 +326,9 @@ def absent(name): def test_gpu_release_never_cancels_an_unrelated_process(monkeypatch): - sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) + sys.path.insert( + 0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next") + ) import qwen3_next_l2 as runner commands = [] @@ -344,7 +348,9 @@ def test_batch_callback_loads_once_and_freezes_before_recompute_cases(tmp_path, import json from types import SimpleNamespace - sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) + sys.path.insert( + 0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next") + ) import qwen3_next_l2 as runner calls = [] @@ -424,7 +430,7 @@ def eager_recompute(cases): def test_passed_flag_cannot_replace_full_matrix(): - from rl_engine.validation.models.qwen3_next_l2 import validate_matrix_report + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_matrix_report with pytest.raises(ValueError, match="sixteen-case"): validate_matrix_report({"status": "passed", "raw_bit_comparison": True}) @@ -484,7 +490,7 @@ def deep_verify(path, expected): def test_debug_capture_must_contain_matching_layer_state(small_artifact): - from rl_engine.validation.models.qwen3_next_l2 import validate_debug_capture + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_debug_capture request, artifact = small_artifact request = {**request, "trace_layers": [2]} @@ -509,7 +515,7 @@ def test_debug_capture_must_contain_matching_layer_state(small_artifact): def test_model_artifact_hash_is_rechecked_before_deserialization(tmp_path): from qwen3_next_runtime import file_digest - from rl_engine.validation.models.qwen3_next_l2 import checked_artifact_path + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import checked_artifact_path artifact = tmp_path / "output.pt" save_tensor_artifact(artifact, {"logits": torch.zeros(2, dtype=torch.bfloat16)}) @@ -562,7 +568,9 @@ def scheduler_request_and_audit(frozen): def test_actual_scheduler_must_follow_requested_chunks_and_offsets(frozen): - from rl_engine.validation.models.qwen3_next_l2 import validate_native_scheduler_execution + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( + validate_native_scheduler_execution, + ) request, artifact = scheduler_request_and_audit(frozen) validate_native_scheduler_execution(artifact, request) @@ -577,7 +585,9 @@ def test_actual_scheduler_must_follow_requested_chunks_and_offsets(frozen): def test_scheduler_cannot_shrink_cache_or_claim_certified_execution(frozen): - from rl_engine.validation.models.qwen3_next_l2 import validate_native_scheduler_execution + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( + validate_native_scheduler_execution, + ) request, artifact = scheduler_request_and_audit(frozen) artifact["native_runtime_profile"]["kv_cache_memory_bytes_per_gpu"] //= 2 @@ -590,7 +600,7 @@ def test_scheduler_cannot_shrink_cache_or_claim_certified_execution(frozen): def test_layer_hashes_cannot_replace_four_original_worker_traces(frozen): - from rl_engine.validation.models.qwen3_next_l2 import validate_native_worker_traces + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_native_worker_traces request, artifact = scheduler_request_and_audit(frozen) with pytest.raises(ValueError, match="each TP rank"): @@ -602,11 +612,14 @@ def test_matrix_recomputes_tensor_comparisons_even_after_reports_are_rehashed(tm from qwen3_next_runtime import file_digest - from rl_engine.validation.models.qwen3_next_binary import ( + from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( archive_provider_binary, validate_binary_record, ) - from rl_engine.validation.models.qwen3_next_l2 import json_digest, validate_matrix_report + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( + json_digest, + validate_matrix_report, + ) source = tmp_path / "source" (source / "rl_engine").mkdir(parents=True) diff --git a/tests/models/qwen3_next/test_qwen3_next_layers.py b/tests/models/qwen3_next/test_qwen3_next_layers.py index b3bf66e88..c8160d3d8 100644 --- a/tests/models/qwen3_next/test_qwen3_next_layers.py +++ b/tests/models/qwen3_next/test_qwen3_next_layers.py @@ -11,7 +11,7 @@ import pytest ROOT = Path(__file__).resolve().parents[3] -sys.path.insert(0, str(ROOT / "tools/validation/models")) +sys.path.insert(0, str(ROOT / "tools/validation/models/qwen3_next")) from qwen3_next_layer_contract import ( # noqa: E402 checked_record, @@ -86,7 +86,7 @@ def test_artifact_hash_and_attempt_escape_are_rejected(tmp_path): def test_complete_component_flags_cannot_replace_native_scheduler(tmp_path): - from rl_engine.validation.models.qwen3_next_binary import ( + from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( archive_provider_binary, validate_binary_record, ) diff --git a/tests/models/qwen3_next/test_qwen3_next_native_recompute.py b/tests/models/qwen3_next/test_qwen3_next_native_recompute.py index e7830d4de..47b1157f1 100644 --- a/tests/models/qwen3_next/test_qwen3_next_native_recompute.py +++ b/tests/models/qwen3_next/test_qwen3_next_native_recompute.py @@ -57,7 +57,7 @@ def test_recompute_batch_loads_once_and_keeps_verifiable_original_rank_traces( from rl_engine.integrations.orchestrators.vime.experiments.qwen3_next import ( native_recompute as recompute, ) - from rl_engine.validation.models.qwen3_next_l2 import ( + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( load_tensor_artifact, save_tensor_artifact, validate_native_worker_traces, @@ -217,7 +217,7 @@ def spawn(target, *, args, nprocs, join): @pytest.mark.parametrize("batch", [1, 4]) @pytest.mark.parametrize("prompt_length", [8, 64, 256, 1024]) def test_complete_native_state_shapes_bind_every_consumed_token(batch, prompt_length): - from rl_engine.validation.models.qwen3_next_l2 import native_state_shapes + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import native_state_shapes request = { "prompt_tokens": [[1] * prompt_length for _ in range(batch)], diff --git a/tests/models/qwen3_next/test_qwen3_next_operator_gates.py b/tests/models/qwen3_next/test_qwen3_next_operator_gates.py index 9f2832fa0..819bd56a1 100644 --- a/tests/models/qwen3_next/test_qwen3_next_operator_gates.py +++ b/tests/models/qwen3_next/test_qwen3_next_operator_gates.py @@ -8,8 +8,8 @@ import pytest -from rl_engine.validation.models.qwen3_next_cases import execution_verdict -from tools.validation.models import qwen3_next_operator_gates as gates +from rl_engine.validation.models.qwen3_next.qwen3_next_cases import execution_verdict +from tools.validation.models.qwen3_next import qwen3_next_operator_gates as gates CASE = "tests/example.py::test_required" diff --git a/tests/models/qwen3_next/test_qwen3_next_profile.py b/tests/models/qwen3_next/test_qwen3_next_profile.py index 7013c7f0b..f3c2bb599 100644 --- a/tests/models/qwen3_next/test_qwen3_next_profile.py +++ b/tests/models/qwen3_next/test_qwen3_next_profile.py @@ -127,7 +127,7 @@ def test_updated_export_cannot_be_labeled_as_baseline(updated_identity): def test_historical_export_identity_checks_do_not_open_deleted_shards(updated_identity): - from rl_engine.validation.models.qwen3_next_l2 import validate_weight_identity + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_weight_identity identity, _ = updated_identity validate_weight_identity(identity) diff --git a/tests/models/qwen3_next/test_qwen3_next_rl_fixture.py b/tests/models/qwen3_next/test_qwen3_next_rl_fixture.py index 31a823368..d7994ce25 100644 --- a/tests/models/qwen3_next/test_qwen3_next_rl_fixture.py +++ b/tests/models/qwen3_next/test_qwen3_next_rl_fixture.py @@ -6,7 +6,7 @@ import pytest import torch -from rl_engine.validation.models.qwen3_next_rl_fixture import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_rl_fixture import ( OPTIMIZER_SETTINGS, POLICY_SETTINGS, PolicyFixture, diff --git a/tests/models/qwen3_next/test_qwen3_next_run.py b/tests/models/qwen3_next/test_qwen3_next_run.py index 3b99d763e..ab24383dc 100644 --- a/tests/models/qwen3_next/test_qwen3_next_run.py +++ b/tests/models/qwen3_next/test_qwen3_next_run.py @@ -9,14 +9,14 @@ import pytest -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) import qwen3_next_run as run # noqa: E402 def manifest(tmp_path, stage="layers", script=None): source = tmp_path / "source" - (source / "tools/validation/models").mkdir(parents=True, exist_ok=True) + (source / "tools/validation/models/qwen3_next").mkdir(parents=True, exist_ok=True) if script is not None: (source / run.STAGES[stage][3]).write_text(script) return { @@ -44,7 +44,9 @@ def frozen(monkeypatch): def test_prepare_refuses_a_dirty_tree(monkeypatch, tmp_path): monkeypatch.setattr( - run, "_git", lambda *args, cwd=None: " M tools/validation/models/qwen3_next_run.py" + run, + "_git", + lambda *args, cwd=None: " M tools/validation/models/qwen3_next/qwen3_next_run.py", ) with pytest.raises(SystemExit, match="Commit the source"): run.prepare("preflight", None, None) diff --git a/tests/models/qwen3_next/test_qwen3_next_storage.py b/tests/models/qwen3_next/test_qwen3_next_storage.py index e897ab7af..84d46d2c7 100644 --- a/tests/models/qwen3_next/test_qwen3_next_storage.py +++ b/tests/models/qwen3_next/test_qwen3_next_storage.py @@ -12,7 +12,7 @@ import pytest -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) import qwen3_next_runtime as storage # noqa: E402 from qwen3_next_checkpoint import ( # noqa: E402 newest_complete_checkpoint, diff --git a/tests/models/qwen3_next/test_qwen3_next_training_checkpoint.py b/tests/models/qwen3_next/test_qwen3_next_training_checkpoint.py index f486a09b5..697918b0e 100644 --- a/tests/models/qwen3_next/test_qwen3_next_training_checkpoint.py +++ b/tests/models/qwen3_next/test_qwen3_next_training_checkpoint.py @@ -16,7 +16,7 @@ import torch.distributed as dist import torch.multiprocessing as mp -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) import qwen3_next_optimizer_checkpoint as optimizer_checkpoint # noqa: E402 import qwen3_next_training_checkpoint as checkpoint # noqa: E402 from qwen3_next_checkpoint import publish_checkpoint # noqa: E402 @@ -25,7 +25,7 @@ capture_rng_state, state_digest, ) -from rl_engine.validation.models.qwen3_next_rl_fixture import PolicyFixture # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_rl_fixture import PolicyFixture # noqa: E402 IDENTITY = {"source": "pinned", "provider": "strict", "checkpoint": "official", "tp": 4} diff --git a/tests/models/qwen3_next/test_qwen3_next_training_closeout.py b/tests/models/qwen3_next/test_qwen3_next_training_closeout.py index 4f0e41e0e..a81fdb6af 100644 --- a/tests/models/qwen3_next/test_qwen3_next_training_closeout.py +++ b/tests/models/qwen3_next/test_qwen3_next_training_closeout.py @@ -39,7 +39,7 @@ def test_requeue_repeats_final_reload_without_reusing_stale_attempt(monkeypatch) assert not runner.repeat_reload_in_current_attempt(journal, "1") -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) import qwen3_next_training as runner # noqa: E402 from rl_engine.integrations.orchestrators.vime.experiments.qwen3_next import ( # noqa: E402 @@ -376,7 +376,7 @@ def test_partial_training_evidence_uses_the_phase_export_identity(tmp_path, monk import qwen3_next_contract as contract import qwen3_next_l2 - from rl_engine.validation.models import qwen3_next_l2 as l2_testing + from rl_engine.validation.models.qwen3_next import qwen3_next_l2 as l2_testing manifest = {"stage": "train1", "checkpoint": "/official"} export = {"checkpoint": "/export", "weight_version": "update-1", "base_checkpoint": "/official"} diff --git a/tests/models/qwen3_next/test_qwen3_next_training_export.py b/tests/models/qwen3_next/test_qwen3_next_training_export.py index bec03f57a..73117908f 100644 --- a/tests/models/qwen3_next/test_qwen3_next_training_export.py +++ b/tests/models/qwen3_next/test_qwen3_next_training_export.py @@ -13,7 +13,7 @@ import torch from safetensors.torch import save_file -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) import qwen3_next_training_export as export # noqa: E402 from qwen3_next_runtime import file_digest # noqa: E402 diff --git a/tests/models/qwen3_next/test_qwen3_next_workload.py b/tests/models/qwen3_next/test_qwen3_next_workload.py index 9710f0151..0b06fcb2d 100644 --- a/tests/models/qwen3_next/test_qwen3_next_workload.py +++ b/tests/models/qwen3_next/test_qwen3_next_workload.py @@ -9,12 +9,12 @@ import pytest from rl_engine.config.workload import WorkloadError, load_manifest, manifest_identity_hash -from rl_engine.validation.models.qwen3_next_workload import validate_norm_manifest +from rl_engine.validation.models.qwen3_next.qwen3_next_workload import validate_norm_manifest from rl_engine.validation.operators.gradient_adapters import get_adapter, resolve_profile_candidate MANIFEST = ( Path(__file__).resolve().parents[3] - / "rl_engine/validation/models/qwen3_next_norm_manifest.json" + / "rl_engine/validation/models/qwen3_next/qwen3_next_norm_manifest.json" ) @@ -58,7 +58,7 @@ def test_norm_manifest_rejects_false_evidence_even_with_regenerated_hash(fault): def test_norm_dimension_gate_rejects_shrunk_workload(): - from rl_engine.validation.models.qwen3_next_workload import validate_norm_dimensions + from rl_engine.validation.models.qwen3_next.qwen3_next_workload import validate_norm_dimensions raw = load_manifest(MANIFEST).raw with pytest.raises(WorkloadError, match="hidden 2048"): diff --git a/tools/validation/models/plot_qwen3_next_distributed_logprob.py b/tools/validation/models/plot_qwen3_next_distributed_logprob.py deleted file mode 100644 index 1aad48766..000000000 --- a/tools/validation/models/plot_qwen3_next_distributed_logprob.py +++ /dev/null @@ -1,145 +0,0 @@ -#!/usr/bin/env python -# SPDX-License-Identifier: Apache-2.0 -# Copyright (c) 2026 RL-Kernel Contributors - -"""Render the Qwen3-Next distributed logprob parity evidence as a PNG. - - python tools/validation/models/plot_qwen3_next_distributed_logprob.py \\ - docs/usage/evidence/qwen3-next-distributed-logprob-b200 - -reads ``parity/runs//rank-0.json`` and ``prior-art/report.json`` and -writes ``figure.png``. Top: for every TP4 cross-rank point, whether its digest -under each NCCL setting equals the digest under NCCL defaults (the raw-NCCL -negative control is the last row). Bottom: the selected-token logprob prior art --- invariance checks, the largest difference between implementations, latency. -""" - -from __future__ import annotations - -import argparse -import json -from pathlib import Path - -import matplotlib - -matplotlib.use("Agg") -import matplotlib.pyplot as plt # noqa: E402 -import numpy as np # noqa: E402 -from matplotlib.colors import ListedColormap # noqa: E402 - -INK = "#2b2b29" -MUTED = "#6f6e69" -SURFACE = "#fcfcfb" -RL_KERNEL = "#2a78d6" -EXISTING = ("#eb6834", "#b8501f", "#f29a62") -STATUS = ListedColormap(["#d64545", "#1baf7a"]) -CONTROL = "o_proj_partials.nccl_all_reduce" -LABELS = { - "rl_kernel_ws2": "RL-Kernel WS2\n(both engines here)", - "vllm_gather": "vLLM sampler\n(gather + log_softmax)", - "megatron_ce": "Megatron-core\nvocab_parallel_cross_entropy", - "vime_default": "VIME default\nlogprob", -} -CHECKS = ( - ("tp4_equals_tp1", "TP4 == TP1"), - ("nccl_setting_invariant", "NCCL setting\ninvariant"), - ("batch_bitwise", "batch\ninvariant"), -) - - -def _setting_label(name): - return name.replace("-cdefault", "").replace("-c1", "\n1 ch").replace("-", "\n") - - -def plot(evidence: Path, out: Path) -> None: - runs = sorted( - (evidence / "parity" / "runs").iterdir(), key=lambda p: (p.name != "default", p.name) - ) - digests = {run.name: json.loads((run / "rank-0.json").read_text())["points"] for run in runs} - points = [p for p, v in digests["default"].items() if isinstance(v, str) and p != CONTROL] - points = sorted(points) + [CONTROL] - grid = np.array( - [[digests[run.name][p] == digests["default"][p] for run in runs] for p in points] - ) - prior = json.loads((evidence / "prior-art" / "report.json").read_text())["candidates"] - keys = [k for k in LABELS if k in prior and "unavailable" not in prior[k]] - - fig = plt.figure(figsize=(17, 11), facecolor=SURFACE) - spec = fig.add_gridspec(2, 3, height_ratios=[1.35, 1], width_ratios=[1.1, 1, 1]) - top = fig.add_subplot(spec[0, :]) - top.imshow(grid, cmap=STATUS, vmin=0, vmax=1, aspect="auto") - top.set_yticks( - range(len(points)), - [ - p if p != CONTROL else "NEGATIVE CONTROL: o_proj partials, NCCL all_reduce" - for p in points - ], - fontsize=8, - color=INK, - ) - top.set_xticks(range(len(runs)), [_setting_label(r.name) for r in runs], fontsize=7, color=INK) - top.axhline(len(points) - 1.5, color=SURFACE, linewidth=4) - top.set_title( - "TP4 cross-rank points on real weights: digest under each NCCL setting == NCCL defaults " - "(green) or not (red)", - fontsize=10, - color=INK, - ) - - checks = fig.add_subplot(spec[1, 0]) - table = np.array([[bool(prior[k][c]) for c, _ in CHECKS] for k in keys]) - checks.imshow(table, cmap=STATUS, vmin=0, vmax=1, aspect="auto") - checks.set_yticks(range(len(keys)), [LABELS[k] for k in keys], fontsize=8, color=INK) - checks.set_xticks(range(len(CHECKS)), [label for _, label in CHECKS], fontsize=8, color=INK) - checks.set_title("Selected-token logprob: invariance (bitwise)", fontsize=10, color=INK) - - pairs = fig.add_subplot(spec[1, 1]) - delta = np.array( - [[0.0 if a == b else prior[a]["max_abs_vs"].get(b, np.nan) for b in keys] for a in keys] - ) - pairs.imshow(delta > 0, cmap=ListedColormap(["#1baf7a", "#f29a62"]), vmin=0, vmax=1) - for i in range(len(keys)): - for j in range(len(keys)): - text = "0" if delta[i, j] == 0 else f"{delta[i, j]:.1e}" - pairs.text(j, i, text, ha="center", va="center", fontsize=8, color=INK) - short = [LABELS[k].split("\n")[0] for k in keys] - pairs.set_xticks(range(len(keys)), short, fontsize=7, rotation=20, color=INK) - pairs.set_yticks(range(len(keys)), short, fontsize=7, color=INK) - pairs.set_title("Max |Δ logprob| between implementations", fontsize=10, color=INK) - - latency = fig.add_subplot(spec[1, 2]) - values = [prior[k]["latency_us_tp4"] / 1000.0 for k in keys] - colors = [RL_KERNEL if k == "rl_kernel_ws2" else EXISTING[i % 3] for i, k in enumerate(keys)] - latency.barh(range(len(keys)), values, color=colors) - latency.set_yticks(range(len(keys)), short, fontsize=8, color=INK) - latency.invert_yaxis() - for i, v in enumerate(values): - latency.text(v, i, f" {v:.2f} ms", va="center", fontsize=8, color=MUTED) - latency.set_xlabel("TP4 latency, 1024 tokens (ms, median)", fontsize=9, color=MUTED) - latency.set_title("Latency", fontsize=10, color=INK) - - for axis in fig.axes: - for spine in axis.spines.values(): - spine.set_visible(False) - env = json.loads((evidence / "prior-art" / "report.json").read_text())["environment"] - fig.suptitle( - f"Qwen3-Next TP4 distributed logprob parity - {env['gpu']}, NCCL {env['nccl']}, " - f"torch {env['torch']}", - fontsize=12, - color=INK, - ) - fig.tight_layout() - fig.savefig(out, dpi=150) - - -def main() -> None: - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("evidence", type=Path) - args = parser.parse_args() - out = args.evidence / "figure.png" - plot(args.evidence, out) - print(f"wrote {out}") - - -if __name__ == "__main__": - main() diff --git a/tools/validation/models/qwen3_next/__init__.py b/tools/validation/models/qwen3_next/__init__.py new file mode 100644 index 000000000..86cf4c9da --- /dev/null +++ b/tools/validation/models/qwen3_next/__init__.py @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors diff --git a/tools/validation/models/plot_qwen3_next_attention_prior_art.py b/tools/validation/models/qwen3_next/plot_qwen3_next_attention_prior_art.py similarity index 98% rename from tools/validation/models/plot_qwen3_next_attention_prior_art.py rename to tools/validation/models/qwen3_next/plot_qwen3_next_attention_prior_art.py index 6a3fd7a3f..2e7aa80e6 100644 --- a/tools/validation/models/plot_qwen3_next_attention_prior_art.py +++ b/tools/validation/models/qwen3_next/plot_qwen3_next_attention_prior_art.py @@ -4,7 +4,7 @@ """Render the TP4 attention prior-art report as a PNG. - python tools/validation/models/plot_qwen3_next_attention_prior_art.py \\ + python tools/validation/models/qwen3_next/plot_qwen3_next_attention_prior_art.py \\ docs/usage/evidence/qwen3-next-tp4-mixers-b200/attention/report.json writes ``figure.png`` next to the report: which invariance checks each diff --git a/tools/validation/models/plot_qwen3_next_batch_invariance.py b/tools/validation/models/qwen3_next/plot_qwen3_next_batch_invariance.py similarity index 97% rename from tools/validation/models/plot_qwen3_next_batch_invariance.py rename to tools/validation/models/qwen3_next/plot_qwen3_next_batch_invariance.py index 21a128a9f..5dea43697 100644 --- a/tools/validation/models/plot_qwen3_next_batch_invariance.py +++ b/tools/validation/models/qwen3_next/plot_qwen3_next_batch_invariance.py @@ -6,8 +6,8 @@ Needs only the published evidence directory and matplotlib: - python tools/validation/models/plot_qwen3_next_batch_invariance.py \\ - docs/usage/evidence/qwen3-next-batch-invariance-b200 + python tools/validation/models/qwen3_next/plot_qwen3_next_batch_invariance.py \\ + artifacts/qwen3-next-batch-invariance-b200 writes ``figure.png`` into that directory: one panel per cell family. A cell is green when the target request's response tokens, logits, logprobs and every diff --git a/tools/validation/models/plot_qwen3_next_chain.py b/tools/validation/models/qwen3_next/plot_qwen3_next_chain.py similarity index 98% rename from tools/validation/models/plot_qwen3_next_chain.py rename to tools/validation/models/qwen3_next/plot_qwen3_next_chain.py index f3eab8718..06a3accb2 100644 --- a/tools/validation/models/plot_qwen3_next_chain.py +++ b/tools/validation/models/qwen3_next/plot_qwen3_next_chain.py @@ -6,7 +6,7 @@ Needs only the published evidence directory and matplotlib: - python tools/validation/models/plot_qwen3_next_chain.py \\ + python tools/validation/models/qwen3_next/plot_qwen3_next_chain.py \\ docs/usage/evidence/qwen3-next-chain-b200 writes ``figure.png`` into that directory. Left: for each L2 case and weight diff --git a/tools/validation/models/plot_qwen3_next_moe_prior_art.py b/tools/validation/models/qwen3_next/plot_qwen3_next_moe_prior_art.py similarity index 98% rename from tools/validation/models/plot_qwen3_next_moe_prior_art.py rename to tools/validation/models/qwen3_next/plot_qwen3_next_moe_prior_art.py index 21cee4fb9..05341ad7c 100644 --- a/tools/validation/models/plot_qwen3_next_moe_prior_art.py +++ b/tools/validation/models/qwen3_next/plot_qwen3_next_moe_prior_art.py @@ -6,7 +6,7 @@ Needs only the report JSON and matplotlib, so it runs anywhere: - python tools/validation/models/plot_qwen3_next_moe_prior_art.py \\ + python tools/validation/models/qwen3_next/plot_qwen3_next_moe_prior_art.py \\ docs/usage/evidence/qwen3-next-moe-route-b200/report.json writes ``figure.png`` next to the report. Three panels: which batch-invariance diff --git a/tools/validation/models/plot_qwen3_next_norm_evidence.py b/tools/validation/models/qwen3_next/plot_qwen3_next_norm_evidence.py similarity index 95% rename from tools/validation/models/plot_qwen3_next_norm_evidence.py rename to tools/validation/models/qwen3_next/plot_qwen3_next_norm_evidence.py index 5067d4d7c..78a6b4979 100644 --- a/tools/validation/models/plot_qwen3_next_norm_evidence.py +++ b/tools/validation/models/qwen3_next/plot_qwen3_next_norm_evidence.py @@ -2,9 +2,10 @@ # SPDX-License-Identifier: Apache-2.0 # Copyright (c) 2026 RL-Kernel Contributors -"""Plot a report from tools/validation/models/qwen3_next_norm_evidence.py: one 2x2 figure per op. +"""Plot a report from tools/validation/models/qwen3_next/qwen3_next_norm_evidence.py: one 2x2 +figure per op. - python tools/validation/models/plot_qwen3_next_norm_evidence.py report.json + python tools/validation/models/qwen3_next/plot_qwen3_next_norm_evidence.py report.json Writes figure[-].png beside the report. """ diff --git a/tools/validation/models/qwen3_next_adapter_check.py b/tools/validation/models/qwen3_next/qwen3_next_adapter_check.py similarity index 100% rename from tools/validation/models/qwen3_next_adapter_check.py rename to tools/validation/models/qwen3_next/qwen3_next_adapter_check.py diff --git a/tools/validation/models/qwen3_next_attention_prior_art.py b/tools/validation/models/qwen3_next/qwen3_next_attention_prior_art.py similarity index 92% rename from tools/validation/models/qwen3_next_attention_prior_art.py rename to tools/validation/models/qwen3_next/qwen3_next_attention_prior_art.py index 6f4f0e485..42fd8291f 100644 --- a/tools/validation/models/qwen3_next_attention_prior_art.py +++ b/tools/validation/models/qwen3_next/qwen3_next_attention_prior_art.py @@ -7,9 +7,10 @@ Run it on a clean tree with ``rl_engine._C`` built in place. The report records the commit, whether the tracked tree was dirty and the version of every library compared. -``tools/validation/models/plot_qwen3_next_attention_prior_art.py`` turns the report into a figure. +``tools/validation/models/qwen3_next/plot_qwen3_next_attention_prior_art.py`` turns the report +into a figure. - python tools/validation/models/qwen3_next_attention_prior_art.py \\ + python tools/validation/models/qwen3_next/qwen3_next_attention_prior_art.py \\ --out docs/usage/evidence/qwen3-next-tp4-mixers-b200/attention/report.json """ @@ -22,12 +23,12 @@ import sys from pathlib import Path -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(ROOT)) import torch # noqa: E402 -from rl_engine.validation.models.qwen3_next_attention_prior_art import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_attention_prior_art import ( # noqa: E402 attention_report, ) diff --git a/tools/validation/models/qwen3_next_batch_invariance.py b/tools/validation/models/qwen3_next/qwen3_next_batch_invariance.py similarity index 94% rename from tools/validation/models/qwen3_next_batch_invariance.py rename to tools/validation/models/qwen3_next/qwen3_next_batch_invariance.py index ead3e37b2..762b9b9f6 100644 --- a/tools/validation/models/qwen3_next_batch_invariance.py +++ b/tools/validation/models/qwen3_next/qwen3_next_batch_invariance.py @@ -5,7 +5,8 @@ """Stage ``bi``: the Qwen3-Next batch-invariance matrix (RFC #428 ``batch_invariance_ci``). Runs after a passed ``l2`` stage, under the same certified provider profile -(``VLLM_BATCH_INVARIANT=1``). The cells are ``rl_engine/validation/models/qwen3_next_bi_cases.py`` +(``VLLM_BATCH_INVARIANT=1``). The cells are +``rl_engine/validation/models/qwen3_next/qwen3_next_bi_cases.py`` (pinned by ``qwen3_next_bi_manifest.json``); ``bi_suite: quick`` in the manifest runs the CI subset. Two independent worker processes, as in ``l2``: @@ -28,9 +29,9 @@ import sys from pathlib import Path -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(ROOT)) -sys.path.insert(0, str(ROOT / "tools/validation/models")) +sys.path.insert(0, str(ROOT / "tools/validation/models/qwen3_next")) from qwen3_next_contract import evidence_record, result_envelope, validate_result # noqa: E402 from qwen3_next_l2 import ( # noqa: E402 @@ -50,14 +51,14 @@ verify_environment, ) -from rl_engine.validation.models import qwen3_next_bi_cases as bi # noqa: E402 -from rl_engine.validation.models.qwen3_next_binary import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next import qwen3_next_bi_cases as bi # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( # noqa: E402 archive_provider_binary, validate_binary_record, verify_imported_provider, ) -from rl_engine.validation.models.qwen3_next_chain_report import family_of # noqa: E402 -from rl_engine.validation.models.qwen3_next_l2 import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_chain_report import family_of # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( # noqa: E402 FROZEN_TEXTS, compare_artifacts, compare_batch_prefill, @@ -232,7 +233,7 @@ def execute(manifest_path, output): run_process( [ manifest["python"], - str(ROOT / "tools/validation/models/qwen3_next_l2.py"), + str(ROOT / "tools/validation/models/qwen3_next/qwen3_next_l2.py"), "--gpu-probe", "--output", str(probe), diff --git a/tools/validation/models/qwen3_next_chain_report.py b/tools/validation/models/qwen3_next/qwen3_next_chain_report.py similarity index 92% rename from tools/validation/models/qwen3_next_chain_report.py rename to tools/validation/models/qwen3_next/qwen3_next_chain_report.py index b2f1cf6da..2c8881c4f 100644 --- a/tools/validation/models/qwen3_next_chain_report.py +++ b/tools/validation/models/qwen3_next/qwen3_next_chain_report.py @@ -4,13 +4,13 @@ """Write the WS1 chain-gate report for a finished Qwen3-Next chain. - python tools/validation/models/qwen3_next_chain_report.py \ + python tools/validation/models/qwen3_next/qwen3_next_chain_report.py \ --last --out chain-report.json Follows ``previous`` from the given run back to the ``l2`` stage, reads every passed stage's L2 matrix (``baseline`` from ``l2``, ``update1``/``update2`` from ``train1``/``train2``, ``reload`` from ``train``) and the hash-checked case -artifacts it names, and writes ``rl_engine.validation.models.qwen3_next_chain_report``'s +artifacts it names, and writes ``rl_engine.validation.models.qwen3_next.qwen3_next_chain_report``'s report. Exits 1 when the chain did not pass. """ @@ -19,12 +19,14 @@ import sys from pathlib import Path -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(ROOT)) from rl_engine.contracts.numerical import load_contract # noqa: E402 -from rl_engine.validation.models.qwen3_next_chain_report import build_report # noqa: E402 -from rl_engine.validation.models.qwen3_next_l2 import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_chain_report import ( # noqa: E402 + build_report, +) +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( # noqa: E402 checked_artifact_path, load_tensor_artifact, ) diff --git a/tools/validation/models/qwen3_next_checkpoint.py b/tools/validation/models/qwen3_next/qwen3_next_checkpoint.py similarity index 100% rename from tools/validation/models/qwen3_next_checkpoint.py rename to tools/validation/models/qwen3_next/qwen3_next_checkpoint.py diff --git a/tools/validation/models/qwen3_next_contract.py b/tools/validation/models/qwen3_next/qwen3_next_contract.py similarity index 96% rename from tools/validation/models/qwen3_next_contract.py rename to tools/validation/models/qwen3_next/qwen3_next_contract.py index d436e668e..ba24d15fb 100644 --- a/tools/validation/models/qwen3_next_contract.py +++ b/tools/validation/models/qwen3_next/qwen3_next_contract.py @@ -47,8 +47,8 @@ def checkpoint_identity(checkpoint): index = json.loads((checkpoint / "model.safetensors.index.json").read_text()) reference = json.loads( ( - Path(__file__).resolve().parents[3] - / "rl_engine/validation/models/qwen3_next_norm_manifest.json" + Path(__file__).resolve().parents[4] + / "rl_engine/validation/models/qwen3_next/qwen3_next_norm_manifest.json" ).read_text() )["model_identity"] snapshot = reference["weight_snapshot"] @@ -82,7 +82,9 @@ def checkpoint_identity(checkpoint): def operator_requirements(root, suite): registry = json.loads( - (Path(root) / "rl_engine/validation/models/qwen3_next_operator_cases.json").read_text() + ( + Path(root) / "rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json" + ).read_text() ) if registry.get("schema_version") != 1: raise ValueError("Unsupported mandatory operator case registry") @@ -155,7 +157,7 @@ def validate_partial_training_evidence(manifest, root, evidence, *, attempt_root from qwen3_next_l2 import model_identity from qwen3_next_training import STAGE_LAST_PHASE, read_record, stage_phases - from rl_engine.validation.models.qwen3_next_l2 import validate_matrix_report + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_matrix_report stage = manifest["stage"] progress = json.loads((root / evidence["updates"]["path"]).read_text()) @@ -290,7 +292,7 @@ def validate_result(manifest, result, *, run_dir=None, attempt=None): if manifest["stage"] == "bi": from qwen3_next_l2 import model_identity - from rl_engine.validation.models import qwen3_next_bi_cases as bi + from rl_engine.validation.models.qwen3_next import qwen3_next_bi_cases as bi matrix = json.loads((root / evidence["bi_matrix"]["path"]).read_text()) suite = bi.quick_cells() if manifest.get("bi_suite") == "quick" else bi.cells() @@ -327,7 +329,9 @@ def validate_result(manifest, result, *, run_dir=None, attempt=None): root=attempt_root, ) else: - from rl_engine.validation.models.qwen3_next_l2 import validate_matrix_report + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( + validate_matrix_report, + ) certificate = manifest.get("scheduler_profile_certificate", {}) certificate_path = Path(certificate.get("path", "")) diff --git a/tools/validation/models/qwen3_next_gate_common.py b/tools/validation/models/qwen3_next/qwen3_next_gate_common.py similarity index 99% rename from tools/validation/models/qwen3_next_gate_common.py rename to tools/validation/models/qwen3_next/qwen3_next_gate_common.py index f1a8e94b5..b58dbca91 100644 --- a/tools/validation/models/qwen3_next_gate_common.py +++ b/tools/validation/models/qwen3_next/qwen3_next_gate_common.py @@ -20,7 +20,7 @@ import torch.distributed as dist from safetensors import safe_open -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] class CheckpointWeights(Mapping): diff --git a/tools/validation/models/qwen3_next_l2.py b/tools/validation/models/qwen3_next/qwen3_next_l2.py similarity index 98% rename from tools/validation/models/qwen3_next_l2.py rename to tools/validation/models/qwen3_next/qwen3_next_l2.py index cb874ca0e..f7c5360f1 100644 --- a/tools/validation/models/qwen3_next_l2.py +++ b/tools/validation/models/qwen3_next/qwen3_next_l2.py @@ -14,7 +14,7 @@ import time from pathlib import Path -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(ROOT)) from qwen3_next_contract import ( # noqa: E402 @@ -34,12 +34,12 @@ verify_environment, ) -from rl_engine.validation.models.qwen3_next_binary import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( # noqa: E402 archive_provider_binary, validate_binary_record, verify_imported_provider, ) -from rl_engine.validation.models.qwen3_next_l2 import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( # noqa: E402 NATIVE_WORKER_MODULE, PROMPT_LENGTHS, L2Case, @@ -113,7 +113,7 @@ def model_identity(manifest): base_checkpoint=manifest["base_checkpoint"], base_checkpoint_identity=manifest["base_checkpoint_identity"], ) - from rl_engine.validation.models.qwen3_next_l2 import validate_weight_identity + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_weight_identity validate_weight_identity(identity) return identity @@ -126,7 +126,9 @@ def verify_before_model_load(manifest, output): if checkpoint_identity(manifest["checkpoint"]) != manifest["checkpoint_identity"]: raise RuntimeError("checkpoint_mismatch: official weight snapshot changed") reference = json.loads( - (ROOT / "rl_engine/validation/models/qwen3_next_norm_manifest.json").read_text() + ( + ROOT / "rl_engine/validation/models/qwen3_next/qwen3_next_norm_manifest.json" + ).read_text() )["model_identity"] verified = verify_checkpoint(Path(manifest["checkpoint"]), reference) atomic_json(output / "weight-verification.json", verified) @@ -213,7 +215,7 @@ def cases(): if fixture is None: from transformers import AutoTokenizer - from rl_engine.validation.models.qwen3_next_l2 import freeze_responses + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import freeze_responses tokenizer = AutoTokenizer.from_pretrained( manifest["checkpoint"], local_files_only=True, trust_remote_code=False diff --git a/tools/validation/models/qwen3_next_layer_contract.py b/tools/validation/models/qwen3_next/qwen3_next_layer_contract.py similarity index 94% rename from tools/validation/models/qwen3_next_layer_contract.py rename to tools/validation/models/qwen3_next/qwen3_next_layer_contract.py index 67764821d..4b27d4b6a 100644 --- a/tools/validation/models/qwen3_next_layer_contract.py +++ b/tools/validation/models/qwen3_next/qwen3_next_layer_contract.py @@ -12,7 +12,7 @@ def layer_requirements(root): root = Path(root) - path = root / "rl_engine/validation/models/qwen3_next_layer_cases.json" + path = root / "rl_engine/validation/models/qwen3_next/qwen3_next_layer_cases.json" registry = json.loads(path.read_text()) if registry.get("schema_version") != 1 or set(registry.get("gates", {})) != { "gdn", @@ -95,7 +95,7 @@ def validate_layer_report(report, requirement, identity, *, root): raise ValueError("Layer acceptance identity or completeness mismatch") if set(report.get("gates", {})) != set(requirement["gates"]): raise ValueError("Layer acceptance omitted a mandatory gate") - from rl_engine.validation.models.qwen3_next_binary import validate_binary_record + from rl_engine.validation.models.qwen3_next.qwen3_next_binary import validate_binary_record binary = validate_binary_record( report.get("provider_binary"), source_tree=identity["source_tree"], artifact_root=root @@ -114,7 +114,7 @@ def validate_layer_report(report, requirement, identity, *, root): raise ValueError("Layer ranks did not import the archived CUDA provider") scheduler_path = checked_record(report.get("scheduler"), root=root) scheduler = json.loads(scheduler_path.read_text()) - from rl_engine.validation.models.qwen3_next_l2 import validate_scheduler_report + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_scheduler_report validate_scheduler_report(scheduler, expected_identity=identity, artifact_root=root) if scheduler.get("provider_binary") != report["provider_binary"]: diff --git a/tools/validation/models/qwen3_next_layers.py b/tools/validation/models/qwen3_next/qwen3_next_layers.py similarity index 97% rename from tools/validation/models/qwen3_next_layers.py rename to tools/validation/models/qwen3_next/qwen3_next_layers.py index ea3c62a8d..e8578b9c6 100644 --- a/tools/validation/models/qwen3_next_layers.py +++ b/tools/validation/models/qwen3_next/qwen3_next_layers.py @@ -12,7 +12,7 @@ import sys from pathlib import Path -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(ROOT)) from qwen3_next_contract import ( # noqa: E402 @@ -43,11 +43,11 @@ verify_environment, ) -from rl_engine.validation.models.qwen3_next_binary import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( # noqa: E402 archive_provider_binary, validate_binary_record, ) -from rl_engine.validation.models.qwen3_next_l2 import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( # noqa: E402 L2Case, case_matrix, compare_batch_prefill, @@ -192,7 +192,7 @@ def progress(phase): run_process( [ manifest["python"], - str(ROOT / "tools/validation/models/qwen3_next_l2.py"), + str(ROOT / "tools/validation/models/qwen3_next/qwen3_next_l2.py"), "--gpu-probe", "--output", str(probe), diff --git a/tools/validation/models/qwen3_next_logprob_parity.py b/tools/validation/models/qwen3_next/qwen3_next_logprob_parity.py similarity index 96% rename from tools/validation/models/qwen3_next_logprob_parity.py rename to tools/validation/models/qwen3_next/qwen3_next_logprob_parity.py index 58a3679bd..6f53eaef4 100644 --- a/tools/validation/models/qwen3_next_logprob_parity.py +++ b/tools/validation/models/qwen3_next/qwen3_next_logprob_parity.py @@ -4,7 +4,7 @@ """Digest every cross-rank point of the Qwen3-Next TP4 path on real weights. - torchrun --nproc-per-node 4 tools/validation/models/qwen3_next_logprob_parity.py \\ + torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_logprob_parity.py \\ --checkpoint --output Run it once per NCCL setting (``NCCL_ALGO``, ``NCCL_PROTO``, ``NCCL_MAX_NCHANNELS``) @@ -30,7 +30,7 @@ from contextlib import ExitStack from pathlib import Path -sys.path.insert(0, str(Path(__file__).resolve().parents[3])) +sys.path.insert(0, str(Path(__file__).resolve().parents[4])) import torch # noqa: E402 import torch.distributed as dist # noqa: E402 @@ -60,8 +60,11 @@ moe_hf_shapes, ) from rl_engine.models.qwen3_next.qwen3_next_tp_gdn import TP4GDN # noqa: E402 -from tools.validation.models.qwen3_next_gate_common import CheckpointWeights, run_gate # noqa: E402 -from tools.validation.models.qwen3_next_tp_gdn_check import load_first_gdn # noqa: E402 +from tools.validation.models.qwen3_next.qwen3_next_gate_common import ( # noqa: E402 + CheckpointWeights, + run_gate, +) +from tools.validation.models.qwen3_next.qwen3_next_tp_gdn_check import load_first_gdn # noqa: E402 TOKENS = 1024 VOCAB, SHARD = 151936, 37984 diff --git a/tools/validation/models/qwen3_next_logprob_parity_compare.py b/tools/validation/models/qwen3_next/qwen3_next_logprob_parity_compare.py similarity index 97% rename from tools/validation/models/qwen3_next_logprob_parity_compare.py rename to tools/validation/models/qwen3_next/qwen3_next_logprob_parity_compare.py index 0d128d541..e57f8d115 100644 --- a/tools/validation/models/qwen3_next_logprob_parity_compare.py +++ b/tools/validation/models/qwen3_next/qwen3_next_logprob_parity_compare.py @@ -4,7 +4,7 @@ """Compare ``qwen3_next_logprob_parity.py`` runs made under different NCCL settings. - python tools/validation/models/qwen3_next_logprob_parity_compare.py \\ + python tools/validation/models/qwen3_next/qwen3_next_logprob_parity_compare.py \\ --runs ... --out report.json Every point except the raw-NCCL negative control must have one digest per rank diff --git a/tools/validation/models/qwen3_next_moe_prior_art.py b/tools/validation/models/qwen3_next/qwen3_next_moe_prior_art.py similarity index 92% rename from tools/validation/models/qwen3_next_moe_prior_art.py rename to tools/validation/models/qwen3_next/qwen3_next_moe_prior_art.py index c3c8d088a..3484f6754 100644 --- a/tools/validation/models/qwen3_next_moe_prior_art.py +++ b/tools/validation/models/qwen3_next/qwen3_next_moe_prior_art.py @@ -7,9 +7,10 @@ Run it on a clean tree with ``TRITON_F32_DEFAULT=ieee`` (the FP32 router needs IEEE Triton dots from process start). The report records the commit, whether the tracked tree was dirty and the version of every library compared. -``tools/validation/models/plot_qwen3_next_moe_prior_art.py`` turns the report into a figure. +``tools/validation/models/qwen3_next/plot_qwen3_next_moe_prior_art.py`` turns the report into a +figure. - TRITON_F32_DEFAULT=ieee python tools/validation/models/qwen3_next_moe_prior_art.py \\ + TRITON_F32_DEFAULT=ieee python tools/validation/models/qwen3_next/qwen3_next_moe_prior_art.py \\ --out docs/usage/evidence/qwen3-next-moe-route-b200/report.json """ @@ -22,12 +23,12 @@ import sys from pathlib import Path -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(ROOT)) import torch # noqa: E402 -from rl_engine.validation.models.qwen3_next_moe_prior_art import moe_report # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_moe_prior_art import moe_report # noqa: E402 LIBRARIES = ( "vllm", diff --git a/tools/validation/models/qwen3_next_norm_evidence.py b/tools/validation/models/qwen3_next/qwen3_next_norm_evidence.py similarity index 98% rename from tools/validation/models/qwen3_next_norm_evidence.py rename to tools/validation/models/qwen3_next/qwen3_next_norm_evidence.py index aaeba82c3..144496256 100644 --- a/tools/validation/models/qwen3_next_norm_evidence.py +++ b/tools/validation/models/qwen3_next/qwen3_next_norm_evidence.py @@ -9,8 +9,8 @@ bitwise), and timed. Optional providers (transformers, vLLM, FlashInfer) are skipped when they are not installed; the report says which ran. - python tools/validation/models/qwen3_next_norm_evidence.py --out report.json - python tools/validation/models/plot_qwen3_next_norm_evidence.py report.json + python tools/validation/models/qwen3_next/qwen3_next_norm_evidence.py --out report.json + python tools/validation/models/qwen3_next/plot_qwen3_next_norm_evidence.py report.json """ from __future__ import annotations @@ -27,7 +27,7 @@ import torch import torch.nn.functional as F -REPO_ROOT = Path(__file__).resolve().parents[3] +REPO_ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(REPO_ROOT)) from rl_engine.backends.cuda.norm.rmsnorm import ( # noqa: E402 diff --git a/tools/validation/models/qwen3_next_operator_cases.py b/tools/validation/models/qwen3_next/qwen3_next_operator_cases.py similarity index 87% rename from tools/validation/models/qwen3_next_operator_cases.py rename to tools/validation/models/qwen3_next/qwen3_next_operator_cases.py index d5784a5bd..7c9540ba8 100644 --- a/tools/validation/models/qwen3_next_operator_cases.py +++ b/tools/validation/models/qwen3_next/qwen3_next_operator_cases.py @@ -2,10 +2,11 @@ # SPDX-License-Identifier: Apache-2.0 # Copyright (c) 2026 RL-Kernel Contributors -"""Regenerate ``rl_engine/validation/models/qwen3_next_operator_cases.json`` from pytest collection. +"""Regenerate ``rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json`` from +pytest collection. - python tools/validation/models/qwen3_next_operator_cases.py # check, exit 1 if stale - python tools/validation/models/qwen3_next_operator_cases.py --write + python tools/validation/models/qwen3_next/qwen3_next_operator_cases.py + python tools/validation/models/qwen3_next/qwen3_next_operator_cases.py --write The registry pins the exact test IDs the preflight operator gate must run. The ``norm`` and ``core`` suites are mandatory and must pass without skips; @@ -21,8 +22,8 @@ import sys from pathlib import Path -ROOT = Path(__file__).resolve().parents[3] -REGISTRY = ROOT / "rl_engine/validation/models/qwen3_next_operator_cases.json" +ROOT = Path(__file__).resolve().parents[4] +REGISTRY = ROOT / "rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json" NORM_FILES = ( "tests/models/qwen3_next/test_qwen3_next_norm.py", "tests/models/qwen3_next/check_qwen3_next_norm_providers.py", @@ -53,7 +54,8 @@ } DESCRIPTION = ( "Mandatory norm/core contracts; alternative-provider approximate comparisons are " - "diagnostics only. Generated by tools/validation/models/qwen3_next_operator_cases.py; " + "diagnostics only. Generated by " + "tools/validation/models/qwen3_next/qwen3_next_operator_cases.py; " "case changes require review before a frozen run." ) diff --git a/tools/validation/models/qwen3_next_operator_gates.py b/tools/validation/models/qwen3_next/qwen3_next_operator_gates.py similarity index 94% rename from tools/validation/models/qwen3_next_operator_gates.py rename to tools/validation/models/qwen3_next/qwen3_next_operator_gates.py index 3af9522c9..f96fb2533 100644 --- a/tools/validation/models/qwen3_next_operator_gates.py +++ b/tools/validation/models/qwen3_next/qwen3_next_operator_gates.py @@ -12,10 +12,14 @@ import sys from pathlib import Path -from rl_engine.validation.models.qwen3_next_cases import case_digest, execution_verdict, load_cases +from rl_engine.validation.models.qwen3_next.qwen3_next_cases import ( + case_digest, + execution_verdict, + load_cases, +) -ROOT = Path(__file__).resolve().parents[3] -MANIFEST = ROOT / "rl_engine/validation/models/qwen3_next_operator_cases.json" +ROOT = Path(__file__).resolve().parents[4] +MANIFEST = ROOT / "rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json" def run_suite(out: Path, suite: str) -> dict: @@ -72,7 +76,7 @@ def run_suite(out: Path, suite: str) -> dict: sys.executable, f"tools/validation/operators/check_{gate}_invariance.py", "--manifest", - "rl_engine/validation/models/qwen3_next_norm_manifest.json", + "rl_engine/validation/models/qwen3_next/qwen3_next_norm_manifest.json", "--op", op, "--candidate", diff --git a/tools/validation/models/qwen3_next_optimizer_checkpoint.py b/tools/validation/models/qwen3_next/qwen3_next_optimizer_checkpoint.py similarity index 100% rename from tools/validation/models/qwen3_next_optimizer_checkpoint.py rename to tools/validation/models/qwen3_next/qwen3_next_optimizer_checkpoint.py diff --git a/tools/validation/models/qwen3_next_preflight.py b/tools/validation/models/qwen3_next/qwen3_next_preflight.py similarity index 97% rename from tools/validation/models/qwen3_next_preflight.py rename to tools/validation/models/qwen3_next/qwen3_next_preflight.py index fcf312b96..61cc773c9 100644 --- a/tools/validation/models/qwen3_next_preflight.py +++ b/tools/validation/models/qwen3_next/qwen3_next_preflight.py @@ -179,7 +179,7 @@ def run(name, command, env=None): "torch.distributed.run", "--standalone", "--nproc_per_node=4", - "tools/validation/models/qwen3_next_tp_probe.py", + "tools/validation/models/qwen3_next/qwen3_next_tp_probe.py", "--output", str(output / "tp4"), ], @@ -198,7 +198,9 @@ def run(name, command, env=None): progress["phase"] = "checkpoint-integrity" atomic_json(output / "progress.json", progress) identity = json.loads( - Path("rl_engine/validation/models/qwen3_next_norm_manifest.json").read_text() + Path( + "rl_engine/validation/models/qwen3_next/qwen3_next_norm_manifest.json" + ).read_text() ) checkpoint = verify_checkpoint(Path(manifest["checkpoint"]), identity["model_identity"]) atomic_json(output / "checkpoint.json", checkpoint) @@ -206,7 +208,7 @@ def run(name, command, env=None): "rollout-smoke", [ sys.executable, - "tools/validation/models/qwen3_next_rollout_smoke.py", + "tools/validation/models/qwen3_next/qwen3_next_rollout_smoke.py", "--model", manifest["checkpoint"], "--output", diff --git a/tools/validation/models/qwen3_next_publish_evidence.py b/tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py similarity index 98% rename from tools/validation/models/qwen3_next_publish_evidence.py rename to tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py index b276b63c1..fbd2d9b5f 100644 --- a/tools/validation/models/qwen3_next_publish_evidence.py +++ b/tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py @@ -4,7 +4,8 @@ """Copy a finished Qwen3-Next chain's evidence into the repository, without site details. - python tools/validation/models/qwen3_next_publish_evidence.py --last \\ + python tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py \\ + --last \\ --chain-report chain-report.json --out docs/usage/evidence/qwen3-next-chain- For every stage run on the chain (preflight -> ... -> the given run) this copies diff --git a/tools/validation/models/qwen3_next_publish_parity_evidence.py b/tools/validation/models/qwen3_next/qwen3_next_publish_parity_evidence.py similarity index 65% rename from tools/validation/models/qwen3_next_publish_parity_evidence.py rename to tools/validation/models/qwen3_next/qwen3_next_publish_parity_evidence.py index e2968c182..5539139f7 100644 --- a/tools/validation/models/qwen3_next_publish_parity_evidence.py +++ b/tools/validation/models/qwen3_next/qwen3_next_publish_parity_evidence.py @@ -2,16 +2,14 @@ # SPDX-License-Identifier: Apache-2.0 # Copyright (c) 2026 RL-Kernel Contributors -"""Copy distributed-logprob parity outputs into the repository without site details. +"""Copy distributed-logprob parity outputs into an artifact directory without site details. - python tools/validation/models/qwen3_next_publish_parity_evidence.py \\ + python tools/validation/models/qwen3_next/qwen3_next_publish_parity_evidence.py \\ --parity --parity-report report.json \\ - --prior-art \\ - --out docs/usage/evidence/qwen3-next-distributed-logprob-b200 + --out artifacts/qwen3-next-distributed-logprob-b200 -Writes ``parity/runs//rank-*.json``, ``parity/report.json``, -``prior-art/report.json`` and ``prior-art/runs//rank-0.json``, redacted -with the chain publisher's rules, plus ``redaction.json`` and ``SHA256SUMS``. +Writes ``parity/runs//rank-*.json`` and ``parity/report.json``, +redacted with the chain publisher's rules, plus ``redaction.json`` and ``SHA256SUMS``. Refuses to publish anything a forbidden pattern still matches. """ @@ -23,15 +21,12 @@ from qwen3_next_publish_evidence import leftovers, redact -def publish(parity: Path, parity_report: Path, prior_art: Path, out: Path) -> dict: +def publish(parity: Path, parity_report: Path, out: Path) -> dict: out.mkdir(parents=True, exist_ok=False) sources = {Path("parity/report.json"): parity_report} for run in sorted(p for p in parity.iterdir() if p.is_dir()): for rank in sorted(run.glob("rank-*.json")): sources[Path("parity/runs") / run.name / rank.name] = rank - sources[Path("prior-art/report.json")] = prior_art / "report.json" - for run in sorted(p for p in prior_art.iterdir() if p.is_dir()): - sources[Path("prior-art/runs") / run.name / "rank-0.json"] = run / "rank-0.json" counts: dict[str, int] = {} for relative, source in sources.items(): text = redact(source.read_text(), [], counts) @@ -55,10 +50,9 @@ def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--parity", type=Path, required=True) parser.add_argument("--parity-report", type=Path, required=True) - parser.add_argument("--prior-art", type=Path, required=True) parser.add_argument("--out", type=Path, required=True) args = parser.parse_args() - counts = publish(args.parity, args.parity_report, args.prior_art, args.out) + counts = publish(args.parity, args.parity_report, args.out) print(f"published to {args.out}; placeholders {counts}") diff --git a/tools/validation/models/qwen3_next_rollout_smoke.py b/tools/validation/models/qwen3_next/qwen3_next_rollout_smoke.py similarity index 100% rename from tools/validation/models/qwen3_next_rollout_smoke.py rename to tools/validation/models/qwen3_next/qwen3_next_rollout_smoke.py diff --git a/tools/validation/models/qwen3_next_run.py b/tools/validation/models/qwen3_next/qwen3_next_run.py similarity index 99% rename from tools/validation/models/qwen3_next_run.py rename to tools/validation/models/qwen3_next/qwen3_next_run.py index 92673f611..0179e8a65 100644 --- a/tools/validation/models/qwen3_next_run.py +++ b/tools/validation/models/qwen3_next/qwen3_next_run.py @@ -4,7 +4,7 @@ """Run the Qwen3-Next full-model chain inside any allocation with four B200 GPUs. - python tools/validation/models/qwen3_next_run.py --stage all \\ + python tools/validation/models/qwen3_next/qwen3_next_run.py --stage all \\ --runs-root --checkpoint --cuda-home Stages run in order, each in its own run directory and its own clean clone of @@ -53,7 +53,7 @@ wait_owned_child, ) -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] ORDER = ("preflight", "layers", "l2", "train1", "train2", "train") PROVIDER_SOURCES = ( "rl_engine/integrations/engines/train/vllm/qwen3_next_provider.py", diff --git a/tools/validation/models/qwen3_next_runtime.py b/tools/validation/models/qwen3_next/qwen3_next_runtime.py similarity index 97% rename from tools/validation/models/qwen3_next_runtime.py rename to tools/validation/models/qwen3_next/qwen3_next_runtime.py index ebaafa5f9..685fc98fd 100644 --- a/tools/validation/models/qwen3_next_runtime.py +++ b/tools/validation/models/qwen3_next/qwen3_next_runtime.py @@ -6,7 +6,7 @@ Environment identity, failure classification, memory preconditions, owned child processes, checkpoint verification, stage ordering and crash-recoverable training storage. Nothing here talks to a batch scheduler: a run is identified -by the ``execution_id`` that ``tools/validation/models/qwen3_next_run.py`` writes into +by the ``execution_id`` that ``tools/validation/models/qwen3_next/qwen3_next_run.py`` writes into ``run.json``, and any allocation that provides four GPUs can host it. """ @@ -434,17 +434,47 @@ def wait_owned_child(child, interrupted, *, grace_seconds=10): # --------------------------------------------------------------------------- # STAGES = { - "preflight": ("256G", "01:00:00", None, "tools/validation/models/qwen3_next_preflight.py"), - "layers": ("512G", "04:00:00", "preflight", "tools/validation/models/qwen3_next_layers.py"), - "l2": ("512G", "04:00:00", "layers", "tools/validation/models/qwen3_next_l2.py"), + "preflight": ( + "256G", + "01:00:00", + None, + "tools/validation/models/qwen3_next/qwen3_next_preflight.py", + ), + "layers": ( + "512G", + "04:00:00", + "preflight", + "tools/validation/models/qwen3_next/qwen3_next_layers.py", + ), + "l2": ("512G", "04:00:00", "layers", "tools/validation/models/qwen3_next/qwen3_next_l2.py"), # The training acceptance is split so no single job holds a node for a day: # train1 = update1 + export + L2, train2 = update2 + export + L2 (continues # train1's durable state), train = reload + L2 + continuous/resumed controls. - "train1": ("1280G", "06:00:00", "l2", "tools/validation/models/qwen3_next_training.py"), - "train2": ("1280G", "07:00:00", "train1", "tools/validation/models/qwen3_next_training.py"), - "train": ("1280G", "09:00:00", "train2", "tools/validation/models/qwen3_next_training.py"), + "train1": ( + "1280G", + "06:00:00", + "l2", + "tools/validation/models/qwen3_next/qwen3_next_training.py", + ), + "train2": ( + "1280G", + "07:00:00", + "train1", + "tools/validation/models/qwen3_next/qwen3_next_training.py", + ), + "train": ( + "1280G", + "09:00:00", + "train2", + "tools/validation/models/qwen3_next/qwen3_next_training.py", + ), # Batch invariance branches off a passed l2, under the same certified profile. - "bi": ("512G", "06:00:00", "l2", "tools/validation/models/qwen3_next_batch_invariance.py"), + "bi": ( + "512G", + "06:00:00", + "l2", + "tools/validation/models/qwen3_next/qwen3_next_batch_invariance.py", + ), } diff --git a/tools/validation/models/qwen3_next_tp_attention_check.py b/tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py similarity index 95% rename from tools/validation/models/qwen3_next_tp_attention_check.py rename to tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py index a94487e35..1dac36442 100644 --- a/tools/validation/models/qwen3_next_tp_attention_check.py +++ b/tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py @@ -4,7 +4,8 @@ """TP4 full-attention gate on the official checkpoint's layer-3 weights; four GPUs, torchrun. - torchrun --nproc-per-node 4 tools/validation/models/qwen3_next_tp_attention_check.py \\ + torchrun --nproc-per-node 4 \\ + tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py \\ --checkpoint --output Requires, bitwise: the HF load/export round trip with the KV head replicated on @@ -18,7 +19,7 @@ from contextlib import ExitStack from pathlib import Path -sys.path.insert(0, str(Path(__file__).resolve().parents[3])) +sys.path.insert(0, str(Path(__file__).resolve().parents[4])) import torch # noqa: E402 import torch.distributed as dist # noqa: E402 @@ -31,7 +32,7 @@ from rl_engine.validation.common.tensor_identity import ( # noqa: E402 assert_tensor_bitwise_equal as exact, ) -from tools.validation.models.qwen3_next_gate_common import ( # noqa: E402 +from tools.validation.models.qwen3_next.qwen3_next_gate_common import ( # noqa: E402 CheckpointWeights, check_gradients, gather, diff --git a/tools/validation/models/qwen3_next_tp_gdn_check.py b/tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py similarity index 95% rename from tools/validation/models/qwen3_next_tp_gdn_check.py rename to tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py index 65fee3815..dd6801177 100644 --- a/tools/validation/models/qwen3_next_tp_gdn_check.py +++ b/tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py @@ -4,7 +4,7 @@ """TP4 GDN gate on the official checkpoint's layer-0 weights; four GPUs, torchrun. - torchrun --nproc-per-node 4 tools/validation/models/qwen3_next_tp_gdn_check.py \\ + torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py \\ --checkpoint --output A one-layer integration gate, not full-model evidence. Requires, bitwise: the @@ -18,7 +18,7 @@ import sys from pathlib import Path -sys.path.insert(0, str(Path(__file__).resolve().parents[3])) +sys.path.insert(0, str(Path(__file__).resolve().parents[4])) import torch # noqa: E402 import torch.distributed as dist # noqa: E402 @@ -35,7 +35,10 @@ from rl_engine.validation.common.tensor_identity import ( # noqa: E402 assert_tensor_bitwise_equal as exact, ) -from tools.validation.models.qwen3_next_gate_common import check_gradients, run_gate # noqa: E402 +from tools.validation.models.qwen3_next.qwen3_next_gate_common import ( # noqa: E402 + check_gradients, + run_gate, +) def load_first_gdn(checkpoint): diff --git a/tools/validation/models/qwen3_next_tp_head_check.py b/tools/validation/models/qwen3_next/qwen3_next_tp_head_check.py similarity index 100% rename from tools/validation/models/qwen3_next_tp_head_check.py rename to tools/validation/models/qwen3_next/qwen3_next_tp_head_check.py diff --git a/tools/validation/models/qwen3_next_tp_moe_check.py b/tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py similarity index 95% rename from tools/validation/models/qwen3_next_tp_moe_check.py rename to tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py index c18b5b1fd..060e4ef0f 100644 --- a/tools/validation/models/qwen3_next_tp_moe_check.py +++ b/tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py @@ -5,7 +5,7 @@ """TP4 MoE gate on the official checkpoint's layer-0 weights; four GPUs, torchrun. TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 \\ - tools/validation/models/qwen3_next_tp_moe_check.py \\ + tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py \\ --checkpoint --output Each rank writes ``rank-.json``. The gate requires, bitwise: @@ -23,7 +23,7 @@ from contextlib import ExitStack from pathlib import Path -sys.path.insert(0, str(Path(__file__).resolve().parents[3])) +sys.path.insert(0, str(Path(__file__).resolve().parents[4])) import torch # noqa: E402 import torch.distributed as dist # noqa: E402 @@ -40,7 +40,7 @@ from rl_engine.validation.common.tensor_identity import ( # noqa: E402 assert_tensor_bitwise_equal as exact, ) -from tools.validation.models.qwen3_next_gate_common import ( # noqa: E402 +from tools.validation.models.qwen3_next.qwen3_next_gate_common import ( # noqa: E402 CheckpointWeights, check_gradients, gather, diff --git a/tools/validation/models/qwen3_next_tp_probe.py b/tools/validation/models/qwen3_next/qwen3_next_tp_probe.py similarity index 100% rename from tools/validation/models/qwen3_next_tp_probe.py rename to tools/validation/models/qwen3_next/qwen3_next_tp_probe.py diff --git a/tools/validation/models/qwen3_next_training.py b/tools/validation/models/qwen3_next/qwen3_next_training.py similarity index 99% rename from tools/validation/models/qwen3_next_training.py rename to tools/validation/models/qwen3_next/qwen3_next_training.py index dbedf2a4f..fb56620a1 100644 --- a/tools/validation/models/qwen3_next_training.py +++ b/tools/validation/models/qwen3_next/qwen3_next_training.py @@ -17,7 +17,7 @@ from pathlib import Path from uuid import uuid4 -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(ROOT)) from qwen3_next_checkpoint import ( # noqa: E402 @@ -60,11 +60,11 @@ from rl_engine.integrations.orchestrators.vime.providers.qwen3_next_training import ( # noqa: E402 state_digest, ) -from rl_engine.validation.models.qwen3_next_binary import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( # noqa: E402 archive_provider_binary, validate_binary_record, ) -from rl_engine.validation.models.qwen3_next_l2 import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( # noqa: E402 case_matrix, checked_artifact_path, compare_artifacts, @@ -823,7 +823,7 @@ def execute(manifest_path, output): run_process( [ manifest["python"], - str(ROOT / "tools/validation/models/qwen3_next_l2.py"), + str(ROOT / "tools/validation/models/qwen3_next/qwen3_next_l2.py"), "--gpu-probe", "--output", str(probe), diff --git a/tools/validation/models/qwen3_next_training_checkpoint.py b/tools/validation/models/qwen3_next/qwen3_next_training_checkpoint.py similarity index 99% rename from tools/validation/models/qwen3_next_training_checkpoint.py rename to tools/validation/models/qwen3_next/qwen3_next_training_checkpoint.py index f25519a8a..a60cbcb02 100644 --- a/tools/validation/models/qwen3_next_training_checkpoint.py +++ b/tools/validation/models/qwen3_next/qwen3_next_training_checkpoint.py @@ -44,7 +44,7 @@ restore_rng_state, state_digest, ) -from rl_engine.validation.models.qwen3_next_rl_fixture import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_rl_fixture import ( PolicyFixture, require_policy_configuration, ) diff --git a/tools/validation/models/qwen3_next_training_export.py b/tools/validation/models/qwen3_next/qwen3_next_training_export.py similarity index 98% rename from tools/validation/models/qwen3_next_training_export.py rename to tools/validation/models/qwen3_next/qwen3_next_training_export.py index f785e30e3..575cac6c1 100644 --- a/tools/validation/models/qwen3_next_training_export.py +++ b/tools/validation/models/qwen3_next/qwen3_next_training_export.py @@ -54,16 +54,16 @@ def _json_digest(value): def _reference(): path = ( - Path(__file__).resolve().parents[3] - / "rl_engine/validation/models/qwen3_next_norm_manifest.json" + Path(__file__).resolve().parents[4] + / "rl_engine/validation/models/qwen3_next/qwen3_next_norm_manifest.json" ) return json.loads(path.read_text())["model_identity"] def _schema_reference(): path = ( - Path(__file__).resolve().parents[3] - / "rl_engine/validation/models/qwen3_next_export_schema.json" + Path(__file__).resolve().parents[4] + / "rl_engine/validation/models/qwen3_next/qwen3_next_export_schema.json" ) record = json.loads(path.read_text()) if ( diff --git a/tools/validation/models/qwen3_next_logprob_prior_art.py b/tools/validation/models/qwen3_next_logprob_prior_art.py deleted file mode 100644 index 5122268bc..000000000 --- a/tools/validation/models/qwen3_next_logprob_prior_art.py +++ /dev/null @@ -1,140 +0,0 @@ -#!/usr/bin/env python3 -# SPDX-License-Identifier: Apache-2.0 -# Copyright (c) 2026 RL-Kernel Contributors - -"""Run the vocabulary-parallel logprob prior art at one TP degree; merge launches. - - torchrun --nproc-per-node 4 tools/validation/models/qwen3_next_logprob_prior_art.py \\ - --checkpoint --output /tp4 - torchrun --nproc-per-node 1 tools/validation/models/qwen3_next_logprob_prior_art.py \\ - --checkpoint --output /tp1 - python tools/validation/models/qwen3_next_logprob_prior_art.py \\ - --merge /tp4 /tp4- /tp1 --out report.json - -The first ``--merge`` directory is the TP4 reference, the last the TP1 run; -any directories in between are TP4 runs under other NCCL settings. -""" - -import argparse -import json -import os -import sys -from contextlib import ExitStack -from datetime import timedelta -from pathlib import Path - -sys.path.insert(0, str(Path(__file__).resolve().parents[3])) - -import torch # noqa: E402 -import torch.distributed as dist # noqa: E402 - -from rl_engine.validation.models.qwen3_next_logprob_prior_art import ( # noqa: E402 - VOCAB, - candidate_report, - digest, -) -from tools.validation.models.qwen3_next_gate_common import ( # noqa: E402 - CheckpointWeights, - provenance, -) - -TOKENS = 1024 -NCCL_SETTINGS = ("NCCL_ALGO", "NCCL_PROTO", "NCCL_MAX_NCHANNELS") - - -def measure(checkpoint: Path, output: Path) -> None: - tp = int(os.environ["WORLD_SIZE"]) - rank = int(os.environ["LOCAL_RANK"]) - if tp not in (1, 4): - raise SystemExit("Run with one or four ranks") - torch.cuda.set_device(rank) - dist.init_process_group( - "nccl", timeout=timedelta(minutes=15), device_id=torch.device("cuda", rank) - ) - try: - from megatron.core import parallel_state - - parallel_state.initialize_model_parallel(tensor_model_parallel_size=tp) - group = parallel_state.get_tensor_model_parallel_group() - from rl_engine.models.qwen3_next.qwen3_next_forward import shared_linear - - generator = torch.Generator(device="cuda").manual_seed(4280) - with ExitStack() as stack: - head = CheckpointWeights(checkpoint, "", ("lm_head.weight",), stack)["lm_head.weight"] - hidden = torch.randn( - TOKENS, 2048, device="cuda", dtype=torch.bfloat16, generator=generator - ) - targets = torch.randint(0, VOCAB, (TOKENS,), device="cuda", generator=generator) - with torch.no_grad(): - full = shared_linear(hidden, head.cuda()) - report = { - "tp": tp, - "rank": rank, - "tokens": TOKENS, - "nccl_settings": {name: os.environ.get(name) for name in NCCL_SETTINGS}, - "full_logits_sha256": digest(full), - **provenance(), - "candidates": candidate_report(full, targets, tp=tp, group=group), - } - output.mkdir(parents=True, exist_ok=True) - (output / f"rank-{rank}.json").write_text(json.dumps(report, indent=2) + "\n") - dist.barrier() - finally: - dist.destroy_process_group() - - -def merge(runs: list[Path]) -> dict: - load = lambda run: json.loads((run / "rank-0.json").read_text()) # noqa: E731 - tp4, others, tp1 = load(runs[0]), [load(r) for r in runs[1:-1]], load(runs[-1]) - if tp4["tp"] != 4 or tp1["tp"] != 1 or any(o["tp"] != 4 for o in others): - raise SystemExit("Expected TP4 runs first and a TP1 run last") - if len({r["full_logits_sha256"] for r in (tp4, tp1, *others)}) != 1: - raise SystemExit("Launches did not see the same logits") - commits = {r["rl_kernel_commit"] for r in (tp4, tp1, *others)} - table = {} - for key, cand in tp4["candidates"].items(): - if "unavailable" in cand: - table[key] = cand - continue - table[key] = { - "tp4_equals_tp1": cand["selected_sha256"] - == tp1["candidates"].get(key, {}).get("selected_sha256"), - "nccl_setting_invariant": all( - o["candidates"][key].get("selected_sha256") == cand["selected_sha256"] - for o in others - ), - "batch_bitwise": cand["batch_bitwise"], - "max_abs_vs_fp64": cand["max_abs_vs_fp64"], - "max_abs_vs": cand["max_abs_vs"], - "latency_us_tp4": cand["latency_us"], - } - return { - "kind": "qwen3_next_logprob_prior_art", - "rfc": 428, - "rl_kernel_commit": next(iter(commits)) if len(commits) == 1 else sorted(commits), - "tracked_tree_dirty": any(r["tracked_tree_dirty"] for r in (tp4, tp1, *others)), - "environment": tp4["environment"], - "tokens": tp4["tokens"], - "nccl_settings": [r["nccl_settings"] for r in (tp4, *others)], - "candidates": table, - } - - -def main() -> None: - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--checkpoint", type=Path) - parser.add_argument("--output", type=Path) - parser.add_argument("--merge", nargs="+", type=Path) - parser.add_argument("--out", type=Path) - args = parser.parse_args() - if args.merge: - report = merge(args.merge) - args.out.write_text(json.dumps(report, indent=2) + "\n") - for key, row in report["candidates"].items(): - print(key, {k: v for k, v in row.items() if k != "max_abs_vs"}) - else: - measure(args.checkpoint, args.output) - - -if __name__ == "__main__": - main() diff --git a/tools/validation/operators/check_forward_invariance.py b/tools/validation/operators/check_forward_invariance.py index 6830d8315..22fb43eea 100644 --- a/tools/validation/operators/check_forward_invariance.py +++ b/tools/validation/operators/check_forward_invariance.py @@ -20,7 +20,9 @@ from rl_engine.config.workload import load_manifest, workload_report # noqa: E402 from rl_engine.contracts.numerical import resolve_dtype_policy # noqa: E402 -from rl_engine.validation.models.qwen3_next_workload import validate_norm_dimensions # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_workload import ( # noqa: E402 + validate_norm_dimensions, +) from rl_engine.validation.operators import ( # noqa: E402 BackendProvenance, assert_forward_batch_invariant, diff --git a/tools/validation/operators/check_gradient_invariance.py b/tools/validation/operators/check_gradient_invariance.py index 178a5a4a6..b6555ad8b 100644 --- a/tools/validation/operators/check_gradient_invariance.py +++ b/tools/validation/operators/check_gradient_invariance.py @@ -20,7 +20,9 @@ from rl_engine.config.workload import load_manifest, workload_report # noqa: E402 from rl_engine.contracts.numerical import resolve_dtype_policy # noqa: E402 -from rl_engine.validation.models.qwen3_next_workload import validate_norm_dimensions # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_workload import ( # noqa: E402 + validate_norm_dimensions, +) from rl_engine.validation.operators import ( # noqa: E402 BackendProvenance, assert_gradient_batch_invariant, From a9d0ba8f38ccf80d460eef71938f0d1472d829b9 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 12 Oct 2026 11:21:51 +0000 Subject: [PATCH 31/68] fix(qwen3-next): hash validation artifacts on Python 3.10 Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/ci.yml | 3 ++- .../qwen3_next/native_recompute.py | 5 ++--- rl_engine/validation/common/file_digest.py | 16 ++++++++++++++ .../models/qwen3_next/qwen3_next_binary.py | 6 ++--- .../models/qwen3_next/qwen3_next_l2.py | 4 ++-- .../test_qwen3_next_native_recompute.py | 5 ++--- tests/validation/common/test_file_digest.py | 22 +++++++++++++++++++ .../models/qwen3_next/qwen3_next_runtime.py | 8 +++---- 8 files changed, 53 insertions(+), 16 deletions(-) create mode 100644 rl_engine/validation/common/file_digest.py create mode 100644 tests/validation/common/test_file_digest.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 77b0693ec..e560b48e8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -102,7 +102,8 @@ jobs: tests/models/qwen3_next/test_qwen3_next_run.py \ tests/models/qwen3_next/test_qwen3_next_evidence_redaction.py \ tests/models/qwen3_next/test_qwen3_next_bi_cases.py \ - tests/models/qwen3_next/test_qwen3_next_tp_reductions.py + tests/models/qwen3_next/test_qwen3_next_tp_reductions.py \ + tests/validation/common/test_file_digest.py - name: Run Cross-Configuration Contract Tests (CPU-safe) run: | diff --git a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py index 544788939..de6116401 100644 --- a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py +++ b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py @@ -3,7 +3,6 @@ """Independent native VIME full-model recomputation from frozen tokens only.""" -import hashlib import os import sys import traceback @@ -16,6 +15,7 @@ import torch.multiprocessing as mp from rl_engine.models.qwen3_next import qwen3_next_subnode as subnode +from rl_engine.validation.common.file_digest import file_sha256 from rl_engine.validation.common.tensor_identity import assert_tensor_bitwise_equal as exact from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( embedding_digest, @@ -328,8 +328,7 @@ def run_recompute_cases(cases): files = [staging / f"case-{index}-rank-{rank}.pt" for rank in range(4)] records = [] for rank, path in enumerate(files): - with path.open("rb") as stream: - digest = hashlib.file_digest(stream, "sha256").hexdigest() + digest = file_sha256(path) records.append( { "rank": rank, diff --git a/rl_engine/validation/common/file_digest.py b/rl_engine/validation/common/file_digest.py new file mode 100644 index 000000000..f8858a956 --- /dev/null +++ b/rl_engine/validation/common/file_digest.py @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Content identities for validation artifacts, including large tensor files.""" + +import hashlib +from pathlib import Path + + +def file_sha256(path: str | Path) -> str: + """Hash a file in bounded chunks on every supported Python version.""" + digest = hashlib.sha256() + with Path(path).open("rb") as stream: + for chunk in iter(lambda: stream.read(8 * 1024 * 1024), b""): + digest.update(chunk) + return digest.hexdigest() diff --git a/rl_engine/validation/models/qwen3_next/qwen3_next_binary.py b/rl_engine/validation/models/qwen3_next/qwen3_next_binary.py index 9038a5760..2a4991425 100644 --- a/rl_engine/validation/models/qwen3_next/qwen3_next_binary.py +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_binary.py @@ -3,16 +3,16 @@ """Bind each native model execution to its freshly built and archived CUDA binary.""" -import hashlib import json import os import shutil from pathlib import Path +from rl_engine.validation.common.file_digest import file_sha256 + def _digest(path): - with Path(path).open("rb") as stream: - return hashlib.file_digest(stream, "sha256").hexdigest() + return file_sha256(path) def archive_provider_binary(source, output, *, source_tree): diff --git a/rl_engine/validation/models/qwen3_next/qwen3_next_l2.py b/rl_engine/validation/models/qwen3_next/qwen3_next_l2.py index 5a30a6f84..e98ed6015 100644 --- a/rl_engine/validation/models/qwen3_next/qwen3_next_l2.py +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_l2.py @@ -20,6 +20,7 @@ import torch from rl_engine.models.qwen3_next.qwen3_next_subnode import SUBNODES, subnode_layout +from rl_engine.validation.common.file_digest import file_sha256 from rl_engine.validation.common.tensor_identity import tensor_bitwise_equal VOCAB_SIZE = 151936 @@ -898,8 +899,7 @@ def checked_artifact_path(record, *, artifact_root=None): path = path.resolve(strict=True) if artifact_root is not None and not path.is_relative_to(Path(artifact_root).resolve()): raise ValueError("Tensor artifact escaped its current attempt") - with path.open("rb") as stream: - digest = hashlib.file_digest(stream, "sha256").hexdigest() + digest = file_sha256(path) if digest != record["sha256"]: raise ValueError("Tensor artifact content digest changed") return path diff --git a/tests/models/qwen3_next/test_qwen3_next_native_recompute.py b/tests/models/qwen3_next/test_qwen3_next_native_recompute.py index 47b1157f1..cd0bb7558 100644 --- a/tests/models/qwen3_next/test_qwen3_next_native_recompute.py +++ b/tests/models/qwen3_next/test_qwen3_next_native_recompute.py @@ -52,11 +52,11 @@ def test_recompute_batch_loads_once_and_keeps_verifiable_original_rank_traces( tmp_path, monkeypatch ): import copy - import hashlib from rl_engine.integrations.orchestrators.vime.experiments.qwen3_next import ( native_recompute as recompute, ) + from rl_engine.validation.common.file_digest import file_sha256 from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( load_tensor_artifact, save_tensor_artifact, @@ -204,8 +204,7 @@ def spawn(target, *, args, nprocs, join): trace["local_logits"] = trace["local_logits"].clone() trace["local_logits"][0, 0, 0] = -0.0 save_tensor_artifact(record["path"], trace) - with open(record["path"], "rb") as stream: - record["sha256"] = hashlib.file_digest(stream, "sha256").hexdigest() + record["sha256"] = file_sha256(record["path"]) with pytest.raises(ValueError, match=message): validate_native_worker_traces(artifact, request, artifact_root=tmp_path) if "complete " in message: diff --git a/tests/validation/common/test_file_digest.py b/tests/validation/common/test_file_digest.py new file mode 100644 index 000000000..4131fd1b2 --- /dev/null +++ b/tests/validation/common/test_file_digest.py @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +import hashlib + +import pytest + +from rl_engine.validation.common.file_digest import file_sha256 + + +@pytest.mark.parametrize("size", [0, 17, 8 * 1024 * 1024 + 17]) +def test_artifact_sha256_without_python311_api(tmp_path, monkeypatch, size): + monkeypatch.delattr(hashlib, "file_digest", raising=False) + content = bytes(range(256)) * (size // 256) + bytes(range(size % 256)) + path = tmp_path / "artifact.bin" + path.write_bytes(content) + assert file_sha256(path) == hashlib.sha256(content).hexdigest() + + +def test_missing_artifact_is_not_a_valid_digest(tmp_path): + with pytest.raises(FileNotFoundError): + file_sha256(tmp_path / "missing.bin") diff --git a/tools/validation/models/qwen3_next/qwen3_next_runtime.py b/tools/validation/models/qwen3_next/qwen3_next_runtime.py index 685fc98fd..ee7db9f28 100644 --- a/tools/validation/models/qwen3_next/qwen3_next_runtime.py +++ b/tools/validation/models/qwen3_next/qwen3_next_runtime.py @@ -26,14 +26,15 @@ from pathlib import Path from urllib.parse import unquote, urlparse +from rl_engine.validation.common.file_digest import file_sha256 + # --------------------------------------------------------------------------- # # Environment identity # --------------------------------------------------------------------------- # def file_digest(path): - with Path(path).open("rb") as stream: - return hashlib.file_digest(stream, "sha256").hexdigest() + return file_sha256(path) def identity_digest(value): @@ -308,8 +309,7 @@ def atomic_json(path, payload): def digest(path): - with path.open("rb") as stream: - return hashlib.file_digest(stream, "sha256").hexdigest() + return file_sha256(path) _MEMORY_SUFFIXES = {"K": 1024, "M": 1024**2, "G": 1024**3, "T": 1024**4} From 722ef1ca5b045a8196a366b7f542f6cd0c163689 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 12 Oct 2026 19:10:26 +0000 Subject: [PATCH 32/68] docs(qwen3-next): record post-review B200 validation results Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../qwen3-next-distributed-logprob-b200.md | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md b/reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md index 004e4a6d6..e5fbc3d6e 100644 --- a/reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md @@ -45,3 +45,30 @@ python tools/validation/models/qwen3_next/qwen3_next_publish_parity_evidence.py cd artifacts zip -r qwen3-next-distributed-logprob-b200.zip qwen3-next-distributed-logprob-b200 ``` + +## Post-review rerun (2026-10-12) + +The relocated runners were executed from clean commit `b10b6616a24314a0656c36ac31131a8f9c0ee5ba` +on four B200 GPUs with torch 2.13.0+cu130, vLLM 0.30.0, Triton 3.7.1, +FlashInfer 0.6.18.post1 and NCCL 2.29.7, using the checkpoint revision above. +The commands above reproduce this run with the relocated paths. + +[Download new raw results, logs and provenance (ZIP)](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-pr549-evidence/qwen3-next-pr549-gpu-b10b6616.zip). +ZIP SHA-256: `5fb49bc151fc0224dc92128c0279fb58998435cfa033b62565d9a62957be0e57`. +All 153 internal checksums were verified. Source paths are redacted; measured +values, source commits and the imported extension's SHA-256 are retained. + +- All 233 mandatory operator cases passed with no skips; all four norm + forward/gradient gates passed. The TP reduction guard also passed. +- Vocabulary-parallel logprob tests: 31 passed; four ROCm-only cases skipped. + Two multiprocessing semaphore-cleanup warnings were recorded in the log. +- Across all 13 NCCL settings, all 17 deterministic points had one digest per + rank. TP4 selected-token logprob and LSE equaled TP1, and full-vocabulary + logits equaled gathered shards. The raw-NCCL negative control produced five + distinct digests per rank. Entropy remains non-invariant across TP degrees. + +The archive also contains the separate benchmark PR's rerun at `67a45169`. +This is operator and TP parity evidence; the full-model L2 and 97-cell batch +invariance runs were not repeated. Later commit `3927fad7` changes artifact +hashing for Python 3.10 compatibility and has CPU/CI coverage, not a separate +GPU measurement. From d8333f47c86894b3a07b450e7607c18480a2d447 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 12 Oct 2026 22:22:42 +0000 Subject: [PATCH 33/68] fix(validation): stream artifact SHA-256 on Python 3.10 Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/ci.yml | 2 +- .../qwen3_next/native_recompute.py | 5 ++--- rl_engine/validation/common/file_digest.py | 16 ++++++++++++++ .../validation/models/qwen3_next_binary.py | 6 ++--- rl_engine/validation/models/qwen3_next_l2.py | 4 ++-- .../test_qwen3_next_native_recompute.py | 5 ++--- tests/validation/common/test_file_digest.py | 22 +++++++++++++++++++ tools/validation/models/qwen3_next_runtime.py | 8 +++---- 8 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 rl_engine/validation/common/file_digest.py create mode 100644 tests/validation/common/test_file_digest.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 732966443..0500dd057 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -73,7 +73,7 @@ jobs: - name: Run Mocked Hardware Discovery Tests run: | - python -m pytest tests/runtime/test_dispatch.py -v + python -m pytest tests/runtime/test_dispatch.py tests/validation/common/test_file_digest.py -v PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python -m pytest tests/ops/attention/test_attention_correctness.py -q -rs python -m pytest tests/validation/operators/test_forward_invariance.py tests/contracts/test_tolerance_contract.py tests/validation/operators/test_ws1_workload.py tests/validation/operators/test_gradient_invariance.py tests/validation/operators/test_elementwise_inventory.py tests/validation/operators/test_four_judgment_matrix.py tests/validation/operators/test_op_checks.py tests/validation/operators/test_operator_inputs.py tests/benchmarks/test_profiler.py tests/validation/operators/test_kv_consistency.py tests/models/qwen3/test_ws1_qwen3_dense.py tests/validation/models/test_ws1_chain_integration.py tests/models/qwen3_next/test_qwen3_next_norm.py tests/models/qwen3_next/test_gdn_state_contract.py tests/validation/common/test_tensor_identity.py tests/models/qwen3_next/test_qwen3_next_forward_contract.py tests/models/qwen3_next/test_qwen3_next_tp_blocks.py tests/models/qwen3_next/test_qwen3_next_tp_gdn.py -q python -m pytest -q \ diff --git a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py index 92dad4a1a..cb868323e 100644 --- a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py +++ b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py @@ -3,7 +3,6 @@ """Independent native VIME full-model recomputation from frozen tokens only.""" -import hashlib import os import sys import traceback @@ -16,6 +15,7 @@ import torch.multiprocessing as mp from rl_engine.models.qwen3_next import qwen3_next_subnode as subnode +from rl_engine.validation.common.file_digest import file_sha256 from rl_engine.validation.common.tensor_identity import assert_tensor_bitwise_equal as exact from rl_engine.validation.models.qwen3_next_l2 import ( embedding_digest, @@ -299,8 +299,7 @@ def run_recompute_cases(cases): files = [staging / f"case-{index}-rank-{rank}.pt" for rank in range(4)] records = [] for rank, path in enumerate(files): - with path.open("rb") as stream: - digest = hashlib.file_digest(stream, "sha256").hexdigest() + digest = file_sha256(path) records.append( { "rank": rank, diff --git a/rl_engine/validation/common/file_digest.py b/rl_engine/validation/common/file_digest.py new file mode 100644 index 000000000..f8858a956 --- /dev/null +++ b/rl_engine/validation/common/file_digest.py @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Content identities for validation artifacts, including large tensor files.""" + +import hashlib +from pathlib import Path + + +def file_sha256(path: str | Path) -> str: + """Hash a file in bounded chunks on every supported Python version.""" + digest = hashlib.sha256() + with Path(path).open("rb") as stream: + for chunk in iter(lambda: stream.read(8 * 1024 * 1024), b""): + digest.update(chunk) + return digest.hexdigest() diff --git a/rl_engine/validation/models/qwen3_next_binary.py b/rl_engine/validation/models/qwen3_next_binary.py index 9038a5760..929c6a6c5 100644 --- a/rl_engine/validation/models/qwen3_next_binary.py +++ b/rl_engine/validation/models/qwen3_next_binary.py @@ -3,16 +3,16 @@ """Bind each native model execution to its freshly built and archived CUDA binary.""" -import hashlib import json import os import shutil from pathlib import Path +from rl_engine.validation.common.file_digest import file_sha256 + def _digest(path): - with Path(path).open("rb") as stream: - return hashlib.file_digest(stream, "sha256").hexdigest() + return file_sha256(Path(path)) def archive_provider_binary(source, output, *, source_tree): diff --git a/rl_engine/validation/models/qwen3_next_l2.py b/rl_engine/validation/models/qwen3_next_l2.py index 201836613..d20a76a88 100644 --- a/rl_engine/validation/models/qwen3_next_l2.py +++ b/rl_engine/validation/models/qwen3_next_l2.py @@ -20,6 +20,7 @@ import torch from rl_engine.models.qwen3_next.qwen3_next_subnode import SUBNODES, subnode_layout +from rl_engine.validation.common.file_digest import file_sha256 from rl_engine.validation.common.tensor_identity import tensor_bitwise_equal VOCAB_SIZE = 151936 @@ -896,8 +897,7 @@ def checked_artifact_path(record, *, artifact_root=None): path = path.resolve(strict=True) if artifact_root is not None and not path.is_relative_to(Path(artifact_root).resolve()): raise ValueError("Tensor artifact escaped its current attempt") - with path.open("rb") as stream: - digest = hashlib.file_digest(stream, "sha256").hexdigest() + digest = file_sha256(path) if digest != record["sha256"]: raise ValueError("Tensor artifact content digest changed") return path diff --git a/tests/models/qwen3_next/test_qwen3_next_native_recompute.py b/tests/models/qwen3_next/test_qwen3_next_native_recompute.py index e7830d4de..00e0dd3a8 100644 --- a/tests/models/qwen3_next/test_qwen3_next_native_recompute.py +++ b/tests/models/qwen3_next/test_qwen3_next_native_recompute.py @@ -11,6 +11,7 @@ ) from rl_engine.integrations.orchestrators.vime.providers.qwen3_next_vime_runtime import runtime_argv from rl_engine.models.qwen3_next.qwen3_next_subnode import SUBNODES, subnode_layout +from rl_engine.validation.common.file_digest import file_sha256 @pytest.mark.parametrize("batch", [1, 4]) @@ -52,7 +53,6 @@ def test_recompute_batch_loads_once_and_keeps_verifiable_original_rank_traces( tmp_path, monkeypatch ): import copy - import hashlib from rl_engine.integrations.orchestrators.vime.experiments.qwen3_next import ( native_recompute as recompute, @@ -204,8 +204,7 @@ def spawn(target, *, args, nprocs, join): trace["local_logits"] = trace["local_logits"].clone() trace["local_logits"][0, 0, 0] = -0.0 save_tensor_artifact(record["path"], trace) - with open(record["path"], "rb") as stream: - record["sha256"] = hashlib.file_digest(stream, "sha256").hexdigest() + record["sha256"] = file_sha256(record["path"]) with pytest.raises(ValueError, match=message): validate_native_worker_traces(artifact, request, artifact_root=tmp_path) if "complete " in message: diff --git a/tests/validation/common/test_file_digest.py b/tests/validation/common/test_file_digest.py new file mode 100644 index 000000000..4131fd1b2 --- /dev/null +++ b/tests/validation/common/test_file_digest.py @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +import hashlib + +import pytest + +from rl_engine.validation.common.file_digest import file_sha256 + + +@pytest.mark.parametrize("size", [0, 17, 8 * 1024 * 1024 + 17]) +def test_artifact_sha256_without_python311_api(tmp_path, monkeypatch, size): + monkeypatch.delattr(hashlib, "file_digest", raising=False) + content = bytes(range(256)) * (size // 256) + bytes(range(size % 256)) + path = tmp_path / "artifact.bin" + path.write_bytes(content) + assert file_sha256(path) == hashlib.sha256(content).hexdigest() + + +def test_missing_artifact_is_not_a_valid_digest(tmp_path): + with pytest.raises(FileNotFoundError): + file_sha256(tmp_path / "missing.bin") diff --git a/tools/validation/models/qwen3_next_runtime.py b/tools/validation/models/qwen3_next_runtime.py index f17495de3..8f801eae0 100644 --- a/tools/validation/models/qwen3_next_runtime.py +++ b/tools/validation/models/qwen3_next_runtime.py @@ -26,14 +26,15 @@ from pathlib import Path from urllib.parse import unquote, urlparse +from rl_engine.validation.common.file_digest import file_sha256 + # --------------------------------------------------------------------------- # # Environment identity # --------------------------------------------------------------------------- # def file_digest(path): - with Path(path).open("rb") as stream: - return hashlib.file_digest(stream, "sha256").hexdigest() + return file_sha256(Path(path)) def identity_digest(value): @@ -308,8 +309,7 @@ def atomic_json(path, payload): def digest(path): - with path.open("rb") as stream: - return hashlib.file_digest(stream, "sha256").hexdigest() + return file_sha256(path) _MEMORY_SUFFIXES = {"K": 1024, "M": 1024**2, "G": 1024**3, "T": 1024**4} From b5708e9ae3018b08b339e490fe0205c115d939d0 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 12 Oct 2026 22:23:23 +0000 Subject: [PATCH 34/68] fix(validation): retain batch invariance with Python 3.10 hashing Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/ci.yml | 2 +- .../qwen3_next/native_recompute.py | 5 ++--- rl_engine/validation/common/file_digest.py | 16 ++++++++++++++ .../validation/models/qwen3_next_binary.py | 6 ++--- rl_engine/validation/models/qwen3_next_l2.py | 4 ++-- .../test_qwen3_next_native_recompute.py | 5 ++--- tests/validation/common/test_file_digest.py | 22 +++++++++++++++++++ tools/validation/models/qwen3_next_runtime.py | 8 +++---- 8 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 rl_engine/validation/common/file_digest.py create mode 100644 tests/validation/common/test_file_digest.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aac61850d..8065db807 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -73,7 +73,7 @@ jobs: - name: Run Mocked Hardware Discovery Tests run: | - python -m pytest tests/runtime/test_dispatch.py -v + python -m pytest tests/runtime/test_dispatch.py tests/validation/common/test_file_digest.py -v PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 python -m pytest tests/ops/attention/test_attention_correctness.py -q -rs python -m pytest tests/validation/operators/test_forward_invariance.py tests/contracts/test_tolerance_contract.py tests/validation/operators/test_ws1_workload.py tests/validation/operators/test_gradient_invariance.py tests/validation/operators/test_elementwise_inventory.py tests/validation/operators/test_four_judgment_matrix.py tests/validation/operators/test_op_checks.py tests/validation/operators/test_operator_inputs.py tests/benchmarks/test_profiler.py tests/validation/operators/test_kv_consistency.py tests/models/qwen3/test_ws1_qwen3_dense.py tests/validation/models/test_ws1_chain_integration.py tests/models/qwen3_next/test_qwen3_next_norm.py tests/models/qwen3_next/test_gdn_state_contract.py tests/validation/common/test_tensor_identity.py tests/models/qwen3_next/test_qwen3_next_forward_contract.py tests/models/qwen3_next/test_qwen3_next_tp_blocks.py tests/models/qwen3_next/test_qwen3_next_tp_gdn.py -q python -m pytest -q \ diff --git a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py index 2bc4ede52..445c64be3 100644 --- a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py +++ b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py @@ -3,7 +3,6 @@ """Independent native VIME full-model recomputation from frozen tokens only.""" -import hashlib import os import sys import traceback @@ -16,6 +15,7 @@ import torch.multiprocessing as mp from rl_engine.models.qwen3_next import qwen3_next_subnode as subnode +from rl_engine.validation.common.file_digest import file_sha256 from rl_engine.validation.common.tensor_identity import assert_tensor_bitwise_equal as exact from rl_engine.validation.models.qwen3_next_l2 import ( embedding_digest, @@ -328,8 +328,7 @@ def run_recompute_cases(cases): files = [staging / f"case-{index}-rank-{rank}.pt" for rank in range(4)] records = [] for rank, path in enumerate(files): - with path.open("rb") as stream: - digest = hashlib.file_digest(stream, "sha256").hexdigest() + digest = file_sha256(path) records.append( { "rank": rank, diff --git a/rl_engine/validation/common/file_digest.py b/rl_engine/validation/common/file_digest.py new file mode 100644 index 000000000..f8858a956 --- /dev/null +++ b/rl_engine/validation/common/file_digest.py @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""Content identities for validation artifacts, including large tensor files.""" + +import hashlib +from pathlib import Path + + +def file_sha256(path: str | Path) -> str: + """Hash a file in bounded chunks on every supported Python version.""" + digest = hashlib.sha256() + with Path(path).open("rb") as stream: + for chunk in iter(lambda: stream.read(8 * 1024 * 1024), b""): + digest.update(chunk) + return digest.hexdigest() diff --git a/rl_engine/validation/models/qwen3_next_binary.py b/rl_engine/validation/models/qwen3_next_binary.py index 9038a5760..929c6a6c5 100644 --- a/rl_engine/validation/models/qwen3_next_binary.py +++ b/rl_engine/validation/models/qwen3_next_binary.py @@ -3,16 +3,16 @@ """Bind each native model execution to its freshly built and archived CUDA binary.""" -import hashlib import json import os import shutil from pathlib import Path +from rl_engine.validation.common.file_digest import file_sha256 + def _digest(path): - with Path(path).open("rb") as stream: - return hashlib.file_digest(stream, "sha256").hexdigest() + return file_sha256(Path(path)) def archive_provider_binary(source, output, *, source_tree): diff --git a/rl_engine/validation/models/qwen3_next_l2.py b/rl_engine/validation/models/qwen3_next_l2.py index 462200e28..d1f5a4fa5 100644 --- a/rl_engine/validation/models/qwen3_next_l2.py +++ b/rl_engine/validation/models/qwen3_next_l2.py @@ -20,6 +20,7 @@ import torch from rl_engine.models.qwen3_next.qwen3_next_subnode import SUBNODES, subnode_layout +from rl_engine.validation.common.file_digest import file_sha256 from rl_engine.validation.common.tensor_identity import tensor_bitwise_equal VOCAB_SIZE = 151936 @@ -898,8 +899,7 @@ def checked_artifact_path(record, *, artifact_root=None): path = path.resolve(strict=True) if artifact_root is not None and not path.is_relative_to(Path(artifact_root).resolve()): raise ValueError("Tensor artifact escaped its current attempt") - with path.open("rb") as stream: - digest = hashlib.file_digest(stream, "sha256").hexdigest() + digest = file_sha256(path) if digest != record["sha256"]: raise ValueError("Tensor artifact content digest changed") return path diff --git a/tests/models/qwen3_next/test_qwen3_next_native_recompute.py b/tests/models/qwen3_next/test_qwen3_next_native_recompute.py index e7830d4de..00e0dd3a8 100644 --- a/tests/models/qwen3_next/test_qwen3_next_native_recompute.py +++ b/tests/models/qwen3_next/test_qwen3_next_native_recompute.py @@ -11,6 +11,7 @@ ) from rl_engine.integrations.orchestrators.vime.providers.qwen3_next_vime_runtime import runtime_argv from rl_engine.models.qwen3_next.qwen3_next_subnode import SUBNODES, subnode_layout +from rl_engine.validation.common.file_digest import file_sha256 @pytest.mark.parametrize("batch", [1, 4]) @@ -52,7 +53,6 @@ def test_recompute_batch_loads_once_and_keeps_verifiable_original_rank_traces( tmp_path, monkeypatch ): import copy - import hashlib from rl_engine.integrations.orchestrators.vime.experiments.qwen3_next import ( native_recompute as recompute, @@ -204,8 +204,7 @@ def spawn(target, *, args, nprocs, join): trace["local_logits"] = trace["local_logits"].clone() trace["local_logits"][0, 0, 0] = -0.0 save_tensor_artifact(record["path"], trace) - with open(record["path"], "rb") as stream: - record["sha256"] = hashlib.file_digest(stream, "sha256").hexdigest() + record["sha256"] = file_sha256(record["path"]) with pytest.raises(ValueError, match=message): validate_native_worker_traces(artifact, request, artifact_root=tmp_path) if "complete " in message: diff --git a/tests/validation/common/test_file_digest.py b/tests/validation/common/test_file_digest.py new file mode 100644 index 000000000..4131fd1b2 --- /dev/null +++ b/tests/validation/common/test_file_digest.py @@ -0,0 +1,22 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +import hashlib + +import pytest + +from rl_engine.validation.common.file_digest import file_sha256 + + +@pytest.mark.parametrize("size", [0, 17, 8 * 1024 * 1024 + 17]) +def test_artifact_sha256_without_python311_api(tmp_path, monkeypatch, size): + monkeypatch.delattr(hashlib, "file_digest", raising=False) + content = bytes(range(256)) * (size // 256) + bytes(range(size % 256)) + path = tmp_path / "artifact.bin" + path.write_bytes(content) + assert file_sha256(path) == hashlib.sha256(content).hexdigest() + + +def test_missing_artifact_is_not_a_valid_digest(tmp_path): + with pytest.raises(FileNotFoundError): + file_sha256(tmp_path / "missing.bin") diff --git a/tools/validation/models/qwen3_next_runtime.py b/tools/validation/models/qwen3_next_runtime.py index ebaafa5f9..bc79d079d 100644 --- a/tools/validation/models/qwen3_next_runtime.py +++ b/tools/validation/models/qwen3_next_runtime.py @@ -26,14 +26,15 @@ from pathlib import Path from urllib.parse import unquote, urlparse +from rl_engine.validation.common.file_digest import file_sha256 + # --------------------------------------------------------------------------- # # Environment identity # --------------------------------------------------------------------------- # def file_digest(path): - with Path(path).open("rb") as stream: - return hashlib.file_digest(stream, "sha256").hexdigest() + return file_sha256(Path(path)) def identity_digest(value): @@ -308,8 +309,7 @@ def atomic_json(path, payload): def digest(path): - with path.open("rb") as stream: - return hashlib.file_digest(stream, "sha256").hexdigest() + return file_sha256(path) _MEMORY_SUFFIXES = {"K": 1024, "M": 1024**2, "G": 1024**3, "T": 1024**4} From 7756969eb290f9edf8a9cb66b2dc0af0a15650cf Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 12 Oct 2026 23:21:03 +0000 Subject: [PATCH 35/68] fix(validation): keep standalone evidence runtime free of torch imports Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- tests/validation/common/test_file_digest.py | 27 +++++++++++++++++++ tools/validation/models/qwen3_next_runtime.py | 11 +++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/tests/validation/common/test_file_digest.py b/tests/validation/common/test_file_digest.py index 4131fd1b2..e9dae4a2b 100644 --- a/tests/validation/common/test_file_digest.py +++ b/tests/validation/common/test_file_digest.py @@ -20,3 +20,30 @@ def test_artifact_sha256_without_python311_api(tmp_path, monkeypatch, size): def test_missing_artifact_is_not_a_valid_digest(tmp_path): with pytest.raises(FileNotFoundError): file_sha256(tmp_path / "missing.bin") + + +def test_runtime_digest_does_not_import_torch(tmp_path): + import subprocess + import sys + from pathlib import Path + + runtime = Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next_runtime.py" + artifact = tmp_path / "artifact.bin" + artifact.write_bytes(b"standalone evidence runtime") + program = """ +import importlib.util, sys +spec = importlib.util.spec_from_file_location('runtime', sys.argv[1]) +runtime = importlib.util.module_from_spec(spec) +spec.loader.exec_module(runtime) +assert 'torch' not in sys.modules +assert runtime.file_digest(sys.argv[2]) == runtime.digest(sys.argv[2]) +print(runtime.digest(sys.argv[2])) +""" + result = subprocess.run( + [sys.executable, "-I", "-c", program, str(runtime), str(artifact)], + check=True, + text=True, + capture_output=True, + timeout=30, + ) + assert result.stdout.strip() == hashlib.sha256(artifact.read_bytes()).hexdigest() diff --git a/tools/validation/models/qwen3_next_runtime.py b/tools/validation/models/qwen3_next_runtime.py index 8f801eae0..82191e5b0 100644 --- a/tools/validation/models/qwen3_next_runtime.py +++ b/tools/validation/models/qwen3_next_runtime.py @@ -26,15 +26,18 @@ from pathlib import Path from urllib.parse import unquote, urlparse -from rl_engine.validation.common.file_digest import file_sha256 - # --------------------------------------------------------------------------- # # Environment identity # --------------------------------------------------------------------------- # def file_digest(path): - return file_sha256(Path(path)) + # Keep this launcher/storage utility stdlib-only, including before torch is installed. + value = hashlib.sha256() + with Path(path).open("rb") as stream: + for block in iter(lambda: stream.read(8 * 1024 * 1024), b""): + value.update(block) + return value.hexdigest() def identity_digest(value): @@ -309,7 +312,7 @@ def atomic_json(path, payload): def digest(path): - return file_sha256(path) + return file_digest(path) _MEMORY_SUFFIXES = {"K": 1024, "M": 1024**2, "G": 1024**3, "T": 1024**4} From 6c7c49c671f49118e30d077247d96c2ebfce2296 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 12 Oct 2026 23:21:03 +0000 Subject: [PATCH 36/68] fix(validation): keep standalone evidence runtime free of torch imports Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- tests/validation/common/test_file_digest.py | 27 +++++++++++++++++++ tools/validation/models/qwen3_next_runtime.py | 11 +++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/tests/validation/common/test_file_digest.py b/tests/validation/common/test_file_digest.py index 4131fd1b2..e9dae4a2b 100644 --- a/tests/validation/common/test_file_digest.py +++ b/tests/validation/common/test_file_digest.py @@ -20,3 +20,30 @@ def test_artifact_sha256_without_python311_api(tmp_path, monkeypatch, size): def test_missing_artifact_is_not_a_valid_digest(tmp_path): with pytest.raises(FileNotFoundError): file_sha256(tmp_path / "missing.bin") + + +def test_runtime_digest_does_not_import_torch(tmp_path): + import subprocess + import sys + from pathlib import Path + + runtime = Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next_runtime.py" + artifact = tmp_path / "artifact.bin" + artifact.write_bytes(b"standalone evidence runtime") + program = """ +import importlib.util, sys +spec = importlib.util.spec_from_file_location('runtime', sys.argv[1]) +runtime = importlib.util.module_from_spec(spec) +spec.loader.exec_module(runtime) +assert 'torch' not in sys.modules +assert runtime.file_digest(sys.argv[2]) == runtime.digest(sys.argv[2]) +print(runtime.digest(sys.argv[2])) +""" + result = subprocess.run( + [sys.executable, "-I", "-c", program, str(runtime), str(artifact)], + check=True, + text=True, + capture_output=True, + timeout=30, + ) + assert result.stdout.strip() == hashlib.sha256(artifact.read_bytes()).hexdigest() diff --git a/tools/validation/models/qwen3_next_runtime.py b/tools/validation/models/qwen3_next_runtime.py index bc79d079d..32fb73a02 100644 --- a/tools/validation/models/qwen3_next_runtime.py +++ b/tools/validation/models/qwen3_next_runtime.py @@ -26,15 +26,18 @@ from pathlib import Path from urllib.parse import unquote, urlparse -from rl_engine.validation.common.file_digest import file_sha256 - # --------------------------------------------------------------------------- # # Environment identity # --------------------------------------------------------------------------- # def file_digest(path): - return file_sha256(Path(path)) + # Keep this launcher/storage utility stdlib-only, including before torch is installed. + value = hashlib.sha256() + with Path(path).open("rb") as stream: + for block in iter(lambda: stream.read(8 * 1024 * 1024), b""): + value.update(block) + return value.hexdigest() def identity_digest(value): @@ -309,7 +312,7 @@ def atomic_json(path, payload): def digest(path): - return file_sha256(path) + return file_digest(path) _MEMORY_SUFFIXES = {"K": 1024, "M": 1024**2, "G": 1024**3, "T": 1024**4} From 5a5aed4c15618cd45198aa32fa4987d97a0d8664 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Tue, 13 Oct 2026 09:10:52 +0000 Subject: [PATCH 37/68] fix(ci): resolve the repository root from ci/scripts/ in the Qwen3-Next gate scripts The scripts moved from ci/ to ci/scripts/ with the repository refactor but still resolved the root one directory up, so the preflight operator gates looked for the runner under ci/. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- ci/scripts/run_qwen3_next_chain_gate.sh | 2 +- ci/scripts/run_qwen3_next_operator_gates.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/scripts/run_qwen3_next_chain_gate.sh b/ci/scripts/run_qwen3_next_chain_gate.sh index 8c042ea64..9f0d7c2df 100755 --- a/ci/scripts/run_qwen3_next_chain_gate.sh +++ b/ci/scripts/run_qwen3_next_chain_gate.sh @@ -3,7 +3,7 @@ # Qwen3-Next chain-report adapter checks; with a report path, also validate that report. # bash ci/scripts/run_qwen3_next_chain_gate.sh [chain-report.json] set -euo pipefail -ROOT="$(cd "$(dirname "$0")/.." && pwd)" +ROOT="$(cd "$(dirname "$0")/../.." && pwd)" cd "$ROOT" PY="${PY:-python3}" export PYTHONPATH="$ROOT${PYTHONPATH:+:$PYTHONPATH}" diff --git a/ci/scripts/run_qwen3_next_operator_gates.sh b/ci/scripts/run_qwen3_next_operator_gates.sh index ceba42dd5..d744fc2b9 100644 --- a/ci/scripts/run_qwen3_next_operator_gates.sh +++ b/ci/scripts/run_qwen3_next_operator_gates.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # Required operator acceptance and diagnostics use separate declared case sets. set -euo pipefail -ROOT="$(cd "$(dirname "$0")/.." && pwd)" +ROOT="$(cd "$(dirname "$0")/../.." && pwd)" cd "$ROOT" PY="${PY:-python3}" OUT="${QWEN3_NEXT_EVIDENCE_DIR:?Set QWEN3_NEXT_EVIDENCE_DIR to an artifact directory}" From 25ded88fce972109cbd4018093bd00186cf81946 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Tue, 13 Oct 2026 09:10:54 +0000 Subject: [PATCH 38/68] fix(ci): resolve the repository root from ci/scripts/ in the Qwen3-Next gate scripts The scripts moved from ci/ to ci/scripts/ with the repository refactor but still resolved the root one directory up, so the preflight operator gates looked for the runner under ci/. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- ci/scripts/run_qwen3_next_chain_gate.sh | 2 +- ci/scripts/run_qwen3_next_operator_gates.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/scripts/run_qwen3_next_chain_gate.sh b/ci/scripts/run_qwen3_next_chain_gate.sh index 8c042ea64..9f0d7c2df 100755 --- a/ci/scripts/run_qwen3_next_chain_gate.sh +++ b/ci/scripts/run_qwen3_next_chain_gate.sh @@ -3,7 +3,7 @@ # Qwen3-Next chain-report adapter checks; with a report path, also validate that report. # bash ci/scripts/run_qwen3_next_chain_gate.sh [chain-report.json] set -euo pipefail -ROOT="$(cd "$(dirname "$0")/.." && pwd)" +ROOT="$(cd "$(dirname "$0")/../.." && pwd)" cd "$ROOT" PY="${PY:-python3}" export PYTHONPATH="$ROOT${PYTHONPATH:+:$PYTHONPATH}" diff --git a/ci/scripts/run_qwen3_next_operator_gates.sh b/ci/scripts/run_qwen3_next_operator_gates.sh index ceba42dd5..d744fc2b9 100644 --- a/ci/scripts/run_qwen3_next_operator_gates.sh +++ b/ci/scripts/run_qwen3_next_operator_gates.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # Required operator acceptance and diagnostics use separate declared case sets. set -euo pipefail -ROOT="$(cd "$(dirname "$0")/.." && pwd)" +ROOT="$(cd "$(dirname "$0")/../.." && pwd)" cd "$ROOT" PY="${PY:-python3}" OUT="${QWEN3_NEXT_EVIDENCE_DIR:?Set QWEN3_NEXT_EVIDENCE_DIR to an artifact directory}" From ffa0d1d5f56c014892ee790ae986aeeac063891c Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Tue, 13 Oct 2026 09:10:56 +0000 Subject: [PATCH 39/68] fix(ci): resolve the repository root from ci/scripts/ in the Qwen3-Next gate scripts The scripts moved from ci/ to ci/scripts/ with the repository refactor but still resolved the root one directory up, so the preflight operator gates looked for the runner under ci/. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- ci/scripts/run_qwen3_next_chain_gate.sh | 2 +- ci/scripts/run_qwen3_next_operator_gates.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/scripts/run_qwen3_next_chain_gate.sh b/ci/scripts/run_qwen3_next_chain_gate.sh index 8407c755e..a2bdba2f9 100755 --- a/ci/scripts/run_qwen3_next_chain_gate.sh +++ b/ci/scripts/run_qwen3_next_chain_gate.sh @@ -3,7 +3,7 @@ # Qwen3-Next chain-report adapter checks; with a report path, also validate that report. # bash ci/scripts/run_qwen3_next_chain_gate.sh [chain-report.json] set -euo pipefail -ROOT="$(cd "$(dirname "$0")/.." && pwd)" +ROOT="$(cd "$(dirname "$0")/../.." && pwd)" cd "$ROOT" PY="${PY:-python3}" export PYTHONPATH="$ROOT${PYTHONPATH:+:$PYTHONPATH}" diff --git a/ci/scripts/run_qwen3_next_operator_gates.sh b/ci/scripts/run_qwen3_next_operator_gates.sh index fc8f48f8d..7dd37f35c 100644 --- a/ci/scripts/run_qwen3_next_operator_gates.sh +++ b/ci/scripts/run_qwen3_next_operator_gates.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # Required operator acceptance and diagnostics use separate declared case sets. set -euo pipefail -ROOT="$(cd "$(dirname "$0")/.." && pwd)" +ROOT="$(cd "$(dirname "$0")/../.." && pwd)" cd "$ROOT" PY="${PY:-python3}" OUT="${QWEN3_NEXT_EVIDENCE_DIR:?Set QWEN3_NEXT_EVIDENCE_DIR to an artifact directory}" From 47468366b881d1d149ecc4bbf6aa71188307ac9d Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Tue, 13 Oct 2026 13:48:44 +0000 Subject: [PATCH 40/68] test(qwen3-next): drop the duplicate file_sha256 import left by the merge Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- tests/models/qwen3_next/test_qwen3_next_native_recompute.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/models/qwen3_next/test_qwen3_next_native_recompute.py b/tests/models/qwen3_next/test_qwen3_next_native_recompute.py index 78fdb0929..cd0bb7558 100644 --- a/tests/models/qwen3_next/test_qwen3_next_native_recompute.py +++ b/tests/models/qwen3_next/test_qwen3_next_native_recompute.py @@ -11,7 +11,6 @@ ) from rl_engine.integrations.orchestrators.vime.providers.qwen3_next_vime_runtime import runtime_argv from rl_engine.models.qwen3_next.qwen3_next_subnode import SUBNODES, subnode_layout -from rl_engine.validation.common.file_digest import file_sha256 @pytest.mark.parametrize("batch", [1, 4]) From 57216db60948b2381abea0005045153fa1c97e50 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Wed, 14 Oct 2026 20:24:12 +0000 Subject: [PATCH 41/68] refactor(qwen3-next): MoE route branch to the reviewed layout Per the layout review on #549: - the TP4 MoE gate moves to tools/validation/models/qwen3_next/; - the prior-art runner and its plot move to the Qwen3-Next benchmark PR (benchmarks/models/qwen3_next/); - the committed evidence directory becomes a ZIP release attachment with a short summary in reports/experiments/qwen3_next/ (download link, SHA-256, tested commits, commands); the bytes are unchanged. The GPU workflow now also triggers on files under the qwen3_next/ validation directories. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/qwen3-next-provider-gpu.yml | 2 + .../operators/qwen3-next-moe-route-combine.md | 15 +- .../qwen3_next/qwen3-next-moe-route-b200.md | 57 ++ .../models/qwen3_next_moe_prior_art.py | 688 ------------------ .../models/plot_qwen3_next_moe_prior_art.py | 162 ----- .../validation/models/qwen3_next/__init__.py | 2 + .../qwen3_next_tp_moe_check.py | 6 +- .../models/qwen3_next_moe_prior_art.py | 116 --- 8 files changed, 73 insertions(+), 975 deletions(-) create mode 100644 reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md delete mode 100644 rl_engine/validation/models/qwen3_next_moe_prior_art.py delete mode 100644 tools/validation/models/plot_qwen3_next_moe_prior_art.py create mode 100644 tools/validation/models/qwen3_next/__init__.py rename tools/validation/models/{ => qwen3_next}/qwen3_next_tp_moe_check.py (97%) delete mode 100644 tools/validation/models/qwen3_next_moe_prior_art.py diff --git a/.github/workflows/qwen3-next-provider-gpu.yml b/.github/workflows/qwen3-next-provider-gpu.yml index 84b7a7b0e..46a42a2b3 100644 --- a/.github/workflows/qwen3-next-provider-gpu.yml +++ b/.github/workflows/qwen3-next-provider-gpu.yml @@ -25,6 +25,8 @@ on: - "rl_engine/integrations/engines/train/vllm/qwen3_next*" - "rl_engine/validation/models/qwen3_next*" - "tools/validation/models/*qwen3_next*" + - "rl_engine/validation/models/qwen3_next/**" + - "tools/validation/models/qwen3_next/**" - "tests/models/qwen3_next/**" - "tests/models/qwen3_next/check_gdn_recurrent_golden.py" - ".github/workflows/qwen3-next-provider-gpu.yml" diff --git a/docs/operators/qwen3-next-moe-route-combine.md b/docs/operators/qwen3-next-moe-route-combine.md index 07804b8bc..06d84e879 100644 --- a/docs/operators/qwen3-next-moe-route-combine.md +++ b/docs/operators/qwen3-next-moe-route-combine.md @@ -84,12 +84,15 @@ with no change in bits. ## Results -Measured on B200 with `tools/validation/models/qwen3_next_moe_prior_art.py` and the TP4 gate -`tools/validation/models/qwen3_next_tp_moe_check.py`; the reports, the figure and the exact -commands are in -[`docs/usage/evidence/qwen3-next-moe-route-b200/`](../usage/evidence/qwen3-next-moe-route-b200/README.md). +Measured on B200 with the prior-art runner of the Qwen3-Next benchmark PR +(`benchmarks/models/qwen3_next/qwen3_next_moe_prior_art.py`) and this PR's TP4 gate +`tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py`. The summary, tested +commits and commands are in +[`reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md`](https://github.com/fusheng-ji/RL-Kernel/blob/feat/ws1-qwen3-next-moe-route-combine/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md); +the raw reports and the figure are a +[ZIP release attachment](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-rfc428-evidence/qwen3-next-moe-route-b200.zip). -![Qwen3-Next routed MoE prior art](../usage/evidence/qwen3-next-moe-route-b200/figure.png) +![Qwen3-Next routed MoE prior art](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-rfc428-evidence/qwen3-next-moe-route-b200.png) TP1 shape (H=2048, 512 experts, top-10, width 512), random weights, BF16: @@ -134,7 +137,7 @@ shared expert + all-reduce) takes 1.9-2.4 ms at 8-1024 tokens. | `tests/models/qwen3_next/test_qwen3_next_tp_blocks.py` | CPU | HF shard/assemble round trips, replica drift rejection, TP4 parameter ownership | | `tests/validation/common/test_tensor_identity.py` | CPU | raw-bit identity (signed zero, NaN/Inf, dtype) | | `tests/models/qwen3_next/check_qwen3_next_forward.py` | CUDA + vLLM | GEMM batch/chunk/reorder, route ties, combine order, MoE VJP vs FP64, route and output batch invariance, grouped == per-expert bitwise, fail-closed TD path | -| `tools/validation/models/qwen3_next_tp_moe_check.py` | 4 x CUDA | real layer-0 weights: HF round trip, replicated routes, chunk/reorder, training forward, gradients | +| `tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py` | 4 x CUDA | real layer-0 weights: HF round trip, replicated routes, chunk/reorder, training forward, gradients | The `check_` file imports vLLM, so it runs in the `Qwen3-Next-provider-GPU` workflow rather than the default collection. diff --git a/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md b/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md new file mode 100644 index 000000000..a7d90d059 --- /dev/null +++ b/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md @@ -0,0 +1,57 @@ +# Qwen3-Next routed MoE: prior art and TP4 gate (B200) + +[Download raw results (ZIP)](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-rfc428-evidence/qwen3-next-moe-route-b200.zip). + +ZIP SHA-256: `9fcc704291bf697274edfe16d6958ca799134008acd9d5f452767b9a92758cb0`. + +The archive holds `report.json`, `figure.png`, the TP4 gate's +`tp4-moe/rank-{0..3}.json` and the original README, byte for byte as measured, +plus `SHA256SUMS`. The prior art was measured at commit `c58816b` from a clean +clone on one B200 (torch 2.13.0+cu130, Triton 3.7.1); the TP4 gate at +`2e950f3`, whose MoE code is identical, on four B200. The prior art uses +random weights (scale 0.02) at the TP1 shape (H=2048, 512 experts, top-10, +width 512); the TP4 gate reads layer 0 of the official +`Qwen/Qwen3-Next-80B-A3B-Instruct` revision +`9c7f2fbe84465e40164a94cc16cd30b6999b0cc7`. + +```bash +# prior art (benchmark PR, benchmarks/models/qwen3_next/), training environment +TRITON_F32_DEFAULT=ieee python benchmarks/models/qwen3_next/qwen3_next_moe_prior_art.py \ + --only rl_kernel_cuda,hf_transformers,vllm_bi0,vllm_bi1,flashinfer_cutlass,megatron_te --out main.json +# SGLang environment +TRITON_F32_DEFAULT=ieee python benchmarks/models/qwen3_next/qwen3_next_moe_prior_art.py \ + --only sglang_triton,sglang_deterministic --merge main.json --out report.json +python benchmarks/models/qwen3_next/plot_qwen3_next_moe_prior_art.py report.json +# TP4 gate on real layer-0 weights (this PR) +TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py \ + --checkpoint --output tp4-moe +``` + +At `c58816b` / `2e950f3` the runners lived in `tools/validation/models/` and +`rl_engine/validation/models/`; the commands above use the current layout. + +| Candidate | Forward batch invariant | Backward batch invariant | Different expert set (of 256 tokens) | Max abs vs FP64 | Forward, 1024 tokens | +| --- | --- | --- | --- | --- | --- | +| **RL-Kernel `shared_moe`** | **yes** | **yes** | **0** | 2.1e-3 | 2.04 ms | +| HF transformers `Qwen3NextExperts` (eager) | yes | no (1024) | 11 | 1.1e-1 | 89.3 ms | +| vLLM `fused_experts`, `VLLM_BATCH_INVARIANT=0` | no (256, 1024) | n/a (inference) | 11 | 1.1e-1 | 0.87 ms | +| vLLM `fused_experts`, `VLLM_BATCH_INVARIANT=1` | yes | n/a (inference) | 11 | 1.1e-1 | 0.87 ms | +| FlashInfer `cutlass_fused_moe` | no (256, 1024) | n/a (inference) | 11 | 1.1e-1 | 0.90 ms | +| Megatron-core `MoELayer` + TE grouped GEMM | no (1024) | no (1024) | 0 | 2.2e-3 | 11.7 ms | +| SGLang `fused_moe` (Triton), default | no (256, 1024) | n/a (inference) | 11 | 1.1e-1 | 0.83 ms | +| SGLang `fused_moe`, deterministic inference | yes | n/a (inference) | 11 | 1.1e-1 | 0.84 ms | + +- Batch invariance: 8 probe tokens computed alone and placed first and last in + batches of 16, 64, 256 and 1024 tokens; the token counts in parentheses are + the batches that differ. +- Accuracy: 256 tokens against the HF formula in FP64 with FP64 routing. The + candidates with a BF16 router pick a different expert set for 11 of 256 + tokens, which dominates their error. +- SGLang's compiled `sgl_kernel` 0.3.21 cannot load next to torch 2.13, so its + two kernels on this path are SGLang's own replacements (Triton + `moe_sum_reduce`, JIT `moe_align_block_size`); `environments` in + `report.json` records both Python environments. +- TP4 gate (`tp4-moe/rank-*.json`): HF round trip, replicated routes, chunk and + reorder invariance, training forward and gradients pass on every rank. + +Site paths, host names and the scheduler do not appear in any file. diff --git a/rl_engine/validation/models/qwen3_next_moe_prior_art.py b/rl_engine/validation/models/qwen3_next_moe_prior_art.py deleted file mode 100644 index 7c0cc33c9..000000000 --- a/rl_engine/validation/models/qwen3_next_moe_prior_art.py +++ /dev/null @@ -1,688 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 -# Copyright (c) 2026 RL-Kernel Contributors - -"""Prior-art comparison for the Qwen3-Next routed MoE (RFC #428 ``moe_route_combine_contract``). - -Every candidate computes the routed half of ``Qwen3NextSparseMoeBlock`` at the -official TP1 shape (H=2048, 512 experts, top-10, expert width 512): router -logits, top-10 selection with renormalised weights, the selected SwiGLU -experts and the weighted combine. The shared expert is left out because every -candidate evaluates it as the same dense MLP. - -For each candidate the report records - -* **routing batch invariance** - the expert ids and weights of a fixed block - of probe tokens computed alone, and embedded first and last in larger - batches, must be bitwise equal; -* **output batch invariance** - the same for the routed output; -* **dx / dW batch invariance** (candidates with a backward) - the probe rows' - input gradient must not change when unrelated rows join the batch, and the - expert weight gradient must not change when rows whose output gradient is - zero join it (mathematically neither can); -* **repeatability** - two identical calls are bitwise equal; -* **accuracy** - error against an FP64 evaluation of the HF formula with FP64 - routing, plus the fraction of tokens whose selected expert set differs; -* **performance** - median CUDA-event latency, candidates interleaved. - -A candidate that cannot be imported or launched in the pinned environment is -recorded as ``unavailable`` with the reason instead of failing the run. -""" - -from __future__ import annotations - -import os -import statistics -from contextlib import contextmanager -from typing import Any, Callable - -import torch -import torch.nn.functional as F - -HIDDEN = 2048 -EXPERTS = 512 -TOPK = 10 -WIDTH = 512 -PROBE = 8 -BI_SIZES = (16, 64, 256, 1024) -PERF_TOKENS = (1, 8, 64, 256, 1024, 4096) -BACKWARD_TOKENS = (64, 1024) - - -# --------------------------------------------------------------------------- # -# Measurement helpers -# --------------------------------------------------------------------------- # - - -def _sample_us(fn: Callable[[], Any]) -> float: - torch.cuda.synchronize() - start = torch.cuda.Event(enable_timing=True) - end = torch.cuda.Event(enable_timing=True) - start.record() - fn() - end.record() - torch.cuda.synchronize() - return start.elapsed_time(end) * 1000.0 - - -def time_interleaved(calls: dict[str, Callable[..., Any]], warmup: int = 5, iters: int = 30): - """Median latency per candidate; the order alternates so none always runs first.""" - keys = list(calls) - orders = (keys, list(reversed(keys))) - for i in range(warmup): - for key in orders[i % 2]: - calls[key]() - samples: dict[str, list[float]] = {key: [] for key in keys} - for i in range(iters): - for key in orders[i % 2]: - samples[key].append(_sample_us(calls[key])) - return {key: statistics.median(values) for key, values in samples.items()} - - -def _bitwise(a: torch.Tensor, b: torch.Tensor) -> bool: - return a.shape == b.shape and a.dtype == b.dtype and torch.equal(a, b) - - -def _rel(value: torch.Tensor, reference: torch.Tensor) -> float: - reference = reference.double() - return float((value.double() - reference).norm() / reference.norm().clamp_min(1e-300)) - - -def _guard(key: str, factory: Callable[[], dict[str, Any]]) -> dict[str, Any]: - try: - return {"key": key, **factory()} - except Exception as exc: # noqa: BLE001 - record and continue - return {"key": key, "unavailable": f"{type(exc).__name__}: {str(exc)[:300]}"} - - -@contextmanager -def _env(name: str, value: str | None): - old = os.environ.get(name) - if value is None: - os.environ.pop(name, None) - else: - os.environ[name] = value - try: - yield - finally: - if old is None: - os.environ.pop(name, None) - else: - os.environ[name] = old - - -# --------------------------------------------------------------------------- # -# Inputs and the FP64 reference -# --------------------------------------------------------------------------- # - - -def make_weights(seed: int = 0) -> dict[str, torch.Tensor]: - g = torch.Generator(device="cuda").manual_seed(seed) - - def randn(*shape, scale): - return (torch.randn(shape, device="cuda", generator=g) * scale).to(torch.bfloat16) - - return { - "router": randn(EXPERTS, HIDDEN, scale=0.02), - "gate_up": randn(EXPERTS, 2 * WIDTH, HIDDEN, scale=0.02), - "down": randn(EXPERTS, HIDDEN, WIDTH, scale=0.02), - } - - -def make_tokens(count: int, seed: int) -> torch.Tensor: - g = torch.Generator(device="cuda").manual_seed(seed) - return torch.randn(count, HIDDEN, device="cuda", generator=g).to(torch.bfloat16) - - -def fp64_reference(x: torch.Tensor, w: dict[str, torch.Tensor]): - """HF formula in FP64 with FP64 routing; returns (output, expert ids).""" - x64 = x.double() - probs = (x64 @ w["router"].double().T).softmax(-1) - weights, ids = probs.topk(TOPK, dim=-1) - weights = weights / weights.sum(-1, keepdim=True) - out = torch.zeros_like(x64) - gate_up, down = w["gate_up"], w["down"] - for expert in ids.unique().tolist(): - token, slot = torch.where(ids == expert) - gate, up = (x64[token] @ gate_up[expert].double().T).chunk(2, dim=-1) - y = (F.silu(gate) * up) @ down[expert].double().T - out.index_add_(0, token, y * weights[token, slot, None]) - return out, ids - - -# --------------------------------------------------------------------------- # -# Candidates -# -# ``route(x) -> (ids [T,10], weights [T,10])`` exposes the routing decision; -# ``fwd(x) -> [T, H]`` is the routed output; ``graph(x) -> (y, leaves)`` (when -# the candidate is differentiable) returns the output and [x, gate_up, down]. -# --------------------------------------------------------------------------- # - - -def _rl_kernel(w): - from rl_engine.models.qwen3_next import qwen3_next_forward as provider - - def route(x): - routes = provider.stable_top10_routes(provider.shared_router(x, w["router"])) - return routes.indices, routes.weights - - def fwd(x): - with torch.no_grad(): - return provider.shared_moe(x, w["router"], w["gate_up"], w["down"])[0] - - gate_up = w["gate_up"].detach().clone().requires_grad_(True) - down = w["down"].detach().clone().requires_grad_(True) - - def graph(x): - leaf = x.detach().clone().requires_grad_(True) - y, _ = provider.shared_moe(leaf, w["router"], gate_up, down) - return y, [leaf, gate_up, down] - - return { - "name": "rl-kernel shared_moe", - "source": "rl_engine.models.qwen3_next.qwen3_next_forward.shared_moe", - "route": route, - "fwd": fwd, - "graph": graph, - } - - -def _hf_modules(w, device="cuda"): - from transformers import Qwen3NextConfig - from transformers.models.qwen3_next import modeling_qwen3_next as hf - - config = Qwen3NextConfig( - hidden_size=HIDDEN, - num_experts=EXPERTS, - num_experts_per_tok=TOPK, - moe_intermediate_size=WIDTH, - norm_topk_prob=True, - ) - router = hf.Qwen3NextTopKRouter(config).to(device=device, dtype=torch.bfloat16) - experts = hf.Qwen3NextExperts(config).to(device=device, dtype=torch.bfloat16) - with torch.no_grad(): - router.weight.copy_(w["router"]) - experts.gate_up_proj.copy_(w["gate_up"]) - experts.down_proj.copy_(w["down"]) - return router, experts - - -def _hf(w): - import transformers - - router, experts = _hf_modules(w) - - def route(x): - _, weights, ids = router(x) - return ids, weights - - def call(x, mod): - _, weights, ids = router(x) - return mod(x, ids, weights) - - def fwd(x): - with torch.no_grad(): - return call(x, experts) - - def graph(x): - leaf = x.detach().clone().requires_grad_(True) - return call(leaf, experts), [leaf, experts.gate_up_proj, experts.down_proj] - - return { - "name": "HF transformers Qwen3NextExperts (eager)", - "source": f"transformers {transformers.__version__} modeling_qwen3_next", - "route": route, - "fwd": fwd, - "graph": graph, - } - - -def _vllm(w, batch_invariant: bool): - import vllm - from vllm.model_executor.layers.fused_moe.fused_moe import fused_experts - from vllm.model_executor.layers.fused_moe.router.fused_topk_router import fused_topk - - flag = "1" if batch_invariant else "0" - linear = F.linear - if batch_invariant: - from vllm.model_executor.determinism.batch_invariant import linear_batch_invariant - - linear = linear_batch_invariant - - def route(x): - with _env("VLLM_BATCH_INVARIANT", flag): - # vLLM's Qwen3-Next gate is a BF16 ReplicatedLinear; batch-invariant - # mode replaces its aten GEMM with vLLM's persistent Triton matmul. - logits = linear(x, w["router"]) - weights, ids, _ = fused_topk(x, logits, TOPK, renormalize=True) - return ids.long(), weights - - def fwd(x): - with torch.no_grad(), _env("VLLM_BATCH_INVARIANT", flag): - ids, weights = route(x) - return fused_experts(x, w["gate_up"], w["down"], weights, ids.int()) - - return { - "name": f"vLLM fused_topk + fused_experts (Triton), VLLM_BATCH_INVARIANT={flag}", - "source": f"vllm {vllm.__version__} model_executor/layers/fused_moe", - "route": route, - "fwd": fwd, - } - - -def _flashinfer(w): - import flashinfer - from flashinfer.fused_moe import cutlass_fused_moe - - # CUTLASS SwiGLU takes fc1 as [up; gate] (the opposite half order of HF). - gate, up = w["gate_up"].chunk(2, dim=1) - fc1 = torch.cat((up, gate), dim=1).contiguous() - - def route(x): - probs = F.linear(x, w["router"]).float().softmax(-1) - weights, ids = probs.topk(TOPK, dim=-1) - return ids, weights / weights.sum(-1, keepdim=True) - - def fwd(x): - with torch.no_grad(): - ids, weights = route(x) - out = cutlass_fused_moe( - x, ids.int(), weights, fc1, w["down"], torch.bfloat16, quant_scales=[] - ) - return out[0] if isinstance(out, (list, tuple)) else out - - return { - "name": "FlashInfer cutlass_fused_moe (routes from torch.topk)", - "source": f"flashinfer {flashinfer.__version__} fused_moe.cutlass_fused_moe", - "route": route, - "fwd": fwd, - } - - -def _megatron_layer(w): - """Megatron-core MoELayer configured as VIME runs Qwen3-Next. - - VIME's ``scripts/models/qwen3-next-80B-A3B.sh``: - - Softmax router computed in FP32, top-10 of 512, all-to-all dispatcher, TE - grouped GEMM and TE fused permute, no auxiliary loss. Single process, TP1/EP1. - """ - import os - - import torch.distributed as dist - from megatron.core import parallel_state - from megatron.core.models.gpt.moe_module_specs import get_moe_module_spec - from megatron.core.tensor_parallel.random import model_parallel_cuda_manual_seed - from megatron.core.transformer.spec_utils import build_module - from megatron.core.transformer.transformer_config import TransformerConfig - - if not dist.is_initialized(): - os.environ.setdefault("MASTER_ADDR", "127.0.0.1") - os.environ.setdefault("MASTER_PORT", "29531") - device = torch.device("cuda", torch.cuda.current_device()) - dist.init_process_group("nccl", rank=0, world_size=1, device_id=device) - if not parallel_state.model_parallel_is_initialized(): - parallel_state.initialize_model_parallel(1, 1) - model_parallel_cuda_manual_seed(0) - config = TransformerConfig( - num_layers=1, - hidden_size=HIDDEN, - num_attention_heads=16, - ffn_hidden_size=WIDTH, - moe_ffn_hidden_size=WIDTH, - num_moe_experts=EXPERTS, - moe_router_topk=TOPK, - moe_router_score_function="softmax", - moe_router_dtype="fp32", - moe_token_dispatcher_type="alltoall", - moe_grouped_gemm=True, - moe_permute_fusion=True, - moe_aux_loss_coeff=0.0, - moe_router_load_balancing_type="none", - add_bias_linear=False, - gated_linear_unit=True, - activation_func=F.silu, - bf16=True, - params_dtype=torch.bfloat16, - ) - spec = get_moe_module_spec(use_te=True, num_experts=EXPERTS, moe_grouped_gemm=True) - layer = build_module(spec, config=config).cuda() - with torch.no_grad(): - layer.router.weight.copy_(w["router"]) - for expert in range(EXPERTS): - getattr(layer.experts.linear_fc1, f"weight{expert}").copy_(w["gate_up"][expert]) - getattr(layer.experts.linear_fc2, f"weight{expert}").copy_(w["down"][expert]) - return layer - - -def _megatron(w): - import megatron.core - import transformer_engine - - layer = _megatron_layer(w) - gate_up = [getattr(layer.experts.linear_fc1, f"weight{e}") for e in range(EXPERTS)] - down = [getattr(layer.experts.linear_fc2, f"weight{e}") for e in range(EXPERTS)] - - def route(x): - with torch.no_grad(): - probs, routing_map = layer.router(x) - weights, ids = probs.topk(TOPK, dim=-1) - if not bool(routing_map.gather(1, ids).all()): - raise RuntimeError("Megatron routing map disagrees with its routed probabilities") - return ids, weights - - def call(x): - return layer(x.unsqueeze(1))[0].squeeze(1) - - def fwd(x): - with torch.no_grad(): - return call(x) - - def grads(x, dy): - leaf = x.detach().clone().requires_grad_(True) - dx, *dw = torch.autograd.grad(call(leaf), [leaf, *gate_up, *down], dy) - return dx, torch.stack(dw[:EXPERTS]), torch.stack(dw[EXPERTS:]) - - return { - "name": "Megatron-core MoELayer + TE grouped GEMM (VIME's Qwen3-Next config)", - "source": f"megatron-core {megatron.core.__version__}, " - f"transformer-engine {transformer_engine.__version__}", - "route": route, - "fwd": fwd, - "grads": grads, - } - - -def _sglang_shims(): - """Make SGLang's Triton MoE path importable next to torch 2.13. - - ``sgl_kernel`` 0.3.21 is built against another libtorch ABI and cannot load - here. On CUDA, SGLang's Triton MoE calls two of its kernels; both are - replaced by SGLang's own implementations of the same operation: the Triton - ``moe_sum_reduce_triton`` and the JIT ``moe_align_block_size`` that SGLang - registers with the same signature. Every other ``sgl_kernel`` symbol only - has to import; calling one raises. - """ - import importlib.abc - import importlib.machinery - import sys - import types - - class Missing: - def __init__(self, name): - self.name = name - - def __getattr__(self, name): - if name.startswith("__"): - raise AttributeError(name) - return Missing(f"{self.name}.{name}") - - def __call__(self, *args, **kwargs): - raise RuntimeError(f"{self.name} called, but sgl_kernel cannot load next to torch 2.13") - - class Stub(types.ModuleType): - def __getattr__(self, name): - if name.startswith("__"): - raise AttributeError(name) - return Missing(f"{self.__name__}.{name}") - - class Finder(importlib.abc.MetaPathFinder, importlib.abc.Loader): - def find_spec(self, name, path=None, target=None): - if name in ("sgl_kernel", "gguf") or name.startswith("sgl_kernel."): - return importlib.machinery.ModuleSpec(name, self, is_package=True) - - def create_module(self, spec): - module = Stub(spec.name) - module.__path__ = [] - return module - - def exec_module(self, module): - pass - - if not any(type(f).__name__ == "Finder" for f in sys.meta_path): - sys.meta_path.insert(0, Finder()) - import sgl_kernel - import sglang.kernels.ops.moe as moe_ops - from sglang.kernels.ops.moe.fused_moe_triton_kernels import moe_sum_reduce_triton - from sglang.kernels.spec import KernelBackend - - sgl_kernel.moe_sum_reduce = moe_sum_reduce_triton - lookup = moe_ops.get_kernel - moe_ops.get_kernel = lambda op, backend: lookup( - op, KernelBackend.JIT if op == "moe.moe_align_block_size" else backend - ) - - -def _sglang(w, deterministic: bool): - import sglang - - _sglang_shims() - from sglang.srt.layers.moe.moe_runner.base import MoeRunnerConfig - from sglang.srt.layers.moe.moe_runner.triton_utils import fused_moe as fm - from sglang.srt.layers.moe.moe_runner.triton_utils import fused_moe_triton_config as fc - from sglang.srt.layers.moe.topk import StandardTopKOutput, fused_topk_torch_native - - class Bag: - def __init__(self, **values): - self.__dict__.update(values) - - def __getattr__(self, name): - return False - - # SGLang reads these switches from its published server config; publishing - # needs a full server (and sgl_kernel). Only the deterministic switch matters. - settings = Bag(deterministic=Bag(enable_deterministic_inference=deterministic), moe=Bag()) - from sglang.srt.batch_invariant_ops import batch_invariant_ops as bio - - def linear(x, weight): - # Deterministic mode: what enable_batch_invariant_mode() installs for aten::mm, - # called directly so the override does not leak into other candidates. - return bio.mm_batch_invariant(x, weight.t()) if deterministic else F.linear(x, weight) - - config = MoeRunnerConfig( - num_experts=EXPERTS, - num_local_experts=EXPERTS, - hidden_size=HIDDEN, - intermediate_size_per_partition=WIDTH, - top_k=TOPK, - params_dtype=torch.bfloat16, - inplace=False, - ) - - def set_mode(): - fm.get_exec = fc.get_exec = lambda: settings - fm.is_batch_invariant_mode_enabled = lambda: deterministic - # One process, no TP group: symmetric memory is disabled, the group unused. - fm.get_parallel = lambda: Bag(tp_group=None) - fm.is_allocation_symmetric = lambda: False - - def route(x): - set_mode() - logits = linear(x, w["router"]) - weights, ids = fused_topk_torch_native(x, logits, TOPK, renormalize=True)[:2] - return ids.long(), weights - - def fwd(x): - with torch.no_grad(): - ids, weights = route(x) - topk = StandardTopKOutput(weights, ids.int(), None) - return fm.fused_experts(x, w["gate_up"], w["down"], topk, config) - - mode = "deterministic inference" if deterministic else "default" - return { - "name": f"SGLang fused_moe (Triton), {mode}", - "source": f"sglang {sglang.__version__} moe_runner/triton_utils", - "route": route, - "fwd": fwd, - } - - -def candidates(w) -> list[dict[str, Any]]: - return [ - _guard("rl_kernel_cuda", lambda: _rl_kernel(w)), - _guard("hf_transformers", lambda: _hf(w)), - _guard("vllm_bi0", lambda: _vllm(w, False)), - _guard("vllm_bi1", lambda: _vllm(w, True)), - _guard("flashinfer_cutlass", lambda: _flashinfer(w)), - _guard("megatron_te", lambda: _megatron(w)), - _guard("sglang_triton", lambda: _sglang(w, False)), - _guard("sglang_deterministic", lambda: _sglang(w, True)), - ] - - -# --------------------------------------------------------------------------- # -# Checks -# --------------------------------------------------------------------------- # - - -def _embedded(x: torch.Tensor, size: int): - """Probe rows first in a batch of ``size``, and the same probe rows last.""" - first = x[:size] - last = x[:size].clone() - last[size - PROBE :] = x[:PROBE] - return first, last - - -def _rows_bi(fn, x) -> dict[str, bool]: - alone = fn(x[:PROBE]) - result = {} - for size in BI_SIZES: - first, last = _embedded(x, size) - a, b = fn(first), fn(last) - if isinstance(alone, tuple): - ok = all( - _bitwise(p[:PROBE], q) and _bitwise(r[size - PROBE :], q) - for p, r, q in zip(a, b, alone) - ) - else: - ok = _bitwise(a[:PROBE], alone) and _bitwise(b[size - PROBE :], alone) - result[str(size)] = bool(ok) - return result - - -def has_backward(cand) -> bool: - return "graph" in cand or "grads" in cand - - -def cand_grads(cand, rows, dy_rows): - """``(dx, d gate_up, d down)`` of one backward.""" - if "grads" in cand: - return cand["grads"](rows, dy_rows) - y, leaves = cand["graph"](rows) - return torch.autograd.grad(y, leaves, dy_rows) - - -def _grad_bi(cand, x) -> dict[str, Any]: - """dx of the probe rows, and dW with zero-gradient rows appended.""" - g = torch.Generator(device="cuda").manual_seed(7) - dy = torch.randn(x.shape, device="cuda", generator=g).to(torch.bfloat16) - - def grads(rows, dy_rows): - return cand_grads(cand, rows, dy_rows) - - alone = grads(x[:PROBE], dy[:PROBE]) - dx, dw = {}, {} - for size in BI_SIZES: - full = grads(x[:size], dy[:size]) - dx[str(size)] = bool(_bitwise(full[0][:PROBE], alone[0])) - padded = dy[:size].clone() - padded[PROBE:] = 0 - masked = grads(x[:size], padded) - dw[str(size)] = bool(all(_bitwise(m, a) for m, a in zip(masked[1:], alone[1:]))) - return {"dx_rows_bitwise": dx, "dweight_zero_rows_bitwise": dw} - - -def _accuracy(cand, x, w) -> dict[str, Any]: - ref, ref_ids = fp64_reference(x, w) - ids, _ = cand["route"](x) - same = ids.long().sort(-1).values == ref_ids.sort(-1).values - out = cand["fwd"](x) - return { - "rel_l2_vs_fp64": _rel(out, ref), - "max_abs_vs_fp64": float((out.double() - ref).abs().max()), - "tokens_with_different_expert_set": int((~same.all(-1)).sum()), - "tokens": int(x.shape[0]), - } - - -def _one(cand, x, w) -> dict[str, Any]: - if "unavailable" in cand: - return cand - out = {"key": cand["key"], "name": cand["name"], "source": cand["source"]} - try: - out["route_rows_bitwise"] = _rows_bi(cand["route"], x) - out["output_rows_bitwise"] = _rows_bi(cand["fwd"], x) - out["repeatable"] = bool(_bitwise(cand["fwd"](x[:256]), cand["fwd"](x[:256]))) - if has_backward(cand): - out.update(_grad_bi(cand, x)) - out["accuracy"] = _accuracy(cand, x[:256], w) - except Exception as exc: # noqa: BLE001 - a crash is a result, not a harness failure - out["failed"] = f"{type(exc).__name__}: {str(exc)[:300]}" - out["batch_invariant"] = bool( - "failed" not in out - and all(out["route_rows_bitwise"].values()) - and all(out["output_rows_bitwise"].values()) - and all(out.get("dx_rows_bitwise", {"": True}).values()) - and all(out.get("dweight_zero_rows_bitwise", {"": True}).values()) - ) - return out - - -def _latency(cands, tokens) -> dict[str, Any]: - ready = [c for c in cands if "unavailable" not in c] - forward = {} - for count in tokens: - x = make_tokens(count, seed=100 + count) - calls = {} - for cand in ready: - try: - cand["fwd"](x) - calls[cand["key"]] = lambda c=cand: c["fwd"](x) - except Exception: # noqa: BLE001 - recorded by the BI section - continue - forward[str(count)] = time_interleaved(calls) - backward = {} - for count in BACKWARD_TOKENS: - x = make_tokens(count, seed=200 + count) - dy = torch.randn_like(x) - calls = {} - for cand in ready: - if not has_backward(cand): - continue - - def step(c=cand): - cand_grads(c, x, dy) - - calls[cand["key"]] = step - backward[str(count)] = time_interleaved(calls, warmup=2, iters=10) - return {"forward_us": forward, "forward_plus_backward_us": backward} - - -def moe_report(only=None) -> dict[str, Any]: - """The report for every candidate, or only for the keys in ``only``. - - Some candidates need another Python environment (SGLang's pinned torch ABI, - Megatron's training stack); run each environment with ``only`` and merge. - """ - torch.manual_seed(0) - w = make_weights() - x = make_tokens(max(BI_SIZES), seed=1) - keys = [ - "rl_kernel_cuda", - "hf_transformers", - "vllm_bi0", - "vllm_bi1", - "flashinfer_cutlass", - "megatron_te", - "sglang_triton", - "sglang_deterministic", - ] - if only is not None and set(only) - set(keys): - raise ValueError(f"Unknown candidates: {sorted(set(only) - set(keys))}") - cands = [c for c in candidates(w) if only is None or c["key"] in only] - return { - "op": "qwen3_next_routed_moe", - "shape": {"hidden": HIDDEN, "experts": EXPERTS, "top_k": TOPK, "expert_width": WIDTH}, - "probe_rows": PROBE, - "candidates": [_one(c, x, w) for c in cands], - "latency": _latency(cands, PERF_TOKENS), - } diff --git a/tools/validation/models/plot_qwen3_next_moe_prior_art.py b/tools/validation/models/plot_qwen3_next_moe_prior_art.py deleted file mode 100644 index 21cee4fb9..000000000 --- a/tools/validation/models/plot_qwen3_next_moe_prior_art.py +++ /dev/null @@ -1,162 +0,0 @@ -#!/usr/bin/env python -# SPDX-License-Identifier: Apache-2.0 -# Copyright (c) 2026 RL-Kernel Contributors - -"""Render the routed-MoE prior-art report as a PNG. - -Needs only the report JSON and matplotlib, so it runs anywhere: - - python tools/validation/models/plot_qwen3_next_moe_prior_art.py \\ - docs/usage/evidence/qwen3-next-moe-route-b200/report.json - -writes ``figure.png`` next to the report. Three panels: which batch-invariance -checks each implementation passes, accuracy against an FP64 evaluation with -FP64 routing, and forward latency. RL-Kernel is blue, existing libraries orange. -""" - -from __future__ import annotations - -import argparse -import json -from pathlib import Path - -import matplotlib - -matplotlib.use("Agg") -import matplotlib.pyplot as plt # noqa: E402 -from matplotlib.colors import ListedColormap # noqa: E402 - -RL_KERNEL = "#2a78d6" -INK = "#2b2b29" -MUTED = "#6f6e69" -GRID = "#e4e3dd" -SURFACE = "#fcfcfb" -PASS = "#1baf7a" -FAIL = "#d64545" -NA = "#e4e3dd" -EXISTING = ("#eb6834", "#b8501f", "#f29a62", "#8a3a15", "#f6bd94", "#d9773f", "#6b2a0e") - -LABELS = { - "rl_kernel_cuda": "RL-Kernel shared_moe", - "hf_transformers": "HF Qwen3NextExperts", - "vllm_bi0": "vLLM fused_moe (BI=0)", - "vllm_bi1": "vLLM fused_moe (BI=1)", - "flashinfer_cutlass": "FlashInfer cutlass_fused_moe", - "megatron_te": "Megatron-core + TE (VIME config)", - "sglang_triton": "SGLang fused_moe (default)", - "sglang_deterministic": "SGLang fused_moe (deterministic)", -} -CHECKS = ( - ("route_rows_bitwise", "routes"), - ("output_rows_bitwise", "output"), - ("dx_rows_bitwise", "dx"), - ("dweight_zero_rows_bitwise", "dW"), -) - - -def _colors(keys): - existing = iter(EXISTING) - return {k: RL_KERNEL if k == "rl_kernel_cuda" else next(existing) for k in keys} - - -def _style(ax): - ax.set_facecolor(SURFACE) - for side in ("top", "right"): - ax.spines[side].set_visible(False) - ax.tick_params(colors=INK, labelsize=8) - ax.grid(color=GRID, linewidth=0.6) - - -def plot(report: dict, out: Path) -> None: - cands = [c for c in report["candidates"] if "unavailable" not in c] - keys = [c["key"] for c in cands] - colors = _colors(keys) - sizes = list(next(iter(cands))["output_rows_bitwise"]) - fig, axes = plt.subplots(1, 3, figsize=(17, 4.6), gridspec_kw={"width_ratios": [1.5, 1, 1.4]}) - fig.patch.set_facecolor("white") - - # Panel 1: batch-invariance matrix; 1 = pass, 0 = fail, 0.5 = no backward. - ax = axes[0] - columns = [f"{label} {size}" for _, label in CHECKS for size in sizes] - grid = [] - for cand in cands: - row = [] - for field, _ in CHECKS: - values = cand.get(field) - row.extend(0.5 if values is None else float(values[s]) for s in sizes) - grid.append(row) - ax.imshow(grid, cmap=ListedColormap([FAIL, NA, PASS]), vmin=0, vmax=1, aspect="auto") - ax.set_xticks(range(len(columns)), columns, fontsize=7, rotation=90) - ax.set_yticks(range(len(cands)), [LABELS.get(k, k) for k in keys], fontsize=8) - for i, cand in enumerate(cands): - mark = "BI" if cand.get("batch_invariant") else "not BI" - ax.text(len(columns) - 0.4, i, f" {mark}", va="center", fontsize=8, color=INK) - ax.set_title( - f"Batch invariance: {report['probe_rows']} probe tokens alone vs. in a batch\n" - "(green bitwise equal, red differs, grey: no backward)", - fontsize=9, - color=INK, - ) - ax.tick_params(length=0) - - # Panel 2: accuracy. - ax = axes[1] - _style(ax) - rel = [c["accuracy"]["rel_l2_vs_fp64"] if "accuracy" in c else float("nan") for c in cands] - ax.barh(range(len(cands)), rel, color=[colors[k] for k in keys]) - for i, cand in enumerate(cands): - acc = cand.get("accuracy") - if acc: - ax.text( - rel[i], - i, - f" {rel[i]:.2e} ({acc['tokens_with_different_expert_set']}/{acc['tokens']}" - " tokens route differently)", - va="center", - fontsize=7, - color=MUTED, - ) - ax.set_yticks(range(len(cands)), [LABELS.get(k, k) for k in keys], fontsize=8) - ax.invert_yaxis() - ax.set_xlim(0, 2.2 * max(r for r in rel if r == r)) - ax.set_xlabel("relative L2 error vs. FP64 HF formula with FP64 routing", fontsize=8) - ax.set_title("Accuracy (256 tokens)", fontsize=9, color=INK) - - # Panel 3: forward latency. - ax = axes[2] - _style(ax) - forward = report["latency"]["forward_us"] - tokens = [int(t) for t in forward] - for key in keys: - ys = [forward[str(t)].get(key, float("nan")) / 1000.0 for t in tokens] - ax.plot(tokens, ys, marker="o", ms=3.5, color=colors[key], label=LABELS.get(key, key)) - ax.set_xscale("log", base=2) - ax.set_yscale("log") - ax.set_xlabel("tokens", fontsize=8) - ax.set_ylabel("forward latency (ms, median)", fontsize=8) - ax.set_title("Forward latency\n(H=2048, 512 experts, top-10, width 512)", fontsize=9) - ax.legend(fontsize=7, frameon=False) - - env = report["environment"] - fig.suptitle( - f"Qwen3-Next routed MoE prior art - {env['gpu']}, torch {env['torch']}, " - f"commit {report['rl_kernel_commit'][:7]}", - fontsize=10, - color=INK, - ) - fig.tight_layout() - fig.savefig(out, dpi=160) - - -def main() -> None: - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("report", type=Path) - args = parser.parse_args() - report = json.loads(args.report.read_text()) - out = args.report.with_name("figure.png") - plot(report, out) - print(f"wrote {out}") - - -if __name__ == "__main__": - main() diff --git a/tools/validation/models/qwen3_next/__init__.py b/tools/validation/models/qwen3_next/__init__.py new file mode 100644 index 000000000..86cf4c9da --- /dev/null +++ b/tools/validation/models/qwen3_next/__init__.py @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors diff --git a/tools/validation/models/qwen3_next_tp_moe_check.py b/tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py similarity index 97% rename from tools/validation/models/qwen3_next_tp_moe_check.py rename to tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py index 775c9a84b..f5a65f8ab 100644 --- a/tools/validation/models/qwen3_next_tp_moe_check.py +++ b/tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py @@ -5,7 +5,7 @@ """TP4 MoE gate on the official checkpoint's layer-0 weights; four GPUs, torchrun. TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 \\ - tools/validation/models/qwen3_next_tp_moe_check.py \\ + tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py \\ --checkpoint --output Each rank writes ``rank-.json``. The gate requires, bitwise: @@ -30,7 +30,7 @@ from importlib.metadata import version from pathlib import Path -sys.path.insert(0, str(Path(__file__).resolve().parents[3])) +sys.path.insert(0, str(Path(__file__).resolve().parents[4])) import torch # noqa: E402 import torch.distributed as dist # noqa: E402 @@ -50,7 +50,7 @@ assert_tensor_bitwise_equal as exact, ) -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] TOKEN_COUNTS = (8, 64, 256, 1024) diff --git a/tools/validation/models/qwen3_next_moe_prior_art.py b/tools/validation/models/qwen3_next_moe_prior_art.py deleted file mode 100644 index c3c8d088a..000000000 --- a/tools/validation/models/qwen3_next_moe_prior_art.py +++ /dev/null @@ -1,116 +0,0 @@ -#!/usr/bin/env python -# SPDX-License-Identifier: Apache-2.0 -# Copyright (c) 2026 RL-Kernel Contributors - -"""Write the Qwen3-Next routed-MoE prior-art report (BI + accuracy + latency) as JSON. - -Run it on a clean tree with ``TRITON_F32_DEFAULT=ieee`` (the FP32 router needs -IEEE Triton dots from process start). The report records the commit, whether -the tracked tree was dirty and the version of every library compared. -``tools/validation/models/plot_qwen3_next_moe_prior_art.py`` turns the report into a figure. - - TRITON_F32_DEFAULT=ieee python tools/validation/models/qwen3_next_moe_prior_art.py \\ - --out docs/usage/evidence/qwen3-next-moe-route-b200/report.json -""" - -from __future__ import annotations - -import argparse -import importlib.metadata as md -import json -import subprocess -import sys -from pathlib import Path - -ROOT = Path(__file__).resolve().parents[3] -sys.path.insert(0, str(ROOT)) - -import torch # noqa: E402 - -from rl_engine.validation.models.qwen3_next_moe_prior_art import moe_report # noqa: E402 - -LIBRARIES = ( - "vllm", - "flashinfer-python", - "transformers", - "triton", - "sglang", - "sgl-kernel", - "megatron_core", - "megatron-core", - "transformer_engine", - "vime", -) - - -def _git(*args: str) -> str: - return subprocess.run( - ["git", *args], cwd=ROOT, check=True, capture_output=True, text=True - ).stdout.strip() - - -def _version(name: str) -> str | None: - try: - return md.version(name) - except md.PackageNotFoundError: - return None - - -def merge(base: dict, extra: dict) -> dict: - """``base`` with ``extra``'s candidates and latencies added (same commit and GPU).""" - for field in ("rl_kernel_commit", "tracked_tree_dirty", "shape", "op"): - if base[field] != extra[field]: - raise SystemExit(f"Reports differ in {field}; they cannot be merged") - if base["environment"]["gpu"] != extra["environment"]["gpu"]: - raise SystemExit("Reports were measured on different GPUs") - keys = {c["key"] for c in base["candidates"]} - added = [c for c in extra["candidates"] if c["key"] not in keys] - merged = dict(base, candidates=base["candidates"] + added) - merged["latency"] = { - section: { - size: {**values, **extra["latency"][section].get(size, {})} - for size, values in sizes.items() - } - for section, sizes in base["latency"].items() - } - merged["environments"] = {"main": base["environment"], "extra": extra["environment"]} - return merged - - -def main() -> None: - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--out", type=Path, required=True) - parser.add_argument("--only", help="comma-separated candidate keys (default: all)") - parser.add_argument( - "--merge", type=Path, help="a report from another environment to merge into this one" - ) - args = parser.parse_args() - if not torch.cuda.is_available(): - raise SystemExit("needs a CUDA device") - torch.backends.cuda.matmul.allow_tf32 = False - torch.backends.cudnn.allow_tf32 = False - report = { - "kind": "qwen3_next_prior_art_report", - "rfc": 428, - "rl_kernel_commit": _git("rev-parse", "HEAD"), - "tracked_tree_dirty": bool(_git("status", "--porcelain", "--untracked-files=no")), - "environment": { - "gpu": torch.cuda.get_device_name(), - "torch": torch.__version__, - "cuda": torch.version.cuda, - "libraries": {name: _version(name) for name in LIBRARIES}, - }, - **moe_report(args.only.split(",") if args.only else None), - } - if args.merge is not None: - report = merge(json.loads(args.merge.read_text()), report) - args.out.parent.mkdir(parents=True, exist_ok=True) - args.out.write_text(json.dumps(report, indent=2) + "\n") - print( - f"wrote {args.out} (commit {report['rl_kernel_commit'][:7]}, " - f"dirty={report['tracked_tree_dirty']})" - ) - - -if __name__ == "__main__": - main() From 0d04347654ad735f89da2b17a54271bae018ee32 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Wed, 14 Oct 2026 20:42:52 +0000 Subject: [PATCH 42/68] refactor(qwen3-next): full-model chain to the reviewed layout Per the layout review on #549, this branch's own files move the way #549 already moved them, by applying that migration to this branch's versions: - validation modules and JSON resources go to rl_engine/validation/models/qwen3_next/, runners and stage scripts to tools/validation/models/qwen3_next/; imports, resource paths, CI scripts and tests follow; - the chain plot moves to the Qwen3-Next benchmark PR; - the committed chain evidence becomes a ZIP release attachment with a short summary in reports/experiments/qwen3_next/ (bytes unchanged), so the pre-commit exclusion for it is dropped. The RMSNorm files that #467/#468 introduce keep their paths here. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .pre-commit-config.yaml | 5 +- ci/scripts/run_qwen3_next_chain_gate.sh | 2 +- ci/scripts/run_qwen3_next_operator_gates.sh | 2 +- docs/usage/qwen3-next-chain.md | 20 ++- .../qwen3_next/qwen3-next-chain-b200.md | 48 ++++++ .../rollout/vllm/qwen3_next_native_rollout.py | 2 +- .../qwen3_next/native_recompute.py | 2 +- .../experiments/qwen3_next/native_training.py | 11 +- .../vime/experiments/qwen3_next/profile.py | 4 +- .../vime/providers/qwen3_next_training.py | 4 +- .../vime/providers/qwen3_next_vime_runtime.py | 2 +- .../validation/models/pytest_case_manifest.py | 2 +- .../validation/models/qwen3_next/__init__.py | 2 + .../{ => qwen3_next}/qwen3_next_binary.py | 0 .../{ => qwen3_next}/qwen3_next_cases.py | 0 .../qwen3_next_chain_report.py | 3 +- .../qwen3_next_export_schema.json | 0 .../models/{ => qwen3_next}/qwen3_next_l2.py | 2 +- .../qwen3_next_layer_cases.json | 10 +- .../qwen3_next_operator_cases.json | 2 +- .../{ => qwen3_next}/qwen3_next_rl_fixture.py | 0 .../check_qwen3_next_native_rollout.py | 2 +- .../test_qwen3_next_binary_evidence.py | 2 +- .../test_qwen3_next_case_manifest.py | 6 +- .../test_qwen3_next_chain_report.py | 2 +- .../qwen3_next/test_qwen3_next_checkpoint.py | 2 +- .../test_qwen3_next_environment_identity.py | 2 +- .../test_qwen3_next_evidence_redaction.py | 3 +- .../qwen3_next/test_qwen3_next_failure.py | 2 +- ..._qwen3_next_installed_provider_identity.py | 2 +- tests/models/qwen3_next/test_qwen3_next_l2.py | 43 +++-- .../qwen3_next/test_qwen3_next_layers.py | 4 +- .../test_qwen3_next_native_recompute.py | 4 +- .../test_qwen3_next_operator_gates.py | 4 +- .../qwen3_next/test_qwen3_next_profile.py | 2 +- .../qwen3_next/test_qwen3_next_rl_fixture.py | 2 +- .../models/qwen3_next/test_qwen3_next_run.py | 8 +- .../qwen3_next/test_qwen3_next_storage.py | 2 +- .../test_qwen3_next_training_checkpoint.py | 4 +- .../test_qwen3_next_training_closeout.py | 4 +- .../test_qwen3_next_training_export.py | 2 +- tests/validation/common/test_file_digest.py | 2 +- .../models/plot_qwen3_next_chain.py | 152 ------------------ .../qwen3_next_adapter_check.py | 0 .../qwen3_next_chain_report.py | 12 +- .../{ => qwen3_next}/qwen3_next_checkpoint.py | 0 .../{ => qwen3_next}/qwen3_next_contract.py | 12 +- .../models/{ => qwen3_next}/qwen3_next_l2.py | 10 +- .../qwen3_next_layer_contract.py | 6 +- .../{ => qwen3_next}/qwen3_next_layers.py | 8 +- .../qwen3_next_operator_cases.py | 14 +- .../qwen3_next_operator_gates.py | 10 +- .../qwen3_next_optimizer_checkpoint.py | 0 .../{ => qwen3_next}/qwen3_next_preflight.py | 4 +- .../qwen3_next_publish_evidence.py | 7 +- .../qwen3_next_rollout_smoke.py | 0 .../models/{ => qwen3_next}/qwen3_next_run.py | 4 +- .../{ => qwen3_next}/qwen3_next_runtime.py | 39 ++++- .../qwen3_next_tp_head_check.py | 0 .../{ => qwen3_next}/qwen3_next_tp_probe.py | 0 .../{ => qwen3_next}/qwen3_next_training.py | 8 +- .../qwen3_next_training_checkpoint.py | 2 +- .../qwen3_next_training_export.py | 6 +- 63 files changed, 244 insertions(+), 277 deletions(-) create mode 100644 reports/experiments/qwen3_next/qwen3-next-chain-b200.md create mode 100644 rl_engine/validation/models/qwen3_next/__init__.py rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_binary.py (100%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_cases.py (100%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_chain_report.py (99%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_export_schema.json (100%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_l2.py (99%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_layer_cases.json (87%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_operator_cases.json (99%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_rl_fixture.py (100%) delete mode 100644 tools/validation/models/plot_qwen3_next_chain.py rename tools/validation/models/{ => qwen3_next}/qwen3_next_adapter_check.py (100%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_chain_report.py (91%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_checkpoint.py (100%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_contract.py (97%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_l2.py (98%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_layer_contract.py (94%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_layers.py (97%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_operator_cases.py (87%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_operator_gates.py (95%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_optimizer_checkpoint.py (100%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_preflight.py (98%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_publish_evidence.py (95%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_rollout_smoke.py (100%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_run.py (99%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_runtime.py (97%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_tp_head_check.py (100%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_tp_probe.py (100%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_training.py (99%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_training_checkpoint.py (99%) rename tools/validation/models/{ => qwen3_next}/qwen3_next_training_export.py (99%) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9ef274150..3e37f8bd1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,10 +4,9 @@ repos: hooks: - id: trailing-whitespace # Unified diffs include significant context markers and pinned hashes. - # Published Qwen3-Next chain evidence is byte-exact (SHA256SUMS). - exclude: '(\.patch$|^reports/archive/|^docs/usage/evidence/qwen3-next-chain-b200/)' + exclude: '(\.patch$|^reports/archive/)' - id: end-of-file-fixer - exclude: '^(reports/archive/|docs/usage/evidence/qwen3-next-chain-b200/)' + exclude: ^reports/archive/ - id: check-yaml exclude: 'mkdocs\.yaml$' - id: check-added-large-files diff --git a/ci/scripts/run_qwen3_next_chain_gate.sh b/ci/scripts/run_qwen3_next_chain_gate.sh index 9f0d7c2df..a2bdba2f9 100755 --- a/ci/scripts/run_qwen3_next_chain_gate.sh +++ b/ci/scripts/run_qwen3_next_chain_gate.sh @@ -13,7 +13,7 @@ if [ "$#" -ge 1 ]; then import dataclasses, json, sys from rl_engine.validation.models.chain_gate import ChainGateReport -from rl_engine.validation.models.qwen3_next_chain_report import COMPATIBLE_SCHEMA, SCHEMA_VERSION +from rl_engine.validation.models.qwen3_next.qwen3_next_chain_report import COMPATIBLE_SCHEMA, SCHEMA_VERSION report = json.load(open(sys.argv[1])) missing = {f.name for f in dataclasses.fields(ChainGateReport)} - set(report) diff --git a/ci/scripts/run_qwen3_next_operator_gates.sh b/ci/scripts/run_qwen3_next_operator_gates.sh index d744fc2b9..7dd37f35c 100644 --- a/ci/scripts/run_qwen3_next_operator_gates.sh +++ b/ci/scripts/run_qwen3_next_operator_gates.sh @@ -6,5 +6,5 @@ cd "$ROOT" PY="${PY:-python3}" OUT="${QWEN3_NEXT_EVIDENCE_DIR:?Set QWEN3_NEXT_EVIDENCE_DIR to an artifact directory}" export PYTHONPATH="$ROOT${PYTHONPATH:+:$PYTHONPATH}" -exec "$PY" tools/validation/models/qwen3_next_operator_gates.py --out "$OUT" \ +exec "$PY" tools/validation/models/qwen3_next/qwen3_next_operator_gates.py --out "$OUT" \ --suite "${QWEN3_NEXT_OPERATOR_SUITE:-all}" diff --git a/docs/usage/qwen3-next-chain.md b/docs/usage/qwen3-next-chain.md index c5d5801aa..5b96b19cd 100644 --- a/docs/usage/qwen3-next-chain.md +++ b/docs/usage/qwen3-next-chain.md @@ -27,13 +27,13 @@ layer family. Inside an allocation with four GPUs, from a clean checkout of the commit to measure: ```bash -python tools/validation/models/qwen3_next_run.py --stage all \ +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage all \ --runs-root --checkpoint --cuda-home \ --python /bin/python --until l2 -python tools/validation/models/qwen3_next_run.py --stage all --from train1 --until train2 \ +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage all --from train1 --until train2 \ --previous /<...-l2-...> \ --runs-root --checkpoint --cuda-home --python /bin/python -python tools/validation/models/qwen3_next_run.py --stage train --previous /<...-train2-...> \ +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage train --previous /<...-train2-...> \ --runs-root --checkpoint --cuda-home --python /bin/python ``` @@ -45,7 +45,7 @@ provider or topology identity. | Stage | What it checks | Typical time | | --- | --- | --- | -| `preflight` | environment and versions, the mandatory operator cases (`rl_engine/validation/models/qwen3_next_operator_cases.json`) and norm C3/C4 gates, multi-GPU device guards, a four-rank TP probe, checkpoint hashes, a vLLM rollout smoke | 15 min | +| `preflight` | environment and versions, the mandatory operator cases (`rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json`) and norm C3/C4 gates, multi-GPU device guards, a four-rank TP probe, checkpoint hashes, a vLLM rollout smoke | 15 min | | `layers` | the real-weight TP4 gates (GDN, attention, MoE, GDN engine adapters, embedding/LM head) and the native scheduler on 16 cases; certifies the shared provider profile | 25 min | | `l2` | 16 cases (prompt 8/64/256/1024 x batch 1/4 x full/chunked prefill, 16 response tokens): rollout vs replay, raw bits, plus every layer and sub-node digest | 30 min | | `train1`, `train2` | one optimizer update each, checkpoint, HF export, L2 on the exported weights | 3-4 h each | @@ -54,12 +54,16 @@ provider or topology identity. ## Reports ```bash -python tools/validation/models/qwen3_next_chain_report.py --last /<...-train-...> --out chain-report.json +python tools/validation/models/qwen3_next/qwen3_next_chain_report.py --last /<...-train-...> --out chain-report.json bash ci/scripts/run_qwen3_next_chain_gate.sh chain-report.json -python tools/validation/models/qwen3_next_publish_evidence.py --last /<...-train-...> \ - --chain-report chain-report.json --out docs/usage/evidence/qwen3-next-chain- +python tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py --last /<...-train-...> \ + --chain-report chain-report.json --out artifacts/qwen3-next-chain- ``` +The published copy is attached to a release as a ZIP rather than committed; the +B200 results, tested commit and download link are in +[`reports/experiments/qwen3_next/qwen3-next-chain-b200.md`](https://github.com/fusheng-ji/RL-Kernel/blob/feat/ws1-qwen3-next-full-model-chain/reports/experiments/qwen3_next/qwen3-next-chain-b200.md). + `chain-report.json` has every field of the Qwen3-8B chain gate's report (`rl_engine.validation.models.chain_gate.ChainGateReport`, schema `ws1-c10-c11-v5`) with `schema_version: qwen3-next-chain-v1`; each case at each weight version is @@ -74,7 +78,7 @@ both engines (`rl_engine/models/qwen3_next/qwen3_next_subnode.py`): the normed mixer input, the mixer output, the normed MLP input, the ten selected expert ids and the MoE output, plus the embedding once per model. -`tools/validation/models/qwen3_next_publish_evidence.py` copies the stage records and the +`tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py` copies the stage records and the report, replaces site paths, host and user names with placeholders, refuses to publish if any survive, and writes `SHA256SUMS`. diff --git a/reports/experiments/qwen3_next/qwen3-next-chain-b200.md b/reports/experiments/qwen3_next/qwen3-next-chain-b200.md new file mode 100644 index 000000000..ebb16c526 --- /dev/null +++ b/reports/experiments/qwen3_next/qwen3-next-chain-b200.md @@ -0,0 +1,48 @@ +# Qwen3-Next full-model chain (B200) + +[Download raw results (ZIP)](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-rfc428-evidence/qwen3-next-chain-b200.zip). + +ZIP SHA-256: `76028dfea0a2b8698aa8d14d1f82e8da47df4ea11f9cd5240ebfb33094e527c1`. + +Measured at commit `c67ec14` (`measured-commit.txt`) from clean clones, one +stage after another on nodes with four B200 GPUs, in one environment +(`environment_sha256` `118384371a88…` in every `run.json`), on the official +`Qwen/Qwen3-Next-80B-A3B-Instruct` revision +`9c7f2fbe84465e40164a94cc16cd30b6999b0cc7`. The archive holds every stage's +redacted `run.json`, `result.json` and evidence files, `chain-report.json`, +`figure.png`, `redaction.json`, the original README and `SHA256SUMS`, byte for +byte as published. + +```bash +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage all \ + --runs-root --checkpoint --cuda-home --python /bin/python +python tools/validation/models/qwen3_next/qwen3_next_chain_report.py --last /<...-train-...> --out chain-report.json +bash ci/scripts/run_qwen3_next_chain_gate.sh chain-report.json +python tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py --last /<...-train-...> \ + --chain-report chain-report.json --out artifacts/qwen3-next-chain-b200 +python benchmarks/models/qwen3_next/plot_qwen3_next_chain.py artifacts/qwen3-next-chain-b200 +``` + +At `c67ec14` the runners lived in `scripts/` and `ci/`; the commands use the +current layout (the plot script is in the Qwen3-Next benchmark PR). The +stages ran the code of `c67ec14`; the published copy was made by the publisher +of `3025497`, which only adds redaction rules and changes no measured byte. + +| Stage | Checks | Result | +| --- | --- | --- | +| `preflight` | operator gates, device guards, TP4 NCCL probe, checkpoint identity, rollout smoke | passed | +| `layers` | per-layer TP4 contracts on real weights (GDN, attention, MoE, adapters, head) | passed | +| `l2` | vLLM rollout vs VIME recompute: tokens, BF16 logits, FP32 logprobs, layer and sub-node digests | 16 / 16 raw-bit | +| `train1`, `train2` | one RL update each, then L2 on the updated weights | 16 / 16 raw-bit each | +| `train` | checkpoint reload, L2 on the reloaded weights; reload == update 2; continuous == resumed step 3; checkpoint fault injection | 16 / 16 raw-bit, all sections passed | +| chain-gate report | `qwen3-next-chain-v1`, a superset of `ws1-c10-c11-v5` | 64 cells, `passed: true`, `first_drift: null` | + +The 16 cases are prompt length 8 / 64 / 256 / 1024 × batch 1 / 4 × full / +chunked prefill, 16 response tokens. `train` was preempted and restarted; +before the published run the journal's earlier reload and controls were +retired into `recovery` (`train/reload.json`), so this run repeated all three +itself (see the known issue in the PR). + +Site paths, host names, users and the scheduler are replaced by placeholders +(``, ``, ``, ``, ``); `redaction.json` +counts them. diff --git a/rl_engine/integrations/engines/rollout/vllm/qwen3_next_native_rollout.py b/rl_engine/integrations/engines/rollout/vllm/qwen3_next_native_rollout.py index 3e0dbda76..dc1bc4dd8 100644 --- a/rl_engine/integrations/engines/rollout/vllm/qwen3_next_native_rollout.py +++ b/rl_engine/integrations/engines/rollout/vllm/qwen3_next_native_rollout.py @@ -24,7 +24,7 @@ ) from rl_engine.models.qwen3_next import qwen3_next_subnode as subnode from rl_engine.validation.common.tensor_identity import assert_tensor_bitwise_equal as exact -from rl_engine.validation.models.qwen3_next_l2 import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( embedding_digest, json_digest, layer_digest, diff --git a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py index cb868323e..0f91ab559 100644 --- a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py +++ b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_recompute.py @@ -17,7 +17,7 @@ from rl_engine.models.qwen3_next import qwen3_next_subnode as subnode from rl_engine.validation.common.file_digest import file_sha256 from rl_engine.validation.common.tensor_identity import assert_tensor_bitwise_equal as exact -from rl_engine.validation.models.qwen3_next_l2 import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( embedding_digest, layer_digest, load_tensor_artifact, diff --git a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_training.py b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_training.py index c3e2ae3fc..4ea41e570 100644 --- a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_training.py +++ b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/native_training.py @@ -16,8 +16,11 @@ import torch.distributed as dist import torch.multiprocessing as mp -from rl_engine.validation.models.qwen3_next_l2 import load_tensor_artifact, validate_fixture -from rl_engine.validation.models.qwen3_next_rl_fixture import PolicyFixture +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( + load_tensor_artifact, + validate_fixture, +) +from rl_engine.validation.models.qwen3_next.qwen3_next_rl_fixture import PolicyFixture def policy_fixture(path): @@ -168,7 +171,9 @@ def _rank_main(rank, request, staging): from rl_engine.integrations.orchestrators.vime.providers.qwen3_next_vime_runtime import ( create_actor, ) - from rl_engine.validation.models.qwen3_next_binary import verify_imported_provider + from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( + verify_imported_provider, + ) fixture = policy_fixture(request["fixture"]) manifest = request["manifest"] diff --git a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/profile.py b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/profile.py index 8e2e99fe0..87abbe803 100644 --- a/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/profile.py +++ b/rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/profile.py @@ -24,7 +24,7 @@ def profile_is_certified(): def validate_certified_weights(identity, certified): """The base certificate is not a certificate for newly updated weight bytes.""" - from rl_engine.validation.models.qwen3_next_l2 import validate_weight_identity + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_weight_identity validate_weight_identity(identity) if identity.get("weight_version", "baseline") == "baseline": @@ -71,7 +71,7 @@ def activate_for_identity(identity): return "qualification" if mode != "1": raise ValueError("A certified shared profile must execute strict BI=1") - sys.path.insert(0, str(ROOT / "tools/validation/models")) + sys.path.insert(0, str(ROOT / "tools/validation/models/qwen3_next")) from qwen3_next_contract import validate_result from qwen3_next_l2 import model_identity from qwen3_next_runtime import file_digest, identity_digest, verify_environment diff --git a/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_training.py b/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_training.py index 8b60fc415..91292b9b8 100644 --- a/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_training.py +++ b/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_training.py @@ -16,7 +16,9 @@ import numpy as np import torch -from rl_engine.validation.models.qwen3_next_rl_fixture import require_policy_configuration +from rl_engine.validation.models.qwen3_next.qwen3_next_rl_fixture import ( + require_policy_configuration, +) def tensor_digest(value, *, chunk_bytes=64 * 1024 * 1024): diff --git a/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py b/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py index a7481150a..8888dc15b 100644 --- a/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py +++ b/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py @@ -8,7 +8,7 @@ import sys from pathlib import Path -from rl_engine.validation.models.qwen3_next_rl_fixture import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_rl_fixture import ( OPTIMIZER_SETTINGS, POLICY_SETTINGS, require_policy_configuration, diff --git a/rl_engine/validation/models/pytest_case_manifest.py b/rl_engine/validation/models/pytest_case_manifest.py index a25e41769..7cdb38a5c 100644 --- a/rl_engine/validation/models/pytest_case_manifest.py +++ b/rl_engine/validation/models/pytest_case_manifest.py @@ -7,7 +7,7 @@ import time from pathlib import Path -from rl_engine.validation.models.qwen3_next_cases import execution_verdict, load_cases +from rl_engine.validation.models.qwen3_next.qwen3_next_cases import execution_verdict, load_cases def pytest_addoption(parser): diff --git a/rl_engine/validation/models/qwen3_next/__init__.py b/rl_engine/validation/models/qwen3_next/__init__.py new file mode 100644 index 000000000..86cf4c9da --- /dev/null +++ b/rl_engine/validation/models/qwen3_next/__init__.py @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors diff --git a/rl_engine/validation/models/qwen3_next_binary.py b/rl_engine/validation/models/qwen3_next/qwen3_next_binary.py similarity index 100% rename from rl_engine/validation/models/qwen3_next_binary.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_binary.py diff --git a/rl_engine/validation/models/qwen3_next_cases.py b/rl_engine/validation/models/qwen3_next/qwen3_next_cases.py similarity index 100% rename from rl_engine/validation/models/qwen3_next_cases.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_cases.py diff --git a/rl_engine/validation/models/qwen3_next_chain_report.py b/rl_engine/validation/models/qwen3_next/qwen3_next_chain_report.py similarity index 99% rename from rl_engine/validation/models/qwen3_next_chain_report.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_chain_report.py index 28cc0fae8..0770be733 100644 --- a/rl_engine/validation/models/qwen3_next_chain_report.py +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_chain_report.py @@ -3,7 +3,8 @@ """Qwen3-Next full-model chain as a WS1 chain-gate report. -The Qwen3-Next chain runs as stages (``tools/validation/models/qwen3_next_run.py``): L2 on the +The Qwen3-Next chain runs as stages +(``tools/validation/models/qwen3_next/qwen3_next_run.py``): L2 on the pinned weights, then L2 again after each of two optimizer updates and after a reload. Each L2 stage writes a 16-case raw-bit matrix comparing rollout (native vLLM prefill + decode) with replay (VIME recompute). This module turns diff --git a/rl_engine/validation/models/qwen3_next_export_schema.json b/rl_engine/validation/models/qwen3_next/qwen3_next_export_schema.json similarity index 100% rename from rl_engine/validation/models/qwen3_next_export_schema.json rename to rl_engine/validation/models/qwen3_next/qwen3_next_export_schema.json diff --git a/rl_engine/validation/models/qwen3_next_l2.py b/rl_engine/validation/models/qwen3_next/qwen3_next_l2.py similarity index 99% rename from rl_engine/validation/models/qwen3_next_l2.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_l2.py index d20a76a88..95938895a 100644 --- a/rl_engine/validation/models/qwen3_next_l2.py +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_l2.py @@ -907,7 +907,7 @@ def _validate_report_artifacts(report, *, roles, expected_identity, artifact_roo identity = report["identity"] if expected_identity is not None and identity != expected_identity: raise ValueError("Model matrix source/environment/provider identity mismatch") - from rl_engine.validation.models.qwen3_next_binary import validate_binary_record + from rl_engine.validation.models.qwen3_next.qwen3_next_binary import validate_binary_record binary = validate_binary_record( report.get("provider_binary"), diff --git a/rl_engine/validation/models/qwen3_next_layer_cases.json b/rl_engine/validation/models/qwen3_next/qwen3_next_layer_cases.json similarity index 87% rename from rl_engine/validation/models/qwen3_next_layer_cases.json rename to rl_engine/validation/models/qwen3_next/qwen3_next_layer_cases.json index 4e1af4c67..3914f4eeb 100644 --- a/rl_engine/validation/models/qwen3_next_layer_cases.json +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_layer_cases.json @@ -2,7 +2,7 @@ "schema_version": 1, "gates": { "gdn": { - "script": "tools/validation/models/qwen3_next_tp_gdn_check.py", + "script": "tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py", "output_option": "--output", "scope": "tp4_gdn_layer0", "groups": { @@ -18,7 +18,7 @@ } }, "attention": { - "script": "tools/validation/models/qwen3_next_tp_attention_check.py", + "script": "tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py", "output_option": "--output", "scope": "tp4_full_attention_layer3", "groups": { @@ -37,7 +37,7 @@ } }, "moe": { - "script": "tools/validation/models/qwen3_next_tp_moe_check.py", + "script": "tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py", "output_option": "--output", "scope": "tp4_moe_block_layer0", "groups": { @@ -58,7 +58,7 @@ } }, "adapters": { - "script": "tools/validation/models/qwen3_next_adapter_check.py", + "script": "tools/validation/models/qwen3_next/qwen3_next_adapter_check.py", "output_option": "--out", "scope": "gdn_engine_adapter_component", "groups": { @@ -81,7 +81,7 @@ } }, "heads": { - "script": "tools/validation/models/qwen3_next_tp_head_check.py", + "script": "tools/validation/models/qwen3_next/qwen3_next_tp_head_check.py", "output_option": "--output", "scope": "tp4_embedding_lm_head_only", "groups": { diff --git a/rl_engine/validation/models/qwen3_next_operator_cases.json b/rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json similarity index 99% rename from rl_engine/validation/models/qwen3_next_operator_cases.json rename to rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json index 101ad30f1..1d8582f8e 100644 --- a/rl_engine/validation/models/qwen3_next_operator_cases.json +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json @@ -1,6 +1,6 @@ { "schema_version": 1, - "description": "Mandatory norm/core contracts; alternative-provider approximate comparisons are diagnostics only. Generated by tools/validation/models/qwen3_next_operator_cases.py; case changes require review before a frozen run.", + "description": "Mandatory norm/core contracts; alternative-provider approximate comparisons are diagnostics only. Generated by tools/validation/models/qwen3_next/qwen3_next_operator_cases.py; case changes require review before a frozen run.", "suites": { "norm": [ "tests/models/qwen3_next/check_qwen3_next_norm_providers.py::test_custom_op_has_distinct_native_and_cuda_paths", diff --git a/rl_engine/validation/models/qwen3_next_rl_fixture.py b/rl_engine/validation/models/qwen3_next/qwen3_next_rl_fixture.py similarity index 100% rename from rl_engine/validation/models/qwen3_next_rl_fixture.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_rl_fixture.py diff --git a/tests/models/qwen3_next/check_qwen3_next_native_rollout.py b/tests/models/qwen3_next/check_qwen3_next_native_rollout.py index 67e7cd08d..10f6f2c3f 100644 --- a/tests/models/qwen3_next/check_qwen3_next_native_rollout.py +++ b/tests/models/qwen3_next/check_qwen3_next_native_rollout.py @@ -9,7 +9,7 @@ import torch from rl_engine.integrations.engines.rollout.vllm import qwen3_next_native_rollout as rollout -from rl_engine.validation.models.qwen3_next_l2 import L2Case, json_digest +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import L2Case, json_digest def request(batch=4, length=8, chunked=False, frozen=False): diff --git a/tests/models/qwen3_next/test_qwen3_next_binary_evidence.py b/tests/models/qwen3_next/test_qwen3_next_binary_evidence.py index 19cf4309c..c73e523fe 100644 --- a/tests/models/qwen3_next/test_qwen3_next_binary_evidence.py +++ b/tests/models/qwen3_next/test_qwen3_next_binary_evidence.py @@ -9,7 +9,7 @@ import pytest -from rl_engine.validation.models.qwen3_next_binary import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( archive_provider_binary, validate_binary_record, verify_imported_provider, diff --git a/tests/models/qwen3_next/test_qwen3_next_case_manifest.py b/tests/models/qwen3_next/test_qwen3_next_case_manifest.py index 7d78a236f..9e9e077c1 100644 --- a/tests/models/qwen3_next/test_qwen3_next_case_manifest.py +++ b/tests/models/qwen3_next/test_qwen3_next_case_manifest.py @@ -10,7 +10,11 @@ import pytest -from rl_engine.validation.models.qwen3_next_cases import case_digest, execution_verdict, load_cases +from rl_engine.validation.models.qwen3_next.qwen3_next_cases import ( + case_digest, + execution_verdict, + load_cases, +) CASE = "tests/example.py::test_required" SUCCESS = {"setup": "passed", "call": "passed", "teardown": "passed"} diff --git a/tests/models/qwen3_next/test_qwen3_next_chain_report.py b/tests/models/qwen3_next/test_qwen3_next_chain_report.py index 1d3164404..7f586037b 100644 --- a/tests/models/qwen3_next/test_qwen3_next_chain_report.py +++ b/tests/models/qwen3_next/test_qwen3_next_chain_report.py @@ -9,8 +9,8 @@ import torch from rl_engine.contracts.numerical import load_contract -from rl_engine.validation.models import qwen3_next_chain_report as chain from rl_engine.validation.models.chain_gate import ChainGateReport +from rl_engine.validation.models.qwen3_next import qwen3_next_chain_report as chain CASES = ("p0008-b1-full", "p0008-b1-chunked") diff --git a/tests/models/qwen3_next/test_qwen3_next_checkpoint.py b/tests/models/qwen3_next/test_qwen3_next_checkpoint.py index 95672f6a1..682347a2c 100644 --- a/tests/models/qwen3_next/test_qwen3_next_checkpoint.py +++ b/tests/models/qwen3_next/test_qwen3_next_checkpoint.py @@ -9,7 +9,7 @@ import pytest -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) from qwen3_next_checkpoint import ( # noqa: E402 newest_complete_checkpoint, publish_checkpoint, diff --git a/tests/models/qwen3_next/test_qwen3_next_environment_identity.py b/tests/models/qwen3_next/test_qwen3_next_environment_identity.py index 26d3e7231..bb4233f3e 100644 --- a/tests/models/qwen3_next/test_qwen3_next_environment_identity.py +++ b/tests/models/qwen3_next/test_qwen3_next_environment_identity.py @@ -4,7 +4,7 @@ import sys from pathlib import Path -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) from qwen3_next_runtime import current_identity, installation_roots # noqa: E402 diff --git a/tests/models/qwen3_next/test_qwen3_next_evidence_redaction.py b/tests/models/qwen3_next/test_qwen3_next_evidence_redaction.py index 5ceb298e3..29ac2a731 100644 --- a/tests/models/qwen3_next/test_qwen3_next_evidence_redaction.py +++ b/tests/models/qwen3_next/test_qwen3_next_evidence_redaction.py @@ -11,7 +11,7 @@ import pytest ROOT = Path(__file__).resolve().parents[3] -sys.path.insert(0, str(ROOT / "tools/validation/models")) +sys.path.insert(0, str(ROOT / "tools/validation/models/qwen3_next")) import qwen3_next_publish_evidence as publisher # noqa: E402 @@ -33,6 +33,7 @@ def published_files(): roots = [ *ROOT.glob("docs/usage/evidence/qwen3-next-*"), + ROOT / "reports/experiments/qwen3_next", *ROOT.glob("docs/operators/qwen3-next-*.md"), *ROOT.glob("docs/usage/qwen3-next-*.md"), ] diff --git a/tests/models/qwen3_next/test_qwen3_next_failure.py b/tests/models/qwen3_next/test_qwen3_next_failure.py index 21de8b2c1..dcb6c3f0e 100644 --- a/tests/models/qwen3_next/test_qwen3_next_failure.py +++ b/tests/models/qwen3_next/test_qwen3_next_failure.py @@ -11,7 +11,7 @@ import pytest -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) from qwen3_next_runtime import failure_kind, subprocess_failure # noqa: E402 diff --git a/tests/models/qwen3_next/test_qwen3_next_installed_provider_identity.py b/tests/models/qwen3_next/test_qwen3_next_installed_provider_identity.py index 9bfb87677..9db266b82 100644 --- a/tests/models/qwen3_next/test_qwen3_next_installed_provider_identity.py +++ b/tests/models/qwen3_next/test_qwen3_next_installed_provider_identity.py @@ -9,7 +9,7 @@ import pytest -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) from qwen3_next_runtime import installed_provider_sources # noqa: E402 diff --git a/tests/models/qwen3_next/test_qwen3_next_l2.py b/tests/models/qwen3_next/test_qwen3_next_l2.py index 9c213ff3f..7d9c99ae9 100644 --- a/tests/models/qwen3_next/test_qwen3_next_l2.py +++ b/tests/models/qwen3_next/test_qwen3_next_l2.py @@ -10,7 +10,7 @@ import pytest import torch -from rl_engine.validation.models.qwen3_next_l2 import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( PROMPT_LENGTHS, RESPONSE_LENGTH, VOCAB_SIZE, @@ -231,7 +231,7 @@ def test_first_divergence_tracks_state_even_when_final_logits_match(small_artifa "node", ["input_layernorm", "mixer", "post_attention_layernorm", "router", "mlp"] ) def test_first_divergent_node_names_the_sub_node(small_artifact, node): - from rl_engine.validation.models.qwen3_next_l2 import first_divergent_node + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import first_divergent_node _, artifact = small_artifact changed = copy.deepcopy(artifact["layer_digests"]) @@ -243,7 +243,7 @@ def test_first_divergent_node_names_the_sub_node(small_artifact, node): def test_first_divergent_node_starts_at_the_embedding(small_artifact): - from rl_engine.validation.models.qwen3_next_l2 import first_divergent_node + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import first_divergent_node _, artifact = small_artifact changed = copy.deepcopy(artifact["layer_digests"]) @@ -257,7 +257,7 @@ def test_first_divergent_node_starts_at_the_embedding(small_artifact): def test_first_divergent_node_falls_back_to_layer_output_and_state(small_artifact): - from rl_engine.validation.models.qwen3_next_l2 import first_divergent_node + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import first_divergent_node _, artifact = small_artifact assert first_divergent_node(artifact, artifact) is None @@ -312,7 +312,9 @@ def test_only_initial_rollout_can_generate_fixture(frozen): def test_native_worker_absence_fails_readiness(monkeypatch): - sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) + sys.path.insert( + 0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next") + ) import qwen3_next_l2 as runner def absent(name): @@ -324,7 +326,9 @@ def absent(name): def test_gpu_release_never_cancels_an_unrelated_process(monkeypatch): - sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) + sys.path.insert( + 0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next") + ) import qwen3_next_l2 as runner commands = [] @@ -344,7 +348,9 @@ def test_batch_callback_loads_once_and_freezes_before_recompute_cases(tmp_path, import json from types import SimpleNamespace - sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) + sys.path.insert( + 0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next") + ) import qwen3_next_l2 as runner calls = [] @@ -424,7 +430,7 @@ def eager_recompute(cases): def test_passed_flag_cannot_replace_full_matrix(): - from rl_engine.validation.models.qwen3_next_l2 import validate_matrix_report + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_matrix_report with pytest.raises(ValueError, match="sixteen-case"): validate_matrix_report({"status": "passed", "raw_bit_comparison": True}) @@ -484,7 +490,7 @@ def deep_verify(path, expected): def test_debug_capture_must_contain_matching_layer_state(small_artifact): - from rl_engine.validation.models.qwen3_next_l2 import validate_debug_capture + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_debug_capture request, artifact = small_artifact request = {**request, "trace_layers": [2]} @@ -509,7 +515,7 @@ def test_debug_capture_must_contain_matching_layer_state(small_artifact): def test_model_artifact_hash_is_rechecked_before_deserialization(tmp_path): from qwen3_next_runtime import file_digest - from rl_engine.validation.models.qwen3_next_l2 import checked_artifact_path + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import checked_artifact_path artifact = tmp_path / "output.pt" save_tensor_artifact(artifact, {"logits": torch.zeros(2, dtype=torch.bfloat16)}) @@ -562,7 +568,9 @@ def scheduler_request_and_audit(frozen): def test_actual_scheduler_must_follow_requested_chunks_and_offsets(frozen): - from rl_engine.validation.models.qwen3_next_l2 import validate_native_scheduler_execution + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( + validate_native_scheduler_execution, + ) request, artifact = scheduler_request_and_audit(frozen) validate_native_scheduler_execution(artifact, request) @@ -577,7 +585,9 @@ def test_actual_scheduler_must_follow_requested_chunks_and_offsets(frozen): def test_scheduler_cannot_shrink_cache_or_claim_certified_execution(frozen): - from rl_engine.validation.models.qwen3_next_l2 import validate_native_scheduler_execution + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( + validate_native_scheduler_execution, + ) request, artifact = scheduler_request_and_audit(frozen) artifact["native_runtime_profile"]["kv_cache_memory_bytes_per_gpu"] //= 2 @@ -590,7 +600,7 @@ def test_scheduler_cannot_shrink_cache_or_claim_certified_execution(frozen): def test_layer_hashes_cannot_replace_four_original_worker_traces(frozen): - from rl_engine.validation.models.qwen3_next_l2 import validate_native_worker_traces + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_native_worker_traces request, artifact = scheduler_request_and_audit(frozen) with pytest.raises(ValueError, match="each TP rank"): @@ -602,11 +612,14 @@ def test_matrix_recomputes_tensor_comparisons_even_after_reports_are_rehashed(tm from qwen3_next_runtime import file_digest - from rl_engine.validation.models.qwen3_next_binary import ( + from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( archive_provider_binary, validate_binary_record, ) - from rl_engine.validation.models.qwen3_next_l2 import json_digest, validate_matrix_report + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( + json_digest, + validate_matrix_report, + ) source = tmp_path / "source" (source / "rl_engine").mkdir(parents=True) diff --git a/tests/models/qwen3_next/test_qwen3_next_layers.py b/tests/models/qwen3_next/test_qwen3_next_layers.py index b3bf66e88..c8160d3d8 100644 --- a/tests/models/qwen3_next/test_qwen3_next_layers.py +++ b/tests/models/qwen3_next/test_qwen3_next_layers.py @@ -11,7 +11,7 @@ import pytest ROOT = Path(__file__).resolve().parents[3] -sys.path.insert(0, str(ROOT / "tools/validation/models")) +sys.path.insert(0, str(ROOT / "tools/validation/models/qwen3_next")) from qwen3_next_layer_contract import ( # noqa: E402 checked_record, @@ -86,7 +86,7 @@ def test_artifact_hash_and_attempt_escape_are_rejected(tmp_path): def test_complete_component_flags_cannot_replace_native_scheduler(tmp_path): - from rl_engine.validation.models.qwen3_next_binary import ( + from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( archive_provider_binary, validate_binary_record, ) diff --git a/tests/models/qwen3_next/test_qwen3_next_native_recompute.py b/tests/models/qwen3_next/test_qwen3_next_native_recompute.py index 00e0dd3a8..1362676f5 100644 --- a/tests/models/qwen3_next/test_qwen3_next_native_recompute.py +++ b/tests/models/qwen3_next/test_qwen3_next_native_recompute.py @@ -57,7 +57,7 @@ def test_recompute_batch_loads_once_and_keeps_verifiable_original_rank_traces( from rl_engine.integrations.orchestrators.vime.experiments.qwen3_next import ( native_recompute as recompute, ) - from rl_engine.validation.models.qwen3_next_l2 import ( + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( load_tensor_artifact, save_tensor_artifact, validate_native_worker_traces, @@ -216,7 +216,7 @@ def spawn(target, *, args, nprocs, join): @pytest.mark.parametrize("batch", [1, 4]) @pytest.mark.parametrize("prompt_length", [8, 64, 256, 1024]) def test_complete_native_state_shapes_bind_every_consumed_token(batch, prompt_length): - from rl_engine.validation.models.qwen3_next_l2 import native_state_shapes + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import native_state_shapes request = { "prompt_tokens": [[1] * prompt_length for _ in range(batch)], diff --git a/tests/models/qwen3_next/test_qwen3_next_operator_gates.py b/tests/models/qwen3_next/test_qwen3_next_operator_gates.py index 9f2832fa0..819bd56a1 100644 --- a/tests/models/qwen3_next/test_qwen3_next_operator_gates.py +++ b/tests/models/qwen3_next/test_qwen3_next_operator_gates.py @@ -8,8 +8,8 @@ import pytest -from rl_engine.validation.models.qwen3_next_cases import execution_verdict -from tools.validation.models import qwen3_next_operator_gates as gates +from rl_engine.validation.models.qwen3_next.qwen3_next_cases import execution_verdict +from tools.validation.models.qwen3_next import qwen3_next_operator_gates as gates CASE = "tests/example.py::test_required" diff --git a/tests/models/qwen3_next/test_qwen3_next_profile.py b/tests/models/qwen3_next/test_qwen3_next_profile.py index 7013c7f0b..f3c2bb599 100644 --- a/tests/models/qwen3_next/test_qwen3_next_profile.py +++ b/tests/models/qwen3_next/test_qwen3_next_profile.py @@ -127,7 +127,7 @@ def test_updated_export_cannot_be_labeled_as_baseline(updated_identity): def test_historical_export_identity_checks_do_not_open_deleted_shards(updated_identity): - from rl_engine.validation.models.qwen3_next_l2 import validate_weight_identity + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_weight_identity identity, _ = updated_identity validate_weight_identity(identity) diff --git a/tests/models/qwen3_next/test_qwen3_next_rl_fixture.py b/tests/models/qwen3_next/test_qwen3_next_rl_fixture.py index 31a823368..d7994ce25 100644 --- a/tests/models/qwen3_next/test_qwen3_next_rl_fixture.py +++ b/tests/models/qwen3_next/test_qwen3_next_rl_fixture.py @@ -6,7 +6,7 @@ import pytest import torch -from rl_engine.validation.models.qwen3_next_rl_fixture import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_rl_fixture import ( OPTIMIZER_SETTINGS, POLICY_SETTINGS, PolicyFixture, diff --git a/tests/models/qwen3_next/test_qwen3_next_run.py b/tests/models/qwen3_next/test_qwen3_next_run.py index 3b99d763e..ab24383dc 100644 --- a/tests/models/qwen3_next/test_qwen3_next_run.py +++ b/tests/models/qwen3_next/test_qwen3_next_run.py @@ -9,14 +9,14 @@ import pytest -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) import qwen3_next_run as run # noqa: E402 def manifest(tmp_path, stage="layers", script=None): source = tmp_path / "source" - (source / "tools/validation/models").mkdir(parents=True, exist_ok=True) + (source / "tools/validation/models/qwen3_next").mkdir(parents=True, exist_ok=True) if script is not None: (source / run.STAGES[stage][3]).write_text(script) return { @@ -44,7 +44,9 @@ def frozen(monkeypatch): def test_prepare_refuses_a_dirty_tree(monkeypatch, tmp_path): monkeypatch.setattr( - run, "_git", lambda *args, cwd=None: " M tools/validation/models/qwen3_next_run.py" + run, + "_git", + lambda *args, cwd=None: " M tools/validation/models/qwen3_next/qwen3_next_run.py", ) with pytest.raises(SystemExit, match="Commit the source"): run.prepare("preflight", None, None) diff --git a/tests/models/qwen3_next/test_qwen3_next_storage.py b/tests/models/qwen3_next/test_qwen3_next_storage.py index e897ab7af..84d46d2c7 100644 --- a/tests/models/qwen3_next/test_qwen3_next_storage.py +++ b/tests/models/qwen3_next/test_qwen3_next_storage.py @@ -12,7 +12,7 @@ import pytest -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) import qwen3_next_runtime as storage # noqa: E402 from qwen3_next_checkpoint import ( # noqa: E402 newest_complete_checkpoint, diff --git a/tests/models/qwen3_next/test_qwen3_next_training_checkpoint.py b/tests/models/qwen3_next/test_qwen3_next_training_checkpoint.py index f486a09b5..697918b0e 100644 --- a/tests/models/qwen3_next/test_qwen3_next_training_checkpoint.py +++ b/tests/models/qwen3_next/test_qwen3_next_training_checkpoint.py @@ -16,7 +16,7 @@ import torch.distributed as dist import torch.multiprocessing as mp -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) import qwen3_next_optimizer_checkpoint as optimizer_checkpoint # noqa: E402 import qwen3_next_training_checkpoint as checkpoint # noqa: E402 from qwen3_next_checkpoint import publish_checkpoint # noqa: E402 @@ -25,7 +25,7 @@ capture_rng_state, state_digest, ) -from rl_engine.validation.models.qwen3_next_rl_fixture import PolicyFixture # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_rl_fixture import PolicyFixture # noqa: E402 IDENTITY = {"source": "pinned", "provider": "strict", "checkpoint": "official", "tp": 4} diff --git a/tests/models/qwen3_next/test_qwen3_next_training_closeout.py b/tests/models/qwen3_next/test_qwen3_next_training_closeout.py index 4f0e41e0e..a81fdb6af 100644 --- a/tests/models/qwen3_next/test_qwen3_next_training_closeout.py +++ b/tests/models/qwen3_next/test_qwen3_next_training_closeout.py @@ -39,7 +39,7 @@ def test_requeue_repeats_final_reload_without_reusing_stale_attempt(monkeypatch) assert not runner.repeat_reload_in_current_attempt(journal, "1") -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) import qwen3_next_training as runner # noqa: E402 from rl_engine.integrations.orchestrators.vime.experiments.qwen3_next import ( # noqa: E402 @@ -376,7 +376,7 @@ def test_partial_training_evidence_uses_the_phase_export_identity(tmp_path, monk import qwen3_next_contract as contract import qwen3_next_l2 - from rl_engine.validation.models import qwen3_next_l2 as l2_testing + from rl_engine.validation.models.qwen3_next import qwen3_next_l2 as l2_testing manifest = {"stage": "train1", "checkpoint": "/official"} export = {"checkpoint": "/export", "weight_version": "update-1", "base_checkpoint": "/official"} diff --git a/tests/models/qwen3_next/test_qwen3_next_training_export.py b/tests/models/qwen3_next/test_qwen3_next_training_export.py index bec03f57a..73117908f 100644 --- a/tests/models/qwen3_next/test_qwen3_next_training_export.py +++ b/tests/models/qwen3_next/test_qwen3_next_training_export.py @@ -13,7 +13,7 @@ import torch from safetensors.torch import save_file -sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models")) +sys.path.insert(0, str(Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next")) import qwen3_next_training_export as export # noqa: E402 from qwen3_next_runtime import file_digest # noqa: E402 diff --git a/tests/validation/common/test_file_digest.py b/tests/validation/common/test_file_digest.py index e9dae4a2b..22ef42a87 100644 --- a/tests/validation/common/test_file_digest.py +++ b/tests/validation/common/test_file_digest.py @@ -27,7 +27,7 @@ def test_runtime_digest_does_not_import_torch(tmp_path): import sys from pathlib import Path - runtime = Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next_runtime.py" + runtime = Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next/qwen3_next_runtime.py" artifact = tmp_path / "artifact.bin" artifact.write_bytes(b"standalone evidence runtime") program = """ diff --git a/tools/validation/models/plot_qwen3_next_chain.py b/tools/validation/models/plot_qwen3_next_chain.py deleted file mode 100644 index f3eab8718..000000000 --- a/tools/validation/models/plot_qwen3_next_chain.py +++ /dev/null @@ -1,152 +0,0 @@ -#!/usr/bin/env python -# SPDX-License-Identifier: Apache-2.0 -# Copyright (c) 2026 RL-Kernel Contributors - -"""Render a published Qwen3-Next full-model chain as a PNG. - -Needs only the published evidence directory and matplotlib: - - python tools/validation/models/plot_qwen3_next_chain.py \\ - docs/usage/evidence/qwen3-next-chain-b200 - -writes ``figure.png`` into that directory. Left: for each L2 case and weight -version, whether the vLLM rollout and the VIME recompute agree bit for bit on -response tokens, logits, logprobs and the per-layer digests. Right: for each -decoder layer, the share of cases whose layer digest changes from one weight -version to the next, which shows the updates reach every layer and the reload -reproduces update 2 exactly. -""" - -from __future__ import annotations - -import argparse -import json -from pathlib import Path - -import matplotlib - -matplotlib.use("Agg") -import matplotlib.pyplot as plt # noqa: E402 -import numpy as np # noqa: E402 -from matplotlib.colors import ListedColormap # noqa: E402 - -INK = "#2b2b29" -MUTED = "#6f6e69" -SURFACE = "#fcfcfb" -PASS = "#1baf7a" -FAIL = "#d64545" -CHANGE = ("#e4e3dd", "#2a78d6") - -STAGES = (("l2", "baseline"), ("train1", "update 1"), ("train2", "update 2"), ("train", "reload")) -COMPARISONS = ( - ("response_tokens", "tokens"), - ("logits", "logits"), - ("logprobs", "logprobs"), - ("layer_states", "layer digests"), -) -STEPS = (("baseline", "update1"), ("update1", "update2"), ("update2", "reload")) - - -def _node_order(name): - return (0, 0) if name == "embedding" else (1, int(name.removeprefix("layer"))) - - -def plot(evidence: Path, out: Path) -> None: - matrices = { - label: json.loads((evidence / stage / "l2_matrix.json").read_text()) - for stage, label in STAGES - } - cases = matrices["baseline"]["case_ids"] - grid = np.array( - [ - [ - bool(matrices[label]["cases"][case]["comparisons"][key]) - for _, label in STAGES - for key, _ in COMPARISONS - ] - for case in cases - ] - ) - report = json.loads((evidence / "chain-report.json").read_text()) - cells = report["cells"] - nodes = sorted(cells[f"baseline/{cases[0]}"]["node_digests"], key=_node_order) - change = np.array( - [ - [ - np.mean( - [ - cells[f"{a}/{case}"]["node_digests"][node] - != cells[f"{b}/{case}"]["node_digests"][node] - for case in cases - ] - ) - for node in nodes - ] - for a, b in STEPS - ] - ) - - fig, (left, right) = plt.subplots( - 1, 2, figsize=(16, 6.2), gridspec_kw={"width_ratios": [1, 1.55]}, facecolor=SURFACE - ) - left.imshow(grid, cmap=ListedColormap([FAIL, PASS]), vmin=0, vmax=1, aspect="auto") - left.set_yticks(range(len(cases)), cases, fontsize=8, color=INK) - names = [name for _, name in COMPARISONS] - left.set_xticks(range(grid.shape[1]), names * len(STAGES), rotation=90, fontsize=8) - for i, (_, label) in enumerate(STAGES): - left.text(i * 4 + 1.5, -1.2, label, ha="center", fontsize=9, color=INK) - if i: - left.axvline(i * 4 - 0.5, color=SURFACE, linewidth=3) - left.set_title( - f"vLLM rollout vs VIME recompute, raw bits\n{int(grid.sum())}/{grid.size} equal", - fontsize=10, - color=INK, - pad=24, - ) - - right.imshow(change, cmap=ListedColormap(CHANGE), vmin=0, vmax=1, aspect="auto") - right.set_yticks(range(len(STEPS)), [f"{a} → {b}" for a, b in STEPS], fontsize=9, color=INK) - labels = ["emb" if n == "embedding" else n.removeprefix("layer") for n in nodes] - right.set_xticks(range(len(nodes)), labels, fontsize=6, color=MUTED) - right.set_xlabel("embedding and decoder layers 0-47", fontsize=9, color=MUTED) - for row in range(len(STEPS)): - right.text( - len(nodes) + 0.6, - row, - f"{change[row].mean():.0%} of digests change", - va="center", - fontsize=8, - color=INK, - ) - right.set_xlim(-0.5, len(nodes) + 9) - right.set_title( - "Per-layer digest change between weight versions (share of the 16 cases)", - fontsize=10, - color=INK, - ) - for axis in (left, right): - for spine in axis.spines.values(): - spine.set_visible(False) - commit = report["measured_commit"][:7] - gpu = f" on {report['gpu_name']}" if report.get("gpu_name") else "" - fig.suptitle( - f"Qwen3-Next-80B-A3B full-model chain, TP4{gpu}, commit {commit}: " - f"passed={report['passed']}, first drift={report['first_drift']}", - fontsize=11, - color=INK, - ) - fig.tight_layout() - fig.savefig(out, dpi=160) - - -def main() -> None: - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("evidence", type=Path) - args = parser.parse_args() - out = args.evidence / "figure.png" - plot(args.evidence, out) - print(f"wrote {out}") - - -if __name__ == "__main__": - main() diff --git a/tools/validation/models/qwen3_next_adapter_check.py b/tools/validation/models/qwen3_next/qwen3_next_adapter_check.py similarity index 100% rename from tools/validation/models/qwen3_next_adapter_check.py rename to tools/validation/models/qwen3_next/qwen3_next_adapter_check.py diff --git a/tools/validation/models/qwen3_next_chain_report.py b/tools/validation/models/qwen3_next/qwen3_next_chain_report.py similarity index 91% rename from tools/validation/models/qwen3_next_chain_report.py rename to tools/validation/models/qwen3_next/qwen3_next_chain_report.py index 99f19ea76..b1ffd5883 100644 --- a/tools/validation/models/qwen3_next_chain_report.py +++ b/tools/validation/models/qwen3_next/qwen3_next_chain_report.py @@ -4,13 +4,13 @@ """Write the WS1 chain-gate report for a finished Qwen3-Next chain. - python tools/validation/models/qwen3_next_chain_report.py \ + python tools/validation/models/qwen3_next/qwen3_next_chain_report.py \ --last --out chain-report.json Follows ``previous`` from the given run back to the ``l2`` stage, reads every passed stage's L2 matrix (``baseline`` from ``l2``, ``update1``/``update2`` from ``train1``/``train2``, ``reload`` from ``train``) and the hash-checked case -artifacts it names, and writes ``rl_engine.validation.models.qwen3_next_chain_report``'s +artifacts it names, and writes ``rl_engine.validation.models.qwen3_next.qwen3_next_chain_report``'s report. Exits 1 when the chain did not pass. """ @@ -19,12 +19,14 @@ import sys from pathlib import Path -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(ROOT)) from rl_engine.contracts.numerical import load_contract # noqa: E402 -from rl_engine.validation.models.qwen3_next_chain_report import build_report # noqa: E402 -from rl_engine.validation.models.qwen3_next_l2 import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_chain_report import ( # noqa: E402 + build_report, +) +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( # noqa: E402 checked_artifact_path, load_tensor_artifact, ) diff --git a/tools/validation/models/qwen3_next_checkpoint.py b/tools/validation/models/qwen3_next/qwen3_next_checkpoint.py similarity index 100% rename from tools/validation/models/qwen3_next_checkpoint.py rename to tools/validation/models/qwen3_next/qwen3_next_checkpoint.py diff --git a/tools/validation/models/qwen3_next_contract.py b/tools/validation/models/qwen3_next/qwen3_next_contract.py similarity index 97% rename from tools/validation/models/qwen3_next_contract.py rename to tools/validation/models/qwen3_next/qwen3_next_contract.py index 428746bfc..8826a4907 100644 --- a/tools/validation/models/qwen3_next_contract.py +++ b/tools/validation/models/qwen3_next/qwen3_next_contract.py @@ -46,7 +46,7 @@ def checkpoint_identity(checkpoint): index = json.loads((checkpoint / "model.safetensors.index.json").read_text()) reference = json.loads( ( - Path(__file__).resolve().parents[3] + Path(__file__).resolve().parents[4] / "rl_engine/validation/models/qwen3_next_norm_manifest.json" ).read_text() )["model_identity"] @@ -81,7 +81,9 @@ def checkpoint_identity(checkpoint): def operator_requirements(root, suite): registry = json.loads( - (Path(root) / "rl_engine/validation/models/qwen3_next_operator_cases.json").read_text() + ( + Path(root) / "rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json" + ).read_text() ) if registry.get("schema_version") != 1: raise ValueError("Unsupported mandatory operator case registry") @@ -154,7 +156,7 @@ def validate_partial_training_evidence(manifest, root, evidence, *, attempt_root from qwen3_next_l2 import model_identity from qwen3_next_training import STAGE_LAST_PHASE, read_record, stage_phases - from rl_engine.validation.models.qwen3_next_l2 import validate_matrix_report + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_matrix_report stage = manifest["stage"] progress = json.loads((root / evidence["updates"]["path"]).read_text()) @@ -310,7 +312,9 @@ def validate_result(manifest, result, *, run_dir=None, attempt=None): root=attempt_root, ) else: - from rl_engine.validation.models.qwen3_next_l2 import validate_matrix_report + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( + validate_matrix_report, + ) certificate = manifest.get("scheduler_profile_certificate", {}) certificate_path = Path(certificate.get("path", "")) diff --git a/tools/validation/models/qwen3_next_l2.py b/tools/validation/models/qwen3_next/qwen3_next_l2.py similarity index 98% rename from tools/validation/models/qwen3_next_l2.py rename to tools/validation/models/qwen3_next/qwen3_next_l2.py index cb874ca0e..75d0d93a9 100644 --- a/tools/validation/models/qwen3_next_l2.py +++ b/tools/validation/models/qwen3_next/qwen3_next_l2.py @@ -14,7 +14,7 @@ import time from pathlib import Path -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(ROOT)) from qwen3_next_contract import ( # noqa: E402 @@ -34,12 +34,12 @@ verify_environment, ) -from rl_engine.validation.models.qwen3_next_binary import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( # noqa: E402 archive_provider_binary, validate_binary_record, verify_imported_provider, ) -from rl_engine.validation.models.qwen3_next_l2 import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( # noqa: E402 NATIVE_WORKER_MODULE, PROMPT_LENGTHS, L2Case, @@ -113,7 +113,7 @@ def model_identity(manifest): base_checkpoint=manifest["base_checkpoint"], base_checkpoint_identity=manifest["base_checkpoint_identity"], ) - from rl_engine.validation.models.qwen3_next_l2 import validate_weight_identity + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_weight_identity validate_weight_identity(identity) return identity @@ -213,7 +213,7 @@ def cases(): if fixture is None: from transformers import AutoTokenizer - from rl_engine.validation.models.qwen3_next_l2 import freeze_responses + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import freeze_responses tokenizer = AutoTokenizer.from_pretrained( manifest["checkpoint"], local_files_only=True, trust_remote_code=False diff --git a/tools/validation/models/qwen3_next_layer_contract.py b/tools/validation/models/qwen3_next/qwen3_next_layer_contract.py similarity index 94% rename from tools/validation/models/qwen3_next_layer_contract.py rename to tools/validation/models/qwen3_next/qwen3_next_layer_contract.py index 67764821d..4b27d4b6a 100644 --- a/tools/validation/models/qwen3_next_layer_contract.py +++ b/tools/validation/models/qwen3_next/qwen3_next_layer_contract.py @@ -12,7 +12,7 @@ def layer_requirements(root): root = Path(root) - path = root / "rl_engine/validation/models/qwen3_next_layer_cases.json" + path = root / "rl_engine/validation/models/qwen3_next/qwen3_next_layer_cases.json" registry = json.loads(path.read_text()) if registry.get("schema_version") != 1 or set(registry.get("gates", {})) != { "gdn", @@ -95,7 +95,7 @@ def validate_layer_report(report, requirement, identity, *, root): raise ValueError("Layer acceptance identity or completeness mismatch") if set(report.get("gates", {})) != set(requirement["gates"]): raise ValueError("Layer acceptance omitted a mandatory gate") - from rl_engine.validation.models.qwen3_next_binary import validate_binary_record + from rl_engine.validation.models.qwen3_next.qwen3_next_binary import validate_binary_record binary = validate_binary_record( report.get("provider_binary"), source_tree=identity["source_tree"], artifact_root=root @@ -114,7 +114,7 @@ def validate_layer_report(report, requirement, identity, *, root): raise ValueError("Layer ranks did not import the archived CUDA provider") scheduler_path = checked_record(report.get("scheduler"), root=root) scheduler = json.loads(scheduler_path.read_text()) - from rl_engine.validation.models.qwen3_next_l2 import validate_scheduler_report + from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import validate_scheduler_report validate_scheduler_report(scheduler, expected_identity=identity, artifact_root=root) if scheduler.get("provider_binary") != report["provider_binary"]: diff --git a/tools/validation/models/qwen3_next_layers.py b/tools/validation/models/qwen3_next/qwen3_next_layers.py similarity index 97% rename from tools/validation/models/qwen3_next_layers.py rename to tools/validation/models/qwen3_next/qwen3_next_layers.py index ea3c62a8d..e8578b9c6 100644 --- a/tools/validation/models/qwen3_next_layers.py +++ b/tools/validation/models/qwen3_next/qwen3_next_layers.py @@ -12,7 +12,7 @@ import sys from pathlib import Path -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(ROOT)) from qwen3_next_contract import ( # noqa: E402 @@ -43,11 +43,11 @@ verify_environment, ) -from rl_engine.validation.models.qwen3_next_binary import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( # noqa: E402 archive_provider_binary, validate_binary_record, ) -from rl_engine.validation.models.qwen3_next_l2 import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( # noqa: E402 L2Case, case_matrix, compare_batch_prefill, @@ -192,7 +192,7 @@ def progress(phase): run_process( [ manifest["python"], - str(ROOT / "tools/validation/models/qwen3_next_l2.py"), + str(ROOT / "tools/validation/models/qwen3_next/qwen3_next_l2.py"), "--gpu-probe", "--output", str(probe), diff --git a/tools/validation/models/qwen3_next_operator_cases.py b/tools/validation/models/qwen3_next/qwen3_next_operator_cases.py similarity index 87% rename from tools/validation/models/qwen3_next_operator_cases.py rename to tools/validation/models/qwen3_next/qwen3_next_operator_cases.py index d5784a5bd..7c9540ba8 100644 --- a/tools/validation/models/qwen3_next_operator_cases.py +++ b/tools/validation/models/qwen3_next/qwen3_next_operator_cases.py @@ -2,10 +2,11 @@ # SPDX-License-Identifier: Apache-2.0 # Copyright (c) 2026 RL-Kernel Contributors -"""Regenerate ``rl_engine/validation/models/qwen3_next_operator_cases.json`` from pytest collection. +"""Regenerate ``rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json`` from +pytest collection. - python tools/validation/models/qwen3_next_operator_cases.py # check, exit 1 if stale - python tools/validation/models/qwen3_next_operator_cases.py --write + python tools/validation/models/qwen3_next/qwen3_next_operator_cases.py + python tools/validation/models/qwen3_next/qwen3_next_operator_cases.py --write The registry pins the exact test IDs the preflight operator gate must run. The ``norm`` and ``core`` suites are mandatory and must pass without skips; @@ -21,8 +22,8 @@ import sys from pathlib import Path -ROOT = Path(__file__).resolve().parents[3] -REGISTRY = ROOT / "rl_engine/validation/models/qwen3_next_operator_cases.json" +ROOT = Path(__file__).resolve().parents[4] +REGISTRY = ROOT / "rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json" NORM_FILES = ( "tests/models/qwen3_next/test_qwen3_next_norm.py", "tests/models/qwen3_next/check_qwen3_next_norm_providers.py", @@ -53,7 +54,8 @@ } DESCRIPTION = ( "Mandatory norm/core contracts; alternative-provider approximate comparisons are " - "diagnostics only. Generated by tools/validation/models/qwen3_next_operator_cases.py; " + "diagnostics only. Generated by " + "tools/validation/models/qwen3_next/qwen3_next_operator_cases.py; " "case changes require review before a frozen run." ) diff --git a/tools/validation/models/qwen3_next_operator_gates.py b/tools/validation/models/qwen3_next/qwen3_next_operator_gates.py similarity index 95% rename from tools/validation/models/qwen3_next_operator_gates.py rename to tools/validation/models/qwen3_next/qwen3_next_operator_gates.py index 3af9522c9..a2612d239 100644 --- a/tools/validation/models/qwen3_next_operator_gates.py +++ b/tools/validation/models/qwen3_next/qwen3_next_operator_gates.py @@ -12,10 +12,14 @@ import sys from pathlib import Path -from rl_engine.validation.models.qwen3_next_cases import case_digest, execution_verdict, load_cases +from rl_engine.validation.models.qwen3_next.qwen3_next_cases import ( + case_digest, + execution_verdict, + load_cases, +) -ROOT = Path(__file__).resolve().parents[3] -MANIFEST = ROOT / "rl_engine/validation/models/qwen3_next_operator_cases.json" +ROOT = Path(__file__).resolve().parents[4] +MANIFEST = ROOT / "rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json" def run_suite(out: Path, suite: str) -> dict: diff --git a/tools/validation/models/qwen3_next_optimizer_checkpoint.py b/tools/validation/models/qwen3_next/qwen3_next_optimizer_checkpoint.py similarity index 100% rename from tools/validation/models/qwen3_next_optimizer_checkpoint.py rename to tools/validation/models/qwen3_next/qwen3_next_optimizer_checkpoint.py diff --git a/tools/validation/models/qwen3_next_preflight.py b/tools/validation/models/qwen3_next/qwen3_next_preflight.py similarity index 98% rename from tools/validation/models/qwen3_next_preflight.py rename to tools/validation/models/qwen3_next/qwen3_next_preflight.py index fcf312b96..1fea2cfc4 100644 --- a/tools/validation/models/qwen3_next_preflight.py +++ b/tools/validation/models/qwen3_next/qwen3_next_preflight.py @@ -179,7 +179,7 @@ def run(name, command, env=None): "torch.distributed.run", "--standalone", "--nproc_per_node=4", - "tools/validation/models/qwen3_next_tp_probe.py", + "tools/validation/models/qwen3_next/qwen3_next_tp_probe.py", "--output", str(output / "tp4"), ], @@ -206,7 +206,7 @@ def run(name, command, env=None): "rollout-smoke", [ sys.executable, - "tools/validation/models/qwen3_next_rollout_smoke.py", + "tools/validation/models/qwen3_next/qwen3_next_rollout_smoke.py", "--model", manifest["checkpoint"], "--output", diff --git a/tools/validation/models/qwen3_next_publish_evidence.py b/tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py similarity index 95% rename from tools/validation/models/qwen3_next_publish_evidence.py rename to tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py index 83ebd1e06..4b067e8b0 100644 --- a/tools/validation/models/qwen3_next_publish_evidence.py +++ b/tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py @@ -2,10 +2,11 @@ # SPDX-License-Identifier: Apache-2.0 # Copyright (c) 2026 RL-Kernel Contributors -"""Copy a finished Qwen3-Next chain's evidence into the repository, without site details. +"""Copy a finished Qwen3-Next chain's evidence into a directory to attach, without site details. - python tools/validation/models/qwen3_next_publish_evidence.py --last \\ - --chain-report chain-report.json --out docs/usage/evidence/qwen3-next-chain- + python tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py \\ + --last \\ + --chain-report chain-report.json --out artifacts/qwen3-next-chain- For every stage run on the chain (preflight -> ... -> the given run) this copies ``run.json``, ``result.json`` and every evidence file the result names (JSON diff --git a/tools/validation/models/qwen3_next_rollout_smoke.py b/tools/validation/models/qwen3_next/qwen3_next_rollout_smoke.py similarity index 100% rename from tools/validation/models/qwen3_next_rollout_smoke.py rename to tools/validation/models/qwen3_next/qwen3_next_rollout_smoke.py diff --git a/tools/validation/models/qwen3_next_run.py b/tools/validation/models/qwen3_next/qwen3_next_run.py similarity index 99% rename from tools/validation/models/qwen3_next_run.py rename to tools/validation/models/qwen3_next/qwen3_next_run.py index b36207e8f..44d94cc93 100644 --- a/tools/validation/models/qwen3_next_run.py +++ b/tools/validation/models/qwen3_next/qwen3_next_run.py @@ -4,7 +4,7 @@ """Run the Qwen3-Next full-model chain inside any allocation with four B200 GPUs. - python tools/validation/models/qwen3_next_run.py --stage all \\ + python tools/validation/models/qwen3_next/qwen3_next_run.py --stage all \\ --runs-root --checkpoint --cuda-home Stages run in order, each in its own run directory and its own clean clone of @@ -52,7 +52,7 @@ wait_owned_child, ) -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] ORDER = ("preflight", "layers", "l2", "train1", "train2", "train") PROVIDER_SOURCES = ( "rl_engine/integrations/engines/train/vllm/qwen3_next_provider.py", diff --git a/tools/validation/models/qwen3_next_runtime.py b/tools/validation/models/qwen3_next/qwen3_next_runtime.py similarity index 97% rename from tools/validation/models/qwen3_next_runtime.py rename to tools/validation/models/qwen3_next/qwen3_next_runtime.py index 82191e5b0..46edafa93 100644 --- a/tools/validation/models/qwen3_next_runtime.py +++ b/tools/validation/models/qwen3_next/qwen3_next_runtime.py @@ -6,7 +6,7 @@ Environment identity, failure classification, memory preconditions, owned child processes, checkpoint verification, stage ordering and crash-recoverable training storage. Nothing here talks to a batch scheduler: a run is identified -by the ``execution_id`` that ``tools/validation/models/qwen3_next_run.py`` writes into +by the ``execution_id`` that ``tools/validation/models/qwen3_next/qwen3_next_run.py`` writes into ``run.json``, and any allocation that provides four GPUs can host it. """ @@ -437,15 +437,40 @@ def wait_owned_child(child, interrupted, *, grace_seconds=10): # --------------------------------------------------------------------------- # STAGES = { - "preflight": ("256G", "01:00:00", None, "tools/validation/models/qwen3_next_preflight.py"), - "layers": ("512G", "04:00:00", "preflight", "tools/validation/models/qwen3_next_layers.py"), - "l2": ("512G", "04:00:00", "layers", "tools/validation/models/qwen3_next_l2.py"), + "preflight": ( + "256G", + "01:00:00", + None, + "tools/validation/models/qwen3_next/qwen3_next_preflight.py", + ), + "layers": ( + "512G", + "04:00:00", + "preflight", + "tools/validation/models/qwen3_next/qwen3_next_layers.py", + ), + "l2": ("512G", "04:00:00", "layers", "tools/validation/models/qwen3_next/qwen3_next_l2.py"), # The training acceptance is split so no single job holds a node for a day: # train1 = update1 + export + L2, train2 = update2 + export + L2 (continues # train1's durable state), train = reload + L2 + continuous/resumed controls. - "train1": ("1280G", "06:00:00", "l2", "tools/validation/models/qwen3_next_training.py"), - "train2": ("1280G", "07:00:00", "train1", "tools/validation/models/qwen3_next_training.py"), - "train": ("1280G", "09:00:00", "train2", "tools/validation/models/qwen3_next_training.py"), + "train1": ( + "1280G", + "06:00:00", + "l2", + "tools/validation/models/qwen3_next/qwen3_next_training.py", + ), + "train2": ( + "1280G", + "07:00:00", + "train1", + "tools/validation/models/qwen3_next/qwen3_next_training.py", + ), + "train": ( + "1280G", + "09:00:00", + "train2", + "tools/validation/models/qwen3_next/qwen3_next_training.py", + ), } diff --git a/tools/validation/models/qwen3_next_tp_head_check.py b/tools/validation/models/qwen3_next/qwen3_next_tp_head_check.py similarity index 100% rename from tools/validation/models/qwen3_next_tp_head_check.py rename to tools/validation/models/qwen3_next/qwen3_next_tp_head_check.py diff --git a/tools/validation/models/qwen3_next_tp_probe.py b/tools/validation/models/qwen3_next/qwen3_next_tp_probe.py similarity index 100% rename from tools/validation/models/qwen3_next_tp_probe.py rename to tools/validation/models/qwen3_next/qwen3_next_tp_probe.py diff --git a/tools/validation/models/qwen3_next_training.py b/tools/validation/models/qwen3_next/qwen3_next_training.py similarity index 99% rename from tools/validation/models/qwen3_next_training.py rename to tools/validation/models/qwen3_next/qwen3_next_training.py index dbedf2a4f..fb56620a1 100644 --- a/tools/validation/models/qwen3_next_training.py +++ b/tools/validation/models/qwen3_next/qwen3_next_training.py @@ -17,7 +17,7 @@ from pathlib import Path from uuid import uuid4 -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(ROOT)) from qwen3_next_checkpoint import ( # noqa: E402 @@ -60,11 +60,11 @@ from rl_engine.integrations.orchestrators.vime.providers.qwen3_next_training import ( # noqa: E402 state_digest, ) -from rl_engine.validation.models.qwen3_next_binary import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( # noqa: E402 archive_provider_binary, validate_binary_record, ) -from rl_engine.validation.models.qwen3_next_l2 import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( # noqa: E402 case_matrix, checked_artifact_path, compare_artifacts, @@ -823,7 +823,7 @@ def execute(manifest_path, output): run_process( [ manifest["python"], - str(ROOT / "tools/validation/models/qwen3_next_l2.py"), + str(ROOT / "tools/validation/models/qwen3_next/qwen3_next_l2.py"), "--gpu-probe", "--output", str(probe), diff --git a/tools/validation/models/qwen3_next_training_checkpoint.py b/tools/validation/models/qwen3_next/qwen3_next_training_checkpoint.py similarity index 99% rename from tools/validation/models/qwen3_next_training_checkpoint.py rename to tools/validation/models/qwen3_next/qwen3_next_training_checkpoint.py index f25519a8a..a60cbcb02 100644 --- a/tools/validation/models/qwen3_next_training_checkpoint.py +++ b/tools/validation/models/qwen3_next/qwen3_next_training_checkpoint.py @@ -44,7 +44,7 @@ restore_rng_state, state_digest, ) -from rl_engine.validation.models.qwen3_next_rl_fixture import ( +from rl_engine.validation.models.qwen3_next.qwen3_next_rl_fixture import ( PolicyFixture, require_policy_configuration, ) diff --git a/tools/validation/models/qwen3_next_training_export.py b/tools/validation/models/qwen3_next/qwen3_next_training_export.py similarity index 99% rename from tools/validation/models/qwen3_next_training_export.py rename to tools/validation/models/qwen3_next/qwen3_next_training_export.py index f785e30e3..75a78c00e 100644 --- a/tools/validation/models/qwen3_next_training_export.py +++ b/tools/validation/models/qwen3_next/qwen3_next_training_export.py @@ -54,7 +54,7 @@ def _json_digest(value): def _reference(): path = ( - Path(__file__).resolve().parents[3] + Path(__file__).resolve().parents[4] / "rl_engine/validation/models/qwen3_next_norm_manifest.json" ) return json.loads(path.read_text())["model_identity"] @@ -62,8 +62,8 @@ def _reference(): def _schema_reference(): path = ( - Path(__file__).resolve().parents[3] - / "rl_engine/validation/models/qwen3_next_export_schema.json" + Path(__file__).resolve().parents[4] + / "rl_engine/validation/models/qwen3_next/qwen3_next_export_schema.json" ) record = json.loads(path.read_text()) if ( From 299ea4c3c5d47df0619b21ac4dee8d998f22dbf2 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Wed, 14 Oct 2026 20:50:03 +0000 Subject: [PATCH 43/68] refactor(qwen3-next): batch-invariance branch to the reviewed layout Per the layout review on #549, this branch's own files move the way #549 already moved them: the BI cases and manifest to rl_engine/validation/models/qwen3_next/, the BI runner to tools/validation/models/qwen3_next/, with imports and tests following. The BI plot moves to the Qwen3-Next benchmark PR, and the committed BI evidence is replaced by the summary #549 already publishes (ZIP release attachment, SHA-256, tested commit, commands). Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/usage/qwen3-next-batch-invariance.md | 9 +- .../qwen3-next-batch-invariance-b200.md | 48 ++++++ .../{ => qwen3_next}/qwen3_next_bi_cases.py | 4 +- .../qwen3_next_bi_manifest.json | 2 +- .../check_qwen3_next_bi_requests.py | 2 +- .../qwen3_next/test_qwen3_next_bi_cases.py | 2 +- tests/validation/common/test_file_digest.py | 5 +- .../plot_qwen3_next_batch_invariance.py | 144 ------------------ .../qwen3_next_batch_invariance.py | 17 ++- .../models/qwen3_next/qwen3_next_contract.py | 2 +- 10 files changed, 73 insertions(+), 162 deletions(-) create mode 100644 reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_bi_cases.py (98%) rename rl_engine/validation/models/{ => qwen3_next}/qwen3_next_bi_manifest.json (99%) delete mode 100644 tools/validation/models/plot_qwen3_next_batch_invariance.py rename tools/validation/models/{ => qwen3_next}/qwen3_next_batch_invariance.py (94%) diff --git a/docs/usage/qwen3-next-batch-invariance.md b/docs/usage/qwen3-next-batch-invariance.md index c3fea5dbb..584a214e7 100644 --- a/docs/usage/qwen3-next-batch-invariance.md +++ b/docs/usage/qwen3-next-batch-invariance.md @@ -8,10 +8,10 @@ passed `l2` stage of the [full-model chain](qwen3-next-chain.md), under the same certified provider profile. ```bash -python tools/validation/models/qwen3_next_run.py --stage bi --previous /<...-l2-...> \ +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage bi --previous /<...-l2-...> \ --runs-root --checkpoint --cuda-home \ --python /bin/python [--bi-suite quick] -python tools/validation/models/qwen3_next_chain_report.py --last <...-train-...> --bi <...-bi-...> --out chain-report.json +python tools/validation/models/qwen3_next/qwen3_next_chain_report.py --last <...-train-...> --bi <...-bi-...> --out chain-report.json ``` ## Cells @@ -19,7 +19,7 @@ python tools/validation/models/qwen3_next_chain_report.py --last <...-train-...> Every cell has one *target* (16 generated tokens) and compares it bitwise - response tokens, BF16 logits, FP32 logprobs, the embedding and every layer, sub-node and recurrent/KV-state digest - with a reference. The cells are -generated by `rl_engine/validation/models/qwen3_next_bi_cases.py` and pinned by +generated by `rl_engine/validation/models/qwen3_next/qwen3_next_bi_cases.py` and pinned by `qwen3_next_bi_manifest.json`. | Family | Cells | The target runs | Compared with | @@ -53,3 +53,6 @@ layer and sub-node, and its layer family (`embedding`, `norm`, `gdn`, `full_attention`, `moe` or `lm_head_logprob`). The chain report's `batch_invariance` section summarises it per family; a failing cell fails the chain report. + +The [B200 result summary](https://github.com/fusheng-ji/RL-Kernel/blob/feat/ws1-qwen3-next-batch-invariance-ci/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md) +links the raw evidence ZIP, tested commit and reproduction commands. diff --git a/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md b/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md new file mode 100644 index 000000000..6b18e5692 --- /dev/null +++ b/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md @@ -0,0 +1,48 @@ +# Qwen3-Next batch invariance (B200) + +[Download raw results (ZIP)](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-pr549-evidence/qwen3-next-batch-invariance-b200.zip). + +ZIP SHA-256: `a6f7245c83d4e2fbbc949ba915abaf05a9b71d5fbeaaf241f846eab5addeb878`. + +Measured at commit `759755e` (`measured-commit.txt`) from clean clones, one +stage after another on one node with four B200 GPUs, in one environment +(`environment_sha256` `118384371a88…` in every `run.json`): + +```bash +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage all --until l2 \ + --runs-root --checkpoint --cuda-home --python /bin/python +python tools/validation/models/qwen3_next/qwen3_next_run.py --stage bi --bi-suite full --previous /<...-l2-...> \ + --runs-root --checkpoint --cuda-home --python /bin/python +python tools/validation/models/qwen3_next/qwen3_next_chain_report.py --last <...-l2-...> --bi <...-bi-...> --out chain-report.json +python tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py --last <...-l2-...> --also <...-bi-...> \ + --chain-report chain-report.json --out artifacts/qwen3-next-batch-invariance-b200 +python benchmarks/models/qwen3_next/plot_qwen3_next_batch_invariance.py artifacts/qwen3-next-batch-invariance-b200 +``` + +Commands use the current layout; at `759755e` the runners lived in `scripts/`, and the plot script is now in the Qwen3-Next benchmark PR. +The archive preserves the original evidence bytes and `SHA256SUMS`; the publisher +at `3025497` only added site redactions. Extract the ZIP before inspecting its JSON. + +| Stage | Result | +| --- | --- | +| `preflight` | passed: environment, mandatory operator cases and norm gates, device guards, TP4 probe, checkpoint hashes, rollout smoke | +| `layers` | passed: GDN, attention, MoE, GDN-adapter and embedding/head TP4 gates on real weights; native scheduler on 16 cases | +| `l2` | passed: 16/16 cases bitwise rollout vs replay, every layer, sub-node and state digest equal | +| `bi` (full) | passed: **97/97 cells bitwise** | + +| `bi` family | Cells | Passed | Compared with | +| --- | --- | --- | --- | +| concurrency (1-64 requests, target first or last) | 13 | 13 | the target alone, same chunk plan | +| interference (content, lengths, arrival, generation length) | 10 | 10 | the target alone, same chunk plan | +| chunk (prompts 63-1024, chunk 64/128, first chunk 1/63/65) | 66 | 66 | the replay engine's recompute | +| packing (replay THD batch with 1-7 neighbours) | 8 | 8 | the replay of the target alone | + +Each comparison is raw bits of the target's 16 response tokens, its BF16 +logits and FP32 logprobs over the full vocabulary, the embedding rows and, for +all 48 layers, the five sub-node outputs, the layer output and the +convolution/recurrent or KV state. `bi/bi_matrix.json` lists every cell; +`chain-report.json` is the chain-gate report with its `batch_invariance` +section. + +Site paths, host names and the scheduler are replaced by placeholders +(``, ``, ``, ``, `/name`, `/file`). diff --git a/rl_engine/validation/models/qwen3_next_bi_cases.py b/rl_engine/validation/models/qwen3_next/qwen3_next_bi_cases.py similarity index 98% rename from rl_engine/validation/models/qwen3_next_bi_cases.py rename to rl_engine/validation/models/qwen3_next/qwen3_next_bi_cases.py index 1aaea196b..8df724891 100644 --- a/rl_engine/validation/models/qwen3_next_bi_cases.py +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_bi_cases.py @@ -34,7 +34,7 @@ from dataclasses import asdict, dataclass from pathlib import Path -from rl_engine.validation.models.qwen3_next_l2 import json_digest +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import json_digest MANIFEST = Path(__file__).with_name("qwen3_next_bi_manifest.json") MAX_MODEL_LEN = 1040 @@ -243,7 +243,7 @@ def manifest_payload(): return { "schema_version": 1, "description": "Qwen3-Next batch-invariance cells; regenerate with " - "python -m rl_engine.validation.models.qwen3_next_bi_cases --write", + "python -m rl_engine.validation.models.qwen3_next.qwen3_next_bi_cases --write", "cells": entries, "cells_sha256": hashlib.sha256( json.dumps(entries, sort_keys=True, separators=(",", ":")).encode() diff --git a/rl_engine/validation/models/qwen3_next_bi_manifest.json b/rl_engine/validation/models/qwen3_next/qwen3_next_bi_manifest.json similarity index 99% rename from rl_engine/validation/models/qwen3_next_bi_manifest.json rename to rl_engine/validation/models/qwen3_next/qwen3_next_bi_manifest.json index 63a4c3911..07bf6792b 100644 --- a/rl_engine/validation/models/qwen3_next_bi_manifest.json +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_bi_manifest.json @@ -1,6 +1,6 @@ { "schema_version": 1, - "description": "Qwen3-Next batch-invariance cells; regenerate with python -m rl_engine.validation.models.qwen3_next_bi_cases --write", + "description": "Qwen3-Next batch-invariance cells; regenerate with python -m rl_engine.validation.models.qwen3_next.qwen3_next_bi_cases --write", "cells": [ { "cell_id": "concurrency-n01-first", diff --git a/tests/models/qwen3_next/check_qwen3_next_bi_requests.py b/tests/models/qwen3_next/check_qwen3_next_bi_requests.py index 567bbf242..952e96a9e 100644 --- a/tests/models/qwen3_next/check_qwen3_next_bi_requests.py +++ b/tests/models/qwen3_next/check_qwen3_next_bi_requests.py @@ -6,7 +6,7 @@ import pytest from rl_engine.integrations.engines.rollout.vllm import qwen3_next_native_rollout as rollout -from rl_engine.validation.models import qwen3_next_bi_cases as bi +from rl_engine.validation.models.qwen3_next import qwen3_next_bi_cases as bi TEXTS = [list(range(1000 + 50 * i, 1000 + 50 * i + 17 + i)) for i in range(4)] IDENTITY = { diff --git a/tests/models/qwen3_next/test_qwen3_next_bi_cases.py b/tests/models/qwen3_next/test_qwen3_next_bi_cases.py index a2b7096e6..93ee28e13 100644 --- a/tests/models/qwen3_next/test_qwen3_next_bi_cases.py +++ b/tests/models/qwen3_next/test_qwen3_next_bi_cases.py @@ -10,7 +10,7 @@ from rl_engine.integrations.orchestrators.vime.experiments.qwen3_next.native_recompute import ( packed_layout, ) -from rl_engine.validation.models import qwen3_next_bi_cases as bi +from rl_engine.validation.models.qwen3_next import qwen3_next_bi_cases as bi TEXTS = [list(range(1000 + 50 * i, 1000 + 50 * i + 17 + i)) for i in range(4)] IDENTITY = { diff --git a/tests/validation/common/test_file_digest.py b/tests/validation/common/test_file_digest.py index 22ef42a87..479db5677 100644 --- a/tests/validation/common/test_file_digest.py +++ b/tests/validation/common/test_file_digest.py @@ -27,7 +27,10 @@ def test_runtime_digest_does_not_import_torch(tmp_path): import sys from pathlib import Path - runtime = Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next/qwen3_next_runtime.py" + runtime = ( + Path(__file__).resolve().parents[3] + / "tools/validation/models/qwen3_next/qwen3_next_runtime.py" + ) artifact = tmp_path / "artifact.bin" artifact.write_bytes(b"standalone evidence runtime") program = """ diff --git a/tools/validation/models/plot_qwen3_next_batch_invariance.py b/tools/validation/models/plot_qwen3_next_batch_invariance.py deleted file mode 100644 index 21a128a9f..000000000 --- a/tools/validation/models/plot_qwen3_next_batch_invariance.py +++ /dev/null @@ -1,144 +0,0 @@ -#!/usr/bin/env python -# SPDX-License-Identifier: Apache-2.0 -# Copyright (c) 2026 RL-Kernel Contributors - -"""Render a published Qwen3-Next batch-invariance matrix as a PNG. - -Needs only the published evidence directory and matplotlib: - - python tools/validation/models/plot_qwen3_next_batch_invariance.py \\ - docs/usage/evidence/qwen3-next-batch-invariance-b200 - -writes ``figure.png`` into that directory: one panel per cell family. A cell is -green when the target request's response tokens, logits, logprobs and every -layer digest are bitwise equal to its reference, red otherwise. -""" - -from __future__ import annotations - -import argparse -import json -import re -from pathlib import Path - -import matplotlib - -matplotlib.use("Agg") -import matplotlib.pyplot as plt # noqa: E402 -import numpy as np # noqa: E402 -from matplotlib.colors import ListedColormap # noqa: E402 - -INK = "#2b2b29" -MUTED = "#6f6e69" -SURFACE = "#fcfcfb" -NA = "#e4e3dd" -COLORS = ListedColormap([NA, "#d64545", "#1baf7a"]) # 0 absent, 1 differs, 2 bitwise equal - - -def _value(cell): - return 2 if cell["passed"] and all(cell["comparisons"].values()) else 1 - - -def _grid(axis, values, rows, cols, title, xlabel=None, ylabel=None): - axis.imshow(values, cmap=COLORS, vmin=0, vmax=2, aspect="auto") - axis.set_yticks(range(len(rows)), rows, fontsize=8, color=INK) - axis.set_xticks(range(len(cols)), cols, fontsize=8, color=INK, rotation=0) - axis.set_xticks(np.arange(-0.5, len(cols)), minor=True) - axis.set_yticks(np.arange(-0.5, len(rows)), minor=True) - axis.grid(which="minor", color=SURFACE, linewidth=2) - axis.tick_params(which="minor", length=0) - passed = int((values == 2).sum()) - total = int((values > 0).sum()) - axis.set_title(f"{title}: {passed}/{total} bitwise", fontsize=10, color=INK) - if xlabel: - axis.set_xlabel(xlabel, fontsize=9, color=MUTED) - if ylabel: - axis.set_ylabel(ylabel, fontsize=9, color=MUTED) - for spine in axis.spines.values(): - spine.set_visible(False) - - -def plot(evidence: Path, out: Path) -> None: - matrix = json.loads((evidence / "bi" / "bi_matrix.json").read_text()) - cells = matrix["cells"] - - conc = {} - for cid, cell in cells.items(): - m = re.fullmatch(r"concurrency-n(\d+)-(first|last)", cid) - if m: - conc[(int(m.group(1)), m.group(2))] = _value(cell) - ns = sorted({n for n, _ in conc}) - conc_grid = np.array([[conc.get((n, p), 0) for n in ns] for p in ("first", "last")]) - - chunk = {} - for cid, cell in cells.items(): - m = re.fullmatch(r"chunk-p(\d+)-c(\d+)-f(\d+)", cid) - if m: - chunk[(int(m.group(1)), int(m.group(2)), int(m.group(3)))] = _value(cell) - prompts = sorted({p for p, _, _ in chunk}) - plans = sorted({(c, f) for _, c, f in chunk}) - chunk_grid = np.array([[chunk.get((p, c, f), 0) for c, f in plans] for p in prompts]) - - def listed(prefix): - ids = [cid for cid in matrix["cell_ids"] if cid.startswith(prefix)] - return ids, np.array([[_value(cells[cid])] for cid in ids]) - - inter_ids, inter_grid = listed("interference-") - pack_ids, pack_grid = listed("packing-") - - fig = plt.figure(figsize=(16, 8.5), facecolor=SURFACE) - spec = fig.add_gridspec(2, 3, width_ratios=[1.5, 0.9, 0.9], height_ratios=[1, 2.6]) - _grid( - fig.add_subplot(spec[0, 0]), - conc_grid, - ["target first", "target last"], - [str(n) for n in ns], - "Concurrency (N requests, vs. the target alone)", - xlabel="N", - ) - _grid( - fig.add_subplot(spec[1, 0]), - chunk_grid, - [str(p) for p in prompts], - [f"c{c}\nfirst {f}" for c, f in plans], - "Chunk boundaries (vs. the replay engine)", - xlabel="chunk size / first chunk", - ylabel="prompt tokens", - ) - _grid( - fig.add_subplot(spec[:, 1]), - inter_grid, - [cid.removeprefix("interference-") for cid in inter_ids], - [""], - "Interference", - ) - _grid( - fig.add_subplot(spec[:, 2]), - pack_grid, - [cid.removeprefix("packing-") for cid in pack_ids], - [""], - "Packing", - ) - total = len(cells) - passed = sum(_value(cell) == 2 for cell in cells.values()) - fig.suptitle( - f"Qwen3-Next-80B-A3B batch invariance, TP4, suite {matrix.get('suite', '')}: " - f"{passed}/{total} cells bitwise (tokens, logits, logprobs, layer digests)", - fontsize=11, - color=INK, - ) - fig.tight_layout() - fig.savefig(out, dpi=160) - - -def main() -> None: - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("evidence", type=Path) - args = parser.parse_args() - out = args.evidence / "figure.png" - plot(args.evidence, out) - print(f"wrote {out}") - - -if __name__ == "__main__": - main() diff --git a/tools/validation/models/qwen3_next_batch_invariance.py b/tools/validation/models/qwen3_next/qwen3_next_batch_invariance.py similarity index 94% rename from tools/validation/models/qwen3_next_batch_invariance.py rename to tools/validation/models/qwen3_next/qwen3_next_batch_invariance.py index ead3e37b2..762b9b9f6 100644 --- a/tools/validation/models/qwen3_next_batch_invariance.py +++ b/tools/validation/models/qwen3_next/qwen3_next_batch_invariance.py @@ -5,7 +5,8 @@ """Stage ``bi``: the Qwen3-Next batch-invariance matrix (RFC #428 ``batch_invariance_ci``). Runs after a passed ``l2`` stage, under the same certified provider profile -(``VLLM_BATCH_INVARIANT=1``). The cells are ``rl_engine/validation/models/qwen3_next_bi_cases.py`` +(``VLLM_BATCH_INVARIANT=1``). The cells are +``rl_engine/validation/models/qwen3_next/qwen3_next_bi_cases.py`` (pinned by ``qwen3_next_bi_manifest.json``); ``bi_suite: quick`` in the manifest runs the CI subset. Two independent worker processes, as in ``l2``: @@ -28,9 +29,9 @@ import sys from pathlib import Path -ROOT = Path(__file__).resolve().parents[3] +ROOT = Path(__file__).resolve().parents[4] sys.path.insert(0, str(ROOT)) -sys.path.insert(0, str(ROOT / "tools/validation/models")) +sys.path.insert(0, str(ROOT / "tools/validation/models/qwen3_next")) from qwen3_next_contract import evidence_record, result_envelope, validate_result # noqa: E402 from qwen3_next_l2 import ( # noqa: E402 @@ -50,14 +51,14 @@ verify_environment, ) -from rl_engine.validation.models import qwen3_next_bi_cases as bi # noqa: E402 -from rl_engine.validation.models.qwen3_next_binary import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next import qwen3_next_bi_cases as bi # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_binary import ( # noqa: E402 archive_provider_binary, validate_binary_record, verify_imported_provider, ) -from rl_engine.validation.models.qwen3_next_chain_report import family_of # noqa: E402 -from rl_engine.validation.models.qwen3_next_l2 import ( # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_chain_report import family_of # noqa: E402 +from rl_engine.validation.models.qwen3_next.qwen3_next_l2 import ( # noqa: E402 FROZEN_TEXTS, compare_artifacts, compare_batch_prefill, @@ -232,7 +233,7 @@ def execute(manifest_path, output): run_process( [ manifest["python"], - str(ROOT / "tools/validation/models/qwen3_next_l2.py"), + str(ROOT / "tools/validation/models/qwen3_next/qwen3_next_l2.py"), "--gpu-probe", "--output", str(probe), diff --git a/tools/validation/models/qwen3_next/qwen3_next_contract.py b/tools/validation/models/qwen3_next/qwen3_next_contract.py index 764bebfda..ed88e5ce4 100644 --- a/tools/validation/models/qwen3_next/qwen3_next_contract.py +++ b/tools/validation/models/qwen3_next/qwen3_next_contract.py @@ -292,7 +292,7 @@ def validate_result(manifest, result, *, run_dir=None, attempt=None): if manifest["stage"] == "bi": from qwen3_next_l2 import model_identity - from rl_engine.validation.models import qwen3_next_bi_cases as bi + from rl_engine.validation.models.qwen3_next import qwen3_next_bi_cases as bi matrix = json.loads((root / evidence["bi_matrix"]["path"]).read_text()) suite = bi.quick_cells() if manifest.get("bi_suite") == "quick" else bi.cells() From d84fbf3595ddaff4900002bd1e1e8aba77e9ae06 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:41:31 +0000 Subject: [PATCH 44/68] style(qwen3-next): black-format the relocated runtime path in the digest test Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- tests/validation/common/test_file_digest.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/validation/common/test_file_digest.py b/tests/validation/common/test_file_digest.py index 22ef42a87..479db5677 100644 --- a/tests/validation/common/test_file_digest.py +++ b/tests/validation/common/test_file_digest.py @@ -27,7 +27,10 @@ def test_runtime_digest_does_not_import_torch(tmp_path): import sys from pathlib import Path - runtime = Path(__file__).resolve().parents[3] / "tools/validation/models/qwen3_next/qwen3_next_runtime.py" + runtime = ( + Path(__file__).resolve().parents[3] + / "tools/validation/models/qwen3_next/qwen3_next_runtime.py" + ) artifact = tmp_path / "artifact.bin" artifact.write_bytes(b"standalone evidence runtime") program = """ From 32040a71169be9fb307d8fb288544033fd547344 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 18 Oct 2026 15:31:55 +0000 Subject: [PATCH 45/68] docs(qwen3-next): record the full chain rerun on the stack top Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../qwen3_next/qwen3-next-chain-b200.md | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/reports/experiments/qwen3_next/qwen3-next-chain-b200.md b/reports/experiments/qwen3_next/qwen3-next-chain-b200.md index ebb16c526..d80f87e3e 100644 --- a/reports/experiments/qwen3_next/qwen3-next-chain-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-chain-b200.md @@ -46,3 +46,29 @@ itself (see the known issue in the PR). Site paths, host names, users and the scheduler are replaced by placeholders (``, ``, ``, ``, ``); `redaction.json` counts them. + +## Rerun on the stack top (`90c09164`) + +After the stack was aligned with the reviewed layout (runners under +`tools/validation/models/qwen3_next/`, code under `rl_engine/models/qwen3_next/`), +the whole TP4 x EP1 chain was run again from clean clones of `90c09164` +(commit and tree in `measured-commit.txt` and every `run.json`), same environment +and checkpoint as above: [raw results (ZIP)](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-rfc428-evidence/qwen3-next-chain-stacktop-b200.zip), +SHA-256 `bde7e27c5db8d92d1171f08d7858231e4f8dee4e4831f60c3103c95505245f09` +(`figure.png` is the chain figure, `figure-bi.png` the batch-invariance matrix +of the same commit). + +| Stage | Result | +| --- | --- | +| `preflight`, `layers` | passed | +| `l2` | 16 / 16 raw-bit; every artifact bitwise identical to the `c67ec14` run | +| `train1`, `train2` | passed, 16 / 16 raw-bit on the updated weights each | +| `train` | passed: checkpoint reload, reload == update 2, continuous == resumed step 3, checkpoint fault injection; 16 / 16 raw-bit | +| chain-gate report | `qwen3-next-chain-v1`, 64 cells, `passed: true`, `first_drift: null` | + +`train1` and `train2` were preempted and restarted several times; each stage is +published only as a complete, passed attempt, and before `train` the training +journal was checked to hold exactly the events of the two updates (the known +resume issue, see the PR). `train` ran in one attempt without a restart. The +published chain is a copy of the stage outputs made by the committed publisher +(`redaction.json` counts the placeholders; the ZIP contains no site detail). From 7b71b9337aedf2f55e659ecf5c50e0f99856429f Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sun, 18 Oct 2026 15:31:57 +0000 Subject: [PATCH 46/68] docs(qwen3-next): record the full BI suite rerun on the stack top Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../qwen3_next/qwen3-next-batch-invariance-b200.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md b/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md index 6b18e5692..b29e5a1ce 100644 --- a/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md @@ -46,3 +46,15 @@ section. Site paths, host names and the scheduler are replaced by placeholders (``, ``, ``, ``, `/name`, `/file`). + +## Rerun of the full suite on the stack top (`90c09164`) + +The full 97-cell suite was run again from a clean clone of `90c09164` (TP4 x EP1, +the same checkpoint and environment; `preflight`, `layers` and `l2` passed first +and `l2` is bitwise identical to the `c67ec14` run). Result: **97 / 97 cells +bitwise** (concurrency 13 / 13, interference 10 / 10, chunk 66 / 66, packing 8 / 8), +no failed cell, `first_failure: null`. The published copy is inside the chain +archive of the full-model chain, +[`qwen3-next-chain-stacktop-b200.zip`](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-rfc428-evidence/qwen3-next-chain-stacktop-b200.zip) +(SHA-256 `bde7e27c5db8d92d1171f08d7858231e4f8dee4e4831f60c3103c95505245f09`, +`bi/bi_matrix.json`, `figure-bi.png`). From 8943c14fdab9d374dbbd5dcdccabf7dcc4743a5f Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 19 Oct 2026 00:56:55 +0000 Subject: [PATCH 47/68] docs: move the measurement outputs out of the pull request The raw results (JSON, figures, audit files) now live on the fork's pr_assets branch; the documents link to them at a fixed commit. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/qwen3-next-rms-norm-gated.md | 6 +++--- docs/operators/qwen3-next-rms-norm.md | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/operators/qwen3-next-rms-norm-gated.md b/docs/operators/qwen3-next-rms-norm-gated.md index 217fa45ed..38441ed42 100644 --- a/docs/operators/qwen3-next-rms-norm-gated.md +++ b/docs/operators/qwen3-next-rms-norm-gated.md @@ -121,13 +121,13 @@ python tools/validation/operators/check_operator.py --op rms_norm_gated --candid ## Evidence -![gated RMSNorm vs existing implementations on B200](../usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) +![gated RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) -[`report.json`](../usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by +[`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by `tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `822b085`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0). Head dim 128, BF16; the FP64 golden uses vLLM's convention. The same report re-measures the zero-centred op -([figure](../usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). +([figure](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). | | rl-kernel CUDA | PyTorch reference | transformers (cast-first) | vLLM `RMSNormGated`* | |---|---|---|---|---| diff --git a/docs/operators/qwen3-next-rms-norm.md b/docs/operators/qwen3-next-rms-norm.md index 2fbd8baa5..897b0ffff 100644 --- a/docs/operators/qwen3-next-rms-norm.md +++ b/docs/operators/qwen3-next-rms-norm.md @@ -100,9 +100,9 @@ element and no extra memory traffic. ## Evidence -![zero-centred RMSNorm vs existing implementations on B200](../usage/evidence/qwen3-next-rms-norm-b200/figure.png) +![zero-centred RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr467/docs/usage/evidence/qwen3-next-rms-norm-b200/figure.png) -[`report.json`](../usage/evidence/qwen3-next-rms-norm-b200/report.json) was written by +[`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr467/docs/usage/evidence/qwen3-next-rms-norm-b200/report.json) was written by `tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `3d0bae7`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0, FlashInfer 0.6.18). Hidden 2048, BF16. From 0fdbf80be3d70cbdaa28db8f8d9ed42b168ac6fc Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 19 Oct 2026 00:56:57 +0000 Subject: [PATCH 48/68] docs: move the measurement outputs out of the pull request The raw results (JSON, figures, audit files) now live on the fork's pr_assets branch; the documents link to them at a fixed commit. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/qwen3-next-rms-norm-gated.md | 6 +++--- docs/operators/qwen3-next-rms-norm.md | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/operators/qwen3-next-rms-norm-gated.md b/docs/operators/qwen3-next-rms-norm-gated.md index 217fa45ed..38441ed42 100644 --- a/docs/operators/qwen3-next-rms-norm-gated.md +++ b/docs/operators/qwen3-next-rms-norm-gated.md @@ -121,13 +121,13 @@ python tools/validation/operators/check_operator.py --op rms_norm_gated --candid ## Evidence -![gated RMSNorm vs existing implementations on B200](../usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) +![gated RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) -[`report.json`](../usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by +[`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by `tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `822b085`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0). Head dim 128, BF16; the FP64 golden uses vLLM's convention. The same report re-measures the zero-centred op -([figure](../usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). +([figure](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). | | rl-kernel CUDA | PyTorch reference | transformers (cast-first) | vLLM `RMSNormGated`* | |---|---|---|---|---| diff --git a/docs/operators/qwen3-next-rms-norm.md b/docs/operators/qwen3-next-rms-norm.md index 2fbd8baa5..897b0ffff 100644 --- a/docs/operators/qwen3-next-rms-norm.md +++ b/docs/operators/qwen3-next-rms-norm.md @@ -100,9 +100,9 @@ element and no extra memory traffic. ## Evidence -![zero-centred RMSNorm vs existing implementations on B200](../usage/evidence/qwen3-next-rms-norm-b200/figure.png) +![zero-centred RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr467/docs/usage/evidence/qwen3-next-rms-norm-b200/figure.png) -[`report.json`](../usage/evidence/qwen3-next-rms-norm-b200/report.json) was written by +[`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr467/docs/usage/evidence/qwen3-next-rms-norm-b200/report.json) was written by `tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `3d0bae7`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0, FlashInfer 0.6.18). Hidden 2048, BF16. From 46d510869928e2dd8e460d86faedeb8962a9bb13 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 19 Oct 2026 00:56:59 +0000 Subject: [PATCH 49/68] docs: move the measurement outputs out of the pull request The raw results (JSON, figures, audit files) now live on the fork's pr_assets branch; the documents link to them at a fixed commit. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/qwen3-next-rms-norm-gated.md | 6 +++--- docs/operators/qwen3-next-rms-norm.md | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/operators/qwen3-next-rms-norm-gated.md b/docs/operators/qwen3-next-rms-norm-gated.md index 217fa45ed..38441ed42 100644 --- a/docs/operators/qwen3-next-rms-norm-gated.md +++ b/docs/operators/qwen3-next-rms-norm-gated.md @@ -121,13 +121,13 @@ python tools/validation/operators/check_operator.py --op rms_norm_gated --candid ## Evidence -![gated RMSNorm vs existing implementations on B200](../usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) +![gated RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) -[`report.json`](../usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by +[`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by `tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `822b085`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0). Head dim 128, BF16; the FP64 golden uses vLLM's convention. The same report re-measures the zero-centred op -([figure](../usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). +([figure](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). | | rl-kernel CUDA | PyTorch reference | transformers (cast-first) | vLLM `RMSNormGated`* | |---|---|---|---|---| diff --git a/docs/operators/qwen3-next-rms-norm.md b/docs/operators/qwen3-next-rms-norm.md index 2fbd8baa5..897b0ffff 100644 --- a/docs/operators/qwen3-next-rms-norm.md +++ b/docs/operators/qwen3-next-rms-norm.md @@ -100,9 +100,9 @@ element and no extra memory traffic. ## Evidence -![zero-centred RMSNorm vs existing implementations on B200](../usage/evidence/qwen3-next-rms-norm-b200/figure.png) +![zero-centred RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr467/docs/usage/evidence/qwen3-next-rms-norm-b200/figure.png) -[`report.json`](../usage/evidence/qwen3-next-rms-norm-b200/report.json) was written by +[`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr467/docs/usage/evidence/qwen3-next-rms-norm-b200/report.json) was written by `tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `3d0bae7`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0, FlashInfer 0.6.18). Hidden 2048, BF16. From 8fd657cb6fdecbec274515c35d6104d363ff31c4 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 19 Oct 2026 00:57:01 +0000 Subject: [PATCH 50/68] docs: move the measurement outputs out of the pull request The raw results (JSON, figures, audit files) now live on the fork's pr_assets branch; the documents link to them at a fixed commit. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/qwen3-next-rms-norm-gated.md | 6 +++--- docs/operators/qwen3-next-rms-norm.md | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/operators/qwen3-next-rms-norm-gated.md b/docs/operators/qwen3-next-rms-norm-gated.md index 217fa45ed..38441ed42 100644 --- a/docs/operators/qwen3-next-rms-norm-gated.md +++ b/docs/operators/qwen3-next-rms-norm-gated.md @@ -121,13 +121,13 @@ python tools/validation/operators/check_operator.py --op rms_norm_gated --candid ## Evidence -![gated RMSNorm vs existing implementations on B200](../usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) +![gated RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) -[`report.json`](../usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by +[`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by `tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `822b085`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0). Head dim 128, BF16; the FP64 golden uses vLLM's convention. The same report re-measures the zero-centred op -([figure](../usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). +([figure](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). | | rl-kernel CUDA | PyTorch reference | transformers (cast-first) | vLLM `RMSNormGated`* | |---|---|---|---|---| diff --git a/docs/operators/qwen3-next-rms-norm.md b/docs/operators/qwen3-next-rms-norm.md index 2fbd8baa5..897b0ffff 100644 --- a/docs/operators/qwen3-next-rms-norm.md +++ b/docs/operators/qwen3-next-rms-norm.md @@ -100,9 +100,9 @@ element and no extra memory traffic. ## Evidence -![zero-centred RMSNorm vs existing implementations on B200](../usage/evidence/qwen3-next-rms-norm-b200/figure.png) +![zero-centred RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr467/docs/usage/evidence/qwen3-next-rms-norm-b200/figure.png) -[`report.json`](../usage/evidence/qwen3-next-rms-norm-b200/report.json) was written by +[`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr467/docs/usage/evidence/qwen3-next-rms-norm-b200/report.json) was written by `tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `3d0bae7`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0, FlashInfer 0.6.18). Hidden 2048, BF16. From 3d4fb2e80e9604b4ec727e52832680af6ecb15d2 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 19 Oct 2026 00:57:02 +0000 Subject: [PATCH 51/68] docs: move the measurement outputs out of the pull request The raw results (JSON, figures, audit files) now live on the fork's pr_assets branch; the documents link to them at a fixed commit. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/qwen3-next-rms-norm-gated.md | 6 +++--- docs/operators/qwen3-next-rms-norm.md | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/operators/qwen3-next-rms-norm-gated.md b/docs/operators/qwen3-next-rms-norm-gated.md index f8cbaf3ec..d563c7e62 100644 --- a/docs/operators/qwen3-next-rms-norm-gated.md +++ b/docs/operators/qwen3-next-rms-norm-gated.md @@ -121,13 +121,13 @@ python tools/validation/operators/check_operator.py --op rms_norm_gated --candid ## Evidence -![gated RMSNorm vs existing implementations on B200](../usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) +![gated RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) -[`report.json`](../usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by +[`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by `tools/validation/models/qwen3_next/qwen3_next_norm_evidence.py` from a clean tree at `822b085`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0). Head dim 128, BF16; the FP64 golden uses vLLM's convention. The same report re-measures the zero-centred op -([figure](../usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). +([figure](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). | | rl-kernel CUDA | PyTorch reference | transformers (cast-first) | vLLM `RMSNormGated`* | |---|---|---|---|---| diff --git a/docs/operators/qwen3-next-rms-norm.md b/docs/operators/qwen3-next-rms-norm.md index 522fc1bbd..5ec64885d 100644 --- a/docs/operators/qwen3-next-rms-norm.md +++ b/docs/operators/qwen3-next-rms-norm.md @@ -100,9 +100,9 @@ element and no extra memory traffic. ## Evidence -![zero-centred RMSNorm vs existing implementations on B200](../usage/evidence/qwen3-next-rms-norm-b200/figure.png) +![zero-centred RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr467/docs/usage/evidence/qwen3-next-rms-norm-b200/figure.png) -[`report.json`](../usage/evidence/qwen3-next-rms-norm-b200/report.json) was written by +[`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr467/docs/usage/evidence/qwen3-next-rms-norm-b200/report.json) was written by `tools/validation/models/qwen3_next/qwen3_next_norm_evidence.py` from a clean tree at `3d0bae7`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0, FlashInfer 0.6.18). Hidden 2048, BF16. From bc431137651b4dbda982bd36f4db7dc28b88471c Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 23 Oct 2026 22:06:05 +0000 Subject: [PATCH 52/68] docs: point commit hashes at the rewritten history The history of this branch was rewritten on 2026-10-23 (evidence directories removed from every commit, sign-offs added). Commit hashes quoted in the reports and documents now name the rewritten commits; the evidence archives still record the pre-rewrite hashes. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/qwen3-next-rms-norm-gated.md | 2 +- reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/operators/qwen3-next-rms-norm-gated.md b/docs/operators/qwen3-next-rms-norm-gated.md index 38441ed42..a3895292a 100644 --- a/docs/operators/qwen3-next-rms-norm-gated.md +++ b/docs/operators/qwen3-next-rms-norm-gated.md @@ -124,7 +124,7 @@ python tools/validation/operators/check_operator.py --op rms_norm_gated --candid ![gated RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) [`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by -`tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `822b085`, on an otherwise idle +`tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `ff271d5`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0). Head dim 128, BF16; the FP64 golden uses vLLM's convention. The same report re-measures the zero-centred op ([figure](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). diff --git a/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md b/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md index a7d90d059..61a0aeb12 100644 --- a/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md @@ -6,9 +6,9 @@ ZIP SHA-256: `9fcc704291bf697274edfe16d6958ca799134008acd9d5f452767b9a92758cb0`. The archive holds `report.json`, `figure.png`, the TP4 gate's `tp4-moe/rank-{0..3}.json` and the original README, byte for byte as measured, -plus `SHA256SUMS`. The prior art was measured at commit `c58816b` from a clean +plus `SHA256SUMS`. The prior art was measured at commit `dc79953` from a clean clone on one B200 (torch 2.13.0+cu130, Triton 3.7.1); the TP4 gate at -`2e950f3`, whose MoE code is identical, on four B200. The prior art uses +`cbdc00a`, whose MoE code is identical, on four B200. The prior art uses random weights (scale 0.02) at the TP1 shape (H=2048, 512 experts, top-10, width 512); the TP4 gate reads layer 0 of the official `Qwen/Qwen3-Next-80B-A3B-Instruct` revision @@ -27,7 +27,7 @@ TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen --checkpoint --output tp4-moe ``` -At `c58816b` / `2e950f3` the runners lived in `tools/validation/models/` and +At `dc79953` / `cbdc00a` the runners lived in `tools/validation/models/` and `rl_engine/validation/models/`; the commands above use the current layout. | Candidate | Forward batch invariant | Backward batch invariant | Different expert set (of 256 tokens) | Max abs vs FP64 | Forward, 1024 tokens | From f9b85d15a36e86cfd1a8361c04493410f2b4be87 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 23 Oct 2026 22:06:06 +0000 Subject: [PATCH 53/68] docs: point commit hashes at the rewritten history The history of this branch was rewritten on 2026-10-23 (evidence directories removed from every commit, sign-offs added). Commit hashes quoted in the reports and documents now name the rewritten commits; the evidence archives still record the pre-rewrite hashes. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/qwen3-next-rms-norm-gated.md | 2 +- .../experiments/qwen3_next/qwen3-next-moe-route-b200.md | 6 +++--- .../experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md | 8 ++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/operators/qwen3-next-rms-norm-gated.md b/docs/operators/qwen3-next-rms-norm-gated.md index 38441ed42..a3895292a 100644 --- a/docs/operators/qwen3-next-rms-norm-gated.md +++ b/docs/operators/qwen3-next-rms-norm-gated.md @@ -124,7 +124,7 @@ python tools/validation/operators/check_operator.py --op rms_norm_gated --candid ![gated RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) [`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by -`tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `822b085`, on an otherwise idle +`tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `ff271d5`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0). Head dim 128, BF16; the FP64 golden uses vLLM's convention. The same report re-measures the zero-centred op ([figure](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). diff --git a/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md b/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md index a7d90d059..61a0aeb12 100644 --- a/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md @@ -6,9 +6,9 @@ ZIP SHA-256: `9fcc704291bf697274edfe16d6958ca799134008acd9d5f452767b9a92758cb0`. The archive holds `report.json`, `figure.png`, the TP4 gate's `tp4-moe/rank-{0..3}.json` and the original README, byte for byte as measured, -plus `SHA256SUMS`. The prior art was measured at commit `c58816b` from a clean +plus `SHA256SUMS`. The prior art was measured at commit `dc79953` from a clean clone on one B200 (torch 2.13.0+cu130, Triton 3.7.1); the TP4 gate at -`2e950f3`, whose MoE code is identical, on four B200. The prior art uses +`cbdc00a`, whose MoE code is identical, on four B200. The prior art uses random weights (scale 0.02) at the TP1 shape (H=2048, 512 experts, top-10, width 512); the TP4 gate reads layer 0 of the official `Qwen/Qwen3-Next-80B-A3B-Instruct` revision @@ -27,7 +27,7 @@ TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen --checkpoint --output tp4-moe ``` -At `c58816b` / `2e950f3` the runners lived in `tools/validation/models/` and +At `dc79953` / `cbdc00a` the runners lived in `tools/validation/models/` and `rl_engine/validation/models/`; the commands above use the current layout. | Candidate | Forward batch invariant | Backward batch invariant | Different expert set (of 256 tokens) | Max abs vs FP64 | Forward, 1024 tokens | diff --git a/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md b/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md index 29537c4c4..c833bb9f0 100644 --- a/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md @@ -15,10 +15,10 @@ attention gate: layer 3). | Files | Measured at | Command (current layout) | | --- | --- | --- | -| `tp4-gdn/` | `9e6c4f2` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py --checkpoint --output tp4-gdn` | -| `tp4-attention/` | `9e6c4f2` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py --checkpoint --output tp4-attention` | -| `tp4-moe/` | `9e6c4f2` | `TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py --checkpoint --output tp4-moe` | -| `attention/` | `703c6dc` | `python benchmarks/models/qwen3_next/qwen3_next_attention_prior_art.py --only --out report-main.json`, then `--only te_fused --merge report-main.json --out report.json`; `python benchmarks/models/qwen3_next/plot_qwen3_next_attention_prior_art.py report.json` | +| `tp4-gdn/` | `fce346d` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py --checkpoint --output tp4-gdn` | +| `tp4-attention/` | `fce346d` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py --checkpoint --output tp4-attention` | +| `tp4-moe/` | `fce346d` | `TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py --checkpoint --output tp4-moe` | +| `attention/` | `137d0fb` | `python benchmarks/models/qwen3_next/qwen3_next_attention_prior_art.py --only --out report-main.json`, then `--only te_fused --merge report-main.json --out report.json`; `python benchmarks/models/qwen3_next/plot_qwen3_next_attention_prior_art.py report.json` | At those commits the runners lived in `tools/validation/models/`; the attention prior-art runner is now part of the Qwen3-Next benchmark PR. From 4f61a39c47f4f38b3f09122ce910718ddfb3980d Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 23 Oct 2026 22:06:10 +0000 Subject: [PATCH 54/68] docs: point commit hashes at the rewritten history The history of this branch was rewritten on 2026-10-23 (evidence directories removed from every commit, sign-offs added). Commit hashes quoted in the reports and documents now name the rewritten commits; the evidence archives still record the pre-rewrite hashes. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/qwen3-next-rms-norm-gated.md | 2 +- .../qwen3_next/qwen3-next-chain-b200.md | 14 +++++++------- .../qwen3_next/qwen3-next-moe-route-b200.md | 6 +++--- .../qwen3_next/qwen3-next-tp4-mixers-b200.md | 8 ++++---- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/docs/operators/qwen3-next-rms-norm-gated.md b/docs/operators/qwen3-next-rms-norm-gated.md index 38441ed42..a3895292a 100644 --- a/docs/operators/qwen3-next-rms-norm-gated.md +++ b/docs/operators/qwen3-next-rms-norm-gated.md @@ -124,7 +124,7 @@ python tools/validation/operators/check_operator.py --op rms_norm_gated --candid ![gated RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) [`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by -`tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `822b085`, on an otherwise idle +`tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `ff271d5`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0). Head dim 128, BF16; the FP64 golden uses vLLM's convention. The same report re-measures the zero-centred op ([figure](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). diff --git a/reports/experiments/qwen3_next/qwen3-next-chain-b200.md b/reports/experiments/qwen3_next/qwen3-next-chain-b200.md index d80f87e3e..05fa61dfa 100644 --- a/reports/experiments/qwen3_next/qwen3-next-chain-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-chain-b200.md @@ -4,7 +4,7 @@ ZIP SHA-256: `76028dfea0a2b8698aa8d14d1f82e8da47df4ea11f9cd5240ebfb33094e527c1`. -Measured at commit `c67ec14` (`measured-commit.txt`) from clean clones, one +Measured at commit `92e93a4` (`measured-commit.txt`) from clean clones, one stage after another on nodes with four B200 GPUs, in one environment (`environment_sha256` `118384371a88…` in every `run.json`), on the official `Qwen/Qwen3-Next-80B-A3B-Instruct` revision @@ -23,10 +23,10 @@ python tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py --last python benchmarks/models/qwen3_next/plot_qwen3_next_chain.py artifacts/qwen3-next-chain-b200 ``` -At `c67ec14` the runners lived in `scripts/` and `ci/`; the commands use the +At `92e93a4` the runners lived in `scripts/` and `ci/`; the commands use the current layout (the plot script is in the Qwen3-Next benchmark PR). The -stages ran the code of `c67ec14`; the published copy was made by the publisher -of `3025497`, which only adds redaction rules and changes no measured byte. +stages ran the code of `92e93a4`; the published copy was made by the publisher +of `f431bad`, which only adds redaction rules and changes no measured byte. | Stage | Checks | Result | | --- | --- | --- | @@ -47,11 +47,11 @@ Site paths, host names, users and the scheduler are replaced by placeholders (``, ``, ``, ``, ``); `redaction.json` counts them. -## Rerun on the stack top (`90c09164`) +## Rerun on the stack top (`f05aaef6`) After the stack was aligned with the reviewed layout (runners under `tools/validation/models/qwen3_next/`, code under `rl_engine/models/qwen3_next/`), -the whole TP4 x EP1 chain was run again from clean clones of `90c09164` +the whole TP4 x EP1 chain was run again from clean clones of `f05aaef6` (commit and tree in `measured-commit.txt` and every `run.json`), same environment and checkpoint as above: [raw results (ZIP)](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-rfc428-evidence/qwen3-next-chain-stacktop-b200.zip), SHA-256 `bde7e27c5db8d92d1171f08d7858231e4f8dee4e4831f60c3103c95505245f09` @@ -61,7 +61,7 @@ of the same commit). | Stage | Result | | --- | --- | | `preflight`, `layers` | passed | -| `l2` | 16 / 16 raw-bit; every artifact bitwise identical to the `c67ec14` run | +| `l2` | 16 / 16 raw-bit; every artifact bitwise identical to the `92e93a4` run | | `train1`, `train2` | passed, 16 / 16 raw-bit on the updated weights each | | `train` | passed: checkpoint reload, reload == update 2, continuous == resumed step 3, checkpoint fault injection; 16 / 16 raw-bit | | chain-gate report | `qwen3-next-chain-v1`, 64 cells, `passed: true`, `first_drift: null` | diff --git a/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md b/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md index a7d90d059..61a0aeb12 100644 --- a/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md @@ -6,9 +6,9 @@ ZIP SHA-256: `9fcc704291bf697274edfe16d6958ca799134008acd9d5f452767b9a92758cb0`. The archive holds `report.json`, `figure.png`, the TP4 gate's `tp4-moe/rank-{0..3}.json` and the original README, byte for byte as measured, -plus `SHA256SUMS`. The prior art was measured at commit `c58816b` from a clean +plus `SHA256SUMS`. The prior art was measured at commit `dc79953` from a clean clone on one B200 (torch 2.13.0+cu130, Triton 3.7.1); the TP4 gate at -`2e950f3`, whose MoE code is identical, on four B200. The prior art uses +`cbdc00a`, whose MoE code is identical, on four B200. The prior art uses random weights (scale 0.02) at the TP1 shape (H=2048, 512 experts, top-10, width 512); the TP4 gate reads layer 0 of the official `Qwen/Qwen3-Next-80B-A3B-Instruct` revision @@ -27,7 +27,7 @@ TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen --checkpoint --output tp4-moe ``` -At `c58816b` / `2e950f3` the runners lived in `tools/validation/models/` and +At `dc79953` / `cbdc00a` the runners lived in `tools/validation/models/` and `rl_engine/validation/models/`; the commands above use the current layout. | Candidate | Forward batch invariant | Backward batch invariant | Different expert set (of 256 tokens) | Max abs vs FP64 | Forward, 1024 tokens | diff --git a/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md b/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md index 29537c4c4..c833bb9f0 100644 --- a/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md @@ -15,10 +15,10 @@ attention gate: layer 3). | Files | Measured at | Command (current layout) | | --- | --- | --- | -| `tp4-gdn/` | `9e6c4f2` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py --checkpoint --output tp4-gdn` | -| `tp4-attention/` | `9e6c4f2` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py --checkpoint --output tp4-attention` | -| `tp4-moe/` | `9e6c4f2` | `TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py --checkpoint --output tp4-moe` | -| `attention/` | `703c6dc` | `python benchmarks/models/qwen3_next/qwen3_next_attention_prior_art.py --only --out report-main.json`, then `--only te_fused --merge report-main.json --out report.json`; `python benchmarks/models/qwen3_next/plot_qwen3_next_attention_prior_art.py report.json` | +| `tp4-gdn/` | `fce346d` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py --checkpoint --output tp4-gdn` | +| `tp4-attention/` | `fce346d` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py --checkpoint --output tp4-attention` | +| `tp4-moe/` | `fce346d` | `TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py --checkpoint --output tp4-moe` | +| `attention/` | `137d0fb` | `python benchmarks/models/qwen3_next/qwen3_next_attention_prior_art.py --only --out report-main.json`, then `--only te_fused --merge report-main.json --out report.json`; `python benchmarks/models/qwen3_next/plot_qwen3_next_attention_prior_art.py report.json` | At those commits the runners lived in `tools/validation/models/`; the attention prior-art runner is now part of the Qwen3-Next benchmark PR. From 804f0a45a67e9772de4ea48126ce26884d2a7c54 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 23 Oct 2026 22:06:11 +0000 Subject: [PATCH 55/68] docs: point commit hashes at the rewritten history The history of this branch was rewritten on 2026-10-23 (evidence directories removed from every commit, sign-offs added). Commit hashes quoted in the reports and documents now name the rewritten commits; the evidence archives still record the pre-rewrite hashes. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/qwen3-next-rms-norm-gated.md | 2 +- .../qwen3_next/qwen3-next-batch-invariance-b200.md | 12 ++++++------ .../qwen3_next/qwen3-next-chain-b200.md | 14 +++++++------- .../qwen3_next/qwen3-next-moe-route-b200.md | 6 +++--- .../qwen3_next/qwen3-next-tp4-mixers-b200.md | 8 ++++---- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/docs/operators/qwen3-next-rms-norm-gated.md b/docs/operators/qwen3-next-rms-norm-gated.md index 38441ed42..a3895292a 100644 --- a/docs/operators/qwen3-next-rms-norm-gated.md +++ b/docs/operators/qwen3-next-rms-norm-gated.md @@ -124,7 +124,7 @@ python tools/validation/operators/check_operator.py --op rms_norm_gated --candid ![gated RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) [`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by -`tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `822b085`, on an otherwise idle +`tools/validation/models/qwen3_next_norm_evidence.py` from a clean tree at `ff271d5`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0). Head dim 128, BF16; the FP64 golden uses vLLM's convention. The same report re-measures the zero-centred op ([figure](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). diff --git a/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md b/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md index b29e5a1ce..f5940d137 100644 --- a/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md @@ -4,7 +4,7 @@ ZIP SHA-256: `a6f7245c83d4e2fbbc949ba915abaf05a9b71d5fbeaaf241f846eab5addeb878`. -Measured at commit `759755e` (`measured-commit.txt`) from clean clones, one +Measured at commit `02c576c` (`measured-commit.txt`) from clean clones, one stage after another on one node with four B200 GPUs, in one environment (`environment_sha256` `118384371a88…` in every `run.json`): @@ -19,9 +19,9 @@ python tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py --last python benchmarks/models/qwen3_next/plot_qwen3_next_batch_invariance.py artifacts/qwen3-next-batch-invariance-b200 ``` -Commands use the current layout; at `759755e` the runners lived in `scripts/`, and the plot script is now in the Qwen3-Next benchmark PR. +Commands use the current layout; at `02c576c` the runners lived in `scripts/`, and the plot script is now in the Qwen3-Next benchmark PR. The archive preserves the original evidence bytes and `SHA256SUMS`; the publisher -at `3025497` only added site redactions. Extract the ZIP before inspecting its JSON. +at `f431bad` only added site redactions. Extract the ZIP before inspecting its JSON. | Stage | Result | | --- | --- | @@ -47,11 +47,11 @@ section. Site paths, host names and the scheduler are replaced by placeholders (``, ``, ``, ``, `/name`, `/file`). -## Rerun of the full suite on the stack top (`90c09164`) +## Rerun of the full suite on the stack top (`f05aaef6`) -The full 97-cell suite was run again from a clean clone of `90c09164` (TP4 x EP1, +The full 97-cell suite was run again from a clean clone of `f05aaef6` (TP4 x EP1, the same checkpoint and environment; `preflight`, `layers` and `l2` passed first -and `l2` is bitwise identical to the `c67ec14` run). Result: **97 / 97 cells +and `l2` is bitwise identical to the `92e93a4` run). Result: **97 / 97 cells bitwise** (concurrency 13 / 13, interference 10 / 10, chunk 66 / 66, packing 8 / 8), no failed cell, `first_failure: null`. The published copy is inside the chain archive of the full-model chain, diff --git a/reports/experiments/qwen3_next/qwen3-next-chain-b200.md b/reports/experiments/qwen3_next/qwen3-next-chain-b200.md index d80f87e3e..05fa61dfa 100644 --- a/reports/experiments/qwen3_next/qwen3-next-chain-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-chain-b200.md @@ -4,7 +4,7 @@ ZIP SHA-256: `76028dfea0a2b8698aa8d14d1f82e8da47df4ea11f9cd5240ebfb33094e527c1`. -Measured at commit `c67ec14` (`measured-commit.txt`) from clean clones, one +Measured at commit `92e93a4` (`measured-commit.txt`) from clean clones, one stage after another on nodes with four B200 GPUs, in one environment (`environment_sha256` `118384371a88…` in every `run.json`), on the official `Qwen/Qwen3-Next-80B-A3B-Instruct` revision @@ -23,10 +23,10 @@ python tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py --last python benchmarks/models/qwen3_next/plot_qwen3_next_chain.py artifacts/qwen3-next-chain-b200 ``` -At `c67ec14` the runners lived in `scripts/` and `ci/`; the commands use the +At `92e93a4` the runners lived in `scripts/` and `ci/`; the commands use the current layout (the plot script is in the Qwen3-Next benchmark PR). The -stages ran the code of `c67ec14`; the published copy was made by the publisher -of `3025497`, which only adds redaction rules and changes no measured byte. +stages ran the code of `92e93a4`; the published copy was made by the publisher +of `f431bad`, which only adds redaction rules and changes no measured byte. | Stage | Checks | Result | | --- | --- | --- | @@ -47,11 +47,11 @@ Site paths, host names, users and the scheduler are replaced by placeholders (``, ``, ``, ``, ``); `redaction.json` counts them. -## Rerun on the stack top (`90c09164`) +## Rerun on the stack top (`f05aaef6`) After the stack was aligned with the reviewed layout (runners under `tools/validation/models/qwen3_next/`, code under `rl_engine/models/qwen3_next/`), -the whole TP4 x EP1 chain was run again from clean clones of `90c09164` +the whole TP4 x EP1 chain was run again from clean clones of `f05aaef6` (commit and tree in `measured-commit.txt` and every `run.json`), same environment and checkpoint as above: [raw results (ZIP)](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-rfc428-evidence/qwen3-next-chain-stacktop-b200.zip), SHA-256 `bde7e27c5db8d92d1171f08d7858231e4f8dee4e4831f60c3103c95505245f09` @@ -61,7 +61,7 @@ of the same commit). | Stage | Result | | --- | --- | | `preflight`, `layers` | passed | -| `l2` | 16 / 16 raw-bit; every artifact bitwise identical to the `c67ec14` run | +| `l2` | 16 / 16 raw-bit; every artifact bitwise identical to the `92e93a4` run | | `train1`, `train2` | passed, 16 / 16 raw-bit on the updated weights each | | `train` | passed: checkpoint reload, reload == update 2, continuous == resumed step 3, checkpoint fault injection; 16 / 16 raw-bit | | chain-gate report | `qwen3-next-chain-v1`, 64 cells, `passed: true`, `first_drift: null` | diff --git a/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md b/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md index a7d90d059..61a0aeb12 100644 --- a/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md @@ -6,9 +6,9 @@ ZIP SHA-256: `9fcc704291bf697274edfe16d6958ca799134008acd9d5f452767b9a92758cb0`. The archive holds `report.json`, `figure.png`, the TP4 gate's `tp4-moe/rank-{0..3}.json` and the original README, byte for byte as measured, -plus `SHA256SUMS`. The prior art was measured at commit `c58816b` from a clean +plus `SHA256SUMS`. The prior art was measured at commit `dc79953` from a clean clone on one B200 (torch 2.13.0+cu130, Triton 3.7.1); the TP4 gate at -`2e950f3`, whose MoE code is identical, on four B200. The prior art uses +`cbdc00a`, whose MoE code is identical, on four B200. The prior art uses random weights (scale 0.02) at the TP1 shape (H=2048, 512 experts, top-10, width 512); the TP4 gate reads layer 0 of the official `Qwen/Qwen3-Next-80B-A3B-Instruct` revision @@ -27,7 +27,7 @@ TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen --checkpoint --output tp4-moe ``` -At `c58816b` / `2e950f3` the runners lived in `tools/validation/models/` and +At `dc79953` / `cbdc00a` the runners lived in `tools/validation/models/` and `rl_engine/validation/models/`; the commands above use the current layout. | Candidate | Forward batch invariant | Backward batch invariant | Different expert set (of 256 tokens) | Max abs vs FP64 | Forward, 1024 tokens | diff --git a/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md b/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md index 29537c4c4..c833bb9f0 100644 --- a/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md @@ -15,10 +15,10 @@ attention gate: layer 3). | Files | Measured at | Command (current layout) | | --- | --- | --- | -| `tp4-gdn/` | `9e6c4f2` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py --checkpoint --output tp4-gdn` | -| `tp4-attention/` | `9e6c4f2` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py --checkpoint --output tp4-attention` | -| `tp4-moe/` | `9e6c4f2` | `TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py --checkpoint --output tp4-moe` | -| `attention/` | `703c6dc` | `python benchmarks/models/qwen3_next/qwen3_next_attention_prior_art.py --only --out report-main.json`, then `--only te_fused --merge report-main.json --out report.json`; `python benchmarks/models/qwen3_next/plot_qwen3_next_attention_prior_art.py report.json` | +| `tp4-gdn/` | `fce346d` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py --checkpoint --output tp4-gdn` | +| `tp4-attention/` | `fce346d` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py --checkpoint --output tp4-attention` | +| `tp4-moe/` | `fce346d` | `TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py --checkpoint --output tp4-moe` | +| `attention/` | `137d0fb` | `python benchmarks/models/qwen3_next/qwen3_next_attention_prior_art.py --only --out report-main.json`, then `--only te_fused --merge report-main.json --out report.json`; `python benchmarks/models/qwen3_next/plot_qwen3_next_attention_prior_art.py report.json` | At those commits the runners lived in `tools/validation/models/`; the attention prior-art runner is now part of the Qwen3-Next benchmark PR. From 78fc14abcf9f0a572f4cce61ba10ed186fb6c2d9 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 23 Oct 2026 22:06:13 +0000 Subject: [PATCH 56/68] docs: point commit hashes at the rewritten history The history of this branch was rewritten on 2026-10-23 (evidence directories removed from every commit, sign-offs added). Commit hashes quoted in the reports and documents now name the rewritten commits; the evidence archives still record the pre-rewrite hashes. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- docs/operators/qwen3-next-rms-norm-gated.md | 2 +- .../qwen3_next/qwen3-next-batch-invariance-b200.md | 12 ++++++------ .../qwen3_next/qwen3-next-chain-b200.md | 14 +++++++------- .../qwen3-next-distributed-logprob-b200.md | 12 ++++++------ .../qwen3_next/qwen3-next-moe-route-b200.md | 6 +++--- .../qwen3_next/qwen3-next-tp4-mixers-b200.md | 8 ++++---- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/docs/operators/qwen3-next-rms-norm-gated.md b/docs/operators/qwen3-next-rms-norm-gated.md index d563c7e62..39368f680 100644 --- a/docs/operators/qwen3-next-rms-norm-gated.md +++ b/docs/operators/qwen3-next-rms-norm-gated.md @@ -124,7 +124,7 @@ python tools/validation/operators/check_operator.py --op rms_norm_gated --candid ![gated RMSNorm vs existing implementations on B200](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-gated_rmsnorm.png) [`report.json`](https://github.com/fusheng-ji/RL-Kernel/blob/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/report.json) was written by -`tools/validation/models/qwen3_next/qwen3_next_norm_evidence.py` from a clean tree at `822b085`, on an otherwise idle +`tools/validation/models/qwen3_next/qwen3_next_norm_evidence.py` from a clean tree at `ff271d5`, on an otherwise idle B200 (torch 2.13.0+cu130, transformers 5.17.0, vLLM 0.30.0). Head dim 128, BF16; the FP64 golden uses vLLM's convention. The same report re-measures the zero-centred op ([figure](https://raw.githubusercontent.com/fusheng-ji/RL-Kernel/6d6193d94a83266d5f5f62bb9d90fb3215716ee3/pr468/docs/usage/evidence/qwen3-next-rms-norm-gated-b200/figure-zero_centred_rmsnorm.png)). diff --git a/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md b/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md index b29e5a1ce..f5940d137 100644 --- a/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-batch-invariance-b200.md @@ -4,7 +4,7 @@ ZIP SHA-256: `a6f7245c83d4e2fbbc949ba915abaf05a9b71d5fbeaaf241f846eab5addeb878`. -Measured at commit `759755e` (`measured-commit.txt`) from clean clones, one +Measured at commit `02c576c` (`measured-commit.txt`) from clean clones, one stage after another on one node with four B200 GPUs, in one environment (`environment_sha256` `118384371a88…` in every `run.json`): @@ -19,9 +19,9 @@ python tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py --last python benchmarks/models/qwen3_next/plot_qwen3_next_batch_invariance.py artifacts/qwen3-next-batch-invariance-b200 ``` -Commands use the current layout; at `759755e` the runners lived in `scripts/`, and the plot script is now in the Qwen3-Next benchmark PR. +Commands use the current layout; at `02c576c` the runners lived in `scripts/`, and the plot script is now in the Qwen3-Next benchmark PR. The archive preserves the original evidence bytes and `SHA256SUMS`; the publisher -at `3025497` only added site redactions. Extract the ZIP before inspecting its JSON. +at `f431bad` only added site redactions. Extract the ZIP before inspecting its JSON. | Stage | Result | | --- | --- | @@ -47,11 +47,11 @@ section. Site paths, host names and the scheduler are replaced by placeholders (``, ``, ``, ``, `/name`, `/file`). -## Rerun of the full suite on the stack top (`90c09164`) +## Rerun of the full suite on the stack top (`f05aaef6`) -The full 97-cell suite was run again from a clean clone of `90c09164` (TP4 x EP1, +The full 97-cell suite was run again from a clean clone of `f05aaef6` (TP4 x EP1, the same checkpoint and environment; `preflight`, `layers` and `l2` passed first -and `l2` is bitwise identical to the `c67ec14` run). Result: **97 / 97 cells +and `l2` is bitwise identical to the `92e93a4` run). Result: **97 / 97 cells bitwise** (concurrency 13 / 13, interference 10 / 10, chunk 66 / 66, packing 8 / 8), no failed cell, `first_failure: null`. The published copy is inside the chain archive of the full-model chain, diff --git a/reports/experiments/qwen3_next/qwen3-next-chain-b200.md b/reports/experiments/qwen3_next/qwen3-next-chain-b200.md index d80f87e3e..05fa61dfa 100644 --- a/reports/experiments/qwen3_next/qwen3-next-chain-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-chain-b200.md @@ -4,7 +4,7 @@ ZIP SHA-256: `76028dfea0a2b8698aa8d14d1f82e8da47df4ea11f9cd5240ebfb33094e527c1`. -Measured at commit `c67ec14` (`measured-commit.txt`) from clean clones, one +Measured at commit `92e93a4` (`measured-commit.txt`) from clean clones, one stage after another on nodes with four B200 GPUs, in one environment (`environment_sha256` `118384371a88…` in every `run.json`), on the official `Qwen/Qwen3-Next-80B-A3B-Instruct` revision @@ -23,10 +23,10 @@ python tools/validation/models/qwen3_next/qwen3_next_publish_evidence.py --last python benchmarks/models/qwen3_next/plot_qwen3_next_chain.py artifacts/qwen3-next-chain-b200 ``` -At `c67ec14` the runners lived in `scripts/` and `ci/`; the commands use the +At `92e93a4` the runners lived in `scripts/` and `ci/`; the commands use the current layout (the plot script is in the Qwen3-Next benchmark PR). The -stages ran the code of `c67ec14`; the published copy was made by the publisher -of `3025497`, which only adds redaction rules and changes no measured byte. +stages ran the code of `92e93a4`; the published copy was made by the publisher +of `f431bad`, which only adds redaction rules and changes no measured byte. | Stage | Checks | Result | | --- | --- | --- | @@ -47,11 +47,11 @@ Site paths, host names, users and the scheduler are replaced by placeholders (``, ``, ``, ``, ``); `redaction.json` counts them. -## Rerun on the stack top (`90c09164`) +## Rerun on the stack top (`f05aaef6`) After the stack was aligned with the reviewed layout (runners under `tools/validation/models/qwen3_next/`, code under `rl_engine/models/qwen3_next/`), -the whole TP4 x EP1 chain was run again from clean clones of `90c09164` +the whole TP4 x EP1 chain was run again from clean clones of `f05aaef6` (commit and tree in `measured-commit.txt` and every `run.json`), same environment and checkpoint as above: [raw results (ZIP)](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-rfc428-evidence/qwen3-next-chain-stacktop-b200.zip), SHA-256 `bde7e27c5db8d92d1171f08d7858231e4f8dee4e4831f60c3103c95505245f09` @@ -61,7 +61,7 @@ of the same commit). | Stage | Result | | --- | --- | | `preflight`, `layers` | passed | -| `l2` | 16 / 16 raw-bit; every artifact bitwise identical to the `c67ec14` run | +| `l2` | 16 / 16 raw-bit; every artifact bitwise identical to the `92e93a4` run | | `train1`, `train2` | passed, 16 / 16 raw-bit on the updated weights each | | `train` | passed: checkpoint reload, reload == update 2, continuous == resumed step 3, checkpoint fault injection; 16 / 16 raw-bit | | chain-gate report | `qwen3-next-chain-v1`, 64 cells, `passed: true`, `first_drift: null` | diff --git a/reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md b/reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md index e5fbc3d6e..6ac1873ef 100644 --- a/reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-distributed-logprob-b200.md @@ -11,15 +11,15 @@ clone with `rl_engine._C` built in place. | Directory | Measured at | Produced by | | --- | --- | --- | -| `parity/runs//rank-{0..3}.json` | `04db726` | ` torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_logprob_parity.py --checkpoint --output parity/runs/` | -| `parity/report.json` | `04db726` | `python tools/validation/models/qwen3_next/qwen3_next_logprob_parity_compare.py --runs parity/runs/* --out parity/report.json` | +| `parity/runs//rank-{0..3}.json` | `99c244a` | ` torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_logprob_parity.py --checkpoint --output parity/runs/` | +| `parity/report.json` | `99c244a` | `python tools/validation/models/qwen3_next/qwen3_next_logprob_parity_compare.py --runs parity/runs/* --out parity/report.json` | The 13 NCCL settings are the defaults plus `NCCL_ALGO` Ring and all-reduce Tree (`allreduce:tree`; NCCL 2.29 has no Tree all-gather) × `NCCL_PROTO` LL, LL128, Simple × one channel (`NCCL_MAX_NCHANNELS=1`) or the default channel count. The archive preserves the published JSON bytes and their `SHA256SUMS`, including the historical prior-art results used by the separate benchmark PR. The TP -parity measurements were collected at `04db726`; prior-art data at `33495ad`. +parity measurements were collected at `99c244a`; prior-art data at `c5cea58`. ## Results @@ -48,12 +48,12 @@ zip -r qwen3-next-distributed-logprob-b200.zip qwen3-next-distributed-logprob-b2 ## Post-review rerun (2026-10-12) -The relocated runners were executed from clean commit `b10b6616a24314a0656c36ac31131a8f9c0ee5ba` +The relocated runners were executed from clean commit `1106e461e30d724c700eeffa5df7550fc1389556` on four B200 GPUs with torch 2.13.0+cu130, vLLM 0.30.0, Triton 3.7.1, FlashInfer 0.6.18.post1 and NCCL 2.29.7, using the checkpoint revision above. The commands above reproduce this run with the relocated paths. -[Download new raw results, logs and provenance (ZIP)](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-pr549-evidence/qwen3-next-pr549-gpu-b10b6616.zip). +[Download new raw results, logs and provenance (ZIP)](https://github.com/fusheng-ji/RL-Kernel/releases/download/qwen3-next-pr549-evidence/qwen3-next-pr549-gpu-1106e461.zip). ZIP SHA-256: `5fb49bc151fc0224dc92128c0279fb58998435cfa033b62565d9a62957be0e57`. All 153 internal checksums were verified. Source paths are redacted; measured values, source commits and the imported extension's SHA-256 are retained. @@ -69,6 +69,6 @@ values, source commits and the imported extension's SHA-256 are retained. The archive also contains the separate benchmark PR's rerun at `67a45169`. This is operator and TP parity evidence; the full-model L2 and 97-cell batch -invariance runs were not repeated. Later commit `3927fad7` changes artifact +invariance runs were not repeated. Later commit `a9d0ba8f` changes artifact hashing for Python 3.10 compatibility and has CPU/CI coverage, not a separate GPU measurement. diff --git a/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md b/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md index a7d90d059..61a0aeb12 100644 --- a/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-moe-route-b200.md @@ -6,9 +6,9 @@ ZIP SHA-256: `9fcc704291bf697274edfe16d6958ca799134008acd9d5f452767b9a92758cb0`. The archive holds `report.json`, `figure.png`, the TP4 gate's `tp4-moe/rank-{0..3}.json` and the original README, byte for byte as measured, -plus `SHA256SUMS`. The prior art was measured at commit `c58816b` from a clean +plus `SHA256SUMS`. The prior art was measured at commit `dc79953` from a clean clone on one B200 (torch 2.13.0+cu130, Triton 3.7.1); the TP4 gate at -`2e950f3`, whose MoE code is identical, on four B200. The prior art uses +`cbdc00a`, whose MoE code is identical, on four B200. The prior art uses random weights (scale 0.02) at the TP1 shape (H=2048, 512 experts, top-10, width 512); the TP4 gate reads layer 0 of the official `Qwen/Qwen3-Next-80B-A3B-Instruct` revision @@ -27,7 +27,7 @@ TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen --checkpoint --output tp4-moe ``` -At `c58816b` / `2e950f3` the runners lived in `tools/validation/models/` and +At `dc79953` / `cbdc00a` the runners lived in `tools/validation/models/` and `rl_engine/validation/models/`; the commands above use the current layout. | Candidate | Forward batch invariant | Backward batch invariant | Different expert set (of 256 tokens) | Max abs vs FP64 | Forward, 1024 tokens | diff --git a/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md b/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md index 29537c4c4..c833bb9f0 100644 --- a/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md +++ b/reports/experiments/qwen3_next/qwen3-next-tp4-mixers-b200.md @@ -15,10 +15,10 @@ attention gate: layer 3). | Files | Measured at | Command (current layout) | | --- | --- | --- | -| `tp4-gdn/` | `9e6c4f2` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py --checkpoint --output tp4-gdn` | -| `tp4-attention/` | `9e6c4f2` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py --checkpoint --output tp4-attention` | -| `tp4-moe/` | `9e6c4f2` | `TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py --checkpoint --output tp4-moe` | -| `attention/` | `703c6dc` | `python benchmarks/models/qwen3_next/qwen3_next_attention_prior_art.py --only --out report-main.json`, then `--only te_fused --merge report-main.json --out report.json`; `python benchmarks/models/qwen3_next/plot_qwen3_next_attention_prior_art.py report.json` | +| `tp4-gdn/` | `fce346d` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_gdn_check.py --checkpoint --output tp4-gdn` | +| `tp4-attention/` | `fce346d` | `torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_attention_check.py --checkpoint --output tp4-attention` | +| `tp4-moe/` | `fce346d` | `TRITON_F32_DEFAULT=ieee torchrun --nproc-per-node 4 tools/validation/models/qwen3_next/qwen3_next_tp_moe_check.py --checkpoint --output tp4-moe` | +| `attention/` | `137d0fb` | `python benchmarks/models/qwen3_next/qwen3_next_attention_prior_art.py --only --out report-main.json`, then `--only te_fused --merge report-main.json --out report.json`; `python benchmarks/models/qwen3_next/plot_qwen3_next_attention_prior_art.py report.json` | At those commits the runners lived in `tools/validation/models/`; the attention prior-art runner is now part of the Qwen3-Next benchmark PR. From d53ac95edb2746e5948194c1df339b4391a29227 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 26 Oct 2026 21:53:31 +0000 Subject: [PATCH 57/68] ci(qwen3-next): trigger the provider job on validation/common check_qwen3_next_forward.py imports rl_engine.validation.common.tensor_identity for its raw-bit comparisons, so a change there must run the provider job. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/qwen3-next-provider-gpu.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/qwen3-next-provider-gpu.yml b/.github/workflows/qwen3-next-provider-gpu.yml index 30189fafb..299f184a8 100644 --- a/.github/workflows/qwen3-next-provider-gpu.yml +++ b/.github/workflows/qwen3-next-provider-gpu.yml @@ -32,6 +32,7 @@ on: - "tools/validation/models/qwen3_next/**" - "tests/models/qwen3_next/**" - "rl_engine/config/**" + - "rl_engine/validation/common/**" - ".github/workflows/qwen3-next-provider-gpu.yml" workflow_dispatch: inputs: From b9390f32134fa0572b3f8be03b71544b559b87b9 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 26 Oct 2026 21:53:33 +0000 Subject: [PATCH 58/68] ci(qwen3-next): trigger the provider job on the CUDA sources it builds From this PR the provider modules reach compiled code: the shared GDN core uses the gated RMSNorm kernel (csrc/cuda/norm) and the D=256 attention kernels are bound in csrc/bindings/ops.cpp; deterministic_attn imports rl_engine/contracts. rl_engine/backends/** only covers the Python wrappers, so these paths are added. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/qwen3-next-provider-gpu.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/qwen3-next-provider-gpu.yml b/.github/workflows/qwen3-next-provider-gpu.yml index 5c5a6f596..c3dbabc2a 100644 --- a/.github/workflows/qwen3-next-provider-gpu.yml +++ b/.github/workflows/qwen3-next-provider-gpu.yml @@ -34,6 +34,9 @@ on: - "tests/models/qwen3_next/**" - "rl_engine/config/**" - "rl_engine/validation/common/**" + - "csrc/cuda/norm/**" + - "csrc/bindings/**" + - "rl_engine/contracts/**" - ".github/workflows/qwen3-next-provider-gpu.yml" workflow_dispatch: inputs: From 73872d5cc70b6aaba1dfe5c0f77c4b34ef81d70d Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 26 Oct 2026 21:54:00 +0000 Subject: [PATCH 59/68] chore(qwen3-next): executable operator-gate script; say what is checked before the KV write run_qwen3_next_operator_gates.sh gets mode 100755 like run_qwen3_next_chain_gate.sh, so it also runs as ./ci/scripts/...; the preflight still calls it through bash. The comment above the KV-cache write in the shared attention bridge now says which checks precede the write and that the prefix finiteness check after it reads data from earlier steps. No logic change. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- ci/scripts/run_qwen3_next_operator_gates.sh | 0 .../engines/rollout/vllm/qwen3_next_vllm_attention.py | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) mode change 100644 => 100755 ci/scripts/run_qwen3_next_operator_gates.sh diff --git a/ci/scripts/run_qwen3_next_operator_gates.sh b/ci/scripts/run_qwen3_next_operator_gates.sh old mode 100644 new mode 100755 diff --git a/rl_engine/integrations/engines/rollout/vllm/qwen3_next_vllm_attention.py b/rl_engine/integrations/engines/rollout/vllm/qwen3_next_vllm_attention.py index c8bd85335..ab020bc70 100644 --- a/rl_engine/integrations/engines/rollout/vllm/qwen3_next_vllm_attention.py +++ b/rl_engine/integrations/engines/rollout/vllm/qwen3_next_vllm_attention.py @@ -175,7 +175,8 @@ def forward( plans.append((start, end, length, physical)) if any(not torch.isfinite(tensor[:n]).all() for tensor in (query, key, value)): raise ValueError("Projected attention inputs must be finite") - # Validate every request before mutating any scheduler-owned cache slot. + # Slots, block tables and the new Q/K/V are validated above, before this write into the + # scheduler-owned cache. The prefix check below reads back data written in earlier steps. blocks, offsets = slots // block_size, slots % block_size kv_cache[blocks, 0, offsets, :256] = key[:n, 0] kv_cache[blocks, 0, offsets, 256:] = value[:n, 0] From 5ffd4c317a3e8a75fff4e3dd51579c55b54ff5fd Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Mon, 26 Oct 2026 22:37:12 +0000 Subject: [PATCH 60/68] ci: run test_file_digest.py once in the CPU job The Qwen3-Next unit-test step listed tests/validation/common/test_file_digest.py again, although the dispatch step earlier in the same job already runs it. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/ci.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cd80c8c54..aadef1193 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -102,8 +102,7 @@ jobs: tests/models/qwen3_next/test_qwen3_next_run.py \ tests/models/qwen3_next/test_qwen3_next_evidence_redaction.py \ tests/models/qwen3_next/test_qwen3_next_bi_cases.py \ - tests/models/qwen3_next/test_qwen3_next_tp_reductions.py \ - tests/validation/common/test_file_digest.py + tests/models/qwen3_next/test_qwen3_next_tp_reductions.py - name: Run Cross-Configuration Contract Tests (CPU-safe) run: | From d78b22b38dd7e7f978d6968ed96d0ca7026cc9c4 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Tue, 27 Oct 2026 10:00:08 +0000 Subject: [PATCH 61/68] ci(qwen3-next): trigger the provider job on the code this PR's checks import From this PR the provider step depends on code outside the path filter: check_qwen3_next_paged_attention imports the vLLM rollout bridge (rl_engine/integrations/engines/rollout/vllm/qwen3_next_vllm_attention), which imports the VIME Qwen3-Next profile and, through the L2 validation module, rl_engine/integrations/common/qwen3_next_binary; the forward reaches vocab_parallel_logp, whose _C symbols are built from csrc/cuda/logprob. The last two were only added to the filter in #557. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/qwen3-next-provider-gpu.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/qwen3-next-provider-gpu.yml b/.github/workflows/qwen3-next-provider-gpu.yml index 94300ce16..4f2a218d1 100644 --- a/.github/workflows/qwen3-next-provider-gpu.yml +++ b/.github/workflows/qwen3-next-provider-gpu.yml @@ -37,6 +37,10 @@ on: - "csrc/cuda/norm/**" - "csrc/bindings/**" - "rl_engine/contracts/**" + - "csrc/cuda/logprob/**" + - "rl_engine/integrations/engines/rollout/vllm/qwen3_next*" + - "rl_engine/integrations/common/qwen3_next*" + - "rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/**" - ".github/workflows/qwen3-next-provider-gpu.yml" workflow_dispatch: inputs: From db1eb37f17f72ab803190a4fdc77aed7617e99a8 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Tue, 27 Oct 2026 10:00:10 +0000 Subject: [PATCH 62/68] ci(qwen3-next): trigger the provider job on the distributed collectives check_qwen3_next_bi_requests imports the native rollout, which reaches qwen3_next_tp through the vLLM bridge; qwen3_next_tp uses collective_for_group from rl_engine/distributed/algorithms/collectives.py, so a change there must run the provider job. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/qwen3-next-provider-gpu.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/qwen3-next-provider-gpu.yml b/.github/workflows/qwen3-next-provider-gpu.yml index 5a4a86a54..64269c85b 100644 --- a/.github/workflows/qwen3-next-provider-gpu.yml +++ b/.github/workflows/qwen3-next-provider-gpu.yml @@ -41,6 +41,7 @@ on: - "rl_engine/integrations/engines/rollout/vllm/qwen3_next*" - "rl_engine/integrations/common/qwen3_next*" - "rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/**" + - "rl_engine/distributed/algorithms/collectives.py" - ".github/workflows/qwen3-next-provider-gpu.yml" workflow_dispatch: inputs: From 62cc12257c40b485fbf63ca51c3c3ad0ada28bd7 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 13 Nov 2026 02:02:03 +0100 Subject: [PATCH 63/68] fix(workload): sync operator case ids with pytest collection a2037197 parametrised test_cuda_gated_parameter_vjp_contributions_match_the_fold over shape and added four norm tests, but the registry still pinned the old ids, so the preflight operator gate reported them as not found. Regenerated with qwen3_next_operator_cases.py --write. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../qwen3_next/qwen3_next_operator_cases.json | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json b/rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json index 1d8582f8e..24757f2bc 100644 --- a/rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json +++ b/rl_engine/validation/models/qwen3_next/qwen3_next_operator_cases.json @@ -23,6 +23,7 @@ "tests/models/qwen3_next/test_qwen3_next_norm.py::test_chunked_reduction_is_slice_invariant_on_device[128]", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_chunked_reduction_is_slice_invariant_on_device[2048]", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_default_offset_preserves_plain_convention", + "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_backward_is_offset_aware", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_backward_matches_autograd_golden", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_batch_invariance[16]", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_batch_invariance[1]", @@ -40,10 +41,14 @@ "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_dweight_is_batch_invariant[64]", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_dweight_is_batch_invariant[8]", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_matches_golden_within_contract", - "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[sigmoid-dtype0]", - "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[sigmoid-dtype1]", - "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[silu-dtype0]", - "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[silu-dtype1]", + "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[shape0-sigmoid-dtype0]", + "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[shape0-sigmoid-dtype1]", + "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[shape0-silu-dtype0]", + "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[shape0-silu-dtype1]", + "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[shape1-sigmoid-dtype0]", + "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[shape1-sigmoid-dtype1]", + "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[shape1-silu-dtype0]", + "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_parameter_vjp_contributions_match_the_fold[shape1-silu-dtype1]", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_rejects_bad_activation", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_rejects_mismatched_gate", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_cuda_gated_rstd_is_bitwise_identical_to_plain_kernel[bf16-128-sigmoid-0.0]", @@ -124,7 +129,10 @@ "tests/models/qwen3_next/test_qwen3_next_norm.py::test_gated_call_matches_forward[Qwen3NextRMSNormGatedHFOp]", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_gated_call_matches_forward[Qwen3NextRMSNormGatedOp]", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_gated_conventions_diverge_in_low_precision", + "tests/models/qwen3_next/test_qwen3_next_norm.py::test_gated_cuda_constructor_rejects_stale_api_version", + "tests/models/qwen3_next/test_qwen3_next_norm.py::test_gated_cuda_forward_raises_runtime_error_without_extension", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_gated_cuda_op_rejects_bad_activation_at_construction", + "tests/models/qwen3_next/test_qwen3_next_norm.py::test_gated_dtype_mismatch_names_both_dtypes", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_gated_extension_handles_empty_batch_and_nondefault_stream", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_gated_extension_rejects_invalid_tensor_contract[dy_dtype]", "tests/models/qwen3_next/test_qwen3_next_norm.py::test_gated_extension_rejects_invalid_tensor_contract[gate_dtype]", From fd87c0e115e0fa70124996a4dd5ba7ea8f6e174e Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 13 Nov 2026 02:02:54 +0100 Subject: [PATCH 64/68] feat(vime): load the Megatron model without DDP or an optimizer for recompute create_actor(training=False) built the full actor through setup_model_and_optimizer, which wraps the model in DDP and allocates optimizer state. The recompute/logprob path never steps, and on 80 GB GPUs that state does not fit. With training=False the model is now built through get_model(wrap_with_ddp=False) and loaded without an optimizer, and (args, model, None, None) is returned. The training=True path is unchanged. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../vime/providers/qwen3_next_vime_runtime.py | 23 ++++- .../test_qwen3_next_vime_runtime_actor.py | 95 +++++++++++++++++++ 2 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 tests/models/qwen3_next/test_qwen3_next_vime_runtime_actor.py diff --git a/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py b/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py index 8888dc15b..dee6dc534 100644 --- a/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py +++ b/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py @@ -177,10 +177,31 @@ def create_actor(checkpoint, *, training): if (args.vocab_size, args.padded_vocab_size) != (151936, 151936): raise ValueError("Tokenizer initialization changed the official LM-head vocabulary") from vime.backends.megatron_utils.checkpoint import load_checkpoint - from vime.backends.megatron_utils.model import setup_model_and_optimizer from rl_engine.models.qwen3_next.qwen3_next_logprob import install_vime_logprob_provider + if not training: + from megatron.core.enums import ModelType + from megatron.training.training import get_model + from vime.backends.megatron_utils.linear_logp_provider import linear_logp_provider_path + from vime.backends.megatron_utils.model import ( + _install_linear_logp_capture, + get_model_provider_func, + ) + + model = get_model( + get_model_provider_func(args, "actor"), + ModelType.encoder_or_decoder, + wrap_with_ddp=False, + ) + if linear_logp_provider_path(args) is not None: + _install_linear_logp_capture(model, args) + load_checkpoint(model, None, None, checkpointing_context={}) + install_vime_logprob_provider() + return args, model, None, None + + from vime.backends.megatron_utils.model import setup_model_and_optimizer + model, optimizer, scheduler = setup_model_and_optimizer(args, role="actor") load_checkpoint(model, optimizer, scheduler, checkpointing_context={}) install_vime_logprob_provider() diff --git a/tests/models/qwen3_next/test_qwen3_next_vime_runtime_actor.py b/tests/models/qwen3_next/test_qwen3_next_vime_runtime_actor.py new file mode 100644 index 000000000..be787657e --- /dev/null +++ b/tests/models/qwen3_next/test_qwen3_next_vime_runtime_actor.py @@ -0,0 +1,95 @@ +# SPDX-License-Identifier: Apache-2.0 +# Copyright (c) 2026 RL-Kernel Contributors + +"""CPU check of which native setup path ``create_actor`` takes; no vime or Megatron needed.""" + +import sys +from types import ModuleType, SimpleNamespace + +import pytest +import torch +import torch.distributed as dist + +from rl_engine.integrations.orchestrators.vime.providers import qwen3_next_vime_runtime as runtime + + +def _module(name, **attrs): + module = ModuleType(name) + module.__dict__.update(attrs) + return module + + +@pytest.fixture +def native(monkeypatch): + """Stub every native import so only the branching in ``create_actor`` runs.""" + calls = [] + args = SimpleNamespace(vocab_size=151936, padded_vocab_size=151936) + model = object() + + def setup_model_and_optimizer(args, role): + calls.append("setup_model_and_optimizer") + return [model], "optimizer", "scheduler" + + def get_model(provider, model_type, wrap_with_ddp): + calls.append(("get_model", wrap_with_ddp)) + return [model] + + def load_checkpoint(model, optimizer, scheduler, checkpointing_context): + calls.append(("load_checkpoint", optimizer, scheduler)) + + stubs = { + "vime": _module("vime"), + "vime.backends": _module("vime.backends"), + "vime.backends.megatron_utils": _module("vime.backends.megatron_utils"), + "vime.backends.megatron_utils.initialize": _module("i", init=lambda args: None), + "vime.backends.megatron_utils.checkpoint": _module("c", load_checkpoint=load_checkpoint), + "vime.backends.megatron_utils.linear_logp_provider": _module( + "l", linear_logp_provider_path=lambda args: None + ), + "vime.backends.megatron_utils.model": _module( + "m", + setup_model_and_optimizer=setup_model_and_optimizer, + get_model_provider_func=lambda args, role: None, + _install_linear_logp_capture=lambda model, args: calls.append("capture"), + ), + "vime.utils": _module("vime.utils"), + "vime.utils.arguments": _module("a", parse_args=lambda: args), + "vime.utils.distributed_utils": _module("d", init_gloo_group=lambda: None), + "megatron": _module("megatron"), + "megatron.core": _module("megatron.core"), + "megatron.core.enums": _module("e", ModelType=SimpleNamespace(encoder_or_decoder="eod")), + "megatron.training": _module("megatron.training"), + "megatron.training.training": _module("t", get_model=get_model), + "rl_engine.models.qwen3_next.qwen3_next_logprob": _module( + "p", install_vime_logprob_provider=lambda: calls.append("install") + ), + } + for name, module in stubs.items(): + monkeypatch.setitem(sys.modules, name, module) + monkeypatch.setattr(sys, "prefix", "/venv") + monkeypatch.setattr(sys, "base_prefix", "/base") + monkeypatch.setattr(dist, "is_initialized", lambda: True) + monkeypatch.setattr(dist, "get_world_size", lambda: 4) + monkeypatch.setattr(dist, "get_rank", lambda: 0) + monkeypatch.setattr(torch.cuda, "device_count", lambda: 4) + monkeypatch.setenv("LOCAL_RANK", "0") + monkeypatch.setattr(runtime, "runtime_argv", lambda checkpoint, **kwargs: ["prog"]) + monkeypatch.setattr(runtime, "require_policy_configuration", lambda args: None) + return calls, model + + +def test_inference_actor_loads_without_ddp_or_optimizer(native): + calls, model = native + _, actor_model, optimizer, scheduler = runtime.create_actor("ckpt", training=False) + assert ("get_model", False) in calls + assert "setup_model_and_optimizer" not in calls + assert ("load_checkpoint", None, None) in calls + assert (actor_model, optimizer, scheduler) == ([model], None, None) + + +def test_training_actor_still_builds_the_optimizer(native): + calls, _ = native + _, _, optimizer, scheduler = runtime.create_actor("ckpt", training=True) + assert "setup_model_and_optimizer" in calls + assert not any(isinstance(call, tuple) and call[0] == "get_model" for call in calls) + assert (optimizer, scheduler) == ("optimizer", "scheduler") From 1c681029faa44d0bf4783e051145f9bdf2ae3a60 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 13 Nov 2026 14:15:25 +0100 Subject: [PATCH 65/68] fix(vime): pass skip_load_to_model_and_opt to the VIME checkpoint loader VIME's load_checkpoint requires skip_load_to_model_and_opt; both create_actor calls omitted it, so the full-model l2 recompute failed with a TypeError before any model work. The stub in the actor test now requires it. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .../vime/providers/qwen3_next_vime_runtime.py | 8 ++++++-- .../qwen3_next/test_qwen3_next_vime_runtime_actor.py | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py b/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py index dee6dc534..bcc4f3795 100644 --- a/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py +++ b/rl_engine/integrations/orchestrators/vime/providers/qwen3_next_vime_runtime.py @@ -196,13 +196,17 @@ def create_actor(checkpoint, *, training): ) if linear_logp_provider_path(args) is not None: _install_linear_logp_capture(model, args) - load_checkpoint(model, None, None, checkpointing_context={}) + load_checkpoint( + model, None, None, checkpointing_context={}, skip_load_to_model_and_opt=False + ) install_vime_logprob_provider() return args, model, None, None from vime.backends.megatron_utils.model import setup_model_and_optimizer model, optimizer, scheduler = setup_model_and_optimizer(args, role="actor") - load_checkpoint(model, optimizer, scheduler, checkpointing_context={}) + load_checkpoint( + model, optimizer, scheduler, checkpointing_context={}, skip_load_to_model_and_opt=False + ) install_vime_logprob_provider() return args, model, optimizer, scheduler diff --git a/tests/models/qwen3_next/test_qwen3_next_vime_runtime_actor.py b/tests/models/qwen3_next/test_qwen3_next_vime_runtime_actor.py index be787657e..b7f0efefb 100644 --- a/tests/models/qwen3_next/test_qwen3_next_vime_runtime_actor.py +++ b/tests/models/qwen3_next/test_qwen3_next_vime_runtime_actor.py @@ -34,7 +34,10 @@ def get_model(provider, model_type, wrap_with_ddp): calls.append(("get_model", wrap_with_ddp)) return [model] - def load_checkpoint(model, optimizer, scheduler, checkpointing_context): + def load_checkpoint( + model, optimizer, scheduler, checkpointing_context, skip_load_to_model_and_opt + ): + assert skip_load_to_model_and_opt is False calls.append(("load_checkpoint", optimizer, scheduler)) stubs = { From 2e85d223daa7dfc8ad65d83282c6a73225643e2a Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:47:24 +0000 Subject: [PATCH 66/68] ci(qwen3-next): assert the full pinned runtime, retrigger on pyproject.toml, gate the remaining new tests Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- .github/workflows/ci.yml | 2 ++ .github/workflows/qwen3-next-provider-gpu.yml | 14 +++++++++++++- .../backends/cuda/attention/deterministic_attn.py | 2 ++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0500dd057..800222330 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -95,8 +95,10 @@ jobs: tests/models/qwen3_next/test_qwen3_next_rotary_dtype.py \ tests/models/qwen3_next/test_qwen3_next_storage.py \ tests/models/qwen3_next/test_qwen3_next_training_audit.py \ + tests/models/qwen3_next/test_qwen3_next_training_checkpoint.py \ tests/models/qwen3_next/test_qwen3_next_training_closeout.py \ tests/models/qwen3_next/test_qwen3_next_training_export.py \ + tests/models/qwen3_next/test_qwen3_next_vime_runtime_actor.py \ tests/models/qwen3_next/test_qwen3_next_subnode.py \ tests/models/qwen3_next/test_qwen3_next_chain_report.py \ tests/models/qwen3_next/test_qwen3_next_run.py \ diff --git a/.github/workflows/qwen3-next-provider-gpu.yml b/.github/workflows/qwen3-next-provider-gpu.yml index 4f2a218d1..7a0eee37d 100644 --- a/.github/workflows/qwen3-next-provider-gpu.yml +++ b/.github/workflows/qwen3-next-provider-gpu.yml @@ -41,6 +41,7 @@ on: - "rl_engine/integrations/engines/rollout/vllm/qwen3_next*" - "rl_engine/integrations/common/qwen3_next*" - "rl_engine/integrations/orchestrators/vime/experiments/qwen3_next/**" + - "pyproject.toml" - ".github/workflows/qwen3-next-provider-gpu.yml" workflow_dispatch: inputs: @@ -103,6 +104,16 @@ jobs: assert torch.__version__ == "2.13.0+cu130" assert vllm.__version__ == "0.30.0" assert torch.cuda.is_available() + + # The rest of the pinned set in pyproject.toml's qwen3-next extra. + from importlib.metadata import version + + for name, pinned in ( + ("transformers", "5.17.0"), + ("triton", "3.7.1"), + ("flashinfer-python", "0.6.18.post1"), + ): + assert version(name) == pinned, f"{name} {version(name)} != {pinned}" PY - name: Build the extension against that runtime # The runner is long-lived: remove any extension or build tree left by an earlier @@ -140,7 +151,8 @@ jobs: tests/models/qwen3_next/check_qwen3_next_gdn_bridge.py tests/models/qwen3_next/check_qwen3_next_gdn_sequence.py \ tests/models/qwen3_next/check_qwen3_next_conv_bridge.py tests/models/qwen3_next/check_qwen3_next_core_matrix.py \ tests/models/qwen3_next/check_qwen3_next_shared_core.py tests/models/qwen3_next/check_qwen3_next_paged_attention.py \ - tests/models/qwen3_next/check_qwen3_next_batch_invariance.py | tee "$log" + tests/models/qwen3_next/check_qwen3_next_batch_invariance.py \ + tests/models/qwen3_next/test_qwen3_next_vllm_attention_metadata.py | tee "$log" # Fail closed: these modules skip as a whole when CUDA or vLLM is missing, and pytest # exits 0 for an all-skipped run. A skipped provider check is not provider evidence. if grep -Eq '^SKIPPED|[0-9]+ skipped' "$log"; then diff --git a/rl_engine/backends/cuda/attention/deterministic_attn.py b/rl_engine/backends/cuda/attention/deterministic_attn.py index 8b6d9fc4c..628f04114 100644 --- a/rl_engine/backends/cuda/attention/deterministic_attn.py +++ b/rl_engine/backends/cuda/attention/deterministic_attn.py @@ -28,6 +28,8 @@ from rl_engine.utils.logger import logger _IS_ROCM = torch.version.hip is not None +# D=256 (Qwen3-Next full attention) is CUDA-only: ROCm has no 256 evidence, and the Triton twin +# still raises for any D other than 128 instead of diverging. _HEAD_DIMS = (128,) if _IS_ROCM else (128, 256) _GPU_PLATFORM = "ROCm" if _IS_ROCM else "CUDA" From 74a1b1a68b64ab2b896d32693b342dced5bf3e7a Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:48:39 +0000 Subject: [PATCH 67/68] ci(qwen3-next): reject a malformed chain report with an explicit check, not assert Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- ci/scripts/run_qwen3_next_chain_gate.sh | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/ci/scripts/run_qwen3_next_chain_gate.sh b/ci/scripts/run_qwen3_next_chain_gate.sh index a2bdba2f9..21f409c9d 100755 --- a/ci/scripts/run_qwen3_next_chain_gate.sh +++ b/ci/scripts/run_qwen3_next_chain_gate.sh @@ -15,11 +15,21 @@ import dataclasses, json, sys from rl_engine.validation.models.chain_gate import ChainGateReport from rl_engine.validation.models.qwen3_next.qwen3_next_chain_report import COMPATIBLE_SCHEMA, SCHEMA_VERSION + + +def invalid(message): + """Reject a malformed report with exit 2, apart from exit 1 for a failed gate.""" + print(f"invalid chain report: {message}", file=sys.stderr) + sys.exit(2) + + report = json.load(open(sys.argv[1])) missing = {f.name for f in dataclasses.fields(ChainGateReport)} - set(report) -assert not missing, f"chain report lacks ChainGateReport fields: {sorted(missing)}" -assert report["schema_version"] == SCHEMA_VERSION, report["schema_version"] -assert report["compatible_schema"] == COMPATIBLE_SCHEMA, report["compatible_schema"] +if missing: + invalid(f"lacks ChainGateReport fields: {sorted(missing)}") +for key, expected in (("schema_version", SCHEMA_VERSION), ("compatible_schema", COMPATIBLE_SCHEMA)): + if report.get(key) != expected: + invalid(f"{key} is {report.get(key)!r}, expected {expected!r}") print(f"passed={report['passed']} first_drift={report['first_drift']} " f"family={report['first_drift_family']} cells={len(report['cells'])}") sys.exit(0 if report["passed"] else 1) From 4e2c33afb151959cf886d31f204a964be7916620 Mon Sep 17 00:00:00 2001 From: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> Date: Sat, 14 Nov 2026 02:06:05 +0000 Subject: [PATCH 68/68] fix(ci): route unreadable chain reports to exit 2; fail closed on a missing TP group run_qwen3_next_chain_gate.sh read the report with a bare open() and indexed JSON-only keys, so a truncated file, a non-object report or one without first_drift_family raised a traceback and exited 1, the code reserved for a failed gate. Read it in a with-block, require an object and the payload-only first_drift_family, and send every such failure through invalid() (exit 2). The TP reduce dereferenced collective_for_group(...), which returns None for a missing group; raise a clear RuntimeError as the EP exchange does. Signed-off-by: Wenbo Ji <36562829+fusheng-ji@users.noreply.github.com> --- ci/scripts/run_qwen3_next_chain_gate.sh | 15 ++++++++++++--- rl_engine/models/qwen3_next/qwen3_next_tp.py | 2 ++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/ci/scripts/run_qwen3_next_chain_gate.sh b/ci/scripts/run_qwen3_next_chain_gate.sh index 21f409c9d..3c965dc23 100755 --- a/ci/scripts/run_qwen3_next_chain_gate.sh +++ b/ci/scripts/run_qwen3_next_chain_gate.sh @@ -23,10 +23,19 @@ def invalid(message): sys.exit(2) -report = json.load(open(sys.argv[1])) -missing = {f.name for f in dataclasses.fields(ChainGateReport)} - set(report) +try: + with open(sys.argv[1]) as handle: + report = json.load(handle) +except (OSError, ValueError) as error: + invalid(f"cannot read {sys.argv[1]!r} as JSON: {error}") +if not isinstance(report, dict): + invalid(f"top level is {type(report).__name__}, expected an object") +# first_drift_family is added to the payload by build_report; it is not a dataclass field. +missing = ({f.name for f in dataclasses.fields(ChainGateReport)} | {"first_drift_family"}) - set(report) if missing: - invalid(f"lacks ChainGateReport fields: {sorted(missing)}") + invalid(f"lacks report fields: {sorted(missing)}") +if not isinstance(report["cells"], dict): + invalid(f"cells is {type(report['cells']).__name__}, expected an object") for key, expected in (("schema_version", SCHEMA_VERSION), ("compatible_schema", COMPATIBLE_SCHEMA)): if report.get(key) != expected: invalid(f"{key} is {report.get(key)!r}, expected {expected!r}") diff --git a/rl_engine/models/qwen3_next/qwen3_next_tp.py b/rl_engine/models/qwen3_next/qwen3_next_tp.py index 876d373ff..b97443037 100644 --- a/rl_engine/models/qwen3_next/qwen3_next_tp.py +++ b/rl_engine/models/qwen3_next/qwen3_next_tp.py @@ -16,6 +16,8 @@ def _reduce(value, group): from rl_engine.distributed.algorithms.collectives import collective_for_group collective = collective_for_group(group, min_size_bytes=value.numel() * value.element_size()) + if collective is None: + raise RuntimeError("Qwen3-Next TP reduction needs a process group") return collective.all_reduce(value.contiguous())