Repository navigation
Fix MPS crashes: float64 swap scores and guidance map resize - #69
Merged
hvoss-tech merged 2 commits intoNov 18, 2026
Merged
Conversation
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 <noreply@anthropic.com>
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 hvoss-tech#70. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Owner
|
Thank you very much for the PR :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #68.
Fixes #70.
Two MPS-only crashes on Apple Silicon. Both come from ops MPS doesn't support, and both are fixed by doing a small one-off step on the CPU.
1. float64 cast with "Swaps: At most" (#68)
Problem
With Swaps: At most enabled on
mps, the optimization crashes withCannot convert a MPS Tensor to float64 dtype.score.detach().to("cpu", torch.float64)attempts the float64 cast on the MPS device, which has no float64.Change
In
src/autoforge/Helper/ConstraintHelper.py, copy to the host first, then cast:Applied in both call sites (
_viterbi_swapsandproject_assignment). No behavior change on CPU/CUDA.2. "Adaptive pool MPS" in height guidance pixel targets (#70)
Problem
Once the Depth Anything model is available, the WebUI adds default depth height guidance (
{"strength": 1.0, "depth": True}). Onmps, every such run crashes at the first discrete check:HeightGuidance.maps()shrinks the full-resolution target and weight maps to the solving size withF.interpolate(mode="area"), which isadaptive_avg_pool2d. MPS supports it only when the input size is an exact multiple of the output size (pytorch/pytorch#96056), and these sizes usually aren't. Offline without the model cached, the default guidance is dropped silently, which hides the crash.Change
In
src/autoforge/Helper/HeightGuidance.py(maps()), when downsizing withareaon an MPS tensor, interpolate on the CPU and move the result back to the device. This follows the workaround already inOptimizer.py(around line 812).maps()caches each shape, so the CPU step runs once per size, not every iteration. No behavior change on CPU/CUDA, and upsampling (bilinear) is unchanged.Verification
_viterbi_swapson a randommpsscore tensor runs and returns the expected shape (previously raised the float64 TypeError).DeterministicOps.segment_gather,_cluster._segment_sums) are already guarded byDeviceUtils.has_float64.test_maps_downsize_to_non_divisible_shape_on_mps(skipped without MPS) shrinks a 1000×750 map to 333×250 onmps. It failed with theAdaptive pool MPSerror before the change. It now passes, and the result matches the CPU result within1e-5.tests/test_height_guidance.py: 25 passed, 4 skipped on Apple Silicon (torch 2.12.0).mode="area"/adaptive_avg_pooluses remain insrc/autoforgeoutside the already-guardedOptimizer.pypath.🤖 Generated with Claude Code