Repository navigation
Studio: re-decode a NaN 16-bit VAE decode in fp32; fp32 VAE on GPUs without tensor cores (GTX 16xx) - #18454
Conversation
… GPUs without tensor cores A NaN or Inf decode from finite latents is decoded again once in fp32 from the same latents, and later decodes of that VAE stay fp32. Non-finite latents raise a message that names the denoiser. GTX 16xx, MX450/MX550, TU117 Quadro/T-series and pre-Volta cards decode and encode in fp32 from the start, and keep the SDXL fp32 upcast. Weights stay stored in 16 bits. UNSLOTH_DIFFUSION_VAE_FP32=1 forces fp32, 0 turns the layer off.
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ddee701822
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| with fp32_keeping_storage(vae, encode_parts): | ||
| return encode(x.to(torch.float32), *args, **kwargs) |
There was a problem hiding this comment.
Preserve the encoder output dtype after fp32 computation
When fp32 mode is forced or automatically enabled on a GTX 16xx, this wrapper changes the encoded latent distribution from 16-bit to float32 without restoring the caller's expected dtype. This breaks dense 16-bit FLUX img2img: FluxImg2ImgPipeline.prepare_latents passes the encoded latents through scheduler.scale_noise without casting them back, and its denoising loop sends those float32 latents directly into the transformer's 16-bit x_embedder, causing a matrix dtype mismatch. Keep the encoder arithmetic in fp32, but restore the latent distribution's output dtype (or explicitly adapt the affected pipeline consumers) before returning.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in ce96fa0. The encoder still runs in fp32, and the output (tuple, AutoencoderKLOutput and the DiagonalGaussianDistribution's parameters, mean, std, var) is handed back in the VAE's 16-bit dtype, so img2img latents reach the denoiser in its dtype. test_fp32_from_the_start now asserts the 16-bit dtypes and a 16-bit sample; it fails on the previous head.
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🎉 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
…decode test (#18561) ## Summary `Repo tests (CPU, rest-2)` is red on `main` (and so on every PR) because `tests/test_source_read_encoding.py` flags a new read: ``` AssertionError: 1 file reads in the test trees touch a checked-in file with the platform default encoding, so they break on Windows as soon as that file gains a non-ASCII byte. Pass encoding = "utf-8": ['studio/backend/tests/test_diffusion_vae_finite.py:281: read_text()'] ``` ## Cause #18454 added `test_load_installs_before_the_speed_layer`, which reads `studio/backend/core/inference/diffusion.py` with a bare `read_text()`. On Windows that uses the locale code page, so the test breaks as soon as `diffusion.py` gains a non-ASCII character. The guard is right; the new read is the problem. ## Change - `studio/backend/tests/test_diffusion_vae_finite.py`: pass `encoding = "utf-8"` to that one `read_text`. No behaviour change. ## Testing - `tests/test_source_read_encoding.py`: fails on `main` with the offender above, passes with this change (16 passed). - `studio/backend/tests/test_diffusion_vae_finite.py`: 43 passed.
Problem
A GTX 1650 user on Windows gets HTTP 500 "The decoded image contains NaN values" at the end of every render, with FLUX.2 klein 4B GGUF and Z-Image Turbo, even at 256x256. The same setup works on an Intel iGPU.
On an NVIDIA card these picks run through diffusers. Below compute capability 8.0 there is no bf16, so the whole pipeline runs in float16, including the VAE. GTX 16xx cards (TU116/TU117, no tensor cores) are widely reported to produce NaN or black images from float16 convolutions; other Stable Diffusion front ends special-case this card family. They report the same compute capability (7.5) as the T4 and RTX 20xx, so nothing in Studio told them apart.
On main, the only NaN-to-fp32 VAE fallback covers the SDXL VAE (plus a separate one for video VAEs). For the other image families the NaN surfaces only in postprocess, after the latents are gone, so a VAE overflow and a denoiser overflow look the same.
Change
New module
diffusion_vae_finite.py, plus a 7-line hook indiffusion.pynext to the existing SDXL fp16 decode setup.UNSLOTH_DIFFUSION_VAE_FP32.1forces fp32,autois the default, and0turns the whole layer off (main's behaviour).Results
Colab T4, FLUX.2 klein base and Z-Image Turbo at 256 and 1024 px, merge base vs this branch:
Tests
tests/test_diffusion_vae_finite.py, 43 CPU tests:Not covered