Scale the prefill softmax by row, not by wave

The h256 prefill online-softmax branch must remain wave-uniform, but the
previous branch let one row's growth vote make every row update its reference
maximum. A row's rounding could therefore depend on which other rows shared
the launch.

Keep the branch uniform and make each row update only when its own cross-half
maximum exceeds its own `m_ref + PGROW`. Rows below threshold retain `m_ref`.
This fixes row-coupled rescaling; it does not claim full-model bitwise
invariance across chunk sizes, since other shape-dependent paths remain.

The dedicated regression selector is `--case attn_prefill_rows` (exact match;
`--case rows` is not recognized). The older kernel-name selector runs the
regression only for that named GQA row; rerunning it for GQA4 also passes the
row checks. Three launch pairs run on six BF16 head-256 rows: GQA 2/4/6/8/12
and GQA6 with 4-token pages (18 assertions).
Against v1.15.1, 17/18 assertions failed; with this change 18/18 pass
bit-identically. For Spark GQA4, the baseline 58/128 pair differs in
588/118,784 elements (max abs 0.00195). Although this implementation file is
named `gqa6`, the parameterized body is included by the other head-256 GQA
wrappers too.

Controlled 917-token Spark BF16 rerun (same prompt, binary, model, TP1, BF16
KV, no prefix cache; only libr4d changed): chunk 128 vs 1024 kept top token 198
and all 20 candidates in common before and after. Maximum shared top-20
log-probability delta was 0.23717 on v1.15.1 and 0.28474 with this patch.
This example does not improve; it is included to avoid claiming general chunk
invariance. The earlier raw full-vocabulary pre-fix result (131,070/131,072
logits changed, max abs 0.46925) has no matching post-fix capture; the paired
API top-20 comparison is the comparable result.

On a separate 32-document, 31,391-position BF16 evaluation, chunk-1024 vs
chunk-128 reference achieved 99.49% top-1 agreement (31,231/31,391): 160
positions still choose a different top token between chunk shapes, not against
external ground truth. Mean KL was 0.000879, median 1.29e-5, p99 0.00462,
p99.9 0.10119, max 3.2961. The pre-fix result on this exact set was not
measured. At the separately
investigated scored row 461 (document 20, token position 477), prefix replay's
recorded top token 62594 was replaced by 2561 before the fix; after it, token
62594 was restored with a 0.184-nat top-token log-probability gap. That
0.184 is not a full-set KL, and the 3.296 maximum comes from the separate
31,391-position evaluation.

Fast CTest: 141/141. Full card selftest still has one unrelated
`hc_read_e4m3` prefill-tile failure, reproducible on untouched v1.15.1. No
throughput improvement is claimed or measured.
This commit is contained in:
Constantin authored and StillDeadcode committed 2026-11-01 20:59:44 +01:00
1 parent 55da38afbd
commit b264e9bd0c
2 files changed
+136 -7

No files matched your search

+7 -7
View File
@@ -593,13 +593,13 @@ void r4d_attn_prefill_kernel(const R4DArgs a)
float smax = sc[0];
#pragma unroll
for (int e = 1; e < 8; ++e) smax = fmaxf(smax, sc[e]);
// The rescale is the wave's, so a row's bits depend on the rows sharing its wave. Only
// live rows decide it: a row past qlen limits its keys at its own qpos, past ctx, so it
// scores whatever the cache's last block held before (another request's KV), and the
// last query rows of a sequence's final block would round by that.
if (wave_any(live && smax > m_ref + PGROW)) {
const float rmax = fmaxf(smax, swap16(smax));
const float mnew = fmaxf(fmaxf(m_ref, rmax), -3.0e38f);
// Both half-waves hold the same query and different keys. The branch must be
// wave-uniform, but each query may change scale only on its own growth threshold;
// otherwise other live query rows change its rounding when the chunk length changes.
const float rmax = fmaxf(smax, swap16(smax));
const bool row_grow = live && rmax > m_ref + PGROW;
if (wave_any(row_grow)) {
const float mnew = row_grow ? fmaxf(fmaxf(m_ref, rmax), -3.0e38f) : m_ref;
const float alpha = ex2(m_ref - mnew);
#pragma unroll
for (int dt = 0; dt < NKS; ++dt)