Skip to content

Qwen3.8-Flash-Next on any consumer hardware: one-click install for Windows / Linux. Strata inference engine, OpenAI/Anthropic API on localhost, optional image input.

C++

dsv41: picture test (mkimg.py) and serve_prompts GENI / text output

Niko1221e38025769d58
diff --git a/tools/dsv41/mkimg.py b/tools/dsv41/mkimg.py
new file mode 100644
index 000000000..eba5740d3
--- /dev/null
+++ b/tools/dsv41/mkimg.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python3
+"""mkimg.py OUT_DIR: a 672x672 test picture (left half red, right half blue, a black disc in the middle), its DSI1 file and
+the prompt ids (chat mode, GGUF template) with the picture's span (16 x 17 + 2 = 274 positions of the image token) in them.
+Writes OUT_DIR/img.dsi and OUT_DIR/img_prompts.json in serve_prompts.py's format ("pic": file)."""
+import json, os, struct, subprocess, sys, tempfile
+import jinja2
+from jinja2.sandbox import ImmutableSandboxedEnvironment
+out, template, shard, tokenize = sys.argv[1:5]
+W = H = 672
+rows = bytearray()
+for y in range(H):
+ for x in range(W):
+ if (x - 336) ** 2 + (y - 336) ** 2 < 80 ** 2: rows += bytes((0, 0, 0))
+ elif x < 336: rows += bytes((220, 20, 20))
+ else: rows += bytes((20, 20, 220))
+n_h = n_w = 672 // 42
+span = n_h * (n_w + 1) + 2
+os.makedirs(out, exist_ok=True)
+env = ImmutableSandboxedEnvironment(trim_blocks=True, lstrip_blocks=True)
+env.filters["tojson"] = lambda x, **k: json.dumps(x, ensure_ascii=False)
+env.globals["raise_exception"] = lambda m: (_ for _ in ()).throw(jinja2.exceptions.TemplateError(m))
+tpl = env.from_string(open(template, encoding="utf-8").read())
+text = tpl.render(messages=[{"role": "user", "content": [{"type": "image_url", "image_url": {"url": "x"}},
+ {"type": "text", "text": "What colors are the left half and the right half of this picture? Answer in one short sentence."}]}],
+ tools=None, thinking_mode="chat", reasoning_effort=None, add_generation_prompt=True)
+PH = "<\uff5cdeepseek_image\uff5c>"
+assert text.count(PH) == 1, text[:300]
+a, b = text.split(PH)
+def tok(t):
+ with tempfile.NamedTemporaryFile("w", suffix=".txt", delete=False, encoding="utf-8") as f:
+ f.write(t); p = f.name
+ r = subprocess.run([tokenize, "-m", shard, "-f", p, "--ids", "--no-bos", "--no-escape"], capture_output=True, text=True)
+ os.unlink(p)
+ return json.loads([l for l in r.stdout.splitlines() if l.startswith("[")][-1])
+ia, ib = tok(a), tok(b)
+ids = ia + [129264] * span + ib
+open(os.path.join(out, "img.dsi"), "wb").write(b"DSI1" + struct.pack("<i", 1) + struct.pack("<qii", len(ia), W, H) + bytes(rows))
+json.dump([{"id": "image-colors", "mode": "chat", "ids": ids, "max_tokens": 40, "expect_ids": [],
+ "pic": os.path.join(out, "img.dsi")}], open(os.path.join(out, "img_prompts.json"), "w"))
+print("prompt", len(ids), "tokens, picture span at", len(ia), "length", span)
diff --git a/tools/dsv41/serve_prompts.py b/tools/dsv41/serve_prompts.py
index 8f0b6e93d..e9a363d97 100644
--- a/tools/dsv41/serve_prompts.py
+++ b/tools/dsv41/serve_prompts.py
@@ -4,7 +4,25 @@ Starts the engine in --serve mode (appended), waits for READY, sends GEN for eve
generated ids with the llama.cpp reference's greedy ids (expect_ids), and prints one line each:
id mode prompt_tokens n_gen match_prefix/n_expect first_divergence prefill ms decode tok/s
"""
-import json, subprocess, sys, time
+import json, os, subprocess, sys, time
+
+def _bytes_map():
+ bs = list(range(33, 127)) + list(range(161, 173)) + list(range(174, 256))
+ cs = bs[:]; n = 0
+ for b in range(256):
+ if b not in bs: bs.append(b); cs.append(256 + n); n += 1
+ return {chr(c): b for b, c in zip(bs, cs)}
+_BM = _bytes_map()
+try:
+ _VOC = {i: t for t, i in json.load(open(os.environ.get("DS_VOCAB", "/data/dsv41-port/pack/tokenizer/vocab.json"))).items()}
+except Exception:
+ _VOC = {}
+def detok(ids):
+ b = bytearray()
+ for i in ids:
+ for ch in _VOC.get(i, "?"):
+ b.append(_BM.get(ch, 63))
+ return b.decode("utf-8", "replace")
prompts_path, out_path = sys.argv[1], sys.argv[2]
cmd = sys.argv[sys.argv.index("--") + 1:] + ["--serve"]
@@ -27,7 +45,10 @@ res = []
for pr in prompts:
ids = pr["ids"]
mx = min(pr["max_tokens"], max(len(pr["expect_ids"]) + 4, 8))
- p.stdin.write("GEN %d %s\n" % (mx, ",".join(map(str, ids))))
+ if pr.get("pic"):
+ p.stdin.write("GENI %d %s %s\n" % (pr.get("max_tokens", 40), pr["pic"], ",".join(map(str, ids))))
+ else:
+ p.stdin.write("GEN %d %s\n" % (mx, ",".join(map(str, ids))))
p.stdin.flush()
gen, done = [], None
while True:
@@ -40,6 +61,8 @@ for pr in prompts:
done = line.split()
break
exp = pr["expect_ids"]
+ if pr.get("pic") or os.environ.get("SHOW_TEXT"):
+ print("TEXT", repr(detok(gen)), flush=True)
n = 0
while n < min(len(gen), len(exp)) and gen[n] == exp[n]:
n += 1