|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +"""Unit tests for the framework-hinted GPU runtime selection.""" |
| 4 | + |
| 5 | +import sys |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from fastsafetensors import common |
| 10 | + |
| 11 | + |
| 12 | +class _FakeFramework: |
| 13 | + def __init__(self, cuda_ver): |
| 14 | + self._ver = cuda_ver |
| 15 | + |
| 16 | + def get_cuda_ver(self): |
| 17 | + if isinstance(self._ver, Exception): |
| 18 | + raise self._ver |
| 19 | + return self._ver |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(autouse=True) |
| 23 | +def _force_non_windows(monkeypatch): |
| 24 | + # The hint is intentionally a no-op on Windows (cudart resolver owns it). |
| 25 | + monkeypatch.setattr(sys, "platform", "linux") |
| 26 | + |
| 27 | + |
| 28 | +def test_none_framework_uses_autodetect(): |
| 29 | + assert common.resolve_runtime_lib_name(None) == "" |
| 30 | + |
| 31 | + |
| 32 | +def test_hip_framework_selects_amdhip(): |
| 33 | + assert ( |
| 34 | + common.resolve_runtime_lib_name(_FakeFramework("hip-7.2.0")) == "libamdhip64.so" |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +def test_cuda_framework_selects_cudart(): |
| 39 | + assert ( |
| 40 | + common.resolve_runtime_lib_name(_FakeFramework("cuda-12.1")) == "libcudart.so" |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize("ver", ["", "weird", "rocm-7.0"]) |
| 45 | +def test_unknown_vendor_uses_autodetect(ver): |
| 46 | + assert common.resolve_runtime_lib_name(_FakeFramework(ver)) == "" |
| 47 | + |
| 48 | + |
| 49 | +def test_get_cuda_ver_raises_uses_autodetect(): |
| 50 | + assert common.resolve_runtime_lib_name(_FakeFramework(RuntimeError("boom"))) == "" |
| 51 | + |
| 52 | + |
| 53 | +def test_windows_is_noop(monkeypatch): |
| 54 | + monkeypatch.setattr(sys, "platform", "win32") |
| 55 | + assert common.resolve_runtime_lib_name(_FakeFramework("hip-7.2.0")) == "" |
| 56 | + |
| 57 | + |
| 58 | +def test_load_library_func_hint_with_no_gpu_raises(monkeypatch): |
| 59 | + """A hint that finds no GPU is a hard failure""" |
| 60 | + from fastsafetensors.copier import nogds |
| 61 | + |
| 62 | + calls = [] |
| 63 | + |
| 64 | + def fake_load(lib): |
| 65 | + calls.append(lib) |
| 66 | + |
| 67 | + monkeypatch.setattr(nogds.fstcpp, "load_library_functions", fake_load) |
| 68 | + monkeypatch.setattr( |
| 69 | + nogds, "resolve_runtime_lib_name", lambda fw=None: "libamdhip64.so" |
| 70 | + ) |
| 71 | + |
| 72 | + monkeypatch.setattr(nogds, "is_gpu_found", lambda: False) |
| 73 | + monkeypatch.setattr(nogds, "_loaded_library", False) |
| 74 | + |
| 75 | + with pytest.raises(Exception, match="libamdhip64.so"): |
| 76 | + nogds.load_library_func(_FakeFramework("hip-7.2.0")) |
| 77 | + |
| 78 | + assert calls == ["libamdhip64.so"] |
| 79 | + assert nogds._loaded_library is False |
| 80 | + |
| 81 | + |
| 82 | +def test_load_library_func_hint_succeeds_no_fallback(monkeypatch): |
| 83 | + from fastsafetensors.copier import nogds |
| 84 | + |
| 85 | + calls = [] |
| 86 | + monkeypatch.setattr( |
| 87 | + nogds.fstcpp, "load_library_functions", lambda lib: calls.append(lib) |
| 88 | + ) |
| 89 | + monkeypatch.setattr( |
| 90 | + nogds, |
| 91 | + "resolve_runtime_lib_name", |
| 92 | + lambda fw=None: "libamdhip64.so" if fw is not None else "", |
| 93 | + ) |
| 94 | + monkeypatch.setattr(nogds, "is_gpu_found", lambda: True) |
| 95 | + monkeypatch.setattr(nogds, "_loaded_library", False) |
| 96 | + |
| 97 | + nogds.load_library_func(_FakeFramework("hip-7.2.0")) |
| 98 | + |
| 99 | + assert calls == ["libamdhip64.so"] |
0 commit comments