|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +SPDX-License-Identifier: Apache-2.0 |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +--> |
| 17 | + |
| 18 | +# Running KVBM in TensorRT-LLM |
| 19 | + |
| 20 | +This guide explains how to leverage KVBM (KV Block Manager) to mange KV cache and do KV offloading in TensorRT-LLM (trtllm). |
| 21 | + |
| 22 | +To learn what KVBM is, please check [here](https://docs.nvidia.com/dynamo/latest/architecture/kvbm_intro.html) |
| 23 | + |
| 24 | +> [!Note] |
| 25 | +> - Ensure that `etcd` and `nats` are running before starting. |
| 26 | +> - KVBM does not currently support CUDA graphs in TensorRT-LLM. |
| 27 | +> - KVBM only supports TensorRT-LLM’s PyTorch backend. |
| 28 | +> - To enable disk cache offloading, you must first enable a CPU memory cache offloading. |
| 29 | +> - Disable partial reuse `enable_partial_reuse: false` in the LLM API config’s `kv_connector_config` to increase offloading cache hits. |
| 30 | +> - KVBM requires TensorRT-LLM at commit ce580ce4f52af3ad0043a800b3f9469e1f1109f6 or newer. |
| 31 | +> - Enabling KVBM metrics with TensorRT-LLM is still a work in progress. |
| 32 | +
|
| 33 | +## Quick Start |
| 34 | + |
| 35 | +To use KVBM in TensorRT-LLM, you can follow the steps below: |
| 36 | + |
| 37 | +```bash |
| 38 | +# start up etcd for KVBM leader/worker registration and discovery |
| 39 | +docker compose -f deploy/docker-compose.yml up -d |
| 40 | + |
| 41 | +# Build a container that includes TensorRT-LLM and KVBM. Note: KVBM integration is only available in TensorRT-LLM commit ce580ce4f52af3ad0043a800b3f9469e1f1109f6 or newer. |
| 42 | +./container/build.sh --framework trtllm --tensorrtllm-commit ce580ce4f52af3ad0043a800b3f9469e1f1109f6 --enable-kvbm |
| 43 | + |
| 44 | +# launch the container |
| 45 | +./container/run.sh --framework trtllm -it --mount-workspace --use-nixl-gds |
| 46 | + |
| 47 | +# enable kv offloading to CPU memory |
| 48 | +# 60 means 60GB of pinned CPU memory would be used |
| 49 | +export DYN_KVBM_CPU_CACHE_GB=60 |
| 50 | + |
| 51 | +# enable kv offloading to disk. Note: To enable disk cache offloading, you must first enable a CPU memory cache offloading. |
| 52 | +# 20 means 20GB of disk would be used |
| 53 | +export DYN_KVBM_DISK_CACHE_GB=20 |
| 54 | + |
| 55 | +# Allocating memory and disk storage can take some time. |
| 56 | +# We recommend setting a higher timeout for leader–worker initialization. |
| 57 | +# 1200 means 1200 seconds timeout |
| 58 | +export DYN_KVBM_LEADER_WORKER_INIT_TIMEOUT_SECS=1200 |
| 59 | +``` |
| 60 | + |
| 61 | +```bash |
| 62 | +# write an example LLM API config |
| 63 | +# Note: Disable partial reuse "enable_partial_reuse: false" in the LLM API config’s "kv_connector_config" to increase offloading cache hits. |
| 64 | +cat > "/tmp/kvbm_llm_api_config.yaml" <<EOF |
| 65 | +backend: pytorch |
| 66 | +cuda_graph_config: null |
| 67 | +kv_cache_config: |
| 68 | + enable_partial_reuse: false |
| 69 | + free_gpu_memory_fraction: 0.80 |
| 70 | +kv_connector_config: |
| 71 | + connector_module: dynamo.llm.trtllm_integration.connector |
| 72 | + connector_scheduler_class: DynamoKVBMConnectorLeader |
| 73 | + connector_worker_class: DynamoKVBMConnectorWorker |
| 74 | +EOF |
| 75 | + |
| 76 | +# start dynamo frontend |
| 77 | +python3 -m dynamo.frontend --http-port 8000 & |
| 78 | + |
| 79 | +# To serve an LLM model with dynamo |
| 80 | +python3 -m dynamo.trtllm \ |
| 81 | + --model-path deepseek-ai/DeepSeek-R1-Distill-Llama-8B \ |
| 82 | + --served-model-name deepseek-ai/DeepSeek-R1-Distill-Llama-8B \ |
| 83 | + --extra-engine-args /tmp/kvbm_llm_api_config.yaml & |
| 84 | + |
| 85 | +# make a call to LLM |
| 86 | +curl localhost:8000/v1/chat/completions -H "Content-Type: application/json" -d '{ |
| 87 | + "model": "deepseek-ai/DeepSeek-R1-Distill-Llama-8B", |
| 88 | + "messages": [ |
| 89 | + { |
| 90 | + "role": "user", |
| 91 | + "content": "In the heart of Eldoria, an ancient land of boundless magic and mysterious creatures, lies the long-forgotten city of Aeloria. Once a beacon of knowledge and power, Aeloria was buried beneath the shifting sands of time, lost to the world for centuries. You are an intrepid explorer, known for your unparalleled curiosity and courage, who has stumbled upon an ancient map hinting at ests that Aeloria holds a secret so profound that it has the potential to reshape the very fabric of reality. Your journey will take you through treacherous deserts, enchanted forests, and across perilous mountain ranges. Your Task: Character Background: Develop a detailed background for your character. Describe their motivations for seeking out Aeloria, their skills and weaknesses, and any personal connections to the ancient city or its legends. Are they driven by a quest for knowledge, a search for lost familt clue is hidden." |
| 92 | + } |
| 93 | + ], |
| 94 | + "stream":false, |
| 95 | + "max_tokens": 30 |
| 96 | + }' |
| 97 | + |
| 98 | +# Optionally, we could also serve an LLM with trtllm-serve to utilize the KVBM feature. |
| 99 | +trtllm-serve deepseek-ai/DeepSeek-R1-Distill-Llama-8B --host localhost --port 8001 --backend pytorch --extra_llm_api_options /tmp/kvbm_llm_api_config.yaml |
| 100 | + |
| 101 | +``` |
0 commit comments