Skip to content

Commit

Permalink
Cosine optimization
Browse files Browse the repository at this point in the history
Fixed inner product ordering
  • Loading branch information
cainamisir committed Jul 4, 2024
1 parent 3525ff9 commit 27b93f3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
7 changes: 4 additions & 3 deletions apis/python/test/test_distance_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,17 @@ def test_inner_product(tmp_path):

inner_products_sklearn = np.dot(query_vectors, dataset_vectors.T)

sorted_inner_products_sklearn = np.sort(inner_products_sklearn, axis=1)
sorted_inner_products_sklearn = np.sort(inner_products_sklearn, axis=1)[:, ::-1]

distances, _ = index.query(query_vectors, k=5)

sorted_distances = np.sort(distances, axis=1)
# multiply distances by -1 to get inner products, since library returns negative inner products
distances = -1 * distances

for i in range(len(sorted_inner_products_sklearn)):
compare = sorted_inner_products_sklearn[i][:5]
assert np.allclose(
compare, sorted_distances[i], 1e-4
compare, distances[i], 1e-4
), "Inner products do not match"


Expand Down
10 changes: 5 additions & 5 deletions src/include/scoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,22 +260,22 @@ struct inner_product_distance {
#ifdef __AVX2__
template <feature_vector V, feature_vector U>
constexpr inline float operator()(const V& a, const U& b) const {
return avx2_inner_product(a, b);
return -avx2_inner_product(a, b);
}

template <feature_vector V>
constexpr inline float operator()(const V& a) const {
return avx2_inner_product(a);
return -avx2_inner_product(a);
}
#else
template <feature_vector V, feature_vector U>
constexpr inline float operator()(const V& a, const U& b) const {
return unroll4_inner_product(a, b);
return -unroll4_inner_product(a, b);
}

template <feature_vector V>
constexpr inline float operator()(const V& a) const {
return unroll4_inner_product(a);
return -unroll4_inner_product(a);
}
#endif
};
Expand All @@ -296,7 +296,7 @@ struct cosine_distance {
template <feature_vector V, feature_vector U>
constexpr inline float operator()(const V& a, const U& b) const {
return 1 -
inner_product(a, b) / (sqrt(l2_distance(a)) * sqrt(l2_distance(b)));
(-inner_product(a, b)) / sqrt(l2_distance(a) * l2_distance(b)); // our inner product is negative so we have to flip it
}
};

Expand Down

0 comments on commit 27b93f3

Please sign in to comment.