Spaces:
Sleeping
Sleeping
File size: 5,241 Bytes
e3af00f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example of using Triton Server Wrapper with RAPIDS/CuPy library in Jupyter Notebook"
]
},
{
"cell_type": "markdown",
"source": [
"### Pure Python/CuPy and Triton Wrapper equivalent of The RAPIDS-Triton Linear Example:\n",
" <a href=\"https://github.com/rapidsai/rapids-triton-linear-example#the-rapids-triton-linear-example\">https://github.com/rapidsai/rapids-triton-linear-example#the-rapids-triton-linear-example</a>\n",
" (Remark: Above example is focused on latency minimization - our equivalent is focused on easy of use)"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%% md\n"
}
}
},
{
"cell_type": "markdown",
"source": [
"## Triton server setup with custom linear model"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "markdown",
"source": [
"Install dependencies"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"import sys\n",
"!{sys.executable} -m pip install numpy"
],
"metadata": {
"collapsed": false,
"pycharm": {
"name": "#%%\n"
}
}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Required imports:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"pycharm": {
"name": "#%%\n"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import cupy as cp\n",
"\n",
"from pytriton.model_config import ModelConfig, Tensor\n",
"from pytriton.triton import Triton\n",
"from pytriton.decorators import batch"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define linear model (for simplicity, sample model parameters are defined in class initializer):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"VECTOR_SIZE = 10\n",
"\n",
"class LinearModel:\n",
" def __init__(self):\n",
" self.alpha = 2\n",
" self.beta = cp.arange(VECTOR_SIZE)\n",
"\n",
" @batch\n",
" def linear(self, **inputs):\n",
" u_batch, v_batch = inputs.values()\n",
" u_batch_cp, v_batch_cp = cp.asarray(u_batch), cp.asarray(v_batch)\n",
" lin = u_batch_cp * self.alpha + v_batch_cp + self.beta\n",
" return {\"lin\": cp.asnumpy(lin)}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Instantiate titon wrapper class and load model with defined callable:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triton = Triton()\n",
"lin_model = LinearModel()\n",
"triton.bind(\n",
" model_name=\"Linear\",\n",
" infer_func=lin_model.linear,\n",
" inputs=[\n",
" Tensor(dtype=np.float64, shape=(VECTOR_SIZE,)),\n",
" Tensor(dtype=np.float64, shape=(VECTOR_SIZE,)),\n",
" ],\n",
" outputs=[\n",
" Tensor(name=\"lin\", dtype=np.float64, shape=(-1,)),\n",
" ],\n",
" config=ModelConfig(max_batch_size=128),\n",
" strict=True,\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run triton server with defined model inference callable"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triton.run()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example inference performed with ModelClient calling triton server"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pytriton.client import ModelClient\n",
"\n",
"VECTOR_SIZE = 10\n",
"BATCH_SIZE = 2\n",
"\n",
"u_batch = np.ones((BATCH_SIZE, VECTOR_SIZE), dtype=np.float64)\n",
"v_batch = np.ones((BATCH_SIZE, VECTOR_SIZE), dtype=np.float64)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with ModelClient(\"localhost\", \"Linear\") as client:\n",
" result_batch = client.infer_batch(u_batch, v_batch)\n",
"\n",
"for output_name, data_batch in result_batch.items():\n",
" print(f\"{output_name}: {data_batch.tolist()}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Stop triton server at the end"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triton.stop()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
|