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 chunk's step records and token ids go up from a pinned buffer, not pageable memory (#1712)

Niko1221b97a36a49ac6
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index e36bb6884..15078dbf1 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -694,6 +694,46 @@ struct PeerPrefill {
}
};
+// #1712: a chunk's small int32 tables (the QSA step records, the token ids) go up from a pinned buffer. A cudaMemcpyAsync
+// from pageable memory is staged by the runtime; on gfx1100 the first text prompt after a picture one was seen to read stale
+// step records that way. The event holds the next upload back until the last one has left the buffer. No pinned RAM:
+// the copy is made from the caller's memory, as before.
+struct PinnedUpload {
+ int32_t* p = nullptr;
+ size_t n = 0;
+ cudaEvent_t ev = nullptr;
+ void init(size_t count) {
+ if (p || force_pageable()) return;
+ if (cudaHostAlloc((void**) &p, count * sizeof(int32_t), cudaHostAllocDefault) != cudaSuccess) {
+ cudaGetLastError();
+ p = nullptr;
+ return;
+ }
+ n = count;
+ if (cudaEventCreateWithFlags(&ev, cudaEventDisableTiming) != cudaSuccess) {
+ cudaGetLastError();
+ cudaFreeHost(p);
+ p = nullptr;
+ n = 0;
+ ev = nullptr;
+ }
+ }
+ bool upload(void* dst, const int32_t* src, size_t count, cudaStream_t s) {
+ const size_t bytes = count * sizeof(int32_t);
+ if (!p || count > n) return cudaMemcpyAsync(dst, src, bytes, cudaMemcpyHostToDevice, s) == cudaSuccess;
+ if (cudaEventSynchronize(ev) != cudaSuccess) return false; // the previous upload is out of the buffer
+ std::memcpy(p, src, bytes);
+ return cudaMemcpyAsync(dst, p, bytes, cudaMemcpyHostToDevice, s) == cudaSuccess && cudaEventRecord(ev, s) == cudaSuccess;
+ }
+ void release() {
+ if (ev) cudaEventDestroy(ev);
+ if (p) cudaFreeHost(p);
+ p = nullptr;
+ ev = nullptr;
+ n = 0;
+ }
+};
+
struct Prefill::Impl {
const core::WeightTable* wt = nullptr;
const core::ModelGeometry* g = nullptr;
@@ -723,6 +763,7 @@ struct Prefill::Impl {
uint16_t* attn_h = nullptr;
int32_t* steps_dev = nullptr;
std::vector<int32_t> steps_host;
+ PinnedUpload steps_up;
int32_t* sel_ids = nullptr;
float* sel_scores = nullptr; // [sel_batch, max_blocks]
int64_t sel_batch = 256, max_blocks = 0;
@@ -814,6 +855,7 @@ struct Prefill::Impl {
// C-4: the chunk's token ids on the device, for one batched embedding gather
int32_t* tok_dev = nullptr;
std::vector<int32_t> tok_host;
+ PinnedUpload tok_up;
std::unique_ptr<PeerPrefill> pp; // multi-GPU: the peer GPU's expert share (set_peer)
// layer split: the next stage's GPU as a stream-only peer (set_stage_helper); its buffers are that stage's own
// prompt buffers, bound per prompt, and it takes the place of `pp` for a run it helps
@@ -889,6 +931,8 @@ void Prefill::release() {
if (impl_->kv_released) cudaEventDestroy(impl_->kv_released);
if (impl_->kv_ready) cudaEventDestroy(impl_->kv_ready);
if (impl_->grp_host) cudaFreeHost(impl_->grp_host);
+ impl_->steps_up.release();
+ impl_->tok_up.release();
for (float* p : {impl_->cpu_x, impl_->cpu_rows})
if (p) cudaFreeHost(p);
for (cudaEvent_t e : impl_->cpu_ev)
@@ -1037,6 +1081,7 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co
}
m.owned.push_back(m.tok_dev);
m.tok_host.resize((size_t) chunk);
+ m.tok_up.init((size_t) chunk);
}
if (cudaStreamCreateWithFlags(&m.copy, cudaStreamNonBlocking) != cudaSuccess) { err = "prefill: copy stream"; return false; }
const size_t T = (size_t) chunk;
@@ -1083,6 +1128,7 @@ bool Prefill::init(const core::WeightTable& wt, const core::ModelGeometry& g, co
m.stager->batch = std::min(m.stager->batch, m.stager->kRing);
}
m.steps_host.resize(T * strata::kernels::kStepCount);
+ m.steps_up.init(T * strata::kernels::kStepCount);
m.ids_host.resize(T * K); m.slot_host.resize(T * K); m.src_host.resize(T * K); m.cnt.resize(m.g->n_expert); m.off.resize(m.g->n_expert + 1);
{
const size_t need = 3 * T * K + (size_t) (2 * (m.g->n_expert + m.g->n_expert / MMQ_GROUP + 2));
@@ -2116,8 +2162,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
else m.tok_host[(size_t) t] = (int32_t) tok;
}
if (batched) {
- if (cudaMemcpyAsync(m.tok_dev, m.tok_host.data(), (size_t) T * sizeof(int32_t), cudaMemcpyHostToDevice,
- m.cs) != cudaSuccess) {
+ if (!m.tok_up.upload(m.tok_dev, m.tok_host.data(), (size_t) T, m.cs)) {
err = "prefill: the token id upload failed";
return false;
}
@@ -2188,8 +2233,10 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
for (int64_t t = 0; t < T; ++t) { prev[0] = prev[1]; prev[1] = (int32_t) tokens[c0 + t]; }
// ---- the QSA step records of every position in the chunk
for (int64_t t = 0; t < T; ++t) strata::kernels::qsa_step_fill(m.steps_host.data() + t * strata::kernels::kStepCount, p0 + t, s);
- cudaMemcpyAsync(m.steps_dev, m.steps_host.data(), (size_t) T * strata::kernels::kStepCount * 4,
- cudaMemcpyHostToDevice, m.cs);
+ if (!m.steps_up.upload(m.steps_dev, m.steps_host.data(), (size_t) T * strata::kernels::kStepCount, m.cs)) {
+ err = "prefill: the QSA step records' upload failed";
+ return false;
+ }
int64_t qsa_index = 0, gdn_index = 0;
for (int64_t l = 0; l < LB; ++l) (core::is_qsa_layer(g, l) ? qsa_index : gdn_index) += 1;