From 0eacbaba81fbd69286cbae19580911fd26beb570 Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Mon, 5 Oct 2026 10:42:37 +0200 Subject: [PATCH] vision: --min-tokens N passes mtmd's image_min_tokens (vision.min_tokens, hand-edited); CPU encode time in the docs (#767) --- docs/DETAILS.md | 11 +++++++++-- docs/INSTALL.md | 6 +++--- docs/TROUBLESHOOTING.md | 2 +- serve/server.py | 2 ++ serve/test_server.py | 29 +++++++++++++++++++++++++++++ tools/vision/strata_vision.cpp | 8 +++++--- 6 files changed, 49 insertions(+), 9 deletions(-) diff --git a/docs/DETAILS.md b/docs/DETAILS.md index df47bd2e..fea7515a 100644 --- a/docs/DETAILS.md +++ b/docs/DETAILS.md @@ -901,7 +901,7 @@ helper (`strata-vision`, from llama.cpp's `mtmd` library) and adds it to your st | Encoder on | Time per picture | Cost | | --- | --- | --- | | **GPU** (recommended) | **0.1-0.5 s** (up to 1,024 image tokens) | ~1.4 GB of VRAM is kept free for it, so the expert cache is smaller: text output is a few % slower (table below) | -| CPU | 10-30 s (pictures are scaled down to ~300 image tokens) | nothing on the GPU | +| CPU | about 3 s at 300 image tokens on 8 cores (6-13 s on 4 threads); more tokens take longer, in proportion (#767, #625) | nothing on the GPU | A picture becomes up to 1,024 tokens of the context (a 640x480 photo: 300). The same picture sent again, as chat apps do on every turn, is encoded only once. @@ -911,6 +911,13 @@ do on every turn, is encoded only once. `strata-.json`, which you can also edit by hand. More tokens keep more detail (small text, charts, screenshots) and take longer to encode, on the CPU most of all; a setup run again keeps the value. +**A minimum number of image tokens (0.1.40, #767):** `"min_tokens": N` in the `"vision"` section of +`strata-.json` (edit it by hand; a setup run again keeps it) is passed to the encoder as `--min-tokens N` +(mtmd's `image_min_tokens`): a small picture is scaled up to at least N tokens. llama.cpp's mtmd prints that Qwen-VL +models want at least 1,024 for grounding tasks (pointing, counting small items); the CPU default stays at 300 at most +and no minimum, because a larger minimum changes the image answers and costs encode time (about 3 s at 300 tokens on +8 cores, in proportion to the tokens). + **A Q8_0 encoder (#625):** `"mmproj"` in the `"vision"` section can point to another mmproj file of this model, for example a Q8_0 one (llama.cpp's `convert_hf_to_gguf.py --mmproj --outtype q8_0` makes one): the encoder's library reads quantized weights, the file is half the size, and on the CPU it can encode faster than BF16. Setup downloads @@ -1038,7 +1045,7 @@ the document, +0.4% on the chat. Details: `bench/results/2026-09-27-esp/`. | Slower than the tables | The monitor plugged into the GPU and other GPU programs take VRAM from the expert cache; RAM running below its rated speed (enable EXPO/XMP in the BIOS) slows the CPU half. | | `this server was started without the vision encoder` | The model was set up for text only: run setup again with `--vision gpu`. | | A picture is refused or `cannot read the image` | The file is not a picture Pillow can open (JPEG, PNG, WebP, GIF, BMP, TIFF, AVIF work). | -| Pictures are slow (10-30 s) | The encoder runs on the CPU: run setup again with `--vision gpu` (needs ~1.4 GB of VRAM). | +| Pictures are slow (3-30 s) | The encoder runs on the CPU: run setup again with `--vision gpu` (needs ~1.4 GB of VRAM). | | A request never finishes: "reading the prompt", GPU "100%" at low power | The GPU ran out of VRAM (engines before 0.1.9 could end with ~30 MiB free at large contexts). Run `START-HERE.bat` once to get engine 0.1.9 or newer; the log then says `... MiB of VRAM free with everything loaded` (a few hundred) and names the `--vram-reserve-mib` to add if it is low. | | Generation stops mid-answer, GPU "100%", one CPU core busy | Fixed in engine 0.1.12 (issue #29, a race in the CPU expert pool on big-VRAM cards). Since then a request that stops moving ends with an error instead of hanging (after 2 minutes; 1 minute from 0.1.13): the log says `no progress for ... s ... (issue #29)` with where it stopped, and the next request starts the engine again. If you see that line, please open an issue with it. Engine 0.1.13 adds a stall report under it (what every expert-pool thread and the GPU handshake were doing, memory and page faults) and, on Windows, a `strata-stall-.dmp` file with every thread's stack: attach both. (`STRATA_WATCHDOG_S` sets the time in seconds; 0 turns it off.) Engine 0.1.14 fixes the stall those reports found (issue #31: with the IQ packs the host could wait forever inside the NVIDIA driver while copying experts in a verify window; the experts are now copied by a GPU kernel, `--pcie-mode dma` restores the old way). | | `no progress for 60 s ... reading the prompt` on Linux, and the stall report says `threads waiting on the disk (state D): 16 ...` | The engine waits for the drive, not a deadlock: the n-gram table is read at random (`--ple-io direct`), which a rotational disk cannot keep up with (#605). The engine warns at start when the table is on one; `--ple-io ram` (Linux, needs RAM for the table) or the model on an SSD fixes it. Setup adds `--ple-io ram` itself on a rotational disk when the RAM holds the table (0.1.39). | diff --git a/docs/INSTALL.md b/docs/INSTALL.md index 4ab28fb1..05b1035d 100644 --- a/docs/INSTALL.md +++ b/docs/INSTALL.md @@ -69,8 +69,8 @@ On Linux setup uses a system ROCm 7 when there is one, or installs ROCm into `.v sudo), and compiles the engine on your PC for the card (10-20 minutes, once; it needs a C++ compiler and git: `sudo apt install build-essential git`). Several AMD cards share the model with `--gpus`, as on NVIDIA. -What differs from NVIDIA for now: pictures are read by the image encoder on the CPU (`--vision cpu`, 10-30 s per -picture), `--calibrate` is NVIDIA-only, and Unsloth's 4-bit model needs an NVIDIA card. Measurements per card, the +What differs from NVIDIA for now: pictures are read by the image encoder on the CPU (`--vision cpu`, about 3 s per +picture at the default 300 image tokens on 8 cores), `--calibrate` is NVIDIA-only, and Unsloth's 4-bit model needs an NVIDIA card. Measurements per card, the build by hand and the tuning tables: [AMD_HIP.md](AMD_HIP.md). ## Two or three cards @@ -197,7 +197,7 @@ says so and starts the engine you had. covering factor; `--rope-scaling`/`--rope-scale` override) ([details](DETAILS.md): "Context extension past 262K"). - **Images?** Whether it should also read pictures. The image encoder (0.9 GB) runs on the GPU (0.1-0.5 s per - picture, ~1.4 GB of VRAM kept free for it) or on the CPU (10-30 s per picture, nothing on the GPU); with AMD cards + picture, ~1.4 GB of VRAM kept free for it) or on the CPU (about 3-13 s per picture, nothing on the GPU); with AMD cards on the CPU for now. [Details](DETAILS.md#images-vision). - **Experimental speed projection?** Off unless you say yes - [read what it does](DETAILS.md#experimental-speed-projection-experimental-off-by-default) first. It changes how the model answers, and only the original model offers it. diff --git a/docs/TROUBLESHOOTING.md b/docs/TROUBLESHOOTING.md index 7287409e..bd9226c6 100644 --- a/docs/TROUBLESHOOTING.md +++ b/docs/TROUBLESHOOTING.md @@ -56,7 +56,7 @@ its rated speed (enable EXPO/XMP in the BIOS) slows the CPU half. **Pictures are refused, or slow.** "this server was started without the vision encoder": the model was set up for text only - run setup again with -`--vision gpu` (or `--vision cpu`). Pictures that take 10-30 s are read by the encoder on the CPU; `--vision gpu` +`--vision gpu` (or `--vision cpu`). Pictures that take several seconds (about 3 s at 300 image tokens on 8 cores, more with more tokens) are read by the encoder on the CPU; `--vision gpu` (NVIDIA, ~1.4 GB of VRAM) makes it 0.1-0.5 s. ## AMD cards diff --git a/serve/server.py b/serve/server.py index 1a1dd5ba..e755e00b 100644 --- a/serve/server.py +++ b/serve/server.py @@ -1376,6 +1376,8 @@ class Vision: args += ["--threads", str(cfg["threads"])] if cfg.get("max_tokens"): args += ["--max-tokens", str(cfg["max_tokens"])] + if cfg.get("min_tokens"): # #767: mtmd's image_min_tokens (a hand-edited key) + args += ["--min-tokens", str(cfg["min_tokens"])] self.dir = Path(tempfile.mkdtemp(prefix="strata-vision-")) self.spawn = (args, log, env) # to start it again after an unload self.stopped = False diff --git a/serve/test_server.py b/serve/test_server.py index 498ace48..de6fc3c3 100644 --- a/serve/test_server.py +++ b/serve/test_server.py @@ -3643,3 +3643,32 @@ class PromptProgressBatch(unittest.TestCase): self.assertEqual(prompt_progress(svc), {"total": 4096, "cache": 0, "processed": 512, "time_ms": 900}) engine.batch = 2 self.assertIsNone(prompt_progress(svc)) + + +class VisionArgs(unittest.TestCase): + """#767: vision.min_tokens reaches the encoder as --min-tokens (mtmd's image_min_tokens).""" + + def args_for(self, cfg): + import serve.server as server + seen = [] + + class Proc: + stdout = io.StringIO("READY 4096\n") + stdin = io.StringIO() + + def poll(self): + return None + + def popen(args, **kw): + seen.append(args) + return Proc() + + with mock.patch.object(server.subprocess, "Popen", popen), mock.patch.object(server, "contain"): + server.Vision({"exe": "strata-vision", "mmproj": "m.gguf", "model": "t.gguf", **cfg}) + return seen[0] + + def test_min_tokens_is_passed_only_when_set(self): + base = self.args_for({"max_tokens": 300}) + self.assertNotIn("--min-tokens", base) + self.assertEqual(self.args_for({"max_tokens": 1024, "min_tokens": 768})[-4:], + ["--max-tokens", "1024", "--min-tokens", "768"]) diff --git a/tools/vision/strata_vision.cpp b/tools/vision/strata_vision.cpp index 6be3f44b..99e51f35 100644 --- a/tools/vision/strata_vision.cpp +++ b/tools/vision/strata_vision.cpp @@ -5,7 +5,7 @@ // rows at the image's pad tokens and gives them their 2-D M-RoPE positions (see --serve GENI in generate.cpp). // // strata-vision --mmproj --model [--gpu] [--threads N] -// [--max-tokens N] [--flash-attn on|off|auto] +// [--max-tokens N] [--min-tokens N] [--flash-attn on|off|auto] // // Resident: prints "READY ", then per stdin line // ENC -> "OK " or "ERR " @@ -51,7 +51,7 @@ bool parse_enc(const std::string& line, std::string& img, std::string& out) { int main(int argc, char** argv) { std::string mmproj, model; bool gpu = false; - int threads = 0, max_tokens = 0; + int threads = 0, max_tokens = 0, min_tokens = 0; llama_flash_attn_type fa = LLAMA_FLASH_ATTN_TYPE_AUTO; for (int i = 1; i < argc; ++i) { const std::string a = argv[i]; @@ -64,6 +64,7 @@ int main(int argc, char** argv) { else if (a == "--gpu") gpu = true; else if (a == "--threads") threads = std::atoi(next().c_str()); else if (a == "--max-tokens") max_tokens = std::atoi(next().c_str()); + else if (a == "--min-tokens") min_tokens = std::atoi(next().c_str()); // mtmd image_min_tokens (#767) else if (a == "--flash-attn") { // FA keeps K and V in FP16; off = the attention in FP32 const std::string v = next(); fa = v == "on" ? LLAMA_FLASH_ATTN_TYPE_ENABLED : v == "off" ? LLAMA_FLASH_ATTN_TYPE_DISABLED @@ -73,7 +74,7 @@ int main(int argc, char** argv) { } if (mmproj.empty() || model.empty()) { std::fprintf(stderr, "usage: strata-vision --mmproj --model [--gpu] [--threads N] " - "[--max-tokens N] [--flash-attn on|off|auto]\n"); + "[--max-tokens N] [--min-tokens N] [--flash-attn on|off|auto]\n"); return 2; } // On the CPU the GPU stays unseen: a CUDA build otherwise opens a context there (measured: 0.4-0.7 GB of VRAM, @@ -103,6 +104,7 @@ int main(int argc, char** argv) { if (threads <= 0 && !gpu) threads = std::max(1u, std::thread::hardware_concurrency() / 2); if (threads > 0) cp.n_threads = threads; if (max_tokens > 0) cp.image_max_tokens = max_tokens; + if (min_tokens > 0) cp.image_min_tokens = min_tokens; mtmd_context* ctx = mtmd_init_from_file(mmproj.c_str(), text, cp); if (!ctx || !mtmd_support_vision(ctx)) { std::printf("ERR cannot load the vision encoder %s\n", mmproj.c_str());