-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathpython_backend.hpp
More file actions
63 lines (51 loc) · 2.36 KB
/
python_backend.hpp
File metadata and controls
63 lines (51 loc) · 2.36 KB
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
//*****************************************************************************
// 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.
//*****************************************************************************
#pragma once
#include <memory>
#include <string>
#include <vector>
#pragma warning(push)
#pragma warning(disable : 6326 28182 6011 28020)
#include <pybind11/embed.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#pragma warning(pop)
#include "utils.hpp"
namespace py = pybind11;
using namespace py::literals;
namespace ovms {
#if defined(_WIN32)
#define PYTHON_BACKEND_EXPORT __declspec(dllexport)
#else
#define PYTHON_BACKEND_EXPORT __attribute__((visibility("default")))
#endif
class PythonBackend {
std::unique_ptr<py::module_> pyovmsModule;
std::unique_ptr<py::object> tensorClass;
public:
PYTHON_BACKEND_EXPORT PythonBackend();
PYTHON_BACKEND_EXPORT ~PythonBackend();
PYTHON_BACKEND_EXPORT static bool createPythonBackend(std::unique_ptr<PythonBackend>& pythonBackend);
PYTHON_BACKEND_EXPORT bool 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 = false);
PYTHON_BACKEND_EXPORT bool 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);
// Checks if object is tensorClass instance. Throws UnexpectedPythonObjectError if it's not.
PYTHON_BACKEND_EXPORT void validateOvmsPyTensor(const py::object& object) const;
PYTHON_BACKEND_EXPORT bool getOvmsPyTensorData(std::unique_ptr<PyObjectWrapper<py::object>>& outTensor, void** data);
};
#undef PYTHON_BACKEND_EXPORT
} // namespace ovms