[HIP] DSpark brick 4b: recurrent Markov head GRU + corr/combine kernels + parity

- k_dspark_markov_gru: proj[3*rank]+bias -> gate=sigmoid, cand=tanh,
  state[r]=cand+gate*(state[r]-cand), latent[r]=tanh (GRU-style recurrent head).
- k_dspark_corr_combine: combined = cur_step + corr_raw + hidden_out_bias.
- Host wrappers + parity: GRU 7.6e-7 over a 3-step recurrence (state carries),
  corr_combine bit-exact. hip 43/43; CPU -D warnings green.
This commit is contained in:
adm committed 2026-12-02 20:39:24 +01:00
1 parent 1b10258e6e
commit 142580e9ca
2 files changed
+132

No files matched your search

+81
View File
@@ -57,6 +57,14 @@ mod ffi {
g: *const c_float, u: *const c_float, out: *mut c_float, n: usize, g: *const c_float, u: *const c_float, out: *mut c_float, n: usize,
stream: *mut c_void, stream: *mut c_void,
) -> c_int; ) -> c_int;
pub fn vetta_dspark_markov_gru(
proj: *const c_float, bias: *const c_float, state: *mut c_float,
latent: *mut c_float, rank: c_int, stream: *mut c_void,
) -> c_int;
pub fn vetta_dspark_corr_combine(
cur: *const c_float, corr: *const c_float, bias: *const c_float,
out: *mut c_float, d: c_int, stream: *mut c_void,
) -> c_int;
pub fn vetta_embed_bf16_gather( pub fn vetta_embed_bf16_gather(
table: *const u16, tokens: *const i32, out: *mut c_float, table: *const u16, tokens: *const i32, out: *mut c_float,
b: c_int, d: c_int, vocab: c_int, stream: *mut c_void, b: c_int, d: c_int, vocab: c_int, stream: *mut c_void,
@@ -478,6 +486,23 @@ pub fn dspark_swiglu(g: &DeviceBuf, u: &DeviceBuf, out: &DeviceBuf, n: usize) ->
Ok(()) Ok(())
} }
/// DSpark Markov head GRU update: proj[3*rank]+bias -> state[rank] (in-place),
/// latent[rank]. gate=sigmoid, cand/latent=tanh.
#[cfg(feature = "hip")]
pub fn dspark_markov_gru(proj: &DeviceBuf, bias: &DeviceBuf, state: &DeviceBuf, latent: &DeviceBuf, rank: i32) -> Result<(), String> {
let rc = unsafe { ffi::vetta_dspark_markov_gru(proj.ptr as *const f32, bias.ptr as *const f32, state.ptr as *mut f32, latent.ptr as *mut f32, rank, std::ptr::null_mut()) };
if rc != 0 { return Err("dspark_markov_gru launch failed".into()); }
Ok(())
}
/// DSpark combined = cur_step + corr_raw + hidden_out_bias (D entries).
#[cfg(feature = "hip")]
pub fn dspark_corr_combine(cur: &DeviceBuf, corr: &DeviceBuf, bias: &DeviceBuf, out: &DeviceBuf, d: i32) -> Result<(), String> {
let rc = unsafe { ffi::vetta_dspark_corr_combine(cur.ptr as *const f32, corr.ptr as *const f32, bias.ptr as *const f32, out.ptr as *mut f32, d, std::ptr::null_mut()) };
if rc != 0 { return Err("dspark_corr_combine launch failed".into()); }
Ok(())
}
/// Launch k_embed_bf16_gather: `b` token rows from BF16 table `[vocab, d]` → f32. /// Launch k_embed_bf16_gather: `b` token rows from BF16 table `[vocab, d]` → f32.
#[cfg(feature = "hip")] #[cfg(feature = "hip")]
pub fn embed_bf16_gather(table: &DeviceBuf, tokens: &DeviceBuf, out: &DeviceBuf, b: i32, d: i32, vocab: i32) -> Result<(), String> { pub fn embed_bf16_gather(table: &DeviceBuf, tokens: &DeviceBuf, out: &DeviceBuf, b: i32, d: i32, vocab: i32) -> Result<(), String> {
@@ -1244,6 +1269,62 @@ mod gpu_tests {
assert!(max_rel < 1e-5, "dspark_swiglu parity failed: {max_rel}"); assert!(max_rel < 1e-5, "dspark_swiglu parity failed: {max_rel}");
} }
/// DSpark Markov GRU update over a 3-step recurrence (state carries over).
#[test]
#[ignore]
fn dspark_markov_gru_parity() {
let rank = 256usize;
let mut state = vec![0f32; rank];
let dstate = DeviceBuf::alloc(rank * 4).unwrap();
dstate.h2d(&state).unwrap();
let dlatent = DeviceBuf::alloc(rank * 4).unwrap();
let mut max_rel = 0f32;
for step in 0..3 {
let proj: Vec<f32> = (0..3 * rank).map(|i| ((i % 41 + step * 7) as f32 - 20.0) * 0.15).collect();
let bias: Vec<f32> = (0..3 * rank).map(|i| ((i % 13) as f32 - 6.0) * 0.05).collect();
let mut want_state = state.clone();
let mut want_latent = vec![0f32; rank];
for r in 0..rank {
let p0 = proj[r] + bias[r];
let p1 = proj[rank + r] + bias[rank + r];
let p2 = proj[2 * rank + r] + bias[2 * rank + r];
let g = 1.0f32 / (1.0f32 + (-p0).exp());
let cand = p1.tanh();
want_state[r] = cand + g * (want_state[r] - cand);
want_latent[r] = p2.tanh();
}
let dproj = DeviceBuf::alloc(proj.len() * 4).unwrap(); dproj.h2d(&proj).unwrap();
let dbias = DeviceBuf::alloc(bias.len() * 4).unwrap(); dbias.h2d(&bias).unwrap();
dspark_markov_gru(&dproj, &dbias, &dstate, &dlatent, rank as i32).unwrap();
let mut gs = vec![0f32; rank]; let mut gl = vec![0f32; rank];
dstate.d2h(&mut gs).unwrap(); dlatent.d2h(&mut gl).unwrap();
for (a, b) in gs.iter().zip(want_state.iter()) { max_rel = max_rel.max((a - b).abs() / b.abs().max(1e-3)); }
for (a, b) in gl.iter().zip(want_latent.iter()) { max_rel = max_rel.max((a - b).abs() / b.abs().max(1e-3)); }
state = gs;
}
eprintln!("dspark_markov_gru max_rel_err={max_rel:.3e}");
assert!(max_rel < 1e-5, "dspark_markov_gru parity failed: {max_rel}");
}
/// DSpark corr_combine: combined = cur + corr + bias (exact f32 add).
#[test]
#[ignore]
fn dspark_corr_combine_parity() {
let d = 2304usize;
let cur: Vec<f32> = (0..d).map(|i| ((i % 37) as f32 - 18.0) * 0.1).collect();
let corr: Vec<f32> = (0..d).map(|i| ((i % 29) as f32 - 14.0) * 0.05).collect();
let bias: Vec<f32> = (0..d).map(|i| ((i % 11) as f32 - 5.0) * 0.02).collect();
let want: Vec<f32> = (0..d).map(|i| cur[i] + corr[i] + bias[i]).collect();
let dc = DeviceBuf::alloc(d * 4).unwrap(); dc.h2d(&cur).unwrap();
let dr = DeviceBuf::alloc(d * 4).unwrap(); dr.h2d(&corr).unwrap();
let db = DeviceBuf::alloc(d * 4).unwrap(); db.h2d(&bias).unwrap();
let dy = DeviceBuf::alloc(d * 4).unwrap();
dspark_corr_combine(&dc, &dr, &db, &dy, d as i32).unwrap();
let mut got = vec![0f32; d];
dy.d2h(&mut got).unwrap();
assert_eq!(got, want, "corr_combine must be exact");
}
fn f32_to_bf16(v: f32) -> u16 { let u = v.to_bits(); ((u + 0x7FFFu32 + ((u >> 16) & 1)) >> 16) as u16 } fn f32_to_bf16(v: f32) -> u16 { let u = v.to_bits(); ((u + 0x7FFFu32 + ((u >> 16) & 1)) >> 16) as u16 }
fn bf16_to_f32(h: u16) -> f32 { f32::from_bits((h as u32) << 16) } fn bf16_to_f32(h: u16) -> f32 { f32::from_bits((h as u32) << 16) }
+51
View File
@@ -362,6 +362,57 @@ extern "C" int vetta_dspark_swiglu(const float* g, const float* u, float* out,
return 0; return 0;
} }
// DSpark recurrent Markov head GRU-style update. proj[3*rank] (already GEMV'd,
// bias added here): gate=sigmoid(proj[r]), cand=tanh(proj[rank+r]),
// state[r]=cand+gate*(state[r]-cand), latent[r]=tanh(proj[2*rank+r]).
__global__ void k_dspark_markov_gru(const float* __restrict__ proj,
const float* __restrict__ bias,
float* __restrict__ state,
float* __restrict__ latent, int rank) {
const int r = blockIdx.x * blockDim.x + threadIdx.x;
if (r >= rank) return;
const float p0 = proj[r] + bias[r];
const float p1 = proj[rank + r] + bias[rank + r];
const float p2 = proj[2 * rank + r] + bias[2 * rank + r];
const float g = 1.f / (1.f + expf(-p0));
const float cand = tanhf(p1);
state[r] = cand + g * (state[r] - cand);
latent[r] = tanhf(p2);
}
extern "C" int vetta_dspark_markov_gru(const float* proj, const float* bias,
float* state, float* latent, int rank,
vetta_stream_t stream) {
if (rank <= 0) return 1;
const int t = 256;
const int b = (rank + t - 1) / t;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_dspark_markov_gru<<<b, t, 0, s>>>(proj, bias, state, latent, rank);
HIP_CHECK(hipGetLastError());
return 0;
}
// combined[d] = cur_step[d] + corr_raw[d] + hidden_out_bias[d] (corr = how@latent).
__global__ void k_dspark_corr_combine(const float* __restrict__ cur,
const float* __restrict__ corr,
const float* __restrict__ bias,
float* __restrict__ out, int D) {
const int d = blockIdx.x * blockDim.x + threadIdx.x;
if (d < D) out[d] = cur[d] + corr[d] + bias[d];
}
extern "C" int vetta_dspark_corr_combine(const float* cur, const float* corr,
const float* bias, float* out, int D,
vetta_stream_t stream) {
if (D <= 0) return 1;
const int t = 256;
const int b = (D + t - 1) / t;
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
k_dspark_corr_combine<<<b, t, 0, s>>>(cur, corr, bias, out, D);
HIP_CHECK(hipGetLastError());
return 0;
}
// ---- BF16 helpers ---------------------------------------------------------- // ---- BF16 helpers ----------------------------------------------------------
__device__ __forceinline__ float bf16_to_f32(uint16_t h) { __device__ __forceinline__ float bf16_to_f32(uint16_t h) {