-
Notifications
You must be signed in to change notification settings - Fork 970
feat(turbo): add int8/int4/fp16 record quantizers with scalar kernels, rework kernel dispatch #624
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
JalinWang
wants to merge
4
commits into
alibaba:main
Choose a base branch
from
JalinWang:feat/turbo-int8
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.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,7 +88,10 @@ enum class DataType { | |
| }; | ||
|
|
||
| enum class QuantizeType { | ||
| kDefault, | ||
| //! Deprecated: no dispatch row serves kDefault anymore; request the | ||
| //! explicit quantize type (kFp32, kFp16, kRecord, ...) instead. The | ||
| //! enumerator is kept (value 0) for serialized-header compatibility. | ||
| kDefault [[deprecated("request an explicit QuantizeType instead")]], | ||
| kUniform, | ||
| kRecord, | ||
| kFp16, | ||
|
|
@@ -129,6 +132,21 @@ ZVEC_TURBO_API QueryPreprocessFunc get_query_preprocess_func( | |
| MetricType metric_type, DataType data_type, QuantizeType quantize_type, | ||
| CpuArchType cpu_arch_type = CpuArchType::kAuto); | ||
|
|
||
| // All kernels of a single dispatched kernel family. `preprocess` is non-null | ||
| // when the batch kernel requires the query to be preprocessed first (e.g. | ||
| // the AVX512-VNNI int8 kernels expect a +128 uint8-shifted query). | ||
| struct DistanceKernels { | ||
| DistanceFunc dist{}; | ||
| BatchDistanceFunc batch{}; | ||
| QueryPreprocessFunc preprocess = nullptr; | ||
| }; | ||
|
|
||
| // Aggregate lookup: resolves dist/batch/preprocess in one pass so callers | ||
| // cannot pair functions from different kernel families. | ||
| DistanceKernels get_distance_kernels( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里为何不定义ZVEC_TURBO_API? |
||
| MetricType metric_type, DataType data_type, QuantizeType quantize_type, | ||
| CpuArchType cpu_arch_type = CpuArchType::kAuto); | ||
|
|
||
| // Returns the SIMD kernel for the uniform quantizer on the current CPU for | ||
| // the given output data_type, or nullptr if no SIMD implementation is | ||
| // available (callers must keep a scalar fallback). This is a | ||
|
|
||
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,40 @@ | ||
| // Copyright 2025-present the zvec project | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "scalar/fp16/cosine.h" | ||
| #include "scalar/fp16/inner_product.h" | ||
|
|
||
| namespace zvec::turbo::scalar { | ||
|
|
||
| void cosine_fp16_distance(const void *a, const void *b, size_t dim, | ||
| float *distance) { | ||
| // inner_product_fp16_distance returns -real_IP; cosine = 1 - real_IP = 1 + | ||
| // ip. | ||
| float ip; | ||
| inner_product_fp16_distance(a, b, dim, &ip); | ||
|
|
||
| *distance = 1 + ip; | ||
| } | ||
|
|
||
| void cosine_fp16_batch_distance(const void *const *vectors, const void *query, | ||
| size_t n, size_t dim, float *distances) { | ||
| inner_product_fp16_batch_distance(vectors, query, n, dim, distances); | ||
| // inner_product batch returns -real_IP per element; cosine = 1 - real_IP = 1 | ||
| // + d. | ||
| for (size_t i = 0; i < n; i++) { | ||
| distances[i] = 1 + distances[i]; | ||
| } | ||
| } | ||
|
|
||
| } // namespace zvec::turbo::scalar |
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,30 @@ | ||
| // Copyright 2025-present the zvec project | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| namespace zvec::turbo::scalar { | ||
|
|
||
| // Compute cosine distance (negative inner product after normalization) | ||
| // between a single quantized FP16 vector pair. | ||
| void cosine_fp16_distance(const void *a, const void *b, size_t dim, | ||
| float *distance); | ||
|
|
||
| // Batch version of cosine_fp16_distance. | ||
| void cosine_fp16_batch_distance(const void *const *vectors, const void *query, | ||
| size_t n, size_t dim, float *distances); | ||
|
|
||
| } // namespace zvec::turbo::scalar |
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,46 @@ | ||
| // Copyright 2025-present the zvec project | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "scalar/fp16/inner_product.h" | ||
| #include <cstdint> | ||
| #include <zvec/ailego/utility/float_helper.h> | ||
|
|
||
| namespace zvec::turbo::scalar { | ||
|
|
||
| // Compute negated inner product between a single FP16 vector pair. | ||
| // Returns -dot(a, b) so that callers can derive cosine distance as 1 + ip. | ||
| void inner_product_fp16_distance(const void *a, const void *b, size_t dim, | ||
| float *distance) { | ||
| const uint16_t *m = reinterpret_cast<const uint16_t *>(a); | ||
| const uint16_t *q = reinterpret_cast<const uint16_t *>(b); | ||
|
|
||
| float sum = 0.0f; | ||
| for (size_t i = 0; i < dim; ++i) { | ||
| sum += zvec::ailego::FloatHelper::ToFP32(m[i]) * | ||
| zvec::ailego::FloatHelper::ToFP32(q[i]); | ||
| } | ||
|
|
||
| *distance = -sum; | ||
| } | ||
|
|
||
| // Batch version of inner_product_fp16_distance. | ||
| void inner_product_fp16_batch_distance(const void *const *vectors, | ||
| const void *query, size_t n, size_t dim, | ||
| float *distances) { | ||
| for (size_t i = 0; i < n; ++i) { | ||
| inner_product_fp16_distance(vectors[i], query, dim, &distances[i]); | ||
| } | ||
| } | ||
|
|
||
| } // namespace zvec::turbo::scalar |
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,32 @@ | ||
| // Copyright 2025-present the zvec project | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| namespace zvec::turbo::scalar { | ||
|
|
||
| // Compute inner product distance between a single quantized FP16 | ||
| // vector pair. Returns -dot(a, b) so that callers can derive cosine | ||
| // distance as 1 + ip. | ||
| void inner_product_fp16_distance(const void *a, const void *b, size_t dim, | ||
| float *distance); | ||
|
|
||
| // Batch version of inner_product_fp16_distance. | ||
| void inner_product_fp16_batch_distance(const void *const *vectors, | ||
| const void *query, size_t n, size_t dim, | ||
| float *distances); | ||
|
|
||
| } // namespace zvec::turbo::scalar |
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,45 @@ | ||
| // Copyright 2025-present the zvec project | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include "scalar/fp16/squared_euclidean.h" | ||
| #include <cstdint> | ||
| #include <ailego/utility/math_helper.h> | ||
| #include <zvec/ailego/utility/float_helper.h> | ||
|
|
||
| namespace zvec::turbo::scalar { | ||
|
|
||
| void squared_euclidean_fp16_distance(const void *a, const void *b, size_t dim, | ||
| float *distance) { | ||
| const uint16_t *m = reinterpret_cast<const uint16_t *>(a); | ||
| const uint16_t *q = reinterpret_cast<const uint16_t *>(b); | ||
|
|
||
| float sum = 0.0f; | ||
| for (size_t i = 0; i < dim; ++i) { | ||
| sum += zvec::ailego::MathHelper::SquaredDifference( | ||
| zvec::ailego::FloatHelper::ToFP32(m[i]), | ||
| zvec::ailego::FloatHelper::ToFP32(q[i])); | ||
| } | ||
|
|
||
| *distance = sum; | ||
| } | ||
|
|
||
| void squared_euclidean_fp16_batch_distance(const void *const *vectors, | ||
| const void *query, size_t n, | ||
| size_t dim, float *distances) { | ||
| for (size_t i = 0; i < n; ++i) { | ||
| squared_euclidean_fp16_distance(vectors[i], query, dim, &distances[i]); | ||
| } | ||
| } | ||
|
|
||
| } // namespace zvec::turbo::scalar |
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,31 @@ | ||
| // Copyright 2025-present the zvec project | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| namespace zvec::turbo::scalar { | ||
|
|
||
| // Compute squared euclidean distance between a single quantized FP16 | ||
| // vector pair. | ||
| void squared_euclidean_fp16_distance(const void *a, const void *b, size_t dim, | ||
| float *distance); | ||
|
|
||
| // Batch version of squared euclidean FP16. | ||
| void squared_euclidean_fp16_batch_distance(const void *const *vectors, | ||
| const void *query, size_t n, | ||
| size_t dim, float *distances); | ||
|
|
||
| } // namespace zvec::turbo::scalar |
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,58 @@ | ||
| // Copyright 2025-present the zvec project | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| // Shared scalar helpers for record_quantized_int4 distance implementations. | ||
| // | ||
| // Record-quantized INT4 layout (see core::RecordQuantizer::quantize_record): | ||
| // | ||
| // [ original_dim / 2 bytes: packed signed int4 elements (lo nibble first) ] | ||
| // [ float scale_a ] (a: 1 / scale) | ||
| // [ float bias_a ] (b: -bias / scale) | ||
| // [ float sum_a ] (s: sum of quantized codes) | ||
| // [ float square_sum_a ] (s2: sum of squared codes, euclidean metrics) | ||
| // or | ||
| // [ int int8_sum ] (sum of raw codes, non-euclidean metrics) | ||
| // | ||
| // Total tail size: 16 bytes = 32 int4 units. For the Cosine metric an | ||
| // additional fp32 norm is appended after the tail (20 bytes = 40 int4 | ||
| // units); it is not used for distance computation. | ||
| // | ||
| // All `dim` arguments below are expressed in int4 units, following the core | ||
| // convention (element size in bytes is dim / 2). | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| namespace zvec::turbo::scalar::internal { | ||
|
|
||
| // Raw integer inner product of two packed signed int4 code arrays holding | ||
| // `size` int4 elements (`size` must be even). | ||
| inline float ip_int4_scalar(const void *a, const void *b, size_t size) { | ||
| const uint8_t *lhs = reinterpret_cast<const uint8_t *>(a); | ||
| const uint8_t *rhs = reinterpret_cast<const uint8_t *>(b); | ||
|
|
||
| float sum = 0.0f; | ||
| for (size_t i = 0; i < (size >> 1); ++i) { | ||
| int8_t m_lo = static_cast<int8_t>(lhs[i] << 4) >> 4; | ||
| int8_t m_hi = static_cast<int8_t>(lhs[i] & 0xf0) >> 4; | ||
| int8_t q_lo = static_cast<int8_t>(rhs[i] << 4) >> 4; | ||
| int8_t q_hi = static_cast<int8_t>(rhs[i] & 0xf0) >> 4; | ||
| sum += static_cast<float>(m_lo * q_lo + m_hi * q_hi); | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| } // namespace zvec::turbo::scalar::internal |
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.
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.
这个枚举值删了吧?