|
| 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 sys |
| 28 | + |
| 29 | +sys.path.append("../../common") |
| 30 | + |
| 31 | +import json |
| 32 | +import unittest |
| 33 | + |
| 34 | +import numpy as np |
| 35 | +import shm_util |
| 36 | +import tritonclient.grpc as grpcclient |
| 37 | +from tritonclient.utils import InferenceServerException |
| 38 | + |
| 39 | + |
| 40 | +class ResponseParametersTest(unittest.TestCase): |
| 41 | + _server_address_grpc = "localhost:8001" |
| 42 | + _model_name = "response_parameters" |
| 43 | + _shape = [1, 1] |
| 44 | + |
| 45 | + def setUp(self): |
| 46 | + self._shm_leak_detector = shm_util.ShmLeakDetector() |
| 47 | + |
| 48 | + def _assert_response_parameters_match(self, infer_result, expected_params): |
| 49 | + res_params = {} |
| 50 | + for param_key, param_value in infer_result.get_response().parameters.items(): |
| 51 | + if param_value.HasField("bool_param"): |
| 52 | + value = param_value.bool_param |
| 53 | + elif param_value.HasField("int64_param"): |
| 54 | + value = param_value.int64_param |
| 55 | + elif param_value.HasField("string_param"): |
| 56 | + value = param_value.string_param |
| 57 | + else: |
| 58 | + raise ValueError(f"Unsupported parameter choice: {param_value}") |
| 59 | + res_params[param_key] = value |
| 60 | + self.assertEqual(expected_params, res_params) |
| 61 | + |
| 62 | + def _assert_response_parameters_infer_success(self, params): |
| 63 | + params_str = json.dumps(params) |
| 64 | + |
| 65 | + inputs = [grpcclient.InferInput("RESPONSE_PARAMETERS", self._shape, "BYTES")] |
| 66 | + inputs[0].set_data_from_numpy(np.array([[params_str]], dtype=np.object_)) |
| 67 | + |
| 68 | + with self._shm_leak_detector.Probe() as shm_probe: |
| 69 | + with grpcclient.InferenceServerClient(self._server_address_grpc) as client: |
| 70 | + result = client.infer(self._model_name, inputs) |
| 71 | + |
| 72 | + # verify the response parameters |
| 73 | + self._assert_response_parameters_match(result, params) |
| 74 | + |
| 75 | + # model returns the input as output |
| 76 | + output = str(result.as_numpy("OUTPUT")[0][0], encoding="utf-8") |
| 77 | + self.assertEqual(params_str, output) |
| 78 | + |
| 79 | + def _assert_response_parameters_infer_fail(self, params, expected_err_msg): |
| 80 | + params_str = json.dumps(params) |
| 81 | + |
| 82 | + inputs = [grpcclient.InferInput("RESPONSE_PARAMETERS", self._shape, "BYTES")] |
| 83 | + inputs[0].set_data_from_numpy(np.array([[params_str]], dtype=np.object_)) |
| 84 | + |
| 85 | + with self._shm_leak_detector.Probe() as shm_probe: |
| 86 | + with grpcclient.InferenceServerClient(self._server_address_grpc) as client: |
| 87 | + with self.assertRaises(InferenceServerException) as e: |
| 88 | + client.infer(self._model_name, inputs) |
| 89 | + |
| 90 | + self.assertIn("[StatusCode.INVALID_ARGUMENT] ", str(e.exception)) |
| 91 | + self.assertIn(expected_err_msg, str(e.exception)) |
| 92 | + |
| 93 | + def test_setting_empty_response_parameters(self): |
| 94 | + params = {} |
| 95 | + self._assert_response_parameters_infer_success(params) |
| 96 | + |
| 97 | + def test_setting_one_element_response_parameters(self): |
| 98 | + params = {"many_elements": False} |
| 99 | + self._assert_response_parameters_infer_success(params) |
| 100 | + |
| 101 | + def test_setting_three_element_response_parameters(self): |
| 102 | + params = {"bool": True, "str": "Hello World!", "int": 1024} |
| 103 | + self._assert_response_parameters_infer_success(params) |
| 104 | + |
| 105 | + def test_setting_multi_element_response_parameters(self): |
| 106 | + params = {"a": "1", "b": "2", "c": 3, "d": False, "e": 5, "f": ""} |
| 107 | + self._assert_response_parameters_infer_success(params) |
| 108 | + |
| 109 | + def test_setting_wrong_type_response_parameters(self): |
| 110 | + params = [] |
| 111 | + expected_err_msg = ", got <class 'list'>" |
| 112 | + self._assert_response_parameters_infer_fail(params, expected_err_msg) |
| 113 | + |
| 114 | + def test_setting_int_key_type_response_parameters(self): |
| 115 | + params = {"1": "int key"} |
| 116 | + expected_err_msg = ( |
| 117 | + "Expect parameters keys to have type str, found type <class 'int'>" |
| 118 | + ) |
| 119 | + self._assert_response_parameters_infer_fail(params, expected_err_msg) |
| 120 | + |
| 121 | + def test_setting_float_response_parameters(self): |
| 122 | + params = {"int": 2, "float": 0.5} |
| 123 | + expected_err_msg = "Expect parameters values to have type bool/int/str, found type <class 'float'>" |
| 124 | + self._assert_response_parameters_infer_fail(params, expected_err_msg) |
| 125 | + |
| 126 | + def test_setting_null_response_parameters(self): |
| 127 | + params = {"bool": True, "null": None} |
| 128 | + expected_err_msg = "Expect parameters values to have type bool/int/str, found type <class 'NoneType'>" |
| 129 | + self._assert_response_parameters_infer_fail(params, expected_err_msg) |
| 130 | + |
| 131 | + def test_setting_nested_response_parameters(self): |
| 132 | + params = {"str": "", "list": ["variable"]} |
| 133 | + expected_err_msg = "Expect parameters values to have type bool/int/str, found type <class 'list'>" |
| 134 | + self._assert_response_parameters_infer_fail(params, expected_err_msg) |
| 135 | + |
| 136 | + def test_setting_response_parameters_decoupled(self): |
| 137 | + model_name = "response_parameters_decoupled" |
| 138 | + params = [{"bool": False, "int": 2048}, {"str": "Hello World!"}] |
| 139 | + params_str = json.dumps(params) |
| 140 | + |
| 141 | + inputs = [grpcclient.InferInput("RESPONSE_PARAMETERS", self._shape, "BYTES")] |
| 142 | + inputs[0].set_data_from_numpy(np.array([[params_str]], dtype=np.object_)) |
| 143 | + |
| 144 | + responses = [] |
| 145 | + with self._shm_leak_detector.Probe() as shm_probe: |
| 146 | + with grpcclient.InferenceServerClient(self._server_address_grpc) as client: |
| 147 | + client.start_stream( |
| 148 | + callback=(lambda result, error: responses.append((result, error))) |
| 149 | + ) |
| 150 | + client.async_stream_infer(model_name=model_name, inputs=inputs) |
| 151 | + client.stop_stream() |
| 152 | + |
| 153 | + self.assertEqual(len(params), len(responses)) |
| 154 | + for i in range(len(params)): |
| 155 | + result, error = responses[i] |
| 156 | + self.assertIsNone(error) |
| 157 | + |
| 158 | + # Since this is a decoupled model, the 'triton_final_response' parameter |
| 159 | + # will be a part of the response parameters, so include it into the expected |
| 160 | + # parameters. The model sends the complete final flag separately from the |
| 161 | + # response, so the parameter is always False. |
| 162 | + expected_params = params[i].copy() |
| 163 | + expected_params["triton_final_response"] = False |
| 164 | + self._assert_response_parameters_match(result, expected_params) |
| 165 | + |
| 166 | + output = str(result.as_numpy("OUTPUT")[0][0], encoding="utf-8") |
| 167 | + self.assertEqual(json.dumps(params[i]), output) |
| 168 | + |
| 169 | + |
| 170 | +if __name__ == "__main__": |
| 171 | + unittest.main() |
0 commit comments