[HIP] ShortConv4 decode kernel (Inkling causal depthwise conv + history ring) verified
- k_shortconv4_decode / vetta_shortconv4_decode: exact strata k2_shortconv4_batched static (no-dyn) semantics — per channel y = x0 + sw0*h0 + sw1*h1 + sw2*h2 + sw3*x0, scaled by 1/sqrt(5); the 3-tap history ring (t-3,t-2,t-1) shifts h0<-h1<-h2<-x0 in place. This is a defining Inkling mechanism (applied to k/v/attn/mlp). f32 history here; the BF16 ring for bit-exact CPU match is a later parity refinement. - Multi-step parity test: 6-token recurrence vs a CPU oracle, per-step output match (<1e-5) AND device history ring == host history ring after all steps (exact). The stateful short-conv evolves correctly across tokens. Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS; lean proofs (4 files, 42 theorems) green; GPU 11/11 (--features hip).
This commit is contained in:
1 parent
00a6993c50
commit
e060862660
3 files changed
+96
No files matched your search
@@ -56,6 +56,7 @@ mod ffi {
|
|||||||
) -> c_int;
|
) -> c_int;
|
||||||
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_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_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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -240,6 +241,14 @@ pub fn moe_reduce(y_part: &DeviceBuf, tk_w: &DeviceBuf, out: &DeviceBuf, top_k:
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Launch k_shortconv4_decode (static): updates `hist` ring in place.
|
||||||
|
#[cfg(feature = "hip")]
|
||||||
|
pub fn shortconv4_decode(cur: &DeviceBuf, static_w: &DeviceBuf, hist: &DeviceBuf, out: &DeviceBuf, slot_ids: &DeviceBuf, m: i32, c: i32) -> Result<(), String> {
|
||||||
|
let rc = unsafe { ffi::vetta_shortconv4_decode(cur.ptr as *const f32, static_w.ptr as *const f32, hist.ptr as *mut f32, out.ptr as *mut f32, slot_ids.ptr as *const i32, m, c, std::ptr::null_mut()) };
|
||||||
|
if rc != 0 { return Err("shortconv4 launch failed".into()); }
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(all(test, feature = "hip"))]
|
#[cfg(all(test, feature = "hip"))]
|
||||||
mod gpu_tests {
|
mod gpu_tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -566,6 +575,46 @@ mod gpu_tests {
|
|||||||
assert!(max_rel < 1e-5, "moe_reduce parity failed: {max_rel}");
|
assert!(max_rel < 1e-5, "moe_reduce parity failed: {max_rel}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn shortconv4_multistep_parity() {
|
||||||
|
let c = 64usize;
|
||||||
|
let steps = 6usize;
|
||||||
|
let inv_sqrt5 = 0.44721359549995793f32;
|
||||||
|
let sw: Vec<f32> = (0..c * 4).map(|i| ((i % 17) as f32 - 8.0) * 0.05).collect();
|
||||||
|
let mut hist: Vec<f32> = (0..3 * c).map(|i| ((i % 11) as f32 - 5.0) * 0.1).collect();
|
||||||
|
let xs: Vec<Vec<f32>> = (0..steps).map(|t| (0..c).map(|i| ((i + t * 7) % 23) as f32 * 0.03 - 0.3).collect()).collect();
|
||||||
|
|
||||||
|
let dsw = DeviceBuf::alloc(sw.len() * 4).unwrap(); dsw.h2d(&sw).unwrap();
|
||||||
|
let dh = DeviceBuf::alloc(hist.len() * 4).unwrap(); dh.h2d(&hist).unwrap();
|
||||||
|
let dcur = DeviceBuf::alloc(c * 4).unwrap();
|
||||||
|
let dout = DeviceBuf::alloc(c * 4).unwrap();
|
||||||
|
let dslot = DeviceBuf::alloc(4).unwrap(); dslot.h2d_bytes(&0i32.to_le_bytes()).unwrap();
|
||||||
|
|
||||||
|
for t in 0..steps {
|
||||||
|
// CPU oracle step (hist evolves on host).
|
||||||
|
let mut want = vec![0f32; c];
|
||||||
|
for ch in 0..c {
|
||||||
|
let h0 = hist[ch]; let h1 = hist[c + ch]; let h2 = hist[2 * c + ch];
|
||||||
|
let x0 = xs[t][ch];
|
||||||
|
let w = &sw[ch * 4..ch * 4 + 4];
|
||||||
|
want[ch] = (x0 + w[0] * h0 + w[1] * h1 + w[2] * h2 + w[3] * x0) * inv_sqrt5;
|
||||||
|
}
|
||||||
|
for ch in 0..c { hist[ch] = hist[c + ch]; hist[c + ch] = hist[2 * c + ch]; hist[2 * c + ch] = xs[t][ch]; }
|
||||||
|
// GPU step (hist evolves on device).
|
||||||
|
dcur.h2d(&xs[t]).unwrap();
|
||||||
|
shortconv4_decode(&dcur, &dsw, &dh, &dout, &dslot, 1, c as i32).unwrap();
|
||||||
|
let mut got = vec![0f32; c]; 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)); }
|
||||||
|
assert!(max_rel < 1e-5, "shortconv4 step {t} parity failed: {max_rel}");
|
||||||
|
}
|
||||||
|
// device history must match host history after all steps.
|
||||||
|
let mut dh_host = vec![0f32; 3 * c]; dh.d2h(&mut dh_host).unwrap();
|
||||||
|
assert_eq!(dh_host, hist, "shortconv4 history ring must match");
|
||||||
|
eprintln!("shortconv4 {steps}-step recurrence + history ring: exact");
|
||||||
|
}
|
||||||
|
|
||||||
fn bytemuck_u16(v: &[u16]) -> &[u8] {
|
fn bytemuck_u16(v: &[u16]) -> &[u8] {
|
||||||
unsafe { std::slice::from_raw_parts(v.as_ptr() as *const u8, v.len() * 2) }
|
unsafe { std::slice::from_raw_parts(v.as_ptr() as *const u8, v.len() * 2) }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,6 +78,12 @@ int vetta_situ(const float* gu, float* act, int inter, float beta, float lb,
|
|||||||
int vetta_moe_reduce(const float* y_part, const float* tk_w, float* out,
|
int vetta_moe_reduce(const float* y_part, const float* tk_w, float* out,
|
||||||
int top_k, int hidden, vetta_stream_t stream);
|
int top_k, int hidden, vetta_stream_t stream);
|
||||||
|
|
||||||
|
// k_shortconv4_decode: Inkling causal depthwise ShortConv4 (static), history
|
||||||
|
// ring [slots,3,C] shifted in place. cur/out [M,C], static_w [C,4].
|
||||||
|
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);
|
||||||
|
|
||||||
// Core <15 microkernel entry points (implemented progressively in M1 & M2):
|
// Core <15 microkernel entry points (implemented progressively in M1 & M2):
|
||||||
// 1. k_embed_bf16
|
// 1. k_embed_bf16
|
||||||
// 2. k_rms_norm
|
// 2. k_rms_norm
|
||||||
|
|||||||
@@ -468,4 +468,45 @@ extern "C" int vetta_moe_reduce(const float* y_part, const float* tk_w, float* o
|
|||||||
k_moe_reduce<<<(hidden + nt - 1) / nt, nt, 0, s>>>(y_part, tk_w, out, top_k, hidden);
|
k_moe_reduce<<<(hidden + nt - 1) / nt, nt, 0, s>>>(y_part, tk_w, out, top_k, hidden);
|
||||||
HIP_CHECK(hipGetLastError());
|
HIP_CHECK(hipGetLastError());
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---- k_shortconv4_decode: Inkling causal depthwise ShortConv (kernel 4) ----
|
||||||
|
// Exact strata k2_shortconv4_batched (static, no dyn): per channel c, token t,
|
||||||
|
// y = x0 + sw0*h0 + sw1*h1 + sw2*h2 + sw3*x0; y *= 1/sqrt(5)
|
||||||
|
// history ring (t-3,t-2,t-1) shifts h0<-h1<-h2<-x0. f32 history here; the
|
||||||
|
// reference uses a BF16 ring for bit-exact CPU match (a parity refinement).
|
||||||
|
|
||||||
|
__global__ void k_shortconv4_decode(const float* __restrict__ cur,
|
||||||
|
const float* __restrict__ static_w,
|
||||||
|
float* __restrict__ hist,
|
||||||
|
float* __restrict__ out,
|
||||||
|
const int32_t* __restrict__ slot_ids,
|
||||||
|
int M, int C) {
|
||||||
|
const int c = blockIdx.x * blockDim.x + threadIdx.x;
|
||||||
|
const int m = blockIdx.y;
|
||||||
|
if (c >= C || m >= M) return;
|
||||||
|
const int slot = slot_ids[m];
|
||||||
|
float* h = hist + (size_t)slot * 3 * C;
|
||||||
|
const float h0 = h[0 * C + c];
|
||||||
|
const float h1 = h[1 * C + c];
|
||||||
|
const float h2 = h[2 * C + c];
|
||||||
|
const float x0 = cur[(size_t)m * C + c];
|
||||||
|
const float* sw = static_w + (size_t)c * 4;
|
||||||
|
float y = x0 + sw[0] * h0 + sw[1] * h1 + sw[2] * h2 + sw[3] * x0;
|
||||||
|
y *= 0.44721359549995793f; // 1/sqrt(5)
|
||||||
|
h[0 * C + c] = h1;
|
||||||
|
h[1 * C + c] = h2;
|
||||||
|
h[2 * C + c] = x0;
|
||||||
|
out[(size_t)m * C + c] = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" 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) {
|
||||||
|
if (M <= 0 || C <= 0) return 1;
|
||||||
|
dim3 grid((C + 255) / 256, M);
|
||||||
|
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||||
|
k_shortconv4_decode<<<grid, 256, 0, s>>>(cur, static_w, hist, out, slot_ids, M, C);
|
||||||
|
HIP_CHECK(hipGetLastError());
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user