From e12ecf6ac87cda5b97f785e7b8ddc198f99eed4f Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 1 Oct 2026 14:34:20 +0200 Subject: [PATCH] #280 follow-up: the rope kernels without the table are 0.1.31's code exactly (template ) With STRATA_ROPE_TABLE unset the table was skipped at run time, yet the compiled default kernels changed their results: the model-level parity run (Swift IQ2_XS, tools/conversation_cache_parity.py, STRATA_STATE_HASH) gave the same tokens but other GDN / indexer / K/V state hashes than 0.1.31's (bisected to #280). native_rope's apply, the indexer's append / append_first / append_blocks and the prompt path's rope_kernel are templates on TAB now; the host launches whenever no table applies, and that instantiation has no table read at all. Checked: the same tokens and every state hash as round 1 with the whole branch. --- src/kernels/cuda/native_qsa_indexer.cu | 47 +++++++++++++++++++------- src/kernels/cuda/native_rope.cu | 18 +++++++--- src/prefill/kernels.cu | 19 ++++++++--- 3 files changed, 62 insertions(+), 22 deletions(-) diff --git a/src/kernels/cuda/native_qsa_indexer.cu b/src/kernels/cuda/native_qsa_indexer.cu index b91fdbd5..8147c709 100644 --- a/src/kernels/cuda/native_qsa_indexer.cu +++ b/src/kernels/cuda/native_qsa_indexer.cu @@ -39,6 +39,10 @@ __device__ float warp_sum(float x) { x += __shfl_xor_sync(0xffffffffu, x, offset); return x; } +// TAB (#280, STRATA_ROPE_TABLE=1): the angles from the session's float64 table. The host launches whenever +// no table applies - the default - so the default kernel is 0.1.31's code exactly (the table read is not in it; +// with it merely skipped at run time, the compiled default path changed its results). +template __global__ void append(const float* __restrict__ raw, const int32_t* __restrict__ pos_dev, int pos_base, const float* __restrict__ gamma, float epsilon, float* __restrict__ tail, float* __restrict__ dead, @@ -88,7 +92,7 @@ __global__ void append(const float* __restrict__ raw, const int32_t* __restrict_ // The spare (pos == 0) keeps its zero angle; under YaRN the mscale magnitude still rides in // through cos(0) - which is exactly what the queries are scaled by, so the top-k is unmoved. float c, s; - if (!rope_tab_cs(rt, pos == 0 ? 0 : mrope_pos(mtab, rope_pos, pair), pair, c, s)) { + if (!(TAB && rope_tab_cs(rt, pos == 0 ? 0 : mrope_pos(mtab, rope_pos, pair), pair, c, s))) { const float theta_extrap = (pos == 0 ? 0 : mrope_pos(mtab, rope_pos, pair)) * powf(theta_scale, float(pair)); rope_scaled_angle(theta_extrap, freq_scale, corr_low, corr_high, ext_factor, mscale, pair, c, s); } @@ -105,6 +109,7 @@ __global__ void append(const float* __restrict__ raw, const int32_t* __restrict_ // the RMS norm, gamma, the rotation at pos_base + 4b. `k(j)` is the key of the block's cell j: a row of this batch // (cell >= p0) or the tail the previous batch left (cell < p0). Only the batch's last completed block writes the // spare after it (every earlier one's is overwritten by the next block in order). +template __device__ __forceinline__ float pooled_value(const float* values, int d, int rope_pos, float theta_scale, float freq_scale, float corr_low, float corr_high, float ext_factor, float mscale, const int32_t* mtab, bool zero_pos, @@ -116,7 +121,7 @@ __device__ __forceinline__ float pooled_value(const float* values, int d, int ro // today's cosf/sinf; under linear/YaRN the pooled keys move with the cache's K, and the spare's zero // angle carries the mscale magnitude through cos(0) exactly as the single append's does. float c, s; - if (!rope_tab_cs(rt, zero_pos ? 0 : mrope_pos(mtab, rope_pos, pair), pair, c, s)) { + if (!(TAB && rope_tab_cs(rt, zero_pos ? 0 : mrope_pos(mtab, rope_pos, pair), pair, c, s))) { const float theta_extrap = (zero_pos ? 0 : mrope_pos(mtab, rope_pos, pair)) * powf(theta_scale, float(pair)); rope_scaled_angle(theta_extrap, freq_scale, corr_low, corr_high, ext_factor, mscale, pair, c, s); } @@ -136,6 +141,7 @@ __device__ __forceinline__ float norm_scale(float mean, float* partials, int d) return square_sum; } // cell 0 of a sequence: the spare (every gather index names cell 0), written to pooled[0] and dead +template __global__ void append_first(const float* __restrict__ raw, const float* __restrict__ gamma, float epsilon, float* __restrict__ dead, float* __restrict__ pooled, float theta_scale, float freq_scale, float corr_low, float corr_high, float ext_factor, float mscale, @@ -156,11 +162,12 @@ __global__ void append_first(const float* __restrict__ raw, const float* __restr if (d < D) values[d] = scale * mean * gamma[d]; __syncthreads(); if (d >= D) return; - const float y = pooled_value(values, d, 0, theta_scale, freq_scale, corr_low, corr_high, ext_factor, mscale, + const float y = pooled_value(values, d, 0, theta_scale, freq_scale, corr_low, corr_high, ext_factor, mscale, mtab, true, rt); pooled[d] = y; dead[d] = y; } +template __global__ void append_blocks(const float* __restrict__ raw, int64_t n, int64_t p0, int pos_base, const float* __restrict__ gamma, float epsilon, const float* __restrict__ tail, const float* __restrict__ dead, float* __restrict__ pooled, int32_t* __restrict__ block_pos, @@ -188,7 +195,7 @@ __global__ void append_blocks(const float* __restrict__ raw, int64_t n, int64_t __syncthreads(); if (d >= D) return; const int rope_pos = pos_base + R * (int) b; - pooled[std::size_t(b) * D + d] = pooled_value(values, d, rope_pos, theta_scale, freq_scale, corr_low, corr_high, + pooled[std::size_t(b) * D + d] = pooled_value(values, d, rope_pos, theta_scale, freq_scale, corr_low, corr_high, ext_factor, mscale, mtab, false, rt); if (b == last_block) { pooled[std::size_t(b + 1) * D + d] = dead[d]; @@ -233,9 +240,15 @@ void native_qsa_indexer_append(const float* raw, const int32_t* relative_pos_dev if (overlaps(spans[i], spans[j])) throw std::invalid_argument("native QSA indexer buffers overlap"); const float theta_scale = powf((float) scaling.freq_base, -2.0f / ROT); const RopeKernelArgs k = scaling.kernel_args(ROT); // none: the identity constants - append<<<1,THREADS,0,static_cast(stream)>>>(raw,relative_pos_device,pos_base,gamma,epsilon, - b.tail,b.dead,b.pooled,b.block_pos,int(max_cells),theta_scale,k.freq_scale, - k.corr_low,k.corr_high,k.ext_factor,k.attn_factor,mrope_table(),rope_table_for(scaling)); + const RopeTab rt = rope_table_for(scaling); + if (rt.cos != nullptr) + append<<<1,THREADS,0,static_cast(stream)>>>(raw,relative_pos_device,pos_base,gamma,epsilon, + b.tail,b.dead,b.pooled,b.block_pos,int(max_cells),theta_scale,k.freq_scale, + k.corr_low,k.corr_high,k.ext_factor,k.attn_factor,mrope_table(),rt); + else + append<<<1,THREADS,0,static_cast(stream)>>>(raw,relative_pos_device,pos_base,gamma,epsilon, + b.tail,b.dead,b.pooled,b.block_pos,int(max_cells),theta_scale,k.freq_scale, + k.corr_low,k.corr_high,k.ext_factor,k.attn_factor,mrope_table(),rt); const auto error = cudaGetLastError(); if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error)); } @@ -253,15 +266,23 @@ void native_qsa_indexer_append_batch(const float* raw, int64_t n, int64_t p0, in const cudaStream_t st = static_cast(stream); const int32_t* mtab = mrope_table(); const RopeTab rt = rope_table_for(scaling); - if (p0 == 0) append_first<<<1, THREADS, 0, st>>>(raw, gamma, epsilon, b.dead, b.pooled, theta_scale, - fsf, cl, ch, ef, ms, mtab, rt); + if (p0 == 0) { + if (rt.cos != nullptr) + append_first<<<1, THREADS, 0, st>>>(raw, gamma, epsilon, b.dead, b.pooled, theta_scale, + fsf, cl, ch, ef, ms, mtab, rt); + else + append_first<<<1, THREADS, 0, st>>>(raw, gamma, epsilon, b.dead, b.pooled, theta_scale, + fsf, cl, ch, ef, ms, mtab, rt); + } // completed blocks: those whose last cell (4b+3) lies in [p0, p0 + n) const int64_t first = p0 <= R - 1 ? 0 : (p0 - (R - 1) + R - 1) / R; // the smallest b with 4b+3 >= p0 const int64_t hi = p0 + n - 1 >= R - 1 ? (p0 + n - 1 - (R - 1)) / R : -1; // the largest b with 4b+3 <= p0+n-1 - if (hi >= first) - append_blocks<<<(unsigned) (hi - first + 1), THREADS, 0, st>>>(raw, n, p0, pos_base, gamma, epsilon, b.tail, - b.dead, b.pooled, b.block_pos, first, hi, - theta_scale, fsf, cl, ch, ef, ms, mtab, rt); + if (hi >= first && rt.cos != nullptr) + append_blocks<<<(unsigned) (hi - first + 1), THREADS, 0, st>>>(raw, n, p0, pos_base, gamma, epsilon, + b.tail, b.dead, b.pooled, b.block_pos, first, hi, theta_scale, fsf, cl, ch, ef, ms, mtab, rt); + else if (hi >= first) + append_blocks<<<(unsigned) (hi - first + 1), THREADS, 0, st>>>(raw, n, p0, pos_base, gamma, epsilon, + b.tail, b.dead, b.pooled, b.block_pos, first, hi, theta_scale, fsf, cl, ch, ef, ms, mtab, rt); append_tail<<>>(raw, n, p0, b.tail); const auto error = cudaGetLastError(); if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error)); diff --git a/src/kernels/cuda/native_rope.cu b/src/kernels/cuda/native_rope.cu index b34f4e67..57cbe79f 100644 --- a/src/kernels/cuda/native_rope.cu +++ b/src/kernels/cuda/native_rope.cu @@ -39,6 +39,10 @@ bool overlaps(const void* a, size_t an, const void* b, size_t bn) { auto x = reinterpret_cast(a), y = reinterpret_cast(b); return x <= y ? y - x < an : x - y < bn; } +// TAB (#280, STRATA_ROPE_TABLE=1): the angles from the session's float64 table. The host launches whenever +// no table applies - the default - so the default kernel is 0.1.31's code exactly (the table read is not in it; +// with it merely skipped at run time, the compiled default path changed its results). +template __global__ void apply(const float* x, float* out, int rows, int width, int n_rot, float theta_scale, float freq_scale, float corr_low, float corr_high, float ext_factor, float mscale, const int* positions, const int32_t* mtab, RopeTab rt) { @@ -54,7 +58,7 @@ __global__ void apply(const float* x, float* out, int rows, int width, return; } float c, s; - if (!rope_tab_cs(rt, mrope_pos(mtab, positions[row], pair), pair, c, s)) { + if (!(TAB && rope_tab_cs(rt, mrope_pos(mtab, positions[row], pair), pair, c, s))) { const float theta_extrap = mrope_pos(mtab, positions[row], pair) * powf(theta_scale, float(pair)); rope_scaled_angle(theta_extrap, freq_scale, corr_low, corr_high, ext_factor, mscale, pair, c, s); } @@ -129,10 +133,14 @@ void native_rope_apply(const float* x, float* out, int rows, int head_dim, // Match pinned host-side float powf before device fast powf/trigonometry. const float theta_scale = powf((float) scaling.freq_base, -2.0f / n_rot); const RopeKernelArgs k = scaling.kernel_args(n_rot); // none: the identity constants - apply<<(stream)>>>(x, out, rows, head_dim, n_rot, theta_scale, - k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, - k.attn_factor, positions, mrope_table(), rope_table_for(scaling)); + const RopeTab rt = rope_table_for(scaling); + const dim3 grid((head_dim / 2 + 127) / 128, rows); + if (rt.cos != nullptr) + apply<<(stream)>>>(x, out, rows, head_dim, n_rot, theta_scale, + k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor, positions, mrope_table(), rt); + else + apply<<(stream)>>>(x, out, rows, head_dim, n_rot, theta_scale, + k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor, positions, mrope_table(), rt); const auto error = cudaGetLastError(); if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error)); } diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cu index 0035833b..de833442 100644 --- a/src/prefill/kernels.cu +++ b/src/prefill/kernels.cu @@ -559,6 +559,10 @@ __global__ void rms_rows_kernel(float* __restrict__ x, const float* __restrict__ __syncthreads(); for (int64_t c = threadIdx.x; c < cols; c += blockDim.x) r[c] = s * r[c] * w[c]; } +// TAB (#280, STRATA_ROPE_TABLE=1): the angles from the session's float64 table. The host launches whenever +// no table applies - the default - so the default kernel is 0.1.31's code exactly (the table read is not in it; +// with it merely skipped at run time, the compiled default path changed its results). +template __global__ void rope_kernel(float* __restrict__ x, int64_t heads, int64_t dim, int64_t ld, int64_t pos0, float theta_scale, float freq_scale, float corr_low, float corr_high, float ext_factor, float mscale, const int32_t* __restrict__ mtab, @@ -568,7 +572,8 @@ __global__ void rope_kernel(float* __restrict__ x, int64_t heads, int64_t dim, i const int64_t t = row / heads, h = row % heads; float* p = x + t * ld + h * dim; float c, s; - if (!strata::kernels::rope_tab_cs(rt, strata::kernels::mrope_pos(mtab, (int) (pos0 + t), pair), pair, c, s)) { + if (!(TAB && strata::kernels::rope_tab_cs(rt, strata::kernels::mrope_pos(mtab, (int) (pos0 + t), pair), pair, c, + s))) { const float theta_extrap = (float) strata::kernels::mrope_pos(mtab, (int) (pos0 + t), pair) * powf(theta_scale, (float) pair); strata::kernels::rope_scaled_angle(theta_extrap, freq_scale, corr_low, corr_high, ext_factor, mscale, @@ -814,9 +819,15 @@ void rope(float* x, int64_t T, int64_t heads, int64_t dim, int64_t ld, int64_t p } const float theta_scale = powf((float) scaling.freq_base, -2.0f / 64.0f); const strata::kernels::RopeKernelArgs k = scaling.kernel_args(64); // none: the identity constants - rope_kernel<<<(unsigned) (T * heads), 32, 0, (cudaStream_t) stream>>>( - x, heads, dim, ld, pos0, theta_scale, k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor, - strata::kernels::mrope_table(), strata::kernels::rope_table_for(scaling)); + const strata::kernels::RopeTab rt = strata::kernels::rope_table_for(scaling); + if (rt.cos != nullptr) + rope_kernel<<<(unsigned) (T * heads), 32, 0, (cudaStream_t) stream>>>( + x, heads, dim, ld, pos0, theta_scale, k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor, + strata::kernels::mrope_table(), rt); + else + rope_kernel<<<(unsigned) (T * heads), 32, 0, (cudaStream_t) stream>>>( + x, heads, dim, ld, pos0, theta_scale, k.freq_scale, k.corr_low, k.corr_high, k.ext_factor, k.attn_factor, + strata::kernels::mrope_table(), rt); check("rope"); } void split_q(const float* q_full, float* q, int64_t T, void* stream) {