File size: 6,133 Bytes
e8055cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""Tests for harness/B/run.py -- result-record shape and result-file writing."""

import json

from harness import B
from harness.B import run as harness_run

_FAKE_ANSWER = {
    "prompt_text": "<rendered chat template>",
    "answer_text": "4",
    "answer_raw": "<|im_start|>assistant\n4<|im_end|>",
    "input_token_count": 2558,
    "vision_input_shapes": {"mm_token_type_ids": [1, 2558]},
    "output_token_ids": [19, 151645],
    "output_token_count": 2,
    "hit_token_limit": False,
    "eos_token_ids": [151645],
    "generation_seconds": 0.65,
    "device": "cuda",
    "dtype": "bfloat16",
    "library_versions": {"transformers": "5.14.1", "torch": "2.13.0+cu130"},
    "generation_config": {
        "max_new_tokens": 16,
        "do_sample": False,
        "temperature": 0.0,
        "top_p": None,
        "top_k": None,
        "enable_thinking": False,
    },
}

_FAKE_ROW = {
    "id": 7,
    "scene_name": "scene0001_00",
    "dataset": "scannet",
    "question_type": "object_counting",
    "question": "How many chairs?",
    "options": None,
    "ground_truth": "4",
}

_FAKE_CODE_INFO = {
    "protocol": "thinking",
    "spatial_code_format": "explicit",
    "input_selection": "selective",
    "frame_count": 64,
    "depth": "metric",
    "tracking": "tracking",
    "spatial_code_path": "/workspace/data/spatial codes/.../scene0001_00.json",
}


def test_results_dir_for_matches_established_dimension_nesting():
    root = harness_run.results_dir_for(
        "qwen3.5-4b", "thinking", "explicit", "metric", "tracking", "uniform", 32
    )
    assert root == (
        B.RESULTS_DIR
        / "qwen3.5-4b"
        / "explicit"
        / "metric"
        / "tracking"
        / "uniform"
        / "32"
    )


def test_results_dir_for_keeps_protocols_together():
    base = harness_run.results_dir_for(
        "qwen3.5-4b", "base", "explicit", "metric", "tracking", "uniform", 32
    )
    extended = harness_run.results_dir_for(
        "qwen3.5-4b", "thinking", "explicit", "metric", "tracking", "uniform", 32
    )
    assert base == extended


def test_results_dir_for_honors_explicit_override(tmp_path):
    root = harness_run.results_dir_for(
        "qwen3.5-4b",
        "base",
        "explicit",
        "relative",
        "no tracking",
        "selective",
        16,
        tmp_path,
    )
    assert root == tmp_path


def test_build_record_preserves_every_field_untruncated():
    record = harness_run._build_record(
        _FAKE_ROW,
        "full prompt text",
        _FAKE_ANSWER,
        "MRA:.5:.95:.05",
        1.0,
        "qwen3.5-4b",
        "/root/models/qwen3.5-4b",
        _FAKE_CODE_INFO,
    )
    assert record["question"] == "How many chairs?"
    assert record["full_prompt"] == "full prompt text"
    assert record["rendered_prompt"] == _FAKE_ANSWER["prompt_text"]
    assert record["answer_given"] == "4"
    assert record["answer_raw"] == _FAKE_ANSWER["answer_raw"]
    assert record["spatial_code_format"] == "explicit"
    assert record["input_selection"] == "selective"
    assert record["frame_count"] == 64
    assert record["depth"] == "metric"
    assert record["tracking"] == "tracking"
    assert record["spatial_code_path"] == _FAKE_CODE_INFO["spatial_code_path"]
    assert record["condition"] == "thinking:explicit:metric:tracking:selective:64"
    assert record["protocol"] == "thinking"
    assert record["vision_input_shapes"] == {"mm_token_type_ids": [1, 2558]}
    assert record["generation_config"] == _FAKE_ANSWER["generation_config"]
    assert record["metric"] == "MRA:.5:.95:.05"
    assert record["score"] == 1.0
    assert record["scene"] == "scene0001_00"
    assert record["question_id"] == 7
    # No frame-provenance fields -- B has no video frames.
    assert "frame_selection" not in record
    assert "video_path" not in record
    assert "frame_indices" not in record


def test_write_question_result_writes_one_json_file_per_question(tmp_path):
    path, record = harness_run.write_question_result(
        _FAKE_ROW,
        "full prompt text",
        _FAKE_ANSWER,
        "MRA:.5:.95:.05",
        1.0,
        "qwen3.5-4b",
        "/root/models/qwen3.5-4b",
        _FAKE_CODE_INFO,
        results_dir=tmp_path,
    )
    assert path == tmp_path / "scene0001_00" / "7.json"
    on_disk = json.loads(path.read_text())
    assert on_disk == record


def test_build_record_carries_reasoning_fields_when_forced():
    extended_answer = {
        **_FAKE_ANSWER,
        "reasoning_text": "long reasoning about the spatial code",
        "reasoning_raw": "long reasoning about the spatial code<|im_end|>",
        "reasoning_token_ids": list(range(50)),
        "reasoning_token_count": 50,
        "reasoning_hit_limit": True,
        "forced": True,
        "forced_input_token_count": 2510,
    }
    record = harness_run._build_record(
        _FAKE_ROW,
        "full prompt text",
        extended_answer,
        "MRA:.5:.95:.05",
        1.0,
        "qwen3.5-4b",
        "/root/models/qwen3.5-4b",
        _FAKE_CODE_INFO,
    )
    assert record["reasoning_text"] == "long reasoning about the spatial code"
    assert record["reasoning_raw"] == "long reasoning about the spatial code<|im_end|>"
    assert record["reasoning_token_ids"] == list(range(50))
    assert record["reasoning_token_count"] == 50
    assert record["reasoning_hit_limit"] is True
    assert record["forced"] is True
    assert record["forced_input_token_count"] == 2510



def test_video_results_use_video_branch():
    assert (
        harness_run.results_dir_for(
            "qwen3.5-4b", "thinking", "explicit", "metric", "tracking", "video", None
        )
        == B.RESULTS_DIR / "qwen3.5-4b" / "explicit" / "metric" / "tracking" / "video"
    )


def test_video_record_has_no_frame_count_in_condition():
    info = dict(_FAKE_CODE_INFO, input_selection="video", frame_count=None)
    record = harness_run._build_record(
        _FAKE_ROW, "prompt", _FAKE_ANSWER, "metric", 1.0, "qwen3.5-4b", "/model", info
    )
    assert record["condition"] == "thinking:explicit:metric:tracking:video"
    assert record["frame_count"] is None