@@ -0,0 +1,149 @@ |
| 1 | +"""InferencePlan resolver — audit F05 cross-hardware coverage.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +from types import SimpleNamespace |
| 8 | + |
| 9 | +from dlm.hardware.backend import Backend |
| 10 | +from dlm.inference.plan import InferencePlan, resolve_inference |
| 11 | + |
| 12 | + |
| 13 | +def _caps( |
| 14 | + *, |
| 15 | + backend: Backend, |
| 16 | + supports_bf16: bool = False, |
| 17 | + has_bitsandbytes: bool = False, |
| 18 | + has_flash_attention: bool = False, |
| 19 | +) -> object: |
| 20 | + return SimpleNamespace( |
| 21 | + backend=backend, |
| 22 | + supports_bf16=supports_bf16, |
| 23 | + has_bitsandbytes=has_bitsandbytes, |
| 24 | + has_flash_attention=has_flash_attention, |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +def _write_pinned(adapter_dir: Path, *, bnb: str | None) -> None: |
| 29 | + adapter_dir.mkdir(parents=True, exist_ok=True) |
| 30 | + (adapter_dir / "pinned_versions.json").write_text( |
| 31 | + json.dumps({"torch": "2.4.0", "bitsandbytes": bnb}) |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +class TestQLoRAOnCUDAWithBnb: |
| 36 | + def test_loads_4bit_native(self, tmp_path: Path) -> None: |
| 37 | + _write_pinned(tmp_path, bnb="0.43.1") |
| 38 | + plan = resolve_inference( |
| 39 | + tmp_path, _caps(backend=Backend.CUDA, supports_bf16=True, has_bitsandbytes=True) |
| 40 | + ) |
| 41 | + assert plan.backend == Backend.CUDA |
| 42 | + assert plan.precision == "bf16" |
| 43 | + assert plan.dequantize_on_load is False |
| 44 | + assert "4-bit" in plan.reason |
| 45 | + |
| 46 | + |
| 47 | +class TestQLoRAOnCUDAWithoutBnb: |
| 48 | + def test_dequantizes(self, tmp_path: Path) -> None: |
| 49 | + _write_pinned(tmp_path, bnb="0.43.1") |
| 50 | + plan = resolve_inference(tmp_path, _caps(backend=Backend.CUDA, has_bitsandbytes=False)) |
| 51 | + assert plan.dequantize_on_load is True |
| 52 | + assert plan.precision == "fp16" |
| 53 | + assert "bitsandbytes not installed" in plan.reason |
| 54 | + |
| 55 | + |
| 56 | +class TestQLoRAOnMPS: |
| 57 | + """Audit F05 canonical case — CUDA-trained QLoRA resumed on Apple Silicon.""" |
| 58 | + |
| 59 | + def test_dequantizes_to_fp16(self, tmp_path: Path) -> None: |
| 60 | + _write_pinned(tmp_path, bnb="0.43.1") |
| 61 | + plan = resolve_inference(tmp_path, _caps(backend=Backend.MPS)) |
| 62 | + assert plan.backend == Backend.MPS |
| 63 | + assert plan.precision == "fp16" |
| 64 | + assert plan.dequantize_on_load is True |
| 65 | + assert plan.attn_implementation == "sdpa" |
| 66 | + assert "F05" in plan.reason |
| 67 | + |
| 68 | + |
| 69 | +class TestLoRANonCUDA: |
| 70 | + def test_mps_plain_lora(self, tmp_path: Path) -> None: |
| 71 | + _write_pinned(tmp_path, bnb=None) |
| 72 | + plan = resolve_inference(tmp_path, _caps(backend=Backend.MPS)) |
| 73 | + assert plan.precision == "fp16" |
| 74 | + assert plan.dequantize_on_load is False |
| 75 | + |
| 76 | + def test_cpu_plain_lora(self, tmp_path: Path) -> None: |
| 77 | + _write_pinned(tmp_path, bnb=None) |
| 78 | + plan = resolve_inference(tmp_path, _caps(backend=Backend.CPU)) |
| 79 | + assert plan.dequantize_on_load is False |
| 80 | + |
| 81 | + |
| 82 | +class TestLoRAOnCUDA: |
| 83 | + def test_bf16_when_supported(self, tmp_path: Path) -> None: |
| 84 | + _write_pinned(tmp_path, bnb=None) |
| 85 | + plan = resolve_inference(tmp_path, _caps(backend=Backend.CUDA, supports_bf16=True)) |
| 86 | + assert plan.precision == "bf16" |
| 87 | + assert plan.dequantize_on_load is False |
| 88 | + |
| 89 | + def test_fp16_when_bf16_unsupported(self, tmp_path: Path) -> None: |
| 90 | + _write_pinned(tmp_path, bnb=None) |
| 91 | + plan = resolve_inference(tmp_path, _caps(backend=Backend.CUDA, supports_bf16=False)) |
| 92 | + assert plan.precision == "fp16" |
| 93 | + |
| 94 | + |
| 95 | +class TestAttnImplPick: |
| 96 | + def test_flash_attn_when_available(self, tmp_path: Path) -> None: |
| 97 | + _write_pinned(tmp_path, bnb="0.43.1") |
| 98 | + plan = resolve_inference( |
| 99 | + tmp_path, |
| 100 | + _caps( |
| 101 | + backend=Backend.CUDA, |
| 102 | + supports_bf16=True, |
| 103 | + has_bitsandbytes=True, |
| 104 | + has_flash_attention=True, |
| 105 | + ), |
| 106 | + ) |
| 107 | + assert plan.attn_implementation == "flash_attention_2" |
| 108 | + |
| 109 | + def test_sdpa_default(self, tmp_path: Path) -> None: |
| 110 | + _write_pinned(tmp_path, bnb=None) |
| 111 | + plan = resolve_inference(tmp_path, _caps(backend=Backend.CUDA)) |
| 112 | + assert plan.attn_implementation == "sdpa" |
| 113 | + |
| 114 | + |
| 115 | +class TestMissingPinnedFile: |
| 116 | + def test_no_pinned_versions_treated_as_lora(self, tmp_path: Path) -> None: |
| 117 | + """Missing `pinned_versions.json` is conservative: assume LoRA.""" |
| 118 | + plan = resolve_inference(tmp_path, _caps(backend=Backend.MPS)) |
| 119 | + assert plan.dequantize_on_load is False |
| 120 | + |
| 121 | + def test_malformed_pinned_file_treated_as_lora(self, tmp_path: Path) -> None: |
| 122 | + (tmp_path / "pinned_versions.json").write_text("not json {{{") |
| 123 | + plan = resolve_inference(tmp_path, _caps(backend=Backend.MPS)) |
| 124 | + assert plan.dequantize_on_load is False |
| 125 | + |
| 126 | + |
| 127 | +class TestPlanSerialization: |
| 128 | + def test_to_dict_is_json_friendly(self, tmp_path: Path) -> None: |
| 129 | + _write_pinned(tmp_path, bnb=None) |
| 130 | + plan = resolve_inference(tmp_path, _caps(backend=Backend.MPS)) |
| 131 | + data = plan.to_dict() |
| 132 | + # Round-trip via json to prove serializability. |
| 133 | + encoded = json.dumps(data) |
| 134 | + decoded = json.loads(encoded) |
| 135 | + assert decoded["backend"] == "mps" |
| 136 | + assert decoded["precision"] == "fp16" |
| 137 | + |
| 138 | + def test_plan_is_frozen(self, tmp_path: Path) -> None: |
| 139 | + import dataclasses |
| 140 | + |
| 141 | + _write_pinned(tmp_path, bnb=None) |
| 142 | + plan = resolve_inference(tmp_path, _caps(backend=Backend.MPS)) |
| 143 | + assert isinstance(plan, InferencePlan) |
| 144 | + try: |
| 145 | + plan.precision = "bf16" # type: ignore[misc] |
| 146 | + except dataclasses.FrozenInstanceError: |
| 147 | + pass |
| 148 | + else: |
| 149 | + raise AssertionError("frozen=True not enforced") |