Skip to content
Prev Previous commit
Next Next commit
fix(core): twin decodes 4-bit codes to integers, as the tiles do
At 4 bits the twin multiplied input j by 16^-j and left code j in
place. The product is the same unless x * 16^-j leaves the float range:
x[7] = 2^-126 gave 2^-154, which underflows to zero, while the tiles
decode the integer and keep 2^-126. The twin now decodes the integer
code.

tf-tile-check adds the case: 4 bits, K 64, code 7 is 1, scales 1,
biases 0, x[7] = 2^-126. The tiles and the twin at widths 1 to 32 must
all give bf16 0x0080.
  • Loading branch information
BlivionIaG committed Oct 15, 2026
commit 6b9d392e94cebefee9df877e7e03b2ec32702e33
10 changes: 5 additions & 5 deletions zig/kernels/metal/core/tile_qmm.metal
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ inline float tf_tq_code(const thread uint* w, int k) {
template <int BITS> struct TfTqCols { static constant constexpr int value = 4; };
constant constexpr int TQ_SG = 2;

// Group g's 64-step fma chains (the MMA order) and input sum; at 4 bits a code in place meets x * 16^-j (same product).
// Group g's 64-step fma chains (the MMA order) and input sum; each code decoded to its integer, as in the tiles.
template <int BITS, int NC>
inline float tf_tq_group(const device bfloat* x, const device uint8_t* w, size_t stride, int g, thread float* p, float one) {
constexpr int GW = 2 * BITS; // words a group of 64 codes
Expand All @@ -294,14 +294,14 @@ inline float tf_tq_group(const device bfloat* x, const device uint8_t* w, size_t
for (int j = 0; j < 8; ++j) {
const float v = as_type<float>((j % 2) ? (xw[j / 2] & 0xFFFF0000u) : (xw[j / 2] << 16));
t = kb == 0 && j == 0 ? v : fma(v, one, t);
xf[j] = BITS == 4 ? v * as_type<float>(uint(127 - 4 * j) << 23) : v;
xf[j] = v;
}
if (BITS == 4) {
#pragma unroll
for (int c = 0; c < NC; ++c) {
const uint word = wq[c][kb];
#pragma unroll
for (int j = 0; j < 8; ++j) p[c] = fma(xf[j], float(word & (0xFu << (4 * j))), p[c]);
for (int j = 0; j < 8; ++j) p[c] = fma(xf[j], float((word >> (4 * j)) & 0xFu), p[c]);
}
} else {
// Codes 8 kb.. of this group sit in the two words from bit 8 kb BITS (48 or 64 bits).
Expand Down Expand Up @@ -353,12 +353,12 @@ inline void tf_tq_group_rows(const device bfloat* x, int K, int rows, const devi
for (int j = 0; j < 8; ++j) {
const float v = as_type<float>((j % 2) ? (xw[j / 2] & 0xFFFF0000u) : (xw[j / 2] << 16));
t[r] = kb == 0 && j == 0 ? v : fma(v, one, t[r]);
xf[j] = v * as_type<float>(uint(127 - 4 * j) << 23);
xf[j] = v;
}
#pragma unroll
for (int c = 0; c < NC; ++c)
#pragma unroll
for (int j = 0; j < 8; ++j) p[r][c] = fma(xf[j], float(wq[c][kb] & (0xFu << (4 * j))), p[r][c]);
for (int j = 0; j < 8; ++j) p[r][c] = fma(xf[j], float((wq[c][kb] >> (4 * j)) & 0xFu), p[r][c]);
}
}
}
Expand Down
44 changes: 44 additions & 0 deletions zig/tests/tile_qmm_check.zig
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,48 @@ fn dump(io: std.Io, dir: []const u8, s: Synth, out: mtl.Buffer) !void {
}
}

/// 4 bits, K 64: code 7 is 1, scales 1, biases 0, x[7] = 2^-126; every path keeps bf16 0x0080 (no range change).
fn witness(g: Gpu) !void {
const rows = tile.twin_rows;
const w = try g.device.buffer(n * 32, opts);
defer w.deinit();
const sc = try g.device.buffer(n * 2, opts);
defer sc.deinit();
const bi = try g.device.buffer(n * 2, opts);
defer bi.deinit();
const x = try g.device.buffer(rows * 64 * 2, opts);
defer x.deinit();
for (w.slice(u8, n * 32), 0..) |*v, i| v.* = if (i % 32 == 3) 0x10 else 0;
for (sc.slice(u16, n)) |*v| v.* = toBf(1);
@memset(bi.slice(u16, n), 0);
for (x.slice(u16, rows * 64), 0..) |*v, i| v.* = if (i % 64 == 7) 0x0080 else 0;
const m = tile.Matrix{
.w = w,
.w_off = 0,
.scales = sc,
.s_off = 0,
.biases = bi,
.b_off = 0,
.n = n,
.k = 64,
.bits = 4,
};
const sums = try g.device.buffer(rows * 4, opts);
defer sums.deinit();
const y = try g.device.buffer(rows * n * 2, opts);
defer y.deinit();
try g.run(Tiles{ .m = m, .x = x, .sums = sums, .y = y, .first = 0, .rows = rows });
for (bf16s(y, rows)) |v| if (v != 0x0080) return error.WitnessTiles;
for ([_]usize{ 1, 2, 7, 8, 16, 32 }) |width| {
@memset(y.slice(u16, rows * n), 0);
try g.run(Twin{ .m = m, .x = x, .y = y, .first = 0, .rows = width });
for (bf16s(y, width)) |v| if (v != 0x0080) {
std.debug.print("witness: twin at width {d} gives 0x{x:0>4}, the tiles 0x0080\n", .{ width, v });
return error.WitnessTwin;
};
}
}

pub fn main(init: std.process.Init) !void {
const args = try init.minimal.args.toSlice(init.arena.allocator());
const dir: ?[]const u8 = if (args.len == 3 and std.mem.eql(u8, args[1], "--dump")) args[2] else null;
Expand All @@ -273,6 +315,8 @@ pub fn main(init: std.process.Init) !void {
defer p.deinit();
const g = Gpu{ .device = device, .queue = queue, .p = &p };
var passed: usize = 0;
try witness(g);
std.debug.print("4-bit witness (code 7 = 1, x[7] = 2^-126): tiles and twin at widths 1-32 keep bf16 0x0080\n", .{});
for (tile.bit_widths, 0..) |bits, i| {
const s = try Synth.init(device, bits, 0x7173 + i);
defer s.deinit();
Expand Down
Loading