summaryrefslogtreecommitdiffstats
path: root/searchlib
diff options
context:
space:
mode:
authorArne Juul <arnej@yahooinc.com>2023-04-24 08:53:56 +0000
committerArne Juul <arnej@yahooinc.com>2023-04-24 08:53:56 +0000
commit46dcacf8b4402f349411171588e1485c2e58569f (patch)
treeec2939507e8b14d44e6882af56c322cc2cba737f /searchlib
parent06ae8f17e040fe27e19dd7bdf4857ce0c4ccaba1 (diff)
split out TemporaryVectorStore
Diffstat (limited to 'searchlib')
-rw-r--r--searchlib/src/vespa/searchlib/tensor/CMakeLists.txt1
-rw-r--r--searchlib/src/vespa/searchlib/tensor/angular_distance.cpp1
-rw-r--r--searchlib/src/vespa/searchlib/tensor/bound_distance_function.cpp56
-rw-r--r--searchlib/src/vespa/searchlib/tensor/bound_distance_function.h47
-rw-r--r--searchlib/src/vespa/searchlib/tensor/euclidean_distance.cpp1
-rw-r--r--searchlib/src/vespa/searchlib/tensor/temporary_vector_store.cpp57
-rw-r--r--searchlib/src/vespa/searchlib/tensor/temporary_vector_store.h32
7 files changed, 92 insertions, 103 deletions
diff --git a/searchlib/src/vespa/searchlib/tensor/CMakeLists.txt b/searchlib/src/vespa/searchlib/tensor/CMakeLists.txt
index 090042e5b83..1783e0da1dd 100644
--- a/searchlib/src/vespa/searchlib/tensor/CMakeLists.txt
+++ b/searchlib/src/vespa/searchlib/tensor/CMakeLists.txt
@@ -34,6 +34,7 @@ vespa_add_library(searchlib_tensor OBJECT
serialized_tensor_ref.cpp
small_subspaces_buffer_type.cpp
subspace_type.cpp
+ temporary_vector_store.cpp
tensor_attribute.cpp
tensor_attribute_loader.cpp
tensor_attribute_saver.cpp
diff --git a/searchlib/src/vespa/searchlib/tensor/angular_distance.cpp b/searchlib/src/vespa/searchlib/tensor/angular_distance.cpp
index 5101373c047..85eac76728c 100644
--- a/searchlib/src/vespa/searchlib/tensor/angular_distance.cpp
+++ b/searchlib/src/vespa/searchlib/tensor/angular_distance.cpp
@@ -1,6 +1,7 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "angular_distance.h"
+#include "temporary_vector_store.h"
using vespalib::typify_invoke;
using vespalib::eval::TypifyCellType;
diff --git a/searchlib/src/vespa/searchlib/tensor/bound_distance_function.cpp b/searchlib/src/vespa/searchlib/tensor/bound_distance_function.cpp
index 19c2e744954..33b94e5218c 100644
--- a/searchlib/src/vespa/searchlib/tensor/bound_distance_function.cpp
+++ b/searchlib/src/vespa/searchlib/tensor/bound_distance_function.cpp
@@ -1,59 +1,3 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "bound_distance_function.h"
-#include <vespa/log/log.h>
-
-LOG_SETUP(".searchlib.tensor.bound_distance_function");
-
-using vespalib::ConstArrayRef;
-using vespalib::ArrayRef;
-using vespalib::eval::CellType;
-using vespalib::eval::TypedCells;
-
-namespace search::tensor {
-
-namespace {
-
-template<typename FromType, typename ToType>
-ConstArrayRef<ToType>
-convert_cells(ArrayRef<ToType> space, TypedCells cells)
-{
- assert(cells.size == space.size());
- auto old_cells = cells.typify<FromType>();
- ToType *p = space.data();
- for (FromType value : old_cells) {
- ToType conv(value);
- *p++ = conv;
- }
- return space;
-}
-
-template <typename ToType>
-struct ConvertCellsSelector
-{
- template <typename FromType> static auto invoke(ArrayRef<ToType> dst, TypedCells src) {
- return convert_cells<FromType, ToType>(dst, src);
- }
-};
-
-} // namespace
-
-template <typename FloatType>
-ConstArrayRef<FloatType>
-TemporaryVectorStore<FloatType>::internal_convert(TypedCells cells, size_t offset) {
- LOG_ASSERT(cells.size * 2 == _tmpSpace.size());
- ArrayRef<FloatType> where(_tmpSpace.data() + offset, cells.size);
- using MyTypify = vespalib::eval::TypifyCellType;
- using MySelector = ConvertCellsSelector<FloatType>;
- ConstArrayRef<FloatType> result = vespalib::typify_invoke<1,MyTypify,MySelector>(cells.type, where, cells);
- return result;
-}
-
-template class TemporaryVectorStore<vespalib::eval::Int8Float>;
-template class TemporaryVectorStore<float>;
-template class TemporaryVectorStore<double>;
-
-template class ConvertingBoundDistance<float>;
-template class ConvertingBoundDistance<double>;
-
-}
diff --git a/searchlib/src/vespa/searchlib/tensor/bound_distance_function.h b/searchlib/src/vespa/searchlib/tensor/bound_distance_function.h
index 8949aea8796..5d602a52227 100644
--- a/searchlib/src/vespa/searchlib/tensor/bound_distance_function.h
+++ b/searchlib/src/vespa/searchlib/tensor/bound_distance_function.h
@@ -42,51 +42,4 @@ public:
double limit) const = 0;
};
-
-/** helper class - temporary storage of possibly-converted vector cells */
-template <typename FloatType>
-class TemporaryVectorStore {
-private:
- std::vector<FloatType> _tmpSpace;
- vespalib::ConstArrayRef<FloatType> internal_convert(vespalib::eval::TypedCells cells, size_t offset);
-public:
- TemporaryVectorStore(size_t vectorSize) : _tmpSpace(vectorSize * 2) {}
- vespalib::ConstArrayRef<FloatType> storeLhs(vespalib::eval::TypedCells cells) {
- return internal_convert(cells, 0);
- }
- vespalib::ConstArrayRef<FloatType> convertRhs(vespalib::eval::TypedCells cells) {
- if (vespalib::eval::get_cell_type<FloatType>() == cells.type) [[likely]] {
- return cells.unsafe_typify<FloatType>();
- } else {
- return internal_convert(cells, cells.size);
- }
- }
-};
-
-template<typename FloatType>
-class ConvertingBoundDistance : public BoundDistanceFunction {
- mutable TemporaryVectorStore<FloatType> _tmpSpace;
- const vespalib::eval::TypedCells _lhs;
- const DistanceFunction &_df;
-public:
- ConvertingBoundDistance(const vespalib::eval::TypedCells& lhs, const DistanceFunction &df)
- : BoundDistanceFunction(vespalib::eval::get_cell_type<FloatType>()),
- _tmpSpace(lhs.size),
- _lhs(_tmpSpace.storeLhs(lhs)),
- _df(df)
- {}
- double calc(const vespalib::eval::TypedCells& rhs) const override {
- return _df.calc(_lhs, vespalib::eval::TypedCells(_tmpSpace.convertRhs(rhs)));
- }
- double convert_threshold(double threshold) const override {
- return _df.convert_threshold(threshold);
- }
- double to_rawscore(double distance) const override {
- return _df.to_rawscore(distance);
- }
- double calc_with_limit(const vespalib::eval::TypedCells& rhs, double limit) const override {
- return _df.calc_with_limit(_lhs, vespalib::eval::TypedCells(_tmpSpace.convertRhs(rhs)), limit);
- }
-};
-
}
diff --git a/searchlib/src/vespa/searchlib/tensor/euclidean_distance.cpp b/searchlib/src/vespa/searchlib/tensor/euclidean_distance.cpp
index 6a54798883f..92d4e7af406 100644
--- a/searchlib/src/vespa/searchlib/tensor/euclidean_distance.cpp
+++ b/searchlib/src/vespa/searchlib/tensor/euclidean_distance.cpp
@@ -1,6 +1,7 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "euclidean_distance.h"
+#include "temporary_vector_store.h"
using vespalib::typify_invoke;
using vespalib::eval::TypifyCellType;
diff --git a/searchlib/src/vespa/searchlib/tensor/temporary_vector_store.cpp b/searchlib/src/vespa/searchlib/tensor/temporary_vector_store.cpp
new file mode 100644
index 00000000000..cc45f857d9f
--- /dev/null
+++ b/searchlib/src/vespa/searchlib/tensor/temporary_vector_store.cpp
@@ -0,0 +1,57 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#include "temporary_vector_store.h"
+
+#include <vespa/log/log.h>
+
+LOG_SETUP(".searchlib.tensor.temporary_vector_store");
+
+using vespalib::ConstArrayRef;
+using vespalib::ArrayRef;
+using vespalib::eval::CellType;
+using vespalib::eval::TypedCells;
+
+namespace search::tensor {
+
+namespace {
+
+template<typename FromType, typename ToType>
+ConstArrayRef<ToType>
+convert_cells(ArrayRef<ToType> space, TypedCells cells)
+{
+ assert(cells.size == space.size());
+ auto old_cells = cells.typify<FromType>();
+ ToType *p = space.data();
+ for (FromType value : old_cells) {
+ ToType conv(value);
+ *p++ = conv;
+ }
+ return space;
+}
+
+template <typename ToType>
+struct ConvertCellsSelector
+{
+ template <typename FromType> static auto invoke(ArrayRef<ToType> dst, TypedCells src) {
+ return convert_cells<FromType, ToType>(dst, src);
+ }
+};
+
+} // namespace
+
+template <typename FloatType>
+ConstArrayRef<FloatType>
+TemporaryVectorStore<FloatType>::internal_convert(TypedCells cells, size_t offset) {
+ LOG_ASSERT(cells.size * 2 == _tmpSpace.size());
+ ArrayRef<FloatType> where(_tmpSpace.data() + offset, cells.size);
+ using MyTypify = vespalib::eval::TypifyCellType;
+ using MySelector = ConvertCellsSelector<FloatType>;
+ ConstArrayRef<FloatType> result = vespalib::typify_invoke<1,MyTypify,MySelector>(cells.type, where, cells);
+ return result;
+}
+
+template class TemporaryVectorStore<vespalib::eval::Int8Float>;
+template class TemporaryVectorStore<float>;
+template class TemporaryVectorStore<double>;
+
+}
diff --git a/searchlib/src/vespa/searchlib/tensor/temporary_vector_store.h b/searchlib/src/vespa/searchlib/tensor/temporary_vector_store.h
new file mode 100644
index 00000000000..cd816621f91
--- /dev/null
+++ b/searchlib/src/vespa/searchlib/tensor/temporary_vector_store.h
@@ -0,0 +1,32 @@
+// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+#pragma once
+
+#include <memory>
+#include <vespa/eval/eval/cell_type.h>
+#include <vespa/eval/eval/typed_cells.h>
+#include <vespa/vespalib/util/arrayref.h>
+
+namespace search::tensor {
+
+/** helper class - temporary storage of possibly-converted vector cells */
+template <typename FloatType>
+class TemporaryVectorStore {
+private:
+ std::vector<FloatType> _tmpSpace;
+ vespalib::ConstArrayRef<FloatType> internal_convert(vespalib::eval::TypedCells cells, size_t offset);
+public:
+ TemporaryVectorStore(size_t vectorSize) : _tmpSpace(vectorSize * 2) {}
+ vespalib::ConstArrayRef<FloatType> storeLhs(vespalib::eval::TypedCells cells) {
+ return internal_convert(cells, 0);
+ }
+ vespalib::ConstArrayRef<FloatType> convertRhs(vespalib::eval::TypedCells cells) {
+ if (vespalib::eval::get_cell_type<FloatType>() == cells.type) [[likely]] {
+ return cells.unsafe_typify<FloatType>();
+ } else {
+ return internal_convert(cells, cells.size);
+ }
+ }
+};
+
+}