mirror of
https://github.com/hasso5703/alzeo.git
synced 2026-12-06 12:54:19 +01:00
296 lines
19 KiB
Python
296 lines
19 KiB
Python
#!/usr/bin/env python3
|
|
"""switch-model.sh <target> --fetch: the target's checkpoint, fetched as a switch fetches it
|
|
(the cache first, resumable, its room checked), and nothing else: no unit rewritten, no boot
|
|
moved, no engine touched, no record written. Library's "Download" and a lane installed
|
|
without its weights (the on-demand install) get their weights this way before a Load.
|
|
|
|
The script runs as written on a copy whose unit paths point at this test's files, with a
|
|
fake docker whose download succeeds and a fake systemctl that records every call.
|
|
"""
|
|
import pathlib
|
|
import re
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
REPO = pathlib.Path(__file__).resolve().parents[1]
|
|
SWITCH = (REPO / "switch-model.sh").read_text()
|
|
|
|
FAKE_DOCKER = """#!/bin/sh
|
|
case "$1" in
|
|
image) exit 0 ;;
|
|
run) printf '%s\\n' "$*" >> "$DOCKER_LOG"; exit 0 ;; # the download succeeds
|
|
esac
|
|
exit 0
|
|
"""
|
|
|
|
|
|
class Box:
|
|
def __init__(self):
|
|
self.t = pathlib.Path(tempfile.mkdtemp(prefix="switch-fetch-"))
|
|
self.home = self.t / "home"
|
|
(self.home / ".config/qwen38").mkdir(parents=True)
|
|
units = self.t / "units"
|
|
units.mkdir()
|
|
self.unit = units / "qwen38-sglang.service"
|
|
self.unit.write_text(
|
|
"[Service]\nExecStart=/bin/bash -c 'exec docker run --rm --name qwen38-sglang \\\n"
|
|
f" -v {self.home}/.cache/huggingface:/root/.cache/huggingface \\\n"
|
|
" lmsysorg/sglang@sha256:" + "a" * 64 + " \\\n"
|
|
" python3 -m sglang.launch_server --model-path RadixArk/Qwen3.8-27B-NVFP4 \\\n"
|
|
" --host 127.0.0.1 --port 30000'\n")
|
|
repo = self.t / "repo"
|
|
repo.mkdir()
|
|
(repo / "install.sh").write_text((REPO / "install.sh").read_text())
|
|
text = SWITCH
|
|
for unit in ("qwen38-sglang.service", "qwen38-flash.service", "qwen38-nemotron.service",
|
|
"qwen38-nemotron-super.service", "qwen38-flash-local.service", "qwen38-ornith-122b.service",
|
|
"qwen38-flash-unsloth.service"):
|
|
old = f'"/etc/systemd/system/{unit}"'
|
|
assert old in text, f"switch-model.sh no longer names {unit} that way"
|
|
text = text.replace(old, f'"{units}/{unit}"')
|
|
(repo / "switch-model.sh").write_text(text)
|
|
# install-flash-local.sh --room runs before its fetch, with what it sources and its table's tool
|
|
# and the Unsloth GGUF lane's installer, whose --fetch the switch runs, with what it sources and its fetcher
|
|
for f in ("lanes.sh", "ckpt-fetch.py", "install-nemotron.sh", "install-nemotron-super.sh", "install-flash-local.sh",
|
|
"install-ornith-122b.sh", "vllm-lane.sh", "ple-table.py", "install-flash-unsloth.sh", "llamacpp-lane.sh",
|
|
"gguf-fetch.py"):
|
|
(repo / f).write_text((REPO / f).read_text())
|
|
# the Nemotron lane's unit, as install-nemotron.sh writes it: its image, its cache, the
|
|
# checkpoint and the memory share on lines of their own
|
|
pins = dict(line.split("=", 1) for line in (REPO / "install-nemotron.sh").read_text().splitlines()
|
|
if line.startswith("NEMOTRON_BOLT_") and "=" in line)
|
|
self.nemotron_pins = {k: v.strip('"') for k, v in pins.items()}
|
|
self.nemotron_unit = units / "qwen38-nemotron.service"
|
|
self.nemotron_unit.write_text(
|
|
"[Service]\nExecStart=/usr/bin/docker run --rm --name qwen38-nemotron \\\n"
|
|
f" -v {self.home}/.cache/huggingface:/root/.cache/huggingface \\\n"
|
|
" qwen38-nemotron:test \\\n"
|
|
f" {self.nemotron_pins['NEMOTRON_BOLT_REPO']} --revision {self.nemotron_pins['NEMOTRON_BOLT_REV']} \\\n"
|
|
f" --gpu-memory-utilization {self.nemotron_pins['NEMOTRON_BOLT_GMU']} \\\n"
|
|
" --port 30000\n")
|
|
# the Nemotron Super lane's unit, the same way (the image is the Bolt lane's)
|
|
spins = dict(line.split("=", 1) for line in (REPO / "install-nemotron-super.sh").read_text().splitlines()
|
|
if line.startswith("NEMOTRON_SUPER_") and "=" in line)
|
|
self.super_pins = {k: v.strip('"') for k, v in spins.items()}
|
|
self.super_unit = units / "qwen38-nemotron-super.service"
|
|
self.super_unit.write_text(
|
|
"[Service]\nExecStart=/usr/bin/docker run --rm --name qwen38-nemotron-super \\\n"
|
|
f" -v {self.home}/.cache/huggingface:/root/.cache/huggingface \\\n"
|
|
" qwen38-nemotron:test \\\n"
|
|
f" {self.super_pins['NEMOTRON_SUPER_REPO']} --revision {self.super_pins['NEMOTRON_SUPER_REV']} \\\n"
|
|
f" --gpu-memory-utilization {self.super_pins['NEMOTRON_SUPER_GMU']} \\\n"
|
|
" --port 30000\n")
|
|
# the Qwen3.8 Flash-Next FP4-Local lane's unit: its image by digest, its cache, its checkpoint
|
|
fpins = dict(line.split("=", 1) for line in (REPO / "install-flash-local.sh").read_text().splitlines()
|
|
if line.startswith("FLASH_LOCAL_") and "=" in line)
|
|
self.flocal_pins = {k: v.strip('"') for k, v in fpins.items()}
|
|
self.flocal_unit = units / "qwen38-flash-local.service"
|
|
self.flocal_unit.write_text(
|
|
"[Service]\nExecStart=/bin/bash -c 'exec /usr/bin/docker run --rm --name qwen38-flash-local \\\n"
|
|
f" -v {self.home}/.cache/huggingface:/root/.cache/huggingface \\\n"
|
|
f" {self.flocal_pins['FLASH_LOCAL_IMAGE']} \\\n"
|
|
f" vllm serve {self.flocal_pins['FLASH_LOCAL_REPO']} --revision {self.flocal_pins['FLASH_LOCAL_REV']} \\\n"
|
|
" --gpu-memory-utilization 0.8 --port 30000'\n")
|
|
# the Ornith lane's unit: the Qwen3.8 Flash-Next FP4-Local lane's image by digest, its cache, its checkpoint
|
|
opins = dict(line.split("=", 1) for line in (REPO / "install-ornith-122b.sh").read_text().splitlines()
|
|
if line.startswith("ORNITH_122B_") and "=" in line)
|
|
self.ornith_pins = {k: v.strip('"') for k, v in opins.items()}
|
|
self.ornith_unit = units / "qwen38-ornith-122b.service"
|
|
self.ornith_unit.write_text(
|
|
"[Service]\nExecStart=/bin/bash -c 'exec /usr/bin/docker run --rm --name qwen38-ornith-122b \\\n"
|
|
f" -v {self.home}/.cache/huggingface:/root/.cache/huggingface \\\n"
|
|
f" {self.flocal_pins['FLASH_LOCAL_IMAGE']} \\\n"
|
|
f" vllm serve {self.ornith_pins['ORNITH_122B_REPO']} --revision {self.ornith_pins['ORNITH_122B_REV']} \\\n"
|
|
" --gpu-memory-utilization 0.8 --port 30000'\n")
|
|
# the Unsloth GGUF lane's unit: llama.cpp's image by digest, its cache read-only, its GGUF by its snapshot
|
|
upins = dict(line.split("=", 1) for line in (REPO / "install-flash-unsloth.sh").read_text().splitlines()
|
|
if line.startswith("FLASH_UNSLOTH_") and "=" in line)
|
|
self.unsloth_pins = {k: v.strip('"') for k, v in upins.items()}
|
|
self.unsloth_unit = units / "qwen38-flash-unsloth.service"
|
|
self.unsloth_unit.write_text(
|
|
"[Service]\nExecStart=/bin/bash -c 'exec /usr/bin/docker run --rm --name qwen38-flash-unsloth \\\n"
|
|
f" -v {self.home}/.cache/huggingface:/root/.cache/huggingface:ro \\\n"
|
|
f" {self.unsloth_pins['FLASH_UNSLOTH_IMAGE']} \\\n"
|
|
" -m /root/.cache/huggingface/hub/models--unsloth--Qwen3.8-Flash-Next-GGUF/snapshots/"
|
|
f"{self.unsloth_pins['FLASH_UNSLOTH_REV']}/x.gguf -c 262144 --port 30000'\n")
|
|
(repo / "switch-model.sh").chmod(0o755)
|
|
self.bin = self.t / "bin"
|
|
self.bin.mkdir()
|
|
(self.bin / "docker").write_text(FAKE_DOCKER)
|
|
(self.bin / "systemctl").write_text('#!/bin/sh\nprintf "%s\\n" "$*" >> "$SYSTEMCTL_LOG"\nexit 1\n')
|
|
(self.bin / "sudo").write_text('#!/bin/sh\nprintf "sudo %s\\n" "$*" >> "$SYSTEMCTL_LOG"\nexit 1\n')
|
|
# a disk with room for the checkpoint, whatever this machine's has: the real df
|
|
# answered here, and a 29 GB disk got the 45 GB refusal (found in a cloud review,
|
|
# 2026-10-18); the room check has its own tests, in tests/test_switch_room.py
|
|
(self.bin / "df").write_text("#!/bin/sh\necho Avail; echo 500G\n")
|
|
# the GGUF fetcher answers as if its files were whole, and says how it was run; any other Python is the real one
|
|
(self.bin / "python3").write_text('#!/bin/sh\nfor a in "$@"; do case "$a" in */gguf-fetch.py) '
|
|
'printf "%s\\n" "$*" >> "$GGUF_LOG"; exit 0 ;; esac; done\n'
|
|
'exec /usr/bin/python3 "$@"\n')
|
|
for n in ("docker", "systemctl", "sudo", "df", "python3"):
|
|
(self.bin / n).chmod(0o755)
|
|
self.gguf_log = self.t / "gguf.log"
|
|
self.docker_log = self.t / "docker.log"
|
|
self.sysctl_log = self.t / "systemctl.log"
|
|
self.script = repo / "switch-model.sh"
|
|
|
|
def run(self, *args):
|
|
e = {"PATH": f"{self.bin}:/usr/bin:/bin", "HOME": str(self.home),
|
|
"DOCKER_LOG": str(self.docker_log), "SYSTEMCTL_LOG": str(self.sysctl_log), "GGUF_LOG": str(self.gguf_log)}
|
|
r = subprocess.run([str(self.script), *args], capture_output=True, text=True, env=e, timeout=60)
|
|
runs = self.docker_log.read_text().splitlines() if self.docker_log.exists() else []
|
|
calls = self.sysctl_log.read_text().splitlines() if self.sysctl_log.exists() else []
|
|
return r.returncode, r.stdout + r.stderr, runs, calls
|
|
|
|
|
|
class FetchOnly(unittest.TestCase):
|
|
def test_a_text_target_is_fetched_and_nothing_else_changes(self):
|
|
box = Box()
|
|
unit_before = box.unit.read_bytes()
|
|
rc, out, runs, calls = box.run("stock", "--fetch")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn("nothing else was changed", out)
|
|
self.assertEqual(len(runs), 1, runs)
|
|
self.assertIn("/opt/qwen38/ckpt-fetch.py RadixArk/Qwen3.8-27B-NVFP4 ", runs[0])
|
|
self.assertEqual(box.unit.read_bytes(), unit_before)
|
|
self.assertFalse([c for c in calls if re.search(r"\b(enable|disable|daemon-reload|restart|start|stop)\b", c)], calls)
|
|
self.assertFalse((box.home / ".config/qwen38/text-lane").exists(), "--fetch wrote the text-lane record")
|
|
|
|
def test_the_nemotron_lane_fetches_its_drafter_before_it_stops(self):
|
|
# Library's Download of the Nemotron target stopped after the model, its drafter left for
|
|
# the Load (found in review, 2026-10-15): the lane loads both, so both are fetched first
|
|
box = Box()
|
|
unit_before = box.nemotron_unit.read_bytes()
|
|
rc, out, runs, calls = box.run("nemotron", "--fetch")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn("with its drafter, is in the cache; nothing else was changed", out)
|
|
pins = box.nemotron_pins
|
|
self.assertEqual(len(runs), 2, runs)
|
|
self.assertIn(f"/opt/qwen38/ckpt-fetch.py {pins['NEMOTRON_BOLT_REPO']} {pins['NEMOTRON_BOLT_REV']} ", runs[0])
|
|
self.assertIn(f"/opt/qwen38/ckpt-fetch.py {pins['NEMOTRON_BOLT_DRAFT_REPO']} {pins['NEMOTRON_BOLT_DRAFT_REV']} ", runs[1])
|
|
self.assertIn(f"--need-gb {pins['NEMOTRON_BOLT_GB']} ", runs[0])
|
|
self.assertIn("--need-gb 1 ", runs[1])
|
|
self.assertEqual(box.nemotron_unit.read_bytes(), unit_before)
|
|
self.assertFalse([c for c in calls if re.search(r"\b(enable|disable|daemon-reload|restart|start|stop)\b", c)], calls)
|
|
self.assertFalse((box.home / ".config/qwen38/text-lane").exists(), "--fetch wrote the text-lane record")
|
|
|
|
def test_the_nemotron_super_lane_fetches_its_drafter_before_it_stops(self):
|
|
box = Box()
|
|
unit_before = box.super_unit.read_bytes()
|
|
rc, out, runs, calls = box.run("nemotron-super", "--fetch")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn("with its drafter, is in the cache; nothing else was changed", out)
|
|
pins = box.super_pins
|
|
self.assertEqual(len(runs), 2, runs)
|
|
self.assertIn(f"/opt/qwen38/ckpt-fetch.py {pins['NEMOTRON_SUPER_REPO']} {pins['NEMOTRON_SUPER_REV']} ", runs[0])
|
|
self.assertIn(f"/opt/qwen38/ckpt-fetch.py {pins['NEMOTRON_SUPER_DRAFT_REPO']} {pins['NEMOTRON_SUPER_DRAFT_REV']} ", runs[1])
|
|
self.assertIn(f"--need-gb {pins['NEMOTRON_SUPER_GB']} ", runs[0])
|
|
self.assertIn(" qwen38-nemotron:test ", runs[0], "in the image the two Nemotron lanes run")
|
|
self.assertEqual(box.super_unit.read_bytes(), unit_before)
|
|
self.assertFalse([c for c in calls if re.search(r"\b(enable|disable|daemon-reload|restart|start|stop)\b", c)], calls)
|
|
|
|
def test_the_flash_local_lane_fetches_in_its_own_image_and_stops(self):
|
|
box = Box()
|
|
unit_before = box.flocal_unit.read_bytes()
|
|
rc, out, runs, calls = box.run("flash-local", "--fetch")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn("is in the cache; nothing else was changed", out)
|
|
pins = box.flocal_pins
|
|
self.assertEqual(len(runs), 1, runs)
|
|
self.assertIn(f"/opt/qwen38/ckpt-fetch.py {pins['FLASH_LOCAL_REPO']} {pins['FLASH_LOCAL_REV']} ", runs[0])
|
|
self.assertIn(f"--need-gb {pins['FLASH_LOCAL_GB']} ", runs[0])
|
|
self.assertIn(f" {pins['FLASH_LOCAL_IMAGE']} ", runs[0], "in the image the lane runs, pulled by digest")
|
|
self.assertEqual(box.flocal_unit.read_bytes(), unit_before)
|
|
self.assertFalse([c for c in calls if re.search(r"\b(enable|disable|daemon-reload|restart|start|stop)\b", c)], calls)
|
|
|
|
def test_the_ornith_lane_fetches_in_the_shared_image_and_stops(self):
|
|
box = Box()
|
|
unit_before = box.ornith_unit.read_bytes()
|
|
rc, out, runs, calls = box.run("ornith-122b", "--fetch")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn("is in the cache; nothing else was changed", out)
|
|
pins = box.ornith_pins
|
|
self.assertEqual(len(runs), 1, runs)
|
|
self.assertIn(f"/opt/qwen38/ckpt-fetch.py {pins['ORNITH_122B_REPO']} {pins['ORNITH_122B_REV']} ", runs[0])
|
|
self.assertIn(f"--need-gb {pins['ORNITH_122B_GB']} ", runs[0])
|
|
self.assertIn(f" {box.flocal_pins['FLASH_LOCAL_IMAGE']} ", runs[0], "in the Qwen3.8 Flash-Next FP4-Local lane's image, by digest")
|
|
self.assertEqual(box.ornith_unit.read_bytes(), unit_before)
|
|
self.assertFalse([c for c in calls if re.search(r"\b(enable|disable|daemon-reload|restart|start|stop)\b", c)], calls)
|
|
|
|
def test_the_unsloth_lane_fetches_through_its_installer_and_stops(self):
|
|
# its GGUF files on the host, by gguf-fetch.py (the image has no Python): the model's four parts, then the draft,
|
|
# into the cache its unit mounts; no container, no unit rewritten (v1.48)
|
|
box = Box()
|
|
unit_before = box.unsloth_unit.read_bytes()
|
|
rc, out, runs, calls = box.run("flash-unsloth", "--fetch")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn("is in the cache; nothing else was changed", out)
|
|
pins = box.unsloth_pins
|
|
fetches = box.gguf_log.read_text().splitlines()
|
|
self.assertEqual(len(fetches), 2, fetches)
|
|
hf = f"{box.home}/.cache/huggingface"
|
|
parts = " ".join(pins[f"FLASH_UNSLOTH_PART{i}"] for i in range(1, 5))
|
|
self.assertTrue(fetches[0].endswith(f"gguf-fetch.py {hf} {pins['FLASH_UNSLOTH_REPO']} {pins['FLASH_UNSLOTH_REV']} {parts}"),
|
|
fetches[0])
|
|
self.assertTrue(fetches[1].endswith(f"gguf-fetch.py {hf} {pins['FLASH_UNSLOTH_DRAFT_REPO']} "
|
|
f"{pins['FLASH_UNSLOTH_DRAFT_REV']} {pins['FLASH_UNSLOTH_DRAFT']}"), fetches[1])
|
|
self.assertEqual(runs, [], "no container")
|
|
self.assertEqual(box.unsloth_unit.read_bytes(), unit_before)
|
|
self.assertFalse([c for c in calls if re.search(r"\b(enable|disable|daemon-reload|restart|start|stop)\b", c)], calls)
|
|
self.assertFalse((box.home / ".config/qwen38/text-lane").exists(), "--fetch wrote the text-lane record")
|
|
|
|
def test_the_unsloth_lanes_second_target_fetches_its_own_parts(self):
|
|
# flash-unsloth-iq3 (v1.49): UD-IQ3_XXS's four parts, the fourth the very file of UD-Q4_K_XL's, then the draft,
|
|
# whatever target the unit in place serves; nothing else changes
|
|
box = Box()
|
|
unit_before = box.unsloth_unit.read_bytes()
|
|
rc, out, runs, calls = box.run("flash-unsloth-iq3", "--fetch")
|
|
self.assertEqual(rc, 0, out)
|
|
self.assertIn("is in the cache; nothing else was changed", out)
|
|
pins = box.unsloth_pins
|
|
fetches = box.gguf_log.read_text().splitlines()
|
|
self.assertEqual(len(fetches), 2, fetches)
|
|
parts = " ".join(pins[f"FLASH_UNSLOTH_IQ3_PART{i}"] for i in range(1, 5))
|
|
self.assertTrue(fetches[0].endswith(f"gguf-fetch.py {box.home}/.cache/huggingface {pins['FLASH_UNSLOTH_REPO']} "
|
|
f"{pins['FLASH_UNSLOTH_REV']} {parts}"), fetches[0])
|
|
self.assertTrue(fetches[1].endswith(f" {pins['FLASH_UNSLOTH_DRAFT']}"), fetches[1])
|
|
self.assertEqual((runs, box.unsloth_unit.read_bytes()), ([], unit_before))
|
|
self.assertFalse([c for c in calls if re.search(r"\b(enable|disable|daemon-reload|restart|start|stop)\b", c)], calls)
|
|
|
|
def test_without_it_the_switch_goes_on_past_the_download(self):
|
|
rc, out, runs, _ = Box().run("stock")
|
|
self.assertEqual(len(runs), 1, out)
|
|
self.assertNotIn("nothing else was changed", out)
|
|
|
|
def test_an_unknown_option_is_refused_before_anything_runs(self):
|
|
rc, out, runs, calls = Box().run("stock", "--fetchh")
|
|
self.assertNotEqual(rc, 0)
|
|
self.assertIn("unknown option", out)
|
|
self.assertEqual((runs, calls), ([], []))
|
|
|
|
def test_every_lane_stops_right_after_its_own_download(self):
|
|
# the nine fetch calls of the switch: the seven branches that fetch (Kolibri, Nemotron, Flash FP4-Local, Ornith,
|
|
# image, video, the text lanes) and MiniMax-H3-Image's two (its checkpoint, then its text encoder), that pair
|
|
# ending with ONE stop (v1.50); each call is followed at once by the --fetch stop, before anything it writes;
|
|
# the Nemotron lane's loop over its model and its drafter ends first, with nothing else in it
|
|
calls = [m.start() for m in re.finditer(r"ckpt-fetch\.py\"? \"\$", SWITCH)]
|
|
self.assertEqual(len(calls), 9)
|
|
for at in calls:
|
|
rest = SWITCH[at:]
|
|
die = rest.index("|| die ")
|
|
after = rest[rest.index("\n", die) + 1:].splitlines()
|
|
while after and re.fullmatch(r"\s*(NEM_ASK=1|done|#.*)", after[0]):
|
|
after.pop(0)
|
|
if after and after[0].lstrip().startswith('HF_HOME="${H3_HF:-$HF_CACHE}"'):
|
|
# the checkpoint's fetch is followed by its encoder's own call: the stop comes after that second one
|
|
rest2 = "\n".join(after)
|
|
after = rest2[rest2.index("\n", rest2.index("|| die ")) + 1:].splitlines()
|
|
while after and re.fullmatch(r"\s*(#.*|done)", after[0]):
|
|
after.pop(0)
|
|
self.assertTrue(after and after[0].lstrip().startswith("fetched "), after[:3])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|