entrypoints.rs 19.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use std::path::Path;
use std::time::Instant;

use anyhow::{Result, bail};
use dynamo_kv_router::config::KvRouterConfig;

use super::online;
use super::validate::{
12
13
    validate_offline_concurrency_args, validate_offline_disagg_concurrency_args,
    validate_offline_disagg_replay_args, validate_offline_replay_args,
14
15
    validate_online_concurrency_args, validate_online_replay_args,
};
16
use super::{
17
18
    OfflineDisaggReplayConfig, ReplayPrefillLoadEstimator, ReplayRouterMode, ReplayWorkerArtifacts,
    TraceSimulationReport,
19
};
20
use crate::common::protocols::{DirectRequest, MockEngineArgs};
21
use crate::loadgen::Trace;
22

23
24
25
26
27
28
29
30
pub fn generate_trace_worker_artifacts_offline(
    args: MockEngineArgs,
    trace: Trace,
) -> Result<ReplayWorkerArtifacts> {
    let args = args.normalized()?;
    crate::replay::offline::generate_trace_worker_artifacts(args, trace)
}

31
32
33
pub fn simulate_trace_file(
    args: MockEngineArgs,
    trace_path: &Path,
34
    trace_block_size: usize,
35
36
37
38
39
40
    num_workers: usize,
    arrival_speedup_ratio: f64,
) -> Result<TraceSimulationReport> {
    simulate_trace_file_with_router_mode(
        args,
        None,
41
        None,
42
        trace_path,
43
        trace_block_size,
44
45
46
47
48
49
        num_workers,
        arrival_speedup_ratio,
        ReplayRouterMode::RoundRobin,
    )
}

50
#[allow(clippy::too_many_arguments)]
51
52
53
pub fn simulate_trace_file_with_router_mode(
    args: MockEngineArgs,
    router_config: Option<KvRouterConfig>,
54
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
55
    trace_path: &Path,
56
    trace_block_size: usize,
57
58
59
60
61
62
    num_workers: usize,
    arrival_speedup_ratio: f64,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let args = args.normalized()?;
    validate_offline_replay_args(&args, num_workers, router_mode)?;
63
    let trace = Trace::from_mooncake(trace_path, trace_block_size)?
64
65
        .normalize_session_starts()?
        .speed_up_timing(arrival_speedup_ratio)?;
66
    let started_at = Instant::now();
67
    let report = crate::replay::offline::simulate_trace_workload(
68
69
        args,
        router_config,
70
        prefill_load_estimator,
71
        trace,
72
73
74
75
76
77
        num_workers,
        router_mode,
    )?;
    Ok(report.with_wall_time_ms(started_at.elapsed().as_secs_f64() * 1000.0))
}

78
79
80
pub fn simulate_trace_file_disagg_with_router_mode(
    config: OfflineDisaggReplayConfig,
    router_config: Option<KvRouterConfig>,
81
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
82
    trace_path: &Path,
83
    trace_block_size: usize,
84
85
86
87
88
    arrival_speedup_ratio: f64,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let config = config.normalized()?;
    validate_offline_disagg_replay_args(&config, router_mode)?;
89
    let trace = Trace::from_mooncake(trace_path, trace_block_size)?
90
91
92
93
94
95
        .normalize_session_starts()?
        .speed_up_timing(arrival_speedup_ratio)?;
    let started_at = Instant::now();
    let report = crate::replay::offline::simulate_trace_workload_disagg(
        config,
        router_config,
96
        prefill_load_estimator,
97
98
99
100
101
102
        trace,
        router_mode,
    )?;
    Ok(report.with_wall_time_ms(started_at.elapsed().as_secs_f64() * 1000.0))
}

103
104
105
pub fn simulate_trace_live_file(
    args: MockEngineArgs,
    trace_path: &Path,
106
    trace_block_size: usize,
107
108
109
110
111
112
    num_workers: usize,
    arrival_speedup_ratio: f64,
) -> Result<TraceSimulationReport> {
    simulate_trace_live_file_with_router_mode(
        args,
        None,
113
        None,
114
        trace_path,
115
        trace_block_size,
116
117
118
119
120
121
        num_workers,
        arrival_speedup_ratio,
        ReplayRouterMode::RoundRobin,
    )
}

122
#[allow(clippy::too_many_arguments)]
123
124
125
pub fn simulate_trace_live_file_with_router_mode(
    args: MockEngineArgs,
    router_config: Option<KvRouterConfig>,
126
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
127
    trace_path: &Path,
128
    trace_block_size: usize,
129
130
131
132
133
134
    num_workers: usize,
    arrival_speedup_ratio: f64,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let args = args.normalized()?;
    validate_online_replay_args(&args, num_workers)?;
135
    let trace = Trace::from_mooncake(trace_path, trace_block_size)?
136
137
        .normalize_session_starts()?
        .speed_up_timing(arrival_speedup_ratio)?;
138
139
140
141
142
143
144
145
    online::simulate_trace_workload(
        args,
        router_config,
        prefill_load_estimator,
        trace,
        num_workers,
        router_mode,
    )
146
147
148
149
150
151
152
153
154
155
156
}

pub fn simulate_trace_requests(
    args: MockEngineArgs,
    requests: Vec<DirectRequest>,
    num_workers: usize,
    arrival_speedup_ratio: f64,
) -> Result<TraceSimulationReport> {
    simulate_trace_requests_with_router_mode(
        args,
        None,
157
        None,
158
159
160
161
162
163
164
165
166
167
        requests,
        num_workers,
        arrival_speedup_ratio,
        ReplayRouterMode::RoundRobin,
    )
}

pub fn simulate_trace_requests_with_router_mode(
    args: MockEngineArgs,
    router_config: Option<KvRouterConfig>,
168
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    requests: Vec<DirectRequest>,
    num_workers: usize,
    arrival_speedup_ratio: f64,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let args = args.normalized()?;
    validate_offline_replay_args(&args, num_workers, router_mode)?;
    if requests.is_empty() {
        bail!("trace replay requires at least one request");
    }

    let started_at = Instant::now();
    let report = crate::replay::offline::simulate_trace(
        args,
        router_config,
184
        prefill_load_estimator,
185
186
187
188
189
190
191
192
        requests,
        num_workers,
        arrival_speedup_ratio,
        router_mode,
    )?;
    Ok(report.with_wall_time_ms(started_at.elapsed().as_secs_f64() * 1000.0))
}

193
194
195
pub fn simulate_trace_requests_disagg_with_router_mode(
    config: OfflineDisaggReplayConfig,
    router_config: Option<KvRouterConfig>,
196
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
197
198
199
200
201
202
203
204
205
206
207
208
209
210
    requests: Vec<DirectRequest>,
    arrival_speedup_ratio: f64,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let config = config.normalized()?;
    validate_offline_disagg_replay_args(&config, router_mode)?;
    if requests.is_empty() {
        bail!("trace replay requires at least one request");
    }

    let started_at = Instant::now();
    let report = crate::replay::offline::simulate_trace_disagg(
        config,
        router_config,
211
        prefill_load_estimator,
212
213
214
215
216
217
218
        requests,
        arrival_speedup_ratio,
        router_mode,
    )?;
    Ok(report.with_wall_time_ms(started_at.elapsed().as_secs_f64() * 1000.0))
}

219
220
221
222
223
224
225
226
227
pub fn simulate_trace_live_requests(
    args: MockEngineArgs,
    requests: Vec<DirectRequest>,
    num_workers: usize,
    arrival_speedup_ratio: f64,
) -> Result<TraceSimulationReport> {
    simulate_trace_live_requests_with_router_mode(
        args,
        None,
228
        None,
229
230
231
232
233
234
235
236
237
238
        requests,
        num_workers,
        arrival_speedup_ratio,
        ReplayRouterMode::RoundRobin,
    )
}

pub fn simulate_trace_live_requests_with_router_mode(
    args: MockEngineArgs,
    router_config: Option<KvRouterConfig>,
239
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
240
241
242
243
244
245
246
247
248
249
250
251
252
253
    requests: Vec<DirectRequest>,
    num_workers: usize,
    arrival_speedup_ratio: f64,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let args = args.normalized()?;
    validate_online_replay_args(&args, num_workers)?;
    if requests.is_empty() {
        bail!("trace replay requires at least one request");
    }

    online::simulate_trace_requests(
        args,
        router_config,
254
        prefill_load_estimator,
255
256
257
258
259
260
261
262
263
264
        requests,
        num_workers,
        arrival_speedup_ratio,
        router_mode,
    )
}

pub fn simulate_concurrency_file(
    args: MockEngineArgs,
    trace_path: &Path,
265
    trace_block_size: usize,
266
267
268
269
270
271
    max_in_flight: usize,
    num_workers: usize,
) -> Result<TraceSimulationReport> {
    simulate_concurrency_file_with_router_mode(
        args,
        None,
272
        None,
273
        trace_path,
274
        trace_block_size,
275
276
277
278
279
280
        max_in_flight,
        num_workers,
        ReplayRouterMode::RoundRobin,
    )
}

281
#[allow(clippy::too_many_arguments)]
282
283
284
pub fn simulate_concurrency_file_with_router_mode(
    args: MockEngineArgs,
    router_config: Option<KvRouterConfig>,
285
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
286
    trace_path: &Path,
287
    trace_block_size: usize,
288
289
290
291
292
    max_in_flight: usize,
    num_workers: usize,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let args = args.normalized()?;
293
    validate_offline_concurrency_args(&args, num_workers, max_in_flight, router_mode)?;
294
    let trace = Trace::from_mooncake(trace_path, trace_block_size)?;
295
    let started_at = Instant::now();
296
    let report = simulate_concurrency_workload_with_router_mode(
297
298
        args,
        router_config,
299
        prefill_load_estimator,
300
        trace,
301
302
303
304
305
306
307
        max_in_flight,
        num_workers,
        router_mode,
    )?;
    Ok(report.with_wall_time_ms(started_at.elapsed().as_secs_f64() * 1000.0))
}

308
309
310
pub fn simulate_concurrency_file_disagg_with_router_mode(
    config: OfflineDisaggReplayConfig,
    router_config: Option<KvRouterConfig>,
311
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
312
    trace_path: &Path,
313
    trace_block_size: usize,
314
315
316
317
318
    max_in_flight: usize,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let config = config.normalized()?;
    validate_offline_disagg_concurrency_args(&config, max_in_flight, router_mode)?;
319
    let trace = Trace::from_mooncake(trace_path, trace_block_size)?;
320
321
322
323
    let started_at = Instant::now();
    let report = simulate_concurrency_workload_disagg_with_router_mode(
        config,
        router_config,
324
        prefill_load_estimator,
325
326
327
328
329
330
331
        trace,
        max_in_flight,
        router_mode,
    )?;
    Ok(report.with_wall_time_ms(started_at.elapsed().as_secs_f64() * 1000.0))
}

332
333
334
pub fn simulate_concurrency_live_file(
    args: MockEngineArgs,
    trace_path: &Path,
335
    trace_block_size: usize,
336
337
338
339
340
341
    max_in_flight: usize,
    num_workers: usize,
) -> Result<TraceSimulationReport> {
    simulate_concurrency_live_file_with_router_mode(
        args,
        None,
342
        None,
343
        trace_path,
344
        trace_block_size,
345
346
347
348
349
350
        max_in_flight,
        num_workers,
        ReplayRouterMode::RoundRobin,
    )
}

351
#[allow(clippy::too_many_arguments)]
352
353
354
pub fn simulate_concurrency_live_file_with_router_mode(
    args: MockEngineArgs,
    router_config: Option<KvRouterConfig>,
355
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
356
    trace_path: &Path,
357
    trace_block_size: usize,
358
359
360
361
362
363
    max_in_flight: usize,
    num_workers: usize,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let args = args.normalized()?;
    validate_online_concurrency_args(&args, num_workers, max_in_flight)?;
364
    let trace = Trace::from_mooncake(trace_path, trace_block_size)?;
365
    online::simulate_concurrency_workload(
366
367
        args,
        router_config,
368
        prefill_load_estimator,
369
        trace,
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
        max_in_flight,
        num_workers,
        router_mode,
    )
}

pub fn simulate_concurrency_live_requests(
    args: MockEngineArgs,
    requests: Vec<DirectRequest>,
    max_in_flight: usize,
    num_workers: usize,
) -> Result<TraceSimulationReport> {
    simulate_concurrency_live_requests_with_router_mode(
        args,
        None,
385
        None,
386
387
388
389
390
391
392
393
394
395
        requests,
        max_in_flight,
        num_workers,
        ReplayRouterMode::RoundRobin,
    )
}

pub fn simulate_concurrency_live_requests_with_router_mode(
    args: MockEngineArgs,
    router_config: Option<KvRouterConfig>,
396
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
397
398
399
400
401
402
403
404
405
406
407
408
409
410
    requests: Vec<DirectRequest>,
    max_in_flight: usize,
    num_workers: usize,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let args = args.normalized()?;
    validate_online_concurrency_args(&args, num_workers, max_in_flight)?;
    if requests.is_empty() {
        bail!("concurrency replay requires at least one request");
    }

    online::simulate_concurrency_requests(
        args,
        router_config,
411
        prefill_load_estimator,
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
        requests,
        max_in_flight,
        num_workers,
        router_mode,
    )
}

pub fn simulate_concurrency_requests(
    args: MockEngineArgs,
    requests: Vec<DirectRequest>,
    max_in_flight: usize,
    num_workers: usize,
) -> Result<TraceSimulationReport> {
    simulate_concurrency_requests_with_router_mode(
        args,
        None,
428
        None,
429
430
431
432
433
434
435
436
437
438
        requests,
        max_in_flight,
        num_workers,
        ReplayRouterMode::RoundRobin,
    )
}

pub fn simulate_concurrency_requests_with_router_mode(
    args: MockEngineArgs,
    router_config: Option<KvRouterConfig>,
439
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
440
441
442
443
444
445
446
447
448
449
450
451
452
453
    requests: Vec<DirectRequest>,
    max_in_flight: usize,
    num_workers: usize,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let args = args.normalized()?;
    validate_offline_concurrency_args(&args, num_workers, max_in_flight, router_mode)?;
    if requests.is_empty() {
        bail!("concurrency replay requires at least one request");
    }

    crate::replay::offline::simulate_concurrency(
        args,
        router_config,
454
        prefill_load_estimator,
455
456
457
458
459
460
        requests,
        max_in_flight,
        num_workers,
        router_mode,
    )
}
461

462
463
464
pub fn simulate_concurrency_requests_disagg_with_router_mode(
    config: OfflineDisaggReplayConfig,
    router_config: Option<KvRouterConfig>,
465
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
466
467
468
469
470
471
472
473
474
475
476
477
478
    requests: Vec<DirectRequest>,
    max_in_flight: usize,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let config = config.normalized()?;
    validate_offline_disagg_concurrency_args(&config, max_in_flight, router_mode)?;
    if requests.is_empty() {
        bail!("concurrency replay requires at least one request");
    }

    crate::replay::offline::simulate_concurrency_disagg(
        config,
        router_config,
479
        prefill_load_estimator,
480
481
482
483
484
485
        requests,
        max_in_flight,
        router_mode,
    )
}

486
487
488
489
490
491
492
493
pub fn simulate_trace_workload(
    args: MockEngineArgs,
    trace: Trace,
    num_workers: usize,
) -> Result<TraceSimulationReport> {
    simulate_trace_workload_with_router_mode(
        args,
        None,
494
        None,
495
496
497
498
499
500
501
502
503
        trace,
        num_workers,
        ReplayRouterMode::RoundRobin,
    )
}

pub fn simulate_trace_workload_with_router_mode(
    args: MockEngineArgs,
    router_config: Option<KvRouterConfig>,
504
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
505
506
507
508
509
510
511
512
513
514
    trace: Trace,
    num_workers: usize,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let args = args.normalized()?;
    validate_offline_replay_args(&args, num_workers, router_mode)?;
    let started_at = Instant::now();
    let report = crate::replay::offline::simulate_trace_workload(
        args,
        router_config,
515
        prefill_load_estimator,
516
517
518
519
520
521
522
        trace,
        num_workers,
        router_mode,
    )?;
    Ok(report.with_wall_time_ms(started_at.elapsed().as_secs_f64() * 1000.0))
}

523
524
525
pub fn simulate_trace_workload_disagg_with_router_mode(
    config: OfflineDisaggReplayConfig,
    router_config: Option<KvRouterConfig>,
526
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
527
528
529
530
531
532
533
534
535
    trace: Trace,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let config = config.normalized()?;
    validate_offline_disagg_replay_args(&config, router_mode)?;
    let started_at = Instant::now();
    let report = crate::replay::offline::simulate_trace_workload_disagg(
        config,
        router_config,
536
        prefill_load_estimator,
537
538
539
540
541
542
        trace,
        router_mode,
    )?;
    Ok(report.with_wall_time_ms(started_at.elapsed().as_secs_f64() * 1000.0))
}

543
544
545
546
547
548
549
550
pub fn simulate_trace_live_workload(
    args: MockEngineArgs,
    trace: Trace,
    num_workers: usize,
) -> Result<TraceSimulationReport> {
    simulate_trace_live_workload_with_router_mode(
        args,
        None,
551
        None,
552
553
554
555
556
557
558
559
560
        trace,
        num_workers,
        ReplayRouterMode::RoundRobin,
    )
}

pub fn simulate_trace_live_workload_with_router_mode(
    args: MockEngineArgs,
    router_config: Option<KvRouterConfig>,
561
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
562
563
564
565
566
567
    trace: Trace,
    num_workers: usize,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let args = args.normalized()?;
    validate_online_replay_args(&args, num_workers)?;
568
569
570
571
572
573
574
575
    online::simulate_trace_workload(
        args,
        router_config,
        prefill_load_estimator,
        trace,
        num_workers,
        router_mode,
    )
576
577
578
579
580
581
582
583
584
585
586
}

pub fn simulate_concurrency_workload(
    args: MockEngineArgs,
    trace: Trace,
    max_in_flight: usize,
    num_workers: usize,
) -> Result<TraceSimulationReport> {
    simulate_concurrency_workload_with_router_mode(
        args,
        None,
587
        None,
588
589
590
591
592
593
594
595
596
597
        trace,
        max_in_flight,
        num_workers,
        ReplayRouterMode::RoundRobin,
    )
}

pub fn simulate_concurrency_workload_with_router_mode(
    args: MockEngineArgs,
    router_config: Option<KvRouterConfig>,
598
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
599
600
601
602
603
604
605
606
607
608
    trace: Trace,
    max_in_flight: usize,
    num_workers: usize,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let args = args.normalized()?;
    validate_offline_concurrency_args(&args, num_workers, max_in_flight, router_mode)?;
    crate::replay::offline::simulate_concurrency_workload(
        args,
        router_config,
609
        prefill_load_estimator,
610
611
612
613
614
615
616
        trace,
        max_in_flight,
        num_workers,
        router_mode,
    )
}

617
618
619
pub fn simulate_concurrency_workload_disagg_with_router_mode(
    config: OfflineDisaggReplayConfig,
    router_config: Option<KvRouterConfig>,
620
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
621
622
623
624
625
626
627
628
629
    trace: Trace,
    max_in_flight: usize,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let config = config.normalized()?;
    validate_offline_disagg_concurrency_args(&config, max_in_flight, router_mode)?;
    crate::replay::offline::simulate_concurrency_workload_disagg(
        config,
        router_config,
630
        prefill_load_estimator,
631
632
633
634
635
636
        trace,
        max_in_flight,
        router_mode,
    )
}

637
638
639
640
641
642
643
644
645
pub fn simulate_concurrency_live_workload(
    args: MockEngineArgs,
    trace: Trace,
    max_in_flight: usize,
    num_workers: usize,
) -> Result<TraceSimulationReport> {
    simulate_concurrency_live_workload_with_router_mode(
        args,
        None,
646
        None,
647
648
649
650
651
652
653
654
655
656
        trace,
        max_in_flight,
        num_workers,
        ReplayRouterMode::RoundRobin,
    )
}

pub fn simulate_concurrency_live_workload_with_router_mode(
    args: MockEngineArgs,
    router_config: Option<KvRouterConfig>,
657
    prefill_load_estimator: Option<ReplayPrefillLoadEstimator>,
658
659
660
661
662
663
664
665
666
667
    trace: Trace,
    max_in_flight: usize,
    num_workers: usize,
    router_mode: ReplayRouterMode,
) -> Result<TraceSimulationReport> {
    let args = args.normalized()?;
    validate_online_concurrency_args(&args, num_workers, max_in_flight)?;
    online::simulate_concurrency_workload(
        args,
        router_config,
668
        prefill_load_estimator,
669
670
671
672
673
674
        trace,
        max_in_flight,
        num_workers,
        router_mode,
    )
}