|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# This is a test for the AITER ops. |
| 3 | +# It tests if the AITER ops are |
| 4 | +# 1. correctly registered as custom ops |
| 5 | +# 2. correctly defined the relationship between |
| 6 | +# implementation and fake function |
| 7 | +# 3. can be used with torch.compile |
| 8 | +# This file will be skipped if AITER is not installed |
| 9 | +# and the platform is not ROCm. |
| 10 | + |
| 11 | +import importlib.util |
| 12 | + |
| 13 | +import pytest |
| 14 | +import torch |
| 15 | + |
| 16 | +# this import statement is needed to ensure the ops are registered |
| 17 | +import vllm.model_executor.layers.fused_moe.rocm_aiter_fused_moe # noqa: F401 |
| 18 | +from vllm.platforms import current_platform |
| 19 | + |
| 20 | +# need to import once to ensure the ops are registered |
| 21 | +# Check if aiter package is installed |
| 22 | +aiter_available = importlib.util.find_spec("aiter") is not None |
| 23 | + |
| 24 | +pytestmark = pytest.mark.skipif( |
| 25 | + not (current_platform.is_rocm() and aiter_available), |
| 26 | + reason="AITER ops are only available on ROCm with aiter package installed") |
| 27 | + |
| 28 | + |
| 29 | +def test_rocm_aiter_biased_grouped_topk_custom_op_registration(): |
| 30 | + """Test that the custom op is correctly registered.""" |
| 31 | + # Check if the op exists in torch.ops.vllm |
| 32 | + assert hasattr(torch.ops.vllm, 'rocm_aiter_biased_grouped_topk') |
| 33 | + |
| 34 | + # Check if the op is callable |
| 35 | + assert callable(torch.ops.vllm.rocm_aiter_biased_grouped_topk) |
| 36 | + |
| 37 | + |
| 38 | +def test_rocm_aiter_biased_grouped_topk_torch_compile_compatibility(): |
| 39 | + """Test that the op can be used with torch.compile.""" |
| 40 | + # Create test tensors |
| 41 | + token = 64 |
| 42 | + expert = 256 |
| 43 | + num_expert_group = 8 |
| 44 | + topk = 8 |
| 45 | + topk_group = 4 |
| 46 | + renormalize = True |
| 47 | + scale_factor = 1.0 |
| 48 | + |
| 49 | + gating_output = torch.randn((token, expert), |
| 50 | + dtype=torch.bfloat16, |
| 51 | + device="cuda") |
| 52 | + e_score_correction_bias = torch.randn((expert, ), |
| 53 | + dtype=torch.bfloat16, |
| 54 | + device="cuda") |
| 55 | + |
| 56 | + device = gating_output.device |
| 57 | + topk_ids = torch.empty((token, topk), dtype=torch.int32, device=device) |
| 58 | + topk_weights = torch.empty((token, topk), |
| 59 | + dtype=torch.float32, |
| 60 | + device=device) |
| 61 | + |
| 62 | + # Define a function that uses the op |
| 63 | + def biased_grouped_topk_fn(gating_output, e_score_correction_bias, |
| 64 | + topk_weights, topk_ids): |
| 65 | + return torch.ops.vllm.rocm_aiter_biased_grouped_topk( |
| 66 | + gating_output, e_score_correction_bias, topk_weights, topk_ids, |
| 67 | + num_expert_group, topk_group, renormalize, scale_factor) |
| 68 | + |
| 69 | + # Verify the op's fake implementation |
| 70 | + torch.library.opcheck( |
| 71 | + torch.ops.vllm.rocm_aiter_biased_grouped_topk, |
| 72 | + (gating_output, e_score_correction_bias, topk_weights, topk_ids), |
| 73 | + kwargs={ |
| 74 | + "num_expert_group": num_expert_group, |
| 75 | + "topk_group": topk_group, |
| 76 | + "need_renorm": renormalize, |
| 77 | + "routed_scaling_factor": scale_factor |
| 78 | + }, |
| 79 | + test_utils=("test_faketensor")) |
| 80 | + |
| 81 | + # Compile the function with appropriate settings |
| 82 | + compiled_fn = torch.compile(biased_grouped_topk_fn, |
| 83 | + fullgraph=True, |
| 84 | + backend="inductor", |
| 85 | + mode="reduce-overhead", |
| 86 | + dynamic=False) |
| 87 | + |
| 88 | + topk_weights_original = torch.empty((token, topk), |
| 89 | + dtype=torch.float32, |
| 90 | + device=device) |
| 91 | + topk_ids_original = torch.empty((token, topk), |
| 92 | + dtype=torch.int32, |
| 93 | + device=device) |
| 94 | + |
| 95 | + topk_weights_compiled = torch.empty((token, topk), |
| 96 | + dtype=torch.float32, |
| 97 | + device=device) |
| 98 | + topk_ids_compiled = torch.empty((token, topk), |
| 99 | + dtype=torch.int32, |
| 100 | + device=device) |
| 101 | + |
| 102 | + # Run both compiled (V1 graph mode) and uncompiled versions (V1 eager mode) |
| 103 | + biased_grouped_topk_fn(gating_output, e_score_correction_bias, |
| 104 | + topk_weights_original, topk_ids_original) |
| 105 | + compiled_fn(gating_output, e_score_correction_bias, topk_weights_compiled, |
| 106 | + topk_ids_compiled) |
| 107 | + |
| 108 | + # Sort the results for comparison since the order might not be deterministic |
| 109 | + topk_ids_original, indices_original = torch.sort(topk_ids_original) |
| 110 | + topk_weights_original = torch.gather(topk_weights_original, 1, |
| 111 | + indices_original) |
| 112 | + |
| 113 | + topk_ids_compiled, indices_compiled = torch.sort(topk_ids_compiled) |
| 114 | + topk_weights_compiled = torch.gather(topk_weights_compiled, 1, |
| 115 | + indices_compiled) |
| 116 | + |
| 117 | + # Verify results match |
| 118 | + assert torch.allclose(topk_weights_original, |
| 119 | + topk_weights_compiled, |
| 120 | + rtol=1e-2, |
| 121 | + atol=1e-2) |
| 122 | + assert torch.allclose(topk_ids_original, topk_ids_compiled) |
0 commit comments