[XPU][Tests] Make tests device-agnostic (#51968)
Signed-off-by: pmanczak <pawel.manczak@intel.com>
This commit is contained in:
@@ -23,6 +23,13 @@ from vllm.platforms import current_platform
|
||||
from vllm.triton_utils import tl
|
||||
from vllm.utils.torch_utils import set_random_seed
|
||||
|
||||
DEVICE = current_platform.device_type
|
||||
|
||||
pytestmark = pytest.mark.skipif(
|
||||
not (current_platform.is_cuda_alike() or current_platform.is_xpu()),
|
||||
reason="Triton MoE kernels require CUDA/ROCm/XPU.",
|
||||
)
|
||||
|
||||
MNK_FACTORS = [
|
||||
(1, 128, 128),
|
||||
(1, 512, 512),
|
||||
@@ -71,19 +78,19 @@ class BatchedMMTensors:
|
||||
A = (
|
||||
torch.randn(
|
||||
(config.num_experts, config.max_tokens_per_expert, config.K),
|
||||
device="cuda",
|
||||
device=DEVICE,
|
||||
dtype=config.in_dtype,
|
||||
)
|
||||
/ 10
|
||||
)
|
||||
B = torch.randn(
|
||||
(config.num_experts, config.N, config.K),
|
||||
device="cuda",
|
||||
device=DEVICE,
|
||||
dtype=config.in_dtype,
|
||||
)
|
||||
C = torch.zeros(
|
||||
(config.num_experts, config.max_tokens_per_expert, config.N),
|
||||
device="cuda",
|
||||
device=DEVICE,
|
||||
dtype=config.out_dtype,
|
||||
)
|
||||
|
||||
@@ -91,7 +98,7 @@ class BatchedMMTensors:
|
||||
low=0,
|
||||
high=config.max_tokens_per_expert,
|
||||
size=(config.num_experts,),
|
||||
device="cuda",
|
||||
device=DEVICE,
|
||||
dtype=torch.int32,
|
||||
)
|
||||
|
||||
@@ -120,8 +127,10 @@ def test_batched_mm(
|
||||
|
||||
use_fp8_w8a8 = dtype == torch.float8_e4m3fn
|
||||
|
||||
if (dtype == torch.float8_e4m3fn) and not current_platform.has_device_capability(
|
||||
89
|
||||
if (
|
||||
dtype == torch.float8_e4m3fn
|
||||
and current_platform.is_cuda_alike()
|
||||
and not current_platform.has_device_capability(89)
|
||||
):
|
||||
pytest.skip(
|
||||
"Triton limitation: fp8e4nv data type is not supported on CUDA arch < 89"
|
||||
@@ -144,7 +153,7 @@ def test_batched_mm(
|
||||
low=0,
|
||||
high=max_tokens_per_expert,
|
||||
size=(num_experts,),
|
||||
device="cuda",
|
||||
device=DEVICE,
|
||||
dtype=torch.int32,
|
||||
)
|
||||
|
||||
@@ -169,9 +178,9 @@ def test_batched_mm(
|
||||
)
|
||||
|
||||
out_shape = (num_experts, max_tokens_per_expert, N)
|
||||
test_output = torch.zeros(out_shape, dtype=act_dtype, device="cuda")
|
||||
ref_output = torch.zeros(out_shape, dtype=act_dtype, device="cuda")
|
||||
q_ref_output = torch.zeros(out_shape, dtype=act_dtype, device="cuda")
|
||||
test_output = torch.zeros(out_shape, dtype=act_dtype, device=DEVICE)
|
||||
ref_output = torch.zeros(out_shape, dtype=act_dtype, device=DEVICE)
|
||||
q_ref_output = torch.zeros(out_shape, dtype=act_dtype, device=DEVICE)
|
||||
|
||||
compute_tl_dtype = {
|
||||
torch.float16: tl.float16,
|
||||
@@ -257,8 +266,10 @@ def test_fused_moe_batched_experts(
|
||||
|
||||
use_fp8_w8a8 = dtype == torch.float8_e4m3fn
|
||||
|
||||
if (dtype == torch.float8_e4m3fn) and not current_platform.has_device_capability(
|
||||
89
|
||||
if (
|
||||
dtype == torch.float8_e4m3fn
|
||||
and current_platform.is_cuda_alike()
|
||||
and not current_platform.has_device_capability(89)
|
||||
):
|
||||
pytest.skip(
|
||||
"Triton limitation: fp8e4nv data type is not supported on CUDA arch < 89"
|
||||
@@ -273,8 +284,8 @@ def test_fused_moe_batched_experts(
|
||||
if per_act_token_quant and block_shape is not None:
|
||||
pytest.skip("Skip illegal quantization test.")
|
||||
|
||||
a = torch.randn((m, k), device="cuda", dtype=torch.bfloat16) / 10
|
||||
score = torch.randn((m, e), device="cuda", dtype=torch.bfloat16)
|
||||
a = torch.randn((m, k), device=DEVICE, dtype=torch.bfloat16) / 10
|
||||
score = torch.randn((m, e), device=DEVICE, dtype=torch.bfloat16)
|
||||
|
||||
if dtype.itemsize == 1:
|
||||
act_dtype = torch.bfloat16
|
||||
@@ -294,8 +305,8 @@ def test_fused_moe_batched_experts(
|
||||
)
|
||||
|
||||
if input_scales and quant_dtype is not None:
|
||||
a1_scale = torch.tensor(1, device="cuda", dtype=torch.float32)
|
||||
a2_scale = torch.tensor(1, device="cuda", dtype=torch.float32)
|
||||
a1_scale = torch.tensor(1, device=DEVICE, dtype=torch.float32)
|
||||
a2_scale = torch.tensor(1, device=DEVICE, dtype=torch.float32)
|
||||
else:
|
||||
a1_scale = None
|
||||
a2_scale = None
|
||||
@@ -489,10 +500,7 @@ def test_batched_triton_experts_supports_current_device():
|
||||
BatchedTritonExperts,
|
||||
)
|
||||
|
||||
if current_platform.is_xpu() or current_platform.is_cuda_alike():
|
||||
assert BatchedTritonExperts._supports_current_device()
|
||||
else:
|
||||
pytest.skip("No GPU device available")
|
||||
assert BatchedTritonExperts._supports_current_device()
|
||||
|
||||
|
||||
@pytest.mark.parametrize("m,n,k,e,topk", [(32, 512, 512, 8, 2), (45, 1024, 128, 8, 1)])
|
||||
@@ -500,9 +508,6 @@ def test_batched_experts_end_to_end(m, n, k, e, topk):
|
||||
"""End-to-end BatchedTritonExperts via the reference (no-comms)
|
||||
BatchedPrepareAndFinalize, validated against a torch reference. Exercises
|
||||
the device-enablement path."""
|
||||
if not (current_platform.is_xpu() or current_platform.is_cuda_alike()):
|
||||
pytest.skip("No GPU device available")
|
||||
|
||||
from vllm.v1.worker.workspace import init_workspace_manager
|
||||
|
||||
set_random_seed(7)
|
||||
|
||||
@@ -10,6 +10,14 @@ import pytest
|
||||
import torch
|
||||
|
||||
from vllm.model_executor.layers.fused_moe.utils import count_expert_num_tokens
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
DEVICE = current_platform.device_type
|
||||
|
||||
pytestmark = pytest.mark.skipif(
|
||||
not (current_platform.is_cuda_alike() or current_platform.is_xpu()),
|
||||
reason="Triton MoE kernels require CUDA/ROCm/XPU.",
|
||||
)
|
||||
|
||||
|
||||
@dataclasses.dataclass
|
||||
@@ -88,9 +96,9 @@ def do_test_compute_expert_num_tokens(
|
||||
(num_local_experts), device="cpu", dtype=torch.int32
|
||||
)
|
||||
ref_impl(tt_rank, ref_expert_num_tokens)
|
||||
ref_expert_num_tokens = ref_expert_num_tokens.to("cuda")
|
||||
ref_expert_num_tokens = ref_expert_num_tokens.to(DEVICE)
|
||||
|
||||
tt_rank.to_device("cuda")
|
||||
tt_rank.to_device(DEVICE)
|
||||
# Test with expert_map
|
||||
triton_expert_num_tokens_w_emap = count_expert_num_tokens(
|
||||
tt_rank.topk_ids, num_local_experts, tt_rank.expert_map
|
||||
|
||||
@@ -13,6 +13,7 @@ from vllm.triton_utils import triton
|
||||
from vllm.utils.deep_gemm import is_deep_gemm_e8m0_used
|
||||
from vllm.utils.torch_utils import set_random_seed
|
||||
|
||||
DEVICE = current_platform.device_type
|
||||
FLOAT8_DTYPE = torch.float8_e4m3fn
|
||||
GROUP_SIZE = 128
|
||||
|
||||
@@ -61,7 +62,7 @@ def reference_quant(x: torch.Tensor, use_ue8m0: bool):
|
||||
|
||||
def reference(x: torch.Tensor, use_ue8m0: bool) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
T, N = x.size()
|
||||
ref_act_out = torch.empty((T, N // 2), dtype=torch.bfloat16, device="cuda")
|
||||
ref_act_out = torch.empty((T, N // 2), dtype=torch.bfloat16, device=DEVICE)
|
||||
torch.ops._C.silu_and_mul(ref_act_out, x)
|
||||
return reference_quant(ref_act_out, use_ue8m0)
|
||||
|
||||
@@ -93,7 +94,7 @@ def reference_with_clamp(
|
||||
def test_silu_mul_fp8_quant_deep_gemm(T: int, N: int):
|
||||
set_random_seed(42)
|
||||
|
||||
input = torch.rand((T, N), dtype=torch.bfloat16, device="cuda")
|
||||
input = torch.rand((T, N), dtype=torch.bfloat16, device=DEVICE)
|
||||
|
||||
use_ue8m0 = is_deep_gemm_e8m0_used()
|
||||
|
||||
@@ -122,7 +123,7 @@ def test_silu_mul_fp8_quant_deep_gemm_clamp(T: int, N: int, clamp_limit: float):
|
||||
# Use a wide distribution so values routinely exceed both clamp limits and
|
||||
# the clamp branch is actually exercised (uniform [0, 1) inputs would never
|
||||
# trigger it).
|
||||
input = torch.randn((T, N), dtype=torch.bfloat16, device="cuda") * 8.0
|
||||
input = torch.randn((T, N), dtype=torch.bfloat16, device=DEVICE) * 8.0
|
||||
|
||||
use_ue8m0 = is_deep_gemm_e8m0_used()
|
||||
|
||||
|
||||
@@ -23,6 +23,20 @@ from vllm.model_executor.layers.fused_moe.config import (
|
||||
from vllm.model_executor.layers.fused_moe.experts.triton_moe import TritonExperts
|
||||
from vllm.platforms import current_platform
|
||||
|
||||
DEVICE = current_platform.device_type
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.skipif(
|
||||
not (current_platform.is_cuda_alike() or current_platform.is_xpu()),
|
||||
reason="Triton MoE kernels require a CUDA-alike or XPU device.",
|
||||
),
|
||||
pytest.mark.skipif(
|
||||
current_platform.is_cuda_alike()
|
||||
and not current_platform.has_device_capability(80),
|
||||
reason="Triton MoE kernels require compute capability >= 8.0 on CUDA/ROCm.",
|
||||
),
|
||||
]
|
||||
|
||||
# Test parameters
|
||||
M_SIZES = [1, 16, 64]
|
||||
N_SIZES = [128, 256]
|
||||
@@ -86,7 +100,7 @@ def make_test_tensors(
|
||||
num_experts: int,
|
||||
topk: int,
|
||||
dtype: torch.dtype = torch.bfloat16,
|
||||
device: str = "cuda",
|
||||
device: str = DEVICE,
|
||||
):
|
||||
"""Create test tensors for MoE with non-gated activation.
|
||||
|
||||
@@ -106,10 +120,6 @@ def make_test_tensors(
|
||||
return hidden_states, w1, w2, topk_weights, topk_ids
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not current_platform.has_device_capability(80),
|
||||
reason="Requires compute capability >= 8.0",
|
||||
)
|
||||
@pytest.mark.parametrize("m", M_SIZES)
|
||||
@pytest.mark.parametrize("n", N_SIZES)
|
||||
@pytest.mark.parametrize("k", K_SIZES)
|
||||
@@ -192,10 +202,6 @@ def test_triton_experts_no_mul_activation(
|
||||
assert output.abs().sum() > 0, "Output is all zeros"
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not current_platform.has_device_capability(80),
|
||||
reason="Requires compute capability >= 8.0",
|
||||
)
|
||||
@torch.inference_mode()
|
||||
def test_workspace_shapes_no_mul_vs_gated():
|
||||
"""Test that workspace shapes differ correctly between gated and non-gated."""
|
||||
@@ -233,10 +239,6 @@ def test_workspace_shapes_no_mul_vs_gated():
|
||||
assert out_no_mul == out_gated == (M, K)
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
not current_platform.has_device_capability(80),
|
||||
reason="Requires compute capability >= 8.0",
|
||||
)
|
||||
@torch.inference_mode()
|
||||
def test_adjust_n_for_activation():
|
||||
"""Test the adjust_N_for_activation method."""
|
||||
|
||||
@@ -40,6 +40,8 @@ from vllm.platforms import current_platform
|
||||
from vllm.utils.deep_gemm import per_block_cast_to_fp8
|
||||
from vllm.utils.math_utils import round_up
|
||||
|
||||
DEVICE = current_platform.device_type
|
||||
|
||||
|
||||
def shuffle_weight(w: torch.Tensor) -> torch.Tensor:
|
||||
"""Fold weights to adjacent locations for Triton MoE / SwiGLU kernel layout."""
|
||||
@@ -80,7 +82,7 @@ def make_dummy_moe_config(
|
||||
moe_parallel_config=FusedMoEParallelConfig.make_no_parallel(),
|
||||
activation=activation,
|
||||
in_dtype=in_dtype,
|
||||
device="cuda",
|
||||
device=DEVICE,
|
||||
routing_method=RoutingMethodType.TopK,
|
||||
max_num_tokens=max_num_tokens,
|
||||
)
|
||||
@@ -238,7 +240,7 @@ def make_quantized_test_activations(
|
||||
block_shape: list[int] | None = None,
|
||||
per_act_token_quant: bool = False,
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None]:
|
||||
a = torch.randn((E, m, k), device="cuda", dtype=in_dtype) / 10
|
||||
a = torch.randn((E, m, k), device=DEVICE, dtype=in_dtype) / 10
|
||||
a_q = a
|
||||
a_scale = None
|
||||
|
||||
@@ -369,7 +371,7 @@ def make_test_weight(
|
||||
block_shape: list[int] | None = None,
|
||||
per_out_ch_quant: bool = False,
|
||||
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None, torch.Tensor | None]:
|
||||
w_16 = torch.randn((e, rows, cols), device="cuda", dtype=in_dtype) / 15
|
||||
w_16 = torch.randn((e, rows, cols), device=DEVICE, dtype=in_dtype) / 15
|
||||
|
||||
if quant_dtype is not None:
|
||||
w, w_s, w_gs = moe_quantize_weights(
|
||||
@@ -449,8 +451,8 @@ def make_test_quant_config(
|
||||
a1_gscale: torch.Tensor | None = None
|
||||
a2_gscale: torch.Tensor | None = None
|
||||
if quant_dtype == "nvfp4":
|
||||
a1_gscale = torch.ones((e,), device="cuda", dtype=torch.float32)
|
||||
a2_gscale = torch.ones((e,), device="cuda", dtype=torch.float32)
|
||||
a1_gscale = torch.ones((e,), device=DEVICE, dtype=torch.float32)
|
||||
a2_gscale = torch.ones((e,), device=DEVICE, dtype=torch.float32)
|
||||
a1_scale = a1_gscale
|
||||
a2_scale = a2_gscale
|
||||
else:
|
||||
@@ -551,8 +553,8 @@ def make_naive_shared_experts(
|
||||
K: int,
|
||||
in_dtype: torch.dtype = torch.bfloat16,
|
||||
) -> torch.nn.Module:
|
||||
w1 = torch.randn((K, N * 2), device="cuda", dtype=in_dtype) / 15
|
||||
w2 = torch.randn((N, K), device="cuda", dtype=in_dtype) / 15
|
||||
w1 = torch.randn((K, N * 2), device=DEVICE, dtype=in_dtype) / 15
|
||||
w2 = torch.randn((N, K), device=DEVICE, dtype=in_dtype) / 15
|
||||
return TestMLP(w1, w2, out_dtype=in_dtype)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user