diff --git a/docs/run-guide.md b/docs/run-guide.md index fb4f269739a..a5b0f9ebed8 100644 --- a/docs/run-guide.md +++ b/docs/run-guide.md @@ -179,7 +179,7 @@ The GLP flags are left out where the build cannot serve them (everything but Met | `--learn-dir DIR` | Where `--learn` keeps them, `~/.cache/tensorfold/learned` by default; implies `--learn`. Servers of one model and build may share it: one writes at a time, and a server that finds another writing skips that write. A learned index from another format version stops startup with its directory named; its files are kept. | | `--learn-gib GIB` | Disk for learned prefixes on each Mac, 32 by default; the least recently used go first. Implies `--learn`. | | `--learn-min-free-gib GIB` | Free disk `--learn` leaves on each Mac, 4 by default; below it learning pauses and serving goes on. | -| `--learn-conversations` | Keep every prompt state on disk too, each conversation's and the shared prefixes, in one store in place of `--learn`'s, so a next turn after a restart, a crash or an eviction reads it back. Implies `--learn`; see [Conversations on disk](api.md#conversations-on-disk). | +| `--learn-conversations` | Keep every prompt state on disk too, each conversation's and the shared prefixes, in one store in place of `--learn`'s, so a next turn after a restart, a crash or an eviction reads it back. Implies `--learn`; on by default where the engine keeps an exact image on disk; see [Conversations on disk](api.md#conversations-on-disk). | | `--handover` | Open the conversation handover routes. Off unless given; refused at startup without `--prompt-cache-disk DIR` (or `--learn-conversations`). See [Conversation handover](handover.md). | | `--learn-daily-gib GIB` | What `--learn-conversations` may write to disk a day, every model together, 32 by default; past it states stay in RAM only. | | `--recall` | Draft from a record of the token ids earlier requests read and wrote, so repeated text is verified in a few rounds; replies are unchanged. Off by default; see [Recall](recall.md) for what stays on disk. | diff --git a/zig/src/server/engine_refusal_test.zig b/zig/src/server/engine_refusal_test.zig index 68fa77c79a7..fb26740c8eb 100644 --- a/zig/src/server/engine_refusal_test.zig +++ b/zig/src/server/engine_refusal_test.zig @@ -12,6 +12,10 @@ const pre = @import("stream_preflight_test.zig"); const TestText = pre.TestText; const NoHistory = pre.NoHistory; +test { + _ = @import("spill_default_test.zig"); +} + /// What the fake engine says when asked; `off` builds a vtable with no `refusal` entry at all. const Mode = enum { off, always, tools_only, long_message, literal, empty }; diff --git a/zig/src/server/engines.zig b/zig/src/server/engines.zig index 728306b76f5..7c599914464 100644 --- a/zig/src/server/engines.zig +++ b/zig/src/server/engines.zig @@ -89,9 +89,23 @@ pub fn follow(a: Allocator, gpa: Allocator, io: std.Io, dir: []const u8, model_t fn released(a: Allocator, dir: []const u8, model_type: []const u8, args: cli.Args) !api.Open { var o = try cli.request(a, dir, model_type, args); o.release = @import("build_options").version; + // #3619: sessions spill to SSD by default where the family keeps an exact image; named flags keep their refusals. + if (comptime @hasDecl(native, "registry") and @hasDecl(native, "refusals")) { + const env = @import("sys").environment; + if (!args.confidential and !spill.requested(args.learn, args.learn_conversations, args.spill_gib) and (o.prompt_cache_gib orelse 1) > 0 and + env.rawText("TF_KV_SHARE") == null and env.rawText("TF_GLM_REQUEST_STARTS") == null) + { + switch (spill.shape(native.refusals, native.registry, o)) { + .none => {}, + .conversations, .spilled => |s| if (api.prompt_cache.rows.spillRoot(a) catch null) |root| spill.flags(&o, root, s), + } + } + } return o; } +const spill = @import("spill_default.zig"); + /// `tensorfold device`'s side of the join: what `capabilities --json` lists, without its chip probe or a driver. pub fn offer(a: Allocator) !device.Offer { const families = try a.alloc(device.Family, native.families.len); diff --git a/zig/src/server/spill_default.zig b/zig/src/server/spill_default.zig new file mode 100644 index 00000000000..ef1e8c4ff80 --- /dev/null +++ b/zig/src/server/spill_default.zig @@ -0,0 +1,45 @@ +//! #3619: sessions spill to SSD and resume without re-prefill, on by default where the family keeps an exact image. +const std = @import("std"); +const api = @import("engine_api"); + +/// What the default asks a family to keep on disk: whole conversations, and where it takes them, evicted states too. +pub const Shape = enum { none, conversations, spilled }; + +/// Whether the command line named a disk-state flag: a serve then keeps the flags' explicit semantics. +pub fn requested(learn: bool, learn_conversations: bool, spill_gib: ?f64) bool { + return learn or learn_conversations or spill_gib != null; +} + +/// The family's answer through `refusals.family`: flags it takes; .spilled first, then conversations alone (GLM). +pub fn shape(comptime refusals: type, comptime registry: anytype, o: api.Open) Shape { + inline for (registry) |F| if (matches(F, o.model_type)) { + var learned = o; + learned.learn = "spill"; + learned.learn_conversations = true; + learned.spill = .evicted; + if (same(refusals.family(F, o), refusals.family(F, learned))) return .spilled; + learned.spill = .off; + return if (same(refusals.family(F, o), refusals.family(F, learned))) .conversations else .none; + }; + return .none; +} + +/// The flags the default writes for a Shape once `root` resolves: what --spill-gib/--learn give today. +pub fn flags(o: *api.Open, root: []const u8, s: Shape) void { + o.learn = root; + o.learn_conversations = true; + if (s == .spilled) o.spill = .evicted; +} + +fn matches(comptime F: type, model_type: []const u8) bool { + if (std.mem.eql(u8, model_type, F.model_type)) return true; + if (@hasDecl(F, "cuda_also_model_types")) inline for (F.cuda_also_model_types) |t| { + if (std.mem.eql(u8, model_type, t)) return true; + }; + return false; +} + +fn same(a: ?[]const u8, b: ?[]const u8) bool { + if (a == null or b == null) return a == null and b == null; + return std.mem.eql(u8, a.?, b.?); +} diff --git a/zig/src/server/spill_default_test.zig b/zig/src/server/spill_default_test.zig new file mode 100644 index 00000000000..3ef8877ab7d --- /dev/null +++ b/zig/src/server/spill_default_test.zig @@ -0,0 +1,78 @@ +//! #3619's default: a family takes it only where its own refusals accept the flags. +const std = @import("std"); +const api = @import("engine_api"); +const spill = @import("spill_default.zig"); + +const Family = struct { + pub const model_type = "whole"; +}; + +const OnlyConversations = struct { + pub const model_type = "conv_only"; + pub const cuda_also_model_types = &.{"conv_alias"}; +}; + +const NoDisk = struct { + pub const model_type = "ram_only"; +}; + +const OneRank = struct { + pub const model_type = "one_rank"; +}; + +const refusals = struct { + pub fn family(comptime F: type, o: api.Open) ?[]const u8 { + if (F == NoDisk and (o.learn != null or o.learn_conversations or o.spill != .off)) return "this family keeps no states on disk"; + if (F == OnlyConversations and o.spill != .off) return "conversations only: serve without --spill-gib"; + if (F == OneRank and o.learn_conversations and o.tp > 1) return "one rank's states on disk only"; + if (o.slide) return "--slide is refused here"; + return null; + } +}; + +const registry = [_]type{ Family, OnlyConversations, NoDisk, OneRank }; + +test "spill default: a family keeping whole and evicted states takes the spilled shape" { + const o: api.Open = .{ .dir = "d", .model_type = "whole" }; + try std.testing.expectEqual(spill.Shape.spilled, spill.shape(refusals, registry, o)); +} + +test "spill default: a family refusing evicted spill still takes conversations, by alias too" { + const o: api.Open = .{ .dir = "d", .model_type = "conv_alias" }; + try std.testing.expectEqual(spill.Shape.conversations, spill.shape(refusals, registry, o)); +} + +test "spill default: a family refusing the flags takes none, so its serve is untouched" { + const o: api.Open = .{ .dir = "d", .model_type = "ram_only" }; + try std.testing.expectEqual(spill.Shape.none, spill.shape(refusals, registry, o)); + try std.testing.expectEqual(spill.Shape.none, spill.shape(refusals, registry, .{ .dir = "d", .model_type = "unlisted" })); +} + +test "spill default: a TP serve of a one-rank family takes none, as its flags refuse" { + const o: api.Open = .{ .dir = "d", .model_type = "one_rank", .tp = 4, .rank = 1, .given = .{ .rank = true } }; + try std.testing.expectEqual(spill.Shape.none, spill.shape(refusals, registry, o)); + try std.testing.expectEqual(spill.Shape.none, spill.shape(refusals, registry, .{ .dir = "d", .model_type = "one_rank", .tp = 4 })); + try std.testing.expectEqual(spill.Shape.spilled, spill.shape(refusals, registry, .{ .dir = "d", .model_type = "one_rank" })); +} + +test "spill default: the flags' other refusals still hold when the default engages" { + const o: api.Open = .{ .dir = "d", .model_type = "whole", .slide = true }; + try std.testing.expectEqual(spill.Shape.spilled, spill.shape(refusals, registry, o)); +} + +test "spill default: naming a disk flag keeps explicit semantics" { + try std.testing.expect(spill.requested(true, false, null)); + try std.testing.expect(spill.requested(false, true, null)); + try std.testing.expect(spill.requested(false, false, 0)); + try std.testing.expect(!spill.requested(false, false, null)); +} + +test "spill default: the flags written match --learn-conversations and --spill-gib" { + var o: api.Open = .{ .dir = "d", .model_type = "m" }; + spill.flags(&o, "/root", .conversations); + try std.testing.expectEqualStrings("/root", o.learn.?); + try std.testing.expect(o.learn_conversations); + try std.testing.expectEqual(api.prompt_cache.rows.Spill.off, o.spill); + spill.flags(&o, "/root", .spilled); + try std.testing.expectEqual(api.prompt_cache.rows.Spill.evicted, o.spill); +}