diff --git a/data/expert-profile-sycl.bin b/data/expert-profile-sycl.bin new file mode 100644 index 00000000..3e040a7c Binary files /dev/null and b/data/expert-profile-sycl.bin differ diff --git a/sycl/include/strata/copy_queue.hpp b/sycl/include/strata/copy_queue.hpp index 138298b8..38447631 100644 --- a/sycl/include/strata/copy_queue.hpp +++ b/sycl/include/strata/copy_queue.hpp @@ -11,7 +11,8 @@ // sycl::make_queue, so everything built on SYCL queues (memcpy, events, ext_oneapi_submit_barrier // across queues) keeps working, only the engine changes. It returns nullptr (the caller keeps an ordinary in-order queue) // when the backend is not Level Zero, the device has no copy-only group, the loader or the call fails, or -// STRATA_COPY_ENGINE=0. Same bytes, same kernels: the tokens do not change. +// STRATA_COPY_ENGINE=0 (default: on for a Battlemage card, which is where it was measured; =1 forces it on any Level Zero +// device, =0 off). Same bytes, same kernels: the tokens do not change. #pragma once #include diff --git a/sycl/setup_intel.py b/sycl/setup_intel.py index e8040cd7..7e323457 100644 --- a/sycl/setup_intel.py +++ b/sycl/setup_intel.py @@ -121,6 +121,7 @@ def drop(args, name, value=False): del args[i:i + 1 + int(value)] +SYCL_PROFILE = "expert-profile-sycl.bin" # data/: the xe cards' expert ranking (to_sycl) SMALL_CARD_GB = 12.0 # under this the engine's own advice is --vram-reserve-mib 300 (a 1024 MiB reserve leaves an # 8 GB card no room for even one expert slot) and the English draft vocabulary SMALL_RESERVE_MIB = 300 @@ -149,6 +150,16 @@ def to_sycl(cfg: dict, exe: Path, ram: float, keep: dict, vram_gb: float = 0.0, for f in ("--resident-experts", "--mmap-experts"): # setup's low-RAM mode is the CUDA engine's drop(args, f) drop(args, "--kv-resident", True) # decided below on the real RAM + if driver != "i915" and "--expert-profile" in args and (ROOT / "data" / SYCL_PROFILE).is_file(): + # 0.1.43: a card on xe runs every expert on the GPU (the ones the cache does not hold are read from the pinned mirror + # by the same kernels), so WHICH experts the cache holds changes speed and no token: data/expert-profile-sycl.bin + # ranks them from the routing of 45 varied prompts (chat, code, maths, six languages) and eight document summaries, + # where the shipped profile holds 60% of a chat prompt's experts and 85% of a summary's (B70: +9..25% decode on prompts left + # out of it, same tokens). Another profile the user chose is kept; i915 keeps the shipped one (there the CPU + # computes the misses, so the residency moves roundings). + i = args.index("--expert-profile") + 1 + if args[i].replace("\\", "/").rsplit("/", 1)[-1] == "expert-profile.bin": + args[i] = args[i][:-len("expert-profile.bin")] + SYCL_PROFILE for f in ("--pack", "--native", "--ple-gguf", "--expert-profile", "--mtp", "--control-vector-scaled"): if f in args: v = args[args.index(f) + 1] diff --git a/sycl/src/core/copy_queue.cpp b/sycl/src/core/copy_queue.cpp index d833d150..247db9ff 100644 --- a/sycl/src/core/copy_queue.cpp +++ b/sycl/src/core/copy_queue.cpp @@ -1,5 +1,6 @@ // src/core/copy_queue.cpp - see include/strata/copy_queue.hpp. #include "strata/copy_queue.hpp" +#include "strata/sycl_queue.hpp" #include #include @@ -84,6 +85,13 @@ sycl::queue* make_copy_engine_queue(const char* what) { try { if (dev.get_backend() != sycl::backend::ext_oneapi_level_zero) return nullptr; + // Battlemage only unless STRATA_COPY_ENGINE=1: on an Arc A750 (i915, JIT) the prompt path hung with it once the stager's + // completion flag became a copy-engine memcpy (4K and 600-token prompts, 10+ minutes, GPU not hung), and the measured gain + // there was 1.4% (a prompt there is compute bound). + { + const char* force = std::getenv("STRATA_COPY_ENGINE"); + if (!(force && force[0] == '1') && strata::intel_gpu_gen(dev) != strata::IntelGpuGen::Battlemage) return nullptr; + } // The legacy Level Zero adapter already puts memcpy on the copy engine (and its kernel launches are slower); only // the v2 adapter needs this. SYCL_UR_USE_LEVEL_ZERO_V2=0 selects the legacy one. const char* v2 = std::getenv("SYCL_UR_USE_LEVEL_ZERO_V2"); diff --git a/tools/test_make_profile_mix.py b/tools/test_make_profile_mix.py new file mode 100644 index 00000000..956cb7dd --- /dev/null +++ b/tools/test_make_profile_mix.py @@ -0,0 +1,53 @@ +"""Tests for tools/make_profile_mix.py, without a model: synthetic routing traces. + + python -m unittest tools.test_make_profile_mix +""" +from __future__ import annotations + +import subprocess +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "tools")) +import make_profile as MP # noqa: E402 +from test_make_profile import write_trace # noqa: E402 + +NE = 4 + + +def run(args): + r = subprocess.run([sys.executable, str(ROOT / "tools" / "make_profile_mix.py")] + args, capture_output=True, text=True) + assert r.returncode == 0, r.stderr + return r.stdout + + +class MixProfile(unittest.TestCase): + def test_a_long_trace_does_not_outvote_a_short_one(self): + with tempfile.TemporaryDirectory() as d: + long_t, short_t, out = Path(d, "long"), Path(d, "short"), Path(d, "p.bin") + # the long trace uses pair (0, 1) 1000 times and (0, 2) 100 times; the short one uses (0, 3) 5 times + write_trace(long_t, [(0, [1] * 10)] * 100 + [(0, [2] * 10)] * 10) + write_trace(short_t, [(0, [3] * 5)]) + run([str(long_t), str(short_t), "--out", str(out), "--n-expert", str(NE)]) + ranked = MP.read_profile(out, NE) + # as distributions: (0,1) 0.909, (0,2) 0.091 in the long one; (0,3) 1.0 in the short one -> (0,3) first + self.assertEqual(ranked[:3], [(0, 3), (0, 1), (0, 2)]) + self.assertEqual(len(ranked), MP.N_LAYER * NE) # every pair, once + self.assertEqual(len(set(ranked)), len(ranked)) + + def test_weights(self): + with tempfile.TemporaryDirectory() as d: + a, b, out = Path(d, "a"), Path(d, "b"), Path(d, "p.bin") + write_trace(a, [(1, [0] * 4)]) + write_trace(b, [(1, [2] * 4)]) + run([f"{a}:1", f"{b}:3", "--out", str(out), "--n-expert", str(NE)]) + self.assertEqual(MP.read_profile(out, NE)[:2], [(1, 2), (1, 0)]) + run([f"{a}:3", f"{b}:1", "--out", str(out), "--n-expert", str(NE)]) + self.assertEqual(MP.read_profile(out, NE)[:2], [(1, 0), (1, 2)]) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/test_setup_sycl.py b/tools/test_setup_sycl.py index cec1e5e9..0de2799d 100644 --- a/tools/test_setup_sycl.py +++ b/tools/test_setup_sycl.py @@ -179,6 +179,17 @@ class ToSycl(unittest.TestCase): self.assertEqual(c["args"][c["args"].index("--vram-reserve-mib") + 1], "1024") self.assertNotIn("STRATA_VERIFY_NO_HOST", c.get("env", {})) + def test_xe_ranks_the_experts_with_the_sycl_profile_and_i915_keeps_the_shipped_one(self): + m = self.mod() + shipped = str(m.MOUNT / "data" / "expert-profile.bin") + other = str(m.MOUNT / "mine" / "learned.bin") + c = m.to_sycl(self.cfg(m, ["--expert-profile", shipped]), m.ROOT / "build-sycl-aot" / "strata", 64.0, {}, 32.0, "xe") + self.assertTrue(c["args"][c["args"].index("--expert-profile") + 1].endswith("expert-profile-sycl.bin")) + c = m.to_sycl(self.cfg(m, ["--expert-profile", other]), m.ROOT / "build-sycl-aot" / "strata", 64.0, {}, 32.0, "xe") + self.assertTrue(c["args"][c["args"].index("--expert-profile") + 1].endswith("learned.bin")) # the user's own file stays + c = m.to_sycl(self.cfg(m, ["--expert-profile", shipped]), m.ROOT / "build-sycl" / "strata", 64.0, {}, 8.0, "i915") + self.assertTrue(c["args"][c["args"].index("--expert-profile") + 1].endswith("expert-profile.bin")) # the CPU computes the misses there + def test_an_explicit_reserve_is_kept_on_both_drivers(self): m = self.mod() for vram, drv in ((32.0, "xe"), (8.0, "i915")):