[Core] DSpark brick 5b+6: speculative runner + LOSSLESS GATE green (200 tokens)
LayerState: per-prefix ShortConv hist snapshots (snap_k/v/a/m[3]) captured during a spec verify batch via spec_update_hist (gated by thread-local spec_begin/end); rollback_spec restores hist<-snap[accepted]. KV ring truncates with no restore (proofs/speculative_draft.lean kept_rejected_disjoint). Normal path unchanged. DsparkContext: persistent bf16 ctx rings (k/v[3] + frnn) + commit (scatter). spec_round: draft nd -> batched trunk verify (forward_batch_taps B=nd+1) -> per-row greedy from taps[3] -> spec_accept_greedy -> commit ctx + rollback hist. Mirrors strata ink_serve.h indexing exactly. b6 GATE: spec_lossless_gate emits EXACTLY the same 200 tokens as plain batched greedy (forward_batch B=1) on real trunk+mtp (163 rounds, nd=7). Diagnostics: batch_determinism, rollback_hist (exact), spec_lockstep (200 rounds hist+KV exact). Acceptance 3.2% on the arbitrary synthetic prompt -> NO speedup claim yet. Verified: CPU -D warnings clean, Lean 0 sorryAx, hip 45/45, ld GPU 65/65.
This commit is contained in:
1 parent
6e854a9d73
commit
7b617bc333
2 files changed
+374
-9
No files matched your search
@@ -48,6 +48,13 @@ pub struct LayerState {
|
||||
pub hist_k: [DeviceBuf; 3], pub hist_v: [DeviceBuf; 3], // [3, n_kv*hd] each
|
||||
pub hist_a: [DeviceBuf; 3], pub hist_m: [DeviceBuf; 3], // [3, hidden] each
|
||||
pub max_seq: usize,
|
||||
// DSpark speculative rollback: per-prefix ShortConv history snapshots
|
||||
// [(B+1)*3*C] captured during a spec verify batch (spec_recording). On
|
||||
// partial accept, hist <- snap[accepted]; the KV ring truncates with no
|
||||
// restore (proofs/speculative_draft.lean kept_rejected_disjoint).
|
||||
pub spec_b: usize,
|
||||
pub snap_k: [Option<DeviceBuf>; 3], pub snap_v: [Option<DeviceBuf>; 3],
|
||||
pub snap_a: [Option<DeviceBuf>; 3], pub snap_m: [Option<DeviceBuf>; 3],
|
||||
}
|
||||
|
||||
/// Forward-pass dimensions (subset of TextConfig needed by a layer).
|
||||
@@ -268,9 +275,49 @@ pub fn new_state(d: &Dims, max_seq: usize, n_passes: usize) -> Result<LayerState
|
||||
hist_a: [zero(3 * d.hidden)?, zero(3 * d.hidden)?, zero(3 * d.hidden)?],
|
||||
hist_m: [zero(3 * d.hidden)?, zero(3 * d.hidden)?, zero(3 * d.hidden)?],
|
||||
max_seq,
|
||||
spec_b: 0,
|
||||
snap_k: [None, None, None], snap_v: [None, None, None],
|
||||
snap_a: [None, None, None], snap_m: [None, None, None],
|
||||
})
|
||||
}
|
||||
|
||||
thread_local! {
|
||||
static SPEC_REC: std::cell::Cell<(bool, usize)> = const { std::cell::Cell::new((false, 0)) };
|
||||
}
|
||||
/// Enable DSpark spec capture for a verify batch of `b` tokens (thread-local).
|
||||
pub fn spec_begin(b: usize) { SPEC_REC.with(|c| c.set((true, b))); }
|
||||
/// Disable spec capture (snapshots stay allocated for reuse across rounds).
|
||||
pub fn spec_end() { SPEC_REC.with(|c| c.set((false, c.get().1))); }
|
||||
fn spec_recording() -> bool { SPEC_REC.with(|c| c.get().0) }
|
||||
|
||||
/// update_hist + (when spec-recording) per-prefix history snapshots. The snaps
|
||||
/// kernel reads hist BEFORE update_hist rolls it, so snaps[0]=hist0.
|
||||
fn spec_update_hist(in_x: &DeviceBuf, hist: &DeviceBuf, snap: &mut Option<DeviceBuf>, b: i32, c: i32) -> Result<(), String> {
|
||||
if spec_recording() {
|
||||
let n = (b as usize + 1) * 3 * c as usize;
|
||||
if snap.as_ref().map_or(true, |s| s.bytes != n * 4) {
|
||||
*snap = Some(DeviceBuf::alloc(n * 4)?);
|
||||
}
|
||||
vetta_infer_hip::shortconv4_hist_snaps(in_x, hist, snap.as_ref().unwrap(), b, c)?;
|
||||
}
|
||||
vetta_infer_hip::shortconv4_update_hist(in_x, hist, b, c)
|
||||
}
|
||||
|
||||
/// Roll back every layer's ShortConv history to the state after `accepted` tokens
|
||||
/// of the spec verify batch (hist <- snap[accepted]). KV needs no restore.
|
||||
pub fn rollback_spec(d: &Dims, states: &mut [Option<LayerState>], accepted: usize) -> Result<(), String> {
|
||||
let kv = 3 * d.kv_dim(); let hh = 3 * d.hidden;
|
||||
for st in states.iter_mut().flatten() {
|
||||
for p in 0..3 {
|
||||
if let Some(s) = st.snap_k[p].as_ref() { vetta_infer_hip::copy_f32_sub(s, accepted * kv, &st.hist_k[p], kv)?; }
|
||||
if let Some(s) = st.snap_v[p].as_ref() { vetta_infer_hip::copy_f32_sub(s, accepted * kv, &st.hist_v[p], kv)?; }
|
||||
if let Some(s) = st.snap_a[p].as_ref() { vetta_infer_hip::copy_f32_sub(s, accepted * hh, &st.hist_a[p], hh)?; }
|
||||
if let Some(s) = st.snap_m[p].as_ref() { vetta_infer_hip::copy_f32_sub(s, accepted * hh, &st.hist_m[p], hh)?; }
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A checkpoint of one layer's decode state at a prefix boundary `seq_len`: the
|
||||
/// KV prefix `[n_kv, seq_len, hd]` per pass + the ShortConv history rings. Sized
|
||||
/// to seq_len (KV unpooled), held in the prefix cache across requests. Restoring
|
||||
@@ -611,9 +658,9 @@ fn layer_forward_swa_batched(
|
||||
}
|
||||
// k2: ShortConv prefill (parallel causal) + history roll, then clip/relpos/KV.
|
||||
vetta_infer_hip::shortconv4_prefill_par(&k_raw, &w.k_sconv, None, &st.hist_k[pass], &k_cv, b, kv_dim as i32, 0)?;
|
||||
vetta_infer_hip::shortconv4_update_hist(&k_raw, &st.hist_k[pass], b, kv_dim as i32)?;
|
||||
spec_update_hist(&k_raw, &st.hist_k[pass], &mut st.snap_k[pass], b, kv_dim as i32)?;
|
||||
vetta_infer_hip::shortconv4_prefill_par(&v_raw, &w.v_sconv, None, &st.hist_v[pass], &v_cv, b, kv_dim as i32, 0)?;
|
||||
vetta_infer_hip::shortconv4_update_hist(&v_raw, &st.hist_v[pass], b, kv_dim as i32)?;
|
||||
spec_update_hist(&v_raw, &st.hist_v[pass], &mut st.snap_v[pass], b, kv_dim as i32)?;
|
||||
vetta_infer_hip::l2_clip_batched(&q, Some(&w.q_sc), (b as usize * d.n_head) as i32, d.hd as i32, d.qk_clip, false, 0, d.n_head as i32, None)?;
|
||||
vetta_infer_hip::l2_clip_batched(&r_raw, Some(&w.r_sc), (b as usize * d.n_head) as i32, d.d_rel as i32, d.rel_clip, false, 0, d.n_head as i32, None)?;
|
||||
vetta_infer_hip::l2_clip_batched(&k_cv, None, (b as usize * d.n_kv) as i32, d.hd as i32, d.qk_clip, false, 0, d.n_kv as i32, None)?;
|
||||
@@ -625,7 +672,7 @@ fn layer_forward_swa_batched(
|
||||
vetta_infer_hip::cast_f32_bf16(&aout, &aout_bf, bu * q_dim)?;
|
||||
vetta_infer_hip::gemm_bf16(&aout_bf, &w.wo, &oproj, b, h as i32, q_dim as i32)?;
|
||||
vetta_infer_hip::shortconv4_prefill_par(&oproj, &w.a_sconv, None, &st.hist_a[pass], &asc, b, h as i32, 0)?;
|
||||
vetta_infer_hip::shortconv4_update_hist(&oproj, &st.hist_a[pass], b, h as i32)?;
|
||||
spec_update_hist(&oproj, &st.hist_a[pass], &mut st.snap_a[pass], b, h as i32)?;
|
||||
if w.ip % d.block_size == 0 {
|
||||
blocks.snapshot(cur)?;
|
||||
vetta_infer_hip::copy_f32(&asc, cur, bu * h)?;
|
||||
@@ -651,7 +698,7 @@ fn layer_forward_swa_batched(
|
||||
// k7: mlp shortconv + plain residual add.
|
||||
let msc = DeviceBuf::alloc(bu * h * 4)?;
|
||||
vetta_infer_hip::shortconv4_prefill_par(&moeout, &w.m_sconv, None, &st.hist_m[pass], &msc, b, h as i32, 0)?;
|
||||
vetta_infer_hip::shortconv4_update_hist(&moeout, &st.hist_m[pass], b, h as i32)?;
|
||||
spec_update_hist(&moeout, &st.hist_m[pass], &mut st.snap_m[pass], b, h as i32)?;
|
||||
vetta_infer_hip::vec_add(cur, &msc, cur, bu * h)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -1374,9 +1421,9 @@ fn layer_forward_full_batched(
|
||||
let k_cv = DeviceBuf::alloc(bu * kv_dim * 4)?;
|
||||
let v_cv = DeviceBuf::alloc(bu * kv_dim * 4)?;
|
||||
vetta_infer_hip::shortconv4_prefill_par(&k_raw, &w.k_static, Some(&k_dyn), &st.hist_k[pass], &k_cv, b, kv_dim as i32, d.n_kv as i32)?;
|
||||
vetta_infer_hip::shortconv4_update_hist(&k_raw, &st.hist_k[pass], b, kv_dim as i32)?;
|
||||
spec_update_hist(&k_raw, &st.hist_k[pass], &mut st.snap_k[pass], b, kv_dim as i32)?;
|
||||
vetta_infer_hip::shortconv4_prefill_par(&v_raw, &w.v_static, Some(&v_dyn), &st.hist_v[pass], &v_cv, b, kv_dim as i32, d.n_kv as i32)?;
|
||||
vetta_infer_hip::shortconv4_update_hist(&v_raw, &st.hist_v[pass], b, kv_dim as i32)?;
|
||||
spec_update_hist(&v_raw, &st.hist_v[pass], &mut st.snap_v[pass], b, kv_dim as i32)?;
|
||||
// tau on Q/R (per-token pos), K clip without tau.
|
||||
vetta_infer_hip::l2_clip_batched(&q, Some(&w.q_sc), (b as usize * d.n_head) as i32, d.hd as i32, d.qk_clip, true, 0, d.n_head as i32, Some(pos))?;
|
||||
vetta_infer_hip::l2_clip_batched(&r_raw, Some(&w.r_sc), (b as usize * d.n_head) as i32, d.d_rel as i32, d.rel_clip, true, 0, d.n_head as i32, Some(pos))?;
|
||||
@@ -1392,7 +1439,7 @@ fn layer_forward_full_batched(
|
||||
vetta_infer_hip::gemm_bf16(&aout_bf, &w.wo, &oproj, b, h as i32, q_dim as i32)?;
|
||||
let asc = DeviceBuf::alloc(bu * h * 4)?;
|
||||
vetta_infer_hip::shortconv4_prefill_par(&oproj, &w.a_sconv, None, &st.hist_a[pass], &asc, b, h as i32, 0)?;
|
||||
vetta_infer_hip::shortconv4_update_hist(&oproj, &st.hist_a[pass], b, h as i32)?;
|
||||
spec_update_hist(&oproj, &st.hist_a[pass], &mut st.snap_a[pass], b, h as i32)?;
|
||||
if w.ip % d.block_size == 0 {
|
||||
blocks.snapshot(cur)?;
|
||||
vetta_infer_hip::copy_f32(&asc, cur, bu * h)?;
|
||||
@@ -1416,7 +1463,7 @@ fn layer_forward_full_batched(
|
||||
let moeout = moe_batched(cache, w.ip, &mlpin, &ids, &weights, d, h, b)?;
|
||||
let msc = DeviceBuf::alloc(bu * h * 4)?;
|
||||
vetta_infer_hip::shortconv4_prefill_par(&moeout, &w.m_sconv, None, &st.hist_m[pass], &msc, b, h as i32, 0)?;
|
||||
vetta_infer_hip::shortconv4_update_hist(&moeout, &st.hist_m[pass], b, h as i32)?;
|
||||
spec_update_hist(&moeout, &st.hist_m[pass], &mut st.snap_m[pass], b, h as i32)?;
|
||||
vetta_infer_hip::vec_add(cur, &msc, cur, bu * h)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user