Skip to content

tests: the attention accumulator must stay bounded by max|V|, not by the context length (Refs engine#1536) - #323

Merged
bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/fa-accumulator-range-test
Nov 12, 2026
Merged

bong-water-water-bong merged 1 commit into
1bit/hrx-vulkan-patchedfrom
1bit/fa-accumulator-range-test

Conversation

@bong-water-water-bong

Copy link
Copy Markdown

Refs engine#1536 and engine#1346. Two rows that pin the property the f16 P·V accumulator lives on — and that no existing flash_attention.* row can see, because they all fill Q and V with small magnitudes and compare against a CPU reference that narrows identically. A range limit inside the accumulator is invisible to both sides of such a comparison.

The construction is model-free and deterministic

Q = 0, K = 0 ⇒ every score is identical, so after the running-max subtraction every softmax weight is exactly 1; V = constant ⇒ the correct output is that constant at any context length. An unnormalized accumulator would instead hold kv · V:

flash_attention.accumulator_range.128.1024.v100      kv·V = 1.0e5   → past f16
flash_attention.accumulator_range.128.1024.v60000    kv·V = 6.1e7   → past f16 by 10^3

The oracle is an analytic constant, not a second implementation — which is the only kind of oracle that cannot share the defect under test.

Both pass, and that is the point

[PASS] 1/2  flash_attention.accumulator_range.128.1024.v100      88 ms
[PASS] 2/2  flash_attention.accumulator_range.128.1024.v60000    95 ms
test-hrx-ops summary: 2 passed, 0 failed

So the accumulator is normalized per block: its bound is max|V|, not n_ctx · max|V|. That refutes a mechanism I had asserted twice on #1536 and once on #1346, and the refutation is now in the tree rather than only in a comment — both issues have the retraction with these numbers.

Why the arms are worth keeping after the refutation

They are the regression test for the normalization. If the per-block scaling is ever "optimized" away — it is a multiply per block that a future reader may see as redundant, since the sum is tracked separately — these two rows go red for exactly the right reason, at a context length and a magnitude that make the failure unambiguous.

And the reason the mistake happened

The loop's initial value is named scaled_current_output0, which reads as an unnormalized carry if you stop there; it is the running result divided by the running sum. One test on one ticket settled what two careful readings had gotten wrong, which is the argument for writing the test before the second comment rather than after it.

…the context length

Two arms that pin the property the f16 P*V accumulator lives on, and that no existing
`flash_attention.*` row can see: they all fill Q and V with small magnitudes and compare against a
CPU reference that narrows identically, so a range limit in the accumulator is invisible to them.

The construction is model-free and deterministic: Q = 0 and K = 0 make every score identical, so
after the running-max subtraction every softmax weight is exactly 1, and the correct output is the
constant V whatever the context length. An UNNORMALISED accumulator would hold kv * V:

  flash_attention.accumulator_range.128.1024.v100       kv*V = 1.0e5   -> overflows f16
  flash_attention.accumulator_range.128.1024.v60000     kv*V = 6.1e7   -> overflows f16 by 10^3

Both pass on 1bit/hrx-vulkan-patched (88 ms and 95 ms, 2 passed / 0 failed), which is the useful
result: the accumulator IS normalised per block, so its bound is max|V| rather than n_ctx * max|V|.
That refutes a mechanism that had been asserted twice on engine#1536 and once on engine#1346 -- with
numbers instead of another reading -- and leaves these rows as the regression test for the
normalisation: if the per-block scaling is ever "optimised" away, they go red for the right reason.

Worth recording why the refutation was needed: the loop's initial value is named `scaled_current_output0`,
which reads as an unnormalised carry if you stop there. It is the running result divided by the running
sum. A test cost one ticket; the two comments it retracts cost more.

Refs ggml-org#1536, ggml-org#1346
@bong-water-water-bong

Copy link
Copy Markdown
Author

VERDICT: READY at 6113e92 — the construction is exact, model-free, and it pins the one property no existing row can see.

The hand-derivable oracle

// the P*V accumulator is f16 and holds the UNNORMALISED sum, so its ceiling is n_ctx * max|V| rather than max|V|
// every weight is exactly 1 and the accumulator holds n_ctx * V while the correct answer is V itself
// 102,400, past 65,520, and the output is exactly 100 if the accumulation did not overflow

Q = 0 and K = 0 → every score identical → after the running-max subtraction every softmax weight is exactly 1 → the output is the mean of the V rows, which with V = 100 is exactly 100. Nothing about the model matters, so the expected value is arithmetic rather than a reference implementation — the strongest oracle available, and the same shape as the hand-computed conv rows in ggml-org#2237 and the 11,158,839.9 alpha base in ggml-org#1976.

Against it: n_ctx × max|V| = 102,400, which is past f16's 65,520. So an f16 accumulator saturates and an f32 one does not, and the assertion distinguishes them exactly rather than by tolerance.

The observation about the existing rows is the PR's real argument

"no existing flash_attention.* row can see [it]: they all fill Q and V with small magnitudes and compare against a CPU reference that narrows identically, so a range limit in the accumulator is invisible to them."

Two independent reasons for blindness, and both are the class this fleet keeps finding: the inputs are too small to reach the ceiling, and the reference narrows the same way, so the reference inherits the defect. A test whose oracle commits the same narrowing can only ever confirm agreement, which is why this one had to be constructed instead of compared.

REQUIRE(setenv(kDisableQwenDispatchEnv, "1", 1) == 0) is the right complement: pin the property on the generic path so the arm cannot pass by taking a fast path that never accumulates this way.

Two asks

  1. confirm both arms are red on the parent — for a property this exact, one line ("on the pre-fix tree the second arm returns 0/inf instead of 100") is the whole proof that it tests what it claims;
  2. state the second arm's construction so the file has two distinguishable cases (the body mentions two arms; the comment block visible here only derives one). If the second is the f32-accumulator control, say so — a control that passes both ways is what proves the first arm's failure is about the accumulation and not about the launch.

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.

1 participant