This repository was archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 218
Expand file tree
/
Copy pathfunction.cpp
More file actions
109 lines (99 loc) · 5.12 KB
/
function.cpp
File metadata and controls
109 lines (99 loc) · 5.12 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
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
//*****************************************************************************
// Copyright 2017-2020 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 <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "ngraph/function.hpp" // ngraph::Function
#include "ngraph/op/parameter.hpp" // ngraph::op::v0::Parameter
#include "ngraph/output_vector.hpp"
#include "pyngraph/function.hpp"
namespace py = pybind11;
static const char* CAPSULE_NAME = "ngraph_function";
void regclass_pyngraph_Function(py::module m)
{
py::class_<ngraph::Function, std::shared_ptr<ngraph::Function>> function(m, "Function");
function.doc() = "ngraph.impl.Function wraps ngraph::Function";
function.def(py::init([](const ngraph::NodeVector& outputs,
const ngraph::ParameterVector& in,
const std::string& name) {
ngraph::OutputVector ov;
for (std::shared_ptr<ngraph::Node> output : outputs)
{
ov.push_back(output->output(0));
}
return std::make_shared<ngraph::Function>(ov, in, name);
}));
function.def(py::init<const std::vector<ngraph::Output<ngraph::Node>>&,
const ngraph::ParameterVector&,
const std::string&>());
function.def(py::init([](const std::shared_ptr<ngraph::Node>& output,
const ngraph::ParameterVector& in,
const std::string& name) {
return std::make_shared<ngraph::Function>(output->outputs(), in, name);
}));
function.def("get_output_size", &ngraph::Function::get_output_size);
function.def("get_ops", &ngraph::Function::get_ops);
function.def("get_ordered_ops", &ngraph::Function::get_ordered_ops);
function.def("get_output_op", &ngraph::Function::get_output_op);
function.def("get_output_element_type", &ngraph::Function::get_output_element_type);
function.def("get_output_shape", &ngraph::Function::get_output_shape);
function.def("get_output_partial_shape", &ngraph::Function::get_output_partial_shape);
function.def("get_parameters", &ngraph::Function::get_parameters);
function.def("get_results", &ngraph::Function::get_results);
function.def("get_result", &ngraph::Function::get_result);
function.def("get_unique_name", &ngraph::Function::get_name);
function.def("get_name", &ngraph::Function::get_friendly_name);
function.def("set_friendly_name", &ngraph::Function::set_friendly_name);
function.def("is_dynamic", &ngraph::Function::is_dynamic);
function.def("__repr__", [](const ngraph::Function& self) {
std::string class_name = py::cast(self).get_type().attr("__name__").cast<std::string>();
std::string shape =
py::cast(self.get_output_shape(0)).attr("__str__")().cast<std::string>();
return "<" + class_name + ": '" + self.get_friendly_name() + "' (" + shape + ")>";
});
function.def_static("from_capsule", [](py::object* capsule) {
// get the underlying PyObject* which is a PyCapsule pointer
auto* pybind_capsule_ptr = capsule->ptr();
// extract the pointer stored in the PyCapsule under the name CAPSULE_NAME
auto* capsule_ptr = PyCapsule_GetPointer(pybind_capsule_ptr, CAPSULE_NAME);
auto* ngraph_function = static_cast<std::shared_ptr<ngraph::Function>*>(capsule_ptr);
if (ngraph_function && *ngraph_function)
{
return *ngraph_function;
}
else
{
throw std::runtime_error("The provided capsule does not contain an ngraph::Function");
}
});
function.def_static("to_capsule", [](std::shared_ptr<ngraph::Function>& ngraph_function) {
// create a shared pointer on the heap before putting it in the capsule
// this secures the lifetime of the object transferred by the capsule
auto* sp_copy = new std::shared_ptr<ngraph::Function>(ngraph_function);
// a destructor callback that will delete the heap allocated shared_ptr
// when the capsule is destructed
auto sp_deleter = [](PyObject* capsule) {
auto* capsule_ptr = PyCapsule_GetPointer(capsule, CAPSULE_NAME);
auto* function_sp = static_cast<std::shared_ptr<ngraph::Function>*>(capsule_ptr);
if (function_sp)
{
delete function_sp;
}
};
// put the shared_ptr in a new capsule under the same name as in "from_capsule"
auto pybind_capsule = py::capsule(sp_copy, CAPSULE_NAME, sp_deleter);
return pybind_capsule;
});
}