| 1 | # Automated vLLM Server Parameter Tuning |
| 2 | |
| 3 | This script automates the process of finding the optimal server parameter combination (`max-num-seqs` and `max-num-batched-tokens`) to maximize throughput for a vLLM server. It also supports additional constraints such as E2E latency and prefix cache hit rate. |
| 4 | |
| 5 | ## Table of Contents |
| 6 | |
| 7 | - [Prerequisites](#prerequisites) |
| 8 | - [Configuration](#configuration) |
| 9 | - [How to Run](#how-to-run) |
| 10 | - [Example Use Cases](#example-use-cases) |
| 11 | - [Output](#output) |
| 12 | - [How It Works](#how-it-works) |
| 13 | |
| 14 | ## Prerequisites |
| 15 | |
| 16 | Before running the script, please ensure the following steps are completed: |
| 17 | |
| 18 | 1. **Clone vLLM & Set Up Branch**: Clone the vLLM repository and check out to your desired branch. |
| 19 | |
| 20 | ```bash |
| 21 | git clone https://github.com/vllm-project/vllm.git |
| 22 | cd vllm |
| 23 | # git checkout <your-branch> |
| 24 | ``` |
| 25 | |
| 26 | 1. **Install Environment**: Install or update the correct running environment. For TPU usage, activate your `conda` environment and install the corresponding `torch` and `torch_xla` versions. |
| 27 | |
| 28 | 2. **Model Configuration**: If you are using a customized model, ensure its configuration files are correctly placed and accessible. |
| 29 | |
| 30 | ## Configuration |
| 31 | |
| 32 | You must set the following variables at the top of the script before execution. |
| 33 | |
| 34 | Note: You can also override the default values below via environment variables when running the script. |
| 35 | |
| 36 | ```bash |
| 37 | MODEL=meta-llama/Llama-3.3-70B-Instruct SYSTEM=TPU TP=8 DOWNLOAD_DIR='' INPUT_LEN=128 OUTPUT_LEN=2048 MAX_MODEL_LEN=2300 MIN_CACHE_HIT_PCT=0 MAX_LATENCY_ALLOWED_MS=100000000000 NUM_SEQS_LIST="128 256" NUM_BATCHED_TOKENS_LIST="1024 2048 4096" VLLM_LOGGING_LEVEL=DEBUG bash auto_tune.sh |
| 38 | ``` |
| 39 | |
| 40 | | Variable | Description | Example Value | |
| 41 | | --- | --- | --- | |
| 42 | | `BASE` | **Required.** The absolute path to the parent directory of your vLLM repository directory. | `"$HOME"` | |
| 43 | | `MODEL` | **Required.** The Hugging Face model identifier to be served by vllm. | `"meta-llama/Llama-3.1-8B-Instruct"` | |
| 44 | | `SYSTEM` | **Required.** The hardware you are running on. Choices: `TPU` or `GPU`. (For other systems, it might not support saving profiles) | `"TPU"` | |
| 45 | | `TP` | **Required.** The tensor-parallelism size. | `1` | |
| 46 | | `DOWNLOAD_DIR` | **Required.** Directory to download and load model weights from. | `""` (default download path) | |
| 47 | | `INPUT_LEN` | **Required.** Request input length. | `4000` | |
| 48 | | `OUTPUT_LEN` | **Required.** Request output length. | `16` | |
| 49 | | `MAX_MODEL_LEN` | **Required.** Max model length. | `4096` | |
| 50 | | `MIN_CACHE_HIT_PCT` | Prefix cache hit rate in percentage (0-100). Set to `0` to disable. | `60` | |
| 51 | | `MAX_LATENCY_ALLOWED_MS` | The maximum allowed P99 end-to-end latency in milliseconds. Set to a very large number (e.g., `100000000000`) to effectively ignore the latency constraint. | `500` | |
| 52 | | `NUM_SEQS_LIST` | A space-separated string of `max-num-seqs` values to test. | `"128 256"` | |
| 53 | | `NUM_BATCHED_TOKENS_LIST` | A space-separated string of `max-num-batched-tokens` values to test. | `"1024 2048 4096"` | |
| 54 | |
| 55 | **Note**: The default `NUM_SEQS_LIST` and `NUM_BATCHED_TOKENS_LIST` are set for medium-sized inputs/outputs. For very short contexts (e.g., 20 input, 20 output tokens), you may need to test larger values for `max-num-seqs`. |
| 56 | |
| 57 | ## How to Run |
| 58 | |
| 59 | 1. **Configure**: Edit the script and set the variables in the [Configuration](#configuration) section. |
| 60 | 2. **Execute**: Run the script. Since the process can take a long time, it is highly recommended to use a terminal multiplexer like `tmux` or `screen` to prevent the script from stopping if your connection is lost. |
| 61 | |
| 62 | ```bash |
| 63 | cd <FOLDER_OF_THIS_SCRIPT> |
| 64 | bash auto_tune.sh |
| 65 | ``` |
| 66 | |
| 67 | Please note that the `bash auto_tune.sh` command cannot contain full or partial path with keyword `vllm`, otherwise `pkill -f vllm` command will also kill this script itself. |
| 68 | |
| 69 | ## Example Use Cases |
| 70 | |
| 71 | Here are a few examples of how to configure the script for different goals: |
| 72 | |
| 73 | ### 1. Maximize Throughput (No Latency Constraint) |
| 74 | |
| 75 | - **Goal**: Find the best `max-num-seqs` and `max-num-batched-tokens` to get the highest possible throughput for 1800 input tokens and 20 output tokens. |
| 76 | - **Configuration**: |
| 77 | |
| 78 | ```bash |
| 79 | INPUT_LEN=1800 |
| 80 | OUTPUT_LEN=20 |
| 81 | MAX_MODEL_LEN=2048 |
| 82 | MIN_CACHE_HIT_PCT=0 |
| 83 | MAX_LATENCY_ALLOWED_MS=100000000000 # A very large number |
| 84 | ``` |
| 85 | |
| 86 | ### 2. Maximize Throughput with a Latency Requirement |
| 87 | |
| 88 | - **Goal**: Find the best server parameters when P99 end-to-end latency must be below 500ms. |
| 89 | - **Configuration**: |
| 90 | |
| 91 | ```bash |
| 92 | INPUT_LEN=1800 |
| 93 | OUTPUT_LEN=20 |
| 94 | MAX_MODEL_LEN=2048 |
| 95 | MIN_CACHE_HIT_PCT=0 |
| 96 | MAX_LATENCY_ALLOWED_MS=500 |
| 97 | ``` |
| 98 | |
| 99 | ### 3. Maximize Throughput with Prefix Caching and Latency Requirements |
| 100 | |
| 101 | - **Goal**: Find the best server parameters assuming a 60% prefix cache hit rate and a latency requirement of 500ms. |
| 102 | - **Configuration**: |
| 103 | |
| 104 | ```bash |
| 105 | INPUT_LEN=1800 |
| 106 | OUTPUT_LEN=20 |
| 107 | MAX_MODEL_LEN=2048 |
| 108 | MIN_CACHE_HIT_PCT=60 |
| 109 | MAX_LATENCY_ALLOWED_MS=500 |
| 110 | ``` |
| 111 | |
| 112 | ## Output |
| 113 | |
| 114 | After the script finishes, you will find the results in a new, timestamped directory created inside `$BASE/auto-benchmark/`. |
| 115 | |
| 116 | - **Log Files**: The directory (`$BASE/auto-benchmark/YYYY_MM_DD_HH_MM/`) contains detailed logs for each run: |
| 117 | - `vllm_log_...txt`: The log output from the vLLM server for each parameter combination. |
| 118 | - `bm_log_...txt`: The log output from the `vllm bench serve` command for each benchmark run. |
| 119 | |
| 120 | - **Final Result Summary**: A file named `result.txt` is created in the log directory. It contains a summary of each tested combination and concludes with the overall best parameters found. |
| 121 | |
| 122 | ```text |
| 123 | # Example result.txt content |
| 124 | hash:a1b2c3d4... |
| 125 | max_num_seqs: 128, max_num_batched_tokens: 2048, request_rate: 10.0, e2el: 450.5, throughput: 9.8, goodput: 9.8 |
| 126 | max_num_seqs: 128, max_num_batched_tokens: 4096 does not meet latency requirement 500 |
| 127 | ... |
| 128 | best_max_num_seqs: 256, best_num_batched_tokens: 2048, best_throughput: 12.5, profile saved in: /home/user/vllm/auto-benchmark/2024_08_01_10_30/profile |
| 129 | ``` |
| 130 | |
| 131 | If it cannot find the best parameters, the final row will be `best_max_num_seqs: 0, best_num_batched_tokens: 0, best_throughput: 0`. This can be due to either the server not starting properly, or the latency requirement being too strict. |
| 132 | |
| 133 | - **Profiler Trace**: A directory named `profile` is created inside the log directory. It contains the profiler trace file (e.g., `.xplane.pb` for TPU or a `.json` trace for GPU) from the single best-performing run. |
| 134 | |
| 135 | ## How It Works |
| 136 | |
| 137 | The script follows a systematic process to find the optimal parameters: |
| 138 | |
| 139 | 1. **Find Max GPU Memory Utilization**: The script first determines the highest safe `gpu-memory-utilization` (starting from 0.98 and decreasing) that does not cause an Out-Of-Memory (OOM) error when launching the server. This ensures the benchmark runs use the maximum available memory without crashing. |
| 140 | |
| 141 | 2. **Iterate and Benchmark**: It then enters a nested loop, iterating through every combination of `max-num-seqs` and `max-num-batched-tokens` provided in the configuration lists. |
| 142 | |
| 143 | 3. **Latency-Aware Throughput Search**: For each parameter combination: |
| 144 | - The vLLM server is started. |
| 145 | - A benchmark is first run with an infinite request rate (`--request-rate inf`). |
| 146 | - If the resulting P99 E2E latency is within the `MAX_LATENCY_ALLOWED_MS` limit, this throughput is considered the maximum for this configuration. |
| 147 | - If the latency is too high, the script performs a search by iteratively decreasing the request rate until the latency constraint is met. This finds the highest sustainable throughput for the given parameters and latency requirement. |
| 148 | |
| 149 | 4. **Track Best Result**: Throughout the process, the script tracks the parameter combination that has yielded the highest valid throughput so far. |
| 150 | |
| 151 | 5. **Profile Collection**: For the best-performing run, the script saves the vLLM profiler output, which can be used for deep-dive performance analysis with tools like TensorBoard. |
| 152 | |
| 153 | ## Batched `auto_tune` |
| 154 | |
| 155 | The `batch_auto_tune.sh` script allows you to run multiple `auto_tune.sh` experiments sequentially from a single configuration file. It iterates through a list of parameter sets, executes `auto_tune.sh` for each, and records the results back into the input file. |
| 156 | |
| 157 | ### Prerequisites |
| 158 | |
| 159 | - **jq**: This script requires `jq` to parse the JSON configuration file. |
| 160 | - **gcloud**: If you plan to upload results to Google Cloud Storage, the `gcloud` CLI must be installed and authenticated. |
| 161 | |
| 162 | ### How to Run |
| 163 | |
| 164 | 1. **Create a JSON configuration file**: Create a file (e.g., `runs_config.json`) containing an array of JSON objects. Each object defines the parameters for a single `auto_tune.sh` run. |
| 165 | |
| 166 | 2. **Execute the script**: |
| 167 | |
| 168 | ```bash |
| 169 | bash batch_auto_tune.sh <path_to_json_file> [gcs_upload_path] |
| 170 | ``` |
| 171 | |
| 172 | - `<path_to_json_file>`: **Required.** Path to your JSON configuration file. |
| 173 | - `[gcs_upload_path]`: **Optional.** A GCS path (e.g., `gs://my-bucket/benchmark-results`) where the detailed results and profiles for each run will be uploaded. If this is empty, the results will be available on the local filesystem (see the log for `RESULT_FILE=/path/to/results/file.txt`). |
| 174 | |
| 175 | ### Configuration File |
| 176 | |
| 177 | The JSON configuration file should contain an array of objects. Each object's keys correspond to the configuration variables for `auto_tune.sh` (see the [Configuration table above](#configuration)). These keys will be converted to uppercase environment variables for each run. |
| 178 | |
| 179 | Here is an example `runs_config.json` with two benchmark configurations: |
| 180 | |
| 181 | ```json |
| 182 | [ |
| 183 | { |
| 184 | "base": "/home/user", |
| 185 | "model": "meta-llama/Llama-3.1-8B-Instruct", |
| 186 | "system": "TPU", # OR GPU |
| 187 | "tp": 8, |
| 188 | "input_len": 128, |
| 189 | "output_len": 2048, |
| 190 | "max_model_len": 2300, |
| 191 | "num_seqs_list": "128 256", |
| 192 | "num_batched_tokens_list": "8192 16384" |
| 193 | }, |
| 194 | { |
| 195 | "base": "/home/user", |
| 196 | "model": "meta-llama/Llama-3.1-70B-Instruct", |
| 197 | "system": "TPU", # OR GPU |
| 198 | "tp": 8, |
| 199 | "input_len": 4000, |
| 200 | "output_len": 16, |
| 201 | "max_model_len": 4096, |
| 202 | "num_seqs_list": "64 128", |
| 203 | "num_batched_tokens_list": "4096 8192", |
| 204 | "max_latency_allowed_ms": 500 |
| 205 | } |
| 206 | ] |
| 207 | ``` |
| 208 | |
| 209 | ### Output |
| 210 | |
| 211 | The script modifies the input JSON file in place, adding the results of each run to the corresponding object. The following fields are added: |
| 212 | |
| 213 | - `run_id`: A unique identifier for the run, derived from the timestamp. |
| 214 | - `status`: The outcome of the run (`SUCCESS`, `FAILURE`, or `WARNING_NO_RESULT_FILE`). |
| 215 | - `results`: The content of the `result.txt` file from the `auto_tune.sh` run. |
| 216 | - `gcs_results`: The GCS URL where the run's artifacts are stored (if a GCS path was provided). |
| 217 | |
| 218 | A summary of successful and failed runs is also printed to the console upon completion. |