|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | + |
| 4 | +from verl.utils.qat.mxfp_quant import ( |
| 5 | + MXFPMode, |
| 6 | + dequantize_mxfp4, |
| 7 | + dequantize_mxfp8, |
| 8 | + fake_quantize, |
| 9 | + normalize_mxfp_mode, |
| 10 | + quantize_mxfp4, |
| 11 | + quantize_mxfp8, |
| 12 | +) |
| 13 | +from verl.utils.qat.mxfp_rotation import RotationConfig, _matrix, rotate_blocks |
| 14 | +from verl.utils.qat.mxfp_linear import MXFPQATConfig, MXFPQATLinear |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize("mode", ["mxfp_w8a8", "w8a8_mxfp8", "mxfp_w4a8", "mxfp_w4a4"]) |
| 18 | +def test_mode_names_normalize(mode): |
| 19 | + assert normalize_mxfp_mode(mode) in {MXFPMode.W8A8, MXFPMode.W4A8, MXFPMode.W4A4} |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize("quantize,dequantize", [(quantize_mxfp8, dequantize_mxfp8), (quantize_mxfp4, dequantize_mxfp4)]) |
| 23 | +def test_mxfp_qdq_preserves_shape_and_is_finite(quantize, dequantize): |
| 24 | + x = torch.linspace(-2, 2, 96, dtype=torch.float32).reshape(3, 32) |
| 25 | + payload, scale = quantize(x) |
| 26 | + y = dequantize(payload, scale, x.dtype) |
| 27 | + assert y.shape == x.shape |
| 28 | + assert torch.isfinite(y).all() |
| 29 | + assert torch.count_nonzero(y).item() > 0 |
| 30 | + |
| 31 | + |
| 32 | +def test_mxfp8_zero_block_stays_zero(): |
| 33 | + x = torch.zeros((2, 64), dtype=torch.float32) |
| 34 | + payload, scale = quantize_mxfp8(x) |
| 35 | + assert torch.count_nonzero(dequantize_mxfp8(payload, scale, x.dtype)).item() == 0 |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize("fmt", ["mxfp8", "mxfp4"]) |
| 39 | +def test_fake_quant_pads_and_restores_non_aligned_dimension(fmt): |
| 40 | + x = torch.randn((2, 37), dtype=torch.float32) |
| 41 | + y = fake_quantize(x, fmt) |
| 42 | + assert y.shape == x.shape |
| 43 | + assert torch.isfinite(y).all() |
| 44 | + |
| 45 | + |
| 46 | +def test_rotation_is_seeded_and_orthogonal(): |
| 47 | + config = RotationConfig(enable=True, targets=("fprop",), block_size=8, seed=17) |
| 48 | + matrix_a = _matrix(8, 17, torch.device("cpu"), torch.float32) |
| 49 | + matrix_b = _matrix(8, 17, torch.device("cpu"), torch.float32) |
| 50 | + assert torch.equal(matrix_a, matrix_b) |
| 51 | + assert torch.allclose(matrix_a @ matrix_a.T, torch.eye(8), atol=1e-6) |
| 52 | + x = torch.randn((3, 19)) |
| 53 | + assert rotate_blocks(x, config).shape == x.shape |
| 54 | + |
| 55 | + |
| 56 | +def test_linear_replacement_reuses_parameters_and_has_identity_without_transforms(): |
| 57 | + linear = torch.nn.Linear(32, 64) |
| 58 | + config = MXFPQATConfig( |
| 59 | + mode="mxfp_w8a8", quant_backend="torch", fake_quant_targets=(), rotation_enable=False |
| 60 | + ) |
| 61 | + qat = MXFPQATLinear.from_linear(linear, config) |
| 62 | + assert qat.weight is linear.weight |
| 63 | + assert qat.bias is linear.bias |
| 64 | + x = torch.randn((3, 32)) |
| 65 | + assert torch.equal(qat(x), linear(x)) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("targets", [("fprop",), ("dgrad",), ("wgrad",), ("fprop", "dgrad", "wgrad")]) |
| 69 | +@pytest.mark.parametrize("save_backward_axes", [False, True]) |
| 70 | +def test_linear_backward_targets_keep_gradient_shapes(targets, save_backward_axes): |
| 71 | + linear = torch.nn.Linear(32, 64) |
| 72 | + config = MXFPQATConfig( |
| 73 | + mode="mxfp_w8a8", |
| 74 | + quant_backend="torch", |
| 75 | + fake_quant_targets=targets, |
| 76 | + backward_reduction_axis_fake_quant=save_backward_axes, |
| 77 | + ) |
| 78 | + qat = MXFPQATLinear.from_linear(linear, config) |
| 79 | + x = torch.randn((4, 32), requires_grad=True) |
| 80 | + qat(x).square().mean().backward() |
| 81 | + assert x.grad is not None and x.grad.shape == x.shape and torch.isfinite(x.grad).all() |
| 82 | + assert qat.weight.grad is not None and qat.weight.grad.shape == qat.weight.shape |
| 83 | + assert torch.isfinite(qat.weight.grad).all() |
| 84 | + |
| 85 | + |
| 86 | +def test_linear_fprop_rotation_returns_gradients_in_original_coordinates(): |
| 87 | + linear = torch.nn.Linear(32, 64) |
| 88 | + config = MXFPQATConfig( |
| 89 | + mode="mxfp_w8a8", |
| 90 | + quant_backend="torch", |
| 91 | + fake_quant_targets=(), |
| 92 | + rotation_enable=True, |
| 93 | + rotation_targets=("fprop",), |
| 94 | + rotation_seed=23, |
| 95 | + ) |
| 96 | + qat = MXFPQATLinear.from_linear(linear, config) |
| 97 | + x = torch.randn((4, 32), requires_grad=True) |
| 98 | + qat(x).sum().backward() |
| 99 | + assert x.grad.shape == x.shape |
| 100 | + assert qat.weight.grad.shape == linear.weight.shape |
0 commit comments