|
| 1 | +# Copyright 2024, 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 | +import unittest |
| 29 | + |
| 30 | +import numpy as np |
| 31 | +import tritonclient.grpc as grpcclient |
| 32 | + |
| 33 | + |
| 34 | +class InferTest(unittest.TestCase): |
| 35 | + _grpc_url = "localhost:8001" |
| 36 | + _model_name = "vllm_opt" |
| 37 | + _sampling_parameters = {"temperature": "0", "top_p": "1"} |
| 38 | + _prompt = "In this example," |
| 39 | + |
| 40 | + def _get_inputs( |
| 41 | + self, |
| 42 | + prompt, |
| 43 | + stream=True, |
| 44 | + sampling_parameters=None, |
| 45 | + output_finish_reason=None, |
| 46 | + output_cumulative_logprob=None, |
| 47 | + output_num_token_ids=None, |
| 48 | + ): |
| 49 | + inputs = [] |
| 50 | + |
| 51 | + inputs.append(grpcclient.InferInput("text_input", [1], "BYTES")) |
| 52 | + inputs[-1].set_data_from_numpy( |
| 53 | + np.array([prompt.encode("utf-8")], dtype=np.object_) |
| 54 | + ) |
| 55 | + |
| 56 | + inputs.append(grpcclient.InferInput("stream", [1], "BOOL")) |
| 57 | + inputs[-1].set_data_from_numpy(np.array([stream], dtype=bool)) |
| 58 | + |
| 59 | + if sampling_parameters is not None: |
| 60 | + inputs.append(grpcclient.InferInput("sampling_parameters", [1], "BYTES")) |
| 61 | + inputs[-1].set_data_from_numpy( |
| 62 | + np.array( |
| 63 | + [json.dumps(sampling_parameters).encode("utf-8")], dtype=np.object_ |
| 64 | + ) |
| 65 | + ) |
| 66 | + |
| 67 | + if output_finish_reason is not None: |
| 68 | + inputs.append(grpcclient.InferInput("output_finish_reason", [1], "BOOL")) |
| 69 | + inputs[-1].set_data_from_numpy(np.array([output_finish_reason], dtype=bool)) |
| 70 | + |
| 71 | + if output_cumulative_logprob is not None: |
| 72 | + inputs.append( |
| 73 | + grpcclient.InferInput("output_cumulative_logprob", [1], "BOOL") |
| 74 | + ) |
| 75 | + inputs[-1].set_data_from_numpy( |
| 76 | + np.array([output_cumulative_logprob], dtype=bool) |
| 77 | + ) |
| 78 | + |
| 79 | + if output_num_token_ids is not None: |
| 80 | + inputs.append(grpcclient.InferInput("output_num_token_ids", [1], "BOOL")) |
| 81 | + inputs[-1].set_data_from_numpy(np.array([output_num_token_ids], dtype=bool)) |
| 82 | + |
| 83 | + return inputs |
| 84 | + |
| 85 | + def _callback(self, result, error): |
| 86 | + self._responses.append({"result": result, "error": error}) |
| 87 | + |
| 88 | + def _llm_infer(self, inputs): |
| 89 | + self._responses = [] |
| 90 | + with grpcclient.InferenceServerClient(self._grpc_url) as client: |
| 91 | + client.start_stream(self._callback) |
| 92 | + client.async_stream_infer( |
| 93 | + self._model_name, inputs=inputs, parameters=self._sampling_parameters |
| 94 | + ) |
| 95 | + client.stop_stream() |
| 96 | + |
| 97 | + def _assert_text_output_valid(self): |
| 98 | + text_output = "" |
| 99 | + for response in self._responses: |
| 100 | + result, error = response["result"], response["error"] |
| 101 | + self.assertIsNone(error) |
| 102 | + text_output += result.as_numpy(name="text_output")[0].decode("utf-8") |
| 103 | + self.assertGreater(len(text_output), 0, "output is empty") |
| 104 | + self.assertGreater(text_output.count(" "), 4, "output is not a sentence") |
| 105 | + |
| 106 | + def _assert_finish_reason(self, output_finish_reason): |
| 107 | + for i in range(len(self._responses)): |
| 108 | + result, error = self._responses[i]["result"], self._responses[i]["error"] |
| 109 | + self.assertIsNone(error) |
| 110 | + finish_reason_np = result.as_numpy(name="finish_reason") |
| 111 | + if output_finish_reason is None or output_finish_reason == False: |
| 112 | + self.assertIsNone(finish_reason_np) |
| 113 | + continue |
| 114 | + finish_reason = finish_reason_np[0].decode("utf-8") |
| 115 | + if i < len(self._responses) - 1: |
| 116 | + self.assertEqual(finish_reason, "None") |
| 117 | + else: |
| 118 | + self.assertEqual(finish_reason, "length") |
| 119 | + |
| 120 | + def _assert_cumulative_logprob(self, output_cumulative_logprob): |
| 121 | + prev_cumulative_logprob = 0.0 |
| 122 | + for response in self._responses: |
| 123 | + result, error = response["result"], response["error"] |
| 124 | + self.assertIsNone(error) |
| 125 | + cumulative_logprob_np = result.as_numpy(name="cumulative_logprob") |
| 126 | + if output_cumulative_logprob is None or output_cumulative_logprob == False: |
| 127 | + self.assertIsNone(cumulative_logprob_np) |
| 128 | + continue |
| 129 | + cumulative_logprob = cumulative_logprob_np[0].astype(float) |
| 130 | + self.assertNotEqual(cumulative_logprob, prev_cumulative_logprob) |
| 131 | + prev_cumulative_logprob = cumulative_logprob |
| 132 | + |
| 133 | + def _assert_num_token_ids(self, output_num_token_ids): |
| 134 | + for response in self._responses: |
| 135 | + result, error = response["result"], response["error"] |
| 136 | + self.assertIsNone(error) |
| 137 | + num_token_ids_np = result.as_numpy(name="num_token_ids") |
| 138 | + if output_num_token_ids is None or output_num_token_ids == False: |
| 139 | + self.assertIsNone(num_token_ids_np) |
| 140 | + continue |
| 141 | + num_token_ids = num_token_ids_np[0].astype(int) |
| 142 | + self.assertGreater(num_token_ids, 0) |
| 143 | + |
| 144 | + def _assert_additional_outputs_valid( |
| 145 | + self, |
| 146 | + stream, |
| 147 | + output_finish_reason, |
| 148 | + output_cumulative_logprob, |
| 149 | + output_num_token_ids, |
| 150 | + ): |
| 151 | + inputs = self._get_inputs( |
| 152 | + self._prompt, |
| 153 | + stream=stream, |
| 154 | + sampling_parameters=self._sampling_parameters, |
| 155 | + output_finish_reason=output_finish_reason, |
| 156 | + output_cumulative_logprob=output_cumulative_logprob, |
| 157 | + output_num_token_ids=output_num_token_ids, |
| 158 | + ) |
| 159 | + self._llm_infer(inputs) |
| 160 | + self._assert_text_output_valid() |
| 161 | + self._assert_finish_reason(output_finish_reason) |
| 162 | + self._assert_cumulative_logprob(output_cumulative_logprob) |
| 163 | + self._assert_num_token_ids(output_num_token_ids) |
| 164 | + |
| 165 | + def test_additional_outputs(self): |
| 166 | + for stream in [True, False]: |
| 167 | + choices = [None, False, True] |
| 168 | + for output_finish_reason in choices: |
| 169 | + for output_cumulative_logprob in choices: |
| 170 | + for output_num_token_ids in choices: |
| 171 | + self._assert_additional_outputs_valid( |
| 172 | + stream, |
| 173 | + output_finish_reason, |
| 174 | + output_cumulative_logprob, |
| 175 | + output_num_token_ids, |
| 176 | + ) |
| 177 | + |
| 178 | + |
| 179 | +if __name__ == "__main__": |
| 180 | + unittest.main() |
0 commit comments