|
| 1 | +# Quick Start Recipe for Llama4 Scout 17B FP8 and NVFP4 |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +This deployment guide provides step-by-step instructions for running the Llama-4-Scout-17B-16E-Instruct model using TensorRT-LLM with FP8 and NVFP4 quantization, optimized for NVIDIA GPUs. It covers the complete setup required; from accessing model weights and preparing the software environment to configuring TensorRT-LLM parameters, launching the server, and validating inference output. |
| 6 | + |
| 7 | +The guide is intended for developers and practitioners seeking high-throughput or low-latency inference using NVIDIA’s accelerated stack—starting with the PyTorch container from NGC, then installing TensorRT-LLM for model serving, FlashInfer for optimized CUDA kernels, and ModelOpt to enable FP8 and NVFP4 quantized execution. |
| 8 | + |
| 9 | +## Access & Licensing |
| 10 | + |
| 11 | +To use Llama4 Scout 17B, you must first agree to Meta’s Llama 4 Community License ([https://github.com/meta-llama/llama-models/blob/main/models/llama4/LICENSE](https://github.com/meta-llama/llama-models/blob/main/models/llama4/LICENSE)). NVIDIA’s quantized versions (FP8 and NVFP4) are built on top of the base model and are available for research and commercial use under the same license. |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +GPU: NVIDIA Blackwell or Hopper Architecture |
| 16 | +OS: Linux |
| 17 | +Drivers: CUDA Driver 575 or Later |
| 18 | +Docker with NVIDIA Container Toolkit installed |
| 19 | +Python3 and python3-pip (Optional, for accuracy evaluation only) |
| 20 | + |
| 21 | +## Models |
| 22 | + |
| 23 | +* FP8 model: [Llama-4-Scout-17B-16E-Instruct-FP8](https://huggingface.co/nvidia/Llama-4-Scout-17B-16E-Instruct-FP8) |
| 24 | +* NVFP4 model: [Llama-4-Scout-17B-16E-Instruct-FP4](https://huggingface.co/nvidia/Llama-4-Scout-17B-16E-Instruct-FP4) |
| 25 | + |
| 26 | +Note that NVFP4 is only supported on NVIDIA Blackwell platform. |
| 27 | + |
| 28 | +## Deployment Steps |
| 29 | + |
| 30 | +### Run Docker Container |
| 31 | + |
| 32 | +Run the docker container using the TensorRT-LLM NVIDIA NGC image. |
| 33 | + |
| 34 | +```shell |
| 35 | +docker run --rm -it \ |
| 36 | +--ipc=host \ |
| 37 | +--gpus all \ |
| 38 | +-p 8000:8000 \ |
| 39 | +-v ~/.cache:/root/.cache:rw \ |
| 40 | +--name tensorrt_llm \ |
| 41 | +nvcr.io/nvidia/tensorrt-llm/release:1.0.0rc4 \ |
| 42 | +/bin/bash |
| 43 | +``` |
| 44 | + |
| 45 | +Note: |
| 46 | + |
| 47 | +* You can mount additional directories and paths using the `-v <local_path>:<path>` flag if needed, such as mounting the downloaded weight paths. |
| 48 | +* The command mounts your user .cache directory to save the downloaded model checkpoints which are saved to `~/.cache/huggingface/hub/` by default. This prevents having to redownload the weights each time you rerun the container. If the `~/.cache` directory doesn’t exist please create it using mkdir `~/.cache`. |
| 49 | +* The command also maps port 8000 from the container to your host so you can access the LLM API endpoint from your host |
| 50 | +* See the [https://catalog.ngc.nvidia.com/orgs/nvidia/teams/tensorrt-llm/containers/release/tags](https://catalog.ngc.nvidia.com/orgs/nvidia/teams/tensorrt-llm/containers/release/tags) for all the available containers. The containers published in the main branch weekly have “rcN” suffix, while the monthly release with QA tests has no “rcN” suffix. Use the rc release to get the latest model and feature support. |
| 51 | + |
| 52 | +If you want to use latest main branch, you can choose to build from source to install TensorRT-LLM, the steps refer to [https://nvidia.github.io/TensorRT-LLM/latest/installation/build-from-source-linux.html](https://nvidia.github.io/TensorRT-LLM/latest/installation/build-from-source-linux.html) |
| 53 | + |
| 54 | +### Creating the TRT-LLM Server config |
| 55 | + |
| 56 | +We create a YAML configuration file /tmp/config.yml for the TensorRT-LLM Server and populate it with the following recommended performance settings. |
| 57 | + |
| 58 | +```shell |
| 59 | +EXTRA_LLM_API_FILE=/tmp/config.yml |
| 60 | + |
| 61 | +cat << EOF > ${EXTRA_LLM_API_FILE} |
| 62 | +enable_attention_dp: false |
| 63 | +cuda_graph_config: |
| 64 | + enable_padding: true |
| 65 | + max_batch_size: 1024 |
| 66 | +kv_cache_config: |
| 67 | + dtype: fp8 |
| 68 | +EOF |
| 69 | +``` |
| 70 | + |
| 71 | +### Launch the TRT-LLM Server |
| 72 | + |
| 73 | +Below is an example command to launch the TRT-LLM server with the Llama-4-Scout-17B-16E-Instruct-FP8 model from within the container. The command is specifically configured for the 1024/1024 Input/Output Sequence Length test. The explanation of each flag is shown in the “Configs and Parameters” section. |
| 74 | + |
| 75 | +```shell |
| 76 | +trtllm-serve nvidia/Llama-4-Scout-17B-16E-Instruct-FP8 \ |
| 77 | + --host 0.0.0.0 \ |
| 78 | + --port 8000 \ |
| 79 | + --backend pytorch \ |
| 80 | + --max_batch_size 1024 \ |
| 81 | + --max_num_tokens 2048 \ |
| 82 | + --max_seq_len 2048 \ |
| 83 | + --kv_cache_free_gpu_memory_fraction 0.9 \ |
| 84 | + --tp_size 1 \ |
| 85 | + --ep_size 1 \ |
| 86 | + --trust_remote_code \ |
| 87 | + --extra_llm_api_options ${EXTRA_LLM_API_FILE} |
| 88 | +``` |
| 89 | + |
| 90 | +After the server is set up, the client can now send prompt requests to the server and receive results. |
| 91 | + |
| 92 | +### Configs and Parameters |
| 93 | + |
| 94 | +These options are used directly on the command line when you start the `trtllm-serve` process. |
| 95 | +#### `--tp_size` |
| 96 | + |
| 97 | + **Description:** Sets the **tensor-parallel size**. This should typically match the number of GPUs you intend to use for a single model instance. |
| 98 | + |
| 99 | +#### `--ep_size` |
| 100 | + |
| 101 | + **Description:** Sets the **expert-parallel size** for Mixture-of-Experts (MoE) models. Like `tp_size`, this should generally match the number of GPUs you're using. This setting has no effect on non-MoE models. |
| 102 | + |
| 103 | +#### `--kv_cache_free_gpu_memory_fraction` |
| 104 | + |
| 105 | + **Description:** A value between 0.0 and 1.0 that specifies the fraction of free GPU memory to reserve for the KV cache after the model is loaded. Since memory usage can fluctuate, this buffer helps prevent out-of-memory (OOM) errors. |
| 106 | + |
| 107 | + **Recommendation:** If you experience OOM errors, try reducing this value to **0.8** or lower. |
| 108 | + |
| 109 | +#### `--backend pytorch` |
| 110 | + |
| 111 | + **Description:** Tells TensorRT-LLM to use the **pytorch** backend. |
| 112 | + |
| 113 | +#### `--max_batch_size` |
| 114 | + |
| 115 | + **Description:** The maximum number of user requests that can be grouped into a single batch for processing. |
| 116 | + |
| 117 | +#### `--max_num_tokens` |
| 118 | + |
| 119 | + **Description:** The maximum total number of tokens (across all requests) allowed inside a single scheduled batch. |
| 120 | + |
| 121 | +#### `--max_seq_len` |
| 122 | + |
| 123 | + **Description:** The maximum possible sequence length for a single request, including both input and generated output tokens. |
| 124 | + |
| 125 | +#### `--trust_remote_code` |
| 126 | + |
| 127 | + **Description:** Allows TensorRT-LLM to download models and tokenizers from Hugging Face. This flag is passed directly to the Hugging Face API. |
| 128 | + |
| 129 | + |
| 130 | +#### Extra LLM API Options (YAML Configuration) |
| 131 | + |
| 132 | +These options provide finer control over performance and are set within a YAML file passed to the trtllm-serve command via the `--extra_llm_api_options` argument. |
| 133 | + |
| 134 | +#### `kv_cache_config` |
| 135 | + |
| 136 | + **Description**: A section for configuring the Key-Value (KV) cache. |
| 137 | + |
| 138 | + **Options**: |
| 139 | + |
| 140 | +  `dtype`: Sets the data type for the KV cache. |
| 141 | + |
| 142 | +  **Default**: auto (uses the data type specified in the model checkpoint). |
| 143 | + |
| 144 | +#### `cuda_graph_config` |
| 145 | + |
| 146 | + **Description**: A section for configuring CUDA graphs to optimize performance. |
| 147 | + |
| 148 | + **Options**: |
| 149 | + |
| 150 | +  `enable_padding`: If true, input batches are padded to the nearest `cuda_graph_batch_size`. This can significantly improve performance. |
| 151 | + |
| 152 | +  **Default**: false |
| 153 | + |
| 154 | +  `max_batch_size`: Sets the maximum batch size for which a CUDA graph will be created. |
| 155 | + |
| 156 | +  **Default**: 0 |
| 157 | + |
| 158 | +  **Recommendation**: Set this to the same value as the `--max_batch_size` command-line option. |
| 159 | + |
| 160 | +  `batch_sizes`: A specific list of batch sizes to create CUDA graphs for. |
| 161 | + |
| 162 | +  **Default**: None |
| 163 | + |
| 164 | +#### `moe_config` |
| 165 | + |
| 166 | + **Description**: Configuration for Mixture-of-Experts (MoE) models. |
| 167 | + |
| 168 | + **Options**: |
| 169 | + |
| 170 | +  `backend`: The backend to use for MoE operations. |
| 171 | + |
| 172 | +  **Default**: CUTLASS |
| 173 | + |
| 174 | +#### `attention_backend` |
| 175 | + |
| 176 | + **Description**: The backend to use for attention calculations. |
| 177 | + |
| 178 | + **Default**: TRTLLM |
| 179 | + |
| 180 | +See the [TorchLlmArgs](https://github.com/nvidia/TensorRT-LLM/blob/main/tensorrt_llm/llmapi/llm_args.py#L1980) class for the full list of options which can be used in the `extra_llm_api_options`. |
| 181 | + |
| 182 | +## Testing API Endpoint |
| 183 | + |
| 184 | +### Basic Test |
| 185 | + |
| 186 | +Start a new terminal on the host to test the TensorRT-LLM server you just launched. |
| 187 | + |
| 188 | +You can query the health/readiness of the server using: |
| 189 | + |
| 190 | +```shell |
| 191 | +curl -s -o /dev/null -w "Status: %{http_code}\n" "http://localhost:8000/health" |
| 192 | +``` |
| 193 | + |
| 194 | +When the `Status: 200` code is returned, the server is ready for queries. Note that the very first query may take longer due to initialization and compilation. |
| 195 | + |
| 196 | +After the TRT-LLM server is set up and shows Application startup complete, you can send requests to the server. |
| 197 | + |
| 198 | +```shell |
| 199 | +curl http://localhost:8000/v1/completions -H "Content-Type: application/json" -d '{ |
| 200 | + "model": "nvidia/Llama-4-Scout-17B-16E-Instruct-FP8", |
| 201 | + "prompt": "Where is New York?", |
| 202 | + "max_tokens": 16, |
| 203 | + "temperature": 0 |
| 204 | +}' |
| 205 | +``` |
| 206 | + |
| 207 | +Here is an example response, showing that the TRT-LLM server returns “New York is a state located in the northeastern United States. It is bordered by”, completing the input sequence. |
| 208 | + |
| 209 | +```json |
| 210 | +{"id":"cmpl-bc1393d529ce485c961d9ffee5b25d72","object":"text_completion","created":1753843963,"model":"$MODEL","choices":[{"index":0,"text":" New York is a state located in the northeastern United States. It is bordered by","token_ids":null,"logprobs":null,"context_logits":null,"finish_reason":"length","stop_reason":null,"disaggregated_params":null}],"usage":{"prompt_tokens":6,"total_tokens":22,"completion_tokens":16},"prompt_token_ids":null} |
| 211 | +``` |
| 212 | + |
| 213 | +### Troubleshooting Tips |
| 214 | + |
| 215 | +* If you encounter CUDA out-of-memory errors, try reducing `max_batch_size` or `max_seq_len`. |
| 216 | +* Ensure your model checkpoints are compatible with the expected format. |
| 217 | +* For performance issues, check GPU utilization with nvidia-smi while the server is running. |
| 218 | +* If the container fails to start, verify that the NVIDIA Container Toolkit is properly installed. |
| 219 | +* For connection issues, make sure port 8000 is not being used by another application. |
| 220 | + |
| 221 | +### Running Evaluations to Verify Accuracy (Optional) |
| 222 | + |
| 223 | +We use the lm-eval tool to test the model’s accuracy. For more information see [https://github.com/EleutherAI/lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness). |
| 224 | + |
| 225 | +To run the evaluation harness exec into the running TensorRT-LLM container and install with this command: |
| 226 | + |
| 227 | +```shell |
| 228 | +docker exec -it tensorrt_llm /bin/bash |
| 229 | + |
| 230 | +pip install lm_eval |
| 231 | +``` |
| 232 | + |
| 233 | +FP8 command for GSM8K |
| 234 | + |
| 235 | +```shell |
| 236 | +MODEL_PATH=nvidia/Llama-4-Scout-17B-16E-Instruct-FP8 |
| 237 | + |
| 238 | +lm_eval --model local-completions --tasks gsm8k --batch_size 256 --gen_kwargs temperature=0.0 --num_fewshot 5 --model_args model=${MODEL_PATH},base_url=http://localhost:8000/v1/completions,num_concurrent=32,max_retries=20,tokenized_requests=False --log_samples --output_path trtllm.fp8.gsm8k |
| 239 | +``` |
| 240 | + |
| 241 | +Sample result in Blackwell. |
| 242 | + |
| 243 | +```shell |
| 244 | +|Tasks|Version| Filter |n-shot| Metric | |Value | |Stderr| |
| 245 | +|-----|------:|----------------|-----:|-----------|---|-----:|---|-----:| |
| 246 | +|gsm8k| 3|flexible-extract| 5|exact_match|↑ |0.9189|± |0.0075| |
| 247 | +| | |strict-match | 5|exact_match|↑ |0.8984|± |0.0083| |
| 248 | +``` |
| 249 | + |
| 250 | +FP4 command for GSM8K |
| 251 | + |
| 252 | +```shell |
| 253 | +MODEL_PATH=nvidia/Llama-4-Scout-17B-16E-Instruct-FP4 |
| 254 | + |
| 255 | +lm_eval --model local-completions --tasks gsm8k --batch_size 256 --gen_kwargs temperature=0.0 --num_fewshot 5 --model_args model=${MODEL_PATH},base_url=http://localhost:8000/v1/completions,num_concurrent=32,max_retries=20,tokenized_requests=False --log_samples --output_path trtllm.fp4.gsm8k |
| 256 | +``` |
| 257 | + |
| 258 | +Sample result in Blackwell |
| 259 | + |
| 260 | +```shell |
| 261 | +|Tasks|Version| Filter |n-shot| Metric | |Value | |Stderr| |
| 262 | +|-----|------:|----------------|-----:|-----------|---|-----:|---|-----:| |
| 263 | +|gsm8k| 3|flexible-extract| 5|exact_match|↑ |0.9075|± |0.0080| |
| 264 | +| | |strict-match | 5|exact_match|↑ |0.8908|± |0.0086| |
| 265 | +``` |
| 266 | + |
| 267 | +## Benchmarking Performance |
| 268 | + |
| 269 | +To benchmark the performance of your TensorRT-LLM server you can leverage the built-in `benchmark_serving.py` script. To do this first creating a wrapper [bench.sh](http://bench.sh) script. |
| 270 | + |
| 271 | +```shell |
| 272 | +cat <<EOF > bench.sh |
| 273 | +concurrency_list="1 2 4 8 16 32 64 128 256" |
| 274 | +multi_round=5 |
| 275 | +isl=1024 |
| 276 | +osl=1024 |
| 277 | +result_dir=/tmp/llama4_output |
| 278 | +
|
| 279 | +for concurrency in ${concurrency_list}; do |
| 280 | + num_prompts=$((concurrency * multi_round)) |
| 281 | + python -m tensorrt_llm.serve.scripts.benchmark_serving \ |
| 282 | + --model nvidia/Llama-4-Scout-17B-16E-Instruct-FP8 \ |
| 283 | + --backend openai \ |
| 284 | + --dataset-name "random" \ |
| 285 | + --random-input-len ${isl} \ |
| 286 | + --random-output-len ${osl} \ |
| 287 | + --random-prefix-len 0 \ |
| 288 | + --random-ids \ |
| 289 | + --num-prompts ${num_prompts} \ |
| 290 | + --max-concurrency ${concurrency} \ |
| 291 | + --ignore-eos \ |
| 292 | + --tokenize-on-client \ |
| 293 | + --percentile-metrics "ttft,tpot,itl,e2el" |
| 294 | +done |
| 295 | +EOF |
| 296 | +chmod +x bench.sh |
| 297 | +``` |
| 298 | + |
| 299 | +To benchmark the FP4 model, replace `--model nvidia/Llama-4-Scout-17B-16E-Instruct-FP8` with `--model nvidia/Llama-4-Scout-17B-16E-Instruct-FP4`. |
| 300 | + |
| 301 | +If you want to save the results to a file add the following options. |
| 302 | + |
| 303 | +```shell |
| 304 | +--save-result \ |
| 305 | +--result-dir "${result_dir}" \ |
| 306 | +--result-filename "concurrency_${concurrency}.json" |
| 307 | +``` |
| 308 | + |
| 309 | +For more benchmarking options see. [https://github.com/NVIDIA/TensorRT-LLM/blob/main/tensorrt\_llm/serve/scripts/benchmark\_serving.py](https://github.com/NVIDIA/TensorRT-LLM/blob/main/tensorrt_llm/serve/scripts/benchmark_serving.py) |
| 310 | + |
| 311 | +Run bench.sh to begin a serving benchmark. This will take a long time if you run all the concurrencies mentioned in the above bench.sh script. |
| 312 | + |
| 313 | +```shell |
| 314 | +./bench.sh |
| 315 | +``` |
| 316 | + |
| 317 | +Sample TensorRT-LLM serving benchmark output. Your results may vary due to ongoing software optimizations. |
| 318 | + |
| 319 | +``` |
| 320 | +============ Serving Benchmark Result ============ |
| 321 | +Successful requests: 16 |
| 322 | +Benchmark duration (s): 17.66 |
| 323 | +Total input tokens: 16384 |
| 324 | +Total generated tokens: 16384 |
| 325 | +Request throughput (req/s): [result] |
| 326 | +Output token throughput (tok/s): [result] |
| 327 | +Total Token throughput (tok/s): [result] |
| 328 | +User throughput (tok/s): [result] |
| 329 | +---------------Time to First Token---------------- |
| 330 | +Mean TTFT (ms): [result] |
| 331 | +Median TTFT (ms): [result] |
| 332 | +P99 TTFT (ms): [result] |
| 333 | +-----Time per Output Token (excl. 1st token)------ |
| 334 | +Mean TPOT (ms): [result] |
| 335 | +Median TPOT (ms): [result] |
| 336 | +P99 TPOT (ms): [result] |
| 337 | +---------------Inter-token Latency---------------- |
| 338 | +Mean ITL (ms): [result] |
| 339 | +Median ITL (ms): [result] |
| 340 | +P99 ITL (ms): [result] |
| 341 | +----------------End-to-end Latency---------------- |
| 342 | +Mean E2EL (ms): [result] |
| 343 | +Median E2EL (ms): [result] |
| 344 | +P99 E2EL (ms): [result] |
| 345 | +================================================== |
| 346 | +``` |
| 347 | + |
| 348 | +### Key Metrics |
| 349 | + |
| 350 | +* Median Time to First Token (TTFT) |
| 351 | + * The typical time elapsed from when a request is sent until the first output token is generated. |
| 352 | +* Median Time Per Output Token (TPOT) |
| 353 | + * The typical time required to generate each token *after* the first one. |
| 354 | +* Median Inter-Token Latency (ITL) |
| 355 | + * The typical time delay between the completion of one token and the completion of the next. |
| 356 | +* Median End-to-End Latency (E2EL) |
| 357 | + * The typical total time from when a request is submitted until the final token of the response is received. |
| 358 | +* Total Token Throughput |
| 359 | + * The combined rate at which the system processes both input (prompt) tokens and output (generated) tokens. |
0 commit comments