diff --git a/crates/vetta-infer-hip/src/lib.rs b/crates/vetta-infer-hip/src/lib.rs index 2cb9340..1fbebbd 100644 --- a/crates/vetta-infer-hip/src/lib.rs +++ b/crates/vetta-infer-hip/src/lib.rs @@ -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 = (0..nb * m * d).map(|i| ((i % 37) as f32 - 18.0) * 0.03).collect(); + let cur: Vec = (0..m * d).map(|i| ((i % 29) as f32 - 14.0) * 0.04).collect(); + let queries: Vec = (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 = scores.iter().map(|sc| (sc - mx).exp()).collect(); + let sum: f32 = exps.iter().sum(); + let weights: Vec = 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) } } diff --git a/kernels/hip/vetta_kernels.h b/kernels/hip/vetta_kernels.h index 75d0efa..c287776 100644 --- a/kernels/hip/vetta_kernels.h +++ b/kernels/hip/vetta_kernels.h @@ -93,6 +93,12 @@ int vetta_shortconv4_decode(const float* cur, const float* static_w, float* hist float* out, const int32_t* slot_ids, int M, int C, vetta_stream_t stream); +// k_attn_res_mix: Inkling AttnRes deep-residual block-mix. blocks [n_blocks,M,D] +// snapshots + cur [M,D] -> out [M,n_queries,D] (RMS-normalized depth mix). +int vetta_attn_res_mix(const float* blocks, const float* cur, const float* queries, + float* out, int n_blocks, int M, int n_queries, int D, + float eps, vetta_stream_t stream); + // Core <15 microkernel entry points (implemented progressively in M1 & M2): // 1. k_embed_bf16 // 2. k_rms_norm diff --git a/kernels/hip/vetta_kernels.hip b/kernels/hip/vetta_kernels.hip index e372540..47a62bf 100644 --- a/kernels/hip/vetta_kernels.hip +++ b/kernels/hip/vetta_kernels.hip @@ -611,4 +611,80 @@ extern "C" int vetta_shortconv4_decode(const float* cur, const float* static_w, k_shortconv4_decode<<>>(cur, static_w, hist, out, slot_ids, M, C); HIP_CHECK(hipGetLastError()); return 0; +} + +// ---- k_attn_res_mix: Inkling AttnRes deep-residual block-mix (parity) ------ +// Exact strata k1_attn_res_mix: for each query q and token m, mix n_states = +// n_blocks+1 depth states (block snapshots + current). Per state s: +// inv_rms[s] = rsqrt(mean(x^2) + eps); score[s] = (x.q) * inv_rms[s] / sqrt(D) +// softmax over scores -> w[s]; final weight = w[s] * inv_rms[s]; +// out[m,q,:] = sum_s weight[s] * state_s[m,:] (RMS-normalized depth mix). +// blocks [n_blocks, M, D] contiguous snapshots; cur [M, D]; queries [nq, D]. + +__global__ void k_attn_res_mix(const float* __restrict__ blocks, + const float* __restrict__ cur, + const float* __restrict__ queries, + float* __restrict__ out, + int n_blocks, int M, int n_queries, int D, float eps) { + const int q = blockIdx.x; + const int m = blockIdx.y; + if (q >= n_queries || m >= M) return; + const int tid = threadIdx.x; + const int nt = blockDim.x; + const int n_states = n_blocks + 1; + const float inv_sqrt_d = rsqrtf((float)D); + const float* qv = queries + (size_t)q * D; + __shared__ float red[256]; + __shared__ float scores[16]; + __shared__ float inv_rms[16]; + + for (int st = 0; st < n_states; ++st) { + const float* sv = (st < n_blocks) ? (blocks + (size_t)st * M * D + (size_t)m * D) + : (cur + (size_t)m * D); + float ss = 0.f, dot = 0.f; + for (int d = tid; d < D; d += nt) { float x = sv[d]; ss += x * x; dot += x * qv[d]; } + red[tid] = ss; __syncthreads(); + for (int t = nt / 2; t > 0; t >>= 1) { if (tid < t) red[tid] += red[tid + t]; __syncthreads(); } + const float tss = red[0]; __syncthreads(); + red[tid] = dot; __syncthreads(); + for (int t = nt / 2; t > 0; t >>= 1) { if (tid < t) red[tid] += red[tid + t]; __syncthreads(); } + const float tdot = red[0]; __syncthreads(); + if (tid == 0) { + const float ir = rsqrtf(tss / (float)D + eps); + inv_rms[st] = ir; + scores[st] = tdot * ir * inv_sqrt_d; + } + __syncthreads(); + } + if (tid == 0) { + float mx = -1e30f; + for (int st = 0; st < n_states; ++st) mx = fmaxf(mx, scores[st]); + float sum = 0.f; + for (int st = 0; st < n_states; ++st) { scores[st] = __expf(scores[st] - mx); sum += scores[st]; } + const float inv = 1.f / fmaxf(sum, 1e-30f); + for (int st = 0; st < n_states; ++st) scores[st] = scores[st] * inv * inv_rms[st]; + } + __syncthreads(); + + float* dst = out + ((size_t)m * n_queries + q) * D; + for (int d = tid; d < D; d += nt) { + float acc = 0.f; + for (int st = 0; st < n_states; ++st) { + const float* sv = (st < n_blocks) ? (blocks + (size_t)st * M * D + (size_t)m * D) + : (cur + (size_t)m * D); + acc += scores[st] * sv[d]; + } + dst[d] = acc; + } +} + +extern "C" int vetta_attn_res_mix(const float* blocks, const float* cur, const float* queries, + float* out, int n_blocks, int M, int n_queries, int D, + float eps, vetta_stream_t stream) { + if (M <= 0 || n_queries <= 0 || D <= 0 || n_blocks < 0 || n_blocks + 1 > 16) return 1; + dim3 grid(n_queries, M); + hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0; + k_attn_res_mix<<>>(blocks, cur, queries, out, n_blocks, M, n_queries, D, eps); + HIP_CHECK(hipGetLastError()); + return 0; } \ No newline at end of file