| 164 | ], |
| 165 | ) |
| 166 | def test_fp16_weights( |
| 167 | quantization, expected_weight, expected_weight_scale, expected_bias |
| 168 | ): |
| 169 | class Spec(ctranslate2.specs.LayerSpec): |
| 170 | def __init__(self, weight, bias): |
| 171 | self.weight = weight |
| 172 | self.weight_scale = OPTIONAL |
| 173 | self.bias = bias |
| 174 | |
| 175 | weight = np.array([[-10, -3, 5, 2]], dtype=np.float16) |
| 176 | bias = np.array([4], dtype=np.float16) |
| 177 | |
| 178 | spec = Spec(weight, bias) |
| 179 | spec.validate() |
| 180 | spec.optimize(quantization=quantization) |
| 181 | |
| 182 | assert test_utils.array_equal(spec.weight.numpy(), expected_weight) |
| 183 | assert test_utils.array_equal(spec.bias.numpy(), expected_bias) |
| 184 | |
| 185 | # Check the weights were not copied or converted. |
| 186 | if quantization in (None, "float16"): |
| 187 | assert spec.weight.numpy() is weight |
| 188 | assert spec.bias.numpy() is bias |
| 189 | elif quantization in ("int8", "int8_float16"): |
| 190 | assert spec.bias.numpy() is bias |
| 191 | |
| 192 | if expected_weight_scale is None: |
| 193 | assert spec.weight_scale == OPTIONAL |
| 194 | else: |
| 195 | assert test_utils.array_equal(spec.weight_scale.numpy(), expected_weight_scale) |
| 196 | |
| 197 | |
| 198 | def test_index_spec(): |