Qiming Bao · Blog · 24 November 2026 · English / 中文

Tag or generate? Fine-tuning both on the same documents

In an earlier post a fine-tuned Qwen3.5 token tagger beat zero-shot JSON generation by a wide margin. That comparison mixed two things: tagging versus generating, and trained versus untrained. Here both are fine-tuned on the same 801 documents and tested on the same 199. The tagger is no longer the most accurate, but it is still about ten times faster, and sending only its least confident documents to the generator keeps most of both.

Data: the public nanonets/nn-auto-bench-ds (invoices, receipts, bank statements, passports; MIT), 1000 documents, split 801 train / 199 test by document type with a fixed seed. Scoring is field-level F1 on the queried fields. For the tagger, each gold value was located in the dataset's OCR words by normalised string matching (95.5% of values were found; the rest stay untagged), giving word-level BIO tags over 54 field names. All runs are on one A100 shared with other jobs; accuracy differences are paired bootstraps over documents.

The two models

Results

methodp50p95docs/s, batchedF1 (199 test docs)
generate, 9B zero-shot668 ms818 ms6.00.680
generate, 9B fine-tuned663 ms748 ms6.50.882
tag, 2B fine-tuned72 ms88 ms16.90.857
tag, 0.8B fine-tuned68 ms95 ms18.30.841

Offline vLLM runs; latency is the p50 and p95 of single requests, throughput is all documents at once with 16 in flight for generation. The taggers use the 1-megapixel image cap described below.

Fine-tuning the generator added 20 points of F1 and did not slow it down. The MTP drafter is not retrained with the target, but acceptance only fell from 0.86 to 0.84, and the fine-tuned model writes shorter answers (157 tokens instead of 193). The fine-tuned 2B tagger is 2.6 points behind it (95% CI −4.5 to −0.8), at about a ninth of the single-request latency. The 2B tagger is 1.5 points better than the 0.8B one (CI +0.2 to +3.0) and costs a few milliseconds more.

A 19.5-megapixel page

Batched, the 2B tagger first managed only 3 to 4.5 documents per second, slower than sending the documents one at a time. Image preprocessing was not the cause (32 ms per image on one CPU thread, and parallel renderer workers did not help), and neither was vLLM's multiprocess engine. The engine was mostly waiting on the GPU. The first 80 test documents ran at 38 documents per second; the rest averaged 150 ms each. A few images in the dataset are very large: one is 19.5 megapixels (64,896 vision patches, 16,224 image tokens), and one document type averages 5.3 megapixels, for pages that hold about 150 words. Vision attention cost grows with the square of the patch count, so these pages dominated the batch.

Capping images at 1 megapixel at inference time (mm_processor_kwargs={"max_pixels": 1048576}), on the same full-resolution-trained model:

taggerdocs/s before → afterΔF1 [95% CI]
0.8B8–10 → 18.3−0.001 [−0.006, +0.003]
2B3–4.5 → 16.9−0.001 [−0.006, +0.004]

The generative setup already resized pages to at most 1280 pixels on the long side, which is why it never showed this. Raising max_num_batched_tokens from 8192 to 32768 also helped the uncapped 2B (+40%), but the cap is what fixed it.

Route only the doubtful documents

The tagger's probabilities say when it is unsure. I took a document's confidence as the lowest top-label probability over the words of its predicted fields and over its untagged words. Documents below a threshold are also sent to the generator, whose answer is used for them. I chose the threshold by 2-fold cross-validation over the test documents (20 random splits; 0.7 was picked most often), so the accuracy estimate is not tuned on the documents it is scored on. That routes about 35% of documents.

Then I ran it as a service: the fine-tuned 9B behind vllm serve on one GPU, and the 2B tagger with the router on another, with prefix and multimodal caches off on both so that no request could be served from a cache:

methodone at a time: p50p95meanbatched docs/sF1
generate only (9B fine-tuned)590 ms735 ms607 ms6.60.882
tag only (2B, 1 MP)72 ms90 ms78 ms21.10.857
tag, then generate 35%72 ms767 ms291 ms10.00.870

Routing halves the mean latency and makes two thirds of requests as fast as the tagger. The cost is 1.2 points against generating everything, which is within noise in the cross-validated estimate (−1.1 points, not significant). The batched number runs the two stages one after the other in this demo; with both GPUs working at once, the generator only has to keep up with 35% of the traffic. Keeping the tagger's confident fields and taking only the rest from the generator was 1 point better than generating everything, but it routes about 80% of documents, so it buys accuracy rather than speed.

A measurement trap

My first latency numbers for the tagger were 20 to 25 ms too low. The script timed single requests on documents it had just processed in the throughput run, and vLLM's prefix cache served most of the forward pass. The 199 tagger prompts fit in the cache; the 1000-document generation runs did not, so their earlier numbers held up when I re-measured them on unseen documents (933, 859 and 670 ms against the published 939, 873 and 678 ms). Time single requests on documents the engine has not seen, or switch the caches off.

When to use which

Caveats

One public dataset with 199 test documents, labels made by string matching, one GPU shared with other jobs (p95 is the noisiest number here). The generator's latency is measured on the first documents of the dataset, some of which are training documents; that affects its accuracy there, not its speed, and accuracy is only reported on the test documents.

I used an AI coding assistant (Claude) to help write the code, run the experiments and draft this post, and I checked the results myself.