[HIP] AttnRes block-mix kernel — Inkling deep-residual parity (last defining mechanism)
- k_attn_res_mix / vetta_attn_res_mix: exact strata k1_attn_res_mix semantics. For each learned query q and token m, mix n_states = n_blocks+1 depth states (block snapshots + current): per state inv_rms = rsqrt(mean(x^2)+eps), score = (x.q)*inv_rms/sqrt(D); softmax over scores -> w; final weight = w*inv_rms; out = sum_s weight[s]*state_s (RMS-normalized depth mix). This is Inkling's deep residual that replaces the standard additive residual stream. Contiguous blocks [n_blocks,M,D] snapshot buffer (FFI-friendly vs ptr array). - Parity test vs CPU oracle (nb=3, M=2, nq=2, D=64): max_rel_err 1.0e-5. With this, ALL defining Inkling mechanisms are verified kernels: ShortConv4, flash SWA GQA (window+relpos+sink+gate), AttnRes block-mix, NVFP4 experts + SiTU + sigmoid router. The kernel parity foundation is complete; what remains is integration (lowering the full graph + wiring a complete layer + loop controllers + KV store + projections). Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS; lean proofs (4 files, 42 theorems) green; GPU 13/13 (--features hip).
This commit is contained in:
1 parent
aea1c437d4
commit
61c0b21a93
3 files changed
+142
No files matched your search
@@ -63,6 +63,7 @@ mod ffi {
|
||||
pub fn vetta_situ(gu: *const c_float, act: *mut c_float, inter: c_int, beta: c_float, lb: c_float, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_moe_reduce(y_part: *const c_float, tk_w: *const c_float, out: *mut c_float, top_k: c_int, hidden: c_int, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_shortconv4_decode(cur: *const c_float, static_w: *const c_float, hist: *mut c_float, out: *mut c_float, slot_ids: *const i32, m: c_int, c: c_int, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_attn_res_mix(blocks: *const c_float, cur: *const c_float, queries: *const c_float, out: *mut c_float, n_blocks: c_int, m: c_int, n_queries: c_int, d: c_int, eps: c_float, stream: *mut c_void) -> c_int;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,6 +271,15 @@ pub fn shortconv4_decode(cur: &DeviceBuf, static_w: &DeviceBuf, hist: &DeviceBuf
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Launch k_attn_res_mix: Inkling AttnRes deep-residual block-mix.
|
||||
#[cfg(feature = "hip")]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn attn_res_mix(blocks: &DeviceBuf, cur: &DeviceBuf, queries: &DeviceBuf, out: &DeviceBuf, n_blocks: i32, m: i32, n_queries: i32, d: i32, eps: f32) -> Result<(), String> {
|
||||
let rc = unsafe { ffi::vetta_attn_res_mix(blocks.ptr as *const f32, cur.ptr as *const f32, queries.ptr as *const f32, out.ptr as *mut f32, n_blocks, m, n_queries, d, eps, std::ptr::null_mut()) };
|
||||
if rc != 0 { return Err("attn_res_mix launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "hip"))]
|
||||
mod gpu_tests {
|
||||
use super::*;
|
||||
@@ -705,6 +715,56 @@ mod gpu_tests {
|
||||
eprintln!("shortconv4 {steps}-step recurrence + history ring: exact");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn attn_res_mix_parity_vs_cpu() {
|
||||
let (nb, m, nq, d) = (3usize, 2usize, 2usize, 64usize);
|
||||
let eps = 1e-6f32;
|
||||
let blocks: Vec<f32> = (0..nb * m * d).map(|i| ((i % 37) as f32 - 18.0) * 0.03).collect();
|
||||
let cur: Vec<f32> = (0..m * d).map(|i| ((i % 29) as f32 - 14.0) * 0.04).collect();
|
||||
let queries: Vec<f32> = (0..nq * d).map(|i| ((i % 19) as f32 - 9.0) * 0.05).collect();
|
||||
let n_states = nb + 1;
|
||||
let inv_sqrt_d = 1.0 / (d as f32).sqrt();
|
||||
let state = |s: usize, mi: usize, dd: usize| -> f32 {
|
||||
if s < nb { blocks[(s * m + mi) * d + dd] } else { cur[mi * d + dd] }
|
||||
};
|
||||
let mut want = vec![0f32; m * nq * d];
|
||||
for mi in 0..m {
|
||||
for q in 0..nq {
|
||||
let mut scores = vec![0f32; n_states];
|
||||
let mut irs = vec![0f32; n_states];
|
||||
for s in 0..n_states {
|
||||
let mut ss = 0f32; let mut dot = 0f32;
|
||||
for dd in 0..d { let x = state(s, mi, dd); ss += x * x; dot += x * queries[q * d + dd]; }
|
||||
let ir = 1.0 / (ss / d as f32 + eps).sqrt();
|
||||
irs[s] = ir;
|
||||
scores[s] = dot * ir * inv_sqrt_d;
|
||||
}
|
||||
let mx = scores.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
|
||||
let exps: Vec<f32> = scores.iter().map(|sc| (sc - mx).exp()).collect();
|
||||
let sum: f32 = exps.iter().sum();
|
||||
let weights: Vec<f32> = exps.iter().zip(&irs).map(|(e, ir)| (e / sum) * ir).collect();
|
||||
for dd in 0..d {
|
||||
let mut acc = 0f32;
|
||||
for s in 0..n_states { acc += weights[s] * state(s, mi, dd); }
|
||||
want[(mi * nq + q) * d + dd] = acc;
|
||||
}
|
||||
}
|
||||
}
|
||||
let db = DeviceBuf::alloc(blocks.len() * 4).unwrap();
|
||||
let dc = DeviceBuf::alloc(cur.len() * 4).unwrap();
|
||||
let dq = DeviceBuf::alloc(queries.len() * 4).unwrap();
|
||||
let dout = DeviceBuf::alloc(want.len() * 4).unwrap();
|
||||
db.h2d(&blocks).unwrap(); dc.h2d(&cur).unwrap(); dq.h2d(&queries).unwrap();
|
||||
attn_res_mix(&db, &dc, &dq, &dout, nb as i32, m as i32, nq as i32, d as i32, eps).unwrap();
|
||||
let mut got = vec![0f32; want.len()];
|
||||
dout.d2h(&mut got).unwrap();
|
||||
let mut max_rel = 0f32;
|
||||
for (g, w) in got.iter().zip(want.iter()) { max_rel = max_rel.max((g - w).abs() / w.abs().max(1e-3)); }
|
||||
eprintln!("attn_res_mix nb={nb} m={m} nq={nq} d={d} max_rel_err={max_rel:.3e}");
|
||||
assert!(max_rel < 1e-4, "attn_res_mix parity failed: {max_rel}");
|
||||
}
|
||||
|
||||
fn bytemuck_u16(v: &[u16]) -> &[u8] {
|
||||
unsafe { std::slice::from_raw_parts(v.as_ptr() as *const u8, v.len() * 2) }
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user