|
| 1 | +import ctypes |
| 2 | +from typing import Any, Dict, List |
| 3 | + |
| 4 | +import torch |
| 5 | +from core.challenge_base import ChallengeBase |
| 6 | + |
| 7 | + |
| 8 | +class Challenge(ChallengeBase): |
| 9 | + def __init__(self): |
| 10 | + super().__init__( |
| 11 | + name="Sparse Matrix-Dense Matrix Multiplication (SpMM)", |
| 12 | + atol=1e-03, |
| 13 | + rtol=1e-03, |
| 14 | + num_gpus=1, |
| 15 | + access_tier="free", |
| 16 | + ) |
| 17 | + |
| 18 | + def reference_impl( |
| 19 | + self, |
| 20 | + A: torch.Tensor, |
| 21 | + B: torch.Tensor, |
| 22 | + C: torch.Tensor, |
| 23 | + M: int, |
| 24 | + N: int, |
| 25 | + K: int, |
| 26 | + nnz: int, |
| 27 | + ): |
| 28 | + if A.shape == (M * N,): |
| 29 | + A_matrix = A.view(M, N) |
| 30 | + elif A.shape == (M, N): |
| 31 | + A_matrix = A |
| 32 | + else: |
| 33 | + raise AssertionError( |
| 34 | + f"A.shape {A.shape} does not match expected {(M * N,)} or {(M, N)}" |
| 35 | + ) |
| 36 | + if B.shape == (N * K,): |
| 37 | + B_matrix = B.view(N, K) |
| 38 | + elif B.shape == (N, K): |
| 39 | + B_matrix = B |
| 40 | + else: |
| 41 | + raise AssertionError( |
| 42 | + f"B.shape {B.shape} does not match expected {(N * K,)} or {(N, K)}" |
| 43 | + ) |
| 44 | + assert C.shape == (M, K) or C.shape == ( |
| 45 | + M * K, |
| 46 | + ), f"C.shape {C.shape} does not match expected {(M, K)} or {(M * K,)}" |
| 47 | + assert A_matrix.dtype == torch.float32 |
| 48 | + assert B_matrix.dtype == torch.float32 |
| 49 | + assert A_matrix.device.type == "cuda" |
| 50 | + assert B_matrix.device.type == "cuda" |
| 51 | + assert C.device.type == "cuda" |
| 52 | + result = torch.matmul(A_matrix, B_matrix) |
| 53 | + C.copy_(result.view(C.shape)) |
| 54 | + |
| 55 | + def get_solve_signature(self) -> Dict[str, tuple]: |
| 56 | + return { |
| 57 | + "A": (ctypes.POINTER(ctypes.c_float), "in"), |
| 58 | + "B": (ctypes.POINTER(ctypes.c_float), "in"), |
| 59 | + "C": (ctypes.POINTER(ctypes.c_float), "out"), |
| 60 | + "M": (ctypes.c_int, "in"), |
| 61 | + "N": (ctypes.c_int, "in"), |
| 62 | + "K": (ctypes.c_int, "in"), |
| 63 | + "nnz": (ctypes.c_int, "in"), |
| 64 | + } |
| 65 | + |
| 66 | + def generate_example_test(self) -> Dict[str, Any]: |
| 67 | + dtype = torch.float32 |
| 68 | + A = torch.tensor( |
| 69 | + [ |
| 70 | + [2.0, 0.0, 0.0, 1.0], |
| 71 | + [0.0, 3.0, 0.0, 0.0], |
| 72 | + [0.0, 0.0, 4.0, 0.0], |
| 73 | + ], |
| 74 | + device="cuda", |
| 75 | + dtype=dtype, |
| 76 | + ) |
| 77 | + B = torch.tensor( |
| 78 | + [ |
| 79 | + [1.0, 2.0], |
| 80 | + [3.0, 4.0], |
| 81 | + [5.0, 6.0], |
| 82 | + [7.0, 8.0], |
| 83 | + ], |
| 84 | + device="cuda", |
| 85 | + dtype=dtype, |
| 86 | + ) |
| 87 | + C = torch.empty((3, 2), device="cuda", dtype=dtype) |
| 88 | + return { |
| 89 | + "A": A, |
| 90 | + "B": B, |
| 91 | + "C": C, |
| 92 | + "M": 3, |
| 93 | + "N": 4, |
| 94 | + "K": 2, |
| 95 | + "nnz": 4, |
| 96 | + } |
| 97 | + |
| 98 | + def generate_functional_test(self) -> List[Dict[str, Any]]: |
| 99 | + dtype = torch.float32 |
| 100 | + tests = [] |
| 101 | + |
| 102 | + # edge_1x1x1 |
| 103 | + tests.append( |
| 104 | + { |
| 105 | + "A": torch.tensor([[3.0]], device="cuda", dtype=dtype), |
| 106 | + "B": torch.tensor([[2.0]], device="cuda", dtype=dtype), |
| 107 | + "C": torch.empty((1, 1), device="cuda", dtype=dtype), |
| 108 | + "M": 1, |
| 109 | + "N": 1, |
| 110 | + "K": 1, |
| 111 | + "nnz": 1, |
| 112 | + } |
| 113 | + ) |
| 114 | + |
| 115 | + # edge_2x2_k1_spmv_like |
| 116 | + tests.append( |
| 117 | + { |
| 118 | + "A": torch.tensor([[1.0, 0.0], [0.0, 2.0]], device="cuda", dtype=dtype), |
| 119 | + "B": torch.tensor([[3.0], [4.0]], device="cuda", dtype=dtype), |
| 120 | + "C": torch.empty((2, 1), device="cuda", dtype=dtype), |
| 121 | + "M": 2, |
| 122 | + "N": 2, |
| 123 | + "K": 1, |
| 124 | + "nnz": 2, |
| 125 | + } |
| 126 | + ) |
| 127 | + |
| 128 | + # edge_zero_matrix |
| 129 | + tests.append( |
| 130 | + { |
| 131 | + "A": torch.zeros((3, 3), device="cuda", dtype=dtype), |
| 132 | + "B": torch.tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]], device="cuda", dtype=dtype), |
| 133 | + "C": torch.empty((3, 2), device="cuda", dtype=dtype), |
| 134 | + "M": 3, |
| 135 | + "N": 3, |
| 136 | + "K": 2, |
| 137 | + "nnz": 0, |
| 138 | + } |
| 139 | + ) |
| 140 | + |
| 141 | + # edge_identity_a |
| 142 | + tests.append( |
| 143 | + { |
| 144 | + "A": torch.eye(4, device="cuda", dtype=dtype), |
| 145 | + "B": torch.tensor( |
| 146 | + [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0], [10.0, 11.0, 12.0]], |
| 147 | + device="cuda", |
| 148 | + dtype=dtype, |
| 149 | + ), |
| 150 | + "C": torch.empty((4, 3), device="cuda", dtype=dtype), |
| 151 | + "M": 4, |
| 152 | + "N": 4, |
| 153 | + "K": 3, |
| 154 | + "nnz": 4, |
| 155 | + } |
| 156 | + ) |
| 157 | + |
| 158 | + # power_of_2_16x16x8 |
| 159 | + M, N, K = 16, 16, 8 |
| 160 | + A_dense = torch.empty((M, N), device="cuda", dtype=dtype).uniform_(-2.0, 2.0) |
| 161 | + mask = torch.rand((M, N), device="cuda") > 0.65 |
| 162 | + A_sparse = A_dense * mask |
| 163 | + tests.append( |
| 164 | + { |
| 165 | + "A": A_sparse, |
| 166 | + "B": torch.empty((N, K), device="cuda", dtype=dtype).uniform_(-1.0, 1.0), |
| 167 | + "C": torch.empty((M, K), device="cuda", dtype=dtype), |
| 168 | + "M": M, |
| 169 | + "N": N, |
| 170 | + "K": K, |
| 171 | + "nnz": int(mask.sum().item()), |
| 172 | + } |
| 173 | + ) |
| 174 | + |
| 175 | + # power_of_2_64x32x16 |
| 176 | + M, N, K = 64, 32, 16 |
| 177 | + A_dense = torch.empty((M, N), device="cuda", dtype=dtype).uniform_(-3.0, 3.0) |
| 178 | + mask = torch.rand((M, N), device="cuda") > 0.70 |
| 179 | + A_sparse = A_dense * mask |
| 180 | + tests.append( |
| 181 | + { |
| 182 | + "A": A_sparse, |
| 183 | + "B": torch.empty((N, K), device="cuda", dtype=dtype).uniform_(-1.0, 1.0), |
| 184 | + "C": torch.empty((M, K), device="cuda", dtype=dtype), |
| 185 | + "M": M, |
| 186 | + "N": N, |
| 187 | + "K": K, |
| 188 | + "nnz": int(mask.sum().item()), |
| 189 | + } |
| 190 | + ) |
| 191 | + |
| 192 | + # non_power_of_2_negative_values |
| 193 | + M, N, K = 30, 50, 20 |
| 194 | + A_dense = torch.empty((M, N), device="cuda", dtype=dtype).uniform_(-5.0, 5.0) |
| 195 | + mask = torch.rand((M, N), device="cuda") > 0.65 |
| 196 | + A_sparse = A_dense * mask |
| 197 | + tests.append( |
| 198 | + { |
| 199 | + "A": A_sparse, |
| 200 | + "B": torch.empty((N, K), device="cuda", dtype=dtype).uniform_(-3.0, 3.0), |
| 201 | + "C": torch.empty((M, K), device="cuda", dtype=dtype), |
| 202 | + "M": M, |
| 203 | + "N": N, |
| 204 | + "K": K, |
| 205 | + "nnz": int(mask.sum().item()), |
| 206 | + } |
| 207 | + ) |
| 208 | + |
| 209 | + # non_power_of_2_255x100x33 |
| 210 | + M, N, K = 255, 100, 33 |
| 211 | + A_dense = torch.empty((M, N), device="cuda", dtype=dtype).uniform_(-2.0, 2.0) |
| 212 | + mask = torch.rand((M, N), device="cuda") > 0.70 |
| 213 | + A_sparse = A_dense * mask |
| 214 | + tests.append( |
| 215 | + { |
| 216 | + "A": A_sparse, |
| 217 | + "B": torch.empty((N, K), device="cuda", dtype=dtype).uniform_(-1.0, 1.0), |
| 218 | + "C": torch.empty((M, K), device="cuda", dtype=dtype), |
| 219 | + "M": M, |
| 220 | + "N": N, |
| 221 | + "K": K, |
| 222 | + "nnz": int(mask.sum().item()), |
| 223 | + } |
| 224 | + ) |
| 225 | + |
| 226 | + # realistic_1000x500x64 |
| 227 | + M, N, K = 1000, 500, 64 |
| 228 | + A_dense = torch.empty((M, N), device="cuda", dtype=dtype).uniform_(-1.0, 1.0) |
| 229 | + mask = torch.rand((M, N), device="cuda") > 0.65 |
| 230 | + A_sparse = A_dense * mask |
| 231 | + tests.append( |
| 232 | + { |
| 233 | + "A": A_sparse, |
| 234 | + "B": torch.empty((N, K), device="cuda", dtype=dtype).uniform_(-1.0, 1.0), |
| 235 | + "C": torch.empty((M, K), device="cuda", dtype=dtype), |
| 236 | + "M": M, |
| 237 | + "N": N, |
| 238 | + "K": K, |
| 239 | + "nnz": int(mask.sum().item()), |
| 240 | + } |
| 241 | + ) |
| 242 | + |
| 243 | + return tests |
| 244 | + |
| 245 | + def generate_performance_test(self) -> Dict[str, Any]: |
| 246 | + dtype = torch.float32 |
| 247 | + M = 4096 |
| 248 | + N = 2048 |
| 249 | + K = 512 |
| 250 | + A_dense = torch.empty((M, N), device="cuda", dtype=dtype).uniform_(-1.0, 1.0) |
| 251 | + mask = torch.rand((M, N), device="cuda") > 0.65 |
| 252 | + A_sparse = A_dense * mask |
| 253 | + nnz = int(mask.sum().item()) |
| 254 | + B = torch.empty((N, K), device="cuda", dtype=dtype).uniform_(-1.0, 1.0) |
| 255 | + C = torch.empty((M, K), device="cuda", dtype=dtype) |
| 256 | + return { |
| 257 | + "A": A_sparse, |
| 258 | + "B": B, |
| 259 | + "C": C, |
| 260 | + "M": M, |
| 261 | + "N": N, |
| 262 | + "K": K, |
| 263 | + "nnz": nnz, |
| 264 | + } |
0 commit comments