File size: 5,513 Bytes
0e6a3f5
 
 
 
 
 
218384f
0e6a3f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218384f
 
 
 
 
 
2a7e238
 
 
 
 
 
218384f
 
2a7e238
 
 
 
 
218384f
 
 
 
 
 
 
2a7e238
 
 
 
c6fc1a8
 
9422a5b
 
 
 
c6fc1a8
 
 
 
 
 
9422a5b
 
218384f
 
 
 
 
 
 
0e6a3f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/* ํŒŒ์ด์ฌ ๋ฐ”์ธ๋”ฉ. ์‹คํ–‰์€ ํŒŒ์ด์ฌ์—์„œ ํ•˜๋˜ ์ƒ‰์ธยท๊ฒ€์ƒ‰ยท์ฑ„์ ์€ ์ „๋ถ€ C++์—์„œ ๋ˆ๋‹ค. */
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include "bm25.h"
#include "dense.h"
#include "graph.h"

namespace py = pybind11;

PYBIND11_MODULE(_ngdb_core, m) {
    m.doc() = "NeuroGraphDB baseline retrieval core (C++)";

    py::class_<BM25>(m, "BM25")
        .def(py::init<float,float>(), py::arg("k1") = 1.2f, py::arg("b") = 0.75f)
        .def("add", &BM25::add, py::arg("text"))
        .def("finalize", &BM25::finalize)
        .def("search", [](const BM25& self, const std::string& q, int k) {
            std::vector<std::pair<int,float>> out;
            for (auto& r : self.search(q, k)) out.emplace_back(r.doc, r.score);
            return out;
        }, py::arg("query"), py::arg("k") = 10)
        .def_property_readonly("size", &BM25::size);

    py::class_<Graph>(m, "Graph")
        .def(py::init<int32_t>(), py::arg("n_nodes"))
        .def("add_edge", &Graph::add_edge, py::arg("src"), py::arg("dst"), py::arg("w") = 1.0f)
        .def("add_edges", [](Graph& self, const std::vector<std::tuple<int,int,float>>& es) {
            for (auto& [s, d, w] : es) self.add_edge(s, d, w);
        }, py::arg("edges"))
        /* ๋ช…์ œ ์—ฃ์ง€ โ€” (src, dst, w, ์ˆ ์–ดํƒ€์ž…, ๊ทน์„ฑ) */
        .def("add_edges_typed", [](Graph& self,
                const std::vector<std::tuple<int,int,float,int,int>>& es) {
            for (auto& [s, d, w, t, p] : es)
                self.add_edge_typed(s, d, w, (int16_t)t, (int8_t)p);
        }, py::arg("edges"))
        .def("spread", [](const Graph& self, const std::vector<int32_t>& seeds,
                          const std::vector<float>& acts, int max_depth,
                          float depth_decay, float min_act, int top_k,
                          const std::vector<int16_t>& query_types,
                          float alpha, float beta) {
            auto r = self.spread(seeds, acts, max_depth, depth_decay, min_act,
                                 query_types, alpha, beta);
            if (top_k > 0 && (int)r.size() > top_k) r.resize(top_k);
            std::vector<std::pair<int,float>> out;
            out.reserve(r.size());
            for (auto& x : r) out.emplace_back(x.doc, x.score);
            return out;
        }, py::arg("seeds"), py::arg("seed_acts"), py::arg("max_depth") = 3,
           py::arg("depth_decay") = 0.65f, py::arg("min_act") = 0.02f,
           py::arg("top_k") = 0,
           /* ๊ธฐ๋ณธ๊ฐ’์ด "์ œ์•ฝ ์—†์Œ"์ด๋ผ ๊ธฐ์กด ํ˜ธ์ถœ๋ถ€๋Š” ํ•œ ๊ธ€์ž๋„ ์•ˆ ๋ฐ”๊ฟ”๋„ ๋œ๋‹ค */
           py::arg("query_types") = std::vector<int16_t>{},
           py::arg("alpha") = 1.0f, py::arg("beta") = 1.0f)
        .def("beam_search", [](const Graph& self, const std::vector<int32_t>& seeds,
                               const std::vector<float>& qsim, int max_depth,
                               int beam_width, float min_sim, int top_k,
                               int score_mode) {
            auto r = self.beam_search(seeds, qsim, max_depth, beam_width,
                                      min_sim, score_mode);
            if (top_k > 0 && (int)r.size() > top_k) r.resize(top_k);
            std::vector<std::pair<int,float>> out;
            out.reserve(r.size());
            for (auto& x : r) out.emplace_back(x.doc, x.score);
            return out;
        }, py::arg("seeds"), py::arg("qsim"), py::arg("max_depth") = 3,
           py::arg("beam_width") = 4, py::arg("min_sim") = 0.0f, py::arg("top_k") = 0,
           py::arg("score_mode") = 0)
        .def("reinforce", &Graph::reinforce, py::arg("coactive"),
             py::arg("delta") = 0.03f, py::arg("w_max") = 1.0f)
        .def("decay", &Graph::decay, py::arg("lambda_") = 0.02f)
        .def("edge_weight", &Graph::edge_weight, py::arg("src"), py::arg("dst"))
        .def_property_readonly("n_nodes", &Graph::n_nodes)
        .def_property_readonly("n_edges", &Graph::n_edges);

    py::class_<DenseIndex>(m, "DenseIndex")
        .def(py::init<int>(), py::arg("dim"))
        .def("add", [](DenseIndex& self, py::array_t<float, py::array::c_style | py::array::forcecast> v) {
            if (v.ndim() != 1 || v.shape(0) != self.dim())
                throw std::runtime_error("๋ฒกํ„ฐ ์ฐจ์›์ด ์ƒ‰์ธ๊ณผ ๋‹ค๋ฅด๋‹ค");
            return self.add(v.data());
        }, py::arg("vec"))
        .def("add_batch", [](DenseIndex& self, py::array_t<float, py::array::c_style | py::array::forcecast> m) {
            if (m.ndim() != 2 || m.shape(1) != self.dim())
                throw std::runtime_error("๋ฐฐ์น˜ shape์ด (n, dim)์ด ์•„๋‹ˆ๋‹ค");
            std::vector<int32_t> ids;
            ids.reserve(m.shape(0));
            for (py::ssize_t i = 0; i < m.shape(0); i++)
                ids.push_back(self.add(m.data() + i * self.dim()));
            return ids;
        }, py::arg("mat"))
        .def("search", [](const DenseIndex& self, py::array_t<float, py::array::c_style | py::array::forcecast> q, int k) {
            if (q.ndim() != 1 || q.shape(0) != self.dim())
                throw std::runtime_error("์งˆ์˜ ๋ฒกํ„ฐ ์ฐจ์›์ด ์ƒ‰์ธ๊ณผ ๋‹ค๋ฅด๋‹ค");
            std::vector<std::pair<int,float>> out;
            for (auto& r : self.search(q.data(), k)) out.emplace_back(r.doc, r.score);
            return out;
        }, py::arg("vec"), py::arg("k") = 10)
        .def_property_readonly("size", &DenseIndex::size)
        .def_property_readonly("dim", &DenseIndex::dim);
}