Fix MPS upscale/VAE failures: enforce contiguous tiled input and safer split-attention chunking#12463
Fix MPS upscale/VAE failures: enforce contiguous tiled input and safer split-attention chunking#12463fritzprix wants to merge 2 commits intoComfy-Org:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes Apple Silicon/MPS runtime failures in upscale and VAE encode workflows by addressing tensor contiguity issues and attention computation limits.
Changes:
- Added
.contiguous()calls to tiled upscaling to ensure memory layout compatibility with model view operations - Improved VAE attention slice sizing with proper ceil-based chunking (fixes incorrect logic for non-evenly-divisible cases)
- Added MPS-specific safeguards to prevent INT_MAX tensor size errors with automatic retry logic
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| comfy/utils.py | Added contiguous() calls before passing tiled tensors to model functions, fixing "view size is not compatible" errors on MPS |
| comfy/ldm/modules/diffusionmodules/model.py | Improved slice_attention chunking with ceil-based sizing, MPS INT_MAX safeguards, and retry logic for MPS-specific errors |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| del s2 | ||
| break | ||
| except RuntimeError as e: | ||
| if q.device.type == "mps" and ("INT_MAX" in str(e) or "MPSGraph" in str(e) or "MPSGaph" in str(e)): |
There was a problem hiding this comment.
There appears to be a typo in the error string check: "MPSGaph" should likely be "MPSGraph". This typo means the error handling won't catch errors containing "MPSGraph" misspelled in this way. Consider removing this misspelled variant unless it's intentionally included to handle a known typo in MPS error messages.
| if q.device.type == "mps" and ("INT_MAX" in str(e) or "MPSGraph" in str(e) or "MPSGaph" in str(e)): | |
| if q.device.type == "mps" and ("INT_MAX" in str(e) or "MPSGraph" in str(e)): |
| del s2 | ||
| break | ||
| except RuntimeError as e: | ||
| if q.device.type == "mps" and ("INT_MAX" in str(e) or "MPSGraph" in str(e) or "MPSGaph" in str(e)): |
There was a problem hiding this comment.
Did you consider moving this complex logic to a new function?
Summary
This PR fixes two Apple Silicon/MPS runtime failures seen in upscale + VAE encode workflows.
Problem
On MPS, some workflows fail with:
view size is not compatible with input tensor's size and strideMPSGraph does not support tensor dims larger than INT_MAXRoot cause
view-compatible memory layout.Changes
s.contiguous(),s_in.contiguous()).slice_attention:Impact / compatibility
Validation
#11851