Repository navigation
Studio: return a video render's pinned host memory to the driver at render end - #15036
Merged
Merged
Conversation
…ender end The uint8 clip from device_uint8 and the streamed encode's chunks are pinned host memory. torch's caching host allocator kept them (about 3x the clip, power-of-two rounded) for as long as Studio ran. Drop the last references at the end of generate and empty the host cache.
Member
Author
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. You're on a roll. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
A Wan render pins its clip twice in host memory: once as the uint8 frames from
device_uint8(video_frames.py) and once as the streamed encode's per-chunk copies (video_stream_encode.py, #14405). When those tensors are freed, torch's caching host allocator keeps the page-locked blocks, rounded up to a power of two. Nothing empties that cache until the model is unloaded, so every resident video model kept about 3x its largest clip pinned while Studio was idle.VideoBackend.generatenow drops its last references to the clip at the end of the render, after the stream session is closed, and calls the existingrelease_pinned_host_memory()(torch._C._host_emptyCache, present since torch 2.6, guarded withgetattr). This happens on success, failure and cancel. Nothing changes inside the denoise or decode loop, and the output is unchanged.Details
Setup: tiny random
AutoencoderKLWanon CUDA, decoding a1x4x21x60x104latent to 81 frames at 480x832 (a 92.5 MB uint8 clip) throughuint8_video_framesplusStreamEncode, the same callsgeneratemakes. torch 2.14.1+cu130. Memory comes fromtorch.cuda.host_memory_stats():allocated_bytes.currentis the page-locked memory the host allocator holds, andactive_bytes.currentis the part still in use.7a359e5840921104...7a359e5840921104...4750003984323fab...4750003984323fab...The 290 MB is the 128 MB block for the whole clip plus 21 decoder chunks at 8 MB each. That is roughly 3.1x the clip. At 720p and 81 frames the same arithmetic gives about 580 MB.
Per-render time, 3 interleaved runs per arm, 6 renders per run, median of renders 1 to 5 in each run:
The spread between repeated runs of the unchanged code (A/A) is 0.49 s, and both arms fall inside it. The work this adds is a fresh
cudaHostAllocof the clip's buffers on each render: about 23 allocations and 160 to 230 ms per render for this clip, according to the allocator'shost_alloc_time. It also adds about 60 to 80 ms ofcudaFreeHostat the end of the render. Both are small next to a real Wan render. Only the free can synchronize, and it runs after the render has finished.ComfyUI does not pin VAE output at all. The only pinning in
comfy/model_management.pyis for model weights, done withcudaHostRegisteragainst aMAX_PINNED_MEMORYbudget and released withcudaHostUnregister. So pinned memory there never outlives its use without a bound.Tests
test_a_render_returns_its_pinned_clip_to_the_driver[False|True]intests/test_video_backend.py. It checks that each render empties the host cache exactly once, after the stream session is closed. On success it also checks that the clip is no longer referenced at that point. On main both cases fail (2 failed); on this branch both pass.tests/test_video_backend.py,test_video_stream_encode.py,test_video_frames.py,test_video_encode.pyandtest_video_stream_residency.py: 571 passed, 7 skipped on this branch, compared with 569 passed, 2 failed (the new test), 7 skipped on main.