[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:
1 parent
ff524df917
commit
2dd9ff952c
4 files changed
+65
-1
No files matched your search
@@ -67,6 +67,7 @@ mod ffi {
|
||||
pub fn vetta_l2_clip(x: *mut c_float, scale: *const c_float, rows: c_int, hd: c_int, clip: c_float, apply_tau: c_int, pos: c_int, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_relpos_bias(r: *const c_float, rel_proj: *const c_float, rel_bias: *mut c_float, n_head: c_int, d_rel: c_int, rel_extent: c_int, lim: c_int, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_kv_store(k_in: *const c_float, v_in: *const c_float, k_cache: *mut c_float, v_cache: *mut c_float, pos: c_int, n_kv: c_int, hd: c_int, seq: c_int, rms_eps: c_float, stream: *mut c_void) -> c_int;
|
||||
pub fn vetta_softcap(x: *const c_float, y: *mut c_float, n: usize, cap: c_float, stream: *mut c_void) -> c_int;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -309,6 +310,14 @@ pub fn kv_store(k_in: &DeviceBuf, v_in: &DeviceBuf, k_cache: &DeviceBuf, v_cache
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Launch k_softcap: y = cap * tanh(x / cap).
|
||||
#[cfg(feature = "hip")]
|
||||
pub fn softcap(x: &DeviceBuf, y: &DeviceBuf, n: usize, cap: f32) -> Result<(), String> {
|
||||
let rc = unsafe { ffi::vetta_softcap(x.ptr as *const f32, y.ptr as *mut f32, n, cap, std::ptr::null_mut()) };
|
||||
if rc != 0 { return Err("softcap launch failed".into()); }
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(all(test, feature = "hip"))]
|
||||
mod gpu_tests {
|
||||
use super::*;
|
||||
@@ -864,6 +873,23 @@ mod gpu_tests {
|
||||
eprintln!("k2 prep kernels (l2_clip + relpos_bias + kv_store): parity OK");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn softcap_parity_vs_cpu() {
|
||||
let cap = 30.0f32;
|
||||
let n = 5000usize;
|
||||
let x: Vec<f32> = (0..n).map(|i| ((i % 201) as f32 - 100.0) * 0.5).collect();
|
||||
let want: Vec<f32> = x.iter().map(|&v| cap * (v / cap).tanh()).collect();
|
||||
let dx = DeviceBuf::alloc(n * 4).unwrap(); dx.h2d(&x).unwrap();
|
||||
let dy = DeviceBuf::alloc(n * 4).unwrap();
|
||||
softcap(&dx, &dy, n, cap).unwrap();
|
||||
let mut got = vec![0f32; n]; dy.d2h(&mut got).unwrap();
|
||||
let mut mr = 0f32; for (g, w) in got.iter().zip(want.iter()) { mr = mr.max((g - w).abs() / w.abs().max(1e-3)); }
|
||||
eprintln!("softcap max_rel_err={mr:.3e}");
|
||||
assert!(mr < 1e-5, "softcap parity failed: {mr}");
|
||||
assert!(got.iter().all(|v| v.abs() <= cap + 1e-3), "softcap must bound logits");
|
||||
}
|
||||
|
||||
fn bytemuck_u16(v: &[u16]) -> &[u8] {
|
||||
unsafe { std::slice::from_raw_parts(v.as_ptr() as *const u8, v.len() * 2) }
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user