Skip to content
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] Revisits python API #13583

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions docs/howto/how-to-use-nnfw-python-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ Please see [nnfw python api](https://github.com/SAMSUNG/ONE/tree/master/infra/nn
1. Initialize nnfw_session

```python
import 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)
else:
session = nnfw_session(nnpackage_path, backends)
session = onert.infer.session(nnpackage_path, backends)
```

2. Prepare Input
Expand Down
4 changes: 2 additions & 2 deletions infra/nnfw/python/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
build/
dist/
nnfwapi/
nnfwapi.egg-info/
onert/
onert.egg-info/
22 changes: 15 additions & 7 deletions infra/nnfw/python/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# nnfwapi package
# onert package

`nnfwapi` is a package to run `nnpackage` with the nnfw python API.
`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 `onert/infer.py` interface.

## Packaging
Execute this command, then the tasks such as copying modules, and packaging.
Expand Down Expand Up @@ -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 onert
```

Or you can import the onert module directly.

```
from onert.infer import *
```

This can be use onert session directly.
13 changes: 7 additions & 6 deletions infra/nnfw/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import sys

architecture_directory = ['x86_64', 'armv7l', 'aarch64']
package_directory = 'nnfwapi'
package_name = 'onert'
package_directory = 'onert'
packaging_directory = ['build', package_directory + '.egg-info']
THIS_FILE_DIR = os.path.dirname(os.path.abspath(__file__))
DEFAULT_PRODUCT_DIR = "../../../Product"
Expand Down Expand Up @@ -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}'")

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions runtime/onert/api/python/package/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__all__ = ['libnnfw_api_pybind']
from . import libnnfw_api_pybind
__all__ = ['infer']
from . import infer
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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"):
Expand Down
10 changes: 3 additions & 7 deletions runtime/onert/sample/minimal-python/src/minimal.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from nnfwapi.libnnfw_api_pybind import *
from onert import infer
import sys


def main(nnpackage_path, backends="cpu", operations=""):
def main(nnpackage_path, backends="cpu"):
# 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)
else:
session = nnfw_session(nnpackage_path, backends)
session = infer.session(nnpackage_path, backends)

# Prepare input. Here we just allocate dummy input arrays.
input_size = session.input_size()
Expand Down