Skip to content

Fix GPU memory leak in voice conditioning cache - #166

Closed
secretzer0 wants to merge 1 commit into
devnen:mainfrom
secretzer0:fix-conds-cache-memory-leak
Closed

Fix GPU memory leak in voice conditioning cache#166
secretzer0 wants to merge 1 commit into
devnen:mainfrom
secretzer0:fix-conds-cache-memory-leak

Conversation

@secretzer0

Copy link
Copy Markdown

prepare_conditionals runs inside generate on a cache miss without a grad guard, so each entry stored in _conds_cache carries the autograd graph of the reference encoders. On CUDA that is roughly 200 MB per distinct voice, held for the life of the process.

Measured on an RTX 4070 8 GB with the cu128 image: 6 voices, +1341 MB allocated (embedding.grad_fn set); the same 6 under torch.inference_mode, +1 MB. Around 20 distinct voices produced CUDA out of memory ... 7.39 GiB is allocated by PyTorch.

Fix: wrap the generate call in torch.inference_mode(). With this change, 26 new voices plus long text went from 4414 MB to 5256 MB and stayed flat.

Wrap generation in torch.inference_mode so cached conds do not retain autograd graphs (~200 MB per voice).
@secretzer0

Copy link
Copy Markdown
Author

Saw this is a duplicate of #164.

@secretzer0 secretzer0 closed this Aug 16, 2026
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