Archived
unlimitedocr: tiled/base e2e parity, per-segment tower layout, n-gram whitelist
The pass packer now keeps a view and its one-cell separator in one pass, and RadBatch carries each segment's grid host-side (enc_seg_h/enc_seg_w) so the DeepEncoder no longer reads device coordinates to pick a geometry. The tower lays out a line of n tiles as kMap rows of n*kMap features plus one newline cell, accepts an earlier chunk of a longer line, and emits the learned view_seperator for an explicit 4x4 separator rather than any square segment. The device sam_attn resamples a rel_pos table that is not 2*tile-1 rows on read (the 127-row global tables serve a 40-grid block as 79), matching libref/libavx. The n-gram ban gains the reference's optional whitelist (5th operand, OAI keys, generation_config alias) with range/dtype checks. Gates: tiled e2e stage cosines 0.9994-1.0, det/text 0.9901 with every coordinate within 1 px; two-page 0.9917 within 2 px; device selftest sam_attn (incl. 127->79) and ngram whitelist (1344 columns) 0 mismatches.
This commit is contained in:
1 parent
fad4ba7f1a
commit
52637ec186
37 files changed
+401566
-778
No files matched your search
+19
-13
@@ -2114,11 +2114,15 @@ static const RadOperandSpec oDry[] = {
|
||||
/* The n-gram ban's own plane: one i32 {n-gram length, window} pair a row (n 0 = off, window <= 0
|
||||
* = the whole staged history). A plane beside `params` rather than fields in it, because the
|
||||
* params row is the fixed 24-word RadSampleParams every sampler op shares. */
|
||||
/* `whitelist` is optional: a bitmask plane a row (a SET bit exempts that column from the ban,
|
||||
* the reference's whitelist), [M, ceil(n_vocab/32)] i32/u32, contiguous. Absent, nothing is
|
||||
* exempt. */
|
||||
static const RadOperandSpec oNgramBan[] = {
|
||||
{ "logits", RAD_OPD_INOUT, 0 },
|
||||
{ "params", RAD_OPD_IN, 0 },
|
||||
{ "history", RAD_OPD_IN, 0 },
|
||||
{ "ngram", RAD_OPD_IN, 0 },
|
||||
{ "logits", RAD_OPD_INOUT, 0 },
|
||||
{ "params", RAD_OPD_IN, 0 },
|
||||
{ "history", RAD_OPD_IN, 0 },
|
||||
{ "ngram", RAD_OPD_IN, 0 },
|
||||
{ "whitelist", RAD_OPD_IN, 1 },
|
||||
};
|
||||
static const RadOperandSpec oTemp[] = {
|
||||
{ "logits", RAD_OPD_INOUT, 0 },
|
||||
@@ -2838,10 +2842,12 @@ static const RadOpSchema kSchemas[] = {
|
||||
"occurred before, by its longest such repeat -- what stops a model looping on a phrase "
|
||||
"rather than on a token. Tokens the breaker plane marks are exempt."),
|
||||
SCHEMA("sample_ngram_ban", pPenal, oNgramBan,
|
||||
"the windowed no-repeat-ngram ban: every token completing an n-gram the last `window` "
|
||||
"tokens of the row's history contain is set to -infinity. `ngram` is one i32 "
|
||||
"{n, window} pair a row; the window bounds the n-gram's START index, as in the "
|
||||
"reference, window <= 0 searches the whole span and n 0 is off."),
|
||||
"the windowed no-repeat-ngram ban: every token completing an n-gram the last `window` "
|
||||
"tokens of the row's history contain is set to -infinity. `ngram` is one i32 "
|
||||
"{n, window} pair a row; the window bounds the n-gram's START index, as in the "
|
||||
"reference, window <= 0 searches the whole span and n 0 is off. The optional "
|
||||
"`whitelist` bitmask exempts a token whose column bit is set; absent, nothing is "
|
||||
"exempt."),
|
||||
SCHEMA("sample_temp", pMVocab, oTemp,
|
||||
"per-request temperature. temp <= 0 leaves the row untouched; greedy is sample_argmax."),
|
||||
SCHEMA("sample_mask", pMVocab, oMask,
|
||||
@@ -3860,9 +3866,9 @@ static const RadConstraint cAttnDenseH64[] = {
|
||||
C_EQ("head_dim", 64), C_EQ("gqa", 1), C_IN("dtype", "bf16"),
|
||||
};
|
||||
/* Unlimited-OCR's SAM tower (r4d_sam_attn_bf16.hip): one block per (segment, tile, head), a thread
|
||||
* a query row, q and the output accumulator in registers; only head_dim 64 is instantiated and the
|
||||
* tables must be exactly 2*tile-1 rows (a geometry needing get_rel_pos's resampling is libref's
|
||||
* and libavx's). */
|
||||
* a query row, q and the output accumulator in registers; only head_dim 64 is instantiated and a
|
||||
* rel_pos table that is not 2*tile-1 rows is linear-resampled on read (get_rel_pos), so the 127-row
|
||||
* global tables serve a 40-grid block as 79 rows. */
|
||||
static const RadConstraint cSamAttn[] = {
|
||||
C_EQ("head_dim", 64), C_IN("dtype", "bf16"),
|
||||
};
|
||||
@@ -6361,8 +6367,8 @@ R4D_WIN_PAIR(512, 1) R4D_WIN_PAIR(512, 2) R4D_WIN_PAIR(512, 4) R4D_WIN_PAIR(512,
|
||||
{ "sam_attn_bf16", "sam_attn", "attn",
|
||||
"Unlimited-OCR's SAM tower attention: windowed tiles with a decomposed relative-position term",
|
||||
"head_dim 64; any grid, window and segment count (a segment is one grid_h*grid_w); q/k/v/out at "
|
||||
"their own row pitch (column slices of one fused projection); rel_pos tables exactly 2*tile-1 "
|
||||
"rows (a geometry needing get_rel_pos's resampling is libref's and libavx's)",
|
||||
"their own row pitch (column slices of one fused projection); rel_pos tables of any row count "
|
||||
">= 1 (a table that is not 2*tile-1 rows is linear-resampled on read)",
|
||||
"bf16 query / key / value / out and rel_pos tables, i32 cu_seqlens, fp32 accumulate",
|
||||
RAD_DOMAIN_DEVICE, R4D_PRIO_ONLY,
|
||||
ROW(cSamAttn), NOTUNE,
|
||||
|
||||
@@ -17,9 +17,10 @@
|
||||
// row is computed once per (query row, tile row) rather than once per key.
|
||||
//
|
||||
// Head widths. HD is a template parameter and only 64 is instantiated (SAM ViT-B's 12 x 64); the
|
||||
// shim refuses any other width rather than reading past the registers. The tables must be exactly
|
||||
// 2*tile-1 rows, which every geometry the tower declares is; the shim declines a table that would
|
||||
// need get_rel_pos's resampling (libref and libavx serve that).
|
||||
// shim refuses any other width rather than reading past the registers. A table whose row count is
|
||||
// not 2*tile-1 is resampled on read (get_rel_pos's F.interpolate(size=want, mode="linear",
|
||||
// align_corners=False) in f32): the 127-row global tables give 79 rows at a 40 grid, and every
|
||||
// other geometry the tower declares already has exactly the rows it wants.
|
||||
//
|
||||
// Exactness. f32 throughout, one bf16 rounding of the output, exp2 (the hardware transcendental)
|
||||
// with the log2(e) factor; the selftest holds it to the house tolerance against a host port of the
|
||||
@@ -35,12 +36,41 @@ namespace {
|
||||
constexpr uint32_t kThreads = 128;
|
||||
constexpr float kLog2e = 1.4426950408889634f;
|
||||
|
||||
/* q . row(d) of a rel_pos table as the reference resized it to `want` rows: linear interpolation
|
||||
* between the two source rows at align_corners=False's coordinate, clamped at the edges. With
|
||||
* rows == want the source is row d itself (f == 0), so the direct read is unchanged bit for bit. */
|
||||
template <int HD>
|
||||
__device__ inline float tab_dot(const uint16_t* __restrict__ tab, int rows, int want, int d,
|
||||
const float* qf) {
|
||||
if (rows == want) {
|
||||
const uint16_t* rp = tab + (size_t)d * HD;
|
||||
float s = 0.0f;
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HD; ++c) s += qf[c] * bf16_to_f32(rp[c]);
|
||||
return s;
|
||||
}
|
||||
double src = ((double)d + 0.5) * (double)rows / (double)want - 0.5;
|
||||
if (src < 0.0) src = 0.0;
|
||||
if (src > (double)(rows - 1)) src = (double)(rows - 1);
|
||||
const int i0 = (int)src, i1 = i0 + 1 < rows ? i0 + 1 : i0;
|
||||
const float f = (float)(src - (double)i0);
|
||||
const uint16_t* a = tab + (size_t)i0 * HD;
|
||||
const uint16_t* b = tab + (size_t)i1 * HD;
|
||||
float s = 0.0f;
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HD; ++c) {
|
||||
const float av = bf16_to_f32(a[c]);
|
||||
s += qf[c] * (av + (bf16_to_f32(b[c]) - av) * f);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
template <int HD>
|
||||
__global__ __launch_bounds__(kThreads) void sam_attn_kernel(
|
||||
const uint16_t* __restrict__ gq, const uint16_t* __restrict__ gk,
|
||||
const uint16_t* __restrict__ gv, uint16_t* __restrict__ go,
|
||||
const int* __restrict__ cu, const uint16_t* __restrict__ rh,
|
||||
const uint16_t* __restrict__ rw,
|
||||
const uint16_t* __restrict__ rw, int rh_rows, int rw_rows,
|
||||
int q_ld, int k_ld, int v_ld, int o_ld, int nhead, int gh, int gw, int th, int tw,
|
||||
int nth, int ntw, float scale) {
|
||||
// blockIdx.x = ((seq * nth + ty) * ntw + tx) * nhead + head: heads adjacent, so the tile's k/v
|
||||
@@ -72,18 +102,12 @@ __global__ __launch_bounds__(kThreads) void sam_attn_kernel(
|
||||
float m = -INFINITY, l = 0.0f;
|
||||
|
||||
for (int jh = 0; jh < th; ++jh) {
|
||||
const uint16_t* hrp = rh + (size_t)(lh - jh + th - 1) * HD;
|
||||
float hr = 0.0f;
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HD; ++c) hr += qf[c] * bf16_to_f32(hrp[c]);
|
||||
const float hr = tab_dot<HD>(rh, rh_rows, 2 * th - 1, lh - jh + th - 1, qf);
|
||||
|
||||
const int kh = ty * th + jh;
|
||||
const bool hreal = kh < gh;
|
||||
for (int jw = 0; jw < tw; ++jw) {
|
||||
const uint16_t* wrp = rw + (size_t)(lw - jw + tw - 1) * HD;
|
||||
float s = hr;
|
||||
#pragma unroll
|
||||
for (int c = 0; c < HD; ++c) s += qf[c] * bf16_to_f32(wrp[c]);
|
||||
float s = hr + tab_dot<HD>(rw, rw_rows, 2 * tw - 1, lw - jw + tw - 1, qf);
|
||||
|
||||
const int kw = tx * tw + jw;
|
||||
const bool real = hreal && kw < gw;
|
||||
@@ -124,6 +148,7 @@ __global__ __launch_bounds__(kThreads) void sam_attn_kernel(
|
||||
|
||||
int launch_sam_attn(const uint16_t* q, const uint16_t* k, const uint16_t* v, uint16_t* o,
|
||||
const int* cu, const uint16_t* rh, const uint16_t* rw,
|
||||
int rh_rows, int rw_rows,
|
||||
int q_ld, int k_ld, int v_ld, int o_ld, int nseq, int nhead, int gh, int gw,
|
||||
int win, float scale, long stream) {
|
||||
const int th = win > 0 ? win : gh, tw = win > 0 ? win : gw;
|
||||
@@ -131,8 +156,8 @@ int launch_sam_attn(const uint16_t* q, const uint16_t* k, const uint16_t* v, uin
|
||||
const size_t blocks = (size_t)nseq * nth * ntw * nhead;
|
||||
if (blocks == 0 || blocks > 0x7fffffff) return R4D_NO(RAD_E_SHAPE);
|
||||
hipLaunchKernelGGL((sam_attn_kernel<64>), dim3((uint32_t)blocks), dim3(kThreads), 0,
|
||||
(hipStream_t)stream, q, k, v, o, cu, rh, rw, q_ld, k_ld, v_ld, o_ld, nhead,
|
||||
gh, gw, th, tw, nth, ntw, scale);
|
||||
(hipStream_t)stream, q, k, v, o, cu, rh, rw, rh_rows, rw_rows,
|
||||
q_ld, k_ld, v_ld, o_ld, nhead, gh, gw, th, tw, nth, ntw, scale);
|
||||
return hipGetLastError() == hipSuccess ? RAD_OK : R4D_NO(RAD_E_DEVICE);
|
||||
}
|
||||
|
||||
@@ -188,19 +213,20 @@ extern "C" int r4d_rad_sam_attn(const RadArgs* a, RadStream s) {
|
||||
const long long nseq = r4d_numel(cu) - 1;
|
||||
if (nseq < 1) return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
// The tables: [2*tile-1, head_dim] each, the tile being the window or the whole grid.
|
||||
const long long th = win > 0 ? win : gh, tw = win > 0 ? win : gw;
|
||||
// The tables: [2*tile-1, head_dim] when the geometry has its own, else the checkpoint's rows
|
||||
// (the 127-row global table at a 40 grid), resampled on read.
|
||||
int64_t rhrows = 0, rhld = 0, rwrows = 0, rwld = 0;
|
||||
R4D_NEED(bf16_rows(rh, hd, &rhrows, &rhld));
|
||||
R4D_NEED(bf16_rows(rw, hd, &rwrows, &rwld));
|
||||
if (rhrows != 2 * th - 1 || rwrows != 2 * tw - 1) return R4D_NO(RAD_E_UNSUPPORTED);
|
||||
if (rhrows < 1 || rwrows < 1) return R4D_NO(RAD_E_UNSUPPORTED);
|
||||
|
||||
const float scale = (float)r4d_getf(a, "scale", 1.0 / std::sqrt((double)hd));
|
||||
|
||||
return launch_sam_attn((const uint16_t*)r4d_addr(q), (const uint16_t*)r4d_addr(k),
|
||||
(const uint16_t*)r4d_addr(v), (uint16_t*)r4d_addr(o),
|
||||
(const int*)r4d_addr(cu), (const uint16_t*)r4d_addr(rh),
|
||||
(const uint16_t*)r4d_addr(rw), (int)qld, (int)kld, (int)vld, (int)old,
|
||||
(const uint16_t*)r4d_addr(rw), (int)rhrows, (int)rwrows,
|
||||
(int)qld, (int)kld, (int)vld, (int)old,
|
||||
(int)nseq, (int)nh, (int)gh, (int)gw, (int)win, scale,
|
||||
(long)(uintptr_t)s);
|
||||
}
|
||||
@@ -28,6 +28,11 @@
|
||||
// less `vocab_off`, and a token outside the shard is another rank's to mask (it is not a column
|
||||
// here at all).
|
||||
//
|
||||
// The optional `whitelist` plane is the reference's whitelist (modeling_unlimitedocr.py:359,379):
|
||||
// word w bit b of the row's own plane row covers column 32w + b, and a SET bit removes that token
|
||||
// from the banned set -- it is never written. Absent, nothing is exempt, and the kernel's answer
|
||||
// is the one before the whitelist existed.
|
||||
//
|
||||
// The scan is O(w * n) a row over a history of at most max_hist tokens, so one block a row with
|
||||
// the positions dealt out across the block's threads; the n-1-token prefix is staged in LDS once
|
||||
// when it fits (n <= 256), which is every request the model card asks for (35).
|
||||
@@ -53,7 +58,8 @@ using SP = RadSampleParams;
|
||||
|
||||
__global__ __launch_bounds__(kThreads) void ngram_ban_kernel(
|
||||
float* __restrict__ lg, const SP* __restrict__ sp, const int32_t* __restrict__ hist,
|
||||
const int32_t* __restrict__ ng, uint32_t n_vocab, uint32_t lg_ld, uint32_t hmax, int32_t voff) {
|
||||
const int32_t* __restrict__ ng, const uint32_t* __restrict__ wl, uint32_t wl_words,
|
||||
uint32_t n_vocab, uint32_t lg_ld, uint32_t hmax, int32_t voff) {
|
||||
__shared__ int32_t pf[kMaxPrefix];
|
||||
__shared__ int64_t s_start, s_end, s_plen, s_p0;
|
||||
|
||||
@@ -96,6 +102,9 @@ __global__ __launch_bounds__(kThreads) void ngram_ban_kernel(
|
||||
|
||||
const int32_t* __restrict__ h = hist + (size_t)m * hmax + ho;
|
||||
float* __restrict__ row = lg + (size_t)m * lg_ld;
|
||||
// The row's own whitelist words (a SET bit exempts the column, the reference's
|
||||
// `banned.difference_update(self.whitelist)`), read at the position loop below.
|
||||
const uint32_t* __restrict__ w = wl ? wl + (size_t)m * wl_words : nullptr;
|
||||
|
||||
// The prefix: LDS when it fits, otherwise each position reads it from the plane.
|
||||
const bool in_lds = plen <= (int64_t)kMaxPrefix;
|
||||
@@ -116,18 +125,21 @@ __global__ __launch_bounds__(kThreads) void ngram_ban_kernel(
|
||||
const int32_t tok = h[idx + plen];
|
||||
const int64_t col = (int64_t)tok - voff;
|
||||
if (tok < 0 || col < 0 || col >= (int64_t)n_vocab) continue;
|
||||
if (wl && ((w[col >> 5] >> (uint32_t)(col & 31)) & 1u) != 0u) continue;
|
||||
// Two positions may ban one token; the store is idempotent and the value is constant.
|
||||
row[col] = -INFINITY;
|
||||
}
|
||||
}
|
||||
|
||||
int r4d_sample_ngram_ban_f32(long lg, long params, long hist, long ng, int M, int n_vocab,
|
||||
int hmax, int lg_ld, int vocab_off, long stream) {
|
||||
int r4d_sample_ngram_ban_f32(long lg, long params, long hist, long ng, long wl, int wl_words,
|
||||
int M, int n_vocab, int hmax, int lg_ld, int vocab_off, long stream) {
|
||||
if (!lg || !params || !hist || !ng) return R4D_NO(RAD_E_INVAL);
|
||||
if (M <= 0 || n_vocab <= 0 || hmax <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
if (wl && wl_words <= 0) return R4D_NO(RAD_E_SHAPE);
|
||||
if (!lg_ld) lg_ld = n_vocab;
|
||||
hipLaunchKernelGGL(ngram_ban_kernel, dim3((uint32_t)M), dim3(kThreads), 0, (hipStream_t)stream,
|
||||
(float*)lg, (const SP*)params, (const int32_t*)hist, (const int32_t*)ng,
|
||||
(const uint32_t*)wl, (uint32_t)(wl ? wl_words : 0),
|
||||
(uint32_t)n_vocab, (uint32_t)lg_ld, (uint32_t)hmax, (int32_t)vocab_off);
|
||||
return hipGetLastError() == hipSuccess ? RAD_OK : R4D_NO(RAD_E_DEVICE);
|
||||
}
|
||||
@@ -159,7 +171,7 @@ static int params_ok(const RadTensor* t, long long M) {
|
||||
}
|
||||
|
||||
extern "C" int r4d_rad_sample_ngram_ban(const RadArgs* a, RadStream s) {
|
||||
enum { V_LOGITS = 0, V_PARAMS, V_HIST, V_NGRAM };
|
||||
enum { V_LOGITS = 0, V_PARAMS, V_HIST, V_NGRAM, V_WL };
|
||||
const RadTensor* lg = r4d_opt(a, V_LOGITS);
|
||||
if (!lg) return R4D_NO(RAD_E_INVAL);
|
||||
R4D_NEED(dt_f32(lg));
|
||||
@@ -183,8 +195,20 @@ extern "C" int r4d_rad_sample_ngram_ban(const RadArgs* a, RadStream s) {
|
||||
/* One {n, window} pair a row, at least M of them. */
|
||||
if (r4d_numel(ng) < 2 * M) return R4D_NO(RAD_E_SHAPE);
|
||||
|
||||
/* The optional whitelist bitmask: contiguous words of 32 columns, M rows of them. */
|
||||
const RadTensor* wl = r4d_opt(a, V_WL);
|
||||
const long long wwords = (nv + 31) / 32;
|
||||
long long wl_addr = 0;
|
||||
if (wl) {
|
||||
R4D_NEED(dt_i32(wl));
|
||||
if (!r4d_contig(wl)) return R4D_NO(RAD_E_STRIDE);
|
||||
if (r4d_numel(wl) < M * wwords) return R4D_NO(RAD_E_SHAPE);
|
||||
wl_addr = r4d_addr(wl);
|
||||
}
|
||||
|
||||
const long long voff = r4d_geti_or(a, "vocab_off", 0);
|
||||
return r4d_sample_ngram_ban_f32(r4d_addr(lg), r4d_addr(pr), r4d_addr(h), r4d_addr(ng),
|
||||
wl_addr, (int)wwords,
|
||||
(int)M, (int)nv, (int)hmax, (int)ld, (int)voff,
|
||||
(long)(uintptr_t)s);
|
||||
}
|
||||
+57
-11
@@ -18868,8 +18868,10 @@ static void sample_ngram_ban_case(const Plugin& pl) {
|
||||
}
|
||||
|
||||
/* Two shard configurations: the whole vocabulary, and this rank holding ids [16, 64). */
|
||||
struct Shard { int64_t voff, nv; };
|
||||
const Shard shards[2] = { { 0, V }, { 16, V - 16 } };
|
||||
struct Shard { int64_t voff, nv; bool wl; };
|
||||
const Shard shards[4] = { { 0, V, false }, { 16, V - 16, false }, { 0, V, true },
|
||||
{ 16, V - 16, true } };
|
||||
/* The whitelist pass un-bans ids 7 and 21 (the reference bans.difference_update(whitelist)). */
|
||||
|
||||
int64_t n_cases = 0, banned_total = 0, mismatches = 0;
|
||||
std::string first;
|
||||
@@ -18904,10 +18906,23 @@ static void sample_ngram_ban_case(const Plugin& pl) {
|
||||
st_memcpy(d_h, hist.data(), (size_t)(M * H) * 4, hipMemcpyHostToDevice);
|
||||
st_memcpy(d_ng, ng.data(), (size_t)(M * 2) * 4, hipMemcpyHostToDevice);
|
||||
|
||||
const int64_t wwords = (nv + 31) / 32;
|
||||
std::vector<uint32_t> wl((size_t)(M * wwords), 0u);
|
||||
for (int64_t m = 0; m < M && sh.wl; ++m)
|
||||
for (int32_t id : { 7, 21 }) {
|
||||
const int64_t col = (int64_t)id - sh.voff;
|
||||
if (col >= 0 && col < nv) wl[(size_t)(m * wwords + col / 32)] |= 1u << (col % 32);
|
||||
}
|
||||
void* d_wl = nullptr;
|
||||
if (sh.wl) {
|
||||
d_wl = dalloc((size_t)(M * wwords) * 4);
|
||||
st_memcpy(d_wl, wl.data(), (size_t)(M * wwords) * 4, hipMemcpyHostToDevice);
|
||||
}
|
||||
std::vector<RadTensor> ts = { T(d_lg, RAD_F32, { M, nv }),
|
||||
T(d_pr, RAD_F32, { M, (int64_t)(sizeof(RadSampleParams) / 4) }),
|
||||
T(d_h, RAD_I32, { M, H }),
|
||||
T(d_ng, RAD_I32, { M, 2 }) };
|
||||
if (sh.wl) ts.push_back(T(d_wl, RAD_U32, { M, wwords }));
|
||||
std::vector<RadParam> ps = { PI("M", M), PI("n_vocab", nv), PI("vocab_off", sh.voff) };
|
||||
RadArgs a;
|
||||
std::memset(&a, 0, sizeof(a));
|
||||
@@ -18927,7 +18942,11 @@ static void sample_ngram_ban_case(const Plugin& pl) {
|
||||
const int64_t g = sh.voff + i;
|
||||
const size_t k = (size_t)(m * nv + i);
|
||||
++n_cases;
|
||||
const bool want = (size_t)g < banned.size() && banned[(size_t)g] != 0;
|
||||
const int64_t wcol = g - sh.voff;
|
||||
const bool wled = sh.wl && wcol >= 0 && wcol < nv &&
|
||||
((wl[(size_t)(m * wwords + wcol / 32)] >>
|
||||
(wcol % 32)) & 1u);
|
||||
const bool want = !wled && (size_t)g < banned.size() && banned[(size_t)g] != 0;
|
||||
if (want) ++banned_total;
|
||||
const float gv = got[k];
|
||||
const bool is_inf = std::isinf(gv) && gv < 0.0f;
|
||||
@@ -18945,7 +18964,7 @@ static void sample_ngram_ban_case(const Plugin& pl) {
|
||||
* [16, 64). dalloc's buffers are released by the case's dfree_all. */
|
||||
}
|
||||
|
||||
std::printf(" ngram_ban: %lld columns over %lld rows x 2 shards, %lld banned, "
|
||||
std::printf(" ngram_ban: %lld columns over %lld rows x 4 configs (2 shards + 2 whitelist), %lld banned, "
|
||||
"%lld mismatches\n", (long long)n_cases, (long long)M, (long long)banned_total,
|
||||
(long long)mismatches);
|
||||
ok(what, mismatches == 0 && banned_total > 0,
|
||||
@@ -18964,14 +18983,18 @@ static void sam_attn_case(const Plugin& pl) {
|
||||
const RadKernelInfo* r = find(pl, "sam_attn_bf16");
|
||||
if (!r) { ok(what, false, "row absent"); return; }
|
||||
|
||||
struct Geo { int64_t gh, gw, win, nh, hd, nseq; };
|
||||
const Geo geos[2] = { { 16, 16, 14, 2, 64, 2 }, { 16, 16, 0, 2, 64, 1 } };
|
||||
/* tab_rows 0 means "2*tile-1, no resize". The third geometry is the production 40-grid global
|
||||
* block: the checkpoint's 127-row tables serve the 79 rows that block wants. */
|
||||
struct Geo { int64_t gh, gw, win, nh, hd, nseq, tab_rows; };
|
||||
const Geo geos[3] = { { 16, 16, 14, 2, 64, 2, 0 }, { 16, 16, 0, 2, 64, 1, 0 },
|
||||
{ 40, 40, 0, 2, 64, 1, 127 } };
|
||||
int64_t checked = 0, bad_total = 0;
|
||||
float worst = 0.0f;
|
||||
|
||||
for (const Geo& g : geos) {
|
||||
const int64_t W = g.nh * g.hd, R = g.gh * g.gw, M = g.nseq * R;
|
||||
const int64_t tab = 2 * (g.win > 0 ? g.win : (g.gh > g.gw ? g.gh : g.gw)) - 1;
|
||||
const int64_t want = 2 * (g.win > 0 ? g.win : (g.gh > g.gw ? g.gh : g.gw)) - 1;
|
||||
const int64_t tab = g.tab_rows > 0 ? g.tab_rows : want;
|
||||
/* One filler column, so the row pitch is W + 1: q/k/v/out are all slices. */
|
||||
const int64_t ld = W + 1;
|
||||
const float scale = 1.0f / std::sqrt((float)g.hd);
|
||||
@@ -18987,6 +19010,29 @@ static void sam_attn_case(const Plugin& pl) {
|
||||
rh[i] = h_to_bf16(0.1f * h_bf16(st_rand_bf16((uint32_t)i + 0x1234567u)));
|
||||
rw[i] = h_to_bf16(0.1f * h_bf16(st_rand_bf16((uint32_t)i + 0x7654321u)));
|
||||
}
|
||||
/* The oracle's tables, resampled exactly as libref's get_rel_pos does (f32, align_corners
|
||||
* False, clamp at the edges); the kernel gets the raw rows. */
|
||||
auto resized = [&](const std::vector<uint16_t>& raw) {
|
||||
std::vector<float> out((size_t)(want * g.hd));
|
||||
if (tab == want) {
|
||||
for (size_t i = 0; i < out.size(); ++i) out[i] = h_bf16(raw[i]);
|
||||
return out;
|
||||
}
|
||||
for (int64_t d = 0; d < want; ++d) {
|
||||
double src = ((double)d + 0.5) * (double)tab / (double)want - 0.5;
|
||||
if (src < 0.0) src = 0.0;
|
||||
if (src > (double)(tab - 1)) src = (double)(tab - 1);
|
||||
const int64_t i0 = (int64_t)src, i1 = i0 + 1 < tab ? i0 + 1 : i0;
|
||||
const float f = (float)(src - (double)i0);
|
||||
for (int64_t c = 0; c < g.hd; ++c) {
|
||||
const float a = h_bf16(raw[(size_t)(i0 * g.hd + c)]);
|
||||
out[(size_t)(d * g.hd + c)] =
|
||||
a + (h_bf16(raw[(size_t)(i1 * g.hd + c)]) - a) * f;
|
||||
}
|
||||
}
|
||||
return out;
|
||||
};
|
||||
const std::vector<float> rhf = resized(rh), rwf = resized(rw);
|
||||
std::vector<int32_t> cu((size_t)g.nseq + 1);
|
||||
for (int64_t s = 0; s <= g.nseq; ++s) cu[(size_t)s] = (int32_t)(s * R);
|
||||
|
||||
@@ -19036,13 +19082,13 @@ static void sam_attn_case(const Plugin& pl) {
|
||||
for (int64_t c = 0; c < g.hd; ++c)
|
||||
qf[c] = h_bf16(q[(size_t)(row * ld + h * g.hd + c)]);
|
||||
for (int64_t jh = 0; jh < th; ++jh) {
|
||||
const uint16_t* hrp = &rh[(size_t)((lh - jh + th - 1) * g.hd)];
|
||||
const float* hrp = &rhf[(size_t)((lh - jh + th - 1) * g.hd)];
|
||||
double hr = 0.0;
|
||||
for (int64_t c = 0; c < g.hd; ++c) hr += qf[c] * h_bf16(hrp[c]);
|
||||
for (int64_t c = 0; c < g.hd; ++c) hr += qf[c] * hrp[c];
|
||||
for (int64_t jw = 0; jw < tw; ++jw) {
|
||||
const uint16_t* wrp = &rw[(size_t)((lw - jw + tw - 1) * g.hd)];
|
||||
const float* wrp = &rwf[(size_t)((lw - jw + tw - 1) * g.hd)];
|
||||
double sc = hr;
|
||||
for (int64_t c = 0; c < g.hd; ++c) sc += qf[c] * h_bf16(wrp[c]);
|
||||
for (int64_t c = 0; c < g.hd; ++c) sc += qf[c] * wrp[c];
|
||||
const int64_t kh = ty * th + jh, kw = tx * tw + jw;
|
||||
if (kh < g.gh && kw < g.gw) {
|
||||
const int64_t krow = cu[(size_t)s] + kh * g.gw + kw;
|
||||
|
||||
Reference in new issue
Block a user