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

Benchmarks

Scans and atomics

Row cumulative-sum bandwidth against MLX, and the throughput of a histogram built with global atomics.

What it measures

bench_scan_atomics runs two example kernels:

  • Row cumsum from examples/10_cumsum.py, on a 4096 × 4096 float32 matrix, one row per program, with 4, 8, and 16 SIMD groups. It compares against mx.cumsum(x, axis=1). GB/s counts one read and one write of the matrix.
  • Histogram from examples/09_histogram.py, on 224 normally distributed float32 values, with 64 and 4,096 equal bins over [-4, 4). Each value adds one count to a shared histogram in device memory with tl.atomic_add. GB/s counts one read of the input.

Enceladus times are the minimum of 20 GPU-timed runs. The MLX time is the minimum of 20 wall-clock runs after 10 warmup runs, so it includes about 0.1 ms of launch and synchronization overhead.

Results

The following table lists the row cumsum by SIMD-group count:

num_warpsMin (ms)GB/s
40.556241.2
80.557241.0
160.554242.2
MLX cumsum189.5

The following table lists the histogram by bin count:

BinsMin (ms)GB/s
642.15931.1
40961.07462.5

Reading the results

  • The scan runs at copy bandwidth. The row cumsum reaches 241.0-242.2 GB/s, close to the row softmax on the same matrix, and the SIMD-group count changes it by under 1%.
  • The lead over MLX is mostly overhead. MLX's 189.5 GB/s corresponds to about 0.71 ms, against 0.55-0.56 ms for Enceladus. That 0.15 ms difference is only a little more than the wall-clock overhead of about 0.1 ms, so the data doesn't show that the Enceladus kernel is much faster.
  • Atomic contention limits the histogram. Every count goes to a counter in device memory, and there are no threadgroup-memory atomics to privatize the counters per threadgroup. With 64 bins, normally distributed values pile onto a few central counters, and the kernel reaches 31.1 GB/s. With 4,096 bins, the same values spread over more counters, and throughput doubles to 62.5 GB/s.

For the atomic and scan builtins, see the language reference.

Raw output

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

bench_scan_atomics outputFull report
Device: Apple M4 Pro
Row cumsum 4096 x 4096 fp32 (MLX: 189.5 GB/s)
  num_warps=4     0.556 ms   241.2 GB/s
  num_warps=8     0.557 ms   241.0 GB/s
  num_warps=16    0.554 ms   242.2 GB/s
Histogram of 16777216 fp32 values, 64 bins:   2.159 ms    31.1 GB/s
Histogram of 16777216 fp32 values, 4096 bins:   1.074 ms    62.5 GB/s

Source

benchmarks/bench_scan_atomics.pyGitHub
"""Row cumsum and histogram throughput, with MLX's cumsum for comparison.

GB/s counts one read and one write of the matrix for cumsum, and one read of the input
for the histogram. Enceladus uses GPU timestamps; MLX uses wall clock around a
synchronized call, so it includes about 0.1 ms of launch and sync overhead.

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

from __future__ import annotations

import importlib.util
import time
from pathlib import Path

import enceladus
from enceladus.testing import do_bench

M = N = 4096
EXAMPLES = Path(__file__).resolve().parents[1] / "examples"


def _example(stem: str):
    spec = importlib.util.spec_from_file_location(stem, 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 mlx_cumsum_ms() -> float | None:
    try:
        import mlx.core as mx
    except ImportError:
        return None
    x = mx.random.normal((M, N))
    mx.eval(x)
    for _ in range(10):
        mx.eval(mx.cumsum(x, axis=1))
    out = []
    for _ in range(20):
        t0 = time.perf_counter()
        mx.eval(mx.cumsum(x, axis=1))
        out.append((time.perf_counter() - t0) * 1e3)
    return min(out)


def main() -> None:
    print(f"Device: {enceladus.get_device().caps.name}")
    cs = _example("10_cumsum")
    x = enceladus.randn(M, N)
    out = enceladus.empty_like(x)
    ref = mlx_cumsum_ms()
    print(f"Row cumsum {M} x {N} fp32 (MLX: "
          f"{f'{gbps(2 * M * N * 4, ref):.1f} GB/s' if ref else 'n/a'})")  # fmt: skip
    for nw in (4, 8, 16):
        def run(nw=nw):
            cs.cumsum_kernel[(M,)](out, x, M, N, N, N, REVERSE=False, BLOCK_M=1, BLOCK_N=N,
                                   num_warps=nw)  # fmt: skip

        ms = do_bench(run, rep=20, return_mode="min")
        print(f"  num_warps={nw:<3} {ms:7.3f} ms {gbps(2 * M * N * 4, ms):7.1f} GB/s")

    hist = _example("09_histogram")
    n = 1 << 24
    data = enceladus.randn(n)
    for bins in (64, 4096):
        h = enceladus.zeros(bins, dtype="int32")

        def run(h=h, bins=bins):
            hist.histogram_kernel[(enceladus.cdiv(n, 1024),)](
                data, h, n, -4.0, bins / 8.0, NUM_BINS=bins, BLOCK=1024)

        ms = do_bench(run, rep=20, return_mode="min")
        print(f"Histogram of {n} fp32 values, {bins} bins: {ms:7.3f} ms "
              f"{gbps(n * 4, ms):7.1f} GB/s")  # fmt: skip


if __name__ == "__main__":
    main()