Spaces:
Running on Zero
perf(pipeline): load the 5hz lm only for text2music calls that use it
Browse filesOn ZeroGPU each decorated handler has its own worker, and every cold worker
runs _ensure_loaded. Since the LM is found on the Space, that made every
cold Cover, Extend and Edit call pay the LM tokenizer, constrained
processor and 3.7 GB weight load inside its GPU request, although the fork
never runs the LM for task_type cover or repaint (inference.py:372-381,
skip_lm_tasks). The same held for Generate with thinking and all CoT flags
off.
_ensure_loaded now loads the DiT, VAE and text encoder only (same failure
rules). The LM gets its own lazy step, taken only when the fork would use
it: task_type not cover/repaint and thinking or any use_cot_* flag on, the
fork's own rule. Otherwise generate_music gets None, which the fork
accepts. The LM is still attempted once per process, a failure is still
logged loudly and generation continues without it, and nothing the LM does
touches the DiT. Each load logs its own [ams] timing line.
- ace_pipeline.py +67 -18
- tests/test_ace_pipeline_lazy.py +180 -49
|
@@ -126,13 +126,32 @@ def _audio_duration_s(path: str | None) -> float:
|
|
| 126 |
)
|
| 127 |
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
class ACEStepStudio:
|
| 130 |
"""Wrapper around the apple-silicon fork's split-handler API.
|
| 131 |
|
| 132 |
Owns one ``AceStepHandler`` (DiT) and one ``LLMHandler`` (5Hz LM
|
| 133 |
-
planner).
|
| 134 |
-
The
|
| 135 |
-
|
|
|
|
|
|
|
| 136 |
"""
|
| 137 |
|
| 138 |
def __init__(
|
|
@@ -143,6 +162,7 @@ class ACEStepStudio:
|
|
| 143 |
) -> None:
|
| 144 |
self._dit = None
|
| 145 |
self._llm = None
|
|
|
|
| 146 |
self._dit_config = dit_config or os.environ.get("ACE_DIT_CONFIG", _DEFAULT_DIT_CONFIG)
|
| 147 |
self._lm_model = lm_model or os.environ.get("ACE_LM_MODEL", _DEFAULT_LM_MODEL)
|
| 148 |
self._device = device or detect_device()
|
|
@@ -157,18 +177,18 @@ class ACEStepStudio:
|
|
| 157 |
return self._dit is not None
|
| 158 |
|
| 159 |
def _ensure_loaded(self) -> None:
|
| 160 |
-
"""First-call lazy load of
|
| 161 |
|
| 162 |
-
|
| 163 |
-
rather than raising. A
|
| 164 |
-
fork's status text and caches nothing, so
|
| 165 |
-
|
|
|
|
| 166 |
"""
|
| 167 |
if self.is_loaded:
|
| 168 |
return
|
| 169 |
|
| 170 |
from acestep.handler import AceStepHandler
|
| 171 |
-
from acestep.llm_inference import LLMHandler
|
| 172 |
|
| 173 |
t0 = time.perf_counter()
|
| 174 |
try:
|
|
@@ -189,9 +209,29 @@ class ACEStepStudio:
|
|
| 189 |
)
|
| 190 |
raise RuntimeError(f"ACE-Step model initialisation failed: {dit_status}")
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
t1 = time.perf_counter()
|
| 193 |
llm = None
|
| 194 |
try:
|
|
|
|
|
|
|
| 195 |
candidate = LLMHandler()
|
| 196 |
lm_status, lm_ok = candidate.initialize(
|
| 197 |
checkpoint_dir=str(_CHECKPOINTS_DIR),
|
|
@@ -211,12 +251,10 @@ class ACEStepStudio:
|
|
| 211 |
f"Reason: {lm_status}"
|
| 212 |
)
|
| 213 |
|
| 214 |
-
self._dit = dit
|
| 215 |
self._llm = llm
|
| 216 |
_log(
|
| 217 |
-
f"
|
| 218 |
-
f"
|
| 219 |
-
f"(dit={self._dit_config}, lm={self._lm_model}, device={self._device})"
|
| 220 |
)
|
| 221 |
|
| 222 |
def generate(self, params: dict) -> str:
|
|
@@ -349,6 +387,10 @@ class ACEStepStudio:
|
|
| 349 |
# - ``use_cot_metas`` / ``use_cot_caption`` / ``use_cot_language``
|
| 350 |
# keys renamed from ``cot_*`` for consistency with the dataclass.
|
| 351 |
task_kwargs = {"instruction": instruction} if instruction is not None else {}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
gen_params = GenerationParams(
|
| 353 |
task_type=task_type,
|
| 354 |
caption=caption,
|
|
@@ -376,15 +418,15 @@ class ACEStepStudio:
|
|
| 376 |
# 5Hz language model knobs — defaults flipped to True so the
|
| 377 |
# LM actually reasons about each prompt instead of returning
|
| 378 |
# blank captions / metadata back to the DiT.
|
| 379 |
-
thinking=
|
| 380 |
lm_temperature=float(lm_opts.get("temperature", 0.85)),
|
| 381 |
lm_cfg_scale=float(lm_opts.get("cfg", 2.0)),
|
| 382 |
lm_top_k=int(lm_opts.get("top_k", 0)),
|
| 383 |
lm_top_p=float(lm_opts.get("top_p", 0.9)),
|
| 384 |
lm_negative_prompt=lm_opts.get("negative_prompt", "NO USER INPUT"),
|
| 385 |
-
use_cot_metas=
|
| 386 |
-
use_cot_caption=
|
| 387 |
-
use_cot_language=
|
| 388 |
**task_kwargs,
|
| 389 |
)
|
| 390 |
|
|
@@ -400,9 +442,16 @@ class ACEStepStudio:
|
|
| 400 |
# Pass an explicit output dir so the path is always usable.
|
| 401 |
_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 402 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 403 |
result = generate_music(
|
| 404 |
self._dit,
|
| 405 |
-
|
| 406 |
gen_params,
|
| 407 |
gen_config,
|
| 408 |
save_dir=str(_OUTPUT_DIR),
|
|
|
|
| 126 |
)
|
| 127 |
|
| 128 |
|
| 129 |
+
# Task types the fork runs without the 5Hz LM (inference.py ``skip_lm_tasks``).
|
| 130 |
+
_LM_SKIP_TASKS = frozenset({"cover", "repaint"})
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def _fork_uses_lm(
|
| 134 |
+
task_type: str, thinking: bool, use_cot_caption: bool, use_cot_language: bool, use_cot_metas: bool
|
| 135 |
+
) -> bool:
|
| 136 |
+
"""Whether ``generate_music`` would run the 5Hz LM for these params.
|
| 137 |
+
|
| 138 |
+
Mirrors the fork's own decision (inference.py:372-381) minus its check
|
| 139 |
+
that the handler is loaded: the LM is used for every task type except
|
| 140 |
+
cover and repaint, when thinking or any CoT flag is on.
|
| 141 |
+
"""
|
| 142 |
+
need_lm_for_cot = use_cot_caption or use_cot_language or use_cot_metas
|
| 143 |
+
return bool((thinking or need_lm_for_cot) and task_type not in _LM_SKIP_TASKS)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
class ACEStepStudio:
|
| 147 |
"""Wrapper around the apple-silicon fork's split-handler API.
|
| 148 |
|
| 149 |
Owns one ``AceStepHandler`` (DiT) and one ``LLMHandler`` (5Hz LM
|
| 150 |
+
planner). The DiT is lazy-loaded on the first ``generate(...)`` call.
|
| 151 |
+
The LM is optional: it is loaded on the first call the fork would use
|
| 152 |
+
it for (text2music with thinking or CoT on), once per process, and
|
| 153 |
+
stays ``None`` when it fails to load (``generate_music`` then runs the
|
| 154 |
+
DiT alone). Calls that do not use it pass ``None`` and never load it.
|
| 155 |
"""
|
| 156 |
|
| 157 |
def __init__(
|
|
|
|
| 162 |
) -> None:
|
| 163 |
self._dit = None
|
| 164 |
self._llm = None
|
| 165 |
+
self._lm_tried = False # one LM load attempt per process
|
| 166 |
self._dit_config = dit_config or os.environ.get("ACE_DIT_CONFIG", _DEFAULT_DIT_CONFIG)
|
| 167 |
self._lm_model = lm_model or os.environ.get("ACE_LM_MODEL", _DEFAULT_LM_MODEL)
|
| 168 |
self._device = device or detect_device()
|
|
|
|
| 177 |
return self._dit is not None
|
| 178 |
|
| 179 |
def _ensure_loaded(self) -> None:
|
| 180 |
+
"""First-call lazy load of the DiT, VAE and text encoder.
|
| 181 |
|
| 182 |
+
The fork's initialiser reports failure by returning
|
| 183 |
+
``(status, False)`` rather than raising. A failure raises
|
| 184 |
+
``RuntimeError`` with the fork's status text and caches nothing, so
|
| 185 |
+
the next call retries. The 5Hz LM is not loaded here; see
|
| 186 |
+
``_ensure_lm_loaded``. Heavy imports stay local.
|
| 187 |
"""
|
| 188 |
if self.is_loaded:
|
| 189 |
return
|
| 190 |
|
| 191 |
from acestep.handler import AceStepHandler
|
|
|
|
| 192 |
|
| 193 |
t0 = time.perf_counter()
|
| 194 |
try:
|
|
|
|
| 209 |
)
|
| 210 |
raise RuntimeError(f"ACE-Step model initialisation failed: {dit_status}")
|
| 211 |
|
| 212 |
+
self._dit = dit
|
| 213 |
+
_log(
|
| 214 |
+
f"pipeline load: DiT/VAE/text encoder {dit_s:.1f} s "
|
| 215 |
+
f"(dit={self._dit_config}, device={self._device})"
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
def _ensure_lm_loaded(self) -> None:
|
| 219 |
+
"""Load the 5Hz LM on the first call that uses it; one attempt per process.
|
| 220 |
+
|
| 221 |
+
``LLMHandler.initialize`` reports failure by returning
|
| 222 |
+
``(status, False)``. A failure (or exception) is logged loudly and
|
| 223 |
+
not retried; ``self._llm`` stays ``None`` and generation runs the
|
| 224 |
+
DiT alone. Nothing here touches the DiT.
|
| 225 |
+
"""
|
| 226 |
+
if self._llm is not None or self._lm_tried:
|
| 227 |
+
return
|
| 228 |
+
self._lm_tried = True
|
| 229 |
+
|
| 230 |
t1 = time.perf_counter()
|
| 231 |
llm = None
|
| 232 |
try:
|
| 233 |
+
from acestep.llm_inference import LLMHandler
|
| 234 |
+
|
| 235 |
candidate = LLMHandler()
|
| 236 |
lm_status, lm_ok = candidate.initialize(
|
| 237 |
checkpoint_dir=str(_CHECKPOINTS_DIR),
|
|
|
|
| 251 |
f"Reason: {lm_status}"
|
| 252 |
)
|
| 253 |
|
|
|
|
| 254 |
self._llm = llm
|
| 255 |
_log(
|
| 256 |
+
f"5Hz LM load: {lm_s:.1f} s, LM active: {'yes' if llm is not None else 'no'} "
|
| 257 |
+
f"(lm={self._lm_model}, device={self._device})"
|
|
|
|
| 258 |
)
|
| 259 |
|
| 260 |
def generate(self, params: dict) -> str:
|
|
|
|
| 387 |
# - ``use_cot_metas`` / ``use_cot_caption`` / ``use_cot_language``
|
| 388 |
# keys renamed from ``cot_*`` for consistency with the dataclass.
|
| 389 |
task_kwargs = {"instruction": instruction} if instruction is not None else {}
|
| 390 |
+
thinking = bool(lm_opts.get("thinking", True))
|
| 391 |
+
use_cot_metas = bool(lm_opts.get("use_cot_metas", True))
|
| 392 |
+
use_cot_caption = bool(lm_opts.get("use_cot_caption", True))
|
| 393 |
+
use_cot_language = bool(lm_opts.get("use_cot_language", True))
|
| 394 |
gen_params = GenerationParams(
|
| 395 |
task_type=task_type,
|
| 396 |
caption=caption,
|
|
|
|
| 418 |
# 5Hz language model knobs — defaults flipped to True so the
|
| 419 |
# LM actually reasons about each prompt instead of returning
|
| 420 |
# blank captions / metadata back to the DiT.
|
| 421 |
+
thinking=thinking,
|
| 422 |
lm_temperature=float(lm_opts.get("temperature", 0.85)),
|
| 423 |
lm_cfg_scale=float(lm_opts.get("cfg", 2.0)),
|
| 424 |
lm_top_k=int(lm_opts.get("top_k", 0)),
|
| 425 |
lm_top_p=float(lm_opts.get("top_p", 0.9)),
|
| 426 |
lm_negative_prompt=lm_opts.get("negative_prompt", "NO USER INPUT"),
|
| 427 |
+
use_cot_metas=use_cot_metas,
|
| 428 |
+
use_cot_caption=use_cot_caption,
|
| 429 |
+
use_cot_language=use_cot_language,
|
| 430 |
**task_kwargs,
|
| 431 |
)
|
| 432 |
|
|
|
|
| 442 |
# Pass an explicit output dir so the path is always usable.
|
| 443 |
_OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 444 |
|
| 445 |
+
# Load the 5Hz LM only for calls the fork will use it for; the others
|
| 446 |
+
# (cover, repaint, or no thinking/CoT) get None and never pay its load.
|
| 447 |
+
llm = None
|
| 448 |
+
if _fork_uses_lm(task_type, thinking, use_cot_caption, use_cot_language, use_cot_metas):
|
| 449 |
+
self._ensure_lm_loaded()
|
| 450 |
+
llm = self._llm
|
| 451 |
+
|
| 452 |
result = generate_music(
|
| 453 |
self._dit,
|
| 454 |
+
llm,
|
| 455 |
gen_params,
|
| 456 |
gen_config,
|
| 457 |
save_dir=str(_OUTPUT_DIR),
|
|
@@ -84,24 +84,92 @@ def _install_fake_handlers(
|
|
| 84 |
return fake_dit_cls, fake_lm_cls
|
| 85 |
|
| 86 |
|
| 87 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
fake_dit_cls, fake_lm_cls = _install_fake_handlers(monkeypatch)
|
| 89 |
|
| 90 |
studio = ap.ACEStepStudio()
|
| 91 |
studio._ensure_loaded()
|
| 92 |
|
| 93 |
fake_dit_cls.assert_called_once()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
fake_lm_cls.assert_called_once()
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
assert fake_dit.initialize_service.call_args.kwargs["device"] == "mps"
|
| 100 |
-
assert fake_lm.initialize.call_args.kwargs["device"] == "mps"
|
| 101 |
-
assert fake_dit.initialize_service.call_args.kwargs["config_path"] == "acestep-v15-xl-sft"
|
| 102 |
# Default 5Hz LM since f9a1a8e.
|
| 103 |
-
assert
|
| 104 |
-
assert
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
def test_lm_checkpoint_dir_is_the_vendored_fork_checkpoints(monkeypatch):
|
|
@@ -112,10 +180,8 @@ def test_lm_checkpoint_dir_is_the_vendored_fork_checkpoints(monkeypatch):
|
|
| 112 |
directory app.py populates on the Space. ``<repo>/checkpoints`` exists
|
| 113 |
only on the operator's Mac.
|
| 114 |
"""
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
studio = ap.ACEStepStudio()
|
| 118 |
-
studio._ensure_loaded()
|
| 119 |
|
| 120 |
expected = ap._REPO_ROOT / "vendor" / "ace-step" / "checkpoints"
|
| 121 |
assert studio._llm.initialize.call_args.kwargs["checkpoint_dir"] == str(expected)
|
|
@@ -124,60 +190,112 @@ def test_lm_checkpoint_dir_is_the_vendored_fork_checkpoints(monkeypatch):
|
|
| 124 |
def test_ace_lm_model_absolute_path_is_passed_through(monkeypatch, tmp_path):
|
| 125 |
"""An absolute ACE_LM_MODEL keeps working: the fork joins it onto
|
| 126 |
checkpoint_dir with os.path.join, which keeps the absolute path."""
|
| 127 |
-
|
| 128 |
lm_dir = tmp_path / "my-lm"
|
| 129 |
monkeypatch.setenv("ACE_LM_MODEL", str(lm_dir))
|
| 130 |
-
|
| 131 |
-
studio
|
| 132 |
-
studio._ensure_loaded()
|
| 133 |
|
| 134 |
kwargs = studio._llm.initialize.call_args.kwargs
|
| 135 |
assert kwargs["lm_model_path"] == str(lm_dir)
|
| 136 |
assert os.path.join(kwargs["checkpoint_dir"], kwargs["lm_model_path"]) == str(lm_dir)
|
| 137 |
|
| 138 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
def test_lm_not_found_is_logged_and_generation_runs_without_lm(monkeypatch, capsys):
|
| 140 |
"""ACE_LM_MODEL naming a model that does not exist: the fork returns
|
| 141 |
("... not found ...", False) without raising. That must be loud on
|
| 142 |
stderr and must NOT stop generation (today's effective behaviour)."""
|
| 143 |
-
|
| 144 |
monkeypatch,
|
| 145 |
lm_result=("❌ 5Hz LM model not found at /x/checkpoints/no-such-lm", False),
|
| 146 |
)
|
| 147 |
monkeypatch.setenv("ACE_LM_MODEL", "no-such-lm")
|
|
|
|
| 148 |
|
| 149 |
-
studio
|
| 150 |
-
studio._ensure_loaded()
|
| 151 |
|
| 152 |
err = capsys.readouterr().err
|
| 153 |
assert "[ams]" in err
|
| 154 |
assert "no-such-lm" in err
|
| 155 |
assert "not found" in err
|
| 156 |
-
assert studio.
|
| 157 |
-
assert
|
| 158 |
-
|
| 159 |
-
# Generation goes ahead with the DiT only.
|
| 160 |
-
fake_generate, _captured = _install_fake_inference(monkeypatch)
|
| 161 |
-
studio.generate({"mode": "generate", "prompt": "p", "duration_s": 5, "seed": 1})
|
| 162 |
-
fake_generate.assert_called_once()
|
| 163 |
-
assert fake_generate.call_args.args[1] is None
|
| 164 |
|
| 165 |
-
# A failed LM is not retried on
|
| 166 |
-
|
| 167 |
fake_lm_cls.assert_called_once()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
|
| 170 |
def test_lm_init_exception_is_logged_and_not_fatal(monkeypatch, capsys):
|
| 171 |
-
|
|
|
|
|
|
|
| 172 |
|
| 173 |
-
studio
|
| 174 |
-
|
| 175 |
|
| 176 |
err = capsys.readouterr().err
|
| 177 |
assert "[ams]" in err
|
| 178 |
assert "mlx exploded" in err
|
| 179 |
assert studio.is_loaded is True
|
| 180 |
assert studio._llm is None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
|
| 183 |
def test_dit_init_failure_raises_with_fork_status_and_is_retried(monkeypatch):
|
|
@@ -211,32 +329,45 @@ def test_dit_init_exception_raises_runtime_error_and_is_not_cached(monkeypatch):
|
|
| 211 |
|
| 212 |
|
| 213 |
def test_generate_does_not_run_when_dit_init_fails(monkeypatch):
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
studio = ap.ACEStepStudio()
|
| 218 |
with pytest.raises(RuntimeError, match="Unknown DiT model"):
|
| 219 |
-
|
| 220 |
fake_generate.assert_not_called()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
|
| 223 |
@pytest.mark.parametrize(
|
| 224 |
("lm_result", "active"),
|
| 225 |
[(("LM ready", True), "yes"), (("❌ 5Hz LM model not found at /x", False), "no")],
|
| 226 |
)
|
| 227 |
-
def
|
| 228 |
-
|
| 229 |
|
| 230 |
-
studio
|
| 231 |
-
|
| 232 |
-
studio._ensure_loaded() # already loaded: no second line
|
| 233 |
|
| 234 |
-
|
|
|
|
|
|
|
| 235 |
assert len(lines) == 1
|
| 236 |
-
|
| 237 |
-
assert
|
| 238 |
-
assert re.search(r"5Hz LM \d+\.\d s", line), line
|
| 239 |
-
assert f"LM active: {active}" in line
|
| 240 |
|
| 241 |
|
| 242 |
def _install_fake_inference(monkeypatch, success=True, audios=None, error=None):
|
|
|
|
| 84 |
return fake_dit_cls, fake_lm_cls
|
| 85 |
|
| 86 |
|
| 87 |
+
def _install_fake_constants(monkeypatch):
|
| 88 |
+
fake_constants = MagicMock()
|
| 89 |
+
fake_constants.TASK_INSTRUCTIONS = {"repaint": "Repaint the mask area based on the given conditions:"}
|
| 90 |
+
monkeypatch.setitem(sys.modules, "acestep.constants", fake_constants)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
ALL_LM_FLAGS_OFF = {
|
| 94 |
+
"thinking": False,
|
| 95 |
+
"use_cot_caption": False,
|
| 96 |
+
"use_cot_metas": False,
|
| 97 |
+
"use_cot_language": False,
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
# One call per song mode, shaped like backend.dispatch's params.
|
| 101 |
+
MODE_CALLS = {
|
| 102 |
+
"generate": {"mode": "generate", "prompt": "p", "duration_s": 5, "seed": 1},
|
| 103 |
+
"cover": {"mode": "cover", "ref_audio": "/r.wav", "prompt": "p", "duration_s": 5, "seed": 1},
|
| 104 |
+
"extend": {"mode": "extend", "seed_audio": "/s.wav", "extra_duration_s": 5, "seed": 1},
|
| 105 |
+
"edit_repaint": {
|
| 106 |
+
"mode": "edit",
|
| 107 |
+
"source_audio": "/s.wav",
|
| 108 |
+
"segment_start_s": 1.0,
|
| 109 |
+
"segment_end_s": 2.0,
|
| 110 |
+
"sub_mode": "repaint",
|
| 111 |
+
"seed": 1,
|
| 112 |
+
},
|
| 113 |
+
"flow_edit": {
|
| 114 |
+
"mode": "edit",
|
| 115 |
+
"source_audio": "/s.wav",
|
| 116 |
+
"segment_start_s": 0.0,
|
| 117 |
+
"segment_end_s": 5.0,
|
| 118 |
+
"sub_mode": "flow_edit",
|
| 119 |
+
"flow_source_caption": "c",
|
| 120 |
+
"seed": 1,
|
| 121 |
+
},
|
| 122 |
+
}
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def _cold_studio(monkeypatch, **handler_kwargs):
|
| 126 |
+
"""A cold pipeline with fake fork handlers, inference and constants."""
|
| 127 |
+
fake_dit_cls, fake_lm_cls = _install_fake_handlers(monkeypatch, **handler_kwargs)
|
| 128 |
+
fake_generate, _captured = _install_fake_inference(monkeypatch)
|
| 129 |
+
_install_fake_constants(monkeypatch)
|
| 130 |
+
monkeypatch.setattr(ap, "_audio_duration_s", lambda path: 10.0)
|
| 131 |
+
return ap.ACEStepStudio(), fake_dit_cls, fake_lm_cls, fake_generate
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
def _call(studio, name, lm=None):
|
| 135 |
+
params = dict(MODE_CALLS[name])
|
| 136 |
+
if lm is not None:
|
| 137 |
+
params["lm"] = lm
|
| 138 |
+
return studio.generate(params)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def test_ensure_loaded_loads_only_the_dit(monkeypatch):
|
| 142 |
fake_dit_cls, fake_lm_cls = _install_fake_handlers(monkeypatch)
|
| 143 |
|
| 144 |
studio = ap.ACEStepStudio()
|
| 145 |
studio._ensure_loaded()
|
| 146 |
|
| 147 |
fake_dit_cls.assert_called_once()
|
| 148 |
+
studio._dit.initialize_service.assert_called_once()
|
| 149 |
+
kwargs = studio._dit.initialize_service.call_args.kwargs
|
| 150 |
+
assert kwargs["device"] == "mps"
|
| 151 |
+
assert kwargs["config_path"] == "acestep-v15-xl-sft"
|
| 152 |
+
assert studio.is_loaded is True
|
| 153 |
+
# The LM is not part of the base load (see the lazy-LM tests below).
|
| 154 |
+
fake_lm_cls.assert_not_called()
|
| 155 |
+
assert studio._llm is None
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def test_generate_with_defaults_loads_the_lm_once(monkeypatch):
|
| 159 |
+
studio, fake_dit_cls, fake_lm_cls, fake_generate = _cold_studio(monkeypatch)
|
| 160 |
+
|
| 161 |
+
_call(studio, "generate")
|
| 162 |
+
_call(studio, "generate")
|
| 163 |
+
|
| 164 |
fake_lm_cls.assert_called_once()
|
| 165 |
+
lm = studio._llm
|
| 166 |
+
lm.initialize.assert_called_once()
|
| 167 |
+
kwargs = lm.initialize.call_args.kwargs
|
| 168 |
+
assert kwargs["device"] == "mps"
|
|
|
|
|
|
|
|
|
|
| 169 |
# Default 5Hz LM since f9a1a8e.
|
| 170 |
+
assert kwargs["lm_model_path"] == "acestep-5Hz-lm-1.7B"
|
| 171 |
+
assert [c.args[1] for c in fake_generate.call_args_list] == [lm, lm]
|
| 172 |
+
fake_dit_cls.assert_called_once()
|
| 173 |
|
| 174 |
|
| 175 |
def test_lm_checkpoint_dir_is_the_vendored_fork_checkpoints(monkeypatch):
|
|
|
|
| 180 |
directory app.py populates on the Space. ``<repo>/checkpoints`` exists
|
| 181 |
only on the operator's Mac.
|
| 182 |
"""
|
| 183 |
+
studio, *_ = _cold_studio(monkeypatch)
|
| 184 |
+
_call(studio, "generate")
|
|
|
|
|
|
|
| 185 |
|
| 186 |
expected = ap._REPO_ROOT / "vendor" / "ace-step" / "checkpoints"
|
| 187 |
assert studio._llm.initialize.call_args.kwargs["checkpoint_dir"] == str(expected)
|
|
|
|
| 190 |
def test_ace_lm_model_absolute_path_is_passed_through(monkeypatch, tmp_path):
|
| 191 |
"""An absolute ACE_LM_MODEL keeps working: the fork joins it onto
|
| 192 |
checkpoint_dir with os.path.join, which keeps the absolute path."""
|
| 193 |
+
studio, *_ = _cold_studio(monkeypatch)
|
| 194 |
lm_dir = tmp_path / "my-lm"
|
| 195 |
monkeypatch.setenv("ACE_LM_MODEL", str(lm_dir))
|
| 196 |
+
studio = ap.ACEStepStudio() # reads ACE_LM_MODEL at construction
|
| 197 |
+
_call(studio, "generate")
|
|
|
|
| 198 |
|
| 199 |
kwargs = studio._llm.initialize.call_args.kwargs
|
| 200 |
assert kwargs["lm_model_path"] == str(lm_dir)
|
| 201 |
assert os.path.join(kwargs["checkpoint_dir"], kwargs["lm_model_path"]) == str(lm_dir)
|
| 202 |
|
| 203 |
|
| 204 |
+
@pytest.mark.parametrize("name", ["cover", "extend", "edit_repaint", "flow_edit"])
|
| 205 |
+
def test_modes_the_fork_runs_without_lm_never_load_it(monkeypatch, name):
|
| 206 |
+
"""The fork skips the LM for task_type cover and repaint
|
| 207 |
+
(inference.py skip_lm_tasks), so a cold Cover/Extend/Edit call must not
|
| 208 |
+
pay for loading it, even with thinking and CoT on (the UI defaults)."""
|
| 209 |
+
studio, fake_dit_cls, fake_lm_cls, fake_generate = _cold_studio(monkeypatch)
|
| 210 |
+
|
| 211 |
+
_call(studio, name)
|
| 212 |
+
_call(studio, name)
|
| 213 |
+
|
| 214 |
+
fake_lm_cls.assert_not_called()
|
| 215 |
+
assert studio._llm is None
|
| 216 |
+
assert [c.args[1] for c in fake_generate.call_args_list] == [None, None]
|
| 217 |
+
fake_dit_cls.assert_called_once()
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def test_generate_without_thinking_or_cot_does_not_load_the_lm(monkeypatch):
|
| 221 |
+
studio, _dit_cls, fake_lm_cls, fake_generate = _cold_studio(monkeypatch)
|
| 222 |
+
|
| 223 |
+
_call(studio, "generate", lm=dict(ALL_LM_FLAGS_OFF))
|
| 224 |
+
|
| 225 |
+
fake_lm_cls.assert_not_called()
|
| 226 |
+
assert fake_generate.call_args.args[1] is None
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
@pytest.mark.parametrize("flag", ["thinking", "use_cot_caption", "use_cot_metas", "use_cot_language"])
|
| 230 |
+
def test_generate_loads_the_lm_when_any_lm_flag_is_on(monkeypatch, flag):
|
| 231 |
+
"""Mirror of the fork's rule: thinking OR any use_cot_* flag."""
|
| 232 |
+
studio, _dit_cls, fake_lm_cls, fake_generate = _cold_studio(monkeypatch)
|
| 233 |
+
|
| 234 |
+
_call(studio, "generate", lm={**ALL_LM_FLAGS_OFF, flag: True})
|
| 235 |
+
|
| 236 |
+
fake_lm_cls.assert_called_once()
|
| 237 |
+
assert fake_generate.call_args.args[1] is studio._llm
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
def test_lm_loads_on_the_first_generate_after_other_modes(monkeypatch):
|
| 241 |
+
studio, fake_dit_cls, fake_lm_cls, fake_generate = _cold_studio(monkeypatch)
|
| 242 |
+
|
| 243 |
+
for name in ("cover", "extend", "edit_repaint", "flow_edit"):
|
| 244 |
+
_call(studio, name)
|
| 245 |
+
fake_lm_cls.assert_not_called()
|
| 246 |
+
|
| 247 |
+
_call(studio, "generate")
|
| 248 |
+
fake_lm_cls.assert_called_once()
|
| 249 |
+
assert fake_generate.call_args.args[1] is studio._llm
|
| 250 |
+
fake_dit_cls.assert_called_once()
|
| 251 |
+
|
| 252 |
+
|
| 253 |
def test_lm_not_found_is_logged_and_generation_runs_without_lm(monkeypatch, capsys):
|
| 254 |
"""ACE_LM_MODEL naming a model that does not exist: the fork returns
|
| 255 |
("... not found ...", False) without raising. That must be loud on
|
| 256 |
stderr and must NOT stop generation (today's effective behaviour)."""
|
| 257 |
+
studio, fake_dit_cls, fake_lm_cls, fake_generate = _cold_studio(
|
| 258 |
monkeypatch,
|
| 259 |
lm_result=("❌ 5Hz LM model not found at /x/checkpoints/no-such-lm", False),
|
| 260 |
)
|
| 261 |
monkeypatch.setenv("ACE_LM_MODEL", "no-such-lm")
|
| 262 |
+
studio = ap.ACEStepStudio() # reads ACE_LM_MODEL at construction
|
| 263 |
|
| 264 |
+
_call(studio, "generate")
|
|
|
|
| 265 |
|
| 266 |
err = capsys.readouterr().err
|
| 267 |
assert "[ams]" in err
|
| 268 |
assert "no-such-lm" in err
|
| 269 |
assert "not found" in err
|
| 270 |
+
assert studio._llm is None
|
| 271 |
+
assert fake_generate.call_args.args[1] is None # the DiT runs alone
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
+
# A failed LM is not retried on the next Generate, and the DiT is untouched.
|
| 274 |
+
_call(studio, "generate")
|
| 275 |
fake_lm_cls.assert_called_once()
|
| 276 |
+
assert fake_generate.call_count == 2
|
| 277 |
+
assert fake_generate.call_args.args[1] is None
|
| 278 |
+
fake_dit_cls.assert_called_once()
|
| 279 |
+
studio._dit.initialize_service.assert_called_once()
|
| 280 |
|
| 281 |
|
| 282 |
def test_lm_init_exception_is_logged_and_not_fatal(monkeypatch, capsys):
|
| 283 |
+
studio, fake_dit_cls, fake_lm_cls, fake_generate = _cold_studio(
|
| 284 |
+
monkeypatch, lm_error=RuntimeError("mlx exploded")
|
| 285 |
+
)
|
| 286 |
|
| 287 |
+
_call(studio, "generate")
|
| 288 |
+
_call(studio, "generate")
|
| 289 |
|
| 290 |
err = capsys.readouterr().err
|
| 291 |
assert "[ams]" in err
|
| 292 |
assert "mlx exploded" in err
|
| 293 |
assert studio.is_loaded is True
|
| 294 |
assert studio._llm is None
|
| 295 |
+
assert [c.args[1] for c in fake_generate.call_args_list] == [None, None]
|
| 296 |
+
fake_lm_cls.assert_called_once()
|
| 297 |
+
fake_dit_cls.assert_called_once()
|
| 298 |
+
studio._dit.initialize_service.assert_called_once()
|
| 299 |
|
| 300 |
|
| 301 |
def test_dit_init_failure_raises_with_fork_status_and_is_retried(monkeypatch):
|
|
|
|
| 329 |
|
| 330 |
|
| 331 |
def test_generate_does_not_run_when_dit_init_fails(monkeypatch):
|
| 332 |
+
studio, _dit_cls, fake_lm_cls, fake_generate = _cold_studio(
|
| 333 |
+
monkeypatch, dit_result=("❌ Unknown DiT model", False)
|
| 334 |
+
)
|
|
|
|
| 335 |
with pytest.raises(RuntimeError, match="Unknown DiT model"):
|
| 336 |
+
_call(studio, "generate")
|
| 337 |
fake_generate.assert_not_called()
|
| 338 |
+
fake_lm_cls.assert_not_called()
|
| 339 |
+
|
| 340 |
+
|
| 341 |
+
def test_dit_load_logs_one_timing_line(monkeypatch, capsys):
|
| 342 |
+
_install_fake_handlers(monkeypatch)
|
| 343 |
+
|
| 344 |
+
studio = ap.ACEStepStudio()
|
| 345 |
+
studio._ensure_loaded()
|
| 346 |
+
studio._ensure_loaded() # already loaded: no second line
|
| 347 |
+
|
| 348 |
+
err = capsys.readouterr().err.splitlines()
|
| 349 |
+
lines = [ln for ln in err if ln.startswith("[ams] pipeline load:")]
|
| 350 |
+
assert len(lines) == 1
|
| 351 |
+
assert re.search(r"DiT/VAE/text encoder \d+\.\d s", lines[0]), lines[0]
|
| 352 |
+
assert not [ln for ln in err if ln.startswith("[ams] 5Hz LM load:")]
|
| 353 |
|
| 354 |
|
| 355 |
@pytest.mark.parametrize(
|
| 356 |
("lm_result", "active"),
|
| 357 |
[(("LM ready", True), "yes"), (("❌ 5Hz LM model not found at /x", False), "no")],
|
| 358 |
)
|
| 359 |
+
def test_lm_load_logs_one_timing_line_when_it_happens(monkeypatch, capsys, lm_result, active):
|
| 360 |
+
studio, *_ = _cold_studio(monkeypatch, lm_result=lm_result)
|
| 361 |
|
| 362 |
+
_call(studio, "cover")
|
| 363 |
+
assert not [ln for ln in capsys.readouterr().err.splitlines() if "5Hz LM load:" in ln]
|
|
|
|
| 364 |
|
| 365 |
+
_call(studio, "generate")
|
| 366 |
+
_call(studio, "generate")
|
| 367 |
+
lines = [ln for ln in capsys.readouterr().err.splitlines() if ln.startswith("[ams] 5Hz LM load:")]
|
| 368 |
assert len(lines) == 1
|
| 369 |
+
assert re.search(r"5Hz LM load: \d+\.\d s", lines[0]), lines[0]
|
| 370 |
+
assert f"LM active: {active}" in lines[0]
|
|
|
|
|
|
|
| 371 |
|
| 372 |
|
| 373 |
def _install_fake_inference(monkeypatch, success=True, audios=None, error=None):
|