|
| 1 | +# Copyright 2026, 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 | +import json |
| 28 | + |
| 29 | +import numpy as np |
| 30 | +import triton_python_backend_utils as pb_utils |
| 31 | + |
| 32 | + |
| 33 | +class TritonPythonModel: |
| 34 | + def initialize(self, args): |
| 35 | + self.model_config = model_config = json.loads(args["model_config"]) |
| 36 | + |
| 37 | + output0_config = pb_utils.get_output_config_by_name(model_config, "OUTPUT0") |
| 38 | + output1_config = pb_utils.get_output_config_by_name(model_config, "OUTPUT1") |
| 39 | + |
| 40 | + self.output0_dtype = pb_utils.triton_string_to_numpy( |
| 41 | + output0_config["data_type"] |
| 42 | + ) |
| 43 | + self.output1_dtype = pb_utils.triton_string_to_numpy( |
| 44 | + output1_config["data_type"] |
| 45 | + ) |
| 46 | + |
| 47 | + def execute(self, requests): |
| 48 | + output0_dtype = self.output0_dtype |
| 49 | + output1_dtype = self.output1_dtype |
| 50 | + |
| 51 | + responses = [] |
| 52 | + for request in requests: |
| 53 | + in_0 = pb_utils.get_input_tensor_by_name(request, "INPUT0") |
| 54 | + in_1 = pb_utils.get_input_tensor_by_name(request, "INPUT1") |
| 55 | + if ( |
| 56 | + in_0.as_numpy().dtype.type is np.bytes_ |
| 57 | + or in_0.as_numpy().dtype == np.object_ |
| 58 | + ): |
| 59 | + out_0, out_1 = ( |
| 60 | + in_0.as_numpy().astype(np.int32) + in_1.as_numpy().astype(np.int32), |
| 61 | + in_0.as_numpy().astype(np.int32) - in_1.as_numpy().astype(np.int32), |
| 62 | + ) |
| 63 | + else: |
| 64 | + out_0, out_1 = ( |
| 65 | + in_0.as_numpy() + in_1.as_numpy(), |
| 66 | + in_0.as_numpy() - in_1.as_numpy(), |
| 67 | + ) |
| 68 | + |
| 69 | + out_tensor_0 = pb_utils.Tensor("OUTPUT0", out_0.astype(output0_dtype)) |
| 70 | + out_tensor_1 = pb_utils.Tensor("OUTPUT1", out_1.astype(output1_dtype)) |
| 71 | + responses.append(pb_utils.InferenceResponse([out_tensor_0, out_tensor_1])) |
| 72 | + return responses |
0 commit comments