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
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

- Core: TFQ3 records and wire constants live in one shared copy that every backend uses; layouts, constants and output are unchanged (#5281). Thanks to @jschmied.

- Nemotron on pre-M5 Metal: with `TF_NEMOTRON_METAL_KV_PAGES=1`, drafted verification computes attention one row at a time, as plain decoding does, so drafted output stays byte-identical to plain; the attention contracts now compare every output byte, NaN payloads included (refs #5223).

- Inkling 2 Mini on Metal: `--drafter` takes a DFlash 2 drafter, and drafted output stays byte-identical to plain decoding (#4917). Thanks to @cwschroeder.
Expand Down
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ pub fn build(b: *std.Build) void {
@import("zig/build/save_guard.zig").wire(b);
@import("zig/build/cpu_contract.zig").steps(b, test_step);
@import("zig/build/bf16_round.zig").wire(b, test_step);
@import("zig/build/tfq3_format.zig").wire(b, test_step);
@import("zig/build/chunked_write.zig").wire(b, test_step);
@import("zig/build/qwen35_layers.zig").wire(b, test_step);
@import("zig/build/dev_tools.zig").wire(b, target);
Expand Down
1 change: 1 addition & 0 deletions zig/build/product.zig
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub fn build(b: *std.Build, name: []const u8) void {
@import("checkpoint_config.zig").wire(b);
@import("save_guard.zig").wire(b);
@import("bf16_round.zig").wire(b, null);
@import("tfq3_format.zig").wire(b, null);
@import("qwen35_layers.zig").wire(b, null);
}

Expand Down
47 changes: 47 additions & 0 deletions zig/build/tfq3_format.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//! One std-only `tfq3_format` module (the TFQ3 records) for every build consumer; its file has a single module owner.

const std = @import("std");

/// `test-tfq3-format` under `parent`, then the module for every module the declared steps compile.
pub fn wire(b: *std.Build, parent: ?*std.Build.Step) void {
if (parent) |p| {
const tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("zig/src/core/tfq3_format.zig"), .target = b.graph.host }) });
const step = b.step("test-tfq3-format", "TFQ3 records: field offsets, sizes and bytes against the copies they replaced");
step.dependOn(&b.addRunArtifact(tests).step);
p.dependOn(step);
}
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 module itself and generated modules do not
if (m.root_source_file) |root| if (root == .src_path and !m.import_table.contains("tfq3_format") and !std.mem.endsWith(u8, root.src_path.sub_path, "core/tfq3_format.zig"))
m.addImport("tfq3_format", w.format(target));
for (m.import_table.values()) |dep| w.module(dep, target);
}

fn format(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/tfq3_format.zig"), .target = target });
return slot.value_ptr.*;
}
};
73 changes: 73 additions & 0 deletions zig/src/core/tfq3_format.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! TFQ3's on-disk records and wire constants, one copy for every backend; each backend keeps its checks and loaders.
const std = @import("std");

pub const magic_layer: u32 = 0x57504654; // "TFPW": layer_NN.tffpw and mtp_00.tffpw, routed experts
pub const magic_nonmoe: u32 = 0x4E504654; // "TFPN": nonmoe.tffpw and dense shards
pub const magic_tables: u32 = 0x33565750; // "PWV3": pw_v3_tables.bin
pub const format_version: u32 = 3;
/// The boundary a GPU section (scales, weights) starts on in a published file.
pub const section_align: usize = 16384;
/// The kernels' whole table: magic, version, six affine floats, padding, three LUTs and three c64 tables.
pub const tables_bytes: usize = 10816;
/// A published pw_v3_tables.bin: the table padded to one section.
pub const tables_file_bytes: usize = 16384;

/// A TFPW file's header: one layer's routed experts, their sections and the converter's tag.
pub const LayerHeader = extern struct {
magic: u32,
version: u32,
layer_idx: u32,
num_experts: u32,
num_tensors: u32,
hidden_size: u32,
moe_inter: u32,
reserved0: u32,
raw_fp8_bytes: u64,
stream_words: u64,
scale_halfs: u64,
off_meta: u64,
off_scales: u64,
off_weights: u64,
reserved1: [4]u64,
meta_tag: [32]u8,
};

/// One routed expert's record in a TFPW file: its width and the gate, up and down offsets into both sections.
pub const ExpertMeta = extern struct {
b_bits: u32,
heat_rank: u32,
gate_word_off: u64,
up_word_off: u64,
down_word_off: u64,
gate_sc_off: u32,
up_sc_off: u32,
down_sc_off: u32,
gate_snr_db: f32,
up_snr_db: f32,
down_snr_db: f32,
reserved: [2]u32,
};

/// A TFPN file's header: its dense tensor records and both sections' extents.
pub const NonMoeHeader = extern struct {
magic: u32,
version: u32,
num_tensors: u32,
pad0: u32,
off_meta: u64,
off_scales: u64,
off_weights: u64,
total_scale_halfs: u64,
total_weight_words: u64,
pad1: u64,
};

comptime {
std.debug.assert(@sizeOf(LayerHeader) == 144);
std.debug.assert(@sizeOf(ExpertMeta) == 64);
std.debug.assert(@sizeOf(NonMoeHeader) == 64);
}

test {
_ = @import("tfq3_format_test.zig");
}
80 changes: 80 additions & 0 deletions zig/src/core/tfq3_format_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//! The TFQ3 records' bytes pinned field by field: offsets, sizes, alignment and a fixture each converter writes.
const std = @import("std");
const f = @import("tfq3_format.zig");
const t = std.testing;

const Field = struct { []const u8, usize, usize }; // name, byte offset, byte size

fn expectLayout(comptime T: type, comptime fields: []const Field, size: usize, alignment: usize) !void {
try t.expectEqual(size, @sizeOf(T));
try t.expectEqual(alignment, @alignOf(T));
const info = @typeInfo(T).@"struct";
try t.expectEqual(fields.len, info.field_names.len);
inline for (fields, info.field_names, info.field_types) |want, name, ty| {
try t.expectEqualStrings(want[0], name);
try t.expectEqual(want[1], @offsetOf(T, want[0]));
try t.expectEqual(want[2], @sizeOf(ty));
}
}

test "TFQ3 format: every record's fields sit where main's three copies put them" {
try expectLayout(f.LayerHeader, &.{
.{ "magic", 0, 4 }, .{ "version", 4, 4 }, .{ "layer_idx", 8, 4 }, .{ "num_experts", 12, 4 },
.{ "num_tensors", 16, 4 }, .{ "hidden_size", 20, 4 }, .{ "moe_inter", 24, 4 }, .{ "reserved0", 28, 4 },
.{ "raw_fp8_bytes", 32, 8 }, .{ "stream_words", 40, 8 }, .{ "scale_halfs", 48, 8 }, .{ "off_meta", 56, 8 },
.{ "off_scales", 64, 8 }, .{ "off_weights", 72, 8 }, .{ "reserved1", 80, 32 }, .{ "meta_tag", 112, 32 },
}, 144, 8);
try expectLayout(f.ExpertMeta, &.{
.{ "b_bits", 0, 4 }, .{ "heat_rank", 4, 4 }, .{ "gate_word_off", 8, 8 }, .{ "up_word_off", 16, 8 },
.{ "down_word_off", 24, 8 }, .{ "gate_sc_off", 32, 4 }, .{ "up_sc_off", 36, 4 }, .{ "down_sc_off", 40, 4 },
.{ "gate_snr_db", 44, 4 }, .{ "up_snr_db", 48, 4 }, .{ "down_snr_db", 52, 4 }, .{ "reserved", 56, 8 },
}, 64, 8);
try expectLayout(f.NonMoeHeader, &.{
.{ "magic", 0, 4 }, .{ "version", 4, 4 }, .{ "num_tensors", 8, 4 }, .{ "pad0", 12, 4 },
.{ "off_meta", 16, 8 }, .{ "off_scales", 24, 8 }, .{ "off_weights", 32, 8 }, .{ "total_scale_halfs", 40, 8 },
.{ "total_weight_words", 48, 8 }, .{ "pad1", 56, 8 },
}, 64, 8);
}

test "TFQ3 format: the magics spell their file kinds and the sizes are the kernels' and converter's" {
try t.expectEqualStrings("TFPW", std.mem.asBytes(&std.mem.nativeToLittle(u32, f.magic_layer)));
try t.expectEqualStrings("TFPN", std.mem.asBytes(&std.mem.nativeToLittle(u32, f.magic_nonmoe)));
try t.expectEqualStrings("PWV3", std.mem.asBytes(&std.mem.nativeToLittle(u32, f.magic_tables)));
try t.expectEqual(@as(u32, 3), f.format_version);
try t.expectEqual(@as(usize, 16384), f.section_align);
try t.expectEqual(@as(usize, 10816), f.tables_bytes);
try t.expectEqual(@as(usize, 16384), f.tables_file_bytes);
}

/// A record filled with each field's own index, so any moved, resized or reordered field changes the bytes.
fn numbered(comptime T: type) T {
var v = std.mem.zeroes(T);
const info = @typeInfo(T).@"struct";
inline for (info.field_names, info.field_types, 1..) |name, ty, i| switch (@typeInfo(ty)) {
.int => @field(v, name) = @intCast(i),
.float => @field(v, name) = @floatFromInt(i),
.array => @field(v, name) = @splat(@intCast(i)),
else => unreachable,
};
return v;
}

fn expectBytes(comptime T: type, want_hex: []const u8) !void {
const v = numbered(T);
var want: [@sizeOf(T)]u8 = undefined;
_ = try std.fmt.hexToBytes(&want, want_hex);
try t.expectEqualSlices(u8, &want, std.mem.asBytes(&v));
}

test "TFQ3 format: a numbered record of each kind serializes to main's bytes" {
try expectBytes(f.LayerHeader, "01000000" ++ "02000000" ++ "03000000" ++ "04000000" ++ "05000000" ++ "06000000" ++
"07000000" ++ "08000000" ++ "0900000000000000" ++ "0a00000000000000" ++ "0b00000000000000" ++
"0c00000000000000" ++ "0d00000000000000" ++ "0e00000000000000" ++
"0f000000000000000f000000000000000f000000000000000f00000000000000" ++
"1010101010101010101010101010101010101010101010101010101010101010");
try expectBytes(f.ExpertMeta, "01000000" ++ "02000000" ++ "0300000000000000" ++ "0400000000000000" ++
"0500000000000000" ++ "06000000" ++ "07000000" ++ "08000000" ++ "00001041" ++ "00002041" ++ "00003041" ++
"0c0000000c000000");
try expectBytes(f.NonMoeHeader, "01000000" ++ "02000000" ++ "03000000" ++ "04000000" ++ "0500000000000000" ++
"0600000000000000" ++ "0700000000000000" ++ "0800000000000000" ++ "0900000000000000" ++ "0a00000000000000");
}
67 changes: 12 additions & 55 deletions zig/src/cuda/tfq3_format.zig
Original file line number Diff line number Diff line change
@@ -1,26 +1,17 @@
//! Shared TFQ3 binary format headers, validators, and directory discovery (PWV3, TFPN, TFPW v3).
const std = @import("std");

pub const magic_tables: u32 = 0x33565750; // "PWV3"
pub const magic_tfpn: u32 = 0x4E504654; // "TFPN"
pub const magic_tfpw: u32 = 0x57504654; // "TFPW"
pub const format_version: u32 = 3;
pub const section_align: u64 = 16384;
pub const tables_min_bytes: usize = 10816;
pub const tables_file_bytes: usize = 16384;
const format = @import("tfq3_format");

pub const TfpnHeader = extern struct {
magic: u32,
version: u32,
num_tensors: u32,
pad0: u32,
off_meta: u64,
off_scales: u64,
off_weights: u64,
total_scale_halfs: u64,
total_weight_words: u64,
pad1: u64,
};
pub const magic_tables = format.magic_tables;
pub const magic_tfpn = format.magic_nonmoe;
pub const magic_tfpw = format.magic_layer;
pub const format_version = format.format_version;
pub const section_align: u64 = format.section_align;
pub const tables_min_bytes = format.tables_bytes;
pub const tables_file_bytes = format.tables_file_bytes;

pub const TfpnHeader = format.NonMoeHeader;

pub const TfpnTensorMeta = extern struct {
name: [80]u8,
Expand All @@ -39,45 +30,11 @@ pub const TfpnTensorMeta = extern struct {
}
};

pub const TfpwHeader = extern struct {
magic: u32,
version: u32,
layer_idx: u32,
num_experts: u32,
num_tensors: u32,
hidden_size: u32,
moe_inter: u32,
reserved0: u32,
raw_fp8_bytes: u64,
stream_words: u64,
scale_halfs: u64,
off_meta: u64,
off_scales: u64,
off_weights: u64,
reserved1: [4]u64,
meta_tag: [32]u8,
};

pub const TfpwExpertMeta = extern struct {
b_bits: u32,
heat_rank: u32,
gate_word_off: u64,
up_word_off: u64,
down_word_off: u64,
gate_sc_off: u32,
up_sc_off: u32,
down_sc_off: u32,
gate_snr_db: f32,
up_snr_db: f32,
down_snr_db: f32,
reserved: [2]u32,
};
pub const TfpwHeader = format.LayerHeader;
pub const TfpwExpertMeta = format.ExpertMeta;

comptime {
if (@sizeOf(TfpnHeader) != 64) @compileError("TfpnHeader must be 64 bytes");
if (@sizeOf(TfpnTensorMeta) != 128) @compileError("TfpnTensorMeta must be 128 bytes");
if (@sizeOf(TfpwHeader) != 144) @compileError("TfpwHeader must be 144 bytes");
if (@sizeOf(TfpwExpertMeta) != 64) @compileError("TfpwExpertMeta must be 64 bytes");
}

pub fn validBits(bits: u32) bool {
Expand Down
50 changes: 8 additions & 42 deletions zig/src/families/flashnext/tfq3_check.zig
Original file line number Diff line number Diff line change
@@ -1,54 +1,20 @@
//! Flash Next TFQ3 shard records and the checks a shard passes before admission, budgeting or a pointer into it.
const std = @import("std");

pub const magic_layer: u32 = 0x57504654;
pub const magic_v3_tables: u32 = 0x33565750;
pub const format_version: u32 = 3;
const format = @import("tfq3_format");

pub const magic_layer = format.magic_layer;
pub const magic_v3_tables = format.magic_tables;
pub const format_version = format.format_version;
/// The routed kernels' fixed shape (moe_decode.metal): 512 experts of 640 x 2560 gate/up and 2560 x 640 down.
pub const num_experts: u32 = 512;
pub const hidden_size: u32 = 2560;
pub const moe_inter: u32 = 640;
/// The kernels' FnPwV3Tables (flashnext_tfq3/common.metal:40): a file holds at least it (published ones pad to 16 KiB).
pub const tables_bytes: usize = 10816;

pub const LayerHeader = extern struct {
magic: u32,
version: u32,
layer_idx: u32,
num_experts: u32,
num_tensors: u32,
hidden_size: u32,
moe_inter: u32,
reserved0: u32,
raw_fp8_bytes: u64,
stream_words: u64,
scale_halfs: u64,
off_meta: u64,
off_scales: u64,
off_weights: u64,
reserved1: [4]u64,
meta_tag: [32]u8,
};
pub const tables_bytes = format.tables_bytes;

pub const ExpertMeta = extern struct {
b_bits: u32,
heat_rank: u32,
gate_word_off: u64,
up_word_off: u64,
down_word_off: u64,
gate_sc_off: u32,
up_sc_off: u32,
down_sc_off: u32,
gate_snr_db: f32,
up_snr_db: f32,
down_snr_db: f32,
reserved: [2]u32,
};

comptime {
std.debug.assert(@sizeOf(LayerHeader) == 144);
std.debug.assert(@sizeOf(ExpertMeta) == 64);
}
pub const LayerHeader = format.LayerHeader;
pub const ExpertMeta = format.ExpertMeta;

/// Each refusal names the boundary it guards.
pub const Error = error{
Expand Down
Loading
Loading