Skip to content

feat(mps): prompt embedding cache + VAE latent cache for Apple Silicon#1774

Open
huangyebiaoke wants to merge 1 commit into
deepbeepmeep:mainfrom
huangyebiaoke:feat/mps-cache-layers
Open

feat(mps): prompt embedding cache + VAE latent cache for Apple Silicon#1774
huangyebiaoke wants to merge 1 commit into
deepbeepmeep:mainfrom
huangyebiaoke:feat/mps-cache-layers

Conversation

@huangyebiaoke

Copy link
Copy Markdown

Summary

Adds two caching layers to accelerate repeat generations on Apple Silicon Macs:

1. Prompt Embedding Cache (TextEncoderCache)

The existing TextEncoderCache (LRU, 100MB CPU-internal) was only used in WanAny2V. This PR extends it to:

  • DTT2V (diffusion_forcing.py) — T5 encoding of prompt and negative prompt now cached across seeds
  • OviFusionEngine (ovi_fusion_engine.py) — batched T5 encoding cached

2. VAE Latent Cache (VAELatentCache — new)

New shared/utils/vae_latent_cache.py:

  • LRU cache (500MB default, CPU-stored, Apple unified-memory-friendly)
  • Content-based hashing (first/last elements + shape + dtype + mean/std)
  • Wraps 6 VAE encode call sites in WanAny2V:
    • get_vae_latents() — per-reference-image
    • _build_mocha_latents() — mocha ref images
    • SVI-Pro reference image encodes (image_ref, img_end_frame)
    • SCAIL reference image encode

Impact

Repeat generations with same prompt/images skip T5 re-encoding and VAE re-encoding of reference/guide images. Estimated savings: 2–8 seconds per seed depending on model size.

Risk

Low — caching only, no model/data changes. Cache stores detached CPU tensors, evicted on overflow. First run identical to before.

Add two caching layers to accelerate repeated generations:

1. **Prompt Embedding Cache** (TextEncoderCache):
   - Already existed in WanAny2V pipeline, now extended to:
     - DTT2V (diffusion_forcing.py) — T5 text encoding reused across seeds
     - OviFusionEngine (ovi_fusion_engine.py) — batched text encoding cached

2. **VAE Latent Cache** (VAELatentCache — new):
   - LRU cache (500MB default, CPU-stored, unified-memory-friendly)
   - Hashed by tensor content (first/last elements + shape + mean/std)
   - Wrapped at 6 key encode sites in WanAny2V:
     - get_vae_latents() — per-reference-image caching
     - _build_mocha_latents() — mocha ref image encoding
     - SVI-Pro reference image encoding (image_ref, img_end_frame)
     - SCAIL reference image encoding

Impact: repeat generations with same prompt/images skip T5 re-encoding
and VAE re-encoding of reference/guide images. On M4 Max, saves ~2-8s
per seed depending on model size.
@huangyebiaoke huangyebiaoke force-pushed the feat/mps-cache-layers branch from 6f51122 to 63f5582 Compare May 13, 2026 02:12
@huangyebiaoke huangyebiaoke changed the base branch from MPS to main May 13, 2026 02:12
@huangyebiaoke

Copy link
Copy Markdown
Author

Rebased onto latest main (includes the MPS→main merge) and changed target branch from MPS to main. Ready for review.

@deepbeepmeep This adds prompt embedding cache + VAE latent cache to reduce repeat-generation latency on Apple Silicon.

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