Fix masked-image VAE encode dtype in fine_tune and train_textual_inversion#2320
Merged
Fix masked-image VAE encode dtype in fine_tune and train_textual_inversion#2320
Conversation
…rsion Match the regular latents path: encode in vae_dtype, cast output to weight_dtype. Prevents torch.cat dtype mismatch and VAE numerical instability under --no_half_vae (vae_dtype != weight_dtype). Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
fine_tune.pyandtrain_textual_inversion.py, the masked-image VAE encode path usedweight_dtypefor input and skipped the output cast, while the regularlatentspath encodes invae_dtypeand casts the sample toweight_dtype.--no_half_vae(VAE kept in fp32 whileweight_dtypeis fp16/bf16), this could cause atorch.cat([noisy_latents, mask, masked_latents], dim=1)dtype mismatch and VAE numerical instability.sdxl_train.pyand the regular latents path:.to(dtype=vae_dtype)on input,.latent_dist.sample().to(weight_dtype)on output.train_db.pyis internally consistent (both paths useweight_dtype) and is intentionally left out of this PR; its broadervae_dtype/weight_dtypehandling is a pre-existing concern to be addressed separately.Test plan
--train_inpaintingruns onfine_tune.pyafter the change--train_inpaintingruns ontrain_textual_inversion.pyafter the change--no_half_vae+ fp16/bf16--mixed_precision🤖 Generated with Claude Code