disagg_router.sh 4.94 KB
Newer Older
1
#!/bin/bash
2
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3
# SPDX-License-Identifier: Apache-2.0
4
5
6
#
# Disaggregated serving with KV-aware routing: 2 prefill + 2 decode workers.
# GPUs: 4
7
8
9
10

# Setup cleanup trap
cleanup() {
    echo "Cleaning up background processes..."
11
12
    kill $DYNAMO_PID $PREFILL_PID1 $PREFILL_PID2 $DECODE_PID1 $DECODE_PID2 2>/dev/null || true
    wait $DYNAMO_PID $PREFILL_PID1 $PREFILL_PID2 $DECODE_PID1 $DECODE_PID2 2>/dev/null || true
13
14
15
16
    echo "Cleanup complete."
}
trap cleanup EXIT INT TERM

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Parse command line arguments
ENABLE_OTEL=false
while [[ $# -gt 0 ]]; do
    case $1 in
        --enable-otel)
            ENABLE_OTEL=true
            shift
            ;;
        -h|--help)
            echo "Usage: $0 [OPTIONS]"
            echo "Options:"
            echo "  --enable-otel        Enable OpenTelemetry tracing"
            echo "  -h, --help           Show this help message"
            echo ""
            echo "Note: System metrics are enabled by default on ports:"
32
            echo "  8081-8082 (prefill workers), 8083-8084 (decode workers)"
33
34
35
36
37
38
39
40
41
42
43
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            echo "Use --help for usage information"
            exit 1
            ;;
    esac
done

# Enable tracing if requested
44
TRACE_ARGS=()
45
46
47
48
if [ "$ENABLE_OTEL" = true ]; then
    export DYN_LOGGING_JSONL=true
    export OTEL_EXPORT_ENABLED=1
    export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT:-http://localhost:4317}
49
    TRACE_ARGS+=(--enable-trace --otlp-traces-endpoint localhost:4317)
50
51
fi

52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
MODEL="Qwen/Qwen3-0.6B"
HTTP_PORT="${DYN_HTTP_PORT:-8000}"
echo "=========================================="
echo "Launching Disaggregated Router (2P + 2D)"
echo "=========================================="
echo "Model:       $MODEL"
echo "Frontend:    http://localhost:$HTTP_PORT"
echo "=========================================="
echo ""
echo "Example test command:"
echo ""
echo "  curl http://localhost:${HTTP_PORT}/v1/chat/completions \\"
echo "    -H 'Content-Type: application/json' \\"
echo "    -d '{"
echo "      \"model\": \"${MODEL}\","
67
echo "      \"messages\": [{\"role\": \"user\", \"content\": \"Explain why Roger Federer is considered one of the greatest tennis players of all time\"}],"
68
69
70
71
72
echo "      \"max_tokens\": 32"
echo "    }'"
echo ""
echo "=========================================="

73
74
# Start frontend with KV routing
# The frontend will automatically detect prefill workers and activate an internal prefill router
75
# No standalone prefill router needed - the frontend handles prefill routing internally
76
# dynamo.frontend accepts either --http-port flag or DYN_HTTP_PORT env var (defaults to 8000)
77
OTEL_SERVICE_NAME=dynamo-frontend \
78
python3 -m dynamo.frontend \
79
80
    --router-mode kv \
    --router-reset-states &
81
82
83
DYNAMO_PID=$!

# run prefill worker
84
OTEL_SERVICE_NAME=dynamo-worker-prefill-1 DYN_SYSTEM_PORT=${DYN_SYSTEM_PORT1:-8081} \
85
python3 -m dynamo.sglang \
86
87
  --model-path Qwen/Qwen3-0.6B \
  --served-model-name Qwen/Qwen3-0.6B \
88
89
90
91
92
93
  --page-size 64 \
  --tp 1 \
  --trust-remote-code \
  --disaggregation-mode prefill \
  --host 0.0.0.0 \
  --kv-events-config '{"publisher":"zmq","topic":"kv-events","endpoint":"tcp://*:5557"}' \
94
  --disaggregation-transfer-backend nixl \
95
96
  --enable-metrics \
  "${TRACE_ARGS[@]}" &
97
PREFILL_PID1=$!
98
99

# run prefill worker
100
OTEL_SERVICE_NAME=dynamo-worker-prefill-2 DYN_SYSTEM_PORT=${DYN_SYSTEM_PORT2:-8082} \
101
CUDA_VISIBLE_DEVICES=1 python3 -m dynamo.sglang \
102
103
  --model-path Qwen/Qwen3-0.6B \
  --served-model-name Qwen/Qwen3-0.6B \
104
105
106
107
108
109
  --page-size 64 \
  --tp 1 \
  --trust-remote-code \
  --disaggregation-mode prefill \
  --host 0.0.0.0 \
  --kv-events-config '{"publisher":"zmq","topic":"kv-events","endpoint":"tcp://*:5558"}' \
110
  --disaggregation-transfer-backend nixl \
111
112
  --enable-metrics \
  "${TRACE_ARGS[@]}" &
113
PREFILL_PID2=$!
114
115

# run decode worker
116
OTEL_SERVICE_NAME=dynamo-worker-decode-1 DYN_SYSTEM_PORT=${DYN_SYSTEM_PORT3:-8083} \
117
CUDA_VISIBLE_DEVICES=3 python3 -m dynamo.sglang \
118
119
  --model-path Qwen/Qwen3-0.6B \
  --served-model-name Qwen/Qwen3-0.6B \
120
121
122
123
124
125
  --page-size 64 \
  --tp 1 \
  --trust-remote-code \
  --disaggregation-mode decode \
  --host 0.0.0.0 \
  --kv-events-config '{"publisher":"zmq","topic":"kv-events","endpoint":"tcp://*:5560"}' \
126
  --disaggregation-transfer-backend nixl \
127
128
  --enable-metrics \
  "${TRACE_ARGS[@]}" &
129
DECODE_PID1=$!
130
131

# run decode worker
132
OTEL_SERVICE_NAME=dynamo-worker-decode-2 DYN_SYSTEM_PORT=${DYN_SYSTEM_PORT4:-8084} \
133
CUDA_VISIBLE_DEVICES=2 python3 -m dynamo.sglang \
134
135
  --model-path Qwen/Qwen3-0.6B \
  --served-model-name Qwen/Qwen3-0.6B \
136
137
138
139
140
141
  --page-size 64 \
  --tp 1 \
  --trust-remote-code \
  --disaggregation-mode decode \
  --host 0.0.0.0 \
  --kv-events-config '{"publisher":"zmq","topic":"kv-events","endpoint":"tcp://*:5559"}' \
142
  --disaggregation-transfer-backend nixl \
143
  --enable-metrics \
144
145
146
147
148
  "${TRACE_ARGS[@]}" &
DECODE_PID2=$!

# Wait for any worker to exit (keeps script running)
wait