-
Notifications
You must be signed in to change notification settings - Fork 129
Add Metal attention benchmark tool #178
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
WindChimeRan
merged 2 commits into
vllm-project:main
from
Kingwl:feat/add-benchmark-script
Mar 20, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # Tools | ||
|
|
||
| ## Attention Benchmark | ||
|
|
||
| The repository includes a local benchmark utility for comparing Metal attention backends: | ||
|
|
||
| ```bash | ||
| source .venv-vllm-metal/bin/activate | ||
| python -m tools.benchmark.attention_benchmark | ||
| ``` | ||
|
|
||
| Running with no arguments executes the built-in `all` preset group and prints one combined text table to stdout. | ||
| By default, presets run `v1`, `v2`, `textbook`, and `sdpa`. Use `--backend all` when you also want `sdpa-compute-only`. | ||
| `num_layers` is supported as a shared benchmark setting; multi-layer runs repeat the same workload across layers and report per-layer latency. | ||
|
|
||
| Built-in groups: | ||
| - `all`: every built-in case | ||
| - `decode`: all decode cases | ||
| - `varlen`: all varlen cases | ||
| - `small`: `decode-small` + `varlen-light` | ||
| - `typical`: `decode-typical` + `varlen-typical` | ||
| - `long`: `decode-big-head` + `decode-long` + `varlen-single-long` + `varlen-ragged-longtail` | ||
|
|
||
| Built-in cases: | ||
| - `decode-small` | ||
| - `decode-typical` | ||
| - `decode-big-head` | ||
| - `decode-long` | ||
| - `varlen-light` | ||
| - `varlen-typical` | ||
| - `varlen-single-long` | ||
| - `varlen-ragged-longtail` | ||
|
|
||
| Useful examples: | ||
|
|
||
| ```bash | ||
| # Run the default all group | ||
| python -m tools.benchmark.attention_benchmark | ||
|
|
||
| # Run a built-in group | ||
| python -m tools.benchmark.attention_benchmark --group decode | ||
| python -m tools.benchmark.attention_benchmark --group varlen | ||
| python -m tools.benchmark.attention_benchmark --group typical | ||
| python -m tools.benchmark.attention_benchmark --group long | ||
|
|
||
| # Run explicit cases | ||
| python -m tools.benchmark.attention_benchmark --cases decode-small,varlen-light | ||
|
|
||
| # Include sdpa-compute-only in addition to the default backends | ||
| python -m tools.benchmark.attention_benchmark --group all --backend all | ||
|
|
||
| # Write structured exports in addition to the stdout table | ||
| python -m tools.benchmark.attention_benchmark --group decode --output-json /tmp/attention.json | ||
| python -m tools.benchmark.attention_benchmark --group decode --output-csv /tmp/attention.csv | ||
|
|
||
| # Override shared benchmark settings on a built-in preset run | ||
| python -m tools.benchmark.attention_benchmark --group decode --num-layers 10 --iters 200 | ||
|
|
||
| # Define a manual workload | ||
| python -m tools.benchmark.attention_benchmark --mode decode --batch-size 8 --kv-lens 2048 | ||
|
|
||
| # Define a manual varlen workload | ||
| python -m tools.benchmark.attention_benchmark --mode varlen --q-lens 1,4,16,64 --kv-lens 128,256,512,1024 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| """Shared helpers for attention correctness tests and benchmarks.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import mlx.core as mx | ||
| import numpy as np | ||
|
|
||
|
|
||
| def ref_paged_attn( | ||
| query: mx.array, | ||
| key_cache: mx.array, | ||
| value_cache: mx.array, | ||
| query_lens: list[int], | ||
| kv_lens: list[int], | ||
| block_tables: np.ndarray, | ||
| scale: float, | ||
| sliding_window: int | None = None, | ||
| soft_cap: float | None = None, | ||
| ) -> mx.array: | ||
| """Pure-MLX reference: gather K/V from paged cache, compute attention.""" | ||
| _, block_size, num_kv_heads, head_size = key_cache.shape | ||
|
|
||
| outputs: list[mx.array] = [] | ||
| start_idx = 0 | ||
| for i, query_len in enumerate(query_lens): | ||
| kv_len = kv_lens[i] | ||
| q = query[start_idx : start_idx + query_len] * scale | ||
|
|
||
| num_kv_blocks = (kv_len + block_size - 1) // block_size | ||
| block_indices = mx.array(block_tables[i, :num_kv_blocks]) | ||
|
|
||
| k = key_cache[block_indices].reshape(-1, num_kv_heads, head_size)[:kv_len] | ||
| v = value_cache[block_indices].reshape(-1, num_kv_heads, head_size)[:kv_len] | ||
|
|
||
| if q.shape[1] != k.shape[1]: | ||
| n_rep = q.shape[1] // k.shape[1] | ||
| k = mx.repeat(k, n_rep, axis=1) | ||
| v = mx.repeat(v, n_rep, axis=1) | ||
|
|
||
| attn = mx.einsum("qhd,khd->hqk", q, k).astype(mx.float32) | ||
|
|
||
| empty_mask = mx.ones((query_len, kv_len)) | ||
| mask = mx.triu(empty_mask, k=kv_len - query_len + 1).astype(mx.bool_) | ||
|
|
||
| if sliding_window is not None: | ||
| sliding_window_mask = mx.logical_not( | ||
| mx.triu(empty_mask, k=kv_len - (query_len + sliding_window) + 1).astype( | ||
| mx.bool_ | ||
| ) | ||
| ) | ||
| mask = mx.logical_or(mask, sliding_window_mask) | ||
|
|
||
| if soft_cap is not None and soft_cap > 0: | ||
| attn = soft_cap * mx.tanh(attn / soft_cap) | ||
|
|
||
| attn = mx.where(mask, float("-inf"), attn) | ||
| attn = mx.softmax(attn, axis=-1).astype(v.dtype) | ||
| outputs.append(mx.einsum("hqk,khd->qhd", attn, v)) | ||
| start_idx += query_len | ||
|
|
||
| return mx.concatenate(outputs, axis=0) | ||
|
|
||
|
|
||
| def run_v1_paged_attention( | ||
| query: mx.array, | ||
| key_cache: mx.array, | ||
| value_cache: mx.array, | ||
| num_kv_heads: int, | ||
| scale: float, | ||
| block_tables: mx.array, | ||
| seq_lens: mx.array, | ||
| block_size: int, | ||
| max_seq_len: int, | ||
| ) -> mx.array: | ||
| """Run kernel_v1 paged attention.""" | ||
| from vllm_metal.metal import get_ops | ||
|
|
||
| ops = get_ops() | ||
| out = mx.zeros_like(query) | ||
| mx.eval(out, query, key_cache, value_cache, block_tables, seq_lens) | ||
| ops.paged_attention_v1( | ||
| out, | ||
| query, | ||
| key_cache, | ||
| value_cache, | ||
| num_kv_heads, | ||
| scale, | ||
| block_tables, | ||
| seq_lens, | ||
| block_size, | ||
| max_seq_len, | ||
| ) | ||
| mx.synchronize() | ||
| return out | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.