Tests for the image noise switch: the exact sd-cli argument list with the switch off, the merged model-args string, the refusal for a build without the generator, the build stamp and rebuild decisions; run_set.py --noise for the gates

This commit is contained in:
Niko1221 committed 2026-11-10 16:02:24 +01:00
1 parent 4b9682183d
commit fccdb48a36
3 files changed
+177 -2

No files matched your search

+85
View File
@@ -111,6 +111,91 @@ class Command(unittest.TestCase):
cfg["native"] = False
self.assertNotIn("--model-args", images_api.sd_cli_args(cfg, job, "x", "p.txt"))
CARD = ["sd-cli", "--diffusion-model", "dit.gguf", "--vae", "vae.st", "--llm", "te.gguf",
"--prompt-file", "o/prompt.txt",
"--steps", "8", "--cfg-scale", "1.0", "--sampling-method", "euler", "-W", "1024", "-H", "768",
"--sigmas", "1.0,0.978453,0.95418,0.926626,0.89508,0.845148,0.704534,0.414568,0.0",
"-s", "42", "-o", "o/img_%d.png", "--diffusion-fa", "--vae-tiling"]
def test_noise_switch_off_leaves_the_command_line_alone(self):
"""images "noise" absent or "sdcpp" (the default): the exact argument list of before the switch existed."""
job = {"prompt": "a sloth", "width": 1024, "height": 768, "n": 1, "steps": 8, "seed": 42}
base = {"exe": "sd-cli", "diffusion_model": "dit.gguf", "vae": "vae.st", "llm": "te.gguf",
"args": ["--diffusion-fa", "--vae-tiling"]}
for extra in ({}, {"noise": "sdcpp"}, {"noise": None}, {"native": False, "noise": "sdcpp"}):
with self.subTest(extra=extra):
self.assertEqual(images_api.sd_cli_args({**base, **extra}, job, "o/img_%d.png", "o/prompt.txt"), self.CARD)
# and the native switch alone is still the last two arguments, as before
self.assertEqual(images_api.sd_cli_args({**base, "native": True}, job, "o/img_%d.png", "o/prompt.txt"),
self.CARD + ["--model-args", "qwen_image_2_1_native=1"])
def test_noise_switch_on_is_one_model_args_string(self):
job = {"prompt": "a sloth", "width": 1024, "height": 768, "n": 1, "steps": 8, "seed": 42}
base = {"exe": "sd-cli", "diffusion_model": "dit.gguf", "vae": "vae.st", "llm": "te.gguf",
"args": ["--diffusion-fa", "--vae-tiling"]}
self.assertEqual(images_api.sd_cli_args({**base, "noise": "strata"}, job, "o/img_%d.png", "o/prompt.txt"),
self.CARD + ["--model-args", "strata_noise=1"])
both = images_api.sd_cli_args({**base, "noise": "strata", "native": True}, job, "o/img_%d.png", "o/prompt.txt")
self.assertEqual(both, self.CARD + ["--model-args", "qwen_image_2_1_native=1,strata_noise=1"])
self.assertEqual(both.count("--model-args"), 1)
def test_noise_value_is_checked(self):
with tempfile.TemporaryDirectory() as d:
cfg = {"exe": "sd-cli", "diffusion_model": "d", "vae": "v", "llm": "l", "out_dir": d}
for ok in ("sdcpp", "strata"):
images_api.ImageGen({**cfg, "noise": ok})
for bad in ("on", "", "Strata", True, 1):
with self.subTest(bad=bad), self.assertRaises(ValueError):
images_api.ImageGen({**cfg, "noise": bad})
class NoiseRefusal(unittest.TestCase):
"""images "noise": "strata" with an sd-cli that was built without the generator: the job is refused, in plain
words with one next step, instead of quietly running on stable-diffusion.cpp's noise."""
WORDS = ("Image generation is set to use Strata's own noise (\"noise\": \"strata\" in the images settings), but the "
"image program here was built without it. Run setup again with: --images qwen-image --image-noise on")
def gen(self, d, build=None, **kw):
cfg = image_cfg(d, noise="strata", **kw)
exe = Path(d) / "engine-image" / "sd-cli"
exe.parent.mkdir(exist_ok=True)
exe.write_bytes(b"x")
cfg["exe"] = str(exe)
if build is not None:
(exe.parent / "BUILD.json").write_text(build if isinstance(build, str) else json.dumps(build))
return images_api.ImageGen(cfg)
def test_the_words(self):
self.assertEqual(images_api.NOISE_MISSING, self.WORDS)
def test_problem_by_build_json(self):
with tempfile.TemporaryDirectory() as d:
self.assertEqual(self.gen(d).noise_problem(), self.WORDS) # no BUILD.json
for build in ({"sd_cpp": "x", "backend": "cuda"}, {"noise": ""}, "not json", "[1]", "{"):
with self.subTest(build=build), tempfile.TemporaryDirectory() as d:
self.assertEqual(self.gen(d, build).noise_problem(), self.WORDS)
with tempfile.TemporaryDirectory() as d:
self.assertIsNone(self.gen(d, {"sd_cpp": "x", "noise": "0123456789abcdef"}).noise_problem())
def test_switch_off_asks_nothing(self):
with tempfile.TemporaryDirectory() as d:
for extra in ({}, {"noise": "sdcpp"}):
cfg = {**image_cfg(d), **extra}
self.assertIsNone(images_api.ImageGen(cfg).noise_problem()) # no BUILD.json needed
def test_a_job_is_refused_before_sd_cli_starts(self):
with tempfile.TemporaryDirectory() as d:
g = self.gen(d)
job = images_api.parse_request({"prompt": "a cat"}, {})
before = sorted(p.name for p in g.dir.iterdir())
with mock.patch("serve.lifeline.popen") as popen, self.assertRaises(images_api.ImageJobFailed) as cm:
g.run(job)
popen.assert_not_called()
self.assertEqual(str(cm.exception), self.WORDS)
self.assertEqual(sorted(p.name for p in g.dir.iterdir()), before) # no job folder left behind
self.assertEqual(g.totals["failed"], 1)
def test_env(self):
env = images_api.image_env({"backend": "cuda", "gpu": 1, "env": {"X": 2}}, {"PATH": "p"})
self.assertEqual((env["CUDA_VISIBLE_DEVICES"], env["CUDA_DEVICE_ORDER"], env["X"]), ("1", "PCI_BUS_ID", "2"))
+3 -2
View File
@@ -2,9 +2,10 @@
(serve/images.py sd_cli_args + image_env from the repo copy), one process per picture, and record time and memory.
python3 -I -B run_set.py <root> <backend> <tag> <W>x<H> <quant> <models_dir> <prompt-indices e.g. 0-7> <seeds e.g. 42,1234>
[--native] [--threads N] [--min-free-gb N] [--steps N] [--build NAME]
[--native] [--noise] [--threads N] [--min-free-gb N] [--steps N] [--build NAME]
[--dump] [--cond-from TAG] [--latent-from TAG] [--noise-from DIR] [--exit-after K]
[--env K=V ...] [--extra-args "sd-cli args"]
--noise: the config's images "noise" is "strata" (--model-args strata_noise=1); the build must have the generator.
--build NAME uses <root>/build-NAME.json (e.g. a stage_hooks.py variant); --dump/--cond-from/--latent-from/
--noise-from/--exit-after drive that variant's hooks (OWN5_* variables, see stage_hooks.py), per picture.
@@ -89,7 +90,7 @@ def main() -> int:
build = json.loads((root / f"build-{bname}.json").read_text())
md = Path(models).expanduser()
cfg = QI.image_config(backend, quant, Path(build["exe"]), md.parent if md.name == QI.MODEL_DIR else md,
native=native)
native=native, noise="--noise" in a)
if md.name != QI.MODEL_DIR: # files sit directly in models_dir
for k, name in (("diffusion_model", quant), ("vae", "vae"), ("llm", "llm")):
cfg[k] = str(md / Path(QI.FILES[name][1]).name)
+89
View File
@@ -198,5 +198,94 @@ class Native(unittest.TestCase):
A.apply(sd)
class Noise(unittest.TestCase):
"""OWN-5 step 1: Strata's noise generator for sd-cli (tools/image/sdcpp_noise): off by default; nobody compiles
again unasked."""
def test_stamp_without_the_switch_is_what_it_was(self):
self.assertEqual(QI.build_stamp("cuda", 86), {"sd_cpp": QI.SD_CPP_COMMIT, "ggml": QI.SD_GGML_COMMIT,
"backend": "cuda", "arch": "86", "native": QI.native_hash()})
self.assertEqual(QI.build_stamp("vulkan", None), {"sd_cpp": QI.SD_CPP_COMMIT, "ggml": QI.SD_GGML_COMMIT,
"backend": "vulkan", "arch": ""})
self.assertEqual(QI.build_stamp("cuda", 86, False), QI.build_stamp("cuda", 86))
def test_stamp_with_the_switch(self):
for backend, arch in (("cuda", 86), ("hip", "gfx1103"), ("vulkan", None), ("cpu", None)):
s = QI.build_stamp(backend, arch, True)
self.assertEqual(s["noise"], QI.noise_hash())
self.assertEqual({k: v for k, v in s.items() if k != "noise"}, QI.build_stamp(backend, arch))
def test_the_native_hash_does_not_move(self):
"""The noise files live apart from sdcpp_native, so no CUDA / HIP user's sd-cli is rebuilt for them."""
self.assertNotEqual(QI.NOISE_DIR, QI.NATIVE_DIR)
before = QI.native_hash()
with tempfile.TemporaryDirectory() as d:
for name in QI.NOISE_FILES:
(Path(d) / name).write_bytes((QI.NOISE_DIR / name).read_bytes() + b"changed")
with mock.patch.object(QI, "NOISE_DIR", Path(d)):
self.assertNotEqual(QI.noise_hash(), before)
self.assertEqual(QI.native_hash(), before)
self.assertFalse({p.name for p in QI.NATIVE_DIR.iterdir()} & (set(QI.NOISE_FILES) - {"apply.py"})) # both folders have an apply.py
def test_noise_hash(self):
with tempfile.TemporaryDirectory() as d:
for name in QI.NOISE_FILES:
(Path(d) / name).write_bytes((QI.NOISE_DIR / name).read_bytes())
with mock.patch.object(QI, "NOISE_DIR", Path(d)):
h = QI.noise_hash()
self.assertRegex(h, r"^[0-9a-f]{16}$")
self.assertEqual(h, QI.noise_hash())
p = Path(d) / "strata_noise.hpp"
p.write_bytes(p.read_bytes().replace(b"\n", b"\r\n")) # a Windows checkout: the same hash
self.assertEqual(QI.noise_hash(), h)
p.write_bytes(p.read_bytes() + b"// edit\n")
self.assertNotEqual(QI.noise_hash(), h)
self.assertEqual(QI.noise_hash(), QI.noise_hash())
def test_config(self):
c = QI.image_config("cuda", "Q8_0", Path("/e/sd-cli"), Path("/m"), noise=True)
self.assertEqual(c["noise"], "strata")
self.assertNotIn("noise", QI.image_config("cuda", "Q8_0", Path("/e/sd-cli"), Path("/m")))
from serve.images import ImageGen
with tempfile.TemporaryDirectory() as d:
ImageGen({**c, "out_dir": d}) # serve accepts what setup writes
def test_flags(self):
self.assertRegex((ROOT / "setup.py").read_text(encoding="utf-8"), re.escape('ap.add_argument("--image-noise"'))
self.assertRegex((ROOT / "tools" / "image" / "install.py").read_text(encoding="utf-8"),
re.escape('ap.add_argument("--noise"'))
def test_build_decisions(self):
def stamp_file(d, stamp):
(Path(d) / QI.SD_EXE).write_bytes(b"x")
(Path(d) / "BUILD.json").write_text(json.dumps({**stamp, "lib_dirs": [d]}))
with tempfile.TemporaryDirectory() as d, mock.patch.object(setup, "IMAGE_ENGINE", Path(d)):
# a build made before the switch existed: kept when the switch is off, rebuilt with the generator when on
stamp_file(d, QI.build_stamp("cuda", 86))
with mock.patch.object(setup, "cmake_build") as cb:
quiet(setup.build_image_engine, "cuda", {"arch": 86})
quiet(setup.build_image_engine, "cuda", {"arch": 86}, False)
cb.assert_not_called()
with mock.patch.object(setup, "get_sd_cpp"), mock.patch.object(setup, "apply_native_runner"), \
mock.patch.object(setup, "apply_noise_generator") as an, \
mock.patch.object(setup, "find_nvcc", return_value=(None, None)):
with self.assertRaises(SystemExit): # stops at "no nvcc", after the patches
quiet(setup.build_image_engine, "cuda", {"arch": 86}, True)
an.assert_called_once()
# a build that has the generator: kept with the switch on, kept with it off
stamp_file(d, QI.build_stamp("cuda", 86, True))
with mock.patch.object(setup, "cmake_build") as cb:
quiet(setup.build_image_engine, "cuda", {"arch": 86}, True)
quiet(setup.build_image_engine, "cuda", {"arch": 86}, False)
cb.assert_not_called()
# another arch with the switch off: compiled again, without the generator
with mock.patch.object(setup, "get_sd_cpp"), mock.patch.object(setup, "apply_native_runner"), \
mock.patch.object(setup, "apply_noise_generator") as an, \
mock.patch.object(setup, "find_nvcc", return_value=(None, None)):
with self.assertRaises(SystemExit):
quiet(setup.build_image_engine, "cuda", {"arch": 89})
an.assert_not_called()
if __name__ == "__main__":
unittest.main()