Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Each release's page on GitHub has its notes and measurements. See [the 1.0.0 rel

## Unreleased

- Flash Next on HIP: the verify window holds eight rows instead of four, so a stream alone in a round can land copy drafts of up to seven tokens a round; rounds shared by several streams keep three-token copies, MTP still drafts at most three, and each stream's window snapshots take more device memory, which admission counts (#2730). Thanks to @gilby.

- OCR: the native HIP engine reads documents through `POST /v1/ocr` in Mistral's OCR contract (its Python and TypeScript SDKs), with dots.mocr's reader (a box for each layout block). Images and PDFs (`--vision-pdf`) share one page budget per request (`TENSORFOLD_MAX_PDF_PAGES`, default 16), each PDF measured before any inference and each page rendered just before it is read, and Mistral crops share one image budget (256 images, 4 times the page pixels, 16 MiB plus 2 MiB a page). An option that changes content and that TensorFold cannot apply, such as annotations, is refused. A page cut at the context limit or out of its model's format fails the whole request with HTTP 422; `docs/api.md` lists the contract's fields, statuses and limits (#2570, #2571, #2440). Thanks to @laudney.

- CUDA: with `TF_FLASHNEXT_KV_PAGES` on Linux, the Flash Next page pool no longer takes its whole arena at load: its 256 MiB pieces are mapped as streams first reach them, and a helper thread maps the next ones between requests, so at 262,144 x 3 on a GB10 about 14 GiB more stays free after load (MemAvailable 40.8-41.6 GiB against 26.5-27.0). Replies are unchanged; without `TF_FLASHNEXT_KV_PAGES`, and on other systems, nothing changes (#3066). Thanks to @Arminova.
Expand Down
17 changes: 15 additions & 2 deletions zig/src/families/flashnext/hip/cache.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const model = @import("model.zig");

const Dims = model.Dims;

/// Most rows a verified window holds: the pending token and up to three drafts.
pub const max_window = 4;
/// Most rows a verified window holds: the pending token and up to seven drafts (copies use the width; MTP drafts 3).
pub const max_window = 8;

/// Where the bytes of one sparse layer's caches start, and their strides.
pub const SparseView = struct { k: u64, v: u64, raw: u64, pooled: u64 };
Expand Down Expand Up @@ -257,3 +257,16 @@ test "a stream's bytes at tp 4: 12 sparse layers' caches, 36 DeltaNet states and
const linear = 3 * 2560 * 4 + 12 * 128 * 128 * 4;
try std.testing.expectEqual(@as(usize, 12 * per_layer + 36 * linear + 9 * 10240 * 2), l.bytes());
}

test "a window's snapshots hold eight rows at tp 4: the pending token and seven copied or drafted ones" {
const config = @import("../config.zig");
var c = try config.parse(std.testing.allocator, @embedFile("../fixtures/config.json"));
defer c.deinit();
const d = try Dims.of(&c, 4);
var kinds: [48]bool = undefined;
for (&kinds, 0..) |*k, i| k.* = (try c.kind(i)) == .linear_attention;
const l = Layout.of(d, &kinds, 4096, .f16);
const linear = 3 * 2560 * 4 + 12 * 128 * 128 * 4;
try std.testing.expectEqual(@as(usize, 8 * 36 * linear), l.windowBytes());
try std.testing.expectEqual(l.tail_bytes + 8 * (l.tail_bytes / l.tail_rows), l.cinBytes());
}
48 changes: 48 additions & 0 deletions zig/src/families/flashnext/hip/facts.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! The lane core's facts of a loaded Flash Next model, pure so a host test drives them without a device.
const std = @import("std");
const lanes = @import("lanes");
const cache = @import("cache.zig");

/// MTP drafts the HIP head takes at most: `--mtp-drafts` is refused past it (native/hip.zig's check).
pub const mtp_max_drafts: u32 = 3;

/// The policy's `--mtp-drafts` capped at the head's.
pub fn capDrafts(asked: u8) u32 {
return @min(mtp_max_drafts, @as(u32, asked));
}

/// A shared round's copy window and a lone stream's first: the pending row and three copies; lone runs grow past it.
const shared_copy_rows = 4;

/// The lane core's facts of a loaded model: `drafting` names a head that drafts, `drafts` its capped chains.
pub fn factsFor(exact_width: u32, streams: usize, drafting: bool, drafts: u32) lanes.Model {
return .{
.exact_width = exact_width,
.first_copy_rows = shared_copy_rows,
.gpu_tokens = false,
.mtp = drafting,
.speculate = drafting,
.speculate_early = false,
.drafts = drafts,
.hidden_rows = true,
.batch_rows = @intCast(exact_width * streams),
.max_streams = @intCast(streams),
.draft_streams = drafting,
};
}

test "the shared round's copy window: the lane core takes three copies, a lone stream the whole window" {
const a = std.testing.allocator;
var cfg = try lanes.Config.init(a, factsFor(cache.max_window, 2, true, 3), cache.max_window, cache.max_window - 1);
defer cfg.deinit(a);
try std.testing.expectEqual(@as(u32, 3), cfg.first_copy);
try std.testing.expectEqual(@as(u32, 7), cfg.max_copy);
try std.testing.expectEqual(@as(u32, 4), cfg.base_width);
}

test "the extracted facts retain the loaded head drafting mode" {
for ([_]bool{ false, true }) |drafting| {
const facts = factsFor(cache.max_window, 2, drafting, 3);
try std.testing.expectEqual(drafting, facts.draft_streams);
}
}
29 changes: 14 additions & 15 deletions zig/src/families/flashnext/hip/lanes.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const lanes = @import("lanes");
const serve = @import("hip_serve");
const Engine = @import("engine.zig").Engine;
const cache = @import("cache.zig");
const facts_mod = @import("facts.zig");
const forward = @import("forward.zig");
const head_mod = @import("head.zig");
const draft_batch = @import("draft_batch.zig");
Expand All @@ -20,7 +21,6 @@ const draw = serve.draw;
pub const ring = 1024;
/// Drafts the head holds for a stream at most.
pub const max_depth = cache.max_window - 1;

pub const Fill = struct {
at: usize,
stops: [16]u32 = undefined,
Expand Down Expand Up @@ -82,7 +82,7 @@ pub const Fx = struct {
.streams = streams,
.ones = try hip.DeviceBuffer.fromHost(&e.driver, std.mem.sliceAsBytes(host)),
.draft_ids = &.{},
.drafts = @min(max_depth, e.o.policy.mtp.drafts),
.drafts = facts_mod.capDrafts(e.o.policy.mtp.drafts),
};
errdefer f.ones.free();
if (e.weights.model.mtp != null) f.draft_ids = try load.draftIds(gpa);
Expand Down Expand Up @@ -206,19 +206,7 @@ pub const Fx = struct {
}

pub fn facts(f: *const Fx) lanes.Model {
const drafting = f.e.weights.model.mtp != null and f.drafts > 0;
return .{
.exact_width = cache.max_window,
.gpu_tokens = false,
.mtp = drafting,
.speculate = drafting,
.speculate_early = false,
.drafts = f.drafts,
.hidden_rows = true,
.batch_rows = @intCast(cache.max_window * f.streams),
.max_streams = @intCast(f.streams),
.draft_streams = drafting,
};
return facts_mod.factsFor(cache.max_window, f.streams, f.e.weights.model.mtp != null and f.drafts > 0, f.drafts);
}

fn free(f: *Fx, l: *Lane) void {
Expand Down Expand Up @@ -522,3 +510,14 @@ pub const Fx = struct {
f.free(kv.value);
}
};

test "MTP stays at three: the constant, the cap and the facts never report more" {
try std.testing.expectEqual(@as(u32, 3), facts_mod.mtp_max_drafts);
try std.testing.expectEqual(@as(u32, 3), facts_mod.capDrafts(3));
try std.testing.expectEqual(@as(u32, 3), facts_mod.capDrafts(7));
const m = facts_mod.factsFor(cache.max_window, 2, true, facts_mod.capDrafts(7));
try std.testing.expectEqual(@as(u32, 3), m.drafts);
var cfg = try lanes.Config.init(std.testing.allocator, m, cache.max_window, cache.max_window - 1);
defer cfg.deinit(std.testing.allocator);
try std.testing.expectEqual(@as(u32, 3), cfg.most_drafts);
}
8 changes: 7 additions & 1 deletion zig/src/native/hip.zig
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ test "the group flags: one rank source, a supported world, a master past one ran
try std.testing.expectError(error.Refused, checkGroup(a, outside, &problem));
}

test "--drafter is refused before any load, on every rank, until the HIP engine runs DFlash2" {
test "--drafter, and --mtp-drafts past three, are refused before any load, on every rank" {
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
defer arena.deinit();
const a = arena.allocator();
Expand All @@ -535,6 +535,12 @@ test "--drafter is refused before any load, on every rank, until the HIP engine
follower.rank = 1;
follower.master = "node0";
try std.testing.expectError(error.Refused, checkGroup(a, follower, &problem));
// three drafts and fewer are not refused
var o: api.Open = .{ .dir = "d", .model_type = "qwen4_exp", .mtp_drafts = 3 };
try checkGroup(a, o, &problem);
o.mtp_drafts = 4;
try std.testing.expectError(error.Refused, checkGroup(a, o, &problem));
try std.testing.expectEqualStrings("--mtp-drafts 4: the HIP engine drafts at most 3", problem);
}

test "--kv-cache fp8 is refused by name on HIP; bf16 caches are not" {
Expand Down
Loading