|
1 | | -"""Tests for OneIG alignment (masking, wiring) and OneIG reasoning (LLM2CLIP).""" |
| 1 | +"""Tests for OneIG alignment masking and wiring.""" |
2 | 2 |
|
3 | 3 | from unittest.mock import MagicMock |
4 | 4 |
|
|
12 | 12 | aggregate_oneig_alignment_per_cell, |
13 | 13 | apply_oneig_dependency_mask, |
14 | 14 | ) |
15 | | -from pruna.evaluation.metrics.metric_oneig_reasoning import ( |
16 | | - OneIGReasoningMetric, |
17 | | - _LLM2CLIPScorer, |
18 | | -) |
19 | | -from pruna.evaluation.metrics.registry import MetricRegistry |
20 | 15 | from pruna.evaluation.metrics.vlm_base import BaseVLM |
21 | 16 |
|
22 | 17 |
|
@@ -125,107 +120,3 @@ def test_oneig_alignment_all_padding_questions_yields_zero_without_vlm() -> None |
125 | 120 | assert metric.compute().result == 0.0 |
126 | 121 | mock_vlm.score.assert_not_called() |
127 | 122 |
|
128 | | - |
129 | | -def test_to_oneig_record_strips_null_questions_and_dependencies() -> None: |
130 | | - """Null-valued Q_D entries are filtered out at record construction time.""" |
131 | | - row = {"category": "Anime_Stylization", "id": "001", "class": "None", "prompt_en": "a cat"} |
132 | | - questions_by_key = { |
133 | | - "anime_001": { |
134 | | - "questions": {"1": "Is there a cat?", "21": None}, |
135 | | - "dependencies": {"1": [0], "21": None}, |
136 | | - } |
137 | | - } |
138 | | - record = _to_oneig_record(row, questions_by_key, {}, {}) |
139 | | - assert "21" not in record["questions"] |
140 | | - assert "21" not in record["dependencies"] |
141 | | - assert record["questions"] == {"1": "Is there a cat?"} |
142 | | - assert record["dependencies"] == {"1": [0]} |
143 | | - |
144 | | - |
145 | | -def _make_mock_scorer(return_value: float = 0.5) -> MagicMock: |
146 | | - mock = MagicMock(spec=_LLM2CLIPScorer) |
147 | | - mock.score.return_value = [return_value] |
148 | | - return mock |
149 | | - |
150 | | - |
151 | | -@pytest.mark.cpu |
152 | | -def test_oneig_reasoning_uses_gt_answer_from_aux() -> None: |
153 | | - """Metric reads reasoning_gt_answer from aux.""" |
154 | | - mock_scorer = _make_mock_scorer(0.7) |
155 | | - metric = OneIGReasoningMetric(scorer=mock_scorer, device="cpu") |
156 | | - images = torch.rand(1, 3, 64, 64) |
157 | | - aux = {"reasoning_gt_answer": "A blue circle"} |
158 | | - metric.update(["p"], [aux], images) |
159 | | - result = metric.compute() |
160 | | - assert result.name == "oneig_reasoning" |
161 | | - assert result.result == 0.7 |
162 | | - mock_scorer.score.assert_called_once() |
163 | | - call_args = mock_scorer.score.call_args |
164 | | - assert call_args[0][1] == "A blue circle" |
165 | | - |
166 | | - |
167 | | -@pytest.mark.cpu |
168 | | -def test_oneig_reasoning_averages_per_sample_scores() -> None: |
169 | | - """Compute returns mean of per-sample scores.""" |
170 | | - mock_scorer = _make_mock_scorer(0.5) |
171 | | - metric = OneIGReasoningMetric(scorer=mock_scorer, device="cpu") |
172 | | - images = torch.rand(2, 3, 64, 64) |
173 | | - aux_list = [ |
174 | | - {"reasoning_gt_answer": "First answer"}, |
175 | | - {"reasoning_gt_answer": "Second answer"}, |
176 | | - ] |
177 | | - metric.update(["p1", "p2"], aux_list, images) |
178 | | - result = metric.compute() |
179 | | - assert result.result == 0.5 |
180 | | - assert mock_scorer.score.call_count == 2 |
181 | | - |
182 | | - |
183 | | -@pytest.mark.cpu |
184 | | -def test_oneig_reasoning_missing_gt_raises() -> None: |
185 | | - """Missing GT answer raises ValueError.""" |
186 | | - mock_scorer = _make_mock_scorer(0.8) |
187 | | - metric = OneIGReasoningMetric(scorer=mock_scorer, device="cpu") |
188 | | - images = torch.rand(1, 3, 64, 64) |
189 | | - aux = {} |
190 | | - with pytest.raises(ValueError, match="reasoning_gt_answer"): |
191 | | - metric.update(["p"], [aux], images) |
192 | | - mock_scorer.score.assert_not_called() |
193 | | - |
194 | | - |
195 | | -@pytest.mark.cpu |
196 | | -def test_oneig_reasoning_scorer_none_raises() -> None: |
197 | | - """When scorer returns None, metric raises RuntimeError.""" |
198 | | - mock_scorer = _make_mock_scorer() |
199 | | - mock_scorer.score.return_value = None |
200 | | - metric = OneIGReasoningMetric(scorer=mock_scorer, device="cpu") |
201 | | - images = torch.rand(1, 3, 64, 64) |
202 | | - aux = {"reasoning_gt_answer": "Some answer"} |
203 | | - with pytest.raises(RuntimeError, match="no scores"): |
204 | | - metric.update(["p"], [aux], images) |
205 | | - |
206 | | - |
207 | | -@pytest.mark.cpu |
208 | | -def test_oneig_reasoning_compute_without_update_raises() -> None: |
209 | | - """Compute with no updates raises RuntimeError.""" |
210 | | - mock_scorer = _make_mock_scorer() |
211 | | - metric = OneIGReasoningMetric(scorer=mock_scorer, device="cpu") |
212 | | - with pytest.raises(RuntimeError, match="no samples were scored"): |
213 | | - metric.compute() |
214 | | - |
215 | | - |
216 | | -@pytest.mark.cpu |
217 | | -def test_oneig_reasoning_has_metric_registered() -> None: |
218 | | - """oneig_reasoning is available via MetricRegistry (lazy).""" |
219 | | - assert MetricRegistry.has_metric("oneig_reasoning") |
220 | | - |
221 | | - |
222 | | -@pytest.mark.cpu |
223 | | -def test_transformers_major_version_supported_for_oneig_reasoning() -> None: |
224 | | - """Enforce pyproject ``transformers<5`` expectation for LLM2CLIP loading.""" |
225 | | - import transformers |
226 | | - |
227 | | - major = int(transformers.__version__.split(".", 1)[0]) |
228 | | - assert major < 5, ( |
229 | | - "oneig_reasoning expects transformers 4.x (see pyproject.toml); " |
230 | | - "5.x from_pretrained buffer initialization can break CLIP/Llama stacks." |
231 | | - ) |
0 commit comments