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: bf16 Wan VAE decode on ROCm (gfx11 / gfx12) (#12681)

Daniel Han6295d6cc9629
diff --git a/studio/backend/core/inference/diffusion_device.py b/studio/backend/core/inference/diffusion_device.py
index 2c7519b3ef6..1de1136bb6e 100644
--- a/studio/backend/core/inference/diffusion_device.py
+++ b/studio/backend/core/inference/diffusion_device.py
@@ -147,6 +147,130 @@ def install_decoder_sync(
return True
+VAE_BF16_DECODE_ENV = "UNSLOTH_VIDEO_VAE_BF16_DECODE"
+# RDNA3 / RDNA3.5 / RDNA4: bf16 WMMA. Measured on gfx1151 (Strix Halo); RDNA2 and older have no bf16 matrix path.
+_ROCM_BF16_DECODE_ARCH_PREFIXES = ("gfx11", "gfx12")
+
+
+def _rocm_bf16_decode_arch(torch: Any, target: DiffusionDeviceTarget) -> Optional[str]:
+ try:
+ index = target.ordinal if target.ordinal is not None else torch.cuda.current_device()
+ arch = str(getattr(torch.cuda.get_device_properties(index), "gcnArchName", "") or "")
+ except Exception: # noqa: BLE001 -- unreadable arch: keep fp32
+ return None
+ return arch if arch.startswith(_ROCM_BF16_DECODE_ARCH_PREFIXES) else None
+
+
+def _as_float32(value: Any, torch: Any) -> Any:
+ if isinstance(value, torch.Tensor):
+ return value.float() if value.is_floating_point() else value
+ if isinstance(value, tuple):
+ return tuple(_as_float32(v, torch) for v in value)
+ if isinstance(value, list):
+ return [_as_float32(v, torch) for v in value]
+ sample = getattr(value, "sample", None)
+ if isinstance(sample, torch.Tensor):
+ value.sample = _as_float32(sample, torch)
+ return value
+
+
+_VAE_BF16_OFF = ("0", "false", "off", "no")
+_VAE_BF16_FORCE = ("1", "true", "on", "yes")
+VAE_BF16_DECODE_MODES = ("weights", "autocast")
+
+
+def _vae_bf16_decode_request(gate: str) -> tuple[str, bool]:
+ """(mode, forced) for a non-off UNSLOTH_VIDEO_VAE_BF16_DECODE; unknown values cast weights, "1" also forces."""
+ if gate in VAE_BF16_DECODE_MODES:
+ return gate, False
+ return "weights", gate in _VAE_BF16_FORCE
+
+
+def _cast_float_args(torch: Any, dtype: Any) -> Any:
+ def _hook(module: Any, args: tuple) -> tuple:
+ return tuple(
+ a.to(dtype)
+ if isinstance(a, torch.Tensor) and a.is_floating_point() and a.dtype != dtype
+ else a
+ for a in args
+ )
+
+ return _hook
+
+
+def install_rocm_vae_bf16_decode(
+ pipe: Any,
+ target: DiffusionDeviceTarget,
+ *,
+ logger: Any = None,
+) -> Optional[str]:
+ """Decode an fp32-pinned video VAE (Wan) in bf16 on ROCm gfx11 / gfx12; returns the mode engaged, else None.
+
+ fp32 runs Wan's 3D convs as im2col plus a small-tile fp32 GEMM without matrix cores (~385 s of a 1280x704x21 clip on
+ gfx1151); ComfyUI decodes this VAE in bf16 on these cards. "weights" (default) casts only ``post_quant_conv`` +
+ ``decoder``, so ``vae.dtype`` and image-to-video encodes stay fp32; "autocast" keeps fp32 weights. Both return fp32.
+ UNSLOTH_VIDEO_VAE_BF16_DECODE: 0 off, auto / weights / autocast pick the mode, 1 also allows any bf16 CUDA device."""
+ gate = os.environ.get(VAE_BF16_DECODE_ENV, "auto").strip().lower()
+ if gate in _VAE_BF16_OFF or target.device != "cuda":
+ return None
+ vae = getattr(pipe, "vae", None)
+ decode = getattr(vae, "decode", None)
+ if not callable(decode) or getattr(decode, "_unsloth_bf16_decode", False):
+ return None
+ # NVIDIA's fp16 decode (diffusion_speed) owns the decoder dtype and recasts it to fp32 on a non-finite output.
+ if getattr(vae, "_unsloth_half_decode", False):
+ return None
+ import torch
+
+ if getattr(vae, "dtype", None) is not torch.float32:
+ return None
+ mode, forced = _vae_bf16_decode_request(gate)
+ if forced:
+ if not torch.cuda.is_bf16_supported():
+ return None
+ arch = "forced"
+ else:
+ if target.backend != "rocm":
+ return None
+ arch = _rocm_bf16_decode_arch(torch, target)
+ if arch is None:
+ return None
+
+ parts = [
+ m
+ for m in (getattr(vae, "post_quant_conv", None), getattr(vae, "decoder", None))
+ if isinstance(m, torch.nn.Module)
+ ]
+ if mode == "weights" and not parts:
+ mode = "autocast" # no separable decode half: cast nothing
+
+ if mode == "weights":
+ for part in parts:
+ part.to(torch.bfloat16)
+ # A path that reaches the decoder without vae.decode (a custom tiled / untiled decode) still gets bf16.
+ part.register_forward_pre_hook(_cast_float_args(torch, torch.bfloat16))
+
+ def _bf16_decode(z: Any, *args: Any, **kwargs: Any) -> Any:
+ if isinstance(z, torch.Tensor) and z.is_floating_point():
+ z = z.to(torch.bfloat16)
+ return _as_float32(decode(z, *args, **kwargs), torch)
+
+ else:
+
+ def _bf16_decode(*args: Any, **kwargs: Any) -> Any:
+ with torch.autocast(device_type = "cuda", dtype = torch.bfloat16):
+ out = decode(*args, **kwargs)
+ return _as_float32(out, torch)
+
+ _bf16_decode._unsloth_bf16_decode = True # type: ignore[attr-defined]
+ _bf16_decode.__wrapped__ = decode # type: ignore[attr-defined]
+ vae.decode = _bf16_decode
+ vae._unsloth_bf16_decode_mode = mode
+ if logger is not None:
+ logger.info("video.vae_decode: bf16 %s on %s", mode, arch)
+ return mode
+
+
def _studio_device_is(studio_device: Any, device_type: Any, name: str) -> bool:
"""True if ``studio_device`` equals ``DeviceType.<name>`` (when that member exists)."""
member = getattr(device_type, name, None)
diff --git a/studio/backend/core/inference/video.py b/studio/backend/core/inference/video.py
index e96462eedd6..ddbe119c299 100644
--- a/studio/backend/core/inference/video.py
+++ b/studio/backend/core/inference/video.py
@@ -87,6 +87,7 @@ from .diffusion_device import (
diffusion_device_scope,
force_float32_rope,
install_decoder_sync,
+ install_rocm_vae_bf16_decode,
pin_cuda_ordinal,
placed_cuda_ordinal,
resolve_diffusion_device_target,
@@ -6569,6 +6570,11 @@ class VideoBackend:
speed_optims += ("vae_untiled_when_fits",)
# Wan's decode also grows within a single tile, which tiling alone cannot bound.
install_decoder_sync(pipe, target, logger = logger)
+ # Last, so the bf16 entry wraps whatever decode path the steps above installed. Not on SPEED_OFF.
+ if getattr(fam, "vae_force_fp32", False) and effective_speed != SPEED_OFF:
+ vae_bf16_mode = install_rocm_vae_bf16_decode(pipe, target, logger = logger)
+ if vae_bf16_mode:
+ speed_optims += ("vae_bf16_decode", f"vae_bf16_{vae_bf16_mode}")
resolved = build_resolved_record(
{
diff --git a/studio/backend/tests/test_video_vae_bf16_decode_rocm.py b/studio/backend/tests/test_video_vae_bf16_decode_rocm.py
new file mode 100644
index 00000000000..077698e413b
--- /dev/null
+++ b/studio/backend/tests/test_video_vae_bf16_decode_rocm.py
@@ -0,0 +1,281 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+"""ROCm RDNA3+ decodes the fp32-pinned Wan VAE in bf16 (weights cast by default, autocast on request); every other
+device keeps the fp32 decode."""
+
+from __future__ import annotations
+
+import contextlib
+import types
+
+import pytest
+import torch
+
+from core.inference import diffusion_device as dd
+from core.inference.diffusion_device import DiffusionDeviceTarget, install_rocm_vae_bf16_decode
+
+
+def _target(
+ backend = "rocm",
+ vendor = "amd",
+ device = "cuda",
+):
+ return DiffusionDeviceTarget(
+ device = device,
+ dtype = torch.bfloat16,
+ backend = backend,
+ vendor = vendor,
+ supports_model_cpu_offload = True,
+ supports_default_torch_compile = backend != "rocm",
+ supports_pinned_transfer = True,
+ )
+
+
+class _FakeVAE:
+ def __init__(self, dtype = torch.float32):
+ self.dtype = dtype
+ self.calls = []
+ self.weight = torch.ones(2, dtype = dtype)
+
+ def decode(
+ self,
+ z,
+ return_dict = False,
+ ):
+ self.calls.append(_AUTOCAST[-1] if _AUTOCAST else None)
+ dtype = torch.bfloat16 if _AUTOCAST else torch.float32
+ return (z.to(dtype) * 2,)
+
+
+_AUTOCAST: list = []
+
+
+def gpu(monkeypatch):
+ state = {"arch": "gfx1151", "bf16": True}
+
+ @contextlib.contextmanager
+ def fake_autocast(device_type, dtype):
+ _AUTOCAST.append((device_type, dtype))
+ try:
+ yield
+ finally:
+ _AUTOCAST.pop()
+
+ monkeypatch.setattr(torch, "autocast", fake_autocast)
+ monkeypatch.setattr(torch.cuda, "current_device", lambda: 0)
+ monkeypatch.setattr(
+ torch.cuda,
+ "get_device_properties",
+ lambda i: types.SimpleNamespace(gcnArchName = state["arch"]),
+ )
+ monkeypatch.setattr(torch.cuda, "is_bf16_supported", lambda *a, **k: state["bf16"])
+ monkeypatch.delenv(dd.VAE_BF16_DECODE_ENV, raising = False)
+ return state
+
+
+def _pipe(dtype = torch.float32):
+ return types.SimpleNamespace(vae = _FakeVAE(dtype))
+
+
[email protected]("arch", ["gfx1151", "gfx1100", "gfx1201", "gfx1151:sramecc-:xnack-"])
+def test_rocm_rdna3_plus_decodes_under_bf16_autocast_and_returns_fp32(gpu, arch, monkeypatch):
+ monkeypatch.setenv(dd.VAE_BF16_DECODE_ENV, "autocast")
+ gpu["arch"] = arch
+ pipe = _pipe()
+ original = pipe.vae.decode
+ assert install_rocm_vae_bf16_decode(pipe, _target()) == "autocast"
+ out = pipe.vae.decode(torch.ones(3), return_dict = False)
+ assert pipe.vae.calls == [("cuda", torch.bfloat16)]
+ assert out[0].dtype == torch.float32 and torch.equal(out[0], torch.full((3,), 2.0))
+ assert (
+ pipe.vae.dtype is torch.float32 and pipe.vae.weight.dtype == torch.float32
+ ) # weights untouched
+ assert pipe.vae.decode.__wrapped__ == original
+
+
+def test_nvidia_is_untouched(gpu):
+ pipe = _pipe()
+ decode = pipe.vae.decode
+ assert install_rocm_vae_bf16_decode(pipe, _target(backend = "cuda", vendor = "nvidia")) is None
+ assert pipe.vae.decode == decode
+ assert pipe.vae.decode(torch.ones(1))[0].dtype == torch.float32 and pipe.vae.calls == [None]
+
+
[email protected]("arch", ["gfx1030", "gfx90a", "gfx942", ""])
+def test_other_rocm_arches_keep_fp32(gpu, arch):
+ gpu["arch"] = arch
+ pipe = _pipe()
+ decode = pipe.vae.decode
+ assert install_rocm_vae_bf16_decode(pipe, _target()) is None
+ assert pipe.vae.decode == decode
+
+
[email protected]("device", ["mps", "cpu", "xpu"])
+def test_non_cuda_devices_untouched(gpu, device):
+ pipe = _pipe()
+ assert install_rocm_vae_bf16_decode(pipe, _target(device = device)) is None
+
+
+def test_kill_switch(gpu, monkeypatch):
+ monkeypatch.setenv(dd.VAE_BF16_DECODE_ENV, "0")
+ pipe = _pipe()
+ assert install_rocm_vae_bf16_decode(pipe, _target()) is None
+
+
+def test_force_allows_nvidia_with_bf16(gpu, monkeypatch):
+ monkeypatch.setenv(dd.VAE_BF16_DECODE_ENV, "1")
+ assert (
+ install_rocm_vae_bf16_decode(_pipe(), _target(backend = "cuda", vendor = "nvidia"))
+ == "autocast"
+ ) # no decoder module to cast: autocast
+ gpu["bf16"] = False
+ assert install_rocm_vae_bf16_decode(_pipe(), _target(backend = "cuda", vendor = "nvidia")) is None
+
+
+def test_only_fp32_vaes_and_only_once(gpu):
+ assert install_rocm_vae_bf16_decode(_pipe(torch.bfloat16), _target()) is None
+ pipe = _pipe()
+ assert (
+ install_rocm_vae_bf16_decode(pipe, _target()) == "autocast"
+ ) # no decoder module to cast: autocast
+ assert install_rocm_vae_bf16_decode(pipe, _target()) is None # no double wrap
+ pipe.vae.decode(torch.ones(1))
+ assert pipe.vae.calls == [("cuda", torch.bfloat16)]
+
+
+def test_decoder_output_object_is_widened(gpu):
+ class Out:
+ def __init__(self, sample):
+ self.sample = sample
+
+ vae = _FakeVAE()
+ vae.decode = lambda z, return_dict = True: Out(z.to(torch.bfloat16))
+ pipe = types.SimpleNamespace(vae = vae)
+ assert (
+ install_rocm_vae_bf16_decode(pipe, _target()) == "autocast"
+ ) # no decoder module to cast: autocast
+ assert pipe.vae.decode(torch.ones(2)).sample.dtype == torch.float32
+
+
+def test_video_backend_gates_on_fp32_families_and_speed_tier():
+ from pathlib import Path
+
+ src = (Path(dd.__file__).with_name("video.py")).read_text(encoding = "utf-8")
+ i = src.index("install_rocm_vae_bf16_decode(pipe, target")
+ window = src[i - 400 : i]
+ assert (
+ 'getattr(fam, "vae_force_fp32", False)' in window
+ and "effective_speed != SPEED_OFF" in window
+ )
+
+
+def tiny_wan_vae():
+ # Built before the ``gpu`` fixture fakes the device properties diffusers reads on import.
+ diffusers = pytest.importorskip("diffusers")
+ torch.manual_seed(0)
+ return diffusers.AutoencoderKLWan(
+ base_dim = 8, z_dim = 4, dim_mult = [1, 2], num_res_blocks = 1, temperal_downsample = [True]
+ ).eval()
+
+
[email protected]("gate", [None, "auto", "weights", "1"])
+def test_default_casts_only_the_decode_half_and_matches_fp32(tiny_wan_vae, gpu, monkeypatch, gate):
+ if gate is not None:
+ monkeypatch.setenv(dd.VAE_BF16_DECODE_ENV, gate)
+ vae = tiny_wan_vae
+ z = torch.randn(1, 4, 2, 4, 4)
+ with torch.no_grad():
+ ref = vae.decode(z, return_dict = False)[0]
+ pipe = types.SimpleNamespace(vae = vae)
+ assert install_rocm_vae_bf16_decode(pipe, _target()) == "weights"
+ assert vae._unsloth_bf16_decode_mode == "weights"
+ assert all(p.dtype == torch.bfloat16 for p in vae.decoder.parameters())
+ assert all(p.dtype == torch.bfloat16 for p in vae.post_quant_conv.parameters())
+ assert all(p.dtype == torch.float32 for p in vae.encoder.parameters())
+ assert (
+ vae.dtype == torch.float32
+ ) # pipelines keep handing it fp32 latents; the encode stays fp32
+ with torch.no_grad():
+ out = vae.decode(z, return_dict = False)[0]
+ assert vae.encode(torch.randn(1, 3, 1, 8, 8)).latent_dist.mean.dtype == torch.float32
+ assert out.dtype == torch.float32 and out.shape == ref.shape
+ assert (out - ref).abs().max().item() < 0.05
+
+
+def test_decoder_reached_without_vae_decode_still_gets_bf16_inputs(tiny_wan_vae, gpu):
+ vae = tiny_wan_vae
+ pipe = types.SimpleNamespace(vae = vae)
+ assert install_rocm_vae_bf16_decode(pipe, _target()) == "weights"
+ with torch.no_grad():
+ x = vae.post_quant_conv(
+ torch.randn(1, 4, 1, 4, 4)
+ ) # fp32 input, bf16 weights: the pre-hook casts
+ assert x.dtype == torch.bfloat16
+
+
+def test_weights_mode_with_tiling_matches_fp32(tiny_wan_vae, gpu):
+ vae = tiny_wan_vae
+ vae.enable_tiling(
+ tile_sample_min_height = 16,
+ tile_sample_min_width = 16,
+ tile_sample_stride_height = 8,
+ tile_sample_stride_width = 8,
+ )
+ z = torch.randn(1, 4, 2, 4, 4)
+ with torch.no_grad():
+ ref = vae.decode(z, return_dict = False)[0]
+ assert install_rocm_vae_bf16_decode(types.SimpleNamespace(vae = vae), _target()) == "weights"
+ with torch.no_grad():
+ out = vae.decode(z, return_dict = False)[0]
+ assert out.dtype == torch.float32 and (out - ref).abs().max().item() < 0.05
+
+
+def test_autocast_mode_keeps_every_weight_fp32(tiny_wan_vae, gpu, monkeypatch):
+ monkeypatch.setenv(dd.VAE_BF16_DECODE_ENV, "autocast")
+ vae = tiny_wan_vae
+ assert install_rocm_vae_bf16_decode(types.SimpleNamespace(vae = vae), _target()) == "autocast"
+ assert all(p.dtype == torch.float32 for p in vae.parameters())
+
+
+def test_nvidia_real_vae_untouched(tiny_wan_vae, gpu):
+ vae = tiny_wan_vae
+ assert (
+ install_rocm_vae_bf16_decode(
+ types.SimpleNamespace(vae = vae), _target(backend = "cuda", vendor = "nvidia")
+ )
+ is None
+ )
+ assert all(p.dtype == torch.float32 for p in vae.parameters())
+
+
+ not torch.cuda.is_available(), reason = "needs a real CUDA device for the NVIDIA fp16 decode"
+)
+def test_forced_bf16_leaves_nvidia_fp16_decode_alone(monkeypatch):
+ # Stacked, the fp16 wrapper's non-finite fallback recasts the decoder to fp32 under bf16 input hooks: crash.
+ diffusers = pytest.importorskip("diffusers")
+ from core.inference import diffusion_speed as ds
+
+ monkeypatch.setenv(dd.VAE_BF16_DECODE_ENV, "1")
+ torch.manual_seed(0)
+ vae = (
+ diffusers.AutoencoderKLWan(
+ base_dim = 8, z_dim = 4, dim_mult = [1, 2], num_res_blocks = 1, temperal_downsample = [True]
+ )
+ .eval()
+ .cuda()
+ )
+ pipe = types.SimpleNamespace(vae = vae)
+ target = dd.diffusion_device_target_from_torch_device("cuda", torch.bfloat16)
+ if target.backend != "cuda" or not ds._video_vae_half_decode(
+ pipe, target, types.SimpleNamespace(vae_force_fp32 = True), None
+ ):
+ pytest.skip("this device has no NVIDIA fp16 VAE decode")
+ assert install_rocm_vae_bf16_decode(pipe, target) is None
+ with torch.no_grad():
+ vae.decode(torch.full((1, 4, 2, 4, 4), float("nan"), device = "cuda"), return_dict = False)
+ out = vae.decode(torch.randn(1, 4, 2, 4, 4, device = "cuda"), return_dict = False)[0]
+ assert out.dtype == torch.float32 and bool(torch.isfinite(out).all())