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

Add NBG runner python binding #677

Merged
merged 1 commit into from
Jan 10, 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
4 changes: 4 additions & 0 deletions samples/nbg_runner_pybind/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BasedOnStyle: Google
PointerAlignment: Left
DerivePointerAlignment: false
ColumnLimit: 80
69 changes: 69 additions & 0 deletions samples/nbg_runner_pybind/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
Checks: >
-*,
bugprone-*,
-bugprone-easily-swappable-parameters,
performance-*,
portability-*,
readability-*,
modernize-*,
-modernize-loop-convert,
-modernize-use-trailing-return-type,
clang-analyzer-*,
misc-*,
google-*,

HeaderFilterRegex: "(src/).*\\.(hpp|h)$"

CheckOptions:
- key: readability-identifier-naming.NamespaceCase
value: snake_case
- key: readability-identifier-naming.ClassCase
value: CamelCase
- key: readability-identifier-naming.StructCase
value: CamelCase
- key: readability-identifier-naming.EnumCase
value: CamelCase
- key: readability-identifier-naming.TemplateParameterCase
value: CamelCase
- key: readability-identifier-naming.FunctionCase
value: lower_case
- key: readability-identifier-naming.VariableCase
value: lower_case
- key: readability-identifier-naming.ClassMemberCase
value: lower_case
- key: readability-identifier-naming.ClassMemberSuffix
value: _
- key: readability-identifier-naming.PrivateMemberCase
value: lower_case
- key: readability-identifier-naming.PrivateMemberSuffix
value: _
- key: readability-identifier-naming.ClassMethodCase
value: lower_case
- key: readability-identifier-naming.EnumConstantCase
value: CamelCase
- key: readability-identifier-naming.EnumConstantPrefix
value: k
- key: readability-identifier-naming.ConstexprVariableCase
value: CamelCase
- key: readability-identifier-naming.ConstexprVariablePrefix
value: k
- key: readability-identifier-naming.GlobalConstantCase
value: CamelCase
- key: readability-identifier-naming.GlobalConstantPrefix
value: k
- key: readability-identifier-naming.MemberConstantCase
value: CamelCase
- key: readability-identifier-naming.MemberConstantPrefix
value: k
- key: readability-identifier-naming.StaticConstantCase
value: CamelCase
- key: readability-identifier-naming.StaticConstantPrefix
value: k
- key: readability-identifier-length.IgnoredVariableNames
value: "^c|d|e|m|op|fd|it$"
- key: readability-identifier-length.IgnoredParameterNames
value: "^c|d|e|m|op|fd|it$"
- key: readability-function-cognitive-complexity.IgnoreMacros
value: true

FormatStyle: file
15 changes: 15 additions & 0 deletions samples/nbg_runner_pybind/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[Bb]uild/
*_build/
.vscode/
.cache/
__pycache__/
*.pyc
temp/
tmp/
*.o
*.a
*.lib
*.dll
*.so
*.nb
.env
23 changes: 23 additions & 0 deletions samples/nbg_runner_pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.18)
project(nbg_runner LANGUAGES C CXX)

# Set C/C++ standard.
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Enable warnings as errors.
add_compile_options(-Wall -Werror)

# Add custom CMake modules.
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/modules)

# Find dependencies.
include(FindPyBind11)
include(FindVivanteSDK)

add_subdirectory(src)
84 changes: 84 additions & 0 deletions samples/nbg_runner_pybind/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# VSI NBG Runner Python Binding

This project is a python package that wraps OpenVX API using pybind11. It provides simple python API to load, query and run model NBG.

## Installation

```cmake
cmake -B build -DVIVANTE_SDK_DIR=${VIV_SDK_INSTALL_PATH}
cmake --build build
```

The built python binding lib can be found at
`build/src/_nbg_runner.cpython-{python_version}-{platform}.so`. Place the lib into `python/nbg_runner/_binding/`.

## Usage

### Tensor Info

| Field | Type | Value Sample |
|:-----------------:|:-----------------:|:-------------:|
| rank | int | 4 |
| shape | Tuple[int, ...] | (1,3,224,224) |
| dtype | str | "uint8" |
| qtype | str | "affine" |
| scale | float | 0.007874 |
| zero_point | int | 128 |
| fixed_point_pos | int | 0 |

- `shape` is in C-style row major order, which is consistent with NumPy.

### Set Environment Vars

```shell
# Set HW target If the driver is compiled with vsimulator.
VSIMULATOR_CONFIG=VIP9000ULSI_PID0XBA
# Locate the OVX driver.
VIVANTE_SDK_DIR=${VIV_SDK_INSTALL_PATH}
LD_LIBRARY_PATH=${VIVANTE_SDK_DIR}/[lib|lib64|drivers]
# Set PYTHONPATH to the dir containing nbg_runner module.
PYTHONPATH=${workspaceFolder}/python
```

### Example

See detailed examples in `examples/*.py`

```python
from nbg_runner import OVXExecutor

# Load a model NBG file.
executor = OVXExecutor("path/to/model.nbg")

# Query model I/O tensors count.
num_inputs = executor.get_num_inputs()
num_outputs = executor.get_num_outputs()

# Get I/O tensor info by index.
input_info = executor.get_input_info(0)
output_info = executor.get_output_info(0)

# Or get all I/O tensors infos at once.
input_infos = executor.get_input_infos()
output_infos = executor.get_output_infos()

# Prepare inputs.
input_tensors: List[NDArray] = ...

# Set input tensor by index.
for i, input_tensor in enumerate(input_tensors):
executor.set_input(i, input_tensor)

# Or set all input tensors at once.
executor.set_inputs(input_tensors)

# Run inference.
executor.run()

# Get output tensor by index.
for i in range(num_outputs):
output_tensor = executor.get_output(i)

# Or get all output tensors at once.
output_tensors = executor.get_outputs()
```
13 changes: 13 additions & 0 deletions samples/nbg_runner_pybind/cmake/modules/FindPyBind11.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Try to use installed pybind11 CMake module.
find_package(pybind11)

if(NOT ${pybind11_FOUND})
include(FetchContent)

FetchContent_Declare(
pybind11
GIT_REPOSITORY "https://github.com/pybind/pybind11.git"
GIT_TAG "v2.11.1"
)
FetchContent_MakeAvailable(pybind11)
endif()
14 changes: 14 additions & 0 deletions samples/nbg_runner_pybind/cmake/modules/FindVivanteSDK.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
if(NOT VIVANTE_SDK_DIR)
message(FATAL_ERROR "VIVANTE_SDK_DIR is not set")
endif()

find_library(
OPENVX_LIB
NAMES OpenVX
HINTS ${VIVANTE_SDK_DIR}/lib ${VIVANTE_SDK_DIR}/lib64 ${VIVANTE_SDK_DIR}/drivers
REQUIRED
)

add_library(viv_sdk INTERFACE)
target_link_libraries(viv_sdk INTERFACE ${OPENVX_LIB})
target_include_directories(viv_sdk INTERFACE ${VIVANTE_SDK_DIR}/include)
Binary file added samples/nbg_runner_pybind/examples/data/bird.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added samples/nbg_runner_pybind/examples/data/cat.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading