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