[HIP] ShortConv prefill parallel (tile 16) + update_hist: batched attention brick
k_shortconv4_prefill_par (exact strata k2_shortconv4_prefill_par): one thread per channel processes a tile of 16 contiguous tokens; 3-token history seeded from hist [3,C] (bf16-rounded f32, same ring as decode) for t0<3 else in_x bf16 round-trip. Static 1/sqrt(5) or dynamic taps [B,n_heads,4] 1/3. Replaces B sequential decode calls with one parallel launch (grid C-tiles x ceil(B/16)). k_shortconv4_update_hist: roll history <- last 3 in_x (bf16), shift for B<3. Parity vs B sequential decode steps: B=37 (partial last tile) C=64, output AND history BIT-EXACT (0.0). Note: k_attn_res_mix already supports M>1 (grid nq x M). Additive — decode default unchanged. Remaining batched attention: causal flash M=B + kv_store B positions + batched depth-blocks/controllers/bank, then forward_batch orchestration + serve wiring + EXACT parity. Verified: hip GPU 24 green, workspace CPU -D warnings green, 6 Lean 0 sorryAx.
This commit is contained in:
1 parent
b6113b549a
commit
ec2ad3fdc5
2 files changed
+140
No files matched your search
@@ -107,6 +107,8 @@ mod ffi {
|
||||
pub fn vetta_moe_reduce_buckets_masked(bucket_y: *const c_float, slotk_to_bidx: *const c_int, tk_w: *const c_float, out: *mut c_float, b: c_int, top_k: c_int, d: 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_shortconv4_dyn_decode(cur: *const c_float, static_w: *const c_float, dyn_taps: *const c_float, hist: *mut c_float, out: *mut c_float, slot_ids: *const i32, m: c_int, c: c_int, n_heads: c_int, stream: *mut c_void) -> c_int;
|
||||
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, 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;
|
||||
@@ -592,6 +594,25 @@ pub fn shortconv4_dyn_decode(cur: &DeviceBuf, static_w: &DeviceBuf, dyn_taps: &D
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Parallel causal ShortConv over B contiguous prefill tokens (tile 16). hist
|
||||
/// [3,C] bf16-rounded f32 seed; dyn_taps nullable (static 1/sqrt5 vs dyn 1/3).
|
||||
#[cfg(feature = "hip")]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn shortconv4_prefill_par(in_x: &DeviceBuf, static_w: &DeviceBuf, dyn_taps: Option<&DeviceBuf>, hist: &DeviceBuf, out_y: &DeviceBuf, b: i32, c: i32, n_heads: i32) -> Result<(), String> {
|
||||
let dt = dyn_taps.map(|t| t.ptr as *const f32).unwrap_or(std::ptr::null());
|
||||
let rc = unsafe { ffi::vetta_shortconv4_prefill_par(in_x.ptr as *const f32, static_w.ptr as *const f32, dt, hist.ptr as *const f32, out_y.ptr as *mut f32, b, c, n_heads, std::ptr::null_mut()) };
|
||||
if rc != 0 { return Err("shortconv4_prefill_par launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Roll the 3-token ShortConv history after a prefill chunk (last 3 in_x bf16).
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn shortconv4_update_hist(in_x: &DeviceBuf, hist: &DeviceBuf, b: i32, c: i32) -> Result<(), String> {
|
||||
let rc = unsafe { ffi::vetta_shortconv4_update_hist(in_x.ptr as *const f32, hist.ptr as *mut f32, b, c, std::ptr::null_mut()) };
|
||||
if rc != 0 { return Err("shortconv4_update_hist launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Launch k_attn_res_mix: Inkling AttnRes deep-residual block-mix.
|
||||
#[cfg(feature = "hip")]
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
@@ -1284,6 +1305,45 @@ mod gpu_tests {
|
||||
assert!(max_rel < 1e-4, "reduce_buckets_masked parity failed: {max_rel}");
|
||||
}
|
||||
|
||||
/// prefill_par (parallel causal ShortConv, tile 16) must equal B sequential
|
||||
/// decode steps bit-for-bit (same bf16 history semantics), and update_hist must
|
||||
/// equal the decode's final history. B=37 exercises a partial last tile.
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn shortconv4_prefill_par_parity() {
|
||||
let (b, c) = (37usize, 64usize);
|
||||
let inx: Vec<f32> = (0..b * c).map(|i| ((i % 53) as f32 - 26.0) * 0.03).collect();
|
||||
let sw: Vec<f32> = (0..c * 4).map(|i| 0.1 + (i % 7) as f32 * 0.05).collect();
|
||||
let hist0: Vec<f32> = (0..3 * c).map(|i| bf16_to_f32(f32_to_bf16(((i % 17) as f32 - 8.0) * 0.1))).collect();
|
||||
let din = DeviceBuf::alloc(b * c * 4).unwrap(); din.h2d(&inx).unwrap();
|
||||
let dsw = DeviceBuf::alloc(c * 4 * 4).unwrap(); dsw.h2d(&sw).unwrap();
|
||||
let dhist_p = DeviceBuf::alloc(3 * c * 4).unwrap(); dhist_p.h2d(&hist0).unwrap();
|
||||
let dout_p = DeviceBuf::alloc(b * c * 4).unwrap();
|
||||
shortconv4_prefill_par(&din, &dsw, None, &dhist_p, &dout_p, b as i32, c as i32, 0).unwrap();
|
||||
let mut got_p = vec![0f32; b * c]; dout_p.d2h(&mut got_p).unwrap();
|
||||
let dhist_s = DeviceBuf::alloc(3 * c * 4).unwrap(); dhist_s.h2d(&hist0).unwrap();
|
||||
let dslot = DeviceBuf::alloc(4).unwrap(); dslot.h2d_bytes(&0i32.to_le_bytes()).unwrap();
|
||||
let dcur = DeviceBuf::alloc(c * 4).unwrap();
|
||||
let dout1 = DeviceBuf::alloc(c * 4).unwrap();
|
||||
let mut got_s = vec![0f32; b * c];
|
||||
for t in 0..b {
|
||||
dcur.h2d(&inx[t * c..(t + 1) * c]).unwrap();
|
||||
shortconv4_decode(&dcur, &dsw, &dhist_s, &dout1, &dslot, 1, c as i32).unwrap();
|
||||
let mut o = vec![0f32; c]; dout1.d2h(&mut o).unwrap();
|
||||
got_s[t * c..(t + 1) * c].copy_from_slice(&o);
|
||||
}
|
||||
let mut max_rel = 0f32;
|
||||
for (g, w) in got_p.iter().zip(got_s.iter()) { max_rel = max_rel.max((g - w).abs() / w.abs().max(1e-3)); }
|
||||
shortconv4_update_hist(&din, &dhist_p, b as i32, c as i32).unwrap();
|
||||
let mut hp = vec![0f32; 3 * c]; dhist_p.d2h(&mut hp).unwrap();
|
||||
let mut hs = vec![0f32; 3 * c]; dhist_s.d2h(&mut hs).unwrap();
|
||||
let mut hmax = 0f32;
|
||||
for (a, bb) in hp.iter().zip(hs.iter()) { hmax = hmax.max((a - bb).abs()); }
|
||||
eprintln!("shortconv4_prefill_par B={b} C={c} out_max_rel={max_rel:.3e} hist_max_abs={hmax:.3e}");
|
||||
assert!(max_rel < 1e-5, "prefill_par output parity failed: {max_rel}");
|
||||
assert!(hmax < 1e-6, "update_hist parity failed: {hmax}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn shortconv4_multistep_parity() {
|
||||
|
||||
@@ -1191,6 +1191,86 @@ extern "C" int vetta_shortconv4_dyn_decode(const float* cur, const float* static
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_shortconv4_prefill_par: parallel causal ShortConv over B tokens ------
|
||||
// Exact strata k2_shortconv4_prefill_par. One thread per channel c processes a
|
||||
// tile of 16 contiguous tokens [t0,t0+16); the 3-token history is seeded from
|
||||
// hist [3,C] (bf16-rounded f32, same ring as decode) for t0<3 else from in_x with
|
||||
// bf16 round-trip. Static scale 1/sqrt(5), or dynamic taps [B,n_heads,4] scale 1/3.
|
||||
// This is the PREFILL ShortConv (M=B parallel), replacing B sequential decode calls.
|
||||
__global__ void __launch_bounds__(256) k_shortconv4_prefill_par(
|
||||
const float* __restrict__ in_x,
|
||||
const float* __restrict__ static_w,
|
||||
const float* __restrict__ dyn_taps,
|
||||
const float* __restrict__ hist,
|
||||
float* __restrict__ out_y,
|
||||
int B, int C, int n_heads) {
|
||||
const int c = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
const int t0 = blockIdx.y * 16;
|
||||
if (c >= C || t0 >= B) return;
|
||||
const float* sw = static_w + (size_t)c * 4;
|
||||
float x0 = (t0 >= 3) ? bf16_round(in_x[(size_t)(t0 - 3) * C + c]) : hist[(size_t)t0 * C + c];
|
||||
float x1 = (t0 >= 2) ? bf16_round(in_x[(size_t)(t0 - 2) * C + c]) : hist[(size_t)(t0 + 1) * C + c];
|
||||
float x2 = (t0 >= 1) ? bf16_round(in_x[(size_t)(t0 - 1) * C + c]) : hist[(size_t)2 * C + c];
|
||||
const bool has_dyn = (dyn_taps != nullptr && n_heads > 0);
|
||||
const int head = has_dyn ? (c / (C / n_heads)) : 0;
|
||||
const int tend = (t0 + 16 < B) ? (t0 + 16) : B;
|
||||
for (int t = t0; t < tend; ++t) {
|
||||
const float x3 = in_x[(size_t)t * C + c];
|
||||
float y = x3 + sw[0] * x0 + sw[1] * x1 + sw[2] * x2 + sw[3] * x3;
|
||||
if (has_dyn) {
|
||||
const float* dt = dyn_taps + ((size_t)t * n_heads + head) * 4;
|
||||
y += dt[0] * x3 + dt[1] * x2 + dt[2] * x1 + dt[3] * x0;
|
||||
y *= (1.0f / 3.0f);
|
||||
} else {
|
||||
y *= 0.44721359549995793f; // 1/sqrt(5)
|
||||
}
|
||||
out_y[(size_t)t * C + c] = y;
|
||||
x0 = x1; x1 = x2; x2 = bf16_round(x3);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_shortconv4_prefill_par(const float* in_x, const float* static_w,
|
||||
const float* dyn_taps, const float* hist, float* out_y,
|
||||
int B, int C, int n_heads, vetta_stream_t stream) {
|
||||
if (B <= 0 || C <= 0) return 1;
|
||||
if (dyn_taps && (n_heads <= 0 || (C % n_heads) != 0)) return 1;
|
||||
dim3 grid((C + 255) / 256, (B + 15) / 16);
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_shortconv4_prefill_par<<<grid, 256, 0, s>>>(in_x, static_w, dyn_taps, hist, out_y, B, C, n_heads);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_shortconv4_update_hist: roll the 3-token history after a prefill chunk
|
||||
// Exact strata k2_shortconv4_update_hist: hist <- last 3 in_x (bf16-rounded),
|
||||
// with shift semantics for B<3. hist [3,C] f32 (bf16-rounded values).
|
||||
__global__ void k_shortconv4_update_hist(const float* __restrict__ in_x,
|
||||
float* __restrict__ hist, int B, int C) {
|
||||
const int c = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (c >= C) return;
|
||||
if (B >= 3) {
|
||||
hist[0 * C + c] = bf16_round(in_x[(size_t)(B - 3) * C + c]);
|
||||
hist[1 * C + c] = bf16_round(in_x[(size_t)(B - 2) * C + c]);
|
||||
hist[2 * C + c] = bf16_round(in_x[(size_t)(B - 1) * C + c]);
|
||||
} else if (B == 2) {
|
||||
hist[0 * C + c] = hist[2 * C + c];
|
||||
hist[1 * C + c] = bf16_round(in_x[0 * C + c]);
|
||||
hist[2 * C + c] = bf16_round(in_x[1 * C + c]);
|
||||
} else if (B == 1) {
|
||||
hist[0 * C + c] = hist[1 * C + c];
|
||||
hist[1 * C + c] = hist[2 * C + c];
|
||||
hist[2 * C + c] = bf16_round(in_x[0 * C + c]);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_shortconv4_update_hist(const float* in_x, float* hist, int B, int C, vetta_stream_t stream) {
|
||||
if (B <= 0 || C <= 0) return 1;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_shortconv4_update_hist<<<(C + 255) / 256, 256, 0, s>>>(in_x, hist, B, 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:
|
||||
|
||||
Reference in new issue
Block a user