Dispatch overheadBenchmarks pages
Benchmarks
Dispatch overhead
The host cost of launching a kernel through each Enceladus path, next to the native dispatch floor and the launch paths of PyTorch and MLX.
What it measures
bench_dispatch launches a 1,024-element float32 vector add, which does almost no GPU work, so the time per launch is launch overhead. It measures three patterns with the host's wall clock:
- Sustained: 10,000 back-to-back launches followed by one synchronization, repeated 5 times. The result is the time per launch. The stream commits a command buffer every 64 dispatches.
- Sync round trip: 300 launches that each wait for the GPU before the next one starts.
- Lazy chain: on MLX arrays, a chain of 1,000 dependent launches followed by one
mx.eval, repeated 10 times. The result is the time per launch.
The script compares the following launch paths:
- A raw
enceladus.metal_kernelwith handwritten MSL. - The native
Stream.dispatchcall with prebuilt arguments, which is the floor for any Python launch. - An
@enceladus.jitkernel, which also binds arguments, computes the specialization key, and packs arguments. - The same
@enceladus.jitkernel on PyTorch MPS tensors and on MLX arrays. - The same
@enceladus.jitkernel on MLX arrays withenceladus.lazy_mlx(True), where each launch writes a fresh output fromenceladus.new_emptyand joins MLX's lazy graph instead of waiting. - PyTorch floors: a
torch.mps.compile_shaderkernel called directly, andtorch.add(out=). - MLX floor: a chain of 1,000
x + yadditions in MLX itself.
Before measuring, the script warms up the GPU clocks with 20,000 launches.
Results
The following table lists the minimum and median time per launch, in microseconds:
| Launch path | Pattern | Min (µs) | Median (µs) |
|---|---|---|---|
Native Stream.dispatch | Sustained | 1.02 | 1.05 |
Raw metal_kernel | Sustained | 1.15 | 1.18 |
@enceladus.jit on enceladus.Tensor | Sustained | 3.39 | 3.41 |
@enceladus.jit on PyTorch MPS | Sustained | 4.59 | 4.60 |
torch.mps.compile_shader call | Sustained | 2.31 | 2.34 |
torch.add(out=) | Sustained | 2.08 | 2.11 |
@enceladus.jit on MLX arrays, lazy | Lazy chain | 13.38 | 13.69 |
MLX x + y | Lazy chain | 1.69 | 1.75 |
Raw metal_kernel | Sync round trip | 82.92 | 99.81 |
@enceladus.jit on PyTorch MPS | Sync round trip | 87.71 | 125.02 |
@enceladus.jit on MLX arrays | Sync round trip | 126.08 | 160.96 |
Reading the results
- The GPU sets the floor. The native dispatch call and the raw
metal_kernellaunch both cost about 1 µs, which is the GPU's cost for a dependent dispatch. Python adds little on the raw path. @enceladus.jitadds about 2.4 µs. Binding, the specialization key, and argument packing bring a launch to 3.39 µs, which is still well under a synchronized round trip.- The PyTorch path is host-bound. A launch on MPS tensors costs 4.59 µs, against 2.31 µs for the same MSL through
compile_shadercalled directly, and 2.08 µs for a built-intorch.add. - Synchronization dominates small kernels. A round trip costs 83-126 µs by minimum time, about 25 to 37 times a sustained
@enceladus.jitlaunch. By default, launches on MLX arrays are synchronous, so each one pays this cost. - Lazy MLX launches avoid the round trip. With
enceladus.lazy_mlx(True), a chain of launches costs 13.38 µs per launch, about a tenth of a synchronous launch on MLX arrays. That's still about 8 times MLX's ownx + y, at 1.69 µs.
To keep launches asynchronous, allocate with enceladus.empty, zeros, or randn, and synchronize only when you need the result. On MLX arrays, call enceladus.lazy_mlx(True) and write each result to a fresh output from enceladus.new_empty. For more information, see Performance and Framework interop.
Raw output
The following output is the bench_dispatch section of the results file:
Device: Apple M4 Pro (applegpu_g16s), flush every 64 dispatches
raw metal_kernel, sustained min 1.15 µs median 1.18 µs
raw metal_kernel, sync round trip min 82.92 µs median 99.81 µs
native Stream.dispatch, sustained min 1.02 µs median 1.05 µs
@enceladus.jit launch, sustained min 3.39 µs median 3.41 µs
@enceladus.jit on torch MPS, sustained min 4.59 µs median 4.60 µs
@enceladus.jit on torch MPS, sync round trip min 87.71 µs median 125.02 µs
torch compile_shader call, sustained min 2.31 µs median 2.34 µs
torch.add(out=), sustained min 2.08 µs median 2.11 µs
@enceladus.jit on MLX arrays, sync round trip min 126.08 µs median 160.96 µs
@enceladus.jit on MLX arrays, lazy chain min 13.38 µs median 13.69 µs
MLX x + y chain min 1.69 µs median 1.75 µsSource
"""Measures launch overhead: sustained batched launches and synchronous round trips.
The PyTorch rows launch through `torch.mps.compile_shader` on PyTorch's MPS stream. The
MLX rows measure the synchronous MLX path, and lazy launches (`enceladus.lazy_mlx`)
against MLX's own ops. Rows for a framework that isn't installed are skipped.
Run with `uv run python benchmarks/bench_dispatch.py`.
"""
from __future__ import annotations
import statistics
import time
from typing import Any
import enceladus
import enceladus.language as tl
SRC = """
#include <metal_stdlib>
using namespace metal;
kernel void vadd(device const float* x [[buffer(0)]], device const float* y [[buffer(1)]],
device float* o [[buffer(2)]], constant uint& n [[buffer(3)]],
uint i [[thread_position_in_grid]]) {
if (i < n) o[i] = x[i] + y[i];
}
"""
def sustained(launch, n: int = 10_000, reps: int = 5, sync=enceladus.synchronize) -> list[float]:
"""Returns µs per launch for `reps` runs of `n` back-to-back launches.
`sync` waits for the stream that `launch` uses.
"""
out = []
for _ in range(reps):
sync()
t0 = time.perf_counter()
for _ in range(n):
launch()
sync()
out.append((time.perf_counter() - t0) / n * 1e6)
return out
def round_trip(launch, n: int = 300, sync=enceladus.synchronize) -> list[float]:
"""Returns µs for `n` launch-and-wait round trips."""
out = []
for _ in range(n):
t0 = time.perf_counter()
launch()
sync()
out.append((time.perf_counter() - t0) * 1e6)
return out
def report(name: str, samples: list[float]) -> None:
print(f"{name:<40} min {min(samples):8.2f} µs median {statistics.median(samples):8.2f} µs")
def main() -> None:
dev = enceladus.get_device()
print(f"Device: {dev.caps.name} ({dev.caps.architecture}), flush every "
f"{dev.stream.flush_every} dispatches")
k = enceladus.metal_kernel(SRC, "vadd")
n = 1024
x, y, o = enceladus.randn(n), enceladus.randn(n), enceladus.empty(n)
launcher = k[(n // 256,), (256,)]
def launch() -> None:
launcher(x, y, o, n)
# Warm up the GPU clocks: the GPU runs slowly for the first ~50 ms of work.
sustained(launch, n=20_000, reps=1)
report("raw metal_kernel, sustained", sustained(launch))
report("raw metal_kernel, sync round trip", round_trip(launch))
# Native floor: the stream's dispatch call with prebuilt arguments.
stream = dev.stream.native
plan = k._plan((True, True, True, False))
bufs, offs, scal = [x.buffer, y.buffer, o.buffer], [0, 0, 0], n.to_bytes(4, "little")
def native() -> None:
stream.dispatch(k.pipeline, plan, bufs, offs, scal, (n // 256, 1, 1), (256, 1, 1))
report("native Stream.dispatch, sustained", sustained(native))
# @enceladus.jit hit path: binding, specialization key, packing, and dispatch.
@enceladus.jit
def add_kernel(x_ptr, y_ptr, out_ptr, n, BLOCK: tl.constexpr):
offs = tl.program_id(0) * BLOCK + tl.arange(0, BLOCK)
mask = offs < n
tl.store(out_ptr + offs, tl.load(x_ptr + offs, mask=mask) + tl.load(y_ptr + offs,
mask=mask), mask=mask) # fmt: skip
def jit_launch() -> None:
add_kernel[(1,)](x, y, o, n, BLOCK=1024)
jit_launch()
report("@enceladus.jit launch, sustained", sustained(jit_launch))
bench_torch(add_kernel, n)
bench_mlx(add_kernel, n)
def bench_torch(add_kernel, n: int) -> None:
"""Measures launches through the PyTorch path against PyTorch's own floors."""
try:
import torch
except ImportError:
return
if not torch.backends.mps.is_available():
return
sync = torch.mps.synchronize
x, y, o = (torch.randn(n, device="mps") for _ in range(3))
def jit_launch() -> None:
add_kernel[(1,)](x, y, o, n, BLOCK=1024)
jit_launch()
sustained(jit_launch, n=20_000, reps=1, sync=sync) # warm up
report("@enceladus.jit on torch MPS, sustained", sustained(jit_launch, sync=sync))
report("@enceladus.jit on torch MPS, sync round trip", round_trip(jit_launch, sync=sync))
# Floors: the same compile_shader kernel called directly, and a built-in torch op.
lib = torch.mps.compile_shader(SRC)
def direct() -> None:
lib.vadd(x, y, o, n, threads=(n,), group_size=(256,), arg_casts={3: "int32"})
report("torch compile_shader call, sustained", sustained(direct, sync=sync))
report("torch.add(out=), sustained", sustained(lambda: torch.add(x, y, out=o), sync=sync))
def bench_mlx(add_kernel, n: int) -> None:
"""Measures the synchronous MLX path, and lazy launches against MLX's own ops."""
try:
import mlx.core as mx
except ImportError:
return
x, y, o = mx.random.normal((n,)), mx.random.normal((n,)), mx.zeros((n,))
mx.eval(x, y, o)
def jit_launch() -> None:
add_kernel[(1,)](x, y, o, n, BLOCK=1024)
jit_launch()
report("@enceladus.jit on MLX arrays, sync round trip", round_trip(jit_launch))
# Lazy launches: a chain of 1,000 dependent adds, each into a fresh output, then one
# evaluation. MLX's own `x + y` chain is the floor.
def lazy_chain() -> Any:
acc = y
for _ in range(1000):
out = enceladus.new_empty(x)
add_kernel[(1,)](x, acc, out, n, BLOCK=1024)
acc = out
return acc
def mlx_chain() -> Any:
acc = y
for _ in range(1000):
acc = x + acc
return acc
enceladus.lazy_mlx(True)
try:
for name, chain in [("@enceladus.jit on MLX arrays, lazy chain", lazy_chain),
("MLX x + y chain", mlx_chain)]: # fmt: skip
mx.eval(chain()) # warm up
samples = []
for _ in range(10):
t0 = time.perf_counter()
mx.eval(chain())
samples.append((time.perf_counter() - t0) / 1000 * 1e6)
report(name, samples)
finally:
enceladus.lazy_mlx(False)
if __name__ == "__main__":
main()