okami.mom/strata

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

waveringanaserve: DeepSeek-V4.1 pictures through the engine's own vision tower (a pack with vision.*: pack_gguf --mmproj) - sized as DeepSeek's image_processor.py, spans of image tokens, DSI1 file in GENI; images on without a vision section, --vision off turns them offfe0b0b39a

main
4.2 KiB96 linesraw
1"""DeepSeek-V4.1's in-engine vision tower, the server's half (serve/ds4_vision.py): pictures sized as DeepSeek's
2inference/image_processor.py sizes them, each placeholder widened to its span, and the DSI1 file strata-dsv41 reads."""
3import io
4import struct
5import tempfile
6import unittest
7from pathlib import Path
8
9from serve import ds4_vision
10from serve.server import ds4_engine_vision
11
12try:
13    from PIL import Image
14except ImportError:
15    Image = None
16
17GEO = {"vision.num_hidden_layers": "32", "vision.patch_size": "14", "vision.downsample_ratio": "3",
18       "vision.max_image_tokens": "1024", "vision.min_pixels": "295936", "image_token_id": "129264"}
19TOK = 129264
20
21
22def vision(d):
23    return ds4_vision.Ds4Vision(GEO, Path(d), lambda src: Path(src).read_bytes())
24
25
26def png(size, mode="RGB", color=(200, 30, 30)):
27    out = io.BytesIO()
28    Image.new(mode, size, color).save(out, format="PNG")
29    return out.getvalue()
30
31
32class Plan(unittest.TestCase):
33    def test_deepseeks_examples(self):
34        v = vision(tempfile.mkdtemp())
35        self.assertEqual(v.plan(1024, 701), (17, 25, 714, 1036))     # carrots.jpeg: 444 tokens
36        self.assertEqual(v.plan(450, 308), (11, 16, 462, 658))       # corn.jpeg, under min_pixels: scaled up, 189
37        self.assertEqual(v.span(17, 25), 444)
38
39    def test_a_large_picture_is_cut_to_the_token_limit(self):
40        v = vision(tempfile.mkdtemp())
41        n_h, n_w, h, w = v.plan(4000, 3000)
42        self.assertEqual((n_h, n_w, h, w), (27, 36, 1134, 1512))
43        for size in ((4000, 3000), (8000, 60), (60, 8000), (20000, 20000)):
44            n_h, n_w, h, w = v.plan(*size)
45            self.assertLessEqual(v.span(n_h, n_w), 1024, size)
46            self.assertEqual((h % 14, w % 14), (0, 0), size)
47
48
49@unittest.skipIf(Image is None, "Pillow is not installed")
50class Files(unittest.TestCase):
51    def test_spans_and_the_dsi1_file(self):
52        d = tempfile.mkdtemp()
53        v = vision(d)
54        a, b = v.encode(png((1024, 701))), v.encode(png((450, 308), "RGBA", (0, 0, 0, 0)))
55        self.assertEqual((a[1], b[1]), (444, 189))
56        self.assertEqual(v.encode(png((1024, 701))), a)                       # the same picture: sized once
57        self.assertEqual(len(list(Path(d).glob("*.dsp"))), 2)
58        ids, starts = v.expand([1, TOK, 2, 3, TOK, 4], [a, b])
59        self.assertEqual(starts, [1, 447])
60        self.assertEqual(ids[:1] + ids[445:447] + ids[-1:], [1, 2, 3, 4])
61        self.assertEqual(ids.count(TOK), 444 + 189)
62        with self.assertRaises(ValueError):
63            v.expand([1, TOK, 2], [a, b])
64        f = Path(d) / "req.dsi"
65        v.write(f, [a, b], starts)
66        raw = f.read_bytes()
67        self.assertEqual(raw[:8], b"DSI1" + struct.pack("<i", 2))
68        start, w, h = struct.unpack("<qii", raw[8:24])
69        self.assertEqual((start, w, h), (1, 1036, 714))
70        rgb = raw[24:24 + w * h * 3]
71        self.assertEqual(rgb[:3], bytes((127, 127, 127)))                    # padded grey above the picture
72        self.assertEqual(rgb[(h // 2 * w + w // 2) * 3:][:3], bytes((200, 30, 30)))
73        start, w, h = struct.unpack("<qii", raw[24 + 1036 * 714 * 3:][:16])
74        self.assertEqual((start, w, h), (447, 658, 462))
75        mid = 24 + 1036 * 714 * 3 + 16 + (h // 2 * w + w // 2) * 3
76        self.assertEqual(raw[mid:mid + 3], bytes((255, 255, 255)))           # transparent: white
77        v.shutdown()
78        self.assertFalse(Path(d).exists())
79
80
81class Config(unittest.TestCase):
82    def test_on_with_a_vision_pack_off_without_or_when_asked(self):
83        d = tempfile.mkdtemp()
84        (Path(d) / "ds4.txt").write_text("hidden_size 5120\n" + "".join(f"{k} {v}\n" for k, v in GEO.items()))
85        cfg = {"arch": "deepseek41", "args": ["--pack", d]}
86        engine = type("E", (), {"info": {}})()
87        self.assertIsInstance(ds4_engine_vision(cfg, engine), ds4_vision.Ds4Vision)
88        self.assertIsNone(ds4_engine_vision({**cfg, "args": ["--pack", d, "--vision", "off"]}, engine))
89        engine.info = {"vision": "off"}
90        self.assertIsNone(ds4_engine_vision(cfg, engine))
91        (Path(d) / "ds4.txt").write_text("hidden_size 5120\n")
92        self.assertIsNone(ds4_engine_vision(cfg, type("E", (), {"info": {}})()))
93
94
95if __name__ == "__main__":
96    unittest.main()