use mlx_native::DType;
use super::forward_gpu::Qwen35JointVerifyLane;
use super::joint_decode_routes::{JointDenseFfnRoute, JointRouteMode};
use super::joint_projection::JointProjectionKernel;
use super::joint_step_options::JointStepOptions;
use super::joint_verify_tests::{
dflash_arm, exact_policy, lane_digest, padded_policy, row_bits, synthetic_cfg,
with_synthetic_fixture_cfg, with_synthetic_fixture_embedding, Arm, Fixture, LaneDigest,
RowBits, SLOTS,
};
use super::prepared_switch_override::{Override, PreparedSwitches};
use super::{Qwen35Config, Qwen35JointOutput, Qwen35JointRow, Qwen35MoeConfig};
use crate::inference::spec_decode::dflash2::adapters::DFlash2TargetCapture;
use crate::serve::multi_seq_kv::SlotId;
pub(super) const TARGET: u32 = 2;
const PADDING_TOKEN: u32 = 509;
pub(super) fn moe_layout_cfg(query_heads: u32) -> Qwen35Config {
Qwen35Config {
num_attention_heads: query_heads,
moe: Some(Qwen35MoeConfig {
moe_intermediate_size: 512,
num_experts: 8,
num_experts_per_tok: 2,
shared_expert_intermediate_size: 512,
}),
..synthetic_cfg()
}
}
pub(super) fn exact_arm(attention_tile: bool) -> Arm {
Arm {
mode: JointRouteMode::Exact,
dense_ffn: JointDenseFfnRoute::SerialBuilder,
options: JointStepOptions {
projection: JointProjectionKernel::Canonical,
pooled_outputs: true,
verify_attention_tile: attention_tile,
verify_mixer_fused: false,
dflash2: Default::default(),
},
}
}
#[derive(Clone, Copy)]
pub(super) enum Tokens {
Host,
GpuPoisonedPadding,
}
#[derive(Clone, PartialEq)]
pub(super) struct LaneTrace {
pub(super) rows: Vec<RowBits>,
pub(super) committed: LaneDigest,
pub(super) captured: Vec<u32>,
}
pub(super) fn dflash2_verify(
fixture: &Fixture<'_>,
cohort: &[(u32, usize)],
tokens: Tokens,
) -> LaneTrace {
let model = fixture.model;
let mut cache = (fixture.new_cache)();
let lane_tokens = cohort
.iter()
.map(|&(slot, rows)| {
(0..rows as i32)
.map(|row| (fixture.token)(slot, fixture.start[slot as usize] + row))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let lanes = cohort
.iter()
.zip(&lane_tokens)
.map(|(&(slot, _), tokens)| Qwen35JointVerifyLane {
slot_id: SlotId(slot),
position: fixture.start[slot as usize],
tokens,
output: Qwen35JointOutput::LogitsAndHidden,
})
.collect::<Vec<_>>();
let layers = [0, model.cfg.num_hidden_layers as usize - 1];
let feature_width = model.cfg.hidden_size as usize * layers.len();
let (captured, verify_rows, token_tile) = model
.with_gpu_cache_mut(|device, _registry| {
let captured = device.alloc_buffer(
SLOTS as usize * 8 * feature_width * 4,
DType::F32,
vec![SLOTS as usize * 8, feature_width],
)?;
let mut verify_rows =
device.alloc_buffer(SLOTS as usize * 4, DType::U32, vec![SLOTS as usize])?;
verify_rows.as_mut_slice::<u32>()?.fill(8);
let mut token_tile = device.alloc_buffer(32 * 4, DType::U32, vec![32])?;
let tile = token_tile.as_mut_slice::<u32>()?;
tile.fill(PADDING_TOKEN);
for (slot, token) in tile.iter_mut().zip(lane_tokens.iter().flatten()) {
*slot = *token;
}
Ok((captured, verify_rows, token_tile))
})
.expect("allocate DFlash2 capture");
let capture_lanes = cohort
.iter()
.map(|&(slot, _)| slot as usize)
.collect::<Vec<_>>();
let capture = DFlash2TargetCapture {
layer_ids: &layers,
output: &captured,
lane_indices: &capture_lanes,
verify_rows: &verify_rows,
lanes: SLOTS as usize,
rows_per_lane: 8,
feature_width,
};
let arm = fixture.arm;
let verify = match tokens {
Tokens::Host => model.forward_gpu_dflash2_verify(
&lanes,
&mut cache,
arm.mode,
arm.dense_ffn,
arm.options,
Some(capture),
None,
),
Tokens::GpuPoisonedPadding => model.forward_gpu_dflash2_verify_from_gpu_tokens(
&lanes,
&token_tile,
&mut cache,
arm.mode,
arm.dense_ffn,
arm.options,
Some(capture),
None,
None,
),
}
.unwrap_or_else(|error| panic!("DFlash2 verify of {cohort:?}: {error:#}"));
let index = cohort
.iter()
.position(|&(slot, _)| slot == TARGET)
.expect("the cohort holds the target lane");
let rows = verify.rows[index]
.iter()
.map(|row| row_bits(model, row))
.collect::<Vec<_>>();
let start = TARGET as usize * 8 * feature_width;
let captured = captured.as_slice::<f32>().expect("read capture")
[start..start + cohort[index].1 * feature_width]
.iter()
.map(|value| value.to_bits())
.collect();
let accepted = cohort.iter().map(|&(_, rows)| rows).collect::<Vec<_>>();
model
.commit_joint_verify(&mut cache, verify.pending, &accepted)
.expect("commit DFlash2 verify");
LaneTrace {
rows,
committed: lane_digest(&cache, &model.cfg, TARGET),
captured,
}
}
fn mtp_verify(fixture: &Fixture<'_>, cohort: &[(u32, usize)]) -> (Vec<RowBits>, LaneDigest) {
let model = fixture.model;
let mut cache = (fixture.new_cache)();
let lane_tokens = cohort
.iter()
.map(|&(slot, rows)| {
(0..rows as i32)
.map(|row| (fixture.token)(slot, fixture.start[slot as usize] + row))
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let lanes = cohort
.iter()
.zip(&lane_tokens)
.map(|(&(slot, _), tokens)| Qwen35JointVerifyLane {
slot_id: SlotId(slot),
position: fixture.start[slot as usize],
tokens,
output: Qwen35JointOutput::LogitsAndHidden,
})
.collect::<Vec<_>>();
let arm = fixture.arm;
let verify = model
.forward_gpu_joint_verify(&lanes, &mut cache, arm.mode, arm.dense_ffn, arm.options)
.unwrap_or_else(|error| panic!("joint MTP verify of {cohort:?}: {error:#}"));
let index = cohort
.iter()
.position(|&(slot, _)| slot == TARGET)
.expect("the cohort holds the target lane");
let rows = verify.rows[index]
.iter()
.map(|row| row_bits(model, row))
.collect();
let accepted = cohort.iter().map(|&(_, rows)| rows).collect::<Vec<_>>();
model
.commit_joint_verify(&mut cache, verify.pending, &accepted)
.expect("commit joint MTP verify");
(rows, lane_digest(&cache, &model.cfg, TARGET))
}
fn plain_joint(fixture: &Fixture<'_>, slots: &[u32], steps: usize) -> (Vec<RowBits>, LaneDigest) {
let model = fixture.model;
let mut cache = (fixture.new_cache)();
let arm = fixture.arm;
let index = slots
.iter()
.position(|&slot| slot == TARGET)
.expect("the step holds the target slot");
let rows = (0..steps as i32)
.map(|step| {
let step_rows = slots
.iter()
.map(|&slot| {
let position = fixture.start[slot as usize] + step;
Qwen35JointRow {
slot_id: SlotId(slot),
token: (fixture.token)(slot, position),
position,
output: Qwen35JointOutput::LogitsAndHidden,
}
})
.collect::<Vec<_>>();
let outputs = model
.forward_gpu_decode_joint_with_options(
&step_rows,
&mut cache,
arm.mode,
arm.dense_ffn,
arm.options,
)
.unwrap_or_else(|error| panic!("plain joint step of {slots:?}: {error:#}"));
row_bits(model, &outputs[index])
})
.collect();
(rows, lane_digest(&cache, &model.cfg, TARGET))
}
fn serial(fixture: &Fixture<'_>, steps: usize) -> (Vec<RowBits>, LaneDigest) {
let model = fixture.model;
let hidden = model.cfg.hidden_size as usize;
let mut cache = (fixture.new_cache)();
let rows = (0..steps as i32)
.map(|step| {
let position = fixture.start[TARGET as usize] + step;
let (logits, nextn) = model
.forward_gpu_with_nextn_hidden(
&[(fixture.token)(TARGET, position)],
&[position; 4],
&mut cache,
SlotId(TARGET),
)
.unwrap_or_else(|error| panic!("serial step {step}: {error:#}"));
(
logits.iter().map(|value| value.to_bits()).collect(),
nextn.as_slice::<f32>().expect("read hidden")[..hidden]
.iter()
.map(|value| value.to_bits())
.collect(),
)
})
.collect();
(rows, lane_digest(&cache, &model.cfg, TARGET))
}
pub(super) fn whole_steps_repeat() -> bool {
let repeat = std::env::var_os("MTL_SHADER_VALIDATION").is_none();
if !repeat {
eprintln!("skipped: whole joint steps do not repeat under Metal shader validation");
}
repeat
}
fn logits_of(trace: &LaneTrace) -> Vec<&Vec<u32>> {
trace.rows.iter().map(|row| &row.0).collect()
}
fn hidden_of(trace: &LaneTrace) -> Vec<&Vec<u32>> {
trace.rows.iter().map(|row| &row.1).collect()
}
pub(super) fn assert_same_lane(actual: &LaneTrace, expected: &LaneTrace, label: &str) {
assert!(
actual.rows == expected.rows,
"{label}: the lane's rows differ"
);
assert!(
actual.committed == expected.committed,
"{label}: the lane's committed cache differs"
);
assert!(
actual.captured == expected.captured,
"{label}: the lane's target captures differ"
);
}
fn assert_each_switch_selects_its_route(fixture: &Fixture<'_>, cohort: &[(u32, usize)]) {
let run = |switches| {
let _switches = Override::set(switches);
dflash2_verify(fixture, cohort, Tokens::Host)
};
let any_width = PreparedSwitches {
any_width: true,
..PreparedSwitches::OFF
};
let stock = run(PreparedSwitches::OFF);
assert!(
run(any_width) == stock,
"the any-width switch alone changed a step"
);
let ffn = run(PreparedSwitches {
ffn: true,
..any_width
});
assert!(
ffn.rows != stock.rows && ffn.captured != stock.captured,
"the dense FFN kept the stock route"
);
let projections = run(PreparedSwitches {
ffn: true,
projections: true,
..any_width
});
assert!(
projections.rows != ffn.rows && projections.captured != ffn.captured,
"the projections kept the stock route"
);
let head = run(PreparedSwitches {
ffn: true,
projections: true,
head: true,
..any_width
});
assert!(
logits_of(&head) != logits_of(&projections),
"the head kept the stock route"
);
assert!(
hidden_of(&head) == hidden_of(&projections) && head.captured == projections.captured,
"the head switch changed rows upstream of the head"
);
assert!(
run(PreparedSwitches::ALL) == head,
"the shared head changed a step"
);
}
#[test]
fn dense_layout_dflash2_step_takes_the_prepared_routes_and_ignores_cohort_and_padding() {
if !whole_steps_repeat() {
return;
}
let (policy, arm, cfg) = (padded_policy(), dflash_arm(), synthetic_cfg());
with_synthetic_fixture_embedding(policy, arm, cfg, true, |fixture| {
let alone: &[(u32, usize)] = &[(TARGET, 8)];
{
let _switches = Override::set(PreparedSwitches {
head: false,
..PreparedSwitches::ALL
});
dflash2_verify(fixture, alone, Tokens::Host);
assert!(
!fixture.model.prepared_output_head.is_prepared(),
"the shared head was prepared for no consumer"
);
}
assert_each_switch_selects_its_route(fixture, alone);
assert!(fixture.model.prepared_output_head.is_prepared());
let _switches = Override::set(PreparedSwitches::ALL);
let reference = dflash2_verify(fixture, alone, Tokens::Host);
assert_same_lane(
&dflash2_verify(fixture, alone, Tokens::Host),
&reference,
"repeat",
);
let cohorts: [(&str, &[(u32, usize)]); 6] = [
("16 live rows, target last", &[(0, 8), (TARGET, 8)]),
("16 live rows, target first", &[(TARGET, 8), (3, 8)]),
("24 live rows", &[(0, 8), (1, 8), (TARGET, 8)]),
("32 live rows", &[(0, 8), (1, 8), (TARGET, 8), (3, 8)]),
(
"32 live rows, permuted",
&[(3, 8), (TARGET, 8), (1, 8), (0, 8)],
),
("11 live rows", &[(0, 3), (TARGET, 8)]),
];
for (label, cohort) in cohorts {
for tokens in [Tokens::Host, Tokens::GpuPoisonedPadding] {
assert_same_lane(&dflash2_verify(fixture, cohort, tokens), &reference, label);
}
}
assert_same_lane(
&dflash2_verify(fixture, alone, Tokens::GpuPoisonedPadding),
&reference,
"8 live rows beside 24 poisoned padding rows",
);
let _width_32 = Override::set(PreparedSwitches::WIDTH_32);
assert_same_lane(
&dflash2_verify(fixture, alone, Tokens::Host),
&reference,
"any-width off",
);
});
}
fn moe_layout_rows_have_one_value(attention_tile: bool) {
if !whole_steps_repeat() {
return;
}
let cfg = moe_layout_cfg(if attention_tile { 8 } else { 2 });
let (policy, arm) = (exact_policy(), exact_arm(attention_tile));
with_synthetic_fixture_embedding(policy, arm, cfg, true, |fixture| {
let alone: &[(u32, usize)] = &[(TARGET, 8)];
let full: &[(u32, usize)] = &[(0, 8), (1, 8), (TARGET, 8), (3, 8)];
assert_each_switch_selects_its_route(fixture, alone);
let (stock_8, width_32) = {
let _off = Override::set(PreparedSwitches::OFF);
let stock_8 = dflash2_verify(fixture, alone, Tokens::Host);
let _width_32 = Override::set(PreparedSwitches::WIDTH_32);
assert_same_lane(
&dflash2_verify(fixture, alone, Tokens::Host),
&stock_8,
"8-row tile without any-width",
);
let width_32 = dflash2_verify(fixture, full, Tokens::Host);
assert!(
width_32.rows != stock_8.rows,
"the 32-row tile kept the stock route"
);
(stock_8, width_32)
};
let _switches = Override::set(PreparedSwitches::ALL);
let reference = dflash2_verify(fixture, alone, Tokens::Host);
assert!(reference.rows != stock_8.rows);
assert_same_lane(&reference, &width_32, "8-row tile against the 32-row tile");
assert_same_lane(
&dflash2_verify(fixture, alone, Tokens::Host),
&reference,
"repeat",
);
let cohorts: [(&str, &[(u32, usize)]); 8] = [
("width 16, target last", &[(0, 8), (TARGET, 8)]),
("width 16, target first", &[(TARGET, 8), (3, 8)]),
("width 16, 11 live rows", &[(0, 3), (TARGET, 8)]),
("width 16, 9 live rows", &[(TARGET, 8), (1, 1)]),
("width 32, 24 live rows", &[(0, 8), (1, 8), (TARGET, 8)]),
("width 32, 17 live rows", &[(TARGET, 8), (0, 8), (3, 1)]),
("width 32, full", full),
(
"width 32, full, permuted",
&[(3, 8), (TARGET, 8), (1, 8), (0, 8)],
),
];
for (label, cohort) in cohorts {
for tokens in [Tokens::Host, Tokens::GpuPoisonedPadding] {
assert_same_lane(&dflash2_verify(fixture, cohort, tokens), &reference, label);
}
}
let windows: [(&str, &[(u32, usize)]); 3] = [
("width 4", &[(TARGET, 4)]),
("width 8, 5 live rows", &[(TARGET, 4), (1, 1)]),
("width 16", &[(0, 8), (TARGET, 4)]),
];
for (label, cohort) in windows {
for tokens in [Tokens::Host, Tokens::GpuPoisonedPadding] {
let window = dflash2_verify(fixture, cohort, tokens);
assert!(
window.rows[..] == reference.rows[..4],
"{label}: a 4-row window differs from the 8-row window's first rows"
);
}
}
});
}
#[test]
fn moe_layout_dflash2_rows_have_one_value_at_every_tile_width_cohort_and_padding() {
moe_layout_rows_have_one_value(false);
}
#[test]
fn moe_layout_dflash2_rows_have_one_value_with_the_tiled_verify_attention() {
moe_layout_rows_have_one_value(true);
}
#[test]
fn plain_joint_and_mtp_steps_keep_their_routes_under_every_switch() {
if !whole_steps_repeat() {
return;
}
let stock_and_switched = |fixture: &Fixture<'_>, exact: bool| {
let run = |switches| {
let _switches = Override::set(switches);
let mut cases = vec![
("MTP verify, 4 rows", mtp_verify(fixture, &[(TARGET, 4)])),
(
"MTP verify, 8 rows",
mtp_verify(fixture, &[(0, 4), (TARGET, 4)]),
),
(
"MTP verify, 16 rows",
mtp_verify(fixture, &[(0, 4), (1, 4), (TARGET, 4), (3, 4)]),
),
("plain joint, 1 slot", plain_joint(fixture, &[TARGET], 4)),
(
"plain joint, 4 slots",
plain_joint(fixture, &[0, 1, TARGET, 3], 4),
),
("serial", serial(fixture, 4)),
];
if exact {
cases.push((
"MTP verify, 12 rows",
mtp_verify(fixture, &[(1, 3), (TARGET, 4), (3, 5)]),
));
}
cases
};
let stock = run(PreparedSwitches::OFF);
let switched = run(PreparedSwitches::ALL);
for ((label, stock), (_, switched)) in stock.iter().zip(&switched) {
assert!(
stock == switched,
"{label}: the prepared-route switches changed the step"
);
}
switched
};
with_synthetic_fixture_cfg(
exact_policy(),
exact_arm(false),
moe_layout_cfg(2),
|fixture| {
let switched = stock_and_switched(fixture, true);
let of = |label: &str| {
&switched
.iter()
.find(|(name, _)| *name == label)
.expect("case")
.1
};
let serial = of("serial");
for label in ["plain joint, 1 slot", "plain joint, 4 slots"] {
assert!(
of(label) == serial,
"{label} differs from the serial forward"
);
}
},
);
with_synthetic_fixture_cfg(padded_policy(), dflash_arm(), synthetic_cfg(), |fixture| {
stock_and_switched(fixture, false);
});
}