Skip to content

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

Open
fritzprix wants to merge 8 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 8 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?

@coderabbitai
Copy link

coderabbitai bot commented Feb 21, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between be61a53 and f70e030.

📒 Files selected for processing (1)
  • comfy/utils.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • comfy/utils.py

📝 Walkthrough

Walkthrough

Replaces integer-based attention slicing with ceil division and a minimum slice size; adds an MPS-specific cap on slice_size derived from q/k shapes to avoid hardware overflow. Introduces MPS-targeted RuntimeError recovery: on certain MPS errors it clears cache, doubles steps, and retries up to a cap (4096), re-raising non-MPS errors. Preserves existing OOM handling (soft cache clear and steps doubling). Also changes two utility call sites to pass tensors as contiguous (.contiguous()). Lines changed across files: ~+13/-1 (plus small util edits).

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the PR's main changes: fixing MPS failures through contiguous tensor handling and safer attention chunking.
Description check ✅ Passed The description clearly relates to the changeset, explaining the root causes, changes made, and impact of the fixes with good detail.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@comfy-pr-bot
Copy link
Member

Test Evidence Check

⚠️ Warning: Visual Documentation Missing

If this PR changes user-facing behavior, visual proof (screen recording or screenshot) is required. PRs without applicable visual documentation may not be reviewed until provided.

You can add it by:

  • GitHub: Drag & drop media directly into the PR description
  • YouTube: Include a link to a short demo

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.

4 participants