|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | +from compressed_tensors.quantization import ( |
| 4 | + QuantizationArgs, |
| 5 | + QuantizationScheme, |
| 6 | + initialize_module_for_quantization, |
| 7 | +) |
| 8 | + |
| 9 | +from llmcompressor.modifiers.quantization.calibration import initialize_observer |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize( |
| 13 | + "shape,group_size,actorder", |
| 14 | + [ |
| 15 | + ((1, 1), None, False), |
| 16 | + ((1, 1), 128, False), |
| 17 | + ((1, 1), 128, True), |
| 18 | + ((64, 64), None, False), |
| 19 | + ((64, 64), 128, False), |
| 20 | + ((64, 64), 128, True), |
| 21 | + ((1792, 4096), None, False), |
| 22 | + ((1792, 4096), 128, False), |
| 23 | + ((1792, 4096), 128, True), |
| 24 | + ((3420, 64), None, False), |
| 25 | + ((3420, 64), 128, False), |
| 26 | + ((3420, 64), 128, True), |
| 27 | + ], |
| 28 | +) |
| 29 | +def test_observers_update(shape, group_size, actorder): |
| 30 | + module = torch.nn.Linear(*shape) |
| 31 | + scheme = QuantizationScheme( |
| 32 | + targets=["Linear"], |
| 33 | + weights=QuantizationArgs(group_size=group_size, actorder=actorder), |
| 34 | + input_activations=QuantizationArgs(), |
| 35 | + output_activations=QuantizationArgs(), |
| 36 | + ) |
| 37 | + |
| 38 | + input = torch.empty(module.in_features, dtype=module.weight.dtype) |
| 39 | + output = torch.empty(module.out_features, dtype=module.weight.dtype) |
| 40 | + |
| 41 | + initialize_module_for_quantization(module, scheme) |
| 42 | + initialize_observer(module, "weight") |
| 43 | + initialize_observer(module, "input") |
| 44 | + initialize_observer(module, "output") |
| 45 | + |
| 46 | + for location, value in ( |
| 47 | + ("weight", module.weight), |
| 48 | + ("input", input), |
| 49 | + ("output", output), |
| 50 | + ): |
| 51 | + observer = getattr(module, f"{location}_observer") |
| 52 | + g_idx = getattr(module, "g_idx", None) |
| 53 | + updated_scale, updated_zero_point = observer(value, g_idx=g_idx) |
| 54 | + |
| 55 | + assert_alike(updated_scale, getattr(module, f"{location}_scale")) |
| 56 | + assert_alike(updated_zero_point, getattr(module, f"{location}_zero_point")) |
| 57 | + |
| 58 | + |
| 59 | +def assert_alike(a, b): |
| 60 | + assert a.dtype == b.dtype |
| 61 | + assert a.shape == b.shape |
0 commit comments