Skip to content

Commit c4fb713

Browse files
authored
Fix model imports (triton-inference-server#25)
* Fix model imports * Update documentation
1 parent 4c9f209 commit c4fb713

4 files changed

Lines changed: 28 additions & 43 deletions

File tree

README.md

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,25 @@ $ cp examples/add_sub.py models/add_sub/1/model.py
5555
$ cp examples/config.pbtxt models/add_sub/config.pbtxt
5656
```
5757

58-
4. Copy `triton_python_backend_utils.py`
59-
60-
```
61-
$ cp src/resources/triton_python_backend_utils.py models/add_sub/1/
62-
```
63-
64-
5. Start the Triton server.
58+
4. Start the Triton server.
6559

6660
```
6761
$ tritonserver --model-repository `pwd`/models
6862
```
6963

70-
6. In the host machine, start the client container.
64+
5. In the host machine, start the client container.
7165

7266
```
73-
docker run -ti --net host nvcr.io/nvidia/tritonserver:20.10-py3-clientsdk /bin/bash
67+
docker run -ti --net host nvcr.io/nvidia/tritonserver:20.10-py3-sdk /bin/bash
7468
```
7569

76-
7. In the client container, clone the Python backend repository.
70+
6. In the client container, clone the Python backend repository.
7771

7872
```
7973
$ git clone https://github.com/triton-inference-server/python_backend -b r20.10
8074
```
8175

82-
8. Run the example client.
76+
7. Run the example client.
8377
```
8478
$ python3 python_backend/examples/add_sub_client.py
8579
```
@@ -122,20 +116,14 @@ but the listed CMake argument can be used to override.
122116
Set `DCMAKE_INSTALL_PREFIX` to the location where the Triton Server is installed. In the released containers,
123117
this location is `/opt/tritonserver`.
124118

125-
2. Copy example model and configuration
119+
3. Copy example model and configuration
126120

127121
```
128122
$ mkdir -p models/add_sub/1/
129123
$ cp examples/add_sub/model.py models/add_sub/1/model.py
130124
$ cp examples/add_sub/config.pbtxt models/add_sub/config.pbtxt
131125
```
132126

133-
3. Copy `triton_python_backend_utils.py`
134-
135-
```
136-
$ cp src/resources/triton_python_backend_utils.py models/add_sub/1/
137-
```
138-
139127
4. Start the Triton Server
140128

141129
```
@@ -287,23 +275,15 @@ the model configuration. In order to use this backend you must set the `backend`
287275
field of your model `config.pbtxt` file to `python`. You shouldn't set
288276
`platform` field of the configuration.
289277

290-
Also, you need to make a copy of
291-
[triton_python_backend_utils.py](src/resources/triton_python_backend_utils.py)
292-
available to your `model.py`.
293-
294278
Your models directory should look like below:
295279
```
296280
models
297281
└── add_sub
298282
├── 1
299-
│ ├── model.py
300-
│ └── triton_python_backend_utils.py
283+
│ └── model.py
301284
└── config.pbtxt
302285
```
303286

304-
It is recommended to have one copy of `triton_python_backend_utils.py` along
305-
with every `model.py` file like the tree structure shown above.
306-
307287
## Changing Python Runtime Path
308288

309289
Python backend by default uses `python3` available inside `PATH`. In order to change

examples/add_sub/model.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,10 @@
2828
import sys
2929
import json
3030

31-
# You need to include triton_python_backend_utils here to be able to work with
32-
# inference requests and responses. It also contains some utility functions for
33-
# extracting information from model_config and converting Triton input/output
34-
# types to numpy types. You must copy
35-
# src/resources/triton_python_backend_utils.py in the appropriate location so
36-
# that the import below works properly.
31+
# triton_python_backend_utils is available in every Triton Python model. You
32+
# need to use this module to create inference requests and responses. It also
33+
# contains some utility functions for extracting information from model_config
34+
# and converting Triton input/output types to numpy types.
3735
import triton_python_backend_utils as pb_utils
3836

3937

examples/pytorch/model.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@
2929
import json
3030
from torch import nn
3131

32-
# You need to include triton_python_backend_utils here to be able to work with
33-
# inference requests and responses. It also contains some utility functions for
34-
# extracting information from model_config and converting Triton input/output
35-
# types to numpy types. You must copy
36-
# src/resources/triton_python_backend_utils.py in the appropriate location so
37-
# that the import below works properly.
32+
# triton_python_backend_utils is available in every Triton Python model. You
33+
# need to use this module to create inference requests and responses. It also
34+
# contains some utility functions for extracting information from model_config
35+
# and converting Triton input/output types to numpy types.
3836
import triton_python_backend_utils as pb_utils
3937

4038

@@ -53,7 +51,6 @@ def forward(self, input0, input1):
5351
return (input0 + input1), (input0 - input1)
5452

5553

56-
5754
class TritonPythonModel:
5855
"""Your Python model must use the same class name. Every Python model
5956
that is created must have "TritonPythonModel" as the class name.
@@ -92,7 +89,7 @@ def initialize(self, args):
9289
output0_config['data_type'])
9390
self.output1_dtype = pb_utils.triton_string_to_numpy(
9491
output1_config['data_type'])
95-
92+
9693
# Instantiate the PyTorch model
9794
self.add_sub_model = AddSubNet()
9895

src/resources/startup.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import time
3636
import struct
3737
import traceback
38+
from pathlib import Path
3839

3940
from grpc_channelz.v1 import channelz
4041
from grpc_channelz.v1 import channelz_pb2
@@ -155,10 +156,19 @@ class PythonHost(PythonInterpreterServicer):
155156

156157
def __init__(self, module_path, *args, **kwargs):
157158
super(PythonInterpreterServicer, self).__init__(*args, **kwargs)
158-
spec = importlib.util.spec_from_file_location('TritonPythonModel',
159-
module_path)
159+
160+
module_path = Path(module_path).resolve()
161+
# Add model parent directories so that relative and absolute import work
162+
sys.path.append(str(module_path.parent))
163+
sys.path.append(str(module_path.parent.parent))
164+
165+
# We need to import the parent directory of the module
166+
# so that the relative imports work too.
167+
spec = importlib.util.spec_from_file_location(
168+
f'{module_path.parent.name}.{module_path.name}', str(module_path))
160169
module = importlib.util.module_from_spec(spec)
161170
spec.loader.exec_module(module)
171+
162172
self.module_path = module_path
163173

164174
if hasattr(module, 'TritonPythonModel'):

0 commit comments

Comments
 (0)