- 
                Notifications
    You must be signed in to change notification settings 
- Fork 32
feat: Support sending additional outputs from vLLM inference #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from 7 commits
      Commits
    
    
            Show all changes
          
          
            15 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      10a5b94
              
                Add additional outputs and their input switches to auto complete
              
              
                kthui 892f0d0
              
                chore: Refactor generate function
              
              
                kthui 58ee481
              
                Add additional outputs to response
              
              
                kthui 5e605ca
              
                Add test for additional outputs
              
              
                kthui f35e9c4
              
                Add docs for additonal outputs
              
              
                kthui e6e6404
              
                chore: Unify vLLM test names
              
              
                kthui 44edd6e
              
                Switch to pytest
              
              
                kthui 1773dea
              
                pytest to dump additional outputs
              
              
                kthui 29099df
              
                Rename output_* to return_*
              
              
                kthui 457eeaa
              
                Return token ids instead of number of token ids
              
              
                kthui 5e9b09f
              
                Revert "Return token ids instead of number of token ids"
              
              
                kthui dae3c13
              
                Rename num_token_ids to num_output_tokens
              
              
                kthui 2b531dd
              
                Merge branch 'main' of github.com:triton-inference-server/vllm_backen…
              
              
                kthui ccb3323
              
                [chore] Fix pre-commit on utils/metrics.py
              
              
                kthui 00aa413
              
                [docs] Update targeted release version
              
              
                kthui File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            185 changes: 185 additions & 0 deletions
          
          185 
        
  ci/L0_additional_outputs_vllm/additional_outputs_test.py
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| # Copyright 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions | ||
| # are met: | ||
| # * Redistributions of source code must retain the above copyright | ||
| # notice, this list of conditions and the following disclaimer. | ||
| # * Redistributions in binary form must reproduce the above copyright | ||
| # notice, this list of conditions and the following disclaimer in the | ||
| # documentation and/or other materials provided with the distribution. | ||
| # * Neither the name of NVIDIA CORPORATION nor the names of its | ||
| # contributors may be used to endorse or promote products derived | ||
| # from this software without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY | ||
| # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
| # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|  | ||
| import json | ||
|  | ||
| import numpy as np | ||
| import pytest | ||
| import tritonclient.grpc as grpcclient | ||
|  | ||
|  | ||
| class TestAdditionalOutputs: | ||
| _grpc_url = "localhost:8001" | ||
| _model_name = "vllm_opt" | ||
| _sampling_parameters = {"temperature": "0", "top_p": "1"} | ||
| _prompt = "In this example," | ||
|  | ||
| def _get_inputs( | ||
| self, | ||
| prompt, | ||
| stream=True, | ||
| sampling_parameters=None, | ||
| output_finish_reason=None, | ||
| output_cumulative_logprob=None, | ||
| output_num_token_ids=None, | ||
| ): | ||
| inputs = [] | ||
|  | ||
| inputs.append(grpcclient.InferInput("text_input", [1], "BYTES")) | ||
| inputs[-1].set_data_from_numpy( | ||
| np.array([prompt.encode("utf-8")], dtype=np.object_) | ||
| ) | ||
|  | ||
| inputs.append(grpcclient.InferInput("stream", [1], "BOOL")) | ||
| inputs[-1].set_data_from_numpy(np.array([stream], dtype=bool)) | ||
|  | ||
| if sampling_parameters is not None: | ||
| inputs.append(grpcclient.InferInput("sampling_parameters", [1], "BYTES")) | ||
| inputs[-1].set_data_from_numpy( | ||
| np.array( | ||
| [json.dumps(sampling_parameters).encode("utf-8")], dtype=np.object_ | ||
| ) | ||
| ) | ||
|  | ||
| if output_finish_reason is not None: | ||
| inputs.append(grpcclient.InferInput("output_finish_reason", [1], "BOOL")) | ||
| inputs[-1].set_data_from_numpy(np.array([output_finish_reason], dtype=bool)) | ||
|  | ||
| if output_cumulative_logprob is not None: | ||
| inputs.append( | ||
| grpcclient.InferInput("output_cumulative_logprob", [1], "BOOL") | ||
| ) | ||
| inputs[-1].set_data_from_numpy( | ||
| np.array([output_cumulative_logprob], dtype=bool) | ||
| ) | ||
|  | ||
| if output_num_token_ids is not None: | ||
| inputs.append(grpcclient.InferInput("output_num_token_ids", [1], "BOOL")) | ||
| inputs[-1].set_data_from_numpy(np.array([output_num_token_ids], dtype=bool)) | ||
|  | ||
| return inputs | ||
|  | ||
| def _callback(self, result, error): | ||
| self._responses.append({"result": result, "error": error}) | ||
|  | ||
| def _llm_infer(self, inputs): | ||
| self._responses = [] | ||
| with grpcclient.InferenceServerClient(self._grpc_url) as client: | ||
| client.start_stream(self._callback) | ||
| client.async_stream_infer( | ||
| self._model_name, inputs=inputs, parameters=self._sampling_parameters | ||
| ) | ||
| client.stop_stream() | ||
| assert len(self._responses) > 0 | ||
|  | ||
| def _assert_text_output_valid(self): | ||
| text_output = "" | ||
| for response in self._responses: | ||
| result, error = response["result"], response["error"] | ||
| assert error is None | ||
| text_output += result.as_numpy(name="text_output")[0].decode("utf-8") | ||
| assert len(text_output) > 0, "output is empty" | ||
| assert text_output.count(" ") > 4, "output is not a sentence" | ||
|  | ||
| def _assert_finish_reason(self, output_finish_reason): | ||
| for i in range(len(self._responses)): | ||
| result, error = self._responses[i]["result"], self._responses[i]["error"] | ||
| assert error is None | ||
| finish_reason_np = result.as_numpy(name="finish_reason") | ||
| if output_finish_reason is None or output_finish_reason == False: | ||
| assert finish_reason_np is None | ||
| continue | ||
| finish_reason = finish_reason_np[0].decode("utf-8") | ||
| if i < len(self._responses) - 1: | ||
| assert finish_reason == "None" | ||
| else: | ||
| assert finish_reason == "length" | ||
|  | ||
| def _assert_cumulative_logprob(self, output_cumulative_logprob): | ||
| prev_cumulative_logprob = 0.0 | ||
| for response in self._responses: | ||
| result, error = response["result"], response["error"] | ||
| assert error is None | ||
| cumulative_logprob_np = result.as_numpy(name="cumulative_logprob") | ||
| if output_cumulative_logprob is None or output_cumulative_logprob == False: | ||
| assert cumulative_logprob_np is None | ||
| continue | ||
| cumulative_logprob = cumulative_logprob_np[0].astype(float) | ||
| assert cumulative_logprob != prev_cumulative_logprob | ||
| prev_cumulative_logprob = cumulative_logprob | ||
|  | ||
| def _assert_num_token_ids(self, output_num_token_ids): | ||
| for response in self._responses: | ||
| result, error = response["result"], response["error"] | ||
| assert error is None | ||
| num_token_ids_np = result.as_numpy(name="num_token_ids") | ||
| if output_num_token_ids is None or output_num_token_ids == False: | ||
| assert num_token_ids_np is None | ||
| continue | ||
| num_token_ids = num_token_ids_np[0].astype(int) | ||
| # TODO: vLLM may return token ids identical to the previous one when | ||
| # streaming, for example: | ||
| # | ||
| # prev: None | ||
| # curr: text=' the', token_ids=array('l', [5]) | ||
| # | ||
| # prev: text=' the', token_ids=array('l', [5, 1385]) | ||
| # curr: text=' the term', token_ids=array('l', [5, 1385]) | ||
| # | ||
| # prev: text=' the term', token_ids=array('l', [5, 1385, 44]) | ||
| # curr: text=' the term', token_ids=array('l', [5, 1385, 44]) | ||
| # | ||
| # prev: text=' the term', token_ids=array('l', [5, 1385, 44, 48]) | ||
| # curr: text=' the term “', token_ids=array('l', [5, 1385, 44, 48]) | ||
| # | ||
| # If this is no longer the case in a future release, change the assert | ||
| # to assert num_token_ids > 0. | ||
| assert num_token_ids >= 0 | ||
|  | ||
| @pytest.mark.parametrize("stream", [True, False]) | ||
| @pytest.mark.parametrize("output_finish_reason", [None, True, False]) | ||
| @pytest.mark.parametrize("output_cumulative_logprob", [None, True, False]) | ||
| @pytest.mark.parametrize("output_num_token_ids", [None, True, False]) | ||
| def test_additional_outputs( | ||
| self, | ||
| stream, | ||
| output_finish_reason, | ||
| output_cumulative_logprob, | ||
| output_num_token_ids, | ||
| ): | ||
| inputs = self._get_inputs( | ||
| self._prompt, | ||
| stream=stream, | ||
| sampling_parameters=self._sampling_parameters, | ||
| output_finish_reason=output_finish_reason, | ||
| output_cumulative_logprob=output_cumulative_logprob, | ||
| output_num_token_ids=output_num_token_ids, | ||
| ) | ||
| self._llm_infer(inputs) | ||
| self._assert_text_output_valid() | ||
| self._assert_finish_reason(output_finish_reason) | ||
| self._assert_cumulative_logprob(output_cumulative_logprob) | ||
| self._assert_num_token_ids(output_num_token_ids) | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| #!/bin/bash | ||
| # Copyright 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions | ||
| # are met: | ||
| # * Redistributions of source code must retain the above copyright | ||
| # notice, this list of conditions and the following disclaimer. | ||
| # * Redistributions in binary form must reproduce the above copyright | ||
| # notice, this list of conditions and the following disclaimer in the | ||
| # documentation and/or other materials provided with the distribution. | ||
| # * Neither the name of NVIDIA CORPORATION nor the names of its | ||
| # contributors may be used to endorse or promote products derived | ||
| # from this software without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY | ||
| # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
| # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|  | ||
| export CUDA_VISIBLE_DEVICES=0 | ||
| source ../common/util.sh | ||
|  | ||
| pip3 install pytest==8.1.1 | ||
| pip3 install tritonclient[grpc] | ||
|  | ||
| # Prepare Model | ||
| rm -rf models vllm_baseline_output.pkl && mkdir -p models | ||
| SAMPLE_MODELS_REPO="../../samples/model_repository" | ||
| cp -r $SAMPLE_MODELS_REPO/vllm_model models/vllm_opt | ||
| sed -i 's/"gpu_memory_utilization": 0.5/"gpu_memory_utilization": 0.3/' models/vllm_opt/1/model.json | ||
|  | ||
| RET=0 | ||
|  | ||
| # Test | ||
| SERVER_LOG="vllm_opt.server.log" | ||
| SERVER_ARGS="--model-repository=models" | ||
| run_server | ||
| if [ "$SERVER_PID" == "0" ]; then | ||
| echo -e "\n***\n*** Failed to start $SERVER\n***" | ||
| cat $SERVER_LOG | ||
| exit 1 | ||
| fi | ||
| set +e | ||
| python3 -m pytest -s -v additional_outputs_test.py | ||
|         
                  kthui marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| if [ $? -ne 0 ]; then | ||
| echo -e "\n***\n*** additional_outputs_test FAILED. \n***" | ||
| RET=1 | ||
| fi | ||
| set -e | ||
| kill $SERVER_PID | ||
| wait $SERVER_PID | ||
|  | ||
| if [ $RET -eq 0 ]; then | ||
| echo -e "\n***\n*** Test Passed\n***" | ||
| else | ||
| echo -e "\n***\n*** Test FAILED\n***" | ||
| fi | ||
| exit $RET | ||
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
            File renamed without changes.
          
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <!-- | ||
| # Copyright 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions | ||
| # are met: | ||
| # * Redistributions of source code must retain the above copyright | ||
| # notice, this list of conditions and the following disclaimer. | ||
| # * Redistributions in binary form must reproduce the above copyright | ||
| # notice, this list of conditions and the following disclaimer in the | ||
| # documentation and/or other materials provided with the distribution. | ||
| # * Neither the name of NVIDIA CORPORATION nor the names of its | ||
| # contributors may be used to endorse or promote products derived | ||
| # from this software without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY | ||
| # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | ||
| # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
| # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
| # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
| # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
| # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| --> | ||
|  | ||
| # Additional Outputs from vLLM | ||
|         
                  kthui marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| The vLLM backend supports sending additional outputs from vLLM on top of the | ||
| usual `text_output` when requested. | ||
|  | ||
| All additional outputs are disabled by default and they need to be enabled on a | ||
| per-request basis. If enabled, the corresponding output tensor will be set for | ||
| all responses from the request. | ||
|  | ||
| ## Supported Additional Outputs | ||
|  | ||
| ### Finish Reason | ||
|  | ||
| The reason why the sequence is finished. See | ||
| [here](https://github.com/vllm-project/vllm/blob/v0.6.3.post1/vllm/outputs.py#L26) | ||
| for more details. | ||
|  | ||
| To enable, set `output_finish_reason` input tensor to `True`. The reason will be | ||
|         
                  kthui marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| sent as a string on the `finish_reason` output tensor. | ||
|  | ||
| Supported since r24.11. | ||
|  | ||
| ### Cumulative Log Probabilities | ||
|  | ||
| The cumulative log probability of the generated output text. See | ||
| [here](https://github.com/vllm-project/vllm/blob/v0.6.3.post1/vllm/outputs.py#L22) | ||
| for more details. | ||
|  | ||
| To enable, set `output_cumulative_logprob` input tensor to `True`. The floating | ||
| point value will be sent on the `cumulative_logprob` output tensor. | ||
|  | ||
| Supported since r24.11. | ||
|  | ||
| ### Number of token IDs | ||
|  | ||
| The number of token IDs of the generated output text sent on this response. It | ||
| is the difference in length of the token IDs generated from the last response to | ||
| this response. If this is the first response, the last response length is | ||
| presumed to be zero. See | ||
| [here](https://github.com/vllm-project/vllm/blob/v0.6.3.post1/vllm/outputs.py#L21) | ||
| for more details on the token IDs of the generated output text. | ||
|  | ||
| To enable, set `output_num_token_ids` input tensor to `True`. The unsigned | ||
| integer value will be sent on the `num_token_ids` output tensor. | ||
|  | ||
| Supported since r24.11. | ||
|  | ||
| ## Examples | ||
|  | ||
| ### Add Finish Reason to Outputs | ||
|  | ||
| ```python | ||
| import numpy as np | ||
| import tritonclient.grpc as grpcclient | ||
|  | ||
| inputs = [] | ||
|  | ||
| inputs.append(grpcclient.InferInput("text_input", [1], "BYTES")) | ||
| inputs[-1].set_data_from_numpy( | ||
| np.array(["example prompt".encode("utf-8")], dtype=np.object_) | ||
| ) | ||
|  | ||
| inputs.append(grpcclient.InferInput("output_finish_reason", [1], "BOOL")) | ||
| inputs[-1].set_data_from_numpy(np.array([True], dtype=bool)) | ||
|  | ||
| def callback(result, error): | ||
| ... | ||
| print(result.as_numpy(name="finish_reason")) | ||
|  | ||
| with grpcclient.InferenceServerClient("localhost:8001") as client: | ||
| client.start_stream(callback) | ||
| client.async_stream_infer("vLLM_model_name", inputs=inputs, ...) | ||
| client.stop_stream() | ||
| ``` | ||
|  | ||
| ## Notes | ||
|  | ||
| * Enabling additional outputs may impact performance, only add additional | ||
| outputs when necessary. | ||
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.