[LD] Fix mlp loop-controller order: gate RAW mix BEFORE mlp_norm (exact strata)

Root cause of the residual argmax gap (tok100: 220 vs ref 68014). Per-visit norm bisection vs the reference localized it: prelude + core pass 0 matched (~1e-4), but pass 1/2 diverged ~3x. Stage dump at visit 18 (ip6 pass1) showed attention matched (q/k_cv/v_cv/aout/cur_after_attn ~1e-3) while mlpin diverged 2.8% and y_part 2.85x (router top-k flip).

The reference (ink_hip_backend.hip.cpp ~5049) applies the mlp loop controller to the RAW depth-mix THEN rms_norm: run_attnres -> run_loop_ctrl(mlpin,3) -> k_rms_norm_rows. vetta applied it AFTER rms_norm (to the normalized mlpin). Swapped order in both layer_forward_swa and layer_forward_full: apply_controller(&mix,...) before rms_norm.

MEASURED after fix: tok100 argmax = 68014 EXACT reference match (logit 18.371). All 48 per-visit cur norms now match reference to ~1e-3 (pass1/2 + coda restored). Generation 'The capital of France is' -> first 5 tokens [152717,13,68014,32,13] EXACT reference (was 2); coherent continuation ' Paris.\n\nA. True\nB. False\nAnswer A. True'.

Verified: 18 ld GPU + 17 hip GPU + workspace (-D warnings) green, 5 Lean files 0 sorryAx. Reference debug patch reverted + rebuilt clean.
This commit is contained in:
adm committed 2026-12-01 17:00:33 +01:00
1 parent e4dcc5e015
commit 5ec55f753f
1 file changed
+15 -12
+15 -12
View File
@@ -180,11 +180,11 @@ pub fn layer_forward_swa(
let lim = (pos + 1).min(d.rel_extent as i32); 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::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)?; 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); } if w.ip == 0 || (w.ip == 6 && pass == 1) { 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. // 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))?; 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); } if w.ip == 0 || (w.ip == 6 && pass == 1) { 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. // k4: O-proj + attn shortconv + block boundary.
vetta_infer_hip::cast_f32_bf16(&aout, &aout_bf, q_dim)?; vetta_infer_hip::cast_f32_bf16(&aout, &aout_bf, q_dim)?;
@@ -199,7 +199,7 @@ pub fn layer_forward_swa(
} else { } else {
vetta_infer_hip::vec_add(cur, &asc, cur, h)?; 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); } if w.ip == 0 || (w.ip == 6 && pass == 1) { dbg_norm("cur_after_attn", cur, h); dbg_norm("asc", &asc, h); }
// k5: MoE input = mlp_norm(mix(blocks, cur, mlp_query)). // k5: MoE input = mlp_norm(mix(blocks, cur, mlp_query)).
let n_blocks = blocks.len(); let n_blocks = blocks.len();
@@ -209,12 +209,14 @@ pub fn layer_forward_swa(
blk.h2d(&bh)?; blk.h2d(&bh)?;
let mix = DeviceBuf::alloc(h * 4)?; let mix = DeviceBuf::alloc(h * 4)?;
vetta_infer_hip::attn_res_mix(&blk, cur, &w.mlp_query, &mix, n_blocks as i32, 1, 1, h as i32, d.eps)?; vetta_infer_hip::attn_res_mix(&blk, cur, &w.mlp_query, &mix, n_blocks as i32, 1, 1, h as i32, d.eps)?;
// Exact strata order: the mlp loop controller gates the RAW depth-mix
// BEFORE mlp_norm (run_attnres -> run_loop_ctrl(mlpin,3) -> k_rms_norm_rows).
if let Some((cw, msgs)) = ctrl {
apply_controller(&mix, &msgs.channel(3, h)?, cw, 3, h, 64, d.eps)?;
}
let mlpin = DeviceBuf::alloc(h * 4)?; let mlpin = DeviceBuf::alloc(h * 4)?;
vetta_infer_hip::rms_norm_f32(&mix, Some(&w.mlp_norm), &mlpin, 1, h as i32, d.eps)?; 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 w.ip == 0 || (w.ip == 6 && pass == 1) { 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. Router reads F32 mlpin (exact strata B=1 // k6: router + experts + reduce. Router reads F32 mlpin (exact strata B=1
// launch_f32_linear); bf16-rounding the router input flips top-k selection. // launch_f32_linear); bf16-rounding the router input flips top-k selection.
@@ -229,7 +231,7 @@ pub fn layer_forward_swa(
let y_part = DeviceBuf::alloc(d.top_k * h * 4)?; y_part.h2d(&y_part_h)?; let y_part = DeviceBuf::alloc(d.top_k * h * 4)?; y_part.h2d(&y_part_h)?;
let moeout = DeviceBuf::alloc(h * 4)?; let moeout = DeviceBuf::alloc(h * 4)?;
vetta_infer_hip::moe_reduce(&y_part, &weights, &moeout, d.top_k as i32, h as i32)?; 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); } if w.ip == 0 || (w.ip == 6 && pass == 1) { dbg_norm("y_part", &y_part, d.top_k * h); dbg_norm("moeout", &moeout, h); }
// k7: mlp shortconv + plain residual add. // k7: mlp shortconv + plain residual add.
let msc = DeviceBuf::alloc(h * 4)?; let msc = DeviceBuf::alloc(h * 4)?;
@@ -618,11 +620,12 @@ pub fn layer_forward_full(
blk.h2d(&bh)?; blk.h2d(&bh)?;
let mix = DeviceBuf::alloc(h * 4)?; let mix = DeviceBuf::alloc(h * 4)?;
vetta_infer_hip::attn_res_mix(&blk, cur, &w.mlp_query, &mix, n_blocks as i32, 1, 1, h as i32, d.eps)?; vetta_infer_hip::attn_res_mix(&blk, cur, &w.mlp_query, &mix, n_blocks as i32, 1, 1, h as i32, d.eps)?;
// Exact strata order: mlp controller gates the RAW mix BEFORE mlp_norm.
if let Some((cw, msgs)) = ctrl {
apply_controller(&mix, &msgs.channel(3, h)?, cw, 3, h, 64, d.eps)?;
}
let mlpin = DeviceBuf::alloc(h * 4)?; let mlpin = DeviceBuf::alloc(h * 4)?;
vetta_infer_hip::rms_norm_f32(&mix, Some(&w.mlp_norm), &mlpin, 1, h as i32, d.eps)?; vetta_infer_hip::rms_norm_f32(&mix, Some(&w.mlp_norm), &mlpin, 1, h as i32, d.eps)?;
if let Some((cw, msgs)) = ctrl {
apply_controller(&mlpin, &msgs.channel(3, h)?, cw, 3, h, 64, d.eps)?;
}
let rlog = DeviceBuf::alloc(d.n_experts * 4)?; let rlog = DeviceBuf::alloc(d.n_experts * 4)?;
vetta_infer_hip::router_gemv(&mlpin, &w.gate_w, &rlog, 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 ids = DeviceBuf::alloc(d.top_k * 4)?;
@@ -634,7 +637,7 @@ pub fn layer_forward_full(
let y_part = DeviceBuf::alloc(d.top_k * h * 4)?; y_part.h2d(&y_part_h)?; let y_part = DeviceBuf::alloc(d.top_k * h * 4)?; y_part.h2d(&y_part_h)?;
let moeout = DeviceBuf::alloc(h * 4)?; let moeout = DeviceBuf::alloc(h * 4)?;
vetta_infer_hip::moe_reduce(&y_part, &weights, &moeout, d.top_k as i32, h as i32)?; 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); } if w.ip == 0 || (w.ip == 6 && pass == 1) { dbg_norm("y_part", &y_part, d.top_k * h); dbg_norm("moeout", &moeout, h); }
let msc = DeviceBuf::alloc(h * 4)?; 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::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)?; vetta_infer_hip::vec_add(cur, &msc, cur, h)?;