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