Files
xpu-llm-ports/lfm3/tests/vl/image_ttft.py
T
Pascal LinxweilerandClaude Opus 5.5 1834792bb8 lfm3: add INT4, FP8 per-channel and VL notes, tests, eval and results
Sync the repo with the working directory: full NOTES.md (rounds 4-5, LFM3-VL),
VL tests and test images, quality eval, AutoRound W4A16 quantization scripts
and calibration recipe, the variant.sh MODEL/DOCKER_ENV options, and the
remaining benchmark results. Large regenerable artifacts (INT4 checkpoint,
ref_vl.pt, raw logprob dumps) stay out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 13:59:43 +01:00

76 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""TTFT of a chat request with one ~1024px image vs a text-only request of the same prompt length,
streaming, max_tokens 1, sequential. Each image request carries a freshly perturbed image (one pixel
changed, base64 PNG data URL as Open WebUI sends it) and each text request a fresh random prefix, so
neither vLLM's multimodal cache nor its prefix cache can hit. TTFT(image) - TTFT(text) = image cost
(decode base64 + preprocessing + vision encoder + projector).
python3 tests/vl/image_ttft.py PORT OUT.json [REPS=10]
Needs Pillow: run with a python that has it (e.g. the vLLM image).
"""
import base64, io, json, random, statistics, sys, time, urllib.request
from PIL import Image
port, out = int(sys.argv[1]), sys.argv[2]
reps = int(sys.argv[3]) if len(sys.argv) > 3 else 10
URL = f"http://127.0.0.1:{port}/v1/chat/completions"
rng = random.Random(0)
src = Image.open("tests/vl/images/photo.jpeg").convert("RGB")
SIZES = {"1024x768": (1024, 768), "1024x1024": (1024, 1024)}
def ttft(content):
body = {"model": "lfm3-vl-2b", "messages": [{"role": "user", "content": content}], "max_tokens": 1,
"stream": True, "stream_options": {"include_usage": True}, "temperature": 0.0}
req = urllib.request.Request(URL, json.dumps(body).encode(), {"Content-Type": "application/json"})
t0 = time.perf_counter(); first = None; usage = None
with urllib.request.urlopen(req, timeout=300) as r:
for line in r:
line = line.decode().strip()
if not line.startswith("data:") or line == "data: [DONE]":
continue
d = json.loads(line[5:])
if first is None and d.get("choices"):
first = time.perf_counter() - t0
usage = d.get("usage") or usage
return first * 1000, usage["prompt_tokens"]
def image_content(size):
im = src.resize(size, Image.BICUBIC)
im.putpixel((rng.randrange(size[0]), rng.randrange(size[1])), tuple(rng.randrange(256) for _ in range(3)))
buf = io.BytesIO(); im.save(buf, "PNG")
url = "data:image/png;base64," + base64.b64encode(buf.getvalue()).decode()
return [{"type": "image_url", "image_url": {"url": url}}, {"type": "text", "text": "Describe this image."}], len(url)
WORDS = "alpha bravo charlie delta echo foxtrot golf hotel india juliet kilo lima mike november oscar papa".split()
def text_content(n_tokens):
# ~1 token per word for these words; fresh random words → no prefix-cache hit
return " ".join(rng.choice(WORDS) for _ in range(n_tokens)) + "\n\nDescribe this text."
res = {}
ttft(image_content((640, 480))[0]); ttft("warmup") # warm both paths
for name, size in SIZES.items():
img_ms, txt_ms = [], []
for _ in range(reps):
content, b64_len = image_content(size)
ms, img_prompt = ttft(content); img_ms.append(ms)
# text prompt of about the same length (calibrate once)
n = img_prompt
for _ in range(3):
_, got = ttft(text_content(n)); n += img_prompt - got
for _ in range(reps):
ms, txt_prompt = ttft(text_content(n)); txt_ms.append(ms)
med_i, med_t = statistics.median(img_ms), statistics.median(txt_ms)
res[name] = {"image_prompt_tokens": img_prompt, "text_prompt_tokens": txt_prompt, "base64_chars": b64_len,
"ttft_image_ms_median": round(med_i, 1), "ttft_image_ms_all": [round(x, 1) for x in img_ms],
"ttft_text_ms_median": round(med_t, 1), "ttft_text_ms_all": [round(x, 1) for x in txt_ms],
"image_cost_ms": round(med_i - med_t, 1)}
print(name, {k: v for k, v in res[name].items() if not k.endswith("_all")}, flush=True)
json.dump(res, open(out, "w"), indent=1)