Skip to content

fix: VAE decode crash at 2048x2048 and above on CUDA/HIP - #2492

Open
RapidMark wants to merge 1 commit into
leejet:masterfrom
RapidMark:vae-chunked-attention-pr
Open

RapidMark wants to merge 1 commit into
leejet:masterfrom
RapidMark:vae-chunked-attention-pr

Conversation

@RapidMark

@RapidMark RapidMark commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

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:

ggml-cuda/scale.cu:34: GGML_ASSERT(k <= std::numeric_limits<uint32_t>::max()) failed

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):

1024 1536 2048 2560
master 0.94s 1.84s crash crash
this PR 0.95s 1.81s 3.20s 5.49s

Repro:

cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DSD_CUDA=ON
cmake --build build --target sd-cli
./build/bin/sd-cli --diffusion-model flux1-krea-dev-Q4_K_S.gguf --clip_l clip_l.safetensors --t5xxl t5xxl_fp16.safetensors --vae ae.safetensors -p "a corgi astronaut on the moon" --cfg-scale 1.0 --steps 4 -W 2048 -H 2048 --seed 42

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

  • 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.

@coderabbitai

coderabbitai Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Explain Change →

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.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: leejet, mirek190

Merge Risk: Minimal · up to c7b6c

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed 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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • 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.

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