Merge f-027-kva (KVA ships off in 0.2.7: STRATA_KVA unset is the exact 0.2.6 engine; STRATA_KVA=1 approximates layers 36-47 with a 2048-token exact tail, the configuration that kept agent loops and long extraction at the exact level; geometry gate; draft fixes; opt-in STRATA_KVA_SKIP=moe)
This commit is contained in:
commit
9c059bade1
5 files changed
+164
-78
No files matched your search
+45
-20
@@ -1,30 +1,53 @@
|
||||
# KVA: the faster prompt read for long cold prompts (on by default since 0.2.7)
|
||||
# KVA: a faster prompt read for long cold prompts (opt-in, off by default)
|
||||
|
||||
KVA ("approximate late-layer state") reads a long prompt exactly through layer 23, then for the late layers
|
||||
24–47 builds **only their recurrent state** — the GDN recurrence and convolution history, the QSA K/V pages and
|
||||
the indexer keys — instead of running the whole layer. The attention output, the output projection, the MoE half
|
||||
KVA ("approximate late-layer state") reads a long prompt exactly through the early layers, then for the late layers
|
||||
builds **only their recurrent state** — the GDN recurrence and convolution history, the QSA K/V pages and the
|
||||
indexer keys — instead of running the whole layer. The attention output, the output projection, the MoE half
|
||||
(router, experts, shared expert) and the late layers' expert transfers are skipped, because nothing reads their
|
||||
results. The last positions of the prompt are then read fully, exactly as before, and decode runs the ordinary
|
||||
full path against the state KVA built.
|
||||
|
||||
It is an approximation: the answers can differ from the exact read (see "Measured" for how much). **`STRATA_KVA=0`
|
||||
turns it off and gives the exact 0.2.6 prompt path.** The Strata app shows this as the "Faster long prompts" switch
|
||||
in Expert settings; it sends `STRATA_KVA=0` when the switch is off.
|
||||
**It is off by default.** Unset, the engine is 0.2.6's, prompt path and draft layer included. Set `STRATA_KVA=1` to
|
||||
turn it on (the Strata app shows it as the "Faster long prompts" switch in Expert settings, off until the user turns
|
||||
it on; the Terminal passes `STRATA_KVA=1`). Switched on, it uses the configuration the 0.2.7 quality runs found
|
||||
cleanest: **split 36** (only layers 36–47 of 48 are approximated) and a **2048-token exact tail**. Both can be
|
||||
changed with `STRATA_KVA_SPLIT` and `STRATA_KVA_SUFFIX`.
|
||||
|
||||
## Why it is off, and why split 36
|
||||
|
||||
KVA is an approximation, and the 0.2.7 quality runs (greedy, same build, KVA off against on, with a noise run of off
|
||||
against off) show where it costs answers when more layers are approximated:
|
||||
|
||||
- **Split 24 (the first version, layers 24–47).** On long extraction tasks (a list of scattered register entries in a
|
||||
14K–30K document) 4 of 6 tasks passed against 6 of 6 exact, 249 of 270 facts found against 270 on a Tesla P100:
|
||||
records were silently dropped (3 of 8 at 14K) and a 30K answer began with an invented row, the request's own reference
|
||||
id. Agent loops with a ~10K-token system prompt, of which the first 8192 tokens (about 84 %) were approximated, had
|
||||
a final answer without its totals and a quote with wrong arithmetic. On the RTX 5070 (`--agent-long`, 22 tasks) the
|
||||
split-24 default passed 0 of 10 agent runs and found 556 extraction facts, by applying the handbook's rules wrongly
|
||||
in the approximated region; exact passed 4 and 7 of 10 in two runs. A tail of 8192 tokens with split 24 found 574.
|
||||
- **Split 36.** On the same 22 tasks on the RTX 5070: agents 6 of 10 (exact: 4 and 7), extraction 585 of 606 facts
|
||||
(exact: 585), with prompt speed +11 % at 14K and +18 % at 30K. This is the cleanest configuration measured and the
|
||||
one `STRATA_KVA=1` selects.
|
||||
- Needle retrieval, unanswerable questions, short summaries, code and JSON were unchanged in every variant.
|
||||
- Split 24 is faster (about 1.3x at 14K and 1.5x at 30K on the P100); the extra layers that stay exact are what buy
|
||||
the quality.
|
||||
|
||||
## When it runs
|
||||
|
||||
KVA runs when all of these hold, and says so in the engine log:
|
||||
|
||||
- Qwen3.8-Flash-Next on CUDA or HIP (not SYCL, not Metal; Qwen3.6 and the other models read exactly);
|
||||
- Qwen3.8-Flash-Next on CUDA or HIP (not SYCL, not Metal). The split layer and the tail were measured on one shape, 48
|
||||
layers and 512 experts. Any other model (Qwen3.6-35B-A3B, other layer or expert counts) reads exactly even with
|
||||
`STRATA_KVA=1` and logs `KVA configured but NOT applied: this model's shape (L layers, E experts) is not one KVA was
|
||||
measured on`. `STRATA_KVA_ANY_MODEL=1` (research) lets it run there anyway, with one warning;
|
||||
- one GPU, no `--batch` slots, no `--peer-device`, no DFlash2 drafter;
|
||||
- the read is cold (it starts at position 0 of the session) and the text has no pictures;
|
||||
- the prompt (or the part of it read in one go, see below) has at least `STRATA_KVA_MIN_PROMPT` tokens (4096) and
|
||||
more than the fully computed tail plus 64.
|
||||
|
||||
Otherwise the prompt is read exactly. An engine that was started with `STRATA_KVA=1` logs the reason for every
|
||||
read it turned away (`strata prefill: KVA configured but NOT applied: <reason>`); with the default it logs only
|
||||
a long prompt turned away for an unusual reason (a layer split, pictures), and once at start-up when the engine
|
||||
configuration rules KVA out for the whole run.
|
||||
Otherwise the prompt is read exactly. An engine started with `STRATA_KVA=1` logs the reason for every read it turned
|
||||
away (`strata prefill: KVA configured but NOT applied: <reason>`), and once at start-up when the engine configuration
|
||||
rules KVA out for the whole run. Without `STRATA_KVA=1` it logs nothing about KVA.
|
||||
|
||||
## The fully computed tail and the draft layer
|
||||
|
||||
@@ -37,8 +60,8 @@ attends to the same cells as with `STRATA_KVA=0`. The target model verifies ever
|
||||
how many tokens a verify window keeps (decode speed), never the answer.
|
||||
|
||||
Measured on the P100 (IQ3_XXS, auto cache and prefill, 14K and 30K prompts, 256 tokens), the draft layer attends to
|
||||
those cells with acceptance within noise of the exact read. A forced small window (the first default-on build set
|
||||
`--mtp-window 2048` for every session) and the research option below both cost acceptance on faster cards.
|
||||
those cells with acceptance within noise of the exact read at 14K and a few points lower at 30K (decode 6 % lower at
|
||||
30K). A forced small window (`--mtp-window 2048`) and the research option below both cost acceptance on faster cards.
|
||||
|
||||
`STRATA_KVA_DRAFT_FLOOR=1` (research): the draft layer gets a *cell floor* per session, the first cell built from a real
|
||||
row. It builds no draft cell below it and attends to `[max(floor, pos - window), pos)`, the exact tail growing with
|
||||
@@ -62,7 +85,7 @@ greater than 0 and is read exactly.
|
||||
debug check) differ with KVA on, as they would between any two reads that take different paths.
|
||||
- **A cold read again** (the cache evicted the conversation, a new chat with the same system prompt) approximates
|
||||
its own prefix again.
|
||||
- **`STRATA_KVA=0`** restores 0.2.6: every prompt is read exactly, the draft window is 32768 cells, nothing else
|
||||
- **Unset (or `STRATA_KVA=0`)** is 0.2.6: every prompt is read exactly, the draft window is 32768 cells, nothing else
|
||||
changes.
|
||||
|
||||
## Layer splits, batch slots, SYCL, Metal
|
||||
@@ -76,11 +99,13 @@ there, with a line when it is set).
|
||||
|
||||
| variable | default | what it does |
|
||||
|---|---:|---|
|
||||
| `STRATA_KVA` | on | `0` turns KVA off (the exact 0.2.6 prompt read). `1` is the same as the default but logs every read it turned away |
|
||||
| `STRATA_KVA_SPLIT` | 24 | first approximated layer (zero-based) |
|
||||
| `STRATA_KVA` | off | `1` turns KVA on (split 36, 2048-token tail). Unset or `0`: the exact 0.2.6 prompt read and draft layer |
|
||||
| `STRATA_KVA_ANY_MODEL` | off | `1` (research): also run KVA on a model whose shape (layers, experts) it was not measured on, with a warning |
|
||||
| `STRATA_KVA_SPLIT` | 36 | first approximated layer (zero-based); 24 approximates more, is faster and lost records and agent loops |
|
||||
| `STRATA_KVA_SUFFIX` | 2048 | fully computed tail positions; see above |
|
||||
| `STRATA_KVA_MIN_PROMPT` | 4096 | shorter prompts stay on the full path |
|
||||
| `STRATA_KVA_DRAFT_FLOOR` | off | `1`: the draft layer skips the draft cells of approximated chunks (research) |
|
||||
| `STRATA_KVA_SKIP` | state | `moe` (opt-in): the late layers of an approximated chunk run their whole attention half (GDN and QSA outputs into the residual) and skip only the MoE half and its expert transfers; the log line says `mode moe-skip`. Slower than the default state-only form, closer to the exact residual |
|
||||
| `STRATA_KVA_ONLY` | all | `gdn` or `qsa`: approximate only that layer type |
|
||||
| `STRATA_KVA_MODE` | identity | `identity`, `oracle`, `linear` (linear is not implemented) |
|
||||
| `STRATA_KVA_CAPTURE` | — | directory: write every late layer's real input for calibration (the read is exact while capturing) |
|
||||
@@ -91,12 +116,12 @@ there, with a line when it is set).
|
||||
|
||||
Every read says what it did:
|
||||
|
||||
- applied: `strata prefill: KVA on: split 24, suffix 2048, approximated 5120 of 7533 positions in 5 of 8 chunks, mode identity`
|
||||
- applied: `strata prefill: KVA on: split 36, suffix 2048, approximated 5120 of 7533 positions in 5 of 8 chunks, mode identity`
|
||||
- turned away: `strata prefill: KVA configured but NOT applied: <reason> (prompt … positions, min_prompt …, suffix …, split …)`
|
||||
|
||||
A quality comparison needs the control arm to say `STRATA_KVA=0` (an unset variable is now KVA).
|
||||
A quality comparison needs the KVA arm to say `STRATA_KVA=1`; the control arm leaves it unset or says `STRATA_KVA=0`.
|
||||
|
||||
## Measured (Tesla P100 16 GB, Qwen3.8-Flash-Next IQ3_XXS, chunk 1024, expert cache 1500, MTP window 2048)
|
||||
## Measured with split 24 and a 2048-token tail (Tesla P100 16 GB, Qwen3.8-Flash-Next IQ3_XXS, chunk 1024, expert cache 1500, MTP window 2048)
|
||||
|
||||
| prompt tokens | control prompt tok/s | KVA prompt tok/s | speedup | approximated | residual error on the suffix |
|
||||
|---:|---:|---:|---:|---:|---:|
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// include/strata/prefill/kva.hpp - STRATA_KVA: the approximate late-layer prompt read (on by default since 0.2.7).
|
||||
// include/strata/prefill/kva.hpp - STRATA_KVA: the approximate late-layer prompt read (opt-in since 0.2.7).
|
||||
//
|
||||
// After layer `split` the prompt's late layers need only their recurrent state (GDN recurrence + conv history)
|
||||
// and their KV/indexer caches; the attention output, the output projection, the MoE half and its expert
|
||||
@@ -14,9 +14,11 @@
|
||||
// oracle a captured real input per late layer (STRATA_KVA_ORACLE=<dir>; the plumbing/parity mode),
|
||||
// linear a fitted low-rank projector (STRATA_KVA_PROJECTOR=<file>; not implemented in this step).
|
||||
//
|
||||
// 0.2.7: the read is ON by default for a cold prompt of STRATA_KVA_MIN_PROMPT tokens or more on one CUDA/HIP
|
||||
// device. STRATA_KVA=0 brings back the exact 0.2.6 prompt path and the 0.2.6 draft window. Everything is read from
|
||||
// the environment once per process.
|
||||
// 0.2.7: the read is OFF unless STRATA_KVA=1 is set (the quality runs found lost records and invented rows in long
|
||||
// extraction and agent tasks, docs/KVA.md). Unset, nothing here runs and the engine is 0.2.6's. Switched on, it applies
|
||||
// to a cold prompt of STRATA_KVA_MIN_PROMPT tokens or more on one CUDA/HIP device, approximating only the layers from
|
||||
// 36 on, with a 2048-token exact tail (the configuration the 0.2.7 quality runs found cleanest; STRATA_KVA_SPLIT and
|
||||
// STRATA_KVA_SUFFIX override). Everything is read from the environment once per process.
|
||||
//
|
||||
// What the engine tells this header at start-up (kva_engine()): whether its configuration rules the read out for the
|
||||
// whole process (batch slots, a layer split, a peer device).
|
||||
@@ -46,8 +48,13 @@ inline constexpr bool kKvaBackend = false;
|
||||
inline constexpr bool kKvaBackend = true;
|
||||
#endif
|
||||
|
||||
/// The reason decline() gives for a model whose shape KVA was not measured on (prefill.cpp prints it with the numbers).
|
||||
inline constexpr const char* kKvaShapeReason = "this model's shape is not one KVA was measured on";
|
||||
|
||||
/// The fully computed tail of every approximated read.
|
||||
inline constexpr int64_t kKvaSuffixDefault = 2048;
|
||||
/// The first approximated layer when switched on: 36 of 48 (the cleanest measured; 24 lost records and agent loops).
|
||||
inline constexpr int64_t kKvaSplitDefault = 36;
|
||||
|
||||
/// What the running engine knows and the environment cannot say.
|
||||
struct KvaEngine {
|
||||
@@ -62,19 +69,25 @@ inline KvaEngine& kva_engine() {
|
||||
struct KvaConfig {
|
||||
enum class Mode { Identity, Oracle, Linear };
|
||||
enum class Only { All, Gdn, Qsa };
|
||||
/// What a late layer of an approximated chunk still does: StateOnly (the default) advances only its state (GDN
|
||||
/// recurrence, K/V and indexer pages); Moe runs the whole attention half (outputs into the residual) and skips only
|
||||
/// the MoE half and its expert transfers.
|
||||
enum class Skip { StateOnly, Moe };
|
||||
|
||||
bool on = true; ///< on unless STRATA_KVA=0 (or this build has no path)
|
||||
bool on = false; ///< STRATA_KVA=1 (and this build has a path); unset or 0: off
|
||||
bool explicit_on = false; ///< STRATA_KVA was set to something other than 0: every declined read says why
|
||||
bool capture = false; ///< STRATA_KVA_CAPTURE=<dir>: dump every late layer's real input rows
|
||||
bool capture_fp32 = false; ///< STRATA_KVA_CAPTURE_FP32=1: fp32 records (else bf16; fp32 is for parity runs)
|
||||
bool oracle_fp32 = false; ///< STRATA_KVA_ORACLE_FP32=1: the oracle files are fp32 (else bf16)
|
||||
bool parity = false; ///< STRATA_KVA_PARITY=1: report the state-only path against the full path
|
||||
bool any_model = false; ///< STRATA_KVA_ANY_MODEL=1 (research): also run on a shape KVA was not measured on, with a warning
|
||||
bool draft_floor = false; ///< STRATA_KVA_DRAFT_FLOOR=1: the draft layer skips the cells of approximated chunks (research; default: it reads them)
|
||||
int64_t split = 24; ///< STRATA_KVA_SPLIT: first approximated layer (zero-based)
|
||||
int64_t split = kKvaSplitDefault; ///< STRATA_KVA_SPLIT: first approximated layer (zero-based)
|
||||
int64_t suffix_env = 0; ///< STRATA_KVA_SUFFIX: fully computed tail positions (0 = not set: kKvaSuffixDefault)
|
||||
int64_t min_prompt = 4096; ///< STRATA_KVA_MIN_PROMPT: shorter prompts stay on the full path
|
||||
Mode mode = Mode::Identity; ///< STRATA_KVA_MODE=identity|oracle|linear
|
||||
Only only = Only::All; ///< STRATA_KVA_ONLY=all|gdn|qsa: which late layers take the approximate path
|
||||
Skip skip = Skip::StateOnly; ///< STRATA_KVA_SKIP=moe: skip only the MoE half of the late layers (opt-in)
|
||||
std::string oracle_dir; ///< STRATA_KVA_ORACLE
|
||||
std::string capture_dir; ///< STRATA_KVA_CAPTURE
|
||||
std::string projector; ///< STRATA_KVA_PROJECTOR
|
||||
@@ -91,17 +104,14 @@ struct KvaConfig {
|
||||
/// Why this read cannot take the approximate path, or nullptr when it can: a cold read of one prompt (not a
|
||||
/// continuation, no layer split, no pictures, no DFlash2 taps), on an engine that allows it, long enough for a
|
||||
/// suffix to leave approximated positions behind it. `routine` (optional) is set when the reason is an
|
||||
/// everyday one (a continuation, a short prompt) that an engine running KVA by default does not log.
|
||||
/// everyday one (a continuation, a short prompt) that is not worth a log line.
|
||||
const char* decline(const KvaEngine& e, int64_t pos0, int64_t n, int64_t lb, int64_t le, int64_t n_layers,
|
||||
bool pictures = false, bool taps = false, bool* routine = nullptr,
|
||||
bool unvalidated_model = false) const {
|
||||
bool unmeasured_shape = false) const {
|
||||
if (routine) *routine = false;
|
||||
if (!on) return "off (STRATA_KVA=0)";
|
||||
if (!on) return "off (STRATA_KVA is not 1)";
|
||||
if (capture) return "capture mode";
|
||||
if (!e.blocked.empty()) return e.blocked.c_str();
|
||||
// The split layer, the state-only layers and the suffix rule were measured on one geometry (Qwen3.8-Flash-Next,
|
||||
// 48 layers, 512 experts). Another model reads exactly unless STRATA_KVA=1 asks for it.
|
||||
if (unvalidated_model && !explicit_on) return "KVA was not validated for this model (STRATA_KVA=1 forces it)";
|
||||
if (pos0 != 0) {
|
||||
if (routine) *routine = true;
|
||||
return "a continuation read (pos0 != 0)";
|
||||
@@ -117,6 +127,9 @@ struct KvaConfig {
|
||||
if (routine) *routine = true;
|
||||
return "the suffix leaves too few positions to approximate";
|
||||
}
|
||||
// the split layer, the state-only layers and the tail were measured on one geometry (Qwen3.8-Flash-Next, 48
|
||||
// layers, 512 experts); another model reads exactly unless the research override asks for it
|
||||
if (unmeasured_shape && !any_model) return kKvaShapeReason;
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
@@ -138,14 +151,15 @@ inline KvaConfig kva_config_from_env() {
|
||||
KvaConfig k;
|
||||
const char* sw = std::getenv("STRATA_KVA");
|
||||
const bool set = sw != nullptr && *sw != '\0';
|
||||
k.on = !set || sw[0] != '0';
|
||||
k.explicit_on = set && sw[0] != '0';
|
||||
k.on = set && sw[0] != '0';
|
||||
k.explicit_on = k.on;
|
||||
if (!kKvaBackend) k.on = false;
|
||||
k.split = kva_env_i64("STRATA_KVA_SPLIT", 24);
|
||||
k.split = kva_env_i64("STRATA_KVA_SPLIT", kKvaSplitDefault);
|
||||
k.suffix_env = std::max<int64_t>(0, kva_env_i64("STRATA_KVA_SUFFIX", 0));
|
||||
k.min_prompt = kva_env_i64("STRATA_KVA_MIN_PROMPT", 4096);
|
||||
k.parity = kva_env_flag("STRATA_KVA_PARITY", false);
|
||||
k.draft_floor = kva_env_flag("STRATA_KVA_DRAFT_FLOOR", false);
|
||||
k.any_model = kva_env_flag("STRATA_KVA_ANY_MODEL", false);
|
||||
if (const char* d = std::getenv("STRATA_KVA_CAPTURE"); d != nullptr && *d != '\0') {
|
||||
k.capture = true;
|
||||
k.capture_dir = d;
|
||||
@@ -165,6 +179,7 @@ inline KvaConfig kva_config_from_env() {
|
||||
else if (std::string(m) == "linear") k.mode = KvaConfig::Mode::Linear;
|
||||
else k.mode = KvaConfig::Mode::Identity;
|
||||
}
|
||||
if (const char* sk = std::getenv("STRATA_KVA_SKIP"); sk != nullptr && std::string(sk) == "moe") k.skip = KvaConfig::Skip::Moe;
|
||||
if (const char* o = std::getenv("STRATA_KVA_ONLY"); o != nullptr && *o != '\0') {
|
||||
if (std::string(o) == "gdn") k.only = KvaConfig::Only::Gdn;
|
||||
else if (std::string(o) == "qsa") k.only = KvaConfig::Only::Qsa;
|
||||
|
||||
+46
-26
@@ -31,36 +31,43 @@ static void put(const char* k, const char* v) {
|
||||
static void clear_all() {
|
||||
for (const char* k : {"STRATA_KVA", "STRATA_KVA_SPLIT", "STRATA_KVA_SUFFIX", "STRATA_KVA_MIN_PROMPT", "STRATA_KVA_PARITY",
|
||||
"STRATA_KVA_CAPTURE", "STRATA_KVA_CAPTURE_FP32", "STRATA_KVA_ORACLE", "STRATA_KVA_ORACLE_FP32",
|
||||
"STRATA_KVA_PROJECTOR", "STRATA_KVA_MODE", "STRATA_KVA_ONLY", "STRATA_KVA_DRAFT_FLOOR"})
|
||||
"STRATA_KVA_PROJECTOR", "STRATA_KVA_MODE", "STRATA_KVA_ONLY", "STRATA_KVA_DRAFT_FLOOR", "STRATA_KVA_SKIP", "STRATA_KVA_ANY_MODEL"})
|
||||
unset(k);
|
||||
}
|
||||
static bool is(const char* got, const char* want) { return got != nullptr && std::strcmp(got, want) == 0; }
|
||||
|
||||
int main() {
|
||||
// ---- the switch: on by default, STRATA_KVA=0 off, anything else on and chatty
|
||||
// ---- the switch: off unless STRATA_KVA=1 (0.2.7 ships KVA opt-in; unset is the exact 0.2.6 engine)
|
||||
clear_all();
|
||||
{
|
||||
const KvaConfig c = kva_config_from_env();
|
||||
CHECK(c.on && !c.explicit_on);
|
||||
CHECK(c.split == 24 && c.min_prompt == 4096 && c.suffix_env == 0);
|
||||
CHECK(!c.on && !c.explicit_on); // unset: off
|
||||
CHECK(c.split == 36 && c.min_prompt == 4096 && c.suffix_env == 0 && kKvaSplitDefault == 36);
|
||||
CHECK(c.mode == KvaConfig::Mode::Identity && c.only == KvaConfig::Only::All && !c.capture);
|
||||
KvaEngine eng;
|
||||
CHECK(!c.wants_cell_floor(eng, true));
|
||||
CHECK(is(c.decline(eng, 0, 30000, 0, -1, 48), "off (STRATA_KVA is not 1)")); // a 30K cold prompt is read exactly
|
||||
}
|
||||
put("STRATA_KVA", "0");
|
||||
{ const KvaConfig c = kva_config_from_env(); CHECK(!c.on && !c.explicit_on); }
|
||||
put("STRATA_KVA", "1");
|
||||
{ const KvaConfig c = kva_config_from_env(); CHECK(c.on && c.explicit_on); }
|
||||
put("STRATA_KVA", ""); // an empty value is "not set"
|
||||
{ const KvaConfig c = kva_config_from_env(); CHECK(c.on && !c.explicit_on); }
|
||||
{ const KvaConfig c = kva_config_from_env(); CHECK(!c.on && !c.explicit_on); }
|
||||
clear_all();
|
||||
put("STRATA_KVA", "1"); // everything below runs with KVA switched on
|
||||
|
||||
// ---- the tail: 2048, or STRATA_KVA_SUFFIX; the draft window plays no part (the cell floor protects the draft layer)
|
||||
// ---- switched on: split 36 and a 2048-token tail (the configuration measured), or STRATA_KVA_SPLIT / STRATA_KVA_SUFFIX
|
||||
{
|
||||
const KvaConfig c = kva_config_from_env();
|
||||
CHECK(c.effective_suffix() == kKvaSuffixDefault && kKvaSuffixDefault == 2048);
|
||||
CHECK(c.on && c.split == 36 && c.effective_suffix() == kKvaSuffixDefault && kKvaSuffixDefault == 2048);
|
||||
put("STRATA_KVA_SPLIT", "30");
|
||||
CHECK(kva_config_from_env().split == 30);
|
||||
unset("STRATA_KVA_SPLIT");
|
||||
put("STRATA_KVA_SUFFIX", "3000");
|
||||
const KvaConfig e = kva_config_from_env();
|
||||
CHECK(e.effective_suffix() == 3000);
|
||||
clear_all();
|
||||
unset("STRATA_KVA_SUFFIX");
|
||||
}
|
||||
|
||||
// ---- who is turned away, and why (the first reason that holds is the one named)
|
||||
@@ -68,42 +75,43 @@ int main() {
|
||||
const KvaConfig c = kva_config_from_env();
|
||||
KvaEngine eng;
|
||||
bool routine = false;
|
||||
// a cold 14.6K prompt on one device: applies
|
||||
// a cold 14.6K prompt on one device: applies (14600 > 2048 + 64)
|
||||
CHECK(c.decline(eng, 0, 14600, 0, -1, 48) == nullptr);
|
||||
CHECK(c.decline(eng, 0, 14600, 0, 48, 48) == nullptr); // le == n_layers is the whole trunk too
|
||||
// a continuation (turn 2 of a chat, a resumed prompt): routine
|
||||
CHECK(is(c.decline(eng, 9000, 14600, 0, -1, 48, false, false, &routine), "a continuation read (pos0 != 0)") && routine);
|
||||
// short prompts and a prompt the tail would swallow: routine
|
||||
CHECK(is(c.decline(eng, 0, 3000, 0, -1, 48, false, false, &routine), "the prompt is shorter than STRATA_KVA_MIN_PROMPT") && routine);
|
||||
CHECK(c.decline(eng, 0, 4096, 0, -1, 48) == nullptr); // 4096 > 2112 + 64: it applies
|
||||
put("STRATA_KVA_SUFFIX", "5000");
|
||||
CHECK(c.decline(eng, 0, 4096, 0, -1, 48) == nullptr); // 4096 is the first prompt that is long enough
|
||||
put("STRATA_KVA_SUFFIX", "5000"); // a longer tail swallows a prompt of 5000 and not one of 5065
|
||||
{ const KvaConfig s = kva_config_from_env();
|
||||
CHECK(is(s.decline(eng, 0, 5000, 0, -1, 48, false, false, &routine), "the suffix leaves too few positions to approximate") && routine); }
|
||||
clear_all();
|
||||
CHECK(is(s.decline(eng, 0, 5000, 0, -1, 48, false, false, &routine), "the suffix leaves too few positions to approximate") && routine);
|
||||
CHECK(s.decline(eng, 0, 5065, 0, -1, 48) == nullptr); }
|
||||
unset("STRATA_KVA_SUFFIX");
|
||||
// not routine: the user may want to know
|
||||
CHECK(is(c.decline(eng, 0, 14600, 24, -1, 48, false, false, &routine), "a layer split (not the whole trunk)") && !routine);
|
||||
CHECK(is(c.decline(eng, 0, 14600, 0, 24, 48), "a layer split (not the whole trunk)"));
|
||||
CHECK(is(c.decline(eng, 0, 14600, 0, -1, 48, true), "a prompt with pictures"));
|
||||
CHECK(is(c.decline(eng, 0, 14600, 0, -1, 48, false, true), "the DFlash2 drafter reads the late layers' hidden states"));
|
||||
// a model the split and the tail were not measured on reads exactly unless STRATA_KVA=1 asks
|
||||
CHECK(is(c.decline(eng, 0, 14600, 0, -1, 40, false, false, &routine, true),
|
||||
"KVA was not validated for this model (STRATA_KVA=1 forces it)") && !routine);
|
||||
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();
|
||||
// no draft layer at all: nothing to cover
|
||||
// the engine rules the whole process out (batch slots, a layer split, a peer device)
|
||||
KvaEngine blocked = eng;
|
||||
blocked.blocked = "batch slots (--batch)";
|
||||
CHECK(is(c.decline(blocked, 0, 14600, 0, -1, 48, false, false, &routine), "batch slots (--batch)") && !routine);
|
||||
// off wins over everything, capture over the rest
|
||||
put("STRATA_KVA", "0");
|
||||
{ const KvaConfig o = kva_config_from_env(); CHECK(is(o.decline(eng, 0, 14600, 0, -1, 48), "off (STRATA_KVA=0)")); }
|
||||
put("STRATA_KVA", "1");
|
||||
// a model whose shape KVA was not measured on (not 48 layers / 512 experts) is turned away, not warned about;
|
||||
// the routine reasons (a short prompt, a continuation) still come first
|
||||
CHECK(is(c.decline(eng, 0, 14600, 0, -1, 40, false, false, &routine, true), kKvaShapeReason) && !routine);
|
||||
CHECK(is(c.decline(eng, 0, 3000, 0, -1, 40, false, false, &routine, true), "the prompt is shorter than STRATA_KVA_MIN_PROMPT") && routine);
|
||||
CHECK(c.decline(eng, 0, 14600, 0, -1, 48, false, false, &routine, false) == nullptr); // the measured shape applies
|
||||
put("STRATA_KVA_ANY_MODEL", "1"); // the research override allows it (prefill.cpp then warns once)
|
||||
{ const KvaConfig a = kva_config_from_env(); CHECK(a.any_model && a.decline(eng, 0, 14600, 0, -1, 40, false, false, nullptr, true) == nullptr); }
|
||||
unset("STRATA_KVA_ANY_MODEL");
|
||||
CHECK(!kva_config_from_env().any_model);
|
||||
// capture wins over the rest
|
||||
put("STRATA_KVA_CAPTURE", "/tmp/kva-cap");
|
||||
{ const KvaConfig o = kva_config_from_env(); CHECK(o.capture && is(o.decline(eng, 0, 14600, 0, -1, 48), "capture mode")); }
|
||||
clear_all();
|
||||
unset("STRATA_KVA_CAPTURE");
|
||||
}
|
||||
clear_all();
|
||||
|
||||
// ---- the research modes still parse
|
||||
put("STRATA_KVA_ORACLE", "/tmp/o");
|
||||
@@ -118,12 +126,24 @@ int main() {
|
||||
}
|
||||
clear_all();
|
||||
|
||||
// ---- STRATA_KVA_SKIP: the default is the state-only form; "moe" runs the attention halves and skips the MoE half
|
||||
{
|
||||
CHECK(kva_config_from_env().skip == KvaConfig::Skip::StateOnly);
|
||||
put("STRATA_KVA_SKIP", "moe");
|
||||
CHECK(kva_config_from_env().skip == KvaConfig::Skip::Moe);
|
||||
put("STRATA_KVA_SKIP", "state");
|
||||
CHECK(kva_config_from_env().skip == KvaConfig::Skip::StateOnly);
|
||||
put("STRATA_KVA_SKIP", "bogus");
|
||||
CHECK(kva_config_from_env().skip == KvaConfig::Skip::StateOnly);
|
||||
unset("STRATA_KVA_SKIP");
|
||||
}
|
||||
// ---- "not applied => identical to STRATA_KVA=0", the parts that need no GPU
|
||||
// (1) by default the draft layer is 0.2.6's own code; the cell floor is a research option, wired only when asked
|
||||
{
|
||||
KvaEngine eng;
|
||||
put("STRATA_KVA", "1");
|
||||
const KvaConfig on = kva_config_from_env();
|
||||
CHECK(!on.draft_floor && !on.wants_cell_floor(eng, true));
|
||||
CHECK(on.on && !on.draft_floor && !on.wants_cell_floor(eng, true));
|
||||
put("STRATA_KVA_DRAFT_FLOOR", "1");
|
||||
const KvaConfig fl = kva_config_from_env();
|
||||
CHECK(fl.draft_floor && fl.wants_cell_floor(eng, true));
|
||||
|
||||
+42
-16
@@ -2722,6 +2722,14 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
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,
|
||||
g.n_layers != 48 || g.n_expert != 512);
|
||||
if (kva_why_not == nullptr && (g.n_layers != 48 || g.n_expert != 512)) {
|
||||
// STRATA_KVA_ANY_MODEL=1 (research): allowed on a shape KVA was not measured on, with a warning once
|
||||
static std::atomic<bool> warned{false};
|
||||
if (!warned.exchange(true))
|
||||
std::fprintf(stderr, "strata prefill: WARNING: STRATA_KVA_ANY_MODEL=1: KVA was only measured on Qwen3.8-Flash-Next "
|
||||
"(48 layers, 512 experts); this model has %lld layers and %lld experts\n",
|
||||
(long long) g.n_layers, (long long) g.n_expert);
|
||||
}
|
||||
const bool kva_run = kva_why_not == nullptr;
|
||||
const int64_t kva_split = std::min<int64_t>(std::max<int64_t>(kva.split, 1), g.n_layers);
|
||||
const int64_t kva_frontier = std::max<int64_t>(0, n - kva_suffix);
|
||||
@@ -3272,6 +3280,10 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
const bool kva_layer = kva_approx && l >= kva_split &&
|
||||
(kva.only == KvaConfig::Only::All ||
|
||||
(kva.only == KvaConfig::Only::Gdn) == !core::is_qsa_layer(g, l));
|
||||
// STRATA_KVA_SKIP=moe: the layer's attention half (GDN or QSA, outputs included) runs in full and only the MoE
|
||||
// half is skipped; `kva_state_only` is the default form, where only the state advances
|
||||
const bool kva_moe_only = kva_layer && kva.skip == KvaConfig::Skip::Moe;
|
||||
const bool kva_state_only = kva_layer && !kva_moe_only;
|
||||
const bool kva_subst = kva_layer && kva.mode == KvaConfig::Mode::Oracle;
|
||||
const bool kva_cap_layer = kva.capture && LB == 0 && (LE < 0 || LE == g.n_layers) && l >= kva_split;
|
||||
if (l == std::max<int64_t>(LB, 1)) { // the PLE rows, read from layer 1 on (a wait for the SSD read of the first chunk lands on "ple")
|
||||
@@ -3341,6 +3353,15 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
normed = false; // the previous half's norm of R is stale once the input is replaced
|
||||
}
|
||||
for (int half = 0; half < 2; ++half) {
|
||||
if (kva_moe_only && half == 1) {
|
||||
// KVA moe-skip: the attention half wrote its output into R; this layer's MoE half has no consumer
|
||||
// left for an approximated chunk, so it (and its expert transfers) is skipped. R is not normed for
|
||||
// it (the write after the attention half normed it with this layer's FFN weights).
|
||||
normed = false;
|
||||
consumed = std::max(consumed, seq_start[(size_t) l + 1]);
|
||||
give_back(consumed);
|
||||
break;
|
||||
}
|
||||
// ---- the hyper-connection read of this half
|
||||
const char* pre = half == 0 ? "hc_attn_" : "hc_ffn_";
|
||||
const std::string sn = std::string(pre) + "norm.weight", sd = std::string(pre) + "down.weight",
|
||||
@@ -3441,7 +3462,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
const char* v = std::getenv("STRATA_KVA_GDN_NORM");
|
||||
return v != nullptr && std::atoi(v) != 0;
|
||||
}();
|
||||
if ((!kva_layer || kva_gdn_norm) &&
|
||||
if ((!kva_state_only || kva_gdn_norm) &&
|
||||
!native_proj(m.gemm, wg, m.mixed_h, m.z, T, v.name("attn_gate.weight"), err)) return false;
|
||||
if (!bf16_proj(m.gemm, wa, m.mixed_bf, m.ab, T, v.name("ssm_alpha.weight"), err, 2 * HV, m.mixed_bf_lo)) return false;
|
||||
if (!bf16_proj(m.gemm, wb, m.mixed_bf, m.ab + HV, T, v.name("ssm_beta.weight"), err, 2 * HV, m.mixed_bf_lo)) return false;
|
||||
@@ -3450,16 +3471,16 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
gdn_conv(conv, m.qkv, (const float*) wc->data, m.hbuf, T, EPS, m.cs);
|
||||
pt.mark(kPfGdnRec, cs);
|
||||
const int64_t ld_y = pf_pad() && T >= std::max<int64_t>(pf_switch_min_t(), 64) ? ZV + ZV_PAD : 0;
|
||||
if (kva_layer && !kva_gdn_norm) // KVA: the recurrence without the output normalization
|
||||
if (kva_state_only && !kva_gdn_norm) // KVA: the recurrence without the output normalization
|
||||
gdn_recurrence_state_only(state, m.hbuf, m.gate, m.beta, m.y, T, m.cs);
|
||||
else
|
||||
gdn_recurrence(state, m.hbuf, m.gate, m.beta, m.z, (const float*) wnm->data, EPS, m.y, m.y_h, T, m.cs,
|
||||
ld_y);
|
||||
pt.mark(kPfGdnOut, cs);
|
||||
if (!kva_layer) // KVA: the out projection feeds only the residual write and the MoE half
|
||||
if (!kva_state_only) // KVA: the out projection feeds only the residual write and the MoE half
|
||||
if (!native_proj(m.gemm, wo, m.y_h, m.bo, T, v.name("ssm_out.weight"), err, 0, ld_y)) return false;
|
||||
++gdn_index;
|
||||
if (kva_layer) {
|
||||
if (kva_state_only) {
|
||||
// the layer consumed no expert: its stream entries must still be released, or the issuer
|
||||
// thread fills the ring and the next fully computed layer waits for a copy that never comes
|
||||
consumed = std::max(consumed, seq_start[(size_t) l + 1]);
|
||||
@@ -3481,9 +3502,9 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
if (!native_proj(m.gemm, wk, m.mixed_h, m.Kc, T, v.name("attn_k.weight"), err)) return false;
|
||||
if (!native_proj(m.gemm, wv, m.mixed_h, m.Vc, T, v.name("attn_v.weight"), err)) return false;
|
||||
// KVA: the query projections have no consumer when only the KV/indexer state advances
|
||||
if (!kva_layer && !native_proj(m.gemm, wq, m.mixed_h, m.Qf, T, v.name("attn_q.weight"), err)) return false;
|
||||
if (!kva_state_only && !native_proj(m.gemm, wq, m.mixed_h, m.Qf, T, v.name("attn_q.weight"), err)) return false;
|
||||
if (!bf16_proj(m.gemm, wik, m.mixed_bf, m.idx_raw, T, v.name("indexer.k_proj.weight"), err, 0, m.mixed_bf_lo)) return false;
|
||||
if (!kva_layer && !bf16_proj(m.gemm, wiq, m.mixed_bf, m.q_idx, T, v.name("indexer.q_proj.weight"), err, 0, m.mixed_bf_lo)) return false;
|
||||
if (!kva_state_only && !bf16_proj(m.gemm, wiq, m.mixed_bf, m.q_idx, T, v.name("indexer.q_proj.weight"), err, 0, m.mixed_bf_lo)) return false;
|
||||
rms_rows(m.Kc, (const float*) wkn->data, T * 2, 256, 256, EPS, m.cs);
|
||||
rope(m.Kc, T, 2, 256, 512, p0, strata::kernels::rope_scaling(), m.cs);
|
||||
// KV streaming: this layer's cells [0, p0) come in from the host copy to the staging pool, and the
|
||||
@@ -3575,7 +3596,7 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
// KVA: the KV pages and the indexer's keys/pool are complete for this chunk. Nothing that
|
||||
// reads them (query side, selection, attention, output projection, the MoE half) has a
|
||||
// consumer left, so the layer ends here.
|
||||
if (kva_layer) {
|
||||
if (kva_state_only) {
|
||||
++qsa_index;
|
||||
if (!kv_prefetch_after(l + 1, qsa_index)) return false;
|
||||
// no expert is consumed here: release the stream entries so the issuer cannot stall
|
||||
@@ -5274,7 +5295,8 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
}
|
||||
}
|
||||
if (kva_run || kva.capture) {
|
||||
const char* mode = kva.mode == KvaConfig::Mode::Oracle ? "oracle"
|
||||
const char* mode = kva.skip == KvaConfig::Skip::Moe ? "moe-skip"
|
||||
: kva.mode == KvaConfig::Mode::Oracle ? "oracle"
|
||||
: kva.mode == KvaConfig::Mode::Linear ? "linear" : "identity";
|
||||
const char* only = kva.only == KvaConfig::Only::Gdn ? ", gdn layers only"
|
||||
: kva.only == KvaConfig::Only::Qsa ? ", qsa layers only" : "";
|
||||
@@ -5294,17 +5316,21 @@ bool Prefill::run_impl(const int64_t* tokens, int64_t n, int64_t pos0, std::stri
|
||||
"the first %lld positions of this prompt; a follow-up turn that continues from "
|
||||
"this state reads its new tokens exactly on top of them (nothing is approximated "
|
||||
"twice); STRATA_KVA=0 reads every prompt exactly\n",
|
||||
(long long) kva_split, (long long) kva_frontier);
|
||||
(long long) kva_split, (long long) kva_positions);
|
||||
}
|
||||
} else if (kva.on && (kva.explicit_on || (n >= kva.min_prompt && !kva_routine && kva_eng.blocked.empty()))) {
|
||||
// KVA-8 (observability): a run that was configured for KVA but fell back completely must say so and why,
|
||||
// or an operator cannot tell it apart from a run where the feature was never asked for. With KVA on by
|
||||
// default only a long prompt that was turned away for an unusual reason is worth a line (STRATA_KVA=1 asks
|
||||
// for every reason).
|
||||
std::fprintf(stderr, "strata prefill: KVA configured but NOT applied: %s (prompt %lld positions, "
|
||||
"min_prompt %lld, suffix %lld, split %lld)\n",
|
||||
kva_why_not != nullptr ? kva_why_not : "not applicable", (long long) n, (long long) kva.min_prompt,
|
||||
(long long) kva_suffix, (long long) kva_split);
|
||||
// or an operator cannot tell it apart from a run where the feature was never asked for. (KVA only runs
|
||||
// when STRATA_KVA=1 asked for it, so every reason is worth a line.)
|
||||
if (kva_why_not == kKvaShapeReason)
|
||||
std::fprintf(stderr, "strata prefill: KVA configured but NOT applied: this model's shape (%lld layers, %lld experts) "
|
||||
"is not one KVA was measured on (STRATA_KVA_ANY_MODEL=1 allows it)\n",
|
||||
(long long) g.n_layers, (long long) g.n_expert);
|
||||
else
|
||||
std::fprintf(stderr, "strata prefill: KVA configured but NOT applied: %s (prompt %lld positions, "
|
||||
"min_prompt %lld, suffix %lld, split %lld)\n",
|
||||
kva_why_not != nullptr ? kva_why_not : "not applicable", (long long) n, (long long) kva.min_prompt,
|
||||
(long long) kva_suffix, (long long) kva_split);
|
||||
}
|
||||
if (std::getenv("STRATA_STATE_HASH_GDN") != nullptr) { // debug: the GDN states as the prompt path leaves them
|
||||
cudaStreamSynchronize(m.cs);
|
||||
|
||||
@@ -5529,7 +5529,7 @@ int main(int argc, char** argv) {
|
||||
// slots' draft-KV copies use this one too
|
||||
static const strata::core::ModelGeometry draft_geometry{};
|
||||
std::vector<std::unique_ptr<strata::core::MtpDrafter>> slot_mtp;
|
||||
// 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
|
||||
// KVA (0.2.7: opt-in with STRATA_KVA=1; unset 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) keeps its window, whatever it is: a session KVA approximated tells it the first cell built from a real row
|
||||
|
||||
Reference in new issue
Block a user