Repository navigation
audio_ops: softmax_row 3.24x by breaking the dependent reduce chains (engine#2366) - #2379
Conversation
PR Reviewer Guide 🔍Here are some key observations to aid the review process:
|
|
VERDICT: NEEDS-CHANGE at The bug. The vectorised body applies for (; i + 4 <= n; i += 4) {
x[i] = std::exp(x[i] - mx); s0 += x[i]; // ... four lanes
}
for (; i < n; ++i) {
s0 += x[i]; // <-- no exp, no write-back
}The tail adds the raw logits to the denominator and leaves those elements un-exponentiated in the output buffer, so for any for (; i < n; ++i) {
x[i] = std::exp(x[i] - mx);
s0 += x[i];
}Why the verification cannot see it. Your measurement shape is 1500×1500 — What to add so it stays fixed. A row length with a remainder — The rest is well argued and I would keep it. Four accumulators with One edge case worth a line of comment, not a change: the max scan now finishes with |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at 4de0ad3: READY
The trade you declare is real, but it is smaller than you say — I measured it, and the float output is bit-identical while the double sum is marginally more accurate.
Your comment says "the four partial sums are combined as ((s0+s1)+(s2+s3)) rather than left to right, so the result differs from the old one in the last bits". The accumulator is double (double s0..s3), and the output is static_cast<float>(x[k] * inv) — so the reordering perturbs a double sum by a few double-eps, and only then rounds to float. Computing both orders on the encoder's row shape:
2000 rows x n = 1500, exp(x - max) as float in, float out
float elements differing 0 of 3,000,000
max |float delta| 0.000e+00
max relative delta in the double sum 6.196e-16 (≈5.6 double-eps)
float eps 1.192e-07
The double sum moves by 6e-16; a float rounds at 1.2e-7. Nine orders of magnitude apart — so the number that flows onward does not move at all in three million elements.
And the direction is worth stating too: four chains of 375 terms have a tighter error bound than one chain of 1500, so the new order is slightly more accurate, not less. "One changes order for a 3× win" undersells it: this changes order for a 3× win and a marginally better sum.
So the honest version of the trade, if you want it in the comment, is stronger than the one there now:
the partial order changes the double sum by ~6e-16 relative (measured, 5.6 eps), which is nine orders below float eps: the float output is unchanged in 0 of 3,000,000 elements at the encoder's row shape, and the four-chain order is marginally more accurate than the single chain.
That also fixes the evidential gap: "the whisper chain's ids are the check that it did not matter" is a coarse check — 23 greedy ids will not flip for a last-bit change, so passing is consistent with both "harmless" and "small real drift". The count above discriminates; the ids cannot. Both arms are already in one process for your microbenchmark, so the count costs one loop over the rows.
What is right and worth keeping as-is: the max/sum split into four independent chains is the correct fix for the dependency chain, the tail loop handles n % 4, 1.0/sum in double then a single float multiply is the right precision ladder, and the 3.24× is measured on the real shape in one process so load cancels. In situ 0.98 s → 0.578 s is consistent with the microbenchmark, which is the check that matters.
Caveat I would state if the number goes in: my rows are Gaussian exponentials, not your encoder's activations. Both live in (0,1] after subtracting the max, so the conclusion should carry, but it is a proxy distribution — running the count on a real ONEBIT_AUDIO_OP_TIMES capture would make it a property of the model rather than of the shape.
Softmax is 0.98 s of the whisper encoder's 10.4 s (12 launches of 1500×1500), and the loop held it for two reasons a compiler cannot fix on its own: the max scan is a reduction with no
-ffast-math, andsum += x[i]with anexp()between each add is a chain of dependent double adds. Four accumulators for each turns both into independent work.Microbenchmark on the encoder's real shape (18000 rows × 1500, same input both arms, one process so load cancels):
In situ (
ONEBIT_AUDIO_OP_TIMES=1, whisper-small encoder):Softmax 12 calls0.98 s → 0.578 s — less than the microbenchmark because the executor runs 4 threads and the row loop was already partly overlapped there.The trade, stated plainly: per-element arithmetic is unchanged, but the summation order is not — the four partial sums combine as
((s0+s1)+(s2+s3))instead of left-to-right, so results differ from the old ones in the last bits. That is the same tolerance-side trade the MatMul loop refused (there the order was kept and only aliasing was declared); here order changes for 3×, so it needs a check that it did not matter, not an argument that it cannot:audio_ops_testpasses.whisper_transcribeonjfk.wavyields the same 23 ids:400 370 452 7177 6280 11 1029 406 437 428 1941 393 360 337 291 11 1029 437 291 393 360 337 428, atttft 38.504 s (load+mel 11.837 + encoder 26.667)on a box at load 11 (the quiet encoder is 10.4 s; absolute numbers are not comparable across loads, the ids are).Where this leaves #2366: the non-GEMM remainder was 30 % of the encoder and Softmax was its largest single member; that member is now ~0.58 s. The next largest are Conv 0.75 s and the 179 elementwise calls at 1.42 s — both still on the list, neither touched here.