Spirivia

Writing / note

Writing my own Vulkan inference runtime

Why Spirivia runs its models on its own raw-Vulkan runtime: a 50 ms ceiling on every GPU submission so a job cannot hold the screen, 13x to 19x less job CPU, and results that are bit-identical alone or batched and keyed to what produced them.

My laptop has a GPU, a Radeon 860M built into the chip. For most of Spirivia’s life it sat idle while the CPU did all the model work: transcription, word alignment, separating voices, recognising speakers.

This note is about why I ended up writing my own inference runtime for it, on raw Vulkan, instead of using one of the engines that already exist. The short answer is that every borrowed engine failed a test I cared about, and the tests turned out to be the interesting part.

Where the CPU went

ASR and alignment were 85% of job CPU, MEASURED on one 24 minute record: 1.113 and 1.000 CPU-seconds per second of audio, out of 2.483 total. The previous note explains why I measure CPU-seconds from the cgroup and not wall time. The GPU next to it was idle.

The question was how.

The borrowed engines, and how each one failed

I tried the existing GPU paths first. Each had a kill rule written down before it ran.

  • whisper.cpp with Vulkan used 11x to 25x less CPU for transcription. But the words came out different enough that the speaker attached to a word changed on 11% to 23% of words, MEASURED on two records. Kill at 2%. It also kept repeated phrases. Killed on quality.
  • ONNX Runtime with WebGPU for alignment passed on quality: every word time identical. Then, about once in 110 forward passes, it caused a GPU ring timeout and reset, on the same GPU that draws my desktop.
  • Parakeet-TDT v3 was much cheaper but 14 points of word error rate worse on Portuguese.

Other options didn’t need a run to rule out, because each gave up something structural (ADR 0008). IREE picks tiling per shape, so bits can change with batch size. Extending ggml-vulkan means a fork with shape-dependent split-k and no control of priority. wgpu has no cooperative matrices and no subgroup size control.

None of them was rejected on effort. ADR 0007 says effort is not a reason. An earlier analysis had dismissed an own runtime on “4 to 8 weeks of work” and “at most 2.4%”. Both were wrong reasons. Effort is not a measure of compute, and the 2.4% was one kernel scaled by compute-unit count, an estimate, not a measurement of the hardware.

The desktop must never stutter

The ring reset taught me the most important rule of the runtime.

A GPU queue is not preemptive in the way a CPU is. If you submit a long chunk of work, everything else that wants the GPU waits until it finishes. On an integrated GPU, “everything else” is the compositor that draws my screen. A long submission is a dropped frame. An unbounded one is a hang and a reset.

So spirivia-infer treats submission length as something it measures and bounds:

  • It uses a compute-only queue at LOW global priority, never the graphics queue.
  • At warm-up, calibrate runs every operation alone, reads GPU timestamps, and packs operations into slices that fit a budget. The default budget is 16 ms, one 60 Hz frame. The models set 4 ms.
  • Any single operation estimated above 12 ms splits itself before recording.
  • HARD_SLICE_MS is 50 ms. If a measured slice ever exceeds it, the run stops. The worker answers busy and the job retries in 2 seconds. No larger slice is ever submitted.

In the verdict runs, MEASURED: zero amdgpu timeouts or resets in the kernel journal. Three lab jobs and one test hit the 50 ms guard and stopped. Those cases now come back as busy and are retried.

One thing I haven’t measured yet: compositor frame pacing during an hour-long job. The guard bounds the worst case. It doesn’t prove every frame is on time. That measurement is still open.

One process owns the GPU

spirivia-gpud is the only process that touches the device. Jobs talk to it over a Unix socket. systemd starts it on the first connection and it exits after ten minutes with nothing loaded.

It runs like any other Spirivia background work: Nice=19, CPUWeight=idle, idle I/O. One device thread keeps a queue per model and serves them round robin, one small unit at a time, so cancel and pause act between units.

Keeping one resident worker instead of starting one per job was also measured, not assumed. Worker CPU-seconds were 1.45 and 5.35 resident against 7.67 and 10.49 per job, identical output, MEASURED.

GPU memory doesn’t live in any cgroup, so the kernel can’t count it for me. The worker reads available memory, memory pressure and the Vulkan memory budget before each load, leaves a reserve free, evicts idle models, or answers busy.

Bit-identical, alone or beside other work

The library needs caches keyed by content, and a resumable journal for long records (resumable processing is planned, not built yet). Both only work if a result does not depend on what else was running. If a window of audio gives one transcript when processed alone and a slightly different one when batched with three others, you can never trust a cached result.

Most inference engines don’t guarantee this. Reduction order changes with batch size, kernels get picked per shape, and the last bits of a float move. Usually nobody notices. A cache notices.

So the kernels are written for invariance: fixed per-row reduction order, a required subgroup size, pinned float controls, their own transcendental functions, exact-zero padding, keyed random numbers, a total order on beam search. The test called T4 checks it: a result alone must be bit-identical to the same result beside other work.

Results know what made them

Bit-identical is only useful if you also know when the bits are allowed to change. That’s Backend.

Backend is a blake3 digest over everything that decides a result’s bits: the runtime’s source hash, the weights’ address, precision, decode options, and per pipeline the SPIR-V hash, specialisation constants, subgroup size and the compiled pipeline binaries. That last one is the GPU’s actual machine code.

Every result carries its Backend. The lab puts it into the stage keys for ASR, alignment, diarization and speaker id. If a Mesa update changes the compiled machine code, the affected stages become stale and get redone. If the update doesn’t change the machine code, nothing happens.

There’s a check for the check. At warm-up a probe battery hashes one fixed input per kernel family. If a hash changes while Backend didn’t, the key is missing something that decides bits, and the worker refuses to serve.

The project rule follows from this: a change that alters a result’s bits must change Backend.

What it bought

The verdict ran on two real records, 321 and 1426 seconds, five interleaved rounds, each in its own idle-weight scope. MEASURED:

modelCPU path, CPU-sown runtime, CPU-s
ASR (Whisper turbo, excl. VAD)334 / 13560.9 / 3.1 plus worker
Align (wav2vec2)211 / 9910.77 / 2.9
Diarize (Nemotron-3)35 / 1791.5 / 3.0
Speaker id (resnet293)70.2 / 268.30.62 / 0.70 per request

Whole pipeline: 586.6 / 2471.9 CPU-seconds against 44.2 / 127.0. That’s 13x and 19x less CPU. Alignment had 0 word-time differences against the CPU reference, diarization 0 speaker changes, speaker id 0 decision differences. ASR is not bit-identical to the CPU engine’s int8 output. It stays within the gap between that engine’s own int8 and float32 outputs, which is the floor I hold it to.

Wall time only improved 1.4x to 1.6x. The GPU itself is now the bottleneck, in Whisper’s encoder attention. And the voice activity detection still on the CPU is now about 90% of what’s left. That’s the next target.

What it cost

Spirivia now owns six model programs over about twenty kernel files. That’s permanent, model-specific code. Results are keyed per host and per GPU, with no identity across devices. There’s no CPU fallback inside the runtime; the original CPU paths stay in the lab as the reference.

What to take from this

  • On a shared GPU, bound every submission by measured time, and stop rather than submit a longer one.
  • If you cache model outputs, test that a result alone equals the same result in a batch. Most engines don’t promise it.
  • Key results on what actually decides their bits, down to the compiled machine code, and check the key with a fixed probe.

Sources: ADR 0008, docs/design/spirivia-infer.md and docs/research/processing-performance-2026-11.md in the Spirivia repository.