cpu-service2: register-built multi-token IQ3 opt-in only (slower on Zen 5 in the bench); vectorized output check of computed rows (LELEKA_Q8K=legacy keeps the scan)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Pavlo Ratushnyi
2026-10-31 21:57:03 +01:00
co-authored by Claude Sonnet 5.5
parent a2cd2b3acc
commit 4c04459b95
2 changed files with 5 additions and 4 deletions
+4 -3
View File
@@ -51,8 +51,9 @@ pub(super) unsafe fn dot_multi512(t:i32,row:&[u8],ys:&[&[Q8K]],out:&mut [f32]) {
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),_=>unreachable!()}
}}
/// `LELEKA_IQ_MULTI=legacy` keeps the stack-array decode in the multi-token AVX-512 IQ3 tree (A/B arm).
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("legacy"))}
/// `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.
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.
#[target_feature(enable="avx2,fma,f16c")]
@@ -69,7 +70,7 @@ unsafe fn iq3_half<const T:i32>(b:&[u8],j:usize,h:usize)->(__m256i,u32,i16) {
}
#[target_feature(enable="avx512f,avx512bw,avx512vl,avx512dq,avx512vbmi,avx512vnni,avx2,fma,f16c")]
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())}}
/// `fast`: the register-built IQ3 halves (bitwise the stack-array decode, `iq3_multi_trees_are_bitwise_equal`).
/// `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")]
unsafe fn dot_multi512_impl<const T:i32,const N:usize>(row:&[u8],ys:&[&[Q8K]],out:&mut [f32],fast:bool) {unsafe {
let t=T;
+1 -1
View File
@@ -180,7 +180,7 @@ impl NativePool {
if let (Some(t),Some(acc))=(t_pre,self.timing.as_mut()){acc[0]+=t.elapsed().as_secs_f64()*1e3;}
self.run_published(f,experts.len());
let t_post=self.timing.is_some().then(Instant::now);
for x in experts{for &(token,slot) in x.pairs{let at=(token*stride+slot)*H;if out[at..at+H].iter().any(|v|!v.is_finite()){return Err(Error::NonFinite);}}}
for x in experts{for &(token,slot) in x.pairs{let at=(token*stride+slot)*H;if !all_finite(&out[at..at+H]){return Err(Error::NonFinite);}}}
if let (Some(t),Some(acc))=(t_post,self.timing.as_mut()){acc[12]+=t.elapsed().as_secs_f64()*1e3;}
Ok(())
}