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: keep the stream encode hook out of a compiled VAE decode graph (#14822)

Daniel Han8cbcf05f66e5
diff --git a/studio/backend/core/inference/video_stream_encode.py b/studio/backend/core/inference/video_stream_encode.py
index 82ac41ae875..22780964ca3 100644
--- a/studio/backend/core/inference/video_stream_encode.py
+++ b/studio/backend/core/inference/video_stream_encode.py
@@ -183,7 +183,11 @@ class StreamEncode:
try:
_wrap("decode", _decode_wrapper)
_wrap("tiled_decode", _tiled_wrapper)
- handle = vae.decoder.register_forward_hook(self._on_chunk, with_kwargs = True)
+ # Disabled for Dynamo: under a compiled decode (UNSLOTH_DIFFUSION_COMPILE_VAE=1) the hook's thread, queue and
+ # CUDA stream code broke the graph at each of them; as one eager island the chunks still stream.
+ handle = vae.decoder.register_forward_hook(
+ self._torch.compiler.disable(self._on_chunk), with_kwargs = True
+ )
except Exception: # noqa: BLE001 - a VAE that refuses the hooks just encodes afterwards
self.invalidate("hooks refused")
try:
diff --git a/studio/backend/tests/test_video_stream_encode.py b/studio/backend/tests/test_video_stream_encode.py
index 23f7c67be90..b12d0446df1 100644
--- a/studio/backend/tests/test_video_stream_encode.py
+++ b/studio/backend/tests/test_video_stream_encode.py
@@ -100,6 +100,40 @@ def test_streamed_frames_are_the_exported_frames(patch, tmp_path):
assert "decode" not in vars(pipe.vae) and not pipe.vae.decoder._forward_hooks
[email protected]("device", ["cpu", "cuda"])
+def test_a_compiled_decode_runs_the_chunk_hook_as_one_eager_island(device):
+ """UNSLOTH_DIFFUSION_COMPILE_VAE=1 compiles ``vae.decode`` (fullgraph=False, dynamic=True): Dynamo must not trace
+ into the hook's thread, queue and CUDA stream code, and the streamed frames are still the exported ones."""
+ if device == "cuda" and not torch.cuda.is_available():
+ pytest.skip("needs CUDA")
+ from torch._dynamo.utils import counters
+
+ torch._dynamo.reset()
+ counters.clear()
+ pipe = _Pipe(_vae().to(device))
+ z = torch.randn(1, 4, 4, 8, 8, device = device)
+ with torch.inference_mode():
+ reference = pipe.video_processor.postprocess_video(
+ pipe.vae.decode(z, return_dict = False)[0], output_type = "np"
+ )[0]
+ pipe.vae.decode = torch.compile(
+ pipe.vae.decode, backend = "eager", fullgraph = False, dynamic = True
+ )
+ try:
+ for _ in range(2):
+ session = vse.StreamEncode(pipe.vae, pipe.video_processor, 8)
+ frames = _render(pipe, session, z)
+ assert len(session.chunks) == 4
+ assert np.array_equal(frames.cpu().numpy(), (reference * 255).round().astype(np.uint8))
+ assert session.take(frames, 8) is not None
+ reasons = {k.splitlines()[0] for k in counters["graph_break"]}
+ assert all("torch.compiler.disable" in r for r in reasons), reasons
+ assert sum(counters["graph_break"].values()) <= 2
+ finally:
+ del pipe.vae.decode
+ torch._dynamo.reset()
+
+
def test_tiled_decode_falls_back():
vae = _vae()
vae.enable_tiling(tile_sample_min_height = 32, tile_sample_min_width = 32)