okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-split-pics into f-split-int22e1d62961

main
4.6 KiB107 linesraw
1"""Importing a model together with its picture reader (mmproj), and the plain words when a model type is not ours."""
2
3import io
4import json
5import struct
6import tempfile
7import unittest
8from contextlib import redirect_stdout
9from pathlib import Path
10
11from unittest import mock
12
13from strata_cli.cli import main
14from strata_control import models
15from tests.terminal.test_models import gguf_fixture
16
17
18def projector_fixture(projector_type="qwen3vl_merger", architecture="clip") -> bytes:
19    def string(value):
20        raw = value.encode()
21        return struct.pack("<Q", len(raw)) + raw
22
23    pairs = [("general.architecture", 8, string(architecture)), ("clip.projector_type", 8, string(projector_type))]
24    header = (b"GGUF" + struct.pack("<IQQ", 3, 1, len(pairs)) +
25              b"".join(string(key) + struct.pack("<I", kind) + value for key, kind, value in pairs))
26    header += string("v.blk.0.attn_q.weight") + struct.pack("<IQIQ", 1, 64, 1, 0)
27    return header + b"\x00" * (-len(header) % 32) + b"\x00" * 128
28
29
30class ImportProjectorTests(unittest.TestCase):
31    def setUp(self):
32        self.temp = tempfile.TemporaryDirectory()
33        self.addCleanup(self.temp.cleanup)
34        self.source = Path(self.temp.name) / "model"
35        self.source.mkdir()
36
37    def plan(self, path):
38        output = io.StringIO()
39        with redirect_stdout(output):
40            main(["models", "import", str(path), "--plan", "--output", "json"])
41        return json.loads(output.getvalue())["plan"]
42
43    def model(self, **kwargs):
44        first = self.source / "Qwen3.8-Flash-Next-GSQ-RCO-Q2_0-00001-of-00001.gguf"
45        first.write_bytes(gguf_fixture(**kwargs))
46        return first
47
48    def test_pinned_projector_next_to_a_model_means_pictures(self):
49        self.model()
50        file = self.source / "mmproj-Qwen3.8-Flash-Next-BF16.gguf"
51        file.write_bytes(projector_fixture())
52        with mock.patch.dict(models.PINNED_PROJECTORS, {file.name.lower(): file.stat().st_size}):
53            plan = self.plan(self.source)
54        self.assertTrue(plan["reads_pictures"])
55        self.assertEqual(plan["projectors"][0]["status"], "ok")
56        self.assertIn("pictures will work", plan["messages"][0])
57
58    def test_same_name_but_another_size_is_not_called_official(self):
59        self.model()
60        (self.source / "mmproj-Qwen3.8-Flash-Next-BF16.gguf").write_bytes(projector_fixture())
61        plan = self.plan(self.source)
62        self.assertEqual(plan["projectors"][0]["status"], "other_name")
63
64    def test_projector_beside_a_chosen_model_file_is_found(self):
65        first = self.model()
66        (self.source / "mmproj-F16.gguf").write_bytes(projector_fixture())
67        plan = self.plan(first)
68        self.assertEqual([p["name"] for p in plan["projectors"]], ["mmproj-F16.gguf"])
69        self.assertTrue(plan["reads_pictures"])
70
71    def test_projector_of_another_kind_is_not_used(self):
72        self.model()
73        (self.source / "mmproj-other.gguf").write_bytes(projector_fixture("mlp"))
74        plan = self.plan(self.source)
75        self.assertFalse(plan["reads_pictures"])
76        self.assertEqual(plan["projectors"][0]["status"], "unsupported")
77        self.assertIn("Custom model API", plan["projectors"][0]["plain"])
78
79    def test_other_name_projector_of_our_kind_is_explained(self):
80        self.model()
81        (self.source / "mmproj-mine.gguf").write_bytes(projector_fixture())
82        plan = self.plan(self.source)
83        self.assertEqual(plan["projectors"][0]["status"], "other_name")
84        self.assertTrue(plan["reads_pictures"])
85
86    def test_unsupported_model_type_says_custom_api(self):
87        first = self.source / "SmolVLM-256M-Q8_0.gguf"
88        first.write_bytes(gguf_fixture(architecture="llama"))
89        (self.source / "mmproj-SmolVLM-256M-Instruct-Q8_0.gguf").write_bytes(projector_fixture("idefics3"))
90        plan = self.plan(self.source)
91        self.assertEqual(plan["file_set_status"], "invalid_or_incomplete")
92        self.assertFalse(plan["reads_pictures"])
93        self.assertEqual(plan["messages"], ["This model type isn't supported by the Strata engine. "
94                                            "Add it as a Custom model API (llama-server) instead."])
95
96    def test_choosing_the_projector_itself_says_so(self):
97        projector = self.source / "mmproj-F16.gguf"
98        projector.write_bytes(projector_fixture())
99        output = io.StringIO()
100        with redirect_stdout(output):
101            code = main(["models", "import", str(projector), "--plan", "--output", "json"])
102        self.assertEqual(code, 5)
103        self.assertIn("picture reader", json.dumps(json.loads(output.getvalue())))
104
105
106if __name__ == "__main__":
107    unittest.main()