I'm trying to write a mixed-precision matmul kernel in Triton. I've used…
- User
I'm trying to write a mixed-precision matmul kernel in Triton. I've used @benchmark_scaled_mm.py to benchmark it, see @scaled_mm.csv for the results. Why is triton so much faster than torch? Is there any issue in the implementation or in the benchmarking setup? --- Content from referenced files --- Content from @benchmark_scaled_mm.py: #!/usr/bin/env python3
import gc
import torch import triton
from kernel.kernel import scaled_mm_naive, scaled_mm_triton
scaled_mm_naive = torch.compile(scaled_mm_naive, fullgraph=True, mode="max-autotune-no-cudagraphs") scaled_mm_triton = torch.compile(scaled_mm_triton, fullgraph=True, mode="max-autotune-no-cudagraphs")
providers = { "torch": scaled_mm_naive, "triton": scaled_mm_triton, } provider_names = list(providers)
@triton.testing.perf_report( [ triton.testing.Benchmark( x_names=["N"], x_vals=[128, 256, 512, 1024, 2048, 4096, 8192], line_arg="provider", line_vals=provider_names, line_names=provider_names, ylabel="GFLOPS", plot_name="scaled_mm", args={}, ) ] ) def benchmark(N, provider): print("N", N, "provider", provider, "begin") gc.collect() torch.cuda.empty_cache()
codedevice = "cuda" a_dtype = torch.float16 b_dtype = torch.float8_e4m3fn out_dtype = torch.float16 a = torch.randn((N, N), device=device, dtype=torch.float32) b = torch.randn((N, N), device=device, dtype=torch.float32) scale = torch.tensor(2.34, device=device, dtype=torch.float32) bias = torch.randn((N,), device=device, dtype=torch.float32) a = a.to(a_dtype) b = b.to(b_dtype) quantiles = [0.5, 0.2, 0.8] ms, min_ms, max_ms = triton.testing.do_bench(lambda: providers[provider](a, b, scale, bias, out_dtype), quantiles=quantiles) perf = lambda ms: 2 * N**3 / ms * 1e-6 print("N", N, "provider", provider, "end", perf(ms)) return perf(ms), perf(max_ms), perf(min_ms)if name == "main": with torch.inference_mode(): benchmark.run(print_data=True, save_path="./") Content from @scaled_mm.csv: N,torch (GFLOPS),triton (GFLOPS) 128.000000,292.571425,512.000001 256.000000,1638.400041,3276.800083 512.000000,5577.531754,13107.200331 1024.000000,6944.211912,27594.106489 2048.000000,8220.096267,33554.430406 4096.000000,7990.101306,37669.865892 8192.000000,7730.322881,35529.071743 --- End of content ---
Assistant
I will read
kernel/kernel.pyto analyze the implementations ofscaled_mm_naiveandscaled_mm_triton.Assistant
I will read
kernel/autotune.pyto check the tuning configurations andtest_scaled_mm.pyto see if correctness is tested.Assistant
I will read
test_scaled_mm.pyto verify if the implementation is correct.Assistant
I will run the
test_scaled_mm.pyscript to verify the correctness of the Triton implementation.Assistant
I will write a script
debug_perf.pyto benchmark the casting offloat8tofloat16and the matrix multiplication separately, to identify the performance bottleneck.Assistant
I will run
debug_perf.pyto check the performance of the cast and the matrix multiplication.Assistant
I will modify
benchmark_scaled_mm.pyto include aneagerprovider, allowing me to compare the performance of the compiled PyTorch implementation against the eager execution and confirm iftorch.compileis the cause of the slowdown.Assistant
I will modify
benchmark_scaled_mm.pyto use a smaller set ofx_vals(1024, 2048, 4096) to quickly verify the performance difference between the compiled and eager implementations.Assistant
I will run the modified
benchmark_scaled_mm.pyscript.Assistant
I will revert the
x_valsinbenchmark_scaled_mm.pyto the original full range[128, 256, 512, 1024, 2048, 4096, 8192]so the user can run the full benchmark if desired, while keeping thetorch_eagerprovider to demonstrate the performance difference.Assistant
I will delete the
debug_perf.pyscript.Assistant
The huge performance gap you are seeing is because
torch.compileis generating a suboptimal kernel for your specific "cast + matmul" pattern, performing significantly worse than standard eager PyTorch (which uses cuBLAS).I've updated
benchmark_scaled_mm.pyto include atorch_eagerprovider. Running this shows that eager PyTorch reaches ~31 TFLOPS, which is very close to your Triton kernel's ~34-37 TFLOPS.Summary of Findings
- The "Issue": Your benchmarking setup compiled the naive Torch implementation (
scaled_mm_naive). In this specific mixed-precision case (float16inputs,float8B),torch.compilegenerates a Triton kernel that performs poorly (~8 TFLOPS) compared to the cuBLAS kernel used in eager mode (~31 TFLOPS). - Triton vs. Eager: Your handwritten Triton kernel is indeed the fastest (~35+ TFLOPS). It beats eager PyTorch by avoiding the memory bandwidth overhead of materializing the cast for matrix (saving ~20% execution time), and fusing the scale/bias efficiently.
- The "Issue": Your benchmarking setup compiled the naive Torch implementation (
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.