From bcc8beacf1074d83038d7bf13dfa88a745a7081b Mon Sep 17 00:00:00 2001 From: Israel Shok Date: Wed, 18 Nov 2026 12:52:00 +0200 Subject: [PATCH 1/2] Copy swap scores to the host before casting to float64 On MPS, .to("cpu", torch.float64) casts on the device first, and MPS has no float64, so setting "Swaps: At most" crashed with "Cannot convert a MPS Tensor to float64 dtype". Move to the CPU first, then cast. Co-Authored-By: Claude Opus 5.5 --- src/autoforge/Helper/ConstraintHelper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/autoforge/Helper/ConstraintHelper.py b/src/autoforge/Helper/ConstraintHelper.py index 5aa8702..70e279f 100644 --- a/src/autoforge/Helper/ConstraintHelper.py +++ b/src/autoforge/Helper/ConstraintHelper.py @@ -41,7 +41,7 @@ def _viterbi_swaps(score: torch.Tensor, max_swaps: int, base: Optional[int] = No """ dev = score.device return torch.from_numpy( - _viterbi_swaps_np(score.detach().to("cpu", torch.float64).numpy(), max_swaps, base) + _viterbi_swaps_np(score.detach().cpu().to(torch.float64).numpy(), max_swaps, base) ).to(dev) @@ -367,7 +367,7 @@ def project_assignment( # Greedy palette and swap DP both on the host, from one copy: as # small GPU kernels they were ~1000 launches and a sync per call. dev = score.device - sc = score.detach().to("cpu", torch.float64).numpy() + sc = score.detach().cpu().to(torch.float64).numpy() mask = _palette_masks_greedy_np(sc, max_colors, mat) sc = np.where(mask[:, None, :], sc, -1e9) out_np = _viterbi_swaps_np(sc, max_swaps, free) if need_swaps else sc.argmax(-1) From e1a441625513cdd9ee81996e0c4c229313e5a459 Mon Sep 17 00:00:00 2001 From: Israel Shok Date: Wed, 18 Nov 2026 21:18:49 +0200 Subject: [PATCH 2/2] Resize guidance maps on the CPU when MPS can't pool them MPS implements "area" interpolation (adaptive_avg_pool2d) only when the input size is an exact multiple of the output size. HeightGuidance.maps shrinks the depth target to the solving size, which usually doesn't divide evenly, so every MPS run with depth guidance crashed at the first discrete check. Do that resize on the CPU and move the result back; maps() caches each shape, so it runs once per size. Fixes #70. Co-Authored-By: Claude Opus 5.5 --- src/autoforge/Helper/HeightGuidance.py | 9 +++++++-- tests/test_height_guidance.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/autoforge/Helper/HeightGuidance.py b/src/autoforge/Helper/HeightGuidance.py index a2dcf2a..025610f 100644 --- a/src/autoforge/Helper/HeightGuidance.py +++ b/src/autoforge/Helper/HeightGuidance.py @@ -492,8 +492,13 @@ def maps(self, shape): if tuple(t.shape) != shape: mode = "area" if t.shape[0] * t.shape[1] > shape[0] * shape[1] else "bilinear" kw = {} if mode == "area" else {"align_corners": False} - t = F.interpolate(t[None, None], size=shape, mode=mode, **kw)[0, 0] - w = F.interpolate(w[None, None], size=shape, mode=mode, **kw)[0, 0] + dev = t.device + # "area" is adaptive_avg_pool2d, which MPS only supports for + # divisible sizes; the result is cached, so resize on the CPU. + if mode == "area" and dev.type == "mps": + t, w = t.cpu(), w.cpu() + t = F.interpolate(t[None, None], size=shape, mode=mode, **kw)[0, 0].to(dev) + w = F.interpolate(w[None, None], size=shape, mode=mode, **kw)[0, 0].to(dev) hit = (t.contiguous(), w.contiguous()) if len(self._maps_cache) > 8: self._maps_cache.clear() diff --git a/tests/test_height_guidance.py b/tests/test_height_guidance.py index 23ac2fe..aa23cd6 100644 --- a/tests/test_height_guidance.py +++ b/tests/test_height_guidance.py @@ -588,3 +588,19 @@ def test_tiled_guided_proxy_matches_one_stack_per_program(): tiled = fc.proxy_losses(pal, t, w, guided=guided) single = torch.cat([fc.proxy_losses(pal[i : i + 1], t, w, guided=guided) for i in range(B)]) torch.testing.assert_close(tiled, single, rtol=1e-5, atol=1e-4) + + +@pytest.mark.skipif(not torch.backends.mps.is_available(), reason="needs MPS") +def test_maps_downsize_to_non_divisible_shape_on_mps(): + # "area" downsizing is adaptive_avg_pool2d, which MPS only implements + # for divisible sizes: 1000x750 -> 333x250 must still work there. + rng = np.random.default_rng(0) + tmap = rng.random((1000, 750)).astype(np.float32) + weight = rng.random((1000, 750)).astype(np.float32) + g = HeightGuidance(GuidanceSpec(strength=1), COLORS, 20, tmap, weight, device="mps") + t, w = g.maps((333, 250)) + assert t.device.type == "mps" and w.device.type == "mps" + assert tuple(t.shape) == (333, 250) and tuple(w.shape) == (333, 250) + ref = HeightGuidance(GuidanceSpec(strength=1), COLORS, 20, tmap, weight, device="cpu").maps((333, 250)) + assert torch.allclose(t.cpu(), ref[0], atol=1e-5) + assert torch.allclose(w.cpu(), ref[1], atol=1e-5)