|
| 1 | +import itertools |
| 2 | + |
| 3 | +import torch |
| 4 | + |
| 5 | +import torchao |
| 6 | +from torchao.ops import s8s4_linear_cutlass |
| 7 | +from torchao.quantization.utils import group_quantize_tensor_symmetric |
| 8 | +from torchao.utils import compute_max_diff |
| 9 | + |
| 10 | +import pytest |
| 11 | + |
| 12 | + |
| 13 | +S8S4_LINEAR_CUTLASS_DTYPE = [torch.float16, torch.bfloat16] |
| 14 | +S8S4_LINEAR_CUTLASS_BATCH_SIZE = [1, 4, 8, 16, 32, 64] |
| 15 | +S8S4_LINEAR_CUTLASS_SIZE_MNK = [ |
| 16 | + (2, 512, 128), |
| 17 | + (3, 2048, 2048), |
| 18 | + (4, 3584, 640), |
| 19 | + (13, 8704, 8576), |
| 20 | + (26, 18944, 1664), |
| 21 | + (67, 6656, 1408), |
| 22 | +] |
| 23 | +S8S4_LINEAR_CUTLASS_USE_BIAS = [False, True] |
| 24 | +S8S4_LINEAR_CUTLASS_TEST_PARAMS = list( |
| 25 | + itertools.product( |
| 26 | + S8S4_LINEAR_CUTLASS_DTYPE, |
| 27 | + S8S4_LINEAR_CUTLASS_BATCH_SIZE, |
| 28 | + S8S4_LINEAR_CUTLASS_SIZE_MNK, |
| 29 | + S8S4_LINEAR_CUTLASS_USE_BIAS, |
| 30 | + ) |
| 31 | +) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.skipif(not torch.cuda.is_available(), reason="CUDA not available") |
| 35 | +@pytest.mark.parametrize( |
| 36 | + "dtype, batch_size, size_mnk, use_bias", S8S4_LINEAR_CUTLASS_TEST_PARAMS |
| 37 | +) |
| 38 | +def test_s8s4_linear_cutlass(dtype, batch_size, size_mnk, use_bias): |
| 39 | + size_m, size_n, size_k = size_mnk |
| 40 | + |
| 41 | + input = torch.randn((batch_size, size_m, size_k), dtype=dtype, device="cuda") |
| 42 | + weight = torch.rand((size_n, size_k), dtype=dtype, device="cuda") |
| 43 | + bias = torch.rand((size_n,), dtype=dtype, device="cuda") if use_bias else None |
| 44 | + |
| 45 | + input_2d = input.view(-1, input.shape[-1]) |
| 46 | + input_2d_s8, input_2d_scales, input_2d_zeros = group_quantize_tensor_symmetric( |
| 47 | + input_2d, 8, size_k, dtype |
| 48 | + ) |
| 49 | + assert torch.all(input_2d_zeros == 0) |
| 50 | + input_s8 = input_2d_s8.reshape(input.shape) |
| 51 | + input_scales = input_2d_scales.reshape(input.shape[:-1]) |
| 52 | + |
| 53 | + weight_s8, weight_scales, weight_zeros = group_quantize_tensor_symmetric( |
| 54 | + weight, 4, size_n, dtype |
| 55 | + ) |
| 56 | + assert torch.all(weight_zeros == 0) |
| 57 | + weight_s4 = ((weight_s8[:, 1::2] & 0xF) << 4) | (weight_s8[:, 0::2] & 0xF) |
| 58 | + |
| 59 | + # If torch.nn.functional.linear(input, weight, bias) used as |
| 60 | + # reference, the error would be too big. The calculation below is |
| 61 | + # approximately what s8s4_linear_cutlass kernel is doing (except |
| 62 | + # that matrrix multiplication is over integers there)). |
| 63 | + size_m_2d = input_2d.shape[0] |
| 64 | + output_ref = ( |
| 65 | + (input_2d_s8.to(dtype) @ weight_s8.to(dtype).T) |
| 66 | + * input_2d_scales.view(size_m_2d, 1) |
| 67 | + * weight_scales.view(1, size_n) |
| 68 | + ) |
| 69 | + if bias is not None: |
| 70 | + output_ref += bias |
| 71 | + output_ref = output_ref.reshape(input.shape[:-1] + (size_n,)) |
| 72 | + |
| 73 | + fn_inputs = (input_s8, input_scales, weight_s4, weight_scales, bias) |
| 74 | + try: |
| 75 | + output = s8s4_linear_cutlass(*fn_inputs) |
| 76 | + except NotImplementedError as e: |
| 77 | + pytest.xfail("s8s4_linear_cutlass() op not implemented") |
| 78 | + |
| 79 | + max_diff = compute_max_diff(output, output_ref) |
| 80 | + assert max_diff < 5e-3 |
0 commit comments