|
| 1 | +<!-- |
| 2 | +# Copyright 2022, 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 | +# Building Complex Pipelines: Stable Diffusion |
| 30 | + |
| 31 | +*Note*: This tutorial aims at demonstrating the ease of deployment and doesn't incorporate all possible optimizations using the NVIDIA ecosystem. |
| 32 | + |
| 33 | +It is recommended to watch [this explainer video](https://youtu.be/JgP2WgNIq_w) with discusses the pipeline, before proceeding with the example. This example focuses on showcasing two of Triton Inference Server's features: |
| 34 | +* Using multiple frameworks in the same inference pipeline. Refer [this for more information](https://github.com/triton-inference-server/backend#where-can-i-find-all-the-backends-that-are-available-for-triton) about supported frameworks. |
| 35 | +* Using the Python Backend's [Business Logic Scripting](https://github.com/triton-inference-server/python_backend#business-logic-scripting) API to build complex non linear pipelines. |
| 36 | + |
| 37 | +## Using Multiple Backends |
| 38 | + |
| 39 | +Building a pipeline powered by deep learning models is a collaborative effort which often involves multiple contributors. Contributors often have differing development environment. This can lead to issues whilst building a single pipeline with work from different contributors. Triton users can solve this challenge with the use of the Python or a C++ backend along with the Business Logic Scripting API (BLS) API to trigger model execution. |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +In this example, the models are being run on: |
| 44 | +* ONNX Backend |
| 45 | +* TensorRT Backend |
| 46 | +* Python Backend |
| 47 | + |
| 48 | +Both the models deployed on a framework backend can be triggered using the following API: |
| 49 | +``` |
| 50 | +encoding_request = pb_utils.InferenceRequest( |
| 51 | + model_name="text_encoder", |
| 52 | + requested_output_names=["last_hidden_state"], |
| 53 | + inputs=[input_ids_1], |
| 54 | +) |
| 55 | +
|
| 56 | +response = encoding_request.exec() |
| 57 | +text_embeddings = pb_utils.get_output_tensor_by_name(response, "last_hidden_state") |
| 58 | +``` |
| 59 | + |
| 60 | +Refer to `model.py` in the `pipeline` model for a complete example. |
| 61 | + |
| 62 | +## Stable Diffusion Example |
| 63 | + |
| 64 | +Before starting, clone this repository and navigate to the root folder. Use three different terminals for an easier user experience. |
| 65 | + |
| 66 | +### Step 1: Prepare the Server Environment |
| 67 | +* First, run the Triton Inference Server Container. |
| 68 | +``` |
| 69 | +# Replace yy.mm with year and month of release. Eg. 22.08 |
| 70 | +docker run --gpus=all -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 |
| 71 | +``` |
| 72 | +* Next, install all the dependencies required by the models running in the python backend and login with your [huggingface token](https://huggingface.co/settings/tokens)(Account on [HuggingFace](https://huggingface.co/) is required). |
| 73 | + |
| 74 | +``` |
| 75 | +# PyTorch & Transformers Lib |
| 76 | +pip install torch torchvision torchaudio |
| 77 | +pip install transformers ftfy scipy accelerate |
| 78 | +pip install diffusers==0.9.0 |
| 79 | +pip install transformers[onnxruntime] |
| 80 | +huggingface-cli login |
| 81 | +``` |
| 82 | + |
| 83 | +### Step 2: Exporting and converting the models |
| 84 | +Use the NGC PyTorch container, to export and convert the models. |
| 85 | + |
| 86 | +``` |
| 87 | +docker run -it --gpus all -p 8888:8888 -v ${PWD}:/mount nvcr.io/nvidia/pytorch:yy.mm-py3 |
| 88 | +
|
| 89 | +pip install transformers ftfy scipy |
| 90 | +pip install transformers[onnxruntime] |
| 91 | +pip install diffusers==0.9.0 |
| 92 | +huggingface-cli login |
| 93 | +cd /mount |
| 94 | +python export.py |
| 95 | +
|
| 96 | +# Accelerating VAE with TensorRT |
| 97 | +trtexec --onnx=vae.onnx --saveEngine=vae.plan --minShapes=latent_sample:1x4x64x64 --optShapes=latent_sample:4x4x64x64 --maxShapes=latent_sample:8x4x64x64 --fp16 |
| 98 | +
|
| 99 | +# Place the models in the model repository |
| 100 | +mkdir model_repository/vae/1 |
| 101 | +mkdir model_repository/text_encoder/1 |
| 102 | +mv vae.plan model_repository/vae/1/model.plan |
| 103 | +mv encoder.onnx model_repository/text_encoder/1/model.onnx |
| 104 | +``` |
| 105 | + |
| 106 | +### Step 3: Launch the Server |
| 107 | +From the server container, launch the Triton Inference Server. |
| 108 | +``` |
| 109 | +tritonserver --model-repository=/models |
| 110 | +``` |
| 111 | + |
| 112 | +### Step 4: Run the client |
| 113 | +Use the client container and run the client. |
| 114 | +``` |
| 115 | +docker run -it --net=host -v ${PWD}:/workspace/ nvcr.io/nvidia/tritonserver:yy.mm-py3-sdk bash |
| 116 | +
|
| 117 | +# Client with no GUI |
| 118 | +python3 client.py |
| 119 | +
|
| 120 | +# Client with GUI |
| 121 | +pip install gradio packaging |
| 122 | +python3 gui/client.py --triton_url="localhost:8001" |
| 123 | +``` |
| 124 | +Note: First Inference query may take more time than successive queries |
0 commit comments