Kernels documentation
kernels benchmark
kernels benchmark
Use kernels benchmark to run benchmark scripts shipped with a kernel
repository.
The command:
- Downloads the kernel repo at a specific branch or version
- Runs all
benchmarks/benchmark*.pyscripts - Times each
benchmark_*workload and prints a results table - Optionally saves results as JSON Lines records
Installation
kernels benchmark requires extra dependencies:
uv pip install 'kernels[benchmark]' # or pip install 'kernels[benchmark]'Example
kernels benchmark kernels-community/activation --version 1
Example output:
Downloading kernels-community/activation@v1... Running benchmark.py... GPU NVIDIA GeForce RTX 4090 CPU x86_64 OS Linux 6.12.63 PyTorch 2.14.0+cu130 Running SiluWorkloads on cuda Running SiluWorkloadsGraph on cuda ββββββββββββββββββββββ¬βββββββββββββββββββββ¬ββββββ¬ββββββββββββ¬βββββββββββββ¬ββββββββββββ¬ββββββββββ β Benchmark β Workload β N β Speedup β Wall(ms) β Ref(ms) β Match β ββββββββββββββββββββββΌβββββββββββββββββββββΌββββββΌββββββββββββΌβββββββββββββΌββββββββββββΌββββββββββ€ β SiluWorkloads β qwen3_0_6b_decode β 100 β 2.05x β 0.006 β 0.0123 β β β β SiluWorkloads β qwen3_0_6b_prefill β 100 β 1.50x β 0.0207 β 0.0311 β β β β SiluWorkloads β qwen3_32b_decode β 100 β 1.09x β 0.0113 β 0.0123 β β β β SiluWorkloads β qwen3_32b_prefill β 100 β 1.69x β 0.3476 β 0.5864 β β β β SiluWorkloads β qwen3_8b_decode β 100 β 1.52x β 0.0078 β 0.0119 β β β β SiluWorkloads β qwen3_8b_prefill β 100 β 1.70x β 0.1706 β 0.2902 β β β β SiluWorkloadsGraph β qwen3_0_6b_decode β 100 β 1.32x β 0.0017 β 0.0023 β β β β SiluWorkloadsGraph β qwen3_0_6b_prefill β 100 β 1.44x β 0.0152 β 0.0218 β β β β SiluWorkloadsGraph β qwen3_32b_decode β 100 β 0.35x β 0.0065 β 0.0023 β β β β SiluWorkloadsGraph β qwen3_32b_prefill β 100 β 1.67x β 0.341 β 0.5685 β β β β SiluWorkloadsGraph β qwen3_8b_decode β 100 β 0.62x β 0.0037 β 0.0023 β β β β SiluWorkloadsGraph β qwen3_8b_prefill β 100 β 1.70x β 0.165 β 0.2813 β β β ββββββββββββββββββββββ΄βββββββββββββββββββββ΄ββββββ΄ββββββββββββ΄βββββββββββββ΄ββββββββββββ΄ββββββββββ Kernel: cedce2e Benchmark: dc8eb09
Times are medians of the wall time per call, and Speedup is the reference
median divided by the kernel median. This benchmark script defines its benchmark
twice, the second time with cuda_graph = True, so that each workload is timed
both eagerly and in a CUDA graph (see Writing Benchmark Scripts).
Pass --show-stats to also show the mean, standard deviation, min, max, IQR and
outlier count of each workload, a 95% confidence interval for the mean, and the
device time (see below).
Usage
You must specify which revision to benchmark, either via flags or with @... in
the repo id:
kernels benchmark <repo_id> --version <N> kernels benchmark <repo_id> --branch <name> kernels benchmark <repo_id>@v<N> kernels benchmark <repo_id>@<branch>
Examples
Benchmark a tagged kernel version:
kernels benchmark kernels-community/activation --version 1
Equivalent shorthand:
kernels benchmark kernels-community/activation@v1
Benchmark a branch:
kernels benchmark kernels-community/activation --branch main
Tune warmup and iteration count:
kernels benchmark kernels-community/activation@v1 --warmup 20 --iterations 200
Save results to a file:
kernels benchmark kernels-community/activation@v1 --output results.jsonl
Benchmark a local kernel checkout (must contain benchmarks/):
kernels benchmark ./my_kernel
Output
- By default, a table is printed (timings in ms).
--output <file>.jsonlwrites one record per workload as JSON Lines.--jsonprints the same records to stdout.
Result records
Each JSON Lines record is a flat row that describes one workload of one run:
what was benchmarked (repo_id, revision, kernel_sha, benchmark_sha, benchmark, workload, params), where (gpu, backend, cpu, os, torch_version, kernels_version, driver_version, gpu_max_clock_mhz, gpu_power_limit_w), how (iterations, warmup, timing_mode), the GPU state
during the workload (gpu_clock_mhz, gpu_mem_clock_mhz, gpu_power_draw_w, gpu_temperature_c, gpu_throttled, hardware), and the results (verified, wall_speedup, device_speedup and the timing statistics). Records from one
run share a run_id.
Timing statistics are named [ref_]<clock>_<stat>. The ref_ prefix marks the verify_* reference, the clock is wall or device, and every combination has
the same statistics: mean_ms, median_ms, std_ms, min_ms, max_ms, q1_ms, q3_ms, iqr_ms, outliers, and the raw samples_ms. For example wall_median_ms is the kernelβs median wall time and ref_device_median_ms the
referenceβs median device time. The device fields are null on devices
without timing events.
params (set by the benchmark) and hardware (vendor-specific, with a vendor key) are JSON objects whose keys differ between benchmarks and vendors. Every
other field is present in every record, null when unknown.
Because every record is self-contained, files from many runs can be concatenated and loaded as a single table, for example with pandas:
import glob
import pandas as pd
df = pd.concat(pd.read_json(f, lines=True) for f in glob.glob("results/*.jsonl"))GPU conditions
Clocks, power limits and throttling affect timings, but kernels benchmark does
not change them. They are recorded instead, so that they can be taken into
account when comparing runs. They are collected with nvidia-smi on NVIDIA GPUs
and amd-smi on AMD GPUs, and are null on other devices. To get more stable
results, lock them yourself before benchmarking, e.g.:
sudo nvidia-smi --lock-gpu-clocks=2520,2520
sudo nvidia-smi --power-limit=400Comparing runs
Save a run as JSON Lines and pass it as a baseline to a later run, for example to check whether a kernel or builder change affected performance:
kernels benchmark ./my_kernel --output base.jsonl
# ... make changes and rebuild ...
kernels benchmark ./my_kernel --baseline base.jsonlThis compares the kernel against an earlier run of itself. Speedup in the
results table is different: it compares the kernel against its verify_* reference in the same run.
Two saved runs can also be compared directly:
kernels benchmark-compare base.jsonl new.jsonl
Workloads are compared by their median time. A workload is reported as faster or
slower only when its median changed by more than --threshold (default 0.05,
i.e. 5%) and the interquartile ranges of the two runs do not overlap. A
warning is printed when the runs were made on a different GPU, backend, PyTorch
version, driver version, or power limit. A note is printed for workloads whose params or timing mode differ, and for changed workloads whose sampled clock or
power draw differ.
Use --fail-on-regression to exit with a non-zero status when any workload got
slower, which is useful in CI.
Writing Benchmark Scripts
Benchmark scripts must live under benchmarks/ in the kernel repository and
match benchmark*.py. Each script should define one or more subclasses of kernels.benchmark.Benchmark.
Minimal example (benchmarks/benchmark_activation.py):
import torch
from kernels.benchmark import Benchmark
class ActivationBenchmark(Benchmark):
seed = 0
def setup(self):
self.x = torch.randn(128, 1024, device=self.device, dtype=torch.float16)
self.out = torch.empty(128, 512, device=self.device, dtype=torch.float16)
self.params = {"shape": list(self.x.shape), "dtype": self.x.dtype} # optional
def benchmark_silu_and_mul(self):
self.kernel.silu_and_mul(self.out, self.x)
def verify_silu_and_mul(self):
# Return reference tensor; runner compares with self.out
return torch.nn.functional.silu(self.x[..., :512]) * self.x[..., 512:]The runner will:
- Set
self.deviceto the device of the active backend (cudaon CUDA and ROCm,xpu,mpson Metal,npuon CANN), so scripts do not pick it themselves - Call
setup()once per workload (orsetup_<workload>()if present) - Warm up (
--warmup) - Time
benchmark_<workload>()for--iterations - If
verify_<workload>()exists, check that outputs match (torch.allclose(..., atol=1e-2)) and show a speedup vs the reference computation (reference median / kernel median), timed with the same loop as the kernel - Store
self.params, if set, as the workloadβsparams
Each iteration is timed with the host clock and a synchronize, so a sample is
the cost of one call, including its launch. This is the wall time.
On CUDA, ROCm and XPU, each workload and its reference are then timed again in a
separate pass with device events recorded before and after each call. This is
the device time: it leaves out the launch, but includes the time the device
waits between the kernels of a call. When the host cannot launch kernels as fast
as they run, as with eager calls of small kernels, the device waits for the host
and the device time is not much lower than the wall time. The events have a
resolution of about a microsecond, so the device IQR of small kernels is
often 0.
Set cuda_graph = True on a benchmark class to capture several calls of each
workload and of its reference in a CUDA graph and time its replays, which is how
a kernel runs in a model captured with CUDA graphs: the launch overhead is
removed and the replay overhead is spread over the calls. Replays are timed the
same way, and each sample is divided by the number of calls. To time a benchmark
both ways, subclass it:
class SiluWorkloadsGraph(SiluWorkloads):
cuda_graph = TrueCUDA graphs are supported on CUDA and ROCm. Other devices, and workloads that
fail to capture, fall back to eager timing. The mode used is recorded as timing_mode.
Troubleshooting
- If the repo does not contain a
benchmarks/directory (or nobenchmark*.pyfiles), the command exits with an error. - If a benchmark script defines no
Benchmarksubclasses, the command exits with an error. - If
verify_<workload>()exists and the outputs do not match, the command exits with an error.