numpy==2.2.6
pandas==2.3.3
PyYAML==6.0.3
# Three packages, and for this post that is a deliberate statement rather
# than minimalism. The three mechanisms the post is about are all invisible
# behind the obvious library call:
#
#   - `torch.nn.Embedding` plus a `CrossEntropyLoss` over a B-by-B logit
#     matrix hides the fact that the negatives ARE the other rows' positives.
#   - `faiss.IndexHNSWFlat(d, M)` hides the distance-computation count, which
#     is the only latency unit that does not depend on which machine you ran
#     on.
#   - `faiss.IndexIVFPQ` hides that there are TWO independent recall losses
#     in it, and that only one of them can be tuned away with n_probe.
#
# Everything below is optional and deliberately not installed.
#
# What it would replace: the dense arrays. `data/splits.py` builds a 2,400 by
# 1,500 boolean `seen` mask and `models/baselines.py` builds a 1,500 by 1,500
# item-item product, which are 3.6 MB and 9 MB and therefore fine. At a real
# catalogue they are not: 200 million users by 20 million items is 4 x 10^15
# cells. The item-item product in `ItemKNN.fit` flips first because it is
# quadratic in the catalogue and stops fitting in memory at roughly 40,000
# items; `seen` becomes a per-user Bloom filter or a bitmap in the serving
# cache rather than any kind of matrix.
# scipy==1.15.3
#
# What it would replace: `index/ivfpq.py::kmeans`, which is Lloyd's algorithm
# with k-means++ seeding in 30 lines. `sklearn.cluster.MiniBatchKMeans` is
# the right call at production scale because the IVF coarse quantiser is
# fitted on a sample of a billion vectors and the full-batch version does not
# terminate. Also `sklearn.metrics.ndcg_score`, which is `eval/metrics.py`
# and is four lines, and whose only real difference is that it expects a
# dense relevance matrix over the whole catalogue and therefore cannot be
# used for a retrieval evaluation at all without materialising one.
# scikit-learn==1.6.1
#
# What it would replace: all of `models/towers.py`. The towers are an
# embedding lookup, a two-layer MLP and an L2 normalisation, so the forward
# pass is six lines of torch and the backward pass is zero. What flips at
# production scale is not the model, it is the EMBEDDING TABLE: a 20 million
# item table at 64 dimensions is 5 GB in fp32, which does not fit on one
# accelerator alongside the activations, so the table is sharded and the
# lookup becomes a collective operation. That is the point where a
# hand-written numpy version stops being a teaching tool and starts being
# wrong, because the thing you need to reason about is the sharding and this
# file has nothing to say about it.
# torch==2.6.0
#
# What it would replace: `index/hnsw.py` and `index/ivfpq.py` entirely, and
# in production it should. The numbers that change: the Python graph walk in
# `_search_layer` costs 0.64 ms per query at 1,500 items and ef=128, where
# faiss's C++ walk over the same graph shape is roughly two orders of
# magnitude faster and the per-query cost stops being dominated by the
# interpreter. The DISTANCE COUNTS in the README's tables do not change,
# because they are a property of the algorithm rather than of the
# implementation, which is exactly why they are the number reported.
# faiss-cpu==1.10.0
#
# What it would replace: nothing in this repository, and it is listed because
# it is what you would actually reach for at scale. ScaNN's anisotropic
# quantisation loss is the one real improvement over plain PQ for a
# maximum-inner-product index: it weights the quantisation error by how much
# it distorts the inner product rather than the L2 distance, which matters
# because `index/ivfpq.py` measures a mean reconstruction error of 0.2409
# and then spends the whole n_probe sweep living with it.
# scann==1.3.5
