Archived
feat(sampler): port linear MTP coupling and cooperative verify upstream
Port GitHub PRs #12 and #13 onto Codeberg upstream main, retaining upstream kernel-chain handling. Include the source Clang target-attribute fixes required by the host build; omit the KFD header workaround and unrelated fork work. Co-Authored-By: Claude Code <noreply@anthropic.com> Co-authored-by: multica-agent <github@multica.ai>
This commit is contained in:
1 parent
123c439684
commit
d912c694f3
37 files changed
+3730
-23
No files matched your search
@@ -240,6 +240,22 @@ if(RAD_HIP_FOUND)
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
# spechub_wiring_test dlopens the built spechub plugin and checks how its rows are classified: the
|
||||
# device rows must be production kernels and the host oracle rows must stay reference. It needs the
|
||||
# library path and the same RAD_SPECHUB_WITH_DEVICE answer the plugin was built with.
|
||||
if(TARGET spechub)
|
||||
target_link_libraries(spechub_wiring_test PRIVATE ${CMAKE_DL_LIBS})
|
||||
target_compile_definitions(spechub_wiring_test PRIVATE
|
||||
RAD_SPECHUB_LIB="${RAD_PLUGIN_OUT}/kernels/spechub.so")
|
||||
if(RAD_HIP_FOUND)
|
||||
target_compile_definitions(spechub_wiring_test PRIVATE RAD_SPECHUB_WITH_DEVICE=1)
|
||||
endif()
|
||||
add_dependencies(spechub_wiring_test spechub)
|
||||
# sample_test's coupling case declares a Sampler with RADIANCE_MTP_COUPLED and needs the verify
|
||||
# op the spechub plugin carries (its host row); it skips when the library is absent.
|
||||
add_dependencies(sample_test spechub)
|
||||
endif()
|
||||
|
||||
# ================================================================== whole models on the CPU
|
||||
#
|
||||
# engine_e2e_test runs the radiance binary on tiny synthetic checkpoints (tests/tiny_model.h) on
|
||||
@@ -498,6 +514,9 @@ endif()
|
||||
# parameters). Cheap, so in the fast tier. libr4d.so is added when built; the script reports
|
||||
# " SKIP " rather than passing when a plugin will not dump.
|
||||
set(RAD_OPSDOC_PLUGINS ${RAD_PLUGIN_OUT}/kernels/libref.so)
|
||||
if(TARGET spechub)
|
||||
list(APPEND RAD_OPSDOC_PLUGINS ${RAD_PLUGIN_OUT}/kernels/spechub.so)
|
||||
endif()
|
||||
if(RAD_HIP_FOUND AND TARGET libr4d)
|
||||
list(APPEND RAD_OPSDOC_PLUGINS ${RAD_PLUGIN_OUT}/kernels/libr4d.so)
|
||||
endif()
|
||||
|
||||
@@ -727,6 +727,74 @@ TEST(a_checkpoint_store_the_host_cannot_hold_is_refused_at_startup) {
|
||||
CHECK(named);
|
||||
}
|
||||
|
||||
/* RADIANCE_MTP_COUPLED is the coupling's only switch, and these are the engine's answers to it
|
||||
* before any request: a value that is not 0/1 refuses the start (never a silent uncoupled run),
|
||||
* and so does a model whose drafter has no per-round draw to couple. Both are checked through the
|
||||
* real binary because both are bringup paths, not helper functions; the drafter shape is the
|
||||
* declaration the engine has in hand, which no environment variable describes. The same model with
|
||||
* the flag off is the control: it starts, which is what makes the two refusals attributable to the
|
||||
* switch and not to the fixture. */
|
||||
TEST(the_coupling_switch_refuses_what_it_cannot_serve_at_startup) {
|
||||
World& w = world();
|
||||
REQUIRE(w.ok);
|
||||
/* qwen35's tiny checkpoint declares no draft head: no serial chain to couple. The spechub
|
||||
* kernels are loaded beside libref, so the refusal is the drafter's shape and not a missing
|
||||
* coupling kernel. */
|
||||
const Ran on = radiance(w.qwen35, { "--kernels", "libref,spechub" },
|
||||
{ "RADIANCE_MTP_COUPLED=1" }, 120);
|
||||
const std::string want = "RADIANCE_MTP_COUPLED requires the linear serial draft chain "
|
||||
"(RAD_DRAFT_SERIAL with depth > 0)";
|
||||
if (on.status == 0 || on.status == 124 || !has(on.log, want))
|
||||
fprintf(stderr, " RADIANCE_MTP_COUPLED=1 exited %d:\n%s\n", on.status,
|
||||
tail(on.log, 20).c_str());
|
||||
CHECK(on.status != 0);
|
||||
CHECK(on.status != 124);
|
||||
CHECK(has(on.log, want));
|
||||
CHECK(has(on.log, "declares drafter '(none)'"));
|
||||
|
||||
/* THE GATE IS NOT RELAXED EITHER WAY: the same start as a deployment runs it (no
|
||||
* --debug-accept-reference-kernels), with a production library for the model's ops and the
|
||||
* spechub library beside it. On this card-less machine the plugin's device rows are left out
|
||||
* of selection (no card is visible), so the only coupled row left is the host oracle -- and
|
||||
* the gate must still refuse it, naming the coupled op. Marking the library, or the oracle
|
||||
* rows, production would pass this start on a machine that cannot run the coupling at all. */
|
||||
if (::access(RAD_E2E_HOME "/kernels/libavx.so", R_OK) == 0) {
|
||||
std::string gate_log;
|
||||
const int gate = tiny::run(serving_argv(w.qwen35, { "--kernels", "libavx,spechub" }),
|
||||
engine_env({ "RADIANCE_MTP_COUPLED=1" }), &gate_log, 120);
|
||||
const std::string gate_ref = "1 declared op(s) resolve only to a reference implementation "
|
||||
"(spechub)";
|
||||
if (gate == 0 || gate == 124 || !has(gate_log, gate_ref) ||
|
||||
!has(gate_log, "1 distinct op(s): mtp_couple_verify"))
|
||||
fprintf(stderr, " serving start exited %d:\n%s\n", gate,
|
||||
tail(gate_log, 20).c_str());
|
||||
CHECK(gate != 0);
|
||||
CHECK(gate != 124);
|
||||
CHECK(has(gate_log, gate_ref));
|
||||
CHECK(has(gate_log, "distinct op(s): mtp_couple_verify"));
|
||||
}
|
||||
|
||||
/* A typo is refused, not read as off. */
|
||||
const Ran typo = radiance(w.qwen35, {}, { "RADIANCE_MTP_COUPLED=yes" }, 120);
|
||||
const std::string refused = "RADIANCE_MTP_COUPLED accepts 0|1 (or unset)";
|
||||
if (typo.status == 0 || typo.status == 124 || !has(typo.log, refused))
|
||||
fprintf(stderr, " RADIANCE_MTP_COUPLED=yes exited %d:\n%s\n", typo.status,
|
||||
tail(typo.log, 20).c_str());
|
||||
CHECK(typo.status != 0);
|
||||
CHECK(typo.status != 124);
|
||||
CHECK(has(typo.log, refused));
|
||||
|
||||
/* The control on the same checkpoint: flag off starts and declares, so both refusals above are
|
||||
* the switch's answer and not the fixture's. --debug-graph runs declare and plan and exits, so
|
||||
* it is a start, not a serve. */
|
||||
const Ran off = radiance(w.qwen35, { "--debug-graph" }, { "RADIANCE_MTP_COUPLED=0" }, 120);
|
||||
if (off.status != 0)
|
||||
fprintf(stderr, " RADIANCE_MTP_COUPLED=0 exited %d:\n%s\n", off.status,
|
||||
tail(off.log, 20).c_str());
|
||||
CHECK_EQ(off.status, 0);
|
||||
CHECK(!has(off.log, "MTP coupled LIVE"));
|
||||
}
|
||||
|
||||
/* Two ranks split heads, the FFN and the vocabulary, and sum partial products in an all-reduce,
|
||||
* so the rounding differs from one rank's; anything more than rounding is a wrong column or row
|
||||
* (a rank's logits read at another rank's vocabulary offset score near chance). */
|
||||
|
||||
@@ -0,0 +1,339 @@
|
||||
/* mtp_couple_plan_test.cpp -- the draw the engine stages for a serial drafting row, resolved
|
||||
* against a step plan (core/sched/mtp_couple_plan.h, called by Engine::stage_couple_draws).
|
||||
*
|
||||
* The coupling failed twice before its first draft, and both failures were here rather than in the
|
||||
* ABI:
|
||||
*
|
||||
* - The draw plane was sized from nothing, so every row lookup missed and the first step that
|
||||
* drafted refused with "has no plan entry on its card" -- the engine started and then could
|
||||
* not take one coupled step. The plane is now sized from cfg_.max_seqs, and the rule refuses a
|
||||
* plane that is too small instead of silently missing.
|
||||
* - A row whose card carries another sequence's draw is stale: the plugin refuses the staged
|
||||
* draw, so the row is refused here, named, before any pass reads it.
|
||||
*
|
||||
* The plan is the real StepPlan the scheduler produces; only its entries are built by hand, since
|
||||
* the rule reads nothing else off it.
|
||||
*/
|
||||
#include "rad_test.h"
|
||||
|
||||
#include "sched/mtp_couple_bind.h"
|
||||
#include "sched/mtp_couple_plan.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
using namespace rad;
|
||||
|
||||
namespace {
|
||||
|
||||
/* One plan entry for a sequence on a card, with the pre-commit output size the draw coordinates
|
||||
* on. `greedy` is the request's own choice (top_k 1). */
|
||||
StepEntry entry(Request* r, uint64_t seq, int32_t card, size_t output, bool greedy) {
|
||||
r->sp = SamplingParams{};
|
||||
r->sp.top_k = greedy ? 1 : 0;
|
||||
r->sp.temp = 1.0f;
|
||||
r->output.assign(output, 0);
|
||||
StepEntry e;
|
||||
e.req = r;
|
||||
e.seq = seq;
|
||||
e.card = card;
|
||||
return e;
|
||||
}
|
||||
|
||||
Scheduler::SerialRow row(uint64_t seq, int32_t card) {
|
||||
Scheduler::SerialRow r;
|
||||
r.seq = seq;
|
||||
r.card = card;
|
||||
return r;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* The staged draw names the plan entry on the row's card: sequence, card, the PRE-commit output
|
||||
* size as the coordinate, the request's seed and whether it samples. Cards the plan does not use
|
||||
* stay zero, so a row landing on one of them is refused rather than served a stale draw. */
|
||||
TEST(the_staged_draw_names_the_plan_entry_on_the_rows_card) {
|
||||
Request a, b, c;
|
||||
StepPlan plan;
|
||||
plan.e.push_back(entry(&a, 11, 3, 7, false));
|
||||
plan.e.push_back(entry(&b, 22, 5, 9, true));
|
||||
const Scheduler::SerialRow rows[] = { row(11, 3), row(22, 5) };
|
||||
|
||||
std::vector<RadMtpCoupleDraw> by_card(8);
|
||||
RadMtpCoupleDraw stage[2];
|
||||
std::string why;
|
||||
REQUIRE_EQ(mtp_couple_plan_stage(plan, 8, rows, 2, &by_card, stage, &why), RAD_OK);
|
||||
|
||||
CHECK_EQ(stage[0].seq, (uint64_t)11);
|
||||
CHECK_EQ(stage[0].card, 3);
|
||||
CHECK_EQ(stage[0].base, (uint64_t)7);
|
||||
CHECK_EQ(stage[0].seed, a.sp.seed);
|
||||
CHECK_EQ(stage[0].mode, 1u);
|
||||
/* A greedy request keeps the head's proposal instead of sampling x~Q. */
|
||||
CHECK_EQ(stage[1].seq, (uint64_t)22);
|
||||
CHECK_EQ(stage[1].card, 5);
|
||||
CHECK_EQ(stage[1].mode, 0u);
|
||||
/* Card 0 carried no entry: zeroed, so its seq names nothing a row could match. */
|
||||
CHECK_EQ(by_card[0].seq, (uint64_t)0);
|
||||
CHECK_EQ(by_card[7].seq, (uint64_t)0);
|
||||
(void)c;
|
||||
}
|
||||
|
||||
/* THE FIRST-DRAFT REGRESSION: a plane sized for fewer slots than the step can use would make every
|
||||
* row miss. It is refused by name -- never a silent miss, and never a start that cannot draft. */
|
||||
TEST(a_draw_plane_sized_for_fewer_slots_than_the_step_uses_is_refused) {
|
||||
Request a;
|
||||
StepPlan plan;
|
||||
plan.e.push_back(entry(&a, 11, 30, 7, false));
|
||||
const Scheduler::SerialRow rows[] = { row(11, 30) };
|
||||
|
||||
std::vector<RadMtpCoupleDraw> empty;
|
||||
RadMtpCoupleDraw stage[1];
|
||||
std::string why;
|
||||
const int st = mtp_couple_plan_stage(plan, 32, rows, 1, &empty, stage, &why);
|
||||
if (st == RAD_OK || why.find("holds 0 card slot(s)") == std::string::npos)
|
||||
fprintf(stderr, " status %d, why '%s'\n", st, why.c_str());
|
||||
CHECK_EQ(st, RAD_E_STATE);
|
||||
CHECK(why.find("holds 0 card slot(s), the step can use 32") != std::string::npos);
|
||||
}
|
||||
|
||||
/* A drafting row whose card names another sequence is stale: the staged draw would name a card the
|
||||
* advance writes for someone else, which the plugin refuses. Refused here, naming the row. */
|
||||
TEST(a_row_whose_card_names_another_sequence_is_refused) {
|
||||
Request a;
|
||||
StepPlan plan;
|
||||
plan.e.push_back(entry(&a, 11, 3, 7, false));
|
||||
const Scheduler::SerialRow rows[] = { row(11, 3), row(33, 3) };
|
||||
|
||||
std::vector<RadMtpCoupleDraw> by_card(8);
|
||||
RadMtpCoupleDraw stage[2];
|
||||
std::string why;
|
||||
const int st = mtp_couple_plan_stage(plan, 8, rows, 2, &by_card, stage, &why);
|
||||
if (st == RAD_OK || why.find("drafting row 1 (sequence 33, card 3)") == std::string::npos)
|
||||
fprintf(stderr, " status %d, why '%s'\n", st, why.c_str());
|
||||
CHECK_EQ(st, RAD_E_STATE);
|
||||
CHECK(why.find("drafting row 1 (sequence 33, card 3) has no plan entry on its card") !=
|
||||
std::string::npos);
|
||||
}
|
||||
|
||||
/* ================================================== THE VERIFY POSITION CONTRACT
|
||||
*
|
||||
* The sampler stages row k of a request at `n_out + k`, `n_out` being that request's committed
|
||||
* output length when the step is built. Round rd is verified at row k = rd - 1, so the state bound
|
||||
* after the drafting step must carry `n_out + (rd - 1)` for the `n_out` the NEXT step will see.
|
||||
*
|
||||
* These tests walk successive steps through the REAL staging function, the REAL bind rule
|
||||
* (core/sched/mtp_couple_bind.h) and the REAL row check (rad_mtp_couple_binds), with the request's
|
||||
* output growing the way a commit grows it.
|
||||
*
|
||||
* THE ENGINE USED TO REBUILD the coordinate as `pre-commit base + slot n_acc`. For a commit that
|
||||
* emitted exactly what the card accepted -- every commit of a request that is still running -- that
|
||||
* is the same number. It differs only when the commit emits fewer, and such a commit closes the
|
||||
* request (a max_tokens or context cut): the row is then terminal and will never sample again. Work
|
||||
* bound for it is carried terminal work, and a coupled verify there reads as a live round in any
|
||||
* log without being one. So the bind skips a request that is not Running, and the old formula's
|
||||
* one-ahead position for it is checked not to satisfy rad_mtp_couple_binds. */
|
||||
|
||||
namespace {
|
||||
|
||||
/* A sampled state as the propose kernel would leave it for `draw`, at round `rd` and position
|
||||
* `pos`: one Q token, x drawn from it, status 1. */
|
||||
RadMtpCoupleState sampled(const RadMtpCoupleDraw& draw, int rd, uint64_t pos) {
|
||||
RadMtpCoupleState s{};
|
||||
s.draw = draw;
|
||||
s.round = rd;
|
||||
s.status = 1;
|
||||
s.count = 1;
|
||||
s.x = 100 + rd;
|
||||
for (int i = 0; i < 8; ++i) s.ids[i] = -1; /* the proposer's padding */
|
||||
s.ids[0] = s.x;
|
||||
s.q[0] = 1.0;
|
||||
s.pos = pos;
|
||||
return s;
|
||||
}
|
||||
|
||||
/* One step's staging through mtp_couple_plan_stage, for request `r` on `card`. */
|
||||
RadMtpCoupleDraw stage_one(Request* r, uint64_t seq, int32_t card, std::string* why) {
|
||||
StepPlan plan;
|
||||
plan.e.push_back(entry(r, seq, card, r->output.size(), false));
|
||||
const Scheduler::SerialRow rows[] = { row(seq, card) };
|
||||
std::vector<RadMtpCoupleDraw> by_card(8);
|
||||
RadMtpCoupleDraw stage[1];
|
||||
if (mtp_couple_plan_stage(plan, 8, rows, 1, &by_card, stage, why) != RAD_OK) return {};
|
||||
return stage[0];
|
||||
}
|
||||
|
||||
/* The card's slot after a step that accepted `n_acc - 1` drafts: the bind checks each round's
|
||||
* state against the token the card holds for it. */
|
||||
StepSlot slot_for(const int32_t* tok, int rounds, int n_acc) {
|
||||
StepSlot S{};
|
||||
S.acc = n_acc - 1;
|
||||
S.n_acc = n_acc;
|
||||
for (int rd = 1; rd <= rounds; ++rd) S.tok[rd] = tok[rd];
|
||||
return S;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* VARYING n_acc, SEVERAL ROUNDS, A CUT COMMIT. Every round of a live request binds at the length
|
||||
* its commit left, which is the position the next build samples it at. The cut commit closes the
|
||||
* request; its rows bind nothing, and the old formula's position for them is provably not a row the
|
||||
* sampler would stage. */
|
||||
TEST(the_bound_position_is_the_verify_rows_sampling_position) {
|
||||
const int kRounds = 3;
|
||||
/* The card's one-based acceptance (S.n_acc) and what the commit actually appended, per step.
|
||||
* emitted == n_acc while the request runs; the last step is the cut: the card accepted all
|
||||
* three drafts (n_acc 4), the token budget let two through, and the commit closed the request
|
||||
* with "length". A step after it does not exist for this request. */
|
||||
const int n_acc[] = { 1, 4, 3, 4 };
|
||||
const int emitted[] = { 1, 4, 3, 2 };
|
||||
const int steps = (int)(sizeof n_acc / sizeof n_acc[0]);
|
||||
|
||||
Request r;
|
||||
r.id = 7;
|
||||
r.sp = SamplingParams{};
|
||||
r.sp.temp = 1.0f;
|
||||
r.state = ReqState::Running;
|
||||
|
||||
std::vector<RadMtpCoupleState> bound; /* the states bound after the previous step */
|
||||
uint64_t prev_base = 0; /* the previous draw's pre-commit coordinate */
|
||||
int prev_n_acc = 0; /* and the card's one-based acceptance of it */
|
||||
for (int step = 0; step < steps; ++step) {
|
||||
std::string why;
|
||||
const RadMtpCoupleDraw draw = stage_one(&r, r.id, 2, &why);
|
||||
REQUIRE_EQ(why, std::string());
|
||||
/* The draw's base is the PRE-commit length: the draft's own coordinate, not the verify's. */
|
||||
CHECK_EQ(draw.base, (uint64_t)r.output.size());
|
||||
|
||||
/* The build of THIS step runs over the states bound at the previous one, at the length
|
||||
* that commit left. Every round lands on its row. */
|
||||
if (!bound.empty()) {
|
||||
for (int k = 0; k < kRounds; ++k) {
|
||||
const uint64_t n_out = (uint64_t)r.output.size();
|
||||
CHECK(rad_mtp_couple_binds(bound[(size_t)k], r.id, draw.seed, n_out, k,
|
||||
bound[(size_t)k].x, 1 << 20));
|
||||
/* The old formula on the same states: identical while the previous commit emitted
|
||||
* its whole acceptance, one ahead per missing token otherwise. */
|
||||
const uint64_t old_pos = prev_base + (uint64_t)prev_n_acc + (uint64_t)k;
|
||||
CHECK_EQ(old_pos == n_out + (uint64_t)k,
|
||||
emitted[step - 1] == n_acc[step - 1]);
|
||||
}
|
||||
}
|
||||
|
||||
/* The commit, and the request's state afterwards: a cut closes it. */
|
||||
for (int j = 0; j < emitted[step]; ++j) r.output.push_back(0);
|
||||
const bool live = emitted[step] == n_acc[step];
|
||||
r.state = live ? ReqState::Running : ReqState::Finished;
|
||||
|
||||
/* The bind, after the commit, through the production rule. The point is what the
|
||||
* scheduler's commit snapshot would carry: this fixture owns the request, so it is built
|
||||
* here directly. */
|
||||
MtpCoupleBindPoint bp;
|
||||
bp.found = true;
|
||||
bp.live = r.state == ReqState::Running;
|
||||
bp.n_out = (uint64_t)r.output.size();
|
||||
CHECK_EQ(bp.live, live);
|
||||
|
||||
const int32_t tok[] = { 11, 21, 22, 23 };
|
||||
const StepSlot S = slot_for(tok, kRounds, n_acc[step]);
|
||||
RadMtpCoupleState rb[(size_t)kRounds * 3]{};
|
||||
for (int rd = 1; rd <= kRounds; ++rd) {
|
||||
rb[(size_t)(rd - 1) * 3] = sampled(draw, rd, 0);
|
||||
rb[(size_t)(rd - 1) * 3].x = tok[rd]; /* the card's proposal */
|
||||
rb[(size_t)(rd - 1) * 3].ids[0] = tok[rd];
|
||||
}
|
||||
RadMtpCoupleState out[RAD_MTP_COUPLE_MAX_ROUNDS]{};
|
||||
int n_bound = -1;
|
||||
std::string bwhy;
|
||||
CHECK_EQ(mtp_couple_bind_rounds(bp, draw, rb, 3, kRounds, S, 1 << 20, out, &n_bound,
|
||||
&bwhy), RAD_OK);
|
||||
/* NOT LIVE, NOT BOUND: a terminal row must not read as a coupled verify. */
|
||||
CHECK_EQ(n_bound, live ? kRounds : 0);
|
||||
for (int rd = 1; rd <= (live ? kRounds : 0); ++rd) {
|
||||
const uint64_t want = (uint64_t)r.output.size() + (uint64_t)(rd - 1);
|
||||
CHECK_EQ(out[rd - 1].pos, want);
|
||||
CHECK(rad_mtp_couple_binds(out[rd - 1], r.id, draw.seed,
|
||||
(uint64_t)r.output.size(), rd - 1, tok[rd], 1 << 20));
|
||||
}
|
||||
|
||||
/* THE REGRESSION: where the old formula would have bound this step's rounds, the row
|
||||
* check fails as soon as the commit emitted fewer than the card accepted. */
|
||||
if (!live) {
|
||||
for (int rd = 1; rd <= kRounds; ++rd) {
|
||||
const uint64_t old_pos = draw.base + (uint64_t)n_acc[step] + (uint64_t)(rd - 1);
|
||||
CHECK(old_pos != (uint64_t)r.output.size() + (uint64_t)(rd - 1));
|
||||
CHECK(!rad_mtp_couple_binds(sampled(draw, rd, old_pos), r.id, draw.seed,
|
||||
(uint64_t)r.output.size(), rd - 1, tok[rd], 1 << 20));
|
||||
}
|
||||
}
|
||||
|
||||
prev_base = draw.base;
|
||||
prev_n_acc = n_acc[step];
|
||||
bound.assign(out, out + (live ? kRounds : 0));
|
||||
if (!live) break; /* the request is closed: no further step drafts it */
|
||||
}
|
||||
}
|
||||
|
||||
/* STALE STATES ARE REJECTED. A round from another step, a round whose position is off by one, and
|
||||
* a token the row does not propose all fail the rule -- the coupling never applies a law this step
|
||||
* did not draw. */
|
||||
TEST(a_state_from_another_step_is_refused) {
|
||||
Request r;
|
||||
r.id = 11;
|
||||
r.sp = SamplingParams{};
|
||||
r.sp.temp = 1.0f;
|
||||
std::string why;
|
||||
const RadMtpCoupleDraw d = stage_one(&r, r.id, 3, &why);
|
||||
REQUIRE_EQ(why, std::string());
|
||||
|
||||
const uint64_t n_out = 5;
|
||||
const RadMtpCoupleState good = sampled(d, 1, rad_mtp_couple_verify_pos(n_out, 1));
|
||||
CHECK(rad_mtp_couple_binds(good, r.id, d.seed, n_out, 0, good.x, 1 << 20));
|
||||
|
||||
/* One position ahead: the old arithmetic's answer. */
|
||||
CHECK(!rad_mtp_couple_binds(sampled(d, 1, rad_mtp_couple_verify_pos(n_out, 1) + 1), r.id,
|
||||
d.seed, n_out, 0, good.x, 1 << 20));
|
||||
/* Another step's round. */
|
||||
CHECK(!rad_mtp_couple_binds(sampled(d, 2, rad_mtp_couple_verify_pos(n_out, 2)), r.id,
|
||||
d.seed, n_out, 0, good.x, 1 << 20));
|
||||
/* Another seed, another sequence. */
|
||||
CHECK(!rad_mtp_couple_binds(good, r.id, d.seed + 1, n_out, 0, good.x, 1 << 20));
|
||||
CHECK(!rad_mtp_couple_binds(good, r.id + 1, d.seed, n_out, 0, good.x, 1 << 20));
|
||||
/* The row proposes a token this state did not draw, and a state with no Q at all. */
|
||||
CHECK(!rad_mtp_couple_binds(good, r.id, d.seed, n_out, 0, good.x + 1, 1 << 20));
|
||||
RadMtpCoupleState greedy = good;
|
||||
greedy.status = 0;
|
||||
greedy.draw.mode = 0;
|
||||
CHECK(!rad_mtp_couple_binds(greedy, r.id, d.seed, n_out, 0, good.x, 1 << 20));
|
||||
}
|
||||
|
||||
/* REQUEST AND SLOT REUSE. Two requests take turns on the same card, so each step's staged draw
|
||||
* names the sequence currently on it. The previous request's rounds are not accepted by the new
|
||||
* one's rows, whichever position they carry. */
|
||||
TEST(a_reused_card_serves_the_sequence_that_is_on_it) {
|
||||
Request a, b;
|
||||
a.id = 21;
|
||||
b.id = 22;
|
||||
a.sp = SamplingParams{};
|
||||
b.sp = SamplingParams{};
|
||||
a.output.assign(4, 0);
|
||||
b.output.assign(9, 0);
|
||||
|
||||
std::string why;
|
||||
const RadMtpCoupleDraw da = stage_one(&a, a.id, 5, &why);
|
||||
REQUIRE_EQ(why, std::string());
|
||||
const RadMtpCoupleDraw db = stage_one(&b, b.id, 5, &why);
|
||||
REQUIRE_EQ(why, std::string());
|
||||
CHECK_EQ(da.seq, (uint64_t)21);
|
||||
CHECK_EQ(db.seq, (uint64_t)22);
|
||||
|
||||
/* a's rounds, at a's length, do not bind a row of b -- not even at b's position. */
|
||||
const RadMtpCoupleState sa = sampled(da, 1, rad_mtp_couple_verify_pos((uint64_t)b.output.size(), 1));
|
||||
CHECK(!rad_mtp_couple_binds(sa, b.id, db.seed, (uint64_t)b.output.size(), 0, sa.x, 1 << 20));
|
||||
/* b's own state does. */
|
||||
const RadMtpCoupleState sb = sampled(db, 1, rad_mtp_couple_verify_pos((uint64_t)b.output.size(), 1));
|
||||
CHECK(rad_mtp_couple_binds(sb, b.id, db.seed, (uint64_t)b.output.size(), 0, sb.x, 1 << 20));
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
+190
-3
@@ -11,6 +11,7 @@
|
||||
#include "gbnf_oracle.h"
|
||||
#include "sample/grammar.h"
|
||||
#include "sample/host_ref.h"
|
||||
#include "sample/mtp_couple_evidence.h"
|
||||
#include "sample/sampler.h"
|
||||
#include "sample/vocab_view.h"
|
||||
#include "text/tokenizer.h"
|
||||
@@ -1496,6 +1497,7 @@ struct HostChain {
|
||||
int64_t nv = 0;
|
||||
bool ok = false;
|
||||
int st = RAD_OK;
|
||||
int64_t step_no = 0; /* the engine step the next step() stages for */
|
||||
|
||||
static inline HostChain* cur = nullptr;
|
||||
static void trampoline(RadCtx* c, const RadBatch*) {
|
||||
@@ -1503,11 +1505,14 @@ struct HostChain {
|
||||
}
|
||||
|
||||
HostChain(int64_t n_vocab, int64_t max_seqs, int max_spec, const VocabView* vocab = nullptr,
|
||||
int64_t max_candidates = 8)
|
||||
int64_t max_candidates = 8, const char* second_plugin = nullptr)
|
||||
: 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. */
|
||||
if (reg.load_plugin(home() + "/kernels/libref.so", 0) < 0) return;
|
||||
/* A second library by name, for an op libref does not carry (the coupling's verify op is
|
||||
* the spechub plugin's, host row included). */
|
||||
if (second_plugin && reg.load_plugin(home() + "/kernels/" + second_plugin, 0) < 0) return;
|
||||
RadModelMeta meta{};
|
||||
meta.arch_id = "sampler_chain";
|
||||
meta.name = "sampler_chain";
|
||||
@@ -1570,7 +1575,7 @@ struct HostChain {
|
||||
const int b = s.begin_request(r->id, r->sp, &err);
|
||||
if (b < 0) return b;
|
||||
}
|
||||
const int bs = s.build_step(reqs);
|
||||
const int bs = s.build_step(reqs, step_no++);
|
||||
if (bs < 0) return bs;
|
||||
int64_t n = 0;
|
||||
s.staged_params(&n);
|
||||
@@ -1610,6 +1615,27 @@ Request make_req(uint64_t id, std::vector<int32_t> prompt, const SamplingParams&
|
||||
return r;
|
||||
}
|
||||
|
||||
/* The per-round states the engine binds after a live commit: `rounds` sampled states of `id`,
|
||||
* each at `first_pos + rd - 1`, proposing the matching draft token. */
|
||||
void couple_states(uint64_t id, uint64_t seed, const int32_t* draft, int rounds,
|
||||
uint64_t first_pos, RadMtpCoupleState* out) {
|
||||
for (int rd = 1; rd <= rounds; ++rd) {
|
||||
RadMtpCoupleState& c = out[rd - 1];
|
||||
c = RadMtpCoupleState{};
|
||||
c.draw.seq = id;
|
||||
c.draw.seed = seed;
|
||||
c.draw.mode = 1;
|
||||
c.round = rd;
|
||||
c.status = 1;
|
||||
c.count = 1;
|
||||
c.x = draft[rd - 1];
|
||||
for (int i = 0; i < 8; ++i) c.ids[i] = -1;
|
||||
c.ids[0] = c.x;
|
||||
c.q[0] = 1.0;
|
||||
c.pos = first_pos + (uint64_t)(rd - 1);
|
||||
}
|
||||
}
|
||||
|
||||
SamplingParams greedy_params() {
|
||||
SamplingParams sp;
|
||||
sp.temp = 0.0f;
|
||||
@@ -3253,4 +3279,165 @@ TEST(a_dry_row_with_breakers_has_its_raw_logprob) {
|
||||
CHECK_NEAR(lp.logprob, 5.0 - log_sum_exp(lg), 1e-5);
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
/* ================================================== the coupled verify row */
|
||||
|
||||
/* THE COUPLED STATE AT THE SAMPLER'S OWN BUILD.
|
||||
*
|
||||
* The engine binds a round's state at `n_out + (round - 1)` (core/sched/mtp_couple_bind.h, through
|
||||
* rad_mtp_couple_verify_pos), and build_step stages row k of the request at `n_out + k`, taking the
|
||||
* state only when it names that exact row (rad_mtp_couple_binds). This drives the REAL build_step
|
||||
* with the coupling on: the bound rounds become the draft rows' states, the bonus row stays
|
||||
* unbound, and a state one position ahead -- what the engine's old `base + n_acc` formula produced
|
||||
* once a commit came up short -- is refused. A refused row is deliberately silent in production;
|
||||
* here it is the assertion.
|
||||
*
|
||||
* The verify op is declared by the spechub plugin, whose host row (ref_mtp_couple_verify) is what
|
||||
* makes the declaration succeed here; build_step alone is driven, so the op never runs. */
|
||||
TEST(the_coupled_state_is_staged_only_at_the_row_it_was_bound_for) {
|
||||
setenv("RADIANCE_MTP_COUPLED", "1", 1);
|
||||
HostChain ch(64, 2, 3, nullptr, 8, "spechub.so");
|
||||
unsetenv("RADIANCE_MTP_COUPLED");
|
||||
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));
|
||||
|
||||
/* The states the engine binds after a live commit, at the positions it writes them. */
|
||||
const uint64_t n_out = r.output.size();
|
||||
auto states_at = [&](uint64_t first_pos, RadMtpCoupleState* out) {
|
||||
couple_states(r.id, r.sp.seed, draft, 3, first_pos, out);
|
||||
};
|
||||
RadMtpCoupleState bound[3];
|
||||
states_at(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));
|
||||
int64_t n = 0;
|
||||
const DeviceSampleParams* p = ch.s.staged_params(&n);
|
||||
CHECK_EQ(n, 4); /* three draft rows and the bonus row */
|
||||
CHECK(ch.s.staged_any_couple());
|
||||
for (int k = 0; k < 3; ++k) {
|
||||
CHECK_EQ(ch.s.staged_couple(k).round, k + 1);
|
||||
CHECK_EQ(ch.s.staged_couple(k).x, draft[k]);
|
||||
CHECK_EQ(ch.s.staged_couple(k).pos, rad_mtp_couple_verify_pos(n_out, k + 1));
|
||||
/* The row is sampled at the position its state names -- asserted against the row itself,
|
||||
* not through the formula, so a drifted formula cannot move both sides together. */
|
||||
CHECK_EQ(p[k].pos, n_out + (uint64_t)k);
|
||||
CHECK_EQ(ch.s.staged_couple(k).pos, p[k].pos);
|
||||
}
|
||||
CHECK_EQ(ch.s.staged_couple(3).round, 0); /* the bonus row carries no law */
|
||||
|
||||
/* THE OLD FORMULA'S POSITION: one ahead of the row. Every row is refused, so the batch issues
|
||||
* no verify at all -- an inert coupling, which is what the engine's own formula used to
|
||||
* produce. */
|
||||
RadMtpCoupleState ahead[3];
|
||||
states_at(rad_mtp_couple_verify_pos(n_out, 1) + 1, ahead);
|
||||
CHECK_OK(ch.s.set_mtp_couple(r.id, ahead, 3));
|
||||
CHECK_OK(ch.s.build_step(reqs));
|
||||
CHECK(!ch.s.staged_any_couple());
|
||||
for (int k = 0; k < 4; ++k) CHECK_EQ(ch.s.staged_couple(k).round, 0);
|
||||
|
||||
/* REUSE: an ended request's binding is gone, and a new one starts unbound. */
|
||||
ch.s.end_request(r.id);
|
||||
CHECK_EQ(ch.s.set_mtp_couple(r.id, bound, 3), RAD_E_NOTFOUND);
|
||||
Request r2 = make_req(8, { 1, 2, 3 }, sp);
|
||||
r2.draft = draft;
|
||||
r2.n_draft = 3;
|
||||
CHECK_OK(ch.s.begin_request(r2.id, r2.sp, &err));
|
||||
CHECK_OK(ch.s.build_step({ &r2 }));
|
||||
CHECK(!ch.s.staged_any_couple());
|
||||
}
|
||||
|
||||
/* THE SNAPSHOT THE EVIDENCE READS.
|
||||
*
|
||||
* In the coupled serial path build_step stages the bound rows, then the drafter's build_greedy
|
||||
* clears any_couple_ and replaces the staged rows before collect_step's wait returns. An evidence
|
||||
* line gated on staged_any_couple() therefore never fires -- and forced, it would list the greedy
|
||||
* pass's rows. This drives the real chain for two requests: build_step, run() issuing the verify
|
||||
* op, build_greedy (where issue_serial_drafter calls it), then the evidence decision over the
|
||||
* frozen snapshot. The negatives are what the line must not count: a terminal request, another
|
||||
* step's snapshot, and a host row. */
|
||||
TEST(the_coupled_evidence_survives_the_drafters_greedy_build) {
|
||||
setenv("RADIANCE_MTP_COUPLED", "1", 1);
|
||||
HostChain ch(64, 2, 3, nullptr, 8, "spechub.so");
|
||||
unsetenv("RADIANCE_MTP_COUPLED");
|
||||
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 a = make_req(7, { 1, 2, 3 }, sp);
|
||||
Request b = make_req(8, { 4, 5, 6 }, sp);
|
||||
a.draft = b.draft = draft;
|
||||
a.n_draft = b.n_draft = 3;
|
||||
std::string err;
|
||||
for (Request* r : { &a, &b }) {
|
||||
CHECK_OK(ch.s.begin_request(r->id, r->sp, &err));
|
||||
RadMtpCoupleState bound[3];
|
||||
couple_states(r->id, r->sp.seed, draft, 3, rad_mtp_couple_verify_pos(r->output.size(), 1),
|
||||
bound);
|
||||
CHECK_OK(ch.s.set_mtp_couple(r->id, bound, 3));
|
||||
}
|
||||
|
||||
std::vector<float> lg(8 * 64, 0.0f); /* two requests, 3 draft rows + bonus each */
|
||||
for (int row = 0; row < 8; ++row) lg[(size_t)row * 64 + 5] = 5.0f;
|
||||
std::vector<int32_t> tok;
|
||||
CHECK_OK(ch.step({ &a, &b }, lg, &tok));
|
||||
const int64_t step = ch.step_no - 1;
|
||||
|
||||
const Sampler::CoupleBuild& cb = ch.s.couple_build();
|
||||
CHECK_EQ(cb.step, step);
|
||||
CHECK_EQ(cb.rows, (int64_t)6);
|
||||
REQUIRE_EQ(cb.seqs.size(), (size_t)2);
|
||||
CHECK_EQ(cb.seqs[0].first, (uint64_t)7);
|
||||
CHECK_EQ(cb.seqs[0].second, (int64_t)3);
|
||||
CHECK_EQ(cb.seqs[1].first, (uint64_t)8);
|
||||
CHECK_EQ(cb.seqs[1].second, (int64_t)3);
|
||||
/* run() issued the verify op for THIS build, not merely staged rows for it. */
|
||||
CHECK_EQ(ch.s.staged_couple_issued_step(), step);
|
||||
|
||||
/* The drafter's greedy passes, where issue_serial_drafter runs them: after the build, before
|
||||
* the wait. The old gate is false now; the snapshot is not the greedy pass's. */
|
||||
CHECK_OK(ch.s.build_greedy(4));
|
||||
CHECK(!ch.s.staged_any_couple());
|
||||
CHECK_EQ(ch.s.couple_build().step, step);
|
||||
CHECK_EQ(ch.s.couple_build().rows, (int64_t)6);
|
||||
|
||||
auto all_live = [](uint64_t) { return true; };
|
||||
MtpCoupleEvidence ev = mtp_couple_evidence(cb, step, RAD_DOMAIN_DEVICE, all_live);
|
||||
CHECK_EQ(ev.rows, (int64_t)6);
|
||||
CHECK(ev.device);
|
||||
CHECK_EQ(ev.seqs, std::string("7,8"));
|
||||
|
||||
/* A terminal request is not counted: b finished while the step ran. */
|
||||
ev = mtp_couple_evidence(cb, step, RAD_DOMAIN_DEVICE, [](uint64_t s) { return s == 7; });
|
||||
CHECK_EQ(ev.rows, (int64_t)3);
|
||||
CHECK_EQ(ev.seqs, std::string("7"));
|
||||
|
||||
/* Nothing live, or another step's snapshot: no evidence. */
|
||||
CHECK_EQ(mtp_couple_evidence(cb, step, RAD_DOMAIN_DEVICE, [](uint64_t) { return false; }).rows,
|
||||
(int64_t)0);
|
||||
CHECK_EQ(mtp_couple_evidence(cb, step + 1, RAD_DOMAIN_DEVICE, all_live).rows, (int64_t)0);
|
||||
|
||||
/* A host row is reported as host, not read as a device verify. */
|
||||
ev = mtp_couple_evidence(cb, step, RAD_DOMAIN_HOST, all_live);
|
||||
CHECK(!ev.device);
|
||||
CHECK_EQ(ev.rows, (int64_t)6);
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "rad_test.h"
|
||||
|
||||
#include "mm/processor.h"
|
||||
#include "sched/mtp_couple_bind.h"
|
||||
#include "sched/scheduler.h"
|
||||
#include "sched/advance_impl.h"
|
||||
#include "sched/derive.h"
|
||||
@@ -1471,6 +1472,204 @@ TEST(rejection_rolls_back_paged_kv_and_moves_the_linear_read_offset) {
|
||||
CHECK_EQ(f.sch.plan().e[0].conv_cursor, (cursor_before + 3) % W);
|
||||
}
|
||||
|
||||
/* THE COUPLED BIND AFTER A REAL COMMIT.
|
||||
*
|
||||
* Engine::finish_serial_drafter binds each round at the request's committed output length, read
|
||||
* from the plan after commit -- the value the next Sampler::build_step stages that row at. This
|
||||
* drives the same rule (sched/mtp_couple_bind.h) against the real Scheduler::commit: a live
|
||||
* commit, a max_tokens cut, and a card reused by a new request afterwards.
|
||||
*
|
||||
* The old engine formula (the draw's pre-commit base + the slot's one-based n_acc) agrees on every
|
||||
* commit of a request that is still running, and drifts by exactly the tokens a cut withheld. A
|
||||
* commit that emits fewer than the card accepted has closed the request; its plan entry is carried
|
||||
* for the bind, but the request will never sample again. Binding it would put a coupled verify on
|
||||
* a terminal row, which reads as a live round in the log without being one. */
|
||||
TEST(the_coupled_bind_skips_a_request_the_commit_closed) {
|
||||
Fixture f;
|
||||
f.build();
|
||||
f.cfg.n_spec = 3;
|
||||
CHECK_OK(f.start());
|
||||
const int64_t vocab = f.prog.meta.n_vocab;
|
||||
|
||||
int32_t draft[3] = { 21, 22, 23 };
|
||||
auto readback = [&](uint64_t seq, uint64_t base, int rounds) {
|
||||
RadMtpCoupleDraw d{};
|
||||
d.seq = seq;
|
||||
d.base = base;
|
||||
d.mode = 1;
|
||||
std::vector<RadMtpCoupleState> rb((size_t)rounds);
|
||||
for (int rd = 1; rd <= rounds; ++rd) {
|
||||
RadMtpCoupleState& s = rb[(size_t)(rd - 1)];
|
||||
s.draw = d;
|
||||
s.round = rd;
|
||||
s.status = 1;
|
||||
s.count = 1;
|
||||
s.x = draft[rd - 1]; /* what the card holds for that round */
|
||||
for (int i = 0; i < 8; ++i) s.ids[i] = -1;
|
||||
s.ids[0] = s.x;
|
||||
s.q[0] = 1.0;
|
||||
}
|
||||
return rb;
|
||||
};
|
||||
auto slot = [&]() {
|
||||
StepSlot S{};
|
||||
S.acc = 3;
|
||||
S.n_acc = 4;
|
||||
for (int rd = 1; rd <= 3; ++rd) S.tok[rd] = draft[rd - 1];
|
||||
return S;
|
||||
};
|
||||
|
||||
/* ---- a live commit: three drafts accepted, the request keeps running. */
|
||||
const uint64_t a = f.submit(160, 64);
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
f.commit_plain(11);
|
||||
CHECK_OK(f.sch.set_draft(a, draft, 3));
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
const uint64_t a_base = f.sch.find(a)->output.size(); /* the draw's pre-commit coordinate */
|
||||
CHECK_OK(f.commit(9, 0, [](const StepEntry&) { return 3; }));
|
||||
REQUIRE(f.sch.find(a) != nullptr);
|
||||
CHECK_EQ((int)f.sch.find(a)->state, (int)ReqState::Running);
|
||||
CHECK_EQ((int)f.sch.find(a)->output.size(), (int)a_base + 4);
|
||||
|
||||
MtpCoupleBindPoint pa;
|
||||
pa.found = f.sch.committed_seq(a, &pa.n_out, &pa.live);
|
||||
CHECK(pa.found);
|
||||
CHECK(pa.live);
|
||||
CHECK_EQ(pa.n_out, (uint64_t)f.sch.find(a)->output.size());
|
||||
|
||||
const std::vector<RadMtpCoupleState> rba = readback(a, a_base, 3);
|
||||
const StepSlot Sa = slot();
|
||||
RadMtpCoupleState bound[RAD_MTP_COUPLE_MAX_ROUNDS]{};
|
||||
int n_bound = -1;
|
||||
std::string why;
|
||||
CHECK_EQ(mtp_couple_bind_rounds(pa, rba[0].draw, rba.data(), 1, 3, Sa, vocab, bound,
|
||||
&n_bound, &why), RAD_OK);
|
||||
CHECK_EQ(n_bound, 3);
|
||||
for (int k = 0; k < 3; ++k) {
|
||||
/* The next build stages row k at n_out + k. */
|
||||
CHECK_EQ(bound[k].pos, pa.n_out + (uint64_t)k);
|
||||
CHECK(rad_mtp_couple_binds(bound[k], a, rba[0].draw.seed, pa.n_out, k, draft[k], vocab));
|
||||
}
|
||||
/* The old formula on the same data: identical, because this commit emitted its whole
|
||||
* acceptance. */
|
||||
for (int k = 0; k < 3; ++k)
|
||||
CHECK_EQ(a_base + 4 + (uint64_t)k, pa.n_out + (uint64_t)k);
|
||||
|
||||
/* ---- a cut commit: the card accepted three drafts, the budget lets two through. */
|
||||
const uint64_t b = f.submit(160, /*max_tokens=*/3);
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
f.commit_plain(11); /* b's first output token */
|
||||
CHECK_OK(f.sch.set_draft(a, draft, 3));
|
||||
CHECK_OK(f.sch.set_draft(b, draft, 3));
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
const uint64_t b_base = f.sch.find(b)->output.size();
|
||||
CHECK_OK(f.commit(9, 0, [&](const StepEntry& e) { return e.seq == b ? 3 : 0; }));
|
||||
REQUIRE(f.sch.find(b) != nullptr);
|
||||
CHECK_EQ((int)f.sch.find(b)->state, (int)ReqState::Finished);
|
||||
CHECK_EQ((int)f.sch.find(b)->output.size(), 3); /* 1 + 2, not 1 + 4 */
|
||||
|
||||
/* The commit snapshot still names b -- the terminal step is carried into the bind -- but it is
|
||||
* not live: nothing binds, so no coupled verify is staged for a request that is gone. */
|
||||
MtpCoupleBindPoint pb;
|
||||
pb.found = f.sch.committed_seq(b, &pb.n_out, &pb.live);
|
||||
CHECK(pb.found);
|
||||
CHECK(!pb.live);
|
||||
CHECK_EQ(pb.n_out, (uint64_t)f.sch.find(b)->output.size());
|
||||
|
||||
const std::vector<RadMtpCoupleState> rbb = readback(b, b_base, 3);
|
||||
const StepSlot Sb = slot();
|
||||
CHECK_EQ(mtp_couple_bind_rounds(pb, rbb[0].draw, rbb.data(), 1, 3, Sb, vocab, bound,
|
||||
&n_bound, &why), RAD_OK);
|
||||
CHECK_EQ(n_bound, 0);
|
||||
/* THE OLD FORMULA would have bound b's rounds at base + n_acc + k -- two past the length it
|
||||
* actually committed, on a row no build will stage. */
|
||||
for (int k = 0; k < 3; ++k) {
|
||||
const uint64_t old_pos = b_base + 4 + (uint64_t)k;
|
||||
CHECK(old_pos != pb.n_out + (uint64_t)k);
|
||||
RadMtpCoupleState stale = rbb[(size_t)k];
|
||||
stale.pos = old_pos;
|
||||
CHECK(!rad_mtp_couple_binds(stale, b, rbb[0].draw.seed, pb.n_out, k, draft[k], vocab));
|
||||
}
|
||||
|
||||
/* ---- reuse: the next request on a card binds at its own length, and the closed request's
|
||||
* rounds are not accepted by it. */
|
||||
const uint64_t c = f.submit(160, 64);
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
f.commit_plain(11);
|
||||
CHECK_OK(f.sch.set_draft(c, draft, 3));
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
const uint64_t c_base = f.sch.find(c)->output.size();
|
||||
CHECK_OK(f.commit(9, 0, [](const StepEntry&) { return 3; }));
|
||||
MtpCoupleBindPoint pc;
|
||||
pc.found = f.sch.committed_seq(c, &pc.n_out, &pc.live);
|
||||
CHECK(pc.found);
|
||||
CHECK(pc.live);
|
||||
CHECK_EQ(pc.n_out, (uint64_t)f.sch.find(c)->output.size());
|
||||
CHECK_EQ(pc.n_out, c_base + 4);
|
||||
CHECK(!rad_mtp_couple_binds(rbb[0], c, rbb[0].draw.seed, pc.n_out, 0, draft[0], vocab));
|
||||
RadMtpCoupleState own = readback(c, c_base, 1)[0];
|
||||
own.pos = rad_mtp_couple_verify_pos(pc.n_out, 1);
|
||||
CHECK(rad_mtp_couple_binds(own, c, own.draw.seed, pc.n_out, 0, draft[0], vocab));
|
||||
}
|
||||
|
||||
/* THE BIND POINT OUTLIVES A REAP.
|
||||
*
|
||||
* commit() retires the step, and a request it finished that the next step does not carry is pinned
|
||||
* by nothing any more. The server calls reap() from an HTTP thread as soon as it sees the finish,
|
||||
* and reap() destroys the Request -- while Engine::finish_serial_drafter still has to bind that
|
||||
* row. The bind point is therefore copied by commit under the scheduler lock
|
||||
* (Scheduler::committed_seq), not read off the plan's StepEntry::req, which is dangling by then.
|
||||
* This reaps a non-carried terminal request between commit and bind: the plan-pointer version
|
||||
* could only answer by reading freed memory, and the snapshot answers from the copy.
|
||||
*/
|
||||
TEST(the_committed_bind_point_outlives_a_reap) {
|
||||
Fixture f;
|
||||
f.build();
|
||||
f.cfg.n_spec = 3;
|
||||
CHECK_OK(f.start());
|
||||
const int64_t vocab = f.prog.meta.n_vocab;
|
||||
|
||||
int32_t draft[3] = { 21, 22, 23 };
|
||||
const uint64_t a = f.submit(160, 64); /* live through the commit */
|
||||
const uint64_t b = f.submit(160, /*max_tokens=*/3); /* closed on it by the cut */
|
||||
CHECK(f.sch.step() != nullptr);
|
||||
f.commit_plain(11);
|
||||
CHECK_OK(f.sch.set_draft(a, draft, 3));
|
||||
CHECK_OK(f.sch.set_draft(b, draft, 3));
|
||||
REQUIRE(f.sch.step() != nullptr);
|
||||
CHECK_OK(f.commit(9, 0, [&](const StepEntry& e) { return e.seq == b ? 3 : 0; }));
|
||||
CHECK_EQ((int)f.sch.find(b)->state, (int)ReqState::Finished);
|
||||
CHECK_EQ((int)f.sch.find(a)->state, (int)ReqState::Running);
|
||||
|
||||
/* The server's reap lands between commit and bind: b's Request is destroyed here. */
|
||||
CHECK_OK(f.sch.reap(b));
|
||||
CHECK(f.sch.find(b) == nullptr);
|
||||
|
||||
MtpCoupleBindPoint pb;
|
||||
pb.found = f.sch.committed_seq(b, &pb.n_out, &pb.live);
|
||||
CHECK(pb.found);
|
||||
CHECK(!pb.live); /* terminal: nothing binds */
|
||||
CHECK_EQ(pb.n_out, (uint64_t)3);
|
||||
|
||||
MtpCoupleBindPoint pa;
|
||||
pa.found = f.sch.committed_seq(a, &pa.n_out, &pa.live);
|
||||
CHECK(pa.found);
|
||||
CHECK(pa.live);
|
||||
CHECK_EQ(pa.n_out, (uint64_t)f.sch.find(a)->output.size());
|
||||
|
||||
RadMtpCoupleState rb[RAD_MTP_COUPLE_MAX_ROUNDS]{};
|
||||
RadMtpCoupleState out[RAD_MTP_COUPLE_MAX_ROUNDS]{};
|
||||
StepSlot S{};
|
||||
S.acc = 3;
|
||||
S.n_acc = 4;
|
||||
for (int rd = 1; rd <= 3; ++rd) S.tok[rd] = draft[rd - 1];
|
||||
int n_bound = -1;
|
||||
std::string why;
|
||||
CHECK_EQ(mtp_couple_bind_rounds(pb, RadMtpCoupleDraw{}, rb, 1, 3, S, vocab, out, &n_bound,
|
||||
&why), RAD_OK);
|
||||
CHECK_EQ(n_bound, 0);
|
||||
}
|
||||
|
||||
/* A decode benchmark needs the request to last as long as asked, not as long as the model
|
||||
* chooses. EOS still stops an ordinary request, and max_tokens still stops one ignoring EOS. */
|
||||
TEST(ignore_eos_runs_to_max_tokens_and_nothing_else_changes) {
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
/* tests/spechub_accept_test.cpp -- the coupling's ACCEPTANCE, measured on a real candidate stream.
|
||||
*
|
||||
* spechub_couple_test pins the law. This file measures the number the flag exists for: how many
|
||||
* draft tokens survive verification, compared against the same drafts WITHOUT the coupling's
|
||||
* acceptance rule.
|
||||
*
|
||||
* The comparator is honest about what it is. It does not run a card, a checkpoint or libr4d; it
|
||||
* runs the two decisions over the SAME candidate stream, the same drafts and the same uniforms:
|
||||
*
|
||||
* ORDINARY sample_pick's rule: the draft is a proposal the target checks for equality, so a
|
||||
* draft survives only when it IS the target's own pick. That is the equality
|
||||
* acceptance the plain MTP path applies (core/sample/accept.h).
|
||||
*
|
||||
* COUPLED rad_mtp_couple_decide: accept x with probability min(1, P(x)/Q(x)), and on refusal
|
||||
* emit a draw from the residual (P-Q)+. The COMPOSED law is P (spechub_couple_test
|
||||
* proves it), so the emitted token agrees with the target's own draw more often.
|
||||
*
|
||||
* Both are measured as "the emitted token equals the token the target itself drew" over the same
|
||||
* trials, which is the quantity acceptance length is built from. The report prints the two rates.
|
||||
*
|
||||
* What this is NOT: a serving measurement. The source's own ABI-16 benchmark is the only end-to-end
|
||||
* evidence and it is cited as such in the PR body.
|
||||
*/
|
||||
#include "rad_test.h"
|
||||
|
||||
#include "rad_sample.h"
|
||||
#include "rad_spechub.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
/* Softmax over a shortlist in the verify's own FP64 formula, and the index it picks for a uniform. */
|
||||
int32_t pick_from(const std::vector<float>& v, double u) {
|
||||
float top = -INFINITY;
|
||||
for (float x : v) top = std::max(top, x);
|
||||
double sum = 0, w[16] = {};
|
||||
for (size_t i = 0; i < v.size(); ++i) { w[i] = std::exp((double)v[i] - top); sum += w[i]; }
|
||||
double c = u * sum;
|
||||
for (size_t i = 0; i < v.size(); ++i) { c -= w[i]; if (c < 0) return (int32_t)i; }
|
||||
return (int32_t)v.size() - 1;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* THE ACCEPTANCE COMPARATOR. Over 20000 trials on one shortlist: the target's own draw t, a draft
|
||||
* x drawn from the head's Q, and the two decisions. Prints both rates and requires the coupled
|
||||
* rate to be at least the ordinary one -- never below, which is the whole claim. */
|
||||
TEST(the_coupled_acceptance_beats_equality_on_the_same_stream) {
|
||||
/* The target's candidate plane: 8 live candidates, a plausible decode shape. */
|
||||
const std::vector<float> pval = { 3.10f, 2.95f, 2.40f, 1.80f, 1.10f, 0.40f, -0.60f, -2.00f };
|
||||
/* The head's own shortlist: the same 8, a flatter ranking, which is what a 2-bit head gives. */
|
||||
const std::vector<float> qval = { 2.60f, 2.55f, 2.30f, 2.00f, 1.60f, 1.20f, 0.60f, -0.40f };
|
||||
std::vector<int32_t> ids;
|
||||
for (int i = 0; i < 8; ++i) ids.push_back(i);
|
||||
|
||||
const uint64_t seed = 90210;
|
||||
const int trials = 20000;
|
||||
int64_t ordinary_ok = 0, coupled_ok = 0, drafts = 0;
|
||||
|
||||
for (int k = 0; k < trials; ++k) {
|
||||
const uint64_t pos = (uint64_t)k;
|
||||
/* The target's own draw at this position: what acceptance is measured against. */
|
||||
const int32_t target =
|
||||
pick_from(pval, rad_spechub_uniform(seed, pos, RAD_SAMPLE_KEY_PICK));
|
||||
|
||||
/* The head's draft, from its own Q, with the coupling's own draw key. */
|
||||
RadMtpCoupleState s{};
|
||||
s.draw.seed = seed;
|
||||
s.draw.base = pos;
|
||||
s.draw.mode = 1;
|
||||
s.round = 1;
|
||||
s.status = 1;
|
||||
int32_t qidx[8];
|
||||
float qv[8];
|
||||
for (int i = 0; i < 8; ++i) { qidx[i] = i; qv[i] = qval[(size_t)i]; }
|
||||
CHECK(rad_mtp_couple_build_q(qidx, qv, 8, &s));
|
||||
s.x = s.ids[rad_mtp_couple_pick(
|
||||
s, rad_spechub_uniform(seed, pos, RAD_MTP_COUPLE_KEY_DRAFT + 1))];
|
||||
++drafts;
|
||||
|
||||
/* ORDINARY: equality, the plain MTP verifier's rule. */
|
||||
if (s.x == target) ++ordinary_ok;
|
||||
|
||||
/* COUPLED: the maximal coupling, then the emitted token against the same target draw. */
|
||||
int32_t out = -1;
|
||||
CHECK(rad_mtp_couple_decide(s, ids.data(), pval.data(), 8, 8,
|
||||
rad_spechub_uniform(seed, pos, RAD_SAMPLE_KEY_ACCEPT),
|
||||
rad_spechub_uniform(seed, pos, RAD_SAMPLE_KEY_RESID), &out));
|
||||
if (out == target) ++coupled_ok;
|
||||
}
|
||||
|
||||
const double ordinary = (double)ordinary_ok / (double)drafts;
|
||||
const double coupled = (double)coupled_ok / (double)drafts;
|
||||
std::fprintf(stderr,
|
||||
" acceptance over %d coupled draws: equality %.6f, coupling %.6f\n",
|
||||
trials, ordinary, coupled);
|
||||
|
||||
/* THE TWO RATES IN CLOSED FORM. The target's draw and the draft are independent, so:
|
||||
* equality acceptance = sum_i P_i Q_i (the draft must equal the target's draw)
|
||||
* the coupling = sum_i P_i^2 (its output is distributed as P, and the target's own
|
||||
* draw is an independent P draw)
|
||||
* Checking against these turns the measurement into an oracle rather than a threshold: a ported
|
||||
* rule that drifted would miss its own closed form, not a number somebody picked. */
|
||||
auto softmax = [](const std::vector<float>& v, double* out) {
|
||||
float top = -INFINITY;
|
||||
for (float x : v) top = std::max(top, x);
|
||||
double sum = 0;
|
||||
for (size_t i = 0; i < v.size(); ++i) { out[i] = std::exp((double)v[i] - top); sum += out[i]; }
|
||||
for (size_t i = 0; i < v.size(); ++i) out[i] /= sum;
|
||||
};
|
||||
const size_t n = pval.size();
|
||||
std::vector<double> P(n), Q(n);
|
||||
softmax(pval, P.data());
|
||||
softmax(qval, Q.data());
|
||||
double want_ordinary = 0.0, want_coupled = 0.0;
|
||||
for (size_t i = 0; i < n; ++i) {
|
||||
want_ordinary += P[i] * Q[i];
|
||||
want_coupled += P[i] * P[i];
|
||||
}
|
||||
const double sig = std::sqrt(0.25 / (double)drafts); /* a binomial's widest sigma */
|
||||
CHECK_NEAR(ordinary, want_ordinary, 5.0 * sig);
|
||||
CHECK_NEAR(coupled, want_coupled, 5.0 * sig);
|
||||
/* AND THE CLAIM, which follows from the two closed forms: sigma(P^2) >= sigma(P Q) because P is
|
||||
* the draft's own law. Strictly here, since P != Q on this stream. */
|
||||
CHECK(want_coupled > want_ordinary);
|
||||
CHECK(coupled > ordinary);
|
||||
}
|
||||
|
||||
/* AND THE DRAFT ITSELF IS STILL SAMPLED FROM Q. The coupling can only pay if x~Q, so this pins that
|
||||
* the propose row's draw is the FP64 softmax of the shortlist and not the argmax: over many rounds
|
||||
* every candidate with mass appears, and the frequencies match Q. */
|
||||
TEST(the_drafted_token_is_sampled_from_the_heads_own_law) {
|
||||
const std::vector<float> qval = { 1.0f, 0.4f, -0.3f, -1.2f };
|
||||
int32_t qidx[8];
|
||||
float qv[8];
|
||||
for (int i = 0; i < 8; ++i) { qidx[i] = i < 4 ? i : -1; qv[i] = i < 4 ? qval[(size_t)i] : -INFINITY; }
|
||||
|
||||
double sum = 0, Q[4] = {};
|
||||
for (int i = 0; i < 4; ++i) { Q[i] = std::exp((double)qval[(size_t)i] - 1.0); sum += Q[i]; }
|
||||
for (int i = 0; i < 4; ++i) Q[i] /= sum;
|
||||
|
||||
const int trials = 20000;
|
||||
int64_t seen[4] = {};
|
||||
for (int k = 0; k < trials; ++k) {
|
||||
RadMtpCoupleState s{};
|
||||
s.draw.seed = 7;
|
||||
s.draw.base = (uint64_t)k;
|
||||
s.draw.mode = 1;
|
||||
s.round = 1;
|
||||
s.status = 1;
|
||||
CHECK(rad_mtp_couple_build_q(qidx, qv, 8, &s));
|
||||
const int32_t x = s.ids[rad_mtp_couple_pick(
|
||||
s, rad_spechub_uniform(7, (uint64_t)k, RAD_MTP_COUPLE_KEY_DRAFT + 1))];
|
||||
CHECK(x >= 0 && x < 4);
|
||||
++seen[x];
|
||||
}
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
const double got = (double)seen[i] / (double)trials;
|
||||
const double sigma = std::sqrt(Q[i] * (1.0 - Q[i]) / (double)trials);
|
||||
CHECK_NEAR(got, Q[i], 5.0 * sigma + 1e-9);
|
||||
}
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
@@ -0,0 +1,282 @@
|
||||
/* tests/spechub_cooperative_test.cpp -- the cooperative verify against the serial one.
|
||||
*
|
||||
* The cooperative kernel writes P's weights and the residual mass across the workgroup and then
|
||||
* walks both planes in ascending candidate order on one thread. That is a REORDERING of the
|
||||
* compute, not of the arithmetic -- and the arithmetic is the ABI here, so the claim has to be
|
||||
* checked rather than argued.
|
||||
*
|
||||
* What this file checks, over the same rows, the same states and the same uniforms:
|
||||
*
|
||||
* PARITY the cooperative decision emits the same token as rad_mtp_couple_decide (the serial
|
||||
* reference the plugin's host row runs) on every row of every width case.
|
||||
*
|
||||
* WIDTHS 129, 1024 and 1025 candidates. 1025 is the case that falls out of the shared-plane
|
||||
* band into the general single-threaded algorithm; it must still agree.
|
||||
*
|
||||
* HOSTILE a row with a hole in the middle, a duplicate id, an -inf candidate, and a row whose
|
||||
* state does not bind -- all of which the planes must treat exactly as the serial walk
|
||||
* does.
|
||||
*
|
||||
* The kernel itself is not launched here: this is the HOST half of the same algorithm, stepped
|
||||
* through the same phase sequence, so a divergence between the two would have to live in the
|
||||
* device code alone. The device launch is covered by the comparator probe the PR body reports.
|
||||
*/
|
||||
#include "rad_test.h"
|
||||
|
||||
#include "rad_sample.h"
|
||||
#include "rad_spechub.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
/* The cooperative algorithm, on the host: the same phases the kernel runs, over the same shared
|
||||
* planes. Deliberately a transcription of the kernel's body, so a change to one that is not made to
|
||||
* the other shows up as a mismatch against rad_mtp_couple_decide. */
|
||||
bool cooperative_decide(const RadMtpCoupleState& s, const std::vector<int32_t>& ids,
|
||||
const std::vector<float>& val, int64_t vocab, double u_accept,
|
||||
double u_resid, int32_t* out) {
|
||||
const int64_t n_cand = (int64_t)ids.size();
|
||||
std::vector<double> weight((size_t)n_cand, 0.0), mass((size_t)n_cand, 0.0);
|
||||
double pq[8] = {};
|
||||
int first[8];
|
||||
double sum = 0;
|
||||
float top = -INFINITY;
|
||||
int ix = -1;
|
||||
bool dominated = true;
|
||||
|
||||
/* PHASE 1: the gate, exactly the kernel's. */
|
||||
bool eligible = false;
|
||||
if (s.status == 1) {
|
||||
float maximum = -INFINITY;
|
||||
bool valid = true;
|
||||
for (int64_t j = 0; j < n_cand; ++j) {
|
||||
if (ids[(size_t)j] < 0) continue;
|
||||
if (ids[(size_t)j] >= vocab || val[(size_t)j] != val[(size_t)j] ||
|
||||
val[(size_t)j] == INFINITY) {
|
||||
valid = false;
|
||||
break;
|
||||
}
|
||||
if (val[(size_t)j] > maximum) maximum = val[(size_t)j];
|
||||
}
|
||||
if (valid && maximum != -INFINITY) { top = maximum; eligible = true; }
|
||||
}
|
||||
if (!eligible) return false;
|
||||
|
||||
/* PHASE 2: the weights, per slot. */
|
||||
for (int64_t j = 0; j < n_cand; ++j)
|
||||
if (ids[(size_t)j] >= 0 && val[(size_t)j] != -INFINITY)
|
||||
weight[(size_t)j] = std::exp((double)val[(size_t)j] - top);
|
||||
|
||||
/* PHASE 3: the ordered sum and the Q aggregation. */
|
||||
for (int k = 0; k < s.count; ++k) { pq[k] = 0; first[k] = -1; }
|
||||
for (int64_t j = 0; j < n_cand; ++j) {
|
||||
if (ids[(size_t)j] < 0 || val[(size_t)j] == -INFINITY) continue;
|
||||
const double w = weight[(size_t)j];
|
||||
sum += w;
|
||||
for (int k = 0; k < s.count; ++k)
|
||||
if (ids[(size_t)j] == s.ids[k]) {
|
||||
if (first[k] < 0) first[k] = (int)j;
|
||||
pq[k] += w;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!(sum > 0) || !rad_spechub_finite(sum)) return false;
|
||||
for (int k = 0; k < s.count; ++k) {
|
||||
pq[k] /= sum;
|
||||
dominated = dominated && pq[k] >= s.q[k];
|
||||
if (s.ids[k] == s.x) ix = k;
|
||||
}
|
||||
if (ix < 0) return false;
|
||||
|
||||
/* PHASE 4: the residual mass, per slot. */
|
||||
for (int64_t j = 0; j < n_cand; ++j) {
|
||||
double m = 0;
|
||||
if (ids[(size_t)j] >= 0 && val[(size_t)j] != -INFINITY) {
|
||||
bool in_q = false;
|
||||
for (int k = 0; k < s.count; ++k)
|
||||
if (s.ids[k] == ids[(size_t)j]) {
|
||||
in_q = true;
|
||||
if (first[k] == (int)j) m = rad_spechub_positive(pq[k] - s.q[k]);
|
||||
break;
|
||||
}
|
||||
if (!in_q) m = weight[(size_t)j] / sum;
|
||||
}
|
||||
mass[(size_t)j] = m;
|
||||
}
|
||||
|
||||
/* PHASE 5: the ordered walk. */
|
||||
double resid = 0;
|
||||
for (int64_t j = 0; j < n_cand; ++j) resid += mass[(size_t)j];
|
||||
int32_t result = -1;
|
||||
if (!dominated && !(resid > 0 && rad_spechub_finite(resid))) {
|
||||
double cdf = u_resid * sum;
|
||||
for (int64_t j = 0; j < n_cand; ++j)
|
||||
if (ids[(size_t)j] >= 0 && val[(size_t)j] != -INFINITY) {
|
||||
const double w = weight[(size_t)j];
|
||||
if (w > 0) { result = ids[(size_t)j]; cdf -= w; if (cdf < 0) break; }
|
||||
}
|
||||
} else if (pq[ix] >= s.q[ix] || u_accept * s.q[ix] < pq[ix]) {
|
||||
result = s.x;
|
||||
} else {
|
||||
double cdf = u_resid * resid;
|
||||
for (int64_t j = 0; j < n_cand; ++j) {
|
||||
if (ids[(size_t)j] == s.x) continue;
|
||||
const double m = mass[(size_t)j];
|
||||
if (m > 0) { result = ids[(size_t)j]; cdf -= m; if (cdf < 0) break; }
|
||||
}
|
||||
}
|
||||
*out = result;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* A bound state over the first `qcount` ids of the plane. */
|
||||
RadMtpCoupleState bound_state(const std::vector<int32_t>& ids, int qcount, uint64_t seed,
|
||||
uint64_t pos) {
|
||||
RadMtpCoupleState s{};
|
||||
s.draw.seed = seed;
|
||||
s.draw.base = pos;
|
||||
s.draw.mode = 1;
|
||||
s.round = 1;
|
||||
s.status = 1;
|
||||
s.pos = pos;
|
||||
int32_t qidx[8];
|
||||
float qv[8];
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
qidx[i] = i < qcount ? ids[(size_t)i] : -1;
|
||||
qv[i] = i < qcount ? -0.3f * (float)i : -INFINITY;
|
||||
}
|
||||
CHECK(rad_mtp_couple_build_q(qidx, qv, (int64_t)ids.size() + 1024, &s));
|
||||
s.x = s.ids[rad_mtp_couple_pick(
|
||||
s, rad_spechub_uniform(seed, pos, RAD_MTP_COUPLE_KEY_DRAFT + 1))];
|
||||
return s;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* PARITY AT EVERY WIDTH. 129, 1024 and 1025 candidates, 64 rows each, the same state and uniforms:
|
||||
* the cooperative decision must equal the serial reference on every row. 1025 is the width that
|
||||
* leaves the shared-plane band, so it exercises both paths. */
|
||||
TEST(the_cooperative_decision_equals_the_serial_reference_at_every_width) {
|
||||
for (int64_t n : { 129, 1024, 1025 }) {
|
||||
std::vector<int32_t> ids((size_t)n);
|
||||
std::vector<float> val((size_t)n);
|
||||
for (int64_t i = 0; i < n; ++i) {
|
||||
ids[(size_t)i] = (int32_t)i;
|
||||
/* A long decaying tail: the nucleus is narrow and the residual is a small, spread set,
|
||||
* which is where a mass-plane mistake would show. */
|
||||
val[(size_t)i] = 6.0f - 0.01f * (float)i;
|
||||
}
|
||||
int64_t agree = 0, refusals = 0;
|
||||
for (int k = 0; k < 64; ++k) {
|
||||
const uint64_t pos = (uint64_t)(n + k);
|
||||
const RadMtpCoupleState s = bound_state(ids, 8, 4242, pos);
|
||||
const double ua = rad_spechub_uniform(4242, pos, RAD_SAMPLE_KEY_ACCEPT);
|
||||
const double ur = rad_spechub_uniform(4242, pos, RAD_SAMPLE_KEY_RESID);
|
||||
|
||||
int32_t serial = -7, coop = -7;
|
||||
const bool ok_s = rad_mtp_couple_decide(s, ids.data(), val.data(), n, n, ua, ur,
|
||||
&serial);
|
||||
const bool ok_c = cooperative_decide(s, ids, val, n, ua, ur, &coop);
|
||||
CHECK_EQ(ok_c, ok_s);
|
||||
if (!ok_s) { ++refusals; continue; }
|
||||
if (serial == coop) ++agree;
|
||||
}
|
||||
CHECK_EQ(agree + refusals, (int64_t)64);
|
||||
CHECK_EQ(refusals, (int64_t)0);
|
||||
CHECK_EQ(agree, (int64_t)64);
|
||||
}
|
||||
}
|
||||
|
||||
/* HOSTILE PLANES. A hole in the middle, a duplicate id and an -inf candidate: the cooperative
|
||||
* planes must treat each exactly as the serial walk does, including the duplicate-first rule that
|
||||
* makes a repeated candidate contribute its Q mass once. */
|
||||
TEST(the_cooperative_decision_agrees_on_hostile_candidate_planes) {
|
||||
std::vector<int32_t> ids = { 0, 1, -1, 3, 3, 5, 6, -1, 8, 9 };
|
||||
std::vector<float> val = { 4.0f, 3.0f, -INFINITY, 1.5f, 1.4f, 0.5f, -1.0f, -INFINITY,
|
||||
-2.0f, -INFINITY };
|
||||
int64_t agree = 0, refused = 0;
|
||||
for (int k = 0; k < 256; ++k) {
|
||||
const uint64_t pos = (uint64_t)k;
|
||||
/* Q's shortlist holds each id ONCE -- a duplicate there is a malformed shortlist, which
|
||||
* build_q refuses by design -- while the CANDIDATE plane keeps its duplicate 3, which is the
|
||||
* duplicate-first rule the two decisions must share. */
|
||||
int32_t qidx[8];
|
||||
float qv[8];
|
||||
const int32_t qids[6] = { 0, 1, 3, 5, 6, 8 };
|
||||
for (int i = 0; i < 8; ++i) {
|
||||
qidx[i] = i < 6 ? qids[i] : -1;
|
||||
qv[i] = i < 6 ? -0.3f * (float)i : -INFINITY;
|
||||
}
|
||||
RadMtpCoupleState s{};
|
||||
s.draw.seed = 77;
|
||||
s.draw.base = pos;
|
||||
s.draw.mode = 1;
|
||||
s.round = 1;
|
||||
s.status = 1;
|
||||
s.pos = pos;
|
||||
CHECK(rad_mtp_couple_build_q(qidx, qv, 16, &s));
|
||||
s.x = s.ids[rad_mtp_couple_pick(
|
||||
s, rad_spechub_uniform(77, pos, RAD_MTP_COUPLE_KEY_DRAFT + 1))];
|
||||
const double ua = rad_spechub_uniform(77, pos, RAD_SAMPLE_KEY_ACCEPT);
|
||||
const double ur = rad_spechub_uniform(77, pos, RAD_SAMPLE_KEY_RESID);
|
||||
int32_t serial = -7, coop = -7;
|
||||
const bool ok_s =
|
||||
rad_mtp_couple_decide(s, ids.data(), val.data(), (int64_t)ids.size(), 16, ua, ur,
|
||||
&serial);
|
||||
const bool ok_c = cooperative_decide(s, ids, val, 16, ua, ur, &coop);
|
||||
CHECK_EQ(ok_c, ok_s);
|
||||
if (!ok_s) { ++refused; continue; }
|
||||
CHECK_EQ(coop, serial);
|
||||
++agree;
|
||||
}
|
||||
/* The duplicate id 3 appears twice; the plane is well formed, so no row should refuse. */
|
||||
CHECK_EQ(refused, (int64_t)0);
|
||||
CHECK_EQ(agree, (int64_t)256);
|
||||
}
|
||||
|
||||
/* THE GATE IS `rad_mtp_couple_state_valid`, AND IT IS THE SAME TEST ON BOTH SIDES. `decide` is total
|
||||
* over a candidate plane -- it answers whatever bytes it is given -- so the refusal that keeps an
|
||||
* uncoupled or stale row from overwriting the pick's token lives in the row gate both the kernel and
|
||||
* the sampler apply before calling it. This pins that gate rather than pretending `decide` checks a
|
||||
* state it is never given. */
|
||||
TEST(an_unbound_row_is_refused_by_the_shared_gate) {
|
||||
std::vector<int32_t> ids = { 0, 1, 2, 3 };
|
||||
std::vector<float> val = { 3.0f, 2.5f, 1.0f, -0.5f };
|
||||
RadMtpCoupleState s = bound_state(ids, 4, 9, 100);
|
||||
CHECK(rad_mtp_couple_state_valid(s, 8));
|
||||
|
||||
/* status 0: a greedy row, which has no Q to couple against. */
|
||||
RadMtpCoupleState bad = s;
|
||||
bad.status = 0;
|
||||
CHECK(!rad_mtp_couple_state_valid(bad, 8));
|
||||
|
||||
/* A q[] that no longer sums to 1: the draft's law is gone, so there is nothing to couple to. */
|
||||
bad = s;
|
||||
bad.q[0] += 0.25;
|
||||
CHECK(!rad_mtp_couple_state_valid(bad, 8));
|
||||
|
||||
/* An id outside the vocabulary, and an x outside the support. */
|
||||
bad = s;
|
||||
bad.ids[0] = 99;
|
||||
CHECK(!rad_mtp_couple_state_valid(bad, 8));
|
||||
bad = s;
|
||||
bad.x = 6;
|
||||
CHECK(!rad_mtp_couple_state_valid(bad, 8));
|
||||
|
||||
/* AND THE KERNEL GATES ON THE SAME THING. Its eligibility test is `s.status == 1 &&
|
||||
* !(p.flags & RAD_SP_GREEDY) && s.draw.seed == p.seed && s.pos == p.pos &&
|
||||
* rad_mtp_couple_state_valid(s, vocab)`, so a state that fails the validator above is refused
|
||||
* before any plane is touched. The cooperative host transcription begins with that same gate:
|
||||
* a status-0 state makes it refuse without writing. */
|
||||
RadMtpCoupleState unbound = s;
|
||||
unbound.status = 0;
|
||||
int32_t out = -7;
|
||||
CHECK(!cooperative_decide(unbound, ids, val, 8, 0.5, 0.5, &out));
|
||||
CHECK_EQ(out, (int32_t)-7); /* nothing was written */
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
@@ -0,0 +1,282 @@
|
||||
/* tests/spechub_couple_test.cpp -- the MTP coupling's arithmetic, checked against its own law.
|
||||
*
|
||||
* What can silently go wrong in a maximal coupling, and what each test pins:
|
||||
*
|
||||
* 1. THE COMPOSED LAW. A coupling is correct when the emitted token is distributed as the
|
||||
* TARGET's P, whatever the draft's Q was. The test builds a (P, Q) pair, draws many tokens
|
||||
* through the coupling, and compares the empirical frequencies against P. This is the one
|
||||
* property that a wrong acceptance rule, a wrong residual or a swapped mass cannot survive.
|
||||
*
|
||||
* 2. THE ACCEPTANCE RULE ITSELF. A draft token is accepted when it equals the token the target
|
||||
* itself drew at that position: with Q = delta_x the coupling must return x on every draw.
|
||||
* With P(x) >= Q(x) it must accept unconditionally, because there is no residual to fall
|
||||
* back on.
|
||||
*
|
||||
* 3. THE UNDERFLOW AND DUPLICATE RULES. r(x) is exactly zero on rejection (distinct doubles never
|
||||
* subtract to 0), so the residual walk can skip x. A candidate that appears twice contributes
|
||||
* its mass once. Both are properties of the shared helpers, checked directly.
|
||||
*
|
||||
* 4. THE Q BUILD. build_q drops entries that underflow, refuses duplicate ids, and stores exactly
|
||||
* the law the draw walks -- so the numbers in state.q[] are the law, not a normalised copy of
|
||||
* it that drifted.
|
||||
*
|
||||
* Nothing here talks to a card or to libr4d: the helpers are abi/rad_spechub.h, which the plugin and
|
||||
* the engine both compile.
|
||||
*/
|
||||
#include "rad_test.h"
|
||||
|
||||
#include "rad_sample.h"
|
||||
#include "rad_spechub.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
/* A state as the propose kernel would leave it: Q over `ids` from `scores`, x drawn from Q. */
|
||||
RadMtpCoupleState make_state(const std::vector<int32_t>& ids, const std::vector<float>& scores,
|
||||
int64_t vocab, uint64_t seed, uint64_t base, int round) {
|
||||
RadMtpCoupleState s{};
|
||||
s.draw.seed = seed;
|
||||
s.draw.base = base;
|
||||
s.draw.seq = 1;
|
||||
s.draw.card = 0;
|
||||
s.draw.mode = 1;
|
||||
s.round = round;
|
||||
s.status = 1;
|
||||
std::vector<int32_t> id8(8, -1);
|
||||
std::vector<float> v8(8, -INFINITY);
|
||||
for (size_t i = 0; i < ids.size() && i < 8; ++i) { id8[i] = ids[i]; v8[i] = scores[i]; }
|
||||
CHECK(rad_mtp_couple_build_q(id8.data(), v8.data(), vocab, &s));
|
||||
const double u = rad_spechub_uniform(seed, base, RAD_MTP_COUPLE_KEY_DRAFT + (uint64_t)round);
|
||||
s.x = s.ids[rad_mtp_couple_pick(s, u)];
|
||||
return s;
|
||||
}
|
||||
|
||||
/* The target's candidate plane: ids in [0, n), softmax scores. */
|
||||
void target_plane(int n, std::vector<int32_t>* ids, std::vector<float>* val) {
|
||||
ids->clear();
|
||||
val->clear();
|
||||
for (int i = 0; i < n; ++i) {
|
||||
ids->push_back(i);
|
||||
val->push_back(-0.15f * (float)i); /* a decaying, non-uniform target */
|
||||
}
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
/* THE COMPOSED LAW. Whatever Q is, the coupling's output must be distributed as the target's P.
|
||||
*
|
||||
* The draft must be DRAWN FROM Q ON EVERY TRIAL, which is what a round does. Holding x fixed and
|
||||
* repeating one draw measures the acceptance rate of a single proposal -- P(x)/Q(x) -- not the
|
||||
* composed distribution, and fails against P however correct the rule is.
|
||||
*
|
||||
* The tolerance is five sigma of a binomial at this sample count, so a wrong accept rule (mass on
|
||||
* x alone) or a naive residual (a renormalised P over the non-Q tokens only) cannot pass. */
|
||||
TEST(the_coupled_token_is_distributed_as_the_target) {
|
||||
const uint64_t seed = 424242;
|
||||
const int n = 8;
|
||||
std::vector<int32_t> ids;
|
||||
std::vector<float> val;
|
||||
target_plane(n, &ids, &val);
|
||||
|
||||
/* P from the same FP64 formula the verify uses. */
|
||||
double sum = 0, P[8] = {};
|
||||
float top = -INFINITY;
|
||||
for (float v : val) top = std::max(top, v);
|
||||
for (int i = 0; i < n; ++i) { P[i] = std::exp((double)val[i] - top); sum += P[i]; }
|
||||
for (int i = 0; i < n; ++i) P[i] /= sum;
|
||||
|
||||
/* Q deliberately differs from P: three tokens, in a different order of preference. */
|
||||
const int32_t qids[8] = { 0, 1, 2, -1, -1, -1, -1, -1 };
|
||||
const float qval[8] = { 0.0f, -0.5f, -1.0f, -INFINITY, -INFINITY, -INFINITY, -INFINITY,
|
||||
-INFINITY };
|
||||
|
||||
const int draws = 40000;
|
||||
std::map<int32_t, int64_t> seen;
|
||||
for (int k = 0; k < draws; ++k) {
|
||||
const uint64_t trial = (uint64_t)k;
|
||||
/* A fresh draft from the same shortlist on every trial, at that trial's coordinate. */
|
||||
RadMtpCoupleState s{};
|
||||
s.draw.seed = seed;
|
||||
s.draw.base = trial;
|
||||
s.draw.mode = 1;
|
||||
s.round = 1;
|
||||
s.status = 1;
|
||||
CHECK(rad_mtp_couple_build_q(qids, qval, 8, &s));
|
||||
const double u_draft =
|
||||
rad_spechub_uniform(seed, trial, RAD_MTP_COUPLE_KEY_DRAFT + 1);
|
||||
s.x = s.ids[rad_mtp_couple_pick(s, u_draft)];
|
||||
|
||||
int32_t out = -1;
|
||||
CHECK(rad_mtp_couple_decide(s, ids.data(), val.data(), n, 8,
|
||||
rad_spechub_uniform(seed, trial, RAD_SAMPLE_KEY_ACCEPT),
|
||||
rad_spechub_uniform(seed, trial, RAD_SAMPLE_KEY_RESID), &out));
|
||||
++seen[out];
|
||||
}
|
||||
for (int i = 0; i < n; ++i) {
|
||||
const double want = P[i];
|
||||
const double got = (double)seen[(int32_t)i] / (double)draws;
|
||||
const double sigma = std::sqrt(want * (1.0 - want) / (double)draws);
|
||||
CHECK_NEAR(got, want, 5.0 * sigma + 1e-9);
|
||||
}
|
||||
/* Every survivor can be emitted: a rule that lost a tail token would show as a zero here. */
|
||||
CHECK_EQ((int64_t)seen.size(), (int64_t)n);
|
||||
}
|
||||
|
||||
/* A DRAFT THE TARGET DOMINATES ON EVERY Q TOKEN IS ACCEPTED UNCONDITIONALLY: P(x) >= Q(x), so the
|
||||
* residual is empty and the ratio test has nothing to reject. The state here has ONE Q token, which
|
||||
* is the case a softmax over two tokens cannot give (two equal Q tokens of 0.5 each need P above
|
||||
* 0.5 on both, which a normalised P over three tokens cannot do). */
|
||||
TEST(a_draft_the_target_dominates_is_always_accepted) {
|
||||
RadMtpCoupleState one = make_state({ 2 }, { 0.0f }, 8, 9, 0, 1);
|
||||
CHECK_EQ(one.count, 1);
|
||||
CHECK_EQ(one.x, 2);
|
||||
std::vector<int32_t> ids = { 0, 1, 2 };
|
||||
std::vector<float> peak = { -20.0f, -20.0f, 9.0f }; /* P(2) ~ 1, Q(2) = 1 */
|
||||
for (int k = 0; k < 500; ++k) {
|
||||
int32_t out = -1;
|
||||
CHECK(rad_mtp_couple_decide(one, ids.data(), peak.data(), 3, 8,
|
||||
rad_spechub_uniform(7, (uint64_t)k, RAD_SAMPLE_KEY_ACCEPT),
|
||||
rad_spechub_uniform(7, (uint64_t)k, RAD_SAMPLE_KEY_RESID),
|
||||
&out));
|
||||
CHECK_EQ(out, 2);
|
||||
}
|
||||
|
||||
/* AND WHERE P IS SHORT OF Q THE RATIO RULE APPLIES: the same Q token with a target that barely
|
||||
* admits it is rejected most of the time, and the fallback can never emit x. */
|
||||
std::vector<float> flat = { 4.0f, 4.0f, -4.0f }; /* P(2) is small */
|
||||
int rejected = 0;
|
||||
for (int k = 0; k < 2000; ++k) {
|
||||
int32_t out = -1;
|
||||
CHECK(rad_mtp_couple_decide(one, ids.data(), flat.data(), 3, 8,
|
||||
rad_spechub_uniform(7, (uint64_t)k, RAD_SAMPLE_KEY_ACCEPT),
|
||||
rad_spechub_uniform(7, (uint64_t)k, RAD_SAMPLE_KEY_RESID),
|
||||
&out));
|
||||
if (out != 2) { ++rejected; CHECK(out == 0 || out == 1); }
|
||||
}
|
||||
CHECK(rejected > 0);
|
||||
}
|
||||
|
||||
/* THE RESIDUAL IS (P - Q)+ AND r(x) IS EXACTLY ZERO ON REJECTION. Here Q puts more mass on x than P
|
||||
* does, so x is sometimes rejected and the token must come from the OTHERS. The check that matters
|
||||
* is that y is never x on a rejected draw: r(x) contributes +0 exactly, which is what lets the walk
|
||||
* skip x without changing the total. */
|
||||
TEST(a_rejected_draft_falls_back_to_the_residual_support) {
|
||||
RadMtpCoupleState s = make_state({ 0, 1 }, { 0.0f, 0.0f }, 8, 11, 0, 1); /* Q(0) = Q(1) = 0.5 */
|
||||
CHECK_EQ(s.count, 2);
|
||||
std::vector<int32_t> ids = { 0, 1, 2 };
|
||||
/* P(0) = 0.09, so Q(0) = 0.5 exceeds it and x = 0 is rejected most of the time. */
|
||||
std::vector<float> val = { 0.0f, 2.2f, 1.0f };
|
||||
int accepted = 0, rejected = 0;
|
||||
for (int k = 0; k < 4000; ++k) {
|
||||
int32_t out = -1;
|
||||
CHECK(rad_mtp_couple_decide(s, ids.data(), val.data(), 3, 8,
|
||||
rad_spechub_uniform(3, (uint64_t)k, RAD_SAMPLE_KEY_ACCEPT),
|
||||
rad_spechub_uniform(3, (uint64_t)k, RAD_SAMPLE_KEY_RESID),
|
||||
&out));
|
||||
if (out == s.x) ++accepted;
|
||||
else { ++rejected; CHECK(out != s.x); }
|
||||
}
|
||||
CHECK(accepted > 0);
|
||||
CHECK(rejected > 0);
|
||||
}
|
||||
|
||||
/* build_q STORES THE LAW IT WALKS. A shortlist with a hole, a -inf score and a duplicate id: the
|
||||
* stored q[] sums to 1, the duplicate is refused, and the hole contributes nothing. */
|
||||
TEST(build_q_refuses_malformed_shortlists_and_drops_zero_mass) {
|
||||
RadMtpCoupleState s{};
|
||||
/* A hole at slot 1 and -inf at slot 3: two live candidates. */
|
||||
const int32_t ids[8] = { 4, -1, 7, 9, -1, -1, -1, -1 };
|
||||
const float val[8] = { 0.0f, 0.0f, -1.0f, -INFINITY, 0.0f, 0.0f, 0.0f, 0.0f };
|
||||
CHECK(rad_mtp_couple_build_q(ids, val, 16, &s));
|
||||
CHECK_EQ(s.count, 2);
|
||||
CHECK_EQ(s.ids[0], 4);
|
||||
CHECK_EQ(s.ids[1], 7);
|
||||
CHECK_EQ(s.ids[2], -1);
|
||||
CHECK_NEAR(s.q[0] + s.q[1], 1.0, 1e-15);
|
||||
|
||||
/* A duplicate id is a broken shortlist, not something to aggregate. */
|
||||
const int32_t dup[8] = { 4, 4, -1, -1, -1, -1, -1, -1 };
|
||||
CHECK(!rad_mtp_couple_build_q(dup, val, 16, &s));
|
||||
CHECK_EQ(s.count, 0);
|
||||
|
||||
/* An id outside the vocabulary, and a NaN score, are both refused. */
|
||||
const int32_t wide[8] = { 99, -1, -1, -1, -1, -1, -1, -1 };
|
||||
CHECK(!rad_mtp_couple_build_q(wide, val, 16, &s));
|
||||
const float nan_val[8] = { NAN, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f };
|
||||
const int32_t one[8] = { 2, -1, -1, -1, -1, -1, -1, -1 };
|
||||
CHECK(!rad_mtp_couple_build_q(one, nan_val, 16, &s));
|
||||
|
||||
/* Every candidate at -inf has no support at all. */
|
||||
float all_inf[8];
|
||||
for (int i = 0; i < 8; ++i) all_inf[i] = -INFINITY;
|
||||
CHECK(!rad_mtp_couple_build_q(one, all_inf, 16, &s));
|
||||
CHECK_EQ(s.count, 0);
|
||||
}
|
||||
|
||||
/* state_valid IS THE ONE RULE BOTH SIDES REFUSE A ROW BY. A status-0 row, a q[] that does not sum to
|
||||
* one, an id outside the vocabulary, x missing from the support, and a round outside the band are
|
||||
* all refused -- because a verifier that accepted any of them would couple against a law that is
|
||||
* not the draft's. */
|
||||
TEST(state_valid_refuses_every_malformed_row) {
|
||||
RadMtpCoupleState s = make_state({ 0, 1 }, { 0.0f, -0.2f }, 8, 5, 0, 2);
|
||||
CHECK(rad_mtp_couple_state_valid(s, 8));
|
||||
|
||||
RadMtpCoupleState bad = s;
|
||||
bad.status = 0;
|
||||
CHECK(!rad_mtp_couple_state_valid(bad, 8));
|
||||
|
||||
bad = s; bad.q[0] += 0.5; /* no longer sums to 1 */
|
||||
CHECK(!rad_mtp_couple_state_valid(bad, 8));
|
||||
|
||||
bad = s; bad.ids[0] = 99; /* outside the vocabulary */
|
||||
CHECK(!rad_mtp_couple_state_valid(bad, 8));
|
||||
|
||||
bad = s; bad.x = 6; /* x is not in the support */
|
||||
CHECK(!rad_mtp_couple_state_valid(bad, 8));
|
||||
|
||||
bad = s; bad.round = 0; /* outside the band */
|
||||
CHECK(!rad_mtp_couple_state_valid(bad, 8));
|
||||
bad = s; bad.round = RAD_MTP_COUPLE_MAX_ROUNDS + 1;
|
||||
CHECK(!rad_mtp_couple_state_valid(bad, 8));
|
||||
|
||||
bad = s; bad.draw.mode = 0; /* a greedy row's state has no Q */
|
||||
CHECK(!rad_mtp_couple_state_valid(bad, 8));
|
||||
|
||||
bad = s; bad.ids[1] = -1; /* a hole above count must carry no mass */
|
||||
CHECK(!rad_mtp_couple_state_valid(bad, 8) || s.count < 2);
|
||||
}
|
||||
|
||||
/* THE UNIFORM IS THE SPECIFIED DRAW, and it is 53-bit: two seeds that differ in one bit must give
|
||||
* different numbers, and the range must be [0, 1) with no value at 1. This is the number the
|
||||
* acceptance compares P(x) against, so a uniform that repeated would make two draft positions
|
||||
* accept together. */
|
||||
TEST(the_coupling_uniform_is_the_specified_53_bit_draw) {
|
||||
for (uint64_t pos = 0; pos < 32; ++pos) {
|
||||
const double u = rad_spechub_uniform(1234, pos, RAD_SAMPLE_KEY_ACCEPT);
|
||||
CHECK(u >= 0.0 && u < 1.0);
|
||||
/* 53 bits: a multiple of 2^-53, exactly. */
|
||||
CHECK_EQ(u, std::floor(u * 9007199254740992.0) / 9007199254740992.0);
|
||||
}
|
||||
CHECK(rad_spechub_uniform(1, 0, RAD_SAMPLE_KEY_ACCEPT) !=
|
||||
rad_spechub_uniform(2, 0, RAD_SAMPLE_KEY_ACCEPT));
|
||||
CHECK(rad_spechub_uniform(1, 0, RAD_SAMPLE_KEY_ACCEPT) !=
|
||||
rad_spechub_uniform(1, 1, RAD_SAMPLE_KEY_ACCEPT));
|
||||
CHECK(rad_spechub_uniform(1, 0, RAD_SAMPLE_KEY_ACCEPT) !=
|
||||
rad_spechub_uniform(1, 0, RAD_SAMPLE_KEY_RESID));
|
||||
}
|
||||
|
||||
/* THE WIRE ROWS ARE THE SIZE THE PLANES ARE ADDRESSED AT. A field inserted into either struct would
|
||||
* 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(RAD_MTP_COUPLE_DRAW_WORDS, (size_t)8);
|
||||
CHECK_EQ(RAD_MTP_COUPLE_STATE_WORDS, (size_t)38);
|
||||
CHECK_EQ(offsetof(RadMtpCoupleState, q) % 8, (size_t)0);
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
@@ -0,0 +1,122 @@
|
||||
/* spechub_wiring_test.cpp -- what the loader and the reference gate see of the spechub plugin.
|
||||
*
|
||||
* The three spechub_*_test binaries call the abi helpers directly; none of them loads the library
|
||||
* or asks how its rows are classified, so none of them could see the bug this test exists for: a
|
||||
* plugin that declared itself a reference library had EVERY row marked reference, including its
|
||||
* two device rows, and the engine's reference gate then refused to serve `mtp_couple_propose` /
|
||||
* `mtp_couple_verify` at bringup (RAD_E_NOKERNEL) unless --debug-accept-reference-kernels was
|
||||
* passed. The switch alone could not start a coupled run.
|
||||
*
|
||||
* The check is on the real DSO, loaded the way the loader loads it (dlopen, the exported symbols,
|
||||
* nothing else): the classification the gate consumes is exactly `RadKernelInfo::domain` and the
|
||||
* reference answer, so that is what is asserted here. A host build (no RAD_SPECHUB_WITH_DEVICE)
|
||||
* has only the oracle rows and must keep being classified as reference -- that is the half of the
|
||||
* rule that must NOT be relaxed, and it is asserted too.
|
||||
*/
|
||||
#include "rad_test.h"
|
||||
|
||||
#include "rad_abi.h"
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
struct Plugin {
|
||||
void* handle = nullptr;
|
||||
const RadPluginInfo* (*info)() = nullptr;
|
||||
int (*reference)() = nullptr;
|
||||
int (*kern_ref)(int) = nullptr;
|
||||
int (*count)() = nullptr;
|
||||
const RadKernelInfo* (*at)(int) = nullptr;
|
||||
|
||||
bool open(const char* path) {
|
||||
handle = dlopen(path, RTLD_NOW | RTLD_LOCAL);
|
||||
if (!handle) return false;
|
||||
info = (const RadPluginInfo* (*)())dlsym(handle, "rad_plugin_info");
|
||||
reference = (int (*)())dlsym(handle, "rad_plugin_reference");
|
||||
kern_ref = (int (*)(int))dlsym(handle, "rad_kernel_reference");
|
||||
count = (int (*)())dlsym(handle, "rad_kernel_count");
|
||||
at = (const RadKernelInfo* (*)(int))dlsym(handle, "rad_kernel_at");
|
||||
return info && reference && count && at;
|
||||
}
|
||||
~Plugin() { if (handle) dlclose(handle); }
|
||||
};
|
||||
|
||||
/* The rule rad_reference_ops applies, over the rows as the loader would classify them: an op is on
|
||||
* the reference when no row for it is a non-reference row. */
|
||||
std::vector<std::string> reference_only_ops(const Plugin& p, const std::vector<int>& is_ref) {
|
||||
std::map<std::string, bool> has_real;
|
||||
for (int i = 0; i < p.count(); ++i) {
|
||||
const RadKernelInfo* k = p.at(i);
|
||||
if (!k) continue;
|
||||
bool& real = has_real[k->op];
|
||||
real = real || !is_ref[(size_t)i];
|
||||
}
|
||||
std::vector<std::string> out;
|
||||
for (const auto& kv : has_real)
|
||||
if (!kv.second) out.push_back(kv.first);
|
||||
return out;
|
||||
}
|
||||
|
||||
} /* namespace */
|
||||
|
||||
TEST(spechub_wiring_the_device_rows_are_production_and_the_oracle_rows_are_reference) {
|
||||
Plugin p;
|
||||
REQUIRE(p.open(RAD_SPECHUB_LIB));
|
||||
|
||||
CHECK_EQ(p.info()->kind, (int)RAD_PLUGIN_KERNEL);
|
||||
CHECK_EQ(std::string(p.info()->name), std::string("spechub"));
|
||||
|
||||
/* The per-row answer must exist: without it the loader falls back to the library answer, which
|
||||
* is what mis-classified the device rows. */
|
||||
REQUIRE(p.kern_ref != nullptr);
|
||||
/* And the library as a whole is NOT a reference library -- otherwise the loader sorts it below
|
||||
* the kernel libraries it serves beside and every row inherits the flag. */
|
||||
CHECK_EQ(p.reference(), 0);
|
||||
|
||||
std::vector<int> is_ref;
|
||||
int dev = 0, host = 0;
|
||||
for (int i = 0; i < p.count(); ++i) {
|
||||
const RadKernelInfo* k = p.at(i);
|
||||
REQUIRE(k != nullptr);
|
||||
const int r = p.kern_ref(i);
|
||||
is_ref.push_back(r);
|
||||
if (k->domain == RAD_DOMAIN_DEVICE) {
|
||||
++dev;
|
||||
/* A device row is a kernel, never a reference: this is the line that decides whether a
|
||||
* coupled run may start at all. */
|
||||
CHECK_EQ(r, 0);
|
||||
CHECK(std::string(k->name).rfind("ref_", 0) != 0);
|
||||
} else {
|
||||
++host;
|
||||
CHECK_EQ(k->domain, (int)RAD_DOMAIN_HOST);
|
||||
/* The host oracle beside it keeps its reference status. */
|
||||
CHECK_EQ(r, 1);
|
||||
CHECK(std::string(k->name).rfind("ref_", 0) == 0);
|
||||
}
|
||||
}
|
||||
CHECK_EQ(dev + host, p.count());
|
||||
#ifdef RAD_SPECHUB_WITH_DEVICE
|
||||
CHECK_EQ(dev, 2);
|
||||
CHECK_EQ(host, 2);
|
||||
#else
|
||||
CHECK_EQ(dev, 0);
|
||||
CHECK_EQ(host, 2);
|
||||
#endif
|
||||
|
||||
/* Both ops must have at least one production row on a device build. This is the property the
|
||||
* gate reads: with it, the coupling is servable without --debug-accept-reference-kernels. */
|
||||
const std::vector<std::string> refonly = reference_only_ops(p, is_ref);
|
||||
#ifdef RAD_SPECHUB_WITH_DEVICE
|
||||
CHECK_EQ(refonly.size(), (size_t)0);
|
||||
#else
|
||||
/* No card: both ops are oracle-only, and the gate must still see them as such. */
|
||||
CHECK_EQ(refonly.size(), (size_t)2);
|
||||
#endif
|
||||
}
|
||||
|
||||
RAD_TEST_MAIN()
|
||||
Reference in new issue
Block a user