metal: the MTP drafter, the prompt path through the window, the PLE block and the Q8_0 HC read on the GPU

- MTP (src/core/mtp.cpp hook under STRATA_GPU_METAL: load / bind / prefill / draft / draft_first): the draft layer
  of mtp.hpp on mac-kern's kernels, from the MTP GGUF (tools/mtp_pack.py, beside --mtp's rt dir; the F32 norms
  folded to 1 + w as tools/mtp_rt.py does): input branches (msl/mtp.metal: rms, strided xprep, stream combine),
  the three HC reads, K/V and indexer keys through qsa_prep into its own cache, dense attention over the last
  2,051 cells, the MoE (fused gate_up experts), the final mixer and the head over data/draft_vocab.bin's subset
  (STRATA_METAL_DRAFT_VOCAB=full: the whole head).  One command buffer per draft step (the next step's embedding
  is dequantized on the host).  Greedy output with MTP equals the output without it, token for token (Q2_0, two
  prompts, BF16 and Q8_0 HC); acceptance equals the HIP engine's on the same prompts (124 of 228; 104 of 291 vs 285).
- Prefill (src/prefill/prefill.cpp hook): init brings up the decode window, run reads the prompt through windows
  of 8 rows (no head) and feeds on_chunk (the MTP's K/V); draft_kv declines.  Prompt windows of 8 rows give the
  same tokens as one-token windows.  generate.cpp's own Metal prompt loop is gone again (--prefill auto as on the
  PC).  One window per process (the prompt path may bring it up before the Verifier).
- msl/ple.metal: the PLE block's norms, gate, conv and residual update on the GPU (same tokens as the CPU version).
- The HC read in Q8_0 by default (the owner's decision; STRATA_HC_Q8=0: BF16): the down / up / inject matrices
  quantized at load (290 matrices, 680 MB, 70 ms), read by gr_down_q8 / gr_up_q8.
- The encoded window is cached per (T, head, history parity) and only its positions are patched per window.
- f-mac-kern merged up to 4775d233.

M5 Max, Q2_0, greedy, 200 tokens: Q8_0 HC 85 tok/s without MTP; with MTP 101 tok/s (acceptance 0.54) and 79 tok/s
(acceptance 0.36, a prompt the drafter does badly on everywhere: HIP 71 tok/s there).
This commit is contained in:
Niko1221 committed 2026-10-12 14:55:46 +02:00
1 parent 682ae446a2
commit b507277646
9 files changed
+833 -75

No files matched your search

-29
View File
@@ -2918,12 +2918,8 @@ int main(int argc, char** argv) {
}
strata::core::NativeEmbed native_embed;
if (native_pack) {
#if defined(STRATA_GPU_METAL) // Metal: the prompt is read through the decode windows (no batched prompt path yet)
if (o.native_preset.empty() || o.spec < 2 || o.keep_canonical) {
#else
if (o.native_preset.empty() || o.spec < 2 || o.keep_canonical ||
(o.prefill_chunk <= 0 && o.tokens.size() > 1)) {
#endif
std::fprintf(stderr, "strata generate: %s is a native (IQ) pack: it needs --native SHARD1, --spec T (T >= 2) "
"and --prefill CHUNK\n", o.pack.c_str());
return 2;
@@ -12903,11 +12899,7 @@ int main(int argc, char** argv) {
// round emits (accepted drafts + 1) tokens. `commit` keeps the state of the tokens that were emitted.
const bool ended = o.stop_eos && !produced.empty() &&
std::find(o.eos_ids.begin(), o.eos_ids.end(), (int64_t) produced.back()) != o.eos_ids.end();
#if defined(STRATA_GPU_METAL) // Metal: also from position 0 (the prompt is read through the windows below)
if ((spec_pos > 0 || (native_pack && o.prefill_chunk <= 0)) && (int64_t) produced.size() < o.max_new && !ended) {
#else
if (spec_pos > 0 && (int64_t) produced.size() < o.max_new && !ended) {
#endif
auto read_ids = [](const std::string& path, std::vector<int64_t>& ids) {
std::ifstream in(path);
std::string text((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
@@ -13104,27 +13096,6 @@ int main(int argc, char** argv) {
};
int64_t p = spec_pos;
int32_t x = (int32_t) tok;
#if defined(STRATA_GPU_METAL)
// Metal (no batched prompt path yet): the prompt's tokens before the last one go through one-token windows,
// each committed; the last one is the first window below, as after the batched path
if (native_pack && o.prefill_chunk <= 0 && spec_pos == 0 && n_prompt > 1) {
const Clock::time_point tpr = Clock::now();
std::vector<int32_t> po(1);
ver.set_head_sampling(false);
for (int64_t i = 0; i + 1 < n_prompt; ++i) {
const int32_t ti = (int32_t) o.tokens[(size_t) i];
if (!ver.run(1, &ti, i, &drive_pool_multi, &drive, po.data(), err) || !ver.commit(1, err)) {
std::fprintf(stderr, "strata generate: the prompt window at %lld: %s\n", (long long) i, err.c_str());
return 1;
}
}
ver.set_head_sampling(true);
p = n_prompt - 1;
x = (int32_t) o.tokens[(size_t) (n_prompt - 1)];
std::fprintf(stderr, "strata generate: Metal: the prompt's %lld tokens through one-token windows in %.1f ms\n",
(long long) (n_prompt - 1), std::chrono::duration<double, std::milli>(Clock::now() - tpr).count());
}
#endif
std::vector<int32_t> drafts((size_t) o.spec, 0);
std::vector<float> dprob((size_t) o.spec, 1.0f);
std::vector<int64_t> window_hist((size_t) o.spec + 1, 0);