ling32: --mtp-auto loads the MTP sidecar only where it measurably pays; the engine measures the host-to-device link at start

Measured (2026-12-02, L32-TABLE / L32-MID / L32-SMALL, real Ling-3.2-Air, plain vs MTP, greedy): the drafter pays on an RTX 5090 (PCIe 5.0 x16: +4..11 % decode,
115.8 tok/s with it at ctx 131072 on our 5090 box) and where everything is resident or memory is unified; on tiered cards over PCIe 4.0 x16 or narrower
(3090: 1.00 tokens per verify, prefill 947 vs 1042 tok/s at 4K, 89 fewer pool slots; 4090: acceptance 0; 5080 16 GB -3.4 %; 5060 Ti -3 %) it never does, yet
the sidecar still costs prefill and pool slots.

--mtp-auto FILE (with --spec mtp --spec-adaptive; --mtp FILE stays the forced 'on'): after the memory plan, tier A / unified memory: loaded; tier C: not loaded;
tier B: one pinned 256 MiB host-to-device copy (3 repeats, median) and loaded only from 32 GB/s (kMtpAutoMinLinkGbps, between PCIe 4.0 x16 = 24.4 GB/s measured
and PCIe 5.0 x16 = 45+; 53.5 GB/s measured on the 5090 box); an unmeasured link: not loaded.  When it is not loaded the sidecar, --spec mtp and the speculative loop
are dropped and the plan is made again without the sidecar's bytes.  The rule is the pure function mtp_auto_pays (planner header, rows in its comment), table-tested in
ling32_tier_planner_test; ling32_mtp_auto_regress runs the wiring on the wide fixture (link by STRATA_L32_H2D_GBPS: 24.4 -> not loaded and plain ids, 46 -> loaded and
speculation on, --mtp forced -> loaded, tier A -> loaded; ids equal to the plain run in every leg).

Also: the measured link is printed ('tier store: host-to-device link moved N GB/s (pinned 256 MiB copies, median of 3)') whenever a tier store is planned, the serve INFO line
carries h2d_gbps=N (before images=), and --info FILE --kv says mtp_auto=1 so the launcher can tell old engines apart.
This commit is contained in:
Niko1221 committed 2026-12-02 20:49:13 +01:00
1 parent bb74f8ce88
commit fe96b81b73
7 files changed
+225 -11

No files matched your search

@@ -284,4 +284,21 @@ inline TierPlan plan_tier(const TierHardware& hw, const TierRequestOpts& rq, con
return p;
}
// --mtp-auto: does the MTP sidecar (the speculative drafter) pay on this card, so that it is worth its prefill chunks and its pool slots? Measured rows (2026-12-02, plain vs MTP, greedy, real Ling-3.2-Air):
// RTX 5090, PCIe 5.0 x16, 32 GB, tier B: +11 % decode (short prompts 102 -> 113 tok/s, 4K 96 -> 104); RTX 3090, PCIe 4.0 x16 (24.4 GB/s measured), tier B: the controller sits at 1.00 tokens per verify,
// decode unchanged, prefill 947 against 1042 tok/s at 4K and 89 fewer slots; RTX 4090 (PCIe 4.0 x16): acceptance 0; RTX 5080 16 GB: -3.4 %; RTX 5060 Ti 16 GB (PCIe 4.0 x8): -3 %.
// The tiers decode at the host-to-device link's speed (about 0.3 GB of expert slabs per token): a verify of several rows touches more distinct experts, so the drafter only wins when the link has the room, i.e.
// on a PCIe 5.0 x16 class card (45-50 GB/s practical); with everything resident (tier A) and on unified memory there is no link to wait for. Tier C reads the SSD: never.
constexpr double kMtpAutoMinLinkGbps = 32.0; // between PCIe 4.0 x16 (24.4 GB/s measured) and PCIe 5.0 x16 (45+ GB/s)
inline bool mtp_auto_pays(TierMode mode, bool unified, double h2d_gbps, std::string* why = nullptr) {
auto say = [&](const char* s) { if (why) *why = s; };
if (unified) { say("unified memory: no link to wait for"); return true; }
if (mode == TierMode::A) { say("all experts resident: no link to wait for"); return true; }
if (mode == TierMode::C) { say("the SSD tier is slower than any drafter gain"); return false; }
if (h2d_gbps >= kMtpAutoMinLinkGbps) { say("the host-to-device link has room (>= 32 GB/s)"); return true; }
if (h2d_gbps <= 0.0) { say("the host-to-device link could not be measured"); return false; }
say("tier B over a link below 32 GB/s: the controller never finds a gain");
return false;
}
} // namespace strata::ling32
+2
View File
@@ -206,6 +206,8 @@ if(LING32_CUDA OR LING32_HIP)
set_tests_properties(ling32_mtp_tier_regress PROPERTIES FIXTURES_REQUIRED ling32_wide_mtp TIMEOUT 600)
add_test(NAME ling32_spec_span_regress COMMAND ${Python3_EXECUTABLE} -I ${_tests_dir}/ling32_spec_span_test.py $<TARGET_FILE:strata-ling32> ${_wide} ${_wide_mtp} ${CMAKE_BINARY_DIR}/spec_span_work)
set_tests_properties(ling32_spec_span_regress PROPERTIES FIXTURES_REQUIRED ling32_wide_mtp TIMEOUT 600)
add_test(NAME ling32_mtp_auto_regress COMMAND ${Python3_EXECUTABLE} -I ${_tests_dir}/ling32_mtp_auto_test.py $<TARGET_FILE:strata-ling32> ${_wide} ${_wide_mtp} ${CMAKE_BINARY_DIR}/mtp_auto_work)
set_tests_properties(ling32_mtp_auto_regress PROPERTIES FIXTURES_REQUIRED ling32_wide_mtp TIMEOUT 600)
add_test(NAME ling32_kv_grow_regress COMMAND ${Python3_EXECUTABLE} -I ${_tests_dir}/ling32_kv_grow_test.py $<TARGET_FILE:strata-ling32> ${_wide} ${_wide_mtp} ${CMAKE_BINARY_DIR}/kv_grow_work)
set_tests_properties(ling32_kv_grow_regress PROPERTIES FIXTURES_REQUIRED ling32_wide_mtp TIMEOUT 900)
endif()
+63 -3
View File
@@ -87,7 +87,7 @@ int cmd_info_kv(const std::string& file) {
Ling32File f;
std::string err;
if (!f.open(file, err, false)) { std::fprintf(stderr, "strata-ling32: %s\n", err.c_str()); return 1; }
std::printf("arch=ling32 layers=%d vocab=%d drafter=none mtp=1 images=1 tiers=A,B,C\n", f.cfg().n_layers, f.cfg().V); // mtp=1: this engine takes --mtp FILE --spec mtp
std::printf("arch=ling32 layers=%d vocab=%d drafter=none mtp=1 mtp_auto=1 images=1 tiers=A,B,C\n", f.cfg().n_layers, f.cfg().V); // mtp=1: this engine takes --mtp FILE --spec mtp; mtp_auto=1: and --mtp-auto FILE
return 0;
}
@@ -195,7 +195,8 @@ struct RunOpts {
// speculative decoding (MTP drafting)
bool spec = false; // --spec mtp
bool spec_mtp = false; // --spec mtp: the MTP layer of the sidecar (--mtp FILE) drafts
std::string mtp_file; // --mtp FILE: Ling-3.2-Air-MTP.strata
std::string mtp_file; // --mtp FILE: Ling-3.2-Air-MTP.strata (loads the sidecar whatever the card)
bool mtp_auto = false; // --mtp-auto FILE: the sidecar is loaded only where it measurably pays (tier A, unified memory, tier B over a host-to-device link of 32 GB/s or more); see mtp_auto_pays
bool spec_block_set = false;
int spec_block = 8; // --spec-block N: slots drafted per cycle (<= 8)
bool spec_adaptive = false; // --spec-adaptive: the guide's EMA controller picks K_max (0..--spec-block) per cycle
@@ -626,7 +627,44 @@ void print_spec_stats(const SpecStats& s, double t_gen, size_t ntok) {
std::printf("\n");
}
int cmd_run(const RunOpts& o) {
// one pinned 256 MiB host-to-device copy, three repeats (after a warm-up), the median in GB/s (1e9 bytes per second); 0 when it cannot be measured
double measure_h2d_gbps(int device) {
if (const char* ov = std::getenv("STRATA_L32_H2D_GBPS")) return std::atof(ov); // test hook: the link figure by hand (ling32_mtp_auto_regress)
constexpr size_t n = 256ull << 20;
if (cudaSetDevice(device) != cudaSuccess) return 0.0;
void* h = nullptr;
void* d = nullptr;
cudaStream_t st = nullptr;
cudaEvent_t e0 = nullptr, e1 = nullptr;
double med = 0.0;
if (cudaMallocHost(&h, n) == cudaSuccess && cudaMalloc(&d, n) == cudaSuccess && cudaStreamCreate(&st) == cudaSuccess && cudaEventCreate(&e0) == cudaSuccess && cudaEventCreate(&e1) == cudaSuccess) {
std::memset(h, 1, n);
cudaMemcpyAsync(d, h, n, cudaMemcpyHostToDevice, st);
cudaStreamSynchronize(st);
double v[3] = {0, 0, 0};
for (int i = 0; i < 3; ++i) {
cudaEventRecord(e0, st);
cudaMemcpyAsync(d, h, n, cudaMemcpyHostToDevice, st);
cudaEventRecord(e1, st);
cudaEventSynchronize(e1);
float ms = 0.0f;
if (cudaEventElapsedTime(&ms, e0, e1) == cudaSuccess && ms > 0.0f) v[i] = static_cast<double>(n) / (static_cast<double>(ms) * 1e6);
}
std::sort(v, v + 3);
med = v[1];
}
if (e0) cudaEventDestroy(e0);
if (e1) cudaEventDestroy(e1);
if (st) cudaStreamDestroy(st);
if (d) cudaFree(d);
if (h) cudaFreeHost(h);
(void)cudaGetLastError();
return med;
}
int cmd_run(const RunOpts& o_in) {
RunOpts o = o_in; // --mtp-auto may drop the sidecar after the plan is known
double h2d_gbps = 0.0; // the measured host-to-device link (tier B / C), 0 = not measured
if (o.serve) { // only protocol lines on stdout: keep a dup of it for them, point the process's own stdout (reports, self-check, store logs) at stderr
std::fflush(stdout);
#ifdef _WIN32
@@ -723,6 +761,26 @@ int cmd_run(const RunOpts& o) {
for (const auto& n : plan.notes) std::printf(" note: %s\n", n.c_str());
tiered = plan.mode != TierMode::A;
}
if (o.mtp_auto && !o.mtp_file.empty()) { // --mtp-auto: the sidecar only where it pays (tier A, unified memory, tier B over a fast link)
std::string why;
if (tiered) h2d_gbps = measure_h2d_gbps(o.device);
const bool pays = mtp_auto_pays(tiered ? plan.mode : TierMode::A, integrated, h2d_gbps, &why);
if (tiered) std::printf("tier store: host-to-device link moved %.1f GB/s (pinned 256 MiB copies, median of 3)\n", h2d_gbps);
std::printf("MTP sidecar (--mtp-auto): %s: %s\n", pays ? "loaded" : "not loaded (--mtp FILE forces it)", why.c_str());
if (!pays) {
o.mtp_file.clear();
o.spec_mtp = false;
o.spec = false; // the speculative loop is the drafter's: a plain run follows
if (tiered) { // the plan counted the sidecar's bytes: plan again without it
RunOpts po = o;
std::string perr2;
if (make_plan(f, po, plan_vram, plan_ram, plan, rt, perr2)) { std::printf("%s\n", plan.describe().c_str()); tiered = plan.mode != TierMode::A; }
}
}
} else if (tiered && o.tier != "vram") {
h2d_gbps = measure_h2d_gbps(o.device);
std::printf("tier store: host-to-device link moved %.1f GB/s (pinned 256 MiB copies, median of 3)\n", h2d_gbps);
}
}
GpuModel M;
LoadOptions lo;
@@ -909,6 +967,7 @@ int cmd_run(const RunOpts& o) {
sc.ram_ranks = plan.ram_ranks;
sc.ssd_ranks = std::max(0, f.cfg().n_exp - plan.vram_ranks - plan.ram_ranks);
sc.dyn_slots = plan.dyn_slots;
sc.h2d_gbps = h2d_gbps;
sc.lookahead_k = plan.lookahead_k;
sc.prefetch = plan.prefetch;
sc.turbo_ssd = plan.turbo_ssd;
@@ -1634,6 +1693,7 @@ int main(int argc, char** argv) {
else if (a == "--layers-for") o.layers_for = nx();
else if (a == "--spec") { const std::string v = nx(); if (v == "dfly") { std::fprintf(stderr, "strata-ling32: --spec dfly was removed: the speculative drafter is the MTP layer (--mtp Ling-3.2-Air-MTP.strata --spec mtp)\n"); return 2; } o.spec = v == "mtp"; o.spec_mtp = v == "mtp"; }
else if (a == "--mtp") o.mtp_file = nx();
else if (a == "--mtp-auto") { o.mtp_file = nx(); o.mtp_auto = true; }
else if (a == "--spec-block") { o.spec_block = std::atoi(nx().c_str()); o.spec_block_set = true; }
else if (a == "--genjobs") o.genjobs = nx();
else if (a == "--dump-hidden-jobs") o.hidden_jobs = nx();
+3 -2
View File
@@ -71,6 +71,7 @@ struct ServeCfg {
std::string tier = "A";
int vram_ranks = 0, ram_ranks = 0, ssd_ranks = 0, dyn_slots = 0, lookahead_k = 0;
bool prefetch = false, turbo_ssd = false;
double h2d_gbps = 0.0; // the measured host-to-device link at start (tier B / C), 0 = not measured
std::function<ServeCounters()> counters; // the store's counters (null: Mode A)
// speculative generation (null: off): runs a whole request, calling emit(token) for every generated token (false = stop); the request's first token included
using SpecRun = std::function<bool(const std::vector<int>& ids, int limit, float temp, float top_p, int top_k, uint64_t seed, int resume_from, const std::function<bool(int)>& emit, ServeSpecResult& res, std::string& err)>;
@@ -267,9 +268,9 @@ inline int ling32_serve(Ling32Engine& E, const ServeCfg& cfg) {
using namespace serve_detail;
const int V = cfg.vocab, max_ctx = cfg.max_ctx;
proto("INFO engine=strata-ling32-serve-0 arch=ling32 backend=cuda model=%s layers=%d vocab=%d chunk=%d session_reuse=1 tier=%s vram_ranks=%d ram_ranks=%d ssd_ranks=%d dyn_slots=%d lookahead_k=%d "
"prefetch=%d turbo_ssd=%d spec=%s images=%d%s",
"prefetch=%d turbo_ssd=%d spec=%s h2d_gbps=%.1f images=%d%s",
cfg.model_base.c_str(), cfg.layers, V, cfg.chunk, cfg.tier.c_str(), cfg.vram_ranks, cfg.ram_ranks, cfg.ssd_ranks, cfg.dyn_slots, cfg.lookahead_k, cfg.prefetch ? 1 : 0, cfg.turbo_ssd ? 1 : 0,
cfg.spec_run ? "mtp" : "off", cfg.vision ? 1 : 0, cfg.vision ? (" vision=" + cfg.vision->backend()).c_str() : "");
cfg.spec_run ? "mtp" : "off", cfg.h2d_gbps, cfg.vision ? 1 : 0, cfg.vision ? (" vision=" + cfg.vision->backend()).c_str() : "");
proto("READY %d stop", max_ctx);
LineInput in;
@@ -0,0 +1,89 @@
#!/usr/bin/env python3
"""ling32_mtp_auto_test.py - --mtp-auto: the MTP sidecar is loaded only where it pays (the wiring of the engine, on the wide fixture).
python ling32_mtp_auto_test.py STRATA_LING32_EXE WIDE_FIXTURE WIDE_MTP_FIXTURE WORKDIR
The rule (mtp_auto_pays in ling32_tier_planner.hpp, table-tested in ling32_tier_planner_test) decides from the memory tier and the measured host-to-device link; STRATA_L32_H2D_GBPS replaces the measurement here.
Legs (greedy, 24 generated tokens, 300-token prompt):
tier B, link 24.4 GB/s (a PCIe 4.0 x16 card) --mtp-auto: 'not loaded', no speculative decoding, the plan is made again without the sidecar
tier B, link 46 GB/s (PCIe 5.0 x16) --mtp-auto: 'loaded', speculative decoding on
tier B, link 24.4 GB/s --mtp FILE (forced): the sidecar is loaded and speculation runs
tier A (all resident) --mtp-auto: 'loaded'
Every leg must give the ids of the plain run (greedy speculation is exact). Exit code 0 = pass.
"""
import json
import os
import random
import subprocess
import sys
def run(exe, wide, jobs, out, extra, link=None):
os.makedirs(out, exist_ok=True)
env = dict(os.environ)
env.pop("STRATA_L32_H2D_GBPS", None)
if link is not None:
env["STRATA_L32_H2D_GBPS"] = str(link)
cmd = [exe, "--model", wide, "--genjobs", jobs, "--out", out, "--ctx", "400", "--no-selfcheck", "--chunk", "64", "--stop", "999999"] + extra
p = subprocess.run(cmd, capture_output=True, text=True, errors="replace", env=env)
txt = p.stdout + p.stderr
gj = os.path.join(out, "p300.gen.json")
if p.returncode != 0 or not os.path.exists(gj):
return None, txt, "exit %d: %s" % (p.returncode, " | ".join(txt.strip().splitlines()[-3:]))
return json.load(open(gj)).get("gen_ids"), txt, ""
def main():
exe, wide, mtp, work = sys.argv[1:5]
os.makedirs(work, exist_ok=True)
rnd = random.Random(20261203)
ids = [rnd.randrange(1, 500) for _ in range(300)]
jobs = os.path.join(work, "jobs.jsonl")
with open(jobs, "w") as f:
f.write('{"id":"p300","prompt_ids":[' + ",".join(map(str, ids)) + '],"gen":24}\n')
tier_b = ["--tier", "hybrid", "--vram-budget-gib", "3", "--ram-budget-gib", "4"]
spec = ["--spec", "mtp", "--spec-adaptive"]
legs = [
# name, extra args, link, expected 'MTP sidecar (--mtp-auto): ' text, whether speculation runs
("plain tier B", tier_b, None, None, False),
("auto, tier B, link 24.4", tier_b + ["--mtp-auto", mtp] + spec, 24.4, "not loaded", False),
("auto, tier B, link 46", tier_b + ["--mtp-auto", mtp] + spec, 46.0, "loaded", True),
("forced, tier B, link 24.4", tier_b + ["--mtp", mtp] + spec, 24.4, None, True),
("auto, tier A", ["--mtp-auto", mtp] + spec, None, "loaded", True),
]
ref = None
for i, (name, extra, link, want, spec_on) in enumerate(legs):
got, txt, err = run(exe, wide, jobs, os.path.join(work, "leg%d" % i), extra, link)
if got is None:
print("FAIL: %s: %s" % (name, err))
return 1
decided = [l for l in txt.splitlines() if l.startswith("MTP sidecar (--mtp-auto): ")]
spec_line = any(l.startswith("speculative decoding: MTP") for l in txt.splitlines())
print("%-28s %d ids, first %s, %s, speculation %s" % (name, len(got), got[:5], decided[0][26:60] if decided else "(no decision line)", "on" if spec_line else "off"))
if want is not None:
if not decided:
print("FAIL: %s: no 'MTP sidecar (--mtp-auto):' line" % name)
return 1
tag = decided[0][len("MTP sidecar (--mtp-auto): "):]
if not tag.startswith(want) or (want == "loaded" and tag.startswith("not loaded")):
print("FAIL: %s: expected '%s', got '%s'" % (name, want, tag[:60]))
return 1
elif decided:
print("FAIL: %s: a decision line without --mtp-auto" % name)
return 1
if spec_line != spec_on:
print("FAIL: %s: speculation %s, expected %s" % (name, "on" if spec_line else "off", "on" if spec_on else "off"))
return 1
if ref is None:
ref = got
elif got != ref:
print("FAIL: %s differs from the plain run" % name)
return 1
print("PASS: --mtp-auto loads the sidecar in tier A and over a fast link, drops it (and the speculation) over a slow link, --mtp forces it; greedy ids are the same in every leg")
return 0
if __name__ == "__main__":
sys.exit(main())
@@ -178,6 +178,18 @@ int main(int argc, char** argv) {
static_cast<unsigned long long>(rt.max_slab));
CHECK(rt.total == f.section_bytes(4));
}
{ // --mtp-auto: the measured rows (see mtp_auto_pays)
std::string why;
CHECK(mtp_auto_pays(TierMode::A, false, 0.0, &why) && !why.empty()); // everything resident: pays (5090 tier A, PRO 6000, Spark)
CHECK(mtp_auto_pays(TierMode::B, true, 0.0)); // unified memory (Mac, Spark, Strix): pays
CHECK(mtp_auto_pays(TierMode::B, false, 46.0, &why)); // RTX 5090, PCIe 5.0 x16: +11 % decode
CHECK(mtp_auto_pays(TierMode::B, false, 32.0)); // the threshold itself
CHECK(!mtp_auto_pays(TierMode::B, false, 24.4, &why) && !why.empty()); // RTX 3090 / 4090, PCIe 4.0 x16: 1.00 tokens per verify
CHECK(!mtp_auto_pays(TierMode::B, false, 12.0)); // PCIe 3.0 x16 / 4.0 x8 (5060 Ti, 5080 16 GB): -3 %
CHECK(!mtp_auto_pays(TierMode::B, false, 0.0, &why) && !why.empty()); // link not measured: not loaded (--mtp forces it)
CHECK(!mtp_auto_pays(TierMode::C, false, 50.0)); // SSD tier: never
CHECK(kMtpAutoMinLinkGbps > 24.4 && kMtpAutoMinLinkGbps < 45.0);
}
std::printf(g_fail ? "FAILED (%d)\n" : "ling32_tier_planner_test: all passed\n", g_fail);
return g_fail ? 1 : 0;
}
+39 -6
View File
@@ -32310,7 +32310,7 @@
"reads": [
{
"file": "engine/src/ling32/ling32_main.cpp",
"line": 1113,
"line": 1172,
"backend": "cuda-hip",
"fn": "getenv"
}
@@ -32368,6 +32368,39 @@
"retire_after": "",
"retired_in": ""
},
{
"name": "STRATA_L32_H2D_GBPS",
"backends": [
"cuda-hip"
],
"reads": [
{
"file": "engine/src/ling32/ling32_main.cpp",
"line": 632,
"backend": "cuda-hip",
"fn": "getenv"
}
],
"parse": "float",
"default_state": null,
"code_default": null,
"engine_sets": [],
"engine_defaults": [],
"launch_files": [],
"launch_sets": [],
"launch_reads": [],
"doc_mentions": [],
"first_commit": null,
"first_date": null,
"since": null,
"last_commit": null,
"last_date": null,
"status": "research",
"owner": "L32",
"note": "test hook: replaces the measured host-to-device link (GB/s) that --mtp-auto and the serve INFO line use (ling32_mtp_auto_regress)",
"retire_after": "",
"retired_in": ""
},
{
"name": "STRATA_L32_KDA_SEQ2",
"backends": [
@@ -32442,7 +32475,7 @@
"reads": [
{
"file": "engine/src/ling32/ling32_main.cpp",
"line": 212,
"line": 213,
"backend": "cuda-hip",
"fn": "getenv"
}
@@ -32607,7 +32640,7 @@
"reads": [
{
"file": "engine/src/ling32/ling32_main.cpp",
"line": 1657,
"line": 1717,
"backend": "cuda-hip",
"fn": "getenv"
}
@@ -32772,13 +32805,13 @@
"reads": [
{
"file": "engine/src/ling32/ling32_main.cpp",
"line": 336,
"line": 337,
"backend": "cuda-hip",
"fn": "getenv"
},
{
"file": "engine/src/ling32/ling32_main.cpp",
"line": 743,
"line": 801,
"backend": "cuda-hip",
"fn": "getenv"
}
@@ -33576,7 +33609,7 @@
"reads": [
{
"file": "engine/src/ling32/ling32_main.cpp",
"line": 802,
"line": 860,
"backend": "cuda-hip",
"fn": "getenv"
}