|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# |
| 4 | +# Redistribution and use in source and binary forms, with or without |
| 5 | +# modification, are permitted provided that the following conditions |
| 6 | +# are met: |
| 7 | +# * Redistributions of source code must retain the above copyright |
| 8 | +# notice, this list of conditions and the following disclaimer. |
| 9 | +# * Redistributions in binary form must reproduce the above copyright |
| 10 | +# notice, this list of conditions and the following disclaimer in the |
| 11 | +# documentation and/or other materials provided with the distribution. |
| 12 | +# * Neither the name of NVIDIA CORPORATION nor the names of its |
| 13 | +# contributors may be used to endorse or promote products derived |
| 14 | +# from this software without specific prior written permission. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 17 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 19 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 20 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 21 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 22 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 23 | +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 24 | +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | + |
| 28 | +import os |
| 29 | +import unittest |
| 30 | + |
| 31 | +import numpy as np |
| 32 | +import tritonclient.grpc as grpcclient |
| 33 | +import tritonclient.http as httpclient |
| 34 | + |
| 35 | + |
| 36 | +class BFloat16Test(unittest.TestCase): |
| 37 | + def setUp(self): |
| 38 | + self.protocol = os.environ.get("CLIENT_TYPE", "http") |
| 39 | + if self.protocol == "http": |
| 40 | + self.client_ = httpclient.InferenceServerClient("localhost:8000") |
| 41 | + else: |
| 42 | + self.client_ = grpcclient.InferenceServerClient("localhost:8001") |
| 43 | + self.model_name_ = "add_bf16" |
| 44 | + self.shape_ = [1] |
| 45 | + |
| 46 | + def _infer_bf16(self, input0_data, input1_data): |
| 47 | + """Helper to run BF16 inference and return the output numpy array.""" |
| 48 | + if self.protocol == "http": |
| 49 | + input0 = httpclient.InferInput("INPUT0", self.shape_, "BF16") |
| 50 | + input1 = httpclient.InferInput("INPUT1", self.shape_, "BF16") |
| 51 | + else: |
| 52 | + input0 = grpcclient.InferInput("INPUT0", self.shape_, "BF16") |
| 53 | + input1 = grpcclient.InferInput("INPUT1", self.shape_, "BF16") |
| 54 | + input0.set_data_from_numpy(input0_data) |
| 55 | + input1.set_data_from_numpy(input1_data) |
| 56 | + |
| 57 | + results = self.client_.infer(self.model_name_, [input0, input1]) |
| 58 | + return results.as_numpy("OUTPUT") |
| 59 | + |
| 60 | + def test_bf16_add_variants(self): |
| 61 | + """Run BF16 add across multiple cases: zeros, negatives, large, small, cancellation, and identical.""" |
| 62 | + for input0_val, input1_val, expected_val in [ |
| 63 | + (0.0, 0.0, 0.0), # zeros |
| 64 | + (-1.5, 3.5, 2.0), # negatives / mixed |
| 65 | + (100.0, 200.0, 300.0), # large |
| 66 | + (1e-2, 1e-2, 2e-2), # small (near underflow) |
| 67 | + (1.0, -1.0, 0.0), # cancellation |
| 68 | + (2.0, 2.0, 4.0), # identical inputs |
| 69 | + ]: |
| 70 | + output = self._infer_bf16( |
| 71 | + np.full(self.shape_, input0_val, dtype=np.float32), |
| 72 | + np.full(self.shape_, input1_val, dtype=np.float32), |
| 73 | + ) |
| 74 | + self.assertEqual(output.dtype, np.float32) |
| 75 | + # TODO: BF16 to FP32 conversion loses precision. Remove rtol and atol in TRI-801. |
| 76 | + # BF16 has ~3 decimal digits; use relaxed tol for computed values |
| 77 | + np.testing.assert_allclose(output, expected_val, rtol=1e-2, atol=1e-3) |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + unittest.main() |
0 commit comments