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

prefill: the CPU share on NVFP4 experts - the CPU's rows take row_sd 1 (upstream's CPU share is the base)

sergiywith77c6991d18e2
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index aeb4e31e6..90dfbe1ab 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -793,9 +793,10 @@ struct Prefill::Impl {
std::vector<int32_t> bounds_host;
std::unique_ptr<mmq::Context> mmq_ctx;
std::vector<int32_t> ids_host, slot_host, src_host, cnt, off;
- // set_cpu_pool: a small chunk's CPU experts. The layer's MoE input (T x N, from `mixed`) and the rows the pool
- // writes (Dm's tail, in Dm's row order), both pinned; and the pool's per-token activations and jobs.
- float *cpu_x = nullptr, *cpu_rows = nullptr;
+ // set_cpu_pool: a small chunk's CPU experts. The layer's MoE input (T x N, from `mixed`), the rows the pool writes
+ // (Dm's tail, in Dm's row order), ones for row_sd (the CPU's rows carry s_down already), all pinned; and the
+ // pool's per-token activations and jobs.
+ float *cpu_x = nullptr, *cpu_rows = nullptr, *cpu_ones = nullptr;
size_t cpu_x_n = 0, cpu_rows_n = 0;
bool cpu_dead = false; // a pinned buffer of the share could not be had (host memory is short): the GPU does it all
std::vector<uint8_t> cpu_nact;
@@ -945,7 +946,7 @@ void Prefill::release() {
impl_->steps_up.release();
impl_->tok_up.release();
impl_->up_ring.release();
- for (float* p : {impl_->cpu_x, impl_->cpu_rows})
+ for (float* p : {impl_->cpu_x, impl_->cpu_rows, impl_->cpu_ones})
if (p) cudaFreeHost(p);
for (cudaEvent_t e : impl_->cpu_ev)
if (e) cudaEventDestroy(e);
@@ -3360,14 +3361,17 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
const size_t want = (size_t) rows_cpu * N;
if (m.cpu_rows_n < want) {
cpu_cold = true;
- if (m.cpu_rows) cudaFreeHost(m.cpu_rows);
- m.cpu_rows = nullptr;
+ for (float* q : {m.cpu_rows, m.cpu_ones})
+ if (q) cudaFreeHost(q);
+ m.cpu_rows = m.cpu_ones = nullptr;
m.cpu_rows_n = 0;
const size_t cap_rows = (size_t) rows_cpu * 2; // headroom: few reallocations
- if (cudaHostAlloc((void**) &m.cpu_rows, cap_rows * N * sizeof(float), cudaHostAllocDefault) != cudaSuccess) {
+ if (cudaHostAlloc((void**) &m.cpu_rows, cap_rows * N * sizeof(float), cudaHostAllocDefault) != cudaSuccess ||
+ cudaHostAlloc((void**) &m.cpu_ones, cap_rows * sizeof(float), cudaHostAllocDefault) != cudaSuccess) {
err = "prefill: cannot allocate the CPU experts' rows";
return false;
}
+ std::fill(m.cpu_ones, m.cpu_ones + cap_rows, 1.0f);
m.cpu_rows_n = cap_rows * N;
}
const int64_t r0c = T * K - rows_cpu;
@@ -3980,7 +3984,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
}
}
pt.mark(kPfCombine, cs);
- if (cpu_fut.valid()) { // set_cpu_pool: the CPU's rows into Dm's tail
+ if (cpu_fut.valid()) { // set_cpu_pool: the CPU's rows into Dm's tail, row_sd 1 (s_down is in them)
if (cpu_share_env() < 0.0) cudaEventRecord(m.cpu_ev[1], m.cs); // after the GPU's expert work
if (!cpu_fut.get()) { err = "prefill: a CPU expert has no blob"; return false; }
if (cpu_share_env() < 0.0 && n_stream > n_cpu) {
@@ -3993,6 +3997,9 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
const int64_t r0c = T * K - rows_cpu;
cudaMemcpyAsync(m.Dm + (size_t) r0c * N, m.cpu_rows, (size_t) rows_cpu * N * sizeof(float),
cudaMemcpyHostToDevice, m.cs);
+ if (use_mmq && !fused_l && lay.native && lay.fmt[(size_t) l].tail_off)
+ cudaMemcpyAsync(m.row_sd + r0c, m.cpu_ones, (size_t) rows_cpu * sizeof(float),
+ cudaMemcpyHostToDevice, m.cs);
stats_.experts_cpu += n_cpu;
}
// no reading from a layer the share could not change (nothing taken) or with one-time costs