- SamplerParams moves to original_sampler_params.rs (pure) and is checked byte for byte against the Original struct (size, alignment, offsets, padding) and defaults. - validate() no longer rejects top_p == 0 (the Original accepts it). - Draft cell -1 (draft_first at position 0, counter 0) is accepted by coupled_draft_counter and OriginalSampler::sample_coupled. - set_subset keeps the first index and ignores out-of-range ids like setup_coupled. - Runtime penalty_rows / coupled helpers are compared with the Original header fixture; run_port_host_tests.sh runs these pure tests on macOS. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
508 lines
26 KiB
C++
508 lines
26 KiB
C++
// Golden-fixture generator for the Rust CPU sampler reference (crates/sampler-ref).
|
|
//
|
|
// WHAT THIS RUNS. The Original's own host-side logic, not a re-implementation:
|
|
// * `engine/strata/src/kernels/sampler_parity.cpp` is #included UNMODIFIED (its `main` renamed, a
|
|
// header-only stub standing in for <cuda_runtime.h>) so that `host_philox_uniform`,
|
|
// `sampled_reference`, `mirror_select`, `mirror_pick`, `sampled_k` are the Original's code;
|
|
// * `strata/kernels/sampler.hpp` (`penalty_rows`) and `strata/core/coupled_draft.hpp`
|
|
// (`coupled_draft_cell/counter`, `coupled_hist_len/start`, `coupled_hist_base`) are the Original's headers.
|
|
// The only code written here is the case generator, the JSON writer and `coupled_ref` below, which
|
|
// transcribes the DEVICE-ONLY coupled kernels (`coupled_penalize_kernel`, `coupled_merge_kernel`,
|
|
// `sampled_tail_warp<true>` in sampler.cu) because the Original has no host twin for them; it is
|
|
// cross-checked against the Original's `mirror_pick` on every case.
|
|
//
|
|
// Build/run: crates/sampler-ref/oracle/build_oracle.sh (compiles with -ffp-contract=off).
|
|
#define main original_sampler_parity_main
|
|
#include "../../../engine/strata/src/kernels/sampler_parity.cpp"
|
|
#undef main
|
|
|
|
#include "strata/core/coupled_draft.hpp"
|
|
|
|
#include <cinttypes>
|
|
#include <cstddef>
|
|
#include <new>
|
|
|
|
namespace {
|
|
|
|
using strata::kernels::SamplerParams;
|
|
|
|
// ---- deterministic case source (splitmix64; no std::random distributions, which differ per stdlib) ----
|
|
struct Rng {
|
|
uint64_t s;
|
|
uint64_t next() {
|
|
uint64_t z = (s += 0x9E3779B97F4A7C15ull);
|
|
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull;
|
|
z = (z ^ (z >> 27)) * 0x94D049BB133111EBull;
|
|
return z ^ (z >> 31);
|
|
}
|
|
double uni() { return (double) (next() >> 11) * (1.0 / 9007199254740992.0); }
|
|
int below(int n) { return (int) (next() % (uint64_t) n); }
|
|
double normal() { return (uni() + uni() + uni() + uni() - 2.0) * 1.7; }
|
|
template <class T> T pick(std::initializer_list<T> v) { return *(v.begin() + below((int) v.size())); }
|
|
};
|
|
|
|
uint32_t bits_of(float f) { uint32_t u; std::memcpy(&u, &f, 4); return u; }
|
|
float float_of(uint32_t u) { float f; std::memcpy(&f, &u, 4); return f; }
|
|
constexpr uint32_t kNaN = 0x7fc00000u, kNegNaN = 0xffc00000u, kInf = 0x7f800000u, kNegInf = 0xff800000u;
|
|
|
|
FILE* out = nullptr;
|
|
bool first_item = true;
|
|
void comma() { if (!first_item) std::fputs(",", out); first_item = false; }
|
|
|
|
void put_u32s(const std::vector<float>& v) {
|
|
std::fputc('[', out);
|
|
for (size_t i = 0; i < v.size(); ++i) std::fprintf(out, i ? ",%u" : "%u", bits_of(v[i]));
|
|
std::fputc(']', out);
|
|
}
|
|
void put_ints(const std::vector<int>& v) {
|
|
std::fputc('[', out);
|
|
for (size_t i = 0; i < v.size(); ++i) std::fprintf(out, i ? ",%d" : "%d", v[i]);
|
|
std::fputc(']', out);
|
|
}
|
|
void put_i32s(const std::vector<int32_t>& v) { put_ints(std::vector<int>(v.begin(), v.end())); }
|
|
void put_params(const SamplerParams& p) {
|
|
std::fprintf(out,
|
|
"{\"top_k\":%d,\"top_p\":%u,\"min_p\":%u,\"temperature\":%u,\"min_keep\":%d,\"penalty_last_n\":%d,"
|
|
"\"penalty_repeat\":%u,\"penalty_freq\":%u,\"penalty_present\":%u,\"seed\":%" PRIu64
|
|
",\"counter\":%" PRIu64 ",\"greedy\":%s}",
|
|
p.top_k, bits_of(p.top_p), bits_of(p.min_p), bits_of(p.temperature), p.min_keep, p.penalty_last_n,
|
|
bits_of(p.penalty_repeat), bits_of(p.penalty_freq), bits_of(p.penalty_present), p.seed, p.counter,
|
|
p.greedy ? "true" : "false");
|
|
}
|
|
|
|
std::vector<int> window_of(const std::vector<int>& hist, int penalty_last_n) {
|
|
// sampler.cu: hlen = min(penalty_last_n, history_len), clamped at 0; the window is the TAIL of the row
|
|
int hlen = std::min(penalty_last_n, (int) hist.size());
|
|
if (hlen < 0) hlen = 0;
|
|
return std::vector<int>(hist.end() - hlen, hist.end());
|
|
}
|
|
|
|
bool all_finite(const std::vector<float>& l) {
|
|
for (float x : l) if (!std::isfinite(x)) return false;
|
|
return true;
|
|
}
|
|
|
|
int mismatches = 0;
|
|
int n_cases = 0;
|
|
|
|
// One chain case: either a dense logit vector or a sparse full-vocabulary description.
|
|
struct SparseLogits {
|
|
int n = 0;
|
|
float fill = 0;
|
|
std::vector<std::pair<int, float>> set;
|
|
};
|
|
|
|
void emit_chain_case(const char* name, const std::vector<float>& logits, const SparseLogits* sparse,
|
|
const SamplerParams& p, int row, const std::vector<int>& hist) {
|
|
const int nv = (int) logits.size();
|
|
const std::vector<int> window = window_of(hist, p.penalty_last_n);
|
|
const int k = sampled_k(p.top_k, nv);
|
|
const bool greedy = p.greedy || p.temperature <= 0.0f;
|
|
// the Original's own semantics for the row: greedy = the first entry of the k=1 selection
|
|
const SelList sel1 = mirror_select(logits.data(), nv, window, p, 1);
|
|
const SelList sel = greedy ? sel1 : mirror_select(logits.data(), nv, window, p, k);
|
|
const int token = greedy ? sel1.ids[0] : mirror_pick(sel, k, p, row);
|
|
// cross-check the two Original references on rows where they must agree (finite, sampled)
|
|
if (!greedy && all_finite(logits)) {
|
|
const int ref = sampled_reference(logits, window, p, row);
|
|
if (ref != token) {
|
|
++mismatches;
|
|
std::fprintf(stderr, "reference disagreement in %s: mirror %d vs sampled_reference %d\n", name, token, ref);
|
|
}
|
|
}
|
|
comma();
|
|
std::fprintf(out, "\n{\"name\":\"%s\",\"n_vocab\":%d,\"row\":%d,\"params\":", name, nv, row);
|
|
put_params(p);
|
|
if (sparse) {
|
|
std::fprintf(out, ",\"sparse\":{\"fill\":%u,\"set\":[", bits_of(sparse->fill));
|
|
for (size_t i = 0; i < sparse->set.size(); ++i)
|
|
std::fprintf(out, i ? ",[%d,%u]" : "[%d,%u]", sparse->set[i].first, bits_of(sparse->set[i].second));
|
|
std::fputs("]}", out);
|
|
} else {
|
|
std::fputs(",\"logits\":", out);
|
|
put_u32s(logits);
|
|
}
|
|
std::fputs(",\"history\":", out);
|
|
put_ints(hist);
|
|
std::fprintf(out, ",\"expect\":{\"token\":%d,\"sel_ids\":", token);
|
|
put_ints(sel.ids);
|
|
std::fputs(",\"sel_logits\":", out);
|
|
put_u32s(sel.logit);
|
|
if (!greedy) std::fprintf(out, ",\"u\":%u", bits_of(host_philox_uniform(p.seed, p.counter + (uint64_t) row)));
|
|
std::fputs("}}", out);
|
|
++n_cases;
|
|
}
|
|
|
|
std::vector<float> make_logits(Rng& g, int nv, int mode) {
|
|
std::vector<float> l((size_t) nv);
|
|
for (int v = 0; v < nv; ++v) {
|
|
double x = g.normal();
|
|
switch (mode) {
|
|
case 0: l[(size_t) v] = (float) x; break; // plain
|
|
case 1: l[(size_t) v] = (float) (std::round(x * 2.0) / 2.0); break; // many exact ties
|
|
case 2: l[(size_t) v] = 1.25f; break; // all equal
|
|
case 3: { // non-finite mix
|
|
const int r = g.below(12);
|
|
l[(size_t) v] = r == 0 ? float_of(kNegInf) : r == 1 ? float_of(kNaN) : r == 2 ? float_of(kInf)
|
|
: r == 3 ? float_of(kNegNaN) : (float) x;
|
|
break;
|
|
}
|
|
case 4: l[(size_t) v] = (float) (x * 1e30); break; // huge magnitudes
|
|
case 5: l[(size_t) v] = (float) (x * 1e-40); break; // denormals
|
|
default: l[(size_t) v] = float_of(kNegInf); break; // all -inf
|
|
}
|
|
}
|
|
return l;
|
|
}
|
|
|
|
SamplerParams make_params(Rng& g, bool allow_greedy) {
|
|
SamplerParams p;
|
|
p.top_k = g.pick<int>({0, 1, 2, 5, 20, 20, 40, 63, 64, 65, 200, -3});
|
|
p.top_p = g.pick<float>({1.0f, 1.0f, 0.95f, 0.9f, 0.5f, 0.2f, 0.0f});
|
|
p.min_p = g.pick<float>({0.0f, 0.0f, 0.01f, 0.1f, 0.5f, 1.0f});
|
|
p.temperature = g.pick<float>({0.3f, 0.7f, 1.0f, 1.0f, 1.5f, 2.0f});
|
|
if (allow_greedy && g.below(6) == 0) p.temperature = g.pick<float>({0.0f, -1.0f});
|
|
p.greedy = allow_greedy && g.below(12) == 0;
|
|
p.min_keep = g.pick<int>({1, 1, 1, 2, 3});
|
|
p.penalty_last_n = g.pick<int>({0, 0, 4, 16, 64, 5000, -2});
|
|
p.penalty_repeat = g.pick<float>({1.0f, 1.1f, 1.5f, 2.0f, 0.8f});
|
|
p.penalty_freq = g.pick<float>({0.0f, 0.25f, 0.5f});
|
|
p.penalty_present = g.pick<float>({0.0f, 0.5f, 1.5f});
|
|
p.seed = g.pick<uint64_t>({0ull, 1ull, 42ull, 123456789ull, 0xFFFFFFFFull, 0x1FFFFFFFFull, 0xDEADBEEFCAFEF00Dull,
|
|
~0ull});
|
|
p.counter = g.pick<uint64_t>({0ull, 1ull, 7ull, 1000ull, (1ull << 32) + 7ull, 0xFFFFFFFFull, ~0ull - 3ull});
|
|
return p;
|
|
}
|
|
|
|
std::vector<int> make_history(Rng& g, int nv, int pln) {
|
|
int hl = g.pick<int>({0, 8, 32, 80});
|
|
if (pln > 0 && hl == 0) hl = 8;
|
|
std::vector<int> h((size_t) hl);
|
|
for (auto& t : h) {
|
|
const int r = g.below(10);
|
|
t = r < 2 ? -1 : r == 2 ? nv + g.below(3) : g.below(std::min(nv + 1, 12));
|
|
}
|
|
return h;
|
|
}
|
|
|
|
// ---- the coupled draft, serial (TRANSCRIPTION of the device-only kernels; see the file header) ----
|
|
struct CoupledResult { int id; float prob; int sub; };
|
|
|
|
CoupledResult coupled_ref(std::vector<float> l, const std::vector<int>& sub_to_id, const std::vector<int>& id_to_sub,
|
|
int id_vocab, const SamplerParams& params, std::vector<int>& ring, int cap, int j,
|
|
int step0, int& tail_pick_check) {
|
|
const int nv = (int) l.size();
|
|
// coupled_penalize_kernel
|
|
const int h = strata::core::coupled_hist_len(params.penalty_last_n, cap);
|
|
if (h > 0) {
|
|
const int* hrow = ring.data() + strata::core::coupled_hist_start(cap, j, h);
|
|
std::vector<char> seen((size_t) nv, 0);
|
|
for (int i = 0; i < h; ++i) {
|
|
const int v = hrow[i];
|
|
if (v < 0 || v >= id_vocab) continue;
|
|
const int s = id_to_sub.empty() ? v : id_to_sub[(size_t) v];
|
|
if (s < 0 || s >= nv) continue;
|
|
if (seen[(size_t) s]) continue;
|
|
seen[(size_t) s] = 1;
|
|
int c = 0;
|
|
for (int q = 0; q < h; ++q) c += hrow[q] == v;
|
|
float x = l[(size_t) s];
|
|
if (c > 0) {
|
|
if (x <= 0.0f) x *= params.penalty_repeat; else x /= params.penalty_repeat;
|
|
x -= (float) c * params.penalty_freq + (c > 0 ? 1.0f : 0.0f) * params.penalty_present;
|
|
}
|
|
l[(size_t) s] = x;
|
|
}
|
|
}
|
|
// coupled_merge_kernel
|
|
SamplerParams p = params;
|
|
p.counter = strata::core::coupled_draft_counter((int64_t) step0);
|
|
const int k = sampled_k(p.top_k, nv);
|
|
SamplerParams none; // the split-part launch passes SamplerParams{} (no penalties)
|
|
const SelList sel = mirror_select(l.data(), nv, std::vector<int>(), none, k);
|
|
int pick_idx = 0;
|
|
float prob = 1.0f;
|
|
if (p.greedy || p.temperature <= 0.0f) {
|
|
pick_idx = 0;
|
|
tail_pick_check = sel.ids[0];
|
|
} else {
|
|
// sampled_tail_warp<true>: top_p, min_p, temperature, draw, and the pick's probability
|
|
int n_keep = k;
|
|
float mx = sel.logit[0];
|
|
for (int i = 1; i < k; ++i) mx = std::fmax(mx, sel.logit[(size_t) i]);
|
|
if (p.top_p < 1.0f) {
|
|
double sum = 0.0;
|
|
for (int i = 0; i < k; ++i) sum += std::exp((double) sel.logit[(size_t) i] - (double) mx);
|
|
double cum = 0.0;
|
|
int cut = k;
|
|
for (int i = 0; i < k; ++i) {
|
|
cum += std::exp((double) sel.logit[(size_t) i] - (double) mx) / sum;
|
|
if (cum >= (double) p.top_p) { cut = i + 1; break; }
|
|
}
|
|
if (cut < p.min_keep) cut = p.min_keep < k ? p.min_keep : k;
|
|
n_keep = cut;
|
|
}
|
|
if (p.min_p > 0.0f) {
|
|
const float thresh = sel.logit[0] + std::log(p.min_p);
|
|
for (int i = 0; i < n_keep; ++i)
|
|
if (sel.logit[(size_t) i] < thresh) { n_keep = i; break; }
|
|
}
|
|
const float inv_t = p.temperature > 0.0f ? 1.0f / p.temperature : 0.0f;
|
|
float smx = sel.logit[0] * inv_t;
|
|
for (int i = 1; i < n_keep; ++i) smx = std::fmax(smx, sel.logit[(size_t) i] * inv_t);
|
|
std::vector<double> ex((size_t) std::max(n_keep, 1));
|
|
for (int i = 0; i < n_keep; ++i) ex[(size_t) i] = std::exp((double) (sel.logit[(size_t) i] * inv_t) - (double) smx);
|
|
double sum = 0.0;
|
|
for (int i = 0; i < n_keep; ++i) sum += ex[(size_t) i];
|
|
for (int i = 0; i < n_keep; ++i) ex[(size_t) i] = ex[(size_t) i] / sum;
|
|
const float u = host_philox_uniform(p.seed, p.counter + 0u);
|
|
double cum = 0.0;
|
|
pick_idx = n_keep > 0 ? n_keep - 1 : 0;
|
|
for (int i = 0; i < n_keep; ++i) {
|
|
cum += ex[(size_t) i];
|
|
if ((double) u < cum) { pick_idx = i; break; }
|
|
}
|
|
prob = n_keep > 0 ? (float) ex[(size_t) pick_idx] : 1.0f;
|
|
tail_pick_check = mirror_pick(sel, k, p, 0); // the Original's own tail must agree
|
|
}
|
|
const int s = sel.ids[(size_t) pick_idx];
|
|
const int id = sub_to_id.empty() ? s : sub_to_id[(size_t) s];
|
|
ring[(size_t) (cap + j)] = id;
|
|
return {id, prob, s};
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) { std::fprintf(stderr, "usage: %s out.json\n", argv[0]); return 2; }
|
|
out = std::fopen(argv[1], "w");
|
|
if (!out) { std::perror("open"); return 2; }
|
|
Rng g{0x5A17E7E5ull};
|
|
|
|
std::fputs("{\n\"meta\":{\"generator\":\"crates/sampler-ref/oracle/gen_fixtures.cpp\","
|
|
"\"oracle\":\"Original engine/strata sampler_parity.cpp host reference, included unmodified; "
|
|
"sampler.hpp + coupled_draft.hpp headers\","
|
|
"\"compile\":\"-std=c++17 -O1 -ffp-contract=off -fno-fast-math\","
|
|
"\"encoding\":\"floats are IEEE-754 binary32 bit patterns (u32)\"},\n", out);
|
|
|
|
// ---------------- philox ----------------
|
|
std::fputs("\"philox\":[", out);
|
|
first_item = true;
|
|
{
|
|
std::vector<std::pair<uint64_t, uint64_t>> pts = {
|
|
{0, 0}, {0, 1}, {1, 0}, {1, 1}, {42, 7}, {123, (1ull << 32) + 7}, {0xFFFFFFFFull, 0xFFFFFFFFull},
|
|
{0x100000000ull, 0}, {0, 0x100000000ull}, {~0ull, ~0ull}, {0xDEADBEEFCAFEF00Dull, 12345678901234ull}};
|
|
for (int i = 0; i < 80; ++i) pts.push_back({g.next(), g.next() >> (g.below(40))});
|
|
for (auto& pr : pts) {
|
|
comma();
|
|
std::fprintf(out, "\n[%" PRIu64 ",%" PRIu64 ",%u]", pr.first, pr.second,
|
|
bits_of(host_philox_uniform(pr.first, pr.second)));
|
|
}
|
|
}
|
|
std::fputs("\n],\n", out);
|
|
|
|
// ---------------- chain ----------------
|
|
std::fputs("\"chain\":[", out);
|
|
first_item = true;
|
|
{
|
|
int idx = 0;
|
|
char name[64];
|
|
// random dense rows over several vocabularies and logit shapes
|
|
for (int nv : {1, 2, 5, 16, 33, 64, 65, 100, 257})
|
|
for (int rep = 0; rep < (nv > 100 ? 2 : 14); ++rep) {
|
|
const int mode = rep % 7 == 6 ? 1 : (rep % 11 == 10 ? 2 : 0);
|
|
SamplerParams p = make_params(g, true);
|
|
const auto l = make_logits(g, nv, mode);
|
|
const auto h = make_history(g, nv, p.penalty_last_n);
|
|
std::snprintf(name, sizeof name, "rand-%d-%d", nv, rep);
|
|
emit_chain_case(name, l, nullptr, p, g.below(4), h);
|
|
++idx;
|
|
}
|
|
// edge shapes: non-finite mix, huge, denormal, all -inf, ties, k larger than the finite count
|
|
for (int mode : {3, 3, 3, 3, 4, 4, 5, 5, 6, 1, 1, 2})
|
|
for (int nv : {3, 12, 70}) {
|
|
SamplerParams p = make_params(g, true);
|
|
const auto l = make_logits(g, nv, mode);
|
|
const auto h = make_history(g, nv, p.penalty_last_n);
|
|
std::snprintf(name, sizeof name, "edge-m%d-%d-%d", mode, nv, idx++);
|
|
emit_chain_case(name, l, nullptr, p, g.below(3), h);
|
|
}
|
|
// penalties that matter: a clear head token repeatedly in the window
|
|
for (int rep = 0; rep < 16; ++rep) {
|
|
SamplerParams p = make_params(g, false);
|
|
p.penalty_last_n = 16;
|
|
p.penalty_repeat = g.pick<float>({1.5f, 2.0f, 3.0f});
|
|
p.penalty_freq = g.pick<float>({0.0f, 0.5f, 0.25f});
|
|
p.penalty_present = g.pick<float>({0.0f, 1.0f, 0.5f});
|
|
p.top_k = 8;
|
|
p.top_p = g.pick<float>({1.0f, 0.9f});
|
|
auto l = make_logits(g, 24, 0);
|
|
l[3] = 6.0f; l[9] = 5.5f; l[17] = -2.0f;
|
|
std::vector<int> h = {-1, -1, 3, 3, 3, 9, 17, 17, 40, -1, 3, 9, 17, 5, 5, 3};
|
|
std::snprintf(name, sizeof name, "penal-%d", rep);
|
|
emit_chain_case(name, l, nullptr, p, rep % 3, h);
|
|
}
|
|
// top_p lands EXACTLY on a cumulative boundary (equal logits: cum = 1/n, 2/n, ...), so `cum >= top_p` vs `>` is
|
|
// observable; 16 seeds per case so the draw reaches the entry that only the wrong comparison would keep
|
|
for (int n : {4, 8})
|
|
for (int step = 1; step < n; ++step)
|
|
for (int sd = 0; sd < 6; ++sd) {
|
|
SamplerParams p;
|
|
p.top_k = n; p.top_p = (float) step / (float) n; p.temperature = 1.0f; p.min_p = 0.0f;
|
|
p.seed = 1000 + (uint64_t) sd * 7919; p.counter = (uint64_t) sd;
|
|
std::vector<float> l((size_t) n + 2, 3.0f);
|
|
l[(size_t) n] = -20.0f; l[(size_t) n + 1] = -20.0f;
|
|
std::snprintf(name, sizeof name, "tp-boundary-%d-%d-%d", n, step, sd);
|
|
emit_chain_case(name, l, nullptr, p, 0, std::vector<int>());
|
|
}
|
|
// full-vocabulary sparse rows (the closed GPU op's geometry: 248,320 logits, up to 4,096 history slots)
|
|
for (int rep = 0; rep < 36; ++rep) {
|
|
SparseLogits sp;
|
|
sp.n = 248320;
|
|
sp.fill = g.pick<float>({-5.0f, -9.5f, 0.0f});
|
|
std::vector<float> l((size_t) sp.n, sp.fill);
|
|
const int n_set = 6 + g.below(120);
|
|
for (int i = 0; i < n_set; ++i) {
|
|
const int at = g.below(sp.n);
|
|
const float val = (float) (g.normal() * 3.0 + 2.0);
|
|
sp.set.push_back({at, val});
|
|
l[(size_t) at] = val;
|
|
}
|
|
if (rep % 4 == 1)
|
|
for (int i = 0; i < 8; ++i) { sp.set.push_back({1000 + 7 * i, 4.5f}); l[(size_t) (1000 + 7 * i)] = 4.5f; } // ties
|
|
if (rep % 13 == 12) { sp.set.push_back({5, float_of(kNegInf)}); l[5] = float_of(kNegInf); }
|
|
if (rep % 17 == 16) { sp.set.push_back({248319, float_of(kNaN)}); l[248319] = float_of(kNaN); } // last index: NaN is never picked
|
|
SamplerParams p = make_params(g, rep % 5 == 4);
|
|
if (rep % 3 == 0) { p.penalty_last_n = 64; }
|
|
std::vector<int> h(rep == 2 ? 4096 : 64, -1);
|
|
for (size_t i = h.size() > 40 ? h.size() - 40 : 0; i < h.size(); ++i)
|
|
h[i] = sp.set.empty() ? 0 : sp.set[(size_t) g.below((int) sp.set.size())].first;
|
|
if (rep == 2) p.penalty_last_n = 4096;
|
|
if (p.penalty_last_n > 0 && h.empty()) h.assign(8, -1);
|
|
std::snprintf(name, sizeof name, "full-%d", rep);
|
|
emit_chain_case(name, l, &sp, p, 0, h);
|
|
}
|
|
}
|
|
std::fputs("\n],\n", out);
|
|
|
|
// ---------------- ABI of strata::kernels::SamplerParams (the 64-byte blob every Original kernel takes by value) ----------------
|
|
std::fprintf(out, "\"abi\":{\"size\":%zu,\"align\":%zu,\"offsets\":{", sizeof(SamplerParams), alignof(SamplerParams));
|
|
std::fprintf(out, "\"top_k\":%zu,\"top_p\":%zu,\"min_p\":%zu,\"temperature\":%zu,\"min_keep\":%zu,\"penalty_last_n\":%zu,"
|
|
"\"penalty_repeat\":%zu,\"penalty_freq\":%zu,\"penalty_present\":%zu,\"seed\":%zu,\"counter\":%zu,\"greedy\":%zu},\"samples\":[",
|
|
offsetof(SamplerParams, top_k), offsetof(SamplerParams, top_p), offsetof(SamplerParams, min_p),
|
|
offsetof(SamplerParams, temperature), offsetof(SamplerParams, min_keep), offsetof(SamplerParams, penalty_last_n),
|
|
offsetof(SamplerParams, penalty_repeat), offsetof(SamplerParams, penalty_freq), offsetof(SamplerParams, penalty_present),
|
|
offsetof(SamplerParams, seed), offsetof(SamplerParams, counter), offsetof(SamplerParams, greedy));
|
|
first_item = true;
|
|
for (int rep = 0; rep < 12; ++rep) {
|
|
SamplerParams p = rep == 0 ? SamplerParams() : make_params(g, true);
|
|
alignas(SamplerParams) unsigned char raw[sizeof(SamplerParams)];
|
|
std::memset(raw, 0, sizeof raw);
|
|
SamplerParams* q = new (raw) SamplerParams(); // default member initialisers, padding left as the memset made it
|
|
q->top_k = p.top_k; q->top_p = p.top_p; q->min_p = p.min_p; q->temperature = p.temperature; q->min_keep = p.min_keep;
|
|
q->penalty_last_n = p.penalty_last_n; q->penalty_repeat = p.penalty_repeat; q->penalty_freq = p.penalty_freq;
|
|
q->penalty_present = p.penalty_present; q->seed = p.seed; q->counter = p.counter; q->greedy = p.greedy;
|
|
comma();
|
|
std::fputs("\n{\"params\":", out); put_params(p);
|
|
std::fputs(",\"bytes\":[", out);
|
|
for (size_t i = 0; i < sizeof raw; ++i) std::fprintf(out, i ? ",%u" : "%u", (unsigned) raw[i]);
|
|
std::fputs("]}", out);
|
|
}
|
|
std::fputs("\n]},\n", out);
|
|
|
|
// ---------------- counters (coupled_draft.hpp, verbatim header functions) ----------------
|
|
std::fputs("\"counters\":{\"cells\":[", out);
|
|
first_item = true;
|
|
for (int64_t p : {0ll, 1ll, 100ll, 4095ll, 1000000ll, (long long) 1 << 40})
|
|
for (int a : {0, 1, 7})
|
|
for (int j : {0, 1, 6}) {
|
|
comma();
|
|
const int64_t cell = strata::core::coupled_draft_cell(p, a, j);
|
|
std::fprintf(out, "\n[%" PRId64 ",%d,%d,%" PRId64 ",%" PRIu64 "]", p, a, j, cell,
|
|
strata::core::coupled_draft_counter(cell));
|
|
}
|
|
std::fputs("\n],\"hist_len\":[", out);
|
|
first_item = true;
|
|
for (int cap : {8, 64, strata::core::kCoupledHistCap})
|
|
for (int pln : {-1, 0, 1, 7, 8, 9, 64, 4095, 4096, 4097, 100000}) {
|
|
comma();
|
|
std::fprintf(out, "\n[%d,%d,%d]", pln, cap, strata::core::coupled_hist_len(pln, cap));
|
|
}
|
|
std::fputs("\n],\"hist_start\":[", out);
|
|
first_item = true;
|
|
for (int cap : {8, 64, strata::core::kCoupledHistCap})
|
|
for (int h : {1, 3, 8})
|
|
for (int j : {0, 1, 6}) {
|
|
comma();
|
|
std::fprintf(out, "\n[%d,%d,%d,%d]", cap, j, h, strata::core::coupled_hist_start(cap, j, h));
|
|
}
|
|
std::fputs("\n]},\n", out);
|
|
|
|
// ---------------- penalty_rows + coupled_hist_base (sampler.hpp / coupled_draft.hpp, verbatim) ----------------
|
|
std::fputs("\"penalty_rows\":[", out);
|
|
first_item = true;
|
|
for (int rep = 0; rep < 60; ++rep) {
|
|
const int n_tail = g.pick<int>({0, 1, 2, 5, 7, 20});
|
|
const int T = 1 + g.below(8);
|
|
const int h = g.pick<int>({1, 3, 8, 16});
|
|
std::vector<int32_t> tail((size_t) n_tail), window((size_t) T), rows((size_t) T * (size_t) h);
|
|
for (auto& t : tail) t = g.below(1000);
|
|
for (auto& t : window) t = g.below(1000);
|
|
strata::kernels::penalty_rows(tail.data(), n_tail, window.data(), T, h, rows.data());
|
|
comma();
|
|
std::fputs("\n{\"tail\":", out); put_i32s(tail);
|
|
std::fputs(",\"window\":", out); put_i32s(window);
|
|
std::fprintf(out, ",\"h\":%d,\"rows\":", h); put_i32s(rows);
|
|
const int32_t next = g.below(1000);
|
|
std::vector<int32_t> base((size_t) h, 777777);
|
|
strata::core::coupled_hist_base(tail.data(), n_tail, next, h, base.data());
|
|
std::fprintf(out, ",\"next\":%d,\"base\":", next); put_i32s(base);
|
|
std::fputs("}", out);
|
|
}
|
|
std::fputs("\n],\n", out);
|
|
|
|
// ---------------- coupled draft sampling (transcribed kernels, cross-checked against mirror_pick) ----------------
|
|
std::fputs("\"coupled\":[", out);
|
|
first_item = true;
|
|
for (int rep = 0; rep < 60; ++rep) {
|
|
const int nv = g.pick<int>({7, 24, 70, 130});
|
|
const bool subset = rep % 3 == 1;
|
|
const int id_vocab = subset ? nv * 3 : nv;
|
|
std::vector<int> sub_to_id, id_to_sub;
|
|
if (subset) {
|
|
id_to_sub.assign((size_t) id_vocab, -1);
|
|
for (int s = 0; s < nv; ++s) { // subset s -> token id 3 s + 1 (an injective map)
|
|
sub_to_id.push_back(3 * s + 1);
|
|
id_to_sub[(size_t) (3 * s + 1)] = s;
|
|
}
|
|
}
|
|
SamplerParams p = make_params(g, false);
|
|
p.penalty_last_n = g.pick<int>({0, 3, 8, 12, 20});
|
|
auto l = make_logits(g, nv, rep % 9 == 8 ? 1 : 0);
|
|
const int cap = g.pick<int>({16, 32});
|
|
const int j = g.below(4);
|
|
std::vector<int> ring((size_t) cap + 8, -1);
|
|
for (int i = 0; i < cap + j; ++i)
|
|
ring[(size_t) i] = g.below(5) == 0 ? -1 : (subset ? 3 * g.below(std::min(nv, 6)) + 1 : g.below(std::min(nv, 6)));
|
|
if (rep % 7 == 3) for (int i = 0; i < cap + j; ++i) if (g.below(4) == 0) ring[(size_t) i] = id_vocab + 1; // out of vocab
|
|
const int step0 = g.below(100000);
|
|
const std::vector<int> ring_in = ring;
|
|
int check = -1;
|
|
const CoupledResult r = coupled_ref(l, sub_to_id, id_to_sub, id_vocab, p, ring, cap, j, step0, check);
|
|
if (check != r.sub) { ++mismatches; std::fprintf(stderr, "coupled tail disagreement in case %d\n", rep); }
|
|
comma();
|
|
std::fprintf(out, "\n{\"cap\":%d,\"j\":%d,\"step0\":%d,\"id_vocab\":%d,\"logits\":", cap, j, step0, id_vocab);
|
|
put_u32s(l);
|
|
std::fputs(",\"params\":", out); put_params(p);
|
|
std::fputs(",\"sub_to_id\":", out); put_ints(sub_to_id);
|
|
std::fputs(",\"id_to_sub\":", out); put_ints(id_to_sub);
|
|
std::fputs(",\"ring\":", out); put_ints(ring_in);
|
|
std::fprintf(out, ",\"expect\":{\"id\":%d,\"sub\":%d,\"prob\":%u}}", r.id, r.sub, bits_of(r.prob));
|
|
++n_cases;
|
|
}
|
|
std::fputs("\n]\n}\n", out);
|
|
std::fclose(out);
|
|
std::fprintf(stderr, "wrote %d cases, %d oracle disagreements\n", n_cases, mismatches);
|
|
return mismatches == 0 ? 0 : 3;
|
|
}
|