[HIP] SiTU interleaved gate/up + F32 router: generation matches reference
Root-cause fixes that make Inkling-2-Mini generation match strata-inkling: - k_situ: w13 rows are INTERLEAVED gate/up (row 2j=gate[j], 2j+1=up[j]), exact strata k_moe_direct_w13_situ. Was reading block layout gu[j]/gu[inter+j] -> sign-flipped y_part. Now gu[2j]/gu[2j+1]. - k_router_gemv (new): B=1 router reads F32 mlpin @ bf16 gate promoted to f32 (strata launch_f32_linear). bf16-rounding the router input flipped top-k expert selection. Used in both layer_forward_swa/full. - situ_cpu oracle + situ_parity_vs_cpu updated to interleaved. - real_full_layer/full_attn CPU oracle: router reads F32 mlpin (was bf16) -> experts now match GPU exactly. - real_attention_block parity: allclose (atol 2e-3 + rtol 3e-3). Stage bisection proved aout matches CPU to <1e-6 abs; pure-relative metric exploded only at near-cancellation O-proj elements after bf16 cast (benign accumulation artifact). - gated VETTA_DBG_NORM per-visit norm dumps for future bisection. Verified: 'The capital of France is' -> ' Paris.' (152717, exact ref). cargo test --workspace green (-D warnings), 17 ld GPU + 17 hip GPU green, 5 Lean files 0 sorryAx.
This commit is contained in:
1 parent
df120e6f5f
commit
08514e81b1
5 files changed
+114
-22
No files matched your search
@@ -45,6 +45,11 @@ int vetta_embed_bf16_gather(const uint16_t* table, const int32_t* tokens,
|
||||
int vetta_gemm_bf16(const uint16_t* x, const uint16_t* w, float* y,
|
||||
int M, int N, int K, vetta_stream_t stream);
|
||||
|
||||
// k_router_gemv (M=1): y[N] = x_f32[K] @ w_bf16[N,K]^T, f32 accumulate. The
|
||||
// router reads F32 mlpin (exact strata B=1 launch_f32_linear), not bf16.
|
||||
int vetta_router_gemv(const float* x, const uint16_t* w, float* y,
|
||||
int N, int K, vetta_stream_t stream);
|
||||
|
||||
// k_flash_gqa_decode (base): single-token GQA attention over a f32 KV cache.
|
||||
int vetta_flash_gqa_decode(const float* q, const float* k, const float* v,
|
||||
float* out, int n_head, int n_kv, int seq,
|
||||
|
||||
@@ -432,6 +432,37 @@ extern "C" int vetta_vec_add(const float* a, const float* b, float* y, size_t n,
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_router_gemv: y[N] = x_f32[K] @ w_bf16[N,K]^T, f32 accumulate --------
|
||||
// Exact strata B=1 router (launch_f32_linear): the router reads the F32 mlpin
|
||||
// (NOT bf16-rounded) against the bf16 gate weight promoted to f32. Rounding the
|
||||
// router input to bf16 flips top-k expert selection -> large MoE divergence.
|
||||
|
||||
__global__ void k_router_gemv(const float* __restrict__ x,
|
||||
const uint16_t* __restrict__ w,
|
||||
float* __restrict__ y, int N, int K) {
|
||||
const int n = blockIdx.x;
|
||||
if (n >= N) return;
|
||||
const int tid = threadIdx.x;
|
||||
const int nt = blockDim.x;
|
||||
const uint16_t* wr = w + (size_t)n * K;
|
||||
float acc = 0.f;
|
||||
for (int kk = tid; kk < K; kk += nt) acc += x[kk] * bf16_to_f32(wr[kk]);
|
||||
__shared__ float red[256];
|
||||
red[tid] = acc;
|
||||
__syncthreads();
|
||||
for (int s = nt / 2; s > 0; s >>= 1) { if (tid < s) red[tid] += red[tid + s]; __syncthreads(); }
|
||||
if (tid == 0) y[n] = red[0];
|
||||
}
|
||||
|
||||
extern "C" int vetta_router_gemv(const float* x, const uint16_t* w, float* y,
|
||||
int N, int K, vetta_stream_t stream) {
|
||||
if (N <= 0 || K <= 0) return 1;
|
||||
hipStream_t s = stream ? (hipStream_t)stream : (hipStream_t)0;
|
||||
k_router_gemv<<<N, 256, 0, s>>>(x, w, y, N, K);
|
||||
HIP_CHECK(hipGetLastError());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ---- k_cast_f32_bf16: f32 activation -> BF16 (truncate) before GEMM --------
|
||||
// The residual stream is f32; trunk/expert GEMMs read BF16 activations (the
|
||||
// reference inserts k_f32_to_bf16/f16 before the matmul). Truncation matches
|
||||
@@ -583,7 +614,8 @@ extern "C" int vetta_mxfp8_gemv(const uint8_t* weights, const uint8_t* scales,
|
||||
}
|
||||
|
||||
// ---- SiTU activation (exact strata situ_f32, double-softcapped) ------------
|
||||
// gu = [gate(inter); up(inter)] from w13. act[j] = g_act * u_act where
|
||||
// gu rows are INTERLEAVED gate/up (exact strata k_moe_direct_w13_situ:
|
||||
// row 2j=gate[j], row 2j+1=up[j]). act[j] = g_act * u_act where
|
||||
// g_act = beta*tanh(g/beta)*sigmoid(g), u_act = lb*tanh(u/lb).
|
||||
|
||||
__device__ __forceinline__ float situ_f32(float g, float u, float beta, float lb) {
|
||||
@@ -596,7 +628,7 @@ __device__ __forceinline__ float situ_f32(float g, float u, float beta, float lb
|
||||
__global__ void k_situ(const float* __restrict__ gu, float* __restrict__ act,
|
||||
int inter, float beta, float lb) {
|
||||
int j = blockIdx.x * blockDim.x + threadIdx.x;
|
||||
if (j < inter) act[j] = situ_f32(gu[j], gu[inter + j], beta, lb);
|
||||
if (j < inter) act[j] = situ_f32(gu[2 * j], gu[2 * j + 1], beta, lb);
|
||||
}
|
||||
|
||||
extern "C" int vetta_situ(const float* gu, float* act, int inter, float beta,
|
||||
|
||||
Reference in new issue
Block a user