From 74e1f86442421466909ff440c2a0ca924c0fcc4e Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 29 Oct 2026 15:48:38 +0100 Subject: [PATCH] Setup and the launcher write --spec 2 for exactly two Arc B-series cards On two Battlemage cards the engine pipelines the two cards' verify windows by default, and then answers as a 2-token window whatever --spec says (pipelined --spec 4 equals serial --spec 2 in 100 of 100 agent requests; serial --spec 4 differs in 3). So the generated config says --spec 2 there: the same answers, and about 20% faster than the old --spec 4. strata_control/intel_spec.py holds the rule: the cards are found by PCI device id (the xe driver and an id in 0xE200-0xE2FF, the id the engine's architecture enum comes from), never by name. It changes only setup's own default "--spec 4", only with the draft layer on, and only for exactly two such cards. A --spec in "user_args", any other value, one card, an A-series card in the pair, three cards and "mtp": false are left alone. Applied in three places: sycl/setup_intel.py when it writes the run config of a layer split (the copy in "args_written" follows, so a rerun does not take it for a hand edit), the launcher's derive_config (before the person's --set options, which win), and the Intel server's engine_args at every start (a config an older setup wrote). Tests in tests/sycl/test_intel_two_card_spec.py. --- strata_cli/launch/runner.py | 3 + strata_control/intel_spec.py | 127 +++++++++++++++++ sycl/serve/server_intel.py | 24 ++++ sycl/setup_intel.py | 8 +- tests/sycl/test_intel_two_card_spec.py | 182 +++++++++++++++++++++++++ 5 files changed, 343 insertions(+), 1 deletion(-) create mode 100644 strata_control/intel_spec.py create mode 100644 tests/sycl/test_intel_two_card_spec.py diff --git a/strata_cli/launch/runner.py b/strata_cli/launch/runner.py index cd323caa..5ef69318 100644 --- a/strata_cli/launch/runner.py +++ b/strata_cli/launch/runner.py @@ -534,6 +534,9 @@ def derive_config(base: dict, s: S.LaunchSettings, *, bool_flags: frozenset[str] draft_layer.apply(cfg) elif s.mtp == "on": cfg["mtp"] = True + if cfg.get("backend") == "sycl": # exactly two Arc B-series cards: --spec 2 (strata_control/intel_spec.py); the person's + from strata_control import intel_spec # own choices (user_args, an extra option below) come after it and win + intel_spec.apply_for_launch(cfg) if s.extra: a_changes, e_changes = S.as_argv_changes(s.extra, bool_flags) cfg["args"] = tune_knobs.apply_to_argv([str(x) for x in cfg.get("args") or []], a_changes) diff --git a/strata_control/intel_spec.py b/strata_control/intel_spec.py new file mode 100644 index 00000000..701a81aa --- /dev/null +++ b/strata_control/intel_spec.py @@ -0,0 +1,127 @@ +"""--spec 2 for a run config on exactly two Intel Arc B-series (Battlemage) cards. + +On two B-series cards the SYCL engine runs the verify windows of the two cards overlapped (--pipeline-windows 2, on by +default there). Its answers are those of a width-2 draft window whatever --spec says: on 2x Arc Pro B65 the pipelined +answers equal a serial --spec 2 in 100 of 100 agent requests, while a serial --spec 4 differs from them in 3 of 100 (an end +token against a digit at a near-tie; a serial --spec 3 differs from --spec 4 in the same way). So setup and the launcher +write `--spec 2` for such a config: the same answers as the engine gives anyway, about 20% faster than the old +`--spec 4` serial default (chat 73.9 against 60.7 tok/s), and no surprise between a serial and a pipelined start. + +Only the generated default changes. Single cards, Arc A-series (Alchemist) cards, three or more cards, a config with no +draft layer, and any config where the person set --spec (in "user_args", or a value other than setup's own 4 in "args") keep +what they have. The card generation is the PCI device id from sysfs, the same id the engine's architecture enum comes from +(strata::intel_gpu_gen in sycl/include/strata/sycl_queue.hpp), never the marketing name. +""" + +from __future__ import annotations + +import os +from pathlib import Path +from typing import Any + +from . import user_args + +SETUP_DEFAULT_SPEC = "4" # what setup.py and the launcher write for every model +TWO_CARD_SPEC = "2" +# Battlemage: BMG-G21 (0xE20x: B580, B570, Pro B50) and BMG-G31 (0xE22x: Pro B70, B65, B60), driven by xe. Alchemist (0x56A0..) +# and anything else (an integrated GPU, i915) is not. Same range as sycl_queue.hpp's name-less fallback. +BATTLEMAGE_ID_RANGE = (0xE200, 0xE2FF) + + +def is_battlemage(driver: str, devid: str) -> bool: + """True for an Arc B-series card: the xe driver and a PCI device id in the Battlemage range.""" + if driver != "xe": + return False + try: + n = int(str(devid).lower().replace("0x", ""), 16) + except ValueError: + return False + return BATTLEMAGE_ID_RANGE[0] <= n <= BATTLEMAGE_ID_RANGE[1] + + +def sysfs_cards(drm: str | os.PathLike = "/sys/class/drm") -> list[dict]: + """The Intel discrete cards in sysfs card order (the order setup's `--gpus` indexes and Level Zero follows on the tested + machines): [{"driver": "xe", "devid": "e222"}, ...]. Vendor 0x8086 under xe or i915; an i915 card with an id that is not an + Alchemist one is an integrated GPU and is left out, as in sycl/setup_intel.py.""" + out = [] + for card in sorted(Path(drm).glob("card[0-9]*")): + if "-" in card.name: + continue + dev = card / "device" + try: + vendor = (dev / "vendor").read_text().strip().lower() + devid = (dev / "device").read_text().strip().lower().replace("0x", "") + driver = os.path.basename(os.path.realpath(dev / "driver")) if (dev / "driver").exists() else "" + except OSError: + continue + if vendor != "0x8086" or driver not in ("xe", "i915"): + continue + if driver == "i915" and not devid.startswith("56"): + continue + out.append({"index": len(out), "driver": driver, "devid": devid}) + return out + + +def exactly_two_battlemage(cards: list[dict]) -> bool: + return len(cards) == 2 and all(is_battlemage(c.get("driver", ""), c.get("devid", "")) for c in cards) + + +def _spec_group(args: list[str]) -> int: + """Index of the value after --spec in `args`, or -1.""" + for i, a in enumerate(args): + if a == "--spec" and i + 1 < len(args): + return i + 1 + return -1 + + +def plan(args: Any, user: Any, cards: list[dict], mtp_off: bool = False) -> tuple[list[str], str]: + """(new args, note): `args` with --spec 4 turned into --spec 2 when the run is on exactly two Battlemage cards, and "" + when nothing changed. `user` is the config's "user_args"; `cards` the cards the run uses (exactly two to qualify); + `mtp_off` is the config's "mtp": false. `args` is not changed in place.""" + out = [str(a) for a in args] if isinstance(args, list) else [] + if not exactly_two_battlemage(cards) or mtp_off: + return out, "" + if "--mtp" not in out: # the pipeline needs the draft layer + return out, "" + if any(user_args.flag_of(g) == "--spec" for g in user_args.groups(user)): + return out, "" # the person's own --spec wins + i = _spec_group(out) + if i < 0 or out[i] != SETUP_DEFAULT_SPEC: # absent, or a value somebody chose + return out, "" + out[i] = TWO_CARD_SPEC + return out, ("Two Arc B-series cards: --spec 2 instead of 4. With both cards working on neighbouring windows the engine " + "answers as a 2-token window anyway, and --spec 2 is about 20% faster here. To keep your own value, put " + "\"--spec\" and its number in the config's \"user_args\".") + + +def apply_for_launch(cfg: dict, sysfs: list[dict] | None = None) -> str: + """The launcher's run config: the cards in cfg["gpu"] (a list of two numbers after a layer split) looked up in sysfs, then + `apply_to_config`. Nothing happens for a single card, another backend or a card list that sysfs cannot place.""" + if cfg.get("backend") != "sycl": + return "" + gpu = cfg.get("gpu") + if not isinstance(gpu, (list, tuple)) or len(gpu) != 2: + return "" + try: + have = sysfs if sysfs is not None else sysfs_cards() + cards = [have[int(i)] for i in gpu if 0 <= int(i) < len(have)] + except (TypeError, ValueError): + return "" + return apply_to_config(cfg, cards) if len(cards) == 2 else "" + + +def apply_to_config(cfg: dict, cards: list[dict]) -> str: + """`plan` on a run config (in place): changes cfg["args"], and the copy in cfg["args_written"] so a later setup run does not + take the change for the person's own edit. Returns the note, "" when nothing changed.""" + args, note = plan(cfg.get("args"), cfg.get("user_args"), cards, mtp_off=cfg.get("mtp") is False) + if not note: + return "" + cfg["args"] = args + written = cfg.get(user_args.WRITTEN_KEY) + if isinstance(written, list): + w = [str(a) for a in written] + i = _spec_group(w) + if i >= 0 and w[i] == SETUP_DEFAULT_SPEC: + w[i] = TWO_CARD_SPEC + cfg[user_args.WRITTEN_KEY] = w + return note diff --git a/sycl/serve/server_intel.py b/sycl/serve/server_intel.py index 8310bc76..66bbe1a6 100644 --- a/sycl/serve/server_intel.py +++ b/sycl/serve/server_intel.py @@ -324,7 +324,31 @@ def top_logprobs(req: dict): return None +def install_two_card_spec(): + """engine_args -> the config's --spec 2 on exactly two Arc B-series cards (strata_control/intel_spec.py), at every start: a + config written before (by an older setup, or by the launcher) gets what a new setup run would write. The person's own + --spec ("user_args", or a value other than setup's 4) is never touched; one plain line says what changed.""" + from strata_control import intel_spec + original = S.engine_args + + def engine_args(cfg): + args = original(cfg) + try: + if cfg.get("backend") == "sycl" and S.layer_split_of(cfg): + sysfs = intel_spec.sysfs_cards() + cards = [sysfs[i] for i in S.gpu_list(cfg) if isinstance(i, int) and 0 <= i < len(sysfs)] + new, note = intel_spec.plan(args, cfg.get("user_args"), cards, mtp_off=cfg.get("mtp") is False) + if note: + print("[strata] " + note, flush=True) + return new + except Exception as exc: # noqa: BLE001 - a rule that cannot be read leaves the config's own args + print(f"[strata] two-card --spec rule skipped: {exc}", flush=True) + return args + S.engine_args = engine_args + + if __name__ == "__main__": + install_two_card_spec() install_xe_reader() install_shared_embeddings() install_narrator() diff --git a/sycl/setup_intel.py b/sycl/setup_intel.py index 617ea277..2a0bf012 100644 --- a/sycl/setup_intel.py +++ b/sycl/setup_intel.py @@ -35,7 +35,7 @@ from pathlib import Path ROOT = Path(__file__).resolve().parent.parent sys.path.insert(0, str(ROOT)) import setup as S # noqa: E402 -from strata_control import vram_fit # noqa: E402 +from strata_control import intel_spec, vram_fit # noqa: E402 SYCL_WRAPPER = ROOT / "sycl" / "serve" / "strata-sycl.sh" SYCL_IMAGE = "strata-sycl-dev" @@ -510,6 +510,12 @@ def install(argv) -> None: f"{sum(cards):.0f} GB of VRAM: the config sets STRATA_VERIFY_NO_HOST=0, so the CPU computes the " "experts the later card does not hold instead of the start being refused (#1980). Set it to 1 " "in the config's env if your cards hold everything; a smaller model fits fully (docs/INTEL_ARC.md)") + # exactly two Battlemage cards: --spec 2 (the engine pipelines the two cards' windows by default there and then answers + # as a 2-token window whatever --spec says; strata_control/intel_spec.py). The person's own --spec always wins. + cards_used = [intel[i] for i in gsel if isinstance(i, int) and 0 <= i < len(intel)] + spec_note = intel_spec.apply_to_config(cfg, cards_used) + if spec_note: + S.say(" " + spec_note) need = S.MODELS.get(model, {}).get("ram_gb", 0) sel_alch = sel["driver"] == "i915" or sel.get("devid") in ALCHEMIST_IDS if sel_alch and need and real_ram < need: diff --git a/tests/sycl/test_intel_two_card_spec.py b/tests/sycl/test_intel_two_card_spec.py new file mode 100644 index 00000000..10b49c7e --- /dev/null +++ b/tests/sycl/test_intel_two_card_spec.py @@ -0,0 +1,182 @@ +"""strata_control/intel_spec.py: --spec 2 for a run config on exactly two Arc B-series (Battlemage) cards. + +The rule changes only the generated default (--spec 4). Single cards, Alchemist cards, three cards, a config with no draft +layer and anything the person set stay as they are. Also checks the two places that apply it: sycl/setup_intel.py's +config rewrite (through apply_to_config) and the Intel server's engine_args wrapper.""" +import importlib.util +import json +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent.parent +sys.path.insert(0, str(ROOT)) +from strata_control import intel_spec # noqa: E402 + +B65 = {"driver": "xe", "devid": "e222"} +B70 = {"driver": "xe", "devid": "e223"} +B50 = {"driver": "xe", "devid": "e212"} +B580 = {"driver": "xe", "devid": "e20b"} +A770 = {"driver": "xe", "devid": "56a0"} +A750 = {"driver": "i915", "devid": "56a1"} +IGPU = {"driver": "xe", "devid": "64a0"} +ARGS = ["--pack", "/p", "--native", "/m.gguf", "--prefill", "auto", "--spec", "4", "--spec-min-p", "0.5", "--mtp", "/m/rt", + "--max-context", "32768", "--kv", "int8"] + + +def spec_of(args): + return args[args.index("--spec") + 1] + + +def test_battlemage_by_pci_id(): + for c in (B65, B70, B50, B580, {"driver": "xe", "devid": "0xE223"}, {"driver": "xe", "devid": "E20C"}): + assert intel_spec.is_battlemage(c["driver"], c["devid"]), c + assert not intel_spec.is_battlemage("xe", "56a0") # an A770 on xe is Alchemist + assert not intel_spec.is_battlemage("i915", "56a1") # the A750 + assert not intel_spec.is_battlemage("i915", "e223") # the driver decides too + assert not intel_spec.is_battlemage("xe", "64a0") # an integrated Xe2 part is not a B-series card + assert not intel_spec.is_battlemage("xe", "") + assert not intel_spec.is_battlemage("xe", "zzzz") + + +def test_two_b_series_cards_get_spec_2(): + for pair in ([B65, B65], [B70, B70], [B65, B50], [B580, B580]): + args, note = intel_spec.plan(ARGS, None, pair) + assert spec_of(args) == "2" and note, pair + assert spec_of(ARGS) == "4" # the input list is not changed + # nothing else moved + args, _ = intel_spec.plan(ARGS, None, [B65, B65]) + assert [a for i, a in enumerate(args) if args[i - 1:i] != ["--spec"]] == [a for i, a in enumerate(ARGS) if ARGS[i - 1:i] != ["--spec"]] + + +def test_everything_else_stays(): + for cards in ([], [B65], [A750], [A770], [A750, A750], [A770, A770], [B65, A770], [A750, B65], [B65, IGPU], + [B65, B65, B65], [B65, B65, B65, B65]): + args, note = intel_spec.plan(ARGS, None, cards) + assert args == ARGS and note == "", cards + + +def test_the_persons_own_spec_wins(): + # in user_args, whatever its value + for val in ("4", "3", "2", "6"): + args, note = intel_spec.plan(ARGS, ["--spec", val], [B65, B65]) + assert args == ARGS and note == "", val + # unrelated user options do not stop the rule + args, note = intel_spec.plan(ARGS, ["--vram-reserve-mib", "800"], [B65, B65]) + assert spec_of(args) == "2" and note + # a value in args that is not setup's own 4 was chosen by somebody + for val in ("3", "2", "5", "8"): + mine = list(ARGS) + mine[mine.index("--spec") + 1] = val + args, note = intel_spec.plan(mine, None, [B65, B65]) + assert args == mine and note == "", val + # no --spec at all: nothing is added + no_spec = [a for a in ARGS if a not in ("--spec", "4")] + args, note = intel_spec.plan(no_spec, None, [B65, B65]) + assert args == no_spec and note == "" + + +def test_no_draft_layer_no_change(): + no_mtp = ARGS[:ARGS.index("--mtp")] + ARGS[ARGS.index("--mtp") + 2:] + assert intel_spec.plan(no_mtp, None, [B65, B65]) == (no_mtp, "") + assert intel_spec.plan(ARGS, None, [B65, B65], mtp_off=True) == (ARGS, "") + + +def test_apply_to_config_keeps_the_written_copy_in_step(): + cfg = {"args": list(ARGS), "args_written": list(ARGS), "env": {}} + note = intel_spec.apply_to_config(cfg, [B65, B65]) + assert note and spec_of(cfg["args"]) == "2" and spec_of(cfg["args_written"]) == "2" + # a second call (setup run again) changes nothing and says nothing + assert intel_spec.apply_to_config(cfg, [B65, B65]) == "" + # a config of the person's: user_args with --spec, "mtp": false, one card + own = {"args": list(ARGS), "args_written": list(ARGS), "user_args": ["--spec", "4"]} + assert intel_spec.apply_to_config(own, [B65, B65]) == "" and spec_of(own["args"]) == "4" + off = {"args": list(ARGS), "mtp": False} + assert intel_spec.apply_to_config(off, [B65, B65]) == "" and spec_of(off["args"]) == "4" + one = {"args": list(ARGS)} + assert intel_spec.apply_to_config(one, [B65]) == "" and spec_of(one["args"]) == "4" + + +def test_sysfs_cards_in_card_order(tmp_path): + def card(n, vendor, devid, driver): + d = tmp_path / f"card{n}" / "device" + d.mkdir(parents=True) + (d / "vendor").write_text(vendor + "\n") + (d / "device").write_text(devid + "\n") + drv = tmp_path / "drivers" / driver + drv.mkdir(parents=True, exist_ok=True) + try: + (d / "driver").symlink_to(drv, target_is_directory=True) + except OSError: # no symlinks (Windows without rights): nothing to read + return False + return True + ok = card(0, "0x8086", "0xe222", "xe") + card(1, "0x8086", "0xe222", "xe") + card(2, "0x10de", "0x2d05", "nvidia") + (tmp_path / "card0-DP-1").mkdir() + if not ok: + return + cards = intel_spec.sysfs_cards(tmp_path) + assert [c["devid"] for c in cards] == ["e222", "e222"] and [c["index"] for c in cards] == [0, 1] + assert intel_spec.exactly_two_battlemage(cards) + + +def _server_intel(): + sys.path.insert(0, str(ROOT / "sycl" / "serve")) + spec = importlib.util.spec_from_file_location("server_intel_under_test", ROOT / "sycl" / "serve" / "server_intel.py") + mod = importlib.util.module_from_spec(spec) + spec.loader.exec_module(mod) + return mod + + +def test_server_wrapper_applies_the_rule_at_start(monkeypatch): + mod = _server_intel() + S = mod.S + seen = [] + monkeypatch.setattr(S, "engine_args", lambda cfg: list(cfg["args"])) + monkeypatch.setattr(intel_spec, "sysfs_cards", lambda *a, **k: [dict(B65, index=0), dict(B65, index=1)]) + mod.install_two_card_spec() + cfg = {"backend": "sycl", "gpu": [0, 1], "args": list(ARGS)} + assert spec_of(S.engine_args(cfg)) == "2" + assert spec_of(cfg["args"]) == "4" # the config in memory is not rewritten by a start + assert spec_of(S.engine_args({**cfg, "user_args": ["--spec", "4"]})) == "4" + assert spec_of(S.engine_args({**cfg, "gpu": 0})) == "4" # one card + assert spec_of(S.engine_args({**cfg, "backend": "hip"})) == "4" + assert spec_of(S.engine_args({**cfg, "args": ARGS + ["--peer-device", "1"]})) == "4" # not a layer split + assert spec_of(S.engine_args({**cfg, "mtp": False})) == "4" + + +def test_server_wrapper_one_a_series_card_stays(monkeypatch): + mod = _server_intel() + S = mod.S + monkeypatch.setattr(S, "engine_args", lambda cfg: list(cfg["args"])) + monkeypatch.setattr(intel_spec, "sysfs_cards", lambda *a, **k: [dict(B65, index=0), dict(A750, index=1)]) + mod.install_two_card_spec() + cfg = {"backend": "sycl", "gpu": [0, 1], "args": list(ARGS)} + assert spec_of(S.engine_args(cfg)) == "4" + + + +def _derive(base, settings, cards): + from unittest import mock + from strata_cli.launch.runner import derive_config + with mock.patch.object(intel_spec, "sysfs_cards", lambda *a, **k: cards): + return derive_config(base, settings, bool_flags=frozenset()) + + +def test_launcher_writes_spec_2_for_two_b_series_cards(): + from strata_cli.launch import settings as L + base = {"backend": "sycl", "gpu": [0, 1], "gpus_asked": True, "layer_split": "auto", "exe": "component:strata-engine", + "args": list(ARGS), "port": 8095} + two = [dict(B65, index=0), dict(B65, index=1)] + cfg = _derive(base, L.LaunchSettings(), two) + assert spec_of(cfg["args"]) == "2" and spec_of(base["args"]) == "4" # the installed config is never edited + # the person's choices come after the rule and win: --set --spec 4 on the command line, user_args in the config + assert spec_of(_derive(base, L.LaunchSettings(extra={"--spec": "4"}), two)["args"]) == "4" + assert spec_of(_derive(base, L.LaunchSettings(extra={"--spec": "3"}), two)["args"]) == "3" + assert spec_of(_derive({**base, "user_args": ["--spec", "4"]}, L.LaunchSettings(), two)["args"]) == "4" + # no draft layer for this launch: nothing to pipeline, nothing changed + assert "--spec" not in _derive(base, L.LaunchSettings(mtp="off"), two)["args"] or spec_of(_derive(base, L.LaunchSettings(mtp="off"), two)["args"]) == "4" + # one card, an A-series card in the pair, another backend + assert spec_of(_derive({**base, "gpu": 0}, L.LaunchSettings(), two)["args"]) == "4" + assert spec_of(_derive(base, L.LaunchSettings(), [dict(B65, index=0), dict(A750, index=1)])["args"]) == "4" + assert spec_of(_derive({**base, "backend": "hip"}, L.LaunchSettings(), two)["args"]) == "4"