You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: VAE decode crash at 2048x2048 and above on CUDA/HIP - #2492
At 2048x2048 and up on CUDA or HIP, the VAE decode crashes on the last step and no image is produced. The automatic tiling fallback does not kick in, and the only way to get images is forcing --vae-tiling, which is slower and is not an exact decode.
The VAE's attention compares every pixel with every other pixel in one pass, which at 2048x2048 is about 4.3 billion. That's more than the CUDA/HIP kernels can count, so ggml asserts:
Flash Attention doesn't support this layer, so the regular attention runs anyway.
We split those into smaller chunks once it gets large (1536x1536 and up), so each chunk stays within what the kernels can handle. The output is byte-identical to master, and anything that fits in one chunk runs exactly as before.
While datacenters have plenty of VRAM, this crashes regardless, but we also found that in consumer situations (much less VRAM available) this fix also holds and, together with the ggml conv2d fix, eliminated the OOM issue.
Related Issue / Discussion
This replaces #1621, which also had the tiling fallback. The review there asked to keep it to one change.
Two other crashes at these sizes are separate from this one:
CUDA/HIP has a separate problem in ggml (CUDA/HIP: fix conv2d out of memory abort when its scratch does not fit ggml-org/llama.cpp#33559): conv2d grabs a large chunk of memory at run time that sd.cpp can't plan for, so it can run out of memory even when everything else fits. This fix makes it more visible. At 2560x2560 on a 32 GB R9700, master never got that far, because the oversized attention ran out of memory first and forced tiling, so with this PR that run now aborts instead. With the ggml fix it decodes.
It also uses less memory: at 2048x2048 the attention needs 2 GB instead of 16. There's no speed cost we could measure: at 1536x1536, where it already splits, decode is 1.81s vs 1.84s on master. It's not FLUX-only: SD1.5 crashes the same way at 2048x2048 on master and decodes fine with this PR, and at 512 and 1024 its output is byte-identical.
It doesn't handle actually running out of memory. If another part of the decode doesn't fit, it still fails, and falling back to tiling there would be a separate PR.
FLUX.1 Krea Q4_K_S, 4 steps, seed 42, VAE decode on an RTX A6000 (CUDA):
If this PR adds or changes model support, I have added or updated its usage guide and relevant model index/README entries following the model documentation requirements.
Summary by CodeRabbit
Improvements
Attention calculations now process queries in smaller batches when no mask is provided, reducing the amount of intermediate data held at once. This can help limit memory use for longer query sequences.
When a mask is provided, calculations continue to process the full query sequence together. Results are combined across query positions in either case.
No actionable comments were generated in the recent review.
Walkthrough
The manual attention fallback now processes unmasked queries in bounded chunks, computes each chunk’s scores and value output, and concatenates the outputs. Masked attention remains a single chunk.
Changes: Manual attention fallback
Layer / File(s)
Summary
Query chunking and output assembly src/core/ggml_extend.cpp
Unmasked attention uses chunks capped by the key length and query-head count, with a minimum chunk size of one. Each chunk applies F32 accumulation, scaling, optional mask addition, and softmax before computing its value output. The outputs are concatenated along the query dimension. Masked attention uses one chunk.
The change splits large VAE attention into bounded chunks to avoid the CUDA/HIP crash at high resolutions. No concrete defect on supported inputs was found, so merge risk is low.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: fixing a VAE decode crash at 2048x2048 and above on CUDA/HIP. The raw summary describes chunking the manual attention fallback in query-length chunks, which addresses …
Description check
The description follows the template sections: Summary, Related Issue / Discussion, Additional Information, and Checklist. The Summary explains the crash, its cause (the attention score matrix exceeds…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title names the main change: fixing a VAE decode crash at 2048x2048 and above on CUDA/HIP. The raw summary describes chunking the manual attention fallback in query-length chunks, which addresses the cause of that crash. The title is specific and matches the changeset.
Full details: Description check
Explanation
The description follows the template sections: Summary, Related Issue / Discussion, Additional Information, and Checklist. The Summary explains the crash, its cause (the attention score matrix exceeds the CUDA/HIP kernel index limit), and the chunking fix. It includes a repro, test hardware, and benchmark tables. The Related Issue section links the replaced PR and separate issues. The first checklist item is checked. The model-documentation checklist item is unchecked. That item only applies when model support changes, and this PR does not change model support, so the gap is non-critical.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
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.
File: No configuration file from this repository applied
Commit: Unavailable
Layers: Repository UI
Review profile: CHILL
Plan: Advanced
Run ID: 2178211d-d0bb-4cee-8717-ea55c8f762ac
Commits
Reviewing files that changed from the base of the PR and between 95bb623 and c7b6c5e.
Files selected for processing: 1
src/core/ggml_extend.cpp
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
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
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.
Summary
At 2048x2048 and up on CUDA or HIP, the VAE decode crashes on the last step and no image is produced. The automatic tiling fallback does not kick in, and the only way to get images is forcing
--vae-tiling, which is slower and is not an exact decode.The VAE's attention compares every pixel with every other pixel in one pass, which at 2048x2048 is about 4.3 billion. That's more than the CUDA/HIP kernels can count, so ggml asserts:
Flash Attention doesn't support this layer, so the regular attention runs anyway.
We split those into smaller chunks once it gets large (1536x1536 and up), so each chunk stays within what the kernels can handle. The output is byte-identical to master, and anything that fits in one chunk runs exactly as before.
While datacenters have plenty of VRAM, this crashes regardless, but we also found that in consumer situations (much less VRAM available) this fix also holds and, together with the ggml conv2d fix, eliminated the OOM issue.
Related Issue / Discussion
This replaces #1621, which also had the tiling fallback. The review there asked to keep it to one change.
Two other crashes at these sizes are separate from this one:
Additional Information
It also uses less memory: at 2048x2048 the attention needs 2 GB instead of 16. There's no speed cost we could measure: at 1536x1536, where it already splits, decode is 1.81s vs 1.84s on master. It's not FLUX-only: SD1.5 crashes the same way at 2048x2048 on master and decodes fine with this PR, and at 512 and 1024 its output is byte-identical.
It doesn't handle actually running out of memory. If another part of the decode doesn't fit, it still fails, and falling back to tiling there would be a separate PR.
FLUX.1 Krea Q4_K_S, 4 steps, seed 42, VAE decode on an RTX A6000 (CUDA):
Repro:
Tested on an RTX A6000 48 GB (CUDA 13.0.1) and an R9700 32 GB (ROCm 7.0.2), Ubuntu 24.04.
Checklist
Summary by CodeRabbit