From f201b033f06b046b08aecb2fa00ea2bc598ed880 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 16 Nov 2026 17:22:47 +0100 Subject: [PATCH] drafting: DFlash2 on the Coder when it is chosen by name (#5240) DFlash2 chosen by name (strata launch --drafting dflash2, or a run config's "drafting": "dflash2") on the Coder (ISTA-DASLab's coding version of the same weights) started the MTP with "DFlash2 is for Qwen3.8-Flash-Next only". It starts DFlash2 now, with the stock pack (no tuned pack for the Coder), and setup fetches that pack for the named choice. Auto (and no choice) keeps the MTP on the Coder; Swift and NVFP4 stay refused. drafting.supported_model(..., named=True) also takes NAMED_ONLY_FAMILIES ("coder") and qwen3.8-flash-next-coder-*; choice_of / decide / apply pass named when the choice is dflash2. The app follows: policy-generated.mjs (DFLASH2_NAMED_FAMILIES, DFLASH2_NAMED_PATTERN, from app_policy.py), policy-vectors.json ("named"), resolveDrafter in native-settings.mjs and in the screen copy src/lib/drafting.ts. DRAFTING_FAMILIES is unchanged, so the app shows no Drafting dropdown for the Coder. Measured on a Tesla V100-SXM2 32 GB (Volta; the reporter used an RTX 4090), Coder IQ1_M fully resident, r060 engine, greedy, 32K context, MTP vs DFlash2 stock pack: decode 83.3 -> 95.4 tok/s (short), 77.6 -> 86.4 (4K prompt), 82.5 -> 90.4 (16K prompt); drafts accepted 151/291 -> 157/250, 220/454 -> 221/364, 330/605 -> 314/465; answers identical token for token in all three; prompt reading equal. Tests: tests/terminal/test_drafting_coder_named.py; older tests that pinned "never on the Coder" now pin it for Auto (Swift stands in for the refused named case); drafting/policy tests 735 passed on Boston; app node tests (policy-generated, drafting, df2ux-0213) 35/35 on p1006. --- apps/host/server/native-settings.mjs | 7 +- apps/host/server/policy-generated.mjs | 3 + apps/host/src/lib/drafting.ts | 5 +- apps/host/tests/data/policy-vectors.json | 51 +++-- apps/host/tests/policy-generated.test.mjs | 18 +- strata_control/app_policy.py | 220 ++++++++++---------- strata_control/drafting.py | 30 ++- tests/terminal/test_drafting.py | 20 +- tests/terminal/test_drafting_coder_named.py | 66 ++++++ tests/terminal/test_drafting_hardware.py | 4 +- tests/terminal/test_drafting_single.py | 2 + 11 files changed, 274 insertions(+), 152 deletions(-) create mode 100644 tests/terminal/test_drafting_coder_named.py diff --git a/apps/host/server/native-settings.mjs b/apps/host/server/native-settings.mjs index bdcdb2c4a7..e9bd5919e5 100644 --- a/apps/host/server/native-settings.mjs +++ b/apps/host/server/native-settings.mjs @@ -1,5 +1,5 @@ import { validMcpName, MCP_NAME_HELP } from './mcp-names.mjs'; -import { SPEED_SWITCH_TABLE, DRAFTING_FAMILIES as GENERATED_DRAFTING_FAMILIES, DFLASH2_MODEL_PATTERN } from './policy-generated.mjs'; +import { SPEED_SWITCH_TABLE, DRAFTING_FAMILIES as GENERATED_DRAFTING_FAMILIES, DFLASH2_MODEL_PATTERN, DFLASH2_NAMED_FAMILIES, DFLASH2_NAMED_PATTERN } from './policy-generated.mjs'; /** Editable native keys. Unknown config keys and program paths stay intact. */ /* The Drafting hint, word for word, with the size of the drafter this model would really fetch (the tuned one is 0.34 GB, 0.52 on an Arc card; the stock one 1.2). */ export function draftingHint(gb=1.2){const size=Number.isFinite(Number(gb))&&Number(gb)>0?Number(gb):1.2;return `Which drafter guesses words ahead to make answers faster, one at a time. Auto (recommended) is DFlash2 for Qwen3.8-Flash-Next where the engine has it, the MTP otherwise; on two or more identical graphics cards that the all-cards split engine takes, it is that engine with the MTP draft layer. DFlash2 downloads a ${size} GB drafter once, the MTP its draft layer once. With Auto, several requests at once use the MTP. If you choose DFlash2 yourself, it runs with the number of requests you set. If DFlash2 cannot run, the MTP is used alone and Details says why. Off turns the drafter off.`;} @@ -78,11 +78,14 @@ export const APP_OWNED_FIELDS=['mtp','batchMtp','drafting']; /** The families that have a Drafting choice and the model names the DFlash2 drafter was made for (strata_control/drafting.py SUPPORTED_FAMILIES, _SUPPORTED_NAME): Qwen3.8-Flash-Next, Gyro and Unsloth's quantizations of the same weights; not Swift, Coder or NVFP4. */ export const DRAFTING_FAMILIES=GENERATED_DRAFTING_FAMILIES; const DFLASH2_MODEL=new RegExp(DFLASH2_MODEL_PATTERN,'i'); +const DFLASH2_NAMED=new RegExp(DFLASH2_NAMED_PATTERN,'i'); /** The drafter a choice means (one per run; strata_control/drafting.py choice_of): Auto is DFlash2 where the engine has it (`served`, Control's word) and the model is one it was made for, else the MTP; off and mtp are what they say. `model` is a run config's model name or a family. */ /** `split` (Control's plan: the all-cards split engine will take this start) makes Auto the MTP, the draft layer that engine runs; a DFlash2 the person chose by name stays. */ export function resolveDrafter(choice,model,served,split){ const c=String(choice??'auto').toLowerCase();if(c==='off'||c==='mtp')return c; - const name=String(model??'');if(!(served===true&&(DRAFTING_FAMILIES.includes(name.toLowerCase())||DFLASH2_MODEL.test(name))))return 'mtp'; + const name=String(model??''); + const named=c==='dflash2'&&(DFLASH2_NAMED_FAMILIES.includes(name.toLowerCase())||DFLASH2_NAMED.test(name)); // #5240: the Coder runs DFlash2 when it is chosen by name; Auto stays the MTP there + if(!(served===true&&(DRAFTING_FAMILIES.includes(name.toLowerCase())||DFLASH2_MODEL.test(name)||named)))return 'mtp'; return split===true&&c!=='dflash2'?'mtp':'dflash2'; } /** The engine arguments without the draft layer: --mtp , --spec-min-p

(its confidence threshold) and --batch-mtp. --spec stays: prompt lookup still uses its windows. Same rule as strata_control/draft_layer.py. */ diff --git a/apps/host/server/policy-generated.mjs b/apps/host/server/policy-generated.mjs index f0cb5c3763..a78cd0fcf6 100644 --- a/apps/host/server/policy-generated.mjs +++ b/apps/host/server/policy-generated.mjs @@ -11,3 +11,6 @@ export const SPEED_SWITCH_TABLE = [ export const DRAFTING_FAMILIES = ["qwen", "gyro", "unsloth"]; /** The model names the DFlash2 drafter was made for (strata_control/drafting.py _SUPPORTED_NAME), a case-insensitive pattern. */ export const DFLASH2_MODEL_PATTERN = "^qwen3\\.8-flash-next(-gyro-[a-z0-9._-]+|-unsloth-[a-z0-9._-]+|-(q|iq|tq)\\d[a-z0-9_.]*)?$"; +/** #5240: the families and model names DFlash2 runs on only when the person chooses it by name (strata_control/drafting.py NAMED_ONLY_FAMILIES, _NAMED_ONLY_NAME). */ +export const DFLASH2_NAMED_FAMILIES = ["coder"]; +export const DFLASH2_NAMED_PATTERN = "^qwen3\\.8-flash-next-coder-[a-z0-9._-]+$"; diff --git a/apps/host/src/lib/drafting.ts b/apps/host/src/lib/drafting.ts index 5045c943d5..5a3c513372 100644 --- a/apps/host/src/lib/drafting.ts +++ b/apps/host/src/lib/drafting.ts @@ -5,12 +5,15 @@ * nothing to pick against it (Settings > Draft layer turns it off). */ export const DRAFTING_FAMILIES = ['qwen', 'gyro', 'unsloth']; +/** The families DFlash2 runs on only when chosen by name (strata_control/drafting.py NAMED_ONLY_FAMILIES, #5240). */ +export const DFLASH2_NAMED_FAMILIES = ['coder']; /** The drafter a choice means for a family (one per run; strata_control/drafting.py choice_of): Auto is DFlash2 where the engine has it (`served`) and the family is one it was made for, else the MTP. */ export function resolveDrafter(choice: string | null | undefined, family: string | null | undefined, served: boolean | null | undefined, split?: boolean | null): 'dflash2' | 'mtp' | 'off' { const c = String(choice ?? 'auto').toLowerCase(); if (c === 'off' || c === 'mtp') return c; - if (!(served === true && DRAFTING_FAMILIES.includes(String(family)))) return 'mtp'; + // #5240: the Coder runs DFlash2 when it is chosen by name (strata_control/drafting.py NAMED_ONLY_FAMILIES); it has no Drafting dropdown, Auto stays the MTP there + if (!(served === true && (DRAFTING_FAMILIES.includes(String(family)) || (c === 'dflash2' && DFLASH2_NAMED_FAMILIES.includes(String(family)))))) return 'mtp'; // `split`: Control's plan says the all-cards split engine takes this start; it runs the MTP, so Auto is the MTP there (a DFlash2 the person chose stays) return split === true && c !== 'dflash2' ? 'mtp' : 'dflash2'; } diff --git a/apps/host/tests/data/policy-vectors.json b/apps/host/tests/data/policy-vectors.json index 38c0e3257d..76370f4f63 100644 --- a/apps/host/tests/data/policy-vectors.json +++ b/apps/host/tests/data/policy-vectors.json @@ -1834,73 +1834,90 @@ "names": [ { "name": "qwen3.8-flash-next", - "supported": true + "supported": true, + "named": true }, { "name": "qwen3.8-flash-next-iq3_s", - "supported": true + "supported": true, + "named": true }, { "name": "qwen3.8-flash-next-Q2_0", - "supported": true + "supported": true, + "named": true }, { "name": "qwen3.8-flash-next-unsloth-ud-q4_k_xl", - "supported": true + "supported": true, + "named": true }, { "name": "qwen3.8-flash-next-unsloth-ud-iq4_xs-v31", - "supported": true + "supported": true, + "named": true }, { "name": "qwen3.8-flash-next-gyro-iq4", - "supported": true + "supported": true, + "named": true }, { "name": "qwen3.8-flash-next-coder-iq1_m", - "supported": false + "supported": false, + "named": true }, { "name": "qwen3.8-flash-next-swift-iq3_s", - "supported": false + "supported": false, + "named": false }, { "name": "swift-1.5-iq3_s", - "supported": false + "supported": false, + "named": false }, { "name": "qwen3.6-35b-a3b-ud-q4_k_xl", - "supported": false + "supported": false, + "named": false }, { "name": "", - "supported": false + "supported": false, + "named": false } ], "families": [ { "family": "qwen", - "supported": true + "supported": true, + "named": true }, { "family": "gyro", - "supported": true + "supported": true, + "named": true }, { "family": "unsloth", - "supported": true + "supported": true, + "named": true }, { "family": "swift", - "supported": false + "supported": false, + "named": false }, { "family": "coder", - "supported": false + "supported": false, + "named": true }, { "family": "nvfp4", - "supported": false + "supported": false, + "named": false } ] } diff --git a/apps/host/tests/policy-generated.test.mjs b/apps/host/tests/policy-generated.test.mjs index d5c4b46ad7..d9a013b601 100644 --- a/apps/host/tests/policy-generated.test.mjs +++ b/apps/host/tests/policy-generated.test.mjs @@ -44,10 +44,18 @@ test('the host shows the speed switches strata_control shows, for every card, en } }); -test('the host and the screen agree with strata_control on which models and families have a Drafting choice', () => { - for (const { name, supported } of V.names) assert.equal(S.resolveDrafter('dflash2', name, true, false) === 'dflash2', supported, name); - for (const { family, supported } of V.families) { - assert.equal(T.resolveDrafter('dflash2', family, true, false) === 'dflash2', supported, `screen: ${family}`); - assert.equal(S.resolveDrafter('dflash2', family, true, false) === 'dflash2', supported, `host: ${family}`); +test('the host and the screen agree with strata_control on which models and families have a Drafting choice', async () => { + // a DFlash2 chosen by name runs where strata_control's named rule says (#5240: also the Coder); Auto only where the model was made for it + for (const { name, supported, named } of V.names) { + assert.equal(S.resolveDrafter('dflash2', name, true, false) === 'dflash2', named, name); + assert.equal(S.resolveDrafter('auto', name, true, false) === 'dflash2', supported, `auto: ${name}`); } + for (const { family, supported, named } of V.families) { + assert.equal(T.resolveDrafter('dflash2', family, true, false) === 'dflash2', named, `screen: ${family}`); + assert.equal(S.resolveDrafter('dflash2', family, true, false) === 'dflash2', named, `host: ${family}`); + assert.equal(T.resolveDrafter('auto', family, true, false) === 'dflash2', supported, `screen auto: ${family}`); + assert.equal(S.resolveDrafter('auto', family, true, false) === 'dflash2', supported, `host auto: ${family}`); + } + const { DFLASH2_NAMED_FAMILIES: generated } = await import('../server/policy-generated.mjs'); + assert.deepEqual(T.DFLASH2_NAMED_FAMILIES, generated, 'the screen copy of the named-only families is the generated list'); }); diff --git a/strata_control/app_policy.py b/strata_control/app_policy.py index 0ce3dbc6e8..73019b5404 100644 --- a/strata_control/app_policy.py +++ b/strata_control/app_policy.py @@ -1,108 +1,112 @@ -"""The launch-policy tables the app's host (JavaScript) reads, generated from the Python that owns them. - -`strata_control` decides the speed switches (`speed_switches.py`) and which models have a Drafting choice (`drafting.py`). The app's host used to carry its own -copy of those tables. It now imports `apps/host/server/policy-generated.mjs`, which this module writes, and a test (`tests/terminal/test_app_policy.py`, and -`apps/host/tests/policy-generated.test.mjs` on the JavaScript side) fails when the committed file differs from what `render()` makes now. - - python -m strata_control.app_policy # rewrite apps/host/server/policy-generated.mjs - python -m strata_control.app_policy --check # exit 1 when it is out of date -""" - -from __future__ import annotations - -import json -import sys -from pathlib import Path - -from strata_control import drafting, speed_switches - -TARGET = Path(__file__).resolve().parents[1] / "apps" / "host" / "server" / "policy-generated.mjs" -VECTORS = Path(__file__).resolve().parents[1] / "apps" / "host" / "tests" / "data" / "policy-vectors.json" - -HEADER = ("// GENERATED by `python -m strata_control.app_policy` from strata_control/speed_switches.py and strata_control/drafting.py. Do not edit it by hand:\n" - "// change the Python and regenerate; tests/terminal/test_app_policy.py and apps/host/tests/policy-generated.test.mjs fail when this file is out of date.\n") - - -def _entries(pairs) -> dict: - return {name: value for name, value in pairs} - - -def table() -> list[dict]: - out = [] - for s in speed_switches.app_switches(): - row = {"id": s.id, "label": s.label, "hint": s.effect, "default": s.default, "env": _entries(s.env)} - if s.legacy: - row["legacy"] = _entries(s.legacy) - if s.group != "Speed": - row["group"] = s.group - if s.words: - row["words"] = _entries(s.words) - out.append(row) - return out - - -def render() -> str: - rows = ",\n".join(" " + json.dumps(r, ensure_ascii=False, separators=(", ", ": ")) for r in table()) - return (HEADER - + "/** The speed switches of the app's Expert settings (strata_control/speed_switches.py app_switches()): id, words, default, the environment entries outside the default. */\n" - + f"export const SPEED_SWITCH_TABLE = [\n{rows},\n];\n" - + "/** The families that have a Drafting choice (strata_control/drafting.py SUPPORTED_FAMILIES). */\n" - + f"export const DRAFTING_FAMILIES = {json.dumps(list(drafting.SUPPORTED_FAMILIES))};\n" - + "/** The model names the DFlash2 drafter was made for (strata_control/drafting.py _SUPPORTED_NAME), a case-insensitive pattern. */\n" - + f"export const DFLASH2_MODEL_PATTERN = {json.dumps(drafting._SUPPORTED_NAME.pattern)};\n") - - -#: Cases the host's copies of the RULES (speedSwitchApplies, resolveDrafter) must answer as Python does. The table above is data and is generated; the rules are code, so the -#: answers of the Python are written down here and apps/host/tests/policy-generated.test.mjs runs the host's rules over them. -_CARDS = { - "rtx4090": {"name": "NVIDIA GeForce RTX 4090", "vendor": "nvidia", "arch": "89", "vram_gb": 24.0}, - "rtx3060": {"name": "NVIDIA GeForce RTX 3060", "vendor": "nvidia", "arch": "86", "vram_gb": 12.0}, - "rtx4060ti16": {"name": "NVIDIA GeForce RTX 4060 Ti 16GB", "vendor": "nvidia", "arch": "89", "vram_gb": 16.0}, - "spark": {"name": "NVIDIA GB10", "vendor": "nvidia", "arch": "121", "vram_gb": 120.0, "uma": True}, - "r9700": {"name": "AMD Radeon AI PRO R9700", "vendor": "amd", "arch": "gfx1201", "vram_gb": 32.0}, - "rx7900": {"name": "AMD Radeon RX 7900 XTX", "vendor": "amd", "arch": "gfx1100", "vram_gb": 24.0}, - "rx9070": {"name": "AMD Radeon RX 9070 XT", "vendor": "amd", "vram_gb": 16.0}, - "strix": {"name": "AMD Radeon 8060S", "vendor": "amd", "arch": "gfx1151", "uma": True}, - "arc": {"name": "Intel(R) Arc(TM) A750", "vendor": "intel", "vram_gb": 8.0}, - "nameonly3060": {"name": "GeForce RTX 3060", "vram_gb": 12.0}, -} -_MODELS = {"qwen": {}, "qwen36": {"arch": "qwen35moe"}, - "qwen_pictures": {"vision": {"exe": "strata-vision"}}, "qwen36_pictures": {"arch": "qwen35moe", "vision": {"exe": "strata-vision"}}} -_NAMES = ["qwen3.8-flash-next", "qwen3.8-flash-next-iq3_s", "qwen3.8-flash-next-Q2_0", "qwen3.8-flash-next-unsloth-ud-q4_k_xl", "qwen3.8-flash-next-unsloth-ud-iq4_xs-v31", - "qwen3.8-flash-next-gyro-iq4", "qwen3.8-flash-next-coder-iq1_m", "qwen3.8-flash-next-swift-iq3_s", "swift-1.5-iq3_s", "qwen3.6-35b-a3b-ud-q4_k_xl", ""] - - -def vectors() -> dict: - switches = [] - for model, cfg in _MODELS.items(): - for backend in ("cuda", "hip", "sycl", "metal", "cpu", None): - for card, g in _CARDS.items(): - row = {"model": model, "backend": backend, "card": card, - "shows": sorted(s.id for s in speed_switches.applicable(cfg, [g], backend) if not s.terminal_only)} - switches.append(row) - names = [{"name": n, "supported": bool(drafting.supported_model(n, None))} for n in _NAMES] - families = [{"family": f, "supported": bool(drafting.supported_model(None, f))} for f in ("qwen", "gyro", "unsloth", "swift", "coder", "nvfp4")] - return {"cards": _CARDS, "models": _MODELS, "switches": switches, "names": names, "families": families} - - -def render_vectors() -> str: - return json.dumps(vectors(), indent=1, ensure_ascii=False) + "\n" - - -def main(argv: list[str] | None = None) -> int: - argv = sys.argv[1:] if argv is None else argv - files = ((TARGET, render()), (VECTORS, render_vectors())) - if "--check" in argv: - bad = [str(p) for p, text in files if (p.read_text(encoding="utf-8") if p.is_file() else "") != text] - for p in bad: - print(f"{p} is out of date: run python -m strata_control.app_policy", file=sys.stderr) - return 1 if bad else 0 - for path, text in files: - path.parent.mkdir(parents=True, exist_ok=True) - path.write_text(text, encoding="utf-8", newline="\n") - print(f"wrote {path}") - return 0 - - -if __name__ == "__main__": - raise SystemExit(main()) +"""The launch-policy tables the app's host (JavaScript) reads, generated from the Python that owns them. + +`strata_control` decides the speed switches (`speed_switches.py`) and which models have a Drafting choice (`drafting.py`). The app's host used to carry its own +copy of those tables. It now imports `apps/host/server/policy-generated.mjs`, which this module writes, and a test (`tests/terminal/test_app_policy.py`, and +`apps/host/tests/policy-generated.test.mjs` on the JavaScript side) fails when the committed file differs from what `render()` makes now. + + python -m strata_control.app_policy # rewrite apps/host/server/policy-generated.mjs + python -m strata_control.app_policy --check # exit 1 when it is out of date +""" + +from __future__ import annotations + +import json +import sys +from pathlib import Path + +from strata_control import drafting, speed_switches + +TARGET = Path(__file__).resolve().parents[1] / "apps" / "host" / "server" / "policy-generated.mjs" +VECTORS = Path(__file__).resolve().parents[1] / "apps" / "host" / "tests" / "data" / "policy-vectors.json" + +HEADER = ("// GENERATED by `python -m strata_control.app_policy` from strata_control/speed_switches.py and strata_control/drafting.py. Do not edit it by hand:\n" + "// change the Python and regenerate; tests/terminal/test_app_policy.py and apps/host/tests/policy-generated.test.mjs fail when this file is out of date.\n") + + +def _entries(pairs) -> dict: + return {name: value for name, value in pairs} + + +def table() -> list[dict]: + out = [] + for s in speed_switches.app_switches(): + row = {"id": s.id, "label": s.label, "hint": s.effect, "default": s.default, "env": _entries(s.env)} + if s.legacy: + row["legacy"] = _entries(s.legacy) + if s.group != "Speed": + row["group"] = s.group + if s.words: + row["words"] = _entries(s.words) + out.append(row) + return out + + +def render() -> str: + rows = ",\n".join(" " + json.dumps(r, ensure_ascii=False, separators=(", ", ": ")) for r in table()) + return (HEADER + + "/** The speed switches of the app's Expert settings (strata_control/speed_switches.py app_switches()): id, words, default, the environment entries outside the default. */\n" + + f"export const SPEED_SWITCH_TABLE = [\n{rows},\n];\n" + + "/** The families that have a Drafting choice (strata_control/drafting.py SUPPORTED_FAMILIES). */\n" + + f"export const DRAFTING_FAMILIES = {json.dumps(list(drafting.SUPPORTED_FAMILIES))};\n" + + "/** The model names the DFlash2 drafter was made for (strata_control/drafting.py _SUPPORTED_NAME), a case-insensitive pattern. */\n" + + f"export const DFLASH2_MODEL_PATTERN = {json.dumps(drafting._SUPPORTED_NAME.pattern)};\n" + + "/** #5240: the families and model names DFlash2 runs on only when the person chooses it by name (strata_control/drafting.py NAMED_ONLY_FAMILIES, _NAMED_ONLY_NAME). */\n" + + f"export const DFLASH2_NAMED_FAMILIES = {json.dumps(list(drafting.NAMED_ONLY_FAMILIES))};\n" + + f"export const DFLASH2_NAMED_PATTERN = {json.dumps(drafting._NAMED_ONLY_NAME.pattern)};\n") + + +#: Cases the host's copies of the RULES (speedSwitchApplies, resolveDrafter) must answer as Python does. The table above is data and is generated; the rules are code, so the +#: answers of the Python are written down here and apps/host/tests/policy-generated.test.mjs runs the host's rules over them. +_CARDS = { + "rtx4090": {"name": "NVIDIA GeForce RTX 4090", "vendor": "nvidia", "arch": "89", "vram_gb": 24.0}, + "rtx3060": {"name": "NVIDIA GeForce RTX 3060", "vendor": "nvidia", "arch": "86", "vram_gb": 12.0}, + "rtx4060ti16": {"name": "NVIDIA GeForce RTX 4060 Ti 16GB", "vendor": "nvidia", "arch": "89", "vram_gb": 16.0}, + "spark": {"name": "NVIDIA GB10", "vendor": "nvidia", "arch": "121", "vram_gb": 120.0, "uma": True}, + "r9700": {"name": "AMD Radeon AI PRO R9700", "vendor": "amd", "arch": "gfx1201", "vram_gb": 32.0}, + "rx7900": {"name": "AMD Radeon RX 7900 XTX", "vendor": "amd", "arch": "gfx1100", "vram_gb": 24.0}, + "rx9070": {"name": "AMD Radeon RX 9070 XT", "vendor": "amd", "vram_gb": 16.0}, + "strix": {"name": "AMD Radeon 8060S", "vendor": "amd", "arch": "gfx1151", "uma": True}, + "arc": {"name": "Intel(R) Arc(TM) A750", "vendor": "intel", "vram_gb": 8.0}, + "nameonly3060": {"name": "GeForce RTX 3060", "vram_gb": 12.0}, +} +_MODELS = {"qwen": {}, "qwen36": {"arch": "qwen35moe"}, + "qwen_pictures": {"vision": {"exe": "strata-vision"}}, "qwen36_pictures": {"arch": "qwen35moe", "vision": {"exe": "strata-vision"}}} +_NAMES = ["qwen3.8-flash-next", "qwen3.8-flash-next-iq3_s", "qwen3.8-flash-next-Q2_0", "qwen3.8-flash-next-unsloth-ud-q4_k_xl", "qwen3.8-flash-next-unsloth-ud-iq4_xs-v31", + "qwen3.8-flash-next-gyro-iq4", "qwen3.8-flash-next-coder-iq1_m", "qwen3.8-flash-next-swift-iq3_s", "swift-1.5-iq3_s", "qwen3.6-35b-a3b-ud-q4_k_xl", ""] + + +def vectors() -> dict: + switches = [] + for model, cfg in _MODELS.items(): + for backend in ("cuda", "hip", "sycl", "metal", "cpu", None): + for card, g in _CARDS.items(): + row = {"model": model, "backend": backend, "card": card, + "shows": sorted(s.id for s in speed_switches.applicable(cfg, [g], backend) if not s.terminal_only)} + switches.append(row) + names = [{"name": n, "supported": bool(drafting.supported_model(n, None)), "named": bool(drafting.supported_model(n, None, named=True))} for n in _NAMES] + families = [{"family": f, "supported": bool(drafting.supported_model(None, f)), "named": bool(drafting.supported_model(None, f, named=True))} + for f in ("qwen", "gyro", "unsloth", "swift", "coder", "nvfp4")] + return {"cards": _CARDS, "models": _MODELS, "switches": switches, "names": names, "families": families} + + +def render_vectors() -> str: + return json.dumps(vectors(), indent=1, ensure_ascii=False) + "\n" + + +def main(argv: list[str] | None = None) -> int: + argv = sys.argv[1:] if argv is None else argv + files = ((TARGET, render()), (VECTORS, render_vectors())) + if "--check" in argv: + bad = [str(p) for p, text in files if (p.read_text(encoding="utf-8") if p.is_file() else "") != text] + for p in bad: + print(f"{p} is out of date: run python -m strata_control.app_policy", file=sys.stderr) + return 1 if bad else 0 + for path, text in files: + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(text, encoding="utf-8", newline="\n") + print(f"wrote {path}") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/strata_control/drafting.py b/strata_control/drafting.py index 84815c8033..25758786ba 100644 --- a/strata_control/drafting.py +++ b/strata_control/drafting.py @@ -471,11 +471,18 @@ SUPPORTED_FAMILIES = ("qwen", "gyro", "unsloth") _SUPPORTED_NAME = re.compile(r"^qwen3\.8-flash-next(-gyro-[a-z0-9._-]+|-unsloth-[a-z0-9._-]+|-(q|iq|tq)\d[a-z0-9_.]*)?$", re.IGNORECASE) -def supported_model(model_name: str | None = None, family: str | None = None) -> bool: - """DFlash2 is for Qwen3.8-Flash-Next only: by family when it is known (the Terminal), else by the run config's model name.""" +#: #5240: models DFlash2 runs on only when the person chooses it by name (Auto keeps the MTP): the Coder (ISTA-DASLab's coding version of the same weights, +#: 256 of the 512 experts). Reporter's numbers (RTX 4090, stock pack): the same 45/45 agent tasks, 414 against 274 tasks an hour, decode +21-37%. +NAMED_ONLY_FAMILIES = ("coder",) +_NAMED_ONLY_NAME = re.compile(r"^qwen3\.8-flash-next-coder-[a-z0-9._-]+$", re.IGNORECASE) + + +def supported_model(model_name: str | None = None, family: str | None = None, *, named: bool = False) -> bool: + """DFlash2 is for Qwen3.8-Flash-Next only: by family when it is known (the Terminal), else by the run config's model name. ``named``: DFlash2 is the + person's own choice, which also takes the Coder (NAMED_ONLY_FAMILIES, #5240).""" if family is not None: - return family.lower() in SUPPORTED_FAMILIES - return bool(model_name and _SUPPORTED_NAME.match(str(model_name))) + return family.lower() in SUPPORTED_FAMILIES or (named and family.lower() in NAMED_ONLY_FAMILIES) + return bool(model_name and (_SUPPORTED_NAME.match(str(model_name)) or (named and _NAMED_ONLY_NAME.match(str(model_name))))) # --------------------------------------------------------------------------------------------------- the model file itself @@ -1372,9 +1379,10 @@ def vocab_path(args: list[Any], data_dir: str | Path | None = None) -> Path | No return shipped if shipped.is_file() else None -def can_run_dflash2(model_name: str | None, family: str | None, backend: str | None, archs: Any = None) -> bool: - """Is this model one the drafter was made for, and does this computer's engine have the DFlash2 code? (The disk and the mode of the start are not looked at.)""" - return bool(supported_model(model_name, family) and ENGINE_SERVES_DFLASH2 and backend is not None and hardware_support(backend, archs).can_run) +def can_run_dflash2(model_name: str | None, family: str | None, backend: str | None, archs: Any = None, *, named: bool = False) -> bool: + """Is this model one the drafter was made for (``named``: or one it runs on when chosen by name, the Coder), and does this computer's engine have the DFlash2 code? + (The disk and the mode of the start are not looked at.)""" + return bool(supported_model(model_name, family, named=named) and ENGINE_SERVES_DFLASH2 and backend is not None and hardware_support(backend, archs).can_run) def choice_of(mode: Any, *, model_name: str | None = None, family: str | None = None, backend: str | None = None, archs: Any = None, @@ -1395,7 +1403,7 @@ def choice_of(mode: Any, *, model_name: str | None = None, family: str | None = klass = class_entry(backend, model_name, archs, args=args, vram_mib=vram_mib) if asked == "auto" and klass is not None and klass.drafter == "mtp": return "mtp" # this set on this class of card (CLASS_DEFAULTS) - return "dflash2" if can_run_dflash2(model_name, family, backend, archs) else "mtp" + return "dflash2" if can_run_dflash2(model_name, family, backend, archs, named=asked == "dflash2") else "mtp" def decide(mode: Any, *, klass: str = "other", model_name: str | None = None, family: str | None = None, data_dir: str | Path | None = None, @@ -1415,7 +1423,7 @@ def decide(mode: Any, *, klass: str = "other", model_name: str | None = None, fa # start never goes without a drafter while one is on disk; not where Auto keeps the MTP because DFlash2 stalls there (AUTO_MTP, gfx1201) # (and never as the stand-in for a chosen MTP either: owner 11-15, a chosen drafter stays chosen and its fallback must be safe on the card; DFlash2 named by # itself is the person's own choice and is not looked at here) - runnable = can_run_dflash2(model_name, family, backend, archs) and not (asked != "dflash2" and auto_mtp_only(backend, archs)) + runnable = can_run_dflash2(model_name, family, backend, archs, named=asked == "dflash2") and not (asked != "dflash2" and auto_mtp_only(backend, archs)) if want == "dflash2": ready = _here(Path(pack)) if pack is not None else drafter_ready(data_dir, model_name, backend) @@ -1429,7 +1437,7 @@ def decide(mode: Any, *, klass: str = "other", model_name: str | None = None, fa if have_mtp: why = "" if asked == "dflash2": # named, and it cannot run on this model or this computer - why = NOT_SUPPORTED if not supported_model(model_name, family) else NOT_SERVED if not ENGINE_SERVES_DFLASH2 else NOT_THIS_CARD + why = NOT_SUPPORTED if not supported_model(model_name, family, named=True) else NOT_SERVED if not ENGINE_SERVES_DFLASH2 else NOT_THIS_CARD return Decision(asked, "mtp", why) # the layer is not on this computer: DFlash2 instead when it can run and its pack is here (said), else none (said) if runnable and not blocked and vocab_here and (_here(Path(pack)) if pack is not None else drafter_ready(data_dir, model_name, backend)): @@ -1561,7 +1569,7 @@ def apply(cfg: dict[str, Any], *, klass: str = "other", data_dir: str | Path | N fit = file_fit(args, data_dir) if family is None and not _EXCLUDED_NAME.search(str(name or "")) else "" if fit == "fits": name = known_name_of_file(args) or FILE_FITS_AS # a file that is exactly a known set gets that set's table entry; any other the general defaults - if not supported_model(name, family): # another model: its drafting is not this choice's business (Swift, Coder, NVFP4, Qwen3.6...) + if not supported_model(name, family, named=normalize(cfg.get("drafting")) == "dflash2"): # another model: its drafting is not this choice's business (Swift, NVFP4, Qwen3.6, the Coder under Auto...) asked = normalize(cfg.get("drafting")) if asked == "off" or cfg.get("mtp") is False: # no drafter for this model (its draft layer is off, or a feed model whose entry says drafter: none): say so, start none # "mtp": false: the server's own draft-layer step takes the layer out (and says so); "drafting": "off" alone: taken out here diff --git a/tests/terminal/test_drafting.py b/tests/terminal/test_drafting.py index c7565da43e..248f40b1e6 100644 --- a/tests/terminal/test_drafting.py +++ b/tests/terminal/test_drafting.py @@ -63,7 +63,8 @@ class AutoAndTheBackends(unittest.TestCase): self.assertEqual(drafting.choice_of("dflash2", family="qwen", backend="cuda"), "dflash2") self.assertEqual(drafting.choice_of("dflash2", family="qwen", backend="sycl"), "dflash2") self.assertEqual(drafting.choice_of("dflash2", family="qwen", backend="cpu"), "mtp", "named, but the engine has none") - self.assertEqual(drafting.choice_of("dflash2", family="coder", backend="cuda"), "mtp") + self.assertEqual(drafting.choice_of("dflash2", family="coder", backend="cuda"), "dflash2", "#5240: the Coder runs DFlash2 when it is chosen by name") + self.assertEqual(drafting.choice_of("dflash2", family="swift", backend="cuda"), "mtp") def test_the_two_drafters_have_their_own_memory_figure(self): self.assertEqual((drafting.DRAFTER_GB["dflash2"], drafting.DRAFTER_GB["mtp"], drafting.DRAFTER_GB["off"]), (0.89, 1.08, 0.0)) @@ -154,7 +155,7 @@ class Decisions(unittest.TestCase): def test_dflash2_is_for_qwen38_flash_next_only(self): self.ready() - for name in ("qwen3.6-35b-a3b-q4", "qwen3.8-flash-next-coder-iq3_xxs", "qwen3.8-flash-next-swift-iq2_xs", None): + for name in ("qwen3.6-35b-a3b-q4", "qwen3.8-flash-next-swift-iq2_xs", None): d = drafting.decide("dflash2", model_name=name, data_dir=self.data, backend="cuda") self.assertEqual((d.drafter, d.note), ("mtp", drafting.NOT_SUPPORTED), name) self.assertFalse(drafting.needs_pack("dflash2", model_name=name, data_dir=self.data, backend="cuda")) @@ -163,6 +164,9 @@ class Decisions(unittest.TestCase): self.assertEqual(drafting.decide("dflash2", model_name=name, data_dir=self.data, backend="cuda").drafter, "dflash2", name) self.assertTrue(drafting.supported_model(family="qwen") and drafting.supported_model(family="gyro")) self.assertFalse(drafting.supported_model(family="coder") or drafting.supported_model(family="swift") or drafting.supported_model(family="nvfp4")) + coder = "qwen3.8-flash-next-coder-iq3_xxs" # #5240: the Coder runs DFlash2 when it is chosen by name; Auto keeps the MTP + self.assertEqual(drafting.decide("dflash2", model_name=coder, data_dir=self.data, backend="cuda").drafter, "dflash2") + self.assertEqual(drafting.decide("auto", model_name=coder, data_dir=self.data, backend="cuda").drafter, "mtp") def test_the_engine_of_this_branch_serves_dflash2(self): self.assertTrue(drafting.ENGINE_SERVES_DFLASH2, "src/program/generate.cpp: --draft dflash2 runs with --serve (one card, one request at a time)") @@ -339,7 +343,7 @@ class EngineArguments(unittest.TestCase): c = self.cfg(model_name="qwen3.8-flash-next-coder-iq3_xxs") d = self.apply(c) self.assertEqual((d.drafter, d.note, c["args"]), ("mtp", "", self.args)) - c = self.cfg(model_name="qwen3.8-flash-next-coder-iq3_xxs", drafting="dflash2", args=[*self.args, "--draft", "dflash2", "--draft-model", "/p"]) + c = self.cfg(model_name="qwen3.8-flash-next-swift-iq2_xs", drafting="dflash2", args=[*self.args, "--draft", "dflash2", "--draft-model", "/p"]) # (the Coder named: test_drafting_coder_named.py) d = self.apply(c) self.assertEqual((d.drafter, d.note, c["args"]), ("mtp", drafting.NOT_SUPPORTED, self.args)) @@ -500,6 +504,8 @@ class UnslothAndTheOtherFlashNextFiles(unittest.TestCase): for backend, archs in self.HARDWARE: for name in ("qwen3.8-flash-next-coder-iq1_m", "qwen3.8-flash-next-nvfp4-nvfp4", "swift-1.5-iq2_xs"): for mode in (None, "auto", "dflash2"): + if mode == "dflash2" and "-coder-" in name: + continue # #5240: the Coder named DFlash2 runs it (test_drafting_coder_named.py) with tempfile.TemporaryDirectory() as tmp: cfg, data, mtp = self.run_config(name, tmp, mode) original = list(cfg["args"]) @@ -996,14 +1002,14 @@ class TheAppAsksControl(unittest.TestCase): self.assertTrue(plan["needed"]) self.assertEqual(plan["gb"], 1.2) self.assertEqual(Path(plan["pack"]), drafting.pack_path(self.data), "the data folder comes from the run config's own arguments") - self.cfg.write_text(json.dumps({"model_name": "qwen3.8-flash-next-coder-iq3_xxs", "drafting": "dflash2", "args": ["--pack", str(self.data / "packs" / "c")]})) + self.cfg.write_text(json.dumps({"model_name": "qwen3.8-flash-next-swift-iq2_xs", "drafting": "dflash2", "args": ["--pack", str(self.data / "packs" / "c")]})) other = self.plan() self.assertFalse(other["needed"]) self.assertEqual(other["note"], drafting.NOT_SUPPORTED) def test_a_model_that_is_not_installed_yet_is_asked_about_by_its_family(self): with serving_dflash2(): - for family, needed in (("gyro", True), ("qwen", True), ("coder", False), ("swift", False)): + for family, needed in (("gyro", True), ("qwen", True), ("coder", True), ("swift", False)): # (#5240: the Coder named) code, out, err = run_cli(["drafting", "plan", "--family", family, "--mode", "dflash2", "--data-dir", str(self.data), "--output", "json"]) self.assertEqual(code, 0, err) self.assertEqual(json.loads(out)["needed"], needed, family) @@ -1044,8 +1050,8 @@ class TheDownloadPlanListsIt(unittest.TestCase): self.assertNotIn("dflash2", kinds) self.assertIn("mtp", kinds, "the MTP is chosen: its layer is the download, DFlash2's is not") self.assertNotIn("mtp", [p["kind"] for p in auto.download_plan()["parts"]], "one drafter per run: a new install fetches only the one it will use") - other = Runner(S.LaunchSettings(family="coder", model="IQ3_XXS", context=32768, vision="off", drafting="dflash2"), catalog=catalog(HW_12)) - self.assertNotIn("dflash2", [p["kind"] for p in other.download_plan()["parts"]], "not for this model: hidden, not offered") + other = Runner(S.LaunchSettings(family="coder", model="IQ1_M", context=32768, vision="off"), catalog=catalog(HW_12)) + self.assertNotIn("dflash2", [p["kind"] for p in other.download_plan()["parts"]], "Auto on the Coder is the MTP: not offered") if __name__ == "__main__": diff --git a/tests/terminal/test_drafting_coder_named.py b/tests/terminal/test_drafting_coder_named.py new file mode 100644 index 0000000000..a7593eee40 --- /dev/null +++ b/tests/terminal/test_drafting_coder_named.py @@ -0,0 +1,66 @@ +"""#5240: DFlash2 on the Coder is an explicit opt-in. A DFlash2 chosen by name on the Coder starts DFlash2 with the stock pack (the Coder has no tuned pack) and no MTP +layer; Auto (and no choice) keeps the MTP on the Coder; Swift and NVFP4 stay refused (the MTP, with the NOT_SUPPORTED line); setup fetches the stock pack only for the +named choice. The app's copies (resolveDrafter) are checked against the same rule by apps/host/tests/policy-generated.test.mjs.""" + +from __future__ import annotations + +import tempfile +import unittest + +from strata_control import drafting +from tests.terminal import test_drafting as TD + +CODER = "qwen3.8-flash-next-coder-iq1_m" + + +class CoderNamed(unittest.TestCase): + run_config = TD.UnslothAndTheOtherFlashNextFiles.run_config + + def test_named_dflash2_runs_on_the_coder_with_the_stock_pack(self): + for backend, archs in TD.UnslothAndTheOtherFlashNextFiles.HARDWARE: + with self.subTest(backend=backend), tempfile.TemporaryDirectory() as tmp: + cfg, data, _mtp = self.run_config(CODER, tmp, "dflash2") + d = drafting.apply(cfg, data_dir=data, backend=backend, archs=archs) + args = cfg["args"] + self.assertEqual((d.drafter, d.note), ("dflash2", "")) + self.assertEqual(args[args.index("--draft-model") + 1], str(drafting.pack_path(data))) + self.assertNotIn("--mtp", args) + + def test_auto_and_no_choice_keep_the_mtp_on_the_coder(self): + for mode in (None, "auto"): + with self.subTest(mode=mode), tempfile.TemporaryDirectory() as tmp: + cfg, data, _mtp = self.run_config(CODER, tmp, mode) + original = list(cfg["args"]) + d = drafting.apply(cfg, data_dir=data, backend="cuda", archs=["sm_89"]) + self.assertEqual((d.drafter, cfg["args"]), ("mtp", original)) + self.assertEqual(drafting.choice_of("auto", family="coder", backend="cuda"), "mtp") + self.assertEqual(drafting.choice_of("auto", model_name=CODER, backend="cuda"), "mtp") + + def test_swift_and_nvfp4_stay_refused(self): + for name in ("qwen3.8-flash-next-nvfp4-nvfp4", "swift-1.5-iq2_xs", "qwen3.8-flash-next-swift-iq2_xs"): + with self.subTest(name=name), tempfile.TemporaryDirectory() as tmp: + cfg, data, _mtp = self.run_config(name, tmp, "dflash2") + d = drafting.apply(cfg, data_dir=data, backend="cuda", archs=["sm_89"]) + self.assertEqual((d.drafter, d.note), ("mtp", drafting.NOT_SUPPORTED)) + for family in ("swift", "nvfp4", "qwen36"): + self.assertEqual(drafting.choice_of("dflash2", family=family, backend="cuda"), "mtp", family) + + def test_the_named_rule(self): + self.assertFalse(drafting.supported_model(CODER)) + self.assertTrue(drafting.supported_model(CODER, named=True)) + self.assertTrue(drafting.supported_model(None, "coder", named=True)) + self.assertFalse(drafting.supported_model(None, "swift", named=True)) + self.assertFalse(drafting.supported_model("qwen3.8-flash-next-coder", named=True), "the family name alone is not a set setup writes") + self.assertEqual(drafting.NAMED_ONLY_FAMILIES, ("coder",)) + + def test_setup_is_told_to_fetch_the_stock_pack_only_for_the_named_choice(self): + with tempfile.TemporaryDirectory() as tmp: + kw = dict(model_name=CODER, family="coder", data_dir=tmp, backend="cuda", archs=["sm_89"], args=[], vram_mib=24576) + self.assertTrue(drafting.needs_pack("dflash2", **kw)) + self.assertFalse(drafting.needs_pack("auto", **kw)) + self.assertFalse(drafting.needs_tuned("dflash2", **kw), "no tuned pack for the Coder") + self.assertTrue(drafting.wants_one_request("dflash2", **kw)) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/terminal/test_drafting_hardware.py b/tests/terminal/test_drafting_hardware.py index 58e5d3c1a4..80e2a32063 100644 --- a/tests/terminal/test_drafting_hardware.py +++ b/tests/terminal/test_drafting_hardware.py @@ -278,7 +278,9 @@ class OneRequestAtATime(unittest.TestCase): self.assertTrue(want(mode, model_name=GYRO, backend="cuda", archs=["sm_121"]), mode) for mode in ("mtp", "off"): self.assertFalse(want(mode, model_name=GYRO, backend="cuda", archs=["sm_121"]), mode) - self.assertFalse(want("dflash2", model_name="qwen3.8-flash-next-coder-iq1_m", backend="cuda", archs=["sm_121"])) + self.assertTrue(want("dflash2", model_name="qwen3.8-flash-next-coder-iq1_m", backend="cuda", archs=["sm_121"]), "#5240: the Coder named") + self.assertFalse(want("auto", model_name="qwen3.8-flash-next-coder-iq1_m", backend="cuda", archs=["sm_121"])) + self.assertFalse(want("dflash2", model_name="qwen3.8-flash-next-swift-iq2_xs", backend="cuda", archs=["sm_121"])) self.assertTrue(want("auto", model_name=GYRO, backend="sycl", archs=[]), "Intel runs DFlash2 one request at a time") self.assertFalse(want("auto", model_name=GYRO, backend="cpu", archs=[])) self.assertTrue(want("auto", model_name=GYRO, backend="metal", archs=[])) diff --git a/tests/terminal/test_drafting_single.py b/tests/terminal/test_drafting_single.py index e03420ff3f..af02cb418b 100644 --- a/tests/terminal/test_drafting_single.py +++ b/tests/terminal/test_drafting_single.py @@ -107,6 +107,8 @@ class EveryChoiceOnEveryBackend(Base): def test_other_families_never_get_dflash2_whatever_is_saved(self): for family in ("coder", "nvfp4", "swift"): for mode in (None, "auto", "mtp", "dflash2"): + if family == "coder" and mode == "dflash2": + continue # #5240: the Coder named runs DFlash2 (test_drafting_coder_named.py) cfg = self.cfg(family, **({} if mode is None else {"drafting": mode})) d, args = self.apply(cfg, family, "cuda", ("sm_120",)) self.assertNotIn("--draft", args, (family, mode))