From a9ae80945aeb3a5b1c7fd407081c65a620d6597a Mon Sep 17 00:00:00 2001 From: Aditya <1894981+hiraditya@users.noreply.github.com> Date: Sun, 22 Nov 2026 17:13:11 -0800 Subject: [PATCH 1/2] rounds: lm_head of the rows that can be the greedy token A round of 3 or 4 positions spent about 10 ms in lm_head, and a round of 8 about 20 ms, for the arithmetic of the 6-bit weights. A round needs only the greedy token of each position, and its logit. For 2 or more positions, `run_round35` now runs three kernels: - `lm_rough_q6`: rough logits a for up to 8 positions at once, with f16 weights and inputs on the simdgroup matrices. Its time is about that of one position. - `lm_pick_threshold`: for each position, M = the largest a - B, where B is a bound of |a - e| from the norms of x and of the rows (src/q6.vx gives the terms of the bound). - `matvec_q6_rows4_pick`: the exact logits e (those of `matvec_q6`) of each row that does not have a + B < M; the other rows get the lowest f32 and cannot be the greedy token. A block with no such row reads no weights. The norms of the rows come from `lm_q6_norms` in the first round. The greedy tokens, and their logits, stay those of the plain steps bit for bit; the other logits of a round are no longer exact. `Round35.pick` false gives all the logits exactly, as before. tests/round35.vx: each round runs again with the pick, and its greedy tokens and their logits must be those of the plain steps. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014U9gTxfQzAEpmg4WJhYQqw --- src/q6.vx | 602 +++++++++++++++++++++++++++++++++++++++++++++++ src/round35.vx | 28 ++- tests/round35.vx | 38 ++- 3 files changed, 658 insertions(+), 10 deletions(-) diff --git a/src/q6.vx b/src/q6.vx index e321200..bab2e9f 100644 --- a/src/q6.vx +++ b/src/q6.vx @@ -576,3 +576,605 @@ fn matvec_q6_rows4(y : &mut Tensor, codes : &Tensor, an : &mut Tensor, codes : &Tensor, high : &Tensor, scales : &Tensor, rows : i32, cols : i32) -> void { + assert(cols % 32 == 0 && wn.len() >= rows && an.len() >= rows, "lm_q6_norms: cols must be a multiple of 32, and wn and an need rows values"); + let words = cols / 8; + let hwords = cols / 16; + let groups = cols / 32; + spawn on(Topology::Device) { + for r in 0..rows across blocks { + let mut pw = unsafe { Tensor::uninit() }; + let mut pa = unsafe { Tensor::uninit() }; + for l in 0..32 across lanes(32) { + let mut sw : f32 = 0.0; + let mut sa : f32 = 0.0; + let mut i = l * 2; + while i < words { + let w0 = codes[r * words + i]; + let w1 = codes[r * words + i + 1]; + let h = high[r * hwords + i / 2]; + let sc = scales[r * groups + i / 4] as f32; + for t in 0..16 { + let mut lo = (w0 >> ((t * 4) as u32)) & 15u32; + if t >= 8 { + lo = (w1 >> (((t - 8) * 4) as u32)) & 15u32; + } + let q = (lo + 16u32 * ((h >> ((t * 2) as u32)) & 3u32)) as f32; + let v = (q - 32.0) * sc; + let u = (q + 32.0) * sc; + sw = sw + v * v; + sa = sa + u * u; + } + i = i + 64; + } + pw[l] = sw; + pa[l] = sa; + } + let s = lanes::sum(&pw, 32); + let s2 = lanes::sum(&pa, 32); + wn[r] = s.sqrt(); + an[r] = s2.sqrt(); + } + } + return; +} + +/// The rough logits a of the bound above, for x[0] to x[n - 1]: a[p][r] is about (row r) . x[p]. +/// A block takes 64 rows and 8 vectors (rows p of a past n - 1 get the products of zeros), and +/// subgroup sg makes rows sg * 16 to + 15. For each 32 columns, each lane writes 16 weights +/// (q - 32) * scale as f16 to shared memory, and the sums stay in the simdgroup matrices. +fn lm_rough_q6(a : &mut Tensor, codes : &Tensor, high : &Tensor, scales : &Tensor, x : &Tensor, n : i32, rows : i32, cols : i32) -> void { + assert(cols % 32 == 0 && rows % 64 == 0 && n >= 1 && a.extent(0) >= ((n + 7) / 8) * 8 && a.extent(1) >= rows && x.extent(0) >= n && x.extent(1) >= cols, "lm_rough_q6: cols must be a multiple of 32 and rows of 64, a needs n rows rounded up to 8, and x n rows"); + let words = cols / 8; + let hwords = cols / 16; + let groups = cols / 32; + spawn on(Topology::Device) { + for rb in 0..rows / 64 across blocks { + for pt in 0..(n + 7) / 8 across blocks { + let mut sw = unsafe { Tensor::uninit() }; + let mut sx = unsafe { Tensor::uninit() }; + let mut acc = unsafe { Tensor::uninit() }; + for sg in 0..4 across subgroups { + lanes::fill(&mut acc[sg], 0.0); + } + for g in 0..groups { + for sg in 0..4 across subgroups { + for l in 0..32 across lanes(32) { + let rl = l % 16; + let half = l / 16; + let row = rb * 64 + sg * 16 + rl; + let at = row * words + g * 4 + half * 2; + let w0 = codes[at]; + let w1 = codes[at + 1]; + let h = high[row * hwords + g * 2 + half]; + let sc = scales[row * groups + g]; + sw[sg][half * 16 + 0][rl] = ((((w0 & 15u32) + ((h << 4u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 1][rl] = (((((w0 >> 4u32) & 15u32) + ((h << 2u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 2][rl] = (((((w0 >> 8u32) & 15u32) + ((h >> 0u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 3][rl] = (((((w0 >> 12u32) & 15u32) + ((h >> 2u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 4][rl] = (((((w0 >> 16u32) & 15u32) + ((h >> 4u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 5][rl] = (((((w0 >> 20u32) & 15u32) + ((h >> 6u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 6][rl] = (((((w0 >> 24u32) & 15u32) + ((h >> 8u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 7][rl] = (((((w0 >> 28u32) & 15u32) + ((h >> 10u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 8][rl] = ((((w1 & 15u32) + ((h >> 12u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 9][rl] = (((((w1 >> 4u32) & 15u32) + ((h >> 14u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 10][rl] = (((((w1 >> 8u32) & 15u32) + ((h >> 16u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 11][rl] = (((((w1 >> 12u32) & 15u32) + ((h >> 18u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 12][rl] = (((((w1 >> 16u32) & 15u32) + ((h >> 20u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 13][rl] = (((((w1 >> 20u32) & 15u32) + ((h >> 22u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 14][rl] = (((((w1 >> 24u32) & 15u32) + ((h >> 24u32) & 48u32)) as i32 - 32) as f16) * sc; + sw[sg][half * 16 + 15][rl] = (((((w1 >> 28u32) & 15u32) + ((h >> 26u32) & 48u32)) as i32 - 32) as f16) * sc; + let p = pt * 8 + sg * 2 + l / 16; + let k = g * 32 + (l % 16) * 2; + let mut v0 : f32 = 0.0; + let mut v1 : f32 = 0.0; + if p < n { + v0 = x[p][k]; + v1 = x[p][k + 1]; + } + sx[0][sg * 2 + l / 16][(l % 16) * 2] = v0 as f16; + sx[0][sg * 2 + l / 16][(l % 16) * 2 + 1] = v1 as f16; + } + } + for sg in 0..4 across subgroups { + acc[sg] = acc[sg] + (sx[0] as f32) @ (sw[sg] as f32); + } + } + for sg in 0..4 across subgroups { + lanes::copy(a, &acc[sg], [pt * 8, rb * 64 + sg * 16]); + } + } + } + } + return; +} + +/// thr[p] = (M, |x[p]|, |x[p] - f16(x[p])|) for p below n, with M of the bound above. +fn lm_pick_threshold(thr : &mut Tensor, a : &Tensor, wn : &Tensor, an : &Tensor, x : &Tensor, n : i32, rows : i32, cols : i32) -> void { + assert(n >= 1 && thr.extent(0) >= n && thr.extent(1) >= 3 && a.extent(0) >= n && a.extent(1) >= rows && x.extent(0) >= n && x.extent(1) >= cols && wn.len() >= rows && an.len() >= rows, "lm_pick_threshold: thr needs n rows of 3 values, a and x n rows, and wn and an rows values"); + spawn on(Topology::Device) { + for p in 0..n across blocks { + let mut px = unsafe { Tensor::uninit() }; + let mut pd = unsafe { Tensor::uninit() }; + let mut pm = unsafe { Tensor::uninit() }; + for l in 0..1024 across lanes { + let mut sx : f32 = 0.0; + let mut sd : f32 = 0.0; + let mut i = l; + while i < cols { + let v = x[p][i]; + let d = v - ((v as f16) as f32); + sx = sx + v * v; + sd = sd + d * d; + i = i + 1024; + } + px[l] = sx; + pd[l] = sd; + } + let s = lanes::sum(&px, 1024); + let s2 = lanes::sum(&pd, 1024); + let xs = s.sqrt(); + let ds = s2.sqrt(); + for l in 0..1024 across lanes { + let mut m : f32 = -340000000000000000000000000000000000000.0; + let mut r = l; + while r < rows { + let v = a[p][r] - (1.25 * (wn[r] * (0.001 * xs + 1.01 * ds) + 0.00001 * an[r] * xs + 0.000002 * xs)); + if v > m { + m = v; + } + r = r + 1024; + } + pm[l] = m; + } + let mut m = lanes::max(&pm, 1024); + if !(xs < 60000.0) { + m = -340000000000000000000000000000000000000.0; + } + thr[p][0] = m; + thr[p][1] = xs; + thr[p][2] = ds; + } + } + return; +} + +/// `matvec_q6_rows4` for the rows that can be the greedy token of each vector, after +/// `lm_rough_q6` (a) and `lm_pick_threshold` (thr): y[p][r] is the y of `matvec_q6` for x[p] bit for +/// bit when !(a[p][r] + B < M), and the lowest f32 otherwise. +fn matvec_q6_rows4_pick(y : &mut Tensor, codes : &Tensor, high : &Tensor, scales : &Tensor, x : &Tensor, a : &Tensor, wn : &Tensor, an : &Tensor, thr : &Tensor, n : i32, rows : i32, cols : i32) -> void { + assert(a.extent(0) >= n && a.extent(1) >= rows && thr.extent(0) >= n && wn.len() >= rows && an.len() >= rows, "matvec_q6_rows4_pick: a and thr need n rows, and wn and an rows values"); + assert(cols % 32 == 0 && rows % 8 == 0 && n >= 1 && y.extent(0) >= n && x.extent(0) >= n && y.extent(1) >= rows && x.extent(1) >= cols, "matvec_q6_rows4_pick: cols must be a multiple of 32 and rows of 8, and y and x need n rows of rows and cols values"); + assert(codes.len() / rows >= cols / 8 && high.len() / rows >= cols / 16 && scales.len() / rows >= cols / 32, "matvec_q6_rows4_pick: the matrix has too few words or scales for rows x cols"); + let words = cols / 8; + let hwords = cols / 16; + let groups = cols / 32; + spawn on(Topology::Device) { + for rb in 0..rows / 8 across blocks { + for pq in 0..(n + 3) / 4 across blocks { + let mut part = unsafe { + Tensor::uninit() + }; + let mut flag = unsafe { Tensor::uninit() }; + for s in 0..1 across subgroups { + // Lane l: row rb * 8 + l / 4 for vector pq * 4 + l % 4 (the last vector again past n - 1). + for l in 0..32 across lanes(32) { + let mut p = pq * 4 + l % 4; + if p >= n { + p = n - 1; + } + let row = rb * 8 + l / 4; + let xs = thr[p][1]; + let ds = thr[p][2]; + let mut f : f32 = 0.0; + if !(a[p][row] + 1.25 * (wn[row] * (0.001 * xs + 1.01 * ds) + 0.00001 * an[row] * xs + 0.000002 * xs) < thr[p][0]) { + f = 1.0; + } + flag[l] = f; + } + // A block with no row to pick reads no weights. + let live = lanes::max(&flag, 32); + let mut end = 0; + if live > 0.5 { + end = words; + } + for l in 0..32 across lanes(32) { + let mut p0 = pq * 4; + if p0 >= n { + p0 = n - 1; + } + let mut p1 = pq * 4 + 1; + if p1 >= n { + p1 = n - 1; + } + let mut p2 = pq * 4 + 2; + if p2 >= n { + p2 = n - 1; + } + let mut p3 = pq * 4 + 3; + if p3 >= n { + p3 = n - 1; + } + let mut acc = unsafe { Tensor::uninit() }; + for j in 0..32 { + acc[j] = 0.0; + } + let mut i = l * 2; + while i < end { + let c = i * 8; + let mut xa = unsafe { Tensor::uninit() }; + for t in 0..16 { + xa[t] = x[p0][c + t]; + } + let suma = 32.0 * (xa[0] + xa[1] + xa[2] + xa[3] + xa[4] + xa[5] + xa[6] + xa[7] + xa[8] + xa[9] + xa[10] + xa[11] + xa[12] + xa[13] + xa[14] + xa[15]); + xa[1] = xa[1] * 0.0625; + xa[2] = xa[2] * 0.00390625; + xa[3] = xa[3] * 0.000244140625; + xa[4] = xa[4] * 0.0000152587890625; + xa[5] = xa[5] * 0.00000095367431640625; + xa[6] = xa[6] * 0.000000059604644775390625; + xa[7] = xa[7] * 0.0000000037252902984619140625; + xa[9] = xa[9] * 0.0625; + xa[10] = xa[10] * 0.00390625; + xa[11] = xa[11] * 0.000244140625; + xa[12] = xa[12] * 0.0000152587890625; + xa[13] = xa[13] * 0.00000095367431640625; + xa[14] = xa[14] * 0.000000059604644775390625; + xa[15] = xa[15] * 0.0000000037252902984619140625; + let mut xb = unsafe { Tensor::uninit() }; + for t in 0..16 { + xb[t] = x[p1][c + t]; + } + let sumb = 32.0 * (xb[0] + xb[1] + xb[2] + xb[3] + xb[4] + xb[5] + xb[6] + xb[7] + xb[8] + xb[9] + xb[10] + xb[11] + xb[12] + xb[13] + xb[14] + xb[15]); + xb[1] = xb[1] * 0.0625; + xb[2] = xb[2] * 0.00390625; + xb[3] = xb[3] * 0.000244140625; + xb[4] = xb[4] * 0.0000152587890625; + xb[5] = xb[5] * 0.00000095367431640625; + xb[6] = xb[6] * 0.000000059604644775390625; + xb[7] = xb[7] * 0.0000000037252902984619140625; + xb[9] = xb[9] * 0.0625; + xb[10] = xb[10] * 0.00390625; + xb[11] = xb[11] * 0.000244140625; + xb[12] = xb[12] * 0.0000152587890625; + xb[13] = xb[13] * 0.00000095367431640625; + xb[14] = xb[14] * 0.000000059604644775390625; + xb[15] = xb[15] * 0.0000000037252902984619140625; + let mut xc = unsafe { Tensor::uninit() }; + for t in 0..16 { + xc[t] = x[p2][c + t]; + } + let sumc = 32.0 * (xc[0] + xc[1] + xc[2] + xc[3] + xc[4] + xc[5] + xc[6] + xc[7] + xc[8] + xc[9] + xc[10] + xc[11] + xc[12] + xc[13] + xc[14] + xc[15]); + xc[1] = xc[1] * 0.0625; + xc[2] = xc[2] * 0.00390625; + xc[3] = xc[3] * 0.000244140625; + xc[4] = xc[4] * 0.0000152587890625; + xc[5] = xc[5] * 0.00000095367431640625; + xc[6] = xc[6] * 0.000000059604644775390625; + xc[7] = xc[7] * 0.0000000037252902984619140625; + xc[9] = xc[9] * 0.0625; + xc[10] = xc[10] * 0.00390625; + xc[11] = xc[11] * 0.000244140625; + xc[12] = xc[12] * 0.0000152587890625; + xc[13] = xc[13] * 0.00000095367431640625; + xc[14] = xc[14] * 0.000000059604644775390625; + xc[15] = xc[15] * 0.0000000037252902984619140625; + let mut xd = unsafe { Tensor::uninit() }; + for t in 0..16 { + xd[t] = x[p3][c + t]; + } + let sumd = 32.0 * (xd[0] + xd[1] + xd[2] + xd[3] + xd[4] + xd[5] + xd[6] + xd[7] + xd[8] + xd[9] + xd[10] + xd[11] + xd[12] + xd[13] + xd[14] + xd[15]); + xd[1] = xd[1] * 0.0625; + xd[2] = xd[2] * 0.00390625; + xd[3] = xd[3] * 0.000244140625; + xd[4] = xd[4] * 0.0000152587890625; + xd[5] = xd[5] * 0.00000095367431640625; + xd[6] = xd[6] * 0.000000059604644775390625; + xd[7] = xd[7] * 0.0000000037252902984619140625; + xd[9] = xd[9] * 0.0625; + xd[10] = xd[10] * 0.00390625; + xd[11] = xd[11] * 0.000244140625; + xd[12] = xd[12] * 0.0000152587890625; + xd[13] = xd[13] * 0.00000095367431640625; + xd[14] = xd[14] * 0.000000059604644775390625; + xd[15] = xd[15] * 0.0000000037252902984619140625; + for j in 0..8 { + let row = rb * 8 + j; + let at = row * words + i; + let w0 = codes[at]; + let w1 = codes[at + 1]; + let h = high[row * hwords + i / 2]; + let sc = scales[row * groups + i / 4] as f32; + let f0 = (w0 & 15u32) as f32; + let f1 = (w0 & 240u32) as f32; + let f2 = (w0 & 3840u32) as f32; + let f3 = (w0 & 61440u32) as f32; + let f4 = (w0 & 983040u32) as f32; + let f5 = (w0 & 15728640u32) as f32; + let f6 = (w0 & 251658240u32) as f32; + let f7 = (w0 & 4026531840u32) as f32; + let f8 = (w1 & 15u32) as f32; + let f9 = (w1 & 240u32) as f32; + let f10 = (w1 & 3840u32) as f32; + let f11 = (w1 & 61440u32) as f32; + let f12 = (w1 & 983040u32) as f32; + let f13 = (w1 & 15728640u32) as f32; + let f14 = (w1 & 251658240u32) as f32; + let f15 = (w1 & 4026531840u32) as f32; + let g0 = ((h & 3u32) as f32) * 16.0; + let g1 = ((h & 12u32) as f32) * 64.0; + let g2 = ((h & 48u32) as f32) * 256.0; + let g3 = ((h & 192u32) as f32) * 1024.0; + let g4 = ((h & 768u32) as f32) * 4096.0; + let g5 = ((h & 3072u32) as f32) * 16384.0; + let g6 = ((h & 12288u32) as f32) * 65536.0; + let g7 = ((h & 49152u32) as f32) * 262144.0; + let g8 = ((h & 196608u32) as f32) * 0.000244140625; + let g9 = ((h & 786432u32) as f32) * 0.0009765625; + let g10 = ((h & 3145728u32) as f32) * 0.00390625; + let g11 = ((h & 12582912u32) as f32) * 0.015625; + let g12 = ((h & 50331648u32) as f32) * 0.0625; + let g13 = ((h & 201326592u32) as f32) * 0.25; + let g14 = ((h & 805306368u32) as f32) * 1.0; + let g15 = ((h & 3221225472u32) as f32) * 4.0; + let lowa = f0 * xa[0] + f1 * xa[1] + f2 * xa[2] + f3 * xa[3] + f4 * xa[4] + f5 * xa[5] + f6 * xa[6] + f7 * xa[7] + f8 * xa[8] + f9 * xa[9] + f10 * xa[10] + f11 * xa[11] + f12 * xa[12] + f13 * xa[13] + f14 * xa[14] + f15 * xa[15]; + let topa = g0 * xa[0] + g1 * xa[1] + g2 * xa[2] + g3 * xa[3] + g4 * xa[4] + g5 * xa[5] + g6 * xa[6] + g7 * xa[7] + g8 * xa[8] + g9 * xa[9] + g10 * xa[10] + g11 * xa[11] + g12 * xa[12] + g13 * xa[13] + g14 * xa[14] + g15 * xa[15]; + acc[0 + j] = acc[0 + j] + (lowa + topa - suma) * sc; + let lowb = f0 * xb[0] + f1 * xb[1] + f2 * xb[2] + f3 * xb[3] + f4 * xb[4] + f5 * xb[5] + f6 * xb[6] + f7 * xb[7] + f8 * xb[8] + f9 * xb[9] + f10 * xb[10] + f11 * xb[11] + f12 * xb[12] + f13 * xb[13] + f14 * xb[14] + f15 * xb[15]; + let topb = g0 * xb[0] + g1 * xb[1] + g2 * xb[2] + g3 * xb[3] + g4 * xb[4] + g5 * xb[5] + g6 * xb[6] + g7 * xb[7] + g8 * xb[8] + g9 * xb[9] + g10 * xb[10] + g11 * xb[11] + g12 * xb[12] + g13 * xb[13] + g14 * xb[14] + g15 * xb[15]; + acc[8 + j] = acc[8 + j] + (lowb + topb - sumb) * sc; + let lowc = f0 * xc[0] + f1 * xc[1] + f2 * xc[2] + f3 * xc[3] + f4 * xc[4] + f5 * xc[5] + f6 * xc[6] + f7 * xc[7] + f8 * xc[8] + f9 * xc[9] + f10 * xc[10] + f11 * xc[11] + f12 * xc[12] + f13 * xc[13] + f14 * xc[14] + f15 * xc[15]; + let topc = g0 * xc[0] + g1 * xc[1] + g2 * xc[2] + g3 * xc[3] + g4 * xc[4] + g5 * xc[5] + g6 * xc[6] + g7 * xc[7] + g8 * xc[8] + g9 * xc[9] + g10 * xc[10] + g11 * xc[11] + g12 * xc[12] + g13 * xc[13] + g14 * xc[14] + g15 * xc[15]; + acc[16 + j] = acc[16 + j] + (lowc + topc - sumc) * sc; + let lowd = f0 * xd[0] + f1 * xd[1] + f2 * xd[2] + f3 * xd[3] + f4 * xd[4] + f5 * xd[5] + f6 * xd[6] + f7 * xd[7] + f8 * xd[8] + f9 * xd[9] + f10 * xd[10] + f11 * xd[11] + f12 * xd[12] + f13 * xd[13] + f14 * xd[14] + f15 * xd[15]; + let topd = g0 * xd[0] + g1 * xd[1] + g2 * xd[2] + g3 * xd[3] + g4 * xd[4] + g5 * xd[5] + g6 * xd[6] + g7 * xd[7] + g8 * xd[8] + g9 * xd[9] + g10 * xd[10] + g11 * xd[11] + g12 * xd[12] + g13 * xd[13] + g14 * xd[14] + g15 * xd[15]; + acc[24 + j] = acc[24 + j] + (lowd + topd - sumd) * sc; + } + i = i + 64; + } + for j in 0..8 { + part[0][j][l] = acc[0 + j]; + part[1][j][l] = acc[8 + j]; + part[2][j][l] = acc[16 + j]; + part[3][j][l] = acc[24 + j]; + } + } + let u0_0 = lanes::sum(&part[0][0], 32); + let u0_1 = lanes::sum(&part[0][1], 32); + let u0_2 = lanes::sum(&part[0][2], 32); + let u0_3 = lanes::sum(&part[0][3], 32); + let u0_4 = lanes::sum(&part[0][4], 32); + let u0_5 = lanes::sum(&part[0][5], 32); + let u0_6 = lanes::sum(&part[0][6], 32); + let u0_7 = lanes::sum(&part[0][7], 32); + let u1_0 = lanes::sum(&part[1][0], 32); + let u1_1 = lanes::sum(&part[1][1], 32); + let u1_2 = lanes::sum(&part[1][2], 32); + let u1_3 = lanes::sum(&part[1][3], 32); + let u1_4 = lanes::sum(&part[1][4], 32); + let u1_5 = lanes::sum(&part[1][5], 32); + let u1_6 = lanes::sum(&part[1][6], 32); + let u1_7 = lanes::sum(&part[1][7], 32); + let u2_0 = lanes::sum(&part[2][0], 32); + let u2_1 = lanes::sum(&part[2][1], 32); + let u2_2 = lanes::sum(&part[2][2], 32); + let u2_3 = lanes::sum(&part[2][3], 32); + let u2_4 = lanes::sum(&part[2][4], 32); + let u2_5 = lanes::sum(&part[2][5], 32); + let u2_6 = lanes::sum(&part[2][6], 32); + let u2_7 = lanes::sum(&part[2][7], 32); + let u3_0 = lanes::sum(&part[3][0], 32); + let u3_1 = lanes::sum(&part[3][1], 32); + let u3_2 = lanes::sum(&part[3][2], 32); + let u3_3 = lanes::sum(&part[3][3], 32); + let u3_4 = lanes::sum(&part[3][4], 32); + let u3_5 = lanes::sum(&part[3][5], 32); + let u3_6 = lanes::sum(&part[3][6], 32); + let u3_7 = lanes::sum(&part[3][7], 32); + if pq * 4 + 0 < n { + let mut v0_0 : f32 = -340000000000000000000000000000000000000.0; + if flag[0] > 0.5 { + v0_0 = u0_0; + } + y[pq * 4 + 0][rb * 8 + 0] = v0_0; + let mut v0_1 : f32 = -340000000000000000000000000000000000000.0; + if flag[4] > 0.5 { + v0_1 = u0_1; + } + y[pq * 4 + 0][rb * 8 + 1] = v0_1; + let mut v0_2 : f32 = -340000000000000000000000000000000000000.0; + if flag[8] > 0.5 { + v0_2 = u0_2; + } + y[pq * 4 + 0][rb * 8 + 2] = v0_2; + let mut v0_3 : f32 = -340000000000000000000000000000000000000.0; + if flag[12] > 0.5 { + v0_3 = u0_3; + } + y[pq * 4 + 0][rb * 8 + 3] = v0_3; + let mut v0_4 : f32 = -340000000000000000000000000000000000000.0; + if flag[16] > 0.5 { + v0_4 = u0_4; + } + y[pq * 4 + 0][rb * 8 + 4] = v0_4; + let mut v0_5 : f32 = -340000000000000000000000000000000000000.0; + if flag[20] > 0.5 { + v0_5 = u0_5; + } + y[pq * 4 + 0][rb * 8 + 5] = v0_5; + let mut v0_6 : f32 = -340000000000000000000000000000000000000.0; + if flag[24] > 0.5 { + v0_6 = u0_6; + } + y[pq * 4 + 0][rb * 8 + 6] = v0_6; + let mut v0_7 : f32 = -340000000000000000000000000000000000000.0; + if flag[28] > 0.5 { + v0_7 = u0_7; + } + y[pq * 4 + 0][rb * 8 + 7] = v0_7; + } + if pq * 4 + 1 < n { + let mut v1_0 : f32 = -340000000000000000000000000000000000000.0; + if flag[1] > 0.5 { + v1_0 = u1_0; + } + y[pq * 4 + 1][rb * 8 + 0] = v1_0; + let mut v1_1 : f32 = -340000000000000000000000000000000000000.0; + if flag[5] > 0.5 { + v1_1 = u1_1; + } + y[pq * 4 + 1][rb * 8 + 1] = v1_1; + let mut v1_2 : f32 = -340000000000000000000000000000000000000.0; + if flag[9] > 0.5 { + v1_2 = u1_2; + } + y[pq * 4 + 1][rb * 8 + 2] = v1_2; + let mut v1_3 : f32 = -340000000000000000000000000000000000000.0; + if flag[13] > 0.5 { + v1_3 = u1_3; + } + y[pq * 4 + 1][rb * 8 + 3] = v1_3; + let mut v1_4 : f32 = -340000000000000000000000000000000000000.0; + if flag[17] > 0.5 { + v1_4 = u1_4; + } + y[pq * 4 + 1][rb * 8 + 4] = v1_4; + let mut v1_5 : f32 = -340000000000000000000000000000000000000.0; + if flag[21] > 0.5 { + v1_5 = u1_5; + } + y[pq * 4 + 1][rb * 8 + 5] = v1_5; + let mut v1_6 : f32 = -340000000000000000000000000000000000000.0; + if flag[25] > 0.5 { + v1_6 = u1_6; + } + y[pq * 4 + 1][rb * 8 + 6] = v1_6; + let mut v1_7 : f32 = -340000000000000000000000000000000000000.0; + if flag[29] > 0.5 { + v1_7 = u1_7; + } + y[pq * 4 + 1][rb * 8 + 7] = v1_7; + } + if pq * 4 + 2 < n { + let mut v2_0 : f32 = -340000000000000000000000000000000000000.0; + if flag[2] > 0.5 { + v2_0 = u2_0; + } + y[pq * 4 + 2][rb * 8 + 0] = v2_0; + let mut v2_1 : f32 = -340000000000000000000000000000000000000.0; + if flag[6] > 0.5 { + v2_1 = u2_1; + } + y[pq * 4 + 2][rb * 8 + 1] = v2_1; + let mut v2_2 : f32 = -340000000000000000000000000000000000000.0; + if flag[10] > 0.5 { + v2_2 = u2_2; + } + y[pq * 4 + 2][rb * 8 + 2] = v2_2; + let mut v2_3 : f32 = -340000000000000000000000000000000000000.0; + if flag[14] > 0.5 { + v2_3 = u2_3; + } + y[pq * 4 + 2][rb * 8 + 3] = v2_3; + let mut v2_4 : f32 = -340000000000000000000000000000000000000.0; + if flag[18] > 0.5 { + v2_4 = u2_4; + } + y[pq * 4 + 2][rb * 8 + 4] = v2_4; + let mut v2_5 : f32 = -340000000000000000000000000000000000000.0; + if flag[22] > 0.5 { + v2_5 = u2_5; + } + y[pq * 4 + 2][rb * 8 + 5] = v2_5; + let mut v2_6 : f32 = -340000000000000000000000000000000000000.0; + if flag[26] > 0.5 { + v2_6 = u2_6; + } + y[pq * 4 + 2][rb * 8 + 6] = v2_6; + let mut v2_7 : f32 = -340000000000000000000000000000000000000.0; + if flag[30] > 0.5 { + v2_7 = u2_7; + } + y[pq * 4 + 2][rb * 8 + 7] = v2_7; + } + if pq * 4 + 3 < n { + let mut v3_0 : f32 = -340000000000000000000000000000000000000.0; + if flag[3] > 0.5 { + v3_0 = u3_0; + } + y[pq * 4 + 3][rb * 8 + 0] = v3_0; + let mut v3_1 : f32 = -340000000000000000000000000000000000000.0; + if flag[7] > 0.5 { + v3_1 = u3_1; + } + y[pq * 4 + 3][rb * 8 + 1] = v3_1; + let mut v3_2 : f32 = -340000000000000000000000000000000000000.0; + if flag[11] > 0.5 { + v3_2 = u3_2; + } + y[pq * 4 + 3][rb * 8 + 2] = v3_2; + let mut v3_3 : f32 = -340000000000000000000000000000000000000.0; + if flag[15] > 0.5 { + v3_3 = u3_3; + } + y[pq * 4 + 3][rb * 8 + 3] = v3_3; + let mut v3_4 : f32 = -340000000000000000000000000000000000000.0; + if flag[19] > 0.5 { + v3_4 = u3_4; + } + y[pq * 4 + 3][rb * 8 + 4] = v3_4; + let mut v3_5 : f32 = -340000000000000000000000000000000000000.0; + if flag[23] > 0.5 { + v3_5 = u3_5; + } + y[pq * 4 + 3][rb * 8 + 5] = v3_5; + let mut v3_6 : f32 = -340000000000000000000000000000000000000.0; + if flag[27] > 0.5 { + v3_6 = u3_6; + } + y[pq * 4 + 3][rb * 8 + 6] = v3_6; + let mut v3_7 : f32 = -340000000000000000000000000000000000000.0; + if flag[31] > 0.5 { + v3_7 = u3_7; + } + y[pq * 4 + 3][rb * 8 + 7] = v3_7; + } + } + } + } + } + return; +} diff --git a/src/round35.vx b/src/round35.vx index 3315863..f25cc3f 100644 --- a/src/round35.vx +++ b/src/round35.vx @@ -60,6 +60,15 @@ struct Round35 { logits : Tensor, next : Tensor, kept : Vec, + // The lm_head of 2 or more positions (src/q6.vx): the rough logits, the norms of the rows of + // lm_head (from the first round that uses them), and the threshold of each position. With `pick` + // false, each round computes all the logits exactly, as for 1 position. + rough : Tensor, + wn : Tensor, + an : Tensor, + thr : Tensor, + norms_ready : bool, + pick : bool, } /// What a round keeps of one layer for `commit_round35`. A full attention layer keeps only `proj`, @@ -149,6 +158,12 @@ fn new_round35(c : &Config) -> Round35 { logits: transfer(Tensor([n, c.vocab_size]), Memory::HBM), next: transfer(Tensor([n]), Memory::HBM), kept: kept, + rough: transfer(Tensor([((n + 7) / 8) * 8, c.vocab_size]), Memory::HBM), + wn: transfer(Tensor([c.vocab_size]), Memory::HBM), + an: transfer(Tensor([c.vocab_size]), Memory::HBM), + thr: transfer(Tensor([n, 4]), Memory::HBM), + norms_ready: false, + pick: true, }; } @@ -689,7 +704,18 @@ fn run_round35(w : &Qwen35Weights, s : &mut Qwen35State, r : &mut Round35, token l = l + 1; } rms_norm_f32_rows(&mut r.xn, &r.x, &w.final_norm, n, c.hidden_size, eps); - if n >= 3 { + let lm = &w.lm_head; + if r.pick && n >= 2 && c.vocab_size % 64 == 0 { + // Only the rows that can be the greedy token get their exact logits; the others get the lowest + // f32, so the greedy tokens, and their logits, are those of all the rows. + if !r.norms_ready { + lm_q6_norms(&mut r.wn, &mut r.an, &lm.codes, &lm.high, &lm.group_scales, c.vocab_size, c.hidden_size); + r.norms_ready = true; + } + lm_rough_q6(&mut r.rough, &lm.codes, &lm.high, &lm.group_scales, &r.xn, n, c.vocab_size, c.hidden_size); + lm_pick_threshold(&mut r.thr, &r.rough, &r.wn, &r.an, &r.xn, n, c.vocab_size, c.hidden_size); + matvec_q6_rows4_pick(&mut r.logits, &lm.codes, &lm.high, &lm.group_scales, &r.xn, &r.rough, &r.wn, &r.an, &r.thr, n, c.vocab_size, c.hidden_size); + } else if n >= 3 { matvec_q6_rows4(&mut r.logits, &w.lm_head.codes, &w.lm_head.high, &w.lm_head.group_scales, &r.xn, n, c.vocab_size, c.hidden_size); } else { matvec_q6_rows(&mut r.logits, &w.lm_head.codes, &w.lm_head.high, &w.lm_head.group_scales, &r.xn, n, c.vocab_size, c.hidden_size); diff --git a/tests/round35.vx b/tests/round35.vx index 8598d5c..9cccac6 100644 --- a/tests/round35.vx +++ b/tests/round35.vx @@ -10,22 +10,24 @@ // `r` runs rounds of 6, 4, 2 and 5 tokens after a prompt, and keeps 3, 1, 2 and 5 of them with // `commit_round35`. State `b` decodes the kept tokens one at a time, and state `a`, a copy of `b` // before each round, decodes all the tokens of the round. The logits of each position of a round -// must be those of `a`, and the DeltaNet states after the commit those of `b`. Then a round of 8 -// tokens after a prompt of 2,044 tokens: its positions go past 2,048, where the attention of one -// position takes 64 SIMD groups for each head in place of 32. +// must be those of `a`, and the DeltaNet states after the commit those of `b`. Each round runs +// twice: with all the logits exact, and with the pick of the rows of lm_head (src/q6.vx), where +// the greedy tokens and their logits must be those of `a`. Then a round of 8 tokens after a +// prompt of 2,044 tokens: its positions go past 2,048, where the attention of one position takes +// 64 SIMD groups for each head in place of 32. // // RUN: python3 %S/../scripts/make_tiny_model.py --qwen35 --q4 --q6-lm-head --max-positions 2200 %t.vxw > /dev/null // RUN: vxc %s --package qwen=%S/../src --host default --machine $VX_FLEET/m4-uma.vx -- %t.vxw | FileCheck %s // -// CHECK: {{^}}round of 6: logits 0 differ, tokens 0 differ{{$}} +// CHECK: {{^}}round of 6: logits 0 differ, tokens 0 differ, picked 0 differ{{$}} // CHECK: {{^}}keep 3: states 0 differ{{$}} -// CHECK: {{^}}round of 4: logits 0 differ, tokens 0 differ{{$}} +// CHECK: {{^}}round of 4: logits 0 differ, tokens 0 differ, picked 0 differ{{$}} // CHECK: {{^}}keep 1: states 0 differ{{$}} -// CHECK: {{^}}round of 2: logits 0 differ, tokens 0 differ{{$}} +// CHECK: {{^}}round of 2: logits 0 differ, tokens 0 differ, picked 0 differ{{$}} // CHECK: {{^}}keep 2: states 0 differ{{$}} -// CHECK: {{^}}round of 5: logits 0 differ, tokens 0 differ{{$}} +// CHECK: {{^}}round of 5: logits 0 differ, tokens 0 differ, picked 0 differ{{$}} // CHECK: {{^}}keep 5: states 0 differ{{$}} -// CHECK: {{^}}round of 8: logits 0 differ, tokens 0 differ{{$}} +// CHECK: {{^}}round of 8: logits 0 differ, tokens 0 differ, picked 0 differ{{$}} import core::convert; import core::num; import std::nn; @@ -94,6 +96,7 @@ fn states_differ(w : &Qwen35Weights, a : &Qwen35State, b : &Qwen35State) -> i32 fn check_round(w : &Qwen35Weights, rs : &mut Qwen35State, round : &mut Round35, a : &mut Qwen35State, tokens : &Vec, pos : i32) -> void { let n = tokens.len(); let want = decode(w, a, tokens, pos); + round.pick = false; run_round35(w, rs, round, tokens, pos, n); let got = transfer(&round.logits, Memory::CPU_DRAM); let next = transfer(&round.next, Memory::CPU_DRAM); @@ -114,7 +117,24 @@ fn check_round(w : &Qwen35Weights, rs : &mut Qwen35State, round : &mut Round35, tokens_differ = tokens_differ + 1; } } - println!("round of {}: logits {} differ, tokens {} differ", n, differ, tokens_differ); + // The same round with the pick of the rows of lm_head: the greedy tokens and their logits. + round.pick = true; + run_round35(w, rs, round, tokens, pos, n); + let got2 = transfer(&round.logits, Memory::CPU_DRAM); + let next2 = transfer(&round.next, Memory::CPU_DRAM); + let mut picked_differ = 0; + for t in 0..n { + let mut best = 0; + for i in 0..vocab { + if want[t * vocab + i] > want[t * vocab + best] { + best = i; + } + } + if next2[t] != best || got2[t][best].to_bits() != want[t * vocab + best].to_bits() { + picked_differ = picked_differ + 1; + } + } + println!("round of {}: logits {} differ, tokens {} differ, picked {} differ", n, differ, tokens_differ, picked_differ); return; } From 4c02a06c9c5472edec5b011361a9271b3f60a258 Mon Sep 17 00:00:00 2001 From: Aditya <1894981+hiraditya@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:40:54 -0800 Subject: [PATCH 2/2] docs/optimizations.md: entry 18, the lm_head of a round The entry gives the problem and the change. The numbers come after a run that follows the benchmarking policy. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014U9gTxfQzAEpmg4WJhYQqw --- docs/optimizations.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/docs/optimizations.md b/docs/optimizations.md index 19b7efc..8d253f0 100644 --- a/docs/optimizations.md +++ b/docs/optimizations.md @@ -342,6 +342,33 @@ Each position keeps the bits of a plain step (`tests/round35.vx`, `tests/round.v **Result.** The numbers come after a run that follows the benchmarking policy. +### 18. The lm_head of a round: only the rows that can be the greedy token (#111) + +**Problem.** A round needs only the greedy token of each position and its logit. But it computed +all 248,320 logits of each position exactly, and the arithmetic of the 6-bit weights grows with +the number of positions. A profile by subtraction of a round cannot show the `lm_head`, because +without it nothing waits for the round. A separate bench showed the arithmetic, not the bytes, as +the limit of `matvec_q6_rows4` at 4 positions and more. + +**Change.** For 2 or more positions, `run_round35` runs three kernels (`src/q6.vx`): + +- `lm_rough_q6` computes rough logits a for up to 8 positions at once, with f16 weights and inputs + on the simdgroup matrices. +- `lm_pick_threshold` finds, for each position, M = the largest a - B over the rows. B is a bound + of |a - e|, where e is the exact logit. It comes from the norms of x and of the rows of + `lm_head`, and `src/q6.vx` gives each of its terms. `lm_q6_norms` computes the norms of the rows + in the first round. +- `matvec_q6_rows4_pick` computes the exact logit of each row that does not have a + B < M, bit for + bit as `matvec_q6` does. The other rows get the lowest f32, and cannot be the greedy token. A + block with no such row reads no weights. + +The true error is about 3% of B. In the rounds of a bench on the real model, at most 3 of the +248,320 rows of a position needed the exact logit. The greedy tokens and their logits are those +of the plain steps (`tests/round35.vx`). Only the other logits of a round change, and the app does +not read them. + +**Result.** The numbers come after a run that follows the benchmarking policy. + ## What did not help - **Other tile shapes for the Q4_0 products of a chunk, in Vx v0.0.4** (#19). Tiles of 32 tokens,