[Core] DSpark brick 5a: greedy speculative accept loop (exact reference indexing)

spec_accept_greedy + committed_prefix mirror strata ink_serve.h block-verify:
row i feeds anchor (i=0) / draft[i-1] at cur_pos+i, greedy[i] predicts
cur_pos+1+i; accept while argmax==draft[i], else emit correction (or bonus at
i==nd); accepted=emit.len(); rollback keeps cur_pos+accepted; the correction's
KV is deferred to the next round (becomes its anchor). Stop/length cap end the
request mid-round. Lossless per proofs/speculative_draft.lean spec_lossless.
CPU tests: all-accept+bonus, mid/first mismatch, stop, length cap, prefix.
CPU -D warnings green.
This commit is contained in:
adm committed 2026-12-02 20:48:30 +01:00
1 parent f9cde4954a
commit 3812845f1a
1 file changed
+107
+107
View File
@@ -323,6 +323,63 @@ mod gpu {
#[cfg(feature = "hip")] #[cfg(feature = "hip")]
pub use gpu::{DsparkLayer, DsparkWeights, dspark_context, dspark_draft}; pub use gpu::{DsparkLayer, DsparkWeights, dspark_context, dspark_draft};
/// Outcome of one greedy speculative round (mirrors strata ink_serve.h block
/// verify loop). `greedy[i]` is the trunk argmax from feeding row i (anchor for
/// i=0, draft[i-1] for i>0); it predicts the token at position cur_pos+1+i.
/// Lossless by proofs/speculative_draft.lean `spec_lossless`.
pub struct SpecRound {
/// Tokens to emit this round (accepted drafts + one correction/bonus).
pub emit: Vec<i32>,
/// Number of tokens committed to the KV prefix = emit.len(); rollback keeps
/// cur_pos + accepted (anchor + accepted drafts; the correction's KV is
/// deferred to the next round, becoming its anchor).
pub accepted: usize,
/// Anchor token for the next round (the correction/bonus), unless done.
pub next_tok: i32,
/// Request finished (stop token or length cap hit mid-round).
pub done: bool,
}
/// Greedy accept loop. `draft` = nd proposed ids; `greedy` = nd+1 trunk argmax
/// (row i predicts position cur_pos+1+i). `max_left` = remaining token budget;
/// `is_stop` flags end-of-sequence ids. Exact reference indexing.
pub fn spec_accept_greedy(
draft: &[i32], greedy: &[i32], anchor_tok: i32, max_left: usize,
is_stop: impl Fn(i32) -> bool,
) -> SpecRound {
let nd = draft.len();
debug_assert_eq!(greedy.len(), nd + 1);
let mut emit = Vec::with_capacity(nd + 1);
let mut next_tok = anchor_tok;
let mut done = false;
for i in 0..=nd {
let tok = greedy[i];
emit.push(tok);
if is_stop(tok) || emit.len() >= max_left {
done = true;
break;
}
if i < nd && tok == draft[i] {
continue; // accept draft[i], verify next position
}
next_tok = tok; // correction (mismatch) or bonus (i == nd)
break;
}
let accepted = emit.len();
SpecRound { emit, accepted, next_tok, done }
}
/// The KV-prefix tokens committed by a round (the fed inputs of the accepted
/// rows): anchor then draft[0..accepted-1]. Used to advance the cached prefix.
pub fn committed_prefix(anchor_tok: i32, draft: &[i32], accepted: usize) -> Vec<i32> {
let mut v = Vec::with_capacity(accepted);
v.push(anchor_tok);
for i in 0..accepted.saturating_sub(1) {
v.push(draft[i]);
}
v
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@@ -340,6 +397,56 @@ mod tests {
assert_eq!(ar.tensors.len(), expected_tensors().len()); assert_eq!(ar.tensors.len(), expected_tensors().len());
} }
/// Greedy accept loop: all drafts accepted + bonus.
#[test]
fn spec_accept_all_then_bonus() {
let r = spec_accept_greedy(&[10, 20, 30], &[10, 20, 30, 99], 5, 10, |_| false);
assert_eq!(r.emit, vec![10, 20, 30, 99]);
assert_eq!(r.accepted, 4);
assert_eq!(r.next_tok, 99);
assert!(!r.done);
assert_eq!(committed_prefix(5, &[10, 20, 30], r.accepted), vec![5, 10, 20, 30]);
}
/// Mismatch at i=1: accept draft[0], emit correction, rollback keeps 2.
#[test]
fn spec_accept_mid_mismatch() {
let r = spec_accept_greedy(&[10, 20, 30], &[10, 77, 0, 0], 5, 10, |_| false);
assert_eq!(r.emit, vec![10, 77]);
assert_eq!(r.accepted, 2);
assert_eq!(r.next_tok, 77);
assert!(!r.done);
assert_eq!(committed_prefix(5, &[10, 20, 30], r.accepted), vec![5, 10]);
}
/// Mismatch at i=0: only the correction is emitted/committed.
#[test]
fn spec_accept_first_mismatch() {
let r = spec_accept_greedy(&[10, 20], &[88, 0, 0], 5, 10, |_| false);
assert_eq!(r.emit, vec![88]);
assert_eq!(r.accepted, 1);
assert_eq!(r.next_tok, 88);
assert_eq!(committed_prefix(5, &[10, 20], r.accepted), vec![5]);
}
/// Stop token mid-round ends the request.
#[test]
fn spec_accept_stop() {
let r = spec_accept_greedy(&[10, 20, 30], &[10, 20, 99, 0], 5, 10, |t| t == 99);
assert_eq!(r.emit, vec![10, 20, 99]);
assert_eq!(r.accepted, 3);
assert!(r.done);
}
/// Length cap mid-round ends the request (accepted == budget).
#[test]
fn spec_accept_length_cap() {
let r = spec_accept_greedy(&[10, 20, 30], &[10, 20, 30, 99], 5, 2, |_| false);
assert_eq!(r.emit, vec![10, 20]);
assert_eq!(r.accepted, 2);
assert!(r.done);
}
/// A wrong shape must be rejected (drafter disabled), not silently accepted. /// A wrong shape must be rejected (drafter disabled), not silently accepted.
#[test] #[test]
fn layout_check_rejects_wrong_shape() { fn layout_check_rejects_wrong_shape() {