-
Notifications
You must be signed in to change notification settings - Fork 552
tilelang: T.fp8_scaled_matmul DSL intrinsic + Metal lowering #2142
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
Open
apstenku123
wants to merge
11
commits into
tile-ai:main
Choose a base branch
from
apstenku123:cppmega/fp8-scaled-matmul-intrinsic
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ee01743
[Metal] Add Metal GEMM support with simdgroup_matrix MMA
oraluben 01dff39
fix(metal): support scalar local.var lowering
jorgecurious 1ab387c
fix(jit): select mps when cuda is unavailable
jorgecurious d1ccdc4
test(metal): add internal runtime coverage probes
jorgecurious 911a3a2
docs(metal): document internal runtime coverage
jorgecurious 3ee8b7f
test(metal): tolerate split local.var initialization
jorgecurious 79158bd
style(metal): apply pre-commit formatting
jorgecurious d4fb922
fix(metal): harden simdgroup review paths
jorgecurious 7f948ec
fix(metal): address swarm eval review followups
jorgecurious 971c17b
fix(metal): harden simdgroup store lowering
jorgecurious 1984db9
tilelang: T.fp8_scaled_matmul DSL intrinsic + Metal lowering
apstenku123 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,126 @@ | ||
| import argparse | ||
| import logging | ||
| import time | ||
|
|
||
| import torch | ||
|
|
||
| import tilelang | ||
| import tilelang.language as T | ||
|
|
||
| logging.getLogger("tilelang").setLevel(logging.WARNING) | ||
|
|
||
| BLOCK_CONFIGS = [ | ||
| (16, 16, 16), | ||
| (32, 32, 16), | ||
| (32, 32, 32), | ||
| (64, 64, 32), | ||
| ] | ||
|
|
||
|
|
||
| @tilelang.jit | ||
| def matmul_simdgroup(M, N, K, block_M=64, block_N=64, block_K=32, dtype=T.float16, accum_dtype=T.float32): | ||
|
|
||
| @T.prim_func | ||
| def gemm_kernel( | ||
| A: T.Tensor((M, K), dtype), | ||
| B: T.Tensor((K, N), dtype), | ||
| C: T.Tensor((M, N), accum_dtype), | ||
| ): | ||
| with T.Kernel(T.ceildiv(N, block_N), T.ceildiv(M, block_M), threads=128) as (bx, by): | ||
| A_shared = T.alloc_shared((block_M, block_K), dtype, scope="shared") | ||
| B_shared = T.alloc_shared((block_K, block_N), dtype, scope="shared") | ||
| C_local = T.alloc_fragment((block_M, block_N), accum_dtype) | ||
| T.clear(C_local) | ||
| for ko in T.Pipelined(T.ceildiv(K, block_K), num_stages=0): | ||
| T.copy(A[by * block_M, ko * block_K], A_shared) | ||
| T.copy(B[ko * block_K, bx * block_N], B_shared) | ||
| T.gemm(A_shared, B_shared, C_local) | ||
| T.copy(C_local, C[by * block_M, bx * block_N]) | ||
|
|
||
| return gemm_kernel | ||
|
|
||
|
|
||
| def _tflops(M, N, K, seconds): | ||
| return 2.0 * M * N * K / seconds / 1e12 | ||
|
|
||
|
|
||
| def _bench(fn, warmup, repeats): | ||
| for _ in range(warmup): | ||
| fn() | ||
| torch.mps.synchronize() | ||
| t0 = time.perf_counter() | ||
| for _ in range(repeats): | ||
| fn() | ||
| torch.mps.synchronize() | ||
| return (time.perf_counter() - t0) / repeats | ||
|
|
||
|
|
||
| def bench_torch_mps(M, N, K, warmup, repeats): | ||
| a = torch.randn(M, K, dtype=torch.float16, device="mps") | ||
| b = torch.randn(K, N, dtype=torch.float16, device="mps") | ||
| avg_s = _bench(lambda: torch.mm(a, b), warmup, repeats) | ||
| return _tflops(M, N, K, avg_s) | ||
|
|
||
|
|
||
| def bench_tilelang(M, N, K, block_M, block_N, block_K, warmup, repeats): | ||
| kernel = matmul_simdgroup(M, N, K, block_M, block_N, block_K) | ||
| a = torch.randn(M, K, dtype=torch.float16, device="mps") | ||
| b = torch.randn(K, N, dtype=torch.float16, device="mps") | ||
| c = torch.zeros(M, N, dtype=torch.float32, device="mps") | ||
| avg_s = _bench(lambda: kernel(a, b, c), warmup, repeats) | ||
| return _tflops(M, N, K, avg_s) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser(description="Metal GEMM Benchmark (simdgroup)") | ||
| parser.add_argument("--m", type=int, default=4096) | ||
| parser.add_argument("--n", type=int, default=4096) | ||
| parser.add_argument("--k", type=int, default=4096) | ||
| parser.add_argument("--warmup", type=int, default=10) | ||
| parser.add_argument("--repeats", type=int, default=100) | ||
| parser.add_argument("--sweep", action="store_true", help="Sweep all block configs instead of using default (64,64,32)") | ||
| args = parser.parse_args() | ||
|
|
||
| M, N, K = args.m, args.n, args.k | ||
| for name, value in (("m", M), ("n", N), ("k", K), ("repeats", args.repeats)): | ||
| if value <= 0: | ||
| raise SystemExit(f"--{name} must be a positive integer, got {value}") | ||
| if args.warmup < 0: | ||
| raise SystemExit(f"--warmup must be non-negative, got {args.warmup}") | ||
|
|
||
| print(f"torch: {torch.__version__}") | ||
| print(f"tilelang: {tilelang.__version__}") | ||
| print(f"MPS: {torch.backends.mps.is_available()}") | ||
| if not torch.backends.mps.is_available(): | ||
| raise SystemExit("Metal GEMM benchmark requires PyTorch MPS support") | ||
| print(f"M={M}, N={N}, K={K}, warmup={args.warmup}, repeats={args.repeats}") | ||
| print() | ||
|
|
||
| ref_tflops = bench_torch_mps(M, N, K, args.warmup, args.repeats) | ||
| print(f"PyTorch MPS (torch.mm fp16): {ref_tflops:.1f} TFLOPS") | ||
| print() | ||
|
|
||
| configs = BLOCK_CONFIGS if args.sweep else [(64, 64, 32)] | ||
|
|
||
| print(f"{'block (M,N,K)':>16s} | {'TileLang':>14s} | {'Ratio':>6s}") | ||
| print("-" * 44) | ||
|
|
||
| best_tflops = 0.0 | ||
| best_config = configs[0] | ||
| for bM, bN, bK in configs: | ||
| try: | ||
| tl = bench_tilelang(M, N, K, bM, bN, bK, args.warmup, args.repeats) | ||
| ratio = tl / ref_tflops * 100 | ||
| tag = "" | ||
| if tl > best_tflops: | ||
| best_tflops = tl | ||
| best_config = (bM, bN, bK) | ||
| print(f"{f'({bM},{bN},{bK})':>16s} | {tl:>10.1f} TFLOPS | {ratio:>5.0f}%") | ||
| except Exception as e: | ||
| print(f"{f'({bM},{bN},{bK})':>16s} | {'FAILED':>14s} | {e}") | ||
|
|
||
| if args.sweep: | ||
| print() | ||
| print(f"Best config: {best_config}") | ||
| print(f"Best TFlops: {best_tflops:.1f}") | ||
| print(f"Reference TFlops (PyTorch MPS): {ref_tflops:.1f}") | ||
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
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
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.
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.
Benchmark both paths with the same output-allocation policy.
bench_torch_mpsmeasurestorch.mm(a, b)with a fresh output tensor every iteration, butbench_tilelangreusesc. That bakes allocator cost into the reference only, so the printed TileLang/PyTorch ratio is artificially optimistic. Preallocate the reference output too, or include allocation cost on both sides.🤖 Prompt for AI Agents