use anyhow::{Context, Result};
use mlx_native::MlxBuffer;
use std::time::{Duration, Instant};
use crate::inference::models::qwen35::joint_decode_core::rows_view;
use crate::inference::models::qwen35::joint_decode_routes::{
JointDenseFfnRoute, JointRouteMode, MAX_JOINT_DECODE_ROWS, MAX_JOINT_VERIFY_ROWS,
};
use crate::inference::models::qwen35::joint_projection::JointProjectionKernel;
use crate::inference::models::qwen35::joint_step_options::JointStepOptions;
use crate::inference::models::qwen35::kv_cache::HybridKvCache;
use crate::inference::models::qwen35::mtp::MtpDraftLane;
use crate::inference::models::qwen35::spec_decode::{nth_hidden_row, positions_for_range};
use crate::inference::models::qwen35::{
Qwen35JointOutput, Qwen35JointRowOutput, Qwen35JointVerify, Qwen35JointVerifyLane,
Qwen35JointVerifyPending,
};
use crate::serve::api::engine_supervisor::EngineSupervisor;
use crate::serve::api::qwen35_speculation::{record_proposer_outcome, QwenSpeculationProposer};
use crate::serve::multi_seq_kv::SlotId;
use super::{
mtp_cursor_for_slot, plan_qwen35_verified_block, take_pending_speculation_output,
Qwen35CanonicalDecision, Qwen35DecisionSource, Qwen35DecodeState, Qwen35JointLane,
Qwen35LoadedModel, Qwen35SpecSemanticState, Qwen35TickOutcome, Qwen35VerifiedBlockPlan,
QWEN35_WORKER_TRANSACTION_TIMEOUT,
};
pub(in crate::serve::api) const JOINT_MTP_MAX_ROWS: usize = MAX_JOINT_DECODE_ROWS;
pub(in crate::serve::api) const JOINT_MTP_MAX_DRAFTS: usize = 3;
fn bounded_draft_depths(caps: &[usize], max_rows: usize) -> Vec<usize> {
let mut depths = vec![0; caps.len()];
let mut budget = max_rows.saturating_sub(caps.len());
while budget > 0 {
let mut granted = false;
for (depth, &cap) in depths.iter_mut().zip(caps) {
if budget > 0 && *depth < cap.min(JOINT_MTP_MAX_DRAFTS) {
*depth += 1;
budget -= 1;
granted = true;
}
}
if !granted {
break;
}
}
depths
}
pub(in crate::serve::api) fn cap8_draft_depths(caps: &[usize]) -> Vec<usize> {
bounded_draft_depths(caps, JOINT_MTP_MAX_ROWS)
}
fn diagnostic_draft_depths(caps: &[usize]) -> Vec<usize> {
bounded_draft_depths(caps, MAX_JOINT_VERIFY_ROWS)
}
fn plan_qwen35_window(
drafts: &[u32],
mut canonical_at: impl FnMut(usize) -> Result<Qwen35CanonicalDecision>,
) -> Result<Qwen35VerifiedBlockPlan> {
if !drafts.is_empty() {
return plan_qwen35_verified_block(drafts, canonical_at);
}
let decision = canonical_at(0)?;
Ok(Qwen35VerifiedBlockPlan {
output: if decision.terminal {
Default::default()
} else {
[decision.token].into()
},
terminal_after_pending: decision.terminal,
matched_drafts: 0,
rejected_drafts: 0,
valid_input_tokens: 1,
carry_hidden_row: 0,
})
}
pub(in crate::serve::api) struct Qwen35JointMtpWindow {
pending_token: u32,
position: i32,
prior: u32,
verifier_hidden: MlxBuffer,
greedy: bool,
draft_cap: usize,
}
pub(in crate::serve::api) enum Qwen35JointMtpRole {
Ordinary(Qwen35JointLane),
Window(Qwen35JointMtpWindow),
}
impl Qwen35JointMtpRole {
pub(in crate::serve::api) fn draft_cap(&self) -> usize {
match self {
Self::Ordinary(_) => 0,
Self::Window(window) => window.draft_cap,
}
}
}
pub(in crate::serve::api) struct Qwen35JointMtpPlan {
plan: Qwen35VerifiedBlockPlan,
}
impl Qwen35JointMtpPlan {
pub(in crate::serve::api) fn accepted(&self) -> usize {
self.plan.valid_input_tokens
}
}
pub(in crate::serve::api) struct Qwen35JointMtpDraftRequest<'a> {
pub slot: SlotId,
pub token: u32,
pub position: i32,
pub hidden: &'a MlxBuffer,
pub depth: usize,
pub prior_mtp: u32,
}
impl Qwen35JointMtpWindow {
pub(in crate::serve::api) fn draft_request(
&self,
slot: SlotId,
depth: usize,
) -> Qwen35JointMtpDraftRequest<'_> {
Qwen35JointMtpDraftRequest {
slot,
token: self.pending_token,
position: self.position,
hidden: &self.verifier_hidden,
depth,
prior_mtp: self.prior,
}
}
pub(in crate::serve::api) fn output(&self) -> Qwen35JointOutput {
if self.greedy {
Qwen35JointOutput::GreedyTokenAndHidden
} else {
Qwen35JointOutput::LogitsAndHidden
}
}
pub(in crate::serve::api) fn tokens(&self, drafts: &[u32]) -> Vec<u32> {
let mut tokens = Vec::with_capacity(drafts.len() + 1);
tokens.push(self.pending_token);
tokens.extend_from_slice(drafts);
tokens
}
}
impl Qwen35DecodeState {
pub(in crate::serve::api) fn prepare_joint_mtp(
&mut self,
kv_cache: &HybridKvCache,
) -> Result<Qwen35JointMtpRole> {
let Some(mtp) = self.mtp.as_ref() else {
return Ok(Qwen35JointMtpRole::Ordinary(
self.prepare_joint_tick(kv_cache)?,
));
};
let pending_token = *self
.generated_tokens
.last()
.context("Qwen joint MTP window needs a seeded output token")?;
let position = (self.decode_position_base + self.generated_tokens.len() - 1) as i32;
let prior = kv_cache.sequence_len_for_slot(self.slot_id)?;
let prior_mtp = mtp_cursor_for_slot(kv_cache, self.slot_id)?;
anyhow::ensure!(
prior == prior_mtp,
"Qwen joint MTP window requires equal entry cursors (target={prior}, mtp={prior_mtp})"
);
let remaining = self.max_tokens.saturating_sub(self.generated_tokens.len());
anyhow::ensure!(remaining > 0, "Qwen joint MTP window past the length limit");
Ok(Qwen35JointMtpRole::Window(Qwen35JointMtpWindow {
pending_token,
position,
prior,
verifier_hidden: mtp.verifier_hidden.clone(),
greedy: self.is_greedy && !self.want_logprobs,
draft_cap: (remaining - 1).min(JOINT_MTP_MAX_DRAFTS),
}))
}
pub(in crate::serve::api) fn plan_joint_mtp_window(
&self,
qwen: &Qwen35LoadedModel,
window: &Qwen35JointMtpWindow,
drafts: &[u32],
mut rows: Vec<Qwen35JointRowOutput>,
) -> Result<Qwen35JointMtpPlan> {
anyhow::ensure!(
rows.len() == drafts.len() + 1,
"Qwen joint MTP window verified {} rows for {} drafts",
rows.len(),
drafts.len()
);
let mut semantic = Qwen35SpecSemanticState::from_decode(self);
plan_qwen35_window(drafts, |row| {
let output = &mut rows[row];
let source = if window.greedy {
Qwen35DecisionSource::Argmax(
output
.greedy_token
.context("Qwen joint MTP greedy row returned no argmax")?,
)
} else {
Qwen35DecisionSource::Logits(
output
.logits
.as_deref_mut()
.context("Qwen joint MTP row returned no logits")?,
)
};
semantic.select_and_observe_from(qwen, &self.params, source, None)
})
.map(|plan| Qwen35JointMtpPlan { plan })
}
#[allow(clippy::too_many_arguments)]
pub(in crate::serve::api) fn settle_joint_mtp_window(
&mut self,
qwen: &Qwen35LoadedModel,
kv_cache: &mut HybridKvCache,
supervisor: &EngineSupervisor,
window: Qwen35JointMtpWindow,
tokens: &[u32],
hidden_rows: &MlxBuffer,
plan: Qwen35JointMtpPlan,
projection: JointProjectionKernel,
) -> Result<()> {
let Qwen35JointMtpPlan { plan } = plan;
let accepted = plan.valid_input_tokens;
anyhow::ensure!(
(1..=tokens.len()).contains(&accepted),
"Qwen joint MTP window accepted {accepted} of {} rows",
tokens.len()
);
let hidden_size = qwen.model.cfg.hidden_size;
let mtp = qwen
.model
.mtp
.as_ref()
.context("Qwen joint MTP catch-up weights missing")?;
let accepted_tokens = &tokens[..accepted];
let _carry_commitment =
crate::inference::models::qwen35::carry_memory::commit_target(
&qwen.model.cfg, accepted,
)?;
let catchup_owners =
crate::inference::models::qwen35::carry_memory::CatchupTransaction::open(
&qwen.model.cfg, accepted, hidden_rows, Some(&window.verifier_hidden),
)?;
let shared_embed = catchup_owners.gather(|| qwen.model
.embed_tokens_gpu(accepted_tokens)
.context("Qwen joint MTP catch-up embeddings"))?;
let target_rows = rows_view(hidden_rows, 0, accepted, hidden_size as usize)
.map_err(|error| catchup_owners.settle_error(error))?;
let lease = supervisor.arm(
"Qwen35 joint MTP catch-up",
QWEN35_WORKER_TRANSACTION_TIMEOUT,
).map_err(|error| catchup_owners.settle_error(error))?;
let caught_up = qwen.model.with_gpu_cache_mut(|device, registry| {
mtp.process_target_batch(
accepted_tokens,
Some(&window.verifier_hidden),
&target_rows,
&shared_embed,
kv_cache,
self.slot_id,
&positions_for_range(window.position, accepted),
device,
registry,
&qwen.model.cfg,
projection,
)
});
let caught_up = catchup_owners.finish(caught_up);
caught_up
.and(lease.finish())
.context("Qwen joint MTP catch-up")?;
kv_cache
.validate_speculative_cursors_for_slot(self.slot_id, window.prior as usize + accepted)
.context("Qwen joint MTP committed cursor equality")?;
let carry = nth_hidden_row(hidden_rows, hidden_size, plan.carry_hidden_row as u64)?;
self.mtp
.as_mut()
.context("Qwen joint MTP state vanished")?
.verifier_hidden = carry;
let drafted = tokens.len() - 1;
if drafted > 0 {
record_proposer_outcome(
QwenSpeculationProposer::Mtp,
drafted,
plan.matched_drafts,
plan.rejected_drafts,
1,
0,
);
}
self.pending_speculation_output = plan.output;
self.terminal_after_pending = plan.terminal_after_pending;
Ok(())
}
pub(in crate::serve::api) fn has_pending_speculation_output(&self) -> bool {
!self.pending_speculation_output.is_empty()
}
pub(in crate::serve::api) fn joint_mtp_drain_tick(
&mut self,
qwen: &Qwen35LoadedModel,
) -> Result<Qwen35TickOutcome> {
match take_pending_speculation_output(&mut self.pending_speculation_output) {
Some(token) => self.commit_speculation_output(qwen, token),
None => {
anyhow::ensure!(
self.terminal_after_pending,
"Qwen joint MTP drain without verified output"
);
self.finish_reason = "stop";
Ok(Qwen35TickOutcome {
fragment: String::new(),
is_reasoning: false,
finished: true,
})
}
}
}
}
impl Qwen35LoadedModel {
pub(in crate::serve::api) fn draft_joint_mtp(
&self,
kv_cache: &mut HybridKvCache,
lanes: &[Qwen35JointMtpDraftRequest<'_>],
projection: JointProjectionKernel,
supervisor: &EngineSupervisor,
) -> Result<Vec<Vec<u32>>> {
let max_depth = lanes.iter().map(|lane| lane.depth).max().unwrap_or(0);
if max_depth == 0 {
return Ok(vec![Vec::new(); lanes.len()]);
}
let mtp = self
.model
.mtp
.as_ref()
.context("Qwen joint MTP draft weights missing")?;
let lease = supervisor.arm("Qwen35 joint MTP draft", QWEN35_WORKER_TRANSACTION_TIMEOUT)?;
let draft = || self.model.with_gpu_cache_mut(|device, registry| {
let mut drafts = vec![Vec::with_capacity(max_depth); lanes.len()];
let mut hidden: Vec<Option<MlxBuffer>> = vec![None; lanes.len()];
let mut tokens = lanes.iter().map(|lane| lane.token).collect::<Vec<_>>();
for depth in 0..max_depth {
let active = (0..lanes.len())
.filter(|&index| lanes[index].depth > depth)
.collect::<Vec<_>>();
let active_tokens = active
.iter()
.map(|&index| tokens[index])
.collect::<Vec<_>>();
let shared_embed =
self.model
.embed_tokens_gpu_in_context(&active_tokens, device, registry)?;
let rows = active
.iter()
.map(|&index| MtpDraftLane {
slot_id: lanes[index].slot,
prev_hidden: hidden[index].as_ref().unwrap_or(lanes[index].hidden),
token: tokens[index],
position: lanes[index].position + depth as i32,
})
.collect::<Vec<_>>();
let outputs = mtp.forward_draft_greedy_batch(
&rows,
&shared_embed,
kv_cache,
device,
registry,
&self.model.cfg,
projection,
)?;
drop(rows);
for (&index, (token, next_hidden)) in active.iter().zip(outputs) {
drafts[index].push(token);
tokens[index] = token;
hidden[index] = Some(next_hidden);
}
}
Ok(drafts)
});
#[cfg(test)]
let drafted = crate::inference::models::qwen35::mtp::MtpWeights::with_checked_draft_commands(draft);
#[cfg(not(test))]
let drafted = {
let mut draft = draft;
draft()
};
let supervision = lease.finish();
let mut restore = Ok(());
for lane in lanes {
if let Err(error) = kv_cache.truncate_mtp_to_for_slot(lane.slot, lane.prior_mtp) {
restore = Err(error.context(format!(
"Qwen joint MTP discard draft cache slot {}",
lane.slot.0
)));
}
}
supervision.context("Qwen joint MTP draft supervision")?;
let drafts = drafted.context("Qwen joint MTP draft")?;
restore?;
Ok(drafts)
}
pub(in crate::serve::api) fn forward_joint_verify(
&self,
lanes: &[Qwen35JointVerifyLane<'_>],
kv_cache: &mut HybridKvCache,
mode: JointRouteMode,
dense_ffn: JointDenseFfnRoute,
options: JointStepOptions,
supervisor: &EngineSupervisor,
) -> Result<Qwen35JointVerify> {
let rows = lanes.iter().try_fold(0usize, |rows, lane| {
rows.checked_add(lane.tokens.len()).context("joint carry rows overflow")
})?;
let _carry_commitment =
crate::inference::models::qwen35::carry_memory::commit_target(&self.model.cfg, rows)?;
let lease = supervisor.arm("Qwen35 joint MTP verify", QWEN35_WORKER_TRANSACTION_TIMEOUT)?;
let verify = self
.model
.forward_gpu_joint_verify(lanes, kv_cache, mode, dense_ffn, options);
lease.finish()?;
verify.with_context(|| {
format!(
"Qwen35Model::forward_gpu_joint_verify ({} lanes, {} mode, {} dense FFN, {options:?})",
lanes.len(),
mode.label(),
dense_ffn.label()
)
})
}
pub(in crate::serve::api) fn commit_joint_verify(
&self,
kv_cache: &mut HybridKvCache,
pending: Qwen35JointVerifyPending,
accepted: &[usize],
supervisor: &EngineSupervisor,
) -> Result<()> {
let lease = supervisor.arm("Qwen35 joint MTP commit", QWEN35_WORKER_TRANSACTION_TIMEOUT)?;
let committed = self.model.commit_joint_verify(kv_cache, pending, accepted);
lease.finish()?;
committed.context("Qwen joint MTP commit")
}
}
#[derive(Clone, Copy, Debug)]
pub(in crate::serve::api) struct Qwen35JointMtpForward {
pub mode: JointRouteMode,
pub dense_ffn: JointDenseFfnRoute,
pub options: JointStepOptions,
}
#[derive(Clone, Copy, Debug, Default)]
pub(in crate::serve::api) struct Qwen35JointMtpStageTiming {
pub prepare: Duration,
pub draft: Duration,
pub verify_readback: Duration,
pub selection: Duration,
pub capture_recovery: Duration,
pub settle_catch_up: Duration,
pub total: Duration,
}
pub(in crate::serve::api) type Qwen35JointMtpDrafter<'a> = dyn FnMut(
&Qwen35LoadedModel,
&mut HybridKvCache,
&[Qwen35JointMtpDraftRequest<'_>],
) -> Result<Vec<Vec<u32>>>
+ 'a;
enum Verified {
Ordinary(Qwen35JointRowOutput),
Window {
plan: Qwen35JointMtpPlan,
tokens: Vec<u32>,
},
Failed(anyhow::Error),
}
#[allow(clippy::too_many_arguments)]
pub(in crate::serve::api) fn run_joint_mtp_step(
qwen: &Qwen35LoadedModel,
kv_cache: &mut HybridKvCache,
states: &mut [&mut Qwen35DecodeState],
forward: Qwen35JointMtpForward,
supervisor: &EngineSupervisor,
drafter: &mut Qwen35JointMtpDrafter<'_>,
is_fatal: &dyn Fn(&anyhow::Error) -> bool,
diagnostic_draft_caps: Option<&[usize]>,
diagnostic_timing: Option<&mut Qwen35JointMtpStageTiming>,
) -> Result<Vec<Result<Qwen35TickOutcome>>> {
let lanes = states.len();
if let Some(caps) = diagnostic_draft_caps {
anyhow::ensure!(
caps.len() == lanes,
"diagnostic draft caps have {} lanes, expected {lanes}",
caps.len()
);
}
let round_started = Instant::now();
let mut observed = Qwen35JointMtpStageTiming::default();
let stage_started = Instant::now();
let mut ticks: Vec<Option<Result<Qwen35TickOutcome>>> = (0..lanes).map(|_| None).collect();
let fail_all = |ticks: &mut Vec<Option<Result<Qwen35TickOutcome>>>,
indices: &[usize],
error: anyhow::Error| {
let message = format!("{error:#}");
let mut first = Some(error);
for &index in indices {
ticks[index] = Some(Err(first
.take()
.unwrap_or_else(|| anyhow::anyhow!("{message}"))));
}
};
let mut roles: Vec<Option<Qwen35JointMtpRole>> = Vec::with_capacity(lanes);
for (index, state) in states.iter_mut().enumerate() {
match state.prepare_joint_mtp(kv_cache) {
Ok(role) => roles.push(Some(role)),
Err(error) => {
ticks[index] = Some(Err(error));
roles.push(None);
}
}
}
observed.prepare = stage_started.elapsed();
let stage_started = Instant::now();
let caps = roles
.iter()
.enumerate()
.map(|(index, role)| {
let cap = role.as_ref().map_or(0, Qwen35JointMtpRole::draft_cap);
diagnostic_draft_caps.map_or(cap, |limits| cap.min(limits[index]))
})
.collect::<Vec<_>>();
let depths = if diagnostic_draft_caps.is_some() {
diagnostic_draft_depths(&caps)
} else {
cap8_draft_depths(&caps)
};
let mut drafts = vec![Vec::new(); lanes];
let drafting = (0..lanes)
.filter(|&index| depths[index] > 0)
.collect::<Vec<_>>();
if !drafting.is_empty() {
let requests = drafting
.iter()
.filter_map(|&index| match roles[index].as_ref() {
Some(Qwen35JointMtpRole::Window(window)) => {
Some(window.draft_request(states[index].slot_id, depths[index]))
}
_ => None,
})
.collect::<Vec<_>>();
let drafted = drafter(qwen, kv_cache, &requests).and_then(|outputs| {
anyhow::ensure!(
outputs.len() == requests.len()
&& outputs
.iter()
.zip(&requests)
.all(|(drafts, request)| drafts.len() == request.depth),
"Qwen joint MTP drafter returned the wrong draft counts"
);
Ok(outputs)
});
drop(requests);
match drafted {
Ok(outputs) => {
for (&index, output) in drafting.iter().zip(outputs) {
drafts[index] = output;
}
}
Err(error) if is_fatal(&error) => return Err(error),
Err(error) => {
for &index in &drafting {
roles[index] = None;
}
fail_all(&mut ticks, &drafting, error);
}
}
}
observed.draft = stage_started.elapsed();
let verifying = (0..lanes)
.filter(|&index| roles[index].is_some())
.collect::<Vec<_>>();
if !verifying.is_empty() {
let stage_started = Instant::now();
let tokens = verifying
.iter()
.map(|&index| match roles[index].as_ref() {
Some(Qwen35JointMtpRole::Window(window)) => window.tokens(&drafts[index]),
Some(Qwen35JointMtpRole::Ordinary(lane)) => vec![lane.row.token],
None => unreachable!("verifying lanes hold a role"),
})
.collect::<Vec<_>>();
let verify_lanes = verifying
.iter()
.zip(&tokens)
.map(|(&index, tokens)| match roles[index].as_ref() {
Some(Qwen35JointMtpRole::Window(window)) => Qwen35JointVerifyLane {
slot_id: states[index].slot_id,
position: window.position,
tokens,
output: window.output(),
},
Some(Qwen35JointMtpRole::Ordinary(lane)) => Qwen35JointVerifyLane {
slot_id: lane.row.slot_id,
position: lane.row.position,
tokens,
output: lane.row.output,
},
None => unreachable!("verifying lanes hold a role"),
})
.collect::<Vec<_>>();
let verify = qwen.forward_joint_verify(
&verify_lanes,
kv_cache,
forward.mode,
forward.dense_ffn,
forward.options,
supervisor,
);
drop(verify_lanes);
let Qwen35JointVerify {
rows,
hidden,
pending,
} = match verify {
Ok(verify) => verify,
Err(error) if is_fatal(&error) => return Err(error),
Err(error) => {
fail_all(&mut ticks, &verifying, error);
return Ok(finish_ticks(ticks));
}
};
observed.verify_readback = stage_started.elapsed();
let stage_started = Instant::now();
let mut accepted = Vec::with_capacity(verifying.len());
let mut verified = Vec::with_capacity(verifying.len());
for ((&index, lane_rows), tokens) in verifying.iter().zip(rows).zip(tokens) {
let step = match roles[index].as_ref() {
Some(Qwen35JointMtpRole::Ordinary(_)) => match lane_rows.into_iter().next() {
Some(output) => Verified::Ordinary(output),
None => Verified::Failed(anyhow::anyhow!(
"Qwen joint MTP ordinary row returned no output"
)),
},
Some(Qwen35JointMtpRole::Window(window)) => {
match states[index].plan_joint_mtp_window(
qwen,
window,
&drafts[index],
lane_rows,
) {
Ok(plan) => Verified::Window { plan, tokens },
Err(error) => Verified::Failed(error),
}
}
None => unreachable!("verifying lanes hold a role"),
};
accepted.push(match &step {
Verified::Ordinary(_) => 1,
Verified::Window { plan, .. } => plan.accepted(),
Verified::Failed(_) => 0,
});
verified.push(step);
}
observed.selection = stage_started.elapsed();
let stage_started = Instant::now();
let committed = qwen.commit_joint_verify(kv_cache, pending, &accepted, supervisor);
observed.capture_recovery = stage_started.elapsed();
match committed {
Ok(()) => {
let stage_started = Instant::now();
for ((&index, step), hidden_rows) in verifying.iter().zip(verified).zip(&hidden) {
let role = roles[index].take().expect("verifying lanes hold a role");
let state = &mut *states[index];
ticks[index] = Some(match (role, step) {
(Qwen35JointMtpRole::Ordinary(lane), Verified::Ordinary(output)) => {
state.finish_joint_tick(qwen, kv_cache, supervisor, lane.target, output)
}
(Qwen35JointMtpRole::Window(window), Verified::Window { plan, tokens }) => {
let tick = state
.settle_joint_mtp_window(
qwen,
kv_cache,
supervisor,
window,
&tokens,
hidden_rows,
plan,
forward.options.projection,
)
.and_then(|()| state.joint_mtp_drain_tick(qwen));
#[cfg(test)]
if tick.is_ok() && !drafts[index].is_empty() {
super::anchor_exactness_tests::trace::route(
state.slot_id, "JointMTP-actual-window-completed");
}
tick
}
(_, Verified::Failed(error)) => Err(error),
_ => Err(anyhow::anyhow!("Qwen joint MTP lane role changed")),
});
}
observed.settle_catch_up = stage_started.elapsed();
}
Err(error) if is_fatal(&error) => return Err(error),
Err(error) => fail_all(&mut ticks, &verifying, error),
}
}
observed.total = round_started.elapsed();
if let Some(timing) = diagnostic_timing {
*timing = observed;
}
Ok(finish_ticks(ticks))
}
fn finish_ticks(ticks: Vec<Option<Result<Qwen35TickOutcome>>>) -> Vec<Result<Qwen35TickOutcome>> {
ticks
.into_iter()
.map(|tick| {
tick.unwrap_or_else(|| Err(anyhow::anyhow!("Qwen joint MTP lane produced no tick")))
})
.collect()
}
#[cfg(test)]
#[path = "joint_mtp_tests.rs"]
mod tests;