From d4526cf156f1b7df2e5c63b8fb24e982cbdbb6de Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Wed, 30 Sep 2026 16:44:48 +0200 Subject: [PATCH] qsa: the table indexer's spare rotates at position 0 like the native one (TODO 7) indexer_key_append_kernel skipped the rotation at pos 0, which is only an identity while row 0 of the table is (1, 0). Under YaRN row 0 is (mscale, 0), so dead and pooled[0] stayed the only unscaled keys of a scaled cache and disagreed with the native indexer's spare. The rotation at angle 0 reduces to v * cos_tab[pair] on the rotated dims (the sine is 0 in every scaling), so no partner read and no barrier; unscaled it is v * 1.0f, bit for bit the old value. qsa_parity checks the table spare against the unscaled spare times row 0 (bit-exact), pooled[0] == dead, the native spare's factor, and that YaRN's mscale is observable there. --- src/kernels/cuda/qsa.cu | 16 +++++-- src/kernels/qsa_parity.cpp | 94 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 4 deletions(-) diff --git a/src/kernels/cuda/qsa.cu b/src/kernels/cuda/qsa.cu index f783164e..d1338969 100644 --- a/src/kernels/cuda/qsa.cu +++ b/src/kernels/cuda/qsa.cu @@ -184,10 +184,18 @@ __global__ void indexer_key_append_kernel(const float* __restrict__ raw, const i ss = __dadd_rn(ss, __dmul_rn(v, v)); } const double inv = __ddiv_rn(1.0, __dsqrt_rn(ss / (double) idx_dim + (double) eps)); - dead[d] = (float) (p * inv * (double) w_k_norm[d]); - // rope at position 0 is the identity here (cos = 1, sin = 0 exactly), so no rotation is applied and - // the parity test asserts the value is bit-exact - which is what makes skipping it legitimate. - pooled[d] = dead[d]; + float y = (float) (p * inv * (double) w_k_norm[d]); + // The spare rotates at position 0 - ALWAYS row 0 of the table, whatever `pos_base` is, as the native + // indexer's spare does. At angle 0 the sine is exactly 0 in every scaling (sin(0) * mscale), so the + // pair rotation reduces to `v * cos_tab[pair]` with no partner element to read (and no barrier). + // Unscaled, row 0 is exactly (1, 0) and `v * 1.0f` is `v` bit for bit - today's value, which the + // parity test holds bit-exact. Under YaRN row 0 is (mscale, 0): the rotated dims carry the magnitude + // correction exactly like every other pooled row and like the native kernel's spare (the unrotated + // dims pass through, as in the rotation of the completed blocks below). Skipping the rotation here, + // as this kernel once did, left `dead` and `pooled[0]` the only unscaled keys of a scaled cache. + if (d < n_rot) y *= cos_tab[d % (n_rot / 2)]; + dead[d] = y; + pooled[d] = y; } if (slot != r - 1) return; diff --git a/src/kernels/qsa_parity.cpp b/src/kernels/qsa_parity.cpp index 98495c44..55dec2d3 100644 --- a/src/kernels/qsa_parity.cpp +++ b/src/kernels/qsa_parity.cpp @@ -1454,6 +1454,100 @@ int main(int argc, char** argv) { check(cudaStreamDestroy(cs), "csd"); } + // ================= the spare (position 0) under rope scaling: the table indexer vs the native one ========= + // The spare key (`dead`, and `pooled[0]` until the first block completes) rotates at angle 0. Its sine is + // 0 in every scaling, so the rotation is `v * cos_tab[0][pair]`: exactly `v` unscaled (row 0 is (1, 0), the + // bit-exact value section 2 asserts), `v * mscale` under YaRN - the magnitude every other pooled row and + // the native kernel's spare carry. The table kernel used to skip the rotation at position 0, which left + // the spare the one unscaled key of a scaled cache. Asserted per variant: the table spare is BIT-EXACTLY + // the unscaled spare times row 0 on the rotated dims and the unscaled spare past them; `pooled[0]` equals + // `dead`; the native spare carries the same factor (to f32 rounding: its mscale is formed in float); and + // under YaRN the factor is observable (row 0 is not 1). + { + std::printf("\n-- the spare key (position 0) under rope scaling: table indexer vs native indexer\n"); + using RST = strata::kernels::RopeScalingType; + struct Variant { const char* name; RST type; double factor; double ext; }; + const Variant variants[] = {{"none", RST::None, 1.0, 0.0}, + {"linear 2", RST::Linear, 2.0, 0.0}, + {"yarn 2", RST::YaRN, 2.0, 1.0}}; + const int64_t MC = 64, HALF = S.n_rot / 2, NROT = S.n_rot; + std::vector raw0((size_t) IDXD); + for (int64_t d = 0; d < IDXD; ++d) raw0[(size_t) d] = rnd(3.0); + Dev draw0; + draw0.put(raw0); + Dev dpos0(1); + const int32_t zero = 0; + check(cudaMemcpy(dpos0.p, &zero, 4, cudaMemcpyHostToDevice), "pos0"); + cudaStream_t cs = nullptr; + check(cudaStreamCreate(&cs), "cs"); + const size_t prows = (size_t) (MC / R + 1) * IDXD, trows = (size_t) (R - 1) * IDXD; + // one cell at position 0 through either kernel; returns (dead, pooled[0]) + auto spare = [&](bool native, const strata::kernels::RopeScaling& sc, std::vector& dead, + std::vector& pooled0) { + Dev pooled(prows), dd((size_t) IDXD), tail(trows); + Dev bpos(1); + check(cudaMemset(pooled.p, 0, prows * 4), "zero"); + check(cudaMemset(dd.p, 0, (size_t) IDXD * 4), "zero"); + check(cudaMemset(tail.p, 0, trows * 4), "zero"); + check(cudaMemset(bpos.p, 0, 4), "zero"); + strata::kernels::QsaIndexerBuffers bufs{tail.p, dd.p, pooled.p, bpos.p}; + if (native) { + strata::kernels::native_qsa_indexer_append(draw0.p, dpos0.p, 0, dw_kn.p, EPS, bufs, S, MC, sc, cs); + } else { + std::vector tc((size_t) (MC * HALF)), ts(tc.size()); + strata::kernels::build_rope_table((int) NROT, sc, (int) MC, tc.data(), ts.data()); + Dev dc, dsn; + dc.put(tc); + dsn.put(ts); + strata::kernels::indexer_key_append(draw0.p, dpos0.p, 0, dw_kn.p, EPS, bufs, S, dc.p, dsn.p, cs); + } + check(cudaStreamSynchronize(cs), "sync"); + dead = dd.get((size_t) IDXD); + pooled0 = pooled.get((size_t) IDXD); + }; + std::vector t_none, t_none_p, n_none, n_none_p; + for (const Variant& var : variants) { + strata::kernels::RopeScaling sc; + sc.type = var.type; + sc.factor = var.factor; + sc.ext_factor = var.ext; + std::vector row0c((size_t) (MC * HALF)), row0s(row0c.size()); + strata::kernels::build_rope_table((int) NROT, sc, (int) MC, row0c.data(), row0s.data()); + std::vector td, tp, nd, np; + spare(false, sc, td, tp); + spare(true, sc, nd, np); + if (var.type == RST::None) { t_none = td; t_none_p = tp; n_none = nd; n_none_p = np; } + long long tbad = 0, pbad = 0, sin_bad = 0; + double nworst = 0; + for (int64_t i = 0; i < HALF; ++i) sin_bad += row0s[(size_t) i] != 0.0f; + for (int64_t d = 0; d < IDXD; ++d) { + const float c0 = d < NROT ? row0c[(size_t) (d % HALF)] : 1.0f; + const float want = d < NROT ? t_none[(size_t) d] * c0 : t_none[(size_t) d]; + tbad += std::memcmp(&td[(size_t) d], &want, 4) != 0; + pbad += std::memcmp(&tp[(size_t) d], &td[(size_t) d], 4) != 0; + const double nwant = (double) n_none[(size_t) d] * (double) c0; + const double den = std::max(std::fabs(nwant), 1e-6); + nworst = std::max(nworst, std::fabs((double) nd[(size_t) d] - nwant) / den); + } + require(std::string(" ") + var.name + ": row 0 of the table is (c, 0)", sin_bad == 0, + std::to_string(sin_bad) + " nonzero sines"); + require(std::string(" ") + var.name + ": the table spare is the unscaled spare times row 0, BIT-EXACT", + tbad == 0, std::to_string(tbad) + " of " + std::to_string(IDXD) + " wrong"); + require(std::string(" ") + var.name + ": the table's pooled[0] is its spare", pbad == 0, + std::to_string(pbad) + " wrong"); + require(std::string(" ") + var.name + ": the native spare carries the same row-0 factor", nworst <= 4e-7, + "worst relative " + std::to_string(nworst)); + if (var.type == RST::YaRN) { + int moved = 0; + for (int64_t d = 0; d < NROT; ++d) moved += std::memcmp(&td[(size_t) d], &t_none[(size_t) d], 4) != 0; + require(" yarn 2: the magnitude correction is observable on the table spare", + row0c[0] != 1.0f && moved > NROT / 2, + "row0 cos " + std::to_string(row0c[0]) + ", " + std::to_string(moved) + " dims moved"); + } + } + check(cudaStreamDestroy(cs), "csd"); + } + std::printf("\nqsa: %d failures\n", g_bad); if (g_bad) return 1; if (selftest) std::printf("qsa_parity OK\n");