[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:
1 parent
edcfab6548
commit
1221ac094a
4 files changed
+109
-18
No files matched your search
@@ -110,6 +110,13 @@ __device__ __forceinline__ float bf16_to_f32(uint16_t h) {
|
||||
return __uint_as_float(((uint32_t)h) << 16);
|
||||
}
|
||||
|
||||
// round-to-nearest-even f32 -> bf16 -> f32 (exact strata f32_to_bf16 round trip).
|
||||
__device__ __forceinline__ float bf16_round(float f) {
|
||||
const uint32_t u = __float_as_uint(f);
|
||||
const uint32_t r = 0x7FFFu + ((u >> 16) & 1u);
|
||||
return __uint_as_float(((u + r) >> 16) << 16);
|
||||
}
|
||||
|
||||
// ---- k_embed_bf16 (kernel #1): gather token rows from a BF16 table ---------
|
||||
// out[b, :] = bf16_to_f32(table[token[b], :]). Exact (no arithmetic).
|
||||
|
||||
@@ -433,7 +440,12 @@ extern "C" int vetta_vec_add(const float* a, const float* b, float* y, size_t n,
|
||||
__global__ void k_cast_f32_bf16(const float* __restrict__ x, uint16_t* __restrict__ y,
|
||||
size_t n) {
|
||||
size_t i = (size_t)blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (i < n) y[i] = (uint16_t)(__float_as_uint(x[i]) >> 16);
|
||||
if (i < n) {
|
||||
// round-to-nearest-even (exact strata f32_to_bf16), NOT truncation.
|
||||
const uint32_t u = __float_as_uint(x[i]);
|
||||
const uint32_t rounding = 0x7FFFu + ((u >> 16) & 1u);
|
||||
y[i] = (uint16_t)((u + rounding) >> 16);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" int vetta_cast_f32_bf16(const float* x, uint16_t* y, size_t n,
|
||||
@@ -644,9 +656,9 @@ __global__ void k_shortconv4_decode(const float* __restrict__ cur,
|
||||
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;
|
||||
h[0 * C + c] = bf16_round(h1);
|
||||
h[1 * C + c] = bf16_round(h2);
|
||||
h[2 * C + c] = bf16_round(x0);
|
||||
out[(size_t)m * C + c] = y;
|
||||
}
|
||||
|
||||
@@ -688,9 +700,9 @@ __global__ void k_shortconv4_dyn_decode(const float* __restrict__ cur,
|
||||
const float* dt = dyn_taps + ((size_t)m * n_heads + head) * 4;
|
||||
y += dt[0] * x0 + dt[1] * h2 + dt[2] * h1 + dt[3] * h0;
|
||||
y *= (1.0f / 3.0f);
|
||||
h[0 * C + c] = h1;
|
||||
h[1 * C + c] = h2;
|
||||
h[2 * C + c] = x0;
|
||||
h[0 * C + c] = bf16_round(h1);
|
||||
h[1 * C + c] = bf16_round(h2);
|
||||
h[2 * C + c] = bf16_round(x0);
|
||||
out[(size_t)m * C + c] = y;
|
||||
}
|
||||
|
||||
@@ -929,8 +941,10 @@ __global__ void k_ctrl_pack(const float* __restrict__ cur, const float* __restri
|
||||
__syncthreads();
|
||||
const float ic = inv_c_s, im = inv_m_s;
|
||||
for (int d = tid; d < D; d += nt) {
|
||||
packed[d] = (uint16_t)(__float_as_uint(cur[d] * ic) >> 16);
|
||||
packed[D + d] = (uint16_t)(__float_as_uint(msg[d] * im) >> 16);
|
||||
const uint32_t uc = __float_as_uint(cur[d] * ic);
|
||||
const uint32_t um = __float_as_uint(msg[d] * im);
|
||||
packed[d] = (uint16_t)((uc + 0x7FFFu + ((uc >> 16) & 1u)) >> 16);
|
||||
packed[D + d] = (uint16_t)((um + 0x7FFFu + ((um >> 16) & 1u)) >> 16);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user