-
Notifications
You must be signed in to change notification settings - Fork 15.4k
fix: don't default the Stable Audio 3 VAE to bf16 on MPS #15207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ChrisLundquist
wants to merge
1
commit into
Comfy-Org:master
Choose a base branch
from
ChrisLundquist:fix-sa3-audio-vae-bf16-mps
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+94
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """Stable Audio 3 VAE dtype selection on Apple Silicon (MPS). | ||
|
|
||
| The SA3 audio decoder cannot run in bf16 on backends whose attention | ||
| softmax runs in the input dtype (mps, cpu): its DyT qk norms emit values | ||
| around +-30..50, attention logits land where bf16's 8-bit mantissa | ||
| quantizes in steps of ~0.25-0.5, and the decoded audio is broadband | ||
| noise, fully decorrelated from the fp32 decode (corr ~0). fp16 matches | ||
| fp32 (corr 0.9997). The VAE must therefore not default to bf16 on mps, | ||
| while CUDA keeps its bf16 default. | ||
| """ | ||
|
|
||
| import pytest | ||
| import torch | ||
| import torch.nn as nn | ||
|
|
||
| from comfy.cli_args import args as cli_args | ||
|
|
||
| if not (torch.cuda.is_available() or torch.backends.mps.is_available()): | ||
| cli_args.cpu = True | ||
|
|
||
| import comfy.ldm.audio.vae_sa3 # noqa: E402 | ||
| import comfy.memory_management # noqa: E402 | ||
| import comfy.model_management as mm # noqa: E402 | ||
| import comfy.sd # noqa: E402 | ||
|
|
||
| mps_only = pytest.mark.skipif( | ||
| not (torch.backends.mps.is_available() and mm.is_device_mps(mm.get_torch_device())), | ||
| reason="requires an Apple Silicon MPS device", | ||
| ) | ||
|
|
||
|
|
||
| def sa3_state_dict(): | ||
| # Routes VAE() into the Stable Audio 3 branch (small config). | ||
| return {"decoder.layers.3.transformers.0.pre_norm.alpha": torch.zeros(1)} | ||
|
|
||
|
|
||
| class StubSA3AudioVAE(nn.Module): | ||
| # Stands in for SA3AudioVAE so tests don't build the full 100M+ param | ||
| # module; dtype selection under test happens outside the model class. | ||
| def __init__(self, *args, **kwargs): | ||
| super().__init__() | ||
| self.linear = nn.Linear(2, 2) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def sa3_vae_factory(monkeypatch): | ||
| def build(device): | ||
| monkeypatch.setattr(comfy.ldm.audio.vae_sa3, "SA3AudioVAE", StubSA3AudioVAE) | ||
| monkeypatch.setattr(comfy.memory_management, "aimdo_enabled", False) | ||
| monkeypatch.setattr(mm, "vae_device", lambda: device) | ||
| return comfy.sd.VAE(sd=sa3_state_dict()) | ||
|
|
||
| return build | ||
|
|
||
|
|
||
| def test_sa3_audio_vae_does_not_default_to_bf16_on_mps(monkeypatch, sa3_vae_factory): | ||
| # On macOS >= 14 should_use_bf16() approves bf16 for mps, which is the | ||
| # dtype the audio decode corrupts under; pin the version so the test | ||
| # exercises that path on any host. | ||
| monkeypatch.setattr(mm, "mac_version", lambda: (15, 0)) | ||
| vae = sa3_vae_factory(torch.device("mps")) | ||
| assert vae.vae_dtype != torch.bfloat16 | ||
| assert vae.vae_dtype == torch.float16 | ||
|
|
||
|
|
||
| def test_sa3_audio_vae_keeps_bf16_default_on_cuda(monkeypatch, sa3_vae_factory): | ||
| # The mps exclusion must not leak: CUDA-like devices (bf16 approved) | ||
| # keep preferring bf16. | ||
| monkeypatch.setattr(mm, "should_use_fp16", lambda device=None, **kwargs: True) | ||
| monkeypatch.setattr(mm, "should_use_bf16", lambda device=None, **kwargs: True) | ||
| vae = sa3_vae_factory(torch.device("cuda")) | ||
| assert vae.vae_dtype == torch.bfloat16 | ||
|
|
||
|
|
||
| def test_sa3_audio_vae_explicit_dtype_still_wins(sa3_vae_factory, monkeypatch): | ||
| # --bf16-vae style overrides bypass working_dtypes entirely. | ||
| monkeypatch.setattr(comfy.ldm.audio.vae_sa3, "SA3AudioVAE", StubSA3AudioVAE) | ||
| monkeypatch.setattr(comfy.memory_management, "aimdo_enabled", False) | ||
| vae = comfy.sd.VAE(sd=sa3_state_dict(), device=torch.device("cpu"), dtype=torch.bfloat16) | ||
| assert vae.vae_dtype == torch.bfloat16 | ||
|
|
||
|
|
||
| @mps_only | ||
| def test_sa3_audio_vae_picks_fp16_on_mps_hardware(monkeypatch, sa3_vae_factory): | ||
| vae = sa3_vae_factory(mm.get_torch_device()) | ||
| assert vae.vae_dtype == torch.float16 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Reduce the MPS precision comment.
Keep one short comment that states that MPS BF16 decoding causes noise and FP16 is used instead. The detailed measurements and attention analysis are not needed in this hot-path selection block.
As per path instructions, “use sparse comments only where the MPS/bf16 limitation needs clarification.”
🤖 Prompt for AI Agents
Source: Path instructions