-
Notifications
You must be signed in to change notification settings - Fork 666
Exact vector search #2519
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
Merged
Merged
Exact vector search #2519
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
55ec947
First Knn version
azevaykin 2e20f2c
Serialization/deserialization
azevaykin b636bcc
InnerProductDistance
azevaykin 40afce6
Trivial PR fixes
azevaykin 897ee20
EnumerateVectors
azevaykin d33cc3e
Remove memcpy
azevaykin 8a24959
double -> float
azevaykin 6a37abd
InnerProductSimilarity
azevaykin 6be9f7b
TAutoMap
azevaykin 385cf97
CosineSimilarity
azevaykin 320200b
Typo
azevaykin 1317e6e
CosineDistance
azevaykin 5167376
EuclideanDistance
azevaykin 7e85069
template <typename TCallback>
azevaykin 95de228
Remove excessive passes
azevaykin 02344d4
Review fixes
azevaykin 9d46bed
ISerializer
azevaykin 1006413
refactor TFloatVectorSerializer
azevaykin f21ee11
Remove virtual classes
azevaykin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
#include <ydb/library/yql/public/udf/udf_helpers.h> | ||
|
||
#include <util/generic/buffer.h> | ||
#include <util/stream/format.h> | ||
|
||
using namespace NYql; | ||
using namespace NYql::NUdf; | ||
|
||
enum EFormat : unsigned char { | ||
DoubleVector = 1 | ||
}; | ||
|
||
TString SerializeDoubleVector(const TUnboxedValuePod x) { | ||
const EFormat format = EFormat::DoubleVector; | ||
|
||
TString str; | ||
TStringOutput outStr(str); | ||
if (const auto elements = x.GetElements()) { | ||
const auto size = x.GetListLength(); | ||
outStr.Reserve(1 + size * sizeof(double)); | ||
outStr.Write(&format, sizeof(unsigned char)); | ||
for (ui32 i = 0; i < size; ++i) { | ||
double element = elements[i].Get<double>(); | ||
outStr.Write(&element, sizeof(double)); | ||
} | ||
} else { | ||
outStr.Write(&format, sizeof(unsigned char)); | ||
const auto it = x.GetListIterator(); | ||
TUnboxedValue v; | ||
while(it.Next(v)) { | ||
double element = v.Get<double>(); | ||
outStr.Write(&element, sizeof(double)); | ||
} | ||
} | ||
return str; | ||
} | ||
|
||
NYql::NUdf::TUnboxedValue DeserializeDoubleVector(const IValueBuilder *valueBuilder, TStringRef str) { | ||
if (str.Size() % sizeof(double) != 0) | ||
return {}; | ||
|
||
const ui32 count = str.Size() / sizeof(double); | ||
|
||
TUnboxedValue* items = nullptr; | ||
auto res = valueBuilder->NewArray(count, items); | ||
|
||
for (ui32 i = 0; i < count; ++i) { | ||
double element; | ||
memcpy(&element, str.Data() + i * sizeof(double), sizeof(double)); | ||
azevaykin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*items++ = TUnboxedValuePod{element}; | ||
} | ||
|
||
return res.Release(); | ||
} | ||
|
||
SIMPLE_STRICT_UDF(TToBinaryString, char*(TListType<double>)) { | ||
return valueBuilder->NewString(SerializeDoubleVector(args[0])); | ||
} | ||
|
||
SIMPLE_STRICT_UDF(TFromBinaryString, TOptional<TListType<double>>(const char*)) { | ||
TStringRef str = args[0].AsStringRef(); | ||
if (str.Size() == 0) | ||
return {}; | ||
|
||
const EFormat format = static_cast<EFormat>(str.Data()[0]); | ||
azevaykin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
str = TStringRef{str.Data() + 1, str.Size() -1}; | ||
switch (format) { | ||
case EFormat::DoubleVector: | ||
return DeserializeDoubleVector(valueBuilder, str); | ||
default: | ||
return {}; | ||
} | ||
} | ||
|
||
|
||
std::optional<double> InnerProductDistance(const TUnboxedValuePod vector1, const TUnboxedValuePod vector2) { | ||
double ret = 0; | ||
|
||
const auto elements1 = vector1.GetElements(); | ||
const auto elements2 = vector2.GetElements(); | ||
if (elements1 && elements2) { | ||
const auto size1 = vector1.GetListLength(); | ||
const auto size2 = vector2.GetListLength(); | ||
|
||
if (size1 != size2) | ||
return {}; | ||
|
||
for (ui32 i = 0; i < size1; ++i) { | ||
double element1 = elements1[i].Get<double>(); | ||
double element2 = elements2[i].Get<double>(); | ||
ret += element1 * element2; | ||
} | ||
} else { | ||
TUnboxedValue value1, value2; | ||
const auto it1 = vector1.GetListIterator(); | ||
const auto it2 = vector2.GetListIterator(); | ||
for (; it1.Next(value1) && it2.Next(value2);) { | ||
double element1 = value1.Get<double>(); | ||
double element2 = value2.Get<double>(); | ||
ret += element1 * element2; | ||
} | ||
|
||
// Lenght mismatch | ||
azevaykin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (it1.Next(value1) || it2.Next(value2)) | ||
return {}; | ||
} | ||
azevaykin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return ret; | ||
} | ||
|
||
SIMPLE_STRICT_UDF(TInnerProductDistance, TOptional<double>(TOptional<TListType<double>>, TOptional<TListType<double>>)) { | ||
azevaykin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Y_UNUSED(valueBuilder); | ||
|
||
if (!args[0].HasValue() || !args[1].HasValue()) | ||
return {}; | ||
|
||
auto distance = InnerProductDistance(args[0], args[1]); | ||
if (!distance) | ||
return {}; | ||
|
||
return TUnboxedValuePod{distance.value()}; | ||
} | ||
|
||
SIMPLE_MODULE(TKnnModule, | ||
TFromBinaryString, | ||
TToBinaryString, | ||
TInnerProductDistance | ||
) | ||
|
||
REGISTER_MODULES(TKnnModule) | ||
|
22 changes: 22 additions & 0 deletions
22
ydb/library/yql/udfs/common/knn/test/canondata/result.json
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,22 @@ | ||
{ | ||
"test.test[DeserializationError]": [ | ||
{ | ||
"uri": "file://test.test_DeserializationError_/results.txt" | ||
} | ||
], | ||
"test.test[InnerProductDistance]": [ | ||
{ | ||
"uri": "file://test.test_InnerProductDistance_/results.txt" | ||
} | ||
], | ||
"test.test[LazyListSerialization]": [ | ||
{ | ||
"uri": "file://test.test_LazyListSerialization_/results.txt" | ||
} | ||
], | ||
"test.test[ListSerialization]": [ | ||
{ | ||
"uri": "file://test.test_ListSerialization_/results.txt" | ||
} | ||
] | ||
} |
66 changes: 66 additions & 0 deletions
66
ydb/library/yql/udfs/common/knn/test/canondata/test.test_DeserializationError_/results.txt
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,66 @@ | ||
[ | ||
{ | ||
"Write" = [ | ||
{ | ||
"Type" = [ | ||
"ListType"; | ||
[ | ||
"StructType"; | ||
[ | ||
[ | ||
"column0"; | ||
[ | ||
"OptionalType"; | ||
[ | ||
"ListType"; | ||
[ | ||
"DataType"; | ||
"Double" | ||
] | ||
] | ||
] | ||
] | ||
] | ||
] | ||
]; | ||
"Data" = [ | ||
[ | ||
# | ||
] | ||
] | ||
} | ||
] | ||
}; | ||
{ | ||
"Write" = [ | ||
{ | ||
"Type" = [ | ||
"ListType"; | ||
[ | ||
"StructType"; | ||
[ | ||
[ | ||
"column0"; | ||
[ | ||
"OptionalType"; | ||
[ | ||
"ListType"; | ||
[ | ||
"DataType"; | ||
"Double" | ||
] | ||
] | ||
] | ||
] | ||
] | ||
] | ||
]; | ||
"Data" = [ | ||
[ | ||
# | ||
] | ||
] | ||
} | ||
] | ||
} | ||
] |
153 changes: 153 additions & 0 deletions
153
ydb/library/yql/udfs/common/knn/test/canondata/test.test_InnerProductDistance_/results.txt
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,153 @@ | ||
[ | ||
{ | ||
"Write" = [ | ||
{ | ||
"Type" = [ | ||
"ListType"; | ||
[ | ||
"StructType"; | ||
[ | ||
[ | ||
"column0"; | ||
[ | ||
"OptionalType"; | ||
[ | ||
"DataType"; | ||
"Bool" | ||
] | ||
] | ||
] | ||
] | ||
] | ||
]; | ||
"Data" = [ | ||
[ | ||
[ | ||
%true | ||
] | ||
] | ||
] | ||
} | ||
] | ||
}; | ||
{ | ||
"Write" = [ | ||
{ | ||
"Type" = [ | ||
"ListType"; | ||
[ | ||
"StructType"; | ||
[ | ||
[ | ||
"column0"; | ||
[ | ||
"OptionalType"; | ||
[ | ||
"DataType"; | ||
"Bool" | ||
] | ||
] | ||
] | ||
] | ||
] | ||
]; | ||
"Data" = [ | ||
[ | ||
[ | ||
%true | ||
] | ||
] | ||
] | ||
} | ||
] | ||
}; | ||
{ | ||
"Write" = [ | ||
{ | ||
"Type" = [ | ||
"ListType"; | ||
[ | ||
"StructType"; | ||
[ | ||
[ | ||
"column0"; | ||
[ | ||
"OptionalType"; | ||
[ | ||
"DataType"; | ||
"Bool" | ||
] | ||
] | ||
] | ||
] | ||
] | ||
]; | ||
"Data" = [ | ||
[ | ||
[ | ||
%true | ||
] | ||
] | ||
] | ||
} | ||
] | ||
}; | ||
{ | ||
"Write" = [ | ||
{ | ||
"Type" = [ | ||
"ListType"; | ||
[ | ||
"StructType"; | ||
[ | ||
[ | ||
"column0"; | ||
[ | ||
"OptionalType"; | ||
[ | ||
"DataType"; | ||
"Double" | ||
] | ||
] | ||
] | ||
] | ||
] | ||
]; | ||
"Data" = [ | ||
[ | ||
# | ||
] | ||
] | ||
} | ||
] | ||
}; | ||
{ | ||
"Write" = [ | ||
{ | ||
"Type" = [ | ||
"ListType"; | ||
[ | ||
"StructType"; | ||
[ | ||
[ | ||
"column0"; | ||
[ | ||
"OptionalType"; | ||
[ | ||
"DataType"; | ||
"Double" | ||
] | ||
] | ||
] | ||
] | ||
] | ||
]; | ||
"Data" = [ | ||
[ | ||
# | ||
] | ||
] | ||
} | ||
] | ||
} | ||
] |
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.
Uh oh!
There was an error while loading. Please reload this page.