diff --git a/python/sglang/multimodal_gen/runtime/models/vaes/minimax_h3_video_vae/attention.py b/python/sglang/multimodal_gen/runtime/models/vaes/minimax_h3_video_vae/attention.py index 5307a6a5a8..23fe4bec7a 100644 --- a/python/sglang/multimodal_gen/runtime/models/vaes/minimax_h3_video_vae/attention.py +++ b/python/sglang/multimodal_gen/runtime/models/vaes/minimax_h3_video_vae/attention.py @@ -45,6 +45,9 @@ def _vit_norm_input(module, hidden_states): def _try_fused_qk_rmsnorm_rope(norm_q, norm_k, query, key, rotary_pos_emb): if not _env_flag("MINIMAX_H3_VAE_DECODER_FUSED_NORM", "1"): return None + # NVIDIA only: see _fused_blocks_available in vae_vit.py. + if not current_platform.is_cuda(): + return None if ( rotary_pos_emb is None or torch.is_grad_enabled() diff --git a/python/sglang/multimodal_gen/runtime/models/vaes/minimax_h3_video_vae/vae_vit.py b/python/sglang/multimodal_gen/runtime/models/vaes/minimax_h3_video_vae/vae_vit.py index 6e1272c538..8c570c65d5 100644 --- a/python/sglang/multimodal_gen/runtime/models/vaes/minimax_h3_video_vae/vae_vit.py +++ b/python/sglang/multimodal_gen/runtime/models/vaes/minimax_h3_video_vae/vae_vit.py @@ -9,6 +9,8 @@ from diffusers.configuration_utils import ConfigMixin, register_to_config from diffusers.models.modeling_utils import ModelMixin from diffusers.utils import logging +from sglang.multimodal_gen.runtime.platforms import current_platform + from .base_module import RotaryEmbeddingND, TransformerBlock, _scaled_residual_add from .vit_utils import _env_flag, create_token_ids, prepare_rotary_pos_emb @@ -31,6 +33,10 @@ def _cuda_autocast_disabled(tensor: torch.Tensor): def _fused_blocks_available(blocks, hidden_states) -> bool: if not _env_flag("MINIMAX_H3_VAE_DECODER_FUSED_NORM", "1"): return False + # The fused Triton norms decode to all-NaN on ROCm (MI300), where + # tensor.is_cuda is also true; keep ROCm on the unfused fp32-norm path. + if not current_platform.is_cuda(): + return False if ( not hidden_states.is_cuda or hidden_states.dim() != 3 diff --git a/python/sglang/multimodal_gen/test/unit/test_minimax_h3_vae_parallel_modes.py b/python/sglang/multimodal_gen/test/unit/test_minimax_h3_vae_parallel_modes.py index 787c9fc6a8..352cd5a267 100644 --- a/python/sglang/multimodal_gen/test/unit/test_minimax_h3_vae_parallel_modes.py +++ b/python/sglang/multimodal_gen/test/unit/test_minimax_h3_vae_parallel_modes.py @@ -44,10 +44,15 @@ from sglang.multimodal_gen.runtime.models.vaes.minimax_h3_video_vae.vit_utils im create_token_ids, prepare_rotary_pos_emb, ) -from sglang.multimodal_gen.runtime.platforms import AttentionBackendEnum +from sglang.multimodal_gen.runtime.platforms import ( + AttentionBackendEnum, + current_platform, +) +# torch.cuda.is_available() is also true on ROCm, where these JIT/Triton +# kernels do not compile; gate on the platform instead. requires_cuda = pytest.mark.skipif( - not torch.cuda.is_available(), reason="fused qk-norm+RoPE kernel needs CUDA" + not current_platform.is_cuda(), reason="fused VAE kernels need CUDA" ) @@ -306,7 +311,7 @@ def _h3_shaped_decoder(): return decoder -@pytest.mark.skipif(not torch.cuda.is_available(), reason="requires CUDA") +@requires_cuda def test_fused_decoder_matches_unfused_forward(): from sglang.multimodal_gen.runtime.managers.forward_context import ( set_forward_context, @@ -346,3 +351,27 @@ def test_fused_decoder_matches_unfused_forward(): os.environ["MINIMAX_H3_VAE_DECODER_FUSED_NORM"] = previous torch.testing.assert_close(fused, eager, rtol=1e-3, atol=1e-3) + + +@pytest.mark.skipif(not torch.cuda.is_available(), reason="requires a GPU") +def test_fused_decoder_is_disabled_off_nvidia(): + # On ROCm tensor.is_cuda is true, but the fused Triton norms decoded to NaN + # on MI300; the decoder must keep the unfused path there. + decoder = _h3_shaped_decoder() + latent = torch.randn(2, 4, 1, 2, 2, device="cuda", dtype=torch.float16) + from sglang.kernels.ops import diffusion as diffusion_ops + + vae_dir = "sglang.multimodal_gen.runtime.models.vaes.minimax_h3_video_vae" + with ( + torch.no_grad(), + torch.autocast("cuda", dtype=torch.float16), + set_forward_context(current_timestep=0, attn_metadata=None), + mock.patch.dict(os.environ, {"MINIMAX_H3_VAE_DECODER_FUSED_NORM": "1"}), + mock.patch(f"{vae_dir}.vae_vit.current_platform.is_cuda", return_value=False), + mock.patch(f"{vae_dir}.attention.current_platform.is_cuda", return_value=False), + mock.patch.object(diffusion_ops, "h3_vae_scale_add_rmsnorm") as scale_add, + mock.patch.object(diffusion_ops, "h3_vae_qk_rmsnorm_rope") as qk_rope, + ): + decoder(latent) + scale_add.assert_not_called() + qk_rope.assert_not_called()