STRA-338 size-nce: rust_iq kernels and tests within the function limits
dot_multi512_impl (73 lines/cx17/n6) is split into sum_block512 / operands512 / register_operands512 / stack_operands512 / gather_grid512; dot_single_iq3 (48/n6) uses iq3_half and sum_block_iq3; iq4xs (35/n5) uses sum_block_iq4xs / iq4xs_scale; iq4nl_n (31) uses iq4nl_codes; decode (88/cx17) is a dispatch over decode_iq2xxs..decode_iq4xs; dot_multi256 and dot_multi512 take sum_block256 / dot_multi512_by_tokens; the four over-limit tests are split into arrange/act/assert helpers (test names and module paths unchanged). Wide lines re-wrapped. Functions over a limit in rust_iq/: 11 -> 0. Same arithmetic and accumulation order, target_feature kept on every vector helper. Bitwise old-vs-new proof with to_bits() on xorshift inputs (AVX2 and AVX-512 paths both ran): decode 144000 cases, multi256 1152, multi512 + impl(fast/stack) 2880, single (dot_single, generic, iq3, iq4xs) 2400, iq4nl 768, also under STRATA_IQ_GATHER, LELEKA_IQ_SINGLE=legacy, LELEKA_IQ_MULTI=register. Test-id list identical to the base. codegen not A/B-measured.
This commit is contained in:
@@ -47,4 +47,6 @@ pub(super) use dot_multi512_impl_fn::iq4nl;
|
||||
use dot_multi512_impl_fn::iq4xs;
|
||||
mod iq4nl_n_fn;
|
||||
#[allow(unused_imports)]
|
||||
use iq4nl_n_fn::iq4nl_codes;
|
||||
#[allow(unused_imports)]
|
||||
use iq4nl_n_fn::iq4nl_n;
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
//! Moved out of the parent module by STRA-338: fn dot_multi512_impl, fn iq3_single_fast, fn dot_single, fn dot_single_generic, fn dot_single_iq3, fn iq4xs ....
|
||||
//! Moved out of the parent module by STRA-338: fn dot_multi512_impl, fn iq3_single_fast, fn dot_single, ....
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
/// `fast`: the register-built IQ3 halves (bitwise the stack-array decode, `iq3_multi_trees_are_bitwise_equal`; slower on Zen 5, opt-in).
|
||||
/// `fast`: the register-built IQ3 halves (bitwise the stack-array decode, `iq3_multi_trees_are_bitwise_equal`; slower
|
||||
/// on Zen 5, opt-in).
|
||||
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
|
||||
pub(super) unsafe fn dot_multi512_impl<const T: i32, const N: usize>(
|
||||
row: &[u8],
|
||||
@@ -17,56 +18,10 @@ pub(super) unsafe fn dot_multi512_impl<const T: i32, const N: usize>(
|
||||
let nb = ys[0].len();
|
||||
assert_eq!(row.len(), nb * format_bytes(t));
|
||||
let mut af = [_mm512_setzero_ps(); 8];
|
||||
let zero = _mm512_setzero_si512();
|
||||
for i in 0..nb {
|
||||
let b = &row[i * format_bytes(t)..][..format_bytes(t)];
|
||||
prefetch(b.as_ptr());
|
||||
let mut ai = [_mm512_setzero_si512(); 8];
|
||||
for j in 0..4 {
|
||||
// IQ3 formats (`iq3_multi_fast`): both halves' grid words straight into the 512-bit register, lane order = `decode`'s byte order
|
||||
let reg = (T == 21 || T == 18) && fast && !super::super::rust_simd::dispatch().gather;
|
||||
let (gv, sv, m) = if reg {
|
||||
let (g0, s0, c0) = iq3_half::<T>(b, j, 0);
|
||||
let (g1, s1, c1) = iq3_half::<T>(b, j, 1);
|
||||
(
|
||||
_mm512_inserti64x4::<1>(_mm512_castsi256_si512(g0), g1),
|
||||
_mm512_inserti64x4::<1>(_mm512_castsi256_si512(_mm256_set1_epi16(c0)), _mm256_set1_epi16(c1)),
|
||||
s0 as u64 | ((s1 as u64) << 32),
|
||||
)
|
||||
} else {
|
||||
let (g0, s0, sc0) = decode(t, b, j, 0);
|
||||
let (g1, s1, sc1) = decode(t, b, j, 1);
|
||||
let mut g = [0u8; 64];
|
||||
g[..32].copy_from_slice(&g0);
|
||||
g[32..].copy_from_slice(&g1);
|
||||
let mut sc = [0i16; 32];
|
||||
sc[..16].copy_from_slice(&sc0);
|
||||
sc[16..].copy_from_slice(&sc1);
|
||||
let gv = if super::super::rust_simd::dispatch().gather && (t == 18 || t == 21) {
|
||||
let q = &b[2 + 16 * j..];
|
||||
let mut idx = _mm512_cvtepu8_epi32(_mm_loadu_si128(q.as_ptr().cast()));
|
||||
if t == 21 {
|
||||
let m = b[66 + 2 * j] as u16 | ((b[67 + 2 * j] as u16) << 8);
|
||||
idx = _mm512_mask_add_epi32(idx, m, idx, _mm512_set1_epi32(256));
|
||||
}
|
||||
_mm512_i32gather_epi32::<4>(
|
||||
idx,
|
||||
if t == 18 { IQ3XXS_GRID.as_ptr().cast() } else { IQ3S_GRID.as_ptr().cast() },
|
||||
)
|
||||
} else {
|
||||
_mm512_loadu_si512(g.as_ptr().cast())
|
||||
};
|
||||
let sv = _mm512_loadu_si512(sc.as_ptr().cast());
|
||||
let m = s0 as u64 | ((s1 as u64) << 32);
|
||||
(gv, sv, m)
|
||||
};
|
||||
for k in 0..N {
|
||||
let y = ys[k];
|
||||
let v = _mm512_loadu_si512(y[i].qs.as_ptr().add(64 * j).cast());
|
||||
let sy = _mm512_mask_sub_epi8(v, m, zero, v);
|
||||
ai[k] = _mm512_add_epi32(ai[k], _mm512_madd_epi16(_mm512_maddubs_epi16(gv, sy), sv));
|
||||
}
|
||||
}
|
||||
let ai = sum_block512::<T, N>(b, ys, i, fast);
|
||||
let dx = half(u16(b, 0)) * factor(t);
|
||||
for k in 0..N {
|
||||
let y = ys[k];
|
||||
@@ -78,6 +33,80 @@ pub(super) unsafe fn dot_multi512_impl<const T: i32, const N: usize>(
|
||||
}
|
||||
}
|
||||
}
|
||||
/// The exact i32 accumulators of one weight block `b` against block `i` of each of the `N` tokens' activation rows.
|
||||
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
|
||||
unsafe fn sum_block512<const T: i32, const N: usize>(b: &[u8], ys: &[&[Q8K]], i: usize, fast: bool) -> [__m512i; 8] {
|
||||
unsafe {
|
||||
let zero = _mm512_setzero_si512();
|
||||
let mut ai = [_mm512_setzero_si512(); 8];
|
||||
for j in 0..4 {
|
||||
let (gv, sv, m) = operands512::<T>(b, j, fast);
|
||||
for k in 0..N {
|
||||
let y = ys[k];
|
||||
let v = _mm512_loadu_si512(y[i].qs.as_ptr().add(64 * j).cast());
|
||||
let sy = _mm512_mask_sub_epi8(v, m, zero, v);
|
||||
ai[k] = _mm512_add_epi32(ai[k], _mm512_madd_epi16(_mm512_maddubs_epi16(gv, sy), sv));
|
||||
}
|
||||
}
|
||||
ai
|
||||
}
|
||||
}
|
||||
/// The 64 magnitude bytes, 32 16-bit scales and 64 sign bits of group `j` of block `b`.
|
||||
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
|
||||
unsafe fn operands512<const T: i32>(b: &[u8], j: usize, fast: bool) -> (__m512i, __m512i, u64) {
|
||||
unsafe {
|
||||
// IQ3 formats (`iq3_multi_fast`): both halves' grid words straight into the 512-bit register, lane order =
|
||||
// `decode`'s byte order
|
||||
let reg = (T == 21 || T == 18) && fast && !super::super::rust_simd::dispatch().gather;
|
||||
if reg { register_operands512::<T>(b, j) } else { stack_operands512(T, b, j) }
|
||||
}
|
||||
}
|
||||
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
|
||||
unsafe fn register_operands512<const T: i32>(b: &[u8], j: usize) -> (__m512i, __m512i, u64) {
|
||||
unsafe {
|
||||
let (g0, s0, c0) = iq3_half::<T>(b, j, 0);
|
||||
let (g1, s1, c1) = iq3_half::<T>(b, j, 1);
|
||||
(
|
||||
_mm512_inserti64x4::<1>(_mm512_castsi256_si512(g0), g1),
|
||||
_mm512_inserti64x4::<1>(_mm512_castsi256_si512(_mm256_set1_epi16(c0)), _mm256_set1_epi16(c1)),
|
||||
s0 as u64 | ((s1 as u64) << 32),
|
||||
)
|
||||
}
|
||||
}
|
||||
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
|
||||
unsafe fn stack_operands512(t: i32, b: &[u8], j: usize) -> (__m512i, __m512i, u64) {
|
||||
unsafe {
|
||||
let (g0, s0, sc0) = decode(t, b, j, 0);
|
||||
let (g1, s1, sc1) = decode(t, b, j, 1);
|
||||
let mut g = [0u8; 64];
|
||||
g[..32].copy_from_slice(&g0);
|
||||
g[32..].copy_from_slice(&g1);
|
||||
let mut sc = [0i16; 32];
|
||||
sc[..16].copy_from_slice(&sc0);
|
||||
sc[16..].copy_from_slice(&sc1);
|
||||
let gv = if super::super::rust_simd::dispatch().gather && (t == 18 || t == 21) {
|
||||
gather_grid512(t, b, j)
|
||||
} else {
|
||||
_mm512_loadu_si512(g.as_ptr().cast())
|
||||
};
|
||||
let sv = _mm512_loadu_si512(sc.as_ptr().cast());
|
||||
let m = s0 as u64 | ((s1 as u64) << 32);
|
||||
(gv, sv, m)
|
||||
}
|
||||
}
|
||||
/// The 64 grid bytes of an IQ3_XXS (18) / IQ3_S (21) group straight from the grid table (`idx` <= 255 / <= 511).
|
||||
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
|
||||
unsafe fn gather_grid512(t: i32, b: &[u8], j: usize) -> __m512i {
|
||||
unsafe {
|
||||
let q = &b[2 + 16 * j..];
|
||||
let mut idx = _mm512_cvtepu8_epi32(_mm_loadu_si128(q.as_ptr().cast()));
|
||||
if t == 21 {
|
||||
let m = b[66 + 2 * j] as u16 | ((b[67 + 2 * j] as u16) << 8);
|
||||
idx = _mm512_mask_add_epi32(idx, m, idx, _mm512_set1_epi32(256));
|
||||
}
|
||||
_mm512_i32gather_epi32::<4>(idx, if t == 18 { IQ3XXS_GRID.as_ptr().cast() } else { IQ3S_GRID.as_ptr().cast() })
|
||||
}
|
||||
}
|
||||
/// `LELEKA_IQ_SINGLE=legacy` keeps the generic single-token tree for IQ3_S / IQ3_XXS (A/B arm); the default is the
|
||||
/// register-built tree below (`dot_single_iq3`), the same integer lanes and float operations, so the same bits.
|
||||
pub(super) fn iq3_single_fast() -> bool {
|
||||
@@ -119,60 +148,43 @@ pub(super) unsafe fn dot_single_generic(t: i32, row: &[u8], y: &[Q8K]) -> f32 {
|
||||
}
|
||||
}
|
||||
/// IQ3_S (21) / IQ3_XXS (18), one token: `dot_single_generic`'s tree with the 32 magnitude bytes of a half built in a
|
||||
/// register (`_mm256_setr_epi32` of the eight grid words: lane l = grid word l, the bytes `decode` writes at 4l..4l+4),
|
||||
/// the same sign mask (`signs32`), the same per-half 16-bit scale broadcast, the same two exact i32 accumulators and the
|
||||
/// same per-block FMA and final reduction. Only where the bytes live between decode and use changes (no stack round trip,
|
||||
/// no per-half `match`), so every lane, and the result, is bit for bit the generic one's (`iq3_single_trees_are_bitwise_equal`).
|
||||
/// Bounds: `row` is walked by `chunks_exact(format_bytes(T))`, every offset below is inside a 110/98-byte block, `y[i]` is checked.
|
||||
/// register (`iq3_half`: `_mm256_setr_epi32` of the eight grid words, lane l = grid word l, the bytes `decode` writes
|
||||
/// at 4l..4l+4), the same sign mask (`signs32`), the same per-half 16-bit scale broadcast, the same two exact i32
|
||||
/// accumulators and the same per-block FMA and final reduction. Only where the bytes live between decode and use
|
||||
/// changes (no stack round trip, no per-half `match`), so every lane, and the result, is bit for bit the generic
|
||||
/// one's (`iq3_single_trees_are_bitwise_equal`). Bounds: `row` is walked by `chunks_exact(format_bytes(T))`, every
|
||||
/// offset below is inside a 110/98-byte block, `y[i]` is checked.
|
||||
#[target_feature(enable = "avx2,fma,f16c")]
|
||||
pub(super) unsafe fn dot_single_iq3<const T: i32>(row: &[u8], y: &[Q8K]) -> f32 {
|
||||
unsafe {
|
||||
let mut af = _mm256_setzero_ps();
|
||||
for (i, b) in row.chunks_exact(format_bytes(T)).enumerate() {
|
||||
let yi = &y[i];
|
||||
let mut a = [_mm256_setzero_si256(); 2];
|
||||
for j in 0..4 {
|
||||
for h in 0..2 {
|
||||
let q = &b[2 + 16 * j + 8 * h..2 + 16 * j + 8 * h + 8];
|
||||
let (g, s, sc) = 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];
|
||||
let s0 = 2 * (if h == 0 { sv & 15 } else { sv >> 4 }) as i16 + 1;
|
||||
(
|
||||
_mm256_setr_epi32(w(0), w(1), w(2), w(3), w(4), w(5), w(6), w(7)),
|
||||
u32(b, 74 + 8 * j + 4 * h),
|
||||
s0,
|
||||
)
|
||||
} else {
|
||||
let w = |l: usize| IQ3XXS_GRID[q[l] as usize] as i32;
|
||||
let ws = u32(b, 66 + 8 * j + 4 * h);
|
||||
let mut signs = 0u32;
|
||||
for l in 0..4 {
|
||||
signs |= (KSIGNS_IQ2XS[((ws >> (7 * l)) & 127) as usize] as u32) << (8 * l);
|
||||
}
|
||||
(
|
||||
_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,
|
||||
)
|
||||
};
|
||||
let v = _mm256_loadu_si256(yi.qs.as_ptr().add(64 * j + 32 * h).cast());
|
||||
a[h] = _mm256_add_epi32(
|
||||
a[h],
|
||||
_mm256_madd_epi16(
|
||||
_mm256_maddubs_epi16(g, _mm256_sign_epi8(v, signs32(s))),
|
||||
_mm256_set1_epi16(sc),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
let a = sum_block_iq3::<T>(b, yi);
|
||||
let d = half(u16(b, 0)) * yi.d;
|
||||
af = _mm256_fmadd_ps(_mm256_set1_ps(d), _mm256_cvtepi32_ps(_mm256_add_epi32(a[0], a[1])), af);
|
||||
}
|
||||
factor(T) * hsum_ggml(af)
|
||||
}
|
||||
}
|
||||
/// The two exact i32 accumulators (one per half) of one IQ3 weight block `b` against the activation block `yi`.
|
||||
#[target_feature(enable = "avx2,fma,f16c")]
|
||||
unsafe fn sum_block_iq3<const T: i32>(b: &[u8], yi: &Q8K) -> [__m256i; 2] {
|
||||
unsafe {
|
||||
let mut a = [_mm256_setzero_si256(); 2];
|
||||
for j in 0..4 {
|
||||
for (h, ah) in a.iter_mut().enumerate() {
|
||||
let (g, s, sc) = iq3_half::<T>(b, j, h);
|
||||
let v = _mm256_loadu_si256(yi.qs.as_ptr().add(64 * j + 32 * h).cast());
|
||||
*ah = _mm256_add_epi32(
|
||||
*ah,
|
||||
_mm256_madd_epi16(_mm256_maddubs_epi16(g, _mm256_sign_epi8(v, signs32(s))), _mm256_set1_epi16(sc)),
|
||||
);
|
||||
}
|
||||
}
|
||||
a
|
||||
}
|
||||
}
|
||||
#[target_feature(enable = "avx2,fma,f16c")]
|
||||
pub(super) unsafe fn iq4xs(row: &[u8], y: &[Q8K]) -> f32 {
|
||||
unsafe {
|
||||
@@ -180,26 +192,7 @@ pub(super) unsafe fn iq4xs(row: &[u8], y: &[Q8K]) -> f32 {
|
||||
let m4 = _mm_set1_epi8(15);
|
||||
let mut af = _mm256_setzero_ps();
|
||||
for (i, b) in row.chunks_exact(136).enumerate() {
|
||||
let mut sh = u16(b, 2);
|
||||
let mut sums = [_mm256_setzero_si256(); 2];
|
||||
for ib in (0..8).step_by(2) {
|
||||
for h in 0..2 {
|
||||
let bits = _mm_loadu_si128(b.as_ptr().add(8 + 16 * (ib + h)).cast());
|
||||
let q = _mm256_inserti128_si256::<1>(
|
||||
_mm256_castsi128_si256(_mm_shuffle_epi8(values, _mm_and_si128(bits, m4))),
|
||||
_mm_shuffle_epi8(values, _mm_and_si128(_mm_srli_epi16::<4>(bits), m4)),
|
||||
);
|
||||
let v = _mm256_loadu_si256(y[i].qs.as_ptr().add(32 * (ib + h)).cast());
|
||||
let p = _mm256_maddubs_epi16(_mm256_sign_epi8(q, q), _mm256_sign_epi8(v, q));
|
||||
let ls = if h == 0 {
|
||||
((b[4 + ib / 2] & 15) as u16 | ((sh << 4) & 48)) as i16 - 32
|
||||
} else {
|
||||
((b[4 + ib / 2] >> 4) as u16 | ((sh << 2) & 48)) as i16 - 32
|
||||
};
|
||||
sums[h] = _mm256_add_epi32(_mm256_madd_epi16(p, _mm256_set1_epi16(ls)), sums[h]);
|
||||
}
|
||||
sh >>= 4;
|
||||
}
|
||||
let sums = sum_block_iq4xs(values, m4, b, &y[i]);
|
||||
af = _mm256_fmadd_ps(
|
||||
_mm256_set1_ps(half(u16(b, 0)) * y[i].d),
|
||||
_mm256_cvtepi32_ps(_mm256_add_epi32(sums[0], sums[1])),
|
||||
@@ -209,6 +202,34 @@ pub(super) unsafe fn iq4xs(row: &[u8], y: &[Q8K]) -> f32 {
|
||||
hsum_ggml(af)
|
||||
}
|
||||
}
|
||||
/// The two exact i32 accumulators (one per half) of one IQ4_XS weight block `b` against the activation block `yi`.
|
||||
#[target_feature(enable = "avx2,fma,f16c")]
|
||||
unsafe fn sum_block_iq4xs(values: __m128i, m4: __m128i, b: &[u8], yi: &Q8K) -> [__m256i; 2] {
|
||||
unsafe {
|
||||
let mut sh = u16(b, 2);
|
||||
let mut sums = [_mm256_setzero_si256(); 2];
|
||||
for ib in (0..8).step_by(2) {
|
||||
for h in 0..2 {
|
||||
let bits = _mm_loadu_si128(b.as_ptr().add(8 + 16 * (ib + h)).cast());
|
||||
let q = iq4nl_codes(values, m4, bits);
|
||||
let v = _mm256_loadu_si256(yi.qs.as_ptr().add(32 * (ib + h)).cast());
|
||||
let p = _mm256_maddubs_epi16(_mm256_sign_epi8(q, q), _mm256_sign_epi8(v, q));
|
||||
let ls = iq4xs_scale(b, sh, ib, h);
|
||||
sums[h] = _mm256_add_epi32(_mm256_madd_epi16(p, _mm256_set1_epi16(ls)), sums[h]);
|
||||
}
|
||||
sh >>= 4;
|
||||
}
|
||||
sums
|
||||
}
|
||||
}
|
||||
/// The signed 6-bit scale (ls - 32) of sub-block `ib + h`; `sh` is the high-bit word shifted by 4 per `ib` pair.
|
||||
fn iq4xs_scale(b: &[u8], sh: u16, ib: usize, h: usize) -> i16 {
|
||||
if h == 0 {
|
||||
((b[4 + ib / 2] & 15) as u16 | ((sh << 4) & 48)) as i16 - 32
|
||||
} else {
|
||||
((b[4 + ib / 2] >> 4) as u16 | ((sh << 2) & 48)) as i16 - 32
|
||||
}
|
||||
}
|
||||
/// IQ4_NL rows specialised on the token count; 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) {
|
||||
|
||||
@@ -3,6 +3,16 @@
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
/// The 32 signed IQ4_NL codebook values of 32 nibbles: the low nibbles of `bits` in the low 128-bit lane, the high
|
||||
/// nibbles in the high one.
|
||||
#[target_feature(enable = "avx2,fma,f16c")]
|
||||
pub(super) unsafe fn iq4nl_codes(values: __m128i, m4: __m128i, bits: __m128i) -> __m256i {
|
||||
// Register-only intrinsics under the same target features: no unsafe operation inside.
|
||||
_mm256_inserti128_si256::<1>(
|
||||
_mm256_castsi128_si256(_mm_shuffle_epi8(values, _mm_and_si128(bits, m4))),
|
||||
_mm_shuffle_epi8(values, _mm_and_si128(_mm_srli_epi16::<4>(bits), m4)),
|
||||
)
|
||||
}
|
||||
#[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 {
|
||||
@@ -14,10 +24,7 @@ pub(super) unsafe fn iq4nl_n<const N: usize>(row: &[u8], ys: &[&[Q8Zero]], out:
|
||||
for (ib, b) in row.chunks_exact(18).enumerate() {
|
||||
prefetch(b.as_ptr());
|
||||
let bits = _mm_loadu_si128(b.as_ptr().add(2).cast());
|
||||
let q = _mm256_inserti128_si256::<1>(
|
||||
_mm256_castsi128_si256(_mm_shuffle_epi8(values, _mm_and_si128(bits, m4))),
|
||||
_mm_shuffle_epi8(values, _mm_and_si128(_mm_srli_epi16::<4>(bits), m4)),
|
||||
);
|
||||
let q = iq4nl_codes(values, m4, bits);
|
||||
let aq = _mm256_sign_epi8(q, q);
|
||||
let dx = half(u16(b, 0));
|
||||
for t in 0..N {
|
||||
|
||||
@@ -13,8 +13,9 @@ fn h2f(h: u16) -> f32 {
|
||||
assert!(e > 0 && e < 31, "finite normal FP16 only");
|
||||
f32::from_bits(((h as u32 & 0x8000) << 16) | ((e + 112) << 23) | ((h as u32 & 0x3ff) << 13))
|
||||
}
|
||||
// Scalar model of iq_avx2.cpp row_dot<23,NT> (non-EX): per block one int32 accumulator per lane, sum over the 8 sub-blocks of
|
||||
// (ls-32) * (4 consecutive signed-codebook x activation products), then ONE fma per block and lane, then the original hsum.
|
||||
// Scalar model of iq_avx2.cpp row_dot<23,NT> (non-EX): per block one int32 accumulator per lane, sum over the 8
|
||||
// sub-blocks of (ls-32) * (4 consecutive signed-codebook x activation products), then ONE fma per block and lane,
|
||||
// then the original hsum.
|
||||
fn model_iq4xs(row: &[u8], y: &[Q8K]) -> f32 {
|
||||
let mut acc = [0.0f32; 8];
|
||||
for (i, b) in row.chunks_exact(136).enumerate() {
|
||||
@@ -63,6 +64,22 @@ fn random_act(nb: usize, scale: f32, seed: &mut u64) -> Vec<Q8K> {
|
||||
q8_k(&x, &mut y);
|
||||
y
|
||||
}
|
||||
// One round of the single-token comparison: every row of `rows` random blocks (all grid indices, high bits, sign
|
||||
// words and scales) of format `t` against activations of one magnitude, bit for bit.
|
||||
fn assert_iq3_single_round_equal(t: i32, nb: usize, rows: usize, round: usize, seed: &mut u64) {
|
||||
let w = random_rows(t, nb, rows, seed);
|
||||
let y = random_act(nb, 0.0001 + round as f32 * 0.37, seed);
|
||||
for r in w.chunks_exact(nb * format_bytes(t)) {
|
||||
// SAFETY: the caller checked avx2_ok(); every row is nb whole blocks and y has nb blocks.
|
||||
let (a, b) = unsafe {
|
||||
(
|
||||
dot_single_generic(t, r, &y),
|
||||
if t == 21 { dot_single_iq3::<21>(r, &y) } else { dot_single_iq3::<18>(r, &y) },
|
||||
)
|
||||
};
|
||||
assert_eq!(a.to_bits(), b.to_bits(), "type {t} round {round}");
|
||||
}
|
||||
}
|
||||
// The register-built IQ3 single-token tree against the generic one: every row of random blocks (all grid indices, high
|
||||
// bits, sign words and scales) and activations of several magnitudes, compared bit for bit.
|
||||
#[test]
|
||||
@@ -75,24 +92,83 @@ fn iq3_single_trees_are_bitwise_equal() {
|
||||
let nb = 10usize;
|
||||
for t in [21, 18] {
|
||||
for round in 0..8 {
|
||||
let rows = 64;
|
||||
let w = random_rows(t, nb, rows, &mut seed);
|
||||
let y = random_act(nb, 0.0001 + round as f32 * 0.37, &mut seed);
|
||||
for r in w.chunks_exact(nb * format_bytes(t)) {
|
||||
// SAFETY: avx2_ok() above; every row is nb whole blocks and y has nb blocks.
|
||||
let (a, b) = unsafe {
|
||||
(
|
||||
dot_single_generic(t, r, &y),
|
||||
if t == 21 { dot_single_iq3::<21>(r, &y) } else { dot_single_iq3::<18>(r, &y) },
|
||||
)
|
||||
};
|
||||
assert_eq!(a.to_bits(), b.to_bits(), "type {t} round {round}");
|
||||
}
|
||||
assert_iq3_single_round_equal(t, nb, 64, round, &mut seed);
|
||||
}
|
||||
}
|
||||
}
|
||||
// The weights and activation of the throughput benchmark: 48 distinct IQ3_S experts of gate+up rows, one token.
|
||||
struct Bench {
|
||||
t: i32,
|
||||
nb: usize,
|
||||
experts: usize,
|
||||
w: Vec<u8>,
|
||||
y: Vec<Q8K>,
|
||||
}
|
||||
impl Bench {
|
||||
fn row_bytes(&self) -> usize {
|
||||
self.nb * format_bytes(self.t)
|
||||
}
|
||||
}
|
||||
// Generic against register-built single-token trees.
|
||||
fn bench_single_trees(b: &Bench, pass: usize) {
|
||||
for (name, fast) in [("generic", false), ("iq3", true)] {
|
||||
let start = std::time::Instant::now();
|
||||
let mut s = 0.0f32;
|
||||
for r in b.w.chunks_exact(b.row_bytes()) {
|
||||
// SAFETY: the caller checked avx2_ok(); rows are whole blocks and y has nb blocks.
|
||||
s += unsafe { if fast { dot_single_iq3::<21>(r, &b.y) } else { dot_single_generic(b.t, r, &b.y) } };
|
||||
}
|
||||
let el = start.elapsed().as_secs_f64();
|
||||
eprintln!(
|
||||
"pass {pass} {name}: {:.1} us per expert gate+up, {:.2} GB/s (sum {s})",
|
||||
el * 1e6 / b.experts as f64,
|
||||
b.w.len() as f64 / el / 1e9
|
||||
);
|
||||
}
|
||||
}
|
||||
// Two-token AVX-512 kernel with the stack-array and the register-built halves.
|
||||
fn bench_multi512(b: &Bench, pass: usize, seed: &mut u64) {
|
||||
let y2 = random_act(b.nb, 0.01, seed);
|
||||
let ys = [b.y.as_slice(), y2.as_slice()];
|
||||
for (name, fast) in [("stack", false), ("register", true)] {
|
||||
let start = std::time::Instant::now();
|
||||
let mut s = 0.0f32;
|
||||
for r in b.w.chunks_exact(b.row_bytes()) {
|
||||
let mut o = [0.0f32; 2];
|
||||
// SAFETY: the caller checked avx512_ok() and dq_ok(); rows are whole blocks and both y have nb blocks.
|
||||
unsafe {
|
||||
dot_multi512_impl::<21, 2>(r, &ys, &mut o, fast);
|
||||
}
|
||||
s += o[0];
|
||||
}
|
||||
let el = start.elapsed().as_secs_f64();
|
||||
eprintln!("pass {pass} multi512 x2 {name}: {:.1} us per expert gate+up (sum {s})", el * 1e6 / b.experts as f64);
|
||||
}
|
||||
}
|
||||
// The down rows of the same experts: H=2560 rows of IQ4_NL over FF=640 (20 blocks of 18 bytes), one token.
|
||||
fn bench_iq4nl_down(experts: usize, pass: usize, seed: &mut u64) {
|
||||
let dn = random_iq4nl(2560 * experts, 20, seed);
|
||||
let hq = random_q8zero(20, seed);
|
||||
let start = std::time::Instant::now();
|
||||
let mut s = 0.0f32;
|
||||
for r in dn.chunks_exact(20 * 18) {
|
||||
let mut o = [0.0f32];
|
||||
// SAFETY: the caller checked avx2_ok(); rows are 20 whole blocks and hq has 20 blocks.
|
||||
unsafe {
|
||||
iq4nl(r, &[hq.as_slice()], &mut o, true);
|
||||
}
|
||||
s += o[0];
|
||||
}
|
||||
let el = start.elapsed().as_secs_f64();
|
||||
eprintln!(
|
||||
"pass {pass} iq4nl down x1: {:.1} us per expert down, {:.2} GB/s (sum {s})",
|
||||
el * 1e6 / experts as f64,
|
||||
dn.len() as f64 / el / 1e9
|
||||
);
|
||||
}
|
||||
// Throughput of one token's gate+up rows (FF=640 rows x 2 matrices of H=2560) over 48 distinct experts (past the L3),
|
||||
// generic against register-built, one thread. `cargo test --release -p leleka-native-cpu-expert iq3_single_bench -- --ignored --nocapture`
|
||||
// generic against register-built, one thread.
|
||||
// `cargo test --release -p leleka-native-cpu-expert iq3_single_bench -- --ignored --nocapture`
|
||||
#[test]
|
||||
#[ignore = "benchmark"]
|
||||
fn iq3_single_bench() {
|
||||
@@ -101,77 +177,16 @@ fn iq3_single_bench() {
|
||||
return;
|
||||
}
|
||||
let mut seed = 42u64;
|
||||
let nb = 10usize;
|
||||
let t = 21;
|
||||
let experts = 48usize;
|
||||
let rows = 1280usize;
|
||||
let (nb, t, experts, rows) = (10usize, 21, 48usize, 1280usize);
|
||||
let w = random_rows(t, nb, rows * experts, &mut seed);
|
||||
let y = random_act(nb, 0.01, &mut seed);
|
||||
let rb = nb * format_bytes(t);
|
||||
let bench = Bench { t, nb, experts, w, y };
|
||||
for pass in 0..3 {
|
||||
for (name, fast) in [("generic", false), ("iq3", true)] {
|
||||
let start = std::time::Instant::now();
|
||||
let mut s = 0.0f32;
|
||||
for r in w.chunks_exact(rb) {
|
||||
s += unsafe { if fast { dot_single_iq3::<21>(r, &y) } else { dot_single_generic(t, r, &y) } };
|
||||
}
|
||||
let el = start.elapsed().as_secs_f64();
|
||||
eprintln!(
|
||||
"pass {pass} {name}: {:.1} us per expert gate+up, {:.2} GB/s (sum {s})",
|
||||
el * 1e6 / experts as f64,
|
||||
w.len() as f64 / el / 1e9
|
||||
);
|
||||
}
|
||||
bench_single_trees(&bench, pass);
|
||||
if super::super::rust_simd::avx512_ok() && super::super::rust_simd::dq_ok() {
|
||||
let y2 = random_act(nb, 0.01, &mut seed);
|
||||
let ys = [y.as_slice(), y2.as_slice()];
|
||||
let start = std::time::Instant::now();
|
||||
let mut s = 0.0f32;
|
||||
for r in w.chunks_exact(rb) {
|
||||
let mut o = [0.0f32; 2];
|
||||
unsafe {
|
||||
dot_multi512_impl::<21, 2>(r, &ys, &mut o, false);
|
||||
}
|
||||
s += o[0];
|
||||
}
|
||||
let el = start.elapsed().as_secs_f64();
|
||||
eprintln!(
|
||||
"pass {pass} multi512 x2 stack: {:.1} us per expert gate+up (sum {s})",
|
||||
el * 1e6 / experts as f64
|
||||
);
|
||||
let start = std::time::Instant::now();
|
||||
let mut s = 0.0f32;
|
||||
for r in w.chunks_exact(rb) {
|
||||
let mut o = [0.0f32; 2];
|
||||
unsafe {
|
||||
dot_multi512_impl::<21, 2>(r, &ys, &mut o, true);
|
||||
}
|
||||
s += o[0];
|
||||
}
|
||||
let el = start.elapsed().as_secs_f64();
|
||||
eprintln!(
|
||||
"pass {pass} multi512 x2 register: {:.1} us per expert gate+up (sum {s})",
|
||||
el * 1e6 / experts as f64
|
||||
);
|
||||
bench_multi512(&bench, pass, &mut seed);
|
||||
}
|
||||
// the down rows of the same experts: H=2560 rows of IQ4_NL over FF=640 (20 blocks of 18 bytes), one token
|
||||
let dn = random_iq4nl(2560 * experts, 20, &mut seed);
|
||||
let hq = random_q8zero(20, &mut seed);
|
||||
let start = std::time::Instant::now();
|
||||
let mut s = 0.0f32;
|
||||
for r in dn.chunks_exact(20 * 18) {
|
||||
let mut o = [0.0f32];
|
||||
unsafe {
|
||||
iq4nl(r, &[hq.as_slice()], &mut o, true);
|
||||
}
|
||||
s += o[0];
|
||||
}
|
||||
let el = start.elapsed().as_secs_f64();
|
||||
eprintln!(
|
||||
"pass {pass} iq4nl down x1: {:.1} us per expert down, {:.2} GB/s (sum {s})",
|
||||
el * 1e6 / experts as f64,
|
||||
dn.len() as f64 / el / 1e9
|
||||
);
|
||||
bench_iq4nl_down(experts, pass, &mut seed);
|
||||
}
|
||||
}
|
||||
fn random_iq4nl(rows: usize, nb: usize, seed: &mut u64) -> Vec<u8> {
|
||||
@@ -196,6 +211,28 @@ fn random_q8zero(nb: usize, seed: &mut u64) -> Vec<Q8Zero> {
|
||||
}
|
||||
v
|
||||
}
|
||||
// The stack-array and the register-built multi-token trees on every row of `w`, for `N` tokens, bit for bit.
|
||||
fn assert_multi512_trees_equal<const T: i32, const N: usize>(w: &[u8], ys: &[&[Q8K]], nb: usize) {
|
||||
let mut a = [0.0f32; N];
|
||||
let mut b = [0.0f32; N];
|
||||
for r in w.chunks_exact(nb * format_bytes(T)) {
|
||||
// SAFETY: the caller checked avx512_ok() and dq_ok(); rows are nb whole blocks and every y has nb blocks.
|
||||
unsafe {
|
||||
dot_multi512_impl::<T, N>(r, &ys[..N], &mut a, false);
|
||||
dot_multi512_impl::<T, N>(r, &ys[..N], &mut b, true);
|
||||
}
|
||||
for k in 0..N {
|
||||
assert_eq!(a[k].to_bits(), b[k].to_bits(), "type {} tokens {} token {k}", T, N);
|
||||
}
|
||||
}
|
||||
}
|
||||
// The same comparison for one to four tokens.
|
||||
fn assert_multi512_trees_equal_up_to_four_tokens<const T: i32>(w: &[u8], ys: &[&[Q8K]], nb: usize) {
|
||||
assert_multi512_trees_equal::<T, 1>(w, ys, nb);
|
||||
assert_multi512_trees_equal::<T, 2>(w, ys, nb);
|
||||
assert_multi512_trees_equal::<T, 3>(w, ys, nb);
|
||||
assert_multi512_trees_equal::<T, 4>(w, ys, nb);
|
||||
}
|
||||
#[test]
|
||||
fn iq3_multi_trees_are_bitwise_equal() {
|
||||
if !(super::super::rust_simd::avx512_ok() && super::super::rust_simd::dq_ok()) {
|
||||
@@ -204,40 +241,56 @@ fn iq3_multi_trees_are_bitwise_equal() {
|
||||
}
|
||||
let mut seed = 0x3e3e_5eed_7777_0001u64;
|
||||
let nb = 10usize;
|
||||
macro_rules! cmp {
|
||||
($T:literal,$N:literal,$w:expr,$ys:expr) => {{
|
||||
let mut a = [0.0f32; $N];
|
||||
let mut b = [0.0f32; $N];
|
||||
for r in $w.chunks_exact(nb * format_bytes($T)) {
|
||||
unsafe {
|
||||
dot_multi512_impl::<$T, $N>(r, &$ys[..$N], &mut a, false);
|
||||
dot_multi512_impl::<$T, $N>(r, &$ys[..$N], &mut b, true);
|
||||
}
|
||||
for k in 0..$N {
|
||||
assert_eq!(a[k].to_bits(), b[k].to_bits(), "type {} tokens {} token {k}", $T, $N);
|
||||
}
|
||||
}
|
||||
}};
|
||||
}
|
||||
for round in 0..6 {
|
||||
let acts: Vec<Vec<Q8K>> =
|
||||
(0..4).map(|k| random_act(nb, 0.0003 + (round * 4 + k) as f32 * 0.21, &mut seed)).collect();
|
||||
let ys: Vec<&[Q8K]> = acts.iter().map(|y| y.as_slice()).collect();
|
||||
let w21 = random_rows(21, nb, 32, &mut seed);
|
||||
let w18 = random_rows(18, nb, 32, &mut seed);
|
||||
cmp!(21, 1, w21, ys);
|
||||
cmp!(21, 2, w21, ys);
|
||||
cmp!(21, 3, w21, ys);
|
||||
cmp!(21, 4, w21, ys);
|
||||
cmp!(18, 1, w18, ys);
|
||||
cmp!(18, 2, w18, ys);
|
||||
cmp!(18, 3, w18, ys);
|
||||
cmp!(18, 4, w18, ys);
|
||||
assert_multi512_trees_equal_up_to_four_tokens::<21>(&w21, &ys, nb);
|
||||
assert_multi512_trees_equal_up_to_four_tokens::<18>(&w18, &ys, nb);
|
||||
}
|
||||
}
|
||||
// IQ4_XS gate/up rows at two or more tokens on the AVX2 tree take the multi-token kernel (iq_avx2.cpp iq256_gu_rows, native_expert.cpp
|
||||
// `!cpu512 && iq256_supported(23)`), not ggml's single-token dot: its last bits differ, and a Q2_0 down projection (not requantized
|
||||
// to int8 as IQ4_NL's is) exposed it in KAB1 (STRA-324: cases 23-42-r2/r3/r8).
|
||||
// A row of `nb` random IQ4_XS blocks with a finite normal FP16 block scale.
|
||||
fn random_iq4xs_row(nb: usize, seed: &mut u64) -> Vec<u8> {
|
||||
let mut row = vec![0u8; nb * 136];
|
||||
for v in row.iter_mut() {
|
||||
*v = xorshift(seed) as u8;
|
||||
}
|
||||
for b in row.chunks_exact_mut(136) {
|
||||
let e = 8 + (xorshift(seed) % 8) as u16;
|
||||
let m = (xorshift(seed) & 0x3ff) as u16;
|
||||
b[..2].copy_from_slice(&((e << 10) | m).to_le_bytes());
|
||||
}
|
||||
row
|
||||
}
|
||||
// Eight activation rows of `nb` Q8K blocks, the magnitude growing with `round`.
|
||||
fn random_iq4xs_acts(round: usize, nb: usize, seed: &mut u64) -> Vec<Vec<Q8K>> {
|
||||
let mut ys: Vec<Vec<Q8K>> = Vec::new();
|
||||
for _ in 0..8 {
|
||||
let x: Vec<f32> =
|
||||
(0..nb * 256).map(|_| ((xorshift(seed) % 20001) as f32 - 10000.0) * (0.001 + (round as f32))).collect();
|
||||
let mut y = vec![Q8K::default(); nb];
|
||||
q8_k(&x, &mut y);
|
||||
ys.push(y);
|
||||
}
|
||||
ys
|
||||
}
|
||||
// The AVX2 multi-token kernel of `nt` tokens against the scalar model, bit for bit.
|
||||
fn assert_iq4xs_kernel_matches_model(round: usize, row: &[u8], ys: &[Vec<Q8K>], nt: usize) {
|
||||
let refs: Vec<&[Q8K]> = ys[..nt].iter().map(|y| y.as_slice()).collect();
|
||||
let mut out = vec![0.0f32; nt];
|
||||
// SAFETY: the caller checked avx2_ok(); row is whole 136-byte blocks and every Q8K row has as many blocks.
|
||||
unsafe {
|
||||
dot_multi256(23, row, &refs, &mut out);
|
||||
}
|
||||
for t in 0..nt {
|
||||
assert_eq!(out[t].to_bits(), model_iq4xs(row, &ys[t]).to_bits(), "round {round} nt {nt} token {t}");
|
||||
}
|
||||
}
|
||||
// IQ4_XS gate/up rows at two or more tokens on the AVX2 tree take the multi-token kernel (iq_avx2.cpp iq256_gu_rows,
|
||||
// native_expert.cpp `!cpu512 && iq256_supported(23)`), not ggml's single-token dot: its last bits differ, and a Q2_0
|
||||
// down projection (not requantized to int8 as IQ4_NL's is) exposed it in KAB1 (STRA-324: cases 23-42-r2/r3/r8).
|
||||
#[test]
|
||||
fn iq4xs_multi_token_avx2_kernel_matches_the_scalar_model_bitwise() {
|
||||
if !super::super::rust_simd::avx2_ok() {
|
||||
@@ -247,34 +300,10 @@ fn iq4xs_multi_token_avx2_kernel_matches_the_scalar_model_bitwise() {
|
||||
let mut seed = 0x5eed_1234_abcd_ef01u64;
|
||||
let nb = 10usize;
|
||||
for round in 0..6 {
|
||||
let mut row = vec![0u8; nb * 136];
|
||||
for v in row.iter_mut() {
|
||||
*v = xorshift(&mut seed) as u8;
|
||||
}
|
||||
for b in row.chunks_exact_mut(136) {
|
||||
let e = 8 + (xorshift(&mut seed) % 8) as u16;
|
||||
let m = (xorshift(&mut seed) & 0x3ff) as u16;
|
||||
b[..2].copy_from_slice(&((e << 10) | m).to_le_bytes());
|
||||
}
|
||||
let mut ys: Vec<Vec<Q8K>> = Vec::new();
|
||||
for _ in 0..8 {
|
||||
let x: Vec<f32> = (0..nb * 256)
|
||||
.map(|_| ((xorshift(&mut seed) % 20001) as f32 - 10000.0) * (0.001 + (round as f32)))
|
||||
.collect();
|
||||
let mut y = vec![Q8K::default(); nb];
|
||||
q8_k(&x, &mut y);
|
||||
ys.push(y);
|
||||
}
|
||||
let row = random_iq4xs_row(nb, &mut seed);
|
||||
let ys = random_iq4xs_acts(round, nb, &mut seed);
|
||||
for nt in [1usize, 2, 3, 5, 8] {
|
||||
let refs: Vec<&[Q8K]> = ys[..nt].iter().map(|y| y.as_slice()).collect();
|
||||
let mut out = vec![0.0f32; nt];
|
||||
// SAFETY: avx2_ok() above; row is exactly nb blocks of 136 bytes and every Q8K row has nb blocks.
|
||||
unsafe {
|
||||
dot_multi256(23, &row, &refs, &mut out);
|
||||
}
|
||||
for t in 0..nt {
|
||||
assert_eq!(out[t].to_bits(), model_iq4xs(&row, &ys[t]).to_bits(), "round {round} nt {nt} token {t}");
|
||||
}
|
||||
assert_iq4xs_kernel_matches_model(round, &row, &ys, nt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
|
||||
// Bounds: `row` is walked with chunks_exact/checked slicing and every activation block is safe-indexed (`y[i]`), so a short
|
||||
// operand panics instead of reading past it. The raw loads are 32/64 bytes at 64*j+32*h (j<4,h<2) of a 256-byte Q8K.qs, 16
|
||||
// bytes inside a block (<= block size), and the only data-dependent address is the optional gather: idx <= 255 into
|
||||
// IQ3XXS_GRID[u32;256] and idx <= 511 into IQ3S_GRID[u32;512]. Gate: avx2_ok() / avx512_ok()+dq_ok().
|
||||
// Bounds: `row` is walked with chunks_exact/checked slicing and every activation block is safe-indexed (`y[i]`), so a
|
||||
// short operand panics instead of reading past it. The raw loads are 32/64 bytes at 64*j+32*h (j<4,h<2) of a 256-byte
|
||||
// Q8K.qs, 16 bytes inside a block (<= block size), and the only data-dependent address is the optional gather:
|
||||
// idx <= 255 into IQ3XXS_GRID[u32;256] and idx <= 511 into IQ3S_GRID[u32;512]. Gate: avx2_ok() / avx512_ok()+dq_ok().
|
||||
pub(super) fn u16(b: &[u8], o: usize) -> u16 {
|
||||
u16::from_le_bytes(b[o..o + 2].try_into().unwrap())
|
||||
}
|
||||
@@ -32,96 +32,115 @@ pub(super) fn factor(t: i32) -> f32 {
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
/// One decoded 32-value half: magnitude bytes, literal sign word, and the integer scale of each 16-value quarter.
|
||||
type HalfParts = ([u8; 32], u32, i16, i16);
|
||||
// Whole 32-value half: magnitude bytes, literal signs and 16-value integer scales.
|
||||
#[inline(always)]
|
||||
pub(super) fn decode(t: i32, b: &[u8], j: usize, h: usize) -> ([u8; 32], u32, [i16; 16]) {
|
||||
let mut g = [0u8; 32];
|
||||
let mut signs = 0u32;
|
||||
let (s0, s1);
|
||||
match t {
|
||||
16 => {
|
||||
let a = u32(b, 2 + 16 * j + 8 * h);
|
||||
let w = u32(b, 6 + 16 * j + 8 * h);
|
||||
for l in 0..4 {
|
||||
g[l * 8..l * 8 + 8].copy_from_slice(&IQ2XXS_GRID[((a >> (8 * l)) & 255) as usize].to_le_bytes());
|
||||
signs |= (KSIGNS_IQ2XS[((w >> (7 * l)) & 127) as usize] as u32) << (8 * l);
|
||||
}
|
||||
let s = 2 * (w >> 28) as i16 + 1;
|
||||
s0 = s;
|
||||
s1 = s;
|
||||
}
|
||||
17 => {
|
||||
for l in 0..4 {
|
||||
let v = u16(b, 2 + 16 * j + 8 * h + 2 * l);
|
||||
g[l * 8..l * 8 + 8].copy_from_slice(&IQ2XS_GRID[(v & 511) as usize].to_le_bytes());
|
||||
signs |= (KSIGNS_IQ2XS[(v >> 9) as usize] as u32) << (8 * l);
|
||||
}
|
||||
let s = b[66 + 2 * j + h];
|
||||
s0 = 2 * (s & 15) as i16 + 1;
|
||||
s1 = 2 * (s >> 4) as i16 + 1;
|
||||
}
|
||||
22 => {
|
||||
let q = &b[2 + 8 * j + 4 * h..];
|
||||
let high = b[66 + 2 * j + h] as usize;
|
||||
for l in 0..4 {
|
||||
let idx = q[l] as usize | ((high << (8 - 2 * l)) & 0x300);
|
||||
g[l * 8..l * 8 + 8].copy_from_slice(&IQ2S_GRID[idx].to_le_bytes());
|
||||
}
|
||||
signs = u32(b, 34 + 8 * j + 4 * h);
|
||||
let s = b[74 + 2 * j + h];
|
||||
s0 = 2 * (s & 15) as i16 + 1;
|
||||
s1 = 2 * (s >> 4) as i16 + 1;
|
||||
}
|
||||
18 => {
|
||||
let q = &b[2 + 16 * j + 8 * h..];
|
||||
for l in 0..8 {
|
||||
g[l * 4..l * 4 + 4].copy_from_slice(&IQ3XXS_GRID[q[l] as usize].to_le_bytes());
|
||||
}
|
||||
let w = u32(b, 66 + 8 * j + 4 * h);
|
||||
for l in 0..4 {
|
||||
signs |= (KSIGNS_IQ2XS[((w >> (7 * l)) & 127) as usize] as u32) << (8 * l);
|
||||
}
|
||||
s0 = 2 * (w >> 28) as i16 + 1;
|
||||
s1 = s0;
|
||||
}
|
||||
21 => {
|
||||
let q = &b[2 + 16 * j + 8 * h..];
|
||||
let high = b[66 + 2 * j + h] as usize;
|
||||
for l in 0..8 {
|
||||
g[l * 4..l * 4 + 4].copy_from_slice(&IQ3S_GRID[q[l] as usize | (((high >> l) & 1) << 8)].to_le_bytes());
|
||||
}
|
||||
signs = u32(b, 74 + 8 * j + 4 * h);
|
||||
let s = b[106 + j];
|
||||
s0 = 2 * (if h == 0 { s & 15 } else { s >> 4 }) as i16 + 1;
|
||||
s1 = s0;
|
||||
}
|
||||
// IQ4_XS (AVX2 multi-token tree only, STRA-324): iq_avx2.cpp Fmt32<23>::decode. `g` = |kvalues|, the sign bit = (kvalue<0)
|
||||
// (no kvalue is 0), scale = the signed 6-bit sub-block scale ls-32 of sub-block 2j+h.
|
||||
23 => {
|
||||
let hh = 2 * j + h;
|
||||
let bits = &b[8 + 16 * hh..8 + 16 * hh + 16];
|
||||
for l in 0..16 {
|
||||
let lo = KVALUES_IQ4NL[(bits[l] & 15) as usize];
|
||||
let hi = KVALUES_IQ4NL[(bits[l] >> 4) as usize];
|
||||
g[l] = lo.unsigned_abs();
|
||||
g[16 + l] = hi.unsigned_abs();
|
||||
if lo < 0 {
|
||||
signs |= 1 << l;
|
||||
}
|
||||
if hi < 0 {
|
||||
signs |= 1 << (16 + l);
|
||||
}
|
||||
}
|
||||
let ls = ((b[4 + (hh >> 1)] >> (4 * (hh & 1))) & 15) as i16 | ((((u16(b, 2) >> (2 * hh)) & 3) as i16) << 4);
|
||||
s0 = ls - 32;
|
||||
s1 = s0;
|
||||
}
|
||||
let (g, signs, s0, s1) = match t {
|
||||
16 => decode_iq2xxs(b, j, h),
|
||||
17 => decode_iq2xs(b, j, h),
|
||||
22 => decode_iq2s(b, j, h),
|
||||
18 => decode_iq3xxs(b, j, h),
|
||||
21 => decode_iq3s(b, j, h),
|
||||
23 => decode_iq4xs(b, j, h),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
};
|
||||
let mut sc = [s0; 16];
|
||||
sc[8..].fill(s1);
|
||||
(g, signs, sc)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn decode_iq2xxs(b: &[u8], j: usize, h: usize) -> HalfParts {
|
||||
let mut g = [0u8; 32];
|
||||
let mut signs = 0u32;
|
||||
let a = u32(b, 2 + 16 * j + 8 * h);
|
||||
let w = u32(b, 6 + 16 * j + 8 * h);
|
||||
for l in 0..4 {
|
||||
g[l * 8..l * 8 + 8].copy_from_slice(&IQ2XXS_GRID[((a >> (8 * l)) & 255) as usize].to_le_bytes());
|
||||
signs |= (KSIGNS_IQ2XS[((w >> (7 * l)) & 127) as usize] as u32) << (8 * l);
|
||||
}
|
||||
let s = 2 * (w >> 28) as i16 + 1;
|
||||
(g, signs, s, s)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn decode_iq2xs(b: &[u8], j: usize, h: usize) -> HalfParts {
|
||||
let mut g = [0u8; 32];
|
||||
let mut signs = 0u32;
|
||||
for l in 0..4 {
|
||||
let v = u16(b, 2 + 16 * j + 8 * h + 2 * l);
|
||||
g[l * 8..l * 8 + 8].copy_from_slice(&IQ2XS_GRID[(v & 511) as usize].to_le_bytes());
|
||||
signs |= (KSIGNS_IQ2XS[(v >> 9) as usize] as u32) << (8 * l);
|
||||
}
|
||||
let s = b[66 + 2 * j + h];
|
||||
(g, signs, 2 * (s & 15) as i16 + 1, 2 * (s >> 4) as i16 + 1)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn decode_iq2s(b: &[u8], j: usize, h: usize) -> HalfParts {
|
||||
let mut g = [0u8; 32];
|
||||
let q = &b[2 + 8 * j + 4 * h..];
|
||||
let high = b[66 + 2 * j + h] as usize;
|
||||
for l in 0..4 {
|
||||
let idx = q[l] as usize | ((high << (8 - 2 * l)) & 0x300);
|
||||
g[l * 8..l * 8 + 8].copy_from_slice(&IQ2S_GRID[idx].to_le_bytes());
|
||||
}
|
||||
let signs = u32(b, 34 + 8 * j + 4 * h);
|
||||
let s = b[74 + 2 * j + h];
|
||||
(g, signs, 2 * (s & 15) as i16 + 1, 2 * (s >> 4) as i16 + 1)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn decode_iq3xxs(b: &[u8], j: usize, h: usize) -> HalfParts {
|
||||
let mut g = [0u8; 32];
|
||||
let mut signs = 0u32;
|
||||
let q = &b[2 + 16 * j + 8 * h..];
|
||||
for l in 0..8 {
|
||||
g[l * 4..l * 4 + 4].copy_from_slice(&IQ3XXS_GRID[q[l] as usize].to_le_bytes());
|
||||
}
|
||||
let w = u32(b, 66 + 8 * j + 4 * h);
|
||||
for l in 0..4 {
|
||||
signs |= (KSIGNS_IQ2XS[((w >> (7 * l)) & 127) as usize] as u32) << (8 * l);
|
||||
}
|
||||
let s0 = 2 * (w >> 28) as i16 + 1;
|
||||
(g, signs, s0, s0)
|
||||
}
|
||||
#[inline(always)]
|
||||
fn decode_iq3s(b: &[u8], j: usize, h: usize) -> HalfParts {
|
||||
let mut g = [0u8; 32];
|
||||
let q = &b[2 + 16 * j + 8 * h..];
|
||||
let high = b[66 + 2 * j + h] as usize;
|
||||
for l in 0..8 {
|
||||
let idx = q[l] as usize | (((high >> l) & 1) << 8);
|
||||
g[l * 4..l * 4 + 4].copy_from_slice(&IQ3S_GRID[idx].to_le_bytes());
|
||||
}
|
||||
let signs = u32(b, 74 + 8 * j + 4 * h);
|
||||
let s = b[106 + j];
|
||||
let s0 = 2 * (if h == 0 { s & 15 } else { s >> 4 }) as i16 + 1;
|
||||
(g, signs, s0, s0)
|
||||
}
|
||||
// IQ4_XS (AVX2 multi-token tree only, STRA-324): iq_avx2.cpp Fmt32<23>::decode. `g` = |kvalues|, the sign bit =
|
||||
// (kvalue<0) (no kvalue is 0), scale = the signed 6-bit sub-block scale ls-32 of sub-block 2j+h.
|
||||
#[inline(always)]
|
||||
fn decode_iq4xs(b: &[u8], j: usize, h: usize) -> HalfParts {
|
||||
let mut g = [0u8; 32];
|
||||
let mut signs = 0u32;
|
||||
let hh = 2 * j + h;
|
||||
let bits = &b[8 + 16 * hh..8 + 16 * hh + 16];
|
||||
for l in 0..16 {
|
||||
let lo = KVALUES_IQ4NL[(bits[l] & 15) as usize];
|
||||
let hi = KVALUES_IQ4NL[(bits[l] >> 4) as usize];
|
||||
g[l] = lo.unsigned_abs();
|
||||
g[16 + l] = hi.unsigned_abs();
|
||||
if lo < 0 {
|
||||
signs |= 1 << l;
|
||||
}
|
||||
if hi < 0 {
|
||||
signs |= 1 << (16 + l);
|
||||
}
|
||||
}
|
||||
let ls = ((b[4 + (hh >> 1)] >> (4 * (hh & 1))) & 15) as i16 | ((((u16(b, 2) >> (2 * hh)) & 3) as i16) << 4);
|
||||
let s0 = ls - 32;
|
||||
(g, signs, s0, s0)
|
||||
}
|
||||
#[target_feature(enable = "avx2,fma,f16c")]
|
||||
pub(super) unsafe fn decoded(t: i32, b: &[u8], j: usize, h: usize) -> (__m256i, __m256i, __m256i) {
|
||||
unsafe {
|
||||
@@ -139,17 +158,7 @@ pub(in super::super) unsafe fn dot_multi256(t: i32, row: &[u8], ys: &[&[Q8K]], o
|
||||
for i in 0..nb {
|
||||
let b = &row[i * format_bytes(t)..][..format_bytes(t)];
|
||||
prefetch(b.as_ptr());
|
||||
let mut ai = [_mm256_setzero_si256(); 8];
|
||||
for j in 0..4 {
|
||||
for h in 0..2 {
|
||||
let (g, sgn, sc) = decoded(t, b, j, h);
|
||||
for (k, y) in ys.iter().enumerate() {
|
||||
let v = _mm256_loadu_si256(y[i].qs.as_ptr().add(64 * j + 32 * h).cast());
|
||||
let sy = _mm256_sign_epi8(v, sgn);
|
||||
ai[k] = _mm256_add_epi32(ai[k], _mm256_madd_epi16(_mm256_maddubs_epi16(g, sy), sc));
|
||||
}
|
||||
}
|
||||
}
|
||||
let ai = sum_block256(t, b, ys, i);
|
||||
let dx = half(u16(b, 0)) * factor(t);
|
||||
for (k, y) in ys.iter().enumerate() {
|
||||
accf[k] = _mm256_fmadd_ps(_mm256_set1_ps(dx * y[i].d), _mm256_cvtepi32_ps(ai[k]), accf[k]);
|
||||
@@ -160,44 +169,65 @@ pub(in super::super) unsafe fn dot_multi256(t: i32, row: &[u8], ys: &[&[Q8K]], o
|
||||
}
|
||||
}
|
||||
}
|
||||
/// The exact i32 accumulators of one weight block `b` against block `i` of every token's activation row.
|
||||
#[target_feature(enable = "avx2,fma,f16c")]
|
||||
unsafe fn sum_block256(t: i32, b: &[u8], ys: &[&[Q8K]], i: usize) -> [__m256i; 8] {
|
||||
unsafe {
|
||||
let mut ai = [_mm256_setzero_si256(); 8];
|
||||
for j in 0..4 {
|
||||
for h in 0..2 {
|
||||
let (g, sgn, sc) = decoded(t, b, j, h);
|
||||
for (k, y) in ys.iter().enumerate() {
|
||||
let v = _mm256_loadu_si256(y[i].qs.as_ptr().add(64 * j + 32 * h).cast());
|
||||
let sy = _mm256_sign_epi8(v, sgn);
|
||||
ai[k] = _mm256_add_epi32(ai[k], _mm256_madd_epi16(_mm256_maddubs_epi16(g, sy), sc));
|
||||
}
|
||||
}
|
||||
}
|
||||
ai
|
||||
}
|
||||
}
|
||||
/// The multi-token IQ kernel, specialised on the format and the token count (iq_avx512.cpp templates its
|
||||
/// `row_dot<TY, NT>` the same way): the same operations in the same order per token, so the same bits.
|
||||
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
|
||||
pub(in super::super) unsafe fn dot_multi512(t: i32, row: &[u8], ys: &[&[Q8K]], out: &mut [f32]) {
|
||||
unsafe {
|
||||
macro_rules! by_n {
|
||||
($T:literal) => {
|
||||
match ys.len() {
|
||||
1 => dot_multi512_tn::<$T, 1>(row, ys, out),
|
||||
2 => dot_multi512_tn::<$T, 2>(row, ys, out),
|
||||
3 => dot_multi512_tn::<$T, 3>(row, ys, out),
|
||||
4 => dot_multi512_tn::<$T, 4>(row, ys, out),
|
||||
5 => dot_multi512_tn::<$T, 5>(row, ys, out),
|
||||
6 => dot_multi512_tn::<$T, 6>(row, ys, out),
|
||||
7 => dot_multi512_tn::<$T, 7>(row, ys, out),
|
||||
8 => dot_multi512_tn::<$T, 8>(row, ys, out),
|
||||
_ => panic!("dot_multi512: 1..=8 tokens"),
|
||||
}
|
||||
};
|
||||
}
|
||||
match t {
|
||||
16 => by_n!(16),
|
||||
17 => by_n!(17),
|
||||
18 => by_n!(18),
|
||||
21 => by_n!(21),
|
||||
22 => by_n!(22),
|
||||
16 => dot_multi512_by_tokens::<16>(row, ys, out),
|
||||
17 => dot_multi512_by_tokens::<17>(row, ys, out),
|
||||
18 => dot_multi512_by_tokens::<18>(row, ys, out),
|
||||
21 => dot_multi512_by_tokens::<21>(row, ys, out),
|
||||
22 => dot_multi512_by_tokens::<22>(row, ys, out),
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `LELEKA_IQ_MULTI=register` (opt-in, measured SLOWER on the 9950X3D: 2 tokens 36.2 against 29.4 us of gate/up drain per expert in
|
||||
/// `cpu_service_bench`, runs/cpu-service2 bench out-b) builds the multi-token AVX-512 IQ3 halves in registers; the default keeps the stack arrays.
|
||||
/// `dot_multi512` of one format: the token count (1..=8) picks the monomorphised kernel.
|
||||
#[target_feature(enable = "avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
|
||||
unsafe fn dot_multi512_by_tokens<const T: i32>(row: &[u8], ys: &[&[Q8K]], out: &mut [f32]) {
|
||||
unsafe {
|
||||
match ys.len() {
|
||||
1 => dot_multi512_tn::<T, 1>(row, ys, out),
|
||||
2 => dot_multi512_tn::<T, 2>(row, ys, out),
|
||||
3 => dot_multi512_tn::<T, 3>(row, ys, out),
|
||||
4 => dot_multi512_tn::<T, 4>(row, ys, out),
|
||||
5 => dot_multi512_tn::<T, 5>(row, ys, out),
|
||||
6 => dot_multi512_tn::<T, 6>(row, ys, out),
|
||||
7 => dot_multi512_tn::<T, 7>(row, ys, out),
|
||||
8 => dot_multi512_tn::<T, 8>(row, ys, out),
|
||||
_ => panic!("dot_multi512: 1..=8 tokens"),
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `LELEKA_IQ_MULTI=register` (opt-in, measured SLOWER on the 9950X3D: 2 tokens 36.2 against 29.4 us of gate/up drain
|
||||
/// per expert in `cpu_service_bench`, runs/cpu-service2 bench out-b) builds the multi-token AVX-512 IQ3 halves in
|
||||
/// registers; the default keeps the stack arrays.
|
||||
pub(super) fn iq3_multi_fast() -> bool {
|
||||
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
|
||||
*ON.get_or_init(|| std::env::var("LELEKA_IQ_MULTI").as_deref() == Ok("register"))
|
||||
}
|
||||
/// 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.
|
||||
/// 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.
|
||||
#[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];
|
||||
|
||||
Reference in New Issue
Block a user