model.action === 'load' ? onStart(model.repo) : onDownload(model.repo)} disabled={starting || downloading || locked}>{starting ? tx("Starting model…") : downloading ? pull?.state === 'queued' ? tx("Queued") : pull?.state === 'verifying' ? tx("Checking files…") : tx("Downloading…") : model.action === 'load' ? minimal ? tx("Start server") : tx("Start chatting") : tx("Download model")}
diff --git a/app/src/components/models/ModelRows.tsx b/app/src/components/models/ModelRows.tsx
index 2971bb7c21..d23c1dfb83 100644
--- a/app/src/components/models/ModelRows.tsx
+++ b/app/src/components/models/ModelRows.tsx
@@ -1,3 +1,4 @@
+import type { HubFailureCategory } from '../../../shared/hub-failure'
import { currentIntlLocale } from '../../lib/preferences/current-locale'
import { OriginalDiagnostic } from '../OriginalDiagnostic'
import { useAuthoredText } from '../../lib/preferences/useAuthoredText'
@@ -165,6 +166,7 @@ export function PullRow({ pull, onCancel, onRetry }: { pull: PullProgress; onCan
}
export interface PreflightState {
+ errorKind?: HubFailureCategory | null
loading: boolean
result: ModelPreflight | null
error: string | null
diff --git a/app/src/lib/hub-failure.test.ts b/app/src/lib/hub-failure.test.ts
new file mode 100644
index 0000000000..1ff1f5df07
--- /dev/null
+++ b/app/src/lib/hub-failure.test.ts
@@ -0,0 +1,28 @@
+import assert from 'node:assert/strict'
+import { aggregateHubFailures, decodeHubFailure, hubError, hubFailureCopy, type HubFailureCategory } from '../../shared/hub-failure'
+
+for (const category of ['policy', 'transport', 'offline', 'http', 'check'] as HubFailureCategory[]) {
+ const message = category === 'policy'
+ ? 'Hub host huggingface.co refused address 10.0.0.1. Use fake-ip-filter or HF_ENDPOINT.' : `${category} detail`
+ const mainError = hubError(category, message)
+ // Electron drops main-process class identity and custom properties, preserving only the message.
+ const serialized = JSON.parse(JSON.stringify({ message: `Error invoking remote method 'engine:preflight': Error: ${mainError.message}` }))
+ const failure = decodeHubFailure(serialized)
+ assert.equal(failure.category, category)
+ assert.equal(failure.message, message)
+ assert.equal(failure.coded, true)
+ assert.ok(hubFailureCopy(category).title)
+}
+assert.equal(decodeHubFailure(new Error('unexpected validation failure')).category, 'check')
+assert.equal(decodeHubFailure('[TF_HUB_OTHER] unknown code').category, 'check')
+assert.equal(aggregateHubFailures(['transport', 'http', 'policy']), 'policy')
+assert.equal(aggregateHubFailures(['check', 'offline']), 'offline')
+assert.equal(aggregateHubFailures(['http', 'transport']), 'transport')
+assert.equal(aggregateHubFailures([]), null)
+const policy = hubFailureCopy('policy')
+assert.equal(policy.title, 'Model checks blocked by network policy')
+assert.equal(policy.detail, 'The Hub address was refused. Check your DNS or proxy settings, then retry.')
+assert.doesNotMatch(`${policy.title} ${policy.detail}`, /reconnect|credentials/i)
+assert.equal(hubFailureCopy('transport').title, 'Could not reach the Hub')
+assert.equal(hubFailureCopy('offline').title, 'Model checks are offline')
+assert.notEqual(hubFailureCopy('http').title, hubFailureCopy('transport').title)
diff --git a/build.zig b/build.zig
index 8361b06746..7160cb7934 100644
--- a/build.zig
+++ b/build.zig
@@ -1,3 +1,4 @@
+const metal_build = @import("zig/build/metal_targets.zig");
const std = @import("std");
const glm_expiry_build = @import("zig/build/glm_expiry.zig");
const builtin = @import("builtin");
@@ -34,56 +35,18 @@ const kimi_kernels = metal_lists.kimi_kernels;
const nemotron_packed = metal_lists.nemotron_packed;
/// MLX's language version for mx.fast.metal_kernel on this macOS: 4.1 from 27, 4.0 on 26, 3.2 on 15, else 3.1.
-fn mlxMetalStd(b: *std.Build) []const u8 {
- const os = b.graph.host.result.os;
- const major = if (os.tag == .macos) os.version_range.semver.min.major else 0;
- if (major >= 27) return "metal4.1";
- if (major >= 26) return "metal4.0";
- if (major >= 15) return "metal3.2";
- return "metal3.1";
-}
+const mlxMetalStd = metal_build.mlxMetalStd;
/// xcrun metal -c each source to .air with `flags`, then link them into `name`.metallib.
-fn metallib(b: *std.Build, name: []const u8, sources: []const []const u8, flags: []const []const u8) std.Build.LazyPath {
- const link = b.addSystemCommand(&.{ "xcrun", "-sdk", "macosx", "metallib", "-o" });
- const out = link.addOutputFileArg(b.fmt("{s}.metallib", .{name}));
- for (sources) |src| {
- const cc = b.addSystemCommand(&.{ "xcrun", "-sdk", "macosx", "metal" });
- cc.addArgs(flags);
- cc.addArg("-c");
- cc.addFileArg(b.path(src));
- cc.addArg("-o");
- link.addFileArg(cc.addOutputFileArg(b.fmt("{s}.air", .{std.fs.path.stem(src)})));
- }
- return out;
-}
+const metallib = metal_build.metallib;
/// The Kimi metallib: each source compiled with kimi_dir on the include path, the shared header a dependency.
-fn kimiMetallib(b: *std.Build, flags: []const []const u8) std.Build.LazyPath {
- const link = b.addSystemCommand(&.{ "xcrun", "-sdk", "macosx", "metallib", "-o" });
- const out = link.addOutputFileArg("kimi.metallib");
- for (kimi_kernels) |name| {
- const cc = b.addSystemCommand(&.{ "xcrun", "-sdk", "macosx", "metal" });
- cc.addArgs(flags);
- cc.addPrefixedDirectoryArg("-I", b.path(kimi_dir));
- cc.addFileInput(b.path(kimi_dir ++ "/kimi_common.h"));
- cc.addArg("-c");
- cc.addFileArg(b.path(b.fmt("{s}/{s}", .{ kimi_dir, name })));
- cc.addArg("-o");
- link.addFileArg(cc.addOutputFileArg(b.fmt("kimi_{s}.air", .{std.fs.path.stem(name)})));
- }
- return out;
-}
+const kimiMetallib = metal_build.kimiMetallib;
/// A module whose `bytes` is the metallib at `lib`, embedded at compile time.
-fn embedded(b: *std.Build, lib: std.Build.LazyPath, file: []const u8) *std.Build.Module {
- const wf = b.addWriteFiles();
- _ = wf.addCopyFile(lib, file);
- const src = wf.add("metallib.zig", b.fmt("pub const bytes = @embedFile(\"{s}\");\n", .{file}));
- return b.createModule(.{ .root_source_file = src });
-}
+const embedded = metal_build.embedded;
-/// The page pool's own tests, and its checks under engine_api's paged store and lane host (kv_pool a module of its own).
+/// The page pool's own tests, and its checks under engine_api's paged store and lane host (kv_pool a module of its...
fn poolChecks(b: *std.Build, target: std.Build.ResolvedTarget, test_step: *std.Build.Step) void {
const pool = b.createModule(.{ .root_source_file = b.path("zig/src/core/kv_pool.zig"), .target = target, .optimize = .Debug });
const lanes = b.createModule(.{ .root_source_file = b.path("zig/src/core/lanes/lanes.zig"), .target = target, .optimize = .Debug, .link_libc = true });
@@ -96,6 +59,11 @@ fn poolChecks(b: *std.Build, target: std.Build.ResolvedTarget, test_step: *std.B
}
pub fn build(b: *std.Build) void {
+ @import("zig/build/lane_halt_host.zig").steps(b);
+ @import("zig/build/dsv41_shared.zig").steps(b);
+ @import("zig/build/dsv41_cuda.zig").steps(b);
+ @import("zig/build/dsv41_state.zig").steps(b);
+ @import("zig/build/dsv41_draft.zig").steps(b);
const target = b.standardTargetOptions(.{});
@import("zig/build/copy_source.zig").steps(b);
const release_version = b.option([]const u8, "version", "Release version, exactly MAJOR.MINOR.PATCH");
@@ -113,6 +81,7 @@ pub fn build(b: *std.Build) void {
_ = ids_files.addCopyFile(b.path("zig/src/families/nemotron/draft_ids.txt"), "draft_ids.txt");
const draft_ids = b.createModule(.{ .root_source_file = ids_files.add("draft_ids.zig", "pub const text = @embedFile(\"draft_ids.txt\");\n") });
const test_step = b.step("test", "Unit tests (macOS includes available Metal device checks)");
+ @import("zig/build/kernel_symbols.zig").tests(b, test_step);
if (b.graph.host.result.os.tag == .macos) {
const step5_mods = metalEngineModules(b, b.graph.host, .Debug, draft_ids);
const empty = b.addWriteFiles().add("step5-compile-only.metallib", "");
@@ -164,6 +133,10 @@ pub fn build(b: *std.Build) void {
@import("zig/build/qwen27_host.zig").tests(b);
const xgrammar = grammar_build.option(b);
dist_build.targets(b, draft_ids, build_options, release_version, xgrammar);
+ @import("zig/build/sessions.zig").add(b, test_step);
+ @import("zig/build/tp.zig").add(b, target, optimize, test_step);
+ @import("zig/build/gradient_host.zig").add(b);
+ @import("zig/build/living_tools_host.zig").steps(b);
cuda_build.hostTests(b, draft_ids, build_options, test_step, target.result.os.tag != .macos);
xpu_build.hostTests(b, test_step);
if (target.result.os.tag == .linux or target.result.os.tag == .windows) {
@@ -204,445 +177,19 @@ pub fn build(b: *std.Build) void {
contract_build.hostTests(b, gemma4_contract_host);
sys_build.wire(b);
@import("zig/build/checkpoint_config.zig").wire(b);
+ @import("zig/build/save_guard.zig").wire(b);
}
-/// `zig build native -Dcpu=apple_m1`: tensorfold-native with the Metal engines for the Python package's bundle (a native M5 build traps on M1-M4).
-fn nativeServer(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, metal: *std.Build.Module, engine: *std.Build.Module, api: *std.Build.Module, engines: *std.Build.Module, build_options: *std.Build.Step.Options, test_step: ?*std.Build.Step) *std.Build.Step.Compile {
- // the HTTP side keeps its safety checks; the engine below it runs at `optimize`
- const tokenizer = engines.import_table.get("tokenizer") orelse b.createModule(.{ .root_source_file = b.path("zig/src/core/tokenizer/tokenizer.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true });
- const template = b.createModule(.{ .root_source_file = b.path("zig/src/core/template/template.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true });
- const exe = b.addExecutable(.{ .name = "tensorfold-native", .root_module = b.createModule(.{
- .root_source_file = b.path("zig/src/server/main.zig"),
- .target = target,
- .optimize = .ReleaseSafe,
- .link_libc = true,
- .imports = &.{ .{ .name = "engine_api", .module = api }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "template", .module = template }, .{ .name = "native_engines", .module = engines }, .{ .name = "checkpoint_cli", .module = b.createModule(.{ .root_source_file = b.path("zig/src/cli/cli.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true, .imports = &.{ .{ .name = "native_engines", .module = engines }, exlr8_build.cliImport(b, target, .ReleaseSafe, engine), .{ .name = "core", .module = engine } } }) } },
- }) });
- exe.root_module.addOptions("build_options", build_options);
- _ = server_build.addTo(b, exe.root_module, target, .ReleaseSafe);
- if (test_step == null) {
- exe.root_module.strip = true;
- return exe;
- }
- const install = b.addInstallArtifact(exe, .{ .dest_dir = .{ .override = .{ .custom = "native/bin" } } });
- b.step("native", "tensorfold-native with the Metal engines into zig-out/native/bin (bundles: -Dcpu=apple_m1)").dependOn(&install.step);
- // libSystem's memcpy, not compiler-rt's: a quad-float or 128-bit helper links compiler-rt, whose weak memcpy then wins
- const libc_mem = b.addSystemCommand(&.{ "sh", "-c", "nm -m \"$0\" | grep -q 'external _memcpy (from libSystem)' || { echo 'tensorfold-native links its own memcpy: find the typed std.json int parse or 128-bit float conversion that pulled in compiler-rt'; exit 1; }" });
- libc_mem.addArtifactArg(exe);
- test_step.?.dependOn(&libc_mem.step);
- const server_tests = b.createModule(.{
- .root_source_file = b.path("zig/src/server/root.zig"),
- .target = target,
- .optimize = .Debug, // the server's unit tests run with safety checks, as zig_test.sh builds them
- .link_libc = true,
- .imports = &.{ .{ .name = "engine_api", .module = api }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "template", .module = template } },
- });
- // the required-tools tests' 27B mask policy and lane fake, on the engine API's own lanes module
- if (api.import_table.get("lanes")) |lanes_mod| {
- const q27 = @import("zig/build/qwen27_required.zig").modules(b, lanes_mod);
- server_tests.addImport("qwen27_mask_fake", q27.fake);
- server_tests.addImport("qwen27_masks", q27.masks);
- }
- const server_json = server_build.addTo(b, server_tests, target, .Debug);
- test_step.?.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests })).step);
- test_step.?.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_json })).step);
- @import("zig/build/cortheon_fuzz.zig").add(b, target, api, tokenizer, template);
- // the parse half's tests, as their own test binary (zig/src/server/tool_parse_test.zig)
- const tool_parse_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/src/server/tool_parse_test.zig"), .target = target, .optimize = .Debug, .link_libc = true, .imports = &.{ .{ .name = "engine_api", .module = api }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "template", .module = template } } }) });
- _ = server_build.addTo(b, tool_parse_tests.root_module, target, .Debug);
- test_step.?.dependOn(&b.addRunArtifact(tool_parse_tests).step);
- // the chat-template engine's tests (zig/src/core/template/tests.zig), which no other test binary reaches
- test_step.?.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/src/core/template/tests.zig"), .target = target, .optimize = .Debug, .link_libc = true }) })).step);
- // the engine seam's own tests (lane_host.zig), as zig/tests/server/zig_test.sh runs them
- test_step.?.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = api })).step);
- // the frozen server parity goldens, checked by Zig driving fake_serve
- golden_build.step(b, target, server_tests, api, tokenizer, template);
- b.step("test-native", "The Metal engines module's host-side tests").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = engines })).step);
- const reuse = b.addExecutable(.{ .name = "tf-flashnext-reuse", .root_module = b.createModule(.{
- .root_source_file = b.path("zig/tests/flashnext_reuse.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "tensorfold", .module = engine }, .{ .name = "engine_api", .module = api } },
- }) });
- b.installArtifact(reuse);
- b.step("tf-flashnext-reuse", "Flash Next prompt reuse against fresh prompt passes: replies at every depth and kept states' bytes").dependOn(&b.addInstallArtifact(reuse, .{}).step);
- const batch = b.addExecutable(.{ .name = "tf-flashnext-batch", .root_module = b.createModule(.{
- .root_source_file = b.path("zig/tests/flashnext_batch.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "tensorfold", .module = engine }, .{ .name = "kernel_sources", .module = engine.import_table.get("kernel_sources").? } },
- }) });
- b.step("tf-flashnext-batch", "Flash Next shared rounds against fresh legacy replies with prompt admission and cancellation").dependOn(&b.addInstallArtifact(batch, .{}).step);
- return exe;
-}
+/// The native Metal server build is owned by the shared Metal target module.
+const nativeServer = metal_build.nativeServer;
/// macOS: the metallib, the Metal runtime's test programs and the engine over Metal.
-fn metalTargets(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, draft_ids: *std.Build.Module, build_options: *std.Build.Step.Options, test_step: *std.Build.Step) void {
- const metal_std = b.option([]const u8, "metal-std", "Metal language version (default: MLX's for this macOS)") orelse mlxMetalStd(b);
-
- // MLX's mx.fast.metal_kernel JIT: MTLCompileOptions mathMode safe, fp32 functions fast (the default), its language version.
- const mlx_flags = [_][]const u8{ b.fmt("-std={s}", .{metal_std}), "-fmetal-math-mode=safe", "-fmetal-math-fp32-functions=fast" };
- const lib = metallib(b, "tensorfold", &kernels, &mlx_flags);
- const lib_install = b.addInstallFile(lib, "lib/tensorfold.metallib");
- b.step("metallib", "Compile zig/kernels/metal into tensorfold.metallib").dependOn(&lib_install.step);
-
- // Control: the same Nemotron kernels under fast math, which the bit-exact check must be able to tell apart.
- const fast_flags = [_][]const u8{ b.fmt("-std={s}", .{metal_std}), "-fmetal-math-mode=fast", "-fmetal-math-fp32-functions=fast" };
- const fast_lib = metallib(b, "nemotron_fastmath", generated, &fast_flags);
- b.getInstallStep().dependOn(&b.addInstallFile(fast_lib, "lib/nemotron_fastmath.metallib").step);
- b.getInstallStep().dependOn(&lib_install.step);
-
- const mods = metalEngineModules(b, target, optimize, draft_ids);
- const metal = mods.metal;
- const kernels_mod = embedded(b, lib, "tensorfold.metallib");
- counters_build.targets(b, target, optimize, metal, mods.sources, test_step);
-
- // The engines registry: the families this platform's binaries serve; the CLI reads it for models/info/pull.
- const api = b.createModule(.{ .root_source_file = b.path("zig/src/core/engine_api.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{.{ .name = "lanes", .module = mods.lanes }} });
- const engines = b.createModule(.{
- .root_source_file = b.path("zig/src/native/metal.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "engine_api", .module = api }, .{ .name = "tensorfold", .module = mods.engine } },
- });
- @import("zig/build/drama3_metal.zig").addNative(b, target, optimize, metal, engines, metal_std);
- @import("zig/build/drama3_metal.zig").targets(b, target, optimize, metal, metal_std, test_step);
-
- const programs = [_]struct { name: []const u8, path: []const u8, about: []const u8 }{
- .{ .name = "tf-metal-tests", .path = "zig/tests/gpu_tests.zig", .about = "GPU runtime tests" },
- .{ .name = "tf-dispatch-bench", .path = "zig/tests/dispatch_bench.zig", .about = "Dispatch boundary benchmark" },
- .{ .name = "tf-kernel-exact", .path = "zig/tests/kernel_exact.zig", .about = "Bit-exact check of a kernel against an MLX oracle case" },
- };
- for (programs) |p| {
- const mod = b.createModule(.{
- .root_source_file = b.path(p.path),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "metallib", .module = kernels_mod } },
- });
- const exe = b.addExecutable(.{ .name = p.name, .root_module = mod });
- b.installArtifact(exe);
- b.step(p.name, p.about).dependOn(&b.addInstallArtifact(exe, .{}).step);
- }
-
- // Kimi K3: the family module, its metallib, and its GPU check and benchmark programs.
- const kimi_lib = kimiMetallib(b, &mlx_flags);
- b.getInstallStep().dependOn(&b.addInstallFile(kimi_lib, "lib/kimi.metallib").step);
- const kimi = b.createModule(.{
- .root_source_file = b.path("zig/src/families/kimi_k3/kimi_k3.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{.{ .name = "metal", .module = metal }},
- });
- const kimi_bytes = embedded(b, kimi_lib, "kimi.metallib");
- const kimi_programs = [_]struct { name: []const u8, path: []const u8, about: []const u8 }{
- .{ .name = "tf-k3-check", .path = "zig/tests/kimi_k3/check.zig", .about = "Kimi K3 kernels and layers on synthetic weights at real shapes" },
- .{ .name = "tf-k3-bench", .path = "zig/tests/kimi_k3/bench.zig", .about = "Kimi K3 kernel bandwidth" },
- };
- for (kimi_programs) |p| {
- const mod = b.createModule(.{
- .root_source_file = b.path(p.path),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kimi_k3", .module = kimi }, .{ .name = "kimi_metallib", .module = kimi_bytes } },
- });
- const exe = b.addExecutable(.{ .name = p.name, .root_module = mod });
- b.installArtifact(exe);
- b.step(p.name, p.about).dependOn(&b.addInstallArtifact(exe, .{}).step);
- }
-
- // Kimi's tiktoken tokenizer and its parity program (tools/zig/k3_tokenizer_parity.py).
- const tiktoken = b.createModule(.{ .root_source_file = b.path("zig/src/core/tokenizer/tiktoken.zig"), .target = target, .optimize = optimize });
- const tok_mod = b.createModule(.{
- .root_source_file = b.path("zig/tests/kimi_k3/tokenizer.zig"),
- .target = target,
- .optimize = optimize,
- .imports = &.{.{ .name = "tiktoken", .module = tiktoken }},
- });
- const tok_exe = b.addExecutable(.{ .name = "tf-k3-tokenizer", .root_module = tok_mod });
- b.installArtifact(tok_exe);
- b.step("tf-k3-tokenizer", "Kimi K3's tiktoken tokenizer over parity cases").dependOn(&b.addInstallArtifact(tok_exe, .{}).step);
-
- // The cluster layer: inventory, membership, planner, loading and converged rounds over the lane core.
- const lanes = mods.lanes;
- const fabric = mods.fabric;
- const cluster = b.createModule(.{
- .root_source_file = b.path("zig/src/cluster/cluster.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "lanes", .module = lanes }, .{ .name = "fabric", .module = fabric } },
- });
- // The engine: kernel sources compiled at run time (MLX's options), the families, the lane core.
- const sources = mods.sources;
- // Nemotron's packed kernels, compiled offline and embedded: the runtime fallback when this macOS's
- // compiler refuses uint4b_format (#451).
- const packed_lib = metallib(b, "nemotron_packed", &nemotron_packed, &mlx_flags);
- b.getInstallStep().dependOn(&b.addInstallFile(packed_lib, "lib/nemotron_packed.metallib").step);
- sources.addImport("nemotron_packed_metallib", embedded(b, packed_lib, "nemotron_packed.metallib"));
- const engine = mods.engine;
- // FlashNext's CPU contracts, with the family module's imports so the replay graph type-checks
- const flash_host = b.addTest(.{ .root_module = b.createModule(.{
- .root_source_file = b.path("zig/flashnext_host.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kernel_sources", .module = sources }, .{ .name = "nemotron_draft_ids", .module = draft_ids }, .{ .name = "lanes", .module = lanes }, .{ .name = "fabric", .module = fabric } },
- }) });
- b.step("compile-flashnext-host", "Compile FlashNext CPU metadata contracts without running them").dependOn(&flash_host.step);
- const flash_host_run = b.addRunArtifact(flash_host);
- b.step("test-flashnext-host", "Run FlashNext CPU metadata contracts without Metal").dependOn(&flash_host_run.step);
- test_step.dependOn(&flash_host_run.step);
- // Pack builder cases that do not need the replay graph.
- const pack_cases = b.addTest(.{ .root_module = b.createModule(.{
- .root_source_file = b.path("zig/flashnext_pack_cases.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- }) });
- test_step.dependOn(&b.addRunArtifact(pack_cases).step);
- const load_admission_cases = b.addTest(.{ .root_module = b.createModule(.{
- .root_source_file = b.path("zig/flashnext_load_admission_cases.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- }) });
- const load_admission_run = b.addRunArtifact(load_admission_cases);
- b.step("test-flashnext-load-admission", "CPU-only Flash Next runtime quantization admission").dependOn(&load_admission_run.step);
- test_step.dependOn(&load_admission_run.step);
- const pack_cache_cases = b.addTest(.{ .root_module = b.createModule(.{
- .root_source_file = b.path("zig/flashnext_pack_cache_cases.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- }) });
- const pack_cache_run = b.addRunArtifact(pack_cache_cases);
- b.step("test-flashnext-pack-cache", "CPU-only atomic Flash Next pack cache lifecycle").dependOn(&pack_cache_run.step);
- test_step.dependOn(&pack_cache_run.step);
- const engine_programs = [_]struct { name: []const u8, path: []const u8, about: []const u8, c_source: ?[]const u8 = null }{
- .{ .name = "tf-qwen35-check", .path = "zig/tests/qwen35_check.zig", .about = "Qwen3.5-2B checkpoint and native operations" },
- .{ .name = "tf-qwen35-forward", .path = "zig/tests/qwen35_forward.zig", .about = "Qwen3.5-2B native teacher-forced logits" },
- .{ .name = "tf-qwen35-exact", .path = "zig/tests/qwen35_exact.zig", .about = "Qwen3.5-2B forward and committed-state exactness" },
- .{ .name = "tf-lfm2-forward", .path = "zig/tests/lfm2_forward.zig", .about = "LFM2-MoE native teacher-forced logits" },
- .{ .name = "tf-lfm2-bench", .path = "zig/tests/lfm2_bench.zig", .about = "LFM2-MoE prompt-chunk and one-row decode timing" },
- .{ .name = "tf-lfm2-exact", .path = "zig/tests/lfm2_exact.zig", .about = "LFM2-MoE prompt chunks, windows and committed-state exactness" },
- .{ .name = "tf-lfm2-fp64", .path = "zig/tests/lfm2_fp64.zig", .about = "LFM2 projection classes and attention on the tensor units, the tiles and the twin, each against float64" },
- .{ .name = "tensorfold", .path = "zig/src/main.zig", .about = "The native engine's command line" },
- .{ .name = "tf-nemotron-fixtures", .path = "zig/tests/nemotron_fixtures.zig", .about = "Nemotron kernels against the Python engine's captured ops" },
- .{ .name = "tf-nemotron-bench", .path = "zig/tests/nemotron_bench.zig", .about = "One-row projection kernels timed by tile count" },
- .{ .name = "tf-nemotron-wide", .path = "zig/tests/nemotron_wide.zig", .about = "Window costs by kernel class and expert overlap of drafted, sibling and stream rows" },
- .{ .name = "tf-nemotron-prefill-check", .path = "zig/tests/nemotron_prefill_check.zig", .about = "A prompt chunk layer by layer against prefill_dump.py's rows" },
- .{ .name = "tf-nemotron-dense", .path = "zig/tests/nemotron_dense.zig", .about = "Dense projection schedules bit-checked against the engine's kernels, then timed" },
- .{ .name = "tf-nemotron-experts", .path = "zig/tests/nemotron_experts.zig", .about = "Routed-expert pass shapes bit-checked against the engine's kernels, then timed by window width" },
- .{ .name = "tf-nemotron-sample-check", .path = "zig/tests/nemotron_sample_check.zig", .about = "tf_sample_full against its host reference on synthetic rows, timed beside tf_gpu_sample" },
- .{ .name = "tf-train-check", .path = "zig/tests/train_check.zig", .about = "The learning step's gradients against the engine's own forward by finite differences, on the real model" },
- .{ .name = "tf-train-pack", .path = "zig/tests/train_pack.zig", .about = "Training passes holding several sequences against one a pass, on the real model: differences and time" },
- .{ .name = "tf-pocket-fit-check", .path = "zig/tests/pocket_fit_check.zig", .about = "A dumped pocket lesson's read rows fitted as the learner fits them, timed, each fit's numbers and weights written" },
- .{ .name = "tf-flashnext-run", .path = "zig/tests/flashnext_run.zig", .about = "Flash Next one-row greedy steps on the Python engine's recorded kernels, against its tokens" },
- .{ .name = "tf-flashnext-pack", .path = "zig/tests/flashnext_pack.zig", .about = "Flash Next's three weight packs built from a checkpoint, byte-compared with a Python dump" },
- .{ .name = "tf-flashnext-think", .path = "zig/tests/flashnext_think.zig", .about = "Flash Next's verdict kernels under thinking budgets on a fake target, against the lane core's close" },
- .{ .name = "tf-grid-sync-bench", .path = "zig/tests/grid_sync_bench.zig", .about = "A GPU-wide barrier in one persistent dispatch against dependent relaunches" },
- .{ .name = "tf-weight-read-check", .path = "zig/tests/weight_read_check.zig", .about = "Check native file reads and failed-read cleanup", .c_source = "zig/tests/pread_fault.c" },
- .{ .name = "tf-glm-run", .path = "zig/tests/glm_run.zig", .about = "GLM-5.3-Flash greedy replies at each draft depth against depth 0 and reference tokens" },
- .{ .name = "tf-glm-vision", .path = "zig/tests/glm_vision.zig", .about = "GLM-5.3-Flash's image tower on an image or a reference dump's patches, compared with the dump" },
- .{ .name = "tf-glm-video", .path = "zig/tests/glm_video.zig", .about = "GLM-5.3-Flash's video preparation on a clip (libtfvideo), written for tools/zig/glm_video_fixtures.py" },
- .{ .name = "tf-qwen-vision", .path = "zig/tests/qwen_vision.zig", .about = "The Qwen-VL tower on picture files: patches, grids and merged rows written for a float32 reference" },
- .{ .name = "tf-qwen-image", .path = "zig/tests/qwen_image.zig", .about = "Qwen-Image-2.1's transformer on a reference case: each step compared, then the schedule timed" },
- .{ .name = "tf-glm-attn-probe", .path = "zig/tests/glm_attn_probe.zig", .about = "GLM-5.3-Flash's dense latent attention kernels on given inputs (scores, probabilities, outputs)" },
- .{ .name = "tf-moe-bench", .path = "zig/tests/moe_bench.zig", .about = "Routed-expert prompt kernels from .metal files on a skewed routing: chained speed, bits against the first" },
- .{ .name = "tf-mm-bench", .path = "zig/tests/mm_bench.zig", .about = "Prompt matmul kernels from .metal files: chained speed at one shape, bits against the first, error against fp64" },
- .{ .name = "tf-gemma4-kernels", .path = "zig/tests/gemma4/kernels.zig", .about = "Gemma 4 dense decode kernels against the reference's dumped fixtures, bit for bit by window width" },
- .{ .name = "tf-gemma4-glue", .path = "zig/tests/gemma4/glue.zig", .about = "Gemma 4 dense glue kernels against the reference's dumped fixtures: embed, norm, q/k/v, attention, tails, softcap" },
- .{ .name = "tf-gemma4-run", .path = "zig/tests/gemma4/run.zig", .about = "Gemma 4 dense greedy decode, tokens and top-two margins for the oracle's check" },
- .{ .name = "tf-gemma4-lanes", .path = "zig/tests/gemma4/lanes.zig", .about = "Gemma 4 dense on the lane core: plain, copy-drafted and shared-round streams against the oracle's tokens" },
- .{ .name = "tf-gemma4-vision", .path = "zig/tests/gemma4/vision.zig", .about = "Gemma 4's image tower against gemma4_vision_fixtures.py's dumps: patches bit for bit, each stage's error beside torch bf16's" },
- .{ .name = "tf-gemma4-bench", .path = "zig/tests/gemma4/bench.zig", .about = "Gemma 4 dense step costs: projections alone by rows against whole forwards" },
- .{ .name = "tf-qwen36-row-seam", .path = "zig/tests/qwen36_row_seam.zig", .about = "Model-free fixture: the row projection's bias sums stay fp32 at every width" },
- };
- for (engine_programs) |p| {
- const mod = b.createModule(.{
- .root_source_file = b.path(p.path),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "tensorfold", .module = engine }, .{ .name = "core", .module = engine }, .{ .name = "cluster", .module = cluster }, .{ .name = "kernel_sources", .module = sources }, .{ .name = "native_engines", .module = engines }, .{ .name = "engine_api", .module = api }, exlr8_build.cliImport(b, target, optimize, engine) },
- });
- if (p.c_source) |file| {
- mod.addCSourceFile(.{ .file = b.path(file), .flags = &.{"-std=c11"} });
- }
- const exe = b.addExecutable(.{ .name = p.name, .root_module = mod });
- b.installArtifact(exe);
- b.step(p.name, p.about).dependOn(&b.addInstallArtifact(exe, .{}).step);
- }
-
- glm_checks.sampler(b, target, optimize, metal, lanes, sources);
-
- // MiniMax H3 / FastH3's transformer: the packed rows' denoising forwards against a case file (M5 tensor units).
- const h3_kernels = b.createModule(.{ .root_source_file = b.path("zig/kernels/metal/h3/kernels.zig") });
- const h3_safetensors = b.createModule(.{ .root_source_file = b.path("zig/src/core/safetensors.zig"), .target = target, .optimize = optimize });
- const h3_mod = b.createModule(.{ .root_source_file = b.path("zig/src/families/h3/h3.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "safetensors", .module = h3_safetensors }, .{ .name = "h3_kernels", .module = h3_kernels }, .{ .name = "kernel_sources", .module = sources } } });
- const h3_dit = b.addExecutable(.{ .name = "tf-h3-dit", .root_module = b.createModule(.{
- .root_source_file = b.path("zig/tests/h3_dit.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "h3", .module = h3_mod } },
- }) });
- b.step("tf-h3-dit", "FastH3's Metal transformer against a case file: velocities, every step, timings").dependOn(&b.addInstallArtifact(h3_dit, .{}).step);
- // The core row projection (chips without tensor units) on synthetic matrices; runs on any Mac's GPU
- const row_mod = b.createModule(.{ .root_source_file = b.path("zig/src/core/row_projection.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kernel_sources", .module = sources } } });
- const row_check = b.addExecutable(.{ .name = "tf-row-check", .root_module = b.createModule(.{
- .root_source_file = b.path("zig/tests/row_projection_check.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "row_projection", .module = row_mod } },
- }) });
- b.step("tf-row-check", "The core row projection on synthetic 4-bit matrices: widths bit-equal, a CPU reference, indexed experts").dependOn(&b.addInstallArtifact(row_check, .{}).step);
- core_checks.rowWidths(b, target, optimize, metal, row_mod);
- test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = row_mod })).step);
- core_checks.tileQmm(b, target, optimize, metal, sources, test_step);
- exl3_build.targets(b, target, optimize, metal, test_step);
- exlr8_build.metalTargets(b, target, optimize, metal, mods.sources, test_step);
- exlr8_build.glmTargets(b, target, optimize, metal, mods.engine);
-
- // The checkpoint CLI: models, info and pull, against a synthetic cache and a fake hub, at ReleaseSafe as it ships.
- const cli_tests = b.addTest(.{ .root_module = b.createModule(.{
- .root_source_file = b.path("zig/src/cli/tests.zig"),
- .target = target,
- .optimize = .ReleaseSafe,
- .link_libc = true,
- .imports = &.{ .{ .name = "native_engines", .module = engines }, exlr8_build.cliImport(b, target, optimize, mods.engine), .{ .name = "core", .module = mods.engine } },
- }) });
- const run_cli_tests = b.addRunArtifact(cli_tests);
- test_step.dependOn(&run_cli_tests.step);
- b.step("test-cli", "The checkpoint CLI's host tests").dependOn(&run_cli_tests.step);
-
- // The core lane, shared-context and Qwen3.8-27B programs and host tests.
- qwen27_build.targets(b, target, optimize, metal, sources, engine, lanes, mods.qwen27, test_step);
- core_checks.targets(b, target, optimize, metal, engine);
- contract_build.metal(b, target, optimize, metal, engine, sources);
- glm_checks.targets(b, target, optimize, metal, engine, sources);
- flashnext_checks.targets(b, target, optimize, metal, engine);
-
- _ = nativeServer(b, target, optimize, metal, engine, api, engines, build_options, test_step);
-
- const cluster_metal = b.createModule(.{
- .root_source_file = b.path("zig/src/cluster/metal_sink.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "cluster", .module = cluster } },
- });
- const cluster_exe = b.addExecutable(.{ .name = "tf-cluster", .root_module = b.createModule(.{
- .root_source_file = b.path("zig/src/cluster/main.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{.{ .name = "cluster", .module = cluster }},
- }) });
- b.installArtifact(cluster_exe);
- b.step("tf-cluster", "tensorfold cluster / serve --cluster / node").dependOn(&b.addInstallArtifact(cluster_exe, .{}).step);
- // Kimi K3's checkpoint validator: the cluster's header reader cross-checked with the family's own specs.
- const k3_validate = b.addExecutable(.{ .name = "tf-k3-validate", .root_module = b.createModule(.{
- .root_source_file = b.path("zig/tests/kimi_k3/validate.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "kimi_k3", .module = kimi }, .{ .name = "cluster", .module = cluster } },
- }) });
- b.installArtifact(k3_validate);
- b.step("tf-k3-validate", "Kimi K3's checkpoint: index, headers, coverage and every tensor against the family").dependOn(&b.addInstallArtifact(k3_validate, .{}).step);
- const k3_cluster = b.addExecutable(.{ .name = "tf-k3-cluster", .root_module = b.createModule(.{
- .root_source_file = b.path("zig/tests/kimi_k3/cluster.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kimi_k3", .module = kimi }, .{ .name = "kimi_metallib", .module = kimi_bytes }, .{ .name = "cluster", .module = cluster } },
- }) });
- b.installArtifact(k3_cluster);
- b.step("tf-k3-cluster", "Kimi K3 tensor- and expert-parallel ranks against one node, through the cluster's canon").dependOn(&b.addInstallArtifact(k3_cluster, .{}).step);
- const tp2 = b.addExecutable(.{ .name = "tf-tp2-bench", .root_module = b.createModule(.{
- .root_source_file = b.path("zig/tests/tp2_bench.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "fabric", .module = fabric } },
- }) });
- b.step("tf-tp2-bench", "TP=2's exchange as the GPU sees it, between two Macs over MCDMA").dependOn(&b.addInstallArtifact(tp2, .{}).step);
- const cluster_tests = b.step("test-cluster", "Cluster tests: fake nodes and fabric, K3 planning (TF_K3_DIR), Metal sink");
- cluster_tests.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = cluster })).step);
- cluster_tests.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = cluster_metal })).step);
-
- // These module tests include Metal device checks when the macOS host has a GPU.
- test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = metal })).step);
- const helpers = b.createModule(.{
- .root_source_file = b.path("zig/tests/common.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "metallib", .module = kernels_mod } },
- });
- test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = helpers })).step);
- test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = engine })).step);
- test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = lanes })).step);
- // the fake fabric maps its windows with mmap: the Macs' RDMA, not Windows
- if (target.result.os.tag != .windows) test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = fabric })).step);
- const dashboard = b.createModule(.{ .root_source_file = b.path("zig/src/server/dashboard_test.zig"), .target = target, .optimize = optimize, .link_libc = true });
- test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = dashboard })).step);
- const kimi_test = b.step("test-k3", "Kimi K3's host-side unit tests (no GPU work)");
- for ([_]*std.Build.Module{ kimi, tiktoken }) |m| {
- const run = b.addRunArtifact(b.addTest(.{ .root_module = m }));
- kimi_test.dependOn(&run.step);
- test_step.dependOn(&run.step);
- }
-}
+const metalTargets = metal_build.metalTargets;
/// The server and distribution use the same embedded-source Metal engine.
-fn metalEngineModules(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, draft_ids: *std.Build.Module) struct { metal: *std.Build.Module, lanes: *std.Build.Module, fabric: *std.Build.Module, sources: *std.Build.Module, engine: *std.Build.Module, qwen27: qwen27_build.Shared } {
- const metal = b.createModule(.{
- .root_source_file = b.path("zig/src/metal/metal.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- });
- metal.linkFramework("Metal", .{});
- metal.linkFramework("Foundation", .{});
- metal.linkFramework("IOSurface", .{}); // the Neural Engine's prompt share passes IOSurfaces
- metal.linkSystemLibrary("objc", .{});
- const lanes = b.createModule(.{ .root_source_file = b.path("zig/src/core/lanes/lanes.zig"), .target = target, .optimize = optimize, .link_libc = true });
- const fabric = b.createModule(.{ .root_source_file = b.path("zig/src/fabric/fabric.zig"), .target = target, .optimize = optimize, .link_libc = true });
- const sources = b.createModule(.{ .root_source_file = b.path("zig/kernels/metal/sources.zig") });
- const engine = b.createModule(.{
- .root_source_file = b.path("zig/src/tensorfold.zig"),
- .target = target,
- .optimize = optimize,
- .link_libc = true,
- .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kernel_sources", .module = sources }, .{ .name = "nemotron_draft_ids", .module = draft_ids }, .{ .name = "lanes", .module = lanes }, .{ .name = "fabric", .module = fabric } },
- });
- return .{ .metal = metal, .lanes = lanes, .fabric = fabric, .sources = sources, .engine = engine, .qwen27 = qwen27_build.engineModules(b, target, metal, sources, engine) };
-}
+const metalEngineModules = metal_build.metalEngineModules;
-pub fn distMetalServer(b: *std.Build, target: std.Build.ResolvedTarget, draft_ids: *std.Build.Module, build_options: *std.Build.Step.Options, sdk: []const u8) *std.Build.Step.Compile {
- const m = metalEngineModules(b, target, .fast, draft_ids);
- // the release binary embeds the same prebuilt packed-kernel metallib as the development build
- const flags = [_][]const u8{ b.fmt("-std={s}", .{mlxMetalStd(b)}), "-fmetal-math-mode=safe", "-fmetal-math-fp32-functions=fast" };
- m.sources.addImport("nemotron_packed_metallib", embedded(b, metallib(b, "nemotron_packed", &nemotron_packed, &flags), "nemotron_packed.metallib"));
- // An explicit deployment target is a cross-build even on macOS. Supply the SDK search paths.
- m.metal.addSystemFrameworkPath(b.graph.cwdRelativePath(b.pathJoin(&.{ sdk, "System/Library/Frameworks" })));
- m.metal.addLibraryPath(b.graph.cwdRelativePath(b.pathJoin(&.{ sdk, "usr/lib" })));
- m.metal.addSystemIncludePath(b.graph.cwdRelativePath(b.pathJoin(&.{ sdk, "usr/include" })));
- const api = b.createModule(.{ .root_source_file = b.path("zig/src/core/engine_api.zig"), .target = target, .optimize = .fast, .link_libc = true, .imports = &.{.{ .name = "lanes", .module = m.lanes }} });
- const engines = b.createModule(.{ .root_source_file = b.path("zig/src/native/metal.zig"), .target = target, .optimize = .fast, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = m.metal }, .{ .name = "engine_api", .module = api }, .{ .name = "tensorfold", .module = m.engine } } });
- @import("zig/build/drama3_metal.zig").addNative(b, target, .fast, m.metal, engines, mlxMetalStd(b));
- return nativeServer(b, target, .fast, m.metal, m.engine, api, engines, build_options, null);
-}
+pub const distMetalServer = metal_build.distMetalServer;
test {
_ = dist_build;
diff --git a/build.zig.zon b/build.zig.zon
index dbcb0ed4d6..187350c97e 100644
--- a/build.zig.zon
+++ b/build.zig.zon
@@ -1,6 +1,6 @@
.{
.name = .tensorfold,
- .version = "1.0.12",
+ .version = "1.1.1",
.fingerprint = 0xb6dc5a482d53980a, // Changing this has security and trust implications.
.minimum_zig_version = "0.17.0",
.dependencies = .{},
diff --git a/docs/api.md b/docs/api.md
index 97dd8f7f9c..11793d2ef2 100644
--- a/docs/api.md
+++ b/docs/api.md
@@ -5,42 +5,59 @@ The base URL is `http://127.0.0.1:8080/v1` with the default server settings.
## Native chat template override
`tensorfold serve MODEL --chat-template custom-chat.jinja` loads a UTF-8 Jinja template from that file
-instead of the checkpoint's template files or `tokenizer_config.json` templates. Relative paths resolve
-from the server's working directory. The file is read once at startup, with the same 16 MiB limit as
-the checkpoint's `chat_template.jinja`.
+instead of the checkpoint's template files or `tokenizer_config.json` templates. Relative paths resolve from the server's working directory. The file is read once at startup, with the same 16 MiB limit as the checkpoint's `chat_template.jinja`.
-The override handles both ordinary and tool-bearing chat requests; it does not retain a checkpoint's
-separate `tool_use` template. The checkpoint's tokenizer, special tokens and EOS IDs stay unchanged.
-Unreadable or malformed templates fail startup before the engine opens, without falling back to the
-checkpoint template. An empty file leaves chat rendering unavailable, as a checkpoint with no template does.
-Raw completion prompts do not use the chat template.
+The override handles both ordinary and tool-bearing chat requests; it does not retain a checkpoint's separate `tool_use` template. The checkpoint's tokenizer, special tokens and EOS IDs stay unchanged. Unreadable or malformed templates fail startup before the engine opens, without falling back to the checkpoint template. An empty file leaves chat rendering unavailable, as a checkpoint with no template does. Raw completion prompts do not use the chat template.
## API keys
-Both servers accept repeated `--api-key KEY`, comma-separated `TENSORFOLD_API_KEY`, and `--api-key-file PATH`.
-The sources add keys together. A file has one key per line, optional `label: key` entries, and lines starting with
+Both servers accept repeated `--api-key KEY`, comma-separated `TENSORFOLD_API_KEY`, and `--api-key-file PATH`. The sources add keys together. A file has one key per line, optional `label: key` entries, and lines starting with
`#` for comments. Set its permissions to `0600`; startup refuses a file readable by other users.
The server stores SHA-256 digests and compares every configured digest for each authentication attempt.
-Send `Authorization: Bearer KEY` from OpenAI-compatible clients, or `x-api-key: KEY` from Messages clients.
-Missing or invalid credentials return HTTP 401 with `WWW-Authenticate: Bearer` and the route's error format.
-All `/v1/*`, tokenization, metrics and their inference aliases require a key; `--metrics-open` opens both metrics
-routes. `/health` remains open and returns only `{"status":"ok"}` when keys are configured.
-With no keys, the existing open routes and health details stay unchanged; a non-loopback bind prints a warning.
+Send `Authorization: Bearer KEY` from OpenAI-compatible clients, or `x-api-key: KEY` from Messages clients. Missing or invalid credentials return HTTP 401 with `WWW-Authenticate: Bearer` and the route's error format. With keys configured, the native server requires a key on every route by default. Its public exceptions are GET `/`, `/health`, `/slide` and the eight embedded `/slide/` scripts; `--metrics-open` also opens GET `/metrics` and `/v1/metrics`. Other methods and unknown assets require a key. This includes every `/systemone` inference alias and future private routes. `/health` remains open and returns only `{"status":"ok"}` when keys are configured. Public health is read-only, including when a reset query is supplied. With no keys, its health details remain available; a non-loopback bind prints a warning.
-The native server raises its soft open-file limit before listening when the platform permits it. If `accept`
-runs out of descriptors, it logs one guidance line per burst and keeps a short retry backoff.
+The native server raises its soft open-file limit before listening when the platform permits it. If `accept` runs out of descriptors, it logs one guidance line per burst and keeps a short retry backoff.
-Replace the key file atomically to rotate keys. Its modification time is checked at most once a second; SIGHUP
-requests an immediate reload. An empty, unreadable or malformed replacement closes authenticated routes until a
-valid file returns. Request logs and `tensorfold:requests_total` use labels, never keys or digests.
-Unnamed keys receive `cli-N`, `env-N` or `file-N` labels. Labels contain at most 64 letters, digits, dots,
-underscores or hyphens; choose non-secret labels.
+Replace the key file atomically to rotate keys. Its modification time is checked at most once a second; SIGHUP requests an immediate reload. An empty, unreadable or malformed replacement closes authenticated routes until a valid file returns. Request logs and `tensorfold:requests_total` use labels, never keys or digests. Unnamed keys receive `cli-N`, `env-N` or `file-N` labels. Labels contain at most 64 letters, digits, dots, underscores or hyphens; choose non-secret labels.
-In the Python 0.6.6 engine's [control room](control.md), `tensorfold service install MODEL --api-key-file PATH`
-forwards the file to its server, and `tensorfold tui --url URL --token-env VARIABLE` sends that variable's value as a
-bearer key. The native binary has neither command.
-Prefer a restricted file over command-line keys, which can appear in the operating system's process list.
+In the Python 0.6.6 engine's [control room](control.md), `tensorfold service install MODEL --api-key-file PATH` forwards the file to its server, and `tensorfold tui --url URL --token-env VARIABLE` sends that variable's value as a bearer key. The native binary has neither command. Prefer a restricted file over command-line keys, which can appear in the operating system's process list.
+
+
+## Browser request headers
+
+The server accepts origin-form targets beginning with `/`, with query text and trailing slashes interpreted by the
+shared route helper. Absolute URLs, fragments and control bytes are refused before routing. Routes are case-sensitive
+and percent escapes are not decoded into aliases. Malformed or folded header fields are refused and closed.
+
+On a loopback bind, the native server accepts a `Host` naming `localhost` or a loopback IP, with any valid port.
+It checks the actual bound socket, including a hostname that resolved to loopback. Missing, duplicate, malformed or
+foreign hosts receive HTTP 403 before authentication, `100 Continue` or routing. Repeat `--allowed-host NAME` to add
+trusted hostnames or bracketed IP literals. A name has no scheme, path or port; it admits that name at any port.
+The flag changes header admission, not the listening address. A non-loopback bind skips this host restriction.
+
+On every bind, an `Origin` must match the request's `Host` and the native server's HTTP scheme. Hostname case,
+IPv6 spellings and default ports are normalized. The origin must be a single `http://HOST[:PORT]` or
+`https://HOST[:PORT]` value with no path or userinfo. `null`, malformed values and duplicate origins are refused.
+Repeat `--allowed-origin ORIGIN` for trusted exceptions, including an HTTPS origin used through a TLS proxy.
+Host and origin exceptions are independent; allowing an origin does not admit a foreign loopback Host.
+Forwarded headers do not select the scheme or bypass either check.
+
+Each 403 names the refused header and its corresponding exception flag. Clients without an `Origin`, including curl,
+SDKs and the app, keep their existing requests with a valid Host. Content-Type is not required by these checks, so
+`curl -d` requests keep working. The server adds no CORS headers; these flags do not enable cross-origin browser reads.
+The header checks protect browser access and do not replace API keys. Trailing spaces or tabs in Host or Origin values
+are refused; clients should omit that outer whitespace.
+
+Host and Origin exception lists each accept at most 64 entries. DNS names use nonempty ASCII labels without a trailing
+dot; IPv6 literals are bracketed without zone identifiers. Every HTTP request needs exactly one valid Host, including
+HTTP/1.0; hostless HTTP/0.9 is refused and closed. An external bind skips only loopback-name admission.
+
+For a trusted proxy name and web origin, for example:
+
+```sh
+tensorfold-native serve MODEL --allowed-host inference.example --allowed-origin https://inference.example
+```
| Route | Behavior |
| --- | --- |
@@ -101,7 +118,7 @@ each route and its events.
| Route | What it does |
| --- | --- |
-| `POST /v1/slide/learn` | `{"text": "..."}`: learns the facts in the text, streaming `fact`, `learning` and `learned` events |
+| `POST /v1/slide/learn` | `{"text": "...", "questions": ["..."]}`: learns the facts in the text, streaming `fact`, `learning` and `learned` events; `questions` is optional (see Living Weights) |
| `GET /v1/slide/graph` | Every fact as JSON: its state, neurons, questions and their expected answers, and the model's pocket |
| `DELETE /v1/slide/graph` | Clears the graph; the weights keep what they learned |
| `DELETE /v1/slide/fact/{id}` | Deletes one fact from the weights, every other fact checked again, streaming `fact`, `checking`, `zeroed`, `checked` and `forgot` events; an optional `{"reason": "..."}` goes into the graph |
@@ -121,7 +138,7 @@ delete runs gets 503, an id that is not a number 400, and an id the graph does n
| `parallel_tool_calls` | False returns at most one completed call | Both |
| `max_tokens`, `max_completion_tokens` | Explicit reply limit; rejected if prompt plus reply exceeds the window | Both |
| `temperature`, `top_p`, `top_k`, `min_p` | Sampling overrides; zero temperature is greedy | Both |
-| `seed` | Sampling key; otherwise derived from the prompt (and `TENSORFOLD_SEED_SALT`) | Both |
+| `seed` | Explicit sampling key; native fallback uses configured defaults, then a fresh key (see below) | Both |
| `stream` | Server-sent events; the last event carries usage | Both |
| `stream_options.include_usage` | Usage in its own final event with `"choices": []`, not on the finish event | Both |
| `chat_template_kwargs.enable_thinking` | Template thinking toggle; `chat_template_kwargs.thinking` (`true`/`false` or `{"type": "enabled"}`/`{"type": "disabled"}`, as DeepSeek-V4 clients send it) is read the same way when `enable_thinking` is absent; other values are ignored | Both |
@@ -148,7 +165,8 @@ sequences. `/v1/completions` takes the legacy `logprobs`, an integer from 0 thro
the legacy form, streamed or not: `tokens`, `token_logprobs`, `top_logprobs` (each alternative's text to its logprob)
and `text_offset` (each token's first character in the completion, counting each token's own text). Other engines,
the Responses route and the request modes above return HTTP 400 when probabilities are requested.
-Nonstreamed `/v1/completions` also takes vLLM's `prompt_logprobs`, an integer from 0 through 20, on the same engine.
+Nonstreamed `/v1/completions` also takes vLLM's `prompt_logprobs`, an integer from 0 through 20, on the same engine
+and on GLM-5.3-Flash's CUDA engine.
`choices[0].prompt_logprobs` has one entry per prompt token: `null` for the first, then an object keyed by token id
(a string) whose first key is the prompt token itself, followed by the requested best tokens; each value has
`logprob`, `rank` and `decoded_token`. The prompt token's `rank` counts every vocabulary token at or above it,
@@ -276,7 +294,8 @@ Responses uses `{"type":"function","name":"lookup"}`. Only offered tools are all
call with no tools gets HTTP 400. `auto` remains optional, and `none` hides the tools.
The constrained answer contains one or more complete calls and no prose outside them. Arguments follow the offered
-parameter schema. Qwen XML and JSON call templates keep their envelopes. Gemma 4 uses
+parameter schema. GLM uses `
` and `` tags after the function name.
+Qwen XML and JSON call templates keep their envelopes. Gemma 4 uses
`<|tool_call>call:NAME{...}` with JSON arguments, which the tool parser accepts. An unrecognized template
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
@@ -284,15 +303,17 @@ starts after the think-end token. A reply cut short by `max_tokens` can remain i
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,
-Qwen3.8-27B (`qwen27`), Gemma 4 31B, Step-5 and LFM2 MoE on Metal have that capability; Qwen27 on Intel also has it.
+Qwen3.8-27B (`qwen27`), Gemma 4 31B, Step-5, LFM2 MoE and Flash Next shared sessions on Metal have that capability; Qwen27 on Intel also has it.
Qwen3.8-27B applies the grammar to its first draw, each verified row and each branch of its draft proposals.
Structured requests use its shared lane core, including when the ordinary lone-request serial optimization is enabled.
-CUDA determines support from its loaded backend's mask hook. Flash Next Metal (`flashnext-zig`) and GLM Metal
-(`glm-zig`, `glm-dsa-zig`) lack that hook and refuse required/named choices before generation with HTTP 400, for example:
-`backend 'flashnext-zig' cannot enforce tool_choice "required" or a named function: it has no token grammar support; send "auto" or "none"`.
+CUDA determines support from its loaded backend's mask hook. GLM Metal
+(`glm-zig`, `glm-dsa-zig`) lacks that hook and refuse required/named choices before generation with HTTP 400, for example:
+`backend 'glm-zig' cannot enforce tool_choice "required" or a named function: it has no token grammar support; send "auto" or "none"`.
The server does not silently turn a required call into `auto`. Installing the grammar library does not add a missing
backend mask hook. A missing library or grammar compilation error is also refused before generation with its reason.
+The native GLM CUDA backend also supports token grammars, gathering vocabulary rows before rank 0 masks and draws.
+
HTTP 400 comes before any token for a malformed field, a grammar xgrammar cannot compile (with xgrammar's reason), a
server without xgrammar (with the install command; on the native server, a server that cannot load `libtfgrammar`),
a grammar sent with `tool_choice: "required"` or a named function, and `logprobs` with a grammar or tools. A reply whose
@@ -419,10 +440,12 @@ For exactness comparisons, hold the checkpoint, template, runtime, prompt, seed
constant, then compare the decoded reply with `draft` enabled and disabled. Repeat with fresh and reused
prefixes, and compare each MLX concurrent request with its solo run.
-A request without `seed` takes one derived from its prompt, so running the same evaluation twice against one server
-repeats the same samples wherever the conversations agree (an agent benchmark's second pass then mostly replays its
-first). To draw independent repeats, send a `seed` per run, or start each run's server with a different
-`TENSORFOLD_SEED_SALT` (an integer mixed into every prompt-derived seed; 0, the default, keeps today's seeds).
+Native sampled requests use the request seed, then an injected configured default, then a fresh 63-bit OS key. No stock
+serve flag or generation config supplies a default seed. The done line appends `seed=` after
+its request timing and tok/s fields, for given and drawn keys. Send that seed again to reproduce a draw. Greedy requests draw
+no key. Native `serve --prompt-seed` keeps historical prompt keying, including `TENSORFOLD_SEED_SALT`; CLI runs and
+speech keep their existing prompt keying. Always give comparison requests the same explicit seed, including release
+cells, drafted/plain, cached/fresh and concurrent/solo checks. See [request seeds](request-seeds.md).
## Metrics
@@ -523,8 +546,8 @@ configured the body is only `{"status": "ok"}`. Without keys it has these fields
`live.connections` counts running and waiting requests, and `live.waiting` the waiting ones.
`decode_tokens_per_second` is the tokens decoded in the last 2 s, divided by 2. `prefill_tokens_per_second` is the
last prompt pass's uncached tokens over its time, shown for 2 s after the pass ends and 0 otherwise. Both rates are
-rounded to one decimal. `?reset_peak=1` starts a new CUDA peak at the current `active`; `/memory`'s `device.peak_bytes`
-is the same peak. With keys configured, `reset_peak` is ignored.
+rounded to one decimal. Public health never resets the peak, with or without keys. Reset queries on `/health` and `/`
+are ignored. The separate `/memory` route also reports memory without resetting it and requires a key when keys are configured.
The Python 0.6.6 server's body differs: its MLX `memory` can add `budget`, `mlx_budget` and `footprint`, and its CUDA
server sends the counters described under [Responses](#responses). For memory figures on a Mac, use
diff --git a/docs/community-benchmarks.md b/docs/community-benchmarks.md
index 87a242cdee..0e3c106b74 100644
--- a/docs/community-benchmarks.md
+++ b/docs/community-benchmarks.md
@@ -11,7 +11,7 @@ The examples call the command `tensorfold`. A source build's binary is `zig-out/
The command starts its own single-request `serve` on a loopback port, disables prompt reuse, warms each workload, and measures five repeats at sampled and greedy settings. It stops only the server it started, and a Ctrl-C or a closed terminal stops that server too; a second Ctrl-C exits at once and kills it. The default output limit is 256 tokens. The checkpoint must already be in the cache: `tensorfold benchmark` never downloads, so run `tensorfold pull` first. `--drafter DIR_OR_REPO` passes a drafter to engines that take one. Drafts stay on by default, and only `--serial` disables them. The default `--drafter auto` passes the target's own drafter when it is already cached (`z-lab/Qwen3.8-27B-DFlash2` for Qwen3.8-27B) and names it in the run output and the receipt; when it is not cached, the run passes none and says why. `--drafter none` passes none. When `--serial` overrides a given `--drafter`, the run prints a line saying so.
-An engine that decodes greedily only refuses the sampled cells, so it cannot complete community-v1; the run prints the server's reason, and `--temperatures 0` gives a local run. A large checkpoint may need its engine's own load limit raised (`GLM_LOAD_LIMIT_GB` for GLM-5.3-Flash) and a longer `--startup-timeout`: without the flag, the run waits up to 600 s plus 6 s per GB of weights past 20 GB, at most 3600 s.
+An engine that decodes greedily only refuses the sampled cells, so it cannot complete community-v2; the run prints the server's reason, and `--temperatures 0` gives a local run. A large checkpoint may need its engine's own load limit raised (`GLM_LOAD_LIMIT_GB` for GLM-5.3-Flash) and a longer `--startup-timeout`: without the flag, the run waits up to 600 s plus 6 s per GB of weights past 20 GB, at most 3600 s.
Results are saved locally in `~/.tensorfold/benchmarks/RUN_ID.json`; `--output FILE` chooses another location. Files contain public hardware and model metadata and individual attempts, never prompt text, generated output, machine names, serial numbers, GPU UUIDs, addresses or local model paths. Local model directories can be benchmarked but cannot be published, and a drafter directory is never named. Managed native runs record `python_version` as `"none"`. An attached manifest may attest the same value; an unattested attached run records `"unknown"`.
@@ -21,7 +21,7 @@ Results are saved locally in `~/.tensorfold/benchmarks/RUN_ID.json`; `--output F
# Inspect a saved receipt without uploading it.
tensorfold benchmark inspect receipt.json
-# Show the immutable community-v1 fixture and settings manifest.
+# Show the immutable community-v2 fixture and settings manifest.
tensorfold benchmark suites
# A short custom run, outside the standard five-repeat comparison.
@@ -35,7 +35,7 @@ tensorfold benchmark example-org/public-checkpoint --server http://127.0.0.1:808
Attached-server results do not claim the client's hardware is the server's hardware. The client reads `GET /v1/benchmark/manifest` from the server it attached to. A manifest that passes the receipt checks fills the version, hardware, checkpoint hashes, revision and rank count (1 or 2). Without that document, or if any field fails those checks, the receipt keeps unknown hardware and an unknown revision and remains unranked. An attached run records the backend, model and context you declare, unchecked against the server. The runner does not clear or restart an attached server's caches.
-The runner records failures, incomplete streams, missing usage, early EOS and cancellations. Exit code 0 means every measured attempt completed its requested output with a delivery interval; 1 means an error, and with `--publish` the receipt is still saved when the run finished; 2 means incomplete attempts remain in the saved receipt; 130 means the run was cancelled with Ctrl-C: an attempt in flight is kept in the receipt, and a cancel during server startup saves nothing. SIGTERM and SIGHUP exit 143 and 129 without a receipt. Errors never silently disappear from an average.
+The runner records failures, incomplete streams, missing usage, early EOS and cancellations. Exit code 0 means every measured attempt completed under its protocol with a delivery interval; 1 means an error; v2 `--publish` is refused before a run starts and creates no receipt; 2 means incomplete attempts remain in the saved receipt; 130 means the run was cancelled with Ctrl-C: an attempt in flight is kept in the receipt, and a cancel during server startup saves nothing. SIGTERM and SIGHUP exit 143 and 129 without a receipt. Errors never silently disappear from an average.
## What the rates mean
@@ -43,19 +43,35 @@ The runner records failures, incomplete streams, missing usage, early EOS and ca
**Server-reported tok/s** is kept separately when the server supplies it and is never substituted for stream delivery. First-text latency, request duration and server-reported prefill time remain separate measurements.
-The suite fixes prompts, seeds, sampled top-k and top-p, and thinking settings. Successful warm-ups are excluded; failed warm-ups are kept and make their cells incomplete. A cell has aggregates only when every declared repetition succeeds. Early EOS is visible and excluded from full-length comparisons.
+The suite fixes prompts, seeds, sampled top-k and top-p, and thinking settings. Successful warm-ups are excluded; failed warm-ups are kept and make their cells incomplete. A cell has aggregates only when every declared repetition succeeds. In community-v2, a sampled reply that naturally stops after at least 128 tokens is complete. Its `finish_reason` is recorded, and its rate uses the actual completion count, never the requested budget. Greedy replies still need the full requested count. Shorter replies, premature length stops and unmeasured delivery remain incomplete; empty output fails.
Receipts include token hashes supplied by the server, but the command does not run paired serial and drafted equality tests. A token hash alone is not an exactness proof.
+## Protocol versions
+
+New runs use `community-v2` and its manifest digest. Existing `community-v1` receipts keep their original
+full-output rule and remain readable; inspection never upgrades them. Every ranking must select exactly
+one suite ID and digest, including contributor speed standings and profiles. A receiver must validate the
+version's sample fields and completion rule before computing that version's aggregates. It must reject an
+unknown or mismatched digest, never relabel it or merge v1 and v2 medians. Deploy the receiver's v2 support
+before accepting v2 uploads; an older v1-only receiver will reject them. The receiver is maintained separately
+from this source tree. V2 submissions are disabled in this release, including `--publish`, saved-receipt
+publication and direct uploads. Local runs and inspection remain available. The lead enables v2 only
+once both the client and receiver have shipped support, including separate rankings.
+
+Thanks to @BlivionIaG and @easthambuilds for the sampled-stop reports and proposal.
+
## Publishing
-The browser upload flow needs no login, pasted token, email request or administrator approval. Install or update the CLI using the [runbook](../RUNBOOK.md), run a cached public model locally, and save its receipt:
+Community-v2 runs stay local until the receiver accepts v2 and separates its rankings. Both `publish` and a run with `--publish` refuse v2 before a run, credential lookup, public-checkpoint lookup or upload. Local run, save and inspect remain available.
+
+The browser upload flow remains available for eligible community-v1 receipts, without a login, pasted token, email request or administrator approval. Install or update the CLI using the [runbook](../RUNBOOK.md). New runs save v2 locally:
```sh
-tensorfold benchmark TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit --output benchmark.json
+tensorfold benchmark TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit --output benchmark-v2.json
```
-Open [the benchmark board](https://tensorfold.dev/benchmarks), choose **Upload result**, enter a public display name and select `benchmark.json`. Review the hardware, model and timing fields, then confirm the data to submit it. Receipt contents are not sent before confirmation. The website creates a scoped browser profile privately and remembers it across reloads using a Secure, HttpOnly, SameSite cookie; there is no upload key to copy or paste.
+For an existing eligible community-v1 receipt, open [the benchmark board](https://tensorfold.dev/benchmarks), choose **Upload result**, enter a public display name and select `legacy-v1-receipt.json`. Review the hardware, model and timing fields, then confirm the data to submit it. Receipt contents are not sent before confirmation. The website creates a scoped browser profile privately and remembers it across reloads using a Secure, HttpOnly, SameSite cookie; there is no upload key to copy or paste.
That browser profile keeps later submissions together, with up to 20 uploads per day. Clearing browser data or using another browser creates a separate profile. Display names are self-chosen and do not verify an identity; matching names do not merge profiles. New-profile limits apply to reduce spam.
@@ -67,14 +83,11 @@ Retries return the same saved submission without using another upload. Older man
### Existing CLI publishing credentials
-CLI installations that already have scoped upload credentials can continue to publish directly:
+CLI installations that already have scoped upload credentials can continue to publish eligible community-v1 receipts directly:
```sh
-# Run, show the public fields, and ask before publishing.
-tensorfold benchmark TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit --publish
-
-# Publish an existing receipt; --yes consents without the prompt.
-tensorfold benchmark publish receipt.json --yes
+# Publish an eligible legacy receipt; --yes consents without the prompt.
+tensorfold benchmark publish legacy-v1-receipt.json --yes
# Remove locally stored CLI upload credentials.
tensorfold benchmark logout
@@ -107,3 +120,24 @@ The parameter-versus-speed plot uses the selected parameter basis on a logarithm
## AMD (ROCm)
A ROCm build runs the same suite and saves its receipt locally, with backend `rocm` on platform `linux`. `--backend auto` picks `rocm` on that build; an attached run names it with `--backend rocm`. A managed ROCm run records the CPU and system memory, while GPU names and the driver version stay unknown. `inspect` shows a ROCm receipt as usual. The board accepts mlx and cuda receipts for now, so `--publish` with ROCm is refused before the run starts and `benchmark publish` refuses a ROCm receipt before any upload; publishing waits for the board to accept rocm.
+
+
+## Chat-only checkpoints in community-v2
+
+When a native family declares `raw_text = false`, the code workload sends its unchanged prompt text as one user
+message to `/v1/chat/completions`. Native runs use the matched family declaration from the admitted config, not a
+model-name guess. Families that accept raw text keep `/v1/completions` for code.
+
+For an attached server, declare this explicitly:
+
+```sh
+tensorfold benchmark owner/model --server http://127.0.0.1:8080 --backend cuda --chat-only
+```
+
+Use `--chat-only` only when the checkpoint has no raw-text mode. It applies to attached v2 runs. Without it, code
+keeps the completion route. Empty replies always fail; the client never infers a capability or retries through chat
+from an empty response. Each v2 sample records its API route, including failures and retained warmups. The code
+prompt, seeds, token budget and temperature are unchanged. The ordinary chat workload still uses the chat route.
+
+The v2 manifest digest includes this routing rule. V1's manifest, raw code fixture and receipt fields are unchanged.
+V2 publication remains disabled until the receiver accepts the updated protocol and separates its rankings.
diff --git a/docs/credits.md b/docs/credits.md
index bf56fb06cb..f67ff5f41f 100644
--- a/docs/credits.md
+++ b/docs/credits.md
@@ -226,3 +226,5 @@ credited here.
[#289](https://github.com/ashhart/TensorFold/pull/289)), @taussoe ([#66](https://github.com/ashhart/TensorFold/pull/66)),
@jasontitus ([#33](https://github.com/ashhart/TensorFold/pull/33)), @feni6
([#120](https://github.com/ashhart/TensorFold/pull/120)).
+
+- @tpischke requested fresh per-request sampling keys and reproducible done-line seeds in [#1026](https://github.com/ashhart/TensorFold/issues/1026).
diff --git a/docs/csa2-reference-cases.md b/docs/csa2-reference-cases.md
new file mode 100644
index 0000000000..05a5449b21
--- /dev/null
+++ b/docs/csa2-reference-cases.md
@@ -0,0 +1,41 @@
+# CSA2 sink and output-mix reference cases
+
+`zig/tests/deepseek_v41_reference/operator_csa2_fp64_sink.zig` compares the
+Metal sparse-attention output with the FP64 equation. The cases below preserve
+the test's fixed seeds, shapes and tolerances.
+
+## Sink normalization
+
+One live column fixes the running maximum at one score. Changing the sink
+therefore isolates the `sink - max` normalization term. The sweep includes
+underflow, balanced mass and overflow boundaries of FP32 exponentiation.
+Near overflow, a one-ULP change in the maximum can determine whether the
+denominator remains finite; the corresponding output can be near `1e-38`.
+The exact boundary depends on the generated score, not just the sink value.
+
+The per-head case assigns sink values `-36`, `-12`, `12` and `36`. These
+different masses check both head indexing and normalization arithmetic.
+
+## Online tile denominator
+
+The equal-score case spans ten tiles and 639 terms. Equal exponentials make
+the unsunk denominator equal to the number of terms. Ramped scores exercise
+the online correction as earlier mass falls toward FP32 underflow. Descending
+scores keep the running maximum fixed and remove that rescaling step. The
+single-tile case applies no inter-tile correction.
+
+## Output mix and cancellation
+
+Window and compressed rows share an accumulator but use different scale grids.
+The dominance cases reverse their relative magnitudes across six orders of
+magnitude; a third case balances their scales.
+
+The mirror cases use 128 window rows with a large shape whose sign alternates
+across columns and rows. A uniform query has zero dot product with that shape.
+The large contributions also cancel in the weighted sum, leaving a small shared
+perturbation. This exposes error that would be hidden by a large final result.
+Shape exponents are 8, 16 and 23; the shared perturbation is scaled by `2^-16`.
+Smaller perturbations can disappear in the FP8 representation itself.
+
+These are operator comparisons. Passing them does not establish whole-model
+logit parity, distributed state correctness or throughput.
diff --git a/docs/deepseek-execution.md b/docs/deepseek-execution.md
new file mode 100644
index 0000000000..6b5a7a04fa
--- /dev/null
+++ b/docs/deepseek-execution.md
@@ -0,0 +1,70 @@
+# DeepSeek execution contracts
+
+The served EXL3 product assigns two columns and eight rows to a SIMD group.
+Lanes 0 through 15 and 16 through 31 own the two columns. A lane decodes its tile state and
+shuffles it to the lanes that sum that column. A one-row product uses eight
+times as many groups as `tf_exl3_matmul_impl`; each row/column retains its
+products and TwoSum steps in K order. The kept-dense product instead assigns
+one output element per thread and accumulates BF16 weights in FP32, in
+sequential K order, into the region consumed by output rotation.
+
+## Concurrent groups and dispatch thresholds
+
+Attention can group `wq_a`, `wkv` and cache-owner compressor `wkv`/`wgate`.
+They read only the input and write separate outputs. A kept-dense compressor
+repack declines this group without encoding work, allowing the caller to run
+the individual products. Diagnostics also retain the individual path.
+
+The concurrent forward uses barriers after other dispatches to preserve serial
+ordering. `TF_DSV41_SERIAL=1` or an installed profiler selects the serial
+encoder. Grouped projection jobs receive separate consecutive scratch slices;
+a group is flushed before the next job would exceed capacity.
+
+For up to four rows, attention indexes all eight slices in one product: row
+`8*t+g` represents token `t`, group `g`, at the individual products' offsets.
+Indexed rows decode tiles independently. With more rows, the per-slice path
+shares each decoded tile across eight rows.
+
+The following M5 Max timings are inherited tuning observations, not M3 Ultra
+qualification or measurements from the TP2 run:
+
+| Attention rows | Indexed slices | Separate slices |
+| --- | --- | --- |
+| 4 | 1.0 ms | 1.5 ms |
+| 8 | 1.9 ms | 1.6 ms |
+| 256 | 58 ms | 17 ms |
+
+The shared expert's merged row also decodes alone, while its separate product
+shares tiles across rows. Inherited per-layer M5 Max observations were
+2.81 → 2.23 ms for one row, 4.20 → 4.03 ms for two and 7.23 → 7.29 ms for
+four. The implementation merges only one or two rows. `work.moeSlot` preserves
+routed row positions and places shared rows below their block, so SwiGLU and
+combine use the same values as the separate shared path.
+
+## Streamed Engram and lanes
+
+Engram tables remain in their safetensors shards. Each chunk deduplicates row
+IDs, reads misses through uncached preads on worker threads and compacts the
+rows into staging. A bounded row cache can supply hits. The unchanged gather
+kernel consumes the same row bytes as a resident table.
+
+`Fetcher.fetch` receives all misses for one stage call, sorted ascending, and
+writes each row's 256 weight bytes and 8 scale bytes at the corresponding
+output slot. `Pread` is the current implementation. The interface can describe
+another source, but does not itself implement remote table storage. Staging
+rejects out-of-range IDs before fetching and returns the distinct row count
+plus each original ID's local staging index.
+
+Each stream owns a lane slot. A window executes as that slot's own sub-forward;
+retaining only a prefix rolls the slot back and re-feeds those rows. Generation
+uses greedy draws with drafts disabled. The native load limit defaults to 70%
+of RAM unless `GLM_LOAD_LIMIT_GB` explicitly overrides it. `--engram-stream`
+leaves the two large tables on storage and binds the backbone in memory.
+
+## Template fixtures
+
+The G8 checker renders a supplied template with the engine's Jinja against the
+release's `encoding/tests/test_input_N.json` and `test_output_N.txt` fixtures.
+It follows their `test_examples_encoding_golden_outputs` convention, defaulting
+`thinking_mode` to `chat` unless a case supplies one. This checks template
+bytes, not model inference or tokenizer-normalization parity.
diff --git a/docs/deepseek-v41-mhc-fp64.json b/docs/deepseek-v41-mhc-fp64.json
new file mode 100644
index 0000000000..833a67ae98
--- /dev/null
+++ b/docs/deepseek-v41-mhc-fp64.json
@@ -0,0 +1,103 @@
+{
+ "scope": "mHC (hyperconnection mix) stage fp64 qualification: rounding level, Metal deltas and 1-ULP input amplification. Independent CPU reference parameterized comptime (math.zig MixOf/denseOf/mixesOf/splitOf/inverseOf/collapseOf/expandOf, f32 source-contract path bit-identical, f64 ideal path); harness zig/tests/deepseek_v41_reference/operator_hc_fp64.zig; fixtures hc_attention_rms.json and hc_fp64_rows.json (retained inputs only).",
+ "nrms_definition": "sqrt(sum(delta^2)/sum(ref^2)) in f64; ULP = |delta| / fp32 spacing at the fp64 reference value.",
+ "inputs": [
+ "token671_attention (retained token-671 embedding, four copies)",
+ "token1_ffn (retained token-1 FFN stream and branch, real hc_ffn scale/base)",
+ "eight_teacher_rows (the eight retained teacher embeddings)",
+ "wide_dynamic_range (copy magnitudes 2^24..2^-12, rotating signs)",
+ "split_args (16 retained unary arguments incl. 1.4393715858459473, controlling post argument, integer exp sweep -32..32, plus sigmoid/softmax spans)",
+ "cancellation fixtures (half-ULP terms after 1.0)"
+ ],
+ "rounding_level_fp32_reference_vs_fp64": {
+ "inverse_statistic_nrms": [
+ 2.7e-09,
+ 2.8e-08
+ ],
+ "projection_dense_nrms": [
+ 2.2e-08,
+ 2.9e-08
+ ],
+ "pre_sigmoid_nrms": [
+ 1.1e-08,
+ 4.2e-08
+ ],
+ "post_nrms": [
+ 3.9e-08,
+ 7.2e-08
+ ],
+ "comb_sinkhorn_nrms": [
+ 1.4e-08,
+ 6.8e-08
+ ],
+ "collapse_bf16_unequal_cells": 0,
+ "post_expand_bf16_unequal_cells": [
+ 0,
+ 5
+ ]
+ },
+ "metal_vs_fp64": {
+ "inverse_max_ulp": 0.6,
+ "projection_max_ulp": 0.5,
+ "pre_max_ulp": 2.75,
+ "post_max_ulp": 2.29,
+ "comb_max_ulp_near_cancelled_cells_only": 34.2,
+ "comb_max_abs": 1.5e-07,
+ "post_controlling_exponent_row_max_ulp": 1.61,
+ "exponent_sweep_minus32_to_32_worst_stage_nrms": 9e-08,
+ "bf16_unequal_cells": {
+ "token671": 0,
+ "token1_ffn": "3 of 20480",
+ "eight_teacher_rows": "8 of 163840",
+ "wide_dynamic_range": 0
+ },
+ "verdict": "every sub-stage sits at the fp32 source-contract rounding level; Metal exceeds the fp32 reference by at most ~1 ULP at the documented Metal exp boundary"
+ },
+ "cancellation_boundaries": [
+ {
+ "fixture": "collapse_half_ulps",
+ "metal": "0x3500",
+ "f32_ref": "0x3500",
+ "f64_ideal": "0x34c0"
+ },
+ {
+ "fixture": "post_half_ulps",
+ "metal": "0x0",
+ "f32_ref": "0x0",
+ "f64_ideal": "0x3400"
+ },
+ {
+ "fixture": "branch_product",
+ "metal": "0x3500",
+ "f32_ref": "0x3500",
+ "f64_ideal": "0x34c0"
+ }
+ ],
+ "amplification_1ulp_input": {
+ "method": "perturb one bf16 stream cell (or branch cell) by 1 ULP, propagate the ideal fp64 stage, output NRMS growth = sqrt(sum(dy^2)/sum(y^2)) / (|dx|/sqrt(sum(x^2))); 256 sampled cells per row; 8 teacher positions plus the token-1 FFN row",
+ "analog_fp64_gain_mean_per_row": [
+ 0.182,
+ 0.76
+ ],
+ "analog_fp64_gain_max_overall": 0.762,
+ "collapse_gain_mean": 0.5003,
+ "collapse_gain_max": 1.944,
+ "bf16_visible_gain_mean_per_row": [
+ 0.74,
+ 2.382
+ ],
+ "bf16_visible_gain_max": 34.5,
+ "bf16_rows_changed_fraction": "224-250 of 256 perturbations change at least one of 20480 output cells",
+ "mean_changed_cells": "8.1-13.1 of 20480",
+ "statistic_relative_shift_mean": 3.6e-07,
+ "comb_nrms_shift_mean": 3.6e-08,
+ "metal_confirmation_row0": {
+ "amp_mean": 1.886,
+ "fp64_bf16_amp_mean": 1.945,
+ "mean_changed_cells": 12.06
+ }
+ },
+ "gate_reading": "A single-ULP input difference is attenuated by the mix in exact arithmetic (gain < 1) and appears as 1-bf16-ULP flips on about 0.06 percent of output cells; the stage's own rounding-level output NRMS is 0 to 2.5e-5. Hyperconnection mixing therefore cannot turn fp32 ULP noise into the observed 4.096 percent whole-model NRMS; a specific operator-level difference elsewhere (sparse/compressed attention candidates) remains the plausible cause.",
+ "kernel_changes": "none required; no tolerance relaxed, no source coefficient injected",
+ "receipts": "gitignored receipts/ (mhc-fp64-run.log, mhc-fp64-parsed.json, build-summary.log); commit contains code, fixture and this summary"
+}
\ No newline at end of file
diff --git a/docs/deepseek-v41.md b/docs/deepseek-v41.md
new file mode 100644
index 0000000000..d22d6e58fa
--- /dev/null
+++ b/docs/deepseek-v41.md
@@ -0,0 +1,209 @@
+# DeepSeek-V4.1-Flash on one Mac
+
+The Metal path is experimental. Native serving requires
+`TF_EXPERIMENTAL=deepseek` while the shared whole-model release gate remains open.
+The earlier family qualification below used different checkpoints. The pinned
+1017 receipts establish parity for their measured inputs, not release approval
+for the LibertAIDAI pack; see [the shared gate discussion](https://github.com/ashhart/TensorFold/issues/847).
+
+TensorFold serves the whole DeepSeek-V4.1-Flash from the resident EXL3 packs on one Apple
+Silicon Mac. The engine runs on the lane core (one stream per slot, with windows of rows), so
+concurrent requests each keep their own caches and Engram cursor, and replies are greedy
+(`temperature 0`); drafts, grammar masks and tree requests are refused until their pieces
+land. The served checks hold: a drafted request reports `"draft": false` and equals the
+no-drafts reply, a resumed conversation equals the fresh one, and concurrent lanes equal
+their solo runs.
+
+## What you need
+
+- One Mac with 512 GB of unified memory. The 2.9bpw pack stages about 413 GB of weights
+ and the 2.8bpw pack about 190 GB; the server stays at 70% of RAM unless
+ `GLM_LOAD_LIMIT_GB` opts past it, as GLM's load does.
+- The original release's inference directory (`tokenizer.json`, `config.json`,
+ `chat_template.jinja` and the two Engram tables), beside the pack or passed with
+ `--engram-table-dir`. The weights come under DeepSeek's own terms; the inference code
+ is MIT and the native reference follows it (see THIRD_PARTY_NOTICES).
+- An EXL3 pack. The qualification ran jayleaton's 2.9bpw and 2.8bpw repacks; the 2.8bpw
+ pack's duplicated tensors are owned by the index, so a shard contributes only the
+ tensors the index maps to it.
+
+## The token map
+
+The Engram tables are keyed by a compressed vocabulary derived from the release's own
+tokenizer and inference config. The native engine builds the map itself. It decodes each
+id, falls back to the raw token when the decoder emits U+FFFD, normalizes through the
+release's chain (NFKD, strip marks, lowercase, the whitespace replaces) and deduplicates,
+with multipliers drawn from NumPy's PCG64 exactly as the source does. The result is
+byte-identical to the map shipped beside the original qualified 2.9bpw pack.
+Full normalization parity for other tokenizers, including Hangul, remains
+[unqualified](engram-token-map.md#normalization-qualification). When `engram-token-map.bin`
+is missing beside the checkpoint and the original's tokenizer sits beside the tables,
+the engine builds it at open; no Python step. To build it by hand:
+
+```sh
+zig build tf-dsv41-token-map -Dcpu=apple_m3 && ./zig-out/bin/tf-dsv41-token-map \
+ --tokenizer /tokenizer.json --config /config.json --output /engram-token-map.bin
+```
+
+## Serve
+
+```sh
+TF_EXPERIMENTAL=deepseek GLM_LOAD_LIMIT_GB=430 tensorfold-native serve \
+ --engram-table-dir --chat-template /chat_template.jinja \
+ --context 65536 --parallel 2 --no-drafts --prompt-cache-gib 0 \
+ --host 127.0.0.1 --port 8099 --name DeepSeek-V4.1-Flash --keep-warm 0
+```
+
+`--parallel` is the lane count: each lane is one stream with its own caches and Engram
+cursor. `--no-drafts` requests greedy decoding. A drafted request answers with
+`"draft": false` and the same tokens. The resident load
+reads no weights from disk during execution; a run that does fails the receipt's
+residency gate.
+
+## 256 GB: the Engram tables from disk
+
+The two Engram tables are about 203 GB of the release, and a token touches only 48 of
+their rows (24 per table, 256 bytes each). `--engram-stream` leaves them in their
+shards: the load checks both table headers as a resident load does but stages neither,
+and it stages only the tensors the backbone binds (no `mtp.*`, `vision.*`, `aligner.*`
+or image markers). Each chunk then hashes its row IDs in a command buffer of its own,
+reads each table's distinct rows with uncached `pread`s on a few threads, and runs the
+rest of the forward with the same gather kernel over the staged rows, so a streamed
+reply is the resident reply bit for bit (the gather check stages a synthetic shard and
+compares the two gathers byte for byte). The scale columns, 8 bytes a row, are read
+once at open and held in host memory (about 6.1 GB); they count against the load
+limit, not Metal admission.
+
+```sh
+TF_EXPERIMENTAL=deepseek GLM_LOAD_LIMIT_GB=220 tensorfold-native serve \
+ --engram-table-dir --engram-stream --chat-template \
+ --context 65536 --parallel 1 --no-drafts --prompt-cache-gib 0 \
+ --host 127.0.0.1 --port 8099 --name DeepSeek-V4.1-Flash --keep-warm 0
+```
+
+- Set `--context` explicitly: `--context 0` means the config's 1,048,576 positions.
+- The bound backbone tensors must fit the load limit without the tables. The
+ measured LibertAI 1017 2.9bpw checkpoint stages about 202.37 GB and runs with
+ `GLM_LOAD_LIMIT_GB=220` on 256 GB. Fit depends on the pack and which tensors the
+ backbone binds, not only its advertised bpw. See the
+ [pinned checkpoint and receipts](receipts/ds1017-tp2-2026-10-19/README.md).
+- Every row read costs a random read: put the tables on the internal SSD.
+- Releases without `chat_template.jinja` (DeepSeek-V4.1-Flash-1017 ships only
+ `encoding/`): use V4.1's template after checking it against the release's goldens,
+ ```sh
+ zig build tf-dsv41-template-check -Dcpu=apple_m3 && ./zig-out/bin/tf-dsv41-template-check \
+ /encoding/tests
+ ```
+ which renders each `test_input_N.json` with the engine's Jinja and compares it with
+ `test_output_N.txt`. Replies' `<|DSML| calls>` blocks (V4.1's spelling, a space
+ after the bar) parse into `tool_calls` like V4's `<|DSML|tool_calls>`.
+- The token map depends only on the tokenizer and config; a release with V4.1's
+ tokenizer and config has V4.1's map, but its tables are its own.
+
+The residency receipt changes with it, and this is a gate change: `tf-dsv41-run` with
+`DSV41_ENGRAM_STREAM=1` passes when the execution's disk reads stay within the row
+bytes the reader counted itself and no page-ins occur; the resident gate (no disk
+reads at all) is unchanged.
+
+## The numbers
+
+The per-operator reference tests compare the kernel, its FP32 source contract and
+an FP64 ideal, and assert rounding level. The whole-model
+picture is sensitivity-dominated: the FP32 oracle against the FP64 oracle on the same
+tokens is 2.90% NRMS, Metal against FP32 is 4.10% at 2.9bpw and 5.16% at 2.8bpw (the
+error grows as the quantization coarsens, the signature of the model amplifying
+FP32-level differences), and the indexer's 512-pick selection boundary lands inside an
+exact BF16 tie at every indexed layer past the window fill, so pick order there is
+decided by sub-quantum noise, folded into the sensitivity accounting. The 1% absolute
+gate stays a trip-wire next to argmax agreement, which held at every width.
+
+## Receipts
+
+The qualification receipts (served parity, lane bit equality, oracle
+and margin captures) live beside the PRs on the fork; the tree carries the tests that
+regenerate them.
+
+## Parallel sparse attention
+
+`TF_DSV41_PARALLEL_ATTENTION=1` enables parallel scoring of independent keys and
+output dimensions. The default retains the predecessor. The prototype's measured
+single-rank decode median increased from 4.0519 to 4.8595 forwards/s on M3 Ultra,
+with identical measured token IDs, teacher logits and lane results. Its full
+[measurement receipt](receipts/ds1017-attention-2026-10-19/README.md) records the
+binary and checkpoint pins, three runs per mode, isolated operator tests and
+limits. These measurements use `61ab890f`; the
+[current runtime receipt](receipts/ds1017-tp2-slots-2026-10-20/README.md) repeats
+the result after the explicit 256-thread reservation. This switch does not
+enable MTP or qualify model quality.
+
+## Experimental two-Mac MoE
+
+The development branch adds `--speed-up SETTINGS` for two Metal ranks. On two
+M3 Ultra Macs with 256 GB each, the measured logits and tokens match the single-rank
+engine. The current TCP comparison measures 3.10 decode forwards/s with output
+columns and a three-run median of 3.60 with routed slots, against 4.86 on one Mac.
+The [current receipts](receipts/ds1017-tp2-slots-2026-10-20/README.md) pin the runtime,
+checkpoint, numerical checks and remaining limits. The
+[earlier native HTTP receipt](receipts/ds1017-tp2-2026-10-19/README.md) belongs to
+`b27e39af`; the new native binary has not been requalified over HTTP. These
+measurements do not qualify MCDMA, RDMA, drafts or vision.
+
+Each rank holds the full backbone and reads its Engram tables from its own SSD.
+The ranks split the output columns of the MoE gate, up and down projections and
+exchange their output halves. Each dot product still covers its complete input
+width. Attention and routing run on both ranks. This partition does not halve the
+memory required on either Mac. The recorded comparisons cover only the pinned checkpoint and inputs.
+
+`TF_DSV41_TP_SLOTS=1` instead assigns complete routed slots 0/2/4 to rank 0 and
+1/3/5 to rank 1. Each rank computes full-width projections for its three slots,
+duplicates the shared expert and exchanges only finished routed outputs. Both
+ranks restore the original six-slot order before combining. This uses 41 exchanges
+per forward across 40 layers, including the history handshake, instead of 121.
+It still replicates all backbone weights and attention. Keep the switch identical
+on both ranks. The default remains the output-column partition.
+
+For MCDMA, use the existing [speed-up link settings](n-link-speed-up.md), with rank 0
+on one Mac and rank 1 on the other, one peer per rank, and the same library build.
+Alternatively, select TCP explicitly:
+
+```json
+{"transport":"tcp","rank":0,"host":"192.0.2.10","port":19750,"session":"my-ds41-test"}
+```
+
+Rank 0 binds the stated numeric address. Rank 1 uses the same address, port and
+session, with `"rank":1`. Scoped IPv6 is supported, with the scope naming the local
+interface on each Mac. The port and the next port carry ordered window writes and
+completion acknowledgements respectively. Wildcard addresses are refused. Use a
+trusted private link: the session check prevents accidental pairing, but supplies
+neither authentication nor encryption. TCP uses ordinary sockets and does not open
+an RDMA device. Its latency and bandwidth must be measured separately from MCDMA.
+If checkpoint loading finishes at different times, `"connect_timeout_ms":600000`
+allows ten minutes for startup pairing. The default is 30 seconds, the maximum is
+15 minutes, and message and completion waits remain bounded by 30 seconds.
+
+The CPU-only `zig build tf-tcp-probe -Dcpu=apple_m3` target builds a link check. Run
+`tf-tcp-probe RANK HOST PORT SESSION ROUNDS` on both Macs before loading a model.
+It checks peer payloads and reports exchange times for 64 bytes, 4 KiB, one routed
+MoE row and 256 rows. Those times are transport measurements, not inference speed.
+
+On both Macs, pass the respective settings file alongside the streamed-Engram options:
+
+```sh
+TF_DSV41_SPLIT_RESIDENCY=1 TF_DSV41_RESIDENCY_REFRESH=1 \
+TF_EXPERIMENTAL=deepseek GLM_LOAD_LIMIT_GB=220 tensorfold-native serve \
+ --engram-stream --engram-table-dir \
+ --chat-template --speed-up \
+ --context 8192 --parallel 2 --no-drafts --prompt-cache-gib 0 \
+ --host 127.0.0.1 --port 8099 --keep-warm 0
+```
+
+Send generation requests to rank 0. Rank 1 follows its reset, forward, commit and
+rollback commands and refuses independent generation. Each command checks the
+starting cursor; the acknowledgement checks the resulting cursor. A failed exchange
+invalidates the pair. Restart both ranks after a failure.
+
+Both ranks must run the same executable, checkpoint metadata, token map, context,
+chunk size, lane count and arithmetic settings. Startup compares these identities.
+It does not hash all weight and Engram bytes: the launch recipe must verify their
+SHA256 manifests separately, including the original 1017 table revision. A successful
+startup is not a substitute for those checks.
diff --git a/docs/engram-token-map.md b/docs/engram-token-map.md
new file mode 100644
index 0000000000..fdb55b72f3
--- /dev/null
+++ b/docs/engram-token-map.md
@@ -0,0 +1,44 @@
+# Engram token map
+
+`zig/src/families/deepseek_v41/token_map.zig` builds the map from the original
+release's tokenizer and inference config, without a Python runtime dependency.
+The algorithm comes from DeepSeek AI's MIT-licensed `inference/engram.py`,
+revision `2cba9e42aa026125f3ed06c6d98c1db82f7ca027`. The source URL is retained
+in the builder and its output manifest.
+
+The builder decodes token IDs in order. When decoding emits U+FFFD, it uses the
+raw token. Otherwise it normalizes the decoded text. Keys are deduplicated in
+first-seen order. The compressed vocabulary size must match the config.
+Each layer has 24 prime buckets, unique across layers, cumulative table offsets
+and multipliers from PCG64 seeded with `10007 * layer`.
+
+## Normalization qualification
+
+The release uses the Rust chain NFKC, NFD, StripAccents, Lowercase,
+`Replace([ \t\r\n]+, " ")`, `Replace(^ $, U+E000)`, Strip and
+`Replace(U+E000, " ")`. The local implementation uses decomposition tables,
+canonical ordering, Mn/Me removal and lowercase tables before the replacements
+and whitespace trimming. An empty result falls back to the original text.
+
+The intended simplification relies on `NFD(NFKC(x)) = NFKD(x)` and idempotent
+NFKD. Reproducing the Unicode tables does not establish parity of the complete
+Rust normalization chain. Hangul behavior remains a qualification caveat.
+Do not describe this builder as generally byte-identical to the release without
+comparing the resulting map against that release's tokenizer and implementation.
+
+## Random sequence and format
+
+`SeedSequence(seed).generate_state(4, uint64)` hashes the entropy word into a
+four-word pool with NumPy's hashmix/mix scheme. A second hash cycles that pool
+to draw eight words, paired little-endian. The existing reference check uses
+NumPy 2.5.3 for the two seeds used by this map.
+
+PCG64 initialization starts with zero state, steps once, adds `initstate`, then
+steps again. SeedSequence supplies the seed words; the increment is
+`(initseq << 1) | 1`, modulo 2^128. Bounded draws use Lemire's algorithm,
+matching NumPy's `masked=false` integer path.
+
+The binary is little-endian: magic, raw/compressed vocabulary sizes, pad ID,
+order, head and layer counts, tokenizer/config hashes, per-layer blocks, then
+the lookup array. A neighboring JSON manifest records the source, provenance
+hashes, packed-map hash and layer parameters.
diff --git a/docs/exl3-projection.md b/docs/exl3-projection.md
new file mode 100644
index 0000000000..b886be9c24
--- /dev/null
+++ b/docs/exl3-projection.md
@@ -0,0 +1,27 @@
+# EXL3 projection scheduling
+
+`Pipelines.pairs_up_to_groups` selects the two-column SIMD-group product when
+the corresponding sixteen-column grid has at most that many groups. Zero
+disables the path. `TF_EXL3_PAIRS_UP_TO_GROUPS` overrides the threshold.
+Both kernels retain the same result bits.
+
+The earlier M5 Max measurements recorded beside this setting used one row
+and the best of 20 runs. At K of 4096 to 5120, the recorded ratios were 2.15
+for N=512, 1.34 for N=1024 and 1.23 for N=1280. N=2304 (144 groups) gave 0.73.
+These historical tuning measurements do not qualify another GPU.
+
+`encodeJobs` dispatches every job at one stage before inserting the stage
+barrier. A concurrent encoder may overlap independent jobs; a serial encoder
+runs them in order. Each job computes the same product as `encode` or
+`encodeIndexed`. An output or scratch region must not overlap any other job's
+read or write region. The entire group is checked before encoding starts.
+
+The dense path retains both rotation stages and replaces the trellis product
+with a dense product. It accumulates in f32 into the scratch sums consumed by
+`rotate_out`, traversing K sequentially as the fp64 reference does.
+
+The concurrent GPU test chains four rounds of two products. The plain product
+writes the next round's input and the routed product writes a separate output.
+The serial control uses `encode` and `encodeIndexed`. Missing stage or round
+barriers, or overlapping scratch, can expose unfinished rows and change the
+compared bits. All output and intermediate input comparisons are retained.
diff --git a/docs/flash-next-rotary-bounds.md b/docs/flash-next-rotary-bounds.md
new file mode 100644
index 0000000000..9f1bd79482
--- /dev/null
+++ b/docs/flash-next-rotary-bounds.md
@@ -0,0 +1,90 @@
+# Flash Next rotary probe allowances
+
+This is a conditional numerical-contract change, not a kernel change or a passed device gate.
+The normalizer, phases and final reference remain independently evaluated in FP64.
+The allowance compares the device output with that reference after its FP32-to-BF16 rounding.
+The original nonfinite, repeatability, guards, row-count invariance and resource checks remain required.
+
+## Scalar accuracy required before qualification
+
+Let u = 2^-24. The candidate requires relative rsqrt error at most 4u, relative exp2 error at most 8u,
+and absolute sine/cosine error at most 8u at the actual FP32 argument.
+These are explicit scalar accuracy requirements, not guarantees about Metal fast functions.
+For finite normal results, two and four FP32 ulps are no larger than 4u and 8u relative, respectively;
+for sine/cosine, four ulps over [-1, 1] are no larger than 8u absolute.
+They are chosen independently of any observed output-to-bound ratio.
+Device component traces must establish these caps before the candidate can qualify.
+If they fail, do not increase them to fit the final output.
+
+The prepared fixture values are multiples of 1/8 in [-1, 1]; their four-row means are multiples of 1/32.
+The allowance rejects other input grids, weights outside [0.5, 2] in magnitude, nonnormal/nonpositive epsilon,
+and positions outside [0, 1048576), instead of claiming an unchecked universal input bound.
+The numerical probe's separate nonfinite gate retains its original exact class checks.
+
+## Normalization and intermediate BF16 values
+
+For this fixture grid, every square is an integer multiple of 1/1024, at most one.
+At most 256 such terms total at most 262144 units, requiring only 19 significant bits.
+All FP32 products and additions are therefore exact in any order.
+This proof does not assume a particular SIMD reduction tree or weaken the accumulator declaration.
+The registered generic reduction depth remains unchanged.
+
+Let S be that exact sum and q = S/n + epsilon.
+Division by n is a power-of-two scale, so only the epsilon addition contributes to the denominator radius:
+
+ Eq = u |q| + guard(q).
+
+Enclose 1/sqrt(q_device) by evaluating q +/- Eq, then apply the rsqrt accuracy cap.
+Multiply each signed input and weight by both inverse endpoints.
+Expand them for two FP32 products with gamma(2, u), then round both endpoints through FP32 and BF16.
+Monotone rounding encloses midpoint crossings instead of charging their error to a cancelled final result.
+This gives the already rounded reference pair a, b and absolute radii Ea, Eb.
+Unrotated dimensions need only their own BF16 interval radius.
+
+Both pooling probes use an exact four-row FP32 mean in these fixtures.
+The checker verifies every addition and the power-of-two scale is exact before its BF16 store.
+An inexact pooling fixture is refused and needs its own propagated mean interval, not an implicit larger tolerance.
+The subsequent normalization interval is identical to the preparation probes'.
+
+## FP32 phase construction
+
+The plain reference frequency is f = 2^x with x = -(i/32) log2base.
+The device exponent x32 is the FP32 rounding of that exact product; i/32 is exact.
+Evaluate 2^x32 independently in FP64 and enclose the exp2 result with its required relative cap.
+For YaRN, both sides read the same stored FP32 frequency, so this interval has zero frequency radius.
+Supported integer positions are exactly representable in FP32.
+Round the position-times-frequency interval endpoints to FP32 and compare them with the ideal angle t = p f.
+Let h be the largest resulting absolute angle discrepancy.
+
+Sine and cosine are globally 1-Lipschitz and bounded by one.
+Taylor's remainder also gives a local enclosure:
+
+ Dcos = min(2, h, |sin(t)| h + h^2/2)
+ Dsin = min(2, h, |cos(t)| h + h^2/2).
+
+Add the absolute trig accuracy cap, multiply by the stored YaRN scale and propagate that FP32 multiply.
+This yields coefficient errors EC and ES for C = scale cos(t), S = scale sin(t).
+Phase error is weighted by the unrotated inputs; it cannot disappear when a rotated term approaches zero.
+
+## Rotation and the already rounded reference
+
+For each relabelled pair, let r = a C +/- b S.
+A pre-output error allowance is
+
+ E = |C| Ea + |S| Eb
+ + (|a| + Ea) EC + (|b| + Eb) ES
+ + gamma(2,u) [(|a| + Ea)(|C| + EC) + (|b| + Eb)(|S| + ES)].
+
+The depth-two term covers separate products and addition; contraction needs no larger allowance.
+The final absolute comparison to w = BF16(FP32(r)) uses
+
+ E + 2^-8 (|r| + E) + |r - w| + guard(r).
+
+The reference's own rounding is explicit. A relative term based only on |w| would miss cancellation.
+The implementation adds conservative FP64 evaluation guards and a smallest-normal FP32 floor for underflow/flush.
+These guards are independent of measured errors.
+
+The generic kernel bound is unchanged. Only these ten numerical probes provide an absolute-allowance callback;
+the mask keeps its exact check. Both the shape and accumulation gates consume the same callback and reject invalid allowances.
+CPU witnesses and real callback tests establish the stated arithmetic and wiring only.
+All eleven probes, component caps, compiler/resource receipts, model equality and long-context fidelity must be rerun before release.
diff --git a/docs/glm-prompt-k.md b/docs/glm-prompt-k.md
new file mode 100644
index 0000000000..d8feb7203a
--- /dev/null
+++ b/docs/glm-prompt-k.md
@@ -0,0 +1,44 @@
+# GLM prompt K policy experiment
+
+Fresh prompt batching credits @BobClawblaw's #1014. Its default remains one K slice at every prompt row count.
+This separate experiment selects K slices from the weight's N and K dimensions, independent of the number of
+prompts, their row count or their position in a batch. It uses the existing tiled kernels in windows of at most
+64 rows and the existing ordered fp32 slice reduction. Verify and draft windows keep their previous policy.
+
+Select `TF_GLM_PROMPT_K=shape` on every rank to try it. An absent value or `one` keeps the default.
+The experiment requires `TF_GLM_MM=1`; the untiled combination is refused. The selected policy is included in rank
+agreement, so different choices refuse before loading weights. Applying this patch does not enable the experiment.
+
+No speed or device qualification is claimed. The different reduction grouping can change near-ties even though
+precision is unchanged. Keep the current numerical and quality bars. The default must remain unchanged until
+matched device evidence supports another decision.
+
+## Device checks
+
+Run the current kernel contract and measured-resource gate first. This experiment changes no kernel source, ABI,
+thread count or shared-memory specialization. Keep the existing GLM launch records; pending resources still need
+measurement on the device being qualified.
+
+Run `tf-cuda-test glm-mm-fixed` on a GB10. It compares the default and experimental policies for bf16 weights and
+4-, 6- and 8-bit weights, with fp32 and bf16 outputs. Every input row runs alone, then in batches at offsets 0, 1 and
+7, across row buckets and partial tails through 129 rows. Input stride includes padding; output prefix and tail
+bytes are guarded. Two matrix shapes exercise shape-selected K splits. Results are checked bitwise against each
+row alone and an independent fp64 product on exact dyadic fixtures. Packed fixtures use independently assembled
+codes and fp16 scales. These synthetic checks do not establish full-checkpoint numerical fidelity.
+
+The shader-free `test-glm-mm-policy-host` checks actual dispatch with recording CUDA calls. The separate
+`compile-glm-prompt-gate-host` code-generates the actual device gate with inert images, without executing it.
+Neither replaces the device run. The ordinary `compile-cuda-tests` target also includes the gate in the runner.
+
+Then run the existing GLM layer/operator references at all selected checkpoint widths, and the served greedy and
+seeded checks: drafted/plain, fresh/resumed and concurrent/solo. Keep prompt caching enabled for resumes. Include
+MTP and DFlash2, unequal prompt lengths, several 2048-row passes, an already-live stream, cancelled admissions and
+image/resumed prompts alongside fresh text. Repeat both-rank gather/split/ring cases.
+
+## Timing
+
+Use one pinned binary, checkpoint, precision, context, prompt set and cache state. Compare `one` and `shape` on both
+ranks in matched A/B/B/A runs with one, eight and sixteen streams. Record prompt time, first-token latency, decode
+throughput and whole-run throughput separately. Keep drafting and cache settings equal. Repeat structured-output
+and quality checks, including the original IFEval/JSON comparisons. A faster isolated matmul is insufficient to
+resolve the reported lone-stream regression or justify a default change.
diff --git a/docs/known-issues.md b/docs/known-issues.md
index cd919595ae..61c89c5a30 100644
--- a/docs/known-issues.md
+++ b/docs/known-issues.md
@@ -28,6 +28,8 @@ release fixes it, and the release notes say which one.
- On a Mac without tensor units (M1 to M4), Flash Next's uniform 6-bit checkpoint decodes different tokens from 1.0.9
since 1.0.10's faster products. `FZ_M3_LANES=1` keeps the earlier kernels and their tokens.
- @Bizuayeu's regression run on shipped 1.1.0 loaded the whole GLM-5.3-Flash NVFP4 checkpoint on two GB10s at TP=2 in 302.1 s, kept one token hash per prompt across three runs and found a passphrase in 199,307 tokens; scored prompts and more than two ranks were not covered ([#1040](https://github.com/ashhart/TensorFold/issues/1040)).
+- DeepSeek-V4.1 CUDA pieces land in this release, but no release binary serves DeepSeek checkpoints until its whole-model gate ([#827](https://github.com/ashhart/TensorFold/pull/827)) passes.
+
## Living Weights (`--slide`)
diff --git a/docs/living-weights.md b/docs/living-weights.md
index 3c2871e4db..82f9047af5 100644
--- a/docs/living-weights.md
+++ b/docs/living-weights.md
@@ -11,14 +11,15 @@ as Sliding Weights (experimental), and its flag and API use the name `slide`.
## What you need
- An Apple-silicon Mac (tested on an M5 Ultra and an M3 Ultra), or an NVIDIA GB10 (DGX Spark) with the CUDA build,
- with room for the model and a copy of it. On a GB10, `--slide` also keeps about 26 GiB of GPU memory for its
- trainer; on a Mac the trainer works on the model's own weights.
-- The Nemotron 3.5 Lightning MLX 4-bit checkpoint, the only model it learns into today.
+ with room for the model and a copy of it. On a GB10, `--slide` also keeps GPU memory for its trainer (about 26 GiB
+ for Nemotron, 10.6 GiB for Qwen3.8 Flash Next); on a Mac the trainer works on the model's own weights.
+- The Nemotron 3.5 Lightning MLX 4-bit checkpoint, or, on a GB10 only, the Qwen3.8 Flash Next MLX 4-bit group-32
+ checkpoint with its MTP head. Macs refuse `--slide` for Flash Next.
- A TensorFold build with the pocket learner described here (the release after 1.0.6). A folder taught by 1.0.3 to
1.0.6 still serves and answers from what it learned, but `--slide` refuses it: those releases saved lessons into
the shared experts the pocket widens. Teach a fresh copy instead.
-A folder that learned on either backend serves on the other.
+A Nemotron folder that learned on either backend serves on the other.
## Quick start
@@ -81,12 +82,27 @@ curl -N http://127.0.0.1:8080/v1/slide/learn \
-d '{"text": "I like blue."}'
```
-The reply streams progress as server-sent events: `fact` when it finds a fact in your text, `learning` when it starts on
-it, and `learned` when it is done. `"recalled": true` means it answered its held-out questions with the fact, and its
-change is kept. `false` means none of it is kept: the lesson is taken out whole before anything is written, and
+The reply streams server-sent events: `fact` when it finds a fact, `learning` when it starts, `progress` during the
+lesson, and `learned` when it is done. Progress carries `phase`, `at` and `of`; zero `of` means the total is unknown.
+A `progress` event with `heartbeat: true` arrives every five seconds while a long unit is still running. It reports
+that the request remains open, without claiming another unit completed.
+After the facts finish, `saving` progress continues until `saved` confirms the shard write or `failed` reports its
+error with `phase: "saving"`. `[DONE]` follows that result.
+
+`"recalled": true` means it answered its held-out questions with the fact, and its change is kept. `false` means none of it is kept: the lesson is taken out whole before anything is written, and
`message` says why, for example that the fact did not come back on its held-out questions or that it disturbed another
-answer. A fact takes 1.5 to 3 minutes. The folder's first lesson starts by keeping its original files, which adds a
-few seconds on a Mac.
+answer. Lesson time depends on the family and device and can take many minutes. The first lesson also keeps the
+folder's original files before opening the stream.
+
+New lessons use conversation checks only where the learner reads full conversation prompts through taps. Bounded
+readers use standalone held-out questions. Saved graph entries retain their recorded conversation checks.
+
+A body may also carry `"questions": [...]` (at most 16 of up to 300 bytes each; anything but an array of strings is a
+400). Each fact's lesson takes them ahead of the questions the model writes, and the model answers each knowing the
+fact, so you supply no answers. They are judged as the written ones are: one that does not name the fact's subject, asks
+the fact's own value or is not answered from the fact is dropped, and the text-only path is unchanged without the field.
+The model-written questions establish the subject before supplied questions are filtered. For example `{"text": "Our warehouse robot is called
+Pebble.", "questions": ["What is our warehouse robot called?"]}`.
### 5. Ask
@@ -326,6 +342,7 @@ the folder's files alone while a server or a rollback has it.
| `--slide rewrites the model's own files, and … is a link to data another file shares` at startup | The folder holds links into the Hugging Face cache (or hard links); make the copy with `cp -cRL` (Linux: `cp -RL`) as in step 2 |
| `--slide learns on a GB10 (sm_121) so far` at startup | CUDA learning is qualified on a GB10 only; serve this GPU without `--slide`, or teach on a Mac or a GB10 |
| `--slide runs a prompt one chunk at a time` at startup | Serve it without `--segments` or `TF_CUDA_SEGMENTS` |
+| `--slide trains on Flash Next's own glue` at startup | Serve Flash Next without `TENSORFOLD_CUDA_KERNELS` |
| `--slide adds learned neurons beside every shared expert, and … is unquantized` at startup | The folder was taught by TensorFold 1.0.3 to 1.0.6; serve it without `--slide`, or teach a fresh copy |
| `--slide lets any client that reaches this server rewrite the weights in …` at startup | Add a key: `--api-key-file FILE`, `--api-key KEY` or `TENSORFOLD_API_KEY` (step 3) |
| `--slide needs a key this server can read, and --api-key-file … gives none: …` at startup | Make the file (one key a line), `chmod 600` it, and check its path; the end of the line gives the reason |
@@ -357,8 +374,8 @@ Living Weights is experimental. Measured on Nemotron 3.5 Lightning (MLX 4-bit) o
- **Long documents about one subject are the weak spot.** From a 21-fact company handbook it kept about half the facts
and answered 6 of 16 questions, and with ten facts about the company kept, its answers about things the handbook does
not say got worse, most with thinking on. Teach a few facts per subject, and always teach a copy.
-- **Time.** A fact takes about 1.5 to 2.5 minutes on an M5 Ultra or M3 Ultra and 2.5 to 3 minutes on a GB10, a good
- part of it the model writing its own lesson. A document takes that per fact.
+- **Time.** Each fact needs its own generated lesson and checks. The first lesson also writes the keep prompts and
+ personal bank. Independent cold prompts share the serving lanes; time still depends on the model and device.
- **Memory.** On a Mac the trainer works on the model's own weights. On a GB10, `--slide` keeps about 26 GiB for its
trainer, which is ready a few seconds after the server starts.
- **Undo.** On an M3 Ultra, the snapshot of Nemotron 3.5 Lightning (7 files, 18.5 GB) took 5.3 s as clones, a new
@@ -371,6 +388,15 @@ Living Weights is experimental. Measured on Nemotron 3.5 Lightning (MLX 4-bit) o
Living Weights. A folder holding many learned facts decodes slower on a Mac (about 16% with 21 facts in the pocket),
and drafted replies still equal plain ones.
+On Qwen3.8 Flash Next (MLX 4-bit group 32 with its MTP head) on a GB10, five facts about the user were taught one
+after another to a fresh copy. Three were kept, in 8.7 to 10.5 minutes each (two and a half to four of them the model writing its
+lesson and three to four its trainer's first round, half a minute each round of checks), and each came back right after
+its lesson; one was refused at once, the model having written too few clean answers to learn from, and one was not
+kept after five rounds in 40 minutes. A later lesson can stop an earlier fact coming back (#870): after the five, two
+of the three kept facts still answered. Unrelated questions kept their answers, plain and `--slide` restarts gave the
+same replies, drafted replies equal `"draft": false` and four streams at once equal each alone. With three facts in
+the pocket, drafted decode was 5% slower (80 against 84 tokens a second on code) and `--no-drafts` 1% slower.
+
While it learns, near misses and free writing about the fact's subject are trained to stay as they were. Before
keeping a fact, it checks wordings it never trained on, with thinking off and on, near misses and unrelated prompts.
If the fact does not come back, or another answer changes, the whole lesson is taken out before anything is written.
diff --git a/docs/qwen35-learning-port.md b/docs/qwen35-learning-port.md
new file mode 100644
index 0000000000..8fc1712c09
--- /dev/null
+++ b/docs/qwen35-learning-port.md
@@ -0,0 +1,49 @@
+# Dense 27B Living Weights on the owned CUDA engine
+
+The learning work is by @ZackSample in #911.
+The serving engine is by @BobClawblaw in #836, with shared image work from #885.
+The integration uses the same Living Weights commit gate as the maintained Flash Next port.
+
+The dense model learns through gate, up and down rows beside each MLP.
+Its trainer calls the owned serving forward with optional intermediate keeps and MLP nudges.
+Training and conversation taps currently run one row at a time.
+Prefix reuse is disabled for training.
+There is no external captured-kernel execution path in this adapter.
+CUDA graph capture of the repository's own serving launches remains available when no learning hooks or pockets act.
+
+Pocket replacement stages all three vectors before publication.
+Read errors propagate to the common gate.
+The gate judges the stable quantized form, retains earlier facts, and restores the complete pocket after rejection.
+The loader separates saved pocket rows and down columns from the untouched base affine codes.
+Images use the shared Qwen processor, decoder, tower and canonical identity.
+Text uses #836's existing shader ABI; image rotary positions use separate additions.
+
+For a fitted scalar z, the sharp-16 construction is z² sigmoid(16z).
+It is not exactly relu(z)².
+The common positive-row tests at z = 1, 1.5 and 3 use relative tolerance 1e-6.
+A fitted negative margin of -0.5 bounds sigmoid(16z) below about 0.000336.
+Quantization and the actual forward still need separate qualification.
+Power-of-two scaling does not preserve arbitrary subnormal or overflowing f32 values.
+
+Host tests and compile-only checks are evidence about source integration, not CUDA numerical qualification.
+Build on a CUDA machine with the repository's nvcc workflow, then run:
+
+```
+tf-cuda-test q27-back
+tf-cuda-test q27-gemm
+tf-cuda-test gdn-back-27b
+tf-cuda-test kernel-contract
+tf-cuda-test kernel-contract-limits FATBIN_DIRECTORY
+tf-qwen35-learn FRESH_MODEL IDS_JSON_ARRAY
+```
+
+The model check requires a fresh untaught folder and at least 24 token IDs.
+It checks serving/trainer loss, packed/solo loss, and directional differences at early and answer rows.
+All configured finite-difference step sizes must meet the median and worst error bounds.
+The resource extension deliberately fails until real per-SM compiler receipts replace the pending entries.
+
+CUDA numerical runs, learned and fresh full-token parity, sequential held-out retention, unrelated drift,
+rejected-round conservation, save/reopen/delete, image/cache identities, drafted/plain, seeded,
+resumed/fresh and concurrent/solo qualification are unchecked on this Mac.
+The review records that the contributor's older 27B recipe produced garbage on wide unrelated prompts after sixteen facts.
+That historical failing control is not replaced by host tests of this port.
diff --git a/docs/receipts/ds1017-attention-2026-10-19/README.md b/docs/receipts/ds1017-attention-2026-10-19/README.md
new file mode 100644
index 0000000000..dd010f9646
--- /dev/null
+++ b/docs/receipts/ds1017-attention-2026-10-19/README.md
@@ -0,0 +1,82 @@
+# DeepSeek-V4.1-1017: parallel sparse attention on M3 Ultra
+
+The measured prototype increased median decode throughput from 4.0519 to
+4.8595 forwards/s on one M3 Ultra, a factor of 1.1993. All generated token IDs
+and the tested teacher and lane logits match the predecessor byte for byte.
+These measurements use commit `61ab890f46e1f4eb173eb72cd89c0e1766845331`.
+They do not establish the speed of a later binary or qualify the complete
+model against the original checkpoint.
+
+## Runtime and inputs
+
+[build.json](build.json) pins the upstream base, Zig 0.17.0,
+`-Dcpu=apple_m3` and the four measured binaries. The machine has an M3 Ultra,
+80 GPU cores and 256 GB unified memory. The checkpoint and original 1017
+Engram tables are the same as in the [pair receipt](../ds1017-tp2-2026-10-19/model.json).
+The [pack manifest](../ds1017-tp2-2026-10-19/pack-sha256.json) and both Engram
+SHA256s were verified before execution.
+
+Both modes stream Engram from local SSD and use split weight residency with
+periodic refresh. `TF_DSV41_SERIAL` is unset in both modes. The labels
+`serial` and `parallel` in the measurements distinguish the attention
+pipeline, rather than the command encoder mode. The new pipeline is opt-in
+with `TF_DSV41_PARALLEL_ATTENTION=1`; the default keeps the predecessor.
+
+## Decode measurements
+
+Each mode ran three times with the same 32 prompt IDs and 128 generated IDs.
+Prefill produces the first completion token, leaving 127 measured decode
+forwards. The table reports forwards/s, rather than 128 divided by decode time.
+
+| Mode | Run 1 | Run 2 | Run 3 | Median |
+| --- | ---: | ---: | ---: | ---: |
+| Predecessor | 4.0519 | 4.0507 | 4.0536 | 4.0519 |
+| Parallel attention | 4.8595 | 4.8532 | 4.8628 | 4.8595 |
+
+[measurements.json](measurements.json) retains each wall time, prefill time and
+GPU time. Decode GPU time fell from about 30.64 to 25.43 seconds. The separate
+268-token teacher prefill became slightly slower, from 7.4010 to 7.5690
+seconds. The result therefore does not show that every workload improves.
+
+All six generation runs passed the execution I/O gate, with zero page-ins.
+Each counted 126,844,928 Engram bytes; measured disk reads stayed within that
+allowance. This gate permits counted Engram disk I/O.
+
+[checks.json](checks.json) preserves each execution and residency record.
+
+## Numerical and operator checks
+
+The GPU suite passed 100 tests, the reference suite 28 and the EXL3 GPU suite
+29. The new operator matched predecessor bytes across 51 shapes, including
+tile edges and nonfinite inputs. Repeated runs preserved the inputs and guards.
+The isolated warmed attention timings used 32 samples per case:
+
+| Position | Predecessor median | Parallel median |
+| --- | ---: | ---: |
+| 128 | 0.8975 ms | 0.1802 ms |
+| 8191 | 4.2574 ms | 0.7584 ms |
+
+The 268-token teacher sequence matches the original single-rank logit SHA256
+`910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2`
+with the predecessor, the parallel pipeline and one-token teacher chunks.
+Two lane slots at context 8192 and chunk 16 finish at positions 134/134 with
+zero bit disagreements against solo execution. All six generation runs match
+the [frozen token IDs](../ds1017-tp2-2026-10-19/prompts/gen-short-tokens.json).
+
+## Reproduction and limits
+
+Build the runner and GPU checks at the pinned commit. Use the same checkpoint,
+Engram tables, token map and [prompt IDs](../ds1017-tp2-2026-10-19/prompts/).
+Set `DSV41_ENGRAM_STREAM=1`, `TF_DSV41_SPLIT_RESIDENCY=1` and
+`TF_DSV41_RESIDENCY_REFRESH=1`. Run `tf-dsv41-run generate` three times per
+mode with 128 completion tokens. Leave `TF_DSV41_PARALLEL_ATTENTION` unset
+for the predecessor and set it to `1` for the new pipeline. Compare the token
+files and teacher `logits.f32`, and retain each `residency.json`, including
+failed gates.
+
+This receipt covers single-rank execution. It supplies no new TP2 or HTTP
+serving result. The kernel-contract registration and explicit 256-thread
+reservation were added later in `cdfa5203`; those changes have a separate
+[runtime and performance receipt](../ds1017-tp2-slots-2026-10-20/README.md). Whole-model FP64, MTP, long-context and
+Rust-normalizer qualification remain open. The protected measurement window
+restored the original Qwen service and reranker and verified both responses.
diff --git a/docs/receipts/ds1017-attention-2026-10-19/build.json b/docs/receipts/ds1017-attention-2026-10-19/build.json
new file mode 100644
index 0000000000..e3a9856603
--- /dev/null
+++ b/docs/receipts/ds1017-attention-2026-10-19/build.json
@@ -0,0 +1,12 @@
+{
+ "commit": "61ab890f46e1f4eb173eb72cd89c0e1766845331",
+ "upstream": "d1018b318fb51981c4a18f12e40269a4d3ee9713",
+ "zig": "0.17.0",
+ "cpu": "apple_m3",
+ "binary_sha256": {
+ "tf-dsv41-run": "7afd7931bb3f35cc9b58ee90ff8c77682791c35d11351b883277569edb05a304",
+ "tf-dsv41-gpu": "3761ad37dd1a219cb361472af65baae0c019f8695c803719ad2931ca152e8153",
+ "tf-dsv41-reference": "e3632891a409dbba1d85407e9072827f53a73674d82e169521fe47812813d04c",
+ "tf-exl3-gpu": "39c19f7b7e2ebc78296853e9ec06634a31d82a95ac4329ea796910481deb5efc"
+ }
+}
diff --git a/docs/receipts/ds1017-attention-2026-10-19/checks.json b/docs/receipts/ds1017-attention-2026-10-19/checks.json
new file mode 100644
index 0000000000..a67daa8318
--- /dev/null
+++ b/docs/receipts/ds1017-attention-2026-10-19/checks.json
@@ -0,0 +1,503 @@
+{
+ "origin": "actual 61ab890f full-model stdout records",
+ "phases": {
+ "serial-generate-0": {
+ "execution_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.31796583335381,
+ "footprint_bytes": 211363926264,
+ "disk_read_bytes": 20365312,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3180440000724047,
+ "decode_seconds": 31.343201458337717,
+ "decode_gpu_seconds": 30.642673874972388
+ }
+ ],
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": false,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 79,
+ "ready_footprint_bytes": 210974347488,
+ "final_footprint_bytes": 211357552912,
+ "load_seconds": 51.81353220832534,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 99733504,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.424616,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ "serial-generate-1": {
+ "execution_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.3149498333223164,
+ "footprint_bytes": 211363795192,
+ "disk_read_bytes": 20037632,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3150156666524708,
+ "decode_seconds": 31.352408249978907,
+ "decode_gpu_seconds": 30.638499875785783
+ }
+ ],
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": false,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 79,
+ "ready_footprint_bytes": 210974249160,
+ "final_footprint_bytes": 211357421840,
+ "load_seconds": 53.2119858332444,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 99467264,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.431939,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ "serial-generate-2": {
+ "execution_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.319603250012733,
+ "footprint_bytes": 211363811648,
+ "disk_read_bytes": 20037632,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3196700416738167,
+ "decode_seconds": 31.3299550000811,
+ "decode_gpu_seconds": 30.63455495785456
+ }
+ ],
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": false,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 79,
+ "ready_footprint_bytes": 210974200056,
+ "final_footprint_bytes": 211357421912,
+ "load_seconds": 53.10753149993252,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 99467264,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.421751,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ "parallel-generate-0": {
+ "execution_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.3303610000293702,
+ "footprint_bytes": 211330355472,
+ "disk_read_bytes": 20037632,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3304207084001973,
+ "decode_seconds": 26.13416887493804,
+ "decode_gpu_seconds": 25.44145883375313
+ }
+ ],
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 67,
+ "ready_footprint_bytes": 210974314720,
+ "final_footprint_bytes": 211323982120,
+ "load_seconds": 53.10356449999381,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 99467264,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.421481,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ "parallel-generate-1": {
+ "execution_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.328212249907665,
+ "footprint_bytes": 211330486544,
+ "disk_read_bytes": 20037632,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.32828491658438,
+ "decode_seconds": 26.168299750075676,
+ "decode_gpu_seconds": 25.43944699934218
+ }
+ ],
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 67,
+ "ready_footprint_bytes": 210974363824,
+ "final_footprint_bytes": 211324113192,
+ "load_seconds": 53.199200874892995,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 99467264,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.452272,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ "parallel-generate-2": {
+ "execution_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.3295293749542907,
+ "footprint_bytes": 211330175176,
+ "disk_read_bytes": 20037632,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3295881665544584,
+ "decode_seconds": 26.116685458342545,
+ "decode_gpu_seconds": 25.41125166718848
+ }
+ ],
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 67,
+ "ready_footprint_bytes": 210974150808,
+ "final_footprint_bytes": 211323785440,
+ "load_seconds": 53.048179791658185,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 99467264,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.429017,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ "serial-teacher": {
+ "execution_records": [
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 7.401024125050753,
+ "gpu_seconds": 7.259088583406992
+ }
+ ],
+ "residency": {
+ "mode": "teacher",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": false,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 18,
+ "ready_footprint_bytes": 210974298408,
+ "final_footprint_bytes": 211401691624,
+ "load_seconds": 50.918394874897785,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 152551424,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 197459968,
+ "engram_rows_requested": 12864,
+ "engram_rows_unique": 11872,
+ "engram_cache_hits": 0,
+ "engram_reads": 11870,
+ "engram_wait_seconds": 0.098097,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ "parallel-teacher": {
+ "execution_records": [
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 7.568995250039734,
+ "gpu_seconds": 7.427380041684955
+ }
+ ],
+ "residency": {
+ "mode": "teacher",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 18,
+ "ready_footprint_bytes": 210974314768,
+ "final_footprint_bytes": 211368186344,
+ "load_seconds": 53.3257579166675,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 152551424,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 197459968,
+ "engram_rows_requested": 12864,
+ "engram_rows_unique": 11872,
+ "engram_cache_hits": 0,
+ "engram_reads": 11870,
+ "engram_wait_seconds": 0.098072,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ "parallel-decode-teacher": {
+ "execution_records": [
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 56.87473999999929,
+ "gpu_seconds": 55.36408349976409
+ }
+ ],
+ "residency": {
+ "mode": "teacher",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 139,
+ "ready_footprint_bytes": 210974265568,
+ "final_footprint_bytes": 211360223552,
+ "load_seconds": 50.935376124922186,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 167542784,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 214106112,
+ "engram_rows_requested": 12864,
+ "engram_rows_unique": 12864,
+ "engram_cache_hits": 0,
+ "engram_reads": 12864,
+ "engram_wait_seconds": 0.88551,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ "parallel-lanes": {
+ "execution_records": [
+ {
+ "mode": "lanes",
+ "slots": 2,
+ "chunk": 16,
+ "max_logit_error_slot0": 0,
+ "max_logit_error_slot1": 0,
+ "bit_disagreements": 0,
+ "position_slot0": 134,
+ "position_slot1": 134,
+ "gpu_seconds": 16.052
+ }
+ ],
+ "residency": {
+ "mode": "lanes",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 8192,
+ "consumed_tokens": 134,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 202792583168,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 79,
+ "ready_footprint_bytes": 209528164144,
+ "final_footprint_bytes": 210025893848,
+ "load_seconds": 52.159936833311804,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 323092480,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 414777344,
+ "engram_rows_requested": 25728,
+ "engram_rows_unique": 24928,
+ "engram_cache_hits": 0,
+ "engram_reads": 24928,
+ "engram_wait_seconds": 0.284517,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ }
+ },
+ "teacher_logit_sha256": "910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2",
+ "generated_tokens_match_frozen_ids": true,
+ "restoration": {
+ "window_exit": 0,
+ "original_qwen_response_verified": true,
+ "original_reranker_response_verified": true
+ },
+ "operator_tests": {
+ "deepseek_gpu": 100,
+ "reference": 28,
+ "exl3_gpu": 29,
+ "attention_predecessor_bit_equal_shapes": 51,
+ "isolated_timing_samples_per_case": 32
+ },
+ "retained_raw_logit_sha256": {
+ "serial-teacher": "910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2",
+ "parallel-teacher": "910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2",
+ "parallel-decode-teacher": "910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2"
+ },
+ "retained_raw_token_sha256": {
+ "serial-0": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "serial-1": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "serial-2": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "parallel-0": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "parallel-1": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "parallel-2": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac"
+ }
+}
diff --git a/docs/receipts/ds1017-attention-2026-10-19/measurements.json b/docs/receipts/ds1017-attention-2026-10-19/measurements.json
new file mode 100644
index 0000000000..b86f3ed0e8
--- /dev/null
+++ b/docs/receipts/ds1017-attention-2026-10-19/measurements.json
@@ -0,0 +1,67 @@
+{
+ "serial": {
+ "runs": [
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3180440000724047,
+ "decode_seconds": 31.343201458337717,
+ "decode_gpu_seconds": 30.642673874972388,
+ "decode_forwards_per_second": 4.051915378485253
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3150156666524708,
+ "decode_seconds": 31.352408249978907,
+ "decode_gpu_seconds": 30.638499875785783,
+ "decode_forwards_per_second": 4.050725513249383
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3196700416738167,
+ "decode_seconds": 31.3299550000811,
+ "decode_gpu_seconds": 30.63455495785456,
+ "decode_forwards_per_second": 4.053628548131373
+ }
+ ],
+ "median_forwards_per_second": 4.051915378485253
+ },
+ "parallel": {
+ "runs": [
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3304207084001973,
+ "decode_seconds": 26.13416887493804,
+ "decode_gpu_seconds": 25.44145883375313,
+ "decode_forwards_per_second": 4.8595385071453165
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.32828491658438,
+ "decode_seconds": 26.168299750075676,
+ "decode_gpu_seconds": 25.43944699934218,
+ "decode_forwards_per_second": 4.853200292450515
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3295881665544584,
+ "decode_seconds": 26.116685458342545,
+ "decode_gpu_seconds": 25.41125166718848,
+ "decode_forwards_per_second": 4.862791651052793
+ }
+ ],
+ "median_forwards_per_second": 4.8595385071453165
+ },
+ "speedup": 1.1993188537323258
+}
diff --git a/docs/receipts/ds1017-attention-2026-10-19/sha256.json b/docs/receipts/ds1017-attention-2026-10-19/sha256.json
new file mode 100644
index 0000000000..6bb407eac2
--- /dev/null
+++ b/docs/receipts/ds1017-attention-2026-10-19/sha256.json
@@ -0,0 +1,6 @@
+{
+ "measurements.json": "0ab4b16161d51651d9ef2a6c851a874717eea5bd9a44a58c5c27f6fcb7602547",
+ "checks.json": "50fa615789e447ee2851d14a497e6212192774cb7d9cd857136956b06eaa7714",
+ "README.md": "bab87e9ea3ec6a0c8f112aeca9e3da6c9cbb5f21cf4b566232691d58b2de4c65",
+ "build.json": "ed34f8f9a425c985197ce5809f09aef6b9e43c6779584dce3f423b24576d5849"
+}
diff --git a/docs/receipts/ds1017-tp2-2026-10-19/README.md b/docs/receipts/ds1017-tp2-2026-10-19/README.md
new file mode 100644
index 0000000000..d1ba323229
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-2026-10-19/README.md
@@ -0,0 +1,199 @@
+# DeepSeek-V4.1-Flash-1017: two M3 Ultra Macs, TCP and SSD Engram
+
+This experimental path produces the same measured logits and tokens as the
+single-rank EXL3 engine. It is slower over TCP: 2.78 decode steps/s for two Macs,
+against 4.06 for one. It replicates the weights and attention on each Mac, so it
+also does not reduce the memory needed per Mac. These are development receipts,
+not release qualification.
+
+A later [shared-family and whole-slot comparison](../ds1017-tp2-slots-2026-10-20/README.md)
+records runtime `cdfa5203`. This folder retains the earlier `b27e39af` results.
+
+## Scope and provenance
+
+The measured runtime is `b27e39af70a101e4a964035e79335d1f0aa446de`, based on
+upstream `d46d17a784758b8a8b66badde1f728f36bdc9541`. Both Macs have an M3 Ultra,
+80 GPU cores and 256 GB unified memory. The Engram tables are on each Mac's
+internal SSD. The weight sets are resident during the qualified decode runs;
+their initial load location is outside the execution measurement.
+
+[build.json](build.json) pins Zig 0.17.0, `-Dcpu=apple_m3` and binary SHA256s.
+[model.json](model.json) pins the checkpoint and original 1017 Engram revision;
+[pack-sha256.json](pack-sha256.json) records the measured checkpoint files, including the locally generated token map.
+Do not substitute older V4.1 Engram tables: their shapes can load without
+establishing that they are the correct model.
+
+The source series includes the unmerged Metal family from #824 through #827,
+then streamed Engram, decode changes and the experimental pair. Maintainer
+choices about the shared CUDA/Metal family and whole-model gate remain open
+on #847. This branch does not resolve those choices.
+
+## Partition
+
+```mermaid
+flowchart LR
+ R[Requests] --> A[Rank 0: lane owner]
+ A <--> B[Rank 1: command follower]
+ A --> C[Full weights and attention; local SSD Engram]
+ B --> D[Full weights and attention; local SSD Engram]
+ A <--> E[TCP: concatenate MoE output columns]
+ B <--> E
+```
+
+Each rank calculates half the output columns of gate, up and down projections.
+Each dot product keeps its full input reduction. The ranks gather the output
+halves as bytes, with three exchanges per layer and one history handshake:
+121 exchanges per forward across 40 layers. This preserves the measured
+single-rank rounding order, but the synchronization costs exceed the saved
+work in this experiment. This result does not measure MCDMA or RDMA.
+
+## Model measurements
+
+| Check | Single rank | Two ranks |
+| --- | ---: | ---: |
+| 32-token prompt, 128 generated tokens, decode forwards | 127 | 127 |
+| Decode wall time | 31.2933 s | 45.6813 s |
+| Decode forwards/s | 4.0584 | 2.7801 |
+| Decode GPU time, measured rank | 30.6020 s | 33.8378 s |
+| Execution reads | 126,844,928 bytes | 126,844,928 bytes per rank |
+| Counted Engram reads | 126,844,928 bytes | 126,844,928 bytes per rank |
+| Page-ins | 0 | 0 |
+
+The runner's generation timing covers 127 decode forwards: prefill produces
+the first of the 128 completion tokens. The single-rank sample ran on rank 1;
+the two-rank timing above is rank 0. This is one runner sample per configuration,
+not a three-run median. HTTP repetitions are reported separately.
+
+[numerical.json](numerical.json) records zero logit bit disagreements on the
+268-token teacher sequence at chunks 1, 2, 3, 7, 31, 127, 128, 129, 255 and 256.
+Both ranks' logits match the prior single-rank artifact's SHA256. The 128
+completion token IDs also match. Two lane slots at context 8192, chunk 16, finish
+at positions 134/134 with zero bit disagreements against solo execution.
+These comparisons establish partition parity for those inputs, not absolute
+quality against the original checkpoint or FP64 on arbitrary inputs.
+
+[measurements.json](measurements.json) retains the execution receipts, including
+failed I/O gates. The teacher seam sweep reads about 0.5 MB beyond the counted
+Engram rows on the single-rank run and on rank 0 of the pair; those gates fail.
+Decode and paired lane gates pass on both ranks, with no page-ins. Do not treat
+a correct logit comparison as a passed residency gate.
+
+Each Mac passed 29 EXL3 GPU tests, 99 DeepSeek GPU tests and 28 reference tests.
+The chosen template matched all five 1017 encoding fixtures. The token-map
+builder's full Rust-normalizer parity, including Hangul, remains unqualified;
+see [the normalization caveat](../../engram-token-map.md).
+
+## Reproduce the runner comparisons
+
+Build the runtime and qualification binaries with full Xcode and its Metal
+Toolchain installed:
+
+```sh
+zig build native tf-dsv41-run check-deepseek-v41 check-exl3-gpu \
+ test-deepseek-v41-tp-host -Dcpu=apple_m3
+```
+
+Set `TF_DSV41_SPLIT_RESIDENCY=1` and `TF_DSV41_RESIDENCY_REFRESH=1` on both Macs.
+The measured model used 19 immutable weight residency sets, requested every
+400 ms. Execution buffers use a separate set. These options are opt-in; they
+address long pre-GPU residency stalls observed with the monolithic set.
+
+The prompt ID arrays are in [prompts](prompts/). Run the one-rank engine first,
+then use identical inputs, binaries and model manifests for both ranks. Set
+`DSV41_ENGRAM_STREAM=1`; for the pair set `TF_DSV41_TP_SETTINGS` to each rank's
+settings file. For example, with `MODEL`, `TABLES` and `RECEIPT` pointing to your local paths:
+
+```sh
+export DSV41_ENGRAM_STREAM=1
+export TF_DSV41_SPLIT_RESIDENCY=1
+export TF_DSV41_RESIDENCY_REFRESH=1
+./zig-out/bin/tf-dsv41-run seams "$MODEL" "$TABLES" "$RECEIPT/prompts/mid.json" out/seams
+./zig-out/bin/tf-dsv41-run generate "$MODEL" "$TABLES" "$RECEIPT/prompts/short.json" out/generate 128
+./zig-out/bin/tf-dsv41-run lanes "$MODEL" "$TABLES" "$RECEIPT/prompts/mid.json" out/lanes 16 8192
+```
+
+For each paired command, start both ranks with their respective
+`TF_DSV41_TP_SETTINGS` value. Use distinct output directories for single-rank
+and paired runs. Preserve `logits.f32`, token IDs and `residency.json`, including
+failed gates. The native server uses
+`--engram-stream --speed-up ` instead of the runner variables.
+See [the launch settings](../../deepseek-v41.md#experimental-two-mac-moe).
+
+## Native HTTP state and failure checks
+
+[http.json](http.json) records three sequential repetitions of three prompts,
+three complete SSE samples, and comparison against the single-rank replies on
+both Macs. It includes reasoning fields and finish reasons; it is a parity
+check, not a task-accuracy score. The same checks repeat after restarting both
+ranks.
+
+Each paired phase also sends twelve requests from two clients, observes two
+running slots through server metrics, and compares every reply with solo
+execution. Closing a stream after its first generated delta must increment the
+server's disconnect counter, drain the slots and leave the next reply equal to
+solo. Stopping rank 1 makes two subsequent leader requests return HTTP 500 with
+`restart both ranks`, in 0.127 and 0.114 seconds. Both processes then restart
+before the repeated parity phase. Both paired phases pass, with zero observer
+errors; the cancellation counters increment and queues drain in 0.528 and
+0.471 seconds respectively. A 1 GB load limit is refused cleanly on both
+Macs. The protected windows restore the pre-existing service and verify a real
+response before finishing.
+
+HTTP end-to-end medians below use three samples per prompt and include the
+local SSH tunnel. They are separate from runner decode timing:
+
+| Phase | Arithmetic prompt | Colors prompt | Multi-turn prompt |
+| --- | ---: | ---: | ---: |
+| TP1, Mac 0 | 5.967 s | 8.727 s | 9.346 s |
+| TP1, Mac 1 | 5.959 s | 8.733 s | 9.382 s |
+| TP2 | 8.341 s | 13.294 s | 13.265 s |
+| TP2 after restart | 8.406 s | 12.590 s | 13.368 s |
+
+The portable [HTTP probe](../../../tools/zig/dsv41_tp_http_probe.py) only sends
+requests and reads metrics; starting, stopping and restoring servers belongs to
+the operator. It requires servers named `DeepSeek-TP-Probe`, as used here.
+
+```sh
+python3 tools/zig/dsv41_tp_http_probe.py baseline http://127.0.0.1:8099 out/http-tp1
+python3 tools/zig/dsv41_tp_http_probe.py parity http://127.0.0.1:8099 out/http-tp2 \
+ --baseline out/http-tp1 --follower http://192.0.2.11:8099
+```
+
+Use fresh output directories. Run the second command only after both paired
+servers are ready. The peer-loss mode checks an already failed pair; it does
+not stop the peer itself.
+
+## Limits before merge or release
+
+- Text and greedy decoding only. Drafts/MTP, vision, grammar masks and tree
+ requests are outside this qualification.
+- Full weights are replicated, about 202.37 GB staged and 211 GB process
+ footprint per Mac. These are snapshots, not a sampled peak. There is no
+ memory-capacity scaling claim.
+- The longest numerical prompt here is 268 tokens. Long-context cold prompts,
+ the release context matrix and another-engine comparisons have not run.
+- The new family kernels still need kernel-contract registration and backend
+ `SUMMARY` receipts. Passing the existing registered-launch host test does
+ not cover unregistered kernels.
+- [local-checks.json](local-checks.json) records the local CI results. Linux
+ no-GPU commands, format and lean pass. The app test suite fails: one sandbox
+ Git test also fails on unchanged upstream, and one settings test timed out
+ only on the candidate run. App typecheck and mobile validation pass. Windows
+ and actual PR CI remain separate evidence.
+- TCP session IDs prevent accidental pairing; they provide no authentication
+ or encryption. Use a trusted private link. A failed pair requires both
+ processes to restart.
+
+## Related implementations
+
+The design investigation considered [MLX-LM's DeepSeek implementation](https://github.com/ml-explore/mlx-lm/blob/93e6d4a6447b3080861985947f0820e76fcb957d/mlx_lm/models/deepseek_v41.py),
+[MLX distributed operations](https://github.com/ml-explore/mlx/blob/b6642e604843776705b57b1eaecb6f57e519b793/docs/src/usage/distributed.rst),
+[DwarfStar distributed inference](https://github.com/antirez/ds4/blob/fc80bd695da76ee14bcb86820bf900a5cdfda806/docs/DISTRIBUTED.md)
+and [exo's automatic partitioning](https://github.com/exo-explore/exo/blob/bc412e2230f395e652c23426a5eb88dfa515769b/src/exo/worker/engines/mlx/auto_parallel.py). These informed the investigation; their
+performance is not measured by this receipt.
+
+The residency options follow two independently published MLX changes:
+[split residency sets](https://github.com/ml-explore/mlx/pull/4211) and
+[periodic residency requests](https://github.com/ml-explore/mlx/pull/4633).
+The local before/after traces place the eliminated multi-second delay before
+GPU execution, rather than in a delayed completion callback.
diff --git a/docs/receipts/ds1017-tp2-2026-10-19/build.json b/docs/receipts/ds1017-tp2-2026-10-19/build.json
new file mode 100644
index 0000000000..043950831f
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-2026-10-19/build.json
@@ -0,0 +1,15 @@
+{
+ "commit": "b27e39af70a101e4a964035e79335d1f0aa446de",
+ "upstream": "d46d17a784758b8a8b66badde1f728f36bdc9541",
+ "compiler": "0.17.0",
+ "cpu": "apple_m3",
+ "build_steps": 72,
+ "host_tests": 43,
+ "binary_sha256": {
+ "tf-dsv41-run.b27e39af": "d4ec1f2c29c157fd2bc2ee1a54e55b7257302d25ff96994906076a05bd79bb88",
+ "tf-dsv41-gpu.b27e39af": "91a6e7653aa29356352e4d9388bacae70ce08255f9e5aa6bd67ba8087a7e0efa",
+ "tf-dsv41-reference.b27e39af": "0bfced5516402a1858b6bcf04b86f37e0dd8e580b0c48c2e23bef915109ddaca",
+ "tf-exl3-gpu.b27e39af": "28d5d2bc6b6c060d2e54c2c1840a7f055a3d2753e2fe4846596e0e0e7d70a5d3",
+ "tensorfold-native.b27e39af": "d36f40b0bff9d6c62728909826ceca1da3457df7db8e68569c6a19575c396798"
+ }
+}
diff --git a/docs/receipts/ds1017-tp2-2026-10-19/http.json b/docs/receipts/ds1017-tp2-2026-10-19/http.json
new file mode 100644
index 0000000000..f627ac3d69
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-2026-10-19/http.json
@@ -0,0 +1,804 @@
+{
+ "origin": "actual native HTTP on the measured Macs",
+ "runtime_commit": "b27e39af70a101e4a964035e79335d1f0aa446de",
+ "context": 8192,
+ "parallel": 2,
+ "max_completion_tokens": 32,
+ "temperature": 0,
+ "prompt_cache_gib": 0,
+ "drafts": false,
+ "phases": {
+ "baseline-rank0": {
+ "state": "passed",
+ "scope": [
+ "solo-0",
+ "solo-1",
+ "solo-2",
+ "repeat-0-stream",
+ "repeat-1-solo-0",
+ "repeat-1-solo-1",
+ "repeat-1-solo-2",
+ "repeat-1-stream",
+ "repeat-2-solo-0",
+ "repeat-2-solo-1",
+ "repeat-2-solo-2",
+ "repeat-2-stream",
+ "benchmark",
+ "expected"
+ ],
+ "requests": [
+ {
+ "repeat": 0,
+ "prompt": 0,
+ "seconds": 5.966519,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 20,
+ "total_tokens": 64,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 18
+ }
+ }
+ },
+ {
+ "repeat": 0,
+ "prompt": 1,
+ "seconds": 8.7132455,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 32,
+ "total_tokens": 76,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 32
+ }
+ }
+ },
+ {
+ "repeat": 0,
+ "prompt": 2,
+ "seconds": 9.345698041999999,
+ "usage": {
+ "prompt_tokens": 59,
+ "completion_tokens": 32,
+ "total_tokens": 91,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 29
+ }
+ }
+ },
+ {
+ "repeat": 1,
+ "prompt": 0,
+ "seconds": 5.992858540999997,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 20,
+ "total_tokens": 64,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 18
+ }
+ }
+ },
+ {
+ "repeat": 1,
+ "prompt": 1,
+ "seconds": 8.726675125,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 32,
+ "total_tokens": 76,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 32
+ }
+ }
+ },
+ {
+ "repeat": 1,
+ "prompt": 2,
+ "seconds": 9.344452875000002,
+ "usage": {
+ "prompt_tokens": 59,
+ "completion_tokens": 32,
+ "total_tokens": 91,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 29
+ }
+ }
+ },
+ {
+ "repeat": 2,
+ "prompt": 0,
+ "seconds": 5.9545363750000035,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 20,
+ "total_tokens": 64,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 18
+ }
+ }
+ },
+ {
+ "repeat": 2,
+ "prompt": 1,
+ "seconds": 8.748370832999996,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 32,
+ "total_tokens": 76,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 32
+ }
+ }
+ },
+ {
+ "repeat": 2,
+ "prompt": 2,
+ "seconds": 9.350264042000006,
+ "usage": {
+ "prompt_tokens": 59,
+ "completion_tokens": 32,
+ "total_tokens": 91,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 29
+ }
+ }
+ }
+ ],
+ "median_seconds_by_prompt": {
+ "0": 5.966519,
+ "1": 8.726675125,
+ "2": 9.345698041999999
+ },
+ "stream_first_delta_seconds": [
+ 1.7641005000000014,
+ 1.7708203339999997,
+ 1.7772273749999954
+ ]
+ },
+ "baseline-rank1": {
+ "state": "passed",
+ "scope": [
+ "solo-0",
+ "solo-1",
+ "solo-2",
+ "repeat-0-stream",
+ "repeat-1-solo-0",
+ "repeat-1-solo-1",
+ "repeat-1-solo-2",
+ "repeat-1-stream",
+ "repeat-2-solo-0",
+ "repeat-2-solo-1",
+ "repeat-2-solo-2",
+ "repeat-2-stream",
+ "benchmark",
+ "expected"
+ ],
+ "requests": [
+ {
+ "repeat": 0,
+ "prompt": 0,
+ "seconds": 6.029741417,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 20,
+ "total_tokens": 64,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 18
+ }
+ }
+ },
+ {
+ "repeat": 0,
+ "prompt": 1,
+ "seconds": 8.727650833000002,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 32,
+ "total_tokens": 76,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 32
+ }
+ }
+ },
+ {
+ "repeat": 0,
+ "prompt": 2,
+ "seconds": 9.381836666999998,
+ "usage": {
+ "prompt_tokens": 59,
+ "completion_tokens": 32,
+ "total_tokens": 91,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 29
+ }
+ }
+ },
+ {
+ "repeat": 1,
+ "prompt": 0,
+ "seconds": 5.9588062499999985,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 20,
+ "total_tokens": 64,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 18
+ }
+ }
+ },
+ {
+ "repeat": 1,
+ "prompt": 1,
+ "seconds": 8.741661999999998,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 32,
+ "total_tokens": 76,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 32
+ }
+ }
+ },
+ {
+ "repeat": 1,
+ "prompt": 2,
+ "seconds": 9.386317249999998,
+ "usage": {
+ "prompt_tokens": 59,
+ "completion_tokens": 32,
+ "total_tokens": 91,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 29
+ }
+ }
+ },
+ {
+ "repeat": 2,
+ "prompt": 0,
+ "seconds": 5.944791291999998,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 20,
+ "total_tokens": 64,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 18
+ }
+ }
+ },
+ {
+ "repeat": 2,
+ "prompt": 1,
+ "seconds": 8.732845458,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 32,
+ "total_tokens": 76,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 32
+ }
+ }
+ },
+ {
+ "repeat": 2,
+ "prompt": 2,
+ "seconds": 9.365897707999991,
+ "usage": {
+ "prompt_tokens": 59,
+ "completion_tokens": 32,
+ "total_tokens": 91,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 29
+ }
+ }
+ }
+ ],
+ "median_seconds_by_prompt": {
+ "0": 5.9588062499999985,
+ "1": 8.732845458,
+ "2": 9.381836666999998
+ },
+ "stream_first_delta_seconds": [
+ 1.767202832999999,
+ 1.768460832999999,
+ 1.7736275830000068
+ ]
+ },
+ "paired": {
+ "state": "passed",
+ "scope": [
+ "follower-refusal",
+ "solo-0",
+ "solo-1",
+ "solo-2",
+ "repeat-0-stream",
+ "repeat-1-solo-0",
+ "repeat-1-solo-1",
+ "repeat-1-solo-2",
+ "repeat-1-stream",
+ "repeat-2-solo-0",
+ "repeat-2-solo-1",
+ "repeat-2-solo-2",
+ "repeat-2-stream",
+ "benchmark",
+ "expected",
+ "concurrent-0",
+ "concurrent-1",
+ "concurrent-2",
+ "concurrent-3",
+ "concurrent-4",
+ "concurrent-5",
+ "concurrent-6",
+ "concurrent-7",
+ "concurrent-8",
+ "concurrent-9",
+ "concurrent-10",
+ "concurrent-11",
+ "concurrency-observation",
+ "stream",
+ "cancelled-stream",
+ "after-cancelled-stream"
+ ],
+ "requests": [
+ {
+ "repeat": 0,
+ "prompt": 0,
+ "seconds": 8.390127625,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 20,
+ "total_tokens": 64,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 18
+ }
+ }
+ },
+ {
+ "repeat": 0,
+ "prompt": 1,
+ "seconds": 12.538437833,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 32,
+ "total_tokens": 76,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 32
+ }
+ }
+ },
+ {
+ "repeat": 0,
+ "prompt": 2,
+ "seconds": 13.265186624999998,
+ "usage": {
+ "prompt_tokens": 59,
+ "completion_tokens": 32,
+ "total_tokens": 91,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 29
+ }
+ }
+ },
+ {
+ "repeat": 1,
+ "prompt": 0,
+ "seconds": 8.308392250000004,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 20,
+ "total_tokens": 64,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 18
+ }
+ }
+ },
+ {
+ "repeat": 1,
+ "prompt": 1,
+ "seconds": 13.856492583000005,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 32,
+ "total_tokens": 76,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 32
+ }
+ }
+ },
+ {
+ "repeat": 1,
+ "prompt": 2,
+ "seconds": 13.193924041999992,
+ "usage": {
+ "prompt_tokens": 59,
+ "completion_tokens": 32,
+ "total_tokens": 91,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 29
+ }
+ }
+ },
+ {
+ "repeat": 2,
+ "prompt": 0,
+ "seconds": 8.340855000000005,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 20,
+ "total_tokens": 64,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 18
+ }
+ }
+ },
+ {
+ "repeat": 2,
+ "prompt": 1,
+ "seconds": 13.294250957999992,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 32,
+ "total_tokens": 76,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 32
+ }
+ }
+ },
+ {
+ "repeat": 2,
+ "prompt": 2,
+ "seconds": 13.297439124999997,
+ "usage": {
+ "prompt_tokens": 59,
+ "completion_tokens": 32,
+ "total_tokens": 91,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 29
+ }
+ }
+ }
+ ],
+ "median_seconds_by_prompt": {
+ "0": 8.340855000000005,
+ "1": 13.294250957999992,
+ "2": 13.265186624999998
+ },
+ "stream_first_delta_seconds": [
+ 1.908659041,
+ 1.8991380000000078,
+ 1.9180917499999879
+ ],
+ "concurrency": {
+ "requests": 12,
+ "clients": 2,
+ "metric_samples": 445,
+ "max_running": 2.0,
+ "observation_errors": []
+ },
+ "cancellation": {
+ "after_first_nonempty_delta": true,
+ "observed_by_server": true,
+ "disconnect_counter_before": 0.0,
+ "disconnect_counter_after": 1.0,
+ "running_after": 0.0,
+ "waiting_after": 0.0,
+ "drain_seconds": 0.527957707999974,
+ "following_reply_matches_solo": true
+ }
+ },
+ "restarted": {
+ "state": "passed",
+ "scope": [
+ "follower-refusal",
+ "solo-0",
+ "solo-1",
+ "solo-2",
+ "repeat-0-stream",
+ "repeat-1-solo-0",
+ "repeat-1-solo-1",
+ "repeat-1-solo-2",
+ "repeat-1-stream",
+ "repeat-2-solo-0",
+ "repeat-2-solo-1",
+ "repeat-2-solo-2",
+ "repeat-2-stream",
+ "benchmark",
+ "expected",
+ "concurrent-0",
+ "concurrent-1",
+ "concurrent-2",
+ "concurrent-3",
+ "concurrent-4",
+ "concurrent-5",
+ "concurrent-6",
+ "concurrent-7",
+ "concurrent-8",
+ "concurrent-9",
+ "concurrent-10",
+ "concurrent-11",
+ "concurrency-observation",
+ "stream",
+ "cancelled-stream",
+ "after-cancelled-stream"
+ ],
+ "requests": [
+ {
+ "repeat": 0,
+ "prompt": 0,
+ "seconds": 8.354224917000002,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 20,
+ "total_tokens": 64,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 18
+ }
+ }
+ },
+ {
+ "repeat": 0,
+ "prompt": 1,
+ "seconds": 12.757398833,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 32,
+ "total_tokens": 76,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 32
+ }
+ }
+ },
+ {
+ "repeat": 0,
+ "prompt": 2,
+ "seconds": 13.368066333999998,
+ "usage": {
+ "prompt_tokens": 59,
+ "completion_tokens": 32,
+ "total_tokens": 91,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 29
+ }
+ }
+ },
+ {
+ "repeat": 1,
+ "prompt": 0,
+ "seconds": 8.405893124999999,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 20,
+ "total_tokens": 64,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 18
+ }
+ }
+ },
+ {
+ "repeat": 1,
+ "prompt": 1,
+ "seconds": 12.512019207999998,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 32,
+ "total_tokens": 76,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 32
+ }
+ }
+ },
+ {
+ "repeat": 1,
+ "prompt": 2,
+ "seconds": 14.205145583999993,
+ "usage": {
+ "prompt_tokens": 59,
+ "completion_tokens": 32,
+ "total_tokens": 91,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 29
+ }
+ }
+ },
+ {
+ "repeat": 2,
+ "prompt": 0,
+ "seconds": 8.536594042000004,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 20,
+ "total_tokens": 64,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 18
+ }
+ }
+ },
+ {
+ "repeat": 2,
+ "prompt": 1,
+ "seconds": 12.590157125000005,
+ "usage": {
+ "prompt_tokens": 44,
+ "completion_tokens": 32,
+ "total_tokens": 76,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 32
+ }
+ }
+ },
+ {
+ "repeat": 2,
+ "prompt": 2,
+ "seconds": 13.306821750000012,
+ "usage": {
+ "prompt_tokens": 59,
+ "completion_tokens": 32,
+ "total_tokens": 91,
+ "prompt_tokens_details": {
+ "cached_tokens": 0
+ },
+ "completion_tokens_details": {
+ "reasoning_tokens": 29
+ }
+ }
+ }
+ ],
+ "median_seconds_by_prompt": {
+ "0": 8.405893124999999,
+ "1": 12.590157125000005,
+ "2": 13.368066333999998
+ },
+ "stream_first_delta_seconds": [
+ 1.915597708,
+ 2.1023658749999896,
+ 1.9103987920000094
+ ],
+ "concurrency": {
+ "requests": 12,
+ "clients": 2,
+ "metric_samples": 369,
+ "max_running": 2.0,
+ "observation_errors": []
+ },
+ "cancellation": {
+ "after_first_nonempty_delta": true,
+ "observed_by_server": true,
+ "disconnect_counter_before": 0.0,
+ "disconnect_counter_after": 1.0,
+ "running_after": 0.0,
+ "waiting_after": 0.0,
+ "drain_seconds": 0.4711948750000374,
+ "following_reply_matches_solo": true
+ }
+ }
+ },
+ "peer_loss": [
+ {
+ "status": 500,
+ "seconds": 0.127464291,
+ "body": {
+ "error": {
+ "message": "the DeepSeek TP pair lost synchronization; restart both ranks"
+ }
+ }
+ },
+ {
+ "status": 500,
+ "seconds": 0.11438445800000002,
+ "body": {
+ "error": {
+ "message": "the DeepSeek TP pair lost synchronization; restart both ranks"
+ }
+ }
+ }
+ ],
+ "production_restored": [
+ true,
+ true
+ ]
+}
diff --git a/docs/receipts/ds1017-tp2-2026-10-19/local-checks.json b/docs/receipts/ds1017-tp2-2026-10-19/local-checks.json
new file mode 100644
index 0000000000..5a99142a0f
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-2026-10-19/local-checks.json
@@ -0,0 +1,105 @@
+{
+ "linux": {
+ "commit": "b27e39af70a101e4a964035e79335d1f0aa446de",
+ "base": "d46d17a784758b8a8b66badde1f728f36bdc9541",
+ "platform": "Linux-6.8.0-117-generic-aarch64-with-glibc2.39",
+ "zig": "0.17.0",
+ "results": [
+ {
+ "command": [
+ "zig",
+ "build",
+ "test"
+ ],
+ "exit_code": 0,
+ "seconds": 1.8468520270000006,
+ "log": "0-zig_build_test.log"
+ },
+ {
+ "command": [
+ "zig",
+ "build",
+ "test-kernel-contract"
+ ],
+ "exit_code": 0,
+ "seconds": 11.017747375999999,
+ "log": "1-zig_build_test-kernel-contract.log"
+ },
+ {
+ "command": [
+ "zig",
+ "build",
+ "test-golden"
+ ],
+ "exit_code": 0,
+ "seconds": 69.696737998,
+ "log": "2-zig_build_test-golden.log"
+ },
+ {
+ "command": [
+ "zig",
+ "build",
+ "fuzz-cortheon"
+ ],
+ "exit_code": 0,
+ "seconds": 0.06515243200000498,
+ "log": "3-zig_build_fuzz-cortheon.log"
+ },
+ {
+ "command": [
+ "python3",
+ "-m",
+ "unittest",
+ "discover",
+ "-s",
+ "tools/zig",
+ "-p",
+ "flashnext_batch_serve_test.py"
+ ],
+ "exit_code": 0,
+ "seconds": 0.1183854340000039,
+ "log": "4-python3_-m_unittest_discover_-s_tools_zig_-p_flashnext_batch_serve_test.py.log"
+ }
+ ],
+ "running": null,
+ "state": "complete"
+ },
+ "zig_fmt": "passed: zig fmt --check build.zig zig, Zig 0.17.0",
+ "lean": {
+ "base_findings": 503,
+ "head_findings": 503,
+ "new_findings": 0
+ },
+ "app": {
+ "node": "22.23.2",
+ "npm": "10.9.8",
+ "candidate_test_files_passed": 815,
+ "candidate_test_files_total": 817,
+ "candidate_failures": [
+ "electron/agent/sandbox-git-control.test.ts: git commit still works",
+ "scripts/settings-main-transaction.test.mjs: concurrent consumer timed out (5000 ms)"
+ ],
+ "unchanged_base_test_files_passed": 816,
+ "unchanged_base_test_files_total": 817,
+ "unchanged_base_failure": "electron/agent/sandbox-git-control.test.ts: git commit still works",
+ "settings_timeout_reproduced_on_base": false,
+ "typecheck": "passed",
+ "verify_mobile": "passed"
+ },
+ "not_run": [
+ "Windows workflow",
+ "new-family kernel contract registration/SUMMARY",
+ "long-context release matrix",
+ "another-engine comparison",
+ "sampled peak memory trace; footprints are snapshots"
+ ],
+ "portable_http_probe_controls": {
+ "passed": 3,
+ "scope": [
+ "complete repeated replies pass",
+ "changed repeated reply fails",
+ "SSE without DONE fails"
+ ],
+ "origin": "synthetic HTTP fixture; not model-performance evidence"
+ }
+}
diff --git a/docs/receipts/ds1017-tp2-2026-10-19/measurements.json b/docs/receipts/ds1017-tp2-2026-10-19/measurements.json
new file mode 100644
index 0000000000..7a4a8eb0d8
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-2026-10-19/measurements.json
@@ -0,0 +1,821 @@
+[
+ {
+ "rank": 0,
+ "phase": "tp2-seams",
+ "rows": [
+ {
+ "mode": "seam",
+ "chunk": 1,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 2,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 3,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 7,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 31,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 127,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 128,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 129,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 255,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 256,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 9.627040916704573,
+ "gpu_seconds": 8.661569456453435
+ },
+ {
+ "mode": "seams",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "tensor_parallel_exchanges": 67155,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 992,
+ "ready_footprint_bytes": 210978803960,
+ "final_footprint_bytes": 211544248880,
+ "load_seconds": 430.38542404177133,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 2258120704,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 2257240064,
+ "engram_rows_requested": 141504,
+ "engram_rows_unique": 135664,
+ "engram_cache_hits": 0,
+ "engram_reads": 135655,
+ "engram_wait_seconds": 2.559368,
+ "resident_gate_passed": false,
+ "execution_error": null
+ }
+ ],
+ "residency": {
+ "mode": "seams",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "tensor_parallel_exchanges": 67155,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 992,
+ "ready_footprint_bytes": 210978803960,
+ "final_footprint_bytes": 211544248880,
+ "load_seconds": 430.38542404177133,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 2258120704,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 2257240064,
+ "engram_rows_requested": 141504,
+ "engram_rows_unique": 135664,
+ "engram_cache_hits": 0,
+ "engram_reads": 135655,
+ "engram_wait_seconds": 2.559368,
+ "resident_gate_passed": false,
+ "execution_error": null
+ }
+ },
+ {
+ "rank": 0,
+ "phase": "tp2-gen",
+ "rows": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.4910251667024568,
+ "footprint_bytes": 211352047840,
+ "disk_read_bytes": 25542656,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.4910847500432283,
+ "decode_seconds": 45.68133983341977,
+ "decode_gpu_seconds": 33.83778276015073
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "tensor_parallel_exchanges": 15488,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 115,
+ "ready_footprint_bytes": 210978590848,
+ "final_footprint_bytes": 211352031456,
+ "load_seconds": 321.80389829166234,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 126844928,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.417131,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "tensor_parallel_exchanges": 15488,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 115,
+ "ready_footprint_bytes": 210978590848,
+ "final_footprint_bytes": 211352031456,
+ "load_seconds": 321.80389829166234,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 126844928,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.417131,
+ "resident_gate_passed": true,
+ "execution_error": null
+ },
+ "decode_steps": 127,
+ "decode_steps_per_second": 2.7801286140711823
+ },
+ {
+ "rank": 0,
+ "phase": "tp2-lanes",
+ "rows": [
+ {
+ "mode": "lanes",
+ "slots": 2,
+ "chunk": 16,
+ "max_logit_error_slot0": 0,
+ "max_logit_error_slot1": 0,
+ "bit_disagreements": 0,
+ "position_slot0": 134,
+ "position_slot1": 134,
+ "gpu_seconds": 14.133
+ },
+ {
+ "mode": "lanes",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "tensor_parallel_exchanges": 4356,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 8192,
+ "consumed_tokens": 134,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 202796515328,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 81,
+ "ready_footprint_bytes": 209532686248,
+ "final_footprint_bytes": 210046767208,
+ "load_seconds": 322.47598645836115,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 414777344,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 414777344,
+ "engram_rows_requested": 25728,
+ "engram_rows_unique": 24928,
+ "engram_cache_hits": 0,
+ "engram_reads": 24928,
+ "engram_wait_seconds": 0.29364,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "residency": {
+ "mode": "lanes",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "tensor_parallel_exchanges": 4356,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 8192,
+ "consumed_tokens": 134,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 202796515328,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 81,
+ "ready_footprint_bytes": 209532686248,
+ "final_footprint_bytes": 210046767208,
+ "load_seconds": 322.47598645836115,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 414777344,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 414777344,
+ "engram_rows_requested": 25728,
+ "engram_rows_unique": 24928,
+ "engram_cache_hits": 0,
+ "engram_reads": 24928,
+ "engram_wait_seconds": 0.29364,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ {
+ "rank": 1,
+ "phase": "tp1-seams",
+ "rows": [
+ {
+ "mode": "seam",
+ "chunk": 1,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 2,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 3,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 7,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 31,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 127,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 128,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 129,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 255,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 256,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 7.406163791660219,
+ "gpu_seconds": 7.2561467081541196
+ },
+ {
+ "mode": "seams",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 567,
+ "ready_footprint_bytes": 210974068936,
+ "final_footprint_bytes": 211540201960,
+ "load_seconds": 53.96257720841095,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 2257760256,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 2257240064,
+ "engram_rows_requested": 141504,
+ "engram_rows_unique": 135664,
+ "engram_cache_hits": 0,
+ "engram_reads": 135655,
+ "engram_wait_seconds": 2.61761,
+ "resident_gate_passed": false,
+ "execution_error": null
+ }
+ ],
+ "residency": {
+ "mode": "seams",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 567,
+ "ready_footprint_bytes": 210974068936,
+ "final_footprint_bytes": 211540201960,
+ "load_seconds": 53.96257720841095,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 2257760256,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 2257240064,
+ "engram_rows_requested": 141504,
+ "engram_rows_unique": 135664,
+ "engram_cache_hits": 0,
+ "engram_reads": 135655,
+ "engram_wait_seconds": 2.61761,
+ "resident_gate_passed": false,
+ "execution_error": null
+ }
+ },
+ {
+ "rank": 1,
+ "phase": "tp1-gen",
+ "rows": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.3244472916703671,
+ "footprint_bytes": 211363532976,
+ "disk_read_bytes": 25542656,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3245290000922978,
+ "decode_seconds": 31.29328745836392,
+ "decode_gpu_seconds": 30.601965417037718
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 79,
+ "ready_footprint_bytes": 210973986968,
+ "final_footprint_bytes": 211357110472,
+ "load_seconds": 53.68323045829311,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 126844928,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.421519,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 79,
+ "ready_footprint_bytes": 210973986968,
+ "final_footprint_bytes": 211357110472,
+ "load_seconds": 53.68323045829311,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 126844928,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.421519,
+ "resident_gate_passed": true,
+ "execution_error": null
+ },
+ "decode_steps": 127,
+ "decode_steps_per_second": 4.058378339731004
+ },
+ {
+ "rank": 1,
+ "phase": "tp2-seams",
+ "rows": [
+ {
+ "mode": "seam",
+ "chunk": 1,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 2,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 3,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 7,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 31,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 127,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 128,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 129,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 255,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "seam",
+ "chunk": 256,
+ "max_logit_error": 0,
+ "bit_disagreements": 0,
+ "greedy_disagreements": 0
+ },
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 9.68476324994117,
+ "gpu_seconds": 8.666550709400326
+ },
+ {
+ "mode": "seams",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "tensor_parallel_exchanges": 67155,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 740,
+ "ready_footprint_bytes": 210978853064,
+ "final_footprint_bytes": 211544199656,
+ "load_seconds": 53.7666174583137,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 2257240064,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 2257240064,
+ "engram_rows_requested": 141504,
+ "engram_rows_unique": 135664,
+ "engram_cache_hits": 0,
+ "engram_reads": 135655,
+ "engram_wait_seconds": 2.654514,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "residency": {
+ "mode": "seams",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "tensor_parallel_exchanges": 67155,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 740,
+ "ready_footprint_bytes": 210978853064,
+ "final_footprint_bytes": 211544199656,
+ "load_seconds": 53.7666174583137,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 2257240064,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 2257240064,
+ "engram_rows_requested": 141504,
+ "engram_rows_unique": 135664,
+ "engram_cache_hits": 0,
+ "engram_reads": 135655,
+ "engram_wait_seconds": 2.654514,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ {
+ "rank": 1,
+ "phase": "tp2-gen",
+ "rows": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.432881791726686,
+ "footprint_bytes": 211352326392,
+ "disk_read_bytes": 25542656,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.432960041682236,
+ "decode_seconds": 45.681218666723,
+ "decode_gpu_seconds": 33.89711845864076
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "tensor_parallel_exchanges": 15488,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 775,
+ "ready_footprint_bytes": 210978885808,
+ "final_footprint_bytes": 211352228088,
+ "load_seconds": 322.0678685416933,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 126844928,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.461678,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "tensor_parallel_exchanges": 15488,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 775,
+ "ready_footprint_bytes": 210978885808,
+ "final_footprint_bytes": 211352228088,
+ "load_seconds": 322.0678685416933,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 126844928,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.461678,
+ "resident_gate_passed": true,
+ "execution_error": null
+ },
+ "decode_steps": 127,
+ "decode_steps_per_second": 2.780135988195836
+ },
+ {
+ "rank": 1,
+ "phase": "tp2-lanes",
+ "rows": [
+ {
+ "mode": "lanes",
+ "slots": 2,
+ "chunk": 16,
+ "max_logit_error_slot0": 0,
+ "max_logit_error_slot1": 0,
+ "bit_disagreements": 0,
+ "position_slot0": 134,
+ "position_slot1": 134,
+ "gpu_seconds": 14.136
+ },
+ {
+ "mode": "lanes",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "tensor_parallel_exchanges": 4356,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 8192,
+ "consumed_tokens": 134,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 202796515328,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 744,
+ "ready_footprint_bytes": 209532882832,
+ "final_footprint_bytes": 210046963792,
+ "load_seconds": 322.35159287496936,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 414777344,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 414777344,
+ "engram_rows_requested": 25728,
+ "engram_rows_unique": 24928,
+ "engram_cache_hits": 0,
+ "engram_reads": 24928,
+ "engram_wait_seconds": 0.305421,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "residency": {
+ "mode": "lanes",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "tensor_parallel_exchanges": 4356,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 8192,
+ "consumed_tokens": 134,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 202796515328,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 744,
+ "ready_footprint_bytes": 209532882832,
+ "final_footprint_bytes": 210046963792,
+ "load_seconds": 322.35159287496936,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 414777344,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 414777344,
+ "engram_rows_requested": 25728,
+ "engram_rows_unique": 24928,
+ "engram_cache_hits": 0,
+ "engram_reads": 24928,
+ "engram_wait_seconds": 0.305421,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ }
+]
diff --git a/docs/receipts/ds1017-tp2-2026-10-19/model.json b/docs/receipts/ds1017-tp2-2026-10-19/model.json
new file mode 100644
index 0000000000..9cf89362cc
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-2026-10-19/model.json
@@ -0,0 +1,38 @@
+{
+ "checkpoint": {
+ "repository": "LibertAIDAI/DeepSeek-V4.1-Flash-1017-EXL3-2.9bpw",
+ "revision": "afc7aa0aab98ee735c0f54e0a122019af1ac08f1"
+ },
+ "engram": {
+ "repository": "deepseek-ai/DeepSeek-V4.1-Flash-1017",
+ "revision": "1bae0e1088993b9b82de08f59de5be5a34e42170",
+ "files": {
+ "model-00047-of-00048.safetensors": "76a05106ece259be83d1bfea0dcb54f23377864ee60fd321690f35e6698a2af5",
+ "model-00048-of-00048.safetensors": "9dc51433e195bc78b5ad846b68643cc0351d3b174e5e4309222d3b7402c83afd"
+ }
+ },
+ "hardware": {
+ "rank0": {
+ "chip": "Apple M3 Ultra",
+ "gpu_cores": 80,
+ "unified_memory_gb": 256
+ },
+ "rank1": {
+ "chip": "Apple M3 Ultra",
+ "gpu_cores": 80,
+ "unified_memory_gb": 256
+ },
+ "transport": "TCP over Thunderbolt 5 IP link; no RDMA"
+ },
+ "environment": {
+ "TF_DSV41_SPLIT_RESIDENCY": "1",
+ "TF_DSV41_RESIDENCY_REFRESH": "1",
+ "DSV41_ENGRAM_STREAM": "1"
+ },
+ "token_map": {
+ "file": "engram-token-map.bin",
+ "size": 517680,
+ "sha256": "b9a7cf9d341f5daf8fc435dbc36153bf4b28c1b8075be20de30d0556fb910511",
+ "provenance": "generated with this branch; full Rust normalizer parity remains unqualified"
+ }
+}
diff --git a/docs/receipts/ds1017-tp2-2026-10-19/numerical.json b/docs/receipts/ds1017-tp2-2026-10-19/numerical.json
new file mode 100644
index 0000000000..c2b289eb26
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-2026-10-19/numerical.json
@@ -0,0 +1,40 @@
+{
+ "scope": "Comparisons with the same-pack single-rank engine, not a new whole-model FP64 qualification",
+ "teacher_tokens": 268,
+ "chunks": [
+ 1,
+ 2,
+ 3,
+ 7,
+ 31,
+ 127,
+ 128,
+ 129,
+ 255,
+ 256
+ ],
+ "bit_disagreements": 0,
+ "logits_f32_sha256": "910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2",
+ "generated_tokens": 128,
+ "generated_tokens_match_previous_single_rank": true,
+ "lanes": {
+ "slots": 2,
+ "context": 8192,
+ "chunk": 16,
+ "positions": [
+ 134,
+ 134
+ ],
+ "bit_disagreements": 0
+ },
+ "tests_per_mac": {
+ "exl3_gpu": 29,
+ "deepseek_gpu": 99,
+ "reference": 28
+ },
+ "template": {
+ "goldens_equal": 5,
+ "goldens_total": 5,
+ "sha256": "d959d804d5101b79a49b6ff1bf3c54cd5affa9a5a78c503d925f3090040b31c3"
+ }
+}
diff --git a/docs/receipts/ds1017-tp2-2026-10-19/pack-sha256.json b/docs/receipts/ds1017-tp2-2026-10-19/pack-sha256.json
new file mode 100644
index 0000000000..fd730f0b70
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-2026-10-19/pack-sha256.json
@@ -0,0 +1,270 @@
+{
+ ".gitattributes": {
+ "size": 1697,
+ "sha256": "5c43150d6197107ff8d2b87f76f9f88b7a1a6ab1af968d7828ab33a62e9d06f4"
+ },
+ "LICENSE": {
+ "size": 1084,
+ "sha256": "f2c6c602815669d292889e5be8c802f2ed950653b77999b1584e8e6aed25d040"
+ },
+ "README.md": {
+ "size": 8284,
+ "sha256": "f496a76f1667b96662cdb889dd1f46bf9aafd90a9a0d0ad5db93ab6321be670b"
+ },
+ "assets/card.png": {
+ "size": 251794,
+ "sha256": "6f8f9046d169a4e86643cd983c6ffa60e0edd93ea2f02be7680309c303c8aa25"
+ },
+ "config.json": {
+ "size": 3628,
+ "sha256": "eb48f2216645b36f400036fa2d67d8ad6b48233b0f4a1bcb7e99fa6450dad253"
+ },
+ "encoding/README.md": {
+ "size": 12164,
+ "sha256": "a34a5ca98c65e5b5b2bedd693cc2e18e732fe50192723fb6937ae07219c23739"
+ },
+ "encoding/encoding.py": {
+ "size": 37962,
+ "sha256": "13231c592e958fb39e2af6001dae69d90e7a36b265702b9b390236fb387599e2"
+ },
+ "encoding/test_encoding.py": {
+ "size": 22449,
+ "sha256": "288b79c9ea7d018ae03b0dd3e54a8220cc3a2df1ff97f59907e75ed8112765d6"
+ },
+ "encoding/tests/test_input_1.json": {
+ "size": 2761,
+ "sha256": "6a5a86694e47169c8f0c9243a7e24cc5db05447be0d3d3a9891034e913c518de"
+ },
+ "encoding/tests/test_input_2.json": {
+ "size": 527,
+ "sha256": "fd985cda88e8640b8e2404d8e659f7006cad204eb41f73c9e6662b1f218c01aa"
+ },
+ "encoding/tests/test_input_3.json": {
+ "size": 2554,
+ "sha256": "4207613f15c79b95ad92074bdb8b2b81c5ce9cfdf15b7a67f58ded6bb3ec4fa5"
+ },
+ "encoding/tests/test_input_4.json": {
+ "size": 712,
+ "sha256": "4c3890a7bc042ec6396d98d024a39a73cdcffbe1d0253e852da0c35e65296f3e"
+ },
+ "encoding/tests/test_input_5.json": {
+ "size": 1143,
+ "sha256": "88289b0aac8d650aa55c4ffeb392818fff3a0ac83c2293ddfd90546804d5e5c4"
+ },
+ "encoding/tests/test_output_1.txt": {
+ "size": 2476,
+ "sha256": "91f79aa955717b414a82aabfb38bcd83ab5a998bd47db89d2a26338e8d3de550"
+ },
+ "encoding/tests/test_output_2.txt": {
+ "size": 294,
+ "sha256": "9fa162587b73ab9b315be5c88d433f9e0e681b4b36926952f289099ed9bc1dab"
+ },
+ "encoding/tests/test_output_3.txt": {
+ "size": 2481,
+ "sha256": "cf2b801d0738c88abb691bf4a096000c0e0737faab8a54a4ba5243c5513a3711"
+ },
+ "encoding/tests/test_output_4.txt": {
+ "size": 574,
+ "sha256": "94433b68370dcc69fa61efc5011b5d0e28685e4716fee1ee77fbbfc0c9935b54"
+ },
+ "encoding/tests/test_output_5.txt": {
+ "size": 408,
+ "sha256": "4d673091746553d1cb2e40a3fc01dc09a7c53c40627096a3e49c97a269a81f64"
+ },
+ "exl3_k_map.json": {
+ "size": 1409,
+ "sha256": "4359af687bdce7fccd4fc74a57c2f9154b42053447cdc6e0d1768e68fffbec0f"
+ },
+ "model-00001-of-00043.safetensors": {
+ "size": 1323827293,
+ "sha256": "ba5ccb4d39882d08800e856150bce42b23584f3d9943520ae4cff5647d5a3045"
+ },
+ "model-00002-of-00043.safetensors": {
+ "size": 5322748742,
+ "sha256": "11ed8a0cb267169a5f3ea0ceebb24ab09fb5b84b98e4d4c86dc2f06f1f5e0d40"
+ },
+ "model-00003-of-00043.safetensors": {
+ "size": 5223069939,
+ "sha256": "1de02ee6b4d909eb5936e08e7c3825cbf2fa65693c5674deade2e70bcb773462"
+ },
+ "model-00004-of-00043.safetensors": {
+ "size": 5230054628,
+ "sha256": "8e68a9e396d958d6645378b44ef3419a4c0b743da0e92b354d2163a4c8bf85ce"
+ },
+ "model-00005-of-00043.safetensors": {
+ "size": 5223069939,
+ "sha256": "c14a66ba26f57e9577e1a656764b3e5828e09c7630ad7404b80c6e265d03a37d"
+ },
+ "model-00006-of-00043.safetensors": {
+ "size": 5223069939,
+ "sha256": "2866602c20b535eec3bb1a737f62d5d6d2dfecbf362977d3a4f5f8e701580b6d"
+ },
+ "model-00007-of-00043.safetensors": {
+ "size": 5223069939,
+ "sha256": "4d12814cd25cf07fe9c6791f0050a8afbc1038bae02fb201243f9c56de8171cd"
+ },
+ "model-00008-of-00043.safetensors": {
+ "size": 5223069939,
+ "sha256": "cb88f50d0ddd81b98db00a6475c356511c3c21f843b2ccd9845fb4017c3f5d15"
+ },
+ "model-00009-of-00043.safetensors": {
+ "size": 5223069939,
+ "sha256": "dbebd82073a27f79dd1353d3c309255f46e577eb1c2f9d8a10a52bc38f6d8bc2"
+ },
+ "model-00010-of-00043.safetensors": {
+ "size": 5230054628,
+ "sha256": "8cde4e4124614502c2950e9f3a643027eceeeedbdf3c54cf786dbf125d3d536e"
+ },
+ "model-00011-of-00043.safetensors": {
+ "size": 5223069939,
+ "sha256": "c96372d73271d2c59e5a959bb81551aaacb2abe8445f5b9ea9391e2b4a033601"
+ },
+ "model-00012-of-00043.safetensors": {
+ "size": 5223074621,
+ "sha256": "337345b1ca174aebcff619bbc2c2a6e33881a42ad8f638956e69b5de4fe21d41"
+ },
+ "model-00013-of-00043.safetensors": {
+ "size": 5218650941,
+ "sha256": "042b4274c48e55d09bfe51ac099915f8bc3bb0c7be9b0f9d5e75a9f5c2045497"
+ },
+ "model-00014-of-00043.safetensors": {
+ "size": 5218650941,
+ "sha256": "bc27a27e60bebd88ad36f4896aa027911bc5f7fbb30ad9a25f95a59a6374d957"
+ },
+ "model-00015-of-00043.safetensors": {
+ "size": 5297522070,
+ "sha256": "74d3f0d12e32a658d8f6c5f9a104826e473c53299267519b46d594619756ae66"
+ },
+ "model-00016-of-00043.safetensors": {
+ "size": 5225635649,
+ "sha256": "0a1b0b3ecb817f448d9fb34ebf0757d4b00042220515ecb13f1b86908cb092f3"
+ },
+ "model-00017-of-00043.safetensors": {
+ "size": 5218650941,
+ "sha256": "e4b4278a51ce9daf61290d4caba5b953f5a243832ffb5c8d29a0c259807facea"
+ },
+ "model-00018-of-00043.safetensors": {
+ "size": 5218650941,
+ "sha256": "07e9d048f9cefb94b9a30f41e39b9390b6bf54a763c0b7e1dcd5d81144fc0849"
+ },
+ "model-00019-of-00043.safetensors": {
+ "size": 5218650941,
+ "sha256": "2c6116ee6f584af8c4027cee8f5069d0d51a4e9b487dd73027127d29f4caf274"
+ },
+ "model-00020-of-00043.safetensors": {
+ "size": 3519956829,
+ "sha256": "817d948752825a739073b95bc6171a35e4677c095ac617bd6772560ae0e4b5e4"
+ },
+ "model-00021-of-00043.safetensors": {
+ "size": 3519956829,
+ "sha256": "4bdda16b5a00ac5ee0e39bc81271af6155caacb7c03928876b07344e20a4ba5f"
+ },
+ "model-00022-of-00043.safetensors": {
+ "size": 3525291432,
+ "sha256": "00afb7bd831cd50added9b809cc2e9cec57ab44963f152f8bc42cd6d22f0a4e4"
+ },
+ "model-00023-of-00043.safetensors": {
+ "size": 3519956829,
+ "sha256": "28ba9d401e2cd556d0145b31c03ed341f009e2849162e6d9f68438a067d74a59"
+ },
+ "model-00024-of-00043.safetensors": {
+ "size": 3519956829,
+ "sha256": "1240d8744902914287da94cd8e2a5d9f17e7c39167bfab20c737a34d7605e50c"
+ },
+ "model-00025-of-00043.safetensors": {
+ "size": 5218650941,
+ "sha256": "a053c3dc54c4ecfe295cfa4bd7fb44f1054ade1d596e46496f6791c87ffb60df"
+ },
+ "model-00026-of-00043.safetensors": {
+ "size": 5222266717,
+ "sha256": "028c44abbea066c94da7ed0e64f07f8d36ae925a4ff437ef999f5a2b5ec6c9d4"
+ },
+ "model-00027-of-00043.safetensors": {
+ "size": 5218650941,
+ "sha256": "69cbb8f2f00ddcb7a83b04d34ca9151c61f3319ec742d0ac5f06abf295a91eef"
+ },
+ "model-00028-of-00043.safetensors": {
+ "size": 5218650941,
+ "sha256": "451c69700eb30a9e47ec80fe219aa537985c6afa3f7391db5699fba50d93e05b"
+ },
+ "model-00029-of-00043.safetensors": {
+ "size": 5218650941,
+ "sha256": "906a9525cde8176d18066d27ae9d457ff55ee3134438dadea4ee63af8dce45ad"
+ },
+ "model-00030-of-00043.safetensors": {
+ "size": 5222266717,
+ "sha256": "420ad929c1efa3f90ba4a39d28159d9a29175089cd9bd4fe32f25c6fb7e473f6"
+ },
+ "model-00031-of-00043.safetensors": {
+ "size": 5220125501,
+ "sha256": "0312eb9c04dbe5f538efd94a21d1f76266a8cac9ea01250084aa6513657fc5df"
+ },
+ "model-00032-of-00043.safetensors": {
+ "size": 5223074621,
+ "sha256": "4653d193e28120319fe5b3508ffed537d23f57b8d31b20f3f69371fd569e59b2"
+ },
+ "model-00033-of-00043.safetensors": {
+ "size": 5223074621,
+ "sha256": "3a6bfc60065324e606082ae8ad68ff403285b24e7a720b3b2274e516a247252a"
+ },
+ "model-00034-of-00043.safetensors": {
+ "size": 5226690397,
+ "sha256": "b1825ddd7016d3cefea0f13ec78aff33637c68e0eb721cdb12f83104c03b3760"
+ },
+ "model-00035-of-00043.safetensors": {
+ "size": 5223074621,
+ "sha256": "9b513ca99a0e8403d172df623a4c0fe7ecaaa51e09015d9574efa90bd6414a32"
+ },
+ "model-00036-of-00043.safetensors": {
+ "size": 5223074621,
+ "sha256": "206ca962082e2c4de968ba4cc82cc1837dccb0b8960f493e04891b281bbfe497"
+ },
+ "model-00037-of-00043.safetensors": {
+ "size": 5223074621,
+ "sha256": "6ed69fdcd3bd32762ddefae671c7fce7f9b5a212cdbe1aaee64adaa5ef0eef2c"
+ },
+ "model-00038-of-00043.safetensors": {
+ "size": 5226690397,
+ "sha256": "24a61b540656dc210b3bc3215b1713f0b4bee8b724176a4f5968e24b9540035c"
+ },
+ "model-00039-of-00043.safetensors": {
+ "size": 5223074621,
+ "sha256": "29e5b40a97edcb91ed6a382b39bbb4d16ce40de2d1ddce2f75a8fd75e0ad609f"
+ },
+ "model-00040-of-00043.safetensors": {
+ "size": 5223074621,
+ "sha256": "5c850a3950ce68a93a24336ccc417ac660fdbdb84bcc494aa6d57ea5c28246be"
+ },
+ "model-00041-of-00043.safetensors": {
+ "size": 5719789284,
+ "sha256": "5a6f5791d3f5f9635e58db5a693f68c64c6c9e14357e8386a21e5fd232af032d"
+ },
+ "model-00042-of-00043.safetensors": {
+ "size": 4854649316,
+ "sha256": "2f80f4a44e4608c6da139eccf2483122ac88a240046872965d546b4fa26ded06"
+ },
+ "model-00043-of-00043.safetensors": {
+ "size": 3500776125,
+ "sha256": "c5b94d519d323921baacb34b6111e518ce2c36309d06c80fe4e42e3aa9662bc5"
+ },
+ "model.safetensors.index.json": {
+ "size": 15504210,
+ "sha256": "778baeaf9fbcd351800536dc090ff1edddb72d102472b08de0edd9a52cb96656"
+ },
+ "quantization_config.json": {
+ "size": 54653012,
+ "sha256": "696cdad521cdebea0642a3c22d01c9152dca4e5ccbf209b5e7f2b808f359bd27"
+ },
+ "tokenizer.json": {
+ "size": 6367257,
+ "sha256": "c90dfa01249db1be4245780a052ede752e1361c612ac6d08e2bdada7d599476b"
+ },
+ "tokenizer_config.json": {
+ "size": 801,
+ "sha256": "6ac8c8dc065ed118161d02dd532749ae3f52c243deac27872134fae2f50d8547"
+ },
+ "engram-token-map.bin": {
+ "size": 517680,
+ "sha256": "b9a7cf9d341f5daf8fc435dbc36153bf4b28c1b8075be20de30d0556fb910511"
+ }
+}
diff --git a/docs/receipts/ds1017-tp2-2026-10-19/prompts/gen-short-tokens.json b/docs/receipts/ds1017-tp2-2026-10-19/prompts/gen-short-tokens.json
new file mode 100644
index 0000000000..801ba3dbec
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-2026-10-19/prompts/gen-short-tokens.json
@@ -0,0 +1 @@
+[51262,8399,28611,5018,223,9636,27962,78797,27873,16847,118807,127311,78797,5329,55699,15,26614,845,37723,34186,75178,106278,18278,302,343,66755,54908,223,792,13,112365,8538,57374,295,223,22,5871,279,15,35420,75345,11,51163,40695,350,641,49938,1095,559,24945,115860,14,46809,10561,36089,54186,932,36357,61573,15816,5751,50111,16,29991,3850,847,52606,90979,13303,932,81343,6611,650,31868,60248,7567,18489,7663,74105,1424,14,58708,1483,85308,267,78873,69794,7567,823,21427,18440,8051,50,8095,16,54395,118466,3272,932,57073,8203,6067,5620,38801,479,14,62085,10561,45335,75178,9662,302,56609,14,37363,932,63488,1311,60821,350,1950,267,932,1106,73509,20365,16,1]
\ No newline at end of file
diff --git a/docs/receipts/ds1017-tp2-2026-10-19/prompts/mid.json b/docs/receipts/ds1017-tp2-2026-10-19/prompts/mid.json
new file mode 100644
index 0000000000..6bfac92d58
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-2026-10-19/prompts/mid.json
@@ -0,0 +1 @@
+[0, 128803, 77616, 6067, 9165, 112734, 6897, 434, 75, 979, 9854, 27801, 201, 835, 3, 30700, 284, 2374, 966, 17721, 28, 260, 4184, 34868, 295, 1009, 3770, 319, 63248, 568, 593, 29, 1660, 48319, 734, 6946, 83692, 477, 7466, 87, 13193, 989, 835, 3, 1733, 418, 10241, 5246, 852, 10518, 377, 260, 2767, 32610, 343, 272, 538, 260, 37465, 6946, 23809, 11, 305, 21886, 284, 1055, 59983, 989, 835, 3, 832, 270, 41668, 13187, 29009, 19076, 270, 1975, 6946, 20711, 260, 25137, 4184, 1114, 3959, 603, 835, 3, 9996, 273, 807, 1407, 260, 2395, 3608, 40, 319, 8203, 66, 8980, 29, 270, 568, 593, 12791, 42936, 50, 1795, 54975, 344, 1009, 1353, 9963, 603, 3949, 6849, 438, 2390, 1897, 1698, 8017, 4913, 3949, 93086, 438, 2390, 1897, 1698, 12854, 23982, 9391, 4969, 67129, 16, 27801, 17380, 38744, 1600, 5288, 98268, 438, 223, 9636, 510, 38744, 1600, 7617, 98268, 438, 223, 26, 1048, 23295, 4114, 4184, 734, 2445, 295, 1009, 568, 593, 29, 98861, 477, 8321, 304, 3608, 5263, 37864, 16781, 412, 295, 270, 3770, 319, 63248, 21418, 603, 38744, 1600, 13559, 438, 2771, 875, 361, 3618, 28, 121382, 18, 63, 3949, 549, 26, 989, 361, 4245, 98268, 28, 88815, 989, 361, 1499, 37864, 28, 88815, 989, 361, 5288, 65, 8277, 28, 88815, 989, 361, 7617, 65, 8277, 28, 88815, 989, 361, 17721, 28, 88815, 989, 12797, 38744, 1600, 31493, 438, 2771, 875, 361, 8455, 7973, 6946, 83692, 834, 6632, 1926, 6026, 343, 348, 7048, 17721, 1527, 223, 1173, 201, 20759, 40, 18420, 295, 93410, 774, 616, 102982, 1424, 65917, 14, 515, 27072, 7222, 75, 9411, 16, 128804, 128822]
\ No newline at end of file
diff --git a/docs/receipts/ds1017-tp2-2026-10-19/prompts/short.json b/docs/receipts/ds1017-tp2-2026-10-19/prompts/short.json
new file mode 100644
index 0000000000..f76f442938
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-2026-10-19/prompts/short.json
@@ -0,0 +1 @@
+[0, 128803, 6973, 7355, 51033, 295, 54523, 327, 10832, 4626, 14, 4559, 395, 4440, 8399, 28611, 5018, 223, 9636, 27962, 36357, 8025, 75178, 106278, 18278, 302, 38801, 479, 6067, 16, 128804, 128822]
\ No newline at end of file
diff --git a/docs/receipts/ds1017-tp2-2026-10-19/sha256.json b/docs/receipts/ds1017-tp2-2026-10-19/sha256.json
new file mode 100644
index 0000000000..0f9cb9d4a3
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-2026-10-19/sha256.json
@@ -0,0 +1,13 @@
+{
+ "README.md": "e28cbc2a2ca5020b5168b0e662482a810904b8682e895d716219a72d2ca00042",
+ "build.json": "231a6ef5bda3477634ce5d0d3b2a87dd4087530b474de716329fae1f62c27b02",
+ "http.json": "e4b7db1256277cf47698be78f2b0e0398b2ebd9a6f23030c8216beba72b51c2d",
+ "local-checks.json": "4f19af53134dc8aed7b3be72471f59005e11ec8bbb5d4eb050722e23884c61a2",
+ "measurements.json": "32321c9a0d0f19cfe812adf7fb97bc72843d4802641536e6b49e48feb47e51f2",
+ "model.json": "34fee4787291d61eeda6c54ad8c25d0d49503928ed0b54f09809ef0f534f5923",
+ "numerical.json": "9ea58e1eba0f3054acc354238dc720e824bb7a6f35c7fa3b70790a9c5c5d834c",
+ "pack-sha256.json": "26b9ee443b9570580b39153884aaabfa49220e43567dfc592ddbb11fc4addf73",
+ "prompts/gen-short-tokens.json": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "prompts/mid.json": "bd89ca2be66ba1b2f53c0ac519e6bc41937817b7f264b347722fc7d37f40bd5e",
+ "prompts/short.json": "fc091e38591bb777d06dc66a7b492f92755a4b60f1eb1aea00897a01e40c4489"
+}
diff --git a/docs/receipts/ds1017-tp2-slots-2026-10-20/README.md b/docs/receipts/ds1017-tp2-slots-2026-10-20/README.md
new file mode 100644
index 0000000000..0c8fc317f0
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-slots-2026-10-20/README.md
@@ -0,0 +1,112 @@
+# DeepSeek-V4.1-1017: shared family, sparse attention and TCP MoE slots
+
+Runtime `cdfa5203d19805b1e544b7b39647b5bc9170e913` improves the measured TCP
+pair from 3.0992 to 3.6003 decode forwards/s by exchanging complete routed
+slots once per layer. One Mac remains faster at 4.8619 forwards/s. The two-Mac
+path still duplicates the complete backbone and attention; it provides no RAM
+reduction. These are development receipts, not whole-model release qualification.
+
+## Pins and method
+
+The upstream base is `d1018b318fb51981c4a18f12e40269a4d3ee9713`.
+[build.json](build.json) records Zig 0.17.0, `-Dcpu=apple_m3`, the source tree
+and binary SHA256s. Both Macs have an M3 Ultra, 80 GPU cores and 256 GB of
+unified memory. They use the [same checkpoint and original 1017 Engram tables](../ds1017-tp2-2026-10-19/model.json)
+as the earlier receipt. All 69 files, 413,892,636,159 bytes per Mac, were hashed
+before stopping production. [checkpoint-verification.json](checkpoint-verification.json)
+records the receipts and unchanged file identities before and after execution.
+
+All three configurations enable `TF_DSV41_PARALLEL_ATTENTION=1`, split weight
+residency, periodic residency refresh and SSD Engram. `TF_DSV41_SERIAL` is
+unset. The predecessor attention comparison is a separate
+[three-run experiment at 61ab890f](../ds1017-attention-2026-10-19/README.md).
+The current single-Mac result repeats its approximately 4.86 forwards/s with
+the explicit 256-thread reservation.
+
+Each generation uses 32 frozen prompt IDs and produces 128 completion IDs.
+Prefill produces the first completion token, leaving 127 decode forwards.
+Decode timing excludes checkpoint loading and startup pairing. The second Mac
+supplies the three-run solo reference; the first Mac also has one solo sample.
+All raw records remain in [execution.json](execution.json) and the calculated
+summaries in [measurements.json](measurements.json).
+
+| Configuration | Decode forwards/s | Samples |
+| --- | ---: | ---: |
+| Single Mac, second host | 4.8619 | Median of 3 |
+| TCP output columns, rank 0 / rank 1 | 3.0992 / 3.0992 | 1 per rank |
+| TCP routed slots, rank 0 / rank 1 | 3.6003 / 3.6003 | Median of 3 per rank |
+
+The slot partition assigns slots 0/2/4 to rank 0 and 1/3/5 to rank 1. Each
+rank calculates complete projections for its three slots and duplicates the
+shared expert. The ranks gather completed routed rows and combine them in the
+original six-slot order. Generation exchange counts are 15,489 for columns and
+5,249 for slots: 128 forwards at 121 or 41 exchanges, plus startup identity.
+This preserves each full input reduction and avoids a floating-point all-reduce.
+
+## GPU time and remaining overhead
+
+On rank 0, the column sample takes 40.9782 seconds, including 28.8097 seconds
+of GPU timestamps. The slot sample at the median wall time takes 35.2744
+seconds, including 29.6257 seconds of GPU timestamps. The difference between
+wall and GPU time falls from 12.1684 to 5.6487 seconds. This remainder includes
+CPU encoding, waiting and transfer; it is not a measurement of network time
+alone. GPU time does not improve in this comparison.
+
+The 268-token teacher prefill also remains slower on two Macs: 9.7314 seconds
+for columns and 9.5075 for slots on the second host, against 7.5705 solo.
+Loading is separate and varies substantially. The loader creates fresh Metal
+buffers with uncached reads at each invocation. Five bound shards on the first
+Mac, totalling 26.6 GB, live on an external volume. TP `load_seconds` also
+includes peer startup and must not be interpreted as a disk benchmark.
+
+## Numerical, state and execution checks
+
+All six slot generation token files match the
+[frozen original IDs](../ds1017-tp2-2026-10-19/prompts/gen-short-tokens.json).
+Both ranks' teacher logits, with columns, slots and one-token slot chunks,
+match SHA256 `910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2`.
+Two interleaved lane slots at capacity 8192 and chunk 16 finish at positions
+134/134 with zero bit disagreements against solo execution. Every recorded
+model phase passes its Engram-aware execution I/O gate, with zero page-ins.
+This permits counted Engram reads and does not mean zero disk I/O.
+
+Each Mac passes 100 DeepSeek GPU tests, 29 EXL3 GPU tests and 28 reference
+tests. The attention contract passes nonfinite, repeat/guard, shape and exact
+sum gates, plus both actual M3 launch limits. Five planted numerical controls
+are caught, with zero missed controls. The global limits check remains red:
+twelve M1/M2 Flash-Next declarations lack compiled limits. The unchanged
+upstream binary reproduces exactly those findings on both Macs. They remain
+open candidate findings. [checks.json](checks.json) retains all summaries,
+local Linux workflow results, template checks and the reproduced desktop
+failures. Candidate and unchanged base both report 819/821 desktop tests;
+typecheck, mobile and whitespace pass. No Windows execution is claimed locally.
+
+[transport.json](transport.json) records established data and acknowledgement
+sockets on `bridge0` and `en3`, between the two TB5 link addresses. An earlier
+`SYN_SENT` snapshot showed a different source address; it did not establish a
+permanent connection failure. No network configuration changed. This experiment
+uses ordinary TCP and does not qualify MCDMA or RDMA.
+
+## Reproduce and preserve the service
+
+Build the runner and checks at the pinned runtime with Zig 0.17.0 and
+`-Dcpu=apple_m3`. Reuse the earlier receipt's prompts, checkpoint manifest and
+original Engram SHA256s. Set `DSV41_ENGRAM_STREAM=1`,
+`TF_DSV41_SPLIT_RESIDENCY=1`, `TF_DSV41_RESIDENCY_REFRESH=1` and
+`TF_DSV41_PARALLEL_ATTENTION=1`. For the pair, set `TF_DSV41_TP_SETTINGS` to
+each rank's TCP settings. Use `TF_DSV41_TP_SLOTS=0` for columns and `1` for slots.
+Start both model commands together after both file checks complete. Retain
+`tokens.json`, teacher `logits.f32` and every `residency.json`, including failures.
+
+The protected window restores both original Qwen services and the original
+reranker, then verifies real replies. [restoration.json](restoration.json)
+records both Qwen replies as `4`, the reranker's correct synthetic ordering,
+healthy services, cleared test ports, no remaining test processes and removed
+window locks. No production binary was promoted.
+
+The new native binary builds, but its HTTP serving path was not requalified
+at this runtime. The [native HTTP, cancellation and peer-loss receipt](../ds1017-tp2-2026-10-19/README.md)
+belongs to `b27e39af`. Whole-model FP64/noise-floor, original-checkpoint anchors,
+MTP, long-context and Rust-normalizer qualification remain open under
+[#847](https://github.com/ashhart/TensorFold/issues/847). Native serving keeps
+the explicit `TF_EXPERIMENTAL=deepseek` gate.
diff --git a/docs/receipts/ds1017-tp2-slots-2026-10-20/build.json b/docs/receipts/ds1017-tp2-slots-2026-10-20/build.json
new file mode 100644
index 0000000000..702d6ee377
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-slots-2026-10-20/build.json
@@ -0,0 +1,17 @@
+{
+ "runtime_commit": "cdfa5203d19805b1e544b7b39647b5bc9170e913",
+ "upstream": "d1018b318fb51981c4a18f12e40269a4d3ee9713",
+ "compiler": "0.17.0",
+ "cpu": "apple_m3",
+ "runtime_zig_tree": "100af63aa241b97d94e34ff625af00c9e3a5bef7",
+ "binary_sha256": {
+ "tf-dsv41-reference.cdfa5203": "3406c119a2abd4a88c850625db962b2f52a8adbfbd648e55b066253b204cc7ba",
+ "tf-dsv41-gpu.cdfa5203": "6f248d1dd53d2984dfbe5ed047c1a8c8f657d69c9a6c06272ce790cc4c81feb8",
+ "tf-exl3-gpu.cdfa5203": "f5353fe7445bab6d907da95afc07c1f6ea5b706e9bcbc72b8d4a1434eebfdc4c",
+ "tf-kernel-contract.cdfa5203": "d468e2b25583a6a24da05f8155889ab5800963d64c3872e8de66ce72ccbda7e1",
+ "tf-dsv41-run.cdfa5203": "35852fd94c5860e9615f6b0181513121b02bd48d81d37cab4cede24a11b459bc",
+ "tf-kernel-contract.d1018b31": "1cfa845cec595bcd4cb0516d68a1e2203adf94ded1fbbda38741f1f4f5c35122",
+ "tensorfold-native.cdfa5203": "daeaca7bb4979413089cf18180bdc3724160634467d25dc7bb6ec9ce0af98183"
+ },
+ "native_http_tested_at_this_commit": false
+}
diff --git a/docs/receipts/ds1017-tp2-slots-2026-10-20/checkpoint-verification.json b/docs/receipts/ds1017-tp2-slots-2026-10-20/checkpoint-verification.json
new file mode 100644
index 0000000000..e5312ff39d
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-slots-2026-10-20/checkpoint-verification.json
@@ -0,0 +1,40 @@
+{
+ "checkpoint_receipt": "../ds1017-tp2-2026-10-19/model.json",
+ "pack_manifest": "../ds1017-tp2-2026-10-19/pack-sha256.json",
+ "files_per_mac": 69,
+ "bytes_per_mac": 413892636159,
+ "ranks": {
+ "0": {
+ "sha256_receipt": "c53403f55e6469dcfbad5de5cfbed7ac332a02c9dc5be1aff9a3484e78402fe0",
+ "finished_unix": 1792449781.83684,
+ "identity_before": {
+ "state": "unchanged_since_sha256",
+ "files": 69,
+ "receipt_sha256": "c53403f55e6469dcfbad5de5cfbed7ac332a02c9dc5be1aff9a3484e78402fe0",
+ "checked_unix": 1792449894.7394712
+ },
+ "identity_after": {
+ "state": "unchanged_since_sha256",
+ "files": 69,
+ "receipt_sha256": "c53403f55e6469dcfbad5de5cfbed7ac332a02c9dc5be1aff9a3484e78402fe0",
+ "checked_unix": 1792453636.439658
+ }
+ },
+ "1": {
+ "sha256_receipt": "ea4a54eac743704a1c7fbf727c8928b0828f9f27ec709f35b9f11f754a4940c5",
+ "finished_unix": 1792449445.537735,
+ "identity_before": {
+ "state": "unchanged_since_sha256",
+ "files": 69,
+ "receipt_sha256": "ea4a54eac743704a1c7fbf727c8928b0828f9f27ec709f35b9f11f754a4940c5",
+ "checked_unix": 1792449894.657202
+ },
+ "identity_after": {
+ "state": "unchanged_since_sha256",
+ "files": 69,
+ "receipt_sha256": "ea4a54eac743704a1c7fbf727c8928b0828f9f27ec709f35b9f11f754a4940c5",
+ "checked_unix": 1792453636.25322
+ }
+ }
+ }
+}
diff --git a/docs/receipts/ds1017-tp2-slots-2026-10-20/checks.json b/docs/receipts/ds1017-tp2-slots-2026-10-20/checks.json
new file mode 100644
index 0000000000..fde2a1d3ce
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-slots-2026-10-20/checks.json
@@ -0,0 +1,329 @@
+{
+ "candidate": "cdfa5203d19805b1e544b7b39647b5bc9170e913",
+ "upstream": "d1018b318fb51981c4a18f12e40269a4d3ee9713",
+ "zig": "0.17.0",
+ "cpu": "apple_m3",
+ "linux": {
+ "zig": "0.17.0",
+ "platform": "Linux-6.8.0-117-generic-aarch64-with-glibc2.39",
+ "results": [
+ {
+ "command": [
+ "zig",
+ "build",
+ "test"
+ ],
+ "exit_code": 0,
+ "seconds": 3.1937536880000152,
+ "log": "0-zig_build_test.log"
+ },
+ {
+ "command": [
+ "zig",
+ "build",
+ "test-kernel-contract"
+ ],
+ "exit_code": 0,
+ "seconds": 10.83161148499994,
+ "log": "1-zig_build_test-kernel-contract.log"
+ },
+ {
+ "command": [
+ "zig",
+ "build",
+ "test-golden"
+ ],
+ "exit_code": 0,
+ "seconds": 74.71593902999984,
+ "log": "2-zig_build_test-golden.log"
+ },
+ {
+ "command": [
+ "zig",
+ "build",
+ "fuzz-cortheon"
+ ],
+ "exit_code": 0,
+ "seconds": 0.12619894899989959,
+ "log": "3-zig_build_fuzz-cortheon.log"
+ },
+ {
+ "command": [
+ "python3",
+ "-m",
+ "unittest",
+ "discover",
+ "-s",
+ "tools/zig",
+ "-p",
+ "flashnext_batch_serve_test.py"
+ ],
+ "exit_code": 0,
+ "seconds": 0.12447411799985275,
+ "log": "4-python3_-m_unittest_discover_-s_tools_zig_-p_flashnext_batch_serve_test.py.log"
+ },
+ {
+ "command": [
+ "python3",
+ "lean_new.py"
+ ],
+ "exit_code": 0,
+ "log": "lean-new.log"
+ }
+ ]
+ },
+ "app": {
+ "cdfa5203": {
+ "head": "cdfa5203d19805b1e544b7b39647b5bc9170e913",
+ "node": "v22.23.2",
+ "npm": "10.9.8",
+ "state": "failed",
+ "results": [
+ {
+ "command": [
+ "npm",
+ "install",
+ "--global",
+ "--ignore-scripts",
+ "npm@10.9.8"
+ ],
+ "exit_code": 0,
+ "seconds": 3.1424792910111137
+ },
+ {
+ "command": [
+ "npm",
+ "ci",
+ "--ignore-scripts"
+ ],
+ "exit_code": 0,
+ "seconds": 81.39682687498862
+ },
+ {
+ "command": [
+ "npm",
+ "--prefix",
+ "mobile",
+ "ci",
+ "--ignore-scripts"
+ ],
+ "exit_code": 0,
+ "seconds": 32.903031249996275
+ },
+ {
+ "command": [
+ "npm",
+ "test"
+ ],
+ "exit_code": 1,
+ "seconds": 930.9491869999911
+ },
+ {
+ "command": [
+ "npm",
+ "run",
+ "typecheck"
+ ],
+ "exit_code": 0,
+ "seconds": 44.65896029199939
+ },
+ {
+ "command": [
+ "npm",
+ "run",
+ "verify:mobile"
+ ],
+ "exit_code": 0,
+ "seconds": 10.245477958000265
+ },
+ {
+ "command": [
+ "git",
+ "diff",
+ "--check",
+ "HEAD^",
+ "HEAD"
+ ],
+ "exit_code": 0,
+ "seconds": 0.0676065000006929
+ }
+ ]
+ },
+ "d1018b31": {
+ "head": "d1018b318fb51981c4a18f12e40269a4d3ee9713",
+ "node": "v22.23.2",
+ "npm": "10.9.8",
+ "state": "failed",
+ "results": [
+ {
+ "command": [
+ "npm",
+ "install",
+ "--global",
+ "--ignore-scripts",
+ "npm@10.9.8"
+ ],
+ "exit_code": 0,
+ "seconds": 2.724249584018253
+ },
+ {
+ "command": [
+ "npm",
+ "ci",
+ "--ignore-scripts"
+ ],
+ "exit_code": 0,
+ "seconds": 7.655476124986308
+ },
+ {
+ "command": [
+ "npm",
+ "--prefix",
+ "mobile",
+ "ci",
+ "--ignore-scripts"
+ ],
+ "exit_code": 0,
+ "seconds": 11.355899582995335
+ },
+ {
+ "command": [
+ "npm",
+ "test"
+ ],
+ "exit_code": 1,
+ "seconds": 700.9388846250076
+ },
+ {
+ "command": [
+ "npm",
+ "run",
+ "typecheck"
+ ],
+ "exit_code": 0,
+ "seconds": 46.43345195800066
+ },
+ {
+ "command": [
+ "npm",
+ "run",
+ "verify:mobile"
+ ],
+ "exit_code": 0,
+ "seconds": 7.250187666999409
+ },
+ {
+ "command": [
+ "git",
+ "diff",
+ "--check",
+ "HEAD^",
+ "HEAD"
+ ],
+ "exit_code": 0,
+ "seconds": 0.047125833021709695
+ }
+ ]
+ }
+ },
+ "app_failures_reproduced_on_unchanged_base": true,
+ "app_tree_sha1": "f2909da2ed69931a5acb654e371391c5d96f8b95",
+ "template": {
+ "head": "cdfa5203d19805b1e544b7b39647b5bc9170e913",
+ "template_sha256": "d959d804d5101b79a49b6ff1bf3c54cd5affa9a5a78c503d925f3090040b31c3",
+ "binary_sha256": "121bef9f94653b586b1a2c58fe5d01e92b7150bb30c9e7e3ef5350cc3f335e6c",
+ "compiler": "0.17.0",
+ "cpu": "apple_m3",
+ "fixtures": {
+ "test_input_1.json": "6a5a86694e47169c8f0c9243a7e24cc5db05447be0d3d3a9891034e913c518de",
+ "test_input_2.json": "fd985cda88e8640b8e2404d8e659f7006cad204eb41f73c9e6662b1f218c01aa",
+ "test_input_3.json": "4207613f15c79b95ad92074bdb8b2b81c5ce9cfdf15b7a67f58ded6bb3ec4fa5",
+ "test_input_4.json": "4c3890a7bc042ec6396d98d024a39a73cdcffbe1d0253e852da0c35e65296f3e",
+ "test_input_5.json": "88289b0aac8d650aa55c4ffeb392818fff3a0ac83c2293ddfd90546804d5e5c4",
+ "test_output_1.txt": "91f79aa955717b414a82aabfb38bcd83ab5a998bd47db89d2a26338e8d3de550",
+ "test_output_2.txt": "9fa162587b73ab9b315be5c88d433f9e0e681b4b36926952f289099ed9bc1dab",
+ "test_output_3.txt": "cf2b801d0738c88abb691bf4a096000c0e0737faab8a54a4ba5243c5513a3711",
+ "test_output_4.txt": "94433b68370dcc69fa61efc5011b5d0e28685e4716fee1ee77fbbfc0c9935b54",
+ "test_output_5.txt": "4d673091746553d1cb2e40a3fc01dc09a7c53c40627096a3e49c97a269a81f64"
+ },
+ "result": "case 1: equal (2476 bytes)\ncase 2: equal (294 bytes)\ncase 3: equal (2481 bytes)\ncase 4: equal (574 bytes)\ncase 5: equal (408 bytes)\n5/5 cases equal\n"
+ },
+ "native_build": {
+ "candidate": "cdfa5203d19805b1e544b7b39647b5bc9170e913",
+ "upstream": "d1018b318fb51981c4a18f12e40269a4d3ee9713",
+ "zig": "0.17.0",
+ "cpu": "apple_m3",
+ "built_unix": 1792449321.578912,
+ "files": {
+ "tf-kernel-contract.d1018b31": "1cfa845cec595bcd4cb0516d68a1e2203adf94ded1fbbda38741f1f4f5c35122",
+ "tensorfold-native.cdfa5203": "daeaca7bb4979413089cf18180bdc3724160634467d25dc7bb6ec9ce0af98183"
+ },
+ "native_full_model_tested": false,
+ "native_build_steps": 54,
+ "unchanged_baseline_contract_build_steps": 43
+ },
+ "fmt": {
+ "command": [
+ "zig",
+ "fmt",
+ "--check",
+ "build.zig",
+ "zig"
+ ],
+ "exit_code": 0
+ },
+ "local_windows_execution": "not run on Darwin; inspect current-head remote CI after push",
+ "qualification": "development proof only; global kernel limits and whole-model release gates remain open",
+ "kernel_contract": {
+ "0": {
+ "operator_tests": {
+ "dsv41-gpu": 100,
+ "dsv41-reference": 28,
+ "exl3-gpu": 29
+ },
+ "candidate_summary": "SUMMARY 1 kernels: 1 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 12 limit problems; gates 290 ms, limits 74 ms",
+ "unchanged_base_summary": "SUMMARY 0 kernels: 0 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 12 limit problems; gates 0 ms, limits 3599 ms",
+ "numerical_controls_summary": "SUMMARY 0 kernels: 0 pass gates 1-4, 0 fail, 0 known findings; 5 controls: 5 red as planted, 0 missed; 0 limit problems; gates 12 ms, limits 0 ms",
+ "global_missing_limits_reproduced_on_base": [
+ "flashnext rotary absolute fz_idx_pool_abs on M1: no compiled thread limit recorded",
+ "flashnext rotary absolute fz_idx_pool_abs on M2: no compiled thread limit recorded",
+ "flashnext rotary mtp custom_kernel_q4_attn_prep_63a42a6cd8e0434a_bfloat16_t_int32_t_float_float_float_floatc_floatc_bfloat16_t_bfloat16_t_bfloat16_t on M1: no compiled thread limit recorded",
+ "flashnext rotary mtp custom_kernel_q4_attn_prep_63a42a6cd8e0434a_bfloat16_t_int32_t_float_float_float_floatc_floatc_bfloat16_t_bfloat16_t_bfloat16_t on M2: no compiled thread limit recorded",
+ "flashnext rotary pooled custom_kernel_q4_idx_pool_6d826f5ebecec197_bfloat16_t_int32_tc_float_floatc_floatc_bfloat16_t on M1: no compiled thread limit recorded",
+ "flashnext rotary pooled custom_kernel_q4_idx_pool_6d826f5ebecec197_bfloat16_t_int32_tc_float_floatc_floatc_bfloat16_t on M2: no compiled thread limit recorded",
+ "flashnext rotary target custom_kernel_q4_attn_prep_63a42a6cd8e0434a_bfloat16_t_int32_t_float_float_float_floatc_floatc_bfloat16_t_bfloat16_t_bfloat16_t on M1: no compiled thread limit recorded",
+ "flashnext rotary target custom_kernel_q4_attn_prep_63a42a6cd8e0434a_bfloat16_t_int32_t_float_float_float_floatc_floatc_bfloat16_t_bfloat16_t_bfloat16_t on M2: no compiled thread limit recorded",
+ "flashnext vision fn_attn_prep_mrope on M1: no compiled thread limit recorded",
+ "flashnext vision fn_attn_prep_mrope on M2: no compiled thread limit recorded",
+ "flashnext vision fz_mask_rows on M1: no compiled thread limit recorded",
+ "flashnext vision fz_mask_rows on M2: no compiled thread limit recorded"
+ ],
+ "own_attention_launches_pass": true
+ },
+ "1": {
+ "operator_tests": {
+ "dsv41-gpu": 100,
+ "dsv41-reference": 28,
+ "exl3-gpu": 29
+ },
+ "candidate_summary": "SUMMARY 1 kernels: 1 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 12 limit problems; gates 299 ms, limits 75 ms",
+ "unchanged_base_summary": "SUMMARY 0 kernels: 0 pass gates 1-4, 0 fail, 0 known findings; 0 controls: 0 red as planted, 0 missed; 12 limit problems; gates 0 ms, limits 3559 ms",
+ "numerical_controls_summary": "SUMMARY 0 kernels: 0 pass gates 1-4, 0 fail, 0 known findings; 5 controls: 5 red as planted, 0 missed; 0 limit problems; gates 12 ms, limits 0 ms",
+ "global_missing_limits_reproduced_on_base": [
+ "flashnext rotary absolute fz_idx_pool_abs on M1: no compiled thread limit recorded",
+ "flashnext rotary absolute fz_idx_pool_abs on M2: no compiled thread limit recorded",
+ "flashnext rotary mtp custom_kernel_q4_attn_prep_63a42a6cd8e0434a_bfloat16_t_int32_t_float_float_float_floatc_floatc_bfloat16_t_bfloat16_t_bfloat16_t on M1: no compiled thread limit recorded",
+ "flashnext rotary mtp custom_kernel_q4_attn_prep_63a42a6cd8e0434a_bfloat16_t_int32_t_float_float_float_floatc_floatc_bfloat16_t_bfloat16_t_bfloat16_t on M2: no compiled thread limit recorded",
+ "flashnext rotary pooled custom_kernel_q4_idx_pool_6d826f5ebecec197_bfloat16_t_int32_tc_float_floatc_floatc_bfloat16_t on M1: no compiled thread limit recorded",
+ "flashnext rotary pooled custom_kernel_q4_idx_pool_6d826f5ebecec197_bfloat16_t_int32_tc_float_floatc_floatc_bfloat16_t on M2: no compiled thread limit recorded",
+ "flashnext rotary target custom_kernel_q4_attn_prep_63a42a6cd8e0434a_bfloat16_t_int32_t_float_float_float_floatc_floatc_bfloat16_t_bfloat16_t_bfloat16_t on M1: no compiled thread limit recorded",
+ "flashnext rotary target custom_kernel_q4_attn_prep_63a42a6cd8e0434a_bfloat16_t_int32_t_float_float_float_floatc_floatc_bfloat16_t_bfloat16_t_bfloat16_t on M2: no compiled thread limit recorded",
+ "flashnext vision fn_attn_prep_mrope on M1: no compiled thread limit recorded",
+ "flashnext vision fn_attn_prep_mrope on M2: no compiled thread limit recorded",
+ "flashnext vision fz_mask_rows on M1: no compiled thread limit recorded",
+ "flashnext vision fz_mask_rows on M2: no compiled thread limit recorded"
+ ],
+ "own_attention_launches_pass": true
+ }
+ }
+}
diff --git a/docs/receipts/ds1017-tp2-slots-2026-10-20/execution.json b/docs/receipts/ds1017-tp2-slots-2026-10-20/execution.json
new file mode 100644
index 0000000000..d40dec3d0e
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-slots-2026-10-20/execution.json
@@ -0,0 +1,1085 @@
+{
+ "0": {
+ "columns-generate-0": {
+ "json_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.4458014583215117,
+ "footprint_bytes": 211319329064,
+ "disk_read_bytes": 21905408,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.4458600416546687,
+ "decode_seconds": 40.978184666717425,
+ "decode_gpu_seconds": 28.809736449620686
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 15489,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 629,
+ "ready_footprint_bytes": 210979541216,
+ "final_footprint_bytes": 211319247144,
+ "load_seconds": 533.2622895833338,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 106987520,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.411335,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "e3aa8459272f2488b3f519da0a3e0db007ec6db4985736b2edd97ece5f9dbc59",
+ "tokens.json": {
+ "sha256": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "bytes": 657
+ }
+ },
+ "columns-teacher": {
+ "json_records": [
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 9.742232375079766,
+ "gpu_seconds": 8.82532766717486
+ },
+ {
+ "mode": "teacher",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 243,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 327,
+ "ready_footprint_bytes": 210979557720,
+ "final_footprint_bytes": 211372446232,
+ "load_seconds": 449.4933994166786,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 163684352,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 197459968,
+ "engram_rows_requested": 12864,
+ "engram_rows_unique": 11872,
+ "engram_cache_hits": 0,
+ "engram_reads": 11870,
+ "engram_wait_seconds": 0.100831,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "459ab23ca012e34b34cb352c082cfc164d06d21346bc63d7e5bbb185d2e183bd",
+ "logits.f32": {
+ "sha256": "910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2",
+ "bytes": 138588160
+ }
+ },
+ "slots-decode-teacher": {
+ "json_records": [
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 76.39929441665299,
+ "gpu_seconds": 64.28252266824711
+ },
+ {
+ "mode": "teacher",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 10989,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 364,
+ "ready_footprint_bytes": 210979541192,
+ "final_footprint_bytes": 211353473344,
+ "load_seconds": 392.11662612494547,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 179666944,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 214106112,
+ "engram_rows_requested": 12864,
+ "engram_rows_unique": 12864,
+ "engram_cache_hits": 0,
+ "engram_reads": 12864,
+ "engram_wait_seconds": 0.826166,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "d54dc60b03e685090998eafdb9fc60d8a9fde5a5fe5bd27974162c6843b65aca",
+ "logits.f32": {
+ "sha256": "910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2",
+ "bytes": 138588160
+ }
+ },
+ "slots-generate-0": {
+ "json_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.384232750046067,
+ "footprint_bytes": 211318706520,
+ "disk_read_bytes": 21446656,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3842877499992028,
+ "decode_seconds": 35.27436200005468,
+ "decode_gpu_seconds": 29.62567888130434
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 5249,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 90,
+ "ready_footprint_bytes": 210979508472,
+ "final_footprint_bytes": 211318657392,
+ "load_seconds": 319.91302254155744,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 106680320,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.409357,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "9614e46c3d49d6889d551d0cde5632147b1d7b4a1025096fc4d1aa67956b12ad",
+ "tokens.json": {
+ "sha256": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "bytes": 657
+ }
+ },
+ "slots-generate-1": {
+ "json_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.392772625084035,
+ "footprint_bytes": 211318722928,
+ "disk_read_bytes": 21446656,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3928204167168587,
+ "decode_seconds": 35.391926166717894,
+ "decode_gpu_seconds": 29.642197125707753
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 5249,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 90,
+ "ready_footprint_bytes": 210979574080,
+ "final_footprint_bytes": 211318690184,
+ "load_seconds": 319.35464254161343,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 106676224,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.407941,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "5f5a1c1d3d473f880dc8d91f18584042b7658547ec9b9bfb6b5995c24383b8d6",
+ "tokens.json": {
+ "sha256": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "bytes": 657
+ }
+ },
+ "slots-generate-2": {
+ "json_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.3941294166725129,
+ "footprint_bytes": 211318755624,
+ "disk_read_bytes": 21446656,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3941821666667238,
+ "decode_seconds": 35.21301570837386,
+ "decode_gpu_seconds": 29.64614437648561
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 5249,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 89,
+ "ready_footprint_bytes": 210979606776,
+ "final_footprint_bytes": 211318706496,
+ "load_seconds": 319.82512804167345,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 106676224,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.413062,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "a8dfb9099e01d25c1ddf32ef0b2789374335501e93e942a5fcd7965a630ae7dd",
+ "tokens.json": {
+ "sha256": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "bytes": 657
+ }
+ },
+ "slots-lanes": {
+ "json_records": [
+ {
+ "mode": "lanes",
+ "slots": 2,
+ "chunk": 16,
+ "max_logit_error_slot0": 0,
+ "max_logit_error_slot1": 0,
+ "bit_disagreements": 0,
+ "position_slot0": 134,
+ "position_slot1": 134,
+ "gpu_seconds": 14.114
+ },
+ {
+ "mode": "lanes",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 1477,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 8192,
+ "consumed_tokens": 134,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 202796646400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 75,
+ "ready_footprint_bytes": 209533423504,
+ "final_footprint_bytes": 210013278264,
+ "load_seconds": 318.0446677500149,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 346652672,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 414777344,
+ "engram_rows_requested": 25728,
+ "engram_rows_unique": 24928,
+ "engram_cache_hits": 0,
+ "engram_reads": 24928,
+ "engram_wait_seconds": 0.289244,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "00b16a4f5141c3e0721057a8ab1a65664a8e203356da6ba67a2e588dc6eeb7ca"
+ },
+ "slots-teacher": {
+ "json_records": [
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 9.507720541558228,
+ "gpu_seconds": 8.991306541720405
+ },
+ {
+ "mode": "teacher",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 83,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 173,
+ "ready_footprint_bytes": 210979688744,
+ "final_footprint_bytes": 211369841128,
+ "load_seconds": 381.5674634583993,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 163946496,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 197459968,
+ "engram_rows_requested": 12864,
+ "engram_rows_unique": 11872,
+ "engram_cache_hits": 0,
+ "engram_reads": 11870,
+ "engram_wait_seconds": 0.108197,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "89523c795b6e63370b4cbd41b853febdd0952bff196f810737f7fa00971aacc4",
+ "logits.f32": {
+ "sha256": "910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2",
+ "bytes": 138588160
+ }
+ },
+ "tp1-generate-0": {
+ "json_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.327677333378233,
+ "footprint_bytes": 211330257096,
+ "disk_read_bytes": 21790720,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.327748708310537,
+ "decode_seconds": 25.991475666640326,
+ "decode_gpu_seconds": 25.323699957923964
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 66,
+ "ready_footprint_bytes": 210974232752,
+ "final_footprint_bytes": 211323834592,
+ "load_seconds": 339.81886474997737,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 106242048,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.400901,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "72bb95d9d7f7536d240068cd7d02d0b402ef7df9c7abb1940834c5875121445e",
+ "tokens.json": {
+ "sha256": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "bytes": 657
+ }
+ }
+ },
+ "1": {
+ "columns-generate-0": {
+ "json_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.4444922499824315,
+ "footprint_bytes": 211319591160,
+ "disk_read_bytes": 19808256,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.4445543749025092,
+ "decode_seconds": 40.97845041658729,
+ "decode_gpu_seconds": 28.897420239751227
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 15489,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 1274,
+ "ready_footprint_bytes": 210979688624,
+ "final_footprint_bytes": 211319476472,
+ "load_seconds": 529.2609789583366,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98435072,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.42877,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "1c42c294f246f1d625073f5e50a19fa13f08057f3f2dae0fdfc846dc4081b86d",
+ "tokens.json": {
+ "sha256": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "bytes": 657
+ }
+ },
+ "columns-teacher": {
+ "json_records": [
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 9.731449583312497,
+ "gpu_seconds": 8.83248583250679
+ },
+ {
+ "mode": "teacher",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 243,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 1005,
+ "ready_footprint_bytes": 210979737848,
+ "final_footprint_bytes": 211372724688,
+ "load_seconds": 449.76429037505295,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 151044096,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 197459968,
+ "engram_rows_requested": 12864,
+ "engram_rows_unique": 11872,
+ "engram_cache_hits": 0,
+ "engram_reads": 11870,
+ "engram_wait_seconds": 0.098543,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "04e85f3f50a8b470624dbac498a0920ae730d6394ffcbe8cbbfaf4dc5b1650b0",
+ "logits.f32": {
+ "sha256": "910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2",
+ "bytes": 138588160
+ }
+ },
+ "slots-decode-teacher": {
+ "json_records": [
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 76.39729166671168,
+ "gpu_seconds": 64.42432146286592
+ },
+ {
+ "mode": "teacher",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 10989,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 1024,
+ "ready_footprint_bytes": 210979901760,
+ "final_footprint_bytes": 211353768376,
+ "load_seconds": 391.9069152083248,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 166232064,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 214106112,
+ "engram_rows_requested": 12864,
+ "engram_rows_unique": 12864,
+ "engram_cache_hits": 0,
+ "engram_reads": 12864,
+ "engram_wait_seconds": 0.896239,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "152ef09dd29ba5fd34e9fd043541f2de0cb667250edec278a70eadc400fc106c",
+ "logits.f32": {
+ "sha256": "910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2",
+ "bytes": 138588160
+ }
+ },
+ "slots-generate-0": {
+ "json_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.3599206249928102,
+ "footprint_bytes": 211319083328,
+ "disk_read_bytes": 19906560,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3599923750152811,
+ "decode_seconds": 35.274359500035644,
+ "decode_gpu_seconds": 29.726705047884025
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 5249,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 746,
+ "ready_footprint_bytes": 210979705080,
+ "final_footprint_bytes": 211319034200,
+ "load_seconds": 320.47340358328074,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98729984,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.422371,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "3a90c738757721dbf591e3a775e6e6fe493f32c3140f51543991c8bc803f7f80",
+ "tokens.json": {
+ "sha256": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "bytes": 657
+ }
+ },
+ "slots-generate-1": {
+ "json_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.3628625000128523,
+ "footprint_bytes": 211319083424,
+ "disk_read_bytes": 19906560,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3629197083646432,
+ "decode_seconds": 35.3921791666653,
+ "decode_gpu_seconds": 29.743165998719633
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 5249,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 742,
+ "ready_footprint_bytes": 210979869016,
+ "final_footprint_bytes": 211319050680,
+ "load_seconds": 319.0521850417135,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98729984,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.431218,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "b29c9df7220517872966fcc8f0c9d837b6cc81ca53b6612cf912f2ee521b8ab1",
+ "tokens.json": {
+ "sha256": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "bytes": 657
+ }
+ },
+ "slots-generate-2": {
+ "json_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.3589045833796263,
+ "footprint_bytes": 211319148936,
+ "disk_read_bytes": 19906560,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3589659166755155,
+ "decode_seconds": 35.21327924996149,
+ "decode_gpu_seconds": 29.72321683098562
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 5249,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 751,
+ "ready_footprint_bytes": 210979869016,
+ "final_footprint_bytes": 211319099808,
+ "load_seconds": 320.506057749968,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98729984,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.425047,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "1879872ba3532e67fa2a3fa41fa7b5551aa037fe3204832972567f70f4ac33e9",
+ "tokens.json": {
+ "sha256": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "bytes": 657
+ }
+ },
+ "slots-lanes": {
+ "json_records": [
+ {
+ "mode": "lanes",
+ "slots": 2,
+ "chunk": 16,
+ "max_logit_error_slot0": 0,
+ "max_logit_error_slot1": 0,
+ "bit_disagreements": 0,
+ "position_slot0": 134,
+ "position_slot1": 134,
+ "gpu_seconds": 14.151
+ },
+ {
+ "mode": "lanes",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 1477,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 8192,
+ "consumed_tokens": 134,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 202796646400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 729,
+ "ready_footprint_bytes": 209533800408,
+ "final_footprint_bytes": 210013933720,
+ "load_seconds": 317.16930083336774,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 320700416,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 414777344,
+ "engram_rows_requested": 25728,
+ "engram_rows_unique": 24928,
+ "engram_cache_hits": 0,
+ "engram_reads": 24928,
+ "engram_wait_seconds": 0.281271,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "c21c204bb70395d33ef25f4ba3d46bd4d26deb9b7fc0745d9b7767d58bbcf6e3"
+ },
+ "slots-teacher": {
+ "json_records": [
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 9.507501541636884,
+ "gpu_seconds": 9.001792000606656
+ },
+ {
+ "mode": "teacher",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 83,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 836,
+ "ready_footprint_bytes": 210979852584,
+ "final_footprint_bytes": 211370004992,
+ "load_seconds": 382.2181032083463,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 151388160,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 197459968,
+ "engram_rows_requested": 12864,
+ "engram_rows_unique": 11872,
+ "engram_cache_hits": 0,
+ "engram_reads": 11870,
+ "engram_wait_seconds": 0.100124,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "a34f3c9e5075cf0595f02a3f0fbbd3282f6f5ef4953d2d1bb2aff2753d7c0bcf",
+ "logits.f32": {
+ "sha256": "910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2",
+ "bytes": 138588160
+ }
+ },
+ "tp1-generate-0": {
+ "json_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.333230625023134,
+ "footprint_bytes": 211330322704,
+ "disk_read_bytes": 20168704,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3333148750243708,
+ "decode_seconds": 26.13036499998998,
+ "decode_gpu_seconds": 25.422184582799673
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 67,
+ "ready_footprint_bytes": 210974347488,
+ "final_footprint_bytes": 211323982120,
+ "load_seconds": 51.176145458361134,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98516992,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.430668,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "13c74cc9650ee1eafac32fd2c9223eb008907483d91327a0c8cdb890fc8f07e2",
+ "tokens.json": {
+ "sha256": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "bytes": 657
+ }
+ },
+ "tp1-generate-1": {
+ "json_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.3297579999780282,
+ "footprint_bytes": 211330224256,
+ "disk_read_bytes": 19759104,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3298087916336954,
+ "decode_seconds": 26.11694283329416,
+ "decode_gpu_seconds": 25.41051291662734
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 67,
+ "ready_footprint_bytes": 210974249064,
+ "final_footprint_bytes": 211323818136,
+ "load_seconds": 52.83263145829551,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98156544,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.429698,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "65cd13b59ee511d0876825d999c2e3022034489f5162fca589a596a3791ee7a7",
+ "tokens.json": {
+ "sha256": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "bytes": 657
+ }
+ },
+ "tp1-generate-2": {
+ "json_records": [
+ {
+ "mode": "prefill_progress",
+ "completed_tokens": 32,
+ "prompt_tokens": 32,
+ "elapsed_seconds": 1.3348871249472722,
+ "footprint_bytes": 211330339160,
+ "disk_read_bytes": 19812352,
+ "pageins": 0
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3349456250434741,
+ "decode_seconds": 26.121429416700266,
+ "decode_gpu_seconds": 25.40447116701398
+ },
+ {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 67,
+ "ready_footprint_bytes": 210974314768,
+ "final_footprint_bytes": 211323916656,
+ "load_seconds": 50.97725549992174,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98422784,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.437327,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "321f7518abc432fe196bb78e3276ee1a2ee3bcb55993708bb07f98b205cfefe7",
+ "tokens.json": {
+ "sha256": "0f125059d45e9b79c773a6849c3115647c95a55ade930a753faf2490a8a1a9ac",
+ "bytes": 657
+ }
+ },
+ "tp1-teacher": {
+ "json_records": [
+ {
+ "mode": "teacher",
+ "tokens": 268,
+ "elapsed_seconds": 7.57047454174608,
+ "gpu_seconds": 7.429877708316781
+ },
+ {
+ "mode": "teacher",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 268,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 18,
+ "ready_footprint_bytes": 210974265544,
+ "final_footprint_bytes": 211368087968,
+ "load_seconds": 50.89376679167617,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 150749184,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 197459968,
+ "engram_rows_requested": 12864,
+ "engram_rows_unique": 11872,
+ "engram_cache_hits": 0,
+ "engram_reads": 11870,
+ "engram_wait_seconds": 0.097792,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ ],
+ "log_sha256": "d85f9abec892662467423419bb46a70ad1070afe925a1e2c3283bc2b11ca348f",
+ "logits.f32": {
+ "sha256": "910189ca833f31f7f905667f2df2f5b767391deac975dd34e7782ab694ad8eb2",
+ "bytes": 138588160
+ }
+ }
+ }
+}
diff --git a/docs/receipts/ds1017-tp2-slots-2026-10-20/measurements.json b/docs/receipts/ds1017-tp2-slots-2026-10-20/measurements.json
new file mode 100644
index 0000000000..b9fc0992ff
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-slots-2026-10-20/measurements.json
@@ -0,0 +1,565 @@
+{
+ "candidate": "cdfa5203d19805b1e544b7b39647b5bc9170e913",
+ "decode_forwards_per_generation": 127,
+ "ranks": {
+ "0": {
+ "tp1": {
+ "runs": [
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.327748708310537,
+ "decode_seconds": 25.991475666640326,
+ "decode_gpu_seconds": 25.323699957923964,
+ "forwards_per_second": 4.886217374837344,
+ "wall_minus_gpu_seconds": 0.6677757087163627,
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 66,
+ "ready_footprint_bytes": 210974232752,
+ "final_footprint_bytes": 211323834592,
+ "load_seconds": 339.81886474997737,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 106242048,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.400901,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ }
+ ],
+ "median_forwards_per_second": 4.886217374837344,
+ "median_decode_wall_seconds": 25.991475666640326,
+ "median_decode_gpu_seconds": 25.323699957923964,
+ "median_wall_minus_gpu_seconds": 0.6677757087163627
+ },
+ "columns": {
+ "runs": [
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.4458600416546687,
+ "decode_seconds": 40.978184666717425,
+ "decode_gpu_seconds": 28.809736449620686,
+ "forwards_per_second": 3.0992100072980953,
+ "wall_minus_gpu_seconds": 12.168448217096739,
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 15489,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 629,
+ "ready_footprint_bytes": 210979541216,
+ "final_footprint_bytes": 211319247144,
+ "load_seconds": 533.2622895833338,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 106987520,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.411335,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ }
+ ],
+ "median_forwards_per_second": 3.0992100072980953,
+ "median_decode_wall_seconds": 40.978184666717425,
+ "median_decode_gpu_seconds": 28.809736449620686,
+ "median_wall_minus_gpu_seconds": 12.168448217096739
+ },
+ "slots": {
+ "runs": [
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3842877499992028,
+ "decode_seconds": 35.27436200005468,
+ "decode_gpu_seconds": 29.62567888130434,
+ "forwards_per_second": 3.600348604456776,
+ "wall_minus_gpu_seconds": 5.648683118750341,
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 5249,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 90,
+ "ready_footprint_bytes": 210979508472,
+ "final_footprint_bytes": 211318657392,
+ "load_seconds": 319.91302254155744,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 106680320,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.409357,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3928204167168587,
+ "decode_seconds": 35.391926166717894,
+ "decode_gpu_seconds": 29.642197125707753,
+ "forwards_per_second": 3.588389041097999,
+ "wall_minus_gpu_seconds": 5.749729041010141,
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 5249,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 90,
+ "ready_footprint_bytes": 210979574080,
+ "final_footprint_bytes": 211318690184,
+ "load_seconds": 319.35464254161343,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 106676224,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.407941,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3941821666667238,
+ "decode_seconds": 35.21301570837386,
+ "decode_gpu_seconds": 29.64614437648561,
+ "forwards_per_second": 3.6066209452716276,
+ "wall_minus_gpu_seconds": 5.566871331888251,
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 5249,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 89,
+ "ready_footprint_bytes": 210979606776,
+ "final_footprint_bytes": 211318706496,
+ "load_seconds": 319.82512804167345,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 106676224,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.413062,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ }
+ ],
+ "median_forwards_per_second": 3.600348604456776,
+ "median_decode_wall_seconds": 35.27436200005468,
+ "median_decode_gpu_seconds": 29.642197125707753,
+ "median_wall_minus_gpu_seconds": 5.648683118750341
+ },
+ "lane_state": {
+ "mode": "lanes",
+ "slots": 2,
+ "chunk": 16,
+ "max_logit_error_slot0": 0,
+ "max_logit_error_slot1": 0,
+ "bit_disagreements": 0,
+ "position_slot0": 134,
+ "position_slot1": 134,
+ "gpu_seconds": 14.114
+ },
+ "slots_vs_columns_speedup": 1.161698818724316,
+ "slots_vs_tp1_speedup": 0.7368375838122895
+ },
+ "1": {
+ "tp1": {
+ "runs": [
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3333148750243708,
+ "decode_seconds": 26.13036499998998,
+ "decode_gpu_seconds": 25.422184582799673,
+ "forwards_per_second": 4.860245924618685,
+ "wall_minus_gpu_seconds": 0.7081804171903059,
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 67,
+ "ready_footprint_bytes": 210974347488,
+ "final_footprint_bytes": 211323982120,
+ "load_seconds": 51.176145458361134,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98516992,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.430668,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3298087916336954,
+ "decode_seconds": 26.11694283329416,
+ "decode_gpu_seconds": 25.41051291662734,
+ "forwards_per_second": 4.8627437296412435,
+ "wall_minus_gpu_seconds": 0.7064299166668206,
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 67,
+ "ready_footprint_bytes": 210974249064,
+ "final_footprint_bytes": 211323818136,
+ "load_seconds": 52.83263145829551,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98156544,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.429698,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3349456250434741,
+ "decode_seconds": 26.121429416700266,
+ "decode_gpu_seconds": 25.40447116701398,
+ "forwards_per_second": 4.861908510979297,
+ "wall_minus_gpu_seconds": 0.7169582496862859,
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 1,
+ "tensor_parallel_rank": 0,
+ "tensor_parallel_partition": "none",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 0,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204238438400,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 67,
+ "ready_footprint_bytes": 210974314768,
+ "final_footprint_bytes": 211323916656,
+ "load_seconds": 50.97725549992174,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98422784,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.437327,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ }
+ ],
+ "median_forwards_per_second": 4.861908510979297,
+ "median_decode_wall_seconds": 26.121429416700266,
+ "median_decode_gpu_seconds": 25.41051291662734,
+ "median_wall_minus_gpu_seconds": 0.7081804171903059
+ },
+ "columns": {
+ "runs": [
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.4445543749025092,
+ "decode_seconds": 40.97845041658729,
+ "decode_gpu_seconds": 28.897420239751227,
+ "forwards_per_second": 3.099189908571868,
+ "wall_minus_gpu_seconds": 12.081030176836066,
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_output_columns_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 15489,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242370560,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 1274,
+ "ready_footprint_bytes": 210979688624,
+ "final_footprint_bytes": 211319476472,
+ "load_seconds": 529.2609789583366,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98435072,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.42877,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ }
+ ],
+ "median_forwards_per_second": 3.099189908571868,
+ "median_decode_wall_seconds": 40.97845041658729,
+ "median_decode_gpu_seconds": 28.897420239751227,
+ "median_wall_minus_gpu_seconds": 12.081030176836066
+ },
+ "slots": {
+ "runs": [
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3599923750152811,
+ "decode_seconds": 35.274359500035644,
+ "decode_gpu_seconds": 29.726705047884025,
+ "forwards_per_second": 3.6003488596262585,
+ "wall_minus_gpu_seconds": 5.547654452151619,
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 5249,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 746,
+ "ready_footprint_bytes": 210979705080,
+ "final_footprint_bytes": 211319034200,
+ "load_seconds": 320.47340358328074,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98729984,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.422371,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3629197083646432,
+ "decode_seconds": 35.3921791666653,
+ "decode_gpu_seconds": 29.743165998719633,
+ "forwards_per_second": 3.5883633896049276,
+ "wall_minus_gpu_seconds": 5.649013167945668,
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 5249,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 742,
+ "ready_footprint_bytes": 210979869016,
+ "final_footprint_bytes": 211319050680,
+ "load_seconds": 319.0521850417135,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98729984,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.431218,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ },
+ {
+ "mode": "generate",
+ "prompt_tokens": 32,
+ "completion_tokens": 128,
+ "prefill_seconds": 1.3589659166755155,
+ "decode_seconds": 35.21327924996149,
+ "decode_gpu_seconds": 29.72321683098562,
+ "forwards_per_second": 3.6065939527668074,
+ "wall_minus_gpu_seconds": 5.490062418975867,
+ "residency": {
+ "mode": "generate",
+ "tensor_parallel_ranks": 2,
+ "tensor_parallel_rank": 1,
+ "tensor_parallel_partition": "moe_route_slots_replicated_weights",
+ "attention_parallel": true,
+ "tensor_parallel_exchanges": 5249,
+ "evidence_origin": "current_invocation_not_historical_qualification",
+ "context_capacity": 1048576,
+ "consumed_tokens": 159,
+ "weight_bytes": 202366929272,
+ "resident_allocation_bytes": 204242501632,
+ "weight_residency_sets": 19,
+ "residency_refresh_ticks": 751,
+ "ready_footprint_bytes": 210979869016,
+ "final_footprint_bytes": 211319099808,
+ "load_seconds": 320.506057749968,
+ "measurement_scope": "model_execution",
+ "execution_disk_read_bytes": 98729984,
+ "execution_pageins": 0,
+ "engram": "streamed",
+ "engram_read_bytes": 126844928,
+ "engram_rows_requested": 7632,
+ "engram_rows_unique": 7632,
+ "engram_cache_hits": 0,
+ "engram_reads": 7632,
+ "engram_wait_seconds": 0.425047,
+ "resident_gate_passed": true,
+ "execution_error": null
+ }
+ }
+ ],
+ "median_forwards_per_second": 3.6003488596262585,
+ "median_decode_wall_seconds": 35.274359500035644,
+ "median_decode_gpu_seconds": 29.726705047884025,
+ "median_wall_minus_gpu_seconds": 5.547654452151619
+ },
+ "lane_state": {
+ "mode": "lanes",
+ "slots": 2,
+ "chunk": 16,
+ "max_logit_error_slot0": 0,
+ "max_logit_error_slot1": 0,
+ "bit_disagreements": 0,
+ "position_slot0": 134,
+ "position_slot1": 134,
+ "gpu_seconds": 14.151
+ },
+ "slots_vs_columns_speedup": 1.1617064348552066,
+ "slots_vs_tp1_speedup": 0.7405217213560993
+ }
+ },
+ "failed_residency_gates": []
+}
diff --git a/docs/receipts/ds1017-tp2-slots-2026-10-20/restoration.json b/docs/receipts/ds1017-tp2-slots-2026-10-20/restoration.json
new file mode 100644
index 0000000000..3a0fbaeb7b
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-slots-2026-10-20/restoration.json
@@ -0,0 +1,44 @@
+{
+ "verified_at_utc": "2026-10-19T23:49:10.139856+00:00",
+ "controller_exit": 0,
+ "ranks": {
+ "0": {
+ "window_exit": 0,
+ "original_qwen_reply": "4",
+ "health": "ok",
+ "warming": false,
+ "own_test_processes": [],
+ "locks": {
+ ".tp-window-lock": false,
+ ".r3-window-lock": false,
+ ".s1-copy-lock": false
+ },
+ "test_ports_clear": true
+ },
+ "1": {
+ "window_exit": 0,
+ "original_qwen_reply": "4",
+ "health": "ok",
+ "warming": false,
+ "own_test_processes": [],
+ "locks": {
+ ".tp-window-lock": false,
+ ".r3-window-lock": false,
+ ".s1-copy-lock": false
+ },
+ "test_ports_clear": true
+ }
+ },
+ "original_reranker_after_both": {
+ "origin": "synthetic reranker availability control",
+ "health": {
+ "status": "ok"
+ },
+ "model": "qwen3-reranker-0.6b",
+ "scores": {
+ "0": 0.9979732632637024,
+ "1": 1.4130221643426921e-05
+ },
+ "correct_document_ranked_first": true
+ }
+}
diff --git a/docs/receipts/ds1017-tp2-slots-2026-10-20/sha256.json b/docs/receipts/ds1017-tp2-slots-2026-10-20/sha256.json
new file mode 100644
index 0000000000..933c7692f7
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-slots-2026-10-20/sha256.json
@@ -0,0 +1,10 @@
+{
+ "measurements.json": "0366ee923947a27024997239b057c433e3327bd0982f0574fa0924a8f67412d6",
+ "restoration.json": "0eacce2c9c337a03695050946e54d1602b3a739adb343f47fd8b7162b9f5cb8e",
+ "checks.json": "4de64f53600f933cc85dc2ad6ef77dc67e6fdf56d7e039f8ebf358e68939c410",
+ "README.md": "3c5b700ea3a01c8dd045764af879e46de384b1c583c256c20c44844eac3b7400",
+ "transport.json": "c97c0105006d0208b637861e9c7af23dda6dcf7aedd4fb22490fc8b805e82110",
+ "checkpoint-verification.json": "7b5fae2bfcedba8f6bfcddf7de382f3cab944d71668cb3d4e1f7b06dc4b02127",
+ "execution.json": "d7126668fe76fd7e167cd4844823b7c6cae533d0fce42062ed88cc8209530b81",
+ "build.json": "fc82f5d4f486b6e530d9b845036d73d95e001af2b8715c1e4a1216db4eeb41c7"
+}
diff --git a/docs/receipts/ds1017-tp2-slots-2026-10-20/transport.json b/docs/receipts/ds1017-tp2-slots-2026-10-20/transport.json
new file mode 100644
index 0000000000..8266a1f416
--- /dev/null
+++ b/docs/receipts/ds1017-tp2-slots-2026-10-20/transport.json
@@ -0,0 +1,17 @@
+{
+ "transport": "ordinary TCP over TB5 IP interfaces",
+ "rdma_device_used": false,
+ "observations": {
+ "0": {
+ "at_utc": "2026-10-19T23:08:54.299185+00:00",
+ "socket_addresses": "p68699\nctf-dsv41-run.cdfa5203\nf10\nn169.254.75.241:19816->169.254.92.133:63025\nTST=ESTABLISHED\nTQR=0\nTQS=0\nf11\nn169.254.75.241:19817->169.254.92.133:63026\nTST=ESTABLISHED\nTQR=0\nTQS=0\n",
+ "nettop_csv": "time,,interface,state,bytes_in,bytes_out,rx_dupe,rx_ooo,re-tx,rtt_avg,rcvsize,tx_win,tc_class,tc_mgt,cc_algo,P,C,R,W,arch,\n01:08:54.298028,tf-dsv41-run.cd.68699,,,17433504,17433504,0,0,0,,,,,,,,,,,,\n01:08:54.297703,tcp4 169.254.75.241:19816<->169.254.92.133:63025,bridge0,Established,17433400,17433400,0,0,0,1.22 ms,4194240,3567232,BE,-,cubic,-,-,-,-,so,\n01:08:54.297729,tcp4 169.254.75.241:19817<->169.254.92.133:63026,bridge0,Established,104,104,0,0,0,1.00 ms,131712,131712,BE,-,cubic,-,-,-,-,so,\n"
+ },
+ "1": {
+ "at_utc": "2026-10-19T23:08:54.322437+00:00",
+ "socket_addresses": "p47316\nctf-dsv41-run.cdfa5203\nf9\nn169.254.92.133:63025->169.254.75.241:19816\nTST=ESTABLISHED\nTQR=0\nTQS=0\nf10\nn169.254.92.133:63026->169.254.75.241:19817\nTST=ESTABLISHED\nTQR=0\nTQS=0\n",
+ "nettop_csv": "time,,interface,state,bytes_in,bytes_out,rx_dupe,rx_ooo,re-tx,rtt_avg,rcvsize,tx_win,tc_class,tc_mgt,cc_algo,P,C,R,W,arch,\n01:08:54.318483,tf-dsv41-run.cd.47316,,,17433504,17433504,0,0,0,,,,,,,,,,,,\n01:08:54.318020,tcp4 169.254.92.133:63025<->169.254.75.241:19816,en3,Established,17433400,17433400,0,0,0,1.22 ms,3567176,4194240,BE,-,cubic,-,-,-,-,so,\n01:08:54.318045,tcp4 169.254.92.133:63026<->169.254.75.241:19817,en3,Established,104,104,0,0,0,1.00 ms,131712,131712,BE,-,cubic,-,-,-,-,so,\n"
+ }
+ },
+ "scope": "actual established data and acknowledgement sockets; snapshot counters, not a standalone throughput benchmark"
+}
diff --git a/docs/recipes/README.md b/docs/recipes/README.md
index e8094405d7..f923176e96 100644
--- a/docs/recipes/README.md
+++ b/docs/recipes/README.md
@@ -20,6 +20,7 @@ For agent-assisted contributions, read the [TensorFold contributor skill](SKILL.
| DeepSeek-V4-Flash | [MLX on a 256 GB Mac, DSpark and MTP drafts](deepseek-v4-flash.md) |
| Qwen3.6-35B-A3B | [MLX with MTP drafts, one-GPU CUDA](qwen3.6-moe.md) |
| Step-5-Preview | [Native Metal on one 256 GB Mac: 2-bit experts, MTP drafts, images](step-5-preview.md) |
+| Kolibri-1 | [Native CUDA, block FP8, research checkpoint](kolibri-1.md) |
## Capability floor
diff --git a/docs/recipes/adding-living-weights-to-a-family.md b/docs/recipes/adding-living-weights-to-a-family.md
new file mode 100644
index 0000000000..66b06a2ca9
--- /dev/null
+++ b/docs/recipes/adding-living-weights-to-a-family.md
@@ -0,0 +1,212 @@
+# Adding Living Weights to a family
+
+This is how a family on the Zig engine learns facts with `--slide`. [Living Weights](../living-weights.md) is one
+learner for every family (`zig/src/living`): it writes lessons, reads the model, fits a pocket of new neurons and saves
+them into the checkpoint's own shards. A family describes itself as data, and its host and trainer give the learner a
+few hooks. The steps are in order. It comes from three families:
+
+- Nemotron 3.5 Lightning on Metal and CUDA, the first family and the only one listed in `living/families.zig` today.
+ Its relu^2 shared experts carry the pocket at the MoE layers.
+- Qwen3.8 Flash Next on CUDA (#895 and #896). A SwiGLU shared expert behind its own gate at every layer, with
+ hyper-connections, Gated DeltaNet and gated attention below it.
+- Qwen3.8-27B on CUDA, a port on #726's branch that is not a pull request yet. A dense SwiGLU MLP at every layer, with
+ no experts at all.
+
+Only Nemotron learns in this tree. Flash Next's and the 27B's learning have not landed, so their file names and
+numbers below come from those branches.
+
+Bring the family up on the Zig engine first, exact and at speed ([adding a Zig family](adding-a-zig-family.md)). The
+learner trains through the engine's own forward, so everything below assumes that forward is done.
+
+## How to use it
+
+1. Do the steps in order. Each ends in a check that has to pass before the next step means anything.
+2. Never teach the original folder. Teach a `cp -RL` copy (`cp -cRL` on a Mac), and check the original's sha256
+ after each run.
+3. Put the receipts at the end of this page in the PR: an untaught folder serves as before, the trainer's forward is
+ the serving forward, lessons are kept, and a taught folder serves.
+
+## 1. Describe the family as data
+
+Add a `Family` to `living/families.zig`. The learner, the shard writer and the server's Living Weights view all read
+it, by the checkpoint's `model_type`.
+
+| Field | Nemotron (`nemotron_h`) | Flash Next (`qwen4_exp`) | Qwen3.8-27B (`qwen3_5`) |
+| --- | --- | --- | --- |
+| `names.layers`, `names.expert` | `backbone.layers.`, `mixer.shared_experts.` | `language_model.model.layers.`, `mlp.shared_expert.` | `language_model.model.layers.`, `mlp.` |
+| `names.inputs` | `up_proj` | `gate_proj`, `up_proj` | `gate_proj`, `up_proj` |
+| `names.mtp` | its own file, `mtp-4bit.safetensors` | in the index, `language_model.mtp.layers.0.mlp.shared_expert.` | none |
+| `neuron` | relu^2, margin 0.25 | SwiGLU, gated, margin 0.5, sharp 16 | SwiGLU, margin 0.5, sharp 16 |
+| `quant` | 4-bit, groups of 64 | 4-bit, groups of 32 | 4-bit, groups of 64 |
+| `keys` | `moe_shared_expert_intermediate_size` at the top level | `shared_expert_intermediate_size` in `text_config` | `intermediate_size` in `text_config` |
+| `experts` | `.pattern`: the MoE layers of `hybrid_override_pattern` | `.every` | `.every` |
+
+**Where the pocket goes.** A pocket is 64 neurons a layer (`Family.neurons`, a whole number of quantization groups),
+zero until a fact uses one. Each is one more row of every input projection and one more column of the output
+projection, so the first save makes the projection 64 neurons wider and any MLX loader reads a slightly wider model.
+
+- **An MoE layer with a shared expert:** beside the shared expert, as Nemotron and Flash Next do. The routed experts
+ are left alone: a pocket there would fire only when its expert is picked.
+- **A shared expert behind a gate:** set `neuron.gated`. Flash Next scales its shared expert by
+ `sigmoid(shared_expert_gate x)`, so a pocket neuron adds `gate act(x) v` and its output is fitted through the gate.
+ The trainer reports the gate at each row (`gateAt`).
+- **A dense MLP:** the MLP is the expert (`expert = "mlp."`, `experts = .every`), and the width key is the MLP's
+ (the 27B's 17,408 neurons become 17,472).
+
+**The neuron's kind.** A relu^2 neuron is exactly zero below its threshold. A SwiGLU neuron is not: `silu` leaks
+below zero. A SwiGLU neuron fitted as `u` is written as gate row `sharp u` and up row `u / sharp`
+(`living/neuron.zig`), so at z = u x it fires `z^2 sigmoid(sharp z)`, which is relu(z)^2 only where z is well above
+zero: `neuron.zig`'s test checks the two agree within 1e-6, relative, at z = 1, 1.5 and 3 with `sharp` 16, and at
+z = 0.1 they differ by about 0.0017. The output fit reads each neuron through its own activation (`Pocket.act`), not
+as relu^2. Rows a neuron stays silent on are fitted to z at or below `-margin`, and with margin 0.5 what leaks there
+is at most `0.25 sigmoid(-8)`, about 8.4e-5 (the test checks z from -0.5 down to -20). Keep `sharp` a power of two, so
+the up row gives `u` back bit for bit in fp32 while `u / sharp` stays a normal number and `sharp u` finite. A save
+still rounds every row to the family's affine format, so the checks read the rows as saved (#877).
+
+`Family.check()` refuses a description whose inputs do not match the neuron, whose code width is 0 or does not divide
+a 32-bit word, whose group or pocket is empty or not a whole number of groups, or, for SwiGLU, whose `sharp` is not a
+whole power of two from 1 up (`living/family.zig`). Add your family to the tests in `families.zig` and `family.zig`:
+its model type is found, and a tensor name of its expert gives the layer back while a similar one (a gate, the MTP
+head's) does not.
+
+## 2. The pocket in every pass
+
+The engine adds the pocket wherever the model runs: prompt chunks, verify windows and decode rounds. Load it from the
+shards as fp32 rows beside the packed weights (Flash Next: `cuda_pocket.zig`; the 27B: its `pocket.zig`), and take
+the shard's projection apart at the old width.
+
+- **An untaught folder's bits do not change.** A layer with no neuron in use launches nothing. Flash Next sums a
+ pocket layer's MoE output in fp32, adds the pocket and rounds once; every other layer writes back exactly as before.
+ Check the untaught folder against the last release: the same replies, drafted and `"draft": false`, and the same
+ speed.
+- **Row by row.** The pocket's kernels read each row alone, so drafted still equals `"draft": false` and a concurrent
+ stream equals its solo run. Flash Next uses the two-kernel form of #787 (`tf_fn_pocket_in`, then `_out`).
+- **Untaught streams.** A request with `untaught` set (the lesson writer's and the judge's, in
+ `server/slide_ask.zig`) reads the model as shipped. Zero the pocket's output on its rows. The 27B missed this at
+ first: the judge then read the lesson's own neurons, called a reworded "I don't know" a changed answer, and took a
+ good lesson out.
+- **The config check accepts a taught folder.** If the engine checks the checkpoint's widths at load, subtract
+ `tensorfold_learned_neurons` from the expert's width first. Flash Next's check refused its own taught folder at the
+ first restart.
+- **The MTP head.** The first save widens the MTP head's shared expert with zero neurons, so its tensors stay one
+ width. Drafts are made without the pocket; the verify rows run it, so drafted replies still equal plain ones.
+
+## 3. The host
+
+The host is a type the learner is generic over (`Learner(H)` in `living/learner.zig`). A family's slide file
+(Nemotron's `cuda_slide.zig`, Flash Next's in #896) declares it.
+
+- **Always:** `Trainer`, `max_rows`, `Ctx`, `family`, `layout(ctx)`, `trainer(gpa, io, ctx)`, `free(t, gpa)` and
+ `seconds(io)`.
+- **The pocket, relu^2:** `pocket(ctx, layer, up, down, used)` hands the forward a layer's neurons;
+ `pocketRead(ctx, layer, up, down) usize` gives back the neurons loaded from the shards; and
+ `pocketSave(ctx, gpa, io, layers, neurons, up, down) usize` writes every layer and hands the rows back as written.
+- **The pocket, SwiGLU:** `pocketSwiglu`, `pocketReadSwiglu` and `pocketSaveSwiglu`, the same three with a `gate`
+ beside `up`.
+- `up` and `gate` are each neuron's input rows as the forward multiplies them (`[neurons, D]` f32), and `down` is its
+ output. A save hands the rows back rounded as the shards hold them (#877 checks every lesson on those).
+- **Optional:** `attach`, `write` and `layers` (Nemotron's routed blocks), and `codes`, `commit` and `fold4`
+ (weight edits). The learner offers them only when the trainer has `sites`.
+
+`layout(ctx)` is a `Layout`: the hidden size, the expert's own width with any saved pocket left out, the layer count,
+the layers whose expert carries the pocket (`widen`) and the ones lessons put neurons in (`learn`).
+`Layout.fromExperts` widens every layer that has an expert and learns at all but the last, whose nudge has no
+gradient to read. Read the widths from `config.json` at load, as Flash Next's `layoutOf` does.
+
+## 4. The trainer
+
+The trainer runs the learner's passes on the engine: a keys pass (the rows a pocket reads), a loss pass, and a target
+pass that runs the loss's gradient back to the lowest nudged layer.
+
+- **What it gives:** `normed(layer, row)` (a row's input to the expert, as the forward normed it), `pass(seqs, mode,
+ turn, out)` and `step(ids, start, mode, turn)`, `spans`, `packs()`, `pack`, `count`, `prefixes`,
+ `prefixSlots(n)`, `pushAt(i, key, layer)` (the gradient's push at a nudged row), `deltaAt(i, key, layer)`,
+ `residual(layer, row)`, `nudge` and `from`. A gated family also gives `gateAt(layer, row)`.
+- **Optional:** `tapRows` (#891) reads a question asked after a conversation through the engine's own prompt chunks,
+ so a pocket can read it in context. Nemotron's routed blocks, edits and probes also use `sites`, `sketched`,
+ `score()`, `projected()`, `key()` and `config()`.
+- **Sequences side by side.** A pass packs several sequences into one forward (Flash Next 1,024 rows, the 27B 1,024
+ rows over up to 16 sequences). Row-wise launches run over every row; a recurrent mixer or attention runs each
+ sequence from position 0. A sequence's loss in a pack equals its loss alone, exactly.
+- **What the forward keeps.** The backward reads what the forward computed: each layer's expert input and its routing,
+ and, from the lowest layer that learns, every mixer's projections, the norms' inputs, the gates. Keep them in the
+ engine's forward rather than in a second forward. Flash Next's batched pass keeps them on request (`Keep`): every
+ layer's expert input, and the rest from `layers / 5` up, as the 27B's does.
+- **The backward kernels.** One input-gradient kernel set for each layer type above the lowest learned layer, checked
+ against float64 with central differences. What another family could use goes in the core, a family's own in its
+ folder (#895):
+ - core: a Gated DeltaNet layer (`cuda.gdn_train`), 4-bit products at the checkpoint's group, routed SwiGLU experts
+ and top-k routing (`cuda.train_q4`), gated attention (`cuda.train_attn`), Nemotron's Mamba (`train.cu`);
+ - family: Flash Next's hyper-connections and its gated pocket (`flashnext_train.cu`); the 27B's dense SwiGLU MLP and
+ its ungated pocket.
+- **Memory.** On a GB10 the trainer keeps MLX copies of the small linears its backward reads, and its keep lists. It
+ took about 26 GiB for Nemotron, 10.6 GiB for Flash Next and 23 GiB for the 27B. `open` reports it as
+ `learn_bytes`, and the native host keeps it out of the streams' room.
+
+**Checks.** Add a `tf-cuda-test` case for each backward kernel (Flash Next: `gdn-back`, `q4-back`, `moe-back`,
+`attn-back`, `fn-hc-back`, `fn-moe-back`, `fn-pocket`), and a tool that runs the trainer on the real checkpoint
+(`flashnext-learn`):
+
+- the trainer's keys and loss passes equal the serving forward byte for byte, untaught and with a pocket neuron;
+- a row's scores never depend on later rows;
+- a pack of sequences gives each one's loss exactly as alone;
+- a pocket neuron moves the forward by exactly `gate act v` at its row.
+
+A finite difference against the gradient is noisy in bf16. Below about one ulp of the residual the rounding decides,
+and a large step leaves the linear range. Flash Next's direct path agreed within a median of 6% in three windows of
+four, and its cross-row paths stayed noisy. The 27B's direct rows gave 2-36%. Treat it as a smoke check: the kernels'
+float64 tests and the identity checks above are the real ones, and a lesson that is kept is the end-to-end one.
+
+## 5. The shard writer
+
+`living/shards.zig` writes any family's pocket: `Io(core).write(gpa, io, dir, family, layout, layers, neurons, inputs,
+down)` rounds the rows to the family's affine format (`living/affine.zig`), widens every expert on the first save
+(the MTP head's with zeros), records `tensorfold_learned_neurons` in the family's key scope, and writes a later save in
+place. Wrap it in the family's folder (Flash Next's `living.zig`, `Shards(core)`).
+
+Test it on a small fake checkpoint (Nemotron's and Flash Next's `pocket_shards_test.zig`): the first save widens every
+expert and tells `config.json`; a later save lands in place; every other tensor's bytes are kept; and the rows read
+back are the rows written.
+
+## 6. `--slide` on the native host
+
+The native host (`native/cuda.zig`) serves `--slide` for a family whose module declares:
+
+- `slide`, a type whose `Learner` is `living.learner.Learner(Host)`, with `Model`, `Example`, `Lesson`, `Report` and
+ `Step`;
+- `learnModel(ctx, dir)`, the learner's `Model` for a loaded engine;
+- `linked(arena, io, dir)`, the first file a lesson would rewrite (config, index, a shard holding an expert) that is a
+ link, so a folder of links into the Hugging Face cache is refused at startup.
+
+`open` with `.slide = true` reserves the trainer's rows in the pass's scratch and reads the layout from `config.json`.
+The host already refuses `--slide` off a GB10 (sm_121) and with `--segments`. A family adds its own refusals: Flash
+Next refuses a captured kernel set, whose read-out kernels keep nothing a backward reads. Add each refusal's message
+to the table in [Living Weights](../living-weights.md), and a line for the family under "What you need".
+
+## Receipts to produce
+
+On a fresh copy for each run, greedy, with the checkpoint's revision, the GPU, driver and CUDA version:
+
+- **Untaught, as before.** Against the last release on the same machine: the same replies drafted and with
+ `"draft": false`, decode and cold-prompt speed, and the model's memory.
+- **Trainer.** The kernel checks and the identity checks of step 4, and the finite-difference numbers as measured.
+- **Lessons.** A handful of facts about the user, one learn request each (Flash Next taught five, Nemotron ten). For
+ each: kept or not, rounds and steps, seconds, and its held-out wordings right after its lesson and after the last.
+ Ask wordings no lesson trained on. Then a fixed set of unrelated prompts against the untaught replies: #891 used
+ twelve (capitals, arithmetic, a haiku, a Python one-liner, a translation and "What is my cat called?").
+- **A taught folder serves.** Restart without `--slide` and with it: the same replies as after the last lesson.
+ Drafted equals `"draft": false`, concurrent equals solo, a resumed prompt equals a fresh one, and the decode cost of
+ the pocket (Flash Next: 5% drafted and 1% plain with three facts).
+- **Tests.** A plain `zig build` (every step), `zig build test-cuda-host` and `zig build test-living` from a fresh
+ cache, `zig fmt --check build.zig zig`, and `python3 tools/zig/lean_check.py` with no new problems.
+
+## Traps
+
+- **A finished lesson can still hurt later ones.** A pocket neuron's output reaches higher layers, so a lower pocket
+ neuron can push a higher one past its threshold. On the 27B, after 16 facts, seven of 40 wide unrelated prompts gave
+ garbage tokens: one neuron leaking +0.4 on LaTeX digits set off about 100 others, layer by layer. The rows that set it
+ off were in no prompt a lesson read. Read unrelated prompts in several languages, code, math and tables while you
+ teach, and check them after every lesson, not only at the end.
+- **The checks only see what they ask.** Lessons ask single-turn questions. A fact can come back cold and miss after a
+ long conversation (#891).
+- **A captured kernel set keeps nothing.** If a family still has a captured comparison set, `--slide` must refuse it.
diff --git a/docs/recipes/flash-next-vision.md b/docs/recipes/flash-next-vision.md
index 53a6d3fe7d..2a538541a2 100644
--- a/docs/recipes/flash-next-vision.md
+++ b/docs/recipes/flash-next-vision.md
@@ -2,9 +2,9 @@
This recipe describes the Python engine, frozen at 0.6.6 and available at the
[`v0.6.6` tag](https://github.com/ashhart/TensorFold/tree/v0.6.6). @shantanugoel added Flash Next images on CUDA in
-[#146](https://github.com/ashhart/TensorFold/pull/146), adapting @MiaAI-Lab's vision patch. The native binary refuses
-`--vision`, has no Flash Next CUDA backend, reads no vision tower and does not read the `TENSORFOLD_VISION_*`
-variables below.
+[#146](https://github.com/ashhart/TensorFold/pull/146), adapting @MiaAI-Lab's vision patch. The native binary has no Flash Next CUDA backend and does not read the
+`TENSORFOLD_VISION_*` variables below; on a Mac it reads Flash Next images and videos itself (see
+[Qwen3.8 Flash Next](../vision.md#qwen38-flash-next)).
Run a complete local Flash Next checkpoint with `--vision --parallel 2` (or more), on one CUDA GPU.
This port supports images, including multiple images within the existing 4096 visual-token budget; video and
diff --git a/docs/recipes/kolibri-1.md b/docs/recipes/kolibri-1.md
new file mode 100644
index 0000000000..9601290198
--- /dev/null
+++ b/docs/recipes/kolibri-1.md
@@ -0,0 +1,86 @@
+# Kolibri-1
+
+This page describes the native engine. `kolibri1` serves Aleph Alpha's Kolibri-1, a research checkpoint, on one
+NVIDIA GPU through the lane host. It is a draft family: plain decoding and copy drafts from the prompt's own text are
+in; the EAGLE-3 drafter is the next step and is not loaded here.
+
+## Checkpoint
+
+Tested revision: `Aleph-Alpha/Kolibri-1` at `e52eb4627d11516b0c01de49210ab5a4e4061444` (32 shards, 74 GB). The
+weights are block FP8: e4m3 codes in 128 x 128 blocks with fp32 `weight_scale_inv`, the routers, norms, embeddings and
+head in bf16. Publisher sha256 of `model.safetensors.index.json`:
+`c6769a34820c9057b404b2a5cae1ac24fd7b4a9ca08b84eb0a06e7b5bad3005e`.
+
+```bash
+tensorfold-native serve /path/to/Kolibri-1 --name kolibri-1 --context 32768 --parallel 8
+```
+
+The model has 50 layers, 2,560 wide, 48 query heads over 4 key heads of 128. Every fifth layer (4, 9, ..., 49) attends
+to every key without positions; the other 40 attend to a 513-key window with RoPE. Each layer routes by sigmoid with an
+expert bias to 6 of 384 experts, weighted by the unnormalised sigmoid, plus one ungated shared expert.
+
+## What the family adds
+
+It brings no kernels of its own. Every launch is a shared CUDA operator:
+
+- `cuda/fp8.zig` and `qlinear` for the attention projections (block FP8, `fp8g` lane tiles);
+- `cuda/experts.zig` for the routed and shared experts (`fp8g`, the shared expert stacked last at weight 1);
+- `qk_rows` (QK norm, RoPE, the cache write), `norms`, `embed_rows`, `moe_glue` (sigmoid top-k with bias),
+ `dense_rows` (router and head);
+- `window_attention` for prompt rows of every layer (the full layers with no window) and for sliding decode rows;
+- `tree_attention` for the full layers' decode rows over committed keys and each window's chain.
+
+The family supplies its config check, the tensor map (116,303 tensors, none left over), the packing into those
+layouts, the layer graph and the lane backend. One-row steps replay a CUDA graph a stream; wider windows and shared
+rounds run eager. Both give a row the same bits.
+
+## Serving
+
+- Prompts run in passes of up to 8,192 rows, as the Python engine ran them, one a step, so other streams' rounds run
+ between them (a full pass holds them for about 3.7 s). A row's bits do not depend on its chunk: 1,000-, 1,536- and
+ 2,048-row cuts of a 5,000- and a 20,000-token prompt give the 8,192-row passes' logits byte for byte.
+- Concurrent streams share rounds of up to 64 rows. Each stream's caches are allocated at load, one per `--parallel`
+ stream: at a 32k window 1.29 GiB a stream, at 64k 1.92 GiB. Each sliding layer's ring holds a pass and its window
+ (8,768 keys).
+- The prompt cache keeps a stream's state at any row: full layers' keys up to it and each sliding layer's last 512.
+- Drafts: the lane core's copy drafts verify in windows of up to 16 rows. There is no draft head yet, so a request
+ for head drafts and `--drafter` are refused by name.
+- Refused at load: other quantizations, `norm_topk_prob`, other layer kinds, a config with a vision tower, shapes the
+ shared kernels do not take, `--vision`, `--precision` and a captured kernel set (`TENSORFOLD_CUDA_KERNELS`).
+
+## Checks
+
+```bash
+zig build -Doptimize=ReleaseFast -Dnvcc=/usr/local/cuda/bin/nvcc
+zig-out/bin/tf-cuda-test kolibri-reference MODEL IDS # layer 0 and the head against float64 from the checkpoint
+zig-out/bin/tf-cuda-test kolibri-layers MODEL IDS # the 513-key window edge, layer 4's full reach
+zig-out/bin/tf-cuda-test kolibri-full MODEL IDS 128 # steps, drafted, sampled, shared rounds, cuts, kept states
+zig-out/bin/tf-cuda-test kolibri-greedy MODEL PROMPTS N OUT # greedy ids and top-2 margins a step
+zig-out/bin/tf-cuda-test kolibri-logits MODEL IDS OUT # teacher-forced logits of 2,048 rows, for quality
+```
+
+`IDS` is a file of real-text token ids (20,000 or more for `kolibri-full`, 3,000 for `kolibri-layers`). Random ids give
+flat logits and say nothing about quality.
+
+The native rows are not byte-equal to the Python engine's: its glue was Triton, these are the shared CUDA operators,
+and their fp32 roundings differ by an ulp here and there. Kolibri-1's top-6-of-384 routing turns such differences into
+different experts on near-tied rows, so greedy replies part where two tokens are nearly tied. On 2,048 rows of real
+text, teacher-forced: next-token top-1 agrees on 92.3% of rows, KL 0.06; NLL 3.574 here against 3.578 for the
+Python engine, top-1 35.96% against 35.47%, mean entropy 2.354 against 2.358.
+
+## Speed
+
+GB10, greedy, served by `tensorfold-native` with copy drafts on:
+
+| | tok/s |
+| --- | ---: |
+| One stream, code / chat (`bench_openai.py`, 256 tokens) | 51.9 / 49.8 |
+| 4 streams, aggregate | 124.0 / 126.0 |
+| 8 streams, aggregate | 175.5 / 183.1 |
+
+Cold prompts (`bench_cache.py --require-cold --repeats 3`, `--prompt-cache-gib 0`, median of the cold requests):
+2,048 tokens 0.97 s, 8,192 tokens 3.72 s, 32,768 tokens 16.8 s, 64,000 tokens 37.8 s to the first token.
+
+## Next
+
+- The EAGLE-3 drafter through the lane core's drafter interface.
diff --git a/docs/recipes/qwen3.8-flash-next.md b/docs/recipes/qwen3.8-flash-next.md
index 079ac82040..c2ab98761f 100644
--- a/docs/recipes/qwen3.8-flash-next.md
+++ b/docs/recipes/qwen3.8-flash-next.md
@@ -100,6 +100,26 @@ budget. Shared sessions draw sampled tokens with the same GPU sampler as the sin
Each packed window uses its own seed, sampling settings and absolute token positions. The two-Mac path remains
greedy only. The model gate below must verify sampled token parity across routes, drafting and batch composition.
+Shared sessions enforce structured output (`response_format`, the `guided_*` fields, `tool_choice: "required"` and a
+named function; [API](../api.md#structured-output)) with `libtfgrammar` beside the binary. Before the argmax and the
+draw, `fz_mask_rows` sets each constrained row's disallowed logits to -inf (`families/flashnext/grammar.zig`); the
+first token is drawn on the host from the prompt's last row among the grammar's tokens, as the GPU draws it. Rounds
+with no constrained stream skip the mask. The single-reply driver (`--parallel 1`, the two-Mac path) answers a
+structured request with HTTP 400.
+
+`--context` past the trained 262,144 tokens, up to 1,048,576, rotates with static YaRN x4 (transformers'
+`_compute_yarn_parameters`: factor 4 over the trained window, beta 32 and 1, cos and sin scaled by 0.1 ln 4 + 1) in
+every rotary kernel, attention, the indexer's queries and pooled keys and the MTP head alike, for every prompt; at
+262,144 or less the plain rotary runs as before. Qwen recommends YaRN only where long prompts need it, since static
+scaling can cost short texts. Only the engine's own session holds the long window; on shared sessions the others hold
+262,144 tokens (`FLASHNEXT_SLOT_CONTEXTS` sets other sizes), and a request longer than that takes the long one. Past the trained window prompt chunks take fewer rows, so the prompt selector's scores keep the 2 GiB they take at
+262,144. The two-Mac path refuses such a window. On an M5 Ultra (6-bit MTP checkpoint, `--parallel 2 --vision`,
+1,048,576 and 262,144-token sessions and an 8 GiB prompt cache: 203.2 GiB wired, `FLASHNEXT_LOAD_LIMIT_GB=240`),
+needles were found at 138,830, 553,139 and 1,032,827 prompt tokens (34 s, 169 s and 417 s to the first token, 4,038 to
+2,479 tok/s), drafted replies equal `"draft": false` at 316,469 tokens, and a short request beside a 666,334-token
+prompt equals its reply alone (it waits for that prompt's pass, about 205 s). One stream decodes at 137 to 216 tok/s
+there, against 142 to 214 at 262,144 (`bench/ruler.py`, the qwen recipe's).
+
The shared host's residency set includes the engine, session caches and metadata, shared hidden rows and later
prompt snapshots. Residency uses the same validated load limit as the host, with a 2 GiB margin inside it for
growing buffers. The default stays at 70% of RAM; `FLASHNEXT_LOAD_LIMIT_GB` can raise or lower that ceiling, capped
diff --git a/docs/reference-precision.md b/docs/reference-precision.md
new file mode 100644
index 0000000000..936a571f8d
--- /dev/null
+++ b/docs/reference-precision.md
@@ -0,0 +1,80 @@
+# DeepSeek reference precision
+
+The reference harness compares Metal arithmetic with two CPU paths on the same
+inputs: the FP32 source contract and an FP64 ideal. Existing tolerances remain
+part of the tests; moving their explanations here does not change them.
+
+CSA2 isolation uses synthetic tensors and emits `CSA64` receipts. Cache codes
+are generated directly so Metal and the references decode identical BF16 keys.
+Window rows use a power-of-two FP8 scale and FP8 codes per 32-column block.
+Compressed rows use an FP8 scale and FP4 codes per 16-column block. Encoding
+uses nearest-integer, ties-to-even semantics where Metal uses `rint`.
+
+mHC isolation emits `HC64` receipts and accepts retained rows and checkpoint
+scale/base tensors. Statistic, dense, split, collapse and post-expansion stages
+run on identical inputs. The rounding gate bounds Metal's distance from the
+ideal relative to the FP32 contract's own distance. The amplification path
+retains pre-rounding FP64 outputs for analog-gain measurements and probes
+one-ULP input changes. Neither isolation harness injects source coefficients to
+make an operator pass.
+
+## Attention boundaries
+
+The FP32 sparse-head reference uses a wide FP64 dot followed by one FP32
+narrowing per score, FP32 online softmax, BF16-rounded probabilities before
+the value product, and FP32 per-tile accumulator narrowing. The FP64 path
+removes those FP32 narrowings while retaining the algorithm's quantized inputs,
+specified probability rounding and final output rounding.
+
+Optional capture of `acc / denominator` before output rounding exposes
+arithmetic ULP differences that the final BF16 output can hide. Indexer probes
+report the selection boundary margin in both precisions and count changed
+top-k picks on the same inputs.
+
+## Other reference boundaries
+
+BF16 cells decode exactly at either reference precision. RMS uses the source
+contract's FP32 mean projection for FP32, and a wide mean, division and
+reciprocal square root for FP64. Direct FP64-to-BF16 conversion rounds once,
+nearest-even, without an intermediate FP32 rounding. Explicit decoded BF16 or
+native FP32 weight cells let operator tests run without a checkpoint tensor.
+
+The dense projection reference keeps the quantized path's input rotation and
+output stages. It replaces tile decoding with an FP64 dense dot over the
+f16-rounded rotated input row.
+
+The margin-capture runner prefills a long prompt, then copies one diagnostic
+token's per-layer score tiles, top-k heap and positions for offline analysis.
+It does not reload the model or run a second reference chain. This capture is
+distinct from the per-window comparison of FP32 and FP64 selection margins.
+
+## Selection and rotary isolation
+
+The indexer compares the top-k boundary margin with one stored-score quantum.
+A margin near that quantum can make the selected set sensitive to arithmetic
+rounding. Both selectors resolve equal scores by lowest position. The position
+view starts after score, heap and candidate storage; reading the backing buffer
+without its offset would make the position check compare the wrong words.
+
+The rotary contract computes frequency, angle and trigonometric functions in
+FP32. The ideal retains those values and the products in FP64. Metal's factor
+table supplies its own unary results. Cases use 64 rotary columns, apply YaRN
+only to the compressed spectrum and include positions up to resident capacity.
+
+## Retained mHC checks
+
+The split-stage isolation supplies identical projected mixes and inverse to
+each path, leaving unary and reduction boundaries to compare. The test keeps
+its documented one-ULP Metal exp allowance and NRMS bound of `1e-7` for
+near-cancelled cells, where absolute ULP counts can grow.
+
+Retained cancellation fixtures require the original FP32 accumulation order
+exactly. Their FP64 results document the intended half-ULP difference. The post
+fixture combines a column `{1, 2^-24, 2^-24, 0}` with stream `{1,1,1,1}`,
+branch `{1}` and post `{-1,0,0,0}`. The two half-ULP terms follow `1.0`.
+
+The retained-row checks expand token 671 into four streams and use checkpoint
+FFN scale/base tensors. A separate bitwise comparison keeps the verbatim
+pre-parameterization FP32 algorithm as a control. The amplification check
+batches the first teacher row with 16 stream-cell perturbations through Metal
+and compares their response with the FP64 analog gain.
diff --git a/docs/request-seeds.md b/docs/request-seeds.md
new file mode 100644
index 0000000000..e886969d69
--- /dev/null
+++ b/docs/request-seeds.md
@@ -0,0 +1,22 @@
+# Native request seeds
+
+A sampled HTTP request uses its explicit `seed`, then a configured default seed, then a fresh 63-bit OS seed.
+Default seeds cover injected `Server.Config.default_sampling`; no stock serve flag or generation config sets one.
+An omitted or null request seed uses this fallback. Sampled defaults without a seed still draw fresh entropy.
+An explicit seed keeps its existing bit conversion and sampling behavior. Greedy requests do not draw a seed.
+If the secure random source fails, the request fails rather than falling back to prompt keying.
+
+Every sampled done line appends `seed=` after `ttft`, `decode`, `elapsed`, `queued` and `tok/s`.
+Copy that number into the next request's `seed` field to reproduce the draw at identical sampling settings.
+A fresh seed permits a different draw; it does not guarantee a different token or reply.
+
+Native `serve --prompt-seed` restores historical prompt keying when neither request nor defaults supplies a seed:
+the key comes from prompt IDs and `TENSORFOLD_SEED_SALT`. Request seeds still override configured defaults.
+The standalone CLI and speech retain their prompt keying. This flag concerns the native server.
+
+Exactness checks must use identical explicit seeds or start the native server with `--prompt-seed`.
+The release-cell harness pins missing keys to 1234 and preserves every supplied key. The shared-prefix load tool
+uses 1234 plus item ID by default. `bench_openai --seed-from-prompt` requires native `serve --prompt-seed`.
+No output comparison or acceptance threshold is weakened by fresh default seeds.
+
+Requested by @tpischke in #1026.
diff --git a/docs/vision.md b/docs/vision.md
index 00255b6c19..673e78db01 100644
--- a/docs/vision.md
+++ b/docs/vision.md
@@ -1,8 +1,8 @@
# Image input
This page describes image input in the Python engine, frozen at 0.6.6, until [The native server](#the-native-server),
-which covers the native binary: it reads images for GLM-5.3-Flash and Qwen3.8-27B on a Mac and refuses `--vision`
-for other models. Install the Python engine from the
+which covers the native binary: it reads images for GLM-5.3-Flash, Qwen3.8-27B and Qwen3.8 Flash Next on a Mac and
+refuses `--vision` for other models. Install the Python engine from the
[`v0.6.6` tag](https://github.com/ashhart/TensorFold/tree/v0.6.6) for the rest.
Parts of it came from pull requests: GLM-5.3-Flash images by @mgoldwasser
([#101](https://github.com/ashhart/TensorFold/pull/101)), Flash Next images on CUDA by @shantanugoel
@@ -146,7 +146,7 @@ For Qwen, each image request carries its own multimodal rotary positions and con
## The native server
`tensorfold-native serve` takes `--vision`, `--vision-max-images` (default 4) and `--vision-image-tokens` (default
-4,096; 13,312 for Nemotron 3.5 Super VL) for engines that read images: GLM-5.3-Flash, Qwen3.8-27B, Gemma 4
+4,096; 13,312 for Nemotron 3.5 Super VL) for engines that read images: GLM-5.3-Flash, Qwen3.8-27B, Qwen3.8 Flash Next, Gemma 4
[31B](recipes/gemma-4-native.md#images) and [12B](recipes/gemma-4-12b-native.md#images) and Step-5-Preview on one
Mac, and Nemotron 3.5 Super VL on CUDA. Other native engines refuse the flag at startup.
@@ -206,10 +206,38 @@ tower there. With an image, drafted replies equal `"draft": false`, four request
turn resumes past its image and equals a fresh server's reply, and DFlash2 keeps 2.6 to 3.0 tokens a round where the
same asks without the image get 2.8 to 3.4.
+### Qwen3.8 Flash Next
+
+With `--vision`, Flash Next loads its checkpoint's image tower beside the language model, one stream or several
+(`--parallel`). It is the 27B's tower (the same 333 bf16 `vision_tower.*` tensors and processor) with 2,560-wide output,
+run by the same code (`zig/src/core/qwen_vl/`); images are prepared as above. Its rows replace the `<|image_pad|>`
+embeddings in all four of the model's hyper-connection streams, and the prompt pass turns them by interleaved M-RoPE
+(sections 11, 11 and 10 over the first 64 dimensions). The positions are transformers' `get_rope_index` shifted so that
+the text after the last image keeps its own slot: decoding, drafts and later tokens run as for text. Text-only requests
+get the tokens they got without the flag.
+
+- **Prompt cache:** image and video conversations resume past their images. The shift moves every position before the
+ request's last image, so a kept state is named by the shift as well as the tokens and images: a later turn with the
+ same images resumes, and a turn that adds an image reads its prompt again. The tower skips the images a resumed state
+ already holds. Replies equal the cache off's in multi-turn image and video chats, on one stream and shared sessions.
+- **Memory:** the tower and its scratch take 1.73 GiB at the default 4,096 image tokens, counted against the load limit;
+ a request's rows take 20 KiB a visual token while its prompt pass runs.
+- **Checks on an M5 Ultra:** five test pictures got the grids and patches of transformers' Pillow processor bit for bit;
+ the tower's rows came closer to transformers' float32 `Qwen4ExpVisionModel` than its bf16 run does (mean row error 1.4
+ to 3.1% against 1.8 to 5.7%). Six fixed image prompts gave drafted replies equal to `"draft": false`, the same tokens
+ on one stream and on shared sessions, and the expected answers. A 441-token picture takes 53 ms in the tower.
+- **Video:** `video_url` parts as described below, in the Qwen3-VL layout: each pair of frames is one image, and its
+ block is `<|vision_start|>` and its `<|video_pad|>` rows then `<|vision_end|>`, inside the template's own
+ `<|vision_start|>…<|vision_end|>`, as transformers 5.17's `Qwen3VLProcessor` writes it. Frames are sampled at 2 a
+ second (at least 4; at most 256, where the processor allows 768), the clip's pixels kept between 65,536 and 16,777,216
+ and inside the request's `--vision-video-tokens` share, and resized with torch's antialiased uint8 bicubic. On three
+ test clips the served prompt was as long as the processor's (239, 387 and 8,215 tokens); a 30-second 720p clip takes
+ 3.2 s to its first token.
+
### Video on the native server
-With `--vision`, GLM-5.3-Flash also reads `video_url` parts in user messages, as transformers 5.19's
-`Glm5NextProcessor` prepares them:
+With `--vision`, GLM-5.3-Flash also reads `video_url` parts in user messages (Flash Next too, in its own layout above),
+as transformers 5.19's `Glm5NextProcessor` prepares them:
- **Sampling:** 2 frames a second, deduplicated and padded to an even count, at most 256 frames.
- **Frame pairs:** each pair of frames is one image to the tower.
- **Prompt:** the template's `<|begin_of_video|><|video|><|end_of_video|>` holds one
diff --git a/packaging/RUNTIME.md b/packaging/RUNTIME.md
index 6ef0895ca2..b48dad737d 100644
--- a/packaging/RUNTIME.md
+++ b/packaging/RUNTIME.md
@@ -26,3 +26,36 @@ Flash Next speed-up settings also name the separately built MCDMA dynamic librar
That library is not part of these archives. The server can read an API key file when requested.
None of those paths requires an installed Python package, though capture generation currently uses Python tools.
Archives marked `host-only` are CPU verification artifacts and cannot serve CUDA inference.
+
+
+## Optional shared libraries
+
+The release includes the native executable, built-in CUDA kernels and `libtfgrammar`. A driver-only NVIDIA host does not
+supply cuBLASLt or NCCL. A GLM CUDA pair needs NCCL on both ranks; adding `--vision` also needs cuBLASLt.
+
+| Feature | Library | Library filename (Linux; macOS where applicable) | Where it comes from |
+| --- | --- | --- | --- |
+| CUDA vision towers (`--vision`), cuBLASLt projections | cuBLASLt | `libcublasLt.so.13` (`cublasLt64_13.dll` on Windows) | NVIDIA CUDA 13 cuBLAS runtime; not the display driver |
+| Drama3 CUDA prompt and codec matrix operations | cuBLAS | `libcublas.so.13` | NVIDIA CUDA 13 cuBLAS runtime |
+| CUDA tensor-parallel ranks, including the GLM pair | NCCL | `libnccl.so.2` | NVIDIA NCCL 2 runtime compatible with CUDA 13 |
+| RoCE/RDMA gathers | verbs | `libibverbs.so.1`; `/usr/lib/librdma.dylib` | rdma-core and adapter provider on Linux; the macOS RDMA runtime |
+| Video inputs | TensorFold video | `libtfvideo.so`; `libtfvideo.dylib` | Optional `zig build video -Dffmpeg=PREFIX` runtime built against FFmpeg 9.0.2, installed in the server's `lib` directory |
+| JPEG inputs | libjpeg-turbo 3.1.4.1 | `libturbojpeg.so.0`; `libturbojpeg.0.dylib` | Separately installed pinned decoder; `TENSORFOLD_LIBTURBOJPEG` can name its file |
+| WebP inputs | libwebp 1.6.0 | `libwebpdecoder.so.3`; `libwebpdecoder.3.dylib` | Separately installed pinned decoder; `TENSORFOLD_LIBWEBP` can name its file |
+| Structured output and required tool calls | TensorFold grammar | `libtfgrammar.so`; `libtfgrammar.dylib` | The matching release archive's `lib` directory, or `zig build grammar` |
+| HIP tensor-parallel ranks | RCCL | `librccl.so.1` | ROCm's matching RCCL runtime |
+| MCDMA mailbox/Metal speed-up communication | MCDMA RPC | `libmcdma-rpc.so`; `libmcdma-rpc.dylib` | Separately built MCDMA RPC helper; `MCDMA_RPC_LIBRARY` can name its file |
+| MCDMA peer fabric | MCDMA fabric | `libmcdma-fabric.so`; `libmcdma-fabric.dylib` | Separately built MCDMA fabric helper; name the library's full path in the peer configuration file |
+
+`capabilities --json` includes `optional_libraries`, with each dependency's feature, soname, `found` flag and installation
+source. Discovery keeps every found library mapped until process exit, without calling its APIs or creating a CUDA context itself.
+A library can register process or thread exit handlers during loading; those handlers must retain their code.
+`found` means the loader found a file. It does not establish whether available libraries
+meet the feature's required functions, decoder version and hardware checks.
+Codec environment overrides take precedence, including a missing override. Capabilities does not read your peer
+configuration, so a library configured only there may be reported missing.
+No private candidate paths are emitted in the JSON. An unneeded missing optional dependency does not stop capabilities
+or plain text serving; a requested feature refuses with the needed library and installation instructions.
+
+On Linux, add the containing directory to `LD_LIBRARY_PATH` when an installed runtime is outside the loader's normal
+search path. Use the library's full path in the peer configuration file when its option asks for one.
diff --git a/tools/bench_openai.py b/tools/bench_openai.py
index f29a5a323c..d9ad6c3bf2 100644
--- a/tools/bench_openai.py
+++ b/tools/bench_openai.py
@@ -96,7 +96,7 @@ def main() -> None:
p.add_argument("--output")
p.add_argument("--api-key-env", metavar="NAME", help="read the bearer API key from this environment variable")
p.add_argument("--seed-from-prompt", action="store_true",
- help="send no seed: cuda_server then seeds from the prompt, as the engine benches do, "
+ help="send no seed; requires native serve --prompt-seed to match prompt-keyed engine benches, "
"so the reply equals the bench's and the timings compare directly")
args = p.parse_args()
api_key = read_api_key(p, args.api_key_env)
diff --git a/tools/release/check_archive.py b/tools/release/check_archive.py
new file mode 100644
index 0000000000..0b4d7f575a
--- /dev/null
+++ b/tools/release/check_archive.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+"""Inspect the finished archive's metadata and decoded file contents before publication."""
+from pathlib import Path
+import subprocess
+import sys
+import tarfile
+import tempfile
+
+
+def scan(checker, build_root, file):
+ subprocess.run([checker, build_root, str(file)], check=True)
+
+
+def check(checker, build_root, archive):
+ with tempfile.TemporaryDirectory(prefix=".archive-privacy-", dir=archive.parent) as temporary:
+ directory = Path(temporary)
+ with tarfile.open(archive, "r:gz") as tar:
+ members = tar.getmembers()
+ listing = directory / "listing"
+ with listing.open("wb") as out:
+ for key, value in tar.pax_headers.items():
+ out.write(f"{key}={value}\0".encode("utf-8", "surrogateescape"))
+ for member in members:
+ for value in [member.name, member.linkname, member.uname, member.gname]:
+ out.write(value.encode("utf-8", "surrogateescape") + b"\0")
+ for key, value in member.pax_headers.items():
+ out.write(f"{key}={value}\0".encode("utf-8", "surrogateescape"))
+ scan(checker, build_root, listing)
+ for member in members:
+ name = member.name.replace("\\", "/")
+ if name.startswith("/") or ".." in name.split("/") or (len(name) > 1 and name[1] == ":"):
+ raise ValueError("nonrelative member name")
+ if not (member.isfile() or member.isdir()):
+ raise ValueError("nonregular archive member")
+ if member.isdir():
+ continue
+ if member.size > 1 << 30:
+ raise ValueError("oversized archive member")
+ payload = directory / "payload"
+ with tar.extractfile(member) as source, payload.open("wb") as out:
+ while block := source.read(1 << 20):
+ out.write(block)
+ scan(checker, build_root, payload)
+
+
+def main():
+ if len(sys.argv) != 4:
+ print("usage: check_archive.py CHECKER BUILD_ROOT ARCHIVE", file=sys.stderr)
+ return 2
+ try:
+ check(sys.argv[1], sys.argv[2], Path(sys.argv[3]))
+ except (OSError, ValueError, tarfile.TarError, subprocess.CalledProcessError) as error:
+ print(f"release privacy: archive refused ({type(error).__name__})", file=sys.stderr)
+ return 1
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/tools/release/check_paths.sh b/tools/release/check_paths.sh
new file mode 100644
index 0000000000..e794a4ac46
--- /dev/null
+++ b/tools/release/check_paths.sh
@@ -0,0 +1,7 @@
+#!/bin/sh
+# Check materialized release inputs or an archive stage, including every overlay and captured image.
+set -eu
+[ "$#" -eq 3 ] || { echo 'usage: check_paths.sh CHECKER BUILD_ROOT DIRECTORY' >&2; exit 2; }
+[ -d "$3" ] || { echo 'release privacy input directory is missing' >&2; exit 1; }
+[ ! -L "$3" ] && [ -z "$(find "$3" -type l -print -quit)" ] || { echo 'release privacy: symlink refused' >&2; exit 1; }
+find "$3" -type f -exec "$1" "$2" {} +
diff --git a/tools/release/package.sh b/tools/release/package.sh
index 4291cc19c4..5fbe825be0 100644
--- a/tools/release/package.sh
+++ b/tools/release/package.sh
@@ -14,12 +14,17 @@ shift 9
# then optionally a CUDA capture directory, and --overlay=DIR: files copied into the archive's tree (libtfgrammar)
capture=
overlay=
+path_checker=
+build_root=
for extra in "$@"; do
case "$extra" in
--overlay=*) overlay=${extra#--overlay=} ;;
+ --path-checker=*) path_checker=${extra#--path-checker=} ;;
+ --build-root=*) build_root=${extra#--build-root=} ;;
*) capture=$extra ;;
esac
done
+[ -n "$path_checker" ] && [ -n "$build_root" ] || { echo 'release packaging requires its privacy checker and build root' >&2; exit 2; }
name="tensorfold-$version-$platform"
mkdir -p "$output"
# Each invocation owns a fresh stage, including reruns of the same version/platform.
@@ -62,6 +67,7 @@ fi
case "$platform" in
*-host-only) printf '%s\n' 'CPU verification only. This binary has no embedded CUDA kernels and cannot run inference.' > "$stage/HOST-ONLY" ;;
esac
+sh "$(dirname "$0")/check_paths.sh" "$path_checker" "$build_root" "$stage"
sha() {
if command -v sha256sum >/dev/null 2>&1; then sha256sum "$@"; else shasum -a 256 "$@"; fi
}
@@ -70,7 +76,9 @@ sha() {
# no macOS metadata or builder identity in the entries (Linux tar warned once per entry)
if tar --version 2>/dev/null | grep -q bsdtar; then owner="--no-xattrs --no-mac-metadata --uid 0 --gid 0 --uname root --gname root"; else owner="--owner=0 --group=0 --numeric-owner"; fi
# shellcheck disable=SC2086
-COPYFILE_DISABLE=1 tar $owner -czf "$output/$name.tar.gz" -C "$staging" "$name"
+COPYFILE_DISABLE=1 tar $owner -czf "$staging/$name.tar.gz" -C "$staging" "$name"
+python3 "$(dirname "$0")/check_archive.py" "$path_checker" "$build_root" "$staging/$name.tar.gz"
+mv "$staging/$name.tar.gz" "$output/$name.tar.gz"
(cd "$output"; sha "$name.tar.gz") > "$output/$name.tar.gz.sha256"
# Install only the final archive and checksum, never the staging tree.
# Fresh stages stay in the build cache for inspection.
diff --git a/tools/release/test_package.py b/tools/release/test_package.py
index b2e2ec6aa3..1f189962df 100644
--- a/tools/release/test_package.py
+++ b/tools/release/test_package.py
@@ -1,15 +1,22 @@
#!/usr/bin/env python3
"""CPU regression for repeated packaging; keeps its receipt directory for inspection."""
+import argparse
import hashlib
+import io
import os
from pathlib import Path
+import shutil
import subprocess
import sys
import tarfile
import tempfile
root = Path(__file__).resolve().parents[2]
-script = Path(sys.argv[1]).resolve() if len(sys.argv) == 2 else root / "tools/release/package.sh"
+parser = argparse.ArgumentParser(description=__doc__)
+parser.add_argument("--path-checker", type=Path, required=True)
+parser.add_argument("script", nargs="?", type=Path, default=root / "tools/release/package.sh")
+options = parser.parse_args()
+script = options.script.resolve()
(root / ".zig-cache").mkdir(exist_ok=True)
receipt = Path(tempfile.mkdtemp(prefix="package-repeat-", dir=root / ".zig-cache"))
output = receipt / "archives"
@@ -38,15 +45,19 @@ def capture(dirname, sets):
(overlay / "lib/libtfgrammar.so").write_bytes(b"synthetic grammar library fixture\n")
(overlay / "LICENSES/xgrammar-Apache-2.0.txt").write_text("synthetic license fixture\n")
-def pack(tree=None, extra=None):
+def command(tree=None, extra=None, license_tree=None):
args = ["sh", str(script), version, "linux-x86_64", str(binary), str(root / "LICENSE"),
- str(root / "NOTICE"), str(root / "packaging/RUNTIME.md"), str(root / "LICENSES"),
- str(root / "THIRD_PARTY_NOTICES.md"), str(output)]
+ str(root / "NOTICE"), str(root / "packaging/RUNTIME.md"), str(license_tree or root / "LICENSES"),
+ str(root / "THIRD_PARTY_NOTICES.md"), str(output),
+ f"--path-checker={options.path_checker.resolve()}", f"--build-root={root}"]
if extra is not None:
args.append(f"--overlay={extra}")
if tree is not None:
args.append(str(tree))
- subprocess.run(args, check=True)
+ return args
+
+def pack(tree=None, extra=None):
+ subprocess.run(command(tree, extra), check=True)
archive = output / (name + ".tar.gz")
companion = archive.with_name(archive.name + ".sha256")
assert hashlib.sha256(archive.read_bytes()).hexdigest() == companion.read_text().split()[0]
@@ -70,4 +81,90 @@ def pack(tree=None, extra=None):
assert "lib/libtfgrammar.so" not in third, "overlay files survived removal of the overlay input"
for file in ["LICENSES/MIT.txt", "LICENSES/Apache-2.0.txt", "LICENSES/MiaAI-Lab-MIT.txt", "THIRD_PARTY_NOTICES.md"]:
assert third[file] == (root / file).read_bytes(), file
-print(f"PASS same-version full -> smaller -> no capture, all hashes and notice files; receipt {receipt}")
+
+archive = output / (name + ".tar.gz")
+companion = archive.with_name(archive.name + ".sha256")
+clean_archive = archive.read_bytes()
+clean_hash = companion.read_bytes()
+for file, tree, extra in [(binary, None, None), (overlay / "lib/libtfgrammar.so", None, overlay),
+ (smaller / "sm121/cubins/keep.cubin", smaller, None)]:
+ original = file.read_bytes()
+ try:
+ file.write_bytes(original + b"\0" + str(root).encode() + b"/zig/kernels/cuda/exl3_block\0")
+ run = subprocess.run(command(tree, extra), capture_output=True, text=True)
+ assert run.returncode != 0 and "PrivateBuildPath" in run.stderr, run.stderr
+ assert str(root) not in run.stderr, "path check leaked the private build root in its diagnostic"
+ assert archive.read_bytes() == clean_archive, "a rejected package replaced the clean archive"
+ assert companion.read_bytes() == clean_hash, "a rejected package replaced the clean checksum"
+ finally:
+ file.write_bytes(original)
+licenses = receipt / "license-links"
+shutil.copytree(root / "LICENSES", licenses)
+safe_license = receipt / "private-build-root/clean-license.txt"
+safe_license.parent.mkdir()
+safe_license.write_text("clean license bytes, no path\n")
+for target in [safe_license, safe_license.parent, safe_license.parent / "absent"]:
+ link = licenses / "example.txt"
+ link.symlink_to(target)
+ try:
+ run = subprocess.run(command(license_tree=licenses), capture_output=True, text=True)
+ assert run.returncode != 0 and "symlink refused" in run.stderr, run.stderr
+ assert str(target) not in run.stderr and str(root) not in run.stderr
+ assert archive.read_bytes() == clean_archive and companion.read_bytes() == clean_hash
+ finally:
+ link.unlink()
+
+def archive_case(label, member_name, data=b"clean payload\n", target="", kind=tarfile.REGTYPE, pax=None):
+ fixture = receipt / (label + ".tar.gz")
+ with tarfile.open(fixture, "w:gz") as tar:
+ member = tarfile.TarInfo(member_name)
+ member.type = kind
+ member.linkname = target
+ member.pax_headers = pax or {}
+ member.size = len(data) if kind == tarfile.REGTYPE else 0
+ tar.addfile(member, io.BytesIO(data) if kind == tarfile.REGTYPE else None)
+ return fixture
+
+archive_checker = root / "tools/release/check_archive.py"
+cases = [
+ (archive_case("clean", "package/LICENSES/example.txt"), 0),
+ (archive_case("private-name", str(root) + "/zig/kernels/cuda/source.cu"), 1),
+ (archive_case("private-link", "package/example.txt", target=str(safe_license), kind=tarfile.SYMTYPE), 1),
+ (archive_case("private-hardlink", "package/example.txt", target=str(safe_license), kind=tarfile.LNKTYPE), 1),
+ (archive_case("relative-link", "package/example.txt", target="clean.txt", kind=tarfile.SYMTYPE), 1),
+ (archive_case("private-pax", "package/example.txt", pax={"comment": str(root)}), 1),
+ (archive_case("private-contents", "package/bin/native", data=str(root).encode()), 1),
+]
+for fixture, expected in cases:
+ run = subprocess.run([sys.executable, str(archive_checker), str(options.path_checker.resolve()), str(root), str(fixture)],
+ capture_output=True, text=True)
+ assert run.returncode == expected, run.stderr
+ assert str(root) not in run.stderr, "archive check leaked the private root in its diagnostic"
+
+fake_bin = receipt / "late-link-tar"
+fake_bin.mkdir()
+fake_tar = fake_bin / "tar"
+fake_tar.write_text(f'''#!/usr/bin/env python3
+import io, os, subprocess, sys, tarfile
+real_tar = {shutil.which("tar")!r}
+if sys.argv[1:] == ["--version"]:
+ os.execv(real_tar, [real_tar, "--version"])
+subprocess.run([real_tar, *sys.argv[1:]], check=True)
+archive = sys.argv[sys.argv.index("-czf") + 1]
+with tarfile.open(archive) as source:
+ entries = [(m, source.extractfile(m).read() if m.isfile() else None) for m in source.getmembers()]
+with tarfile.open(archive, "w:gz") as target:
+ for member, data in entries:
+ target.addfile(member, io.BytesIO(data) if data is not None else None)
+ link = tarfile.TarInfo("package/late-link")
+ link.type = tarfile.SYMTYPE
+ link.linkname = {str(safe_license)!r}
+ target.addfile(link)
+''')
+fake_tar.chmod(0o755)
+run = subprocess.run(command(), env=dict(os.environ, PATH=str(fake_bin) + os.pathsep + os.environ["PATH"]),
+ capture_output=True, text=True)
+assert run.returncode != 0 and "archive refused" in run.stderr, run.stderr
+assert str(root) not in run.stderr
+assert archive.read_bytes() == clean_archive and companion.read_bytes() == clean_hash
+print(f"PASS repeated packaging, hashes, notices, contents and LICENSES symlink refusal; receipt {receipt}")
diff --git a/tools/shared_prefix_load.py b/tools/shared_prefix_load.py
index 90341d9179..3df2c98c40 100644
--- a/tools/shared_prefix_load.py
+++ b/tools/shared_prefix_load.py
@@ -116,7 +116,7 @@ def main() -> None:
p.add_argument("--first", type=int, default=0, help="only the first N items")
p.add_argument("--max-tokens", type=int, default=512)
p.add_argument("--temperature", type=float, default=0.0)
- p.add_argument("--seed", type=int, default=None, help="sampled: item i gets seed + i")
+ p.add_argument("--seed", type=int, default=1234, help="sampled: item i gets seed + i (default: 1234)")
p.add_argument("--no-draft", action="store_true", help='send "draft": false (the serial reference)')
p.add_argument("--mem", action="store_true")
p.add_argument("--ignore", default="", help="comma list of process names nvidia-smi should not count")
diff --git a/tools/zig/check_cuda_sources.py b/tools/zig/check_cuda_sources.py
new file mode 100644
index 0000000000..b04549d0cf
--- /dev/null
+++ b/tools/zig/check_cuda_sources.py
@@ -0,0 +1,116 @@
+"""Resolve CUDA source names through their real includes without generating PTX or invoking nvcc."""
+import argparse
+import ast
+import json
+import os
+from pathlib import Path
+import re
+import shutil
+import subprocess
+
+ROOT = Path(__file__).resolve().parents[2]
+HEADERS = Path(__file__).with_name("cuda_host")
+ARCHES = (800, 860, 890, 900, 1200, 1210)
+
+
+def unit_floors():
+ """Read literal per-unit floors from the build catalogs; unknown sources remain checked at every SM."""
+ floors = {}
+ for name in ("cuda_kernels.zig", "dsv41_units.zig"):
+ text = (ROOT / "zig/build" / name).read_text()
+ for entry in re.finditer(r'\.\{\s*\.name\s*=\s*"([^"]+)"(.*?)(?=\n\s*\.\{|^\};)', text, re.S | re.M):
+ body = entry[2]
+ src = re.search(r'\.src\s*=\s*"([^"]+)"', body)
+ floor = re.search(r'\.min_sm\s*=\s*(\d+)', body)
+ if ".min_sm" in body and floor is None:
+ raise ValueError("nonliteral SM floor: " + entry[1])
+ key = "zig/kernels/cuda/" + (src[1] if src else entry[1]) + ".cu"
+ value = int(floor[1]) * 10 if floor else 0
+ if key in floors and floors[key] != value:
+ raise ValueError("inconsistent SM floor: " + key)
+ floors[key] = value
+ return floors
+
+
+def command(compiler, source, arch):
+ """Use Clang's semantic parser only, with a fixed SDK declaration surface."""
+ flags = [compiler, "-std=c++20", "-x", "cuda", "--cuda-device-only", "-nocudainc", "-nocudalib",
+ "-fsyntax-only", "-ferror-limit=0", "-Wno-unknown-pragmas", "-I", str(HEADERS),
+ "-include", str(HEADERS / "cuda_runtime.h"), "-U__CUDA_ARCH__", f"-D__CUDA_ARCH__={arch}"]
+ if arch >= 1200:
+ flags += [f"-D__CUDA_ARCH_FEAT_SM{arch // 10}_ALL"]
+ return flags + [str(source)]
+
+
+def check(compiler, source, arch):
+ """Return every diagnostic, without ignoring unresolved project identifiers."""
+ run = subprocess.run(command(compiler, source, arch), capture_output=True, text=True)
+ diagnostics = run.stdout + run.stderr
+ code = run.returncode
+ if arch < 900:
+ args = command(compiler, source, arch)
+ args[args.index("-fsyntax-only")] = "-E"
+ prep = subprocess.run(args, capture_output=True, text=True)
+ if prep.returncode:
+ code = code or prep.returncode
+ diagnostics += prep.stderr
+ else:
+ assemblies = re.finditer(r'\b(?:asm|__asm__)\s*(?:volatile|__volatile__)?\s*\(\s*((?:"(?:\\.|[^"\\])*"\s*)+)', prep.stdout)
+ for assembly in assemblies:
+ text = "".join(ast.literal_eval(s) for s in re.findall(r'"(?:\\.|[^"\\])*"', assembly[1]))
+ text = re.sub(r'/\*.*?\*/|//[^\n]*', '', text, flags=re.S)
+ for op in re.findall(r'\b[a-zA-Z_][a-zA-Z0-9_]*(?:[.:][a-zA-Z0-9_:]+)*', text):
+ floor = 0
+ if op.startswith("cvt.") and {"e4m3x2", "e5m2x2"} & set(op.split(".")):
+ floor = 890
+ if op.startswith("barrier.cluster.") or op == "mapa" or op.startswith("mapa.") or "::cluster" in op:
+ floor = 900
+ if arch < floor:
+ diagnostics += f"SM-FLOOR: {op} requires sm_{floor // 10}+, requested sm_{arch // 10}\n"
+ code = 1
+ return dict(source=str(source.relative_to(ROOT)), arch=arch, returncode=code, diagnostics=diagnostics)
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--compiler", default=os.environ.get("CLANGXX", "clang++"))
+ parser.add_argument("--json", type=Path)
+ parser.add_argument("--arch", type=int, choices=ARCHES, action="append")
+ parser.add_argument("sources", nargs="*", help="Explicit paths force every requested SM; default: all .cu at build-eligible SMs")
+ args = parser.parse_args()
+ compiler = shutil.which(args.compiler)
+ if compiler is None:
+ parser.error("clang++ is required; this check never falls back to nvcc")
+ version = subprocess.check_output([compiler, "--version"], text=True)
+ if "clang" not in version.lower():
+ parser.error("the semantic check requires clang++")
+ sources = [ROOT / p for p in args.sources] if args.sources else sorted((ROOT / "zig/kernels/cuda").rglob("*.cu"))
+ if not sources:
+ parser.error("no CUDA sources found")
+ results = []
+ excluded = []
+ floors = unit_floors()
+ for arch in args.arch or ARCHES:
+ for source in sources:
+ floor = floors.get(str(source.relative_to(ROOT)), 0)
+ if not args.sources and arch < floor:
+ excluded.append(dict(source=str(source.relative_to(ROOT)), arch=arch, min_sm=floor // 10))
+ continue
+ result = check(compiler, source, arch)
+ results.append(result)
+ if result["returncode"]:
+ print(f"FAIL {result['source']} arch={arch}\n{result['diagnostics']}", flush=True)
+ print(f"Checked {len(sources)} translation units at architecture branch {arch}", flush=True)
+ failures = sum(r["returncode"] != 0 for r in results)
+ receipt = dict(compiler=version.splitlines()[0], source_count=len(sources), checks=len(results),
+ failures=failures, mode="semantic-only; no PTX, object, assembly, linker or device execution",
+ sdk="fixed declarations in tools/zig/cuda_host; no generated project declarations",
+ excluded_by_build_floor=excluded, results=results)
+ if args.json:
+ args.json.write_text(json.dumps(receipt, indent=2) + "\n")
+ print(f"{len(results) - failures}/{len(results)} CUDA source/include checks passed")
+ return bool(failures)
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/tools/zig/check_cuda_symbols.py b/tools/zig/check_cuda_symbols.py
new file mode 100644
index 0000000000..1f4cf8b521
--- /dev/null
+++ b/tools/zig/check_cuda_symbols.py
@@ -0,0 +1,74 @@
+"""Require each registered x3ld function in each SM's cuobjdump --dump-sass output."""
+import argparse
+from pathlib import Path
+import re
+import sys
+import unittest
+
+
+def expected(registry):
+ symbols = set(re.findall(r'\.image\s*=\s*"dsv41_x3ld"\s*,\s*\.symbol\s*=\s*"([^"]+)"', registry))
+ if not symbols:
+ raise ValueError("registry has no x3ld symbols")
+ return symbols
+
+
+def functions(text):
+ result, sm = {}, None
+ for line in text.splitlines():
+ arch = re.search(r'(?:code for |arch\s*=\s*)sm_(\d+)[a-z]?\b', line)
+ if arch:
+ sm = int(arch[1])
+ result.setdefault(sm, set())
+ function = re.fullmatch(r'\s*Function\s*:\s*(\S+)\s*', line)
+ if function and sm is not None:
+ result[sm].add(function[1])
+ return result
+
+
+def missing(text, symbols, sms):
+ have = functions(text)
+ return [(sm, symbol) for sm in sms for symbol in sorted(symbols - have.get(sm, set()))]
+
+
+class Controls(unittest.TestCase):
+ def test_missing_entry_and_arch_are_refused(self):
+ self.assertEqual([(121, "required")], missing("code for sm_121\n Function : other\n", {"required"}, [121]))
+ self.assertEqual([(80, "required")], missing("code for sm_121\n Function : required\n", {"required"}, [80]))
+
+ def test_declaration_or_substring_is_not_a_function(self):
+ self.assertTrue(missing("code for sm_121\n .entry required\n Function : required_extra\n", {"required"}, [121]))
+
+ def test_each_sm_must_hold_each_function(self):
+ text = "code for sm_80\n Function : a\n Function : b\narch = sm_121a\n Function : a\n Function : b\n"
+ self.assertFalse(missing(text, {"a", "b"}, [80, 121]))
+ self.assertEqual({"a"}, expected('.image = "dsv41_x3ld", .symbol = "a"'))
+ with self.assertRaises(ValueError):
+ expected("no entries")
+
+
+def main():
+ if sys.argv[1:] == ["--self-test"]:
+ unittest.main(argv=[sys.argv[0]])
+ return
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--sms", required=True)
+ parser.add_argument("--registry", type=Path, required=True)
+ parser.add_argument("dump", type=Path)
+ args = parser.parse_args()
+ try:
+ sms = [int(sm) for sm in args.sms.split(",")]
+ if not sms or any(sm <= 0 for sm in sms):
+ raise ValueError("SM targets must be positive integers")
+ absent = missing(args.dump.read_text(), expected(args.registry.read_text()), sms)
+ except ValueError as err:
+ parser.error(str(err))
+ for sm, symbol in absent:
+ print(f"sm_{sm} missing SASS function: {symbol}", file=sys.stderr)
+ if absent:
+ sys.exit(1)
+ print(f"x3ld symbols present on {len(set(sms))} SM targets")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/tools/zig/cuda_host/README.md b/tools/zig/cuda_host/README.md
new file mode 100644
index 0000000000..53748f2ca1
--- /dev/null
+++ b/tools/zig/cuda_host/README.md
@@ -0,0 +1,30 @@
+# CUDA source/include check
+
+Run `python3 tools/zig/check_cuda_sources.py` with Clang available. This is a semantic name and type check of every
+`zig/kernels/cuda/**/*.cu` and its real transitive includes, including standalone sources normally included by another
+translation unit. New sources are discovered automatically. Six macro configurations cover sm_80, sm_86, sm_89,
+sm_90, sm_120 and sm_121. The default scan reads each unit's literal min_sm from the build catalogs and records
+ineligible pairs separately. Explicit source arguments force checks even below their declared floor.
+
+Clang runs with `-fsyntax-only -nocudainc -nocudalib`. Its device semantic pass checks function bodies, explicit template
+instantiations, CUDA launches, launch bounds and inline-assembly operands. It emits no PTX, object, assembly or executable and never
+invokes nvcc. The macro values select source branches, not a code-generation target. Production source text is not
+rewritten. No failed eligible translation unit or unresolved project identifier is excluded. The cluster declarations
+exist only from sm_90. Below sm_89, preprocessed inline PTX cvt instructions using e4m3x2 or e5m2x2 are rejected.
+Below sm_90, inline PTX barrier.cluster instructions, mapa and ::cluster modifiers are also rejected.
+Preprocessing respects include chains and architecture guards; comments and disabled branches cannot trigger that rule.
+
+The headers in this directory supply a fixed, manually reviewed subset of CUDA SDK and standard math declarations.
+They contain no project helpers and must never be populated from unknown identifiers in compiler diagnostics. CUDA
+runtime builtins are implicitly visible, as in nvcc; other headers are reached through each source's own includes.
+`algorithm` supplies only min/max, and `vector` is empty because production defines exclude the Torch-only wrappers.
+The remaining standard integer, size and type-trait headers come from the host compiler.
+
+This check catches missing declarations such as EXL3's unpack helpers and these known architecture floors. It is not
+an SDK ABI, link, general PTX instruction, register, resource, architecture-support or numerical qualification.
+Templates not instantiated by a source and
+preprocessor branches outside the production no-Torch configurations remain unchecked. Clang warnings are preserved
+in the JSON receipt. A real CUDA image build, the SASS symbol gate and device exactness tests remain required.
+
+Run `python3 tools/zig/test_cuda_sources.py` for include, launch-bound, layout, cluster API, inline PTX and FP8-floor controls.
+The layout assertion covers every coordinate of the 16 by 16 mma B fragment using an independent inverse mapping.
diff --git a/tools/zig/cuda_host/algorithm b/tools/zig/cuda_host/algorithm
new file mode 100644
index 0000000000..93304c8956
--- /dev/null
+++ b/tools/zig/cuda_host/algorithm
@@ -0,0 +1,5 @@
+#pragma once
+namespace std {
+template __host__ __device__ constexpr T min(T a,T b) { return b __host__ __device__ constexpr T max(T a,T b) { return a= 900
+struct cluster_group {
+ __device__ void sync(); __device__ unsigned block_rank(); __device__ unsigned num_blocks();
+ template __device__ T* map_shared_rank(T*,unsigned);
+};
+__device__ cluster_group this_cluster();
+#endif
+}
diff --git a/tools/zig/cuda_host/cuda_bf16.h b/tools/zig/cuda_host/cuda_bf16.h
new file mode 100644
index 0000000000..9f96566052
--- /dev/null
+++ b/tools/zig/cuda_host/cuda_bf16.h
@@ -0,0 +1,15 @@
+#pragma once
+#include "cuda_fp16.h"
+struct __nv_bfloat16 { unsigned short x; };
+struct __nv_bfloat162 { __nv_bfloat16 x,y; };
+__device__ float __bfloat162float(__nv_bfloat16); __device__ __nv_bfloat16 __float2bfloat16(float);
+__device__ __nv_bfloat16 __float2bfloat16_rn(float); __device__ __nv_bfloat16 __ushort_as_bfloat16(unsigned short);
+__device__ unsigned short __bfloat16_as_ushort(__nv_bfloat16);
+__device__ float2 __bfloat1622float2(__nv_bfloat162);
+__device__ __nv_bfloat162 __floats2bfloat162_rn(float,float);
+__device__ __nv_bfloat162 __halves2bfloat162(__nv_bfloat16,__nv_bfloat16);
+__device__ __nv_bfloat162 __hadd2(__nv_bfloat162,__nv_bfloat162);
+__device__ __nv_bfloat162 __hsub2(__nv_bfloat162,__nv_bfloat162);
+__device__ __nv_bfloat162 __hmul2(__nv_bfloat162,__nv_bfloat162);
+__device__ __nv_bfloat162 __hfma2(__nv_bfloat162,__nv_bfloat162,__nv_bfloat162);
+__device__ float __low2float(__nv_bfloat162); __device__ float __high2float(__nv_bfloat162);
diff --git a/tools/zig/cuda_host/cuda_fp16.h b/tools/zig/cuda_host/cuda_fp16.h
new file mode 100644
index 0000000000..d423d31f12
--- /dev/null
+++ b/tools/zig/cuda_host/cuda_fp16.h
@@ -0,0 +1,16 @@
+#pragma once
+#include "cuda_runtime.h"
+struct __half_raw { unsigned short x; };
+struct __half { unsigned short x; __half()=default; __host__ __device__ explicit __half(float); __host__ __device__ __half(__half_raw); };
+struct __half2_raw { unsigned short x,y; };
+struct __half2 { __half x,y; __half2()=default; __host__ __device__ __half2(__half2_raw); };
+using half=__half; using half2=__half2;
+__device__ float __half2float(half); __device__ half __ushort_as_half(unsigned short);
+__device__ unsigned short __half_as_ushort(half); __device__ half __float2half(float);
+__device__ half __float2half_rn(float); __device__ half __uint2half_rn(unsigned);
+__device__ float2 __half22float2(half2); __device__ half2 __half2half2(half);
+__device__ half2 __float2half2_rn(float); __device__ half2 __floats2half2_rn(float,float);
+__device__ half2 __halves2half2(half,half); __device__ half __low2half(half2); __device__ half __high2half(half2);
+__device__ float __low2float(half2); __device__ float __high2float(half2);
+__device__ half2 __hadd2(half2,half2); __device__ half2 __hsub2(half2,half2); __device__ half2 __hmul2(half2,half2);
+__device__ half2 __hfma2(half2,half2,half2); __device__ half __hadd(half,half);
diff --git a/tools/zig/cuda_host/cuda_fp8.h b/tools/zig/cuda_host/cuda_fp8.h
new file mode 100644
index 0000000000..eca6a59006
--- /dev/null
+++ b/tools/zig/cuda_host/cuda_fp8.h
@@ -0,0 +1,8 @@
+#pragma once
+#include "cuda_fp16.h"
+using __nv_fp8_storage_t=unsigned char; using __nv_fp8x2_storage_t=unsigned short;
+enum { __NV_SATFINITE, __NV_E4M3 };
+struct __nv_fp8_e4m3 { unsigned char __x; __nv_fp8_e4m3()=default; __device__ explicit __nv_fp8_e4m3(float); __device__ explicit operator float() const; };
+__device__ __nv_fp8_storage_t __nv_cvt_float_to_fp8(float,int,int);
+__device__ __half_raw __nv_cvt_fp8_to_halfraw(__nv_fp8_storage_t,int);
+__device__ __half2_raw __nv_cvt_fp8x2_to_halfraw2(__nv_fp8x2_storage_t,int);
diff --git a/tools/zig/cuda_host/cuda_runtime.h b/tools/zig/cuda_host/cuda_runtime.h
new file mode 100644
index 0000000000..a6fd866351
--- /dev/null
+++ b/tools/zig/cuda_host/cuda_runtime.h
@@ -0,0 +1,78 @@
+#pragma once
+#define __host__ __attribute__((host))
+#define __device__ __attribute__((device))
+#define __global__ __attribute__((global))
+#define __shared__ __attribute__((shared))
+#define __constant__ __attribute__((constant))
+#define __forceinline__ inline __attribute__((always_inline))
+#define __noinline__ __attribute__((noinline))
+#define __align__(n) __attribute__((aligned(n)))
+#define __launch_bounds__(...) __attribute__((launch_bounds(__VA_ARGS__)))
+#define __grid_constant__
+#define __CUDACC__ 1
+#include
+#include
+#include
+#include
+template __host__ __device__ T min(T,T);
+template __host__ __device__ T max(T,T);
+struct dim3 { unsigned x, y, z; __host__ __device__ constexpr dim3(unsigned a=1, unsigned b=1, unsigned c=1):x(a),y(b),z(c){} };
+extern __device__ const dim3 threadIdx, blockIdx, blockDim, gridDim;
+struct uint2 { unsigned x,y; }; struct uint3 { unsigned x,y,z; }; struct uint4 { unsigned x,y,z,w; };
+struct int2 { int x,y; }; struct int4 { int x,y,z,w; };
+struct float2 { float x,y; }; struct float4 { float x,y,z,w; };
+struct double2 { double x,y; }; struct ulonglong2 { unsigned long long x,y; };
+__device__ float2 make_float2(float,float); __device__ float4 make_float4(float,float,float,float);
+__device__ uint2 make_uint2(unsigned,unsigned); __device__ uint4 make_uint4(unsigned,unsigned,unsigned,unsigned);
+template __device__ T __ldg(const T*); template __device__ T __ldcg(const T*);
+template __device__ T __ldcv(const T*);
+template __device__ T __shfl_sync(unsigned,T,int,int=32);
+template __device__ T __shfl_xor_sync(unsigned,T,int,int=32);
+template __device__ T __shfl_up_sync(unsigned,T,unsigned,int=32);
+template __device__ T __shfl_down_sync(unsigned,T,unsigned,int=32);
+__device__ void __syncthreads(); __device__ int __syncthreads_count(int); __device__ void __syncwarp(unsigned=~0u);
+__device__ void __threadfence(); __device__ void __threadfence_system(); __device__ void __trap();
+__device__ void __nanosleep(unsigned); __device__ unsigned long long clock64();
+__device__ unsigned __ballot_sync(unsigned,int); __device__ int __any_sync(unsigned,int);
+__device__ unsigned __match_any_sync(unsigned,unsigned long long);
+__device__ unsigned __reduce_add_sync(unsigned,unsigned); __device__ int __reduce_add_sync(unsigned,int);
+__device__ unsigned __reduce_max_sync(unsigned,unsigned); __device__ int __reduce_max_sync(unsigned,int);
+__device__ unsigned __reduce_min_sync(unsigned,unsigned); __device__ int __reduce_min_sync(unsigned,int);
+__device__ unsigned __funnelshift_l(unsigned,unsigned,unsigned);
+__device__ unsigned __funnelshift_r(unsigned,unsigned,unsigned);
+__device__ unsigned __dp4a(unsigned,unsigned,unsigned); __device__ int __dp4a(int,int,int);
+__device__ unsigned __byte_perm(unsigned,unsigned,unsigned); __device__ unsigned __brev(unsigned);
+__device__ int __clz(unsigned); __device__ int __popc(unsigned); __device__ int __popcll(unsigned long long);
+__device__ int __ffs(int); __device__ int __ffsll(long long);
+__device__ unsigned __umulhi(unsigned,unsigned); __device__ size_t __cvta_generic_to_shared(const void*);
+__device__ int __float_as_int(float);
+__device__ unsigned __float_as_uint(float); __device__ float __int_as_float(int); __device__ float __uint_as_float(unsigned);
+__device__ double __longlong_as_double(long long); __device__ long long __double2ll_rz(double);
+__device__ float __double2float_rn(double);
+__device__ float __fadd_rn(float,float); __device__ float __fsub_rn(float,float);
+__device__ float __fmul_rn(float,float); __device__ float __fdiv_rn(float,float); __device__ float __fsqrt_rn(float);
+__device__ float __fmaf_rn(float,float,float); __device__ float __expf(float);
+__device__ double __dadd_rn(double,double); __device__ double __dsub_rn(double,double);
+__device__ double __dmul_rn(double,double); __device__ double __ddiv_rn(double,double);
+template __device__ T atomicAdd(T*,T); template __device__ T atomicMax(T*,T);
+template __device__ T atomicMin(T*,T); template __device__ T atomicExch(T*,T);
+template __device__ T atomicCAS(T*,T,T); template __device__ T atomicOr(T*,T);
+__device__ unsigned atomicInc(unsigned*,unsigned);
+using cudaStream_t=void*;
+using cudaError_t=int;
+enum { cudaSuccess, cudaFuncAttributeMaxDynamicSharedMemorySize };
+template int cudaFuncSetAttribute(T,int,int);
+template int cudaOccupancyMaxActiveBlocksPerMultiprocessor(int*,T,int,size_t);
+extern "C" int cudaConfigureCall(dim3,dim3,size_t=0,cudaStream_t=nullptr);
+extern "C" int cudaSetupArgument(const void*,size_t,size_t);
+extern "C" int cudaLaunch(const void*);
+__device__ int2 make_int2(int,int); __device__ uint3 make_uint3(unsigned,unsigned,unsigned);
+int cudaGetLastError();
+enum { cudaErrorInvalidValue, cudaFuncAttributePreferredSharedMemoryCarveout, cudaSharedmemCarveoutMaxShared,
+ cudaLaunchAttributeClusterDimension, cudaLaunchAttributeProgrammaticStreamSerialization };
+struct cudaLaunchAttribute { int id; struct { dim3 clusterDim; int programmaticStreamSerializationAllowed; } val; };
+struct cudaLaunchConfig_t { dim3 gridDim,blockDim; size_t dynamicSmemBytes; cudaStream_t stream;
+ cudaLaunchAttribute* attrs; unsigned numAttrs; };
+template int cudaLaunchKernelEx(const cudaLaunchConfig_t*,F,A...);
+__host__ __device__ long min(long,int); __host__ __device__ long min(int,long);
+__host__ __device__ long max(long,int); __host__ __device__ long max(int,long);
diff --git a/tools/zig/cuda_host/cuda_runtime_api.h b/tools/zig/cuda_host/cuda_runtime_api.h
new file mode 100644
index 0000000000..cf546fc3b3
--- /dev/null
+++ b/tools/zig/cuda_host/cuda_runtime_api.h
@@ -0,0 +1,2 @@
+#pragma once
+#include "cuda_runtime.h"
diff --git a/tools/zig/cuda_host/exl3_layout.cpp b/tools/zig/cuda_host/exl3_layout.cpp
new file mode 100644
index 0000000000..1c8d51f791
--- /dev/null
+++ b/tools/zig/cuda_host/exl3_layout.cpp
@@ -0,0 +1,14 @@
+#include "../../../zig/kernels/cuda/exl3_decode.cuh"
+
+// Invert the documented mma B-fragment coordinates independently, covering every lane and value.
+constexpr bool layout() {
+ for (int row = 0; row < 16; ++row) {
+ for (int col = 0; col < 16; ++col) {
+ const int lane = 4 * (col % 8) + (row % 8) / 2;
+ const int value = 4 * (col / 8) + 2 * (row / 8) + row % 2;
+ if (tf_exl3::value_row(lane, value) != row || tf_exl3::value_col(lane, value) != col) return false;
+ }
+ }
+ return true;
+}
+static_assert(layout(), "EXL3 unpack coordinates must bijectively cover the mma 16x16 tile");
diff --git a/tools/zig/cuda_host/math.h b/tools/zig/cuda_host/math.h
new file mode 100644
index 0000000000..4aade403d6
--- /dev/null
+++ b/tools/zig/cuda_host/math.h
@@ -0,0 +1,23 @@
+#pragma once
+#define INFINITY (__builtin_inff())
+#define NAN (__builtin_nanf(""))
+__host__ __device__ float fminf(float,float); __host__ __device__ float fmaxf(float,float);
+__host__ __device__ float expf(float); __host__ __device__ float exp2f(float); __host__ __device__ float logf(float);
+__host__ __device__ float log2f(float); __host__ __device__ float log1pf(float); __host__ __device__ float sqrtf(float);
+__host__ __device__ float rsqrtf(float); __host__ __device__ float sinf(float); __host__ __device__ float cosf(float);
+__host__ __device__ float tanhf(float); __host__ __device__ float fabsf(float); __host__ __device__ float floorf(float);
+__host__ __device__ float ceilf(float); __host__ __device__ float roundf(float); __host__ __device__ float nearbyintf(float);
+__host__ __device__ float powf(float,float); __host__ __device__ float fmaf(float,float,float);
+__host__ __device__ double fma(double,double,double); __host__ __device__ double sqrt(double);
+__host__ __device__ double exp(double); __host__ __device__ double log(double); __host__ __device__ double fabs(double);
+__host__ __device__ double floor(double); __host__ __device__ double nearbyint(double);
+__host__ __device__ double fmin(double,double); __host__ __device__ double fmax(double,double);
+__host__ __device__ void sincosf(float,float*,float*);
+template __host__ __device__ bool isnan(T); template __host__ __device__ bool isinf(T);
+template __host__ __device__ bool isfinite(T);
+#define M_SQRT2 1.41421356237309504880
+#define M_2_SQRTPI 1.12837916709551257390
+#define M_SQRT1_2 0.70710678118654752440
+__host__ __device__ float erff(float); __host__ __device__ float rintf(float);
+__host__ __device__ double pow(double,double); __host__ __device__ double sin(double); __host__ __device__ double cos(double);
+template __host__ __device__ bool signbit(T);
diff --git a/tools/zig/cuda_host/math_constants.h b/tools/zig/cuda_host/math_constants.h
new file mode 100644
index 0000000000..e4559c4f8d
--- /dev/null
+++ b/tools/zig/cuda_host/math_constants.h
@@ -0,0 +1,3 @@
+#pragma once
+#define CUDART_INF_F (__builtin_inff())
+#define CUDART_NAN_F (__builtin_nanf(""))
diff --git a/tools/zig/cuda_host/vector b/tools/zig/cuda_host/vector
new file mode 100644
index 0000000000..8b14fbed74
--- /dev/null
+++ b/tools/zig/cuda_host/vector
@@ -0,0 +1,2 @@
+#pragma once
+#include
diff --git a/tools/zig/dsv41_tp_http_probe.py b/tools/zig/dsv41_tp_http_probe.py
new file mode 100644
index 0000000000..fe7f99bedc
--- /dev/null
+++ b/tools/zig/dsv41_tp_http_probe.py
@@ -0,0 +1,245 @@
+"""Compare one-rank and paired native HTTP replies, then verify refusal after peer loss."""
+import argparse
+import concurrent.futures
+import json
+import pathlib
+import time
+import threading
+import urllib.error
+import urllib.request
+
+PROMPTS = [
+ [{"role": "user", "content": "Antworte nur mit dem Ergebnis: 17 + 25 ="}],
+ [{"role": "user", "content": "Nenne drei deutsche Farben, durch Kommas getrennt."}],
+ [{"role": "user", "content": "Merke dir das Wort Kiefer."},
+ {"role": "assistant", "content": "Kiefer."},
+ {"role": "user", "content": "Welches Wort war es? Antworte mit einem Wort."}],
+]
+
+
+def payload(index, stream=False):
+ return {"model": "DeepSeek-TP-Probe", "messages": PROMPTS[index],
+ "temperature": 0, "max_tokens": 32, "stream": stream}
+
+
+def normalize(body):
+ choices = body.get("choices", [])
+ assert len(choices) == 1 and choices[0].get("finish_reason"), body
+ message = choices[0]["message"]
+ assert not message.get("tool_calls"), message
+ fields = {key: message.get(key) or "" for key in ("content", "reasoning_content", "reasoning")}
+ assert any(fields.values()), "No generated text or reasoning"
+ return {"message": fields, "finish_reason": choices[0]["finish_reason"]}
+
+
+def equal(actual, expected):
+ assert actual == expected, {"actual": actual, "expected": expected}
+
+
+def request(url, data, timeout=180):
+ started = time.monotonic()
+ req = urllib.request.Request(url.rstrip('/') + '/v1/chat/completions',
+ data=json.dumps(data).encode(),
+ headers={'Content-Type': 'application/json'})
+ try:
+ with urllib.request.urlopen(req, timeout=timeout) as response:
+ raw = response.read()
+ status = response.status
+ except urllib.error.HTTPError as exc:
+ raw = exc.read()
+ status = exc.code
+ # Network failure is not evidence that a live leader refused a request.
+ return {"status": status, "seconds": time.monotonic()-started,
+ "body": json.loads(raw)}
+
+
+def streamed(url, index):
+ req = urllib.request.Request(url.rstrip('/') + '/v1/chat/completions',
+ data=json.dumps(payload(index, True)).encode(),
+ headers={'Content-Type': 'application/json'})
+ fields = {key: "" for key in ("content", "reasoning_content", "reasoning")}
+ chunks, reason, done = [], None, False
+ first_delta_seconds = None
+ started = time.monotonic()
+ with urllib.request.urlopen(req, timeout=180) as response:
+ assert response.status == 200
+ for raw in response:
+ line = raw.decode().strip()
+ if not line.startswith('data:'):
+ continue
+ data = line[5:].strip()
+ if data == '[DONE]':
+ done = True
+ break
+ chunk = json.loads(data)
+ chunks.append(chunk)
+ assert 'error' not in chunk, chunk
+ for choice in chunk.get('choices', []):
+ assert choice.get('index', 0) == 0
+ delta = choice.get('delta', {})
+ assert not delta.get('tool_calls'), delta
+ if first_delta_seconds is None and any(delta.get(key) for key in fields):
+ first_delta_seconds = time.monotonic() - started
+ for key in fields:
+ fields[key] += delta.get(key) or ''
+ if choice.get('finish_reason') is not None:
+ reason = choice['finish_reason']
+ assert done and reason and any(fields.values()), (done, reason, fields)
+ return {"seconds": time.monotonic()-started, "first_delta_seconds": first_delta_seconds, "chunks": chunks,
+ "normalized": {"message": fields, "finish_reason": reason}}
+
+
+def metrics(url):
+ with urllib.request.urlopen(url.rstrip('/') + '/metrics', timeout=5) as response:
+ text = response.read().decode()
+ return {line.split()[0]: float(line.split()[1]) for line in text.splitlines()
+ if line and not line.startswith('#')}
+
+
+def cancel_stream(url):
+ before = metrics(url)
+ data = payload(1, True)
+ data['max_tokens'] = 128
+ req = urllib.request.Request(url.rstrip('/') + '/v1/chat/completions',
+ data=json.dumps(data).encode(),
+ headers={'Content-Type': 'application/json'})
+ chunks = []
+ with urllib.request.urlopen(req, timeout=180) as response:
+ assert response.status == 200
+ for raw in response:
+ line = raw.decode().strip()
+ if not line.startswith('data:'):
+ continue
+ value = line[5:].strip()
+ assert value != '[DONE]', 'Stream completed before cancellation'
+ chunk = json.loads(value)
+ chunks.append(chunk)
+ assert 'error' not in chunk, chunk
+ choices = chunk.get('choices', [])
+ assert not any(c.get('finish_reason') for c in choices), 'Reply finished before cancellation'
+ if any(any(c.get('delta', {}).get(key) for key in ('content', 'reasoning_content', 'reasoning'))
+ for c in choices):
+ break
+ else:
+ raise AssertionError('No generated delta before EOF')
+ closed = time.monotonic()
+ samples = []
+ while time.monotonic() - closed < 20:
+ after = metrics(url)
+ samples.append({'seconds': time.monotonic() - closed, 'metrics': after})
+ if (after['tensorfold:client_disconnections_total'] > before['tensorfold:client_disconnections_total']
+ and after['tensorfold:requests_running'] == 0
+ and after['tensorfold:requests_waiting'] == 0):
+ return {'before': before, 'partial_chunks': chunks, 'after_close': samples,
+ 'server_observed_disconnect': True}
+ time.sleep(0.1)
+ return {'before': before, 'partial_chunks': chunks, 'after_close': samples,
+ 'server_observed_disconnect': False}
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument('mode', choices=['baseline', 'parity', 'refuse'])
+ parser.add_argument('url')
+ parser.add_argument('out', type=pathlib.Path)
+ parser.add_argument('--baseline', type=pathlib.Path)
+ parser.add_argument('--follower')
+ args = parser.parse_args()
+ args.out.mkdir(parents=True, exist_ok=False)
+ receipts = []
+
+ def save(name, value):
+ (args.out / (name + '.json')).write_text(json.dumps(value, indent=2) + '\n')
+ receipts.append(name)
+ (args.out / 'progress.json').write_text(json.dumps({'completed': receipts}) + '\n')
+
+ if args.mode == 'refuse':
+ for i in range(2):
+ result = request(args.url, payload(0), timeout=35)
+ save('refusal-' + str(i), result)
+ assert result['status'] >= 400 and not result['body'].get('choices'), result
+ assert 'restart both ranks' in json.dumps(result['body']).lower(), result
+ assert result['seconds'] < (35 if i == 0 else 5), result
+ else:
+ expected = []
+ if args.mode == 'parity':
+ assert args.baseline and args.follower
+ prior = json.loads((args.baseline / 'result.json').read_text())
+ assert prior['state'] == 'passed' and prior['mode'] == 'baseline', prior
+ expected = json.loads((args.baseline / 'expected.json').read_text())
+ assert len(expected) == len(PROMPTS)
+ follower = request(args.follower, payload(0), timeout=35)
+ save('follower-refusal', follower)
+ assert follower['status'] >= 400 and not follower['body'].get('choices'), follower
+ assert 'follows rank 0' in json.dumps(follower['body']).lower(), follower
+ solo, benchmark = [], []
+ for repeat in range(3):
+ for i in range(len(PROMPTS)):
+ result = request(args.url, payload(i))
+ save(('solo-' if repeat == 0 else 'repeat-' + str(repeat) + '-solo-') + str(i), result)
+ assert result['status'] == 200, result
+ current = normalize(result['body'])
+ if expected:
+ equal(current, expected[i])
+ if repeat == 0:
+ solo.append(current)
+ else:
+ equal(current, solo[i])
+ benchmark.append({'repeat': repeat, 'prompt': i, 'seconds': result['seconds'], 'usage': result['body'].get('usage')})
+ sample = streamed(args.url, 0)
+ save('repeat-' + str(repeat) + '-stream', sample)
+ equal(sample['normalized'], solo[0])
+ save('benchmark', benchmark)
+ save('expected', solo)
+ if args.mode == 'parity':
+ # Four repetitions per prompt; two workers match the server's two lane slots.
+ order = [0, 1, 2, 1, 0, 2, 2, 0, 1, 0, 2, 1]
+ samples, errors = [], []
+ stop = threading.Event()
+
+ def observe():
+ while not stop.is_set():
+ try:
+ with urllib.request.urlopen(args.url.rstrip('/') + '/metrics', timeout=3) as response:
+ text = response.read().decode()
+ values = {line.split()[0]: float(line.split()[1]) for line in text.splitlines()
+ if line and not line.startswith('#')}
+ samples.append({'at': time.monotonic(),
+ 'running': values['tensorfold:requests_running'],
+ 'waiting': values['tensorfold:requests_waiting']})
+ except Exception as exc:
+ errors.append(type(exc).__name__ + ': ' + str(exc))
+ stop.wait(0.1)
+
+ monitor = threading.Thread(target=observe, daemon=True)
+ monitor.start()
+ try:
+ with concurrent.futures.ThreadPoolExecutor(max_workers=2) as pool:
+ futures = [pool.submit(request, args.url, payload(i)) for i in order]
+ for n, (i, future) in enumerate(zip(order, futures)):
+ result = future.result()
+ save('concurrent-' + str(n), result)
+ assert result['status'] == 200, result
+ equal(normalize(result['body']), solo[i])
+ finally:
+ stop.set()
+ monitor.join(timeout=5)
+ save('concurrency-observation', {'samples': samples, 'errors': errors})
+ assert not monitor.is_alive() and not errors, errors
+ assert samples and max(x['running'] for x in samples) >= 2, 'No evidence of two admitted requests'
+ stream = streamed(args.url, 0)
+ save('stream', stream)
+ equal(stream['normalized'], solo[0])
+ cancelled = cancel_stream(args.url)
+ save('cancelled-stream', cancelled)
+ assert cancelled['server_observed_disconnect'], 'No server-side cancellation evidence'
+ last = request(args.url, payload(0))
+ save('after-cancelled-stream', last)
+ assert last['status'] == 200
+ equal(normalize(last['body']), solo[0])
+ save('result', {'state': 'passed', 'mode': args.mode, 'scope': receipts[:]})
+ print('HTTP_PROBE_PASSED', args.mode, flush=True)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/tools/zig/dsv41_tp_http_probe_test.py b/tools/zig/dsv41_tp_http_probe_test.py
new file mode 100644
index 0000000000..74b94c1f78
--- /dev/null
+++ b/tools/zig/dsv41_tp_http_probe_test.py
@@ -0,0 +1,85 @@
+"""Synthetic controls for HTTP receipt validation, not model-performance evidence."""
+import http.server
+import json
+from pathlib import Path
+import subprocess
+import sys
+import tempfile
+import threading
+import unittest
+
+
+class ProbeControls(unittest.TestCase):
+ def exercise(self, mode):
+ class Handler(http.server.BaseHTTPRequestHandler):
+ calls = 0
+
+ def log_message(self, *args):
+ pass
+
+ def do_POST(self):
+ data = json.loads(self.rfile.read(int(self.headers['Content-Length'])))
+ text = 'fixture'
+ if not data.get('stream'):
+ type(self).calls += 1
+ if mode == 'unstable-repeat' and self.calls > 3:
+ text = 'changed'
+ body = {'choices': [{'message': {'content': text}, 'finish_reason': 'stop'}]}
+ raw = json.dumps(body).encode()
+ self.send_response(200)
+ self.send_header('Content-Length', str(len(raw)))
+ self.end_headers()
+ self.wfile.write(raw)
+ else:
+ self.send_response(200)
+ self.send_header('Content-Type', 'text/event-stream')
+ self.end_headers()
+ chunks = [
+ {'choices': [{'index': 0, 'delta': {'role': 'assistant'}}]},
+ {'choices': [{'index': 0, 'delta': {'content': text}}]},
+ {'choices': [{'index': 0, 'delta': {}, 'finish_reason': 'stop'}]},
+ ]
+ for item in chunks:
+ self.wfile.write(('data: ' + json.dumps(item) + '\n\n').encode())
+ if mode != 'truncated-stream':
+ self.wfile.write(b'data: [DONE]\n\n')
+
+ probe = Path(__file__).with_name('dsv41_tp_http_probe.py')
+ with tempfile.TemporaryDirectory() as temporary:
+ out = Path(temporary) / mode
+ server = http.server.ThreadingHTTPServer(('127.0.0.1', 0), Handler)
+ thread = threading.Thread(target=server.serve_forever)
+ thread.start()
+ try:
+ result = subprocess.run(
+ [sys.executable, str(probe), 'baseline',
+ 'http://127.0.0.1:' + str(server.server_port), str(out)],
+ capture_output=True, timeout=30,
+ )
+ finally:
+ server.shutdown()
+ thread.join()
+ server.server_close()
+ if mode == 'valid':
+ self.assertEqual(result.returncode, 0, result.stderr.decode())
+ self.assertEqual(json.loads((out / 'result.json').read_text())['state'], 'passed')
+ self.assertEqual(len(json.loads((out / 'benchmark.json').read_text())), 9)
+ for repeat in range(3):
+ receipt = json.loads((out / f'repeat-{repeat}-stream.json').read_text())
+ self.assertGreaterEqual(receipt['first_delta_seconds'], 0)
+ else:
+ self.assertNotEqual(result.returncode, 0)
+ self.assertFalse((out / 'result.json').exists())
+
+ def test_repeated_answers_and_complete_streams_pass(self):
+ self.exercise('valid')
+
+ def test_changed_repeated_answer_fails(self):
+ self.exercise('unstable-repeat')
+
+ def test_stream_without_done_marker_fails(self):
+ self.exercise('truncated-stream')
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/tools/zig/gate_visit.py b/tools/zig/gate_visit.py
index a5e5901243..6533c2ebbf 100644
--- a/tools/zig/gate_visit.py
+++ b/tools/zig/gate_visit.py
@@ -15,8 +15,11 @@
def capped(body: dict, plan: dict) -> dict:
- """The body with max_tokens cut to the plan's cap, when the plan sets one (short proof runs)."""
+ """Pin omitted sampling keys and apply the plan's optional token cap without changing named keys."""
+ body = {**body}
+ if body.get("seed") is None:
+ body["seed"] = 1234
cap = plan.get("max_tokens")
return {**body, "max_tokens": cap} if cap and body["max_tokens"] > cap else body
@@ -48,7 +51,7 @@ def phases(client: C.Client, plan: dict, visit_no: int, canonical: Path, checks:
if "resume" in want:
resume(client, plan, visit_no, canonical, checks)
for name, body in W.ladder(model, visit_no, plan["ladder"], plan["ladder_tokens"]) if "ladder" in want else ():
- row = client.send("ladder", name, body, cell=name, unit=f"v{visit_no}")
+ row = client.send("ladder", name, capped(body, plan), cell=name, unit=f"v{visit_no}")
checks.append({"check": "ladder-cold", "name": name, "pass": row["error"] is None and row["cached_tokens"] < 64})
replies = sorted({str(row.get("reply_model")) for row in client.rows})
checks.append({"check": "reply==requested", "name": model, "replies": replies,
@@ -89,7 +92,7 @@ def resume(client: C.Client, plan: dict, visit_no: int, canonical: Path, checks:
canonical.write_text(json.dumps(bodies, indent=1) + "\n")
elif canonical.exists():
for spec in json.loads(canonical.read_text()):
- fresh, _ = C.pair(client, "resume", spec["name"].replace("prefix", "fresh"), spec["body"], checks)
+ fresh, _ = C.pair(client, "resume", spec["name"].replace("prefix", "fresh"), capped(spec["body"], plan), checks)
checks.append({"check": "fresh==resumed", "name": spec["name"], "fresh_cached": fresh["cached_tokens"],
"pass": fresh["cached_tokens"] == 0 and fresh["error"] is None
and all(fresh[k] == spec["resumed"][k] for k in ("content", "reasoning", "token_sha"))})
diff --git a/tools/zig/lean_check.py b/tools/zig/lean_check.py
index a97aa37701..6b85198058 100644
--- a/tools/zig/lean_check.py
+++ b/tools/zig/lean_check.py
@@ -14,8 +14,9 @@
DOCSTRING = re.compile(r'^\s*(?:[rubf]{0,2})("""|\'\'\')')
GENERATED = ("unicode_data.zig",)
HEX_METAL = re.compile(r"_[0-9a-f]{16}\.metal$")
-# Generator outputs are skipped entirely. unicode_data.zig stays exempt from the length check only.
+# Generated outputs are skipped entirely. unicode_data.zig has only the length exemption.
GENERATOR_OUTPUT = (
+ "zig/src/core/tokenizer/nfkd_data.zig",
"zig/src/families/flashnext/roles_gen.zig",
"zig/kernels/metal/flashnext/sources_gen.zig",
)
diff --git a/tools/zig/nfkd_tables.py b/tools/zig/nfkd_tables.py
new file mode 100644
index 0000000000..5da16d2604
--- /dev/null
+++ b/tools/zig/nfkd_tables.py
@@ -0,0 +1,82 @@
+"""Regenerate the Engram normalizer's Unicode 16.0.0 tables without changing tokenizer policy."""
+import argparse
+import re
+import subprocess
+import sys
+import unicodedata as ucd
+from pathlib import Path
+
+from unicode_tables import combining_runs, rows
+
+ROOT = Path(__file__).resolve().parents[2]
+VERSION = "16.0.0"
+
+
+def mappings(transform):
+ """Sorted mappings into concatenated code-point sequences, including Hangul decomposition."""
+ entries, sequences = [], []
+ for cp in range(0x110000):
+ text = transform(chr(cp))
+ if text != chr(cp):
+ entries.append((cp, len(sequences), len(text)))
+ sequences.extend(map(ord, text))
+ return entries, sequences
+
+
+def render():
+ """Emit data only; normalization order and mark removal stay in token_map.zig."""
+ if ucd.unidata_version != VERSION:
+ raise ValueError(f"Need Unicode {VERSION}, found {ucd.unidata_version}")
+ decompositions, sequences = mappings(lambda s: ucd.normalize("NFKD", s))
+ lowercases, lower_sequences = mappings(str.lower)
+ marks = [cp for cp in range(0x110000) if ucd.category(chr(cp)).startswith("M")]
+ cccs = combining_runs(range(0x110000))
+ scalar = lambda v: f"0x{v:04X}"
+ entry = lambda v: f".{{ .cp = 0x{v[0]:04X}, .start = {v[1]}, .len = {v[2]} }}"
+ triple = lambda v: ".{ " + ", ".join(scalar(x) for x in v) + " }"
+ out = [
+ f"//! Generated by tools/zig/nfkd_tables.py from Unicode {VERSION}; do not edit.",
+ "", "pub const Decomp = struct { cp: u21, start: u32, len: u8 };", "",
+ "/// All NFKD sequences concatenated, indexed by Decomp.start/.len.",
+ "pub const sequences = [_]u21{", *rows(sequences, 8, scalar), "};", "",
+ "pub const decompositions = [_]Decomp{", *rows(decompositions, 1, entry), "};", "",
+ "/// Sorted Mn/Mc/Me code points; StripAccents removes all category M* characters.",
+ "pub const marks = [_]u21{", *rows(marks, 8, scalar), "};", "",
+ "pub const Lower = struct { cp: u21, start: u32, len: u8 };", "",
+ "/// Full lowercase mappings concatenated (Rust char::to_lowercase semantics).",
+ "pub const lower_sequences = [_]u21{", *rows(lower_sequences, 8, scalar), "};", "",
+ "pub const lowercases = [_]Lower{", *rows(lowercases, 1, entry), "};", "",
+ "/// First, last, canonical combining class for nonzero combining classes.",
+ "pub const cccs = [_][3]u21{", *rows(cccs, 4, triple), "};", "",
+ ]
+ return "\n".join(out)
+
+
+def tokens(text):
+ """Ignore comments and whitespace when checking an existing generated data file."""
+ return re.sub(r"\s+", "", re.sub(r"//[^\n]*", "", text))
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ parser.add_argument("--check", action="store_true")
+ parser.add_argument("--zig", default="zig", help="Zig 0.17.0 formatter, used when writing")
+ args = parser.parse_args()
+ path = ROOT / "zig/src/core/tokenizer/nfkd_data.zig"
+ text = render()
+ if args.check:
+ if tokens(path.read_text()) != tokens(text):
+ sys.exit("NFKD table data differs from Unicode 16.0.0")
+ print("NFKD_DATA_IDENTICAL Unicode", VERSION)
+ return
+ version = subprocess.check_output([args.zig, "version"], text=True).strip()
+ if version != "0.17.0":
+ sys.exit(f"Need Zig 0.17.0, found {version}")
+ formatted = subprocess.run([args.zig, "fmt", "--stdin"], input=text, text=True,
+ capture_output=True, check=True).stdout
+ path.write_text(formatted)
+ print("Wrote", path.relative_to(ROOT))
+
+
+if __name__ == "__main__":
+ main()
diff --git a/tools/zig/test_cuda_sources.py b/tools/zig/test_cuda_sources.py
new file mode 100644
index 0000000000..53e4adcab8
--- /dev/null
+++ b/tools/zig/test_cuda_sources.py
@@ -0,0 +1,94 @@
+"""Controls for the shader-free CUDA source/include check."""
+from pathlib import Path
+import shutil
+import subprocess
+import tempfile
+import unittest
+
+import check_cuda_sources as check
+
+
+class SourceNames(unittest.TestCase):
+ def setUp(self):
+ self.compiler = shutil.which("clang++")
+ if self.compiler is None:
+ self.skipTest("clang++ is required")
+ scratch = check.ROOT / "work"
+ scratch.mkdir(exist_ok=True)
+ self.temp = tempfile.TemporaryDirectory(dir=scratch)
+ self.addCleanup(self.temp.cleanup)
+ self.root = Path(self.temp.name)
+
+ def parse(self, source):
+ return subprocess.run(check.command(self.compiler, source, 1210), capture_output=True, text=True)
+
+ def test_include_chain_supplies_unrelated_project_name(self):
+ (self.root / "inner.cuh").write_text("__device__ int project_fixture_helper(int x) { return x; }\n")
+ (self.root / "outer.cuh").write_text('#include "inner.cuh"\n')
+ source = self.root / "fixture.cu"
+ source.write_text('#include "outer.cuh"\n__global__ void k(int* x) { *x = project_fixture_helper(*x); }\n')
+ self.assertEqual(self.parse(source).returncode, 0)
+ (self.root / "outer.cuh").write_text("")
+ result = self.parse(source)
+ self.assertNotEqual(result.returncode, 0)
+ self.assertIn("undeclared identifier 'project_fixture_helper'", result.stderr)
+
+ def test_launch_bound_names_are_resolved(self):
+ source = self.root / "bounds.cu"
+ source.write_text("__global__ void __launch_bounds__(project_bound) k() {}\n")
+ result = self.parse(source)
+ self.assertNotEqual(result.returncode, 0)
+ self.assertIn("undeclared identifier 'project_bound'", result.stderr)
+ source.write_text("constexpr int project_bound = 32;\n" + source.read_text())
+ self.assertEqual(self.parse(source).returncode, 0)
+
+ def test_cluster_api_requires_sm90(self):
+ source = self.root / "cluster.cu"
+ source.write_text('#include \n__global__ void k() { cooperative_groups::this_cluster().sync(); }\n')
+ low = check.check(self.compiler, source, 890)
+ self.assertNotEqual(low["returncode"], 0, low["diagnostics"])
+ self.assertEqual(check.check(self.compiler, source, 900)["returncode"], 0)
+
+ def test_fp8_conversion_requires_sm89_after_preprocessing(self):
+ source = self.root / "fp8.cu"
+ for fmt in ("e4m3x2", "e5m2x2"):
+ source.write_text('__device__ unsigned k(unsigned short x) { unsigned y; asm("cvt.rn.f16x2.' + fmt + ' %0, %1;" : "=r"(y) : "h"(x)); return y; }\n')
+ self.assertNotEqual(check.check(self.compiler, source, 860)["returncode"], 0)
+ self.assertEqual(check.check(self.compiler, source, 890)["returncode"], 0)
+ source.write_text('#if __CUDA_ARCH__ >= 890\n' + source.read_text() + '#endif\n')
+ self.assertEqual(check.check(self.compiler, source, 860)["returncode"], 0)
+
+ def test_cluster_ptx_requires_sm90_after_preprocessing(self):
+ source = self.root / "cluster_ptx.cu"
+ for op in ("barrier.cluster.arrive.aligned;", "barrier.cluster.wait.aligned;",
+ "mapa.shared::cluster.u32 %0, %0, 0;", "ld.shared::cluster.u32 %0, [%0];"):
+ body = '__device__ void k() { unsigned x = 0; __asm__ __volatile__("' + op + '" : "+r"(x)); }\n'
+ source.write_text(body)
+ low = check.check(self.compiler, source, 890)
+ self.assertNotEqual(low["returncode"], 0, op)
+ self.assertIn("requires sm_90+", low["diagnostics"])
+ self.assertEqual(check.check(self.compiler, source, 900)["returncode"], 0)
+ source.write_text('#if __CUDA_ARCH__ >= 900\n' + body + '#endif\n')
+ self.assertEqual(check.check(self.compiler, source, 890)["returncode"], 0)
+ source.write_text('// asm("barrier.cluster.arrive;");\n__device__ const char* p = "mapa.shared::cluster";\n')
+ self.assertEqual(check.check(self.compiler, source, 890)["returncode"], 0)
+
+ def test_real_exl3_layout(self):
+ result = self.parse(check.HEADERS / "exl3_layout.cpp")
+ self.assertEqual(result.returncode, 0, result.stderr)
+
+ def test_layout_rejects_wrong_coordinate(self):
+ header = (check.ROOT / "zig/kernels/cuda/exl3_decode.cuh").read_text()
+ bad = header.replace("return (lane >> 2) + 8 * (j >> 2);", "return (lane >> 2);", 1)
+ self.assertNotEqual(header, bad)
+ (self.root / "bad.cuh").write_text(bad)
+ test = (check.HEADERS / "exl3_layout.cpp").read_text().splitlines()
+ source = self.root / "layout.cu"
+ source.write_text('#include "bad.cuh"\n' + "\n".join(test[1:]) + "\n")
+ result = self.parse(source)
+ self.assertNotEqual(result.returncode, 0)
+ self.assertIn("static assertion failed", result.stderr)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/zig/test_gate_seeds.py b/tools/zig/test_gate_seeds.py
new file mode 100644
index 0000000000..973f46f8c0
--- /dev/null
+++ b/tools/zig/test_gate_seeds.py
@@ -0,0 +1,78 @@
+"""Release phases retain exact comparisons with explicit keys, including old stored resume bodies."""
+from pathlib import Path
+import importlib.util
+import json
+import sys
+import tempfile
+import types
+import unittest
+from unittest.mock import patch
+
+HERE = Path(__file__).resolve().parent
+
+
+class Client:
+ model = "fixture"
+
+ def __init__(self):
+ self.bodies = []
+
+ def send(self, phase, name, body, **fields):
+ self.bodies.append(body)
+ return {"error": None, "cached_tokens": 0, "content": "fixed", "reasoning": "",
+ "token_sha": "fixed", "stream": fields.get("stream", 0), "aggregate_tps": 1,
+ "reply_model": self.model}
+
+ @property
+ def rows(self):
+ return [{"reply_model": self.model}]
+
+ def send_many(self, phase, name, bodies, **fields):
+ return [self.send(phase, name, body, stream=i) for i, body in enumerate(bodies)]
+
+
+class GateSeeds(unittest.TestCase):
+ def test_all_phases_pin_omitted_keys_and_keep_named_keys(self):
+ work = types.ModuleType("qual_work")
+ body = {"temperature": 0.7, "max_tokens": 64}
+ work.CODE, work.SEEDS = ["prompt"], [7]
+ for name in ("warmup", "serial9", "resume_prefixes", "ladder"):
+ setattr(work, name, lambda *a: [("fixture", body)])
+ work.cells = lambda *a, **kw: [("sampled", "p0-s7", {**body, "seed": 7})]
+ work.sweep_body = lambda *a: {**body, "seed": None}
+ work.follow_up = lambda *a: body
+ client_module = types.ModuleType("qual_client")
+
+ def pair(client, phase, name, request, checks):
+ one = client.send(phase, name, request)
+ two = client.send(phase, name, {**request, "draft": False})
+ self.assertEqual(one["token_sha"], two["token_sha"])
+ return one, two
+
+ client_module.pair = pair
+ client_module.same = lambda one, two: one["token_sha"] == two["token_sha"]
+ modules = {"qual_work": work, "qual_client": client_module, "qual_run": types.ModuleType("qual_run")}
+ with patch.dict(sys.modules, modules), patch.object(sys, "path", [str(HERE), *sys.path]):
+ spec = importlib.util.spec_from_file_location("seed_gate_visit", HERE / "gate_visit.py")
+ gate = importlib.util.module_from_spec(spec)
+ spec.loader.exec_module(gate)
+ plan = {"phases": ["serial9", "cells", "sweep", "resume", "ladder"], "cell_tokens": [64],
+ "sweep": [2], "sweep_reps": 1, "ladder": [1], "ladder_tokens": 64, "max_tokens": 16}
+ client = Client()
+ with tempfile.TemporaryDirectory() as folder:
+ canonical = Path(folder) / "resume.json"
+ gate.phases(client, plan, 1, canonical, [])
+ # A preexisting canonical file may have omitted its key too.
+ stored = json.loads(canonical.read_text())
+ for entry in stored:
+ entry["body"].pop("seed", None)
+ canonical.write_text(json.dumps(stored))
+ gate.phases(client, plan, 2, canonical, [])
+ self.assertTrue(client.bodies)
+ self.assertTrue(all(b.get("seed") in (7, 1234) for b in client.bodies))
+ self.assertTrue(any(b["seed"] == 7 for b in client.bodies))
+ self.assertNotIn("seed", body)
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/zig/build/check_release_paths.zig b/zig/build/check_release_paths.zig
new file mode 100644
index 0000000000..e81451c705
--- /dev/null
+++ b/zig/build/check_release_paths.zig
@@ -0,0 +1,28 @@
+//! Check release payloads without echoing a private path into the build log.
+const std = @import("std");
+const paths = @import("release_paths.zig");
+pub fn main(init: std.process.Init) !void {
+ const args = try init.minimal.args.toSlice(init.arena.allocator());
+ if (args.len < 3) {
+ std.debug.print("usage: check-release-paths ABSOLUTE_BUILD_ROOT FILE...\n", .{});
+ std.process.exit(2);
+ }
+ if (!paths.validRoot(args[1])) {
+ std.debug.print("release privacy: BadBuildRoot: use a named absolute build root\n", .{});
+ std.process.exit(2);
+ }
+ var failed = false;
+ for (args[2..]) |file| {
+ const bytes = std.Io.Dir.cwd().readFileAlloc(init.io, file, init.gpa, .limited(1 << 30)) catch |err| {
+ std.debug.print("release privacy: unreadable {s} ({t})\n", .{ std.fs.path.basename(file), err });
+ failed = true;
+ continue;
+ };
+ defer init.gpa.free(bytes);
+ paths.check(init.gpa, bytes, args[1]) catch |err| {
+ std.debug.print("release privacy: refused {s} ({t})\n", .{ std.fs.path.basename(file), err });
+ failed = true;
+ };
+ }
+ if (failed) std.process.exit(1);
+}
diff --git a/zig/build/contract_symbols.zig b/zig/build/contract_symbols.zig
new file mode 100644
index 0000000000..38a427a980
--- /dev/null
+++ b/zig/build/contract_symbols.zig
@@ -0,0 +1,19 @@
+//! The compiler's SASS dump must contain every registered x3ld entry on every requested SM.
+const std = @import("std");
+
+pub fn check(b: *std.Build, image: std.Build.LazyPath, sms: []const u8, install: *std.Build.Step) void {
+ const step = b.step("check-dsv41-symbols", "Check the compiled x3ld image's registered entries for every SM");
+ const tool = b.option([]const u8, "cuobjdump", "CUDA cuobjdump for the compiler's x3ld symbol gate") orelse {
+ step.dependOn(&b.addFail("check-dsv41-symbols needs -Dcuobjdump=PATH and -Dnvcc=PATH or -Dfatbins=DIR").step);
+ return;
+ };
+ const dump = b.addSystemCommand(&.{ tool, "--dump-sass" });
+ dump.addFileArg(image);
+ const run = b.addSystemCommand(&.{"python3"});
+ run.addFileArg(b.path("tools/zig/check_cuda_symbols.py"));
+ run.addArgs(&.{ "--sms", sms, "--registry" });
+ run.addFileArg(b.path("zig/src/core/contract/dsv41_pending_1.zig"));
+ run.addFileArg(dump.captureStdOut(.{}));
+ step.dependOn(&run.step);
+ install.dependOn(step);
+}
diff --git a/zig/build/core_checks.zig b/zig/build/core_checks.zig
index ca6af8501d..cae2cc892c 100644
--- a/zig/build/core_checks.zig
+++ b/zig/build/core_checks.zig
@@ -6,6 +6,7 @@ const std = @import("std");
pub fn rowWidths(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, metal: *std.Build.Module, row_mod: *std.Build.Module) void {
const tokenizer = b.createModule(.{ .root_source_file = b.path("zig/src/core/tokenizer/tokenizer.zig"), .target = target, .optimize = optimize, .link_libc = true });
const core = b.createModule(.{ .root_source_file = b.path("zig/src/core/root.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{.{ .name = "tokenizer", .module = tokenizer }} });
+ core.addImport("gpu_memory", @import("gpu_memory.zig").module(b, target));
const exe = b.addExecutable(.{ .name = "tf-row-widths-check", .root_module = b.createModule(.{
.root_source_file = b.path("zig/tests/row_widths_check.zig"),
.target = target,
diff --git a/zig/build/cuda.zig b/zig/build/cuda.zig
index 5fadb17f06..6dfec448ca 100644
--- a/zig/build/cuda.zig
+++ b/zig/build/cuda.zig
@@ -3,9 +3,9 @@ const std = @import("std");
const exlr8_build = @import("exlr8.zig");
const golden_build = @import("golden.zig");
const catalog = @import("cuda_kernels.zig");
-const Kernel = catalog.Kernel;
-const torch_flags = catalog.torch_flags;
-const kernels = catalog.kernels;
+pub const Kernel = catalog.Kernel;
+pub const kernels = catalog.kernels;
+
fn stagger(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Module {
return b.createModule(.{ .root_source_file = b.path("zig/src/core/stagger.zig"), .target = target, .optimize = optimize });
}
@@ -17,19 +17,25 @@ pub fn runtime(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.bu
for (images) |i| with = with and i != null;
options.addOption(bool, "with_kernels", with);
const cuda = b.createModule(.{ .root_source_file = b.path("zig/src/cuda/root.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ cuda.addImport("gpu_memory", @import("gpu_memory.zig").module(b, target));
cuda.addOptions("kernel_options", options);
cuda.addImport("stagger", stagger(b, target, optimize));
if (with) for (kernels, images) |k, image| cuda.addAnonymousImport(b.fmt("fatbin_{s}", .{k.name}), .{ .root_source_file = image.? });
return cuda;
}
-fn family(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, cuda: *std.Build.Module, draft_ids: *std.Build.Module) struct { core: *std.Build.Module, lanes: *std.Build.Module, nemotron: *std.Build.Module, qwen35: *std.Build.Module, qwen_vision: *std.Build.Module, tokenizer: *std.Build.Module, heat: *std.Build.Module, radio: *std.Build.Module } {
+fn family(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, cuda: *std.Build.Module, draft_ids: *std.Build.Module) struct { core: *std.Build.Module, lanes: *std.Build.Module, nemotron: *std.Build.Module, qwen35: *std.Build.Module, qwen_vision: *std.Build.Module, tokenizer: *std.Build.Module, heat: *std.Build.Module, radio: *std.Build.Module, living: *std.Build.Module } {
const tokenizer = b.createModule(.{ .root_source_file = b.path("zig/src/core/tokenizer/tokenizer.zig"), .target = target, .optimize = optimize, .link_libc = true });
const core = b.createModule(.{ .root_source_file = b.path("zig/src/core/root.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ core.addImport("gpu_memory", @import("gpu_memory.zig").module(b, target));
core.addImport("tokenizer", tokenizer);
const lanes = b.createModule(.{ .root_source_file = b.path("zig/src/core/lanes/lanes.zig"), .target = target, .optimize = optimize, .link_libc = true });
const heat = b.createModule(.{ .root_source_file = b.path("zig/src/core/heat.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ // Living Weights' family-neutral learner: a family's lessons, and the server's view of them, import it
+ const living = b.createModule(.{ .root_source_file = b.path("zig/src/living/living.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ core.addImport("living", living);
const nemotron = b.createModule(.{ .root_source_file = b.path("zig/src/families/nemotron/cuda.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ nemotron.addImport("living", living);
nemotron.addImport("cuda", cuda);
nemotron.addImport("core", core);
nemotron.addImport("lanes", lanes);
@@ -37,13 +43,16 @@ fn family(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin
nemotron.addImport("nemotron_draft_ids", draft_ids);
const radio = radioModule(b, target, optimize, cuda, core, lanes);
nemotron.addImport("radio", radio);
+ nemotron.addImport("living", living);
const qwen35 = b.createModule(.{ .root_source_file = b.path("zig/src/families/qwen3_5/cuda.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ qwen35.addImport("living", living);
+ qwen35.addImport("nemotron", nemotron);
qwen35.addImport("cuda", cuda);
qwen35.addImport("core", core);
qwen35.addImport("lanes", lanes);
const qwen_vision = qwenVisionModule(b, target, optimize, cuda, core, lanes);
qwen35.addImport("qwen_vision", qwen_vision);
- return .{ .core = core, .lanes = lanes, .nemotron = nemotron, .qwen35 = qwen35, .qwen_vision = qwen_vision, .tokenizer = tokenizer, .heat = heat, .radio = radio };
+ return .{ .core = core, .lanes = lanes, .nemotron = nemotron, .qwen35 = qwen35, .qwen_vision = qwen_vision, .tokenizer = tokenizer, .heat = heat, .radio = radio, .living = living };
}
/// Linux targets: fatbins (-Dnvcc builds them, -Dfatbins embeds prebuilt ones), `tensorfold` and `tf-cuda-test`.
@@ -88,9 +97,11 @@ pub fn targets(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.bu
b.installArtifact(b.addExecutable(.{ .name = "tensorfold", .root_module = cli }));
const runner = b.createModule(.{ .root_source_file = b.path("zig/tests/cuda/main.zig"), .target = target, .optimize = optimize, .link_libc = true, .strip = strip });
runner.addImport("cuda", cuda);
+ runner.addImport("gpu_memory", @import("gpu_memory.zig").module(b, target));
runner.addImport("core", mods.core);
runner.addImport("lanes", mods.lanes);
runner.addImport("nemotron", mods.nemotron);
+ runner.addImport("qwen35", mods.qwen35);
runner.addImport("flashnext", flashnextModule(b, target, optimize, cuda, mods.core, mods.lanes, mods.qwen_vision));
runner.addImport("lfm2", lfm2Module(b, target, optimize, cuda, mods.core));
runner.addImport("radio", mods.radio);
@@ -103,18 +114,23 @@ pub fn targets(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.bu
for (exlr8_build.denseModules(b, target, optimize, exlr8, exlr8_tests[0])) |m| runner.addImport(m.name, m.module);
const glm_cuda = glmCuda(b, target, optimize, cuda, mods.core, mods.lanes, mods.heat);
runner.addImport("glm_cuda", glm_cuda);
+ runner.addImport("kolibri1", kolibri1Module(b, target, optimize, cuda, mods.core, mods.lanes));
const runner_exe = b.addExecutable(.{ .name = "tf-cuda-test", .root_module = runner });
b.installArtifact(runner_exe);
// tf-tp-watch-check: the ranks' lines with no GPU or NCCL; a failing or dying rank stops every rank
b.installArtifact(b.addExecutable(.{ .name = "tf-tp-watch-check", .root_module = b.createModule(.{ .root_source_file = b.path("zig/tests/cuda/tp_watch_check.zig"), .target = target, .optimize = optimize, .link_libc = true, .strip = strip, .imports = &.{.{ .name = "cuda", .module = cuda }} }) }));
+ @import("living_dense.zig").target(b, target, optimize, cuda, mods.qwen35);
flashnextChecks(b, target, optimize, cuda, mods.core, mods.lanes, mods.qwen_vision);
b.step("compile-cuda-tests", "Compile CUDA runner without executing a GPU").dependOn(&runner_exe.step);
const startup = b.addObject(.{ .name = "modelopt-startup", .root_module = b.createModule(.{ .root_source_file = b.path("zig/modelopt_startup_compile.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{.{ .name = "native", .module = native.engines }} }) });
_ = startup.getEmittedBin();
b.step("compile-modelopt-startup", "Compile actual native startup with test-only absent GPU images").dependOn(&startup.step);
- const controls = if (nvcc) |tool| fatbin(b, tool, version.?, .{ .name = "contract_controls", .flags = &.{"-O3"} }, sms) else null;
- @import("qwen_contract.zig").images(b, runner_exe, controls, h3_image, kernels, &images);
- const server_exe = nativeServer(b, target, optimize, cuda, mods.core, mods.lanes, mods.nemotron, mods.heat, mods.qwen35, mods.qwen_vision, mods.tokenizer, build_options, true, strip);
+ const controls_file = b.option([]const u8, "contract-controls", "Compile-only controls image");
+ const controls = if (controls_file) |file| b.graph.cwdRelativePath(file) else if (nvcc) |tool| fatbin(b, tool, version.?, .{ .name = "contract_controls", .flags = &.{"-O3"} }, sms) else null;
+ const contract_images = @import("qwen_contract.zig").images(b, runner_exe, controls, h3_image, kernels, &images);
+ contract_images.dependOn(fatbin_step);
+ @import("cuda_contract_deepseek.zig").images(b, runner_exe, contract_images, kernels, &images, prebuilt, nvcc, version, sms);
+ const server_exe = nativeServer(b, target, optimize, cuda, mods.core, mods.lanes, mods.nemotron, mods.heat, mods.qwen35, mods.qwen_vision, mods.tokenizer, mods.living, build_options, true, strip);
b.step("compile-config-server", "Compile the actual CUDA server config entry point without installing or running").dependOn(&server_exe.step);
// tf-qwen35-cuda: the Qwen CUDA forward's rows for a reference to compare, and its tower on picture files
const q35 = b.createModule(.{ .root_source_file = b.path("zig/tests/qwen35_cuda_main.zig"), .target = target, .optimize = optimize, .link_libc = true, .strip = strip });
@@ -139,6 +155,9 @@ fn glmCuda(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builti
return m;
}
+/// The Kolibri-1 CUDA family over the runtime, the core and the lane core.
+const kolibri1Module = @import("kolibri.zig").module;
+
/// LFM2 (d1's decision model) on CUDA: config, weights, kernels and the forward that scores labels.
fn lfm2Module(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, cuda: *std.Build.Module, core: *std.Build.Module) *std.Build.Module {
return b.createModule(.{ .root_source_file = b.path("zig/src/families/lfm2/lfm2.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "core", .module = core } } });
@@ -161,6 +180,7 @@ fn flashnextModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: st
m.addImport("core", core);
m.addImport("lanes", lanes);
m.addImport("qwen_vision", qwen_vision);
+ m.addImport("living", core.import_table.get("living").?);
return m;
}
@@ -183,6 +203,7 @@ fn engines(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builti
.{ .name = "flashnext", .module = flashnextModule(b, target, optimize, cuda, core, lanes, qwen_vision) },
.{ .name = "glm_cuda", .module = glmCuda(b, target, optimize, cuda, core, lanes, heat) },
.{ .name = "qwen35", .module = qwen35 },
+ .{ .name = "kolibri1", .module = kolibri1Module(b, target, optimize, cuda, core, lanes) },
},
});
return .{ .api = api, .engines = mod };
@@ -194,7 +215,7 @@ fn checkpointCli(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.
}
/// `zig build native`: tensorfold-native with the CUDA engines into zig-out/native/bin, as the Metal build makes it.
-fn nativeServer(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, cuda: *std.Build.Module, core: *std.Build.Module, lanes: *std.Build.Module, nemotron: *std.Build.Module, heat: *std.Build.Module, qwen35: *std.Build.Module, qwen_vision: *std.Build.Module, tokenizer: *std.Build.Module, build_options: *std.Build.Step.Options, install_native: bool, strip: ?bool) *std.Build.Step.Compile {
+fn nativeServer(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, cuda: *std.Build.Module, core: *std.Build.Module, lanes: *std.Build.Module, nemotron: *std.Build.Module, heat: *std.Build.Module, qwen35: *std.Build.Module, qwen_vision: *std.Build.Module, tokenizer: *std.Build.Module, living: *std.Build.Module, build_options: *std.Build.Step.Options, install_native: bool, strip: ?bool) *std.Build.Step.Compile {
const m = engines(b, target, optimize, cuda, core, lanes, nemotron, heat, qwen35, qwen_vision);
// the HTTP side keeps its safety checks; the engine below it runs at `optimize` (the tokenizer is the family's)
const template = b.createModule(.{ .root_source_file = b.path("zig/src/core/template/template.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true });
@@ -203,7 +224,7 @@ fn nativeServer(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.b
.target = target,
.optimize = .ReleaseSafe,
.link_libc = true,
- .imports = &.{ .{ .name = "engine_api", .module = m.api }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "template", .module = template }, .{ .name = "native_engines", .module = m.engines }, .{ .name = "checkpoint_cli", .module = checkpointCli(b, target, .ReleaseSafe, null, m.engines, core) } },
+ .imports = &.{ .{ .name = "engine_api", .module = m.api }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "template", .module = template }, .{ .name = "native_engines", .module = m.engines }, .{ .name = "living", .module = living }, .{ .name = "checkpoint_cli", .module = checkpointCli(b, target, .ReleaseSafe, null, m.engines, core) } },
}) });
exe.root_module.addOptions("build_options", build_options);
_ = @import("server.zig").addTo(b, exe.root_module, target, .ReleaseSafe);
@@ -218,22 +239,32 @@ fn nativeServer(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.b
return exe;
}
-/// Host tests of the CUDA runtime, the backend-neutral core, the lane core and the CUDA family (no GPU), on any host.
+/// CPU tests for the CUDA runtime, shared core, lanes and CUDA family, on any host.
pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *std.Build.Step.Options, all: *std.Build.Step, server_steps: bool) void {
const step = b.step("test-cuda-host", "The CUDA side's host unit tests alone (no GPU work)");
all.dependOn(step);
+ @import("release_version.zig").tests(b, step);
+ const flags = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/build/cuda_release_flags.zig"),
+ .target = b.graph.host,
+ }) }));
+ b.step("test-cuda-release-flags-host", "All CUDA image flags omit private source line data").dependOn(&flags.step);
+ step.dependOn(&flags.step);
+ @import("cuda_release_paths.zig").tests(b, step);
+ @import("runtime_libraries.zig").tests(b, step);
const host = b.graph.host;
step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/build/check_fatbins.zig"), .target = host }) })).step);
const cuda = runtime(b, host, .debug, &.{});
@import("h3_host.zig").targets(b, cuda, step);
+ b.step("test-gradient-host", "Gradient bounds through real wrappers with recorded CUDA launches").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = cuda, .filters = &.{"train launch bounds:"} })).step);
+ const runtime_host = b.step("test-runtime-host", "Shared CUDA runtime and graph host regressions");
+ runtime_host.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = cuda })).step);
const mods = family(b, host, .debug, cuda, draft_ids);
@import("qwen_contract.zig").host(b, mods.core, step);
- const mtp_policy = b.createModule(.{ .root_source_file = b.path("zig/nemotron_mtp_policy_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "nemotron", .module = mods.nemotron }, .{ .name = "lanes", .module = mods.lanes } } });
- const mtp_run = b.addRunArtifact(b.addTest(.{ .root_module = mtp_policy, .filters = &.{"mtp policy:"} }));
- b.step("test-nemotron-mtp-policy-host", "Nemotron controls through shared rounds, resumes and handoff without a GPU").dependOn(&mtp_run.step);
- b.step("test-nemotron-restored-head-host", "Actual Nemotron cache restore, bind and captured draft dispatch without a GPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mods.nemotron, .filters = &.{"restored head:"} })).step);
- b.step("test-nemotron-subset-head-host", "Actual subset-head ownership and row gathering without a GPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mods.nemotron, .filters = &.{"subset head:"} })).step);
- step.dependOn(&mtp_run.step);
+ @import("inc4_host.zig").round(b, cuda, mods.core);
+ @import("inc4_host.zig").rows(b, cuda, mods.core);
+ @import("living_dense.zig").host(b, cuda, mods.qwen35, draft_ids, runtime, family, kernels.len);
+ @import("cuda_family_host.zig").steps(b, cuda, mods, step);
const exl3_host = b.step("test-exl3-cuda-host", "EXL3 loader and CUDA host regressions without a GPU");
const exl3_compile = b.addObject(.{ .name = "exl3_host_compile", .root_module = b.createModule(.{
.root_source_file = b.path("zig/tests/exl3_compile.zig"),
@@ -248,6 +279,11 @@ pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *st
}
const contract_arch = b.createModule(.{ .root_source_file = b.path("zig/tests/cuda/contract_arch_test.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "core", .module = mods.core } } });
step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = contract_arch })).step);
+ b.step("test-pocket-device-host", "Actual pocket device hooks under CUDA ABI failures").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = flashnextModule(b, host, .debug, cuda, mods.core, mods.lanes, mods.qwen_vision), .filters = &.{"Learning persistence:"} })).step);
+ b.step("test-living-host", "Family-neutral learner, saved form and commit gate on host").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mods.living })).step);
+ const shard_tests = b.createModule(.{ .root_source_file = b.path("zig/src/living_shards_test.zig"), .target = host, .optimize = .debug, .link_libc = true });
+ shard_tests.addImport("living", mods.living);
+ b.step("test-living-shards-host", "Quantized save and reopen through actual shard writer").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = shard_tests })).step);
const writeback_contract = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{
.root_source_file = b.path("zig/tests/cuda/contract_flashnext_writeback.zig"),
.target = host,
@@ -311,6 +347,10 @@ pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *st
const cuda_target = b.resolveTargetQuery(.{ .cpu_arch = host.result.cpu.arch, .os_tag = .linux, .abi = .gnu });
const check_cuda = runtime(b, cuda_target, .debug, &.{});
const check_mods = family(b, cuda_target, .debug, check_cuda, draft_ids);
+ const rounds_tool = b.createModule(.{ .root_source_file = b.path("zig/tests/qwen35_cuda_main.zig"), .target = cuda_target, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = check_cuda }, .{ .name = "qwen35", .module = check_mods.qwen35 }, .{ .name = "core", .module = check_mods.core } } });
+ const rounds_compile = b.addObject(.{ .name = "qwen35-rounds-checker-compile", .root_module = b.createModule(.{ .root_source_file = b.path("zig/qwen35_rounds_compile.zig"), .target = cuda_target, .link_libc = true, .imports = &.{.{ .name = "tool", .module = rounds_tool }} }) });
+ _ = rounds_compile.getEmittedBin();
+ b.step("compile-qwen35-rounds-host", "Compile the real CUDA 64-row checker for Linux, no images or execution").dependOn(&rounds_compile.step);
const compile_bolt = b.addObject(.{ .name = "bolt_host_compile", .root_module = b.createModule(.{
.root_source_file = b.path("zig/tests/bolt_compile.zig"),
.target = cuda_target,
@@ -331,13 +371,19 @@ pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *st
const drama3 = @import("drama3_host.zig").hostTests(b, step, cuda, mods.core);
const native_modules = engines(b, host, .debug, cuda, mods.core, mods.lanes, mods.nemotron, mods.heat, mods.qwen35, mods.qwen_vision);
const native = native_modules.engines;
+ @import("release_cuda_refusal.zig").tests(b, step, native_modules.api, mods.nemotron);
+ @import("qwen_cost_open.zig").tests(b, mods, native_modules, step);
const windows_port = @import("windows_host.zig").steps(b, cuda, native_modules.api);
+ const adapter_tests = b.createModule(.{ .root_source_file = b.path("zig/src/native/slide_adapter_test.zig"), .target = host, .link_libc = true, .imports = &.{.{ .name = "engine_api", .module = native_modules.api }} });
+ b.step("test-slide-adapter-host", "Initial learner progress before its first long unit").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = adapter_tests })).step);
@import("drama3_host.zig").native(b, step, drama3, mods.core, native_modules.api, cuda, "drama_cuda_native_host");
@import("drama3_host.zig").metal(b, mods.core, native_modules.api);
const safety = b.step("test-kv-safety-host", "Actual native KV startup budget and paged constructors under allocation/completion faults");
for ([_]*std.Build.Module{ native, mods.nemotron }) |m| safety.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m, .filters = &.{"kv safety:"} })).step);
const merge_checks = b.step("test-kv-merge-host", "KV page integration with landed snapshot modes and budgets");
for ([_]*std.Build.Module{ native, mods.nemotron }) |m| merge_checks.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m, .filters = &.{"kv merge:"} })).step);
+ const nemo_safety = b.step("test-nemotron-learning-host", "Actual Nemotron CUDA close, checked pocket reads and retry under fake driver failures");
+ for ([_]*std.Build.Module{ native, mods.nemotron }) |m| nemo_safety.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m, .filters = &.{"Learner ownership:"} })).step);
const load_check = b.step("test-bolt-load-host", "Bolt load destination and native-parent retention without a GPU");
const load_filter = b.option([]const u8, "bolt-load-filter", "Select one load regression");
const load_module = b.createModule(.{ .root_source_file = b.path("zig/src/native/cuda_load_gpu.zig"), .target = host, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "nemotron", .module = mods.nemotron } } });
@@ -347,18 +393,26 @@ pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *st
load_check.dependOn(&check.step);
}
+ b.step("test-cuda-readiness-host", "Real native readiness and synchronous startup preparation without shaders").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"family readiness reaches"} })).step);
+ b.step("test-runtime-api", "Optional planner, prompt piece, proposer and halt API tests").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native_modules.api })).step);
+ b.step("test-api-boundaries", "Runtime thread initialization failure regression").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native_modules.api, .filters = &.{"Failure boundaries:"} })).step);
+ b.step("test-review-api", "Ownership rollback: ownership and final batching regressions").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native_modules.api, .filters = &.{"Ownership rollback:"} })).step);
+ const check_native = nativeServer(b, cuda_target, .debug, check_cuda, check_mods.core, check_mods.lanes, check_mods.nemotron, check_mods.heat, check_mods.qwen35, check_mods.qwen_vision, check_mods.tokenizer, check_mods.living, build_options, false, null);
+ b.step("check-native-cuda-host", "Compile the real native CUDA server without kernels or GPU execution").dependOn(&check_native.step);
+
const offer_tests = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/src/native/cuda_vision_offer.zig"), .target = host, .link_libc = true, .imports = &.{ .{ .name = "engine_api", .module = native_modules.api }, .{ .name = "nemotron", .module = mods.nemotron }, .{ .name = "qwen35", .module = mods.qwen35 } } }), .filters = if (video_filter) |f| &.{f} else &.{} }));
offer_tests.has_side_effects = true;
video_step.dependOn(&offer_tests.step);
const compile_modules = engines(b, cuda_target, .debug, check_cuda, check_mods.core, check_mods.lanes, check_mods.nemotron, check_mods.heat, check_mods.qwen35, check_mods.qwen_vision);
- const glm_open = b.addObject(.{ .name = "glm-native-open", .root_module = b.createModule(.{ .root_source_file = b.path("zig/glm_open_compile.zig"), .target = cuda_target, .optimize = .debug, .link_libc = true, .imports = &.{.{ .name = "glm", .module = glmCuda(b, cuda_target, .debug, check_cuda, check_mods.core, check_mods.lanes, check_mods.heat) }} }) });
- _ = glm_open.getEmittedBin();
- b.step("compile-glm-native-open", "Compile the actual GLM Linux open body with empty GPU images").dependOn(&glm_open.step);
+ @import("glm_family_host.zig").compile(b, cuda_target, check_cuda, check_mods, glmCuda);
const open_object = b.addObject(.{ .name = "omni-video-native-compile", .root_module = b.createModule(.{ .root_source_file = b.path("zig/tests/omni_video_compile.zig"), .target = cuda_target, .link_libc = true, .imports = &.{.{ .name = "native", .module = compile_modules.engines }} }) });
_ = open_object.getEmittedBin();
b.step("compile-omni-video-host", "Compile actual native CUDA startup and offer for Linux without images or GPU execution").dependOn(&open_object.step);
const h3_checkpoint = b.createModule(.{ .root_source_file = b.path("zig/src/families/h3/checkpoint.zig"), .target = host, .optimize = .debug, .link_libc = true });
step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = h3_checkpoint })).step);
+
+ b.step("test-save-reopen-host", "Unresolved save through actual checkpoint admission").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mods.core, .filters = &.{"Save recovery:"} })).step);
+ b.step("test-trainer-close-host", "Actual native close and real Flash Next trainer ownership sentinels").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"Save recovery:"} })).step);
// the HTTP server's unit tests (zig/src/server/root.zig), which the macOS build runs, on Linux hosts too
const template = b.createModule(.{ .root_source_file = b.path("zig/src/core/template/template.zig"), .target = host, .optimize = .debug, .link_libc = true });
const server_tests = b.createModule(.{
@@ -366,11 +420,14 @@ pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *st
.target = host,
.optimize = .debug,
.link_libc = true,
- .imports = &.{ .{ .name = "engine_api", .module = native_modules.api }, .{ .name = "tokenizer", .module = mods.tokenizer }, .{ .name = "template", .module = template } },
+ .imports = &.{ .{ .name = "engine_api", .module = native_modules.api }, .{ .name = "tokenizer", .module = mods.tokenizer }, .{ .name = "template", .module = template }, .{ .name = "living", .module = mods.living } },
});
const server_json = @import("server.zig").addTo(b, server_tests, host, .debug);
+ b.step("test-runtime-capabilities-host", "Optional library discovery in actual capabilities JSON").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"runtime library:"} })).step);
windows_port.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"hardware names"} })).step);
b.step("test-benchmark-manifest-host", "Manifest identity through the HTTP server without shaders").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"manifest"} })).step);
+ b.step("test-lesson-questions-host", "Supplied questions through the production lesson without shaders").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"Lesson questions:"} })).step);
+ b.step("test-slide-recovery-host", "Living Weights conversation and stream regressions without a GPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{ "lesson conversations:", "lesson stream:", "lesson writer:" } })).step);
b.step("test-issue-934", "Benchmark config admission and receipt regressions").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"issue 934:"} })).step);
b.step("test-issue-969", "First-EOS classification and incomplete benchmark messages without a GPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"issue 969"} })).step);
b.step("test-tool-required-host", "Required tool parsing, grammar form and constrained lane replies on the CPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"required tools:"} })).step);
@@ -378,8 +435,20 @@ pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *st
server_tests.addImport("qwen27_mask_fake", q27.fake);
server_tests.addImport("qwen27_masks", q27.masks);
b.step("test-qwen27-required-host", "Qwen27 required tool routes through the masked lane fake").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"qwen27 tool calls:"} })).step);
+ b.step("test-save-graph-host", "Actual graph receipts paired with generic learner persistence failures").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"Save recovery:"} })).step);
+ b.step("test-lesson-gate-host", "Production server rounds and earlier-fact retention").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"Learning persistence:"} })).step);
+ b.step("test-frame-cache-identity-host", "Image frame identities through the actual shared cache").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native_modules.api, .filters = &.{"Frame cache:"} })).step);
+ const media_gate = b.step("test-media-request-ownership-host", "Shared request ownership, image frame caches and media expansion on the CPU");
+ media_gate.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native_modules.api })).step);
+ media_gate.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{ "image", "video" } })).step);
+ media_gate.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mods.core, .filters = &.{ "qwen_vl", "video" } })).step);
const server_test = b.addRunArtifact(b.addTest(.{ .root_module = server_tests }));
+ b.step("test-done-times-host", "Request-local done line times and lane admission on the CPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"done times:"} })).step);
+ b.step("test-request-seeds-host", "Fresh and explicit request seeds and completion logs without shaders").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"request seed:"} })).step);
const json_test = b.addRunArtifact(b.addTest(.{ .root_module = server_json }));
+ const publication = b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"benchmark publication:"} }));
+ b.step("test-benchmark-publication-host", "Benchmark command publication boundaries with inert effects").dependOn(&publication.step);
+ b.step("test-benchmark-routing-host", "Benchmark command routing from explicit model capabilities").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests, .filters = &.{"benchmark routing:"} })).step);
const server_step = b.step(
"test-server-cpu",
"The HTTP server's unit tests (routes, templates, tool parsing) without a GPU",
@@ -399,19 +468,10 @@ pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *st
const run = b.addRunArtifact(b.addTest(.{ .root_module = mods.nemotron, .filters = &.{selected} }));
run.has_side_effects = true;
b.step("test-modelopt-host", "Actual Nemotron scratch and CUDA launch records without GPU execution").dependOn(&run.step);
- // the GLM-5.3-Flash CUDA family's host side
const glm = glmCuda(b, host, .debug, cuda, mods.core, mods.lanes, mods.heat);
- const drafter_host = b.createModule(.{ .root_source_file = b.path("zig/glm_dflash_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "glm", .module = glm }, .{ .name = "core", .module = mods.core } } });
- b.step("test-glm-dflash-host", "DFlash rank buffers, partial loader ownership and pending records on the CPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = drafter_host, .filters = &.{"glm dflash:"} })).step);
- const packed_host = b.createModule(.{ .root_source_file = b.path("zig/glm_packed_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "glm", .module = glm }, .{ .name = "core", .module = mods.core } } });
- b.step("test-glm-packed-host", "Packed GLM launch records, cap parsing and native loader on the CPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = packed_host, .filters = &.{"glm packed:"} })).step);
- const ring_host = b.createModule(.{ .root_source_file = b.path("zig/glm_ring_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "glm", .module = glm } } });
- b.step("test-glm-ring-host", "Actual GLM MTP rollback pool through recording CUDA copies").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = ring_host, .filters = &.{"glm ring:"} })).step);
- step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = glm })).step);
- const prompt_host = b.createModule(.{ .root_source_file = b.path("zig/exl3_prompt_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "glm", .module = glm }, .{ .name = "lanes", .module = mods.lanes } } });
- const prompt_run = b.addRunArtifact(b.addTest(.{ .root_module = prompt_host, .filters = &.{"exl3 prompt:"} }));
- prompt_run.has_side_effects = true;
- b.step("test-exl3-prompt-host", "Actual common prompt dispatch and scratch with recording CUDA calls").dependOn(&prompt_run.step);
+ @import("glm_family_host.zig").steps(b, cuda, glm, mods, step);
+ @import("kolibri.zig").host(b, kolibri1Module(b, host, .debug, cuda, mods.core, mods.lanes), native_modules.api, step);
+ @import("glm_contracts.zig").tests(b, glm, mods.lanes, cuda, mods.core);
// the shares a GLM-5.3-Flash rank takes (cluster/canon.zig against the Python family's)
const shares = b.createModule(.{ .root_source_file = b.path("zig/src/cluster/canon_glm_test.zig"), .target = host, .optimize = .debug });
step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = shares })).step);
@@ -419,6 +479,15 @@ pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *st
const parse_tests = b.createModule(.{ .root_source_file = b.path("zig/src/server/tool_parse_test.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "engine_api", .module = native_modules.api }, .{ .name = "tokenizer", .module = mods.tokenizer }, .{ .name = "template", .module = template } } });
_ = @import("server.zig").addTo(b, parse_tests, host, .debug);
step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = parse_tests })).step);
+ // Living Weights: the family-neutral learner, then the pocket in each family's shards over the core's shard files
+ const living_run = b.addRunArtifact(b.addTest(.{ .root_module = mods.living }));
+ const shards = b.createModule(.{ .root_source_file = b.path("zig/src/living_shards_test.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{.{ .name = "living", .module = mods.living }} });
+ const shards_run = b.addRunArtifact(b.addTest(.{ .root_module = shards }));
+ step.dependOn(&living_run.step);
+ step.dependOn(&shards_run.step);
+ const living_step = b.step("test-living", "Living Weights' learner and the pocket in each family's shards, on the host");
+ living_step.dependOn(&living_run.step);
+ living_step.dependOn(&shards_run.step);
const cli = b.createModule(.{ .root_source_file = b.path("zig/src/cli/cuda_main.zig"), .target = host, .optimize = .debug, .link_libc = true });
cli.addImport("cuda", cuda);
cli.addImport("core", mods.core);
@@ -431,26 +500,7 @@ pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *st
}
/// nvcc -fatbin with torch's flags, the kernel's own and one -gencode per SASS target, as the Python build passes them.
-fn fatbin(b: *std.Build, nvcc: []const u8, version: std.Build.LazyPath, k: Kernel, sms: []const u8) std.Build.LazyPath {
- const run = b.addSystemCommand(&.{ nvcc, "-fatbin" });
- run.addFileInput(version);
- run.addArgs(&torch_flags);
- // with cl.exe as host compiler, CUDA 13's headers need MSVC's conforming preprocessor, and M_SQRT2 its math define
- if (b.graph.host.result.os.tag == .windows) run.addArgs(&.{ "-Xcompiler=/Zc:preprocessor", "-D_USE_MATH_DEFINES" });
- run.addArgs(k.flags);
- var it = std.mem.tokenizeScalar(u8, sms, ',');
- while (it.next()) |sm| {
- // arch-specific features (the block-scaled FP4 mma) from sm_120 on; older targets build the trapping fallback
- const a = if (k.arch_specific and (std.fmt.parseInt(u32, sm, 10) catch 0) >= 120) "a" else "";
- run.addArg(b.fmt("-gencode=arch=compute_{s}{s},code=sm_{s}{s}", .{ sm, a, sm, a }));
- }
- run.addArgs(&.{ "-MD", "-MF" });
- _ = run.addDepFileOutputArg2(b.fmt("{s}.d", .{k.name}), .{});
- run.addArg("-o");
- const out = run.addOutputFileArg(b.fmt("{s}.fatbin", .{k.name}));
- run.addFileArg(b.path(b.fmt("zig/kernels/cuda/{s}.cu", .{k.src orelse k.name})));
- return out;
-}
+pub const fatbin = @import("cuda_fatbin.zig").fatbin;
/// Cross-build a release server, embedding the same fatbins on either host CPU.
pub fn distServer(b: *std.Build, target: std.Build.ResolvedTarget, draft_ids: *std.Build.Module, build_options: *std.Build.Step.Options, prebuilt: ?[]const u8) *std.Build.Step.Compile {
@@ -460,19 +510,21 @@ pub fn distServer(b: *std.Build, target: std.Build.ResolvedTarget, draft_ids: *s
};
const cuda = runtime(b, target, .fast, if (prebuilt != null) &images else &.{});
const mods = family(b, target, .fast, cuda, draft_ids);
- return nativeServer(b, target, .fast, cuda, mods.core, mods.lanes, mods.nemotron, mods.heat, mods.qwen35, mods.qwen_vision, mods.tokenizer, build_options, false, null);
+ return nativeServer(b, target, .fast, cuda, mods.core, mods.lanes, mods.nemotron, mods.heat, mods.qwen35, mods.qwen_vision, mods.tokenizer, mods.living, build_options, false, null);
}
/// SASS targets every release fatbin holds: Ampere (sm_80, sm_86), RTX 50/PRO Blackwell (sm_120), GB10 (sm_121).
pub const release_sms = "80,86,120,121";
/// Validate the complete named input set, including images unused by today's server, and each file's release targets.
-pub fn checkDistFatbins(b: *std.Build, dir: []const u8) *std.Build.Step {
+pub fn checkDistFatbins(b: *std.Build, dir: []const u8, privacy_tool: *std.Build.Step.Compile) *std.Build.Step {
const tool = b.addExecutable(.{ .name = "check-fatbins", .root_module = b.createModule(.{ .root_source_file = b.path("zig/build/check_fatbins.zig"), .target = b.graph.host }) });
const check = b.addRunArtifact(tool);
check.addArg(release_sms);
for (kernels) |k| check.addFileArg(b.graph.cwdRelativePath(b.pathJoin(&.{ dir, b.fmt("{s}.fatbin", .{k.name}) })));
- return &check.step;
+ const privacy = @import("cuda_release_paths.zig").directory(b, privacy_tool, b.graph.cwdRelativePath(dir));
+ privacy.dependOn(&check.step);
+ return privacy;
}
/// Explicit model diagnostics; these tools do not qualify a checkpoint merely by compiling.
@@ -511,4 +563,12 @@ fn flashnextChecks(b: *std.Build, target: std.Build.ResolvedTarget, optimize: st
const select_run = b.addRunArtifact(b.addExecutable(.{ .name = "flashnext-select", .root_module = select_check }));
if (b.option([]const u8, "flashnext_fixtures", "capture_sparse.sh's selection fixtures folder")) |dir| select_run.addArgs(&.{dir});
b.step("flashnext-select", "fn_qsa_select against Triton's _select / _select_tiles lists on captured score rows").dependOn(&select_run.step);
+ const learn_check = b.createModule(.{ .root_source_file = b.path("zig/src/families/flashnext/cuda_learn_main.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ learn_check.addImport("cuda", cuda);
+ learn_check.addImport("flashnext", flashnext);
+ const learn_exe = b.addExecutable(.{ .name = "flashnext-learn", .root_module = learn_check });
+ const learn_run = b.addRunArtifact(learn_exe);
+ if (embed_model != null and prompt_ids != null) learn_run.addArgs(&.{ embed_model.?, prompt_ids.? });
+ b.step("flashnext-learn", "Trainer forward identity and all-path finite-difference gates").dependOn(&learn_run.step);
+ b.step("compile-flashnext-learn", "Compile the real trainer runner without executing it").dependOn(&learn_exe.step);
}
diff --git a/zig/build/cuda_contract_deepseek.zig b/zig/build/cuda_contract_deepseek.zig
new file mode 100644
index 0000000000..ef8b4d56d3
--- /dev/null
+++ b/zig/build/cuda_contract_deepseek.zig
@@ -0,0 +1,29 @@
+//! DeepSeek contract images use the same built artifacts as their named installs.
+const std = @import("std");
+const list = @import("dsv41_units.zig");
+pub fn images(b: *std.Build, runner: *std.Build.Step.Compile, install: *std.Build.Step, kernels: anytype, files: []const ?std.Build.LazyPath, prebuilt: ?[]const u8, nvcc: ?[]const u8, version: ?std.Build.LazyPath, sms: []const u8) void {
+ const empty = b.addWriteFiles().add("empty-contract-deepseek.fatbin", "");
+ for (list.units ++ list.optional) |k| {
+ const common = for (kernels, 0..) |known, i| {
+ if (std.mem.eql(u8, known.name, k.name)) break i;
+ } else null;
+ const file = if (common) |i| files[i] else if (prebuilt) |dir|
+ b.graph.cwdRelativePath(b.pathJoin(&.{ dir, b.fmt("{s}.fatbin", .{k.name}) }))
+ else if (nvcc) |tool|
+ @import("cuda.zig").fatbin(b, tool, version.?, k, sms)
+ else
+ null;
+ runner.root_module.addAnonymousImport(b.fmt("contract_fatbin_{s}", .{k.name}), .{ .root_source_file = file orelse empty });
+ if (common != null) continue;
+ if (file) |path| {
+ install.dependOn(&b.addInstallFile(path, b.fmt("fatbin/{s}.fatbin", .{k.name})).step);
+ if (std.mem.eql(u8, k.name, "dsv41_x3ld")) @import("contract_symbols.zig").check(b, path, sms, install);
+ }
+ }
+}
+pub fn inert(b: *std.Build, m: *std.Build.Module) void {
+ const files = b.addWriteFiles();
+ for (list.units ++ list.optional) |k| m.addAnonymousImport(b.fmt("contract_fatbin_{s}", .{k.name}), .{
+ .root_source_file = files.add(b.fmt("{s}.fatbin", .{k.name}), k.name),
+ });
+}
diff --git a/zig/build/cuda_family_host.zig b/zig/build/cuda_family_host.zig
new file mode 100644
index 0000000000..344d17e428
--- /dev/null
+++ b/zig/build/cuda_family_host.zig
@@ -0,0 +1,51 @@
+//! Recording controls for served family rounds, copies, graph shapes and startup warmups.
+const std = @import("std");
+
+pub fn steps(b: *std.Build, cuda: *std.Build.Module, mods: anytype, step: *std.Build.Step) void {
+ const host = b.graph.host;
+ const prompt_moe = b.addRunArtifact(b.addTest(.{ .root_module = mods.qwen35, .filters = &.{"prompt MoE precision:"} }));
+ b.step("test-qwen35-prompt-moe-host", "Prompt expert arithmetic stays separate from verify capacity without devices").dependOn(&prompt_moe.step);
+ step.dependOn(&prompt_moe.step);
+ const startup = b.createModule(.{ .root_source_file = b.path("zig/cuda_startup_costs_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{.{ .name = "lanes", .module = mods.lanes }} });
+ b.step("test-cuda-startup-costs-host", "Eager measured CUDA costs and transactional failure controls without devices").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = startup })).step);
+
+ const mtp_policy = b.createModule(.{ .root_source_file = b.path("zig/nemotron_mtp_policy_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "nemotron", .module = mods.nemotron }, .{ .name = "lanes", .module = mods.lanes } } });
+ const mtp_run = b.addRunArtifact(b.addTest(.{ .root_module = mtp_policy, .filters = &.{"mtp policy:"} }));
+ b.step("test-nemotron-mtp-policy-host", "Nemotron controls through shared rounds, resumes and handoff without a GPU").dependOn(&mtp_run.step);
+ b.step("test-nemotron-restored-head-host", "Actual Nemotron cache restore, bind and captured draft dispatch without a GPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mods.nemotron, .filters = &.{"restored head:"} })).step);
+ b.step("test-nemotron-subset-head-host", "Actual subset-head ownership and row gathering without a GPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mods.nemotron, .filters = &.{"subset head:"} })).step);
+ step.dependOn(&mtp_run.step);
+ const decode_host = b.createModule(.{ .root_source_file = b.path("zig/qwen35_decode_host.zig"), .target = host, .optimize = .debug, .imports = &.{.{ .name = "core", .module = mods.core }} });
+ b.step("test-qwen35-decode-records-host", "Optimized Qwen decode launch records on the CPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = decode_host })).step);
+ b.step("test-shared-lane-regressions-host", "Every shared lane regression without device libraries").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mods.lanes })).step);
+ b.step("test-qwen35-draft-trees-host", "Grown trees through the actual shared draft frame and wrapper").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/qwen35_draft_frame_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "qwen35", .module = mods.qwen35 }, .{ .name = "core", .module = mods.core }, .{ .name = "lanes", .module = mods.lanes } } }), .filters = &.{"Qwen CUDA contracts:"} })).step);
+ const copies = b.createModule(.{ .root_source_file = b.path("zig/qwen35_copies_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "lanes", .module = mods.lanes }, .{ .name = "qwen35", .module = mods.qwen35 } } });
+ const copies_run = b.addRunArtifact(b.addTest(.{ .root_module = copies, .filters = &.{"Suffix copies:"} }));
+ copies_run.has_side_effects = true;
+ b.step("test-qwen35-copies-host", "CUDA family copy facts and shared lane equality, no GPU or shaders").dependOn(&copies_run.step);
+ const rounds = b.createModule(.{ .root_source_file = b.path("zig/qwen35_rounds_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "qwen35", .module = mods.qwen35 }, .{ .name = "core", .module = mods.core }, .{ .name = "lanes", .module = mods.lanes } } });
+ const rounds_run = b.addRunArtifact(b.addTest(.{ .root_module = rounds, .filters = &.{"Verify width calibration:"} }));
+ rounds_run.has_side_effects = true;
+ b.step("test-qwen35-rounds-host", "CUDA 64-row metadata and shared register-slot plans without devices").dependOn(&rounds_run.step);
+ const shared = b.createModule(.{ .root_source_file = b.path("zig/qwen35_shared_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "qwen35", .module = mods.qwen35 }, .{ .name = "core", .module = mods.core }, .{ .name = "lanes", .module = mods.lanes } } });
+ const shared_run = b.addRunArtifact(b.addTest(.{ .root_module = shared, .filters = &.{"Shared verification:"} }));
+ shared_run.has_side_effects = true;
+ b.step("test-qwen35-shared-host", "Shared CUDA slots and rollback without device execution").dependOn(&shared_run.step);
+ const draft_frame = b.createModule(.{ .root_source_file = b.path("zig/qwen35_draft_frame_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "qwen35", .module = mods.qwen35 }, .{ .name = "core", .module = mods.core }, .{ .name = "lanes", .module = mods.lanes } } });
+ b.step("test-qwen35-frame-invalidation-host", "A failed shared frame invalidates held tokens and grown trees").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = draft_frame, .filters = &.{"Draft frames: the real hold adapter"} })).step);
+ const frame_run = b.addRunArtifact(b.addTest(.{ .root_module = draft_frame, .filters = &.{"Draft frames:"} }));
+ frame_run.has_side_effects = true;
+ b.step("test-qwen35-draft-frame-host", "Batched CUDA drafter frame bookkeeping and lane equality without devices").dependOn(&frame_run.step);
+ const cache = b.createModule(.{ .root_source_file = b.path("zig/qwen35_cost_cache_host.zig"), .target = host, .link_libc = true, .imports = &.{ .{ .name = "qwen35", .module = mods.qwen35 }, .{ .name = "lanes", .module = mods.lanes } } });
+ const cost_run1 = b.addRunArtifact(b.addTest(.{ .root_module = cache, .filters = &.{"window cost cache:"} }));
+ b.step("test-qwen35-cost-cache-host", "Real width-loop cache misses and hits without a GPU").dependOn(&cost_run1.step);
+ step.dependOn(&cost_run1.step);
+ const verify_contract = b.createModule(.{ .root_source_file = b.path("zig/tests/cuda/contract_verify_host.zig"), .target = host, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "core", .module = mods.core } } });
+ b.step("test-qwen35-verify-contract-host", "Wider affine verify contracts, FP64 and exact gate controls without devices").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = verify_contract })).step);
+ const verify = b.createModule(.{ .root_source_file = b.path("zig/qwen35_verify_host.zig"), .target = host, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "qwen35", .module = mods.qwen35 } } });
+ b.step("test-qwen35-verify-host", "Actual verify matmul launches, row buckets and weight-pass counts without devices").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = verify })).step);
+ const live = b.createModule(.{ .root_source_file = b.path("zig/qwen35_live_host.zig"), .target = host, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "qwen35", .module = mods.qwen35 } } });
+ const live_run = b.addRunArtifact(b.addTest(.{ .root_module = live }));
+ live_run.has_side_effects = true;
+ b.step("test-qwen35-live-host", "Live CUDA launch buckets and startup warmups without devices").dependOn(&live_run.step);
+}
diff --git a/zig/build/cuda_fatbin.zig b/zig/build/cuda_fatbin.zig
new file mode 100644
index 0000000000..86df1e66af
--- /dev/null
+++ b/zig/build/cuda_fatbin.zig
@@ -0,0 +1,48 @@
+//! Emit only the SASS targets supported by this unit; no eligible targets produces an inert empty image.
+const std = @import("std");
+const catalog = @import("cuda_kernels.zig");
+const Kernel = catalog.Kernel;
+const torch_flags = catalog.torch_flags;
+
+const Target = struct {
+ number: u32,
+ suffix: []const u8,
+
+ fn parse(raw: []const u8) Target {
+ var digits: usize = 0;
+ while (digits < raw.len and std.ascii.isDigit(raw[digits])) : (digits += 1) {}
+ return .{
+ .number = std.fmt.parseInt(u32, raw[0..digits], 10) catch @panic("Invalid CUDA SM target"),
+ .suffix = raw[digits..],
+ };
+ }
+};
+
+pub fn fatbin(b: *std.Build, nvcc: []const u8, version: std.Build.LazyPath, k: Kernel, sms: []const u8) std.Build.LazyPath {
+ var choices = std.mem.tokenizeScalar(u8, sms, ',');
+ var any = false;
+ while (choices.next()) |sm| {
+ any = any or Target.parse(sm).number >= k.min_sm;
+ }
+ if (!any) return b.addWriteFiles().add(b.fmt("unsupported-{s}.fatbin", .{k.name}), "");
+ const run = b.addSystemCommand(&.{ nvcc, "-fatbin" });
+ run.addFileInput(version);
+ run.addArgs(&torch_flags);
+ // CUDA 13's headers with cl.exe need MSVC's conforming preprocessor and M_SQRT2's math define.
+ if (b.graph.host.result.os.tag == .windows) run.addArgs(&.{ "-Xcompiler=/Zc:preprocessor", "-D_USE_MATH_DEFINES" });
+ run.addArgs(k.flags);
+ var it = std.mem.tokenizeScalar(u8, sms, ',');
+ while (it.next()) |sm| {
+ const target = Target.parse(sm);
+ if (target.number < k.min_sm) continue;
+ // arch-specific features (the block-scaled FP4 mma) from sm_120 on; older targets build the trapping fallback
+ const a = if (k.arch_specific and target.number >= 120 and target.suffix.len == 0) "a" else "";
+ run.addArg(b.fmt("-gencode=arch=compute_{s}{s},code=sm_{s}{s}", .{ sm, a, sm, a }));
+ }
+ run.addArgs(&.{ "-MD", "-MF" });
+ _ = run.addDepFileOutputArg2(b.fmt("{s}.d", .{k.name}), .{});
+ run.addArg("-o");
+ const out = run.addOutputFileArg(b.fmt("{s}.fatbin", .{k.name}));
+ run.addFileArg(b.path(b.fmt("zig/kernels/cuda/{s}.cu", .{k.src orelse k.name})));
+ return out;
+}
diff --git a/zig/build/cuda_kernels.zig b/zig/build/cuda_kernels.zig
index 1568d5c453..25315ca223 100644
--- a/zig/build/cuda_kernels.zig
+++ b/zig/build/cuda_kernels.zig
@@ -1,7 +1,13 @@
//! CUDA image names and qualification flags.
/// Each .cu in zig/kernels/cuda (`src`, else `name`) with the flags its Python extension passes in `extra_cuda_cflags`.
-pub const Kernel = struct { name: []const u8, flags: []const []const u8, src: ?[]const u8 = null, arch_specific: bool = false };
+pub const Kernel = struct {
+ name: []const u8,
+ flags: []const []const u8,
+ src: ?[]const u8 = null,
+ arch_specific: bool = false,
+ min_sm: u32 = 0,
+};
/// The torch-op replacements' qualification flags (runs/006): no contraction, no flush to zero.
const torch_ops = &[_][]const u8{ "-O3", "--fmad=false", "--ftz=false" };
@@ -10,6 +16,12 @@ const torch_ops = &[_][]const u8{ "-O3", "--fmad=false", "--ftz=false" };
const glue = &[_][]const u8{ "-O3", "--fmad=false", "--ftz=false", "--prec-div=true", "--prec-sqrt=true" };
pub const kernels = [_]Kernel{
+ .{ .name = "attn_train", .flags = &.{"-O3"} },
+ .{ .name = "qwen27_train", .flags = &.{"-O3"} },
+ .{ .name = "gdn_train", .flags = &.{ "-O3", "--fmad=false" } },
+ .{ .name = "train_q4", .flags = &.{"-O3"} },
+ .{ .name = "train_attn", .flags = &.{"-O3"} },
+ .{ .name = "flashnext_train", .flags = &.{"-O3"} },
.{ .name = "flashnext_gdn", .flags = &.{ "-O3", "--fmad=false" } }, // qwen4_exp/cuda/gdn.cu chain, one row
.{ .name = "flashnext_batch", .flags = &.{ "-O3", "--fmad=false" } }, // ours: Flash Next's batched prompt pass glue
.{ .name = "flashnext_mtp", .flags = &.{"-O3"} }, // ours: Flash Next's MTP drafts, a row's best score and log-sum-exp
diff --git a/zig/build/cuda_release_flags.zig b/zig/build/cuda_release_flags.zig
new file mode 100644
index 0000000000..36a0ec99af
--- /dev/null
+++ b/zig/build/cuda_release_flags.zig
@@ -0,0 +1,15 @@
+//! Release CUDA images carry no source line information.
+const std = @import("std");
+const catalog = @import("cuda_kernels.zig");
+const deepseek = @import("dsv41_units.zig");
+
+test "release CUDA flags omit source line information in every image" {
+ for (catalog.kernels ++ deepseek.units ++ deepseek.optional) |kernel| {
+ for (kernel.flags) |flag| {
+ if (std.mem.eql(u8, flag, "-lineinfo") or std.mem.eql(u8, flag, "--generate-line-info")) {
+ std.debug.print("source line information in release image: {s}\n", .{kernel.name});
+ return error.ReleaseImageContainsLineInfo;
+ }
+ }
+ }
+}
diff --git a/zig/build/cuda_release_paths.zig b/zig/build/cuda_release_paths.zig
new file mode 100644
index 0000000000..79e63614ff
--- /dev/null
+++ b/zig/build/cuda_release_paths.zig
@@ -0,0 +1,76 @@
+//! Host-only release privacy tools, shared by fatbin validation and archive staging.
+const std = @import("std");
+const contract = @import("kernel_contract.zig");
+pub fn checker(b: *std.Build) *std.Build.Step.Compile {
+ return b.addExecutable(.{ .name = "check-release-paths", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/build/check_release_paths.zig"),
+ .target = b.graph.host,
+ .optimize = .fast,
+ .strip = true,
+ .imports = &.{.{ .name = "contract", .module = contract.module(b, b.graph.host, .fast) }},
+ }) });
+}
+pub fn tests(b: *std.Build, all: *std.Build.Step) void {
+ const run = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/build/release_paths_test.zig"),
+ .target = b.graph.host,
+ .imports = &.{.{ .name = "contract", .module = contract.module(b, b.graph.host, .debug) }},
+ }), .filters = &.{"release paths:"} }));
+ b.step("test-release-paths-host", "Planted path leaks in raw, wide and compressed release payloads").dependOn(&run.step);
+ all.dependOn(&run.step);
+ const tool = checker(b);
+ b.step("compile-release-paths-host", "Build the host release checker without shader compilers").dependOn(&b.addInstallArtifact(tool, .{}).step);
+ const clean = b.addWriteFiles();
+ const clean_file = clean.add("noise.bin", "\x01./.\x02");
+ const bad = b.addWriteFiles();
+ const root = absoluteRoot(b);
+ _ = bad.add("leak.bin", b.fmt("prefix:{s}/assets/data.bin", .{root}));
+ const directory_test = b.step("test-release-directory-host", "Actual release directory invocation accepts code fragments and refuses its absolute build root");
+ directory_test.dependOn(directory(b, tool, clean.getDirectory()));
+ const refuses = directory(b, tool, bad.getDirectory());
+ refuses.cast(std.Build.Step.Run).?.expectExitCode(1);
+ directory_test.dependOn(refuses);
+ const command_test = b.step("test-release-paths-command-host", "Compiled privacy command refuses invalid roots before reading inputs");
+ for ([_][]const u8{ "./.", "/", "//", "/.", "/../", "C:\\", "C:/", "C:relative", "relative/build", "\\relative", "C:\\..\\.", "\\\\server" }) |invalid| {
+ const check = b.addRunArtifact(tool);
+ check.addArg(invalid);
+ check.addFileArg(clean_file);
+ check.expectExitCode(2);
+ check.expectStdErrEqual("release privacy: BadBuildRoot: use a named absolute build root\n");
+ command_test.dependOn(&check.step);
+ }
+ const unreadable = b.addRunArtifact(tool);
+ unreadable.addArgs(&.{ "./.", "." });
+ unreadable.expectExitCode(2);
+ unreadable.expectStdErrEqual("release privacy: BadBuildRoot: use a named absolute build root\n");
+ command_test.dependOn(&unreadable.step);
+ const valid = b.addRunArtifact(tool);
+ valid.addArg(root);
+ valid.addFileArg(clean_file);
+ valid.expectExitCode(0);
+ valid.expectStdErrEqual("");
+ command_test.dependOn(&valid.step);
+ for ([_][]const u8{ "C:\\release-root", "\\\\server\\share\\build" }) |absolute| {
+ const check = b.addRunArtifact(tool);
+ check.addArg(absolute);
+ check.addFileArg(clean_file);
+ check.expectExitCode(0);
+ check.expectStdErrEqual("");
+ command_test.dependOn(&check.step);
+ }
+}
+pub fn directory(b: *std.Build, tool: *std.Build.Step.Compile, dir: std.Build.LazyPath) *std.Build.Step {
+ const run = b.addSystemCommand(&.{"sh"});
+ run.addFileArg(b.path("tools/release/check_paths.sh"));
+ run.addArtifactArg(tool);
+ run.addArg(absoluteRoot(b));
+ run.addDirectoryArg(dir);
+ return &run.step;
+}
+
+pub fn absoluteRoot(b: *std.Build) []const u8 {
+ const cwd = std.process.currentPathAlloc(b.graph.io, b.allocator) catch @panic("cannot read release build directory");
+ const root = std.fs.path.resolveAlloc(b.allocator, &.{ cwd, b.root.root_dir.path orelse ".", b.root.sub_path }) catch @panic("OOM");
+ std.debug.assert(std.fs.path.isAbsolute(root));
+ return root;
+}
diff --git a/zig/build/deepseek_v41.zig b/zig/build/deepseek_v41.zig
new file mode 100644
index 0000000000..ba98e3199f
--- /dev/null
+++ b/zig/build/deepseek_v41.zig
@@ -0,0 +1,80 @@
+//! Experimental DS4.1 weight-loading qualification targets.
+const std = @import("std");
+
+pub fn weights(
+ b: *std.Build,
+ target: std.Build.ResolvedTarget,
+ optimize: std.builtin.OptimizeMode,
+ metal: *std.Build.Module,
+) *std.Build.Step {
+ const host = b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/deepseek_v41_weights_checks.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{
+ .{ .name = "metal", .module = metal },
+ },
+ }) });
+ const step = b.step("test-deepseek-v41-weights", "V4.1 configuration, Engram table plans and resident weight staging");
+ step.dependOn(&b.addRunArtifact(host).step);
+ return step;
+}
+
+pub fn checks(
+ b: *std.Build,
+ target: std.Build.ResolvedTarget,
+ optimize: std.builtin.OptimizeMode,
+ metal: *std.Build.Module,
+ sources: *std.Build.Module,
+ engine: *std.Build.Module,
+) struct { device: *std.Build.Step, reference: *std.Build.Step } {
+ const device = b.addTest(.{ .name = "tf-dsv41-gpu", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/deepseek_v41_checks.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{
+ .{ .name = "metal", .module = metal },
+ .{ .name = "kernel_sources", .module = sources },
+ },
+ }) });
+ const tp_host = b.addTest(.{
+ .root_module = engine,
+ .filters = &.{ "TP residency", "TP history", "TP output row", "TP exchanges", "TP control", "TP TCP" },
+ });
+ const tp_step = b.step("test-deepseek-v41-tp-host", "Two-rank row layout, history and message validation");
+ tp_step.dependOn(&b.addRunArtifact(tp_host).step);
+ const tcp_host = b.addTest(.{ .root_module = engine.import_table.get("fabric").?, .filters = &.{"TP TCP"} });
+ tp_step.dependOn(&b.addRunArtifact(tcp_host).step);
+ const tcp_probe = b.addExecutable(.{ .name = "tf-tcp-probe", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/tcp_probe.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{.{ .name = "fabric", .module = engine.import_table.get("fabric").? }},
+ }) });
+ b.step("tf-tcp-probe", "Build the CPU-only two-rank TCP qualification tool").dependOn(&b.addInstallArtifact(tcp_probe, .{}).step);
+ const device_step = b.step("test-deepseek-v41", "Experimental DS4.1 EXL3, Engram, mHC and sparse attention on Metal");
+ device_step.dependOn(&b.addRunArtifact(device).step);
+ const reference = b.addTest(.{
+ .name = "tf-dsv41-reference",
+ .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/deepseek_v41_reference_checks.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{
+ .{ .name = "tensorfold", .module = engine },
+ .{ .name = "metal", .module = metal },
+ },
+ }),
+ .filters = &.{"tests.deepseek_v41_reference."},
+ });
+ const reference_step = b.step("test-deepseek-v41-reference", "Independent CPU teacher-oracle arithmetic boundaries");
+ reference_step.dependOn(&b.addRunArtifact(reference).step);
+ const staged = b.step("check-deepseek-v41", "Build and install V4.1 qualification binaries without running the GPU");
+ staged.dependOn(&b.addInstallArtifact(device, .{}).step);
+ staged.dependOn(&b.addInstallArtifact(reference, .{}).step);
+ return .{ .device = device_step, .reference = reference_step };
+}
diff --git a/zig/build/dist.zig b/zig/build/dist.zig
index 172c8be9f1..64317090de 100644
--- a/zig/build/dist.zig
+++ b/zig/build/dist.zig
@@ -17,8 +17,10 @@ pub fn validVersion(v: []const u8) bool {
}
pub fn targets(b: *std.Build, ids: *std.Build.Module, options: *std.Build.Step.Options, version: ?[]const u8, xgrammar: ?[]const u8) void {
+ const privacy = @import("cuda_release_paths.zig").checker(b);
const package_tests = b.addSystemCommand(&.{"python3"});
package_tests.addFileArg(b.path("tools/release/test_package.py"));
+ package_tests.addPrefixedFileArg("--path-checker=", privacy.getEmittedBin());
package_tests.has_side_effects = true;
b.step("test-dist-package", "CPU regression for repeated same-version archives and bundled notices").dependOn(&package_tests.step);
const step = b.step("dist", "Package macOS arm64 (M1 CPU) and Linux x86_64/aarch64 CUDA servers");
@@ -48,7 +50,7 @@ pub fn targets(b: *std.Build, ids: *std.Build.Module, options: *std.Build.Step.O
step.dependOn(&b.addFail("dist requires -Dxgrammar=DIR (structured output's libtfgrammar): " ++ grammar.clone).step);
return;
}
- const fatbin_check = if (fatbins) |dir| cuda.checkDistFatbins(b, dir) else null;
+ const fatbin_check = if (fatbins) |dir| cuda.checkDistFatbins(b, dir, privacy) else null;
const sdk = sdk_override orelse if (b.graph.host.result.os.tag == .macos)
std.mem.trim(u8, b.run(&.{ "xcrun", "--sdk", "macosx", "--show-sdk-path" }), "\r\n")
else {
@@ -83,6 +85,8 @@ pub fn targets(b: *std.Build, ids: *std.Build.Module, options: *std.Build.Step.O
pack.addDirectoryArg(b.path("LICENSES"));
pack.addFileArg(b.path("THIRD_PARTY_NOTICES.md"));
const archives = pack.addOutputDirectoryArg("archives");
+ pack.addPrefixedFileArg("--path-checker=", privacy.getEmittedBin());
+ pack.addArg(b.fmt("--build-root={s}", .{@import("cuda_release_paths.zig").absoluteRoot(b)}));
if (aot != null and p.query.os_tag == .linux and !host_only) pack.addDirectoryArg(b.graph.cwdRelativePath(aot.?));
if (xgrammar) |dir| pack.addPrefixedDirectoryArg("--overlay=", grammar.overlay(b, target, dir) orelse {
step.dependOn(&b.addFail(b.fmt("-Dxgrammar={s} is not an xgrammar " ++ grammar.pinned ++ " checkout: " ++ grammar.clone, .{dir})).step);
diff --git a/zig/build/dsv41_cuda.zig b/zig/build/dsv41_cuda.zig
new file mode 100644
index 0000000000..7967ba0ef1
--- /dev/null
+++ b/zig/build/dsv41_cuda.zig
@@ -0,0 +1,92 @@
+const std = @import("std");
+const shared = @import("dsv41_shared.zig");
+const units = @import("dsv41_units.zig");
+pub fn runtime(b: *std.Build, target: std.Build.ResolvedTarget, images: bool) *std.Build.Module {
+ const options = b.addOptions();
+ options.addOption(bool, "with_kernels", images);
+ const cuda = b.createModule(.{ .root_source_file = b.path("zig/src/cuda/root.zig"), .target = target, .optimize = .Debug, .link_libc = true });
+ cuda.addImport("gpu_memory", @import("gpu_memory.zig").module(b, target));
+ cuda.addOptions("kernel_options", options);
+ cuda.addImport("stagger", b.createModule(.{ .root_source_file = b.path("zig/src/core/stagger.zig"), .target = target, .optimize = .Debug }));
+ if (images) {
+ const empty = b.addWriteFiles().add("compile-only.fatbin", "SYNTHETIC-COMPILE-ONLY");
+ const all = @import("cuda.zig").kernels;
+ for (all) |k| cuda.addAnonymousImport(b.fmt("fatbin_{s}", .{k.name}), .{ .root_source_file = empty });
+ }
+ return cuda;
+}
+pub fn module(b: *std.Build, target: std.Build.ResolvedTarget, file: []const u8, images: bool) *std.Build.Module {
+ const cuda = runtime(b, target, images);
+ const opts = b.addOptions();
+ opts.addOption(bool, "with_kernels", images);
+ opts.addOption(u32, "family_scope", 41);
+ const dk = b.createModule(.{ .root_source_file = b.path("zig/src/families/deepseek_v41/kernels.zig"), .target = target, .optimize = .Debug, .link_libc = true });
+ dk.addImport("cuda", cuda);
+ dk.addOptions("dsv41_kernel_options", opts);
+ const empty = b.addWriteFiles().add("compile-only-dsv41.fatbin", "SYNTHETIC-COMPILE-ONLY");
+ for (units.units ++ units.optional) |k| dk.addAnonymousImport(b.fmt("dsv41_fatbin_{s}", .{k.name}), .{ .root_source_file = empty });
+ const m = shared.module(b, target, file);
+ m.addImport("cuda", cuda);
+ m.addImport("dsv41_kernels", dk);
+ const tp = @import("tp.zig").module(b, target, .Debug, cuda);
+ m.addImport("tp", tp);
+ m.addImport("kv", @import("dsv41_state.zig").kvModule(b, target, cuda, tp));
+ @import("dsv41_draft.zig").addImports(b, m, target);
+ return m;
+}
+pub fn steps(b: *std.Build) void {
+ _ = @import("dsv41_images.zig").images(b, b.option([]const u8, "dsv41-nvcc", "Optional family nvcc compiler"), b.option([]const u8, "dsv41-fatbins", "Optional complete family prebuilt image directory"));
+ _ = @import("dsv41_images.zig").module(b, b.graph.host, .Debug, runtime(b, b.graph.host, false), null);
+ const planner = module(b, b.graph.host, "cuda_test.zig", false);
+ b.step("test-dsv41-bounds-host", "DeepSeek resource bounds and real wrapper controls without devices").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = planner, .filters = &.{"DeepSeek bounds:"} })).step);
+ const binding_tests = b.addRunArtifact(b.addTest(.{ .root_module = planner.import_table.get("dsv41_kernels").? }));
+ const common_tests = b.addRunArtifact(b.addTest(.{ .root_module = planner.import_table.get("cuda").? }));
+ const host = b.addRunArtifact(b.addTest(.{ .root_module = module(b, b.graph.host, "cuda_test.zig", false) }));
+ const host_step = b.step("test-dsv41-cuda-host", "DeepSeek launch plans and binding contracts without device calls");
+ host_step.dependOn(&host.step);
+ host_step.dependOn(&binding_tests.step);
+ host_step.dependOn(&common_tests.step);
+ const sm_module = module(b, b.graph.host, "cuda_test.zig", true).import_table.get("dsv41_kernels").?;
+ const sm_test = b.addTest(.{ .root_module = sm_module, .filters = &.{"DeepSeek refuses pre-cluster GPUs"} });
+ const sm_run = b.addRunArtifact(sm_test);
+ b.step("test-dsv41-sm-host", "Check SM refusal before image load with a fake driver and inert images").dependOn(&sm_run.step);
+ const forms = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/src/core/exl3_prepare.zig"), .target = b.graph.host, .optimize = .Debug }) }));
+ host_step.dependOn(&forms.step);
+ const target = b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu });
+ const cross = b.addTest(.{ .root_module = module(b, target, "cuda_test.zig", true) });
+ _ = cross.getEmittedBin();
+ b.step("compile-dsv41-cuda", "Emit Linux CUDA binding bodies with synthetic nonexecuted images").dependOn(&cross.step);
+ const consumers_base = module(b, target, "cuda_test.zig", true);
+ const consumers = b.createModule(.{
+ .root_source_file = b.path("zig/tests/cuda/contract_compile.zig"),
+ .target = target,
+ .optimize = .Debug,
+ .link_libc = true,
+ });
+ consumers.addImport("cuda", consumers_base.import_table.get("cuda").?);
+ consumers.addImport("core", consumers_base.import_table.get("core").?);
+ consumers.addImport("gpu_memory", @import("gpu_memory.zig").module(b, target));
+ const controls = b.addOptions();
+ controls.addOption(bool, "controls", true);
+ consumers.addOptions("contract_options", controls);
+ consumers.addAnonymousImport("fatbin_h3", .{ .root_source_file = b.addWriteFiles().add("compile-h3", "SYNTHETIC-COMPILE-ONLY") });
+ consumers.addAnonymousImport("fatbin_contract_controls", .{ .root_source_file = b.addWriteFiles().add("compile-controls", "SYNTHETIC-COMPILE-ONLY") });
+ @import("cuda_contract_deepseek.zig").inert(b, consumers);
+ const consumer_object = b.addObject(.{ .name = "contract-consumers", .root_module = consumers });
+ _ = consumer_object.getEmittedBin();
+ b.step("compile-cuda-contract-consumers", "Emit both real resource consumers with synthetic nonexecuted images").dependOn(&consumer_object.step);
+ const catalog = b.addExecutable(.{ .name = "dsv41-catalog", .root_module = module(b, b.graph.host, "catalog.zig", false) });
+ const record = b.addRunArtifact(catalog);
+ const output = record.captureStdOut(.{});
+ b.step("emit-dsv41-catalog", "Record symbol resolution without CUDA device work").dependOn(&b.addInstallFile(output, "dsv41-launches.txt").step);
+ const runner_base = module(b, target, "cuda_test.zig", true);
+ const runner = b.addExecutable(.{ .name = "tf-dsv41-test", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/tests/cuda/dsv41_main.zig"),
+ .target = target,
+ .optimize = .Debug,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "cuda", .module = runner_base.import_table.get("cuda").? }, .{ .name = "dsv41_kernels", .module = runner_base.import_table.get("dsv41_kernels").? } },
+ }) });
+ _ = runner.getEmittedBin();
+ b.step("compile-dsv41-runner", "Emit the supplied device fixture runner with nonexecuted synthetic images").dependOn(&runner.step);
+}
diff --git a/zig/build/dsv41_draft.zig b/zig/build/dsv41_draft.zig
new file mode 100644
index 0000000000..efeea97af5
--- /dev/null
+++ b/zig/build/dsv41_draft.zig
@@ -0,0 +1,69 @@
+const std = @import("std");
+pub fn addImports(b: *std.Build, m: *std.Build.Module, target: std.Build.ResolvedTarget) void {
+ const core = m.import_table.get("core").?;
+ const tokenizer = core.import_table.get("tokenizer").?;
+ const lanes = b.createModule(.{ .root_source_file = b.path("zig/src/core/lanes/lanes.zig"), .target = target, .optimize = .Debug, .link_libc = true });
+ const json = b.createModule(.{ .root_source_file = b.path("zig/src/server/json.zig"), .target = target, .optimize = .Debug, .link_libc = true });
+ const fixtures = b.createModule(.{ .root_source_file = b.path("zig/src/families/deepseek_v41/fixtures/serve/fixtures.zig"), .target = target, .optimize = .Debug });
+ const serve = b.createModule(.{ .root_source_file = b.path("zig/src/families/deepseek_v41/serve/root.zig"), .target = target, .optimize = .Debug, .link_libc = true, .imports = &.{ .{ .name = "json", .module = json }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "lanes", .module = lanes }, .{ .name = "dsv41_serve_fixtures", .module = fixtures } } });
+ m.addImport("lanes", lanes);
+ m.addImport("json", json);
+ m.addImport("tokenizer", tokenizer);
+ m.addImport("dsv41_serve", serve);
+ m.addImport("engine_api", b.createModule(.{ .root_source_file = b.path("zig/src/core/engine_api.zig"), .target = target, .optimize = .Debug, .link_libc = true, .imports = &.{.{ .name = "lanes", .module = lanes }} }));
+}
+pub fn steps(b: *std.Build) void {
+ inline for (.{ false, true }) |cross| {
+ const target = if (cross) b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu }) else b.graph.host;
+ const root = @import("dsv41_cuda.zig").module(b, target, "device_host_test.zig", cross);
+ const tests = b.addTest(.{ .root_module = root });
+ if (cross) {
+ _ = tests.getEmittedBin();
+ b.step("compile-dsv41-device-host", "Emit Linux model, drafting, rows and session adapters").dependOn(&tests.step);
+ const cli = b.addExecutable(.{ .name = "tf-dsv41", .root_module = @import("dsv41_cuda.zig").module(b, target, "tool_main.zig", true) });
+ _ = cli.getEmittedBin();
+ b.step("compile-dsv41-tool", "Emit actual Linux DeepSeek host and GPU-check CLI, never execute").dependOn(&cli.step);
+ b.step("compile-dsv41-server", "Emit actual Linux server with the DeepSeek lane engine, never execute").dependOn(&server(b, target).step);
+ const sampler = b.addExecutable(.{ .name = "tf-dsv41-samp", .root_module = @import("dsv41_cuda.zig").module(b, target, "sampling_check.zig", true) });
+ _ = sampler.getEmittedBin();
+ b.step("compile-dsv41-sampler", "Emit the actual Linux sampler check, never execute").dependOn(&sampler.step);
+ } else b.step("test-dsv41-device-host", "Host checks of actual row and device plans, no device work").dependOn(&b.addRunArtifact(tests).step);
+ }
+ inline for (.{ false, true }) |cross| {
+ const target = if (cross) b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu }) else b.graph.host;
+ const m = @import("dsv41_cuda.zig").module(b, target, "draft_host_test.zig", cross);
+ const test_bin = b.addTest(.{ .root_module = m });
+ if (cross) {
+ _ = test_bin.getEmittedBin();
+ b.step("compile-dsv41-draft", "Emit Linux sampling and host drafting tests").dependOn(&test_bin.step);
+ } else b.step("test-dsv41-draft", "Keyed sampling, DSpark and shared lane core host tests").dependOn(&b.addRunArtifact(test_bin).step);
+ }
+}
+
+fn server(b: *std.Build, target: std.Build.ResolvedTarget) *std.Build.Step.Compile {
+ const engine = @import("dsv41_cuda.zig").module(b, target, "serve_engine.zig", true);
+ const build_options = b.addOptions();
+ build_options.addOption([]const u8, "version", @import("../../build.zig.zon").version);
+ build_options.addOption(bool, "slide_test_hooks", false);
+ const name = "tensorfold-dsv41";
+ const api = engine.import_table.get("engine_api").?;
+ const tokenizer = engine.import_table.get("tokenizer").?;
+ const template = b.createModule(.{ .root_source_file = b.path("zig/src/core/template/template.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true });
+ const exlr8 = @import("exlr8.zig").cliImport(b, target, .ReleaseSafe, engine.import_table.get("core").?);
+ const cli = b.createModule(.{ .root_source_file = b.path("zig/src/cli/cli.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true, .imports = &.{ .{ .name = "native_engines", .module = engine }, .{ .name = "core", .module = engine.import_table.get("core").? }, exlr8 } });
+ const m = b.createModule(.{
+ .root_source_file = b.path("zig/src/server/main.zig"),
+ .target = target,
+ .optimize = .ReleaseSafe,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "engine_api", .module = api }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "template", .module = template }, .{ .name = "native_engines", .module = engine }, .{ .name = "checkpoint_cli", .module = cli } },
+ });
+ m.addImport("living", b.createModule(.{ .root_source_file = b.path("zig/src/living/living.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true }));
+ m.addOptions("build_options", build_options);
+ // the server's json and the family's text side share the engine's serving module (one `dsv41_serve`)
+ m.addImport("json", engine.import_table.get("dsv41_serve").?.import_table.get("json").?);
+ m.addImport("dsv41_serve", engine.import_table.get("dsv41_serve").?);
+ const exe = b.addExecutable(.{ .name = name, .root_module = m });
+ _ = exe.getEmittedBin();
+ return exe;
+}
diff --git a/zig/build/dsv41_images.zig b/zig/build/dsv41_images.zig
new file mode 100644
index 0000000000..c386755fca
--- /dev/null
+++ b/zig/build/dsv41_images.zig
@@ -0,0 +1,50 @@
+//! Production image ownership. Host compile checks use separate synthetic modules and never invoke this graph.
+const std = @import("std");
+const cuda_build = @import("cuda.zig");
+const list = @import("dsv41_units.zig");
+pub const Images = struct { units: [list.units.len]std.Build.LazyPath, optional: [list.optional.len]?std.Build.LazyPath };
+pub fn images(b: *std.Build, nvcc: ?[]const u8, prebuilt: ?[]const u8) ?*const Images {
+ const step = b.step("fatbins-dsv41", "Build or install the complete family images, sm_121 only");
+ const result = b.allocator.create(Images) catch @panic("OOM");
+ if (prebuilt) |dir| {
+ for (list.units, &result.units) |unit, *path| {
+ const file = b.pathJoin(&.{ dir, b.fmt("{s}.fatbin", .{unit.name}) });
+ std.Io.Dir.cwd().access(b.graph.io, file, .{}) catch {
+ step.dependOn(&b.addFail(b.fmt("Missing DeepSeek image: {s}", .{file})).step);
+ return null;
+ };
+ path.* = b.graph.cwdRelativePath(file);
+ }
+ for (list.optional, &result.optional) |unit, *path| {
+ const file = b.pathJoin(&.{ dir, b.fmt("{s}.fatbin", .{unit.name}) });
+ path.* = if (std.Io.Dir.cwd().access(b.graph.io, file, .{})) |_| b.graph.cwdRelativePath(file) else |_| null;
+ }
+ } else if (nvcc) |compiler| {
+ const version = b.addSystemCommand(&.{ compiler, "--version" }).captureStdOut(.{});
+ for (list.units, &result.units) |unit, *path| path.* = cuda_build.fatbin(b, compiler, version, unit, "121");
+ for (list.optional, &result.optional) |unit, *path| path.* = cuda_build.fatbin(b, compiler, version, unit, "121");
+ } else {
+ step.dependOn(&b.addFail("fatbins-dsv41 needs -Ddsv41-nvcc=PATH or -Ddsv41-fatbins=DIR").step);
+ return null;
+ }
+ for (list.units, result.units) |unit, path| step.dependOn(&b.addInstallFile(path, b.fmt("fatbin/{s}.fatbin", .{unit.name})).step);
+ for (list.optional, result.optional) |unit, path| if (path) |file| {
+ step.dependOn(&b.addInstallFile(file, b.fmt("fatbin/{s}.fatbin", .{unit.name})).step);
+ };
+ return result;
+}
+pub fn module(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, cuda: *std.Build.Module, embedded: ?*const Images) *std.Build.Module {
+ const options = b.addOptions();
+ options.addOption(bool, "with_kernels", embedded != null);
+ options.addOption(u32, "family_scope", 41);
+ const m = b.createModule(.{ .root_source_file = b.path("zig/src/families/deepseek_v41/kernels.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ m.addImport("cuda", cuda);
+ m.addOptions("dsv41_kernel_options", options);
+ if (embedded) |im| for (list.units, im.units) |unit, path| m.addAnonymousImport(b.fmt("dsv41_fatbin_{s}", .{unit.name}), .{ .root_source_file = path });
+ const empty = b.addWriteFiles().add("empty-dsv41.fatbin", "");
+ for (list.optional, 0..) |unit, i| {
+ const path = if (embedded) |im| im.optional[i] orelse empty else empty;
+ m.addAnonymousImport(b.fmt("dsv41_fatbin_{s}", .{unit.name}), .{ .root_source_file = path });
+ }
+ return m;
+}
diff --git a/zig/build/dsv41_shared.zig b/zig/build/dsv41_shared.zig
new file mode 100644
index 0000000000..05dc1f3cd4
--- /dev/null
+++ b/zig/build/dsv41_shared.zig
@@ -0,0 +1,28 @@
+const std = @import("std");
+pub fn module(b: *std.Build, target: std.Build.ResolvedTarget, file: []const u8) *std.Build.Module {
+ const tok = b.createModule(.{ .root_source_file = b.path("zig/src/core/tokenizer/tokenizer.zig"), .target = target, .optimize = .Debug, .link_libc = true });
+ const core = b.createModule(.{ .root_source_file = b.path("zig/src/core/root.zig"), .target = target, .optimize = .Debug, .link_libc = true });
+ core.addImport("gpu_memory", @import("gpu_memory.zig").module(b, target));
+ core.addImport("tokenizer", tok);
+ return b.createModule(.{ .root_source_file = b.path(b.fmt("zig/src/families/deepseek_v41/{s}", .{file})), .target = target, .optimize = .Debug, .link_libc = true, .imports = &.{.{ .name = "core", .module = core }} });
+}
+pub fn steps(b: *std.Build) void {
+ const host = b.addTest(.{ .root_module = module(b, b.graph.host, "shared_test.zig") });
+ b.step("test-dsv41-engram-alloc", "Actual Engram startup failure sweeps with varying remap behavior").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = host.root_module, .filters = &.{ "production batch", "engram alloc:" } })).step);
+ const arm = b.addTest(.{ .root_module = module(b, b.resolveTargetQuery(.{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .gnu }), "shared_test.zig"), .filters = &.{ "production batch", "engram alloc:" } });
+ _ = arm.getEmittedBin();
+ b.step("compile-dsv41-engram-alloc-aarch64", "Emit Linux ARM64 Engram failure controls; never execute").dependOn(&arm.step);
+ const run = b.addRunArtifact(host);
+ run.setCwd(b.path("."));
+ b.step("test-dsv41-shared", "Shared DeepSeek checkpoint host tests, no GPU dependencies").dependOn(&run.step);
+ const linux_target = b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu });
+ const cross = b.addTest(.{ .root_module = module(b, linux_target, "shared_test.zig") });
+ _ = cross.getEmittedBin();
+ b.step("compile-dsv41-shared", "Compile shared tests for Linux without executing").dependOn(&cross.step);
+ const builder = b.createModule(.{ .root_source_file = b.path("zig/dsv41_token_map.zig"), .target = b.graph.host, .optimize = .Debug, .link_libc = true });
+ const token_tests = b.addRunArtifact(b.addTest(.{ .root_module = builder }));
+ b.step("test-dsv41-token-map", "PCG64 and Unicode normalization without Metal").dependOn(&token_tests.step);
+ const tool = b.addExecutable(.{ .name = "tf-dsv41-token-map", .root_module = builder });
+ b.step("tf-dsv41-token-map", "Build the shared Engram token-map tool").dependOn(&b.addInstallArtifact(tool, .{}).step);
+ b.step("compile-dsv41-token-map", "Compile actual token-map CLI without running or Metal").dependOn(&tool.step);
+}
diff --git a/zig/build/dsv41_state.zig b/zig/build/dsv41_state.zig
new file mode 100644
index 0000000000..0a4bf6f036
--- /dev/null
+++ b/zig/build/dsv41_state.zig
@@ -0,0 +1,32 @@
+const std = @import("std");
+pub fn kvModule(b: *std.Build, target: std.Build.ResolvedTarget, cuda: *std.Build.Module, tp: *std.Build.Module) *std.Build.Module {
+ const m = b.createModule(.{ .root_source_file = b.path("zig/src/families/deepseek_v41/kv/kv.zig"), .target = target, .optimize = .Debug, .link_libc = true });
+ m.addImport("sessions", @import("sessions.zig").module(b, target, .Debug));
+ m.addImport("cuda", cuda);
+ m.addImport("tp", tp);
+ return m;
+}
+pub fn steps(b: *std.Build) void {
+ const state = @import("dsv41_shared.zig").module(b, b.graph.host, "kv_state.zig");
+ b.step("test-dsv41-state", "Shared KV transactions and state seam regressions").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = state })).step);
+ inline for (.{ false, true }) |cross| {
+ const target = if (cross) b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu }) else b.graph.host;
+ const root = @import("dsv41_cuda.zig").module(b, target, "state_test.zig", cross);
+ const check = b.addTest(.{ .root_module = root });
+ if (cross) {
+ _ = check.getEmittedBin();
+ b.step("compile-dsv41-forward", "Emit Linux forward, graphs and prefill bodies").dependOn(&check.step);
+ } else b.step("test-dsv41-forward", "Host forward and prompt plan checks").dependOn(&b.addRunArtifact(check).step);
+ }
+ const linux = b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu });
+ inline for (.{ false, true }) |cross| {
+ const target = if (cross) linux else b.graph.host;
+ const cuda = @import("dsv41_cuda.zig").runtime(b, target, false);
+ const tp = @import("tp.zig").module(b, target, .Debug, cuda);
+ const t = b.addTest(.{ .root_module = kvModule(b, target, cuda, tp) });
+ if (cross) {
+ _ = t.getEmittedBin();
+ b.step("compile-dsv41-kv", "Emit Linux paged/split KV host tests").dependOn(&t.step);
+ } else b.step("test-dsv41-kv", "Paged/split KV and session host tests").dependOn(&b.addRunArtifact(t).step);
+ }
+}
diff --git a/zig/build/dsv41_units.zig b/zig/build/dsv41_units.zig
new file mode 100644
index 0000000000..0df9892a8f
--- /dev/null
+++ b/zig/build/dsv41_units.zig
@@ -0,0 +1,39 @@
+const cuda_build = @import("cuda_kernels.zig");
+pub const units = [_]cuda_build.Kernel{
+ .{ .name = "dsv41_exl3_experts", .src = "exl3_block/exl3_experts", .flags = &.{"-O3"} }, // tensorfold_exl3_experts_v1
+ .{ .name = "dsv41_x3ld", .src = "exl3_block/x3ld", .flags = &.{"-O3"} }, // tf_dsv41_x3ld_v1
+ .{ .name = "dsv41_x3pf", .src = "exl3_block/x3pf", .flags = &.{"-O3"} }, // tf_dsv41_x3pf_v1
+ .{ .name = "dsv41_x3gm", .src = "exl3_block/x3gm", .flags = &.{"-O3"} }, // tf_dsv41_x3gm_v1
+ .{ .name = "dsv41_linear", .src = "exl3_block/linear", .flags = &.{ "-O3", "--expt-relaxed-constexpr" } }, // tensorfold_exl3_linear_v4
+ .{ .name = "dsv41_x3seg", .src = "exl3_block/x3seg", .flags = &.{ "-O3", "--expt-relaxed-constexpr" } }, // tf_dsv41_x3seg_v3
+ .{ .name = "dsv41_dense3", .src = "exl3_block/dense3", .flags = &.{ "-O3", "--expt-relaxed-constexpr" } }, // tf_dsv41_dense3_v1
+ .{ .name = "dsv41_attn", .src = "deepseek_v41/attn_cuda", .min_sm = 90, .flags = &.{ "-O3", "--fmad=false" } }, // tf_dsv41_attn_cuda_v1
+ .{ .name = "dsv41_topk", .src = "deepseek_v41/topk_cuda", .min_sm = 90, .flags = &.{ "-O3", "--fmad=false" } }, // tf_dsv41_attn_cuda_v1
+ .{ .name = "dsv41_mhc", .src = "deepseek_v41/mhc_cuda", .flags = &.{ "-O3", "--fmad=false" } }, // tf_dsv41_mhc_cuda_v1
+ .{ .name = "dsv41_mhc_pf", .src = "deepseek_v41/mhc_pf", .flags = &.{ "-O3", "--fmad=false" } }, // tf_dsv41_mhc_pf_v1
+ .{ .name = "dsv41_router_gemv", .src = "deepseek_v41/router_gemv", .flags = &.{"-O3"} }, // tf_dsv41_router_gemv_v2
+ .{ .name = "dsv41_pfdense_k8", .src = "exl3_block/pfdense_k8", .flags = &.{ "-O3", "--expt-relaxed-constexpr" } }, // tf_dsv41_pfdense_v1
+ .{ .name = "dsv41_pfdense_k10", .src = "exl3_block/pfdense_k10", .flags = &.{ "-O3", "--expt-relaxed-constexpr" } },
+ .{ .name = "dsv41_pfdense_k12", .src = "exl3_block/pfdense_k12", .flags = &.{ "-O3", "--expt-relaxed-constexpr" } },
+ .{ .name = "dsv41_pfdense_k16", .src = "exl3_block/pfdense_k16", .flags = &.{ "-O3", "--expt-relaxed-constexpr" } },
+ .{ .name = "dsv41_engram_gate", .src = "deepseek_v41/engram_gate", .flags = &.{"-O3"} }, // tf_dsv41_engram_gate_v1
+ .{ .name = "dsv41_l2pace", .src = "deepseek_v41/l2pace", .flags = &.{"-O3"} }, // tf_dsv41_l2pace_v1
+ .{ .name = "dsv41_l2pf", .src = "deepseek_v41/l2pf", .flags = &.{"-O3"} }, // tf_dsv41_l2pf_v1
+ .{ .name = "dsv41_x3gm_plan", .src = "exl3_block/x3gm_plan", .flags = &.{"-O3"} }, // ours: x3gm.plan's torch ops
+ .{ .name = "dsv41_topk_keys", .src = "deepseek_v41/topk_keys", .flags = &.{"-O3"} }, // ours: pick.top's torch.topk
+ .{ .name = "dsv41_pointwise", .src = "deepseek_v41/pointwise", .flags = &.{ "-O3", "--fmad=false" } }, // ours: prefill MoE's torch pointwise ops
+ .{ .name = "dsv41_kvsplit", .src = "deepseek_v41/kvsplit", .flags = &.{"-O3"} }, // ours: split KV's exchange copies
+ .{ .name = "dsv41_glue", .src = "deepseek_v41/glue", .flags = &.{ "-O3", "--fmad=false" } }, // ours: the window's torch glue
+ .{ .name = "dsv41_x3gm3", .src = "exl3_block/x3gm3", .flags = &.{"-O3"} }, // ours: x3gm v3 (x3gm.cu's device code)
+ .{ .name = "dsv41_pfglue", .src = "deepseek_v41/prefill_glue", .flags = &.{ "-O3", "--fmad=false" } }, // ours: the prefill segment's torch glue
+ .{ .name = "dsv41_router_glue", .src = "deepseek_v41/router_glue", .flags = &.{"-O3"} }, // histogram group + upstream rotation
+ .{ .name = "dsv41_kv_glue", .src = "deepseek_v41/kv_glue", .flags = &.{"-O3"} }, // KV RMS in rounded FP64; store keeps Triton's FMA
+ .{ .name = "dsv41_x3ld_epi", .src = "exl3_block/x3ld_epi", .flags = &.{"-O3"} }, // ours: x3ld + the expert epilogues
+ .{ .name = "dsv41_topk_b", .src = "deepseek_v41/topk_b", .min_sm = 90, .flags = &.{ "-O3", "--fmad=false" } }, // ours: topk_cuda.cu row-bounded
+};
+
+/// Optional images: the keyed sampler (TF_DSV41_SAMPLING) and two-tile linears (TF_DSV41_LIN2); empty, they refuse.
+pub const optional = [_]cuda_build.Kernel{
+ .{ .name = "dsv41_sampling", .src = "deepseek_v41/sampling", .flags = &.{ "-O3", "--fmad=false" } },
+ .{ .name = "dsv41_lin2", .src = "exl3_block/lin2", .min_sm = 90, .flags = &.{ "-O3", "--expt-relaxed-constexpr" } },
+};
diff --git a/zig/build/exl3.zig b/zig/build/exl3.zig
index 646dee6f2c..24b87bef0f 100644
--- a/zig/build/exl3.zig
+++ b/zig/build/exl3.zig
@@ -47,6 +47,8 @@ pub fn targets(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.bu
.imports = imports,
}) });
const gpu_run = b.addRunArtifact(gpu);
+ b.step("check-exl3-gpu", "Compile EXL3 Metal qualification tests without executing GPU work")
+ .dependOn(&b.addInstallArtifact(gpu, .{ .dest_sub_path = "tf-exl3-gpu" }).step);
gpu_run.setCwd(b.path("."));
b.step("test-exl3-gpu", "EXL3 Metal arithmetic, row windows, routes and validation regressions")
.dependOn(&gpu_run.step);
diff --git a/zig/build/flashnext_batch_host.zig b/zig/build/flashnext_batch_host.zig
index f97a6e3a70..2fc8859589 100644
--- a/zig/build/flashnext_batch_host.zig
+++ b/zig/build/flashnext_batch_host.zig
@@ -7,7 +7,15 @@ pub fn tests(b: *std.Build, all: *std.Build.Step) void {
sources.addAnonymousImport("nemotron_packed_metallib", .{ .root_source_file = empty });
const lanes = b.createModule(.{ .root_source_file = b.path("zig/src/core/lanes/lanes.zig"), .target = b.graph.host, .link_libc = true });
const fabric = b.createModule(.{ .root_source_file = b.path("zig/src/fabric/fabric.zig"), .target = b.graph.host, .link_libc = true });
- const root = b.createModule(.{ .root_source_file = b.path("zig/flashnext_batch_tests.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kernel_sources", .module = sources }, .{ .name = "lanes", .module = lanes }, .{ .name = "fabric", .module = fabric } } });
+ const root = b.createModule(.{ .root_source_file = b.path("zig/flashnext_batch_tests.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kernel_sources", .module = sources }, .{ .name = "lanes", .module = lanes }, .{ .name = "fabric", .module = fabric }, .{ .name = "contract", .module = @import("kernel_contract.zig").module(b, b.graph.host, .debug) } } });
+ b.step("test-flashnext-vision-contracts-host", "Flash Next masked draws and vision contracts without device execution").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = root, .filters = &.{"Flash Next contracts:"} })).step);
+ b.step("test-flashnext-load-context-host", "Independent long and ordinary Flash Next load settings without a GPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = root, .filters = &.{ "Flash Next context:", "load sets no DeltaNet" } })).step);
+ b.step("test-flashnext-rotary-bound-host", "Scalar rotary error propagation and contract hooks without shaders").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = root, .filters = &.{"rotary bound:"} })).step);
+ const facade = b.createModule(.{ .root_source_file = b.path("zig/flashnext_bound_family.zig"), .target = b.graph.host, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kernel_sources", .module = sources } } });
+ const probes = b.createModule(.{ .root_source_file = b.path("zig/flashnext_bound_probes_test.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "tensorfold", .module = facade }, .{ .name = "contract", .module = @import("kernel_contract.zig").module(b, b.graph.host, .debug) } } });
+ b.step("test-flashnext-bound-probes-host", "Real rotary allowance callbacks without GPU calls").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = probes })).step);
+ b.step("test-flashnext-precise-host", "Explicit rotary math and FP64 golden references without shaders").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = root, .filters = &.{"precise rotary:"} })).step);
+ b.step("test-rotary-sweep-host", "Exhaustive finite rotary input enumeration and receipt controls without shaders").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = root, .filters = &.{"rotary sweep:"} })).step);
const tests_ = b.addTest(.{ .root_module = root, .filters = &.{"batch widths:"} });
const run = b.addRunArtifact(tests_);
run.has_side_effects = true;
diff --git a/zig/build/flashnext_budget.zig b/zig/build/flashnext_budget.zig
index 39f9383300..c87fe0c09d 100644
--- a/zig/build/flashnext_budget.zig
+++ b/zig/build/flashnext_budget.zig
@@ -11,6 +11,14 @@ pub fn steps(b: *std.Build, all: *std.Build.Step) void {
pub fn compileHost(b: *std.Build, metal: *std.Build.Module, engine: *std.Build.Module, lanes: *std.Build.Module) void {
const api = b.createModule(.{ .root_source_file = b.path("zig/src/core/engine_api.zig"), .target = b.graph.host, .optimize = .Debug, .link_libc = true, .imports = &.{.{ .name = "lanes", .module = lanes }} });
const host = b.createModule(.{ .root_source_file = b.path("zig/src/native/flashnext_batch_host.zig"), .target = b.graph.host, .optimize = .Debug, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "tensorfold", .module = engine }, .{ .name = "engine_api", .module = api } } });
+ const legacy = b.createModule(.{ .root_source_file = b.path("zig/src/native/flashnext_host.zig"), .target = b.graph.host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "tensorfold", .module = engine }, .{ .name = "engine_api", .module = api } } });
+ const legacy_object = b.addObject(.{ .name = "flashnext-legacy-host", .root_module = b.createModule(.{ .root_source_file = b.path("zig/flashnext_legacy_compile.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{.{ .name = "host", .module = legacy }} }) });
+ _ = legacy_object.getEmittedBin();
+ b.step("compile-flashnext-legacy-host", "Compile the existing Flash Next legacy host with no shaders or execution").dependOn(&legacy_object.step);
+ const native_window = b.createModule(.{ .root_source_file = b.path("zig/src/native/metal.zig"), .target = b.graph.host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "tensorfold", .module = engine }, .{ .name = "engine_api", .module = api } } });
+ const window_object = b.addObject(.{ .name = "flashnext-window-open", .root_module = b.createModule(.{ .root_source_file = b.path("zig/flashnext_window_compile.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{.{ .name = "native", .module = native_window }} }) });
+ _ = window_object.getEmittedBin();
+ b.step("compile-flashnext-context-entry-host", "Compile the actual Flash Next context entry with empty embedded data").dependOn(&window_object.step);
const object = b.addObject(.{ .name = "flashnext-budget-host", .root_module = b.createModule(.{ .root_source_file = b.path("zig/native_budget_compile.zig"), .target = b.graph.host, .optimize = .Debug, .link_libc = true, .imports = &.{.{ .name = "batch_host", .module = host }} }) });
_ = object.getEmittedBin();
b.step("compile-flashnext-budget-host", "Compile the actual shared host against an empty test-only packed image").dependOn(&object.step);
diff --git a/zig/build/glm_contracts.zig b/zig/build/glm_contracts.zig
new file mode 100644
index 0000000000..df12c17ec1
--- /dev/null
+++ b/zig/build/glm_contracts.zig
@@ -0,0 +1,21 @@
+//! Shader-free prompt-logprob owner and maintained projection-policy controls.
+const std = @import("std");
+pub fn tests(b: *std.Build, glm: *std.Build.Module, lanes: *std.Build.Module, cuda: *std.Build.Module, core: *std.Build.Module) void {
+ const host = b.graph.host;
+ const prompt_arena = b.createModule(.{ .root_source_file = b.path("zig/glm_prompt_arena_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "glm", .module = glm }, .{ .name = "cuda", .module = cuda } } });
+ b.step("test-glm-prompt-arena-host", "Actual sequence arenas retain and account prompt rows").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = prompt_arena })).step);
+ const prompt_owner = b.createModule(.{ .root_source_file = b.path("zig/glm_prompt_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{.{ .name = "glm", .module = glm }} });
+ b.step("test-glm-prompts-host", "Actual prompt workflow with recording device operations").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = prompt_owner, .filters = &.{ "GLM prompt workflow:", "GLM structured:" } })).step);
+
+ const run = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/glm_prompt_logprobs_host.zig"),
+ .target = b.graph.host,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "glm", .module = glm }, .{ .name = "lanes", .module = lanes }, .{ .name = "cuda", .module = cuda } },
+ }) }));
+ b.step("test-glm-logprobs-host", "Actual GLM prompt row ownership and head policy without GPU calls").dependOn(&run.step);
+ const structured = b.createModule(.{ .root_source_file = b.path("zig/glm_structured_host.zig"), .target = host, .link_libc = true, .imports = &.{ .{ .name = "glm", .module = glm }, .{ .name = "core", .module = core }, .{ .name = "cuda", .module = cuda } } });
+ b.step("test-glm-structured-host", "Actual prompt redraw workflow without GPU calls").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = structured })).step);
+ const protocol = b.addRunArtifact(b.addTest(.{ .root_module = glm, .filters = &.{ "flag 1", "the head's columns" } }));
+ b.step("test-glm-protocol-host", "Prompt and grammar follow flags without GPU calls").dependOn(&protocol.step);
+}
diff --git a/zig/build/glm_family_host.zig b/zig/build/glm_family_host.zig
new file mode 100644
index 0000000000..d95b5bcc5e
--- /dev/null
+++ b/zig/build/glm_family_host.zig
@@ -0,0 +1,37 @@
+//! Existing GLM family recording controls on the CPU runtime.
+const std = @import("std");
+
+pub fn steps(b: *std.Build, cuda: *std.Build.Module, glm: *std.Build.Module, mods: anytype, step: *std.Build.Step) void {
+ const host = b.graph.host;
+ const api = b.createModule(.{ .root_source_file = b.path("zig/src/core/engine_api.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{.{ .name = "lanes", .module = mods.lanes }} });
+ const admission = b.createModule(.{ .root_source_file = b.path("zig/glm_admission_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "glm", .module = glm }, .{ .name = "lanes", .module = mods.lanes }, .{ .name = "engine_api", .module = api } } });
+ const admission_run = b.addRunArtifact(b.addTest(.{ .root_module = admission, .filters = &.{"GLM CUDA admission:"} }));
+ b.step("test-glm-admission-host", "Real GLM admission capabilities through the lane worker without GPU work").dependOn(&admission_run.step);
+ step.dependOn(&admission_run.step);
+ b.step("test-glm-prompt-settings-host", "Actual opt-in settings and rank agreement on the CPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = glm, .filters = &.{"prompt K experiment"} })).step);
+ const mm_record = b.createModule(.{ .root_source_file = b.path("zig/test_support/glm_mm_cuda.zig"), .target = host, .optimize = .debug });
+ const mm_actual = b.createModule(.{ .root_source_file = b.path("zig/src/families/glm/cuda_mm.zig"), .target = host, .optimize = .debug, .imports = &.{.{ .name = "cuda", .module = mm_record }} });
+ const mm_policy = b.createModule(.{ .root_source_file = b.path("zig/glm_mm_policy_host.zig"), .target = host, .optimize = .debug, .imports = &.{ .{ .name = "cuda", .module = mm_record }, .{ .name = "mm", .module = mm_actual } } });
+ b.step("test-glm-mm-policy-host", "Actual prompt K policy dispatch without GPU work").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mm_policy })).step);
+ const drafter_host = b.createModule(.{ .root_source_file = b.path("zig/glm_dflash_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "glm", .module = glm }, .{ .name = "core", .module = mods.core } } });
+ b.step("test-glm-dflash-host", "DFlash rank buffers, partial loader ownership and pending records on the CPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = drafter_host, .filters = &.{"glm dflash:"} })).step);
+ const packed_host = b.createModule(.{ .root_source_file = b.path("zig/glm_packed_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "glm", .module = glm }, .{ .name = "core", .module = mods.core } } });
+ b.step("test-glm-packed-host", "Packed GLM launch records, cap parsing and native loader on the CPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = packed_host, .filters = &.{"glm packed:"} })).step);
+ const ring_host = b.createModule(.{ .root_source_file = b.path("zig/glm_ring_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "glm", .module = glm } } });
+ b.step("test-glm-ring-host", "Actual GLM MTP rollback pool through recording CUDA copies").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = ring_host, .filters = &.{"glm ring:"} })).step);
+ step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = glm })).step);
+ const prompt_host = b.createModule(.{ .root_source_file = b.path("zig/exl3_prompt_host.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "glm", .module = glm }, .{ .name = "lanes", .module = mods.lanes } } });
+ const prompt_run = b.addRunArtifact(b.addTest(.{ .root_module = prompt_host, .filters = &.{"exl3 prompt:"} }));
+ prompt_run.has_side_effects = true;
+ b.step("test-exl3-prompt-host", "Actual common prompt dispatch and scratch with recording CUDA calls").dependOn(&prompt_run.step);
+}
+
+pub fn compile(b: *std.Build, target: std.Build.ResolvedTarget, cuda: *std.Build.Module, mods: anytype, comptime makeGlm: anytype) void {
+ const glm_open = b.addObject(.{ .name = "glm-open-check", .root_module = b.createModule(.{ .root_source_file = b.path("zig/glm_open_compile.zig"), .target = target, .optimize = .debug, .link_libc = true, .imports = &.{.{ .name = "glm", .module = makeGlm(b, target, .debug, cuda, mods.core, mods.lanes, mods.heat) }} }) });
+ _ = glm_open.getEmittedBin();
+ b.step("compile-glm-native-open", "Compile the actual GLM Linux open body with empty GPU images").dependOn(&glm_open.step);
+ const prompt_gate = b.createModule(.{ .root_source_file = b.path("zig/tests/cuda/glm_mm_fixed_tests.zig"), .target = target, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "glm_cuda", .module = makeGlm(b, target, .debug, cuda, mods.core, mods.lanes, mods.heat) }, .{ .name = "cuda", .module = cuda } } });
+ const prompt_gate_compile = b.addObject(.{ .name = "glm-prompt-gate-compile", .root_module = b.createModule(.{ .root_source_file = b.path("zig/glm_prompt_gate_compile.zig"), .target = target, .optimize = .debug, .link_libc = true, .imports = &.{.{ .name = "gate", .module = prompt_gate }} }) });
+ _ = prompt_gate_compile.getEmittedBin();
+ b.step("compile-glm-prompt-gate-host", "Compile the actual device prompt comparison gate with inert images").dependOn(&prompt_gate_compile.step);
+}
diff --git a/zig/build/golden.zig b/zig/build/golden.zig
index 6d9ba224cc..4cd3fa66f1 100644
--- a/zig/build/golden.zig
+++ b/zig/build/golden.zig
@@ -18,11 +18,15 @@ pub fn step(b: *std.Build, target: std.Build.ResolvedTarget, server: *std.Build.
.optimize = .Debug,
.link_libc = true,
}) });
- const golden_run = b.addRunArtifact(golden_check);
- golden_run.addArtifactArg(fake_serve);
- golden_run.addFileArg(b.path("zig/tests/server/golden/cases.json"));
- golden_run.addDirectoryArg(b.path("zig/tests/server/golden"));
- golden_run.addDirectoryArg(b.path("zig/tests/server/fixtures"));
- golden_run.setCwd(b.path("."));
- b.step("test-golden", "The frozen server parity goldens checked by a Zig step driving fake_serve").dependOn(&golden_run.step);
+ const all = b.step("test-golden", "Frozen parity answers and native HTTP security goldens");
+ for ([_][]const u8{ "cases.json", "security-cases.json" }) |file| {
+ const run = b.addRunArtifact(golden_check);
+ run.addArtifactArg(fake_serve);
+ run.addFileArg(b.path(b.fmt("zig/tests/server/golden/{s}", .{file})));
+ run.addDirectoryArg(b.path("zig/tests/server/golden"));
+ run.addDirectoryArg(b.path("zig/tests/server/fixtures"));
+ run.setCwd(b.path("."));
+ all.dependOn(&run.step);
+ if (std.mem.eql(u8, file, "security-cases.json")) b.step("test-security-golden-host", "Native HTTP key, host and origin security with the local fake server").dependOn(&run.step);
+ }
}
diff --git a/zig/build/gpu_memory.zig b/zig/build/gpu_memory.zig
new file mode 100644
index 0000000000..617536722d
--- /dev/null
+++ b/zig/build/gpu_memory.zig
@@ -0,0 +1,6 @@
+const std = @import("std");
+pub fn module(b: *std.Build, target: std.Build.ResolvedTarget) *std.Build.Module {
+ const key = b.fmt("gpu_memory_{s}", .{target.result.zigTriple(b.allocator) catch @panic("target name")});
+ if (b.modules.get(key)) |m| return m;
+ return b.addModule(key, .{ .root_source_file = b.path("zig/src/gpu_memory.zig"), .target = target, .optimize = .Debug });
+}
diff --git a/zig/build/gradient_host.zig b/zig/build/gradient_host.zig
new file mode 100644
index 0000000000..585dea36ec
--- /dev/null
+++ b/zig/build/gradient_host.zig
@@ -0,0 +1,16 @@
+//! Compile the production file copies into one host identity fixture; no GPU or shader dependency.
+const std = @import("std");
+pub fn add(b: *std.Build) void {
+ const files = b.addWriteFiles();
+ var lines = std.mem.tokenizeScalar(u8, @embedFile("../tests/fixtures/gradient-serving.tsv"), '\n');
+ while (lines.next()) |line| {
+ const tab = std.mem.indexOfScalar(u8, line, '\t').?;
+ const path = line[0..tab];
+ _ = files.addCopyFile(b.path(path), path);
+ }
+ _ = files.addCopyFile(b.path("zig/tests/fixtures/gradient-serving.tsv"), "manifest.tsv");
+ _ = files.addCopyFile(b.path("zig/tests/fixtures/pocket-serving-approved.tsv"), "approved.tsv");
+ const root = files.addCopyFile(b.path("zig/tests/gradient_serving_pin.zig"), "main.zig");
+ const test_ = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = root, .target = b.graph.host, .optimize = .debug }) });
+ b.step("test-gradient-serving", "Pin the serving kernels' sources and the explicit learning extensions").dependOn(&b.addRunArtifact(test_).step);
+}
diff --git a/zig/build/hip.zig b/zig/build/hip.zig
index 0f1a3510ee..366d317dcc 100644
--- a/zig/build/hip.zig
+++ b/zig/build/hip.zig
@@ -95,6 +95,9 @@ pub fn steps(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.buil
// the GPU programs compile on any host against stub kernels, so `test` catches what only they reach
test_step.dependOn(&checksExe(b, b.graph.host, .Debug, host).step);
test_step.dependOn(&server(b, b.graph.host, host, build_options).step);
+ const check_target = b.resolveTargetQuery(.{ .cpu_arch = b.graph.host.result.cpu.arch, .os_tag = .linux, .abi = .gnu });
+ const check = engineModules(b, check_target, .Debug, stub_kernels, null);
+ b.step("check-native-hip-host", "Compile the real native HIP server with inert kernels, without GPU execution").dependOn(&server(b, check_target, check, build_options).step);
const gpu = engineModules(b, target, optimize, objects, hip_include);
b.step("tf-qwen35-test", "Qwen3.5 / 3.6 model checks on the GPU (-Dhip-arch)").dependOn(&b.addInstallArtifact(checksExe(b, target, optimize, gpu), .{}).step);
const fx_test = b.addExecutable(.{ .name = "tf-flashnext-hip-test", .root_module = b.createModule(.{
@@ -261,6 +264,7 @@ fn kernelObjects(b: *std.Build, hipcc: []const u8, hipcc_resolved: []const u8, d
/// The backend-neutral core (checkpoint formats, the kernel registry), as the other backends import it.
fn coreModule(b: *std.Build, target: std.Build.ResolvedTarget) *std.Build.Module {
const core = b.createModule(.{ .root_source_file = b.path("zig/src/core/root.zig"), .target = target, .link_libc = true });
+ core.addImport("gpu_memory", @import("gpu_memory.zig").module(b, target));
core.addImport("tokenizer", b.createModule(.{ .root_source_file = b.path("zig/src/core/tokenizer/tokenizer.zig"), .target = target, .link_libc = true }));
return core;
}
diff --git a/zig/build/inc4_host.zig b/zig/build/inc4_host.zig
new file mode 100644
index 0000000000..ef1d485366
--- /dev/null
+++ b/zig/build/inc4_host.zig
@@ -0,0 +1,10 @@
+//! Host controls for the incremented operator contracts; empty CUDA images and no device execution.
+const std = @import("std");
+pub fn round(b: *std.Build, cuda: *std.Build.Module, core: *std.Build.Module) void {
+ const m = b.createModule(.{ .root_source_file = b.path("zig/tests/cuda/contract_round_host.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "core", .module = core } } });
+ b.step("test-inc4-round-host", "Actual round fixture layout and pending launch controls").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m })).step);
+}
+pub fn rows(b: *std.Build, cuda: *std.Build.Module, core: *std.Build.Module) void {
+ const m = b.createModule(.{ .root_source_file = b.path("zig/tests/cuda/contract_rows_host.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "core", .module = core } } });
+ b.step("test-inc4-rows-host", "Actual custom numerical checkers and pending operator launches").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m })).step);
+}
diff --git a/zig/build/kernel_contract.zig b/zig/build/kernel_contract.zig
index 1101785a2f..d343f3dc68 100644
--- a/zig/build/kernel_contract.zig
+++ b/zig/build/kernel_contract.zig
@@ -3,7 +3,9 @@ const std = @import("std");
/// The contract module alone (its host tests run in `zig build test` through core/root.zig).
pub fn module(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Module {
- return b.createModule(.{ .root_source_file = b.path("zig/src/core/contract.zig"), .target = target, .optimize = optimize });
+ const m = b.createModule(.{ .root_source_file = b.path("zig/src/core/contract.zig"), .target = target, .optimize = optimize });
+ m.addImport("gpu_memory", @import("gpu_memory.zig").module(b, target));
+ return m;
}
/// `test-kernel-contract` on every host: the gates' own tests and every registered launch against every chip's limits.
@@ -13,6 +15,12 @@ pub fn hostTests(b: *std.Build, family: *std.Build.Step) void {
const step = b.step("test-kernel-contract", "The kernel contract's host tests: gate logic, controls, every launch against every chip");
step.dependOn(&run.step);
step.dependOn(family);
+ const zstd = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/src/core/contract/fatbin_zstd.zig"),
+ .target = b.graph.host,
+ }) }));
+ b.step("test-fatbin-zstd-host", "Bounded compressed-image reader and encoded expansion controls").dependOn(&zstd.step);
+ step.dependOn(&zstd.step);
const delta = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{
.root_source_file = b.path("zig/delta_slots_contract_test.zig"),
.target = b.graph.host,
@@ -24,6 +32,7 @@ pub fn hostTests(b: *std.Build, family: *std.Build.Step) void {
/// macOS: tf-kernel-contract over the engine's Metal kernels; `kernel-contract` runs it on this Mac's GPU.
pub fn metal(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, metal_mod: *std.Build.Module, engine: *std.Build.Module, sources: *std.Build.Module) void {
+ @import("rotary_sweep.zig").steps(b, target, metal_mod);
const exe = b.addExecutable(.{ .name = "tf-kernel-contract", .root_module = b.createModule(.{
.root_source_file = b.path("zig/tests/contract/metal_main.zig"),
.target = target,
diff --git a/zig/build/kernel_symbols.zig b/zig/build/kernel_symbols.zig
new file mode 100644
index 0000000000..8b5c743bba
--- /dev/null
+++ b/zig/build/kernel_symbols.zig
@@ -0,0 +1,16 @@
+//! Literal device lookups must have a matching source token before any device build.
+const std = @import("std");
+
+pub fn tests(b: *std.Build, all: *std.Build.Step) void {
+ const cases = b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/tests/kernel_symbols.zig"),
+ .target = b.graph.host,
+ .optimize = .debug,
+ }) });
+ const run = b.addRunArtifact(cases);
+ run.setCwd(b.path("."));
+ run.has_side_effects = true;
+ const step = b.step("test-kernel-symbols-host", "Every literal CUDA/HIP lookup has a source token, without a device");
+ step.dependOn(&run.step);
+ all.dependOn(step);
+}
diff --git a/zig/build/kolibri.zig b/zig/build/kolibri.zig
new file mode 100644
index 0000000000..5594da2359
--- /dev/null
+++ b/zig/build/kolibri.zig
@@ -0,0 +1,14 @@
+//! Kolibri family construction and CPU metadata controls on the maintained CUDA runtime.
+const std = @import("std");
+
+pub fn module(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, cuda: *std.Build.Module, core: *std.Build.Module, lanes: *std.Build.Module) *std.Build.Module {
+ return b.createModule(.{ .root_source_file = b.path("zig/src/families/kolibri1/cuda.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "core", .module = core }, .{ .name = "lanes", .module = lanes } } });
+}
+
+pub fn host(b: *std.Build, m: *std.Build.Module, api: *std.Build.Module, all: *std.Build.Step) void {
+ const run = b.addRunArtifact(b.addTest(.{ .root_module = m }));
+ b.step("test-kolibri-host", "Kolibri config, ownership and copy-draft metadata without a GPU").dependOn(&run.step);
+ all.dependOn(&run.step);
+ const options = b.createModule(.{ .root_source_file = b.path("zig/src/native/cuda_options.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{.{ .name = "engine_api", .module = api }} });
+ b.step("test-native-options-host", "Generic family option forwarding through declared fields only").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = options })).step);
+}
diff --git a/zig/build/lane_halt_host.zig b/zig/build/lane_halt_host.zig
new file mode 100644
index 0000000000..479cfa402b
--- /dev/null
+++ b/zig/build/lane_halt_host.zig
@@ -0,0 +1,10 @@
+//! CPU checks for halt ordering, with the real lane host and fake target.
+const std = @import("std");
+
+pub fn steps(b: *std.Build) void {
+ const lanes = b.createModule(.{ .root_source_file = b.path("zig/src/core/lanes/lanes.zig"), .target = b.graph.host, .optimize = .debug, .link_libc = true });
+ const api = b.createModule(.{ .root_source_file = b.path("zig/src/core/engine_api.zig"), .target = b.graph.host, .optimize = .debug, .link_libc = true, .imports = &.{.{ .name = "lanes", .module = lanes }} });
+ const halt = b.addTest(.{ .root_module = api, .filters = &.{ "halt (", "halt fails" } });
+ b.step("test-lane-halt-host", "Halt ordering with a fake backend and no GPU dependencies").dependOn(&b.addRunArtifact(halt).step);
+ b.step("test-lane-api-host", "Shared lane host and engine API CPU regressions").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = api })).step);
+}
diff --git a/zig/build/living_dense.zig b/zig/build/living_dense.zig
new file mode 100644
index 0000000000..a3b964119e
--- /dev/null
+++ b/zig/build/living_dense.zig
@@ -0,0 +1,23 @@
+//! Dense CUDA learner checks, including the real constructor under a fake driver with deliberately invalid images.
+const std = @import("std");
+
+pub fn target(b: *std.Build, t: std.Build.ResolvedTarget, o: std.builtin.OptimizeMode, cuda: *std.Build.Module, qwen: *std.Build.Module) void {
+ const m = b.createModule(.{ .root_source_file = b.path("zig/src/cli/qwen35_learn.zig"), .target = t, .optimize = o, .link_libc = true, .imports = &.{ .{ .name = "cuda", .module = cuda }, .{ .name = "qwen35", .module = qwen } } });
+ const exe = b.addExecutable(.{ .name = "tf-qwen35-learn", .root_module = m });
+ b.installArtifact(exe);
+ b.step("compile-qwen35-learn", "Compile actual dense model qualification without execution").dependOn(&exe.step);
+}
+
+pub fn host(b: *std.Build, cuda: *std.Build.Module, qwen: *std.Build.Module, draft_ids: *std.Build.Module, comptime runtime: anytype, comptime family: anytype, comptime n: usize) void {
+ const t = b.graph.host;
+ const pin = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/dense_serving_test.zig"), .target = t, .optimize = .debug }) }));
+ b.step("test-qwen-serving-host", "Landed CUDA kernels and dense serving launch pins").dependOn(&pin.step);
+ b.step("test-qwen-pocket-host", "Dense device publication and checked reads under CUDA failures").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = qwen, .filters = &.{ "dense pocket:", "dense media:", "dense save:", "dense read cost:" } })).step);
+ const placeholder = b.addWriteFiles().add("fake-driver-only.fatbin", "FAKE DRIVER ONLY, NEVER A DEVICE IMAGE");
+ const images: [n]?std.Build.LazyPath = @splat(placeholder);
+ const fake_cuda = runtime(b, t, .debug, &images);
+ const fake = family(b, t, .debug, fake_cuda, draft_ids);
+ const constructors = b.addRunArtifact(b.addTest(.{ .root_module = fake.qwen35, .filters = &.{"dense constructor:"} }));
+ b.step("test-dense-constructors-host", "Actual dense constructor ownership and completion failures").dependOn(&constructors.step);
+ _ = cuda;
+}
diff --git a/zig/build/living_tools_host.zig b/zig/build/living_tools_host.zig
new file mode 100644
index 0000000000..b81567628f
--- /dev/null
+++ b/zig/build/living_tools_host.zig
@@ -0,0 +1,10 @@
+//! Compile the actual CPU pocket-fit tool against its real fitter without loading an engine or shaders.
+const std = @import("std");
+pub fn steps(b: *std.Build) void {
+ const fit = b.createModule(.{ .root_source_file = b.path("zig/src/living/pocket_fit.zig"), .target = b.graph.host });
+ const files = b.addWriteFiles();
+ const facade = b.createModule(.{ .root_source_file = files.add("pocket-fit-family.zig", "pub const nemotron = struct { pub const pocket_fit = @import(\"living_fit\"); };\n"), .target = b.graph.host, .imports = &.{.{ .name = "living_fit", .module = fit }} });
+ const sys = b.createModule(.{ .root_source_file = b.path("zig/src/platform/sys.zig"), .target = b.graph.host, .link_libc = true });
+ const tool = b.addExecutable(.{ .name = "pocket-fit-host", .root_module = b.createModule(.{ .root_source_file = b.path("zig/tests/pocket_fit_check.zig"), .target = b.graph.host, .optimize = .debug, .link_libc = true, .imports = &.{ .{ .name = "tensorfold", .module = facade }, .{ .name = "sys", .module = sys } } }) });
+ b.step("compile-pocket-fit-host", "Compile the actual pocket-fit tool with its real CPU fitter and no shaders").dependOn(&tool.step);
+}
diff --git a/zig/build/metal_targets.zig b/zig/build/metal_targets.zig
new file mode 100644
index 0000000000..e1ee522f30
--- /dev/null
+++ b/zig/build/metal_targets.zig
@@ -0,0 +1,513 @@
+//! Metal build targets and their shared source/library/module construction.
+const std = @import("std");
+const builtin = @import("builtin");
+const qwen27_build = @import("qwen27.zig");
+const core_checks = @import("core_checks.zig");
+const glm_checks = @import("glm_checks.zig");
+const flashnext_checks = @import("flashnext_checks.zig");
+const counters_build = @import("metal_counters.zig");
+const exl3_build = @import("exl3.zig");
+const dsv41_build = @import("deepseek_v41.zig");
+const exlr8_build = @import("exlr8.zig");
+const server_build = @import("server.zig");
+const golden_build = @import("golden.zig");
+const contract_build = @import("kernel_contract.zig");
+const metal_lists = @import("metal_source_lists.zig");
+const kernels = metal_lists.kernels;
+const generated = metal_lists.generated;
+const kimi_dir = metal_lists.kimi_dir;
+const kimi_kernels = metal_lists.kimi_kernels;
+const nemotron_packed = metal_lists.nemotron_packed;
+
+pub fn mlxMetalStd(b: *std.Build) []const u8 {
+ const os = b.graph.host.result.os;
+ const major = if (os.tag == .macos) os.version_range.semver.min.major else 0;
+ if (major >= 27) return "metal4.1";
+ if (major >= 26) return "metal4.0";
+ if (major >= 15) return "metal3.2";
+ return "metal3.1";
+}
+
+pub fn metallib(b: *std.Build, name: []const u8, sources: []const []const u8, flags: []const []const u8) std.Build.LazyPath {
+ const link = b.addSystemCommand(&.{ "xcrun", "-sdk", "macosx", "metallib", "-o" });
+ const out = link.addOutputFileArg(b.fmt("{s}.metallib", .{name}));
+ for (sources) |src| {
+ const cc = b.addSystemCommand(&.{ "xcrun", "-sdk", "macosx", "metal" });
+ cc.addArgs(flags);
+ cc.addArg("-c");
+ cc.addFileArg(b.path(src));
+ cc.addArg("-o");
+ link.addFileArg(cc.addOutputFileArg(b.fmt("{s}.air", .{std.fs.path.stem(src)})));
+ }
+ return out;
+}
+
+pub fn kimiMetallib(b: *std.Build, flags: []const []const u8) std.Build.LazyPath {
+ const link = b.addSystemCommand(&.{ "xcrun", "-sdk", "macosx", "metallib", "-o" });
+ const out = link.addOutputFileArg("kimi.metallib");
+ for (kimi_kernels) |name| {
+ const cc = b.addSystemCommand(&.{ "xcrun", "-sdk", "macosx", "metal" });
+ cc.addArgs(flags);
+ cc.addPrefixedDirectoryArg("-I", b.path(kimi_dir));
+ cc.addFileInput(b.path(kimi_dir ++ "/kimi_common.h"));
+ cc.addArg("-c");
+ cc.addFileArg(b.path(b.fmt("{s}/{s}", .{ kimi_dir, name })));
+ cc.addArg("-o");
+ link.addFileArg(cc.addOutputFileArg(b.fmt("kimi_{s}.air", .{std.fs.path.stem(name)})));
+ }
+ return out;
+}
+
+pub fn embedded(b: *std.Build, lib: std.Build.LazyPath, file: []const u8) *std.Build.Module {
+ const wf = b.addWriteFiles();
+ _ = wf.addCopyFile(lib, file);
+ const src = wf.add("metallib.zig", b.fmt("pub const bytes = @embedFile(\"{s}\");\n", .{file}));
+ return b.createModule(.{ .root_source_file = src });
+}
+
+pub fn nativeServer(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, metal: *std.Build.Module, engine: *std.Build.Module, living: *std.Build.Module, api: *std.Build.Module, engines: *std.Build.Module, build_options: *std.Build.Step.Options, test_step: ?*std.Build.Step) *std.Build.Step.Compile {
+ // the HTTP side keeps its safety checks; the engine below it runs at `optimize`
+ const tokenizer = engines.import_table.get("tokenizer") orelse b.createModule(.{ .root_source_file = b.path("zig/src/core/tokenizer/tokenizer.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true });
+ const template = b.createModule(.{ .root_source_file = b.path("zig/src/core/template/template.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true });
+ const exe = b.addExecutable(.{ .name = "tensorfold-native", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/src/server/main.zig"),
+ .target = target,
+ .optimize = .ReleaseSafe,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "engine_api", .module = api }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "template", .module = template }, .{ .name = "native_engines", .module = engines }, .{ .name = "living", .module = living }, .{ .name = "checkpoint_cli", .module = b.createModule(.{ .root_source_file = b.path("zig/src/cli/cli.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true, .imports = &.{ .{ .name = "native_engines", .module = engines }, exlr8_build.cliImport(b, target, .ReleaseSafe, engine), .{ .name = "core", .module = engine } } }) } },
+ }) });
+ exe.root_module.addOptions("build_options", build_options);
+ _ = server_build.addTo(b, exe.root_module, target, .ReleaseSafe);
+ if (test_step == null) {
+ exe.root_module.strip = true;
+ return exe;
+ }
+ const install = b.addInstallArtifact(exe, .{ .dest_dir = .{ .override = .{ .custom = "native/bin" } } });
+ b.step("native", "tensorfold-native with the Metal engines into zig-out/native/bin (bundles: -Dcpu=apple_m1)").dependOn(&install.step);
+ // Link libSystem's memcpy to avoid compiler-rt's weak replacement.
+ const libc_mem = b.addSystemCommand(&.{ "sh", "-c", "nm -m \"$0\" | grep -q 'external _memcpy (from libSystem)' || { echo 'tensorfold-native links its own memcpy: find the typed std.json int parse or 128-bit float conversion that pulled in compiler-rt'; exit 1; }" });
+ libc_mem.addArtifactArg(exe);
+ test_step.?.dependOn(&libc_mem.step);
+ const server_tests = b.createModule(.{
+ .root_source_file = b.path("zig/src/server/root.zig"),
+ .target = target,
+ .optimize = .Debug, // the server's unit tests run with safety checks, as zig_test.sh builds them
+ .link_libc = true,
+ .imports = &.{ .{ .name = "engine_api", .module = api }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "template", .module = template }, .{ .name = "living", .module = living } },
+ });
+ // the required-tools tests' 27B mask policy and lane fake, on the engine API's own lanes module
+ if (api.import_table.get("lanes")) |lanes_mod| {
+ const q27 = @import("qwen27_required.zig").modules(b, lanes_mod);
+ server_tests.addImport("qwen27_mask_fake", q27.fake);
+ server_tests.addImport("qwen27_masks", q27.masks);
+ }
+ const server_json = server_build.addTo(b, server_tests, target, .Debug);
+ test_step.?.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests })).step);
+ test_step.?.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_json })).step);
+ @import("cortheon_fuzz.zig").add(b, target, api, tokenizer, template);
+ // the parse half's tests, as their own test binary (zig/src/server/tool_parse_test.zig)
+ const tool_parse_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/src/server/tool_parse_test.zig"), .target = target, .optimize = .Debug, .link_libc = true, .imports = &.{ .{ .name = "engine_api", .module = api }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "template", .module = template } } }) });
+ _ = server_build.addTo(b, tool_parse_tests.root_module, target, .Debug);
+ test_step.?.dependOn(&b.addRunArtifact(tool_parse_tests).step);
+ const dsv41_template = b.addExecutable(.{ .name = "tf-dsv41-template-check", .root_module = b.createModule(.{ .root_source_file = b.path("zig/tests/dsv41_template_check.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true, .imports = &.{.{ .name = "template", .module = template }} }) });
+ b.step("tf-dsv41-template-check", "Check a DeepSeek template against encoding fixtures").dependOn(&b.addInstallArtifact(dsv41_template, .{}).step);
+ // the chat-template engine's tests (zig/src/core/template/tests.zig), which no other test binary reaches
+ test_step.?.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/src/core/template/tests.zig"), .target = target, .optimize = .Debug, .link_libc = true }) })).step);
+ // the engine seam's own tests (lane_host.zig), as zig/tests/server/zig_test.sh runs them
+ test_step.?.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = api })).step);
+ // the frozen server parity goldens, checked by Zig driving fake_serve
+ golden_build.step(b, target, server_tests, api, tokenizer, template);
+ b.step("test-native", "The Metal engines module's host-side tests").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = engines })).step);
+ const reuse = b.addExecutable(.{ .name = "tf-flashnext-reuse", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/tests/flashnext_reuse.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "tensorfold", .module = engine }, .{ .name = "engine_api", .module = api } },
+ }) });
+ b.installArtifact(reuse);
+ b.step("tf-flashnext-reuse", "Flash Next prompt reuse against fresh prompt passes: replies at every depth and kept states' bytes").dependOn(&b.addInstallArtifact(reuse, .{}).step);
+ const batch = b.addExecutable(.{ .name = "tf-flashnext-batch", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/tests/flashnext_batch.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "tensorfold", .module = engine }, .{ .name = "kernel_sources", .module = engine.import_table.get("kernel_sources").? } },
+ }) });
+ b.step("tf-flashnext-batch", "Flash Next shared rounds against fresh legacy replies with prompt admission and cancellation").dependOn(&b.addInstallArtifact(batch, .{}).step);
+ return exe;
+}
+
+pub fn metalTargets(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, draft_ids: *std.Build.Module, build_options: *std.Build.Step.Options, test_step: *std.Build.Step) void {
+ const metal_std = b.option([]const u8, "metal-std", "Metal language version (default: MLX's for this macOS)") orelse mlxMetalStd(b);
+
+ // MLX's mx.fast.metal_kernel JIT: MTLCompileOptions mathMode safe, fp32 functions fast (the default), its...
+ const mlx_flags = [_][]const u8{ b.fmt("-std={s}", .{metal_std}), "-fmetal-math-mode=safe", "-fmetal-math-fp32-functions=fast" };
+ const lib = metallib(b, "tensorfold", &kernels, &mlx_flags);
+ const lib_install = b.addInstallFile(lib, "lib/tensorfold.metallib");
+ b.step("metallib", "Compile zig/kernels/metal into tensorfold.metallib").dependOn(&lib_install.step);
+
+ // Control: the same Nemotron kernels under fast math, which the bit-exact check must be able to tell apart.
+ const fast_flags = [_][]const u8{ b.fmt("-std={s}", .{metal_std}), "-fmetal-math-mode=fast", "-fmetal-math-fp32-functions=fast" };
+ const fast_lib = metallib(b, "nemotron_fastmath", generated, &fast_flags);
+ b.getInstallStep().dependOn(&b.addInstallFile(fast_lib, "lib/nemotron_fastmath.metallib").step);
+ b.getInstallStep().dependOn(&lib_install.step);
+
+ const mods = metalEngineModules(b, target, optimize, draft_ids);
+ const metal = mods.metal;
+ const kernels_mod = embedded(b, lib, "tensorfold.metallib");
+ counters_build.targets(b, target, optimize, metal, mods.sources, test_step);
+
+ // The engines registry: the families this platform's binaries serve; the CLI reads it for models/info/pull.
+ const api = b.createModule(.{ .root_source_file = b.path("zig/src/core/engine_api.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{.{ .name = "lanes", .module = mods.lanes }} });
+ const engines = b.createModule(.{
+ .root_source_file = b.path("zig/src/native/metal.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "engine_api", .module = api }, .{ .name = "tensorfold", .module = mods.engine } },
+ });
+ engines.addImport("tokenizer", mods.tokenizer);
+ @import("drama3_metal.zig").addNative(b, target, optimize, metal, engines, metal_std);
+ @import("drama3_metal.zig").targets(b, target, optimize, metal, metal_std, test_step);
+
+ const programs = [_]struct { name: []const u8, path: []const u8, about: []const u8 }{
+ .{ .name = "tf-metal-tests", .path = "zig/tests/gpu_tests.zig", .about = "GPU runtime tests" },
+ .{ .name = "tf-dispatch-bench", .path = "zig/tests/dispatch_bench.zig", .about = "Dispatch boundary benchmark" },
+ .{ .name = "tf-kernel-exact", .path = "zig/tests/kernel_exact.zig", .about = "Bit-exact check of a kernel against an MLX oracle case" },
+ };
+ for (programs) |p| {
+ const mod = b.createModule(.{
+ .root_source_file = b.path(p.path),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "metallib", .module = kernels_mod } },
+ });
+ const exe = b.addExecutable(.{ .name = p.name, .root_module = mod });
+ b.installArtifact(exe);
+ b.step(p.name, p.about).dependOn(&b.addInstallArtifact(exe, .{}).step);
+ }
+
+ // Kimi K3: the family module, its metallib, and its GPU check and benchmark programs.
+ const kimi_lib = kimiMetallib(b, &mlx_flags);
+ b.getInstallStep().dependOn(&b.addInstallFile(kimi_lib, "lib/kimi.metallib").step);
+ const kimi = b.createModule(.{
+ .root_source_file = b.path("zig/src/families/kimi_k3/kimi_k3.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{.{ .name = "metal", .module = metal }},
+ });
+ const kimi_bytes = embedded(b, kimi_lib, "kimi.metallib");
+ const kimi_programs = [_]struct { name: []const u8, path: []const u8, about: []const u8 }{
+ .{ .name = "tf-k3-check", .path = "zig/tests/kimi_k3/check.zig", .about = "Kimi K3 kernels and layers on synthetic weights at real shapes" },
+ .{ .name = "tf-k3-bench", .path = "zig/tests/kimi_k3/bench.zig", .about = "Kimi K3 kernel bandwidth" },
+ };
+ for (kimi_programs) |p| {
+ const mod = b.createModule(.{
+ .root_source_file = b.path(p.path),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kimi_k3", .module = kimi }, .{ .name = "kimi_metallib", .module = kimi_bytes } },
+ });
+ const exe = b.addExecutable(.{ .name = p.name, .root_module = mod });
+ b.installArtifact(exe);
+ b.step(p.name, p.about).dependOn(&b.addInstallArtifact(exe, .{}).step);
+ }
+
+ // Kimi's tiktoken tokenizer and its parity program (tools/zig/k3_tokenizer_parity.py).
+ const tiktoken = b.createModule(.{ .root_source_file = b.path("zig/src/core/tokenizer/tiktoken.zig"), .target = target, .optimize = optimize });
+ const tok_mod = b.createModule(.{
+ .root_source_file = b.path("zig/tests/kimi_k3/tokenizer.zig"),
+ .target = target,
+ .optimize = optimize,
+ .imports = &.{.{ .name = "tiktoken", .module = tiktoken }},
+ });
+ const tok_exe = b.addExecutable(.{ .name = "tf-k3-tokenizer", .root_module = tok_mod });
+ b.installArtifact(tok_exe);
+ b.step("tf-k3-tokenizer", "Kimi K3's tiktoken tokenizer over parity cases").dependOn(&b.addInstallArtifact(tok_exe, .{}).step);
+
+ // The cluster layer: inventory, membership, planner, loading and converged rounds over the lane core.
+ const lanes = mods.lanes;
+ const fabric = mods.fabric;
+ const cluster = b.createModule(.{
+ .root_source_file = b.path("zig/src/cluster/cluster.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "lanes", .module = lanes }, .{ .name = "fabric", .module = fabric } },
+ });
+ // The engine: kernel sources compiled at run time (MLX's options), the families, the lane core.
+ const sources = mods.sources;
+ // Nemotron's packed kernels, compiled offline and embedded: the runtime fallback when this macOS's compiler...
+ const packed_lib = metallib(b, "nemotron_packed", &nemotron_packed, &mlx_flags);
+ b.getInstallStep().dependOn(&b.addInstallFile(packed_lib, "lib/nemotron_packed.metallib").step);
+ sources.addImport("nemotron_packed_metallib", embedded(b, packed_lib, "nemotron_packed.metallib"));
+ const engine = mods.engine;
+ // FlashNext's CPU contracts, with the family module's imports so the replay graph type-checks
+ const flash_host = b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/flashnext_host.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kernel_sources", .module = sources }, .{ .name = "nemotron_draft_ids", .module = draft_ids }, .{ .name = "lanes", .module = lanes }, .{ .name = "fabric", .module = fabric } },
+ }) });
+ b.step("compile-flashnext-host", "Compile FlashNext CPU metadata contracts without running them").dependOn(&flash_host.step);
+ const flash_host_run = b.addRunArtifact(flash_host);
+ b.step("test-flashnext-host", "Run FlashNext CPU metadata contracts without Metal").dependOn(&flash_host_run.step);
+ test_step.dependOn(&flash_host_run.step);
+ // Pack builder cases that do not need the replay graph.
+ const pack_cases = b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/flashnext_pack_cases.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ }) });
+ test_step.dependOn(&b.addRunArtifact(pack_cases).step);
+ const load_admission_cases = b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/flashnext_load_admission_cases.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ }) });
+ const load_admission_run = b.addRunArtifact(load_admission_cases);
+ b.step("test-flashnext-load-admission", "CPU-only Flash Next runtime quantization admission").dependOn(&load_admission_run.step);
+ test_step.dependOn(&load_admission_run.step);
+ const pack_cache_cases = b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/flashnext_pack_cache_cases.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ }) });
+ const pack_cache_run = b.addRunArtifact(pack_cache_cases);
+ b.step("test-flashnext-pack-cache", "CPU-only atomic Flash Next pack cache lifecycle").dependOn(&pack_cache_run.step);
+ test_step.dependOn(&pack_cache_run.step);
+ const engine_programs = [_]struct { name: []const u8, path: []const u8, about: []const u8, c_source: ?[]const u8 = null }{
+ .{ .name = "tf-dsv41-run", .path = "zig/deepseek_v41_run.zig", .about = "DeepSeek Metal admission, numerical and streamed Engram receipts" },
+ .{ .name = "tf-dsv41-transport-probe", .path = "zig/dsv41_transport_probe.zig", .about = "DeepSeek framed transport without model allocation" },
+ .{ .name = "tf-qwen35-check", .path = "zig/tests/qwen35_check.zig", .about = "Qwen3.5-2B checkpoint and native operations" },
+ .{ .name = "tf-qwen35-forward", .path = "zig/tests/qwen35_forward.zig", .about = "Qwen3.5-2B native teacher-forced logits" },
+ .{ .name = "tf-qwen35-exact", .path = "zig/tests/qwen35_exact.zig", .about = "Qwen3.5-2B forward and committed-state exactness" },
+ .{ .name = "tf-lfm2-forward", .path = "zig/tests/lfm2_forward.zig", .about = "LFM2-MoE native teacher-forced logits" },
+ .{ .name = "tf-lfm2-bench", .path = "zig/tests/lfm2_bench.zig", .about = "LFM2-MoE prompt-chunk and one-row decode timing" },
+ .{ .name = "tf-lfm2-exact", .path = "zig/tests/lfm2_exact.zig", .about = "LFM2-MoE prompt chunks, windows and committed-state exactness" },
+ .{ .name = "tf-lfm2-fp64", .path = "zig/tests/lfm2_fp64.zig", .about = "LFM2 projection classes and attention on the tensor units, the tiles and the twin, each against float64" },
+ .{ .name = "tensorfold", .path = "zig/src/main.zig", .about = "The native engine's command line" },
+ .{ .name = "tf-nemotron-fixtures", .path = "zig/tests/nemotron_fixtures.zig", .about = "Nemotron kernels against the Python engine's captured ops" },
+ .{ .name = "tf-nemotron-bench", .path = "zig/tests/nemotron_bench.zig", .about = "One-row projection kernels timed by tile count" },
+ .{ .name = "tf-nemotron-wide", .path = "zig/tests/nemotron_wide.zig", .about = "Window costs by kernel class and expert overlap of drafted, sibling and stream rows" },
+ .{ .name = "tf-nemotron-prefill-check", .path = "zig/tests/nemotron_prefill_check.zig", .about = "A prompt chunk layer by layer against prefill_dump.py's rows" },
+ .{ .name = "tf-nemotron-dense", .path = "zig/tests/nemotron_dense.zig", .about = "Dense projection schedules bit-checked against the engine's kernels, then timed" },
+ .{ .name = "tf-nemotron-experts", .path = "zig/tests/nemotron_experts.zig", .about = "Routed-expert pass shapes bit-checked against the engine's kernels, then timed by window width" },
+ .{ .name = "tf-nemotron-sample-check", .path = "zig/tests/nemotron_sample_check.zig", .about = "tf_sample_full against its host reference on synthetic rows, timed beside tf_gpu_sample" },
+ .{ .name = "tf-train-check", .path = "zig/tests/train_check.zig", .about = "The learning step's gradients against the engine's own forward by finite differences, on the real model" },
+ .{ .name = "tf-train-pack", .path = "zig/tests/train_pack.zig", .about = "Training passes holding several sequences against one a pass, on the real model: differences and time" },
+ .{ .name = "tf-pocket-fit-check", .path = "zig/tests/pocket_fit_check.zig", .about = "A dumped pocket lesson's read rows fitted as the learner fits them, timed, each fit's numbers and weights written" },
+ .{ .name = "tf-flashnext-run", .path = "zig/tests/flashnext_run.zig", .about = "Flash Next one-row greedy steps on the Python engine's recorded kernels, against its tokens" },
+ .{ .name = "tf-flashnext-pack", .path = "zig/tests/flashnext_pack.zig", .about = "Flash Next's three weight packs built from a checkpoint, byte-compared with a Python dump" },
+ .{ .name = "tf-flashnext-think", .path = "zig/tests/flashnext_think.zig", .about = "Flash Next's verdict kernels under thinking budgets on a fake target, against the lane core's close" },
+ .{ .name = "tf-grid-sync-bench", .path = "zig/tests/grid_sync_bench.zig", .about = "A GPU-wide barrier in one persistent dispatch against dependent relaunches" },
+ .{ .name = "tf-weight-read-check", .path = "zig/tests/weight_read_check.zig", .about = "Check native file reads and failed-read cleanup", .c_source = "zig/tests/pread_fault.c" },
+ .{ .name = "tf-glm-run", .path = "zig/tests/glm_run.zig", .about = "GLM-5.3-Flash greedy replies at each draft depth against depth 0 and reference tokens" },
+ .{ .name = "tf-glm-vision", .path = "zig/tests/glm_vision.zig", .about = "GLM-5.3-Flash's image tower on an image or a reference dump's patches, compared with the dump" },
+ .{ .name = "tf-glm-video", .path = "zig/tests/glm_video.zig", .about = "GLM-5.3-Flash's video preparation on a clip (libtfvideo), written for tools/zig/glm_video_fixtures.py" },
+ .{ .name = "tf-qwen-vision", .path = "zig/tests/qwen_vision.zig", .about = "The Qwen-VL tower on picture files: patches, grids and merged rows written for a float32 reference" },
+ .{ .name = "tf-qwen-image", .path = "zig/tests/qwen_image.zig", .about = "Qwen-Image-2.1's transformer on a reference case: each step compared, then the schedule timed" },
+ .{ .name = "tf-glm-attn-probe", .path = "zig/tests/glm_attn_probe.zig", .about = "GLM-5.3-Flash's dense latent attention kernels on given inputs (scores, probabilities, outputs)" },
+ .{ .name = "tf-moe-bench", .path = "zig/tests/moe_bench.zig", .about = "Routed-expert prompt kernels from .metal files on a skewed routing: chained speed, bits against the first" },
+ .{ .name = "tf-mm-bench", .path = "zig/tests/mm_bench.zig", .about = "Prompt matmul kernels from .metal files: chained speed at one shape, bits against the first, error against fp64" },
+ .{ .name = "tf-gemma4-kernels", .path = "zig/tests/gemma4/kernels.zig", .about = "Gemma 4 dense decode kernels against the reference's dumped fixtures, bit for bit by window width" },
+ .{ .name = "tf-gemma4-glue", .path = "zig/tests/gemma4/glue.zig", .about = "Gemma 4 dense glue kernels against the reference's dumped fixtures: embed, norm, q/k/v, attention, tails, softcap" },
+ .{ .name = "tf-gemma4-run", .path = "zig/tests/gemma4/run.zig", .about = "Gemma 4 dense greedy decode, tokens and top-two margins for the oracle's check" },
+ .{ .name = "tf-gemma4-lanes", .path = "zig/tests/gemma4/lanes.zig", .about = "Gemma 4 dense on the lane core: plain, copy-drafted and shared-round streams against the oracle's tokens" },
+ .{ .name = "tf-gemma4-vision", .path = "zig/tests/gemma4/vision.zig", .about = "Gemma 4's image tower against gemma4_vision_fixtures.py's dumps: patches bit for bit, each stage's error beside torch bf16's" },
+ .{ .name = "tf-gemma4-bench", .path = "zig/tests/gemma4/bench.zig", .about = "Gemma 4 dense step costs: projections alone by rows against whole forwards" },
+ .{ .name = "tf-qwen36-row-seam", .path = "zig/tests/qwen36_row_seam.zig", .about = "Model-free fixture: the row projection's bias sums stay fp32 at every width" },
+ };
+ for (engine_programs) |p| {
+ const mod = b.createModule(.{
+ .root_source_file = b.path(p.path),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "tensorfold", .module = engine }, .{ .name = "core", .module = engine }, .{ .name = "cluster", .module = cluster }, .{ .name = "kernel_sources", .module = sources }, .{ .name = "native_engines", .module = engines }, .{ .name = "engine_api", .module = api }, exlr8_build.cliImport(b, target, optimize, engine) },
+ });
+ if (p.c_source) |file| {
+ mod.addCSourceFile(.{ .file = b.path(file), .flags = &.{"-std=c11"} });
+ }
+ const exe = b.addExecutable(.{ .name = p.name, .root_module = mod });
+ b.installArtifact(exe);
+ b.step(p.name, p.about).dependOn(&b.addInstallArtifact(exe, .{}).step);
+ }
+
+ glm_checks.sampler(b, target, optimize, metal, lanes, sources);
+
+ // MiniMax H3 / FastH3's transformer: the packed rows' denoising forwards against a case file (M5 tensor units).
+ const h3_kernels = b.createModule(.{ .root_source_file = b.path("zig/kernels/metal/h3/kernels.zig") });
+ const h3_safetensors = b.createModule(.{ .root_source_file = b.path("zig/src/core/safetensors.zig"), .target = target, .optimize = optimize });
+ const h3_mod = b.createModule(.{ .root_source_file = b.path("zig/src/families/h3/h3.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "safetensors", .module = h3_safetensors }, .{ .name = "h3_kernels", .module = h3_kernels }, .{ .name = "kernel_sources", .module = sources } } });
+ const h3_dit = b.addExecutable(.{ .name = "tf-h3-dit", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/tests/h3_dit.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "h3", .module = h3_mod } },
+ }) });
+ b.step("tf-h3-dit", "FastH3's Metal transformer against a case file: velocities, every step, timings").dependOn(&b.addInstallArtifact(h3_dit, .{}).step);
+ // The core row projection (chips without tensor units) on synthetic matrices; runs on any Mac's GPU
+ const row_mod = b.createModule(.{ .root_source_file = b.path("zig/src/core/row_projection.zig"), .target = target, .optimize = optimize, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kernel_sources", .module = sources } } });
+ const row_check = b.addExecutable(.{ .name = "tf-row-check", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/tests/row_projection_check.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "row_projection", .module = row_mod } },
+ }) });
+ b.step("tf-row-check", "The core row projection on synthetic 4-bit matrices: widths bit-equal, a CPU reference, indexed experts").dependOn(&b.addInstallArtifact(row_check, .{}).step);
+ core_checks.rowWidths(b, target, optimize, metal, row_mod);
+ test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = row_mod })).step);
+ core_checks.tileQmm(b, target, optimize, metal, sources, test_step);
+ exl3_build.targets(b, target, optimize, metal, test_step);
+ test_step.dependOn(dsv41_build.weights(b, target, optimize, metal));
+ const dsv41 = dsv41_build.checks(b, target, optimize, metal, sources, engine);
+ test_step.dependOn(dsv41.device);
+ test_step.dependOn(dsv41.reference);
+ exlr8_build.metalTargets(b, target, optimize, metal, mods.sources, test_step);
+ exlr8_build.glmTargets(b, target, optimize, metal, mods.engine);
+
+ // The checkpoint CLI: models, info and pull, against a synthetic cache and a fake hub, at ReleaseSafe as it ships.
+ const cli_tests = b.addTest(.{ .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/src/cli/tests.zig"),
+ .target = target,
+ .optimize = .ReleaseSafe,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "native_engines", .module = engines }, exlr8_build.cliImport(b, target, optimize, mods.engine), .{ .name = "core", .module = mods.engine } },
+ }) });
+ const run_cli_tests = b.addRunArtifact(cli_tests);
+ test_step.dependOn(&run_cli_tests.step);
+ b.step("test-cli", "The checkpoint CLI's host tests").dependOn(&run_cli_tests.step);
+
+ // The core lane, shared-context and Qwen3.8-27B programs and host tests.
+ qwen27_build.targets(b, target, optimize, metal, sources, engine, lanes, mods.qwen27, test_step);
+ core_checks.targets(b, target, optimize, metal, engine);
+ contract_build.metal(b, target, optimize, metal, engine, sources);
+ glm_checks.targets(b, target, optimize, metal, engine, sources);
+ flashnext_checks.targets(b, target, optimize, metal, engine);
+
+ _ = nativeServer(b, target, optimize, metal, engine, mods.living, api, engines, build_options, test_step);
+
+ const cluster_metal = b.createModule(.{
+ .root_source_file = b.path("zig/src/cluster/metal_sink.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "cluster", .module = cluster } },
+ });
+ const cluster_exe = b.addExecutable(.{ .name = "tf-cluster", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/src/cluster/main.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{.{ .name = "cluster", .module = cluster }},
+ }) });
+ b.installArtifact(cluster_exe);
+ b.step("tf-cluster", "tensorfold cluster / serve --cluster / node").dependOn(&b.addInstallArtifact(cluster_exe, .{}).step);
+ // Kimi K3's checkpoint validator: the cluster's header reader cross-checked with the family's own specs.
+ const k3_validate = b.addExecutable(.{ .name = "tf-k3-validate", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/tests/kimi_k3/validate.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "kimi_k3", .module = kimi }, .{ .name = "cluster", .module = cluster } },
+ }) });
+ b.installArtifact(k3_validate);
+ b.step("tf-k3-validate", "Kimi K3's checkpoint: index, headers, coverage and every tensor against the family").dependOn(&b.addInstallArtifact(k3_validate, .{}).step);
+ const k3_cluster = b.addExecutable(.{ .name = "tf-k3-cluster", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/tests/kimi_k3/cluster.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kimi_k3", .module = kimi }, .{ .name = "kimi_metallib", .module = kimi_bytes }, .{ .name = "cluster", .module = cluster } },
+ }) });
+ b.installArtifact(k3_cluster);
+ b.step("tf-k3-cluster", "Kimi K3 tensor- and expert-parallel ranks against one node, through the cluster's canon").dependOn(&b.addInstallArtifact(k3_cluster, .{}).step);
+ const tp2 = b.addExecutable(.{ .name = "tf-tp2-bench", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/tests/tp2_bench.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "fabric", .module = fabric } },
+ }) });
+ b.step("tf-tp2-bench", "TP=2's exchange as the GPU sees it, between two Macs over MCDMA").dependOn(&b.addInstallArtifact(tp2, .{}).step);
+ const cluster_tests = b.step("test-cluster", "Cluster tests: fake nodes and fabric, K3 planning (TF_K3_DIR), Metal sink");
+ cluster_tests.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = cluster })).step);
+ cluster_tests.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = cluster_metal })).step);
+
+ // These module tests include Metal device checks when the macOS host has a GPU.
+ test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = metal })).step);
+ const helpers = b.createModule(.{
+ .root_source_file = b.path("zig/tests/common.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "metallib", .module = kernels_mod } },
+ });
+ test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = helpers })).step);
+ test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = engine })).step);
+ test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = lanes })).step);
+ // the fake fabric maps its windows with mmap: the Macs' RDMA, not Windows
+ if (target.result.os.tag != .windows) test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = fabric })).step);
+ const dashboard = b.createModule(.{ .root_source_file = b.path("zig/src/server/dashboard_test.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = dashboard })).step);
+ const kimi_test = b.step("test-k3", "Kimi K3's host-side unit tests (no GPU work)");
+ for ([_]*std.Build.Module{ kimi, tiktoken }) |m| {
+ const run = b.addRunArtifact(b.addTest(.{ .root_module = m }));
+ kimi_test.dependOn(&run.step);
+ test_step.dependOn(&run.step);
+ }
+}
+
+pub fn metalEngineModules(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, draft_ids: *std.Build.Module) struct { metal: *std.Build.Module, lanes: *std.Build.Module, fabric: *std.Build.Module, sources: *std.Build.Module, engine: *std.Build.Module, living: *std.Build.Module, tokenizer: *std.Build.Module, qwen27: qwen27_build.Shared } {
+ const metal = b.createModule(.{
+ .root_source_file = b.path("zig/src/metal/metal.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ });
+ metal.linkFramework("Metal", .{});
+ metal.linkFramework("Foundation", .{});
+ metal.linkFramework("IOSurface", .{}); // the Neural Engine's prompt share passes IOSurfaces
+ metal.linkSystemLibrary("objc", .{});
+ const lanes = b.createModule(.{ .root_source_file = b.path("zig/src/core/lanes/lanes.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ const fabric = b.createModule(.{ .root_source_file = b.path("zig/src/fabric/fabric.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ const sources = b.createModule(.{ .root_source_file = b.path("zig/kernels/metal/sources.zig") });
+ const tokenizer = b.createModule(.{ .root_source_file = b.path("zig/src/core/tokenizer/tokenizer.zig"), .target = target, .optimize = .ReleaseSafe, .link_libc = true });
+ // Living Weights' family-neutral learner, which Nemotron's lessons and the server's view of them share
+ const living = b.createModule(.{ .root_source_file = b.path("zig/src/living/living.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ const engine = b.createModule(.{
+ .root_source_file = b.path("zig/src/tensorfold.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "kernel_sources", .module = sources }, .{ .name = "nemotron_draft_ids", .module = draft_ids }, .{ .name = "lanes", .module = lanes }, .{ .name = "fabric", .module = fabric }, .{ .name = "living", .module = living }, .{ .name = "tokenizer", .module = tokenizer } },
+ });
+ return .{ .metal = metal, .lanes = lanes, .fabric = fabric, .sources = sources, .engine = engine, .living = living, .tokenizer = tokenizer, .qwen27 = qwen27_build.engineModules(b, target, metal, sources, engine) };
+}
+
+pub fn distMetalServer(b: *std.Build, target: std.Build.ResolvedTarget, draft_ids: *std.Build.Module, build_options: *std.Build.Step.Options, sdk: []const u8) *std.Build.Step.Compile {
+ const m = metalEngineModules(b, target, .fast, draft_ids);
+ // the release binary embeds the same prebuilt packed-kernel metallib as the development build
+ const flags = [_][]const u8{ b.fmt("-std={s}", .{mlxMetalStd(b)}), "-fmetal-math-mode=safe", "-fmetal-math-fp32-functions=fast" };
+ m.sources.addImport("nemotron_packed_metallib", embedded(b, metallib(b, "nemotron_packed", &nemotron_packed, &flags), "nemotron_packed.metallib"));
+ // An explicit deployment target is a cross-build even on macOS. Supply the SDK search paths.
+ m.metal.addSystemFrameworkPath(b.graph.cwdRelativePath(b.pathJoin(&.{ sdk, "System/Library/Frameworks" })));
+ m.metal.addLibraryPath(b.graph.cwdRelativePath(b.pathJoin(&.{ sdk, "usr/lib" })));
+ m.metal.addSystemIncludePath(b.graph.cwdRelativePath(b.pathJoin(&.{ sdk, "usr/include" })));
+ const api = b.createModule(.{ .root_source_file = b.path("zig/src/core/engine_api.zig"), .target = target, .optimize = .fast, .link_libc = true, .imports = &.{.{ .name = "lanes", .module = m.lanes }} });
+ const engines = b.createModule(.{ .root_source_file = b.path("zig/src/native/metal.zig"), .target = target, .optimize = .fast, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = m.metal }, .{ .name = "engine_api", .module = api }, .{ .name = "tensorfold", .module = m.engine } } });
+ engines.addImport("tokenizer", m.tokenizer);
+ @import("drama3_metal.zig").addNative(b, target, .fast, m.metal, engines, mlxMetalStd(b));
+ return nativeServer(b, target, .fast, m.metal, m.engine, m.living, api, engines, build_options, null);
+}
diff --git a/zig/build/qwen27_host.zig b/zig/build/qwen27_host.zig
index 7eab2f52b7..7ef1d6d56d 100644
--- a/zig/build/qwen27_host.zig
+++ b/zig/build/qwen27_host.zig
@@ -1,6 +1,8 @@
//! Host regressions use no shaders, model or GPU execution.
const std = @import("std");
pub fn tests(b: *std.Build) void {
+ const ane_policy = b.createModule(.{ .root_source_file = b.path("zig/src/families/qwen27/ane_policy.zig"), .target = b.graph.host });
+ b.step("test-qwen27-ane-policy-host", "Neural Engine opt-in and tensor-unit chip policy without shaders").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = ane_policy })).step);
const metal = b.createModule(.{ .root_source_file = b.path("zig/src/metal/metal.zig"), .target = b.graph.host });
const root = b.createModule(.{ .root_source_file = b.path("zig/src/qwen27_speed_test.zig"), .target = b.graph.host, .optimize = .debug, .link_libc = true, .imports = &.{.{ .name = "metal", .module = metal }} });
const run = b.addRunArtifact(b.addTest(.{ .root_module = root, .filters = &.{"qwen27 speed:"} }));
@@ -8,13 +10,17 @@ pub fn tests(b: *std.Build) void {
b.step("test-qwen27-speed-host", "Prompt tap guards, attention admission and cache failure ownership").dependOn(&run.step);
const fake = b.createModule(.{ .root_source_file = b.path("zig/src/qwen27_speed_metal.zig"), .target = b.graph.host });
const core = b.createModule(.{ .root_source_file = b.path("zig/qwen27_speed_core.zig"), .target = b.graph.host, .imports = &.{.{ .name = "metal", .module = fake }} });
+ const sources = b.createModule(.{ .root_source_file = b.addWriteFiles().add("qwen27-fake-source.zig", "pub const core_deltanet = \"\";\n"), .target = b.graph.host });
const delta = b.createModule(.{ .root_source_file = b.path("zig/src/core/deltanet_kernels.zig"), .target = b.graph.host, .imports = &.{.{ .name = "metal", .module = fake }} });
+ delta.addImport("kernel_sources", sources);
const keep_module = b.createModule(.{
.root_source_file = b.path("zig/src/qwen27_keep_test.zig"),
.target = b.graph.host,
.optimize = .debug,
.imports = &.{ .{ .name = "metal", .module = fake }, .{ .name = "core", .module = core }, .{ .name = "core_delta", .module = delta } },
});
+ const bank_module = b.createModule(.{ .root_source_file = b.path("zig/src/qwen27_bank_test.zig"), .target = b.graph.host, .optimize = .debug, .imports = &.{ .{ .name = "metal", .module = fake }, .{ .name = "core", .module = core }, .{ .name = "core_delta", .module = delta } } });
+ b.step("test-qwen27-bank-host", "Actual DeltaNet bank ownership with a recording device, no GPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = bank_module, .filters = &.{"qwen27 bank:"} })).step);
const keep = b.addRunArtifact(b.addTest(.{ .root_module = keep_module, .filters = &.{"qwen27 speed:"} }));
keep.has_side_effects = true;
b.step("test-qwen27-keep-host", "Actual shared round keep with a recording Metal facade").dependOn(&keep.step);
@@ -39,6 +45,8 @@ pub fn tests(b: *std.Build) void {
pub fn compileHost(b: *std.Build, metal: *std.Build.Module, engine: *std.Build.Module, lanes: *std.Build.Module) void {
const api = b.createModule(.{ .root_source_file = b.path("zig/src/core/engine_api.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{.{ .name = "lanes", .module = lanes }} });
const host = b.createModule(.{ .root_source_file = b.path("zig/src/native/qwen27_served.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{ .{ .name = "metal", .module = metal }, .{ .name = "tensorfold", .module = engine }, .{ .name = "engine_api", .module = api } } });
+ const boundary = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/qwen27_bank_boundary_host.zig"), .target = b.graph.host, .optimize = .debug, .link_libc = true, .imports = &.{.{ .name = "tensorfold", .module = engine }} }), .filters = &.{"qwen27 bank boundary:"} });
+ b.step("test-qwen27-bank-boundary-host", "Runner pending-keep guards with no device or shader execution").dependOn(&b.addRunArtifact(boundary).step);
const object = b.addObject(.{ .name = "qwen27-speed-compile", .root_module = b.createModule(.{
.root_source_file = b.path("zig/qwen27_speed_compile.zig"),
.target = b.graph.host,
diff --git a/zig/build/qwen_contract.zig b/zig/build/qwen_contract.zig
index ba84730974..aafaa046e5 100644
--- a/zig/build/qwen_contract.zig
+++ b/zig/build/qwen_contract.zig
@@ -9,13 +9,42 @@ pub fn host(b: *std.Build, core: *std.Build.Module, all: *std.Build.Step) void {
}) }));
b.step("test-qwen-served-contract-host", "FP64 Qwen contract references and corruption controls without GPU code").dependOn(&run.step);
all.dependOn(&run.step);
+ imageTests(b, core, all);
}
-pub fn images(b: *std.Build, runner: *std.Build.Step.Compile, controls: ?std.Build.LazyPath, h3: ?std.Build.LazyPath, kernels: anytype, files: []const ?std.Build.LazyPath) void {
+pub fn images(b: *std.Build, runner: *std.Build.Step.Compile, controls: ?std.Build.LazyPath, h3: ?std.Build.LazyPath, kernels: anytype, files: []const ?std.Build.LazyPath) *std.Build.Step {
const install = @import("kernel_contract.zig").cuda(b, runner, controls, h3);
for (kernels, files) |k, file| {
- if (std.mem.eql(u8, k.name, "moe") or std.mem.eql(u8, k.name, "qwen36_mtp")) {
+ if (std.mem.eql(u8, k.name, "moe") or std.mem.eql(u8, k.name, "qwen36_mtp") or std.mem.eql(u8, k.name, "sample") or std.mem.eql(u8, k.name, "glm_glue")) {
if (file) |image| install.dependOn(&b.addInstallFile(image, b.fmt("fatbin/{s}.fatbin", .{k.name})).step);
}
}
+ return install;
+}
+
+fn imageTests(b: *std.Build, core: *std.Build.Module, all: *std.Build.Step) void {
+ const files = b.addWriteFiles();
+ const kernels = b.createModule(.{ .root_source_file = b.path("zig/src/cuda/kernels.zig"), .target = b.graph.host });
+ const kernel_options = b.addOptions();
+ kernel_options.addOption(bool, "with_kernels", true);
+ kernels.addOptions("kernel_options", kernel_options);
+ for (@import("cuda_kernels.zig").kernels) |k| kernels.addAnonymousImport(b.fmt("fatbin_{s}", .{k.name}), .{
+ .root_source_file = files.add(b.fmt("{s}.fatbin", .{k.name}), k.name),
+ });
+ const facade = b.createModule(.{ .root_source_file = files.add("image_cuda.zig", "pub const kernels = @import(\"kernels\");"), .target = b.graph.host });
+ facade.addImport("kernels", kernels);
+ const tests = b.createModule(.{
+ .root_source_file = b.path("zig/tests/cuda/contract_images_test.zig"),
+ .target = b.graph.host,
+ .imports = &.{ .{ .name = "cuda", .module = facade }, .{ .name = "core", .module = core } },
+ });
+ @import("cuda_contract_deepseek.zig").inert(b, tests);
+ const opts = b.addOptions();
+ opts.addOption(bool, "controls", true);
+ tests.addOptions("contract_options", opts);
+ tests.addAnonymousImport("fatbin_contract_controls", .{ .root_source_file = files.add("contract_controls.fatbin", "contract_controls") });
+ tests.addAnonymousImport("fatbin_h3", .{ .root_source_file = files.add("h3.fatbin", "h3") });
+ const run = b.addRunArtifact(b.addTest(.{ .root_module = tests, .filters = &.{"contract image:"} }));
+ b.step("test-contract-images-host", "Live contract image routing and unknown-name refusal with inert bytes").dependOn(&run.step);
+ all.dependOn(&run.step);
}
diff --git a/zig/build/qwen_cost_open.zig b/zig/build/qwen_cost_open.zig
new file mode 100644
index 0000000000..b85652cd57
--- /dev/null
+++ b/zig/build/qwen_cost_open.zig
@@ -0,0 +1,25 @@
+//! Source-bound startup cache integration and descriptor identity checks, with no shader producer.
+const std = @import("std");
+pub fn tests(b: *std.Build, mods: anytype, native: anytype, all: *std.Build.Step) void {
+ const root = b.createModule(.{ .root_source_file = b.path("zig/qwen35_cost_open_host.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{ .{ .name = "native", .module = native.engines }, .{ .name = "engine_api", .module = native.api }, .{ .name = "qwen35", .module = mods.qwen35 }, .{ .name = "lanes", .module = mods.lanes } } });
+ const cost_run1 = b.addRunArtifact(b.addTest(.{ .root_module = root, .filters = &.{"window cost open:"} }));
+ b.step("test-qwen35-cost-open-host", "Actual native opening consumes cached measured widths before its worker").dependOn(&cost_run1.step);
+ all.dependOn(&cost_run1.step);
+ const files = b.addWriteFiles();
+ _ = files.add("target/config.json", "{}");
+ _ = files.add("target/model.safetensors", "\x02\x00\x00\x00\x00\x00\x00\x00{}");
+ _ = files.add("draft/model.safetensors", "\x02\x00\x00\x00\x00\x00\x00\x00{}");
+ _ = files.addCopyFile(b.path("zig/tests/fixtures/qwen35_cost_dflash.json"), "draft/config.json");
+ _ = files.addCopyFile(b.path("zig/tests/fixtures/qwen35_cost_dflash8.json"), "draft8/config.json");
+ _ = files.add("draft8/model.safetensors", "\x02\x00\x00\x00\x00\x00\x00\x00{}");
+ const process = b.createModule(.{ .root_source_file = b.path("zig/qwen35_cost_process.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{ .{ .name = "qwen35", .module = mods.qwen35 }, .{ .name = "lanes", .module = mods.lanes }, .{ .name = "core", .module = mods.core } } });
+ const run = b.addRunArtifact(b.addExecutable(.{ .name = "qwen-cost-key-process", .root_module = process }));
+ run.addDirectoryArg(files.getDirectory());
+ run.has_side_effects = true;
+ b.step("test-qwen35-cost-process-host", "Production cost keys across separate processes with inactive config tails").dependOn(&run.step);
+ all.dependOn(&run.step);
+ const identity = b.createModule(.{ .root_source_file = b.path("zig/src/families/qwen3_5/cuda_cost_identity.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{.{ .name = "core", .module = mods.core }} });
+ const cost_run2 = b.addRunArtifact(b.addTest(.{ .root_module = identity, .filters = &.{"cost identity:"} }));
+ b.step("test-qwen35-cost-identity-host", "Drafter file identity invalidates stale timings without reading weight payloads").dependOn(&cost_run2.step);
+ all.dependOn(&cost_run2.step);
+}
diff --git a/zig/build/release_cuda_refusal.zig b/zig/build/release_cuda_refusal.zig
new file mode 100644
index 0000000000..095035b50e
--- /dev/null
+++ b/zig/build/release_cuda_refusal.zig
@@ -0,0 +1,10 @@
+//! Release-server refusal controls use the real admission and a private environment provider.
+const std = @import("std");
+pub fn tests(b: *std.Build, all: *std.Build.Step, api: *std.Build.Module, nemotron: *std.Build.Module) void {
+ const env = b.createModule(.{ .root_source_file = b.path("zig/cuda_release_refusal_env.zig"), .target = b.graph.host });
+ const refusal = b.createModule(.{ .root_source_file = b.path("zig/src/native/cuda_refusal.zig"), .target = b.graph.host, .imports = &.{ .{ .name = "engine_api", .module = api }, .{ .name = "nemotron", .module = nemotron }, .{ .name = "sys", .module = env } } });
+ const root = b.createModule(.{ .root_source_file = b.path("zig/cuda_release_refusal_tests.zig"), .target = b.graph.host, .link_libc = true, .imports = &.{ .{ .name = "release_refusal", .module = refusal }, .{ .name = "policy_env", .module = env } } });
+ const run = b.addRunArtifact(b.addTest(.{ .root_module = root, .filters = &.{"DeepSeek release:"} }));
+ b.step("test-release-cuda-refusal-host", "Release DeepSeek refusal across controlled opt-in values without device work").dependOn(&run.step);
+ all.dependOn(&run.step);
+}
diff --git a/zig/build/release_paths.zig b/zig/build/release_paths.zig
new file mode 100644
index 0000000000..9f75c51767
--- /dev/null
+++ b/zig/build/release_paths.zig
@@ -0,0 +1,192 @@
+//! Release payloads may not disclose the build root or absolute source/cache paths.
+const std = @import("std");
+const cubin = @import("contract").cubin;
+const magic = "\x50\xed\x55\xba";
+
+fn separator(c: u8) bool {
+ return c == '/' or c == '\\';
+}
+fn boundary(c: u8) bool {
+ return c <= ' ' or c == '"' or c == '\'' or c == '=' or c == '(' or c == '[';
+}
+fn equalPath(a: u8, b: u8) bool {
+ return std.ascii.toLower(if (separator(a)) '/' else a) == std.ascii.toLower(b);
+}
+fn starts(path: []const u8, text: []const u8) bool {
+ if (path.len < text.len) return false;
+ for (path[0..text.len], text) |a, b| if (!equalPath(a, b)) return false;
+ return true;
+}
+fn contains(path: []const u8, text: []const u8) bool {
+ if (path.len < text.len) return false;
+ for (0..path.len - text.len + 1) |i| if (starts(path[i..], text)) return true;
+ return false;
+}
+fn absoluteAt(bytes: []const u8, i: usize) bool {
+ if (i > 0 and !boundary(bytes[i - 1])) return false;
+ if (separator(bytes[i])) return true;
+ return i + 2 < bytes.len and std.ascii.isAlphabetic(bytes[i]) and bytes[i + 1] == ':' and separator(bytes[i + 2]);
+}
+// Isolated drive-like instruction bytes need two named components before they qualify as a text path.
+fn driveTail(path: []const u8) bool {
+ var named = false;
+ var separated = false;
+ for (path) |c| {
+ if (c < ' ' or c == 127) break;
+ if (separator(c)) {
+ if (named) separated = true;
+ } else if (c > ' ') {
+ if (separated) return true;
+ named = true;
+ }
+ }
+ return false;
+}
+fn privatePath(path: []const u8) bool {
+ if (path.len >= 3 and std.ascii.isAlphabetic(path[0]) and path[1] == ':' and separator(path[2]) and driveTail(path[3..]))
+ return true;
+ for ([_][]const u8{ "/users/", "/home/", "/root/", "/build/", "/workspace/", "/workspaces/", "/__w/", "/agent/_work/" }) |prefix| {
+ if (starts(path, prefix) and path.len > prefix.len and !boundary(path[prefix.len])) return true;
+ }
+ for ([_][]const u8{ "/zig/src/", "/zig/build/", "/zig/kernels/", "/zig-cache/", "/.zig-cache/" }) |part| {
+ if (contains(path, part)) return true;
+ }
+ for ([_][]const u8{ ".zig", ".cu", ".cuh", ".c", ".cpp", ".h", ".hpp", ".metal", ".ptx", ".cubin", ".fatbin", ".o", ".obj" }) |ext| {
+ if (path.len < ext.len) continue;
+ for (0..path.len - ext.len + 1) |i| {
+ if (!starts(path[i..], ext)) continue;
+ const end = i + ext.len;
+ if (end == path.len or boundary(path[end]) or path[end] == ':') return true;
+ }
+ }
+ return false;
+}
+pub fn validRoot(root: []const u8) bool {
+ if (root.len <= 1) return false;
+ const drive = root.len >= 3 and std.ascii.isAlphabetic(root[0]) and root[1] == ':' and separator(root[2]);
+ const unc = std.mem.startsWith(u8, root, "\\\\");
+ if (!std.fs.path.isAbsolutePosix(root) and !drive and !unc) return false;
+ var parts = std.mem.tokenizeAny(u8, if (drive) root[3..] else root, "/\\");
+ var names: usize = 0;
+ while (parts.next()) |part| {
+ if (!std.mem.eql(u8, part, ".") and !std.mem.eql(u8, part, "..")) names += 1;
+ }
+ return names >= @as(usize, if (unc) 2 else 1);
+}
+fn textLeak(bytes: []const u8, root: []const u8) bool {
+ if (validRoot(root) and std.mem.indexOf(u8, bytes, root) != null) return true;
+ for (0..bytes.len) |i| {
+ if (!absoluteAt(bytes, i)) continue;
+ const quoted = i == 0 or bytes[i - 1] == 0 or bytes[i - 1] == '"' or bytes[i - 1] == '\'';
+ var end = i;
+ while (end < bytes.len and end - i < 32768 and bytes[end] >= ' ' and bytes[end] != 127 and bytes[end] != '"' and bytes[end] != '\'') : (end += 1) {
+ if (!quoted and bytes[end] == ' ') break;
+ }
+ const path = bytes[i..end];
+ if (std.unicode.utf8ValidateSlice(path) and privatePath(path)) return true;
+ }
+ return false;
+}
+fn rawLeak(a: std.mem.Allocator, bytes: []const u8, root: []const u8) !bool {
+ if (textLeak(bytes, root)) return true;
+ if (validRoot(root)) {
+ const wide = try std.unicode.utf8ToUtf16LeAlloc(a, root);
+ defer a.free(wide);
+ if (std.mem.indexOf(u8, bytes, std.mem.sliceAsBytes(wide)) != null) return true;
+ for (wide) |*unit| unit.* = @byteSwap(unit.*);
+ if (std.mem.indexOf(u8, bytes, std.mem.sliceAsBytes(wide)) != null) return true;
+ }
+ for ([_]usize{ 0, 1 }) |offset| {
+ for ([_]bool{ false, true }) |big| {
+ var text: std.ArrayList(u8) = .empty;
+ defer text.deinit(a);
+ var i = offset;
+ while (i + 1 < bytes.len) : (i += 2) {
+ const low = bytes[i + @intFromBool(big)];
+ const high = bytes[i + @intFromBool(!big)];
+ try text.append(a, if (high == 0) low else '?');
+ }
+ if (textLeak(text.items, root)) return true;
+ }
+ }
+ return false;
+}
+fn read(comptime T: type, bytes: []const u8, at: usize) T {
+ return std.mem.readInt(T, bytes[at..][0..@sizeOf(T)], .little);
+}
+fn uncompressedElf(bytes: []const u8) !void {
+ if (bytes.len < 4 or !std.mem.eql(u8, bytes[0..4], "\x7fELF")) return;
+ if (bytes.len < 64 or bytes[4] != 2 or bytes[5] != 1) return error.UnsupportedElf;
+ const at = std.math.cast(usize, read(u64, bytes, 40)) orelse return error.BadElf;
+ const size: usize = read(u16, bytes, 58);
+ const count: usize = read(u16, bytes, 60);
+ if (count == 0) {
+ if (at != 0) return error.UnsupportedExtendedElf;
+ return;
+ }
+ const end = std.math.add(usize, at, count * size) catch return error.BadElf;
+ if (size < 64 or end > bytes.len) return error.BadElf;
+ const strings: usize = read(u16, bytes, 62);
+ if (strings >= count) return error.BadElf;
+ var names: []const u8 = &.{};
+ if (strings != 0) {
+ const off = std.math.cast(usize, read(u64, bytes, at + strings * size + 24)) orelse return error.BadElf;
+ const len = std.math.cast(usize, read(u64, bytes, at + strings * size + 32)) orelse return error.BadElf;
+ const tail = std.math.add(usize, off, len) catch return error.BadElf;
+ if (tail > bytes.len) return error.BadElf;
+ names = bytes[off..tail];
+ }
+ for (0..count) |i| {
+ const h = at + i * size;
+ if (read(u64, bytes, h + 8) & 0x800 != 0) return error.UnsupportedCompressedSection;
+ if (names.len > 0) {
+ const n: usize = read(u32, bytes, h);
+ if (n >= names.len) return error.BadElf;
+ if (std.mem.startsWith(u8, std.mem.sliceTo(names[n..], 0), ".zdebug_")) return error.UnsupportedCompressedSection;
+ }
+ }
+}
+fn inspectContainer(a: std.mem.Allocator, bytes: []const u8, root: []const u8) !void {
+ const head: usize = read(u16, bytes, 6);
+ var at = head;
+ while (at + 48 <= bytes.len) {
+ const h: usize = read(u32, bytes, at + 4);
+ const size = std.math.cast(usize, read(u64, bytes, at + 8)) orelse return error.BadFatbin;
+ const start = std.math.add(usize, at, h) catch return error.BadFatbin;
+ const end = std.math.add(usize, start, size) catch return error.BadFatbin;
+ if (h < 48 or end > bytes.len) return error.BadFatbin;
+ const payload = bytes[start..end];
+ if (read(u16, bytes, at) != 2 and (read(u64, bytes, at + 40) & 0x2000 != 0 or std.mem.startsWith(u8, payload, "\x28\xb5\x2f\xfd"))) return error.UnsupportedImageEncoding;
+ at = end;
+ }
+ if (bytes.len - at > 7 or !std.mem.allEqual(u8, bytes[at..], 0)) return error.BadFatbin;
+ const images = try cubin.images(a, bytes);
+ defer if (comptime @hasDecl(cubin, "freeImages")) cubin.freeImages(a, images) else a.free(images);
+ for (images) |image| {
+ if (!std.mem.startsWith(u8, image.elf, "\x7fELF")) return error.UnsupportedImageEncoding;
+ try uncompressedElf(image.elf);
+ if (try rawLeak(a, image.elf, root)) return error.PrivateBuildPath;
+ }
+}
+pub fn check(a: std.mem.Allocator, bytes: []const u8, root: []const u8) !void {
+ if (try rawLeak(a, bytes, root)) return error.PrivateBuildPath;
+ try uncompressedElf(bytes);
+ var at: usize = 0;
+ while (std.mem.indexOfPos(u8, bytes, at, magic)) |start| {
+ at = start + 4;
+ if (bytes.len - start < 16) {
+ if (start == 0) return error.BadFatbin;
+ continue;
+ }
+ const h: usize = read(u16, bytes, start + 6);
+ const size = std.math.cast(usize, read(u64, bytes, start + 8)) orelse return error.BadFatbin;
+ const body = std.math.add(usize, start, h) catch return error.BadFatbin;
+ const end = std.math.add(usize, body, size) catch return error.BadFatbin;
+ if (h < 16 or end > bytes.len) {
+ if (start == 0) return error.BadFatbin;
+ continue;
+ }
+ try inspectContainer(a, bytes[start..end], root);
+ at = end;
+ }
+}
diff --git a/zig/build/release_paths_test.zig b/zig/build/release_paths_test.zig
new file mode 100644
index 0000000000..4b51b340b2
--- /dev/null
+++ b/zig/build/release_paths_test.zig
@@ -0,0 +1,198 @@
+//! Planted source paths must fail even inside compressed CUDA payloads.
+const std = @import("std");
+const paths = @import("release_paths.zig");
+const cubin = @import("contract").cubin;
+const a = std.testing.allocator;
+const root = "/opt/private-build-root";
+
+test "release paths: build roots and absolute source paths refuse without rejecting runtime paths or URLs" {
+ for ([_][]const u8{ "/opt/private-build-root/data.bin", "/home/sample/repo/zig/kernels/cuda/part", "/some/location/source.cu", "D:\\work\\source.cuh", "/ci/zig-cache/o/hash/object" }) |text|
+ try std.testing.expectError(error.PrivateBuildPath, paths.check(a, text, root));
+ for ([_][]const u8{ "/dev/urandom", "/proc/self/maps", "/v1/chat/completions", "https://example.org/repo/zig/src/main.zig", "zig/kernels/cuda/part.cu", "\x00T:\\B\xa9\xe0\x00" }) |text|
+ try paths.check(a, text, root);
+}
+
+test "release paths: UTF16 roots and foreign home paths refuse at either byte alignment" {
+ for ([_][]const u8{ "/opt/例/private/data.bin", "/home/sample/source.cu" }) |text| {
+ const wide = try std.unicode.utf8ToUtf16LeAlloc(a, text);
+ defer a.free(wide);
+ for (0..2) |_| {
+ var bytes: std.ArrayList(u8) = .empty;
+ defer bytes.deinit(a);
+ try bytes.append(a, 0xff);
+ try bytes.appendSlice(a, std.mem.sliceAsBytes(wide));
+ try std.testing.expectError(error.PrivateBuildPath, paths.check(a, bytes.items, "/opt/例/private"));
+ for (wide) |*unit| unit.* = @byteSwap(unit.*);
+ }
+ }
+}
+fn elf(text: []const u8) ![]u8 {
+ const bytes = try a.alloc(u8, 64 + text.len);
+ @memset(bytes, 0);
+ @memcpy(bytes[0..4], "\x7fELF");
+ bytes[4] = 2;
+ bytes[5] = 1;
+ @memcpy(bytes[64..], text);
+ return bytes;
+}
+fn compressedFatbin(text: []const u8) ![]u8 {
+ const plain = try elf(text);
+ defer a.free(plain);
+ var frame: std.ArrayList(u8) = .empty;
+ defer frame.deinit(a);
+ try frame.appendSlice(a, "\x28\xb5\x2f\xfd\xa0");
+ var length: [4]u8 = undefined;
+ std.mem.writeInt(u32, &length, @intCast(plain.len), .little);
+ try frame.appendSlice(a, &length);
+ for (plain, 0..) |byte, i| {
+ var head: [3]u8 = undefined;
+ std.mem.writeInt(u24, &head, 8 | @as(u24, @intFromBool(i + 1 == plain.len)), .little);
+ try frame.appendSlice(a, &head);
+ try frame.append(a, byte);
+ }
+ const bytes = try a.alloc(u8, 80 + frame.items.len);
+ @memset(bytes, 0);
+ std.mem.writeInt(u32, bytes[0..4], 0xba55ed50, .little);
+ std.mem.writeInt(u16, bytes[4..6], 1, .little);
+ std.mem.writeInt(u16, bytes[6..8], 16, .little);
+ std.mem.writeInt(u64, bytes[8..16], bytes.len - 16, .little);
+ std.mem.writeInt(u16, bytes[16..18], 2, .little);
+ std.mem.writeInt(u32, bytes[20..24], 64, .little);
+ std.mem.writeInt(u64, bytes[24..32], frame.items.len, .little);
+ std.mem.writeInt(u32, bytes[44..48], 121, .little);
+ @memcpy(bytes[80..], frame.items);
+ return bytes;
+}
+
+test "release paths: compressed CUDA images are inspected standalone and inside the packaged executable" {
+ const bad = try compressedFatbin("/opt/private-build-root/zig/kernels/cuda/unit.cu");
+ defer a.free(bad);
+ try std.testing.expect(std.mem.indexOf(u8, bad, root) == null);
+ const refusal = if (@hasDecl(cubin, "freeImages")) error.PrivateBuildPath else error.UnsupportedImageEncoding;
+ try std.testing.expectError(refusal, paths.check(a, bad, root));
+ var binary: std.ArrayList(u8) = .empty;
+ defer binary.deinit(a);
+ const head = try elf("");
+ defer a.free(head);
+ try binary.appendSlice(a, head);
+ try binary.appendSlice(a, bad);
+ try std.testing.expectError(refusal, paths.check(a, binary.items, root));
+ const clean = try compressedFatbin("zig/kernels/cuda/unit.cu");
+ defer a.free(clean);
+ if (@hasDecl(cubin, "freeImages")) try paths.check(a, clean, root) else try std.testing.expectError(error.UnsupportedImageEncoding, paths.check(a, clean, root));
+}
+
+test "release paths: malformed containers and unauditable compressed ELF sections fail closed" {
+ var bad: [16]u8 = @splat(0);
+ @memcpy(bad[0..4], "\x50\xed\x55\xba");
+ try std.testing.expectError(error.BadFatbin, paths.check(a, &bad, root));
+ const object = try elf(&@as([64]u8, @splat(0)));
+ defer a.free(object);
+ std.mem.writeInt(u64, object[40..48], 64, .little);
+ std.mem.writeInt(u16, object[58..60], 64, .little);
+ std.mem.writeInt(u16, object[60..62], 1, .little);
+ std.mem.writeInt(u64, object[72..80], 0x800, .little);
+ try std.testing.expectError(error.UnsupportedCompressedSection, paths.check(a, object, root));
+}
+
+fn stringTableElf(text: []const u8) ![]u8 {
+ const names = "\x00.shstrtab\x00.strtab\x00";
+ const names_at = 64;
+ const text_at = names_at + names.len;
+ const table_at = std.mem.alignForward(usize, text_at + text.len + 2, 8);
+ const bytes = try a.alloc(u8, table_at + 3 * 64);
+ @memset(bytes, 0);
+ @memcpy(bytes[0..4], "\x7fELF");
+ bytes[4] = 2;
+ bytes[5] = 1;
+ bytes[6] = 1;
+ std.mem.writeInt(u16, bytes[16..18], 1, .little);
+ std.mem.writeInt(u16, bytes[18..20], 190, .little);
+ std.mem.writeInt(u32, bytes[20..24], 1, .little);
+ std.mem.writeInt(u16, bytes[52..54], 64, .little);
+ std.mem.writeInt(u64, bytes[40..48], table_at, .little);
+ std.mem.writeInt(u16, bytes[58..60], 64, .little);
+ std.mem.writeInt(u16, bytes[60..62], 3, .little);
+ std.mem.writeInt(u16, bytes[62..64], 1, .little);
+ @memcpy(bytes[names_at..][0..names.len], names);
+ @memcpy(bytes[text_at + 1 ..][0..text.len], text);
+ for ([_]usize{ 1, 2 }, [_]usize{ 1, 11 }, [_]usize{ names_at, text_at }, [_]usize{ names.len, text.len + 2 }) |index, name, offset, size| {
+ const h = table_at + index * 64;
+ std.mem.writeInt(u32, bytes[h..][0..4], @intCast(name), .little);
+ std.mem.writeInt(u32, bytes[h + 4 ..][0..4], 3, .little);
+ std.mem.writeInt(u64, bytes[h + 24 ..][0..8], offset, .little);
+ std.mem.writeInt(u64, bytes[h + 32 ..][0..8], size, .little);
+ }
+ return bytes;
+}
+
+test "release paths: short drive-like machine-code bytes are not a text path" {
+ const noise = "\x01r:\\&\x02";
+ try paths.check(a, noise, root);
+ const container = try compressedFatbin(noise);
+ defer a.free(container);
+ if (@hasDecl(cubin, "freeImages")) try paths.check(a, container, root) else try std.testing.expectError(error.UnsupportedImageEncoding, paths.check(a, container, root));
+}
+
+test "release paths: Windows source paths in ELF string tables still refuse" {
+ const clean = try stringTableElf("relative/source.cu");
+ defer a.free(clean);
+ try paths.check(a, clean, root);
+ const object = try stringTableElf("C:\\Users\\x\\build\\y.cu");
+ defer a.free(object);
+ try std.testing.expectError(error.PrivateBuildPath, paths.check(a, object, root));
+ for ([_][]const u8{ "C:\\Users\\x", "C:/ci/workspace/unit.cubin" }) |text|
+ try std.testing.expectError(error.PrivateBuildPath, paths.check(a, text, root));
+}
+
+test "release paths: single-component drive source paths fall through in raw ELF and UTF16 text" {
+ for ([_][]const u8{ "C:\\main.cu", "C:/main.cu", "D:\\private.h", "E:/source.zig" }) |text| {
+ try std.testing.expectError(error.PrivateBuildPath, paths.check(a, text, root));
+ const object = try stringTableElf(text);
+ defer a.free(object);
+ try std.testing.expectError(error.PrivateBuildPath, paths.check(a, object, root));
+ const wide = try std.unicode.utf8ToUtf16LeAlloc(a, text);
+ defer a.free(wide);
+ for (0..2) |_| {
+ for (0..2) |alignment| {
+ var bytes: std.ArrayList(u8) = .empty;
+ defer bytes.deinit(a);
+ if (alignment == 1) try bytes.append(a, 0xff);
+ try bytes.appendSlice(a, std.mem.sliceAsBytes(wide));
+ try std.testing.expectError(error.PrivateBuildPath, paths.check(a, bytes.items, root));
+ }
+ for (wide) |*unit| unit.* = @byteSwap(unit.*);
+ }
+ }
+}
+
+test "release paths: relative root text cannot classify instruction bytes as a private path" {
+ const noise = "\x01./.\x02";
+ try paths.check(a, noise, "./.");
+ const container = try compressedFatbin(noise);
+ defer a.free(container);
+ try paths.check(a, container, "./.");
+ const wide = try std.unicode.utf8ToUtf16LeAlloc(a, "./.");
+ defer a.free(wide);
+ for (0..2) |_| {
+ for (0..2) |alignment| {
+ var bytes: std.ArrayList(u8) = .empty;
+ defer bytes.deinit(a);
+ if (alignment == 1) try bytes.append(a, 0xff);
+ try bytes.appendSlice(a, std.mem.sliceAsBytes(wide));
+ try paths.check(a, bytes.items, "./.");
+ }
+ for (wide) |*unit| unit.* = @byteSwap(unit.*);
+ }
+}
+
+test "release paths: a named absolute build root still refuses in an ELF string table" {
+ const object = try stringTableElf(root ++ "/assets/data.bin");
+ defer a.free(object);
+ try std.testing.expectError(error.PrivateBuildPath, paths.check(a, object, root));
+}
+
+test "release paths: a bare root has no named build directory text" {
+ for ([_][]const u8{ "//", "C:\\", "/." }, [_][]const u8{ "\x01//\x02", "\x01C:\\&\x02", "\x01/.\x02" }) |base, noise|
+ try paths.check(a, noise, base);
+}
diff --git a/zig/build/release_version.zig b/zig/build/release_version.zig
new file mode 100644
index 0000000000..be40c87c01
--- /dev/null
+++ b/zig/build/release_version.zig
@@ -0,0 +1,10 @@
+//! The source manifest and newest release notes must name the same version.
+const std = @import("std");
+pub fn tests(b: *std.Build, all: *std.Build.Step) void {
+ const root = b.createModule(.{ .root_source_file = b.path("zig/build/release_version_test.zig"), .target = b.graph.host });
+ root.addAnonymousImport("release_manifest", .{ .root_source_file = b.path("build.zig.zon") });
+ root.addAnonymousImport("release_changelog", .{ .root_source_file = b.path("CHANGELOG.md") });
+ const run = b.addRunArtifact(b.addTest(.{ .root_module = root }));
+ b.step("test-release-version-host", "Source package version matches the newest changelog release").dependOn(&run.step);
+ all.dependOn(&run.step);
+}
diff --git a/zig/build/release_version_test.zig b/zig/build/release_version_test.zig
new file mode 100644
index 0000000000..6ea2702c81
--- /dev/null
+++ b/zig/build/release_version_test.zig
@@ -0,0 +1,24 @@
+//! Compare actual source metadata instead of a second hardcoded release string.
+const std = @import("std");
+test "source version matches the newest changelog release heading" {
+ const a = std.testing.allocator;
+ var arena: std.heap.ArenaAllocator = .init(a);
+ defer arena.deinit();
+ var diagnostic: std.zon.parse.Diagnostics = undefined;
+ const manifest = try std.zon.parse.fromSlice(struct { version: []const u8 }, .{ .gpa = a, .arena = arena.allocator(), .source = @embedFile("release_manifest"), .diagnostics = &diagnostic, .ignore_unknown_fields = true });
+ _ = try std.SemanticVersion.parse(manifest.version);
+ var newest: ?std.SemanticVersion = null;
+ var heading: []const u8 = "";
+ var lines = std.mem.splitScalar(u8, @embedFile("release_changelog"), '\n');
+ while (lines.next()) |line| {
+ if (!std.mem.startsWith(u8, line, "## ")) continue;
+ const text = std.mem.trim(u8, line[3..], " \t\r");
+ const version = std.SemanticVersion.parse(text) catch continue;
+ if (newest == null or version.order(newest.?) == .gt) {
+ newest = version;
+ heading = text;
+ }
+ }
+ try std.testing.expect(newest != null);
+ try std.testing.expectEqualStrings(heading, manifest.version);
+}
diff --git a/zig/build/rotary_sweep.zig b/zig/build/rotary_sweep.zig
new file mode 100644
index 0000000000..aa27b243d7
--- /dev/null
+++ b/zig/build/rotary_sweep.zig
@@ -0,0 +1,20 @@
+//! The runtime-compiled sweep uses embedded source and no offline shader producer.
+const std = @import("std");
+pub fn steps(b: *std.Build, target: std.Build.ResolvedTarget, metal: *std.Build.Module) void {
+ const sources = b.createModule(.{ .root_source_file = b.path("zig/kernels/metal/sources.zig") });
+ const family = b.createModule(.{ .root_source_file = b.path("zig/flashnext_bound_family.zig"), .target = target, .imports = &.{.{ .name = "kernel_sources", .module = sources }} });
+ const exe = b.addExecutable(.{ .name = "tf-rotary-sweep", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/tests/contract/rotary_sweep_main.zig"),
+ .target = target,
+ .optimize = .safe,
+ .link_libc = true,
+ .imports = &.{
+ .{ .name = "metal", .module = metal },
+ .{ .name = "tensorfold", .module = family },
+ .{ .name = "kernel_sources", .module = sources },
+ .{ .name = "contract", .module = @import("kernel_contract.zig").module(b, target, .safe) },
+ },
+ }) });
+ b.step("tf-rotary-sweep", "Build the exhaustive rotary sweep without offline shader compilation; no run").dependOn(&b.addInstallArtifact(exe, .{}).step);
+ b.step("compile-rotary-sweep-host", "Compile-analysis of the real sweep without device execution").dependOn(&exe.step);
+}
diff --git a/zig/build/runtime_libraries.zig b/zig/build/runtime_libraries.zig
new file mode 100644
index 0000000000..8b23fe3dec
--- /dev/null
+++ b/zig/build/runtime_libraries.zig
@@ -0,0 +1,38 @@
+//! Optional runtime refusal and discovery regressions with absent files and a fake loader.
+const std = @import("std");
+pub fn tests(b: *std.Build, all: *std.Build.Step) void {
+ const experimental = b.createModule(.{ .root_source_file = b.path("zig/experimental_family_test.zig"), .target = b.graph.host });
+ const policy = b.addRunArtifact(b.addTest(.{ .root_module = experimental }));
+ b.step("test-experimental-family-host", "Exact release opt-in policy without model or GPU work").dependOn(&policy.step);
+ all.dependOn(&policy.step);
+ const fixture_module = b.createModule(.{ .target = b.graph.host, .link_libc = true, .link_libcpp = true });
+ fixture_module.addCSourceFile(.{ .file = b.path("zig/tests/runtime_discovery_mock.cpp"), .flags = &.{"-std=c++17"} });
+ const fixture_library = b.addLibrary(.{ .name = "runtime-discovery-mock", .linkage = .dynamic, .root_module = fixture_module });
+ const fixture = b.addOptions();
+ fixture.addOptionPath("library", fixture_library.getEmittedBin());
+ const exit_module = b.createModule(.{ .root_source_file = b.path("zig/runtime_discovery_exit.zig"), .target = b.graph.host, .link_libc = true });
+ exit_module.addOptions("discovery_fixture", fixture);
+ const exit_run = b.addRunArtifact(b.addExecutable(.{ .name = "runtime-discovery-exit", .root_module = exit_module }));
+ exit_run.has_side_effects = true;
+ b.step("test-runtime-discovery-exit-host", "Discovery preserves loaded exit callbacks in separate host processes").dependOn(&exit_run.step);
+ all.dependOn(&exit_run.step);
+ const root = b.createModule(.{ .root_source_file = b.path("zig/runtime_library_host_test.zig"), .target = b.graph.host, .link_libc = true });
+ const fixtures = b.addOptions();
+ for ([_][]const u8{ "complete", "abi", "wait", "store", "wrong" }) |kind| {
+ const m = b.createModule(.{ .target = b.graph.host, .link_libc = true });
+ m.addCSourceFile(.{ .file = b.path("zig/tests/rpc_library_mock.c"), .flags = &.{b.fmt("-DMOCK_{s}=1", .{kind})} });
+ const lib = b.addLibrary(.{ .name = b.fmt("rpc-mock-{s}", .{kind}), .linkage = .dynamic, .root_module = m });
+ fixtures.addOptionPath(kind, lib.getEmittedBin());
+ }
+ root.addOptions("rpc_fixtures", fixtures);
+ const run = b.addRunArtifact(b.addTest(.{ .root_module = root, .filters = &.{"runtime library:"} }));
+ const step = b.step("test-runtime-libraries-host", "Optional library refusals and discovery without a GPU");
+ step.dependOn(&run.step);
+ step.dependOn(&exit_run.step);
+ const warnings = b.createModule(.{ .root_source_file = b.path("zig/rpc_warning_check.zig"), .target = b.graph.host, .link_libc = true });
+ warnings.addOptions("rpc_fixtures", fixtures);
+ const check = b.addRunArtifact(b.addExecutable(.{ .name = "rpc-warning-check", .root_module = warnings }));
+ step.dependOn(&check.step);
+ all.dependOn(&check.step);
+ all.dependOn(&run.step);
+}
diff --git a/zig/build/save_guard.zig b/zig/build/save_guard.zig
new file mode 100644
index 0000000000..dc163d892c
--- /dev/null
+++ b/zig/build/save_guard.zig
@@ -0,0 +1,40 @@
+//! One save-state admission module for every build consumer; shared source files have a single module owner.
+
+const std = @import("std");
+
+pub fn wire(b: *std.Build) void {
+ var seen_steps: std.AutoHashMapUnmanaged(*std.Build.Step, void) = .empty;
+ var seen_mods: std.AutoHashMapUnmanaged(*std.Build.Module, void) = .empty;
+ var per_target: std.StringHashMapUnmanaged(*std.Build.Module) = .empty;
+ var w: Walk = .{ .b = b, .seen_steps = &seen_steps, .seen_mods = &seen_mods, .per_target = &per_target };
+ for (b.top_level_steps.values()) |top| w.step(&top.step);
+}
+
+const Walk = struct {
+ b: *std.Build,
+ seen_steps: *std.AutoHashMapUnmanaged(*std.Build.Step, void),
+ seen_mods: *std.AutoHashMapUnmanaged(*std.Build.Module, void),
+ per_target: *std.StringHashMapUnmanaged(*std.Build.Module),
+
+ fn step(w: *Walk, s: *std.Build.Step) void {
+ if ((w.seen_steps.getOrPut(w.b.allocator, s) catch @panic("OOM")).found_existing) return;
+ if (s.cast(std.Build.Step.Compile)) |compile| w.module(compile.root_module, compile.root_module.resolved_target);
+ for (s.dependencies.items) |dep| w.step(dep);
+ }
+
+ fn module(w: *Walk, m: *std.Build.Module, inherited: ?std.Build.ResolvedTarget) void {
+ if ((w.seen_mods.getOrPut(w.b.allocator, m) catch @panic("OOM")).found_existing) return;
+ const target = m.resolved_target orelse inherited orelse w.b.graph.host;
+ // only TensorFold's own sources see it; the sys module itself and generated modules do not
+ if (m.root_source_file) |root| if (root == .src_path and !m.import_table.contains("save_guard") and !std.mem.endsWith(u8, root.src_path.sub_path, "core/save_guard.zig"))
+ m.addImport("save_guard", w.guard(target));
+ for (m.import_table.values()) |dep| w.module(dep, target);
+ }
+
+ fn guard(w: *Walk, target: std.Build.ResolvedTarget) *std.Build.Module {
+ const key = target.result.zigTriple(w.b.allocator) catch @panic("OOM");
+ const slot = w.per_target.getOrPut(w.b.allocator, key) catch @panic("OOM");
+ if (!slot.found_existing) slot.value_ptr.* = w.b.createModule(.{ .root_source_file = w.b.path("zig/src/core/save_guard.zig"), .target = target, .link_libc = true });
+ return slot.value_ptr.*;
+ }
+};
diff --git a/zig/build/server.zig b/zig/build/server.zig
index f0fa1052fa..48ad8ace3e 100644
--- a/zig/build/server.zig
+++ b/zig/build/server.zig
@@ -10,15 +10,18 @@ pub fn json(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.built
});
}
-/// Attach a server's shared JSON module; its tests run separately from the server's root.
+/// Attach a server's shared JSON module (tests run apart), and Living Weights' families where the build gave none.
pub fn addTo(
b: *std.Build,
m: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
) *std.Build.Module {
+ if (!m.import_table.contains("living")) m.addImport("living", b.createModule(.{ .root_source_file = b.path("zig/src/living/living.zig"), .target = target, .optimize = optimize, .link_libc = true }));
const j = json(b, target, optimize);
m.addImport("json", j);
if (m.import_table.get("engine_api")) |api| if (api.import_table.get("lanes")) |lanes| m.addImport("lanes", lanes);
+ // a build whose engines learn hands the server their own living module; the rest read only its families
+ if (m.import_table.get("living") == null) m.addImport("living", b.createModule(.{ .root_source_file = b.path("zig/src/living/living.zig"), .target = target, .optimize = optimize, .link_libc = true }));
return j;
}
diff --git a/zig/build/sessions.zig b/zig/build/sessions.zig
new file mode 100644
index 0000000000..d220b27ac3
--- /dev/null
+++ b/zig/build/sessions.zig
@@ -0,0 +1,28 @@
+//! Session memory: portable bookkeeping tests everywhere, NVMe tier tests on Linux.
+
+const std = @import("std");
+
+pub fn module(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Module {
+ return b.createModule(.{ .root_source_file = b.path("zig/src/sessions/sessions.zig"), .target = target, .optimize = optimize, .link_libc = true });
+}
+
+pub fn add(b: *std.Build, test_step: *std.Build.Step) void {
+ const host = b.graph.host;
+ const tests = if (host.result.os.tag == .linux) module(b, host, .debug) else b.createModule(.{
+ .root_source_file = b.path("zig/src/sessions/portable_test.zig"),
+ .target = host,
+ .optimize = .debug,
+ .link_libc = true,
+ });
+ if (host.result.os.tag != .linux) {
+ const linux = b.resolveTargetQuery(.{ .cpu_arch = host.result.cpu.arch, .os_tag = .linux, .abi = .gnu });
+ const full = b.addTest(.{ .root_module = module(b, linux, .debug) });
+ b.step("check-sessions-linux", "Compile the full session/NVMe tests without executing Linux code")
+ .dependOn(&full.step);
+ }
+ b.step("test-review-sessions", "Ownership rollback: actual RAM Store failure regressions").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = tests, .filters = &.{"Ownership rollback:"} })).step);
+ b.step("test-residency-review", "Residency rollback: rollback after committed disk policy side effects").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = tests, .filters = &.{"Residency rollback:"} })).step);
+ const run = b.addRunArtifact(b.addTest(.{ .root_module = tests }));
+ b.step("test-sessions", "Session memory's host tests: pool, prefix trie, RAM tier, NVMe tier on Linux (no GPU)").dependOn(&run.step);
+ test_step.dependOn(&run.step);
+}
diff --git a/zig/build/tp.zig b/zig/build/tp.zig
new file mode 100644
index 0000000000..6419f55b70
--- /dev/null
+++ b/zig/build/tp.zig
@@ -0,0 +1,73 @@
+//! Tensor parallelism's build: Linux host tests, and on Linux the `tf-tp-test` GPU runner with the mailbox kernel.
+
+const std = @import("std");
+
+/// The CUDA runtime without the engine's kernels (tp only needs the driver, memory, streams and NCCL).
+fn runtime(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Module {
+ const options = b.addOptions();
+ options.addOption(bool, "with_kernels", false);
+ const cuda = b.createModule(.{ .root_source_file = b.path("zig/src/cuda/root.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ cuda.addImport("gpu_memory", @import("gpu_memory.zig").module(b, target));
+ cuda.addOptions("kernel_options", options);
+ cuda.addImport("stagger", b.createModule(.{ .root_source_file = b.path("zig/src/core/stagger.zig"), .target = target, .optimize = optimize }));
+ return cuda;
+}
+
+/// The `tp` module over `cuda` (an engine passes its own runtime module); verbs (zig/src/fabric/verbs.zig) for RoCE.
+pub fn module(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, cuda: *std.Build.Module) *std.Build.Module {
+ const m = b.createModule(.{ .root_source_file = b.path("zig/src/fabric/tp/tp.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ m.addImport("cuda", cuda);
+ m.addImport("verbs", b.createModule(.{ .root_source_file = b.path("zig/src/fabric/verbs.zig"), .target = target, .optimize = optimize, .link_libc = true }));
+ return m;
+}
+
+/// mailbox.cu as a fatbin: sm_120 (RTX PRO 6000), sm_121 (GB10), and compute_90 PTX for anything else.
+fn mailboxFatbin(b: *std.Build, nvcc: []const u8) std.Build.LazyPath {
+ const run = b.addSystemCommand(&.{ nvcc, "-fatbin", "-O3", "-std=c++17", "-gencode=arch=compute_120,code=sm_120", "-gencode=arch=compute_121,code=sm_121", "-gencode=arch=compute_90,code=compute_90", "-o" });
+ const out = run.addOutputFileArg("tp_mailbox.fatbin");
+ run.addFileArg(b.path("zig/src/fabric/tp/mailbox.cu"));
+ return out;
+}
+
+/// `pub const mailbox: []const u8`, the fatbin's bytes or empty.
+fn kernelModule(b: *std.Build, image: ?std.Build.LazyPath) *std.Build.Module {
+ const wf = b.addWriteFiles();
+ if (image) |file| {
+ _ = wf.addCopyFile(file, "tp_mailbox.fatbin");
+ // the driver takes 8-byte aligned images (Module.load refuses others); @embedFile alone promises nothing
+ return b.createModule(.{ .root_source_file = wf.add("tp_kernel.zig", "const bytes align(16) = @embedFile(\"tp_mailbox.fatbin\").*;\npub const mailbox: []const u8 = &bytes;\n") });
+ }
+ return b.createModule(.{ .root_source_file = wf.add("tp_kernel.zig", "pub const mailbox: []const u8 = \"\";\n") });
+}
+
+pub fn add(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, test_step: *std.Build.Step) void {
+ const host = b.graph.host;
+ const portable = b.createModule(.{ .root_source_file = b.path("zig/src/fabric/tp/portable_test.zig"), .target = host, .optimize = .debug, .link_libc = true, .imports = &.{.{ .name = "cuda", .module = runtime(b, host, .debug) }} });
+ const tests = b.addRunArtifact(b.addTest(.{ .root_module = if (host.result.os.tag == .linux)
+ module(b, host, .debug, runtime(b, host, .debug))
+ else
+ portable }));
+ b.step("test-tp", "TP bootstrap, failure, settings and in-process transport tests").dependOn(&tests.step);
+ test_step.dependOn(&tests.step);
+ const linux = b.resolveTargetQuery(.{ .cpu_arch = host.result.cpu.arch, .os_tag = .linux, .abi = .gnu });
+ const check_cuda = runtime(b, linux, .debug);
+ const object = b.addObject(.{ .name = "tp_host_compile", .root_module = b.createModule(.{
+ .root_source_file = b.path("zig/tests/tp_compile.zig"),
+ .target = linux,
+ .link_libc = true,
+ .imports = &.{ .{ .name = "cuda", .module = check_cuda }, .{ .name = "tp", .module = module(b, linux, .debug, check_cuda) } },
+ }) });
+ tests.step.dependOn(&object.step);
+ if (target.result.os.tag != .linux) return;
+ const nvcc = b.option([]const u8, "tp-nvcc", "nvcc that builds the TP mailbox kernel");
+ const prebuilt = b.option([]const u8, "tp-fatbin", "absolute path of a prebuilt tp_mailbox.fatbin to embed");
+ const image: ?std.Build.LazyPath = if (prebuilt) |p| b.graph.cwdRelativePath(p) else if (nvcc) |tool| mailboxFatbin(b, tool) else null;
+ const cuda = runtime(b, target, optimize);
+ const runner = b.createModule(.{ .root_source_file = b.path("zig/src/fabric/tp/gpu_main.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ runner.addImport("cuda", cuda);
+ runner.addImport("tp", module(b, target, optimize, cuda));
+ runner.addImport("tp_kernel", kernelModule(b, image));
+ const exe = b.addExecutable(.{ .name = "tf-tp-test", .root_module = runner });
+ b.installArtifact(exe);
+ b.step("tf-tp-test", "TP=2 on two GPUs: bootstrap, every collective bit-exact, graph capture, fail-fast, latency").dependOn(&b.addInstallArtifact(exe, .{}).step);
+}
diff --git a/zig/build/xpu.zig b/zig/build/xpu.zig
index 1ff9d51a83..714be2873a 100644
--- a/zig/build/xpu.zig
+++ b/zig/build/xpu.zig
@@ -165,6 +165,7 @@ const Modules = struct { xpu: *std.Build.Module, core: *std.Build.Module, nemotr
fn family(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, xpu: *std.Build.Module, lanes: *std.Build.Module) Modules {
const core = b.createModule(.{ .root_source_file = b.path("zig/src/core/root.zig"), .target = target, .optimize = optimize, .link_libc = true });
+ core.addImport("gpu_memory", @import("gpu_memory.zig").module(b, target));
const nemotron = b.createModule(.{ .root_source_file = b.path("zig/src/families/nemotron/xpu.zig"), .target = target, .optimize = optimize, .link_libc = true });
nemotron.addImport("xpu", xpu);
nemotron.addImport("core", core);
diff --git a/zig/contract_bounds_host_test.zig b/zig/contract_bounds_host_test.zig
new file mode 100644
index 0000000000..65d1ec82ab
--- /dev/null
+++ b/zig/contract_bounds_host_test.zig
@@ -0,0 +1,103 @@
+//! The actual shape and sum gates must apply a probe's derived absolute allowance.
+const std = @import("std");
+const con = @import("contract");
+const g = con.gates;
+const c = con.spec;
+const P = struct {
+ output_bytes: [4]u8 = @bitCast(@as(f32, 0.125)),
+ allowance: f64 = 0.125,
+ fn prepare(_: *anyopaque, _: c.Shape, _: g.Fill, _: u64) !bool {
+ return true;
+ }
+ fn run(_: *anyopaque) !void {}
+ fn output(ctx: *anyopaque) ![]const u8 {
+ const p: *P = @ptrCast(@alignCast(ctx));
+ return &p.output_bytes;
+ }
+ fn inputs(_: *anyopaque) ![]const u8 {
+ return &.{};
+ }
+ fn intact(_: *anyopaque) !bool {
+ return true;
+ }
+ fn overlap(_: *anyopaque) bool {
+ return false;
+ }
+ fn count(_: *anyopaque) usize {
+ return 1;
+ }
+ fn reference(_: *anyopaque, want: []f64, mag: []f64) !void {
+ want[0] = 0;
+ mag[0] = 0;
+ }
+ fn simulate(_: *anyopaque, _: c.Accum, _: g.Order, got: []f64) !void {
+ got[0] = 0.125;
+ }
+ fn bounds(ctx: *anyopaque, _: []const f64, _: []const f64, allow: []f64) !void {
+ const p: *P = @ptrCast(@alignCast(ctx));
+ allow[0] = p.allowance;
+ }
+ fn intervals(_: *anyopaque, low: []f64, high: []f64) !void {
+ low[0] = 0.125;
+ high[0] = 0.125;
+ }
+ fn depth(_: c.Shape) usize {
+ return 1;
+ }
+ fn kernel(p: *P, v: *const g.VTable) g.Kernel {
+ return .{ .ctx = p, .vt = v, .spec = .{ .name = "absolute interval", .accum = .f32, .depth = depth, .out = .f32, .served = &.{.{ .rows = 1, .n = 1, .k = 1 }}, .sums = .{ .f16 = 0, .bf16 = 0, .exact = false, .why = "an interval hook control" } } };
+ }
+};
+fn table() g.VTable {
+ var v: g.VTable = .{ .prepare = P.prepare, .run = P.run, .output = P.output, .inputs = P.inputs, .intact = P.intact, .overlap = P.overlap, .count = P.count, .reference = P.reference, .simulate = P.simulate };
+ if (@hasField(g.VTable, "bounds")) v.bounds = P.bounds;
+ return v;
+}
+test "rotary bound: both production gates use the absolute hook and reject finite corruption" {
+ g.quiet = true;
+ defer g.quiet = false;
+ var p: P = .{};
+ const v = table();
+ try std.testing.expectEqual(g.Verdict.pass, try g.shapes(std.testing.allocator, p.kernel(&v), 0));
+ try std.testing.expectEqual(g.Verdict.pass, try g.sums(std.testing.allocator, p.kernel(&v), 0));
+ p.output_bytes = @bitCast(std.math.nan(f32));
+ try std.testing.expectEqual(g.Verdict.fail, try g.shapes(std.testing.allocator, p.kernel(&v), 0));
+ try std.testing.expectEqual(g.Verdict.fail, try g.sums(std.testing.allocator, p.kernel(&v), 0));
+ p.output_bytes = @bitCast(@as(f32, 0.25));
+ try std.testing.expectEqual(g.Verdict.fail, try g.shapes(std.testing.allocator, p.kernel(&v), 0));
+ try std.testing.expectEqual(g.Verdict.fail, try g.sums(std.testing.allocator, p.kernel(&v), 0));
+}
+test "rotary bound: invalid or nonfinite allowances fail closed" {
+ g.quiet = true;
+ defer g.quiet = false;
+ if (!@hasField(g.VTable, "bounds")) return;
+ var p: P = .{};
+ const v = table();
+ for ([_]f64{ -1, std.math.nan(f64), std.math.inf(f64) }) |bad| {
+ p.allowance = bad;
+ try std.testing.expectError(error.InvalidErrorBound, g.shapes(std.testing.allocator, p.kernel(&v), 0));
+ try std.testing.expectError(error.InvalidErrorBound, g.sums(std.testing.allocator, p.kernel(&v), 0));
+ }
+}
+
+test "rotary sweep: the real shape gate rejects a sign flip admitted by a symmetric allowance" {
+ g.quiet = true;
+ defer g.quiet = false;
+ var p: P = .{};
+ var v = table();
+ v.intervals = P.intervals;
+ try std.testing.expectEqual(g.Verdict.pass, try g.shapes(std.testing.allocator, p.kernel(&v), 0));
+ p.output_bytes = @bitCast(@as(f32, -0.125));
+ try std.testing.expectEqual(g.Verdict.fail, try g.shapes(std.testing.allocator, p.kernel(&v), 0));
+}
+
+test "rotary sweep: the real sum gate rejects a sign flip admitted by a symmetric allowance" {
+ g.quiet = true;
+ defer g.quiet = false;
+ var p: P = .{};
+ var v = table();
+ v.intervals = P.intervals;
+ try std.testing.expectEqual(g.Verdict.pass, try g.sums(std.testing.allocator, p.kernel(&v), 0));
+ p.output_bytes = @bitCast(@as(f32, -0.125));
+ try std.testing.expectEqual(g.Verdict.fail, try g.sums(std.testing.allocator, p.kernel(&v), 0));
+}
diff --git a/zig/cuda_release_refusal_env.zig b/zig/cuda_release_refusal_env.zig
new file mode 100644
index 0000000000..14eb357f32
--- /dev/null
+++ b/zig/cuda_release_refusal_env.zig
@@ -0,0 +1,7 @@
+//! The release refusal reads controlled values without changing the process environment.
+const std = @import("std");
+pub const experimental = @import("sys").experimental;
+pub var value: ?[:0]const u8 = null;
+pub fn getenv(name: [*:0]const u8) ?[:0]const u8 {
+ return if (std.mem.eql(u8, std.mem.span(name), "TF_EXPERIMENTAL")) value else null;
+}
diff --git a/zig/cuda_release_refusal_tests.zig b/zig/cuda_release_refusal_tests.zig
new file mode 100644
index 0000000000..112c384449
--- /dev/null
+++ b/zig/cuda_release_refusal_tests.zig
@@ -0,0 +1,27 @@
+//! The actual release admission refuses unavailable DeepSeek backends independently of the environment.
+const std = @import("std");
+const gate = @import("release_refusal");
+const env = @import("policy_env");
+const expected = "DeepSeek-V4.1 is not served by this release's CUDA server; its whole-model gate is #827";
+fn check(value: ?[:0]const u8) !void {
+ env.value = value;
+ defer env.value = null;
+ for ([_][]const u8{ "deepseek_v4", "deepseek_v41" }) |name| {
+ try std.testing.expectEqualStrings(expected, gate.backend(.{ .dir = "not-opened", .model_type = name }) orelse "");
+ try std.testing.expectEqualStrings(expected, gate.backend(.{ .dir = "another-unread-checkpoint", .model_type = name, .policy = "unsupported", .mtp_drafts = 32 }) orelse "");
+ }
+}
+test "DeepSeek release: TF_EXPERIMENTAL unset is a constant refusal before device work" {
+ try check(null);
+}
+test "DeepSeek release: TF_EXPERIMENTAL=1 is the same constant refusal" {
+ try check("1");
+}
+test "DeepSeek release: TF_EXPERIMENTAL=deepseek is the same constant refusal" {
+ try check("deepseek");
+}
+test "DeepSeek release: other CUDA admission stays available" {
+ env.value = "deepseek";
+ defer env.value = null;
+ try std.testing.expect(gate.backend(.{ .dir = "not-opened", .model_type = "nemotron_h" }) == null);
+}
diff --git a/zig/cuda_startup_costs_host.zig b/zig/cuda_startup_costs_host.zig
new file mode 100644
index 0000000000..7d63c3affa
--- /dev/null
+++ b/zig/cuda_startup_costs_host.zig
@@ -0,0 +1,101 @@
+//! Opening publishes actual measurements once and leaves the old configuration intact on failure.
+const std = @import("std");
+const lanes = @import("lanes");
+const startup = @import("src/native/cuda_startup_costs.zig");
+const a = std.testing.allocator;
+
+const Context = struct {
+ bound: bool = false,
+ pub fn makeCurrent(c: *@This()) !void {
+ c.bound = true;
+ }
+};
+const Gpu = struct { ctx: Context = .{} };
+const State = struct {
+ facts: lanes.Model = .{ .exact_width = 64, .batch_rows = 128 },
+ prepare: ?startup.Prepare,
+ pub fn calibrate(s: *@This(), gpa: std.mem.Allocator, cfg: *lanes.Config) !void {
+ try startup.calibrate(s, gpa, cfg);
+ }
+};
+const Probe = struct {
+ gpu: *Gpu,
+ calls: usize = 0,
+ fail: bool = false,
+ costs: [64]lanes.config.Cost = undefined,
+ fn init(gpu: *Gpu) Probe {
+ var p: Probe = .{ .gpu = gpu };
+ for (&p.costs, 1..) |*c, rows| c.* = .{ .width = @intCast(rows), .ms = @as(f64, @floatFromInt(rows)) + 0.25 };
+ return p;
+ }
+ fn run(ctx: *anyopaque) !lanes.drafter.Facts {
+ const p: *Probe = @ptrCast(@alignCast(ctx));
+ try std.testing.expect(p.gpu.ctx.bound);
+ p.calls += 1;
+ if (p.fail) return error.PlantedProbeFailure;
+ return .{ .depth = 15, .step_ms = 1, .window_costs = &p.costs, .lane_costs = &p.costs };
+ }
+};
+
+fn check(gpa: std.mem.Allocator, invalid: bool, fail: bool) !void {
+ var gpu: Gpu = .{};
+ var probe = Probe.init(&gpu);
+ probe.fail = fail;
+ if (invalid) probe.costs[31].width = 31;
+ var state: State = .{ .prepare = .{ .ctx = &probe, .run = Probe.run } };
+ const H = struct { gpu: ?*Gpu, served: *State, gpa: std.mem.Allocator, cfg: lanes.Config };
+ var h: H = .{ .gpu = &gpu, .served = &state, .gpa = gpa, .cfg = try lanes.Config.init(gpa, state.facts, 64, 63) };
+ const cfg = &h.cfg;
+ defer cfg.deinit(gpa);
+ var target: lanes.fake.Fake = .{ .gpa = gpa };
+ defer target.deinit();
+ var timer: lanes.fake.FixedClock = .{};
+ var core = lanes.Engine.init(gpa, cfg, target.backend(), timer.clock());
+ defer core.deinit();
+ var bound: ?*Context = null;
+ try std.testing.expect(cfg.family_costs.empty() and probe.calls == 0);
+ const result = startup.open(&h, &bound);
+ if (fail or invalid) {
+ if (fail) try std.testing.expectError(error.PlantedProbeFailure, result) else try std.testing.expectError(error.InvalidWindowMeasurements, result);
+ try std.testing.expect(cfg.family_costs.empty() and cfg.shared_costs.empty() and cfg.lane_costs.empty());
+ try std.testing.expect(state.prepare != null and state.facts.window_costs.len == 0);
+ return;
+ }
+ result catch |err| {
+ try std.testing.expect(cfg.family_costs.empty() and cfg.shared_costs.empty() and cfg.lane_costs.empty());
+ try std.testing.expect(state.prepare != null);
+ return err;
+ };
+ try std.testing.expect(bound == &gpu.ctx);
+ for (probe.costs) |c| {
+ try std.testing.expectEqual(@as(?f64, c.ms), cfg.family_costs.get(c.width));
+ try std.testing.expectEqual(@as(?f64, c.ms), core.rule.family_costs.get(c.width));
+ try std.testing.expectEqual(@as(?f64, c.ms), cfg.lane_costs.get(c.width));
+ }
+ try std.testing.expectEqual(@as(usize, 64), state.facts.window_costs.len);
+ try startup.open(&h, &bound);
+ try std.testing.expectEqual(@as(usize, 1), probe.calls);
+}
+
+test "eager costs: bind before calibration, publish real widths before core use, and never repeat it" {
+ try check(a, false, false);
+}
+test "eager costs: failures and malformed measurements publish nothing" {
+ try check(a, true, false);
+ try check(a, false, true);
+}
+test "eager costs: allocation failure leaves every old table intact without leaking" {
+ try std.testing.checkAllAllocationFailures(a, check, .{ false, false });
+}
+
+test "eager costs: temporary calibration storage is charged before stream admission" {
+ var probe: u8 = 0;
+ var arena: std.heap.ArenaAllocator = .init(a);
+ defer arena.deinit();
+ var problem: []const u8 = "";
+ const prepare: startup.Prepare = .{ .ctx = &probe, .run = undefined, .bytes = 65 };
+ try std.testing.expectEqual(@as(?u64, 35), try startup.room(arena.allocator(), prepare, 100, &problem));
+ try std.testing.expectEqual(@as(?u64, null), try startup.room(arena.allocator(), prepare, 64, &problem));
+ try std.testing.expect(std.mem.indexOf(u8, problem, "calibration cache") != null);
+ try std.testing.expectEqual(@as(?u64, 100), try startup.room(arena.allocator(), null, 100, &problem));
+}
diff --git a/zig/deepseek_v41_checks.zig b/zig/deepseek_v41_checks.zig
new file mode 100644
index 0000000000..b386856318
--- /dev/null
+++ b/zig/deepseek_v41_checks.zig
@@ -0,0 +1,13 @@
+//! Source-built V4.1 operator qualification; real Metal execution, no checkpoint needed.
+test {
+ _ = @import("src/core/exl3_projection_gpu_test.zig");
+ _ = @import("src/families/deepseek_v41/engram.zig");
+ _ = @import("src/families/deepseek_v41/csa2.zig");
+ _ = @import("src/families/deepseek_v41/ops_check.zig");
+ _ = @import("src/families/deepseek_v41/metal_load.zig");
+ _ = @import("src/families/deepseek_v41/index_precision_test.zig");
+ _ = @import("src/families/deepseek_v41/config.zig");
+ _ = @import("src/families/deepseek_v41/profiler.zig");
+ _ = @import("src/core/gpu_counters.zig");
+ _ = @import("src/core/expert_groups.zig");
+}
diff --git a/zig/deepseek_v41_reference_checks.zig b/zig/deepseek_v41_reference_checks.zig
new file mode 100644
index 0000000000..cc6d35f278
--- /dev/null
+++ b/zig/deepseek_v41_reference_checks.zig
@@ -0,0 +1,9 @@
+//! Independent CPU arithmetic boundaries used by the resident-checkpoint teacher oracle.
+test {
+ _ = @import("tests/deepseek_v41_reference/projection.zig");
+ _ = @import("tests/deepseek_v41_reference/math.zig");
+ _ = @import("tests/deepseek_v41_reference/attention_math.zig");
+ _ = @import("tests/deepseek_v41_reference/qualify.zig");
+ _ = @import("tests/deepseek_v41_reference/operator_hc_fp64.zig");
+ _ = @import("tests/deepseek_v41_reference/operator_csa2_fp64.zig");
+}
diff --git a/zig/deepseek_v41_run.zig b/zig/deepseek_v41_run.zig
new file mode 100644
index 0000000000..6cf4cfcffe
--- /dev/null
+++ b/zig/deepseek_v41_run.zig
@@ -0,0 +1,457 @@
+//! Full-checkpoint admission and execution receipts; no synthetic weights or replay substitute.
+const std = @import("std");
+const mtl = @import("metal");
+const tf = @import("tensorfold");
+const ds = tf.deepseek_v41;
+const config = tf.deepseek_v41_config;
+const load = tf.deepseek_v41_load;
+const weights = tf.deepseek_v41_weights;
+const process_memory = @import("src/server/process_memory.zig");
+const oracle = @import("tests/deepseek_v41_reference/qualify.zig");
+const margins = @import("tests/deepseek_v41_reference/margins.zig");
+const profile = @import("tests/deepseek_v41_reference/profile.zig");
+const trace = @import("tests/deepseek_v41_reference/trace.zig");
+const operators = @import("tests/deepseek_v41_reference/operators.zig");
+const attention_operators = @import("tests/deepseek_v41_reference/operator_attention.zig");
+const history_operators = @import("tests/deepseek_v41_reference/operator_history.zig");
+const hc_operators = @import("tests/deepseek_v41_reference/operator_hc.zig");
+const moe_operators = @import("tests/deepseek_v41_reference/operator_moe.zig");
+
+fn usage(code: u8) noreturn {
+ std.debug.print("usage: tf-dsv41-run plan MODEL TABLES\n" ++
+ " tf-dsv41-run teacher MODEL TABLES IDS_JSON OUT_DIR [CHUNK]\n" ++
+ " tf-dsv41-run seams MODEL TABLES IDS_JSON OUT_DIR\n" ++
+ " tf-dsv41-run oracle MODEL TABLES IDS_JSON OUT_DIR\n" ++
+ " tf-dsv41-run margins MODEL TABLES IDS_JSON OUT_DIR [DEEP]\n" ++
+ " tf-dsv41-run profile MODEL TABLES IDS_JSON OUT_DIR\n" ++
+ " tf-dsv41-run trace MODEL TABLES IDS_JSON OUT_DIR\n" ++
+ " tf-dsv41-run operators MODEL TABLES OUT_DIR\n" ++
+ " tf-dsv41-run attention-sameinput MODEL TABLES TRACE_CPU_BIN OUT_DIR [LAYER]\n" ++
+ " tf-dsv41-run attention-history-sameinput MODEL TABLES TRACE_DIR OUT_DIR TOKEN LAYER\n" ++
+ " tf-dsv41-run hc-sameinput MODEL TABLES TRACE_CPU_BIN OUT_DIR [LAYER]\n" ++
+ " tf-dsv41-run moe-sameinput MODEL TABLES TRACE_GPU_BIN OUT_DIR [LAYER]\n" ++
+ " tf-dsv41-run generate MODEL TABLES IDS_JSON OUT_DIR [TOKENS]\n" ++
+ "IDS_JSON: nonempty JSON array of token IDs below 129280.\n" ++
+ "Limits: oracle/trace 16 IDs; profile 256 IDs; teacher/seams 4096 IDs.\n" ++
+ "History: TOKEN below 4096; retained normalized prefix at most 256 MiB.\n" ++
+ "Local diagnostics do not qualify the model. Oracle retains the independent 1% NRMS gate.\n" ++
+ "DSV41_ENGRAM_STREAM=1 reads the Engram tables from their shards (not with oracle or trace);\n" ++
+ "the receipt then admits disk reads up to the counted Engram row bytes and no page-ins.\n", .{});
+ std.process.exit(code);
+}
+fn streamEngram() bool {
+ const v = std.c.getenv("DSV41_ENGRAM_STREAM") orelse return false;
+ return std.mem.eql(u8, std.mem.span(v), "1");
+}
+fn number(s: []const u8) !u32 {
+ return std.fmt.parseInt(u32, s, 10);
+}
+fn validArguments(args: []const [:0]const u8) bool {
+ if (args.len < 2) return false;
+ const mode = args[1];
+ for ([_][]const u8{ "plan", "operators" }, [_]usize{ 4, 5 }) |name, count|
+ if (std.mem.eql(u8, mode, name)) return args.len == count;
+ if (std.mem.eql(u8, mode, "attention-history-sameinput")) return args.len == 8;
+ for ([_][]const u8{ "teacher", "generate", "attention-sameinput", "hc-sameinput", "moe-sameinput", "margins" }) |name|
+ if (std.mem.eql(u8, mode, name)) return args.len == 6 or args.len == 7;
+ if (std.mem.eql(u8, mode, "lanes")) return args.len == 6 or args.len == 7 or args.len == 8;
+ for ([_][]const u8{ "seams", "oracle", "profile", "trace" }) |name|
+ if (std.mem.eql(u8, mode, name)) return args.len == 6;
+ return false;
+}
+fn argmax(row: []const f32) !u32 {
+ var best: u32 = 0;
+ if (row.len == 0) return error.EmptyLogits;
+ var score = -std.math.inf(f32);
+ for (row, 0..) |v, i| {
+ if (!std.math.isFinite(v)) return error.NonfiniteLogits;
+ if (v > score) {
+ best = @intCast(i);
+ score = v;
+ }
+ }
+ return best;
+}
+fn write(io: std.Io, a: std.mem.Allocator, dir: []const u8, name: []const u8, data: []const u8) !void {
+ const path = try std.fs.path.join(a, &.{ dir, name });
+ defer a.free(path);
+ try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = data });
+}
+const Counters = struct { disk_read: u64, pageins: u64, footprint: u64 };
+/// Darwin's full rusage_info_v4: the kernel writes all 336 bytes, so every field must be here.
+const RUsage = extern struct {
+ uuid: [16]u8,
+ user_time: u64,
+ system_time: u64,
+ pkg_idle_wkups: u64,
+ interrupt_wkups: u64,
+ pageins: u64,
+ wired_size: u64,
+ resident_size: u64,
+ phys_footprint: u64,
+ proc_start_abstime: u64,
+ proc_exit_abstime: u64,
+ child_user_time: u64,
+ child_system_time: u64,
+ child_pkg_idle_wkups: u64,
+ child_interrupt_wkups: u64,
+ child_pageins: u64,
+ child_elapsed_abstime: u64,
+ diskio_bytesread: u64,
+ diskio_byteswritten: u64,
+ cpu_time_qos_default: u64,
+ cpu_time_qos_maintenance: u64,
+ cpu_time_qos_background: u64,
+ cpu_time_qos_utility: u64,
+ cpu_time_qos_legacy: u64,
+ cpu_time_qos_user_initiated: u64,
+ cpu_time_qos_user_interactive: u64,
+ billed_system_time: u64,
+ serviced_system_time: u64,
+ logical_writes: u64,
+ lifetime_max_phys_footprint: u64,
+ instructions: u64,
+ cycles: u64,
+ billed_energy: u64,
+ serviced_energy: u64,
+ interval_max_phys_footprint: u64,
+ runnable_time: u64,
+};
+fn counters() !Counters {
+ const proc_pid_rusage = struct {
+ extern "c" fn proc_pid_rusage(pid: c_int, flavor: c_int, buffer: *anyopaque) c_int;
+ }.proc_pid_rusage;
+ const getpid = struct {
+ extern "c" fn getpid() c_int;
+ }.getpid;
+ var r: RUsage = undefined;
+ if (proc_pid_rusage(getpid(), 4, &r) != 0) return error.MemoryCountersUnavailable;
+ return .{ .disk_read = r.diskio_bytesread, .pageins = r.pageins, .footprint = r.phys_footprint };
+}
+fn teacher(m: *ds.Model, ids: []const u32, chunk: u32, out: []f32) !f64 {
+ if (chunk == 0 or chunk > m.workspace.plan.rows or ids.len > m.cache.plan.capacity)
+ return error.InvalidTeacherWindow;
+ if (out.len != ids.len * 129280) return error.InvalidTeacherOutputLength;
+ try m.reset();
+ var elapsed: f64 = 0;
+ var start: usize = 0;
+ while (start < ids.len) {
+ const end = @min(start + chunk, ids.len);
+ const row = try m.forward(ids[start..end], .all);
+ @memcpy(out[start * 129280 .. end * 129280], row);
+ for (row) |value| if (!std.math.isFinite(value)) return error.NonfiniteLogits;
+ elapsed += m.last_gpu_seconds;
+ start = end;
+ }
+ return elapsed;
+}
+
+fn teacherOn(m: *ds.Model, slot: u32, ids: []const u32, chunk: u32, out: []f32) !f64 {
+ if (chunk == 0 or chunk > m.workspace.plan.rows or ids.len > m.caches[slot].plan.capacity)
+ return error.InvalidTeacherWindow;
+ if (out.len != ids.len * 129280) return error.InvalidTeacherOutputLength;
+ try m.resetSlot(slot);
+ var elapsed: f64 = 0;
+ var start: usize = 0;
+ while (start < ids.len) {
+ const end = @min(start + chunk, ids.len);
+ const row = try m.forwardOn(slot, ids[start..end], .all);
+ @memcpy(out[start * 129280 .. end * 129280], row);
+ for (row) |value| if (!std.math.isFinite(value)) return error.NonfiniteLogits;
+ elapsed += m.last_gpu_seconds;
+ start = end;
+ }
+ return elapsed;
+}
+
+pub fn main(init: std.process.Init) !void {
+ const a = init.gpa;
+ const io = init.io;
+ const args = try init.minimal.args.toSlice(init.arena.allocator());
+ if (args.len == 2 and (std.mem.eql(u8, args[1], "--help") or std.mem.eql(u8, args[1], "-h"))) usage(0);
+ if (!validArguments(args)) usage(2);
+ const arena = init.arena.allocator();
+ const mode = args[1];
+ const pool = mtl.objc.Pool.push();
+ defer pool.pop();
+ if (std.mem.eql(u8, mode, "plan")) {
+ const cfg = try config.Config.load(arena, io, args[2]);
+ var plan = try load.Plan.openWith(a, io, args[2], args[3], if (streamEngram()) .{ .engram = .streamed, .bound_only = true } else .{});
+ defer plan.deinit();
+ try weights.validate(&plan);
+ const device = try mtl.Device.init();
+ defer device.deinit();
+ std.debug.print("{{\"mode\":\"plan\",\"weight_bytes\":{d},\"shards\":{d},\"tensors\":{d},\"context\":{d},\"metal_working_set_bytes\":{d}}}\n", .{ plan.bytes, plan.shards.items.len, plan.tensors.count(), cfg.text_config.max_position_embeddings, device.maxWorkingSet() });
+ return;
+ }
+ const is_operators = std.mem.eql(u8, mode, "operators");
+ const is_attention = std.mem.eql(u8, mode, "attention-sameinput");
+ const is_attention_history = std.mem.eql(u8, mode, "attention-history-sameinput");
+ const is_hc = std.mem.eql(u8, mode, "hc-sameinput");
+ const is_moe = std.mem.eql(u8, mode, "moe-sameinput");
+ const is_lanes = std.mem.eql(u8, mode, "lanes");
+ const max_ids: usize = if (std.mem.eql(u8, mode, "margins")) 1048576 else if (std.mem.eql(u8, mode, "oracle") or std.mem.eql(u8, mode, "trace")) 16 else if (std.mem.eql(u8, mode, "profile")) ds.prefill_step else if (std.mem.eql(u8, mode, "teacher") or std.mem.eql(u8, mode, "seams") or is_lanes) 4096 else 1048576;
+ const input = if (is_operators or is_attention or is_attention_history or is_hc or is_moe) &.{} else try std.Io.Dir.cwd().readFileAlloc(io, args[4], arena, .limited(max_ids * 12 + 2));
+ const ids: []const u32 = if (is_operators or is_attention or is_attention_history or is_hc or is_moe) &.{} else try std.json.parseFromSliceLeaky([]const u32, arena, input, .{});
+ if (!is_operators and !is_attention and !is_attention_history and !is_hc and !is_moe and (ids.len == 0 or ids.len > 1048576)) return error.InvalidPrompt;
+ if (ids.len > max_ids) return error.InvalidPrompt;
+ for (ids) |id| if (id >= 129280) return error.InvalidToken;
+ const is_oracle = std.mem.eql(u8, mode, "oracle");
+ const is_trace = std.mem.eql(u8, mode, "trace");
+ const dir = args[if (is_operators) @as(usize, 4) else 5];
+ try std.Io.Dir.cwd().createDirPath(io, dir);
+ const map = try std.fs.path.join(arena, &.{ args[2], "engram-token-map.bin" });
+ const map_blob = if (is_oracle or is_trace)
+ try std.Io.Dir.cwd().readFileAlloc(io, map, arena, .limited(2 << 20))
+ else
+ &.{};
+ const model = try a.create(ds.Model);
+ defer a.destroy(model);
+ const begin = mtl.clock.seconds();
+ model.* = try ds.Model.open(a, io, .{
+ .dir = args[2],
+ .table_dir = args[3],
+ .token_map = map,
+ .lanes = if (is_lanes) 2 else 1,
+ // Two full-capacity caches sit at the machine's wired edge; the receipt needs only the prompt's window.
+ .context = if (is_lanes and args.len > 7) try number(args[7]) else 0,
+ .engram = if (streamEngram()) .streamed else .resident,
+ .bound_only = streamEngram(),
+ });
+ defer model.deinit();
+ var tp_transport: ?ds.tp_transport.Transport = null;
+ defer if (tp_transport) |*transport| transport.deinit();
+ var tp_context: ?ds.tp_moe.Context = null;
+ defer if (tp_context) |*context| context.deinit();
+ if (std.c.getenv("TF_DSV41_TP_SETTINGS")) |path| {
+ const settings_json = try std.Io.Dir.cwd().readFileAlloc(io, std.mem.span(path), arena, .limited(65536));
+ tp_transport = try ds.tp_transport.Transport.init(a, settings_json, model.workspace.plan.rows);
+ const transport = &tp_transport.?;
+ tp_context = try ds.tp_moe.Context.init(a, model.device, &model.weights, @intCast(transport.channel.myRank()), model.workspace.plan.rows, transport.exchange());
+ tp_context.?.registerResidency(model.residency);
+ const own_identity = try tf.deepseek_v41_tp_pair.identity(a, io, args[2]);
+ var peer_identity: [32]u8 = undefined;
+ const exchange = transport.exchange();
+ try exchange.call(exchange.ptr, std.math.maxInt(u64) - 1, &own_identity, &peer_identity);
+ if (!std.mem.eql(u8, &own_identity, &peer_identity)) return error.TensorParallelIdentityMismatch;
+ model.residency.commit();
+ model.residency.requestResidency();
+ model.tp = &tp_context.?;
+ }
+ const ready = mtl.clock.seconds();
+ const before = try counters();
+ const engram_before = model.engramStats();
+ var execution_after: ?Counters = null;
+ var execution_error: ?anyerror = null;
+ execute(a, io, model, args, ids, map_blob, dir, before, &execution_after) catch |err| {
+ execution_error = err;
+ };
+ const after = execution_after orelse try counters();
+ if (after.disk_read < before.disk_read or after.pageins < before.pageins) return error.NonmonotonicCounters;
+ const engram_after = model.engramStats();
+ const engram_read_bytes = engram_after.read_bytes - engram_before.read_bytes;
+ const streamed = model.engram_sources != null;
+ // Streamed I/O may read only counted F_NOCACHE row bytes; page-fault reads still fail the gate.
+ const resident_gate_passed = if (streamed)
+ after.disk_read - before.disk_read <= engram_read_bytes and after.pageins == before.pageins
+ else
+ after.disk_read == before.disk_read;
+ const receipt = try std.json.Stringify.valueAlloc(a, .{
+ .mode = mode,
+ .tensor_parallel_ranks = if (model.tp != null) @as(u32, 2) else 1,
+ .tensor_parallel_rank = if (model.tp) |context| @as(u32, context.rank) else 0,
+ .tensor_parallel_partition = if (model.tp) |c| (if (c.slot_ids != null) "moe_route_slots_replicated_weights" else "moe_output_columns_replicated_weights") else "none",
+ .attention_parallel = model.pipelines.attention.sparse.parallel_enabled,
+ .tensor_parallel_exchanges = if (tp_transport) |transport| transport.channel.step else 0,
+ .evidence_origin = "current_invocation_not_historical_qualification",
+ .context_capacity = model.cache.plan.capacity,
+ .consumed_tokens = model.cache.position,
+ .weight_bytes = model.checkpoint.residentBytes(),
+ .resident_allocation_bytes = model.residency.allocatedSize() + model.weight_residency.bytes(),
+ .weight_residency_sets = model.weight_residency.count,
+ .residency_refresh_ticks = if (model.residency_refresh) |refresh| refresh.ticks.load(.acquire) else 0,
+ .ready_footprint_bytes = before.footprint,
+ .final_footprint_bytes = after.footprint,
+ .load_seconds = ready - begin,
+ .measurement_scope = if (execution_after != null) "model_execution" else "execution_and_diagnostic_io",
+ .execution_disk_read_bytes = after.disk_read - before.disk_read,
+ .execution_pageins = after.pageins - before.pageins,
+ .engram = if (streamed) "streamed" else "resident",
+ .engram_read_bytes = engram_read_bytes,
+ .engram_rows_requested = engram_after.requested - engram_before.requested,
+ .engram_rows_unique = engram_after.unique - engram_before.unique,
+ .engram_cache_hits = engram_after.hits - engram_before.hits,
+ .engram_reads = engram_after.reads - engram_before.reads,
+ .engram_wait_seconds = @as(f64, @floatFromInt(engram_after.wait_ns - engram_before.wait_ns)) / 1e9,
+ .resident_gate_passed = resident_gate_passed,
+ .execution_error = if (execution_error) |err| @as(?[]const u8, @errorName(err)) else null,
+ }, .{});
+ defer a.free(receipt);
+ std.debug.print("{s}\n", .{receipt});
+ try write(io, a, dir, "residency.json", receipt);
+ if (!resident_gate_passed) std.debug.print("residency gate failed: ResidentExecutionReadFromDisk\n", .{});
+ if (execution_error) |err| return err;
+ if (!resident_gate_passed) return error.ResidentExecutionReadFromDisk;
+}
+
+fn execute(a: std.mem.Allocator, io: std.Io, model: *ds.Model, args: []const [:0]const u8, ids: []const u32, map_blob: []const u8, dir: []const u8, before: Counters, execution_after: *?Counters) !void {
+ const mode = args[1];
+ const is_operators = std.mem.eql(u8, mode, "operators");
+ const is_attention = std.mem.eql(u8, mode, "attention-sameinput");
+ const is_attention_history = std.mem.eql(u8, mode, "attention-history-sameinput");
+ const is_hc = std.mem.eql(u8, mode, "hc-sameinput");
+ const is_moe = std.mem.eql(u8, mode, "moe-sameinput");
+ const is_oracle = std.mem.eql(u8, mode, "oracle");
+ const is_profile = std.mem.eql(u8, mode, "profile");
+ const is_trace = std.mem.eql(u8, mode, "trace");
+ const is_teacher = std.mem.eql(u8, mode, "teacher");
+ const is_seams = std.mem.eql(u8, mode, "seams");
+ const is_lanes = std.mem.eql(u8, mode, "lanes");
+ if (is_operators) {
+ try operators.run(a, io, model, dir);
+ } else if (is_attention) {
+ try attention_operators.runAttention(a, io, model, args[4], dir, if (args.len > 6) try number(args[6]) else 0);
+ } else if (is_attention_history) {
+ try history_operators.runAttentionHistory(a, io, model, args[4], dir, try number(args[6]), try number(args[7]));
+ } else if (is_hc) {
+ try hc_operators.runHyperconnections(a, io, model, args[4], dir, if (args.len > 6) try number(args[6]) else 0);
+ } else if (is_moe) {
+ try moe_operators.runMoe(a, io, model, args[4], dir, if (args.len > 6) try number(args[6]) else 21);
+ } else if (is_oracle) {
+ try operators.run(a, io, model, dir);
+ _ = try oracle.run(a, io, model, ids, map_blob, dir);
+ } else if (std.mem.eql(u8, mode, "margins")) {
+ try margins.run(a, io, model, ids, dir, if (args.len > 6) try number(args[6]) else 1536);
+ } else if (is_profile) {
+ try profile.run(a, io, model, ids, dir);
+ } else if (is_lanes) {
+ // Two lane slots, interleaved: each slot's rows and logits equal its own solo run's bits.
+ if (ids.len < 4) return error.InvalidPrompt;
+ const split = ids.len / 2;
+ const chunk = if (args.len > 6) try number(args[6]) else 1;
+ if (chunk == 0 or chunk > model.workspace.plan.rows) return error.InvalidPrefillChunk;
+ const solo_a = try a.alloc(f32, split * 129280);
+ defer a.free(solo_a);
+ const solo_b = try a.alloc(f32, (ids.len - split) * 129280);
+ defer a.free(solo_b);
+ var elapsed: f64 = 0;
+ elapsed += try teacherOn(model, 0, ids[0..split], chunk, solo_a);
+ elapsed += try teacherOn(model, 1, ids[split..], chunk, solo_b);
+ const position_a: u32 = model.caches[0].position;
+ const position_b: u32 = model.caches[1].position;
+ try model.reset();
+ const mixed_a = try a.alloc(f32, solo_a.len);
+ defer a.free(mixed_a);
+ const mixed_b = try a.alloc(f32, solo_b.len);
+ defer a.free(mixed_b);
+ var at_a: usize = 0;
+ var at_b: usize = 0;
+ while (at_a < split or at_b < ids.len - split) {
+ if (at_a < split) {
+ const end = @min(at_a + chunk, split);
+ const row = try model.forwardOn(0, ids[at_a..end], .all);
+ @memcpy(mixed_a[at_a * 129280 .. end * 129280], row);
+ at_a = end;
+ }
+ if (at_b < ids.len - split) {
+ const end = @min(at_b + chunk, ids.len - split);
+ const row = try model.forwardOn(1, ids[split + at_b .. split + end], .all);
+ @memcpy(mixed_b[at_b * 129280 .. end * 129280], row);
+ at_b = end;
+ }
+ }
+ var error_a: f64 = 0;
+ var error_b: f64 = 0;
+ var bit_disagreements: usize = 0;
+ for (solo_a, mixed_a) |x, y| {
+ error_a = @max(error_a, @abs(@as(f64, x) - y));
+ bit_disagreements += @intFromBool(@as(u32, @bitCast(x)) != @as(u32, @bitCast(y)));
+ }
+ for (solo_b, mixed_b) |x, y| {
+ error_b = @max(error_b, @abs(@as(f64, x) - y));
+ bit_disagreements += @intFromBool(@as(u32, @bitCast(x)) != @as(u32, @bitCast(y)));
+ }
+ execution_after.* = try counters();
+ std.debug.print("{{\"mode\":\"lanes\",\"slots\":2,\"chunk\":{d},\"max_logit_error_slot0\":{d},\"max_logit_error_slot1\":{d},\"bit_disagreements\":{d},\"position_slot0\":{d},\"position_slot1\":{d},\"gpu_seconds\":{d:.3}}}\n", .{ chunk, error_a, error_b, bit_disagreements, position_a, position_b, elapsed });
+ if (bit_disagreements != 0) return error.LaneInterference;
+ } else if (is_trace) {
+ try trace.run(a, io, model, ids, map_blob, dir);
+ } else if (is_teacher or is_seams) {
+ const chunk = if (args.len > 6) try number(args[6]) else model.workspace.plan.rows;
+ if (chunk == 0 or chunk > model.workspace.plan.rows) return error.InvalidPrefillChunk;
+ const outputs = try a.alloc(f32, ids.len * 129280);
+ defer a.free(outputs);
+ const teacher_start = mtl.clock.seconds();
+ const elapsed = try teacher(model, ids, chunk, outputs);
+ const wall_seconds = mtl.clock.seconds() - teacher_start;
+ // Capture execution before writing the receipt, not filesystem metadata reads from the receipt itself.
+ if (is_seams) {
+ const alternative = try a.alloc(f32, outputs.len);
+ defer a.free(alternative);
+ for ([_]u32{ 1, 2, 3, 7, 31, 127, 128, 129, 255, 256 }) |split| {
+ if (split > model.workspace.plan.rows) continue;
+ _ = try teacher(model, ids, split, alternative);
+ var error_max: f64 = 0;
+ var disagreements: usize = 0;
+ var bit_disagreements: usize = 0;
+ for (outputs, alternative) |x, y| {
+ error_max = @max(error_max, @abs(@as(f64, x) - y));
+ bit_disagreements += @intFromBool(@as(u32, @bitCast(x)) != @as(u32, @bitCast(y)));
+ }
+ for (0..ids.len) |i| {
+ if (try argmax(outputs[i * 129280 ..][0..129280]) != try argmax(alternative[i * 129280 ..][0..129280]))
+ disagreements += 1;
+ }
+ std.debug.print("{{\"mode\":\"seam\",\"chunk\":{d},\"max_logit_error\":{d},\"bit_disagreements\":{d},\"greedy_disagreements\":{d}}}\n", .{ split, error_max, bit_disagreements, disagreements });
+ if (disagreements != 0 or bit_disagreements != 0) {
+ var name: [48]u8 = undefined;
+ const file = try std.fmt.bufPrint(&name, "seam-{d}-logits.f32", .{split});
+ execution_after.* = try counters();
+ try write(io, a, dir, file, std.mem.sliceAsBytes(alternative));
+ return error.ChunkSeamMismatch;
+ }
+ }
+ }
+ std.debug.print("{{\"mode\":\"teacher\",\"tokens\":{d},\"elapsed_seconds\":{d},\"gpu_seconds\":{d}}}\n", .{ ids.len, wall_seconds, elapsed });
+ execution_after.* = try counters();
+ try write(io, a, dir, "logits.f32", std.mem.sliceAsBytes(outputs));
+ } else if (std.mem.eql(u8, mode, "generate")) {
+ const n = if (args.len > 6) try number(args[6]) else 64;
+ if (ids.len > model.cache.plan.capacity or n == 0 or n > model.cache.plan.capacity - ids.len)
+ return error.ContextFull;
+ try model.reset();
+ const prefill_start = mtl.clock.seconds();
+ var row: []const f32 = &.{};
+ var start: usize = 0;
+ var progress_at = prefill_start;
+ while (start < ids.len) {
+ const end = @min(start + model.workspace.plan.rows, ids.len);
+ row = try model.forward(ids[start..end], if (end == ids.len) .last else .none);
+ start = end;
+ const now = mtl.clock.seconds();
+ if (now - progress_at >= 60 or start == ids.len) {
+ const memory = try counters();
+ std.debug.print("{{\"mode\":\"prefill_progress\",\"completed_tokens\":{d},\"prompt_tokens\":{d},\"elapsed_seconds\":{d},\"footprint_bytes\":{d},\"disk_read_bytes\":{d},\"pageins\":{d}}}\n", .{ start, ids.len, now - prefill_start, memory.footprint, memory.disk_read - before.disk_read, memory.pageins - before.pageins });
+ progress_at = now;
+ }
+ }
+ const prefill_end = mtl.clock.seconds();
+ const generated = try a.alloc(u32, n);
+ defer a.free(generated);
+ var gpu_seconds: f64 = 0;
+ const decode_start = mtl.clock.seconds();
+ for (generated, 0..) |*token, i| {
+ token.* = try argmax(row);
+ if (i + 1 < generated.len) {
+ row = try model.forward(&.{token.*}, .last);
+ gpu_seconds += model.last_gpu_seconds;
+ }
+ }
+ const decode_end = mtl.clock.seconds();
+ execution_after.* = try counters();
+ const json = try std.json.Stringify.valueAlloc(a, generated, .{});
+ defer a.free(json);
+ try write(io, a, dir, "tokens.json", json);
+ std.debug.print("{{\"mode\":\"generate\",\"prompt_tokens\":{d},\"completion_tokens\":{d},\"prefill_seconds\":{d},\"decode_seconds\":{d},\"decode_gpu_seconds\":{d}}}\n", .{ ids.len, n, prefill_end - prefill_start, decode_end - decode_start, gpu_seconds });
+ } else usage(2);
+}
diff --git a/zig/deepseek_v41_weights_checks.zig b/zig/deepseek_v41_weights_checks.zig
new file mode 100644
index 0000000000..1bd3f9deda
--- /dev/null
+++ b/zig/deepseek_v41_weights_checks.zig
@@ -0,0 +1,10 @@
+//! Host checks for V4.1 configuration, Engram table plans and resident weight staging; no kernels compiled.
+test {
+ _ = @import("src/families/deepseek_v41/metal_admission.zig");
+ _ = @import("src/families/deepseek_v41/metal_config.zig");
+ _ = @import("src/families/deepseek_v41/view.zig");
+ _ = @import("src/families/deepseek_v41/engram_hash.zig");
+ _ = @import("src/families/deepseek_v41/metal_load.zig");
+ _ = @import("src/families/deepseek_v41/weights.zig");
+ _ = @import("src/families/deepseek_v41/engram_safetensors.zig");
+}
diff --git a/zig/dense_serving_test.zig b/zig/dense_serving_test.zig
new file mode 100644
index 0000000000..6d64b3e0f6
--- /dev/null
+++ b/zig/dense_serving_test.zig
@@ -0,0 +1,43 @@
+//! Reviewed lane/decode serving bytes and calls; changing them needs a reviewed implementation and receipt.
+const std = @import("std");
+fn hash(bytes: []const u8) [64]u8 {
+ var h: [32]u8 = undefined;
+ std.crypto.hash.sha2.Sha256.hash(bytes, &h, .{});
+ return std.fmt.bytesToHex(h, .lower);
+}
+test "dense serving: owned CUDA images retain their reviewed lane and decode bytes" {
+ try std.testing.expectEqualStrings("51298c868da178dcffec07a748fc6ac449e55f68c7b397d86572bec9bf878f24", &hash(@embedFile("kernels/cuda/experts.cu")));
+ try std.testing.expectEqualStrings("f195f5678edf7d3267ec30e442b1e0048543006e595ad21801afd905df621baf", &hash(@embedFile("kernels/cuda/experts_pack.cu")));
+ try std.testing.expectEqualStrings("db560d705729142c66df8dee18fc635f20f09e4f09b8431de265bd6fac68e892", &hash(@embedFile("kernels/cuda/experts_prefill.cu")));
+ try std.testing.expectEqualStrings("a6bf09198ffd685f7392d4d433af9739c91d67f7039309d3fd5fe6785f810ab9", &hash(@embedFile("kernels/cuda/gdn.cu")));
+ try std.testing.expectEqualStrings("9551469ae616283ee893d7f117d143c43de08bc868650f539cf38570a47e7a13", &hash(@embedFile("kernels/cuda/lane_gemv.cu")));
+ try std.testing.expectEqualStrings("71b9aba5ba586b12454a4c2cb8168e59227aed4db8bc78190ecc9f262ba3d62f", &hash(@embedFile("kernels/cuda/prefill_attention.cu")));
+ const group = @embedFile("kernels/cuda/qmm_group.cu");
+ try std.testing.expectEqualStrings("3b27b8e964f6e7453e962cd7a259253ae7e1940297439b4b518f05da04dd1041", &hash(group[0..16132]));
+ try std.testing.expectEqualStrings("dd599cefbda6648f2a19e429fea3dcd869f2a864dc01b0e130f8240cab036d08", &hash(group));
+ try std.testing.expectEqualStrings("6dbafecbf5c28b48da1bcd222f85494b471e728a482eb17fc5fffa8c522dee95", &hash(@embedFile("kernels/cuda/qmm_prefill.cu")));
+ try std.testing.expectEqualStrings("774d1a16eac9617c55d8e9c3c3364fcc62613ea5a46fa40e29bf7eac0247134d", &hash(@embedFile("kernels/cuda/qwen35.cu")));
+ try std.testing.expectEqualStrings("9ab11702f17f4573f344118be7da76f81100cefc8bf4be931d4a5f25feced038", &hash(@embedFile("kernels/cuda/qwen35.cuh")));
+ try std.testing.expectEqualStrings("60ffcb12aaf8155d8b82274299a0e21cb007d06e540c47cfeaeb6de2e69a6317", &hash(@embedFile("kernels/cuda/qwen35_draft.cu")));
+ try std.testing.expectEqualStrings("873d87646b38a43339817c2aa367a8b279786921b7b5c62bd7754aa80aed0073", &hash(@embedFile("kernels/cuda/qwen35_keep.cu")));
+ try std.testing.expectEqualStrings("d1a379a98cf7504a7ed3b3f2d24c8ab6a39fe5511bcded6449e598ce5c3c79e7", &hash(@embedFile("kernels/cuda/scan_rows.cu")));
+}
+test "dense serving: owned launches and projections retain their reviewed argument sequence" {
+ const source = @embedFile("src/families/qwen3_5/cuda_engine.zig") ++ @embedFile("src/families/qwen3_5/cuda_forward.zig");
+ var out: std.ArrayList(u8) = .empty;
+ defer out.deinit(std.testing.allocator);
+ var at: usize = 0;
+ while (std.mem.indexOfPos(u8, source, at, "try e.")) |start| {
+ const rest = source[start..];
+ const end = std.mem.indexOfScalar(u8, rest, ';') orelse return error.BadSource;
+ const call = rest[0 .. end + 1];
+ at = start + 6;
+ if (!(std.mem.startsWith(u8, call, "try e.run(e.fns.") or std.mem.startsWith(u8, call, "try e.proj(") or
+ std.mem.startsWith(u8, call, "try e.norm("))) continue;
+ at = start + end + 1;
+ for (call) |c| if (!std.ascii.isWhitespace(c)) {
+ try out.append(std.testing.allocator, c);
+ };
+ }
+ try std.testing.expectEqualStrings("b585fc87d31e7cefdb26c9ede437905886cb7e65544f1c890ed26868317b49ce", &hash(out.items));
+}
diff --git a/zig/dsv41_token_map.zig b/zig/dsv41_token_map.zig
new file mode 100644
index 0000000000..a2c111f280
--- /dev/null
+++ b/zig/dsv41_token_map.zig
@@ -0,0 +1,485 @@
+//! Builds the canonical Engram token map from the release tokenizer and typed configuration.
+
+// Algorithm: DeepSeek AI inference/engram.py, MIT; the pinned revision is retained in source below.
+const std = @import("std");
+const tok = @import("src/core/tokenizer/tokenizer.zig");
+const unicode = @import("src/core/tokenizer/unicode.zig");
+const nfkd_data = @import("src/core/tokenizer/nfkd_data.zig");
+
+const Layer = struct { layer_id: u64, rows: u64, primes: [24]u32, offsets: [24]u32, multipliers: [4]u64 };
+const magic = "DSENGR01";
+const source = "https://huggingface.co/deepseek-ai/DeepSeek-V4.1-Flash/blob/2cba9e42aa026125f3ed06c6d98c1db82f7ca027/inference/engram.py";
+
+pub fn main(init: std.process.Init) !void {
+ const a = init.gpa;
+ const io = init.io;
+ const args = try init.minimal.args.toSlice(init.arena.allocator());
+ const paths = try parseArgs(args);
+ const tokenizer_path: ?[]const u8 = paths.tokenizer;
+ const config_path: ?[]const u8 = paths.config;
+ const output_path: ?[]const u8 = paths.output;
+ const tokenizer_bytes = try std.Io.Dir.cwd().readFileAlloc(io, tokenizer_path.?, a, .limited(64 << 20));
+ defer a.free(tokenizer_bytes);
+ const config_bytes = try std.Io.Dir.cwd().readFileAlloc(io, config_path.?, a, .limited(16 << 20));
+ defer a.free(config_bytes);
+ var t = try tok.loadTokenizer(io, a, tokenizer_path.?);
+ defer t.deinit();
+
+ const config = try @import("src/families/deepseek_v41/config.zig").Config.parse(a, config_bytes);
+
+ // Decode, fall back to the raw token on U+FFFD, normalize, then deduplicate keys in first-seen order.
+ var key_arena = std.heap.ArenaAllocator.init(a);
+ defer key_arena.deinit();
+ const ka = key_arena.allocator();
+ var keys: std.StringArrayHashMapUnmanaged(u32) = .empty;
+ defer keys.deinit(a);
+ const raw_vocab: usize = config.vocab;
+ const compressed_expected: usize = config.engram_compressed_vocab;
+ const lookup = try a.alloc(u32, raw_vocab);
+ defer a.free(lookup);
+ for (0..raw_vocab) |id| {
+ const text = try t.decode(a, &.{@intCast(id)}, false);
+ defer a.free(text);
+ const key = if (std.mem.indexOfScalar(u8, text, 0xEF) != null and std.mem.indexOf(u8, text, "\u{fffd}") != null)
+ try ka.dupe(u8, t.id_to_token.get(@intCast(id)) orelse return error.MissingTokenizerEntry)
+ else
+ try normalizeKey(ka, text);
+ const gop = try keys.getOrPut(a, key);
+ if (!gop.found_existing) {
+ gop.value_ptr.* = @intCast(keys.count() - 1);
+ gop.key_ptr.* = key;
+ }
+ lookup[id] = gop.value_ptr.*;
+ }
+ if (keys.count() != compressed_expected) {
+ std.log.err("tokenizer mismatch: compressed keys {d} != {d}", .{ keys.count(), compressed_expected });
+ return error.TokenizerMismatch;
+ }
+
+ const order: u64 = config.engram_max_ngram;
+ const heads: u64 = config.engram_heads;
+ if (order != 4 or heads != 8) return error.UnsupportedEngramShape;
+ const compressed = keys.count();
+ const bound: u64 = @max(1, ((@as(u64, 1) << 63) - 1) / compressed / 2);
+
+ // Each layer owns 24 distinct prime buckets; PCG64 multipliers use seed 10007 * layer.
+ var seen: std.AutoHashMapUnmanaged(u64, void) = .empty;
+ defer seen.deinit(a);
+ const layer_count = config.engram_layers.len;
+ const layers = try a.alloc(Layer, layer_count);
+ defer a.free(layers);
+ const engram_vocab_size = config.engram_vocab;
+ for (config.engram_layers.items(), config.engram_rows.items(), 0..) |layer_id, rows, li| {
+ var primes: [24]u32 = undefined;
+ var offsets: [24]u32 = undefined;
+ var n: usize = 0;
+ var current: u64 = engram_vocab_size - 1;
+ while (n < (order - 1) * heads) : (n += 1) {
+ current += 1;
+ while (true) {
+ if (isPrime(current)) {
+ const gop = try seen.getOrPut(a, current);
+ if (!gop.found_existing) break;
+ }
+ current += 1;
+ }
+ var sum: u64 = 0;
+ for (primes[0..n]) |p| sum += p;
+ offsets[n] = @intCast(sum);
+ primes[n] = @intCast(current);
+ }
+ var sum: u64 = 0;
+ for (primes) |p| sum += p;
+ if (sum != rows) {
+ std.log.err("layer {d}: prime buckets sum {d} != table rows {d}", .{ layer_id, sum, rows });
+ return error.ConfigMismatch;
+ }
+ var rng = Pcg64.init(10007 * layer_id);
+ var multipliers: [4]u64 = undefined;
+ for (&multipliers) |*m| m.* = rng.boundedBelow(bound) * 2 + 1;
+ layers[li] = .{ .layer_id = layer_id, .rows = rows, .primes = primes, .offsets = offsets, .multipliers = multipliers };
+ }
+
+ const pad_id: usize = config.engram_pad;
+ const pad = lookup[pad_id];
+
+ // LE layout: magic, geometry, provenance hashes, layer blocks, then the token lookup array.
+ var packed_buf: std.ArrayList(u8) = .empty;
+ defer packed_buf.deinit(a);
+ try packed_buf.appendSlice(a, magic);
+ try appendU32(&packed_buf, a, raw_vocab);
+ try appendU32(&packed_buf, a, compressed);
+ try appendU32(&packed_buf, a, pad);
+ try appendU32(&packed_buf, a, order);
+ try appendU32(&packed_buf, a, heads);
+ try appendU32(&packed_buf, a, layer_count);
+ var tokenizer_sha: [32]u8 = undefined;
+ std.crypto.hash.sha2.Sha256.hash(tokenizer_bytes, &tokenizer_sha, .{});
+ var config_sha: [32]u8 = undefined;
+ std.crypto.hash.sha2.Sha256.hash(config_bytes, &config_sha, .{});
+ try packed_buf.appendSlice(a, &tokenizer_sha);
+ try packed_buf.appendSlice(a, &config_sha);
+ for (layers) |layer| {
+ try appendU32(&packed_buf, a, layer.layer_id);
+ try appendU32(&packed_buf, a, layer.rows);
+ for (layer.multipliers) |m| try appendU64(&packed_buf, a, m);
+ for (layer.primes) |p| try appendU32(&packed_buf, a, p);
+ for (layer.offsets) |o| try appendU32(&packed_buf, a, o);
+ }
+ for (lookup) |v| try appendU32(&packed_buf, a, v);
+
+ var out_file = try std.Io.Dir.cwd().createFile(io, output_path.?, .{});
+ defer out_file.close(io);
+ try out_file.writeStreamingAll(io, packed_buf.items);
+
+ // Manifest beside the map, matching the Python tool's fields.
+ var packed_sha: [32]u8 = undefined;
+ std.crypto.hash.sha2.Sha256.hash(packed_buf.items, &packed_sha, .{});
+ const manifest = try writeManifest(a, tokenizer_sha, config_sha, packed_sha, raw_vocab, compressed, pad, order, heads, layers);
+ defer a.free(manifest);
+ const manifest_path = try std.fmt.allocPrint(a, "{s}.json", .{output_path.?});
+ defer a.free(manifest_path);
+ var m_file = try std.Io.Dir.cwd().createFile(io, manifest_path, .{});
+ defer m_file.close(io);
+ try m_file.writeStreamingAll(io, manifest);
+
+ std.debug.print("token map: {d} raw ids -> {d} keys, {d} layers, {d} bytes written to {s}\n", .{ raw_vocab, compressed, layer_count, packed_buf.items.len, output_path.? });
+}
+
+fn writeManifest(a: std.mem.Allocator, tokenizer_sha: [32]u8, config_sha: [32]u8, packed_sha: [32]u8, raw_vocab: usize, compressed: usize, pad: usize, order: u64, heads: u64, layers: []const Layer) ![]u8 {
+ return std.json.Stringify.valueAlloc(a, .{
+ .source = source,
+ .tokenizer_sha256 = std.fmt.bytesToHex(tokenizer_sha, .lower),
+ .config_sha256 = std.fmt.bytesToHex(config_sha, .lower),
+ .packed_sha256 = std.fmt.bytesToHex(packed_sha, .lower),
+ .raw_vocab = raw_vocab,
+ .compressed_vocab = compressed,
+ .pad = pad,
+ .order = order,
+ .heads = heads,
+ .layers = layers,
+ }, .{});
+}
+const Paths = struct { tokenizer: []const u8, config: []const u8, output: []const u8 };
+fn parseArgs(args: []const []const u8) !Paths {
+ if (args.len != 7) return error.InvalidArguments;
+ var paths: [3]?[]const u8 = @splat(null);
+ var i: usize = 1;
+ while (i + 1 < args.len) : (i += 2) {
+ const index: usize = if (std.mem.eql(u8, args[i], "--tokenizer")) 0 else if (std.mem.eql(u8, args[i], "--config")) 1 else if (std.mem.eql(u8, args[i], "--output")) 2 else return error.InvalidArguments;
+ if (paths[index] != null or args[i + 1].len == 0) return error.InvalidArguments;
+ paths[index] = args[i + 1];
+ }
+ return .{ .tokenizer = paths[0] orelse return error.InvalidArguments, .config = paths[1] orelse return error.InvalidArguments, .output = paths[2] orelse return error.InvalidArguments };
+}
+
+fn appendU32(list: *std.ArrayList(u8), a: std.mem.Allocator, v: u64) !void {
+ const tail = try list.addManyAsSlice(a, 4);
+ std.mem.writeInt(u32, tail[0..4], @intCast(v), .little);
+}
+
+fn appendU64(list: *std.ArrayList(u8), a: std.mem.Allocator, v: u64) !void {
+ const tail = try list.addManyAsSlice(a, 8);
+ std.mem.writeInt(u64, tail[0..8], v, .little);
+}
+
+fn isPrime(n: u64) bool {
+ if (n < 2 or n % 2 == 0) return n == 2;
+ var d: u64 = 3;
+ while (d * d <= n) : (d += 2) {
+ if (n % d == 0) return false;
+ }
+ return true;
+}
+
+// Rust normalization composes to NFKD, mark stripping, lowercase, whitespace replacement and trimming.
+
+const sentinel: u21 = 0xE000;
+
+fn normalizeKey(a: std.mem.Allocator, text: []const u8) ![]u8 {
+ var cps: std.ArrayList(u21) = .empty;
+ defer cps.deinit(a);
+ var i: usize = 0;
+ while (i < text.len) {
+ const d = unicode.decodeAt(text, i);
+ try decomposeFull(d.cp, &cps, a);
+ i += d.len;
+ }
+ orderCanonical(cps.items);
+ var kept: std.ArrayList(u21) = .empty;
+ defer kept.deinit(a);
+ for (cps.items) |cp| {
+ if (!isMark(cp)) try kept.append(a, cp);
+ }
+ var low: std.ArrayList(u21) = .empty;
+ defer low.deinit(a);
+ for (kept.items) |cp| {
+ if (lowerOf(cp)) |seq| try low.appendSlice(a, seq) else try low.append(a, cp);
+ }
+ // Replace([ \t\r\n]+, " "): collapse runs of the four regex chars.
+ var col: std.ArrayList(u21) = .empty;
+ defer col.deinit(a);
+ var k: usize = 0;
+ while (k < low.items.len) : (k += 1) {
+ if (isRegexSpace(low.items[k])) {
+ try col.append(a, ' ');
+ while (k + 1 < low.items.len and isRegexSpace(low.items[k + 1])) k += 1;
+ } else try col.append(a, low.items[k]);
+ }
+ // Replace(^ $, U+E000): the whole string being a single space.
+ if (col.items.len == 1 and col.items[0] == ' ') col.items[0] = sentinel;
+ // Strip: trim White_Space at both ends (Rust char::is_whitespace's set).
+ var lo: usize = 0;
+ var hi: usize = col.items.len;
+ while (lo < hi and unicode.isSpace(col.items[lo])) lo += 1;
+ while (hi > lo and unicode.isSpace(col.items[hi - 1])) hi -= 1;
+ // Replace(U+E000, " ").
+ var out: std.ArrayList(u8) = .empty;
+ errdefer out.deinit(a);
+ var wrote = false;
+ for (col.items[lo..hi]) |cp| {
+ try unicode.appendCodepoint(&out, a, if (cp == sentinel) ' ' else cp);
+ wrote = true;
+ }
+ if (!wrote) return a.dupe(u8, text);
+ return out.toOwnedSlice(a);
+}
+
+fn isRegexSpace(cp: u21) bool {
+ return cp == 0x20 or cp == 0x09 or cp == 0x0D or cp == 0x0A;
+}
+
+fn decomposeFull(cp: u21, out: *std.ArrayList(u21), a: std.mem.Allocator) !void {
+ if (decompOf(cp)) |seq| {
+ for (seq) |c| try out.append(a, c);
+ } else try out.append(a, cp);
+}
+
+fn decompOf(cp: u21) ?[]const u21 {
+ var lo: usize = 0;
+ var hi: usize = nfkd_data.decompositions.len;
+ while (lo < hi) {
+ const mid = (lo + hi) / 2;
+ const e = nfkd_data.decompositions[mid];
+ if (e.cp == cp) return nfkd_data.sequences[e.start..][0..e.len];
+ if (e.cp < cp) lo = mid + 1 else hi = mid;
+ }
+ return null;
+}
+
+fn orderCanonical(items: []u21) void {
+ var start: usize = 0;
+ while (start < items.len) : (start += 1) {
+ if (combiningClass(items[start]) == 0) continue;
+ var end = start;
+ while (end < items.len and combiningClass(items[end]) != 0) end += 1;
+ std.sort.insertion(u21, items[start..end], {}, struct {
+ fn lt(_: void, x: u21, y: u21) bool {
+ return combiningClass(x) < combiningClass(y);
+ }
+ }.lt);
+ start = end - 1;
+ }
+}
+
+fn combiningClass(cp: u21) u8 {
+ if (cp < 0x300) return 0;
+ var lo: usize = 0;
+ var hi: usize = nfkd_data.cccs.len;
+ while (lo < hi) {
+ const mid = (lo + hi) / 2;
+ const run = nfkd_data.cccs[mid];
+ if (cp < run[0]) hi = mid else if (cp > run[1]) lo = mid + 1 else return @intCast(run[2]);
+ }
+ return 0;
+}
+
+fn isMark(cp: u21) bool {
+ var lo: usize = 0;
+ var hi: usize = nfkd_data.marks.len;
+ while (lo < hi) {
+ const mid = (lo + hi) / 2;
+ const v = nfkd_data.marks[mid];
+ if (v == cp) return true;
+ if (v < cp) lo = mid + 1 else hi = mid;
+ }
+ return false;
+}
+
+fn lowerOf(cp: u21) ?[]const u21 {
+ var lo: usize = 0;
+ var hi: usize = nfkd_data.lowercases.len;
+ while (lo < hi) {
+ const mid = (lo + hi) / 2;
+ const e = nfkd_data.lowercases[mid];
+ if (e.cp == cp) return nfkd_data.lower_sequences[e.start..][0..e.len];
+ if (e.cp < cp) lo = mid + 1 else hi = mid;
+ }
+ return null;
+}
+
+// numpy Generator.integers: SeedSequence, PCG64, then Lemire bounded integers.
+
+const Pcg64 = struct {
+ state: u128,
+ inc: u128,
+
+ const multiplier: u128 = 0x2360ED051FC65DA44385DF649FCCF645;
+
+ /// PCG seeding: step from zero, add initstate, step again; inc = (initseq << 1) | 1 modulo 2^128.
+ fn init(seed: u64) Pcg64 {
+ const w = seedSequenceWords(seed);
+ const initstate: u128 = (@as(u128, w[0]) << 64) | @as(u128, w[1]);
+ const initseq: u128 = (@as(u128, w[2]) << 64) | @as(u128, w[3]);
+ const inc: u128 = (initseq << 1) | 1;
+ var state: u128 = 0;
+ state = state *% multiplier +% inc;
+ state +%= initstate;
+ state = state *% multiplier +% inc;
+ return .{ .state = state, .inc = inc };
+ }
+
+ fn next64(self: *Pcg64) u64 {
+ self.state = self.state *% multiplier +% self.inc;
+ const hi: u64 = @truncate(self.state >> 64);
+ const lo: u64 = @truncate(self.state);
+ const rot: u6 = @intCast(hi >> 58);
+ return std.math.rotr(u64, lo ^ hi, rot);
+ }
+
+ /// Lemire's bounded generator, numpy's masked=false path for Generator.integers.
+ fn boundedBelow(self: *Pcg64, bound: u64) u64 {
+ const threshold = (0 -% bound) % bound;
+ while (true) {
+ const x = self.next64();
+ const m = @as(u128, x) * @as(u128, bound);
+ if (@as(u64, @truncate(m)) >= threshold) return @intCast(m >> 64);
+ }
+ }
+};
+
+/// SeedSequence hashes into four words, draws eight words with a second hash, then pairs them little endian.
+fn seedSequenceWords(seed: u64) [4]u64 {
+ const entropy = [1]u32{@truncate(seed)};
+ var mixer = [4]u32{ 0, 0, 0, 0 };
+ var hc: u32 = 0x43b0d7e5;
+ for (&mixer, 0..) |*slot, i| {
+ slot.* = hashmix(if (i < entropy.len) entropy[i] else 0, &hc);
+ }
+ for (0..mixer.len) |i_src| {
+ for (0..mixer.len) |i_dst| {
+ if (i_src != i_dst) mixer[i_dst] = mixWords(mixer[i_dst], hashmix(mixer[i_src], &hc));
+ }
+ }
+ var out: [8]u32 = undefined;
+ var hcb: u32 = 0x8b51f9dd;
+ for (&out, 0..) |*slot, i| {
+ var dv = mixer[i % mixer.len];
+ dv ^= hcb;
+ hcb = hcb *% 0x58f38ded;
+ dv = dv *% hcb;
+ dv ^= dv >> 16;
+ slot.* = dv;
+ }
+ var w: [4]u64 = undefined;
+ for (0..4) |i| w[i] = @as(u64, out[2 * i]) | (@as(u64, out[2 * i + 1]) << 32);
+ return w;
+}
+
+fn hashmix(value: u32, hc: *u32) u32 {
+ var v = value ^ hc.*;
+ hc.* = hc.* *% 0x931e8875;
+ v = v *% hc.*;
+ v ^= v >> 16;
+ return v;
+}
+
+fn mixWords(x: u32, y: u32) u32 {
+ var result = 0xca01f9dd *% x -% 0x4973f715 *% y;
+ result ^= result >> 16;
+ return result;
+}
+
+test "pcg64 draws match the release's recorded multipliers" {
+ // From exl3-2.9bpw/engram-token-map.bin, multipliers (m-1)/2 per layer.
+ var rng1 = Pcg64.init(10007);
+ try std.testing.expectEqual(@as(u64, 38316048023122), rng1.boundedBelow(46539438283891));
+ try std.testing.expectEqual(@as(u64, 2419938046656), rng1.boundedBelow(46539438283891));
+ try std.testing.expectEqual(@as(u64, 17979836159674), rng1.boundedBelow(46539438283891));
+ try std.testing.expectEqual(@as(u64, 36993668729195), rng1.boundedBelow(46539438283891));
+ var rng14 = Pcg64.init(140098);
+ try std.testing.expectEqual(@as(u64, 33858405369630), rng14.boundedBelow(46539438283891));
+ try std.testing.expectEqual(@as(u64, 25755403400457), rng14.boundedBelow(46539438283891));
+ try std.testing.expectEqual(@as(u64, 15460673601360), rng14.boundedBelow(46539438283891));
+ try std.testing.expectEqual(@as(u64, 41309613242795), rng14.boundedBelow(46539438283891));
+}
+
+test "the normalizer chain matches the release's recorded behavior" {
+ const a = std.testing.allocator;
+ const cases = [_]struct { in: []const u8, want: []const u8 }{
+ .{ .in = "é", .want = "e" },
+ .{ .in = "fi", .want = "fi" },
+ .{ .in = "İ", .want = "i" },
+ .{ .in = "가", .want = "가" },
+ .{ .in = "Äffin", .want = "affin" },
+ .{ .in = "Dž", .want = "dz" },
+ .{ .in = "ẞ", .want = "ß" },
+ .{ .in = " ", .want = " " },
+ .{ .in = " a ", .want = "a" },
+ .{ .in = "a\t\tb", .want = "a b" },
+ .{ .in = "ffl", .want = "ffl" },
+ .{ .in = "Ⅷ", .want = "viii" },
+ .{ .in = "½", .want = "1⁄2" },
+ .{ .in = "㍿", .want = "株式会社" },
+ .{ .in = "Ω", .want = "ω" },
+ .{ .in = "K", .want = "k" },
+ .{ .in = "a\u{0301}\u{0328}", .want = "a" },
+ .{ .in = "x\u{200b}\u{00ad}y", .want = "x\u{200b}\u{00ad}y" },
+ .{ .in = "😀", .want = "😀" },
+ };
+ for (cases) |c| {
+ const got = try normalizeKey(a, c.in);
+ defer a.free(got);
+ try std.testing.expectEqualStrings(c.want, got);
+ }
+}
+
+test "export manifest parses strictly with complete arrays and digests" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const a = arena.allocator();
+ const layer: Layer = .{ .layer_id = 1, .rows = 24, .primes = @splat(3), .offsets = @splat(7), .multipliers = .{ 11, 13, 17, 19 } };
+ const bytes = try writeManifest(a, @splat(1), @splat(2), @splat(3), 129280, 99092, 2, 4, 8, &.{layer});
+ const parsed = try std.json.parseFromSlice(std.json.Value, a, bytes, .{});
+ defer parsed.deinit();
+ const top = parsed.value.object;
+ const got = top.get("layers").?.array.items[0].object;
+ try std.testing.expectEqual(@as(usize, 24), got.get("primes").?.array.items.len);
+ try std.testing.expectEqual(@as(i64, 7), got.get("offsets").?.array.items[23].integer);
+ try std.testing.expectEqual(@as(i64, 19), got.get("multipliers").?.array.items[3].integer);
+ try std.testing.expectEqualStrings(&std.fmt.bytesToHex(@as([32]u8, @splat(1)), .lower), top.get("tokenizer_sha256").?.string);
+ try std.testing.expectEqualStrings(&std.fmt.bytesToHex(@as([32]u8, @splat(2)), .lower), top.get("config_sha256").?.string);
+ try std.testing.expectEqualStrings(&std.fmt.bytesToHex(@as([32]u8, @splat(3)), .lower), top.get("packed_sha256").?.string);
+}
+test "export CLI rejects duplicate and missing options before opening files" {
+ try std.testing.expectError(error.InvalidArguments, parseArgs(&.{ "tool", "--tokenizer", "A", "--tokenizer", "B", "--output", "C" }));
+ try std.testing.expectError(error.InvalidArguments, parseArgs(&.{ "tool", "--tokenizer", "A", "--config", "B", "--config", "C", "--output", "D" }));
+ try std.testing.expectError(error.InvalidArguments, parseArgs(&.{ "tool", "--tokenizer", "A" }));
+ const paths = try parseArgs(&.{ "tool", "--output", "C", "--config", "B", "--tokenizer", "A" });
+ try std.testing.expectEqualStrings("B", paths.config);
+}
+test "token exporter keeps owned lists and exact integral Config values" {
+ const a = std.testing.allocator;
+ const fixture = @embedFile("src/families/deepseek_v41/fixtures/config.json");
+ const exponent = try std.mem.replaceOwned(u8, a, fixture, "129280", "1.2928e5");
+ defer a.free(exponent);
+ const fractional = try std.mem.replaceOwned(u8, a, exponent, "\n 14", "\n 14.0");
+ defer a.free(fractional);
+ const Config = @import("src/families/deepseek_v41/config.zig").Config;
+ const want = try Config.parse(a, fixture);
+ const got = try Config.parse(a, fractional);
+ try std.testing.expectEqual(want.vocab, got.vocab);
+ try std.testing.expectEqual(want.engram_layers, got.engram_layers);
+ try std.testing.expectEqual(want.engram_rows, got.engram_rows);
+ try std.testing.expectEqual(want.engram_pad, got.engram_pad);
+}
diff --git a/zig/dsv41_transport_probe.zig b/zig/dsv41_transport_probe.zig
new file mode 100644
index 0000000000..8d92a94f6a
--- /dev/null
+++ b/zig/dsv41_transport_probe.zig
@@ -0,0 +1,45 @@
+//! CPU-only checks of the same framed exchanges used by DeepSeek MoE; no model, Metal device or RDMA is opened.
+const std = @import("std");
+const tf = @import("tensorfold");
+const clock = @import("metal").clock;
+const Transport = tf.deepseek_v41.tp_transport.Transport;
+
+pub fn main(init: std.process.Init) !void {
+ const args = try init.minimal.args.toSlice(init.arena.allocator());
+ if (args.len != 3) {
+ std.debug.print("usage: tf-dsv41-transport-probe TCP_SETTINGS ROUNDS\n", .{});
+ return error.Arguments;
+ }
+ const rounds = try std.fmt.parseInt(usize, args[2], 10);
+ if (rounds == 0 or rounds > 10000) return error.Arguments;
+ const json = try std.Io.Dir.cwd().readFileAlloc(init.io, args[1], init.arena.allocator(), .limited(65536));
+ const parsed = try std.json.parseFromSliceLeaky(std.json.Value, init.arena.allocator(), json, .{});
+ if (parsed != .object) return error.TcpRequired;
+ const kind = parsed.object.get("transport") orelse return error.TcpRequired;
+ if (kind != .string or !std.mem.eql(u8, kind.string, "tcp")) return error.TcpRequired;
+ var transport = try Transport.init(init.gpa, json, 256);
+ defer transport.deinit();
+ const rank = transport.channel.myRank();
+ const exchange = transport.exchange();
+ const sizes = [_]usize{ 48, 4096, 7 * 5120, 256 * 7 * 5120 };
+ const send = try init.gpa.alloc(u8, sizes[sizes.len - 1]);
+ defer init.gpa.free(send);
+ const receive = try init.gpa.alloc(u8, send.len);
+ defer init.gpa.free(receive);
+ var tag: u64 = 0;
+ for (sizes) |size| {
+ var elapsed: f64 = 0;
+ for (0..rounds + 4) |step| {
+ @memset(send[0..size], @truncate(step * 2 + rank));
+ const began = clock.seconds();
+ try exchange.call(exchange.ptr, tag, send[0..size], receive[0..size]);
+ const duration = clock.seconds() - began;
+ tag += 1;
+ if (step >= 4) elapsed += duration;
+ const expected: u8 = @truncate(step * 2 + 1 - rank);
+ if (!std.mem.allEqual(u8, receive[0..size], expected)) return error.PayloadMismatch;
+ }
+ std.debug.print("{{\"mode\":\"deepseek_transport_probe\",\"rank\":{d},\"bytes\":{d},\"rounds\":{d},\"exchange_seconds_total\":{d},\"payload_verified\":true}}\n", .{ rank, size, rounds, elapsed });
+ }
+ try transport.endpoint.tcp.finish();
+}
diff --git a/zig/exl3_projection_bench.zig b/zig/exl3_projection_bench.zig
index 5569cee9fd..b520ef5ca9 100644
--- a/zig/exl3_projection_bench.zig
+++ b/zig/exl3_projection_bench.zig
@@ -322,6 +322,7 @@ pub fn main(init: std.process.Init) !void {
// Keep the arithmetic reference independent of the optional prefill kernel.
if (p[0].prefill) |pf| pf.deinit();
p[0].prefill = null;
+ p[0].pairs_up_to_groups = 0;
try checkCodebooks(device, queue);
for ([_]ex.format.Codebook{ .inst3, .mcg, .mul1 }) |book| {
for ([_]u16{ 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16 }) |width| {
diff --git a/zig/experimental_family_test.zig b/zig/experimental_family_test.zig
new file mode 100644
index 0000000000..b938d46712
--- /dev/null
+++ b/zig/experimental_family_test.zig
@@ -0,0 +1,26 @@
+//! Release policy branches use explicit inputs without mutating the process environment.
+const std = @import("std");
+const policy = @import("sys").experimental;
+test "experimental family: DeepSeek defaults to refused until the exact opt-in" {
+ for ([_][]const u8{ "deepseek_v4", "deepseek_v41" }) |name| {
+ try std.testing.expect(policy.refusal(name, null) != null);
+ try std.testing.expectEqualStrings(policy.deepseek_message, policy.refusal(name, null).?);
+ for ([_][]const u8{ "", "1", "true", "DeepSeek", "deepseek,other" }) |value|
+ try std.testing.expect(policy.refusal(name, value) != null);
+ try std.testing.expect(policy.refusal(name, "deepseek") == null);
+ }
+ try std.testing.expect(policy.refusal("qwen3_5", null) == null);
+}
+
+test "experimental family: admission precedes CUDA model or asset work" {
+ const common = @embedFile("src/native/cuda_refusal.zig");
+ const shared = std.mem.indexOf(u8, common, "return deepseek_release_message;").?;
+ try std.testing.expect(shared < std.mem.indexOf(u8, common, "if (o.mtp_drafts)").?);
+ const server = @embedFile("src/families/deepseek_v41/serve_engine.zig");
+ const entry = std.mem.indexOf(u8, server, "pub fn open(").?;
+ const guard = std.mem.indexOfPos(u8, server, entry, "experimental.refusal(").?;
+ try std.testing.expect(guard < std.mem.indexOfPos(u8, server, entry, "TF_DSV41_ASSETS").?);
+ const model = @embedFile("src/families/deepseek_v41/cuda_model.zig");
+ const open = std.mem.indexOf(u8, model, "pub fn open(").?;
+ try std.testing.expect(std.mem.indexOfPos(u8, model, open, "experimental.refusal(").? < std.mem.indexOfPos(u8, model, open, "gpa.create(Model)").?);
+}
diff --git a/zig/flashnext_batch_tests.zig b/zig/flashnext_batch_tests.zig
index fa94863972..7db52b3236 100644
--- a/zig/flashnext_batch_tests.zig
+++ b/zig/flashnext_batch_tests.zig
@@ -1,3 +1,26 @@
test {
_ = @import("src/families/flashnext/backend.zig");
}
+
+test {
+ _ = @import("src/families/flashnext/grammar_host_test.zig");
+}
+
+test {
+ _ = @import("src/families/flashnext/vision_contract.zig");
+}
+
+test {
+ _ = @import("src/families/flashnext/window_config_test.zig");
+}
+
+test {
+ _ = @import("flashnext_rotary_bounds_tests.zig");
+ _ = @import("contract_bounds_host_test.zig");
+}
+
+test {
+ _ = @import("flashnext_precise_rotary_test.zig");
+ _ = @import("rotary_sweep_tests.zig");
+ _ = @import("rotary_receipt_host_test.zig");
+}
diff --git a/zig/flashnext_bound_family.zig b/zig/flashnext_bound_family.zig
new file mode 100644
index 0000000000..0b7fbf7d03
--- /dev/null
+++ b/zig/flashnext_bound_family.zig
@@ -0,0 +1,2 @@
+//! Family contract types for CPU-only testing of the real probe allowance callbacks.
+pub const flashnext_vision_contract = @import("src/families/flashnext/vision_contract.zig");
diff --git a/zig/flashnext_bound_probes_test.zig b/zig/flashnext_bound_probes_test.zig
new file mode 100644
index 0000000000..1e28e1b659
--- /dev/null
+++ b/zig/flashnext_bound_probes_test.zig
@@ -0,0 +1,105 @@
+//! Real probe callbacks without pipeline construction, GPU commands or readback substitution.
+const std = @import("std");
+const f = @import("tensorfold").flashnext_vision_contract;
+const c = @import("contract").spec;
+const prep = @import("tests/contract/metal_flashnext.zig");
+const pool = @import("tests/contract/metal_flashnext_pool.zig");
+const long = @import("tests/contract/flashnext_positions.zig");
+
+test "rotary probe: the sweep accumulator refuses skipped duplicated and incorrectly rounded cases" {
+ const cpu = @import("tests/contract/rotary_sweep_cpu.zig");
+ const accuracy = f.rotary_accuracy;
+ var row: accuracy.Row = .{ .role = .target, .mode = .normal };
+ var sample: cpu.Sample = .{ .frequency = 1, .angle = 0, .cosine = 1, .sine = 0, .co = 1, .si = 0 };
+ const table = accuracy.domain.table(.normal);
+ try std.testing.expectError(error.NoncontiguousRotarySweep, cpu.observe(&row, 1, sample, &table));
+ sample.angle = 1;
+ try std.testing.expectError(error.UnrecordedPhaseRounding, cpu.observe(&row, 0, sample, &table));
+ sample.angle = 0;
+ sample.co = 0;
+ try std.testing.expectError(error.UnrecordedCoefficientRounding, cpu.observe(&row, 0, sample, &table));
+ sample.co = 1;
+ try cpu.observe(&row, 0, sample, &table);
+ try std.testing.expectEqual(@as(u64, 1), row.cases);
+ try std.testing.expectError(error.NoncontiguousRotarySweep, cpu.observe(&row, 0, sample, &table));
+}
+
+test "rotary probe: real callbacks refuse an unrecorded deployment before buffer or device access" {
+ var low: [1]f64 = undefined;
+ var high: [1]f64 = undefined;
+ var p: prep.Probe = .{ .a = std.testing.allocator, .rig = undefined, .pipe = undefined, .which = .target };
+ try std.testing.expectError(error.UnrecordedRotaryCompiler, p.intervals(&low, &high));
+ var q: pool.Probe = .{ .a = std.testing.allocator, .rig = undefined, .pipe = undefined, .which = .pooled };
+ try std.testing.expectError(error.UnrecordedRotaryCompiler, q.intervals(&low, &high));
+}
+
+test "rotary probe: a one-ulp mutation of the actual bound table refuses before receipt lookup" {
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer arena.deinit();
+ const allocator = arena.allocator();
+ const metal = @import("metal");
+ metal.allocator = allocator;
+ defer metal.allocator = undefined;
+ var receipt: f.rotary_accuracy.Receipt = .{ .identity = undefined, .rows = &.{} };
+ var p: prep.Probe = .{ .a = allocator, .rig = .{ .a = allocator, .device = .{}, .queue = undefined }, .pipe = undefined, .which = .target, .accuracy = &receipt };
+ _ = try p.prepare(.{ .rows = 1, .n = f.output_width, .k = 256 }, .random, 7);
+ const word = p.rig.bytes(p.base, f.table_words * 4)[48..][0..4];
+ std.mem.writeInt(u32, word, std.mem.readInt(u32, word, .little) + 1, .little);
+ var low: [f.output_width]f64 = undefined;
+ var high: [f.output_width]f64 = undefined;
+ try std.testing.expectError(error.UnrecordedRotaryTable, p.intervals(&low, &high));
+ var q: pool.Probe = .{ .a = allocator, .rig = .{ .a = allocator, .device = .{}, .queue = undefined }, .pipe = undefined, .which = .pooled, .accuracy = &receipt };
+ _ = try q.prepare(.{ .rows = 1, .n = 128, .k = 128 }, .random, 7);
+ const other = q.rig.bytes(q.base, f.table_words * 4)[48..][0..4];
+ std.mem.writeInt(u32, other, std.mem.readInt(u32, other, .little) + 1, .little);
+ try std.testing.expectError(error.UnrecordedRotaryTable, q.intervals(low[0..128], high[0..128]));
+}
+
+test "rotary probe: long positions reach the full margin and pooling stays in the real allocation" {
+ for (0..8) |row| {
+ for (0..3) |axis| try std.testing.expect(long.token(row, axis) >= 0 and long.token(row, axis) < f.maximum);
+ }
+ try std.testing.expectEqual(@as(i32, 1048576), long.token(6, 0));
+ try std.testing.expectEqual(@as(i32, f.maximum - 1), long.token(7, 0));
+ for ([_]usize{ 1, 7, 16, 128 }) |rows| for ([_]f.Which{ .pooled, .absolute }) |which| {
+ const p: pool.Probe = .{ .a = std.testing.allocator, .rig = undefined, .pipe = undefined, .which = which, .long_positions = true, .shape = .{ .rows = rows, .n = 128, .k = 128 } };
+ try std.testing.expectEqual(f.maximum, 4 * (p.firstBlock() + rows));
+ if (rows == 16) try std.testing.expectEqual(@as(usize, 1048576), 4 * p.firstBlock());
+ };
+}
+test "rotary probe: Q K and indexer allowances cover every block with the witness row in its actual layout" {
+ const a = std.testing.allocator;
+ for ([_]f.Which{ .mrope, .target, .mtp }) |which| for ([_]usize{ 1, 7, 16, 128 }) |rows| {
+ if (which == .mtp and rows > 16) continue;
+ const host = try a.alloc(u16, rows * f.packed_width);
+ defer a.free(host);
+ for (host, 0..) |*h, i| h.* = c.toBf16(@as(f32, @floatFromInt((i + 2) % 17)) / 8 - 1);
+ const positions = try a.alloc(i32, rows * 3);
+ defer a.free(positions);
+ for (0..rows) |r| for (0..3) |axis| {
+ positions[r * 3 + axis] = @intCast(r * 17);
+ };
+ var p: prep.Probe = .{ .a = a, .rig = undefined, .pipe = undefined, .which = which, .host = host, .positions = positions, .shape = .{ .rows = rows, .n = f.output_width, .k = 256 } };
+ if (which != .mrope) for (0..rows) |r| {
+ positions[r] = @intCast(r * 17);
+ };
+ const allow = try a.alloc(f64, rows * f.output_width);
+ defer a.free(allow);
+ @memset(allow, std.math.nan(f64));
+ try p.conditionalBounds(&.{}, &.{}, allow);
+ for (allow) |v| try std.testing.expect(std.math.isFinite(v) and v >= 0);
+ if (rows == 128) try std.testing.expect(allow[50 * 24 * 256 + 33] >= 0.000011444091796875);
+ };
+}
+test "rotary probe: both pooled callbacks include the absolute block prefix without widening its guard" {
+ const a = std.testing.allocator;
+ for ([_]f.Which{ .pooled, .absolute }) |which| for ([_]f64{ 0, 4 }) |factor| {
+ const host = try a.alloc(u16, (7 + 128) * 4 * 128);
+ defer a.free(host);
+ for (host, 0..) |*h, i| h.* = c.toBf16(@as(f32, @floatFromInt((i + 11) % 17)) / 8 - 1);
+ var p: pool.Probe = .{ .a = a, .rig = undefined, .pipe = undefined, .which = which, .factor = factor, .host = host, .shape = .{ .rows = 128, .n = 128, .k = 128 } };
+ var allow: [128 * 128]f64 = @splat(std.math.nan(f64));
+ try p.conditionalBounds(&.{}, &.{}, &allow);
+ for (allow) |v| try std.testing.expect(std.math.isFinite(v) and v >= 0);
+ };
+}
diff --git a/zig/flashnext_legacy_compile.zig b/zig/flashnext_legacy_compile.zig
new file mode 100644
index 0000000000..f901c96c45
--- /dev/null
+++ b/zig/flashnext_legacy_compile.zig
@@ -0,0 +1,4 @@
+//! Analyze the real legacy host's constructor without linking or running GPU work.
+export fn compileLegacy() usize {
+ return @intFromPtr(&@import("host").openWarmed);
+}
diff --git a/zig/flashnext_precise_golden.zig b/zig/flashnext_precise_golden.zig
new file mode 100644
index 0000000000..d2c09ed727
--- /dev/null
+++ b/zig/flashnext_precise_golden.zig
@@ -0,0 +1,99 @@
+pub const Case = struct { position: i32, index: usize, upper: bool, bf16: u16 };
+pub const cases = [_]Case{
+ .{ .position = 0, .index = 0, .upper = false, .bf16 = 0x3f80 },
+ .{ .position = 0, .index = 0, .upper = true, .bf16 = 0x3f80 },
+ .{ .position = 0, .index = 1, .upper = false, .bf16 = 0x3f80 },
+ .{ .position = 0, .index = 1, .upper = true, .bf16 = 0x3f80 },
+ .{ .position = 0, .index = 2, .upper = false, .bf16 = 0x3f80 },
+ .{ .position = 0, .index = 2, .upper = true, .bf16 = 0x3f80 },
+ .{ .position = 0, .index = 8, .upper = false, .bf16 = 0x3f80 },
+ .{ .position = 0, .index = 8, .upper = true, .bf16 = 0x3f80 },
+ .{ .position = 0, .index = 12, .upper = false, .bf16 = 0x3f80 },
+ .{ .position = 0, .index = 12, .upper = true, .bf16 = 0x3f80 },
+ .{ .position = 0, .index = 31, .upper = false, .bf16 = 0x3f80 },
+ .{ .position = 0, .index = 31, .upper = true, .bf16 = 0x3f80 },
+ .{ .position = 1, .index = 0, .upper = false, .bf16 = 0xbe9a },
+ .{ .position = 1, .index = 0, .upper = true, .bf16 = 0x3fb1 },
+ .{ .position = 1, .index = 1, .upper = false, .bf16 = 0x3e82 },
+ .{ .position = 1, .index = 1, .upper = true, .bf16 = 0x3fb2 },
+ .{ .position = 1, .index = 2, .upper = false, .bf16 = 0x3f14 },
+ .{ .position = 1, .index = 2, .upper = true, .bf16 = 0x3fa5 },
+ .{ .position = 1, .index = 8, .upper = false, .bf16 = 0x3f7b },
+ .{ .position = 1, .index = 8, .upper = true, .bf16 = 0x3f82 },
+ .{ .position = 1, .index = 12, .upper = false, .bf16 = 0x3f7f },
+ .{ .position = 1, .index = 12, .upper = true, .bf16 = 0x3f80 },
+ .{ .position = 1, .index = 31, .upper = false, .bf16 = 0x3f80 },
+ .{ .position = 1, .index = 31, .upper = true, .bf16 = 0x3f80 },
+ .{ .position = 850, .index = 0, .upper = false, .bf16 = 0xbf97 },
+ .{ .position = 850, .index = 0, .upper = true, .bf16 = 0x3f48 },
+ .{ .position = 850, .index = 1, .upper = false, .bf16 = 0x3f80 },
+ .{ .position = 850, .index = 1, .upper = true, .bf16 = 0xbf80 },
+ .{ .position = 850, .index = 2, .upper = false, .bf16 = 0xbfb3 },
+ .{ .position = 850, .index = 2, .upper = true, .bf16 = 0xbe6f },
+ .{ .position = 850, .index = 8, .upper = false, .bf16 = 0xbfb2 },
+ .{ .position = 850, .index = 8, .upper = true, .bf16 = 0xbe8b },
+ .{ .position = 850, .index = 12, .upper = false, .bf16 = 0xbfab },
+ .{ .position = 850, .index = 12, .upper = true, .bf16 = 0x3ef2 },
+ .{ .position = 850, .index = 31, .upper = false, .bf16 = 0x3f80 },
+ .{ .position = 850, .index = 31, .upper = true, .bf16 = 0x3f80 },
+ .{ .position = 262143, .index = 0, .upper = false, .bf16 = 0xbfb3 },
+ .{ .position = 262143, .index = 0, .upper = true, .bf16 = 0x3e3c },
+ .{ .position = 262143, .index = 1, .upper = false, .bf16 = 0x3f07 },
+ .{ .position = 262143, .index = 1, .upper = true, .bf16 = 0x3fa8 },
+ .{ .position = 262143, .index = 2, .upper = false, .bf16 = 0xbf11 },
+ .{ .position = 262143, .index = 2, .upper = true, .bf16 = 0xbfa6 },
+ .{ .position = 262143, .index = 8, .upper = false, .bf16 = 0x3fad },
+ .{ .position = 262143, .index = 8, .upper = true, .bf16 = 0x3ed4 },
+ .{ .position = 262143, .index = 12, .upper = false, .bf16 = 0x3fa7 },
+ .{ .position = 262143, .index = 12, .upper = true, .bf16 = 0x3f09 },
+ .{ .position = 262143, .index = 31, .upper = false, .bf16 = 0x3f75 },
+ .{ .position = 262143, .index = 31, .upper = true, .bf16 = 0x3f85 },
+ .{ .position = 262144, .index = 0, .upper = false, .bf16 = 0xbf6a },
+ .{ .position = 262144, .index = 0, .upper = true, .bf16 = 0xbf8a },
+ .{ .position = 262144, .index = 1, .upper = false, .bf16 = 0xbe9f },
+ .{ .position = 262144, .index = 1, .upper = true, .bf16 = 0x3fb1 },
+ .{ .position = 262144, .index = 2, .upper = false, .bf16 = 0xbd87 },
+ .{ .position = 262144, .index = 2, .upper = true, .bf16 = 0xbfb5 },
+ .{ .position = 262144, .index = 8, .upper = false, .bf16 = 0x3fac },
+ .{ .position = 262144, .index = 8, .upper = true, .bf16 = 0x3ee0 },
+ .{ .position = 262144, .index = 12, .upper = false, .bf16 = 0x3fa7 },
+ .{ .position = 262144, .index = 12, .upper = true, .bf16 = 0x3f0a },
+ .{ .position = 262144, .index = 31, .upper = false, .bf16 = 0x3f75 },
+ .{ .position = 262144, .index = 31, .upper = true, .bf16 = 0x3f85 },
+ .{ .position = 1048575, .index = 0, .upper = false, .bf16 = 0x3fb4 },
+ .{ .position = 1048575, .index = 0, .upper = true, .bf16 = 0x3e31 },
+ .{ .position = 1048575, .index = 1, .upper = false, .bf16 = 0xbf2f },
+ .{ .position = 1048575, .index = 1, .upper = true, .bf16 = 0xbf9e },
+ .{ .position = 1048575, .index = 2, .upper = false, .bf16 = 0xbfb0 },
+ .{ .position = 1048575, .index = 2, .upper = true, .bf16 = 0xbea8 },
+ .{ .position = 1048575, .index = 8, .upper = false, .bf16 = 0x3f20 },
+ .{ .position = 1048575, .index = 8, .upper = true, .bf16 = 0xbfa2 },
+ .{ .position = 1048575, .index = 12, .upper = false, .bf16 = 0x3f7e },
+ .{ .position = 1048575, .index = 12, .upper = true, .bf16 = 0xbf81 },
+ .{ .position = 1048575, .index = 31, .upper = false, .bf16 = 0x3f50 },
+ .{ .position = 1048575, .index = 31, .upper = true, .bf16 = 0x3f94 },
+ .{ .position = 1048576, .index = 0, .upper = false, .bf16 = 0x3f1d },
+ .{ .position = 1048576, .index = 0, .upper = true, .bf16 = 0x3fa3 },
+ .{ .position = 1048576, .index = 1, .upper = false, .bf16 = 0x3e10 },
+ .{ .position = 1048576, .index = 1, .upper = true, .bf16 = 0xbfb4 },
+ .{ .position = 1048576, .index = 2, .upper = false, .bf16 = 0xbf96 },
+ .{ .position = 1048576, .index = 2, .upper = true, .bf16 = 0xbf4c },
+ .{ .position = 1048576, .index = 8, .upper = false, .bf16 = 0x3f26 },
+ .{ .position = 1048576, .index = 8, .upper = true, .bf16 = 0xbfa1 },
+ .{ .position = 1048576, .index = 12, .upper = false, .bf16 = 0x3f7f },
+ .{ .position = 1048576, .index = 12, .upper = true, .bf16 = 0xbf81 },
+ .{ .position = 1048576, .index = 31, .upper = false, .bf16 = 0x3f50 },
+ .{ .position = 1048576, .index = 31, .upper = true, .bf16 = 0x3f94 },
+ .{ .position = 1048639, .index = 0, .upper = false, .bf16 = 0x3ec8 },
+ .{ .position = 1048639, .index = 0, .upper = true, .bf16 = 0x3fae },
+ .{ .position = 1048639, .index = 1, .upper = false, .bf16 = 0x3f24 },
+ .{ .position = 1048639, .index = 1, .upper = true, .bf16 = 0xbfa1 },
+ .{ .position = 1048639, .index = 2, .upper = false, .bf16 = 0xbd77 },
+ .{ .position = 1048639, .index = 2, .upper = true, .bf16 = 0x3fb5 },
+ .{ .position = 1048639, .index = 8, .upper = false, .bf16 = 0x3fb5 },
+ .{ .position = 1048639, .index = 8, .upper = true, .bf16 = 0x3d0d },
+ .{ .position = 1048639, .index = 12, .upper = false, .bf16 = 0x3f91 },
+ .{ .position = 1048639, .index = 12, .upper = true, .bf16 = 0xbf58 },
+ .{ .position = 1048639, .index = 31, .upper = false, .bf16 = 0x3f50 },
+ .{ .position = 1048639, .index = 31, .upper = true, .bf16 = 0x3f94 },
+};
diff --git a/zig/flashnext_precise_rotary_test.zig b/zig/flashnext_precise_rotary_test.zig
new file mode 100644
index 0000000000..21214c7a38
--- /dev/null
+++ b/zig/flashnext_precise_rotary_test.zig
@@ -0,0 +1,41 @@
+//! Explicit rotary math policy and independent long-position reference vectors, without Metal execution.
+const std = @import("std");
+const f = @import("src/families/flashnext/vision_contract.zig");
+const golden = @import("flashnext_precise_golden.zig");
+
+test "precise rotary: every live source role selects precise exp2, sine and cosine" {
+ for ([_]f.Which{ .mrope, .target, .mtp, .pooled, .absolute }) |which| {
+ const source = f.source(which);
+ for ([_][]const u8{ "metal::precise::exp2(", "metal::precise::sin(", "metal::precise::cos(" }) |call| {
+ try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, source, call));
+ }
+ for ([_][]const u8{ "metal::fast::sin(", "metal::fast::cos(", "metal::exp2(" }) |call| {
+ try std.testing.expectEqual(@as(usize, 0), std.mem.count(u8, source, call));
+ }
+ }
+}
+
+test "precise rotary: FP64 reference agrees with independent long-position golden values" {
+ const input: [128]u16 = @splat(0x3f80);
+ const weights: [128]f32 = @splat(1);
+ const rotary = f.table(23.253496170043945, 0);
+ for (golden.cases) |case| {
+ var output: [128]u16 = undefined;
+ f.normalizedRotation(&input, @splat(case.position), &weights, 0, &rotary, .f64, .sequential, &output, null);
+ try std.testing.expectEqual(case.bf16, output[case.index + @as(usize, if (case.upper) 32 else 0)]);
+ }
+}
+
+test "precise rotary: conditional allowance accepts the full source capacity and refuses its end" {
+ const input: [128]u16 = @splat(0x3f80);
+ const weights: [128]f32 = @splat(1);
+ var allow: [128]f64 = undefined;
+ for ([_]f64{ 0, 4 }) |factor| {
+ const rotary = f.table(23.253496170043945, factor);
+ for ([_]i32{ 1048575, 1048576, 1048639 }) |position| {
+ try f.normalizedBounds(&input, @splat(position), &weights, 1e-5, &rotary, &allow);
+ for (allow) |v| try std.testing.expect(std.math.isFinite(v) and v >= 0);
+ }
+ try std.testing.expectError(error.InvalidVisionBoundInput, f.normalizedBounds(&input, @splat(1048640), &weights, 1e-5, &rotary, &allow));
+ }
+}
diff --git a/zig/flashnext_rotary_bounds_tests.zig b/zig/flashnext_rotary_bounds_tests.zig
new file mode 100644
index 0000000000..5d84112694
--- /dev/null
+++ b/zig/flashnext_rotary_bounds_tests.zig
@@ -0,0 +1,124 @@
+//! Scalar phase and rounding controls for the conditional rotary probe allowance.
+const std = @import("std");
+const f = @import("src/families/flashnext/vision_contract.zig");
+const c = @import("contract").spec;
+fn fp32(input: []const u16, positions: [3]i32, weights: []const f32, rotary: []const f32, output: []u16) void {
+ @setFloatMode(.strict);
+ var total: f32 = 0;
+ for (input) |h| {
+ const x: f32 = c.fromBf16(h);
+ total += x * x;
+ }
+ const denominator: f32 = total / @as(f32, @floatFromInt(input.len)) + @as(f32, 1e-5);
+ const inv: f32 = @floatCast(1 / @sqrt(@as(f64, denominator)));
+ var normed: [256]f32 = undefined;
+ for (input, weights, 0..) |h, w, i| {
+ const product: f32 = c.fromBf16(h) * inv;
+ normed[i] = c.fromBf16(c.toBf16(product * w));
+ }
+ for (input, 0..) |_, d| {
+ var value = normed[d];
+ if (d < 64) {
+ const i = d % 32;
+ const exponent: f32 = -(@as(f32, @floatFromInt(i)) / 32) * rotary[0];
+ const freq: f32 = if (rotary[1] != 0) rotary[3 + i] else @floatCast(@exp2(@as(f64, exponent)));
+ const angle: f32 = @as(f32, @floatFromInt(positions[f.axis(i)])) * freq;
+ const scale: f32 = if (rotary[1] != 0) rotary[2] else 1;
+ const co: f32 = @as(f32, @floatCast(@cos(@as(f64, angle)))) * scale;
+ const si: f32 = @as(f32, @floatCast(@sin(@as(f64, angle)))) * scale;
+ const left: f32 = if (d < 32) normed[d] * co else normed[d - 32] * si;
+ const right: f32 = if (d < 32) normed[d + 32] * si else normed[d] * co;
+ value = if (d < 32) left - right else left + right;
+ }
+ output[d] = c.toBf16(value);
+ }
+}
+
+fn witness(factor: f64, seed: usize, row: usize, head: usize, d: usize) !void {
+ const width: usize = if (head < 26) 256 else 128;
+ const source: usize = if (head < 24) head * 512 else if (head < 26) 24 * 512 + (head - 24) * 256 else 24 * 512 + 4 * 256 + (head - 26) * 128;
+ const weight: f32 = if (head < 24) 1 else if (head < 26) 0.75 else 1.25;
+ var input: [256]u16 = undefined;
+ const weights: [256]f32 = @splat(weight);
+ for (input[0..width], 0..) |*h, i| h.* = c.toBf16(@as(f32, @floatFromInt((row * f.packed_width + source + i + seed) % 17)) / 8 - 1);
+ const pos: [3]i32 = @splat(@intCast((if (factor > 1) @as(usize, 262140) else 0) + row * 17));
+ const rotary = f.table(23.253496170043945, factor);
+ var want: [256]u16 = undefined;
+ var got: [256]u16 = undefined;
+ var mag: [256]f64 = undefined;
+ var allow: [256]f64 = undefined;
+ f.normalizedRotation(input[0..width], pos, weights[0..width], 1e-5, &rotary, .f64, .sequential, want[0..width], mag[0..width]);
+ fp32(input[0..width], pos, weights[0..width], &rotary, got[0..width]);
+ if (@hasDecl(f, "normalizedBounds")) {
+ try f.normalizedBounds(input[0..width], pos, weights[0..width], 1e-5, &rotary, allow[0..width]);
+ } else {
+ for (want[0..width], mag[0..width], allow[0..width]) |h, m, *a| a.* = c.bound(f.spec(c, .target), .{ .rows = 128, .n = f.output_width, .k = 256 }, c.fromBf16(h), m);
+ }
+ const error_observed = @abs(@as(f64, c.fromBf16(got[d])) - c.fromBf16(want[d]));
+ try std.testing.expect(error_observed <= allow[d]);
+ try std.testing.expect(@abs(2 * @as(f64, c.fromBf16(want[d]))) > allow[d]);
+ for (want[0..width], got[0..width], allow[0..width]) |w, g, a| try std.testing.expect(@abs(@as(f64, c.fromBf16(w)) - c.fromBf16(g)) <= a);
+}
+test "rotary bound: the original plain and scaled phase witnesses fit without accepting a sign change" {
+ try witness(0, 2, 50, 0, 33);
+ try witness(4, 11, 10, 26, 12);
+}
+test "rotary bound: every residue, all coordinate axes and context boundaries enclose the FP32 source stages" {
+ const weights: [128]f32 = @splat(1.25);
+ for ([_]f64{ 0, 4 }) |factor| for (0..17) |seed| for ([_]i32{ 0, 1, 262143, 262144, 1048575 }) |position| {
+ var p: [128]u16 = undefined;
+ for (&p, 0..) |*h, d| h.* = c.toBf16(@as(f32, @floatFromInt((d + seed) % 17)) / 8 - 1);
+ const pos: [3]i32 = .{ position, @max(position - 3, 0), @max(position - 7, 0) };
+ const rotary = f.table(23.253496170043945, factor);
+ var want: [128]u16 = undefined;
+ var got: [128]u16 = undefined;
+ var allow: [128]f64 = undefined;
+ f.normalizedRotation(&p, pos, &weights, 1e-5, &rotary, .f64, .sequential, &want, null);
+ fp32(&p, pos, &weights, &rotary, &got);
+ try f.normalizedBounds(&p, pos, &weights, 1e-5, &rotary, &allow);
+ for (want, got, allow) |w, actual, e| {
+ try std.testing.expect(std.math.isFinite(e) and e >= 0);
+ try std.testing.expect(@abs(@as(f64, c.fromBf16(actual)) - c.fromBf16(w)) <= e);
+ }
+ };
+}
+test "rotary bound: normalization midpoint crossings survive endpoint rounding" {
+ const p: [128]u16 = @splat(0x3f80);
+ const w: [128]f32 = @splat(1.00390625);
+ const rotary = f.table(23.253496170043945, 0);
+ var allow: [128]f64 = undefined;
+ try f.normalizedBounds(&p, .{ 0, 0, 0 }, &w, 1e-30, &rotary, &allow);
+ try std.testing.expect(allow[64] >= 0.0078125);
+ try std.testing.expect(allow[64] < 0.015625);
+ try std.testing.expectError(error.InvalidVisionBoundInput, f.normalizedBounds(&p, .{ f.maximum, 0, 0 }, &w, 1e-5, &rotary, &allow));
+}
+test "rotary bound: wrong axes and a wrong rotary scale still fail" {
+ const p: [128]u16 = @splat(0x3f80);
+ const w: [128]f32 = @splat(1.25);
+ const rotary = f.table(23.253496170043945, 4);
+ var wrong = rotary;
+ wrong[2] *= 2;
+ var want: [128]u16 = undefined;
+ var got: [128]u16 = undefined;
+ var allow: [128]f64 = undefined;
+ f.normalizedRotation(&p, .{ 3, 5, 7 }, &w, 1e-5, &rotary, .f64, .sequential, &want, null);
+ try f.normalizedBounds(&p, .{ 3, 5, 7 }, &w, 1e-5, &rotary, &allow);
+ fp32(&p, .{ 3, 3, 3 }, &w, &rotary, &got);
+ try std.testing.expect(@abs(@as(f64, c.fromBf16(got[1])) - c.fromBf16(want[1])) > allow[1]);
+ fp32(&p, .{ 3, 5, 7 }, &w, &wrong, &got);
+ try std.testing.expect(@abs(@as(f64, c.fromBf16(got[0])) - c.fromBf16(want[0])) > allow[0]);
+}
+test "rotary bound: an unsupported fixture cannot silently obtain a wider allowance" {
+ var p: [128]u16 = @splat(0x3f80);
+ const weights: [128]f32 = @splat(1.25);
+ const rotary = f.table(23.253496170043945, 0);
+ var allow: [128]f64 = undefined;
+ p[0] = c.toBf16(0.1);
+ try std.testing.expectError(error.UnsupportedVisionBoundFixture, f.normalizedBounds(&p, .{ 0, 0, 0 }, &weights, 1e-5, &rotary, &allow));
+ p[0] = 0x3f80;
+ try std.testing.expectError(error.InvalidVisionBoundInput, f.normalizedBounds(&p, .{ 0, 0, 0 }, &weights, 0, &rotary, &allow));
+ var raw: [4 * 128]u16 = @splat(0);
+ raw[0] = c.toBf16(1);
+ raw[128] = c.toBf16(0x1p-30);
+ try std.testing.expectError(error.InexactPoolBoundFixture, f.pooledBounds(&raw, 0, &weights, 1e-5, &rotary, &allow));
+}
diff --git a/zig/flashnext_window_compile.zig b/zig/flashnext_window_compile.zig
new file mode 100644
index 0000000000..2d67d980e2
--- /dev/null
+++ b/zig/flashnext_window_compile.zig
@@ -0,0 +1,4 @@
+//! Analyze the actual context entry without linking or running a device.
+export fn compileWindowOpen() usize {
+ return @intFromPtr(&@import("native").openFlashNext);
+}
diff --git a/zig/glm_admission_host.zig b/zig/glm_admission_host.zig
new file mode 100644
index 0000000000..ffb9bd1dd4
--- /dev/null
+++ b/zig/glm_admission_host.zig
@@ -0,0 +1,151 @@
+//! The real worker with production GLM admission capabilities and fake prompt/decode forwards.
+const std = @import("std");
+const glm = @import("glm");
+const lanes = @import("lanes");
+const api = @import("engine_api");
+const a = std.testing.allocator;
+const Stream = lanes.Stream;
+const Backend = lanes.backend.Backend;
+const Choice = union(enum) { prompt: usize, decode };
+
+const Fake = struct {
+ target: lanes.fake.Fake = .{ .gpa = a },
+ table: Backend.VTable = undefined,
+ choices: std.ArrayList(Choice) = .empty,
+ ready: std.atomic.Value(bool) = .init(false),
+ go: std.atomic.Value(bool) = .init(false),
+ batches: usize = 0,
+
+ fn backend(f: *Fake) Backend {
+ var real: glm.lanes.Lanes = undefined;
+ real.whole = false;
+ const caps = real.backend().vtable;
+ f.table = f.target.backend().vtable.*;
+ f.table.prefill = whole;
+ f.table.prefill_step = if (caps.prefill_step != null) step else null;
+ f.table.prefill_many = if (caps.prefill_many != null) many else null;
+ f.table.many_rows = caps.many_rows;
+ f.table.verify = verify;
+ return .{ .ptr = &f.target, .vtable = &f.table };
+ }
+
+ fn from(ptr: *anyopaque) *Fake {
+ const target: *lanes.fake.Fake = @ptrCast(@alignCast(ptr));
+ return @fieldParentPtr("target", target);
+ }
+
+ fn whole(ptr: *anyopaque, s: *Stream) anyerror!void {
+ const f = from(ptr);
+ if (s.prompt_len != 4) {
+ if (!glm.lanes.canBatch(s)) return error.IneligibleFreshPrompt;
+ try f.choices.append(a, .{ .prompt = s.prompt_len });
+ }
+ try f.target.backend().vtable.prefill(ptr, s);
+ }
+
+ fn step(ptr: *anyopaque, s: *Stream) anyerror!bool {
+ if (s.prompt_len > glm.engine.prompt_rows) return error.UnboundedPromptChoice;
+ try whole(ptr, s);
+ return true;
+ }
+
+ fn many(ptr: *anyopaque, streams: []const *Stream) anyerror!bool {
+ from(ptr).batches += 1;
+ for (streams) |s| try whole(ptr, s);
+ return true;
+ }
+
+ fn verify(ptr: *anyopaque, windows: []const lanes.backend.Window, out: []lanes.backend.Verified) anyerror!void {
+ const f = from(ptr);
+ for (windows) |w| if (w.stream.prompt_len == 4) {
+ if (!f.ready.load(.acquire)) {
+ f.ready.store(true, .release);
+ while (!f.go.load(.acquire)) std.Thread.yield() catch {};
+ }
+ try f.choices.append(a, .decode);
+ };
+ try f.target.backend().vtable.verify(ptr, windows, out);
+ }
+
+ fn deinit(f: *Fake) void {
+ f.target.deinit();
+ f.choices.deinit(a);
+ }
+};
+
+const Box = struct {
+ done: std.atomic.Value(u32) = .init(0),
+ bad: bool = false,
+
+ fn event(ptr: *anyopaque, _: api.Id, e: *const api.Event) void {
+ const b: *Box = @ptrCast(@alignCast(ptr));
+ if (e.* == .finished) {
+ if (e.finished.reason != .length) b.bad = true;
+ _ = b.done.fetchAdd(1, .release);
+ }
+ }
+};
+
+fn wait(value: anytype, wanted: anytype) !void {
+ const start = std.Io.Clock.awake.now(std.testing.io).toNanoseconds();
+ while (value.load(.acquire) != wanted) {
+ if (std.Io.Clock.awake.now(std.testing.io).toNanoseconds() - start > 5 * std.time.ns_per_s)
+ return error.WorkerTimedOut;
+ std.Io.sleep(std.testing.io, .fromMilliseconds(1), .awake) catch {};
+ }
+}
+
+test "GLM CUDA admission: a live decoder runs between three eligible fresh prompt choices" {
+ var f: Fake = .{};
+ defer f.deinit();
+ var cfg = try lanes.Config.init(a, .{ .exact_width = 1 }, 8, 7);
+ defer cfg.deinit(a);
+ var clock: lanes.fake.FixedClock = .{};
+ var core = lanes.Engine.init(a, &cfg, f.backend(), clock.clock());
+ defer core.deinit();
+ var host = api.LaneHost.init(a, std.testing.io, &core, .{ .lanes = 4 });
+ var box: Box = .{};
+ const engine = host.engine();
+ const first: api.Request = .{ .prompt = &.{ 3, 1, 4, 1 }, .max_tokens = 32, .drafts = false };
+ try engine.submit(1, &first, .{ .ctx = &box, .event = Box.event });
+ try host.start();
+ var stopped = false;
+ defer {
+ f.go.store(true, .release);
+ if (!stopped) host.stop();
+ }
+ try wait(&f.ready, true);
+ var prompts: [3][1536]u32 = undefined;
+ for (&prompts, 0..) |*prompt, i| {
+ @memset(prompt, @intCast(i + 1));
+ const request: api.Request = .{ .prompt = prompt, .max_tokens = 8, .drafts = false };
+ try engine.submit(@intCast(i + 2), &request, .{ .ctx = &box, .event = Box.event });
+ }
+ f.go.store(true, .release);
+ try wait(&box.done, @as(u32, 4));
+ host.stop();
+ stopped = true;
+ try std.testing.expect(!box.bad);
+ var prompts_seen: usize = 0;
+ var interleaved = false;
+ for (f.choices.items) |choice| switch (choice) {
+ .prompt => |rows| {
+ try std.testing.expect(rows <= glm.engine.prompt_rows);
+ prompts_seen += 1;
+ },
+ .decode => if (prompts_seen == 1) {
+ interleaved = true;
+ },
+ };
+ try std.testing.expectEqual(@as(usize, 3), prompts_seen);
+ try std.testing.expect(interleaved);
+ try std.testing.expectEqual(@as(usize, 0), f.batches);
+}
+
+test "GLM CUDA admission: whole-prompt mode retains its batch callback" {
+ var real: glm.lanes.Lanes = undefined;
+ real.whole = true;
+ const caps = real.backend().vtable;
+ try std.testing.expect(caps.prefill_many != null);
+ try std.testing.expect(caps.prefill_step == null);
+}
diff --git a/zig/glm_mm_policy_host.zig b/zig/glm_mm_policy_host.zig
new file mode 100644
index 0000000000..c71dc69bdd
--- /dev/null
+++ b/zig/glm_mm_policy_host.zig
@@ -0,0 +1,46 @@
+const std = @import("std");
+const cuda = @import("cuda");
+const mm = @import("mm");
+const kernels: mm.Kernels = .{
+ .by_rows = .{ .{ .id = 16 }, .{ .id = 32 }, .{ .id = 64 }, .{ .id = 128 } },
+ .packed_rows = @splat(.{ .{ .id = 16 }, .{ .id = 32 }, .{ .id = 64 }, .{ .id = 128 } }),
+ .prompt = .{ .id = 512 },
+ .reduce = .{ .id = 1024 },
+ .tile = .{},
+ .tile_packed = @splat(.{}),
+ .pdl = false,
+};
+test "verify and draft windows keep shape-selected split K at every width" {
+ for ([_]u8{ 16, 4, 6, 8 }) |bits| for ([_]usize{ 1, 2, 3, 4, 8, 16, 17, 31, 32 }) |rows| {
+ cuda.count = 0;
+ try mm.matmul(&kernels, .{}, 4096, 2048, .{ .w = 8192, .s = 12288, .n = 4096, .k = 2048, .bits = bits }, 16384, 32768, rows, false, false);
+ try std.testing.expectEqual(@as(usize, 2), cuda.count);
+ try std.testing.expectEqual(@as(u32, 4), cuda.configs[0].grid.z);
+ try std.testing.expectEqual(@as(u32, 1024), cuda.symbols[1]);
+ };
+}
+test "prompt flag chooses one K slice even for one row, alone or many" {
+ for ([_]u8{ 16, 4, 6, 8 }) |bits| for ([_]usize{ 1, 3, 16, 64, 127, 128, 129, 2048 }) |rows| {
+ cuda.count = 0;
+ try mm.matmul(&kernels, .{}, 4096, 2048, .{ .w = 8192, .s = 12288, .n = 4096, .k = 2048, .bits = bits }, 16384, 32768, rows, false, true);
+ try std.testing.expectEqual(@as(usize, 1), cuda.count);
+ try std.testing.expectEqual(@as(u32, 1), cuda.configs[0].grid.z);
+ try std.testing.expectEqual(@as(u32, 256), cuda.configs[0].block.x);
+ };
+}
+
+test "fixed K prompt experiment uses the same shape split across bucket, offset and tail boundaries" {
+ var k = kernels;
+ k.prompt_fixed_k = true;
+ for ([_]u8{ 16, 4, 6, 8 }) |bits| for ([_]usize{ 1, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 2048 }) |rows| {
+ cuda.count = 0;
+ try mm.matmul(&k, .{}, 4096 + 7 * 2048 * 2, 2048, .{ .w = 8192, .s = 12288, .n = 4096, .k = 2048, .bits = bits }, 16384, 32768, rows, false, true);
+ try std.testing.expectEqual(@as(usize, 2 * ((rows + 63) / 64)), cuda.count);
+ var i: usize = 0;
+ while (i < cuda.count) : (i += 2) {
+ try std.testing.expectEqual(@as(u32, 4), cuda.configs[i].grid.z);
+ try std.testing.expectEqual(@as(u32, 1024), cuda.symbols[i + 1]);
+ }
+ };
+ try std.testing.expect(!kernels.prompt_fixed_k);
+}
diff --git a/zig/glm_prompt_arena_host.zig b/zig/glm_prompt_arena_host.zig
new file mode 100644
index 0000000000..a9d86a76ad
--- /dev/null
+++ b/zig/glm_prompt_arena_host.zig
@@ -0,0 +1,57 @@
+//! Actual State allocation puts the retained prompt row inside each charged sequence arena.
+const std = @import("std");
+const cuda = @import("cuda");
+const glm = @import("glm");
+var allocations: [4]?[]align(256) u8 = @splat(null);
+fn alloc(ptr: *u64, n: usize) callconv(.c) cuda.abi.Result {
+ const bytes = std.testing.allocator.alignedAlloc(u8, .fromByteUnits(256), n) catch return 2;
+ for (&allocations) |*entry| if (entry.* == null) {
+ entry.* = bytes;
+ ptr.* = @intFromPtr(bytes.ptr);
+ return 0;
+ };
+ std.testing.allocator.free(bytes);
+ return 2;
+}
+fn free(ptr: u64) callconv(.c) cuda.abi.Result {
+ for (&allocations) |*entry| if (entry.*) |bytes| if (@intFromPtr(bytes.ptr) == ptr) {
+ std.testing.allocator.free(bytes);
+ entry.* = null;
+ return 0;
+ };
+ return 1;
+}
+fn fill(ptr: u64, value: u8, n: usize) callconv(.c) cuda.abi.Result {
+ const bytes: [*]u8 = @ptrFromInt(ptr);
+ @memset(bytes[0..n], value);
+ return 0;
+}
+test "GLM prompt arena: two actual sequence allocations own disjoint, charged prompt rows" {
+ const before = cuda.memory.usage(false).device;
+ var d: cuda.Driver = undefined;
+ d.api.cuMemAlloc_v2 = alloc;
+ d.api.cuMemFree_v2 = free;
+ d.api.cuMemsetD8_v2 = fill;
+ var w: glm.weights.Weights = undefined;
+ w.config = .{ .hidden = 16 };
+ w.share = try glm.shard.Share.of(&w.config, 0, 1);
+ w.layers = &.{};
+ var a = try glm.forward.State.init(std.testing.allocator, &d, &w, 32);
+ defer a.deinit();
+ var b = try glm.forward.State.init(std.testing.allocator, &d, &w, 32);
+ defer b.deinit();
+ try std.testing.expect(a.prompt_hidden != b.prompt_hidden);
+ for ([_]glm.forward.State{ a, b }) |state| {
+ try std.testing.expect(state.prompt_hidden >= state.arena.buf.ptr);
+ try std.testing.expect(state.prompt_hidden + 32 <= state.arena.buf.ptr + state.arena.buf.len);
+ }
+ const x: [*]u8 = @ptrFromInt(a.prompt_hidden);
+ const y: [*]u8 = @ptrFromInt(b.prompt_hidden);
+ @memset(x[0..32], 41);
+ @memset(y[0..32], 93);
+ for (x[0..32]) |byte| try std.testing.expectEqual(@as(u8, 41), byte);
+ try std.testing.expectEqual(before + a.arena.buf.len + b.arena.buf.len, cuda.memory.usage(false).device);
+ a.deinit();
+ a = try glm.forward.State.init(std.testing.allocator, &d, &w, 32);
+ for (y[0..32]) |byte| try std.testing.expectEqual(@as(u8, 93), byte);
+}
diff --git a/zig/glm_prompt_gate_compile.zig b/zig/glm_prompt_gate_compile.zig
new file mode 100644
index 0000000000..28e999aaa6
--- /dev/null
+++ b/zig/glm_prompt_gate_compile.zig
@@ -0,0 +1,5 @@
+//! Compile the real gate on a runtime device bundle, with no device execution.
+const gate = @import("gate");
+export fn compileGlmPromptGate(gpu: *const gate.Gpu) void {
+ gate.run(gpu.*) catch {};
+}
diff --git a/zig/glm_prompt_host.zig b/zig/glm_prompt_host.zig
new file mode 100644
index 0000000000..1c094d175f
--- /dev/null
+++ b/zig/glm_prompt_host.zig
@@ -0,0 +1,266 @@
+//! Actual GLM prompt workflow with recording device primitives and sequence storage.
+const std = @import("std");
+const glm = @import("glm");
+const t = std.testing;
+const fw = struct {
+ pub const Taps = struct { out: u64 };
+ pub const prefill_rows = 2048;
+ pub const window_rows = 8;
+ pub const State = struct {
+ pos: usize = 0,
+ mtp_len: usize = 0,
+ prompt_hidden: u64 = 0,
+ pub fn setMtpLen(s: *State, _: u8, n: usize) !void {
+ s.mtp_len = n;
+ }
+ };
+ pub const Seg = struct { st: *State, row0: usize, rows: usize };
+ pub const Buffers = struct { fnormed: u64, logits: u64 };
+ pub const Rank = struct { w: *u8, st: *State, b: *Buffers, segs: []const Seg = &.{} };
+ pub var observed_ring = false;
+ pub var observed_taps = false;
+ pub const Forward = struct {
+ d: u8,
+ m: *u8,
+ stream: u8,
+ ranks: []const Rank,
+ exchange: u8,
+ pieces: ?*u8,
+ split: bool,
+ kda_wide: bool,
+ ring: bool = false,
+ taps: ?Taps = null,
+ pub fn run(f: *Forward, ids: []const i32) !void {
+ observed_ring = f.ring;
+ observed_taps = f.taps != null;
+ if (f.taps) |taps| if (taps.out != 0) {
+ for (ids, 0..) |id, i| {
+ const ptr: *u32 = @ptrFromInt(taps.out + i * glm.dflash.TAP_ROW);
+ ptr.* = @as(u32, @intCast(id)) + @as(u32, f.d) * 1000;
+ }
+ };
+ const b = f.ranks[0].b;
+ const hidden: [*]u16 = @ptrFromInt(b.fnormed);
+ const logits: [*]u16 = @ptrFromInt(b.logits);
+ for (ids, 0..) |id, i| hidden[i] = @intCast(id);
+ for (f.ranks[0].segs, 0..) |g, i| logits[i] = @intCast(ids[g.row0 + g.rows - 1]);
+ }
+ pub fn mtp(f: *Forward, _: []const i32, _: []const u64) !void {
+ const first: *u16 = @ptrFromInt(f.ranks[0].b.fnormed);
+ first.* = 777;
+ }
+ pub fn commit(f: *Forward, n: usize) !void {
+ f.ranks[0].st.pos += n;
+ }
+ };
+};
+const engine = struct {
+ pub const max_together = 16;
+ pub const prompt_rows = 2048;
+ pub const Prompt = glm.engine.Prompt;
+ pub const Seq = struct { st: fw.State = .{}, held_at: u64 = 0 };
+ pub const Engine = struct {
+ pub const holdPrompt = glm.prompt_hold.save;
+ capacity: usize = 4096,
+ c: struct { hidden: usize = 1 } = .{},
+ hold: struct { ptr: u64 },
+ mtp: bool = true,
+ cur: *Seq = undefined,
+ seqs: [32]Seq = @splat(.{}),
+ bp: fw.Buffers,
+ share: struct { vocab: Vocab = .{} } = .{},
+ settings: struct { split: bool = true, ring: bool = true, kda_wide: bool = true } = .{},
+ d: u8 = 0,
+ m: u8 = 0,
+ stream: u8 = 0,
+ w: u8 = 0,
+ pieces: u8 = 0,
+ drafter: ?*Drafter = null,
+ const Vocab = struct {
+ pub fn len(_: Vocab) usize {
+ return 1;
+ }
+ };
+ pub fn bind(e: *Engine, slot: u32) !void {
+ if (slot >= 32) return error.BadSlot;
+ e.cur = &e.seqs[slot];
+ }
+ pub fn gate(_: *Engine, _: ?u8) !void {}
+ pub fn exchange(_: *Engine) u8 {
+ return 0;
+ }
+ pub fn draw(_: *Engine, logits: u64, _: usize, _: usize, _: u64, out: []u32) !void {
+ const p: *const u16 = @ptrFromInt(logits);
+ out[0] = p.*;
+ }
+ pub fn copy(_: *Engine, dst: u64, src: u64, n: usize) !void {
+ const d: [*]u8 = @ptrFromInt(dst);
+ const s: [*]const u8 = @ptrFromInt(src);
+ @memcpy(d[0..n], s[0..n]);
+ }
+ };
+ pub const Drafter = struct {
+ taps_prefill: u64 = 0,
+ states: [32]Slot = @splat(.{}),
+ const Slot = struct { ring: ?struct { ptr: u64 } = null };
+ pub fn taps(dr: *Drafter, _: bool) fw.Taps {
+ return .{ .out = dr.taps_prefill };
+ }
+ pub const promptSegment = glm.dflash.ingestPrompt;
+ pub fn slot(dr: *Drafter, i: u32) !*Slot {
+ return &dr.states[i];
+ }
+ pub fn ingest(_: *Drafter, _: *Engine, runs: []const glm.dflash.Run) !void {
+ for (runs) |run| for (0..run.n) |i| {
+ const src: *const u32 = @ptrFromInt(run.taps + i * glm.dflash.TAP_ROW);
+ const dst: [*]u32 = @ptrFromInt(run.ring);
+ dst[@as(usize, @intCast(run.pos0)) + i] = src.*;
+ };
+ }
+ };
+};
+const FakeEngine = engine.Engine;
+const Box = struct {
+ hidden: [2048]u16 = @splat(0),
+ logits: [16]u16 = @splat(0),
+ hold: [32]u16 = @splat(0),
+ fn make(b: *Box) FakeEngine {
+ var e: FakeEngine = .{ .hold = .{ .ptr = @intFromPtr(&b.hold) }, .bp = .{ .fnormed = @intFromPtr(&b.hidden), .logits = @intFromPtr(&b.logits) } };
+ for (&e.seqs, 0..) |*q, i| q.st.prompt_hidden = @intFromPtr(&b.hold[i]);
+ return e;
+ }
+};
+fn pass(e: *FakeEngine, parts: []const engine.Prompt) !void {
+ var first: [16]u32 = undefined;
+ try glm.prompts.pass(e, parts, &first, fw.Forward, fw.Rank);
+}
+fn held(e: *FakeEngine, slot: usize) u16 {
+ e.bind(@intCast(slot)) catch unreachable;
+ const p: *const u16 = @ptrFromInt(glm.prompt_hold.input(e));
+ return p.*;
+}
+test "GLM prompt workflow: multiple passes, nonzero and reused slots retain the first-draft rows" {
+ var b: Box = .{};
+ var e = b.make();
+ const a: [1025]u32 = @splat(41);
+ const z: [1025]u32 = @splat(93);
+ try pass(&e, &.{.{ .slot = 7, .ids = &a }});
+ try pass(&e, &.{.{ .slot = 11, .ids = &z }});
+ try t.expectEqual(@as(u16, 41), held(&e, 7));
+ try t.expectEqual(@as(u16, 93), held(&e, 11));
+ e.seqs[11].st.pos = 0;
+ e.seqs[11].st.mtp_len = 0;
+ try pass(&e, &.{.{ .slot = 11, .ids = &.{ 12, 13, 14 } }});
+ try t.expectEqual(@as(u16, 41), held(&e, 7));
+ try t.expectEqual(@as(u16, 14), held(&e, 11));
+}
+test "GLM prompt workflow: normal path and batched passes retain the incumbent" {
+ var b: Box = .{};
+ var e = b.make();
+ var prior: u16 = 71;
+ try e.bind(23);
+ try e.holdPrompt(@intFromPtr(&prior));
+ try pass(&e, &.{ .{ .slot = 4, .ids = &.{ 31, 32 } }, .{ .slot = 9, .ids = &.{ 61, 62, 63 } } });
+ try t.expectEqual(@as(u16, 71), held(&e, 23));
+ try t.expectEqual(@as(u16, 32), held(&e, 4));
+ try t.expectEqual(@as(u16, 63), held(&e, 9));
+ prior = 81;
+ try e.bind(9);
+ try e.holdPrompt(@intFromPtr(&prior));
+ try t.expectEqual(@as(u16, 71), held(&e, 23));
+ try t.expectEqual(@as(u16, 32), held(&e, 4));
+ try t.expectEqual(@as(u16, 81), held(&e, 9));
+}
+test "GLM prompt workflow: ring and enabled drafter taps reach the actual pass" {
+ var b: Box = .{};
+ var e = b.make();
+ var dr: engine.Drafter = .{};
+ e.drafter = &dr;
+ var tap_rows: [2 * glm.dflash.TAP_ROW]u8 align(8) = @splat(0);
+ var contexts: [32][16]u32 = @splat(@splat(0));
+ dr.taps_prefill = @intFromPtr(&tap_rows);
+ for (&dr.states, 0..) |*v, i| v.ring = .{ .ptr = @intFromPtr(&contexts[i]) };
+ try pass(&e, &.{ .{ .slot = 3, .ids = &.{41} }, .{ .slot = 8, .ids = &.{93} } });
+ try t.expect(fw.observed_ring);
+ try t.expect(fw.observed_taps);
+}
+
+test "GLM prompt workflow: DFlash context equals ordinary ingest at own offsets and positions on both ranks" {
+ const aa = [_]u32{ 11, 12, 13 };
+ const bb = [_]u32{ 51, 52, 53, 54, 55 };
+ for (0..2) |rank| {
+ var b: Box = .{};
+ var e = b.make();
+ e.d = @intCast(rank);
+ var tap_rows: [8 * glm.dflash.TAP_ROW]u8 align(8) = @splat(0xa5);
+ var actual: [32][16]u32 = @splat(@splat(0xcccccccc));
+ var ordinary: [32][16]u32 = actual;
+ var dr: engine.Drafter = .{ .taps_prefill = @intFromPtr(&tap_rows) };
+ for (&dr.states, 0..) |*v, i| v.ring = .{ .ptr = @intFromPtr(&actual[i]) };
+ e.drafter = &dr;
+ try pass(&e, &.{ .{ .slot = 4, .ids = &aa }, .{ .slot = 9, .ids = &bb } });
+ var single: [5 * glm.dflash.TAP_ROW]u8 align(8) = @splat(0xa5);
+ dr.taps_prefill = @intFromPtr(&single);
+ for (&dr.states, 0..) |*v, i| v.ring = .{ .ptr = @intFromPtr(&ordinary[i]) };
+ for ([_][]const u32{ &aa, &bb }, [_]u32{ 4, 9 }) |ids, slot| {
+ for (ids, 0..) |id, i| {
+ const ptr: *u32 = @ptrFromInt(dr.taps_prefill + i * glm.dflash.TAP_ROW);
+ ptr.* = id + @as(u32, @intCast(rank)) * 1000;
+ }
+ try glm.dflash.Drafter.prompt(&dr, &e, slot, 0, ids.len, ids.len);
+ for (ids, 0..) |id, i| try t.expectEqual(id + @as(u32, @intCast(rank)) * 1000, actual[slot][i]);
+ }
+ try t.expectEqualSlices(u8, std.mem.asBytes(&ordinary), std.mem.asBytes(&actual));
+ }
+}
+test "GLM prompt workflow: context window cut preserves sequence positions independently of tap offsets" {
+ const dr = glm.dflash;
+ const run = (try dr.promptRun(1000, 2000, 2048, 2048, 5000, 0)).?;
+ try t.expectEqual(@as(i64, 2952), run.pos0);
+ try t.expectEqual(@as(usize, 1144), run.n);
+ try t.expectEqual(@as(u64, 2000 + 904 * dr.TAP_ROW), run.taps);
+ const shifted = (try dr.promptRun(1000, 2000, 0, 5, 5, 11)).?;
+ try t.expectEqual(@as(i64, 0), shifted.pos0);
+ try t.expectEqual(@as(u64, 2000 + 11 * dr.TAP_ROW), shifted.taps);
+ try t.expectEqual(@as(?dr.Run, null), try dr.promptRun(1000, 2000, 0, 2048, 5000, 0));
+ try t.expectError(error.BadPromptContext, dr.promptRun(0, 0, 9, 2, 10, 0));
+ try t.expectError(error.DrafterContextTooWide, dr.promptRun(0, 0, 0, 5, 5, 2044));
+}
+
+test "GLM prompt workflow: both ranks share exact packets and reject malformed batches" {
+ const wire = glm.prompt_wire;
+ var buf: [wire.words]u32 = undefined;
+ const parts = [_]engine.Prompt{ .{ .slot = 7, .ids = &.{ 11, 12 } }, .{ .slot = 13, .ids = &.{ 21, 22, 23 } } };
+ const ids = try wire.encode(&parts, 4096, &buf);
+ var decoded: [16]engine.Prompt = undefined;
+ const back = try wire.decode(ids, 2, 4096, &decoded);
+ for (parts, back) |a, b| {
+ try t.expectEqual(a.slot, b.slot);
+ try t.expectEqualSlices(u32, a.ids, b.ids);
+ }
+ try t.expectEqual(@as(u32, 12), @backingInt(glm.follow.Kind.block));
+ try t.expectEqual(@as(u32, 13), @backingInt(glm.follow.Kind.many));
+ try t.expectError(error.BadStep, wire.decode(ids, 0, 4096, &decoded));
+ try t.expectError(error.BadStep, wire.decode(ids, 17, 4096, &decoded));
+ for (0..ids.len) |cut| try t.expectError(error.BadStep, wire.decode(ids[0..cut], 2, 4096, &decoded));
+ buf[ids.len] = 99;
+ try t.expectError(error.BadStep, wire.decode(buf[0 .. ids.len + 1], 2, 4096, &decoded));
+ buf[2] = buf[0];
+ try t.expectError(error.BadStep, wire.decode(ids, 2, 4096, &decoded));
+ buf[2] = 13;
+ buf[1] = 0;
+ try t.expectError(error.BadStep, wire.decode(ids, 2, 4096, &decoded));
+ try t.expectError(error.BadPrompts, wire.encode(&.{.{ .slot = 1, .ids = &.{} }}, 4096, &buf));
+}
+
+test "GLM structured: prompt rows survive without an MTP head" {
+ var b: Box = .{};
+ var e = b.make();
+ e.mtp = false;
+ try pass(&e, &.{ .{ .slot = 4, .ids = &.{ 31, 32 } }, .{ .slot = 9, .ids = &.{ 61, 62, 63 } } });
+ try t.expectEqual(@as(u16, 32), held(&e, 4));
+ try t.expectEqual(@as(u16, 63), held(&e, 9));
+ try pass(&e, &.{.{ .slot = 13, .ids = &.{ 81, 82 } }});
+ try t.expectEqual(@as(u16, 32), held(&e, 4));
+ try t.expectEqual(@as(u16, 63), held(&e, 9));
+}
diff --git a/zig/glm_prompt_logprobs_host.zig b/zig/glm_prompt_logprobs_host.zig
new file mode 100644
index 0000000000..361a01e28d
--- /dev/null
+++ b/zig/glm_prompt_logprobs_host.zig
@@ -0,0 +1,194 @@
+//! Controls at the real prompt-logprob owner, head policy and batching boundaries.
+const std = @import("std");
+const glm = @import("glm");
+const lanes = @import("lanes");
+test "GLM prompt rows: reader cannot allocate after ranks enter the pass" {
+ var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{});
+ var s: lanes.Stream = undefined;
+ s.prompt_rows = .empty;
+ s.prompt_len = 3000;
+ s.prompt_logprobs = 0;
+ defer s.prompt_rows.deinit(std.testing.allocator);
+ var sink = glm.prompt_sink.Sink{ .gpa = failing.allocator(), .s = &s };
+ try sink.prepare();
+ failing.fail_index = failing.alloc_index;
+ var words: [glm.prompt_logprobs.row_words * 128]u32 = @splat(0);
+ var picks: [128]u32 = @splat(7);
+ var left: usize = 2999;
+ while (left > 0) {
+ const n: usize = @min(left, 128);
+ try glm.prompt_sink.Sink.read(&sink, words[0 .. n * glm.prompt_logprobs.row_words], picks[0..n]);
+ left -= n;
+ }
+ try std.testing.expectEqual(@as(usize, 2999), s.prompt_rows.items.len);
+}
+test "GLM prompt rows: tiled packed heads use the forward policy" {
+ var e: glm.engine.Engine = undefined;
+ e.m.tiled = true;
+ var packs: glm.mm.Packed = .{};
+ e.m.packs = &packs;
+ const set = glm.prompt_logprobs.projectionSet(&e);
+ try std.testing.expect(set.mm == &e.m.mm);
+ try std.testing.expect(set.packs == &packs);
+ try std.testing.expect(set.prompt);
+}
+test "GLM prompt rows: requests for rows cannot enter the unscored batch path" {
+ const S = struct { images: []const u8 = &.{}, reuse: struct { saved: ?*u8 = null } = .{}, prompt_len: usize = 19, prompt_logprobs: ?u8 = null };
+ var s: S = .{};
+ try std.testing.expect(glm.lanes.canBatch(&s));
+ s.prompt_logprobs = 2;
+ try std.testing.expect(!glm.lanes.canBatch(&s));
+}
+
+test "GLM prompt rows: reservation failure happens before a prompt step" {
+ var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 });
+ var s: lanes.Stream = undefined;
+ s.prompt_rows = .empty;
+ s.prompt_len = 3000;
+ s.prompt_logprobs = 0;
+ var sink = glm.prompt_sink.Sink{ .gpa = failing.allocator(), .s = &s };
+ try std.testing.expectError(error.OutOfMemory, sink.prepare());
+ try std.testing.expectEqual(@as(usize, 0), s.prompt_rows.items.len);
+}
+
+const cuda = @import("cuda");
+const Output = glm.prompt_logprobs.Output;
+const Mapping = struct {
+ var data: [2][glm.prompt_logprobs.out_bytes]u8 align(16) = undefined;
+ var allocating: usize = 0;
+ var refuse: [2]bool = .{ true, false };
+ var maps: [2]usize = .{ 0, 0 };
+ var frees: [2]usize = .{ 0, 0 };
+ fn rank(p: ?*anyopaque) usize {
+ return if (p == @as(?*anyopaque, @ptrCast(&data[0]))) 0 else 1;
+ }
+ fn alloc(p: *?*anyopaque, n: usize, _: c_uint) callconv(.c) cuda.abi.Result {
+ if (n != data[0].len) return 2;
+ p.* = &data[allocating];
+ return 0;
+ }
+ fn map(p: *u64, ptr: ?*anyopaque, _: c_uint) callconv(.c) cuda.abi.Result {
+ const r = rank(ptr);
+ maps[r] += 1;
+ if (refuse[r]) return cuda.abi.error_not_ready;
+ p.* = 0x10000 + r * 0x10000;
+ return 0;
+ }
+ fn free(p: ?*anyopaque) callconv(.c) cuda.abi.Result {
+ frees[rank(p)] += 1;
+ return 0;
+ }
+};
+fn driver() cuda.Driver {
+ var d: cuda.Driver = undefined;
+ d.api.cuMemHostAlloc = Mapping.alloc;
+ d.api.cuMemHostGetDevicePointer_v2 = Mapping.map;
+ d.api.cuMemFreeHost = Mapping.free;
+ return d;
+}
+
+const Heads = struct {
+ var broadcasts: [2]usize = .{ 0, 0 };
+ fn launch(_: cuda.abi.Function, _: c_uint, _: c_uint, _: c_uint, _: c_uint, _: c_uint, _: c_uint, _: c_uint, _: cuda.abi.Stream, _: ?[*]?*anyopaque, _: ?[*]?*anyopaque) callconv(.c) cuda.abi.Result {
+ return 0;
+ }
+ fn copy(_: u64, _: u64, _: usize, _: cuda.abi.Stream) callconv(.c) cuda.abi.Result {
+ return 0;
+ }
+ fn group() callconv(.c) cuda.nccl.Result {
+ return 0;
+ }
+ fn broadcast(_: u64, _: u64, _: usize, _: cuda.nccl.DataType, _: c_int, comm: cuda.nccl.Comm, _: cuda.abi.Stream) callconv(.c) cuda.nccl.Result {
+ broadcasts[@intFromPtr(comm) - 1] += 1;
+ return 0;
+ }
+ fn score(d: *cuda.Driver, output: Output, rank_: usize) !void {
+ d.api.cuLaunchKernel = launch;
+ d.api.cuMemcpyDtoDAsync_v2 = copy;
+ var lib: cuda.nccl.Library = undefined;
+ lib.api.ncclGroupStart = group;
+ lib.api.ncclGroupEnd = group;
+ lib.api.ncclBroadcast = broadcast;
+ var e: glm.engine.Engine = undefined;
+ e.prompt_tried = true;
+ e.prompt_bufs = null;
+ e.prompt_out = output;
+ e.d = d;
+ e.c.hidden = 64;
+ e.c.vocab = 16;
+ e.share.rank = rank_;
+ e.share.vocab = .{ .lo = rank_ * 8, .hi = (rank_ + 1) * 8 };
+ var spans = [_]glm.shard.Span{ .{ .lo = 0, .hi = 8 }, .{ .lo = 8, .hi = 16 } };
+ e.spans = &spans;
+ e.lib = &lib;
+ e.comm = @ptrFromInt(rank_ + 1);
+ e.group = null;
+ e.stream = .{ .d = d, .handle = null };
+ e.m.tiled = false;
+ e.m.packs = null;
+ e.m.glue.bmm_decode = .{ .d = d, .handle = @ptrFromInt(1) };
+ e.bp.fnormed = 0x10000;
+ e.bp.kda.mm_part = 0x20000;
+ e.bd.logits = 0x30000;
+ e.w.head = 0x40000;
+ e.smp.d = d;
+ e.smp.vocab = 16;
+ e.smp.full = .{ .d = d, .ptr = 0x50000, .len = 1024 };
+ try glm.prompt_logprobs.run(&e, .{}, &.{7});
+ }
+};
+
+test "GLM prompt rows: rank zero mapping failure is a startup refusal while follower mapping succeeds" {
+ var d = [_]cuda.Driver{ driver(), driver() };
+ var outputs: [2]?Output = .{ null, null };
+ defer for (&outputs) |*o| if (o.*) |*p| p.free();
+ var errors: [2]?anyerror = .{ null, null };
+ Heads.broadcasts = .{ 0, 0 };
+ Mapping.refuse = .{ true, false };
+ Mapping.maps = .{ 0, 0 };
+ Mapping.frees = .{ 0, 0 };
+ const before = cuda.memory.usage(false).host;
+ for (0..2) |r| {
+ Mapping.allocating = r;
+ outputs[r] = Output.init(&d[r]) catch |err| blk: {
+ errors[r] = err;
+ break :blk null;
+ };
+ }
+ if (errors[0] == null and errors[1] == null) {
+ for (0..2) |r| Heads.score(&d[r], outputs[r].?, r) catch |err| {
+ errors[r] = err;
+ };
+ }
+ try std.testing.expectEqualSlices(usize, &.{ 0, 0 }, &Heads.broadcasts);
+ try std.testing.expectEqual(@as(?anyerror, error.NotReady), errors[0]);
+ try std.testing.expect(outputs[0] == null);
+ try std.testing.expect(errors[1] == null and outputs[1] != null);
+ try std.testing.expectEqualSlices(usize, &.{ 1, 1 }, &Mapping.maps);
+ try std.testing.expectEqualSlices(usize, &.{ 1, 0 }, &Mapping.frees);
+ try std.testing.expectEqual(before + glm.prompt_logprobs.out_bytes, cuda.memory.usage(false).host);
+ outputs[1].?.free();
+ outputs[1] = null;
+ try std.testing.expectEqual(before, cuda.memory.usage(false).host);
+}
+
+test "GLM prompt rows: both cached addresses survive later driver mapping refusal" {
+ var d = [_]cuda.Driver{ driver(), driver() };
+ var outputs: [2]?Output = .{ null, null };
+ defer for (&outputs) |*o| if (o.*) |*p| p.free();
+ Mapping.refuse = .{ false, false };
+ Mapping.maps = .{ 0, 0 };
+ for (0..2) |r| {
+ Mapping.allocating = r;
+ outputs[r] = try Output.init(&d[r]);
+ }
+ Mapping.refuse = .{ true, true };
+ Heads.broadcasts = .{ 0, 0 };
+ for (0..2) |r| {
+ try Heads.score(&d[r], outputs[r].?, r);
+ try std.testing.expectEqual(@as(u64, 0x10000) + r * 0x10000, outputs[r].?.address);
+ outputs[r].?.slice(u32)[0] = @intCast(7 + r);
+ }
+ try std.testing.expectEqualSlices(usize, &.{ 1, 1 }, &Mapping.maps);
+ try std.testing.expectEqualSlices(usize, &.{ 2, 2 }, &Heads.broadcasts);
+}
diff --git a/zig/glm_structured_host.zig b/zig/glm_structured_host.zig
new file mode 100644
index 0000000000..eb2016315c
--- /dev/null
+++ b/zig/glm_structured_host.zig
@@ -0,0 +1,97 @@
+//! Prompt redraw workflow with distinct owned rows and overwritten admission scratch.
+const std = @import("std");
+const glm = @import("glm");
+const E = struct {
+ bp: struct { logits: u64 },
+ bd: struct { logits: u64 },
+ share: struct { rank: usize = 0 } = .{},
+ row: u16,
+ projected: u16 = 0,
+ projections: usize = 0,
+ position: usize = 0,
+ pub fn promptHead(e: *E) !void {
+ e.projected = e.row;
+ e.projections += 1;
+ e.bd.logits = @intFromPtr(&e.projected);
+ }
+ pub fn drawWhole(e: *E, logits: u64, rows: usize, first: usize, bits: u64, out: []u32) !void {
+ try std.testing.expectEqual(@as(usize, 1), rows);
+ try std.testing.expectEqual(@as(u64, 1234), bits);
+ e.position = first;
+ if (e.share.rank != 0) return;
+ const p: *const u16 = @ptrFromInt(logits);
+ out[0] = p.*;
+ }
+};
+test "GLM structured: redraw uses the bound prompt after later admissions overwrite scratch" {
+ var scratch: u16 = 999;
+ var e: E = .{ .bp = .{ .logits = @intFromPtr(&scratch) }, .bd = .{ .logits = 0 }, .row = 41 };
+ try std.testing.expectEqual(@as(u32, 41), try glm.structured.redrawFirst(&e, 1025, 1234));
+ scratch = 777;
+ e.row = 93;
+ try std.testing.expectEqual(@as(u32, 93), try glm.structured.redrawFirst(&e, 2051, 1234));
+ try std.testing.expectEqual(@as(usize, 2051), e.position);
+ try std.testing.expectEqual(@as(usize, 2), e.projections);
+}
+
+test "GLM structured: a follower redraw returns a defined unused token" {
+ var scratch: u16 = 999;
+ var e: E = .{ .bp = .{ .logits = @intFromPtr(&scratch) }, .bd = .{ .logits = 0 }, .row = 41, .share = .{ .rank = 1 } };
+ try std.testing.expectEqual(@as(u32, 0), try glm.structured.redrawFirst(&e, 19, 1234));
+ try std.testing.expectEqual(@as(usize, 1), e.projections);
+}
+
+test "GLM structured: sampling launches have pending resource records" {
+ const registry = @import("core").contract.registry;
+ const cases = .{ .{ "tf_draw", 1024 }, .{ "tf_logprob_rows", 1024 }, .{ "tf_grammar_mask", 256 }, .{ "tf_glm_top1", 1024 } };
+ inline for (cases) |c| {
+ var found = false;
+ for (registry.all_cuda) |r| if (std.mem.eql(u8, r.launch.symbol, c[0])) {
+ found = true;
+ try std.testing.expectEqual(@as(u32, c[1]), r.launch.threads);
+ for (r.usage) |u| try std.testing.expectEqual(@as(u32, 0), u.regs);
+ };
+ try std.testing.expect(found);
+ }
+}
+
+const cuda = @import("cuda");
+var mapped: [1024]u8 align(16) = undefined;
+var refuse_mapping = true;
+var freed: usize = 0;
+fn hostAlloc(p: *?*anyopaque, n: usize, _: c_uint) callconv(.c) cuda.abi.Result {
+ if (n > mapped.len) return 2;
+ p.* = &mapped;
+ return 0;
+}
+fn hostFree(_: ?*anyopaque) callconv(.c) cuda.abi.Result {
+ freed += 1;
+ return 0;
+}
+fn hostMap(p: *u64, _: ?*anyopaque, _: c_uint) callconv(.c) cuda.abi.Result {
+ if (refuse_mapping) return cuda.abi.error_not_ready;
+ p.* = @intFromPtr(&mapped);
+ return 0;
+}
+test "GLM structured: failed mask mapping publishes no owner and can retry" {
+ var d: cuda.Driver = undefined;
+ d.api.cuMemHostAlloc = hostAlloc;
+ d.api.cuMemFreeHost = hostFree;
+ d.api.cuMemHostGetDevicePointer_v2 = hostMap;
+ var e: glm.engine.Engine = undefined;
+ e.gpa = std.testing.allocator;
+ e.d = &d;
+ e.c.vocab = 64;
+ var l: glm.lanes.Lanes = .{ .x = .{ .e = &e, .link = undefined } };
+ defer l.deinit();
+ refuse_mapping = true;
+ freed = 0;
+ const before = cuda.memory.usage(false).host;
+ try std.testing.expectError(error.NotReady, l.putBits(0, 1, &.{ 7, 9 }));
+ try std.testing.expect(l.bits == null);
+ try std.testing.expectEqual(@as(usize, 1), freed);
+ try std.testing.expectEqual(before, cuda.memory.usage(false).host);
+ refuse_mapping = false;
+ try std.testing.expect(try l.putBits(0, 1, &.{ 7, 9 }) != 0);
+ try std.testing.expectEqualSlices(u32, &.{ 7, 9 }, l.bits.?.slice(u32)[0..2]);
+}
diff --git a/zig/kernels/cuda/attn_train.cu b/zig/kernels/cuda/attn_train.cu
new file mode 100644
index 0000000000..44a9a62493
--- /dev/null
+++ b/zig/kernels/cuda/attn_train.cu
@@ -0,0 +1,210 @@
+// Living Weights' gated attention backward.
+
+#include
+#include
+
+typedef __nv_bfloat16 bf16;
+
+__device__ __forceinline__ float f32(bf16 v) { return __bfloat162float(v); }
+__device__ __forceinline__ bf16 b16(float v) { return __float2bfloat16(v); }
+
+__device__ __forceinline__ float warp_sum(float v) {
+#pragma unroll
+ for (int o = 16; o > 0; o >>= 1) v += __shfl_xor_sync(0xffffffffu, v, o);
+ return v;
+}
+
+__device__ __forceinline__ float warp_max(float v) {
+#pragma unroll
+ for (int o = 16; o > 0; o >>= 1) v = fmaxf(v, __shfl_xor_sync(0xffffffffu, v, o));
+ return v;
+}
+
+// The sum over a block of `groups` warps, the same in every thread.
+__device__ __forceinline__ float block_sum(float v, float* scratch, int groups) {
+ const int lane = threadIdx.x & 31, w = threadIdx.x >> 5;
+ v = warp_sum(v);
+ if (lane == 0) scratch[w] = v;
+ __syncthreads();
+ v = warp_sum(lane < groups ? scratch[lane] : 0.0f);
+ __syncthreads();
+ return v;
+}
+
+__device__ __forceinline__ float block_max(float v, float* scratch, int groups) {
+ const int lane = threadIdx.x & 31, w = threadIdx.x >> 5;
+ v = warp_max(v);
+ if (lane == 0) scratch[w] = v;
+ __syncthreads();
+ v = warp_max(lane < groups ? scratch[lane] : -INFINITY);
+ __syncthreads();
+ return v;
+}
+
+__device__ __forceinline__ float sigm(float v) { return 1.0f / (1.0f + expf(-v)); }
+
+// ---- the attention layer --------------------------------------------------------------------------------------------
+
+// Attention's shape (train.cu's Heads): rows, heads, kv heads, head dim, q|k|v row width, scale, prefix keys.
+struct Heads {
+ int rows, heads, kv_heads, dim, qkv;
+ float scale;
+ int prefix;
+};
+
+// The rotate-half RoPE angle of pair i at position pos.
+__device__ __forceinline__ void rope_cs(int pos, const float* inv, int i, float& c, float& s) {
+ const float ang = (float)pos * inv[i];
+ c = cosf(ang), s = sinf(ang);
+}
+
+// The attention's prepared heads as glue.attn_prep writes them, into qkv [rows, (heads + 2 kv) dim].
+extern "C" __global__ void __launch_bounds__(256) tf_attn_prep(const bf16* pq, const bf16* pk, const bf16* pv,
+ const float* qscale, const float* kscale, const float* inv,
+ int pos0, bf16* qkv, int heads, int kv_heads, int dim, int half,
+ float eps) {
+ __shared__ float xn[256], scratch[32];
+ const int hd = blockIdx.x, r = blockIdx.y, d = threadIdx.x;
+ const int qw = heads * dim, kw = kv_heads * dim, width = qw + 2 * kw;
+ if (hd >= heads + kv_heads) {
+ const int k = hd - heads - kv_heads;
+ qkv[(size_t)r * width + qw + kw + k * dim + d] = pv[(size_t)r * kw + k * dim + d];
+ return;
+ }
+ const bool q = hd < heads;
+ const float x = q ? f32(pq[(size_t)r * 2 * qw + hd * 2 * dim + d]) : f32(pk[(size_t)r * kw + (hd - heads) * dim + d]);
+ const float* w = q ? qscale : kscale;
+ const float rinv = 1.0f / sqrtf(block_sum(x * x, scratch, dim / 32) / (float)dim + eps);
+ xn[d] = f32(b16(x * rinv * w[d]));
+ __syncthreads();
+ float out = xn[d];
+ if (d < 2 * half) {
+ float c, s;
+ rope_cs(pos0 + r, inv, d % half, c, s);
+ out = d < half ? xn[d] * c - xn[d + half] * s : xn[d - half] * s + xn[d] * c;
+ }
+ qkv[(size_t)r * width + (q ? hd * dim : qw + (hd - heads) * dim) + d] = b16(out);
+}
+
+#define ATTN_KEYS 2048
+
+// tf_train_attn_q for up to ATTN_KEYS keys: one query row i (position prefix + i) of head h.
+extern "C" __global__ void __launch_bounds__(256) tf_attn_q(const bf16* qkv, const float* dout, float* probs, float* dscores,
+ float* dq, Heads a, const bf16* pk, const bf16* pv) {
+ __shared__ float qi[256], gi[256], sc[ATTN_KEYS], dp[ATTN_KEYS], scratch[32];
+ const int t = threadIdx.x, lane = t & 31, w = t >> 5, h = blockIdx.x, i = blockIdx.y;
+ const int kv = h / (a.heads / a.kv_heads), qw = a.heads * a.dim, kw = a.kv_heads * a.dim;
+ const int keys = a.prefix + a.rows, at = a.prefix + i;
+ const bf16* k = qkv + qw;
+ const bf16* v = qkv + qw + kw;
+ for (int d = t; d < a.dim; d += 256) {
+ qi[d] = f32(qkv[(size_t)i * a.qkv + h * a.dim + d]);
+ gi[d] = dout[(size_t)i * qw + h * a.dim + d];
+ }
+ __syncthreads();
+ for (int j = w; j <= at; j += 8) {
+ const bf16* kr = (j < a.prefix ? pk + (size_t)j * kw : k + (size_t)(j - a.prefix) * a.qkv) + kv * a.dim;
+ const bf16* vr = (j < a.prefix ? pv + (size_t)j * kw : v + (size_t)(j - a.prefix) * a.qkv) + kv * a.dim;
+ float s = 0, g = 0;
+ for (int d = lane; d < a.dim; d += 32) {
+ s += qi[d] * f32(kr[d]);
+ g += gi[d] * f32(vr[d]);
+ }
+ s = warp_sum(s);
+ g = warp_sum(g);
+ if (lane == 0) sc[j] = s * a.scale, dp[j] = g;
+ }
+ __syncthreads();
+ float top = -INFINITY;
+ for (int j = t; j <= at; j += 256) top = fmaxf(top, sc[j]);
+ top = block_max(top, scratch, 8);
+ float total = 0;
+ for (int j = t; j <= at; j += 256) {
+ const float e = expf(sc[j] - top);
+ sc[j] = e;
+ total += e;
+ }
+ total = block_sum(total, scratch, 8);
+ float mix = 0;
+ for (int j = t; j <= at; j += 256) {
+ sc[j] /= total;
+ mix += sc[j] * dp[j];
+ }
+ mix = block_sum(mix, scratch, 8);
+ const size_t row = ((size_t)h * a.rows + i) * keys;
+ for (int j = t; j < keys; j += 256) {
+ const bool live = j <= at;
+ const float p = live ? sc[j] : 0.0f, g = live ? p * (dp[j] - mix) : 0.0f;
+ if (live) dp[j] = g;
+ probs[row + j] = p;
+ dscores[row + j] = g;
+ }
+ __syncthreads();
+ for (int d = t; d < a.dim; d += 256) {
+ float acc = 0;
+ for (int j = 0; j <= at; j++) {
+ const bf16* kr = j < a.prefix ? pk + (size_t)j * kw : k + (size_t)(j - a.prefix) * a.qkv;
+ acc += dp[j] * f32(kr[kv * a.dim + d]);
+ }
+ dq[(size_t)i * qw + h * a.dim + d] = acc * a.scale;
+ }
+}
+
+// The output gate gated = o sigmoid(gate) undone from dgated [rows, heads dim].
+extern "C" __global__ void tf_attn_gate_back(const float* dgated, const bf16* gated, const bf16* pq, float* dout, float* dpq,
+ int heads, int dim) {
+ const int c = blockIdx.x * 256 + threadIdx.x, r = blockIdx.y, qw = heads * dim;
+ if (c >= qw) return;
+ const int h = c / dim, d = c % dim;
+ const size_t at = (size_t)r * 2 * qw + h * 2 * dim + dim + d;
+ const float sg = sigm(f32(pq[at])), g = dgated[(size_t)r * qw + c];
+ dout[(size_t)r * qw + c] = g * sg;
+ dpq[at] = g * f32(gated[(size_t)r * qw + c]) * (1.0f - sg);
+}
+
+// tf_attn_prep undone for the q and k heads: dq [rows, heads dim] and dk [rows.
+extern "C" __global__ void __launch_bounds__(256) tf_attn_prep_back(const bf16* pq, const bf16* pk, const float* qscale,
+ const float* kscale, const float* inv, int pos0,
+ const float* dq, const float* dk, float* dpq, float* dpk,
+ int heads, int kv_heads, int dim, int half, float eps) {
+ __shared__ float go[256], scratch[32];
+ const int hd = blockIdx.x, r = blockIdx.y, d = threadIdx.x;
+ const int qw = heads * dim, kw = kv_heads * dim;
+ const bool q = hd < heads;
+ const float x = q ? f32(pq[(size_t)r * 2 * qw + hd * 2 * dim + d]) : f32(pk[(size_t)r * kw + (hd - heads) * dim + d]);
+ go[d] = q ? dq[(size_t)r * qw + hd * dim + d] : dk[(size_t)r * kw + (hd - heads) * dim + d];
+ __syncthreads();
+ float gn = go[d];
+ if (d < 2 * half) {
+ float c, s;
+ rope_cs(pos0 + r, inv, d % half, c, s);
+ gn = d < half ? go[d] * c + go[d + half] * s : go[d] * c - go[d - half] * s;
+ }
+ const float w = (q ? qscale : kscale)[d];
+ const float sq = block_sum(x * x, scratch, dim / 32), dot = block_sum(w * gn * x, scratch, dim / 32);
+ const float rinv = 1.0f / sqrtf(sq / (float)dim + eps);
+ const float g = rinv * w * gn - rinv * rinv * rinv * x * dot / (float)dim;
+ if (q)
+ dpq[(size_t)r * 2 * qw + hd * 2 * dim + d] = g;
+ else
+ dpk[(size_t)r * kw + (hd - heads) * dim + d] = g;
+}
+
+
+// ---- shared by the learners' products and nudges ------------------------------------------------------------------
+
+// C [M, N] (rows ldc apart) (+)= the split products' partials [splits, M, N], summed in split order.
+extern "C" __global__ void tf_sum_parts(const float* parts, int splits, int M, int N, float* C, int ldc, int accumulate) {
+ const long long i = (long long)blockIdx.x * blockDim.x + threadIdx.x;
+ if (i >= (long long)M * N) return;
+ float s = 0;
+ for (int z = 0; z < splits; z++) s += parts[(size_t)z * M * N + i];
+ float* out = C + (size_t)(i / N) * ldc + i % N;
+ *out = accumulate ? *out + s : s;
+}
+
+// y[i] += x[i] for n floats: a nudge onto its row of a layer's output.
+extern "C" __global__ void tf_axpy(float* y, const float* x, int n) {
+ const int i = blockIdx.x * 256 + threadIdx.x;
+ if (i < n) y[i] += x[i];
+}
diff --git a/zig/kernels/cuda/deepseek_v41/attn_cuda.cu b/zig/kernels/cuda/deepseek_v41/attn_cuda.cu
new file mode 100644
index 0000000000..ec43c81ecb
--- /dev/null
+++ b/zig/kernels/cuda/deepseek_v41/attn_cuda.cu
@@ -0,0 +1,541 @@
+// Copy of src/tensorfold/families/deepseek_v41/cuda/csa2/attn_cuda.cu (blob f4be064c7368): sass_check.sh
+#define DSV41_ATTN_NO_TORCH
+
+
+
+// CSA2 decode attention (chunks + merge) in ONE launch, TF_DSV41_ATTN_CUDA=1: the Triton pair's ops, same bits
+#include
+#include
+#include
+#include
+
+namespace dsv41_attn {
+
+constexpr int D = 512, CH = 128, NCOMP = 4, NCH = 5, BMQ = 16, WINDOW = 128; // tiles of 32 keys
+constexpr int NOPE = 448, VB = 576, SB = 8, NT = 256;
+constexpr int SM_Q = 0; // q A fragments [32 steps][32 lanes] uint4 16384
+constexpr int SM_KV = 16384; // 128 rows x 576 B, 8-byte pieces swizzled by key & 7 73728
+constexpr int SM_SC = SM_KV + CH * VB; // 128 x 8 scale bytes 1024
+constexpr int SM_P = SM_SC + CH * SB; // P A fragments [4 tiles][2 steps][32 lanes] uint4 4096
+constexpr int SM_TMAX = SM_P + 4096; // [4 tiles][4 n8][16 rows] fp32 1024
+constexpr int SM_WSUM = SM_TMAX + 1024; // same shape: the warps' row sums 1024
+constexpr int SM_ROW = SM_WSUM + 1024; // [4 tiles][16 rows] nm, alpha, active 768
+constexpr int SM_PHYS = SM_ROW + 768; // 128 physical rows (int32, -1: masked) 512
+constexpr int SM_MISC = SM_PHYS + 512; // final m, l [16] each + the ticket flag 256
+constexpr int SMEM = SM_MISC + 256; // 98,816 B (sm_121: <= 101,376 a block)
+static_assert(SMEM <= 101376, "shared memory");
+constexpr unsigned FULL = 0xffffffffu;
+
+struct Args {
+ const uint16_t* q; // [R, H, 512] bf16 (RoPE'd)
+ const uint8_t* cv; const uint8_t* csc; long long cvs, css; // compressed rows (values, scales), row strides
+ const int* tok; long long ts; const int* cnt; // selection [R, ts] int32 (-1 pad), counts [R]
+ const uint8_t* sv; const uint8_t* ssc; long long svs, sss; // SWA rings
+ const int* lo; const int* hi; // [R] window starts; window ends (HAS_HI) or null
+ const void* pos; // int32 [1] (POS + r) or int64 [R] (ROWS)
+ const long long* sl; // ROWS: the rows' slots [R]
+ const int* pt; long long pts; int psh; // page table of the compressed rows (psh 0: contiguous)
+ const float* sink; const float* cs; long long csst; // [H] fp32, RoPE table fp32 [max_pos, 64]
+ float* po; float* pm; float* pl; // partials (attn.Scratch layout)
+ uint16_t* out; // [R, H, 512] bf16
+ int* ticket; // [R, H / 16] (self-cleaning)
+ int R, H, ring, has_comp;
+ int qrope; // split_kernel: q arrives un-rotated, RoPE'd as it loads
+};
+
+__device__ __forceinline__ float ex2(float x) {
+ float y;
+ asm("ex2.approx.f32 %0, %1;" : "=f"(y) : "f"(x));
+ return y;
+}
+__device__ __forceinline__ float div_full(float a, float b) {
+ float y;
+ asm("div.full.f32 %0, %1, %2;" : "=f"(y) : "f"(a), "f"(b));
+ return y;
+}
+__device__ __forceinline__ float texp(float x) { return ex2(__fmul_rn(x, __int_as_float(0x3FB8AA3B))); }
+__device__ __forceinline__ float pow2b(uint32_t s) { return __int_as_float(static_cast(s) << 23); }
+
+__device__ __forceinline__ void mma(float (&d)[4], const uint4& a, uint32_t b0, uint32_t b1) {
+ asm volatile("mma.sync.aligned.m16n8k16.row.col.f32.bf16.bf16.f32 {%0,%1,%2,%3}, {%4,%5,%6,%7}, {%8,%9}, "
+ "{%0,%1,%2,%3};"
+ : "+f"(d[0]), "+f"(d[1]), "+f"(d[2]), "+f"(d[3])
+ : "r"(a.x), "r"(a.y), "r"(a.z), "r"(a.w), "r"(b0), "r"(b1));
+}
+
+// two e4m3 bytes (low byte = the lower k) with their own scales -> bf16x2 (exact: e4m3 x 2^e is a bf16)
+__device__ __forceinline__ uint32_t deq2(uint32_t two, float s0, float s1) {
+ uint32_t h2;
+ asm("cvt.rn.f16x2.e4m3x2 %0, %1;" : "=r"(h2) : "h"(static_cast(two)));
+ const __half2 h = *reinterpret_cast(&h2);
+ const __nv_bfloat162 b = __floats2bfloat162_rn(__fmul_rn(__low2float(h), s0), __fmul_rn(__high2float(h), s1));
+ return *reinterpret_cast(&b);
+}
+
+// a byte offset x of row `key` in the staged rows: 8-byte pieces swizzled within groups of 8 (bank-conflict free)
+__device__ __forceinline__ int swz(int key, int x) {
+ const int ch = x >> 3;
+ return key * VB + ((((ch & ~7) | ((ch & 7) ^ (key & 7)))) << 3) + (x & 7);
+}
+
+// Triton's dot-operand k order: step s (16 k) of a 32-k block takes k = klo(s), klo + 1 and khi(s), khi + 1
+template __device__ __forceinline__ int klo(int s, int t) {
+ return KW == 4 ? 32 * (s >> 1) + 4 * t + 2 * (s & 1) : 16 * s + 2 * t;
+}
+template __device__ __forceinline__ int khi(int s, int t) { return klo(s, t) + (KW == 4 ? 16 : 8); }
+// where element k (< 32) of a row sits in a 2-step A fragment: step, quad lane, high half (+16 / +8)
+template __device__ __forceinline__ void frag_of(int k, int& step, int& tq, int& hi) {
+ if (KW == 4) { step = (k >> 1) & 1; hi = (k >> 4) & 1; tq = (k & 15) >> 2; }
+ else { step = k >> 4; hi = (k >> 3) & 1; tq = (k & 7) >> 1; }
+}
+
+// the bf16 pair (k, k + 1) of staged row `key` (S's B operand: k along the row)
+__device__ __forceinline__ uint32_t pair_k(const uint8_t* sm, int key, int k) {
+ if (k < NOPE) {
+ const uint32_t two = *reinterpret_cast(sm + SM_KV + swz(key, k));
+ const float s = pow2b(sm[SM_SC + key * SB + (k >> 6)]);
+ return deq2(two, s, s);
+ }
+ return *reinterpret_cast(sm + SM_KV + swz(key, NOPE + 2 * (k - NOPE)));
+}
+
+// value (key, n) of the staged rows as a bf16 bit pattern / its e4m3 byte (PV's B operand: k along the keys)
+__device__ __forceinline__ uint32_t pair_n(const uint8_t* sm, int k0, int k1, int n) {
+ if (n < NOPE) {
+ const uint32_t two = sm[SM_KV + swz(k0, n)] | (static_cast(sm[SM_KV + swz(k1, n)]) << 8);
+ return deq2(two, pow2b(sm[SM_SC + k0 * SB + (n >> 6)]), pow2b(sm[SM_SC + k1 * SB + (n >> 6)]));
+ }
+ const int x = NOPE + 2 * (n - NOPE);
+ return *reinterpret_cast(sm + SM_KV + swz(k0, x)) |
+ (static_cast(*reinterpret_cast(sm + SM_KV + swz(k1, x))) << 16);
+}
+
+__device__ __forceinline__ void cp8(uint32_t dst, const void* src, bool ok) {
+ asm volatile("cp.async.ca.shared.global [%0], [%1], 8, %2;" :: "r"(dst), "l"(src), "r"(ok ? 8 : 0));
+}
+
+template __device__ __forceinline__ int row_pos(const Args& a, int r) {
+ return ROWS ? static_cast(static_cast(a.pos)[r]) : static_cast(a.pos)[0] + r;
+}
+
+// how many chunks of row r have keys (the merge's ticket target; the others are Triton's empty partial)
+__device__ __forceinline__ bool chunk_works(const Args& a, int r, int c) {
+ return c == NCOMP || (a.has_comp && c * CH < a.cnt[r]);
+}
+
+template
+__device__ void merge(const Args& a, uint8_t* sm, int r, int hb) {
+ const int tid = threadIdx.x, H = a.H, R = a.R;
+ float* ca = reinterpret_cast(sm + SM_TMAX); // [5][16] a, [5][16] b, [16] af, [16] den
+ float* cb = ca + NCH * BMQ;
+ float* fa = cb + NCH * BMQ;
+ float* fd = fa + BMQ;
+ if (tid < BMQ) { // a head's scalar chain (Triton's _merge, unrolled)
+ const int h = hb * BMQ + tid;
+ float m = -INFINITY, l = 0.f;
+ for (int c = 0; c < NCH; ++c) {
+ const bool w = chunk_works(a, r, c);
+ const long long base = (static_cast(c) * R + r) * H + h;
+ const float cm = w ? __ldcg(a.pm + base) : -INFINITY, cl = w ? __ldcg(a.pl + base) : 0.f;
+ const bool act = cl > 0.f;
+ float aa, bb;
+ if (c == 0) {
+ const float nm = act ? fmaxf(cm, -INFINITY) : -INFINITY;
+ bb = act ? texp(__fsub_rn(cm, nm)) : 0.f;
+ aa = 0.f;
+ l = __fmaf_rn(bb, cl, 0.f);
+ m = nm;
+ } else {
+ const float nm = act ? fmaxf(m, cm) : m;
+ aa = act ? (m == -INFINITY ? 0.f : texp(__fsub_rn(m, nm))) : 1.f;
+ bb = act ? texp(__fsub_rn(cm, nm)) : 0.f;
+ l = __fmaf_rn(l, aa, __fmul_rn(bb, cl));
+ m = nm;
+ }
+ ca[c * BMQ + tid] = aa;
+ cb[c * BMQ + tid] = bb;
+ }
+ const float sink = a.sink[h];
+ const float top = fmaxf(m, sink);
+ const float af = m == -INFINITY ? 0.f : texp(__fsub_rn(m, top));
+ const float e = sink == -INFINITY ? 0.f : texp(__fsub_rn(sink, top));
+ fa[tid] = af;
+ fd[tid] = __fmaf_rn(l, af, e);
+ }
+ __syncthreads();
+ const int at = row_pos(a, r);
+ const float* csr = a.cs + static_cast(at) * a.csst;
+ for (int u = tid; u < BMQ * (D / 4); u += NT) { // float4 units: head hl, columns d .. d + 3
+ const int hl = u / (D / 4), d = (u % (D / 4)) * 4, h = hb * BMQ + hl;
+ float o[4];
+ for (int c = 0; c < NCH; ++c) {
+ float4 v = make_float4(0.f, 0.f, 0.f, 0.f);
+ if (chunk_works(a, r, c))
+ v = __ldcg(reinterpret_cast(a.po + ((static_cast(c) * R + r) * H + h) * D
+ + d));
+ const float co[4] = {v.x, v.y, v.z, v.w};
+ const float aa = ca[c * BMQ + hl], bb = cb[c * BMQ + hl];
+ for (int i = 0; i < 4; ++i)
+ o[i] = c == 0 ? __fmaf_rn(bb, co[i], 0.f) : __fmaf_rn(o[i], aa, __fmul_rn(co[i], bb));
+ }
+ float x[4];
+ for (int i = 0; i < 4; ++i) x[i] = div_full(__fmul_rn(o[i], fa[hl]), fd[hl]);
+ uint16_t ob[4];
+ for (int i = 0; i < 4; i += 2) { // pair (d + i, d + i + 1): GPT-J, inverse
+ const int p = (d + i) >> 1;
+ const float c = p >= 224 ? csr[p - 224] : 1.f, s = p >= 224 ? csr[32 + p - 224] : 0.f;
+ const float ne = __fmaf_rn(x[i], c, __fmul_rn(x[i + 1], s));
+ const float no = __fmaf_rn(x[i + 1], c, -__fmul_rn(x[i], s));
+ const __nv_bfloat16 b0 = __float2bfloat16_rn(ne), b1 = __float2bfloat16_rn(no);
+ ob[i] = *reinterpret_cast(&b0);
+ ob[i + 1] = *reinterpret_cast(&b1);
+ }
+ uint2 w;
+ w.x = ob[0] | (static_cast(ob[1]) << 16);
+ w.y = ob[2] | (static_cast(ob[3]) << 16);
+ *reinterpret_cast(a.out + (static_cast(r) * H + h) * D + d) = w;
+ }
+}
+
+template
+__global__ void __launch_bounds__(NT, 1) attn_kernel(Args a) {
+ extern __shared__ __align__(16) uint8_t sm[];
+ const int c = blockIdx.x, hb = blockIdx.y, r = blockIdx.z;
+ const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5, g = lane >> 2, t = lane & 3;
+ const int H = a.H, R = a.R;
+ if (!chunk_works(a, r, c)) return;
+ int* phys = reinterpret_cast(sm + SM_PHYS);
+ const long long sl = ROWS ? a.sl[r] : 0;
+ if (tid < CH) { // the chunk's 128 keys -> physical rows (-1: masked)
+ int row = -1;
+ if (c < NCOMP) {
+ const int idx = c * CH + tid;
+ if (idx < a.cnt[r]) {
+ const int tk = a.tok[static_cast(r) * a.ts + idx];
+ if (tk >= 0) {
+ row = tk;
+ if (a.psh > 0) {
+ const int* ptr = a.pt + (ROWS ? sl * a.pts : 0);
+ row = (ptr[tk >> a.psh] << a.psh) + (tk & ((1 << a.psh) - 1));
+ }
+ }
+ }
+ } else {
+ const int hi = HAS_HI ? a.hi[r] : row_pos(a, r);
+ const int lo = max(a.lo[r], hi - (WINDOW - 1));
+ const int p = hi - (WINDOW - 1) + tid;
+ if (p >= lo && p >= 0) row = (p % a.ring) + (ROWS ? static_cast(sl) * a.ring : 0);
+ }
+ phys[tid] = row;
+ }
+ { // q -> A fragments (Triton's k order)
+ const uint16_t* qb = a.q + (static_cast(r) * H + hb * BMQ) * D;
+ for (int i = tid; i < 32 * 32; i += NT) {
+ const int s = i >> 5, ln = i & 31, gg = ln >> 2, tt = ln & 3;
+ const int k0 = klo(s, tt), k1 = khi(s, tt);
+ uint4 f;
+ f.x = *reinterpret_cast(qb + gg * D + k0);
+ f.y = *reinterpret_cast(qb + (gg + 8) * D + k0);
+ f.z = *reinterpret_cast(qb + gg * D + k1);
+ f.w = *reinterpret_cast(qb + (gg + 8) * D + k1);
+ reinterpret_cast(sm + SM_Q)[i] = f;
+ }
+ }
+ __syncthreads();
+ { // the rows: 72 value pieces + 1 scale piece a key
+ const bool comp = c < NCOMP;
+ const uint8_t* V = comp ? a.cv : a.sv;
+ const uint8_t* S = comp ? a.csc : a.ssc;
+ const long long vs = comp ? a.cvs : a.svs, ss = comp ? a.css : a.sss;
+ const uint32_t base = static_cast(__cvta_generic_to_shared(sm));
+ for (int i = tid; i < CH * 73; i += NT) {
+ const int key = i / 73, pc = i % 73, row = phys[key];
+ const bool ok = row >= 0;
+ const long long rr = ok ? row : 0;
+ if (pc < 72) cp8(base + SM_KV + swz(key, pc * 8), V + rr * vs + pc * 8, ok);
+ else cp8(base + SM_SC + key * SB, S + rr * ss, ok);
+ }
+ asm volatile("cp.async.commit_group;\ncp.async.wait_group 0;" ::: "memory");
+ }
+ __syncthreads();
+ const float NINF = -INFINITY;
+ const int n8 = warp & 3, ta = warp >> 2; // this warp: S of tiles ta and ta + 2, block n8
+ float sv[2][4] = {{0.f, 0.f, 0.f, 0.f}, {0.f, 0.f, 0.f, 0.f}};
+ {
+ const int kA = 32 * ta + 8 * n8 + g, kB = kA + 64;
+ const uint4* qf = reinterpret_cast(sm + SM_Q);
+#pragma unroll 4
+ for (int s = 0; s < 32; ++s) {
+ const uint4 af = qf[s * 32 + lane];
+ const int k0 = klo(s, t), k1 = khi(s, t);
+ mma(sv[0], af, pair_k(sm, kA, k0), pair_k(sm, kA, k1));
+ mma(sv[1], af, pair_k(sm, kB, k0), pair_k(sm, kB, k1));
+ }
+ }
+ float* tmax = reinterpret_cast(sm + SM_TMAX);
+ float* wsum = reinterpret_cast(sm + SM_WSUM);
+ float* rowv = reinterpret_cast(sm + SM_ROW); // [tile][row]: nm, alpha, active at 0 / 64 / 128
+ bool val[2][2];
+ for (int j = 0; j < 2; ++j) {
+ const int tile = ta + 2 * j, kc = 32 * tile + 8 * n8 + 2 * t;
+ val[j][0] = phys[kc] >= 0;
+ val[j][1] = phys[kc + 1] >= 0;
+ for (int e = 0; e < 4; ++e)
+ sv[j][e] = val[j][e & 1] ? __fmul_rn(sv[j][e], __int_as_float(0x3D3504F3)) : NINF;
+ float m0 = fmaxf(sv[j][0], sv[j][1]), m1 = fmaxf(sv[j][2], sv[j][3]);
+ m0 = fmaxf(m0, __shfl_xor_sync(FULL, m0, 2));
+ m0 = fmaxf(m0, __shfl_xor_sync(FULL, m0, 1));
+ m1 = fmaxf(m1, __shfl_xor_sync(FULL, m1, 2));
+ m1 = fmaxf(m1, __shfl_xor_sync(FULL, m1, 1));
+ if (t == 0) {
+ tmax[(tile * 4 + n8) * 16 + g] = m0;
+ tmax[(tile * 4 + n8) * 16 + g + 8] = m1;
+ }
+ }
+ __syncthreads();
+ if (tid < BMQ) { // the running max over the tiles, a row a thread
+ float m = NINF;
+ for (int tile = 0; tile < 4; ++tile) {
+ const float* tm4 = tmax + tile * 64 + tid;
+ const float tm = fmaxf(fmaxf(tm4[0], tm4[16]), fmaxf(tm4[32], tm4[48]));
+ const bool act = tm != NINF;
+ const float nm = act ? fmaxf(m, tm) : m;
+ const float al = act ? (m == NINF ? 0.f : texp(__fsub_rn(m, nm))) : 1.f;
+ rowv[tile * 16 + tid] = nm;
+ rowv[64 + tile * 16 + tid] = al;
+ rowv[128 + tile * 16 + tid] = act ? 1.f : 0.f;
+ m = nm;
+ }
+ reinterpret_cast(sm + SM_MISC)[tid] = m;
+ }
+ __syncthreads();
+ uint32_t* pf = reinterpret_cast(sm + SM_P); // [tile][step][lane][4]
+ for (int j = 0; j < 2; ++j) { // p, the thread / quad sums, bf16(p) -> fragments
+ const int tile = ta + 2 * j;
+ float p[4];
+ for (int e = 0; e < 4; ++e) {
+ const int row = g + (e >> 1) * 8;
+ const bool act = rowv[128 + tile * 16 + row] != 0.f;
+ p[e] = (val[j][e & 1] && act) ? texp(__fsub_rn(sv[j][e], rowv[tile * 16 + row])) : 0.f;
+ }
+ float a0 = __fadd_rn(p[0], p[1]), a1 = __fadd_rn(p[2], p[3]);
+ a0 = __fadd_rn(a0, __shfl_xor_sync(FULL, a0, 2));
+ a0 = __fadd_rn(a0, __shfl_xor_sync(FULL, a0, 1));
+ a1 = __fadd_rn(a1, __shfl_xor_sync(FULL, a1, 2));
+ a1 = __fadd_rn(a1, __shfl_xor_sync(FULL, a1, 1));
+ if (t == 0) {
+ wsum[(tile * 4 + n8) * 16 + g] = a0;
+ wsum[(tile * 4 + n8) * 16 + g + 8] = a1;
+ }
+ int step, tq, hi;
+ frag_of(8 * n8 + 2 * t, step, tq, hi);
+ const __nv_bfloat162 lo2 = __floats2bfloat162_rn(p[0], p[1]), hi2 = __floats2bfloat162_rn(p[2], p[3]);
+ uint32_t* f = pf + ((tile * 2 + step) * 32 + g * 4 + tq) * 4;
+ f[2 * hi] = *reinterpret_cast(&lo2); // reg 0 / 2: row g; 1 / 3: row g + 8
+ f[2 * hi + 1] = *reinterpret_cast(&hi2);
+ }
+ __syncthreads();
+ if (tid < BMQ) { // l: fma(l, alpha, (W0 + W2) + (W1 + W3))
+ float l = 0.f;
+ for (int tile = 0; tile < 4; ++tile) {
+ const float* w4 = wsum + tile * 64 + tid;
+ const float sum = __fadd_rn(__fadd_rn(w4[0], w4[32]), __fadd_rn(w4[16], w4[48]));
+ l = __fmaf_rn(l, rowv[64 + tile * 16 + tid], sum);
+ }
+ reinterpret_cast(sm + SM_MISC)[16 + tid] = l;
+ }
+ float acc[8][4];
+ for (int j = 0; j < 8; ++j)
+ for (int e = 0; e < 4; ++e) acc[j][e] = 0.f;
+ for (int tile = 0; tile < 4; ++tile) { // o = mma(P, kv, rn(o * alpha)), 2 k steps a tile
+ const float al0 = rowv[64 + tile * 16 + g], al1 = rowv[64 + tile * 16 + g + 8];
+ for (int j = 0; j < 8; ++j) {
+ acc[j][0] = __fmul_rn(acc[j][0], al0);
+ acc[j][1] = __fmul_rn(acc[j][1], al0);
+ acc[j][2] = __fmul_rn(acc[j][2], al1);
+ acc[j][3] = __fmul_rn(acc[j][3], al1);
+ }
+ for (int step = 0; step < 2; ++step) {
+ const uint4 af = reinterpret_cast(pf)[(tile * 2 + step) * 32 + lane];
+ const int k0 = 32 * tile + klo(step, t), k1 = 32 * tile + khi(step, t);
+#pragma unroll
+ for (int j = 0; j < 8; ++j) {
+ const int n = 64 * j + 8 * warp + g;
+ mma(acc[j], af, pair_n(sm, k0, k0 + 1, n), pair_n(sm, k1, k1 + 1, n));
+ }
+ }
+ }
+ const long long pbase = (static_cast(c) * R + r) * H + hb * BMQ;
+ for (int j = 0; j < 8; ++j) {
+ const int n = 64 * j + 8 * warp + 2 * t;
+ *reinterpret_cast(a.po + (pbase + g) * D + n) = make_float2(acc[j][0], acc[j][1]);
+ *reinterpret_cast(a.po + (pbase + g + 8) * D + n) = make_float2(acc[j][2], acc[j][3]);
+ }
+ if (tid < BMQ) {
+ a.pm[pbase + tid] = reinterpret_cast(sm + SM_MISC)[tid];
+ a.pl[pbase + tid] = reinterpret_cast(sm + SM_MISC)[16 + tid];
+ }
+ int* flag = reinterpret_cast(sm + SM_MISC) + 32;
+ __threadfence();
+ __syncthreads();
+ if (tid == 0) {
+ int need = 0;
+ for (int cc = 0; cc < NCH; ++cc) need += chunk_works(a, r, cc) ? 1 : 0;
+ *flag = atomicAdd(a.ticket + r * (H / BMQ) + hb, 1) == need - 1;
+ }
+ __syncthreads();
+ if (!*flag) return;
+ __threadfence();
+ if (tid == 0) atomicExch(a.ticket + r * (H / BMQ) + hb, 0); // self-cleaning: the next launch / replay
+ merge(a, sm, r, hb);
+}
+
+template
+cudaError_t launch_one(const Args& a, cudaStream_t stream) {
+ static bool init = false;
+ if (!init) {
+ const cudaError_t e = cudaFuncSetAttribute(attn_kernel,
+ cudaFuncAttributeMaxDynamicSharedMemorySize, SMEM);
+ if (e != cudaSuccess) return e;
+ init = true;
+ }
+ attn_kernel<<>>(a);
+ return cudaGetLastError();
+}
+
+#include "attn_split.cuh"
+
+template
+cudaError_t launch_split(const Args& a, cudaStream_t stream) {
+ using G = Split;
+ static bool init = false;
+ if (!init) {
+ const cudaError_t e = cudaFuncSetAttribute(split_kernel,
+ cudaFuncAttributeMaxDynamicSharedMemorySize, G::SMEM);
+ if (e != cudaSuccess) return e;
+ init = true;
+ }
+ cudaLaunchConfig_t cfg = {};
+ cfg.gridDim = dim3(NCH * P, a.H / BMQ, a.R);
+ cfg.blockDim = dim3(NT);
+ cfg.dynamicSmemBytes = G::SMEM;
+ cfg.stream = stream;
+ cudaLaunchAttribute at[1];
+ at[0].id = cudaLaunchAttributeClusterDimension;
+ at[0].val.clusterDim.x = P;
+ at[0].val.clusterDim.y = 1;
+ at[0].val.clusterDim.z = 1;
+ cfg.attrs = at;
+ cfg.numAttrs = P > 1 ? 1 : 0;
+ return cudaLaunchKernelEx(&cfg, split_kernel, a);
+}
+
+template inline cudaError_t dispatch_split(const Args& a, int kw, bool rows, bool has_hi, cudaStream_t s) {
+ switch ((kw == 4 ? 4 : 0) + (rows ? 2 : 0) + (has_hi ? 1 : 0)) {
+ case 0: return launch_split<2, false, false, P>(a, s);
+ case 1: return launch_split<2, false, true, P>(a, s);
+ case 2: return launch_split<2, true, false, P>(a, s);
+ case 3: return launch_split<2, true, true, P>(a, s);
+ case 4: return launch_split<4, false, false, P>(a, s);
+ case 5: return launch_split<4, false, true, P>(a, s);
+ case 6: return launch_split<4, true, false, P>(a, s);
+ default: return launch_split<4, true, true, P>(a, s);
+ }
+}
+
+// split: 0 attn_kernel (the default launch), 1 / 4 split_kernel with that many CTAs a chunk (the same bits)
+inline cudaError_t dispatch(const Args& a, int kw, bool rows, bool has_hi, cudaStream_t stream, int split = 0) {
+ if (a.R < 1 || a.H % BMQ || a.ring < WINDOW || (a.ring & (a.ring - 1))) return cudaErrorInvalidValue;
+ if (split == 1) return dispatch_split<1>(a, kw, rows, has_hi, stream);
+ if (split == 4) return dispatch_split<4>(a, kw, rows, has_hi, stream);
+ if (split != 0) return cudaErrorInvalidValue;
+ switch ((kw == 4 ? 4 : 0) + (rows ? 2 : 0) + (has_hi ? 1 : 0)) {
+ case 0: return launch_one<2, false, false>(a, stream);
+ case 1: return launch_one<2, false, true>(a, stream);
+ case 2: return launch_one<2, true, false>(a, stream);
+ case 3: return launch_one<2, true, true>(a, stream);
+ case 4: return launch_one<4, false, false>(a, stream);
+ case 5: return launch_one<4, false, true>(a, stream);
+ case 6: return launch_one<4, true, false>(a, stream);
+ default: return launch_one<4, true, true>(a, stream);
+ }
+}
+
+} // namespace dsv41_attn
+
+#ifndef DSV41_ATTN_NO_TORCH // the compile test builds the kernels alone (nvcc, no torch headers)
+#include
+#include
+#include
+
+namespace {
+template T* tp(const at::Tensor& t) { return t.numel() ? static_cast(t.data_ptr()) : nullptr; }
+}
+
+void dsv41_attn_run_cuda(const at::Tensor& q, const at::Tensor& cv, const at::Tensor& csc, const at::Tensor& tok,
+ const at::Tensor& cnt, const at::Tensor& sv, const at::Tensor& ssc, const at::Tensor& lo,
+ const at::Tensor& hi, const at::Tensor& pos, const at::Tensor& sl, const at::Tensor& pt,
+ const at::Tensor& sink, const at::Tensor& cs, at::Tensor& po, at::Tensor& pm,
+ at::Tensor& pl, at::Tensor& out, at::Tensor& ticket, int64_t ring, int64_t psh,
+ int64_t pts, int64_t kw, int64_t split, int64_t qrope) {
+ using namespace dsv41_attn;
+ Args a{};
+ a.q = tp(q);
+ a.has_comp = cv.numel() ? 1 : 0;
+ a.cv = tp(cv); a.csc = tp(csc);
+ a.cvs = cv.numel() ? cv.stride(0) : 0; a.css = csc.numel() ? csc.stride(0) : 0;
+ a.tok = tp(tok); a.ts = tok.numel() ? tok.stride(0) : 0; a.cnt = tp(cnt);
+ a.sv = tp(sv); a.ssc = tp(ssc); a.svs = sv.stride(0); a.sss = ssc.stride(0);
+ a.lo = tp(lo); a.hi = tp(hi);
+ a.pos = pos.data_ptr();
+ a.sl = tp(sl);
+ a.pt = tp(pt); a.pts = pts; a.psh = static_cast(psh);
+ a.sink = tp(sink); a.cs = tp(cs); a.csst = cs.stride(0);
+ a.po = tp(po); a.pm = tp(pm); a.pl = tp(pl);
+ a.out = tp(out); a.ticket = tp(ticket);
+ a.R = static_cast(q.size(0)); a.H = static_cast(q.size(1)); a.ring = static_cast(ring);
+ a.qrope = qrope && split > 0 ? 1 : 0;
+ TORCH_CHECK(!qrope || split > 0, "attn_cuda: q's RoPE folds into the split kernel only");
+ C10_CUDA_CHECK(dispatch(a, static_cast(kw), sl.numel() > 0, hi.numel() > 0,
+ at::cuda::getCurrentCUDAStream(), static_cast(split)));
+}
+
+std::vector dsv41_attn_info_cuda(int64_t device) { // GB10's limits for the two launches
+ int sms = 0, optin = 0, cl = 0, blocks = 0;
+ cudaDeviceGetAttribute(&sms, cudaDevAttrMultiProcessorCount, static_cast(device));
+ cudaDeviceGetAttribute(&optin, cudaDevAttrMaxSharedMemoryPerBlockOptin, static_cast(device));
+ cudaDeviceGetAttribute(&cl, cudaDevAttrClusterLaunch, static_cast(device));
+ cudaFuncSetAttribute(dsv41_attn::attn_kernel<4, true, false>, cudaFuncAttributeMaxDynamicSharedMemorySize,
+ dsv41_attn::SMEM);
+ cudaOccupancyMaxActiveBlocksPerMultiprocessor(&blocks, dsv41_attn::attn_kernel<4, true, false>, dsv41_attn::NT,
+ dsv41_attn::SMEM);
+ return {sms, optin, cl, blocks, dsv41_attn::SMEM};
+}
+#endif
+
+// The instantiations dispatch() launches: attn_kernel.
+template __global__ void dsv41_attn::attn_kernel<2, false, false>(dsv41_attn::Args);
+template __global__ void dsv41_attn::attn_kernel<2, false, true>(dsv41_attn::Args);
+template __global__ void dsv41_attn::attn_kernel<2, true, false>(dsv41_attn::Args);
+template __global__ void dsv41_attn::attn_kernel<2, true, true>(dsv41_attn::Args);
+template __global__ void dsv41_attn::attn_kernel<4, false, false>(dsv41_attn::Args);
+template __global__ void dsv41_attn::attn_kernel<4, false, true>(dsv41_attn::Args);
+template __global__ void dsv41_attn::attn_kernel<4, true, false>(dsv41_attn::Args);
+template __global__ void dsv41_attn::attn_kernel<4, true, true>(dsv41_attn::Args);
+// dispatch_split's split_kernel at P 1 and 4 (a cluster of 4)
+template __global__ void dsv41_attn::split_kernel<2, false, false, 1>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<2, false, true, 1>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<2, true, false, 1>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<2, true, true, 1>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<4, false, false, 1>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<4, false, true, 1>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<4, true, false, 1>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<4, true, true, 1>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<2, false, false, 4>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<2, false, true, 4>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<2, true, false, 4>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<2, true, true, 4>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<4, false, false, 4>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<4, false, true, 4>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<4, true, false, 4>(dsv41_attn::Args);
+template __global__ void dsv41_attn::split_kernel<4, true, true, 4>(dsv41_attn::Args);
diff --git a/zig/kernels/cuda/deepseek_v41/attn_split.cuh b/zig/kernels/cuda/deepseek_v41/attn_split.cuh
new file mode 100644
index 0000000000..cd70b1594a
--- /dev/null
+++ b/zig/kernels/cuda/deepseek_v41/attn_split.cuh
@@ -0,0 +1,538 @@
+// attn_cuda.cu's split_kernel (TF_DSV41_ATTN_SPLIT): its helpers and the cluster kernel, inside namespace dsv41_attn
+#pragma once
+// ---- split_kernel (TF_DSV41_ATTN_SPLIT=P, 1 / 4): a chunk over a CLUSTER of P CTAs, attn_kernel's bits at any P ---
+constexpr int MAXP = 4; // the ticket's ranks a (row, head tile)
+
+__host__ __device__ constexpr int col2byte(int n) { return n <= NOPE ? n : NOPE + 2 * (n - NOPE); }
+
+template struct Split {
+ static constexpr int TPC = 4 / P; // S tiles a CTA
+ static constexpr int KS = 32 * TPC; // S keys a CTA
+ static constexpr int W = D / P; // PV / merge columns a CTA
+ static constexpr int J = 8 / P; // n8 blocks a warp in PV
+ static constexpr int NPW = TPC == 4 ? 2 : 1; // S chains a warp ((tile, n8) pairs: 4 TPC over 8 warps)
+ static constexpr int SLB = P == 1 ? VB : col2byte(D) - col2byte(D - W); // slice pitch (the widest rank's)
+ static constexpr int SM_Q = 0; // q A fragments 16384
+ static constexpr int SM_KP = 16384; // PV slice: 128 keys x SLB (P = 1: the whole rows)
+ static constexpr int SM_KS = P == 1 ? SM_KP : SM_KP + CH * SLB; // S rows: KS keys x 576 (P = 1: the slice)
+ static constexpr int SM_SC = SM_KS + KS * VB; // 128 keys x 8 scale bytes 1024
+ static constexpr int SM_KB = SM_SC + CH * SB; // P > 1: S's B operands as bf16 pairs [KS][256] words
+ static constexpr int SM_P = SM_KB + (P == 1 ? 0 : KS * D * 2); // P A fragments [4][2][32] uint4 4096
+ static constexpr int SM_TMAX = SM_P + 4096; // [4 tiles][4 n8][16 rows] fp32 (merge: ca, cb, ...)
+ static constexpr int SM_WSUM = SM_TMAX + 1024;
+ static constexpr int SM_ROW = SM_WSUM + 1024; // [4 tiles][16 rows] nm, alpha, active
+ static constexpr int SM_PHYS = SM_ROW + 768; // 128 physical rows (-1: masked)
+ static constexpr int SM_MISC = SM_PHYS + 512; // final m, l [16] each + the ticket flag
+ static constexpr int SMEM = SM_MISC + 256; // P = 1: 98,816 B; P = 4: 100,864 B
+ static constexpr int SM_VB = SM_KB; // P > 1, after S: PV's B pairs [64 key pairs][W] words
+ static_assert(P == 1 || (CH / 2) * W * 4 <= KS * D * 2, "PV's pairs fit where S's were");
+ static_assert(SMEM <= 101376, "shared memory");
+ static_assert(SLB % 64 == 0 && W % 128 == 0, "16-byte swizzle groups; whole n8 blocks a warp");
+};
+
+__device__ __forceinline__ int fsw(int key) { return ((key >> 1) ^ (key >> 3)) & 3; }
+__device__ __forceinline__ int swz16(int key, int x, int pitch) {
+ const int c = x >> 4;
+ return key * pitch + (((c & ~3) | ((c & 3) ^ fsw(key))) << 4) + (x & 15);
+}
+
+// S's B pair (k, k + 1) of the S row `key` (local to the S rows; `ck`: its chunk key, for the scale)
+template
+__device__ __forceinline__ uint32_t spair_k(const uint8_t* sm, int key, int ck, int k) {
+ using G = Split;
+ if (k < NOPE) {
+ const uint32_t two = *reinterpret_cast(sm + G::SM_KS + swz16(key, k, VB));
+ const float s = pow2b(sm[G::SM_SC + ck * SB + (k >> 6)]);
+ return deq2(two, s, s);
+ }
+ return *reinterpret_cast(sm + G::SM_KS + swz16(key, NOPE + 2 * (k - NOPE), VB));
+}
+
+// P > 1: S's B operands dequantized once into bf16 pairs, word k / 2 of S row `key` at w ^ bsw(key): conflict-free
+template __device__ __forceinline__ int bsw(int key) {
+ return (KW == 4 ? (key & 1) : ((key & 1) << 2)) | (((key >> 1) & 3) << 3);
+}
+template
+__device__ __forceinline__ uint32_t sbf_k(const uint8_t* sm, int key, int k) {
+ return reinterpret_cast(sm + Split::SM_KB)[key * (D / 2) + ((k >> 1) ^ bsw(key))];
+}
+
+// PV's B pair: value (k0, n), (k1, n) of the slice (byte x0: the rank's first byte)
+template
+__device__ __forceinline__ uint32_t spair_n(const uint8_t* sm, int k0, int k1, int n, int x0) {
+ using G = Split;
+ if (n < NOPE) {
+ const int x = n - x0;
+ const uint32_t two = sm[G::SM_KP + swz16(k0, x, G::SLB)] |
+ (static_cast(sm[G::SM_KP + swz16(k1, x, G::SLB)]) << 8);
+ return deq2(two, pow2b(sm[G::SM_SC + k0 * SB + (n >> 6)]), pow2b(sm[G::SM_SC + k1 * SB + (n >> 6)]));
+ }
+ const int x = col2byte(n) - x0;
+ return *reinterpret_cast(sm + G::SM_KP + swz16(k0, x, G::SLB)) |
+ (static_cast(*reinterpret_cast(sm + G::SM_KP + swz16(k1, x, G::SLB))) << 16);
+}
+
+// P > 1: PV's B pairs dequantized once, word (key pair kp, column x) at x ^ vsw(kp): conflict-free
+template __device__ __forceinline__ int vsw(int kp) { return (KW == 4 ? (kp >> 1) & 3 : kp & 3) << 3; }
+
+// TF_DSV41_ATTN_ROPE: q's rope pair j in place, (rn(e c) - rn(o s), rn(o c) + rn(e s)), no contraction, then bf16
+__device__ __forceinline__ uint32_t rope_word(uint32_t w, const float* csr, int j) {
+ const float e = __uint_as_float(w << 16), o = __uint_as_float(w & 0xffff0000u);
+ const float c = csr[j], sn = csr[32 + j];
+ const __nv_bfloat162 b = __floats2bfloat162_rn(__fsub_rn(__fmul_rn(e, c), __fmul_rn(o, sn)),
+ __fadd_rn(__fmul_rn(o, c), __fmul_rn(e, sn)));
+ return *reinterpret_cast(&b);
+}
+
+__device__ __forceinline__ void cp16(uint32_t dst, const void* src, bool ok) {
+ asm volatile("cp.async.cg.shared.global [%0], [%1], 16, %2;" :: "r"(dst), "l"(src), "r"(ok ? 16 : 0));
+}
+__device__ __forceinline__ void cl_arrive() { asm volatile("barrier.cluster.arrive.release.aligned;" ::: "memory"); }
+__device__ __forceinline__ void cl_wait() { asm volatile("barrier.cluster.wait.acquire.aligned;" ::: "memory"); }
+__device__ __forceinline__ uint32_t cl_rank() {
+ uint32_t r;
+ asm volatile("mov.u32 %0, %%cluster_ctarank;" : "=r"(r));
+ return r;
+}
+// store word v at shared address `p` of cluster rank q (DSMEM)
+__device__ __forceinline__ void st_peer(void* p, uint32_t q, uint32_t v) {
+ uint32_t a = static_cast(__cvta_generic_to_shared(p)), b;
+ asm volatile("mapa.shared::cluster.u32 %0, %1, %2;" : "=r"(b) : "r"(a), "r"(q));
+ asm volatile("st.shared::cluster.u32 [%0], %1;" :: "r"(b), "r"(v) : "memory");
+}
+// word i of shared array `p` in cluster rank q (DSMEM)
+__device__ __forceinline__ uint32_t ld_peer(const void* p, uint32_t q) {
+ uint32_t a = static_cast(__cvta_generic_to_shared(p)), b, v;
+ asm volatile("mapa.shared::cluster.u32 %0, %1, %2;" : "=r"(b) : "r"(a), "r"(q));
+ asm volatile("ld.shared::cluster.u32 %0, [%1];" : "=r"(v) : "r"(b) : "memory");
+ return v;
+}
+
+// stage row bytes [b0, b0 + 16 n) at key slot `key` (16-byte pieces, or 8-byte halves without v16); masked: zeros
+__device__ __forceinline__ void stage_piece(uint32_t base, int region, int key, int pitch, int pc, const uint8_t* src,
+ bool ok, bool v16) {
+ const uint32_t dst = base + region + swz16(key, pc * 16, pitch);
+ if (v16) {
+ cp16(dst, src, ok);
+ } else {
+ cp8(dst, src, ok);
+ cp8(dst + 8, src + 8, ok);
+ }
+}
+
+// the merge of columns [c0, c0 + NC) of (row r, head tile hb): attn_kernel's merge per element, loads first
+template
+__device__ void merge_cols(const Args& a, uint8_t* sm, int smt, int r, int hb, int c0) {
+ const int tid = threadIdx.x, H = a.H, R = a.R;
+ float* ca = reinterpret_cast(sm + smt); // [5][16] a, [5][16] b, [16] af, [16] den
+ float* cb = ca + NCH * BMQ;
+ float* fa = cb + NCH * BMQ;
+ float* fd = fa + BMQ;
+ bool cw[NCH];
+#pragma unroll
+ for (int c = 0; c < NCH; ++c) cw[c] = chunk_works(a, r, c);
+ constexpr int UB = BMQ * (NC / 4) / NT; // float4 units a thread (8 at P = 1, 2 at P = 4)
+ static_assert(UB * NT == BMQ * (NC / 4), "whole units");
+ float4 pv[UB][NCH];
+#pragma unroll
+ for (int ub = 0; ub < UB; ++ub) { // the partials in flight first
+ const int u = tid + ub * NT, hl = u / (NC / 4), d = c0 + (u % (NC / 4)) * 4, h = hb * BMQ + hl;
+#pragma unroll
+ for (int c = 0; c < NCH; ++c)
+ pv[ub][c] = cw[c] ? __ldcg(reinterpret_cast(
+ a.po + ((static_cast(c) * R + r) * H + h) * D + d))
+ : make_float4(0.f, 0.f, 0.f, 0.f);
+ }
+ if (tid < BMQ) { // a head's scalar chain (Triton's _merge, unrolled)
+ const int h = hb * BMQ + tid;
+ float pmv[NCH], plv[NCH];
+#pragma unroll
+ for (int c = 0; c < NCH; ++c) {
+ const long long base = (static_cast(c) * R + r) * H + h;
+ pmv[c] = cw[c] ? __ldcg(a.pm + base) : -INFINITY;
+ plv[c] = cw[c] ? __ldcg(a.pl + base) : 0.f;
+ }
+ const float sink = a.sink[h];
+ float m = -INFINITY, l = 0.f;
+#pragma unroll
+ for (int c = 0; c < NCH; ++c) {
+ const float cm = pmv[c], cl = plv[c];
+ const bool act = cl > 0.f;
+ float aa, bb;
+ if (c == 0) {
+ const float nm = act ? fmaxf(cm, -INFINITY) : -INFINITY;
+ bb = act ? texp(__fsub_rn(cm, nm)) : 0.f;
+ aa = 0.f;
+ l = __fmaf_rn(bb, cl, 0.f);
+ m = nm;
+ } else {
+ const float nm = act ? fmaxf(m, cm) : m;
+ aa = act ? (m == -INFINITY ? 0.f : texp(__fsub_rn(m, nm))) : 1.f;
+ bb = act ? texp(__fsub_rn(cm, nm)) : 0.f;
+ l = __fmaf_rn(l, aa, __fmul_rn(bb, cl));
+ m = nm;
+ }
+ ca[c * BMQ + tid] = aa;
+ cb[c * BMQ + tid] = bb;
+ }
+ const float top = fmaxf(m, sink);
+ const float af = m == -INFINITY ? 0.f : texp(__fsub_rn(m, top));
+ const float e = sink == -INFINITY ? 0.f : texp(__fsub_rn(sink, top));
+ fa[tid] = af;
+ fd[tid] = __fmaf_rn(l, af, e);
+ }
+ __syncthreads();
+ const float* csr = a.cs + static_cast(row_pos(a, r)) * a.csst;
+#pragma unroll
+ for (int ub = 0; ub < UB; ++ub) {
+ const int u = tid + ub * NT, hl = u / (NC / 4), d = c0 + (u % (NC / 4)) * 4, h = hb * BMQ + hl;
+ float o[4];
+#pragma unroll
+ for (int c = 0; c < NCH; ++c) {
+ const float4 v = pv[ub][c];
+ const float co[4] = {v.x, v.y, v.z, v.w};
+ const float aa = ca[c * BMQ + hl], bb = cb[c * BMQ + hl];
+#pragma unroll
+ for (int i = 0; i < 4; ++i)
+ o[i] = c == 0 ? __fmaf_rn(bb, co[i], 0.f) : __fmaf_rn(o[i], aa, __fmul_rn(co[i], bb));
+ }
+ float x[4];
+#pragma unroll
+ for (int i = 0; i < 4; ++i) x[i] = div_full(__fmul_rn(o[i], fa[hl]), fd[hl]);
+ uint16_t ob[4];
+#pragma unroll
+ for (int i = 0; i < 4; i += 2) { // pair (d + i, d + i + 1): GPT-J, inverse
+ const int p = (d + i) >> 1;
+ const float c = p >= 224 ? csr[p - 224] : 1.f, s = p >= 224 ? csr[32 + p - 224] : 0.f;
+ const float ne = __fmaf_rn(x[i], c, __fmul_rn(x[i + 1], s));
+ const float no = __fmaf_rn(x[i + 1], c, -__fmul_rn(x[i], s));
+ const __nv_bfloat16 b0 = __float2bfloat16_rn(ne), b1 = __float2bfloat16_rn(no);
+ ob[i] = *reinterpret_cast(&b0);
+ ob[i + 1] = *reinterpret_cast(&b1);
+ }
+ uint2 w;
+ w.x = ob[0] | (static_cast(ob[1]) << 16);
+ w.y = ob[2] | (static_cast(ob[3]) << 16);
+ *reinterpret_cast(a.out + (static_cast(r) * H + h) * D + d) = w;
+ }
+}
+
+template
+__global__ void __launch_bounds__(NT, 1) split_kernel(Args a) {
+ using G = Split;
+ extern __shared__ __align__(16) uint8_t sm[];
+ const int k = P == 1 ? 0 : static_cast(cl_rank());
+ const int c = blockIdx.x / P, hb = blockIdx.y, r = blockIdx.z;
+ const int tid = threadIdx.x, lane = tid & 31, warp = tid >> 5, g = lane >> 2, t = lane & 3;
+ const int H = a.H, R = a.R;
+ if (!chunk_works(a, r, c)) return; // the whole cluster: no barrier is pending
+ uint4 qv[32 * 32 / NT]; // q's A fragments: every load in flight at once
+ {
+ const uint16_t* qb = a.q + (static_cast(r) * H + hb * BMQ) * D;
+#pragma unroll
+ for (int u = 0; u < 32 * 32 / NT; ++u) {
+ const int i = tid + u * NT, s = i >> 5, ln = i & 31, gg = ln >> 2, tt = ln & 3;
+ const int k0 = klo(s, tt), k1 = khi(s, tt);
+ qv[u].x = *reinterpret_cast(qb + gg * D + k0);
+ qv[u].y = *reinterpret_cast(qb + (gg + 8) * D + k0);
+ qv[u].z = *reinterpret_cast(qb + gg * D + k1);
+ qv[u].w = *reinterpret_cast(qb + (gg + 8) * D + k1);
+ }
+ if (a.qrope) { // the rope dims (k >= 448): their pair's rotation
+ const float* csr = a.cs + static_cast(row_pos(a, r)) * a.csst;
+#pragma unroll
+ for (int u = 0; u < 32 * 32 / NT; ++u) {
+ const int i = tid + u * NT, s = i >> 5, tt = (i & 31) & 3;
+ const int k0 = klo(s, tt), k1 = khi(s, tt);
+ if (k0 >= NOPE) {
+ qv[u].x = rope_word(qv[u].x, csr, (k0 - NOPE) >> 1);
+ qv[u].y = rope_word(qv[u].y, csr, (k0 - NOPE) >> 1);
+ }
+ if (k1 >= NOPE) {
+ qv[u].z = rope_word(qv[u].z, csr, (k1 - NOPE) >> 1);
+ qv[u].w = rope_word(qv[u].w, csr, (k1 - NOPE) >> 1);
+ }
+ }
+ }
+ }
+ int* phys = reinterpret_cast