Archived
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:
1 parent
1f661bf2e0
commit
859efc7208
35 files changed
+799
-399093
No files matched your search
@@ -58,7 +58,7 @@ std::vector<Case> build_cases() {
|
||||
add("scale", { HI("M", 6), HI("n", 645), HS("dtype", dt) }, EB(6, 645, w, 2));
|
||||
add("softmax", { HI("M", 6), HI("n", 645), HS("dtype", dt) }, EB(6, 645, w, 2));
|
||||
add("gather_rows", { HI("M", 6), HI("n", 640), HS("dtype", dt) }, EB(6, 640, w, 2));
|
||||
for (const char* act : { "silu", "gelu", "gelu_tanh", "sigmoid" })
|
||||
for (const char* act : { "silu", "gelu", "gelu_tanh", "sigmoid", "quick_gelu" })
|
||||
add("act_mul", { HI("M", 6), HI("n", 645), HS("act", act), HS("dtype", dt) },
|
||||
EB(6, 645, w, 3));
|
||||
add("mul_weight", { HI("M", 9), HI("n", 645), HS("dtype", dt) }, EB(9, 645, w, 2));
|
||||
|
||||
@@ -117,16 +117,27 @@ AVX_KERNEL(mul_weight) {
|
||||
* rather than the algebraically equal g / (1 + e^(-2z)), which differs by an ulp or two and after
|
||||
* bf16 rounding measurably moves embeddings away from transformers'. vf_exp saturates, so the
|
||||
* large-|g| limits come out as g and 0. erf has no vector form here, so gelu stays scalar. */
|
||||
static inline float act_of(int act, float g) {
|
||||
static inline float round1(uint32_t dt, float v) {
|
||||
if (dt == RAD_F32) return v;
|
||||
uint16_t t;
|
||||
row_from_f32(dt, &v, &t, 1);
|
||||
float r;
|
||||
row_to_f32(dt, &t, &r, 1);
|
||||
return r;
|
||||
}
|
||||
|
||||
static inline float act_of(int act, float g, uint32_t dt) {
|
||||
if (act == 0) return g / (1.0f + sc_exp(-g));
|
||||
if (act == 1) return 0.5f * g * (1.0f + sc_erf(g * 0.70710678118654752f));
|
||||
if (act == 3) return 1.0f / (1.0f + sc_exp(-g));
|
||||
if (act == 4) // quick_gelu: sigmoid of the 1.702*g the unfused mul would have stored
|
||||
return 1.0f / (1.0f + sc_exp(-round1(dt, g * round1(RAD_BF16, 1.702f))));
|
||||
const float k0 = 0.7978845608028654f, k1 = 0.044715f;
|
||||
return 0.5f * g * (1.0f + sc_tanh(k0 * (g + k1 * g * g * g)));
|
||||
}
|
||||
|
||||
/* sy[0..n) = act(g[0..n)) for silu (0) and gelu_tanh (2); the tail is the scalar form. */
|
||||
static inline void act_row(int act, const float* g, float* sy, int64_t n) {
|
||||
static inline void act_row(int act, const float* g, float* sy, int64_t n, uint32_t dt) {
|
||||
int64_t i = 0;
|
||||
const vf one = vf_set1(1.0f);
|
||||
if (act == 0) {
|
||||
@@ -145,7 +156,7 @@ static inline void act_row(int act, const float* g, float* sy, int64_t n) {
|
||||
vf_storeu(sy + i, vf_mul(vf_mul(half, x), vf_add(one, t)));
|
||||
}
|
||||
}
|
||||
for (; i < n; ++i) sy[i] = act_of(act, g[i]);
|
||||
for (; i < n; ++i) sy[i] = act_of(act, g[i], dt);
|
||||
}
|
||||
|
||||
AVX_KERNEL(act_mul) {
|
||||
@@ -155,8 +166,8 @@ AVX_KERNEL(act_mul) {
|
||||
if (n <= 0) return RAD_E_SHAPE;
|
||||
const int64_t rows = rows_of(Y, n);
|
||||
if (rows <= 0 || numel(A) < rows * n || numel(B) < rows * n) return RAD_E_SHAPE;
|
||||
static const char* kActs[] = { "silu", "gelu", "gelu_tanh", "sigmoid" };
|
||||
const int act = p_enum(a, "act", kActs, 4, -1);
|
||||
static const char* kActs[] = { "silu", "gelu", "gelu_tanh", "sigmoid", "quick_gelu" };
|
||||
const int act = p_enum(a, "act", kActs, 5, -1);
|
||||
if (act < 0) return RAD_E_INVAL;
|
||||
|
||||
AVX_PARALLEL_FOR_IF(rows * n >= AVX_PAR_MIN)
|
||||
@@ -166,7 +177,7 @@ AVX_KERNEL(act_mul) {
|
||||
float* sy = scratch_f32_c((size_t)n);
|
||||
const float* g = in_row(A, rowoff(A, r, n), n, sa);
|
||||
const float* u = in_row(B, rowoff(B, r, n), n, sb);
|
||||
act_row(act, g, sy, n);
|
||||
act_row(act, g, sy, n, Y->dtype);
|
||||
/* sa is free once the activation is taken (g is sa or the tensor); sb may hold u, which
|
||||
* is still needed. */
|
||||
roundtrip(Y->dtype, sy, n, sa);
|
||||
@@ -314,7 +325,7 @@ AVX_KERNEL(conv2d) {
|
||||
if (gelu) {
|
||||
/* The biased sum as y's dtype holds it, then the activation of the rounded value. */
|
||||
const float* yr = in_row(Y, rowoff(Y, o0 + to, rout), rout, acc);
|
||||
for (int64_t e = 0; e < rout; ++e) acc[e] = act_of(1, yr[e]);
|
||||
for (int64_t e = 0; e < rout; ++e) acc[e] = act_of(1, yr[e], RAD_F32);
|
||||
out_row(Y, rowoff(Y, o0 + to, rout), rout, acc);
|
||||
}
|
||||
}
|
||||
|
||||
+19
-8
@@ -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;
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include "rad_sample.h"
|
||||
|
||||
#include <cfloat>
|
||||
#include <vector>
|
||||
|
||||
using namespace avx;
|
||||
|
||||
@@ -287,11 +288,14 @@ AVX_KERNEL(sample_ngram_ban) {
|
||||
return ((word >> (uint32_t)(col % 32)) & 1u) != 0u;
|
||||
};
|
||||
const int64_t plen = n - 1, p0 = hl - plen;
|
||||
/* The prefix is the same n-1 tokens compared at every search position: read once. */
|
||||
std::vector<int64_t> pref((size_t)plen);
|
||||
for (int64_t j = 0; j < plen; ++j) pref[(size_t)j] = tok_at(p0 + j);
|
||||
|
||||
for (int64_t idx = search_start; idx < search_end; ++idx) {
|
||||
bool match = true;
|
||||
for (int64_t j = 0; j < plen; ++j) {
|
||||
if (tok_at(idx + j) != tok_at(p0 + j)) { match = false; break; }
|
||||
if (tok_at(idx + j) != pref[(size_t)j]) { match = false; break; }
|
||||
}
|
||||
if (!match) continue;
|
||||
const int64_t tok = tok_at(idx + plen);
|
||||
|
||||
Reference in new issue
Block a user