Speed relative to fastText on one core, GlotLID v3, fastText-turbo 0.1.1: x86 command line 4.22x, x86 Python 4.58x, Mac command line 1.72x, Mac Python 1.84x

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.

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.

Pipeline: sentence, hash words and n-grams, average about 650 rows, score 2,102 labels, softmax and top-k
Fractions from a profile of fastText on GlotLID v3 (one x86 core, 20,000 sentences).

Two steps take almost all the time:

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.

fastText adds 256 products one after another into one running total

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.

Eight labels are scored side by side, each in fastText's order

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.

Eight lanes add every eighth product of one label, then the lanes are summed

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

Version 0.1.1 adds more, all with the same output:

After: where the time goes now

fastText: 12.7 s output layer, 3.6 s averaging, 1.3 s rest, 17.6 s total. fastText-turbo: 2.0 s, 3.5 s, and the rest, 6.3 s total
Profile on one x86 core, GlotLID v3, 20,000 sentences, fastText-turbo 0.1.0 (0.1.1 is within 4% of it on this model).

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:

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 trains on a random span of the line; -normProb trains on the lowercased line without punctuation and digits

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:

Lines per second from Python on one machine. x86 server, 44 threads: GlotLID v3 with fastText 1,130 (one thread), with turbo 85,226; GlotLID v4 with turbo 146,400; GlotLID v4 small with turbo 329,098. Mac, 12 threads: 4,165; 62,944; 142,784; 582,239.
20,000 CommonLID sentences, 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:

MachineCPU, all threadsGPU
x86 server, 48 threads, Tesla T4141,000290,000 (one T4) · 392,000 (two)
Mac, Apple M4 Pro, 12 threads149,000258,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

VersionUbuntuMacOutput vs fastText
fastText before kpu (6c2204b)1,311 · 1.00x4,369 · 1.00xreference
fastText v0.9.21,341 · 1.01x4,581 · 1.00xidentical
kpu/fasterText1,370 · 1.03x4,486 · 0.98xidentical
fastText main (1142dc4)1,382 · 1.04x4,559 · 0.99xidentical
tshev/faster-FastText4,468 · 3.37x7,093 · 1.54xsame labels, probabilities differ
tshev/faster-FastText, giphy branch1,159 · 0.87x7,145 · 1.56xsame labels, probabilities differ
fastText-turbo 0.1.05,340 · 4.07x7,548 · 1.73xUbuntu: same labels, probabilities within 1.1e-5; Mac: identical
fastText-turbo 0.1.15,538 · 4.22x7,496 · 1.72xUbuntu: 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 packageNumPyUbuntu, listUbuntu, one by oneMac, listMac, one by one
fastText before kpu (built from source)11,130 · 1.00x1,123 · 1.00x4,165 · 1.00x4,104 · 1.00x
fasttext 0.9.311,292 · 1.14x1,281 · 1.14x3,904 · 0.99x3,808 · 0.95x
fasttext 0.9.321,289 · 1.13xfails3,998 · 1.01xfails
fasttext-wheel11,263 · 1.11x1,239 · 1.10x3,991 · 1.01x3,931 · 0.99x
fasttext-numpy221,386 · 1.22x1,367 · 1.22x3,941 · 1.00x3,927 · 0.98x
fasttext-numpy2-wheel21,352 · 1.19x1,332 · 1.18x3,972 · 1.01x3,948 · 0.99x
fasttext-predictnonefails1,230 · 1.09xfails3,980 · 1.00x
fasttext-turbo 0.1.024,087 · 3.62x3,953 · 3.52x7,225 · 1.73x7,158 · 1.74x
fasttext-turbo 0.1.125,179 · 4.58x4,106 · 3.66x7,677 · 1.84x7,334 · 1.79x
fasttext-turbo 0.1.115,212 · 4.61x4,237 · 3.77x7,612 · 1.83x7,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.

ThreadsUbuntu, lines/sUbuntuMac, lines/sMac
fastText, 11,3111.00x4,3691.00x
turbo, 15,6964.34x7,4951.72x
turbo, 211,8759.06x14,7093.37x
turbo, 421,18616.2x28,3046.48x
turbo, 842,89232.7x53,79712.3x
turbo, 1258,72544.8x63,87914.6x
turbo, 1674,61956.9x––
turbo, 3295,71073.0x––
turbo, 44120,02391.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, modeUbuntu, fastTextUbuntu, turboMac, fastTextMac, turbo
GlotLID v4, command line, 1 thread3,71212,012 · 3.24x12,65619,229 · 1.52x
GlotLID v4, command line, all threads–227,409–150,106
GlotLID v4, Python list2,9299,682 · 3.31x12,54118,933 · 1.51x
GlotLID v4, Python list, all threads–146,400–142,784
GlotLID v4 small, command line, 1 thread29,98646,458 · 1.55x78,783121,073 · 1.54x
GlotLID v4 small, command line, all threads–648,703–505,320
GlotLID v4 small, Python list22,38646,753 · 2.09x75,345126,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.