Skip to content

Studio: return a video render's pinned host memory to the driver at render end - #15036

Merged
danielhanchen merged 2 commits into
mainfrom
studio-video-pinned-release
Oct 23, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-video-pinned-release

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

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.generate now drops its last references to the clip at the end of the render, after the stream session is closed, and calls the existing release_pinned_host_memory() (torch._C._host_emptyCache, present since torch 2.6, guarded with getattr). This happens on success, failure and cancel. Nothing changes inside the denoise or decode loop, and the output is unchanged.

Details

Setup: tiny random AutoencoderKLWan on CUDA, decoding a 1x4x21x60x104 latent to 81 frames at 480x832 (a 92.5 MB uint8 clip) through uint8_video_frames plus StreamEncode, the same calls generate makes. torch 2.14.1+cu130. Memory comes from torch.cuda.host_memory_stats(): allocated_bytes.current is the page-locked memory the host allocator holds, and active_bytes.current is the part still in use.

before after
Pinned host memory held after the render, idle (4 to 6 renders, every run) 290.0 MB 0.0 MB
Pinned memory still in use after the render 0 MB 0 MB
Process RSS when idle 2.20 to 2.29 GB 1.91 to 1.99 GB
sha256 of the frames 7a359e5840921104... 7a359e5840921104...
sha256 of the mp4 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:

arm run 1 run 2 run 3 median
before 1.590 s 1.818 s 1.329 s 1.590 s
after 1.456 s 1.776 s 1.393 s 1.456 s

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 cudaHostAlloc of the clip's buffers on each render: about 23 allocations and 160 to 230 ms per render for this clip, according to the allocator's host_alloc_time. It also adds about 60 to 80 ms of cudaFreeHost at 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.py is for model weights, done with cudaHostRegister against a MAX_PINNED_MEMORY budget and released with cudaHostUnregister. So pinned memory there never outlives its use without a bound.

Tests

  • New test_a_render_returns_its_pinned_clip_to_the_driver[False|True] in tests/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.py and test_video_stream_residency.py: 571 passed, 7 skipped on this branch, compared with 569 passed, 2 failed (the new test), 7 skipped on main.

…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.
@danielhanchen danielhanchen self-assigned this Oct 23, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T22:05:26.781080Z ed739ab Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: ed739ab1de

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

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".

@danielhanchen
danielhanchen merged commit 74288f4 into main Oct 23, 2026
31 of 44 checks passed
@danielhanchen
danielhanchen deleted the studio-video-pinned-release branch October 23, 2026 23:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant