Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Living Weights learns into any family described as data: the family's…
… layout, neuron kind and shard format

A family now describes itself once, as data (living/families.zig): where its shared experts sit and their tensor
names, the neuron's activation (relu^2, or SwiGLU with an optional per-row gate), the quantization, the config keys and
the object they live in, and its MTP head's expert. Its host gives the learner a layout and the pocket hooks
(living/family.zig). Nemotron is the one family listed, and its lessons, routed blocks, edits and probes are unchanged.

- The pocket reads its layers and width from the family's layout, not from Nemotron's config, and the learner offers
  routed blocks, edits and probes only to a trainer with `sites` (Nemotron's).
- living/shards.zig widens any family's experts at its group and width; Nemotron's writer is now a wrapper over it.
  shard_edit sets and reads nested config keys (text_config).
- A SwiGLU neuron fitted as u is written as gate row 16u and up row u/16: relu(u x)^2 on its fact's rows, at most 8e-5
  of it at a 0.5 margin; a gated family's outputs fit gate * act(x). No listed family uses it yet; the tests use a
  stand-in family shaped as Qwen3.8 Flash Next's shared expert.
- The server's view reads layers, pockets and shards through the family its config's model_type names, and
  native/nemotron_slide.zig is native/slide_adapter.zig.
- pocket.zig's quiet refit and its loading move to pocket_quiet.zig and pocket_load.zig, for the 600-line rule.
- Tests: the generic writer equals affine4_host at 4 bits in groups of 64, Nemotron's save test over the wrapper, a
  SwiGLU neuron fitted and stored at 4 bits in groups of 32, and a whole pocket lesson over a stand-in host for each
  activation (test-living, also under test-cuda-host).
  • Loading branch information
ZackSample committed Oct 15, 2026
commit 75978a1c5c05d9c2aff212a0e2a7d2fd3938145a
14 changes: 7 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ pub fn build(b: *std.Build) void {
}

/// `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 {
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 = 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 });
Expand All @@ -201,7 +201,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 = 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) } }) } },
.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) } }) } },
}) });
exe.root_module.addOptions("build_options", build_options);
if (test_step == null) {
Expand All @@ -219,7 +219,7 @@ fn nativeServer(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.b
.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 } },
.imports = &.{ .{ .name = "engine_api", .module = api }, .{ .name = "tokenizer", .module = tokenizer }, .{ .name = "template", .module = template }, .{ .name = "living", .module = living } },
});
test_step.?.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = server_tests })).step);
@import("zig/build/cortheon_fuzz.zig").add(b, target, api, tokenizer, template);
Expand Down Expand Up @@ -466,7 +466,7 @@ fn metalTargets(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.b
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);
_ = 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"),
Expand Down Expand Up @@ -539,7 +539,7 @@ fn metalTargets(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.b
}

/// 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, living: *std.Build.Module } {
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, qwen27: qwen27_build.Shared } {
const metal = b.createModule(.{
.root_source_file = b.path("zig/src/metal/metal.zig"),
.target = target,
Expand All @@ -553,7 +553,7 @@ fn metalEngineModules(b: *std.Build, target: std.Build.ResolvedTarget, optimize:
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") });
// Living Weights' learner, pocket and fits, which Nemotron's lessons import
// 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"),
Expand All @@ -576,7 +576,7 @@ pub fn distMetalServer(b: *std.Build, target: std.Build.ResolvedTarget, draft_id
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 } } });
return nativeServer(b, target, .fast, m.metal, m.engine, api, engines, build_options, null);
return nativeServer(b, target, .fast, m.metal, m.engine, m.living, api, engines, build_options, null);
}

test {
Expand Down
21 changes: 13 additions & 8 deletions zig/build/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn family(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin
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' learner, pocket and fits, which a family's lessons import
// 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 });
const nemotron = b.createModule(.{ .root_source_file = b.path("zig/src/families/nemotron/cuda.zig"), .target = target, .optimize = optimize, .link_libc = true });
nemotron.addImport("cuda", cuda);
Expand Down Expand Up @@ -142,7 +142,7 @@ pub fn targets(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.bu
runner.addImport("exlr8_frozen_dense", b.createModule(.{ .root_source_file = b.path("zig/tests/fixtures/exlr8/frozen_dense.zig"), .target = target, .optimize = optimize }));
for (exlr8_build.denseModules(b, target, optimize, exlr8, exlr8_tests[0])) |m| runner.addImport(m.name, m.module);
b.installArtifact(b.addExecutable(.{ .name = "tf-cuda-test", .root_module = runner }));
nativeServer(b, target, optimize, cuda, mods.core, mods.lanes, mods.nemotron, mods.tokenizer, build_options, true).root_module.strip = strip;
nativeServer(b, target, optimize, cuda, mods.core, mods.lanes, mods.nemotron, mods.tokenizer, mods.living, build_options, true).root_module.strip = strip;
}

/// The CUDA engines a native server opens (native/cuda.zig), over the given runtime and families.
Expand All @@ -164,7 +164,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, tokenizer: *std.Build.Module, build_options: *std.Build.Step.Options, install_native: 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, tokenizer: *std.Build.Module, living: *std.Build.Module, build_options: *std.Build.Step.Options, install_native: bool) *std.Build.Step.Compile {
const m = engines(b, target, optimize, cuda, lanes, nemotron);
// 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 });
Expand All @@ -173,7 +173,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);
if (!install_native) {
Expand Down Expand Up @@ -202,7 +202,7 @@ 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_test = b.addRunArtifact(b.addTest(.{ .root_module = server_tests }));
step.dependOn(&server_test.step);
Expand All @@ -214,10 +214,15 @@ pub fn hostTests(b: *std.Build, draft_ids: *std.Build.Module, build_options: *st
}
// the tokenizer's own tests (memo, steps, regex, unicode) run here: no other test binary reaches them
for ([_]*std.Build.Module{ cuda, mods.core, mods.lanes, mods.nemotron, mods.heat, mods.tokenizer, native, stagger(b, host, .debug) }) |m| step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m })).step);
// Living Weights' learner on the host, beside the family that imports it
// 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);
b.step("test-living", "Living Weights' learner, pocket and fits, on the host").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);
Expand Down Expand Up @@ -253,7 +258,7 @@ 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.tokenizer, build_options, false);
return nativeServer(b, target, .fast, cuda, mods.core, mods.lanes, mods.nemotron, mods.tokenizer, mods.living, build_options, false);
}

/// SASS targets every release fatbin holds: Ampere (sm_86), RTX 50 and RTX PRO Blackwell (sm_120), GB10 (sm_121).
Expand Down
47 changes: 45 additions & 2 deletions zig/src/core/shard_edit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,44 @@ pub fn grow(gpa: Allocator, io: Io, dir: []const u8, edits: []const Replacement)

/// config.json in `dir` with each of `ints` set (added where missing), every other key as it was.
pub fn setConfig(gpa: Allocator, io: Io, dir: []const u8, ints: []const struct { []const u8, i64 }) !void {
return setConfigIn(gpa, io, dir, null, ints);
}

/// config.json in `dir` with each of `ints` set inside the object `scope` names (null: the top level), as setConfig.
pub fn setConfigIn(gpa: Allocator, io: Io, dir: []const u8, scope: ?[]const u8, ints: []const struct { []const u8, i64 }) !void {
var arena_state = std.heap.ArenaAllocator.init(gpa);
defer arena_state.deinit();
const arena = arena_state.allocator();
const path = try std.fs.path.join(arena, &.{ dir, "config.json" });
var config = try readJson(arena, io, path);
if (config != .object) return error.BadConfig;
for (ints) |kv| try config.object.put(arena, kv[0], .{ .integer = kv[1] });
const o = if (scope) |name| blk: {
const inner = config.object.getPtr(name) orelse return error.BadConfig;
if (inner.* != .object) return error.BadConfig;
break :blk &inner.object;
} else &config.object;
for (ints) |kv| try o.put(arena, kv[0], .{ .integer = kv[1] });
try writeJson(arena, path, config);
}

/// An integer of `dir`'s config.json, or null when it has none.
pub fn configInt(gpa: Allocator, io: Io, dir: []const u8, key: []const u8) !?i64 {
return configIntIn(gpa, io, dir, null, key);
}

/// An integer of the object `scope` names in `dir`'s config.json (null: the top level), or null when it has none.
pub fn configIntIn(gpa: Allocator, io: Io, dir: []const u8, scope: ?[]const u8, key: []const u8) !?i64 {
var arena_state = std.heap.ArenaAllocator.init(gpa);
defer arena_state.deinit();
const arena = arena_state.allocator();
const config = try readJson(arena, io, try std.fs.path.join(arena, &.{ dir, "config.json" }));
if (config != .object) return error.BadConfig;
const v = config.object.get(key) orelse return null;
const o = if (scope) |name| blk: {
const inner = config.object.get(name) orelse return error.BadConfig;
if (inner != .object) return error.BadConfig;
break :blk inner.object;
} else config.object;
const v = o.get(key) orelse return null;
return if (v == .integer) v.integer else error.BadConfig;
}

Expand Down Expand Up @@ -508,6 +528,29 @@ test "a tensor grown in its shard: the shard is rewritten, the index's total mov
try std.testing.expectEqualStrings("x", (try field(config, "model_type")).string);
}

test "integers set and read inside a nested config object, the top level and every other key as they were" {
const gpa = std.testing.allocator;
const io = std.testing.io;
var tmp = std.testing.tmpDir(.{});
defer tmp.cleanup();
try tmp.dir.writeFile(io, .{ .sub_path = "config.json", .data = "{\"model_type\":\"x\",\"width\":7,\"text_config\":{\"width\":2,\"name\":\"t\"}}" });
const dir = try std.fs.path.join(gpa, &.{ ".zig-cache", "tmp", &tmp.sub_path });
defer gpa.free(dir);
try std.testing.expectEqual(@as(?i64, 2), try configIntIn(gpa, io, dir, "text_config", "width"));
try std.testing.expectEqual(@as(?i64, null), try configIntIn(gpa, io, dir, "text_config", "learned"));
try setConfigIn(gpa, io, dir, "text_config", &.{ .{ "width", 3 }, .{ "learned", 1 } });
try std.testing.expectEqual(@as(?i64, 3), try configIntIn(gpa, io, dir, "text_config", "width"));
try std.testing.expectEqual(@as(?i64, 1), try configIntIn(gpa, io, dir, "text_config", "learned"));
try std.testing.expectEqual(@as(?i64, 7), try configInt(gpa, io, dir, "width"));
try std.testing.expectEqual(@as(?i64, null), try configInt(gpa, io, dir, "learned"));
try std.testing.expectError(error.BadConfig, configIntIn(gpa, io, dir, "vision_config", "width"));
var arena_state = std.heap.ArenaAllocator.init(gpa);
defer arena_state.deinit();
const arena = arena_state.allocator();
const config = try readJson(arena, io, try std.fs.path.join(arena, &.{ dir, "config.json" }));
try std.testing.expectEqualStrings("t", (try field(try field(config, "text_config"), "name")).string);
}

test "a folder whose shard is a symlink or a hard link is refused for learning; its own files pass" {
const gpa = std.testing.allocator;
const io = std.testing.io;
Expand Down
1 change: 1 addition & 0 deletions zig/src/families/nemotron/cuda.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ test {
_ = @import("cuda_prompt_cuts.zig");
_ = @import("cuda_profile_window.zig");
_ = reuse;
_ = slide;
_ = @import("cuda_train_back.zig");
}
7 changes: 7 additions & 0 deletions zig/src/families/nemotron/cuda_slide.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const state = @import("cuda_state.zig");

const Living = living.Io(@import("core"));
const PocketShards = @import("pocket_shards.zig").Io(@import("core"));
const pocket_layout = @import("pocket_layout.zig");

pub const Example = lessons.Example;
pub const Lesson = lessons.Lesson;
Expand All @@ -27,11 +28,17 @@ const Host = struct {
pub const Trainer = train.Trainer;
pub const max_rows = train.max_rows;
pub const Ctx = Model;
pub const family = pocket_layout.family;

pub fn layers(m: Ctx) usize {
return m.e.c.layers;
}

/// Where the pocket sits and learns: every MoE layer, all but the last learning.
pub fn layout(m: Ctx) @import("living").Layout {
return pocket_layout.layout(m.e.c);
}

pub fn trainer(gpa: std.mem.Allocator, io: std.Io, m: Ctx) !*Trainer {
return train.Trainer.init(gpa, io, m.e, m.dir, m.living);
}
Expand Down
12 changes: 12 additions & 0 deletions zig/src/families/nemotron/pocket_layout.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//! Nemotron as Living Weights reads it: its family (living/families.zig) and the layout its config gives.
const living = @import("living");
const Config = @import("config.zig").Config;

pub const family = living.families.nemotron_h;

/// Every MoE layer's shared expert carries the pocket; all but the last layer's learn.
pub fn layout(c: Config) living.Layout {
var moe: living.Layers = .empty;
for (0..c.layers) |l| if (c.kinds[l] == .moe) moe.set(l);
return .fromExperts(c.hidden, c.shared_width, c.layers, moe);
}
Loading
Loading