Skip to content

Commit

Permalink
implement scalar quantization as data type for NGT-graph and QBG
Browse files Browse the repository at this point in the history
  • Loading branch information
masajiro committed Oct 28, 2024
1 parent 1e44fff commit 0416078
Show file tree
Hide file tree
Showing 37 changed files with 7,722 additions and 3,342 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.4
2.3.0
2 changes: 2 additions & 0 deletions bin/ngt/ngt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ main(int argc, char **argv)
}
} catch(NGT::Exception &err) {
cerr << "ngt: Error: " << err.what() << endl;
return 1;
}
return 0;

}
11 changes: 10 additions & 1 deletion bin/qbg/qbg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ main(int argc, char **argv)

QBG::CLI ngt;

ngt.execute(args);
try {
ngt.execute(args);
} catch(NGT::Exception &err) {
cerr << "qbg: Error: " << err.what() << endl;
return 1;
} catch(...) {
cerr << "qbg: Error: " << endl;
return 1;
}
return 0;
}


Expand Down
57 changes: 53 additions & 4 deletions lib/NGT/Capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,18 @@ bool ngt_set_property_object_type_integer(NGTProperty prop, NGTError error) {
return true;
}

bool ngt_set_property_object_type_qsint8(NGTProperty prop, NGTError error) {
if(prop == NULL){
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : parametor error: prop = " << prop;
operate_error_string_(ss, error);
return false;
}

(*static_cast<NGT::Property*>(prop)).objectType = NGT::ObjectSpace::ObjectType::Qsuint8;
return true;
}

bool ngt_set_property_distance_type(NGTProperty prop, NGT::Index::Property::DistanceType type, NGTError error) {
if(prop == NULL){
std::stringstream ss;
Expand Down Expand Up @@ -428,11 +440,7 @@ NGTPropertyInfo ngt_get_property_info(NGTIndex index, NGTError error) {
prop.prefetchSize,
prop.accuracyTable.c_str(),
prop.searchType.c_str(),
#ifdef NGT_INNER_PRODUCT
prop.maxMagnitude,
#else
-1,
#endif
prop.nOfNeighborsForInsertionOrder,
prop.epsilonForInsertionOrder,
#ifdef NGT_REFINEMENT
Expand Down Expand Up @@ -1055,6 +1063,47 @@ ObjectID ngt_append_index_as_float16(NGTIndex index, NGTFloat16 *obj, uint32_t o
}
}

ObjectID ngt_insert_to_refinement_as_float(NGTIndex index, float *obj, uint32_t obj_dim, NGTError error) {
if(index == NULL || obj == NULL || obj_dim == 0){
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : parametor error: index = " << index << " obj = " << obj << " obj_dim = " << obj_dim;
operate_error_string_(ss, error);
return 0;
}

try{
NGT::Index* pindex = static_cast<NGT::Index*>(index);
std::vector<float> vobj(&obj[0], &obj[obj_dim]);
return pindex->insertToRefinement(vobj);
}catch(std::exception &err) {
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : Error: " << err.what();
operate_error_string_(ss, error);
return 0;
}
}

ObjectID ngt_append_to_refinement_as_float(NGTIndex index, float *obj, uint32_t obj_dim, NGTError error) {
if(index == NULL || obj == NULL || obj_dim == 0){
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : parametor error: index = " << index << " obj = " << obj << " obj_dim = " << obj_dim;
operate_error_string_(ss, error);
return 0;
}

try{
NGT::Index* pindex = static_cast<NGT::Index*>(index);
std::vector<float> vobj(&obj[0], &obj[obj_dim]);
return pindex->appendToRefinement(vobj);
}catch(std::exception &err) {
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : Error: " << err.what();
operate_error_string_(ss, error);
return 0;
}
}


bool ngt_batch_append_index(NGTIndex index, float *obj, uint32_t data_count, NGTError error) {
try{
NGT::Index* pindex = static_cast<NGT::Index*>(index);
Expand Down
6 changes: 6 additions & 0 deletions lib/NGT/Capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ bool ngt_set_property_object_type_float16(NGTProperty, NGTError);

bool ngt_set_property_object_type_integer(NGTProperty, NGTError);

bool ngt_set_property_object_type_qsint8(NGTProperty, NGTError);

bool ngt_set_property_distance_type_l1(NGTProperty, NGTError);

bool ngt_set_property_distance_type_l2(NGTProperty, NGTError);
Expand Down Expand Up @@ -272,6 +274,10 @@ ObjectID ngt_insert_index_as_float16(NGTIndex, NGTFloat16*, uint32_t, NGTError);

ObjectID ngt_append_index_as_float16(NGTIndex, NGTFloat16*, uint32_t, NGTError);

ObjectID ngt_append_to_refinement_as_float(NGTIndex, float*, uint32_t, NGTError);

ObjectID ngt_insert_to_refinement_as_float(NGTIndex, float*, uint32_t, NGTError);

bool ngt_batch_append_index(NGTIndex, float*, uint32_t, NGTError);

bool ngt_batch_insert_index(NGTIndex, float*, uint32_t, uint32_t *, NGTError);
Expand Down
22 changes: 19 additions & 3 deletions lib/NGT/Clustering.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ namespace NGT {
}

static void
loadVectors(const std::string &file, std::vector<std::vector<float> > &vectors)
loadVectors(const std::string &file, std::vector<std::vector<float>> &vectors)
{
std::ifstream is(file);
if (!is) {
Expand All @@ -152,7 +152,7 @@ namespace NGT {
}

static void
saveVectors(const std::string &file, std::vector<std::vector<float> > &vectors)
saveVectors(const std::string &file, std::vector<std::vector<float>> &vectors)
{
std::ofstream os(file);
for (auto vit = vectors.begin(); vit != vectors.end(); ++vit) {
Expand All @@ -167,6 +167,22 @@ namespace NGT {
}
}

static void
saveVectors(const std::string &file, std::vector<std::vector<uint32_t>> &vectors)
{
std::ofstream os(file);
for (auto vit = vectors.begin(); vit != vectors.end(); ++vit) {
std::vector<uint32_t> &v = *vit;
for (auto it = v.begin(); it != v.end(); ++it) {
os << (*it);
if (it + 1 != v.end()) {
os << "\t";
}
}
os << std::endl;
}
}

static void
loadVector(const std::string &file, std::vector<size_t> &vectors)
{
Expand Down Expand Up @@ -403,7 +419,7 @@ namespace NGT {
}
}

std::vector<Entry> sortedObjects(vectors.size());
std::vector<Entry> sortedObjects(vectors.size());
#pragma omp parallel for
for (size_t vi = 0; vi < vectors.size(); vi++) {
auto vit = vectors.begin() + vi;
Expand Down
Loading

0 comments on commit 0416078

Please sign in to comment.