Repository navigation
Conversation
cosine_int8_neon() summed the products of int8 elements exactly, but in 32 bits, which the squares of -128 overflow from 131,072 elements. vec_distance_cosine() then returned NULL where a magnitude wrapped negative or to zero, or a wrong distance: 3.27 for a vector of 300,000 elements of 127 and one of 300,000 elements of 64, whose distance is 0. Each pass now sums its products in 32-bit lanes, which hold at most 4 products each, and adds them into 64-bit lanes, as mceachen's photostructure/sqlite-vec@4e7a65b does in that fork for the same kernel. Widening only the final sum would not be enough: over 1,048,576 elements, the lane that holds every 8th square of -128 reaches 2^31 on its own. test_vec_distance_cosine_int8_overflow checks mceachen's three cases from that commit against a float64 reference: 131,072 squares of 128, whose sum is 2^31; 2^32 in total, which wraps a 32-bit sum to 0; and 1,048,576 squares of 128, which overflow each 32-bit lane. Their squares keep every partial sum exact in float, so the scalar loop matches too. On main the NEON build returns NULL for all three.
This branch has not been deployed
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.
cosine_int8_neon()sums the products ofint8elements exactly, but in 32 bits, which the squares of -128 overflow from 131,072 elements.vec_distance_cosine()then returns NULL where a magnitude wraps negative or to zero, or a wrong distance:Each pass now sums its products in 32-bit lanes, which hold at most 4 products each, and adds them into 64-bit lanes, as mceachen's photostructure/sqlite-vec@4e7a65b does in that fork for the same kernel. Widening only the final sum would not be enough: over 1,048,576 elements, the lane that holds every 8th square of -128 reaches 2^31 on its own.
test_vec_distance_cosine_int8_overflowchecks mceachen's three cases from that commit against a float64 reference: 131,072 squares of 128, whose sum is 2^31; 2^32 in total, which wraps a 32-bit sum to 0; and 1,048,576 squares of 128, which overflow each 32-bit lane. Their squares keep every partial sum exact infloat, so the scalar loop matches too. On main the NEON build returns NULL for all three and the test fails; with this change it passes on the NEON and the scalar build, as does the rest oftests/test-*.py(240 passed, 66 skipped on both). The NEON build here is Clang's, since GCC cannot build the NEON code on main (#322). Merged with #339, which builds on #322 and turns NEON on for Linux arm64, it builds with GCC, with the same warnings as #339 alone, and passes the suite.The kernel is 0de765f's, which both forks ported: photostructure/sqlite-vec's main has this fix as mceachen's 4e7a65b, and vlasky#20 ports the kernel with it. #338 sums the
float32cosine kernel and scalar loop in double and leaves this kernel alone. This merges cleanly with #327 through #331 and #336 through #355; #267 conflicts with main itself, in the same three places with or without this change.