|
| 1 | +# SPDX-FileCopyrightText: 2026 ModelCloud.ai |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +from importlib import import_module |
| 5 | +from types import SimpleNamespace |
| 6 | + |
| 7 | +import pytest |
| 8 | +import torch |
| 9 | + |
| 10 | +from gptqmodel import extension |
| 11 | +from tests.kernels import test_asymmetric_real_models as real_models |
| 12 | + |
| 13 | + |
| 14 | +_CASES = real_models._AWQ_REAL_CASES + real_models._GPTQ_REAL_CASES |
| 15 | + |
| 16 | + |
| 17 | +def _mock_device(monkeypatch, capability=(9, 0), shared_memory=227328, rocm=False): |
| 18 | + def get_capability(device=None): |
| 19 | + assert device == real_models._DEVICE |
| 20 | + return capability |
| 21 | + |
| 22 | + monkeypatch.setattr(torch.cuda, "is_available", lambda: True) |
| 23 | + monkeypatch.setattr(torch.cuda, "get_device_capability", get_capability) |
| 24 | + monkeypatch.setattr( |
| 25 | + torch.cuda, |
| 26 | + "get_device_properties", |
| 27 | + lambda device=None: SimpleNamespace( |
| 28 | + name="test GPU", |
| 29 | + shared_memory_per_block=49152, |
| 30 | + shared_memory_per_block_optin=shared_memory, |
| 31 | + ), |
| 32 | + ) |
| 33 | + for module_name in ( |
| 34 | + "gptqmodel.utils.machete", |
| 35 | + "gptqmodel.utils.marlin", |
| 36 | + *{case.candidate_cls.__module__ for case in _CASES}, |
| 37 | + ): |
| 38 | + monkeypatch.setattr(import_module(module_name), "IS_ROCM", rocm) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("case", _CASES, ids=lambda case: case.name) |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "capability,shared_memory,rocm,supported", |
| 44 | + [ |
| 45 | + ((9, 0), 227328, False, {case.name for case in _CASES}), |
| 46 | + ((8, 9), 102400, False, {"awq_marlin"}), |
| 47 | + ((10, 0), 227328, False, {"awq_marlin"}), |
| 48 | + ((7, 5), 65536, False, set()), |
| 49 | + ((9, 0), 102400, False, {"awq_marlin"}), |
| 50 | + ((9, 0), 227328, True, set()), |
| 51 | + ], |
| 52 | + ids=["hopper", "ada", "blackwell", "turing", "low_shared_memory", "rocm"], |
| 53 | +) |
| 54 | +def test_gate_checks_hardware_without_loading_extensions( |
| 55 | + monkeypatch, |
| 56 | + case, |
| 57 | + capability, |
| 58 | + shared_memory, |
| 59 | + rocm, |
| 60 | + supported, |
| 61 | +): |
| 62 | + _mock_device(monkeypatch, capability, shared_memory, rocm) |
| 63 | + |
| 64 | + def unexpected_load(*args, **kwargs): |
| 65 | + pytest.fail("Hardware gating must not load or compile an extension") |
| 66 | + |
| 67 | + monkeypatch.setattr(extension, "_load_one", unexpected_load) |
| 68 | + if case.name in supported: |
| 69 | + real_models._require_candidate_backend(case) |
| 70 | + else: |
| 71 | + with pytest.raises(pytest.skip.Exception): |
| 72 | + real_models._require_candidate_backend(case) |
| 73 | + |
| 74 | + |
| 75 | +@pytest.mark.parametrize("case", _CASES, ids=lambda case: case.name) |
| 76 | +def test_generation_propagates_extension_build_failure(monkeypatch, case): |
| 77 | + _mock_device(monkeypatch) |
| 78 | + message = "CUDA source compilation regression" |
| 79 | + |
| 80 | + def failed_load(*args, **kwargs): |
| 81 | + raise RuntimeError(message) |
| 82 | + |
| 83 | + monkeypatch.setattr(extension, "_load_one", failed_load) |
| 84 | + monkeypatch.setattr(extension, "error", lambda name: message) |
| 85 | + |
| 86 | + def load_model(model_id, backend): |
| 87 | + if backend == case.baseline_backend: |
| 88 | + return object() |
| 89 | + extension_name = "marlin_fp16" if case.name == "awq_marlin" else "machete" |
| 90 | + return extension.op(extension_name, "unused") |
| 91 | + |
| 92 | + monkeypatch.setattr(real_models, "_load_model", load_model) |
| 93 | + monkeypatch.setattr(real_models, "_generate_completion", lambda model: "baseline") |
| 94 | + monkeypatch.setattr(real_models, "_release_model", lambda model: None) |
| 95 | + |
| 96 | + # Exercise the actual test entry point: a supported host must reach model |
| 97 | + # loading and report the build error, rather than skip before loading. |
| 98 | + with pytest.raises(RuntimeError, match=message): |
| 99 | + try: |
| 100 | + real_models.test_asymmetric_real_generation_matches_torch_and_is_sane(case) |
| 101 | + except pytest.skip.Exception as exc: |
| 102 | + pytest.fail(f"Build failure was hidden by a skip: {exc}") |
0 commit comments