kv_router.rs 8.97 KB
Newer Older
1
2
3
// SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

4
5
use std::sync::Arc;

6
use anyhow::Result;
7
use dynamo_runtime::{
8
    component::{Component, InstanceSource},
9
    pipeline::{
10
11
        async_trait, AsyncEngine, AsyncEngineContextProvider, Error, ManyOut, PushRouter,
        ResponseStream, SingleIn,
12
13
14
15
16
    },
    prelude::*,
    protocols::annotated::Annotated,
};
use futures::stream::{self, StreamExt};
17

18
pub mod approx;
19
pub mod indexer;
20
pub mod metrics_aggregator;
21
22
pub mod protocols;
pub mod publisher;
23
pub mod recorder;
24
25
pub mod scheduler;
pub mod scoring;
26

27
28
29
30
31
32
33
34
use crate::{
    kv_router::{
        indexer::{KvIndexer, KvIndexerInterface, RouterEvent},
        metrics_aggregator::KvMetricsAggregator,
        protocols::{LocalBlockHash, RouterRequest, RouterResponse, WorkerSelectionResult},
        scheduler::{KvScheduler, KvSchedulerError, SchedulingRequest},
        scoring::ProcessedEndpoints,
    },
35
    preprocessor::PreprocessedRequest,
36
    protocols::common::llm_backend::LLMEngineOutput,
Ryan Olson's avatar
Ryan Olson committed
37
    tokens::TokenBlockSequence,
38
39
};

40
use dynamo_runtime::traits::events::EventSubscriber;
41
42
43

// [gluo TODO] shouldn't need to be public
// this should be discovered from the component
44
pub const KV_EVENT_SUBJECT: &str = "kv_events";
45
pub const KV_HIT_RATE_SUBJECT: &str = "kv-hit-rate";
46
pub const KV_METRICS_ENDPOINT: &str = "load_metrics";
47

48
49
50
51
52
53
/// A trait that users can implement to define custom selection logic
pub trait WorkerSelector {
    fn select_worker(
        &self,
        workers: &ProcessedEndpoints,
        request: &SchedulingRequest,
54
        block_size: u32,
55
56
    ) -> Result<WorkerSelectionResult, KvSchedulerError>;
}
57

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/// KV Router configuration parameters
#[derive(Debug, Clone)]
pub struct KvRouterConfig {
    /// Weight for overlap score in worker selection.
    /// Higher values prioritize KV cache reuse. Default: 2.0
    pub overlap_score_weight: f64,

    /// Weight for GPU cache usage in worker selection.
    /// Higher values avoid workers with nearly full KV caches. Default: 1.0
    pub gpu_cache_usage_weight: f64,

    /// Weight for waiting requests in worker selection.
    /// Higher values avoid workers with queued requests. Default: 1.0
    pub waiting_requests_weight: f64,
}

impl Default for KvRouterConfig {
    fn default() -> Self {
        Self {
77
            overlap_score_weight: 1.0,
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
            gpu_cache_usage_weight: 1.0,
            waiting_requests_weight: 1.0,
        }
    }
}

impl KvRouterConfig {
    /// Create a new KvRouterConfig with optional weight values.
    /// If a weight is None, the default value will be used.
    pub fn new(
        overlap_score_weight: Option<f64>,
        gpu_cache_usage_weight: Option<f64>,
        waiting_requests_weight: Option<f64>,
    ) -> Self {
        let default = Self::default();
        Self {
            overlap_score_weight: overlap_score_weight.unwrap_or(default.overlap_score_weight),
            gpu_cache_usage_weight: gpu_cache_usage_weight
                .unwrap_or(default.gpu_cache_usage_weight),
            waiting_requests_weight: waiting_requests_weight
                .unwrap_or(default.waiting_requests_weight),
        }
    }
}

103
104
/// A KvRouter only decides which worker you should use. It doesn't send you there.
/// TODO: Rename this to indicate it only selects a worker, it does not route.
105
pub struct KvRouter {
106
    indexer: KvIndexer,
107
    scheduler: KvScheduler,
108
    block_size: u32,
109
110
111
112
}

impl KvRouter {
    pub async fn new(
113
        component: Component,
114
        block_size: u32,
115
        selector: Option<Box<dyn WorkerSelector + Send + Sync>>,
116
    ) -> Result<Self> {
117
118
119
120
121
        let cancellation_token = component
            .drt()
            .primary_lease()
            .expect("Cannot KV route static workers")
            .primary_token();
122
        tracing::info!("KV Routing initialized");
123
124
125
126
127
128
129
130
131
132
        let metrics_aggregator =
            KvMetricsAggregator::new(component.clone(), cancellation_token.clone()).await;
        let indexer = KvIndexer::new(cancellation_token.clone(), block_size);
        let scheduler = KvScheduler::start(
            component.namespace().clone(),
            block_size,
            metrics_aggregator.endpoints_watcher(),
            selector,
        )
        .await?;
133

134
135
136
        // [gluo TODO] try subscribe_with_type::<RouterEvent>,
        // error checking below will be different.
        let mut kv_events_rx = component.subscribe(KV_EVENT_SUBJECT).await?;
137
138
139
140
        let kv_events_tx = indexer.event_sender();

        tokio::spawn(async move {
            while let Some(event) = kv_events_rx.next().await {
Alec's avatar
Alec committed
141
                let event: RouterEvent = match serde_json::from_slice(&event.payload) {
142
                    Ok(event) => event,
Alec's avatar
Alec committed
143
144
145
146
147
148
149
                    Err(e) => {
                        tracing::warn!("Failed to deserialize RouterEvent: {:?}", e);
                        // Choosing warn and continue to process other events from other workers
                        // A bad event likely signals a problem with a worker, but potentially other workers are still healthy
                        continue;
                    }
                };
150
                if let Err(e) = kv_events_tx.send(event).await {
151
                    tracing::debug!("failed to send kv event to indexer; shutting down: {:?}", e);
152
153
154
155
                }
            }
        });

156
        Ok(Self {
157
158
            scheduler,
            indexer,
159
            block_size,
160
        })
161
162
163
    }

    // [TODO] indexer needs to take 'lora_id' as parameter
GuanLuo's avatar
GuanLuo committed
164
    pub async fn schedule(&self, token_ids: &Vec<u32>, _lora_id: u64) -> Result<i64> {
165
166
167
168
169
170
171
        // Extracting part of the code in KvRouter::generate() for only
        // the decision making part, routing is done by the caller
        let isl_tokens = token_ids.len();
        let overlap_scores = self
            .indexer
            .find_matches_for_request(token_ids.as_slice())
            .await?;
Alec's avatar
Alec committed
172
        tracing::debug!("KV router overlap_scores: {:?}", overlap_scores);
GuanLuo's avatar
GuanLuo committed
173
174
        let worker_id = self.scheduler.schedule(overlap_scores, isl_tokens).await?;
        Ok(worker_id)
175
    }
176

177
    /// Give these tokens, find the worker with the best match in it's KV cache.
178
179
    /// Returned overlap amount is in number of blocks.
    async fn find_best_match(&self, tokens: &[u32]) -> anyhow::Result<(i64, u32)> {
180
        let isl_tokens = tokens.len();
181
182
        let block_size = self.block_size;

Ryan Olson's avatar
Ryan Olson committed
183
        let (complete_blocks, _partial_block) =
184
            TokenBlockSequence::split_tokens(tokens, block_size, 1337_u64);
Ryan Olson's avatar
Ryan Olson committed
185
186
187
188
189

        let local_block_hashes = complete_blocks
            .into_iter()
            .map(|block| LocalBlockHash(block.block_hash()))
            .collect();
190
        let overlap_scores = self.indexer.find_matches(local_block_hashes).await?;
191
192
193
194
195
196
        let worker_id = self
            .scheduler
            .schedule(overlap_scores.clone(), isl_tokens)
            .await?;
        let overlap_amount = overlap_scores.scores.get(&worker_id).copied().unwrap_or(0);
        Ok((worker_id, overlap_amount))
197
    }
198
199

    /// Get the block size this router was configured with
200
    pub fn block_size(&self) -> u32 {
201
202
        self.block_size
    }
203
204
205
206
207
208
209
210
211
}

#[async_trait]
impl AsyncEngine<SingleIn<RouterRequest>, ManyOut<Annotated<RouterResponse>>, Error> for KvRouter {
    async fn generate(
        &self,
        request: SingleIn<RouterRequest>,
    ) -> Result<ManyOut<Annotated<RouterResponse>>> {
        let (request, ctx) = request.into_parts();
212
        let (worker_id, _) = self.find_best_match(&request.tokens).await?;
213
214
215
216
217
218
219

        let response = RouterResponse { worker_id };
        let response = Annotated::from_data(response);
        let stream = stream::iter(vec![response]);
        Ok(ResponseStream::new(Box::pin(stream), ctx.context()))
    }
}
220
221

pub struct KvPushRouter {
222
    inner: PushRouter<PreprocessedRequest, Annotated<LLMEngineOutput>>,
223
224
225
226
227
    chooser: Arc<KvRouter>,
}

impl KvPushRouter {
    pub fn new(
228
        inner: PushRouter<PreprocessedRequest, Annotated<LLMEngineOutput>>,
229
230
231
232
233
234
235
        chooser: Arc<KvRouter>,
    ) -> Self {
        KvPushRouter { inner, chooser }
    }
}

#[async_trait]
236
impl AsyncEngine<SingleIn<PreprocessedRequest>, ManyOut<Annotated<LLMEngineOutput>>, Error>
237
238
239
240
    for KvPushRouter
{
    async fn generate(
        &self,
241
        request: SingleIn<PreprocessedRequest>,
242
    ) -> Result<ManyOut<Annotated<LLMEngineOutput>>, Error> {
243
        match self.inner.client.instance_source.as_ref() {
244
245
            InstanceSource::Static => self.inner.r#static(request).await,
            InstanceSource::Dynamic(_) => {
246
247
248
249
250
251
252
                let (instance_id, overlap_amount) =
                    self.chooser.find_best_match(&request.token_ids).await?;
                // Update the request with the estimated prefix hit blocks
                let (mut backend_input, context) = request.into_parts();
                backend_input.estimated_prefix_hit_num_blocks = Some(overlap_amount);
                let updated_request = context.map(|_| backend_input);
                self.inner.direct(updated_request, instance_id).await
253
254
255
256
            }
        }
    }
}