首页 > 编程语言 >C++ AI异构搜索

C++ AI异构搜索

时间:2024-07-10 19:54:49浏览次数:8  
标签:异构 index AI nb ++ C++ int printf include

GitHub - facebookresearch/faiss: A library for efficient similarity search and clustering of dense vectors.


#include <faiss/utils/simdlib.h>

#include <cstddef>
#include <cstdint>
#include <memory>
#include <random>
#include <vector>

#include <omp.h>

#include <faiss/IndexFlat.h>
#include <faiss/IndexIVFPQFastScan.h>
#include <faiss/impl/AuxIndexStructures.h>

#include <faiss/IndexFlat.h>
//#include <faiss/gpu/GpuIndexFlat.h>
//#include <faiss/gpu/GpuIndexIVFFlat.h>
//#include <faiss/gpu/StandardGpuResources.h>


using namespace faiss;


void testCmpltAndBlendInplace() {
    simd8float32 lowestValues(0, 1, 2, 3, 4, 5, 6, 7);
    simd8uint32 lowestIndices(0, 1, 2, 3, 4, 5, 6, 7);

    simd8float32 candidateValues0(5, 5, 5, 5, 5, 5, 5, 5);
    simd8uint32 candidateIndices0(10, 11, 12, 13, 14, 15, 16, 17);
    cmplt_and_blend_inplace(
        candidateValues0, candidateIndices0, lowestValues, lowestIndices);

    simd8float32 candidateValues1(6, 6, 6, 6, 6, 6, 6, 6);
    simd8uint32 candidateIndices1(20, 21, 22, 23, 24, 25, 26, 27);
    cmplt_and_blend_inplace(
        candidateValues1, candidateIndices1, lowestValues, lowestIndices);

    simd8float32 candidateValues2(0, 1, 2, 3, 4, 5, 5, 5);
    simd8uint32 candidateIndices2(30, 31, 32, 33, 34, 35, 36, 37);
    cmplt_and_blend_inplace(
        candidateValues2, candidateIndices2, lowestValues, lowestIndices);

    simd8float32 expectedValues(0, 1, 2, 3, 4, 5, 5, 5);
    simd8uint32 expectedIndices(0, 1, 2, 3, 4, 5, 16, 17);
    //ASSERT_TRUE(lowestValues.is_same_as(expectedValues));
    //ASSERT_TRUE(lowestIndices.is_same_as(expectedIndices));
}

void testCmpltMinMaxFloat() {
    simd8float32 minValues(0, 0, 0, 0, 0, 0, 0, 0);
    simd8uint32 minIndices(0, 0, 0, 0, 0, 0, 0, 0);
    simd8float32 maxValues(0, 0, 0, 0, 0, 0, 0, 0);
    simd8uint32 maxIndices(0, 0, 0, 0, 0, 0, 0, 0);

    simd8float32 candidateValues0(5, 5, 5, 5, 5, 5, 5, 5);
    simd8uint32 candidateIndices0(10, 11, 12, 13, 14, 15, 16, 17);
    simd8float32 currentValues0(0, 1, 2, 3, 4, 5, 6, 7);
    simd8uint32 currentIndices0(0, 1, 2, 3, 4, 5, 6, 7);

    cmplt_min_max_fast(
        candidateValues0,
        candidateIndices0,
        currentValues0,
        currentIndices0,
        minValues,
        minIndices,
        maxValues,
        maxIndices);

    simd8float32 expectedMinValues(0, 1, 2, 3, 4, 5, 5, 5);
    simd8uint32 expectedMinIndices(0, 1, 2, 3, 4, 5, 16, 17);
    //ASSERT_TRUE(minValues.is_same_as(expectedMinValues));
    //ASSERT_TRUE(minIndices.is_same_as(expectedMinIndices));

    simd8float32 expectedMaxValues(5, 5, 5, 5, 5, 5, 6, 7);
    // the result is not 10,11,12,13,14,5,6,7 because it is _fast version
    simd8uint32 expectedMaxIndices(10, 11, 12, 13, 14, 15, 6, 7);
    //ASSERT_TRUE(maxValues.is_same_as(expectedMaxValues));
    //ASSERT_TRUE(maxIndices.is_same_as(expectedMaxIndices));
}

void testSearch() {
    // small vectors and database
    int d = 64;
    size_t nb = 4000;

    // ivf centroids
    size_t nlist = 4;

    // more than 2 threads to surface
    // problems related to multi-threading
    omp_set_num_threads(8);

    // random database, also used as queries
    std::vector<float> database(nb * d);
    std::mt19937 rng;
    std::uniform_real_distribution<> distrib;
    for (size_t i = 0; i < nb * d; i++) {
        database[i] = distrib(rng);
    }

    // build index
    faiss::IndexFlatL2 coarse_quantizer(d);
    faiss::IndexIVFPQFastScan index(
        &coarse_quantizer, d, nlist, d / 2, 4, faiss::METRIC_L2, 32);
    index.pq.cp.niter = 10; // speed up train
    index.nprobe = nlist;
    index.train(nb, database.data());
    index.add(nb, database.data());

    std::vector<float> distances(nb);
    std::vector<faiss::idx_t> labels(nb);
    auto t = std::chrono::high_resolution_clock::now();
    int k = 1;
    index.nprobe = k;
    index.search(nb, database.data(), k, distances.data(), labels.data());
    auto knn_time = std::chrono::high_resolution_clock::now() - t;

    int n_ok = 0;
    for (int q = 0; q < nb; q++) {
        for (int i = 0; i < k; i++)
            if (database[q * k + i] == distances[q])
                n_ok++;
    }
    //EXPECT_GT(n_ok, nb * 0.4);
    //faiss::RangeSearchResult rsr(nb);
    //t = std::chrono::high_resolution_clock::now();
    //index.range_search(nb, database.data(), 1.0, &rsr);
    //auto range_time = std::chrono::high_resolution_clock::now() - t;

     we expect the perf of knn and range search
     to be similar, at least within a factor of 4
    //ASSERT_LE(range_time, knn_time * 4);
    //ASSERT_LE(knn_time, range_time * 4);
}

/*
void testGPU() {
    int d = 64;      // dimension
    int nb = 100000; // database size
    int nq = 10000;  // nb of queries

    std::mt19937 rng;
    std::uniform_real_distribution<> distrib;

    float* xb = new float[d * nb];
    float* xq = new float[d * nq];

    for (int i = 0; i < nb; i++) {
        for (int j = 0; j < d; j++)
            xb[d * i + j] = distrib(rng);
        xb[d * i] += i / 1000.;
    }

    for (int i = 0; i < nq; i++) {
        for (int j = 0; j < d; j++)
            xq[d * i + j] = distrib(rng);
        xq[d * i] += i / 1000.;
    }

    faiss::gpu::StandardGpuResources res;

    // Using a flat index

    faiss::gpu::GpuIndexFlatL2 index_flat(&res, d);

    printf("is_trained = %s\n", index_flat.is_trained ? "true" : "false");
    index_flat.add(nb, xb); // add vectors to the index
    printf("ntotal = %ld\n", index_flat.ntotal);

    int k = 4;

    { // search xq
        long* I = new long[k * nq];
        float* D = new float[k * nq];

        index_flat.search(nq, xq, k, D, I);

        // print results
        printf("I (5 first results)=\n");
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < k; j++)
                printf("%5ld ", I[i * k + j]);
            printf("\n");
        }

        printf("I (5 last results)=\n");
        for (int i = nq - 5; i < nq; i++) {
            for (int j = 0; j < k; j++)
                printf("%5ld ", I[i * k + j]);
            printf("\n");
        }

        delete[] I;
        delete[] D;
    }

    // Using an IVF index

    int nlist = 100;
    faiss::gpu::GpuIndexIVFFlat index_ivf(&res, d, nlist, faiss::METRIC_L2);

    assert(!index_ivf.is_trained);
    index_ivf.train(nb, xb);
    assert(index_ivf.is_trained);
    index_ivf.add(nb, xb); // add vectors to the index

    printf("is_trained = %s\n", index_ivf.is_trained ? "true" : "false");
    printf("ntotal = %ld\n", index_ivf.ntotal);

    { // search xq
        long* I = new long[k * nq];
        float* D = new float[k * nq];

        index_ivf.search(nq, xq, k, D, I);

        // print results
        printf("I (5 first results)=\n");
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < k; j++)
                printf("%5ld ", I[i * k + j]);
            printf("\n");
        }

        printf("I (5 last results)=\n");
        for (int i = nq - 5; i < nq; i++) {
            for (int j = 0; j < k; j++)
                printf("%5ld ", I[i * k + j]);
            printf("\n");
        }

        delete[] I;
        delete[] D;
    }

    delete[] xb;
    delete[] xq;
}
*/

void test() {
    testCmpltAndBlendInplace();
    testCmpltMinMaxFloat();
    testSearch();
    //testGPU():
}


创作不易,小小的支持一下吧!

标签:异构,index,AI,nb,++,C++,int,printf,include
From: https://blog.csdn.net/qq_30220519/article/details/140332918

相关文章

  • 【专题】2024年国产AI大模型应用报告合集PDF分享(附原数据表)
    原文链接:tecdat.cn/?p=36958原文出处:拓端数据部落公众号进入21世纪初期,随着计算能力飞跃与大数据浪潮的席卷,AI大模型技术经历了从无到有的蜕变,从纯学术构想迅速转化为实际应用,其复杂性与功能性均实现了质的飞跃。特别是自2022年11月OpenAI推出ChatGPT以来,大模型技术正式步入公......
  • C++反射的实现方式
    在C++中,反射(reflection)通常是指在运行时检查或修改程序结构的能力,比如类型、对象、方法、属性等。与许多动态语言(如Python、JavaScript)不同,C++是一种静态类型的编译语言,缺乏内置的反射机制。不过,我们可以使用一些技巧和库来实现类似反射的功能。 1.使用RTTI(运行时类型信息)......
  • 【ROS2】中级-编写动作服务器和客户端(C++)
    目标:用C++实现一个动作服务器和客户端。教程级别:中级 时间:15分钟 目录 背景 先决条件 任务1.创建custom_action_cpp包2.编写动作服务器3.编写动作客户端 摘要 相关内容 背景动作是ROS中异步通信的一种形式。动作客户端向动作服务器发送目标请求。动作......
  • C++中各类常用算法的总结以及使用
    1.常用算法文章目录1.常用算法1.常用遍历算法1.for_each2.transform2.常用查找算法1.find2.find_if3.adjacent_find4.binary_search5.count6.count_if3.常用排序算法1.sort2.random_shuffle3.merge4.reverse4.常用拷贝和替换算法1.copy2.replace3.repla......
  • 无人直播/ai自动直播-APP源码开发
    无人直播APP的源码开发通常涉及到几个关键技术和组件:实时流媒体处理:使用WebRTC(WebReal-TimeCommunication)技术,它允许在浏览器或移动端实现实时音视频通信。开发者需要理解和集成相关的SDK,如Agora、Kurento等。用户认证和权限管理:通过OAuth或其他身份验证机制,确保只有注册......
  • 模拟增益(Analog Gain)、数字增益(Digital Gain)
    在WebRTC中,模拟增益和数字增益是两种增强音频信号的技术,它们在确保通话质量中扮演着重要角色。下面我将详细解释这两种增益的概念及其作用。模拟增益(AnalogGain)模拟增益是在模拟信号处理阶段调整信号强度的过程。模拟增益通常在音频信号被转换为数字信号之前,在麦克风放大器级别......
  • 伙伴活动|AI硬件大潮来袭,深圳的创客们在哪里?
    「每一种硬件产品,都会被GenAI重新做一遍。」 分享一个社区伙伴「未来光锥」参与主办的活动。如果你同时对AI和硬件感兴趣,提到maker一词仍然会激动。推荐你参与这次活动。 AI玩具Folotoy的创始人、RTE开发者社区成员王乐也将参与本次活动并分享。 也推荐你收听王......
  • 【C++】14.多态
    一、多态的概念在编程与现实的映射中就是,不同的对象完成相同的行为而产生的不同状态。举个栗子:比如买票这个行为,当普通人买票时,是全价买票;学生买票时,是半价买票;军人买票时是优先买票。再举个栗子:动物的叫声,猫的叫声是“喵喵”;狗的叫声是“汪汪”;老虎的叫声是“劳资蜀道山......
  • 学习AI大模型,入门小白必看!应用开发极简入门PDF来了!
    人工智能大潮已来,不加入就可能被淘汰。就好像现在职场里谁不会用PPT和excel一样,基本上你见不到。而大模型是人工智能代表,潜力与使用方式有关。使用好大模型可提高效率,让人获得更好的待遇和更多机会。你发现PPT和excel用的好的PPT一看就惊艳,excel用的特别熟练,你这个数据分......
  • 【NOI】C++算法设计入门之贪心
    文章目录前言一、概念1.导入2.概念2.1贪心算法的核心思想2.2贪心算法的步骤2.3贪心算法的应用场景二、例题讲解问题:1372.活动选择问题:1456.淘淘捡西瓜问题:1551-任务调度问题:1561.买木头三、总结五、感谢前言贪心算法,如同成语"得陇望蜀"所描述的那样,总是......