disagg_same_gpu.sh 5.13 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
#
# Disaggregated prefill/decode on a SINGLE GPU.
6
7
8
# Per-worker VRAM is controlled via absolute KV token caps (not fractions).
# Profiler overrides (_PROFILE_OVERRIDE_TRTLLM_MAX_TOTAL_TOKENS) are handled via
# build_trtllm_override_args_with_mem; standalone runs use MAX_TOTAL_TOKENS.
9
#
10
11
12
13
# Measured reference (Qwen/Qwen3-0.6B, RTX 6000 Ada 48 GiB):
#   peak VRAM (nvidia-smi)     : ~6.6 GiB total (both workers)
#   default MAX_TOTAL_TOKENS   : 25000 per worker
#   min tokens (profiled)      : 256 per worker
14

15
16
17
set -e
trap 'echo Cleaning up...; kill 0' EXIT

18
19
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
source "$SCRIPT_DIR/../../../common/gpu_utils.sh"
20

21
MODEL="Qwen/Qwen3-0.6B"
22

23
24
25
# ---- Tunable (override via env vars) ----
MAX_SEQ_LEN="${MAX_SEQ_LEN:-4096}"
MAX_CONCURRENT_SEQS="${MAX_CONCURRENT_SEQS:-2}"
26
MAX_TOTAL_TOKENS="${MAX_TOTAL_TOKENS:-25000}"
27
28
29

# Environment variables with defaults
export DYNAMO_HOME=${DYNAMO_HOME:-"/workspace"}
30
31
export PREFILL_ENGINE_ARGS=${PREFILL_ENGINE_ARGS:-"$DYNAMO_HOME/examples/backends/trtllm/engine_configs/qwen3/prefill.yaml"}
export DECODE_ENGINE_ARGS=${DECODE_ENGINE_ARGS:-"$DYNAMO_HOME/examples/backends/trtllm/engine_configs/qwen3/decode.yaml"}
32
33
34
export CUDA_VISIBLE_DEVICES=${CUDA_VISIBLE_DEVICES:-"0"}
export MODALITY=${MODALITY:-"text"}

35
source "$SCRIPT_DIR/../../../common/launch_utils.sh"
36

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 ""
            exit 0
            ;;
        *)
            echo "Unknown option: $1"
            echo "Use --help for usage information"
            exit 1
            ;;
    esac
done
59

60
# Build --override-engine-args JSON.
61
62
63
64
65
66
67
68
#
# KV cache control (always absolute caps, never fractions):
#   1. Profiler env var (_PROFILE_OVERRIDE_TRTLLM_MAX_TOTAL_TOKENS or
#      _PROFILE_OVERRIDE_TRTLLM_MAX_GPU_TOTAL_BYTES) via build_trtllm_override_args_with_mem.
#   2. MAX_TOTAL_TOKENS env var (default 25000) for standalone runs.

# Collect non-memory override pairs (otel, etc.)
NON_MEM_PAIRS=""
69
70
71
72
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}
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    NON_MEM_PAIRS="\"return_perf_metrics\": true, \"otlp_traces_endpoint\": \"${OTEL_EXPORTER_OTLP_TRACES_ENDPOINT}\""
fi

if [[ -n "${_PROFILE_OVERRIDE_TRTLLM_MAX_TOTAL_TOKENS:-}" ]] || [[ -n "${_PROFILE_OVERRIDE_TRTLLM_MAX_GPU_TOTAL_BYTES:-}" ]]; then
    # Profiler provides absolute cap
    BASE_JSON=""
    [[ -n "$NON_MEM_PAIRS" ]] && BASE_JSON="{${NON_MEM_PAIRS}}"
    FINAL_JSON=$(build_trtllm_override_args_with_mem ${BASE_JSON:+--merge-with-json "$BASE_JSON"})
    OVERRIDE_ARGS=(--override-engine-args "$FINAL_JSON")
else
    # No profiler — use absolute token cap from MAX_TOTAL_TOKENS
    OVERRIDE_PAIRS="\"kv_cache_config\": {\"max_tokens\": ${MAX_TOTAL_TOKENS}}"
    if [[ -n "$NON_MEM_PAIRS" ]]; then
        OVERRIDE_PAIRS="${OVERRIDE_PAIRS}, $NON_MEM_PAIRS"
    fi
    OVERRIDE_ARGS=(--override-engine-args "{${OVERRIDE_PAIRS}}")
89
fi
90

91
92
HTTP_PORT="${DYN_HTTP_PORT:-8000}"
print_launch_banner "Launching Disaggregated on Same GPU (1 GPU)" "$MODEL" "$HTTP_PORT" \
93
    "Workers:     2 (prefill + decode, fraction is per worker)"
94

95
# run frontend
96
# dynamo.frontend accepts either --http-port flag or DYN_HTTP_PORT env var (defaults to 8000)
97
OTEL_SERVICE_NAME=dynamo-frontend \
98
python3 -m dynamo.frontend &
99
100

# run prefill worker (shares GPU with decode)
101
OTEL_SERVICE_NAME=dynamo-worker-prefill \
102
CUDA_VISIBLE_DEVICES=$CUDA_VISIBLE_DEVICES \
103
DYN_SYSTEM_PORT=${DYN_SYSTEM_PORT1:-8081} \
104
python3 -m dynamo.trtllm \
105
106
  --model-path "$MODEL" \
  --served-model-name "$MODEL" \
107
108
109
  --extra-engine-args  "$PREFILL_ENGINE_ARGS" \
  --modality "$MODALITY" \
  --publish-events-and-metrics \
110
  --disaggregation-mode prefill \
111
  "${OVERRIDE_ARGS[@]}" &
112

113
114
115
116
117
118
119
# Wait for prefill worker to load model and allocate KV cache before starting
# decode.  Both workers share one GPU; without this wait they compete for GPU
# memory during model loading, which can cause OOM.
# || true: don't let set -e kill the script on timeout (wait_for_ready returns 1).
PREFILL_SYSTEM_PORT="${DYN_SYSTEM_PORT1:-8081}"
wait_for_ready "http://localhost:${PREFILL_SYSTEM_PORT}/health" 45 || true

120
# run decode worker (shares GPU with prefill)
121
OTEL_SERVICE_NAME=dynamo-worker-decode \
122
CUDA_VISIBLE_DEVICES=$CUDA_VISIBLE_DEVICES \
123
DYN_SYSTEM_PORT=${DYN_SYSTEM_PORT2:-8082} \
124
python3 -m dynamo.trtllm \
125
126
  --model-path "$MODEL" \
  --served-model-name "$MODEL" \
127
128
129
  --extra-engine-args  "$DECODE_ENGINE_ARGS" \
  --modality "$MODALITY" \
  --publish-events-and-metrics \
130
  --disaggregation-mode decode \
131
132
133
134
  "${OVERRIDE_ARGS[@]}" &

# Exit on first worker failure; kill 0 in the EXIT trap tears down the rest
wait_any_exit