Skip to content

feat(mm): improved VAE encode VRAM usage - #8414

Merged
psychedelicious merged 9 commits into
mainfrom
psyche/feat/vae-encode-working-memory
Aug 12, 2025
Merged

feat(mm): improved VAE encode VRAM usage#8414
psychedelicious merged 9 commits into
mainfrom
psyche/feat/vae-encode-working-memory

Conversation

@psychedelicious

@psychedelicious psychedelicious commented Aug 11, 2025

Copy link
Copy Markdown
Contributor

Summary

  • Reserve extra working VRAM for VAE encode. It needs ~50% the working memory as decoding.
  • Use latent-space concat for FLUX Kontext ref images. This prevents a big VRAM spike when we prepare Kontext ref images. Instead of encoding one giant image (spike!), we encode N normal-sized ref images individually, then concat the tensors.
  • Fix a bug in FLUX kontext latent space concat.

This PR includes a reverted commit with the investigation that determined the magic numbers required for VAE encode working memory estimation. Claude did the testing - reverted commit includes prompt, test scripts, results. Our existing magic numbers used for VAE decoding were confirmed to be accurate.

Related Issues / Discussions

QA Instructions

This change uses existing, proven APIs. I don't think it will cause any issues.

Merge Plan

n/a

Checklist

  • The PR has a short but descriptive title, suitable for a changelog
  • Tests added / updated (if applicable)
  • Documentation added / updated (if applicable)
  • Updated What's New copy (if doing a release after this PR)

@github-actions github-actions Bot added python PRs that change python files invocations PRs that change invocations backend PRs that change backend files labels Aug 11, 2025
@github-actions github-actions Bot added the frontend PRs that change frontend files label Aug 11, 2025
@psychedelicious psychedelicious changed the title feat(mm): estimate working memory for VAE encode feat(mm): improved VAE encode VRAM usage Aug 11, 2025
This commit includes a task delegated to Claude to investigate our VAE
working memory calculations and investigation results.

See VAE_INVESTIGATION.md for motivation and detail. Everything else is
its output.

Result data includes empirical measurements for all supported model
architectures at a variety of resolutions and fp16/fp32 precision.
Testing conducted on a 4090.

The summarized conclusion is that our working memory estimations for
decoding are spot-on, but decoding also needs some extra working memory.
Empirical measurements suggest ~45% the amount needed for encoding.

A followup commit will implement working memory estimations for VAE
encoding with the goal of preventing unexpected OOMs during encode.
…models

Tell the model manager that we need some extra working memory for VAE
encoding operations to prevent OOMs.

See previous commit for investigation and determination of the magic
numbers used.

This safety measure is especially relevant now that we have FLUX Kontext
and may be encoding rather large ref images. Without the working memory
estimation we can OOM as we prepare for denoising.

See #8405 for an example of this issue on a very low VRAM system. It's
possible we can have the same issue on any GPU, though - just a matter
of hitting the right combination of models loaded.
If the transformer fills up VRAM, then when we VAE encode kontext
latents, we'll need to first offload the transformer (partially, if
partial loading is enabled).

No need to do this - we can encode kontext latents before loading the
transformer to reduce model thrashing.
… in latent space

We weren't tracking the canvas dimensions properly which coudl result in
FLUX not "seeing" ref images after the first very well
Prevents a large spike in VRAM when preparing to denoise w/ multiple ref
images.

There doesn't appear to be any different in image quality / ref
adherence when concatenating in latent space vs image space, though
images _are_ different.
… extension

Slightly reduces VRAM allocations.
@psychedelicious
psychedelicious force-pushed the psyche/feat/vae-encode-working-memory branch from 788f7ba to 51d004e Compare August 11, 2025 12:56
@psychedelicious
psychedelicious merged commit 23d59ab into main Aug 12, 2025
12 checks passed
@psychedelicious
psychedelicious deleted the psyche/feat/vae-encode-working-memory branch August 12, 2025 00:51
kappacommit pushed a commit to kappacommit/InvokeAI that referenced this pull request Jul 8, 2026
…code

Flux2RefImageExtension encoded reference images with model_on_device() and
no working-memory estimate, unlike FLUX.1's KontextExtension which passes
one. A single reference can be up to ~4.1MP after the cap, whose encode
needs far more than the default reserve - the encode then allocates past
what the cache holds free and, on Windows, silently spills to shared
memory. Pre-existing on main (independent of smart_partial_loading, since
op-provided estimates are the existing VAE-op mechanism); surfaced by
review of this PR. Mirrors the KontextExtension estimate (encode uses ~50%
of the decode scaling constant, see invoke-ai#8414).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend PRs that change backend files frontend PRs that change frontend files invocations PRs that change invocations python PRs that change python files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: Flux Kontext with 2/3 reference images - Out of Memory Error (low VRAM 4GB)

2 participants