|
| 1 | +"""Tests for PyTorch integration extractor.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from unittest.mock import MagicMock |
| 6 | + |
| 7 | +from wildedge.integrations.pytorch import ( |
| 8 | + PytorchExtractor, |
| 9 | +) |
| 10 | +from wildedge.integrations.pytorch import ( |
| 11 | + _detect_accelerator as torch_detect_accelerator, |
| 12 | +) |
| 13 | +from wildedge.integrations.pytorch import ( |
| 14 | + _detect_quantization as torch_detect_quantization, |
| 15 | +) |
| 16 | +from wildedge.model import ModelHandle, ModelInfo |
| 17 | + |
| 18 | + |
| 19 | +def make_handle(publish_spy) -> ModelHandle: |
| 20 | + info = ModelInfo( |
| 21 | + model_name="test", |
| 22 | + model_version="1.0", |
| 23 | + model_source="local", |
| 24 | + model_format="test", |
| 25 | + ) |
| 26 | + return ModelHandle(model_id="m", info=info, publish=publish_spy) |
| 27 | + |
| 28 | + |
| 29 | +class _TorchBase: |
| 30 | + """Looks like torch.nn.Module to the MRO check.""" |
| 31 | + |
| 32 | + |
| 33 | +_TorchBase.__name__ = "Module" |
| 34 | +_TorchBase.__module__ = "torch.nn.modules.module" |
| 35 | + |
| 36 | + |
| 37 | +class _FakeParam: |
| 38 | + class _Device: |
| 39 | + type = "cpu" |
| 40 | + |
| 41 | + device = _Device() |
| 42 | + dtype = "torch.float32" |
| 43 | + |
| 44 | + |
| 45 | +class _FakeTorchModel(_TorchBase): |
| 46 | + def parameters(self): |
| 47 | + yield _FakeParam() |
| 48 | + |
| 49 | + def modules(self): |
| 50 | + return iter([self]) |
| 51 | + |
| 52 | + def register_forward_pre_hook(self, hook): |
| 53 | + self._pre_hook = hook |
| 54 | + return MagicMock() |
| 55 | + |
| 56 | + def register_forward_hook(self, hook): |
| 57 | + self._post_hook = hook |
| 58 | + return MagicMock() |
| 59 | + |
| 60 | + |
| 61 | +class TestPytorchExtractor: |
| 62 | + extractor = PytorchExtractor() |
| 63 | + |
| 64 | + def test_can_handle_torch_module(self): |
| 65 | + assert self.extractor.can_handle(_FakeTorchModel()) is True |
| 66 | + |
| 67 | + def test_can_handle_rejects_plain_object(self): |
| 68 | + assert self.extractor.can_handle(object()) is False |
| 69 | + |
| 70 | + def test_detect_accelerator_reads_parameter_device(self): |
| 71 | + model = _FakeTorchModel() |
| 72 | + assert torch_detect_accelerator(model) == "cpu" |
| 73 | + |
| 74 | + def test_detect_accelerator_cuda(self): |
| 75 | + model = _FakeTorchModel() |
| 76 | + model.parameters = lambda: iter([MagicMock(device=MagicMock(type="cuda"))]) |
| 77 | + assert torch_detect_accelerator(model) == "cuda" |
| 78 | + |
| 79 | + def test_detect_accelerator_no_parameters_falls_back(self): |
| 80 | + model = _FakeTorchModel() |
| 81 | + model.parameters = lambda: iter([]) |
| 82 | + assert isinstance(torch_detect_accelerator(model), str) |
| 83 | + |
| 84 | + def test_detect_quantization_by_module_name(self): |
| 85 | + model = _FakeTorchModel() |
| 86 | + |
| 87 | + class QuantizedLinear: |
| 88 | + pass |
| 89 | + |
| 90 | + QuantizedLinear.__module__ = "torch.nn.quantized" |
| 91 | + model.modules = lambda: iter([QuantizedLinear()]) |
| 92 | + model.parameters = lambda: iter([]) |
| 93 | + assert torch_detect_quantization(model) == "int8" |
| 94 | + |
| 95 | + def test_detect_quantization_by_param_dtype(self): |
| 96 | + model = _FakeTorchModel() |
| 97 | + model.modules = lambda: iter([]) |
| 98 | + model.parameters = lambda: iter([MagicMock(dtype="torch.float16")]) |
| 99 | + assert torch_detect_quantization(model) == "f16" |
| 100 | + |
| 101 | + def test_detect_quantization_by_param_dtype_bf16(self): |
| 102 | + model = _FakeTorchModel() |
| 103 | + model.modules = lambda: iter([]) |
| 104 | + model.parameters = lambda: iter([MagicMock(dtype="torch.bfloat16")]) |
| 105 | + assert torch_detect_quantization(model) == "bf16" |
| 106 | + |
| 107 | + def test_detect_quantization_by_param_dtype_qint(self): |
| 108 | + model = _FakeTorchModel() |
| 109 | + model.modules = lambda: iter([]) |
| 110 | + model.parameters = lambda: iter([MagicMock(dtype="torch.qint8")]) |
| 111 | + assert torch_detect_quantization(model) == "int8" |
| 112 | + |
| 113 | + def test_detect_quantization_by_param_dtype_quint(self): |
| 114 | + model = _FakeTorchModel() |
| 115 | + model.modules = lambda: iter([]) |
| 116 | + model.parameters = lambda: iter([MagicMock(dtype="torch.quint8")]) |
| 117 | + assert torch_detect_quantization(model) == "int8" |
| 118 | + |
| 119 | + def test_detect_quantization_by_param_dtype_int8(self): |
| 120 | + model = _FakeTorchModel() |
| 121 | + model.modules = lambda: iter([]) |
| 122 | + model.parameters = lambda: iter([MagicMock(dtype="torch.int8")]) |
| 123 | + assert torch_detect_quantization(model) == "int8" |
| 124 | + |
| 125 | + def test_detect_quantization_returns_none_when_unknown(self): |
| 126 | + model = _FakeTorchModel() |
| 127 | + model.modules = lambda: iter([]) |
| 128 | + model.parameters = lambda: iter([MagicMock(dtype="torch.float32")]) |
| 129 | + assert torch_detect_quantization(model) is None |
| 130 | + |
| 131 | + def test_detect_quantization_returns_none_on_exception(self): |
| 132 | + model = _FakeTorchModel() |
| 133 | + model.modules = lambda: iter([]) |
| 134 | + |
| 135 | + def broken_parameters(): |
| 136 | + raise RuntimeError("broken params") |
| 137 | + |
| 138 | + model.parameters = broken_parameters |
| 139 | + assert torch_detect_quantization(model) is None |
| 140 | + |
| 141 | + def test_extract_info_uses_class_name_as_model_id(self): |
| 142 | + model = _FakeTorchModel() |
| 143 | + model_id, info = self.extractor.extract_info(model, {}) |
| 144 | + assert model_id == "_FakeTorchModel" |
| 145 | + assert info.model_format == "pytorch" |
| 146 | + |
| 147 | + def test_extract_info_override_model_id(self): |
| 148 | + model = _FakeTorchModel() |
| 149 | + model_id, _ = self.extractor.extract_info(model, {"id": "my-resnet"}) |
| 150 | + assert model_id == "my-resnet" |
| 151 | + |
| 152 | + def test_install_hooks_publishes_inference(self, publish_spy): |
| 153 | + model = _FakeTorchModel() |
| 154 | + handle = make_handle(publish_spy) |
| 155 | + self.extractor.install_hooks(model, handle) |
| 156 | + model._pre_hook(model, (None,)) |
| 157 | + model._post_hook(model, (None,), None) |
| 158 | + assert len(publish_spy.events) == 1 |
| 159 | + assert publish_spy.events[0]["event_type"] == "inference" |
| 160 | + |
| 161 | + def test_install_hooks_sets_detected_accelerator(self, publish_spy): |
| 162 | + model = _FakeTorchModel() |
| 163 | + handle = make_handle(publish_spy) |
| 164 | + self.extractor.install_hooks(model, handle) |
| 165 | + assert handle.detected_accelerator == "cpu" |
0 commit comments