dynamo-mocker 1.4.0

Mock LLM scheduler and KV manager for testing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use std::sync::Arc;

use anyhow::{Result, bail};
use tokio::sync::{Notify, Semaphore, mpsc, watch};
use tokio::task::JoinSet;
use tokio::time::Instant;
use tokio_util::sync::CancellationToken;

use crate::common::protocols::{DirectRequest, FpmPublisher};
use crate::live::{LiveEngine, LiveEngineOptions, ObservedAdmission};
use crate::loadgen::WorkloadDriver;
use crate::replay::TraceSimulationReport;

use super::ReplayRouter;
use super::entrypoints::OnlineReplayConfig;
use super::recorder::{OnlineRecorderOptions, OnlineTraceRecorder, forward_admissions};
use super::state::{
    LiveReplayMode, LiveRuntimeStats, SharedLiveRuntimeStats, WorkloadDispatchState, arrival_event,
    now_ms,
};
use super::task::{
    InFlightGuard, RequestTaskContext, run_request_task, wait_for_workload_progress,
};

pub(super) struct LiveRuntime {
    pending: std::collections::VecDeque<DirectRequest>,
    engines: Arc<[LiveEngine]>,
    admission_rx: mpsc::UnboundedReceiver<ObservedAdmission>,
    start: Instant,
    mode: LiveReplayMode,
    router: Arc<ReplayRouter>,
    recorder_options: OnlineRecorderOptions,
    cancel: CancellationToken,
}

struct LiveRunSession {
    task_ctx: RequestTaskContext,
    tasks: JoinSet<Result<()>>,
    recorder_tx: super::recorder::RecorderSender,
    recorder: OnlineTraceRecorder,
    admission_task: tokio::task::JoinHandle<Result<()>>,
}

impl LiveRunSession {
    fn new(
        engines: Arc<[LiveEngine]>,
        router: Arc<ReplayRouter>,
        admission_rx: mpsc::UnboundedReceiver<ObservedAdmission>,
        start: Instant,
        workload: Option<Arc<WorkloadDispatchState>>,
        recorder_options: OnlineRecorderOptions,
        cancel: CancellationToken,
    ) -> Self {
        let stats = Arc::new(SharedLiveRuntimeStats::default());
        let recorder = OnlineTraceRecorder::start(recorder_options);
        let recorder_tx = recorder.sender();
        let admission_task =
            tokio::spawn(forward_admissions(start, admission_rx, recorder.sender()));
        let task_ctx = RequestTaskContext {
            engines,
            router,
            recorder: recorder_tx.clone(),
            stats,
            workload,
            cancel,
            start,
        };
        Self {
            task_ctx,
            tasks: JoinSet::new(),
            recorder_tx,
            recorder,
            admission_task,
        }
    }

    async fn finish(mut self) -> Result<(TraceSimulationReport, LiveRuntimeStats)> {
        while !self.tasks.is_empty() {
            tokio::select! {
                biased;
                _ = self.task_ctx.cancel.cancelled() => bail!("online replay cancelled"),
                joined = self.tasks.join_next() => {
                    if let Some(joined) = joined {
                        joined??;
                    }
                }
            }
        }
        if self.task_ctx.cancel.is_cancelled() {
            bail!("online replay cancelled");
        }

        let LiveRunSession {
            task_ctx,
            recorder_tx,
            recorder,
            admission_task,
            ..
        } = self;
        let wall_time_ms = now_ms(task_ctx.start);
        let vllm_preemptions_total = task_ctx
            .engines
            .iter()
            .map(|engine| engine.metrics_receiver().borrow().vllm_preemptions_total)
            .sum();
        let stats_snapshot = task_ctx.stats.snapshot(vllm_preemptions_total);
        let engines = Arc::clone(&task_ctx.engines);
        let router = Arc::clone(&task_ctx.router);
        let cancel = task_ctx.cancel.clone();
        drop(task_ctx);
        drop(recorder_tx);

        for engine in engines.iter() {
            tokio::select! {
                biased;
                _ = cancel.cancelled() => bail!("online replay cancelled"),
                result = engine.shutdown() => result?,
            }
        }
        tokio::select! {
            biased;
            _ = cancel.cancelled() => bail!("online replay cancelled"),
            result = admission_task => result??,
        }
        tokio::select! {
            biased;
            _ = cancel.cancelled() => bail!("online replay cancelled"),
            result = router.shutdown() => result?,
        }
        let report = tokio::select! {
            biased;
            _ = cancel.cancelled() => bail!("online replay cancelled"),
            result = recorder.finish(wall_time_ms) => result?,
        };
        if cancel.is_cancelled() {
            bail!("online replay cancelled");
        }
        Ok((report, stats_snapshot))
    }
}

impl LiveRuntime {
    /// Build the shared router and one request-scoped live engine per replay worker.
    pub(super) fn new(
        config: OnlineReplayConfig,
        pending: std::collections::VecDeque<DirectRequest>,
        mode: LiveReplayMode,
        cancel: CancellationToken,
    ) -> Result<Self> {
        Self::new_inner(config, pending, mode, None, cancel)
    }

    #[cfg(test)]
    pub(super) fn new_with_output_gate(
        config: OnlineReplayConfig,
        pending: std::collections::VecDeque<DirectRequest>,
        mode: LiveReplayMode,
        output_gate: watch::Receiver<bool>,
        cancel: CancellationToken,
    ) -> Result<Self> {
        Self::new_inner(config, pending, mode, Some(output_gate), cancel)
    }

    fn new_inner(
        config: OnlineReplayConfig,
        pending: std::collections::VecDeque<DirectRequest>,
        mode: LiveReplayMode,
        output_gate: Option<watch::Receiver<bool>>,
        cancel: CancellationToken,
    ) -> Result<Self> {
        let OnlineReplayConfig {
            args,
            router_config,
            prefill_load_estimator,
            num_workers,
            router_mode,
            options: replay_options,
        } = config;
        let recorder_options = OnlineRecorderOptions {
            capture_per_request: replay_options.record_per_request,
            sla: replay_options.sla,
            num_workers,
            gpus_per_worker: args.aic_gpus_per_worker(),
        };
        let (admission_tx, admission_rx) = mpsc::unbounded_channel();
        let router = Arc::new(ReplayRouter::new(
            router_mode,
            &args,
            router_config,
            prefill_load_estimator,
            num_workers,
        )?);
        let mut engines = Vec::with_capacity(num_workers);
        for worker_idx in 0..num_workers {
            let options = LiveEngineOptions {
                kv_event_publishers: router.sink(worker_idx as _),
                admission_tx: Some(admission_tx.clone()),
                fpm_publisher: FpmPublisher::default(),
                request_output_capacity: None,
                allow_zero_output: true,
            };
            let engine = match output_gate.as_ref() {
                Some(gate) => LiveEngine::start_with_options_and_output_gate(
                    args.clone(),
                    0,
                    options,
                    gate.clone(),
                )?,
                None => LiveEngine::start_with_options(args.clone(), 0, options)?,
            };
            engines.push(engine);
        }
        drop(admission_tx);

        Ok(Self {
            pending,
            engines: Arc::from(engines),
            admission_rx,
            start: Instant::now(),
            mode,
            router,
            recorder_options,
            cancel,
        })
    }

    #[cfg(test)]
    pub(super) fn engines(&self) -> Arc<[LiveEngine]> {
        Arc::clone(&self.engines)
    }

    #[cfg(test)]
    pub(super) fn router(&self) -> Arc<ReplayRouter> {
        Arc::clone(&self.router)
    }

    /// Replay a finite queue of requests and return the final trace report plus debug stats.
    pub(super) async fn run(self) -> Result<(TraceSimulationReport, LiveRuntimeStats)> {
        let LiveRuntime {
            mut pending,
            engines,
            admission_rx,
            start,
            mode,
            router,
            recorder_options,
            cancel,
        } = self;
        let mut session = LiveRunSession::new(
            engines,
            router,
            admission_rx,
            start,
            None,
            recorder_options,
            cancel,
        );

        match mode {
            LiveReplayMode::Trace => {
                while let Some(request) = pending.pop_front() {
                    let arrival_ms = request.arrival_timestamp_ms.unwrap_or(0.0);
                    let deadline =
                        start + tokio::time::Duration::from_secs_f64(arrival_ms / 1000.0);
                    loop {
                        tokio::select! {
                            biased;
                            _ = session.task_ctx.cancel.cancelled() => {
                                bail!("online replay cancelled");
                            }
                            joined = session.tasks.join_next(), if !session.tasks.is_empty() => {
                                if let Some(joined) = joined {
                                    joined??;
                                }
                            }
                            _ = tokio::time::sleep_until(deadline) => break,
                        }
                    }
                    if session.task_ctx.cancel.is_cancelled() {
                        bail!("online replay cancelled");
                    }
                    let arrival = arrival_event(&request, arrival_ms)?;
                    session.recorder_tx.record_arrival(arrival)?;
                    if session.task_ctx.cancel.is_cancelled() {
                        bail!("online replay cancelled");
                    }
                    session
                        .tasks
                        .spawn(run_request_task(session.task_ctx.clone(), request, None));
                }
            }
            LiveReplayMode::Concurrency { max_in_flight } => {
                let semaphore = Arc::new(Semaphore::new(max_in_flight));
                while let Some(request) = pending.pop_front() {
                    let acquire = semaphore.clone().acquire_owned();
                    tokio::pin!(acquire);
                    let permit = loop {
                        tokio::select! {
                            biased;
                            _ = session.task_ctx.cancel.cancelled() => {
                                bail!("online replay cancelled");
                            }
                            joined = session.tasks.join_next(), if !session.tasks.is_empty() => {
                                if let Some(joined) = joined {
                                    joined??;
                                }
                            }
                            permit = &mut acquire => {
                                break permit?;
                            }
                        }
                    };
                    let arrival = arrival_event(&request, now_ms(start))?;
                    session.recorder_tx.record_arrival(arrival)?;
                    if session.task_ctx.cancel.is_cancelled() {
                        bail!("online replay cancelled");
                    }
                    let task_ctx = session.task_ctx.clone();
                    session.tasks.spawn(async move {
                        let _permit = permit;
                        run_request_task(task_ctx, request, None).await
                    });
                }
            }
        }

        session.finish().await
    }

    /// Drive a multi-turn workload driver until it is drained and all spawned request tasks finish.
    pub(super) async fn run_workload(
        self,
        driver: WorkloadDriver,
        _total_turns: usize,
    ) -> Result<(TraceSimulationReport, LiveRuntimeStats)> {
        let LiveRuntime {
            engines,
            admission_rx,
            start,
            mode,
            router,
            recorder_options,
            cancel,
            ..
        } = self;
        let cap_enabled = matches!(mode, LiveReplayMode::Concurrency { .. });
        let workload = Arc::new(WorkloadDispatchState {
            driver: std::sync::Mutex::new(driver),
            wakeup: Notify::new(),
            start,
        });
        let mut session = LiveRunSession::new(
            engines,
            router,
            admission_rx,
            start,
            Some(Arc::clone(&workload)),
            recorder_options,
            cancel,
        );

        loop {
            if session.task_ctx.cancel.is_cancelled() {
                bail!("online replay cancelled");
            }
            while let Some(joined) = session.tasks.try_join_next() {
                joined??;
            }

            let now = now_ms(start);
            let ready_turns =
                tokio::task::block_in_place(|| workload.driver.lock().unwrap().pop_ready(now, 1));
            if let Some(ready_turn) = ready_turns.into_iter().next() {
                let guard = cap_enabled
                    .then(|| InFlightGuard::new(Arc::clone(&workload), ready_turn.request_uuid));
                let arrival_at_ms = match mode {
                    LiveReplayMode::Trace => ready_turn.scheduled_ready_at_ms,
                    LiveReplayMode::Concurrency { .. } => now_ms(start),
                };
                let arrival = arrival_event(&ready_turn.request, arrival_at_ms)?;
                session.recorder_tx.record_arrival(arrival)?;
                if ready_turn.emit_session_metadata {
                    session.recorder_tx.record_session_metadata(
                        ready_turn.request_uuid,
                        ready_turn.session_id,
                        ready_turn.turn_index,
                    )?;
                }
                if session.task_ctx.cancel.is_cancelled() {
                    bail!("online replay cancelled");
                }
                session.tasks.spawn(run_request_task(
                    session.task_ctx.clone(),
                    ready_turn.request,
                    guard,
                ));
                tokio::task::yield_now().await;
                continue;
            }

            let wake = workload.wakeup.notified();
            tokio::pin!(wake);
            let (is_drained, next_ready_ms) = {
                let mut driver = workload.driver.lock().unwrap();
                (driver.is_drained(), driver.next_ready_time_ms())
            };
            if is_drained {
                break;
            }

            tokio::select! {
                biased;
                _ = session.task_ctx.cancel.cancelled() => {
                    bail!("online replay cancelled");
                }
                joined = session.tasks.join_next(), if !session.tasks.is_empty() => {
                    if let Some(joined) = joined {
                        joined??;
                    }
                }
                _ = wait_for_workload_progress(next_ready_ms, start, wake.as_mut()) => {}
            }
        }

        session.finish().await
    }
}