|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 4 | +# |
| 5 | +# Redistribution and use in source and binary forms, with or without |
| 6 | +# modification, are permitted provided that the following conditions |
| 7 | +# are met: |
| 8 | +# * Redistributions of source code must retain the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer. |
| 10 | +# * Redistributions in binary form must reproduce the above copyright |
| 11 | +# notice, this list of conditions and the following disclaimer in the |
| 12 | +# documentation and/or other materials provided with the distribution. |
| 13 | +# * Neither the name of NVIDIA CORPORATION nor the names of its |
| 14 | +# contributors may be used to endorse or promote products derived |
| 15 | +# from this software without specific prior written permission. |
| 16 | +# |
| 17 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 18 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 20 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 21 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 22 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 23 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 24 | +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 25 | +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + |
| 29 | +import sys |
| 30 | + |
| 31 | +sys.path.append("../common") |
| 32 | + |
| 33 | +import os |
| 34 | +import unittest |
| 35 | + |
| 36 | +import numpy as np |
| 37 | +import test_util as tu |
| 38 | +import tritonclient.grpc as grpcclient |
| 39 | +import tritonclient.http as httpclient |
| 40 | +from tritonclient.utils import InferenceServerException |
| 41 | + |
| 42 | + |
| 43 | +class ClassificationParameterTest(tu.TestResultCollector): |
| 44 | + def setUp(self): |
| 45 | + self.protocol = os.environ.get("CLIENT_TYPE", "http") |
| 46 | + if self.protocol == "http": |
| 47 | + self.client = httpclient.InferenceServerClient("localhost:8000") |
| 48 | + else: |
| 49 | + self.client = grpcclient.InferenceServerClient("localhost:8001") |
| 50 | + |
| 51 | + def _prepare_io(self, input_data, dtype): |
| 52 | + if self.protocol == "http": |
| 53 | + inputs = [httpclient.InferInput("INPUT0", input_data.shape, dtype)] |
| 54 | + outputs = [httpclient.InferRequestedOutput(name="OUTPUT0", class_count=5)] |
| 55 | + else: |
| 56 | + inputs = [grpcclient.InferInput("INPUT0", input_data.shape, dtype)] |
| 57 | + outputs = [grpcclient.InferRequestedOutput(name="OUTPUT0", class_count=5)] |
| 58 | + inputs[0].set_data_from_numpy(input_data) |
| 59 | + return inputs, outputs |
| 60 | + |
| 61 | + def test_classificattion(self): |
| 62 | + shape = (1, 8) |
| 63 | + dtype = "FP32" |
| 64 | + model_name = "identity_fp32" |
| 65 | + input_data = np.ones(shape, dtype=np.float32) |
| 66 | + |
| 67 | + inputs, outputs = self._prepare_io(input_data, dtype) |
| 68 | + result = self.client.infer( |
| 69 | + model_name=model_name, inputs=inputs, outputs=outputs |
| 70 | + ) |
| 71 | + output = result.get_output("OUTPUT0") |
| 72 | + if self.protocol == "http": |
| 73 | + output_dtype = output["datatype"] |
| 74 | + else: |
| 75 | + output_dtype = output.datatype |
| 76 | + |
| 77 | + self.assertEqual(output_dtype, "BYTES") |
| 78 | + |
| 79 | + # Validate shape matches to the class_count |
| 80 | + output_data = result.as_numpy("OUTPUT0") |
| 81 | + self.assertIsNotNone(output_data) |
| 82 | + self.assertEqual(output_data.shape, (1, 5)) |
| 83 | + |
| 84 | + for res_str_bytes in np.nditer(output_data, flags=["refs_ok"]): |
| 85 | + res_str = res_str_bytes.item().decode("utf-8") |
| 86 | + self.assertTrue(res_str.startswith("1.000000:")) |
| 87 | + |
| 88 | + def test_classificattion_unsupported_data_type(self): |
| 89 | + shape = (1, 8) |
| 90 | + model_name = "identity_bytes" |
| 91 | + dtype = "BYTES" |
| 92 | + input_data = np.array([["test"] * shape[1]], dtype=object) |
| 93 | + |
| 94 | + inputs, outputs = self._prepare_io(input_data, dtype) |
| 95 | + with self.assertRaises(InferenceServerException) as e: |
| 96 | + self.client.infer(model_name=model_name, inputs=inputs, outputs=outputs) |
| 97 | + |
| 98 | + self.assertIn( |
| 99 | + "class result not available for output due to unsupported type 'BYTES'", |
| 100 | + str(e.exception), |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + unittest.main() |
0 commit comments