-
Notifications
You must be signed in to change notification settings - Fork 204
Add SGLang Router Support #3267
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
12 commits
Select commit
Hold shift + click to select a range
3c08dc7
Add SGLang Router Support
f191132
Rename router_config to router
23d3e69
Rename sglang_workers.jinja2 to router_workers.jinja2
e6c2bcb
Resolve SGLang API expose issue
178d5a5
Resolve model_id based single router to multi router
b8794f1
Resolve gateway installation command
5699556
Resolved Minor Review Comments
a475858
Resolve comments and kubernetes sglang router intregration
741c0ea
Resolve additional comments
d7b21f9
Pinned sglang-router to 0.2.1
ebd88b8
Merge branch 'master' into add_sglang_router_support
Bihan c7a6c01
Fix linting error
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
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
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 |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from enum import Enum | ||
| from typing import Literal | ||
|
|
||
| from dstack._internal.core.models.common import CoreModel | ||
|
|
||
|
|
||
| class RouterType(str, Enum): | ||
| SGLANG = "sglang" | ||
|
|
||
|
|
||
| class SGLangRouterConfig(CoreModel): | ||
| type: Literal["sglang"] = "sglang" | ||
| policy: Literal["random", "round_robin", "cache_aware", "power_of_two"] = "cache_aware" | ||
|
|
||
|
|
||
| AnyRouterConfig = SGLangRouterConfig |
23 changes: 23 additions & 0 deletions
23
src/dstack/_internal/proxy/gateway/resources/nginx/router_workers.jinja2
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 |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| {% for replica in replicas %} | ||
| # Worker {{ loop.index }} | ||
| upstream router_worker_{{ domain|replace('.', '_') }}_{{ ports[loop.index0] }}_upstream { | ||
| server unix:{{ replica.socket }}; | ||
| } | ||
|
|
||
| server { | ||
| listen 127.0.0.1:{{ ports[loop.index0] }}; | ||
| access_log off; # disable access logs for this internal endpoint | ||
|
|
||
| proxy_read_timeout 300s; | ||
| proxy_send_timeout 300s; | ||
|
|
||
| location / { | ||
| proxy_pass http://router_worker_{{ domain|replace('.', '_') }}_{{ ports[loop.index0] }}_upstream; | ||
| proxy_http_version 1.1; | ||
| proxy_set_header Host $host; | ||
| proxy_set_header X-Real-IP $remote_addr; | ||
| proxy_set_header Connection ""; | ||
| proxy_set_header Upgrade $http_upgrade; | ||
| } | ||
| } | ||
| {% endfor %} |
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
18 changes: 18 additions & 0 deletions
18
src/dstack/_internal/proxy/gateway/services/model_routers/__init__.py
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| from dstack._internal.core.models.routers import AnyRouterConfig, RouterType | ||
| from dstack._internal.proxy.gateway.services.model_routers.sglang import SglangRouter | ||
| from dstack._internal.proxy.lib.errors import ProxyError | ||
|
|
||
| from .base import Router, RouterContext | ||
|
|
||
|
|
||
| def get_router(router: AnyRouterConfig, context: RouterContext) -> Router: | ||
| if router.type == RouterType.SGLANG: | ||
| return SglangRouter(config=router, context=context) | ||
| raise ProxyError(f"Router type '{router.type}' is not available") | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "Router", | ||
| "RouterContext", | ||
| "get_router", | ||
| ] |
91 changes: 91 additions & 0 deletions
91
src/dstack/_internal/proxy/gateway/services/model_routers/base.py
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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| from abc import ABC, abstractmethod | ||
| from pathlib import Path | ||
| from typing import List, Literal, Optional | ||
|
|
||
| from pydantic import BaseModel | ||
|
|
||
| from dstack._internal.core.models.routers import AnyRouterConfig | ||
|
|
||
|
|
||
| class RouterContext(BaseModel): | ||
| """Context for router initialization and configuration.""" | ||
|
|
||
| class Config: | ||
| frozen = True | ||
|
|
||
| host: str = "127.0.0.1" | ||
| port: int | ||
| log_dir: Path | ||
| log_level: Literal["debug", "info", "warning", "error"] = "info" | ||
|
|
||
|
|
||
| class Router(ABC): | ||
| """Abstract base class for router implementations. | ||
| A router manages the lifecycle of worker replicas and handles request routing. | ||
| Different router implementations may have different mechanisms for managing | ||
| replicas. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| context: RouterContext, | ||
| config: Optional[AnyRouterConfig] = None, | ||
| ): | ||
| """Initialize router with context. | ||
|
|
||
| Args: | ||
| context: Runtime context for the router (host, port, logging, etc.) | ||
| config: Optional router configuration (implementation-specific) | ||
| """ | ||
| self.context = context | ||
|
|
||
| @abstractmethod | ||
| def start(self) -> None: | ||
| """Start the router process. | ||
|
|
||
| Raises: | ||
| Exception: If the router fails to start. | ||
| """ | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def stop(self) -> None: | ||
| """Stop the router process. | ||
|
|
||
| Raises: | ||
| Exception: If the router fails to stop. | ||
| """ | ||
| ... | ||
jvstme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @abstractmethod | ||
| def is_running(self) -> bool: | ||
| """Check if the router is currently running and responding. | ||
|
|
||
| Returns: | ||
| True if the router is running and healthy, False otherwise. | ||
| """ | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def remove_replicas(self, replica_urls: List[str]) -> None: | ||
|
Collaborator
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. (nit) This method looks redundant, because it is possible to remove replicas using For example, this can be replaced with |
||
| """Unregister replicas from the router (actual API calls to remove workers). | ||
|
|
||
| Args: | ||
| replica_urls: The list of replica URLs to remove from router. | ||
|
|
||
| Raises: | ||
| Exception: If removing replicas fails. | ||
| """ | ||
| ... | ||
|
|
||
| @abstractmethod | ||
| def update_replicas(self, replica_urls: List[str]) -> None: | ||
| """Update replicas for service, replacing the current set. | ||
|
|
||
| Args: | ||
| replica_urls: The new list of replica URLs for this service. | ||
|
|
||
| Raises: | ||
| Exception: If updating replicas fails. | ||
| """ | ||
| ... | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(nit) I'd suggest to move the
model_routersdirectory toproxy/gateway/services/model_routers.The subdirectories in
proxy/gatewayare supposed to represent architectural tiers:proxy/gateway/repo- the data tierproxy/gateway/services- the logic tierproxy/gateway/routersandproxy/gateway/schemas- the presentation tierThe model routers implementation is part of the logic tier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved