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

Nmahapatro/allow result less candidates #505

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions include/parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,12 @@ enum State : uint8_t
template <typename LabelT = uint32_t> class IndexSearchContext
{
public:
IndexSearchContext(uint32_t time_limit_in_microseconds = 0u, uint32_t io_limit = UINT32_MAX)
: _time_limit_in_microseconds(time_limit_in_microseconds), _io_limit(io_limit), _result_state(State::Unknown)
IndexSearchContext(uint32_t time_limit_in_microseconds = 0u, uint32_t io_limit = UINT32_MAX, bool allowLessThanKResults = false)
: _time_limit_in_microseconds(time_limit_in_microseconds), _io_limit(io_limit), _result_state(State::Unknown), _allowLessThankResults(allowLessThanKResults)
{
_use_filter = false;
_label = (LabelT)0;
_total_result_returned = 0;
}

void SetLabel(LabelT label, bool use_filter)
Expand All @@ -157,6 +158,16 @@ template <typename LabelT = uint32_t> class IndexSearchContext
_result_state = state;
}

void UpdateResultReturned(size_t result_returned)
{
_total_result_returned = result_returned;
}

size_t GetResultCount()
{
return _total_result_returned;
}

State GetState() const
{
return _result_state;
Expand Down Expand Up @@ -198,6 +209,11 @@ template <typename LabelT = uint32_t> class IndexSearchContext
return _stats;
}

bool GetAllowLessThanKResults()
{
return _allowLessThankResults;
}

private:
uint32_t _time_limit_in_microseconds;
uint32_t _io_limit;
Expand All @@ -206,6 +222,8 @@ template <typename LabelT = uint32_t> class IndexSearchContext
LabelT _label;
Timer _timer;
QueryStats _stats;
bool _allowLessThankResults;
size_t _total_result_returned;
};

} // namespace diskann
20 changes: 16 additions & 4 deletions src/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2251,14 +2251,20 @@ std::pair<uint32_t, uint32_t> Index<T, TagT, LabelT>::search(const T *query, con
break;
}

if (pos < K)
if (pos < K && context.GetAllowLessThanKResults())
{
context.SetState(State::Success);
context.UpdateResultReturned(pos);
}
else if(pos < K)
{
context.SetState(State::Failure);
diskann::cerr << "Found pos: " << pos << "fewer than K elements " << K << " for query" << std::endl;
context.UpdateResultReturned(pos);
}
else
{
context.SetState(State::Success);
context.UpdateResultReturned(K);
}

return retval;
Expand Down Expand Up @@ -2387,14 +2393,20 @@ std::pair<uint32_t, uint32_t> Index<T, TagT, LabelT>::search_with_filters(const
if (pos == K)
break;
}
if (pos < K)
if (pos < K && context.GetAllowLessThanKResults())
{
context.SetState(State::Success);
context.UpdateResultReturned(pos);
}
else if(pos < K)
{
context.SetState(State::Failure);
diskann::cerr << "Found fewer than K elements for query" << std::endl;
context.UpdateResultReturned(pos);
}
else
{
context.SetState(State::Success);
context.UpdateResultReturned(K);
}

return retval;
Expand Down
1 change: 1 addition & 0 deletions src/pq_flash_index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,7 @@ void PQFlashIndex<T, LabelT>::cached_beam_search(const T *query1, const uint64_t
}

context.SetState(State::Success);
context.UpdateResultReturned(k_search);
}

// range search returns results of all neighbors within distance of range.
Expand Down
Loading