[Core] batched full-attn layer (M=B) bit-exact + l2_clip per-token tau

layer_forward_full_batched mirrors layer_forward_full at M=B: 3-query mix
[B*3,h] split strided via gather_channel(nq=3) into in0/in1/in2 with separate
q/k/v norms; DYNAMIC ShortConv K/V via shortconv4_prefill_par(dyn_taps[B,n_kv,4])
(already supported dyn path, no new kernel); Q/R l2_clip with per-token tau;
global flash window=0; MoE sublayer identical to batched SWA.

k_l2_clip gains pos_arr (device [M]): tau uses pos_arr[r/scale_rows] per token
(full-attn) else scalar pos. l2_clip passes null (M=1 unchanged); l2_clip_batched
takes Option<&DeviceBuf> pos_arr.

layer_full_batched_parity (real weights ip=5, B=8, f16 KV + device MoE):
cur[B,h] BIT-EXACT (0.0) AND KV byte-identical vs 8 sequential M=1 calls.
Both layer types (SWA + full-attn) now batched and verified.
This commit is contained in:
adm committed 2026-12-02 00:24:37 +01:00
1 parent 3dbb6bb1b4
commit 1aaf7f12c2
4 files changed
+170 -13

No files matched your search

+8 -5
View File
@@ -122,7 +122,7 @@ mod ffi {
pub fn vetta_shortconv4_prefill_par(in_x: *const c_float, static_w: *const c_float, dyn_taps: *const c_float, hist: *const c_float, out_y: *mut c_float, b: c_int, c: c_int, n_heads: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_shortconv4_update_hist(in_x: *const c_float, hist: *mut c_float, b: 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;
pub fn vetta_l2_clip(x: *mut c_float, scale: *const c_float, rows: c_int, hd: c_int, clip: c_float, apply_tau: c_int, pos: c_int, scale_rows: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_l2_clip(x: *mut c_float, scale: *const c_float, rows: c_int, hd: c_int, clip: c_float, apply_tau: c_int, pos: c_int, scale_rows: c_int, pos_arr: *const c_int, stream: *mut c_void) -> c_int;
pub fn vetta_relpos_bias(r: *const c_float, rel_proj: *const c_float, rel_bias: *mut c_float, n_head: c_int, d_rel: c_int, rel_extent: c_int, lim: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_relpos_bias_batched(r: *const c_float, rel_proj: *const c_float, rel_bias: *mut c_float, pos: *const c_int, n_head: c_int, d_rel: c_int, rel_extent: c_int, m: c_int, stream: *mut c_void) -> c_int;
pub fn vetta_kv_store(k_in: *const c_float, v_in: *const c_float, k_cache: *mut c_float, v_cache: *mut c_float, pos: c_int, n_kv: c_int, hd: c_int, seq: c_int, rms_eps: c_float, stream: *mut c_void) -> c_int;
@@ -715,7 +715,7 @@ pub fn attn_res_mix(blocks: &DeviceBuf, cur: &DeviceBuf, queries: &DeviceBuf, ou
#[cfg(feature = "hip")]
pub fn l2_clip(x: &DeviceBuf, scale: Option<&DeviceBuf>, rows: i32, hd: i32, clip: f32, apply_tau: bool, pos: i32) -> Result<(), String> {
let sc = scale.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
let rc = unsafe { ffi::vetta_l2_clip(x.ptr as *mut f32, sc, rows, hd, clip, apply_tau as i32, pos, rows, std::ptr::null_mut()) };
let rc = unsafe { ffi::vetta_l2_clip(x.ptr as *mut f32, sc, rows, hd, clip, apply_tau as i32, pos, rows, std::ptr::null(), std::ptr::null_mut()) };
if rc != 0 { return Err("l2_clip launch failed".into()); }
Ok(())
}
@@ -723,11 +723,14 @@ pub fn l2_clip(x: &DeviceBuf, scale: Option<&DeviceBuf>, rows: i32, hd: i32, cli
/// Batched L2 clip (rows = M*n_head): the per-head scale repeats every
/// `scale_rows` (=n_head) rows, so row r uses scale[r % scale_rows]. Without this
/// the batched rows >= n_head would index the [n_head,hd] scale out of bounds.
/// `pos_arr` (device [M]) supplies the per-token position for tau (full-attn);
/// pass None to use the scalar `pos` (SWA / no tau).
#[cfg(feature = "hip")]
#[allow(clippy::too_many_arguments)]
pub fn l2_clip_batched(x: &DeviceBuf, scale: Option<&DeviceBuf>, rows: i32, hd: i32, clip: f32, apply_tau: bool, pos: i32, scale_rows: i32) -> Result<(), String> {
pub fn l2_clip_batched(x: &DeviceBuf, scale: Option<&DeviceBuf>, rows: i32, hd: i32, clip: f32, apply_tau: bool, pos: i32, scale_rows: i32, pos_arr: Option<&DeviceBuf>) -> Result<(), String> {
let sc = scale.map(|b| b.ptr as *const f32).unwrap_or(std::ptr::null());
let rc = unsafe { ffi::vetta_l2_clip(x.ptr as *mut f32, sc, rows, hd, clip, apply_tau as i32, pos, scale_rows, std::ptr::null_mut()) };
let pa = pos_arr.map(|b| b.ptr as *const i32).unwrap_or(std::ptr::null());
let rc = unsafe { ffi::vetta_l2_clip(x.ptr as *mut f32, sc, rows, hd, clip, apply_tau as i32, pos, scale_rows, pa, std::ptr::null_mut()) };
if rc != 0 { return Err("l2_clip_batched launch failed".into()); }
Ok(())
}
@@ -1676,7 +1679,7 @@ mod gpu_tests {
let sc: Vec<f32> = (0..n_head * hd).map(|i| 0.5 + (i % 13) as f32 * 0.05).collect();
let dx = DeviceBuf::alloc(x.len() * 4).unwrap(); dx.h2d(&x).unwrap();
let dsc = DeviceBuf::alloc(sc.len() * 4).unwrap(); dsc.h2d(&sc).unwrap();
l2_clip_batched(&dx, Some(&dsc), (m * n_head) as i32, hd as i32, clip, false, 0, n_head as i32).unwrap();
l2_clip_batched(&dx, Some(&dsc), (m * n_head) as i32, hd as i32, clip, false, 0, n_head as i32, None).unwrap();
let mut got = vec![0f32; x.len()]; dx.d2h(&mut got).unwrap();
let dx1 = DeviceBuf::alloc(n_head * hd * 4).unwrap();
let mut max_rel = 0f32;