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 Qwen Image 2.1 renders on Apple Silicon failing at the final decode (#11500)

Stephen Hilderbrand5113a735101d
diff --git a/scripts/z_image_turbo.py b/scripts/z_image_turbo.py
index 51d7a5bc11..48e6414ea4 100644
--- a/scripts/z_image_turbo.py
+++ b/scripts/z_image_turbo.py
@@ -20,6 +20,7 @@ encoder (e.g. HiDream-I1 loads Llama-3.1-8B-Instruct as `text_encoder_4`), so
"""
import argparse
+import gc
import inspect
import os
import sys
@@ -208,7 +209,24 @@ def encode_references_in_vae_dtype(pipe):
pipe._encode_vae_image = encode_in_vae_dtype
+def release_denoising_components(pipe) -> None:
+ """Drop the transformer and text encoder once the denoising loop is done.
+
+ The full Qwen 2.1 pipeline sits on MPS with no offload, and its float32 VAE
+ decode needs another few GiB on top — on a 32 GB Mac that crossed the MPS
+ high-watermark and failed a finished 40-step render at its very last step.
+ The runner is one-shot per image, so nothing needs these modules again."""
+ for name in ("transformer", "text_encoder"):
+ if getattr(pipe, name, None) is not None:
+ setattr(pipe, name, None)
+ gc.collect()
+ mps = getattr(torch, "mps", None)
+ if mps is not None and torch.backends.mps.is_available():
+ mps.empty_cache()
+
+
def decode_qwen21_latents(pipe, latents, height: int, width: int):
+ release_denoising_components(pipe)
vae = pipe.vae
unpacked = pipe._unpack_latents(latents, height, width, pipe.vae_scale_factor)
diff --git a/scripts/z_image_turbo_test.py b/scripts/z_image_turbo_test.py
index f53b77f9bf..2eb8b371e7 100644
--- a/scripts/z_image_turbo_test.py
+++ b/scripts/z_image_turbo_test.py
@@ -83,6 +83,8 @@ class FakeQwenDecodePipeline:
def __init__(self, outcomes):
self.vae = FakeVae(outcomes)
+ self.transformer = object()
+ self.text_encoder = object()
self.image_processor = SimpleNamespace(postprocess=lambda decoded, output_type: [('image', decoded, output_type)])
def _unpack_latents(self, _latents, _height, _width, _scale_factor):
@@ -154,6 +156,10 @@ class RunnerContract(unittest.TestCase):
self.assertEqual(images[0][0], 'image')
self.assertEqual(images[0][2], 'pil')
self.assertEqual(pipe.vae.decode_dtypes, expected_calls)
+ # The denoiser and text encoder must not stay resident through
+ # the float32 decode — that combination OOMs a 32 GB Mac.
+ self.assertIsNone(pipe.transformer)
+ self.assertIsNone(pipe.text_encoder)
self.assertEqual('recovered after a float32 retry' in stderr.getvalue(), expect_retry_log)
def test_qwen21_decode_guard_fails_before_writing_non_finite_output(self):