review: apply the Unlimited-OCR code-review findings

The 2a1ca01..f308216 range's 45 review findings (12 performance, 33 quality)
are applied: ABI 23 for the RadBatch append, the fused q|k|v projection, the
batched patch-embed GEMM and pitched layout copies, the sam_attn w-dot hoist
with an alignment-gated vector row path and NaN poisoning for malformed
segments, the fused quick_gelu act, and the R-SWA/scheduler/drafting gates.

Quality items: stale comments corrected, env reads hoisted, the ngram_window
negative refusal, the parts-form chat template instruction, the whitelist
fallback warning, and the segmented R-SWA refusal of the fixed/runtime window
mix.

The 3.2 MB generated resample header becomes a 9.6 MB little-endian blob
(tests/data/golden/uocr_resample_golden.bin) read by a small loader;
resample_golden.py emits and round-trip-verifies it.

One perf-med item (AVX2 bicubic passes/LUT patch staging in the processor) is
left as the follow-up the review itself recommends; the code says why.

Verified: ninja -C build-review -k 0; host ctest 89/89; GPU r4d_selftest
0 failures, kbench/kbench_moe and the card tests pass.
This commit is contained in:
Juup committed 2026-10-20 07:06:18 +00:00
1 parent 1f661bf2e0
commit 859efc7208
35 files changed
+799 -399093

No files matched your search

+19 -8
View File
@@ -11,11 +11,13 @@
* values of their tile and take part in the softmax; `window` 0 is one tile over the segment.
*
* This is the same structure as libref's: one thread owns one output row and one head, the tile's
* keys are walked jh-major so the q.Rh dot is computed once a tile row, the softmax subtracts the
* tile maximum, and the output is rounded once. The dots are plain f32 loops over head_dim, which
* LLVM vectorises at every level; the tile is small enough that no blocking is needed. The tables
* are widened to f32 once a launch and resampled along dim 0 when a geometry's axes need it
* (get_rel_pos's linear interpolation).
* keys are walked jh-major so the q.Rh dot is computed once a tile row and the q.Rw dots are
* hoisted per query row (the hip kernel's hoist, bit-identical reuse), the softmax subtracts the
* tile maximum, and the output is rounded once. The dots stay scalar f32 loops over head_dim on
* purpose: the x86 leg is the no-GPU/harness path, and a SIMD reduction would reassociate the f32
* accumulation and break bit-parity with the libref oracle. The tables are widened to f32 once a
* launch and resampled along dim 0 when a geometry's axes need it (get_rel_pos's linear
* interpolation).
*/
#include "avx_vec.h"
#include "avx_common.h"
@@ -114,14 +116,23 @@ AVX_KERNEL(sam_attn) {
const int64_t qst = laststride(q);
for (int64_t c = 0; c < hd; ++c) qt[c] = ldt(q, qbase + c * qst);
/* The w rel-pos dot depends on (lw, jw) alone, not on jh: dot it once per query
* row and reuse it through the tile rows. The same scalar loop in the same order,
* so every reused value is bit-identical to the per-(jh, jw) recompute. */
std::vector<float> wr((size_t)tw);
for (int64_t jw = 0; jw < tw; ++jw) {
const float* rwrow = &Rw[(size_t)((lw - jw + tw - 1) * hd)];
float wv = 0.0f;
for (int64_t c = 0; c < hd; ++c) wv += qt[c] * rwrow[c];
wr[(size_t)jw] = wv;
}
for (int64_t jh = 0; jh < th; ++jh) {
const float* rhrow = &Rh[(size_t)((lh - jh + th - 1) * hd)];
float hr = 0.0f;
for (int64_t c = 0; c < hd; ++c) hr += qt[c] * rhrow[c];
for (int64_t jw = 0; jw < tw; ++jw) {
const float* rwrow = &Rw[(size_t)((lw - jw + tw - 1) * hd)];
float lgt = hr;
for (int64_t c = 0; c < hd; ++c) lgt += qt[c] * rwrow[c];
float lgt = hr + wr[(size_t)jw];
const int64_t kh = ty * th + jh, kw = tx * tw + jw;
if (kh < gh && kw < gw) {
const int64_t krow = base + kh * gw + kw;