launch_router.py 3.67 KB
Newer Older
1
import argparse
2
import logging
3
import sys
4
from typing import List, Optional
5

6
7
8
import setproctitle
from sglang_router.mini_lb import MiniLoadBalancer
from sglang_router.router_args import RouterArgs
9

10
logger = logging.getLogger("router")
11

12
13
14
15
16
17
try:
    from sglang_router.router import Router
except ImportError:
    Router = None
    logger.warning(
        "Rust Router is not installed, only python MiniLB (debugging only) is available"
18
19
    )

20
21
22
23
24
25
26
27
28
29
30
31

def launch_router(args: argparse.Namespace) -> Optional[Router]:
    """
    Launch the SGLang router with the configuration from parsed arguments.

    Args:
        args: Namespace object containing router configuration
            Can be either raw argparse.Namespace or converted RouterArgs

    Returns:
        Router instance if successful, None if failed
    """
32
    setproctitle.setproctitle("sglang::router")
33
34
35
36
37
38
39
    try:
        # Convert to RouterArgs if needed
        if not isinstance(args, RouterArgs):
            router_args = RouterArgs.from_cli_args(args)
        else:
            router_args = args

40
41
42
43
        if router_args.mini_lb:
            mini_lb = MiniLoadBalancer(router_args)
            mini_lb.start()
        else:
44
45
46
47
            # TODO: support tracing for router(Rust).
            del router_args.enable_trace
            del router_args.otlp_traces_endpoint

48
49
50
51
52
            if Router is None:
                raise RuntimeError("Rust Router is not installed")
            router_args._validate_router_args()
            router = Router.from_args(router_args)
            router.start()
53
54

    except Exception as e:
55
        logger.error(f"Error starting router: {e}")
56
        raise e
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76


class CustomHelpFormatter(
    argparse.RawDescriptionHelpFormatter, argparse.ArgumentDefaultsHelpFormatter
):
    """Custom formatter that preserves both description formatting and shows defaults"""

    pass


def parse_router_args(args: List[str]) -> RouterArgs:
    """Parse command line arguments and return RouterArgs instance."""
    parser = argparse.ArgumentParser(
        description="""SGLang Router - High-performance request distribution across worker nodes

Usage:
This launcher enables starting a router with individual worker instances. It is useful for
multi-node setups or when you want to start workers and router separately.

Examples:
77
  # Regular mode
78
  python -m sglang_router.launch_router --worker-urls http://worker1:8000 http://worker2:8000
79

80
  # PD disaggregated mode with same policy for both
81
  python -m sglang_router.launch_router --pd-disaggregation \\
82
    --prefill http://prefill1:8000 9000 --prefill http://prefill2:8000 \\
83
84
    --decode http://decode1:8001 --decode http://decode2:8001 \\
    --policy cache_aware
85

86
87
88
89
90
91
92
  # PD mode with optional bootstrap ports
  python -m sglang_router.launch_router --pd-disaggregation \\
    --prefill http://prefill1:8000 9000 \\    # With bootstrap port
    --prefill http://prefill2:8000 none \\    # Explicitly no bootstrap port
    --prefill http://prefill3:8000 \\         # Defaults to no bootstrap port
    --decode http://decode1:8001 --decode http://decode2:8001

93
94
  # PD mode with different policies for prefill and decode
  python -m sglang_router.launch_router --pd-disaggregation \\
95
    --prefill http://prefill1:8000 --prefill http://prefill2:8000 \\
96
97
98
    --decode http://decode1:8001 --decode http://decode2:8001 \\
    --prefill-policy cache_aware --decode-policy power_of_two

99
100
101
102
103
104
105
106
107
108
    """,
        formatter_class=CustomHelpFormatter,
    )

    RouterArgs.add_cli_args(parser, use_router_prefix=False)
    return RouterArgs.from_cli_args(parser.parse_args(args), use_router_prefix=False)


def main() -> None:
    router_args = parse_router_args(sys.argv[1:])
109
    launch_router(router_args)
110
111
112
113


if __name__ == "__main__":
    main()