Skip to main content

fastlinkage

tests PyPI License: MIT Python 3.10+

Exact hierarchical clustering on the GPU, without the n x n distance matrix.

It returns the same tree as scipy.cluster.hierarchy.linkage for Ward and average linkage, runs in seconds at 100,000+ points, and uses memory that grows with the number of points, not its square.

Time to build the exact tree. 20,000 text embeddings, average linkage with cosine distance: 0.8 s against 47 s for SciPy. 100,000 points with 32 features, Ward: 2.2 s against 2.3 minutes for fastcluster. 120,000 text embeddings, Ward: 8.7 s against 59 minutes for fastcluster.

Status: early release. Tested on NVIDIA GPUs (CUDA), Apple GPUs (PyTorch MPS) and the CPU on Linux, macOS and Windows.

Why

SciPy and scikit-learn build the full distance matrix. At 120,000 points that is 58 GB, so most people give up on hierarchical clustering long before that and switch to k-means or HDBSCAN. This package keeps the exact tree and drops the matrix:

SciPy / scikit-learn fastlinkage
Memory n x n distances one mean vector per cluster
20,000 text embeddings (384 features), average cosine 47 s 0.8 s
120,000 text embeddings, average cosine needs a 58 GB matrix 6.9 s
Result exact exact, same tree

Install

pip install fastlinkage

Needs Python 3.10+, NumPy, SciPy and PyTorch. It uses CUDA or Apple MPS when PyTorch sees one, and the CPU otherwise.

Hardware Status
Apple GPU (MPS) tested; main benchmarks below
CPU on Linux, macOS, Windows tested on every commit
NVIDIA GPU (CUDA) tested on a Tesla T4 (notebook)

The package is pure Python on top of PyTorch, so there is nothing to compile.

Use

Drop-in for SciPy's linkage, with the same arguments:

from fastlinkage import linkage    # was: from scipy.cluster.hierarchy import linkage

Z = linkage(X, "ward")
Z = linkage(X, "average", metric="cosine")
Z = linkage(X, "average", metric="sqeuclidean")

X is a NumPy array or a torch tensor with one point per row. Other linkages raise an error that says so; nothing falls back silently to the slow path.

Z is SciPy's linkage matrix, so everything downstream keeps working:

from scipy.cluster.hierarchy import dendrogram, fcluster

labels = fcluster(Z, t=50, criterion="maxclust")
dendrogram(Z, truncate_mode="lastp", p=40)

Cluster embeddings without choosing the number of clusters:

from fastlinkage import AgglomerativeClustering

model = AgglomerativeClustering(
    n_clusters=None, distance_threshold=0.35, linkage="average", metric="cosine"
).fit(embeddings)

model.labels_       # cluster of every row
model.n_clusters_   # how many clusters the threshold gave

The class follows scikit-learn's AgglomerativeClustering (labels_, children_, distances_, fit_predict, get_params), so it works with sklearn.base.clone and in pipelines. Cutting one tree at several levels costs almost nothing:

from fastlinkage import cut, linkage

Z = linkage(embeddings, "average", metric="cosine")
coarse = cut(Z, n_clusters=20)
fine = cut(Z, distance_threshold=0.25)

A runnable version is in examples/quickstart.py.

Reference

linkage(X, method="ward", metric=None, *, device=None) SciPy-format linkage matrix. method and metric as in SciPy: "ward", or "average" with "cosine" or "sqeuclidean". device is "cuda", "mps" or "cpu"; by default the GPU if there is one.
cut(Z, n_clusters=None, distance_threshold=None) Labels from a tree. Give exactly one of the two.
AgglomerativeClustering(n_clusters=2, *, metric="euclidean", linkage="ward", distance_threshold=None, device=None) scikit-learn style estimator. Supported pairs: ward + euclidean, average + cosine, average + sqeuclidean.

Benchmarks

Measured on an Apple laptop GPU (PyTorch MPS, 18 CPU cores for the baselines). Every timing builds the full tree.

Data Points Linkage fastlinkage Baseline
Text embeddings, 384 features 20,000 Average, cosine 0.8 s SciPy 47 s
Text embeddings, 384 features 20,000 Ward 1.0 s SciPy 16 s
Text embeddings, 384 features 120,000 Average, cosine 6.9 s SciPy needs 58 GB
Text embeddings, 384 features 120,000 Ward 8.7 s fastcluster 59 min
Synthetic, 32 features 100,000 Ward 2.2 s fastcluster 135 s
Synthetic, 32 features 200,000 all three 5.9 to 6.4 s not run
Synthetic, 32 features 500,000 Average, cosine 33 s not run
Synthetic, 32 features 500,000 Ward 36 s not run
  • The text embeddings are AG News titles and descriptions, embedded with all-MiniLM-L6-v2.
  • fastcluster's linkage_vector is the standard exact Ward that avoids the matrix. It is single-threaded.
  • The same code on the CPU is 2x to 4x slower than on this GPU, so most of the gap to SciPy comes from the method, not the hardware.
Exact Ward tree on 32-feature data. fastlinkage: 0.4 s at 20,000 points, 2.2 s at 100,000, 6.4 s at 200,000, 36 s at 500,000. fastcluster: 5.2 s at 20,000, 32 s at 50,000, 2.3 minutes at 100,000.

Time grows with the square of the number of points. About a million points is the practical limit.

NVIDIA GPU

A free Google Colab machine: Tesla T4 with 2 virtual CPUs. Synthetic data.

Points Features Ward Average, cosine Average, sq. Euclidean Baseline on the same machine
20,000 32 1.0 s 0.8 s 0.9 s SciPy 23 to 24 s
100,000 32 3.9 s 4.0 s 5.3 s fastcluster Ward 490 s
100,000 384 11.4 s 12.7 s 14.8 s not run
200,000 32 10.8 s 10.0 s 11.1 s not run

The trees match SciPy at 20,000 points and fastcluster at 100,000 to 1e-12. notebooks/cuda_check.ipynb reproduces this.

Exactness

The tree is checked against independent implementations:

Check Result
SciPy, 5,000 and 20,000 points, all three linkages, synthetic and real embeddings merge heights equal to 1e-12; flat clusterings identical
fastcluster Ward, 100,000 synthetic points heights equal to 5e-13
fastcluster Ward, 120,000 text embeddings heights equal to 1.4e-14
Duplicated rows, near-duplicates (1e-7 noise), data offset by 1e4 equal to SciPy to 1e-13

This takes some care. The GPU search runs in float32, and a plain float32 search picks a slightly wrong neighbour often enough to change the tree: an earlier version of this code was off by up to 1.6 in merge height (tallest merge 82) on the 120,000 embeddings. The current version accepts a float32 result only when it wins by more than the possible rounding error, and otherwise compares the close contenders in float64.

With exact ties between different pairs (an integer grid, for example) the tree is not unique, and this package and SciPy can return different valid trees.

How it works

Three known ideas and two additions.

  1. Cluster summaries instead of a matrix. For these linkages the distance between two clusters follows from each cluster's size, mean vector and one scalar:

    Linkage Distance between clusters A and B
    Ward sqrt(2 |A||B| / (|A|+|B|)) x ||mean(A) - mean(B)||
    Average, cosine 1 - mean(A) . mean(B), rows scaled to unit length
    Average, squared Euclidean ||mean(A) - mean(B)||^2 + var(A) + var(B)
  2. Merge in rounds. Every pair of clusters that are each other's nearest neighbour merges in the same round. For these linkages that gives exactly the tree of the one-merge-at-a-time algorithm. A full run takes 50 to 180 rounds.

  3. Nearest-neighbour search as one matrix product on the GPU.

  4. Neighbour lists that survive merges. A search returns 16 candidates and a radius beyond which nothing else can be. When a cluster's neighbour is merged away, the next one usually comes from that list with no new search, and a merged cluster inherits its parents' lists. This halves the search work (from about 3 n^2 to 1.5 n^2 pair evaluations).

  5. A float32 rounding check with a float64 fallback, described above.

docs/DESIGN.md has the details: the neighbour-list rule, the rounding check, more measurements, and what was tried and dropped.

None of the underlying algorithm is new, and this package does not claim it.

Idea Where it comes from
Merging reciprocal nearest neighbours in rounds Murtagh (1980s); ParChain (2021, CPU); RAC (2021)
Average linkage from means and variances in linear memory ParChain (CPU, kd-trees, low dimensions); arXiv 1404.3439
Exact Ward on the GPU cuhc (2026, CUDA)

What I could not find elsewhere is a GPU implementation of exact average linkage (cosine or squared Euclidean) that avoids the distance matrix and works on high-dimensional embeddings. cuhc keeps the matrix for average linkage; ParChain's version is CPU-only and built for low dimensions. The search was not exhaustive, so treat "first" as unconfirmed. I have also not checked whether the neighbour-list rule (item 4) is published.

Limits

  • Three linkages only: Ward, average with cosine, average with squared Euclidean. Complete linkage and average linkage with plain Euclidean distance have no per-cluster summary and are not supported.
  • Time is quadratic in the number of points.
  • A float32 copy of every cluster mean lives on the GPU: 8 n d bytes (3 GB for a million points with 384 features). On CUDA a float64 copy is kept there too when it fits in a quarter of the free memory.
  • When distances are below float32 resolution (cosine distance between almost parallel vectors), the float64 fallback does most of the work and is slow. A warning is raised.
  • When several pairs are at exactly the same distance (integer or binary data, grids), more than one tree is correct. The tree returned is one of them, but it can differ from the one SciPy picks.
  • Data that merges like a chain (evenly spaced points on a line, for example) needs about as many rounds as there are points, and is slow: 3,000 such points take 8 s on an Apple GPU, against 0.5 s on the CPU.
  • Tested on one NVIDIA GPU (Tesla T4) so far, and not compared with cuhc on the same hardware.

Reproduce

git clone https://github.com/michael-L-i/fastlinkage && cd fastlinkage
pip install -e ".[test,bench]"
pytest                                        # exactness tests, CPU only
python experiments/correctness.py             # against SciPy
python experiments/make_embeddings.py         # AG News embeddings (176 MB)
python experiments/neighbour_lists.py         # 20k to 120k points
python experiments/large_runs.py              # fastcluster check, 200k, 500k
python experiments/make_charts.py             # the charts above

experiments/ lists every script, and the raw numbers are in results/.

Repository layout

src/fastlinkage/   the package
tests/             exactness tests against SciPy and scikit-learn
notebooks/         CUDA check for Google Colab
examples/          quickstart
experiments/       benchmark and correctness scripts
results/           their output (JSON)
docs/DESIGN.md     how it works, measurements, prior work
assets/            README charts

License

MIT

Metadata

Release files for fastlinkage 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastlinkage 0.2.1
File Size Uploaded Provenance Actions
fastlinkage-0.2.1.tar.gz 22.7 kB With provenance Details for fastlinkage-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fastlinkage 0.2.1
File Interpreter ABI Platform Actions
fastlinkage-0.2.1-py3-none-any.whl Python 3 none any Details for fastlinkage-0.2.1-py3-none-any.whl

Total release size: 44.4 kB

Release files / fastlinkage-0.2.1.tar.gz

Download URL fastlinkage-0.2.1.tar.gz
Size 22.7 kB
Tags Source
SHA-256 checksum
How to use checksums
005ea7d437da7fef2762fd9e39f4bae37b371027628ce1e65bf3dd3bdfa84ba7
BLAKE2b-256 checksum
How to use checksums
a1c5c7040c1931c08f607920cca130afd261d8ed9b71ede8302220f4a5a0e00d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Release files / fastlinkage-0.2.1-py3-none-any.whl

Download URL fastlinkage-0.2.1-py3-none-any.whl
Size 21.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3dfebb0705d1a1a1a92d74d18ae15419aa8a930dd870755078f97941e6a1b808
BLAKE2b-256 checksum
How to use checksums
cebb590e5138f194734d79d3b8ed9db5e377d67609bd91677553ad85017390c4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

Security for fastlinkage 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for fastlinkage-0.2.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page