|
| 1 | +<!-- |
| 2 | +# Copyright 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions |
| 6 | +# are met: |
| 7 | +# * Redistributions of source code must retain the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer. |
| 9 | +# * Redistributions in binary form must reproduce the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer in the |
| 11 | +# documentation and/or other materials provided with the distribution. |
| 12 | +# * Neither the name of NVIDIA CORPORATION nor the names of its |
| 13 | +# contributors may be used to endorse or promote products derived |
| 14 | +# from this software without specific prior written permission. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 17 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 19 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 20 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 23 | +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 24 | +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | +--> |
| 28 | + |
| 29 | +# Deploying a GPT-2 Model using Python Backend and Iterative Scheduling |
| 30 | + |
| 31 | +In this tutorial, we will deploy a GPT-2 model using the Python backend and |
| 32 | +demonstrate the |
| 33 | +[iterative scheduling](https://docs.nvidia.com/deeplearning/triton-inference-server/user-guide/docs/user_guide/model_configuration.html#iterative-sequences) |
| 34 | +feature. |
| 35 | + |
| 36 | +## Prerequisites |
| 37 | + |
| 38 | +Before getting started with this tutorial, make sure you're familiar |
| 39 | +with the following concepts: |
| 40 | + |
| 41 | +* [Triton-Server Quick Start](https://docs.nvidia.com/deeplearning/triton-inference-server/user-guide/docs/getting_started/quickstart.html) |
| 42 | +* [Python Backend](https://github.com/triton-inference-server/python_backend) |
| 43 | + |
| 44 | +## Iterative Scheduling |
| 45 | + |
| 46 | +Iterative scheduling is a technique that allows the Triton Inference Server to |
| 47 | +schedule the same request multiple times with the same input. This is useful for |
| 48 | +models that have an auto-regressive loop. Iterative scheduling enables Triton |
| 49 | +Server to implement inflight batching for your models and gives you the ability |
| 50 | +to combine new sequences as they are arriving with inflight sequences. |
| 51 | + |
| 52 | +## Tutorial Overview |
| 53 | + |
| 54 | +In this tutorial we deploy two models: |
| 55 | + |
| 56 | +* simple-gpt2: This model receives a batch of requests and proceeds to the next |
| 57 | +batch only when it is done generating tokens for the current batch. |
| 58 | + |
| 59 | +* iterative-gpt2: This model uses iterative scheduling to process |
| 60 | +new sequences in a batch even when it is still generating tokens for the |
| 61 | +previous sequences |
| 62 | + |
| 63 | +### Demo |
| 64 | + |
| 65 | +[](https://asciinema.org/a/TUZtHwZsYrJzHuZF7XCOj1Avx) |
| 66 | + |
| 67 | +### Step 1: Prepare the Server Environment |
| 68 | + |
| 69 | +* First, run the Triton Inference Server Container: |
| 70 | + |
| 71 | +``` |
| 72 | +# Replace yy.mm with year and month of release. Please use 24.04 release upward. |
| 73 | +docker run --gpus=all --name iterative-scheduling -it --shm-size=256m --rm -p8000:8000 -p8001:8001 -p8002:8002 -v ${PWD}:/workspace/ -v ${PWD}/model_repository:/models nvcr.io/nvidia/tritonserver:yy.mm-py3 bash |
| 74 | +``` |
| 75 | + |
| 76 | +* Next, install all the dependencies required by the models running in the |
| 77 | +python backend and login with your [huggingface token](https://huggingface.co/settings/tokens) |
| 78 | +(Account on [HuggingFace](https://huggingface.co/) is required). |
| 79 | + |
| 80 | +``` |
| 81 | +pip install transformers[torch] |
| 82 | +``` |
| 83 | + |
| 84 | +> [!NOTE] |
| 85 | +> Optional: If you want to avoid installing the dependencies each time you run the |
| 86 | +> container, you can run `docker commit iterative-scheduling iterative-scheduling-image` to save the container |
| 87 | +> and use that for subsequent runs. |
| 88 | +
|
| 89 | +Then, start the server: |
| 90 | + |
| 91 | +``` |
| 92 | +tritonserver --model-repository=/models |
| 93 | +``` |
| 94 | + |
| 95 | +### Step 2: Install the client side dependencies |
| 96 | + |
| 97 | +In another terminal install the client dependencies: |
| 98 | + |
| 99 | +``` |
| 100 | +pip3 install tritonclient[grpc] |
| 101 | +pip3 install tqdm |
| 102 | +``` |
| 103 | + |
| 104 | +### Step 3: Run the client |
| 105 | + |
| 106 | +The simple-gpt2 model doesn't use iterative scheduling and will proceed to the |
| 107 | +next batch only when it is done generating tokens for the current batch. |
| 108 | + |
| 109 | +Run the following command to start the client: |
| 110 | + |
| 111 | +``` |
| 112 | +python3 client/client.py --model simple-gpt2 |
| 113 | +``` |
| 114 | + |
| 115 | +As you can see, the tokens for one request are processed first before proceeding |
| 116 | +to the next request. |
| 117 | + |
| 118 | +Run `Ctrl+C` to stop the client. |
| 119 | + |
| 120 | + |
| 121 | +The iterative scheduler is able to incorporate new requests as they are arriving |
| 122 | +in the server. |
| 123 | + |
| 124 | +Run the following command to start the client: |
| 125 | +``` |
| 126 | +python3 client/client.py --model iterative-gpt2 |
| 127 | +``` |
| 128 | + |
| 129 | +As you can see, the tokens for both prompts are getting generated simultaneously. |
| 130 | + |
| 131 | +## Next Steps |
| 132 | + |
| 133 | +We plan to integrate KV-Cache with these models for better performance. Currently, |
| 134 | +the main goal of tutorial is to demonstrate how to use iterative scheduling with |
| 135 | +Python backend. |
0 commit comments