Repository navigation
Cheaper float32 cosine KNN on x86 (v0.1.10-paperless.4) - #4
Merged
Merged
Conversation
The three AVX dispatch sites each cached __builtin_cpu_supports("avx2") in a
function-local static. Fill one vec_avx_caps struct (avx, avx2, fma) in
sqlite3_vec_init and read it at the sites, so a new kernel can gate on fma
without another lazy cache.
Idea and struct from feature/simd-additional (277aed8).
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…k-th A float32 cosine KNN recomputed the query's squared magnitude for every stored row. Compute it once per query and use an AVX2+FMA kernel that accumulates only dot and |a|^2, gated on vec_avx_caps.avx2 and .fma. Once the global top k is full, drop rows that are not strictly nearer than the k-th before min_idx(): merge_sorted_lists keeps the earlier row on a tie and ranks NaN last, so such a row can never enter the result. A full scan of 60k 768-dim vectors runs 181.8M instead of 109.9M instructions. Wall time moves little there (about 3%): the scan is bound by memory bandwidth. float32 distances can differ in the last digits. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.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.
Fork-only change for the unreleased
v0.1.10-paperless.4. No upstream PR.Changes
0a241f9Centralize AVX CPU capability detection into avec_avx_capsstruct (avx,avx2,fma) filled once insqlite3_vec_init. The l2, cosine and hamming dispatch sites read it instead of each caching__builtin_cpu_supports("avx2")in a function-local static. Idea and struct fromfeature/simd-additional(277aed8); the rest of that branch is not carried.43e3c80Compute the cosine query norm once per KNN query, not once per row, with an AVX2+FMA kernel that accumulates only dot and |a|^2 (gated onvec_avx_caps.avx2and.fma). Rows that cannot beat the k-th distance are dropped beforemin_idx().acf062dChangelog entry.Measurements (i7-9750H VM,
-O3, d=768 float32 cosine)Tested
make test-unitpasses;tests/test-loadable.pygives 46 passed, 4 skipped (on the VM, x86_64).Not carried
The rest of
feature/simd-additional. paperless-ngx only uses float32 cosine, and that branch'scosine_float_avx2(2c64ea3) measured about 14% slower than the asg017#364 kernel with the data in L1.🤖 Generated with Claude Code