From 03338f4df9f2e2b101ebc804add4273bdb516c45 Mon Sep 17 00:00:00 2001 From: Hyeongseok Oh Date: Fri, 2 Aug 2024 17:05:07 +0900 Subject: [PATCH] Draft: revise python API - Rename python package: nnfwapi -> nnfw-onert - Native library install path: "nnfwapi/onert" -> "nnfw_onert/natvie" - Rename binding file: "libnnfw_api_pybind.py" -> "onert.py" - Rename binding session class: "nnfw_session" -> "session" - Update documentation - Update example --- docs/howto/how-to-use-nnfw-python-api.md | 6 ++++-- infra/nnfw/python/.gitignore | 4 ++-- infra/nnfw/python/README.md | 20 +++++++++++++------ infra/nnfw/python/setup.py | 13 ++++++------ runtime/onert/api/python/package/__init__.py | 4 ++-- .../{libnnfw_api_pybind.py => onert.py} | 4 ++-- .../sample/minimal-python/src/minimal.py | 6 +++--- 7 files changed, 34 insertions(+), 23 deletions(-) rename runtime/onert/api/python/package/{libnnfw_api_pybind.py => onert.py} (95%) diff --git a/docs/howto/how-to-use-nnfw-python-api.md b/docs/howto/how-to-use-nnfw-python-api.md index 5d5b839cc63..f3b9aecc9b6 100644 --- a/docs/howto/how-to-use-nnfw-python-api.md +++ b/docs/howto/how-to-use-nnfw-python-api.md @@ -17,13 +17,15 @@ Please see [nnfw python api](https://github.com/SAMSUNG/ONE/tree/master/infra/nn 1. Initialize nnfw_session ```python +import nnfw_onert + # Create session and load nnpackage # operations is optional to assign a specific backends to each operation. # The default value of backends is "cpu". if operations: - session = nnfw_session(nnpackage_path, backends, operations) + session = nnfw_onert.onert.session(nnpackage_path, backends, operations) else: - session = nnfw_session(nnpackage_path, backends) + session = nnfw_onert.onert.session(nnpackage_path, backends) ``` 2. Prepare Input diff --git a/infra/nnfw/python/.gitignore b/infra/nnfw/python/.gitignore index 15d72def6a6..cb4ee5f7e8c 100644 --- a/infra/nnfw/python/.gitignore +++ b/infra/nnfw/python/.gitignore @@ -1,4 +1,4 @@ build/ dist/ -nnfwapi/ -nnfwapi.egg-info/ +nnfw_onert/ +nnfw_onert.egg-info/ diff --git a/infra/nnfw/python/README.md b/infra/nnfw/python/README.md index 2a5942e5eb3..b45132d51db 100644 --- a/infra/nnfw/python/README.md +++ b/infra/nnfw/python/README.md @@ -1,12 +1,12 @@ # nnfwapi package -`nnfwapi` is a package to run `nnpackage` with the nnfw python API. +`nnfw-onert` is a package to run `nnpackage` with the nnfw onert's python API. -This package includes the nnfw python API module resulting from runtime build. +This package includes the nnfw-onert python API module resulting from runtime build. It is provided separate package for each architecture(x86_64, armv7l, aarch64). -It uses `nnfwapi/libnnfw_api_pybind.py` interface. +It uses `nnfw_onert/onert.py` interface. ## Packaging Execute this command, then the tasks such as copying modules, and packaging. @@ -39,17 +39,25 @@ $ twine upload --repository-url https://test.pypi.org/legacy/ dist/* You can install the package as follows: ``` -$ pip install -i https://test.pypi.org/simple/ nnfwapi +$ pip install -i https://test.pypi.org/simple/ nnfw-onert ``` By specifying the version, you can use a specific version of the package. (recommended) ``` -$ pip install -i https://test.pypi.org/simple/ nnfwapi==0.1.1 +$ pip install -i https://test.pypi.org/simple/ nnfw-onert==0.1.1 ``` This definition has to be set at the top of the script using nnfw python API. ``` -from nnfwapi.libnnfw_api_pybind import * +import nnfw_onert ``` + +Or you can import the onert module directly. + +``` +from nnfw_onert.onert import * +``` + +This can be use onert session directly. diff --git a/infra/nnfw/python/setup.py b/infra/nnfw/python/setup.py index 8217c1219fb..0bc7c1e1b32 100644 --- a/infra/nnfw/python/setup.py +++ b/infra/nnfw/python/setup.py @@ -4,7 +4,8 @@ import sys architecture_directory = ['x86_64', 'armv7l', 'aarch64'] -package_directory = 'nnfwapi' +package_name = 'nnfw-onert' +package_directory = 'nnfw_onert' packaging_directory = ['build', package_directory + '.egg-info'] THIS_FILE_DIR = os.path.dirname(os.path.abspath(__file__)) DEFAULT_PRODUCT_DIR = "../../../Product" @@ -66,7 +67,7 @@ shutil.rmtree(arch_path) # make architecture_directory and copy .so files to the directories - arch_path = os.path.join(package_directory, 'onert') + arch_path = os.path.join(package_directory, 'native') os.makedirs(arch_path) print(f"Created directory '{arch_path}'") @@ -81,7 +82,7 @@ def get_directories(): for so in os.listdir(so_core_dir): if so.endswith(".so"): - so_list.append('onert/' + so) + so_list.append('native/' + so) src_path = os.path.join(so_core_dir, so) tgt_path = os.path.join(arch_path, so) shutil.copy(src_path, tgt_path) @@ -94,7 +95,7 @@ def get_directories(): os.makedirs(so_backend_tgt_dir) for so in os.listdir(so_backend_dir): if so.endswith(".so"): - so_list.append('onert/nnfw/backend/' + so) + so_list.append('native/nnfw/backend/' + so) src_path = os.path.join(so_backend_dir, so) tgt_path = os.path.join(so_backend_tgt_dir, so) shutil.copy(src_path, tgt_path) @@ -107,7 +108,7 @@ def get_directories(): os.makedirs(so_odc_tgt_dir) for so in os.listdir(so_odc_dir): if so.endswith(".so"): - so_list.append('onert/nnfw/odc/' + so) + so_list.append('native/nnfw/odc/' + so) src_path = os.path.join(so_odc_dir, so) tgt_path = os.path.join(so_odc_tgt_dir, so) shutil.copy(src_path, tgt_path) @@ -121,7 +122,7 @@ def get_directories(): # copy .so files to architecture directories setup( - name=package_directory, + name=package_name, version='0.1.0', description='nnfw_api binding', long_description='It provides nnfw Python api', diff --git a/runtime/onert/api/python/package/__init__.py b/runtime/onert/api/python/package/__init__.py index cd1eaccc9d3..99eb033539a 100644 --- a/runtime/onert/api/python/package/__init__.py +++ b/runtime/onert/api/python/package/__init__.py @@ -1,2 +1,2 @@ -__all__ = ['libnnfw_api_pybind'] -from . import libnnfw_api_pybind +__all__ = ['onert'] +from . import onert diff --git a/runtime/onert/api/python/package/libnnfw_api_pybind.py b/runtime/onert/api/python/package/onert.py similarity index 95% rename from runtime/onert/api/python/package/libnnfw_api_pybind.py rename to runtime/onert/api/python/package/onert.py index f9d51e91dc8..146c4b03efc 100644 --- a/runtime/onert/api/python/package/libnnfw_api_pybind.py +++ b/runtime/onert/api/python/package/onert.py @@ -2,7 +2,7 @@ import os import shutil -from .onert import libnnfw_api_pybind +from .native import libnnfw_api_pybind def num_elems(tensor_info): @@ -13,7 +13,7 @@ def num_elems(tensor_info): return n -class nnfw_session(libnnfw_api_pybind.nnfw_session): +class session(libnnfw_api_pybind.nnfw_session): """Class inherited nnfw_session for easily processing input/output""" def __init__(self, nnpackage_path, backends="cpu"): diff --git a/runtime/onert/sample/minimal-python/src/minimal.py b/runtime/onert/sample/minimal-python/src/minimal.py index 60e8569b798..cb37e6ae2d6 100644 --- a/runtime/onert/sample/minimal-python/src/minimal.py +++ b/runtime/onert/sample/minimal-python/src/minimal.py @@ -1,4 +1,4 @@ -from nnfwapi.libnnfw_api_pybind import * +from nnfw_onert import onert import sys @@ -7,9 +7,9 @@ def main(nnpackage_path, backends="cpu", operations=""): # operations is optional to assign a specific backends to each operation. # The default value of backends is "cpu". if operations: - session = nnfw_session(nnpackage_path, backends, operations) + session = onert.session(nnpackage_path, backends, operations) else: - session = nnfw_session(nnpackage_path, backends) + session = onert.session(nnpackage_path, backends) # Prepare input. Here we just allocate dummy input arrays. input_size = session.input_size()