Repository navigation
perf(r2t2): audio encoder and prompt prefill on the NPU - #1146
Merged
fastflowlm-devkit-swap-github-app[bot] merged 14 commits intoNov 25, 2026
Merged
Conversation
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>
Security scan found no critical or high findings on lines changed by this pull requestView 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
self-requested a review
November 25, 2026 19:19
zaneni6
approved these changes
Nov 25, 2026
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
This was referenced Nov 25, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.FLM_Xclbin/bfp16_gemm, a 32-core block-float GEMM for bf16 weights. It ships asaudio_gemm.xclbin.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.prefill.xclbin's hw context through control-packet switches (FLM_Xclbin/r2t2_switches→r2t2.switches,prefill/qwen3_asr_launch.hpp).attn.xclbinfrom 1024 tokens.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.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.libmhainstead of compiling../mha/*.cppin, which avoids a symbol clash.The new xclbins /
.switchescome from this PR's CI run (pending swap). No binaries are committed here.Speed (Ryzen AI 9 HX 370, Linux)
attn.xclbin346, host attention 233)attn.xclbin919, host attention 1334)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):
QWEN3_ASR_CPU_ENCODER=1 QWEN3_ASR_CPU_PREFILL=1)In-house 180-clip set (BF16 reference in parentheses):
Testing
QWEN3_ASR_NPU_ATTENTION_MIN=0): 20/20;make run_npuinaudio/test: 8/8 clips at cos ≥ 0.99963.dequant_mm_mix_bfp16self-test host: all four layer shapes, L 28-2048, relL2 0.0041.bfp16_gemmself-test host.devkit_check.pypasses.kernel_assignment.json/.md,build_units.jsonandrtp_addresses.lock.txtare updated together.🤖 Generated with Claude Code