Skip to content

Commit

Permalink
[onert/python] Modularize python binding code.
Browse files Browse the repository at this point in the history
This commit modularizes the python binding code.
  - Decompose binding code into session and tensorinfo
  - Decompose files to session and tensorinfo

ONE-DCO-1.0-Signed-off-by: ragmani <[email protected]>
  • Loading branch information
ragmani committed Jan 7, 2025
1 parent d978911 commit ae0a4a9
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 16 deletions.
36 changes: 36 additions & 0 deletions runtime/onert/api/python/include/nnfw_session_bindings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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.
*/

#ifndef __ONERT_API_PYTHON_NNFW_SESSION_BINDINGS_H__
#define __ONERT_API_PYTHON_NNFW_SESSION_BINDINGS_H__

#include <pybind11/pybind11.h>

namespace onert
{
namespace api
{
namespace python
{

// Declare binding common functions
void bind_nnfw_session(pybind11::module_ &m);

} // namespace python
} // namespace api
} // namespace onert

#endif // __ONERT_API_PYTHON_NNFW_SESSION_BINDINGS_H__
36 changes: 36 additions & 0 deletions runtime/onert/api/python/include/nnfw_tensorinfo_bindings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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.
*/

#ifndef __ONERT_API_PYTHON_NNFW_TENSORINFO_BINDINGS_H__
#define __ONERT_API_PYTHON_NNFW_TENSORINFO_BINDINGS_H__

#include <pybind11/pybind11.h>

namespace onert
{
namespace api
{
namespace python
{

// Declare binding tensorinfo
void bind_tensorinfo(pybind11::module_ &m);

} // namespace python
} // namespace api
} // namespace onert

#endif // __ONERT_API_PYTHON_NNFW_TENSORINFO_BINDINGS_H__
39 changes: 39 additions & 0 deletions runtime/onert/api/python/src/bindings/nnfw_api_wrapper_pybind.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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 "nnfw_session_bindings.h"
#include "nnfw_tensorinfo_bindings.h"

using namespace onert::api::python;

PYBIND11_MODULE(libnnfw_api_pybind, m)
{
m.doc() = "Main module that contains infer and experimental submodules";

// Bind common `NNFW_SESSION` class
bind_nnfw_session(m);

// Bind `NNFW_SESSION` class for inference
// Currently, the `infer` session is the same as common.
auto infer = m.def_submodule("infer", "Inference submodule");
infer.attr("nnfw_session") = m.attr("nnfw_session");

// Bind common `tensorinfo` class
bind_tensorinfo(m);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,26 +14,23 @@
* limitations under the License.
*/

#include "nnfw_api_wrapper.h"

namespace py = pybind11;
#include "nnfw_session_bindings.h"

using namespace onert::api::python;
#include "nnfw_api_wrapper.h"

PYBIND11_MODULE(libnnfw_api_pybind, m)
namespace onert
{
namespace api
{
namespace python
{
m.doc() = "nnfw python plugin";

py::class_<tensorinfo>(m, "tensorinfo", "tensorinfo describes the type and shape of tensors")
.def(py::init<>(), "The constructor of tensorinfo")
.def_readwrite("dtype", &tensorinfo::dtype, "The data type")
.def_readwrite("rank", &tensorinfo::rank, "The number of dimensions (rank)")
.def_property(
"dims", [](const tensorinfo &ti) { return get_dims(ti); },
[](tensorinfo &ti, const py::list &dims_list) { set_dims(ti, dims_list); },
"The dimension of tensor. Maximum rank is 6 (NNFW_MAX_RANK).");
namespace py = pybind11;

py::class_<NNFW_SESSION>(m, "nnfw_session")
// Bind the `NNFW_SESSION` class with common inference APIs
void bind_nnfw_session(py::module_ &m)
{
py::class_<NNFW_SESSION>(m, "nnfw_session", py::module_local())
.def(
py::init<const char *, const char *>(), py::arg("package_file_path"), py::arg("backends"),
"Create a new session instance, load model from nnpackage file or directory, "
Expand All @@ -50,6 +47,7 @@ PYBIND11_MODULE(libnnfw_api_pybind, m)
"Parameters:\n"
"\tindex (int): Index of input to be set (0-indexed)\n"
"\ttensor_info (tensorinfo): Tensor info to be set")
.def("prepare", &NNFW_SESSION::prepare, "Prepare for inference")
.def("run", &NNFW_SESSION::run, "Run inference")
.def("run_async", &NNFW_SESSION::run_async, "Run inference asynchronously")
.def("wait", &NNFW_SESSION::wait, "Wait for asynchronous run to finish")
Expand Down Expand Up @@ -226,3 +224,7 @@ PYBIND11_MODULE(libnnfw_api_pybind, m)
"Returns:\n"
"\ttensorinfo: Tensor info (shape, type, etc)");
}

} // namespace python
} // namespace api
} // namespace onert
46 changes: 46 additions & 0 deletions runtime/onert/api/python/src/bindings/nnfw_tensorinfo_bindings.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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 "nnfw_tensorinfo_bindings.h"

#include "nnfw_api_wrapper.h"

namespace onert
{
namespace api
{
namespace python
{

namespace py = pybind11;

// Bind the `tensorinfo` class
void bind_tensorinfo(py::module_ &m)
{
py::class_<tensorinfo>(m, "tensorinfo", "tensorinfo describes the type and shape of tensors",
py::module_local())
.def(py::init<>(), "The constructor of tensorinfo")
.def_readwrite("dtype", &tensorinfo::dtype, "The data type")
.def_readwrite("rank", &tensorinfo::rank, "The number of dimensions (rank)")
.def_property(
"dims", [](const tensorinfo &ti) { return get_dims(ti); },
[](tensorinfo &ti, const py::list &dims_list) { set_dims(ti, dims_list); },
"The dimension of tensor. Maximum rank is 6 (NNFW_MAX_RANK).");
}

} // namespace python
} // namespace api
} // namespace onert

0 comments on commit ae0a4a9

Please sign in to comment.