|
| 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 os |
| 29 | + |
| 30 | +import numpy as np |
| 31 | +import pytest |
| 32 | +import tritonclient.grpc as grpcclient |
| 33 | + |
| 34 | + |
| 35 | +class TestCheckHealth: |
| 36 | + _grpc_url = "localhost:8001" |
| 37 | + _model_name = "vllm_opt" |
| 38 | + _sampling_parameters = {"temperature": "0", "top_p": "1"} |
| 39 | + _prompt = "In this example," |
| 40 | + |
| 41 | + def _get_inputs(self, prompt, stream=True, sampling_parameters=None): |
| 42 | + inputs = [] |
| 43 | + |
| 44 | + inputs.append(grpcclient.InferInput("text_input", [1], "BYTES")) |
| 45 | + inputs[-1].set_data_from_numpy( |
| 46 | + np.array([prompt.encode("utf-8")], dtype=np.object_) |
| 47 | + ) |
| 48 | + |
| 49 | + inputs.append(grpcclient.InferInput("stream", [1], "BOOL")) |
| 50 | + inputs[-1].set_data_from_numpy(np.array([stream], dtype=bool)) |
| 51 | + |
| 52 | + if sampling_parameters is not None: |
| 53 | + inputs.append(grpcclient.InferInput("sampling_parameters", [1], "BYTES")) |
| 54 | + inputs[-1].set_data_from_numpy( |
| 55 | + np.array( |
| 56 | + [json.dumps(sampling_parameters).encode("utf-8")], dtype=np.object_ |
| 57 | + ) |
| 58 | + ) |
| 59 | + |
| 60 | + return inputs |
| 61 | + |
| 62 | + def _callback(self, result, error): |
| 63 | + self._responses.append({"result": result, "error": error}) |
| 64 | + |
| 65 | + def _llm_infer(self): |
| 66 | + inputs = self._get_inputs( |
| 67 | + self._prompt, stream=True, sampling_parameters=self._sampling_parameters |
| 68 | + ) |
| 69 | + self._responses = [] |
| 70 | + with grpcclient.InferenceServerClient(self._grpc_url) as client: |
| 71 | + client.start_stream(self._callback) |
| 72 | + client.async_stream_infer( |
| 73 | + self._model_name, inputs=inputs, parameters=self._sampling_parameters |
| 74 | + ) |
| 75 | + client.stop_stream() |
| 76 | + |
| 77 | + def _assert_text_output_valid(self): |
| 78 | + text_output = "" |
| 79 | + for response in self._responses: |
| 80 | + result, error = response["result"], response["error"] |
| 81 | + assert error is None |
| 82 | + text_output += result.as_numpy(name="text_output")[0].decode("utf-8") |
| 83 | + assert len(text_output) > 0, "output is empty" |
| 84 | + assert text_output.count(" ") > 4, "output is not a sentence" |
| 85 | + |
| 86 | + def _assert_infer_exception(self, expected_exception_message): |
| 87 | + assert len(self._responses) == 1 |
| 88 | + for response in self._responses: |
| 89 | + result, error = response["result"], response["error"] |
| 90 | + assert result is None |
| 91 | + assert str(error) == expected_exception_message |
| 92 | + |
| 93 | + def _assert_model_ready(self, expected_readiness): |
| 94 | + with grpcclient.InferenceServerClient(self._grpc_url) as client: |
| 95 | + assert client.is_model_ready(self._model_name) == expected_readiness |
| 96 | + |
| 97 | + def test_vllm_is_healthy(self): |
| 98 | + num_repeats = 3 |
| 99 | + for i in range(num_repeats): |
| 100 | + self._assert_model_ready(True) |
| 101 | + self._llm_infer() |
| 102 | + self._assert_text_output_valid() |
| 103 | + self._assert_model_ready(True) |
| 104 | + |
| 105 | + def test_vllm_not_healthy(self): |
| 106 | + self._assert_model_ready(True) |
| 107 | + # The 1st infer should complete successfully |
| 108 | + self._llm_infer() |
| 109 | + self._assert_text_output_valid() |
| 110 | + self._assert_model_ready(True) |
| 111 | + # The 2nd infer should begin with health check failed |
| 112 | + self._llm_infer() |
| 113 | + self._assert_infer_exception("vLLM engine is not healthy") |
| 114 | + self._assert_model_ready(False) |
| 115 | + # The 3rd infer should have model not found |
| 116 | + self._llm_infer() |
| 117 | + self._assert_infer_exception( |
| 118 | + "Request for unknown model: 'vllm_opt' has no available versions" |
| 119 | + ) |
| 120 | + self._assert_model_ready(False) |
| 121 | + |
| 122 | + def test_vllm_enable_health_check_multi_instance(self): |
| 123 | + with open(os.environ["SERVER_LOG"]) as f: |
| 124 | + server_log = f.read() |
| 125 | + expected_vllm_warning = "[vllm] Health check may only be enabled when the model has exactly 1 instance but 2 are found" |
| 126 | + assert expected_vllm_warning in server_log |
| 127 | + # Health check should be disabled |
| 128 | + self.test_vllm_is_healthy() |
0 commit comments