Skip to content

Conversation

@tedzhouhk
Copy link
Contributor

@tedzhouhk tedzhouhk commented May 29, 2025

Expose router configurations to dynamo-run (following the guide: https://github.com/ai-dynamo/dynamo/blob/main/docs/guides/kv_router_perf_tuning.md)

Rust reviewers: please double-check everything, I'm not sure if I break anything

Summary by CodeRabbit

  • New Features

    • Introduced three new optional command-line flags for KV-aware routing: --kv-overlap-score-weight, --kv-gpu-cache-usage-weight, and --kv-waiting-requests-weight, allowing users to fine-tune worker selection behavior.
    • Enhanced configuration options for KV router worker selection, enabling more granular control over routing based on overlap score, GPU cache usage, and waiting requests.
    • Added a new KV router configuration option to customize routing weights, improving flexibility in model watcher and routing behavior.
  • Documentation

    • Expanded and reorganized the dynamo run guide to include detailed steps and descriptions for the new KV-aware routing flags and usage examples.

@copy-pr-bot
Copy link

copy-pr-bot bot commented May 29, 2025

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented May 29, 2025

Walkthrough

This change introduces three new optional floating-point weights—overlap_score_weight, gpu_cache_usage_weight, and waiting_requests_weight—for KV-aware routing in the worker selection process. The weights are plumbed through CLI flags, configuration, and internal APIs, allowing dynamic adjustment of KV routing behavior. Documentation and usage instructions are updated accordingly.

Changes

File(s) Change Summary
lib/llm/src/kv_router.rs Added KvRouterConfig struct with three weight fields, default values, and constructor.
lib/llm/src/kv_router/scheduler.rs Added weight fields to DefaultWorkerSelector, new constructor, and default implementation; scoring formula uses weights.
lib/llm/src/discovery/model_manager.rs Updated kv_chooser_for and create_kv_chooser to accept and forward the new KvRouterConfig parameter.
lib/llm/src/discovery/watcher.rs Extended ModelWatcher struct and constructor with kv_router_config; passed it to kv_chooser_for.
launch/dynamo-run/src/flags.rs Added three new optional CLI flags for the weights to Flags struct and argument parsing; added method to build KvRouterConfig.
launch/dynamo-run/src/input/http.rs Updated run and run_watcher to accept and forward KvRouterConfig to ModelWatcher.
launch/dynamo-run/src/input/common.rs, launch/llmctl/src/main.rs, components/http/src/main.rs Updated calls to ModelWatcher::new to include additional None argument for kv_router_config.
launch/dynamo-run/src/main.rs Updated usage string to document the three new optional CLI flags.
docs/guides/dynamo_run.md Expanded and reorganized documentation; added details and examples for the new KV routing weight flags.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant Flags
    participant Main
    participant ModelWatcher
    participant ModelManager
    participant KvRouter

    User->>CLI: Runs `dynamo run` with optional KV weight flags
    CLI->>Flags: Parses CLI arguments (including weights)
    Flags->>Main: Passes weights to main logic
    Main->>ModelWatcher: Instantiates with weights
    ModelWatcher->>ModelManager: Calls kv_chooser_for(..., weights)
    ModelManager->>KvRouter: Creates KvRouter with weighted selector
Loading

Suggested reviewers

  • biswapanda
  • ryanolson
  • paulhendricks
  • kkranen
  • rmccorm4
  • alec-flowers
  • GuanLuo
  • jthomson04
  • grahamking

Poem

Three new weights for bunnies to tweak,
Overlap, cache, and requests so sleek.
Routing now dances, as flags take the stage,
Through code and through docs, they hop and engage.
With carrots of logic and lettuce of flow,
The garden of inference continues to grow!
🥕🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4ee0c82 and 3c1a92e.

📒 Files selected for processing (1)
  • lib/llm/src/kv_router/scheduler.rs (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • lib/llm/src/kv_router/scheduler.rs
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: pre-merge-rust (lib/bindings/python)
  • GitHub Check: pre-merge-rust (lib/runtime/examples)
  • GitHub Check: pre-merge-rust (.)
  • GitHub Check: Build and Test - vllm
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the feat label May 29, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8941db8 and b5b5792.

📒 Files selected for processing (10)
  • components/http/src/main.rs (1 hunks)
  • docs/guides/dynamo_run.md (2 hunks)
  • launch/dynamo-run/src/flags.rs (2 hunks)
  • launch/dynamo-run/src/input/common.rs (1 hunks)
  • launch/dynamo-run/src/input/http.rs (2 hunks)
  • launch/dynamo-run/src/main.rs (1 hunks)
  • launch/llmctl/src/main.rs (2 hunks)
  • lib/llm/src/discovery/model_manager.rs (3 hunks)
  • lib/llm/src/discovery/watcher.rs (3 hunks)
  • lib/llm/src/kv_router/scheduler.rs (3 hunks)
🧰 Additional context used
🪛 GitHub Actions: Pre Merge Validation of (ai-dynamo/dynamo/refs/pull/1259/merge) by tedzhouhk.
launch/dynamo-run/src/input/http.rs

[error] 110-117: Trailing whitespace detected and fixed by pre-commit hook 'trailing-whitespace'. Please remove trailing whitespace.

lib/llm/src/discovery/watcher.rs

[error] 218-225: Trailing whitespace detected and fixed by pre-commit hook 'trailing-whitespace'. Please remove trailing whitespace.


[error] 260-267: Trailing whitespace detected and fixed by pre-commit hook 'trailing-whitespace'. Please remove trailing whitespace.

lib/llm/src/kv_router/scheduler.rs

[error] 319-319: Trailing whitespace detected and fixed by pre-commit hook 'trailing-whitespace'. Please remove trailing whitespace.

lib/llm/src/discovery/model_manager.rs

[error] 201-208: Trailing whitespace detected and fixed by pre-commit hook 'trailing-whitespace'. Please remove trailing whitespace.

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: pre-merge-rust (lib/runtime/examples)
  • GitHub Check: pre-merge-rust (lib/bindings/python)
  • GitHub Check: Build and Test - vllm
  • GitHub Check: pre-merge-rust (.)
  • GitHub Check: Build Documentation
🔇 Additional comments (21)
launch/dynamo-run/src/input/common.rs (1)

53-55: LGTM! Consistent API extension.

The addition of three None parameters for the new KV-aware routing weights is appropriate for existing code that doesn't need to specify these optional parameters.

launch/llmctl/src/main.rs (2)

261-263: LGTM! Consistent with API extension.

The addition of three None parameters aligns with the updated ModelWatcher::new signature for KV-aware routing weights.


319-321: LGTM! Consistent with API extension.

The addition of three None parameters matches the pattern used throughout the codebase for the new optional routing weights.

components/http/src/main.rs (1)

59-59: LGTM! Consistent API update.

The addition of three None parameters correctly extends the ModelWatcher::new call to accommodate the new optional KV-aware routing weights.

launch/dynamo-run/src/main.rs (2)

33-33: LGTM! Good documentation of new CLI flags.

The USAGE string correctly documents the new KV-aware routing weight flags with their default values, making them discoverable to users.


33-33:

✅ Verification successful

Verify all ModelWatcher::new calls have been updated.

Ensure that all instances of ModelWatcher::new throughout the codebase have been updated to include the three new optional parameters.


🏁 Script executed:

#!/bin/bash
# Description: Search for all ModelWatcher::new calls to verify they include the new parameters
# Expected: All calls should have 6 parameters (3 original + 3 new None values)

echo "Searching for ModelWatcher::new calls..."
ast-grep --pattern 'ModelWatcher::new($$$)'

echo -e "\nSearching for any remaining 3-parameter ModelWatcher::new calls..."
rg -A 5 "ModelWatcher::new\(" --type rust

Length of output: 4392


All ModelWatcher::new invocations updated

Confirmed that every ModelWatcher::new call now includes all three new optional parameters (no remaining 3-parameter invocations).

launch/dynamo-run/src/flags.rs (2)

108-122: LGTM: Well-documented KV router weight configuration flags.

The three new optional weight parameters are properly implemented with clear documentation explaining their purpose and default values. The Option type allows for optional configuration while maintaining type safety.


193-204: LGTM: Proper handling of optional flags in command line generation.

The as_vec method correctly includes the new weight flags only when they are set, maintaining the expected behavior for command line argument generation.

docs/guides/dynamo_run.md (2)

3-34: LGTM: Well-structured table of contents.

The hierarchical restructuring of the table of contents improves the document's organization and readability.


44-44: LGTM: Usage example updated with new KV router weight flags.

The CLI usage example correctly documents the three new optional weight parameters with their default values, providing clear guidance for users wanting to tune KV-aware routing.

launch/dynamo-run/src/input/http.rs (1)

49-52: LGTM: Correct parameter propagation to run_watcher.

The new weight parameters are properly passed from the flags to the run_watcher function when using dynamic engine configuration.

lib/llm/src/discovery/watcher.rs (2)

39-42: LGTM: Proper addition of weight fields to ModelWatcher.

The three new optional weight fields are correctly added to the ModelWatcher struct with consistent Option typing.


49-61: LGTM: Constructor properly updated for new weight parameters.

The ModelWatcher::new constructor correctly accepts and initializes the three new weight parameters.

lib/llm/src/discovery/model_manager.rs (3)

186-188: LGTM: Clean addition of configurable weight parameters

The three new optional weight parameters are properly typed and consistently named across the function signature.


223-225: LGTM: Consistent parameter propagation

The method signature correctly mirrors the parameters from kv_chooser_for, maintaining consistency in the API.


227-231: LGTM: Proper instantiation with configurable weights

The change from default constructor to DefaultWorkerSelector::new() with the weight parameters correctly enables the configurable routing behavior.

lib/llm/src/kv_router/scheduler.rs (5)

234-239: LGTM: Well-structured configurable weight fields

The addition of the three weight fields with clear, descriptive names enables the desired configurable routing behavior. The Clone derivation is appropriate for this data structure.


241-249: LGTM: Reasonable default values

The default values (2.0 for overlap_score_weight, 1.0 for both cache usage and waiting requests) appear well-balanced and maintain backward compatibility.


251-263: LGTM: Clean constructor with optional parameters

The constructor properly handles optional parameters with sensible fallbacks to the same defaults used in the Default implementation, ensuring consistency.


99-99: LGTM: Consistent use of default implementation

Using Default::default() maintains backward compatibility while enabling the new configurable behavior when weights are explicitly provided.


320-323: LGTM: Updated trace logging with dynamic weights

The trace logging correctly reflects the configurable weights in the formula, which will be helpful for debugging and performance tuning.

@grahamking
Copy link
Contributor

This is great. It should solve most of the times people want to write a custom router.

Can you put those three values in a struct, and pass that around instead? That will allow us to add / modify settings in future.

@tedzhouhk tedzhouhk requested a review from PeaBrane as a code owner May 29, 2025 18:32
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
lib/llm/src/kv_router.rs (1)

83-100: Consider optimizing the constructor implementation.

The constructor works correctly but has a minor inefficiency and lacks input validation.

Apply this diff to optimize the constructor and add basic validation:

    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),
+            overlap_score_weight: overlap_score_weight.unwrap_or(2.0).max(0.0),
+            gpu_cache_usage_weight: gpu_cache_usage_weight.unwrap_or(1.0).max(0.0),
+            waiting_requests_weight: waiting_requests_weight.unwrap_or(1.0).max(0.0),
        }
    }

This eliminates the unnecessary default instance creation and adds basic validation to ensure non-negative weights.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b5b5792 and 4ee0c82.

📒 Files selected for processing (9)
  • components/http/src/main.rs (1 hunks)
  • launch/dynamo-run/src/flags.rs (4 hunks)
  • launch/dynamo-run/src/input/common.rs (1 hunks)
  • launch/dynamo-run/src/input/http.rs (3 hunks)
  • launch/llmctl/src/main.rs (2 hunks)
  • lib/llm/src/discovery/model_manager.rs (4 hunks)
  • lib/llm/src/discovery/watcher.rs (4 hunks)
  • lib/llm/src/kv_router.rs (1 hunks)
  • lib/llm/src/kv_router/scheduler.rs (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (8)
  • launch/dynamo-run/src/input/common.rs
  • launch/llmctl/src/main.rs
  • components/http/src/main.rs
  • launch/dynamo-run/src/input/http.rs
  • launch/dynamo-run/src/flags.rs
  • lib/llm/src/discovery/watcher.rs
  • lib/llm/src/kv_router/scheduler.rs
  • lib/llm/src/discovery/model_manager.rs
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: pre-merge-rust (lib/runtime/examples)
  • GitHub Check: pre-merge-rust (lib/bindings/python)
  • GitHub Check: pre-merge-rust (.)
  • GitHub Check: Build and Test - vllm
🔇 Additional comments (2)
lib/llm/src/kv_router.rs (2)

57-71: Well-structured configuration struct with clear documentation.

The KvRouterConfig struct is well-designed with comprehensive documentation explaining the purpose of each weight parameter. The field visibility and documentation follow good Rust practices.


73-81: Appropriate default values for KV router weights.

The default implementation provides sensible starting values that prioritize KV cache reuse (overlap_score_weight: 2.0) while balancing GPU cache usage and waiting requests (both 1.0).

@tedzhouhk tedzhouhk enabled auto-merge (squash) May 29, 2025 20:55
@tedzhouhk tedzhouhk merged commit d849f7e into main Jun 2, 2025
11 checks passed
@tedzhouhk tedzhouhk deleted the hzhou/kv_router_arg branch June 2, 2025 15:49
@coderabbitai coderabbitai bot mentioned this pull request Jun 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants