Skip to content

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++

#280 follow-up: the float64 RoPE table is opt-in (STRATA_ROPE_TABLE=1); a freed session's table is forgotten

Niko12210f461187d2c5
diff --git a/include/strata/core/layer.hpp b/include/strata/core/layer.hpp
index 1669256f3..34254a613 100644
--- a/include/strata/core/layer.hpp
+++ b/include/strata/core/layer.hpp
@@ -237,6 +237,7 @@ struct QsaState {
int32_t* idx_block_pos = nullptr;
float* cos_tab = nullptr; ///< (max_cells, n_rot/2), built on the HOST in float64
+ bool owns_rope = false; ///< built the table above (not borrowed with share_rope): it releases it
float* sin_tab = nullptr;
/// THE PER-TOKEN COUNTS, IN DEVICE MEMORY - the whole reason this layer can be a graph. `qsa_step_fill`
diff --git a/include/strata/core/session.hpp b/include/strata/core/session.hpp
index c05d7e991..6d1becfac 100644
--- a/include/strata/core/session.hpp
+++ b/include/strata/core/session.hpp
@@ -99,6 +99,9 @@ uint64_t session_bytes(const ModelGeometry& g, int64_t max_cells, int64_t k, int
/// Carves `base` (DEVICE memory) into `s`. Returns the bytes used. Same range convention as `session_bytes`.
uint64_t session_init(const ModelGeometry& g, int64_t max_cells, int64_t k, void* base, SessionState& s,
int64_t layer_lo = 0, int64_t layer_hi = -1);
+/// Before the memory `session_init` carved is freed: forgets what points into it from outside the session (the
+/// rope kernels' registered angle table, #280), so a later session never rotates by freed memory.
+void session_release(SessionState& s);
/// Zeroes every layer's state - the residual to `R_init`, everything else to zero, so a fresh sequence starts
/// from the reference's own `zeros()`.
void session_zero(SessionState& s, const ModelGeometry& g, const float* R_init, void* stream);
diff --git a/include/strata/kernels/mrope.hpp b/include/strata/kernels/mrope.hpp
index 8f456e43a..cb1451c40 100644
--- a/include/strata/kernels/mrope.hpp
+++ b/include/strata/kernels/mrope.hpp
@@ -24,9 +24,9 @@ namespace strata::kernels {
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
+/// #280, opt-in (STRATA_ROPE_TABLE=1): the rotation angles, 32 pairs of a 64-wide rotary slice, from 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 {
@@ -36,7 +36,10 @@ struct RopeTab {
};
/// 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.
+/// Forgets the table `cos_tab` (on every device) - before the memory that holds it is freed (session_release).
+void rope_table_release(const float* cos_tab);
+/// The registered table when STRATA_ROPE_TABLE=1 and it was built with `scaling`; otherwise none (the default: the
+/// kernels compute the angle exactly as before).
RopeTab rope_table_for(const RopeScaling& scaling);
#if defined(__CUDACC__) || defined(__HIPCC__)
diff --git a/src/core/layer.cpp b/src/core/layer.cpp
index e1d0490a1..e816da1ca 100644
--- a/src/core/layer.cpp
+++ b/src/core/layer.cpp
@@ -663,7 +663,8 @@ uint64_t qsa_state_init(const ModelGeometry& g, int64_t max_cells, void* base, Q
hc.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)
+ st.owns_rope = true;
+ if (s.n_rot == 64) // the native and prompt-path rope kernels read it (mrope.hpp, STRATA_ROPE_TABLE=1)
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
diff --git a/src/core/session.cpp b/src/core/session.cpp
index 2211e9ba5..14c61663b 100644
--- a/src/core/session.cpp
+++ b/src/core/session.cpp
@@ -1,5 +1,6 @@
// src/core/session.cpp - one token through all 48 layers. See the header for why the graphs are per-layer.
#include "strata/core/session.hpp"
+#include "strata/kernels/mrope.hpp"
#include "strata/core/progress.hpp"
#include "strata/kernels/qsa.hpp"
@@ -127,6 +128,14 @@ uint64_t session_init(const ModelGeometry& g, int64_t max_cells, int64_t k, void
return used;
}
+void session_release(SessionState& s) {
+ for (int64_t j = 0; s.qsa_states != nullptr && j < s.qsa_alloc; ++j)
+ if (s.qsa_states[j].owns_rope) {
+ strata::kernels::rope_table_release(s.qsa_states[j].cos_tab);
+ s.qsa_states[j].owns_rope = false;
+ }
+}
+
void session_zero(SessionState& s, const ModelGeometry& g, const float* R_init, void* stream) {
cudaStream_t cs = (cudaStream_t) stream;
// the residual: `hc` copies of the one vector a caller hands in. A real sequence's first token is the
diff --git a/src/kernels/cuda/native_rope.cu b/src/kernels/cuda/native_rope.cu
index 444184019..b34f4e672 100644
--- a/src/kernels/cuda/native_rope.cu
+++ b/src/kernels/cuda/native_rope.cu
@@ -87,14 +87,23 @@ bool same_scaling(const RopeScaling& a, const RopeScaling& b) {
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;
}
+// opt-in: STRATA_ROPE_TABLE=1 (the table's angles differ from the fast-math ones in the last bits, so outputs move)
bool rope_table_enabled() {
- static const bool on = std::getenv("STRATA_ROPE_LEGACY") == nullptr; // STRATA_ROPE_LEGACY=1: the A/B arm
+ static const bool on = [] {
+ const char* e = std::getenv("STRATA_ROPE_TABLE");
+ return e != nullptr && e[0] == '1';
+ }();
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};
}
+void rope_table_release(const float* cos_tab) {
+ // every device's entry that points at it (the caller may free it from another current device)
+ for (RopeReg& r : rope_tab)
+ if (cos_tab != nullptr && r.tab.cos == cos_tab) r = RopeReg{};
+}
RopeTab rope_table_for(const RopeScaling& scaling) {
if (!rope_table_enabled()) return {};
const RopeReg& r = rope_tab[mrope_dev()];