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

E-4 (prompt side): the draft layer's pass over a prompt chunk without a host sync per group

Niko1221459a1d15e6b1
diff --git a/include/strata/core/mtp.hpp b/include/strata/core/mtp.hpp
index dbdebb3f3..29f6b14ac 100644
--- a/include/strata/core/mtp.hpp
+++ b/include/strata/core/mtp.hpp
@@ -76,6 +76,7 @@ public:
private:
bool record_forward(int T, int step_row0, cudaStream_t cs, std::string& err);
bool capture_prefill(int T, std::string& err);
+ bool capture_prefill_dev(int T, std::string& err); ///< E-4: without the mapped staging (inputs copied on device)
bool capture_round(int T, std::string& err);
bool capture_step(int j, std::string& err);
cudaGraphExec_t step_exec_[9] = {};
@@ -95,6 +96,9 @@ private:
uint64_t vram_ = 0;
cudaStream_t cs_ = nullptr;
cudaGraphExec_t prefill_exec_[9] = {};
+ cudaGraphExec_t prefill_dev_exec_[9] = {};
+ 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
cudaGraphExec_t round_exec_[9] = {};
struct Tensor { std::string name, kind; int64_t rows = 0, cols = 0; uint64_t off = 0, bytes = 0; };
diff --git a/src/core/mtp.cpp b/src/core/mtp.cpp
index 401085d17..2944a5c3a 100644
--- a/src/core/mtp.cpp
+++ b/src/core/mtp.cpp
@@ -83,6 +83,8 @@ bool read_file(const std::string& path, std::vector<uint8_t>& out) {
MtpDrafter::~MtpDrafter() {
if (cs_) cudaStreamSynchronize(cs_);
for (auto& e : prefill_exec_) if (e) cudaGraphExecDestroy(e);
+ for (auto& e : prefill_dev_exec_) if (e) cudaGraphExecDestroy(e);
+ if (pf_dev_) cudaFree(pf_dev_);
for (auto& e : round_exec_) if (e) cudaGraphExecDestroy(e);
for (auto& e : step_exec_) if (e) cudaGraphExecDestroy(e);
if (cs_) cudaStreamDestroy(cs_);
@@ -480,6 +482,13 @@ bool MtpDrafter::capture_prefill(int T, std::string& err) {
return finish_capture(cs_, ok, prefill_exec_[T], "prefill", err);
}
+bool MtpDrafter::capture_prefill_dev(int T, std::string& err) {
+ if (prefill_dev_exec_[T]) return true;
+ if (cudaStreamBeginCapture(cs_, cudaStreamCaptureModeThreadLocal) != cudaSuccess) { err = "mtp: begin capture"; return false; }
+ const bool ok = record_forward(T, -1, cs_, err); // K/V only, rows [0, T); tok_/step_/pos_ filled before launch
+ return finish_capture(cs_, ok, prefill_dev_exec_[T], "prefill (device inputs)", err);
+}
+
bool MtpDrafter::capture_round(int T, std::string& err) {
if (round_exec_[T]) return true;
using namespace strata::kernels;
@@ -537,6 +546,64 @@ bool MtpDrafter::prefill(const float* R_rows, const int32_t* next_tokens, int64_
const int64_t HCN = g_->hc * g_->n_embd;
// cells the window can never reach again need no K/V
const int64_t first_needed = (window_ > 0 && prompt_len_ > 0) ? prompt_len_ - window_ - 64 : 0;
+ // E-4: every group's token / step / position records uploaded at once; each group is then device copies and a
+ // graph on the one stream, with a single sync at the end (a group of <= max_t rows used to be staged in mapped
+ // memory and synced before the next: ~5,500 host round trips on a 32K prompt). The same work in the same
+ // order. STRATA_MTP_PREFILL_SYNC=1 keeps the old loop.
+ static const bool per_group_sync = std::getenv("STRATA_MTP_PREFILL_SYNC") != nullptr;
+ const int64_t NHp = g_->n_head, per_row = 1 + 4 + NHp;
+ if (!per_group_sync && n > 0) {
+ if (pf_cap_ < n * per_row) {
+ if (pf_dev_) cudaFree(pf_dev_);
+ pf_dev_ = nullptr;
+ pf_cap_ = 0;
+ if (cudaMalloc((void**) &pf_dev_, (size_t) (n * per_row) * sizeof(int32_t)) != cudaSuccess) {
+ err = "mtp prefill: the input records do not fit";
+ return false;
+ }
+ pf_cap_ = n * per_row;
+ }
+ std::vector<int32_t> rec((size_t) (n * per_row));
+ int32_t* tk = rec.data();
+ int32_t* stp = tk + n;
+ int32_t* ps = stp + 4 * n;
+ for (int64_t i = 0; i < n; ++i) {
+ const int64_t cell = cell0 + i;
+ tk[i] = next_tokens[i];
+ stp[i * 4 + 0] = (int32_t) cell;
+ stp[i * 4 + 1] = (int32_t) (cell + 1);
+ stp[i * 4 + 2] = (int32_t) ((cell + 1) / 4);
+ 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) {
+ err = "mtp prefill: the input records upload failed";
+ return false;
+ }
+ const int32_t* d_tk = pf_dev_;
+ const int32_t* d_stp = d_tk + n;
+ const int32_t* d_ps = d_stp + 4 * n;
+ for (int64_t c = 0; c < n; c += max_t_) {
+ const int T = (int) std::min<int64_t>(max_t_, n - c);
+ if (cell0 + c + T <= first_needed) continue;
+ if (!capture_prefill_dev(T, err)) return false;
+ if (cudaMemcpyAsync(tok_, d_tk + c, (size_t) T * 4, cudaMemcpyDeviceToDevice, cs_) != cudaSuccess ||
+ cudaMemcpyAsync(step_, d_stp + c * 4, (size_t) T * 16, cudaMemcpyDeviceToDevice, cs_) != cudaSuccess ||
+ cudaMemcpyAsync(pos_, d_ps + c * NHp, (size_t) (T * NHp) * 4, cudaMemcpyDeviceToDevice, cs_) != cudaSuccess ||
+ cudaMemcpyAsync(Rin_, R_rows + (size_t) c * HCN, (size_t) T * HCN * sizeof(float), cudaMemcpyDeviceToDevice,
+ cs_) != cudaSuccess ||
+ cudaGraphLaunch(prefill_dev_exec_[T], cs_) != cudaSuccess) {
+ err = std::string("mtp prefill: ") + cudaGetErrorString(cudaGetLastError());
+ return false;
+ }
+ }
+ if (cudaStreamSynchronize(cs_) != cudaSuccess) {
+ err = std::string("mtp prefill: ") + cudaGetErrorString(cudaGetLastError());
+ return false;
+ }
+ ms_prefill += ms_since(t0);
+ return true;
+ }
for (int64_t c = 0; c < n; c += max_t_) {
const int T = (int) std::min<int64_t>(max_t_, n - c);
if (cell0 + c + T <= first_needed) continue;