|
| 1 | +"""Benchmark for kbit grouped expert GEMM kernel. |
| 2 | +
|
| 3 | +Compares: |
| 4 | +1. Grouped GEMM (one kernel launch for all experts) |
| 5 | +2. Individual kbit_gemm_prod calls (one per expert, sequential) |
| 6 | +3. cuBLAS fp16 GEMM (one per expert, sequential) |
| 7 | +
|
| 8 | +Simulates MoE inference with varying batch sizes and expert counts. |
| 9 | +""" |
| 10 | + |
| 11 | +import argparse |
| 12 | +import sys |
| 13 | +import time |
| 14 | + |
| 15 | +import torch |
| 16 | + |
| 17 | +sys.path.insert(0, ".") |
| 18 | +import bitsandbytes # noqa: E402 |
| 19 | +from bitsandbytes import _ops # noqa: E402, F401 |
| 20 | +from scipy.stats import norm # noqa: E402 |
| 21 | + |
| 22 | +BLOCKSIZE = 32 |
| 23 | + |
| 24 | + |
| 25 | +def create_normal_float_codebook(k: int) -> torch.Tensor: |
| 26 | + n_levels = 1 << k |
| 27 | + quantiles = torch.linspace(0.5 / n_levels, 1.0 - 0.5 / n_levels, n_levels) |
| 28 | + values = torch.tensor(norm.ppf(quantiles.numpy()), dtype=torch.float32) |
| 29 | + values = values / values.abs().max() |
| 30 | + return values |
| 31 | + |
| 32 | + |
| 33 | +def prepare_expert_weights(K_dim, N, k, num_experts): |
| 34 | + codebook = create_normal_float_codebook(k).cuda() |
| 35 | + packed_list = [] |
| 36 | + absmax_list = [] |
| 37 | + W_list = [] |
| 38 | + |
| 39 | + for _ in range(num_experts): |
| 40 | + W = torch.randn(N, K_dim, dtype=torch.float16, device="cuda") |
| 41 | + packed_flat, absmax = torch.ops.bitsandbytes.quantize_kbit( |
| 42 | + W.reshape(-1), codebook, k |
| 43 | + ) |
| 44 | + packed_tiled, absmax_tiled = torch.ops.bitsandbytes.repack_kbit( |
| 45 | + packed_flat, absmax.cuda(), K_dim, N, k |
| 46 | + ) |
| 47 | + packed_list.append(packed_tiled) |
| 48 | + absmax_list.append(absmax_tiled) |
| 49 | + W_list.append(W) |
| 50 | + |
| 51 | + B_packed_all = torch.cat(packed_list, dim=0) |
| 52 | + B_absmax_all = torch.cat(absmax_list, dim=0) |
| 53 | + return B_packed_all, B_absmax_all, codebook, W_list, packed_list, absmax_list |
| 54 | + |
| 55 | + |
| 56 | +def bench_grouped_gemm(A_concat, B_packed_all, B_absmax_all, codebook, |
| 57 | + expert_offsets, K_dim, N, k, num_experts, |
| 58 | + warmup=20, iters=200): |
| 59 | + for _ in range(warmup): |
| 60 | + torch.ops.bitsandbytes.kbit_grouped_gemm( |
| 61 | + A_concat, B_packed_all, B_absmax_all, codebook, |
| 62 | + expert_offsets, K_dim, N, k, num_experts, |
| 63 | + ) |
| 64 | + torch.cuda.synchronize() |
| 65 | + |
| 66 | + start = time.perf_counter() |
| 67 | + for _ in range(iters): |
| 68 | + torch.ops.bitsandbytes.kbit_grouped_gemm( |
| 69 | + A_concat, B_packed_all, B_absmax_all, codebook, |
| 70 | + expert_offsets, K_dim, N, k, num_experts, |
| 71 | + ) |
| 72 | + torch.cuda.synchronize() |
| 73 | + return (time.perf_counter() - start) / iters |
| 74 | + |
| 75 | + |
| 76 | +def bench_individual_kbit(A_list, packed_list, absmax_list, codebook, |
| 77 | + K_dim, N, k, warmup=20, iters=200): |
| 78 | + for _ in range(warmup): |
| 79 | + for i in range(len(A_list)): |
| 80 | + torch.ops.bitsandbytes.kbit_gemm_prod( |
| 81 | + A_list[i], packed_list[i], absmax_list[i], codebook, |
| 82 | + K_dim, N, k, 1, |
| 83 | + ) |
| 84 | + torch.cuda.synchronize() |
| 85 | + |
| 86 | + start = time.perf_counter() |
| 87 | + for _ in range(iters): |
| 88 | + for i in range(len(A_list)): |
| 89 | + torch.ops.bitsandbytes.kbit_gemm_prod( |
| 90 | + A_list[i], packed_list[i], absmax_list[i], codebook, |
| 91 | + K_dim, N, k, 1, |
| 92 | + ) |
| 93 | + torch.cuda.synchronize() |
| 94 | + return (time.perf_counter() - start) / iters |
| 95 | + |
| 96 | + |
| 97 | +def bench_individual_cublas(A_list, W_list, warmup=20, iters=200): |
| 98 | + for _ in range(warmup): |
| 99 | + for i in range(len(A_list)): |
| 100 | + torch.mm(A_list[i], W_list[i].T) |
| 101 | + torch.cuda.synchronize() |
| 102 | + |
| 103 | + start = time.perf_counter() |
| 104 | + for _ in range(iters): |
| 105 | + for i in range(len(A_list)): |
| 106 | + torch.mm(A_list[i], W_list[i].T) |
| 107 | + torch.cuda.synchronize() |
| 108 | + return (time.perf_counter() - start) / iters |
| 109 | + |
| 110 | + |
| 111 | +def main(): |
| 112 | + parser = argparse.ArgumentParser(description="Benchmark grouped expert GEMM") |
| 113 | + parser.add_argument("--k", type=int, default=4, help="Bit width (2-5)") |
| 114 | + parser.add_argument("--warmup", type=int, default=20) |
| 115 | + parser.add_argument("--iters", type=int, default=200) |
| 116 | + args = parser.parse_args() |
| 117 | + |
| 118 | + k = args.k |
| 119 | + |
| 120 | + # MoE scenarios |
| 121 | + configs = [ |
| 122 | + # (K_dim, N, num_experts, M_per_expert, description) |
| 123 | + # Qwen3-Coder-Next gate/up expert |
| 124 | + (2048, 512, 8, 1, "Qwen3 gate/up 8exp M=1"), |
| 125 | + (2048, 512, 8, 4, "Qwen3 gate/up 8exp M=4"), |
| 126 | + (2048, 512, 8, 8, "Qwen3 gate/up 8exp M=8"), |
| 127 | + (2048, 512, 32, 1, "Qwen3 gate/up 32exp M=1"), |
| 128 | + (2048, 512, 64, 1, "Qwen3 gate/up 64exp M=1"), |
| 129 | + (2048, 512, 128, 1, "Qwen3 gate/up 128exp M=1"), |
| 130 | + # Qwen3-Coder-Next down expert |
| 131 | + (512, 2048, 8, 1, "Qwen3 down 8exp M=1"), |
| 132 | + (512, 2048, 8, 4, "Qwen3 down 8exp M=4"), |
| 133 | + (512, 2048, 64, 1, "Qwen3 down 64exp M=1"), |
| 134 | + # GLM-4.7-Flash routed expert |
| 135 | + (2048, 1536, 8, 1, "GLM4.7 routed 8exp M=1"), |
| 136 | + (2048, 1536, 8, 4, "GLM4.7 routed 8exp M=4"), |
| 137 | + (2048, 1536, 64, 1, "GLM4.7 routed 64exp M=1"), |
| 138 | + ] |
| 139 | + |
| 140 | + print(f"Grouped Expert GEMM Benchmark: K={k}") |
| 141 | + print(f"Warmup={args.warmup}, Iters={args.iters}") |
| 142 | + print() |
| 143 | + print(f"{'Description':<30} | {'K_dim':>5} {'N':>5} {'#exp':>4} {'M/e':>3} | " |
| 144 | + f"{'Grouped(us)':>11} {'Indiv(us)':>10} {'cuBLAS(us)':>10} | " |
| 145 | + f"{'vs Indiv':>8} {'vs cuBLAS':>9}") |
| 146 | + print("-" * 120) |
| 147 | + |
| 148 | + for K_dim, N, num_experts, M_per_expert, desc in configs: |
| 149 | + N_padded = ((N + 127) // 128) * 128 |
| 150 | + |
| 151 | + B_packed_all, B_absmax_all, codebook, W_list, packed_list, absmax_list = ( |
| 152 | + prepare_expert_weights(K_dim, N_padded, k, num_experts) |
| 153 | + ) |
| 154 | + |
| 155 | + # Build activations |
| 156 | + A_list = [] |
| 157 | + offsets = [0] |
| 158 | + for i in range(num_experts): |
| 159 | + A_i = torch.randn(M_per_expert, K_dim, dtype=torch.float16, device="cuda") |
| 160 | + A_list.append(A_i) |
| 161 | + offsets.append(offsets[-1] + M_per_expert) |
| 162 | + |
| 163 | + A_concat = torch.cat(A_list, dim=0) |
| 164 | + expert_offsets = torch.tensor(offsets, dtype=torch.int32, device="cuda") |
| 165 | + |
| 166 | + # Benchmark grouped |
| 167 | + t_grouped = bench_grouped_gemm( |
| 168 | + A_concat, B_packed_all, B_absmax_all, codebook, |
| 169 | + expert_offsets, K_dim, N_padded, k, num_experts, |
| 170 | + warmup=args.warmup, iters=args.iters, |
| 171 | + ) |
| 172 | + |
| 173 | + # Benchmark individual kbit |
| 174 | + t_individual = bench_individual_kbit( |
| 175 | + A_list, packed_list, absmax_list, codebook, |
| 176 | + K_dim, N_padded, k, |
| 177 | + warmup=args.warmup, iters=args.iters, |
| 178 | + ) |
| 179 | + |
| 180 | + # Benchmark individual cuBLAS |
| 181 | + W_fp16_list = [W.half().cuda() for W in W_list] |
| 182 | + t_cublas = bench_individual_cublas( |
| 183 | + A_list, W_fp16_list, |
| 184 | + warmup=args.warmup, iters=args.iters, |
| 185 | + ) |
| 186 | + |
| 187 | + speedup_vs_indiv = t_individual / t_grouped |
| 188 | + speedup_vs_cublas = t_cublas / t_grouped |
| 189 | + |
| 190 | + print(f"{desc:<30} | {K_dim:5d} {N_padded:5d} {num_experts:4d} {M_per_expert:3d} | " |
| 191 | + f"{t_grouped*1e6:11.1f} {t_individual*1e6:10.1f} {t_cublas*1e6:10.1f} | " |
| 192 | + f"{speedup_vs_indiv:7.2f}x {speedup_vs_cublas:8.2f}x") |
| 193 | + |
| 194 | + print() |
| 195 | + |
| 196 | + |
| 197 | +if __name__ == "__main__": |
| 198 | + main() |
0 commit comments