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

+7 -3
View File
@@ -28,6 +28,8 @@ __device__ __forceinline__ float act_of(float g) {
if (ACT == 0) return g / (1.0f + expf(-g));
if (ACT == 1) return 0.5f * g * (1.0f + erff(g * 0.70710678118654752f));
if (ACT == 3) return 1.0f / (1.0f + expf(-g));
if (ACT == 4) // quick_gelu: sigmoid of the bf16-rounded 1.702*g, as the unfused mul stores it
return 1.0f / (1.0f + expf(-bf2f(f32_to_bf16(g * bf2f(f32_to_bf16(1.702f))))));
return 0.5f * g * (1.0f + tanhf(0.7978845608028654f * (g + 0.044715f * g * g * g)));
}
@@ -739,11 +741,11 @@ __global__ __launch_bounds__(kLrThreads) void attn_local_rel_kernel(
// ============================================================== launchers
// act: 0 silu, 1 gelu (erf), 2 gelu_tanh, 3 sigmoid.
// act: 0 silu, 1 gelu (erf), 2 gelu_tanh, 3 sigmoid, 4 quick_gelu (sigmoid(bf16(1.702*a))).
extern "C" int r4d_act_mul_bf16(long a, long b, long y, int M, int n, int act, int a_ld, int b_ld,
int y_ld, long stream) {
if (!a || !b || !y) return -1;
if (M <= 0 || n <= 0 || act < 0 || act > 3) return -2;
if (M <= 0 || n <= 0 || act < 0 || act > 4) return -2;
const dim3 grid((uint32_t)((n + kThreads - 1) / kThreads), grid_rows(M));
#define R4D_AM(A) \
hipLaunchKernelGGL(act_mul_kernel<A>, grid, dim3(kThreads), 0, (hipStream_t)stream, \
@@ -752,7 +754,8 @@ extern "C" int r4d_act_mul_bf16(long a, long b, long y, int M, int n, int act, i
if (act == 0) R4D_AM(0);
else if (act == 1) R4D_AM(1);
else if (act == 2) R4D_AM(2);
else R4D_AM(3);
else if (act == 3) R4D_AM(3);
else R4D_AM(4);
#undef R4D_AM
return hipGetLastError() == hipSuccess ? 0 : -4;
}
@@ -1033,6 +1036,7 @@ extern "C" int r4d_rad_act_mul(const RadArgs* a, RadStream s) {
else if (act && !std::strcmp(act, "gelu")) ai = 1;
else if (act && !std::strcmp(act, "gelu_tanh")) ai = 2;
else if (act && !std::strcmp(act, "sigmoid")) ai = 3;
else if (act && !std::strcmp(act, "quick_gelu")) ai = 4;
if (ai < 0) return R4D_NO(RAD_E_INVAL);
const long long n = r4d_geti_or(a, "n", r4d_dim(Y, (int)Y->rank - 1));
if (n <= 0) return R4D_NO(RAD_E_SHAPE);