← Ramblings

Batch photo tagging with Qwen3-VL & Llama-swap

TL;DR: My tool ai_img_json_exif was tagging photos with Qwen3-VL 8B one at a time at about 3.5 s each, with the GPU mostly idle between requests. The fix had two halves: a llama-server entry with --parallel 8 --kv-unified, so eight requests share one 32k context at no extra VRAM, and a batch runner rewritten from a sequential loop into a worker pool. It now takes 1.3 to 1.4 s per photo. That is 2.6 times faster, not 8, and I think the image side is why.

One photo at a time

ai_img_json_exif sends each photo to Qwen3-VL 8B Instruct (Q4_K_M) with a short prompt and gets a small JSON answer back. One at a time that took about 3.5 s per photo on a 12 GB RTX 4070 Ti, with the GPU mostly waiting on me.

Decode is limited by memory bandwidth: every step reads all the weights, and that costs about the same for one sequence as for eight. A short JSON answer per photo is exactly the workload where stacking requests into the same pass is nearly free.

Server: slots that share one pool

llama-server can run several requests in one batch with --parallel N, with a catch: by default the context is split between slots. Eight slots on a 32k context is 4k each. A tagging request is 2 to 3k tokens (a 1536 px image is about 1.4k of that), so it fits, but only just. --kv-unified makes the slots share one pool instead, so VRAM stays exactly the same as the single-slot setup.

I ended up with two entries in llama-swap pointing at the same files:

qwen3-vl-8b-8batch:
  name: "Qwen3-VL 8B Instruct (Q4_K_M, batch: 8 parallel)"
  ttl: 300
  cmd: |
    llama-server
    --host 127.0.0.1 --port ${PORT}
    --model "${models_dir}/Qwen/Qwen3-VL-8B-Instruct-GGUF/Qwen3VL-8B-Instruct-Q4_K_M.gguf"
    --mmproj "${models_dir}/Qwen/Qwen3-VL-8B-Instruct-GGUF/mmproj-Qwen3VL-8B-Instruct-Q8_0.gguf"
    --ctx-size 32768
    --cache-type-k q8_0 --cache-type-v q8_0
    --flash-attn on
    --parallel 8
    --kv-unified
    --n-gpu-layers 99
    --jinja

The plain qwen3-vl-8b entry went back to --parallel 1 so a single interactive request gets the whole 32k. My first attempt had been to give the one entry four slots, which sped up batches and shrank everything else. For the record, the batch entry was named -batch, then -octo-parallel, then -8batch, across three commits in two minutes.

Loading from a local SSD

One fix came before any of the slot work. llama-swap unloads the model after five idle minutes so the card is free for other jobs, which means model loads happen all the time, and mine were reading the GGUF files from the NAS over NFS.

I added a one-shot pre-flight service to the stack. It rsyncs the models on a short list from the NAS to a local SSD and exits, and llama-swap only starts after it succeeds. The cache is mounted at the old path, so no model paths changed. Unchanged files are skipped, so a normal start takes seconds. llama-swap no longer mounts the NFS share at all, which also means a NAS outage cannot stall a model load.

Client: a worker pool

Server slots do nothing if the client sends one request and waits. The batch runner in app/static/batch.js was a for loop with await in it, so it became a pool of workers pulling from a shared queue. Trimmed down:

const parallel = Math.min(batchParallel(), todo.length);
const queue = todo.slice();
const pause = new AbortController();

const worker = async () => {
  while (queue.length && !stopping()) {
    const item = queue.shift();
    let ok = await runItem(item, signal);
    for (let attempt = 0; ok === "transient" && attempt < RETRY_DELAYS.length && !stopping(); attempt++) {
      const wait = retryDelay(RETRY_DELAYS[attempt], item.lastError);
      await abortableSleep(wait, waitSignal);
      ok = await runItem(item, signal);
    }
    if (ok === "transient") {
      pausedOn ??= item;
      pause.abort();
      return;
    }
    finished.add(item);
  }
};

await Promise.all(Array.from({ length: parallel }, worker));

The parts that took more thought than the pool itself:

Results

Parallel Per photo
1 about 3.5 s
4 1.85 s
8 1.3 to 1.4 s

That is 2.6 times faster, not 8 times. I have not profiled where the rest goes. My guess is the image side: every request still pays for its own image encode and a 1.4k-token prefill, and that work does not get cheaper by sharing a pass the way decode does. Going from 4 to 8 slots bought much less than going from 1 to 4, which fits.

What I would do again

  1. Fix the client first. Eight server slots behind a sequential loop is one slot.
  2. Use a unified KV pool for small, uneven requests. Splitting the context evenly wastes it.
  3. Keep a separate entry for batch work. Interactive use and batch use want different slot counts from the same weights.
  4. Remember to consider all steps in a workflow. Every swap is a load. Keep model files on local disk if the server swaps models.
  5. Key cached results on the model, not the endpoint name. Otherwise a faster route to the same model redoes finished work.

References