server: only the image markers the template writes are images; the same text in a message stays text (#150)
This commit is contained in:
1 parent
ddfb98a6ea
commit
ed01b4de03
2 files changed
+39
-2
No files matched your search
+10
-2
@@ -49,6 +49,7 @@ from serve.mcp import McpCancelled, hub_from_config # noqa: E402
|
||||
|
||||
IM_END = "<|im_end|>"
|
||||
IMAGE_PAD = "<|image_pad|>"
|
||||
VISION_START = "<|vision_start|>"
|
||||
CTX_SLACK = 8 # `strata --serve` rejects prompt + max_new + 8 > context: keep the same margin here
|
||||
# The live tok/s is a rate over a window, not a mean since the first token: a mean reads ~1/elapsed at the first
|
||||
# token (the Monitor showed five-digit numbers) and then undershoots for the first second of every answer.
|
||||
@@ -720,17 +721,24 @@ class Service:
|
||||
raise ValueError("this server was started without the vision encoder (run setup again and choose "
|
||||
"'vision'), so it cannot read images")
|
||||
pad = self.tok.encode(IMAGE_PAD, parse_special=True)[0]
|
||||
start = self.tok.encode(VISION_START, parse_special=True)[0]
|
||||
# Encode only while the engine is idle: the engine and the image encoder (a separate process) must not
|
||||
# run on the GPU at the same time - an encode during a running request left that request stuck at
|
||||
# "reading the prompt" with CPU and GPU busy, for good (reproduced). So encoding takes its turn in the
|
||||
# same FIFO as the requests.
|
||||
with self.fifo:
|
||||
encoded = [self.vision.encode(src) for src in images]
|
||||
# one <|image_pad|> per image -> one per image token. Only the markers the template writes for an image
|
||||
# (right after <|vision_start|>) are images: the same text inside a message (an agent reading these docs,
|
||||
# #150) is kept as plain text, or it took an image's place and the counts no longer matched.
|
||||
literal = self.tok.encode(IMAGE_PAD, parse_special=False)
|
||||
out, k = [], 0
|
||||
for t in ids: # one <|image_pad|> per image -> one per image token
|
||||
if t == pad and k < len(encoded):
|
||||
for j, t in enumerate(ids):
|
||||
if t == pad and j > 0 and ids[j - 1] == start and k < len(encoded):
|
||||
out += [pad] * encoded[k][1]
|
||||
k += 1
|
||||
elif t == pad:
|
||||
out += literal
|
||||
else:
|
||||
out.append(t)
|
||||
if k != len(encoded):
|
||||
|
||||
@@ -167,6 +167,35 @@ class FitMaxTokens(unittest.TestCase):
|
||||
self.assertIn("no room to answer", b["error"]["message"])
|
||||
|
||||
|
||||
class ImageMarkers(unittest.TestCase):
|
||||
"""#150: the text "<|image_pad|>" inside a message is text, not an image's place."""
|
||||
|
||||
class FakeVision:
|
||||
def __init__(self, d):
|
||||
self.dir = Path(d)
|
||||
self.rows = self.dir / "img.sve"
|
||||
self.rows.write_bytes(b"rows")
|
||||
|
||||
def encode(self, source):
|
||||
return self.rows, 3
|
||||
|
||||
def test_literal_marker_with_an_image(self):
|
||||
import tempfile
|
||||
tok = ByteTokenizer()
|
||||
with tempfile.TemporaryDirectory() as d:
|
||||
svc = Service(MockEngine(tok, "ok", max_context=CTX), tok, ChatTemplate(ROOT / "serve/chat_template.jinja"),
|
||||
vision=self.FakeVision(d))
|
||||
pad = tok.encode("<|image_pad|>", parse_special=True)[0]
|
||||
for text in ("the docs say <|image_pad|> marks an image", "plain"):
|
||||
with self.subTest(text=text):
|
||||
msgs = [{"role": "user", "content": [{"type": "text", "text": text},
|
||||
{"type": "image", "source": "x.png"}]}]
|
||||
ids, _, _ = svc.prepare(msgs, None, {})
|
||||
self.assertEqual(ids.count(pad), 3) # the image's three rows, nothing else
|
||||
self.assertIn("<|image_pad|> marks" if "docs" in text else "plain", tok.decode(ids))
|
||||
svc.embeddings.path.unlink(missing_ok=True)
|
||||
|
||||
|
||||
class ClientShapes(unittest.TestCase):
|
||||
"""What real clients send: Claude Code posts /v1/messages?beta=true (issue #55) and puts hook context into the
|
||||
conversation as a mid-conversation system message (issue #56); some OpenAI clients send a late developer message."""
|
||||
|
||||
Reference in new issue
Block a user