Skip to content

Optimize Latent Caching Speed with VAE Optimizations - #1910

Open
alefh123 wants to merge 1 commit into
kohya-ss:mainfrom
alefh123:main
Open

Optimize Latent Caching Speed with VAE Optimizations#1910
alefh123 wants to merge 1 commit into
kohya-ss:mainfrom
alefh123:main

Conversation

@alefh123

Copy link
Copy Markdown

PR Summary:

With some help from DeepSeek, this PR aims to improve latent caching speed.

Key Changes:

Mixed Precision Caching: VAE encoding now uses FP16 (or BF16) during latent caching for faster computation and reduced memory use.

Channels-Last VAE: VAE is temporarily switched to channels_last memory format during caching to improve GPU performance.

--vae_batch_size Utilization: This leverages the existing --vae_batch_size option; users should increase it for further speedups.

Benefits:

Significantly Faster Latent Caching: Reduces preprocessing time.

Improved GPU Efficiency: Optimizes VAE encoding on GPUs.

Impact: Faster training setup due to quicker latent caching.

This is much more concise and directly highlights the essential changes and their impact. Let me know if you would like it even shorter or with any other adjustments!

Based on the optimizations implemented—mixed precision and channels-last format for the VAE during caching—a speedup of 2x to 4x is a reasonable estimate.

PR Summary:

This PR accelerates latent caching, a slow preprocessing step, by optimizing the VAE's encoding process.

Key Changes:

Mixed Precision Caching: VAE encoding now uses FP16 (or BF16) during latent caching for faster computation and reduced memory use.

Channels-Last VAE: VAE is temporarily switched to channels_last memory format during caching to improve GPU performance.

--vae_batch_size Utilization: This leverages the existing --vae_batch_size option; users should increase it for further speedups.

Benefits:

Significantly Faster Latent Caching: Reduces preprocessing time.

Improved GPU Efficiency: Optimizes VAE encoding on GPUs.

Impact: Faster training setup due to quicker latent caching.

This is much more concise and directly highlights the essential changes and their impact. Let me know if you would like it even shorter or with any other adjustments!

Based on the optimizations implemented—mixed precision and channels-last format for the VAE during caching—a speedup of 2x to 4x is a reasonable estimate.
@rockerBOO

Copy link
Copy Markdown
Contributor

I don't think this would be where it is doing the caching though. Converting SDXL VAE to fp16 also can cause NaN issues so we probably wouldn't want to convert it to fp16 regardless.

The memory format might be what we can try from this as it might improve the performance.

@kohya-ss

kohya-ss commented Feb 4, 2025

Copy link
Copy Markdown
Owner

As rockerBoo said, the code throws RuntimeError: NaN detected in latents. vae_dtype is already fp16 or bf16 if the mixed precision is enabled:

def prepare_dtype(args: argparse.Namespace):
weight_dtype = torch.float32
if args.mixed_precision == "fp16":
weight_dtype = torch.float16
elif args.mixed_precision == "bf16":
weight_dtype = torch.bfloat16
save_dtype = None
if args.save_precision == "fp16":
save_dtype = torch.float16
elif args.save_precision == "bf16":
save_dtype = torch.bfloat16
elif args.save_precision == "float":
save_dtype = torch.float32
return weight_dtype, save_dtype

If the user want to train with float32, the VAE dtype should be float32. Therefore, there seems to be no need to change the code for the VAE dtype.

Also, if you want to change the VAE settings, you can just enter the processing here:

vae.to(accelerator.device, dtype=vae_dtype)

Also, in my environment (Windows 10, torch 2.4.0, CUDA 12.4), when I added torch.channels_last(), the processing became about 40% slower. So the effect may depend on the environment.

@feffy380

feffy380 commented Feb 14, 2025

Copy link
Copy Markdown
Contributor

The bigger issue is that VAE caching loads batches synchronously, so it blocks on every single batch.
Just move the image loading and preprocessing to a dataloader for a massive speedup. Quick and dirty implementation: feffy380@c33b31c

@rockerBOO

Copy link
Copy Markdown
Contributor

With the new caching strategy you can set the --vae_batch_size and it does it somewhat asynchronously.

@feffy380

feffy380 commented Feb 15, 2025

Copy link
Copy Markdown
Contributor

Oof, I was looking at the wrong branch. I see sd3 uses threading for this now

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.

4 participants