Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree (570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base = the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main). Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details, conflict table and left-out files are in docs/upstream.md. Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61), docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out. scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run"). Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR macros on. No GPU run, no quality gate. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
124 lines
5.5 KiB
Python
124 lines
5.5 KiB
Python
"""The elastic image encoder (the vision section's "elastic": true with "vram_elastic"): the first picture makes the
|
|
expert cache keep the encoder's VRAM free and then starts it, later pictures use it as it is, an idle encoder stops and
|
|
the cache takes its VRAM back, a POST /v1/vram reserve stacks on the encoder's share, and the engine's load never starts
|
|
it on its own."""
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from serve.frontend import ChatTemplate
|
|
from serve.server import ByteTokenizer, Service, Vision
|
|
from serve.test_vram import VramEngine
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
IMAGE = {"type": "image", "source": "x.png"}
|
|
|
|
|
|
class ElasticVision:
|
|
"""An encoder process stand-in: starts, stops, and turns every picture into three image tokens."""
|
|
def __init__(self, d, elastic=True):
|
|
self.elastic, self.elastic_mib, self.elastic_idle_s = elastic, 2048, 60.0
|
|
self.up, self.starts, self.unloads = False, 0, 0
|
|
self.dir = Path(d)
|
|
self.rows = self.dir / "img.sve"
|
|
self.rows.write_bytes(b"rows")
|
|
|
|
def alive(self):
|
|
return self.up
|
|
|
|
def restart(self):
|
|
self.up, self.starts = True, self.starts + 1
|
|
|
|
def unload(self):
|
|
self.up, self.unloads = False, self.unloads + 1
|
|
|
|
def encode(self, source):
|
|
assert self.up, "a picture reached an encoder that was not started"
|
|
return self.rows, 3
|
|
|
|
|
|
class Elastic(unittest.TestCase):
|
|
def setUp(self):
|
|
d = tempfile.mkdtemp()
|
|
self.tok = ByteTokenizer()
|
|
self.engine = VramEngine(self.tok)
|
|
self.engine.loaded = True
|
|
self.vision = ElasticVision(d)
|
|
self.svc = Service(self.engine, self.tok, ChatTemplate(ROOT / "serve/chat_template.jinja"), vision=self.vision)
|
|
self.addCleanup(self.drop_embeddings)
|
|
|
|
def drop_embeddings(self):
|
|
path = getattr(getattr(self.svc, "embeddings", None), "path", None)
|
|
if path is not None:
|
|
path.unlink(missing_ok=True)
|
|
|
|
def picture(self):
|
|
self.svc.prepare([{"role": "user", "content": [{"type": "text", "text": "what is it"}, IMAGE]}], None, {})
|
|
|
|
def test_the_first_picture_makes_room_then_starts_the_encoder(self):
|
|
self.picture()
|
|
self.assertEqual(self.engine.calls, [2048]) # the cache keeps the encoder's share free first
|
|
self.assertEqual(self.vision.starts, 1)
|
|
self.assertTrue(self.svc.vision_held)
|
|
self.picture() # the next one uses the running encoder
|
|
self.assertEqual(self.engine.calls, [2048])
|
|
self.assertEqual(self.vision.starts, 1)
|
|
|
|
def test_an_idle_encoder_stops_and_the_cache_grows_back_whole(self):
|
|
self.picture()
|
|
self.assertEqual(self.svc._vision_idle_down(), "busy") # a picture came just now
|
|
self.svc.last_image_at = time.time() - 61
|
|
with self.svc.fifo: # a request holds the turn
|
|
self.assertEqual(self.svc._vision_idle_down(), "busy")
|
|
self.assertEqual(self.svc._vision_idle_down(), "down")
|
|
self.assertFalse(self.vision.up)
|
|
self.assertFalse(self.svc.vision_held)
|
|
self.assertEqual(self.engine.calls, [2048, 0]) # 0, not None: the full size, not the start's reserve
|
|
self.assertEqual(self.svc._vision_idle_down(), "nothing")
|
|
self.picture() # and the next picture starts it again
|
|
self.assertEqual(self.vision.starts, 2)
|
|
self.assertEqual(self.engine.calls, [2048, 0, 2048])
|
|
|
|
def test_a_vram_reserve_stacks_on_the_encoders_share(self):
|
|
self.svc.vram(4000)
|
|
self.picture()
|
|
self.assertEqual(self.engine.calls, [4000, 6048])
|
|
self.svc.vram(1000) # asked while the encoder runs
|
|
self.assertEqual(self.engine.calls[-1], 3048)
|
|
self.svc.last_image_at = 0
|
|
self.svc._vision_idle_down()
|
|
self.assertEqual(self.engine.calls[-1], 1000) # back to the asked reserve, not to 0
|
|
|
|
def test_an_engine_that_cannot_shrink_still_gets_its_encoder(self):
|
|
self.engine.elastic = False
|
|
self.picture()
|
|
self.assertEqual(self.vision.starts, 1)
|
|
self.assertFalse(self.svc.vision_held)
|
|
self.svc.last_image_at = 0
|
|
self.assertEqual(self.svc._vision_idle_down(), "down")
|
|
self.assertEqual(self.engine.calls, [2048]) # the refused shrink only: nothing to give back
|
|
|
|
def test_the_load_never_starts_an_elastic_encoder(self):
|
|
self.assertFalse(self.svc._vision_down())
|
|
self.svc.vision = ElasticVision(tempfile.mkdtemp(), elastic=False)
|
|
self.assertTrue(self.svc._vision_down()) # a non-elastic encoder that is down is started again
|
|
|
|
def test_an_unload_lets_go_of_the_share(self):
|
|
self.picture()
|
|
self.assertEqual(self.svc.unload(), "unloaded")
|
|
self.assertFalse(self.svc.vision_held)
|
|
self.assertFalse(self.vision.up)
|
|
|
|
def test_the_config_keys(self):
|
|
v = Vision({"exe": "e", "mmproj": "m", "model": "t", "elastic": True, "elastic_mib": 3000,
|
|
"elastic_idle_s": 30}, lazy=False) # elastic: nothing starts with the server
|
|
self.assertTrue(v.elastic and v.elastic_mib == 3000 and v.elastic_idle_s == 30 and not v.alive())
|
|
v = Vision({"exe": "e", "mmproj": "m", "model": "t"}, lazy=True)
|
|
self.assertFalse(v.elastic)
|
|
self.assertEqual((v.elastic_mib, v.elastic_idle_s), (2048, 120.0))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|