diff --git a/src/program/generate.cpp b/src/program/generate.cpp index 1091fa9f49..c724017e8b 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -933,6 +933,7 @@ struct Options { bool draft_auto = false; std::string draft_rule = "df2"; ///< --draft-rule df2|mtp|learn: what a request without a draft= key gets int draft_mtp_spec = 4; ///< --draft-mtp-spec N: the widest window of a request that drafts with the MTP layer under --draft auto + bool draft_mtp_spec_set = false; ///< --draft-mtp-spec was given: it is also the window the MTP runs when --draft dflash2 falls back to it (the wider --spec was for DFlash2) double draft_mtp_min_p = -1.0; ///< --draft-mtp-min-p P: the MTP requests' --spec-min-p under --draft auto (-1 = --spec-min-p) int draft_cap = 0; ///< --draft-cap N: the widest verify window with --draft dflash2 (0 = by card class, see where the drafter loads) /// A control vector on the residual stream (strata/kernels/cvec.hpp), with llama.cpp's flags: the @@ -3504,7 +3505,7 @@ int main(int argc, char** argv) { } } else if (a == "--draft-rule") o.draft_rule = next("--draft-rule"); - else if (a == "--draft-mtp-spec") o.draft_mtp_spec = std::max(2, std::atoi(next("--draft-mtp-spec"))); + else if (a == "--draft-mtp-spec") { o.draft_mtp_spec = std::max(2, std::atoi(next("--draft-mtp-spec"))); o.draft_mtp_spec_set = true; } else if (a == "--draft-mtp-min-p") o.draft_mtp_min_p = std::clamp(std::atof(next("--draft-mtp-min-p")), 0.0, 1.0); else if (a == "--draft-model") o.draft_model = next("--draft-model"); else if (a == "--draft-conf") o.draft_conf = std::clamp(std::atof(next("--draft-conf")), 0.0, 1.0); @@ -6608,6 +6609,8 @@ int main(int argc, char** argv) { } else { std::fprintf(stderr, "strata generate: --draft dflash2 is off: %s; the MTP drafts instead\n", why); } + // DFlash2 did not start: the MTP drafts, with its own window (the wider --spec was DFlash2's), when --draft-mtp-spec names it + if (!use_df2 && o.draft_mtp_spec_set && o.spec > o.draft_mtp_spec) o.spec = o.draft_mtp_spec; } #endif if (batch_mtp) { @@ -10601,6 +10604,7 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata serve: --draft dflash2: %s. It needs about 1.2 GiB of VRAM in all (the pack 0.76 GiB, buffers and K/V ring 0.25 GiB, " "a draft head 0.2 GiB, 0.1 GiB of prompt staging); --vram-reserve-mib 1900 keeps it. The MTP drafts instead.\n", why_not.c_str()); use_df2 = false; + if (o.draft_mtp_spec_set && o.spec > o.draft_mtp_spec) o.spec = o.draft_mtp_spec; // the MTP drafts with its own window, not DFlash2's } else { sp.on_taps = [&](const float* taps, int64_t rows, int64_t pos0, std::string& e) -> bool { return df2.context(taps, rows, pos0, e); }; std::fprintf(stderr, "strata serve: DFlash2 drafter on (%s, graphs captured in %.0f ms); windows up to %d tokens; the MTP layer stays as the fallback\n", @@ -18831,6 +18835,7 @@ int main(int argc, char** argv) { use_df2 = false; } if (use_df2) std::fprintf(stderr, "strata generate: DFlash2 drafter on (%s, graphs captured in %.0f ms), the MTP layer stays as the fallback\n", o.draft_model.c_str(), df2.ms_capture); + else if (o.draft == "dflash2" && o.draft_mtp_spec_set && o.spec > o.draft_mtp_spec) o.spec = o.draft_mtp_spec; // fell back: the MTP's own window #endif mem_mark("the verifier and the drafter's binding"); ver.set_sampling(sp); // the CLI's own sampling (until 0.1.19 this loop was always greedy); no penalties here diff --git a/strata_control/drafting.py b/strata_control/drafting.py index b88a5fc35c..5d9a3182a2 100644 --- a/strata_control/drafting.py +++ b/strata_control/drafting.py @@ -166,6 +166,7 @@ PACK_NAME = "qwen3.8-flash-next-dflash2.q8.pack" VRAM_GB = 1.2 # what the drafter needs in graphics memory (the engine's own estimate) FOLDER = "dflash2" # under the model data folder, next to ``mtp`` SPEC_WINDOW = 6 # --spec with DFlash2: the widest window of drafted words per round (measured best on Blackwell; the engine caps it per card) +MTP_WINDOW = 4 # the MTP's usual --spec: the window it runs when DFlash2 falls back to it, unless the config names another DRAFT_CONF = 0.35 # --draft-conf with DFlash2: the window ends where the chance that every draft so far holds falls below this #: model families (strata launch --family) the drafter was trained for and measured on: Qwen3.8-Flash-Next and its Gyro build. @@ -275,11 +276,11 @@ def needs_pack(mode: Any, *, klass: str, model_name: str | None = None, family: return want == "dflash2" and ENGINE_SERVES_DFLASH2 and supported_model(model_name, family) and not pack_ready(data_dir) -DRAFT_FLAGS = {"--draft": True, "--draft-model": True, "--draft-conf": True} +DRAFT_FLAGS = {"--draft": True, "--draft-model": True, "--draft-conf": True, "--draft-mtp-spec": True} def strip_draft_flags(args: list[Any]) -> list[str]: - """The arguments without ``--draft`` / ``--draft-model`` / ``--draft-conf`` (and their values).""" + """The arguments without ``--draft`` / ``--draft-model`` / ``--draft-conf`` / ``--draft-mtp-spec`` (and their values).""" out: list[str] = [] items = [str(a) for a in args] i = 0 @@ -344,7 +345,12 @@ def apply(cfg: dict[str, Any], *, klass: str, data_dir: str | Path | None = None if kept_conf is not None: flags[flags.index("--draft-conf") + 1] = kept_conf if _flag_value(mine, "--spec") is None: - args = _without_flag(args, "--spec") # the person's own --spec stays; otherwise the window is the DFlash2 one + # The person's own --spec stays; otherwise the window is the DFlash2 one. If the engine then falls back to the MTP (the card is too + # full for the drafter), the MTP must run its usual window, not DFlash2's: --draft-mtp-spec names it (the engine uses it for that). + mtp_window = _flag_value(args, "--spec") + args = _without_flag(args, "--spec") + kept_window = _flag_value(mine, "--draft-mtp-spec") + flags += ["--draft-mtp-spec", kept_window if kept_window is not None else mtp_window if (mtp_window or "").isdigit() and int(mtp_window) >= 2 else str(MTP_WINDOW)] else: i = flags.index("--spec") del flags[i:i + 2] diff --git a/tests/terminal/test_drafting.py b/tests/terminal/test_drafting.py index ff09bba381..a74e651504 100644 --- a/tests/terminal/test_drafting.py +++ b/tests/terminal/test_drafting.py @@ -175,12 +175,50 @@ class EngineArguments(unittest.TestCase): d = drafting.apply(c, klass="blackwell", data_dir=self.data) self.assertEqual(d.drafter, "dflash2") spec_at = self.ARGS.index("--spec") - self.assertEqual(c["args"], [*self.ARGS[:spec_at], *self.ARGS[spec_at + 2:], "--draft", "dflash2", "--draft-model", str(pack), "--spec", "6", "--draft-conf", "0.35"], - "the MTP window of 4 gives way to DFlash2's 6; the MTP's own arguments stay") + self.assertEqual(c["args"], [*self.ARGS[:spec_at], *self.ARGS[spec_at + 2:], "--draft", "dflash2", "--draft-model", str(pack), "--spec", "6", "--draft-conf", "0.35", "--draft-mtp-spec", "4"], + "the MTP window of 4 gives way to DFlash2's 6 (and is named for the fallback); the MTP's own arguments stay") self.assertEqual(c["args"].count("--draft"), 1) self.assertEqual(c["args"].count("--spec"), 1) self.assertIn("--mtp", c["args"], "the MTP stays as the fallback the engine names") + def test_a_fallback_to_the_mtp_keeps_the_mtps_own_window(self): + """DFlash2 starts with --spec 6; when the engine falls back to the MTP the window must be the MTP's usual one, named with --draft-mtp-spec.""" + pack = drafting.pack_path(self.data) + pack.parent.mkdir(parents=True) + pack.write_bytes(b"x" * (2 << 20)) + with serving_dflash2(): + c = self.cfg(drafting="dflash2") + drafting.apply(c, klass="blackwell", data_dir=self.data) + args = c["args"] + self.assertEqual(args[args.index("--draft-mtp-spec") + 1], "4", "the window the config had for the MTP") + self.assertEqual(args[args.index("--spec") + 1], "6") + # a config whose MTP window is 3 (a Mac preset) falls back to 3, and one with no window to the usual 4 + c = self.cfg(drafting="dflash2", args=[a if a != "4" else "3" for a in self.ARGS]) + drafting.apply(c, klass="blackwell", data_dir=self.data) + self.assertEqual(c["args"][c["args"].index("--draft-mtp-spec") + 1], "3") + c = self.cfg(drafting="dflash2", args=[a for i, a in enumerate(self.ARGS) if i not in (self.ARGS.index("--spec"), self.ARGS.index("--spec") + 1)]) + drafting.apply(c, klass="blackwell", data_dir=self.data) + self.assertEqual(c["args"][c["args"].index("--draft-mtp-spec") + 1], str(drafting.MTP_WINDOW)) + # a window the person typed by hand is theirs: DFlash2 and the fallback both run it, nothing is added + c = self.cfg(drafting="dflash2", args=[*self.ARGS[:self.ARGS.index("--spec")], "--spec", "5"], user_args=["--spec", "5"]) + drafting.apply(c, klass="blackwell", data_dir=self.data) + self.assertNotIn("--draft-mtp-spec", c["args"]) + self.assertEqual(c["args"][c["args"].index("--spec") + 1], "5") + # a --draft-mtp-spec the person typed stays as typed + c = self.cfg(drafting="dflash2", args=[*self.ARGS, "--draft-mtp-spec", "2"], user_args=["--draft-mtp-spec", "2"]) + drafting.apply(c, klass="blackwell", data_dir=self.data) + self.assertEqual((c["args"].count("--draft-mtp-spec"), c["args"][c["args"].index("--draft-mtp-spec") + 1]), (1, "2")) + # choosing the MTP again leaves no trace of it + c = self.cfg(drafting="mtp", args=[*self.ARGS, "--draft", "dflash2", "--draft-model", "/p", "--draft-mtp-spec", "4"]) + drafting.apply(c, klass="blackwell", data_dir=self.data) + self.assertEqual(c["args"], self.ARGS) + + def test_the_engine_uses_the_named_window_when_dflash2_falls_back(self): + """The engine side (src/program/generate.cpp): the flag marks itself as given, and each place DFlash2 falls back to the MTP applies it.""" + src = (ROOT / "src" / "program" / "generate.cpp").read_text(encoding="utf-8") + self.assertIn("o.draft_mtp_spec_set = true", src) + self.assertEqual(src.count("o.spec = o.draft_mtp_spec;"), 3, "the CLI start, the CLI loop and serve") + def test_mtp_removes_a_drafter_that_was_written_by_hand(self): c = self.cfg(drafting="mtp", args=[*self.ARGS, "--draft", "dflash2", "--draft-model", "/p"]) drafting.apply(c, klass="blackwell", data_dir=self.data) @@ -208,7 +246,7 @@ class EngineArguments(unittest.TestCase): with mock.patch("serve.server.drafting_card_class", return_value="blackwell"), serving_dflash2(), \ mock.patch.object(drafting, "pack_ready", return_value=True), mock.patch.object(drafting, "data_dir_of", return_value=Path("/d")): args = engine_args(self.cfg(drafting="dflash2")) - self.assertEqual(args[-8:], ["--draft", "dflash2", "--draft-model", str(pack), "--spec", "6", "--draft-conf", "0.35"]) + self.assertEqual(args[-10:], ["--draft", "dflash2", "--draft-model", str(pack), "--spec", "6", "--draft-conf", "0.35", "--draft-mtp-spec", "4"]) with mock.patch("serve.server.drafting_card_class", return_value="blackwell"), mock.patch.object(drafting, "ENGINE_SERVES_DFLASH2", False): self.assertEqual(engine_args(self.cfg()), self.ARGS, "no choice: nothing changes") off = engine_args(self.cfg(drafting="off"))