diff --git a/docs/INTEL_ARC.md b/docs/INTEL_ARC.md index 1db7e3de..683467f3 100644 --- a/docs/INTEL_ARC.md +++ b/docs/INTEL_ARC.md @@ -126,6 +126,13 @@ Things that matter on an Arc (details in [INTEL.md](INTEL.md)): - `--layer-split auto` on two equal cards takes the split that leaves the fullest card the most VRAM to spare, within 0.2% of the best predicted time (`STRATA_SPLIT_HEADROOM_TOL`). `STRATA_SPLIT_HEADROOM=0` turns that off. A full card under `xe` moves buffers to system memory instead of failing. The startup line names both splits when the choice moves. +- **Two Arc B-series cards decode faster by default.** With exactly two B-series cards (B50, B60, B65, B70, B570, B580) in a layer + split, the first card starts the next window while the second still checks this one (`--pipeline-windows 2`), and setup writes + `--spec 2` for the pair. On 2x Arc Pro B65 this decodes 17% to 23% faster than the old serial `--spec 4` setup, 5 pairs out of 5: + chat 71-74 tok/s against 60-63, JSON 80-83 against 67-68, code 72-78 against 59-65, plain text 67-68 against 56-57. The answers are + those of a serial `--spec 2` run (100 of 100 agent requests); a serial `--spec 4` run differs in a rare near-tie (3 of 100). + One card, an A-series card, a mixed pair and three or more cards run as before. `--pipeline-windows 0` turns it off, and a + `--spec` of your own, put in the config's `user_args`, is kept. - **Arc Pro B60:** PCI id `8086:e211` (`lspci -nn`), `sycl-ls` prints `Intel(R) Arc(TM) Pro B60 Graphics 20.1.0`, so `-DSTRATA_SYCL_AOT=bmg-g21` is the right target. `setup_intel.py` knows both B60 ids, `e211` and `e221`. On a `--layer-split` the startup line "N experts are neither in VRAM nor mirrored" counts the other card's layers; read the following @@ -154,6 +161,7 @@ Things that matter on an Arc (details in [INTEL.md](INTEL.md)): | Community | One Arc Pro B60 24 GB | Coder IQ1_M with 4,042 of 12,288 experts mirrored in RAM: 11.9 tok/s decode | same | | Maintainers | Arc Pro B70 32 GB (xe), 0.1.40.2 and later | Flash-Next IQ3_S with 11.5k of 24.6k experts resident: 10-prompt gate passes, prompt 980-1,000 tok/s on 4K tokens, decode 30 (prose) to 41 (code) tok/s | [INTEL.md](INTEL.md#arc-pro-b70-with-a-model-that-does-not-fit-flash-next-iq3_s) | | Maintainers | Arc A750 8 GB (i915) | Flash-Next IQ3_XXS about 10-19 tok/s decode | [INTEL.md](INTEL.md#arc-a750-and-the-other-alchemist-cards) | +| Maintainers | 2x Arc Pro B65 32 GB (xe), 0.2.8 | Flash-Next IQ3_S, every expert in VRAM, the JIT package build: decode 71-83 tok/s (chat, JSON, code), prompts 1,261 / 1,511 / 1,628 tok/s at 4K / 8K / 16K | the bullet above | Two cards on `xe` (2x Arc Pro B70, IQ3_S, 0.1.44): `--layer-split auto` picked a split at about 11 tok/s where `--layer-split 30` ran about 54. The auto choice now prefers the split with the most VRAM headroom (above). diff --git a/docs/MULTI_GPU.md b/docs/MULTI_GPU.md index 78277bca..51a65471 100644 --- a/docs/MULTI_GPU.md +++ b/docs/MULTI_GPU.md @@ -181,6 +181,9 @@ tokens. The tokens are the serial loop's, up to CPU rounding. - **Default:** on for AMD RDNA4 (gfx1200 / gfx1201) with `--serve`, the draft layer, and a split of two or more stages when the drafter can sit on the first card, otherwise three or more. The log says "is on by default for a layer split of N stages". `--pipeline-windows 0` or `STRATA_PIPELINE_AUTO=0` turns it off. NVIDIA builds keep it opt-in (`--pipeline-windows 2`). + The Intel Arc engine turns it on for exactly two Arc B-series (Battlemage) cards, with `--serve` and the draft layer (`--spec 2` + or more): on 2x Arc Pro B65 decode is 17% to 23% faster, and setup writes `--spec 2` for those cards ([INTEL_ARC.md](INTEL_ARC.md)). + It runs exactly two cards, so one card, Arc A-series cards and three or more cards stay serial. - **Draft layer placement:** with the pipeline on, the draft layer loads on the first card, whose window has finished when the chain starts. `STRATA_MTP_STAGE0=0` keeps the old place. - **Off, with one log line saying why:** `--batch` slots, `--peer-device`, the helper caches (`--expert-cache-device1..3`, which diff --git a/strata_cli/launch/runner.py b/strata_cli/launch/runner.py index 582e185c..bd256234 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/CMakeLists.txt b/sycl/CMakeLists.txt index 17e47c51..dac10518 100644 --- a/sycl/CMakeLists.txt +++ b/sycl/CMakeLists.txt @@ -441,6 +441,9 @@ if(STRATA_SYCL_PARITY) add_executable(esimd_moe_mode_test ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/esimd_moe_mode_test.cpp) # 151-b70: the default choice of the expert chain (no GPU) target_include_directories(esimd_moe_mode_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) add_test(NAME esimd_moe_mode_test COMMAND esimd_moe_mode_test) + add_executable(pipeline_auto_test ${CMAKE_CURRENT_SOURCE_DIR}/src/core/pipeline_auto_test.cpp) # the default of --pipeline-windows (no GPU) + target_include_directories(pipeline_auto_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) + add_test(NAME pipeline_auto_test COMMAND pipeline_auto_test) if(_i8_esimd) # DPAS s8 8x16x32 (Xe2): not for an Alchemist / Xe-HPC AOT build, nor with STRATA_SYCL_I8_ESIMD=OFF add_executable(esimd_dpas_probe ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/esimd_dpas_probe.cpp) # ESIMD DPAS peak (registers only) add_executable(esimd_i8_bench ${CMAKE_CURRENT_SOURCE_DIR}/src/kernels/esimd_i8_bench.cpp) # ESIMD int8 DPAS GEMM on pre-decoded weights diff --git a/sycl/include/strata/pipeline_auto.hpp b/sycl/include/strata/pipeline_auto.hpp new file mode 100644 index 00000000..6198698b --- /dev/null +++ b/sycl/include/strata/pipeline_auto.hpp @@ -0,0 +1,32 @@ +// The default for --pipeline-windows when it is not given (host-only logic, no SYCL runtime: tested by +// src/core/pipeline_auto_test.cpp). On for a layer split of exactly two Arc Battlemage (Xe2) cards, with --serve and the +// MTP drafter; off for everything else. The caller finds `battlemage_*` from the devices' architecture ids +// (strata::intel_gpu_gen), never from a name. +#pragma once + +namespace strata { + +struct PipelineAutoInputs { + bool serve = false; ///< --serve + bool multi_gpu = false; ///< a layer split across GPUs + bool split_same = false; ///< the split puts every stage on one GPU + unsigned later_stages = 0; ///< GPUs after the first one: 1 = a split of exactly two stages + bool auto_off = false; ///< STRATA_PIPELINE_AUTO=0 + bool has_mtp = false; ///< --mtp given + int spec = 0; ///< --spec after the engine's own widening (>= 2 means a draft window) + int batch = 0; ///< --batch slots + bool peer_device = false; ///< --peer-device + bool helper_caches = false; ///< --expert-cache-device1..3 + bool first_is_battlemage = false; ///< stage 0's GPU is a Battlemage card (architecture id) + bool later_is_battlemage = false; ///< the second stage's GPU is a Battlemage card +}; + +/// True when --pipeline-windows 2 is the default for this run. Single card, an Arc A-series card (Alchemist), any other +/// vendor, three or more stages (the SYCL pipeline runs exactly two), --batch, --peer-device and the helper caches stay serial. +inline bool pipeline_auto_on(const PipelineAutoInputs& in) { + return in.serve && in.multi_gpu && !in.split_same && in.later_stages == 1 && !in.auto_off && in.has_mtp && + in.spec >= 2 && in.batch == 0 && !in.peer_device && !in.helper_caches && in.first_is_battlemage && + in.later_is_battlemage; +} + +} // namespace strata 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/sycl/src/core/pipeline_auto_test.cpp b/sycl/src/core/pipeline_auto_test.cpp new file mode 100644 index 00000000..88ace2bb --- /dev/null +++ b/sycl/src/core/pipeline_auto_test.cpp @@ -0,0 +1,40 @@ +// The default of --pipeline-windows (strata::pipeline_auto_on), no GPU needed: on only for a layer split of exactly two +// Battlemage cards with --serve and the MTP drafter. +#include +#include "strata/pipeline_auto.hpp" + +using strata::PipelineAutoInputs; +using strata::pipeline_auto_on; +static int bad = 0; +#define CHECK(cond, what) do { if (!(cond)) { std::printf("FAIL: %s\n", what); ++bad; } } while (0) + +static PipelineAutoInputs two_b65() { + PipelineAutoInputs in; + in.serve = true; in.multi_gpu = true; in.later_stages = 1; in.has_mtp = true; in.spec = 4; + in.first_is_battlemage = true; in.later_is_battlemage = true; + return in; +} + +int main() { + CHECK(pipeline_auto_on(two_b65()), "two Battlemage cards, serve, MTP -> on"); + { auto in = two_b65(); in.spec = 2; CHECK(pipeline_auto_on(in), "spec 2 -> on"); } + { auto in = two_b65(); in.spec = 1; CHECK(!pipeline_auto_on(in), "spec 1 (no draft window) -> off"); } + { auto in = two_b65(); in.spec = 0; CHECK(!pipeline_auto_on(in), "no --spec -> off"); } + { auto in = two_b65(); in.has_mtp = false; CHECK(!pipeline_auto_on(in), "no MTP drafter -> off"); } + { auto in = two_b65(); in.serve = false; CHECK(!pipeline_auto_on(in), "no --serve -> off"); } + { auto in = two_b65(); in.multi_gpu = false; CHECK(!pipeline_auto_on(in), "single card -> off"); } + { auto in = two_b65(); in.split_same = true; CHECK(!pipeline_auto_on(in), "both stages on one GPU -> off"); } + { auto in = two_b65(); in.later_stages = 0; CHECK(!pipeline_auto_on(in), "no later stage -> off"); } + { auto in = two_b65(); in.later_stages = 2; CHECK(!pipeline_auto_on(in), "three stages (the pipeline runs exactly two) -> off"); } + { auto in = two_b65(); in.later_stages = 3; CHECK(!pipeline_auto_on(in), "four stages -> off"); } + { auto in = two_b65(); in.first_is_battlemage = false; CHECK(!pipeline_auto_on(in), "first card not Battlemage (A-series) -> off"); } + { auto in = two_b65(); in.later_is_battlemage = false; CHECK(!pipeline_auto_on(in), "second card not Battlemage -> off"); } + { auto in = two_b65(); in.first_is_battlemage = false; in.later_is_battlemage = false; + CHECK(!pipeline_auto_on(in), "two A-series cards -> off"); } + { auto in = two_b65(); in.auto_off = true; CHECK(!pipeline_auto_on(in), "STRATA_PIPELINE_AUTO=0 -> off"); } + { auto in = two_b65(); in.batch = 4; CHECK(!pipeline_auto_on(in), "--batch slots -> off"); } + { auto in = two_b65(); in.peer_device = true; CHECK(!pipeline_auto_on(in), "--peer-device -> off"); } + { auto in = two_b65(); in.helper_caches = true; CHECK(!pipeline_auto_on(in), "helper caches -> off"); } + if (bad == 0) std::printf("pipeline_auto_test: all OK\n"); + return bad == 0 ? 0 : 1; +} diff --git a/sycl/src/program/generate.cpp b/sycl/src/program/generate.cpp index d23f09aa..4f61cad3 100644 --- a/sycl/src/program/generate.cpp +++ b/sycl/src/program/generate.cpp @@ -25,6 +25,7 @@ #include #include #include "strata/sycl_queue.hpp" +#include "strata/pipeline_auto.hpp" #include "strata/sycl_doorbell.hpp" #include "strata/core/arch_defaults.hpp" #include "strata/core/dma_batch.hpp" @@ -704,11 +705,12 @@ struct Options { /// result) goes through the verify windows, S tokens at a time, instead of the batched prompt path (0 = always /// the batched path) int64_t short_read = 64; - /// --pipeline-windows N (--serve, a layer split on exactly two GPUs; opt-in): 1 = a prompt's short reads through + /// --pipeline-windows N (--serve, a layer split on exactly two GPUs): 1 = a prompt's short reads through /// the verify windows run with the stages overlapped (stage 0 reads window K+1 while stage 1 reads window K); /// 2 = the decode windows too: stage 0 runs window K+1 speculatively while stage 1 verifies window K (rolled back - /// when K is not accepted as guessed). 0 = off (the stages take turns). - int pipeline_windows = 0; + /// when K is not accepted as guessed). 0 = off (the stages take turns). -1 (not given) = automatic: 2 when both + /// GPUs of the two-stage split are Battlemage (Xe2) cards and the MTP drafter runs (spec >= 2), else 0. + int pipeline_windows = -1; /// The suffix drafter (prompt lookup): when the text being written repeats an earlier stretch of the context (code /// edits, quoted input, tool-call JSON) by at least this many tokens, the window may be filled with what followed /// it there instead of the MTP's drafts, where the MTP's own first guess agrees and the draft policy expects it to @@ -834,7 +836,8 @@ void usage() { " --batch-mtp --batch (opt-in, one GPU, needs --mtp and --spec): each slot also verifies one MTP\n" " proposal per window (STRATA_BATCH_MTP=1 does the same); needs VRAM per slot\n" " --batch-groups G --batch with a layer split: the slots in G groups pipelined through the GPUs\n" - " --pipeline-windows N --serve with a layer split on two GPUs (opt-in): one conversation's verify windows\n" + " --pipeline-windows N --serve with a layer split on two GPUs (default 2 on two Arc B-series cards with\n" + " the MTP drafter, else 0): one conversation's verify windows\n" " with the GPUs overlapped - 1 = the prompt's short reads, 2 = decode as well\n" " (stage 0 runs the next window while stage 1 verifies this one); docs/MULTI_GPU.md\n" " --trim-stage-weights an explicit --layer-split: each GPU loads only its own layers' dense weights\n" @@ -3405,9 +3408,39 @@ int main(int argc, char **argv) try { } } strata::core::Verifier::set_commit_async(!multi_gpu); // see Verifier::set_commit_async - // --pipeline-windows (opt-in): one conversation's windows with the two stages of a layer split overlapped. Decided + // --pipeline-windows: one conversation's windows with the two stages of a layer split overlapped. Decided // here, before any stage sizes its expert cache (the second verifier per stage and the snapshots are allocated // after the caches, so their room is kept out of them). What it does not support turns it off, said once. + // Not given: on by default (2) for a split of exactly two Battlemage (Xe2) cards, found from the devices' + // architecture ids (strata::intel_gpu_gen), with --serve and the MTP drafter; a single card, an Arc A-series card or + // anything the pipeline does not support stays serial, said nothing. STRATA_PIPELINE_AUTO=0 or + // --pipeline-windows 0 turns it off. + if (o.pipeline_windows < 0) { + static const char* ae = std::getenv("STRATA_PIPELINE_AUTO"); + const auto battlemage = [](int d) -> bool { + try { + return strata::intel_gpu_gen(dpct::get_device(d)) == strata::IntelGpuGen::Battlemage; + } catch (...) { + return false; + } + }; + strata::PipelineAutoInputs in; + in.serve = o.serve; in.multi_gpu = multi_gpu; in.split_same = split_same; + in.later_stages = (unsigned) split_devs.size(); + in.auto_off = ae != nullptr && std::atoi(ae) == 0; + in.has_mtp = !o.mtp.empty(); in.spec = o.spec; in.batch = o.batch; + in.peer_device = o.peer_device >= 1; // the same test as the validation below + in.helper_caches = o.expert_cache_remote[0] > 0 || o.expert_cache_remote[1] > 0 || o.expert_cache_remote[2] > 0; + if (split_devs.size() == 1) { // the devices are looked at only for a split the pipeline could take + in.first_is_battlemage = battlemage(0); + in.later_is_battlemage = battlemage(split_devs[0]); + } + const bool on = strata::pipeline_auto_on(in); + o.pipeline_windows = on ? 2 : 0; + if (on) + std::fprintf(stderr, "strata generate: --pipeline-windows 2 is on by default for a layer split across two Arc " + "B-series cards (--pipeline-windows 0 or STRATA_PIPELINE_AUTO=0 turns it off)\n"); + } if (o.pipeline_windows > 0) { const bool helpers = o.expert_cache_remote[0] > 0 || o.expert_cache_remote[1] > 0 || o.expert_cache_remote[2] > 0; const char* off = !o.serve ? "it needs --serve" 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"