diff --git a/runtime/onert/api/python/include/nnfw_session_bindings.h b/runtime/onert/api/python/include/nnfw_session_bindings.h new file mode 100644 index 00000000000..5052300880c --- /dev/null +++ b/runtime/onert/api/python/include/nnfw_session_bindings.h @@ -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 + +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__ diff --git a/runtime/onert/api/python/include/nnfw_tensorinfo_bindings.h b/runtime/onert/api/python/include/nnfw_tensorinfo_bindings.h new file mode 100644 index 00000000000..777abd72111 --- /dev/null +++ b/runtime/onert/api/python/include/nnfw_tensorinfo_bindings.h @@ -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 + +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__ diff --git a/runtime/onert/api/python/src/bindings/nnfw_api_wrapper_pybind.cc b/runtime/onert/api/python/src/bindings/nnfw_api_wrapper_pybind.cc new file mode 100644 index 00000000000..43c7a370a81 --- /dev/null +++ b/runtime/onert/api/python/src/bindings/nnfw_api_wrapper_pybind.cc @@ -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 + +#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); + +} diff --git a/runtime/onert/api/python/src/nnfw_api_wrapper_pybind.cc b/runtime/onert/api/python/src/bindings/nnfw_session_bindings.cc similarity index 91% rename from runtime/onert/api/python/src/nnfw_api_wrapper_pybind.cc rename to runtime/onert/api/python/src/bindings/nnfw_session_bindings.cc index bdea8270c16..166ae61d1b5 100644 --- a/runtime/onert/api/python/src/nnfw_api_wrapper_pybind.cc +++ b/runtime/onert/api/python/src/bindings/nnfw_session_bindings.cc @@ -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. @@ -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_(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_(m, "nnfw_session") +// Bind the `NNFW_SESSION` class with common inference APIs +void bind_nnfw_session(py::module_ &m) +{ + py::class_(m, "nnfw_session", py::module_local()) .def( py::init(), 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 diff --git a/runtime/onert/api/python/src/bindings/nnfw_tensorinfo_bindings.cc b/runtime/onert/api/python/src/bindings/nnfw_tensorinfo_bindings.cc new file mode 100644 index 00000000000..4fec778a62e --- /dev/null +++ b/runtime/onert/api/python/src/bindings/nnfw_tensorinfo_bindings.cc @@ -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_(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 diff --git a/runtime/onert/api/python/src/nnfw_api_wrapper.cc b/runtime/onert/api/python/src/wrapper/nnfw_api_wrapper.cc similarity index 100% rename from runtime/onert/api/python/src/nnfw_api_wrapper.cc rename to runtime/onert/api/python/src/wrapper/nnfw_api_wrapper.cc