Skip to content

perf(r2t2): audio encoder and prompt prefill on the NPU - #1146

Merged
fastflowlm-devkit-swap-github-app[bot] merged 14 commits into
mainfrom
perf/r2t2-npu-enc-prefill
Nov 25, 2026
Merged

fastflowlm-devkit-swap-github-app[bot] merged 14 commits into
mainfrom
perf/r2t2-npu-enc-prefill

Conversation

@tawei-amd

Copy link
Copy Markdown
Collaborator

Summary

r2t2:1.7b (Confucius4-R2T2) now runs its audio encoder and its prompt prefill on the NPU, with kernels of its own, by default. Before, both ran on the host and only the decode used the NPU.

  • Audio encoder: the products (conv2/3 as im2col, conv_out, qkv, o, fc1, fc2, proj1/2; ~99 % of the FLOPs) run on the new FLM_Xclbin/bfp16_gemm, a 32-core block-float GEMM for bf16 weights. It ships as audio_gemm.xclbin.
    • Each product keeps A and W at bf16 precision: the bfp16 value plus the scaled rest, sent as extra rows of A and extra columns of W in the same call.
    • Plain bfp16 drifts the conv stack on quiet input.
  • Prompt prefill: the products (q/k/v, o, gate/up, down) run on the new FLM_Xclbin/dequant_mm_mix_bfp16 (prefill.xclbin). It reads the decode's mixed Q4_1 / Q8_0 weights in place, so the package needs no extra weight copy.
  • Prompt attention runs on the NPU from 384 tokens. It uses the shared prefill attention inside prefill.xclbin's hw context through control-packet switches (FLM_Xclbin/r2t2_switches → r2t2.switches, prefill/qwen3_asr_launch.hpp).
    • These switches take the same system-wide lock as d3's.
    • Without the file it falls back to attn.xclbin from 1024 tokens.
  • One engine lock (npu_mu) covers every NPU call. Background work no longer submits from another hw context while the decode runlist is in flight; before this lock, that made streaming requests time out.
    • Streaming encodes the next step on the host while the NPU decodes (the encoder keeps a 635 MB host copy of its weights).
  • Fallbacks: QWEN3_ASR_CPU_ENCODER=1, QWEN3_ASR_CPU_PREFILL=1, QWEN3_ASR_CPU_ATTENTION=1, QWEN3_ASR_NO_SWITCH=1. Each also applies on its own when a package lacks the matching file. The README lists them all.
  • MSVC build fixes for the engine. MHA now comes from libmha instead of compiling ../mha/*.cpp in, which avoids a symbol clash.

The new xclbins / .switches come from this PR's CI run (pending swap). No binaries are committed here.

Speed (Ryzen AI 9 HX 370, Linux)

before (host) this PR
encoder, 68 s clip 745 ms (11 ms/s) ~430 ms (6.3 ms/s)
encoder, 7-18 s clips 21-35 ms/s 8-10 ms/s
prompt prefill, 85 tok 121 ms 77-83 ms
prompt prefill, 512 tok — 209 ms (attn.xclbin 346, host attention 233)
prompt prefill, 904 tok 1069 ms 485 ms (without switches)
prompt prefill, 2048 tok 2589 ms 815 ms (attn.xclbin 919, host attention 1334)
decode 26-29 ms/token unchanged (NPU)

Very short clips (~1 s) encode faster on the host: each layer still has to read its weights once. They are cheap either way.

Accuracy

LibriSpeech test-clean, all 2620 utterances, WER (paired bootstrap 95 % CI vs the BF16 PyTorch reference):

run WER errors vs BF16
BF16 reference (CPU) 2.22 1178 —
host encoder + host prefill (QWEN3_ASR_CPU_ENCODER=1 QWEN3_ASR_CPU_PREFILL=1) 2.25 1193 +0.03 [-0.02, +0.08]
NPU encoder + host prefill 2.24 1188 +0.02
NPU encoder + NPU prefill (default) 2.30 1220 +0.08 [+0.02, +0.15]
  • The NPU encoder is lossless at this resolution.
  • The slim drop comes from the NPU prefill reading the 4-bit mixed decode weights; the host prefill reads Q8_0. Most of it is number formatting ("3,000" vs "three thousand").
  • On utterances where no run wrote a digit, the four runs score 2.11 / 2.13 / 2.12 / 2.14.

In-house 180-clip set (BF16 reference in parentheses):

  • English WER: 2.81 (2.89).
  • Chinese CER: 9.30 (8.96). One clip dominates that number; without it: 6.32 (5.98).
  • The paired 95 % CIs both include 0.

Testing

  • Engine harness on the rebased branch: the 180-clip set gives the same tokens as before the rebase.
  • Pressure on a Linux and a Windows NPU CI runner (run 45653508185, green). Each runner built the packages this PR's CI would build, ran the asr suite as Build and Test does, then ran a server pressure script three times:
    • default: 26/26 checks;
    • NPU attention for every prompt (QWEN3_ASR_NPU_ATTENTION_MIN=0): 20/20;
    • host encoder + host prefill: 20/20.
    • The checks cover: texts equal to the golden (EN, ZH, 68 s); 8 simultaneous clients; client hang-ups mid-stream and a hang-up storm; interleaved stream/offline; 5.7 and 10 min files; a 4-thread hammer; bad inputs → 4xx; a stable working set.
    • 18 s clip RTF: 0.07 on Linux, 0.08 on Windows.
  • Streaming goldens vs ground truth: 4.51 % with the NPU path, 6.08 % with the host path.
  • Unit gates:
    • make run_npu in audio/test: 8/8 clips at cos ≥ 0.99963.
    • dequant_mm_mix_bfp16 self-test host: all four layer shapes, L 28-2048, relL2 0.0041.
    • bfp16_gemm self-test host.
  • devkit_check.py passes. kernel_assignment.json/.md, build_units.json and rtp_addresses.lock.txt are updated together.

🤖 Generated with Claude Code

tawei-amd and others added 12 commits November 25, 2026 10:50
Y = act(X W^T + b), bf16 out; X and W packed to bfp16ebs8 by the host
(weights once at load), fp32 accumulation, bias and optional GELU on the
core. The host sequence programs the memtile weight path per call:
hybrid (K <= 1536: round 1 starts on the first chunk, later rounds reuse
the resident panel), panel (K <= 3072) or stream (any K, a memtile ring).
check_memtile_bds.py verifies at build time that the design leaves those
descriptors, locks and queues free. Bit-exact against an fp32 product of
the packed operands; 15-22 TFLOPS on encoder/prefill shapes, DDR-bound.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The audio encoder's matrix products (conv2/conv3 as im2col GEMMs,
conv_out, fused q/k/v, out_proj, fc1, fc2, proj1, proj2: ~99 % of its
FLOPs) run on audio_gemm.xclbin (FLM_Xclbin/bfp16_gemm). The host keeps
conv1, LayerNorm, window attention, GELU and residuals.

- Weights are packed to bfp16 once at load into device buffers.
- Every product keeps A and W at bf16 precision: each is split into
  its bfp16 value and the scaled rest, sent as more rows of A / more
  columns of W in the same call, and summed on the host. Plain bfp16
  products drift the conv stack on quiet input.
- conv2/conv3 pack their im2col rows straight from the activation;
  8 chunks share one conv product.
- Host side vectorized: window attention, residual, LayerNorm (stats
  stay in double), per-encode scratch.
- QWEN3_ASR_CPU_ENCODER=1 (or a package without audio_gemm.xclbin)
  keeps the whole encoder on the host.
- kernel_assignment: r2t2:1.7b audio_gemm.xclbin = xclbins-bfp16_gemm;
  RTP lock stanza for it.

Measured (Ryzen AI 9 HX 370):
- 68 s clip: encoder 745 -> ~430 ms; 1 s clips stay faster on the host.
- 8-clip encoder gate passes (cos >= 0.99963, relL2 <= 0.028).
- conv cache test (ASan): no differences.
- 180 clips, mixed package: EN WER 2.76, ZH CER 6.32 (host encoder,
  same build: 2.85 / 5.88; release: 2.81 / 6.37).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…weights

A 32-core prefill GEMM for the r2t2:1.7b decoder that reads the decode
weights in place: Q4_1 (q, k, o, up, gate) and Q8_0 (v, down) blocks in
the 10240-byte units of qwen3_asr_npu, so the prefill needs no second
copy of the weights. The core pairs dequantize to block float and
multiply with X, which the host packs as block float.

- The weight format is a runtime parameter per column, so one call can
  stack several weights (K, V and Q).
- Each core computes nb slabs of 64 outputs x 128/nb rows (nb = 1, 2,
  4, 8): short prompts read every weight once.
- The even core takes both credits of a weight chunk and hands one to
  the odd core, so both always work on the same chunk.

Self-test (host/host.cpp): every shape of a decoder layer, L 28 to 2048,
matches the block-float reference to the bf16 output rounding (relative
L2 0.0041). Per layer: 1.6 ms at 28 tokens, 6.9 ms at 904.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The prompt's q/k/v, o, up/gate and down products run on prefill.xclbin
(dequant_mm_mix_bfp16), reading the decode layers' Q4_1 / Q8_0 weights in
place. Jobs of up to 512 rows are double-buffered: the host packs the next
job and unpacks the previous one while the NPU runs. Norms, RoPE, attention
and the KV cache stay on the CPU decoder. QWEN3_ASR_CPU_PREFILL=1 keeps the
whole prefill on the CPU.

Decoder prefill, NPU vs CPU: 85 tok 81 vs 120 ms, 904 tok 519 vs 1057 ms,
2048 tok 1443 vs 2589 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The decoder's projections interface gets an optional attention hook. The
NPU prefill implements it with attn.xclbin, the shared prefill attention
(generic_prefill_attention DH_128 GQA_1_2, modules/mha.hpp), one run per
layer for all heads; the host copies the layer's cache into the kernel's
K/V bands and the prompt's queries into 128-row aligned blocks.

Switching between prefill.xclbin and attn.xclbin costs about 2.5 ms twice
per layer, so the NPU takes the attention only when its causal work
reaches that of a 1024-row prompt (QWEN3_ASR_NPU_ATTENTION_MIN rows
overrides it; QWEN3_ASR_CPU_ATTENTION=1 keeps it on the host).

Decoder prefill, NPU vs host attention: 1152 tok 672 vs 711 ms, 1536 tok
831 vs 958 ms, 2048 tok 1099 vs 1443 ms; shorter prompts unchanged.
180 clips with the NPU attention forced on every prompt: English WER 2.72,
Chinese CER 5.78 (host attention 2.81 / 6.32).

The harness gets --split-at-audio (two prefills) to test a prompt that
continues a context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The up/gate unpack now writes silu(gate) * up in the block-float layout
the down call reads, into two device buffers used in turn, instead of a
float [n][I] array that the down job packed again. Same rounding (bf16
product, then the same encoder), so the results are bit-identical.

Decoder prefill: 85 tok 91 -> 77 ms, 150 tok 119 -> 115 ms, 904 tok
513 -> 498 ms, 2048 tok 1099 -> 1041 ms (down pack 68 -> 8 ms, up/gate
unpack 105 -> 71 ms over the 28 layers).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The projections interface now gives each product's bf16 result rows to a
sink of the decoder, called while the NPU computes the next rows. The
decoder does its per-row work there: q/k norm, RoPE and the cache rows
after q/k/v, and the residual add plus the next RMSNorm after o and down.
The float k, v, o and down arrays and three passes over the residual
stream per layer are gone. The CPU prefill runs the same per-row code.
Same operations in the same order, so the results are bit-identical
(NPU and CPU prefill, goldens).

Decoder prefill 2048 tok 1041 -> 931 ms (host rest 178 -> 73 ms),
904 tok 498 -> 485 ms; short prompts unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The background job that encodes the next streaming step (and, offline, the
next piece) now runs the encoder's products on the NPU, during the NPU
decode of the current step. A run of audio_gemm.xclbin's hw_context while
the decode runlist is in flight never completes: every streaming request
ended with "runlist failed execution (ERT_CMD_STATE_TIMEOUT)" after 60 s.

The engine now owns one mutex. The decode (forward, get_logits), the
prompt products and attention (qwen3_asr_prefill_npu) and the encoder's
products (qwen3_asr_audio_gemm_npu) hold it per call, so the background
work runs between two tokens. The wrappers keep a lock of their own when
none is given (the encoder test host).

A 6.7 s stream at 480 ms chunks: 500 after 63 s before, 200 in 1.5 s now,
text equal to the offline transcript.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… NPU decode

Since every NPU call of the engine takes one lock, the background encode
of the next streaming step ran its NPU products between the decode's
tokens: the step paid for it in full. That encode covers at most the
open window (a few hundred ms of new audio at 160-480 ms chunks), where
the NPU encoder is also slower than the CPU (32 frames: 50 vs 21 ms).

The encoder now keeps a host copy of its weights as well (635 MB) and
the streaming background encode uses it: it runs on the CPU while the
NPU decodes, as before the NPU encoder. Offline audio, the first step
and any encode in the critical path stay on the NPU; so does the
prefill. QWEN3_ASR_NPU_STREAM_ENCODE=1 keeps the old behaviour and
skips the copy.

25 streaming goldens (160/480/2000 ms chunks): 153 -> 119 s in total
(CPU fallback 136 s), finals byte-identical to the previous build.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
MSVC's OpenMP needs signed loop indices (two encoder loops used size_t).
On Linux the engine compiled ../mha/*.cpp into itself and exported a
second copy of MHA (the symbol clash check flagged MHA::Impl::IT); it now
links libmha.so, as the Windows build already links mha.lib.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…packet switches

r2t2.switches (FLM_Xclbin/r2t2_switches, aie2p-launchlib) holds the mm cold
start and the switches between prefill.xclbin's GEMM and the shared prefill
attention. With it the engine runs the prompt attention inside the prefill
hardware context, one submission per layer under the lock d3's switches
take, from 384 tokens (was 1024 on attn.xclbin's own context).
QWEN3_ASR_NO_SWITCH=1 falls back to attn.xclbin. Same output tokens as
attn.xclbin. Decoder prefill on a Ryzen AI 9 HX 370: 512 tokens 209 ms
(attn.xclbin 346, host attention 233), 2048 tokens 815 ms (919, 1334).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Security scan found no critical or high findings on lines changed by this pull request

View the security scan run for complete reports.

“New” means the finding is located in a changed file on an added or modified line; it is not a base-versus-head findings delta.

bfp16_gemm and dequant_mm_mix_bfp16 named host/seq_gen.hpp in a comment
of bitstream.mk; the CI guard that keeps kernel builds independent of
host/ counts any mention outside host.mk. Reword the two comments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@zaneni6
zaneni6 self-requested a review November 25, 2026 19:19
The dev-kit engine libraries and kernels that run 45681627194 built from
ef9ded7, staged into its product builds and tested on the NPU
(devkit_swap.py fetch): 6 engine file(s), 5 kernel(s).
CI checks these bytes against the run's staging manifests.

  src/lib/hrx/libqwen3_asr_npu.so
  src/lib/hrx/qwen3_asr_npu.dll
  src/lib/hrx/qwen3_asr_npu.lib
  src/lib/xrt/libqwen3_asr_npu.so
  src/lib/xrt/qwen3_asr_npu.dll
  src/lib/xrt/qwen3_asr_npu.lib
  src/xclbins/Confucius4-R2T2-NPU2/attn.xclbin
  src/xclbins/Confucius4-R2T2-NPU2/audio_gemm.xclbin
  src/xclbins/Confucius4-R2T2-NPU2/layer.xclbin
  src/xclbins/Confucius4-R2T2-NPU2/prefill.xclbin
  src/xclbins/Confucius4-R2T2-NPU2/r2t2.switches

Devkit-Swap-Run: 45681627194
Devkit-Swap-Tested: ef9ded7
@fastflowlm-devkit-swap-github-app

fastflowlm-devkit-swap-github-app Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Dev-kit auto-swap: merged (squash) at swap commit 557a047, which Build and Test run 45691034802 passed (swap commit 557a047 passed run 45691034802; approved at 557a047/ef9ded7635 by zaneni6).

@fastflowlm-devkit-swap-github-app
fastflowlm-devkit-swap-github-app Bot merged commit 4e59ddd into main Nov 25, 2026
70 checks passed
tawei-amd added a commit that referenced this pull request Nov 25, 2026
With #1146 the audio encoder and the prompt prefill of r2t2:1.7b run on
the NPU. Update the benchmark and model pages:

- accuracy: full LibriSpeech test-clean (2620 utterances), BF16 original
  2.22 % WER vs FLM 2.30 % (+0.08, 95 % interval +0.02 ... +0.15); the
  180-recording English / Mandarin set re-measured (EN 2.89 / 2.81,
  ZH 8.96 / 9.30, differences not significant)
- speed: encode + prefill 0.13 s (0-4 s) ... 0.25 s (12-20 s), decode
  ~38 tok/s; end to end ~0.11x real time; streaming 0.88 at 160 ms,
  0.37 at 480 ms chunks
- memory: 3.2 GB after load, 3.6 GB after the first request, 4.5 GB peak
  streaming a 5.5 min recording

Measured on Linux (Strix Point) with the CI-built library and xclbins.

Co-authored-by: tawei-amd <tawei@amd.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants