AscendIndexSQ
The small library algorithm AscendIndexSQ can train and generate a proper quantization function based on a group of data. For the input float32 feature vectors, AscendIndexSQ quantizes them into int8 feature vectors and stores them on the device to further compress the storage space. During vector comparison, AscendIndexSQ dequantizes the int8 feature vectors to the original feature vectors for subsequent calculation. The following is a typical AscendIndexSQ sample:
#include <faiss/ascend/AscendIndexSQ.h>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
const size_t dim = 512;
const size_t ntotal = 10000;
vector<float> data(dim * ntotal);
for (size_t i = 0; i < data.size(); i++) {
data[i] = drand48();
}
const size_t k = 100;
const size_t searchNum = 100;
vector<float> dist(k * searchNum);
vector<long> indices(k * searchNum);
cout << "Search data set successfull." << endl;
faiss::ascend::AscendIndexSQ *index = nullptr;
try {
faiss::ascend::AscendIndexSQConfig chipConf{0};
index = new faiss::ascend::AscendIndexSQ(dim, faiss::ScalarQuantizer::QuantizerType::QT_8bit, faiss::METRIC_L2, chipConf);
index->train(ntotal, data.data());
index->add(ntotal, data.data());
index->search(searchNum, data.data(), k, dist.data(), indices.data());
} catch (...) {
cout << "Exception caught!" << endl;
delete index;
return -1;
}
delete index;
cout << "Search finished successfully" << endl;
return 0;
}
Parent topic: Code Reference