perf: runtime AVX2 distance dispatch; fix %zu crashes in error paths - #1
Merged
Merged
Conversation
Portable x86-64 builds leave SQLITE_VEC_ENABLE_AVX off, so float inner product, L2 and cosine ran scalar strict-order loops. Add AVX2+FMA kernels with per-function target attributes (four accumulators) and select them after a one-time CPUID check; baseline TUs stay baseline ISA. Compile-time AVX builds and non-GCC/Clang compilers are unchanged, and SQLITE_VEC_DISABLE_X86_DISPATCH opts out. Threadripper 3960X, -O2, pinned core, 20k rows: 384d inner product 268 -> 67 ns (4.0x), L2 3.9x, cosine 3.4x; 768d 2.8-4.3x. A new test checks the dispatched kernels against the scalar reference for dims 1-70, 384, and 768.
SQLite's printf has no z length modifier: "%zu" parses as its %z (free-after-use string) conversion, so every error path that formatted a size_t dereferenced the integer as a char* and crashed. That turned invalid input into a segfault instead of an error, e.g. CREATE VIRTUAL TABLE ... vec0(embedding float[0]) and loading an HNSW index with a dangling entry point. Use %llu with explicit casts; the existing test_overflow and test_persistence_fuzz cases now pass instead of crashing.
384 dims pad to 512 for the FWHT rotation, so each vector costs 64 code bytes plus 8 bytes of factors (21.3x vs FP32), and with 100 vectors the fixed centroid/rotation state brings the total to ~17x. The old "> 20x (~32x expected)" bound ignored padding and fixed cost and failed on every platform; assert the achievable range instead.
Merged
8 tasks
3 tasks
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
SQLITE_VEC_ENABLE_AVX) ran scalar strict-order loops for float inner product, L2 and cosine. Newsimd/x86_dispatch.hppadds AVX2+FMA kernels with per-functiontargetattributes (four accumulators) selected after a one-time CPUID check; baseline TUs stay baseline ISA. Compile-time AVX builds and non-GCC/Clang compilers are unchanged;SQLITE_VEC_DISABLE_X86_DISPATCHopts out. Threadripper 3960X, -O2, 20k rows: 384d inner product 268 → 67 ns (4.0x), L2 3.9x, cosine 3.4x; 768d 2.8–4.3x.zlength modifier;%zuparses as its%z(free-after-use string) conversion, so error paths dereferenced asize_taschar*and crashed. Reachable from invalid input (vec0(embedding float[0])) and from corrupt HNSW persistence (dangling entry point). Six call sites fixed;test_overflowandtest_persistence_fuzznow pass instead of segfaulting.> 20x (~32x expected)bound failed on every platform.Tests
AI usage
Consumer: trvon/yams
perf/simd-release-build. Please merge with a merge commit or rebase (not squash) so the submodule SHA YAMS pins stays reachable, or bump the YAMS pointer after merging.