Skip to content

Commit db63ddd

Browse files
committed
result: store vector samples when requested
Signed-off-by: Vicent Marti <vmg@strn.cat>
1 parent de24c6a commit db63ddd

7 files changed

Lines changed: 38 additions & 16 deletions

File tree

AnnService/inc/Core/Common/QueryResultSet.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,29 @@ class QueryResultSet : public QueryResult
7474
return m_results[0].Dist;
7575
}
7676

77-
bool AddPoint(const SizeType index, float dist)
77+
bool AddPoint(const SizeType index, float dist, const void *sample, SizeType sample_length)
7878
{
7979
if (dist < m_results[0].Dist || (dist == m_results[0].Dist && index < m_results[0].VID))
8080
{
8181
m_results[0].VID = index;
8282
m_results[0].Dist = dist;
83+
84+
if (m_withSamples && sample) {
85+
m_results[0].Sample = ByteArray::Alloc(sample_length);
86+
memcpy(m_results[0].Sample.Data(), sample, sample_length);
87+
}
88+
8389
Heapify(m_resultNum);
8490
return true;
8591
}
8692
return false;
8793
}
8894

95+
bool AddPoint(const SizeType index, float dist)
96+
{
97+
return AddPoint(index, dist, nullptr, 0);
98+
}
99+
89100
inline void SortResult()
90101
{
91102
for (int i = m_resultNum - 1; i >= 0; i--)

AnnService/inc/Core/SPANN/ExtraDynamicSearcher.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,14 +1245,17 @@ namespace SPTAG::SPANN {
12451245
SearchStats* p_stats, std::set<int>* truth, std::map<int, std::set<int>>* found) override
12461246
{
12471247
auto exStart = std::chrono::high_resolution_clock::now();
1248+
std::chrono::microseconds remainLimit = std::chrono::microseconds::max();
12481249

12491250
// const auto postingListCount = static_cast<uint32_t>(p_exWorkSpace->m_postingIDs.size());
12501251

12511252
p_exWorkSpace->m_deduper.clear();
12521253

1253-
auto exSetUpEnd = std::chrono::high_resolution_clock::now();
1254-
1255-
p_stats->m_exSetUpLatency = ((double)std::chrono::duration_cast<std::chrono::microseconds>(exSetUpEnd - exStart).count()) / 1000;
1254+
if (p_stats) {
1255+
auto exSetUpEnd = std::chrono::high_resolution_clock::now();
1256+
p_stats->m_exSetUpLatency = ((double)std::chrono::duration_cast<std::chrono::microseconds>(exSetUpEnd - exStart).count()) / 1000;
1257+
remainLimit = m_hardLatencyLimit - std::chrono::microseconds((int)p_stats->m_totalLatency);
1258+
}
12561259

12571260
COMMON::QueryResultSet<ValueType>& queryResults = *((COMMON::QueryResultSet<ValueType>*) & p_queryResults);
12581261

@@ -1265,8 +1268,6 @@ namespace SPTAG::SPANN {
12651268

12661269
std::vector<std::string> postingLists;
12671270

1268-
std::chrono::microseconds remainLimit = m_hardLatencyLimit - std::chrono::microseconds((int)p_stats->m_totalLatency);
1269-
12701271
auto readStart = std::chrono::high_resolution_clock::now();
12711272
db->MultiGet(p_exWorkSpace->m_postingIDs, &postingLists, remainLimit);
12721273
auto readEnd = std::chrono::high_resolution_clock::now();
@@ -1302,7 +1303,7 @@ namespace SPTAG::SPANN {
13021303
continue;
13031304
}
13041305
auto distance2leaf = p_index->ComputeDistance(queryResults.GetQuantizedTarget(), vectorInfo + m_metaDataSize);
1305-
queryResults.AddPoint(vectorID, distance2leaf);
1306+
queryResults.AddPoint(vectorID, distance2leaf, vectorInfo + m_metaDataSize, m_vectorInfoSize - m_metaDataSize);
13061307
}
13071308
auto compEnd = std::chrono::high_resolution_clock::now();
13081309
if (realNum <= m_mergeThreshold && !m_opt->m_inPlace) MergeAsync(p_index.get(), curPostingID);

AnnService/inc/Core/SPANN/ExtraStaticSearcher.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ namespace SPTAG
126126
if (p_exWorkSpace->m_deduper.CheckAndSet(vectorID)) continue; \
127127
(this->*m_parseEncoding)(p_index, listInfo, (ValueType*)(p_postingListFullData + offsetVector));\
128128
auto distance2leaf = p_index->ComputeDistance(queryResults.GetQuantizedTarget(), p_postingListFullData + offsetVector); \
129-
queryResults.AddPoint(vectorID, distance2leaf); \
129+
queryResults.AddPoint(vectorID, distance2leaf, p_postingListFullData + offsetVector, p_index->GetFeatureDim() * sizeof(ValueType)); \
130130
} \
131131

132132
#define ProcessPostingOffset() \
@@ -138,7 +138,7 @@ namespace SPTAG
138138
if (p_exWorkSpace->m_deduper.CheckAndSet(vectorID)) continue; \
139139
(this->*m_parseEncoding)(p_index, listInfo, (ValueType*)(p_postingListFullData + offsetVector));\
140140
auto distance2leaf = p_index->ComputeDistance(queryResults.GetQuantizedTarget(), p_postingListFullData + offsetVector); \
141-
queryResults.AddPoint(vectorID, distance2leaf); \
141+
queryResults.AddPoint(vectorID, distance2leaf, p_postingListFullData + offsetVector, p_index->GetFeatureDim() * sizeof(ValueType)); \
142142
foundResult = true;\
143143
break;\
144144
} \

AnnService/inc/Core/SearchQuery.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class QueryResult
2323
m_quantizedTarget(nullptr),
2424
m_quantizedSize(0),
2525
m_resultNum(0),
26-
m_withMeta(false)
26+
m_withMeta(false),
27+
m_withSamples(false)
2728
{
2829
}
2930

@@ -32,9 +33,9 @@ class QueryResult
3233
Init(nullptr, p_resultNum, true);
3334
}
3435

35-
QueryResult(const void* p_target, int p_resultNum, bool p_withMeta)
36+
QueryResult(const void* p_target, int p_resultNum, bool p_withMeta = false, bool p_withSamples = false)
3637
{
37-
Init(p_target, p_resultNum, p_withMeta);
38+
Init(p_target, p_resultNum, p_withMeta, p_withSamples);
3839
}
3940

4041

@@ -93,11 +94,12 @@ class QueryResult
9394
}
9495

9596

96-
inline void Init(const void* p_target, int p_resultNum, bool p_withMeta)
97+
inline void Init(const void* p_target, int p_resultNum, bool p_withMeta = false, bool p_withSamples = false)
9798
{
9899
m_target = p_target;
99100
m_resultNum = p_resultNum;
100101
m_withMeta = p_withMeta;
102+
m_withSamples = p_withSamples;
101103
m_quantizedTarget = (void*)p_target;
102104
m_quantizedSize = 0;
103105

@@ -177,6 +179,11 @@ class QueryResult
177179
return m_results.Data();
178180
}
179181

182+
inline bool WithSamples() const
183+
{
184+
return m_withSamples;
185+
}
186+
180187

181188
inline bool WithMeta() const
182189
{
@@ -211,6 +218,7 @@ class QueryResult
211218
m_results[i].VID = -1;
212219
m_results[i].Dist = MaxDist;
213220
m_results[i].Meta.Clear();
221+
m_results[i].Sample.Clear();
214222
}
215223
}
216224

@@ -249,6 +257,7 @@ class QueryResult
249257
int m_resultNum;
250258

251259
bool m_withMeta;
260+
bool m_withSamples;
252261

253262
Array<BasicResult> m_results;
254263
};

AnnService/inc/Core/SearchResult.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ namespace SPTAG
6767
SizeType VID;
6868
float Dist;
6969
ByteArray Meta;
70+
ByteArray Sample;
7071
bool RelaxedMono;
7172

7273
BasicResult() : VID(-1), Dist(MaxDist), RelaxedMono(false) {}

AnnService/src/Core/BKT/BKTIndex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ namespace SPTAG
316316
{
317317
if (checkFilter(m_pMetadata, tmpNode, filterFunc))
318318
{
319-
p_query.AddPoint(tmpNode, gnode.distance);
319+
p_query.AddPoint(tmpNode, gnode.distance, (m_pSamples)[tmpNode], GetFeatureDim() * sizeof(T));
320320
}
321321
}
322322
}
@@ -379,7 +379,7 @@ namespace SPTAG
379379
}
380380
if (notDeleted(m_deletedID, tmpNode))
381381
{
382-
p_query.AddPoint(tmpNode, gnode.distance);
382+
p_query.AddPoint(tmpNode, gnode.distance, (m_pSamples)[tmpNode], GetFeatureDim() * sizeof(T));
383383
count++;
384384
if (gnode.distance > p_space.m_Results.worst() ||
385385
p_space.m_iNumberOfCheckedLeaves > p_space.m_iMaxCheck) {

AnnService/src/Core/KDT/KDTIndex.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ namespace SPTAG
200200

201201
if (notDeleted(m_deletedID, gnode.node))
202202
{
203-
if (!p_query.AddPoint(gnode.node, gnode.distance) && p_space.m_iNumberOfCheckedLeaves > p_space.m_iMaxCheck)
203+
if (!p_query.AddPoint(gnode.node, gnode.distance, (m_pSamples)[gnode.node], GetFeatureDim() * sizeof(T)) && p_space.m_iNumberOfCheckedLeaves > p_space.m_iMaxCheck)
204204
{
205205
p_query.SortResult();
206206
return;

0 commit comments

Comments
 (0)