Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.
C++
RoPE: every kernel reads the session's float64 angle table
sergiywith16a369b5744a
diff --git a/include/strata/kernels/mrope.hpp b/include/strata/kernels/mrope.hppindex fb00bf412..8f456e43a 100644--- a/include/strata/kernels/mrope.hpp+++ b/include/strata/kernels/mrope.hpp@@ -16,18 +16,43 @@#include <cstdint>+#include "strata/kernels/rope_scaling.hpp"+namespace strata::kernels {/// The table of the CURRENT device (a layer split sets one per device; null = the identity).void mrope_table_set(const int32_t* device_table);const int32_t* mrope_table();+/// The rotation angles, 32 pairs of a 64-wide rotary slice: the session's float64 table (build_rope_table,+/// [max_pos][32] cos and sin in VRAM, the rope scaling inside) of the CURRENT device. With it the rope kernels+/// rotate by the table's exact angles; without it they compute pos * powf(...) with cosf/sinf, which under+/// --use_fast_math is 0.0014 rad off at 32K and ~0.02 at 262K, and the prompt path (precise libm) and decode+/// (fast-math) disagree with each other.+struct RopeTab {+ const float* cos = nullptr; ///< null: no table, the kernels compute the angle as they always did+ const float* sin = nullptr;+ int max_pos = 0;+};+/// Registers the table the session just built with `scaling` (the latest one of the device wins).+void rope_table_set(const float* cos_tab, const float* sin_tab, int max_pos, const RopeScaling& scaling);+/// The registered table when the rope kernels are to read it and it was built with `scaling`; otherwise none.+RopeTab rope_table_for(const RopeScaling& scaling);+#if defined(__CUDACC__) || defined(__HIPCC__)/// ggml rope_multi, is_imrope, sections {11, 11, 10, 0}: sector = pair % 32; sector % 3 == 1 -> h (sector < 33),/// == 2 -> w (sector < 30), == 0 -> t (sector < 33). For pairs 0..31 all three bounds hold, so it is pair % 3.__device__ __forceinline__ int mrope_pos(const int32_t* tab, int pos, int pair) {return tab ? __ldg(tab + (size_t) pos * 3 + pair % 3) : pos;}+/// cos and sin of rotary position p, pair `pair` (0..31) from the table (scaling and magnitude included); false+/// when there is no table or p lies past it - then the caller computes the angle as it always did+__device__ __forceinline__ bool rope_tab_cs(const RopeTab& t, int p, int pair, float& c, float& s) {+ if (t.cos == nullptr || p < 0 || p >= t.max_pos) return false;+ c = __ldg(t.cos + (size_t) p * 32 + pair);+ s = __ldg(t.sin + (size_t) p * 32 + pair);+ return true;+}#endif} // namespace strata::kernelsdiff --git a/src/core/layer.cpp b/src/core/layer.cppindex 8ad9f41aa..e1d0490a1 100644--- a/src/core/layer.cpp+++ b/src/core/layer.cpp@@ -22,6 +22,7 @@#include "strata/kernels/native_router.hpp"#include "strata/kernels/native_qsa.hpp"#include "strata/kernels/native_qsa_indexer.hpp"+#include "strata/kernels/mrope.hpp"#include "strata/kernels/native_rope.hpp"#include "strata/kernels/native_flash_attn.hpp"#include "strata/kernels/fused_gr.hpp"@@ -662,6 +663,8 @@ uint64_t qsa_state_init(const ModelGeometry& g, int64_t max_cells, void* base, Qhc.data(), hs.data());cudaMemcpy(st.cos_tab, hc.data(), hc.size() * 4, cudaMemcpyHostToDevice);cudaMemcpy(st.sin_tab, hs.data(), hs.size() * 4, cudaMemcpyHostToDevice);+ if (s.n_rot == 64) // the native and prompt-path rope kernels read it (mrope.hpp)+ strata::kernels::rope_table_set(st.cos_tab, st.sin_tab, (int) max_cells, strata::kernels::rope_scaling());}// the page table starts as the IDENTITY, which is the simplest legal mapping and what a caller that does// not page at all wants; a streamed state starts with nothing resident, a ring at `block % n_slots`.diff --git a/src/kernels/cuda/native_qsa_indexer.cu b/src/kernels/cuda/native_qsa_indexer.cuindex 299199a93..b91fdbd5d 100644--- a/src/kernels/cuda/native_qsa_indexer.cu+++ b/src/kernels/cuda/native_qsa_indexer.cu@@ -45,7 +45,7 @@ __global__ void append(const float* __restrict__ raw, const int32_t* __restrict_float* __restrict__ pooled, int32_t* __restrict__ block_pos,int max_cells, float theta_scale, float freq_scale, float corr_low,float corr_high, float ext_factor, float mscale,- const int32_t* __restrict__ mtab) {+ const int32_t* __restrict__ mtab, RopeTab rt) {const int pos = *pos_dev, d = threadIdx.x;if (pos < 0 || pos >= max_cells) return;const int slot = pos % R;@@ -87,9 +87,11 @@ __global__ void append(const float* __restrict__ raw, const int32_t* __restrict_const int pair = d % (ROT / 2);// 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.- const float theta_extrap = (pos == 0 ? 0 : mrope_pos(mtab, rope_pos, pair)) * powf(theta_scale, float(pair));float c, s;- rope_scaled_angle(theta_extrap, freq_scale, corr_low, corr_high, ext_factor, mscale, pair, c, s);+ if (!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);+ }const float a = values[pair], z = values[pair + ROT / 2];y = d < ROT / 2 ? a * c - z * s : a * s + z * c;}@@ -105,16 +107,19 @@ __global__ void append(const float* __restrict__ raw, const int32_t* __restrict_// spare after it (every earlier one's is overwritten by the next block in order).__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) {+ float ext_factor, float mscale, const int32_t* mtab, bool zero_pos,+ const RopeTab& rt) {float y = values[d];if (d < ROT) {const int pair = d % (ROT / 2);// The same rotation the single append applies, rope_scaling.hpp's helper shared: under none this is// 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.- const float theta_extrap = (zero_pos ? 0 : mrope_pos(mtab, rope_pos, pair)) * powf(theta_scale, float(pair));float c, s;- rope_scaled_angle(theta_extrap, freq_scale, corr_low, corr_high, ext_factor, mscale, pair, c, s);+ if (!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);+ }const float a = values[pair], z = values[pair + ROT / 2];y = d < ROT / 2 ? a * c - z * s : a * s + z * c;}@@ -134,7 +139,7 @@ __device__ __forceinline__ float norm_scale(float mean, float* partials, int d)__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,- const int32_t* __restrict__ mtab) {+ const int32_t* __restrict__ mtab, RopeTab rt) {const int d = threadIdx.x;__shared__ float values[D];__shared__ float partials[32];@@ -152,7 +157,7 @@ __global__ void append_first(const float* __restrict__ raw, const float* __restr__syncthreads();if (d >= D) return;const float y = pooled_value(values, d, 0, theta_scale, freq_scale, corr_low, corr_high, ext_factor, mscale,- mtab, true);+ mtab, true, rt);pooled[d] = y;dead[d] = y;}@@ -161,7 +166,7 @@ __global__ void append_blocks(const float* __restrict__ raw, int64_t n, int64_tconst float* __restrict__ dead, float* __restrict__ pooled, int32_t* __restrict__ block_pos,int64_t first_block, int64_t last_block, float theta_scale, float freq_scale,float corr_low, float corr_high, float ext_factor, float mscale,- const int32_t* __restrict__ mtab) {+ const int32_t* __restrict__ mtab, RopeTab rt) {const int64_t b = first_block + blockIdx.x;const int d = threadIdx.x;__shared__ float values[D];@@ -184,7 +189,7 @@ __global__ void append_blocks(const float* __restrict__ raw, int64_t n, int64_tif (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,- ext_factor, mscale, mtab, false);+ ext_factor, mscale, mtab, false, rt);if (b == last_block) {pooled[std::size_t(b + 1) * D + d] = dead[d];if (d == 0) *block_pos = rope_pos;@@ -230,7 +235,7 @@ void native_qsa_indexer_append(const float* raw, const int32_t* relative_pos_devconst RopeKernelArgs k = scaling.kernel_args(ROT); // none: the identity constantsappend<<<1,THREADS,0,static_cast<cudaStream_t>(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());+ k.corr_low,k.corr_high,k.ext_factor,k.attn_factor,mrope_table(),rope_table_for(scaling));const auto error = cudaGetLastError();if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));}@@ -247,15 +252,16 @@ void native_qsa_indexer_append_batch(const float* raw, int64_t n, int64_t p0, inconst float fsf = k.freq_scale, cl = k.corr_low, ch = k.corr_high, ef = k.ext_factor, ms = k.attn_factor;const cudaStream_t st = static_cast<cudaStream_t>(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);+ 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 >= p0const int64_t hi = p0 + n - 1 >= R - 1 ? (p0 + n - 1 - (R - 1)) / R : -1; // the largest b with 4b+3 <= p0+n-1if (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);+ theta_scale, fsf, cl, ch, ef, ms, mtab, rt);append_tail<<<R - 1, D, 0, st>>>(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.cuindex 463117204..444184019 100644--- a/src/kernels/cuda/native_rope.cu+++ b/src/kernels/cuda/native_rope.cu@@ -28,6 +28,7 @@#include <atomic>#include <cmath>#include <cstdint>+#include <cstdlib>#include <limits>#include <stdexcept>@@ -40,7 +41,7 @@ bool overlaps(const void* a, size_t an, const void* b, size_t bn) {}__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) {+ float ext_factor, float mscale, const int* positions, const int32_t* mtab, RopeTab rt) {const int row = blockIdx.y;const int pair = blockIdx.x * blockDim.x + threadIdx.x;if (row >= rows || pair >= width / 2) return;@@ -52,9 +53,11 @@ __global__ void apply(const float* x, float* out, int rows, int width,}return;}- const float theta_extrap = mrope_pos(mtab, positions[row], pair) * powf(theta_scale, float(pair));float c, s;- rope_scaled_angle(theta_extrap, freq_scale, corr_low, corr_high, ext_factor, mscale, pair, c, s);+ if (!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);+ }const float a = x[start + pair], b = x[start + pair + n_rot / 2];out[start + pair] = a * c - b * s;out[start + pair + n_rot / 2] = a * s + b * c;@@ -72,6 +75,31 @@ int mrope_dev() {} // namespacevoid mrope_table_set(const int32_t* device_table) { mrope_tab[mrope_dev()].store(device_table, std::memory_order_relaxed); }const int32_t* mrope_table() { return mrope_tab[mrope_dev()].load(std::memory_order_relaxed); }+namespace {+// #280: the float64 angle table of each device, set at session init (before any graph is captured)+struct RopeReg {+ RopeTab tab;+ RopeScaling scaling;+};+RopeReg rope_tab[kMropeDevices] = {};+bool same_scaling(const RopeScaling& a, const RopeScaling& b) {+ return a.type == b.type && a.freq_base == b.freq_base && a.factor == b.factor && a.freq_scale_in == b.freq_scale_in &&+ a.orig_ctx == b.orig_ctx && a.ext_factor == b.ext_factor && a.attn_factor == b.attn_factor &&+ a.beta_fast == b.beta_fast && a.beta_slow == b.beta_slow;+}+bool rope_table_enabled() {+ static const bool on = std::getenv("STRATA_ROPE_LEGACY") == nullptr; // STRATA_ROPE_LEGACY=1: the A/B arm+ return on;+}+} // namespace+void rope_table_set(const float* cos_tab, const float* sin_tab, int max_pos, const RopeScaling& scaling) {+ rope_tab[mrope_dev()] = RopeReg{RopeTab{cos_tab, sin_tab, max_pos}, scaling};+}+RopeTab rope_table_for(const RopeScaling& scaling) {+ if (!rope_table_enabled()) return {};+ const RopeReg& r = rope_tab[mrope_dev()];+ return r.tab.cos != nullptr && same_scaling(r.scaling, scaling) ? r.tab : RopeTab{};+}void native_rope_set_enabled(bool value) { enabled.store(value, std::memory_order_relaxed); }bool native_rope_enabled() { return enabled.load(std::memory_order_relaxed); }void native_rope_apply(const float* x, float* out, int rows, int head_dim,@@ -95,7 +123,7 @@ void native_rope_apply(const float* x, float* out, int rows, int head_dim,apply<<<dim3((head_dim / 2 + 127) / 128, rows), 128, 0,static_cast<cudaStream_t>(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());+ k.attn_factor, positions, mrope_table(), rope_table_for(scaling));const auto error = cudaGetLastError();if (error != cudaSuccess) throw std::runtime_error(cudaGetErrorString(error));}diff --git a/src/prefill/kernels.cu b/src/prefill/kernels.cuindex 25c120046..0035833b9 100644--- a/src/prefill/kernels.cu+++ b/src/prefill/kernels.cu@@ -561,16 +561,19 @@ __global__ void rms_rows_kernel(float* __restrict__ x, const float* __restrict__}__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) {+ float ext_factor, float mscale, const int32_t* __restrict__ mtab,+ strata::kernels::RopeTab rt) {const int64_t row = blockIdx.x; // t * heads + hconst int pair = threadIdx.x; // 0..31const int64_t t = row / heads, h = row % heads;float* p = x + t * ld + h * dim;- const float theta_extrap =- (float) strata::kernels::mrope_pos(mtab, (int) (pos0 + t), pair) * powf(theta_scale, (float) pair);float c, s;- strata::kernels::rope_scaled_angle(theta_extrap, freq_scale, corr_low, corr_high, ext_factor, mscale,- pair, c, s);+ if (!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,+ pair, c, s);+ }const float a = p[pair], b = p[pair + 32];p[pair] = a * c - b * s;p[pair + 32] = a * s + b * c;@@ -813,7 +816,7 @@ void rope(float* x, int64_t T, int64_t heads, int64_t dim, int64_t ld, int64_t pconst strata::kernels::RopeKernelArgs k = scaling.kernel_args(64); // none: the identity constantsrope_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::mrope_table(), strata::kernels::rope_table_for(scaling));check("rope");}void split_q(const float* q_full, float* q, int64_t T, void* stream) {