papie lemmatization · tagging · any language

CPU inference

INT8 quantization

PaPie.quantize() converts LSTM/GRU/Linear layers to dynamic INT8 after loading — no retraining. Measured on two Latin models — the best benchmark architecture (copy decoder, byte input) and lasla-plus — on 20 000 test tokens (the effect comes from the layer types, not the language):

ModelVariant1 thread8 threadsLemmaPOS
copy decoder (papie)fp32514 tok/s1 377 tok/s0.97000.9635
int8798 (1.55×)2 109 (1.53×)0.96960.9639
lasla-plus (pie)fp32484 tok/s1 438 tok/s0.97180.9606
int8745 (1.54×)2 088 (1.45×)0.97170.9604

About 1.5× faster with no measurable accuracy change (differences of ±0.0004 are noise at this sample size). Most of the gain comes from the decoder, whose per-step linear layers dominate.

Where the time goes

Profiling the copy-decoder model, 66 % of CPU time is the autoregressive lemma decoder loop (many tiny steps) and 34 % the embedding, sentence encoder and POS head.

lemma decoder loop — 66 %rest — 34 %many tiny steps, Linear layers → INT8 helps (≈1.5×)embedding · sentence encoder · POSCPU time of one prediction pass (copy-decoder model). ONNX export covers only the right part.

Several processes or several threads

Both use the cores of a CPU. Measured on 100,000 tokens of Latin (INT8, AMD Ryzen 7 5800X: 8 cores, 16 threads):

Setuptokens/s
1 process, 1 thread1 176
1 process, 4 threads3 039
1 process, 8 threads4 079
1 process, 16 threads3 130
4 single-thread processes3 041 (3 413 without start-up)
8 single-thread processes4 171 (5 012 without start-up)
16 single-thread processes4 071 (5 508 without start-up)

On this machine the wall-clock throughput is the same, about 4 100 tokens/s, for 8 threads and for 8 or 16 processes. The processes pay about 4 to 6 seconds to start and load the model, so they win only on long inputs (about 20 % faster once started) and when one process with many threads stops scaling (16 threads are slower than 8 here). Use workers for large corpora:

tagger = from_pretrained("<org>/<name>").quantize()
tagger.annotate(sentences, workers=8)           # fp32: the same output as workers=1
python -m papie.cli annotate --model <org>/<name> --input raw.txt --output tagged.tsv --workers 8 --quantize

Sentences are sorted by length and handed out in shards, so every process gets batches of similar lengths and the order of the result is unchanged. It needs a model loaded from an exported folder or a Hub repository (each process loads it once), and it is meant for CPU.

  • fp32: the output is identical to a single process (the tests check it).
  • INT8: dynamic quantization scales the activations of each batch, so a different batching, with workers or just another batch_size, can change a very small number of tokens (1 of 986 in a check on Latin). The accuracy is unchanged on average.
  • Scripts: the worker processes are started fresh and import your script, so call annotate(..., workers=N) under if __name__ == "__main__":, as with any Python multiprocessing code.

Finished words leave the decoder batch

The greedy lemma decoder stops feeding a word to the network as soon as it has produced its end-of-word symbol, and trims the source states to the longest word still running. Most lemmas are short, so later steps handle a few words instead of the whole batch: 1.6x faster in fp32 and 1.6x in INT8 on one thread (512 to 826 and 807 to 1 262 tokens/s on Latin). In fp32 the output is identical to decoding the whole batch to the end (the tests compare the two); in INT8 1 token of 20 000 changed, with the same accuracy (97.17 % lemma), because dynamic quantization scales each batch.

ONNX

Exporting the character and sentence BiRNN blocks to ONNX works and is numerically identical (max difference 1e-7), but ONNX Runtime was not faster than PyTorch’s own CPU kernels for them (LSTM 28 vs 30 ms, GRU 30 vs 15 ms on a 600-word batch; ORT’s INT8 LSTM no better). The remaining 66 % is a data-dependent decoding loop that does not export without a rewrite. ONNX is therefore not recommended for these models; INT8 quantization is.

Benchmark your own model

python -m papie.cli cpu-benchmark -c config.yaml --ckpt best.ckpt \
    --tokens 20000 --threads 1,8 --variants fp32,int8
   variant |    threads |      tok/s |   speed-up |  lemma acc |    pos acc
      fp32 |          1 |        514 |      1.00x |     0.9700 |     0.9635
      int8 |          1 |        798 |      1.55x |     0.9696 |     0.9639

It samples the test file once, warms up, and reports tokens per second (decoding only) and per-task exact-match accuracy for every variant and thread count; --json out.json saves the rows. Timings are noisy on a busy machine — compare the ratios.