Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

4.9 KB
Raw Blame

Kernel Benchmarks

Criterion-based microbenchmarks for Metal kernels. Runs on macOS (native) and on iPhone (via cargo-dinghy). Results from both are consolidated under a single target/criterion/<label>/ tree so you can compare baselines side by side.

Prerequisites

  • Rust nightly: rustup toolchain install nightly
  • For iOS runs:
    • Apple target: rustup target add aarch64-apple-ios

    • cargo-dinghy built from the trymirai/dinghy:

      cargo install \
        --git https://github.com/trymirai/dinghy \
        --branch mirai \
        cargo-dinghy
      
    • A physical device connected via USB with a device ID from xcrun devicectl list devices.

Available benchmark groups

Benchmarks live inside the uzu-engine library test target (registered with #[uzu_bench] under crates/uzu-engine/unit/), so the Cargo target is --lib.

Group id Filter
Metal/Kernel/Matmul/GEMM Metal/Kernel/Matmul/GEMM
Metal/Kernel/Matmul/GEMM_MXU Metal/Kernel/Matmul/GEMM_MXU
Metal/Kernel/A8W/w4, .../w8 Metal/Kernel/A8W
Metal/Kernel/UnifiedQuantizedGemm/... Metal/Kernel/UnifiedQuantizedGemm
Metal/Kernel/Gemv/... Metal/Kernel/Gemv
Metal/Kernel/Qwen3Layers/... Metal/Kernel/Qwen3Layers
Metal/Kernel/RMSNorm Metal/Kernel/RMSNorm
Metal/Kernel/Sampling/Argmax Metal/Kernel/Sampling/Argmax
ChatSession run ChatSession run
Forward pass Forward pass

The prefix Metal/Kernel/Matmul runs both GEMM and GEMM_MXU in one pass. The session and language-model groups require the test model path configured by the test helpers.

Output layout

Every run writes into target/criterion/<label>/…, where <label> is a free-form name you choose (e.g. m2_max, a19). The Criterion baseline you saved lives at target/criterion/<label>/<benchmark-path>/<baseline-name>/.

Running on macOS

From the repo root. Use an absolute CRITERION_HOME so it doesn't resolve relative to the package dir:

CRITERION_HOME="$PWD/target/criterion/m2_max" cargo bench \
  -p uzu-engine \
  --lib -- "Metal/Kernel/Matmul" \
  --save-baseline matmul_baseline_m2_max

Set UZU_CAPTURE='<benchmark path>' to capture the first matching batch; named helpers use exact Criterion paths. Append @path.gputrace to choose the output (default /tmp/uzu.gputrace for one capture). Streams also expose prefill and zero-based decode:N per stream; ranges use start..end, e.g. prefill..decode:2.

Running on iPhone (via cargo-dinghy)

Run one benchmark group at a time to avoid the iOS watchdog killing the app.

Set IPHONEOS_DEPLOYMENT_TARGET (value from .config/platforms.toml [envs]) for all iOS builds; without it the link step fails with undefined symbols (e.g. ___chkstk_darwin) because objects are built for a newer SDK than the default deployment target.

Key flags:

  • -e CRITERION_HOME=criterion/a19 — on-device env var. Path is relative to the app's cwd (Documents/), so this becomes Documents/criterion/a19/ on device. Keep it directly under Documents/ — nested parents (e.g. Documents/target/) do not exist on a fresh install and the pre-run sync cannot create them.
  • --sync-dirs "$(pwd)/target/criterion=Documents/criterion" — syncs the criterion tree between host and device before and after the run, so results written on device land back in the repo's target/criterion/. $(pwd) is required (absolute path) because the cargo runner is launched with cwd set to the package dir, not the workspace root.
DEVICE=<DEVICE_ID>

IPHONEOS_DEPLOYMENT_TARGET=26.4 cargo dinghy \
  -d "$DEVICE" \
  -e CRITERION_HOME=criterion/a19 \
  --sync-dirs "$(pwd)/target/criterion=Documents/criterion" \
  bench -p uzu-engine --lib -- \
    "Metal/Kernel/Matmul" \
    --save-baseline matmul_baseline_a19

On-device criterion sanitizes group path separators, so results land in target/criterion/a19/Metal_Kernel_Matmul_<GEMM|GEMM_MXU>/…/ on the host, next to any m2_max/ baselines.

Viewing reports

Open the Criterion HTML report:

open target/criterion/report/index.html

To inspect a specific label only:

open target/criterion/m2_max/report/index.html
open target/criterion/a19/report/index.html

Cold GEMV

GEMV-class benches cycle through enough quant-buffer copies to cover a 256 MiB weight working set before reusing one. This avoids ranking kernels on cache-warm weights; pools allocate lazily, so criterion filters skip their cost.