use super::forward_gpu::JointPrefillLane;
use super::kv_cache::HybridKvCache;
use super::model::Qwen35Model;
use super::prefill_prepared::PrefillScope;
use super::prefill_prepared_tests::with_synthetic_model;
use super::spec_decode::positions_for_range;
use crate::inference::spec_decode::dflash::hidden_capture::DFlashCaptureSession;
use crate::serve::multi_seq_kv::SlotId;
const SLOTS: u32 = 4;
const CAPACITY: u32 = 1024;
const CAPTURE_LAYERS: [usize; 4] = [1, 4, 6, 7];
const FOLLOW_UP_ROWS: usize = 7;
const COMMON_SEED: u32 = 97;
type AnchorWords = (usize, u64, u32, Vec<u32>, Option<u32>, Vec<u32>);
#[derive(Clone, Copy, Debug)]
struct Lane {
slot: u32,
seed: u32,
common: usize,
history: &'static [usize],
content: usize,
cue: usize,
}
impl Lane {
const fn new(slot: u32, history: &'static [usize], content: usize, cue: usize) -> Self {
Self {
slot,
seed: slot + 1,
common: 0,
history,
content,
cue,
}
}
const fn cold(slot: u32, common: usize, content: usize, cue: usize) -> Self {
Self {
slot,
seed: slot + 1,
common,
history: &[],
content,
cue,
}
}
fn start(&self) -> usize {
self.history.iter().sum()
}
fn rows(&self) -> usize {
self.content + self.cue
}
fn tokens(&self, start: usize, len: usize) -> Vec<u32> {
(start..start + len)
.map(|position| {
let seed = if position < self.common {
COMMON_SEED
} else {
self.seed
};
(seed * 131 + position as u32 * 37 + 11) % 512
})
.collect()
}
fn fold(&self) -> Option<(usize, usize)> {
(self.cue > 0).then(|| (self.content, self.start() + self.content))
}
}
fn bits(values: &[f32]) -> Vec<u32> {
values.iter().map(|value| value.to_bits()).collect()
}
fn cache(model: &Qwen35Model) -> HybridKvCache {
model
.with_gpu_cache_mut(|device, _registry| {
HybridKvCache::new_with_options(&model.cfg, device, CAPACITY, SLOTS, true)
})
.expect("allocate TQ cache")
}
fn kv_rows(model: &Qwen35Model, cache: &HybridKvCache, slot: u32) -> Vec<u8> {
let rows = cache
.sequence_len_for_slot(SlotId(slot))
.expect("slot cursor") as usize;
let heads = model.cfg.num_key_value_heads as usize;
let mut bytes = Vec::new();
for layer in &cache.full_attn {
let tq = layer.tq.as_ref().expect("TQ full-attention slot");
for buffer in [&tq.k_packed, &tq.k_norms, &tq.v_packed, &tq.v_norms] {
let total = buffer.byte_len();
let per_head = total / (SLOTS as usize * heads);
let per_row = per_head / CAPACITY as usize;
assert_eq!(
per_row * CAPACITY as usize * heads * SLOTS as usize,
total,
"TQ buffer layout"
);
let all =
unsafe { std::slice::from_raw_parts(buffer.contents_ptr().cast::<u8>(), total) };
for head in 0..heads {
let at = (slot as usize * heads + head) * per_head;
bytes.extend_from_slice(&all[at..at + rows * per_row]);
}
}
}
bytes
}
#[derive(PartialEq)]
struct Outcome {
boundary: Option<(Vec<u32>, AnchorWords)>,
logits: Vec<u32>,
captured: Vec<Vec<u32>>,
after: AnchorWords,
kv: Vec<u8>,
follow_up: Vec<u32>,
}
impl Outcome {
fn differences(&self, other: &Self) -> Vec<&'static str> {
[
(
"boundary checkpoint and logits",
self.boundary == other.boundary,
),
("last-row logits", self.logits == other.logits),
("captured target layers", self.captured == other.captured),
("slot state after the chunk", self.after == other.after),
("full-attention rows", self.kv == other.kv),
("follow-up chunk", self.follow_up == other.follow_up),
]
.into_iter()
.filter_map(|(name, equal)| (!equal).then_some(name))
.collect()
}
}
fn prefill_history(model: &Qwen35Model, cache: &mut HybridKvCache, lane: &Lane) {
let mut start = 0;
for &rows in lane.history {
model
.forward_gpu_last_logits(
&lane.tokens(start, rows),
&positions_for_range(start as i32, rows),
cache,
SlotId(lane.slot),
)
.unwrap_or_else(|error| panic!("history chunk start={start} n={rows}: {error:#}"));
start += rows;
}
}
fn read_back(
model: &Qwen35Model,
cache: &mut HybridKvCache,
lane: &Lane,
) -> (AnchorWords, Vec<u8>, Vec<u32>) {
let end = lane.start() + lane.rows();
let after = cache
.snapshot_slot_anchor(SlotId(lane.slot), end)
.expect("slot state after the chunk")
.test_words();
let kv = kv_rows(model, cache, lane.slot);
let follow_up = model
.forward_gpu(
&lane.tokens(end, FOLLOW_UP_ROWS),
&positions_for_range(end as i32, FOLLOW_UP_ROWS),
cache,
SlotId(lane.slot),
)
.expect("follow-up chunk");
(after, kv, bits(&follow_up))
}
fn solo(model: &Qwen35Model, lane: &Lane, capture: bool) -> Outcome {
let _scope = PrefillScope::forced(true);
let hidden = model.cfg.hidden_size as usize;
let mut cache = cache(model);
prefill_history(model, &mut cache, lane);
let (start, rows) = (lane.start(), lane.rows());
let tokens = lane.tokens(start, rows);
let positions = positions_for_range(start as i32, rows);
let mut session =
capture.then(|| DFlashCaptureSession::new(CAPTURE_LAYERS.to_vec(), rows, hidden, false));
let (boundary, logits) = match lane.fold() {
Some((prefix_rows, boundary_len)) => {
let image = cache.boundary_anchor_image().expect("checkpoint storage");
let folded = model
.forward_gpu_last_logits_folded(
&tokens,
&positions,
&mut cache,
session.as_mut(),
SlotId(lane.slot),
prefix_rows,
boundary_len,
image,
)
.unwrap_or_else(|error| panic!("solo folded chunk of {lane:?}: {error:#}"));
(
Some((
bits(&folded.boundary_logits),
folded.boundary.expect("solo checkpoint").test_words(),
)),
bits(&folded.logits),
)
}
None => {
let logits = match session.as_mut() {
Some(session) => model
.forward_gpu_last_logits_with_hidden_dflash(
&tokens,
&positions,
&mut cache,
Some(session),
SlotId(lane.slot),
)
.map(|(logits, _hidden)| logits),
None => model.forward_gpu_last_logits(
&tokens,
&positions,
&mut cache,
SlotId(lane.slot),
),
}
.unwrap_or_else(|error| panic!("solo chunk of {lane:?}: {error:#}"));
(None, bits(&logits))
}
};
let captured = session.map_or_else(Vec::new, |session| {
session
.hidden_output
.chunks(rows * hidden)
.map(bits)
.collect()
});
let (after, kv, follow_up) = read_back(model, &mut cache, lane);
Outcome {
boundary,
logits,
captured,
after,
kv,
follow_up,
}
}
fn joint_forward(
model: &Qwen35Model,
cache: &mut HybridKvCache,
lanes: &[Lane],
shared_rows: usize,
capture: bool,
) -> anyhow::Result<Vec<(Option<(Vec<u32>, AnchorWords)>, Vec<u32>, Vec<Vec<u32>>)>> {
let _scope = PrefillScope::forced(true);
let hidden = model.cfg.hidden_size as usize;
let chunks: Vec<(Vec<u32>, Vec<i32>)> = lanes
.iter()
.map(|lane| {
(
lane.tokens(lane.start(), lane.rows()),
positions_for_range(lane.start() as i32, lane.rows()),
)
})
.collect();
let joint_lanes: Vec<JointPrefillLane<'_>> = lanes
.iter()
.zip(&chunks)
.map(|(lane, (tokens, positions))| JointPrefillLane {
slot_id: SlotId(lane.slot),
tokens,
positions_flat: positions,
shares_prefix: shared_rows > 0 && lane.common >= shared_rows,
fold: lane.fold(),
image: None,
})
.collect();
let total = Qwen35Model::joint_prefill_rows(&joint_lanes, shared_rows);
let mut session =
capture.then(|| DFlashCaptureSession::new(CAPTURE_LAYERS.to_vec(), total, hidden, false));
let outputs =
model.forward_gpu_last_logits_joint(&joint_lanes, shared_rows, cache, session.as_mut())?;
Ok(lanes
.iter()
.zip(outputs)
.map(|(lane, output)| {
let captured = session.as_ref().map_or_else(Vec::new, |session| {
(0..CAPTURE_LAYERS.len())
.map(|layer| {
output
.forward_rows(0..lane.rows())
.into_iter()
.flat_map(|rows| {
let at = layer * total + rows.start;
bits(
&session.hidden_output[at * hidden..(at + rows.len()) * hidden],
)
})
.collect()
})
.collect()
});
(
output
.boundary
.map(|(logits, anchor, _wall)| {
(bits(&logits), anchor.expect("lane checkpoint").test_words())
}),
bits(&output.logits),
captured,
)
})
.collect())
}
fn joint(model: &Qwen35Model, lanes: &[Lane], shared_rows: usize, capture: bool) -> Vec<Outcome> {
let mut cache = cache(model);
for lane in lanes {
let _scope = PrefillScope::forced(true);
prefill_history(model, &mut cache, lane);
}
let forward = joint_forward(model, &mut cache, lanes, shared_rows, capture)
.unwrap_or_else(|error| panic!("joint forward of {lanes:?}: {error:#}"));
let _scope = PrefillScope::forced(true);
lanes
.iter()
.zip(forward)
.map(|(lane, (boundary, logits, captured))| {
let (after, kv, follow_up) = read_back(model, &mut cache, lane);
Outcome {
boundary,
logits,
captured,
after,
kv,
follow_up,
}
})
.collect()
}
fn assert_lanes_match_solo(model: &Qwen35Model, lanes: &[Lane], shared_rows: usize, capture: bool) {
let together = joint(model, lanes, shared_rows, capture);
assert_eq!(together.len(), lanes.len());
for (lane, together) in lanes.iter().zip(&together) {
let alone = solo(model, lane, capture);
let differences = alone.differences(together);
assert!(
differences.is_empty(),
"{lane:?} in a joint prefill of {} lanes sharing {shared_rows} rows (capture \
{capture}) differs from its solo prefill in {differences:?}",
lanes.len()
);
assert_eq!(
alone.captured.len(),
if capture { CAPTURE_LAYERS.len() } else { 0 }
);
assert_eq!(alone.boundary.is_some(), lane.cue > 0);
}
}
#[test]
fn a_joint_prefill_leaves_each_lane_what_its_own_prefill_leaves() {
with_synthetic_model(|model| {
let cohorts: [&[Lane]; 5] = [
&[Lane::new(2, &[], 40, 5)],
&[Lane::new(0, &[], 720, 5), Lane::new(3, &[33], 5, 0)],
&[
Lane::new(1, &[64, 20], 40, 5),
Lane::new(0, &[], 31, 0),
Lane::new(2, &[17], 97, 8),
],
&[
Lane::new(0, &[], 720, 5),
Lane::new(1, &[33], 5, 0),
Lane::new(2, &[40], 157, 0),
Lane::new(3, &[], 33, 32),
],
&[Lane::new(3, &[], 2, 2), Lane::new(0, &[5], 2, 0)],
];
for lanes in cohorts {
assert_lanes_match_solo(model, lanes, 0, true);
}
assert_lanes_match_solo(model, cohorts[2], 0, false);
});
}
#[test]
fn lanes_that_share_a_prefix_hold_the_bits_of_their_unshared_prefill() {
with_synthetic_model(|model| {
let agents: Vec<Lane> = (0..4).map(|slot| Lane::cold(slot, 444, 720, 5)).collect();
assert_lanes_match_solo(model, &agents, 444, true);
let mixed = [
Lane::cold(2, 70, 100, 5),
Lane::new(1, &[33], 20, 5),
Lane::cold(0, 70, 72, 0),
Lane::new(3, &[], 50, 5),
];
assert_lanes_match_solo(model, &mixed, 70, true);
assert_lanes_match_solo(model, &mixed, 70, false);
assert_lanes_match_solo(model, &mixed, 0, true);
assert_lanes_match_solo(model, &mixed, 33, true);
});
}
#[test]
fn a_joint_prefill_is_repeatable() {
with_synthetic_model(|model| {
let lanes = [
Lane::cold(1, 64, 130, 5),
Lane::new(0, &[40], 37, 0),
Lane::cold(3, 64, 97, 8),
];
let first = joint(model, &lanes, 64, true);
joint(
model,
&[Lane::new(2, &[], 65, 5), Lane::new(0, &[], 9, 0)],
0,
false,
);
let again = joint(model, &lanes, 64, true);
for ((lane, first), again) in lanes.iter().zip(&first).zip(&again) {
let differences = first.differences(again);
assert!(
differences.is_empty(),
"{lane:?}: a repeated joint prefill changed {differences:?}"
);
}
});
}
#[test]
fn a_joint_prefill_that_waits_every_few_layers_keeps_each_lanes_bits() {
use super::prefill_prepared::ForcedWaitLayers;
with_synthetic_model(|model| {
let resumed = [
Lane::new(0, &[], 720, 5),
Lane::new(1, &[33], 5, 0),
Lane::new(2, &[40], 157, 0),
Lane::new(3, &[], 33, 32),
];
let first_prompts: Vec<Lane> = (0..4).map(|slot| Lane::cold(slot, 444, 720, 5)).collect();
for (lanes, shared_rows) in [(&resumed[..], 0), (&first_prompts[..], 444)] {
let alone: Vec<Outcome> = lanes.iter().map(|lane| solo(model, lane, true)).collect();
for wait_layers in [3, 8] {
let together = {
let _cadence = ForcedWaitLayers::set(wait_layers);
joint(model, lanes, shared_rows, true)
};
for ((lane, alone), together) in lanes.iter().zip(&alone).zip(&together) {
let differences = alone.differences(together);
assert!(
differences.is_empty(),
"{lane:?} in a joint prefill that waits every {wait_layers} layers \
differs from its solo prefill in {differences:?}"
);
}
}
}
});
}
#[test]
fn keeping_the_prefill_only_weights_fresh_changes_no_bit() {
use super::prefill_keep_fresh::INTERVAL;
use std::time::{Duration, Instant};
with_synthetic_model(|model| {
let listed = model.prefill_only_weights().expect("prefill-only weights");
assert_eq!(listed.len(), 1, "a dense model lists its output head only");
let lane = Lane::new(0, &[33], 97, 5);
let before = solo(model, &lane, true);
let start = Instant::now() + Duration::from_secs(3_600);
let used = |at: Instant| {
model
.keep_prefill_weights_fresh_at(at)
.expect("keep the prefill-only weights fresh")
};
assert!(used(start), "the first use is due");
assert!(!used(start + INTERVAL / 2), "one use per interval");
assert!(used(start + INTERVAL));
assert!(used(start + 3 * INTERVAL));
let after = solo(model, &lane, true);
let differences = before.differences(&after);
assert!(
differences.is_empty(),
"the keep-alive changed {differences:?}"
);
});
}
#[test]
fn a_lane_reset_after_a_shared_prefill_leaves_the_other_lanes_untouched() {
with_synthetic_model(|model| {
let lanes = [
Lane::cold(0, 96, 150, 5),
Lane::cold(1, 96, 120, 5),
Lane::cold(2, 96, 110, 0),
];
let mut cache = cache(model);
let forward =
joint_forward(model, &mut cache, &lanes, 96, true).expect("shared joint forward");
let _scope = PrefillScope::forced(true);
let held = |cache: &mut HybridKvCache, lane: &Lane| {
(
cache
.snapshot_slot_anchor(SlotId(lane.slot), lane.rows())
.expect("lane state")
.test_words(),
kv_rows(model, cache, lane.slot),
)
};
let before: Vec<_> = lanes[1..]
.iter()
.map(|lane| held(&mut cache, lane))
.collect();
let evicted = lanes[0];
cache.reset_for_slot(SlotId(evicted.slot)).expect("reset");
let other = Lane {
seed: 41,
common: 0,
..Lane::new(evicted.slot, &[], 300, 5)
};
let other_solo = solo(model, &other, true);
let other_here = joint_forward(model, &mut cache, &[other], 0, true)
.expect("the other conversation in the reused slot")
.remove(0);
let same_generation = |boundary: Option<(Vec<u32>, AnchorWords)>| {
boundary.map(|(logits, mut words)| {
words.1 = 0;
(logits, words)
})
};
assert!(
same_generation(other_solo.boundary) == same_generation(other_here.0)
&& other_solo.logits == other_here.1
&& other_solo.captured == other_here.2,
"a slot that shared a prefix does not serve its next conversation as a fresh slot"
);
cache.reset_for_slot(SlotId(evicted.slot)).expect("reset");
let after: Vec<_> = lanes[1..]
.iter()
.map(|lane| held(&mut cache, lane))
.collect();
assert!(
before == after,
"resetting and reusing one lane's slot changed another lane's bytes"
);
for ((lane, (boundary, logits, captured)), index) in
lanes.iter().zip(forward).zip(0..).skip(1)
{
let alone = solo(model, lane, true);
let (after, kv, follow_up) = read_back(model, &mut cache, lane);
let here = Outcome {
boundary,
logits,
captured,
after,
kv,
follow_up,
};
let differences = alone.differences(&here);
assert!(
differences.is_empty(),
"lane {index} after its neighbour's reset differs from its solo prefill in \
{differences:?}"
);
}
});
}
#[test]
fn an_inadmissible_joint_prefill_is_refused_before_a_slot_changes() {
with_synthetic_model(|model| {
let mut cache = cache(model);
let cursors = |cache: &HybridKvCache| -> Vec<u32> {
(0..SLOTS)
.map(|slot| cache.sequence_len_for_slot(SlotId(slot)).expect("cursor") as u32)
.collect()
};
let refused: [(&[Lane], usize, &str); 5] = [
(
&[Lane::new(0, &[], 40, 5), Lane::new(0, &[], 20, 0)],
0,
"one slot twice",
),
(
&[Lane::new(0, &[], 40, 5), Lane::new(1, &[], 1, 0)],
0,
"a one-row lane",
),
(
&[Lane::new(0, &[], 40, 1), Lane::new(1, &[], 9, 0)],
0,
"a one-row cue",
),
(
&[Lane::cold(0, 40, 41, 5), Lane::cold(1, 40, 60, 0)],
40,
"a shared prefix that leaves a lane one content row",
),
(
&[
Lane::cold(0, 40, 60, 5),
Lane::cold(1, 40, 60, 0),
Lane::new(2, &[], 9, 0),
],
0,
"accepted",
),
];
for (lanes, shared_rows, what) in refused {
let result = joint_forward(model, &mut cache, lanes, shared_rows, true);
if what == "accepted" {
assert!(result.is_ok(), "the control cohort was refused");
assert_eq!(cursors(&cache), vec![65, 60, 9, 0]);
} else {
assert!(result.is_err(), "{what} was accepted");
assert_eq!(cursors(&cache), vec![0; 4], "{what} moved a slot");
}
}
let mut cache = self::cache(model);
let _scope = PrefillScope::forced(true);
let (first, second) = (Lane::cold(0, 40, 60, 0), Lane::cold(1, 39, 60, 0));
let chunks: Vec<(Vec<u32>, Vec<i32>)> = [first, second]
.iter()
.map(|lane| (lane.tokens(0, 60), positions_for_range(0, 60)))
.collect();
let lanes: Vec<JointPrefillLane<'_>> = [first, second]
.iter()
.zip(&chunks)
.map(|(lane, (tokens, positions))| JointPrefillLane {
slot_id: SlotId(lane.slot),
tokens,
positions_flat: positions,
shares_prefix: true,
fold: None,
image: None,
})
.collect();
assert!(
model
.forward_gpu_last_logits_joint(&lanes, 40, &mut cache, None)
.is_err(),
"lanes that differ inside the shared prefix were accepted"
);
assert_eq!(cursors(&cache), vec![0; 4]);
});
}
#[test]
#[ignore = "requires HF2Q_QWEN38_GGUF or HF2Q_QWEN36_GGUF and Apple Metal; run alone"]
fn qwen_prefill_joint_real_model_gate() {
use std::time::Instant;
use crate::serve::header::LoadProgress;
use mlx_native::gguf::GgufFile;
const PATTERN: [u32; 7] = [9707, 374, 279, 15, 1_782, 2_610, 13];
const COMMON: usize = 444;
const CONTENT: usize = 720;
const CUE: usize = 5;
const REPLY: usize = 150;
const LATER: [usize; 4] = [31, 157, 200, 266];
const GATE_CAPACITY: u32 = 4096;
const GATE_SLOTS: u32 = 4;
let _gpu = crate::inference::hf2q_gpu_test_lock();
let (path, capture_layers) = match (
std::env::var_os("HF2Q_QWEN38_GGUF"),
std::env::var_os("HF2Q_QWEN36_GGUF"),
) {
(Some(path), None) => (path, vec![5, 19, 33, 47, 61]),
(None, Some(path)) => (path, vec![1, 6, 11, 16, 22, 27, 32, 37]),
_ => panic!("set exactly one of HF2Q_QWEN38_GGUF and HF2Q_QWEN36_GGUF"),
};
let path = std::path::PathBuf::from(path);
let gguf = GgufFile::open(&path).expect("open GGUF");
let mut progress = LoadProgress::new(false, 1, 0);
let mut model = Qwen35Model::load_from_gguf(&gguf, &mut progress).expect("load Qwen model");
let policy = super::routing_policy::resolve_qwen_ggml_routing_policy(&gguf, &model.cfg);
model.bind_ggml_routing_policy(policy);
let model = model
.finalize_canonical_experts()
.expect("finalize bound experts before joint prefill caches");
model.ensure_gpu_cache_primed().expect("prime GPU cache");
let hidden = model.cfg.hidden_size as usize;
let vocab = model.cfg.vocab_size;
let lane_tokens = |slot: u32, start: usize, len: usize| -> Vec<u32> {
(start..start + len)
.map(|index| {
let own = if index < COMMON { 0 } else { slot as usize + 1 };
(PATTERN[(index + own) % PATTERN.len()] + (own * 17) as u32) % vocab
})
.collect()
};
let new_cache = || {
model
.with_gpu_cache_mut(|device, _registry| {
HybridKvCache::new_with_options(&model.cfg, device, GATE_CAPACITY, GATE_SLOTS, true)
})
.expect("allocate TQ cache")
};
let _scope = PrefillScope::forced(true);
assert!(
model.prefill_fold_exact().expect("exactness"),
"a joint prefill is not exact for this artifact on the prepared route"
);
let heads = model.cfg.num_key_value_heads as usize;
let kv = |cache: &HybridKvCache, slot: u32| -> Vec<u8> {
let rows = cache.sequence_len_for_slot(SlotId(slot)).expect("cursor") as usize;
let mut bytes = Vec::new();
for layer in &cache.full_attn {
let tq = layer.tq.as_ref().expect("TQ full-attention slot");
for buffer in [&tq.k_packed, &tq.k_norms, &tq.v_packed, &tq.v_norms] {
let total = buffer.byte_len();
let per_head = total / (GATE_SLOTS as usize * heads);
let per_row = per_head / GATE_CAPACITY as usize;
let all = unsafe {
std::slice::from_raw_parts(buffer.contents_ptr().cast::<u8>(), total)
};
for head in 0..heads {
let at = (slot as usize * heads + head) * per_head;
bytes.extend_from_slice(&all[at..at + rows * per_row]);
}
}
}
bytes
};
type LaneBits = (
Vec<u32>,
AnchorWords,
Vec<u32>,
Vec<Vec<u32>>,
AnchorWords,
Vec<u8>,
);
let turn = |cache: &mut HybridKvCache,
mode: u8,
starts: [usize; 4],
contents: [usize; 4],
shared: usize|
-> (f64, Vec<LaneBits>) {
let chunks: Vec<(Vec<u32>, Vec<i32>)> = (0..4)
.map(|slot| {
let len = contents[slot] + CUE;
(
lane_tokens(slot as u32, starts[slot], len),
positions_for_range(starts[slot] as i32, len),
)
})
.collect();
let read = |cache: &mut HybridKvCache, slot: usize| {
(
cache
.snapshot_slot_anchor(SlotId(slot as u32), starts[slot] + contents[slot] + CUE)
.expect("lane state")
.test_words(),
kv(cache, slot as u32),
)
};
let started = Instant::now();
let mut lanes: Vec<LaneBits> = Vec::with_capacity(4);
if mode == 0 {
let mut forwards = Vec::with_capacity(4);
for slot in 0..4 {
let len = contents[slot] + CUE;
let mut session =
DFlashCaptureSession::new(capture_layers.clone(), len, hidden, false);
let image = cache.boundary_anchor_image().expect("checkpoint storage");
let folded = model
.forward_gpu_last_logits_folded(
&chunks[slot].0,
&chunks[slot].1,
cache,
Some(&mut session),
SlotId(slot as u32),
contents[slot],
starts[slot] + contents[slot],
image,
)
.expect("solo folded chunk");
forwards.push((folded, session));
}
let wall = started.elapsed().as_secs_f64() * 1e3;
for (slot, (folded, session)) in forwards.into_iter().enumerate() {
let len = contents[slot] + CUE;
let (after, kv) = read(cache, slot);
lanes.push((
bits(&folded.boundary_logits),
folded.boundary.expect("solo checkpoint").test_words(),
bits(&folded.logits),
session
.hidden_output
.chunks(len * hidden)
.map(bits)
.collect(),
after,
kv,
));
}
return (wall, lanes);
}
let shared = if mode == 2 { shared } else { 0 };
let joint_lanes: Vec<JointPrefillLane<'_>> = (0..4)
.map(|slot| JointPrefillLane {
slot_id: SlotId(slot as u32),
tokens: &chunks[slot].0,
positions_flat: &chunks[slot].1,
shares_prefix: shared > 0,
fold: Some((contents[slot], starts[slot] + contents[slot])),
image: None,
})
.collect();
let total = Qwen35Model::joint_prefill_rows(&joint_lanes, shared);
let mut session = DFlashCaptureSession::new(capture_layers.clone(), total, hidden, false);
let outputs = model
.forward_gpu_last_logits_joint(&joint_lanes, shared, cache, Some(&mut session))
.expect("joint forward");
let wall = started.elapsed().as_secs_f64() * 1e3;
for (slot, output) in outputs.into_iter().enumerate() {
let len = contents[slot] + CUE;
let captured = (0..capture_layers.len())
.map(|layer| {
output
.forward_rows(0..len)
.into_iter()
.flat_map(|rows| {
let at = layer * total + rows.start;
bits(&session.hidden_output[at * hidden..(at + rows.len()) * hidden])
})
.collect()
})
.collect();
let (boundary_logits, boundary, _wall) = output.boundary.expect("lane checkpoint");
let boundary = boundary.expect("lane checkpoint state");
let (after, kv) = read(cache, slot);
lanes.push((
bits(&boundary_logits),
boundary.test_words(),
bits(&output.logits),
captured,
after,
kv,
));
}
(wall, lanes)
};
let reply = |cache: &mut HybridKvCache, starts: [usize; 4]| {
for slot in 0..4u32 {
model
.forward_gpu_last_logits(
&lane_tokens(slot, starts[slot as usize], REPLY),
&positions_for_range(starts[slot as usize] as i32, REPLY),
cache,
SlotId(slot),
)
.expect("reply rows");
}
};
let names = ["solo", "joint", "joint+shared"];
let mut reference: Option<Vec<Vec<LaneBits>>> = None;
for pass in 0..2 {
for mode in 0..3u8 {
let mut cache = new_cache();
let (first_ms, first) = turn(&mut cache, mode, [0; 4], [CONTENT; 4], COMMON);
let after_first = [CONTENT + CUE; 4];
reply(&mut cache, after_first);
let later_starts = [CONTENT + CUE + REPLY; 4];
let (later_ms, later) = turn(&mut cache, mode, later_starts, LATER, 0);
let rows_first = if mode == 2 {
COMMON + 4 * (CONTENT + CUE - COMMON)
} else {
4 * (CONTENT + CUE)
};
eprintln!(
"[PREFILL_JOINT_GATE] pass={pass} mode={} first_turn_ms={first_ms:.1} \
first_turn_forward_rows={rows_first} later_turn_ms={later_ms:.1} \
later_turn_rows={}",
names[mode as usize],
LATER.iter().sum::<usize>() + 4 * CUE
);
let bits = vec![first, later];
match reference.as_ref() {
None => reference = Some(bits),
Some(reference) => {
for (turn_index, (expected, actual)) in reference.iter().zip(&bits).enumerate()
{
for (slot, (expected, actual)) in expected.iter().zip(actual).enumerate() {
let parts = [
("boundary logits", expected.0 == actual.0),
("boundary checkpoint", expected.1 == actual.1),
("last-row logits", expected.2 == actual.2),
("captured target layers", expected.3 == actual.3),
("slot state", expected.4 == actual.4),
("full-attention rows", expected.5 == actual.5),
];
let differences: Vec<_> = parts
.iter()
.filter_map(|(name, equal)| (!equal).then_some(*name))
.collect();
assert!(
differences.is_empty(),
"pass {pass} mode {} turn {turn_index} slot {slot}: differs from \
the solo prefill in {differences:?}",
names[mode as usize]
);
}
}
}
}
}
}
}
fn dn_state_nonzero(cache: &HybridKvCache, slot: u32) -> usize {
let count = |buffer: &mlx_native::MlxBuffer| -> usize {
let all = buffer.as_slice::<f32>().expect("state words");
let per_slot = all.len() / SLOTS as usize;
all[slot as usize * per_slot..(slot as usize + 1) * per_slot]
.iter()
.filter(|value| value.to_bits() != 0)
.count()
};
cache
.linear_attn
.iter()
.map(|layer| {
count(&layer.conv_state)
+ count(&layer.recurrent)
+ count(&layer.conv_state_scratch)
+ count(&layer.recurrent_scratch)
})
.sum()
}
fn held(model: &Qwen35Model, cache: &mut HybridKvCache, slot: u32, rows: usize) -> (AnchorWords, Vec<u8>) {
(
cache
.snapshot_slot_anchor(SlotId(slot), rows)
.expect("slot state")
.test_words(),
kv_rows(model, cache, slot),
)
}
fn next_conversation(model: &Qwen35Model, cache: &mut HybridKvCache, next: &Lane) -> Vec<u32> {
let _scope = PrefillScope::forced(true);
let image = cache.boundary_anchor_image().expect("image");
let folded = model
.forward_gpu_last_logits_folded(
&next.tokens(0, 65),
&positions_for_range(0, 65),
cache,
None,
SlotId(0),
60,
60,
image,
)
.expect("next conversation");
bits(&folded.logits)
}
#[test]
fn a_failed_forward_leaves_nothing_running_behind_the_slot_reset() {
use super::forward_gpu::FailForwardAtLayer;
use super::prefill_prepared::ForcedWaitLayers;
with_synthetic_model(|model| {
let next = Lane {
seed: 41,
common: 0,
..Lane::new(0, &[], 60, 5)
};
let reference = solo(model, &next, false).logits;
for wait in [1usize, 2, 8] {
for admission_reset in [false, true] {
for round in 0..3 {
let what = format!("wait {wait}, admission reset {admission_reset}, round {round}");
let _cadence = ForcedWaitLayers::set(wait);
let mut cache = cache(model);
let victim = Lane::new(0, &[40], 800, 0);
let other = Lane::new(1, &[], 500, 0);
let bystander = Lane::new(3, &[155], 9, 0);
{
let _scope = PrefillScope::forced(true);
prefill_history(model, &mut cache, &victim);
prefill_history(model, &mut cache, &bystander);
}
let before = held(model, &mut cache, 3, 155);
let failure = {
let _fail = FailForwardAtLayer::set(7);
joint_forward(model, &mut cache, &[other, victim], 0, false)
};
let error = failure.err().map(|error| format!("{error:#}"));
assert!(
error.as_deref().is_some_and(|text| text.contains("injected forward failure")),
"{what}: the injected failure did not surface: {error:?}"
);
cache.reset_for_slot(SlotId(0)).expect("reset");
cache.reset_for_slot(SlotId(1)).expect("reset");
if admission_reset {
cache.reset_for_slot(SlotId(0)).expect("admission reset");
}
assert_eq!(
dn_state_nonzero(&cache, 1),
0,
"{what}: a reset slot holds DeltaNet state"
);
assert!(
next_conversation(model, &mut cache, &next) == reference,
"{what}: the next conversation in a failed slot differs from a fresh slot"
);
assert_eq!(
dn_state_nonzero(&cache, 1),
0,
"{what}: a command of the failed forward wrote a reset slot"
);
assert!(
before == held(model, &mut cache, 3, 155),
"{what}: a slot that was no lane of the failed forward changed"
);
}
}
}
});
}
#[test]
fn a_cohort_retried_after_a_failed_joint_forward_holds_its_solo_bits() {
use super::forward_gpu::FailForwardAtLayer;
use super::prefill_prepared::ForcedWaitLayers;
with_synthetic_model(|model| {
for wait in [1usize, 8] {
let _cadence = ForcedWaitLayers::set(wait);
let mut cache = cache(model);
let bystander = Lane::new(3, &[155], 9, 0);
{
let _scope = PrefillScope::forced(true);
prefill_history(model, &mut cache, &bystander);
}
let before = held(model, &mut cache, 3, 155);
let lanes = [
Lane::cold(0, 96, 150, 5),
Lane::cold(1, 96, 120, 5),
Lane::cold(2, 96, 110, 0),
];
let failure = {
let _fail = FailForwardAtLayer::set(5);
joint_forward(model, &mut cache, &lanes, 96, true)
};
assert!(failure.is_err(), "wait {wait}: the injected failure did not surface");
for lane in &lanes {
cache.reset_for_slot(SlotId(lane.slot)).expect("reset");
}
assert!(
before == held(model, &mut cache, 3, 155),
"wait {wait}: bystander changed by a failed joint forward"
);
let retry = joint_forward(model, &mut cache, &lanes, 96, true).expect("retry");
for (lane, (boundary, logits, captured)) in lanes.iter().zip(retry) {
let alone = solo(model, lane, true);
let same_generation = |boundary: Option<(Vec<u32>, AnchorWords)>| {
boundary.map(|(logits, mut words)| {
words.1 = 0;
(logits, words)
})
};
assert!(
same_generation(alone.boundary) == same_generation(boundary)
&& alone.logits == logits
&& alone.captured == captured,
"wait {wait}: {lane:?} retried after a failed joint forward differs from solo"
);
}
assert!(
before == held(model, &mut cache, 3, 155),
"wait {wait}: bystander changed by the retry"
);
}
});
}
fn joint_forward_with_a_failed_command(
model: &Qwen35Model,
reference: &[u32],
next: &Lane,
wait: usize,
prefix: &str,
occurrence: usize,
) -> (Option<String>, bool) {
use super::encoder_stage::FailSubmitted;
use super::prefill_prepared::ForcedWaitLayers;
let _cadence = ForcedWaitLayers::set(wait);
let mut cache = cache(model);
let lanes = [
Lane::cold(0, 96, 150, 5),
Lane::cold(1, 96, 120, 0),
Lane::cold(2, 96, 110, 0),
];
let fail = FailSubmitted::nth(prefix, occurrence);
let result = joint_forward(model, &mut cache, &lanes, 96, false);
let (hit, seen) = fail.seen();
drop(fail);
let error = result.err().map(|error| format!("{error:#}"));
assert_eq!(
hit,
error.is_some(),
"wait {wait}, {prefix}: a failed command buffer and the forward's result disagree \
({seen} such commands settled, error {error:?})"
);
if let Some(text) = &error {
assert!(
text.contains("committed without a wait failed") && text.contains(prefix),
"wait {wait}, {prefix}: not the kept command's failure: {text}"
);
for lane in &lanes {
cache.reset_for_slot(SlotId(lane.slot)).expect("reset");
}
assert_eq!(dn_state_nonzero(&cache, 1), 0, "wait {wait}, {prefix}: reset slot holds state");
assert!(
next_conversation(model, &mut cache, next) == reference,
"wait {wait}, {prefix}: the next conversation after the failure differs from a fresh slot"
);
assert_eq!(dn_state_nonzero(&cache, 1), 0, "wait {wait}, {prefix}: a late command wrote a reset slot");
}
(error, seen > 0)
}
#[test]
fn every_command_buffer_committed_without_a_wait_can_fail_the_forward() {
use super::encoder_stage::LayerEncoder;
with_synthetic_model(|model| {
let session = LayerEncoder::env_enabled();
let next = Lane {
seed: 41,
common: 0,
..Lane::new(0, &[], 60, 5)
};
let reference = solo(model, &next, false).logits;
for wait in [1usize, 2, 8] {
let fails = |prefix: &str, occurrence: usize| {
joint_forward_with_a_failed_command(model, &reference, &next, wait, prefix, occurrence)
};
for label in ["layer.full_attn.joint_gather", "layer.full_attn.cue_fold_join"] {
for occurrence in [0, 1] {
let (error, _) = fails(label, occurrence);
assert!(
error.is_some(),
"wait {wait} (session {session}): a failed {label} command was dropped"
);
}
}
for label in ["layer.gdn.stage_a", "layer.full_attn."] {
let (error, kept) = fails(label, 0);
assert_eq!(
error.is_some(),
kept,
"wait {wait} (session {session}): a failed {label} command was dropped"
);
if wait == 1 {
assert!(kept, "wait 1 (session {session}): no {label} stage command was kept");
}
}
let (error, kept) = fails("layer.gdn.ops5-9", 1);
assert_eq!(
kept,
!(session && wait > 1),
"wait {wait} (session {session}): where the recurrence stage is its own command buffer"
);
assert_eq!(error.is_some(), kept, "wait {wait}: a failed recurrence command was dropped");
let (error, kept) = fails("layer.dense_ffn", 0);
assert_eq!(
error.is_some(),
kept,
"wait {wait}: a failed layer command buffer was dropped"
);
if wait > 1 {
assert!(kept, "wait {wait}: no layer was submitted without a wait");
}
}
});
}
#[test]
fn a_failed_embedding_command_fails_the_forward() {
use crate::serve::forward_mlx_shared::MlxQWeight;
use crate::serve::gpu::QuantWeightInfo;
use mlx_native::ops::quantized_matmul_ggml::GgmlType;
super::prefill_prepared_tests::with_synthetic_model_mut(|model| {
let (vocab, hidden) = (model.cfg.vocab_size, model.cfg.hidden_size);
let mut seed = 0x0E3B_ED01_u32;
let buffer = model
.with_gpu_cache_mut(|device, _registry| {
Ok(super::joint_decode_test_support::synthetic_blocks(
device,
GgmlType::Q4_K,
vocab,
hidden,
&mut seed,
))
})
.expect("Q4_K embedding blocks");
model.token_embd_native = Some(MlxQWeight {
buffer,
info: QuantWeightInfo {
ggml_dtype: GgmlType::Q4_K,
rows: vocab as usize,
cols: hidden as usize,
},
affine: None,
f16_shadow: None,
decode_record_q6k_m1: std::sync::OnceLock::new(),
});
let model = &*model;
let next = Lane {
seed: 41,
common: 0,
..Lane::new(0, &[], 60, 5)
};
let reference = solo(model, &next, false).logits;
for wait in [1usize, 2, 8] {
let (error, kept) =
joint_forward_with_a_failed_command(model, &reference, &next, wait, "qwen35.embedding", 0);
assert!(kept, "wait {wait}: the forward kept no embedding command buffer");
assert!(error.is_some(), "wait {wait}: a failed embedding command was dropped");
}
});
}
#[test]
fn a_failed_command_buffer_of_the_last_window_fails_the_forward() {
use super::encoder_stage::FailSubmitted;
use super::prefill_prepared::ForcedWaitLayers;
with_synthetic_model(|model| {
let lane = Lane::new(0, &[40], 157, 0);
for wait in [1usize, 8] {
let _cadence = ForcedWaitLayers::set(wait);
let run = |fail: Option<usize>| -> (anyhow::Result<Vec<f32>>, usize) {
let mut cache = cache(model);
let _scope = PrefillScope::forced(true);
prefill_history(model, &mut cache, &lane);
let point = fail.map(|occurrence| FailSubmitted::nth("", occurrence));
let counter = point.is_none().then(|| FailSubmitted::nth("", usize::MAX));
let result = model.forward_gpu_last_logits(
&lane.tokens(lane.start(), lane.rows()),
&positions_for_range(lane.start() as i32, lane.rows()),
&mut cache,
SlotId(0),
);
let seen = point.as_ref().or(counter.as_ref()).map_or(0, |point| point.seen().1);
(result, seen)
};
let (clean, kept) = run(None);
clean.expect("the forward without a failpoint");
assert!(kept > 0, "wait {wait}: the forward kept no command buffer");
for back in 1..=kept.min(2) {
let (result, _) = run(Some(kept - back));
let text = result
.err()
.map(|error| format!("{error:#}"))
.unwrap_or_else(|| {
panic!("wait {wait}: kept command {} of {kept} failed and the forward returned logits", kept - back + 1)
});
assert!(
text.contains("committed without a wait failed"),
"wait {wait}: not the kept command's failure: {text}"
);
}
}
});
}
#[test]
fn the_scratch_caps_bound_the_pool_and_change_no_bit() {
use super::decode_pool;
use super::prefill_prepared::{ForcedWaitLayers, ScratchCapsForTest};
with_synthetic_model(|model| {
let lanes = [
Lane::cold(0, 96, 507, 5),
Lane::cold(1, 96, 400, 0),
Lane::cold(2, 96, 300, 5),
];
let held = || {
decode_pool::checked_reset_decode_pool().expect("reset");
decode_pool::free_bytes()
};
let run = |wait: usize| {
let _cadence = ForcedWaitLayers::set(wait);
joint(model, &lanes, 96, false)
};
let unlimited = usize::MAX;
let every_layer = {
let _caps = ScratchCapsForTest::set(unlimited, unlimited, unlimited);
run(1)
};
let one_layer = held();
assert!(one_layer > 0, "the joint forward pooled no scratch");
let capped = {
let _caps = ScratchCapsForTest::set(0, unlimited, unlimited);
run(8)
};
assert_eq!(held(), one_layer, "a window cap of zero still let layers skip their wait");
let uncapped = {
let _caps = ScratchCapsForTest::set(unlimited, unlimited, unlimited);
run(8)
};
let accumulated = held();
assert!(
accumulated > one_layer,
"eight layers between waits pooled no more than one layer ({accumulated} against {one_layer} bytes): \
the test would not see a missing cap"
);
let released = {
let _caps = ScratchCapsForTest::set(unlimited, one_layer / 2, unlimited);
run(8)
};
let after = held();
assert!(
after <= one_layer / 2,
"the pool kept {after} bytes over a retained cap of {} bytes",
one_layer / 2
);
let before = held();
let hard = {
let _caps = ScratchCapsForTest::set(unlimited, unlimited, before);
run(8)
};
assert!(
held() <= before,
"the pool grew from {before} to {} bytes under a hard cap of {before} bytes",
held()
);
let nothing = {
let _caps = ScratchCapsForTest::set(unlimited, 0, 0);
run(8)
};
assert_eq!(held(), 0, "a hard cap of zero left buffers in the pool");
for (what, other) in [
("window threshold", &capped),
("no cap", &uncapped),
("retained cap", &released),
("hard cap", &hard),
("hard cap of zero", ¬hing),
] {
for ((lane, a), b) in lanes.iter().zip(&every_layer).zip(other) {
let differences = a.differences(b);
assert!(
differences.is_empty(),
"{what}: {lane:?} differs from the forward that waits after every layer in {differences:?}"
);
}
}
});
}
#[test]
fn caps_trim_a_pool_that_uncapped_forwards_left_warm() {
use super::decode_pool;
use super::prefill_prepared::{ForcedWaitLayers, ScratchCapsForTest};
with_synthetic_model(|model| {
let lane = Lane::new(0, &[40], 800, 0);
let run = |wait: usize| -> Vec<u32> {
let _cadence = ForcedWaitLayers::set(wait);
let mut cache = cache(model);
let _scope = PrefillScope::forced(true);
prefill_history(model, &mut cache, &lane);
let logits = model
.forward_gpu_last_logits(
&lane.tokens(lane.start(), lane.rows()),
&positions_for_range(lane.start() as i32, lane.rows()),
&mut cache,
SlotId(0),
)
.expect("forward");
bits(&logits)
};
decode_pool::take_capped_free_peak();
let reference = run(1);
assert_eq!(
decode_pool::take_capped_free_peak(),
0,
"the warming forward ran under caps"
);
decode_pool::checked_reset_decode_pool().expect("reset");
let warm = decode_pool::free_bytes();
assert!(warm > 0, "the uncapped forward left no pool");
let retained = warm / 8;
let capped = {
let _caps = ScratchCapsForTest::set(usize::MAX, retained, warm / 4);
run(8)
};
let peak = decode_pool::take_capped_free_peak();
assert!(
peak <= retained,
"the pool held {peak} free bytes under a retained cap of {retained} (warm pool {warm})"
);
decode_pool::checked_reset_decode_pool().expect("reset");
assert!(
decode_pool::free_bytes() <= retained,
"the pool kept {} bytes over the retained cap of {retained}",
decode_pool::free_bytes()
);
assert!(capped == reference, "the trimmed, capped forward changed bits");
});
}
#[test]
fn a_trimmed_warm_pool_leaves_the_device_at_once() {
let output = std::process::Command::new(std::env::current_exe().expect("test binary"))
.args([
"inference::models::qwen35::prefill_joint_tests::warm_pool_trim_in_a_child",
"--exact",
"--nocapture",
"--test-threads=1",
])
.env("P834_TRIM_CHILD", "1")
.output()
.expect("run the child test process");
let text = format!(
"{}{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
assert!(
output.status.success() && text.contains("[TRIM_CHILD] released") && text.contains("1 passed"),
"the child process did not see the device memory fall at the trim:\n{text}"
);
}
#[test]
fn warm_pool_trim_in_a_child() {
use super::decode_pool;
use super::prefill_prepared::{ForcedWaitLayers, ScratchCapsForTest};
if std::env::var_os("P834_TRIM_CHILD").is_none() {
return;
}
with_synthetic_model(|model| {
let lane = Lane::new(0, &[40], 800, 0);
let run = |wait: usize| {
let _cadence = ForcedWaitLayers::set(wait);
let mut cache = cache(model);
let _scope = PrefillScope::forced(true);
prefill_history(model, &mut cache, &lane);
model
.forward_gpu_last_logits(
&lane.tokens(lane.start(), lane.rows()),
&positions_for_range(lane.start() as i32, lane.rows()),
&mut cache,
SlotId(0),
)
.expect("forward");
};
run(1);
decode_pool::checked_reset_decode_pool().expect("reset");
let warm = decode_pool::free_bytes();
assert!(warm > 0, "the uncapped forward left no pool");
let device = mlx_native::MlxDevice::new().expect("device");
decode_pool::set_trim_probe(Some(Box::new(move || {
device.metal_device().current_allocated_size() as usize
})));
decode_pool::take_trim_sample();
{
let _caps = ScratchCapsForTest::set(usize::MAX, warm / 8, warm / 4);
run(8);
}
decode_pool::set_trim_probe(None);
let (freed, before, after) =
decode_pool::take_trim_sample().expect("no trim at the cap activation");
eprintln!(
"[TRIM_CHILD] warm pool {warm} B, released {freed} B, device allocated {before} -> {after} B"
);
assert_eq!(freed, warm, "the trim released something other than the warm pool");
assert!(
before.saturating_sub(after) >= freed,
"the device's allocated bytes fell by {} of the {freed} released",
before.saturating_sub(after)
);
eprintln!("[TRIM_CHILD] released");
});
}
#[test]
fn without_the_layer_scratch_reset_a_forward_keeps_its_waits_and_runs_uncapped() {
use super::decode_pool;
use super::encoder_stage::FailSubmitted;
use super::forward_gpu::NoScratchResetForTest;
use super::prefill_prepared::{ForcedWaitLayers, ScratchCapsForTest};
with_synthetic_model(|model| {
let lane = Lane::new(0, &[40], 300, 0);
let run = |wait: usize, no_reset: bool| -> (Vec<u32>, usize, usize) {
let _cadence = ForcedWaitLayers::set(wait);
let _override = no_reset.then(NoScratchResetForTest::set);
let _caps = ScratchCapsForTest::set(usize::MAX, usize::MAX, 0);
let mut cache = cache(model);
let _scope = PrefillScope::forced(true);
prefill_history(model, &mut cache, &lane);
decode_pool::checked_reset_decode_pool().expect("reset");
let before = decode_pool::free_bytes();
let layers = FailSubmitted::nth("layer.dense_ffn", usize::MAX);
let logits = model
.forward_gpu_last_logits(
&lane.tokens(lane.start(), lane.rows()),
&positions_for_range(lane.start() as i32, lane.rows()),
&mut cache,
SlotId(0),
)
.expect("forward");
let unwaited_layers = layers.seen().1;
drop(layers);
decode_pool::checked_reset_decode_pool().expect("reset");
(bits(&logits), unwaited_layers, decode_pool::free_bytes() - before)
};
let (reference, _, _) = run(1, false);
let (pipelined, unwaited, grown) = run(8, false);
assert!(unwaited > 0, "cadence 8 committed no layer without a wait");
assert_eq!(grown, 0, "the capped forward grew the pool under a hard cap of zero");
assert!(pipelined == reference, "cadence 8 changed bits");
let (kept_waits, unwaited, grown) = run(8, true);
assert_eq!(unwaited, 0, "a forward without the scratch reset skipped a host wait");
assert!(grown > 0, "a forward without the scratch reset ran under pool caps");
assert!(kept_waits == reference, "the override changed bits");
});
}
#[test]
fn the_joint_arena_bound_covers_the_arenas_a_segment_allocates() {
use super::full_attn::FullAttnShape;
use super::prefill_memory::{attention_arena_bytes, ffn_arena_bytes};
use mlx_native::metal::foreign_types::ForeignType;
let check = |model: &Qwen35Model, what: &str| {
let cfg = &model.cfg;
let shape = FullAttnShape::from_config(cfg);
for rows in [5u32, 37, 300] {
let allocated = model
.with_gpu_cache_mut(|device, _registry| {
let prefill = super::FaPrefillArena::new(
device,
rows,
shape.n_head,
shape.n_kv,
shape.head_dim,
)?;
let projections = super::FaProjectionsArena::new(
device,
rows,
shape.hidden_size,
shape.n_head,
shape.n_kv,
shape.head_dim,
shape.rms_norm_eps,
)?;
let owner = |buffer: &mlx_native::MlxBuffer| {
(buffer.metal_buffer().as_ptr() as usize, buffer.byte_len())
};
let mut buffers = vec![
owner(&prefill.q_bf16_seq),
owner(&prefill.q_bf16_hm),
owner(&prefill.k_bf16_seq),
owner(&prefill.k_bf16_hm),
owner(&prefill.v_bf16_seq),
owner(&prefill.v_bf16_hm),
owner(&prefill.out_bf16_hm),
owner(&projections.x_norm_buf),
owner(&projections.pre_norm_params_buf),
owner(&projections.q_proj_buf),
owner(&projections.k_proj_buf),
owner(&projections.v_proj_buf),
owner(&projections.gate_proj_buf),
owner(&projections.q_normed_buf),
owner(&projections.k_normed_buf),
owner(&projections.qk_rms_params_buf),
owner(&projections.q_rope_buf),
owner(&projections.k_rope_buf),
owner(&projections.gated_buf),
owner(&projections.sigmoid_params_buf),
];
if let Some(moe) = cfg.moe.as_ref() {
let arena = super::MoeFfnArena::new(
device,
rows,
cfg.hidden_size,
moe.num_experts_per_tok,
moe.moe_intermediate_size,
moe.shared_expert_intermediate_size,
moe.num_experts,
)?;
buffers.extend([
owner(&arena.ids_buf),
owner(&arena.weights_buf),
owner(&arena.gate_all_buf),
owner(&arena.up_all_buf),
owner(&arena.h_all_buf),
owner(&arena.y_all_buf),
owner(&arena.h_s_buf),
owner(&arena.silu_params_buf),
owner(&arena.silu_sh_params_buf),
owner(&arena.dummy_residual_buf),
owner(&arena.logits_buf),
owner(&arena.sh_logit_buf),
owner(&arena.a_s_buf),
owner(&arena.b_s_buf),
]);
}
buffers.sort_unstable_by_key(|&(identity, _)| identity);
buffers.dedup_by_key(|&mut (identity, _)| identity);
Ok(buffers.iter().map(|&(_, bytes)| bytes).sum::<usize>() as u64)
})
.expect("arenas");
let bound = attention_arena_bytes(cfg, rows as usize).unwrap()
+ ffn_arena_bytes(cfg, rows as usize, 0).unwrap()
+ 64 * 16 * 1024;
assert!(
allocated <= bound,
"{what}: the arenas of a {rows}-row segment hold {allocated} bytes, over the bound of {bound}"
);
}
};
with_synthetic_model(|model| check(model, "dense FFN"));
super::prefill_fold_tests::with_synthetic_moe_model(|model| check(model, "MoE FFN"));
}
#[test]
fn a_layer_requests_no_more_scratch_than_its_derived_bound() {
use super::decode_pool;
use super::prefill_prepared::prefill_layer_scratch_bound_bytes as bound;
let check = |model: &Qwen35Model, what: &str| {
let mut tightest = f64::INFINITY;
let shared_result = super::resume_attn_fast::resume_attn_fast_enabled();
let mut probe = |name: &str, rows: usize, lanes: usize, shared: bool, run: &mut dyn FnMut()| {
decode_pool::take_layer_scratch_peak();
run();
let peak = decode_pool::take_layer_scratch_peak();
let limit = bound(&model.cfg, rows, lanes, shared);
assert!(peak > 0, "{what}, {name}: the forward requested no scratch");
assert!(
peak <= limit,
"{what}, {name}: a layer requested {peak} bytes of scratch, over the bound of {limit} bytes \
for {rows} rows and {lanes} lanes"
);
tightest = tightest.min(limit as f64 / peak as f64);
};
for (name, lane) in [
("fresh chunk", Lane::new(0, &[], 507, 0)),
("resumed chunk", Lane::new(0, &[155], 300, 0)),
("resumed chunk with a folded cue", Lane::new(0, &[155], 300, 5)),
("short resumed chunk", Lane::new(0, &[155], 20, 0)),
] {
probe(name, lane.rows(), 1, false, &mut || {
solo(model, &lane, false);
});
}
let shared = [
Lane::cold(0, 96, 400, 5),
Lane::cold(1, 96, 300, 5),
Lane::cold(2, 96, 200, 0),
Lane::cold(3, 96, 150, 5),
];
let rows: usize = 96 + shared.iter().map(|lane| lane.rows() - 96).sum::<usize>();
probe("joint, shared prefix", rows, 4, shared_result, &mut || {
joint(model, &shared, 96, false);
});
let mixed = [
Lane::new(0, &[155], 200, 5),
Lane::new(1, &[125], 31, 0),
Lane::new(2, &[110], 266, 5),
];
let rows: usize = mixed.iter().map(Lane::rows).sum();
probe("joint, resumed lanes", rows, 3, shared_result, &mut || {
joint(model, &mixed, 0, false);
});
for (content, cue) in [
(2, 2), (2, 31), (31, 32), (32, 32), (33, 32), (64, 32), (513, 31),
] {
let lane = Lane::new(0, &[1], content, cue);
let mut cache = cache(model);
let _scope = PrefillScope::forced(true);
prefill_history(model, &mut cache, &lane);
probe("joint, coupled segment endpoints", lane.rows(), 1, shared_result, &mut || {
joint_forward(model, &mut cache, &[lane], 0, false)
.expect("coupled endpoint forward");
});
}
for rows in [2048usize, 2080] {
let common = rows - 48;
let lanes = [
Lane::cold(0, common, common + 18, 2),
Lane::cold(1, common, common + 10, 2),
Lane::cold(2, common, common + 6, 2),
Lane::cold(3, common, common + 6, 2),
];
let mut cache = model.with_gpu_cache_mut(|device, _registry| {
HybridKvCache::new_with_options(&model.cfg, device, 4096, SLOTS, true)
}).expect("allocate served-capacity TQ cache");
probe("joint, private shared-prefix capacity edge", rows, 4, shared_result, &mut || {
joint_forward(model, &mut cache, &lanes, common, false)
.expect("served-capacity coupled forward");
});
}
assert!(
tightest < 16.0,
"{what}: the bound is at least {tightest:.1} times every layer's requests"
);
};
with_synthetic_model(|model| check(model, "dense FFN"));
super::prefill_fold_tests::with_synthetic_moe_model(|model| check(model, "MoE FFN"));
}
fn scratch_sites(trace: &[(&'static str, u32, usize)]) -> Vec<(String, usize, usize, usize)> {
let mut sites: std::collections::BTreeMap<(String, u32), (usize, usize, usize)> = Default::default();
for &(file, line, bytes) in trace {
let name = file.rsplit('/').next().unwrap_or(file).to_owned();
let entry = sites.entry((name, line)).or_default();
entry.0 += 1;
entry.1 += bytes;
entry.2 += bytes.max(1).next_power_of_two();
}
sites
.into_iter()
.map(|((file, line), (calls, bytes, buckets))| (format!("{file}:{line}"), calls, bytes, buckets))
.collect()
}
#[test]
#[ignore = "requires HF2Q_QWEN38_GGUF or HF2Q_QWEN36_GGUF and Apple Metal; run alone"]
fn qwen_prefill_scratch_sites_probe() {
use crate::serve::header::LoadProgress;
use mlx_native::gguf::GgufFile;
const PATTERN: [u32; 7] = [9707, 374, 279, 15, 1_782, 2_610, 13];
let _gpu = crate::inference::hf2q_gpu_test_lock();
let path = match (
std::env::var_os("HF2Q_QWEN38_GGUF"),
std::env::var_os("HF2Q_QWEN36_GGUF"),
) {
(Some(path), None) | (None, Some(path)) => std::path::PathBuf::from(path),
_ => panic!("set exactly one of HF2Q_QWEN38_GGUF and HF2Q_QWEN36_GGUF"),
};
let gguf = GgufFile::open(&path).expect("open GGUF");
let mut progress = LoadProgress::new(false, 1, 0);
let mut model = Qwen35Model::load_from_gguf(&gguf, &mut progress).expect("load Qwen model");
let policy = super::routing_policy::resolve_qwen_ggml_routing_policy(&gguf, &model.cfg);
model.bind_ggml_routing_policy(policy);
let model = model
.finalize_canonical_experts()
.expect("finalize bound experts before scratch observation");
model.ensure_gpu_cache_primed().expect("prime GPU cache");
let vocab = model.cfg.vocab_size;
let layers = model.layers.len();
let hidden = model.cfg.hidden_size as usize;
let tokens = |seed: usize, start: usize, len: usize, common: usize| -> Vec<u32> {
(start..start + len)
.map(|index| {
let own = if index < common { 0 } else { seed + 1 };
(PATTERN[(index + own) % PATTERN.len()] + (own * 17) as u32) % vocab
})
.collect()
};
let _scope = PrefillScope::forced(true);
eprintln!("[SCRATCH_SITES] layers {layers} hidden {hidden}");
let report = |what: &str, rows: usize, lanes: usize, trace: Vec<(&'static str, u32, usize)>| {
let peak = super::decode_pool::take_layer_scratch_peak();
let limit = super::prefill_prepared::prefill_layer_scratch_bound_bytes(&model.cfg, rows, lanes, false);
eprintln!(
"[SCRATCH_SITES] {what}: largest layer {:.1} MiB, derived bound {:.1} MiB ({:.2}x)",
peak as f64 / 1048576.0,
limit as f64 / 1048576.0,
limit as f64 / peak.max(1) as f64
);
assert!(peak <= limit, "{what}: a layer requested {peak} bytes, over the derived bound of {limit}");
let sites = scratch_sites(&trace);
let total: usize = sites.iter().map(|site| site.3).sum();
eprintln!(
"[SCRATCH_SITES] {what}: {rows} forward rows, {} requests, {:.1} MiB of buckets in the forward, {:.2} MiB per layer",
trace.len(),
total as f64 / 1048576.0,
total as f64 / 1048576.0 / layers as f64
);
for (site, calls, bytes, buckets) in sites {
eprintln!(
"[SCRATCH_SITES] {site} calls {calls} bytes {bytes} ({:.1} per row and call) buckets {buckets}",
bytes as f64 / calls as f64 / rows as f64
);
}
};
let mut cache = model
.with_gpu_cache_mut(|device, _registry| {
HybridKvCache::new_with_options(&model.cfg, device, 4096, 4, true)
})
.expect("allocate TQ cache");
for (what, start, rows) in [("solo fresh", 0usize, 1024usize), ("solo resumed", 1024, 512)] {
super::decode_pool::trace_scratch(true);
super::decode_pool::take_layer_scratch_peak();
model
.forward_gpu_last_logits(
&tokens(0, start, rows, 0),
&positions_for_range(start as i32, rows),
&mut cache,
SlotId(0),
)
.unwrap_or_else(|error| panic!("{what}: {error:#}"));
report(what, rows, 1, super::decode_pool::trace_scratch(false));
}
for slot in 0..4 {
cache.reset_for_slot(SlotId(slot)).expect("reset");
}
const COMMON: usize = 444;
const CONTENT: usize = 720;
const CUE: usize = 5;
let chunks: Vec<(Vec<u32>, Vec<i32>)> = (0..4)
.map(|slot| (tokens(slot, 0, CONTENT + CUE, COMMON), positions_for_range(0, CONTENT + CUE)))
.collect();
let lanes: Vec<JointPrefillLane<'_>> = chunks
.iter()
.enumerate()
.map(|(slot, (tokens, positions))| JointPrefillLane {
slot_id: SlotId(slot as u32),
tokens,
positions_flat: positions,
shares_prefix: true,
fold: Some((CONTENT, CONTENT)),
image: None,
})
.collect();
super::decode_pool::trace_scratch(true);
super::decode_pool::take_layer_scratch_peak();
model
.forward_gpu_last_logits_joint(&lanes, COMMON, &mut cache, None)
.unwrap_or_else(|error| panic!("joint: {error:#}"));
report(
"joint, four lanes sharing 444 rows",
COMMON + 4 * (CONTENT + CUE - COMMON),
4,
super::decode_pool::trace_scratch(false),
);
}
#[test]
#[ignore = "requires HF2Q_QWEN38_GGUF or HF2Q_QWEN36_GGUF and Apple Metal; run alone"]
fn qwen_prefill_wait_layers_memory_probe() {
use super::prefill_prepared::ForcedWaitLayers;
use crate::serve::header::LoadProgress;
use mlx_native::gguf::GgufFile;
const PATTERN: [u32; 7] = [9707, 374, 279, 15, 1_782, 2_610, 13];
fn footprint_gib() -> (f64, f64) {
unsafe {
let mut info: libc::rusage_info_v4 = std::mem::zeroed();
let rc = libc::proc_pid_rusage(
libc::getpid(),
libc::RUSAGE_INFO_V4,
(&mut info as *mut libc::rusage_info_v4).cast(),
);
assert_eq!(rc, 0, "proc_pid_rusage");
let gib = |bytes: u64| bytes as f64 / (1u64 << 30) as f64;
(
gib(info.ri_phys_footprint),
gib(info.ri_lifetime_max_phys_footprint),
)
}
}
let _gpu = crate::inference::hf2q_gpu_test_lock();
let wait: usize = std::env::var("P834_WAIT_LAYERS")
.ok()
.and_then(|value| value.parse().ok())
.expect("set P834_WAIT_LAYERS to 1..8");
let path = match (
std::env::var_os("HF2Q_QWEN38_GGUF"),
std::env::var_os("HF2Q_QWEN36_GGUF"),
) {
(Some(path), None) | (None, Some(path)) => std::path::PathBuf::from(path),
_ => panic!("set exactly one of HF2Q_QWEN38_GGUF and HF2Q_QWEN36_GGUF"),
};
let gguf = GgufFile::open(&path).expect("open GGUF");
let mut progress = LoadProgress::new(false, 1, 0);
let mut model = Qwen35Model::load_from_gguf(&gguf, &mut progress).expect("load Qwen model");
let policy = super::routing_policy::resolve_qwen_ggml_routing_policy(&gguf, &model.cfg);
model.bind_ggml_routing_policy(policy);
let model = model
.finalize_canonical_experts()
.expect("finalize bound experts before wait-cadence caches");
model.ensure_gpu_cache_primed().expect("prime GPU cache");
let vocab = model.cfg.vocab_size;
let mut cache = model
.with_gpu_cache_mut(|device, _registry| {
HybridKvCache::new_with_options(&model.cfg, device, 8192, 1, true)
})
.expect("allocate TQ cache");
let retained = || {
super::decode_pool::worker_memory_snapshot()
.map(|(kind, snapshot)| (kind, snapshot.retained.bytes, snapshot.retained.roots))
};
let _cadence = ForcedWaitLayers::set(wait);
let _scope = PrefillScope::forced(true);
assert_eq!(super::prefill_prepared::wait_layers(), wait, "the probe's cadence is not in force");
let (loaded, _) = footprint_gib();
eprintln!("[WAIT_LAYERS_MEMORY] wait={wait} loaded: footprint {loaded:.2} GiB, pool {:?}", retained());
for (what, start, rows) in [
("fresh", 0usize, 2048usize),
("fresh", 0, 4096),
("resumed", 4096, 4096),
] {
if start == 0 {
cache.reset_for_slot(SlotId(0)).expect("reset");
}
let tokens: Vec<u32> = (start..start + rows)
.map(|index| PATTERN[index % PATTERN.len()] % vocab)
.collect();
let started = std::time::Instant::now();
model
.forward_gpu_last_logits(
&tokens,
&positions_for_range(start as i32, rows),
&mut cache,
SlotId(0),
)
.unwrap_or_else(|error| panic!("{what} {rows}-row chunk: {error:#}"));
let wall = started.elapsed().as_secs_f64() * 1e3;
let (now, peak) = footprint_gib();
let pool = retained();
eprintln!(
"[WAIT_LAYERS_MEMORY] wait={wait} after a {what} {rows}-row chunk ({wall:.0} ms): pool retained {:.3} GiB in {} buffers ({}), footprint {now:.2} GiB, peak {peak:.2} GiB",
pool.as_ref().map_or(f64::NAN, |(_, bytes, _)| *bytes as f64 / (1u64 << 30) as f64),
pool.as_ref().map_or(0, |(_, _, roots)| *roots),
pool.as_ref().map_or("unavailable", |(kind, _, _)| *kind),
);
}
}