Skip to content
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

feat: add remaining Float16 specializations for simd calculation #78

Merged
merged 4 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion include/svs/core/distance/cosine.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,26 @@ template <size_t N> struct CosineSimilarityImpl<N, float, int8_t> {
template <size_t N> struct CosineSimilarityImpl<N, float, Float16> {
SVS_NOINLINE static float
compute(const float* a, const Float16* b, float a_norm, lib::MaybeStatic<N> length) {
auto [sum, norm] = simd::generic_simd_op(CosineFloatOp<16>(), a, b, length);
auto [sum, norm] = simd::generic_simd_op(CosineFloatOp<16>{}, a, b, length);
return sum / (std::sqrt(norm) * a_norm);
}
};

template <size_t N> struct CosineSimilarityImpl<N, Float16, float> {
SVS_NOINLINE static float
compute(const Float16* a, const float* b, float a_norm, lib::MaybeStatic<N> length) {
auto [sum, norm] = simd::generic_simd_op(CosineFloatOp<16>{}, a, b, length);
return sum / (std::sqrt(norm) * a_norm);
}
};

template <size_t N> struct CosineSimilarityImpl<N, Float16, Float16> {
SVS_NOINLINE static float
compute(const Float16* a, const Float16* b, float a_norm, lib::MaybeStatic<N> length) {
auto [sum, norm] = simd::generic_simd_op(CosineFloatOp<16>{}, a, b, length);
return sum / (std::sqrt(norm) * a_norm);
}
};

#endif
} // namespace svs::distance
7 changes: 6 additions & 1 deletion include/svs/index/inverted/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ template <typename T> inline T bound_with(T nearest, T epsilon, svs::DistanceL2)
}

template <typename T> inline T bound_with(T nearest, T epsilon, svs::DistanceIP) {
// TODO: What do we do if the best match is simply bad?
assert(nearest > 0.0f);
return nearest / (1 + epsilon);
}

template <typename T>
inline T bound_with(T nearest, T epsilon, svs::DistanceCosineSimilarity) {
assert(nearest > 0.0f);
return nearest / (1 + epsilon);
}
Expand Down
Loading