File size: 4,013 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
"""Tests for harness/A/models.py -- VLM adapter registry and prompt assembly.

Deliberately excludes any test that loads real model weights or calls .generate() --
those require the GPU and downloaded checkpoints and are exercised via harness.A.run
smoke runs instead, not the unit suite.
"""

import numpy as np
import pytest

from harness import A
from harness.A import models as vlm_models


def test_all_four_models_are_registered():
    assert vlm_models.available_models() == (
        "internvl3.5-2b",
        "internvl3.5-4b",
        "qwen3.5-2b",
        "qwen3.5-4b",
    )


def test_get_adapter_binds_the_correct_checkpoint_path():
    adapter = vlm_models.get_adapter("qwen3.5-4b")
    assert adapter.model_path == A.MODEL_PATHS["qwen3.5-4b"]
    assert isinstance(adapter, vlm_models.QwenVLAdapter)


def test_get_adapter_returns_the_right_class_per_model():
    assert isinstance(vlm_models.get_adapter("qwen3.5-2b"), vlm_models.QwenVLAdapter)
    assert isinstance(
        vlm_models.get_adapter("internvl3.5-4b"), vlm_models.InternVLAdapter
    )


def test_get_adapter_rejects_unknown_model():
    with pytest.raises(KeyError):
        vlm_models.get_adapter("not-a-real-model")


def test_internvl_adapter_disables_per_frame_tiling():
    # Otherwise InternVL's default per-image dynamic tiling (~3300 tokens/frame) blows
    # past this checkpoint's 40960-token context window at just 16 frames.
    assert vlm_models.InternVLAdapter.chat_template_kwargs == {"crop_to_patches": False}


def test_numbered_content_labels_every_frame_in_order():
    frames = ["frame0", "frame1", "frame2"]
    content = vlm_models._numbered_content(frames, "What is in the room?")
    assert content[0] == {"type": "text", "text": "Frame 1:"}
    assert content[1] == {"type": "image", "image": "frame0"}
    assert content[-1] == {"type": "text", "text": "What is in the room?"}
    image_items = [item for item in content if item["type"] == "image"]
    assert [item["image"] for item in image_items] == frames


def test_numbered_content_handles_zero_frames():
    content = vlm_models._numbered_content([], "question only")
    assert content == [{"type": "text", "text": "question only"}]


def test_adapter_answer_before_load_model_raises():
    adapter = vlm_models.get_adapter("qwen3.5-2b")
    with pytest.raises(RuntimeError):
        adapter.answer(["frame"], "question")


def test_adapter_answer_extended_before_load_model_raises():
    adapter = vlm_models.get_adapter("qwen3.5-2b")
    with pytest.raises(RuntimeError):
        adapter.answer_extended(["frame"], "question")


def test_every_adapter_implements_answer_extended():
    for model in vlm_models.available_models():
        adapter = vlm_models.get_adapter(model)
        assert callable(adapter.answer_extended)


def test_decode_new_tokens_preserves_clean_and_raw_text():
    adapter = vlm_models.get_adapter("qwen3.5-2b")

    class Processor:
        def decode(self, token_ids, skip_special_tokens):
            if skip_special_tokens:
                return "step one, step two, answer B"
            return "<think>step one, step two</think>B<eos>"

    adapter.processor = Processor()
    token_ids, hit_limit, text, raw = adapter._decode_new_tokens(
        np.array([[10, 11, 21, 22, 2]]), 2, 2048, [2]
    )
    assert token_ids == [21, 22, 2]
    assert hit_limit is False
    assert text == "step one, step two, answer B"
    assert raw == "<think>step one, step two</think>B<eos>"


def test_unload_clears_model_and_processor():
    adapter = vlm_models.get_adapter("qwen3.5-2b")
    adapter.model = object()
    adapter.processor = object()
    adapter.unload()
    assert adapter.model is None
    assert adapter.processor is None


def test_native_video_content_uses_one_video_item():
    content = vlm_models._numbered_content("/data/scene.mp4", "What is in the room?")
    assert content == [
        {"type": "video", "video": "/data/scene.mp4"},
        {"type": "text", "text": "What is in the room?"},
    ]