aboutsummaryrefslogtreecommitdiffstats
path: root/eval/src/tests/ann/nns.h
blob: 484cefd41a90142418eccf29de8c7bfec7257a20 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
91
92
93
94
95
96
97
98
99
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once
#include <vespa/vespalib/util/arrayref.h>
#include "doc_vector_access.h"
#include "nns-l2.h"
#include <memory>

struct SqDist {
    double distance;
    explicit SqDist(double d) noexcept : distance(d) {}
};

struct NnsHit {
    uint32_t docid;
    SqDist sq;
    NnsHit(uint32_t di, SqDist sqD) noexcept
        : docid(di), sq(sqD) {}
};
struct NnsHitComparatorLessDistance {
    bool operator() (const NnsHit &lhs, const NnsHit& rhs) const {
        if (lhs.sq.distance > rhs.sq.distance) return false;
        if (lhs.sq.distance < rhs.sq.distance) return true;
        return (lhs.docid > rhs.docid);
    }
};
struct NnsHitComparatorGreaterDistance {
    bool operator() (const NnsHit &lhs, const NnsHit& rhs) const {
        if (lhs.sq.distance < rhs.sq.distance) return false;
        if (lhs.sq.distance > rhs.sq.distance) return true;
        return (lhs.docid > rhs.docid);
    }
};
struct NnsHitComparatorLessDocid {
    bool operator() (const NnsHit &lhs, const NnsHit& rhs) const {
        return (lhs.docid < rhs.docid);
    }
};

class BitVector {
private:
    std::vector<uint64_t> _bits;
public:
    BitVector(size_t sz) : _bits((sz+63)/64) {}
    BitVector& setBit(size_t idx) {
        uint64_t mask = 1;
        mask <<= (idx%64);
        _bits[idx/64] |= mask;
        return *this;
    }
    bool isSet(size_t idx) const {
        uint64_t mask = 1;
        mask <<= (idx%64);
        uint64_t word = _bits[idx/64];
        return (word & mask) != 0;
    }
    BitVector& clearBit(size_t idx) {
        uint64_t mask = 1;
        mask <<= (idx%64);
        _bits[idx/64] &= ~mask;
        return *this;
    }
};

template <typename FltType = float>
class NNS
{
public:
    NNS(uint32_t numDims, const DocVectorAccess<FltType> &dva)
      : _numDims(numDims), _dva(dva)
    {}

    virtual void addDoc(uint32_t docid) = 0;
    virtual void removeDoc(uint32_t docid) = 0;

    using Vector = vespalib::ConstArrayRef<FltType>;
    virtual std::vector<NnsHit> topK(uint32_t k, Vector vector, uint32_t search_k) = 0;
    virtual std::vector<NnsHit> topKfilter(uint32_t k, Vector vector, uint32_t search_k, const BitVector &skipDocIds) = 0;
    virtual ~NNS() {}
protected:
    uint32_t _numDims;
    const DocVectorAccess<FltType> &_dva;
};

extern
std::unique_ptr<NNS<float>>
make_annoy_nns(uint32_t numDims, const DocVectorAccess<float> &dva);

extern
std::unique_ptr<NNS<float>>
make_rplsh_nns(uint32_t numDims, const DocVectorAccess<float> &dva);

extern
std::unique_ptr<NNS<float>>
make_hnsw_nns(uint32_t numDims, const DocVectorAccess<float> &dva);

extern
std::unique_ptr<NNS<float>>
make_hnsw_wrap(uint32_t numDims, const DocVectorAccess<float> &dva);