Skip to content

fix(mps): prevent Metal command buffer double-commit crash on 5B quanto models#1773

Closed
huangyebiaoke wants to merge 1 commit into
deepbeepmeep:MPSfrom
huangyebiaoke:fix/mps-5b-commandbuffer-crash
Closed

fix(mps): prevent Metal command buffer double-commit crash on 5B quanto models#1773
huangyebiaoke wants to merge 1 commit into
deepbeepmeep:MPSfrom
huangyebiaoke:fix/mps-5b-commandbuffer-crash

Conversation

@huangyebiaoke

@huangyebiaoke huangyebiaoke commented May 8, 2026

Copy link
Copy Markdown

Root Cause

Wan 2.2 5B uses quanto mbf16 quantization. Quando ops lack native MPS kernels and fallback to CPU via PYTORCH_ENABLE_MPS_FALLBACK=1. These CPU fallback ops corrupt the MPS command buffer when mixed with native MPS SDPA attention.

The crash happens immediately after first inference step because the first quantized weight access + attention operation triggers the corruption.

Why 14B works but 5B doesn't

  • 14B uses standard safetensors weights -> all ops stay on MPS -> clean
  • 5B uses quanto mbf16 -> weight access ops fallback to CPU -> buffer corruption

Fix (2 changes)

  1. device_patch.py: Add torch.mps.synchronize() before SDPA to flush pending fallback operations
  2. attention.py: Add .contiguous() after .to() and .expand() to ensure tensor buffer independence

Testing Needed

@SquishedSquirrel could you re-test Wan2.2 5B (with and without fastwan) on your M3 Max 36GB?

Ref: #1750 (comment)

…to models

Root cause: Wan 2.2 5B uses quanto mbf16 quantization whose ops lack native MPS
kernels and fallback to CPU via PYTORCH_ENABLE_MPS_FALLBACK=1. These CPU fallback
ops corrupt the MPS command buffer when mixed with native MPS SDPA attention,
triggering: IOGPUMetalCommandBuffer validate:213: failed assertion
'commit an already committed command buffer'.

Fixes (2 changes):
1. device_patch.py: Add torch.mps.synchronize() before SDPA to flush pending
   fallback operations before entering the MPS attention kernel.
2. attention.py: Add .contiguous() after .to() and .expand() calls in
   pay_attention() to ensure tensor buffer independence and prevent shared
   buffer references from being committed twice.

Tested by: NOT YET (needs M3/M4 Mac with 36GB to test 5B quanto model)
Ref: deepbeepmeep#1750 (comment)
@huangyebiaoke

Copy link
Copy Markdown
Author

Update: PR #1775 implements the real fix — skip quanto quantization entirely on MPS by selecting mbf16 model files. This PR (#1773) is still a valid safety net for edge cases, but #1775 should prevent the crash from happening in the first place.

@huangyebiaoke

Copy link
Copy Markdown
Author

Superseded by #1776 which adds manual fallback for macOS 26.x Metal bugs on top of these fixes. Closing.

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.

1 participant