[HIP][Docs] embed_bf16 + gemm_bf16 kernels (GPU-verified) + newcomer orientation guide
- kernels/hip: k_embed_bf16_gather (BF16 table -> f32 token rows, exact) and k_gemm_bf16 (y[M,N] = x[M,K] @ w[N,K]^T, BF16 in / f32 accumulate, correctness-first per-element; WMMA tiling is the later performance pass). C ABI + safe Rust wrappers (embed_bf16_gather, gemm_bf16) + DeviceBuf byte-level h2d/d2h. - Verified LIVE on gfx1150: embed exact, gemm max_rel_err 0.0 across (2,8,16)/(4,32,64)/(1,128,256), rms_norm ~1e-6. Three kernels now cover the trunk + projection compute path (embed, norm, gemm). - docs/ORIENTATION.md: a guide for newcomers arriving from vLLM/llama.cpp/ SGLang — what this engine is, the five ideas behind the mental-model shift (microkernel+services, models compiled to .vbundle, forward pass as a Lean-proven validated plan, proof-first, parity discipline), the crate map, the honest current state (CPU pipeline done, GPU begun, no token yet), and the ordered road to the first agent chat and beyond. Linked from README. Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS; lean proofs (4 files, 42 theorems) green; GPU parity 4/4 (--features hip).
This commit is contained in:
1 parent
bd0d4f2a63
commit
356f9c458e
5 files changed
+313
No files matched your search
@@ -32,6 +32,14 @@ mod ffi {
|
||||
x: *const c_float, w: *const c_float, y: *mut c_float,
|
||||
rows: c_int, d: c_int, eps: c_float, stream: *mut c_void,
|
||||
) -> c_int;
|
||||
pub fn vetta_embed_bf16_gather(
|
||||
table: *const u16, tokens: *const i32, out: *mut c_float,
|
||||
b: c_int, d: c_int, vocab: c_int, stream: *mut c_void,
|
||||
) -> c_int;
|
||||
pub fn vetta_gemm_bf16(
|
||||
x: *const u16, w: *const u16, y: *mut c_float,
|
||||
m: c_int, n: c_int, k: c_int, stream: *mut c_void,
|
||||
) -> c_int;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -101,6 +109,18 @@ impl DeviceBuf {
|
||||
if rc != 0 { return Err("d2h failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
pub fn h2d_bytes(&self, src: &[u8]) -> Result<(), String> {
|
||||
if src.len() > self.bytes { return Err("h2d overflow".into()); }
|
||||
let rc = unsafe { ffi::vetta_buf_h2d(self.ptr, src.as_ptr() as *const _, src.len()) };
|
||||
if rc != 0 { return Err("h2d failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
pub fn d2h_bytes(&self, dst: &mut [u8]) -> Result<(), String> {
|
||||
if dst.len() > self.bytes { return Err("d2h overflow".into()); }
|
||||
let rc = unsafe { ffi::vetta_buf_d2h(dst.as_mut_ptr() as *mut _, self.ptr, dst.len()) };
|
||||
if rc != 0 { return Err("d2h failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "hip")]
|
||||
@@ -121,6 +141,26 @@ pub fn rms_norm_f32(x: &DeviceBuf, w: Option<&DeviceBuf>, y: &DeviceBuf, rows: i
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Launch k_embed_bf16_gather: `b` token rows from BF16 table `[vocab, d]` → f32.
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn embed_bf16_gather(table: &DeviceBuf, tokens: &DeviceBuf, out: &DeviceBuf, b: i32, d: i32, vocab: i32) -> Result<(), String> {
|
||||
let rc = unsafe {
|
||||
ffi::vetta_embed_bf16_gather(table.ptr as *const u16, tokens.ptr as *const i32, out.ptr as *mut f32, b, d, vocab, std::ptr::null_mut())
|
||||
};
|
||||
if rc != 0 { return Err("embed launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Launch k_gemm_bf16: y[m,n] = x[m,k] @ w[n,k]^T (BF16 in, f32 accumulate).
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn gemm_bf16(x: &DeviceBuf, w: &DeviceBuf, y: &DeviceBuf, m: i32, n: i32, k: i32) -> Result<(), String> {
|
||||
let rc = unsafe {
|
||||
ffi::vetta_gemm_bf16(x.ptr as *const u16, w.ptr as *const u16, y.ptr as *mut f32, m, n, k, std::ptr::null_mut())
|
||||
};
|
||||
if rc != 0 { return Err("gemm launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "hip"))]
|
||||
mod gpu_tests {
|
||||
use super::*;
|
||||
@@ -173,4 +213,66 @@ mod gpu_tests {
|
||||
assert!(max_rel < 1e-4, "rms_norm parity failed: {max_rel}");
|
||||
}
|
||||
}
|
||||
|
||||
fn f32_to_bf16(v: f32) -> u16 { (v.to_bits() >> 16) as u16 }
|
||||
fn bf16_to_f32(h: u16) -> f32 { f32::from_bits((h as u32) << 16) }
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn embed_bf16_parity_exact() {
|
||||
let (vocab, d, b) = (16usize, 64usize, 3usize);
|
||||
let table: Vec<u16> = (0..vocab * d).map(|i| f32_to_bf16(((i % 53) as f32 - 26.0) * 0.07)).collect();
|
||||
let tokens: Vec<i32> = vec![1, 5, 15];
|
||||
let mut want = vec![0f32; b * d];
|
||||
for (bi, &tok) in tokens.iter().enumerate() {
|
||||
for i in 0..d { want[bi * d + i] = bf16_to_f32(table[tok as usize * d + i]); }
|
||||
}
|
||||
let dt = DeviceBuf::alloc(table.len() * 2).unwrap();
|
||||
let dk = DeviceBuf::alloc(tokens.len() * 4).unwrap();
|
||||
let dy = DeviceBuf::alloc(want.len() * 4).unwrap();
|
||||
dt.h2d_bytes(bytemuck_u16(&table)).unwrap();
|
||||
dk.h2d_bytes(bytemuck_i32(&tokens)).unwrap();
|
||||
embed_bf16_gather(&dt, &dk, &dy, b as i32, d as i32, vocab as i32).unwrap();
|
||||
let mut got = vec![0f32; want.len()];
|
||||
dy.d2h(&mut got).unwrap();
|
||||
assert_eq!(got, want, "embed gather must be exact");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn gemm_bf16_parity_vs_cpu() {
|
||||
for &(m, n, k) in &[(2usize, 8usize, 16usize), (4, 32, 64), (1, 128, 256)] {
|
||||
let x: Vec<u16> = (0..m * k).map(|i| f32_to_bf16(((i % 41) as f32 - 20.0) * 0.05)).collect();
|
||||
let w: Vec<u16> = (0..n * k).map(|i| f32_to_bf16(((i % 29) as f32 - 14.0) * 0.06)).collect();
|
||||
let mut want = vec![0f32; m * n];
|
||||
for mi in 0..m {
|
||||
for ni in 0..n {
|
||||
let mut acc = 0f32;
|
||||
for kk in 0..k { acc += bf16_to_f32(x[mi * k + kk]) * bf16_to_f32(w[ni * k + kk]); }
|
||||
want[mi * n + ni] = acc;
|
||||
}
|
||||
}
|
||||
let dx = DeviceBuf::alloc(x.len() * 2).unwrap();
|
||||
let dw = DeviceBuf::alloc(w.len() * 2).unwrap();
|
||||
let dy = DeviceBuf::alloc(want.len() * 4).unwrap();
|
||||
dx.h2d_bytes(bytemuck_u16(&x)).unwrap();
|
||||
dw.h2d_bytes(bytemuck_u16(&w)).unwrap();
|
||||
gemm_bf16(&dx, &dw, &dy, m as i32, n as i32, k as i32).unwrap();
|
||||
let mut got = vec![0f32; want.len()];
|
||||
dy.d2h(&mut got).unwrap();
|
||||
let mut max_rel = 0f32;
|
||||
for (g, wnt) in got.iter().zip(want.iter()) {
|
||||
max_rel = max_rel.max((g - wnt).abs() / wnt.abs().max(1e-3));
|
||||
}
|
||||
eprintln!("gemm m={m} n={n} k={k} max_rel_err={max_rel:.3e}");
|
||||
assert!(max_rel < 1e-3, "gemm 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) }
|
||||
}
|
||||
fn bytemuck_i32(v: &[i32]) -> &[u8] {
|
||||
unsafe { std::slice::from_raw_parts(v.as_ptr() as *const u8, v.len() * 4) }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user