The source-port runtime no longer builds on macOS, so the pure arithmetic and its
golden tests live in a platform-independent crate shared by the driver and runtime.
leleka_runtime::{rope_scaling,memory_plan,context_policy} stay as re-exports.
geometry.rs derives every frozen 262144-cell byte literal from the cell count.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
197 lines
10 KiB
C++
197 lines
10 KiB
C++
// gen_rope_golden.cpp - golden-fixture generator for the Rust rope port (leleka-runtime::rope_scaling).
|
|
//
|
|
// It links the ORIGINAL host-side code: `include/strata/kernels/rope_scaling.hpp` is included unmodified, and the
|
|
// two `build_rope_table` definitions are extracted VERBATIM from `src/kernels/cuda/rope.cu` by `gen_rope_golden.sh`
|
|
// into a generated include (`rope_table_original.inc`). Nothing from the Original is re-typed here.
|
|
//
|
|
// Every double/float is printed as its IEEE bit pattern in hex so the fixture is exact (serde_json's default
|
|
// float parser is not guaranteed correctly rounded). Tables are summarised by FNV-1a 64 over the little-endian
|
|
// f32 bytes of the whole cos table and of the whole sin table, plus full rows at selected positions.
|
|
#include "strata/kernels/rope_scaling.hpp"
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace strata::kernels {
|
|
// the engine's other definitions live in the .cu next to the extracted functions; only the table builders are needed
|
|
void build_rope_table(int n_rot, double theta, int max_pos, float* cos_tab, float* sin_tab);
|
|
void build_rope_table(int n_rot, const RopeScaling& sc, int max_pos, float* cos_tab, float* sin_tab);
|
|
} // namespace strata::kernels
|
|
|
|
namespace strata::kernels {
|
|
#include "rope_table_original.inc"
|
|
} // namespace strata::kernels
|
|
|
|
using namespace strata::kernels;
|
|
|
|
static std::string hex64(double d) {
|
|
uint64_t u;
|
|
std::memcpy(&u, &d, 8);
|
|
char b[32];
|
|
std::snprintf(b, sizeof b, "0x%016llx", (unsigned long long) u);
|
|
return b;
|
|
}
|
|
static std::string hex32(float f) {
|
|
uint32_t u;
|
|
std::memcpy(&u, &f, 4);
|
|
char b[32];
|
|
std::snprintf(b, sizeof b, "0x%08x", u);
|
|
return b;
|
|
}
|
|
static uint64_t fnv(const float* p, size_t n) {
|
|
uint64_t h = 0xcbf29ce484222325ull;
|
|
for (size_t i = 0; i < n; ++i) {
|
|
uint32_t u;
|
|
std::memcpy(&u, p + i, 4);
|
|
for (int k = 0; k < 4; ++k) {
|
|
h ^= (uint8_t) (u >> (8 * k));
|
|
h *= 0x100000001b3ull;
|
|
}
|
|
}
|
|
return h;
|
|
}
|
|
|
|
struct Case {
|
|
const char* name;
|
|
const char* desc;
|
|
int n_rot;
|
|
RopeScaling sc;
|
|
bool tables; // build the 524288-position tables (skipped for invalid configs)
|
|
};
|
|
|
|
static RopeScaling mk(RopeScalingType t, double fb, double factor, double fsi, double orig, double ext, double attn,
|
|
double bf, double bs) {
|
|
RopeScaling s;
|
|
s.type = t;
|
|
s.freq_base = fb;
|
|
s.factor = factor;
|
|
s.freq_scale_in = fsi;
|
|
s.orig_ctx = orig;
|
|
s.ext_factor = ext;
|
|
s.attn_factor = attn;
|
|
s.beta_fast = bf;
|
|
s.beta_slow = bs;
|
|
return s;
|
|
}
|
|
|
|
int main() {
|
|
using T = RopeScalingType;
|
|
const double B = 1e7, O = 262144;
|
|
std::vector<Case> cases = {
|
|
{"none_default", "default-constructed RopeScaling (today's engine)", 64, RopeScaling{}, true},
|
|
{"none_junk_knobs", "type none with every scaling knob set: knobs must be inert", 64,
|
|
mk(T::None, B, 4.0, 0.25, 4096, 1.0, 0.5, 20, 2), true},
|
|
{"linear_2", "linear factor 2 (512K over 262144)", 64, mk(T::Linear, B, 2.0, 0, O, 0, 1.0, 32, 1), true},
|
|
{"linear_1_5", "linear factor 1.5 (393216 over 262144)", 64, mk(T::Linear, B, 1.5, 0, O, 0, 1.0, 32, 1), true},
|
|
{"linear_freq_scale_in", "linear with the raw ggml freq_scale 0.25 overriding 1/factor", 64,
|
|
mk(T::Linear, B, 2.0, 0.25, O, 0, 1.0, 32, 1), true},
|
|
{"linear_attn_factor", "linear with attn_factor 0.9 and no correction: mscale = attn_factor", 64,
|
|
mk(T::Linear, B, 2.0, 0, O, 0, 0.9, 32, 1), true},
|
|
{"yarn_2", "yarn factor 2 (setup.py default for 512K), ext 1, betas 32/1", 64,
|
|
mk(T::YaRN, B, 2.0, 0, O, 1.0, 1.0, 32, 1), true},
|
|
{"yarn_1_5", "yarn factor 1.5 (setup.py default for 393216)", 64,
|
|
mk(T::YaRN, B, 1.5, 0, O, 1.0, 1.0, 32, 1), true},
|
|
{"yarn_1", "yarn factor 1 (inside trained range): ramp active, mscale 1", 64,
|
|
mk(T::YaRN, B, 1.0, 0, O, 1.0, 1.0, 32, 1), true},
|
|
{"yarn_4", "yarn factor 4 (1M context)", 64, mk(T::YaRN, B, 4.0, 0, O, 1.0, 1.0, 32, 1), true},
|
|
{"yarn_2_betas", "yarn factor 2 with attn 1.1, betas 16/2", 64, mk(T::YaRN, B, 2.0, 0, O, 1.0, 1.1, 16, 2), true},
|
|
{"yarn_2_ext_half", "yarn factor 2 with ext_factor 0.5", 64, mk(T::YaRN, B, 2.0, 0, O, 0.5, 1.0, 32, 1), true},
|
|
{"yarn_orig_131072", "yarn factor 4 against a 131072 trained context", 64,
|
|
mk(T::YaRN, B, 4.0, 0, 131072, 1.0, 1.0, 32, 1), true},
|
|
{"yarn_ext0", "yarn type with ext_factor 0: correction off, behaves as linear", 64,
|
|
mk(T::YaRN, B, 2.0, 0, O, 0.0, 1.0, 32, 1), true},
|
|
{"yarn_n_rot128", "yarn n_rot 128, base 1e6, orig 4096, factor 8", 128,
|
|
mk(T::YaRN, 1e6, 8.0, 0, 4096, 1.0, 1.0, 32, 1), true},
|
|
{"yarn_n_rot4", "yarn n_rot 4: corr_dims clamps to n_rot-1", 4, mk(T::YaRN, B, 2.0, 0, O, 1.0, 1.0, 32, 1), true},
|
|
// invalid (finite) configurations: only the validity message and the scalar resolutions are recorded
|
|
{"bad_factor_below_1", "factor 0.5", 64, mk(T::Linear, B, 0.5, 0, O, 0, 1.0, 32, 1), false},
|
|
{"bad_freq_base_1", "freq_base 1 (none)", 64, mk(T::None, 1.0, 1.0, 0, O, 0, 1.0, 32, 1), false},
|
|
{"bad_orig_ctx_0", "orig_ctx 0", 64, mk(T::YaRN, B, 2.0, 0, 0.0, 1.0, 1.0, 32, 1), false},
|
|
{"bad_ext_negative", "ext_factor -1", 64, mk(T::YaRN, B, 2.0, 0, O, -1.0, 1.0, 32, 1), false},
|
|
{"bad_attn_0", "attn_factor 0", 64, mk(T::Linear, B, 2.0, 0, O, 0, 0.0, 32, 1), false},
|
|
{"bad_beta_fast_0", "beta_fast 0", 64, mk(T::YaRN, B, 2.0, 0, O, 1.0, 1.0, 0, 1), false},
|
|
{"bad_beta_slow_neg", "beta_slow -1", 64, mk(T::YaRN, B, 2.0, 0, O, 1.0, 1.0, 32, -1), false},
|
|
{"bad_freq_scale_in_neg", "freq_scale_in -0.1", 64, mk(T::Linear, B, 2.0, -0.1, O, 0, 1.0, 32, 1), false},
|
|
{"bad_mscale_negative", "freq_scale_in 1e6 with ext 1: the log term drives mscale negative", 64,
|
|
mk(T::YaRN, B, 2.0, 1e6, O, 1.0, 1.0, 32, 1), false},
|
|
};
|
|
|
|
const int NMAX = 524288;
|
|
const int NS[2] = {262144, 524288};
|
|
const int SAMPLE_POS[] = {0, 1, 2, 4095, 4096, 65535, 131071, 262143, 262144, 524287};
|
|
const int AN_POS[] = {1, 4096, 262143, 524287};
|
|
std::printf("{\n \"generator\": \"gen_rope_golden.cpp over rope_scaling.hpp + rope.cu build_rope_table (verbatim)\",\n");
|
|
std::printf(" \"cases\": [\n");
|
|
std::vector<float> cs, sn;
|
|
for (size_t ci = 0; ci < cases.size(); ++ci) {
|
|
const Case& c = cases[ci];
|
|
const RopeScaling& s = c.sc;
|
|
const int half = c.n_rot / 2;
|
|
std::printf(" {\n \"name\": \"%s\", \"desc\": \"%s\", \"n_rot\": %d,\n", c.name, c.desc, c.n_rot);
|
|
const char* tn = s.type == T::YaRN ? "yarn" : s.type == T::Linear ? "linear" : "none";
|
|
std::printf(" \"type\": \"%s\",\n", tn);
|
|
std::printf(" \"freq_base\": \"%s\", \"factor\": \"%s\", \"freq_scale_in\": \"%s\", \"orig_ctx\": \"%s\",\n",
|
|
hex64(s.freq_base).c_str(), hex64(s.factor).c_str(), hex64(s.freq_scale_in).c_str(),
|
|
hex64(s.orig_ctx).c_str());
|
|
std::printf(" \"ext_factor\": \"%s\", \"attn_factor\": \"%s\", \"beta_fast\": \"%s\", \"beta_slow\": \"%s\",\n",
|
|
hex64(s.ext_factor).c_str(), hex64(s.attn_factor).c_str(), hex64(s.beta_fast).c_str(),
|
|
hex64(s.beta_slow).c_str());
|
|
const char* bad = rope_scaling_invalid(s);
|
|
if (bad) std::printf(" \"invalid\": \"%s\",\n", bad);
|
|
else std::printf(" \"invalid\": null,\n");
|
|
std::printf(" \"freq_scale\": \"%s\", \"mscale\": \"%s\",\n", hex64(s.freq_scale()).c_str(),
|
|
hex64(s.mscale()).c_str());
|
|
double cd[2];
|
|
s.corr_dims(c.n_rot, cd);
|
|
std::printf(" \"corr_dims\": [\"%s\", \"%s\"],\n", hex64(cd[0]).c_str(), hex64(cd[1]).c_str());
|
|
const RopeKernelArgs a = s.kernel_args(c.n_rot);
|
|
std::printf(" \"kernel_args\": {\"freq_scale\": \"%s\", \"corr_low\": \"%s\", \"corr_high\": \"%s\", "
|
|
"\"ext_factor\": \"%s\", \"attn_factor\": \"%s\"},\n",
|
|
hex32(a.freq_scale).c_str(), hex32(a.corr_low).c_str(), hex32(a.corr_high).c_str(),
|
|
hex32(a.ext_factor).c_str(), hex32(a.attn_factor).c_str());
|
|
// analytic path (float32 rope_scaled_angle): theta_extrap = (float)(p * inv_i)
|
|
std::printf(" \"analytic\": [");
|
|
bool first = true;
|
|
const int pairs[] = {0, 1, 7, 15, 16, 31};
|
|
for (int pair : pairs) {
|
|
if (pair >= half) continue;
|
|
for (int p : AN_POS) {
|
|
const double inv = std::pow(s.freq_base, -2.0 * (double) pair / (double) c.n_rot);
|
|
const float te = (float) ((double) p * inv);
|
|
float co, si;
|
|
rope_scaled_angle(te, a.freq_scale, a.corr_low, a.corr_high, a.ext_factor, a.attn_factor, pair, co, si);
|
|
std::printf("%s\n {\"theta_extrap\": \"%s\", \"pair\": %d, \"cos\": \"%s\", \"sin\": \"%s\"}",
|
|
first ? "" : ",", hex32(te).c_str(), pair, hex32(co).c_str(), hex32(si).c_str());
|
|
first = false;
|
|
}
|
|
}
|
|
std::printf("\n ]");
|
|
if (c.tables) {
|
|
cs.assign((size_t) NMAX * half, 0.f);
|
|
sn.assign((size_t) NMAX * half, 0.f);
|
|
build_rope_table(c.n_rot, s, NMAX, cs.data(), sn.data());
|
|
std::printf(",\n \"tables\": [");
|
|
for (int k = 0; k < 2; ++k)
|
|
std::printf("%s\n {\"n_pos\": %d, \"cos_fnv1a64\": \"0x%016llx\", \"sin_fnv1a64\": \"0x%016llx\"}",
|
|
k ? "," : "", NS[k], (unsigned long long) fnv(cs.data(), (size_t) NS[k] * half),
|
|
(unsigned long long) fnv(sn.data(), (size_t) NS[k] * half));
|
|
std::printf("\n ],\n \"rows\": [");
|
|
for (size_t k = 0; k < sizeof SAMPLE_POS / sizeof *SAMPLE_POS; ++k) {
|
|
const int p = SAMPLE_POS[k];
|
|
std::printf("%s\n {\"pos\": %d, \"cos\": [", k ? "," : "", p);
|
|
for (int i = 0; i < half; ++i) std::printf("%s\"%s\"", i ? "," : "", hex32(cs[(size_t) p * half + i]).c_str());
|
|
std::printf("], \"sin\": [");
|
|
for (int i = 0; i < half; ++i) std::printf("%s\"%s\"", i ? "," : "", hex32(sn[(size_t) p * half + i]).c_str());
|
|
std::printf("]}");
|
|
}
|
|
std::printf("\n ]");
|
|
}
|
|
std::printf("\n }%s\n", ci + 1 < cases.size() ? "," : "");
|
|
}
|
|
std::printf(" ]\n}\n");
|
|
return 0;
|
|
}
|