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
- Generate: Qwen3.5-9B, QLoRA (r = 16, 600 steps, 35 minutes), trained to answer the same prompt it gets at inference with single-line JSON. For serving, the adapter is merged into the original BF16 shards with the MTP drafter weights kept, then run with the fastest accuracy-neutral configuration from the decode post (MLP weights in NVFP4 and the rest in FP8, weight-only on the Humming kernel; MTP with 4 draft tokens and an NVFP4 drafter head).
- Tag: Qwen3.5-0.8B or 2B with LoRA and a linear head on the first sub-token of each OCR word (2,000 steps; 43 minutes for 0.8B, 54 for 2B), served with vLLM's pooling runner in one forward pass.
Results
| method | p50 | p95 | docs/s, batched | F1 (199 test docs) |
|---|---|---|---|---|
| generate, 9B zero-shot | 668 ms | 818 ms | 6.0 | 0.680 |
| generate, 9B fine-tuned | 663 ms | 748 ms | 6.5 | 0.882 |
| tag, 2B fine-tuned | 72 ms | 88 ms | 16.9 | 0.857 |
| tag, 0.8B fine-tuned | 68 ms | 95 ms | 18.3 | 0.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:
| tagger | docs/s before → after | ΔF1 [95% CI] |
|---|---|---|
| 0.8B | 8–10 → 18.3 | −0.001 [−0.006, +0.003] |
| 2B | 3–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:
| method | one at a time: p50 | p95 | mean | batched docs/s | F1 |
|---|---|---|---|---|---|
| generate only (9B fine-tuned) | 590 ms | 735 ms | 607 ms | 6.6 | 0.882 |
| tag only (2B, 1 MP) | 72 ms | 90 ms | 78 ms | 21.1 | 0.857 |
| tag, then generate 35% | 72 ms | 767 ms | 291 ms | 10.0 | 0.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
- Highest accuracy: fine-tune the generator. On this data it is the most accurate, at the same speed as zero-shot.
- Lowest latency or cost: the tagger, with a pixel cap. It needs word-level labels and covers only values that appear verbatim in the OCR text and field names seen in training.
- Both: tag first, generate for the doubtful documents.
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.