|
| 1 | +# Copyright 2025, 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 | + """ |
| 35 | + This model (A) is designed to test sending back response parameters when using BLS. |
| 36 | + It takes one input tensor, which is the RESPONSE_PARAMETERS and uses BLS to |
| 37 | + call response_parameters model (B). Model B would set RESPONSE_PARAMETERS (with a bit |
| 38 | + of data massage) as its response parameters. In the end, model A would also set its |
| 39 | + response parameters from model B's response parameters. |
| 40 | +
|
| 41 | + With above model set up, we can easily test whether the real response parameters are |
| 42 | + the same as the input response parameters. |
| 43 | + """ |
| 44 | + |
| 45 | + def execute(self, requests): |
| 46 | + responses = [] |
| 47 | + |
| 48 | + for request in requests: |
| 49 | + passed = True |
| 50 | + |
| 51 | + # test bls response parameters from a regular model |
| 52 | + res_params_tensor = pb_utils.get_input_tensor_by_name( |
| 53 | + request, "RESPONSE_PARAMETERS" |
| 54 | + ).as_numpy() |
| 55 | + res_params_str = str(res_params_tensor[0][0], encoding="utf-8") |
| 56 | + res_params = json.loads(res_params_str) |
| 57 | + bls_input_tensor = pb_utils.Tensor("RESPONSE_PARAMETERS", res_params_tensor) |
| 58 | + bls_req = pb_utils.InferenceRequest( |
| 59 | + model_name="response_parameters", |
| 60 | + inputs=[bls_input_tensor], |
| 61 | + requested_output_names=["OUTPUT"], |
| 62 | + ) |
| 63 | + bls_res = bls_req.exec() # decoupled=False |
| 64 | + bls_res_params_str = bls_res.parameters() |
| 65 | + bls_res_params = ( |
| 66 | + json.loads(bls_res_params_str) if bls_res_params_str != "" else {} |
| 67 | + ) |
| 68 | + passed = passed and bls_res_params == res_params |
| 69 | + |
| 70 | + # test bls response parameters from a decoupled model |
| 71 | + res_params_decoupled_tensor = pb_utils.get_input_tensor_by_name( |
| 72 | + request, "RESPONSE_PARAMETERS_DECOUPLED" |
| 73 | + ).as_numpy() |
| 74 | + res_params_decoupled_str = str( |
| 75 | + res_params_decoupled_tensor[0][0], encoding="utf-8" |
| 76 | + ) |
| 77 | + res_params_decoupled = json.loads(res_params_decoupled_str) |
| 78 | + bls_decoupled_input_tensor = pb_utils.Tensor( |
| 79 | + "RESPONSE_PARAMETERS", res_params_decoupled_tensor |
| 80 | + ) # response_parameters_decoupled model input name is RESPONSE_PARAMETERS |
| 81 | + bls_decoupled_req = pb_utils.InferenceRequest( |
| 82 | + model_name="response_parameters_decoupled", |
| 83 | + inputs=[bls_decoupled_input_tensor], |
| 84 | + requested_output_names=["OUTPUT"], |
| 85 | + ) |
| 86 | + bls_decoupled_res = bls_decoupled_req.exec(decoupled=True) |
| 87 | + for bls_decoupled_r in bls_decoupled_res: |
| 88 | + if len(bls_decoupled_r.output_tensors()) == 0: |
| 89 | + break # meaning reached final response |
| 90 | + bls_decoupled_r_params_str = bls_decoupled_r.parameters() |
| 91 | + bls_decoupled_r_params = ( |
| 92 | + json.loads(bls_decoupled_r_params_str) |
| 93 | + if bls_decoupled_r_params_str != "" |
| 94 | + else {} |
| 95 | + ) |
| 96 | + passed = passed and bls_decoupled_r_params in res_params_decoupled |
| 97 | + res_params_decoupled.remove(bls_decoupled_r_params) |
| 98 | + passed = passed and len(res_params_decoupled) == 0 |
| 99 | + |
| 100 | + output_tensor = pb_utils.Tensor( |
| 101 | + "OUTPUT", np.array([[str(passed)]], dtype=np.object_) |
| 102 | + ) |
| 103 | + response = pb_utils.InferenceResponse(output_tensors=[output_tensor]) |
| 104 | + responses.append(response) |
| 105 | + |
| 106 | + return responses |
0 commit comments