Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- Qwen3.5 on CUDA: `qwen3_5` and `qwen3_5_moe` declare warm turns, so the server prefills a follow-up's prompt in the background and the next turn resumes past the previous reply, with replies unchanged (#2824). Thanks to @devopsfortunato.

- Server: tool-call markup quoted in Markdown inline code spans and fenced code blocks stays prose, in whole replies and in streaming, instead of opening, hiding or running as a live tool call (#3588). Thanks to @gilby.

- Lanes: shared draft pricing prices a chain-cut, unmeasured tail by the rule's rates instead of as a measured zero, and the cache's cut marker survives allocation errors (#2979). Thanks to @devopsfortunato.

- Docs: the Nemotron 3.5 Lightning recipe gains a "Native HIP on a Strix Halo iGPU" section with its configuration, memory footprint, throughput, prefill and exactness checks, and the intro lists HIP beside Metal and CUDA (#3560). Thanks to @gilby.

- Qwen3.5-2B on Metal: the engine declares `warm_turns` from its prompt cache, so a follow-up that re-renders earlier rows resumes the reply instead of prefilling the whole conversation again (#3457). Thanks to @EugeneClaw.

- Kolibri-1: the tree-attention cache offsets are signed, so a server with default flags no longer panics with an integer overflow on its first request when a cache sits below the origin (#3561). Thanks to @jschmied.

- H3 on Metal: `Model.load` reads every transformer shard ahead of its mappings and fails the load on a missing or short shard; `TF_LOAD_PREFETCH` sets the reader count and `0` turns read-ahead off (#2999). Thanks to @plotarmordev.
Expand Down
44 changes: 37 additions & 7 deletions docs/recipes/nemotron-3.5.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Nemotron 3.5 Lightning

The model combines Mamba-2, attention and MoE blocks. The native family is `zig/src/families/nemotron/`, on Metal
and CUDA; the native prompt reuse, routed experts and shared rounds sections below describe it, and the
The model combines Mamba-2, attention and MoE blocks. The native family is `zig/src/families/nemotron/`, on Metal,
CUDA and HIP; the native prompt reuse, routed experts, shared rounds and HIP sections below describe it, and the
[README](../../README.md#qualified-models) lists its qualified checkpoints and hardware.

The MLX execution, CUDA, draft vocabulary and measurements sections describe the Python engine,
Expand All @@ -21,7 +21,7 @@ The checkpoint includes `mtp-4bit.safetensors`; `pull` and `serve` check that it
`--no-drafts` or request field `"draft": false` selects serial decoding. In the Python engine, MLX supports affine 4-bit
projections and experts in groups of 32 or 64; unsupported formats are refused before weight downloads
and checked again at load. CUDA supports the named 4-bit/group-64 checkpoint. The native engine serves the named
checkpoint on Metal and CUDA, and NVIDIA's NVFP4 export on CUDA.
checkpoint on Metal, CUDA and HIP, and NVIDIA's NVFP4 export on CUDA.

On Metal the native engine also serves MLX checkpoints at a width per module (2, 3, 4, 5, 6 or 8 bits, not 7, in groups
of 32, 64 or 128), such as `TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-oQ4`, `-oQ6` and `-oQ8`, the three
Expand All @@ -31,8 +31,8 @@ width-general gathers; dense matrices in other groups stay on the row kernels. T
`mtp-4bit.safetensors` from the 4-bit checkpoint into the folder to draft; without it each round decodes one token. With
the head on an M3 Ultra, greedy prose decodes at 162 tok/s with oQ4, 117 with oQ6 and 111 with oQ8, against 188 with the
4-bit checkpoint, and prompts of 8,192 to 32,768 tokens read at 0.96x to 0.99x mlx_lm's rate on the same checkpoint; a
server's first 2,048-token prompt after it starts reads at 0.78x to 0.90x. For MLX affine checkpoints, CUDA, the Intel
GPU engine and `--slide` refuse widths other than 4-bit group 64 and name the first such module.
server's first 2,048-token prompt after it starts reads at 0.78x to 0.90x. For MLX affine checkpoints, CUDA, HIP, the
Intel GPU engine and `--slide` refuse widths other than 4-bit group 64 and name the first such module.

## MLX execution and exactness

Expand Down Expand Up @@ -92,8 +92,8 @@ for checkpoint requirements, request parts, limits and qualification status.

This section describes the Python 0.6.6 engine's list,
[`src/tensorfold/families/nemotron_h/draft_ids.txt`](https://github.com/ashhart/TensorFold/blob/v0.6.6/src/tensorfold/families/nemotron_h/draft_ids.txt).
The native engine embeds its own 49,152-ID list, `zig/src/families/nemotron/draft_ids.txt`, on Metal and CUDA; the
rebuild below does not produce it.
The native engine embeds its own 49,152-ID list, `zig/src/families/nemotron/draft_ids.txt`, on Metal, CUDA and HIP;
the rebuild below does not produce it.

The shipped `draft_ids.txt` contains 32,768 sorted IDs. It can be rebuilt byte for byte from CPython
3.14.5's standard-library Python files, excluding `site-packages`, plus tracked Python and Markdown
Expand Down Expand Up @@ -233,3 +233,33 @@ with 4,096 reply tokens takes 13 pages (45.5 MiB) instead of a whole window. A r
until a stream ends. The attention kernels read the pages through a table at a fixed device address, so the graphs
keep their addresses. The startup line shows the pool. `TF_KV_PAGES=0` gives every stream a window again;
`TF_KV_PAGES=all` puts every stream on pages (for checks).

## Native HIP on a Strix Halo iGPU

The native HIP build serves `TensorFold/NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit` (`d9d758fb`, MLX affine 4-bit
in groups of 64 with `mtp-4bit.safetensors`) on gfx1151 (AMD Ryzen AI MAX+ 395, Radeon 8060S, 128 GB unified
LPDDR5X-8000, 512 MiB carve-out + 115 GiB GTT). At `--context 131072 --parallel 4 --prompt-cache-gib 0`, it plans
17.34 GiB of model weights plus 1.15 GiB per stream (24.42 GiB GTT in use after load), runs 4-row verify windows with
up to 3 MTP drafts sized by measured costs, prefills prompts in 2,048-row chunks (`prefill_step=4096` default
clamped to the family's chunk width), and replays captured round graphs on lone-stream rounds. `--mtp-drafts` caps
the draft count at up to 3. `--mtp-confidence` and mixed-width oQ checkpoints are refused by name on main.

```bash
zig build native-hip -Dhip=true -Dhip-arch=gfx1151 -Dhipcc=/usr/bin/hipcc -Doptimize=ReleaseFast
TENSORFOLD_MEMORY_RESERVE_GIB=4 ./zig-out/native-hip/bin/tensorfold-native serve \
./NVIDIA-Nemotron-3.5-Lightning-30B-A3B-MLX-4bit --context 131072 --parallel 4 --prompt-cache-gib 0
```

Measured on TensorFold `6c5cef894` (HIP 7.1, clang 21, Zig 0.17.0, power mode `performance`, fresh servers with the page
cache dropped before each run; `tools/bench_openai.py --tokens 256 --reps 3`,
`tools/bench_concurrent.py --levels 1,2,4 --reps 3 --alone --serial --temperatures 1.0,0 --prompts tools/bench_prompts.json`,
and `tools/bench_cache.py --require-cold --tokens 16 --repeats 1` over raw Nemotron-tokenized prompts; two full
servers plus one `--no-drafts` server):

| Workload | Metric | T=1.0 | T=0 | Notes |
|---|---|---|---|---|
| `bench_openai` code (drafts on) | median tok/s | 117.1 / 117.1 | 122.7 / 124.6 | 2.78 / 2.84 (`T=1.0`) and 2.84 / 2.56 (`T=0`) tokens/round |
| `bench_openai` chat (drafts on) | median tok/s | 92.4 / 91.8 | 99.0 / 98.5 | 1.68 (`T=1.0`) and 1.84 (`T=0`) tokens/round |
| `bench_openai` code / chat (`--no-drafts`) | median tok/s | 86.2 / 86.1 | 87.9 / 87.9 | 1.00 token/round (1.828 GB read/token, ~160.7 GB/s greedy) |
| `bench_concurrent` 1 / 2 / 4 streams | aggregate tok/s | 124.4 / 136.0-137.0 / 168.4-168.9 | 131.5-132.4 / 146.5 / 175.5-176.4 | 84/84 concurrent=alone; 48/48 alone=`"draft": false` |
| `bench_cache` 2,048 / 7,099 / 28,333 tokens | cold TTFT (s) | — | 0.731-0.732 / 2.672-2.676 / 13.130-13.205 | 2,797-2,800 / 2,653-2,657 / 2,146-2,158 tok/s (repeated cold) |
6 changes: 6 additions & 0 deletions zig/build/cuda_family_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ pub fn steps(b: *std.Build, cuda: *std.Build.Module, mods: anytype, step: *std.B
const prefill = b.addRunArtifact(b.addTest(.{ .root_module = mods.qwen35, .filters = &.{ "Qwen stepped prefill:", "Qwen whole prefill:", "Qwen prefill opening:", "a yielded pass keeps its completed prefix" } }));
b.step("test-qwen35-prefill-host", "Stepped, whole, opening and interrupted prompt lifecycle without devices").dependOn(&prefill.step);
step.dependOn(&prefill.step);
const warm_turns = b.addRunArtifact(b.addTest(.{ .root_module = mods.qwen35, .filters = &.{"Qwen warm turns:"} }));
b.step("test-qwen35-warm-turns-host", "Qwen3.5's warm turn wait and usable ends declared for both CUDA entry points without devices").dependOn(&warm_turns.step);
step.dependOn(&warm_turns.step);
const draft_config = b.addRunArtifact(b.addTest(.{ .root_module = mods.qwen35, .filters = &.{"Qwen DFlash2 config:"} }));
b.step("test-qwen35-draft-config-host", "DFlash2 attention declarations agree with the CUDA kernels before loading").dependOn(&draft_config.step);
step.dependOn(&draft_config.step);
Expand Down Expand Up @@ -216,6 +219,9 @@ pub fn steps(b: *std.Build, cuda: *std.Build.Module, mods: anytype, step: *std.B
pub fn registerCopyFamilies(b: *std.Build, native: *std.Build.Module, step: *std.Build.Step) void {
b.step("test-glm-formats-host", "CUDA GLM EXL3 offer reaches the published family registry without a GPU").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"GLM capabilities:"} })).step);
b.step("test-request-starts-host", "Opt-in CUDA message starts without GPU execution").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"request starts:"} })).step);
const warm_rules = b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"CUDA warm rules:"} }));
b.step("test-cuda-warm-rules-host", "A family's warm rules and its own quiet wait without devices").dependOn(&warm_rules.step);
step.dependOn(&warm_rules.step);
b.step("test-glm-window-prices-host", "GLM timed lone and unchanged shared price values without devices").dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"GLM window prices:"} })).step);
const family_copies = b.addRunArtifact(b.addTest(.{ .root_module = native, .filters = &.{"CUDA lane copies:"} }));
b.step("test-family-copies-host", "Actual CUDA family copy policies through the lane core").dependOn(&family_copies.step);
Expand Down
2 changes: 1 addition & 1 deletion zig/qwen35_cost_open_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn open(root: []const u8, expected: usize, capacity: usize) ![64]f64 {
h.* = .{ .gpa = a, .gpu = null, .family = &probe, .release = Probe.release, .inner = backend.backend(), .vtable = undefined, .cfg = undefined, .clock = undefined, .core = undefined, .host = undefined, .served = .{ .backend = backend.backend(), .facts = facts, .stream_bytes = 0, .prepare = .{ .ctx = &probe, .run = Probe.run } } };
Probe.opening = true;
defer Probe.opening = false;
try h.serve(io, facts, 64, .{ .lanes = 1 }, null);
try h.serve(io, facts, 64, .{ .lanes = 1 }, null, 0);
defer {
h.host.stop();
h.core.deinit();
Expand Down
6 changes: 6 additions & 0 deletions zig/src/core/lane_host.zig
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ pub const LaneHost = struct {
return .{ .ctx = h, .vtable = if (vt.score != null and vt.decide != null) &decided_vtable else if (vt.score != null) &scored_vtable else &plain_vtable };
}

/// Advertise the cache capabilities: warm turns follow the store, so a cache-less start claims none.
pub fn advertiseCache(h: *LaneHost, store: ?*pc.Store) void {
h.info_.prompt_cache = store != null;
h.info_.warm_turns = store != null;
}

const plain_vtable: Engine.VTable = .{
.info = infoFn,
.submit = submitFn,
Expand Down
63 changes: 63 additions & 0 deletions zig/src/core/lane_host_warm_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const std = @import("std");
const lanes = @import("lanes");
const api = @import("engine_api.zig");
const LaneHost = @import("lane_host.zig").LaneHost;
const prompt_cache = @import("prompt_cache.zig");
const Reason = api.Reason;
const Id = api.Id;
const Event = api.Event;
Expand Down Expand Up @@ -235,6 +236,58 @@ test "a warm_usable store keeps a warm pass's grid mark but not its end off the
}
}

test "a one-snapshot store keeps the warm pass's usable mark over an unusable end; the next request resumes the mark" {
const gpa = std.testing.allocator;
const pc = api.prompt_cache;
const Snaps = struct {
fn bytes(_: *anyopaque, at: u32) u64 {
return 64 + 4 * @as(u64, at);
}
fn save(ptr: *anyopaque, owner: ?*anyopaque, at: u32) anyerror!pc.Saved {
const f: *lanes.fake.Fake = @ptrCast(@alignCast(ptr));
return f.save(@ptrCast(@alignCast(owner.?)), at);
}
fn restore(_: *anyopaque, _: ?*anyopaque, _: pc.Saved) anyerror!void {
return error.BackendRestores;
}
fn drop(ptr: *anyopaque, saved: pc.Saved) void {
const f: *lanes.fake.Fake = @ptrCast(@alignCast(ptr));
f.drop(saved);
}
};
for ([_]bool{ true, false }) |usable| {
var cfg = try lanes.Config.init(gpa, .{ .exact_width = 8, .gpu_tokens = true, .hidden_rows = true }, 8, 7);
defer cfg.deinit(gpa);
var target: lanes.fake.Fake = .{ .gpa = gpa };
defer target.deinit();
var clock: lanes.fake.FixedClock = .{};
var core = lanes.Engine.init(gpa, &cfg, target.backend(), clock.clock());
defer core.deinit();

// The 128-row cut and 192-row warm end, scaled to a grid of 4: the end at 6 is off it.

// A budget of one snapshot (88 B) fits either state, never both.
var store = pc.Store.init(gpa, .{ .ptr = &target, .vtable = &.{ .bytes = Snaps.bytes, .save = Snaps.save, .restore = Snaps.restore, .drop = Snaps.drop } }, .{ .planned = true, .grid = 4, .warm = .on, .warm_usable = usable }, 100);
defer store.deinit();
var host = LaneHost.init(gpa, std.testing.io, &core, .{ .lanes = 2, .warm_turns = true });
host.cache = &store;
try host.start();
defer host.stop();
const prompt = [_]u32{ 2, 7, 1, 8, 2, 8 };
var warm: Box = .{};
const warm_request: Request = .{ .prompt = &prompt, .max_tokens = 0, .background = true, .history_len = prompt.len, .born = 4 };
try host.engine().submit(1, &warm_request, .{ .ctx = &warm, .event = Box.event });
try std.testing.expectEqual(Reason.length, warm.wait());
host.engine().drain();

// The unusable end must not cost the usable mark: a follow-up resumes the grid mark, or nothing.
const longer = [_]u32{ 2, 7, 1, 8, 2, 8, 3, 1 };
const follow = try store.lookup(gpa, &longer, longer.len, &.{}, &.{}, &.{});
defer gpa.free(follow.marks);
try std.testing.expectEqual(if (usable) @as(u32, 4) else @as(u32, 0), if (follow.entry) |e| e.at else 0);
}
}

/// Status polls taken at each prompt chunk, the warm pass's apart from the request's.
const FillPolls = struct {
engine: Engine = undefined,
Expand Down Expand Up @@ -373,3 +426,13 @@ test "a kept warm end supersedes its history at either lookahead and regeneratio
try std.testing.expect(got.same);
}
}

test "a host advertises warm turns only beside a kept-state store" {
var host: LaneHost = undefined;
host.info_ = .{};
host.advertiseCache(null);
try std.testing.expect(!host.info_.prompt_cache and !host.info_.warm_turns);
var store: prompt_cache.Store = undefined;
host.advertiseCache(&store);
try std.testing.expect(host.info_.prompt_cache and host.info_.warm_turns);
}
2 changes: 1 addition & 1 deletion zig/src/core/lanes/backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub const Backend = struct {
clear_proposals: ?*const fn (*anyopaque, *Stream) void = null,
/// Undo an early speculation whose round was cut (the drafts come from a late request instead).
unspeculate: ?*const fn (ptr: *anyopaque, s: *Stream) anyerror!void = null,
/// Each held draft's chance of landing, for heads that give them; false when it has none.
/// Each held draft's chance, or false; also marks s.draft_chances_cut at the first unmeasured chance.
probabilities: ?*const fn (ptr: *anyopaque, s: *Stream, out: []f64) anyerror!bool = null,
/// A tree head's held drafts as host tokens and parents (gpa-owned), read where Python reads them.
tree: ?*const fn (ptr: *anyopaque, s: *Stream, gpa: std.mem.Allocator) anyerror!?Held = null,
Expand Down
1 change: 1 addition & 0 deletions zig/src/core/lanes/lanes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ test {
_ = @import("root_copy_test.zig");
_ = @import("drafted_grammar_test.zig");
_ = @import("windows_grammar_test.zig");
_ = @import("windows_chances_test.zig");
_ = @import("grammar_time_test.zig");
_ = @import("grammar_parallel_test.zig");
_ = @import("pricing_test.zig");
Expand Down
39 changes: 36 additions & 3 deletions zig/src/core/lanes/shared_drafts.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ pub fn budgets(e: *Engine, plans: []const win.Plan, heads: []const usize, out: [
if (most == 0) continue;
head.* = true;
const cached = s.draft_chances.items;
// a legacy cap prices by the rule's rates; granted depths keep the drafter's own chances under their bound
if (cached.len > 0 and (limits == null or e.cfg.granted_depth)) {
// a legacy cap prices by the rule's rates; a cut never starts the tail, so the measured prefix prices the rest
const known = @min(s.draft_chances_cut orelse cached.len, cached.len, most);
if (known > 0 and (limits == null or e.cfg.granted_depth)) {
const prices = try a.alloc(f64, most);
const known = @min(cached.len, most);
@memcpy(prices[0..known], cached[0..known]);
const rates = try e.rule.rates(win.who(s));
var chance = cached[known - 1];
Expand Down Expand Up @@ -187,3 +187,36 @@ test "shared draft prices: plain forced streams cost one row and paused streams
try budgets(&e, &plans, &.{0}, &out);
try std.testing.expectEqual(@as(i64, 7), out[0]);
}

test "shared draft prices: a cut chance tail prices by the rule's rates, not as a measured zero" {
const a = std.testing.allocator;
const Cfg = @import("config.zig");
const S = @import("stream.zig").Stream;
var cfg = try Cfg.Config.init(a, .{ .exact_width = 32, .drafts = 5, .batch_rows = 11 }, 32, 5);
defer cfg.deinit(a);
var fake: @import("fake.zig").Fake = .{ .gpa = a };
defer fake.deinit();
var clock: @import("fake.zig").FixedClock = .{};
var e = Engine.init(a, &cfg, fake.backend(), clock.clock());
defer e.deinit();
var cut = try S.init(a, .{ .id = "cut", .prompt = &.{ 7, 13, 29 }, .max_new = 64 });
defer cut.deinit(a);
var zero = try S.init(a, .{ .id = "zero", .prompt = &.{ 7, 13, 29 }, .max_new = 64 });
defer zero.deinit(a);
var grown = try S.init(a, .{ .id = "grown", .prompt = &.{ 7, 13, 29 }, .max_new = 64 });
defer grown.deinit(a);
try cut.draft_chances.appendSlice(a, &.{ 0.9, 0.4, 0, 0, 0 });
cut.draft_chances_cut = 2; // the chain cut left the last three chances unknown
try zero.draft_chances.appendSlice(a, &.{ 0.9, 0.4, 0, 0, 0 }); // main's rule: that tail priced as measured zeros
const rates = try e.rule.rates(win.who(&grown));
var continuation = [_]f64{ 0.9, 0.4, 0, 0, 0 };
for (continuation[2..], 2..) |*c, j| c.* = continuation[j - 1] * rates[@min(j, rates.len - 1)];
try grown.draft_chances.appendSlice(a, &continuation);
const cutp: win.Plan = .{ .stream = &cut, .position = 0, .kind = .none };
const zerop: win.Plan = .{ .stream = &zero, .position = 0, .kind = .none };
const grownp: win.Plan = .{ .stream = &grown, .position = 0, .kind = .none };
var out: [3]i64 = undefined;
try budgets(&e, &.{ cutp, grownp, zerop }, &.{ 0, 1, 2 }, &out);
try std.testing.expect(out[0] > out[2]); // main's rule prices the cut tail as measured zeros
try std.testing.expectEqual(out[1], out[0]); // the cut stream prices as the rate continuation
}
Loading
Loading