From 49f31d7cee425a6d38f8c5bc76877986daf832ed Mon Sep 17 00:00:00 2001 From: Andreas Karatzas Date: Sun, 26 Jul 2026 23:59:14 -0500 Subject: [PATCH] [ROCm] Make vllm_c RMSNorm output contiguous (#49913) Signed-off-by: Andreas Karatzas --- tests/kernels/ir/test_layernorm.py | 23 +++++++++++++++++++++++ vllm/kernels/vllm_c.py | 4 +++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/tests/kernels/ir/test_layernorm.py b/tests/kernels/ir/test_layernorm.py index 37ec1468a8..b25da988a7 100644 --- a/tests/kernels/ir/test_layernorm.py +++ b/tests/kernels/ir/test_layernorm.py @@ -176,6 +176,29 @@ def test_vllm_c_rms_norm_accepts_nd_input(): assert_close(ir.ops.rms_norm, output, ref_output) +@pytest.mark.skipif( + not current_platform.is_rocm(), + reason="ROCm vllm_c RMSNorm needs a contiguous output for strided inputs", +) +def test_vllm_c_rms_norm_accepts_transposed_input(): + impl = ir.ops.rms_norm.impls["vllm_c"] + if not impl.supported: + pytest.skip("vllm_c impl not supported on this platform") + + x = torch.randn( + 1, 320, 120, dtype=torch.float16, device=current_platform.device_type + ).transpose(1, 2) + assert x.reshape(-1, x.shape[-1]).stride(-1) != 1 + weight = torch.randn(320, dtype=torch.float16, device=current_platform.device_type) + epsilon = 1e-5 + + output = impl.impl_fn(x, weight, epsilon) + ref_output = rms_norm_native(x, weight, epsilon) + + assert output.shape == x.shape + assert_close(ir.ops.rms_norm, output, ref_output) + + fused_add_rms_norm_native = ir.ops.fused_add_rms_norm.impls["native"].impl_fn diff --git a/vllm/kernels/vllm_c.py b/vllm/kernels/vllm_c.py index 6ae5d9939e..0df4011f6d 100644 --- a/vllm/kernels/vllm_c.py +++ b/vllm/kernels/vllm_c.py @@ -32,7 +32,9 @@ def rms_norm( if IS_ROCM and (x.dim() > 2 or not x.is_contiguous()): original_shape = x.shape x = x.reshape(-1, original_shape[-1]) - output = torch.empty_like(x) + # empty_like preserves the strides of transposed inputs, but the + # libtorch-stable kernel requires a contiguous output tensor. + output = torch.empty(x.shape, device=x.device, dtype=x.dtype) torch.ops._C.rms_norm(output, x, weight, epsilon) return output.reshape(original_shape)