|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD 3-Clause license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import pytest |
| 8 | +import torch |
| 9 | + |
| 10 | +triton = pytest.importorskip("triton", reason="Triton required to run this test") |
| 11 | + |
| 12 | +from packaging import version |
| 13 | +from torchao.float8.float8_utils import compute_error |
| 14 | +from torchao.prototype.blockwise_fp8.kernels import ( |
| 15 | + blockwise_fp8_gemm_1x128_1x128, |
| 16 | + blockwise_fp8_gemm_1x128_128x128, |
| 17 | + fp8_blockwise_act_quant, |
| 18 | + fp8_blockwise_weight_dequant, |
| 19 | + fp8_blockwise_weight_quant, |
| 20 | + torch_blockwise_scale_act_quant, |
| 21 | + torch_blockwise_scale_weight_quant, |
| 22 | + triton_quantize_fp8_block, |
| 23 | +) |
| 24 | +from torchao.testing.utils import skip_if_rocm |
| 25 | + |
| 26 | +BLOCKWISE_SIZE_MNK = [ |
| 27 | + (2, 512, 128), |
| 28 | + (3, 2048, 2048), |
| 29 | + (4, 3584, 640), |
| 30 | + (13, 8704, 8576), |
| 31 | + (26, 18944, 1664), |
| 32 | + (67, 6656, 1408), |
| 33 | +] |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA not available") |
| 37 | +@pytest.mark.parametrize("_, N, K", BLOCKWISE_SIZE_MNK) |
| 38 | +@pytest.mark.parametrize("dtype", [torch.float8_e4m3fn]) |
| 39 | +def test_blockwise_quant_dequant(_, N, K, dtype): |
| 40 | + x = torch.randn(N, K).cuda() |
| 41 | + qx, s = fp8_blockwise_weight_quant(x, dtype=dtype) |
| 42 | + x_reconstructed = fp8_blockwise_weight_dequant(qx, s) |
| 43 | + sqnr = compute_error(x, x_reconstructed) |
| 44 | + assert sqnr >= 25.0, f"SQNR {sqnr:.2f} must be >= 25.0" |
| 45 | + |
| 46 | + |
| 47 | +@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA not available") |
| 48 | +@pytest.mark.skipif( |
| 49 | + version.parse(triton.__version__) < version.parse("3.3.0"), |
| 50 | + reason="Triton version < 3.3.0, test skipped", |
| 51 | +) |
| 52 | +@pytest.mark.parametrize("M, N, K", BLOCKWISE_SIZE_MNK) |
| 53 | +@pytest.mark.parametrize("dtype", [torch.float8_e4m3fn]) |
| 54 | +def test_blockwise_fp8_gemm_1x128_128x128(M, N, K, dtype): |
| 55 | + A = torch.randn(M, K).cuda() |
| 56 | + B = torch.randn(N, K).cuda() |
| 57 | + C = A @ B.T |
| 58 | + A_q, A_s = fp8_blockwise_act_quant(A, dtype=dtype) |
| 59 | + B_q, B_s = fp8_blockwise_weight_quant(B, dtype=dtype) |
| 60 | + C_q = blockwise_fp8_gemm_1x128_128x128(A_q, A_s, B_q, B_s) |
| 61 | + assert not C_q.isnan().any(), "C_q must not contain NaNs" |
| 62 | + sqnr = compute_error(C, C_q) |
| 63 | + assert sqnr >= 22.0, f"SQNR {sqnr:.2f} must be >= 22.0" |
| 64 | + |
| 65 | + |
| 66 | +@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA not available") |
| 67 | +@pytest.mark.skipif( |
| 68 | + version.parse(triton.__version__) < version.parse("3.3.0"), |
| 69 | + reason="Triton version < 3.3.0, test skipped", |
| 70 | +) |
| 71 | +@pytest.mark.parametrize("M, N, K", BLOCKWISE_SIZE_MNK) |
| 72 | +@pytest.mark.parametrize("dtype", [torch.float8_e4m3fn]) |
| 73 | +def test_blockwise_fp8_gemm_1x128_1x128(M, N, K, dtype): |
| 74 | + A = torch.randn(M, K).cuda() |
| 75 | + B = torch.randn(N, K).cuda() |
| 76 | + C = A @ B.T |
| 77 | + A_q, A_s = fp8_blockwise_act_quant(A, dtype=dtype) |
| 78 | + B_q, B_s = fp8_blockwise_act_quant(B, dtype=dtype) |
| 79 | + C_q = blockwise_fp8_gemm_1x128_1x128(A_q, A_s, B_q, B_s) |
| 80 | + assert not C_q.isnan().any(), "C_q must not contain NaNs" |
| 81 | + sqnr = compute_error(C, C_q) |
| 82 | + assert sqnr >= 22.0, f"SQNR {sqnr:.2f} must be >= 22.0" |
| 83 | + |
| 84 | + |
| 85 | +@skip_if_rocm("ROCm not supported") |
| 86 | +@pytest.mark.parametrize("tile_size", [128, 256]) |
| 87 | +@pytest.mark.parametrize("test_eps", [True, False]) |
| 88 | +def test_triton_quantize_fp8_act_quant(tile_size: int, test_eps: bool): |
| 89 | + device = "cuda" |
| 90 | + M, K = 256, 256 |
| 91 | + x = torch.randn(M, K, device=device) |
| 92 | + |
| 93 | + # set one scaling block to 0s, so if nan guards/EPS are not applied, the |
| 94 | + # quantized tensor will have NaNs due to division by 0 |
| 95 | + if test_eps: |
| 96 | + x[0, :tile_size] = 0.0 |
| 97 | + |
| 98 | + # Get the quantized tensor and scales using triton implementation |
| 99 | + # Use block_m=1 to match the narrow tiles (1 x tile_size) in the reference implementation |
| 100 | + triton_fp8, triton_scale = triton_quantize_fp8_block( |
| 101 | + x, block_m=1, block_k=tile_size |
| 102 | + ) |
| 103 | + assert not triton_fp8.isnan().any(), "fp8 output must not contain NaNs" |
| 104 | + |
| 105 | + # Get the quantized tensor and scales using reference implementation |
| 106 | + ref_fp8, ref_scale = torch_blockwise_scale_act_quant(x, tile_size=tile_size) |
| 107 | + assert not ref_fp8.isnan().any(), "fp8 output must not contain NaNs" |
| 108 | + |
| 109 | + # Convert both to float32 for comparison |
| 110 | + triton_fp32 = triton_fp8.to(torch.float32) |
| 111 | + ref_fp32 = ref_fp8.to(torch.float32) |
| 112 | + |
| 113 | + # Check that the quantized tensors are close |
| 114 | + # Note: We use a relatively high tolerance because the implementations might have |
| 115 | + # slight differences in how they handle edge cases, rounding, etc. |
| 116 | + assert torch.allclose(triton_fp32, ref_fp32, rtol=1e-2, atol=1e-2), ( |
| 117 | + f"Quantized tensors differ: max diff = {(triton_fp32 - ref_fp32).abs().max().item()}" |
| 118 | + ) |
| 119 | + |
| 120 | + # Check that the scales are close |
| 121 | + # Note: The scales might be stored differently (reciprocal vs. direct), so we need to |
| 122 | + # be careful about how we compare them |
| 123 | + |
| 124 | + # In triton_quantize_fp8_block, scales are stored as reciprocals (1/scale) |
| 125 | + # In torch_blockwise_scale_act_quant, scales are stored directly |
| 126 | + # So we need to take the reciprocal of one of them for comparison |
| 127 | + |
| 128 | + # Reshape triton_scale to match ref_scale shape for comparison |
| 129 | + triton_scale_reshaped = triton_scale.reshape(M, -1) |
| 130 | + |
| 131 | + # Compare reciprocal of triton_scale with ref_scale |
| 132 | + assert torch.allclose( |
| 133 | + 1.0 / triton_scale_reshaped, ref_scale, rtol=1e-2, atol=1e-2 |
| 134 | + ), ( |
| 135 | + f"Scales differ: max diff = {(1.0 / triton_scale_reshaped - ref_scale).abs().max().item()}" |
| 136 | + ) |
| 137 | + |
| 138 | + |
| 139 | +@skip_if_rocm("ROCm not supported") |
| 140 | +@pytest.mark.parametrize("tile_size", [128, 256]) |
| 141 | +@pytest.mark.parametrize("test_eps", [True, False]) |
| 142 | +def test_triton_quantize_fp8_weight_quant(tile_size: int, test_eps: bool): |
| 143 | + device = "cuda" |
| 144 | + # Make sure dimensions are multiples of tile_size for clean comparison |
| 145 | + M = tile_size * 2 |
| 146 | + K = tile_size * 2 |
| 147 | + x = torch.randn(M, K, device=device) |
| 148 | + |
| 149 | + # set one scaling block to 0s, so if nan guards/EPS are not applied, the |
| 150 | + # quantized tensor will have NaNs due to division by 0 |
| 151 | + if test_eps: |
| 152 | + x[:tile_size, :tile_size] = 0.0 |
| 153 | + |
| 154 | + # Get the quantized tensor and scales using triton implementation |
| 155 | + triton_fp8, triton_scale = triton_quantize_fp8_block( |
| 156 | + x, block_m=tile_size, block_k=tile_size |
| 157 | + ) |
| 158 | + assert not triton_fp8.isnan().any(), "fp8 output must not contain NaNs" |
| 159 | + |
| 160 | + # Get the quantized tensor and scales using reference implementation |
| 161 | + ref_fp8, ref_scale = torch_blockwise_scale_weight_quant(x, tile_size=tile_size) |
| 162 | + assert not ref_fp8.isnan().any(), "fp8 output must not contain NaNs" |
| 163 | + |
| 164 | + # Convert both to float32 for comparison |
| 165 | + triton_fp32 = triton_fp8.to(torch.float32) |
| 166 | + ref_fp32 = ref_fp8.to(torch.float32) |
| 167 | + |
| 168 | + # Check that the quantized tensors are close |
| 169 | + assert torch.allclose(triton_fp32, ref_fp32, rtol=1e-2, atol=1e-2), ( |
| 170 | + f"Quantized tensors differ: max diff = {(triton_fp32 - ref_fp32).abs().max().item()}" |
| 171 | + ) |
| 172 | + |
| 173 | + # Check that the scales are close |
| 174 | + # In triton_quantize_fp8_block, scales are stored as reciprocals (1/scale) |
| 175 | + # In torch_blockwise_scale_weight_quant, scales are stored directly |
| 176 | + |
| 177 | + # Compare reciprocal of triton_scale with ref_scale |
| 178 | + assert torch.allclose(1.0 / triton_scale, ref_scale, rtol=1e-2, atol=1e-2), ( |
| 179 | + f"Scales differ: max diff = {(1.0 / triton_scale - ref_scale).abs().max().item()}" |
| 180 | + ) |
0 commit comments