launch_router.py 3.53 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
44
45
46
47
48
        if router_args.mini_lb:
            mini_lb = MiniLoadBalancer(router_args)
            mini_lb.start()
        else:
            if Router is None:
                raise RuntimeError("Rust Router is not installed")
            router_args._validate_router_args()
            router = Router.from_args(router_args)
            router.start()
49
50

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


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:
73
  # Regular mode
74
  python -m sglang_router.launch_router --worker-urls http://worker1:8000 http://worker2:8000
75

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

82
83
84
85
86
87
88
  # 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

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

95
96
97
98
99
100
101
102
103
104
    """,
        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:])
105
    launch_router(router_args)
106
107
108
109


if __name__ == "__main__":
    main()