aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/tensor/distance_calculator.cpp
blob: 8da777d97eb0d4976777d3579422277fe03d2800 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "distance_calculator.h"
#include "distance_function_factory.h"
#include "nearest_neighbor_index.h"
#include <vespa/eval/eval/fast_value.h>
#include <vespa/searchcommon/attribute/iattributevector.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/stringfmt.h>

using vespalib::IllegalArgumentException;
using vespalib::eval::CellType;
using vespalib::eval::FastValueBuilderFactory;
using vespalib::eval::TypedCells;
using vespalib::eval::Value;
using vespalib::eval::ValueType;
using vespalib::make_string;

namespace {

template<typename LCT, typename RCT>
std::unique_ptr<Value>
convert_cells(const ValueType& new_type, const Value& old_value)
{
    auto old_cells = old_value.cells().typify<LCT>();
    auto builder = FastValueBuilderFactory::get().create_value_builder<RCT>(new_type);
    auto new_cells = builder->add_subspace();
    assert(old_cells.size() == new_cells.size());
    auto p = new_cells.begin();
    for (LCT value : old_cells) {
        RCT conv(value);
        *p++ = conv;
    }
    return builder->build(std::move(builder));
}

struct ConvertCellsSelector
{
    template <typename LCT, typename RCT>
    static auto invoke(const ValueType& new_type, const Value& old_value) {
        return convert_cells<LCT, RCT>(new_type, old_value);
    }
    auto operator() (CellType from, CellType to, const Value& old_value) const {
        using MyTypify = vespalib::eval::TypifyCellType;
        ValueType new_type = old_value.type().cell_cast(to);
        return vespalib::typify_invoke<2,MyTypify,ConvertCellsSelector>(from, to, new_type, old_value);
    }
};

}

namespace search::tensor {

DistanceCalculator::DistanceCalculator(const tensor::ITensorAttribute& attr_tensor,
                                       const vespalib::eval::Value& query_tensor_in)
    : _attr_tensor(attr_tensor),
      _query_tensor_uptr(),
      _query_tensor(&query_tensor_in),
      _dist_fun()
{
    auto * nns_index = _attr_tensor.nearest_neighbor_index();
    auto & dff = nns_index ? nns_index->distance_function_factory() : attr_tensor.distance_function_factory();
    auto query_ct = _query_tensor->cells().type;
    CellType required_ct = dff.expected_cell_type;
    if (query_ct != required_ct) {
        ConvertCellsSelector converter;
        _query_tensor_uptr = converter(query_ct, required_ct, *_query_tensor);
        _query_tensor = _query_tensor_uptr.get();
    }
    _dist_fun = dff.for_query_vector(_query_tensor->cells());
    assert(_dist_fun);
}

DistanceCalculator::DistanceCalculator(const tensor::ITensorAttribute& attr_tensor,
                                       BoundDistanceFunction::UP function_in)
    : _attr_tensor(attr_tensor),
      _query_tensor_uptr(),
      _query_tensor(nullptr),
      _dist_fun(std::move(function_in))
{
}

DistanceCalculator::~DistanceCalculator() = default;

namespace {

bool
supported_tensor_type(const vespalib::eval::ValueType& type)
{
    if (type.is_dense() && type.dimensions().size() == 1) {
        return true;
    }
    if (type.is_mixed() && type.dimensions().size() == 2) {
        return true;
    }
    return false;
}

bool
is_compatible(const vespalib::eval::ValueType& lhs,
              const vespalib::eval::ValueType& rhs)
{
    return (lhs.indexed_dimensions() == rhs.indexed_dimensions());
}

}

std::unique_ptr<DistanceCalculator>
DistanceCalculator::make_with_validation(const search::attribute::IAttributeVector& attr,
                                         const vespalib::eval::Value& query_tensor_in)
{
    const ITensorAttribute* attr_tensor = attr.asTensorAttribute();
    if (attr_tensor == nullptr) {
        throw IllegalArgumentException("Attribute is not a tensor");
    }
    const auto& at_type = attr_tensor->getTensorType();
    if (!supported_tensor_type(at_type)) {
        throw IllegalArgumentException(make_string("Attribute tensor type (%s) is not supported",
                                                   at_type.to_spec().c_str()));
    }
    const auto& qt_type = query_tensor_in.type();
    if (!qt_type.is_dense()) {
        throw IllegalArgumentException(make_string("Query tensor type (%s) is not a dense tensor",
                                                   qt_type.to_spec().c_str()));
    }
    if (!is_compatible(at_type, qt_type)) {
        throw IllegalArgumentException(make_string("Attribute tensor type (%s) and query tensor type (%s) are not compatible",
                                                   at_type.to_spec().c_str(), qt_type.to_spec().c_str()));
    }
    return std::make_unique<DistanceCalculator>(*attr_tensor, query_tensor_in);
}

}