This repository has been archived on 2026-11-24. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
radiance-sycl/tests/spechub_wiring_test.cpp
T
mici81andClaude Opus 5.5 cb0abe9ac7 dflash2: lossless coupled acceptance at T > 0 (--draft-coupling on)
DFlash2's drafts were the greedy walk over its candidate lattice, accepted
only when they equal the target's own sampled draw: P(y == x) instead of
rejection sampling's sum min(P, Q). --draft-coupling already did the latter
for a linear MTP head; this extends it to the block drafter.

- dflash_couple_propose (spechub plugin, device row + host oracle): after
  dflash_select, the same lattice walked again, sampled. Position l scores
  each candidate as dflash_select does (unary + low-rank edge from the
  previous token), Q_l = FP64 softmax of score / q_temp over the best eight
  in-vocabulary candidates, x_l ~ Q_l at (seed, base, KEY_DRAFT + l + 1),
  x_l is the next predecessor. Position l writes round l+1's state row, so
  the engine binds it and the sampler's mtp_couple_verify (maximal coupling,
  residual (P - Q)+) verifies it exactly as an MTP round. Q depends on the
  committed text and on x_0..x_{l-1}, never on the verify, and is stored
  exactly. Greedy requests keep dflash_select's tokens (status 0); a draw
  for another card or a Q that cannot be built takes the walk's argmax and
  the ordinary verify (status -1). The shared arithmetic is in
  abi/rad_spechub.h (rad_dflash_couple_step / _walk).
- The block driver stages one draw per drafting row before the query pass,
  reads the states back with the slots and binds them after the commit
  (Engine::bind_couple, shared with the serial driver).
- qwen35_fp8 declares the planes and the op under --draft-coupling;
  RADIANCE_DFLASH_Q_TEMP sets Q's temperature (default 1, exact at any).
- Default (flag off): no op, no plane, no change.

Gates:
- spechub_dflash_couple_test (CPU): emitted token vs P by chi-square over 3
  seeds each, Fisher-combined p 0.135..0.919, for Q = P, Q on fewer tokens
  than P, Q mostly outside P, top_k 20 + top_p 0.95, top_p 0.6, top_k 3 at
  T 0.7, peaked Q; acceptance = sum min(P, Q) within 5 sigma; the walk's
  stored Q equals the softmax of its scores (1e-5) and x follows it (p
  0.993); the whole draft/verify/bonus loop over 3 tokens matches the
  target's autoregressive law (combined p 0.62 at q_temp 1, 0.67 at 0.5,
  0.024 with every 2nd step on the ordinary verify).
- Device row vs host oracle, 40 random lattices at the production geometry
  (V 248320, K 16, R 256, steps 7/3, M 1..16): tokens, x, ids, status
  identical; q differs only by FP64 exp ulps (<= 4.3e-16 relative).
  91 us a launch at M 16, steps 7.
- Engine, Qwen3.8-27B MXFP4 TP2, our serving flags, 4-6 tokens of two chat
  prompts at T 1 / top_p 0.95 / top_k 20, 5000-11000 samples per arm:
  coupled vs ordinary speculation chi-square p 0.23..0.81 (tokens 1-4 and
  pair 1-2); both differ from no speculation on one prompt by the same
  amount (the verify-vs-decode logits, not the rule).
- Flag off, old build (up12 5dbb5dc) vs this: greedy top-5 logprob traces
  identical over 300 tokens (spec 7 and 0, two requests each), 6/6
  fixed-seed sampled texts identical.
- ctest (the environment-only tests excluded) 105/105.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-27 18:37:28 +01:00

116 lines
4.2 KiB
C++

/* spechub_wiring_test.cpp -- what the loader and the reference gate see of the spechub plugin.
*
* The device rows must be production rows and the host rows reference rows; were the whole
* library marked reference, the reference gate would refuse the coupled ops at bringup. The check
* loads the real DSO as the loader does (dlopen and the exported symbols) and asserts the domain
* and the reference answer per row. A host build has only the reference rows and must stay
* classified as such.
*/
#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, 3);
CHECK_EQ(host, 3);
#else
CHECK_EQ(dev, 0);
CHECK_EQ(host, 3);
#endif
/* Every op 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: every op is oracle-only, and the gate must still see them as such. */
CHECK_EQ(refonly.size(), (size_t)3);
#endif
}
RAD_TEST_MAIN()