-
-
Notifications
You must be signed in to change notification settings - Fork 202
perf: EXL3 performance tuning on GeForce Blackwell #1652
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
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b9a3930
perf: tune EXL3 GEMM selection for Blackwell
AlpinDale 3672282
perf: expand EXL3 Blackwell GEMM overrides
AlpinDale a878380
perf: tune EXL3 MoE mgemm on Blackwell
AlpinDale af26558
perf: scope EXL3 Blackwell tuning to MoE mgemm
AlpinDale 1b29391
perf: tune EXL3 dense mgemm on Blackwell
AlpinDale 32897de
perf: reduce EXL3 MoE decode overhead
AlpinDale 5b0b2d1
perf: fuse EXL3 MoE gate up projection
AlpinDale 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import argparse | ||
| from collections.abc import Callable | ||
|
|
||
| import torch | ||
| from safetensors import safe_open | ||
|
|
||
| from aphrodite import _custom_ops as ops | ||
|
|
||
|
|
||
| def _load_tensor(model: str, key: str, device: str) -> torch.Tensor: | ||
| with safe_open(f"{model}/model.safetensors", framework="pt", device="cpu") as f: | ||
| return f.get_tensor(key).to(device=device) | ||
|
|
||
|
|
||
| def _bench_ms(fn: Callable[[], None], warmup: int, iters: int) -> float: | ||
| for _ in range(warmup): | ||
| fn() | ||
| torch.cuda.synchronize() | ||
|
|
||
| start = torch.cuda.Event(enable_timing=True) | ||
| end = torch.cuda.Event(enable_timing=True) | ||
| start.record() | ||
| for _ in range(iters): | ||
| fn() | ||
| end.record() | ||
| torch.cuda.synchronize() | ||
| return start.elapsed_time(end) / iters | ||
|
|
||
|
|
||
| def _make_case(model: str, prefix: str, batch: int, device: str): | ||
| trellis = _load_tensor(model, f"{prefix}.trellis", device) | ||
| suh = _load_tensor(model, f"{prefix}.suh", device) | ||
| svh = _load_tensor(model, f"{prefix}.svh", device) | ||
|
|
||
| with safe_open(f"{model}/model.safetensors", framework="pt", device="cpu") as f: | ||
| keys = set(f.keys()) | ||
| mcg = f"{prefix}.mcg" in keys | ||
| mul1 = f"{prefix}.mul1" in keys | ||
|
|
||
| k = trellis.shape[0] * 16 | ||
| n = trellis.shape[1] * 16 | ||
| x = torch.randn((batch, k), device=device, dtype=torch.float16) | ||
| out = torch.empty((batch, n), device=device, dtype=torch.float16) | ||
| x_had = torch.empty_like(x) | ||
| return x, trellis, out, suh, x_had, svh, mcg, mul1, k, n | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser(description="Sweep EXL3 GEMM kernel shape/SM settings.") | ||
| parser.add_argument( | ||
| "-m", | ||
| "--model", | ||
| default="/home/alpindale/models/Trinity-Nano-Preview-exl3-4.0bpw", | ||
| ) | ||
| parser.add_argument( | ||
| "--prefix", | ||
| default="model.layers.10.mlp.experts.0.down_proj", | ||
| help="Safetensors prefix ending before .trellis/.suh/.svh", | ||
| ) | ||
| parser.add_argument("--batch", type=int, default=1) | ||
| parser.add_argument("--warmup", type=int, default=20) | ||
| parser.add_argument("--iters", type=int, default=100) | ||
| parser.add_argument("--sms", type=int, nargs="*", default=None) | ||
| args = parser.parse_args() | ||
|
|
||
| device = "cuda" | ||
| torch.set_grad_enabled(False) | ||
| device_sms = torch.cuda.get_device_properties(device).multi_processor_count | ||
| sms_values = args.sms or [0, 16, 24, 32, 40, 48, 56, 64, device_sms] | ||
| sms_values = sorted({sms for sms in sms_values if sms == 0 or sms <= device_sms}) | ||
| x, trellis, out, suh, x_had, svh, mcg, mul1, k, n = _make_case(args.model, args.prefix, args.batch, device) | ||
|
|
||
| print(f"model={args.model}") | ||
| print(f"prefix={args.prefix}") | ||
| print(f"batch={args.batch} k={k} n={n} bits={trellis.shape[2] // 16} mcg={mcg} mul1={mul1}") | ||
| print("shape sms ms tflops") | ||
|
|
||
| best: tuple[float, int, int] | None = None | ||
| candidates = [(-1, 0)] | ||
| candidates.extend((shape, sms) for shape in range(1, 5) for sms in sms_values if sms != 0) | ||
| for force_shape, force_sms in candidates: | ||
|
|
||
| def run(force_shape: int = force_shape, force_sms: int = force_sms) -> None: | ||
| ops.exl3_gemm( | ||
| x, | ||
| trellis, | ||
| out, | ||
| suh, | ||
| x_had, | ||
| svh, | ||
| force_shape, | ||
| mcg, | ||
| mul1, | ||
| force_sms, | ||
| ) | ||
|
|
||
| try: | ||
| ms = _bench_ms(run, args.warmup, args.iters) | ||
| except Exception as err: | ||
| print(f"{force_shape:5d} {force_sms:3d} ERROR {type(err).__name__}: {err}") | ||
| continue | ||
|
|
||
| tflops = (2 * args.batch * k * n) * 1e-12 / (ms * 1e-3) | ||
| print(f"{force_shape:5d} {force_sms:3d} {ms:8.4f} {tflops:8.3f}") | ||
| if best is None or ms < best[0]: | ||
| best = (ms, force_shape, force_sms) | ||
|
|
||
| if best is not None: | ||
| print(f"best shape={best[1]} sms={best[2]} ms={best[0]:.4f}") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the Blackwell override path (
K==4,size_m==1,size_k==1024,size_n==256), hard-codingshape_idx = 4selects a kernel withTILESIZE_N=512, so this shape has zero N tiles. Inselect_exl3_mgemm_kernel, that drivesmax_slicesto 0 and setsnum_smsto 0, andexl3_mgemm_grthen computestotal_sms / num_sms, which is a divide-by-zero on this decode shape rather than a pure performance tweak.Useful? React with 👍 / 👎.