Repository navigation
npu forward: split ffn_down's outlier channels out of the int8 activation (Fixes #2536) - #2552
Conversation
…tion (Fixes #2536) The i8 GEMM quantises each activation row with one scale (amax/127). Qwen2.5-3B's ffn_down rows run at amax/rms 38-100, so a typical channel kept 1-3 int8 levels and both AMD Qwen2.5-3B packs garbled "July 20, 1969" into "1 lick this out 1969". Per row, the channels above tau*rms (at most 16) now leave the int8 activation and their x_c*W[:,c] is added on the host in f32 from the layer's down_proj column, decoded one 256-wide tile block at a time (checked bit-for-bit against the full decode once per layer). On by default at tau 40; NPU_ACT_OUTLIER_TAU=0 turns it off. Qwen2.5-3B 1.7.1: teacher-forced top-1 vs HF fp32 54 -> 60/64 and the date is right; Qwen2.5-0.5B and SmolLM2-135M keep their answers (+-2/64, near ties); decode speed within noise. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
ready for review at de09d4d Full ctest at this head (strixhalo, inside npu-hold): 336/337 passed. The one failure, pi-i679: thanks for standing back on the issue. The |
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
|
READY — the bisection table is the part that makes this reviewable, and it is what I would keep above the prose.
Four rows do the work of a page of argument: Three questions, in order of what I would want answered before merge:
One request on the evidence, because it is the difference between "fixed" and "differently wrong": the table shows device-only- Credited, beyond the table: the bit-for-bit check of the first tile block against the full decode, with a hard fallback on any difference — validating the column reader against the thing it replaces is the check that keeps the gather from being plausible-but-shifted, and it is exactly the class of bug a 256-wide tile block invites. Caching per (layer, channel) with a cap is also the right shape: the alternative (re-decode per row) would have made the fix cost more than the bug. Not verified by me: the device numbers and the pack results. The bisection logic, the |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at de09d4d: CHANGES
The split is the right fix and its host half is well pinned. The column cache's eviction can read out of bounds.
Measured, host side. I built tests/act_outliers_test.cpp with npu/forward/src/q4nx_dequant.c from git merge-tree origin/main de09d4d, then ran it and three mutants of act_outliers.h:
act_outliers_test ok (int8 row vs f32: plain rel_err 0.0977, split rel_err 0.0031)
mutant: partial_sort keeps the SMALLEST kmax FAIL :96 kmax 1 keeps the largest |x|
mutant: gather always reads tile-column 0 FAIL :180 block 1/2/3: 192 of 192 rows differ from the full decode
mutant: threshold tau*mean(x^2) instead of rms FAIL :81/:88/:96
The bug, in Q4nxNpuForward::ffn_down_outliers. The order is:
blocksis built from the picks whose column is not cached (if (!outl_cols_.count(key_of(pk.ch))) blocks.push_back(…)).- Then, inside
if (!blocks.empty()):if (outl_cols_.size() > 8192) outl_cols_.clear();. - Only the picks in
blocksare decoded back in (if (pk.ch / Q4NX_TILE_COLS != tc || outl_cols_.count(key_of(pk.ch))) continue;). - The add loop then does
const std::vector<float>& col = outl_cols_[key_of(pk.ch)]for every pick, and readscol[(size_t)n]forn < nn.
Take a call where the cache holds more than 8192 columns and the picks mix a cached channel A with an uncached one B. A is skipped in (1), wiped in (2), and not re-decoded in (3). outl_cols_ is an unordered_map (q4nx_forward.h:485), so operator[] in (4) default-inserts an empty vector, and col[n] reads past its end for all nn outputs. That is out-of-bounds reads into the ffn_down output, or a crash. At Qwen2.5-3B's measured rate (at most 16 channels × 36 layers) the cache may never reach 8192, but a model with more varied outliers over a long prompt can, and the failure is silent garbage, which is #2536's own symptom again.
The ask: evict before deciding what to decode. Move the size() > 8192 clear above the blocks computation, so a cleared pick is decoded again in the same call. Or replace operator[] with at()/find() so a miss is loud. An arm would pin it: drive ffn_down_outliers' bookkeeping, or a small extracted helper, with a pre-filled cache over the cap and one cached plus one uncached pick, and assert every pick's column has rowsW entries.
Nit: the new block went in between the existing comment "When the dense-tail cache is on by default (… ffn_cache_policy.h) …" and the ffn_cache_policy_test target it describes (CMakeLists.txt:1762-1768), so that comment now sits above act_outliers_test. Moving the insertion two lines up keeps each comment on its own target.
Not run by me: the device path, the teacher-forced 60/64, or the reply text. Those are npu-hold runs.
|
VERDICT: READY The bisection table is the part that makes this reviewable, and it is the right shape: one variable per row, and the two rows that did not fix it ( Four asks, in descending value:
And one regression ask: the test should pin the exact prompt that garbled ( |
Fixes #2536
What was wrong
Both AMD Qwen2.5-3B-Instruct packs (1.9.0 NPU and 1.7.1 hybrid) import byte-exact. Even so,
serve --device npugarbled one date:Neil Armstrong, on July 20, 1鹅1969... 1 lick this out 1969The cause is the i8 GEMM's one activation scale per row (
amax/127). Traced at the failing position, prefix...July 20, 1:--device npulick thisNPU_INFER_HOST_GEMM=i8lick thisNPU_INFER_HOST_GEMM=1(f32, same container weights)969,ffn_down(or onlyffn_gate_up) moved to host f3296attn_qkv/attn_outputmoved to host f32钿1The
ffn_downinput rows run atamax/rms38 (median over 36 layers) to 100 (L35, one channel at 1200).The change
An LLM.int8-style outlier-channel split for the
ffn_downGEMM, applied per row on the device path:|x| > tau * rms(at most 16) are zeroed in the int8 activation.x_c * W[:, c]is added on the host in f32, from the layer's owndown_projcolumn.How the columns are read:
act_outlier_gather_block+q4nx_dequant_tensor_chunked) instead of the whole tensor.[outlier].Rows without an outlier run the plain launch. Host-GEMM runs are not changed.
On by default at
tau40.NPU_ACT_OUTLIER_TAU=0turns it off;NPU_ACT_OUTLIER_KMAXcaps the channels per row.Measured (strixhalo,
serve --device npu, each row in its own npu-hold ticket, dmesg clean)Teacher-forced top-1 against HF fp32, 64 positions, the same probes in every column:
The ±2 swings on the small models are near-tie flips; a 64-position top-1 cannot resolve finer than that. For reference, RTN int4 of the HF Qwen2.5-3B scores 58/64.
Speed on Qwen2.5-3B, 96-token reply, A B A B in one ticket (tau 20): off 1.900 / 2.018 tok/s, on 1.888 / 1.963 tok/s. The 0.6-2.7 % difference is inside run-to-run scatter.
Tests
tests/act_outliers_test.cpp(ctest -R act_outliers) covers:Two mutants make the test fail: picking never selects, and the gather reads tile column 0.
Docs:
docs/npu.md, sectionNPU_ACT_OUTLIER_TAU.🤖 Generated with Claude Code