Repository navigation
feat(rotator)!: add FHT/block rotation and make it the default - #268
Merged
Merged
Conversation
FHT/Kac transforms a power-of-two window alternately at the start and end of the padded vector and pairs i with i + n/2 in its Kac steps. When the window t is below 3n/4, some inputs keep a path that misses every transform and leave as two fixed spikes of magnitude 1/4, which makes RaBitQ estimates noisier than under a random rotation when data concentrates on those coordinates. FHT/block splits padded_dim into its power-of-two blocks, transforms every block in each of three rounds, and mixes the blocks with a fixed stride gather between rounds, so every coordinate is transformed in every round. It is specified in docs/formats.md, stored as rotator blob method 3, and bit-identical across the generic, AVX2, AVX-512 and NEON backends. New rotators and indexes use FHT/block. Saved FHT/Kac and matrix rotators keep loading and keep their rotation when re-saved; 0.9 cannot read files written with FHT/block. Creating an FHT/Kac rotator now rejects padded_dim above twice its window, which left coordinates untransformed; saved rotators with such padding still load.
FhtNeonTest restores saved signs with extra padding, which can exceed twice the transform window. New FHT/Kac rotators now refuse that padding, so the seeded constructor threw on NEON hosts; the loading constructor accepts it, as loading saved rotators must.
…ondition - rotator.md: raw FHT/Kac state padded past twice the window has no public C++ restore path; name the internal loading constructor. - formats.md and development.md: list a nested blob value that its readers reject when unknown, such as a new rotator method, among the additive changes of format 2, as the blob rules already allow; quant_python.md states the same exception. - quant.Rotator docstring: FHT/Kac mixes poorly when the largest power of two not above dim is below three quarters of padded_dim, which also happens for some power-of-two paddings. - Test comments: scratch grows only above the largest padded dimension a thread has used; the QuantizedGraph legacy-padding case also covers FHT/block.
gouyt13
marked this pull request as ready for review
December 3, 2026 06:08
This was referenced Dec 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds FHT/block, a structured rotation that transforms every coordinate in every round, and makes it the default for new rotators and indexes. Saved indexes and rotators keep loading with their own rotation, so this changes only what new code writes.
Draft until marked ready by the maintainer; a final audit of the changes made after the first independent review found no high or medium issues, and its documentation fixes are in the last commit. CI passes on x86-64, Linux ARM64, Apple silicon and Windows, and the full suite also passed on an Apple M5 Pro (see "Validation").
Why
When the padded dimension
nis not a power of two, FHT/Kac transforms a power-of-two windowtalternately at the start and the end of the vector, and pairsiwithi + n/2in its Kac steps. Whent < 3n/4, each input in[t, min(dim, 3n/2 - t))keeps one path through the Kac steps that misses every window. The input then ends as two fixed spikes of magnitude 1/4, at outputsiandi - n/2; only their signs are random. This affects 1/6 of the inputs at 768, 1536 and 3072 dimensions, and 38–49% at 420, 960 and 1000. More rounds shrink the spikes but do not remove them. Random permutations and re-paired Kac steps were tried and do not help either.Effect on RaBitQ estimates (
rabitqlib.quant, L2, N = 4000, 100 queries, 3 rotator seeds; columns are 4-bit error std relative to a Haar rotation, with the share of estimates outside the library's error bound in brackets):matrix)On real dense embeddings, the outcome was:
So this is a robustness fix rather than a recall fix for today's workloads.
Two smaller defects of the FHT/Kac path:
tis derived fromdim, notpadded_dim: 310 dimensions whose padding is a power of two (for example 993–1023) take the windowed path.padded_dim > 2tleaves coordinates untransformed. For example, at 768/4096, 256 inputs map to ±e_j. Default padding never reaches this.FHT/block
Specified exactly in
docs/formats.md, "FHT rotations". Every backend implements that specification bit for bit.nis split into its power-of-two blocks, largest first; all blocks have at least 32 coordinates.y[i] = x[(i * s) mod n]mixes the blocks.sisfloor(0.618034 n), made odd, then increased by 2 until it is coprime ton.3n/8bytes.Validation of the construction against an independent NumPy model:
nfrom 64 to 4096 in steps of 32, plus sampled rows up to 65536, the basis-image entries have kurtosis 2.98–3.02 (Haar: 3), and the minimum L1 mass is at Haar level.Changes
include/rabitqlib/core/rotator.hpp:RotatorType::FhtBlockRotator(appended, value 2) androtator_impl::FhtBlockRotator.choose_rotatordefaults to it.FhtKacRotatorcreation rejectspadded_dim > 2t; loading saved rotators still accepts it.src/simd/:fht_block_kernels.hpp).fht_block_scratch.hpp).src/core/blob.cppandpython_bindings/quant_bindings.cpp).quant.Rotator(method="fht_block")is the default;"fht_kac", its"fht"alias and"matrix"remain. The native module'sRotatorTypeenum gainsFhtBlockRotator(it is not re-exported fromrabitqlib, as before). The stubs are updated.Compatibility
unknown rotator method 3.choose_rotator<float>(dim)followed byload(), orquant.Rotator(dim, seed=s)used to recreate an old rotation. Such code must passRotatorType::FhtKacRotator/method="fht_kac", or store blobs, which carry the type. zvec is unaffected: it stores the type and always passes it.HierarchicalNSWgains a member, so downstream code must be rebuilt.dimmessage, which now names'fht_block'.rotate, and again at a larger padded dimension.I suggest the
release:breakinglabel when this merges.Performance
Measurement setup:
RABITQ_ENABLE_NATIVE_OPTIMIZATION=OFF.Rotation alone:
NEON on an Apple M5 Pro (AppleClang 21, portable build, single thread, 4096 vectors, median of 9 repetitions, last of 3 stable runs), FHT/Kac → FHT/block through the dispatched rotators:
A per-stage breakdown on x86 puts the remaining non-power-of-two cost on two things:
tofncoordinates — this is the price of the fix;IVF-RaBitQ end to end. Each dataset uses the first 200k vectors and 1000 queries; for dbpedia the queries are held-out rows. Ground truth is exact (float64). There are 1024 shared RaBitQKMeans centroids with Flat routing. Insertion uses 32 threads; search is single-threaded. Recall@10 at nprobe 64 is the mean of 3 rotator seeds:
Validation
ctest666 passed, 14 skipped (AVX backends, Linux-only or failing-device tests); the same suite under ASan/UBSan without reports;pytest1218 passed, 42 skipped (platform or optional dependencies) with the extension built from this branch.ctest: 668 passed. The same suite under ASan/UBSan: 668 passed, no reports.pytest: 1258 passed, 2 skipped, against the package rebuilt from this branch.check-tidy.shwith clang-tidy 23.check-isa-symbols.sh(49 translation units).check-format.shandcheck-python.sh.mkdocs build --strict.tests/core_consumer/run.cmakewith OpenMP ON and OFF.Not tested