From 9adfaa42e5dbb03a5ff7a8a4020243484c83a06f Mon Sep 17 00:00:00 2001 From: Jakub Luwierski Date: Tue, 29 Sep 2026 15:40:29 +0200 Subject: [PATCH] indexer: the batched append takes the one rope config, with a parity test The rebase onto 0.1.24 had brought the QSA indexer's C-2 chunk append, whose kernels rotated the pooled keys inline - cosf/sinf of the unscaled angle, no mscale - so a scaled prefill would have written indexer keys under a different rotation than the per-token append the decode path uses (rope_scaling.hpp's one-rotation rule, broken silently). The batch path now resolves freq_scale/corr_dims/mscale from the same RopeScaling the single append reads and rotates through the shared rope_scaled_angle; the raw freq_base parameter is gone from its signature. qsa_parity pins the contract the C-2 rework claimed: the batch append's whole end state (pooled, spare, tail ring, block_pos) is BIT-IDENTICAL to the sequential single appends', under none and yarn 2 alike, and the scaling is observable in the batched keys. --- src/kernels/qsa_parity.cpp | 69 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/src/kernels/qsa_parity.cpp b/src/kernels/qsa_parity.cpp index 5a8186dd..50558362 100644 --- a/src/kernels/qsa_parity.cpp +++ b/src/kernels/qsa_parity.cpp @@ -47,6 +47,7 @@ // 1e-3 for FP16 paths. That difference is the FP16 CACHE's cost, not the kernel's, and it is kept // separate so a kernel bug cannot hide inside it. #include "strata/kernels/qsa.hpp" +#include "strata/kernels/native_qsa_indexer.hpp" #include "strata/kernels/f16_bits.hpp" #include "strata/kernels/rope.hpp" @@ -1376,6 +1377,74 @@ int main(int argc, char** argv) { check(cudaGraphDestroy(g2), "gd"); } + // ================= the NATIVE indexer's batched append vs the sequential one ================= + // The prefill path appends a chunk in one native_qsa_indexer_append_batch call, the decode path appends + // token by token - and one cache must never be able to tell which path filled it (rope_scaling.hpp's + // one-rotation rule). So the batch's whole end state must be BIT-IDENTICAL to the sequential appends', + // under the two extremes of the scaling config: none (today's rotation) and yarn factor 2, whose mscale + // and corr-dims both kernels must take from the same RopeScaling. Eight cells: exactly two completed + // blocks, so the paths' end states agree on block_pos too (a mid-block chunk end leaves the sequential + // path pointing at the open block's base and the batch at the last completed one's - upstream C-2's own + // semantics, the same for every scaling). + { + std::printf("\n-- the native indexer's batched append vs the sequential one\n"); + const int64_t NB = 8; // exactly two complete blocks of r=4 + const int32_t BASE = 100; // positions 100..107, not cell indices 0..7 + const int64_t MC = 1024; // the capacity both sides validate against + std::vector raws((size_t) NB * IDXD); + for (int64_t t = 0; t < NB; ++t) + for (int64_t d = 0; d < IDXD; ++d) + raws[(size_t) t * IDXD + d] = (float) (gauss(rng) * std::pow(4.0, (double) (t % 4))); + cudaStream_t cs = nullptr; + check(cudaStreamCreate(&cs), "cs"); + std::vector none_pooled; + for (int variant = 0; variant < 2; ++variant) { + strata::kernels::RopeScaling sc; + if (variant == 1) { sc.type = strata::kernels::RopeScalingType::YaRN; sc.factor = 2.0; sc.ext_factor = 1.0; } + const size_t prows = (size_t) (MC / R + 1) * IDXD, trows = (size_t) (R - 1) * IDXD; + Dev pooledA(prows), deadA(IDXD), tailA(trows), pooledB(prows), deadB(IDXD), tailB(trows); + Dev bposA(1), bposB(1), dpos(1); + for (Dev* dp : {&pooledA, &deadA, &tailA, &pooledB, &deadB, &tailB}) + check(cudaMemset(dp->p, 0, dp == &pooledA || dp == &pooledB ? prows * 4 + : dp == &deadA || dp == &deadB ? IDXD * 4 : trows * 4), "zero"); + check(cudaMemset(bposA.p, 0, 4), "zero"); check(cudaMemset(bposB.p, 0, 4), "zero"); + Dev draw((size_t) IDXD), drawAll; + drawAll.put(raws); + strata::kernels::QsaIndexerBuffers bufsA{tailA.p, deadA.p, pooledA.p, bposA.p}; + strata::kernels::QsaIndexerBuffers bufsB{tailB.p, deadB.p, pooledB.p, bposB.p}; + for (int64_t t = 0; t < NB; ++t) { // the sequential side: one cell, its device position + check(cudaMemcpy(draw.p, &raws[(size_t) t * IDXD], (size_t) IDXD * 4, cudaMemcpyHostToDevice), "raw"); + const int32_t tp = (int32_t) t; + check(cudaMemcpy(dpos.p, &tp, 4, cudaMemcpyHostToDevice), "pos"); + strata::kernels::native_qsa_indexer_append(draw.p, dpos.p, BASE, dw_kn.p, EPS, bufsA, S, MC, sc, cs); + } + strata::kernels::native_qsa_indexer_append_batch(drawAll.p, NB, 0, BASE, dw_kn.p, EPS, bufsB, S, MC, sc, cs); + check(cudaStreamSynchronize(cs), "sync"); + const std::vector pa = pooledA.get(prows), pb = pooledB.get(prows); + const std::vector da = deadA.get((size_t) IDXD), db = deadB.get((size_t) IDXD); + const std::vector ta = tailA.get(trows), tbb = tailB.get(trows); + const std::vector ba = bposA.get(1), bb = bposB.get(1); + long long bad = 0; + for (size_t i = 0; i < pa.size(); ++i) bad += pa[i] != pb[i]; + for (size_t i = 0; i < da.size(); ++i) bad += da[i] != db[i]; + for (size_t i = 0; i < ta.size(); ++i) bad += ta[i] != tbb[i]; + bad += ba[0] != bb[0]; + std::printf(" %-44s %s (%lld of %zu state values differ)\n", + variant == 0 ? "batch vs sequential, none" : "batch vs sequential, yarn 2", + bad ? "*** WRONG ***" : "bit-identical", bad, pa.size() + da.size() + ta.size() + 1); + if (bad) ++g_bad; + if (variant == 0) none_pooled = pa; + else { + int differ = 0; + for (size_t i = 0; i < pa.size(); ++i) differ += pa[i] != none_pooled[i]; + std::printf(" %-44s %d of %zu pooled values moved\n", "the scaling is visible in the batched keys", + differ, pa.size()); + if (!differ) { std::printf(" *** the batched append ignored its scaling ***\n"); ++g_bad; } + } + } + 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");