Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

setup: quick fixes and OS items (#657 #737 #670 #798 #775 #779 #841 #881)

Niko1221fb57b6cd81e1
diff --git a/setup.py b/setup.py
index def43b723..6b8fb56d6 100644
--- a/setup.py
+++ b/setup.py
@@ -255,9 +255,28 @@ def fail(msg, hint=None):
sys.exit(1)
+def flush_typed_ahead() -> None:
+ """#841: keys pressed while setup was busy (a download, a build) are not answers to the next question: Enter pressed
+ to "wake" a slow step answered "Go on anyway?" with its default. Dropped before a question is asked, on a terminal
+ only (a pipe or a test keeps its input)."""
+ try:
+ if not sys.stdin.isatty():
+ return
+ if WIN:
+ import msvcrt
+ while msvcrt.kbhit():
+ msvcrt.getwch()
+ else:
+ import termios
+ termios.tcflush(sys.stdin.fileno(), termios.TCIFLUSH)
+ except (ImportError, OSError, ValueError, AttributeError):
+ pass
+
+
def ask(question, choices, default, yes):
if yes:
return default
+ flush_typed_ahead()
while True:
try:
a = input(f"{question} [{default}]: ").strip()
@@ -357,15 +376,9 @@ def cpu_cores():
classes.append(raw[at + 9])
at += size
else:
- seen = {}
- for cpu in sorted(Path("/sys/devices/system/cpu").glob("cpu[0-9]*"), key=lambda p: int(p.name[3:])):
- cap = cpu / "cpu_capacity"
- pkg, core = cpu / "topology" / "physical_package_id", cpu / "topology" / "core_id"
- if not cap.exists():
- return None
- key = (pkg.read_text().strip(), core.read_text().strip()) if pkg.exists() and core.exists() else cpu.name
- seen.setdefault(key, int(cap.read_text().strip()))
- classes = list(seen.values())
+ classes = linux_core_classes()
+ if classes is None:
+ return None
except (OSError, ValueError, AttributeError):
return None
if not classes or max(classes) == min(classes):
@@ -374,6 +387,52 @@ def cpu_cores():
return p, len(classes) - p
+def linux_core_classes(sys_root: str = "/sys"):
+ """One class per physical core on Linux: 1 for a P-core, 0 for an E-core; None when the kernel does not say.
+ #798: Intel's hybrid PMU lists (cpu_core/cpus, cpu_atom/cpus) when they exist; else cpu_capacity, where an E-core is
+ one under 90% of the largest - Turbo Boost Max 3.0 gives the "favored" P-cores a slightly higher capacity (1024
+ against 1012), so "below the maximum" counted 2 of a Core Ultra 7 270K Plus's 8 P-cores."""
+ pmu = linux_hybrid_pmu(sys_root + "/devices")
+ seen = {}
+ for cpu in sorted((Path(sys_root) / "devices" / "system" / "cpu").glob("cpu[0-9]*"), key=lambda p: int(p.name[3:])):
+ cap = cpu / "cpu_capacity"
+ pkg, core = cpu / "topology" / "physical_package_id", cpu / "topology" / "core_id"
+ if not cap.exists() and pmu is None:
+ return None
+ key = (pkg.read_text(encoding="utf-8").strip(), core.read_text(encoding="utf-8").strip()) \
+ if pkg.exists() and core.exists() else cpu.name
+ if pmu is not None:
+ seen.setdefault(key, 0 if int(cpu.name[3:]) in pmu[1] else 1)
+ else:
+ seen.setdefault(key, int(cap.read_text(encoding="utf-8").strip()))
+ classes = list(seen.values())
+ if pmu is None:
+ top = max(classes, default=0)
+ classes = [1 if c * 10 >= top * 9 else 0 for c in classes]
+ return classes
+
+
+def cpulist(text: str) -> set:
+ """A kernel cpulist ("0-7,16") as a set of CPU numbers."""
+ out = set()
+ for part in text.strip().split(","):
+ lo, _, hi = part.strip().partition("-")
+ if lo.isdigit():
+ out.update(range(int(lo), int(hi or lo) + 1))
+ return out
+
+
+def linux_hybrid_pmu(root: str = "/sys/devices"):
+ """#798: (P-core CPUs, E-core CPUs) from Intel's hybrid PMU lists (cpu_core/cpus, cpu_atom/cpus), or None on a CPU
+ that has no such lists."""
+ try:
+ p = cpulist((Path(root) / "cpu_core" / "cpus").read_text(encoding="utf-8"))
+ e = cpulist((Path(root) / "cpu_atom" / "cpus").read_text(encoding="utf-8"))
+ except OSError:
+ return None
+ return (p, e) if p and e else None
+
+
def hybrid_pool_workers(cores) -> int | None:
"""#642 (Hardin22's measurement): on a hybrid CPU the expert pool runs best on the P-cores but the host loop's one
plus HALF of the E-cores - an E-core runs the expert kernels ~2.2x slower and each layer waits for its slowest
@@ -543,6 +602,13 @@ def gpus():
return found
+def gpu_drives_display(g) -> bool:
+ """#779: True when nvidia-smi says this card has a display attached (display_active Enabled): its desktop needs
+ VRAM too, and a full expert cache beside it has crashed laptops."""
+ text = out(["nvidia-smi", "-i", str(g.get("index", 0)), "--query-gpu=display_active", "--format=csv,noheader"])
+ return text.strip().lower() == "enabled"
+
+
GPU_PICK = None # --gpu N (issue #51); None: the card with the most VRAM
SPLIT_MIN_VRAM_GB = 8 # a card sharing a model holds the dense weights and its own
# prompt buffers too (docs/MULTI_GPU.md)
@@ -727,7 +793,7 @@ def engine_archs(toolkit=13):
"""The GPU generations the installed engine has code for: (archs, ptx), or None when there is none."""
info = engine_dir(toolkit) / "BUILD.json"
try:
- meta = json.loads(info.read_text())
+ meta = json.loads(info.read_text(encoding="utf-8"))
except (OSError, ValueError):
return None
return [int(x) for x in meta.get("archs", [])], bool(meta.get("ptx"))
@@ -736,7 +802,7 @@ def engine_archs(toolkit=13):
def engine_archs_hip():
"""The AMD architectures the installed HIP engine was compiled for ("gfx1201", ...), or None."""
try:
- meta = json.loads((ROOT / "engine" / "BUILD.json").read_text())
+ meta = json.loads((ROOT / "engine" / "BUILD.json").read_text(encoding="utf-8"))
except (OSError, ValueError):
return None
return [str(x) for x in meta.get("archs", [])] if meta.get("backend") == "hip" else None
@@ -841,20 +907,24 @@ def unsloth_split_need_gb(model="UD-Q4_K_XL") -> float:
return MODELS[model]["download_gb"] + UNSLOTH_RAM_LEFT_GB
-def split_budget(cfg: dict) -> bool:
+def split_budget(cfg: dict, yes: bool = False, explicit: bool = False) -> bool:
"""#498: a UD-Q4_K_XL config (its RAM budget, --resident-budget-gib) started on several GPUs. The engine refuses
the budget with a layer split (it exited with code 2), so the split runs without it - all the experts loaded into
- RAM at start - where the RAM holds the GGUFs and 24 GB more; else setup stops, before the config is saved. True
- when the config changed."""
+ RAM at start - where the RAM holds the GGUFs and 24 GB more; else setup says so and asks (#737: a recommendation,
+ not a wall - 128 GB ran it fine): a "no" stops, before the config is saved; `explicit` (--gpus) with --yes goes on.
+ True when the config changed."""
a = cfg.get("args", [])
if "--resident-budget-gib" not in a:
return False
model = budget_model(cfg)
need, ram = unsloth_split_need_gb(model), ram_gb()
if ram < need:
- fail(f"{model} cannot share its RAM budget across GPUs (the engine has no layer split with it), and without "
- f"the budget it needs ~{need:.0f} GB of RAM (its GGUF files and {UNSLOTH_RAM_LEFT_GB} GB more); this PC "
- f"has {ram:.0f} GB", "start it on one GPU: START-HERE.bat --gpu N (Linux: ./setup.sh --gpu N)")
+ confirm_risk(f"{model} cannot share its RAM budget across GPUs (the engine has no layer split with it), and "
+ f"without the budget it needs ~{need:.0f} GB of RAM (its GGUF files and {UNSLOTH_RAM_LEFT_GB} GB "
+ f"more); this PC has {ram:.0f} GB. The estimate is a worst case: a 128 GB PC ran it (#737), but "
+ "it may page or stall here.", explicit, yes,
+ f"{model} on several GPUs needs ~{need:.0f} GB of RAM; this PC has {ram:.0f} GB",
+ "start it on one GPU: START-HERE.bat --gpu N (Linux: ./setup.sh --gpu N)")
i = a.index("--resident-budget-gib")
del a[i:i + 2]
ok(f"{model} on several GPUs: no RAM budget (the engine has none with a layer split) - all its experts are "
@@ -1255,6 +1325,11 @@ def check_shards(shards):
if have < need:
fail(f"{s.name} is short: {have:,} of {need:,} bytes ({need - have:,} missing)",
"delete it and run setup again (or copy the whole file into --gguf-dir)")
+ if len(g.tensors) == 1 and have - need >= 64: # (a few bytes may be the last tensor's alignment padding)
+ # #657: a shard that holds one tensor (the PLE table) is exactly as long as that tensor; extra bytes are a
+ # damaged or wrong file that the engine would refuse at start ("PLE table size mismatch")
+ fail(f"{s.name} is longer than its tensor: {have:,} bytes, {need:,} expected ({have - need:,} extra)",
+ f"delete {s.name} and its .done mark and run setup again")
def get_llama_cpp():
@@ -1319,7 +1394,7 @@ def pip_install(packages, what):
requirements (#214) recorded bare names: those packages are kept as they are (nothing is reinstalled), and the
pinned dependencies it already has count as installed."""
stamp = Path(sys.prefix) / ".strata-pip.json"
- have = json.loads(stamp.read_text()) if stamp.exists() else []
+ have = json.loads(stamp.read_text(encoding="utf-8")) if stamp.exists() else []
bare = {p.lower() for p in have if req_name(p) == p.lower()}
need = [p for p in packages if p not in have and req_name(p) not in bare
and not (bare and "==" in p and _installed(req_name(p)))]
@@ -1473,7 +1548,8 @@ _WIN_AMD_DID = {0x744C: "gfx1100", 0x7448: "gfx1100", 0x745E: "gfx1100",
0x7480: "gfx1102", # RX 7600 / 7600 XT
0x7590: "gfx1200", # RX 9060 XT
0x7550: "gfx1201", 0x7551: "gfx1201", # RX 9070 / 9070 XT, AI PRO R9700
- 0x73BF: "gfx1030", 0x73AF: "gfx1030", 0x73A5: "gfx1030"} # RX 6800 / 6800 XT / 6900 XT / 6950 XT
+ 0x73BF: "gfx1030", 0x73AF: "gfx1030", 0x73A5: "gfx1030", # RX 6800 / 6800 XT / 6900 XT / 6950 XT
+ 0x73DF: "gfx1031"} # RX 6700 XT / 6750 XT / 6800M (#881)
_WIN_AMD_NAME = ((re.compile(r"\b9070\b|R9700", re.I), "gfx1201"),
(re.compile(r"\b9060\b", re.I), "gfx1200"),
(re.compile(r"RX\s*7900|W7900|W7800", re.I), "gfx1100"),
@@ -1586,7 +1662,7 @@ def hip_devices(probe: Path | None = None, text: str | None = None) -> list[dict
if text is None:
probe = probe or ROOT / "engine" / ("strata-device.exe" if WIN else "strata-device")
try:
- hip_engine = json.loads((probe.parent / "BUILD.json").read_text()).get("backend") == "hip"
+ hip_engine = json.loads((probe.parent / "BUILD.json").read_text(encoding="utf-8")).get("backend") == "hip"
except (OSError, ValueError):
hip_engine = False
if not probe.exists() or not hip_engine:
@@ -1627,7 +1703,7 @@ def hip_devices(probe: Path | None = None, text: str | None = None) -> list[dict
def hip_lib_dirs(eng: Path) -> list[Path]:
"""Where the ready-made Windows HIP engine's ROCm DLLs are (its BUILD.json "lib_dirs", relative to engine/)."""
try:
- rel = json.loads((eng / "BUILD.json").read_text()).get("lib_dirs") or []
+ rel = json.loads((eng / "BUILD.json").read_text(encoding="utf-8")).get("lib_dirs") or []
except (OSError, ValueError):
rel = []
return [eng / d for d in rel if (eng / d).is_dir()]
@@ -1694,7 +1770,7 @@ def get_prebuilt_hip(url_base, gpu, updating=False) -> Path | None:
info = eng / "BUILD.json"
if info.exists() and (eng / EXE).exists():
try:
- meta = json.loads(info.read_text())
+ meta = json.loads(info.read_text(encoding="utf-8"))
except ValueError:
meta = {}
ver = tuple(int(x) for x in str(meta.get("version", "0")).split(".")[:3] if x.isdigit())
@@ -1727,7 +1803,7 @@ def get_prebuilt_hip(url_base, gpu, updating=False) -> Path | None:
with zipfile.ZipFile(z) as f:
f.extractall(tmp)
try:
- meta = json.loads((tmp / "BUILD.json").read_text())
+ meta = json.loads((tmp / "BUILD.json").read_text(encoding="utf-8"))
except (OSError, ValueError):
meta = {}
ver = tuple(int(x) for x in str(meta.get("version", "0")).split(".")[:3] if x.isdigit())
@@ -1737,17 +1813,14 @@ def get_prebuilt_hip(url_base, gpu, updating=False) -> Path | None:
elif ver < WIN_HIP_MIN_ENGINE:
why = f"it is version {meta.get('version')}; this setup needs {'.'.join(map(str, WIN_HIP_MIN_ENGINE))}"
elif gpu["arch"] not in meta.get("archs", []):
- why = f"it is built for {', '.join(meta.get('archs', []))}; your GPU is {gpu['arch']}"
+ why = (f"it is built for {', '.join(meta.get('archs', []))}; your GPU is {gpu['arch']}. A card it has no code "
+ "for runs from a self-build of the engine: see docs/AMD_HIP.md (--build)") # #881
if why:
warn(f"the ready-made AMD engine at {base} cannot be used: {why}")
shutil.rmtree(tmp, ignore_errors=True)
drop_archive(z)
return None
- for p in tmp.iterdir():
- dst = eng / p.name
- if dst.exists():
- shutil.rmtree(dst) if dst.is_dir() else dst.unlink()
- p.replace(dst)
+ install_unpacked(tmp, eng)
shutil.rmtree(tmp, ignore_errors=True)
drop_archive(z)
hip_runtime_beside_exe(eng) # #468 #461
@@ -1759,7 +1832,7 @@ def get_prebuilt_hip(url_base, gpu, updating=False) -> Path | None:
def rocm_version(root):
"""(major, minor) of a ROCm install, from rocm-core's header; None when it has none."""
try:
- text = (Path(root) / "include" / "rocm-core" / "rocm_version.h").read_text()
+ text = (Path(root) / "include" / "rocm-core" / "rocm_version.h").read_text(encoding="utf-8")
return tuple(int(re.search(rf"#define\s+ROCM_VERSION_{k}\s+(\d+)", text).group(1)) for k in ("MAJOR", "MINOR"))
except (OSError, AttributeError, ValueError):
return None
@@ -1797,7 +1870,7 @@ def rocm_root(archs):
"wheels come per family", "install ROCm 7 system-wide, or use cards of one family (--gpu N for one card)")
index = indexes[0]
stamp = Path(sys.prefix) / ".strata-rocm.json"
- have = json.loads(stamp.read_text()) if stamp.exists() else {}
+ have = json.loads(stamp.read_text(encoding="utf-8")) if stamp.exists() else {}
if have.get("version") != ROCM_VERSION or have.get("index") != index:
say(f" Installing ROCm {ROCM_VERSION} for AMD GPUs into .venv (AMD's TheRock wheels, ~10 GB, no sudo) ...")
pip = [sys.executable, "-m", "pip", "install", "--quiet", "--disable-pip-version-check", "--index-url", index]
@@ -1827,7 +1900,7 @@ def hipblaslt_version(lib_dirs):
header of the ROCm whose libraries the engine loads; None when not found."""
for d in lib_dirs:
try:
- text = (Path(d).parent / "include" / "hipblaslt" / "hipblaslt-version.h").read_text()
+ text = (Path(d).parent / "include" / "hipblaslt" / "hipblaslt-version.h").read_text(encoding="utf-8")
v = [int(re.search(rf"#define\s+HIPBLASLT_VERSION_{k}\s+(\d+)", text).group(1))
for k in ("MAJOR", "MINOR", "PATCH")]
except (OSError, AttributeError, ValueError):
@@ -1843,7 +1916,7 @@ def hipblaslt_table(arch, lib_dirs, ver=None):
ver = ver or hipblaslt_version(lib_dirs)
table = ROOT / "tools" / "hip" / f"{arch}-hipblaslt-{ver}.txt"
if ver is not None and table.exists():
- head = table.read_text().split("\n", 2)[:2]
+ head = table.read_text(encoding="utf-8").split("\n", 2)[:2]
if f"STRATA_HIPBLASLT_TUNING_V1 {arch} {ver}" in (h.strip() for h in head):
ok(f"hipBLASLt tuning table: {table.name} (faster prompts)")
return table
@@ -1862,7 +1935,7 @@ def build_engine_hip(gpu, llama, vision="none") -> Path:
eng = ROOT / "engine"
eng.mkdir(exist_ok=True)
stamp = eng / "BUILD.json"
- meta = json.loads(stamp.read_text()) if stamp.exists() else {}
+ meta = json.loads(stamp.read_text(encoding="utf-8")) if stamp.exists() else {}
src, vsrc = source_hash(ENGINE_SOURCES), source_hash(VISION_SOURCES)
archs = sorted(set(gpu.get("archs") or [gpu["arch"]]))
has_archs = set(archs) <= set(meta.get("archs", []))
@@ -1925,7 +1998,8 @@ def build_vision_cpu(eng: Path, stamp: Path, meta: dict, llama, vsrc) -> Path:
if not ((eng / VEXE).exists() and meta.get("vision_src") == vsrc):
say(" Compiling the image encoder (for the CPU) ...")
cmake_build(ROOT / "tools" / "vision", ROOT / "build-vision", "strata-vision",
- [f"-DLLAMA_DIR={llama}", "-DSTRATA_VISION_CUDA=OFF"], None, "")
+ [f"-DLLAMA_DIR={llama}", "-DSTRATA_VISION_CUDA=OFF"], find_vcvars() if WIN else None,
+ "build-vision-cpu.bat" if WIN else "") # #881: MSVC's environment on Windows, as the CUDA path has
shutil.copy2(ROOT / "build-vision" / "bin" / VEXE, eng / VEXE)
stamp.write_text(json.dumps({**meta, "vision": "cpu", "vision_src": vsrc}, indent=1))
ok(f"engine: {eng / EXE}, image encoder (CPU): {eng / VEXE}")
@@ -1949,6 +2023,70 @@ def prebuilt_bases(url_base) -> list[str]:
return [PREBUILT_TAG_URL.format(version=source_version()), base]
+PREVIOUS_ENGINE = ".previous"
+
+
+def engine_version_of(folder: Path) -> str:
+ try:
+ return str(json.loads((folder / "BUILD.json").read_text(encoding="utf-8")).get("version", "?"))
+ except (OSError, ValueError):
+ return "?"
+
+
+def install_unpacked(tmp: Path, eng: Path) -> None:
+ """#670: the unpacked engine (tmp) replaces the installed one in `eng`, which is kept, one generation, in
+ eng/.previous (about 210 MiB): a new engine that turns out bad has a way back (`setup.py --rollback-engine`). The
+ swap is all or nothing: a failure part-way puts the old files back instead of leaving a mix of old and new."""
+ prev = eng / PREVIOUS_ENGINE
+ new = list(tmp.iterdir())
+ old = [eng / p.name for p in new if p.name != PREVIOUS_ENGINE and (eng / p.name).exists()]
+ shutil.rmtree(prev, ignore_errors=True)
+ moved, placed = [], []
+ try:
+ if old:
+ prev.mkdir()
+ for dst in old:
+ shutil.move(str(dst), str(prev / dst.name))
+ moved.append(dst)
+ for p in new:
+ p.replace(eng / p.name)
+ placed.append(eng / p.name)
+ except OSError:
+ for dst in placed: # back to the old engine, whole
+ shutil.rmtree(dst) if dst.is_dir() else dst.unlink(missing_ok=True)
+ for dst in moved:
+ shutil.move(str(prev / dst.name), str(dst))
+ shutil.rmtree(prev, ignore_errors=True)
+ raise
+ if moved:
+ ok(f"the engine it replaced ({engine_version_of(prev)}) is kept in {prev}; "
+ "setup.py --rollback-engine puts it back")
+
+
+def rollback_engine(toolkit=13) -> int:
+ """#670: `setup.py --rollback-engine`: the engine kept in engine/.previous becomes the installed one again, and the
+ one it replaces is kept in its place (run it again to go forward)."""
+ eng = engine_dir(toolkit)
+ prev = eng / PREVIOUS_ENGINE
+ if not prev.is_dir() or not any(prev.iterdir()):
+ warn(f"no earlier engine is kept in {prev} (one is kept when an update replaces the engine)")
+ return 1
+ swap = eng / ".swap"
+ shutil.rmtree(swap, ignore_errors=True)
+ swap.mkdir()
+ was = engine_version_of(eng)
+ for p in list(prev.iterdir()): # what the earlier engine has: the current copy goes aside
+ cur = eng / p.name
+ if cur.exists():
+ shutil.move(str(cur), str(swap / p.name))
+ shutil.move(str(p), str(cur))
+ for p in list(swap.iterdir()):
+ shutil.move(str(p), str(prev / p.name))
+ shutil.rmtree(swap, ignore_errors=True)
+ ok(f"engine {engine_version_of(eng)} is installed again (it replaced {was}, which is kept in {prev})")
+ return 0
+
+
def get_prebuilt(url_base, gpu, vision, updating=False, toolkit=13) -> Path | None:
"""The ready-made engine in engine/ (kept between runs), or None when there is none for this PC.
updating: called to replace an installed engine, which starts instead when this fails (no compile).
@@ -1956,8 +2094,8 @@ def get_prebuilt(url_base, gpu, vision, updating=False, toolkit=13) -> Path | No
eng = engine_dir(toolkit)
asset = CUDA12_ASSET if int(toolkit) == 12 else PREBUILT_ASSET
info = eng / "BUILD.json"
- if info.exists() and (eng / EXE).exists() and json.loads(info.read_text()).get("backend") != "hip":
- meta = json.loads(info.read_text())
+ if info.exists() and (eng / EXE).exists() and json.loads(info.read_text(encoding="utf-8")).get("backend") != "hip":
+ meta = json.loads(info.read_text(encoding="utf-8"))
ver = tuple(int(x) for x in str(meta.get("version", "0")).split(".")[:3] if x.isdigit())
if meta.get("source") == "local": # compiled here: build_engine checks its source and cards
return None
@@ -2001,7 +2139,7 @@ def get_prebuilt(url_base, gpu, vision, updating=False, toolkit=13) -> Path | No
except zipfile.BadZipFile: # not a zip, or a damaged one: a kept .done mark would make
drop_archive(z) # every later run fail on it instead of downloading it again
raise
- meta = json.loads((tmp / "BUILD.json").read_text())
+ meta = json.loads((tmp / "BUILD.json").read_text(encoding="utf-8"))
if tuple(int(x) for x in str(meta.get("version", "0")).split(".")[:3] if x.isdigit()) < MIN_ENGINE:
need = ".".join(map(str, MIN_ENGINE))
if updating: # these files are newer than the published release (#58)
@@ -2022,11 +2160,7 @@ def get_prebuilt(url_base, gpu, vision, updating=False, toolkit=13) -> Path | No
shutil.rmtree(tmp, ignore_errors=True)
drop_archive(z)
return None
- for p in tmp.iterdir():
- dst = eng / p.name
- if dst.exists():
- shutil.rmtree(dst) if dst.is_dir() else dst.unlink()
- p.replace(dst)
+ install_unpacked(tmp, eng)
shutil.rmtree(tmp, ignore_errors=True)
drop_archive(z)
if not (eng / EXE).exists():
@@ -2054,7 +2188,7 @@ def update_installed_engine(url_base, toolkit=None) -> None:
info = eng / "BUILD.json"
if not info.exists() or not (eng / EXE).exists():
return
- meta_text = info.read_text()
+ meta_text = info.read_text(encoding="utf-8")
meta = json.loads(meta_text)
if meta.get("backend") == "hip" and WIN: # AMD on Windows: the ready-made HIP engine, when older
ver = tuple(int(x) for x in str(meta.get("version", "0")).split(".")[:3] if x.isdigit())
@@ -2225,6 +2359,9 @@ def cmake_build(src, bdir, target, defs, vcvars, bat_name):
# 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:
+ if vcvars is None: # #881: the build needs MSVC's environment
+ fail("the Visual Studio C++ build tools were not found",
+ "install them (Visual Studio 2019 or 2022, workload 'Desktop development with C++') and run setup again")
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 0\r\n'
@@ -2292,7 +2429,7 @@ def build_engine(gpu, vision, yes, llama, toolkit=None) -> Path:
eng = engine_dir(toolkit)
eng.mkdir(exist_ok=True)
stamp = eng / "BUILD.json"
- meta = json.loads(stamp.read_text()) if stamp.exists() else {}
+ meta = json.loads(stamp.read_text(encoding="utf-8")) if stamp.exists() else {}
want_vision = vision != "none"
local = meta.get("source") == "local"
src, vsrc = source_hash(ENGINE_SOURCES), source_hash(VISION_SOURCES)
@@ -2461,8 +2598,17 @@ def unsloth_together(a, model, ram, gpu, chosen) -> bool:
names = " + ".join(gpu_name(g) for g in chosen)
need = unsloth_split_need_gb(model)
if ram < need:
- warn(f"{model} runs on one GPU here: on several it has no RAM budget and needs ~{need:.0f} GB of RAM (its GGUF "
- f"files and {UNSLOTH_RAM_LEFT_GB} GB more), this PC has {ram:.0f} - using {gpu_name(gpu)} only")
+ note = (f"{model} on several GPUs has no RAM budget and needs ~{need:.0f} GB of RAM (its GGUF files and "
+ f"{UNSLOTH_RAM_LEFT_GB} GB more), this PC has {ram:.0f}; the estimate is a worst case (#737: a 128 GB PC "
+ "ran it)")
+ if a.gpus and a.resident_budget_gib is None: # #737: asked for by name: a question, not a refusal
+ confirm_risk(note + ", but it may page or stall here", True, a.yes,
+ f"{model} on several GPUs needs ~{need:.0f} GB of RAM; this PC has {ram:.0f} GB",
+ f"start it on one GPU: --gpus {gpu['index']}", " Use them together anyway?")
+ ok(f"{model} on {names}, as you chose (--gpus), with {ram:.0f} GB of RAM")
+ return True
+ warn(f"{note} - using {gpu_name(gpu)} only (--gpus " + ",".join(str(g["index"]) for g in chosen) +
+ " uses them together anyway)")
return False
if a.resident_budget_gib is not None:
warn(f"--resident-budget-gib has no layer split: {model} runs on one GPU with it - using {gpu_name(gpu)} only "
@@ -2741,6 +2887,35 @@ def carry_over(old: dict, cfg: dict) -> list[str]:
if isinstance(mm, str) and Path(mm).name != Path(str(nv.get("mmproj"))).name and Path(mm).is_file():
nv["mmproj"] = mm
kept.append("vision mmproj")
+ kept += carry_profile_args(old, cfg)
+ return kept
+
+
+def flag_value(args: list, flag: str):
+ """The value after `flag` in an argument list, or None."""
+ for i, a in enumerate(args[:-1]):
+ if a == flag:
+ return str(args[i + 1])
+ return None
+
+
+def carry_profile_args(old: dict, cfg: dict) -> list[str]:
+ """#775: a learned expert profile the user wired in by hand (--expert-profile <their file>, --expert-profile-save
+ <file>) survives a setup run: setup writes the shipped profile, which a measured one of their own beats (2.5-3.1x at
+ 256K on a 16 GB card in the report). Their --expert-profile replaces the shipped one only when it is another file
+ that still exists; --expert-profile-save is kept when the new config has none. The names kept are returned."""
+ oargs, nargs = old.get("args"), cfg.get("args")
+ if not isinstance(oargs, list) or not isinstance(nargs, list):
+ return []
+ kept = []
+ mine, shipped = flag_value(oargs, "--expert-profile"), flag_value(nargs, "--expert-profile")
+ if mine and shipped and Path(mine).name != Path(shipped).name and Path(mine).is_file():
+ nargs[nargs.index("--expert-profile") + 1] = mine
+ kept.append("args --expert-profile")
+ save = flag_value(oargs, "--expert-profile-save")
+ if save and flag_value(nargs, "--expert-profile-save") is None:
+ nargs += ["--expert-profile-save", save]
+ kept.append("args --expert-profile-save")
return kept
@@ -2886,7 +3061,7 @@ def engine_version(exe: Path) -> tuple:
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())
+ meta = json.loads((Path(exe).parent / "BUILD.json").read_text(encoding="utf-8"))
except (OSError, ValueError):
meta = {}
v = str(meta.get("version") or "")
@@ -3129,7 +3304,7 @@ def start(cfg_path: Path, port: int | None, gpu: int | list | None = None, open_
"set it up for these cards: ./setup.sh --setup --backend hip --gpus " + ",".join(map(str, gpu)))
cfg["gpu"], cfg["gpus_asked"] = gpu, True
cfg["layer_split"] = layer_split or cfg.get("layer_split") or "auto"
- split_budget(cfg) # #498: before it is saved (it stops when the RAM is short)
+ split_budget(cfg, yes, True) # #498: before it is saved (asks when the RAM is short, #737)
write_config(cfg_path, cfg)
gpu = None
elif gpu is not None:
@@ -3157,7 +3332,7 @@ def start(cfg_path: Path, port: int | None, gpu: int | list | None = None, open_
check_gpus(gpu, found, yes=yes, named=True)
cfg["gpu"], cfg["gpus_asked"] = gpu, True
cfg["layer_split"] = layer_split or cfg.get("layer_split") or "auto"
- split_budget(cfg) # #498: before it is saved (it stops when the RAM is short)
+ split_budget(cfg, yes, True) # #498: before it is saved (asks when the RAM is short, #737)
recommend_remote_expert_opt(cfg)
write_config(cfg_path, cfg)
gpu = None
@@ -3358,6 +3533,7 @@ def parallel_note(asked: int | None, vram_gbs, arena_gb: float, ctx: int, kv: st
return lines
+DISPLAY_RESERVE_MIB = 1500 # #779: the tip for an NVIDIA card that also drives a display
DESKTOP_RESERVE_MIB = 3072 # #560 #516: what kept a KDE/Wayland desktop alive beside a full expert cache
@@ -3422,7 +3598,7 @@ def ensure_engine_for(cards, cfg_path: Path, cfg: dict, yes: bool) -> dict:
return cfg
eng = engine_dir(tk)
info = eng / "BUILD.json"
- meta = json.loads(info.read_text())
+ meta = json.loads(info.read_text(encoding="utf-8"))
say()
say(" The installed engine has no code for " + ", ".join(f"{g['name']} (sm_{g['arch']})" for g in missing) +
": it is compiled for " + ("these cards" if len(cards) > 1 else "it") + " now.")
@@ -3430,7 +3606,7 @@ def ensure_engine_for(cards, cfg_path: Path, cfg: dict, yes: bool) -> dict:
archs = sorted({int(x) for x in meta.get("archs", [])} | {int(g["arch"]) for g in cards})
vision = meta.get("vision") or ("gpu" if (eng / VEXE).exists() else "none")
build_engine({**main, "archs": archs}, vision, yes, get_llama_cpp(), toolkit=tk)
- dirs = json.loads(info.read_text()).get("cuda_dirs") or []
+ dirs = json.loads(info.read_text(encoding="utf-8")).get("cuda_dirs") or []
cfg["lib_dirs"] = dirs + [d for d in cfg.get("lib_dirs") or [] if d not in dirs]
write_config(cfg_path, cfg)
return cfg
@@ -3440,7 +3616,7 @@ def get_cuda12_engine(url_base, gpu, vision, yes, build=False) -> Path:
"""The experimental CUDA 12 engine for these cards (gpu["archs"]): the ready-made one (Windows) with NVIDIA's
CUDA 12 libraries, or compiled here with a CUDA 12.x toolkit (Linux, --build, or no ready-made one)."""
eng = None if build else get_prebuilt(url_base, gpu, vision, toolkit=12)
- if eng is not None and json.loads((eng / "BUILD.json").read_text()).get("source") != "local":
+ if eng is not None and json.loads((eng / "BUILD.json").read_text(encoding="utf-8")).get("source") != "local":
pip_cuda_libs(12)
if vision != "none" and not (eng / VEXE).exists():
eng = None
@@ -3449,7 +3625,7 @@ def get_cuda12_engine(url_base, gpu, vision, yes, build=False) -> Path:
def engine_lib_dirs(eng: Path, toolkit=13) -> list:
"""The library folders a CUDA engine loads from: its own (a compiled one: the toolkit's), else pip's wheels."""
- meta = json.loads((eng / "BUILD.json").read_text())
+ meta = json.loads((eng / "BUILD.json").read_text(encoding="utf-8"))
return meta.get("lib_dirs") or meta.get("cuda_dirs") or cuda_lib_dirs(toolkit)
@@ -3608,6 +3784,8 @@ def main() -> int:
ap.add_argument("--update", action="store_true",
help="update the installed engine, Python packages and model settings as a start would, without "
"starting the model (UPDATE.bat / update.sh run it after a git pull)")
+ ap.add_argument("--rollback-engine", action="store_true",
+ help="put back the engine an update replaced (kept in engine/.previous), and keep the current one there")
ap.add_argument("--build", action="store_true", help="compile the engine instead of using the ready-made one")
ap.add_argument("--cuda", choices=["12", "13", "auto"], default=os.environ.get("STRATA_CUDA") or None,
help="NVIDIA: the CUDA toolkit of this model's engine. auto (default): CUDA 13, the ready-made "
@@ -3667,6 +3845,9 @@ def main() -> int:
if a.models_dir is None:
a.models_dir = str(data / "models")
+ if a.rollback_engine: # #670
+ return rollback_engine(12 if a.cuda == "12" else 13)
+
# ---- 0. already installed: just start it
have = installed_configs()
if a.update: # #475: UPDATE.bat / update.sh - never starts the model
@@ -4141,16 +4322,16 @@ def main() -> int:
else:
eng = None if a.build or hip else get_prebuilt(a.prebuilt, gpu, vision, **({"toolkit": 12} if cuda_tk == 12
else {}))
- if eng is not None and not hip and json.loads((eng / "BUILD.json").read_text()).get("source") != "local":
+ if eng is not None and not hip and json.loads((eng / "BUILD.json").read_text(encoding="utf-8")).get("source") != "local":
pip_cuda_libs(cuda_tk)
if vision != "none" and not (eng / VEXE).exists():
warn("the ready-made engine has no image encoder: compiling it")
eng = None
else:
- vision = prebuilt_vision(json.loads((eng / "BUILD.json").read_text()), gpu, vision)
+ vision = prebuilt_vision(json.loads((eng / "BUILD.json").read_text(encoding="utf-8")), gpu, vision)
if eng is None:
eng = build_engine_hip(gpu, llama, vision) if hip else build_engine(gpu, vision, a.yes, llama, toolkit=cuda_tk)
- meta = json.loads((eng / "BUILD.json").read_text())
+ meta = json.loads((eng / "BUILD.json").read_text(encoding="utf-8"))
if hip and WIN: # the ready-made engine's rocm/bin, first on the engine's PATH
lib_dirs = [str(d) for d in hip_lib_dirs(eng)]
else:
@@ -4354,6 +4535,9 @@ def main() -> int:
elif hip and a.vram_reserve_mib is None and linux_desktop():
for line in desktop_reserve_note(): # #560 #516: a recommendation: nothing changes
say(" " + line)
+ if not hip and not multi and a.vram_reserve_mib is None and gpu_drives_display(gpu):
+ say(" tip: this card drives a display. If the PC freezes or the screen goes black once the model is loaded "
+ f"(#779), keep more VRAM free: run setup again with --vram-reserve-mib {DISPLAY_RESERVE_MIB}") # a tip only
if esp is not None:
# the package's profile, with llama.cpp's flags (the engine takes the same ones)
args += ["--control-vector-scaled", f"{esp}:1.0", "--control-vector-layer-range", "4", "44",
diff --git a/tools/hip/package_windows.py b/tools/hip/package_windows.py
index e62988154..12738f198 100644
--- a/tools/hip/package_windows.py
+++ b/tools/hip/package_windows.py
@@ -142,8 +142,8 @@ def main() -> int:
(lic / "NOTICE.txt").write_text(NOTICE.format(rocm=a.rocm_version, dlls="\n".join(" " + d for d in shipped)),
encoding="utf-8")
- version = re.search(r"project\(strata VERSION ([\d.]+)", (ROOT / "CMakeLists.txt").read_text()).group(1)
- hl = (a.rocm / "include" / "hipblaslt" / "hipblaslt-version.h").read_text()
+ version = re.search(r"project\(strata VERSION ([\d.]+)", (ROOT / "CMakeLists.txt").read_text(encoding="utf-8")).group(1)
+ hl = (a.rocm / "include" / "hipblaslt" / "hipblaslt-version.h").read_text(encoding="utf-8")
hlv = [int(re.search(rf"#define\s+HIPBLASLT_VERSION_{k}\s+(\d+)", hl).group(1)) for k in ("MAJOR", "MINOR", "PATCH")]
meta = {"source": "prebuilt", "backend": "hip", "platform": "windows-x64", "version": version, "archs": archs,
"rocm": a.rocm_version, "hipblaslt_version": hlv[0] * 100000 + hlv[1] * 100 + hlv[2], "vision": "none",
diff --git a/tools/test_setup_amd.py b/tools/test_setup_amd.py
index 61a122375..94cc0c390 100644
--- a/tools/test_setup_amd.py
+++ b/tools/test_setup_amd.py
@@ -230,6 +230,22 @@ class WindowsDetection(unittest.TestCase):
self.assertEqual(g[0]["driver"], "32.0.21013.1000")
self.assertEqual([setup.amd_problem(x) is None for x in g], [True, False, True])
+ def test_rx_6800m_is_gfx1031(self):
+ """#881: PCI 73DF (RX 6700 XT / 6750 XT / 6800M) is gfx1031, not an unknown id."""
+ self.assertEqual(setup.win_amd_arch(0x73DF, "AMD Radeon RX 6800M"), "gfx1031")
+ self.assertIsNone(setup.amd_problem({"arch": "gfx1031"}))
+
+ def test_cpu_vision_build_uses_the_visual_studio_environment(self):
+ """#881: build_vision_cpu on Windows hands cmake_build the vcvars file and a real .bat name (it passed None and
+ an empty name before)."""
+ calls = []
+ with tempfile.TemporaryDirectory() as d, mock.patch.object(setup, "WIN", True), mock.patch.object(setup, "find_vcvars", return_value=Path("C:/vs/vcvars64.bat")), mock.patch.object(setup, "cmake_build", lambda *a: calls.append(a)), mock.patch.object(setup, "ROOT", Path(d)), mock.patch.object(setup.shutil, "copy2"):
+ eng = Path(d) / "engine"
+ eng.mkdir()
+ setup.build_vision_cpu(eng, eng / "BUILD.json", {}, Path("llama"), "src1")
+ self.assertEqual(calls[0][4], Path("C:/vs/vcvars64.bat"))
+ self.assertTrue(calls[0][5].endswith(".bat"))
+
def test_registry_alone(self):
"""No WMI answer: the registry's own list (which can hold a removed card)."""
g = setup.amd_gpus_windows([], self.REGISTRY)
diff --git a/tools/test_setup_config.py b/tools/test_setup_config.py
index cd297645d..d031891c6 100644
--- a/tools/test_setup_config.py
+++ b/tools/test_setup_config.py
@@ -114,6 +114,39 @@ class CarryOver(unittest.TestCase):
self.assertEqual(json.loads(p.read_text(encoding="utf-8")), {"exe": "e", "args": []})
self.assertEqual((Path(d) / "strata-x.json.bak").read_text(encoding="utf-8"), "{cut off")
+ def test_display_tip_and_typed_ahead_flush(self):
+ # #779: the tip only when nvidia-smi says the card drives a display; #841: typed-ahead keys are dropped on a tty
+ with unittest.mock.patch.object(setup, "out", lambda cmd: "Enabled\n"):
+ self.assertTrue(setup.gpu_drives_display({"index": 0}))
+ with unittest.mock.patch.object(setup, "out", lambda cmd: "Disabled\n"), unittest.mock.patch.object(setup, "out") as o:
+ o.return_value = "Disabled\n"
+ self.assertFalse(setup.gpu_drives_display({"index": 1}))
+ self.assertIn("1", o.call_args[0][0])
+ with unittest.mock.patch.object(setup, "out", lambda cmd: ""):
+ self.assertFalse(setup.gpu_drives_display({"index": 0}))
+ with unittest.mock.patch.object(setup.sys, "stdin") as stdin:
+ stdin.isatty.return_value = False
+ setup.flush_typed_ahead() # a pipe: nothing touched
+ stdin.fileno.assert_not_called()
+
+ def test_a_learned_expert_profile_is_carried(self):
+ # #775: --expert-profile / --expert-profile-save added by hand survive a setup run
+ with tempfile.TemporaryDirectory() as d:
+ mine = Path(d) / "learned.bin"
+ mine.write_bytes(b"x")
+ old = {"args": ["--kv", "int8", "--expert-profile", str(mine), "--expert-profile-save", str(mine)]}
+ new = {"args": ["--kv", "int8", "--expert-profile", "/data/expert-profile.bin", "--expert-cache", "auto"]}
+ self.assertEqual(setup.carry_over(old, new), ["args --expert-profile", "args --expert-profile-save"])
+ self.assertEqual(setup.flag_value(new["args"], "--expert-profile"), str(mine))
+ self.assertEqual(setup.flag_value(new["args"], "--expert-profile-save"), str(mine))
+ # the shipped profile named again, or a file that is gone: setup's own stays
+ gone = {"args": ["--expert-profile", str(Path(d) / "gone.bin")]}
+ new = {"args": ["--expert-profile", "/data/expert-profile.bin"]}
+ self.assertEqual(setup.carry_over(gone, new), [])
+ self.assertEqual(new["args"], ["--expert-profile", "/data/expert-profile.bin"])
+ same = {"args": ["--expert-profile", "/old/expert-profile.bin"]}
+ self.assertEqual(setup.carry_over(same, new), [])
+
def test_a_missing_mmproj_is_not_carried(self):
old = {"vision": {"mmproj": "/nowhere/mmproj-Q8_0.gguf", "gpu": True}}
new = {"vision": {"mmproj": "/data/mmproj-BF16.gguf", "gpu": True}}
diff --git a/tools/test_setup_engine_keep.py b/tools/test_setup_engine_keep.py
new file mode 100644
index 000000000..6667837c1
--- /dev/null
+++ b/tools/test_setup_engine_keep.py
@@ -0,0 +1,94 @@
+"""#670: an engine update keeps the engine it replaces in engine/.previous (one generation), the swap is all or
+nothing, and `setup.py --rollback-engine` puts the kept one back.
+
+ python -m unittest tools.test_setup_engine_keep
+"""
+from __future__ import annotations
+
+import json
+import sys
+import tempfile
+import unittest
+from pathlib import Path
+from unittest import mock
+
+ROOT = Path(__file__).resolve().parents[1]
+sys.path.insert(0, str(ROOT))
+sys.path.insert(0, str(ROOT / "tools"))
+import setup # noqa: E402
+
+
+def engine(folder: Path, version: str, exe: bytes) -> None:
+ folder.mkdir(parents=True, exist_ok=True)
+ (folder / "BUILD.json").write_text(json.dumps({"version": version}), encoding="utf-8")
+ (folder / "strata.exe").write_bytes(exe)
+ (folder / "lib").mkdir(exist_ok=True)
+ (folder / "lib" / "x.dll").write_bytes(exe)
+
+
+class KeepPrevious(unittest.TestCase):
+ def setUp(self):
+ tmp = tempfile.TemporaryDirectory()
+ self.addCleanup(tmp.cleanup)
+ self.root = Path(tmp.name)
+ self.eng = self.root / "engine"
+ self.new = self.eng / "_unpack"
+
+ def test_the_replaced_engine_is_kept_and_rolled_back(self):
+ engine(self.eng, "0.1.39", b"old")
+ engine(self.new, "0.1.40", b"new")
+ setup.install_unpacked(self.new, self.eng)
+ self.assertEqual((self.eng / "strata.exe").read_bytes(), b"new")
+ self.assertEqual(setup.engine_version_of(self.eng), "0.1.40")
+ prev = self.eng / ".previous"
+ self.assertEqual((prev / "strata.exe").read_bytes(), b"old")
+ self.assertEqual((prev / "lib" / "x.dll").read_bytes(), b"old")
+ self.assertEqual(setup.engine_version_of(prev), "0.1.39")
+ with mock.patch.object(setup, "ROOT", self.root):
+ self.assertEqual(setup.rollback_engine(), 0)
+ self.assertEqual((self.eng / "strata.exe").read_bytes(), b"old")
+ self.assertEqual(setup.engine_version_of(self.eng), "0.1.39")
+ self.assertEqual(setup.engine_version_of(prev), "0.1.40") # forward again
+ self.assertEqual(setup.rollback_engine(), 0)
+ self.assertEqual((self.eng / "strata.exe").read_bytes(), b"new")
+
+ def test_one_generation_only(self):
+ engine(self.eng, "0.1.38", b"a")
+ for version, exe in (("0.1.39", b"b"), ("0.1.40", b"c")):
+ engine(self.new, version, exe)
+ setup.install_unpacked(self.new, self.eng)
+ for p in self.new.iterdir(): # (get_prebuilt removes what is left of the unpack)
+ p.unlink() if p.is_file() else None
+ self.assertEqual(setup.engine_version_of(self.eng / ".previous"), "0.1.39")
+
+ def test_a_first_install_keeps_nothing(self):
+ self.eng.mkdir()
+ engine(self.new, "0.1.40", b"new")
+ setup.install_unpacked(self.new, self.eng)
+ self.assertEqual((self.eng / "strata.exe").read_bytes(), b"new")
+ self.assertFalse((self.eng / ".previous").exists())
+ with mock.patch.object(setup, "ROOT", self.root):
+ self.assertEqual(setup.rollback_engine(), 1)
+
+ def test_a_failure_part_way_leaves_the_old_engine_whole(self):
+ engine(self.eng, "0.1.39", b"old")
+ engine(self.new, "0.1.40", b"new")
+ real = Path.replace
+ calls = []
+
+ def flaky(self_, target):
+ calls.append(self_.name)
+ if len(calls) == 2:
+ raise PermissionError("held open")
+ return real(self_, target)
+
+ with mock.patch.object(Path, "replace", flaky), self.assertRaises(OSError):
+ setup.install_unpacked(self.new, self.eng)
+ self.assertEqual((self.eng / "strata.exe").read_bytes(), b"old")
+ self.assertEqual((self.eng / "lib" / "x.dll").read_bytes(), b"old")
+ self.assertEqual(setup.engine_version_of(self.eng), "0.1.39")
+ self.assertFalse((self.eng / ".previous").exists())
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tools/test_setup_hybrid.py b/tools/test_setup_hybrid.py
index a5b9a164f..3a5882eef 100644
--- a/tools/test_setup_hybrid.py
+++ b/tools/test_setup_hybrid.py
@@ -6,6 +6,7 @@ half of the E-cores) and writes nothing new anywhere else; a calibration's measu
from __future__ import annotations
import sys
+import tempfile
import unittest
from pathlib import Path
from unittest import mock
@@ -17,6 +18,57 @@ import setup # noqa: E402
from test_setup_golden import PROFILES, install # noqa: E402
+def fake_sys(root: Path, caps: list, pmu=None, smt=False) -> None:
+ """A /sys tree: cpu N's capacity (and, with smt, a sibling CPU per core), and the hybrid PMU lists when given."""
+ cpu_dir = root / "devices" / "system" / "cpu"
+ n = 0
+ for core, cap in enumerate(caps):
+ for _ in range(2 if smt and cap >= 1000 else 1):
+ d = cpu_dir / f"cpu{n}"
+ (d / "topology").mkdir(parents=True)
+ (d / "topology" / "physical_package_id").write_text("0")
+ (d / "topology" / "core_id").write_text(str(core))
+ (d / "cpu_capacity").write_text(str(cap))
+ n += 1
+ if pmu:
+ for name, lst in zip(("cpu_core", "cpu_atom"), pmu):
+ (root / "devices" / name).mkdir(parents=True)
+ (root / "devices" / name / "cpus").write_text(lst + "\n")
+
+
+class LinuxCores(unittest.TestCase):
+ """#798: Turbo Boost Max 3.0's "favored" P-cores have a slightly higher cpu_capacity."""
+
+ def classes(self, caps, **kw):
+ with tempfile.TemporaryDirectory() as d:
+ fake_sys(Path(d), caps, **kw)
+ cl = setup.linux_core_classes(d)
+ if not cl or max(cl) == min(cl):
+ return None
+ p = sum(1 for c in cl if c == max(cl))
+ return p, len(cl) - p
+
+ def test_favored_cores_do_not_hide_the_p_cores(self):
+ caps = [1024, 1012, 1024, 1012, 1012, 1012, 1012, 1012] + [768] * 16 # Core Ultra 7 270K Plus
+ self.assertEqual(self.classes(caps), (8, 16)) # by capacity: >= 90% of the largest
+ self.assertEqual(self.classes(caps, pmu=("0-7", "8-23")), (8, 16)) # by the PMU lists
+ self.assertEqual(setup.hybrid_pool_workers((8, 16)), 15)
+
+ def test_plain_capacities_and_plain_cpus(self):
+ self.assertEqual(self.classes([1024] * 8 + [512] * 8), (8, 8))
+ self.assertIsNone(self.classes([1024] * 8)) # no E-cores
+ self.assertIsNone(self.classes([1024, 1012, 1012, 1012])) # favored cores only: not hybrid
+
+ def test_smt_siblings_count_once(self):
+ self.assertEqual(self.classes([1024, 1012] * 4 + [768] * 8, smt=True, pmu=("0-15", "16-23")), (8, 8))
+
+ def test_no_capacity_files_and_no_pmu_says_nothing(self):
+ with tempfile.TemporaryDirectory() as d:
+ fake_sys(Path(d), [1024, 768])
+ (Path(d) / "devices/system/cpu/cpu1/cpu_capacity").unlink()
+ self.assertIsNone(setup.linux_core_classes(d))
+
+
class HybridWorkers(unittest.TestCase):
def test_rule(self):
self.assertIsNone(setup.hybrid_pool_workers(None))
diff --git a/tools/test_setup_unsloth.py b/tools/test_setup_unsloth.py
index 9b9d271d9..faf8bc501 100644
--- a/tools/test_setup_unsloth.py
+++ b/tools/test_setup_unsloth.py
@@ -329,10 +329,9 @@ class Main(Base):
self.assertIsNone(cfg)
def test_one_gpu_and_no_images(self):
- code, out, cfg = self.main(["--context", "8192", "--gpus", "0,1", "--vision", "yes", "--low-ram", "on"],
- n_gpus=2)
+ code, out, cfg = self.main(["--context", "8192", "--vision", "yes", "--low-ram", "on"], n_gpus=2)
self.assertEqual(code, 0, out)
- self.assertIn("runs on one GPU", out)
+ self.assertIn("(NVIDIA GeForce RTX 5070, 12 GB) only (--gpus 0,1 uses them together anyway)", out)
self.assertIn("images are not available with UD-Q4_K_XL", out)
self.assertIn("--low-ram on does not apply", out)
self.assertNotIn("layer_split", cfg)
@@ -352,14 +351,21 @@ class Main(Base):
self.assertIn("OS file cache", out)
self.assertNotIn("RAM budget: ", out)
- def test_split_refused_without_the_ram(self):
+ def test_split_without_the_ram_is_a_warning_not_a_wall(self):
+ # #737: below the worst-case estimate (a 128 GB PC ran it) the split is still the user's call
need = setup.unsloth_split_need_gb()
self.assertAlmostEqual(need, setup.MODELS[M]["download_gb"] + setup.UNSLOTH_RAM_LEFT_GB)
- code, out, cfg = self.main(["--context", "8192", "--gpus", "0,1"], n_gpus=2, ram=127.8)
+ code, out, cfg = self.main(["--context", "8192"], n_gpus=2, ram=127.8) # no --gpus: stays on one GPU
self.assertEqual(code, 0, out)
- self.assertIn("runs on one GPU here", out)
+ self.assertIn("uses them together anyway", out)
self.assertNotIn("layer_split", cfg)
self.assertEqual(cfg["args"][cfg["args"].index("--resident-budget-gib") + 1], "71") # all of them
+ code, out, cfg = self.main(["--context", "8192", "--gpus", "0,1"], n_gpus=2, ram=127.8) # asked for by name
+ self.assertEqual(code, 0, out)
+ self.assertIn("worst case", out)
+ self.assertIn("as you chose (--gpus), with 128 GB of RAM", out)
+ self.assertEqual(cfg["gpu"], [0, 1])
+ self.assertNotIn("--resident-budget-gib", cfg["args"])
def test_an_explicit_budget_keeps_one_gpu(self):
code, out, cfg = self.main(["--context", "8192", "--gpus", "0,1", "--resident-budget-gib", "60"], n_gpus=2,
@@ -550,14 +556,21 @@ class LayerSplit(unittest.TestCase):
self.assertEqual(cfg["args"], ["--pack", "p", "--kv", "int8", "--remote-expert-opt"]) # 0.1.39b: #578
self.assertIn("no RAM budget", out)
- def test_start_with_gpus_refused_without_the_ram(self):
- code, out, cfg, started = self.start(63.7)
+ def test_start_with_gpus_without_the_ram_is_asked(self):
+ # #737: a warning and a question (default no), not a wall; --gpus with --yes is the consent
+ code, out, cfg, started = self.start(63.7, offered="n") # asked: "n" stops, nothing saved
self.assertEqual(code, 1)
self.assertFalse(started)
self.assertIn("cannot share its RAM budget across GPUs", out)
self.assertIn("--gpu N", out)
- self.assertEqual(cfg["gpu"], 0) # nothing saved
+ self.assertEqual(cfg["gpu"], 0)
self.assertIn("--resident-budget-gib", cfg["args"])
+ code, out, cfg, started = self.start(63.7) # --gpus + --yes: goes on, no budget
+ self.assertIsNone(code, out)
+ self.assertTrue(started)
+ self.assertIn("cannot share its RAM budget across GPUs", out)
+ self.assertEqual(cfg["gpu"], [0, 1])
+ self.assertNotIn("--resident-budget-gib", cfg["args"])
def test_offered_at_a_start(self):
code, out, cfg, _ = self.start(165.0, gpu=None, offered="") # Enter: one GPU (the default here)
diff --git a/tools/test_shards.py b/tools/test_shards.py
index 2afe2dc5c..28e94df29 100644
--- a/tools/test_shards.py
+++ b/tools/test_shards.py
@@ -72,6 +72,23 @@ class ShardCheck(unittest.TestCase):
self.assertIn(f"{whole - 40:,} of {whole:,} bytes", msg)
self.assertIn("40 missing", msg)
+ def test_single_tensor_shard_with_extra_bytes_is_refused(self):
+ # #657: the PLE table's shard is exactly as long as its one tensor; 612 extra bytes are a damaged file
+ s = self.dir / "m-00002-of-00002.gguf"
+ whole = write_gguf(s, names=("per_layer_token_embd.weight",))
+ S.check_shards([s])
+ s.write_bytes(s.read_bytes() + bytes(612))
+ with self.assertRaises(Stop) as cm:
+ S.check_shards([s])
+ msg = str(cm.exception)
+ self.assertIn("m-00002-of-00002.gguf is longer than its tensor", msg)
+ self.assertIn(f"{whole + 612:,} bytes, {whole:,} expected", msg)
+ # a shard with several tensors is not held to the rule
+ t = self.dir / "m-00001-of-00002.gguf"
+ write_gguf(t)
+ t.write_bytes(t.read_bytes() + bytes(612))
+ S.check_shards([t])
+
def test_truncated_header_is_refused(self):
s = self.dir / "m-00001-of-00002.gguf"
write_gguf(s)