reading invoices on a cpu: free ocr, a rust parser, no gpu
the brief sounded simple: take a scanned invoice, pull out the header fields and every line item, and do it on one ordinary cpu box. no gpu, and the invoice never leaves the machine.
the first instinct today is to throw a big vision model at the page and ask it nicely. i tried that. it was slow, it was patchy, and it was the wrong tool. what actually worked was boring: free traditional ocr, a parser written in rust, and a small model used only where the rules genuinely couldn’t decide.
where it ended up
| start | now | |
|---|---|---|
| line items, dev set | 72.8% | 89.6% |
| all fields, dev set | n/a | 90.0% |
| all fields, held-out test set | n/a | 87.9% |
| two hand-checked invoices | 76.6% | 97.6% offline / 98.6% with ocria-1 |
| line items add up to the invoice total (300 unlabeled invoices) | 79.3% | 96.5% |
| time per invoice | 10-30 s (vlm only) | 1-3 s pipeline; 5-17 s with the vlm reader |
that last column is the part i care about most. a vision model alone took 10-30 seconds per invoice. the pipeline does the same job in a couple of seconds on a desktop cpu.
the thing that changed my approach
before optimising anything, i checked why cells were wrong. the answer surprised me: in about 87% of the wrong cells, the correct value was already sitting in the ocr text. the ocr had read it fine. the pipeline just put it in the wrong column or glued it to the wrong row.
a reading problem wants a smarter model. a placement problem wants better rules about where things belong in a table. that one finding is why most of the gain came from a rust parser and not from a bigger model.
the pipeline, in one picture
model selection: what i tried, and what each one cost
a lot of the work was ruling things out. every candidate was run against the same labelled dev set on the same cpu:
| candidate | outcome |
|---|---|
| qwen2.5-3b-instruct, extracting line items | 23-43% accuracy, 26-70 s. rejected for items, kept for headers only |
| paddleocr pp-ocrv4 | 60% vs 77% for ocria-nano, 30 s/page |
| qwen2.5-vl-3b on row crops | 5-7 s per row crop, no better than ocr on batch / pack / hsn |
| granite-docling-258m | ~1 s/row but drops empty cells (~8% on dev), so rows shift |
| docling standard pipeline | no table found on 6 of 20 dev pages, 10-27 s per dense page |
| oxr 1.3b document vlm | 2-3 tok/s on cpu, wrong on a test invoice |
| image preprocessing + second ocr pass | gains on the old parser vanished on the 87% parser (-2.0 to +0.17 pts) |
| ocria-1 as the only reader | 43.5% on one vendor’s invoices, 30-60 s per invoice |
none of these are bad models. each one was solving a harder problem than the one i had.
what i kept: ocria-nano for ocr (onnx, cpu), qwen2.5-3b q4_k_m (2.1 gb) for headers and party cleanup, and ocria-1 as a targeted second reader.
the rust parser does the real work
it’s plain box arithmetic over the word boxes the ocr returns, no model involved:
- column assignment: a header-keyword dictionary finds the header row, then a dynamic program over token type and x-position assigns every word to a column
- row assembly: per-page deskew, glued-token splitting (two values fused by the ocr), and a footer-row stop so the table ends where it should
- arithmetic repair (
postfix.rs): when the ocr drops a cell, quantity, rate and total are recovered from the identities (qty × rate = line total, line totals sum to the invoice total, tax is a percentage of both). a quantity of 0 next to a positive total is treated as a misread, not a fact - header and party rules: deterministic extraction first; the small model only cleans what’s left
and the guard rails that made repair safe instead of clever:
- cgst/sgst % and line discount % are derived only when the invoice’s own arithmetic confirms them
- a gstin is accepted only with a valid mod-36 checksum. that stopped a bank account number from being “repaired” into a tax id
it ships as one rust binary: actix-web + tokio, opt-level=3, lto, codegen-units=1, stripped. the median invoice finishes in about 1 s. dead code is gone (zero compiler warnings), and after each cleanup the behaviour was verified identical on the dev and hand-checked sets.
the models, in detail
ocria-1: quantization and repack
ocria-1 is a qwen35-architecture (qwen3.5-based, ~4b) vision-language model. the original release is 10.35 gb of bf16 safetensors. cpu inference needs gguf with low-bit weights.
| artifact | size | used |
|---|---|---|
| bf16 safetensors | 10.35 gb | no |
| gguf f16 | 9.94 gb | no |
| gguf q8_0 | 5.29 gb | no (1.7× more bytes read per token than q4_k_m; not benchmarked) |
| gguf q4_k_m | 3.17 gb | yes, 3.3× smaller than bf16 |
| mmproj f16 | 672 mb | no |
| mmproj q8_0 | 367 mb | yes |
- q4_k_m is a 4-bit k-quant: blocks of weights share scales, and a mix of 4- and 6-bit types goes to the tensors that hurt most when quantized
- the vision projector is quantized separately to q8_0 (672 → 367 mb). it runs once per image and is cheap to keep at 8-bit
- the mtp repack: the base model was repacked so its multi-token-prediction head (
nextn_predict_layersin the gguf metadata) is kept. llama.cpp uses that head as a draft model for self-speculative decoding, so no second model is loaded. i did not re-quantize or edit weights myself; this uses a published mtp repack and its gguf build - our repack (private hugging face repos in the spritle org): only the q4_k_m file, the mmproj-q8_0 and the upstream licence for ocria-1; and the
litocria-nano package (onnx detector, router, recognisers, table model, bundled onnxruntime 1.31.1) byte-for-byte for the ocr step. every repo has a model card with file sizes and sha256 scripts/download_models.shverifies size and sha256 from the hugging face lfs headers, resumes interrupted downloads (curl -C -), and retries
speculative decoding (mtp)
flags: --spec-type draft-mtp --spec-draft-n-max 3. the mtp head proposes up to 3 tokens, the full model verifies them in a single batched forward pass, and accepted tokens come at near-zero extra cost.
llama-server settings
-c 16384 -np 1 -fa on -t 6 -tb 12
- flash attention (
-fa on) cuts attention memory traffic - one slot (
-np 1): parallel slots split the cache and the memory bandwidth, and the workload is one invoice at a time - threads:
-t 6for decode,-tb 12for batch/prefill on the 6-core / 12-thread ryzen 5 7600x - thinking off per request (
chat_template_kwargs.enable_thinking=false). with it on, the model spent ~900 tokens reasoning and returned an empty reply - resident weights:
mmap+mlockso the weights stay in ram, plus a warm-up request on boot so the first real invoice isn’t slow
qwen2.5-3b for headers: gated, not always on
qwen2.5-3b q4_k_m runs header and party-name/address cleanup. LLM_MODE=gated runs it on about 21% of invoices (the ones the rules flag). always gave the same accuracy at 10.6 s vs 2.3 s per invoice, so gated is the default. that’s a policy choice, not a quality trade.
the token budget: reducing what the vlm reads and writes
at ~30 tok/s, tokens are the budget, so the work was cutting both sides.
output
- native output is markdown with html tables: ~190 tokens per row for 17 columns. the prompt asks for tab-separated text with the printed column titles as line 1, and html is converted to tsv if the model returns it anyway
- a stop sequence at
</table>ends generation before it runs on into stamps and totals;max_tokensis 700
input
- image tokens cost prefill time (1000 tokens ≈ 7-12 s at 85-150 tok/s, my estimate from the measured rates), and accuracy drops below ~1000 image tokens for a full page, so full pages aren’t sent
VLM_CROP=colsuses the ocr word boxes to find the header span of the weak columns (upc, cs/pcs, qty, free, batch, exp, pack, units) and sends only that strip, at a 700k-pixel budget and 170 dpi
| what the model reads | time | accuracy (2 hand-checked invoices) |
|---|---|---|
| whole table crop, both invoices | 33-57 s | 99.6% |
| + stop sequence | 13 s (invoice a) | n/a |
| weak-column strips | 5.6 s (invoice a), 16.9 s (invoice b) | 98.6% |
i took the strip configuration: about 1 point of accuracy for 3-6× lower latency.
arbitration: the vlm can’t make things worse
the vlm and the pipeline run concurrently. a vlm cell replaces a pipeline cell only if the pipeline cell is empty or flagged and the new value keeps the row arithmetic valid (qty × rate, discount, gst, line total, invoice-level sums). rows are matched by identity, and any timeout or malformed reply keeps the pipeline result.
how i kept myself honest
- a cell-level grader against labelled dev (18 files) and test splits, plus hand-checked invoices, plus a label-free sum-check (do line items equal the invoice total?) on 300 unlabeled invoices
- every change is compared per file, not just on the aggregate. a rule that took “state name” as the product column dropped one test invoice from 71% to 30% while the aggregate went up
- agents never touched the test split
licensing: the catch on “free”
- the ocr (ocria-nano pipeline), the rust parser and qwen2.5-3b are free to run, but qwen2.5-3b is under the qwen research licence
- the ocria models use a modified openrail-m licence: research purposes only for organisations above its revenue or funding thresholds, and for document-extraction services. production use needs a commercial licence from llamaindex
- the pipeline runs without ocria-1 at 1-3 s per invoice; ocria-1 is the optional accuracy boost (5-17 s) once the licence is sorted
what i’d take away
the lesson wasn’t “cpu is enough”. it was measure where the errors actually come from before reaching for a bigger model. here the answer was layout, which is a rules-and-geometry problem, and rules in rust are fast, free and explainable. the model is still useful, just as a second opinion on a few cells rather than the whole engine.