use std::sync::{
atomic::{AtomicU64, Ordering},
Once,
};
use crate::inference::models::qwen35::joint_decode_routes::{
joint_decode_env_unavailable_reason, joint_ffn_shared_from_process, joint_mode_max_rows,
select_dense_ffn_route, JointDenseFfnRoute, JointRouteMode, MAX_JOINT_DECODE_ROWS,
};
use crate::inference::models::qwen35::joint_projection::{
select_joint_projection_kernel, JointProjectionKernel, QWEN_JOINT_MMA_ENV,
};
use crate::inference::models::qwen35::joint_step_options::{
parse_switch, switch_from_process, JointStepOptions, QWEN_JOINT_MTP_ENV,
QWEN_JOINT_POOLED_OUTPUTS_ENV,
};
use crate::inference::models::qwen35::model::Qwen35Model;
use crate::serve::api::engine_qwen35::{
anchor_store::Qwen35AnchorStore, critical_path, Qwen35JointLane, Qwen35LaneRefusal,
Qwen35TickOutcome,
};
use crate::serve::api::qwen35_speculation::QwenSpeculationRoute;
use crate::serve::api::registry::ModelRegistration;
use super::{
is_fatal_command_buffer_error, settle_qwen35_decode_lane, EngineSupervisor,
InflightBatchedScheduler, PromptCache, Qwen35FatalFailure, Qwen35FatalReply, Qwen35KvGuard,
Qwen35RetainedPrefix, Qwen35Slot, Qwen35SlotWork, Qwen35WorkerFatalKind, SlotHandle,
};
#[path = "qwen35_joint_mtp.rs"]
mod joint_mtp;
const QWEN_JOINT_DECODE_ENV: &str = "HF2Q_QWEN_JOINT_DECODE";
const QWEN_JOINT_DECODE_MAX_ROWS_ENV: &str = "HF2Q_QWEN_JOINT_DECODE_MAX_ROWS";
const DEFAULT_QWEN_JOINT_DECODE_ROWS: usize = 4;
const QWEN_JOINT_SOLO_ENV: &str = "HF2Q_QWEN_JOINT_SOLO";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(in crate::serve::api) struct Qwen35JointDecodeConfig {
pub max_rows: usize,
pub mode: JointRouteMode,
pub dense_ffn: JointDenseFfnRoute,
pub solo: bool,
pub step: JointStepOptions,
pub joint_mtp: bool,
}
fn parse_qwen_joint_decode(raw: Option<&str>) -> Result<Option<bool>, String> {
let Some(raw) = raw.map(str::trim).filter(|raw| !raw.is_empty()) else {
return Ok(None);
};
match raw.to_ascii_lowercase().as_str() {
"1" | "on" | "true" => Ok(Some(true)),
"0" | "off" | "false" => Ok(Some(false)),
_ => Err(format!(
"{QWEN_JOINT_DECODE_ENV} must be 1/on/true or 0/off/false, got {raw:?}"
)),
}
}
fn parse_qwen_joint_decode_max_rows(raw: Option<&str>) -> Result<usize, String> {
let Some(raw) = raw.map(str::trim).filter(|raw| !raw.is_empty()) else {
return Ok(DEFAULT_QWEN_JOINT_DECODE_ROWS);
};
raw.parse::<usize>()
.ok()
.filter(|rows| (2..=MAX_JOINT_DECODE_ROWS).contains(rows))
.ok_or_else(|| {
format!(
"{QWEN_JOINT_DECODE_MAX_ROWS_ENV} must be an integer in 2..={MAX_JOINT_DECODE_ROWS}, got {raw:?}"
)
})
}
fn parse_qwen_joint_solo(raw: Option<&str>) -> Result<bool, String> {
let Some(raw) = raw.map(str::trim).filter(|raw| !raw.is_empty()) else {
return Ok(true);
};
match raw.to_ascii_lowercase().as_str() {
"1" | "on" | "true" => Ok(true),
"0" | "off" | "false" => Ok(false),
_ => Err(format!(
"{QWEN_JOINT_SOLO_ENV} must be 1/on/true or 0/off/false, got {raw:?}"
)),
}
}
fn parse_qwen_joint_mtp(raw: Option<&str>, default: bool) -> Result<bool, String> {
if raw.map(str::trim).filter(|raw| !raw.is_empty()).is_none() {
return Ok(default);
}
parse_switch(QWEN_JOINT_MTP_ENV, raw)
}
fn parse_qwen_joint_mma(raw: Option<&str>, joint_mtp: bool) -> Result<bool, String> {
if raw.map(str::trim).filter(|raw| !raw.is_empty()).is_none() {
return Ok(joint_mtp);
}
parse_switch(QWEN_JOINT_MMA_ENV, raw)
}
#[allow(clippy::too_many_arguments)]
fn select_qwen_joint_decode(
requested: bool,
max_rows: usize,
mode: Result<JointRouteMode, String>,
ffn_shared: bool,
solo_requested: bool,
n_slots: usize,
requested_step: JointStepOptions,
joint_mtp: bool,
) -> Option<Qwen35JointDecodeConfig> {
match (requested, mode) {
(true, Ok(mode)) => {
let solo = solo_requested && mode == JointRouteMode::PaddedMvExt && n_slots >= 2;
Some(Qwen35JointDecodeConfig {
max_rows: joint_mode_max_rows(mode, max_rows),
mode,
dense_ffn: select_dense_ffn_route(mode, ffn_shared),
solo,
step: JointStepOptions {
projection: select_joint_projection_kernel(
mode,
requested_step.projection == JointProjectionKernel::ColumnInvariantMma,
),
pooled_outputs: requested_step.pooled_outputs,
verify_attention_tile: false,
verify_mixer_fused: false,
dflash2: crate::inference::models::qwen35::joint_step_options::DFlash2VerifyOptions::OFF,
},
joint_mtp: joint_mtp && (mode == JointRouteMode::Exact || solo),
})
}
_ => None,
}
}
pub(in crate::serve::api) fn qwen35_joint_decode_config(
model: &Qwen35Model,
n_slots: usize,
) -> Option<Qwen35JointDecodeConfig> {
let moe = model.cfg.moe.is_some();
let default = true;
let raw = std::env::var(QWEN_JOINT_DECODE_ENV).ok();
let explicit = parse_qwen_joint_decode(raw.as_deref()).unwrap_or_else(|error| {
tracing::warn!(
error,
fallback = default,
"invalid Qwen joint decode switch"
);
None
});
let requested = explicit.unwrap_or(default);
let raw_rows = std::env::var(QWEN_JOINT_DECODE_MAX_ROWS_ENV).ok();
let max_rows = parse_qwen_joint_decode_max_rows(raw_rows.as_deref()).unwrap_or_else(|error| {
tracing::warn!(
error,
fallback = DEFAULT_QWEN_JOINT_DECODE_ROWS,
"invalid Qwen joint decode width"
);
DEFAULT_QWEN_JOINT_DECODE_ROWS
});
let mode = if requested {
model.joint_decode_mode()
} else {
Err("disabled by HF2Q_QWEN_JOINT_DECODE".into())
};
let unavailable = mode.as_ref().err().cloned();
let ffn_shared = joint_ffn_shared_from_process().unwrap_or_else(|error| {
tracing::warn!(
error,
fallback = true,
"invalid Qwen joint dense FFN switch"
);
true
});
let raw_solo = std::env::var(QWEN_JOINT_SOLO_ENV).ok();
let solo_requested = parse_qwen_joint_solo(raw_solo.as_deref()).unwrap_or_else(|error| {
tracing::warn!(error, fallback = true, "invalid Qwen joint solo switch");
true
});
let switch = |name: &str, label: &str| {
switch_from_process(name).unwrap_or_else(|error| {
tracing::warn!(error, fallback = false, "invalid Qwen joint {label} switch");
false
})
};
let joint_mtp_default = !moe && model.mtp.is_some();
let raw_mtp = std::env::var(QWEN_JOINT_MTP_ENV).ok();
let joint_mtp =
parse_qwen_joint_mtp(raw_mtp.as_deref(), joint_mtp_default).unwrap_or_else(|error| {
tracing::warn!(
error,
fallback = joint_mtp_default,
"invalid Qwen joint MTP verify switch"
);
joint_mtp_default
});
let raw_mma = std::env::var(QWEN_JOINT_MMA_ENV).ok();
let mma = parse_qwen_joint_mma(raw_mma.as_deref(), joint_mtp).unwrap_or_else(|error| {
tracing::warn!(error, fallback = joint_mtp, "invalid Qwen joint MMA switch");
joint_mtp
});
let requested_step = JointStepOptions {
projection: if mma {
JointProjectionKernel::ColumnInvariantMma
} else {
JointProjectionKernel::Canonical
},
pooled_outputs: switch(QWEN_JOINT_POOLED_OUTPUTS_ENV, "pooled outputs"),
verify_attention_tile: false,
verify_mixer_fused: false,
dflash2: crate::inference::models::qwen35::joint_step_options::DFlash2VerifyOptions::OFF,
};
let selected = select_qwen_joint_decode(
requested,
max_rows,
mode,
ffn_shared,
solo_requested,
n_slots,
requested_step,
joint_mtp,
);
tracing::info!(
target: "hf2q::serve::api::engine_qwen35::progress",
requested,
explicit = explicit.is_some(),
moe,
dense_decode_mvn = model.ggml_routing_policy().dense_decode_mvn,
dense_decode_mv_ext = model.ggml_routing_policy().dense_decode_mv_ext,
moe_ffn = model.moe_ffn_decode_route(),
moe_experts = model.moe_expert_decode_route(),
max_rows = selected.map_or(max_rows, |config| config.max_rows),
selected = selected.is_some(),
mode = selected.map_or("serial", |config| config.mode.label()),
dense_ffn = selected.map_or("serial", |config| config.dense_ffn.label()),
solo = selected.is_some_and(|config| config.solo),
column_invariant_mma = selected.is_some_and(|config| {
config.step.projection == JointProjectionKernel::ColumnInvariantMma
}),
pooled_outputs = selected.is_some_and(|config| config.step.pooled_outputs),
joint_mtp = selected.is_some_and(|config| config.joint_mtp),
verify_replay_reuse = crate::inference::models::qwen35::replay_arena::enabled(),
dflash2_context_first = crate::serve::api::engine_qwen35::dflash2_context_first(),
dflash2_draft_prefetch = crate::serve::api::engine_qwen35::dflash2_draft_prefetch(),
dflash2_decisions = crate::serve::api::engine_qwen35::dflash2_decision_route().label(),
unavailable = unavailable.as_deref().unwrap_or("none"),
"Qwen joint decode configuration"
);
selected
}
pub(in crate::serve::api) fn qwen35_joint_decode_turn_config(
worker: Option<Qwen35JointDecodeConfig>,
) -> Option<Qwen35JointDecodeConfig> {
let config = worker?;
let Some(reason) = joint_decode_env_unavailable_reason() else {
return Some(config);
};
static REVOKED: Once = Once::new();
REVOKED.call_once(|| {
tracing::warn!(
target: "hf2q::serve::api::engine_qwen35::progress",
reason = %reason,
"Qwen joint decode revoked after worker start; decoding serially"
);
});
None
}
#[derive(Debug, PartialEq, Eq)]
struct JointPartition {
groups: Vec<Vec<SlotHandle>>,
serial: Vec<SlotHandle>,
}
fn partition_qwen35_joint_lanes(
handles: &[SlotHandle],
max_rows: usize,
solo: bool,
mut classify: impl FnMut(SlotHandle) -> Option<bool>,
) -> Option<JointPartition> {
let unique = handles.iter().enumerate().all(|(index, handle)| {
handles[..index]
.iter()
.all(|earlier| earlier.slot_id != handle.slot_id)
});
if !unique || max_rows < 2 {
return None;
}
let mut joined = vec![false; handles.len()];
let mut groups: Vec<Vec<SlotHandle>> = Vec::new();
if solo {
let eligible = handles
.iter()
.enumerate()
.filter_map(|(index, &handle)| classify(handle).map(|ordinary| (index, ordinary)))
.collect::<Vec<_>>();
for chunk in eligible.chunks(max_rows) {
if let [(_, false)] = chunk {
continue;
}
for &(index, _) in chunk {
joined[index] = true;
}
groups.push(chunk.iter().map(|&(index, _)| handles[index]).collect());
}
} else {
let mut group = Vec::with_capacity(handles.len().min(max_rows));
for (index, &handle) in handles.iter().enumerate() {
if group.len() < max_rows && classify(handle).is_some() {
group.push(index);
}
}
if group.len() >= 2 {
for &index in &group {
joined[index] = true;
}
groups.push(group.iter().map(|&index| handles[index]).collect());
}
}
if groups.is_empty() {
return None;
}
for group in &mut groups {
group.sort_by_key(|handle| handle.slot_id);
}
let serial = handles
.iter()
.zip(&joined)
.filter(|(_, &joined)| !joined)
.map(|(&handle, _)| handle)
.collect();
Some(JointPartition { groups, serial })
}
fn distribute_step_error<T>(
error: anyhow::Error,
rows: usize,
) -> impl Iterator<Item = anyhow::Result<T>> {
let message = format!("{error:#}");
let mut first = Some(error);
(0..rows).map(move |_| Err(first.take().unwrap_or_else(|| anyhow::anyhow!("{message}"))))
}
pub(in crate::serve::api) struct Qwen35JointTelemetry {
pub capture_active: AtomicU64,
pub duplicate_slots: AtomicU64,
pub max_rows: AtomicU64,
pub too_few_ready: AtomicU64,
pub anchor_capture: AtomicU64,
pub anchor_restore: AtomicU64,
pub lane_anchor_capture: AtomicU64,
pub lane_anchor_restore: AtomicU64,
pub c2_joint_lane_steps: AtomicU64,
pub c2_serial_lane_steps: AtomicU64,
pub lane_step_bound: AtomicU64,
pub lane_pending_speculation_output: AtomicU64,
pub lane_no_seed: AtomicU64,
pub lane_target_cursor: AtomicU64,
pub lane_mtp_cursor: AtomicU64,
pub lane_speculation_route: AtomicU64,
pub lane_client_closed: AtomicU64,
pub lane_handle_mismatch: AtomicU64,
}
pub(in crate::serve::api) static JOINT_TELEMETRY: Qwen35JointTelemetry = Qwen35JointTelemetry {
capture_active: AtomicU64::new(0),
duplicate_slots: AtomicU64::new(0),
max_rows: AtomicU64::new(0),
too_few_ready: AtomicU64::new(0),
anchor_capture: AtomicU64::new(0),
anchor_restore: AtomicU64::new(0),
lane_anchor_capture: AtomicU64::new(0),
lane_anchor_restore: AtomicU64::new(0),
c2_joint_lane_steps: AtomicU64::new(0),
c2_serial_lane_steps: AtomicU64::new(0),
lane_step_bound: AtomicU64::new(0),
lane_pending_speculation_output: AtomicU64::new(0),
lane_no_seed: AtomicU64::new(0),
lane_target_cursor: AtomicU64::new(0),
lane_mtp_cursor: AtomicU64::new(0),
lane_speculation_route: AtomicU64::new(0),
lane_client_closed: AtomicU64::new(0),
lane_handle_mismatch: AtomicU64::new(0),
};
pub(in crate::serve::api) fn record_joint_ineligible(reason: &Qwen35JointIneligible) {
let counter = match reason {
Qwen35JointIneligible::CaptureActive => &JOINT_TELEMETRY.capture_active,
Qwen35JointIneligible::DuplicateSlots => &JOINT_TELEMETRY.duplicate_slots,
Qwen35JointIneligible::MaxRowsBelowTwo => &JOINT_TELEMETRY.max_rows,
Qwen35JointIneligible::AnchorTransaction { kind, lanes } => {
use crate::serve::api::engine_qwen35::anchor_store::AnchorTransaction;
let (turn, lane) = match kind {
AnchorTransaction::Capture => (&JOINT_TELEMETRY.anchor_capture, &JOINT_TELEMETRY.lane_anchor_capture),
AnchorTransaction::Restore => (&JOINT_TELEMETRY.anchor_restore, &JOINT_TELEMETRY.lane_anchor_restore),
};
lane.fetch_add(*lanes as u64, Ordering::Relaxed);
turn
}
Qwen35JointIneligible::TooFewReady { refusals, .. } => {
for refusal in refusals {
let lane = match refusal {
Qwen35LaneRefusal::StepBound => &JOINT_TELEMETRY.lane_step_bound,
Qwen35LaneRefusal::PendingSpeculationOutput => {
&JOINT_TELEMETRY.lane_pending_speculation_output
}
Qwen35LaneRefusal::NoSeed => &JOINT_TELEMETRY.lane_no_seed,
Qwen35LaneRefusal::TargetCursor => &JOINT_TELEMETRY.lane_target_cursor,
Qwen35LaneRefusal::MtpCursor => &JOINT_TELEMETRY.lane_mtp_cursor,
Qwen35LaneRefusal::SpeculationRoute => &JOINT_TELEMETRY.lane_speculation_route,
Qwen35LaneRefusal::ClientClosed => &JOINT_TELEMETRY.lane_client_closed,
Qwen35LaneRefusal::HandleMismatch => &JOINT_TELEMETRY.lane_handle_mismatch,
Qwen35LaneRefusal::AnchorCapture => &JOINT_TELEMETRY.lane_anchor_capture,
Qwen35LaneRefusal::AnchorRestore => &JOINT_TELEMETRY.lane_anchor_restore,
};
lane.fetch_add(1, Ordering::Relaxed);
}
if refusals.contains(&Qwen35LaneRefusal::AnchorCapture) {
&JOINT_TELEMETRY.anchor_capture
} else if refusals.contains(&Qwen35LaneRefusal::AnchorRestore) {
&JOINT_TELEMETRY.anchor_restore
} else {
&JOINT_TELEMETRY.too_few_ready
}
}
};
counter.fetch_add(1, Ordering::Relaxed);
}
pub(in crate::serve::api) fn record_c2_decode_lane_steps(
decode_ready_lanes: usize, joint_lane_steps: usize, serial_lane_steps: usize,
route: &'static str, path: &'static str,
) {
if decode_ready_lanes < 2 || joint_lane_steps + serial_lane_steps == 0 {
return;
}
if joint_lane_steps != 0 {
JOINT_TELEMETRY.c2_joint_lane_steps.fetch_add(joint_lane_steps as u64, Ordering::Relaxed);
}
if serial_lane_steps != 0 {
JOINT_TELEMETRY.c2_serial_lane_steps.fetch_add(serial_lane_steps as u64, Ordering::Relaxed);
}
tracing::debug!(
target: "hf2q::serve::api::engine_qwen35::progress",
decode_ready_lanes, joint_lane_steps, serial_lane_steps, route, path,
"Qwen C2 decode lane steps"
);
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::serve::api) enum Qwen35JointIneligible {
CaptureActive,
DuplicateSlots,
MaxRowsBelowTwo,
AnchorTransaction {
kind: crate::serve::api::engine_qwen35::anchor_store::AnchorTransaction,
lanes: usize,
},
TooFewReady {
ready: usize,
refusals: Vec<Qwen35LaneRefusal>,
},
}
impl Qwen35JointIneligible {
pub fn label(&self) -> &'static str {
match self {
Self::CaptureActive => "capture_active",
Self::DuplicateSlots => "duplicate_slots",
Self::MaxRowsBelowTwo => "max_rows",
Self::AnchorTransaction { kind, .. } => match kind {
crate::serve::api::engine_qwen35::anchor_store::AnchorTransaction::Capture => "anchor_capture",
crate::serve::api::engine_qwen35::anchor_store::AnchorTransaction::Restore => "anchor_restore",
},
Self::TooFewReady { refusals, .. } => {
if refusals.contains(&Qwen35LaneRefusal::AnchorCapture) {
"anchor_capture"
} else if refusals.contains(&Qwen35LaneRefusal::AnchorRestore) {
"anchor_restore"
} else {
"too_few_ready"
}
}
}
}
}
pub(super) enum Qwen35JointRun {
NotEligible(Qwen35JointIneligible),
Ran {
serial: Vec<SlotHandle>,
},
Fatal(Qwen35FatalFailure),
}
struct JointLane {
handle: SlotHandle,
reply: super::SlotReply,
state: crate::serve::api::engine_qwen35::Qwen35DecodeState,
tick: Option<anyhow::Result<Qwen35TickOutcome>>,
}
fn reinstall_lanes(slots: &mut [Option<Qwen35Slot>], lanes: impl IntoIterator<Item = JointLane>) {
for lane in lanes {
let index = lane.handle.slot_id.0 as usize;
debug_assert!(slots[index].is_none());
slots[index] = Some((Qwen35SlotWork::Decode(lane.state), lane.reply, lane.handle));
}
}
pub(in crate::serve::api) fn joint_anchor_admission(
handles: &[SlotHandle],
stores: &[Qwen35AnchorStore],
) -> Option<Qwen35JointIneligible> {
let kind = handles.iter().find_map(|handle|
stores.get(handle.slot_id.0 as usize).and_then(Qwen35AnchorStore::open_transaction))?;
let lanes = handles.iter().filter(|handle|
stores.get(handle.slot_id.0 as usize).and_then(Qwen35AnchorStore::open_transaction) == Some(kind)).count();
Some(Qwen35JointIneligible::AnchorTransaction { kind, lanes })
}
#[allow(clippy::too_many_arguments)]
pub(super) fn run_qwen35_joint_decode(
guard: &mut Qwen35KvGuard<'_>,
scheduler: &mut InflightBatchedScheduler,
slots: &mut [Option<Qwen35Slot>],
retained_tokens: &mut [Qwen35RetainedPrefix],
prompt_caches: &mut [PromptCache],
prompt_anchors: &mut [Qwen35AnchorStore],
registration: Option<&ModelRegistration>,
handles: &[SlotHandle],
decode_ready_lanes: usize,
kv_bytes_per_token: u64,
config: Qwen35JointDecodeConfig,
supervisor: &EngineSupervisor,
) -> Qwen35JointRun {
if let Some(reason) = joint_anchor_admission(handles, prompt_anchors) {
return Qwen35JointRun::NotEligible(reason);
}
if guard
.kv
.as_ref()
.expect("kv Some during loop")
.la_capture_active()
{
return Qwen35JointRun::NotEligible(Qwen35JointIneligible::CaptureActive);
}
if config.max_rows < 2 {
return Qwen35JointRun::NotEligible(Qwen35JointIneligible::MaxRowsBelowTwo);
}
if handles.iter().enumerate().any(|(index, handle)| {
handles[..index]
.iter()
.any(|earlier| earlier.slot_id == handle.slot_id)
}) {
return Qwen35JointRun::NotEligible(Qwen35JointIneligible::DuplicateSlots);
}
for &handle in handles {
if let Some(Some((Qwen35SlotWork::Decode(state), _, installed))) =
slots.get_mut(handle.slot_id.0 as usize)
{
if *installed == handle && state.speculation_route() == QwenSpeculationRoute::Repeatable
{
let _ = state.plan_tick();
}
}
}
let mut refusals = Vec::new();
let mut ready = 0usize;
let Some(JointPartition { groups, mut serial }) = ({
let kv = guard.kv.as_ref().expect("kv Some during loop");
let solo = config.solo || config.joint_mtp;
partition_qwen35_joint_lanes(handles, config.max_rows, solo, |handle| {
let Some((Qwen35SlotWork::Decode(state), reply, installed)) = slots
.get(handle.slot_id.0 as usize)
.and_then(Option::as_ref)
else {
refusals.push(Qwen35LaneRefusal::HandleMismatch);
return None;
};
if *installed != handle {
refusals.push(Qwen35LaneRefusal::HandleMismatch);
return None;
}
if reply.client_closed() {
refusals.push(Qwen35LaneRefusal::ClientClosed);
return None;
}
if let Err(reason) = state.joint_decode_readiness(kv) {
refusals.push(reason);
return None;
}
let class = if config.joint_mtp {
Some(true)
} else {
state.joint_lane_class()
};
if class.is_some() {
ready += 1;
} else {
refusals.push(Qwen35LaneRefusal::SpeculationRoute);
}
class
})
}) else {
return Qwen35JointRun::NotEligible(Qwen35JointIneligible::TooFewReady { ready, refusals });
};
let mut groups = groups.into_iter();
while let Some(group) = groups.next() {
if guard
.kv
.as_ref()
.expect("kv Some during loop")
.la_capture_active()
{
serial.extend(group);
serial.extend(groups.by_ref().flatten());
break;
}
let run = if config.joint_mtp {
joint_mtp::run_qwen35_joint_mtp_group
} else {
run_qwen35_joint_group
};
if let Some(fatal) = run(
guard,
scheduler,
slots,
retained_tokens,
prompt_caches,
prompt_anchors,
registration,
group,
serial.len(),
handles.len(),
decode_ready_lanes,
kv_bytes_per_token,
config,
supervisor,
) {
return Qwen35JointRun::Fatal(fatal);
}
}
Qwen35JointRun::Ran { serial }
}
fn take_joint_lanes(
slots: &mut [Option<Qwen35Slot>],
group: Vec<SlotHandle>,
) -> Result<Vec<JointLane>, Box<Qwen35FatalFailure>> {
let mut lanes = Vec::with_capacity(group.len());
for handle in group {
let index = handle.slot_id.0 as usize;
match slots[index].take() {
Some((Qwen35SlotWork::Decode(state), reply, installed)) if installed == handle => {
lanes.push(JointLane {
handle,
reply,
state,
tick: None,
});
}
other => {
slots[index] = other;
reinstall_lanes(slots, lanes);
return Err(Box::new(Qwen35FatalFailure {
handle: Some(handle),
reply: Qwen35FatalReply::None,
error: anyhow::anyhow!("Qwen joint decode lane changed before execution"),
kind: Qwen35WorkerFatalKind::Invariant,
}));
}
}
}
Ok(lanes)
}
#[allow(clippy::too_many_arguments)]
fn run_qwen35_joint_group(
guard: &mut Qwen35KvGuard<'_>,
scheduler: &mut InflightBatchedScheduler,
slots: &mut [Option<Qwen35Slot>],
retained_tokens: &mut [Qwen35RetainedPrefix],
prompt_caches: &mut [PromptCache],
prompt_anchors: &mut [Qwen35AnchorStore],
registration: Option<&ModelRegistration>,
group: Vec<SlotHandle>,
serial_lanes: usize,
turn_lanes: usize,
decode_ready_lanes: usize,
kv_bytes_per_token: u64,
config: Qwen35JointDecodeConfig,
supervisor: &EngineSupervisor,
) -> Option<Qwen35FatalFailure> {
let mut lanes = match take_joint_lanes(slots, group) {
Ok(lanes) => lanes,
Err(fatal) => return Some(*fatal),
};
let _stage_round =
crate::inference::stage_profile::Round::begin("qwen35", "joint", lanes.len());
let _critical_path_span = if critical_path::enabled() {
critical_path::Span::decode(
"joint",
lanes
.iter()
.map(|lane| (lane.handle.slot_id, lane.state.joint_verify_eligibility())),
)
} else {
None
};
for lane in &mut lanes {
lane.state.consume_planned_tick();
tracing::debug!(
target: "hf2q::serve::api::engine_qwen35::progress",
slot = lane.handle.slot_id.0,
kind = "ordinary",
path = "joint",
turn_lanes,
"Qwen decode tick route"
);
}
static FIRST_JOINT_DECODE: Once = Once::new();
FIRST_JOINT_DECODE.call_once(|| {
tracing::info!(
target: "hf2q::serve::api::engine_qwen35::progress",
lanes = lanes.len(),
mode = config.mode.label(),
dense_ffn = config.dense_ffn.label(),
"Qwen joint decode engaged"
);
});
if lanes.len() >= 2 {
for lane in &mut lanes {
if let Err(error) = lane.state.shadow_draft_before_joint(
guard.model,
guard.kv.as_mut().expect("kv Some during loop"),
supervisor,
) {
lane.tick = Some(Err(error));
}
}
}
let mut prepared: Vec<(usize, Qwen35JointLane)> = Vec::with_capacity(lanes.len());
{
let kv = guard.kv.as_ref().expect("kv Some during loop");
for (index, lane) in lanes.iter_mut().enumerate() {
if lane.tick.is_some() {
continue;
}
match lane.state.prepare_joint_tick(kv) {
Ok(joint_lane) => prepared.push((index, joint_lane)),
Err(error) => lane.tick = Some(Err(error)),
}
}
}
tracing::debug!(
target: "hf2q::serve::api::engine_qwen35::progress",
lanes = lanes.len(),
rows = prepared.len(),
serial = serial_lanes,
path = "joint",
turn_lanes,
"Qwen joint decode turn"
);
if !prepared.is_empty() {
let rows = prepared
.iter()
.map(|(_, lane)| lane.row)
.collect::<Vec<_>>();
let _native_invocation = scheduler.trace_transaction(
"qwen35",
"joint_ordinary_decode",
&prepared
.iter()
.map(|&(index, _)| lanes[index].handle)
.collect::<Vec<_>>(),
);
let forward = guard.model.forward_joint_decode(
&rows,
guard.kv.as_mut().expect("kv Some during loop"),
config.mode,
config.dense_ffn,
config.step,
supervisor,
);
match forward {
Ok(outputs) => {
record_c2_decode_lane_steps(decode_ready_lanes, prepared.len(), 0, "ordinary", "joint");
for ((index, joint_lane), output) in prepared.into_iter().zip(outputs) {
let lane = &mut lanes[index];
lane.tick = Some(lane.state.finish_joint_tick(
guard.model,
guard.kv.as_mut().expect("kv Some during loop"),
supervisor,
joint_lane.target,
output,
));
}
}
Err(error) if !supervisor.is_healthy() || is_fatal_command_buffer_error(&error) => {
let mut owned = lanes.into_iter();
let mut first = owned.next().expect("a joint step has at least one lane");
first.state.emit_route_summary();
reinstall_lanes(slots, owned);
return Some(Qwen35FatalFailure {
handle: Some(first.handle),
reply: Qwen35FatalReply::Slot(first.reply),
error,
kind: Qwen35WorkerFatalKind::Gpu,
});
}
Err(error) => {
let indices = prepared.iter().map(|&(index, _)| index).collect::<Vec<_>>();
for (index, tick) in indices
.iter()
.zip(distribute_step_error(error, indices.len()))
{
lanes[*index].tick = Some(tick);
}
}
}
}
let mut owned = lanes.into_iter();
while let Some(lane) = owned.next() {
let tick = lane
.tick
.unwrap_or_else(|| Err(anyhow::anyhow!("Qwen joint lane produced no tick")));
if let Some(fatal) = settle_qwen35_decode_lane(
guard,
scheduler,
slots,
retained_tokens,
prompt_caches,
prompt_anchors,
registration,
lane.handle,
lane.reply,
lane.state,
tick,
kv_bytes_per_token,
supervisor,
) {
reinstall_lanes(slots, owned);
return Some(fatal);
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use crate::serve::multi_seq_kv::SlotId;
fn handle(slot: u32, generation: u64) -> SlotHandle {
SlotHandle {
slot_id: SlotId(slot),
generation,
}
}
#[test]
fn joint_decode_switch_parses_explicit_arms() {
assert_eq!(parse_qwen_joint_decode(None), Ok(None));
assert_eq!(parse_qwen_joint_decode(Some(" ")), Ok(None));
for raw in ["1", "on", "TRUE"] {
assert_eq!(parse_qwen_joint_decode(Some(raw)), Ok(Some(true)));
}
for raw in ["0", "Off", "false"] {
assert_eq!(parse_qwen_joint_decode(Some(raw)), Ok(Some(false)));
}
assert!(parse_qwen_joint_decode(Some("joint")).is_err());
}
#[test]
fn joint_solo_switch_defaults_on_and_parses_explicit_arms() {
assert_eq!(parse_qwen_joint_solo(None), Ok(true));
assert_eq!(parse_qwen_joint_solo(Some(" ")), Ok(true));
for raw in ["1", "On", "true"] {
assert_eq!(parse_qwen_joint_solo(Some(raw)), Ok(true));
}
for raw in ["0", "OFF", "false"] {
assert_eq!(parse_qwen_joint_solo(Some(raw)), Ok(false));
}
assert!(parse_qwen_joint_solo(Some("solo")).is_err());
}
#[test]
fn joint_mtp_defaults_only_for_eligible_dense_models() {
assert_eq!(parse_qwen_joint_mtp(None, true), Ok(true));
assert_eq!(parse_qwen_joint_mtp(Some(" "), true), Ok(true));
assert_eq!(parse_qwen_joint_mtp(None, false), Ok(false));
assert_eq!(parse_qwen_joint_mtp(Some("0"), true), Ok(false));
assert_eq!(parse_qwen_joint_mtp(Some("1"), false), Ok(true));
assert!(parse_qwen_joint_mtp(Some("mtp"), true).is_err());
}
#[test]
fn joint_mtp_selects_mma_unless_explicitly_overridden() {
assert_eq!(parse_qwen_joint_mma(None, false), Ok(false));
assert_eq!(parse_qwen_joint_mma(Some(" "), false), Ok(false));
assert_eq!(parse_qwen_joint_mma(None, true), Ok(true));
assert_eq!(parse_qwen_joint_mma(Some(" "), true), Ok(true));
assert_eq!(parse_qwen_joint_mma(Some("0"), true), Ok(false));
assert_eq!(parse_qwen_joint_mma(Some("1"), false), Ok(true));
assert!(parse_qwen_joint_mma(Some("mma"), true).is_err());
}
#[test]
fn joint_width_defaults_to_four_and_accepts_two_through_eight() {
assert_eq!(parse_qwen_joint_decode_max_rows(None), Ok(4));
assert_eq!(parse_qwen_joint_decode_max_rows(Some(" ")), Ok(4));
for rows in 2..=MAX_JOINT_DECODE_ROWS {
assert_eq!(
parse_qwen_joint_decode_max_rows(Some(&rows.to_string())),
Ok(rows)
);
}
for raw in ["0", "1", "9", "four"] {
assert!(parse_qwen_joint_decode_max_rows(Some(raw)).is_err());
}
}
#[test]
fn refused_artifacts_and_disabled_switch_keep_serial_decode() {
let pooled = JointStepOptions {
pooled_outputs: true,
..JointStepOptions::default()
};
assert_eq!(
select_qwen_joint_decode(
true,
4,
Ok(JointRouteMode::Exact),
true,
true,
4,
pooled,
true
),
Some(Qwen35JointDecodeConfig {
max_rows: 4,
mode: JointRouteMode::Exact,
dense_ffn: JointDenseFfnRoute::SerialBuilder,
solo: false,
step: pooled,
joint_mtp: true,
})
);
assert_eq!(
select_qwen_joint_decode(
true,
4,
Err("layer 3: attn_qkv is IQ4_NL, which has no proven joint decode route".into()),
true,
true,
4,
JointStepOptions::default(),
true
),
None
);
assert_eq!(
select_qwen_joint_decode(
false,
4,
Ok(JointRouteMode::PaddedMvExt),
true,
true,
4,
JointStepOptions::default(),
true
),
None
);
}
#[test]
fn mma_projection_is_padded_mode_only() {
let mma = JointStepOptions {
projection: JointProjectionKernel::ColumnInvariantMma,
..JointStepOptions::default()
};
let projection = |mode| {
select_qwen_joint_decode(true, 4, Ok(mode), true, true, 4, mma, false)
.expect("admitted mode")
.step
.projection
};
assert_eq!(
projection(JointRouteMode::PaddedMvExt),
JointProjectionKernel::ColumnInvariantMma
);
assert_eq!(
projection(JointRouteMode::Exact),
JointProjectionKernel::Canonical
);
}
#[test]
fn padded_mode_caps_joint_width_at_four() {
for requested in 2..=MAX_JOINT_DECODE_ROWS {
let padded = select_qwen_joint_decode(
true,
requested,
Ok(JointRouteMode::PaddedMvExt),
true,
true,
4,
JointStepOptions::default(),
false,
)
.expect("admitted padded mode");
assert_eq!(padded.max_rows, requested.min(4));
let exact = select_qwen_joint_decode(
true,
requested,
Ok(JointRouteMode::Exact),
true,
true,
4,
JointStepOptions::default(),
false,
)
.expect("admitted exact mode");
assert_eq!(exact.max_rows, requested);
}
}
#[test]
fn joint_mtp_requires_a_lone_lane_joint_route() {
let joint_mtp = |mode, solo_requested, n_slots, requested| {
select_qwen_joint_decode(
true,
4,
Ok(mode),
true,
solo_requested,
n_slots,
JointStepOptions::default(),
requested,
)
.expect("admitted mode")
.joint_mtp
};
assert!(joint_mtp(JointRouteMode::PaddedMvExt, true, 4, true));
assert!(joint_mtp(JointRouteMode::Exact, false, 4, true));
assert!(!joint_mtp(JointRouteMode::PaddedMvExt, false, 4, true));
assert!(!joint_mtp(JointRouteMode::PaddedMvExt, true, 1, true));
assert!(!joint_mtp(JointRouteMode::PaddedMvExt, true, 4, false));
assert!(!joint_mtp(JointRouteMode::Exact, true, 4, false));
}
#[test]
fn solo_route_is_padded_mode_on_multi_slot_workers_only() {
let solo = |mode, solo_requested, n_slots| {
select_qwen_joint_decode(
true,
4,
Ok(mode),
true,
solo_requested,
n_slots,
JointStepOptions::default(),
false,
)
.expect("admitted mode")
.solo
};
for n_slots in 2..=8 {
assert!(solo(JointRouteMode::PaddedMvExt, true, n_slots));
assert!(!solo(JointRouteMode::PaddedMvExt, false, n_slots));
assert!(!solo(JointRouteMode::Exact, true, n_slots));
}
assert!(!solo(JointRouteMode::PaddedMvExt, true, 1));
}
fn classify_by(eligible: &[u32], ordinary: &[u32]) -> impl Fn(SlotHandle) -> Option<bool> {
let eligible = eligible.to_vec();
let ordinary = ordinary.to_vec();
move |handle| {
eligible
.contains(&handle.slot_id.0)
.then(|| ordinary.contains(&handle.slot_id.0))
}
}
#[test]
fn ragged_eligible_lanes_join_sorted_and_the_rest_stay_serial() {
let handles = [handle(3, 9), handle(0, 1), handle(2, 4), handle(1, 7)];
for solo in [false, true] {
let partition = partition_qwen35_joint_lanes(
&handles,
4,
solo,
classify_by(&[0, 1, 3], &[0, 1, 3]),
)
.expect("three eligible lanes form a joint step");
assert_eq!(
partition,
JointPartition {
groups: vec![vec![handle(0, 1), handle(1, 7), handle(3, 9)]],
serial: vec![handle(2, 4)],
}
);
}
}
#[test]
fn phase_one_partition_keeps_fewer_than_two_eligible_lanes_serial() {
let handles = [handle(0, 1), handle(1, 1)];
assert!(
partition_qwen35_joint_lanes(&handles, 4, false, classify_by(&[0], &[0])).is_none()
);
assert!(partition_qwen35_joint_lanes(&[], 4, false, |_| Some(true)).is_none());
assert!(partition_qwen35_joint_lanes(&handles, 1, false, |_| Some(true)).is_none());
}
#[test]
fn solo_partition_runs_a_lone_ordinary_lane_jointly() {
let handles = [handle(0, 1), handle(1, 1)];
assert_eq!(
partition_qwen35_joint_lanes(&handles, 4, true, classify_by(&[1], &[1])),
Some(JointPartition {
groups: vec![vec![handle(1, 1)]],
serial: vec![handle(0, 1)],
})
);
assert!(partition_qwen35_joint_lanes(&handles, 4, true, classify_by(&[1], &[])).is_none());
assert_eq!(
partition_qwen35_joint_lanes(&handles, 4, true, classify_by(&[0, 1], &[])),
Some(JointPartition {
groups: vec![vec![handle(0, 1), handle(1, 1)]],
serial: vec![],
})
);
assert!(partition_qwen35_joint_lanes(&[], 4, true, |_| Some(true)).is_none());
}
#[test]
fn duplicate_slots_never_form_a_joint_step() {
let handles = [handle(0, 1), handle(1, 1), handle(0, 2)];
for solo in [false, true] {
assert!(partition_qwen35_joint_lanes(&handles, 4, solo, |_| Some(true)).is_none());
}
}
#[test]
fn phase_one_partition_caps_the_joint_width_and_overflow_lanes_stay_serial() {
let handles = [handle(4, 1), handle(3, 1), handle(2, 1), handle(1, 1)];
let partition = partition_qwen35_joint_lanes(&handles, 3, false, |_| Some(true))
.expect("capped joint step");
assert_eq!(
partition,
JointPartition {
groups: vec![vec![handle(2, 1), handle(3, 1), handle(4, 1)]],
serial: vec![handle(1, 1)],
}
);
}
#[test]
fn solo_partition_puts_every_eligible_lane_in_a_group_at_any_width() {
let handles = (0..8).rev().map(|slot| handle(slot, 1)).collect::<Vec<_>>();
for max_rows in 2..=4 {
for lanes in 1..=handles.len() {
let partition =
partition_qwen35_joint_lanes(&handles[..lanes], max_rows, true, |_| Some(true))
.expect("ordinary lanes always form groups");
assert!(
partition.serial.is_empty(),
"max_rows={max_rows} lanes={lanes}"
);
assert_eq!(partition.groups.len(), lanes.div_ceil(max_rows));
let mut joined = partition.groups.concat();
assert!(partition.groups.iter().all(|group| {
(1..=max_rows).contains(&group.len())
&& group
.windows(2)
.all(|pair| pair[0].slot_id < pair[1].slot_id)
}));
joined.sort_by_key(|handle| handle.slot_id);
let mut expected = handles[..lanes].to_vec();
expected.sort_by_key(|handle| handle.slot_id);
assert_eq!(joined, expected);
}
}
let partition = partition_qwen35_joint_lanes(
&handles[..5],
4,
true,
classify_by(&[3, 4, 5, 6, 7], &[4, 5, 6, 7]),
)
.expect("one full group");
assert_eq!(
partition,
JointPartition {
groups: vec![vec![handle(4, 1), handle(5, 1), handle(6, 1), handle(7, 1)]],
serial: vec![handle(3, 1)],
}
);
}
#[test]
fn a_failed_step_fails_exactly_its_rows_with_the_original_error_first() {
let errors = distribute_step_error::<()>(
anyhow::anyhow!("command buffer failed").context("Qwen joint decode"),
3,
)
.collect::<Vec<_>>();
assert_eq!(errors.len(), 3);
for error in &errors {
let rendered = format!("{:#}", error.as_ref().expect_err("every row fails"));
assert!(rendered.contains("command buffer failed"), "{rendered}");
assert!(rendered.contains("Qwen joint decode"), "{rendered}");
}
assert_eq!(
distribute_step_error::<()>(anyhow::anyhow!("x"), 0).count(),
0
);
}
}