|
| 1 | +# Copyright 2021, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions |
| 5 | +# are met: |
| 6 | +# * Redistributions of source code must retain the above copyright |
| 7 | +# notice, this list of conditions and the following disclaimer. |
| 8 | +# * Redistributions in binary form must reproduce the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer in the |
| 10 | +# documentation and/or other materials provided with the distribution. |
| 11 | +# * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 | +# contributors may be used to endorse or promote products derived |
| 13 | +# from this software without specific prior written permission. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +# triton_python_backend_utils is available in every Triton Python model. You |
| 28 | +# need to use this module to create inference requests and responses. It also |
| 29 | +# contains some utility functions for extracting information from model_config |
| 30 | +# and converting Triton input/output types to numpy types. |
| 31 | +import triton_python_backend_utils as pb_utils |
| 32 | + |
| 33 | + |
| 34 | +class TritonPythonModel: |
| 35 | + """Your Python model must use the same class name. Every Python model |
| 36 | + that is created must have "TritonPythonModel" as the class name. |
| 37 | + """ |
| 38 | + def initialize(self, args): |
| 39 | + """`initialize` is called only once when the model is being loaded. |
| 40 | + Implementing `initialize` function is optional. This function allows |
| 41 | + the model to intialize any state associated with this model. |
| 42 | +
|
| 43 | + Parameters |
| 44 | + ---------- |
| 45 | + args : dict |
| 46 | + Both keys and values are strings. The dictionary keys and values are: |
| 47 | + * model_config: A JSON string containing the model configuration |
| 48 | + * model_instance_kind: A string containing model instance kind |
| 49 | + * model_instance_device_id: A string containing model instance device ID |
| 50 | + * model_repository: Model repository path |
| 51 | + * model_version: Model version |
| 52 | + * model_name: Model name |
| 53 | + """ |
| 54 | + |
| 55 | + # You must parse model_config. JSON string is not parsed here |
| 56 | + self.model_config = json.loads(args['model_config']) |
| 57 | + |
| 58 | + def execute(self, requests): |
| 59 | + """`execute` must be implemented in every Python model. `execute` |
| 60 | + function receives a list of pb_utils.InferenceRequest as the only |
| 61 | + argument. This function is called when an inference request is made |
| 62 | + for this model. Depending on the batching configuration (e.g. Dynamic |
| 63 | + Batching) used, `requests` may contain multiple requests. Every |
| 64 | + Python model, must create one pb_utils.InferenceResponse for every |
| 65 | + pb_utils.InferenceRequest in `requests`. If there is an error, you can |
| 66 | + set the error argument when creating a pb_utils.InferenceResponse |
| 67 | +
|
| 68 | + Parameters |
| 69 | + ---------- |
| 70 | + requests : list |
| 71 | + A list of pb_utils.InferenceRequest |
| 72 | +
|
| 73 | + Returns |
| 74 | + ------- |
| 75 | + list |
| 76 | + A list of pb_utils.InferenceResponse. The length of this list must |
| 77 | + be the same as `requests` |
| 78 | + """ |
| 79 | + |
| 80 | + responses = [] |
| 81 | + # Every Python backend must iterate over everyone of the requests |
| 82 | + # and create a pb_utils.InferenceResponse for each of them. |
| 83 | + for request in requests: |
| 84 | + # Get INPUT0 |
| 85 | + in_0 = pb_utils.get_input_tensor_by_name(request, "INPUT0") |
| 86 | + |
| 87 | + # Get INPUT1 |
| 88 | + in_1 = pb_utils.get_input_tensor_by_name(request, "INPUT1") |
| 89 | + |
| 90 | + # Get Model Name |
| 91 | + model_name = pb_utils.get_input_tensor_by_name( |
| 92 | + request, "MODEL_NAME") |
| 93 | + |
| 94 | + # Model Name string |
| 95 | + model_name_string = model_name.as_numpy()[0] |
| 96 | + |
| 97 | + # Create inference request object |
| 98 | + infer_request = pb_utils.InferenceRequest( |
| 99 | + model_name=model_name_string, |
| 100 | + requested_output_names=["OUTPUT0", "OUTPUT1"], |
| 101 | + inputs=[in_0, in_1]) |
| 102 | + |
| 103 | + # Perform synchronous blocking inference request |
| 104 | + infer_response = infer_request.exec() |
| 105 | + |
| 106 | + # Make sure that the inference response doesn't have an error. If |
| 107 | + # it has an error, raise an exception. |
| 108 | + if infer_response.has_error(): |
| 109 | + raise pb_utils.TritonModelException( |
| 110 | + infer_response.error().message()) |
| 111 | + |
| 112 | + # Create InferenceResponse. You can set an error here in case |
| 113 | + # there was a problem with handling this inference request. |
| 114 | + # Below is an example of how you can set errors in inference |
| 115 | + # response: |
| 116 | + # |
| 117 | + # pb_utils.InferenceResponse( |
| 118 | + # output_tensors=..., TritonError("An error occured")) |
| 119 | + # |
| 120 | + # Because the infer_response of the models contains the final |
| 121 | + # outputs with correct output names, we can just pass the list |
| 122 | + # of outputs to the InferenceResponse object. |
| 123 | + inference_response = pb_utils.InferenceResponse( |
| 124 | + output_tensors=infer_response.output_tensors()) |
| 125 | + responses.append(inference_response) |
| 126 | + |
| 127 | + # You should return a list of pb_utils.InferenceResponse. Length |
| 128 | + # of this list must match the length of `requests` list. |
| 129 | + return responses |
| 130 | + |
| 131 | + def finalize(self): |
| 132 | + """`finalize` is called only once when the model is being unloaded. |
| 133 | + Implementing `finalize` function is OPTIONAL. This function allows |
| 134 | + the model to perform any necessary clean ups before exit. |
| 135 | + """ |
| 136 | + print('Cleaning up...') |
0 commit comments