Enceladus v0.1.0 · alpha
ElementwiseBenchmarks pages
OverviewMethodologyElementwiseSoftmax and reductionsNormalizationScans and atomicsMatmulFlash attentionDispatch overhead

Benchmarks

Elementwise

Vector add bandwidth on large arrays against MLX, plus the time Enceladus takes to compile a kernel from Python to MSL.

What it measures

bench_elementwise runs the vector add from examples/01_vector_add.py, out = x + y, on 226 elements in float32 and float16, and on 228 elements in int8. That's 256 MB per array in float32 and int8, and 128 MB in float16, far larger than the GPU's caches. The script measures the following:

  • Enceladus with BLOCK=1024 and BLOCK=4096, in float32, float16, and int8, as the minimum of 20 GPU-timed runs.
  • MLX's x + y on the same shape and dtype, as the minimum of 20 wall-clock runs after 10 warmup runs. The script measures MLX again for each row.
  • The compile time of the vector add, from the Python frontend to MSL, as the median of 50 compilations. This excludes Metal's own compilation of the MSL.

GB/s counts three arrays of traffic: two reads and one write.

Results

The following table lists the minimum time and the bandwidth for each configuration:

dtypeBLOCKMin (ms)Enceladus GB/sMLX GB/s
float3210243.404236.6227.4
float3240963.451233.4228.3
float1610241.691238.1213.0
float1640961.697237.2215.8
int810243.407236.4214.6
int840963.479231.5214.4

The vector add compiles from the frontend to MSL in 0.46 ms.

Reading the results

  • All three dtypes reach the same bandwidth. Every configuration runs at 231.5-238.1 GB/s. float16 moves half the bytes in about half the time, and int8 adds four times as many elements as float32 in the same time, so the kernel is bound by memory bandwidth, not by instruction count.
  • Block size matters little. BLOCK=1024 is slightly faster than BLOCK=4096 in every dtype: 236.6 against 233.4 GB/s in float32, 238.1 against 237.2 GB/s in float16, and 236.4 against 231.5 GB/s in int8.
  • Enceladus leads MLX by 2-12%. In float32, the 2-4% lead is about the size of MLX's wall-clock overhead. In float16 and int8, Enceladus leads by 8-12%, and MLX takes 0.17-0.35 ms longer, more than the overhead of about 0.1 ms explains.
  • Compilation is cheap. At 0.46 ms, the Python-to-MSL step costs less than one launch of this kernel on 256 MB arrays.

For guidance on block sizes and SIMD groups for memory-bound kernels, see Performance.

Raw output

The following output is the bench_elementwise section of the results file:

bench_elementwise outputFull report
Device: Apple M4 Pro
kernel                               min ms     GB/s  MLX GB/s
add float32 BLOCK=1024                3.404    236.6     227.4
add float32 BLOCK=4096                3.451    233.4     228.3
add float16 BLOCK=1024                1.691    238.1     213.0
add float16 BLOCK=4096                1.697    237.2     215.8
add int8 BLOCK=1024                   3.407    236.4     214.6
add int8 BLOCK=4096                   3.479    231.5     214.4
Enceladus compile time (frontend to MSL), vector add: 0.46 ms

Source

benchmarks/bench_elementwise.pyGitHub
"""Vector add bandwidth at 256 MB per array, compared with MLX, plus Enceladus compile time.

Run with `uv run python benchmarks/bench_elementwise.py`.
"""

from __future__ import annotations

import importlib.util
import statistics
import time
from pathlib import Path

import numpy as np

import enceladus
from enceladus.compiler.frontend import build_ir
from enceladus.compiler.pipeline import compile_module
from enceladus.testing import do_bench

ROOT = Path(__file__).resolve().parents[1]


def load(stem: str):
    spec = importlib.util.spec_from_file_location(stem, ROOT / "examples" / f"{stem}.py")
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    return mod


def gbps(nbytes: int, ms: float) -> float:
    return nbytes / (ms * 1e-3) / 1e9


def bench_mlx(n: int, dtype: str) -> float | None:
    try:
        import mlx.core as mx
    except ImportError:
        return None
    x = mx.random.normal((n,)).astype(getattr(mx, dtype))
    y = mx.random.normal((n,)).astype(getattr(mx, dtype))
    mx.eval(x, y)
    for _ in range(10):
        mx.eval(x + y)
    samples = []
    for _ in range(20):
        t0 = time.perf_counter()
        mx.eval(x + y)
        samples.append((time.perf_counter() - t0) * 1e3)
    return min(samples)


def compile_time_ms(kernel, args, consts, reps: int = 50) -> float:
    spec = kernel.specialize(kernel.bind(args, consts))
    samples = []
    for _ in range(reps):
        t0 = time.perf_counter()
        m = build_ir(kernel, spec.arg_types, spec.arg_facts, spec.constexprs, 4, "relaxed")
        compile_module(m)
        samples.append((time.perf_counter() - t0) * 1e3)
    return statistics.median(samples)


def main() -> None:
    ex = load("01_vector_add")
    print(f"Device: {enceladus.get_device().caps.name}")
    print(f"{'kernel':<34}{'min ms':>9}{'GB/s':>9}{'MLX GB/s':>10}")
    # 256 MB per float32 and int8 array. int8 shows the effect of vector loads most.
    for dtype, n in (("float32", 1 << 26), ("float16", 1 << 26), ("int8", 1 << 28)):
        if dtype == "int8":
            x = enceladus.from_numpy(np.ones(n, np.int8))
            y = enceladus.from_numpy(np.ones(n, np.int8))
        else:
            x, y = enceladus.randn(n, dtype=dtype), enceladus.randn(n, dtype=dtype)
        out = enceladus.empty_like(x)
        nbytes = 3 * n * x.itemsize
        for block in (1024, 4096):
            grid = (enceladus.cdiv(n, block),)

            def run(grid=grid, x=x, y=y, out=out, block=block):
                ex.add_kernel[grid](x, y, out, n, BLOCK=block)

            ms = do_bench(run, rep=20, return_mode="min")
            mlx_ms = bench_mlx(n, dtype)
            mlx = f"{gbps(nbytes, mlx_ms):10.1f}" if mlx_ms else f"{'n/a':>10}"
            print(f"{f'add {dtype} BLOCK={block}':<34}{ms:9.3f}{gbps(nbytes, ms):9.1f}{mlx}")
        del x, y, out
    x = enceladus.randn(1024)
    t = compile_time_ms(ex.add_kernel, (x, x, x, 1024), {"BLOCK": 1024})
    print(f"Enceladus compile time (frontend to MSL), vector add: {t:.2f} ms")


if __name__ == "__main__":
    main()