[HIP][LD] softcap kernel + correct final output path (AttnRes output mix + softcap)

- k_softcap / vetta_softcap: logits = cap*tanh(x/cap), the final logit
  softcapping (Inkling cap=30). Parity vs CPU 9.7e-8; bounds |logits| <= cap.
- ld::layer::final_logits: the CORRECT final output path extracted from the
  reference — the final norm is itself an AttnRes depth mix over the block
  snapshots + cur with the learned output_query, then norm + unembed + softcap:
    h_norm = norm(attn_res_mix(blocks, cur, output_query))
    logits = softcap(unembed(h_norm), 30)
  (The earlier unembed_logits used a plain rms_norm(cur), missing the final
  depth mix; final_logits fixes that.)
- multi_layer_prelude_forward now uses final_logits: argmax token 128538
  (logit 19.16, bounded by 30). The token shifts vs the plain-norm path,
  confirming the final output mix materially affects the result.

Verified: RUSTFLAGS="-D warnings" cargo test --workspace = 97/97 PASS;
lean proofs (4 files, 42 theorems) green; GPU softcap + multi-layer final PASS
(--features hip, --ignored).
This commit is contained in:
adm committed 2026-12-01 14:51:40 +01:00
1 parent ff524df917
commit 2dd9ff952c
4 files changed
+65 -1

No files matched your search

+19 -1
View File
@@ -241,6 +241,23 @@ pub fn unembed_logits(ar: &SafetensorsArchive, cur: &DeviceBuf, hidden: usize, v
Ok(logits)
}
/// Correct final output path: the final norm is itself an AttnRes depth mix
/// (output_query over blocks + cur), then norm + unembed + logit softcap.
/// h_norm = norm(attn_res_mix(blocks, cur, output_query)); logits = softcap(unembed(h_norm)).
pub fn final_logits(ar: &SafetensorsArchive, cur: &DeviceBuf, blocks: &[DeviceBuf], hidden: usize, vocab: usize, eps: f32, cap: f32) -> Result<DeviceBuf, String> {
let oq = load_raw(ar, "model.llm.attn_res.output_query")?;
let n_blocks = blocks.len();
let blk = DeviceBuf::alloc(n_blocks.max(1) * hidden * 4)?;
let mut bh = vec![0f32; n_blocks.max(1) * hidden];
for (i, b) in blocks.iter().enumerate() { b.d2h(&mut bh[i * hidden..(i + 1) * hidden])?; }
blk.h2d(&bh)?;
let mix = DeviceBuf::alloc(hidden * 4)?;
vetta_infer_hip::attn_res_mix(&blk, cur, &oq, &mix, n_blocks as i32, 1, 1, hidden as i32, eps)?;
let logits = unembed_logits(ar, &mix, hidden, vocab, eps)?;
if cap > 0.0 { vetta_infer_hip::softcap(&logits, &logits, vocab, cap)?; }
Ok(logits)
}
/// Run a schedule of SWA layer steps over an embedded token, returning the
/// final residual stream + the accumulated depth-block snapshots. (Prelude /
/// single-pass; full-attn layers + loop controllers + bank rollback come next.)
@@ -273,9 +290,10 @@ mod tests {
let d = Dims { hidden: 2304, n_head: 24, n_kv: 8, hd: 128, d_rel: 16, rel_extent: 1024, inter: 1152, n_experts: 128, top_k: 8, block_size: 6, qk_clip: 22.627416997969522, rel_clip: 8.0, eps: 1e-6, route_scale: 8.0, beta: 4.0, lb: 25.0, swa_win: 1024 };
let (cur, blocks) = forward_swa_layers(&ar, &[0, 1, 2, 3, 4], &d, 100, 1, 0).unwrap();
assert_eq!(blocks.len(), 1, "prelude 0..4 snapshots only at ip=0");
let logits = unembed_logits(&ar, &cur, d.hidden, 201024, d.eps).unwrap();
let logits = final_logits(&ar, &cur, &blocks, d.hidden, 201024, d.eps, 30.0).unwrap();
let mut lg = vec![0f32; 201024]; logits.d2h(&mut lg).unwrap();
assert!(lg.iter().all(|v| v.is_finite()), "logits must be finite");
assert!(lg.iter().all(|v| v.abs() <= 30.0 + 1e-3), "softcap must bound logits");
let argmax = lg.iter().enumerate().max_by(|a, b| a.1.partial_cmp(b.1).unwrap()).unwrap().0;
eprintln!("multi-layer prelude (0..4) forward: argmax token={argmax} logit={:.3}", lg[argmax]);
assert!(argmax < 201024);