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

Implement the interfaces for add, knnsearch, serialize, and deserialize in Pyramid. #230

Closed
wants to merge 17 commits into from
3 changes: 3 additions & 0 deletions examples/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ target_link_libraries (example_diskann vsag_static)
add_executable (simple_hnsw simple_hnsw.cpp)
target_link_libraries (simple_hnsw vsag_static)

add_executable (simple_pyramid simple_pyramid.cpp)
target_link_libraries (simple_pyramid vsag_static)

add_executable (simple_hgraph_sq8 simple_hgraph_sq8.cpp)
target_link_libraries (simple_hgraph_sq8 vsag_static)

Expand Down
22 changes: 12 additions & 10 deletions examples/cpp/simple_pyramid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,26 @@ create_random_string(bool is_full) {

std::random_device rd;
std::mt19937 mt(rd());
std::uniform_int_distribution<> distr;

std::vector<std::string> selected_levels;

if (is_full) {
selected_levels.emplace_back(level1[rand() % level1.size()]);
selected_levels.emplace_back(level2[rand() % level2.size()]);
selected_levels.emplace_back(level3[rand() % level3.size()]);
selected_levels.emplace_back(level1[distr(mt) % level1.size()]);
selected_levels.emplace_back(level2[distr(mt) % level2.size()]);
selected_levels.emplace_back(level3[distr(mt) % level3.size()]);
} else {
std::uniform_int_distribution<int> dist(1, 3);
std::uniform_int_distribution<> dist(1, 3);
int num_levels = dist(mt);

if (num_levels >= 1) {
selected_levels.emplace_back(level1[rand() % level1.size()]);
selected_levels.emplace_back(level1[distr(mt) % level1.size()]);
}
if (num_levels >= 2) {
selected_levels.emplace_back(level2[rand() % level2.size()]);
selected_levels.emplace_back(level2[distr(mt) % level2.size()]);
}
if (num_levels == 3) {
selected_levels.emplace_back(level3[rand() % level3.size()]);
selected_levels.emplace_back(level3[distr(mt) % level3.size()]);
}
}

Expand Down Expand Up @@ -119,11 +120,11 @@ main(int argc, char** argv) {
query_vector[i] = distrib_real(rng);
}
query_path[0] = create_random_string(false);

std::cout << "query_path:" << query_path[0] << std::endl;
// search on the index
auto pyramid_search_parameters = R"(
{
"pyramid": {
"hnsw": {
"ef_search": 100
}
}
Expand All @@ -136,7 +137,8 @@ main(int argc, char** argv) {
// print the results
std::cout << "results: " << std::endl;
for (int64_t i = 0; i < result->GetDim(); ++i) {
std::cout << result->GetIds()[i] << ": " << result->GetDistances()[i] << std::endl;
std::cout << result->GetIds()[i] << ": " << result->GetDistances()[i]
<< " path:" << paths[result->GetIds()[i]] << std::endl;
}

// free memory
Expand Down
3 changes: 3 additions & 0 deletions include/vsag/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern const char* const INDEX_HGRAPH;
extern const char* const INDEX_DISKANN;
extern const char* const INDEX_HNSW;
extern const char* const INDEX_FRESH_HNSW;
extern const char* const INDEX_PYRAMID;
extern const char* const DIM;
extern const char* const NUM_ELEMENTS;
extern const char* const IDS;
Expand Down Expand Up @@ -73,6 +74,8 @@ extern const char* const HNSW_PARAMETER_REVERSED_EDGES;

extern const char* const INDEX_PARAM;

extern const char* const PYRAMID_PARAMETER_SUBINDEX_TYPE;

// statstic key
extern const char* const STATSTIC_MEMORY;
extern const char* const STATSTIC_INDEX_NAME;
Expand Down
3 changes: 3 additions & 0 deletions src/constants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const char* const INDEX_HGRAPH = "hgraph";
const char* const INDEX_DISKANN = "diskann";
const char* const INDEX_HNSW = "hnsw";
const char* const INDEX_FRESH_HNSW = "fresh_hnsw";
const char* const INDEX_PYRAMID = "pyramid";
const char* const DIM = "dim";
const char* const NUM_ELEMENTS = "num_elements";
const char* const IDS = "ids";
Expand Down Expand Up @@ -75,6 +76,8 @@ const char* const HNSW_PARAMETER_REVERSED_EDGES = "use_reversed_edges";

const char* const INDEX_PARAM = "index_param";

const char* const PYRAMID_PARAMETER_SUBINDEX_TYPE = "sub_index_type";

// statstic key
const char* const STATSTIC_MEMORY = "memory";
const char* const STATSTIC_INDEX_NAME = "index_name";
Expand Down
14 changes: 14 additions & 0 deletions src/factory/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "index/hnsw.h"
#include "index/hnsw_zparameters.h"
#include "index/index_common_param.h"
#include "index/pyramid.h"
#include "index/pyramid_zparameters.h"
#include "vsag/vsag.h"

namespace vsag {
Expand Down Expand Up @@ -83,6 +85,18 @@ Factory::CreateIndex(const std::string& origin_name,
std::make_shared<HGraphIndex>(hgraph_param.GetJson(), index_common_params);
hgraph_index->Init();
return hgraph_index;
} else if (name == INDEX_PYRAMID) {
if (allocator == nullptr) {
index_common_params.allocator_ = DefaultAllocator::Instance().get();
}
// read parameters from json, throw exception if not exists
CHECK_ARGUMENT(parsed_params.contains(INDEX_PARAM),
fmt::format("parameters must contains {}", INDEX_PARAM));
auto& pyramid_param_obj = parsed_params[INDEX_PARAM];
auto pyramid_params =
PyramidParameters::FromJson(pyramid_param_obj, index_common_params);
logger::debug("created a pyramid index");
return std::make_shared<Pyramid>(pyramid_params, index_common_params);
} else {
LOG_ERROR_AND_RETURNS(
ErrorType::UNSUPPORTED_INDEX, "failed to create index(unsupported): ", name);
Expand Down
Loading
Loading