Merge commit '9c282fbc5' into f-027-hip

This commit is contained in:
Niko1221 committed 2026-10-26 12:15:19 +01:00
commit 2d1db1ec6f
13 files changed
+234 -88

No files matched your search

+6 -3
View File
@@ -163,7 +163,7 @@ MtpDrafter::~MtpDrafter() {
if (owns_draft_head_ && dvocab_) cudaFree(dvocab_);
if (ev_chain_) cudaEventDestroy(ev_chain_);
for (cudaEvent_t e : ev_step_) if (e) cudaEventDestroy(e);
void* hosts[] = {h_tok_, h_step_, h_pos_, h_row_, h_out_, h_prob_, h_force_};
void* hosts[] = {h_tok_, h_step_, h_pos_, h_row_, h_out_, h_prob_, h_force_, h_floor_};
for (void* h : hosts) if (h) cudaFreeHost(h);
}
@@ -370,6 +370,7 @@ bool MtpDrafter::load(const std::string& rt_dir, const ModelGeometry& g, Session
// ---- buffers
window_ = (window > 0 && window < max_cells) ? window : 0;
if (floor_on_ && !mapped(64, (void**) &h_floor_, (void**) &m_floor_)) { err = "mtp: the window floor does not fit"; return false; }
cap_ = (((window_ > 0 ? window_ : max_cells) + 63) / 64) * 64;
attn_scratch_floats_ = (int64_t) strata::kernels::qsa_decode_attn_scratch_floats(cap_, s);
const uint64_t T = (uint64_t) max_t, N = (uint64_t) g.n_embd, HC = (uint64_t) g.hc, K = (uint64_t) ss.k;
@@ -1027,7 +1028,9 @@ bool MtpDrafter::record_rest(int step_row, cudaStream_t cs, std::string& err) {
if (st_.kv_rot) fwht256_inplace_cuda(qcur_, (int64_t) T * NH, cs);
g_mp.stamp(5, cs);
const QsaAttnPools pools = qsa_attn_pools(st_);
if (window_ > 0) window_ids(const_cast<int32_t*>(step), T, (int) window_, ident_, cap_, cs);
if (floor_on_ && m_floor_ != nullptr) // KVA: the window (or every cell) from the first real draft cell on
window_ids_floor(const_cast<int32_t*>(step), T, window_ > 0 ? (int) window_ : 0x7fffffff, ident_, cap_, m_floor_, cs);
else if (window_ > 0) window_ids(const_cast<int32_t*>(step), T, (int) window_, ident_, cap_, cs);
// STRATA_QSA_SPLIT_ATTN=1: the draft layer's attention over its window, split the same way
static const bool split_attn = [] { const char* v = std::getenv("STRATA_QSA_SPLIT_ATTN"); return v != nullptr && std::atoi(v) != 0; }();
if (!(split_attn && qsa_decode_attn_split(qcur_, pools, ident_, step, cap_, s, attn_scratch_, (uint64_t) attn_scratch_floats_, attn_, T, cs)))
@@ -1423,7 +1426,7 @@ bool MtpDrafter::prefill(const float* R_rows, const int32_t* next_tokens, int64_
const Clock::time_point t0 = Clock::now();
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;
const int64_t first_needed = this->first_needed();
// 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
+25
View File
@@ -6,6 +6,7 @@
#include "strata/core/geometry.hpp"
#include "strata/core/emulate.hpp"
#include "strata/kernels/q8_1_finite.hpp" // #606: q8_1_ds
#include "strata/kernels/window_start.hpp" // KVA: the draft layer's window floor
#include "strata/kernels/dp4a.hpp"
#include "strata/kernels/pdl.hpp"
@@ -1016,6 +1017,30 @@ void window_ids(int32_t* steps, int n, int window, int32_t* ids, int64_t ids_str
check("window_ids");
}
namespace {
// window_ids with a floor read from (mapped) memory when the kernel RUNS: the captured graph keeps the pointer, the
// value changes between sessions. A floor of 0 gives window_ids' selection exactly (strata::kernels::window_start).
__global__ void window_ids_floor_kernel(int32_t* steps, int window, int32_t* ids, long long stride,
const volatile int32_t* floor_cell) {
const int q = blockIdx.y;
int32_t* st = steps + q * 4;
const int n_kv = st[1];
const int start = strata::kernels::window_start(n_kv, window, *floor_cell);
const int width = n_kv - start;
for (int j = blockIdx.x * blockDim.x + threadIdx.x; j < width; j += gridDim.x * blockDim.x)
ids[q * stride + j] = start + j;
__syncthreads();
if (blockIdx.x == 0 && threadIdx.x == 0) st[3] = width;
}
} // namespace
void window_ids_floor(int32_t* steps, int n, int window, int32_t* ids, int64_t ids_stride, const int32_t* floor_cell,
void* stream) {
window_ids_floor_kernel<<<dim3(8, (unsigned) n), 256, 0, (cudaStream_t) stream>>>(steps, window, ids, (long long) ids_stride,
floor_cell);
check("window_ids_floor");
}
void dense_steps(const int32_t* cells, int n, int32_t* steps, void* stream) {
dense_steps_kernel<<<1, 64, 0, (cudaStream_t) stream>>>(cells, n, steps);
check("dense_steps");
+6 -26
View File
@@ -1,10 +1,13 @@
// src/prefill/kva_test.cpp - the KVA switch, the automatic tail and the reasons a read is turned away (no GPU needed).
#include "strata/kernels/window_start.hpp"
#include "strata/prefill/kva.hpp"
#include "strata/prefill/kva_floor.hpp"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
using namespace strata::prefill;
@@ -50,25 +53,13 @@ int main() {
{ const KvaConfig c = kva_config_from_env(); CHECK(c.on && !c.explicit_on); }
clear_all();
// ---- the automatic tail: 2048, or the draft layer's window + 64 when that is larger; STRATA_KVA_SUFFIX wins
// ---- the tail: 2048, or STRATA_KVA_SUFFIX; the draft window plays no part (the cell floor protects the draft layer)
{
const KvaConfig c = kva_config_from_env();
KvaEngine none; // no draft layer
CHECK(c.effective_suffix(none) == kKvaSuffixDefault);
KvaEngine d; // the window the engine picks (no --mtp-window given)
d.has_drafter = true;
d.drafter_window = kKvaDrafterWindow;
CHECK(c.effective_suffix(d) == 2112);
d.drafter_window = 8192; // --mtp-window 8192
CHECK(c.effective_suffix(d) == 8256);
d.drafter_window = 512; // a small window: the default tail still holds
CHECK(c.effective_suffix(d) == 2048);
d.drafter_window = 0; // every cell: no tail can cover it, the plain tail is returned
CHECK(c.effective_suffix(d) == 2048);
CHECK(c.effective_suffix() == kKvaSuffixDefault && kKvaSuffixDefault == 2048);
put("STRATA_KVA_SUFFIX", "3000");
const KvaConfig e = kva_config_from_env();
d.drafter_window = 8192;
CHECK(e.effective_suffix(d) == 3000);
CHECK(e.effective_suffix() == 3000);
clear_all();
}
@@ -76,8 +67,6 @@ int main() {
{
const KvaConfig c = kva_config_from_env();
KvaEngine eng;
eng.has_drafter = true;
eng.drafter_window = kKvaDrafterWindow;
bool routine = false;
// a cold 14.6K prompt on one device: applies
CHECK(c.decline(eng, 0, 14600, 0, -1, 48) == nullptr);
@@ -102,16 +91,7 @@ int main() {
put("STRATA_KVA", "1");
{ const KvaConfig f = kva_config_from_env(); CHECK(f.decline(eng, 0, 14600, 0, -1, 40, false, false, nullptr, true) == nullptr); }
clear_all();
// a draft layer that attends to every cell has no tail that covers it
KvaEngine all = eng;
all.drafter_window = 0;
CHECK(is(c.decline(all, 0, 14600, 0, -1, 48), "the draft layer attends to every cell (set --mtp-window)"));
put("STRATA_KVA_SUFFIX", "2112"); // ... unless the user fixes the tail by hand
{ const KvaConfig s = kva_config_from_env(); CHECK(s.decline(all, 0, 14600, 0, -1, 48) == nullptr); }
clear_all();
// no draft layer at all: nothing to cover
KvaEngine nod;
CHECK(c.decline(nod, 0, 14600, 0, -1, 48) == nullptr);
// the engine rules the whole process out (batch slots, a layer split, a peer device)
KvaEngine blocked = eng;
blocked.blocked = "batch slots (--batch)";
+3 -1
View File
@@ -2717,7 +2717,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
// stage, no capture). Positions [0, kva_frontier) are approximated in the layers at and after the split.
const KvaConfig& kva = kva_config();
const KvaEngine& kva_eng = kva_engine();
const int64_t kva_suffix = kva.effective_suffix(kva_eng);
const int64_t kva_suffix = kva.effective_suffix();
bool kva_routine = false;
const char* kva_why_not = kva.decline(kva_eng, pos0, n, LB, LE < 0 ? -1 : LE, (int64_t) g.n_layers,
embd_rows != nullptr, !tap_layers_.empty(), &kva_routine,
@@ -2728,6 +2728,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
static KvaTap kva_capture; // process-wide: one cold read at a time, so the per-layer position is enough
static KvaTap kva_oracle;
int64_t kva_positions = 0, kva_chunks = 0; // what actually took the approximate path (diagnostics)
kva_floor_ = 0; // this run's: raised below as chunks are approximated
if (kva.capture && kva_capture.f.empty()) kva_capture.open(kva.capture_dir, kva.capture_fp32);
if (kva.capture && pos0 == 0 && hand_in_ == nullptr) kva_capture.begin_prompt(); // one prompt per capture
if (kva_run && kva.mode == KvaConfig::Mode::Oracle && kva_oracle.f.empty())
@@ -2862,6 +2863,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
if (kva_approx) {
kva_positions += T;
++kva_chunks;
kva_floor_ = p0 + T; // the chunks are a prefix of the run: this is the first position with real rows
}
core::progress_at("reading the prompt (batched): preparing the chunk from token", p0); // #251
#if defined(STRATA_HAVE_DFLASH2)
+23 -19
View File
@@ -73,6 +73,7 @@
#include "strata/core/draft_cap.hpp"
#endif
#include "strata/prefill/kva.hpp"
#include "strata/prefill/kva_floor.hpp"
#include "strata/prefill/prefill.hpp"
#if defined(STRATA_PREFILL_MMQ)
#include "strata/prefill/moe_mmq.hpp"
@@ -682,7 +683,6 @@ struct Options {
/// Plan v0.3 P6: the MTP draft layer's runtime directory (tools/mtp_rt.py); drafts come from it.
std::string mtp;
int64_t mtp_window = 32768; ///< the draft layer attends to the last N cells (0 = every cell)
bool mtp_window_given = false; ///< --mtp-window was given (KVA picks the window itself only when it was not)
/// Plan v0.3 P6: the share (0..1) of each layer's distinct missed experts the GPU reads over PCIe from the
/// pinned arena while the CPU computes the rest (verify windows).
double pcie_frac = -1.0; ///< < 0: the model's default (0.2 direct for the Q2_0 pack, 0.55 DMA for native packs)
@@ -2645,7 +2645,7 @@ int main(int argc, char** argv) {
else if (a == "--spec-follow") o.spec_follow = next("--spec-follow");
else if (a == "--window-hashes") o.window_hashes = next("--window-hashes");
else if (a == "--mtp") o.mtp = next("--mtp");
else if (a == "--mtp-window") { o.mtp_window = std::atoll(next("--mtp-window")); o.mtp_window_given = true; }
else if (a == "--mtp-window") o.mtp_window = std::atoll(next("--mtp-window"));
else if (a == "--pcie-frac") o.pcie_frac = std::atof(next("--pcie-frac"));
else if (a == "--adapt-every") { o.adapt_every = std::atoi(next("--adapt-every")); o.adapt_every_given = true; }
else if (a == "--adapt-decay") o.adapt_decay = (float) std::atof(next("--adapt-decay"));
@@ -5532,9 +5532,9 @@ int main(int argc, char** argv) {
// KVA (0.2.7: on by default, STRATA_KVA=0 is the exact 0.2.6 path): what only the engine knows. A layer split and
// the batch slots read their prompts in pieces and in stages, which the one-device, one-cold-read rule of the
// approximate prompt read cannot follow, so they stay on the exact path (and say so here, once). The draft layer
// (MTP) must find exact cells in its window: with no --mtp-window given the window becomes 2048 cells (the 0.2.6
// default, 32768, is every cell of a chat up to that length, which no fully computed tail could cover) and the
// tail 2112; a window the user gave keeps its value and sets the tail (window + 64).
// (MTP) keeps its window, whatever it is: a session KVA approximated tells it the first cell built from a real row
// (the cell floor, set per request below) and every session KVA did not approximate attends exactly as with
// STRATA_KVA=0.
{
const strata::prefill::KvaConfig& kc = strata::prefill::kva_config();
strata::prefill::KvaEngine& ke = strata::prefill::kva_engine();
@@ -5549,15 +5549,8 @@ int main(int argc, char** argv) {
if (kc.explicit_on)
std::fprintf(stderr, "strata generate: KVA (the approximate late-layer prompt read) is off here: %s\n",
ke.blocked.c_str());
} else if (!o.mtp.empty() && o.spec >= 2 && !o.mtp_window_given && kc.suffix_env <= 0) {
if (o.mtp_window != strata::prefill::kKvaDrafterWindow)
std::fprintf(stderr, "strata generate: KVA on (the faster prompt read for long cold prompts): the draft layer "
"attends to the last %lld cells (--mtp-window %lld); STRATA_KVA=0 keeps the "
"exact 0.2.6 prompt read and its %lld-cell window\n",
(long long) strata::prefill::kKvaDrafterWindow, (long long) strata::prefill::kKvaDrafterWindow,
(long long) o.mtp_window);
o.mtp_window = strata::prefill::kKvaDrafterWindow;
}
mtp.enable_cell_floor(kc.wants_cell_floor(ke, !o.mtp.empty() && o.spec >= 2));
}
}
if (!o.mtp.empty()) {
@@ -5579,11 +5572,6 @@ int main(int argc, char** argv) {
if (o.pipeline_windows >= 2) mtp.set_force_capture(true);
if (!o.mtp.empty() && !mtp.load(o.mtp, draft_geometry, last_st && !mtp_first ? last_st->ss : ss, mtp_t, err, o.mtp_window)) { std::fprintf(stderr, "strata generate: %s%s\n", err.c_str(), vram_free_note().c_str()); return 1; }
mtp.set_ple_session(&ss);
if (!o.mtp.empty()) { // KVA: the cells the draft layer attends to now (0 = every cell) set the tail that must be exact
strata::prefill::KvaEngine& ke = strata::prefill::kva_engine();
ke.has_drafter = true;
ke.drafter_window = mtp.window();
}
#if defined(STRATA_HAVE_DFLASH2)
if (o.draft == "dflash2") {
const char* why = o.mtp.empty() ? "it needs --mtp (the fallback) and --spec 2 or more"
@@ -9493,6 +9481,9 @@ int main(int argc, char** argv) {
// a reporter's prompts failed there every time and passed with --short-read 0.
bool nf_prompt_windows = false, short_read_off_once = false;
std::vector<ConvCheckpoint> checks;
// KVA: which prefixes of token ids a KVA read approximated, so the draft layer knows, for whatever state a request
// continues from, the first cell built from a real row (kva_floor.hpp). Empty without KVA.
strata::prefill::KvaFloorRegistry kva_floors;
// STRATA_CKPT_POOL: the dropped checkpoints' storage, reused. STRATA_SLOT_CKPT_MOVE turns it on too: a moved
// checkpoint frees nothing at the next prune, so every save would fault in fresh pages (17 -> 40-60 ms)
CheckpointPool ckpt_pool(ckpt_pool_env() || slot_ckpt_move_env());
@@ -10002,6 +9993,9 @@ int main(int argc, char** argv) {
// come from Q8_1 x Q8_0 MMQ rather than mmvq: drafts may differ, the target's tokens never.
static const bool split_mtp_batch = strata::program::draft_kv::split_env_on(std::getenv("STRATA_SPLIT_MTP_BATCH"));
sp.on_chunk = [&](const float* R_rows, int64_t T, int64_t p0, std::string& e) -> bool {
// KVA: a chunk the late layers did not compute has no real final rows: its draft cells are neither built
// nor read (the floor rises before the cells are built, so they are skipped)
if (use_mtp && mtp.cell_floor_enabled() && sp.kva_floor() > mtp.cell_floor()) mtp.set_cell_floor(sp.kva_floor());
std::vector<int32_t> nxt((size_t) T);
for (int64_t t = 0; t < T; ++t) nxt[(size_t) t] = (int32_t) cur[(size_t) (p0 + t + 1)];
// E-9: batched through the prompt path when it can (a layer split: only with STRATA_SPLIT_MTP_BATCH=1)
@@ -13181,6 +13175,10 @@ int main(int argc, char** argv) {
tr("request", n, geni ? 1 : 0);
req_times = ReqTimes{};
if (use_mtp) mtp.set_prompt_len(n);
// KVA: the draft layer's first real cell for the state this request continues from (0 for a cold read - the
// first segment raises it if KVA approximates it - and for any state no KVA read produced)
if (use_mtp && mtp.cell_floor_enabled())
mtp.set_cell_floor(resume > 0 && reread_to <= 0 ? kva_floors.floor_for(ids.data(), resume) : 0);
const int64_t read_from = reread_to > 0 ? 0 : resume;
conversations.limit_reuse(read_from);
pp_total = n;
@@ -13623,7 +13621,12 @@ int main(int argc, char** argv) {
} seg_plan_scope{seg_plan};
auto run = [&](int64_t q, int64_t r) -> bool {
if (planned) sp.force_chunk(plan.chunk, plan_single, plan.chunked);
return sp.run(ids.data() + q, r - q, q, e);
const bool run_ok = sp.run(ids.data() + q, r - q, q, e);
if (run_ok && q == 0 && sp.kva_floor() > 0) { // KVA approximated the start of this prompt: remember which tokens
kva_floors.note(ids.data(), sp.kva_floor());
if (use_mtp && mtp.cell_floor_enabled()) mtp.set_cell_floor(sp.kva_floor());
}
return run_ok;
};
// (a layer split reads its stages as a pipeline over one run's chunks: beside slots a chunk at a time, with
// none decoding in pieces of STRATA_BATCH_YIELD_CHUNKS chunks (default 8; 0 = one run) so a BYIELD is taken)
@@ -15964,6 +15967,7 @@ int main(int argc, char** argv) {
return 1;
}
prefill.on_chunk = [&](const float* R_rows, int64_t T, int64_t p0, std::string& e) -> bool {
if (mtp.cell_floor_enabled() && prefill.kva_floor() > mtp.cell_floor()) mtp.set_cell_floor(prefill.kva_floor()); // KVA
// cell i pairs R_i with the token at i + 1 (every such token is in the prompt)
std::vector<int32_t> nxt((size_t) T);
for (int64_t t = 0; t < T; ++t) nxt[(size_t) t] = (int32_t) o.tokens[(size_t) (p0 + t + 1)];