| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """Tests for autotuning Helion kernels, including disabled kernels with no configs.""" |
| 4 | |
| 5 | import pytest |
| 6 | import torch |
| 7 | |
| 8 | from vllm.utils.import_utils import has_helion |
| 9 | |
| 10 | if not has_helion(): |
| 11 | pytest.skip( |
| 12 | "Helion is not installed. Install with: pip install vllm[helion]", |
| 13 | allow_module_level=True, |
| 14 | ) |
| 15 | |
| 16 | import helion |
| 17 | import helion.language as hl |
| 18 | from helion.autotuner.base_search import BaseSearch |
| 19 | |
| 20 | from tests.kernels.helion.helpers import dummy_kernel_registry |
| 21 | from vllm.kernels.helion.register import create_helion_decorated_kernel |
| 22 | |
| 23 | |
| 24 | def _add_kernel(x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: |
| 25 | out = torch.empty_like(x) |
| 26 | for tile in hl.tile(x.size()): |
| 27 | out[tile] = x[tile] + y[tile] |
| 28 | return out |
| 29 | |
| 30 | |
| 31 | class NoCompileSearch(BaseSearch): |
| 32 | """Autotuner that returns the default config without GPU compilation. |
| 33 | |
| 34 | Modeled after helion's test BasicSearch (pytorch/helion#1649). |
| 35 | """ |
| 36 | |
| 37 | def autotune(self, *, skip_cache: bool = False): |
| 38 | return self.config_spec.default_config() |
| 39 | |
| 40 | |
| 41 | def _no_compile_autotuner_fn(bound_kernel, args, **kwargs): |
| 42 | return NoCompileSearch(bound_kernel, args, **kwargs) |
| 43 | |
| 44 | |
| 45 | class TestAutotuneDisabledKernel: |
| 46 | """Test autotuning flow on disabled kernels (no platform configs).""" |
| 47 | |
| 48 | def setup_method(self): |
| 49 | from vllm.kernels.helion.register import _REGISTERED_KERNELS |
| 50 | |
| 51 | self._saved_registry = dict(_REGISTERED_KERNELS) |
| 52 | _REGISTERED_KERNELS.clear() |
| 53 | |
| 54 | def teardown_method(self): |
| 55 | from vllm.kernels.helion.register import _REGISTERED_KERNELS |
| 56 | |
| 57 | _REGISTERED_KERNELS.clear() |
| 58 | _REGISTERED_KERNELS.update(self._saved_registry) |
| 59 | |
| 60 | def test_autotune_disabled_kernel_produces_valid_config(self): |
| 61 | """Register a kernel with no configs (disabled), run autotune, |
| 62 | verify it produces a valid helion.Config.""" |
| 63 | with dummy_kernel_registry(configs={}) as register: |
| 64 | wrapper = register( |
| 65 | "autotune_test_kernel", |
| 66 | config_picker=lambda args, keys: None, |
| 67 | fake_impl=lambda *a, **kw: None, |
| 68 | input_generator=lambda: { |
| 69 | "small": ( |
| 70 | torch.randn(4, 4, device="cuda"), |
| 71 | torch.randn(4, 4, device="cuda"), |
| 72 | ), |
| 73 | }, |
| 74 | )(_add_kernel) |
| 75 | |
| 76 | assert wrapper._disabled is True |
| 77 | |
| 78 | inputs = wrapper.get_inputs() |
| 79 | assert "small" in inputs |
| 80 | |
| 81 | settings = helion.Settings() |
| 82 | settings.autotuner_fn = _no_compile_autotuner_fn |
| 83 | wrapper.helion_settings = settings |
| 84 | |
| 85 | config = wrapper.run_autotune(inputs["small"]) |
| 86 | expected_default = ( |
| 87 | create_helion_decorated_kernel(_add_kernel, helion_settings=settings) |
| 88 | .bind(inputs["small"]) |
| 89 | .config_spec.default_config() |
| 90 | ) |
| 91 | assert config == expected_default |