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 on Apple Silicon failing every generation that has reference images

[._.]/ Adam Eivy7a6605a4c628
diff --git a/scripts/z_image_turbo.py b/scripts/z_image_turbo.py
index b6a03105ad..9aa1af640f 100644
--- a/scripts/z_image_turbo.py
+++ b/scripts/z_image_turbo.py
@@ -191,6 +191,18 @@ def to_i2i_pipeline(pipe):
return AutoPipelineForImage2Image.from_pipe(pipe)
+def encode_references_in_vae_dtype(pipe):
+ """Qwen 2.1 reference images reach the VAE encoder in the transformer's bfloat16,
+ which the float32 MPS VAE rejects ("Input type (BFloat16) and bias type (float)").
+ Encode in the VAE's dtype and hand the latents back in the caller's dtype."""
+ encode_vae_image = pipe._encode_vae_image
+
+ def encode_in_vae_dtype(image, generator):
+ return encode_vae_image(image.to(dtype=pipe.vae.dtype), generator).to(dtype=image.dtype)
+
+ pipe._encode_vae_image = encode_in_vae_dtype
+
+
def decode_qwen21_latents(pipe, latents, height: int, width: int):
vae = pipe.vae
unpacked = pipe._unpack_latents(latents, height, width, pipe.vae_scale_factor)
@@ -320,6 +332,7 @@ def main() -> None:
qwen21_mps = args.pipeline_class == "QwenImage21Pipeline" and device == "mps"
if qwen21_mps:
pipe.vae.to(dtype=torch.float32)
+ encode_references_in_vae_dtype(pipe)
seed = args.seed if args.seed is not None else int(torch.randint(0, 2**31 - 1, (1,)).item())
generator = make_generator(device, seed)
diff --git a/scripts/z_image_turbo_test.py b/scripts/z_image_turbo_test.py
index fea2958333..4ee42ee669 100644
--- a/scripts/z_image_turbo_test.py
+++ b/scripts/z_image_turbo_test.py
@@ -104,6 +104,28 @@ class RunnerContract(unittest.TestCase):
self.assertIn('QwenImage21Pipeline', stderr.getvalue())
self.assertIn('FLUX2_FORCE_REINSTALL=1', stderr.getvalue())
+ def test_qwen21_reference_images_encode_in_the_vae_dtype(self):
+ with patch.dict(sys.modules, {'torch': SimpleNamespace()}):
+ runner = importlib.import_module('z_image_turbo')
+ seen = []
+
+ class Tensor:
+ def __init__(self, dtype):
+ self.dtype = dtype
+
+ def to(self, *, dtype):
+ return Tensor(dtype)
+
+ def encode(image, _generator):
+ seen.append(image.dtype)
+ return Tensor(image.dtype)
+
+ pipe = SimpleNamespace(vae=SimpleNamespace(dtype='fp32'), _encode_vae_image=encode)
+ runner.encode_references_in_vae_dtype(pipe)
+ latents = pipe._encode_vae_image(Tensor('bf16'), None)
+ self.assertEqual(seen, ['fp32'])
+ self.assertEqual(latents.dtype, 'bf16')
+
def test_qwen21_decode_guard_accepts_finite_output_and_retries_once(self):
torch = SimpleNamespace(
float32='fp32',