| 1 | #!/usr/bin/env python3 |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 4 | """Tests for W4A16 kernel selection logic (ROCm). |
| 5 | |
| 6 | Run `pytest tests/kernels/quantization/test_w4a16_kernel_selection.py`. |
| 7 | """ |
| 8 | |
| 9 | import pytest |
| 10 | import torch |
| 11 | |
| 12 | from vllm.model_executor.kernels.linear import ( |
| 13 | MPLinearLayerConfig, |
| 14 | choose_mp_linear_kernel, |
| 15 | ) |
| 16 | from vllm.platforms import current_platform |
| 17 | from vllm.scalar_type import scalar_types |
| 18 | |
| 19 | if current_platform.is_rocm(): |
| 20 | from vllm.platforms.rocm import on_gfx1x, on_gfx1100 |
| 21 | else: |
| 22 | on_gfx1100 = on_gfx1x = lambda: False # noqa: E731 |
| 23 | |
| 24 | # Group sizes the HIP skinny path of RDNAHybridW4A16LinearKernel instantiates. |
| 25 | _HYBRID_GROUP_SIZES = (32, 64, 128) |
| 26 | |
| 27 | |
| 28 | def _expected_rocm_kernel(weight_type, group_size: int) -> str: |
| 29 | """Mirror the ROCm priority order in ``_POSSIBLE_KERNELS[ROCM]``: |
| 30 | RDNA3 (gfx1100, symmetric uint4b8) -> Hybrid (gfx11/gfx12) -> Triton. |
| 31 | """ |
| 32 | if on_gfx1100() and weight_type == scalar_types.uint4b8: |
| 33 | return "RDNA3W4A16LinearKernel" |
| 34 | if on_gfx1x() and group_size in _HYBRID_GROUP_SIZES: |
| 35 | return "RDNAHybridW4A16LinearKernel" |
| 36 | return "TritonW4A16LinearKernel" |
| 37 | |
| 38 | |
| 39 | @pytest.mark.skipif(not current_platform.is_rocm(), reason="ROCm only") |
| 40 | def test_choose_mp_linear_kernel_uint4b8(): |
| 41 | # int4 weights, 16-bit activations (CT W4A16 typical config). |
| 42 | K, N = 1024, 256 |
| 43 | config = MPLinearLayerConfig( |
| 44 | full_weight_shape=(K, N), |
| 45 | partition_weight_shape=(K, N), |
| 46 | weight_type=scalar_types.uint4b8, # symmetric int4 (bias=8) |
| 47 | act_type=torch.float16, |
| 48 | group_size=128, |
| 49 | zero_points=False, |
| 50 | ) |
| 51 | |
| 52 | kernel_type = choose_mp_linear_kernel(config) |
| 53 | assert kernel_type.__name__ == _expected_rocm_kernel(scalar_types.uint4b8, 128) |
| 54 | |
| 55 | |
| 56 | @pytest.mark.skipif(not current_platform.is_rocm(), reason="ROCm only") |
| 57 | def test_choose_mp_linear_kernel_uint4_asymmetric(): |
| 58 | # Asymmetric int4 weights (explicit zero points). |
| 59 | K, N = 512, 512 |
| 60 | config = MPLinearLayerConfig( |
| 61 | full_weight_shape=(K, N), |
| 62 | partition_weight_shape=(K, N), |
| 63 | weight_type=scalar_types.uint4, # asymmetric int4 (explicit zeros) |
| 64 | act_type=torch.bfloat16, |
| 65 | group_size=64, |
| 66 | zero_points=True, |
| 67 | ) |
| 68 | |
| 69 | kernel_type = choose_mp_linear_kernel(config) |
| 70 | assert kernel_type.__name__ == _expected_rocm_kernel(scalar_types.uint4, 64) |