GlotLID is a fastText model. Projects like FineWeb2 run it over billions of documents, so every bit of prediction speed counts. fastText-turbo is a small fork of fastText that predicts about 4x faster on x86 and 1.7x faster on Apple silicon, on one core, with the same models and the same labels. Since version 0.1.1 it also uses every core from Python and can run on a GPU.
- Trained models are byte-identical to fastText's, and model files load in either one.
- On ARM, prediction output is byte-identical. On x86, labels are the same and probabilities differ by at most about 1e-5.
- It is still
import fasttextand thefasttextcommand, with wheels for Python 3.9 to 3.15 and NumPy 1 and 2. model.predict(lines, threads=N)scores a list on N threads, anddevice="cuda"ordevice="mps"runs it on a GPU.
pip uninstall fasttext # both packages provide the `fasttext` module
pip install fasttext-turbo
Where the time goes
To label one sentence, fastText hashes each word and its character n-grams (2 to 5 characters for GlotLID) into rows of a big table. It averages those rows into one vector of 256 numbers. Then it scores that vector against each of the 2,102 labels, and softmax turns the scores into probabilities.
Two steps take almost all the time:
- Scoring the labels (72%). Each label is a dot product of 256 numbers. With 2,102 labels, that is about 538,000 multiply-adds per sentence.
- Averaging the rows (21%). An average GlotLID sentence has about 650 n-gram rows. Each row is 1 KB, read from a 1.7 GB table. This step is limited by memory, not arithmetic.
Everything else (reading, hashing, softmax, top-k) adds up to 7%. The output layer was the place to start.
Why the output layer was slow
fastText computes each label's score as one running total: add a[0]·b[0], then a[1]·b[1],
and so on, 256 times.
The multiplications are cheap. The trouble is that each addition needs the result of the one before it. A modern core can start several floating-point additions per cycle, but an addition takes about 4 cycles to finish. With only one chain of additions, the core spends most of its time waiting.
Fix 1: eight labels at once, exactly
fastText-turbo scores 8 labels together. Each label still has its own running total, added in exactly fastText's order, so every score is the same number, bit for bit. The 8 chains don't depend on each other, so the core can keep 8 additions in flight instead of 1.
One detail matters here: clang on macOS fuses a multiply and an add into one FMA instruction by default. That rounds differently from fastText built elsewhere, so this loop turns FMA contraction off. The training code uses only this exact path, which is why trained models stay byte-identical.
Fix 2: eight lanes inside one label (x86, prediction only)
On x86, an AVX register holds 8 floats. Instead of one total per label, fastText-turbo keeps 8 partial totals for each label: lane 0 adds products 0, 8, 16, …, lane 1 adds 1, 9, 17, …, and so on. One instruction advances all 8 lanes, and at the end the lanes are added together.
This adds the same numbers in a different order, so the last bits of a score can change. On GlotLID v3 the largest change to any probability was 1.1e-5. A probability of 0.99 still prints as 0.99, and on 20,000 test sentences every top-1 label matched fastText. The lane version runs only when predicting; training never uses it.
On Apple silicon the exact 8-label version was already the fastest, so ARM builds keep it, and their output is byte-identical to fastText.
Smaller exact changes
- Top-k: labels whose score is too low to make the top k are skipped before the
logis computed. - Hashing: each n-gram's hash is extended from the previous one instead of being recomputed from its first character.
- Threads:
fasttext predict model.bin input.txt k th threadssplits the input across threads and writes the output in input order.
Version 0.1.1 adds more, all with the same output:
- Batches: a Python list is scored 256 lines at a time, so each block of label rows is read once per batch instead of once per line. With
threads=Nthe batches are shared among threads, without the GIL. - Any width: the rows of a line are summed in registers for every model width up to 320, not only for 16, 32, 64, 96, 128, 256 and 512, so fastText's default width of 100 gets it too.
- Bucket numbers: each n-gram's
hash % bucketsis computed with a multiplication instead of a division (Lemire, Kaser and Kurz, 2019), exact for every hash. - Quantized models: a
.ftzmodel's rows are summed one subquantizer at a time, with its 256 centroids in the L1 cache: 7.2k to 8.6k lines/s for a GlotLID.ftzon one x86 core with the PyPI wheel.
After: where the time goes now
The output layer went from 12.7 s to 2.0 s, 6.5x faster. Averaging the rows did not change, so it is now the biggest cost: 3.5 s of 6.3 s, or 56%.
Where the next speedups are
Averaging is bound by memory. The ~650 rows of a sentence are scattered across a 1.7 GB table, so most reads miss the CPU cache and wait for main memory. Faster arithmetic won't help much here. Reading fewer bytes will:
- Smaller rows: a smaller dimension, or rows stored in 16 or 8 bits, cuts the bytes read per sentence.
- A smaller table: fewer hash buckets, or dropping rare n-grams, keeps more of the table in cache.
- Prefetching: all of a sentence's row numbers are known before any row is read, so the CPU can be told to fetch them early.
Each of these changes the model or its storage, not only the code, so they belong to the model. GlotLID v4 takes the first two: 96 dimensions instead of 256 and a minimum word count of 20,000, so each row a sentence reads is about a third of the size. With fastText-turbo it predicts 2.2x faster than v3 on one x86 core (12,012 vs. 5,538 lines/s) and 2.6x on the Mac (19,229 vs. 7,496), and it is also more accurate. GlotLID v4 small has 16 dimensions and a hierarchical softmax, which scores a path of a tree over the labels instead of all 2,060 of them: 46,458 lines/s on one x86 core and 121,073 on the Mac.
Two training options
We use fastText-turbo to train the next GlotLID, so it has two training options, both off by default. Real inputs are often short, or have been lowercased and stripped of punctuation before language identification. These options show the model such lines during training.
-cropProb P -cropMin A -cropMax B -cropUnit char|word: with probability P, train on a random span of A to B characters or words of the line.-normProb P: with probability P, train on the line lowercased and without punctuation and digits, like the CommonLID input cleaner.
Both are drawn again for each line in each epoch, and the label is kept. They are not saved in the model, so the result is an ordinary fastText model. With both at 0, training is byte-identical to fastText.
New in 0.1.1: every core, and GPUs
fastText's Python predict runs on one thread. In fastText-turbo 0.1.1,
model.predict(lines, threads=N) scores a list on N threads, so one Python process uses the
whole machine:
model.predict(list); fastText on one thread, fastText-turbo 0.1.1 on all threads.
On a GPU, model.predict(lines, device="cuda") builds each line's features on the CPU and does the
rest (averaging the rows, scoring the labels and the top k) with PyTorch, with the same labels as the CPU. The
batch size follows the GPU's free memory, a batch that runs out of memory is split, and a list of devices
(device=["cuda:0", "cuda:1"]) shares the batches. On GlotLID v4 and 1.49 million CommonLID lines:
| Machine | CPU, all threads | GPU |
|---|---|---|
| x86 server, 48 threads, Tesla T4 | 141,000 | 290,000 (one T4) · 392,000 (two) |
| Mac, Apple M4 Pro, 12 threads | 149,000 | 258,000 (device="mps") |
GPU prediction needs PyTorch (pip install fasttext-turbo[gpu]) and a softmax model that is not quantized.
Benchmarks
All numbers are lines per second for GlotLID v3 on 20,000 CommonLID sentences, on one core unless noted.
1x is fastText just before kpu's commits (6c2204b), measured separately
for the command line and for Python, and on each machine. Ubuntu is an x86-64 server; Mac is an Apple M4 Pro.
The fastText and fastText-turbo rows were measured on November 5, 2026, in one session per machine; the
other forks were measured on October 18 against the same baseline.
Command line
| Version | Ubuntu | Mac | Output vs fastText |
|---|---|---|---|
fastText before kpu (6c2204b) | 1,311 · 1.00x | 4,369 · 1.00x | reference |
| fastText v0.9.2 | 1,341 · 1.01x | 4,581 · 1.00x | identical |
| kpu/fasterText | 1,370 · 1.03x | 4,486 · 0.98x | identical |
fastText main (1142dc4) | 1,382 · 1.04x | 4,559 · 0.99x | identical |
| tshev/faster-FastText | 4,468 · 3.37x | 7,093 · 1.54x | same labels, probabilities differ |
| tshev/faster-FastText, giphy branch | 1,159 · 0.87x | 7,145 · 1.56x | same labels, probabilities differ |
| fastText-turbo 0.1.0 | 5,340 · 4.07x | 7,548 · 1.73x | Ubuntu: same labels, probabilities within 1.1e-5; Mac: identical |
| fastText-turbo 0.1.1 | 5,538 · 4.22x | 7,496 · 1.72x | Ubuntu: same labels, probabilities within 1.1e-5; Mac: identical |
tshev's forks are built with -ffast-math -march=native, so their binaries only run on the CPU they were built for. fastText-turbo uses neither, so the same wheel runs everywhere.
Python
model.predict(list) and one sentence at a time, each package in a fresh virtual environment.
| PyPI package | NumPy | Ubuntu, list | Ubuntu, one by one | Mac, list | Mac, one by one |
|---|---|---|---|---|---|
| fastText before kpu (built from source) | 1 | 1,130 · 1.00x | 1,123 · 1.00x | 4,165 · 1.00x | 4,104 · 1.00x |
fasttext 0.9.3 | 1 | 1,292 · 1.14x | 1,281 · 1.14x | 3,904 · 0.99x | 3,808 · 0.95x |
fasttext 0.9.3 | 2 | 1,289 · 1.13x | fails | 3,998 · 1.01x | fails |
fasttext-wheel | 1 | 1,263 · 1.11x | 1,239 · 1.10x | 3,991 · 1.01x | 3,931 · 0.99x |
fasttext-numpy2 | 2 | 1,386 · 1.22x | 1,367 · 1.22x | 3,941 · 1.00x | 3,927 · 0.98x |
fasttext-numpy2-wheel | 2 | 1,352 · 1.19x | 1,332 · 1.18x | 3,972 · 1.01x | 3,948 · 0.99x |
fasttext-predict | none | fails | 1,230 · 1.09x | fails | 3,980 · 1.00x |
fasttext-turbo 0.1.0 | 2 | 4,087 · 3.62x | 3,953 · 3.52x | 7,225 · 1.73x | 7,158 · 1.74x |
fasttext-turbo 0.1.1 | 2 | 5,179 · 4.58x | 4,106 · 3.66x | 7,677 · 1.84x | 7,334 · 1.79x |
fasttext-turbo 0.1.1 | 1 | 5,212 · 4.61x | 4,237 · 3.77x | 7,612 · 1.83x | 7,421 · 1.81x |
"fails": fasttext 0.9.3 raises a ValueError on single strings under NumPy 2, and fasttext-predict does not accept lists. Every package that ran gave the same top-1 label as fastText on all 20,000 sentences. 0.1.1 is faster than 0.1.0 on lists because it scores them in batches. With threads=44 on Ubuntu, 0.1.1 reaches 85,226 lines/s (75x), and with threads=12 on the Mac 62,944 (15x).
Threads
Command line, 200,000 sentences, fastText-turbo 0.1.1, compared with fastText before kpu on one thread.
| Threads | Ubuntu, lines/s | Ubuntu | Mac, lines/s | Mac |
|---|---|---|---|---|
| fastText, 1 | 1,311 | 1.00x | 4,369 | 1.00x |
| turbo, 1 | 5,696 | 4.34x | 7,495 | 1.72x |
| turbo, 2 | 11,875 | 9.06x | 14,709 | 3.37x |
| turbo, 4 | 21,186 | 16.2x | 28,304 | 6.48x |
| turbo, 8 | 42,892 | 32.7x | 53,797 | 12.3x |
| turbo, 12 | 58,725 | 44.8x | 63,879 | 14.6x |
| turbo, 16 | 74,619 | 56.9x | – | – |
| turbo, 32 | 95,710 | 73.0x | – | – |
| turbo, 44 | 120,023 | 91.6x | – | – |
On October 18 the same server gave 160,688 lines/s at 44 threads. 0.1.0 gives 119,632 today, about the same as 0.1.1, so the difference is the machine, not the code.
GlotLID v4 and v4 small
The same 20,000 sentences, fastText before kpu (1x) and fastText-turbo 0.1.1. "All threads" is 44 on Ubuntu and 12 on the Mac.
| Model, mode | Ubuntu, fastText | Ubuntu, turbo | Mac, fastText | Mac, turbo |
|---|---|---|---|---|
| GlotLID v4, command line, 1 thread | 3,712 | 12,012 · 3.24x | 12,656 | 19,229 · 1.52x |
| GlotLID v4, command line, all threads | – | 227,409 | – | 150,106 |
| GlotLID v4, Python list | 2,929 | 9,682 · 3.31x | 12,541 | 18,933 · 1.51x |
| GlotLID v4, Python list, all threads | – | 146,400 | – | 142,784 |
| GlotLID v4 small, command line, 1 thread | 29,986 | 46,458 · 1.55x | 78,783 | 121,073 · 1.54x |
| GlotLID v4 small, command line, all threads | – | 648,703 | – | 505,320 |
| GlotLID v4 small, Python list | 22,386 | 46,753 · 2.09x | 75,345 | 126,875 · 1.68x |
| GlotLID v4 small, Python list, all threads | – | 329,098 | – | 582,239 |
fastText-turbo gains less on these models than on v3: the output layer, which turbo speeds up most, is a smaller share of the work at 96 dimensions, and v4 small's hierarchical softmax does not score every label. On the Mac, v4 small's command line is fastest at 8 threads (513,475 lines/s).
Try it
pip install fasttext-turbo
import fasttext
model = fasttext.load_model("model.bin")
model.predict("Hello, how are you?", k=3)
model.predict(lines, k=1, threads=8) # a list on 8 threads
model.predict(lines, k=1, device="cuda") # on a GPU: pip install fasttext-turbo[gpu]
The code is at github.com/CommonGlot/fastText-turbo and the package is fasttext-turbo on PyPI. It changes fastText in 16 commits on top of upstream, each describing what it changes, so it is easy to review.