zoukankan      html  css  js  c++  java
  • Faiss学习:一

    在多个GPU上运行Faiss以及性能测试

    一、Faiss的基本使用

    1.1在CPU上运行

    Faiss的所有算法都是围绕index展开的。不管运行搜索还是聚类,首先都要建立一个index。

    import faiss
    # make faiss available
    index = faiss.IndexFlatL2(d)
    # build the index
    # d is the dimension of data
    

    在运行上述代码后,就可以添加数据并运行搜索了。

    index.add(xb)
    # xb is the base data
    D, I = index.search(xq, k)
    # xq is the query data
    # k is the num of neigbors you want to search
    # D is the distance matrix between xq and k neigbors
    # I is the index matrix of k neigbors
    

    1.2在单个GPU上运行

    在单个GPU上运行的语法基本与在GPU上运行类似。但是需要申明一个GPU资源的标识.

    res = faiss.StandardGpuResources()
    # we need only a StandardGpuResources per GPU
    flat_config = 0
    # flat_config is an ID. if you have 3 GPUs, flat_configs maybe 0, 1, 2
    index = faiss.GpuIndexFlatL2(res, d, flat_config)
    # build the index
    index.add(xb)
    D, I = index.search(xq, k)
    

    1.3在多个GPU上运行

    在多个GPU上运行时便有所不同, 我们需要将数据集分割给多个GPU以完成并行搜索。

    在Faiss中提供了两种方法实现:IndexProxy和IndexShards。

    下面着重介绍IndexProxy。

    res = [faiss.StandardGpuResources() for i in range(ngpu)]
    # first we get StandardGpuResources of each GPU
    # ngpu is the num of GPUs
    
    indexes = [faiss.GpuIndexFlatL2(res[i], i, d, useFloat16)
               for i in range(ngpu)]
    # then we make an Index array
    # useFloat16 is a boolean value
    
    index = faiss.IndexProxy()
    for sub_index in indexes:
        index.addIndex(sub_index)
    # build the index by IndexProxy
    

    二、kmeans测试

     
    1.jpg

    如图所示数据为1M个,中心点为1K个。

    在不同数据维度以及GPU数目下迭代20次所需要的时间。

    三、暴力搜索测试

    数据集为sift1M, 该数据集共1M个,128维。(运行在两个K40M GPU上)

     
    2.jpg
     
    3.jpg

    可以看到在每次查询10K个数据的1024个最近邻居时平均每个查询只需360ns。当需要查询的邻居数下降时,查询时间能够降至100ns。

    四、IVFPQ搜索测试

    数据集同上,运行环境同上。

    基本参数:

    numCentroids=4096
    numQuantizers=64
    

    首先我们测试nprob对性能的影响

     
    4.jpg

    当nprob上升时, 每次查询时间会增加, 同时查询的准确度也会上升。但上升到一定程度上升幅度便会迅速变小。我们取准确度的拐点值nprob=32进行下一步测试。

    接下来我们测试查询的邻居数即k值对性能的影响。

     
    5.jpg

    可以看到查询时间不再是线性增长了。也就意味着对于IVFPQ邻居数不宜太多。

    选定k=32进行下一步测试。

     
    6.jpg

    如图所示,随着每次查询的数量上升,平均查询时间先变小在变大,这可能是由于数据量小时开销比较大导致平均查询时间较大。可以看到随着查询的数量上升, 平均查询时间上升但上升幅度放缓,估计会在250ns左右稳定。

     


    作者:momo猪
    链接:https://www.jianshu.com/p/4fb45b9070d1
    來源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    exgcd模板
    jnYaVpqwsq
    Http状态码完整说明
    字符串的常用方法
    MVC4.0 Controller和View重复加载
    ASP.NET MVC4中的Model验证 移除指定验证信息
    枚举类型定义字符串
    MVC4.0系统开发新手历程(四)数据列表查询
    MVC4.0系统开发新手历程(三)---数据导入计算
    MVC4.0系统开发新手历程(二)
  • 原文地址:https://www.cnblogs.com/DjangoBlog/p/8615540.html
Copyright © 2011-2022 走看看