|
| 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 | + |
| 29 | +import numpy as np |
| 30 | +import pytest |
| 31 | +import tritonclient.grpc as grpcclient |
| 32 | + |
| 33 | + |
| 34 | +class TestAdditionalOutputs: |
| 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 | + return_finish_reason=None, |
| 46 | + return_cumulative_logprob=None, |
| 47 | + return_num_output_tokens=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 return_finish_reason is not None: |
| 68 | + inputs.append(grpcclient.InferInput("return_finish_reason", [1], "BOOL")) |
| 69 | + inputs[-1].set_data_from_numpy(np.array([return_finish_reason], dtype=bool)) |
| 70 | + |
| 71 | + if return_cumulative_logprob is not None: |
| 72 | + inputs.append( |
| 73 | + grpcclient.InferInput("return_cumulative_logprob", [1], "BOOL") |
| 74 | + ) |
| 75 | + inputs[-1].set_data_from_numpy( |
| 76 | + np.array([return_cumulative_logprob], dtype=bool) |
| 77 | + ) |
| 78 | + |
| 79 | + if return_num_output_tokens is not None: |
| 80 | + inputs.append( |
| 81 | + grpcclient.InferInput("return_num_output_tokens", [1], "BOOL") |
| 82 | + ) |
| 83 | + inputs[-1].set_data_from_numpy( |
| 84 | + np.array([return_num_output_tokens], dtype=bool) |
| 85 | + ) |
| 86 | + |
| 87 | + return inputs |
| 88 | + |
| 89 | + def _callback(self, result, error): |
| 90 | + self._responses.append({"result": result, "error": error}) |
| 91 | + |
| 92 | + def _llm_infer(self, inputs): |
| 93 | + self._responses = [] |
| 94 | + with grpcclient.InferenceServerClient(self._grpc_url) as client: |
| 95 | + client.start_stream(self._callback) |
| 96 | + client.async_stream_infer( |
| 97 | + self._model_name, inputs=inputs, parameters=self._sampling_parameters |
| 98 | + ) |
| 99 | + client.stop_stream() |
| 100 | + assert len(self._responses) > 0 |
| 101 | + |
| 102 | + def _assert_text_output_valid(self): |
| 103 | + text_output = "" |
| 104 | + for response in self._responses: |
| 105 | + result, error = response["result"], response["error"] |
| 106 | + assert error is None |
| 107 | + text_output += result.as_numpy(name="text_output")[0].decode("utf-8") |
| 108 | + assert len(text_output) > 0, "output is empty" |
| 109 | + assert text_output.count(" ") > 4, "output is not a sentence" |
| 110 | + |
| 111 | + def _assert_finish_reason(self, return_finish_reason): |
| 112 | + for i in range(len(self._responses)): |
| 113 | + result, error = self._responses[i]["result"], self._responses[i]["error"] |
| 114 | + assert error is None |
| 115 | + finish_reason_np = result.as_numpy(name="finish_reason") |
| 116 | + if return_finish_reason is None or return_finish_reason == False: |
| 117 | + assert finish_reason_np is None |
| 118 | + continue |
| 119 | + finish_reason = finish_reason_np[0].decode("utf-8") |
| 120 | + if i < len(self._responses) - 1: |
| 121 | + assert finish_reason == "None" |
| 122 | + else: |
| 123 | + assert finish_reason == "length" |
| 124 | + |
| 125 | + def _assert_cumulative_logprob(self, return_cumulative_logprob): |
| 126 | + prev_cumulative_logprob = 0.0 |
| 127 | + for response in self._responses: |
| 128 | + result, error = response["result"], response["error"] |
| 129 | + assert error is None |
| 130 | + cumulative_logprob_np = result.as_numpy(name="cumulative_logprob") |
| 131 | + if return_cumulative_logprob is None or return_cumulative_logprob == False: |
| 132 | + assert cumulative_logprob_np is None |
| 133 | + continue |
| 134 | + cumulative_logprob = cumulative_logprob_np[0].astype(float) |
| 135 | + assert cumulative_logprob != prev_cumulative_logprob |
| 136 | + prev_cumulative_logprob = cumulative_logprob |
| 137 | + |
| 138 | + def _assert_num_output_tokens(self, return_num_output_tokens): |
| 139 | + for response in self._responses: |
| 140 | + result, error = response["result"], response["error"] |
| 141 | + assert error is None |
| 142 | + num_output_tokens_np = result.as_numpy(name="num_output_tokens") |
| 143 | + if return_num_output_tokens is None or return_num_output_tokens == False: |
| 144 | + assert num_output_tokens_np is None |
| 145 | + continue |
| 146 | + num_output_tokens = num_output_tokens_np[0].astype(int) |
| 147 | + # TODO: vLLM may return token ids identical to the previous one when |
| 148 | + # streaming, for example: |
| 149 | + # |
| 150 | + # prev: None |
| 151 | + # curr: text=' the', token_ids=array('l', [5]) |
| 152 | + # |
| 153 | + # prev: text=' the', token_ids=array('l', [5, 1385]) |
| 154 | + # curr: text=' the term', token_ids=array('l', [5, 1385]) |
| 155 | + # |
| 156 | + # prev: text=' the term', token_ids=array('l', [5, 1385, 44]) |
| 157 | + # curr: text=' the term', token_ids=array('l', [5, 1385, 44]) |
| 158 | + # |
| 159 | + # prev: text=' the term', token_ids=array('l', [5, 1385, 44, 48]) |
| 160 | + # curr: text=' the term “', token_ids=array('l', [5, 1385, 44, 48]) |
| 161 | + # |
| 162 | + # If this is no longer the case in a future release, change the assert |
| 163 | + # to assert num_output_tokens > 0. |
| 164 | + assert num_output_tokens >= 0 |
| 165 | + |
| 166 | + @pytest.mark.parametrize("stream", [True, False]) |
| 167 | + @pytest.mark.parametrize("return_finish_reason", [None, True, False]) |
| 168 | + @pytest.mark.parametrize("return_cumulative_logprob", [None, True, False]) |
| 169 | + @pytest.mark.parametrize("return_num_output_tokens", [None, True, False]) |
| 170 | + def test_additional_outputs( |
| 171 | + self, |
| 172 | + stream, |
| 173 | + return_finish_reason, |
| 174 | + return_cumulative_logprob, |
| 175 | + return_num_output_tokens, |
| 176 | + ): |
| 177 | + inputs = self._get_inputs( |
| 178 | + self._prompt, |
| 179 | + stream=stream, |
| 180 | + sampling_parameters=self._sampling_parameters, |
| 181 | + return_finish_reason=return_finish_reason, |
| 182 | + return_cumulative_logprob=return_cumulative_logprob, |
| 183 | + return_num_output_tokens=return_num_output_tokens, |
| 184 | + ) |
| 185 | + self._llm_infer(inputs) |
| 186 | + self._assert_text_output_valid() |
| 187 | + self._assert_finish_reason(return_finish_reason) |
| 188 | + self._assert_cumulative_logprob(return_cumulative_logprob) |
| 189 | + self._assert_num_output_tokens(return_num_output_tokens) |
0 commit comments