fix: portable rotation sampler; exact reflection sign; pin output in CI - #11
Merged
Merged
Conversation
The rotation and QJL projection were drawn with std::normal_distribution, whose algorithm the C++ standard leaves unspecified. From the same mt19937(42), libc++ returns -0x1.08af86p-1, 0x1.38cfd6p+0, 0x1.715284p-1 and libstdc++ returns the first two swapped, then 0x1.bd40ecp-1. So a quantizer built from one (dim, bitwidth, seed) produced different codes on macOS and Linux, and anything that persisted codes keyed by those parameters read garbage when moved across platforms. - rotation.cpp: Box-Muller over mt19937's 32-bit outputs (53-bit uniforms, exact integer handling). Measured bit-identical G and S on macOS arm64 (libc++, Apple clang 21) and Linux arm64 (libstdc++, g++ 11.4). - The proper-rotation sign now comes from the Householder coefficients: det(Q) = (-1)^(nonzero taus), exact and O(dim). Q.determinant() cost an extra O(dim^3) and, in float, returned -0 from dim ~384 up, so the flip never ran at practical dimensions. It was ~18% of construction at d=1536. - types.h: kAlgorithmVersion = 2, bumped whenever output for a given (dim, bitwidth, seed) changes; consumers that persist codes should record it. - tests/determinism_test.cpp: golden hashes of the Gaussian matrix and of quantize() codes at d=64 and d=256. Identical on both platforms above in fastbuild, -c opt, and -c opt -std=c++20. CI now runs `bazel test //...` on ubuntu-latest (libstdc++) and macos-latest (libc++). - README: Determinism section. Breaking: codes for a given seed differ from version 1. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Summary
Codes were not reproducible across platforms. The rotation Π and the QJL projection S were drawn with
std::normal_distribution, whose algorithm the C++ standard leaves unspecified. From the samestd::mt19937(42):-0x1.08af86p-1, 0x1.38cfd6p+0, 0x1.715284p-10x1.38cfd6p+0, -0x1.08af86p-1, 0x1.bd40ecp-1mt19937itself emits identical integers on both. SoQuantizerProd(dim, bitwidth, rng(seed))built a different quantizer on macOS than on Linux, andquantize()output differed at d=128 and d=1536. Anything that persists codes keyed by(dim, bitwidth, seed)— e.g. recall's MediaIndex — read garbage after moving between platforms.Changes
src/rotation.cpp): Box–Muller in double over 53-bit uniforms built from twomt19937draws. Integer handling is exact;log/sqrt/sin/coswere measured bit-identical on both platforms.det(Q) = (−1)^(nonzero Householder taus), O(dim). The oldQ.determinant() < 0cost another O(dim³) and, in float, returned-0from dim ≈ 384 up (|det| = 1, log|det| ≈ 1e-5 from the LU pivots), so the flip silently never ran at practical dimensions.kAlgorithmVersion = 2(types.h): bumped whenever output for a given(dim, bitwidth, seed)changes, so consumers that persist codes can refuse other versions.tests/determinism_test.cpp+ CIbazel test //...: golden hashes of the Gaussian matrix and ofquantize()codes at d=64 and d=256. CI runs on ubuntu-latest (libstdc++) and macos-latest (libc++).Breaking: codes for a given seed differ from version 1.
Verification
-c opt, and-c opt --cxxopt=-std=c++20.bazel build //...,bazel test //...andturboquant_examplepass locally. Through recall's adapter: 18/18 tests pass plain, ASan and UBSan.Performance (construction,
-c opt)Dropping
determinant()saved ~134 ms at d=1536 on the Orin, but Box–Muller (log,sin,cosper pair) is slower than libstdc++'s built-in sampler, so the net is −10%. A portable Marsaglia polar sampler (nosin/cos) could recover some of that; left as a follow-up. Encode, scoring and search are unchanged.🤖 Generated with Claude Code