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

add sq4 quantizer and simd operators #41

Merged
merged 14 commits into from
Oct 14, 2024

Conversation

ShawnShawnYou
Copy link
Collaborator

  1. Add sq4 quantizer and simd operators.
  2. For sq4-uniform, support simd (avx512, avx2, sse).
  3. For sq4, only support generic distance computation.

@ShawnShawnYou ShawnShawnYou force-pushed the add-sq4-quantizer-and-simd-operators branch from 5a799da to b33c714 Compare September 25, 2024 04:08
@wxyucs wxyucs added the kind/feature New feature or request label Sep 25, 2024
@wxyucs wxyucs self-assigned this Sep 25, 2024
Copy link
Collaborator

@jiaweizone jiaweizone left a 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 Show resolved Hide resolved
uint32_t vector_encoded_size_;
};

template <MetricType Metric>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metric -> metric

Copy link
Collaborator Author

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 Show resolved Hide resolved
return true;
}

template <MetricType Metric>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metric -> metric

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

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];
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

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_);
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

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_);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Collaborator Author

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;
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

float
SQ4ComputeCodesIP(const uint8_t* codes1,
const uint8_t* codes2,
const float* lowerBound,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lowerBound -> lower_bound

Comment on lines 240 to 241
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];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

float
SQ4ComputeIP(const float* query,
const uint8_t* codes,
const float* lowerBound,
Copy link
Collaborator

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));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d / 2 -> d >> 1 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if (delta < 0) {
delta = 0;
}
if (delta > 0.999) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing change ?

@ShawnShawnYou ShawnShawnYou force-pushed the add-sq4-quantizer-and-simd-operators branch from e15dcaa to de0ef9c Compare September 27, 2024 08:36
src/quantization/sq4_quantizer.h Outdated Show resolved Hide resolved
src/quantization/sq4_quantizer.h Outdated Show resolved Hide resolved
src/quantization/sq4_quantizer.h Outdated Show resolved Hide resolved
src/quantization/sq4_quantizer.h Outdated Show resolved Hide resolved
src/quantization/sq4_uniform_quantizer.h Outdated Show resolved Hide resolved
src/simd/generic.cpp Outdated Show resolved Hide resolved
src/simd/generic.cpp Outdated Show resolved Hide resolved
src/simd/generic.cpp Outdated Show resolved Hide resolved
src/simd/generic.cpp Outdated Show resolved Hide resolved
src/simd/sq4_simd.h Outdated Show resolved Hide resolved
Copy link
Collaborator

@jiaweizone jiaweizone left a 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

src/quantization/sq4_quantizer.h Show resolved Hide resolved
src/quantization/sq4_uniform_quantizer.h Show resolved Hide resolved
src/quantization/sq4_uniform_quantizer.h Show resolved Hide resolved
src/quantization/sq4_uniform_quantizer.h Outdated Show resolved Hide resolved
src/quantization/sq4_uniform_quantizer.h Show resolved Hide resolved
src/quantization/sq4_uniform_quantizer.h Outdated Show resolved Hide resolved
if (delta < 0) {
delta = 0;
}
if (delta > 0.999) {
Copy link
Collaborator

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lowerBound -> lower_bound

Copy link
Collaborator Author

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?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

LHT129 and others added 11 commits October 9, 2024 16:11
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>
@ShawnShawnYou ShawnShawnYou force-pushed the add-sq4-quantizer-and-simd-operators branch from 9af72b7 to ddc5865 Compare October 9, 2024 08:31
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>
Copy link
Collaborator

@wxyucs wxyucs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@ShawnShawnYou ShawnShawnYou changed the title Add sq4 quantizer and simd operators add sq4 quantizer and simd operators Oct 12, 2024
Copy link
Collaborator

@jiaweizone jiaweizone left a 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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

@ShawnShawnYou ShawnShawnYou merged commit b391616 into main Oct 14, 2024
2 checks passed
@ShawnShawnYou ShawnShawnYou deleted the add-sq4-quantizer-and-simd-operators branch October 14, 2024 06:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature New feature or request size/XXL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants