|
| 1 | +# Owner(s): ["module: inductor"] |
| 2 | + |
| 3 | + |
| 4 | +import unittest |
| 5 | + |
| 6 | +import torch |
| 7 | +from torch import Tensor |
| 8 | +from torch._inductor import config |
| 9 | +from torch._inductor.codegen.cuda.cuda_env import is_datacenter_blackwell_arch |
| 10 | +from torch._inductor.test_case import run_tests, TestCase as InductorTestCase |
| 11 | +from torch._inductor.utils import ensure_cute_available |
| 12 | +from torch.testing._internal.common_utils import ( |
| 13 | + instantiate_parametrized_tests, |
| 14 | + parametrize, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@unittest.skipIf( |
| 19 | + not (ensure_cute_available() and is_datacenter_blackwell_arch()), |
| 20 | + "CuTeDSL library or Blackwell device not available", |
| 21 | +) |
| 22 | +@instantiate_parametrized_tests |
| 23 | +class TestCuTeDSLGroupedGemm(InductorTestCase): |
| 24 | + def _get_inputs( |
| 25 | + self, |
| 26 | + group_size: int, |
| 27 | + M_hint: int, |
| 28 | + K: int, |
| 29 | + N: int, |
| 30 | + device: str, |
| 31 | + dtype: torch.dtype, |
| 32 | + alignment: int = 16, |
| 33 | + ) -> tuple[Tensor, Tensor, Tensor]: |
| 34 | + # --- Random, tile-aligned M sizes --- |
| 35 | + M_sizes = ( |
| 36 | + torch.randint(1, (M_hint // alignment) + 1, (group_size,), dtype=torch.int) |
| 37 | + * alignment |
| 38 | + ) |
| 39 | + |
| 40 | + M_total = torch.sum(M_sizes).item() |
| 41 | + |
| 42 | + # --- Construct input tensors --- |
| 43 | + A = torch.randn(int(M_total), K, dtype=dtype, device=device) * 0.1 |
| 44 | + B = torch.randn((group_size, K, N), dtype=dtype, device=device) * 0.01 |
| 45 | + |
| 46 | + # --- Build offsets (no leading zero, strictly increasing) --- |
| 47 | + offsets = torch.cumsum(M_sizes, dim=0).to(dtype=torch.int32, device=device) |
| 48 | + |
| 49 | + return (A, B, offsets) |
| 50 | + |
| 51 | + @parametrize("group_size", (2, 8)) |
| 52 | + @parametrize("M_hint", (256, 1024)) |
| 53 | + @parametrize("K", (64, 128)) |
| 54 | + @parametrize("N", (128, 256)) |
| 55 | + def test_grouped_gemm_basic(self, group_size: int, M_hint: int, K: int, N: int): |
| 56 | + device = "cuda" |
| 57 | + dtype = torch.bfloat16 |
| 58 | + |
| 59 | + A, B, offsets = self._get_inputs(group_size, M_hint, K, N, device, dtype) |
| 60 | + |
| 61 | + def grouped_gemm_fn(A_packed, B_batched, offs): |
| 62 | + return torch._grouped_mm(A_packed, B_batched, offs=offs) |
| 63 | + |
| 64 | + # Eager execution |
| 65 | + c_eager = grouped_gemm_fn(A, B, offsets) |
| 66 | + |
| 67 | + # Test with Cute backend |
| 68 | + with config.patch( |
| 69 | + { |
| 70 | + "max_autotune": True, |
| 71 | + "max_autotune_gemm_backends": "CUTEDSL", |
| 72 | + "test_configs.autotune_choice_name_regex": "cutedsl", |
| 73 | + "autotune_fallback_to_aten": False, |
| 74 | + } |
| 75 | + ): |
| 76 | + grouped_gemm_compiled = torch.compile( |
| 77 | + grouped_gemm_fn, backend="inductor", dynamic=False |
| 78 | + ) |
| 79 | + c_compiled = grouped_gemm_compiled(A, B, offsets) |
| 80 | + |
| 81 | + self.assertEqual(c_eager.dtype, dtype) |
| 82 | + self.assertEqual(c_compiled.dtype, dtype) |
| 83 | + torch.testing.assert_close(c_eager, c_compiled) |
| 84 | + |
| 85 | + @parametrize("layout_A", ("contiguous", "offset", "padded", "view")) |
| 86 | + @parametrize("layout_B", ("contiguous", "broadcasted")) |
| 87 | + def test_grouped_gemm_assorted_layouts( |
| 88 | + self, |
| 89 | + layout_A: str, |
| 90 | + layout_B: str, |
| 91 | + ): |
| 92 | + device = "cuda" |
| 93 | + dtype = torch.bfloat16 |
| 94 | + |
| 95 | + G, K, N = 8, 64, 128 |
| 96 | + M_sizes = [128] * G |
| 97 | + sum_M = sum(M_sizes) |
| 98 | + offsets = torch.tensor( |
| 99 | + [sum(M_sizes[: i + 1]) for i in range(G)], dtype=torch.int32, device=device |
| 100 | + ) |
| 101 | + |
| 102 | + A_base = torch.randn(sum_M, K, device=device, dtype=dtype) |
| 103 | + A = A_base |
| 104 | + |
| 105 | + if layout_A == "offset": |
| 106 | + # allocate bigger buffer than needed, use nonzero storage offset |
| 107 | + storage = torch.randn(sum_M * K + 512, device=device, dtype=dtype) |
| 108 | + offset = 128 # skip first 128 elements |
| 109 | + A = torch.as_strided(storage[offset:], (sum_M, K), (K, 1)) |
| 110 | + elif layout_A == "padded": |
| 111 | + # simulate row pitch > K (row_stride = K + pad) |
| 112 | + row_pitch = K + 8 |
| 113 | + storage = torch.randn(sum_M * row_pitch, device=device, dtype=dtype) |
| 114 | + A = torch.as_strided(storage, (sum_M, K), (row_pitch, 1)) |
| 115 | + elif layout_A == "view": |
| 116 | + A_storage = torch.randn(sum_M * K, device=device, dtype=dtype) |
| 117 | + A = A_storage.view(sum_M, K) |
| 118 | + assert A._base is not None |
| 119 | + assert A.shape == (sum_M, K) |
| 120 | + |
| 121 | + B = torch.randn((G, K, N), dtype=dtype, device=device) * 0.01 |
| 122 | + |
| 123 | + if layout_B == "broadcasted": |
| 124 | + # Broadcast B across groups (zero stride along G) |
| 125 | + B = B[0].expand(G, K, N) |
| 126 | + assert B.stride(0) == 0 |
| 127 | + |
| 128 | + def grouped_gemm_fn(A_packed, B_batched, offs): |
| 129 | + return torch._grouped_mm(A_packed, B_batched, offs=offs) |
| 130 | + |
| 131 | + # --- eager --- |
| 132 | + c_eager = grouped_gemm_fn(A, B, offsets) |
| 133 | + |
| 134 | + # --- compiled (CUTE backend) --- |
| 135 | + with config.patch( |
| 136 | + { |
| 137 | + "max_autotune": True, |
| 138 | + "max_autotune_gemm_backends": "CUTEDSL", |
| 139 | + "test_configs.autotune_choice_name_regex": "cutedsl", |
| 140 | + "autotune_fallback_to_aten": False, |
| 141 | + } |
| 142 | + ): |
| 143 | + grouped_gemm_compiled = torch.compile( |
| 144 | + grouped_gemm_fn, backend="inductor", dynamic=False |
| 145 | + ) |
| 146 | + c_compiled = grouped_gemm_compiled(A, B, offsets) |
| 147 | + |
| 148 | + self.assertEqual(c_eager.dtype, dtype) |
| 149 | + self.assertEqual(c_compiled.dtype, dtype) |
| 150 | + torch.testing.assert_close(c_eager, c_compiled) |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + run_tests() |
0 commit comments