Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: reassert the MLX cache ceiling before the extend VAE decode too (#5421)

[._.]/ Adam Eivy0833d947e4a7
diff --git a/scripts/generate_ltx2.py b/scripts/generate_ltx2.py
index 2da013308c..7216bf9e0b 100644
--- a/scripts/generate_ltx2.py
+++ b/scripts/generate_ltx2.py
@@ -1090,7 +1090,7 @@ def _run_with_ltx_stepwise_preview(pipe, args: argparse.Namespace, render):
# Every mode routes its render through here, so this is where the allocator
# cache ceiling is reasserted: loading weights resets it, and a two-stage or
# chained job renders more than once inside one process.
- apply_mlx_cache_policy(_MLX_CACHE_POLICY)
+ reassert_mlx_cache_policy()
restore = _install_ltx_stepwise_preview(pipe, args)
try:
return render()
@@ -1491,6 +1491,10 @@ def run_extend(args: argparse.Namespace) -> str:
pipe.feature_extractor = None
pipe._loaded = False
aggressive_cleanup()
+ # The full-res VAE decode below is this mode's largest allocation, and the
+ # cleanup above releases the allocator limit along with the buffers when it
+ # runs — so reassert the ceiling before the decoders come back in.
+ reassert_mlx_cache_policy()
pipe._load_decoders()
bind_output_fps(pipe, args.fps)
return pipe._decode_and_save_video(video_latent, audio_latent, args.output)
@@ -1818,6 +1822,19 @@ def apply_mlx_cache_policy(policy: dict, announce: bool = False) -> bool:
return True
+def reassert_mlx_cache_policy() -> bool:
+ """Re-apply this run's cache ceiling. Silent — startup already announced it.
+
+ Called at every boundary where the allocator limit can have been reset since
+ it was installed: before each render, and before the extend decode (which
+ frees the DiT behind an aggressive_cleanup() and pulls the VAE back in, the
+ single largest allocation of that mode). What a pipeline does INSIDE one call
+ — a stage-2 transformer reload — is out of this wrapper's reach; the cap
+ simply resumes at the next boundary.
+ """
+ return apply_mlx_cache_policy(_MLX_CACHE_POLICY)
+
+
def configure_mlx_cache(args: argparse.Namespace) -> dict:
"""Resolve this run's allocator-cache ceiling and install it. Returns the policy.
diff --git a/scripts/generate_ltx2.test.js b/scripts/generate_ltx2.test.js
index 3a6e350328..1066f4a1cf 100644
--- a/scripts/generate_ltx2.test.js
+++ b/scripts/generate_ltx2.test.js
@@ -1000,9 +1000,10 @@ describe.skipIf(!pyBin)('generate_ltx2.py MLX allocator-cache policy', () => {
it('installs the run policy the render path then reasserts', () => {
const result = runJson([
...MODERN_MLX,
- 'import json',
+ 'import contextlib, io, json',
'from types import SimpleNamespace',
- 'policy = runner.configure_mlx_cache(SimpleNamespace(mlx_cache_limit_mb="2048"))',
+ 'with contextlib.redirect_stderr(io.StringIO()):',
+ ' policy = runner.configure_mlx_cache(SimpleNamespace(mlx_cache_limit_mb="2048"))',
'runner._install_ltx_stepwise_preview = lambda pipe, args: (lambda: None)',
'runner._run_with_ltx_stepwise_preview(object(), SimpleNamespace(), lambda: None)',
'print(json.dumps({"policy": policy, "stored": runner._MLX_CACHE_POLICY, "calls": CALLS}))',