|
| 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 | +# JAX Example |
| 30 | + |
| 31 | +In this section, we demonstrate an end-to-end example for using |
| 32 | +[JAX](https://jax.readthedocs.io/en/latest/) in Python Backend. |
| 33 | + |
| 34 | +## Create a JAX AddSub model repository |
| 35 | + |
| 36 | +We will use the files that come with this example to create the model |
| 37 | +repository. |
| 38 | + |
| 39 | +First, download the [client.py](client.py), [config.pbtxt](config.pbtxt) and |
| 40 | +[model.py](model.py) to your local machine. |
| 41 | + |
| 42 | +Next, at the directory where the three files located, create the model |
| 43 | +repository with the following commands: |
| 44 | +``` |
| 45 | +$ mkdir -p models/jax/1 |
| 46 | +$ mv model.py models/jax/1 |
| 47 | +$ mv config.pbtxt models/jax |
| 48 | +``` |
| 49 | + |
| 50 | +## Pull the Triton Docker images |
| 51 | + |
| 52 | +We need to install Docker and NVIDIA Container Toolkit before proceeding, refer |
| 53 | +to the |
| 54 | +[installation steps](https://github.com/triton-inference-server/server/tree/main/docs#installation). |
| 55 | + |
| 56 | +To pull the latest containers, run the following commands: |
| 57 | +``` |
| 58 | +$ docker pull nvcr.io/nvidia/tritonserver:<yy.mm>-py3 |
| 59 | +$ docker pull nvcr.io/nvidia/tritonserver:<yy.mm>-py3-sdk |
| 60 | +``` |
| 61 | +See the installation steps above for the `<yy.mm>` version. |
| 62 | + |
| 63 | +At the time of writing, the latest version is `22.08`, which translates to the |
| 64 | +following commands: |
| 65 | +``` |
| 66 | +$ docker pull nvcr.io/nvidia/tritonserver:22.08-py3 |
| 67 | +$ docker pull nvcr.io/nvidia/tritonserver:22.08-py3-sdk |
| 68 | +``` |
| 69 | + |
| 70 | +Be sure to replace the `<yy.mm>` with the version pulled for all the remaining |
| 71 | +parts of this example. |
| 72 | + |
| 73 | +## Start the Triton Server |
| 74 | + |
| 75 | +At the directory where we created the JAX models (at where the "models" folder |
| 76 | +is located), run the following command: |
| 77 | +``` |
| 78 | +$ docker run --gpus all -it --rm -p 8000:8000 -v `pwd`:/jax nvcr.io/nvidia/tritonserver:<yy.mm>-py3 /bin/bash |
| 79 | +``` |
| 80 | + |
| 81 | +Inside the container, we need to install JAX to run this example. |
| 82 | + |
| 83 | +We recommend using the `pip` method mentioned in the |
| 84 | +[JAX documentation](https://github.com/google/jax#pip-installation-gpu-cuda). |
| 85 | +Make sure that JAX is available in the same Python environment as other |
| 86 | +dependencies. |
| 87 | + |
| 88 | +To install for this example, run the following command: |
| 89 | +``` |
| 90 | +$ pip3 install --upgrade "jax[cuda]" -f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html |
| 91 | +``` |
| 92 | + |
| 93 | +Finally, we need to start the Triton Server, run the following command: |
| 94 | +``` |
| 95 | +$ tritonserver --model-repository=/jax/models |
| 96 | +``` |
| 97 | + |
| 98 | +To leave the container for the next step, press: `CTRL + P + Q`. |
| 99 | + |
| 100 | +## Test inference |
| 101 | + |
| 102 | +At the directory where the client.py is located, run the following command: |
| 103 | +``` |
| 104 | +$ docker run --rm --net=host -v `pwd`:/jax nvcr.io/nvidia/tritonserver:<yy.mm>-py3-sdk python3 /jax/client.py |
| 105 | +``` |
| 106 | + |
| 107 | +A successful inference will print the following at the end: |
| 108 | +``` |
| 109 | +INPUT0 ([0.89262384 0.645457 0.18913145 0.17099917]) + INPUT1 ([0.5703733 0.21917151 0.22854741 0.97336507]) = OUTPUT0 ([1.4629972 0.86462855 0.41767886 1.1443642 ]) |
| 110 | +INPUT0 ([0.89262384 0.645457 0.18913145 0.17099917]) - INPUT1 ([0.5703733 0.21917151 0.22854741 0.97336507]) = OUTPUT0 ([ 0.32225055 0.4262855 -0.03941596 -0.8023659 ]) |
| 111 | +PASS: jax |
| 112 | +``` |
| 113 | +Note: You inputs can be different from the above, but the outputs always |
| 114 | +correspond to its inputs. |
0 commit comments