Skip to content

Fix masked-image VAE encode dtype in fine_tune and train_textual_inversion#2320

Merged
kohya-ss merged 1 commit intodevfrom
fix/inpainting-vae-dtype
May 5, 2026
Merged

Fix masked-image VAE encode dtype in fine_tune and train_textual_inversion#2320
kohya-ss merged 1 commit intodevfrom
fix/inpainting-vae-dtype

Conversation

@kohya-ss
Copy link
Copy Markdown
Owner

@kohya-ss kohya-ss commented May 5, 2026

Summary

  • Follow-up to PR Add inpainting training and sampling support for SD1.5 and SDXL #2309 / Fix/inpainting followups #2319 — finding Merge requirements #3 from the inpainting review.
  • In fine_tune.py and train_textual_inversion.py, the masked-image VAE encode path used weight_dtype for input and skipped the output cast, while the regular latents path encodes in vae_dtype and casts the sample to weight_dtype.
  • Under --no_half_vae (VAE kept in fp32 while weight_dtype is fp16/bf16), this could cause a torch.cat([noisy_latents, mask, masked_latents], dim=1) dtype mismatch and VAE numerical instability.
  • Fix aligns both scripts with the pattern already used in sdxl_train.py and the regular latents path: .to(dtype=vae_dtype) on input, .latent_dist.sample().to(weight_dtype) on output.
  • train_db.py is internally consistent (both paths use weight_dtype) and is intentionally left out of this PR; its broader vae_dtype/weight_dtype handling is a pre-existing concern to be addressed separately.

Test plan

  • Verified --train_inpainting runs on fine_tune.py after the change
  • Verified --train_inpainting runs on train_textual_inversion.py after the change
  • (Optional) Spot-check with --no_half_vae + fp16/bf16 --mixed_precision

🤖 Generated with Claude Code

…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>
@kohya-ss kohya-ss merged commit b212706 into dev May 5, 2026
3 checks passed
@kohya-ss kohya-ss deleted the fix/inpainting-vae-dtype branch May 5, 2026 14:04
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