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

Prompt path: every pageable async host-to-device copy goes up through pinned bounce slots (#1996)

Niko12217bab660bf8cb
diff --git a/include/strata/core/mtp.hpp b/include/strata/core/mtp.hpp
index 5f33fdd4a..6acf16ae9 100644
--- a/include/strata/core/mtp.hpp
+++ b/include/strata/core/mtp.hpp
@@ -20,6 +20,7 @@
#pragma once
#include "strata/core/layer.hpp"
+#include "strata/core/pinned_ring.hpp"
#include "strata/core/session.hpp"
#include "strata/kernels/sampler.hpp"
@@ -227,6 +228,7 @@ private:
cudaStream_t cs_ = nullptr;
cudaGraphExec_t prefill_exec_[9] = {};
cudaGraphExec_t prefill_dev_exec_[9] = {};
+ PinnedRing pf_ring_; ///< #1996: the records above go up from a pinned slot, not from the pageable vector they are built in
int32_t* pf_dev_ = nullptr; ///< E-4: a prompt's rows' token / step / position records, uploaded at once
int64_t pf_cap_ = 0; ///< its capacity in ints
cudaStream_t side_ = nullptr; // the shared expert's branch of the draft graphs (CUDA)
diff --git a/include/strata/core/pinned_ring.hpp b/include/strata/core/pinned_ring.hpp
new file mode 100644
index 000000000..7b74a24fa
--- /dev/null
+++ b/include/strata/core/pinned_ring.hpp
@@ -0,0 +1,147 @@
+// #1996 (0.1.43.1): host-to-device uploads whose host side is pageable go up through pinned bounce slots.
+//
+// A cudaMemcpyAsync from pageable memory is staged by the runtime, and the staging is not ordered the way a pinned copy
+// is. On gfx1100 (ROCm 7.2.4) a text prompt after a picture one read stale bytes that way (#1712, fixed for the step
+// records and the token ids in 0.1.43); a 4-hour multi-slot load (#1996) still faulted after 31 minutes, with the
+// image rows, the draft tokens and bounds and the non-mapped routing tables still going up from caller memory. The
+// analysis of which copies those are, and the idea of a ring of pinned slots with one event each, are
+// mantovaniluca91's (patch C on #1712); this is our own form of it.
+//
+// A PinnedRing owns a few pinned slots, each with an event. h2d() copies the bytes into the next slot (after the copy
+// that last read that slot is done), queues the DMA from the slot and records the slot's event. So the caller may
+// overwrite or free its source the moment the call returns, as a pageable copy allowed. With no pinned RAM (or
+// STRATA_TEST_PAGEABLE=1) the copy is made from the caller's memory and the stream is waited for, so the source is
+// free on return all the same. A ring belongs to ONE device (the one current when it is first used) and is used by one
+// thread at a time.
+#pragma once
+
+#include <cuda_runtime.h>
+
+#include <cstddef>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+
+namespace strata::core {
+
+class PinnedRing {
+public:
+ static constexpr int kSlots = 4;
+
+ PinnedRing() = default;
+ PinnedRing(const PinnedRing&) = delete;
+ PinnedRing& operator=(const PinnedRing&) = delete;
+ ~PinnedRing() { release(); }
+
+ // dst (device) <- bytes from src (host, any kind of memory); src is free to change when this returns
+ cudaError_t h2d(void* dst, const void* src, size_t bytes, cudaStream_t s) {
+ if (bytes == 0) return cudaSuccess;
+ void* stage = acquire(bytes);
+ if (stage == nullptr) return direct(dst, src, bytes, s);
+ std::memcpy(stage, src, bytes);
+ return submit(dst, stage, bytes, s);
+ }
+
+ // For a caller that gathers many small pieces into one upload: acquire() hands out a pinned slot of at least
+ // `bytes` (nullptr: none could be had - use direct()), the caller fills it, then submit() queues the copy and
+ // guards the slot. An acquired slot must be submitted before the next acquire().
+ void* acquire(size_t bytes) {
+ if (bytes == 0 || pageable_forced()) return nullptr;
+ int cur = -1;
+ if (cudaGetDevice(&cur) != cudaSuccess) { cudaGetLastError(); return nullptr; }
+ if (dev_ < 0) dev_ = cur;
+ if (cur != dev_) return nullptr; // a ring is for one device
+ Slot& sl = slots_[next_];
+ next_ = (next_ + 1) % kSlots;
+ if (sl.pending) { // the copy that last read this slot is done before it is refilled
+ sl.pending = false;
+ if (cudaEventSynchronize(sl.ev) != cudaSuccess) { cudaGetLastError(); return nullptr; }
+ }
+ if (sl.cap < bytes) {
+ if (sl.p) { cudaFreeHost(sl.p); sl.p = nullptr; sl.cap = 0; } // idle: its last copy was waited for above
+ const size_t cap = bytes < kMinCap ? kMinCap : bytes;
+ if (cudaHostAlloc(&sl.p, cap, cudaHostAllocPortable) != cudaSuccess) {
+ cudaGetLastError();
+ sl.p = nullptr;
+ return nullptr;
+ }
+ sl.cap = cap;
+ }
+ if (sl.ev == nullptr && cudaEventCreateWithFlags(&sl.ev, cudaEventDisableTiming) != cudaSuccess) {
+ cudaGetLastError();
+ sl.ev = nullptr;
+ return nullptr;
+ }
+ acquired_ = &sl;
+ return sl.p;
+ }
+
+ cudaError_t submit(void* dst, const void* stage, size_t bytes, cudaStream_t s) {
+ Slot* sl = acquired_;
+ acquired_ = nullptr;
+ cudaError_t e = cudaMemcpyAsync(dst, stage, bytes, cudaMemcpyHostToDevice, s);
+ if (e != cudaSuccess) {
+ std::fprintf(stderr, "strata: pinned upload of %zu bytes failed: %s\n", bytes, cudaGetErrorString(e));
+ return e;
+ }
+ if (sl != nullptr && cudaEventRecord(sl->ev, s) == cudaSuccess) {
+ sl->pending = true;
+ return cudaSuccess;
+ }
+ cudaGetLastError(); // no event: the copy is waited for here instead
+ return cudaStreamSynchronize(s);
+ }
+
+ // the same copy without a pinned slot: from the caller's memory, the stream waited for
+ static cudaError_t direct(void* dst, const void* src, size_t bytes, cudaStream_t s) {
+ if (bytes == 0) return cudaSuccess;
+ cudaError_t e = cudaMemcpyAsync(dst, src, bytes, cudaMemcpyHostToDevice, s);
+ if (e == cudaSuccess) e = cudaStreamSynchronize(s);
+ if (e != cudaSuccess) std::fprintf(stderr, "strata: upload of %zu bytes failed: %s\n", bytes, cudaGetErrorString(e));
+ return e;
+ }
+
+ // dst (pageable host) <- device bytes; the stream is drained and the bytes are in dst on return
+ cudaError_t d2h_sync(void* dst, const void* src, size_t bytes, cudaStream_t s) {
+ if (bytes == 0) return cudaSuccess;
+ void* stage = acquire(bytes);
+ cudaError_t e = cudaMemcpyAsync(stage != nullptr ? stage : dst, src, bytes, cudaMemcpyDeviceToHost, s);
+ if (e == cudaSuccess) e = cudaStreamSynchronize(s);
+ if (e == cudaSuccess && stage != nullptr) std::memcpy(dst, stage, bytes);
+ if (stage != nullptr) acquired_ = nullptr; // no event: the stream was drained
+ if (e != cudaSuccess) std::fprintf(stderr, "strata: download of %zu bytes failed: %s\n", bytes, cudaGetErrorString(e));
+ return e;
+ }
+
+ void release() {
+ for (Slot& sl : slots_) {
+ if (sl.pending && sl.ev) cudaEventSynchronize(sl.ev);
+ if (sl.ev) cudaEventDestroy(sl.ev);
+ if (sl.p) cudaFreeHost(sl.p);
+ sl = Slot{};
+ }
+ cudaGetLastError();
+ acquired_ = nullptr;
+ dev_ = -1;
+ next_ = 0;
+ }
+
+private:
+ static constexpr size_t kMinCap = 64 * 1024;
+ struct Slot {
+ void* p = nullptr;
+ size_t cap = 0;
+ cudaEvent_t ev = nullptr;
+ bool pending = false;
+ };
+ static bool pageable_forced() {
+ static const bool v = [] { const char* e = std::getenv("STRATA_TEST_PAGEABLE"); return e != nullptr && e[0] == '1'; }();
+ return v;
+ }
+ Slot slots_[kSlots];
+ Slot* acquired_ = nullptr;
+ int next_ = 0;
+ int dev_ = -1;
+};
+
+} // namespace strata::core
diff --git a/src/core/mtp.cpp b/src/core/mtp.cpp
index 3ed034d7f..d37163bec 100644
--- a/src/core/mtp.cpp
+++ b/src/core/mtp.cpp
@@ -1195,7 +1195,7 @@ bool MtpDrafter::prefill(const float* R_rows, const int32_t* next_tokens, int64_
stp[i * 4 + 3] = (int32_t) (cell + 1);
for (int64_t h = 0; h < NHp; ++h) ps[i * NHp + h] = (int32_t) cell;
}
- if (cudaMemcpyAsync(pf_dev_, rec.data(), rec.size() * sizeof(int32_t), cudaMemcpyHostToDevice, cs_) != cudaSuccess) {
+ if (pf_ring_.h2d(pf_dev_, rec.data(), rec.size() * sizeof(int32_t), cs_) != cudaSuccess) {
err = "mtp prefill: the input records upload failed";
return false;
}
diff --git a/src/prefill/prefill.cpp b/src/prefill/prefill.cpp
index 15078dbf1..841644452 100644
--- a/src/prefill/prefill.cpp
+++ b/src/prefill/prefill.cpp
@@ -7,6 +7,7 @@
#include "mmq_resident_sort.hpp"
#include "wmma_gemm.h"
#include "strata/core/mtp.hpp"
+#include "strata/core/pinned_ring.hpp"
#include "strata/core/progress.hpp"
#include "strata/core/on_device.hpp"
#include "strata/platform/aux_cpus.hpp"
@@ -657,6 +658,7 @@ struct PeerPrefill {
float *host_w = nullptr, *host_sum = nullptr;
int32_t* host_pair = nullptr;
std::vector<int32_t> bounds_host;
+ core::PinnedRing up_ring; // #1996: this device's pageable uploads (bounds, routed rows) go up from pinned slots
std::vector<void*> owned;
int64_t layers = 0, experts = 0, rows = 0, over_cap = 0; // stats
~PeerPrefill() {
@@ -666,6 +668,7 @@ struct PeerPrefill {
cudaSetDevice(dev);
if (s) cudaStreamSynchronize(s);
ctx.reset();
+ up_ring.release();
for (void* p : owned) cudaFree(p);
if (ev_done) cudaEventDestroy(ev_done);
if (s_out) cudaStreamSynchronize(s_out);
@@ -856,6 +859,7 @@ struct Prefill::Impl {
int32_t* tok_dev = nullptr;
std::vector<int32_t> tok_host;
PinnedUpload tok_up;
+ core::PinnedRing up_ring; // #1996: every other upload of the prompt path whose host side is pageable
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
@@ -933,6 +937,7 @@ void Prefill::release() {
if (impl_->grp_host) cudaFreeHost(impl_->grp_host);
impl_->steps_up.release();
impl_->tok_up.release();
+ impl_->up_ring.release();
for (float* p : {impl_->cpu_x, impl_->cpu_rows})
if (p) cudaFreeHost(p);
for (cudaEvent_t e : impl_->cpu_ev)
@@ -1444,11 +1449,12 @@ bool Prefill::draft_kv(core::MtpDrafter& mtp, const float* R_rows, const int32_t
const int64_t nb = std::min(B, n - b0), c0 = cell0 + b0;
for (int64_t i = 0; i < nb; ++i) tk[(size_t) i] = next_tokens[b0 + i];
const int32_t bh[4] = {0, (int32_t) nb, 0, (int32_t) (nb * g.hc)};
- if (q8 && cudaMemcpyAsync(bnd, bh, sizeof bh, cudaMemcpyHostToDevice, m.cs) != cudaSuccess) {
+ // #1996: bh is on this stack and tk is refilled every pass: both go up through pinned slots
+ if (q8 && m.up_ring.h2d(bnd, bh, sizeof bh, m.cs) != cudaSuccess) {
err = "prefill: the draft bounds' upload failed";
return false;
}
- if (cudaMemcpyAsync(tok, tk.data(), (size_t) nb * 4, cudaMemcpyHostToDevice, m.cs) != cudaSuccess) {
+ if (m.up_ring.h2d(tok, tk.data(), (size_t) nb * 4, m.cs) != cudaSuccess) {
err = "prefill: the draft tokens' upload failed";
return false;
}
@@ -2179,15 +2185,32 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
(uint64_t) (wemb->ne0 / wemb->group_elems), m.emb, m.cs);
}
}
- for (int64_t t = 0; hand_in_ == nullptr && !batched && t < T; ++t) {
+ for (int64_t t = 0; hand_in_ == nullptr && !batched && t < T;) {
const float* row = embd_rows ? embd_rows[p0 + t] : nullptr;
if (row) {
- if (cudaMemcpyAsync(m.emb + t * N, row, (size_t) N * 4, cudaMemcpyHostToDevice, m.cs) != cudaSuccess) {
+ // #1996: the picture's rows live in the caller's (pageable) memory. A run of consecutive rows is
+ // gathered into one pinned slot and goes up as one copy; the slot is guarded by its event.
+ const int64_t row_bytes = N * 4, run_max = std::max<int64_t>(1, (4 << 20) / row_bytes);
+ int64_t t1 = t + 1;
+ while (t1 < T && t1 - t < run_max && embd_rows[p0 + t1]) ++t1;
+ void* stage = m.up_ring.acquire((size_t) ((t1 - t) * row_bytes));
+ cudaError_t e = cudaSuccess;
+ if (stage != nullptr) {
+ for (int64_t r = t; r < t1; ++r)
+ std::memcpy((char*) stage + (size_t) ((r - t) * row_bytes), embd_rows[p0 + r], (size_t) row_bytes);
+ e = m.up_ring.submit(m.emb + t * N, stage, (size_t) ((t1 - t) * row_bytes), m.cs);
+ } else { // no pinned RAM: row by row from the caller's memory, each waited for
+ for (int64_t r = t; r < t1 && e == cudaSuccess; ++r)
+ e = core::PinnedRing::direct(m.emb + r * N, embd_rows[p0 + r], (size_t) row_bytes, m.cs);
+ }
+ if (e != cudaSuccess) {
err = "prefill: the image embedding upload failed";
return false;
}
- } else if (!core::embed_row(*m.wt, g, tokens[c0 + t], m.emb + t * N, m.cs, err)) {
- return false;
+ t = t1;
+ } else {
+ if (!core::embed_row(*m.wt, g, tokens[c0 + t], m.emb + t * N, m.cs, err)) return false;
+ ++t;
}
}
if (hand_in_ == nullptr) gr_broadcast(m.emb, m.R, T, m.cs);
@@ -2209,7 +2232,10 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
err = ple_next_err;
return false;
}
- if (cudaMemcpyAsync(m.ple_emb, m.ple_emb_host[ple_buf], (size_t) T * N * 4, cudaMemcpyHostToDevice, m.cs) !=
+ // #1996: the pageable fallback (no RAM to pin) goes up through a pinned slot; the pinned buffers as before
+ if ((m.ple_pageable[ple_buf].empty()
+ ? cudaMemcpyAsync(m.ple_emb, m.ple_emb_host[ple_buf], (size_t) T * N * 4, cudaMemcpyHostToDevice, m.cs)
+ : m.up_ring.h2d(m.ple_emb, m.ple_emb_host[ple_buf], (size_t) T * N * 4, m.cs)) !=
cudaSuccess ||
cudaEventRecord(m.ple_copied[ple_buf], m.cs) != cudaSuccess) {
err = std::string("prefill: the PLE rows' upload failed: ") + cudaGetErrorString(cudaGetLastError());
@@ -3052,7 +3078,10 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
int32_t* slot_h = grp_mapped ? m.grp_host + m.grp_tk : m.slot_host.data();
int32_t* src_h = grp_mapped ? m.grp_host + 2 * m.grp_tk : m.src_host.data();
if (grp_mapped) copy_i32(m.grp_dev, m.ids, T * K, m.cs);
- else cudaMemcpyAsync(m.ids_host.data(), m.ids, (size_t) T * K * 4, cudaMemcpyDeviceToHost, m.cs);
+ else if (m.up_ring.d2h_sync(m.ids_host.data(), m.ids, (size_t) T * K * 4, m.cs) != cudaSuccess) {
+ err = "prefill: the routed ids' download failed"; // #1996
+ return false;
+ }
bool cpu_maybe = pool_hold.held && !stream_all && m.src != nullptr && !m.cpu_dead &&
lay.native && !m.pp && !lay.fmt.empty();
// The share's two pinned buffers are small (~50 MB) but are had mid-request, when the host may have
@@ -3271,8 +3300,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
copy_i32(m.slot_dev, m.grp_dev + m.grp_tk, T * K, m.cs);
copy_i32(m.src_dev, m.grp_dev + 2 * m.grp_tk, T * K, m.cs);
} else {
- cudaMemcpyAsync(m.slot_dev, m.slot_host.data(), (size_t) T * K * 4, cudaMemcpyHostToDevice, m.cs);
- cudaMemcpyAsync(m.src_dev, m.src_host.data(), (size_t) T * K * 4, cudaMemcpyHostToDevice, m.cs);
+ if (m.up_ring.h2d(m.slot_dev, m.slot_host.data(), (size_t) T * K * 4, m.cs) != cudaSuccess || // #1996
+ m.up_ring.h2d(m.src_dev, m.src_host.data(), (size_t) T * K * 4, m.cs) != cudaSuccess) {
+ err = "prefill: the routing tables' upload failed";
+ return false;
+ }
}
n_order = order.size();
// set_cpu_pool: the CPU's experts on a thread while this one streams and runs the GPU's; their
@@ -3355,8 +3387,10 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
std::memcpy(bh, m.bounds_host.data(), m.bounds_host.size() * 4);
copy_i32(m.bounds_dev, m.grp_dev + 3 * m.grp_tk, (int64_t) m.bounds_host.size(), m.cs);
} else {
- cudaMemcpyAsync(m.bounds_dev, m.bounds_host.data(), m.bounds_host.size() * 4,
- cudaMemcpyHostToDevice, m.cs);
+ if (m.up_ring.h2d(m.bounds_dev, m.bounds_host.data(), m.bounds_host.size() * 4, m.cs) != cudaSuccess) { // #1996
+ err = "prefill: the group bounds' upload failed";
+ return false;
+ }
}
} else {
gather_rows16(m.mixed_h, m.src_dev, m.Xs, T * K, N, m.cs);
@@ -3394,8 +3428,15 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
P.back_at = rows_local;
P.back_rows = rows_peer;
if (P.p2p) {
- cudaMemcpyAsync(P.src, src_h + rows_local, // multi-GPU: the mapped table when on
- (size_t) rows_peer * 4, cudaMemcpyHostToDevice, ps);
+ // multi-GPU: the mapped table when on; src_host (a pageable vector) goes up from a pinned slot (#1996)
+ if ((grp_mapped ? cudaMemcpyAsync(P.src, src_h + rows_local, (size_t) rows_peer * 4,
+ cudaMemcpyHostToDevice, ps)
+ : P.up_ring.h2d(P.src, src_h + rows_local, (size_t) rows_peer * 4, ps)) !=
+ cudaSuccess) {
+ cudaSetDevice(prevd);
+ err = "prefill: the peer's routing table upload failed";
+ return false;
+ }
} else {
std::memcpy(P.host_src, src_h + rows_local, (size_t) rows_peer * 4);
copy_i32(P.src, P.host_src, rows_peer, ps);
@@ -3429,7 +3470,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
P.bounds_host[std::min(P.groups[g2].second, P.groups[g2].first + i)] -
P.bounds_host[P.groups[g2].first];
if (P.p2p || P.bounds_host.size() > PeerPrefill::kHostBounds) {
- cudaMemcpyAsync(P.bounds, P.bounds_host.data(), P.bounds_host.size() * 4, cudaMemcpyHostToDevice, ps);
+ if (P.up_ring.h2d(P.bounds, P.bounds_host.data(), P.bounds_host.size() * 4, ps) != cudaSuccess) { // #1996
+ cudaSetDevice(prevd);
+ err = "prefill: the peer's group bounds upload failed";
+ return false;
+ }
} else {
std::memcpy(P.host_bounds, P.bounds_host.data(), P.bounds_host.size() * 4);
copy_i32(P.bounds, P.host_bounds, (int64_t) P.bounds_host.size(), ps);
@@ -3560,7 +3605,11 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
P.bounds_host[n + 1 + g2 * (MMQ_GROUP + 1) + i] =
P.bounds_host[std::min(n, g2 * MMQ_GROUP + i)] - P.bounds_host[g2 * MMQ_GROUP];
if (P.p2p || P.bounds_host.size() > PeerPrefill::kHostBounds) {
- cudaMemcpyAsync(P.bounds, P.bounds_host.data(), P.bounds_host.size() * 4, cudaMemcpyHostToDevice, ps);
+ if (P.up_ring.h2d(P.bounds, P.bounds_host.data(), P.bounds_host.size() * 4, ps) != cudaSuccess) { // #1996
+ cudaSetDevice(prevd);
+ err = "prefill: the peer's group bounds upload failed";
+ return false;
+ }
} else {
std::memcpy(P.host_bounds, P.bounds_host.data(), P.bounds_host.size() * 4);
copy_i32(P.bounds, P.host_bounds, (int64_t) P.bounds_host.size(), ps);