diff --git a/data/expert-profile.bin b/data/expert-profile.bin index bf4aeed4..a4ed7e5f 100644 Binary files a/data/expert-profile.bin and b/data/expert-profile.bin differ diff --git a/setup.py b/setup.py index 96fce32d..f9d3b161 100644 --- a/setup.py +++ b/setup.py @@ -249,6 +249,7 @@ def find_nvcc(): cands += [str(p / "bin" / "nvcc.exe") for p in sorted(base.iterdir(), reverse=True)] else: cands += [str(p / "bin" / "nvcc") for p in sorted(Path("/usr/local").glob("cuda*"), reverse=True)] + cands += [str(p / "bin" / "nvcc") for p in sorted(Path("/opt").glob("cuda*"), reverse=True)] # Arch (#46) best = (None, None) for c in dict.fromkeys(cands): # every toolkit found; the newest wins if c and Path(c).exists(): @@ -550,8 +551,9 @@ def install_build_tools(gpu, yes): else: apt = shutil.which("apt-get") if apt is None: - fail("automatic install is only done on Ubuntu/Debian", - "install g++ and the CUDA Toolkit 13 (https://developer.nvidia.com/cuda-downloads), then run it again") + fail("missing: " + " and ".join(missing) + " (the automatic install is only done on Ubuntu/Debian)", + "install them with your distribution's packages (Arch: pacman -S base-devel cuda; nvcc is found on " + "PATH, in /usr/local/cuda* and in /opt/cuda*), then run it again") if not have_cc: run(["sudo", "apt-get", "install", "-y", "build-essential"]) if nvcc is None or cuda_v < need_cuda: @@ -582,15 +584,20 @@ def cmake_build(src, bdir, target, defs, vcvars, bat_name): conf = [cmake, "-G", "Ninja", f"-DCMAKE_MAKE_PROGRAM={ninja}", "-S", str(src), "-B", str(bdir), "-DCMAKE_BUILD_TYPE=Release", *defs] build = [cmake, "--build", str(bdir), "--target", target, "-j", str(max(2, (os.cpu_count() or 4) // 2))] + # A failed build is tried once more: CUDA 13.0's ptxas now and then fails to parse a PTX file it just wrote, and + # the same command then gets past it (issue #45); a second attempt only compiles what is still missing. if WIN: bat = ROOT / bat_name q = lambda c: " ".join(f'"{x}"' if " " in str(x) else str(x) for x in c) # noqa: E731 - bat.write_text(f'@echo off\r\ncall "{vcvars}" >nul\r\n{q(conf)} || exit /b 1\r\n{q(build)} || exit /b 1\r\n', + bat.write_text(f'@echo off\r\ncall "{vcvars}" >nul\r\n{q(conf)} || exit /b 1\r\n{q(build)} && exit /b 0\r\n' + f'echo (the build stopped - trying it once more)\r\n{q(build)} || exit /b 1\r\n', encoding="utf-8") run(["cmd", "/c", str(bat)]) else: run(conf) - run(build) + if run(build, check=False).returncode != 0: + say(" (the build stopped - trying it once more)") + run(build) ENGINE_SOURCES = ("CMakeLists.txt", "src", "include", "third_party/ggml") @@ -640,7 +647,8 @@ def build_engine(gpu, vision, yes, llama) -> Path: shutil.copy2(ROOT / "build-vision" / "bin" / VEXE, eng / VEXE) bindir = Path(nvcc).parent # the toolkit's own libraries (bin, bin/x64, lib64) dirs = [str(d) for d in (bindir, bindir / "x64", bindir.parent / "lib64") if d.is_dir()] - stamp.write_text(json.dumps({"source": "local", "archs": [int(gpu["arch"])], "vision": vision, + stamp.write_text(json.dumps({"source": "local", "version": source_version(), "archs": [int(gpu["arch"])], + "vision": vision, "cuda_dirs": dirs, "src": src, "vision_src": vsrc if want_vision else None}, indent=1)) ok(f"engine compiled: {eng / EXE}") return eng @@ -651,15 +659,28 @@ def installed_configs(): return sorted(ROOT.glob("strata-*.json"), key=lambda p: p.stat().st_mtime, reverse=True) +def source_version() -> str: + """The engine version the source tree builds (CMakeLists.txt's project version).""" + m = re.search(r"project\(strata VERSION ([\d.]+)", (ROOT / "CMakeLists.txt").read_text(encoding="utf-8")) + return m.group(1) if m else "0" + + def engine_version(exe: Path) -> tuple: - """The version in the engine folder's BUILD.json; a locally compiled engine is the current source's.""" + """The version in the engine folder's BUILD.json, or else the one compiled into the binary. A locally compiled + engine is not necessarily the source's version: when compiling a `git pull` fails, the previous engine is kept + (issue #49).""" try: meta = json.loads((Path(exe).parent / "BUILD.json").read_text()) except (OSError, ValueError): - return (0, 0, 0) - if meta.get("source") == "local": - return MIN_ENGINE - return tuple(int(x) for x in str(meta.get("version", "0")).split(".")[:3] if x.isdigit()) + meta = {} + v = str(meta.get("version") or "") + if not v: # the version compiled into the binary: 0.1.13 and newer + try: # carry it, so a binary without it is older + m = re.search(rb"engine=(\d+\.\d+\.\d+)\n", Path(exe).read_bytes()) + v = m.group(1).decode() if m else "0.1.12" + except OSError: + v = "0" + return tuple(int(x) for x in v.split(".")[:3] if x.isdigit()) def is_wsl() -> bool: @@ -672,10 +693,15 @@ def upgrade_config(cfg_path: Path, cfg: dict) -> dict: KV streaming is dropped: its RAM copy must be pinned, and the driver pins only about 1 GB there.""" a = cfg.get("args", []) changed = False - if "--prefill" in a and a[a.index("--prefill") + 1] == "2048" and engine_version(cfg["exe"]) >= (0, 1, 13): + ver = engine_version(cfg["exe"]) if "--prefill" in a else (0, 0, 0) + if "--prefill" in a and a[a.index("--prefill") + 1] == "2048" and ver >= (0, 1, 13): a[a.index("--prefill") + 1] = "auto" changed = True ok("prompt reading: the engine now picks its chunk size (--prefill auto)") + elif "--prefill" in a and a[a.index("--prefill") + 1] == "auto" and (0, 0, 0) < ver < (0, 1, 13): + a[a.index("--prefill") + 1] = "2048" # an older engine kept after a failed update (issue #49) + changed = True + warn(f"the installed engine is {'.'.join(map(str, ver))}: prompts are read in 2048-token chunks until it is updated") if is_wsl() and "--kv-resident" in a: i = a.index("--kv-resident") del a[i:i + 2] diff --git a/src/program/generate.cpp b/src/program/generate.cpp index be66c924..ed7a2f7b 100644 --- a/src/program/generate.cpp +++ b/src/program/generate.cpp @@ -1409,10 +1409,10 @@ int main(int argc, char** argv) { std::fprintf(stderr, "strata generate: %s\n", err.c_str()); return 1; } - // The profile knows how many slots it was built for. `--expert-cache 0` means "take the profile's"; - // an explicit smaller number is allowed and simply truncates the ranked list, which is the right - // behaviour for asking "what would 2,000 slots give" without rebuilding the file. - if (o.expert_cache == 0) o.expert_cache = (int) pslots; + // An explicit number truncates the ranked list ("what would 2,000 slots give" without rebuilding the + // file). `--expert-cache 0` used to take the count the profile was built for; the profile now ranks + // every pair (issue #46: a card that holds more than the old 8,000 used to stop there), so it means auto. + if (o.expert_cache == 0) o.expert_cache = -1; std::fprintf(stderr, "strata generate: profile %s: %zu ranked pairs, built for %lld slots\n", o.expert_profile.c_str(), profile.size(), (long long) pslots); } @@ -2330,8 +2330,11 @@ int main(int argc, char** argv) { } mem_mark("the verifier and the drafter's binding"); ver.set_split(o.spec_split); - ver.set_pcie_mode(o.pcie_mode == "dma" ? 0 : o.pcie_mode == "direct" ? 1 : o.pcie_mode == "kernel" ? 2 - : native_pack ? 0 : 2); // auto: DMA for the native packs, the copy kernel for Q2_0 + // auto: the copy kernel for every pack. DMA (the native packs' default until 0.1.13) has the host call + // cudaMemcpyAsync + cudaLaunchHostFunc inside a verify window while the GPU spins on the flag they raise; + // issue #31's thread dumps show the host stuck in that cudaMemcpyAsync on a driver lock for good. The copy + // kernel needs no host CUDA call there, and costs ~1-3% decode on IQ3_S (45.3 -> 44.8 tok/s, 8 requests). + ver.set_pcie_mode(o.pcie_mode == "dma" ? 0 : o.pcie_mode == "direct" ? 1 : 2); std::vector cur; // ---- the conversation cache (see ConvCheckpoint). `live` is what the session holds right now: the tokens // it has consumed, so a request that starts with exactly them continues without any copy. `checks` are the @@ -3541,8 +3544,11 @@ int main(int argc, char** argv) { } mem_mark("the verifier and the drafter's binding"); ver.set_split(o.spec_split); - ver.set_pcie_mode(o.pcie_mode == "dma" ? 0 : o.pcie_mode == "direct" ? 1 : o.pcie_mode == "kernel" ? 2 - : native_pack ? 0 : 2); // auto: DMA for the native packs, the copy kernel for Q2_0 + // auto: the copy kernel for every pack. DMA (the native packs' default until 0.1.13) has the host call + // cudaMemcpyAsync + cudaLaunchHostFunc inside a verify window while the GPU spins on the flag they raise; + // issue #31's thread dumps show the host stuck in that cudaMemcpyAsync on a driver lock for good. The copy + // kernel needs no host CUDA call there, and costs ~1-3% decode on IQ3_S (45.3 -> 44.8 tok/s, 8 requests). + ver.set_pcie_mode(o.pcie_mode == "dma" ? 0 : o.pcie_mode == "direct" ? 1 : 2); drive.d.plan = ver.plan_sink(); drive.d.pcie_num = (int) (o.pcie_frac * 256.0 + 0.5); if (drive.d.pcie_num < 0) drive.d.pcie_num = 0; diff --git a/tools/make_profile.py b/tools/make_profile.py new file mode 100644 index 00000000..df3e342b --- /dev/null +++ b/tools/make_profile.py @@ -0,0 +1,96 @@ +"""tools/make_profile.py - the expert-cache profile (`data/expert-profile.bin`): every (layer, expert) pair, ranked. + +`--expert-cache auto` fills the VRAM slots it can afford with the profile's pairs in order, so the ranking decides +which experts start resident (the adaptive tier then swaps in what a conversation routes most). A profile that +ranks fewer pairs than a card can hold caps the cache (issue #46: a 32 GB card stopped at 8,000 slots); this tool +writes one that ranks all 24,576. + +The order: the base profile's ranking (default: the shipped data/expert-profile.bin), then the pairs your routing +traces used, most frequent first, then every pair still missing, interleaved across the layers. + + python tools/make_profile.py [TRACE ...] [--base data/expert-profile.bin | --no-base] [--out PATH] + +A routing trace comes from a one-shot engine run with `--dump-routing FILE` (a prompt typical of your use; the +routed experts of every layer and position are written). Point the model config's `--expert-profile` at the result. +""" +import argparse +import struct +from collections import defaultdict +from pathlib import Path + +ROOT = Path(__file__).resolve().parent.parent +N_LAYER, N_EXPERT = 48, 512 +MAGIC, VERSION = b"STRP", 1 + + +def read_profile(path): + blob = Path(path).read_bytes() + if blob[:4] != MAGIC: + raise SystemExit(f"{path}: not a Strata profile") + ver, nl, ne, slots, n = struct.unpack_from("<5I", blob, 4) + if (nl, ne) != (N_LAYER, N_EXPERT): + raise SystemExit(f"{path}: {nl}x{ne}, not {N_LAYER}x{N_EXPERT}") + return [struct.unpack_from("