|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. 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 | + |
| 28 | +import os |
| 29 | +import queue |
| 30 | +import unittest |
| 31 | +from functools import partial |
| 32 | + |
| 33 | +import numpy as np |
| 34 | +import tritonclient.grpc as grpcclient |
| 35 | +from tritonclient.utils import InferenceServerException |
| 36 | + |
| 37 | +OUTPUT_NUM_ELEMENTS = int(os.getenv("OUTPUT_NUM_ELEMENTS", 1)) |
| 38 | + |
| 39 | + |
| 40 | +class UserData: |
| 41 | + def __init__(self): |
| 42 | + self._completed_requests = queue.Queue() |
| 43 | + |
| 44 | + |
| 45 | +def callback(user_data, result, error): |
| 46 | + if error: |
| 47 | + user_data._completed_requests.put(error, timeout=100) |
| 48 | + else: |
| 49 | + user_data._completed_requests.put(result, timeout=100) |
| 50 | + |
| 51 | + |
| 52 | +class TestTritonInference(unittest.TestCase): |
| 53 | + def setUp(self): |
| 54 | + self.triton_client = grpcclient.InferenceServerClient(url="localhost:8001") |
| 55 | + |
| 56 | + def tearDown(self): |
| 57 | + self.triton_client.stop_stream() |
| 58 | + |
| 59 | + def test_inference(self): |
| 60 | + model_name = "repeat_int32" |
| 61 | + num_responses = 256 |
| 62 | + in_data = np.random.randint(0, 1000, num_responses, dtype=np.int32) |
| 63 | + delay_data = np.zeros(num_responses, dtype=np.uint32) |
| 64 | + wait_data = np.zeros(1, dtype=np.uint32) |
| 65 | + user_data = UserData() |
| 66 | + |
| 67 | + inputs = [ |
| 68 | + grpcclient.InferInput("IN", [num_responses], "INT32"), |
| 69 | + grpcclient.InferInput("DELAY", [num_responses], "UINT32"), |
| 70 | + grpcclient.InferInput("WAIT", [1], "UINT32"), |
| 71 | + ] |
| 72 | + outputs = [ |
| 73 | + grpcclient.InferRequestedOutput("OUT"), |
| 74 | + grpcclient.InferRequestedOutput("IDX"), |
| 75 | + ] |
| 76 | + |
| 77 | + inputs[0].set_data_from_numpy(in_data) |
| 78 | + inputs[1].set_data_from_numpy(delay_data) |
| 79 | + inputs[2].set_data_from_numpy(wait_data) |
| 80 | + |
| 81 | + self.triton_client.start_stream(callback=partial(callback, user_data)) |
| 82 | + self.triton_client.async_stream_infer( |
| 83 | + model_name=model_name, |
| 84 | + inputs=inputs, |
| 85 | + outputs=outputs, |
| 86 | + ) |
| 87 | + |
| 88 | + recv_count = 0 |
| 89 | + while recv_count < num_responses: |
| 90 | + data_item = user_data._completed_requests.get() |
| 91 | + |
| 92 | + if isinstance(data_item, InferenceServerException): |
| 93 | + self.fail(f"InferenceServerException: {data_item}") |
| 94 | + try: |
| 95 | + response_idx = data_item.as_numpy("IDX")[0] |
| 96 | + response_data = data_item.as_numpy("OUT") |
| 97 | + expected_data = in_data[response_idx] |
| 98 | + |
| 99 | + self.assertEqual( |
| 100 | + response_data[0], |
| 101 | + expected_data, |
| 102 | + f"Validation failed at index {response_idx} - response_data[0]: {response_data[0]}, expected_data: {expected_data}", |
| 103 | + ) |
| 104 | + self.assertEqual( |
| 105 | + response_data.size, |
| 106 | + OUTPUT_NUM_ELEMENTS, |
| 107 | + f"Validation failed - response_data.size: {response_data.size}, OUTPUT_NUM_ELEMENTS: {OUTPUT_NUM_ELEMENTS}", |
| 108 | + ) |
| 109 | + |
| 110 | + except Exception as e: |
| 111 | + self.fail(f"Error processing response: {str(e)}") |
| 112 | + recv_count += 1 |
| 113 | + |
| 114 | + self.assertEqual( |
| 115 | + user_data._completed_requests.qsize(), |
| 116 | + 0, |
| 117 | + "Did not receive the expected number of responses.", |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + unittest.main() |
0 commit comments