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

Layer split: images, control vectors, KV streaming, --mmap-experts and the helper GPUs across GPUs

Niko12219baff315921d
diff --git a/include/strata/kernels/cvec.hpp b/include/strata/kernels/cvec.hpp
index 2dd120fc2..a447eb528 100644
--- a/include/strata/kernels/cvec.hpp
+++ b/include/strata/kernels/cvec.hpp
@@ -43,7 +43,12 @@ const Cvec& cvec();
bool cvec_upload(const std::vector<float>& dir, const std::vector<float>& s, int mode, int first, int last,
int64_t n_embd, int64_t hc, std::string& err);
-/// The per-request switch. Synchronizes the device when it changes, so call it between requests.
+/// A layer split: put the loaded vector's tables on the CURRENT device too (cvec_apply uses the tables of the device
+/// it runs on). No-op without a vector or when this device has them already.
+bool cvec_replicate(std::string& err);
+
+/// The per-request switch, on every device that holds the vector. Synchronizes them when it changes, so call it
+/// between requests.
void cvec_set_enabled(bool on);
bool cvec_enabled();
diff --git a/include/strata/kernels/mrope.hpp b/include/strata/kernels/mrope.hpp
index 165f3edb2..14a291d54 100644
--- a/include/strata/kernels/mrope.hpp
+++ b/include/strata/kernels/mrope.hpp
@@ -18,6 +18,7 @@
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();
diff --git a/src/kernels/cuda/cvec.cu b/src/kernels/cuda/cvec.cu
index 87ba141f8..668293246 100644
--- a/src/kernels/cuda/cvec.cu
+++ b/src/kernels/cuda/cvec.cu
@@ -11,9 +11,33 @@ namespace {
constexpr int THREADS = 256;
constexpr int MAXK = 16; // n_embd up to 4096, held in registers between the dot and the update
-Cvec g_cvec;
-int* g_on = nullptr;
+Cvec g_cvec; // the description; its device pointers are the uploading device's
bool g_on_host = false;
+// the tables on every device that holds them (a layer split applies the vector on several)
+constexpr int kDevices = 64;
+struct DevTables { float* dir = nullptr; float* s = nullptr; int* on = nullptr; };
+DevTables g_dev[kDevices];
+std::vector<float> g_dir_host, g_s_host;
+int cur_device() {
+ int d = 0;
+ if (cudaGetDevice(&d) != cudaSuccess || d < 0 || d >= kDevices) d = 0;
+ return d;
+}
+bool upload_here(std::string& err) {
+ DevTables& t = g_dev[cur_device()];
+ if (t.dir != nullptr) return true;
+ const int flag = g_on_host ? 1 : 0;
+ if (cudaMalloc(&t.dir, g_dir_host.size() * sizeof(float)) != cudaSuccess ||
+ cudaMalloc(&t.s, g_s_host.size() * sizeof(float)) != cudaSuccess || cudaMalloc(&t.on, sizeof(int)) != cudaSuccess ||
+ cudaMemcpy(t.dir, g_dir_host.data(), g_dir_host.size() * sizeof(float), cudaMemcpyHostToDevice) != cudaSuccess ||
+ cudaMemcpy(t.s, g_s_host.data(), g_s_host.size() * sizeof(float), cudaMemcpyHostToDevice) != cudaSuccess ||
+ cudaMemcpy(t.on, &flag, sizeof(int), cudaMemcpyHostToDevice) != cudaSuccess) {
+ err = "control vector: device allocation failed";
+ t = DevTables{};
+ return false;
+ }
+ return true;
+}
// the fused hyper-connection read's gate (fused_gr.cu), so a write done here is bitwise the one it would have folded
__device__ __forceinline__ float sigmoidf_(float x) { return 1.0f / (1.0f + __expf(-x)); }
@@ -78,21 +102,26 @@ bool cvec_upload(const std::vector<float>& dir, const std::vector<float>& s, int
int64_t n_embd, int64_t hc, std::string& err) {
if (n_embd < 1 || n_embd > (int64_t) THREADS * MAXK) { err = "control vector: unsupported n_embd"; return false; }
if (s.empty() || dir.size() != s.size() * (size_t) n_embd) { err = "control vector: bad table sizes"; return false; }
- float* d_dir = nullptr;
- float* d_s = nullptr;
- int* d_on = nullptr;
- const int one = 1;
- if (cudaMalloc(&d_dir, dir.size() * sizeof(float)) != cudaSuccess ||
- cudaMalloc(&d_s, s.size() * sizeof(float)) != cudaSuccess || cudaMalloc(&d_on, sizeof(int)) != cudaSuccess ||
- cudaMemcpy(d_dir, dir.data(), dir.size() * sizeof(float), cudaMemcpyHostToDevice) != cudaSuccess ||
- cudaMemcpy(d_s, s.data(), s.size() * sizeof(float), cudaMemcpyHostToDevice) != cudaSuccess ||
- cudaMemcpy(d_on, &one, sizeof(int), cudaMemcpyHostToDevice) != cudaSuccess) {
- err = "control vector: device allocation failed";
- return false;
+ int prev = 0; // a new vector replaces the old one on every device
+ cudaGetDevice(&prev);
+ for (int d = 0; d < kDevices; ++d) {
+ if (g_dev[d].dir == nullptr) continue;
+ cudaSetDevice(d);
+ cudaDeviceSynchronize();
+ cudaFree(g_dev[d].dir);
+ cudaFree(g_dev[d].s);
+ cudaFree(g_dev[d].on);
+ g_dev[d] = DevTables{};
}
- g_cvec.dir = d_dir;
- g_cvec.s = d_s;
- g_cvec.on = d_on;
+ cudaSetDevice(prev);
+ g_dir_host = dir;
+ g_s_host = s;
+ g_on_host = true;
+ if (!upload_here(err)) return false;
+ const DevTables& t = g_dev[cur_device()];
+ g_cvec.dir = t.dir;
+ g_cvec.s = t.s;
+ g_cvec.on = t.on;
g_cvec.mode = mode;
g_cvec.first = first;
g_cvec.last = last;
@@ -100,26 +129,35 @@ bool cvec_upload(const std::vector<float>& dir, const std::vector<float>& s, int
g_cvec.hc = hc;
g_cvec.steered.assign(s.size(), false);
for (size_t l = 0; l < s.size(); ++l) g_cvec.steered[l] = s[l] != 0.0f;
- g_on = d_on;
- g_on_host = true;
return true;
}
+bool cvec_replicate(std::string& err) { return !g_cvec.loaded() || upload_here(err); }
+
void cvec_set_enabled(bool on) {
- if (g_on == nullptr || on == g_on_host) return;
- cudaDeviceSynchronize(); // nothing in flight may still read the flag
+ if (!g_cvec.loaded() || on == g_on_host) return;
+ int prev = 0;
+ cudaGetDevice(&prev);
const int v = on ? 1 : 0;
- cudaMemcpy(g_on, &v, sizeof(int), cudaMemcpyHostToDevice);
+ for (int d = 0; d < kDevices; ++d) {
+ if (g_dev[d].on == nullptr) continue;
+ cudaSetDevice(d);
+ cudaDeviceSynchronize(); // nothing in flight may still read the flag
+ cudaMemcpy(g_dev[d].on, &v, sizeof(int), cudaMemcpyHostToDevice);
+ }
+ cudaSetDevice(prev);
g_on_host = on;
}
-bool cvec_enabled() { return g_on != nullptr && g_on_host; }
+bool cvec_enabled() { return g_cvec.loaded() && g_on_host; }
void cvec_apply(float* R, int64_t layer, int64_t T, int64_t r_ld, const float* bo, int64_t bo_ld, const float* inj,
int64_t inj_ld, bool write, void* stream) {
if (!g_cvec.loaded() || T < 1) return;
+ const DevTables& t = g_dev[cur_device()];
+ if (t.dir == nullptr) throw std::runtime_error("cvec_apply: the control vector is not on this device (cvec_replicate)");
const dim3 grid((unsigned) g_cvec.hc, (unsigned) T);
- cvec_kernel<<<grid, THREADS, 0, (cudaStream_t) stream>>>(R, g_cvec.dir, g_cvec.s, g_cvec.on, g_cvec.mode, layer,
+ cvec_kernel<<<grid, THREADS, 0, (cudaStream_t) stream>>>(R, t.dir, t.s, t.on, g_cvec.mode, layer,
(int) g_cvec.n_embd, (int) g_cvec.hc, r_ld, bo, bo_ld,
inj, inj_ld, write ? 1 : 0);
if (cudaPeekAtLastError() != cudaSuccess) throw std::runtime_error("cvec_apply: launch failed");
diff --git a/src/kernels/cuda/native_rope.cu b/src/kernels/cuda/native_rope.cu
index d3e9225c2..6dd3f6b59 100644
--- a/src/kernels/cuda/native_rope.cu
+++ b/src/kernels/cuda/native_rope.cu
@@ -58,9 +58,18 @@ __global__ void apply(const float* x, float* out, int rows, int width,
out[start + pair + n_rot / 2] = a * s + b * c;
}
}
-namespace { std::atomic<const int32_t*> mrope_tab{nullptr}; }
-void mrope_table_set(const int32_t* device_table) { mrope_tab.store(device_table, std::memory_order_relaxed); }
-const int32_t* mrope_table() { return mrope_tab.load(std::memory_order_relaxed); }
+// one table per device (a layer split runs the rope kernels on several): set and read for the current device
+namespace {
+constexpr int kMropeDevices = 64;
+std::atomic<const int32_t*> mrope_tab[kMropeDevices] = {};
+int mrope_dev() {
+ int d = 0;
+ if (cudaGetDevice(&d) != cudaSuccess || d < 0 || d >= kMropeDevices) d = 0;
+ return d;
+}
+} // namespace
+void 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); }
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,
diff --git a/src/program/generate.cpp b/src/program/generate.cpp
index 0364a73ed..e186279d4 100644
--- a/src/program/generate.cpp
+++ b/src/program/generate.cpp
@@ -560,6 +560,7 @@ struct GpuStage {
cudaStream_t adapt_stream = nullptr;
cudaEvent_t adapt_ev = nullptr;
bool adapt_live = false; ///< swaps of this request are in flight on it
+ int32_t* mrope = nullptr; ///< --vision: the image-position table on its device
};
// ---- issue #31: what the watchdog prints before it stops a stalled engine
@@ -1111,15 +1112,30 @@ int main(int argc, char** argv) {
}
}
const bool multi_gpu = !split_devs.empty() && !split_same;
+ // the helper-GPU expert caches (--expert-cache-remote, docs/SECOND_GPU.md): CUDA1..3 on one GPU; with a layer
+ // split, the visible GPUs no stage runs on, in order
+ int remote_dev[3] = {1, 2, 3};
if (multi_gpu) {
- if (o.vision || !o.cvec_files.empty() || o.expert_cache_remote[0] > 0 || o.mmap_experts ||
- o.expert_profile.empty()) {
- std::fprintf(stderr, "strata generate: a layer split across GPUs needs --expert-profile, and does not "
- "take --vision, --control-vector, --expert-cache-remote or --mmap-experts yet\n");
+ if (o.expert_profile.empty()) {
+ std::fprintf(stderr, "strata generate: a layer split across GPUs needs --expert-profile\n");
return 2;
}
o.no_prefill_borrow = true; // each stage's prompt path has its own buffers
- o.kv_resident = 0; // one KV streaming arena per process
+ int n_vis = 1;
+ if (cudaGetDeviceCount(&n_vis) != cudaSuccess || n_vis < 1) n_vis = 1;
+ cudaGetLastError();
+ int next_free = 1;
+ for (int r = 0; r < 3; ++r) {
+ if (o.expert_cache_remote[(size_t) r] <= 0) continue;
+ while (next_free < n_vis &&
+ std::find(split_devs.begin(), split_devs.end(), next_free) != split_devs.end()) ++next_free;
+ if (next_free >= n_vis) {
+ std::fprintf(stderr, "strata generate: --expert-cache-remote with a layer split needs a GPU that runs no "
+ "stage (%d visible, %zu used by the split)\n", n_vis, split_devs.size() + 1);
+ return 2;
+ }
+ remote_dev[r] = next_free++;
+ }
std::string devs;
for (const int d : split_devs) devs += (devs.empty() ? "" : ",") + std::to_string(d);
std::fprintf(stderr, "strata generate: layer split across %zu GPUs: CUDA0, then CUDA%s (split %s)\n",
@@ -1270,12 +1286,12 @@ int main(int argc, char** argv) {
// Keep CUDA1's proven startup order: initialise its context before
// allocating GPU0 weights or mapping the large host expert arena.
double free_gib = 0;
- if (!strata::core::RemoteExperts::preflight(1, free_gib, err)) {
+ if (!strata::core::RemoteExperts::preflight(remote_dev[0], free_gib, err)) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
return 1;
}
- std::fprintf(stderr, "strata generate: CUDA1 context ready, %.2f GiB free before expert arena registration\n",
- free_gib);
+ std::fprintf(stderr, "strata generate: CUDA%d context ready, %.2f GiB free before expert arena registration\n",
+ remote_dev[0], free_gib);
}
// plan v0.3 P6: the PCIe share of the missed experts, measured per kind of pack (the paper, finding on PCIe).
// PR #44: a x8 link carries half of what the native default assumes - the GPU's SMs read that share over the
@@ -1623,6 +1639,22 @@ int main(int argc, char** argv) {
wo_s == nullptr ? "output.weight is missing" : err.c_str());
return 1;
}
+ // a control vector (the speed projection): its tables on this device too - the stage's layers apply it here
+ if (!strata::kernels::cvec_replicate(err)) {
+ std::fprintf(stderr, "strata generate: layer split, CUDA%d: %s\n", st.dev, err.c_str());
+ return 1;
+ }
+ // --vision: this device's image-position table (the identity until a picture request), read by every rope
+ // kernel its stage runs - set before any of its graphs is captured
+ if (o.vision) {
+ if (cudaMalloc(&st.mrope, mrope_host.size() * sizeof(int32_t)) != cudaSuccess ||
+ cudaMemcpy(st.mrope, mrope_host.data(), mrope_host.size() * sizeof(int32_t), cudaMemcpyHostToDevice) !=
+ cudaSuccess) {
+ std::fprintf(stderr, "strata generate: layer split, CUDA%d: the image position table failed\n", st.dev);
+ return 1;
+ }
+ strata::kernels::mrope_table_set(st.mrope);
+ }
// its own PCIe share of the missed experts (the same rule as CUDA0's above: its link is probed)
st.pcie_frac = o.pcie_frac;
if (!pcie_given && native_pack) {
@@ -1658,12 +1690,12 @@ int main(int argc, char** argv) {
// before the host arena maps its expert pages into their address spaces.
for (int r = 1; r < 3; ++r) if (o.expert_cache_remote[(size_t) r] > 0) {
double free_gib = 0;
- if (!strata::core::RemoteExperts::preflight(r + 1, free_gib, err)) {
+ if (!strata::core::RemoteExperts::preflight(remote_dev[r], free_gib, err)) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
return 1;
}
std::fprintf(stderr, "strata generate: CUDA%d context ready, %.2f GiB free before expert arena registration\n",
- r + 1, free_gib);
+ remote_dev[r], free_gib);
}
// ---- the CPU expert pool
@@ -2124,6 +2156,11 @@ int main(int argc, char** argv) {
return 2;
}
std::vector<std::pair<int32_t, int32_t>> ranked = profile;
+ if (!stages.empty()) { // a layer split: CUDA0's share of the profile, then the later stages' pairs no cache holds
+ for (auto& st : stages)
+ for (const auto& pr : st->profile)
+ if (st->cache.slot_of(pr.first, pr.second) < 0) ranked.push_back(pr);
+ }
if (multi_remote) {
// The shipped frequency profile names only 8000 of 24576 experts. Once exhausted,
// fill remaining VRAM from unranked pairs in expert-then-layer order: this spreads
@@ -2177,14 +2214,18 @@ int main(int argc, char** argv) {
by_device[0] = std::move(ranked);
}
std::vector<uint8_t> claimed((size_t) g.n_layers * (size_t) g.n_expert, 0);
+ for (auto& st : stages) // a layer split: what a stage's cache holds is no helper's
+ for (const auto& pr : st->profile)
+ if (st->cache.slot_of(pr.first, pr.second) >= 0)
+ claimed[(size_t) pr.first * (size_t) g.n_expert + (size_t) pr.second] = 1;
for (int r = 0; r < 3; ++r) if (o.expert_cache_remote[(size_t) r] > 0) {
- if (!remote_experts[(size_t) r].open(r + 1, o.expert_cache_remote[(size_t) r],
+ if (!remote_experts[(size_t) r].open(remote_dev[r], o.expert_cache_remote[(size_t) r],
g.n_layers, g.n_expert, by_device[(size_t) r], xcache, *srcp, claimed, err)) {
std::fprintf(stderr, "strata generate: %s\n", err.c_str());
return 1;
}
std::fprintf(stderr, "strata generate: CUDA%d: %lld additional experts, %.2f GiB; "
- "results return through pinned host rows\n", r + 1,
+ "results return through pinned host rows\n", remote_dev[r],
(long long) remote_experts[(size_t) r].resident(), remote_experts[(size_t) r].gib());
}
}
@@ -3490,13 +3531,23 @@ int main(int argc, char** argv) {
tr("positions built", (long long) img_rows.size());
cudaDeviceSynchronize();
tr("device idle");
- if (ve.empty() && cudaMemcpy(d_mrope, mrope_host.data(), mrope_host.size() * sizeof(int32_t),
- cudaMemcpyHostToDevice) != cudaSuccess)
- ve = "the image position upload failed";
+ // CUDA0's table and, with a layer split, every later stage's (each device reads its own)
+ auto upload_mrope = [&]() -> bool {
+ bool ok = cudaMemcpy(d_mrope, mrope_host.data(), mrope_host.size() * sizeof(int32_t),
+ cudaMemcpyHostToDevice) == cudaSuccess;
+ for (auto& st : stages) {
+ const strata::core::OnDevice on(st->dev);
+ cudaDeviceSynchronize();
+ ok = ok && cudaMemcpy(st->mrope, mrope_host.data(), mrope_host.size() * sizeof(int32_t),
+ cudaMemcpyHostToDevice) == cudaSuccess;
+ }
+ return ok;
+ };
+ if (ve.empty() && !upload_mrope()) ve = "the image position upload failed";
if (!ve.empty()) {
// leave the table as the identity so the next text request is untouched
for (int64_t c = 0; c < cells; ++c) put(c, c, c, c);
- cudaMemcpy(d_mrope, mrope_host.data(), mrope_host.size() * sizeof(int32_t), cudaMemcpyHostToDevice);
+ upload_mrope();
mrope_identity = true;
std::printf("ERR %s\n", ve.c_str());
std::fflush(stdout);