|
| 1 | +/** |
| 2 | + * @file unit_backwards_compatibility.cc |
| 3 | + * |
| 4 | + * @section LICENSE |
| 5 | + * |
| 6 | + * The MIT License |
| 7 | + * |
| 8 | + * @copyright Copyright (c) 2023 TileDB, Inc. |
| 9 | + * |
| 10 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | + * of this software and associated documentation files (the "Software"), to deal |
| 12 | + * in the Software without restriction, including without limitation the rights |
| 13 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | + * copies of the Software, and to permit persons to whom the Software is |
| 15 | + * furnished to do so, subject to the following conditions: |
| 16 | + * |
| 17 | + * The above copyright notice and this permission notice shall be included in |
| 18 | + * all copies or substantial portions of the Software. |
| 19 | + * |
| 20 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 23 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | + * THE SOFTWARE. |
| 27 | + * |
| 28 | + * @section DESCRIPTION |
| 29 | + * |
| 30 | + * Test that the current codebase can read and query old indexes. |
| 31 | + * |
| 32 | + */ |
| 33 | + |
| 34 | +#include <catch2/catch_all.hpp> |
| 35 | +#include <iostream> |
| 36 | +#include "api/feature_vector_array.h" |
| 37 | +#include "api/flat_l2_index.h" |
| 38 | +#include "api/ivf_flat_index.h" |
| 39 | +#include "api/vamana_index.h" |
| 40 | +#include "array_defs.h" |
| 41 | +#include "detail/linalg/matrix.h" |
| 42 | +#include "index/flat_l2_index.h" |
| 43 | +#include "index/ivf_flat_index.h" |
| 44 | +#include "index/vamana_index.h" |
| 45 | +#include "mdspan/mdspan.hpp" |
| 46 | +#include "utils/print_types.h" |
| 47 | + |
| 48 | +TEST_CASE("backwards_compatibility: test test", "[backwards_compatibility]") { |
| 49 | + REQUIRE(true); |
| 50 | +} |
| 51 | + |
| 52 | +TEST_CASE( |
| 53 | + "backwards_compatibility: test_query_old_indices", |
| 54 | + "[backwards_compatibility]") { |
| 55 | + tiledb::Context ctx; |
| 56 | + std::string datasets_path = backwards_compatibility_root / "data"; |
| 57 | + auto base = |
| 58 | + read_bin_local<siftsmall_feature_type>(ctx, siftmicro_inputs_file); |
| 59 | + |
| 60 | + std::vector<size_t> query_indices = {0, 3, 4, 8, 10, 19, 28, 31, |
| 61 | + 39, 40, 41, 47, 49, 50, 56, 64, |
| 62 | + 68, 70, 71, 79, 82, 89, 90, 94}; |
| 63 | + std::vector<std::span<siftsmall_feature_type>> queries; |
| 64 | + for (size_t idx : query_indices) { |
| 65 | + queries.push_back(base[idx]); |
| 66 | + } |
| 67 | + |
| 68 | + auto queries_matrix = ColMajorMatrix<siftsmall_feature_type>( |
| 69 | + queries[0].size(), query_indices.size()); |
| 70 | + for (size_t i = 0; i < query_indices.size(); ++i) { |
| 71 | + for (size_t j = 0; j < queries[0].size(); ++j) { |
| 72 | + queries_matrix(j, i) = queries[i][j]; |
| 73 | + } |
| 74 | + } |
| 75 | + auto queries_feature_vector_array = FeatureVectorArray(queries_matrix); |
| 76 | + |
| 77 | + for (const auto& directory_name : |
| 78 | + std::filesystem::directory_iterator(datasets_path)) { |
| 79 | + std::string version_path = directory_name.path().string(); |
| 80 | + if (!std::filesystem::is_directory(version_path)) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + for (const auto& index_name : |
| 85 | + std::filesystem::directory_iterator(version_path)) { |
| 86 | + std::string index_uri = index_name.path().string(); |
| 87 | + if (!std::filesystem::is_directory(index_uri)) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + // TODO(paris): Fix bug where we can't load old indexes b/c of a storage |
| 91 | + // version mismatch in metadata. |
| 92 | + if (index_uri.find("0.0.10") != std::string::npos || |
| 93 | + index_uri.find("0.0.17") != std::string::npos) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + std::vector<float> expected_distances(query_indices.size(), 0.0); |
| 97 | + if (index_uri.find("ivf_flat") != std::string::npos) { |
| 98 | + auto index = IndexIVFFlat(ctx, index_uri); |
| 99 | + auto&& [scores, ids] = |
| 100 | + index.query_infinite_ram(queries_feature_vector_array, 1, 10); |
| 101 | + auto scores_span = |
| 102 | + MatrixView<siftsmall_feature_type, stdx::layout_left>{ |
| 103 | + (siftsmall_feature_type*)scores.data(), |
| 104 | + extents(scores)[0], |
| 105 | + extents(scores)[1]}; |
| 106 | + |
| 107 | + auto ids_span = MatrixView<siftsmall_ids_type, stdx::layout_left>{ |
| 108 | + (siftsmall_ids_type*)ids.data(), extents(ids)[0], extents(ids)[1]}; |
| 109 | + |
| 110 | + for (size_t i = 0; i < query_indices.size(); ++i) { |
| 111 | + CHECK(ids_span[0][i] == query_indices[i]); |
| 112 | + CHECK(scores_span[0][i] == 0); |
| 113 | + } |
| 114 | + } else if (index_uri.find("flat") != std::string::npos) { |
| 115 | + // TODO(paris): Fix flat_l2_index and re-enable. Right now it just tries |
| 116 | + // to load the URI as a tdbMatrix. |
| 117 | + } else if (index_uri.find("vamana") != std::string::npos) { |
| 118 | + // TODO(paris): Add once we save a vamana index in backwards |
| 119 | + // compatibility data. |
| 120 | + } else { |
| 121 | + REQUIRE(false); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments