Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

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

Daniel Hand6677ab2c310
diff --git a/studio/backend/core/inference/video_stream_encode.py b/studio/backend/core/inference/video_stream_encode.py
index 4ef98a5b4b8..82ac41ae875 100644
--- a/studio/backend/core/inference/video_stream_encode.py
+++ b/studio/backend/core/inference/video_stream_encode.py
@@ -59,6 +59,13 @@ class StreamEncode:
getattr(sys.modules.get(type(vae).__module__), "unpatchify", None) if patch else None
)
self._patch = patch
+ # ROCm's bf16 decode (diffusion_device.install_rocm_vae_bf16_decode) runs the decoder in bf16 and hands the
+ # pipeline the fp32 widening of its output, so the pipeline postprocesses fp32: the chunks must be widened the
+ # same way or no frame ever matches. Read off the VAE, not ``vae.decode``: the untiled-when-fits decode (and
+ # the OOM scope) wrap it from outside. NVIDIA's fp16 decode returns fp16 as is, which the chunks already are.
+ self._widen = bool(getattr(vae, "_unsloth_bf16_decode_mode", None)) or bool(
+ getattr(getattr(vae, "decode", None), "_unsloth_bf16_decode", False)
+ )
self._side = None
self.chunks: list = []
self.reason = ""
@@ -98,6 +105,8 @@ class StreamEncode:
self.invalidate("no unpatchify")
return
x = self._unpatchify(x, patch_size = self._patch)
+ if self._widen and x.is_floating_point() and x.dtype != torch.float32:
+ x = x.float()
x = torch.clamp(x, min = -1.0, max = 1.0)
x = self.processor.postprocess(x[0].permute(1, 0, 2, 3), "pt")
x = (
diff --git a/studio/backend/tests/test_video_stream_encode.py b/studio/backend/tests/test_video_stream_encode.py
index 84b00b734cf..23f7c67be90 100644
--- a/studio/backend/tests/test_video_stream_encode.py
+++ b/studio/backend/tests/test_video_stream_encode.py
@@ -237,3 +237,43 @@ def test_a_failed_final_flush_falls_back_without_hanging(monkeypatch):
assert result["data"] is None
session.close()
assert not os.path.exists(session.path)
+
+
+def test_rocm_bf16_decode_streams_the_exported_frames(monkeypatch, tmp_path):
+ """ROCm gfx11 / gfx12 decode the Wan VAE in bf16 by default and widen the result to fp32: the streamed chunks
+ must be widened the same way, or the session falls back ("frames differ") on every clip."""
+ from types import SimpleNamespace
+
+ from core.inference.diffusion_device import install_rocm_vae_bf16_decode
+
+ monkeypatch.setenv(
+ "UNSLOTH_VIDEO_VAE_BF16_DECODE", "1"
+ ) # forced: the CPU stands in for the ROCm card
+ monkeypatch.setattr(torch.cuda, "is_bf16_supported", lambda *a, **k: True)
+ for patch, outer in ((False, False), (True, False), (False, True), (True, True)):
+ pipe = _Pipe(_vae(patch))
+ target = SimpleNamespace(device = "cuda", backend = "rocm", dtype = torch.bfloat16)
+ assert install_rocm_vae_bf16_decode(pipe, target) == "weights"
+ if outer:
+ # Studio's default video load wraps the bf16 decode again (video_vae_untiled's untiled-when-fits layer):
+ # the outer function carries none of the inner wrapper's attributes.
+ inner = pipe.vae.decode
+
+ def untiled_when_fits(
+ *args,
+ _inner = inner,
+ **kwargs,
+ ):
+ return _inner(*args, **kwargs)
+
+ pipe.vae.decode = untiled_when_fits
+ assert pipe.vae.decoder.conv_in.weight.dtype == torch.bfloat16
+ z = torch.randn(1, 4, 4, 8, 8)
+ session = vse.StreamEncode(pipe.vae, pipe.video_processor, 8)
+ frames = _render(pipe, session, z)
+ assert len(session.chunks) == 4 and len(frames) == 13
+ before = vse.STATS["streamed"]
+ data = session.take(frames, 8)
+ assert data is not None, vse.STATS["last"]
+ assert vse.STATS["streamed"] == before + 1
+ assert len(_mp4_frames(data, tmp_path)) == 13