Skip to content

Commit 3f58cc3

Browse files
committed
update release v0.2.2
1 parent 9ac89cf commit 3f58cc3

55 files changed

Lines changed: 4095 additions & 918 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

benchmarks/bench_flash_attn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def run_bench(
179179
cu_seqlens_k=cfg.cu_seqlens_kv,
180180
cu_seqlens_k_new=None,
181181
cache_leftpad=None,
182-
max_seqlen_k_new=None,
182+
max_seqlen_k_new=0,
183183
causal=cfg.is_causal,
184184
window_size=cfg.window_size,
185185
page_size=cfg.page_size,

benchmarks/bench_gdn_decode.py

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ def _gdn_decode_bytes(
4444
head_size: int,
4545
input_dtype: torch.dtype,
4646
output_dtype: torch.dtype,
47+
use_cache_indices: bool,
4748
) -> int:
4849
num_o_heads = max(num_q_heads, num_v_heads)
4950
elem_size = _dtype_size(input_dtype)
@@ -57,6 +58,7 @@ def _gdn_decode_bytes(
5758
dt_bias_bytes = num_v_heads * 4
5859
a_bytes = batch_size * num_v_heads * elem_size
5960
b_bytes = batch_size * num_v_heads * elem_size
61+
cache_indices_bytes = batch_size * 4 if use_cache_indices else 0
6062

6163
return (
6264
q_bytes
@@ -68,6 +70,7 @@ def _gdn_decode_bytes(
6870
+ dt_bias_bytes
6971
+ a_bytes
7072
+ b_bytes
73+
+ cache_indices_bytes
7174
)
7275

7376

@@ -77,6 +80,7 @@ def _make_inputs(
7780
num_v_heads: int,
7881
head_size: int,
7982
dtype: torch.dtype,
83+
use_cache_indices: bool,
8084
) -> tuple[torch.Tensor, ...]:
8185
device = torch.device("musa")
8286
q = torch.randn((batch_size, 1, num_q_heads, head_size), dtype=dtype, device=device)
@@ -91,7 +95,12 @@ def _make_inputs(
9195
dt_bias = torch.randn((num_v_heads,), dtype=torch.float32, device=device) * 0.1
9296
a = torch.randn((batch_size, 1, num_v_heads), dtype=dtype, device=device) * 0.1
9397
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
98+
state_indices = (
99+
torch.arange(batch_size, dtype=torch.int32, device=device)
100+
if use_cache_indices
101+
else None
102+
)
103+
return q, k, v, state, A_log, a, dt_bias, b, state_indices
95104

96105

97106
def _bench_one_shape(
@@ -101,13 +110,15 @@ def _bench_one_shape(
101110
head_size: int,
102111
dtype: torch.dtype,
103112
num_tests: int,
113+
use_cache_indices: bool,
104114
) -> float:
105-
q, k, v, state, A_log, a, dt_bias, b = _make_inputs(
115+
q, k, v, state, A_log, a, dt_bias, b, state_indices = _make_inputs(
106116
batch_size=batch_size,
107117
num_q_heads=num_q_heads,
108118
num_v_heads=num_v_heads,
109119
head_size=head_size,
110120
dtype=dtype,
121+
use_cache_indices=use_cache_indices,
111122
)
112123

113124
def _runner():
@@ -121,6 +132,7 @@ def _runner():
121132
dt_bias=dt_bias,
122133
b=b,
123134
state_layout="VK",
135+
state_indices=state_indices,
124136
scale=None,
125137
use_qk_l2norm=True,
126138
)
@@ -155,61 +167,76 @@ def main():
155167
default=list(DEFAULT_HEAD_CONFIGS),
156168
help="Head configs in 'Hq,Hv' form. Default covers 8,16 / 16,32 / 16,64.",
157169
)
170+
parser.add_argument(
171+
"--cache-indices",
172+
choices=["none", "identity", "both"],
173+
default="none",
174+
help="Benchmark without cache_indices, with identity cache_indices, or both.",
175+
)
158176
args = parser.parse_args()
159177

160178
if not (hasattr(torch, "musa") and torch.musa.is_available()):
161179
raise RuntimeError("MUSA device is not available.")
162180

163181
dtype = torch.float16 if args.dtype == "fp16" else torch.bfloat16
164182
head_configs = [_parse_head_config(spec) for spec in args.head_configs]
183+
cache_index_modes = (
184+
("none", "identity") if args.cache_indices == "both" else (args.cache_indices,)
185+
)
165186

166187
print(f"\nMUSA: {torch.musa.get_device_name(0)}")
167188
print(f"Kernel: {KERNEL_NAME}")
168189
print(
169190
f"Config: D={args.head_size}, dtype={args.dtype}, "
170-
f"head_configs={head_configs}, batches={tuple(args.batch_sizes)}"
191+
f"head_configs={head_configs}, batches={tuple(args.batch_sizes)}, "
192+
f"cache_indices={args.cache_indices}"
171193
)
172194
print()
173195

174196
header = (
175-
f"{'Hq':>4s} {'Hv':>4s} {'B':>4s} "
197+
f"{'Hq':>4s} {'Hv':>4s} {'B':>4s} {'cache':>8s} "
176198
f"{'Latency(us)':>12s} {'TFLOPS':>8s} {'GB/s':>8s}"
177199
)
178200
print(header)
179201
print("-" * len(header))
180202

181203
for num_q_heads, num_v_heads in head_configs:
182204
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()
205+
for cache_mode in cache_index_modes:
206+
use_cache_indices = cache_mode == "identity"
207+
seconds = _bench_one_shape(
208+
batch_size=batch_size,
209+
num_q_heads=num_q_heads,
210+
num_v_heads=num_v_heads,
211+
head_size=args.head_size,
212+
dtype=dtype,
213+
num_tests=args.num_tests,
214+
use_cache_indices=use_cache_indices,
215+
)
216+
flops = _gdn_decode_flops(
217+
batch_size=batch_size,
218+
num_q_heads=num_q_heads,
219+
num_v_heads=num_v_heads,
220+
head_size=args.head_size,
221+
)
222+
io_bytes = _gdn_decode_bytes(
223+
batch_size=batch_size,
224+
num_q_heads=num_q_heads,
225+
num_v_heads=num_v_heads,
226+
head_size=args.head_size,
227+
input_dtype=dtype,
228+
output_dtype=dtype,
229+
use_cache_indices=use_cache_indices,
230+
)
231+
latency_us = seconds * 1e6
232+
tflops = flops / seconds / 1e12
233+
bandwidth = io_bytes / seconds / 1e9
234+
print(
235+
f"{num_q_heads:>4d} {num_v_heads:>4d} {batch_size:>4d} "
236+
f"{cache_mode:>8s} {latency_us:>12.3f} "
237+
f"{tflops:>8.3f} {bandwidth:>8.3f}"
238+
)
239+
torch.musa.empty_cache()
213240

214241

215242
if __name__ == "__main__":

benchmarks/bench_gdn_mtp.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,25 @@ def _gdn_mtp_bytes(
4949
head_size: int,
5050
input_dtype: torch.dtype,
5151
output_dtype: torch.dtype,
52+
state_dtype: torch.dtype,
5253
*,
5354
disable_state_update: bool,
5455
cache_intermediate_states: bool,
5556
) -> int:
5657
num_o_heads = max(num_q_heads, num_v_heads)
5758
elem_size = _dtype_size(input_dtype)
59+
state_elem_size = _dtype_size(state_dtype)
5860

5961
q_bytes = batch_size * seq_len * num_q_heads * head_size * elem_size
6062
k_bytes = batch_size * seq_len * num_q_heads * head_size * elem_size
6163
v_bytes = batch_size * seq_len * num_v_heads * head_size * elem_size
6264
o_bytes = batch_size * seq_len * num_o_heads * head_size * _dtype_size(output_dtype)
63-
state_read_bytes = batch_size * num_v_heads * head_size * head_size * 4
65+
state_read_bytes = (
66+
batch_size * num_v_heads * head_size * head_size * state_elem_size
67+
)
6468
state_write_bytes = 0 if disable_state_update else state_read_bytes
6569
intermediate_bytes = (
66-
batch_size * seq_len * num_v_heads * head_size * head_size * 4
70+
batch_size * seq_len * num_v_heads * head_size * head_size * state_elem_size
6771
if cache_intermediate_states
6872
else 0
6973
)
@@ -94,6 +98,7 @@ def _make_inputs(
9498
num_v_heads: int,
9599
head_size: int,
96100
dtype: torch.dtype,
101+
state_dtype: torch.dtype,
97102
*,
98103
cache_intermediate_states: bool,
99104
) -> tuple[torch.Tensor, ...]:
@@ -109,7 +114,7 @@ def _make_inputs(
109114
)
110115
state = torch.randn(
111116
(batch_size, num_v_heads, head_size, head_size),
112-
dtype=torch.float32,
117+
dtype=state_dtype,
113118
device=device,
114119
)
115120
state_indices = torch.arange(batch_size, dtype=torch.int32, device=device)
@@ -129,7 +134,7 @@ def _make_inputs(
129134
intermediate = (
130135
torch.empty(
131136
(batch_size, seq_len, num_v_heads, head_size, head_size),
132-
dtype=torch.float32,
137+
dtype=state_dtype,
133138
device=device,
134139
)
135140
if cache_intermediate_states
@@ -145,6 +150,7 @@ def _make_runner(
145150
num_v_heads: int,
146151
head_size: int,
147152
dtype: torch.dtype,
153+
state_dtype: torch.dtype,
148154
*,
149155
cache_intermediate_states: bool,
150156
disable_state_update: bool,
@@ -159,6 +165,7 @@ def _make_runner(
159165
num_v_heads=num_v_heads,
160166
head_size=head_size,
161167
dtype=dtype,
168+
state_dtype=state_dtype,
162169
cache_intermediate_states=cache_intermediate_states,
163170
)
164171
)
@@ -175,7 +182,7 @@ def _make_runner(
175182
intermediate_arg = (
176183
intermediate
177184
if intermediate is not None
178-
else torch.empty((1, 1, 1, 1, 1), dtype=torch.float32, device=q.device)
185+
else torch.empty((1, 1, 1, 1, 1), dtype=state_dtype, device=q.device)
179186
)
180187
common_kwargs = dict(
181188
seq_len=seq_len,
@@ -186,6 +193,7 @@ def _make_runner(
186193
input_dtype=str(dtype).split(".")[-1],
187194
output_dtype=str(dtype).split(".")[-1],
188195
dt_bias_dtype=str(dt_bias.dtype).split(".")[-1],
196+
state_dtype=str(state_dtype).split(".")[-1],
189197
use_qk_l2norm=True,
190198
disable_state_update=disable_state_update,
191199
use_identity_state_indices=False,
@@ -224,6 +232,7 @@ def _bench_one_shape(
224232
num_v_heads: int,
225233
head_size: int,
226234
dtype: torch.dtype,
235+
state_dtype: torch.dtype,
227236
num_tests: int,
228237
*,
229238
cache_intermediate_states: bool,
@@ -238,6 +247,7 @@ def _bench_one_shape(
238247
num_v_heads=num_v_heads,
239248
head_size=head_size,
240249
dtype=dtype,
250+
state_dtype=state_dtype,
241251
cache_intermediate_states=cache_intermediate_states,
242252
disable_state_update=disable_state_update,
243253
tile_v_override=tile_v_override,
@@ -258,6 +268,12 @@ def _bench_one_shape(
258268
def main():
259269
parser = argparse.ArgumentParser(description="Benchmark MATE GDN MTP kernels.")
260270
parser.add_argument("--dtype", choices=["fp16", "bf16"], default="bf16")
271+
parser.add_argument(
272+
"--state-dtype",
273+
choices=["fp32", "bf16"],
274+
default="fp32",
275+
help="State and intermediate-state buffer dtype.",
276+
)
261277
parser.add_argument("--num-tests", type=int, default=10)
262278
parser.add_argument("--head-size", type=int, default=128)
263279
parser.add_argument(
@@ -311,6 +327,7 @@ def main():
311327
parser.error("--tile-v must be a positive integer.")
312328

313329
dtype = torch.float16 if args.dtype == "fp16" else torch.bfloat16
330+
state_dtype = torch.float32 if args.state_dtype == "fp32" else torch.bfloat16
314331
head_configs = [_parse_head_config(spec) for spec in args.head_configs]
315332
cache_intermediate_states = args.cache_intermediate_states
316333
disable_state_update = not args.update_state
@@ -319,6 +336,7 @@ def main():
319336
print(f"Kernel: {KERNEL_NAME}")
320337
print(
321338
f"Config: D={args.head_size}, dtype={args.dtype}, "
339+
f"state_dtype={args.state_dtype}, "
322340
f"head_configs={head_configs}, batches={tuple(args.batch_sizes)}, "
323341
f"seq_lens={tuple(args.seq_lens)}, cache_intermediate={cache_intermediate_states}, "
324342
f"update_state={not disable_state_update}, tile_v_override={args.tile_v}, "
@@ -343,6 +361,7 @@ def main():
343361
num_v_heads=num_v_heads,
344362
head_size=args.head_size,
345363
dtype=dtype,
364+
state_dtype=state_dtype,
346365
num_tests=args.num_tests,
347366
cache_intermediate_states=cache_intermediate_states,
348367
disable_state_update=disable_state_update,
@@ -364,6 +383,7 @@ def main():
364383
head_size=args.head_size,
365384
input_dtype=dtype,
366385
output_dtype=dtype,
386+
state_dtype=state_dtype,
367387
disable_state_update=disable_state_update,
368388
cache_intermediate_states=cache_intermediate_states,
369389
)

0 commit comments

Comments
 (0)