Skip to content

Fix VAE decode patch kwargs compatibility with ComfyUI - #13

Merged
lmangani merged 3 commits into
mainfrom
copilot/patch-vae-decode-for-mps-limits
Feb 18, 2026
Merged

lmangani merged 3 commits into
mainfrom
copilot/patch-vae-decode-for-mps-limits

Conversation

Copilot AI commented Feb 18, 2026 •

Copy link
Copy Markdown

Problem

The VAE decode patch was failing at runtime with TypeError: VAE.decode() got an unexpected keyword argument 'disable_patcher'. The patched function explicitly passed disable_patcher to ComfyUI's VAE.decode(), but that parameter doesn't exist in the actual implementation.

Changes

fp8_mps_patch.py - Simplified patched_decode signature and calls:

# Before
def patched_decode(self, samples_in, disable_patcher=False, **kwargs):
    return original_decode(self, samples_in, disable_patcher=disable_patcher, **kwargs)

# After  
def patched_decode(self, samples_in, **kwargs):
    return original_decode(self, samples_in, **kwargs)

Applied to all 3 original_decode call sites (lines 404, 428, 437).

test_decode_signature.py - Added test coverage validating kwargs pass-through behavior with and without the disable_patcher parameter.

Impact

  • Patch now accepts any caller kwargs without assumptions about the original method's signature
  • Forward/backward compatible with different ComfyUI versions
  • MPS tensor handling (tiling, CPU fallback) unchanged
  • PYTORCH_ENABLE_MPS_FALLBACK=1 already set correctly (line 451)
Original prompt

This section details on the original issue you should resolve

<issue_title>Exception during processing: MPSGaph does not support tensor dims larger than INT_MAX on Apple MPS</issue_title>
<issue_description>Exception during processing: MPSGaph does not support tensor dims larger than INT_MAX

The following patch has been proposed:

monkey-patch from your custom node. Add this to your fp8-mps-metal init.py:
pythondef patch_vae_decode_for_mps_limits():
"""Force large VAE decodes to CPU to avoid MPS INT_MAX tensor size limit"""
import comfy.sd

original_decode = comfy.sd.VAE.decode

def patched_decode(self, samples_in, disable_patcher=False, **kwargs):
    # Check if samples are huge and on MPS
    if hasattr(samples_in, 'device') and samples_in.device.type == 'mps':
        numel = samples_in.numel()
        # If tensor has >100M elements, move to CPU to avoid MPS INT_MAX limit
        if numel > 100_000_000:
            print(f"[fp8-mps-metal] VAE decode tensor too large for MPS ({numel:,} elements), falling back to CPU")
            samples_in = samples_in.to('cpu')
            self.first_stage_model = self.first_stage_model.to('cpu')
            self.output_device = torch.device('cpu')
    
    return original_decode(self, samples_in, disable_patcher=disable_patcher, **kwargs)

comfy.sd.VAE.decode = patched_decode

Then in the install() function in fp8_mps_patch.py, add:
pythondef install():
"""Monkey-patch torch.scaled_mm, Tensor.to(), Tensor.copy(), and VAE.decode to use Metal FP8 kernels on MPS."""
global _original_scaled_mm, _original_tensor_to, _original_tensor_copy

# ... existing patches ...

# Patch VAE.decode for MPS tensor size limits
patch_vae_decode_for_mps_limits()

print("=" * 70)
print("✓ FP8 MPS Metal patch installed successfully!")
print("=" * 70)
# ... rest of install ...

Add the import at the top of fp8_mps_patch.py:
pythonimport torch
import comfy.sd # Add this
This way the patch lives entirely in your custom node and doesn't touch ComfyUI core files. The VAE will automatically fall back to CPU when tensors are too large for MPS.

We also want our extension to force export PYTORCH_ENABLE_MPS_FALLBACK=1 for any other uncaught exception.</issue_description>

Comments on the Issue (you are @copilot in this section)

@lmangani REOPENING. Strategy Thresholds Test fails with

% python3 mps_validation_script.py

======================================================================
FP8-MPS-METAL: Apple MPS Hardware Validation

This script validates the VAE tiling strategy on real MPS hardware

======================================================================
MPS AVAILABILITY CHECK

✓ MPS is available
PyTorch version: 2.8.0
MPS built: True

======================================================================
BASIC MPS TENSOR OPERATIONS

✓ Created tensor on MPS: torch.Size([100, 100]), device: mps:0
✓ Matrix multiply successful: torch.Size([100, 100])
✓ Device transfers working: CPU ↔ MPS

======================================================================
PATCH INSTALLATION TEST

[fp8-mps-metal] Set PYTORCH_ENABLE_MPS_FALLBACK=1 for automatic CPU fallback
[fp8-mps-metal] ComfyUI not available, skipping VAE decode patch
✓ Patch installed
✓ PYTORCH_ENABLE_MPS_FALLBACK = 1
✓ Patch state: installed

======================================================================
VAE TILING WITH MPS TENSORS

Test 1: Small tensor on MPS (no tiling)
Input: torch.Size([1, 4, 64, 64]), device: mps:0
Tiles: 1
✓ Pass

Test 2: Large tensor on MPS (requires tiling)
Input: torch.Size([1, 4, 512, 512]), device: mps:0, elements: 1,048,576
Tiles: 3
Tile 0: torch.Size([1, 4, 171, 512]), device: mps:0, elements: 350,208
Tile 1: torch.Size([1, 4, 171, 512]), device: mps:0, elements: 350,208
Tile 2: torch.Size([1, 4, 170, 512]), device: mps:0, elements: 348,160
✓ Pass: All tiles remain on MPS

Test 3: Tile reconstruction
Reconst...</comment_new>
<comment_new>@lmangani
REOPENING - the pipeline is still failing with thie error on Apple MPS

ComfyUI Error Report

Error Details

  • Node ID: 8
  • Node Type: VAEDecode
  • Exception Type: TypeError
  • Exception Message: VAE.decode() got an unexpected keyword argument 'disable_patcher'

Stack Trace

  File "/Applications/ComfyUI.app/Contents/Resources/ComfyUI/execution.py", line 530, in execute
    output_data, output_ui, has_subgraph, has_pending_t...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes audiohacking/fp8-mps-metal#10

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey).

Copilot AI and others added 2 commits February 18, 2026 12:30
Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix tensor size limit exception on Apple MPS Fix VAE decode patch kwargs compatibility with ComfyUI Feb 18, 2026
Copilot AI requested a review from lmangani February 18, 2026 12:36
@lmangani
lmangani marked this pull request as ready for review February 18, 2026 12:46
@lmangani
lmangani merged commit 7711653 into main Feb 18, 2026
2 checks passed
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