Files
leleka/docs/igpu-prefill.md
T
Pavlo RatushnyiandClaude Sonnet 5.5 bf4f01b3ac perf: vec4 shared-memory attention kernel for the GPU prefill (STRA-219)
One full layer over a 512-token batch ending at position 4096: 0.52 s -> 0.22 s (8192: 1.07 s -> 0.44 s).
4096-token synthetic prompt, single unpaired run: 91.9 s -> 67.5 s (60.7 tok/s; CPU 247 s earlier). Same
tests against the CPU attention. Teacher-forced vs llama.cpp on the gate text: 95.3% / KL 0.0121 (the
earlier kernel gave 97.1% / 0.0100): the two GPU runs differ from each other by KL 0.0058, as much as GPU vs
CPU (0.0067-0.0080) and the CPU's own scalar vs SIMD (0.0048); a second text shows GPU-vs-CPU noise equal
to CPU-scalar-vs-SIMD noise (docs/igpu-prefill.md). Reported as borderline against the strict KL limit.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-15 00:38:20 +02:00

6.9 KiB

Integrated-GPU prefill (STRA-219) — experimental

Status: experimental, off by default, not wired into the worker yet. Crates: leleka-igpu (WGSL kernels on wgpu, Vulkan or DX12), leleka-gptoss-igpu (the layer driver, implements leleka_gptoss::PrefillAccelerator). Production serving stays Python-free and works without a GPU; the GPU path is an optional accelerator for the prompt-processing phase only.

What runs where

  • Prefill of a long prompt: all layers on the GPU, layer after layer. One layer's weights are repacked (threaded) and uploaded, every batch of up to 512 tokens runs through it, and the layer's keys and values are read back as half-precision rows into the CPU decoder's cache (State::store_kv). Only one layer of weights is on the GPU at a time; the hidden states of the prompt stay there between layers.
  • The output projection of the last token and all decoding stay on the CPU (decode is memory-bandwidth bound and the iGPU shares that bandwidth: ~40 GB/s measured on both sides, see experiments/igpu-spike).
  • Prompts shorter than the threshold passed to Model::prefill_reuse_accelerated and prefix-cache hits take the CPU path (the cached prefix is uploaded to the GPU per layer when the GPU continues a prompt).

Kernels (all tested against the CPU decoder's own functions, crates/igpu/tests)

int8 dot4I8Packed GEMM over Q8_0 and MXFP4 weights with bias and residual-accumulate epilogues, grouped MoE GEMM with a tile table and row gather, RMSNorm+Q8_0 quantize, Q8_0 quantize, SwiGLU-OAI+quantize, RoPE (the decoder's own rotation table), half-precision KV store, grouped-query attention with sinks (full and sliding), router logits + top-4 + softmax, slot-ordered expert combine.

Two findings worth keeping: the compiler folds unpack2x16float(pack2x16float(x)) to x, so Q8_0 block scales are rounded to half with an explicit bit-level round-to-nearest-even (f16_round); and dynamically indexed private arrays spill, so the generated GEMM code uses named scalars.

Numerics and quality gate

Not bit-identical to the CPU path: activations are quantized to int8 with the same rule (scales rounded to half exactly like ggml), but the GPU sums in other orders and its division/inverse square root are not exactly rounded, so about 0.04% of quantized values land one step away. Teacher-forced comparison on the 383-token gate text (benchmarks/quality/gpt-oss-20b-teacher-forced, 2026-10-15, Core Ultra 7 265U, Intel iGPU via Vulkan, batch 512). Two GPU runs are shown because they differ only by the attention kernel (a rewrite that reorders float sums at the 2e-5 level):

Pair best token agrees mean KL (shared top-20) NLL (ours vs reference)
GPU prefill (scalar-load attention) vs llama.cpp 97.1% (372/383) 0.0100 1.2976 vs 1.2828 (+0.0148)
GPU prefill (vec4 attention) vs llama.cpp 95.3% (365/383) 0.0121 1.3108 vs 1.2920 (+0.0188)
GPU prefill (first) vs Leleka CPU (AVX-VNNI) 97.4% (373/383) 0.0067 1.2976 vs 1.2979
GPU prefill (vec4 attention) vs Leleka CPU (AVX-VNNI) 96.6% (370/383) 0.0080 1.3108 vs 1.3067
the two GPU runs against each other 96.6% (370/383) 0.0058
CPU (AVX-VNNI) vs llama.cpp (from the gate README) 97.1% 0.0080 +0.0061

The criteria are ≥95% agreement, mean KL ≤ 0.012 and NLL within 0.02. The second GPU run misses the KL limit by 0.0001; this is the measure's noise on this one text, not a measured regression: the two GPU runs differ from each other (0.0058) about as much as either differs from the CPU path (0.0067, 0.0080) and as the CPU's own scalar and SIMD kernels do (0.0048); the divergence sits in positions 10-60 of this Harmony text, where the model amplifies rounding differences. On a second text (the first 635 tokens of docs/cpu-runtime.md, no llama.cpp reference) the same comparisons give: GPU vs CPU-VNNI KL 0.0019 / 97.5%, CPU-scalar vs CPU-VNNI 0.0014 / 97.3%, GPU vs CPU-scalar 0.0018 / 96.7%, i.e. the GPU adds about as much noise as swapping the CPU kernel level does. Conclusion: borderline against the strict llama.cpp criterion on the one reference text, equal to the CPU's noise floor everywhere else; it stays experimental and off by default until the gate has more reference text. Greedy continuations of a 128-token synthetic prompt were identical from both caches (8 tokens).

Speed (first measurements, no claim yet)

Not a performance result: single runs on a machine under no load control, synthetic random-id prompts, no paired A/B at the workloads in the charter, GPU numbers include the CPU repack thread competing for memory bandwidth.

Prompt CPU (AVX-VNNI, 10 threads) GPU prefill Where the GPU time went
128 tokens 7.0 s (18 tok/s) 11.3 s (weights not yet overlapped) upload 6.0 s
1024 tokens 54.0 s (19.0 tok/s) 20.9 s (48.9 tok/s) attention 6.4, experts 13.8
4096 tokens 247 s (16.6 tok/s, earlier session) 91.9 s (44.6 tok/s) with the first attention kernel, 67.5 s (60.7 tok/s) with the vec4 one attention 45.3 -> 22.8, experts 44
8192 tokens not run 244.9 s (33.5 tok/s) with the first attention kernel attention 147, experts 95

At short prompts the GPU path is not faster (hence the default 256-token threshold in the worker). Long prompts: experts are the floor (the grouped GEMM runs at about 0.6 TOPS of integer work, crates/igpu/examples/gemm_bench.rs) and attention grows with the square of the context (one full layer over a 512-token batch ending at position 4096: 0.52 s with the first kernel, 0.22 s with the vec4 one; sliding layers 0.008 s).

Next

Long-prompt A/B (2k-8k tokens) against the CPU path with token counts and distributions as the charter requires; overlap weight upload with GPU work; optional resident weights when free RAM allows; attention scoring optimization; the worker-side switch with free-RAM admission (Phase D).

The worker (leleka-igpu-worker)

cargo build --release -p leleka-igpu-worker makes a worker with the CPU worker's protocol and decoder plus the GPU prefill: leleka-igpu-worker --serve --model <gpt-oss.gguf> [--gpu-min-tokens 256] [--gpu-batch 512] (other arguments as for leleka-cpu-worker). It is a separate executable so that the CPU worker keeps no GPU dependency. Prompts with at least --gpu-min-tokens tokens to compute (after prefix reuse) are prefilled on the GPU; shorter ones and all decoding run on the CPU. With no usable GPU it says so on standard error and serves from the CPU; if a GPU prefill fails the request is redone on the CPU and the GPU is not used again in that session. The control plane starts it like any worker (set the worker executable to this binary).

Not done yet: admission by free RAM (the GPU path needs about 2 GB of device memory beside the model mapping at batch 512, and the repacking thread competes with the GPU for memory bandwidth), and an A/B run of a full agent workload through the control plane.