WAN VAE: Fix cache VRAM leak on error #10141
Merged
+14
−23
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If this suffers an exception (such as a VRAM oom) it will leave the encode() and decode() methods which skips the cleanup of the WAN feature cache. The comfy node cache then ultimately keeps a reference this object which is in turn reffing large tensors from the failed execution.
The feature cache is currently setup at a class variable on the encoder/decoder however, the encode and decode functions always clear it on both entry and exit of normal execution.
Its likely the design intent is this is usable as a streaming encoder where the input comes in batches, however the functions as they are today don't support that.
So simplify by bringing the cache back to local variable, so that if it does VRAM OOM the cache itself is properly garbage collected when the encode()/decode() functions disappear from the stack.
Example Test conditions:
NVIDIA GeForce GTX 1660 SUPER (6GB)
Tiled VAE Encode 608x608x13f as one large tile (no acutal tiling)
This gives me the OOM I deserve, but VRAM then leaks. See the RHS of the nvtop trace where it does not come down to baseline after the exception.
With this change, VRAM will now return to baseline: