[HIP] BF16 round-to-nearest-even parity (cast, ctrl pack, shortconv history)

Reference comparison (strata-inkling --serve ground truth) exposed a systematic
precision bug: vetta truncated f32->bf16 (>>16) while the reference f32_to_bf16
rounds to nearest even (0x7FFF + ((u>>16)&1)). Truncation bias accumulates over
the 48-visit loop and drifts the residual stream.

- k_cast_f32_bf16: round-to-nearest-even (was truncation).
- k_ctrl_pack: packed cur/msg bf16 now round-to-nearest-even.
- k_shortconv4_decode / k_shortconv4_dyn_decode: history ring rounded through
  bf16 between steps (exact strata "Round through BF16 between steps"), via a
  new bf16_round device helper.
- CPU oracles updated to match (packer bf16_trunc, hip f32_to_bf16 + the
  shortconv/controller test history shifts).

Effect on the full forward (single token 100, reference argmax 68014):
argmax moved 100 -> 8 and logit[68014] rose 8.6 -> 11.1 (toward the reference),
confirming the rounding fix is real and directional. Generation from "The
capital of France is" (prompt ids verified identical to the reference) now
produces structured text but the argmax still diverges from the reference
" Paris" — further precision/composition parity is in progress (reference uses
fp16 KV cache + fp16 expert input; candidate next mismatches).

Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS;
GPU 17/17 (--features hip, --ignored) green with the rounded oracles;
lean proofs (5 files) green.
This commit is contained in:
adm committed 2026-12-01 15:43:31 +01:00
1 parent edcfab6548
commit 1221ac094a
4 files changed
+109 -18

No files matched your search

+5 -5
View File
@@ -237,7 +237,7 @@ pub fn vec_add(a: &DeviceBuf, b: &DeviceBuf, y: &DeviceBuf, n: usize) -> Result<
Ok(())
}
/// Launch k_cast_f32_bf16: f32 activation -> BF16 (truncate).
/// Launch k_cast_f32_bf16: f32 activation -> BF16 (round-to-nearest-even).
#[cfg(feature = "hip")]
pub fn cast_f32_bf16(x: &DeviceBuf, y: &DeviceBuf, n: usize) -> Result<(), String> {
let rc = unsafe { ffi::vetta_cast_f32_bf16(x.ptr as *const f32, y.ptr as *mut u16, n, std::ptr::null_mut()) };
@@ -410,7 +410,7 @@ mod gpu_tests {
}
}
fn f32_to_bf16(v: f32) -> u16 { (v.to_bits() >> 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) }
#[test]
@@ -777,7 +777,7 @@ mod gpu_tests {
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]; }
for ch in 0..c { let h1 = hist[c + ch]; let h2 = hist[2 * c + ch]; hist[ch] = bf16_to_f32(f32_to_bf16(h1)); hist[c + ch] = bf16_to_f32(f32_to_bf16(h2)); hist[2 * c + ch] = bf16_to_f32(f32_to_bf16(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();
@@ -817,7 +817,7 @@ mod gpu_tests {
let y = x0 + w[0] * h0 + w[1] * h1 + w[2] * h2 + w[3] * x0 + dt[0] * x0 + dt[1] * h2 + dt[2] * h1 + dt[3] * h0;
want[ch] = y / 3.0;
}
for ch in 0..c { hist[ch] = hist[c + ch]; hist[c + ch] = hist[2 * c + ch]; hist[2 * c + ch] = xs[t][ch]; }
for ch in 0..c { let h1 = hist[c + ch]; let h2 = hist[2 * c + ch]; hist[ch] = bf16_to_f32(f32_to_bf16(h1)); hist[c + ch] = bf16_to_f32(f32_to_bf16(h2)); hist[2 * c + ch] = bf16_to_f32(f32_to_bf16(xs[t][ch])); }
dcur.h2d(&xs[t]).unwrap(); ddyn.h2d(&dyns[t]).unwrap();
shortconv4_dyn_decode(&dcur, &dsw, &ddyn, &dh, &dout, &dslot, 1, c as i32, n_heads as i32).unwrap();
let mut got = vec![0f32; c]; dout.d2h(&mut got).unwrap();
@@ -969,7 +969,7 @@ mod gpu_tests {
#[test]
#[ignore]
fn loop_controller_parity_vs_cpu() {
let bf = |x: f32| -> u16 { (x.to_bits() >> 16) as u16 };
let bf = |x: f32| -> u16 { let u = x.to_bits(); ((u + 0x7FFFu32 + ((u >> 16) & 1)) >> 16) as u16 };
let ubf = |h: u16| -> f32 { f32::from_bits((h as u32) << 16) };
let trunc = |x: f32| -> f32 { ubf(bf(x)) };
let (d, gr) = (64usize, 8usize);