use std::sync::atomic::Ordering;
use std::sync::Once;
use crate::inference::models::deepseek4::{Deepseek4Model, MAX_JOINT_DECODE_ROWS};
use crate::serve::api::engine_deepseek4::{
Deepseek4LoadedModel, Deepseek4Session, Deepseek4SlotState,
};
use crate::serve::api::registry::ModelRegistration;
use super::{
deepseek4_effective_decode_quantum, is_worker_fatal, settle_deepseek4_decode_lane,
Deepseek4DecodeLaneOutcome, Deepseek4Slot, Deepseek4SlotWork, EngineSupervisor,
InflightBatchedScheduler, SlotAwareGpuFatal, SlotHandle, SlotReply,
};
const DEEPSEEK4_JOINT_DECODE_ENV: &str = "HF2Q_DEEPSEEK_JOINT_DECODE";
const DEEPSEEK4_JOINT_DECODE_DEFAULT: bool = true;
const DEEPSEEK4_JOINT_DECODE_MAX_ROWS_ENV: &str = "HF2Q_DEEPSEEK_JOINT_DECODE_MAX_ROWS";
const DEFAULT_DEEPSEEK4_JOINT_DECODE_ROWS: usize = 4;
fn parse_deepseek4_joint_decode(raw: Option<&str>) -> Result<bool, String> {
let Some(raw) = raw.map(str::trim).filter(|raw| !raw.is_empty()) else {
return Ok(DEEPSEEK4_JOINT_DECODE_DEFAULT);
};
match raw.to_ascii_lowercase().as_str() {
"1" | "on" | "true" => Ok(true),
"0" | "off" | "false" => Ok(false),
_ => Err(format!(
"{DEEPSEEK4_JOINT_DECODE_ENV} must be 1/on/true or 0/off/false, got {raw:?}"
)),
}
}
fn parse_deepseek4_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_DEEPSEEK4_JOINT_DECODE_ROWS);
};
raw.parse::<usize>()
.ok()
.filter(|rows| (2..=MAX_JOINT_DECODE_ROWS).contains(rows))
.ok_or_else(|| {
format!(
"{DEEPSEEK4_JOINT_DECODE_MAX_ROWS_ENV} must be an integer in 2..={MAX_JOINT_DECODE_ROWS}, got {raw:?}"
)
})
}
fn select_deepseek4_joint_decode_rows(
requested: bool,
max_rows: usize,
unavailable: Option<&str>,
) -> Option<usize> {
(requested && unavailable.is_none()).then_some(max_rows)
}
pub(super) fn deepseek4_joint_decode_rows(model: &Deepseek4Model) -> Option<usize> {
let raw = std::env::var(DEEPSEEK4_JOINT_DECODE_ENV).ok();
let requested = parse_deepseek4_joint_decode(raw.as_deref()).unwrap_or_else(|error| {
tracing::warn!(
error,
fallback = DEEPSEEK4_JOINT_DECODE_DEFAULT,
"invalid DeepSeek-V4 joint decode switch"
);
DEEPSEEK4_JOINT_DECODE_DEFAULT
});
let raw_rows = std::env::var(DEEPSEEK4_JOINT_DECODE_MAX_ROWS_ENV).ok();
let max_rows =
parse_deepseek4_joint_decode_max_rows(raw_rows.as_deref()).unwrap_or_else(|error| {
tracing::warn!(
error,
fallback = DEFAULT_DEEPSEEK4_JOINT_DECODE_ROWS,
"invalid DeepSeek-V4 joint decode width"
);
DEFAULT_DEEPSEEK4_JOINT_DECODE_ROWS
});
let unavailable = model.joint_decode_unavailable_reason();
let selected = select_deepseek4_joint_decode_rows(requested, max_rows, unavailable.as_deref());
tracing::info!(
target: "hf2q::serve::api::engine_deepseek4::progress",
requested,
max_rows,
selected = selected.is_some(),
unavailable = unavailable.as_deref().unwrap_or("none"),
"DeepSeek-V4 joint decode configuration"
);
selected
}
pub(super) fn deepseek4_joint_decode_turn_rows(
model: &Deepseek4Model,
worker_rows: Option<usize>,
) -> Option<usize> {
let rows = worker_rows?;
let Some(reason) = model.joint_decode_unavailable_reason() else {
return Some(rows);
};
static REVOKED: Once = Once::new();
REVOKED.call_once(|| {
tracing::warn!(
target: "hf2q::serve::api::engine_deepseek4::progress",
reason = %reason,
"DeepSeek-V4 joint decode revoked after worker start; decoding serially"
);
});
None
}
fn partition_deepseek4_joint_lanes(
handles: &[SlotHandle],
max_rows: usize,
mut eligible: impl FnMut(SlotHandle) -> bool,
) -> Option<(Vec<SlotHandle>, Vec<SlotHandle>)> {
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 joint = Vec::with_capacity(handles.len().min(max_rows));
let mut serial = Vec::new();
for &handle in handles {
if joint.len() < max_rows && eligible(handle) {
joint.push(handle);
} else {
serial.push(handle);
}
}
if joint.len() < 2 {
return None;
}
joint.sort_by_key(|handle| handle.slot_id);
Some((joint, serial))
}
fn deepseek4_joint_lane_eligible(
sessions: &[Deepseek4Session],
slots: &[Option<Deepseek4Slot>],
handle: SlotHandle,
) -> bool {
let slot_index = handle.slot_id.0 as usize;
slots
.get(slot_index)
.and_then(Option::as_ref)
.is_some_and(|(work, reply, installed)| {
*installed == handle
&& matches!(work, Deepseek4SlotWork::Decode { state, .. } if !state.is_finished())
&& !reply.client_closed()
})
&& sessions
.get(slot_index)
.is_some_and(Deepseek4Session::joint_decode_ready)
}
pub(super) enum Deepseek4JointRun {
NotEligible,
Ran {
serial: Vec<SlotHandle>,
},
Fatal(SlotAwareGpuFatal),
}
struct JointLane {
slot_index: usize,
handle: SlotHandle,
state: Deepseek4SlotState,
cold_wave: bool,
reply: SlotReply,
completed_ticks: usize,
tick_error: Option<anyhow::Error>,
client_closed: bool,
}
impl JointLane {
fn active(&self) -> bool {
!self.state.is_finished() && self.tick_error.is_none() && !self.client_closed
}
}
fn reinstall_joint_lanes(slots: &mut [Option<Deepseek4Slot>], lanes: Vec<JointLane>) {
for lane in lanes {
debug_assert!(slots[lane.slot_index].is_none());
slots[lane.slot_index] = Some((
Deepseek4SlotWork::Decode {
state: lane.state,
cold_wave: lane.cold_wave,
},
lane.reply,
lane.handle,
));
}
}
fn joint_lanes_fatal(lanes: Vec<JointLane>, error: anyhow::Error) -> SlotAwareGpuFatal {
let mut owned = lanes.into_iter();
let first = owned.next().expect("joint decode has at least two lanes");
let mut fatal = SlotAwareGpuFatal::slot(first.handle, first.reply, error);
fatal.extend_slots(owned.map(|lane| (Some(lane.handle), lane.reply)));
fatal
}
#[allow(clippy::too_many_arguments)]
pub(super) fn run_deepseek4_joint_decode(
model: &mut Deepseek4LoadedModel,
sessions: &mut [Deepseek4Session],
slots: &mut [Option<Deepseek4Slot>],
last_used: &mut [u64],
clock: &mut u64,
scheduler: &mut InflightBatchedScheduler,
registration: Option<&ModelRegistration>,
handles: &[SlotHandle],
kv_bytes_per_token: u64,
decode_quantum: usize,
max_rows: usize,
cold_runnable_at_start: usize,
supervisor: &EngineSupervisor,
) -> Deepseek4JointRun {
let Some((joint, serial)) = partition_deepseek4_joint_lanes(handles, max_rows, |handle| {
deepseek4_joint_lane_eligible(sessions, slots, handle)
}) else {
return Deepseek4JointRun::NotEligible;
};
let mut lanes = Vec::with_capacity(joint.len());
for handle in joint {
let slot_index = handle.slot_id.0 as usize;
match slots[slot_index].take() {
Some((Deepseek4SlotWork::Decode { state, cold_wave }, reply, installed))
if installed == handle =>
{
lanes.push(JointLane {
slot_index,
handle,
state,
cold_wave,
reply,
completed_ticks: 0,
tick_error: None,
client_closed: false,
});
}
other => {
slots[slot_index] = other;
reinstall_joint_lanes(slots, lanes);
return Deepseek4JointRun::Fatal(SlotAwareGpuFatal::invariant_handle(
handle,
anyhow::anyhow!("DeepSeek-V4 joint decode lane changed before execution"),
));
}
}
}
static FIRST_JOINT_DECODE: Once = Once::new();
FIRST_JOINT_DECODE.call_once(|| {
tracing::info!(
target: "hf2q::serve::api::engine_deepseek4::progress",
lanes = lanes.len(),
"DeepSeek-V4 joint decode engaged"
);
});
let budget = lanes
.iter()
.map(|lane| deepseek4_effective_decode_quantum(decode_quantum, lane.cold_wave))
.min()
.unwrap_or(1);
tracing::debug!(
target: "hf2q::serve::api::engine_deepseek4::progress",
lanes = lanes.len(),
serial = serial.len(),
budget,
"DeepSeek-V4 joint decode turn"
);
for _ in 0..budget {
for lane in lanes.iter_mut().filter(|lane| lane.active()) {
if let SlotReply::Stream { events, cancel, .. } = &lane.reply {
if events.is_closed() {
lane.state.cancelled();
if let Some(counter) = cancel {
counter.fetch_add(1, Ordering::Relaxed);
}
lane.client_closed = true;
}
}
}
let active_lanes = lanes.iter().filter(|lane| lane.active()).count();
let _stage_round =
crate::inference::stage_profile::Round::begin("deepseek4", "joint", active_lanes);
let mut pending = Vec::with_capacity(lanes.len());
for (index, lane) in lanes.iter_mut().enumerate() {
if !lane.active() {
continue;
}
let events = match &lane.reply {
SlotReply::Unary(_) => None,
SlotReply::Stream { events, .. } => Some(events),
};
match lane
.state
.prepare_tick(model, registration, events, supervisor)
{
Ok(Some(token)) => pending.push((index, token)),
Ok(None) => lane.completed_ticks += 1,
Err(error) => lane.tick_error = Some(error),
}
}
let _native_invocation = crate::serve::phase_trace::enabled()
.then(|| joint_turn_transaction_lanes(&pending, |index| lanes[index].handle))
.flatten()
.map(|handles| {
scheduler.trace_transaction("deepseek4", "joint_decode_input", &handles)
});
if let [(index, token)] = pending[..] {
let lane = &mut lanes[index];
sessions[lane.slot_index].swap_with_loaded(model);
let committed = model.commit_generated_token(token, supervisor);
sessions[lane.slot_index].swap_with_loaded(model);
match committed {
Ok(logits) => {
lane.state.install_next_logits(logits);
lane.completed_ticks += 1;
}
Err(error) => lane.tick_error = Some(error),
}
} else if pending.len() > 1 {
let commits = pending
.iter()
.map(|&(index, token)| (lanes[index].slot_index, token))
.collect::<Vec<_>>();
match model.commit_generated_tokens_joint(&commits, sessions, supervisor) {
Ok(logits) => {
for (&(index, _), logits) in pending.iter().zip(logits) {
lanes[index].state.install_next_logits(logits);
lanes[index].completed_ticks += 1;
}
}
Err(error) if is_worker_fatal(supervisor, &error) => {
return Deepseek4JointRun::Fatal(joint_lanes_fatal(lanes, error));
}
Err(error) => {
let message = format!("{error:#}");
let mut error = Some(error);
for &(index, _) in &pending {
lanes[index].tick_error =
Some(error.take().unwrap_or_else(|| anyhow::anyhow!("{message}")));
}
}
}
}
if !lanes.iter().any(JointLane::active) {
break;
}
}
let mut owned = lanes.into_iter();
while let Some(lane) = owned.next() {
if let Some(mut fatal) = settle_deepseek4_decode_lane(
Deepseek4DecodeLaneOutcome {
handle: lane.handle,
reply: lane.reply,
state: lane.state,
cold_wave: lane.cold_wave,
was_parked: false,
deferred_decode_ticks: 0,
completed_ticks: lane.completed_ticks,
tick_error: lane.tick_error,
client_closed: lane.client_closed,
},
sessions,
slots,
last_used,
clock,
scheduler,
registration,
kv_bytes_per_token,
cold_runnable_at_start,
supervisor,
) {
fatal.extend_slots(owned.map(|lane| (Some(lane.handle), lane.reply)));
return Deepseek4JointRun::Fatal(fatal);
}
}
Deepseek4JointRun::Ran { serial }
}
fn joint_turn_transaction_lanes(
pending: &[(usize, u32)],
handle_of: impl Fn(usize) -> SlotHandle,
) -> Option<Vec<SlotHandle>> {
(!pending.is_empty()).then(|| pending.iter().map(|&(index, _)| handle_of(index)).collect())
}
#[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_defaults_on_and_parses_explicit_arms() {
assert_eq!(
parse_deepseek4_joint_decode(None),
Ok(DEEPSEEK4_JOINT_DECODE_DEFAULT)
);
assert_eq!(
parse_deepseek4_joint_decode(Some(" ")),
Ok(DEEPSEEK4_JOINT_DECODE_DEFAULT)
);
assert!(DEEPSEEK4_JOINT_DECODE_DEFAULT);
for raw in ["1", "on", "TRUE"] {
assert_eq!(parse_deepseek4_joint_decode(Some(raw)), Ok(true));
}
for raw in ["0", "Off", "false"] {
assert_eq!(parse_deepseek4_joint_decode(Some(raw)), Ok(false));
}
assert!(parse_deepseek4_joint_decode(Some("joint")).is_err());
}
#[test]
fn joint_width_defaults_to_four_and_wider_steps_need_an_explicit_cap() {
assert_eq!(parse_deepseek4_joint_decode_max_rows(None), Ok(4));
assert_eq!(parse_deepseek4_joint_decode_max_rows(Some(" ")), Ok(4));
assert_eq!(DEFAULT_DEEPSEEK4_JOINT_DECODE_ROWS, 4);
for rows in 2..=MAX_JOINT_DECODE_ROWS {
assert_eq!(
parse_deepseek4_joint_decode_max_rows(Some(&rows.to_string())),
Ok(rows)
);
}
for raw in ["0", "1", "9", "four"] {
assert!(parse_deepseek4_joint_decode_max_rows(Some(raw)).is_err());
}
}
#[test]
fn unproven_artifacts_and_disabled_switch_keep_serial_decode() {
assert_eq!(select_deepseek4_joint_decode_rows(true, 4, None), Some(4));
assert_eq!(
select_deepseek4_joint_decode_rows(
true,
4,
Some("output.weight is F32, which has no proven-exact joint OutputHead route"),
),
None
);
assert_eq!(select_deepseek4_joint_decode_rows(false, 4, None), None);
}
#[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)];
let (joint, serial) =
partition_deepseek4_joint_lanes(&handles, MAX_JOINT_DECODE_ROWS, |handle| {
handle.slot_id != SlotId(2)
})
.expect("three eligible lanes form a joint step");
assert_eq!(joint, [handle(0, 1), handle(1, 7), handle(3, 9)]);
assert_eq!(serial, [handle(2, 4)]);
}
#[test]
fn fewer_than_two_eligible_lanes_keep_the_whole_turn_serial() {
let handles = [handle(0, 1), handle(1, 1)];
assert!(
partition_deepseek4_joint_lanes(&handles, MAX_JOINT_DECODE_ROWS, |handle| {
handle.slot_id == SlotId(0)
})
.is_none()
);
assert!(partition_deepseek4_joint_lanes(&[], MAX_JOINT_DECODE_ROWS, |_| true).is_none());
assert!(partition_deepseek4_joint_lanes(&handles, 1, |_| true).is_none());
}
#[test]
fn duplicate_slots_never_form_a_joint_step() {
let handles = [handle(0, 1), handle(1, 1), handle(0, 2)];
assert!(
partition_deepseek4_joint_lanes(&handles, MAX_JOINT_DECODE_ROWS, |_| true).is_none()
);
}
#[test]
fn joint_width_is_capped_and_overflow_lanes_stay_serial() {
let handles = [handle(4, 1), handle(3, 1), handle(2, 1), handle(1, 1)];
let (joint, serial) =
partition_deepseek4_joint_lanes(&handles, 3, |_| true).expect("capped joint step");
assert_eq!(joint, [handle(2, 1), handle(3, 1), handle(4, 1)]);
assert_eq!(serial, [handle(1, 1)]);
}
#[test]
fn a_joint_turn_without_pending_lanes_opens_no_transaction() {
let lanes = [handle(0, 1), handle(2, 5), handle(3, 7)];
assert_eq!(
joint_turn_transaction_lanes(&[], |index| lanes[index]),
None
);
assert_eq!(
joint_turn_transaction_lanes(&[(2, 11), (0, 17)], |index| lanes[index]),
Some(vec![handle(3, 7), handle(0, 1)])
);
}
}