Sixteen shards, one thread: raggio's IVF fan-out on a box with two kinds of cores

Table of Contents
The first post’s scorecard had a line I glossed over. Attaching the IVF index made the median search 2x faster and made two other rows worse: throughput with 8 queries in flight went down, 129 to 116, and filtered search went from 29 ms to 72. Part of this one was never in raggio’s code.
Twenty threads doing nothing
numpy’s math library, OpenBLAS, starts one thread per core, 20 on the GB10, and they busy-wait for work: a thread with nothing to do spins instead of sleeping, so it’s ready the instant a matrix multiplication arrives. On a dedicated box that’s 20 threads burning CPU at all times, and the probe saw about 16 cores busy with a single query in flight.
The GB10 makes that expensive. It has two kinds of cores, 10 fast Cortex-X925 and 10 slower Cortex-A725, and the same IVF query took 4.1 ms pinned to a fast core and 8.3 ms on a slow one. With 20 spinners competing, the scheduler kept pushing raggio’s own threads onto whatever was free, most likely the slow cores. OPENBLAS_NUM_THREADS=1 took the median IVF search from 11.3 ms to 8.4 and filtered search from 71 ms to 37, without touching a line of raggio. It costs 7 to 9% on the index build, where k-means does use the threads, and I took that trade. In the final A/B, CPU per query under load went from 50 ms to 9.7, and most of that is the spinners: with one BLAS thread, the code before the fan-out was already at 5.2.
The GB10’s 20 cores in two clusters, the fast Cortex-X925 ones outlined. By default OpenBLAS starts 20 spinner threads and the probe saw about 16 cores busy with one query in flight; the query thread gets whatever core is left. Toggle one BLAS thread. The per-core query times are pinned measurements, the server medians come from the probe’s A/B.
Sixteen shards, one thread
The IVF index splits the collection into 256 shards and a query scans the 16 closest. raggio made those 16 calls into turbovec one after the other, on one thread, through a single pipeline that also batched concurrent queries together. A probe measured the ceiling: in process, fanned out over 12 threads, the same index answered 610 to 660 queries per second. The server did 203.
Plan F fans out: a pool of 12 threads, one task per query and shard, results merged in a fixed order so the output is identical to the serial path, bit for bit, ties included. Filters moved into the same tasks. Instead of intersecting the whole filter with each shard, which built the 2 MB arrays from part 3, each task intersects its own shard’s ids, in arrays of about 64 KB.
The A/B: throughput with 8 queries in flight 203 → 342 queries per second, median search 8.8 → 6.2 ms, filtered search 49 → 6.2 ms, recall and results identical, memory the same. The bill is CPU: 5.2 ms per query became 10.1. The same 128 kernel calls that take 27 ms of CPU on one thread take 63 ms spread over twelve, and a placement table from the probe says why in part: a call that lands on a slow core takes 3.2x longer, and a thread on another core starts with cold caches. At 16 queries in flight the server keeps 4 of 20 cores busy, and the rest of the gap to the in-process ceiling is HTTP and the event loop.
A batch of 8 queries, 16 shard calls each, over the pool. The wall times are the probe’s measured sweep for this batch; the block lengths spread them evenly across the threads, which is a simplification. CPU is measured at 1 and 12 threads only.
The grouping that only worked on x86
The desktop research had a better idea than plain fan-out: group queries that probe the same shard into one call, so turbovec scans the shard once for all of them. On the desktop’s AVX2 it was 6.5x with everything combined. On the DGX it never beat plain fan-out, and on one thread it was 39% slower. Two reasons. With 8 queries in flight only 1.4 queries on average share a shard, so there was little to group. And on NEON a call with 2 or 3 queries cost 2 to 5 times as much per query as a call with one.
That second number was itself wrong, and it took until plan F’s A/B to find out. Pinned to a single core, 2 and 3 queries per call cost exactly what one does, and 4 share work as designed. The excess was placement: turbovec runs multi-query calls on its own thread pool, and where that pool thread landed, a slow core or another core’s cache, decided the cost. Unpinned, five runs of the same microbenchmark spread from 1.5x to 3x. The draft of the upstream ask to turbovec changed accordingly, from “pad small batches” to “a 2-query tile”, with its acceptance measured pinned. And part 1’s rule was born: on a box with two kinds of cores, pin before you read a microbenchmark, and pin the pool’s worker separately.
Cost per query of a multi-query turbovec call, relative to a one-query call, by the core the pool’s worker landed on, with the caller pinned to a fast core. Unpinned is the spread of five runs. Measured on the DGX during plan F’s A/B.
The escape hatch failed its own gate
Plan F kept a switch, IVF_SEARCH_THREADS=1, that was supposed to reproduce the old serial path exactly, and its gate said so: within the noise band of the old code. It failed both ways at once, faster on serial search (by 1.2 ms, band 1.05) and slower under concurrency (by 20 queries per second, band 10.8). A simple slowdown wouldn’t do that. A follow-up in balanced order (old, new, new, old) put the two level, without reproducing the gap and without ruling out a smaller one.
This is the fourth override from part 1, the one the agent made itself under my “assume I’m not at the desk” instruction, citing my override from part 4 as precedent. Two later reviews narrowed its wording, from “the gap was container variance” to “consistent with container variance, which the follow-up didn’t test”. The lesson I keep: on a noisy host, an equivalence gate needs paired runs in balanced order from the start. Three runs in a fixed order can’t tell an arm effect from an order effect.
The gate that would have measured nothing
One more from the pre-flight. Plan F’s memory gate compared the container’s peak memory between the old and new code, read after each benchmark run. But the benchmark ends every run with a cold-start measurement, which restarts the container, and a restarted container very likely gets fresh memory accounting with a new peak. The gate would have compared two restart footprints, a collection load and one search, and missed the concurrent, filtered and hybrid phases, which is exactly where 12 pool threads would show. Both arms would have read the same, and the gate would have passed without measuring what it existed for. An agent found that by reading the code, before anything ran. The peak is now read just before the restart.
Part 6 in numbers
From the final A/B, the original code against the final one, IVF index, 2.55 million vectors in a 4 GiB container:
| Metric | Before | After |
|---|---|---|
| Throughput, 8 in flight | 191 q/s | 380 q/s |
| Search, median | 10.9 ms | 6.1 ms |
| Filtered search, median | 37.2 ms | 5.9 ms |
| CPU per query, 8 in flight | 50 ms | 9.7 ms |
| Recall@10 | 0.993 | 0.994 |
Next Monday
Part 7, November 23, the last one: free-threaded Python. A win I couldn’t ship, a lock that spins 40 times before it sleeps, a bug from raggio’s first release, and a crash inside CPython.