Archived
ops: attention sinks and LiLiCorr's correlator
For NVIDIA's LiLiCorr drafter: - attn_paged_sink: attn_paged with a learned logit a query head beside the keys, in libref, libavx and libr4d (the windowed kernels at h128, gqa 8 and 16) - sample_logprobs without params writes every row (a drafter's candidate logprobs) - lili_cand, lili_attn, lili_factor, lili_walk: the correlator's candidates and features, its attention over the candidate lattice, the factor input and the greedy walk ref_test, avx_check_hand and r4d_emu cases; docs/OPS.md.
This commit is contained in:
1 parent
44490c859a
commit
3b278dcec3
26 files changed
+1904
-55
No files matched your search
@@ -539,11 +539,12 @@ set(R4D_EMU_HIP ${R4D_DIR}/r4d_attn_paged_window.hip ${R4D_DIR}/r4d_pair_argmax_
|
||||
${R4D_DIR}/r4d_mhc.hip ${R4D_DIR}/r4d_fp8_f32scale.hip ${R4D_DIR}/r4d_kda.hip
|
||||
${R4D_DIR}/r4d_moe_mxfp4.hip ${R4D_DIR}/r4d_mla.hip ${R4D_DIR}/r4d_quant_act_fp8.hip
|
||||
${R4D_DIR}/r4d_kv_resolve.hip ${R4D_DIR}/r4d_dsa.hip
|
||||
${R4D_DIR}/r4d_gemm_m2xfp4a8_decode.hip ${R4D_DIR}/r4d_gemm_m2xfp4a8_tiled.hip)
|
||||
${R4D_DIR}/r4d_gemm_m2xfp4a8_decode.hip ${R4D_DIR}/r4d_gemm_m2xfp4a8_tiled.hip
|
||||
${R4D_DIR}/r4d_lili.hip)
|
||||
# The M2XFP4 units' plugin entries call two cfg readers that r4d_rows.cpp defines, which is not in
|
||||
# this build: cfg_stub.cpp defines them at their axis defaults, in both arms below.
|
||||
set(R4D_EMU_TESTS attn pick norm escale qsa lora f32 mhc fp8f32 kda moemx mla moefuse kvres
|
||||
dsa m2xfp4)
|
||||
dsa m2xfp4 lili)
|
||||
set(R4D_EMU_BUILT OFF)
|
||||
if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64)$")
|
||||
# No fiber switch for this ISA.
|
||||
@@ -584,7 +585,8 @@ else()
|
||||
${R4D_EMU}/test_f32.cpp ${R4D_EMU}/test_mhc.cpp ${R4D_EMU}/test_fp8f32.cpp
|
||||
${R4D_EMU}/test_kda.cpp ${R4D_EMU}/test_moemx.cpp ${R4D_EMU}/test_mla.cpp
|
||||
${R4D_EMU}/test_moefuse.cpp ${R4D_EMU}/test_kvres.cpp
|
||||
${R4D_EMU}/test_dsa.cpp ${R4D_EMU}/test_m2xfp4.cpp ${R4D_EMU}/cfg_stub.cpp)
|
||||
${R4D_EMU}/test_dsa.cpp ${R4D_EMU}/test_m2xfp4.cpp ${R4D_EMU}/test_lili.cpp
|
||||
${R4D_EMU}/cfg_stub.cpp)
|
||||
get_filename_component(stem ${src} NAME_WE)
|
||||
set(lang "")
|
||||
if(src MATCHES "\\.hip$")
|
||||
@@ -621,7 +623,7 @@ else()
|
||||
endif()
|
||||
endif()
|
||||
if(R4D_EMU_BUILT)
|
||||
foreach(t pick norm escale qsa lora f32 mhc fp8f32 kda moemx mla moefuse kvres dsa m2xfp4)
|
||||
foreach(t pick norm escale qsa lora f32 mhc fp8f32 kda moemx mla moefuse kvres dsa m2xfp4 lili)
|
||||
add_test(NAME r4d_emu_${t} COMMAND ${CMAKE_BINARY_DIR}/tests/r4d_emu_${t})
|
||||
endforeach()
|
||||
# The attention cases take about 11 s, 7 of them in the two long prefills on the WMMA kernel,
|
||||
@@ -632,7 +634,7 @@ if(R4D_EMU_BUILT)
|
||||
endforeach()
|
||||
set_tests_properties(r4d_emu_attn_shard0 r4d_emu_attn_shard1 r4d_emu_attn_shard2 r4d_emu_pick
|
||||
r4d_emu_norm r4d_emu_escale r4d_emu_qsa r4d_emu_lora r4d_emu_mla r4d_emu_moefuse
|
||||
r4d_emu_dsa r4d_emu_m2xfp4
|
||||
r4d_emu_dsa r4d_emu_m2xfp4 r4d_emu_lili
|
||||
PROPERTIES LABELS "r4d_emu" TIMEOUT ${RAD_TEST_TIMEOUT})
|
||||
# The LoRA pair has a libref oracle, and the test holds the emulated kernels to it when it can
|
||||
# load one -- expand bit for bit, shrink to its bound. kda_update is held to libref's within
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/* Drives libr4d's windowed attn_paged entry points (r4d_attn_paged_window.hip) through the SIMT
|
||||
* emulator and checks every output element against a port of libref's ref_attn_paged (same mask
|
||||
* predicates, f64 accumulation). */
|
||||
* predicates, f64 accumulation). The attn_paged_sink entries too: a sink logit a query head joins
|
||||
* the reference's maximum and sum. */
|
||||
#include "rad_abi.h"
|
||||
#include "rad_plugin.h"
|
||||
#include "r4d_plugin.h"
|
||||
@@ -38,6 +39,7 @@ struct Case {
|
||||
int qpitch_extra = 0; /* q as a column slice of a wider buffer */
|
||||
int expect_wmma = -1; /* 1: must take the WMMA kernel, 0: must not, -1: either */
|
||||
bool ahead = false; /* non-causal, the window bounds the keys behind only */
|
||||
bool sink = false; /* attn_paged_sink: a sink logit a query head */
|
||||
};
|
||||
|
||||
using Fn = int (*)(const RadArgs*, RadStream);
|
||||
@@ -54,6 +56,13 @@ static Fn pick(int hd, int gqa, bool fp8, bool ahead) {
|
||||
#undef P
|
||||
return nullptr;
|
||||
}
|
||||
static Fn pick_sink(int hd, int gqa, bool fp8) {
|
||||
if (hd == 128 && gqa == 8)
|
||||
return fp8 ? r4d_rad_attn_window_sink_h128_gqa8_fp8 : r4d_rad_attn_window_sink_h128_gqa8_bf16;
|
||||
if (hd == 128 && gqa == 16)
|
||||
return fp8 ? r4d_rad_attn_window_sink_h128_gqa16_fp8 : r4d_rad_attn_window_sink_h128_gqa16_bf16;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static int run(const Case& c) {
|
||||
const int n_seq = (int)c.qlen.size(), HD = c.hd, kvh = c.kvh, qh = kvh * c.gqa;
|
||||
@@ -101,7 +110,11 @@ static int run(const Case& c) {
|
||||
std::vector<char> scratch(c.scratch + 64);
|
||||
char* sp = scratch.data() + ((16 - ((uintptr_t)scratch.data() & 15)) & 15);
|
||||
|
||||
RadTensor t[8];
|
||||
/* Sinks spread around the scores' scale, a few far above them (the sink takes the maximum) and
|
||||
* one far below (it vanishes). */
|
||||
std::vector<float> sinks(qh);
|
||||
for (int h = 0; h < qh; ++h) sinks[h] = h % 5 == 3 ? 6.f + nrm() : h % 7 == 6 ? -40.f : 1.5f * nrm();
|
||||
RadTensor t[9];
|
||||
t[0] = tens(qb.data(), RAD_BF16, {rows, qh * HD}); t[0].stride[0] = qcols;
|
||||
t[1] = tens(c.fp8 ? (void*)kv8.data() : (void*)kvb.data(), c.fp8 ? RAD_F8E4M3 : RAD_BF16, {nblocks, kvh, 16, slot});
|
||||
t[2] = tens(bt.data(), RAD_I32, {n_seq, maxb});
|
||||
@@ -110,6 +123,7 @@ static int run(const Case& c) {
|
||||
t[5] = c.descale ? tens(vd.data(), RAD_F32, {n_seq, kvh}) : RadTensor{};
|
||||
t[6] = c.ragged ? tens(cu.data(), RAD_I32, {n_seq + 1}) : RadTensor{};
|
||||
t[7] = tens(out.data(), RAD_BF16, {rows, qh, HD});
|
||||
if (c.sink) { t[8] = t[7]; t[7] = tens(sinks.data(), RAD_F32, {qh}); }
|
||||
int qmax = 0; for (int q : c.qlen) qmax = std::max(qmax, q);
|
||||
std::vector<RadParam> p;
|
||||
auto pi = [&](const char* k, long long v) { RadParam x{}; x.key = k; x.kind = RAD_P_INT; x.ival = v; p.push_back(x); };
|
||||
@@ -121,9 +135,10 @@ static int run(const Case& c) {
|
||||
const double scale = 1.0 / std::sqrt((double)HD) * 1.3; /* not the default: the param must be read */
|
||||
pf("scale", scale); pi("n_head", qh); pi("max_seqs", n_seq);
|
||||
if (c.max_ctx) pi("max_ctx", c.max_ctx);
|
||||
RadArgs a{}; a.t = t; a.n_t = 8; a.p = p.data(); a.n_p = (int)p.size();
|
||||
RadArgs a{}; a.t = t; a.n_t = c.sink ? 9 : 8; a.p = p.data(); a.n_p = (int)p.size();
|
||||
a.scratch = c.scratch ? sp : nullptr; a.scratch_bytes = c.scratch;
|
||||
Fn fn = pick(HD, c.gqa, c.fp8, c.ahead);
|
||||
Fn fn = c.sink ? pick_sink(HD, c.gqa, c.fp8) : pick(HD, c.gqa, c.fp8, c.ahead);
|
||||
if (!fn) { std::printf("%-44s no entry point for this geometry\n", c.name.c_str()); return 1; }
|
||||
const int g0 = emu_grids;
|
||||
const int rc = fn(&a, nullptr);
|
||||
const int launches = emu_grids - g0;
|
||||
@@ -146,13 +161,16 @@ static int run(const Case& c) {
|
||||
sc[j] = dot * scale; mx = std::max(mx, sc[j]);
|
||||
}
|
||||
std::vector<double> acc(HD, 0.0); double sum = 0;
|
||||
if (mx > -INFINITY)
|
||||
const bool any = mx > -INFINITY;
|
||||
if (c.sink && any) mx = std::max(mx, (double)sinks[h]);
|
||||
if (any)
|
||||
for (int j = 0; j < c.ctx[s]; ++j) {
|
||||
if (sc[j] == -INFINITY) continue;
|
||||
const double w = std::exp(sc[j] - mx); sum += w;
|
||||
const int64_t kb = (((int64_t)bt[s * maxb + j / 16] * kvh + kh) * 16 + j % 16) * slot;
|
||||
for (int d = 0; d < HD; ++d) acc[d] += w * kvf[kb + HD + d] * vd[s * kvh + kh];
|
||||
}
|
||||
if (c.sink && any) sum += std::exp((double)sinks[h] - mx);
|
||||
for (int d = 0; d < HD; ++d) {
|
||||
const double ref = sum > 0 ? acc[d] / sum : 0.0;
|
||||
const float got = rad_bf16_to_f32(out[((size_t)tok * qh + h) * HD + d]);
|
||||
@@ -244,6 +262,28 @@ int main(int argc, char** argv) {
|
||||
{"h256 gqa4 ahead w20 split", 256, 4, 1, false, false, false, false, 20, {5}, {300}, -1, 4096, s256, 0, 0, true},
|
||||
{"wmma h256 gqa2 ahead w20 picture 100 of 100", 256, 2, 2, false, false, false, false, 20, {100}, {100}, -1, 0, 0, 0, 1, true},
|
||||
{"wmma h256 gqa2 ahead w20 picture 90 of 300 fp8", 256, 2, 2, true, false, false, true, 20, {90}, {300}, -1, 0, 0, 0, 1, true},
|
||||
/* LiLiCorr's drafter (attn_paged_sink): 32 query heads over 2 KV heads at h128 (16 over 1 at
|
||||
* TP 2), a block of 8 non-causal rows past the context, a 1024-token window; one sequence
|
||||
* and a batch, unsplit and split, a bf16 and an fp8 cache */
|
||||
{"sink h128 gqa16 kvh2 block8 w1024", 128, 16, 2, false, false, false, false, 1024, {8}, {1500},
|
||||
-1, 0, 0, 0, -1, false, true},
|
||||
{"sink h128 gqa16 kvh1 block8 x3 split", 128, 16, 1, false, false, true, false, 1024, {8, 8, 8},
|
||||
{900, 40, 2100}, -1, 262144, s128, 0, -1, false, true},
|
||||
{"sink h128 gqa16 kvh2 fp8+descale split", 128, 16, 2, true, false, false, true, 1024, {8}, {1300},
|
||||
-1, 262144, s128, 0, -1, false, true},
|
||||
{"sink h128 gqa16 prefill-shaped stays scalar", 128, 16, 1, false, false, false, false, 1024, {96},
|
||||
{400}, -1, 0, 0, 0, 0, false, true},
|
||||
{"sink h128 gqa16 missing-block", 128, 16, 1, false, false, false, false, 1024, {8}, {70}, 1,
|
||||
0, 0, 0, -1, false, true},
|
||||
/* and at TP 4, 8 query heads over the rank's replicated KV head */
|
||||
{"sink h128 gqa8 kvh1 block8 w1024", 128, 8, 1, false, false, false, false, 1024, {8}, {1500},
|
||||
-1, 0, 0, 0, -1, false, true},
|
||||
{"sink h128 gqa8 kvh1 block8 x3 split", 128, 8, 1, false, false, true, false, 1024, {8, 8, 8},
|
||||
{900, 40, 2100}, -1, 262144, s128, 0, -1, false, true},
|
||||
{"sink h128 gqa8 kvh1 fp8+descale split", 128, 8, 1, true, false, false, true, 1024, {8}, {1300},
|
||||
-1, 262144, s128, 0, -1, false, true},
|
||||
{"sink h128 gqa8 prefill-shaped stays scalar", 128, 8, 1, false, false, false, false, 1024, {96},
|
||||
{400}, -1, 0, 0, 0, 0, false, true},
|
||||
/* and what must stay scalar: a verify batch, many sequences, a few rows each */
|
||||
{"scalar: 8 x verify16, ragged", 512, 4, 1, false, true, true, false, 0, {16, 16, 16, 16, 16, 16, 16, 16},
|
||||
{40, 50, 60, 70, 80, 90, 100, 110}, -1, 262144, s512, 0, 0},
|
||||
|
||||
@@ -0,0 +1,267 @@
|
||||
/* LiLiCorr's correlator (r4d_lili.hip) through the SIMT emulator, at the drafter's geometry: seven
|
||||
* positions of eight candidates, eight heads of 128, a 1024-wide lattice. Each kernel against a host
|
||||
* port of libref's ref_lili.cpp in double: lili_cand over two ranks' disjoint vocabulary halves (ids
|
||||
* exact, features and the additive part to bf16), lili_attn and lili_factor to bf16, lili_walk's
|
||||
* scores to f32 rounding and its tokens exactly wherever the winning margin is not a near-tie. */
|
||||
#include "rad_abi.h"
|
||||
#include "rad_plugin.h"
|
||||
#include "r4d_plugin.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <random>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
static std::mt19937 rng(77);
|
||||
static float nrm() { static std::normal_distribution<float> d(0.f, 1.f); return d(rng); }
|
||||
|
||||
static RadTensor tens(void* p, uint32_t dt, std::vector<int64_t> shape) {
|
||||
RadTensor t{}; t.data = p; t.dtype = dt; t.rank = (uint32_t)shape.size();
|
||||
int64_t st = 1;
|
||||
for (int i = (int)shape.size() - 1; i >= 0; --i) { t.shape[i] = shape[i]; t.stride[i] = st; st *= shape[i]; }
|
||||
return t;
|
||||
}
|
||||
static std::vector<uint16_t> bfv(size_t n, float sc, std::vector<float>* f) {
|
||||
std::vector<uint16_t> b(n);
|
||||
f->resize(n);
|
||||
for (size_t i = 0; i < n; ++i) { b[i] = rad_f32_to_bf16(nrm() * sc); (*f)[i] = rad_bf16_to_f32(b[i]); }
|
||||
return b;
|
||||
}
|
||||
struct Params {
|
||||
std::vector<RadParam> p;
|
||||
std::vector<std::string> s;
|
||||
void i(const char* k, long long v) { RadParam x{}; x.key = k; x.kind = RAD_P_INT; x.ival = v; p.push_back(x); }
|
||||
void f(const char* k, double v) { RadParam x{}; x.key = k; x.kind = RAD_P_F64; x.dval = v; p.push_back(x); }
|
||||
void str(const char* k, const char* v) { RadParam x{}; x.key = k; x.kind = RAD_P_STR; x.sval = v; p.push_back(x); }
|
||||
};
|
||||
/* rel_l2 and the worst element relative to the reference's scale */
|
||||
static bool close(const char* what, const std::vector<double>& ref, const std::vector<float>& got, double tol) {
|
||||
double se = 0, sr = 0, worst = 0, scale = 1e-30;
|
||||
long nan = 0;
|
||||
for (double r : ref) scale = std::max(scale, std::fabs(r));
|
||||
for (size_t i = 0; i < ref.size(); ++i) {
|
||||
if (std::isnan(got[i])) { ++nan; continue; }
|
||||
const double e = std::fabs(got[i] - ref[i]);
|
||||
se += e * e; sr += ref[i] * ref[i];
|
||||
worst = std::max(worst, e / scale);
|
||||
}
|
||||
const double rel = std::sqrt(se / std::max(sr, 1e-30));
|
||||
const bool ok = nan == 0 && rel < tol && worst < 8 * tol;
|
||||
std::printf(" %-34s rel_l2 %.2e worst %.2e nan %ld %s\n", what, rel, worst, nan, ok ? "PASS" : "FAIL");
|
||||
return ok;
|
||||
}
|
||||
|
||||
static int test_cand() {
|
||||
const int K = 8, RK = 9, W = 2, S = 7, N = 1024, M = 2 * S, V = 131072, w = 2 + 2 * RK;
|
||||
/* stats: each rank's row of its own vocabulary half, sorted, a tie across the ranks on row 3 */
|
||||
std::vector<float> st((size_t)M * W * w);
|
||||
std::vector<double> lse(M);
|
||||
std::vector<std::vector<std::pair<float, int>>> all(M);
|
||||
for (int m = 0; m < M; ++m) {
|
||||
double tot = 0, gmx = -INFINITY;
|
||||
std::vector<double> mxr(W), sumr(W);
|
||||
for (int r = 0; r < W; ++r) {
|
||||
std::vector<std::pair<float, int>> v;
|
||||
for (int j = 0; j < K; ++j) v.push_back({ 2.f * nrm() + 3.f, r * (V / W) + (int)(rng() % (V / W)) });
|
||||
if (m == 3 && r == 1) v[0] = { all[m][0].first, all[m][0].second + V / W }; /* a tie */
|
||||
std::sort(v.begin(), v.end(), [](auto& a, auto& b) { return a.first > b.first || (a.first == b.first && a.second < b.second); });
|
||||
const float mx = v[0].first, sum = 1.0f + 300.f * (float)(rng() % 100) / 100.f;
|
||||
float* row = &st[((size_t)m * W + r) * w];
|
||||
row[0] = mx; row[1] = sum;
|
||||
for (int j = 0; j < K; ++j) { row[2 + 2 * j] = (float)v[j].second; row[3 + 2 * j] = v[j].first; all[m].push_back(v[j]); }
|
||||
/* the ninth pair, below the eighth: carried for the all-gather's 16-byte rows, never kept */
|
||||
row[2 + 2 * K] = (float)(r * (V / W)); row[3 + 2 * K] = v[K - 1].first - 1.0f;
|
||||
mxr[r] = mx; sumr[r] = sum; gmx = std::max(gmx, (double)mx);
|
||||
}
|
||||
for (int r = 0; r < W; ++r) tot += sumr[r] * std::exp(mxr[r] - gmx);
|
||||
lse[m] = gmx + std::log(tot);
|
||||
std::sort(all[m].begin(), all[m].end(), [](auto& a, auto& b) { return a.first > b.first || (a.first == b.first && a.second < b.second); });
|
||||
}
|
||||
std::vector<float> pasf, slotf, rankf;
|
||||
auto pas = bfv((size_t)M * N, 1.f, &pasf), slot = bfv((size_t)S * N, 0.5f, &slotf), rank = bfv((size_t)K * N, 0.5f, &rankf);
|
||||
std::vector<float> lnw(5), lnb(5), upw((size_t)N * 5), upb(N);
|
||||
for (auto* v : { &lnw, &lnb, &upw, &upb }) for (auto& x : *v) x = nrm() * 0.7f;
|
||||
std::vector<int32_t> cand((size_t)M * K, -7);
|
||||
std::vector<uint16_t> feat((size_t)M * K * N), base((size_t)M * K * N);
|
||||
RadTensor t[11] = {
|
||||
tens(st.data(), RAD_F32, { M, W * w }), tens(pas.data(), RAD_BF16, { M, N }),
|
||||
tens(lnw.data(), RAD_F32, { 5 }), tens(lnb.data(), RAD_F32, { 5 }),
|
||||
tens(upw.data(), RAD_F32, { N, 5 }), tens(upb.data(), RAD_F32, { N }),
|
||||
tens(slot.data(), RAD_BF16, { S, N }), tens(rank.data(), RAD_BF16, { K, N }),
|
||||
tens(cand.data(), RAD_I32, { M, K }), tens(feat.data(), RAD_BF16, { M * K, N }),
|
||||
tens(base.data(), RAD_BF16, { M * K, N }),
|
||||
};
|
||||
Params P;
|
||||
P.i("M", M); P.i("k", K); P.i("world", W); P.i("steps", S); P.i("n", N); P.str("dtype", "bf16"); P.f("eps", 1e-5);
|
||||
P.i("row_k", RK);
|
||||
RadArgs a{}; a.t = t; a.n_t = 11; a.p = P.p.data(); a.n_p = (int)P.p.size();
|
||||
const int rc = r4d_rad_lili_cand(&a, nullptr);
|
||||
if (rc != RAD_OK) { std::printf(" lili_cand launch %d\n", rc); return 1; }
|
||||
bool ok = true;
|
||||
std::vector<double> rf, rb;
|
||||
std::vector<float> gf, gb;
|
||||
for (int m = 0; m < M; ++m) {
|
||||
double lp[8];
|
||||
for (int j = 0; j < K; ++j) {
|
||||
ok = ok && cand[(size_t)m * K + j] == all[m][j].second;
|
||||
lp[j] = all[m][j].first - lse[m];
|
||||
}
|
||||
for (int j = 0; j < K; ++j) {
|
||||
const double f[5] = { lp[j], std::exp(lp[j]), lp[j] - lp[0], (double)j / (K - 1), j == 0 ? 1.0 : 0.0 };
|
||||
double mean = 0, var = 0;
|
||||
for (double x : f) mean += x;
|
||||
mean /= 5;
|
||||
for (double x : f) var += (x - mean) * (x - mean);
|
||||
var /= 5;
|
||||
double y[5];
|
||||
for (int i = 0; i < 5; ++i) y[i] = (f[i] - mean) / std::sqrt(var + 1e-5) * lnw[i] + lnb[i];
|
||||
for (int c = 0; c < N; ++c) {
|
||||
double u = upb[c];
|
||||
for (int i = 0; i < 5; ++i) u += upw[(size_t)c * 5 + i] * y[i];
|
||||
rf.push_back(u / (1 + std::exp(-u)));
|
||||
rb.push_back((double)pasf[(size_t)m * N + c] + slotf[(size_t)(m % S) * N + c] + rankf[(size_t)j * N + c]);
|
||||
gf.push_back(rad_bf16_to_f32(feat[((size_t)m * K + j) * N + c]));
|
||||
gb.push_back(rad_bf16_to_f32(base[((size_t)m * K + j) * N + c]));
|
||||
}
|
||||
}
|
||||
}
|
||||
std::printf(" lili_cand ids (two ranks, a tie) %s\n", ok ? "PASS" : "FAIL");
|
||||
ok = close("lili_cand features", rf, gf, 5e-3) && ok;
|
||||
ok = close("lili_cand additive part", rb, gb, 5e-3) && ok;
|
||||
return ok ? 0 : 1;
|
||||
}
|
||||
|
||||
static int test_attn() {
|
||||
const int S = 7, K = 8, L = S * K, NH = 8, HD = 128, N = NH * HD, NS = 2, M = NS * L;
|
||||
std::vector<float> qf;
|
||||
auto qkv = bfv((size_t)M * 3 * N, 0.25f, &qf);
|
||||
std::vector<float> rel((size_t)NH * (2 * S + 1)), same(NH);
|
||||
for (auto& x : rel) x = nrm();
|
||||
for (auto& x : same) x = nrm();
|
||||
std::vector<uint16_t> out((size_t)M * N, 0xffff);
|
||||
RadTensor t[4] = { tens(qkv.data(), RAD_BF16, { M, 3 * N }), tens(rel.data(), RAD_F32, { NH, 2 * S + 1 }),
|
||||
tens(same.data(), RAD_F32, { NH }), tens(out.data(), RAD_BF16, { M, N }) };
|
||||
Params P;
|
||||
P.i("M", M); P.i("steps", S); P.i("k", K); P.i("n_head", NH); P.i("head_dim", HD); P.str("dtype", "bf16");
|
||||
RadArgs a{}; a.t = t; a.n_t = 4; a.p = P.p.data(); a.n_p = (int)P.p.size();
|
||||
const int rc = r4d_rad_lili_attn(&a, nullptr);
|
||||
if (rc != RAD_OK) { std::printf(" lili_attn launch %d\n", rc); return 1; }
|
||||
std::vector<double> ref((size_t)M * N);
|
||||
const double scale = 1.0 / std::sqrt((double)HD);
|
||||
for (int i = 0; i < M; ++i) for (int h = 0; h < NH; ++h) {
|
||||
const int q0 = i / L * L, si = (i - q0) / K;
|
||||
std::vector<double> sc(L);
|
||||
double mx = -INFINITY;
|
||||
for (int j = 0; j < L; ++j) {
|
||||
double d = 0;
|
||||
for (int e = 0; e < HD; ++e) d += (double)qf[(size_t)i * 3 * N + h * HD + e] * qf[(size_t)(q0 + j) * 3 * N + N + h * HD + e];
|
||||
const int sj = j / K;
|
||||
sc[j] = d * scale + rel[(size_t)h * (2 * S + 1) + si - sj + S] + (si == sj ? same[h] : 0.f);
|
||||
mx = std::max(mx, sc[j]);
|
||||
}
|
||||
double sum = 0;
|
||||
for (auto& x : sc) { x = std::exp(x - mx); sum += x; }
|
||||
for (int e = 0; e < HD; ++e) {
|
||||
double acc = 0;
|
||||
for (int j = 0; j < L; ++j) acc += sc[j] * qf[(size_t)(q0 + j) * 3 * N + 2 * N + h * HD + e];
|
||||
ref[(size_t)i * N + h * HD + e] = acc / sum;
|
||||
}
|
||||
}
|
||||
std::vector<float> got(out.size());
|
||||
for (size_t i = 0; i < out.size(); ++i) got[i] = rad_bf16_to_f32(out[i]);
|
||||
return close("lili_attn two lattices", ref, got, 5e-3) ? 0 : 1;
|
||||
}
|
||||
|
||||
static int test_factor() {
|
||||
const int L = 56, N = 1024, NS = 3, M = NS * L;
|
||||
std::vector<float> hf, af;
|
||||
auto h = bfv((size_t)M * N, 1.f, &hf), an = bfv((size_t)NS * N, 1.f, &af);
|
||||
std::vector<uint16_t> y((size_t)M * 3 * N);
|
||||
RadTensor t[3] = { tens(h.data(), RAD_BF16, { M, N }), tens(an.data(), RAD_BF16, { NS, N }),
|
||||
tens(y.data(), RAD_BF16, { M, 3 * N }) };
|
||||
Params P;
|
||||
P.i("M", M); P.i("L", L); P.i("n", N); P.str("dtype", "bf16");
|
||||
RadArgs a{}; a.t = t; a.n_t = 3; a.p = P.p.data(); a.n_p = (int)P.p.size();
|
||||
if (r4d_rad_lili_factor(&a, nullptr) != RAD_OK) { std::printf(" lili_factor launch failed\n"); return 1; }
|
||||
long bad = 0;
|
||||
for (int i = 0; i < M; ++i) for (int c = 0; c < N; ++c) {
|
||||
const uint16_t* r = &y[(size_t)i * 3 * N];
|
||||
bad += r[c] != h[(size_t)i * N + c];
|
||||
bad += r[N + c] != an[(size_t)(i / L) * N + c];
|
||||
bad += r[2 * N + c] != rad_f32_to_bf16(hf[(size_t)i * N + c] * af[(size_t)(i / L) * N + c]);
|
||||
}
|
||||
std::printf(" %-34s bad %ld %s\n", "lili_factor (bit for bit)", bad, bad ? "FAIL" : "PASS");
|
||||
return bad ? 1 : 0;
|
||||
}
|
||||
|
||||
static int test_walk() {
|
||||
const int S = 7, K = 8, N = 1024, M = 5, R = S * K;
|
||||
std::vector<float> ef, af;
|
||||
auto ed = bfv((size_t)M * R * 2 * N, 1.f, &ef), ao = bfv((size_t)M * N, 1.f, &af);
|
||||
std::vector<int32_t> cand((size_t)M * S * K), tok((size_t)M * S, -9);
|
||||
for (auto& c : cand) c = (int32_t)(rng() % 131072);
|
||||
std::vector<float> scores((size_t)M * S * K * K);
|
||||
RadTensor t[5] = { tens(ed.data(), RAD_BF16, { M * R, 2 * N }), tens(ao.data(), RAD_BF16, { M, N }),
|
||||
tens(cand.data(), RAD_I32, { M * S, K }), tens(tok.data(), RAD_I32, { M, S }),
|
||||
tens(scores.data(), RAD_F32, { M, S, K, K }) };
|
||||
Params P;
|
||||
P.i("M", M); P.i("steps", S); P.i("k", K); P.i("n", N); P.str("dtype", "bf16"); P.f("eps", 1e-4); P.f("scale", 8.0);
|
||||
RadArgs a{}; a.t = t; a.n_t = 5; a.p = P.p.data(); a.n_p = (int)P.p.size();
|
||||
if (r4d_rad_lili_walk(&a, nullptr) != RAD_OK) { std::printf(" lili_walk launch failed\n"); return 1; }
|
||||
std::vector<double> ref((size_t)M * S * K * K);
|
||||
long tbad = 0, near = 0;
|
||||
for (int m = 0; m < M; ++m) {
|
||||
auto vec = [&](const float* p, std::vector<double>& v) {
|
||||
double ss = 0;
|
||||
v.assign(p, p + N);
|
||||
for (double x : v) ss += x * x;
|
||||
const double inv = 1.0 / std::max(std::sqrt(ss), 1e-4);
|
||||
for (double& x : v) x *= inv;
|
||||
};
|
||||
std::vector<std::vector<double>> vo(R), vi(R);
|
||||
std::vector<double> va;
|
||||
vec(&af[(size_t)m * N], va);
|
||||
for (int r = 0; r < R; ++r) {
|
||||
vec(&ef[((size_t)m * R + r) * 2 * N], vo[r]);
|
||||
vec(&ef[((size_t)m * R + r) * 2 * N + N], vi[r]);
|
||||
}
|
||||
auto dot = [&](const std::vector<double>& x, const std::vector<double>& y) {
|
||||
double s = 0;
|
||||
for (int c = 0; c < N; ++c) s += x[c] * y[c];
|
||||
return 8.0 * s;
|
||||
};
|
||||
double* sc = &ref[(size_t)m * S * K * K];
|
||||
for (int j = 0; j < K; ++j) { const double v = dot(va, vi[j]); for (int i = 0; i < K; ++i) sc[i * K + j] = v; }
|
||||
for (int s = 1; s < S; ++s) for (int i = 0; i < K; ++i) for (int j = 0; j < K; ++j)
|
||||
sc[(s * K + i) * K + j] = dot(vo[(s - 1) * K + i], vi[s * K + j]);
|
||||
/* The walk on the reference scores; a step whose two best differ by less than f32 noise
|
||||
* may legitimately go the other way, and then the rest of the path is not compared. */
|
||||
int prev = 0;
|
||||
for (int s = 0; s < S; ++s) {
|
||||
const double* row = sc + (s * K + prev) * K;
|
||||
int best = 0;
|
||||
for (int j = 1; j < K; ++j) if (row[j] > row[best]) best = j;
|
||||
double second = -INFINITY;
|
||||
for (int j = 0; j < K; ++j) if (j != best) second = std::max(second, row[j]);
|
||||
if (row[best] - second < 1e-4) { ++near; break; }
|
||||
tbad += tok[(size_t)m * S + s] != cand[((size_t)m * S + s) * K + best];
|
||||
prev = best;
|
||||
}
|
||||
}
|
||||
std::printf(" %-34s bad %ld near-ties %ld %s\n", "lili_walk tokens", tbad, near, tbad ? "FAIL" : "PASS");
|
||||
const bool ok = close("lili_walk scores", ref, scores, 1e-4) && tbad == 0;
|
||||
return ok ? 0 : 1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int fails = 0;
|
||||
fails += test_cand();
|
||||
fails += test_attn();
|
||||
fails += test_factor();
|
||||
fails += test_walk();
|
||||
std::printf("lili: %d failure(s)\n", fails);
|
||||
return fails ? 1 : 0;
|
||||
}
|
||||
+62
-3
@@ -1079,6 +1079,7 @@ TEST(op_vocabulary) {
|
||||
"lora_shrink", "lora_expand", "lora_expand_mm",
|
||||
"grid_embed_aa",
|
||||
"attn_paged", "attn_dense", "kv_store", "kv_store_streamed", "rope", "rope_table",
|
||||
"attn_paged_sink",
|
||||
"qk_norm_rope", "qsa_block_key",
|
||||
/* libr4d's row-form names, served by the base op's row. */
|
||||
"rmsnorm_rows", "rope_rows", "gemm_nt_q_short", "gemm_nt_q_gated_wide",
|
||||
@@ -1149,6 +1150,8 @@ TEST(op_vocabulary) {
|
||||
/* A pipelined prefill's first-piece KV length, a device length less a device offset. */
|
||||
"sub_i32",
|
||||
"position_scale", "embed_attn", "ministral_rope", "pixtral_rope", "pixtral_merge",
|
||||
/* LiLiCorr's candidate correlator (Nemotron-3.5-Bolt's drafter). */
|
||||
"lili_cand", "lili_attn", "lili_factor", "lili_walk",
|
||||
};
|
||||
for (const char* op : kOps) {
|
||||
if (!kern(op)) fprintf(stderr, " missing op '%s'\n", op);
|
||||
@@ -1157,16 +1160,18 @@ TEST(op_vocabulary) {
|
||||
CHECK_EQ(api().kernel_count(), (int)(sizeof(kOps) / sizeof(kOps[0])));
|
||||
|
||||
/* The structural constraint on the paged block, and only that; everything else must match
|
||||
* anything. attn_paged_tree is attn_paged's row with the tree operand, the same constraint. */
|
||||
* anything. attn_paged_tree, attn_paged_anchor_window and attn_paged_sink take attn_paged's. */
|
||||
CHECK_EQ(kern("attn_paged")->n_constraints, 1);
|
||||
CHECK(std::strcmp(kern("attn_paged")->constraints[0].key, "block_size") == 0);
|
||||
CHECK_EQ(kern("attn_paged_tree")->n_constraints, 1);
|
||||
CHECK_EQ(kern("attn_paged_sink")->n_constraints, 1);
|
||||
CHECK(std::strcmp(kern("attn_paged_sink")->constraints[0].key, "block_size") == 0);
|
||||
CHECK_EQ(kern("kv_store")->n_constraints, 1);
|
||||
CHECK_EQ(kern("gemm_nt")->n_constraints, 0);
|
||||
CHECK_EQ(kern("gdn_chunk_scan")->n_constraints, 0);
|
||||
int constrained = 0;
|
||||
for (int i = 0; i < api().kernel_count(); ++i) constrained += api().kernel_at(i)->n_constraints > 0;
|
||||
CHECK_EQ(constrained, 4);
|
||||
CHECK_EQ(constrained, 5);
|
||||
}
|
||||
|
||||
/* ================================================================== the numerics
|
||||
@@ -3196,6 +3201,47 @@ TEST(op_attn_paged) {
|
||||
CHECK_NEAR(out2.at(5), 1.0f, 1e-6);
|
||||
}
|
||||
|
||||
TEST(op_attn_paged_sink) {
|
||||
/* op_attn_paged's cache and queries, non-causal, with a sink logit a head: token 0 now sees both
|
||||
* keys too, and each sink adds exp(sink) to the denominator only. Head 0, token 0, q = [1,0]:
|
||||
* scores 1/sqrt(2) and 0, so weights 1, exp(-1/sqrt(2)) and exp(sink0 - 1/sqrt(2)). */
|
||||
const float sc = 1.0f / 1.41421356f;
|
||||
T q(RAD_F32, {2, 2, 2});
|
||||
q.f({ 1, 0, 0, 1,
|
||||
0, 1, 1, 0 });
|
||||
T kv(RAD_F32, {1, 1, 2, 4});
|
||||
kv.f({ 1, 0, 1, 0,
|
||||
0, 1, 0, 1 });
|
||||
T bt(RAD_I32, {1, 1}); bt.n({0});
|
||||
T su(RAD_I32, {1}); su.n({2});
|
||||
T sk(RAD_F32, {2}); sk.f({ 0.5f, 3.0f }); /* head 1's sink lies above every score */
|
||||
T out(RAD_F32, {2, 2, 2});
|
||||
A a; a.t(q).t(kv).t(bt).t(su).tnull().tnull().tnull().t(sk).t(out)
|
||||
.i("q_len", 2).i("head_dim", 2).i("gqa", 2).i("block_size", 2)
|
||||
.i("causal", 0).i("window", 0).s("q_dtype", "f32").s("kv_dtype", "f32");
|
||||
CHECK_OK(run("attn_paged_sink", a));
|
||||
const float w = std::exp(-sc);
|
||||
const float d0 = 1.0f + w + std::exp(0.5f - sc); /* head 0, either token */
|
||||
CHECK_NEAR(out.at(0), 1.0f / d0, 1e-6); CHECK_NEAR(out.at(1), w / d0, 1e-6);
|
||||
CHECK_NEAR(out.at(4), w / d0, 1e-6); CHECK_NEAR(out.at(5), 1.0f / d0, 1e-6);
|
||||
const float d1 = 1.0f + w + std::exp(3.0f - sc); /* head 1 */
|
||||
CHECK_NEAR(out.at(2), w / d1, 1e-6); CHECK_NEAR(out.at(3), 1.0f / d1, 1e-6);
|
||||
CHECK_NEAR(out.at(6), 1.0f / d1, 1e-6); CHECK_NEAR(out.at(7), w / d1, 1e-6);
|
||||
|
||||
/* A sink far below every score changes nothing: attn_paged's values, causal. */
|
||||
T sk2(RAD_F32, {2}); sk2.f({ -1e30f, -1e30f });
|
||||
T o1(RAD_F32, {2, 2, 2}), o2(RAD_F32, {2, 2, 2});
|
||||
A b; b.t(q).t(kv).t(bt).t(su).tnull().tnull().tnull().t(o1)
|
||||
.i("q_len", 2).i("head_dim", 2).i("gqa", 2).i("block_size", 2)
|
||||
.i("causal", 1).i("window", 0).s("q_dtype", "f32").s("kv_dtype", "f32");
|
||||
CHECK_OK(run("attn_paged", b));
|
||||
A c; c.t(q).t(kv).t(bt).t(su).tnull().tnull().tnull().t(sk2).t(o2)
|
||||
.i("q_len", 2).i("head_dim", 2).i("gqa", 2).i("block_size", 2)
|
||||
.i("causal", 1).i("window", 0).s("q_dtype", "f32").s("kv_dtype", "f32");
|
||||
CHECK_OK(run("attn_paged_sink", c));
|
||||
for (int i = 0; i < 8; ++i) CHECK_NEAR(o2.at(i), o1.at(i), 1e-7);
|
||||
}
|
||||
|
||||
TEST(op_attn_paged_ragged) {
|
||||
/* A ragged batch, as a serving step really looks: sequence 0 prefills two tokens while
|
||||
* sequence 1 decodes one. total_q is 3 and n_seq is 2, so without cu_seqlens this batch cannot
|
||||
@@ -10010,6 +10056,13 @@ std::vector<Geo> geometries() {
|
||||
G("mtp_enter", "M", 2, a.i("M", R).i("n", 8).i("hc", 2).d("eps", 1e-6).s("dtype", "f32")),
|
||||
G("grid_embed", "M", 3, a.i("M", R).i("n", 8).i("side", 4).s("dtype", "f32")),
|
||||
G("grid_embed_aa", "M", 3, a.i("M", R).i("n", 8).i("side", 4).s("dtype", "f32")),
|
||||
G("lili_cand", "M", 3, a.i("M", R).i("k", 4).i("world", 1).i("steps", 3).i("n", 8)
|
||||
.s("dtype", "f32")),
|
||||
G("lili_attn", "M", 8, a.i("M", R).i("steps", 2).i("k", 2).i("n_head", 2).i("head_dim", 4)
|
||||
.s("dtype", "f32")),
|
||||
G("lili_factor", "M", 8, a.i("M", R).i("L", 4).i("n", 8).s("dtype", "f32")),
|
||||
G("lili_walk", "M", 2, a.i("M", R).i("steps", 3).i("k", 2).i("n", 8).s("dtype", "f32")
|
||||
.d("eps", 1e-4).d("scale", 8.0)),
|
||||
G("layernorm", "M", 3, a.i("M", R).i("n", 8).d("eps", 1e-6).s("dtype", "f32")),
|
||||
G("add", "M", 3, a.i("M", R).i("n", 8).s("dtype", "f32")),
|
||||
G("mul", "M", 3, a.i("M", R).i("n", 8).s("dtype", "f32")),
|
||||
@@ -10101,6 +10154,10 @@ std::vector<Geo> geometries() {
|
||||
.i("block_size", 4).i("causal", 1).i("window", 0)
|
||||
.s("q_dtype", "f32").s("kv_dtype", "f32").i("n_head", 4)
|
||||
.i("ctx", 9)),
|
||||
G("attn_paged_sink", "q_len", 3, a.i("q_len", R).i("head_dim", 8).i("gqa", 2)
|
||||
.i("block_size", 4).i("causal", 0).i("window", 0)
|
||||
.s("q_dtype", "f32").s("kv_dtype", "f32").i("n_head", 4)
|
||||
.i("ctx", 9)),
|
||||
G("attn_dense", "M", 6, a.i("M", R).i("head_dim", 8).i("gqa", 2).i("causal", 1)
|
||||
.s("dtype", "f32").i("n_head", 4)),
|
||||
/* One-row segments, so M is also the segment count (n_seq, the description's hint for
|
||||
@@ -10371,7 +10428,9 @@ bool undrawable(const char* op) {
|
||||
// Complete raster grids need the hand-built cases in nemotron35_test, not random indices.
|
||||
return !std::strcmp(op, "givens_dense_quant") || !std::strcmp(op, "givens_rotate") ||
|
||||
!std::strcmp(op, "sample_merge_topk") || !std::strcmp(op, "gather_rows2") ||
|
||||
!std::strcmp(op, "pixtral_merge");
|
||||
!std::strcmp(op, "pixtral_merge") ||
|
||||
/* sample_logprobs rows (sorted pairs, positive sums): avx_check_hand.cpp builds them */
|
||||
!std::strcmp(op, "lili_cand");
|
||||
}
|
||||
|
||||
uint32_t lcg(uint32_t* s) { *s = *s * 1664525u + 1013904223u; return *s >> 8; }
|
||||
|
||||
Reference in new issue
Block a user