Skip to content

Studio video: stream-encode Wan clips under the ROCm bf16 VAE decode - #14653

Merged
danielhanchen merged 2 commits into
mainfrom
studio-video-stream-encode-bf16-decode
Oct 22, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
studio-video-stream-encode-bf16-decode

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Problem

On ROCm gfx11 / gfx12 (Strix Halo, RDNA3, RDNA4), Studio decodes the Wan VAE in bf16 by default (install_rocm_vae_bf16_decode, 6x faster per clip than fp32 on gfx1151). The wrapper runs the decoder in bf16 and hands the pipeline the fp32 widening of its output, so diffusers' postprocess runs in fp32.

The overlapped video encode (#14405) converts each decoder chunk to uint8 inside a forward hook, from the raw chunk. On this path the raw chunk is bf16, so the hook postprocessed in bf16 while the exported frames were postprocessed in fp32. The rounding differed, _matches never passed, and every clip did all of the streamed work and then encoded again. A gfx1151 default Wan render logs:

video.stream_encode: fallback (frames differ)

on every clip.

Change

StreamEncode checks whether the VAE carries the ROCm bf16 decode (vae._unsloth_bf16_decode_mode, set by the installer, so it survives the untiled-when-fits layer Studio puts on top). When it is, the hook widens each chunk to fp32 before the clamp and postprocess, which is what the pipeline does with the widened output. Unpatchify and clamp are exact in either dtype, so the chunks now equal the exported frames bit for bit.

Paths left as they are:

  • NVIDIA's fp16 decode returns fp16, so its postprocess already runs in fp16 and its chunks already match.
  • A plain fp32 decoder is unaffected (the widen is a no-op there).

Results

AMD CI, Radeon 8060S (gfx1151), Linux, torch 2.11.0+rocm7.13.0. Base and head ran in the same job on the same machine, run 39753304821:

  • base = main 7fd4988b11;
  • head = base plus this diff;
  • Wan2.2-TI2V-5B, all defaults, 832x480, 49 frames, 6 steps, family CFG, 2 prompts;
  • wan_stream_off sets UNSLOTH_STUDIO_VIDEO_STREAM_ENCODE=0, and every arm ran twice in fresh processes (A/A).
cell base: encode head: encode base steady s/clip head steady s/clip
wan_auto fallback (frames differ) streamed (49 frames) 42.20 42.43
wan_auto_repeat fallback (frames differ) streamed (49 frames) 42.53 42.15
wan_stream_off off off 42.51 42.31
wan_stream_off_repeat off off 42.27 42.08

Verdict CONFIRMED: the base reproduces the fallback, and the head streams on every default clip.

  • Pixels: the exported frames have the same sha256 in every arm and at both states.
  • Peak memory: unchanged (26.08 GiB).
  • Speed: at this clip size, the wall-clock difference between streaming and not is inside the A/A spread (0.3-0.7%). Encoding 49 frames at 832x480 is well under a second of a 42 s render, so the change removes duplicated work (a full streamed encode thrown away, then a second encode) rather than buying a measurable speedup here. The saving scales with encode time (longer or larger clips).

The first version of this change keyed the widen on the outermost vae.decode. Its gfx1151 run (39743742573) still fell back, because Studio's untiled-when-fits decode wraps the bf16 decode from outside. The final version reads the mode the bf16 install records on the VAE.

Tests

New test_rocm_bf16_decode_streams_the_exported_frames in tests/test_video_stream_encode.py. It installs the real install_rocm_vae_bf16_decode (forced mode) on a tiny AutoencoderKLWan, with and without patchify, with and without an outer decode layer like Studio's untiled-when-fits wrapper, and requires the session to stream:

main this branch
tests/test_video_stream_encode.py 1 failed (frames differ), 10 passed 11 passed
new test on the first version (decode-attribute check only) failed (frames differ) -

Not covered

  • Discrete RDNA3 / RDNA4 cards, which take the same bf16 decode path, were not run.
  • Windows ROCm was not run.

The ROCm gfx11 / gfx12 default decodes the Wan VAE in bf16 and widens the
result to fp32 before the pipeline postprocesses it, while the stream
encode hook postprocessed the raw bf16 chunks. The rounding differed, so
every clip fell back (frames differ) after doing the streamed work. The
hook now widens chunks to fp32 when the observed decode is that wrapper;
NVIDIA's fp16 decode returns fp16 and keeps the current path.
…ecode

Studio's default video load wraps the bf16 decode again (untiled when it
fits), so the outermost vae.decode never carries the bf16 marker and the
stream encode still fell back on gfx1151. Read the mode the bf16 install
records on the VAE; the test now stacks an outer decode layer too.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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-22T11:28:05.771136Z ac36630 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. More of your lovely PRs please.

Reviewed commit: ac3663041e

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

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