use super::*;
use crate::api::cuda_batch_scheduler::spawn_cuda_batch_scheduler;
const MODEL_PATH: &str = "/home/noah/models/Qwen3.5-0.8B-Q4_K_M.gguf";
const STEPS: usize = 8;
const PROMPTS: [&[u32]; 4] = [
&[9707],
&[785, 6722, 315, 9625],
&[22043, 374, 264, 3974, 11, 1879, 13],
&[16, 10],
];
type Reply = (Vec<u32>, Vec<StepLogprobs>);
fn greedy() -> QuantizedGenerateConfig {
QuantizedGenerateConfig {
max_tokens: STEPS,
temperature: 0.0,
top_k: 1,
stop_tokens: Vec::new(),
..QuantizedGenerateConfig::default()
}
}
fn band(
tx: &tokio::sync::mpsc::Sender<CudaBatchRequest>,
prompts: &[&[u32]],
top_n: Option<usize>,
) -> Vec<Reply> {
band_with(tx, prompts, top_n, &greedy())
}
fn band_with(
tx: &tokio::sync::mpsc::Sender<CudaBatchRequest>,
prompts: &[&[u32]],
top_n: Option<usize>,
config: &QuantizedGenerateConfig,
) -> Vec<Reply> {
let receivers: Vec<_> = prompts
.iter()
.map(|p| {
let (token_tx, rx) = tokio::sync::mpsc::channel(64);
let (records, records_rx) = tokio::sync::mpsc::unbounded_channel();
let config = config.clone();
tx.try_send(CudaBatchRequest {
prompt_ids: p.to_vec(),
config,
token_tx,
non_streaming: false,
enqueue_time: std::time::Instant::now(),
timing_tx: None,
logprobs: top_n.map(|top_n| RecordSink { top_n, records }),
})
.unwrap_or_else(|e| panic!("queue {p:?}: {e}"));
(rx, records_rx)
})
.collect();
receivers
.into_iter()
.map(|(mut rx, mut records_rx)| {
let mut got = Vec::new();
while let Some(t) = rx.blocking_recv() {
got.push(t.expect("no error"));
}
let mut steps = Vec::new();
while let Ok(step) = records_rx.try_recv() {
steps.push(step);
}
(got, steps)
})
.collect()
}
struct Scheduler {
tx: Option<tokio::sync::mpsc::Sender<CudaBatchRequest>>,
counter: Arc<crate::api::InFlightCounter>,
served: std::sync::Weak<Qwen35Served>,
engine: std::sync::Weak<std::sync::RwLock<Qwen35BatchEngine>>,
}
impl Scheduler {
fn tx(&self) -> &tokio::sync::mpsc::Sender<CudaBatchRequest> {
self.tx.as_ref().expect("held until drop")
}
fn reused(&self) -> u64 {
let engine = self
.engine
.upgrade()
.expect("the scheduler holds its engine");
let rows = engine.read().expect("the engine lock").reused_rows();
rows
}
}
impl Drop for Scheduler {
fn drop(&mut self) {
drop(self.tx.take());
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
while self.served.strong_count() > 0 && std::time::Instant::now() < deadline {
std::thread::sleep(std::time::Duration::from_millis(20));
}
}
}
fn scheduler() -> Option<Scheduler> {
if !std::path::Path::new(MODEL_PATH).exists() {
eprintln!("SKIP: {MODEL_PATH} is absent");
return None;
}
if crate::cuda::CudaExecutor::new(0).is_err() {
eprintln!("SKIP: no CUDA device");
return None;
}
let mapped = crate::gguf::MappedGGUFModel::from_path(MODEL_PATH).expect("map the GGUF");
let session = Qwen35Session::load_cached(std::path::Path::new(MODEL_PATH), &mapped, false)
.expect("load the session");
assert!(
session.on_gpu(),
"the session must hold the GPU for this test"
);
let served = Arc::new(Qwen35Served {
context_length: session.context_length(),
session: std::sync::Mutex::new(session),
on_gpu: std::sync::atomic::AtomicBool::new(true),
});
let engine = Arc::new(std::sync::RwLock::new(Qwen35BatchEngine::new(Arc::clone(
&served,
))));
let counter = crate::api::InFlightCounter::new();
let config = CudaBatchConfig {
max_batch: 4,
window_ms: 200,
};
Some(Scheduler {
tx: Some(spawn_cuda_batch_scheduler(
Arc::clone(&engine),
config,
counter.clone(),
)),
counter,
served: Arc::downgrade(&served),
engine: Arc::downgrade(&engine),
})
}
fn solo(tx: &tokio::sync::mpsc::Sender<CudaBatchRequest>, top_n: Option<usize>) -> Vec<Reply> {
PROMPTS
.iter()
.map(|p| band(tx, &[p], top_n).remove(0))
.collect()
}
#[test]
#[serial_test::serial]
fn four_concurrent_requests_batch_and_match_their_solo_turns() {
let Some(s) = scheduler() else {
return;
};
let alone: Vec<Vec<u32>> = solo(s.tx(), None).into_iter().map(|r| r.0).collect();
assert!(
s.counter.peak_in_flight() <= 1,
"a lone request is never batched"
);
assert!(
alone.iter().all(|s| s.len() == STEPS),
"each solo turn decodes its whole budget: {alone:?}"
);
assert!(
alone.windows(2).any(|w| w[0] != w[1]),
"the solo streams are all equal, so they cannot tell slots apart: {alone:?}"
);
let together = band(s.tx(), &PROMPTS, None);
assert_eq!(
s.counter.peak_in_flight(),
4,
"the band of 4 formed one batch of m = 4"
);
for (i, (got, _)) in together.iter().enumerate() {
assert_eq!(got, &alone[i], "request {i} diverged from its solo turn");
}
eprintln!("[#5293] m=4: 4 requests x {STEPS} tokens equal their solo turns");
}
#[test]
#[serial_test::serial]
fn a_band_extending_an_earlier_one_resumes_its_prefixes_and_matches_solo() {
let Some(s) = scheduler() else {
return;
};
let longer: Vec<Vec<u32>> = PROMPTS
.iter()
.map(|p| [p, &[11, 13][..]].concat())
.collect();
let longer: Vec<&[u32]> = longer.iter().map(Vec::as_slice).collect();
let alone: Vec<Vec<u32>> = longer
.iter()
.map(|p| band(s.tx(), &[p], None).remove(0).0)
.collect();
assert_eq!(s.reused(), 0, "a lone request is the session's turn");
let _ = band(s.tx(), &PROMPTS, None);
assert_eq!(s.reused(), 0, "the first band has nothing to resume");
let together = band(s.tx(), &longer, None);
assert_eq!(s.counter.peak_in_flight(), 4, "both bands formed m = 4");
assert_eq!(
s.reused(),
3 + 6 + 1,
"each slot resumed its earlier prompt's head"
);
for (i, (got, _)) in together.iter().enumerate() {
assert_eq!(got, &alone[i], "request {i} diverged from its solo turn");
}
eprintln!("[#5279] P7: 10 shared rows resumed, 4 requests equal their solo turns");
}
const LOGPROB_TOLERANCE: f32 = 1e-2;
#[test]
#[serial_test::serial]
fn logprobs_requests_batch_and_record_what_their_solo_turns_record() {
let Some(s) = scheduler() else {
return;
};
let alone = solo(s.tx(), Some(3));
assert!(
alone
.iter()
.all(|(t, r)| t.len() == STEPS && r.len() == STEPS),
"each solo turn decodes and records its whole budget"
);
let together = band(s.tx(), &PROMPTS, Some(3));
assert_eq!(
s.counter.peak_in_flight(),
4,
"the band of 4 asking for logprobs formed one batch of m = 4"
);
let mut drift = 0.0_f32;
for (i, (tokens, records)) in together.iter().enumerate() {
assert_eq!(tokens, &alone[i].0, "request {i}'s tokens diverged");
assert_eq!(
records.len(),
alone[i].1.len(),
"request {i}'s record count"
);
for (got, want) in records.iter().zip(&alone[i].1) {
let ids = |r: &StepLogprobs| r.top.iter().map(|t| t.token_id).collect::<Vec<_>>();
assert_eq!(
(got.step, got.chosen, ids(got)),
(want.step, want.chosen, ids(want)),
"request {i}'s record diverged"
);
let pairs = std::iter::once((got.chosen_logprob, want.chosen_logprob)).chain(
got.top
.iter()
.zip(&want.top)
.map(|(g, w)| (g.logprob, w.logprob)),
);
for (g, w) in pairs {
drift = drift.max((g - w).abs());
}
}
}
assert!(
drift <= LOGPROB_TOLERANCE,
"a batched logprob sits {drift} from its solo turn's"
);
eprintln!("[#5293] m=4 with logprobs: tokens equal their solo turns, logprob drift {drift:e}");
}
#[test]
fn greedy_is_temperature_zero_or_top_k_one() {
let at = |temperature, top_k| QuantizedGenerateConfig {
temperature,
top_k,
..QuantizedGenerateConfig::default()
};
assert!(is_greedy(&at(0.0, 40)));
assert!(is_greedy(&at(0.8, 1)));
assert!(!is_greedy(&at(0.8, 40)));
}
#[test]
#[serial_test::serial]
fn sampled_requests_batch_and_draw_what_their_seeded_solo_turns_draw() {
let Some(s) = scheduler() else {
return;
};
let sampled = |seed| QuantizedGenerateConfig {
temperature: 0.8,
top_k: 40,
seed,
..greedy()
};
let solo_at = |seed| -> Vec<Vec<u32>> {
PROMPTS
.iter()
.map(|p| band_with(s.tx(), &[p], None, &sampled(seed)).remove(0).0)
.collect()
};
let alone = solo_at(7);
assert!(
alone.iter().all(|t| t.len() == STEPS),
"each solo turn draws its whole budget: {alone:?}"
);
assert_ne!(alone, solo_at(8), "a second seed draws other tokens");
let together = band_with(s.tx(), &PROMPTS, None, &sampled(7));
assert_eq!(
s.counter.peak_in_flight(),
4,
"the sampled band of 4 formed one batch of m = 4"
);
for (i, (got, _)) in together.iter().enumerate() {
assert_eq!(got, &alone[i], "request {i} drew other tokens than alone");
}
eprintln!("[#5312] m=4 sampled: 4 requests x {STEPS} tokens equal their seeded solo turns");
}