launch_router.py 4 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
        if router_args.mini_lb:
41
42
43
44
45
46
47
48
49
            if router_args.enable_trace:
                from sglang.srt.tracing.trace import (
                    process_tracing_init,
                    trace_set_thread_info,
                )

                process_tracing_init(router_args.otlp_traces_endpoint, "sglang")
                trace_set_thread_info("Mini lb")

50
51
52
            mini_lb = MiniLoadBalancer(router_args)
            mini_lb.start()
        else:
53
54
55
56
            # TODO: support tracing for router(Rust).
            del router_args.enable_trace
            del router_args.otlp_traces_endpoint

57
58
59
60
61
            if Router is None:
                raise RuntimeError("Rust Router is not installed")
            router_args._validate_router_args()
            router = Router.from_args(router_args)
            router.start()
62
63

    except Exception as e:
64
        logger.error(f"Error starting router: {e}")
65
        raise e
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85


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

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

95
96
97
98
99
100
101
  # 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

102
103
  # PD mode with different policies for prefill and decode
  python -m sglang_router.launch_router --pd-disaggregation \\
104
    --prefill http://prefill1:8000 --prefill http://prefill2:8000 \\
105
106
107
    --decode http://decode1:8001 --decode http://decode2:8001 \\
    --prefill-policy cache_aware --decode-policy power_of_two

108
109
110
111
112
113
114
115
116
117
    """,
        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:])
118
    launch_router(router_args)
119
120
121
122


if __name__ == "__main__":
    main()