Skip to content

Fix MPS upscale/VAE failures: enforce contiguous tiled input and safer split-attention chunking#12463

Open
fritzprix wants to merge 2 commits intoComfy-Org:masterfrom
fritzprix:fix/mac-upscale-fail
Open

Fix MPS upscale/VAE failures: enforce contiguous tiled input and safer split-attention chunking#12463
fritzprix wants to merge 2 commits intoComfy-Org:masterfrom
fritzprix:fix/mac-upscale-fail

Conversation

@fritzprix
Copy link

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 stride
  • MPSGraph does not support tensor dims larger than INT_MAX

Root cause

  1. Tiled upscale inputs can become non-contiguous after slicing/narrowing, which breaks models that assume view-compatible memory layout.
  2. VAE split-attention chunking could still produce oversized matmul shapes on MPS for large token counts.

Changes

  • In tiled scaling, pass contiguous tiles into model calls (s.contiguous(), s_in.contiguous()).
  • In VAE slice_attention:
    • use robust ceil-based chunk sizing,
    • cap per-slice size for MPS INT_MAX safety,
    • retry with increased steps on MPS INT_MAX/MPSGraph runtime errors.

Impact / compatibility

  • No intended functional change in outputs.
  • Main tradeoff: potentially slower attention on very large MPS workloads due to smaller chunks.
  • CUDA/CPU behavior is unchanged except for safer chunk sizing logic.

Validation

  • Reproduced failing upscale + VAE path on macOS/MPS before patch.
  • Confirmed workflow completes after patch.

#11851

Copilot AI review requested due to automatic review settings February 14, 2026 05:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)):
Copy link

Copilot AI Feb 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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)):

Copilot uses AI. Check for mistakes.
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)):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you consider moving this complex logic to a new function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants