Skip to content

ggml-openvino: enable structural replay fusion for Qwen - #458

Merged
wine99 merged 23 commits into
dev_backend_openvinofrom
openvino-structural-recurrent-replay
Nov 27, 2026
Merged

wine99 merged 23 commits into
dev_backend_openvinofrom
openvino-structural-recurrent-replay

Conversation

@wine99

@wine99 wine99 commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Translate Qwen recurrent replay into standard OpenVINO graphs for complete dynamic stateless GPU workloads. The translation emits convolution and GDN replay subgraphs and uses Assign sinks for writable GPU cache banks.

The companion GPU pass recognizes the full subgraph, removes matched sinks and preserves the model's Results. Fused kernels update the caller-owned cache inputs directly. When a sink remains, llama.cpp reads its state after inference and copies it back to the GGML bank. Mixed-precision GPU execution of the unfused graph can round bank values. Companion OpenVINO PR · Previous llama.cpp integration

Replay graph contract

Benchmarks

Cache-only batched-bench

Qwen3.6-35B-A3B-UD-Q4_K_M on Intel Arc B390 with Unified KV, 524,288 allocated KV slots, batch/microbatch 2048/512, native softplus, q4_asym64_all, and 4-D KV. All timed cases imported a saved compiled model in cache-only mode. The harness used n_rs_seq=2, the default GGML_OPENVINO_KV_SLICE=1, and GGML_OPENVINO_GPU_KV_ROUND_UP=0. Each cell shows prefill seconds @ aggregate decode tokens/s for one active / four active sequences.

Backend/configuration 1K prompt 8K prompt 32K prompt
OV fused replay, Unified KV 6.970 @ 38.21 / 2.730 @ 93.81 5.256 @ 35.09 / 23.961 @ 71.96 32.132 @ 24.74 / 153.741 @ 40.31
OV unfused replay, Unified KV 15.211 @ 11.43 / 25.311 @ 23.99 54.603 @ 10.43 / 219.892 @ 22.27 Unmeasured

Each case used eight warmup steps and 64 timed decode steps per sequence. The unfused measurements used the same GPU build with the recurrent replay fusion pass disabled in a benchmark-only build. That path executes the standard replay graph and copies its Assign states back to GGML. The fused 32K values are from the repeat run with the same saved blob and KV capacity. The first case in each process includes importing the model in prefill time.

The llama.cpp cache-key fix uses the declared shapes of the replay control inputs to allow reuse across active batch sizes. The writable-input interface uses a new key for replay blobs. Cache-key fix

32K GPU KV rounding repeat

Both runs used the same saved model in cache-only mode, with the same workload order and 524,288 Unified KV slots. The repeat set GGML_OPENVINO_GPU_KV_ROUND_UP=1. Changes are relative to the rounding-off 32K run in the table above.

Active sequences Prefill s, off → on Prefill time increase Aggregate decode tok/s, off → on Decode throughput decrease
1 32.13 → 32.66 +1.6% 24.74 → 22.10 −10.6%
4 153.74 → 186.33 +21.2% 40.31 → 35.59 −11.7%

Final logits matched bitwise. The table compares separate processes. A single-sequence test that switched rounding during decode in the same context measured 24.84 → 23.17 tok/s (−6.7%).

Continuous MMLU

Four server slots, eight question texts, 512 outputs per request, immediate replacement, and a fresh server per case. Rates are decode tokens/s per request, excluding prefill. MTP used a two-token draft limit.

Measured build In flight Comparison Decode tok/s per request Change Draft accepted / generated
Previous structural replay, on-demand 1 MTP off (unfused recurrence) → MTP on (fused replay) 17.48 → 44.37 +153.8% 2614/2936
Previous structural replay, on-demand 4 MTP off (unfused recurrence) → MTP on (fused replay) 10.96 → 24.34 +122.0% 2612/2941
Archived public-op build, cache-only 1 MTP off (fused recurrence) → MTP on (fused replay) 37.35 → 54.83 +46.8% 2614/2940
Archived public-op build, cache-only 4 MTP off (fused recurrence) → MTP on (fused replay) 21.56 → 30.12 +39.7% 2626/2914

Each arrow compares two measurements from the same build. The fused-to-fused rows use the archived November 23 public-op run with GPU KV rounding disabled. Its MTP-off path fused ordinary recurrence and its MTP-on path fused pending-token replay.

Validation

  • Release OpenVINO backend build passed on the branch based on dev_backend_openvino at 35b416e6c.
  • The four-layer Qwen3.6 model passed one- and four-sequence checks including rejected-suffix replay. The full Qwen3.6 35B model ran all 42 layers on the GPU and its 32K cached repeat matched the previous fused interface's final logits bitwise.

AI assistance

Paired with Codex in design, implementation, and validation.

ravi9 and others added 15 commits November 25, 2026 01:59
On the naive path every node of an op-test graph is a model output,
except an in-place node that another node reads: it got no output, so
its destination buffer was never written. GATED_DELTA_NET_CACHE_FUSION
copies the final state into the cache and then reads the copy, so the
cache kept its old contents.

Write such a node back through its view_src, as is already done when
nothing reads it. Model graphs are unchanged.
An in-place FILL on a view into part of a larger buffer produced only
the filled values. Its output stands for the whole in-place source, so
the write-back put those values at the start of the buffer and not at
the view's offset.

For a contiguous view with a static shape, build the whole buffer: the
elements before the view, the filled values, then the elements after
it. Add NodeContext::get_inplace_op_src() to detect the in-place case.
softplus was computed as relu(x) + log(1 + exp(-|x|)). When exp(-|x|)
is below the precision of 1 + exp(-|x|), the log returns 0 or one ulp,
so for x below about -16 in f32 the result was 0 or far off. The
SOFTPLUS test case with inputs in [-16.6, -16] fails on CPU and GPU.

Compute log1p(y) for y = exp(-|x|) with the series
y - y^2/2 + y^3/3 - y^4/4 when y < 0.01, where its relative error is
below 2e-9, and keep log(1 + y) above that.
Rounding the GPU KV length up to a power of two makes a batch attend up
to twice the rows that hold data right after each power of two. Decode
reads every attended row, so long contexts lose decode speed.

The GPU plugin keeps memory for each attended length: several large
buffers for a prompt batch, about one copy of K and V for a decode
batch. Keep the power of two for prompt batches (more than 16 tokens
per sequence), and round decode batches up to a multiple of 2^k / 4 and
of 256. That is four lengths per power of two, with at most 25% masked
rows.
The backend's .clang-format keeps a lambda on one line only when it is a
function argument (AllowShortLambdasOnASingleLine: Inline). The constant
helpers in the softplus and FILL translators are assigned to variables,
so clang-format splits them. Formatting only.
…ep for the outputs

On the static path (NPU, or any device with GGML_OPENVINO_FORCE_STATIC=1) the prefill model of a graph with
KV-shared attention layers is compiled with only the cache writes as outputs. The new conversion pass
KeepCacheResults (openvino/pass/keep_cache_results.cpp, registered in pass_registry/common.cpp from
GraphTraits::kv_only_outputs) removes the other Results and the Parameters nothing reads any more, so OpenVINO
drops everything that only feeds the logits: for models whose tail layers read an earlier layer's KV (Gemma 3n/4
E-series) every KV-shared layer and the LM head. When the batch needs logits, the decode model is run once on the
output token: it rewrites that token's KV rows with the same values and runs the dropped layers against the
complete cache. The decoder keeps its full output list and only reports which outputs are caches; an imported
cached model takes the cache outputs as its output names.

It is on by default and applies only where it pays off and is valid: graphs with KV-shared attention, at most one
output token per batch, and no HFA prefill or decode KV windows. GGML_OPENVINO_KV_ONLY_PREFILL=0 turns it off; the
earlier name GGML_OPENVINO_NPU_KV_ONLY_PREFILL is still accepted.

Gemma 4 E2B Q4_0 on NPU (KV_SLICE=0, NPU_TURBO=YES): llama-bench pp512 1320-1375 -> 2349-2454 t/s,
pp2048 1146-1202 -> 2268-2513 t/s; E4B pp512 587 -> 773, pp2048 591 -> 847 t/s. llama-server, E2B, 3016-token
prompt, prefill chunk 512, context checkpoints off: 1097-1108 -> 2136-2448 t/s. Greedy outputs match the full
prefill on 3 of 4 needle prompts; the fourth differs by one near-tie token that also flips between baseline runs.
GGML_OPENVINO_NPU_HOST_GATHER=1 gathers the rows of large host-resident embedding tables (at least
64 MiB, raw GGUF data, ids from a graph input) on the host. The static graph gets the rows through a
ggml_host_rows_<node> input instead of an OpenVINO Gather over the table. With NPUW function calls the
table becomes a function input, and gathering from an input that large costs about as much as
streaming the whole table every call: 21.6 ms of the ~62 ms Gemma 4 E2B decode step, against 0.45 ms
for the same partition with the table baked in. A table used only for such lookups (the Gemma 3n/4
per-layer embedding table) is no longer converted, requantized or bound to the NPU.

Gemma 4 on NPU (NPU_TURBO=YES, KV_SLICE=0, 868-token prompt), together with KV-only prefill: E2B decode
65.3 -> 40.3 ms/token, prefill 925 -> 392 ms, system commit 8.5 -> 4.4 GB, load 109 -> 61 s; E4B decode
150 -> 72 ms/token, prefill 1931 -> 1022 ms, commit 14.6 -> 9.1 GB. Greedy outputs match the default on
3 of 4 needle prompts; the fourth differs by one near-tie token that also flips between baseline runs.
…s blob cache

Make the two NPU options work with GGML_OPENVINO_NPU_WEIGHTLESS_BANK / SELF_CONTAINED_MMAP:
- host gather also accepts tables in OpenVINO buffers when a raw-weight cache mode keeps (or, with
  SELF_CONTAINED_MMAP, wraps) the raw GGUF bytes there;
- a warm import binds only the decoder inputs present in the compiled blob, since KV-only prefill
  (out_ids) and host gather (token ids) can leave inputs unused, as HFA prefill already did;
- a weights file that cannot be written (e.g. a full disk) no longer fails the graph: the pair is
  compiled and run without being cached.

Gemma 4 on NPU (NPU_TURBO=YES, KV_SLICE=0, 868-token prompt), weightless cache + MMAP warm start with
host gather and KV-only prefill: E2B load 1.8 s, system commit 2.5 GB, prefill 385-414 ms, decode
39.5-41 ms/token, cache 1.5 GB; E4B load 3.3-3.6 s, commit 4.7-4.8 GB, prefill ~1.0 s, decode 68.5-69.8
ms/token, cache 3.3 GB. Greedy outputs are byte-identical to the default path on export and import.
…5_K tables when set

Q4_K and Q5_K token_embd tables default to the channel-wise int8 of ggml_openvino_get_requant_type() on NPU and
ignored GGML_OPENVINO_NPU_TOKEN_EMBD_QUANT. They now follow it when it is set explicitly; the Q6_K behaviour and
all defaults are unchanged. Gemma 4 E2B ties a Q4_K table to the output layer: int8 stores it in 384 MiB,
q4_0_128 in 198 MiB (llama-server working set -0.18 GB; accuracy not yet measured).
… bank cache

With a weightless-bank model cache and sliced (non head-major) KV, round the static prefill attention size up to the decode KV window, so prefill sizes are the window sizes (1024/2048/4096/8192). The export run (SELF_CONTAINED_MMAP=0) then builds the prefill model of every window at startup, so an import-only run (SELF_CONTAINED_MMAP=1) has a pair for any prompt length.
… pruned masks

- GGML_OPENVINO_KV_SLICE takes 0, 1 or a multiple of 256 as the base of the
  attended KV windows; decode windows (BLOB_PREBUILD) use the same base.
- KV-only prefill works together with decode KV windows: the output step runs
  the decode model of the window the next decode step uses.
- gemma-4 prune_shared (upstream ggml-org#32342) adds host-filled out_mask/out_mask_swa
  and inp_prune_ids/inp_prune_keep inputs. The pruned masks are not graph
  inputs: their rows are gathered in the OV model from the (sliced, padded)
  input masks with inp_prune_ids. The KV-only output step ignores
  inp_prune_keep, and HFA prefill is not used for the pruned masks.
Implement KV-only prefill and host gather optimizations for NPU
@ravi9
ravi9 force-pushed the dev_backend_openvino branch from fcf6088 to 2371fe4 Compare November 26, 2026 03:53
@ravi9
ravi9 force-pushed the dev_backend_openvino branch 2 times, most recently from 7d8ccda to e0b9fd7 Compare November 27, 2026 02:29
@wine99
wine99 marked this pull request as ready for review November 27, 2026 06:00
@wine99
wine99 requested a review from cavusmustafa as a code owner November 27, 2026 06:00
@wine99
wine99 merged commit a11efd3 into dev_backend_openvino Nov 27, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants