代码拉取完成,页面将自动刷新
/*
* Copyright(C) 2023. Huawei Technologies Co.,Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// 需要生成aicpu算子+binary_flat算子(-d 512)
#include <random>
#include <sys/time.h>
#include <gtest/gtest.h>
#include <faiss/ascend/AscendIndexBinaryFlat.h>
#include <faiss/ascend/AscendCloner.h>
#include <faiss/impl/AuxIndexStructures.h>
#include <faiss/Clustering.h>
#include <faiss/index_io.h>
namespace {
const int BITS = 8;
const int SEED = 1;
const int MILLI_SECOND = 1000;
std::independent_bits_engine<std::mt19937, BITS, uint8_t> engine(SEED);
void FeatureGenerator(std::vector<uint8_t> &features)
{
size_t n = features.size();
for (size_t i = 0; i < n; ++i) {
features[i] = engine();
}
}
inline double GetMillisecs()
{
struct timeval tv {
0, 0
};
gettimeofday(&tv, nullptr);
return tv.tv_sec * 1e3 + tv.tv_usec * 1e-3;
}
TEST(TestAscendIndexBinaryFlat, QPS)
{
int dim = 512;
size_t ntotal = 10000000;
std::vector<int> searchNum = { 8, 16, 32, 64, 128, 256 };
faiss::ascend::AscendIndexBinaryFlatConfig conf({ 0 });
faiss::ascend::AscendIndexBinaryFlat index(dim, conf);
index.verbose = true;
printf("generate data\n");
std::vector<uint8_t> base(ntotal * index.code_size, 0);
FeatureGenerator(base);
printf("add data\n");
index.add(ntotal, base.data());
int warmUpTimes = 10 ;
std::vector<int32_t> distw(127 * 10, 0);
std::vector<faiss::idx_t> labelw(127 * 10, 0);
for (int i = 0; i < warmUpTimes; i++) {
index.search(127, base.data(), 10, distw.data(), labelw.data());
}
for (size_t n = 0; n < searchNum.size(); n++) {
int k = 128;
int loopTimes = 10;
std::vector<int32_t> dist(searchNum[n] * k, 0);
std::vector<faiss::idx_t> label(searchNum[n] * k, 0);
double ts = GetMillisecs();
for (int l = 0; l < loopTimes; l++) {
index.search(searchNum[n], base.data(), k, dist.data(), label.data());
}
double te = GetMillisecs();
printf("case[%zu]: base:%zu, dim:%d, search num:%d, QPS:%.4f\n", n, ntotal, dim, searchNum[n],
MILLI_SECOND * searchNum[n] * loopTimes / (te - ts));
}
}
} // namespace
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。