Spaces:
Running on Zero
Running on Zero
File size: 9,566 Bytes
8a28a8d 7571157 8a28a8d 7571157 8a28a8d 6d5e8f8 8a28a8d 7571157 8a28a8d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | from __future__ import annotations
import threading
import time
import os
import sys
import tempfile
import types
import unittest
from pathlib import Path
from unittest import mock
import yaml
from core.runtime_config import CONFIG, estimate_gpu_duration
from core.settings import CHECKPOINT_DIR, INPUT_DIR, OUTPUT_DIR
from core.task_scheduler import (
QueueFullError,
TaskCancelledError,
generation_guard,
generation_slot,
submit_background,
)
ROOT = Path(__file__).resolve().parents[1]
class RuntimeConfigTests(unittest.TestCase):
def test_duration_estimation_is_bounded(self):
self.assertEqual(estimate_gpu_duration({"zero_gpu_duration": 999}), 120)
self.assertEqual(estimate_gpu_duration({"zero_gpu_duration": 1}), 30)
self.assertEqual(
estimate_gpu_duration(
{
"model_display_name": "example-lightning",
"num_inference_steps": 4,
"batch_size": 1,
"width": 1024,
"height": 1024,
}
),
45,
)
self.assertEqual(
estimate_gpu_duration(
{
"model_display_name": "large-model",
"num_inference_steps": 40,
"batch_size": 3,
"width": 2048,
"height": 2048,
}
),
120,
)
def test_generation_slots_respect_configured_limit(self):
active = 0
maximum = 0
state_lock = threading.Lock()
def worker():
nonlocal active, maximum
with generation_slot():
with state_lock:
active += 1
maximum = max(maximum, active)
time.sleep(0.02)
with state_lock:
active -= 1
threads = [threading.Thread(target=worker) for _ in range(CONFIG.gpu_concurrency + 2)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
self.assertLessEqual(maximum, CONFIG.gpu_concurrency)
def test_cancelled_job_stops_before_guarded_execution(self):
reached_function = False
@generation_guard
def guarded(ui_inputs):
nonlocal reached_function
reached_function = True
cancel_event = threading.Event()
cancel_event.set()
with self.assertRaises(TaskCancelledError):
guarded({"_cancel_event": cancel_event})
self.assertFalse(reached_function)
def test_waiting_cancelled_job_leaves_gate_before_gpu_is_free(self):
holder_started = threading.Event()
release_holder = threading.Event()
cancellation = threading.Event()
errors = []
def holder():
with generation_slot():
holder_started.set()
release_holder.wait(2)
@generation_guard
def waiting_job(ui_inputs):
raise AssertionError("cancelled job must not execute")
def wait_then_cancel():
try:
waiting_job({"_cancel_event": cancellation})
except BaseException as exc:
errors.append(exc)
holder_thread = threading.Thread(target=holder)
holder_thread.start()
self.assertTrue(holder_started.wait(1))
waiting_thread = threading.Thread(target=wait_then_cancel)
waiting_thread.start()
cancellation.set()
waiting_thread.join(1)
self.assertFalse(waiting_thread.is_alive())
self.assertEqual(len(errors), 1)
self.assertIsInstance(errors[0], TaskCancelledError)
release_holder.set()
holder_thread.join(1)
self.assertFalse(holder_thread.is_alive())
def test_mcp_pending_queue_is_bounded(self):
release = threading.Event()
futures = [
submit_background(lambda: release.wait(2))
for _ in range(CONFIG.mcp_max_pending)
]
with self.assertRaises(QueueFullError):
submit_background(lambda: None)
release.set()
for future in futures:
self.assertTrue(future.result(timeout=3))
class RegistryTests(unittest.TestCase):
def test_runtime_directories_are_project_absolute(self):
for configured in (CHECKPOINT_DIR, INPUT_DIR, OUTPUT_DIR):
path = Path(configured)
self.assertTrue(path.is_absolute())
self.assertTrue(path.is_relative_to(ROOT))
def test_quick_presets_exist(self):
registry = yaml.safe_load((ROOT / "yaml" / "model_list.yaml").read_text("utf-8"))
names = {
model["display_name"]
for architecture in registry["Checkpoint"].values()
for model in architecture.get("models", [])
}
expected = {
"Krea-2-Turbo",
"lightx2v/Qwen-Image-2512-Lightning",
"circlestone-labs/Anima-Turbo-v1.0",
"lightx2v/Qwen-Image-Edit-2511-Lightning",
"CagliostroLab/Animagine XL 4.0",
}
self.assertTrue(expected.issubset(names))
def test_vendor_revisions_are_full_commits(self):
lock = yaml.safe_load((ROOT / "vendor.lock.yaml").read_text("utf-8"))
entries = [lock["comfyui"], *lock["custom_nodes"].values()]
for entry in entries:
revision = entry["revision"]
self.assertEqual(len(revision), 40)
int(revision, 16)
def test_task_input_recipes_are_complete(self):
input_dir = ROOT / "core" / "pipelines" / "workflow_recipes" / "_partials" / "input"
task_recipes = {
"txt2img": "txt2img_latent.yaml",
"img2img": "img2img.yaml",
"inpaint": "inpaint.yaml",
"outpaint": "outpaint.yaml",
"hires_fix": "hires_fix.yaml",
}
for task_type, recipe_name in task_recipes.items():
recipe = yaml.safe_load((input_dir / recipe_name).read_text("utf-8"))
self.assertIn(
"latent_source",
recipe.get("nodes", {}),
f"{task_type} must provide the sampler latent_source",
)
txt2img_router = yaml.safe_load((input_dir / "txt2img.yaml").read_text("utf-8"))
self.assertEqual(
txt2img_router["imports"], ["txt2img_{{ latent_type }}.yaml"]
)
def test_concurrency_regressions_are_absent(self):
mcp_run = (ROOT / "mcp_tools" / "run.py").read_text("utf-8")
input_processor = (
ROOT / "core" / "pipelines" / "pipeline_input_processor.py"
).read_text("utf-8")
studio = (ROOT / "ui" / "shared" / "studio_ui.py").read_text("utf-8")
requirements = (ROOT / "requirements.txt").read_text("utf-8")
self.assertNotIn("threading.Thread", mcp_run)
self.assertIn("uuid.uuid4().hex", input_processor)
self.assertIn('"_task_prefixes": [(prefix, None)]', studio)
self.assertIn("onnxruntime-gpu==", requirements)
class ComfySetupTests(unittest.TestCase):
def test_initialize_registers_application_model_directories(self):
from comfy_integration import setup
with tempfile.TemporaryDirectory() as temp_dir:
root = Path(temp_dir)
comfyui_path = root / "ComfyUI"
comfyui_path.mkdir()
(comfyui_path / "nodes.py").touch()
model_dir = root / "models" / "checkpoints"
input_dir = root / "input"
output_dir = root / "output"
folder_paths = types.ModuleType("folder_paths")
folder_paths.add_model_folder_path = mock.Mock()
folder_paths.set_input_directory = mock.Mock()
folder_paths.set_output_directory = mock.Mock()
comfy_package = types.ModuleType("comfy")
model_management = types.ModuleType("comfy.model_management")
comfy_package.model_management = model_management
with (
mock.patch.object(
setup, "CATEGORY_TO_DIR_MAP", {"checkpoints": str(model_dir)}
),
mock.patch.object(setup, "INPUT_DIR", str(input_dir)),
mock.patch.object(setup, "OUTPUT_DIR", str(output_dir)),
mock.patch.object(setup, "_load_lock", return_value={"comfyui": {}}),
mock.patch.dict(
os.environ,
{
"COMFYUI_PATH": str(comfyui_path),
"IMAGEGEN_SKIP_CUSTOM_NODES": "1",
},
clear=False,
),
mock.patch.dict(
sys.modules,
{
"folder_paths": folder_paths,
"comfy": comfy_package,
"comfy.model_management": model_management,
},
),
):
setup.initialize_comfyui()
folder_paths.add_model_folder_path.assert_called_once_with(
"checkpoints", str(model_dir.resolve()), is_default=True
)
folder_paths.set_input_directory.assert_called_once_with(
str(input_dir.resolve())
)
folder_paths.set_output_directory.assert_called_once_with(
str(output_dir.resolve())
)
if __name__ == "__main__":
unittest.main()
|