Skip to content

Release reused diffusion cache before VAE decode#136

Draft
Paul (nuclearspike) wants to merge 1 commit into
Lightricks:mainfrom
nuclearspike:codex/release-diffusion-cache-after-reuse
Draft

Release reused diffusion cache before VAE decode#136
Paul (nuclearspike) wants to merge 1 commit into
Lightricks:mainfrom
nuclearspike:codex/release-diffusion-cache-after-reuse

Conversation

@nuclearspike

Copy link
Copy Markdown

Summary

  • Scope the diffusion-stage transformer cache to its single intended reuse: carrying an identical transformer from the first diffusion stage into the second.
  • Evict the cached transformer as soon as the reuse-hit context exits, before VAE/audio decoder construction.
  • Keep generation-start eviction as a defensive backstop for cancellation, failure, or non-identical follow-up stages.
  • Enable the corrected cache by default and regenerate the OpenAPI artifacts.
  • Add regression coverage proving a consumed hit is released and a third identical stage rebuilds normally.

Root cause

The generation-scoped cache retained the reused transformer after the second diffusion stage. On Apple Silicon this allowed roughly 37 GiB of transformer state to overlap tiled VAE decoding. A live 1280x704 run with that lifetime peaked at 46.7 GiB of MPS driver memory and still had 35.4 GiB torch-allocated after output encoding. The cache only needs to survive the boundary between the two identical diffusion stages.

Apple Silicon HD A/B

Workload: Fast pipeline, 1280x704, 121 frames, 24 fps (5.04 seconds), fixed prompt and seed. Two fresh runs per condition.

Metric Cache disabled Corrected cache enabled Change
Median wall time 220.570 s 209.465 s -11.106 s (-5.04%)
Median peak MPS driver memory 44,504 MiB 41,713.5 MiB -2,790.5 MiB (-6.27%)

All four measured MP4s had the same SHA-256 (7e82...78aa); the comparison reported 99 dB PSNR. The timing result is directional because each condition has only two runs; the memory/lifetime fix is the primary motivation.

Validation

  • pnpm backend:test — 568 passed
  • pnpm typecheck — TypeScript clean; Pyright 0 errors/0 warnings
  • pnpm build:frontend — production build passed
  • pnpm openapi:check — generated schema/types current

Review note

This is a draft so the newly enabled default can receive cross-platform/CUDA validation before merge.

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