Skip to content

Commit

Permalink
tests are working now
Browse files Browse the repository at this point in the history
  • Loading branch information
grazder committed Apr 16, 2024
1 parent 3e1854c commit 677bb06
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 20 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
cmake_minimum_required(VERSION 3.12)
project(template.cpp)
set(TEMPLATE_LIB template)
set(BINDINGS_LIB py_bindings)
set(BINDINGS_LIB bindings)

add_subdirectory(ggml)
add_library(${TEMPLATE_LIB} STATIC template.cpp template.h)

add_subdirectory(example)
add_subdirectory(tests/pybind11)
pybind11_add_module(${BINDINGS_LIB} tests/py_bindings.cpp)
pybind11_add_module(${BINDINGS_LIB} tests/bindings.cpp)

target_link_libraries(${BINDINGS_LIB} PRIVATE ${TEMPLATE_LIB})
target_link_libraries(${TEMPLATE_LIB} PUBLIC ggml)
Expand Down
1 change: 0 additions & 1 deletion example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "template.h"

#include <string.h>

int main(void)
Expand Down
34 changes: 34 additions & 0 deletions tests/bindings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
#include "template.h" // Include your header file

namespace py = pybind11;

PYBIND11_MODULE(bindings, m) {
m.doc() = "Your module documentation string";

py::class_<module>(m, "Module")
.def(py::init<>())
.def_readwrite("hparams", &module::hparams)
// .def_readwrite("backend", &module::backend)
// .def_readwrite("buffer_w", &module::buffer_w)
.def_readwrite("fc_w", &module::fc_w)
.def_readwrite("bias", &module::bias)
// .def_readwrite("ctx", &module::ctx)
.def_readwrite("tensors", &module::tensors);

m.def("load_model", [](const std::string& fname) -> std::tuple<bool, py::object> {
module model;
bool success = load_model(fname, model);
return std::make_tuple(success, success ? py::cast(model) : py::cast(nullptr));
}, "A function to load the model");

m.def("compute", [](const module& model, const std::vector<float>& input) {
struct ggml_tensor* result = compute(model, input);
std::vector<float> out_data(ggml_nelements(result));
memcpy(out_data.data(), result->data, ggml_nbytes(result));
// ggml_free(result);
return out_data;
}, "A function to compute using the loaded model");
}
14 changes: 0 additions & 14 deletions tests/py_bindings.cpp

This file was deleted.

26 changes: 23 additions & 3 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,29 @@
sys.path.append("../")

try:
from build import py_bindings
except ImportError as e:
from build import bindings
except ImportError:
raise ImportError("Please build package before running tests...")


print(py_bindings.add(1, 2))
def main():
# Load model
success, model = bindings.load_model("../ggml-model.bin")

if success:
print("Model loaded successfully")
else:
print("Failed to load model")
return

# Create example tensor
input_data = [10, 10, 10, 10, 10]

# Compute
result = bindings.compute(model, input_data)

# Print result
print("Result:", result)

if __name__ == "__main__":
main()

0 comments on commit 677bb06

Please sign in to comment.