Refactor singleton to manage cleanup

This commit is contained in:
2023-11-04 11:00:21 +01:00
parent 9b5e7b1ca7
commit 8b159f239b
9 changed files with 104 additions and 181 deletions

View File

@@ -1,8 +1,6 @@
#include "PyClassifier.h" #include "PyClassifier.h"
#include <boost/python/numpy.hpp> #include <boost/python/numpy.hpp>
#include <torch/csrc/autograd/python_variable.h>
#include <torch/csrc/utils/tensor_numpy.h> #include <torch/csrc/utils/tensor_numpy.h>
//#include "tensorflow/python/lib/core/py_func.h"
#include <iostream> #include <iostream>
namespace pywrap { namespace pywrap {
@@ -13,35 +11,20 @@ namespace pywrap {
pyWrap = PyWrap::GetInstance(); pyWrap = PyWrap::GetInstance();
pyWrap->importClass(module, className); pyWrap->importClass(module, className);
} }
PyClassifier::~PyClassifier() PyClassifier::~PyClassifier()
{ {
std::cout << "Cleaning Classifier" << std::endl; std::cout << "Cleaning Classifier" << std::endl;
pyWrap->clean(module, className); pyWrap->clean(module, className);
std::cout << "Classifier cleaned" << std::endl; std::cout << "Classifier cleaned" << std::endl;
} }
PyObject* PyClassifier::toPyObject(torch::Tensor& data_tensor)
{
// return torch::utils::tensor_to_numpy(data_tensor);
return THPVariable_Wrap(data_tensor);
//auto data_numpy = np::from_data(data_tensor.data_ptr(), np::dtype::get_builtin<float>(), p::make_tuple(m, n), p::make_tuple(sizeof(data_tensor.dtype()) * 2 * n, sizeof(data_tensor.dtype()) * 2), p::object());
// PyObject* numpyObject = data_numpy.ptr();
// return numpyObject;
}
// PyObject* PyClassifier::toPyObjecty(torch::Tensor& data_tensor)
// {
// //return THPVariable_Wrap(tensor);
// auto y_numpy = np::from_data(data_tensor.data_ptr(), np::dtype::get_builtin<int32_t>(), p::make_tuple(m), p::make_tuple(sizeof(data_tensor.dtype()) * 2), p::object());
// PyObject* numpyObject = y_numpy.ptr();
// }
std::string PyClassifier::version() std::string PyClassifier::version()
{ {
return pyWrap->version(module, className); return pyWrap->version(module, className);
} }
std::string PyClassifier::graph()
{
return pyWrap->graph(module, className);
}
std::string PyClassifier::callMethodString(const std::string& method) std::string PyClassifier::callMethodString(const std::string& method)
{ {
return pyWrap->callMethodString(module, className, method); return pyWrap->callMethodString(module, className, method);
@@ -53,28 +36,32 @@ 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) 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)
{ {
std::cout << "Converting X to PyObject" << std::endl; std::cout << "PyClassifier:fit:Converting X to PyObject" << std::endl;
std::cout << "X.defined() = " << X.defined() << std::endl; std::cout << "X.defined() = " << X.defined() << std::endl;
//std::cout << "X.pyobj() = " << X.pyobj() << std::endl; int m = X.size(0);
//PyObject* Xp = torch::utils::tensor_to_numpy(X); int n = X.size(1);
auto XX = X.transpose(0, 1); auto data_numpy = np::from_data(X.data_ptr(), np::dtype::get_builtin<float>(), p::make_tuple(m, n), p::make_tuple(sizeof(X.dtype()) * 2 * n, sizeof(X.dtype()) * 2), p::object());
int m = XX.size(0); data_numpy = data_numpy.transpose();
int n = XX.size(1);
auto data_numpy = np::from_data(XX.data_ptr(), np::dtype::get_builtin<float>(), p::make_tuple(m, n), p::make_tuple(sizeof(XX.dtype()) * 2 * n, sizeof(XX.dtype()) * 2), p::object());
print_array(data_numpy); print_array(data_numpy);
CPyObject Xp = data_numpy.ptr(); CPyObject Xp = data_numpy.ptr();
std::cout << "Converting y to PyObject" << std::endl; std::cout << "PyClassifier:fit:Converting y to PyObject" << std::endl;
auto y_numpy = np::from_data(y.data_ptr(), np::dtype::get_builtin<int32_t>(), p::make_tuple(m), p::make_tuple(sizeof(y.dtype()) * 2), p::object()); auto y_numpy = np::from_data(y.data_ptr(), np::dtype::get_builtin<int32_t>(), p::make_tuple(n), p::make_tuple(sizeof(y.dtype()) * 2), p::object());
print_array(y_numpy);
CPyObject yp = y_numpy.ptr(); CPyObject yp = y_numpy.ptr();
std::cout << "Calling fit" << std::endl; std::cout << "PyClassifier:fit:Calling fit" << std::endl;
pyWrap->fit(module, this->className, Xp, yp); pyWrap->fit(module, this->className, Xp, yp);
return *this; return *this;
} }
torch::Tensor PyClassifier::predict(torch::Tensor& X) torch::Tensor PyClassifier::predict(torch::Tensor& X)
{ {
CPyObject Xp = toPyObject(X); int m = X.size(0);
int n = X.size(1);
auto data_numpy = np::from_data(X.data_ptr(), np::dtype::get_builtin<float>(), p::make_tuple(m, n), p::make_tuple(sizeof(X.dtype()) * 2 * n, sizeof(X.dtype()) * 2), p::object());
data_numpy = data_numpy.transpose();
print_array(data_numpy);
CPyObject Xp = data_numpy.ptr();
auto PyResult = pyWrap->predict(module, className, Xp); auto PyResult = pyWrap->predict(module, className, Xp);
auto result = THPVariable_Unpack(PyResult); auto result = torch::tensor({ 1,2,3 });
return result; return result;
} }
double PyClassifier::score(torch::Tensor& X, torch::Tensor& y) double PyClassifier::score(torch::Tensor& X, torch::Tensor& y)
@@ -95,5 +82,4 @@ namespace pywrap {
auto result = pyWrap->score(module, className, Xp, yp); auto result = pyWrap->score(module, className, Xp, yp);
return result; return result;
} }
} /* namespace pywrap */
} /* namespace PyWrap */

View File

@@ -15,9 +15,9 @@ namespace pywrap {
torch::Tensor predict(torch::Tensor& X); torch::Tensor predict(torch::Tensor& X);
double score(torch::Tensor& X, torch::Tensor& y); double score(torch::Tensor& X, torch::Tensor& y);
std::string version(); std::string version();
std::string graph();
std::string callMethodString(const std::string& method); std::string callMethodString(const std::string& method);
private: private:
PyObject* toPyObject(torch::Tensor& tensor);
PyWrap* pyWrap; PyWrap* pyWrap;
std::string module; std::string module;
std::string className; std::string className;

View File

@@ -3,7 +3,7 @@
#pragma once #pragma once
// Code taken and adapted from // Code taken and adapted from
// https ://www.codeproject.com/Articles/820116/Embedding-Python-program-in-a-C-Cplusplus-code // https ://www.codeproject.com/Articles/820116/Embedding-Python-program-in-a-C-Cplusplus-code
#include <iostream>
#include <Python.h> #include <Python.h>
#include <boost/python/numpy.hpp> #include <boost/python/numpy.hpp>
@@ -14,12 +14,14 @@ namespace pywrap {
public: public:
CPyInstance() CPyInstance()
{ {
std::cout << "PyHelper:Initializing Python interpreter" << std::endl;
Py_Initialize(); Py_Initialize();
np::initialize(); np::initialize();
} }
~CPyInstance() ~CPyInstance()
{ {
std::cout << "PyHelper:Finalizing Python interpreter" << std::endl;
Py_Finalize(); Py_Finalize();
} }
}; };

View File

@@ -10,6 +10,7 @@ namespace pywrap {
namespace np = boost::python::numpy; namespace np = boost::python::numpy;
PyWrap* PyWrap::wrapper = nullptr; PyWrap* PyWrap::wrapper = nullptr;
std::mutex PyWrap::mutex; std::mutex PyWrap::mutex;
CPyInstance* PyWrap::pyInstance = nullptr;
PyWrap* PyWrap::GetInstance() PyWrap* PyWrap::GetInstance()
{ {
@@ -17,27 +18,23 @@ namespace pywrap {
if (wrapper == nullptr) { if (wrapper == nullptr) {
std::cout << "Creando instancia" << std::endl; std::cout << "Creando instancia" << std::endl;
wrapper = new PyWrap(); wrapper = new PyWrap();
pyInstance = new CPyInstance();
std::cout << "Instancia creada" << std::endl; std::cout << "Instancia creada" << std::endl;
} }
return wrapper; return wrapper;
} }
void PyWrap::RemoveInstance()
PyWrap::PyWrap()
{ {
PyStatus status = initPython(); std::lock_guard<std::mutex> lock(mutex);
if (PyStatus_Exception(status)) { if (wrapper != nullptr) {
throw std::runtime_error("Error initializing Python"); std::cout << "Liberando instancia" << std::endl;
delete pyInstance;
pyInstance = nullptr;
delete wrapper;
wrapper = nullptr;
std::cout << "Instancia liberada" << std::endl;
} }
np::initialize();
} }
PyWrap::~PyWrap()
{
std::cout << "Destruyendo PyWrap" << std::endl;
Py_Finalize();
std::cout << "PyWrap destruido" << std::endl;
}
void PyWrap::importClass(const std::string& moduleName, const std::string& className) void PyWrap::importClass(const std::string& moduleName, const std::string& className)
{ {
std::cout << "Importando clase" << std::endl; std::cout << "Importando clase" << std::endl;
@@ -61,7 +58,6 @@ namespace pywrap {
moduleClassMap[{moduleName, className}] = { module, classObject, instance }; moduleClassMap[{moduleName, className}] = { module, classObject, instance };
std::cout << "Clase importada" << std::endl; std::cout << "Clase importada" << std::endl;
} }
void PyWrap::clean(const std::string& moduleName, const std::string& className) void PyWrap::clean(const std::string& moduleName, const std::string& className)
{ {
std::cout << "Limpiando" << std::endl; std::cout << "Limpiando" << std::endl;
@@ -71,14 +67,20 @@ namespace pywrap {
} }
std::cout << "--> Limpiando" << std::endl; std::cout << "--> Limpiando" << std::endl;
moduleClassMap.erase(result); moduleClassMap.erase(result);
if (PyErr_Occurred()) {
PyErr_Print();
errorAbort("Error cleaning module " + moduleName + " and class " + className);
}
if (moduleClassMap.empty()) {
RemoveInstance();
}
std::cout << "Limpieza terminada" << std::endl; std::cout << "Limpieza terminada" << std::endl;
} }
void PyWrap::errorAbort(const std::string& message) void PyWrap::errorAbort(const std::string& message)
{ {
std::cout << message << std::endl; std::cout << message << std::endl;
PyErr_Print(); PyErr_Print();
RemoveInstance();
exit(1); exit(1);
} }
PyObject* PyWrap::getClass(const std::string& moduleName, const std::string& className) PyObject* PyWrap::getClass(const std::string& moduleName, const std::string& className)
@@ -93,38 +95,62 @@ namespace pywrap {
std::string PyWrap::callMethodString(const std::string& moduleName, const std::string& className, const std::string& method) std::string PyWrap::callMethodString(const std::string& moduleName, const std::string& className, const std::string& method)
{ {
std::cout << "Llamando método " << method << std::endl; std::cout << "Llamando método " << method << std::endl;
CPyObject instance = getClass(moduleName, className); PyObject* instance = getClass(moduleName, className);
CPyObject result; PyObject* result;
try {
if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL))) if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL)))
errorAbort("Couldn't call method " + method); errorAbort("Couldn't call method " + method);
}
catch (const std::exception& e) {
std::cerr << e.what() << '\n';
RemoveInstance();
exit(1);
}
std::string value = PyUnicode_AsUTF8(result); std::string value = PyUnicode_AsUTF8(result);
std::cout << "Result: " << value << std::endl; std::cout << "Result: " << value << std::endl;
Py_DECREF(result);
return value; return value;
} }
std::string PyWrap::version(const std::string& moduleName, const std::string& className) std::string PyWrap::version(const std::string& moduleName, const std::string& className)
{ {
return callMethodString(moduleName, className, "version"); return callMethodString(moduleName, className, "version");
} }
std::string PyWrap::graph(const std::string& moduleName, const std::string& className)
{
return callMethodString(moduleName, className, "graph");
}
void PyWrap::fit(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y) void PyWrap::fit(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y)
{ {
std::cout << "Llamando método fit" << std::endl; std::cout << "Llamando método fit" << std::endl;
CPyObject instance = getClass(moduleName, className); CPyObject instance = getClass(moduleName, className);
CPyObject result; CPyObject result;
std::string method = "fit"; std::string method = "fit";
try {
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X, y, NULL))) if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X, y, NULL)))
errorAbort("Couldn't call method fit"); errorAbort("Couldn't call method fit");
} }
catch (const std::exception& e) {
std::cerr << e.what() << '\n';
RemoveInstance();
exit(1);
}
}
CPyObject PyWrap::predict(const std::string& moduleName, const std::string& className, CPyObject& X) CPyObject PyWrap::predict(const std::string& moduleName, const std::string& className, CPyObject& X)
{ {
std::cout << "Llamando método predict" << std::endl; std::cout << "Llamando método predict" << std::endl;
CPyObject instance = getClass(moduleName, className); CPyObject instance = getClass(moduleName, className);
CPyObject result; CPyObject result;
std::string method = "predict"; std::string method = "predict";
try {
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X, NULL))) if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X, NULL)))
errorAbort("Couldn't call method predict"); errorAbort("Couldn't call method predict");
return result; // The caller has to decref the result }
catch (const std::exception& e) {
std::cerr << e.what() << '\n';
RemoveInstance();
exit(1);
}
return result;
} }
double PyWrap::score(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y) double PyWrap::score(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y)
{ {
@@ -132,100 +158,15 @@ namespace pywrap {
CPyObject instance = getClass(moduleName, className); CPyObject instance = getClass(moduleName, className);
CPyObject result; CPyObject result;
std::string method = "score"; std::string method = "score";
try {
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X, y, NULL))) if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X, y, NULL)))
errorAbort("Couldn't call method score"); errorAbort("Couldn't call method score");
}
catch (const std::exception& e) {
std::cerr << e.what() << '\n';
RemoveInstance();
exit(1);
}
return PyFloat_AsDouble(result); return PyFloat_AsDouble(result);
} }
// void PyWrap::doCommand2()
// {
// PyObject* list = Py_BuildValue("[s]", "Stree");
// // PyObject* module = PyImport_ImportModuleEx("stree", NULL, NULL, list);
// PyObject* module = PyImport_ImportModule("stree");
// if (PyErr_Occurred()) {
// PyErr_Print();
// cout << "Fails to obtain the module.\n";
// return;
// }
// cout << "Antes de empezar" << endl;
// if (module != nullptr) {
// cout << "Lo consiguió!!!" << endl;
// // dict is a borrowed reference.
// auto pdict = PyModule_GetDict(module);
// if (pdict == nullptr) {
// cout << "Fails to get the dictionary.\n";
// return;
// }
// Py_DECREF(module);
// PyObject* pKeys = PyDict_Keys(pdict);
// PyObject* pValues = PyDict_Values(pdict);
// map<string, string> my_map;
// cout << "size: " << PyDict_Size(pdict) << endl;
// char* cstr_key = new char[100];
// char* cstr_value = new char[500];
// for (Py_ssize_t i = 0; i < PyDict_Size(pdict); ++i) {
// PyArg_Parse(PyList_GetItem(pKeys, i), "s", &cstr_key);
// PyArg_Parse(PyList_GetItem(pValues, i), "s", &cstr_value);
// //cout << cstr<< " "<< cstr2 <<endl;
// my_map.emplace(cstr_key, cstr_value);
// }
// for (auto x : my_map) {
// cout << x.first << " : " << x.second << endl;
// }
// // Builds the name of a callable class
// const char* class_name = "Stree";
// auto python_class = PyDict_GetItemString(pdict, class_name);
// // if (PyErr_Occurred()) {
// // PyErr_Print();
// // cout << "Fails to obtain the class.\n";
// // return;
// // }
// if (python_class == nullptr) {
// cout << "Fails to get the Python class.\n";
// return;
// }
// Py_DECREF(pdict);
// cout << "Clase: " << python_class << endl;
// PyObject* object;
// // Creates an instance of the class
// if (PyCallable_Check(python_class)) {
// cout << "Es callable" << endl;
// object = PyObject_CallObject(python_class, NULL);
// Py_DECREF(python_class);
// } else {
// std::cout << "Cannot instantiate the Python class" << endl;
// Py_DECREF(python_class);
// return;
// }
// if (PyErr_Occurred()) {
// PyErr_Print();
// cout << "Fails to create the Python object.\n";
// return;
// }
// auto val = PyObject_CallMethod(object, "version", NULL);
// if (val != nullptr) {
// cout << "Valor: " << val << endl;
// } else {
// cout << "No se pudo ejecutar" << endl;
// }
// } else {
// cout << "No lo consiguió :(" << endl;
// }
// Py_RunMain();
// }
PyStatus PyWrap::initPython()
{
PyStatus status;
PyConfig config;
PyConfig_InitPythonConfig(&config);
config.isolated = 0;
status = PyConfig_Read(&config);
if (PyStatus_Exception(status)) {
errorAbort("Error reading config");
}
status = Py_InitializeFromConfig(&config);
PyConfig_Clear(&config);
return status;
}
} }

View File

@@ -6,45 +6,36 @@
#include <tuple> #include <tuple>
#include <mutex> #include <mutex>
#include "PyHelper.hpp" #include "PyHelper.hpp"
#pragma once
namespace pywrap { namespace pywrap {
/* /*
Singleton class to handle Python interpreter. Singleton class to handle Python/numpy interpreter.
*/ */
class PyWrap { class PyWrap {
public: public:
PyWrap(PyWrap& other) = delete; PyWrap(PyWrap& other) = delete;
static PyWrap* GetInstance(); static PyWrap* GetInstance();
static void RemoveInstance();
void operator=(const PyWrap&) = delete; void operator=(const PyWrap&) = delete;
~PyWrap(); ~PyWrap() = default;
// template<typename T> T returnMethod(PyObject* result);
// template<std::string> std::string returnMethod(PyObject* result);
// template<int> int returnMethod(PyObject* result);
// template<bool> bool returnMethod(PyObject* result);
// template<torch::Tensor> torch::Tensor returnMethod(PyObject* result)
// {
// // PyObject* THPVariable_Wrap(at::Tensor t);
// // at::Tensor& THPVariable_Unpack(PyObject * obj);
// return THPVariable_Unpack(result);
// };
// PyObject* callMethodArgs(const std::string& moduleName, const std::string& className, const std::string& method, PyObject* args);
void fit(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y); void fit(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y);
CPyObject predict(const std::string& moduleName, const std::string& className, CPyObject& X); CPyObject predict(const std::string& moduleName, const std::string& className, CPyObject& X);
std::string callMethodString(const std::string& moduleName, const std::string& className, const std::string& method); std::string callMethodString(const std::string& moduleName, const std::string& className, const std::string& method);
std::string version(const std::string& moduleName, const std::string& className); std::string version(const std::string& moduleName, const std::string& className);
std::string graph(const std::string& moduleName, const std::string& className);
double score(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y); double score(const std::string& moduleName, const std::string& className, CPyObject& X, CPyObject& y);
void clean(const std::string& moduleName, const std::string& className); void clean(const std::string& moduleName, const std::string& className);
void importClass(const std::string& moduleName, const std::string& className); void importClass(const std::string& moduleName, const std::string& className);
// void doCommand2();
private: private:
PyWrap(); PyWrap() = default;
PyObject* getClass(const std::string& moduleName, const std::string& className); PyObject* getClass(const std::string& moduleName, const std::string& className);
void errorAbort(const std::string& message); void errorAbort(const std::string& message);
PyStatus initPython(); static CPyInstance* pyInstance;
static PyWrap* wrapper; static PyWrap* wrapper;
static std::mutex mutex; static std::mutex mutex;
std::map<std::pair<std::string, std::string>, std::tuple<CPyObject, CPyObject, CPyObject>> moduleClassMap; std::map<std::pair<std::string, std::string>, std::tuple<CPyObject, CPyObject, CPyObject>> moduleClassMap;
}; };
} /* namespace python */ } /* namespace pywrap */
#endif /* PYWRAP_H */ #endif /* PYWRAP_H */

View File

@@ -2,5 +2,9 @@
namespace pywrap { namespace pywrap {
std::string STree::graph()
{
// return callMethodString("graph");
return PyClassifier::graph();
}
} /* namespace pywrap */ } /* namespace pywrap */

View File

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

View File

@@ -13,13 +13,11 @@ namespace pywrap {
PyErr_Print(); PyErr_Print();
exit(1); exit(1);
} }
void print_array(np::ndarray& array) void print_array(np::ndarray& array)
{ {
std::cout << "Array: " << std::endl; std::cout << "Array: " << std::endl;
std::cout << p::extract<char const*>(p::str(array)) << std::endl; std::cout << p::extract<char const*>(p::str(array)) << std::endl;
} }
np::ndarray to_numpy_matrix(torch::Tensor& input_data, np::dtype numpy_dtype) np::ndarray to_numpy_matrix(torch::Tensor& input_data, np::dtype numpy_dtype)
{ {
p::tuple shape = p::make_tuple(input_data.size(0), input_data.size(1)); p::tuple shape = p::make_tuple(input_data.size(0), input_data.size(1));
@@ -135,7 +133,6 @@ int main(int argc, char** argv)
CPyObject result; CPyObject result;
if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL))) if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL)))
errorAbort("Couldn't call method " + method); errorAbort("Couldn't call method " + method);
std::string value = PyUnicode_AsUTF8(result); std::string value = PyUnicode_AsUTF8(result);
cout << "Version: " << value << endl; cout << "Version: " << value << endl;
cout << "Calling fit" << endl; cout << "Calling fit" << endl;

View File

@@ -46,7 +46,8 @@ int main(int argc, char* argv[])
auto stree = pywrap::STree(); auto stree = pywrap::STree();
stree.version(); stree.version();
auto svc = pywrap::SVC(); auto svc = pywrap::SVC();
svc.version(); //svc.version();
cout << "Graph: " << stree.graph() << endl;
stree.version(); stree.version();
cout << string(80, '-') << endl; cout << string(80, '-') << endl;
cout << "X: " << X.sizes() << endl; cout << "X: " << X.sizes() << endl;