| """Tests for harness/A/run.py -- question loading, scoring, and result-file writing.""" |
|
|
| import json |
|
|
| import pytest |
|
|
| from harness import A |
| from harness.A 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": 123, |
| "vision_input_shapes": {"pixel_values": [512, 1536]}, |
| "output_token_ids": [19, 151645], |
| "output_token_count": 2, |
| "hit_token_limit": False, |
| "eos_token_ids": [151645], |
| "generation_seconds": 1.234, |
| "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, |
| }, |
| } |
|
|
| _FAKE_ROW = { |
| "id": 7, |
| "scene_name": "scene0001_00", |
| "dataset": "scannet", |
| "question_type": "object_counting", |
| "question": "How many chairs?", |
| "options": None, |
| "ground_truth": "4", |
| } |
|
|
| _FAKE_FRAME_INFO = { |
| "protocol": "base", |
| "video_path": "/root/data/VSI-Bench/scannet/scene0001_00.mp4", |
| "frame_timestamps": [0.0, 1.0, 2.0], |
| "frame_indices": [0, 30, 60], |
| "frame_selection": "uniform", |
| "frame_count": 16, |
| } |
|
|
|
|
| def test_load_questions_reads_every_row(tmp_path): |
| jsonl = tmp_path / "test.jsonl" |
| jsonl.write_text( |
| "\n".join( |
| json.dumps({"id": i, "scene_name": f"scene{i}", "question": "q"}) |
| for i in range(3) |
| ) |
| ) |
| rows = harness_run.load_questions(jsonl) |
| assert [r["id"] for r in rows] == [0, 1, 2] |
|
|
|
|
| def test_load_questions_filters_by_scene(tmp_path): |
| jsonl = tmp_path / "test.jsonl" |
| jsonl.write_text( |
| "\n".join( |
| json.dumps({"id": i, "scene_name": "a" if i < 2 else "b", "question": "q"}) |
| for i in range(4) |
| ) |
| ) |
| rows = harness_run.load_questions(jsonl, scene="b") |
| assert [r["id"] for r in rows] == [2, 3] |
|
|
|
|
| def test_load_questions_respects_limit(tmp_path): |
| jsonl = tmp_path / "test.jsonl" |
| jsonl.write_text( |
| "\n".join( |
| json.dumps({"id": i, "scene_name": "a", "question": "q"}) for i in range(5) |
| ) |
| ) |
| rows = harness_run.load_questions(jsonl, limit=2) |
| assert [r["id"] for r in rows] == [0, 1] |
|
|
|
|
| def test_scalar_score_returns_metric_name_and_value(): |
| doc = {"question_type": "object_counting", "ground_truth": "4"} |
| score_doc = harness_run.vsi_official_eval.vsibench_process_results(doc, ["4"])[ |
| "vsibench_score" |
| ] |
| metric_name, value = harness_run._scalar_score("object_counting", score_doc) |
| assert metric_name == "MRA:.5:.95:.05" |
| assert value == 1.0 |
|
|
|
|
| def test_scalar_score_rejects_unknown_question_type(): |
| with pytest.raises(ValueError): |
| harness_run._scalar_score("not_a_real_type", {}) |
|
|
|
|
| def test_results_dir_for_matches_established_dimension_nesting(): |
| root = harness_run.results_dir_for("qwen3.5-4b", "base", "selective", 32) |
| assert root == A.RESULTS_DIR / "qwen3.5-4b" / "selective" / "32" |
|
|
|
|
| def test_results_dir_for_keeps_protocols_together(): |
| base = harness_run.results_dir_for("qwen3.5-4b", "base", "selective", 32) |
| extended = harness_run.results_dir_for("qwen3.5-4b", "thinking", "selective", 32) |
| assert base == extended |
|
|
|
|
| def test_results_dir_for_honors_explicit_override(tmp_path): |
| assert ( |
| harness_run.results_dir_for("qwen3.5-4b", "base", "uniform", 16, tmp_path) |
| == 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_FRAME_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["output_token_ids"] == [19, 151645] |
| assert record["output_token_count"] == 2 |
| assert record["hit_token_limit"] is False |
| assert record["generation_config"] == _FAKE_ANSWER["generation_config"] |
| assert record["frame_timestamps_seconds"] == [0.0, 1.0, 2.0] |
| assert record["frame_indices"] == [0, 30, 60] |
| assert record["video_path"] == _FAKE_FRAME_INFO["video_path"] |
| assert record["device"] == "cuda" |
| assert record["dtype"] == "bfloat16" |
| assert record["library_versions"] == _FAKE_ANSWER["library_versions"] |
| assert record["vision_input_shapes"] == {"pixel_values": [512, 1536]} |
| assert record["generation_seconds"] == 1.234 |
| assert record["metric"] == "MRA:.5:.95:.05" |
| assert record["score"] == 1.0 |
| assert record["scene"] == "scene0001_00" |
| assert record["question_id"] == 7 |
|
|
|
|
| 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_FRAME_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_defaults_reasoning_fields_when_not_extended(): |
| 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_FRAME_INFO, |
| ) |
| assert record["reasoning_text"] is None |
| assert record["forced"] is False |
| assert record["forced_input_token_count"] is None |
|
|
|
|
| def test_build_record_carries_reasoning_fields_when_extended(): |
| extended_answer = { |
| **_FAKE_ANSWER, |
| "reasoning_text": "long reasoning about the scene", |
| "reasoning_raw": "long reasoning about the scene<|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_FRAME_INFO, |
| ) |
| assert record["reasoning_text"] == "long reasoning about the scene" |
| assert record["reasoning_raw"] == "long reasoning about the scene<|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", "video", None) |
| == A.RESULTS_DIR / "qwen3.5-4b" / "video" |
| ) |
|
|
|
|
| def test_video_record_has_no_frame_count_in_condition(): |
| info = dict(_FAKE_FRAME_INFO, frame_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"] == "base:video" |
| assert record["frame_count"] is None |
|
|