-
Notifications
You must be signed in to change notification settings - Fork 22
Sq8 to Sq8 dist functions - ip and cosine [MOD-13170] #873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dor-forer
wants to merge
46
commits into
main
Choose a base branch
from
dorer-sq8-dist-functions-ip-cosine
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,629
−94
Open
Changes from all commits
Commits
Show all changes
46 commits
Select commit
Hold shift + click to select a range
746bf31
Add SQ8-to-SQ8 distance functions and optimizations
dor-forer 8697a3e
Add SQ8-to-SQ8 benchmark tests and update related scripts
dor-forer e0ce268
Format
dor-forer ab6b077
Orgnizing
dor-forer 931e339
Add full sq8 bencharks
dor-forer a56474d
Optimize the sq8 sq8
dor-forer a25f45c
Optimize SQ8 distance functions for NEON by reducing operations and i…
dor-forer 0ad941e
format
dor-forer 68cd068
Add NEON DOTPROD-optimized distance functions for SQ8-to-SQ8 calculat…
dor-forer 0b4b568
PR
dor-forer d0fd2e4
Remove NEON DOTPROD-optimized distance functions for INT8, UINT8, and…
dor-forer 9de6163
Fix vector layout documentation by removing inv_norm from comments in…
dor-forer 63a46a1
Remove 'constexpr' from ones vector declaration in NEON inner product…
dor-forer 525f8da
Refactor distance functions to remove inv_norm parameter and update d…
dor-forer 13a477b
Update SQ8 Cosine test to normalize both input vectors and adjust dis…
dor-forer c18000e
Rename 'compressed' to 'quantized' in SQ8 functions for clarity and c…
dor-forer bbf810e
Implement SQ8-to-SQ8 distance functions with precomputed sum and norm…
dor-forer dbbb7d9
Add edge case tests for SQ8-to-SQ8 precomputed cosine distance functions
dor-forer 36ab068
Refactor SQ8 test cases to use CreateSQ8QuantizedVector for vector po…
dor-forer 00617d7
Implement SQ8-to-SQ8 precomputed distance functions using ARM NEON, S…
dor-forer 4331d91
Implement SQ8-to-SQ8 precomputed inner product and cosine functions; …
dor-forer 2e7b30d
Refactor SQ8 distance functions and remove precomputed variants
dor-forer a111e36
Refactor SQ8 distance functions and tests for improved clarity and co…
dor-forer d510b8a
Refactor SQ8 benchmarks by removing precomputed variants and updating…
dor-forer ee26740
foramt
dor-forer afe1a4f
Remove serialization benchmark script for HNSW disk serialization
dor-forer a31f95c
Refactor SQ8 distance functions and tests to remove precomputed norm …
dor-forer f12ecf4
format
dor-forer 0e36030
Merge branch 'main' of https://github.com/RedisAI/VectorSimilarity in…
dor-forer fdc16c6
Refactor SQ8 distance tests to use compressed vectors and improve nor…
dor-forer e5f519c
Update vector layout documentation to reflect removal of sum of squar…
dor-forer db1e671
Refactor SQ8 distance functions to remove norm computation
dor-forer d5b8587
Update SQ8-to-SQ8 distance function comment to remove norm reference
dor-forer 91f48df
Refactor cosine similarity functions to remove unnecessary subtractio…
dor-forer b660111
Refactor cosine similarity functions to use specific SIMD implementat…
dor-forer 9166cac
Refactor benchmark setup to allocate additional space for sum and sum…
dor-forer f28f4e7
Add CPU feature checks to disable optimizations for AArch64 in SQ8 di…
dor-forer e50dc45
Add CPU feature checks to disable optimizations for AArch64 in SQ8 di…
dor-forer 6bbbc38
Fix formatting issues in SQ8 inner product function and clean up cond…
dor-forer 66a5f88
Enhance SQ8 Inner Product Implementations with Optimized Dot Product …
dor-forer d7972e9
Fix header guard duplication and update test assertion for floating-p…
dor-forer a8075bf
Add missing pragma once directive in NEON header files
dor-forer cddc497
Refactor SQ8 distance functions for improved performance and clarity
dor-forer 4f0fec7
Update SQ8 vector population functions to include metadata and adjust…
dor-forer 8ab4192
Refactor SQ8 inner product functions for improved clarity and perform…
dor-forer 8c59cb2
Rename inner product implementation functions for AVX2 and AVX512 for…
dor-forer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| /* | ||
| * Copyright (c) 2006-Present, Redis Ltd. | ||
| * All rights reserved. | ||
| * | ||
| * Licensed under your choice of the Redis Source Available License 2.0 | ||
| * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the | ||
| * GNU Affero General Public License v3 (AGPLv3). | ||
| */ | ||
| #pragma once | ||
| #include "VecSim/spaces/space_includes.h" | ||
| #include "VecSim/spaces/IP/IP_AVX512F_BW_VL_VNNI_UINT8.h" | ||
| #include <immintrin.h> | ||
|
|
||
| /** | ||
| * SQ8-to-SQ8 distance functions using AVX512 VNNI with precomputed sum. | ||
| * These functions compute distance between two SQ8 (scalar quantized 8-bit) vectors, | ||
| * where BOTH vectors are uint8 quantized. | ||
| * | ||
| * Uses precomputed sum stored in the vector data, | ||
| * eliminating the need to compute them during distance calculation. | ||
| * | ||
| * Uses algebraic optimization to leverage integer VNNI instructions: | ||
| * | ||
| * With sum = Σv[i] (sum of original float values), the formula is: | ||
| * IP = min1*sum2 + min2*sum1 + δ1*δ2 * Σ(q1[i]*q2[i]) - dim*min1*min2 | ||
| * | ||
| * Since sum is precomputed, we only need to compute the dot product Σ(q1[i]*q2[i]). | ||
| * The dot product is computed using the efficient UINT8_InnerProductImp which uses | ||
| * VNNI instructions (_mm512_dpwssd_epi32) for native integer dot product computation. | ||
| * | ||
| * Vector layout: [uint8_t values (dim)] [min_val (float)] [delta (float)] [sum (float)] | ||
| */ | ||
|
|
||
| // Common implementation for inner product between two SQ8 vectors with precomputed sum | ||
| // Uses UINT8_InnerProductImp for efficient dot product computation with VNNI | ||
| template <unsigned char residual> // 0..63 | ||
| float SQ8_SQ8_InnerProductImp(const void *pVec1v, const void *pVec2v, size_t dimension) { | ||
| // Compute raw dot product using efficient UINT8 AVX512 VNNI implementation | ||
| // UINT8_InnerProductImp uses _mm512_dpwssd_epi32 for native integer dot product | ||
| int dot_product = UINT8_InnerProductImp<residual>(pVec1v, pVec2v, dimension); | ||
|
|
||
| // Get dequantization parameters and precomputed values from the end of vectors | ||
| // Layout: [data (dim)] [min (float)] [delta (float)] [sum (float)] | ||
| const uint8_t *pVec1 = static_cast<const uint8_t *>(pVec1v); | ||
| const uint8_t *pVec2 = static_cast<const uint8_t *>(pVec2v); | ||
|
|
||
| const float *params1 = reinterpret_cast<const float *>(pVec1 + dimension); | ||
dor-forer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const float min1 = params1[0]; | ||
| const float delta1 = params1[1]; | ||
| const float sum1 = params1[2]; // Precomputed sum of original float elements | ||
|
|
||
| const float *params2 = reinterpret_cast<const float *>(pVec2 + dimension); | ||
| const float min2 = params2[0]; | ||
| const float delta2 = params2[1]; | ||
| const float sum2 = params2[2]; // Precomputed sum of original float elements | ||
|
|
||
| // Apply the algebraic formula using precomputed sums: | ||
| // IP = min1*sum2 + min2*sum1 + δ1*δ2 * Σ(q1[i]*q2[i]) - dim*min1*min2 | ||
| return min1 * sum2 + min2 * sum1 + delta1 * delta2 * static_cast<float>(dot_product) - | ||
| static_cast<float>(dimension) * min1 * min2; | ||
| } | ||
|
|
||
| // SQ8-to-SQ8 Inner Product distance function | ||
| // Returns 1 - inner_product (distance form) | ||
| template <unsigned char residual> // 0..63 | ||
| float SQ8_SQ8_InnerProductSIMD64_AVX512F_BW_VL_VNNI(const void *pVec1v, const void *pVec2v, | ||
| size_t dimension) { | ||
| return 1.0f - SQ8_SQ8_InnerProductImp<residual>(pVec1v, pVec2v, dimension); | ||
| } | ||
|
|
||
| // SQ8-to-SQ8 Cosine distance function | ||
| // Returns 1 - (inner_product) | ||
| template <unsigned char residual> // 0..63 | ||
| float SQ8_SQ8_CosineSIMD64_AVX512F_BW_VL_VNNI(const void *pVec1v, const void *pVec2v, | ||
| size_t dimension) { | ||
| // Assume vectors are normalized. | ||
| return SQ8_SQ8_InnerProductSIMD64_AVX512F_BW_VL_VNNI<residual>(pVec1v, pVec2v, dimension); | ||
| } | ||
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
dor-forer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * Copyright (c) 2006-Present, Redis Ltd. | ||
| * All rights reserved. | ||
| * | ||
| * Licensed under your choice of the Redis Source Available License 2.0 | ||
| * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the | ||
| * GNU Affero General Public License v3 (AGPLv3). | ||
| */ | ||
| #pragma once | ||
| #include "VecSim/spaces/space_includes.h" | ||
| #include "VecSim/spaces/IP/IP_NEON_DOTPROD_UINT8.h" | ||
| #include <arm_neon.h> | ||
|
|
||
| /** | ||
| * SQ8-to-SQ8 distance functions using ARM NEON DOTPROD with precomputed sum. | ||
| * These functions compute distance between two SQ8 (scalar quantized 8-bit) vectors, | ||
| * where BOTH vectors are uint8 quantized. | ||
| * | ||
| * Uses precomputed sum stored in the vector data, | ||
| * eliminating the need to compute them during distance calculation. | ||
| * | ||
| * Uses algebraic optimization with DOTPROD instruction: | ||
| * | ||
| * With sum = Σv[i] (sum of original float values), the formula is: | ||
| * IP = min1*sum2 + min2*sum1 + δ1*δ2 * Σ(q1[i]*q2[i]) - dim*min1*min2 | ||
| * | ||
| * Since sum is precomputed, we only need to compute the dot product Σ(q1[i]*q2[i]). | ||
| * The dot product is computed using the efficient UINT8_InnerProductImp which uses | ||
| * the DOTPROD instruction (vdotq_u32) for native uint8 dot product computation. | ||
| * | ||
| * Vector layout: [uint8_t values (dim)] [min_val (float)] [delta (float)] [sum (float)] | ||
| */ | ||
|
|
||
| // Common implementation for inner product between two SQ8 vectors with precomputed sum | ||
| // Uses UINT8_InnerProductImp for efficient dot product computation with DOTPROD | ||
| template <unsigned char residual> // 0..63 | ||
| float SQ8_SQ8_InnerProductSIMD64_NEON_DOTPROD_IMP(const void *pVec1v, const void *pVec2v, | ||
| size_t dimension) { | ||
| // Compute raw dot product using efficient UINT8 DOTPROD implementation | ||
| // UINT8_InnerProductImp uses vdotq_u32 for native uint8 dot product | ||
| float dot_product = UINT8_InnerProductImp<residual>(pVec1v, pVec2v, dimension); | ||
|
|
||
| // Get dequantization parameters and precomputed values from the end of vectors | ||
| // Layout: [data (dim)] [min (float)] [delta (float)] [sum (float)] | ||
| const uint8_t *pVec1 = static_cast<const uint8_t *>(pVec1v); | ||
| const uint8_t *pVec2 = static_cast<const uint8_t *>(pVec2v); | ||
|
|
||
| const float *params1 = reinterpret_cast<const float *>(pVec1 + dimension); | ||
| const float min1 = params1[0]; | ||
| const float delta1 = params1[1]; | ||
| const float sum1 = params1[2]; // Precomputed sum of original float elements | ||
|
|
||
| const float *params2 = reinterpret_cast<const float *>(pVec2 + dimension); | ||
| const float min2 = params2[0]; | ||
| const float delta2 = params2[1]; | ||
| const float sum2 = params2[2]; // Precomputed sum of original float elements | ||
|
|
||
| // Apply algebraic formula using precomputed sums: | ||
| // IP = min1*sum2 + min2*sum1 + δ1*δ2 * Σ(q1*q2) - dim*min1*min2 | ||
| return min1 * sum2 + min2 * sum1 + delta1 * delta2 * dot_product - | ||
| static_cast<float>(dimension) * min1 * min2; | ||
| } | ||
|
|
||
| // SQ8-to-SQ8 Inner Product distance function | ||
| // Returns 1 - inner_product (distance form) | ||
| template <unsigned char residual> // 0..63 | ||
| float SQ8_SQ8_InnerProductSIMD64_NEON_DOTPROD(const void *pVec1v, const void *pVec2v, | ||
| size_t dimension) { | ||
| return 1.0f - SQ8_SQ8_InnerProductSIMD64_NEON_DOTPROD_IMP<residual>(pVec1v, pVec2v, dimension); | ||
| } | ||
|
|
||
| // SQ8-to-SQ8 Cosine distance function | ||
| // Returns 1 - inner_product (assumes vectors are pre-normalized) | ||
| template <unsigned char residual> // 0..63 | ||
| float SQ8_SQ8_CosineSIMD64_NEON_DOTPROD(const void *pVec1v, const void *pVec2v, size_t dimension) { | ||
| return SQ8_SQ8_InnerProductSIMD64_NEON_DOTPROD<residual>(pVec1v, pVec2v, dimension); | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.