IndexILFlat
IndexILFlat为纯Device侧检索方案,利用昇腾AI处理器和AI Core等资源进行各个接口的使能。程序需要在Host侧编译生成二进制文件,然后将二进制文件和相关运行时依赖部署到Device侧。部署请参见下方使用指导,接口使用约束请参考IndexILFlat。
参考用例代码
#include <IndexILFlat.h> #include <iostream> #include <numeric> #include <vector> #include "acl/acl.h" #include "arm_fp16.h" int TestComputeDistance(ascend::IndexILFlat &index, int queryN, int baseSize, float16_t *queryData) { int baseSizeAlign = (baseSize + 15) / 16 * 16; std::vector<float> distances(queryN * baseSizeAlign); auto ret = index.ComputeDistance(queryN, queryData, distances.data()); return ret; } int TestSearchByThreshold(ascend::IndexILFlat &index, int queryN, float16_t *queryData) { int topK = 10; float threshold = 0.6; std::vector<int> num(queryN); std::vector<float> distances(queryN * topK); std::vector<ascend::idx_t> idxs(queryN * topK); auto ret = index.SearchByThreshold(queryN, queryData, threshold, topK, num.data(), idxs.data(), distances.data()); return ret; } int main(int argc, char **argv) { // 0.1 Remember to set device first, please refer to CANN Application // Software Development Guide (C&C++, Inference) aclError aclSet = aclrtSetDevice(0); if (aclSet != 0) { printf("Set device failed ,error code:%d\n", aclSet); return 0; } // 0.2 construct index const int dim = 512; const int baseSize = 100000; const int queryN = 64; const int capacity = 100000; const int resourceSize = -1; auto metricType = ascend::AscendMetricType::ASCEND_METRIC_INNER_PRODUCT; std::vector<float16_t> base(baseSize * dim); std::vector<ascend::idx_t> ids(baseSize); for (size_t j = 0; j < base.size(); j++) { base[j] = drand48(); } std::iota(ids.begin(), ids.end(), 0); // 1. build Index and initialize ascend::IndexILFlat indexFlat; auto ret = indexFlat.Init(dim, capacity, metricType, resourceSize); if (ret != 0) { printf("Index initialize failed ,error code:%d\n", ret); aclrtResetDevice(0); return 0; } // 2. add base vectors ret = indexFlat.AddFeatures(baseSize, base.data(), ids.data()); if (ret != 0) { printf("Add features failed ,error code:%d\n", ret); indexFlat.Finalize(); aclrtResetDevice(0); return 0; } // 3.1 Test ComputeDistance std::vector<float16_t> queries(queryN * dim); for (size_t i = 0; i < queries.size(); i++) { queries[i] = drand48(); } ret = TestComputeDistance(indexFlat, queryN, baseSize, queries.data()); if (ret != 0) { printf("Compute distance failed ,error code:%d\n", ret); indexFlat.Finalize(); aclrtResetDevice(0); return 0; } // 3.2 Test SearchByThreshold ret = TestSearchByThreshold(indexFlat, queryN, queries.data()); if (ret != 0) { printf("Search by threshold failed ,error code:%d\n", ret); indexFlat.Finalize(); aclrtResetDevice(0); return 0; } // 4. release resource indexFlat.Finalize(); aclrtResetDevice(0); printf("------------Demo correct--------------\n"); return 0; }
使用指导
- IndexILFlat发布件,可在mxIndex软件包安装完成后在安装目录中找到。
- 头文件:mxIndex/device/include/IndexILFlat.h
- 动态库:mxIndex/device/lib/libascendfaiss_minios.so
- 代码需要使用CANN内置的HCC编译器(默认CANN安装路径下,编译器路径为“/usr/local/Ascend/ascend-toolkit/latest/toolkit/toolchain/hcc/bin/aarch64-target-linux-gnu-g++”)进行编译,编译完成后部署到Device侧(具体请参见《CANN 软件安装指南 (开放态, Atlas 推理系列产品》的“定制文件系统”章节进行部署)。
如果需通过SSH服务直接拷贝依赖到Device侧或通过SSH登录到Device上直接运行样例,则需要参考《CANN 软件安装指南 (开放态, Atlas 推理系列产品》的“使用DSMI接口打开SSH服务”章节并解除SSH服务的50M内存占用限制,否则无法发送全部依赖文件,用例无法执行。
- 算子om文件生成。
- 编译用例代码。
在mxIndex工程内新建test路径(mxIndex/test),在test路径下创建“IndexILDemo.cpp”源文件,复制参考用例代码,编译命令参考如下。
/usr/local/Ascend/ascend-toolkit/latest/toolkit/toolchain/hcc/bin/aarch64-target-linux-gnu-g++ -fPIC -fPIE -fstack-protector-all -D_FORTIFY_SOURCE=2 -O2 \ -o IndexILDemo IndexILDemo.cpp \ -fopenmp -O3 -frename-registers -fpeel-loops -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack -pie -s \ -I/usr/local/AscendMiniOs/acllib/include/ \ -I../include \ -I../device/include \ -L../device/lib \ -L/usr/local/AscendMiniOs/acllib/lib64/stub \ -L/usr/local/Ascend/driver/lib64/common \ -lascendcl -lascend_hal -lc_sec -lascendfaiss_minios
- 部署依赖。
- 在可执行二进制文件的同级路径下需要配置路径“modelpath”,放置步骤3生成的算子文件。
- CANN的相关库依赖,需要将“/usr/local/AscendMiniOs/aarch64-linux/lib64”部署到Device侧,并配置到“LD_LIBRARY_PATH”。
- 将动态库(mxIndex/device/lib/libascendfaiss_minios.so)部署到Device侧,且并配置到环境变量“LD_LIBRARY_PATH”。
父主题: 代码参考