-
Notifications
You must be signed in to change notification settings - Fork 676
feat: expose KV routing components for easier router customization #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9e5a0e4
wip: exposing lower level KV components
GuanLuo 6e26ccd
wip: complete implementation
GuanLuo 9f7ca08
wip: verifying
GuanLuo 18f989c
wip: fix up
GuanLuo 7ddd545
fix: fix up
GuanLuo 5f7b8bf
style: doc and format
GuanLuo c30b857
style: clean up
GuanLuo b2c062c
style: fix commit check
GuanLuo ff4ec69
chore: address comment
GuanLuo a1efbd5
fix: rebase artifact
GuanLuo 86b6a55
style: format
GuanLuo d05c624
fix: fix up
GuanLuo 51e3267
chore: adddress comment
GuanLuo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,7 +13,11 @@ | |
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| use std::collections::HashMap; | ||
|
|
||
| use super::*; | ||
| use llm_rs::kv_router::indexer::KvIndexerInterface; | ||
| use tracing; | ||
|
|
||
| #[pyclass] | ||
| pub(crate) struct KvRouter { | ||
|
|
@@ -106,3 +110,160 @@ impl KvMetricsPublisher { | |
| .map_err(to_pyerr) | ||
| } | ||
| } | ||
|
|
||
| #[pyclass] | ||
| #[derive(Clone)] | ||
| pub(crate) struct OverlapScores { | ||
| inner: llm_rs::kv_router::indexer::OverlapScores, | ||
| } | ||
|
|
||
| #[pymethods] | ||
| impl OverlapScores { | ||
| #[getter] | ||
| fn scores(&self) -> HashMap<llm_rs::kv_router::indexer::WorkerId, u32> { | ||
| self.inner.scores.clone() | ||
| } | ||
|
|
||
| #[getter] | ||
| fn frequencies(&self) -> Vec<usize> { | ||
| self.inner.frequencies.clone() | ||
| } | ||
| } | ||
|
|
||
| #[pyclass] | ||
| pub(crate) struct KvIndexer { | ||
| inner: Arc<llm_rs::kv_router::indexer::KvIndexer>, | ||
| } | ||
|
|
||
| #[pymethods] | ||
| impl KvIndexer { | ||
| #[new] | ||
| fn new(component: Component) -> PyResult<Self> { | ||
| let runtime = pyo3_async_runtimes::tokio::get_runtime(); | ||
| runtime.block_on(async { | ||
| let kv_subject = component | ||
| .inner | ||
| .event_subject(llm_rs::kv_router::KV_EVENT_SUBJECT); | ||
| let inner: Arc<llm_rs::kv_router::indexer::KvIndexer> = | ||
| llm_rs::kv_router::indexer::KvIndexer::new( | ||
| component.inner.drt().runtime().child_token(), | ||
| ) | ||
| .into(); | ||
| let mut kv_events_rx = component | ||
| .inner | ||
| .drt() | ||
| .nats_client() | ||
| .client() | ||
| .subscribe(kv_subject) | ||
| .await | ||
| .map_err(to_pyerr)?; | ||
| let kv_events_tx = inner.event_sender(); | ||
|
|
||
| // [FIXME] this is the added functionality to the indexer to subscribe to kv events, | ||
| // should have been made to a trait and implemented here? i.e. AsyncEngine style | ||
| tokio::spawn(async move { | ||
| while let Some(event) = kv_events_rx.next().await { | ||
| let event: llm_rs::kv_router::indexer::RouterEvent = | ||
| serde_json::from_slice(&event.payload).unwrap(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should avoid
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GuanLuo , @alec-flowers - can you fix? |
||
| tracing::debug!("received kv event: {:?}", event); | ||
| if let Err(e) = kv_events_tx.send(event).await { | ||
| tracing::trace!( | ||
| "failed to send kv event to indexer; shutting down: {:?}", | ||
| e | ||
| ); | ||
| } | ||
| } | ||
| }); | ||
| Ok(Self { inner }) | ||
| }) | ||
| } | ||
|
|
||
| fn find_matches_for_request<'p>( | ||
| &self, | ||
| py: Python<'p>, | ||
| token_ids: Vec<u32>, | ||
| _lora_id: u64, | ||
| ) -> PyResult<Bound<'p, PyAny>> { | ||
| let indexer = self.inner.clone(); | ||
| pyo3_async_runtimes::tokio::future_into_py(py, async move { | ||
| let rs_overlap_scores = indexer | ||
| .find_matches_for_request(token_ids.as_slice()) | ||
| .await | ||
| .map_err(to_pyerr)?; | ||
| Ok(OverlapScores { | ||
| inner: rs_overlap_scores, | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[pyclass] | ||
| #[derive(Clone)] | ||
| pub(crate) struct EndpointKvMetrics { | ||
alec-flowers marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #[pyo3(get, set)] | ||
| pub worker_id: i64, | ||
| #[pyo3(get, set)] | ||
| pub request_active_slots: u64, | ||
| #[pyo3(get, set)] | ||
| pub request_total_slots: u64, | ||
| #[pyo3(get, set)] | ||
| pub kv_active_blocks: u64, | ||
| #[pyo3(get, set)] | ||
| pub kv_total_blocks: u64, | ||
| } | ||
|
|
||
| #[pyclass] | ||
| #[derive(Clone)] | ||
| pub(crate) struct AggregatedMetrics { | ||
| #[pyo3(get, set)] | ||
| pub endpoints: Vec<EndpointKvMetrics>, | ||
| #[pyo3(get, set)] | ||
| pub load_avg: f64, | ||
| #[pyo3(get, set)] | ||
| pub load_std: f64, | ||
| } | ||
|
|
||
| #[pyclass] | ||
| pub(crate) struct KvMetricsAggregator { | ||
| inner: Arc<llm_rs::kv_router::metrics_aggregator::KvMetricsAggregator>, | ||
| } | ||
|
|
||
| #[pymethods] | ||
| impl KvMetricsAggregator { | ||
| #[new] | ||
| fn new(component: Component) -> PyResult<Self> { | ||
| let runtime = pyo3_async_runtimes::tokio::get_runtime(); | ||
| runtime.block_on(async { | ||
| let inner = llm_rs::kv_router::metrics_aggregator::KvMetricsAggregator::new( | ||
| component.inner.clone(), | ||
| component.inner.drt().runtime().child_token(), | ||
| ) | ||
| .await; | ||
| Ok(Self { | ||
| inner: inner.into(), | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| fn get_metrics<'p>(&self, py: Python<'p>) -> PyResult<Bound<'p, PyAny>> { | ||
| let endpoints = self.inner.get_endpoints(); | ||
| let endpoint_kv_metrics = endpoints | ||
| .endpoints | ||
| .iter() | ||
| .map(|x| EndpointKvMetrics { | ||
| worker_id: x.worker_id(), | ||
| request_active_slots: x.data.request_active_slots, | ||
| request_total_slots: x.data.request_total_slots, | ||
| kv_active_blocks: x.data.kv_active_blocks, | ||
| kv_total_blocks: x.data.kv_total_blocks, | ||
| }) | ||
| .collect(); | ||
| pyo3_async_runtimes::tokio::future_into_py(py, async move { | ||
| Ok(AggregatedMetrics { | ||
| endpoints: endpoint_kv_metrics, | ||
| load_avg: endpoints.load_avg, | ||
| load_std: endpoints.load_std, | ||
| }) | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.