utils.cpp 2.9 KB
Newer Older
J
jinhai 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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.

X
MS-154  
xj.lin 已提交
18

X
xiaojun.lin 已提交
19
#include <gtest/gtest.h>
X
MS-154  
xj.lin 已提交
20 21
#include <faiss/IndexFlat.h>

S
starlord 已提交
22
#include "wrapper/utils.h"
X
MS-154  
xj.lin 已提交
23

S
starlord 已提交
24 25
void
DataGenBase::GenData(const int &dim, const int &nb, const int &nq,
S
starlord 已提交
26 27
                     float *xb, float *xq, int64_t *ids,
                     const int &k, int64_t *gt_ids, float *gt_dis) {
X
MS-154  
xj.lin 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    for (auto i = 0; i < nb; ++i) {
        for (auto j = 0; j < dim; ++j) {
            //p_data[i * d + j] = float(base + i);
            xb[i * dim + j] = drand48();
        }
        xb[dim * i] += i / 1000.;
        ids[i] = i;
    }
    for (size_t i = 0; i < nq * dim; ++i) {
        xq[i] = xb[i];
    }

    faiss::IndexFlatL2 index(dim);
    //index.add_with_ids(nb, xb, ids);
    index.add(nb, xb);
X
xj.lin 已提交
43
    index.search(nq, xq, k, gt_dis, gt_ids);
X
MS-154  
xj.lin 已提交
44 45
}

S
starlord 已提交
46 47 48 49 50 51
void
DataGenBase::GenData(const int &dim,
                     const int &nb,
                     const int &nq,
                     std::vector<float> &xb,
                     std::vector<float> &xq,
S
starlord 已提交
52
                     std::vector<int64_t> &ids,
S
starlord 已提交
53
                     const int &k,
S
starlord 已提交
54
                     std::vector<int64_t> &gt_ids,
S
starlord 已提交
55
                     std::vector<float> &gt_dis) {
X
MS-154  
xj.lin 已提交
56 57 58 59
    xb.resize(nb * dim);
    xq.resize(nq * dim);
    ids.resize(nb);
    gt_ids.resize(nq * k);
X
xj.lin 已提交
60 61
    gt_dis.resize(nq * k);
    GenData(dim, nb, nq, xb.data(), xq.data(), ids.data(), k, gt_ids.data(), gt_dis.data());
X
MS-154  
xj.lin 已提交
62
}
X
xiaojun.lin 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

void
DataGenBase::AssertResult(const std::vector<int64_t>& ids, const std::vector<float>& dis) {
        EXPECT_EQ(ids.size(), nq * k);
        EXPECT_EQ(dis.size(), nq * k);

        for (auto i = 0; i < nq; i++) {
            EXPECT_EQ(ids[i * k], gt_ids[i * k]);
            //EXPECT_EQ(dis[i * k], gt_dis[i * k]);
        }

        int match = 0;
        for (int i = 0; i < nq; ++i) {
            for (int j = 0; j < k; ++j) {
                for (int l = 0; l < k; ++l) {
                    if (ids[i * nq + j] == gt_ids[i * nq + l]) match++;
                }
            }
        }

        auto precision = float(match) / (nq * k);
        EXPECT_GT(precision, 0.5);
        std::cout << std::endl << "Precision: " << precision
                  << ", match: " << match
                  << ", total: " << nq * k
                  << std::endl;

}