Skip to content

Commit

Permalink
Add alaya-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Yitao committed Jul 10, 2024
1 parent 9ca42f9 commit 4facf6a
Show file tree
Hide file tree
Showing 28 changed files with 7,837 additions and 0 deletions.
14 changes: 14 additions & 0 deletions ann_benchmarks/algorithms/alaya/pyglass-filter/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
cmake_minimum_required (VERSION 3.16)
project(glass LANGUAGES CXX)

add_library(glass INTERFACE)
target_include_directories(glass INTERFACE .)

set(CXX_STANDARD 20)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

SET(CMAKE_CXX_FLAGS "-Wall -Wextra -O3 -lrt -std=c++17 -march=native -fpic -fopenmp -ftree-vectorize -fno-exceptions -fno-rtti" )

# add_executable(main examples/main.cc)
# target_link_libraries(main glass)
21 changes: 21 additions & 0 deletions ann_benchmarks/algorithms/alaya/pyglass-filter/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 zh Wang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
13 changes: 13 additions & 0 deletions ann_benchmarks/algorithms/alaya/pyglass-filter/glass/builder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include "glass/graph.hpp"

namespace glass {

struct Builder {
virtual void Build(float *data, float* timestamps, int nb,int max_nb,int miss_end=0) = 0;
virtual Graph<int> GetGraph() = 0;
virtual ~Builder() = default;
};

} // namespace glass
50 changes: 50 additions & 0 deletions ann_benchmarks/algorithms/alaya/pyglass-filter/glass/common.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#pragma once

#include <string>
#include <unordered_map>

namespace glass {

enum class Metric {
L2,
IP,
};

inline std::unordered_map<std::string, Metric> metric_map;

inline int metric_map_init = [] {
metric_map["L2"] = Metric::L2;
metric_map["IP"] = Metric::IP;
return 42;
}();

inline constexpr size_t upper_div(size_t x, size_t y) {
return (x + y - 1) / y;
}

inline constexpr int64_t do_align(int64_t x, int64_t align) {
return (x + align - 1) / align * align;
}

#if defined(__clang__)

#define FAST_BEGIN
#define FAST_END
#define GLASS_INLINE __attribute__((always_inline))

#elif defined(__GNUC__)

#define FAST_BEGIN \
_Pragma("GCC push_options") _Pragma( \
"GCC optimize (\"unroll-loops,associative-math,no-signed-zeros\")")
#define FAST_END _Pragma("GCC pop_options")
#define GLASS_INLINE [[gnu::always_inline]]
#else

#define FAST_BEGIN
#define FAST_END
#define GLASS_INLINE

#endif

} // namespace glass
140 changes: 140 additions & 0 deletions ann_benchmarks/algorithms/alaya/pyglass-filter/glass/graph.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#pragma once

#include <cstdlib>
#include <cstring>
#include <fstream>
#include <memory>
#include <vector>

#include "glass/hnsw/HNSWInitializer.hpp"
#include "glass/memory.hpp"

namespace glass {

constexpr int EMPTY_ID = -1;

template <typename node_t> struct Graph {
int N, K;

node_t *data = nullptr;

float *timestamps = nullptr;

std::unique_ptr<HNSWInitializer> initializer = nullptr;

std::vector<int> eps;

Graph() = default;

Graph(node_t *edges, int N, int K) : N(N), K(K), data(edges) {}

Graph(int N, int K)
: N(N), K(K), data((node_t *)alloc2M((size_t)N * K * sizeof(node_t))),
timestamps((float *)alloc2M((size_t)N * K * sizeof(float))) {}

Graph(const Graph &g) : Graph(g.N, g.K) {
this->eps = g.eps;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < K; ++j) {
at(i, j) = g.at(i, j);
at_time(i, j) = g.at_time(i, j);
}
}
if (g.initializer) {
initializer = std::make_unique<HNSWInitializer>(*g.initializer);
}
}

Graph(Graph &&g) noexcept : N(g.N), K(g.K), data(g.data), timestamps(g.timestamps), initializer(std::move(g.initializer)), eps(std::move(g.eps)) {
g.data = nullptr;
g.timestamps = nullptr;
}

void init(int N, int K) {
data = (node_t *)alloc2M((size_t)N * K * sizeof(node_t));
timestamps = (float *)alloc2M((size_t)N * K * sizeof(float));
std::memset(data, -1, N * K * sizeof(node_t));
std::memset(timestamps, 0, N * K * sizeof(float));
this->K = K;
this->N = N;
}

~Graph() {
if (data != nullptr) {
free(data);
}
if (timestamps != nullptr) {
free(timestamps);
}
}

const int *edges(int u) const { return data + K * u; }

int *edges(int u) { return data + K * u; }

const float *get_timestamp(int u) const { return timestamps + K * u; }

node_t at(int i, int j) const { return data[i * K + j]; }

node_t &at(int i, int j) { return data[i * K + j]; }

float at_time(int i, int j) const { return timestamps[i * K + j]; }

float &at_time(int i, int j) { return timestamps[i * K + j]; }

void prefetch(int u, int lines) const {
mem_prefetch((char *)edges(u), lines);
mem_prefetch((char*)get_timestamp(u), lines);
}

template <typename Pool, typename Computer>
void initialize_search(Pool &pool, const Computer &computer) const {
if (initializer) {
initializer->initialize(pool, computer);
} else {
for (auto ep : eps) {
pool.insert(ep, computer(ep));
}
}
}

void save(const std::string &filename) const {
static_assert(std::is_same_v<node_t, int32_t>);
std::ofstream writer(filename.c_str(), std::ios::binary);
int nep = eps.size();
writer.write((char *)&nep, 4);
writer.write((char *)eps.data(), nep * 4);
writer.write((char *)&N, 4);
writer.write((char *)&K, 4);
writer.write((char *)data, N * K * 4);
writer.write((char *)timestamps, N * K * 4);
if (initializer) {
initializer->save(writer);
}
// printf("Graph Saving done\n");
}

void load(const std::string &filename) {
static_assert(std::is_same_v<node_t, int32_t>);
free(data);
free(timestamps);
std::ifstream reader(filename.c_str(), std::ios::binary);
int nep;
reader.read((char *)&nep, 4);
eps.resize(nep);
reader.read((char *)eps.data(), nep * 4);
reader.read((char *)&N, 4);
reader.read((char *)&K, 4);
data = (node_t *)alloc2M((size_t)N * K * 4);
reader.read((char *)data, N * K * 4);
timestamps = (float *)alloc2M((size_t)N * K * 4);
reader.read((char *)timestamps, N * K * 4);
if (reader.peek() != EOF) {
initializer = std::make_unique<HNSWInitializer>(N);
initializer->load(reader);
}
// printf("Graph Loding done\n");
}
};

} // namespace glass
Loading

0 comments on commit 4facf6a

Please sign in to comment.