qsa_select: signed -- a ranking for indexers whose scores can be negative

qsa_select ranks max(score, 0) and treats a score at or below -1e30 as dead: QSA's block scores
are sums of relus, so the clamp changes nothing there. GLM-5.3's DSA indexer weights its heads by
signed gates, and its block scores can be negative; ranked through the clamp, every negative block
ties at zero and the selection is wrong. `signed` 1 ranks finite scores as they are (-0 tying +0)
and treats nonfinite ones as dead; absent or 0 is the ranking as it was. libref, libavx and libr4d
take it together, since the three declare one schema. docs/OPS.md; tests/r4d_emu/test_qsa.cpp
runs both rankings against the reference.
This commit is contained in:
John McDonough authored and StillDeadcode committed 2026-10-19 20:13:47 +02:00
1 parent 9e0e28315d
commit c8311068d0
8 files changed
+197 -69

No files matched your search

+9 -1
View File
@@ -2038,7 +2038,7 @@ order, not a slow one.
|---|---|---| |---|---|---|
| `qsa_block_key` | `M`(range) `n` `ratio` `rotary_dim` `pos0`/opt `block_size`/opt `eps`:f64 `theta`:f64 `scale`:f64/opt `wadd`:f64/opt `dtype` `mode`/opt `sections`/opt `yarn_factor`:f64/opt `yarn_low`:f64/opt `yarn_high`:f64/opt `yarn_magnitude`:f64/opt | `k`, `w`w, `page`?, `pos`? → `out` | | `qsa_block_key` | `M`(range) `n` `ratio` `rotary_dim` `pos0`/opt `block_size`/opt `eps`:f64 `theta`:f64 `scale`:f64/opt `wadd`:f64/opt `dtype` `mode`/opt `sections`/opt `yarn_factor`:f64/opt `yarn_low`:f64/opt `yarn_high`:f64/opt `yarn_magnitude`:f64/opt | `k`, `w`w, `page`?, `pos`? → `out` |
| `qsa_score` | `M`(range) `n` `heads` `blocks` `seqs` `dtype` | `q`, `bk`, `bt`, `cu`, `nc` → `score` | | `qsa_score` | `M`(range) `n` `heads` `blocks` `seqs` `dtype` | `q`, `bk`, `bt`, `cu`, `nc` → `score` |
| `qsa_select` | `M`(range) `blocks` `topk` `ratio` `seqs` | `score`, `bt`, `nc`, `pos`, `cu` → `sel`, `nsel`, `seqused` | | `qsa_select` | `M`(range) `blocks` `topk` `ratio` `seqs` `signed`/opt | `score`, `bt`, `nc`, `pos`, `cu` → `sel`, `nsel`, `seqused` |
| `qsa_tail_store` | `M`(range) `n` `ratio` `ring` `seqs` `dtype` | `k`, `pos`, `cu`, `state`:inout, `sidx` | | `qsa_tail_store` | `M`(range) `n` `ratio` `ring` `seqs` `dtype` | `k`, `pos`, `cu`, `state`:inout, `sidx` |
| `qsa_work` | `M`(range) `n` `ratio` `ring` `seqs` `work` `dtype` | `k`, `pos`, `cu`, `state`, `sidx`, `bt`, `rope`? → `stage`, `page`, `bpos`, `nc` | | `qsa_work` | `M`(range) `n` `ratio` `ring` `seqs` `work` `dtype` | `k`, `pos`, `cu`, `state`, `sidx`, `bt`, `rope`? → `stage`, `page`, `bpos`, `nc` |
@@ -2151,6 +2151,14 @@ with the partial page appended.
- **Ties break to the lower block index, through a prefix count.** A block score is a sum of relus - **Ties break to the lower block index, through a prefix count.** A block score is a sum of relus
over a shared grid, so equal scores are ordinary, and a top-k placed by whichever atomic landed over a shared grid, so equal scores are ordinary, and a top-k placed by whichever atomic landed
first would not reproduce across runs; the tree checks generated text byte for byte. first would not reproduce across runs; the tree checks generated text byte for byte.
- **`signed` chooses the ranking.** Absent or `0`, a block is live when its page id is `>= 0` and
its score `> -1e30`, and ranks by `max(score, 0)`: QSA's scores are sums of relus, so the clamp
changes no ranking there. `1` is for indexers whose scores can be negative (GLM's DSA weights
its heads by signed gates): a block is live when its page id is `>= 0` and its score finite, and
ranks by the score itself, `-0` tying `+0`. The two rankings pick different sets whenever a live
score is negative, so an architecture states the one its indexer needs. libr4d's
`qsa_select_bf16` serves both; the signed ranking keys a score by its float's total order over
all 32 bits.
- **`seqused` is why QSA needs no new attention kernel.** `attn_paged` takes its block table and - **`seqused` is why QSA needs no new attention kernel.** `attn_paged` takes its block table and
per-sequence length as ordinary operands, so the sparse attention is that op issued with `sel` in per-sequence length as ordinary operands, so the sparse attention is that op issued with `sel` in
place of the sequence's table and this number in place of its length. The KV it walks is `nsel` place of the sequence's table and this number in place of its length. The KV it walks is `nsel`
+45 -22
View File
@@ -12,9 +12,10 @@
* The keys are read once per tile, which at decode is once per sequence. The dots * The keys are read once per tile, which at decode is once per sequence. The dots
* sum in vector order (libref's order at the scalar level); the heads in order. * sum in vector order (libref's order at the scalar level); the heads in order.
* qsa_select Linear time per row: an order key per block (the bits of max(score, 0) plus one, * qsa_select Linear time per row: an order key per block (the bits of max(score, 0) plus one,
* 0 for a dead block), a histogram of the keys' top bits, an exact select among the * 0 for a dead block; with `signed` the float's total order over all 32 bits), a
* keys in the bin that holds the topk-th largest, and one emitting pass in block * histogram of the keys' top bits, an exact select among the keys in the bin that
* order. The ids match libref's byte for byte. * holds the topk-th largest, and one emitting pass in block order. The ids match
* libref's byte for byte.
* qsa_tail_store, qsa_work Row copies through f32, as libref copies, so every value lands * qsa_tail_store, qsa_work Row copies through f32, as libref copies, so every value lands
* with the bits libref's would. * with the bits libref's would.
* dflash_conv Channels across lanes, in blocks of columns that walk their rows, so a block's base * dflash_conv Channels across lanes, in blocks of columns that walk their rows, so a block's base
@@ -290,25 +291,29 @@ AVX_KERNEL(qsa_score) {
/* ================================================================== qsa_select */ /* ================================================================== qsa_select */
namespace { namespace {
/* Bins of the first selection pass: the top 10 bits of a key, which is below 2^31. */ /* Bins of the first selection pass: the top 10 bits of a key, which is below 2^31 (a signed
enum { kBins = 1024, kBinShift = 21 }; * key uses all 32 bits, so its bins sit one bit higher). */
enum { kBins = 1024, kBinShift = 21, kBinShiftSigned = 22 };
struct SelJob { struct SelJob {
const RadTensor *sc, *bt, *nc, *po, *se, *ns, *su; const RadTensor *sc, *bt, *nc, *po, *se, *ns, *su;
const int64_t* seq; const int64_t* seq;
int64_t nb, topk, ratio; int64_t nb, topk, ratio;
int64_t s_rs, s_cs, bt_rs, bt_cs, o_rs, o_cs; int64_t s_rs, s_cs, bt_rs, bt_cs, o_rs, o_cs;
bool sgn; /* the `signed` order */
}; };
/* The order key of each of the row's first `n` blocks: bits(max(score, 0)) + 1 for a live block /* The order key of each of the row's first `n` blocks: bits(max(score, 0)) + 1 for a live block
* (page id >= 0 and score > -1e30), 0 for a dead one. Non-negative floats order as their bits, so * (page id >= 0 and score > -1e30), 0 for a dead one. Non-negative floats order as their bits, so
* keys order as libref's values; max(score, 0) maps -0 to +0 as libref's `v > 0 ? v : 0` does. * keys order as libref's values; max(score, 0) maps -0 to +0 as libref's `v > 0 ? v : 0` does.
* Returns the live count. */ * The signed order makes a block with a finite score live and keys it by the float's total order
* (sign bit flipped for a positive value, every bit for a negative one), -0 keyed as +0, so keys
* order as libref's signed values; no finite score keys to 0. Returns the live count. */
static int64_t order_keys(const SelJob& j, int64_t m, int64_t s, int64_t n, uint32_t* key) { static int64_t order_keys(const SelJob& j, int64_t m, int64_t s, int64_t n, uint32_t* key) {
const int64_t sb = m * j.s_rs, bb = s * j.bt_rs; const int64_t sb = m * j.s_rs, bb = s * j.bt_rs;
int64_t live = 0, b = 0; int64_t live = 0, b = 0;
#if AVX_LEVEL >= 2 #if AVX_LEVEL >= 2
if (j.sc->dtype == RAD_F32 && j.s_cs == 1 && j.bt->dtype == RAD_I32 && j.bt_cs == 1) { if (!j.sgn && j.sc->dtype == RAD_F32 && j.s_cs == 1 && j.bt->dtype == RAD_I32 && j.bt_cs == 1) {
const float* v = (const float*)j.sc->data + sb; const float* v = (const float*)j.sc->data + sb;
const int32_t* p = (const int32_t*)j.bt->data + bb; const int32_t* p = (const int32_t*)j.bt->data + bb;
#if AVX_LEVEL == 3 #if AVX_LEVEL == 3
@@ -341,6 +346,17 @@ static int64_t order_keys(const SelJob& j, int64_t m, int64_t s, int64_t n, uint
#endif #endif
} }
#endif #endif
if (j.sgn) {
for (; b < n; ++b) {
const float v = ldt(j.sc, sb + b * j.s_cs);
if (ldi(j.bt, bb + b * j.bt_cs) < 0 || !std::isfinite(v)) { key[b] = 0; continue; }
uint32_t u = 0;
if (v != 0.0f) std::memcpy(&u, &v, 4);
key[b] = (u & 0x80000000u) ? ~u : (u ^ 0x80000000u);
++live;
}
return live;
}
for (; b < n; ++b) { for (; b < n; ++b) {
const float v = ldt(j.sc, sb + b * j.s_cs); const float v = ldt(j.sc, sb + b * j.s_cs);
if (ldi(j.bt, bb + b * j.bt_cs) < 0 || !(v > -1e30f)) { key[b] = 0; continue; } if (ldi(j.bt, bb + b * j.bt_cs) < 0 || !(v > -1e30f)) { key[b] = 0; continue; }
@@ -355,19 +371,21 @@ static int64_t order_keys(const SelJob& j, int64_t m, int64_t s, int64_t n, uint
/* The topk-th largest key of a row with more than `topk` live keys, and how many keys are above /* The topk-th largest key of a row with more than `topk` live keys, and how many keys are above
* it. A histogram of the top bits finds the bin holding it; the keys in that bin are gathered and * it. A histogram of the top bits finds the bin holding it; the keys in that bin are gathered and
* the exact value taken among them. `cand` holds up to n keys. */ * the exact value taken among them. `cand` holds up to n keys. `sgn`: the keys are signed-order
static uint32_t threshold(const uint32_t* key, int64_t n, int64_t topk, uint32_t* hist, * keys, which use all 32 bits. */
static uint32_t threshold(const uint32_t* key, int64_t n, int64_t topk, bool sgn, uint32_t* hist,
uint32_t* cand, int64_t* above_out) { uint32_t* cand, int64_t* above_out) {
const int shift = sgn ? (int)kBinShiftSigned : (int)kBinShift;
/* Four interleaved histograms, so runs of keys in one bin do not serialise on one counter. */ /* Four interleaved histograms, so runs of keys in one bin do not serialise on one counter. */
std::memset(hist, 0, 4 * kBins * sizeof(uint32_t)); std::memset(hist, 0, 4 * kBins * sizeof(uint32_t));
int64_t b = 0; int64_t b = 0;
for (; b + 4 <= n; b += 4) { for (; b + 4 <= n; b += 4) {
++hist[0 * kBins + (key[b] >> kBinShift)]; ++hist[0 * kBins + (key[b] >> shift)];
++hist[1 * kBins + (key[b + 1] >> kBinShift)]; ++hist[1 * kBins + (key[b + 1] >> shift)];
++hist[2 * kBins + (key[b + 2] >> kBinShift)]; ++hist[2 * kBins + (key[b + 2] >> shift)];
++hist[3 * kBins + (key[b + 3] >> kBinShift)]; ++hist[3 * kBins + (key[b + 3] >> shift)];
} }
for (; b < n; ++b) ++hist[key[b] >> kBinShift]; for (; b < n; ++b) ++hist[key[b] >> shift];
for (int i = 0; i < kBins; ++i) for (int i = 0; i < kBins; ++i)
hist[i] += hist[kBins + i] + hist[2 * kBins + i] + hist[3 * kBins + i]; hist[i] += hist[kBins + i] + hist[2 * kBins + i] + hist[3 * kBins + i];
/* Bin 0 is never counted: it is the last one left, and holds the dead keys (0) beside the /* Bin 0 is never counted: it is the last one left, and holds the dead keys (0) beside the
@@ -380,8 +398,8 @@ static uint32_t threshold(const uint32_t* key, int64_t n, int64_t topk, uint32_t
} }
/* The keys of the bin, i.e. in [lo, hi], with key 0 (dead) left out of bin 0. Few keys fall in /* The keys of the bin, i.e. in [lo, hi], with key 0 (dead) left out of bin 0. Few keys fall in
* one bin, so the vector forms test a vector and walk only its hits. */ * one bin, so the vector forms test a vector and walk only its hits. */
const uint32_t lo = std::max<uint32_t>((uint32_t)bin << kBinShift, 1u); const uint32_t lo = std::max<uint32_t>((uint32_t)bin << shift, 1u);
const uint32_t hi = (((uint32_t)bin + 1u) << kBinShift) - 1u; const uint32_t hi = (((uint32_t)bin + 1u) << shift) - 1u;
int64_t nc = 0; int64_t nc = 0;
b = 0; b = 0;
#if AVX_LEVEL == 3 #if AVX_LEVEL == 3
@@ -393,9 +411,10 @@ static uint32_t threshold(const uint32_t* key, int64_t n, int64_t topk, uint32_t
nc += __builtin_popcount((unsigned)in); nc += __builtin_popcount((unsigned)in);
} }
#elif AVX_LEVEL == 2 #elif AVX_LEVEL == 2
/* Keys are below 2^31, so the signed compares order them. */ /* Keys are below 2^31, so the signed compares order them; signed-order keys take the scalar
* loop. */
const __m256i vlo = _mm256_set1_epi32((int)lo - 1), vhi = _mm256_set1_epi32((int)hi + 1); const __m256i vlo = _mm256_set1_epi32((int)lo - 1), vhi = _mm256_set1_epi32((int)hi + 1);
for (; b + 8 <= n; b += 8) { for (; !sgn && b + 8 <= n; b += 8) {
const __m256i k = _mm256_loadu_si256((const __m256i*)(key + b)); const __m256i k = _mm256_loadu_si256((const __m256i*)(key + b));
const __m256i in = _mm256_and_si256(_mm256_cmpgt_epi32(k, vlo), _mm256_cmpgt_epi32(vhi, k)); const __m256i in = _mm256_and_si256(_mm256_cmpgt_epi32(k, vlo), _mm256_cmpgt_epi32(vhi, k));
unsigned hit = (unsigned)_mm256_movemask_ps(_mm256_castsi256_ps(in)); unsigned hit = (unsigned)_mm256_movemask_ps(_mm256_castsi256_ps(in));
@@ -439,9 +458,10 @@ static void emit(const SelJob& j, const uint32_t* key, int64_t n, int64_t s, uin
} }
} }
#elif AVX_LEVEL == 2 #elif AVX_LEVEL == 2
/* Keys are below 2^31, so the signed compare orders them. */ /* Keys are below 2^31, so the signed compare orders them; signed-order keys take the scalar
* loop. */
const __m256i vt = _mm256_set1_epi32((int)thr - 1); const __m256i vt = _mm256_set1_epi32((int)thr - 1);
for (; b + 8 <= n && (og < gt || ot < ties); b += 8) { for (; !j.sgn && b + 8 <= n && (og < gt || ot < ties); b += 8) {
const __m256i k = _mm256_loadu_si256((const __m256i*)(key + b)); const __m256i k = _mm256_loadu_si256((const __m256i*)(key + b));
unsigned hit = (unsigned)_mm256_movemask_ps(_mm256_castsi256_ps(_mm256_cmpgt_epi32(k, vt))); unsigned hit = (unsigned)_mm256_movemask_ps(_mm256_castsi256_ps(_mm256_cmpgt_epi32(k, vt)));
while (hit) { while (hit) {
@@ -470,7 +490,7 @@ static void select_row(const SelJob& j, int64_t m) {
} else { } else {
uint32_t* cand = (uint32_t*)scratch_f32_c((size_t)n); uint32_t* cand = (uint32_t*)scratch_f32_c((size_t)n);
int64_t gt = 0; int64_t gt = 0;
const uint32_t thr = threshold(key, n, topk, hist, cand, &gt); const uint32_t thr = threshold(key, n, topk, j.sgn, hist, cand, &gt);
emit(j, key, n, s, thr, gt, topk - gt, row); emit(j, key, n, s, thr, gt, topk - gt, row);
out = topk; out = topk;
} }
@@ -496,7 +516,7 @@ static void select_row(const SelJob& j, int64_t m) {
/* ------------------------------------------------------------------ qsa_select */ /* ------------------------------------------------------------------ qsa_select */
/* The page-id table the sparse attention reads (docs/OPS.md): over the query's complete blocks /* The page-id table the sparse attention reads (docs/OPS.md): over the query's complete blocks
* n = min(nc[seq], (pos + 1) / ratio, width), the live ones (page id >= 0, score > -1e30) ranked * n = min(nc[seq], (pos + 1) / ratio, width), the live ones (page id >= 0, score > -1e30) ranked
* by max(score, 0). All of them when `topk` or fewer are live, else every block above the topk-th * by max(score, 0), or with `signed` 1 those with a finite score, ranked by it. All of them when `topk` or fewer are live, else every block above the topk-th
* largest value in block order, then the blocks equal to it in block order until topk are taken. * largest value in block order, then the blocks equal to it in block order until topk are taken.
* The partial page bt[n] follows when (pos + 1) % ratio != 0 and n < width, and -1 pads the row * The partial page bt[n] follows when (pos + 1) % ratio != 0 and n < width, and -1 pads the row
* to topk + 1. A row no sequence holds reads sequence 0, as libref's and libr4d's do. */ * to topk + 1. A row no sequence holds reads sequence 0, as libref's and libr4d's do. */
@@ -505,7 +525,9 @@ AVX_KERNEL(qsa_select) {
const RadTensor *sc = &a->t[0], *bt = &a->t[1], *nc = &a->t[2], *po = &a->t[3]; const RadTensor *sc = &a->t[0], *bt = &a->t[1], *nc = &a->t[2], *po = &a->t[3];
const RadTensor *cu = &a->t[4], *se = &a->t[5], *ns = &a->t[6], *su = &a->t[7]; const RadTensor *cu = &a->t[4], *se = &a->t[5], *ns = &a->t[6], *su = &a->t[7];
const int64_t topk = p_int(a, "topk", 0), ratio = p_int(a, "ratio", 0); const int64_t topk = p_int(a, "topk", 0), ratio = p_int(a, "ratio", 0);
const int64_t sgn = p_int(a, "signed", 0);
if (topk <= 0 || ratio <= 0) return RAD_E_SHAPE; if (topk <= 0 || ratio <= 0) return RAD_E_SHAPE;
if (sgn != 0 && sgn != 1) return RAD_E_INVAL;
if (sc->rank != 2 || bt->rank != 2 || se->rank != 2) return RAD_E_SHAPE; if (sc->rank != 2 || bt->rank != 2 || se->rank != 2) return RAD_E_SHAPE;
const int64_t M = sc->shape[0], nb = std::min(bt->shape[1], sc->shape[1]); const int64_t M = sc->shape[0], nb = std::min(bt->shape[1], sc->shape[1]);
const int64_t n_seq = numel(cu) - 1; const int64_t n_seq = numel(cu) - 1;
@@ -526,6 +548,7 @@ AVX_KERNEL(qsa_select) {
j.s_rs = sc->stride[0]; j.s_cs = sc->stride[1]; j.s_rs = sc->stride[0]; j.s_cs = sc->stride[1];
j.bt_rs = bt->stride[0]; j.bt_cs = bt->stride[1]; j.bt_rs = bt->stride[0]; j.bt_cs = bt->stride[1];
j.o_rs = se->stride[0]; j.o_cs = se->stride[1]; j.o_rs = se->stride[0]; j.o_cs = se->stride[1];
j.sgn = sgn != 0;
AVX_PARALLEL_FOR_IF(M * nb >= AVX_PAR_MIN && M > 1) AVX_PARALLEL_FOR_IF(M * nb >= AVX_PAR_MIN && M > 1)
for (int64_t m = 0; m < M; ++m) select_row(j, m); for (int64_t m = 0; m < M; ++m) select_row(j, m);
return RAD_OK; return RAD_OK;
+1 -1
View File
@@ -305,7 +305,7 @@ static const RadParamSpec pQsaScore[] = { P_INT("M"), P_INT("n"), P_INT("heads")
static const RadOperandSpec oQsaScore[] = { OPD("q"), OPD("bk"), OPD("bt"), OPD("cu"), OPD("nc"), static const RadOperandSpec oQsaScore[] = { OPD("q"), OPD("bk"), OPD("bt"), OPD("cu"), OPD("nc"),
OUT("score") }; OUT("score") };
static const RadParamSpec pQsaSelect[] = { P_INT("M"), P_INT("blocks"), P_INT("topk"), static const RadParamSpec pQsaSelect[] = { P_INT("M"), P_INT("blocks"), P_INT("topk"),
P_INT("ratio"), P_INT("seqs") }; P_INT("ratio"), P_INT("seqs"), P_INTO("signed") };
static const RadOperandSpec oQsaSelect[] = { OPD("score"), OPD("bt"), OPD("nc"), OPD("pos"), static const RadOperandSpec oQsaSelect[] = { OPD("score"), OPD("bt"), OPD("nc"), OPD("pos"),
OPD("cu"), OUT("sel"), OUT("nsel"), OUT("seqused") }; OPD("cu"), OUT("sel"), OUT("nsel"), OUT("seqused") };
static const RadParamSpec pQsaTailStore[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), P_INT("ring"), static const RadParamSpec pQsaTailStore[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), P_INT("ring"),
+60 -28
View File
@@ -65,15 +65,28 @@ static inline int lds_cap_of() {
return v; return v;
} }
/* The order key, by the op's `signed` parameter: the unsigned order (Signed = false, the
* default) or the signed one. Key 0 means "no block" in both. */
template <bool Signed> __device__ __forceinline__ uint32_t keyof(float v, int page);
/* Order key. Scores are >= 0 or -inf, and non-negative float bit patterns order as unsigned. /* Order key. Scores are >= 0 or -inf, and non-negative float bit patterns order as unsigned.
* `+1` keeps a live score of exactly zero above key 0, which means "no block". The clamp is a * `+1` keeps a live score of exactly zero above key 0, which means "no block". The clamp is a
* select, not fmaxf: fmaxf(-0.0f, 0.0f) may return -0.0f, whose sign bit would rank it above * select, not fmaxf: fmaxf(-0.0f, 0.0f) may return -0.0f, whose sign bit would rank it above
* every positive score. */ * every positive score. */
__device__ __forceinline__ uint32_t keyof(float v, int page) { template <> __device__ __forceinline__ uint32_t keyof<false>(float v, int page) {
if (page < 0 || !(v > -1e30f)) return 0u; if (page < 0 || !(v > -1e30f)) return 0u;
return __float_as_uint(v > 0.0f ? v : 0.0f) + 1u; return __float_as_uint(v > 0.0f ? v : 0.0f) + 1u;
} }
/* Signed order key, for indexers whose scores can be negative (GLM's head gates): the float's
* total order over all 32 bits. Absent pages and nonfinite scores are key 0, which no finite
* score maps to (only NaN bit patterns would); both signed zeros map to one key, so they tie. */
template <> __device__ __forceinline__ uint32_t keyof<true>(float v, int page) {
if (page < 0 || !__builtin_isfinite(v)) return 0u;
const uint32_t bits = v == 0 ? 0u : __float_as_uint(v);
return (bits & 0x80000000u) ? ~bits : (bits ^ 0x80000000u);
}
/* Scans run in registers on DPP (five steps per wave, r4d_common.h); only wave totals go through /* Scans run in registers on DPP (five steps per wave, r4d_common.h); only wave totals go through
* LDS, instead of step-doubling LDS rounds with barriers that wait on up to 32 waves. * LDS, instead of step-doubling LDS rounds with barriers that wait on up to 32 waves.
* *
@@ -115,6 +128,7 @@ __device__ __forceinline__ void counts_before(const uint32_t* wsum, int lane, in
* block table and complete-block count. The per-query bound is (pos + 1) / ratio: block b is * block table and complete-block count. The per-query bound is (pos + 1) / ratio: block b is
* entirely in the query's past when b * ratio + ratio - 1 <= pos. At decode this equals the * entirely in the query's past when b * ratio + ratio - 1 <= pos. At decode this equals the
* sequence's complete count. */ * sequence's complete count. */
template <bool Signed>
__global__ __launch_bounds__(kThreads) void qsa_select_kernel( __global__ __launch_bounds__(kThreads) void qsa_select_kernel(
const float* __restrict__ score, const int32_t* __restrict__ bt, const float* __restrict__ score, const int32_t* __restrict__ bt,
const int32_t* __restrict__ nc, const int32_t* __restrict__ pos, const int32_t* __restrict__ nc, const int32_t* __restrict__ pos,
@@ -153,7 +167,7 @@ __global__ __launch_bounds__(kThreads) void qsa_select_kernel(
if (cached) { if (cached) {
for (int b0 = 0; b0 < n; b0 += kThreads) { for (int b0 = 0; b0 < n; b0 += kThreads) {
const int b = b0 + t; const int b = b0 + t;
if (b < n) sh_key[b] = keyof(sc[b], tb[b]); if (b < n) sh_key[b] = keyof<Signed>(sc[b], tb[b]);
} }
__syncthreads(); __syncthreads();
} }
@@ -168,7 +182,7 @@ __global__ __launch_bounds__(kThreads) void qsa_select_kernel(
__syncthreads(); __syncthreads();
const uint32_t mask = (shift == 24) ? 0u : (0xFFFFFFFFu << (shift + 8)); const uint32_t mask = (shift == 24) ? 0u : (0xFFFFFFFFu << (shift + 8));
for (int b = t; b < n; b += kThreads) { for (int b = t; b < n; b += kThreads) {
const uint32_t k = cached ? sh_key[b] : keyof(sc[b], tb[b]); const uint32_t k = cached ? sh_key[b] : keyof<Signed>(sc[b], tb[b]);
if (k != 0u && (k & mask) == (fixed & mask)) if (k != 0u && (k & mask) == (fixed & mask))
atomicAdd(&hist[(k >> shift) & 255u], 1u); atomicAdd(&hist[(k >> shift) & 255u], 1u);
} }
@@ -214,7 +228,7 @@ __global__ __launch_bounds__(kThreads) void qsa_select_kernel(
for (int b0 = 0; b0 < n; b0 += kThreads) { for (int b0 = 0; b0 < n; b0 += kThreads) {
const int b = b0 + t; const int b = b0 + t;
uint32_t k = 0; uint32_t k = 0;
if (b < n) k = cached ? sh_key[b] : keyof(sc[b], tb[b]); if (b < n) k = cached ? sh_key[b] : keyof<Signed>(sc[b], tb[b]);
const bool hi = k != 0u && (n <= topk || k > T); const bool hi = k != 0u && (n <= topk || k > T);
const bool eq = k != 0u && n > topk && k == T; const bool eq = k != 0u && n > topk && k == T;
@@ -297,7 +311,14 @@ __global__ __launch_bounds__(kThreads) void qsa_select_kernel(
* count) does not depend on the digit split, and the emit is the same. r4d_selftest compares the * count) does not depend on the digit split, and the emit is the same. r4d_selftest compares the
* two byte for byte. */ * two byte for byte. */
constexpr int kWideBits = 12; /* the first digit */ constexpr int kWideBits = 12; /* the first digit */
constexpr int kWideShift = 31 - kWideBits; /* a key's bit 31 is clear: see keyof */ /* The first digit's shift, by key order. */
template <bool Signed> struct WideKey;
template <> struct WideKey<false> {
static constexpr int kWideShift = 31 - kWideBits; /* a key's bit 31 is clear: see keyof */
};
template <> struct WideKey<true> {
static constexpr int kWideShift = 32 - kWideBits; /* the signed key uses all 32 bits */
};
constexpr int kWideBins = 1 << kWideBits; constexpr int kWideBins = 1 << kWideBits;
constexpr int kWideCand = kWideBins - kBins; /* keys the second pass may collect */ constexpr int kWideCand = kWideBins - kBins; /* keys the second pass may collect */
constexpr int kWidePer = kWideBins / kThreads; /* first-digit bins a thread scans */ constexpr int kWidePer = kWideBins / kThreads; /* first-digit bins a thread scans */
@@ -377,6 +398,7 @@ __device__ __forceinline__ void lds_kth(const uint32_t* keys, int cnt, uint32_t
*need = want; *need = want;
} }
template <bool Signed>
__global__ __launch_bounds__(kThreads) void qsa_select_wide_kernel( __global__ __launch_bounds__(kThreads) void qsa_select_wide_kernel(
const float* __restrict__ score, const int32_t* __restrict__ bt, const float* __restrict__ score, const int32_t* __restrict__ bt,
const int32_t* __restrict__ nc, const int32_t* __restrict__ pos, const int32_t* __restrict__ nc, const int32_t* __restrict__ pos,
@@ -402,6 +424,7 @@ __global__ __launch_bounds__(kThreads) void qsa_select_wide_kernel(
const int m = blockIdx.x; const int m = blockIdx.x;
const int t = threadIdx.x; const int t = threadIdx.x;
const int lane = t & (kWave - 1), wave = t >> 5; const int lane = t & (kWave - 1), wave = t >> 5;
constexpr int kWideShift = WideKey<Signed>::kWideShift;
if (t == 0) sh_seq = 0; if (t == 0) sh_seq = 0;
__syncthreads(); __syncthreads();
@@ -426,7 +449,7 @@ __global__ __launch_bounds__(kThreads) void qsa_select_wide_kernel(
for (int i = t; i < kSamp; i += kThreads) { for (int i = t; i < kSamp; i += kThreads) {
const int r = i / kSampRun, j = i % kSampRun; const int r = i / kSampRun, j = i % kSampRun;
const long b = (long)r * (n - kSampRun) / (kSampRuns - 1) + j; const long b = (long)r * (n - kSampRun) / (kSampRuns - 1) + j;
samp[i] = keyof(sc[b], tb[b]); samp[i] = keyof<Signed>(sc[b], tb[b]);
} }
__syncthreads(); __syncthreads();
uint32_t ms = (uint32_t)(((uint64_t)topk * 2u * kSamp + (uint64_t)n - 1u) / (uint64_t)n) + 8u; uint32_t ms = (uint32_t)(((uint64_t)topk * 2u * kSamp + (uint64_t)n - 1u) / (uint64_t)n) + 8u;
@@ -448,14 +471,14 @@ __global__ __launch_bounds__(kThreads) void qsa_select_wide_kernel(
if (vec && bb + kPer <= n) { if (vec && bb + kPer <= n) {
const float4 s0 = *(const float4*)(sc + bb), s1 = *(const float4*)(sc + bb + 4); const float4 s0 = *(const float4*)(sc + bb), s1 = *(const float4*)(sc + bb + 4);
const int4 p0 = *(const int4*)(tb + bb), p1 = *(const int4*)(tb + bb + 4); const int4 p0 = *(const int4*)(tb + bb), p1 = *(const int4*)(tb + bb + 4);
kk[0] = keyof(s0.x, p0.x); kk[1] = keyof(s0.y, p0.y); kk[0] = keyof<Signed>(s0.x, p0.x); kk[1] = keyof<Signed>(s0.y, p0.y);
kk[2] = keyof(s0.z, p0.z); kk[3] = keyof(s0.w, p0.w); kk[2] = keyof<Signed>(s0.z, p0.z); kk[3] = keyof<Signed>(s0.w, p0.w);
kk[4] = keyof(s1.x, p1.x); kk[5] = keyof(s1.y, p1.y); kk[4] = keyof<Signed>(s1.x, p1.x); kk[5] = keyof<Signed>(s1.y, p1.y);
kk[6] = keyof(s1.z, p1.z); kk[7] = keyof(s1.w, p1.w); kk[6] = keyof<Signed>(s1.z, p1.z); kk[7] = keyof<Signed>(s1.w, p1.w);
} else { } else {
#pragma unroll #pragma unroll
for (int e = 0; e < kPer; ++e) for (int e = 0; e < kPer; ++e)
kk[e] = bb + e < n ? keyof(sc[bb + e], tb[bb + e]) : 0u; kk[e] = bb + e < n ? keyof<Signed>(sc[bb + e], tb[bb + e]) : 0u;
} }
uint32_t mine = 0; uint32_t mine = 0;
#pragma unroll #pragma unroll
@@ -514,7 +537,7 @@ __global__ __launch_bounds__(kThreads) void qsa_select_wide_kernel(
for (int i = t; i < kWideBins; i += kThreads) wide[i] = 0; for (int i = t; i < kWideBins; i += kThreads) wide[i] = 0;
__syncthreads(); __syncthreads();
for (int b = t; b < n; b += kThreads) { for (int b = t; b < n; b += kThreads) {
const uint32_t k = keyof(sc[b], tb[b]); const uint32_t k = keyof<Signed>(sc[b], tb[b]);
if (k != 0u) atomicAdd(&wide[k >> kWideShift], 1u); if (k != 0u) atomicAdd(&wide[k >> kWideShift], 1u);
} }
__syncthreads(); __syncthreads();
@@ -565,7 +588,7 @@ __global__ __launch_bounds__(kThreads) void qsa_select_wide_kernel(
uint32_t n_in = 0; uint32_t n_in = 0;
for (int b0 = 0; b0 < n; b0 += kThreads) { for (int b0 = 0; b0 < n; b0 += kThreads) {
const int b = b0 + t; const int b = b0 + t;
const uint32_t k = b < n ? keyof(sc[b], tb[b]) : 0u; const uint32_t k = b < n ? keyof<Signed>(sc[b], tb[b]) : 0u;
const bool abv = k != 0u && (k >> kWideShift) > B; const bool abv = k != 0u && (k >> kWideShift) > B;
const bool inb = k != 0u && (k >> kWideShift) == B; const bool inb = k != 0u && (k >> kWideShift) == B;
const unsigned long long ma = __ballot(abv), mi = __ballot(inb); const unsigned long long ma = __ballot(abv), mi = __ballot(inb);
@@ -594,7 +617,7 @@ __global__ __launch_bounds__(kThreads) void qsa_select_wide_kernel(
__syncthreads(); __syncthreads();
} else if (inlds) { } else if (inlds) {
for (int b = t; b < n; b += kThreads) { for (int b = t; b < n; b += kThreads) {
const uint32_t k = keyof(sc[b], tb[b]); const uint32_t k = keyof<Signed>(sc[b], tb[b]);
if (k != 0u && (k >> kWideShift) == B) cand[atomicAdd(&sh_cnt, 1u)] = k; if (k != 0u && (k >> kWideShift) == B) cand[atomicAdd(&sh_cnt, 1u)] = k;
} }
__syncthreads(); __syncthreads();
@@ -602,7 +625,7 @@ __global__ __launch_bounds__(kThreads) void qsa_select_wide_kernel(
const int ncand = (merged || inlds) ? (int)sh_cnt : n; const int ncand = (merged || inlds) ? (int)sh_cnt : n;
const uint32_t* const keys = merged ? mkey : cand; const uint32_t* const keys = merged ? mkey : cand;
/* ---- the other 19 bits: digits of 8, 8 and 3. */ /* ---- the other 19 bits: digits of 8, 8 and 3 (20 bits and 8, 8, 4 when signed). */
const int shifts[3] = { kWideShift - 8, kWideShift - 16, 0 }; const int shifts[3] = { kWideShift - 8, kWideShift - 16, 0 };
const int widths[3] = { 8, 8, kWideShift - 16 }; const int widths[3] = { 8, 8, kWideShift - 16 };
#pragma unroll #pragma unroll
@@ -613,7 +636,7 @@ __global__ __launch_bounds__(kThreads) void qsa_select_wide_kernel(
for (int i = t; i < kBins; i += kThreads) hist[i] = 0; for (int i = t; i < kBins; i += kThreads) hist[i] = 0;
__syncthreads(); __syncthreads();
for (int i = t; i < ncand; i += kThreads) { for (int i = t; i < ncand; i += kThreads) {
const uint32_t k = (merged || inlds) ? keys[i] : keyof(sc[i], tb[i]); const uint32_t k = (merged || inlds) ? keys[i] : keyof<Signed>(sc[i], tb[i]);
if (k != 0u && (k & hm) == (fixed & hm)) atomicAdd(&hist[(k >> shift) & dm], 1u); if (k != 0u && (k & hm) == (fixed & hm)) atomicAdd(&hist[(k >> shift) & dm], 1u);
} }
__syncthreads(); __syncthreads();
@@ -687,7 +710,7 @@ __global__ __launch_bounds__(kThreads) void qsa_select_wide_kernel(
for (int b0 = 0; b0 < ((merged || sampled) ? 0 : n); b0 += kThreads) { for (int b0 = 0; b0 < ((merged || sampled) ? 0 : n); b0 += kThreads) {
const int b = b0 + t; const int b = b0 + t;
uint32_t k = 0; uint32_t k = 0;
if (b < n) k = keyof(sc[b], tb[b]); if (b < n) k = keyof<Signed>(sc[b], tb[b]);
const bool hi = k != 0u && (n <= topk || k > T); const bool hi = k != 0u && (n <= topk || k > T);
const bool eq = k != 0u && n > topk && k == T; const bool eq = k != 0u && n > topk && k == T;
@@ -887,6 +910,7 @@ __device__ __forceinline__ ParTake par_take(ParEmit& e, uint32_t k, int lane, in
} }
/* ---- pass one: every chunk's own top-k, as (key, block index) in block order. */ /* ---- pass one: every chunk's own top-k, as (key, block index) in block order. */
template <bool Signed>
__global__ __launch_bounds__(kLocalThreads) void qsa_local_kernel( __global__ __launch_bounds__(kLocalThreads) void qsa_local_kernel(
const float* __restrict__ score, const int32_t* __restrict__ bt, const float* __restrict__ score, const int32_t* __restrict__ bt,
const int32_t* __restrict__ nc, const int32_t* __restrict__ pos, const int32_t* __restrict__ nc, const int32_t* __restrict__ pos,
@@ -921,7 +945,7 @@ __global__ __launch_bounds__(kLocalThreads) void qsa_local_kernel(
const float* sc = score + (size_t)m * s_ld; const float* sc = score + (size_t)m * s_ld;
const int32_t* tb = bt + (size_t)seq * t_ld; const int32_t* tb = bt + (size_t)seq * t_ld;
for (int i = t; i < cn; i += LT) sh_key[i] = keyof(sc[lo + i], tb[lo + i]); for (int i = t; i < cn; i += LT) sh_key[i] = keyof<Signed>(sc[lo + i], tb[lo + i]);
__syncthreads(); __syncthreads();
ParEmit e{ wsum, &sh_hi, &sh_ord, 0u, 0u, 0u, cn <= topk }; ParEmit e{ wsum, &sh_hi, &sh_ord, 0u, 0u, 0u, cn <= topk };
@@ -955,6 +979,7 @@ __global__ __launch_bounds__(kLocalThreads) void qsa_local_kernel(
} }
/* ---- pass two: merge the chunk lists and emit the page-id table. */ /* ---- pass two: merge the chunk lists and emit the page-id table. */
template <bool Signed>
__global__ __launch_bounds__(kMergeThreads) void qsa_merge_kernel( __global__ __launch_bounds__(kMergeThreads) void qsa_merge_kernel(
const uint32_t* __restrict__ ck, const int32_t* __restrict__ ci, const uint32_t* __restrict__ ck, const int32_t* __restrict__ ci,
const int32_t* __restrict__ cc, const int32_t* __restrict__ bt, const int32_t* __restrict__ cc, const int32_t* __restrict__ bt,
@@ -1071,6 +1096,8 @@ static inline int64_t par_scratch_bytes(long long topk) {
// cu [nseq+1] i32, cumulative query counts: which sequence a query row belongs to // cu [nseq+1] i32, cumulative query counts: which sequence a query row belongs to
// sel [M][topk+1] i32, `o_ld` elements a row (0 = topk+1); -1 padded // sel [M][topk+1] i32, `o_ld` elements a row (0 = topk+1); -1 padded
// nsel [M] i32 // nsel [M] i32
// Signed selects the key order (keyof), from the op's `signed` parameter.
template <bool Signed>
static int r4d_qsa_select_bf16_emit(long score, long bt, long nc, long pos, long cu, long sel, static int r4d_qsa_select_bf16_emit(long score, long bt, long nc, long pos, long cu, long sel,
long nsel, long seqused, int M, int nb, int topk, int ratio, long nsel, long seqused, int M, int nb, int topk, int ratio,
int nseq, int s_ld, int t_ld, int o_ld, int nseq, int s_ld, int t_ld, int o_ld,
@@ -1093,13 +1120,14 @@ static int r4d_qsa_select_bf16_emit(long score, long bt, long nc, long pos, long
uint32_t* ck = (uint32_t*)scratch; uint32_t* ck = (uint32_t*)scratch;
int32_t* ci = (int32_t*)(ck + (size_t)kParM * kParWMax * topk); int32_t* ci = (int32_t*)(ck + (size_t)kParM * kParWMax * topk);
int32_t* cc = ci + (size_t)kParM * kParWMax * topk; int32_t* cc = ci + (size_t)kParM * kParWMax * topk;
const int rc = R4D_EMIT(stream, qsa_local_kernel, dim3((unsigned)nchunk, (unsigned)M), const int rc = R4D_EMIT(stream, qsa_local_kernel<Signed>,
dim3(kLocalThreads), (size_t)chunk * sizeof(uint32_t), dim3((unsigned)nchunk, (unsigned)M), dim3(kLocalThreads),
(size_t)chunk * sizeof(uint32_t),
(const float*)score, (const int32_t*)bt, (const int32_t*)nc, (const float*)score, (const int32_t*)bt, (const int32_t*)nc,
(const int32_t*)pos, (const int32_t*)cu, ck, ci, cc, nb, topk, (const int32_t*)pos, (const int32_t*)cu, ck, ci, cc, nb, topk,
ratio, nseq, s_ld, t_ld, chunk); ratio, nseq, s_ld, t_ld, chunk);
if (rc != RAD_OK) return rc; if (rc != RAD_OK) return rc;
return R4D_EMIT(stream, qsa_merge_kernel, dim3((unsigned)M), dim3(kMergeThreads), return R4D_EMIT(stream, qsa_merge_kernel<Signed>, dim3((unsigned)M), dim3(kMergeThreads),
(size_t)nchunk * topk * sizeof(uint32_t), (const uint32_t*)ck, (size_t)nchunk * topk * sizeof(uint32_t), (const uint32_t*)ck,
(const int32_t*)ci, (const int32_t*)cc, (const int32_t*)bt, (const int32_t*)ci, (const int32_t*)cc, (const int32_t*)bt,
(const int32_t*)nc, (const int32_t*)pos, (const int32_t*)cu, (const int32_t*)nc, (const int32_t*)pos, (const int32_t*)cu,
@@ -1109,7 +1137,8 @@ static int r4d_qsa_select_bf16_emit(long score, long bt, long nc, long pos, long
/* A wide launch (prefill chunk, key cache off) takes the wide kernel. */ /* A wide launch (prefill chunk, key cache off) takes the wide kernel. */
if (g_qsa_select_form == 2 || (g_qsa_select_form == 0 && M > kCacheMaxM)) { if (g_qsa_select_form == 2 || (g_qsa_select_form == 0 && M > kCacheMaxM)) {
return R4D_EMIT(stream, qsa_select_wide_kernel, dim3((unsigned)M), dim3(kThreads), 0, return R4D_EMIT(stream, qsa_select_wide_kernel<Signed>, dim3((unsigned)M),
dim3(kThreads), 0,
(const float*)score, (const int32_t*)bt, (const int32_t*)nc, (const float*)score, (const int32_t*)bt, (const int32_t*)nc,
(const int32_t*)pos, (const int32_t*)cu, (int32_t*)sel, (const int32_t*)pos, (const int32_t*)cu, (int32_t*)sel,
(int32_t*)nsel, (int32_t*)seqused, nb, topk, ratio, nseq, s_ld, t_ld, (int32_t*)nsel, (int32_t*)seqused, nb, topk, ratio, nseq, s_ld, t_ld,
@@ -1119,7 +1148,7 @@ static int r4d_qsa_select_bf16_emit(long score, long bt, long nc, long pos, long
/* Never more than the table width. */ /* Never more than the table width. */
int cap = (M <= kCacheMaxM) ? lds_cap_of() : 0; int cap = (M <= kCacheMaxM) ? lds_cap_of() : 0;
if (cap > nb) cap = nb; if (cap > nb) cap = nb;
return R4D_EMIT(stream, qsa_select_kernel, dim3((unsigned)M), dim3(kThreads), return R4D_EMIT(stream, qsa_select_kernel<Signed>, dim3((unsigned)M), dim3(kThreads),
(size_t)cap * sizeof(uint32_t), (const float*)score, (const int32_t*)bt, (size_t)cap * sizeof(uint32_t), (const float*)score, (const int32_t*)bt,
(const int32_t*)nc, (const int32_t*)pos, (const int32_t*)cu, (const int32_t*)nc, (const int32_t*)pos, (const int32_t*)cu,
(int32_t*)sel, (int32_t*)nsel, (int32_t*)sel, (int32_t*)nsel,
@@ -1151,7 +1180,9 @@ extern "C" int r4d_rad_qsa_select(const RadArgs* a, RadStream stream) {
if (sc->rank != 2 || bt->rank != 2 || se->rank != 2) return R4D_NO(RAD_E_SHAPE); if (sc->rank != 2 || bt->rank != 2 || se->rank != 2) return R4D_NO(RAD_E_SHAPE);
const long long topk = r4d_geti_or(a, "topk", 0); const long long topk = r4d_geti_or(a, "topk", 0);
const long long ratio = r4d_geti_or(a, "ratio", 0); const long long ratio = r4d_geti_or(a, "ratio", 0);
const long long sgn = r4d_geti_or(a, "signed", 0);
if (topk <= 0 || ratio <= 0) return R4D_NO(RAD_E_INVAL); if (topk <= 0 || ratio <= 0) return R4D_NO(RAD_E_INVAL);
if (sgn != 0 && sgn != 1) return R4D_NO(RAD_E_INVAL);
/* nb = min of the two widths, as in r4d_qsa_score_bf16.hip. */ /* nb = min of the two widths, as in r4d_qsa_score_bf16.hip. */
const long long M = sc->shape[0]; const long long M = sc->shape[0];
const long long nb = bt->shape[1] < sc->shape[1] ? bt->shape[1] : sc->shape[1]; const long long nb = bt->shape[1] < sc->shape[1] ? bt->shape[1] : sc->shape[1];
@@ -1167,10 +1198,11 @@ extern "C" int r4d_rad_qsa_select(const RadArgs* a, RadStream stream) {
const long long sp = r4d_row_pitch(sc), tp = r4d_row_pitch(bt), op = r4d_row_pitch(se); const long long sp = r4d_row_pitch(sc), tp = r4d_row_pitch(bt), op = r4d_row_pitch(se);
if (sp < 0 || tp < 0 || op < 0) return R4D_NO(RAD_E_STRIDE); if (sp < 0 || tp < 0 || op < 0) return R4D_NO(RAD_E_STRIDE);
/* Scratch is optional: without enough, the non-split kernels run. */ /* Scratch is optional: without enough, the non-split kernels run. */
return r4d_qsa_select_bf16_emit(r4d_addr(sc), r4d_addr(bt), r4d_addr(nc), r4d_addr(po), auto* const emit = sgn ? r4d_qsa_select_bf16_emit<true> : r4d_qsa_select_bf16_emit<false>;
r4d_addr(cu), r4d_addr(se), r4d_addr(ns), r4d_addr(su), return emit(r4d_addr(sc), r4d_addr(bt), r4d_addr(nc), r4d_addr(po),
(int)M, (int)nb, (int)topk, (int)ratio, (int)nseq, r4d_addr(cu), r4d_addr(se), r4d_addr(ns), r4d_addr(su),
(int)sp, (int)tp, (int)op, a->scratch, a->scratch_bytes, (int)M, (int)nb, (int)topk, (int)ratio, (int)nseq,
stream); (int)sp, (int)tp, (int)op, a->scratch, a->scratch_bytes,
stream);
} }
+6 -1
View File
@@ -1161,6 +1161,10 @@ static const RadParamSpec pQsaSelect[] = {
{ "topk", RAD_P_INT, RAD_REQUIRED }, { "topk", RAD_P_INT, RAD_REQUIRED },
{ "ratio", RAD_P_INT, RAD_REQUIRED }, { "ratio", RAD_P_INT, RAD_REQUIRED },
{ "seqs", RAD_P_INT, RAD_REQUIRED }, { "seqs", RAD_P_INT, RAD_REQUIRED },
/* The ranking: 0 (absent) ranks max(score, 0) with scores at or below -1e30 dead, the QSA
* indexer's scores being sums of relus; 1 ranks signed finite scores, nonfinite ones dead, for
* indexers whose head weights can be negative. */
{ "signed", RAD_P_INT, RAD_OPTIONAL },
}; };
static const RadOperandSpec oQsaSelect[] = { static const RadOperandSpec oQsaSelect[] = {
{ "score", RAD_OPD_IN, 0 }, { "score", RAD_OPD_IN, 0 },
@@ -2878,7 +2882,8 @@ static const RadOpSchema kSchemas[] = {
"complete block, which the query itself lives in -- is appended LAST and is never a " "complete block, which the query itself lives in -- is appended LAST and is never a "
"candidate: last is what makes the attention's own causal bound mask inside it and " "candidate: last is what makes the attention's own causal bound mask inside it and "
"nowhere else, so the effective query position is (nsel-1)*ratio + pos%ratio. Ties " "nowhere else, so the effective query position is (nsel-1)*ratio + pos%ratio. Ties "
"break to the LOWER block index through a prefix count, never through atomic order."), "break to the LOWER block index through a prefix count, never through atomic order. "
"`signed` 1 ranks signed finite scores; absent or 0 ranks max(score, 0)."),
SCHEMA("qsa_score", pQsaScore, oQsaScore, SCHEMA("qsa_score", pQsaScore, oQsaScore,
"score[m][b] = (sum over heads of relu(q[m][h] . bk[bt[m][b]])) / sqrt(n). THE RELU IS " "score[m][b] = (sum over heads of relu(q[m][h] . bk[bt[m][b]])) / sqrt(n). THE RELU IS "
"PER (BLOCK, HEAD) AND THE SUM IS OVER HEADS, in that order -- four independent votes, " "PER (BLOCK, HEAD) AND THE SUM IS OVER HEADS, in that order -- four independent votes, "
+8 -3
View File
@@ -15,6 +15,7 @@
#include "ref_ops.h" #include "ref_ops.h"
#include <algorithm> #include <algorithm>
#include <cmath>
#include <vector> #include <vector>
using namespace ref; using namespace ref;
@@ -101,7 +102,8 @@ int ref_qsa_score(const RadArgs* a, RadStream) {
/* ------------------------------------------------------------------ qsa_select */ /* ------------------------------------------------------------------ qsa_select */
/* The page-id table the sparse attention reads (docs/OPS.md): over the query's complete blocks /* The page-id table the sparse attention reads (docs/OPS.md): over the query's complete blocks
* n = min(nc[seq], (pos + 1) / ratio, width), the live ones (page id >= 0, score > -1e30) ranked * n = min(nc[seq], (pos + 1) / ratio, width), the live ones (page id >= 0, score > -1e30) ranked
* by max(score, 0). At most `topk` are taken: all of them when that many or fewer are live, else * by max(score, 0); with `signed` 1 the live ones are those with a finite score, ranked by it (so
* -0 ties +0). At most `topk` are taken: all of them when that many or fewer are live, else
* every block above the topk-th largest value in block order, then the blocks equal to it in block * every block above the topk-th largest value in block order, then the blocks equal to it in block
* order until topk are taken. The partial page bt[n] follows when (pos + 1) % ratio != 0 and * order until topk are taken. The partial page bt[n] follows when (pos + 1) % ratio != 0 and
* n < width, and -1 pads the row to topk + 1. nsel counts the entries, seqused is * n < width, and -1 pads the row to topk + 1. nsel counts the entries, seqused is
@@ -112,7 +114,9 @@ int ref_qsa_select(const RadArgs* a, RadStream) {
const RadTensor *sc = &a->t[0], *bt = &a->t[1], *nc = &a->t[2], *po = &a->t[3]; const RadTensor *sc = &a->t[0], *bt = &a->t[1], *nc = &a->t[2], *po = &a->t[3];
const RadTensor *cu = &a->t[4], *se = &a->t[5], *ns = &a->t[6], *su = &a->t[7]; const RadTensor *cu = &a->t[4], *se = &a->t[5], *ns = &a->t[6], *su = &a->t[7];
const int64_t topk = p_int(a, "topk", 0), ratio = p_int(a, "ratio", 0); const int64_t topk = p_int(a, "topk", 0), ratio = p_int(a, "ratio", 0);
const int64_t sgn = p_int(a, "signed", 0);
if (topk <= 0 || ratio <= 0) return RAD_E_SHAPE; if (topk <= 0 || ratio <= 0) return RAD_E_SHAPE;
if (sgn != 0 && sgn != 1) return RAD_E_INVAL;
if (sc->rank != 2 || bt->rank != 2 || se->rank != 2) return RAD_E_SHAPE; if (sc->rank != 2 || bt->rank != 2 || se->rank != 2) return RAD_E_SHAPE;
const int64_t M = sc->shape[0], nb = std::min(bt->shape[1], sc->shape[1]); const int64_t M = sc->shape[0], nb = std::min(bt->shape[1], sc->shape[1]);
const int64_t n_seq = numel(cu) - 1; const int64_t n_seq = numel(cu) - 1;
@@ -136,8 +140,9 @@ int ref_qsa_select(const RadArgs* a, RadStream) {
std::vector<int64_t> live; std::vector<int64_t> live;
for (int64_t b = 0; b < n; ++b) { for (int64_t b = 0; b < n; ++b) {
const float v = ldt(sc, m * s_rs + b * s_cs); const float v = ldt(sc, m * s_rs + b * s_cs);
if (ldi(bt, s * bt_rs + b * bt_cs) < 0 || !(v > -1e30f)) continue; if (ldi(bt, s * bt_rs + b * bt_cs) < 0) continue;
val[(size_t)b] = v > 0.0f ? v : 0.0f; if (sgn ? !std::isfinite(v) : !(v > -1e30f)) continue;
val[(size_t)b] = sgn ? v : v > 0.0f ? v : 0.0f;
live.push_back(b); live.push_back(b);
} }
std::vector<int64_t> take; std::vector<int64_t> take;
+1 -1
View File
@@ -381,7 +381,7 @@ static const RadParamSpec pQsaScore[] = { P_INT("M"), P_INT("n"), P_INT("heads")
static const RadOperandSpec oQsaScore[] = { OPD("q"), OPD("bk"), OPD("bt"), OPD("cu"), OPD("nc"), static const RadOperandSpec oQsaScore[] = { OPD("q"), OPD("bk"), OPD("bt"), OPD("cu"), OPD("nc"),
OUT("score") }; OUT("score") };
static const RadParamSpec pQsaSelect[] = { P_INT("M"), P_INT("blocks"), P_INT("topk"), static const RadParamSpec pQsaSelect[] = { P_INT("M"), P_INT("blocks"), P_INT("topk"),
P_INT("ratio"), P_INT("seqs") }; P_INT("ratio"), P_INT("seqs"), P_INTO("signed") };
static const RadOperandSpec oQsaSelect[] = { OPD("score"), OPD("bt"), OPD("nc"), OPD("pos"), static const RadOperandSpec oQsaSelect[] = { OPD("score"), OPD("bt"), OPD("nc"), OPD("pos"),
OPD("cu"), OUT("sel"), OUT("nsel"), OUT("seqused") }; OPD("cu"), OUT("sel"), OUT("nsel"), OUT("seqused") };
static const RadParamSpec pQsaTailStore[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), P_INT("ring"), static const RadParamSpec pQsaTailStore[] = { P_INT("M"), P_INT("n"), P_INT("ratio"), P_INT("ring"),
+67 -12
View File
@@ -12,7 +12,11 @@
* seeded sweep of small odd shapes (NaN, +-inf, -0.0, denormal and -1e30 scores, pitches, a table * seeded sweep of small odd shapes (NaN, +-inf, -0.0, denormal and -1e30 scores, pitches, a table
* narrower than the scores, rows no sequence holds, topk 1, ratio 1). Which branch a row takes * narrower than the scores, rows no sequence holds, topk 1, ratio 1). Which branch a row takes
* inside the kernels is predicted from their constants, restated in `Path`; the run fails when the * inside the kernels is predicted from their constants, restated in `Path`; the run fails when the
* fixtures stop reaching one of them. `sweep N` runs N seeded fixtures at serving sizes instead. */ * fixtures stop reaching one of them. `sweep N` runs N seeded fixtures at serving sizes instead.
*
* Fixtures marked signed run with the op's `signed` parameter: the kernels' signed key order,
* against the port's signed ranking, over negative, mixed and the sweep's odd scores. The unsigned
* fixtures are the same as without the parameter. */
#include "rad_abi.h" #include "rad_abi.h"
#include "rad_plugin.h" #include "rad_plugin.h"
#include "r4d_plugin.h" #include "r4d_plugin.h"
@@ -42,6 +46,7 @@ struct Fix {
int S = 1, M = 1, nb_s = 1, nb_t = 1; /* sequences, query rows, score and table widths */ int S = 1, M = 1, nb_s = 1, nb_t = 1; /* sequences, query rows, score and table widths */
int s_ld = 0, t_ld = 0, o_ld = 0; /* row pitches; 0 = tight */ int s_ld = 0, t_ld = 0, o_ld = 0; /* row pitches; 0 = tight */
int topk = 1, ratio = 1; int topk = 1, ratio = 1;
bool sgn = false; /* the op's `signed` parameter */
std::vector<float> sc; /* [M][s_ld] */ std::vector<float> sc; /* [M][s_ld] */
std::vector<int32_t> bt; /* [S][t_ld] */ std::vector<int32_t> bt; /* [S][t_ld] */
std::vector<int32_t> nc, pos, cu; /* [S], [M], [S + 1] */ std::vector<int32_t> nc, pos, cu; /* [S], [M], [S + 1] */
@@ -66,7 +71,7 @@ struct Fix {
}; };
/* ---- libref's ref_qsa_select (libref/ref_qsa.cpp), restated over the fixture's arrays: the same /* ---- libref's ref_qsa_select (libref/ref_qsa.cpp), restated over the fixture's arrays: the same
* steps in the same order. */ * steps in the same order, both rankings. */
void ref_select(const Fix& f, std::vector<int32_t>& se, std::vector<int32_t>& ns, std::vector<int32_t>& su) { void ref_select(const Fix& f, std::vector<int32_t>& se, std::vector<int32_t>& ns, std::vector<int32_t>& su) {
const int64_t topk = f.topk, ratio = f.ratio; const int64_t topk = f.topk, ratio = f.ratio;
se.assign((size_t)f.M * (topk + 1), 0); ns.assign((size_t)f.M, 0); su.assign((size_t)f.M, 0); se.assign((size_t)f.M * (topk + 1), 0); ns.assign((size_t)f.M, 0); su.assign((size_t)f.M, 0);
@@ -77,8 +82,9 @@ void ref_select(const Fix& f, std::vector<int32_t>& se, std::vector<int32_t>& ns
std::vector<int64_t> live; std::vector<int64_t> live;
for (int64_t b = 0; b < n; ++b) { for (int64_t b = 0; b < n; ++b) {
const float v = f.sc[(size_t)(m * f.s_ld + b)]; const float v = f.sc[(size_t)(m * f.s_ld + b)];
if (f.bt[(size_t)(s * f.t_ld + b)] < 0 || !(v > -1e30f)) continue; if (f.bt[(size_t)(s * f.t_ld + b)] < 0) continue;
val[(size_t)b] = v > 0.0f ? v : 0.0f; if (f.sgn ? !std::isfinite(v) : !(v > -1e30f)) continue;
val[(size_t)b] = f.sgn ? v : v > 0.0f ? v : 0.0f;
live.push_back(b); live.push_back(b);
} }
std::vector<int64_t> take; std::vector<int64_t> take;
@@ -114,6 +120,8 @@ namespace Path {
constexpr int kCacheCap = 8192, kCacheMaxM = 64; /* the key cache */ constexpr int kCacheCap = 8192, kCacheMaxM = 64; /* the key cache */
constexpr int kSamp = 2048, kSampRun = 32, kSampRuns = 64, kSampMinN = 3 * kSamp, kSampCap = 1920; constexpr int kSamp = 2048, kSampRun = 32, kSampRuns = 64, kSampMinN = 3 * kSamp, kSampCap = 1920;
constexpr int kWideShift = 19, kWideCand = 3840, kMergeAbove = 512, kMergeCand = 1664; constexpr int kWideShift = 19, kWideCand = 3840, kMergeAbove = 512, kMergeCand = 1664;
constexpr int kWideShiftSigned = 20; /* the signed key's 32 bits */
int wide_shift(bool sgn) { return sgn ? kWideShiftSigned : kWideShift; }
constexpr int kParM = 32, kParWMax = 16, kParCap = 8192; /* the split path */ constexpr int kParM = 32, kParWMax = 16, kParCap = 8192; /* the split path */
int par_chunks(int nb) { return std::max(1, std::min(kParWMax, (nb + 4095) / 4096)); } int par_chunks(int nb) { return std::max(1, std::min(kParWMax, (nb + 4095) / 4096)); }
} }
@@ -126,6 +134,14 @@ uint32_t keyof(float v, int32_t page) {
return u + 1u; return u + 1u;
} }
/* The signed order: the float's total order over 32 bits, -0 keyed as +0, nonfinite dead. */
uint32_t keyof_signed(float v, int32_t page) {
if (page < 0 || !std::isfinite(v)) return 0u;
uint32_t u = 0;
if (v != 0.0f) std::memcpy(&u, &v, 4);
return (u & 0x80000000u) ? ~u : (u ^ 0x80000000u);
}
enum Form { kSingle, kWide, kAuto }; enum Form { kSingle, kWide, kAuto };
const char* const kFormName[] = { "single", "wide", "auto" }; const char* const kFormName[] = { "single", "wide", "auto" };
@@ -140,7 +156,11 @@ std::map<std::string, int> g_paths;
void note_paths(const Fix& f, int m, Form form, bool split) { void note_paths(const Fix& f, int m, Form form, bool split) {
const int s = f.seq_of(m), n = f.n_of(m), topk = f.topk; const int s = f.seq_of(m), n = f.n_of(m), topk = f.topk;
std::vector<uint32_t> k((size_t)std::max(n, 0)); std::vector<uint32_t> k((size_t)std::max(n, 0));
for (int b = 0; b < n; ++b) k[(size_t)b] = keyof(f.sc[(size_t)m * f.s_ld + b], f.bt[(size_t)s * f.t_ld + b]); for (int b = 0; b < n; ++b) {
const float v = f.sc[(size_t)m * f.s_ld + b];
const int32_t pg = f.bt[(size_t)s * f.t_ld + b];
k[(size_t)b] = f.sgn ? keyof_signed(v, pg) : keyof(v, pg);
}
std::vector<uint32_t> live; std::vector<uint32_t> live;
for (uint32_t x : k) if (x) live.push_back(x); for (uint32_t x : k) if (x) live.push_back(x);
std::sort(live.begin(), live.end(), std::greater<uint32_t>()); std::sort(live.begin(), live.end(), std::greater<uint32_t>());
@@ -155,6 +175,7 @@ void note_paths(const Fix& f, int m, Form form, bool split) {
int above = 0, eq = 0; int above = 0, eq = 0;
for (uint32_t x : live) { above += x > T; eq += x == T; } for (uint32_t x : live) { above += x > T; eq += x == T; }
if (eq > topk - above) hit("a threshold whose ties are cut short"); if (eq > topk - above) hit("a threshold whose ties are cut short");
if (f.sgn && T < 0x80000000u) hit("signed: a negative threshold");
} }
if (split) { if (split) {
const int nch = Path::par_chunks(f.nb()), chunk = (f.nb() + nch - 1) / nch; const int nch = Path::par_chunks(f.nb()), chunk = (f.nb() + nch - 1) / nch;
@@ -200,9 +221,10 @@ void note_paths(const Fix& f, int m, Form form, bool split) {
hit("wide: a sample that fell back"); hit("wide: a sample that fell back");
} }
if (L <= topk) return; if (L <= topk) return;
const uint32_t B = live[(size_t)topk - 1] >> Path::kWideShift; const int shift = Path::wide_shift(f.sgn);
const uint32_t B = live[(size_t)topk - 1] >> shift;
int in = 0; int in = 0;
for (uint32_t x : live) in += (x >> Path::kWideShift) == B; for (uint32_t x : live) in += (x >> shift) == B;
hit(in <= Path::kMergeCand && topk <= Path::kMergeAbove ? "wide: merged lists" hit(in <= Path::kMergeCand && topk <= Path::kMergeAbove ? "wide: merged lists"
: in <= Path::kWideCand ? "wide: the threshold's bin in LDS" : "wide: the threshold's bin over global memory"); : in <= Path::kWideCand ? "wide: the threshold's bin in LDS" : "wide: the threshold's bin over global memory");
} }
@@ -227,10 +249,10 @@ int run(Fix& f, Form form) {
tens(f.cu.data(), RAD_I32, {f.S + 1}), tens(se.data(), RAD_I32, {M, W}, f.o_ld), tens(f.cu.data(), RAD_I32, {f.S + 1}), tens(se.data(), RAD_I32, {M, W}, f.o_ld),
tens(ns.data(), RAD_I32, {M}), tens(su.data(), RAD_I32, {M}), tens(ns.data(), RAD_I32, {M}), tens(su.data(), RAD_I32, {M}),
}; };
RadParam p[5]{}; RadParam p[6]{};
p[0] = {"M", RAD_P_INT, M}; p[1] = {"blocks", RAD_P_INT, f.nb()}; p[2] = {"topk", RAD_P_INT, f.topk}; p[0] = {"M", RAD_P_INT, M}; p[1] = {"blocks", RAD_P_INT, f.nb()}; p[2] = {"topk", RAD_P_INT, f.topk};
p[3] = {"ratio", RAD_P_INT, f.ratio}; p[4] = {"seqs", RAD_P_INT, f.S}; p[3] = {"ratio", RAD_P_INT, f.ratio}; p[4] = {"seqs", RAD_P_INT, f.S}; p[5] = {"signed", RAD_P_INT, 1};
RadArgs a{}; a.t = t; a.n_t = 8; a.p = p; a.n_p = 5; RadArgs a{}; a.t = t; a.n_t = 8; a.p = p; a.n_p = f.sgn ? 6 : 5;
const int64_t need = r4d_rad_qsa_select_scratch(&a); const int64_t need = r4d_rad_qsa_select_scratch(&a);
std::vector<uint32_t> scratch((size_t)(need / 4 + 1), 0xA5A5A5A5u); std::vector<uint32_t> scratch((size_t)(need / 4 + 1), 0xA5A5A5A5u);
if (form == kAuto) { a.scratch = scratch.data(); a.scratch_bytes = need; } if (form == kAuto) { a.scratch = scratch.data(); a.scratch_bytes = need; }
@@ -245,7 +267,9 @@ int run(Fix& f, Form form) {
: "qsa_select_kernel"; : "qsa_select_kernel";
const char* fn = kFormName[form]; const char* fn = kFormName[form];
if (rc != RAD_OK) { std::printf("FAIL %-48s %-6s launch returned %d\n", f.name.c_str(), fn, rc); return 1; } if (rc != RAD_OK) { std::printf("FAIL %-48s %-6s launch returned %d\n", f.name.c_str(), fn, rc); return 1; }
if (std::strcmp(emu_last_kernel, expect) != 0 || grids != (split ? 2 : 1)) { /* The launch site names the template, `kernel<Signed>`: the name is what precedes it. */
const std::string ran(emu_last_kernel, std::strcspn(emu_last_kernel, "<"));
if (ran != expect || grids != (split ? 2 : 1)) {
std::printf("FAIL %-48s %-6s ran %s in %d grid(s), expected %s in %d\n", f.name.c_str(), fn, std::printf("FAIL %-48s %-6s ran %s in %d grid(s), expected %s in %d\n", f.name.c_str(), fn,
emu_last_kernel, grids, expect, split ? 2 : 1); emu_last_kernel, grids, expect, split ? 2 : 1);
return 1; return 1;
@@ -324,8 +348,11 @@ Fix fix_prefill() {
* score distributions. Over 4096 blocks at six rows, the automatic choice is the split path. */ * score distributions. Over 4096 blocks at six rows, the automatic choice is the split path. */
Fix fix_wide(int dist) { Fix fix_wide(int dist) {
static const char* const kDist[] = { "continuous", "coarse, heavy ties", "an eighth in one first-digit bin", static const char* const kDist[] = { "continuous", "coarse, heavy ties", "an eighth in one first-digit bin",
"all in one first-digit bin", "fine ties at the threshold" }; "all in one first-digit bin", "fine ties at the threshold",
"signed, heavy ties", "signed, all negative, heavy ties",
"signed, continuous across zero" };
Fix f; f.name = std::string("wide: ") + kDist[dist]; Fix f; f.name = std::string("wide: ") + kDist[dist];
f.sgn = dist >= 5;
const int S = 2, nb = 9000, topk = 512, ratio = 4, pages = 1 << 20; const int S = 2, nb = 9000, topk = 512, ratio = 4, pages = 1 << 20;
f.shape(S, 6, nb, nb, topk, ratio); f.shape(S, 6, nb, nb, topk, ratio);
f.cu = { 0, 4, 6 }; f.cu = { 0, 4, 6 };
@@ -341,6 +368,9 @@ Fix fix_wide(int dist) {
case 1: v = (float)(u % 23) * 0.25f; break; case 1: v = (float)(u % 23) * 0.25f; break;
case 2: v = u % 8 == 0 ? 1.0f + (float)(u % 60000) * 1e-6f : (float)(u % 1000) / 2000.0f; break; case 2: v = u % 8 == 0 ? 1.0f + (float)(u % 60000) * 1e-6f : (float)(u % 1000) / 2000.0f; break;
case 3: v = 1.0f + (float)(u % 60000) * 1e-6f; break; case 3: v = 1.0f + (float)(u % 60000) * 1e-6f; break;
case 5: v = (float)(u % 23) * 0.25f - 3.0f; break;
case 6: v = -(float)(u % 23) * 0.25f - 1.0f; break;
case 7: v = (float)(u % 1000003) / 1000.0f - 500.0f; break;
default: v = (float)(u % 4999) * 0.01f; break; default: v = (float)(u % 4999) * 0.01f; break;
} }
} }
@@ -551,6 +581,22 @@ Fix fix_random(int it) {
return f; return f;
} }
/* A fixture under the signed order, its finite scores negated on odd rows when `flip` and moved
* by `shift`, so live scores straddle zero or fall below it. */
Fix as_signed(Fix f, float shift, bool flip = false) {
f.sgn = true;
f.name = "signed " + f.name;
if (shift == 0.0f && !flip) return f; /* as built: keeps -0 apart from +0 */
for (int m = 0; m < f.M; ++m)
for (int b = 0; b < f.nb_s; ++b) {
float& v = f.score(m, b);
if (!std::isfinite(v)) continue;
if (flip && m % 2) v = -v;
v += shift;
}
return f;
}
} // namespace } // namespace
/* No argument: the fixtures above and the branch check. `sweep N`: N fix_random fixtures, about /* No argument: the fixtures above and the branch check. `sweep N`: N fix_random fixtures, about
@@ -562,12 +608,20 @@ int main(int argc, char** argv) {
if (sweep) { if (sweep) {
const int n = argc > 2 ? std::atoi(argv[2]) : 100; const int n = argc > 2 ? std::atoi(argv[2]) : 100;
for (int i = 0; i < n; ++i) fixtures.push_back([i] { return fix_random(i); }); for (int i = 0; i < n; ++i) fixtures.push_back([i] { return fix_random(i); });
for (int i = 0; i < n; ++i) fixtures.push_back([i] { return as_signed(fix_random(i), -1.0f, true); });
} else { } else {
fixtures = { fix_decode, fix_prefill, fix_few_live, fix_wide_launch, fixtures = { fix_decode, fix_prefill, fix_few_live, fix_wide_launch,
[] { return fix_split(1, 4); }, [] { return fix_split(2, 4); }, fix_split_sparse, fix_big_topk, [] { return fix_split(1, 4); }, [] { return fix_split(2, 4); }, fix_split_sparse, fix_big_topk,
[] { return fix_no_split(true); }, [] { return fix_no_split(false); } }; [] { return fix_no_split(true); }, [] { return fix_no_split(false); } };
for (int d = 0; d < 5; ++d) fixtures.push_back([d] { return fix_wide(d); }); for (int d = 0; d < 5; ++d) fixtures.push_back([d] { return fix_wide(d); });
for (int i = 0; i < 150; ++i) fixtures.push_back([i] { return fix_sweep(i); }); for (int i = 0; i < 150; ++i) fixtures.push_back([i] { return fix_sweep(i); });
fixtures.push_back([] { return as_signed(fix_decode(), -5.0f); });
fixtures.push_back([] { return as_signed(fix_decode(), -20.0f); });
fixtures.push_back([] { return as_signed(fix_few_live(), 0.0f); });
fixtures.push_back([] { return as_signed(fix_split(2, 4), -6.0f); });
fixtures.push_back([] { return as_signed(fix_wide_launch(), -1.0f, true); });
for (int d = 5; d < 8; ++d) fixtures.push_back([d] { return fix_wide(d); });
for (int i = 0; i < 150; ++i) fixtures.push_back([i] { return as_signed(fix_sweep(i), 0.0f); });
} }
int fails = 0, launches = 0; int fails = 0, launches = 0;
for (auto& make : fixtures) { for (auto& make : fixtures) {
@@ -592,6 +646,7 @@ int main(int argc, char** argv) {
"wide: the threshold's bin in LDS", "wide: the threshold's bin over global memory", "wide: the threshold's bin in LDS", "wide: the threshold's bin over global memory",
"split: topk or fewer live, every one taken", "split: no chunk over topk, more than topk live", "split: topk or fewer live, every one taken", "split: no chunk over topk, more than topk live",
"split: a chunk over topk", "split: exactly topk candidates, from a chunk over topk", "split: a chunk over topk", "split: exactly topk candidates, from a chunk over topk",
"signed: a negative threshold",
}; };
int missing = 0; int missing = 0;
for (const char* p : kRequired) for (const char* p : kRequired)