-
Notifications
You must be signed in to change notification settings - Fork 24
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
add sq4 quantizer and simd operators #41
Conversation
ShawnShawnYou
commented
Sep 25, 2024
- Add sq4 quantizer and simd operators.
- For sq4-uniform, support simd (avx512, avx2, sse).
- For sq4, only support generic distance computation.
5a799da
to
b33c714
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Part-I
src/quantization/sq4_quantizer.h
Outdated
uint32_t vector_encoded_size_; | ||
}; | ||
|
||
template <MetricType Metric> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metric -> metric
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
src/quantization/sq4_quantizer.h
Outdated
return true; | ||
} | ||
|
||
template <MetricType Metric> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metric -> metric
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
src/quantization/sq4_quantizer.h
Outdated
SQ4Quantizer<Metric>::DecodeOneImpl(const uint8_t* codes, DataType* data) { | ||
for (uint32_t d = 0; d < this->dim_; d++) { | ||
if (d & 1) { | ||
data[d] = ((codes[d / 2] & 0xf0) >> 4) / 15.0 * diff_[d] + lower_bound_[d]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
d / 2
can rewrite to d >> 1
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
} else if (Metric == MetricType::METRIC_TYPE_IP) { | ||
return SQ4ComputeCodesIP(codes1, codes2, lower_bound_.data(), diff_.data(), this->dim_); | ||
} else if (Metric == MetricType::METRIC_TYPE_COSINE) { | ||
return SQ4ComputeCodesIP(codes1, codes2, lower_bound_.data(), diff_.data(), this->dim_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are not implement the Cosine Metric
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, for now, we use IP as a temporary implement for cosine.
SQ4ComputeCodesIP(computer.buf_, codes, lower_bound_.data(), diff_.data(), this->dim_); | ||
} else if (Metric == MetricType::METRIC_TYPE_COSINE) { | ||
dists[0] = | ||
SQ4ComputeCodesIP(computer.buf_, codes, lower_bound_.data(), diff_.data(), this->dim_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
dists[0] = | ||
SQ4ComputeCodesIP(computer.buf_, codes, lower_bound_.data(), diff_.data(), this->dim_); | ||
} else { | ||
dists[0] = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here need print any error log ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
src/simd/generic.cpp
Outdated
float | ||
SQ4ComputeCodesIP(const uint8_t* codes1, | ||
const uint8_t* codes2, | ||
const float* lowerBound, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lowerBound -> lower_bound
src/simd/generic.cpp
Outdated
x_hi = ((codes1[d / 2] & 0xf0) >> 4) * 15.0 / diff[d + 1] + lowerBound[d + 1]; | ||
y_hi = ((codes2[d / 2] & 0xf0) >> 4) * 15.0 / diff[d + 1] + lowerBound[d + 1]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
src/simd/sq4_simd.h
Outdated
float | ||
SQ4ComputeIP(const float* query, | ||
const uint8_t* codes, | ||
const float* lowerBound, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
src/simd/sse.cpp
Outdated
__m128i sum = _mm_setzero_si128(); | ||
__m128i mask = _mm_set1_epi8(0xf); | ||
for (; d + 31 < dim; d += 32) { | ||
auto xx = _mm_loadu_si128((__m128i*)(codes1 + d / 2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
d / 2 -> d >> 1 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
tests/fixtures/fixtures.cpp
Outdated
if (delta < 0) { | ||
delta = 0; | ||
} | ||
if (delta > 0.999) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
else if ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing change ?
e15dcaa
to
de0ef9c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: change all lowerBound to lower_bound
tests/fixtures/fixtures.cpp
Outdated
if (delta < 0) { | ||
delta = 0; | ||
} | ||
if (delta > 0.999) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing change ?
@@ -511,6 +514,76 @@ SQ8ComputeCodesL2Sqr(const uint8_t* codes1, | |||
return Generic::SQ8ComputeCodesIP(codes1, codes2, lowerBound, diff, dim); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lowerBound -> lower_bound
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this issue is belongs to SQ8, it should be fixed in another PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
9af72b7
to
ddc5865
Compare
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
Signed-off-by: zhongxiaoyao.zxy <zhongxiaoyao.zxy@antgroup.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -511,6 +514,76 @@ SQ8ComputeCodesL2Sqr(const uint8_t* codes1, | |||
return Generic::SQ8ComputeCodesIP(codes1, codes2, lowerBound, diff, dim); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK