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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio video: keep the untiled decode gate visible under the stream encoder, skip the fp32 VAE rerun on non-finite latents, refuse NaN clips clearly (#15339)
Daniel Han975e8649c149
diff --git a/studio/backend/core/inference/diffusion_speed.py b/studio/backend/core/inference/diffusion_speed.pyindex e40c647e818..60f89c00c34 100644--- a/studio/backend/core/inference/diffusion_speed.py+++ b/studio/backend/core/inference/diffusion_speed.py@@ -907,6 +907,17 @@ def _vae_channels_last_3d(vae: Any, logger: Any, *, fused: bool) -> bool:VIDEO_VAE_HALF_ENV = "UNSLOTH_VIDEO_VAE_HALF"+# Kill switch: "0" reruns a non-finite fp16 decode in fp32 even when its latents are already non-finite (as before).+VIDEO_VAE_NONFINITE_INPUT_ENV = "UNSLOTH_VIDEO_VAE_NONFINITE_INPUT_KEEP_FP16"+++def _vae_input_nonfinite_skips_fp32() -> bool:+ return os.environ.get(VIDEO_VAE_NONFINITE_INPUT_ENV, "").strip().lower() not in (+ "0",+ "false",+ "no",+ "off",+ )def _video_vae_half_decode(pipe: Any, target: Any, family: Any, logger: Any) -> bool:@@ -952,6 +963,13 @@ def _video_vae_half_decode(pipe: Any, target: Any, family: Any, logger: Any) ->sample = out[0] if isinstance(out, tuple) else getattr(out, "sample", out)if not torch.is_tensor(sample) or bool(torch.isfinite(sample).all()):return out+ # Non-finite latents stay non-finite in fp32; the rerun only doubles memory (T4 OOM).+ if _vae_input_nonfinite_skips_fp32() and not bool(torch.isfinite(z).all()):+ if logger is not None:+ logger.warning(+ "diffusion.speed: the latents reaching the VAE are not finite; fp16 decode kept"+ )+ return outif logger is not None:logger.warning("diffusion.speed: fp16 VAE decode was not finite; decoding in fp32 from now on"diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.pyindex 9ce87fc5ea1..8d0fa9d6c92 100644--- a/studio/backend/core/inference/video.py+++ b/studio/backend/core/inference/video.py@@ -278,7 +278,12 @@ from .video_families import (video_family_uses_i2v_pipeline,video_i2v_call_kwargs,)-from .video_frames import uint8_video_frames+from .video_frames import (+ NON_FINITE_FRAMES_MSG,+ NonFiniteFramesError,+ require_finite_frames,+ uint8_video_frames,+)from .video_minimax_h3 import (H3_ANCHOR_FIRST,H3_ANCHOR_LAST,@@ -4347,6 +4352,8 @@ def video_failure_detail(exc: BaseException, request_shape: Optional[dict[str, Aif isinstance(exc, H3NativeMemoryRefusal):return f"{VIDEO_GENERATION_FAILED_MSG} {exc}"+ if isinstance(exc, NonFiniteFramesError):+ return f"{VIDEO_GENERATION_FAILED_MSG} {NON_FINITE_FRAMES_MSG}"native_memory = _native_memory_failure(exc)if native_memory:return f"{VIDEO_GENERATION_FAILED_MSG} {native_memory}"@@ -9617,6 +9624,7 @@ class VideoBackend:is_gguf = gguf_transformer,offload_active = speed_offload,audit = load_audit,+ logger = logger,apply = lambda family, log, view = view: apply_speed_optims(view,target,@@ -13132,6 +13140,7 @@ class VideoBackend:streamed = claim_stream(video_frames, fps, audio, int(audio_sample_rate))if streamed is not None:return streamed+ require_finite_frames(video_frames)from diffusers.utils.export_utils import encode_videotmp = tempfile.NamedTemporaryFile(suffix = ".mp4", delete = False)diff --git a/studio/backend/core/inference/video_frames.py b/studio/backend/core/inference/video_frames.pyindex 93fc1284ca4..1a55e1c5cc2 100644--- a/studio/backend/core/inference/video_frames.py+++ b/studio/backend/core/inference/video_frames.py@@ -51,6 +51,30 @@ def device_uint8(frames: Any) -> Optional[Any]:return host if bool(in_range) else None+class NonFiniteFramesError(RuntimeError):+ """The decoded clip holds NaN / inf: every encoder would refuse it (PyAV: "Expected numpy array with dtype uint8")."""+++NON_FINITE_FRAMES_MSG = "The decoded video holds NaN or infinite values, so it was not saved. See the server log in Settings > Logs."+++def require_finite_frames(frames: Any) -> None:+ """Raise ``NonFiniteFramesError`` when float ``frames`` (np array or tensor) hold NaN / inf; uint8 / PIL pass."""+ import numpy as np++ if isinstance(frames, np.ndarray):+ if np.issubdtype(frames.dtype, np.floating) and not bool(np.isfinite(frames).all()):+ raise NonFiniteFramesError(NON_FINITE_FRAMES_MSG)+ return+ try:+ import torch+ except Exception: # noqa: BLE001+ return+ if isinstance(frames, torch.Tensor) and frames.is_floating_point():+ if not bool(torch.isfinite(frames).all()):+ raise NonFiniteFramesError(NON_FINITE_FRAMES_MSG)++@contextlib.contextmanagerdef uint8_video_frames(pipe: Any) -> Iterator[None]:"""While active, ``pipe.video_processor``'s np / pil ``postprocess_video`` returns CPU uint8 (B, F, H, W, C)."""diff --git a/studio/backend/core/inference/video_stream_encode.py b/studio/backend/core/inference/video_stream_encode.pyindex 8f643adfae2..badbf44667f 100644--- a/studio/backend/core/inference/video_stream_encode.py+++ b/studio/backend/core/inference/video_stream_encode.py@@ -22,6 +22,8 @@ from typing import Any, Iterator, Optionalfrom .host_pinning import host_pinning_allowedSTREAM_ENCODE_ENV = "UNSLOTH_STUDIO_VIDEO_STREAM_ENCODE"+# Kill switch: "0" hides the decode below the session's wrapper from chain walkers again (as before).+STREAM_CHAIN_ENV = "UNSLOTH_STUDIO_VIDEO_STREAM_ENCODE_CHAIN"QUEUE_DEPTH = 8_STOP = object()@@ -32,6 +34,13 @@ def stream_encode_enabled() -> bool:return os.environ.get(STREAM_ENCODE_ENV, "").strip().lower() not in ("0", "false", "no", "off")+def _expose_inner(wrapper: Any, original: Any) -> Any:+ """Set ``__wrapped__`` so diffusion_vae_oom's chain walkers see the untiled gate below this wrapper."""+ if os.environ.get(STREAM_CHAIN_ENV, "").strip().lower() not in ("0", "false", "no", "off"):+ wrapper.__wrapped__ = original+ return wrapper++def _note(outcome: str, reason: str, logger: Any) -> None:STATS[outcome] += 1STATS["last"] = reason@@ -178,7 +187,7 @@ class StreamEncode:self._decoding = Falseself._decoded = True- return decode+ return _expose_inner(decode, original)def _tiled_wrapper(original: Any) -> Any:def tiled_decode(*args: Any, **kwargs: Any) -> Any:@@ -464,7 +473,7 @@ class H3StreamEncode(StreamEncode):self._decoding = Falseself._decoded = True- return decode+ return _expose_inner(decode, original)def _inner_wrapper(original: Any) -> Any:def _decode(*args: Any, **kwargs: Any) -> Any:diff --git a/studio/backend/tests/test_video_decode_oom_retry.py b/studio/backend/tests/test_video_decode_oom_retry.pynew file mode 100644index 00000000000..e0417111f92--- /dev/null+++ b/studio/backend/tests/test_video_decode_oom_retry.py@@ -0,0 +1,221 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""Kaggle T4 Wan crash: streamed untiled decode OOM, non-finite clip, fp32 rerun on non-finite latents."""++from __future__ import annotations++import ast+import types+from pathlib import Path++import numpy as np+import pytest++torch = pytest.importorskip("torch")+pytest.importorskip("av")+pytest.importorskip("diffusers")++try:+ from diffusers import AutoencoderKLWan # noqa: E402+ from diffusers.video_processor import VideoProcessor # noqa: E402+except (ImportError, RuntimeError) as exc:+ pytest.skip(f"diffusers is not importable here: {type(exc).__name__}", allow_module_level = True)++from core.inference import diffusion_speed as S # noqa: E402+from core.inference import diffusion_vae_oom as O # noqa: E402+from core.inference import video_frames as VF # noqa: E402+from core.inference import video_stream_encode as vse # noqa: E402+from core.inference import video_vae_untiled as U # noqa: E402++FAMILY = "wan2.1-t2v-1.3b"++[email protected](autouse = True)+def _few_threads():+ threads = torch.get_num_threads()+ torch.set_num_threads(min(4, threads))+ yield+ torch.set_num_threads(threads)+++def _vae():+ torch.manual_seed(0)+ vae = AutoencoderKLWan(+ base_dim = 16,+ z_dim = 4,+ dim_mult = [1, 1, 1, 1],+ num_res_blocks = 1,+ temperal_downsample = [False, True, True],+ latents_mean = [0.0] * 4,+ latents_std = [1.0] * 4,+ ).eval()+ vae.enable_tiling()+ return vae+++def _stack(monkeypatch):+ monkeypatch.setattr(U, "_free_bytes", lambda device: 100 * 2**30)+ vae = _vae()+ pipe = types.SimpleNamespace(vae = vae, video_processor = VideoProcessor(vae_scale_factor = 8))+ assert U.install_untiled_decode(pipe, FAMILY)+ assert O.install(vae, None, pipe = pipe)+ fired = []++ def oom_once(_module, _args, _kwargs):+ if not vae.use_tiling and not fired:+ fired.append(True)+ raise torch.OutOfMemoryError("CUDA out of memory. (injected)")++ vae.decoder.register_forward_pre_hook(oom_once, with_kwargs = True)+ return pipe, vae, fired+++def _gate_stats(vae):+ fn = vae.decode+ for _ in range(16):+ if getattr(fn, "_unsloth_untiled_decode", False):+ return fn._unsloth_untiled_stats+ fn = O.decode_layers.next_layer(fn)+ raise AssertionError("untiled gate not in the decode chain")+++def _render(pipe, z):+ session = vse.StreamEncode(pipe.vae, pipe.video_processor, 16)+ try:+ with torch.inference_mode(), session.hooks():+ return pipe.vae.decode(z, return_dict = False)[0]+ finally:+ session.close()+++Z = torch.randn(1, 4, 3, 40, 40, generator = torch.Generator().manual_seed(1))+++def test_streamed_render_untiled_oom_is_handled_by_the_gate(monkeypatch):+ """Fail-before: the wrapper hid the gate, so every later render OOMed again."""+ pipe, vae, fired = _stack(monkeypatch)+ out = _render(pipe, Z)+ assert fired == [True] and bool(torch.isfinite(out).all())+ stats, ladder = _gate_stats(vae), vae._unsloth_vae_oom_stats+ assert stats["oom_fallback"] == 1+ assert ladder["tiled_retry"] == 0+ fired.clear()+ fired.append(True)+ _render(pipe, Z)+ assert stats["tiled"] == 1 and stats["untiled"] == 0+++def test_streamed_render_leaves_the_oom_scope_installed(monkeypatch):+ """Fail-before: the scope reinstalled itself above the per-render wrapper, which hooks() then dropped."""+ pipe, vae, fired = _stack(monkeypatch)+ fired.append(True)+ _render(pipe, Z)+ assert O._scope_intact(vae)+++def test_stream_chain_kill_switch(monkeypatch):+ monkeypatch.setenv(vse.STREAM_CHAIN_ENV, "0")+ pipe, vae, fired = _stack(monkeypatch)+ _render(pipe, Z)+ assert _gate_stats(vae)["oom_fallback"] == 0+ assert vae._unsloth_vae_oom_stats["tiled_retry"] == 1+++def test_streamed_render_without_oom_is_unchanged(monkeypatch):+ pipe, vae, fired = _stack(monkeypatch)+ fired.append(True)+ got = _render(pipe, Z)+ ref = _vae()+ ref.disable_tiling()+ with torch.inference_mode():+ want = ref.decode(Z, return_dict = False)[0]+ assert torch.equal(got, want)+++def _half_pipe(monkeypatch):+ monkeypatch.setattr(S, "_fp16_compile_capable", lambda target: True)+ seen = []++ class Vae(torch.nn.Module):+ def __init__(self):+ super().__init__()+ self.decoder = torch.nn.Conv3d(1, 1, 1)++ def decode(+ self,+ z,+ return_dict = False,+ ):+ seen.append((z.dtype, next(self.decoder.parameters()).dtype))+ return (z * 1,)++ vae = Vae()+ pipe = types.SimpleNamespace(vae = vae)+ target = types.SimpleNamespace(device = "cuda", backend = "cuda", ordinal = None)+ family = types.SimpleNamespace(vae_force_fp32 = True)+ assert S._video_vae_half_decode(pipe, target, family, None)+ return vae, seen+++def test_nonfinite_latents_do_not_rerun_the_vae_in_fp32(monkeypatch):+ vae, seen = _half_pipe(monkeypatch)+ z = torch.zeros(1, 1, 2, 4, 4)+ z[0, 0, 1, 2, 2] = float("nan")+ out = vae.decode(z)[0]+ assert seen == [(torch.float16, torch.float16)]+ assert out.dtype == torch.float16 and not bool(torch.isfinite(out).all())+ vae.decode(torch.zeros(1, 1, 2, 4, 4))+ assert seen[-1] == (torch.float16, torch.float16)+++def test_nonfinite_fp16_output_from_finite_latents_still_reruns_fp32(monkeypatch):+ vae, seen = _half_pipe(monkeypatch)+ z = torch.full((1, 1, 2, 4, 4), 70000.0) # finite in fp32, inf in fp16+ out = vae.decode(z)[0]+ assert seen == [(torch.float16, torch.float16), (torch.float32, torch.float32)]+ assert bool(torch.isfinite(out).all())+++def test_nonfinite_latents_kill_switch(monkeypatch):+ monkeypatch.setenv(S.VIDEO_VAE_NONFINITE_INPUT_ENV, "0")+ vae, seen = _half_pipe(monkeypatch)+ z = torch.full((1, 1, 2, 4, 4), float("nan"))+ vae.decode(z)+ assert seen[-1] == (torch.float32, torch.float32)+++def test_require_finite_frames():+ VF.require_finite_frames(np.zeros((2, 4, 4, 3), np.uint8))+ VF.require_finite_frames(np.full((2, 4, 4, 3), 0.5, np.float32))+ VF.require_finite_frames(torch.zeros(2, 4, 4, 3, dtype = torch.uint8))+ bad = np.full((2, 4, 4, 3), 0.5, np.float32)+ bad[1, 2, 2, 0] = np.nan+ with pytest.raises(VF.NonFiniteFramesError):+ VF.require_finite_frames(bad)+ with pytest.raises(VF.NonFiniteFramesError):+ VF.require_finite_frames(torch.from_numpy(bad))+++def test_encode_mp4_refuses_a_nonfinite_clip_with_a_clear_error(monkeypatch):+ from core.inference import video as V++ monkeypatch.setattr(V, "nvenc_gpu", lambda logger = None: None)+ frames = np.full((2, 16, 16, 3), 0.5, np.float32)+ frames[0, 0, 0, 0] = np.nan+ with pytest.raises(VF.NonFiniteFramesError) as info:+ V.VideoBackend._encode_mp4(frames, 16, None, None)+ detail = V.video_failure_detail(info.value)+ assert detail.startswith(V.VIDEO_GENERATION_FAILED_MSG) and "NaN" in detail+++def test_video_speed_layer_gets_the_logger():+ src = (Path(__file__).resolve().parents[1] / "core" / "inference" / "video.py").read_text(+ encoding = "utf-8"+ )+ calls = [+ n+ for n in ast.walk(ast.parse(src))+ if isinstance(n, ast.Call) and getattr(n.func, "attr", None) == "run_speed_layer"+ ]+ assert calls and all(any(k.arg == "logger" for k in c.keywords) for c in calls)