Complete implementation but runtime failing

This commit is contained in:
2023-11-01 17:58:17 +01:00
parent 296ed6b785
commit ee9d5e738c
8 changed files with 51 additions and 9 deletions

View File

@@ -9,5 +9,14 @@ find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
# Temporary patch while find_package(Torch) is not fixed
file(
GLOB
LIBTORCH_PYTHON
"${TORCH_INSTALL_PREFIX}/lib/libtorch_python.so"
"${TORCH_INSTALL_PREFIX}/lib/libtorch_python.dylib"
)
message(STATUS "TORCH Libraries: ${LIBTORCH_PYTHON}")
add_subdirectory(lib/Files)
add_subdirectory(src)

View File

@@ -2,5 +2,5 @@
Python Wrapper to use Python classifiers from C++
Explain issues with conda and libstdc++ and how to fix them
Explain issues in Linux with conda and libstdc++ and how to fix them
(moving conda libstdc++ to a different folder)

View File

@@ -4,5 +4,5 @@ include_directories(${Python3_INCLUDE_DIRS})
add_executable(main main.cc STree.cc SVC.cc PyClassifier.cc PyWrap.cc)
add_executable(example example.cpp)
target_link_libraries(main ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ArffFiles)
target_link_libraries(main ${Python3_LIBRARIES} "${TORCH_LIBRARIES}" ${LIBTORCH_PYTHON} ArffFiles)
target_link_libraries(example "${TORCH_LIBRARIES}" ArffFiles)

View File

@@ -1,5 +1,7 @@
#include "PyClassifier.h"
#include <torch/csrc/autograd/python_variable.h>
#include <torch/csrc/utils/tensor_numpy.h>
#include <iostream>
namespace pywrap {
@@ -13,7 +15,11 @@ namespace pywrap {
{
pyWrap->clean(module, className);
}
PyObject* PyClassifier::toPyObject(torch::Tensor& tensor)
{
return torch::utils::tensor_to_numpy(tensor);
//return THPVariable_Wrap(tensor);
}
std::string PyClassifier::version()
{
return pyWrap->version(module, className);
@@ -25,10 +31,35 @@ namespace pywrap {
}
PyClassifier& PyClassifier::fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
{
PyObject* Xp = NULL;//THPVariable_Wrap(X);
PyObject* yp = NULL;//THPVariable_Wrap(y);
std::cout << "Converting X to PyObject" << std::endl;
std::cout << "X.defined() = " << X.defined() << std::endl;
//std::cout << "X.pyobj() = " << X.pyobj() << std::endl;
PyObject* Xp = toPyObject(X);
std::cout << "Converting y to PyObject" << std::endl;
PyObject* yp = toPyObject(y);
std::cout << "Calling fit" << std::endl;
pyWrap->fit(module, className, Xp, yp);
Py_DECREF(Xp);
Py_DECREF(yp);
return *this;
}
torch::Tensor PyClassifier::predict(torch::Tensor& X)
{
PyObject* Xp = toPyObject(X);
auto PyResult = pyWrap->predict(module, className, Xp);
auto result = THPVariable_Unpack(PyResult);
Py_DECREF(Xp);
Py_DECREF(PyResult);
return result;
}
double PyClassifier::score(torch::Tensor& X, torch::Tensor& y)
{
PyObject* Xp = toPyObject(X);
PyObject* yp = toPyObject(y);
auto result = pyWrap->score(module, className, Xp, yp);
Py_DECREF(Xp);
Py_DECREF(yp);
return result;
}
} /* namespace PyWrap */

View File

@@ -17,6 +17,7 @@ namespace pywrap {
std::string version();
std::string callMethodString(const std::string& method);
private:
PyObject* toPyObject(torch::Tensor& tensor);
PyWrap* pyWrap;
std::string module;
std::string className;

View File

@@ -7,8 +7,6 @@ namespace pywrap {
public:
STree() : PyClassifier("stree", "Stree") {};
~STree() = default;
private:
};
} /* namespace pywrap */

View File

@@ -8,8 +8,6 @@ namespace pywrap {
SVC() : PyClassifier("sklearn.svm", "SVC") {};
~SVC() = default;
std::string version();
private:
};
} /* namespace pywrap */

View File

@@ -48,5 +48,10 @@ int main(int argc, char* argv[])
auto svc = pywrap::SVC();
svc.version();
stree.version();
cout << string(80, '-') << endl;
cout << "X: " << X.sizes() << endl;
cout << "y: " << y.sizes() << endl;
auto result = svc.fit(X, y, features, className, states).score(X, y);
cout << "SVC score " << result << endl;
return 0;
}