Archived
spec: --draft-verify token|block|traversal for the coupled MTP chain
--draft-coupling verifies each row of the chain on its own. Two lossless sequence-level verifiers accept more drafts from the same chain: Block Verification (arXiv 2403.10444) decides the whole chain at once, Traversal Verification (arXiv 2505.12398) walks a path from its last draft back to its first. --draft-verify token (default) is the engine as it was; block switches the coupling on; traversal needs it and is refused otherwise. Block: the sampler marks the consecutive bound rows of a request with the block's length (RadMtpCoupleState::blk) and mtp_couple_verify decides the block on the workgroup of its round-1 row, every FP64 sum a fixed lane tree so the result is the host reference's bit for bit. The wide-plane serial helper is a kernel of its own, which leaves couple_verify without scratch. Traversal: a new op mtp_trav_verify takes the step's topology (parent row, draft token per row) and decides a request on one workgroup, 0 bytes of scratch: the target's law as integer masses of a fixed-point exponential, the walk in FP64. Its header and kernel also handle nodes with several children (Gumbel-top-k and RheoSampling siblings); the serial head here drafts one, so that part has no producer yet and is covered by the tests only. RadMtpCoupleState grows from 152 to 168 bytes (nkids, scheme, blk), so RAD_ABI_VERSION is 41. Exactness: mtp_block_test enumerates the draft tree of a toy model and runs chi-square over the real verify code; mtp_rrs_test runs chi-square and an exhaustive enumeration of the walk (worst |joint - P| 7e-14) with negative controls that fail; spechub_device_test compares the device rows with the host rows. Measured on Qwen3.8-Flash-Next IQR-V4 (TP2, MTP chain 3, coupling) on our branch: about +1% tok/s with traversal, +4.6% at T 1.0 top_k 0. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9b6eba80c6
commit
d0fd4aa1bb
31 files changed
+5022
-30
No files matched your search
@@ -89,6 +89,7 @@ set_target_properties(harness_fixture PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAK
|
||||
# Binaries registered as several entries, <bin>_shard<k> running `<bin> --shard k/n`, so their
|
||||
# cases run in parallel and no entry is the fast tier's long pole.
|
||||
set(RAD_TEST_SHARDS_gbnf_test 4)
|
||||
set(RAD_TEST_SHARDS_mtp_rrs_test 8) # chi-square statistics: eight ctest entries of under a minute each
|
||||
|
||||
# ------------------------------------------------------------------ the environment of every test
|
||||
#
|
||||
@@ -132,6 +133,8 @@ set(RAD_TEST_ENV ${RAD_TEST_CARD_ENV} "ROCR_VISIBLE_DEVICES=99")
|
||||
|
||||
# Binaries that need more CPU than the default budget, and what costs it. Lower an entry when its
|
||||
# cost comes down, so the tripwire stays tight.
|
||||
set(RAD_TEST_CPU_BUDGET_mtp_rrs_test 100) # chi-square over 2e5..4e5 seeds a case
|
||||
set(RAD_TEST_CPU_BUDGET_mtp_block_test 40) # exact enumeration (g up to 4) plus ~1.6e6 chi-square steps over the real shared verify code
|
||||
set(RAD_TEST_CPU_BUDGET_quant_ggml_test 25) # llama.cpp's iq1_s and iq2_* grid searches, ~13 s
|
||||
set(RAD_TEST_CPU_BUDGET_tokenizer_test 20) # the reference encoder over every corpus: ~3 s built by GCC 16, ~10.5 s by GCC 14 (the image's Ubuntu)
|
||||
set(RAD_TEST_CPU_BUDGET_arch_test 15) # every plugin's graph declared; ~10.8 s with the batched-tower case
|
||||
@@ -350,6 +353,12 @@ if(TARGET spechub)
|
||||
target_compile_definitions(spechub_wiring_test PRIVATE RAD_SPECHUB_WITH_DEVICE=1)
|
||||
endif()
|
||||
add_dependencies(spechub_wiring_test spechub)
|
||||
# spechub_device_test runs the plugin's device verify rows against their host rows on a card
|
||||
# (skips without one); it needs the same library path.
|
||||
target_link_libraries(spechub_device_test PRIVATE ${CMAKE_DL_LIBS})
|
||||
target_compile_definitions(spechub_device_test PRIVATE
|
||||
RAD_SPECHUB_LIB="${RAD_PLUGIN_OUT}/kernels/spechub.so")
|
||||
add_dependencies(spechub_device_test spechub)
|
||||
# sample_test's coupling case declares a Sampler with draft_coupling set and needs the verify
|
||||
# op the spechub plugin carries (its host row); it skips when the library is absent.
|
||||
add_dependencies(sample_test spechub)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* abi_layout_table.h -- every abi/ struct's size and field offsets at RAD_ABI_VERSION 40 and
|
||||
/* abi_layout_table.h -- every abi/ struct's size and field offsets at RAD_ABI_VERSION 41 and
|
||||
* RAD_FORMAT_VER 2, as { type, field, offset, size } rows ("" is the struct itself). A snapshot
|
||||
* of the headers on x86-64 Linux, which abi_layout_test.cpp holds the layout to; regenerate it
|
||||
* from the test's printout only together with a version bump. */
|
||||
@@ -936,7 +936,7 @@
|
||||
{ "RadMtpCoupleDraw", "seq", 16, 8 },
|
||||
{ "RadMtpCoupleDraw", "card", 24, 4 },
|
||||
{ "RadMtpCoupleDraw", "mode", 28, 4 },
|
||||
{ "RadMtpCoupleState", "", 0, 152 },
|
||||
{ "RadMtpCoupleState", "", 0, 168 },
|
||||
{ "RadMtpCoupleState", "draw", 0, 32 },
|
||||
{ "RadMtpCoupleState", "pos", 32, 8 },
|
||||
{ "RadMtpCoupleState", "status", 40, 4 },
|
||||
@@ -945,3 +945,7 @@
|
||||
{ "RadMtpCoupleState", "count", 52, 4 },
|
||||
{ "RadMtpCoupleState", "ids", 56, 32 },
|
||||
{ "RadMtpCoupleState", "q", 88, 64 },
|
||||
{ "RadMtpCoupleState", "nkids", 152, 4 },
|
||||
{ "RadMtpCoupleState", "scheme", 156, 4 },
|
||||
{ "RadMtpCoupleState", "blk", 160, 4 },
|
||||
{ "RadMtpCoupleState", "pad_", 164, 4 },
|
||||
@@ -58,8 +58,8 @@ std::vector<Row> cpp_rows() {
|
||||
};
|
||||
}
|
||||
|
||||
/* The layout at RAD_ABI_VERSION 40 / RAD_FORMAT_VER 2 (see the file header). */
|
||||
constexpr uint32_t kTableAbi = 40, kTableFormat = 2;
|
||||
/* The layout at RAD_ABI_VERSION 41 / RAD_FORMAT_VER 2 (see the file header). */
|
||||
constexpr uint32_t kTableAbi = 41, kTableFormat = 2;
|
||||
const AbiRow kTable[] = {
|
||||
#include "abi_layout_table.h"
|
||||
};
|
||||
|
||||
+32
-2
@@ -587,7 +587,7 @@ const std::map<std::string, std::vector<std::string>>& legal_values() {
|
||||
{ "--num-speculative-tokens", { "2" } }, { "--spec-ngram", { "4" } },
|
||||
{ "--spec-ngram-min", { "1" } }, { "--spec-ngram-max", { "6" } },
|
||||
{ "--draft-window", { "fixed" } }, { "--draft-short-from", { "4" } },
|
||||
{ "--draft-coupling", { "on" } },
|
||||
{ "--draft-coupling", { "on" } }, { "--draft-verify", { "block" } },
|
||||
{ "--host", { "127.0.0.1" } }, { "--port", { "8001" } }, { "--api-key", { "k" } },
|
||||
{ "--api-key-env", { "RAD_KEY_ENV_INVENTORY" } },
|
||||
{ "--allow-host", { "box" } },
|
||||
@@ -1037,6 +1037,36 @@ TEST(the_draft_window_is_fixed_or_adaptive) {
|
||||
CHECK_EQ(std::string(draft_window_name(DraftWindow::Fixed)), std::string("fixed"));
|
||||
}
|
||||
|
||||
/* --draft-verify is token (alias rowwise) unless asked for; block and traversal parse. block
|
||||
* implies the coupled drafting and refuses coupling off; traversal needs the coupling and says so. */
|
||||
TEST(the_draft_verify_is_token_block_or_traversal) {
|
||||
Config d;
|
||||
CHECK_EQ(parse({}, &d), RAD_OK);
|
||||
CHECK(!d.draft_verify_block);
|
||||
CHECK(!d.draft_verify_traversal);
|
||||
Config c;
|
||||
CHECK_EQ(parse({ "--draft-verify", "block" }, &c), RAD_OK);
|
||||
CHECK(c.draft_verify_block);
|
||||
CHECK(!c.draft_verify_traversal);
|
||||
CHECK(c.draft_coupling);
|
||||
Config t;
|
||||
CHECK_EQ(parse({ "--draft-coupling", "on", "--draft-verify", "traversal" }, &t), RAD_OK);
|
||||
CHECK(t.draft_verify_traversal);
|
||||
CHECK(!t.draft_verify_block);
|
||||
CHECK_EQ(parse({ "--draft-verify", "rowwise" }, &t), RAD_OK);
|
||||
CHECK(!t.draft_verify_traversal);
|
||||
CHECK_EQ(parse({ "--draft-verify", "token" }, &t), RAD_OK);
|
||||
CHECK(!t.draft_verify_block);
|
||||
Config b;
|
||||
for (const char* bad : { "Block", "Traversal", "on", "" })
|
||||
CHECK_EQ(parse({ "--draft-verify", bad }, &b), RAD_E_INVAL);
|
||||
CHECK_EQ(parse({ "--draft-verify" }, &b), RAD_E_INVAL);
|
||||
Config off;
|
||||
CHECK_EQ(parse({ "--draft-verify", "block", "--draft-coupling", "off" }, &off), RAD_E_INVAL);
|
||||
Config nocouple;
|
||||
CHECK_EQ(parse({ "--draft-verify", "traversal" }, &nocouple), RAD_E_INVAL);
|
||||
}
|
||||
|
||||
/* --draft-coupling is off unless asked for, and only off and on parse: a misspelt value must not
|
||||
* start a server on the other path. */
|
||||
TEST(the_draft_coupling_is_off_or_on) {
|
||||
@@ -1634,7 +1664,7 @@ TEST(usage_names_every_flag_it_documents) {
|
||||
* for, so a flag added without a help line is caught. */
|
||||
const std::string text = stderr_of([] { config_usage("radiance"); });
|
||||
for (const char* f : { "--model", "--host-pool-mib", "--kv-cache-dtype",
|
||||
"--num-speculative-tokens", "--draft-window", "--draft-coupling",
|
||||
"--num-speculative-tokens", "--draft-window", "--draft-coupling", "--draft-verify",
|
||||
"--checkpoint-policy",
|
||||
"--prefix-cache-mib", "--prefix-cache-ttl", "--tp-wire",
|
||||
"--embedding-placement", "--ngram-placement",
|
||||
|
||||
@@ -0,0 +1,578 @@
|
||||
/* tests/mtp_block_test.cpp -- Block Verification of a linear MTP draft chain (--draft-verify block,
|
||||
* abi/rad_mtp_block.h, arXiv 2403.10444), checked against its own law.
|
||||
*
|
||||
* The property: whatever the head's law Q and the chain's length g, the tokens a verify step commits
|
||||
* are a prefix of a sample of the TARGET's autoregressive law, and the step commits at least as many
|
||||
* tokens as the token-wise (maximal coupling) verify of the same drafts. Tests:
|
||||
*
|
||||
* exact enumeration every draft of a toy chain, the rule written out densely and independently
|
||||
* of the header, the law of the first three committed tokens summed in
|
||||
* closed form: equals the target's product law to 1e-12 (g = 1..4, three
|
||||
* head qualities); and the header's h_i equal the dense ones
|
||||
* chi-square the real shared code (the propose row, rad_mtp_couple_verify_row with the
|
||||
* block marking, the core's token-comparison walk) over 3e5 seeds: the joint
|
||||
* law of the first three tokens and the first token's marginal
|
||||
* negative control the same code with h_i nudged (a wrong acceptance rule) is NOT exact and
|
||||
* the same statistic sees it
|
||||
* dominance the mean committed tokens per step of block verification is at least the
|
||||
* token-wise verify's on the same drafts
|
||||
* inert rows a stale or non-consecutive state leaves the ordinary picks alone; rows
|
||||
* after the first of a block write nothing; a greedy row is untouched
|
||||
*/
|
||||
#include "rad_test.h"
|
||||
|
||||
#include "rad_mtp_block.h"
|
||||
#include "../plugin/spechub/spechub.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int V = 12; /* larger than the head's top 8: the target has mass off Q's support */
|
||||
|
||||
uint64_t H(uint64_t a) { return rad_mtp_rrs_mix(a); }
|
||||
|
||||
struct Law {
|
||||
int32_t pid[V]; float pval[V]; int pn = 0;
|
||||
double p[V];
|
||||
int32_t qid[8]; float qval[8];
|
||||
};
|
||||
|
||||
Law law_at(uint64_t prefix_hash, double head_noise) {
|
||||
Law L;
|
||||
float lt[V], lq[V];
|
||||
for (int i = 0; i < V; ++i) {
|
||||
lt[i] = (float)((H(prefix_hash * 131 + (uint64_t)i * 977 + 1) >> 40) % 3000) / 1000.0f - 1.5f;
|
||||
const float nz = (float)((H(prefix_hash * 257 + (uint64_t)i * 331 + 7) >> 40) % 3000) / 1000.0f - 1.5f;
|
||||
lq[i] = 0.75f * lt[i] + (float)head_noise * nz;
|
||||
}
|
||||
const uint64_t h = H(prefix_hash + 99);
|
||||
const int drop = h % 3 == 0 ? (int)((h >> 8) % V) : -1;
|
||||
double z = 0;
|
||||
for (int i = 0; i < V; ++i) { L.p[i] = 0; if (i != drop) { L.pid[L.pn] = i; L.pval[L.pn++] = lt[i]; } }
|
||||
for (int j = 0; j < L.pn; ++j) z += std::exp((double)L.pval[j]);
|
||||
for (int j = 0; j < L.pn; ++j) L.p[L.pid[j]] = std::exp((double)L.pval[j]) / z;
|
||||
int ord[V];
|
||||
for (int i = 0; i < V; ++i) ord[i] = i;
|
||||
std::stable_sort(ord, ord + V, [&](int a, int b) { return lq[a] > lq[b]; });
|
||||
for (int i = 0; i < 8; ++i) { L.qid[i] = ord[i]; L.qval[i] = lq[ord[i]]; }
|
||||
return L;
|
||||
}
|
||||
|
||||
uint64_t child_hash(uint64_t ph, int tok) { return H(ph * 1000003ull + (uint64_t)tok + 17); }
|
||||
|
||||
int sample_law(const Law& L, uint64_t seed, uint64_t pos, uint64_t key) {
|
||||
double u = rad_spechub_uniform(seed, pos, key);
|
||||
int last = 0;
|
||||
for (int i = 0; i < V; ++i) {
|
||||
if (L.p[i] <= 0) continue;
|
||||
last = i;
|
||||
u -= L.p[i];
|
||||
if (u < 0) return i;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
|
||||
RadMtpCoupleDraw make_draw(uint64_t seed, uint64_t base) {
|
||||
RadMtpCoupleDraw d{};
|
||||
d.seed = seed; d.base = base; d.seq = 1; d.card = 0; d.mode = 1;
|
||||
return d;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ a drafted chain */
|
||||
|
||||
struct Chain {
|
||||
int g = 0;
|
||||
int tok[8] = {}; /* X_1 .. X_g */
|
||||
uint64_t hash[9] = {}; /* the prefix hash after X^i, i = 0 .. g */
|
||||
RadMtpCoupleState st[8] = {}; /* row i's coupled state (round i + 1) */
|
||||
};
|
||||
|
||||
/* The chain drafted by the real propose row, round by round, over the head's law at the chain's own
|
||||
* prefix (x ~ Q at the draw's (seed, base, KEY_DRAFT + round)). */
|
||||
void draft_chain(Chain* t, uint64_t seed, uint64_t base, int g, double noise, uint64_t h0 = 1) {
|
||||
uint64_t h = h0;
|
||||
t->g = g;
|
||||
t->hash[0] = h;
|
||||
for (int r = 1; r <= g; ++r) {
|
||||
const Law law = law_at(h, noise);
|
||||
int32_t idx[8];
|
||||
float val[8];
|
||||
for (int i = 0; i < 8; ++i) { idx[i] = law.qid[i]; val[i] = law.qval[i]; }
|
||||
const RadMtpCoupleDraw draw = make_draw(seed, base);
|
||||
const int32_t card = 0;
|
||||
RadMtpCoupleProposeCall c{};
|
||||
c.idx = idx; c.val = val; c.card = &card;
|
||||
c.draw = reinterpret_cast<const uint32_t*>(&draw);
|
||||
RadMtpCoupleState rows[8] = {};
|
||||
c.state = reinterpret_cast<uint32_t*>(rows);
|
||||
c.M = 1; c.vocab = 1000;
|
||||
c.ld_idx = 8; c.ld_val = 8; c.ld_draw = RAD_MTP_COUPLE_DRAW_WORDS;
|
||||
c.st_round = RAD_MTP_COUPLE_STATE_WORDS; c.st_row = 0;
|
||||
c.round = r;
|
||||
rad_mtp_couple_propose_row(c, 0);
|
||||
t->st[r - 1] = rows[r - 1];
|
||||
t->tok[r - 1] = idx[0];
|
||||
h = child_hash(h, idx[0]);
|
||||
t->hash[r] = h;
|
||||
}
|
||||
}
|
||||
|
||||
enum Mode { TOKEN, BLOCK, CONTROL };
|
||||
|
||||
/* The control's wrong rule: every inner h_i raised by a quarter (clipped at 1). */
|
||||
void nudge(RadBlockPlan* pl) {
|
||||
for (int i = 1; i < pl->g; ++i) pl->h[i] = std::min(1.0, pl->h[i] * 1.25);
|
||||
}
|
||||
|
||||
/* The sampler's planes of one step: g + 1 rows, the last the bonus row with no state. */
|
||||
struct Planes {
|
||||
int32_t ids[9 * V];
|
||||
float val[9 * V];
|
||||
uint32_t params[9 * 24];
|
||||
RadMtpCoupleState st[9];
|
||||
int32_t tok[9];
|
||||
};
|
||||
|
||||
RadMtpCoupleVerifyCall call_of(Planes* pl, int g) {
|
||||
RadMtpCoupleVerifyCall c{};
|
||||
c.idx = pl->ids; c.val = pl->val; c.params = pl->params;
|
||||
c.state = reinterpret_cast<const uint32_t*>(pl->st);
|
||||
c.token = pl->tok;
|
||||
c.M = g + 1; c.n_cand = V; c.vocab = 1000;
|
||||
c.ld[0] = V; c.ld[1] = V; c.ld[2] = 24; c.ld[3] = RAD_MTP_COUPLE_STATE_WORDS; c.ld[4] = 1;
|
||||
return c;
|
||||
}
|
||||
|
||||
/* One verify step over the chain, walked as the core does: the verify op's rows over the sampler's
|
||||
* planes, then the rule "a row's token that equals its draft accepts it". Writes the committed
|
||||
* tokens, extended to `want` by exact draws from the target. Returns the committed count before the
|
||||
* extension. */
|
||||
int step(const Chain& t, uint64_t seed, uint64_t n_out, double noise, Mode mode, int want, int* out) {
|
||||
const int g = t.g;
|
||||
Planes pl;
|
||||
std::memset(&pl, 0, sizeof pl);
|
||||
for (int i = 0; i <= g; ++i) {
|
||||
const Law L = law_at(t.hash[i], noise);
|
||||
for (int j = 0; j < V; ++j) {
|
||||
pl.ids[i * V + j] = j < L.pn ? L.pid[j] : -1;
|
||||
pl.val[i * V + j] = j < L.pn ? L.pval[j] : 0.0f;
|
||||
}
|
||||
RadSampleParams* p = reinterpret_cast<RadSampleParams*>(pl.params + i * 24);
|
||||
p->seed = seed;
|
||||
p->pos = n_out + (uint64_t)i;
|
||||
pl.tok[i] = sample_law(L, seed, n_out + (uint64_t)i, 0); /* the ordinary pick */
|
||||
if (i < g) {
|
||||
pl.st[i] = t.st[i];
|
||||
pl.st[i].pos = rad_mtp_couple_verify_pos(n_out, i + 1);
|
||||
pl.st[i].blk = mode == TOKEN ? 0 : g;
|
||||
}
|
||||
}
|
||||
const RadMtpCoupleVerifyCall c = call_of(&pl, g);
|
||||
if (mode == CONTROL) {
|
||||
RadBlockRow rows[8];
|
||||
RadBlockStat stat[8];
|
||||
for (int i = 0; i < g; ++i) {
|
||||
rows[i] = RadBlockRow{ pl.ids + i * V, pl.val + i * V, V, 0, 0.0f };
|
||||
rad_mtp_block_scan(&rows[i], 1000);
|
||||
CHECK(rad_mtp_block_stat(rows[i], pl.st[i], &stat[i]));
|
||||
}
|
||||
RadBlockPlan bp;
|
||||
rad_mtp_block_plan(pl.st, stat, g, &bp);
|
||||
nudge(&bp);
|
||||
const int tau = rad_mtp_block_tau(bp, seed, n_out);
|
||||
int32_t y = -1;
|
||||
if (tau < g)
|
||||
CHECK(rad_mtp_block_resid_draw(rows[tau], pl.st[tau], stat[tau], bp.p[tau],
|
||||
rad_spechub_uniform(seed, n_out + (uint64_t)tau,
|
||||
RAD_SAMPLE_KEY_RESID), &y));
|
||||
rad_mtp_block_write(c, 0, pl.st, tau, g, y);
|
||||
} else {
|
||||
for (int i = 0; i < g; ++i) rad_mtp_couple_verify_row(c, i);
|
||||
}
|
||||
int len = 0;
|
||||
for (int i = 0; i <= g; ++i) {
|
||||
out[len++] = pl.tok[i];
|
||||
if (i == g || pl.tok[i] != t.tok[i]) break;
|
||||
}
|
||||
const int committed = len;
|
||||
uint64_t ph = t.hash[0];
|
||||
for (int i = 0; i < len; ++i) ph = child_hash(ph, out[i]);
|
||||
while (len < want) {
|
||||
const Law L = law_at(ph, noise);
|
||||
const int y = sample_law(L, seed ^ 0xabcdef1234ull, n_out + (uint64_t)len, 1);
|
||||
out[len++] = y;
|
||||
ph = child_hash(ph, y);
|
||||
}
|
||||
return committed;
|
||||
}
|
||||
|
||||
double zscore(const std::vector<double>& counts, const std::vector<double>& expect, double n) {
|
||||
double chi = 0, pool_c = 0, pool_e = 0;
|
||||
int cells = 0;
|
||||
for (size_t i = 0; i < counts.size(); ++i) {
|
||||
const double e = expect[i] * n;
|
||||
if (e < 20) { pool_c += counts[i]; pool_e += e; continue; }
|
||||
chi += (counts[i] - e) * (counts[i] - e) / e;
|
||||
++cells;
|
||||
}
|
||||
if (pool_e >= 5) { chi += (pool_c - pool_e) * (pool_c - pool_e) / pool_e; ++cells; }
|
||||
const double df = cells - 1;
|
||||
return (chi - df) / std::sqrt(2.0 * df);
|
||||
}
|
||||
|
||||
struct Result { double z, first_z, mean_len, var_len; };
|
||||
|
||||
Result run(Mode mode, int g, int trials, double noise) {
|
||||
std::vector<double> cnt3((size_t)V * V * V, 0), cnt1((size_t)V, 0);
|
||||
double sum = 0, sq = 0;
|
||||
for (int s = 0; s < trials; ++s) {
|
||||
const uint64_t seed = 0x1234567ull + (uint64_t)s * 7919ull;
|
||||
Chain t;
|
||||
draft_chain(&t, seed, 0, g, noise);
|
||||
int out[16];
|
||||
const int len = step(t, seed, 0, noise, mode, 3, out);
|
||||
++cnt1[(size_t)out[0]];
|
||||
++cnt3[(size_t)((out[0] * V + out[1]) * V + out[2])];
|
||||
sum += len; sq += (double)len * len;
|
||||
}
|
||||
std::vector<double> e1((size_t)V, 0), e3((size_t)V * V * V, 0);
|
||||
const Law l0 = law_at(1, noise);
|
||||
for (int a = 0; a < V; ++a) {
|
||||
e1[(size_t)a] = l0.p[a];
|
||||
const Law l1 = law_at(child_hash(1, a), noise);
|
||||
for (int b = 0; b < V; ++b) {
|
||||
const Law l2 = law_at(child_hash(child_hash(1, a), b), noise);
|
||||
for (int c = 0; c < V; ++c) e3[(size_t)((a * V + b) * V + c)] = l0.p[a] * l1.p[b] * l2.p[c];
|
||||
}
|
||||
}
|
||||
const double mean = sum / trials;
|
||||
return { zscore(cnt3, e3, trials), zscore(cnt1, e1, trials), mean, sq / trials - mean * mean };
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ the exact law */
|
||||
|
||||
/* The rule written densely, independent of the header: P_i, Q_{i+1} as length-V vectors. */
|
||||
struct Dense { double P[V], Q[V]; };
|
||||
|
||||
void dense_h(const std::vector<Dense>& d, const int* x, int g, double* h, double* p) {
|
||||
p[0] = 1;
|
||||
for (int i = 1; i <= g; ++i)
|
||||
p[i] = std::min(1.0, p[i - 1] * d[(size_t)i - 1].P[x[i - 1]] / d[(size_t)i - 1].Q[x[i - 1]]);
|
||||
h[g] = p[g];
|
||||
for (int i = 0; i < g; ++i) {
|
||||
double S = 0;
|
||||
for (int v = 0; v < V; ++v) S += std::max(p[i] * d[(size_t)i].P[v] - d[(size_t)i].Q[v], 0.0);
|
||||
h[i] = S / (S + 1.0 - p[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/* The exact law of the first three committed tokens (the step, then the target's own draws), by
|
||||
* summing over every draft, tau and Y. Also compares the header's weights with the dense ones. */
|
||||
double exact_error(int gmax, double noise, double* h_err, int adaptive = 0) {
|
||||
std::map<std::vector<int>, double> emit; /* committed sequence -> probability */
|
||||
*h_err = 0;
|
||||
std::vector<int> x((size_t)gmax);
|
||||
std::vector<uint64_t> hash((size_t)gmax + 1);
|
||||
hash[0] = 1;
|
||||
std::function<void(int, double)> rec = [&](int i, double pr) {
|
||||
/* adaptive: the draft stage stops after X_i when a function of the drafted prefix says so
|
||||
* (what confidence pruning does); the block's length g is then draft-dependent. */
|
||||
const bool stop_here = adaptive && i >= 1 && i < gmax && (H(hash[(size_t)i] + 5) % 3) < (uint64_t)adaptive;
|
||||
const int g = i;
|
||||
if (i < gmax && !stop_here) {
|
||||
const Law L = law_at(hash[(size_t)i], noise);
|
||||
RadMtpCoupleState s{};
|
||||
CHECK(rad_mtp_couple_build_q(L.qid, L.qval, 1000, &s));
|
||||
for (int k = 0; k < s.count; ++k) {
|
||||
x[(size_t)i] = s.ids[k];
|
||||
hash[(size_t)i + 1] = child_hash(hash[(size_t)i], s.ids[k]);
|
||||
rec(i + 1, pr * s.q[k]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
std::vector<Dense> d((size_t)g);
|
||||
std::vector<Law> laws;
|
||||
std::vector<RadMtpCoupleState> st((size_t)g);
|
||||
std::vector<RadBlockRow> rows((size_t)g);
|
||||
for (int j = 0; j < g; ++j) laws.push_back(law_at(hash[(size_t)j], noise));
|
||||
for (int j = 0; j < g; ++j) {
|
||||
CHECK(rad_mtp_couple_build_q(laws[(size_t)j].qid, laws[(size_t)j].qval, 1000, &st[(size_t)j]));
|
||||
st[(size_t)j].x = x[(size_t)j];
|
||||
for (int v = 0; v < V; ++v) { d[(size_t)j].P[v] = laws[(size_t)j].p[v]; d[(size_t)j].Q[v] = 0; }
|
||||
for (int k = 0; k < st[(size_t)j].count; ++k)
|
||||
d[(size_t)j].Q[st[(size_t)j].ids[k]] = st[(size_t)j].q[k];
|
||||
rows[(size_t)j] = RadBlockRow{ laws[(size_t)j].pid, laws[(size_t)j].pval,
|
||||
laws[(size_t)j].pn, 0, 0.0f };
|
||||
rad_mtp_block_scan(&rows[(size_t)j], 1000);
|
||||
}
|
||||
double h[9], p[9];
|
||||
dense_h(d, x.data(), g, h, p);
|
||||
RadBlockStat stat[8];
|
||||
for (int j = 0; j < g; ++j) CHECK(rad_mtp_block_stat(rows[(size_t)j], st[(size_t)j], &stat[j]));
|
||||
RadBlockPlan bp;
|
||||
rad_mtp_block_plan(st.data(), stat, g, &bp);
|
||||
for (int j = 0; j <= g; ++j) *h_err = std::max(*h_err, std::fabs(bp.h[j] - h[j]));
|
||||
double stop = 1; /* probability that no larger i has accepted */
|
||||
for (int tau = g; tau >= 0; --tau) {
|
||||
const double pt = stop * h[tau];
|
||||
stop *= 1 - h[tau];
|
||||
if (pt <= 0) continue;
|
||||
std::vector<int> e(x.begin(), x.begin() + tau);
|
||||
if (tau == g) {
|
||||
const Law Lg = law_at(hash[(size_t)g], noise);
|
||||
for (int z = 0; z < V; ++z) {
|
||||
if (Lg.p[z] <= 0) continue;
|
||||
std::vector<int> e2 = e; e2.push_back(z);
|
||||
emit[e2] += pr * pt * Lg.p[z];
|
||||
}
|
||||
continue;
|
||||
}
|
||||
double S = 0, r[V];
|
||||
for (int v = 0; v < V; ++v) {
|
||||
r[v] = std::max(p[tau] * d[(size_t)tau].P[v] - d[(size_t)tau].Q[v], 0.0);
|
||||
S += r[v];
|
||||
}
|
||||
if (!(S > 0)) { for (int v = 0; v < V; ++v) r[v] = d[(size_t)tau].P[v]; S = 1; }
|
||||
for (int v = 0; v < V; ++v) {
|
||||
if (r[v] <= 0) continue;
|
||||
std::vector<int> e2 = e; e2.push_back(v);
|
||||
emit[e2] += pr * pt * r[v] / S;
|
||||
}
|
||||
}
|
||||
};
|
||||
rec(0, 1.0);
|
||||
/* extend every committed sequence to three tokens with the target's conditionals */
|
||||
std::vector<double> got((size_t)V * V * V, 0);
|
||||
for (const auto& kv : emit) {
|
||||
const std::vector<int>& e = kv.first;
|
||||
std::function<void(std::vector<int>&, uint64_t, double)> ext =
|
||||
[&](std::vector<int>& w, uint64_t h, double pr) {
|
||||
if (w.size() == 3) { got[(size_t)((w[0] * V + w[1]) * V + w[2])] += pr; return; }
|
||||
const Law L = law_at(h, noise);
|
||||
for (int v = 0; v < V; ++v) {
|
||||
if (L.p[v] <= 0) continue;
|
||||
w.push_back(v);
|
||||
ext(w, child_hash(h, v), pr * L.p[v]);
|
||||
w.pop_back();
|
||||
}
|
||||
};
|
||||
std::vector<int> w(e.begin(), e.begin() + (long)std::min<size_t>(e.size(), 3));
|
||||
uint64_t h = 1;
|
||||
for (int t : w) h = child_hash(h, t);
|
||||
ext(w, h, kv.second);
|
||||
}
|
||||
const Law l0 = law_at(1, noise);
|
||||
double err = 0, tot = 0;
|
||||
for (int a = 0; a < V; ++a) {
|
||||
const Law l1 = law_at(child_hash(1, a), noise);
|
||||
for (int b = 0; b < V; ++b) {
|
||||
const Law l2 = law_at(child_hash(child_hash(1, a), b), noise);
|
||||
for (int c = 0; c < V; ++c) {
|
||||
const size_t i = (size_t)((a * V + b) * V + c);
|
||||
const double want = l0.p[a] * l1.p[b] * l2.p[c];
|
||||
err = std::max(err, std::fabs(got[i] - want));
|
||||
tot += got[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
CHECK_NEAR(tot, 1.0, 1e-12);
|
||||
return err;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
TEST(block_verification_emits_exactly_the_target_law_by_enumeration) {
|
||||
for (double noise : { 0.3, 1.0, 3.0 })
|
||||
for (int g = 1; g <= 4; ++g) {
|
||||
double h_err = 0;
|
||||
const double err = exact_error(g, noise, &h_err);
|
||||
std::printf(" g=%d noise=%.1f max |law - target| = %.2e max |h - h_dense| = %.2e\n", g,
|
||||
noise, err, h_err);
|
||||
CHECK(err < 1e-12);
|
||||
CHECK(h_err < 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
/* A draft stage that stops by a function of the drafted prefix (confidence pruning, dynamic rows)
|
||||
* makes the block length draft-dependent. Printed, and checked to be exact. */
|
||||
TEST(block_verification_with_a_draft_dependent_chain_length_is_exact) {
|
||||
for (double noise : { 0.3, 1.0, 3.0 })
|
||||
for (int adaptive : { 1, 2 }) {
|
||||
double h_err = 0;
|
||||
const double err = exact_error(4, noise, &h_err, adaptive);
|
||||
std::printf(" adaptive stop (%d/3 of prefixes) noise=%.1f: max |law - target| = %.2e\n",
|
||||
adaptive, noise, err);
|
||||
CHECK(err < 1e-12);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(the_chain_verify_with_block_marking_commits_the_target_law) {
|
||||
const Result a = run(BLOCK, 3, 300000, 1.0);
|
||||
std::printf(" g=3 noise 1.0: z=%.2f first=%.2f mean tokens %.4f\n", a.z, a.first_z, a.mean_len);
|
||||
CHECK(a.z < 4.5); CHECK(a.first_z < 4.5);
|
||||
const Result b = run(BLOCK, 3, 150000, 3.0);
|
||||
std::printf(" g=3 noise 3.0: z=%.2f first=%.2f mean tokens %.4f\n", b.z, b.first_z, b.mean_len);
|
||||
CHECK(b.z < 4.5); CHECK(b.first_z < 4.5);
|
||||
const Result c = run(BLOCK, 5, 150000, 0.3);
|
||||
std::printf(" g=5 noise 0.3: z=%.2f first=%.2f mean tokens %.4f\n", c.z, c.first_z, c.mean_len);
|
||||
CHECK(c.z < 4.5); CHECK(c.first_z < 4.5);
|
||||
}
|
||||
|
||||
/* The same statistic sees a wrong acceptance rule. */
|
||||
TEST(a_block_rule_with_nudged_weights_is_not_exact) {
|
||||
const Result r = run(CONTROL, 3, 300000, 1.0);
|
||||
std::printf(" nudged h (known wrong): z=%.1f first=%.1f\n", r.z, r.first_z);
|
||||
CHECK(r.z > 8.0);
|
||||
}
|
||||
|
||||
/* Block verification commits at least as many tokens as the token-wise verify of the same drafts,
|
||||
* and both are exact (the token-wise one is the existing coupling). */
|
||||
TEST(block_verification_commits_at_least_as_many_tokens_as_token_wise) {
|
||||
for (double noise : { 0.3, 1.0, 3.0 }) {
|
||||
const int trials = 200000;
|
||||
const Result tk = run(TOKEN, 3, trials, noise);
|
||||
const Result bl = run(BLOCK, 3, trials, noise);
|
||||
const double se = std::sqrt((tk.var_len + bl.var_len) / trials);
|
||||
std::printf(" noise %.1f: token-wise %.4f tokens/step (z=%.2f), block %.4f (z=%.2f), +%.4f\n",
|
||||
noise, tk.mean_len, tk.z, bl.mean_len, bl.z, bl.mean_len - tk.mean_len);
|
||||
CHECK(tk.z < 4.5);
|
||||
CHECK(bl.z < 4.5);
|
||||
CHECK(bl.mean_len > tk.mean_len - 4 * se);
|
||||
}
|
||||
}
|
||||
|
||||
/* A state that is not the one the sampler would have bound, or a block that is not consecutive,
|
||||
* leaves the ordinary picks alone; only the first row of a block writes. */
|
||||
TEST(block_rows_that_do_not_bind_keep_their_ordinary_picks) {
|
||||
const int g = 3;
|
||||
Chain t;
|
||||
draft_chain(&t, 77, 0, g, 1.0);
|
||||
auto build = [&](Planes* pl, uint64_t seed) {
|
||||
std::memset(pl, 0, sizeof *pl);
|
||||
for (int i = 0; i <= g; ++i) {
|
||||
const Law L = law_at(t.hash[i], 1.0);
|
||||
for (int j = 0; j < V; ++j) {
|
||||
pl->ids[i * V + j] = j < L.pn ? L.pid[j] : -1;
|
||||
pl->val[i * V + j] = j < L.pn ? L.pval[j] : 0.0f;
|
||||
}
|
||||
RadSampleParams* p = reinterpret_cast<RadSampleParams*>(pl->params + i * 24);
|
||||
p->seed = seed; p->pos = 10 + (uint64_t)i;
|
||||
pl->tok[i] = 1000 + i; /* a marker no verify could write */
|
||||
if (i < g) { pl->st[i] = t.st[i]; pl->st[i].pos = 10 + (uint64_t)i; pl->st[i].blk = g; }
|
||||
}
|
||||
};
|
||||
auto verify = [&](Planes* pl, int row0, int rows) {
|
||||
const RadMtpCoupleVerifyCall c = call_of(pl, g);
|
||||
for (int i = row0; i < row0 + rows; ++i) rad_mtp_couple_verify_row(c, i);
|
||||
};
|
||||
auto untouched = [&](const Planes& pl) {
|
||||
for (int i = 0; i <= g; ++i) if (pl.tok[i] != 1000 + i) return false;
|
||||
return true;
|
||||
};
|
||||
Planes pl;
|
||||
build(&pl, 77);
|
||||
verify(&pl, 0, g);
|
||||
CHECK(!untouched(pl)); /* the block binds and writes */
|
||||
build(&pl, 77);
|
||||
verify(&pl, 1, g - 1);
|
||||
CHECK(untouched(pl)); /* rows after the first write nothing */
|
||||
build(&pl, 77);
|
||||
pl.st[1].round = 3;
|
||||
verify(&pl, 0, g);
|
||||
CHECK(untouched(pl)); /* a row of the wrong round ends the block's coupling */
|
||||
build(&pl, 77);
|
||||
pl.st[2].draw.seq = 9;
|
||||
verify(&pl, 0, g);
|
||||
CHECK(untouched(pl)); /* another request's state */
|
||||
build(&pl, 77);
|
||||
pl.st[2].pos += 1;
|
||||
verify(&pl, 0, g);
|
||||
CHECK(untouched(pl)); /* a stale position */
|
||||
build(&pl, 77);
|
||||
reinterpret_cast<RadSampleParams*>(pl.params + 2 * 24)->flags = RAD_SP_GREEDY;
|
||||
verify(&pl, 0, g);
|
||||
CHECK(untouched(pl)); /* a greedy row */
|
||||
build(&pl, 78);
|
||||
verify(&pl, 0, g);
|
||||
CHECK(untouched(pl)); /* a seed the state was not drawn under */
|
||||
build(&pl, 77);
|
||||
pl.st[1].nkids = 2;
|
||||
verify(&pl, 0, g);
|
||||
CHECK(untouched(pl)); /* a tree node is not a chain row */
|
||||
}
|
||||
|
||||
|
||||
/* The residual draw over a plane WIDER than the workgroup's 128 lanes (several slots per lane, ids
|
||||
* on and off Q's support, holes): the lane-ordered walk of rad_mtp_block_resid_draw must still
|
||||
* sample max(p P - Q, 0) normalised. Chi-square over the plane's ids against the dense closed
|
||||
* form; the negative control draws from a residual with Q's mass halved (a wrong law) and the same
|
||||
* statistic must see it. Also the tree sum: stat over the wide plane equals a plain ascending sum
|
||||
* to rounding (the tree is a different order, not a different value). */
|
||||
TEST(the_lane_ordered_residual_draw_over_a_wide_plane_is_exact) {
|
||||
const int N = 700;
|
||||
std::vector<int32_t> ids((size_t)N);
|
||||
std::vector<float> val((size_t)N);
|
||||
for (int j = 0; j < N; ++j) {
|
||||
ids[(size_t)j] = j % 11 == 5 ? -1 : 2 * j + 3;
|
||||
val[(size_t)j] = j % 11 == 5 ? -INFINITY : (float)((H((uint64_t)j * 7919 + 5) >> 40) % 4000) / 1000.0f - 2.0f;
|
||||
}
|
||||
int32_t qid[8]; float qv[8];
|
||||
for (int k = 0; k < 8; ++k) { qid[k] = 2 * (k * 83 + 1) + 3; qv[k] = 1.5f - 0.3f * (float)k; }
|
||||
RadMtpCoupleState st{};
|
||||
CHECK(rad_mtp_couple_build_q(qid, qv, 100000, &st));
|
||||
st.x = st.ids[1];
|
||||
RadBlockRow r{ ids.data(), val.data(), N, 0, 0.0f };
|
||||
rad_mtp_block_scan(&r, 100000);
|
||||
RadBlockStat t;
|
||||
CHECK(rad_mtp_block_stat(r, st, &t));
|
||||
double ref = 0, top = r.top;
|
||||
for (int j = 0; j < N; ++j) if (ids[(size_t)j] >= 0) ref += std::exp((double)val[(size_t)j] - top);
|
||||
CHECK(std::fabs(t.sum - ref) <= 1e-12 * ref);
|
||||
const double p = 0.8;
|
||||
std::map<int32_t, double> want;
|
||||
double tot = 0;
|
||||
for (int j = 0; j < N; ++j) {
|
||||
if (ids[(size_t)j] < 0) continue;
|
||||
const double P = std::exp((double)val[(size_t)j] - top) / ref;
|
||||
double Q = 0;
|
||||
for (int k = 0; k < st.count; ++k) if (st.ids[k] == ids[(size_t)j]) Q = st.q[k];
|
||||
const double m = std::max(p * P - Q, 0.0);
|
||||
want[ids[(size_t)j]] = m; tot += m;
|
||||
}
|
||||
const int D = 400000;
|
||||
std::map<int32_t, double> got, bad;
|
||||
for (int d = 0; d < D; ++d) {
|
||||
const double u = (double)(H((uint64_t)d * 2654435761ull + 11) >> 11) / 9007199254740992.0;
|
||||
int32_t y = -1;
|
||||
CHECK(rad_mtp_block_resid_draw(r, st, t, p, u, &y));
|
||||
got[y] += 1;
|
||||
/* control: the walk with the uniform skewed (a wrong law) */
|
||||
int32_t yb = -1;
|
||||
CHECK(rad_mtp_block_resid_draw(r, st, t, p, u * u, &yb));
|
||||
bad[yb] += 1;
|
||||
}
|
||||
auto chi = [&](std::map<int32_t, double>& c) {
|
||||
double x = 0, pc = 0, pe = 0; int cells = 0;
|
||||
for (auto& kv : want) {
|
||||
const double e = D * kv.second / tot, o = c.count(kv.first) ? c[kv.first] : 0;
|
||||
if (e < 5) { pc += o; pe += e; continue; }
|
||||
x += (o - e) * (o - e) / e; ++cells;
|
||||
}
|
||||
if (pe >= 5) { x += (pc - pe) * (pc - pe) / pe; ++cells; }
|
||||
return (x - (cells - 1)) / std::sqrt(2.0 * (cells - 1)); /* z of chi-square */
|
||||
};
|
||||
for (auto& kv : got) CHECK(want.count(kv.first) && want[kv.first] > 0);
|
||||
const double z = chi(got), zb = chi(bad);
|
||||
fprintf(stderr, " wide plane residual draw: z=%.2f negative control z=%.1f\n", z, zb);
|
||||
CHECK(std::fabs(z) < 4.5);
|
||||
CHECK(zb > 20);
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
File diff suppressed because it is too large.
Load diff
+98
-1
@@ -1528,7 +1528,7 @@ struct HostChain {
|
||||
|
||||
HostChain(int64_t n_vocab, int64_t max_seqs, int max_spec, const VocabView* vocab = nullptr,
|
||||
int64_t max_candidates = 8, const char* second_plugin = nullptr,
|
||||
bool draft_coupling = false)
|
||||
bool draft_coupling = false, bool verify_block = false, bool verify_traversal = false)
|
||||
: nv(n_vocab) {
|
||||
/* libref alone: it is the implementation under test, and a directory scan would also
|
||||
* pick up whatever else was built beside it. */
|
||||
@@ -1571,6 +1571,8 @@ struct HostChain {
|
||||
cfg.max_hist = 64;
|
||||
cfg.max_candidates = max_candidates;
|
||||
cfg.draft_coupling = draft_coupling;
|
||||
cfg.draft_verify_block = verify_block;
|
||||
cfg.draft_verify_traversal = verify_traversal;
|
||||
if (s.declare(&b, cfg) < 0) return;
|
||||
if (b.finish() < 0) return;
|
||||
prog = std::move(b.program());
|
||||
@@ -3597,6 +3599,101 @@ TEST(the_coupled_evidence_survives_the_drafters_greedy_build) {
|
||||
CHECK_EQ(ev.rows, (int64_t)6);
|
||||
}
|
||||
|
||||
/* --draft-verify block: the chain rows bound from round 1 on, consecutively, carry the block's
|
||||
* length in `blk` (what the verify's round-1 row decides from); the bonus row and a row after a
|
||||
* gap do not, and without the flag nothing is marked. */
|
||||
TEST(block_verification_marks_the_consecutive_bound_rows_of_a_chain) {
|
||||
HostChain ch(64, 2, 3, nullptr, 8, "spechub.so", true, true);
|
||||
if (!ch.ok) {
|
||||
fprintf(stderr, " SKIP %s: libref/spechub did not load from %s/kernels\n",
|
||||
::radtest::current(), home().c_str());
|
||||
return;
|
||||
}
|
||||
SamplingParams sp;
|
||||
sp.temp = 1.0f;
|
||||
const int32_t draft[3] = { 21, 22, 23 };
|
||||
Request r = make_req(7, { 1, 2, 3 }, sp);
|
||||
r.draft = draft;
|
||||
r.n_draft = 3;
|
||||
std::string err;
|
||||
CHECK_OK(ch.s.begin_request(r.id, r.sp, &err));
|
||||
const uint64_t n_out = r.output.size();
|
||||
RadMtpCoupleState bound[3];
|
||||
couple_states(r.id, r.sp.seed, draft, 3, rad_mtp_couple_verify_pos(n_out, 1), bound);
|
||||
CHECK_OK(ch.s.set_mtp_couple(r.id, bound, 3));
|
||||
std::vector<Request*> reqs = { &r };
|
||||
CHECK_OK(ch.s.build_step(reqs));
|
||||
for (int k = 0; k < 3; ++k) CHECK_EQ(ch.s.staged_couple(k).blk, 3);
|
||||
CHECK_EQ(ch.s.staged_couple(3).blk, 0);
|
||||
|
||||
/* row 1 stale: the block is row 0 alone; row 2 is bound but past the gap, so token-wise */
|
||||
bound[1].pos += 1;
|
||||
CHECK_OK(ch.s.set_mtp_couple(r.id, bound, 3));
|
||||
CHECK_OK(ch.s.build_step(reqs));
|
||||
CHECK_EQ(ch.s.staged_couple(0).blk, 1);
|
||||
CHECK_EQ(ch.s.staged_couple(1).round, 0);
|
||||
CHECK_EQ(ch.s.staged_couple(2).round, 3);
|
||||
CHECK_EQ(ch.s.staged_couple(2).blk, 0);
|
||||
|
||||
/* the same chain without the flag is not marked */
|
||||
HostChain plain(64, 2, 3, nullptr, 8, "spechub.so", true, false);
|
||||
CHECK(plain.ok);
|
||||
CHECK_OK(plain.s.begin_request(r.id, r.sp, &err));
|
||||
couple_states(r.id, r.sp.seed, draft, 3, rad_mtp_couple_verify_pos(n_out, 1), bound);
|
||||
CHECK_OK(plain.s.set_mtp_couple(r.id, bound, 3));
|
||||
CHECK_OK(plain.s.build_step(reqs));
|
||||
for (int k = 0; k < 3; ++k) CHECK_EQ(plain.s.staged_couple(k).blk, 0);
|
||||
}
|
||||
|
||||
/* TRAVERSAL VERIFICATION THROUGH THE SAMPLER (--draft-verify traversal, abi/rad_mtp_traversal.h).
|
||||
*
|
||||
* The chain 21, 22, 23 with a point-mass draft law at every round. The sampler stages the per-row
|
||||
* topology plane (parent row, draft token) and issues `mtp_trav_verify` instead of the row-wise
|
||||
* verify. With the target peaked on 31 at every row the walk rejects the chain (the first draft's
|
||||
* ratio is ~e^-20) and the root row's token comes from the residual law, 31; with the target
|
||||
* peaked on each draft the walk accepts the whole chain and the final token is the bonus row's
|
||||
* own pick. */
|
||||
TEST(traversal_verification_walks_the_staged_chain_through_the_sampler) {
|
||||
HostChain ch(64, 2, 3, nullptr, 8, "spechub.so", true, false, true);
|
||||
if (!ch.ok) {
|
||||
fprintf(stderr, " SKIP %s: libref/spechub did not load from %s/kernels\n",
|
||||
::radtest::current(), home().c_str());
|
||||
return;
|
||||
}
|
||||
CHECK(ch.s.staged_couple_op() != RAD_NULL_HANDLE);
|
||||
SamplingParams sp;
|
||||
sp.temp = 1.0f;
|
||||
const int32_t draft[3] = { 21, 22, 23 };
|
||||
Request r = make_req(7, { 1, 2, 3 }, sp);
|
||||
r.draft = draft;
|
||||
r.n_draft = 3;
|
||||
std::string err;
|
||||
CHECK_OK(ch.s.begin_request(r.id, r.sp, &err));
|
||||
const uint64_t n_out = r.output.size();
|
||||
RadMtpCoupleState bound[3];
|
||||
couple_states(r.id, r.sp.seed, draft, 3, rad_mtp_couple_verify_pos(n_out, 1), bound);
|
||||
CHECK_OK(ch.s.set_mtp_couple(r.id, bound, 3));
|
||||
|
||||
std::vector<float> lg(4 * 64, 0.0f);
|
||||
for (int row = 0; row < 4; ++row) lg[(size_t)row * 64 + 31] = 20.0f;
|
||||
std::vector<int32_t> tok;
|
||||
CHECK_OK(ch.step({ &r }, lg, &tok));
|
||||
REQUIRE_EQ(tok.size(), (size_t)4);
|
||||
CHECK_EQ(tok[0], 31); /* the chain is rejected at its first draft: the residual's token */
|
||||
|
||||
CHECK_OK(ch.s.set_mtp_couple(r.id, bound, 3));
|
||||
std::vector<float> lg2(4 * 64, 0.0f);
|
||||
lg2[0 * 64 + 21] = 20.0f;
|
||||
lg2[1 * 64 + 22] = 20.0f;
|
||||
lg2[2 * 64 + 23] = 20.0f;
|
||||
lg2[3 * 64 + 9] = 20.0f;
|
||||
CHECK_OK(ch.step({ &r }, lg2, &tok));
|
||||
CHECK_EQ(tok[0], 21);
|
||||
CHECK_EQ(tok[1], 22);
|
||||
CHECK_EQ(tok[2], 23);
|
||||
CHECK_EQ(tok[3], 9);
|
||||
}
|
||||
|
||||
/* The planes are sized for the rows a step can sample: one per token the step carries, so never
|
||||
* more than max_tok, and never fewer than max_seqs (a drafter's greedy pass). */
|
||||
TEST(the_sampler_rows_are_bounded_by_the_steps_tokens) {
|
||||
|
||||
@@ -276,9 +276,9 @@ TEST(the_coupling_uniform_is_the_specified_53_bit_draw) {
|
||||
* shift every later operand of a plane the host writes and the device reads. */
|
||||
TEST(the_coupling_wire_rows_keep_their_layout) {
|
||||
CHECK_EQ(sizeof(RadMtpCoupleDraw), (size_t)32);
|
||||
CHECK_EQ(sizeof(RadMtpCoupleState), (size_t)152);
|
||||
CHECK_EQ(sizeof(RadMtpCoupleState), (size_t)168);
|
||||
CHECK_EQ(RAD_MTP_COUPLE_DRAW_WORDS, (size_t)8);
|
||||
CHECK_EQ(RAD_MTP_COUPLE_STATE_WORDS, (size_t)38);
|
||||
CHECK_EQ(RAD_MTP_COUPLE_STATE_WORDS, (size_t)42);
|
||||
CHECK_EQ(offsetof(RadMtpCoupleState, q) % 8, (size_t)0);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,879 @@
|
||||
/* tests/spechub_device_test.cpp -- the spechub plugin's DEVICE verify rows against their host rows,
|
||||
* on a card.
|
||||
*
|
||||
* mtp_couple_verify rows whose state drafted 1..4 children (the chain's maximal coupling for one
|
||||
* child, recursive rejection sampling for several, abi/rad_mtp_rrs.h), and
|
||||
* --draft-verify block's chains of 1..8 rows (abi/rad_mtp_block.h)
|
||||
* mtp_trav_verify --draft-verify traversal, abi/rad_mtp_traversal.h: requests of 1..21 rows
|
||||
*
|
||||
* The device rows and the host rows run the same shared header code, so the outputs must agree
|
||||
* token for token. The traversal walk and the block decision are also compared bit for bit in
|
||||
* what they write. Skipped without a card (the host backend), or without the device rows in the
|
||||
* plugin. Two tests read the plugin's code object (no card needed): the verify kernels' private
|
||||
* segment, since the queue sizes its scratch by it. Run on a card under the shared lock:
|
||||
* flock gpu.lock build/tests/spechub_device_test
|
||||
*/
|
||||
#include "rad_test.h"
|
||||
#include "device/aql.h"
|
||||
|
||||
#include "rad_abi.h"
|
||||
#include "rad_device.h"
|
||||
#include "rad_mtp_rrs.h"
|
||||
#include "rad_mtp_traversal.h"
|
||||
#include "rad_sample.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#ifndef RAD_SPECHUB_LIB
|
||||
#define RAD_SPECHUB_LIB ""
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
struct Plugin {
|
||||
void* handle = nullptr;
|
||||
int (*count)() = nullptr;
|
||||
const RadKernelInfo* (*at)(int) = nullptr;
|
||||
bool open(const char* path) {
|
||||
if (const char* o = std::getenv("RAD_SPECHUB_LIB_OVERRIDE")) path = o; /* a baseline build, for RAD_BENCH */
|
||||
handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
|
||||
if (!handle) return false;
|
||||
count = (int (*)())dlsym(handle, "rad_kernel_count");
|
||||
at = (const RadKernelInfo* (*)(int))dlsym(handle, "rad_kernel_at");
|
||||
return count && at;
|
||||
}
|
||||
const RadKernelInfo* row(const char* name) const {
|
||||
for (int i = 0; i < count(); ++i) {
|
||||
const RadKernelInfo* r = at(i);
|
||||
if (r && r->name && std::string(r->name) == name) return r;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
~Plugin() { if (handle) dlclose(handle); }
|
||||
};
|
||||
|
||||
/* A tensor over a host buffer or its device twin. */
|
||||
struct Buf {
|
||||
std::vector<uint8_t> host;
|
||||
void* dev = nullptr;
|
||||
RadTensor t{};
|
||||
Buf(uint32_t dtype, std::vector<int64_t> shape, size_t elem) {
|
||||
t.dtype = dtype;
|
||||
t.rank = (uint32_t)shape.size();
|
||||
int64_t n = 1;
|
||||
for (size_t i = 0; i < shape.size(); ++i) { t.shape[i] = shape[i]; n *= shape[i]; }
|
||||
int64_t acc = 1;
|
||||
for (int i = (int)shape.size() - 1; i >= 0; --i) { t.stride[i] = acc; acc *= shape[i]; }
|
||||
host.assign((size_t)n * elem + 16, 0);
|
||||
}
|
||||
~Buf() { if (dev) rad_dev_free(dev, RAD_MEM_DEVICE); }
|
||||
RadTensor on_host() { RadTensor x = t; x.data = host.data(); return x; }
|
||||
RadTensor on_dev(RadStream s) {
|
||||
if (!dev) dev = rad_dev_alloc((int64_t)host.size(), RAD_MEM_DEVICE);
|
||||
rad_memcpy_async(dev, host.data(), (int64_t)host.size(), s);
|
||||
RadTensor x = t; x.data = dev; return x;
|
||||
}
|
||||
void fetch(RadStream s) { rad_memcpy_async(host.data(), dev, (int64_t)host.size(), s); }
|
||||
};
|
||||
|
||||
RadParam P(const char* k, long long v) { return RAD_INT(k, v); }
|
||||
|
||||
uint32_t g_seed = 424242;
|
||||
uint32_t rnd() { g_seed = g_seed * 1664525u + 1013904223u; return (g_seed >> 8) & 0xffffff; }
|
||||
|
||||
RadMtpCoupleDraw make_draw(uint64_t seed, uint64_t base, uint64_t seq, int card, uint32_t mode) {
|
||||
RadMtpCoupleDraw d{};
|
||||
d.seed = seed; d.base = base; d.seq = seq; d.card = card; d.mode = mode;
|
||||
return d;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* The queue's scratch is sized by a kernel's private segment times every lane it may run: couple_verify
|
||||
* once carried the block row's planes (17.8 KB a lane, with a workgroup of 128 lanes of which one
|
||||
* works) and the first sampled tree (>= 5 rows) faulted the queue with OUT_OF_RESOURCES once
|
||||
* HSA_NO_SCRATCH_RECLAIM kept the scratch resident. Pin the code object: the workgroup kernel keeps
|
||||
* a small private segment (the serial helper that needs the big one is a kernel of its own). Runs
|
||||
* without a card: it reads the plugin's own code object. */
|
||||
TEST(the_verify_kernels_ask_for_little_scratch) {
|
||||
FILE* f = std::fopen(RAD_SPECHUB_LIB, "rb");
|
||||
if (!f) { fprintf(stderr, " SKIP: %s not readable\n", RAD_SPECHUB_LIB); return; }
|
||||
std::vector<unsigned char> img;
|
||||
unsigned char buf[65536];
|
||||
for (size_t n; (n = std::fread(buf, 1, sizeof buf, f)) > 0;) img.insert(img.end(), buf, buf + n);
|
||||
std::fclose(f);
|
||||
static const char kMagic[] = "__CLANG_OFFLOAD_BUNDLE__";
|
||||
auto at = std::search(img.begin(), img.end(), kMagic, kMagic + sizeof kMagic - 1);
|
||||
if (at == img.end()) { fprintf(stderr, " SKIP: no device code in the plugin\n"); return; }
|
||||
const void* co = nullptr;
|
||||
size_t cosz = 0;
|
||||
std::string why;
|
||||
REQUIRE(rad::aql::bundle_code_object(&*at, { "hipv4-amdgcn-amd-amdhsa--gfx1201", "hip-amdgcn-amd-amdhsa--gfx1201",
|
||||
"amdgcn-amd-amdhsa--gfx1201" }, &co, &cosz, &why));
|
||||
std::vector<rad::aql::KernelMeta> ks;
|
||||
REQUIRE(rad::aql::code_object_kernels(co, cosz, &ks, &why));
|
||||
int seen = 0, seen_trav = 0;
|
||||
for (const auto& k : ks) {
|
||||
if (k.symbol.find("trav_verify") != std::string::npos) {
|
||||
/* the traversal verify keeps a request in registers and LDS: no scratch at all, so its
|
||||
* dispatch never waits for the queue's scratch */
|
||||
++seen_trav;
|
||||
CHECK_EQ((unsigned)k.private_size, 0u);
|
||||
fprintf(stderr, " trav_verify private segment %u bytes\n", (unsigned)k.private_size);
|
||||
continue;
|
||||
}
|
||||
if (k.symbol.find("couple_verify") == std::string::npos || k.symbol.find("wide") != std::string::npos)
|
||||
continue;
|
||||
++seen;
|
||||
CHECK(k.private_size <= 256u);
|
||||
fprintf(stderr, " couple_verify private segment %u bytes\n", (unsigned)k.private_size);
|
||||
}
|
||||
CHECK_EQ(seen, 1);
|
||||
CHECK_EQ(seen_trav, 1);
|
||||
}
|
||||
|
||||
TEST(the_device_verify_row_agrees_with_the_host_row) {
|
||||
RadDeviceProps dp{};
|
||||
if (rad_dev_count() < 1 || rad_dev_props(0, &dp) != RAD_OK || dp.is_host_backend) {
|
||||
fprintf(stderr, " SKIP %s: no card\n", ::radtest::current());
|
||||
return;
|
||||
}
|
||||
Plugin pl;
|
||||
if (!pl.open(RAD_SPECHUB_LIB)) { fprintf(stderr, " SKIP: %s did not load\n", RAD_SPECHUB_LIB); return; }
|
||||
const RadKernelInfo* dver = pl.row("spechub_mtp_couple_verify");
|
||||
const RadKernelInfo* hver = pl.row("ref_mtp_couple_verify");
|
||||
if (!dver) { fprintf(stderr, " SKIP: the plugin has no device rows\n"); return; }
|
||||
REQUIRE(hver);
|
||||
RadStream s{};
|
||||
REQUIRE_EQ(rad_stream_create(&s, 0), RAD_OK);
|
||||
const int SW = (int)RAD_MTP_COUPLE_STATE_WORDS;
|
||||
const int64_t VOC = 50000;
|
||||
|
||||
/* ---- verify over states with 1..4 children, at a 24-slot plane and at the sampler's full 1024-slot
|
||||
* plane (top_k 0: every slot live on even rows, 40 live slots and holes on odd rows) -- the
|
||||
* workgroup's compaction and weights over several chunks, then the host's decision over them */
|
||||
for (const int NC : { 24, 1024, 1100 }) {
|
||||
const int M = 64;
|
||||
const bool wide = NC > 64; /* 1100: past the workgroup's plane, one lane decides */
|
||||
Buf cid(RAD_I32, { M, NC }, 4), cval(RAD_F32, { M, NC }, 4), prm(RAD_U32, { M, 24 }, 4);
|
||||
Buf sts(RAD_U32, { M, SW }, 4), tok(RAD_I32, { M }, 4);
|
||||
int32_t* pi = (int32_t*)cid.host.data();
|
||||
float* pv = (float*)cval.host.data();
|
||||
RadSampleParams* pp = (RadSampleParams*)prm.host.data();
|
||||
RadMtpCoupleState* ps_ = (RadMtpCoupleState*)sts.host.data();
|
||||
int32_t* pt = (int32_t*)tok.host.data();
|
||||
for (int m = 0; m < M; ++m) {
|
||||
const int live = !wide || m % 2 == 0 ? NC : 40;
|
||||
for (int c = 0; c < NC; ++c) {
|
||||
if (c >= live) { pi[m * NC + c] = -1; pv[m * NC + c] = -INFINITY; continue; }
|
||||
int id;
|
||||
bool dup;
|
||||
do {
|
||||
id = (int)(rnd() % (wide ? VOC : 40));
|
||||
dup = false;
|
||||
for (int q = 0; q < c; ++q) dup = dup || pi[m * NC + q] == id;
|
||||
} while (dup);
|
||||
pi[m * NC + c] = id;
|
||||
pv[m * NC + c] = 2.0f - (wide ? 0.01f : 0.25f) * c - (rnd() % 100) * 0.01f;
|
||||
}
|
||||
int32_t qid[8];
|
||||
float qv[8];
|
||||
for (int c = 0; c < 8; ++c) {
|
||||
/* Q's support among the candidates (wide) or the 40-token universe */
|
||||
qid[c] = wide ? pi[m * NC + (int)(rnd() % (uint32_t)(live < 60 ? live : 60))] : (int)(rnd() % 40);
|
||||
qv[c] = 2.0f - 0.3f * c;
|
||||
}
|
||||
for (int c = 0; c < 8; ++c)
|
||||
for (int q = 0; q < c; ++q) if (qid[q] == qid[c]) qid[c] = 100 + c;
|
||||
RadMtpCoupleState s0{};
|
||||
rad_mtp_couple_build_q(qid, qv, VOC, &s0);
|
||||
s0.draw = make_draw(0x900ull + (uint64_t)m, 3, 1, 0, 1u);
|
||||
s0.status = 1; s0.round = 1; s0.pos = 11 + (uint64_t)m;
|
||||
const int nk = 1 + m % 4;
|
||||
if (nk >= 2) rad_mtp_rrs_draw(&s0, 0, nk); else s0.x = s0.ids[0];
|
||||
ps_[m] = s0;
|
||||
pp[m].seed = s0.draw.seed;
|
||||
pp[m].pos = s0.pos;
|
||||
pp[m].flags = m % 16 == 15 ? RAD_SP_GREEDY : 0;
|
||||
pt[m] = -9;
|
||||
}
|
||||
const std::vector<RadParam> pr = { P("M", M), P("n_cand", NC), P("n_vocab", (int)VOC) };
|
||||
Buf htok = tok, hst2 = sts;
|
||||
{
|
||||
RadTensor ts[5] = { cid.on_host(), cval.on_host(), prm.on_host(), hst2.on_host(), htok.on_host() };
|
||||
RadArgs a{}; a.t = ts; a.n_t = 5; a.p = pr.data(); a.n_p = (int)pr.size();
|
||||
REQUIRE_EQ(hver->launch(&a, s), RAD_OK);
|
||||
}
|
||||
{
|
||||
RadTensor ts[5] = { cid.on_dev(s), cval.on_dev(s), prm.on_dev(s), sts.on_dev(s), tok.on_dev(s) };
|
||||
RadArgs a{}; a.t = ts; a.n_t = 5; a.p = pr.data(); a.n_p = (int)pr.size();
|
||||
REQUIRE_EQ(dver->launch(&a, s), RAD_OK);
|
||||
tok.fetch(s);
|
||||
sts.fetch(s);
|
||||
REQUIRE_EQ(rad_stream_sync(s), RAD_OK);
|
||||
}
|
||||
int multi = 0, kept = 0;
|
||||
for (int m = 0; m < M; ++m) {
|
||||
const int32_t g = ((int32_t*)tok.host.data())[m], w = ((int32_t*)htok.host.data())[m];
|
||||
CHECK_EQ(g, w);
|
||||
/* the verify leaves the state plane alone */
|
||||
RadMtpCoupleState dg, hw;
|
||||
std::memcpy(&dg, sts.host.data() + (size_t)m * SW * 4, sizeof dg);
|
||||
std::memcpy(&hw, hst2.host.data() + (size_t)m * SW * 4, sizeof hw);
|
||||
CHECK(std::memcmp(&dg, &hw, sizeof dg) == 0);
|
||||
multi += ps_[m].nkids >= 2 && w != -9;
|
||||
kept += w == -9;
|
||||
}
|
||||
CHECK(multi >= 20);
|
||||
CHECK_EQ(kept, 4); /* the greedy rows keep the ordinary token */
|
||||
}
|
||||
rad_stream_destroy(s);
|
||||
}
|
||||
|
||||
/* --draft-verify block (abi/rad_mtp_block.h): the device verify row decides a whole chain from its
|
||||
* round-1 row and writes the tokens of rows 0 .. tau; the host row is the same shared code over the
|
||||
* dense plane. Blocks of 1..8 rows, planes of 24 (the common case), 1000 (live slots compacted over
|
||||
* several chunks of the workgroup) and 1100 (past the LDS plane: one thread runs the helper) slots,
|
||||
* with holes; tokens must agree row for row, rows the block does not decide keep their sentinel. */
|
||||
TEST(the_device_block_verify_agrees_with_the_host_row) {
|
||||
RadDeviceProps dp{};
|
||||
if (rad_dev_count() < 1 || rad_dev_props(0, &dp) != RAD_OK || dp.is_host_backend) {
|
||||
fprintf(stderr, " SKIP %s: no card\n", ::radtest::current());
|
||||
return;
|
||||
}
|
||||
Plugin pl;
|
||||
if (!pl.open(RAD_SPECHUB_LIB)) { fprintf(stderr, " SKIP: %s did not load\n", RAD_SPECHUB_LIB); return; }
|
||||
const RadKernelInfo* dver = pl.row("spechub_mtp_couple_verify");
|
||||
const RadKernelInfo* hver = pl.row("ref_mtp_couple_verify");
|
||||
if (!dver) { fprintf(stderr, " SKIP: the plugin has no device rows\n"); return; }
|
||||
REQUIRE(hver);
|
||||
RadStream s{};
|
||||
REQUIRE_EQ(rad_stream_create(&s, 0), RAD_OK);
|
||||
const int SW = (int)RAD_MTP_COUPLE_STATE_WORDS;
|
||||
const int64_t VOC = 50000;
|
||||
int shortened = 0, whole = 0, writes = 0;
|
||||
for (int NC : { 24, 1000, 1100, 1001 }) { /* 1001: wide, 300..500 live slots a row */
|
||||
const bool wide = NC == 1001;
|
||||
const int NSEQ = 40, MAXROWS = 9;
|
||||
const int M = NSEQ * MAXROWS;
|
||||
Buf cid(RAD_I32, { M, NC }, 4), cval(RAD_F32, { M, NC }, 4), prm(RAD_U32, { M, 24 }, 4);
|
||||
Buf sts(RAD_U32, { M, SW }, 4), tok(RAD_I32, { M }, 4);
|
||||
int32_t* pi = (int32_t*)cid.host.data();
|
||||
float* pv = (float*)cval.host.data();
|
||||
RadSampleParams* pp = (RadSampleParams*)prm.host.data();
|
||||
RadMtpCoupleState* ps_ = (RadMtpCoupleState*)sts.host.data();
|
||||
int32_t* pt = (int32_t*)tok.host.data();
|
||||
for (int m = 0; m < M; ++m) { pt[m] = -9; for (int c = 0; c < NC; ++c) { pi[m * NC + c] = -1; pv[m * NC + c] = -INFINITY; } }
|
||||
int row = 0;
|
||||
for (int q = 0; q < NSEQ; ++q) {
|
||||
const int g = 1 + q % 8;
|
||||
const uint64_t seed = 0xb10c0000ull + (uint64_t)q, pos0 = 20 + 3 * (uint64_t)q;
|
||||
for (int i = 0; i <= g; ++i, ++row) {
|
||||
/* live candidates at random slots */
|
||||
const int nlive = wide ? 300 + (int)(rnd() % 200) : 6 + (int)(rnd() % 19);
|
||||
int ids[512];
|
||||
for (int c = 0; c < nlive; ++c) {
|
||||
bool dup;
|
||||
do {
|
||||
ids[c] = (int)(rnd() % (wide ? 20000u : 40u));
|
||||
dup = false;
|
||||
for (int k = 0; k < c; ++k) dup = dup || ids[k] == ids[c];
|
||||
} while (dup);
|
||||
int slot;
|
||||
do { slot = (int)(rnd() % NC); } while (pi[row * NC + slot] >= 0);
|
||||
pi[row * NC + slot] = ids[c];
|
||||
pv[row * NC + slot] = wide ? 3.0f - 0.02f * c - (rnd() % 100) * 0.01f : 3.0f - 0.35f * c - (rnd() % 100) * 0.01f;
|
||||
}
|
||||
pp[row].seed = seed;
|
||||
pp[row].pos = pos0 + (uint64_t)i;
|
||||
if (i == g) continue;
|
||||
int32_t qid[8];
|
||||
float qv[8];
|
||||
const int nq = 3 + (int)(rnd() % 6);
|
||||
for (int c = 0; c < 8; ++c) {
|
||||
qid[c] = c < nq ? (c < nlive && rnd() % 4 ? ids[c] : (int)(rnd() % 40)) : -1;
|
||||
qv[c] = c < nq ? 2.5f - 0.4f * c - (rnd() % 100) * 0.01f : -INFINITY;
|
||||
for (int k = 0; k < c; ++k) if (qid[c] >= 0 && qid[k] == qid[c]) qid[c] = 100 + c;
|
||||
}
|
||||
RadMtpCoupleState st{};
|
||||
REQUIRE(rad_mtp_couple_build_q(qid, qv, VOC, &st));
|
||||
st.draw = make_draw(seed, pos0, 7 + (uint64_t)q, 0, 1u);
|
||||
st.status = 1; st.round = i + 1; st.pos = pos0 + (uint64_t)i; st.blk = g;
|
||||
st.x = st.ids[rnd() % (uint32_t)st.count];
|
||||
ps_[row] = st;
|
||||
}
|
||||
}
|
||||
const std::vector<RadParam> pr = { P("M", M), P("n_cand", NC), P("n_vocab", (int)VOC) };
|
||||
Buf htok = tok;
|
||||
{
|
||||
RadTensor ts[5] = { cid.on_host(), cval.on_host(), prm.on_host(), sts.on_host(), htok.on_host() };
|
||||
RadArgs a{}; a.t = ts; a.n_t = 5; a.p = pr.data(); a.n_p = (int)pr.size();
|
||||
REQUIRE_EQ(hver->launch(&a, s), RAD_OK);
|
||||
}
|
||||
{
|
||||
RadTensor ts[5] = { cid.on_dev(s), cval.on_dev(s), prm.on_dev(s), sts.on_dev(s), tok.on_dev(s) };
|
||||
RadArgs a{}; a.t = ts; a.n_t = 5; a.p = pr.data(); a.n_p = (int)pr.size();
|
||||
REQUIRE_EQ(dver->launch(&a, s), RAD_OK);
|
||||
tok.fetch(s);
|
||||
REQUIRE_EQ(rad_stream_sync(s), RAD_OK);
|
||||
}
|
||||
row = 0;
|
||||
for (int q = 0; q < NSEQ; ++q) {
|
||||
const int g = 1 + q % 8;
|
||||
int written = 0;
|
||||
for (int i = 0; i <= g; ++i) {
|
||||
const int32_t gv = ((int32_t*)tok.host.data())[row + i], wv = ((int32_t*)htok.host.data())[row + i];
|
||||
CHECK_EQ(gv, wv);
|
||||
written += wv != -9;
|
||||
}
|
||||
writes += written > 0;
|
||||
whole += written == g; /* tau == g: every draft written, the bonus row alone untouched */
|
||||
shortened += written > 0 && written < g;
|
||||
row += g + 1;
|
||||
}
|
||||
}
|
||||
CHECK(writes >= 100);
|
||||
CHECK(shortened >= 10);
|
||||
CHECK(whole >= 10);
|
||||
rad_stream_destroy(s);
|
||||
}
|
||||
|
||||
/* Traversal verification (abi/rad_mtp_traversal.h): the device row against the host row, over
|
||||
* requests whose rows form a small tree (a root with two children, one of them with two, and a
|
||||
* chain below the other). The device prepares the tree with a workgroup (integer masses, whose sums
|
||||
* do not depend on the order) and runs the header's walk over it, so the tokens must be identical;
|
||||
* planes of 12, 1024 and 1100 slots. */
|
||||
TEST(the_device_traversal_verify_agrees_with_the_host_row) {
|
||||
RadDeviceProps dp{};
|
||||
if (rad_dev_count() < 1 || rad_dev_props(0, &dp) != RAD_OK || dp.is_host_backend) {
|
||||
fprintf(stderr, " SKIP %s: no card\n", ::radtest::current());
|
||||
return;
|
||||
}
|
||||
Plugin pl;
|
||||
if (!pl.open(RAD_SPECHUB_LIB)) { fprintf(stderr, " SKIP: %s did not load\n", RAD_SPECHUB_LIB); return; }
|
||||
const RadKernelInfo* dtr = pl.row("spechub_mtp_trav_verify");
|
||||
const RadKernelInfo* htr = pl.row("ref_mtp_trav_verify");
|
||||
if (!dtr) { fprintf(stderr, " SKIP: the plugin has no traversal device row\n"); return; }
|
||||
REQUIRE(htr);
|
||||
RadStream s{};
|
||||
REQUIRE_EQ(rad_stream_create(&s, 0), RAD_OK);
|
||||
const int SW = (int)RAD_MTP_COUPLE_STATE_WORDS;
|
||||
const int64_t VOC = 50000;
|
||||
for (const int NC : { 12, 1024, 1100 }) {
|
||||
const bool wide = NC > 64;
|
||||
const int RQ = 12, NR = 7, M = RQ * NR;
|
||||
const int par[NR] = { -1, 0, 0, 1, 1, 2, 5 };
|
||||
const int nkid[NR] = { 2, 2, 1, 0, 0, 1, 0 };
|
||||
Buf cid(RAD_I32, { M, NC }, 4), cval(RAD_F32, { M, NC }, 4), prm(RAD_U32, { M, 24 }, 4);
|
||||
Buf sts(RAD_U32, { M, SW }, 4), tok(RAD_I32, { M }, 4), topo(RAD_I32, { M, 2 }, 4);
|
||||
int32_t* pi = (int32_t*)cid.host.data();
|
||||
float* pv = (float*)cval.host.data();
|
||||
RadSampleParams* pp = (RadSampleParams*)prm.host.data();
|
||||
RadMtpCoupleState* ps_ = (RadMtpCoupleState*)sts.host.data();
|
||||
int32_t* pt = (int32_t*)tok.host.data();
|
||||
int32_t* ptopo = (int32_t*)topo.host.data();
|
||||
for (int q = 0; q < RQ; ++q) {
|
||||
const uint64_t seed = 0x700ull + (uint64_t)q, p0 = 20 + (uint64_t)q;
|
||||
int depth[NR] = {};
|
||||
int32_t childtok[NR][4] = {};
|
||||
for (int n = 0; n < NR; ++n) {
|
||||
const int m = q * NR + n;
|
||||
depth[n] = n ? depth[par[n]] + 1 : 0;
|
||||
/* wide: the sampler's full plane at top_k 0 on even requests, 40 live slots and holes on odd */
|
||||
const int live = !wide || q % 2 == 0 ? NC : 40;
|
||||
for (int c = 0; c < NC; ++c) {
|
||||
if (wide && c >= live) { pi[m * NC + c] = -1; pv[m * NC + c] = -INFINITY; continue; }
|
||||
int id;
|
||||
bool dup;
|
||||
do {
|
||||
id = (int)(rnd() % (wide ? VOC : 14));
|
||||
dup = false;
|
||||
for (int z = 0; z < c; ++z) dup = dup || pi[m * NC + z] == id;
|
||||
} while (dup);
|
||||
pi[m * NC + c] = id;
|
||||
pv[m * NC + c] = wide ? 2.0f - 0.01f * c - (rnd() % 100) * 0.01f
|
||||
: (c < 10 ? 2.0f - 0.25f * c : -INFINITY) - (rnd() % 100) * 0.01f;
|
||||
}
|
||||
int32_t qid[8];
|
||||
float qv[8];
|
||||
for (int c = 0; c < 8; ++c) {
|
||||
qid[c] = wide ? pi[m * NC + (int)(rnd() % (uint32_t)(live < 60 ? live : 60))] : (int)(rnd() % 14);
|
||||
qv[c] = 2.0f - 0.3f * c - (rnd() % 50) * 0.01f;
|
||||
}
|
||||
for (int c = 0; c < 8; ++c)
|
||||
for (int z = 0; z < c; ++z) if (qid[z] == qid[c]) qid[c] = 100 + c;
|
||||
RadMtpCoupleState s0{};
|
||||
rad_mtp_couple_build_q(qid, qv, VOC, &s0);
|
||||
s0.draw = make_draw(seed, 3, 1, 0, 1u);
|
||||
s0.status = 1; s0.round = depth[n] + 1; s0.pos = p0 + (uint64_t)depth[n];
|
||||
if (nkid[n] >= 2) rad_mtp_rrs_draw(&s0, (uint64_t)n, nkid[n]);
|
||||
else { s0.x = s0.ids[0]; s0.nkids = 0; }
|
||||
for (int c = 0; c < nkid[n]; ++c) childtok[n][c] = s0.ids[c];
|
||||
if (nkid[n] > 0) ps_[m] = s0; else ps_[m].status = 0;
|
||||
pp[m].seed = seed;
|
||||
pp[m].pos = p0 + (uint64_t)depth[n];
|
||||
pp[m].flags = q == 5 ? RAD_SP_GREEDY : 0; /* one request is greedy: untouched */
|
||||
pt[m] = -9;
|
||||
}
|
||||
int seen[NR] = {};
|
||||
for (int n = 0; n < NR; ++n) {
|
||||
const int m = q * NR + n;
|
||||
ptopo[2 * m] = n ? q * NR + par[n] : -1;
|
||||
ptopo[2 * m + 1] = n ? childtok[par[n]][seen[par[n]]++] : 0;
|
||||
}
|
||||
}
|
||||
const std::vector<RadParam> pr = { P("M", M), P("n_cand", NC), P("n_vocab", (int)VOC) };
|
||||
Buf htok = tok;
|
||||
{
|
||||
RadTensor ts[6] = { cid.on_host(), cval.on_host(), prm.on_host(), sts.on_host(), htok.on_host(),
|
||||
topo.on_host() };
|
||||
RadArgs a{}; a.t = ts; a.n_t = 6; a.p = pr.data(); a.n_p = (int)pr.size();
|
||||
REQUIRE_EQ(htr->launch(&a, s), RAD_OK);
|
||||
}
|
||||
{
|
||||
RadTensor ts[6] = { cid.on_dev(s), cval.on_dev(s), prm.on_dev(s), sts.on_dev(s), tok.on_dev(s),
|
||||
topo.on_dev(s) };
|
||||
RadArgs a{}; a.t = ts; a.n_t = 6; a.p = pr.data(); a.n_p = (int)pr.size();
|
||||
REQUIRE_EQ(dtr->launch(&a, s), RAD_OK);
|
||||
tok.fetch(s);
|
||||
REQUIRE_EQ(rad_stream_sync(s), RAD_OK);
|
||||
}
|
||||
int written = 0, greedy_kept = 0;
|
||||
for (int m = 0; m < M; ++m) {
|
||||
const int32_t g = ((int32_t*)tok.host.data())[m], w = ((int32_t*)htok.host.data())[m];
|
||||
CHECK_EQ(g, w);
|
||||
written += w != -9;
|
||||
if (m / NR == 5) greedy_kept += w == -9;
|
||||
}
|
||||
CHECK(written >= RQ);
|
||||
CHECK_EQ(greedy_kept, NR);
|
||||
}
|
||||
rad_stream_destroy(s);
|
||||
}
|
||||
|
||||
/* The traversal verify over what a step can stage, device against host, bit for bit: requests of 1,
|
||||
* 4, 11 and 21 rows in one launch (a workgroup's four waves each take every fourth row; a lone bonus
|
||||
* row; a tree wider than a wave's share), Gumbel and RheoSampling nodes, chain rows whose x is not
|
||||
* the shortlist's first id, greedy and stale rows, targets that
|
||||
* lack draft tokens, and planes of 24, 1024 (the sampler's), 2048 (two passes a lane), 1100 and 4100
|
||||
* slots (no whole chunks; the fixed-point unit one bit coarser) with holes and -inf entries. */
|
||||
TEST(the_device_traversal_verify_agrees_with_the_host_row_on_mixed_requests) {
|
||||
RadDeviceProps dp{};
|
||||
if (rad_dev_count() < 1 || rad_dev_props(0, &dp) != RAD_OK || dp.is_host_backend) {
|
||||
fprintf(stderr, " SKIP %s: no card\n", ::radtest::current());
|
||||
return;
|
||||
}
|
||||
Plugin pl;
|
||||
if (!pl.open(RAD_SPECHUB_LIB)) { fprintf(stderr, " SKIP: %s did not load\n", RAD_SPECHUB_LIB); return; }
|
||||
const RadKernelInfo* dtr = pl.row("spechub_mtp_trav_verify");
|
||||
const RadKernelInfo* htr = pl.row("ref_mtp_trav_verify");
|
||||
if (!dtr) { fprintf(stderr, " SKIP: the plugin has no traversal device row\n"); return; }
|
||||
REQUIRE(htr);
|
||||
RadStream s{};
|
||||
REQUIRE_EQ(rad_stream_create(&s, 0), RAD_OK);
|
||||
const int SW = (int)RAD_MTP_COUPLE_STATE_WORDS;
|
||||
const int64_t VOC = 60000;
|
||||
/* the request shapes: parents by row (root -1) */
|
||||
struct Shape { int nr; int par[24]; };
|
||||
const Shape shapes[] = {
|
||||
{ 1, { -1 } },
|
||||
{ 4, { -1, 0, 1, 2 } },
|
||||
{ 11, { -1, 0, 0, 1, 1, 2, 3, 3, 5, 6, 6 } },
|
||||
{ 21, { -1, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4 } },
|
||||
{ 7, { -1, 0, 1, 2, 0, 1, 2 } },
|
||||
};
|
||||
long written = 0, paths3 = 0, finals = 0, rheo_nodes = 0;
|
||||
for (const int NC : { 24, 1024, 2048, 1100, 4100 }) {
|
||||
const int REPS = NC > 2048 ? 6 : 24;
|
||||
int M = 0;
|
||||
for (int r = 0; r < REPS; ++r) M += shapes[r % 5].nr;
|
||||
Buf cid(RAD_I32, { M, NC }, 4), cval(RAD_F32, { M, NC }, 4), prm(RAD_U32, { M, 24 }, 4);
|
||||
Buf sts(RAD_U32, { M, SW }, 4), tok(RAD_I32, { M }, 4), topo(RAD_I32, { M, 2 }, 4);
|
||||
int32_t* pi = (int32_t*)cid.host.data();
|
||||
float* pv = (float*)cval.host.data();
|
||||
RadSampleParams* pp = (RadSampleParams*)prm.host.data();
|
||||
RadMtpCoupleState* ps_ = (RadMtpCoupleState*)sts.host.data();
|
||||
int32_t* pt = (int32_t*)tok.host.data();
|
||||
int32_t* ptopo = (int32_t*)topo.host.data();
|
||||
int row0 = 0;
|
||||
for (int r = 0; r < REPS; ++r) {
|
||||
const Shape& sh = shapes[r % 5];
|
||||
const uint64_t seed = 0x51000ull + 977ull * (uint64_t)r + (uint64_t)NC, pos0 = 40 + (uint64_t)r;
|
||||
const int kind = r % 4; /* 0 close laws, 1 flat Q with a token the target lacks, 2 Q off the target, 3 close */
|
||||
const int rheo = r % 6 == 4 ? 2 : r % 6 == 5 ? 3 : 0; /* scheme of the nodes with several children */
|
||||
int depth[24] = {}, nkid[24] = {}, seen[24] = {};
|
||||
int32_t childtok[24][8] = {};
|
||||
for (int n = 1; n < sh.nr; ++n) ++nkid[sh.par[n]];
|
||||
for (int n = 0; n < sh.nr; ++n) {
|
||||
const int m = row0 + n;
|
||||
depth[n] = n ? depth[sh.par[n]] + 1 : 0;
|
||||
/* the plane: a peaked head, a long tail, holes and -inf entries at random slots */
|
||||
const int live = NC <= 24 ? 14 : (r % 3 == 0 ? 20 : NC - NC / 7);
|
||||
std::vector<int> ids((size_t)NC, -1);
|
||||
for (int c = 0; c < NC; ++c) { pi[(size_t)m * NC + c] = -1; pv[(size_t)m * NC + c] = -INFINITY; }
|
||||
for (int c = 0, placed = 0; c < NC && placed < live; ++c) {
|
||||
if (NC > 24 && r % 3 != 0 && rnd() % 7 == 0) continue; /* a hole */
|
||||
bool dup;
|
||||
int id;
|
||||
do { id = (int)(rnd() % (uint32_t)(VOC - 2000)); dup = false;
|
||||
for (int k = 0; k < c; ++k) dup = dup || ids[(size_t)k] == id; } while (dup);
|
||||
ids[(size_t)c] = id;
|
||||
pi[(size_t)m * NC + c] = id;
|
||||
pv[(size_t)m * NC + c] = rnd() % 23 == 0 ? -INFINITY
|
||||
: placed < 20 ? 9.0f - 0.8f * (float)placed - (rnd() % 100) * 0.01f
|
||||
: 1.0f - 0.003f * (float)placed - (rnd() % 100) * 0.01f;
|
||||
++placed;
|
||||
}
|
||||
pp[m].seed = seed; pp[m].pos = pos0 + (uint64_t)depth[n];
|
||||
pp[m].flags = r == 7 ? RAD_SP_GREEDY : 0;
|
||||
pt[m] = -9;
|
||||
ps_[m] = RadMtpCoupleState{};
|
||||
if (nkid[n] == 0) continue;
|
||||
int32_t qid[8];
|
||||
float qv[8];
|
||||
int have = 0;
|
||||
for (int c = 0; c < NC && have < 8; ++c)
|
||||
if (ids[(size_t)c] >= 0) qid[have++] = ids[(size_t)c];
|
||||
for (int c = have; c < 8; ++c) qid[c] = (int)(VOC - 100 + c);
|
||||
for (int c = 0; c < 8; ++c) {
|
||||
if (kind == 2) qid[c] = (int)(VOC - 1000 + 8 * n + c);
|
||||
qv[c] = (kind == 1 ? 7.0f - 0.4f * c : 8.0f - 0.8f * c) - (rnd() % 100) * 0.01f;
|
||||
}
|
||||
if (kind == 1) qid[1 + n % 3] = (int)(VOC - 500 + n);
|
||||
RadMtpCoupleState st{};
|
||||
REQUIRE(rad_mtp_couple_build_q(qid, qv, VOC, &st));
|
||||
st.draw = make_draw(seed, pos0, 7 + (uint64_t)r, 0, 1u);
|
||||
st.status = 1; st.round = depth[n] + 1; st.pos = pos0 + (uint64_t)depth[n];
|
||||
if (nkid[n] >= 2 && rheo) { rad_mtp_rrs_draw_rheo(&st, (uint64_t)n, nkid[n], rheo - 1); ++rheo_nodes; }
|
||||
else if (nkid[n] >= 2) rad_mtp_rrs_draw(&st, (uint64_t)n, nkid[n]);
|
||||
else { st.x = st.ids[rad_mtp_couple_pick(st, rad_spechub_uniform(seed, pos0, 77 + (uint64_t)n))]; st.nkids = 0; }
|
||||
const int nk_real = st.nkids >= 1 ? st.nkids : 1;
|
||||
for (int c = 0; c < nkid[n]; ++c) childtok[n][c] = c < nk_real ? (st.nkids >= 1 ? st.ids[c] : st.x) : (int)(VOC - 50 + c);
|
||||
if (r == 9 && n == 1) st.pos += 3; /* a stale state: a leaf of the walk */
|
||||
ps_[m] = st;
|
||||
}
|
||||
for (int n = 0; n < sh.nr; ++n) {
|
||||
const int m = row0 + n;
|
||||
ptopo[2 * m] = n ? row0 + sh.par[n] : -1;
|
||||
ptopo[2 * m + 1] = n ? childtok[sh.par[n]][seen[sh.par[n]]++] : 0;
|
||||
}
|
||||
row0 += sh.nr;
|
||||
}
|
||||
const std::vector<RadParam> pr = { P("M", M), P("n_cand", NC), P("n_vocab", (int)VOC) };
|
||||
Buf htok = tok, hst = sts;
|
||||
{
|
||||
RadTensor ts[6] = { cid.on_host(), cval.on_host(), prm.on_host(), hst.on_host(), htok.on_host(), topo.on_host() };
|
||||
RadArgs a{}; a.t = ts; a.n_t = 6; a.p = pr.data(); a.n_p = (int)pr.size();
|
||||
REQUIRE_EQ(htr->launch(&a, s), RAD_OK);
|
||||
}
|
||||
{
|
||||
RadTensor ts[6] = { cid.on_dev(s), cval.on_dev(s), prm.on_dev(s), sts.on_dev(s), tok.on_dev(s), topo.on_dev(s) };
|
||||
RadArgs a{}; a.t = ts; a.n_t = 6; a.p = pr.data(); a.n_p = (int)pr.size();
|
||||
REQUIRE_EQ(dtr->launch(&a, s), RAD_OK);
|
||||
tok.fetch(s);
|
||||
sts.fetch(s);
|
||||
REQUIRE_EQ(rad_stream_sync(s), RAD_OK);
|
||||
}
|
||||
row0 = 0;
|
||||
for (int r = 0; r < REPS; ++r) {
|
||||
const Shape& sh = shapes[r % 5];
|
||||
int wr = 0;
|
||||
for (int n = 0; n < sh.nr; ++n) {
|
||||
const int m = row0 + n;
|
||||
const int32_t g = ((int32_t*)tok.host.data())[m], w = ((int32_t*)htok.host.data())[m];
|
||||
CHECK_EQ(g, w);
|
||||
wr += w != -9;
|
||||
RadMtpCoupleState dg, hw;
|
||||
std::memcpy(&dg, sts.host.data() + (size_t)m * SW * 4, sizeof dg);
|
||||
std::memcpy(&hw, hst.host.data() + (size_t)m * SW * 4, sizeof hw);
|
||||
CHECK(std::memcmp(&dg, &hw, sizeof dg) == 0); /* the walk leaves the plane alone */
|
||||
}
|
||||
if (r == 7) CHECK_EQ(wr, 0); /* the greedy request keeps every pick */
|
||||
written += wr;
|
||||
paths3 += wr >= 3;
|
||||
finals += wr >= 1;
|
||||
row0 += sh.nr;
|
||||
}
|
||||
}
|
||||
fprintf(stderr, " mixed requests: %ld tokens written, %ld requests decided, %ld with a path of 2+ drafts, %ld rheo nodes\n",
|
||||
written, finals, paths3, rheo_nodes);
|
||||
CHECK(written >= 100);
|
||||
CHECK(paths3 >= 10);
|
||||
CHECK(rheo_nodes >= 10);
|
||||
rad_stream_destroy(s);
|
||||
}
|
||||
|
||||
/* Microbenchmark of the block verify op (set RAD_BLOCK_BENCH=1; RAD_BENCH_LIB=<spechub.so> to time
|
||||
* another build of the plugin). Wall time per launch over back-to-back launches of one block (g
|
||||
* drafts plus the bonus row, one sequence as at max-num-seqs 1), minus the same launch over rows the
|
||||
* op ignores (the launch floor). Planes of 1024 slots with `live` live candidates: 20 (top_k 20)
|
||||
* or 1024 (top_k 0). Eight different blocks are cycled. */
|
||||
TEST(block_verify_microbenchmark) {
|
||||
if (!getenv("RAD_BLOCK_BENCH")) return;
|
||||
RadDeviceProps dp{};
|
||||
if (rad_dev_count() < 1 || rad_dev_props(0, &dp) != RAD_OK || dp.is_host_backend) return;
|
||||
Plugin pl;
|
||||
const char* lib = getenv("RAD_BENCH_LIB") ? getenv("RAD_BENCH_LIB") : RAD_SPECHUB_LIB;
|
||||
REQUIRE(pl.open(lib));
|
||||
const RadKernelInfo* dver = pl.row("spechub_mtp_couple_verify");
|
||||
REQUIRE(dver);
|
||||
RadStream s{};
|
||||
REQUIRE_EQ(rad_stream_create(&s, 0), RAD_OK);
|
||||
const int SW = (int)RAD_MTP_COUPLE_STATE_WORDS;
|
||||
const int64_t VOC = 150000;
|
||||
const int NC = 1024, NV = 8;
|
||||
const int nrep = getenv("RAD_BENCH_REPS") ? atoi(getenv("RAD_BENCH_REPS")) : 400;
|
||||
const int divide = getenv("RAD_BENCH_DIV") ? atoi(getenv("RAD_BENCH_DIV")) : 1; /* the plugin's in-kernel repeat count */
|
||||
for (int live : { 20, 1024 }) for (int g : { 3, 5, 7 }) {
|
||||
const int M = g + 1;
|
||||
struct Var { Buf cid, cval, prm, sts, tok; Var(int M, int NC, int SW) :
|
||||
cid(RAD_I32, { M, NC }, 4), cval(RAD_F32, { M, NC }, 4), prm(RAD_U32, { M, 24 }, 4),
|
||||
sts(RAD_U32, { M, SW }, 4), tok(RAD_I32, { M }, 4) {} };
|
||||
std::vector<Var*> vars;
|
||||
for (int v = 0; v < NV; ++v) {
|
||||
Var* x = new Var(M, NC, SW);
|
||||
vars.push_back(x);
|
||||
int32_t* pi = (int32_t*)x->cid.host.data();
|
||||
float* pv = (float*)x->cval.host.data();
|
||||
RadSampleParams* pp = (RadSampleParams*)x->prm.host.data();
|
||||
RadMtpCoupleState* ps_ = (RadMtpCoupleState*)x->sts.host.data();
|
||||
for (int m = 0; m < M; ++m) for (int c = 0; c < NC; ++c) { pi[m * NC + c] = -1; pv[m * NC + c] = -INFINITY; }
|
||||
const uint64_t seed = 0xbe00ull + (uint64_t)v, pos0 = 100;
|
||||
for (int i = 0; i < M; ++i) {
|
||||
int ids[1024];
|
||||
for (int c = 0; c < live; ++c) {
|
||||
bool dup;
|
||||
do { ids[c] = (int)(rnd() % VOC); dup = false;
|
||||
for (int k = 0; k < c; ++k) dup = dup || ids[k] == ids[c]; } while (dup);
|
||||
pi[i * NC + c] = ids[c];
|
||||
pv[i * NC + c] = 12.0f - 0.9f * (float)c * (20.0f / (float)live) - (rnd() % 100) * 0.01f;
|
||||
}
|
||||
pp[i].seed = seed; pp[i].pos = pos0 + (uint64_t)i;
|
||||
if (i == g) continue;
|
||||
int32_t qid[8]; float qv[8];
|
||||
for (int c = 0; c < 8; ++c) { qid[c] = ids[c]; qv[c] = 11.0f - 0.9f * c - (rnd() % 100) * 0.01f; }
|
||||
RadMtpCoupleState st{};
|
||||
REQUIRE(rad_mtp_couple_build_q(qid, qv, VOC, &st));
|
||||
st.draw = make_draw(seed, pos0, 7 + (uint64_t)v, 0, 1u);
|
||||
st.status = 1; st.round = i + 1; st.pos = pos0 + (uint64_t)i; st.blk = g;
|
||||
st.x = st.ids[rnd() % 3u];
|
||||
ps_[i] = st;
|
||||
}
|
||||
}
|
||||
const std::vector<RadParam> pr = { P("M", M), P("n_cand", NC), P("n_vocab", (int)VOC) };
|
||||
std::vector<RadTensor> ts[NV];
|
||||
std::vector<RadTensor> tn[NV];
|
||||
for (int v = 0; v < NV; ++v) {
|
||||
Var* x = vars[(size_t)v];
|
||||
ts[v] = { x->cid.on_dev(s), x->cval.on_dev(s), x->prm.on_dev(s), x->sts.on_dev(s), x->tok.on_dev(s) };
|
||||
}
|
||||
/* the floor: the same launch with every state inert (status 0) */
|
||||
std::vector<Var*> inert;
|
||||
for (int v = 0; v < NV; ++v) {
|
||||
Var* x = new Var(M, NC, SW);
|
||||
memcpy(x->cid.host.data(), vars[(size_t)v]->cid.host.data(), x->cid.host.size());
|
||||
memcpy(x->cval.host.data(), vars[(size_t)v]->cval.host.data(), x->cval.host.size());
|
||||
memcpy(x->prm.host.data(), vars[(size_t)v]->prm.host.data(), x->prm.host.size());
|
||||
inert.push_back(x);
|
||||
tn[v] = { x->cid.on_dev(s), x->cval.on_dev(s), x->prm.on_dev(s), x->sts.on_dev(s), x->tok.on_dev(s) };
|
||||
}
|
||||
auto run = [&](std::vector<RadTensor>* t, int reps) {
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
for (int r = 0; r < reps; ++r) {
|
||||
RadArgs a{}; a.t = t[r % NV].data(); a.n_t = 5; a.p = pr.data(); a.n_p = (int)pr.size();
|
||||
if (dver->launch(&a, s) != RAD_OK) return -1.0;
|
||||
}
|
||||
rad_stream_sync(s);
|
||||
return std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count() / reps;
|
||||
};
|
||||
run(ts, 50); run(tn, 50);
|
||||
double best = 1e9, floor_ = 1e9;
|
||||
for (int rep = 0; rep < 5; ++rep) { best = std::min(best, run(ts, nrep)); floor_ = std::min(floor_, run(tn, nrep)); }
|
||||
fprintf(stderr, " BENCH live=%4d g=%d block %.1f us/call floor %.1f us net per block decision %.1f us\n", live, g, best, floor_, (best - floor_) / divide);
|
||||
for (auto* x : vars) delete x;
|
||||
for (auto* x : inert) delete x;
|
||||
}
|
||||
rad_stream_destroy(s);
|
||||
}
|
||||
|
||||
/* Microbenchmark of the traversal verify op (RAD_TRAV_BENCH=1). One request a launch (max-num-seqs 1):
|
||||
* a chain of 3 drafts and trees of 4, 6 and 10 nodes, on the sampler's 1024-slot plane with 20 live
|
||||
* candidates (top_k 20) or all of them (top_k 0); three kinds of data: `acc` (Q close to P: the walk
|
||||
* accepts its first leaf most of the time), `mix` (a flatter Q with one token the target lacks) and `rej`
|
||||
* (Q's support is off the target's: every node is visited and rejected, and the final token is an
|
||||
* off-support draw at the root -- the longest walk a tree has). Eight requests of a kind are cycled.
|
||||
*
|
||||
* RAD_BENCH_LIB is the plugin timed; RAD_BENCH_LIB_REP the same sources built with
|
||||
* -DRAD_TRAV_REPEAT=RAD_BENCH_REP, whose kernel decides the request that many times in one launch:
|
||||
* (t_rep - t_one) / (REP - 1) is the kernel's time per decision with the launch, the queue and the sync
|
||||
* taken out (the AQL backend reports no kernel times). Tokens of the timed plugin are checked against
|
||||
* the host row first. */
|
||||
namespace {
|
||||
struct TravShape { const char* name; int nr; int par[16]; };
|
||||
const TravShape kTravShapes[] = {
|
||||
{ "chain3", 4, { -1, 0, 1, 2 } },
|
||||
{ "tree4", 5, { -1, 0, 1, 2, 0 } }, /* chain 3 + a sibling of its first node */
|
||||
{ "tree6", 7, { -1, 0, 1, 2, 0, 1, 2 } }, /* chain 3 + a sibling at every depth */
|
||||
{ "tree10", 11, { -1, 0, 0, 1, 1, 2, 3, 3, 5, 6, 6 } }, /* a width-2 grower's tree */
|
||||
};
|
||||
struct TravReq {
|
||||
Buf cid, cval, prm, sts, tok, topo;
|
||||
TravReq(int M, int NC, int SW)
|
||||
: cid(RAD_I32, { M, NC }, 4), cval(RAD_F32, { M, NC }, 4), prm(RAD_U32, { M, 24 }, 4),
|
||||
sts(RAD_U32, { M, SW }, 4), tok(RAD_I32, { M }, 4), topo(RAD_I32, { M, 2 }, 4) {}
|
||||
};
|
||||
/* kind 0 acc, 1 mix, 2 rej, 3 inert (no bound state: the op's early out) */
|
||||
void fill_trav_req(TravReq* x, const TravShape& sh, int NC, int live, int kind, uint64_t seed, int64_t VOC,
|
||||
int deep = -1) {
|
||||
const int NR = sh.nr;
|
||||
int32_t* pi = (int32_t*)x->cid.host.data();
|
||||
float* pv = (float*)x->cval.host.data();
|
||||
RadSampleParams* pp = (RadSampleParams*)x->prm.host.data();
|
||||
RadMtpCoupleState* ps_ = (RadMtpCoupleState*)x->sts.host.data();
|
||||
int32_t* pt = (int32_t*)x->tok.host.data();
|
||||
int32_t* ptopo = (int32_t*)x->topo.host.data();
|
||||
int depth[16] = {}, nkid[16] = {}, seen[16] = {};
|
||||
int32_t childtok[16][8] = {};
|
||||
for (int n = 1; n < NR; ++n) ++nkid[sh.par[n]];
|
||||
const uint64_t pos0 = 100;
|
||||
for (int n = 0; n < NR; ++n) {
|
||||
depth[n] = n ? depth[sh.par[n]] + 1 : 0;
|
||||
int ids[1024];
|
||||
for (int c = 0; c < NC; ++c) { pi[n * NC + c] = -1; pv[n * NC + c] = -INFINITY; }
|
||||
for (int c = 0; c < live; ++c) {
|
||||
bool dup;
|
||||
do { ids[c] = (int)(rnd() % (uint32_t)(VOC - 1000)); dup = false;
|
||||
for (int k = 0; k < c; ++k) dup = dup || ids[k] == ids[c]; } while (dup);
|
||||
pi[n * NC + c] = ids[c];
|
||||
/* a peaked head of 20 and, at top_k 0, a long flat tail carrying about 6 % of the mass */
|
||||
pv[n * NC + c] = c < 20 ? 12.0f - 0.9f * (float)c - (rnd() % 100) * 0.01f
|
||||
: 3.0f - 0.004f * (float)(c - 20) - (rnd() % 100) * 0.01f;
|
||||
/* `deep`: the row's mass in one candidate of pass `deep` (the final draw's scan, by depth);
|
||||
* RAD_TRAV_BENCH_DEEP=2 gives every other candidate a small mass of its own (2e-9 of it), so
|
||||
* that no pass of the scan is one of zero masses */
|
||||
if (deep >= 0)
|
||||
pv[n * NC + c] = c == 32 * deep + 5 ? 12.0f : (atoi(getenv("RAD_TRAV_BENCH_DEEP")) == 2 ? -8.0f : -30.0f);
|
||||
}
|
||||
pp[n].seed = seed; pp[n].pos = pos0 + (uint64_t)depth[n]; pp[n].flags = 0;
|
||||
pt[n] = -9;
|
||||
ps_[n] = RadMtpCoupleState{};
|
||||
if (nkid[n] == 0 || kind == 3) continue;
|
||||
int32_t qid[8]; float qv[8];
|
||||
for (int c = 0; c < 8; ++c) {
|
||||
qid[c] = kind == 2 ? (int)(VOC - 1000 + 8 * n + c) : ids[c];
|
||||
qv[c] = kind == 1 ? 11.0f - 0.5f * c - (rnd() % 100) * 0.01f : 11.0f - 0.9f * c - (rnd() % 100) * 0.01f;
|
||||
}
|
||||
if (kind == 1) qid[1 + n % 3] = (int)(VOC - 500 + n); /* a draft token the target lacks */
|
||||
RadMtpCoupleState st{};
|
||||
REQUIRE(rad_mtp_couple_build_q(qid, qv, VOC, &st));
|
||||
st.draw = make_draw(seed, pos0, 7, 0, 1u);
|
||||
st.status = 1; st.round = depth[n] + 1; st.pos = pos0 + (uint64_t)depth[n];
|
||||
if (nkid[n] >= 2) rad_mtp_rrs_draw(&st, (uint64_t)n, nkid[n]);
|
||||
else { st.x = st.ids[rad_mtp_couple_pick(st, rad_spechub_uniform(seed, pos0, 77 + (uint64_t)n))]; st.nkids = 0; }
|
||||
for (int c = 0; c < nkid[n]; ++c) childtok[n][c] = nkid[n] >= 2 ? st.ids[c] : st.x;
|
||||
ps_[n] = st;
|
||||
}
|
||||
for (int n = 0; n < NR; ++n) {
|
||||
ptopo[2 * n] = n ? sh.par[n] : -1;
|
||||
ptopo[2 * n + 1] = n ? childtok[sh.par[n]][seen[sh.par[n]]++] : 0;
|
||||
}
|
||||
}
|
||||
} /* namespace */
|
||||
|
||||
TEST(trav_verify_microbenchmark) {
|
||||
if (!getenv("RAD_TRAV_BENCH")) return;
|
||||
RadDeviceProps dp{};
|
||||
if (rad_dev_count() < 1 || rad_dev_props(0, &dp) != RAD_OK || dp.is_host_backend) return;
|
||||
Plugin pl, plr;
|
||||
const char* lib = getenv("RAD_BENCH_LIB") ? getenv("RAD_BENCH_LIB") : RAD_SPECHUB_LIB;
|
||||
REQUIRE(pl.open(lib));
|
||||
const RadKernelInfo* dtr = pl.row("spechub_mtp_trav_verify");
|
||||
const RadKernelInfo* htr = pl.row("ref_mtp_trav_verify");
|
||||
REQUIRE(dtr && htr);
|
||||
const RadKernelInfo* drep = nullptr;
|
||||
const int rep = getenv("RAD_BENCH_REP") ? atoi(getenv("RAD_BENCH_REP")) : 0;
|
||||
if (getenv("RAD_BENCH_LIB_REP") && rep > 1) {
|
||||
REQUIRE(plr.open(getenv("RAD_BENCH_LIB_REP")));
|
||||
drep = plr.row("spechub_mtp_trav_verify");
|
||||
REQUIRE(drep);
|
||||
}
|
||||
RadStream s{};
|
||||
REQUIRE_EQ(rad_stream_create(&s, 0), RAD_OK);
|
||||
const int SW = (int)RAD_MTP_COUPLE_STATE_WORDS;
|
||||
const int64_t VOC = 150000;
|
||||
const int NC = 1024, NV = 8;
|
||||
const int nrep = getenv("RAD_BENCH_REPS") ? atoi(getenv("RAD_BENCH_REPS")) : 200;
|
||||
const char* kinds[] = { "acc", "mix", "rej", "inert" };
|
||||
/* RAD_TRAV_BENCH_DEEP=1: instead, the chain's final draw off the support with the drawn candidate in
|
||||
* pass 0, 7, 8, .. 31 of a 1024-slot plane: what the scan for it costs by depth. */
|
||||
const bool deep_series = getenv("RAD_TRAV_BENCH_DEEP") != nullptr;
|
||||
const int deeps[] = { 0, 3, 7, 8, 15, 16, 23, 24, 31 };
|
||||
for (int live : { 20, 1024 }) for (const TravShape& sh : kTravShapes) for (int kind = 0; kind < 4; ++kind)
|
||||
for (int di = 0; di < (deep_series ? 9 : 1); ++di) {
|
||||
const int deep = deep_series ? deeps[di] : -1;
|
||||
if (deep_series && (live != 1024 || kind != 2 || &sh != &kTravShapes[0])) continue;
|
||||
const int M = sh.nr;
|
||||
std::vector<TravReq*> vars;
|
||||
std::vector<RadTensor> ts[NV];
|
||||
const std::vector<RadParam> pr = { P("M", M), P("n_cand", NC), P("n_vocab", (int)VOC) };
|
||||
int path_rows = 0, mism = 0;
|
||||
for (int v = 0; v < NV; ++v) {
|
||||
TravReq* x = new TravReq(M, NC, SW);
|
||||
vars.push_back(x);
|
||||
fill_trav_req(x, sh, NC, live, kind, 0x7a00ull + 131ull * (uint64_t)v + (uint64_t)live, VOC, deep);
|
||||
/* the host row, on copies */
|
||||
Buf htok = x->tok, hst = x->sts;
|
||||
RadTensor th[6] = { x->cid.on_host(), x->cval.on_host(), x->prm.on_host(), hst.on_host(), htok.on_host(), x->topo.on_host() };
|
||||
RadArgs ah{}; ah.t = th; ah.n_t = 6; ah.p = pr.data(); ah.n_p = (int)pr.size();
|
||||
REQUIRE_EQ(htr->launch(&ah, s), RAD_OK);
|
||||
ts[v] = { x->cid.on_dev(s), x->cval.on_dev(s), x->prm.on_dev(s), x->sts.on_dev(s), x->tok.on_dev(s), x->topo.on_dev(s) };
|
||||
RadArgs a{}; a.t = ts[v].data(); a.n_t = 6; a.p = pr.data(); a.n_p = (int)pr.size();
|
||||
REQUIRE_EQ(dtr->launch(&a, s), RAD_OK);
|
||||
x->tok.fetch(s);
|
||||
REQUIRE_EQ(rad_stream_sync(s), RAD_OK);
|
||||
for (int m = 0; m < M; ++m) {
|
||||
const int32_t g = ((int32_t*)x->tok.host.data())[m], w = ((int32_t*)htok.host.data())[m];
|
||||
mism += g != w;
|
||||
path_rows += w != -9;
|
||||
}
|
||||
}
|
||||
CHECK_EQ(mism, 0);
|
||||
auto run = [&](const RadKernelInfo* k, int reps) {
|
||||
const auto t0 = std::chrono::steady_clock::now();
|
||||
for (int r = 0; r < reps; ++r) {
|
||||
RadArgs a{}; a.t = ts[r % NV].data(); a.n_t = 6; a.p = pr.data(); a.n_p = (int)pr.size();
|
||||
if (k->launch(&a, s) != RAD_OK) return -1.0;
|
||||
}
|
||||
rad_stream_sync(s);
|
||||
return std::chrono::duration<double, std::micro>(std::chrono::steady_clock::now() - t0).count() / reps;
|
||||
};
|
||||
run(dtr, 40);
|
||||
if (drep) run(drep, 10);
|
||||
double one = 1e9, many = 1e9;
|
||||
for (int it = 0; it < 5; ++it) {
|
||||
one = std::min(one, run(dtr, nrep));
|
||||
if (drep) many = std::min(many, run(drep, std::max(8, nrep / 4)));
|
||||
}
|
||||
if (deep_series) fprintf(stderr, " TRAVBENCH final draw in pass %2d:", deep);
|
||||
if (drep)
|
||||
fprintf(stderr, " TRAVBENCH live=%4d %-6s %-5s rows=%2d launch %.1f us x%d %.1f us kernel per decision %.2f us (path rows/req %.2f, mism %d)\n",
|
||||
live, sh.name, kinds[kind], M, one, rep, many, (many - one) / (rep - 1), (double)path_rows / NV, mism);
|
||||
else
|
||||
fprintf(stderr, " TRAVBENCH live=%4d %-6s %-5s rows=%2d launch %.1f us (path rows/req %.2f, mism %d)\n",
|
||||
live, sh.name, kinds[kind], M, one, (double)path_rows / NV, mism);
|
||||
for (auto* x : vars) delete x;
|
||||
}
|
||||
rad_stream_destroy(s);
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
@@ -94,11 +94,11 @@ TEST(spechub_wiring_the_device_rows_are_production_and_the_oracle_rows_are_refer
|
||||
}
|
||||
CHECK_EQ(dev + host, p.count());
|
||||
#ifdef RAD_SPECHUB_WITH_DEVICE
|
||||
CHECK_EQ(dev, 3);
|
||||
CHECK_EQ(host, 3);
|
||||
CHECK_EQ(dev, 4);
|
||||
CHECK_EQ(host, 4);
|
||||
#else
|
||||
CHECK_EQ(dev, 0);
|
||||
CHECK_EQ(host, 3);
|
||||
CHECK_EQ(host, 4);
|
||||
#endif
|
||||
|
||||
/* Every op must have at least one production row on a device build. This is the property the
|
||||
|
||||
Reference in new issue
Block a user