|
| 1 | +import argparse |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import torch |
| 5 | +import torch.nn.functional as F |
| 6 | + |
| 7 | +from mate.gdn_prefill import chunk_gated_delta_rule |
| 8 | +from mate.testing.utils import bench_kineto |
| 9 | + |
| 10 | + |
| 11 | +HEAD_CONFIGS = [ |
| 12 | + # (h_qk, h_v, d, label) |
| 13 | + # Qwen3.5-397B and 122B (h_k=16, h_v=64, d=128) under different TP |
| 14 | + (2, 8, 128, "397B/122B TP8"), |
| 15 | + (4, 16, 128, "397B/122B TP4"), |
| 16 | + (8, 32, 128, "397B/122B TP2"), |
| 17 | + (16, 64, 128, "397B/122B TP1"), |
| 18 | + # Qwen3.5-35B, 9B and 4B (h_k=16, h_v=32, d=128) |
| 19 | + (16, 32, 128, "35B/9B/4B TP1"), |
| 20 | + # Qwen3.5-27B (h_k=16, h_v=48, d=128) |
| 21 | + (16, 48, 128, "27B TP1"), |
| 22 | + # Qwen3.5-2B and 0.8B (h_k=16, h_v=16, d=128) |
| 23 | + (16, 16, 128, "2B/0.8B TP1"), |
| 24 | + # Symmetric heads |
| 25 | + (32, 32, 128, "Sym h32"), |
| 26 | +] |
| 27 | + |
| 28 | +SEQ_CONFIGS = [ |
| 29 | + # (cu_seqlen_endpoints, label) |
| 30 | + # endpoints are cumulative positions (leading 0 added automatically) |
| 31 | + ((8192,), "1x8192"), |
| 32 | + ((4096,), "1x4096"), |
| 33 | + ((2048,), "1x2048"), |
| 34 | + ((1024 * 6, 8192), "6144+2048"), |
| 35 | + ((1024 * 4, 8192), "4096+4096"), |
| 36 | + ((1024 * 2, 8192), "2048+6144"), |
| 37 | + ((1024 * 1, 8192), "1024+7168"), |
| 38 | + ((2048, 2048 * 2, 2048 * 3, 8192), "2048x4"), |
| 39 | + (tuple(1024 * (i + 1) for i in range(8)), "1024x8"), |
| 40 | +] |
| 41 | + |
| 42 | + |
| 43 | +def _gdn_tflops(total_tokens: int, h_o: int, d: int, time_ms: float) -> float: |
| 44 | + # 2 GEMMs (kv outer product + q@state), MAC counted as 2 FLOPs. |
| 45 | + flops = 2 * 2 * total_tokens * h_o * d * d |
| 46 | + return flops / time_ms / 1e9 |
| 47 | + |
| 48 | + |
| 49 | +def bench_mate( |
| 50 | + endpoints: tuple[int, ...], |
| 51 | + h_qk: int, |
| 52 | + h_v: int, |
| 53 | + d: int, |
| 54 | + chunk_size: int, |
| 55 | + dtype: torch.dtype, |
| 56 | + num_tests: int, |
| 57 | +) -> float: |
| 58 | + device = "musa" |
| 59 | + num_seqs = len(endpoints) |
| 60 | + total_tokens = endpoints[-1] |
| 61 | + head_o = max(h_qk, h_v) |
| 62 | + |
| 63 | + cu_seqlens = torch.tensor([0] + list(endpoints), dtype=torch.int32, device=device) |
| 64 | + |
| 65 | + q = torch.randn((total_tokens, h_qk, d), dtype=dtype, device=device) |
| 66 | + k = F.normalize( |
| 67 | + torch.randn((total_tokens, h_qk, d), dtype=torch.float32, device=device), |
| 68 | + p=2, |
| 69 | + dim=-1, |
| 70 | + ).to(dtype) |
| 71 | + v = torch.randn((total_tokens, h_v, d), dtype=dtype, device=device) |
| 72 | + alpha = torch.exp( |
| 73 | + -torch.rand(total_tokens, head_o, dtype=torch.float32, device=device) |
| 74 | + ) |
| 75 | + beta = torch.sigmoid( |
| 76 | + torch.randn(total_tokens, head_o, dtype=torch.float32, device=device) |
| 77 | + ) |
| 78 | + h0 = torch.randn((num_seqs, head_o, d, d), dtype=torch.float32, device=device) |
| 79 | + out = torch.empty((total_tokens, head_o, d), dtype=dtype, device=device) |
| 80 | + state_out = torch.empty_like(h0) |
| 81 | + |
| 82 | + kernel_times = bench_kineto( |
| 83 | + lambda: chunk_gated_delta_rule( |
| 84 | + q=q, |
| 85 | + k=k, |
| 86 | + v=v, |
| 87 | + g=alpha, |
| 88 | + beta=beta, |
| 89 | + scale=None, |
| 90 | + initial_state=h0, |
| 91 | + output_final_state=True, |
| 92 | + cu_seqlens=cu_seqlens, |
| 93 | + use_qk_l2norm_in_kernel=False, |
| 94 | + chunk_size=chunk_size, |
| 95 | + output=out, |
| 96 | + output_state=state_out, |
| 97 | + ), |
| 98 | + kernel_names=( |
| 99 | + "fused_prepare_compute_w_u_kernel", |
| 100 | + "h_recurrence_kernel", |
| 101 | + "output_o_kernel", |
| 102 | + ), |
| 103 | + num_tests=num_tests, |
| 104 | + suppress_kineto_output=True, |
| 105 | + flush_l2=True, |
| 106 | + with_multiple_kernels=True, |
| 107 | + ) |
| 108 | + return float(np.sum(np.asarray(kernel_times, dtype=np.float64))) |
| 109 | + |
| 110 | + |
| 111 | +def main(): |
| 112 | + parser = argparse.ArgumentParser(description="Benchmark mate.gdn_prefill") |
| 113 | + parser.add_argument("--chunk-size", type=int, default=64) |
| 114 | + parser.add_argument( |
| 115 | + "--num-tests", |
| 116 | + type=int, |
| 117 | + default=10, |
| 118 | + help="Number of profiling iterations for bench_kineto", |
| 119 | + ) |
| 120 | + parser.add_argument( |
| 121 | + "--dtype", |
| 122 | + type=str, |
| 123 | + default="fp16", |
| 124 | + choices=["fp16", "bf16"], |
| 125 | + help="Input dtype for q/k/v", |
| 126 | + ) |
| 127 | + args = parser.parse_args() |
| 128 | + |
| 129 | + if not (hasattr(torch, "musa") and torch.musa.is_available()): |
| 130 | + raise RuntimeError("MUSA device is not available.") |
| 131 | + |
| 132 | + dtype = torch.float16 if args.dtype == "fp16" else torch.bfloat16 |
| 133 | + |
| 134 | + print(f"\nMUSA: {torch.musa.get_device_name(0)}") |
| 135 | + print("Kernel: mate.gdn_prefill.chunk_gated_delta_rule") |
| 136 | + print(f"chunk_size={args.chunk_size}, dtype={args.dtype}") |
| 137 | + print() |
| 138 | + |
| 139 | + header = ( |
| 140 | + f"{'Heads':<15s} {'Seqlens':<16s} {'h_qk':>4s} {'h_v':>4s}" |
| 141 | + f" {'MATE (MUSA)':>12s} {'TFLOPS':>7s}" |
| 142 | + ) |
| 143 | + print(header) |
| 144 | + print("-" * len(header)) |
| 145 | + |
| 146 | + for h_qk, h_v, d, h_label in HEAD_CONFIGS: |
| 147 | + for endpoints, s_label in SEQ_CONFIGS: |
| 148 | + total_tokens = endpoints[-1] |
| 149 | + mate_ms = bench_mate( |
| 150 | + endpoints=endpoints, |
| 151 | + h_qk=h_qk, |
| 152 | + h_v=h_v, |
| 153 | + d=d, |
| 154 | + chunk_size=args.chunk_size, |
| 155 | + dtype=dtype, |
| 156 | + num_tests=args.num_tests, |
| 157 | + ) |
| 158 | + tflops = _gdn_tflops(total_tokens, max(h_qk, h_v), d, mate_ms) |
| 159 | + print( |
| 160 | + f"{h_label:<15s} {s_label:<16s} {h_qk:>4d} {h_v:>4d}" |
| 161 | + f" {mate_ms:>11.3f}ms {tflops:>6.1f}" |
| 162 | + ) |
| 163 | + print() |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == "__main__": |
| 167 | + main() |
0 commit comments