code backup: tests
Browse files
tests/test_calibration/__init__.py
ADDED
|
File without changes
|
tests/test_calibration/test_report.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for calibration/report.py -- budget stats and the recommendation rule."""
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
from calibration import report as calibration_report
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def _record(question_id, score, forced=False, reasoning_tokens=100, seconds=1.0):
|
| 9 |
+
return {
|
| 10 |
+
"question_id": question_id,
|
| 11 |
+
"question_type": "object_counting",
|
| 12 |
+
"answer_expected": "4",
|
| 13 |
+
"metric": "MRA:.5:.95:.05",
|
| 14 |
+
"score": score,
|
| 15 |
+
"forced": forced,
|
| 16 |
+
"reasoning_token_count": reasoning_tokens,
|
| 17 |
+
"generation_seconds": seconds,
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_cell_stats_counts_forced_and_natural_lengths():
|
| 22 |
+
stats = calibration_report.cell_stats(
|
| 23 |
+
[
|
| 24 |
+
_record(1, 1.0, forced=False, reasoning_tokens=50),
|
| 25 |
+
_record(2, 0.0, forced=True, reasoning_tokens=512),
|
| 26 |
+
]
|
| 27 |
+
)
|
| 28 |
+
assert stats["count"] == 2
|
| 29 |
+
assert stats["forced_rate"] == 0.5
|
| 30 |
+
# Forced records are excluded from the natural-stop length mean.
|
| 31 |
+
assert stats["natural_reasoning_tokens_mean"] == 50
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def test_report_scores_only_the_shared_question_intersection():
|
| 35 |
+
grid = {
|
| 36 |
+
"m": {
|
| 37 |
+
256: {1: _record(1, 0.0), 2: _record(2, 1.0)},
|
| 38 |
+
512: {1: _record(1, 1.0)}, # never answered q2
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
result = calibration_report.report(grid)
|
| 42 |
+
assert result["m"]["questions"] == 1
|
| 43 |
+
assert result["m"]["budgets"][256]["count"] == 1
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_recommendation_picks_smallest_saturated_low_forced_budget():
|
| 47 |
+
grid = {
|
| 48 |
+
"m": {
|
| 49 |
+
256: {1: _record(1, 0.0, forced=True)},
|
| 50 |
+
512: {1: _record(1, 1.0, forced=False)},
|
| 51 |
+
2048: {1: _record(1, 1.0, forced=False)},
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
result = calibration_report.report(grid, tolerance=1.0, max_forced_rate=0.15)
|
| 55 |
+
assert result["m"]["recommended"] == 512
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def test_recommendation_rejects_high_forced_rate_even_at_best_accuracy():
|
| 59 |
+
grid = {
|
| 60 |
+
"m": {
|
| 61 |
+
256: {1: _record(1, 1.0, forced=True)}, # accurate but 100% forced
|
| 62 |
+
1024: {1: _record(1, 1.0, forced=False)},
|
| 63 |
+
}
|
| 64 |
+
}
|
| 65 |
+
result = calibration_report.report(grid, max_forced_rate=0.15)
|
| 66 |
+
assert result["m"]["recommended"] == 1024
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def test_load_grid_reads_the_full_config_layout(tmp_path):
|
| 70 |
+
cell = (
|
| 71 |
+
tmp_path / "qwen3.5-2b" / "explicit" / "metric" / "tracking"
|
| 72 |
+
/ "selective" / "64" / "512" / "scene_a"
|
| 73 |
+
)
|
| 74 |
+
cell.mkdir(parents=True)
|
| 75 |
+
(cell / "7.json").write_text(json.dumps(_record(7, 1.0)))
|
| 76 |
+
grid = calibration_report.load_grid(tmp_path)
|
| 77 |
+
assert grid == {
|
| 78 |
+
"qwen3.5-2b/explicit/metric/tracking/selective/64": {
|
| 79 |
+
512: {7: json.loads((cell / "7.json").read_text())}
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def test_load_grid_does_not_mistake_frame_count_dirs_for_budgets(tmp_path):
|
| 85 |
+
# The "64" frame-count level is numeric too -- only the budget leaf (whose
|
| 86 |
+
# children are scene folders with JSONs) may be treated as a budget.
|
| 87 |
+
cell = (
|
| 88 |
+
tmp_path / "qwen3.5-2b" / "explicit" / "metric" / "tracking"
|
| 89 |
+
/ "selective" / "64" / "512" / "scene_a"
|
| 90 |
+
)
|
| 91 |
+
cell.mkdir(parents=True)
|
| 92 |
+
(cell / "7.json").write_text(json.dumps(_record(7, 1.0)))
|
| 93 |
+
grid = calibration_report.load_grid(tmp_path)
|
| 94 |
+
assert list(grid) == ["qwen3.5-2b/explicit/metric/tracking/selective/64"]
|
| 95 |
+
assert list(grid["qwen3.5-2b/explicit/metric/tracking/selective/64"]) == [512]
|
tests/test_calibration/test_run.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for calibration/run.py -- operator-specified budget-grid orchestration."""
|
| 2 |
+
|
| 3 |
+
import pytest
|
| 4 |
+
|
| 5 |
+
from calibration import run as calibration_run
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_results_dir_isolates_every_pilot_axis():
|
| 9 |
+
base = ("qwen3.5-4b", "explicit", "metric", "tracking", "selective", 32, 512)
|
| 10 |
+
variants = {calibration_run.results_dir_for(*base)}
|
| 11 |
+
for index, value in [
|
| 12 |
+
(0, "qwen3.5-2b"), (1, "compact"), (2, "relative"), (3, "no tracking"),
|
| 13 |
+
(4, "uniform"), (5, 64), (6, 1024),
|
| 14 |
+
]:
|
| 15 |
+
changed = list(base)
|
| 16 |
+
changed[index] = value
|
| 17 |
+
variants.add(calibration_run.results_dir_for(*changed))
|
| 18 |
+
assert len(variants) == 8
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_build_plan_orders_cheapest_budget_first():
|
| 22 |
+
plan = calibration_run.build_plan(["m1", "m2"], [2048, 256])
|
| 23 |
+
assert plan == [("m1", 256), ("m2", 256), ("m1", 2048), ("m2", 2048)]
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def test_scenes_for_derives_scenes_and_rejects_unknown_ids(monkeypatch):
|
| 27 |
+
rows = [
|
| 28 |
+
{"id": 1, "scene_name": "scene_a"},
|
| 29 |
+
{"id": 2, "scene_name": "scene_b"},
|
| 30 |
+
{"id": 3, "scene_name": "scene_a"},
|
| 31 |
+
]
|
| 32 |
+
monkeypatch.setattr(calibration_run, "load_questions", lambda: list(rows))
|
| 33 |
+
assert calibration_run.scenes_for({1, 2, 3}) == ["scene_a", "scene_b"]
|
| 34 |
+
with pytest.raises(ValueError):
|
| 35 |
+
calibration_run.scenes_for({1, 999})
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def test_run_grid_passes_budget_questions_and_isolated_dir(monkeypatch):
|
| 39 |
+
launched = []
|
| 40 |
+
monkeypatch.setattr(calibration_run, "scenes_for", lambda ids: ["scene_a"])
|
| 41 |
+
monkeypatch.setattr(
|
| 42 |
+
calibration_run.harness_b_launch, "launch",
|
| 43 |
+
lambda model, fmt, sel, frames, scenes, **kwargs: launched.append(
|
| 44 |
+
(model, fmt, kwargs["reasoning_budget"], str(kwargs["results_dir"]),
|
| 45 |
+
kwargs["question_ids"], scenes)
|
| 46 |
+
),
|
| 47 |
+
)
|
| 48 |
+
calibration_run.run_grid(
|
| 49 |
+
["qwen3.5-2b"], [256, 512], [1, 2], "compact",
|
| 50 |
+
"metric", "tracking", "selective", 64,
|
| 51 |
+
)
|
| 52 |
+
assert [(m, f, b) for m, f, b, _, _, _ in launched] == [
|
| 53 |
+
("qwen3.5-2b", "compact", 256), ("qwen3.5-2b", "compact", 512),
|
| 54 |
+
]
|
| 55 |
+
assert launched[0][3] != launched[1][3]
|
| 56 |
+
assert launched[0][4] == {1, 2}
|
| 57 |
+
assert launched[0][5] == ["scene_a"]
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def test_cli_requires_exactly_one_question_source(monkeypatch, capsys):
|
| 61 |
+
monkeypatch.setattr(
|
| 62 |
+
"sys.argv",
|
| 63 |
+
["run", "--models", "qwen3.5-2b", "--budgets", "256",
|
| 64 |
+
"--spatial-code-format", "explicit", "--depth", "metric",
|
| 65 |
+
"--tracking", "tracking", "--input-selection", "selective", "--frames", "32"],
|
| 66 |
+
)
|
| 67 |
+
with pytest.raises(SystemExit):
|
| 68 |
+
calibration_run.main()
|
| 69 |
+
assert "exactly one of" in capsys.readouterr().err
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_cli_rejects_nonpositive_budget(monkeypatch, capsys):
|
| 73 |
+
monkeypatch.setattr(
|
| 74 |
+
"sys.argv",
|
| 75 |
+
["run", "--models", "qwen3.5-2b", "--budgets", "0", "--questions", "1",
|
| 76 |
+
"--spatial-code-format", "explicit", "--depth", "metric",
|
| 77 |
+
"--tracking", "tracking", "--input-selection", "selective", "--frames", "32"],
|
| 78 |
+
)
|
| 79 |
+
with pytest.raises(SystemExit):
|
| 80 |
+
calibration_run.main()
|
| 81 |
+
assert "must be positive" in capsys.readouterr().err
|