[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
@@ -180,9 +180,11 @@ pub fn layer_forward_swa(
|
||||
let lim = (pos + 1).min(d.rel_extent as i32);
|
||||
vetta_infer_hip::relpos_bias(&r_raw, &w.rel_proj, &rel_bias, d.n_head as i32, d.d_rel as i32, d.rel_extent as i32, lim)?;
|
||||
vetta_infer_hip::kv_store(&k_cv, &v_cv, &st.k_cache, &st.v_cache, pos, d.n_kv as i32, d.hd as i32, st.max_seq as i32, d.eps)?;
|
||||
if w.ip == 0 { dbg_norm("q", &q, q_dim); dbg_norm("k_cv", &k_cv, kv_dim); dbg_norm("v_cv", &v_cv, kv_dim); dbg_norm("r_raw", &r_raw, d.n_head * d.d_rel); }
|
||||
|
||||
// k3: flash SWA GQA.
|
||||
vetta_infer_hip::flash_swa_gqa_decode(&q, &st.k_cache, &st.v_cache, &aout, d.n_head as i32, d.n_kv as i32, st.max_seq as i32, d.hd as i32, d.scale(), d.swa_win, pos, Some(&rel_bias), d.rel_extent as i32, Some(&sinks), Some(&gatea))?;
|
||||
if w.ip == 0 { dbg_norm("aout", &aout, q_dim); dbg_norm("sinks", &sinks, d.n_head); dbg_norm("gatea", &gatea, q_dim); }
|
||||
|
||||
// k4: O-proj + attn shortconv + block boundary.
|
||||
vetta_infer_hip::cast_f32_bf16(&aout, &aout_bf, q_dim)?;
|
||||
@@ -197,6 +199,7 @@ pub fn layer_forward_swa(
|
||||
} else {
|
||||
vetta_infer_hip::vec_add(cur, &asc, cur, h)?;
|
||||
}
|
||||
if w.ip == 0 { dbg_norm("cur_after_attn", cur, h); dbg_norm("asc", &asc, h); }
|
||||
|
||||
// k5: MoE input = mlp_norm(mix(blocks, cur, mlp_query)).
|
||||
let n_blocks = blocks.len();
|
||||
@@ -208,15 +211,15 @@ pub fn layer_forward_swa(
|
||||
vetta_infer_hip::attn_res_mix(&blk, cur, &w.mlp_query, &mix, n_blocks as i32, 1, 1, h as i32, d.eps)?;
|
||||
let mlpin = DeviceBuf::alloc(h * 4)?;
|
||||
vetta_infer_hip::rms_norm_f32(&mix, Some(&w.mlp_norm), &mlpin, 1, h as i32, d.eps)?;
|
||||
if w.ip == 0 { dbg_norm("mlpin", &mlpin, h); dbg_norm("moe_mix", &mix, h); }
|
||||
if let Some((cw, msgs)) = ctrl {
|
||||
apply_controller(&mlpin, &msgs.channel(3, h)?, cw, 3, h, 64, d.eps)?;
|
||||
}
|
||||
|
||||
// k6: router + experts + reduce.
|
||||
let mlpin_bf = DeviceBuf::alloc(h * 2)?;
|
||||
vetta_infer_hip::cast_f32_bf16(&mlpin, &mlpin_bf, h)?;
|
||||
// k6: router + experts + reduce. Router reads F32 mlpin (exact strata B=1
|
||||
// launch_f32_linear); bf16-rounding the router input flips top-k selection.
|
||||
let rlog = DeviceBuf::alloc(d.n_experts * 4)?;
|
||||
vetta_infer_hip::gemm_bf16(&mlpin_bf, &w.gate_w, &rlog, 1, d.n_experts as i32, h as i32)?;
|
||||
vetta_infer_hip::router_gemv(&mlpin, &w.gate_w, &rlog, d.n_experts as i32, h as i32)?;
|
||||
let ids = DeviceBuf::alloc(d.top_k * 4)?;
|
||||
let weights = DeviceBuf::alloc(d.top_k * 4)?;
|
||||
vetta_infer_hip::moe_router_topk(&rlog, Some(&w.gate_b), &ids, &weights, 1, d.n_experts as i32, d.top_k as i32, d.route_scale)?;
|
||||
@@ -238,6 +241,7 @@ pub fn layer_forward_swa(
|
||||
let y_part = DeviceBuf::alloc(d.top_k * h * 4)?; y_part.h2d(&y_part_h)?;
|
||||
let moeout = DeviceBuf::alloc(h * 4)?;
|
||||
vetta_infer_hip::moe_reduce(&y_part, &weights, &moeout, d.top_k as i32, h as i32)?;
|
||||
if w.ip == 0 { dbg_norm("y_part", &y_part, d.top_k * h); dbg_norm("moeout", &moeout, h); }
|
||||
|
||||
// k7: mlp shortconv + plain residual add.
|
||||
let msc = DeviceBuf::alloc(h * 4)?;
|
||||
@@ -326,6 +330,13 @@ fn load_msg_queries(ar: &SafetensorsArchive, ip: usize, hidden: usize) -> Result
|
||||
|
||||
/// Run one physical layer step (SWA or full-attn dispatch), reusing its
|
||||
/// persistent state (KV cache + shortconv history) across loop passes.
|
||||
fn dbg_norm(name: &str, buf: &DeviceBuf, n: usize) {
|
||||
if std::env::var("VETTA_DBG_NORM").is_err() { return; }
|
||||
let mut h = vec![0f32; n]; buf.d2h(&mut h).unwrap();
|
||||
let ss = h.iter().map(|v| v * v).sum::<f32>().sqrt();
|
||||
eprintln!("[NORM] v0 {name} norm={ss:.6} h0={:.6} h1={:.6}", h[0], h[1]);
|
||||
}
|
||||
|
||||
fn run_layer(
|
||||
ar: &SafetensorsArchive, d: &Dims, ip: usize, cur: &DeviceBuf, blocks: &mut Vec<DeviceBuf>,
|
||||
states: &mut [Option<LayerState>], max_seq: usize, pos: i32, pass: usize,
|
||||
@@ -333,13 +344,20 @@ fn run_layer(
|
||||
) -> Result<(), String> {
|
||||
if states[ip].is_none() { states[ip] = Some(new_state(d, max_seq)?); }
|
||||
let st = states[ip].as_mut().unwrap();
|
||||
if is_swa(ip) {
|
||||
let r = if is_swa(ip) {
|
||||
let w = load_layer(ar, ip, true)?;
|
||||
layer_forward_swa(ar, &w, st, d, cur, blocks, pos, pass, ctrl)
|
||||
} else {
|
||||
let w = load_full_layer(ar, ip, d)?;
|
||||
layer_forward_full(ar, &w, st, d, cur, blocks, pos, pass, ctrl)
|
||||
};
|
||||
if std::env::var("VETTA_DBG_NORM").is_ok() {
|
||||
let visit = if ip < 6 { ip } else if ip < 18 { 6 + pass * 12 + (ip - 6) } else { 42 + (ip - 18) };
|
||||
let mut ch = vec![0f32; d.hidden]; cur.d2h(&mut ch).unwrap();
|
||||
let n = ch.iter().map(|v| v * v).sum::<f32>().sqrt();
|
||||
eprintln!("[NORM] visit={visit} ip={ip} norm={n:.6} v0={:.6} v1={:.6} v2={:.6}", ch[0], ch[1], ch[2]);
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
/// Full Inkling-2-Mini forward for one decode token, following the proven
|
||||
@@ -362,6 +380,11 @@ pub fn forward_inkling_with_state(
|
||||
) -> Result<DeviceBuf, String> {
|
||||
let h = d.hidden;
|
||||
let cur = embed_token(ar, token, h, vocab)?;
|
||||
if std::env::var("VETTA_DBG_NORM").is_ok() {
|
||||
let mut eh = vec![0f32; h]; cur.d2h(&mut eh).unwrap();
|
||||
let n = eh.iter().map(|v| v * v).sum::<f32>().sqrt();
|
||||
eprintln!("[NORM] embed token={token} norm={n:.6} v0={:.6} v1={:.6} v2={:.6}", eh[0], eh[1], eh[2]);
|
||||
}
|
||||
let mut blocks: Vec<DeviceBuf> = Vec::new();
|
||||
// Prelude.
|
||||
for ip in 0..6 { run_layer(ar, d, ip, &cur, &mut blocks, states, max_seq, pos, 0, None)?; }
|
||||
@@ -568,9 +591,8 @@ pub fn layer_forward_full(
|
||||
if let Some((cw, msgs)) = ctrl {
|
||||
apply_controller(&mlpin, &msgs.channel(3, h)?, cw, 3, h, 64, d.eps)?;
|
||||
}
|
||||
let mlpin_bf = DeviceBuf::alloc(h * 2)?; vetta_infer_hip::cast_f32_bf16(&mlpin, &mlpin_bf, h)?;
|
||||
let rlog = DeviceBuf::alloc(d.n_experts * 4)?;
|
||||
vetta_infer_hip::gemm_bf16(&mlpin_bf, &w.gate_w, &rlog, 1, d.n_experts as i32, h as i32)?;
|
||||
vetta_infer_hip::router_gemv(&mlpin, &w.gate_w, &rlog, d.n_experts as i32, h as i32)?;
|
||||
let ids = DeviceBuf::alloc(d.top_k * 4)?;
|
||||
let weights = DeviceBuf::alloc(d.top_k * 4)?;
|
||||
vetta_infer_hip::moe_router_topk(&rlog, Some(&w.gate_b), &ids, &weights, 1, d.n_experts as i32, d.top_k as i32, d.route_scale)?;
|
||||
@@ -592,6 +614,7 @@ pub fn layer_forward_full(
|
||||
let y_part = DeviceBuf::alloc(d.top_k * h * 4)?; y_part.h2d(&y_part_h)?;
|
||||
let moeout = DeviceBuf::alloc(h * 4)?;
|
||||
vetta_infer_hip::moe_reduce(&y_part, &weights, &moeout, d.top_k as i32, h as i32)?;
|
||||
if w.ip == 0 { dbg_norm("y_part", &y_part, d.top_k * h); dbg_norm("moeout", &moeout, h); }
|
||||
let msc = DeviceBuf::alloc(h * 4)?;
|
||||
vetta_infer_hip::shortconv4_decode(&moeout, &w.m_sconv, &st.hist_m[pass], &msc, &slot0, 1, h as i32)?;
|
||||
vetta_infer_hip::vec_add(cur, &msc, cur, h)?;
|
||||
|
||||
Reference in new issue
Block a user