dynamo-kv-router 1.4.0

KV Router - Radix tree for LLM KV cache routing
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Conditional-disagg bypass policy.
//!
//! Decides whether a request should skip remote prefill and run prefill
//! locally on the chosen decode worker. The trait operates over summary
//! signals so future policies can plug in without changing the router call site.

use async_trait::async_trait;

use crate::config::{ConditionalDisaggPolicyKind, KvRouterConfig};

/// Default effective-ISL absolute threshold (tokens). A request bypasses to
/// AGG only if its net-new prefill stays under this cap.
pub const DEFAULT_CONDITIONAL_DISAGG_EFF_ISL_THRESHOLD: usize = 2048;

/// Default effective-ISL ratio threshold. A request bypasses to AGG only if
/// `eff_isl / prompt_tokens` stays under this fraction.
pub const DEFAULT_CONDITIONAL_DISAGG_EFF_ISL_RATIO_THRESHOLD: f64 = 0.7;

/// Inputs passed to a `ConditionalDisaggPolicy` when deciding whether to
/// bypass remote prefill.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct ConditionalDisaggDecisionInput {
    /// Total prompt token count.
    pub prompt_tokens: usize,

    /// Effective cache credit on the chosen decode worker, in weighted tokens.
    pub decode_chosen_cached_tokens: usize,

    /// Whether the prefill worker the router would pick for this request is
    /// over the prefill-busy line. `None` means the signal is unavailable.
    pub prefill_chosen_worker_busy: Option<bool>,

    /// Whether the decode worker the router would pick for this request is
    /// over the decode-busy line. `None` means the gate is disabled or the
    /// signal is unavailable.
    pub decode_chosen_worker_busy: Option<bool>,
}

impl ConditionalDisaggDecisionInput {
    pub fn new(prompt_tokens: usize, decode_chosen_cached_tokens: usize) -> Self {
        Self {
            prompt_tokens,
            decode_chosen_cached_tokens,
            prefill_chosen_worker_busy: None,
            decode_chosen_worker_busy: None,
        }
    }

    pub fn with_prefill_chosen_worker_busy(mut self, busy: Option<bool>) -> Self {
        self.prefill_chosen_worker_busy = busy;
        self
    }

    pub fn with_decode_chosen_worker_busy(mut self, busy: Option<bool>) -> Self {
        self.decode_chosen_worker_busy = busy;
        self
    }

    /// Effective net-new prefill in tokens after the decode-side weighted
    /// cache hit is subtracted.
    pub fn net_new_tokens(self) -> usize {
        self.prompt_tokens
            .saturating_sub(self.decode_chosen_cached_tokens.min(self.prompt_tokens))
    }
}

#[async_trait]
pub trait ConditionalDisaggPolicy: Send + Sync {
    fn is_enabled(&self) -> bool;

    /// Decide whether the request should skip remote prefill.
    async fn should_bypass_remote_prefill(&self, input: ConditionalDisaggDecisionInput) -> bool;

    /// True iff this policy consumes
    /// [`ConditionalDisaggDecisionInput::prefill_chosen_worker_busy`].
    fn needs_prefill_worker_busy(&self) -> bool {
        false
    }
}

/// Build the configured conditional-disagg policy. Returns a disabled policy
/// when conditional disagg is not enabled.
pub fn make_conditional_disagg_policy(
    config: Option<&KvRouterConfig>,
) -> Box<dyn ConditionalDisaggPolicy> {
    let Some(config) = config else {
        return Box::new(IslBoundingPolicy::disabled());
    };
    match config.conditional_disagg_policy {
        ConditionalDisaggPolicyKind::IslBounding => {
            Box::new(IslBoundingPolicy::from_config(config))
        }
        ConditionalDisaggPolicyKind::PrefillLoad => {
            Box::new(PrefillLoadPolicy::from_config(config))
        }
        ConditionalDisaggPolicyKind::IslOrLoad => Box::new(IslOrLoadPolicy::from_config(config)),
    }
}

pub fn policy_needs_prefill_worker_busy(config: Option<&KvRouterConfig>) -> bool {
    make_conditional_disagg_policy(config).needs_prefill_worker_busy()
}

/// v1 conditional-disagg policy. Bypasses to AGG when the request is both
/// small in absolute net-new prefill and mostly cached on the decode worker.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct IslBoundingPolicy {
    enabled: bool,
    eff_isl_threshold: usize,
    eff_isl_ratio_threshold: f64,
}

impl IslBoundingPolicy {
    pub fn new(enabled: bool, eff_isl_threshold: usize, eff_isl_ratio_threshold: f64) -> Self {
        Self {
            enabled,
            eff_isl_threshold,
            eff_isl_ratio_threshold,
        }
    }

    pub fn from_config(config: &KvRouterConfig) -> Self {
        Self {
            enabled: config.conditional_disagg_enabled,
            eff_isl_threshold: config.conditional_disagg_eff_isl_threshold,
            eff_isl_ratio_threshold: config.conditional_disagg_eff_isl_ratio_threshold,
        }
    }

    pub fn disabled() -> Self {
        Self {
            enabled: false,
            eff_isl_threshold: DEFAULT_CONDITIONAL_DISAGG_EFF_ISL_THRESHOLD,
            eff_isl_ratio_threshold: DEFAULT_CONDITIONAL_DISAGG_EFF_ISL_RATIO_THRESHOLD,
        }
    }
}

impl Default for IslBoundingPolicy {
    fn default() -> Self {
        Self::disabled()
    }
}

#[async_trait]
impl ConditionalDisaggPolicy for IslBoundingPolicy {
    fn is_enabled(&self) -> bool {
        self.enabled
    }

    async fn should_bypass_remote_prefill(&self, input: ConditionalDisaggDecisionInput) -> bool {
        if !self.enabled {
            return false;
        }
        let eff_isl = input.net_new_tokens();
        if eff_isl >= self.eff_isl_threshold {
            return false;
        }
        let denom = input.prompt_tokens.max(1) as f64;
        let ratio = eff_isl as f64 / denom;
        ratio < self.eff_isl_ratio_threshold
    }
}

/// v1.5 conditional-disagg policy. Bypasses to AGG when the prefill worker
/// the router would pick for this request is already over the prefill-busy line.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PrefillLoadPolicy {
    enabled: bool,
}

impl PrefillLoadPolicy {
    pub fn new(enabled: bool) -> Self {
        Self { enabled }
    }

    pub fn from_config(config: &KvRouterConfig) -> Self {
        Self {
            enabled: config.conditional_disagg_enabled,
        }
    }

    pub fn disabled() -> Self {
        Self { enabled: false }
    }
}

impl Default for PrefillLoadPolicy {
    fn default() -> Self {
        Self::disabled()
    }
}

#[async_trait]
impl ConditionalDisaggPolicy for PrefillLoadPolicy {
    fn is_enabled(&self) -> bool {
        self.enabled
    }

    async fn should_bypass_remote_prefill(&self, input: ConditionalDisaggDecisionInput) -> bool {
        if !self.enabled {
            return false;
        }
        input.prefill_chosen_worker_busy.unwrap_or(false)
    }

    fn needs_prefill_worker_busy(&self) -> bool {
        self.enabled
    }
}

/// v1.5 composition policy: bypass when either the ISL policy or the load
/// policy wants bypass.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct IslOrLoadPolicy {
    isl: IslBoundingPolicy,
    load: PrefillLoadPolicy,
}

impl IslOrLoadPolicy {
    pub fn new(isl: IslBoundingPolicy, load: PrefillLoadPolicy) -> Self {
        Self { isl, load }
    }

    pub fn from_config(config: &KvRouterConfig) -> Self {
        Self {
            isl: IslBoundingPolicy::from_config(config),
            load: PrefillLoadPolicy::from_config(config),
        }
    }

    pub fn disabled() -> Self {
        Self {
            isl: IslBoundingPolicy::disabled(),
            load: PrefillLoadPolicy::disabled(),
        }
    }
}

impl Default for IslOrLoadPolicy {
    fn default() -> Self {
        Self::disabled()
    }
}

#[async_trait]
impl ConditionalDisaggPolicy for IslOrLoadPolicy {
    fn is_enabled(&self) -> bool {
        self.isl.is_enabled() || self.load.is_enabled()
    }

    async fn should_bypass_remote_prefill(&self, input: ConditionalDisaggDecisionInput) -> bool {
        self.isl.should_bypass_remote_prefill(input).await
            || self.load.should_bypass_remote_prefill(input).await
    }

    fn needs_prefill_worker_busy(&self) -> bool {
        self.isl.needs_prefill_worker_busy() || self.load.needs_prefill_worker_busy()
    }
}

#[cfg(test)]
#[derive(Debug, Clone, Copy)]
pub struct RandomBypassConditionalDisaggPolicy {
    enabled: bool,
    bypass_probability: f64,
}

#[cfg(test)]
impl RandomBypassConditionalDisaggPolicy {
    pub fn new(enabled: bool, bypass_probability: f64) -> Self {
        Self {
            enabled,
            bypass_probability: bypass_probability.clamp(0.0, 1.0),
        }
    }
}

#[cfg(test)]
#[async_trait]
impl ConditionalDisaggPolicy for RandomBypassConditionalDisaggPolicy {
    fn is_enabled(&self) -> bool {
        self.enabled
    }

    async fn should_bypass_remote_prefill(&self, _input: ConditionalDisaggDecisionInput) -> bool {
        if !self.enabled {
            return false;
        }
        fastrand::f64() < self.bypass_probability
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn input(prompt_tokens: usize, cached_tokens: usize) -> ConditionalDisaggDecisionInput {
        ConditionalDisaggDecisionInput::new(prompt_tokens, cached_tokens)
    }

    fn input_from_blocks(
        prompt_tokens: usize,
        overlap_blocks: u32,
        block_size: usize,
    ) -> ConditionalDisaggDecisionInput {
        input(
            prompt_tokens,
            (overlap_blocks as usize).saturating_mul(block_size),
        )
    }

    #[tokio::test]
    async fn disabled_never_bypasses() {
        let policy = IslBoundingPolicy::new(false, 2048, 0.7);
        assert!(
            !policy
                .should_bypass_remote_prefill(input_from_blocks(100, 0, 64))
                .await
        );
    }

    #[tokio::test]
    async fn small_and_mostly_cached_bypasses() {
        let policy = IslBoundingPolicy::new(true, 2048, 0.7);
        assert!(
            policy
                .should_bypass_remote_prefill(input_from_blocks(1000, 14, 64))
                .await
        );
    }

    #[tokio::test]
    async fn large_eff_isl_does_not_bypass_even_if_ratio_low() {
        let policy = IslBoundingPolicy::new(true, 2048, 0.99);
        assert!(
            !policy
                .should_bypass_remote_prefill(input_from_blocks(100_000, 1000, 64))
                .await
        );
    }

    #[tokio::test]
    async fn small_eff_isl_but_ratio_at_or_above_threshold_does_not_bypass() {
        let policy = IslBoundingPolicy::new(true, 2048, 0.7);
        assert!(
            !policy
                .should_bypass_remote_prefill(input_from_blocks(200, 0, 64))
                .await
        );
    }

    #[tokio::test]
    async fn boundary_eff_isl_equals_threshold_does_not_bypass() {
        let policy = IslBoundingPolicy::new(true, 2048, 0.99);
        assert!(
            !policy
                .should_bypass_remote_prefill(input_from_blocks(2048, 0, 64))
                .await
        );
    }

    #[tokio::test]
    async fn zero_prompt_tokens_does_not_panic() {
        let policy = IslBoundingPolicy::new(true, 2048, 0.7);
        assert!(
            policy
                .should_bypass_remote_prefill(input_from_blocks(0, 0, 64))
                .await
        );
    }

    #[tokio::test]
    async fn overlap_exceeding_prompt_clamps_to_zero_eff_isl() {
        let policy = IslBoundingPolicy::new(true, 2048, 0.7);
        assert!(
            policy
                .should_bypass_remote_prefill(input_from_blocks(500, 10, 64))
                .await
        );
    }

    #[tokio::test]
    async fn weighted_cached_tokens_do_not_round_up_to_full_block() {
        let policy = IslBoundingPolicy::new(true, 2048, 0.99);
        assert!(!policy.should_bypass_remote_prefill(input(2100, 48)).await);
    }

    #[tokio::test]
    async fn random_bypass_when_disabled_never_bypasses() {
        let policy = RandomBypassConditionalDisaggPolicy::new(false, 1.0);
        assert!(
            !policy
                .should_bypass_remote_prefill(input_from_blocks(100, 0, 64))
                .await
        );
    }

    #[tokio::test]
    async fn random_bypass_zero_probability_never_bypasses() {
        let policy = RandomBypassConditionalDisaggPolicy::new(true, 0.0);
        for _ in 0..50 {
            assert!(
                !policy
                    .should_bypass_remote_prefill(input_from_blocks(100, 0, 64))
                    .await
            );
        }
    }

    #[tokio::test]
    async fn random_bypass_one_probability_always_bypasses() {
        let policy = RandomBypassConditionalDisaggPolicy::new(true, 1.0);
        for _ in 0..50 {
            assert!(
                policy
                    .should_bypass_remote_prefill(input_from_blocks(100, 0, 64))
                    .await
            );
        }
    }

    fn input_with_busy(
        prompt_tokens: usize,
        overlap_blocks: u32,
        block_size: usize,
        busy: Option<bool>,
    ) -> ConditionalDisaggDecisionInput {
        input_from_blocks(prompt_tokens, overlap_blocks, block_size)
            .with_prefill_chosen_worker_busy(busy)
    }

    #[tokio::test]
    async fn prefill_load_disabled_never_bypasses() {
        let policy = PrefillLoadPolicy::new(false);
        assert!(
            !policy
                .should_bypass_remote_prefill(input_with_busy(1000, 0, 64, Some(true)))
                .await
        );
    }

    #[tokio::test]
    async fn prefill_load_signal_none_does_not_bypass() {
        let policy = PrefillLoadPolicy::new(true);
        assert!(
            !policy
                .should_bypass_remote_prefill(input_with_busy(1000, 0, 64, None))
                .await
        );
    }

    #[tokio::test]
    async fn prefill_load_worker_calm_does_not_bypass() {
        let policy = PrefillLoadPolicy::new(true);
        assert!(
            !policy
                .should_bypass_remote_prefill(input_with_busy(1000, 0, 64, Some(false)))
                .await
        );
    }

    #[tokio::test]
    async fn prefill_load_worker_busy_bypasses() {
        let policy = PrefillLoadPolicy::new(true);
        assert!(
            policy
                .should_bypass_remote_prefill(input_with_busy(1000, 0, 64, Some(true)))
                .await
        );
    }

    #[tokio::test]
    async fn prefill_load_independent_of_isl_fields() {
        let policy = PrefillLoadPolicy::new(true);
        assert!(
            policy
                .should_bypass_remote_prefill(input_with_busy(100_000, 0, 64, Some(true)))
                .await
        );
        assert!(
            !policy
                .should_bypass_remote_prefill(input_with_busy(100_000, 0, 64, Some(false)))
                .await
        );
    }

    fn small_input(busy: Option<bool>) -> ConditionalDisaggDecisionInput {
        input_with_busy(1000, 14, 64, busy)
    }

    fn large_input(busy: Option<bool>) -> ConditionalDisaggDecisionInput {
        input_with_busy(100_000, 0, 64, busy)
    }

    fn enabled_or_policy() -> IslOrLoadPolicy {
        IslOrLoadPolicy::new(
            IslBoundingPolicy::new(true, 2048, 0.7),
            PrefillLoadPolicy::new(true),
        )
    }

    #[tokio::test]
    async fn isl_or_load_small_and_calm_bypasses_via_isl() {
        assert!(
            enabled_or_policy()
                .should_bypass_remote_prefill(small_input(Some(false)))
                .await
        );
    }

    #[tokio::test]
    async fn isl_or_load_small_and_busy_bypasses() {
        assert!(
            enabled_or_policy()
                .should_bypass_remote_prefill(small_input(Some(true)))
                .await
        );
    }

    #[tokio::test]
    async fn isl_or_load_large_and_calm_does_not_bypass() {
        assert!(
            !enabled_or_policy()
                .should_bypass_remote_prefill(large_input(Some(false)))
                .await
        );
    }

    #[tokio::test]
    async fn isl_or_load_large_and_busy_bypasses_via_load() {
        assert!(
            enabled_or_policy()
                .should_bypass_remote_prefill(large_input(Some(true)))
                .await
        );
    }

    #[tokio::test]
    async fn isl_or_load_disabled_never_bypasses() {
        let policy = IslOrLoadPolicy::disabled();
        assert!(
            !policy
                .should_bypass_remote_prefill(small_input(Some(true)))
                .await
        );
        assert!(
            !policy
                .should_bypass_remote_prefill(large_input(Some(true)))
                .await
        );
    }

    #[tokio::test]
    async fn isl_or_load_signal_none_falls_back_to_isl_only() {
        let policy = enabled_or_policy();
        assert!(policy.should_bypass_remote_prefill(small_input(None)).await);
        assert!(!policy.should_bypass_remote_prefill(large_input(None)).await);
    }

    #[tokio::test]
    async fn decision_input_new_defaults_busy_to_none() {
        let input = ConditionalDisaggDecisionInput::new(1000, 0);
        assert_eq!(input.prefill_chosen_worker_busy, None);
        assert_eq!(input.decode_chosen_worker_busy, None);
    }

    #[tokio::test]
    async fn decision_input_with_decode_chosen_worker_busy_round_trips() {
        let input =
            ConditionalDisaggDecisionInput::new(1000, 0).with_decode_chosen_worker_busy(Some(true));
        assert_eq!(input.decode_chosen_worker_busy, Some(true));
        assert_eq!(input.prefill_chosen_worker_busy, None);
    }
}