-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[onert/python] Modularize python binding code. #14529
Merged
hseok-oh
merged 1 commit into
Samsung:master
from
ragmani:onert/python/modularize_binding_code
Jan 8, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
runtime/onert/api/python/include/nnfw_tensorinfo_bindings.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |
38 changes: 38 additions & 0 deletions
38
runtime/onert/api/python/src/bindings/nnfw_api_wrapper_pybind.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file is created newly in this PR. |
||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -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, " | ||
|
@@ -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") | ||
|
@@ -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
46
runtime/onert/api/python/src/bindings/nnfw_tensorinfo_bindings.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file already exists. So I keep up the year.