From 8fd1ba261497fa00fec14b1a4301c771efc26993 Mon Sep 17 00:00:00 2001 From: netrunner <299844489+plotarmordev@users.noreply.github.com> Date: Fri, 20 Nov 2026 12:36:20 +0800 Subject: [PATCH 1/6] Upstream PR: Drama 3 on CUDA loads a compact NVFP4 checkpoint as stored The net change of #4144 at a57d9c23d4 over its merge base f35bfa46b3, as one commit: merge a57d9c23d4 carries its own change to zig/src/core/contract/registry_joins.zig. --- tools/zig/drama3_compact_gb10.sh | 52 ++++ tools/zig/drama3_limits_gate.sh | 7 + tools/zig/drama3_limits_gate_test.py | 73 ++++++ zig/kernels/cuda/drama3_moe.cuh | 55 +++++ zig/src/core/contract/registry_joins.zig | 3 +- zig/src/core/contract/registry_media.zig | 4 + zig/src/families/drama3/cuda/engine.zig | 1 + zig/src/families/drama3/cuda/forward.zig | 22 +- zig/src/families/drama3/cuda/kernels.zig | 38 ++- zig/src/families/drama3/cuda/nvfp4_test.zig | 231 ++++++++++++++++++ zig/src/families/drama3/cuda/weights.zig | 153 +++++++++++- zig/src/families/drama3/drama3.zig | 1 + .../families/drama3/metal/quant_format.zig | 15 +- .../drama3/metal/quant_format_test.zig | 17 +- zig/src/families/drama3/metal/weights.zig | 2 +- zig/tests/cuda/contract_drama3.zig | 31 ++- zig/tests/cuda/contract_drama3_router.zig | 188 ++++++++++++++ zig/tests/cuda/contract_tests.zig | 7 +- 18 files changed, 875 insertions(+), 25 deletions(-) create mode 100755 tools/zig/drama3_compact_gb10.sh create mode 100755 tools/zig/drama3_limits_gate.sh create mode 100644 tools/zig/drama3_limits_gate_test.py create mode 100644 zig/src/families/drama3/cuda/nvfp4_test.zig create mode 100644 zig/tests/cuda/contract_drama3_router.zig diff --git a/tools/zig/drama3_compact_gb10.sh b/tools/zig/drama3_compact_gb10.sh new file mode 100755 index 00000000000..2f63e445e01 --- /dev/null +++ b/tools/zig/drama3_compact_gb10.sh @@ -0,0 +1,52 @@ +#!/bin/sh +# Drama 3 compact NVFP4 on one GB10, the checks host tests cannot make; RECEIPT.txt and every log go to RECEIPT_DIR. +set -eu +[ "$#" -eq 6 ] || { echo 'usage: drama3_compact_gb10.sh PRE_FEATURE_COMMIT FP8_MODEL COMPACT_MODEL CODEC NVCC RECEIPT_DIR' >&2; exit 2; } +base=$1 fp8=$2 compact=$3 codec=$4 nvcc=$5 out=$6 +root=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd) +# the FP8 comparison needs a tree without the feature: once merged, merge-base with main is HEAD itself +if git -C "$root" grep -q d3_router_compact "$base" -- zig/kernels/cuda; then echo "$base already has d3_router_compact" >&2; exit 2; fi +mkdir -p "$out" +out=$(CDPATH= cd -- "$out" && pwd) +main_tree=$(mktemp -d "${TMPDIR:-/tmp}/drama3-main.XXXXXX") +trap 'git -C "$root" worktree remove --force "$main_tree"' EXIT +git -C "$root" worktree add --detach "$main_tree" "$base" >/dev/null +for tree in "$root" "$main_tree"; do + (cd "$tree" && zig build drama3-generate -Dsm=121 -Dnvcc="$nvcc" -Doptimize=ReleaseFast) +done +(cd "$root" && zig build install fatbins kernel-contract-fatbins -Dsm=80,86,89,120,121 -Dnvcc="$nvcc" -Doptimize=ReleaseFast) +text='The quick brown fox jumps over the lazy dog.' +run() { # TREE MODEL NAME [ARGS...]: greedy codes into NAME.codes, stderr into NAME.log + bin=$1/zig-out/bin/drama3-generate model=$2 name=$3 + shift 3 + "$bin" --model "$model" --text "$text" --greedy --max-frames 200 --out "$out/$name.codes" "$@" 2>"$out/$name.log" + tail -n 1 "$out/$name.log" | grep -q '^RESULT drama3 OK' +} +run "$main_tree" "$fp8" fp8-main +run "$root" "$fp8" fp8-head +cmp "$out/fp8-main.codes" "$out/fp8-head.codes" +run "$root" "$compact" compact --codec "$codec" --replay 1 +# a pruned checkpoint compacts only the layers that dropped experts: NVFP4 in all 40 and at least one compact router +grep -q 'routed experts NVFP4 in 40 of 40 layers; compact routers in [1-9][0-9]* of 40 layers' "$out/compact.log" +# batched prefill is gated within its own capacity: cross-capacity frames may differ near a tie (docs/drama3-prefill.md) +run "$root" "$compact" compact-batched --codec "$codec" --max-rows 64 --replay 1 +cmp -s "$out/compact.codes" "$out/compact-batched.codes" && capacity=equal || capacity=differ +# gates 1-4 and the unmasked control; the word matches no limits record, so the pending one waits for the audit below +"$root/zig-out/bin/tf-cuda-test" kernel-contract 'compact router' >"$out/contract.log" 2>&1 +status=0 +"$root/zig-out/bin/tf-cuda-test" kernel-contract-limits "$root/zig-out/fatbin" >"$out/limits.log" 2>&1 || status=$? +"$root/tools/zig/drama3_limits_gate.sh" "$status" "$out/limits.log" +{ + echo "head $(git -C "$root" rev-parse HEAD) pre-feature $base" + nvidia-smi --query-gpu=name,driver_version --format=csv,noheader + "$nvcc" --version | tail -n 1 + for f in "$fp8" "$compact" "$codec"; do echo "input $f: $(find "$f" -name '*.safetensors' -o -name '*.pth' | sort | xargs cat | sha256sum | cut -d' ' -f1)"; done + echo "PASS fp8 greedy codes byte-identical to $base: $(sha256sum "$out/fp8-head.codes" | cut -d' ' -f1)" + echo "PASS compact $(grep -o 'routed experts held.*' "$out/compact.log")" + echo "PASS compact decode and replay: $(tail -n 1 "$out/compact.log")" + echo "PASS compact batched prefill (64 rows) decode and replay: $(tail -n 1 "$out/compact-batched.log")" + echo "INFO compact codes at 1 and 64 prompt rows $capacity" + grep '^(filtered) SUMMARY' "$out/contract.log" + grep '^RECORD drama3 d3_router_compact:' "$out/limits.log" +} >"$out/RECEIPT.txt" +cat "$out/RECEIPT.txt" diff --git a/tools/zig/drama3_limits_gate.sh b/tools/zig/drama3_limits_gate.sh new file mode 100755 index 00000000000..744879ec16c --- /dev/null +++ b/tools/zig/drama3_limits_gate.sh @@ -0,0 +1,7 @@ +#!/bin/sh +# drama3_limits_gate.sh STATUS LOG: fails unless d3_router_compact was audited on all five SMs; stale records pass. +status=$1 log=$2 +grep -Eq '^(FAIL|MISSED) limits' "$log" && exit 1 +[ "$(grep -c '^PASS limits sm_[0-9]* drama3 d3_router_compact: ' "$log")" -eq 5 ] || exit 1 +[ "$status" -eq 0 ] && exit 0 +grep -qx "FAIL kernel-contract-limits: $(grep -c '^PASS limits .*(record stale)$' "$log") problems" "$log" diff --git a/tools/zig/drama3_limits_gate_test.py b/tools/zig/drama3_limits_gate_test.py new file mode 100644 index 00000000000..6e85e9bc948 --- /dev/null +++ b/tools/zig/drama3_limits_gate_test.py @@ -0,0 +1,73 @@ +"""drama3_limits_gate.sh: stale records pass; a failed launch, missing image or unaudited router fails.""" +import re +import subprocess +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +GATE = ROOT / "tools/zig/drama3_limits_gate.sh" +# a GB10 audit of the real registry (nvcc 13.0): 877 stale records, no failed launch or missed control +REAL = (ROOT / "docs/receipts/glm-fp4local-80517fc3/head-797dd2e1/single/limits.log").read_text().splitlines() +SMS = (80, 86, 89, 120, 121) +STALE = "PASS limits sm_{sm} {name}: 30 registers, 0 static shared bytes, bound 128 (record stale)" +ROUTER = "drama3 d3_router_compact" + + +def pending_tile_launches() -> list[str]: + """The K32 tile launches the registry holds unmeasured, stale under every nvcc.""" + source = (ROOT / "zig/src/core/contract/nvfp4_tile_pending.zig").read_text() + return re.findall(r'\.name = "([^"]+)"', source) + + +def audit(*names: str, extra: tuple[str, ...] = ()) -> list[str]: + """The real log plus each name's stale record on every SM, its problem count raised to match.""" + log = [line for line in REAL if not line.startswith("FAIL kernel-contract-limits")] + log += [STALE.format(sm=sm, name=name) for name in names for sm in SMS] + list(extra) + stale = sum(line.startswith("PASS limits") and line.endswith("(record stale)") for line in log) + return log + [f"FAIL kernel-contract-limits: {stale} problems"] + + +def gate(status: int, lines: list[str]) -> int: + with tempfile.NamedTemporaryFile("w", suffix=".log") as log: + log.write("\n".join(lines) + "\n") + log.flush() + return subprocess.run(["sh", str(GATE), str(status), log.name]).returncode + + +class LimitsGateTests(unittest.TestCase): + def test_clean_audit_passes(self): + lines = [STALE.format(sm=sm, name=ROUTER).replace("stale", "current") for sm in SMS] + self.assertEqual(gate(0, lines + ["PASS kernel-contract-limits: every registered launch fits"]), 0) + + def test_real_registry_with_pending_records_passes(self): + tiles = pending_tile_launches() + self.assertEqual(len(tiles), 2) + self.assertEqual(gate(1, audit(ROUTER, *tiles)), 0) + + def test_router_not_audited_fails(self): + self.assertNotEqual(gate(1, audit(*pending_tile_launches())), 0) + + def test_failed_launch_fails(self): + failed = "FAIL limits sm_121 nvfp4_experts_tile tf_nvfp4_tile_swiglu: 300 registers, 0 static shared bytes, bound 128 (record stale): too many registers" + self.assertNotEqual(gate(1, audit(ROUTER, extra=(failed,))), 0) + + def test_missing_image_fails(self): + missing = "FAIL limits drama3 d3_rmsnorm: zig-out/fatbin/drama3.fatbin holds no sm_89 image (build with -Dsm=80,86,89,120,121)" + self.assertNotEqual(gate(1, audit(ROUTER, extra=(missing,))), 0) + + def test_missed_control_fails(self): + missed = "MISSED limits sm_121 control: past its bounds: 10 registers, 0 static shared bytes, bound 256 (record current)" + self.assertNotEqual(gate(1, audit(ROUTER, extra=(missed,))), 0) + + def test_problem_count_beyond_stale_records_fails(self): + lines = audit(ROUTER) + lines[-1] = lines[-1].replace(" problems", "1 problems") + self.assertNotEqual(gate(1, lines), 0) + + def test_error_without_summary_fails(self): + self.assertNotEqual(gate(1, audit(ROUTER)[:-1]), 0) + + +if __name__ == "__main__": + unittest.main() diff --git a/zig/kernels/cuda/drama3_moe.cuh b/zig/kernels/cuda/drama3_moe.cuh index bcbd7e9f47a..4367680fa36 100644 --- a/zig/kernels/cuda/drama3_moe.cuh +++ b/zig/kernels/cuda/drama3_moe.cuh @@ -56,6 +56,61 @@ extern "C" __global__ void d3_router(const bf16* __restrict__ sp, int ss, int r_ } } +// d3_router over held ids only (slots[e] >= 0), picks as slots; a poisoned sum gives NaN weights, never a dropped id. +extern "C" __global__ void d3_router_compact(const bf16* __restrict__ sp, int ss, int r_off, int* __restrict__ picks, + float* __restrict__ weights, const int* __restrict__ slots) { + const int t = blockIdx.x, e = threadIdx.x; // 256 threads + const bool kept = slots[e] >= 0; + const float lg = kept ? b2f(sp[(size_t)t * ss + r_off + e]) : -INFINITY; + const float m = block_max(lg); + const float p = expf(lg - m); + const float sum = block_sum(p); + const bool poisoned = !isfinite(sum) || sum <= 0.f; + __shared__ float best_v[8]; + __shared__ int best_i[8]; + __shared__ float wv[32]; + __shared__ int wi[32]; + float mine = kept ? (poisoned ? 0.f : p / sum) : -1.f; + for (int k = 0; k < 8; ++k) { + float v = mine; + int idx = e; +#pragma unroll + for (int o = 16; o > 0; o >>= 1) { + const float ov = __shfl_xor_sync(FULL, v, o); + const int oi = __shfl_xor_sync(FULL, idx, o); + if (ov > v || (ov == v && oi < idx)) { + v = ov; + idx = oi; + } + } + if ((e & 31) == 0) { + wv[e >> 5] = v; + wi[e >> 5] = idx; + } + __syncthreads(); + if (e == 0) { + float bv = wv[0]; + int bi = wi[0]; + for (int j = 1; j < 8; ++j) + if (wv[j] > bv || (wv[j] == bv && wi[j] < bi)) { + bv = wv[j]; + bi = wi[j]; + } + best_v[k] = bv; + best_i[k] = bi; + } + __syncthreads(); + if (e == best_i[k]) mine = -1.f; + __syncthreads(); + } + if (e < 8) { + float s = 0.f; + for (int j = 0; j < 8; ++j) s += best_v[j]; + picks[t * 8 + e] = slots[best_i[e]]; + weights[t * 8 + e] = poisoned ? NAN : best_v[e] / s; + } +} + // y [T, N] = bf16(bf16(silu(g)) * u) with g, u at offsets of rows `stride` apart. extern "C" __global__ void d3_swiglu(const bf16* __restrict__ x, int stride, int g_off, int u_off, int N, bf16* __restrict__ y) { diff --git a/zig/src/core/contract/registry_joins.zig b/zig/src/core/contract/registry_joins.zig index 03358cb10aa..e696c6929e5 100644 --- a/zig/src/core/contract/registry_joins.zig +++ b/zig/src/core/contract/registry_joins.zig @@ -53,7 +53,8 @@ pub const cuda_pending = @import("qwen_whole_records.zig").records ++ @import("cuda_lane_pending.zig").records ++ @import("weight_only_records.zig").cuda ++ @import("hc_activation_records.zig").cuda ++ - @import("qwen4_records.zig").cuda; + @import("qwen4_records.zig").cuda ++ + @import("registry_media.zig").cuda_pending; pub const metal_before = @import("fsr_records.zig").metal ++ @import("scale_index_records.zig").metal ++ @import("lfm3_records.zig").metal; diff --git a/zig/src/core/contract/registry_media.zig b/zig/src/core/contract/registry_media.zig index 45a4b7db8ce..2d699f69303 100644 --- a/zig/src/core/contract/registry_media.zig +++ b/zig/src/core/contract/registry_media.zig @@ -62,3 +62,7 @@ pub const cuda = [_]CudaRecord{ .{ .launch = .{ .name = "fp8_experts prompt down f32", .symbol = "_ZN14tf_fp8_experts24fp8_expert_prompt_kernelILi1ELi0ELi4ELi4EEEvPK13__nv_bfloat16iiPK5uint4PKfiiPKiSA_SA_Pvifi", .image = "fp8_experts", .threads = 128 }, .usage = .{ .{ .regs = 197, .static = 32768, .bound = 128 }, .{ .regs = 197, .static = 32768, .bound = 128 }, .{ .regs = 197, .static = 32768, .bound = 128 }, .{ .regs = 221, .static = 32768, .bound = 128 }, .{ .regs = 221, .static = 32768, .bound = 128 } } }, .{ .launch = .{ .name = "fp8_experts prompt down bf16", .symbol = "_ZN14tf_fp8_experts24fp8_expert_prompt_kernelILi1ELi3ELi4ELi4EEEvPK13__nv_bfloat16iiPK5uint4PKfiiPKiSA_SA_Pvifi", .image = "fp8_experts", .threads = 128 }, .usage = .{ .{ .regs = 197, .static = 32768, .bound = 128 }, .{ .regs = 197, .static = 32768, .bound = 128 }, .{ .regs = 197, .static = 32768, .bound = 128 }, .{ .regs = 221, .static = 32768, .bound = 128 }, .{ .regs = 221, .static = 32768, .bound = 128 } } }, }; +/// Drama 3's compact-checkpoint router, unmeasured: the GB10 check runs its contract and prints its RECORD. +pub const cuda_pending = [_]CudaRecord{ + .{ .launch = .{ .name = "drama3 d3_router_compact", .symbol = "d3_router_compact", .image = "drama3", .threads = 256 }, .usage = @import("registry.zig").none }, +}; diff --git a/zig/src/families/drama3/cuda/engine.zig b/zig/src/families/drama3/cuda/engine.zig index d67836ee1f4..eabe0006fcd 100644 --- a/zig/src/families/drama3/cuda/engine.zig +++ b/zig/src/families/drama3/cuda/engine.zig @@ -142,6 +142,7 @@ pub const Engine = struct { e.w.deinit(); gpa.destroy(e.w); } + if (e.w.nvfp4_layers > 0) try e.k.loadNvfp4(); errdefer e.freeState(); try e.allocState(); errdefer { diff --git a/zig/src/families/drama3/cuda/forward.zig b/zig/src/families/drama3/cuda/forward.zig index 66762c720aa..4db9d3c1277 100644 --- a/zig/src/families/drama3/cuda/forward.zig +++ b/zig/src/families/drama3/cuda/forward.zig @@ -82,12 +82,16 @@ pub fn backboneLayer(e: *Engine, ln: Lane, li: usize, t: usize, start: usize) !v try moe(e, s, L, t); } -/// The MoE block: router, gated shared expert and the routed FP8 experts, added to the residual stream. +/// The MoE block: router (held experts only when compact), gated shared expert and routed FP8 or NVFP4 experts. fn moe(e: *Engine, s: cuda.Stream, L: *const wts.Layer, t: usize) !void { const k = &e.k; const mp = cfg.moe_proj; try k.linear(s, L.moe_in, e.xn, H, H, mp, e.sp, mp, t); - try k.router(s, e.sp, mp, 2 * cfg.shared_width, e.picks, e.rw, t); + if (L.slots != 0) { + try k.routerCompact(s, e.sp, mp, 2 * cfg.shared_width, e.picks, e.rw, L.slots, t); + } else { + try k.router(s, e.sp, mp, 2 * cfg.shared_width, e.picks, e.rw, t); + } try k.swiglu(s, e.sp, mp, 0, cfg.shared_width, cfg.shared_width, e.sact, t); try k.linear(s, L.shared_down, e.sact, cfg.shared_width, cfg.shared_width, H, e.shout, H, t); const pairs = t * cfg.top_k; @@ -95,11 +99,15 @@ fn moe(e: *Engine, s: cuda.Stream, L: *const wts.Layer, t: usize) !void { const tile: usize = if (prompt) cuda.experts.promptTile(.fp8g) else 16; try k.plan.route(s, e.picks, pairs, cfg.experts, tile, e.plan); const items = cuda.grouped.maxItems(pairs, cfg.experts, tile); - // activations to FP8 per row and 128 inputs before each expert projection, as the reference serves them - try k.actQuant(s, e.xn, H, e.xq, H, e.xsc, t, H); - try k.experts(s, true, prompt, e.xq, H, cfg.top_k, e.xsc, L.experts, e.plan, e.gu, items); - try k.actQuant(s, e.gu, cfg.expert_width, e.gq, cfg.expert_width, e.gsc, pairs, cfg.expert_width); - try k.experts(s, false, prompt, e.gq, cfg.expert_width, 0, e.gsc, L.experts, e.plan, e.down, items); + if (L.experts.format == .nvfp4) { + try k.nvfp4Experts(s, prompt, e.xn, cfg.top_k, L.experts, e.plan, e.gu, e.down, items); + } else { + // activations to FP8 per row and 128 inputs before each expert projection, as the reference serves them + try k.actQuant(s, e.xn, H, e.xq, H, e.xsc, t, H); + try k.experts(s, true, prompt, e.xq, H, cfg.top_k, e.xsc, L.experts, e.plan, e.gu, items); + try k.actQuant(s, e.gu, cfg.expert_width, e.gq, cfg.expert_width, e.gsc, pairs, cfg.expert_width); + try k.experts(s, false, prompt, e.gq, cfg.expert_width, 0, e.gsc, L.experts, e.plan, e.down, items); + } try k.moeCombine(s, e.x, e.down, e.rw, e.shout, e.sp, mp, 2 * cfg.shared_width + cfg.experts, H, t); } diff --git a/zig/src/families/drama3/cuda/kernels.zig b/zig/src/families/drama3/cuda/kernels.zig index 3f1319057b9..4b6699e4065 100644 --- a/zig/src/families/drama3/cuda/kernels.zig +++ b/zig/src/families/drama3/cuda/kernels.zig @@ -1,4 +1,4 @@ -//! Drama 3's launches: drama3.cu by symbol, the FP8 experts (drama3_fp8.cu) with grouped.zig's plan, cuBLAS GEMMs. +//! Drama 3's launches: drama3.cu by symbol, the FP8 or NVFP4 experts with grouped.zig's plan, cuBLAS GEMMs. const std = @import("std"); const cuda = @import("cuda"); const blas = @import("blas.zig"); @@ -17,7 +17,7 @@ const names = .{ "d3_causal_softmax", "d3_attn_gate", "d3_router", "d3_swiglu", "d3_moe_combine", "d3_tower_mix", "d3_sample_main", "d3_sample_code", "d3_argmax_main", "d3_finish_frame", "d3_copy_i32", "d3_act_quant", "d3_bf16_f32", "d3_next_input", "d3_attn_partial", - "d3_attn_merge", "d3_sample_main_fast", "d3_sample_code_fast", + "d3_attn_merge", "d3_sample_main_fast", "d3_sample_code_fast", "d3_router_compact", }; pub const F = enum(u8) { @@ -49,6 +49,7 @@ pub const F = enum(u8) { attn_merge, sample_main_fast, sample_code_fast, + router_compact, }; /// drama3_fp8.cu's instances: [decode, prompt][gate-up with the SwiGLU epilogue, down to fp32]. @@ -68,6 +69,7 @@ pub const Kernels = struct { fp8a_mod: cuda.Module, fp8a: [2][2]cuda.Function, fp8a_resident: [2][2]usize, + nvfp4: ?cuda.experts.Nvfp4Experts = null, // loaded by loadNvfp4 when a layer's experts are NVFP4 blas: blas.Blas, ws: cuda.DeviceBuffer, sms: usize, @@ -77,6 +79,7 @@ pub const Kernels = struct { if (!cuda.kernels.available) return error.BuiltWithoutKernels; var k: Kernels = undefined; k.d = d; + k.nvfp4 = null; k.sms = @intCast(try ctx.attribute(.multiprocessor_count)); k.mod = try cuda.Module.load(d, cuda.kernels.drama3); errdefer k.mod.unload(); @@ -100,6 +103,7 @@ pub const Kernels = struct { } pub fn unload(k: *Kernels) void { + if (k.nvfp4) |*n| n.unload(); k.blas.close(); k.ws.free(); k.fp8a_mod.unload(); @@ -107,6 +111,10 @@ pub const Kernels = struct { k.mod.unload(); } + pub fn loadNvfp4(k: *Kernels) !void { + if (k.nvfp4 == null) k.nvfp4 = try cuda.experts.Nvfp4Experts.load(k.d, k.sms); + } + fn f(k: *const Kernels, which: F) cuda.Function { return k.fns[@backingInt(which)]; } @@ -319,6 +327,18 @@ pub const Kernels = struct { try k.go(.router, s, t, 1, 256, 0, &a); } + /// router over held experts only: `slots` (int32 [256], -1 where not held) masks, and turns picks into slots. + pub fn routerCompact(k: *const Kernels, s: cuda.Stream, sp: u64, ss: usize, r_off: usize, picks: u64, weights: u64, slots: u64, t: usize) !void { + var a: cuda.Args = .{}; + a.add(sp); + a.add(i(ss)); + a.add(i(r_off)); + a.add(picks); + a.add(weights); + a.add(slots); + try k.go(.router_compact, s, t, 1, 256, 0, &a); + } + pub fn swiglu(k: *const Kernels, s: cuda.Stream, x: u64, stride: usize, g_off: usize, u_off: usize, n: usize, y: u64, t: usize) !void { var a: cuda.Args = .{}; a.add(x); @@ -445,6 +465,20 @@ pub const Kernels = struct { try cuda.launch.launch(k.fp8a[pi][ei], .{ .grid = .{ .x = @intCast(grid) }, .block = .{ .x = 128 } }, s, &a); } + /// Routed NVFP4 experts on bf16 rows: SwiGLU pair rows, then fp32 down rows; prompts use runFact's decode sums. + pub fn nvfp4Experts(k: *const Kernels, s: cuda.Stream, prompt: bool, x: u64, slots: usize, l: cuda.experts.Layer, p: cuda.grouped.Plan, gu: u64, down: u64, items: usize) !void { + const e = if (k.nvfp4) |*n| n else return error.FormatNotLoaded; + const up: cuda.experts.Rows = .{ .x = x, .stride = l.dims, .slots = slots }; + const pairs: cuda.experts.Rows = .{ .x = gu, .stride = l.width }; + if (prompt) { + try e.runFact(s, .up, up, l, p, gu, items, -1); + try e.runFact(s, .down_f32, pairs, l, p, down, items, -1); + } else { + try e.run(s, .up, up, l, p, gu, items, -1); + try e.run(s, .down_f32, pairs, l, p, down, items, -1); + } + } + pub fn bf16F32(k: *const Kernels, s: cuda.Stream, x: u64, y: u64, n: usize) !void { var a: cuda.Args = .{}; a.add(x); diff --git a/zig/src/families/drama3/cuda/nvfp4_test.zig b/zig/src/families/drama3/cuda/nvfp4_test.zig new file mode 100644 index 00000000000..76ff1d54210 --- /dev/null +++ b/zig/src/families/drama3/cuda/nvfp4_test.zig @@ -0,0 +1,231 @@ +//! Compact NVFP4 experts on the host, no CUDA context: staged as stored in held-slot order, decoded against FP64. +const std = @import("std"); +const core = @import("core"); +const cuda = @import("cuda"); +const cfg = @import("../config.zig"); +const wts = @import("weights.zig"); +const quant = @import("../metal/quant_format.zig"); +const ref = @import("../metal/quant_reference.zig"); +const Shards = @import("../shards.zig").Shards; + +const testing = std.testing; +const io = testing.io; +const E = cuda.experts.Nvfp4Experts; +const held_ids = [_]u8{ 3, 9, 250 }; +const projections = [_][]const u8{ "gate_proj", "up_proj", "down_proj" }; +const shape = [3][2]usize{ .{ cfg.expert_width, cfg.hidden }, .{ cfg.expert_width, cfg.hidden }, .{ cfg.hidden, cfg.expert_width } }; + +/// Layer 0's held experts in NVFP4 as tf-drama-quant writes them; layer 1's expert 0 gate in FP8. +fn writeCheckpoint(arena: std.mem.Allocator, dir: []const u8) !void { + var prng = std.Random.DefaultPrng.init(27); + const rnd = prng.random(); + var tensors: std.ArrayList(core.shard_edit.New) = .empty; + for (held_ids) |e| for (projections, shape) |p, s| { + const name = try std.fmt.allocPrint(arena, "model.language_model.layers.0.mlp.experts.{d}.{s}.weight", .{ e, p }); + const codes = try arena.alloc(u8, s[0] * s[1] / 2); + rnd.bytes(codes); + const scales = try arena.alloc(u8, s[0] * s[1] / 16); + for (scales) |*b| b.* = 0x30 + rnd.uintLessThan(u8, 0x30); + const global = try arena.dupe(u8, std.mem.asBytes(&(0.5 + rnd.float(f32)))); + try tensors.append(arena, .{ .name = name, .dtype = .u8, .shape = try arena.dupe(usize, &.{ s[0], s[1] / 2 }), .bytes = codes }); + try tensors.append(arena, .{ .name = try std.fmt.allocPrint(arena, "{s}_scale", .{name}), .dtype = .f8_e4m3, .shape = try arena.dupe(usize, &.{ s[0], s[1] / 16 }), .bytes = scales }); + try tensors.append(arena, .{ .name = try std.fmt.allocPrint(arena, "{s}_scale_2", .{name}), .dtype = .f32, .shape = &.{}, .bytes = global }); + }; + const fp8 = try arena.alloc(u8, cfg.expert_width * cfg.hidden); + rnd.bytes(fp8); + try tensors.append(arena, .{ .name = "model.language_model.layers.1.mlp.experts.0.gate_proj.weight", .dtype = .f8_e4m3, .shape = &.{ cfg.expert_width, cfg.hidden }, .bytes = fp8 }); + try core.shard_edit.create(arena, try std.fs.path.join(arena, &.{ dir, "model.safetensors" }), &.{.{ "format", "pt" }}, tensors.items); + var index: std.ArrayList(u8) = .empty; + try index.appendSlice(arena, "{\"metadata\": {}, \"weight_map\": {"); + for (tensors.items, 0..) |t, i| try index.print(arena, "{s}\"{s}\": \"model.safetensors\"", .{ if (i == 0) "" else ", ", t.name }); + try index.appendSlice(arena, "}}"); + try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = try std.fs.path.join(arena, &.{ dir, "model.safetensors.index.json" }), .data = index.items }); +} + +/// Code and e4m3 scale of (row, input) in matrix `part` of `parts` packed blocks, as experts.zig documents the layout. +fn unpacked(blocks: []const u32, parts: usize, part: usize, k: usize, row: usize, input: usize) struct { u8, u8 } { + const rr = row % E.cols; + const ii = input % 32; + const h = ii / 16; + const block = blocks[(((row / E.cols) * (k / 32) + input / 32) * parts + part) * E.words ..][0..E.words]; + const word = block[((rr % 8) * 4 + (ii % 16) / 4) * 4 + rr / 8]; + const scale = std.mem.sliceAsBytes(block[128..])[((((rr % 8) / 2) * 2 + h) * 4 + rr / 8) * 2 + rr % 2]; + return .{ @intCast((word >> E.slot(h, ii % 4)) & 0xF), scale }; +} + +/// nvfp4_experts.cu in f32: each 16-group's dot from zero, fmaf by its e4m3 scale into the row sum, then the global. +fn servedDot(codes: []const u8, scales: []const u8, x: []const f32, global: f32) f32 { + var sum: f32 = 0; + for (scales, 0..) |scale, b| { + var dot: f32 = 0; + for (codes[16 * b ..][0..16], x[16 * b ..][0..16]) |code, v| dot = @mulAdd(f32, @floatCast(ref.fp4(code)), v, dot); + sum = @mulAdd(f32, dot, @floatCast(ref.fp8(scale)), sum); + } + return sum * global; +} + +/// A staged row through servedDot. +fn servedRow(blocks: []const u32, parts: usize, part: usize, k: usize, row: usize, x: []const f32, global: f32) f32 { + var codes: [cfg.hidden]u8 = undefined; + var scales: [cfg.hidden / 16]u8 = undefined; + for (0..k) |i| codes[i], scales[i / 16] = unpacked(blocks, parts, part, k, row, i); + return servedDot(codes[0..k], scales[0 .. k / 16], x[0..k], global); +} + +/// Round to nearest even bf16 by the integer trick the kernels' __float2bfloat16_rn performs. +fn bf(x: f32) f32 { + const b: u32 = @bitCast(x); + return @bitCast((b +% 0x7fff +% ((b >> 16) & 1)) & 0xffff0000); +} + +/// nvfp4_experts.cu's SwiGLU epilogue, stored as bf16: bf16(bf16(silu(bf16(g))) * bf16(u)). +fn servedSwiglu(g: f32, u: f32) f32 { + const gv = bf(g); + return bf(bf(gv / (1 + @exp(-gv))) * bf(u)); +} + +/// The CUDA rounding contract in FP64, as drama3's FP8 path (d3_swiglu) and bf16 torch round: silu rounds to bf16. +fn contractSwiglu(gate: f64, up: f64) f64 { + const g = ref.roundBf16(gate); + return ref.roundBf16(ref.roundBf16(g / (1 + @exp(-g))) * ref.roundBf16(up)); +} + +fn bits(v: f64) u16 { + return @truncate(@as(u32, @bitCast(@as(f32, @floatCast(v)))) >> 16); +} + +/// One bf16 step at |v|: the most an f32 sum's last bit can move a bf16-rounded result. +fn ulp(v: f64) f64 { + return if (v == 0) 0x1p-133 else std.math.ldexp(@as(f64, 1), std.math.ilogb(@abs(v)) - 7); +} + +test "SwiGLU rounds silu to bf16 before the up product: the served epilogue and the FP64 contract at a tie" { + try testing.expectEqual(@as(f32, -0.00335693359375), servedSwiglu(-4, 0.046875)); + try testing.expectEqual(@as(f64, -0.00335693359375), contractSwiglu(-4, 0.046875)); + // unrounded silu (Metal's reference) lands one bf16 step away: this case pins the rounding point + try testing.expectEqual(@as(f64, -0.0033721923828125), ref.swiglu(-4, 0.046875)); + try testing.expectEqual(contractSwiglu(1.0078125, -3), servedSwiglu(1.0078125, -3)); + try testing.expectEqual(contractSwiglu(-0.5, 0.0009765625), servedSwiglu(-0.5, 0.0009765625)); +} + +/// servedDot and the FP64 oracle on one crafted row of 32 inputs: codes, one e4m3 scale a group, bf16 inputs. +fn crafted(codes: [32]u8, scales: [2]u8, x: [32]f32) struct { f32, f64 } { + var packed_codes: [16]u8 = undefined; + for (&packed_codes, 0..) |*p, i| p.* = codes[2 * i] | codes[2 * i + 1] << 4; + var xb: [32]u16 = undefined; + for (&xb, x) |*b, v| b.* = bits(v); + return .{ servedDot(&codes, &scales, &x, 1), ref.projection(&packed_codes, &scales, 1, 0, 32, &xb) }; +} + +test "served NVFP4 rows scale each 16-group's dot, not each term: overflow and cancellation the FP64 oracle lacks" { + const six = 0x7; // fp4 6 + const one = 0x2; // fp4 1 + const neg_one = 0xA; + var codes: [32]u8 = @splat(0); + var x: [32]f32 = @splat(0); + // 16 x (6 * 2^125) leaves f32 inside the group's dot, before e4m3 1/512 (0x01) would bring it back + @memset(codes[0..16], six); + @memset(x[0..16], 0x1p125); + var got, var want = crafted(codes, .{ 0x01, 0 }, x); + try testing.expect(std.math.isInf(got)); + try testing.expectEqual(@as(f64, 96 * 0x1p125 / 512), want); + // 2^24 + 1 - 2^24 in a group: its f32 dot drops the 1 before e4m3 3 (0x44); per-term scaling gives 4, FP64 3 + codes = @splat(0); + x = @splat(0); + codes[0..3].* = .{ one, one, neg_one }; + x[0..3].* = .{ 0x1p24, 1, 0x1p24 }; + got, want = crafted(codes, .{ 0x44, 0 }, x); + try testing.expectEqual(@as(f32, 0), got); + try testing.expectEqual(@as(f64, 3), want); +} + +test "compact NVFP4 experts stage as stored in held-slot order and decode gate-up, SwiGLU, down and combine to FP64" { + var arena_state = std.heap.ArenaAllocator.init(testing.allocator); + defer arena_state.deinit(); + const arena = arena_state.allocator(); + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + const dir = try tmp.dir.realPathFileAlloc(io, ".", arena); + try writeCheckpoint(arena, dir); + var sh = try Shards.open(testing.allocator, io, dir); + defer sh.close(io); + + var held: quant.Held = .{}; + held.counts[0] = held_ids.len; + @memcpy(held.ids[0][0..held_ids.len], &held_ids); + try testing.expect(try wts.nvfp4Layer(&sh, 0, &held)); + var stage = try wts.stageNvfp4(testing.allocator, &sh, 0, held.ids[0][0..held.counts[0]], 2); + defer stage.deinit(testing.allocator); + + var prng = std.Random.DefaultPrng.init(5); + var x: [cfg.hidden]u16 = undefined; + for (&x) |*v| v.* = 0x3c00 + prng.random().uintLessThan(u16, 0x400) + (@as(u16, prng.random().int(u1)) << 15); + var xf: [cfg.hidden]f32 = undefined; + for (&xf, x) |*f, b| f.* = @floatCast(ref.fromBf16(b)); + const slots = wts.compactSlots(&held, 0).?; + const per_up = stage.up.len / held_ids.len; + const per_down = stage.down.len / held_ids.len; + const route = [held_ids.len]f32{ 0.5, 0.3125, 0.1875 }; + var want_moe: [cfg.hidden]f64 = @splat(0); + var got_moe: [cfg.hidden]f32 = @splat(0); + for (slots, 0..) |slot, id| { + // an original id the router can pick reaches the slot holding its own weights; the rest are masked + const k_id = std.mem.indexOfScalar(u8, &held_ids, @intCast(id)) orelse { + try testing.expectEqual(@as(i32, -1), slot); + continue; + }; + try testing.expectEqual(@as(i32, @intCast(k_id)), slot); + const s: usize = @intCast(slot); + var stored: [3]struct { w: []const u8, sc: []const u8, global: f32 } = undefined; + for (projections, &stored, 0..) |p, *t, j| { + var nb: [3][128]u8 = undefined; + const base = "model.language_model.layers.0.mlp.experts.{d}.{s}.weight"; + const g = try sh.get(try std.fmt.bufPrint(&nb[2], base ++ "_scale_2", .{ id, p })); + t.* = .{ .w = (try sh.get(try std.fmt.bufPrint(&nb[0], base, .{ id, p }))).bytes, .sc = (try sh.get(try std.fmt.bufPrint(&nb[1], base ++ "_scale", .{ id, p }))).bytes, .global = @bitCast(std.mem.readInt(u32, g.bytes[0..4], .little)) }; + try testing.expectEqual(t.global, if (j < 2) stage.up_scale[2 * s + j] else stage.down_scale[s]); + } + const up = stage.up[s * per_up ..][0..per_up]; + const down = stage.down[s * per_down ..][0..per_down]; + var want_act: [cfg.expert_width]u16 = undefined; + var got_act: [cfg.expert_width]f32 = undefined; + for (0..cfg.expert_width) |row| { + const want = contractSwiglu(ref.projection(stored[0].w, stored[0].sc, stored[0].global, row, cfg.hidden, &x), ref.projection(stored[1].w, stored[1].sc, stored[1].global, row, cfg.hidden, &x)); + got_act[row] = servedSwiglu(servedRow(up, 2, 0, cfg.hidden, row, &xf, stored[0].global), servedRow(up, 2, 1, cfg.hidden, row, &xf, stored[1].global)); + // past gate -88 the served expf(-g) overflows and silu is 0 where FP64 keeps ~1e-37 + try testing.expect(@abs(got_act[row] - want) <= ulp(want) + 0x1p-100); + want_act[row] = bits(want); + } + for (0..cfg.hidden) |row| { + want_moe[row] += route[k_id] * ref.projection(stored[2].w, stored[2].sc, stored[2].global, row, cfg.expert_width, &want_act); + got_moe[row] = @mulAdd(f32, route[k_id], servedRow(down, 1, 0, cfg.expert_width, row, &got_act, stored[2].global), got_moe[row]); + } + } + // d3_moe_combine rounds the weighted sum of the fp32 down rows once; an activation's last-bit flip stays below 1% + for (want_moe, got_moe) |want, got| try testing.expect(@abs(bf(got) - ref.roundBf16(want)) <= ulp(want) + @abs(want) / 128); +} + +test "a compact map over FP8 experts is refused; a full FP8 layer keeps the FP8 path" { + var arena_state = std.heap.ArenaAllocator.init(testing.allocator); + defer arena_state.deinit(); + const arena = arena_state.allocator(); + var tmp = testing.tmpDir(.{}); + defer tmp.cleanup(); + const dir = try tmp.dir.realPathFileAlloc(io, ".", arena); + try writeCheckpoint(arena, dir); + var sh = try Shards.open(testing.allocator, io, dir); + defer sh.close(io); + + var held: quant.Held = .{}; + try testing.expect(!try wts.nvfp4Layer(&sh, 1, &held)); + held.counts[1] = cfg.top_k; + try testing.expectError(error.CompactFp8Experts, wts.nvfp4Layer(&sh, 1, &held)); +} + +test "an unpruned NVFP4 checkpoint keeps the original router in every layer, so it cannot pass as compact" { + const full = try quant.heldMetadata(null); + for (0..cfg.layers) |li| try testing.expectEqual(@as(?[cfg.experts]i32, null), wts.compactSlots(&full, li)); + var pruned = full; + pruned.counts[7] = cfg.experts - 1; + try testing.expectEqual(@as(i32, -1), wts.compactSlots(&pruned, 7).?[cfg.experts - 1]); + try testing.expectEqual(@as(?[cfg.experts]i32, null), wts.compactSlots(&pruned, 6)); +} diff --git a/zig/src/families/drama3/cuda/weights.zig b/zig/src/families/drama3/cuda/weights.zig index 0f7182bb991..4701f4133bc 100644 --- a/zig/src/families/drama3/cuda/weights.zig +++ b/zig/src/families/drama3/cuda/weights.zig @@ -1,10 +1,11 @@ -//! Drama 3's checkpoint on the device: stacked bf16 projections, FP8 experts repacked for drama3_fp8.cu, a cut lm_head. +//! Drama 3's checkpoint on the device: bf16 projections, FP8 experts repacked or NVFP4 as stored, a cut lm_head. const std = @import("std"); const staging = @import("stager.zig"); const cuda = @import("cuda"); const core = @import("core"); const cfg = @import("../config.zig"); const shards = @import("../shards.zig"); +const quant = @import("../metal/quant_format.zig"); const Shards = shards.Shards; const fp8 = core.quant.fp8; @@ -21,6 +22,7 @@ pub const Layer = struct { moe_in: u64, // [1288, 2048] shared_down: u64, // [2048, 512] experts: cuda.experts.Layer, + slots: u64, // int32 [256]: an original expert id's held slot, -1 if not held; 0 when every expert is held }; pub const TowerLayer = struct { @@ -51,6 +53,8 @@ pub const Weights = struct { layers: [cfg.layers]Layer, tower: [cfg.tower_layers]TowerLayer, heads: [cfg.codebooks - 1]u64, // [4096, 2048] + nvfp4_layers: usize, // layers whose routed experts are NVFP4 (the engine loads their kernels) + compact_layers: usize, // layers whose router masks to held experts (a pruned checkpoint) bytes: usize, pub fn deinit(w: *Weights) void { @@ -105,6 +109,137 @@ const Loader = struct { } return b.ptr; } + + fn upload(l: *Loader, comptime T: type, data: []const T) !u64 { + const b = try l.alloc(data.len * @sizeOf(T)); + try b.upload(0, std.mem.sliceAsBytes(data)); + return b.ptr; + } + + /// A layer's held NVFP4 experts on the device as stored (packed, never widened), with its slot table if compact. + fn nvfp4Experts(l: *Loader, li: usize, held: *const quant.Held, threads: usize) !void { + const ids = held.ids[li][0..held.counts[li]]; + var stage = try stageNvfp4(l.gpa, l.sh, li, ids, threads); + defer stage.deinit(l.gpa); + const L = &l.w.layers[li]; + L.experts = .{ .format = .nvfp4, .up = try l.upload(u32, stage.up), .down = try l.upload(u32, stage.down), .up_scale = try l.upload(f32, stage.up_scale), .down_scale = try l.upload(f32, stage.down_scale), .width = cfg.expert_width, .dims = cfg.hidden, .experts = ids.len }; + L.slots = if (compactSlots(held, li)) |slots| try l.upload(i32, &slots) else 0; + if (L.slots != 0) l.w.compact_layers += 1; + } +}; + +/// A pruned layer's router slot table; null when every expert is held and the original router serves it. +pub fn compactSlots(held: *const quant.Held, li: usize) ?[cfg.experts]i32 { + return if (held.counts[li] == cfg.experts) null else held.slots(li); +} + +/// NVFP4 when a layer's first held gate is packed bytes; a compact map over FP8 experts is refused. +pub fn nvfp4Layer(sh: *const Shards, li: usize, held: *const quant.Held) !bool { + var nb: [160]u8 = undefined; + const t = try sh.get(fmt(&nb, "model.language_model.layers.{d}.mlp.experts.{d}.gate_proj.weight", .{ li, held.ids[li][0] })); + if (t.dtype == .u8) return true; + return if (held.counts[li] == cfg.experts) false else error.CompactFp8Experts; +} + +/// One layer's held NVFP4 experts in slot order as nvfp4_experts.cu reads them; slot i is the map's i-th id. +pub const Nvfp4Stage = struct { + up: []u32, // [held][512/32][2048/32][gate, up][144] + down: []u32, // [held][2048/32][512/32][1][144] + up_scale: []f32, // [held][gate, up] global scales + down_scale: []f32, // [held] + + fn init(gpa: std.mem.Allocator, held: usize) !Nvfp4Stage { + const E = cuda.experts.Nvfp4Experts; + const up = try gpa.alloc(u32, E.bytes(held, cfg.expert_width, cfg.hidden, 2) / 4); + errdefer gpa.free(up); + const down = try gpa.alloc(u32, E.bytes(held, cfg.hidden, cfg.expert_width, 1) / 4); + errdefer gpa.free(down); + const up_scale = try gpa.alloc(f32, held * 2); + errdefer gpa.free(up_scale); + return .{ .up = up, .down = down, .up_scale = up_scale, .down_scale = try gpa.alloc(f32, held) }; + } + + pub fn deinit(s: *Nvfp4Stage, gpa: std.mem.Allocator) void { + gpa.free(s.up); + gpa.free(s.down); + gpa.free(s.up_scale); + gpa.free(s.down_scale); + } +}; + +/// Packs a layer's held NVFP4 experts (`ids`: the map's ascending original ids) on `threads` workers. +pub fn stageNvfp4(gpa: std.mem.Allocator, sh: *const Shards, li: usize, ids: []const u8, threads: usize) !Nvfp4Stage { + var out = try Nvfp4Stage.init(gpa, ids.len); + errdefer out.deinit(gpa); + const n = @max(1, @min(threads, 64, ids.len)); + const jobs = try gpa.alloc(Nvfp4Job, n); + defer gpa.free(jobs); + const workers = try gpa.alloc(std.Thread, n); + defer gpa.free(workers); + var spawned: usize = 0; + defer for (workers[0..spawned]) |t| t.join(); + for (jobs, workers, 0..) |*j, *t, i| { + j.* = .{ .sh = sh, .layer = li, .ids = ids, .first = i, .stride = n, .out = &out }; + t.* = try std.Thread.spawn(.{}, Nvfp4Job.run, .{j}); + spawned += 1; + } + for (workers[0..spawned]) |t| t.join(); + spawned = 0; + for (jobs) |j| if (j.err) |e| return e; + return out; +} + +const Nvfp4Job = struct { + sh: *const Shards, + layer: usize, + ids: []const u8, + first: usize, + stride: usize, + out: *const Nvfp4Stage, + err: ?anyerror = null, + + const projections = [_][]const u8{ "gate_proj", "up_proj", "down_proj" }; + const Part = struct { codes: []const u8, scales: []const u8, global: f32 }; + + fn run(j: *Nvfp4Job) void { + j.work() catch |e| { + j.err = e; + }; + } + + fn work(j: *Nvfp4Job) !void { + const E = cuda.experts.Nvfp4Experts; + const n = cfg.expert_width; + const k = cfg.hidden; + const per_up = E.bytes(1, n, k, 2) / 4; + const per_down = E.bytes(1, k, n, 1) / 4; + var slot = j.first; + while (slot < j.ids.len) : (slot += j.stride) { + const e = j.ids[slot]; + const g = try j.part(e, 0, n, k); + const u = try j.part(e, 1, n, k); + const d = try j.part(e, 2, k, n); + E.packGateUp(j.out.up[slot * per_up ..][0..per_up], .{ g.codes, g.scales }, .{ u.codes, u.scales }, n, k); + E.packOne(j.out.down[slot * per_down ..][0..per_down], d.codes, d.scales, k, n); + j.out.up_scale[2 * slot] = g.global; + j.out.up_scale[2 * slot + 1] = u.global; + j.out.down_scale[slot] = d.global; + var nb: [160]u8 = undefined; + for (projections) |p| for ([_][]const u8{ "", "_scale", "_scale_2" }) |suffix| + j.sh.release(fmt(&nb, "model.language_model.layers.{d}.mlp.experts.{d}.{s}.weight{s}", .{ j.layer, e, p, suffix })); + } + } + + /// Projection `p` of expert `e` ([rows, cols] as stored), checked by the Metal loader's quant_format.nvfp4. + fn part(j: *const Nvfp4Job, e: u8, p: usize, rows: usize, cols: usize) !Part { + var nb: [3][160]u8 = undefined; + const base = "model.language_model.layers.{d}.mlp.experts.{d}.{s}.weight"; + const w = try j.sh.get(fmt(&nb[0], base, .{ j.layer, e, projections[p] })); + const s = try j.sh.get(fmt(&nb[1], base ++ "_scale", .{ j.layer, e, projections[p] })); + const g = try j.sh.get(fmt(&nb[2], base ++ "_scale_2", .{ j.layer, e, projections[p] })); + try quant.nvfp4(w, s, g, rows, cols); + return .{ .codes = w.bytes, .scales = s.bytes, .global = @bitCast(std.mem.readInt(u32, g.bytes[0..4], .little)) }; + } }; fn tn(buf: []u8, prefix: []const u8, idx: usize, suffix: []const u8) []const u8 { @@ -180,6 +315,9 @@ pub const LoadOptions = struct { threads: usize = 16, log: bool = true }; pub fn load(gpa: std.mem.Allocator, io: std.Io, d: *const cuda.Driver, dir: []const u8, c: cfg.Config, opt: LoadOptions) !*Weights { var sh = try Shards.open(gpa, io, dir); defer sh.close(io); + // a malformed, unordered or mismatched expert map fails here, before any device memory + const held = try quant.heldMetadata(sh.metadata); + try quant.checkStored(&held, sh.map); var stream = try cuda.Stream.init(d, false); defer stream.deinit(); var stager = staging.Stager.init(gpa, d, stream); @@ -191,6 +329,8 @@ pub fn load(gpa: std.mem.Allocator, io: std.Io, d: *const cuda.Driver, dir: []co w.gpa = gpa; w.bufs = .empty; w.bytes = 0; + w.nvfp4_layers = 0; + w.compact_layers = 0; errdefer w.deinit(); errdefer stream.synchronize() catch {}; var l: Loader = .{ .gpa = gpa, .d = d, .sh = &sh, .w = w, .stager = &stager }; @@ -264,6 +404,12 @@ pub fn load(gpa: std.mem.Allocator, io: std.Io, d: *const cuda.Driver, dir: []co } L.moe_in = try l.stack(&.{ fmt(&nb[0], pre ++ "mlp.shared_expert.gate_proj.weight", .{li}), fmt(&nb[1], pre ++ "mlp.shared_expert.up_proj.weight", .{li}), fmt(&nb[2], pre ++ "mlp.gate.weight", .{li}), fmt(&nb[3], pre ++ "mlp.shared_expert_gate.weight", .{li}) }, &.{ cfg.shared_width, cfg.shared_width, cfg.experts, 1 }, H, cfg.moe_proj); L.shared_down = try l.one(fmt(&nb[0], pre ++ "mlp.shared_expert.down_proj.weight", .{li}), &.{ H, cfg.shared_width }); + L.slots = 0; + if (try nvfp4Layer(&sh, li, &held)) { + try l.nvfp4Experts(li, &held, opt.threads); + w.nvfp4_layers += 1; + continue; + } // experts for (jobs, 0..) |*j, ti| j.* = .{ .sh = &sh, .layer = li, .first = ti, .stride = nthreads, .up = up_h, .down = down_h, .up_scale = us_h, .down_scale = ds_h }; var spawned: usize = 0; @@ -309,7 +455,10 @@ pub fn load(gpa: std.mem.Allocator, io: std.Io, d: *const cuda.Driver, dir: []co } for (0..cfg.codebooks - 1) |hi| w.heads[hi] = try l.one(fmt(&nb[0], "audio_decoder_mtp.heads.{d}.weight", .{hi}), &.{ cfg.codebook_size, H }); try stager.finish(); - if (opt.log) std.log.info("drama3: weights on the device: {d:.2} GB", .{@as(f64, @floatFromInt(w.bytes)) / 1e9}); + if (opt.log) { + std.log.info("drama3: weights on the device: {d:.2} GB", .{@as(f64, @floatFromInt(w.bytes)) / 1e9}); + std.log.info("drama3: routed experts held {d} of {d}; routed experts NVFP4 in {d} of {d} layers; compact routers in {d} of {d} layers", .{ held.total(), cfg.layers * cfg.experts, w.nvfp4_layers, cfg.layers, w.compact_layers, cfg.layers }); + } return w; } diff --git a/zig/src/families/drama3/drama3.zig b/zig/src/families/drama3/drama3.zig index 91f22076e09..dc94302dd2f 100644 --- a/zig/src/families/drama3/drama3.zig +++ b/zig/src/families/drama3/drama3.zig @@ -42,5 +42,6 @@ test { test { _ = @import("cuda/owner_test.zig"); + _ = @import("cuda/nvfp4_test.zig"); _ = @import("cuda/weights.zig"); } diff --git a/zig/src/families/drama3/metal/quant_format.zig b/zig/src/families/drama3/metal/quant_format.zig index a3e4959b377..72e1d0c0c0a 100644 --- a/zig/src/families/drama3/metal/quant_format.zig +++ b/zig/src/families/drama3/metal/quant_format.zig @@ -17,6 +17,11 @@ pub const Held = struct { for (h.ids[layer][0..h.counts[layer]], 0..) |id, slot| out[id] = @intCast(slot); return out; } + pub fn total(h: *const Held) usize { + var n: usize = 0; + for (h.counts) |c| n += c; + return n; + } }; pub fn parseHeld(value: std.json.Value) !Held { @@ -43,8 +48,9 @@ pub fn heldMetadata(metadata: ?std.json.Value) !Held { return if (meta.object.get("drama_experts")) |v| try parseHeld(v) else .{}; } -pub fn checkNames(h: *const Held, names: anytype) !void { - var it = names; +/// The stored experts are exactly the map's: none unlisted, and every listed one's three projections present. +pub fn checkStored(h: *const Held, names: anytype) !void { + var it = names.keyIterator(); const pre = "model.language_model.layers."; while (it.next()) |key| { const name = key.*; @@ -57,6 +63,11 @@ pub fn checkNames(h: *const Held, names: anytype) !void { const e = std.fmt.parseInt(u8, parts.next() orelse return error.BadExpertMap, 10) catch return error.BadExpertMap; if (l >= cfg.layers or std.mem.indexOfScalar(u8, h.ids[l][0..h.counts[l]], e) == null) return error.UnlistedExpert; } + var buf: [96]u8 = undefined; + for (0..cfg.layers) |l| for (h.ids[l][0..h.counts[l]]) |e| for ([_][]const u8{ "gate_proj", "up_proj", "down_proj" }) |p| { + const name = std.fmt.bufPrint(&buf, pre ++ "{d}.mlp.experts.{d}.{s}.weight", .{ l, e, p }) catch unreachable; + if (!names.contains(name)) return error.MissingExpert; + }; } pub fn nvfp4(w: Tensor, s: Tensor, g: Tensor, n: usize, k: usize) !void { diff --git a/zig/src/families/drama3/metal/quant_format_test.zig b/zig/src/families/drama3/metal/quant_format_test.zig index 747124a5190..cfba80317f7 100644 --- a/zig/src/families/drama3/metal/quant_format_test.zig +++ b/zig/src/families/drama3/metal/quant_format_test.zig @@ -47,7 +47,7 @@ fn mapText(a: std.mem.Allocator, ids: []const u8, omit: bool) ![]u8 { return text.toOwnedSlice(a); } -test "expert metadata is complete ascending unique and keeps at least eight original IDs" { +test "expert metadata is complete ascending unique keeps at least eight original IDs and matches the stored experts" { const a = std.testing.allocator; const good = "[1,3,5,7,9,11,13,255]"; for ([_][]const u8{ good, "[1,3,5,7,9,11,13,13]", "[3,1,5,7,9,11,13,15]", "[1,3,5,7,9,11,13,256]", "[1,3]", "[1,3,5,7,9,11,13,1.5]" }, 0..) |ids, i| { @@ -60,10 +60,19 @@ test "expert metadata is complete ascending unique and keeps at least eight orig const slots = h.slots(0); try std.testing.expectEqual(@as(i32, -1), slots[0]); try std.testing.expectEqual(@as(i32, 7), slots[255]); + try std.testing.expectEqual(@as(usize, cfg.layers * 8), h.total()); + var arena: std.heap.ArenaAllocator = .init(a); + defer arena.deinit(); var names = std.StringHashMapUnmanaged(void).empty; - defer names.deinit(a); - try names.put(a, "model.language_model.layers.0.mlp.experts.0.gate_proj.weight", {}); - try std.testing.expectError(error.UnlistedExpert, q.checkNames(&h, names.keyIterator())); + for (0..cfg.layers) |l| for ([_]u8{ 1, 3, 5, 7, 9, 11, 13, 255 }) |e| for ([_][]const u8{ "gate_proj", "up_proj", "down_proj" }) |p| + try names.put(arena.allocator(), try std.fmt.allocPrint(arena.allocator(), "model.language_model.layers.{d}.mlp.experts.{d}.{s}.weight", .{ l, e, p }), {}); + try q.checkStored(&h, names); + const extra = "model.language_model.layers.0.mlp.experts.0.gate_proj.weight"; + try names.put(arena.allocator(), extra, {}); + try std.testing.expectError(error.UnlistedExpert, q.checkStored(&h, names)); + _ = names.remove(extra); + _ = names.remove("model.language_model.layers.39.mlp.experts.255.down_proj.weight"); + try std.testing.expectError(error.MissingExpert, q.checkStored(&h, names)); } else try std.testing.expectError(error.BadExpertMap, q.parseHeld(tree.value)); } const text = try mapText(a, good, true); diff --git a/zig/src/families/drama3/metal/weights.zig b/zig/src/families/drama3/metal/weights.zig index 36f543a1b3d..2dfb995c14b 100644 --- a/zig/src/families/drama3/metal/weights.zig +++ b/zig/src/families/drama3/metal/weights.zig @@ -184,7 +184,7 @@ pub fn load(g: *Gpu, io: std.Io, dir: []const u8, c: cfg.Config) !*Weights { w.sh = try Shards.open(g.gpa, io, dir); errdefer w.sh.close(io); const held = try quant.heldMetadata(w.sh.metadata); - try quant.checkNames(&held, w.sh.map.keyIterator()); + try quant.checkStored(&held, w.sh.map); const first = g.bufs.items.len; const indirect_first = g.indirect.items.len; errdefer { diff --git a/zig/tests/cuda/contract_drama3.zig b/zig/tests/cuda/contract_drama3.zig index 641524d3158..ba124d3a7e9 100644 --- a/zig/tests/cuda/contract_drama3.zig +++ b/zig/tests/cuda/contract_drama3.zig @@ -1,9 +1,10 @@ -//! Drama 3's RMS norm rows (d3_rmsnorm with its plain gamma, the gated-norm form) under the contract on CUDA. +//! Drama 3's kernels under the contract on CUDA: RMS norm rows (d3_rmsnorm, plain gamma) and the compact router. const std = @import("std"); const cuda = @import("cuda"); const core = @import("core"); const check = @import("check.zig"); const rig_ = @import("contract_rig.zig"); +const router_k = @import("contract_drama3_router.zig"); const contract = core.contract; const c = contract.spec; @@ -86,3 +87,31 @@ pub const Norm = struct { return self.r.?.simulate(acc, order, out); } }; + +/// The drama3 image, its norm and its compact router with the unmasked control, appended to `kernels`. +pub const Group = struct { + a: std.mem.Allocator, + mod: cuda.Module, + norm: Norm, + routers: [2]router_k.Router, + + pub fn create(a: std.mem.Allocator, gpu: check.Gpu, s: cuda.Stream, kernels: *std.ArrayList(g.Kernel)) !*Group { + const self = try a.create(Group); + errdefer a.destroy(self); + self.a = a; + self.mod = try cuda.Module.load(gpu.d, cuda.kernels.drama3); + errdefer self.mod.unload(); + self.norm = try Norm.init(gpu, s, self.mod); + self.routers = .{ try .init(gpu, s, self.mod, false), try .init(gpu, s, self.mod, true) }; + try kernels.append(a, self.norm.kernel()); + for (&self.routers) |*r| try kernels.append(a, r.kernel()); + return self; + } + + pub fn destroy(self: *Group) void { + self.norm.deinit(); + for (&self.routers) |*r| r.deinit(); + self.mod.unload(); + self.a.destroy(self); + } +}; diff --git a/zig/tests/cuda/contract_drama3_router.zig b/zig/tests/cuda/contract_drama3_router.zig new file mode 100644 index 00000000000..aca318fadc4 --- /dev/null +++ b/zig/tests/cuda/contract_drama3_router.zig @@ -0,0 +1,188 @@ +//! Drama 3's compact router (d3_router_compact) under the contract on CUDA: picks and weights against FP64. +const std = @import("std"); +const cuda = @import("cuda"); +const core = @import("core"); +const Gpu = @import("check.zig").Gpu; +const rig_ = @import("contract_rig.zig"); + +const contract = core.contract; +const c = contract.spec; +const g = contract.gates; + +const experts = 256; +const top = 8; +// drama3/config.zig's moe_proj rows: [shared gate 512 | up 512 | router 256 | shared gate 1 | pad 7] +const stride: usize = 1288; +const r_off: usize = 1024; + +fn noSums(_: c.Shape) usize { + return 0; +} + +const rule = "only held logits count; a NaN or +inf held logit, or none finite, gives NaN weights over the lowest held ids"; + +/// Rows: tokens; n: experts held of 256 (8 up); k: the router's 256 logits. Prefill, decode, a whole map. +pub fn spec() c.Spec { + return .{ + .name = "cuda drama3 compact router d3_router_compact", + .accum = .none, + .depth = noSums, + .out = .raw, + .nan = .{ .stated = rule }, + .tiles = .{ .{}, .{ .size = 9, .step = 1, .min = 8 }, .{} }, + .served = &.{ .{ .rows = 64, .n = 64, .k = experts }, .{ .rows = 1, .n = 64, .k = experts }, .{ .rows = 64, .n = 256, .k = experts }, .{ .rows = 64, .n = 8, .k = experts } }, + }; +} + +pub const Router = struct { + rig: rig_.Rig, + f: cuda.Function, + control: bool = false, // f is d3_router: no mask, original ids, must fail gate 3 + shape: c.Shape = undefined, + logits: []u16 = &.{}, + slots: [experts]i32 = undefined, + held: [experts]u8 = undefined, + sp: u64 = 0, + sl: u64 = 0, + picks: u64 = 0, + weights: u64 = 0, + msg: [200]u8 = undefined, + + pub fn init(gpu: Gpu, s: cuda.Stream, mod: cuda.Module, control: bool) !Router { + return .{ .rig = .{ .gpu = gpu, .s = s }, .f = try mod.function(if (control) "d3_router" else "d3_router_compact"), .control = control }; + } + + pub fn deinit(self: *Router) void { + self.rig.gpu.gpa.free(self.logits); + self.rig.deinit(); + } + + pub fn kernel(self: *Router) g.Kernel { + var k: g.Kernel = .{ .spec = spec(), .ctx = self, .vt = &vt, .control = if (self.control) .shapes else null }; + if (self.control) k.spec.name = "cuda drama3 compact router control: d3_router, unmasked"; + return k; + } + + const vt = g.hooks(Router); + + fn logit(self: *const Router, t: usize, id: usize) f64 { + return c.fromBf16(self.logits[t * stride + r_off + id]); + } + + /// Held: a random n of 256 in shuffled slots; excluded logits above every held one; held on a 1/4 grid (ties). + pub fn prepare(self: *Router, shape: c.Shape, fill: g.Fill, seed: u64) !bool { + if (shape.k != experts or shape.n < top or shape.n > experts) return false; + self.rig.reset(); + self.shape = shape; + var prng = std.Random.DefaultPrng.init(seed); + const rng = prng.random(); + var ids: [experts]u8 = undefined; + for (&ids, 0..) |*id, i| id.* = @intCast(i); + rng.shuffle(u8, &ids); + @memcpy(self.held[0..shape.n], ids[0..shape.n]); + self.slots = @splat(-1); + for (self.held[0..shape.n], 0..) |id, slot| self.slots[id] = @intCast(slot); + const a = self.rig.gpu.gpa; + a.free(self.logits); + self.logits = &.{}; + self.logits = try a.alloc(u16, shape.rows * stride); + @memset(self.logits, c.toBf16(std.math.nan(f32))); // outside the router window: a misread offset poisons + for (0..shape.rows) |t| for (0..experts) |id| { + const v: f32 = if (self.slots[id] >= 0) @as(f32, @floatFromInt(rng.uintLessThan(u32, 24))) / 4 - 3 else 8 + @as(f32, @floatFromInt(rng.uintLessThan(u32, 8))); + self.logits[t * stride + r_off + id] = c.toBf16(v); + }; + if (fill == .nonfinite) { + const h = self.held[0..shape.n]; + const ex = for (0..experts) |id| { + if (self.slots[id] < 0) break id; + } else null; + if (ex) |id| self.logits[r_off + id] = c.toBf16(std.math.nan(f32)); // row 0: masked away + self.logits[stride + r_off + h[1]] = c.toBf16(std.math.nan(f32)); + self.logits[2 * stride + r_off + h[2]] = c.toBf16(std.math.inf(f32)); + for (h) |id| self.logits[3 * stride + r_off + id] = c.toBf16(-std.math.inf(f32)); + self.logits[4 * stride + r_off + h[0]] = c.toBf16(-std.math.inf(f32)); // one -inf held: finite row + } + self.sp = try self.rig.input(std.mem.sliceAsBytes(self.logits)); + self.sl = try self.rig.input(std.mem.sliceAsBytes(&self.slots)); + self.picks = try self.rig.output(shape.rows * top * 4); + self.weights = try self.rig.output(shape.rows * top * 4); + return true; + } + + pub fn run(self: *Router) !void { + var a: cuda.Args = .{}; + a.add(self.sp); + a.add(@as(c_int, stride)); + a.add(@as(c_int, r_off)); + a.add(self.picks); + a.add(self.weights); + a.add(self.sl); + try cuda.launch.launch(self.f, .{ .grid = .{ .x = @intCast(self.shape.rows) }, .block = .{ .x = experts } }, self.rig.s, &a); + try self.rig.s.synchronize(); + } + + pub fn count(self: *Router) usize { + return 2 * self.shape.rows * top; + } + + pub fn reference(_: *Router, want: []f64, mag: []f64) !void { + @memset(want, 0); + @memset(mag, 0); + } + + /// Row t's eight picks in FP64: held ids by logit, ties to the lower original id; the weights, NaN if poisoned. + fn fp64(self: *const Router, t: usize, ids: *[top]usize, weights: *[top]f64) bool { + var order: [experts]usize = undefined; + const h = order[0..self.shape.n]; + for (h, self.held[0..self.shape.n]) |*o, id| o.* = id; + var max = -std.math.inf(f64); + var poisoned = false; + for (h) |id| { + const v = self.logit(t, id); + poisoned = poisoned or std.math.isNan(v) or v == std.math.inf(f64); + max = @max(max, v); + } + poisoned = poisoned or max == -std.math.inf(f64); + const Cmp = struct { + fn lt(_: *const Router, a: usize, b: usize) bool { + return a < b; // poisoned: every held prob reads 0, so the lowest ids win + } + fn before(r: struct { *const Router, usize }, a: usize, b: usize) bool { + const la = r[0].logit(r[1], a); + const lb = r[0].logit(r[1], b); + return la > lb or (la == lb and a < b); + } + }; + if (poisoned) std.sort.pdq(usize, h, self, Cmp.lt) else std.sort.pdq(usize, h, .{ self, t }, Cmp.before); + var sum: f64 = 0; + for (h[0..top]) |id| sum += @exp(self.logit(t, id) - max); + for (h[0..top], ids, weights) |id, *i, *w| { + i.* = id; + w.* = if (poisoned) std.math.nan(f64) else @exp(self.logit(t, id) - max) / sum; + } + return poisoned; + } + + pub fn check(self: *Router, at: g.Gate) !?[]const u8 { + const n = self.shape.rows * top; + const got_p = try self.rig.gpu.gpa.alloc(i32, n); + defer self.rig.gpu.gpa.free(got_p); + @memcpy(std.mem.sliceAsBytes(got_p), try self.rig.bytes(self.picks, n * 4)); + const got_w: []align(1) const f32 = std.mem.bytesAsSlice(f32, try self.rig.bytes(self.weights, n * 4)); + var poisoned: usize = 0; + for (0..self.shape.rows) |t| { + var ids: [top]usize = undefined; + var weights: [top]f64 = undefined; + poisoned += @intFromBool(self.fp64(t, &ids, &weights)); + for (0..top) |k| { + const p = got_p[t * top + k]; + const w: f64 = got_w[t * top + k]; + const slot = self.slots[ids[k]]; + const same = if (std.math.isNan(weights[k])) std.math.isNan(w) else @abs(w - weights[k]) <= 1e-6; + if (p != slot or !same) return try std.fmt.bufPrint(&self.msg, "row {d} pick {d}: slot {d} weight {e}, FP64 slot {d} (id {d}) weight {e}", .{ t, k, p, w, slot, ids[k], weights[k] }); + } + } + if (at == .nonfinite and poisoned != 3) return try std.fmt.bufPrint(&self.msg, "{d} poisoned rows, not the 3 planted: no proof", .{poisoned}); + return null; + } +}; diff --git a/zig/tests/cuda/contract_tests.zig b/zig/tests/cuda/contract_tests.zig index 15137feeafe..8948e76b71b 100644 --- a/zig/tests/cuda/contract_tests.zig +++ b/zig/tests/cuda/contract_tests.zig @@ -315,11 +315,8 @@ fn registered(gpu: check.Gpu, only: []const []const u8, t0: *i96) !g.Tally { var h3_topk = h3_topk_k.TopK{ .rig = .{ .gpu = gpu, .s = s }, .f = try h3_mod.function("h3_topk") }; defer h3_topk.deinit(); try kernels.append(a, h3_topk.kernel()); - var d3_mod = try cuda.Module.load(gpu.d, kk.drama3); - defer d3_mod.unload(); - var d3_norm = try drama3_k.Norm.init(gpu, s, d3_mod); - defer d3_norm.deinit(); - try kernels.append(a, d3_norm.kernel()); + const d3 = try drama3_k.Group.create(a, gpu, s, &kernels); + defer d3.destroy(); var ck: ?cuda.ck.Ck = null; var ck_reduce: ?cuda.ck.Ck = null; // no clusters: the FP8 slices meet in tf_ck8_reduce if (cap >= 120) { From 0d1eb9d89690cf49c5efcde6002712183fce686f Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Sat, 21 Nov 2026 07:15:25 +0000 Subject: [PATCH 2/6] changelog: #4144 under Unreleased --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 101b88f4aad..190d3bf2eb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel ## Unreleased +- CUDA: Drama 3 loads compact NVFP4 checkpoints as stored, stages held experts in slot order, and masks routing to their original ids (#4144). Thanks to @plotarmordev. + - Docs: the README's build section says that a Linux `zig build native` compiles for the build machine's CPU, and that `-Dcpu=baseline` or `-Dcpu=x86_64_v3` builds a binary for another machine, with thanks to @Nipale-ai (#3179; #3301). - Spark 2.5 on CUDA: gate and up load as one stacked weight and decode in one launch on every CUDA chip instead of two launches, with the bits unchanged; only 4-bit checkpoints take this path (#3130). From f95364e98cf078f9b24aff243f5248594b555b2a Mon Sep 17 00:00:00 2001 From: Jay Leaton <58495321+jayleaton@users.noreply.github.com> Date: Wed, 18 Nov 2026 15:09:33 +0700 Subject: [PATCH 3/6] server: a required or named tool call closes the think block in time for the call tool_choice required and named functions now set a thinking budget of max_tokens minus max(256, max_tokens/4), capped by any requested budget, so a think block that never ends cannot use the whole reply. Requests without tools and tool_choice auto are unchanged. --- zig/src/server/chat.zig | 1 + zig/src/server/tool_required_test.zig | 62 +++++++++++++++++++++++++++ zig/src/server/tool_think.zig | 12 ++++++ 3 files changed, 75 insertions(+) create mode 100644 zig/src/server/tool_think.zig diff --git a/zig/src/server/chat.zig b/zig/src/server/chat.zig index fa84ccdc9dd..6f05dc530e5 100644 --- a/zig/src/server/chat.zig +++ b/zig/src/server/chat.zig @@ -224,6 +224,7 @@ pub fn prepare(srv: *Server, cx: *Cx, input: Input, gone: anytype) Failure!Prepa request.think_close = srv.think_close; request.think_end = srv.think_close_end; } + @import("tool_think.zig").apply(&request, srv.info.capability(.thinking_budget) and (fields_mod.flag(f, "tool_call_required") orelse false), srv.think_close, srv.think_close_end); // The answer quota is separate only on engines that enforce it. if (srv.info.capability(.answer_limit)) @import("thinking_reserve.zig").apply(&request, srv.config.thinking_reserve, if (window > 0) window - n else std.math.maxInt(u32), srv.think_close_end); } diff --git a/zig/src/server/tool_required_test.zig b/zig/src/server/tool_required_test.zig index cb4c88c327b..45db0aa5f04 100644 --- a/zig/src/server/tool_required_test.zig +++ b/zig/src/server/tool_required_test.zig @@ -533,3 +533,65 @@ test "auto tools: a strict tool gets the optional-call grammar, other auto reque try std.testing.expectEqualStrings("sequence", (try value(g, required.structure.?.text)).get("format").?.strField("type").?); }; } + +fn thinkRun(want: []const u32, r: api.Request, seed: u64) !struct { tokens: []u32, stop: bool } { + var seq: lanes.grammar.Sequence = .{ .want = want, .stop = 96, .gpa = a }; + defer seq.log.deinit(a); + var constraint: lanes.grammar.Constraint = .init(seq.rules(), 4, 98); + var cfg = try lanes.Config.init(a, .{ .exact_width = 8, .mtp = true, .speculate = true, .drafts = 0, .hidden_rows = true, .streams_exact = true, .batch_rows = 8, .max_streams = 2, .draft_streams = true }, 8, 7); + defer cfg.deinit(a); + var fake: lanes.fake.Fake = .{ .gpa = a }; + defer fake.deinit(); + var clock: lanes.fake.FixedClock = .{}; + var engine = lanes.Engine.init(a, &cfg, fake.backend(), clock.clock()); + defer engine.deinit(); + var s = try lanes.Stream.init(a, .{ .id = "think", .prompt = &.{ 3, 1, 4, 1 }, .max_new = r.max_tokens, .sampling = .{ .seed = seed, .temperature = 1 }, .drafts = false, .grammar = &constraint, .think_budget = r.think_budget, .think_close = r.think_close, .think_end = if (r.think_end) |e| e else -1, .think_open = true }); + defer s.deinit(a); + try engine.addStream(&s); + while (engine.activeCount() > 0) try engine.step(); + return .{ .tokens = try a.dupe(u32, s.emitted()), .stop = s.reason == .stop }; +} + +test "required tools: with thinking on, a required or named call closes a think block that never ends and leaves room for the call" { + var arena = std.heap.ArenaAllocator.init(a); + defer arena.deinit(); + const g = arena.allocator(); + var stub: Stub = .{}; + var compile: Compile = .{}; + var compiler = compile.compiler(); + const close = [_]u32{ 97, 98 }; + for ([_]bool{ false, true }) |response| for ([_][]const u8{ "\"required\"", "named", "\"auto\"" }) |kind| { + var text: Text = .{}; + const srv = try Server.init(a, std.testing.io, stub.engine(), text.text(), .{ .served_name = "m", .model_ids = &.{"m"} }, null); + defer srv.deinit(); + srv.info.structures = @import("qwen27_masks").supported; + srv.grammars = &compiler; + var cx: errors.Cx = .{ .a = g }; + const named = std.mem.eql(u8, kind, "named"); + const choice = if (!named) kind else if (response) "{\"type\":\"function\",\"name\":\"now\"}" else "{\"type\":\"function\",\"function\":{\"name\":\"now\"}}"; + const input = try openai.chatInput(srv, &cx, try body(&cx, response, choice)); + const required = if (input.fields.get("tool_call_required")) |v| v.truthy() else false; + var request: api.Request = .{ .prompt = &.{1}, .max_tokens = 300 }; + try srv.checkFeatures(&cx, input.fields, true, false, &.{1}, input.tools, &request); + @import("tool_think.zig").apply(&request, required, &close, 98); + try std.testing.expectEqual(!std.mem.eql(u8, kind, "\"auto\""), required); + if (!required) { + try std.testing.expect(request.think_budget == 0 and request.think_end == null); + continue; + } + try std.testing.expectEqual(@as(u32, 300 - 256), request.think_budget); + const want = try Text.encode(undefined, g, try @import("tool_required_witness.zig").text(g, try value(g, request.structure.?.text)), false); + for ([_]u64{ 1235, 1236 }) |seed| { + const before = try thinkRun(want, .{ .prompt = request.prompt, .max_tokens = request.max_tokens }, seed); + defer a.free(before.tokens); + try std.testing.expect(!before.stop and before.tokens.len == 300); + const ran = try thinkRun(want, request, seed); + defer a.free(ran.tokens); + const end = std.mem.indexOfScalar(u32, ran.tokens, 98).?; + try std.testing.expect(ran.stop and end == 300 - 256); + const result = try parse.parse(g, try Text.decode(undefined, g, ran.tokens[end + 1 ..]), input.tools, 1); + try std.testing.expectEqual(@as(usize, 1), result.calls.?.len); + try std.testing.expectEqualStrings(if (named) "now" else "lookup", result.calls.?[0].get("function").?.strField("name").?); + } + }; +} diff --git a/zig/src/server/tool_think.zig b/zig/src/server/tool_think.zig new file mode 100644 index 00000000000..7275c1d1256 --- /dev/null +++ b/zig/src/server/tool_think.zig @@ -0,0 +1,12 @@ +//! A required or named call closes the think block early enough to leave room for the call. +const std = @import("std"); +const api = @import("engine_api"); + +pub fn apply(request: *api.Request, required: bool, think_close: []const u32, think_end: ?u32) void { + if (!required or think_close.len == 0) return; + const room = @max(256, request.max_tokens / 4); + const cap = request.max_tokens -| room; + request.think_budget = @max(1, if (request.think_budget > 0) @min(request.think_budget, cap) else cap); + request.think_close = think_close; + request.think_end = think_end; +} From 929a658fe54e33ea7aa550011c5a6267573d0224 Mon Sep 17 00:00:00 2001 From: Jay Leaton <58495321+jayleaton@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:05:03 +0700 Subject: [PATCH 4/6] server: the required-tool thinking test goes through chat.prepare The fixture called tool_think.apply itself, so it still passed with the call in chat.prepare removed. It now turns thinking on, gives the fake server its close markers and the thinking_budget capability, and builds the request with chat.prepare, so the wiring is what the lane and reply assertions check. Without the call the named case fails (expected 44, found 0). --- zig/src/server/tool_required_test.zig | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/zig/src/server/tool_required_test.zig b/zig/src/server/tool_required_test.zig index 45db0aa5f04..e39d420525a 100644 --- a/zig/src/server/tool_required_test.zig +++ b/zig/src/server/tool_required_test.zig @@ -562,18 +562,21 @@ test "required tools: with thinking on, a required or named call closes a think const close = [_]u32{ 97, 98 }; for ([_]bool{ false, true }) |response| for ([_][]const u8{ "\"required\"", "named", "\"auto\"" }) |kind| { var text: Text = .{}; - const srv = try Server.init(a, std.testing.io, stub.engine(), text.text(), .{ .served_name = "m", .model_ids = &.{"m"} }, null); + const srv = try Server.init(a, std.testing.io, stub.engine(), text.text(), .{ .served_name = "m", .model_ids = &.{"m"}, .default_max_tokens = 300, .enable_thinking = true, .use_drafts = false }, null); defer srv.deinit(); srv.info.structures = @import("qwen27_masks").supported; + srv.info.thinking_budget = true; + srv.think_close = &close; + srv.think_close_end = 98; srv.grammars = &compiler; var cx: errors.Cx = .{ .a = g }; const named = std.mem.eql(u8, kind, "named"); const choice = if (!named) kind else if (response) "{\"type\":\"function\",\"name\":\"now\"}" else "{\"type\":\"function\",\"function\":{\"name\":\"now\"}}"; const input = try openai.chatInput(srv, &cx, try body(&cx, response, choice)); const required = if (input.fields.get("tool_call_required")) |v| v.truthy() else false; - var request: api.Request = .{ .prompt = &.{1}, .max_tokens = 300 }; - try srv.checkFeatures(&cx, input.fields, true, false, &.{1}, input.tools, &request); - @import("tool_think.zig").apply(&request, required, &close, 98); + const prepared = try @import("chat.zig").prepare(srv, &cx, input, @import("rerank_test.zig").Present{}); + defer srv.release(prepared.preparing, prepared.request); + const request = prepared.request; try std.testing.expectEqual(!std.mem.eql(u8, kind, "\"auto\""), required); if (!required) { try std.testing.expect(request.think_budget == 0 and request.think_end == null); From 6059da73aa325f6332c52a308cafae8134e5a0ee Mon Sep 17 00:00:00 2001 From: Jay Leaton <58495321+jayleaton@users.noreply.github.com> Date: Sat, 21 Nov 2026 06:08:20 +0700 Subject: [PATCH 5/6] server: the required-tool think close sits behind --tool-think-close, off by default Closing the think block early changes the reply of a required or named call whose thinking would have ended between the cap and max_tokens, including calls that already succeed. The close is now opt-in through --tool-think-close, as --loop-guard-stop is; without it every request is prepared as before. The thinking tests move to tool_think_test.zig. With the flag, a think block that never ends closes at the cap and the call follows. Without it, a call whose thinking ends past the cap (at 200 of 300 tokens) keeps main's request and emits the same tokens. The docs, run guide and changelog name the flag. --- CHANGELOG.md | 1 + docs/api.md | 4 + docs/run-guide.md | 1 + zig/src/server/chat.zig | 2 +- zig/src/server/cli.zig | 6 +- zig/src/server/main.zig | 2 +- zig/src/server/root.zig | 1 + zig/src/server/serve.zig | 1 + zig/src/server/server.zig | 2 +- zig/src/server/tool_required_test.zig | 85 ++-------------- zig/src/server/tool_think_test.zig | 137 ++++++++++++++++++++++++++ 11 files changed, 161 insertions(+), 81 deletions(-) create mode 100644 zig/src/server/tool_think_test.zig diff --git a/CHANGELOG.md b/CHANGELOG.md index 190d3bf2eb0..a0b5d9f03ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,7 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel - GLM-5.3-Flash on CUDA: `TF_GLM_DRAFT_SPLIT=1` splits the DFlash2 drafter's MLP across three ranks or more where each share is 256-aligned, without `TF_ROCE=1`; the ranks must set it alike, two ranks split as before, and unset changes nothing. On three GB10s whose gathers stay on NCCL, decode with `--drafter` is about 5% faster at one stream (four streams within their start-to-start spread), with the same replies and the same tokens a round on the measured prompts. As with `TF_ROCE=1`, the split drafter adds its down projection's rank shares in another order, so its proposals can differ at near-ties (#3616, for #3581). - Flash Next on HIP: on an integrated GPU that shares the host's pages (no firmware carve-out), the default kept-prompt cache takes no more than the memory the streams leave, at most 16 GiB and none when nothing is left; an explicit `--prompt-cache-gib` still sets it (#3645). +- `--tool-think-close` (opt-in): with thinking on, a request whose `tool_choice` is `"required"` or names a function closes its think block at `max_tokens - max(256, max_tokens / 4)` (or its own smaller `thinking_budget`), so the call the grammar requires fits instead of the reply ending as `length` with no call (#4157). Off, every reply is as before. - Tests: the Web loopback startup test sets its fixture ready before probing, so its health, model-list and relay checks no longer depend on warm-up timing (#3753, fixes #3752). Thanks to @AIFlowML. - Ling-3.0-tiny on Metal: a bf16 forward diagnostic runs all 24 layers of the published checkpoint on kernels that already exist, with no new GPU kernel source; `zig build test-ling-forward`, given `TF_LING_CHECKPOINT` and a Python with torch and safetensors (`TF_LING_PYTHON`), checks whole, chunked and resumed runs byte for byte and reports the distance from float64. Nothing is served yet (#3919, part of #1247). Thanks to @ThinkOffApp. diff --git a/docs/api.md b/docs/api.md index 8461ee5a65f..da53a624aa7 100644 --- a/docs/api.md +++ b/docs/api.md @@ -807,6 +807,10 @@ Qwen XML and JSON call templates keep their envelopes. Gemma 4 uses uses the canonical `{"name":"NAME","arguments":{...}}` form. The native grammar limits whitespace between calls; `parallel_tool_calls: false` allows exactly one. With thinking enabled, the constraint starts after the think-end token. A reply cut short by `max_tokens` can remain incomplete and ends as `length`. +With `--tool-think-close` (off by default), a required or named call on an engine that takes `thinking_budget` gets a +think budget of `max_tokens - max(256, max_tokens / 4)`, or the request's own `thinking_budget` when that is smaller, +so the think block closes with room left for the call instead of ending as `length`. It changes the reply of any such +call whose thinking would have run past that point. `"auto"`, `"none"` and requests without tools are unchanged. Drafting stays enabled and uses the same grammar masks as plain decoding. This requires the loaded backend's structured-output capability and `libtfgrammar`. Nemotron, Qwen3.5-2B, diff --git a/docs/run-guide.md b/docs/run-guide.md index f00ba6360b8..dcdadb6cc94 100644 --- a/docs/run-guide.md +++ b/docs/run-guide.md @@ -123,6 +123,7 @@ The binary's `capabilities --json` response lists its supported flags and platfo | `--thinking-budget N` | Limit reasoning tokens where the engine supports closing the reasoning block. | | `--thinking-reserve N` | With thinking on, let reasoning use up to N tokens beyond a request's `max_tokens`, which then limits the answer (lane engines). | | `--loop-guard` | Close reasoning that repeats itself verbatim (a short cycle, or a long passage) where the engine supports it. | +| `--tool-think-close` | With thinking on, a required or named tool call closes its think block at `max_tokens - max(256, max_tokens / 4)` (or a smaller request `thinking_budget`), so the call fits instead of ending as `length`. Off by default ([API](api.md)). | | `--loop-guard-stop` | As `--loop-guard`, but end the reply where the guard fires: the reasoning so far, no answer or tool calls, `finish_reason` `content_filter` and a `reasoning_loop` error ([API](api.md)). | | `--no-drafts` | Produce the plain reference through the same engine. | | `--drafter DRAFTER` | An external drafter, as a directory or a repo id in the Hugging Face cache, for models that take one (Gemma 4 31B's recipe names its drafter); `none` serves without one. Without the flag, Qwen3.8-27B pairs DFlash2 when the cache holds it (never downloaded, not on HIP): one line names it, and serve runs without it if it does not open. | diff --git a/zig/src/server/chat.zig b/zig/src/server/chat.zig index 6f05dc530e5..a5e86751da3 100644 --- a/zig/src/server/chat.zig +++ b/zig/src/server/chat.zig @@ -224,7 +224,7 @@ pub fn prepare(srv: *Server, cx: *Cx, input: Input, gone: anytype) Failure!Prepa request.think_close = srv.think_close; request.think_end = srv.think_close_end; } - @import("tool_think.zig").apply(&request, srv.info.capability(.thinking_budget) and (fields_mod.flag(f, "tool_call_required") orelse false), srv.think_close, srv.think_close_end); + @import("tool_think.zig").apply(&request, srv.config.tool_think_close and srv.info.capability(.thinking_budget) and (fields_mod.flag(f, "tool_call_required") orelse false), srv.think_close, srv.think_close_end); // The answer quota is separate only on engines that enforce it. if (srv.info.capability(.answer_limit)) @import("thinking_reserve.zig").apply(&request, srv.config.thinking_reserve, if (window > 0) window - n else std.math.maxInt(u32), srv.think_close_end); } diff --git a/zig/src/server/cli.zig b/zig/src/server/cli.zig index e025ad5ef5a..d5b6cf59ede 100644 --- a/zig/src/server/cli.zig +++ b/zig/src/server/cli.zig @@ -85,6 +85,7 @@ pub const flags = [_]Flag{ .{ .name = "--thinking-reserve", .native = true }, .{ .name = "--loop-guard", .kind = .store_true, .native = true }, .{ .name = "--loop-guard-stop", .kind = .store_true, .native = true }, + .{ .name = "--tool-think-close", .kind = .store_true, .native = true }, .{ .name = "--no-drafts", .kind = .store_true, .native = true }, .{ .name = "--keep-warm", .native = true }, .{ .name = "--silence-floor-dbfs", .native = true }, @@ -228,6 +229,7 @@ pub const Args = struct { thinking_reserve: i64 = 0, loop_guard: bool = false, loop_guard_stop: bool = false, // the reply ends where the loop guard fires (implies --loop-guard) + tool_think_close: bool = false, // a required or named tool call closes thinking in time for the call no_drafts: bool = false, keep_warm: i64 = 900, // seconds the idle keepalive runs after the last request ends (0: off) idle_unload: i64 = 0, // seconds of idle past the last request before the engine closes (0: never; the next request reloads) @@ -371,11 +373,9 @@ fn spillWith(u: *Usage, a: Allocator, out: Args) error{ Usage, OutOfMemory }!voi fn spills(args: Args) bool { return (args.spill_gib orelse 0) > 0; } - pub fn int(u: *Usage, a: Allocator, name: []const u8, v: []const u8) error{ Usage, OutOfMemory }!i64 { return std.fmt.parseInt(i64, std.mem.trim(u8, v, " "), 10) catch fail(u, a, "argument {s}: invalid int value: '{s}'", .{ name, v }); } - pub fn float(u: *Usage, a: Allocator, name: []const u8, v: []const u8) error{ Usage, OutOfMemory }!f64 { return @import("fields.zig").pyFloat(v) orelse fail(u, a, "argument {s}: invalid float value: '{s}'", .{ name, v }); } @@ -481,7 +481,7 @@ fn apply(a: Allocator, out: *Args, name: []const u8, value: ?[]const u8, u: *Usa } else if (is(name, "--decode-share")) { out.decode_share = try float(u, a, name, v); if (!(out.decode_share.? >= 0 and out.decode_share.? < 1)) return fail(u, a, "argument --decode-share: expected a share from 0 to under 1: '{s}'", .{v}); - } else if (is(name, "--prompt-seed")) out.prompt_seed = true else if (is(name, "--max-tokens")) out.max_tokens = try int(u, a, name, v) else if (is(name, "--temperature")) out.temperature = try float(u, a, name, v) else if (is(name, "--top-p")) out.top_p = try float(u, a, name, v) else if (is(name, "--top-k")) out.top_k = try int(u, a, name, v) else if (is(name, "--min-p")) out.min_p = try float(u, a, name, v) else if (is(name, "--instruct-sampling")) out.instruct_sampling = if (try @import("family_sampling.zig").pairsProblem(a, v)) |why| return fail(u, a, "argument {s}: {s}: '{s}'", .{ name, why, v }) else v else if (is(name, "--thinking")) out.thinking = true else if (is(name, "--no-thinking")) out.thinking = false else if (is(name, "--reasoning-effort")) out.reasoning_effort = v else if (is(name, "--thinking-budget")) out.thinking_budget = try int(u, a, name, v) else if (is(name, "--thinking-reserve")) out.thinking_reserve = try int(u, a, name, v) else if (is(name, "--loop-guard")) out.loop_guard = true else if (is(name, "--loop-guard-stop")) out.loop_guard_stop = true else if (is(name, "--drafter")) out.drafter = v else if (is(name, "--drafter-bits")) out.drafter_bits = @intCast(try int(u, a, name, v)) else if (is(name, "--record")) out.record = v else if (is(name, "--kv-cache")) out.kv_cache = v else if (is(name, "--prompt-precision")) out.prompt_precision = v else if (is(name, "--no-drafts")) out.no_drafts = true else if (is(name, "--keep-warm")) out.keep_warm = try int(u, a, name, v) else if (is(name, "--idle-unload")) out.idle_unload = try int(u, a, name, v) else if (is(name, "--compact-at")) { + } else if (is(name, "--prompt-seed")) out.prompt_seed = true else if (is(name, "--max-tokens")) out.max_tokens = try int(u, a, name, v) else if (is(name, "--temperature")) out.temperature = try float(u, a, name, v) else if (is(name, "--top-p")) out.top_p = try float(u, a, name, v) else if (is(name, "--top-k")) out.top_k = try int(u, a, name, v) else if (is(name, "--min-p")) out.min_p = try float(u, a, name, v) else if (is(name, "--instruct-sampling")) out.instruct_sampling = if (try @import("family_sampling.zig").pairsProblem(a, v)) |why| return fail(u, a, "argument {s}: {s}: '{s}'", .{ name, why, v }) else v else if (is(name, "--thinking")) out.thinking = true else if (is(name, "--no-thinking")) out.thinking = false else if (is(name, "--reasoning-effort")) out.reasoning_effort = v else if (is(name, "--thinking-budget")) out.thinking_budget = try int(u, a, name, v) else if (is(name, "--thinking-reserve")) out.thinking_reserve = try int(u, a, name, v) else if (is(name, "--loop-guard")) out.loop_guard = true else if (is(name, "--loop-guard-stop")) out.loop_guard_stop = true else if (is(name, "--tool-think-close")) out.tool_think_close = true else if (is(name, "--drafter")) out.drafter = v else if (is(name, "--drafter-bits")) out.drafter_bits = @intCast(try int(u, a, name, v)) else if (is(name, "--record")) out.record = v else if (is(name, "--kv-cache")) out.kv_cache = v else if (is(name, "--prompt-precision")) out.prompt_precision = v else if (is(name, "--no-drafts")) out.no_drafts = true else if (is(name, "--keep-warm")) out.keep_warm = try int(u, a, name, v) else if (is(name, "--idle-unload")) out.idle_unload = try int(u, a, name, v) else if (is(name, "--compact-at")) { out.compact_auto = std.mem.eql(u8, v, "auto"); // each occurrence replaces both fields, so the last one wins out.compact_fraction = null; if (!out.compact_auto) { diff --git a/zig/src/server/main.zig b/zig/src/server/main.zig index 9a3ccc2703d..4118d123b66 100644 --- a/zig/src/server/main.zig +++ b/zig/src/server/main.zig @@ -32,7 +32,7 @@ const usage_line = "usage: tensorfold serve [-h] [--config FILE.toml] [--host HO " [--recall-gib RECALL_GIB] [--recall-read-only] [--max-tokens MAX_TOKENS]" ++ " [--temperature TEMPERATURE] [--top-p TOP_P] [--top-k TOP_K] [--min-p MIN_P] [--prompt-seed]" ++ " [--thinking | --no-thinking] [--reasoning-effort {low,medium,high,xhigh}]" ++ - " [--thinking-budget THINKING_BUDGET] [--thinking-reserve THINKING_RESERVE] [--loop-guard] [--loop-guard-stop] [--no-drafts]" ++ + " [--thinking-budget THINKING_BUDGET] [--thinking-reserve THINKING_RESERVE] [--loop-guard] [--loop-guard-stop] [--tool-think-close] [--no-drafts]" ++ " [--drafter DRAFTER]" ++ " [--drafter-bits {0,4}] [--record RECORD] [--keep-warm SECONDS] [--idle-unload SECONDS]" ++ " [--silence-floor-dbfs DBFS] [--silence-seconds SECONDS]" ++ diff --git a/zig/src/server/root.zig b/zig/src/server/root.zig index 30d9a5ee76f..8dd4338820c 100644 --- a/zig/src/server/root.zig +++ b/zig/src/server/root.zig @@ -75,6 +75,7 @@ test { _ = @import("tool_grammar_lfm2.zig"); _ = @import("tool_grammar_lfm2_test.zig"); _ = @import("tool_required_test.zig"); + _ = @import("tool_think_test.zig"); _ = @import("structure_field_test.zig"); _ = @import("grammar.zig"); _ = @import("grammar_compile_test.zig"); diff --git a/zig/src/server/serve.zig b/zig/src/server/serve.zig index b3e37c27ca6..54f39dd8c9b 100644 --- a/zig/src/server/serve.zig +++ b/zig/src/server/serve.zig @@ -201,6 +201,7 @@ pub fn run(gpa: Allocator, io: std.Io, args: cli.Args, s: Setup) u8 { .thinking_reserve = args.thinking_reserve, .loop_guard = args.loop_guard or args.loop_guard_stop, .loop_stop = args.loop_guard_stop, + .tool_think_close = args.tool_think_close, .keep_warm_s = args.keep_warm, .idle_unload_s = args.idle_unload, .admission = args.admission, diff --git a/zig/src/server/server.zig b/zig/src/server/server.zig index 2a3009396f0..af51d2b9bd3 100644 --- a/zig/src/server/server.zig +++ b/zig/src/server/server.zig @@ -58,6 +58,7 @@ pub const Config = struct { loop_guard: bool = false, /// --loop-guard-stop: a guarded reply ends where the guard fires, with a loop error and no answer or calls. loop_stop: bool = false, + tool_think_close: bool = false, // --tool-think-close: a required or named call closes thinking in time /// Seconds the idle keepalive runs after the last request ends (0: off). keep_warm_s: i64 = 900, /// Seconds of idle past the last request before the engine closes; the next request reloads it (0: never). @@ -124,7 +125,6 @@ pub fn namedBackground(config: Config, f: Value) bool { for (config.background_ids) |id| if (std.mem.eql(u8, id, asked)) return true; return false; } - /// Name the compiled structure with the client's own field when its route carried one (a borrowed static literal). pub fn nameStructure(format: ?[]const u8, request: *api.Request) void { const name = format orelse return; diff --git a/zig/src/server/tool_required_test.zig b/zig/src/server/tool_required_test.zig index e39d420525a..50ea199d6cd 100644 --- a/zig/src/server/tool_required_test.zig +++ b/zig/src/server/tool_required_test.zig @@ -13,17 +13,17 @@ const parse = @import("tool_parse.zig"); const a = std.testing.allocator; const specs = "[{\"type\":\"function\",\"function\":{\"name\":\"lookup\",\"parameters\":{\"type\":\"object\",\"properties\":{\"q\":{\"type\":\"string\"}},\"required\":[\"q\"]}}},{\"type\":\"function\",\"function\":{\"name\":\"now\",\"parameters\":{\"type\":\"object\",\"properties\":{}}}}]"; -const Text = struct { +pub const Text = struct { form: enum { xml, json, gemma, lfm2, unknown } = .json, - fn text(t: *Text) mt.Text { + pub fn text(t: *Text) mt.Text { return .{ .ctx = t, .vtable = &.{ .encode = encode, .decode = decode, .token_id = tokenId, .token_string = tokenString, .vocab_size = vocab, .eos_ids = eos, .render = render, .template_source = source } }; } - fn encode(_: *anyopaque, g: std.mem.Allocator, s: []const u8, _: bool) mt.Error![]u32 { + pub fn encode(_: *anyopaque, g: std.mem.Allocator, s: []const u8, _: bool) mt.Error![]u32 { const ids = try g.alloc(u32, s.len); for (s, ids) |ch, *id| id.* = if (ch == '\n') 95 else @as(u32, ch) - 32; return ids; } - fn decode(_: *anyopaque, g: std.mem.Allocator, ids: []const u32) std.mem.Allocator.Error![]u8 { + pub fn decode(_: *anyopaque, g: std.mem.Allocator, ids: []const u32) std.mem.Allocator.Error![]u8 { var out: std.ArrayList(u8) = .empty; for (ids) |id| if (id < 95) try out.append(g, @intCast(id + 32)) else if (id == 95) try out.append(g, '\n'); return out.toOwnedSlice(g); @@ -54,8 +54,8 @@ const Text = struct { })) }; } }; -const Stub = struct { - fn engine(s: *Stub) api.Engine { +pub const Stub = struct { + pub fn engine(s: *Stub) api.Engine { return .{ .ctx = s, .vtable = &.{ .info = info, .submit = submit, .cancel = cancel, .status = status, .memory = memory } }; } fn info(_: *anyopaque) api.Info { @@ -74,8 +74,8 @@ const Stub = struct { }; // The compiler boundary records the production grammar; no xgrammar library or GPU is loaded in this fixture. -const Compile = struct { - fn compiler(c: *Compile) api.grammar.Compiler { +pub const Compile = struct { + pub fn compiler(c: *Compile) api.grammar.Compiler { var out: api.grammar.Compiler = undefined; out.h = c; out.words = 4; @@ -90,7 +90,7 @@ const Compile = struct { } fn free(_: *anyopaque) callconv(.c) void {} }; -fn value(g: std.mem.Allocator, s: []const u8) !json.Value { +pub fn value(g: std.mem.Allocator, s: []const u8) !json.Value { return (try json.parse(g, s)).ok; } @@ -162,7 +162,7 @@ test "required tools: an LFM2 template gets the Pythonic grammar, one call or se }; } -fn body(cx: *errors.Cx, response: bool, choice: []const u8) !json.Value { +pub fn body(cx: *errors.Cx, response: bool, choice: []const u8) !json.Value { const History = struct { pub fn conversation(_: @This(), c: *errors.Cx, _: json.Value) errors.Refused![]json.Value { return c.refuse("this fixture has no stored response"); @@ -533,68 +533,3 @@ test "auto tools: a strict tool gets the optional-call grammar, other auto reque try std.testing.expectEqualStrings("sequence", (try value(g, required.structure.?.text)).get("format").?.strField("type").?); }; } - -fn thinkRun(want: []const u32, r: api.Request, seed: u64) !struct { tokens: []u32, stop: bool } { - var seq: lanes.grammar.Sequence = .{ .want = want, .stop = 96, .gpa = a }; - defer seq.log.deinit(a); - var constraint: lanes.grammar.Constraint = .init(seq.rules(), 4, 98); - var cfg = try lanes.Config.init(a, .{ .exact_width = 8, .mtp = true, .speculate = true, .drafts = 0, .hidden_rows = true, .streams_exact = true, .batch_rows = 8, .max_streams = 2, .draft_streams = true }, 8, 7); - defer cfg.deinit(a); - var fake: lanes.fake.Fake = .{ .gpa = a }; - defer fake.deinit(); - var clock: lanes.fake.FixedClock = .{}; - var engine = lanes.Engine.init(a, &cfg, fake.backend(), clock.clock()); - defer engine.deinit(); - var s = try lanes.Stream.init(a, .{ .id = "think", .prompt = &.{ 3, 1, 4, 1 }, .max_new = r.max_tokens, .sampling = .{ .seed = seed, .temperature = 1 }, .drafts = false, .grammar = &constraint, .think_budget = r.think_budget, .think_close = r.think_close, .think_end = if (r.think_end) |e| e else -1, .think_open = true }); - defer s.deinit(a); - try engine.addStream(&s); - while (engine.activeCount() > 0) try engine.step(); - return .{ .tokens = try a.dupe(u32, s.emitted()), .stop = s.reason == .stop }; -} - -test "required tools: with thinking on, a required or named call closes a think block that never ends and leaves room for the call" { - var arena = std.heap.ArenaAllocator.init(a); - defer arena.deinit(); - const g = arena.allocator(); - var stub: Stub = .{}; - var compile: Compile = .{}; - var compiler = compile.compiler(); - const close = [_]u32{ 97, 98 }; - for ([_]bool{ false, true }) |response| for ([_][]const u8{ "\"required\"", "named", "\"auto\"" }) |kind| { - var text: Text = .{}; - const srv = try Server.init(a, std.testing.io, stub.engine(), text.text(), .{ .served_name = "m", .model_ids = &.{"m"}, .default_max_tokens = 300, .enable_thinking = true, .use_drafts = false }, null); - defer srv.deinit(); - srv.info.structures = @import("qwen27_masks").supported; - srv.info.thinking_budget = true; - srv.think_close = &close; - srv.think_close_end = 98; - srv.grammars = &compiler; - var cx: errors.Cx = .{ .a = g }; - const named = std.mem.eql(u8, kind, "named"); - const choice = if (!named) kind else if (response) "{\"type\":\"function\",\"name\":\"now\"}" else "{\"type\":\"function\",\"function\":{\"name\":\"now\"}}"; - const input = try openai.chatInput(srv, &cx, try body(&cx, response, choice)); - const required = if (input.fields.get("tool_call_required")) |v| v.truthy() else false; - const prepared = try @import("chat.zig").prepare(srv, &cx, input, @import("rerank_test.zig").Present{}); - defer srv.release(prepared.preparing, prepared.request); - const request = prepared.request; - try std.testing.expectEqual(!std.mem.eql(u8, kind, "\"auto\""), required); - if (!required) { - try std.testing.expect(request.think_budget == 0 and request.think_end == null); - continue; - } - try std.testing.expectEqual(@as(u32, 300 - 256), request.think_budget); - const want = try Text.encode(undefined, g, try @import("tool_required_witness.zig").text(g, try value(g, request.structure.?.text)), false); - for ([_]u64{ 1235, 1236 }) |seed| { - const before = try thinkRun(want, .{ .prompt = request.prompt, .max_tokens = request.max_tokens }, seed); - defer a.free(before.tokens); - try std.testing.expect(!before.stop and before.tokens.len == 300); - const ran = try thinkRun(want, request, seed); - defer a.free(ran.tokens); - const end = std.mem.indexOfScalar(u32, ran.tokens, 98).?; - try std.testing.expect(ran.stop and end == 300 - 256); - const result = try parse.parse(g, try Text.decode(undefined, g, ran.tokens[end + 1 ..]), input.tools, 1); - try std.testing.expectEqual(@as(usize, 1), result.calls.?.len); - try std.testing.expectEqualStrings(if (named) "now" else "lookup", result.calls.?[0].get("function").?.strField("name").?); - } - }; -} diff --git a/zig/src/server/tool_think_test.zig b/zig/src/server/tool_think_test.zig new file mode 100644 index 00000000000..f7715c7e857 --- /dev/null +++ b/zig/src/server/tool_think_test.zig @@ -0,0 +1,137 @@ +//! --tool-think-close: a required or named call closes its think block in time for the call; off, nothing changes. +const std = @import("std"); +const api = @import("engine_api"); +const lanes = @import("lanes"); +const errors = @import("errors.zig"); +const Server = @import("server.zig").Server; +const openai = @import("openai.zig"); +const parse = @import("tool_parse.zig"); +const fx = @import("tool_required_test.zig"); +const a = std.testing.allocator; +const close = [_]u32{ 97, 98 }; + +fn thinkRun(want: []const u32, r: api.Request, seed: u64) !struct { tokens: []u32, stop: bool } { + var seq: lanes.grammar.Sequence = .{ .want = want, .stop = 96, .gpa = a }; + defer seq.log.deinit(a); + var constraint: lanes.grammar.Constraint = .init(seq.rules(), 4, 98); + var cfg = try lanes.Config.init(a, .{ .exact_width = 8, .mtp = true, .speculate = true, .drafts = 0, .hidden_rows = true, .streams_exact = true, .batch_rows = 8, .max_streams = 2, .draft_streams = true }, 8, 7); + defer cfg.deinit(a); + var fake: lanes.fake.Fake = .{ .gpa = a }; + defer fake.deinit(); + var clock: lanes.fake.FixedClock = .{}; + var engine = lanes.Engine.init(a, &cfg, fake.backend(), clock.clock()); + defer engine.deinit(); + var s = try lanes.Stream.init(a, .{ .id = "think", .prompt = &.{ 3, 1, 4, 1 }, .max_new = r.max_tokens, .sampling = .{ .seed = seed, .temperature = 1 }, .drafts = false, .grammar = &constraint, .think_budget = r.think_budget, .think_close = r.think_close, .think_end = if (r.think_end) |e| e else -1, .think_open = true }); + defer s.deinit(a); + try engine.addStream(&s); + while (engine.activeCount() > 0) try engine.step(); + return .{ .tokens = try a.dupe(u32, s.emitted()), .stop = s.reason == .stop }; +} + +/// A thinking server with the close markers on the fake engine, --tool-think-close as given; the caller deinits it. +fn serve(text: *fx.Text, stub: *fx.Stub, compiler: *api.grammar.Compiler, on: bool) !*Server { + const srv = try Server.init(a, std.testing.io, stub.engine(), text.text(), .{ .served_name = "m", .model_ids = &.{"m"}, .default_max_tokens = 300, .enable_thinking = true, .use_drafts = false, .tool_think_close = on }, null); + srv.info.structures = @import("qwen27_masks").supported; + srv.info.thinking_budget = true; + srv.think_close = &close; + srv.think_close_end = 98; + srv.grammars = compiler; + return srv; +} + +fn choiceFor(response: bool, kind: []const u8) []const u8 { + if (!std.mem.eql(u8, kind, "named")) return kind; + return if (response) "{\"type\":\"function\",\"name\":\"now\"}" else "{\"type\":\"function\",\"function\":{\"name\":\"now\"}}"; +} + +test "required tools: with thinking on and --tool-think-close, a required or named call closes a think block that never ends and leaves room for the call" { + var arena = std.heap.ArenaAllocator.init(a); + defer arena.deinit(); + const g = arena.allocator(); + var stub: fx.Stub = .{}; + var compile: fx.Compile = .{}; + var compiler = compile.compiler(); + for ([_]bool{ false, true }) |response| for ([_][]const u8{ "\"required\"", "named", "\"auto\"" }) |kind| { + var text: fx.Text = .{}; + const srv = try serve(&text, &stub, &compiler, true); + defer srv.deinit(); + var cx: errors.Cx = .{ .a = g }; + const named = std.mem.eql(u8, kind, "named"); + const input = try openai.chatInput(srv, &cx, try fx.body(&cx, response, choiceFor(response, kind))); + const required = if (input.fields.get("tool_call_required")) |v| v.truthy() else false; + const prepared = try @import("chat.zig").prepare(srv, &cx, input, @import("rerank_test.zig").Present{}); + defer srv.release(prepared.preparing, prepared.request); + const request = prepared.request; + try std.testing.expectEqual(!std.mem.eql(u8, kind, "\"auto\""), required); + if (!required) { + try std.testing.expect(request.think_budget == 0 and request.think_end == null); + continue; + } + try std.testing.expectEqual(@as(u32, 300 - 256), request.think_budget); + const want = try fx.Text.encode(undefined, g, try @import("tool_required_witness.zig").text(g, try fx.value(g, request.structure.?.text)), false); + for ([_]u64{ 1235, 1236 }) |seed| { + const before = try thinkRun(want, .{ .prompt = request.prompt, .max_tokens = request.max_tokens }, seed); + defer a.free(before.tokens); + try std.testing.expect(!before.stop and before.tokens.len == 300); + const ran = try thinkRun(want, request, seed); + defer a.free(ran.tokens); + const end = std.mem.indexOfScalar(u32, ran.tokens, 98).?; + try std.testing.expect(ran.stop and end == 300 - 256); + const result = try parse.parse(g, try fx.Text.decode(undefined, g, ran.tokens[end + 1 ..]), input.tools, 1); + try std.testing.expectEqual(@as(usize, 1), result.calls.?.len); + try std.testing.expectEqualStrings(if (named) "now" else "lookup", result.calls.?[0].get("function").?.strField("name").?); + } + }; +} + +test "required tools: without --tool-think-close, a call whose thinking ends past the cap and before max_tokens is emitted as before" { + var arena = std.heap.ArenaAllocator.init(a); + defer arena.deinit(); + const g = arena.allocator(); + var stub: fx.Stub = .{}; + var compile: fx.Compile = .{}; + var compiler = compile.compiler(); + // thinking that ends at 200 of 300 tokens, past the flag's cap of 44, then calls (a request budget stands in) + for ([_]bool{ false, true }) |response| for ([_][]const u8{ "\"required\"", "named" }) |kind| for ([_]?i64{ null, 200 }) |own| for ([_]bool{ false, true }) |on| { + var text: fx.Text = .{}; + const srv = try serve(&text, &stub, &compiler, on); + defer srv.deinit(); + var cx: errors.Cx = .{ .a = g }; + var input = try openai.chatInput(srv, &cx, try fx.body(&cx, response, choiceFor(response, kind))); + if (own) |n| try input.fields.object.put(g, "thinking_budget", .{ .int = try std.fmt.allocPrint(g, "{d}", .{n}) }); + const prepared = try @import("chat.zig").prepare(srv, &cx, input, @import("rerank_test.zig").Present{}); + defer srv.release(prepared.preparing, prepared.request); + const request = prepared.request; + // main's request: the client's budget alone, and a close only when the client asked for one + const was: api.Request = .{ .prompt = request.prompt, .max_tokens = request.max_tokens, .think_budget = if (own) |n| @intCast(n) else 0, .think_close = if (own != null) &close else &.{}, .think_end = if (own != null) 98 else null }; + if (on) { + try std.testing.expectEqual(@as(u32, 300 - 256), request.think_budget); + continue; + } + try std.testing.expectEqual(was.think_budget, request.think_budget); + try std.testing.expectEqual(was.think_end, request.think_end); + try std.testing.expectEqualSlices(u32, was.think_close, request.think_close); + const want = try fx.Text.encode(undefined, g, try @import("tool_required_witness.zig").text(g, try fx.value(g, request.structure.?.text)), false); + for ([_]u64{ 1235, 1236 }) |seed| { + const before = try thinkRun(want, was, seed); + defer a.free(before.tokens); + const ran = try thinkRun(want, request, seed); + defer a.free(ran.tokens); + try std.testing.expectEqualSlices(u32, before.tokens, ran.tokens); + if (own == null) continue; + const end = std.mem.indexOfScalar(u32, ran.tokens, 98).?; + try std.testing.expect(ran.stop and end == 200); + const result = try parse.parse(g, try fx.Text.decode(undefined, g, ran.tokens[end + 1 ..]), input.tools, 1); + try std.testing.expectEqual(@as(usize, 1), result.calls.?.len); + } + }; +} + +test "required tools: --tool-think-close parses as a native serve flag, off by default" { + var arena: std.heap.ArenaAllocator = .init(a); + defer arena.deinit(); + var usage: @import("cli.zig").Usage = .{}; + try std.testing.expect((try @import("cli.zig").parse(arena.allocator(), &.{ "model", "--tool-think-close" }, &usage)).tool_think_close); + try std.testing.expect(!(try @import("cli.zig").parse(arena.allocator(), &.{"model"}, &usage)).tool_think_close); + try std.testing.expect(!(@import("server.zig").Config{ .served_name = "m", .model_ids = &.{"m"} }).tool_think_close); +} From 1f22433afc660dd57e47ee36a1053b47087d3f0c Mon Sep 17 00:00:00 2001 From: ashhart <71890694+ashhart@users.noreply.github.com> Date: Sat, 21 Nov 2026 07:15:27 +0000 Subject: [PATCH 6/6] changelog: #4157's line from lines/4157.txt in place of its own --- CHANGELOG.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0b5d9f03ec..bcb34280c9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel ## Unreleased +- Server: opt-in --tool-think-close bounds thinking for required or named tool calls so the call has room in max_tokens; the flag defaults off and keeps smaller thinking budgets (#4157). Thanks to @jayleaton. + - CUDA: Drama 3 loads compact NVFP4 checkpoints as stored, stages held experts in slot order, and masks routing to their original ids (#4144). Thanks to @plotarmordev. - Docs: the README's build section says that a Linux `zig build native` compiles for the build machine's CPU, and that `-Dcpu=baseline` or `-Dcpu=x86_64_v3` builds a binary for another machine, with thanks to @Nipale-ai (#3179; #3301). @@ -18,10 +20,6 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel - CI: the Zig format and TensorFold Web workflows cancel a pull request's older run when it gets a newer push, so superseded runs no longer queue; main pushes all run (#3850). - - - - - Server: chat and legacy completions requests take `deadline_s` (seconds, more than 0 and at most 1,000,000; any other value is refused before the engine): at that time the server cancels the engine itself and the reply ends with `finish_reason: "deadline"`, keeping every token the engine gave and its usage; a reply the engine ended first, or a cancel that came first, keeps its own ending, and without the field nothing changes (#3438). Thanks to @jayleaton. - Tests: the CUDA kernel contract's NVFP4_NZ5 stress fill holds +5s among its 4s and 6s, its float64 reference reads code 0b1000 as 5, and a second planted control goes red at the sums gate, so that gate covers the +5 code; no kernel changes (#3543, refs #2894). Thanks to @Bizuayeu. @@ -48,7 +46,6 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel - GLM-5.3-Flash on CUDA: `TF_GLM_DRAFT_SPLIT=1` splits the DFlash2 drafter's MLP across three ranks or more where each share is 256-aligned, without `TF_ROCE=1`; the ranks must set it alike, two ranks split as before, and unset changes nothing. On three GB10s whose gathers stay on NCCL, decode with `--drafter` is about 5% faster at one stream (four streams within their start-to-start spread), with the same replies and the same tokens a round on the measured prompts. As with `TF_ROCE=1`, the split drafter adds its down projection's rank shares in another order, so its proposals can differ at near-ties (#3616, for #3581). - Flash Next on HIP: on an integrated GPU that shares the host's pages (no firmware carve-out), the default kept-prompt cache takes no more than the memory the streams leave, at most 16 GiB and none when nothing is left; an explicit `--prompt-cache-gib` still sets it (#3645). -- `--tool-think-close` (opt-in): with thinking on, a request whose `tool_choice` is `"required"` or names a function closes its think block at `max_tokens - max(256, max_tokens / 4)` (or its own smaller `thinking_budget`), so the call the grammar requires fits instead of the reply ending as `length` with no call (#4157). Off, every reply is as before. - Tests: the Web loopback startup test sets its fixture ready before probing, so its health, model-list and relay checks no longer depend on warm-up timing (#3753, fixes #3752). Thanks to @AIFlowML. - Ling-3.0-tiny on Metal: a bf16 forward diagnostic runs all 24 layers of the published checkpoint on kernels that already exist, with no new GPU kernel source; `zig build test-ling-forward`, given `TF_LING_CHECKPOINT` and a Python with torch and safetensors (`TF_LING_PYTHON`), checks whole, chunked and resumed runs byte for byte and reports the distance from float64. Nothing is served yet (#3919, part of #1247). Thanks to @ThinkOffApp.