blob: 63b2a83c1b514bb0506f162853b441fc41706261 (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include "distance_function.h"
#include "distance_function_factory.h"
#include <vespa/eval/eval/typed_cells.h>
#include <mutex>
#include <memory>
namespace search::tensor {
/**
* Thread-safe storage of maximum value for squared vector norm.
* sq_norm = |x|^2 = sum(x[i]*x[i]) = dotproduct(x,x)
* Note that the initial value is 1.0; so even if all
* vectors seen have 0 or very small length, you will never
* get a value < 1.0.
*/
class MaximumSquaredNormStore {
private:
std::mutex _lock;
double _max_sq_norm;
public:
MaximumSquaredNormStore() noexcept : _lock(), _max_sq_norm(1.0) {}
/**
* Fetch the maximum value seen so far.
* Usually you will also supply a value computed for a newly seen
* vector, which may update the maximum value.
*/
double get_max(double value = 0.0) {
std::lock_guard<std::mutex> guard(_lock);
if (value > _max_sq_norm) [[unlikely]] {
_max_sq_norm = value;
}
return _max_sq_norm;
}
};
class MipsDistanceFunctionFactoryBase : public DistanceFunctionFactory {
protected:
std::shared_ptr<MaximumSquaredNormStore> _sq_norm_store;
public:
MipsDistanceFunctionFactoryBase()
: _sq_norm_store(std::make_shared<MaximumSquaredNormStore>())
{
}
~MipsDistanceFunctionFactoryBase() = default;
MaximumSquaredNormStore& get_max_squared_norm_store() noexcept { return *_sq_norm_store; }
};
/**
* Factory for distance functions which can apply a transformation
* mapping Maximum Inner Product Search to a nearest neighbor
* problem. When inserting vectors, an extra dimension is
* added ensuring behavior "as if" all vectors had length equal
* to the longest vector inserted so far, or at least length 1.
*/
template<typename FloatType>
class MipsDistanceFunctionFactory : public MipsDistanceFunctionFactoryBase {
public:
MipsDistanceFunctionFactory() : MipsDistanceFunctionFactoryBase() { }
~MipsDistanceFunctionFactory() = default;
BoundDistanceFunction::UP for_query_vector(const vespalib::eval::TypedCells& lhs) override;
BoundDistanceFunction::UP for_insertion_vector(const vespalib::eval::TypedCells& lhs) override;
};
}
|