|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | + |
| 5 | +import torch |
| 6 | + |
| 7 | +import mate |
| 8 | + |
| 9 | +from mate.testing.utils import bench_kineto |
| 10 | + |
| 11 | + |
| 12 | +DEFAULT_BATCH_SIZES = (1, 2, 4, 8, 16, 32, 64, 128, 256, 512) |
| 13 | +DEFAULT_HEAD_CONFIGS = ("8,16", "16,32", "16,64") |
| 14 | +KERNEL_NAME = "gated_deltanet_decode_fp32_vk" |
| 15 | + |
| 16 | + |
| 17 | +def _dtype_size(dtype: torch.dtype) -> int: |
| 18 | + return torch.empty((), dtype=dtype).element_size() |
| 19 | + |
| 20 | + |
| 21 | +def _parse_head_config(spec: str) -> tuple[int, int]: |
| 22 | + parts = [part.strip() for part in spec.split(",") if part.strip()] |
| 23 | + if len(parts) != 2: |
| 24 | + raise ValueError( |
| 25 | + f"Invalid head config {spec!r}. Expected 'Hq,Hv', for example '16,32'." |
| 26 | + ) |
| 27 | + num_q_heads, num_v_heads = (int(part) for part in parts) |
| 28 | + if num_q_heads <= 0 or num_v_heads <= 0: |
| 29 | + raise ValueError(f"Head counts must be positive, got {spec!r}.") |
| 30 | + return num_q_heads, num_v_heads |
| 31 | + |
| 32 | + |
| 33 | +def _gdn_decode_flops( |
| 34 | + batch_size: int, num_q_heads: int, num_v_heads: int, head_size: int |
| 35 | +) -> int: |
| 36 | + num_o_heads = max(num_q_heads, num_v_heads) |
| 37 | + return 6 * batch_size * num_o_heads * head_size * head_size |
| 38 | + |
| 39 | + |
| 40 | +def _gdn_decode_bytes( |
| 41 | + batch_size: int, |
| 42 | + num_q_heads: int, |
| 43 | + num_v_heads: int, |
| 44 | + head_size: int, |
| 45 | + input_dtype: torch.dtype, |
| 46 | + output_dtype: torch.dtype, |
| 47 | +) -> int: |
| 48 | + num_o_heads = max(num_q_heads, num_v_heads) |
| 49 | + elem_size = _dtype_size(input_dtype) |
| 50 | + |
| 51 | + q_bytes = batch_size * num_q_heads * head_size * elem_size |
| 52 | + k_bytes = batch_size * num_q_heads * head_size * elem_size |
| 53 | + v_bytes = batch_size * num_v_heads * head_size * elem_size |
| 54 | + o_bytes = batch_size * num_o_heads * head_size * _dtype_size(output_dtype) |
| 55 | + state_bytes = 2 * batch_size * num_v_heads * head_size * head_size * 4 |
| 56 | + A_log_bytes = num_v_heads * 4 |
| 57 | + dt_bias_bytes = num_v_heads * 4 |
| 58 | + a_bytes = batch_size * num_v_heads * elem_size |
| 59 | + b_bytes = batch_size * num_v_heads * elem_size |
| 60 | + |
| 61 | + return ( |
| 62 | + q_bytes |
| 63 | + + k_bytes |
| 64 | + + v_bytes |
| 65 | + + o_bytes |
| 66 | + + state_bytes |
| 67 | + + A_log_bytes |
| 68 | + + dt_bias_bytes |
| 69 | + + a_bytes |
| 70 | + + b_bytes |
| 71 | + ) |
| 72 | + |
| 73 | + |
| 74 | +def _make_inputs( |
| 75 | + batch_size: int, |
| 76 | + num_q_heads: int, |
| 77 | + num_v_heads: int, |
| 78 | + head_size: int, |
| 79 | + dtype: torch.dtype, |
| 80 | +) -> tuple[torch.Tensor, ...]: |
| 81 | + device = torch.device("musa") |
| 82 | + q = torch.randn((batch_size, 1, num_q_heads, head_size), dtype=dtype, device=device) |
| 83 | + k = torch.randn((batch_size, 1, num_q_heads, head_size), dtype=dtype, device=device) |
| 84 | + v = torch.randn((batch_size, 1, num_v_heads, head_size), dtype=dtype, device=device) |
| 85 | + state = torch.randn( |
| 86 | + (batch_size, num_v_heads, head_size, head_size), |
| 87 | + dtype=torch.float32, |
| 88 | + device=device, |
| 89 | + ) |
| 90 | + A_log = torch.randn((num_v_heads,), dtype=torch.float32, device=device) * 0.1 |
| 91 | + dt_bias = torch.randn((num_v_heads,), dtype=torch.float32, device=device) * 0.1 |
| 92 | + a = torch.randn((batch_size, 1, num_v_heads), dtype=dtype, device=device) * 0.1 |
| 93 | + b = torch.randn((batch_size, 1, num_v_heads), dtype=dtype, device=device) |
| 94 | + return q, k, v, state, A_log, a, dt_bias, b |
| 95 | + |
| 96 | + |
| 97 | +def _bench_one_shape( |
| 98 | + batch_size: int, |
| 99 | + num_q_heads: int, |
| 100 | + num_v_heads: int, |
| 101 | + head_size: int, |
| 102 | + dtype: torch.dtype, |
| 103 | + num_tests: int, |
| 104 | +) -> float: |
| 105 | + q, k, v, state, A_log, a, dt_bias, b = _make_inputs( |
| 106 | + batch_size=batch_size, |
| 107 | + num_q_heads=num_q_heads, |
| 108 | + num_v_heads=num_v_heads, |
| 109 | + head_size=head_size, |
| 110 | + dtype=dtype, |
| 111 | + ) |
| 112 | + |
| 113 | + def _runner(): |
| 114 | + mate.gated_delta_rule_decode( |
| 115 | + q=q, |
| 116 | + k=k, |
| 117 | + v=v, |
| 118 | + state=state, |
| 119 | + A_log=A_log, |
| 120 | + a=a, |
| 121 | + dt_bias=dt_bias, |
| 122 | + b=b, |
| 123 | + state_layout="VK", |
| 124 | + scale=None, |
| 125 | + use_qk_l2norm=True, |
| 126 | + ) |
| 127 | + |
| 128 | + seconds = bench_kineto( |
| 129 | + _runner, |
| 130 | + kernel_names=KERNEL_NAME, |
| 131 | + num_tests=num_tests, |
| 132 | + suppress_kineto_output=True, |
| 133 | + flush_l2=True, |
| 134 | + ) |
| 135 | + if seconds <= 0: |
| 136 | + raise RuntimeError(f"Failed to capture kernel time for {KERNEL_NAME}.") |
| 137 | + return float(seconds) |
| 138 | + |
| 139 | + |
| 140 | +def main(): |
| 141 | + parser = argparse.ArgumentParser(description="Benchmark MATE GDN decode kernel.") |
| 142 | + parser.add_argument("--dtype", choices=["fp16", "bf16"], default="bf16") |
| 143 | + parser.add_argument("--num-tests", type=int, default=10) |
| 144 | + parser.add_argument("--head-size", type=int, default=128) |
| 145 | + parser.add_argument( |
| 146 | + "--batch-sizes", |
| 147 | + type=int, |
| 148 | + nargs="+", |
| 149 | + default=list(DEFAULT_BATCH_SIZES), |
| 150 | + help="Batch sizes to benchmark. Default follows FlashInfer/SGLang decode sweeps.", |
| 151 | + ) |
| 152 | + parser.add_argument( |
| 153 | + "--head-configs", |
| 154 | + nargs="+", |
| 155 | + default=list(DEFAULT_HEAD_CONFIGS), |
| 156 | + help="Head configs in 'Hq,Hv' form. Default covers 8,16 / 16,32 / 16,64.", |
| 157 | + ) |
| 158 | + args = parser.parse_args() |
| 159 | + |
| 160 | + if not (hasattr(torch, "musa") and torch.musa.is_available()): |
| 161 | + raise RuntimeError("MUSA device is not available.") |
| 162 | + |
| 163 | + dtype = torch.float16 if args.dtype == "fp16" else torch.bfloat16 |
| 164 | + head_configs = [_parse_head_config(spec) for spec in args.head_configs] |
| 165 | + |
| 166 | + print(f"\nMUSA: {torch.musa.get_device_name(0)}") |
| 167 | + print(f"Kernel: {KERNEL_NAME}") |
| 168 | + print( |
| 169 | + f"Config: D={args.head_size}, dtype={args.dtype}, " |
| 170 | + f"head_configs={head_configs}, batches={tuple(args.batch_sizes)}" |
| 171 | + ) |
| 172 | + print() |
| 173 | + |
| 174 | + header = ( |
| 175 | + f"{'Hq':>4s} {'Hv':>4s} {'B':>4s} " |
| 176 | + f"{'Latency(us)':>12s} {'TFLOPS':>8s} {'GB/s':>8s}" |
| 177 | + ) |
| 178 | + print(header) |
| 179 | + print("-" * len(header)) |
| 180 | + |
| 181 | + for num_q_heads, num_v_heads in head_configs: |
| 182 | + for batch_size in args.batch_sizes: |
| 183 | + seconds = _bench_one_shape( |
| 184 | + batch_size=batch_size, |
| 185 | + num_q_heads=num_q_heads, |
| 186 | + num_v_heads=num_v_heads, |
| 187 | + head_size=args.head_size, |
| 188 | + dtype=dtype, |
| 189 | + num_tests=args.num_tests, |
| 190 | + ) |
| 191 | + flops = _gdn_decode_flops( |
| 192 | + batch_size=batch_size, |
| 193 | + num_q_heads=num_q_heads, |
| 194 | + num_v_heads=num_v_heads, |
| 195 | + head_size=args.head_size, |
| 196 | + ) |
| 197 | + io_bytes = _gdn_decode_bytes( |
| 198 | + batch_size=batch_size, |
| 199 | + num_q_heads=num_q_heads, |
| 200 | + num_v_heads=num_v_heads, |
| 201 | + head_size=args.head_size, |
| 202 | + input_dtype=dtype, |
| 203 | + output_dtype=dtype, |
| 204 | + ) |
| 205 | + latency_us = seconds * 1e6 |
| 206 | + tflops = flops / seconds / 1e12 |
| 207 | + bandwidth = io_bytes / seconds / 1e9 |
| 208 | + print( |
| 209 | + f"{num_q_heads:>4d} {num_v_heads:>4d} {batch_size:>4d} " |
| 210 | + f"{latency_us:>12.3f} {tflops:>8.3f} {bandwidth:>8.3f}" |
| 211 | + ) |
| 212 | + torch.musa.empty_cache() |
| 213 | + |
| 214 | + |
| 215 | +if __name__ == "__main__": |
| 216 | + main() |
0 commit comments