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) 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)