Files
alzeo/tests/test_switch_room.py

214 lines
12 KiB
Python

#!/usr/bin/env python3
"""A switch checks for room before it downloads, and the cockpit gives it the time to.
switch-model.sh can download a whole checkpoint (22 to 136 GB), and a cockpit button can
start it: it checked no free space, blamed HF_TOKEN for any failed download, and the
cockpit's switch job timed out at 30 min, when a flash download alone takes about 23 at
the reference box's 89 MB/s (found in review, 2026-09-24). The switch's own lines run
here against a fake cache and a df stub.
"""
import pathlib
import re
import subprocess
import tempfile
import unittest
REPO = pathlib.Path(__file__).resolve().parents[1]
TEXT = (REPO / "switch-model.sh").read_text()
START = TEXT.index('DL_NEED_GB="$(ckpt_room_gb ')
BLOCK = TEXT[START:TEXT.index("DL_TOKEN_ARGS=()", START)]
# the room checks read free space through one function, defined once before every
# branch runs; every replay carries it in front of the block it exercises
_lines = TEXT.splitlines(keepends=True)
_at = next(i for i, ln in enumerate(_lines) if ln.startswith("dl_free_gb(){"))
DLFREE = "".join(_lines[_at:_at + 2])
def run(lane, free_gb, cached_gib=0.0, whole=False):
d = pathlib.Path(tempfile.mkdtemp(prefix="sw-room-"))
blobs = d / "hf/hub/models--org--target/blobs"
blobs.mkdir(parents=True)
if cached_gib:
with open(blobs / "b", "wb") as f:
f.truncate(int(cached_gib * 1024 ** 3))
if whole: # the target's revision whole in the cache
(d / "hf/hub/models--org--target/snapshots/abc123").mkdir(parents=True)
(d / "hf/hub/models--org--target/snapshots/abc123/model.safetensors").write_text("w")
(d / "df").write_text(f"#!/bin/sh\necho Avail; echo {free_gb}G\n")
(d / "df").chmod(0o755)
script = ('set -euo pipefail\ndie(){ echo "DIE: $*"; exit 1; }\n'
f'HF_CACHE="{d}/hf"; TARGET_REPO=org/target; TARGET_REV=abc123; TARGET_LANE={lane}\n. "{REPO}/lanes.sh"\n'
+ DLFREE + BLOCK + "echo ROOM-OK\n")
r = subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=30,
env={"PATH": f"{d}:/usr/bin:/bin"})
return r.stdout + r.stderr
class ASwitchChecksForRoom(unittest.TestCase):
def test_a_flash_switch_without_room_stops_before_anything(self):
out = run("flash", free_gb=50)
self.assertIn("DIE: not enough room", out)
self.assertNotIn("ROOM-OK", out)
def test_what_is_cached_comes_off_the_need(self):
self.assertIn("ROOM-OK", run("flash", free_gb=60, cached_gib=126))
def test_a_checkpoint_already_whole_asks_for_nothing_but_what_its_patches_write(self):
# 1, as install.sh asks; 10 until v1.59.1, and 180 less what the blobs hold before that (54 for a
# whole flash checkpoint, found in review, 2026-10-15): with 2 GiB left on the reference box's
# cache disk, no Load of a lane already there could pass (2026-11-30)
for lane in ("flash", "27b"):
self.assertIn("ROOM-OK", run(lane, 1, whole=True))
self.assertIn("DIE: not enough room", run(lane, 0, whole=True))
def test_a_27b_switch_with_room_goes_on(self):
self.assertIn("ROOM-OK", run("27b", free_gb=100))
VID_START = TEXT.index('VID_WANT_GB="$(grep')
VID_END = TEXT.index('nothing was changed). Free some space first, or point HF_HOME at a bigger disk."', VID_START)
VID_BLOCK = TEXT[VID_START:TEXT.index("\n", VID_END) + 1]
def run_video(free_gb, cached_gib=0.0, caches_there=False, home_disk_gb=None, repo_dir=REPO, whole=False):
"""home_disk_gb: $HOME on another disk than the cache, with that free (df and stat stubs); repo_dir:
where the switch reads install-video.sh; whole: the fl2va partition is whole in the cache."""
d = pathlib.Path(tempfile.mkdtemp(prefix="sw-room-"))
blobs = d / "hf/hub/models--MiniMaxAI--MiniMax-H3/blobs"
blobs.mkdir(parents=True)
if whole:
snap = d / "hf/hub/models--MiniMaxAI--MiniMax-H3/snapshots/abc123"
(snap / "FL2VA/transformer").mkdir(parents=True)
(snap / "model_index.json").write_text("{}")
(snap / "FL2VA/transformer/config.json").write_text("{}")
(snap / "FL2VA/transformer/model.safetensors").write_text("w")
if cached_gib:
with open(blobs / "b", "wb") as f:
f.truncate(int(cached_gib * 1024 ** 3))
# the two caches the runtime writes at its first start, already written (lanes.sh)
if caches_there:
for sub, gib in (("sglang/diffusion/host_spill", 12), ("sgl_diffusion/decode_dtype_store", 5)):
(d / "home/.cache" / sub).mkdir(parents=True)
with open(d / "home/.cache" / sub / "w.bin", "wb") as f:
f.truncate(gib * 1024 ** 3)
(d / "df").write_text(f"#!/bin/sh\necho Avail; echo {free_gb}G\n")
if home_disk_gb is not None:
(d / "home").mkdir(exist_ok=True)
(d / "df").write_text('#!/bin/bash\necho Avail\ncase "${@: -1}" in ' + f'"{d}/home"*) echo {home_disk_gb}G ;; *) echo {free_gb}G ;; esac\n')
(d / "stat").write_text('#!/bin/bash\ncase "${@: -1}" in ' + f'"{d}/home"*) echo 2 ;; *) echo 1 ;; esac\n')
(d / "stat").chmod(0o755)
(d / "df").chmod(0o755)
script = ('set -euo pipefail\ndie(){ echo "DIE: $*"; exit 1; }\n'
f'REPO_DIR="{repo_dir}"; HF_CACHE="{d}/hf"; HOME="{d}/home"; VID_MODEL=MiniMaxAI/MiniMax-H3; '
f'VID_MODEL_DIR="{d}/hf/hub/models--MiniMaxAI--MiniMax-H3"; VID_REV=abc123; VID_VARIANT=fl2va\n. "{REPO}/lanes.sh"\n'
+ DLFREE + VID_BLOCK + 'echo "NEED=$VID_WANT_GB"; echo ROOM-OK\n')
r = subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=30,
env={"PATH": f"{d}:/usr/bin:/bin"})
return r.stdout + r.stderr
H3_START = TEXT.index('H3_HAVE_B="$(')
H3_END = TEXT.index('nothing was changed). Free some space first, or point HF_HOME at a bigger disk."', H3_START)
H3_BLOCK = TEXT[H3_START:TEXT.index("\n", H3_END) + 1]
class TheH3ImageSwitchChecksForRoomToo(unittest.TestCase):
def test_weights_already_whole_ask_for_nothing_but_their_small_files(self):
# 10 until v1.59.1, with nothing to fetch (2026-11-30)
for whole, encoder, free, ok in ((True, True, 2, True), (True, False, 2, False), (False, True, 2, False)):
with self.subTest(whole=whole, encoder=encoder):
d = pathlib.Path(tempfile.mkdtemp(prefix="sw-room-"))
hub = d / "hf/hub"
snap = hub / "models--MiniMaxAI--MiniMax-H3-Image/snapshots/abc123"
(snap / "transformer").mkdir(parents=True)
for f in ("model_index.json", "transformer/config.json"):
(snap / f).write_text("{}")
if whole:
(snap / "transformer/model.safetensors").write_text("w")
if encoder:
enc = hub / "models--MiniMaxAI--MiniMax-H3/snapshots/enc1/text_encoder"
enc.mkdir(parents=True)
(enc / "model.safetensors.index.json").write_text('{"weight_map": {"a": "1.safetensors"}}')
(enc / "1.safetensors").write_text("w")
(d / "df").write_text(f"#!/bin/sh\necho Avail; echo {free}G\n")
(d / "df").chmod(0o755)
script = ('set -euo pipefail\ndie(){ echo "DIE: $*"; exit 1; }\n'
f'HF_CACHE="{d}/hf"; H3_HF=""; H3_MODEL=MiniMaxAI/MiniMax-H3-Image; H3_REV=abc123; WEIGHTS_GB=64; ENCODER_GB=63\n'
f'H3_MODEL_DIR="{hub}/models--MiniMaxAI--MiniMax-H3-Image"\n'
f'IMAGE_H3_ENCODER_PIN=MiniMaxAI/MiniMax-H3; IMAGE_H3_ENCODER_PIN_REV=enc1\n. "{REPO}/lanes.sh"\n'
+ DLFREE + H3_BLOCK + "echo ROOM-OK\n")
r = subprocess.run(["bash", "-c", script], capture_output=True, text=True, timeout=30,
env={"PATH": f"{d}:/usr/bin:/bin"})
self.assertIn("ROOM-OK" if ok else "DIE: not enough room", r.stdout + r.stderr)
class TheVideoSwitchChecksForRoomToo(unittest.TestCase):
"""145 GB of fl2va weights behind a button, under a serving engine: the video
branch skipped the room check the text branch had learned (reviews, 2026-09-28).
The need is read from install-video.sh, so the two files drift together."""
def test_a_video_switch_without_room_stops_before_anything(self):
# this box's real numbers of 2026-09-28: 90 GB free, empty cache, 145 needed
out = run_video(free_gb=90)
self.assertIn("DIE: not enough room", out)
self.assertNotIn("ROOM-OK", out)
def test_the_complete_cache_of_a_real_box_answers_without_a_die(self):
self.assertIn("ROOM-OK", run_video(free_gb=90, cached_gib=135))
def test_a_partition_already_whole_asks_for_nothing_but_its_small_files(self):
# it asked 10 of a cache whose disk had 2 left, with nothing to fetch (2026-11-30)
self.assertIn("ROOM-OK", run_video(free_gb=2, caches_there=True, whole=True))
self.assertIn("DIE: not enough room", run_video(free_gb=2, caches_there=True))
def test_a_video_switch_with_room_goes_on(self):
self.assertIn("ROOM-OK", run_video(free_gb=200))
def test_the_caches_of_its_first_start_count_with_the_checkpoint(self):
# 150 GB free, nothing cached: 135 for the checkpoint passed, and its first start then
# wrote 17 more onto a disk with 15 left (found in review, 2026-10-15); written already,
# they take nothing more
out = run_video(free_gb=150)
self.assertIn("DIE: not enough room", out)
self.assertIn("12 for the runtime's transformed weights, 5 for the decoder's converted weights", out)
out = run_video(free_gb=150, caches_there=True)
self.assertIn("NEED=135", out)
self.assertIn("ROOM-OK", out)
def test_the_caches_on_another_disk_are_summed_there(self):
# $HOME on its own disk with 15 GB free: 12 and 5 passed one by one (found in review,
# 2026-10-15)
out = run_video(free_gb=200, home_disk_gb=16)
self.assertIn("DIE: not enough room for the video lane's first start", out)
self.assertIn("about 17 GiB needed", out)
# each folder named, not the last one alone (found in review, 2026-10-15)
self.assertIn("/home/.cache/sglang/diffusion/host_spill and ", out)
self.assertIn("/home/.cache/sgl_diffusion/decode_dtype_store: 16 GiB free", out)
self.assertIn("NEED=135", run_video(free_gb=200, home_disk_gb=17))
def test_without_install_video_sh_it_asks_the_same_room(self):
# its fallback, when install-video.sh or its WEIGHTS_GB line cannot be read, is that number in
# GiB: it said 145, the GB install-video.sh counted before v1.20.0 (found 2026-11-10)
want = re.search(r"^WEIGHTS_GB=(\d+);", (REPO / "install-video.sh").read_text(), re.M).group(1)
with tempfile.TemporaryDirectory(prefix="sw-room-norepo-") as nowhere:
out = run_video(free_gb=200, caches_there=True, repo_dir=nowhere)
self.assertIn("install-video.sh: No such file", out, "the switch did not look where it was pointed")
self.assertIn(f"NEED={want}", out)
self.assertIn("ROOM-OK", out)
class TheCockpitGivesItTheTime(unittest.TestCase):
def test_the_switch_job_outlives_a_flash_download(self):
cockpit = (REPO / "dashboard/cockpit.py").read_text()
block = cockpit[cockpit.index('"switch": {'):]
# ended for not moving, not for taking long (2026-11-30): its cap is a day, and an hour of nothing ends it
self.assertEqual(re.search(r'"timeout":\s*(.+?),\n', block).group(1), 'JOB_CAP_S, "idle": JOB_IDLE_S')
idle, cap = map(int, re.search(r"(?m)^JOB_IDLE_S, JOB_CAP_S = (\d+), (\d+)$", cockpit).groups())
self.assertGreaterEqual(cap, 12 * 3600, "145 GB at 4 MB/s is 10 h")
self.assertGreaterEqual(idle, 1800, "a layer of an image, a copy of 33 GB say nothing for long")
if __name__ == "__main__":
unittest.main(verbosity=2)