|
31 | 31 |
|
32 | 32 | import ml_dtypes |
33 | 33 | import numpy as np |
| 34 | +import pytest |
34 | 35 | import tritonclient.grpc as grpcclient |
35 | 36 | import tritonclient.http as httpclient |
36 | 37 |
|
@@ -79,64 +80,27 @@ def _infer_bf16(self, input0_data, input1_data): |
79 | 80 | results = self.client_.infer(self.model_name_, [input0, input1]) |
80 | 81 | return results.as_numpy("OUTPUT") |
81 | 82 |
|
82 | | - def test_bf16_add_variants(self): |
83 | | - """Run multiple BF16 add cases in one test: zeros, negatives, large, small, cancellation, identical.""" |
| 83 | + @pytest.mark.parametrize( |
| 84 | + "input0_val,input1_val,expected_val", |
| 85 | + [ |
| 86 | + (0.0, 0.0, 0.0), # zeros |
| 87 | + (-1.5, 3.5, 2.0), # negatives / mixed |
| 88 | + (100.0, 200.0, 300.0), # large |
| 89 | + (1e-2, 1e-2, 2e-2), # small (near underflow) |
| 90 | + (1.0, -1.0, 0.0), # cancellation |
| 91 | + (2.0, 2.0, 4.0), # identical inputs |
| 92 | + ], |
| 93 | + ) |
| 94 | + def test_bf16_add_variants(self, input0_val, input1_val, expected_val): |
| 95 | + """Run BF16 add for one case: zeros, negatives, large, small, cancellation, or identical.""" |
84 | 96 | shape = (5, 5) |
85 | | - |
86 | | - # Zeros: 0.0 + 0.0 = 0.0 |
87 | | - output = self._infer_bf16( |
88 | | - np.zeros(shape, dtype=ml_dtypes.bfloat16), |
89 | | - np.zeros(shape, dtype=ml_dtypes.bfloat16), |
90 | | - ) |
91 | | - self.assertEqual(output.dtype, ml_dtypes.bfloat16) |
92 | | - self._assert_allclose_bf16(output, np.zeros(shape, dtype=ml_dtypes.bfloat16)) |
93 | | - |
94 | | - # Negative and mixed: -1.5 + 3.5 = 2.0 |
95 | | - output = self._infer_bf16( |
96 | | - np.full(shape, -1.5, dtype=ml_dtypes.bfloat16), |
97 | | - np.full(shape, 3.5, dtype=ml_dtypes.bfloat16), |
98 | | - ) |
99 | | - self.assertEqual(output.dtype, ml_dtypes.bfloat16) |
100 | | - self._assert_allclose_bf16( |
101 | | - output, np.full(shape, 2.0, dtype=ml_dtypes.bfloat16) |
102 | | - ) |
103 | | - |
104 | | - # Large values within BF16 range: 100 + 200 = 300 |
105 | | - output = self._infer_bf16( |
106 | | - np.full(shape, 100.0, dtype=ml_dtypes.bfloat16), |
107 | | - np.full(shape, 200.0, dtype=ml_dtypes.bfloat16), |
108 | | - ) |
109 | | - self.assertEqual(output.dtype, ml_dtypes.bfloat16) |
110 | | - self._assert_allclose_bf16( |
111 | | - output, np.full(shape, 300.0, dtype=ml_dtypes.bfloat16) |
112 | | - ) |
113 | | - |
114 | | - # Small values (near underflow / precision limit): 0.01 + 0.01 = 0.02 |
115 | | - output = self._infer_bf16( |
116 | | - np.full(shape, 1e-2, dtype=ml_dtypes.bfloat16), |
117 | | - np.full(shape, 1e-2, dtype=ml_dtypes.bfloat16), |
118 | | - ) |
119 | | - self.assertEqual(output.dtype, ml_dtypes.bfloat16) |
120 | | - self._assert_allclose_bf16( |
121 | | - output, np.full(shape, 2e-2, dtype=ml_dtypes.bfloat16) |
122 | | - ) |
123 | | - |
124 | | - # Exact cancellation: 1.0 + (-1.0) = 0.0 |
125 | | - output = self._infer_bf16( |
126 | | - np.full(shape, 1.0, dtype=ml_dtypes.bfloat16), |
127 | | - np.full(shape, -1.0, dtype=ml_dtypes.bfloat16), |
128 | | - ) |
129 | | - self.assertEqual(output.dtype, ml_dtypes.bfloat16) |
130 | | - self._assert_allclose_bf16(output, np.zeros(shape, dtype=ml_dtypes.bfloat16)) |
131 | | - |
132 | | - # Identical inputs: 2.0 + 2.0 = 4.0 |
133 | 97 | output = self._infer_bf16( |
134 | | - np.full(shape, 2.0, dtype=ml_dtypes.bfloat16), |
135 | | - np.full(shape, 2.0, dtype=ml_dtypes.bfloat16), |
| 98 | + np.full(shape, input0_val, dtype=ml_dtypes.bfloat16), |
| 99 | + np.full(shape, input1_val, dtype=ml_dtypes.bfloat16), |
136 | 100 | ) |
137 | 101 | self.assertEqual(output.dtype, ml_dtypes.bfloat16) |
138 | 102 | self._assert_allclose_bf16( |
139 | | - output, np.full(shape, 4.0, dtype=ml_dtypes.bfloat16) |
| 103 | + output, np.full(shape, expected_val, dtype=ml_dtypes.bfloat16) |
140 | 104 | ) |
141 | 105 |
|
142 | 106 |
|
|
0 commit comments