Skip to content

Commit

Permalink
Draft: revise python API
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
hseok-oh committed Aug 2, 2024
1 parent 854658f commit 0597487
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 23 deletions.
6 changes: 4 additions & 2 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,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
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/
nnfw_onert/
nnfw_onert.egg-info/
20 changes: 14 additions & 6 deletions infra/nnfw/python/README.md
Original file line number Diff line number Diff line change
@@ -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.
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 nnfw_onert
```

Or you can import the onert module directly.

```
from nnfw_onert.onert 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 = '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"
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__ = ['onert']
from . import onert
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
6 changes: 3 additions & 3 deletions runtime/onert/sample/minimal-python/src/minimal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from nnfwapi.libnnfw_api_pybind import *
from nnfw_onert import onert
import sys


Expand All @@ -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()
Expand Down

0 comments on commit 0597487

Please sign in to comment.