-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathpython_backend.cpp
122 lines (112 loc) · 4.76 KB
/
python_backend.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//*****************************************************************************
// Copyright 2023 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include "python_backend.hpp"
#pragma warning(push)
#pragma warning(disable : 6326 28182 6011 28020)
#include <pybind11/stl.h>
#pragma warning(pop)
#include "../logging.hpp"
namespace py = pybind11;
using namespace py::literals;
using namespace ovms;
#pragma warning(disable : 4101)
bool PythonBackend::createPythonBackend(std::unique_ptr<PythonBackend>& pythonBackend) {
py::gil_scoped_acquire acquire;
try {
pythonBackend = std::make_unique<PythonBackend>();
} catch (const pybind11::error_already_set& e) {
SPDLOG_DEBUG("PythonBackend initialization failed: {}", e.what());
return false;
} catch (std::exception& e) {
SPDLOG_DEBUG("PythonBackend initialization failed: {}", e.what());
return false;
}
return true;
}
PythonBackend::PythonBackend() {
py::gil_scoped_acquire acquire;
SPDLOG_DEBUG("Creating python backend");
pyovmsModule = std::make_unique<py::module_>(py::module_::import("pyovms"));
tensorClass = std::make_unique<py::object>(pyovmsModule->attr("Tensor"));
}
PythonBackend::~PythonBackend() {
SPDLOG_DEBUG("Python backend destructor start");
py::gil_scoped_acquire acquire;
tensorClass.reset();
pyovmsModule.reset();
SPDLOG_DEBUG("Python backend destructor end");
}
bool PythonBackend::createOvmsPyTensor(const std::string& name, void* ptr, const std::vector<py::ssize_t>& shape,
const std::string& datatype, py::ssize_t size, std::unique_ptr<PyObjectWrapper<py::object>>& outTensor, bool copy) {
py::gil_scoped_acquire acquire;
try {
py::object ovmsPyTensor = tensorClass->attr("_create_from_data")(name, ptr, shape, datatype, size, copy);
outTensor = std::make_unique<PyObjectWrapper<py::object>>(ovmsPyTensor);
return true;
} catch (const pybind11::error_already_set& e) {
SPDLOG_DEBUG("PythonBackend::createOvmsPyTensor - Py Error: {}", e.what());
return false;
} catch (std::exception& e) {
SPDLOG_DEBUG("PythonBackend::createOvmsPyTensor - Error: {}", e.what());
return false;
} catch (...) {
SPDLOG_DEBUG("PythonBackend::createOvmsPyTensor - Unknown Error");
return false;
}
return false;
}
bool PythonBackend::createEmptyOvmsPyTensor(const std::string& name, const std::vector<py::ssize_t>& shape, const std::string& datatype,
py::ssize_t size, std::unique_ptr<PyObjectWrapper<py::object>>& outTensor) {
py::gil_scoped_acquire acquire;
try {
py::object ovmsPyTensor = tensorClass->attr("_create_without_data")(name, shape, datatype, size);
outTensor = std::make_unique<PyObjectWrapper<py::object>>(ovmsPyTensor);
return true;
} catch (const pybind11::error_already_set& e) {
SPDLOG_ERROR("TENSOR {}", size);
SPDLOG_DEBUG("PythonBackend::createEmptyOvmsPyTensor - Py Error: {}", e.what());
return false;
} catch (std::exception& e) {
SPDLOG_DEBUG("PythonBackend::createEmptyOvmsPyTensor - Error: {}", e.what());
return false;
} catch (...) {
SPDLOG_DEBUG("PythonBackend::createEmptyOvmsPyTensor - Unknown Error");
return false;
}
return false;
}
void PythonBackend::validateOvmsPyTensor(const py::object& object) const {
py::gil_scoped_acquire acquire;
if (!py::isinstance(object, *tensorClass)) {
throw UnexpectedPythonObjectError(object, tensorClass->attr("__name__").cast<std::string>());
}
}
bool PythonBackend::getOvmsPyTensorData(std::unique_ptr<PyObjectWrapper<py::object>>& outTensor, void** data) {
py::gil_scoped_acquire acquire;
try {
*data = outTensor->getProperty<void*>("ptr");
return true;
} catch (const pybind11::error_already_set& e) {
SPDLOG_DEBUG("PythonBackend::getOvmsPyTensorData - Py Error: {}", e.what());
return false;
} catch (std::exception& e) {
SPDLOG_DEBUG("PythonBackend::getOvmsPyTensorData - Error: {}", e.what());
return false;
} catch (...) {
SPDLOG_DEBUG("PythonBackend::getOvmsPyTensorData - Unknown Error");
return false;
}
}