CPU expert kernels: IQ3_S single-token grid indices built in a register (like the Original's grid_indices), IQ4_NL down kernel with separate even/odd accumulators; bitwise equal to the Original on all 80 kernel_ab cells (laptop one-thread pool path: gate/up 1158 -> 430 us, down 282 -> 125 us)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-11-12 19:53:35 +01:00
co-authored by Claude Sonnet 5.5
parent 12826daba9
commit 0c48c823e2
3 changed files with 96 additions and 35 deletions
@@ -226,20 +226,31 @@ pub(super) unsafe fn iq4xs(row: &[u8], y: &[Q8K]) -> f32 {
hsum_ggml(af)
}
}
/// IQ4_NL rows specialised on the token count; the same operations in the same order per token.
/// IQ4_NL rows against `ys.len()` tokens (1..=8) of one tree; the same operations in the same order per token.
#[target_feature(enable = "avx2,fma,f16c")]
pub(in super::super) unsafe fn iq4nl(row: &[u8], ys: &[&[Q8Zero]], out: &mut [f32], single: bool) {
unsafe fn iq4nl_tokens<const SINGLE: bool>(row: &[u8], ys: &[&[Q8Zero]], out: &mut [f32]) {
unsafe {
match ys.len() {
1 => iq4nl_n::<1>(row, ys, out, single),
2 => iq4nl_n::<2>(row, ys, out, single),
3 => iq4nl_n::<3>(row, ys, out, single),
4 => iq4nl_n::<4>(row, ys, out, single),
5 => iq4nl_n::<5>(row, ys, out, single),
6 => iq4nl_n::<6>(row, ys, out, single),
7 => iq4nl_n::<7>(row, ys, out, single),
8 => iq4nl_n::<8>(row, ys, out, single),
1 => iq4nl_n::<1, SINGLE>(row, ys, out),
2 => iq4nl_n::<2, SINGLE>(row, ys, out),
3 => iq4nl_n::<3, SINGLE>(row, ys, out),
4 => iq4nl_n::<4, SINGLE>(row, ys, out),
5 => iq4nl_n::<5, SINGLE>(row, ys, out),
6 => iq4nl_n::<6, SINGLE>(row, ys, out),
7 => iq4nl_n::<7, SINGLE>(row, ys, out),
8 => iq4nl_n::<8, SINGLE>(row, ys, out),
_ => panic!("iq4nl: 1..=8 tokens"),
}
}
}
/// IQ4_NL rows specialised on the tree (`single`: the GGML single-token tree) and the token count.
#[target_feature(enable = "avx2,fma,f16c")]
pub(in super::super) unsafe fn iq4nl(row: &[u8], ys: &[&[Q8Zero]], out: &mut [f32], single: bool) {
unsafe {
if single {
iq4nl_tokens::<true>(row, ys, out)
} else {
iq4nl_tokens::<false>(row, ys, out)
}
}
}
+54 -22
View File
@@ -14,31 +14,63 @@ unsafe fn iq4nl_values(values: __m128i, m4: __m128i, b: &[u8]) -> __m256i {
)
}
}
/// One IQ4_NL block (index `ib` of the row) against the `N` tokens, accumulated into `acc` (one FMA per token).
#[inline]
#[target_feature(enable = "avx2,fma,f16c")]
pub(super) unsafe fn iq4nl_n<const N: usize>(row: &[u8], ys: &[&[Q8Zero]], out: &mut [f32], single: bool) {
unsafe fn iq4nl_block<const N: usize, const SINGLE: bool>(
(ib, b): (usize, &[u8]),
ys: &[&[Q8Zero]],
acc: &mut [__m256; N],
) {
unsafe {
assert!(N <= 8 && ys.len() == N && out.len() == N);
let values = _mm_loadu_si128(KVALUES_IQ4NL.as_ptr().cast());
let m4 = _mm_set1_epi8(15);
let ones = _mm256_set1_epi16(1);
let mut af = [[_mm256_setzero_ps(); 2]; 8];
for (ib, b) in row.chunks_exact(18).enumerate() {
prefetch(b.as_ptr());
let q = iq4nl_values(values, m4, b);
let aq = _mm256_sign_epi8(q, q);
let dx = half(u16(b, 0));
for t in 0..N {
let y = ys[t];
let b = &y[ib];
let v = _mm256_loadu_si256(b.qs.as_ptr().cast());
let p = _mm256_madd_epi16(_mm256_maddubs_epi16(aq, _mm256_sign_epi8(v, q)), ones);
let lane = if single { ib & 1 } else { 0 };
let d = if single { half(b.d) * dx } else { dx * half(b.d) };
af[t][lane] = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(p), af[t][lane]);
}
}
prefetch(b.as_ptr());
let q = iq4nl_values(_mm_loadu_si128(KVALUES_IQ4NL.as_ptr().cast()), _mm_set1_epi8(15), b);
let aq = _mm256_sign_epi8(q, q);
let dx = half(u16(b, 0));
for t in 0..N {
out[t] = if single { hsum_ggml(_mm256_add_ps(af[t][0], af[t][1])) } else { hsum_original(af[t][0]) };
let blk = &ys[t][ib];
let v = _mm256_loadu_si256(blk.qs.as_ptr().cast());
let p = _mm256_madd_epi16(_mm256_maddubs_epi16(aq, _mm256_sign_epi8(v, q)), _mm256_set1_epi16(1));
let d = if SINGLE { half(blk.d) * dx } else { dx * half(blk.d) };
acc[t] = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(p), acc[t]);
}
}
}
/// The GGML single-token order: even blocks into `even`, odd blocks into `odd` (a runtime lane index would keep the two
/// accumulators in memory and put a store forward on the FMA chain; separate register arrays do not).
#[target_feature(enable = "avx2,fma,f16c")]
unsafe fn iq4nl_alternating<const N: usize>(row: &[u8], ys: &[&[Q8Zero]], acc: &mut [[__m256; N]; 2]) {
unsafe {
let [even, odd] = acc;
let pairs = row.chunks_exact(36);
let tail = pairs.remainder();
let blocks = row.len() / 36;
for (k, pair) in pairs.enumerate() {
iq4nl_block::<N, true>((2 * k, &pair[..18]), ys, even);
iq4nl_block::<N, true>((2 * k + 1, &pair[18..]), ys, odd);
}
for b in tail.chunks_exact(18) {
iq4nl_block::<N, true>((2 * blocks, b), ys, even);
}
}
}
/// IQ4_NL row against `N` tokens. `SINGLE` is the GGML single-token tree (two alternating accumulators added once at
/// the end); otherwise one accumulator reduced the Original's way. Every block goes to the same accumulator in the same
/// order as the lane-indexed form, so the result is bit for bit the same.
#[target_feature(enable = "avx2,fma,f16c")]
pub(super) unsafe fn iq4nl_n<const N: usize, const SINGLE: bool>(row: &[u8], ys: &[&[Q8Zero]], out: &mut [f32]) {
unsafe {
assert!(N <= 8 && ys.len() == N && out.len() == N);
let mut acc = [[_mm256_setzero_ps(); N]; 2];
if SINGLE {
iq4nl_alternating::<N>(row, ys, &mut acc);
} else {
for (ib, b) in row.chunks_exact(18).enumerate() {
iq4nl_block::<N, false>((ib, b), ys, &mut acc[0]);
}
}
for t in 0..N {
out[t] = if SINGLE { hsum_ggml(_mm256_add_ps(acc[0][t], acc[1][t])) } else { hsum_original(acc[0][t]) };
}
}
}
+21 -3
View File
@@ -223,15 +223,14 @@ pub(super) fn iq3_multi_fast() -> bool {
}
/// One 32-weight half of an IQ3_S (21) / IQ3_XXS (18) block in a register: the eight grid words (lane l = word l,
/// `decode`'s bytes 4l..4l+4), the sign word `decode` returns and its (uniform) 16-bit scale.
#[inline]
#[target_feature(enable = "avx2,fma,f16c")]
pub(super) unsafe fn iq3_half<const T: i32>(b: &[u8], j: usize, h: usize) -> (__m256i, u32, i16) {
let q = &b[2 + 16 * j + 8 * h..2 + 16 * j + 8 * h + 8];
if T == 21 {
let high = b[66 + 2 * j + h] as usize;
let w = |l: usize| IQ3S_GRID[q[l] as usize | (((high >> l) & 1) << 8)] as i32;
let sv = b[106 + j];
(
_mm256_setr_epi32(w(0), w(1), w(2), w(3), w(4), w(5), w(6), w(7)),
iq3s_grid_words(q, b[66 + 2 * j + h]),
u32(b, 74 + 8 * j + 4 * h),
2 * (if h == 0 { sv & 15 } else { sv >> 4 }) as i16 + 1,
)
@@ -245,6 +244,25 @@ pub(super) unsafe fn iq3_half<const T: i32>(b: &[u8], j: usize, h: usize) -> (__
(_mm256_setr_epi32(w(0), w(1), w(2), w(3), w(4), w(5), w(6), w(7)), signs, 2 * (ws >> 28) as i16 + 1)
}
}
/// The eight IQ3_S grid words of a 32-weight half (`q`: its eight low index bytes, `high`: the qh byte whose bit `l`
/// is bit 8 of index `l`). The eight indices are built in one register (zero-extend, spread the bits of `high`, shift
/// to bit 8, or), stored once and read back as scalars, like iq_avx2.cpp `grid_indices` + `G3`; the words are the same
/// as the per-index shift/mask/or form, so the result is bit for bit the same.
#[inline]
#[target_feature(enable = "avx2,fma,f16c")]
fn iq3s_grid_words(q: &[u8], high: u8) -> __m256i {
unsafe {
assert!(q.len() >= 8);
let low = _mm256_cvtepu8_epi32(_mm_loadl_epi64(q.as_ptr().cast()));
let spread = _mm256_srlv_epi32(_mm256_set1_epi32(i32::from(high)), _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7));
let bit8 = _mm256_slli_epi32::<8>(_mm256_and_si256(spread, _mm256_set1_epi32(1)));
let mut ix = [0u32; 8];
_mm256_storeu_si256(ix.as_mut_ptr().cast(), _mm256_or_si256(low, bit8));
// SAFETY: every index is a byte (<= 255) or'ed with bit 8 (<= 511), and IQ3S_GRID has 512 entries.
let w = |l: usize| *IQ3S_GRID.get_unchecked(ix[l] as usize) as i32;
_mm256_setr_epi32(w(0), w(1), w(2), w(3), w(4), w(5), w(6), w(7))
}
}
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
pub(super) unsafe fn dot_multi512_tn<const T: i32, const N: usize>(row: &[u8], ys: &[&[Q8K]], out: &mut [f32]) {
unsafe { dot_multi512_impl::<T, N>(row, ys, out, iq3_multi_fast()) }