function.cpp 4.34 KB
Newer Older
1
//*****************************************************************************
2
// Copyright 2017-2020 Intel Corporation
3 4 5 6 7 8 9 10 11 12 13 14 15
//
// 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.
//*****************************************************************************
16 17 18

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
19

20 21
#include "ngraph/function.hpp"     // ngraph::Function
#include "ngraph/op/parameter.hpp" // ngraph::op::Parameter
22 23 24 25
#include "pyngraph/function.hpp"

namespace py = pybind11;

26 27
static const char* CAPSULE_NAME = "ngraph_function";

28 29 30
void regclass_pyngraph_Function(py::module m)
{
    py::class_<ngraph::Function, std::shared_ptr<ngraph::Function>> function(m, "Function");
31
    function.doc() = "ngraph.impl.Function wraps ngraph::Function";
32
    function.def(py::init<const std::vector<std::shared_ptr<ngraph::Node>>&,
33 34 35 36 37 38 39 40 41 42 43 44
                          const std::vector<std::shared_ptr<ngraph::op::Parameter>>&,
                          const std::string&>());
    function.def(py::init<const std::shared_ptr<ngraph::Node>&,
                          const std::vector<std::shared_ptr<ngraph::op::Parameter>>&,
                          const std::string&>());
    function.def("get_output_size", &ngraph::Function::get_output_size);
    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_parameters", &ngraph::Function::get_parameters);
    function.def("get_results", &ngraph::Function::get_results);
    function.def("get_result", &ngraph::Function::get_result);
45 46
    function.def("get_unique_name", &ngraph::Function::get_name);
    function.def("get_name", &ngraph::Function::get_friendly_name);
47
    function.def("set_friendly_name", &ngraph::Function::set_friendly_name);
48 49 50 51 52 53
    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 + ")>";
    });
54
    function.def_static("from_capsule", [](py::object* capsule) {
tomdol's avatar
tomdol committed
55
        // get the underlying PyObject* which is a PyCapsule pointer
56
        auto* pybind_capsule_ptr = capsule->ptr();
tomdol's avatar
tomdol committed
57
        // extract the pointer stored in the PyCapsule under the name CAPSULE_NAME
58 59
        auto* capsule_ptr = PyCapsule_GetPointer(pybind_capsule_ptr, CAPSULE_NAME);

tomdol's avatar
tomdol committed
60
        auto* ngraph_function = static_cast<std::shared_ptr<ngraph::Function>*>(capsule_ptr);
61
        if (ngraph_function && *ngraph_function)
tomdol's avatar
tomdol committed
62 63 64 65 66
        {
            return *ngraph_function;
        }
        else
        {
67
            throw std::runtime_error("The provided capsule does not contain an ngraph::Function");
tomdol's avatar
tomdol committed
68
        }
69 70
    });
    function.def_static("to_capsule", [](std::shared_ptr<ngraph::Function>& ngraph_function) {
tomdol's avatar
tomdol committed
71 72
        // create a shared pointer on the heap before putting it in the capsule
        // this secures the lifetime of the object transferred by the capsule
73
        auto* sp_copy = new std::shared_ptr<ngraph::Function>(ngraph_function);
tomdol's avatar
tomdol committed
74 75 76 77

        // a destructor callback that will delete the heap allocated shared_ptr
        // when the capsule is destructed
        auto sp_deleter = [](PyObject* capsule) {
78 79 80 81 82 83
            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;
            }
tomdol's avatar
tomdol committed
84 85 86 87
        };

        // 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);
88 89 90

        return pybind_capsule;
    });
91
}