diff --git a/CMakeLists.txt b/CMakeLists.txt index 687e647..660a442 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/example/main.cpp b/example/main.cpp index bd8b3d8..54227d2 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -1,5 +1,4 @@ #include "template.h" - #include int main(void) diff --git a/tests/bindings.cpp b/tests/bindings.cpp new file mode 100644 index 0000000..d6229b7 --- /dev/null +++ b/tests/bindings.cpp @@ -0,0 +1,34 @@ +#include +#include +#include +#include "template.h" // Include your header file + +namespace py = pybind11; + +PYBIND11_MODULE(bindings, m) { + m.doc() = "Your module documentation string"; + + py::class_(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 { + 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& input) { + struct ggml_tensor* result = compute(model, input); + std::vector 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"); +} diff --git a/tests/py_bindings.cpp b/tests/py_bindings.cpp deleted file mode 100644 index 067eedb..0000000 --- a/tests/py_bindings.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include "template.h" -#include - -int add(int i, int j) -{ - return i + j; -} - -namespace py = pybind11; - -PYBIND11_MODULE(py_bindings, m) -{ - m.def("add", &add); -} \ No newline at end of file diff --git a/tests/test.py b/tests/test.py index 0ab5d05..af27a07 100644 --- a/tests/test.py +++ b/tests/test.py @@ -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()