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: channels_last for 3D-conv VAEs and untiled A14B decode (#12672)
Daniel Han729cc3f3d309
diff --git a/studio/backend/core/inference/diffusion_speed.py b/studio/backend/core/inference/diffusion_speed.pyindex 4ccb7883bd0..2d880ce582a 100644--- a/studio/backend/core/inference/diffusion_speed.py+++ b/studio/backend/core/inference/diffusion_speed.py@@ -683,6 +683,9 @@ def _vae_channels_last(return Falsetry:import torch++ if _has_conv3d(vae):+ return _vae_channels_last_3d(vae, logger, fused = fused)vae.to(memory_format = torch.channels_last)return Trueexcept Exception as exc: # noqa: BLE001 - optimisation only@@ -690,13 +693,68 @@ def _vae_channels_last(return False+VAE_CHANNELS_LAST_3D_ENV = "UNSLOTH_VAE_CHANNELS_LAST_3D"+++def _has_conv3d(vae: Any) -> bool:+ modules = getattr(vae, "modules", None)+ if not callable(modules):+ return False+ import torch++ return any(isinstance(m, torch.nn.Conv3d) for m in modules())+++def _vae_channels_last_3d(vae: Any, logger: Any, *, fused: bool) -> bool:+ """Per-rank channels_last(_3d) on post_quant_conv + decoder; ``Module.to(channels_last)`` raises at the first 5D+ weight. Skipped without the fused passes (slower there); the encoder is kept (channels_last encoded slower)."""+ import torch++ name = type(vae).__name__+ if os.environ.get(VAE_CHANNELS_LAST_3D_ENV, "").strip().lower() in _VAE_FALSE_TOKENS:+ reason = f"{VAE_CHANNELS_LAST_3D_ENV}=0"+ elif not fused:+ reason = "3D-conv VAE without the fused VAE passes (channels_last_3d measured slower there)"+ else:+ reason = None+ if reason is not None:+ if logger is not None:+ logger.info("diffusion.speed: channels_last skipped for %s: %s", name, reason)+ return False+ relaid = 0+ for part in (getattr(vae, "post_quant_conv", None), getattr(vae, "decoder", None)):+ if part is None or not callable(getattr(part, "modules", None)):+ continue+ for module in part.modules():+ weight = getattr(module, "weight", None)+ if not isinstance(module, (torch.nn.Conv2d, torch.nn.Conv3d)) or not isinstance(+ weight, torch.Tensor+ ):+ continue+ fmt = torch.channels_last if weight.dim() == 4 else torch.channels_last_3d+ if not weight.is_contiguous(memory_format = fmt):+ weight.data = weight.data.contiguous(memory_format = fmt)+ relaid += 1+ if logger is not None:+ logger.info(+ "diffusion.speed: channels_last(_3d) decode weights on %s (%d convs)", name, relaid+ )+ return relaid > 0+++VIDEO_VAE_HALF_ENV = "UNSLOTH_VIDEO_VAE_HALF"++def _video_vae_half_decode(pipe: Any, target: Any, family: Any, logger: Any) -> bool:"""fp16 channels_last(_3d) decode for fp32-pinned video VAEs (Wan) on NVIDIA sm75+; non-finite output reruns fp32.- fp16, not bf16 (the pin exists because bf16 bands). channels_last_3d alone slows HV1.5 / LTX-2: keep it Wan-only.- """+ fp16, not bf16: bf16 is 9 dB further from fp32. ``UNSLOTH_VIDEO_VAE_HALF=0`` keeps fp32."""if not getattr(family, "vae_force_fp32", False) or getattr(target, "device", None) != "cuda":return False+ if os.environ.get(VIDEO_VAE_HALF_ENV, "").strip().lower() in _VAE_FALSE_TOKENS:+ if logger is not None:+ logger.info("diffusion.speed: video VAE decode kept in fp32 (%s=0)", VIDEO_VAE_HALF_ENV)+ return Falsevae = getattr(pipe, "vae", None)decoder = getattr(vae, "decoder", None)original = getattr(vae, "decode", None)diff --git a/studio/backend/core/inference/video_vae_untiled.py b/studio/backend/core/inference/video_vae_untiled.pyindex 115af1578fb..29d10d9ff18 100644--- a/studio/backend/core/inference/video_vae_untiled.py+++ b/studio/backend/core/inference/video_vae_untiled.py@@ -12,13 +12,16 @@ UNTILED_ENV = "UNSLOTH_VIDEO_VAE_UNTILED"# Untiled fp16 decode peak per latent pixel (h x w), measured on a B200; frame count does not enter (Wan's causal cache# decodes one latent frame at a time). Scaled by the decoder's element size: fp32 peaked at 21.1 GiB vs fp16 9.6 GiB.+# A14B: Wan2.1 VAE (8x spatial), measured <= 0.58 MiB per latent pixel at 480p / 720p._BYTES_PER_LATENT_PIXEL = {"wan2.2-ti2v-5b": 2.5 * 2**20,+ "wan2.2-t2v-a14b": 0.6 * 2**20,}# (spatial, temporal) upsampling to the RGB output, which Wan's decode holds about twice (per-frame torch.cat, then# clamp) and which grows with frames: 1021 frames at 1280x704 is 5.2 GiB per fp16 copy._OUTPUT_SCALE = {"wan2.2-ti2v-5b": (16, 4),+ "wan2.2-t2v-a14b": (8, 4),}_MARGIN = 1.25_MARGIN_BYTES = 2 * 2**30diff --git a/studio/backend/tests/test_vae_channels_last_3d.py b/studio/backend/tests/test_vae_channels_last_3d.pynew file mode 100644index 00000000000..027440c698c--- /dev/null+++ b/studio/backend/tests/test_vae_channels_last_3d.py@@ -0,0 +1,186 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++from __future__ import annotations++import logging+import types++import pytest++torch = pytest.importorskip("torch")++from core.inference import diffusion_speed as ds_mod # noqa: E402+from core.inference import video_vae_untiled as U # noqa: E402+++class _Fake3dVAE(torch.nn.Module):+ """Conv2d before the first Conv3d: Module.to(channels_last) converted it, then raised on the Conv3d."""++ def __init__(self) -> None:+ super().__init__()+ self.encoder = torch.nn.Sequential(torch.nn.Conv2d(3, 4, 3), torch.nn.Conv3d(4, 4, 3))+ self.post_quant_conv = torch.nn.Conv3d(4, 4, 1)+ self.decoder = torch.nn.Sequential(+ torch.nn.Conv2d(4, 4, 3), torch.nn.Conv3d(4, 4, 3), torch.nn.GroupNorm(2, 4)+ )+++def _layouts(vae):+ return {name: (p.stride(), p.data_ptr()) for name, p in vae.named_parameters()}+++def test_fused_3d_vae_gets_channels_last_decode_weights_per_rank_without_a_failure_warning(caplog):+ vae = _Fake3dVAE()+ encoder_before = {n: p.stride() for n, p in vae.encoder.named_parameters()}+ with caplog.at_level(logging.INFO, logger = "t3d"):+ assert ds_mod._vae_channels_last(+ types.SimpleNamespace(vae = vae), logging.getLogger("t3d"), fused = True+ )+ assert vae.decoder[0].weight.is_contiguous(memory_format = torch.channels_last)+ assert not vae.decoder[0].weight.is_contiguous()+ assert vae.decoder[1].weight.is_contiguous(memory_format = torch.channels_last_3d)+ assert not vae.decoder[1].weight.is_contiguous()+ assert vae.post_quant_conv.weight.is_contiguous(memory_format = torch.channels_last_3d)+ assert {n: p.stride() for n, p in vae.encoder.named_parameters()} == encoder_before+ assert vae.decoder[2].weight.is_contiguous()+ assert "failed" not in caplog.text+ assert "channels_last(_3d) decode weights" in caplog.text+++def test_unfused_3d_vae_is_skipped_untouched_and_logged(caplog):+ vae = _Fake3dVAE()+ before = _layouts(vae)+ with caplog.at_level(logging.INFO, logger = "t3d"):+ assert not ds_mod._vae_channels_last(+ types.SimpleNamespace(vae = vae), logging.getLogger("t3d")+ )+ assert _layouts(vae) == before+ assert "channels_last skipped" in caplog.text and "failed" not in caplog.text+++def test_env_switch_keeps_the_3d_vae_contiguous(monkeypatch):+ monkeypatch.setenv(ds_mod.VAE_CHANNELS_LAST_3D_ENV, "0")+ vae = _Fake3dVAE()+ before = _layouts(vae)+ assert not ds_mod._vae_channels_last(types.SimpleNamespace(vae = vae), None, fused = True)+ assert _layouts(vae) == before+++def test_2d_vae_still_takes_the_module_wide_channels_last():+ vae = torch.nn.Sequential(torch.nn.Conv2d(3, 4, 3), torch.nn.Conv2d(4, 4, 3))+ assert ds_mod._vae_channels_last(types.SimpleNamespace(vae = vae), None)+ assert all(m.weight.is_contiguous(memory_format = torch.channels_last) for m in vae)+++ "name", ["AutoencoderKLWan", "AutoencoderKLQwenImage", "AutoencoderKLHunyuanVideo15"]+)+def test_real_3d_vae_classes_take_the_3d_path(name):+ diffusers = pytest.importorskip("diffusers")+ cls = getattr(diffusers, name, None)+ if cls is None:+ # Skips because older diffusers releases predate this VAE class.+ pytest.skip(f"{name} not in this diffusers")+ kw = {+ "AutoencoderKLWan": dict(+ base_dim = 8, z_dim = 4, dim_mult = [1, 1], num_res_blocks = 1, temperal_downsample = [False]+ ),+ "AutoencoderKLQwenImage": dict(+ base_dim = 8, z_dim = 4, dim_mult = [1, 1], num_res_blocks = 1, temperal_downsample = [False]+ ),+ "AutoencoderKLHunyuanVideo15": dict(+ block_out_channels = (8, 8),+ latent_channels = 4,+ layers_per_block = 1,+ spatial_compression_ratio = 2,+ temporal_compression_ratio = 1,+ ),+ }[name]+ try:+ vae = cls(**kw)+ except Exception as exc: # noqa: BLE001 - constructor signature drift across diffusers releases+ # Skips because the tiny config's kwargs differ across diffusers releases.+ pytest.skip(f"cannot build a tiny {name}: {exc}")+ assert ds_mod._has_conv3d(vae)+ assert ds_mod._vae_channels_last(types.SimpleNamespace(vae = vae), None, fused = True)+ convs = [m for m in vae.decoder.modules() if isinstance(m, torch.nn.Conv3d)]+ assert convs and all(+ m.weight.is_contiguous(memory_format = torch.channels_last_3d) for m in convs+ )+ encoder = [m for m in vae.encoder.modules() if isinstance(m, torch.nn.Conv3d)]+ assert all(m.weight.is_contiguous() for m in encoder)+++class _FakeWanVAE(torch.nn.Module):+ def __init__(self) -> None:+ super().__init__()+ self.encoder = torch.nn.Conv3d(3, 4, 3)+ self.post_quant_conv = torch.nn.Conv3d(4, 4, 1)+ self.decoder = torch.nn.Sequential(torch.nn.Conv3d(4, 4, 3))++ def decode(+ self,+ z,+ return_dict = True,+ ):+ return (z,)+++def test_video_vae_half_kill_switch_keeps_fp32(monkeypatch):+ monkeypatch.setattr(ds_mod, "_fp16_compile_capable", lambda target: True)+ target = types.SimpleNamespace(+ device = "cuda", backend = "cuda", dtype = torch.bfloat16, ordinal = None+ )+ family = types.SimpleNamespace(vae_force_fp32 = True)+ monkeypatch.setenv(ds_mod.VIDEO_VAE_HALF_ENV, "0")+ pipe = types.SimpleNamespace(vae = _FakeWanVAE())+ assert ds_mod._video_vae_half_decode(pipe, target, family, None) is False+ assert (+ all(p.dtype == torch.float32 for p in pipe.vae.parameters())+ and "decode" not in pipe.vae.__dict__+ )+ monkeypatch.delenv(ds_mod.VIDEO_VAE_HALF_ENV)+ assert ds_mod._video_vae_half_decode(pipe, target, family, None) is True+ assert all(p.dtype == torch.float16 for p in pipe.vae.decoder.parameters())+++A14B = "wan2.2-t2v-a14b"+++def test_a14b_untiled_estimate_covers_the_measured_peak():+ # B200 untiled fp16 decode peaks over the pre-decode baseline (MiB).+ measured = {+ (21, 90, 160): 8989.3,+ (21, 60, 104): 3891.4,+ (11, 90, 160): 8764.9,+ (31, 90, 160): 9195.6,+ (41, 60, 104): 4078.1,+ }+ for (t, h, w), mib in measured.items():+ need = U.untiled_decode_bytes(A14B, (1, 16, t, h, w))+ assert need is not None and mib * 2**20 <= need <= 1.25 * mib * 2**20+ assert U.untiled_decode_bytes(A14B, (1, 16, 21, 90, 160), itemsize = 4) >= 17960.5 * 2**20+++def test_a14b_resident_vae_decodes_untiled_when_it_fits(monkeypatch):+ class _Tiled:+ def __init__(self) -> None:+ self.decoder = torch.nn.Conv3d(1, 1, 1).to(torch.float16)+ self.use_tiling = True+ self.calls: list = []++ def decode(+ self,+ z,+ return_dict = False,+ ):+ self.calls.append(self.use_tiling)+ return ("tiled" if self.use_tiling else "untiled",)++ vae = _Tiled()+ monkeypatch.setattr(U, "_free_bytes", lambda device: 100 * 2**30)+ assert U.install_untiled_decode(types.SimpleNamespace(vae = vae), A14B)+ assert vae.decode(torch.zeros(1, 16, 21, 90, 160)) == ("untiled",)+ monkeypatch.setattr(U, "_free_bytes", lambda device: 8 * 2**30)+ assert vae.decode(torch.zeros(1, 16, 21, 90, 160)) == ("tiled",)