diff --git a/micropython/README.md b/micropython/README.md new file mode 100644 index 00000000000..fb47c9a5dc7 --- /dev/null +++ b/micropython/README.md @@ -0,0 +1,56 @@ +# NCNN MicroPython Module + +This directory contains the MicroPython bindings for NCNN's C API, allowing you to run neural network inference directly in MicroPython. + +## Prerequisites + +On Debian, Ubuntu, or Raspberry Pi OS, you can install all required dependencies using: +```shell +sudo apt install build-essential git cmake libprotobuf-dev protobuf-compiler libomp-dev libopencv-dev +``` +On Redhat or Centos, you can install all required dependencies using: +```shell +sudo yum install gcc gcc-c++ make git cmake protobuf-devel protobuf-compiler opencv-devel +``` + +## Build Instructions + +### 1. Build NCNN Library + +First, build the NCNN library with C API support: + +```bash +mkdir -p ncnn/build_micropython +cd ncnn/build_micropython +cmake -DCMAKE_BUILD_TYPE=Release \ + -DNCNN_C_API=ON \ + -DNCNN_BUILD_EXAMPLES=OFF \ + -DNCNN_BUILD_TOOLS=OFF \ + .. +make -j$(nproc) +make install +``` + +### 2. Build MicroPython with NCNN Module + +First, clone the MicroPython repository if you haven't already: + +```bash +cd ../.. +git clone https://github.com/micropython/micropython.git +cd micropython + +# Build mpy-cross first +cd mpy-cross +make -j$(nproc) + +# Build MicroPython with NCNN module +cd ../ports/unix +make submodules +make clean +make USER_C_MODULES=/path/to/ncnn/micropython +``` + +## Usage + +TODO \ No newline at end of file diff --git a/micropython/c_api/c_api.cpp b/micropython/c_api/c_api.cpp new file mode 100644 index 00000000000..95289b33aab --- /dev/null +++ b/micropython/c_api/c_api.cpp @@ -0,0 +1,1302 @@ +#include "ncnn/c_api.h" + +extern "C" { +#include "ncnn_module.h" + +mp_obj_t mp_ncnn_version(void) +{ + const char* ver = ncnn_version(); + return mp_obj_new_str(ver, strlen(ver)); +} + +/* allocator api */ +mp_obj_t mp_ncnn_allocator_create_pool_allocator(void) +{ + ncnn_allocator_t allocator = ncnn_allocator_create_pool_allocator(); + return mp_obj_new_int_from_uint((uintptr_t)allocator); +} +mp_obj_t mp_ncnn_allocator_create_unlocked_pool_allocator(void) +{ + ncnn_allocator_t allocator = ncnn_allocator_create_unlocked_pool_allocator(); + return mp_obj_new_int_from_uint((uintptr_t)allocator); +} +mp_obj_t mp_ncnn_allocator_destroy(mp_obj_t ncnn_allocator_obj) +{ + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(ncnn_allocator_obj); + ncnn_allocator_destroy(allocator); + return mp_const_none; +} + +/* option api */ +mp_obj_t mp_ncnn_option_create(void) +{ + return mp_obj_new_int_from_uint((uintptr_t)ncnn_option_create()); +} +mp_obj_t mp_ncnn_option_destroy(mp_obj_t option_obj) +{ + ncnn_option_destroy((ncnn_option_t)mp_obj_get_int(option_obj)); + return mp_const_none; +} +mp_obj_t mp_ncnn_option_get_num_threads(mp_obj_t option_obj) +{ + const ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(option_obj); + return mp_obj_new_int(ncnn_option_get_num_threads(opt)); +} +mp_obj_t mp_ncnn_option_set_num_threads(mp_obj_t option_obj, mp_obj_t num_threads_obj) +{ + ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(option_obj); + int num_threads = mp_obj_get_int(num_threads_obj); + ncnn_option_set_num_threads(opt, num_threads); + return mp_const_none; +} +mp_obj_t mp_ncnn_option_get_use_local_pool_allocator(mp_obj_t option_obj) +{ + const ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(option_obj); + return mp_obj_new_int(ncnn_option_get_use_local_pool_allocator(opt)); +} +mp_obj_t mp_ncnn_option_set_use_local_pool_allocator(mp_obj_t option_obj, mp_obj_t use_local_pool_allocator_obj) +{ + ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(option_obj); + int use_local_pool_allocator = mp_obj_get_int(use_local_pool_allocator_obj); + ncnn_option_set_use_local_pool_allocator(opt, use_local_pool_allocator); + return mp_const_none; +} +mp_obj_t mp_ncnn_option_set_blob_allocator(mp_obj_t option_obj, mp_obj_t allocator_obj) +{ + ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(option_obj); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(allocator_obj); + ncnn_option_set_blob_allocator(opt, allocator); + return mp_const_none; +} +mp_obj_t mp_ncnn_option_set_workspace_allocator(mp_obj_t option_obj, mp_obj_t allocator_obj) +{ + ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(option_obj); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(allocator_obj); + ncnn_option_set_workspace_allocator(opt, allocator); + return mp_const_none; +} +mp_obj_t mp_ncnn_option_get_use_vulkan_compute(mp_obj_t option_obj) +{ + const ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(option_obj); + return mp_obj_new_int(ncnn_option_get_use_vulkan_compute(opt)); +} +mp_obj_t mp_ncnn_option_set_use_vulkan_compute(mp_obj_t option_obj, mp_obj_t use_vulkan_compute_obj) +{ + ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(option_obj); + int use_vulkan_compute = mp_obj_get_int(use_vulkan_compute_obj); + ncnn_option_set_use_vulkan_compute(opt, use_vulkan_compute); + return mp_const_none; +} + +/* mat api */ +mp_obj_t mp_ncnn_mat_create(void) +{ + ncnn_mat_t mat = ncnn_mat_create(); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_1d(mp_obj_t w_obj, mp_obj_t allocator_obj) +{ + int w = mp_obj_get_int(w_obj); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(allocator_obj); + ncnn_mat_t mat = ncnn_mat_create_1d(w, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_2d(mp_obj_t w_obj, mp_obj_t h_obj, mp_obj_t allocator_obj) +{ + int w = mp_obj_get_int(w_obj); + int h = mp_obj_get_int(h_obj); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(allocator_obj); + ncnn_mat_t mat = ncnn_mat_create_2d(w, h, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_3d(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + int h = mp_obj_get_int(args[1]); + int c = mp_obj_get_int(args[2]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[3]); + ncnn_mat_t mat = ncnn_mat_create_3d(w, h, c, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_4d(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + int h = mp_obj_get_int(args[1]); + int d = mp_obj_get_int(args[2]); + int c = mp_obj_get_int(args[3]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[4]); + ncnn_mat_t mat = ncnn_mat_create_4d(w, h, d, c, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_external_1d(mp_obj_t w_obj, mp_obj_t data_obj, mp_obj_t allocator_obj) +{ + int w = mp_obj_get_int(w_obj); + void* data = (void*)mp_obj_get_int(data_obj); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(allocator_obj); + ncnn_mat_t mat = ncnn_mat_create_external_1d(w, data, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_external_2d(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + int h = mp_obj_get_int(args[1]); + void* data = (void*)mp_obj_get_int(args[2]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[3]); + ncnn_mat_t mat = ncnn_mat_create_external_2d(w, h, data, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_external_3d(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + int h = mp_obj_get_int(args[1]); + int c = mp_obj_get_int(args[2]); + void* data = (void*)mp_obj_get_int(args[3]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[4]); + ncnn_mat_t mat = ncnn_mat_create_external_3d(w, h, c, data, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_external_4d(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + int h = mp_obj_get_int(args[1]); + int d = mp_obj_get_int(args[2]); + int c = mp_obj_get_int(args[3]); + void* data = (void*)mp_obj_get_int(args[4]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[5]); + ncnn_mat_t mat = ncnn_mat_create_external_4d(w, h, d, c, data, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_1d_elem(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + size_t elemsize = (size_t)mp_obj_get_int(args[1]); + int elempack = mp_obj_get_int(args[2]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[3]); + ncnn_mat_t mat = ncnn_mat_create_1d_elem(w, elemsize, elempack, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_2d_elem(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + int h = mp_obj_get_int(args[1]); + size_t elemsize = (size_t)mp_obj_get_int(args[2]); + int elempack = mp_obj_get_int(args[3]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[4]); + ncnn_mat_t mat = ncnn_mat_create_2d_elem(w, h, elemsize, elempack, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_3d_elem(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + int h = mp_obj_get_int(args[1]); + int c = mp_obj_get_int(args[2]); + size_t elemsize = (size_t)mp_obj_get_int(args[3]); + int elempack = mp_obj_get_int(args[4]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[5]); + ncnn_mat_t mat = ncnn_mat_create_3d_elem(w, h, c, elemsize, elempack, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_4d_elem(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + int h = mp_obj_get_int(args[1]); + int d = mp_obj_get_int(args[2]); + int c = mp_obj_get_int(args[3]); + size_t elemsize = (size_t)mp_obj_get_int(args[4]); + int elempack = mp_obj_get_int(args[5]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[6]); + ncnn_mat_t mat = ncnn_mat_create_4d_elem(w, h, d, c, elemsize, elempack, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_external_1d_elem(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + void* data = (void*)mp_obj_get_int(args[1]); + size_t elemsize = (size_t)mp_obj_get_int(args[2]); + int elempack = mp_obj_get_int(args[3]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[4]); + ncnn_mat_t mat = ncnn_mat_create_external_1d_elem(w, data, elemsize, elempack, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_external_2d_elem(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + int h = mp_obj_get_int(args[1]); + void* data = (void*)mp_obj_get_int(args[2]); + size_t elemsize = (size_t)mp_obj_get_int(args[3]); + int elempack = mp_obj_get_int(args[4]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[5]); + ncnn_mat_t mat = ncnn_mat_create_external_2d_elem(w, h, data, elemsize, elempack, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_external_3d_elem(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + int h = mp_obj_get_int(args[1]); + int c = mp_obj_get_int(args[2]); + void* data = (void*)mp_obj_get_int(args[3]); + size_t elemsize = (size_t)mp_obj_get_int(args[4]); + int elempack = mp_obj_get_int(args[5]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[6]); + ncnn_mat_t mat = ncnn_mat_create_external_3d_elem(w, h, c, data, elemsize, elempack, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_create_external_4d_elem(size_t n_args, const mp_obj_t* args) +{ + int w = mp_obj_get_int(args[0]); + int h = mp_obj_get_int(args[1]); + int d = mp_obj_get_int(args[2]); + int c = mp_obj_get_int(args[3]); + void* data = (void*)mp_obj_get_int(args[4]); + size_t elemsize = (size_t)mp_obj_get_int(args[5]); + int elempack = mp_obj_get_int(args[6]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[7]); + ncnn_mat_t mat = ncnn_mat_create_external_4d_elem(w, h, d, c, data, elemsize, elempack, allocator); + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_destroy(mp_obj_t mat_obj) +{ + ncnn_mat_destroy((ncnn_mat_t)mp_obj_get_int(mat_obj)); + return mp_const_none; +} + +mp_obj_t mp_ncnn_mat_fill_float(mp_obj_t mat_obj, mp_obj_t v_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + float v = (float)mp_obj_get_float(v_obj); + ncnn_mat_fill_float(mat, v); + return mp_const_none; +} + +mp_obj_t mp_ncnn_mat_clone(mp_obj_t mat_obj, mp_obj_t allocator_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(allocator_obj); + ncnn_mat_t cloned_mat = ncnn_mat_clone(mat, allocator); + return mp_obj_new_int_from_uint((uintptr_t)cloned_mat); +} +mp_obj_t mp_ncnn_mat_reshape_1d(mp_obj_t mat_obj, mp_obj_t w_obj, mp_obj_t allocator_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + int w = mp_obj_get_int(w_obj); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(allocator_obj); + ncnn_mat_t reshaped_mat = ncnn_mat_reshape_1d(mat, w, allocator); + return mp_obj_new_int_from_uint((uintptr_t)reshaped_mat); +} +mp_obj_t mp_ncnn_mat_reshape_2d(size_t n_args, const mp_obj_t* args) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[3]); + ncnn_mat_t reshaped_mat = ncnn_mat_reshape_2d(mat, w, h, allocator); + return mp_obj_new_int_from_uint((uintptr_t)reshaped_mat); +} +mp_obj_t mp_ncnn_mat_reshape_3d(size_t n_args, const mp_obj_t* args) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int c = mp_obj_get_int(args[3]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[4]); + ncnn_mat_t reshaped_mat = ncnn_mat_reshape_3d(mat, w, h, c, allocator); + return mp_obj_new_int_from_uint((uintptr_t)reshaped_mat); +} +mp_obj_t mp_ncnn_mat_reshape_4d(size_t n_args, const mp_obj_t* args) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int d = mp_obj_get_int(args[3]); + int c = mp_obj_get_int(args[4]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[5]); + ncnn_mat_t reshaped_mat = ncnn_mat_reshape_4d(mat, w, h, d, c, allocator); + return mp_obj_new_int_from_uint((uintptr_t)reshaped_mat); +} + +mp_obj_t mp_ncnn_mat_get_dims(mp_obj_t mat_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + return mp_obj_new_int(ncnn_mat_get_dims(mat)); +} +mp_obj_t mp_ncnn_mat_get_w(mp_obj_t mat_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + return mp_obj_new_int(ncnn_mat_get_w(mat)); +} +mp_obj_t mp_ncnn_mat_get_h(mp_obj_t mat_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + return mp_obj_new_int(ncnn_mat_get_h(mat)); +} +mp_obj_t mp_ncnn_mat_get_d(mp_obj_t mat_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + return mp_obj_new_int(ncnn_mat_get_d(mat)); +} +mp_obj_t mp_ncnn_mat_get_c(mp_obj_t mat_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + return mp_obj_new_int(ncnn_mat_get_c(mat)); +} +mp_obj_t mp_ncnn_mat_get_elemsize(mp_obj_t mat_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + return mp_obj_new_int(ncnn_mat_get_elemsize(mat)); +} +mp_obj_t mp_ncnn_mat_get_elempack(mp_obj_t mat_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + return mp_obj_new_int(ncnn_mat_get_elempack(mat)); +} +mp_obj_t mp_ncnn_mat_get_cstep(mp_obj_t mat_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + return mp_obj_new_int(ncnn_mat_get_cstep(mat)); +} +mp_obj_t mp_ncnn_mat_get_data(mp_obj_t mat_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + void* data = ncnn_mat_get_data(mat); + return mp_obj_new_int_from_uint((uintptr_t)data); +} + +mp_obj_t mp_ncnn_mat_get_channel_data(mp_obj_t mat_obj, mp_obj_t c_obj) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + int c = mp_obj_get_int(c_obj); + void* channel_data = ncnn_mat_get_channel_data(mat, c); + return mp_obj_new_int_from_uint((uintptr_t)channel_data); +} + +#if NCNN_PIXEL +/* mat pixel api */ +mp_obj_t mp_ncnn_mat_from_pixels(size_t n_args, const mp_obj_t* args) +{ + const unsigned char* pixels = (const unsigned char*)mp_obj_get_int(args[0]); + int type = mp_obj_get_int(args[1]); + int w = mp_obj_get_int(args[2]); + int h = mp_obj_get_int(args[3]); + int stride = mp_obj_get_int(args[4]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[5]); + ncnn_mat_t mat = ncnn_mat_from_pixels(pixels, type, w, h, stride, allocator); + if (mat == NULL) + { + mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Failed to create NCNN Mat from pixels")); + } + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_from_pixels_resize(size_t n_args, const mp_obj_t* args) +{ + const unsigned char* pixels = (const unsigned char*)mp_obj_get_int(args[0]); + int type = mp_obj_get_int(args[1]); + int w = mp_obj_get_int(args[2]); + int h = mp_obj_get_int(args[3]); + int stride = mp_obj_get_int(args[4]); + int target_width = mp_obj_get_int(args[5]); + int target_height = mp_obj_get_int(args[6]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[7]); + ncnn_mat_t mat = ncnn_mat_from_pixels_resize(pixels, type, w, h, stride, target_width, target_height, allocator); + if (mat == NULL) + { + mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Failed to create NCNN Mat from resized pixels")); + } + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_from_pixels_roi(size_t n_args, const mp_obj_t* args) +{ + const unsigned char* pixels = (const unsigned char*)mp_obj_get_int(args[0]); + int type = mp_obj_get_int(args[1]); + int w = mp_obj_get_int(args[2]); + int h = mp_obj_get_int(args[3]); + int stride = mp_obj_get_int(args[4]); + int roix = mp_obj_get_int(args[5]); + int roiy = mp_obj_get_int(args[6]); + int roiw = mp_obj_get_int(args[7]); + int roih = mp_obj_get_int(args[8]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[9]); + ncnn_mat_t mat = ncnn_mat_from_pixels_roi(pixels, type, w, h, stride, roix, roiy, roiw, roih, allocator); + if (mat == NULL) + { + mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Failed to create NCNN Mat from ROI pixels")); + } + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_from_pixels_roi_resize(size_t n_args, const mp_obj_t* args) +{ + const unsigned char* pixels = (const unsigned char*)mp_obj_get_int(args[0]); + int type = mp_obj_get_int(args[1]); + int w = mp_obj_get_int(args[2]); + int h = mp_obj_get_int(args[3]); + int stride = mp_obj_get_int(args[4]); + int roix = mp_obj_get_int(args[5]); + int roiy = mp_obj_get_int(args[6]); + int roiw = mp_obj_get_int(args[7]); + int roih = mp_obj_get_int(args[8]); + int target_width = mp_obj_get_int(args[9]); + int target_height = mp_obj_get_int(args[10]); + ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(args[11]); + ncnn_mat_t mat = ncnn_mat_from_pixels_roi_resize(pixels, type, w, h, stride, roix, roiy, roiw, roih, target_width, target_height, allocator); + if (mat == NULL) + { + mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Failed to create NCNN Mat from ROI pixels")); + } + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_mat_to_pixels(size_t n_args, const mp_obj_t* args) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(args[0]); + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[1]); + int type = mp_obj_get_int(args[2]); + int stride = mp_obj_get_int(args[3]); + ncnn_mat_to_pixels(mat, pixels, type, stride); + return mp_const_none; +} +mp_obj_t mp_ncnn_mat_to_pixels_resize(size_t n_args, const mp_obj_t* args) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(args[0]); + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[1]); + int type = mp_obj_get_int(args[2]); + int target_width = mp_obj_get_int(args[3]); + int target_height = mp_obj_get_int(args[4]); + int target_stride = mp_obj_get_int(args[5]); + ncnn_mat_to_pixels_resize(mat, pixels, type, target_width, target_height, target_stride); + return mp_const_none; +} +#endif /* NCNN_PIXEL */ + +mp_obj_t mp_ncnn_mat_substract_mean_normalize(size_t n_args, const mp_obj_t* args) +{ + ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(args[0]); + const float* mean_vals = (const float*)mp_obj_get_int(args[1]); + const float* norm_vals = (const float*)mp_obj_get_int(args[2]); + ncnn_mat_substract_mean_normalize(mat, mean_vals, norm_vals); + return mp_const_none; +} +mp_obj_t mp_ncnn_convert_packing(size_t n_args, const mp_obj_t* args) +{ + const ncnn_mat_t src = (ncnn_mat_t)mp_obj_get_int(args[0]); + ncnn_mat_t* dst = (ncnn_mat_t*)mp_obj_get_int(args[1]); + int elempack = mp_obj_get_int(args[2]); + const ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(args[3]); + ncnn_convert_packing(src, dst, elempack, opt); + return mp_const_none; +} +mp_obj_t mp_ncnn_flatten(size_t n_args, const mp_obj_t* args) +{ + ncnn_mat_t src = (ncnn_mat_t)mp_obj_get_int(args[0]); + ncnn_mat_t* dst = (ncnn_mat_t*)mp_obj_get_int(args[1]); + const ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(args[2]); + ncnn_flatten(src, dst, opt); + return mp_const_none; +} + +/* blob api */ +#if NCNN_STRING +mp_obj_t mp_ncnn_blob_get_name(mp_obj_t blob_obj) +{ + const ncnn_blob_t blob = (ncnn_blob_t)mp_obj_get_int(blob_obj); + return mp_obj_new_str(ncnn_blob_get_name(blob), strlen(ncnn_blob_get_name(blob))); +} +#endif + +mp_obj_t mp_ncnn_blob_get_producer(mp_obj_t blob_obj) +{ + const ncnn_blob_t blob = (ncnn_blob_t)mp_obj_get_int(blob_obj); + return mp_obj_new_int(ncnn_blob_get_producer(blob)); +} +mp_obj_t mp_ncnn_blob_get_consumer(mp_obj_t blob_obj) +{ + const ncnn_blob_t blob = (ncnn_blob_t)mp_obj_get_int(blob_obj); + return mp_obj_new_int(ncnn_blob_get_consumer(blob)); +} +mp_obj_t mp_ncnn_blob_get_shape(size_t n_args, const mp_obj_t* args) +{ + const ncnn_blob_t blob = (ncnn_blob_t)mp_obj_get_int(args[0]); + int* dims = (int*)mp_obj_get_int(args[1]); + int* w = (int*)mp_obj_get_int(args[2]); + int* h = (int*)mp_obj_get_int(args[3]); + int* c = (int*)mp_obj_get_int(args[4]); + ncnn_blob_get_shape(blob, dims, w, h, c); + return mp_const_none; +} + +/* paramdict api */ +mp_obj_t mp_ncnn_paramdict_create(void) +{ + return mp_obj_new_int_from_uint((uintptr_t)ncnn_paramdict_create()); +} +mp_obj_t mp_ncnn_paramdict_destroy(mp_obj_t pd_obj) +{ + ncnn_paramdict_destroy((ncnn_paramdict_t)mp_obj_get_int(pd_obj)); + return mp_const_none; +} +mp_obj_t mp_ncnn_paramdict_get_type(mp_obj_t pd_obj, mp_obj_t id_obj) +{ + const ncnn_paramdict_t pd = (ncnn_paramdict_t)mp_obj_get_int(pd_obj); + int id = mp_obj_get_int(id_obj); + return mp_obj_new_int(ncnn_paramdict_get_type(pd, id)); +} +mp_obj_t mp_ncnn_paramdict_get_int(size_t n_args, const mp_obj_t* args) +{ + const ncnn_paramdict_t pd = (ncnn_paramdict_t)mp_obj_get_int(args[0]); + int id = mp_obj_get_int(args[1]); + int def = mp_obj_get_int(args[2]); + return mp_obj_new_int(ncnn_paramdict_get_int(pd, id, def)); +} +mp_obj_t mp_ncnn_paramdict_get_float(size_t n_args, const mp_obj_t* args) +{ + const ncnn_paramdict_t pd = (ncnn_paramdict_t)mp_obj_get_int(args[0]); + int id = mp_obj_get_int(args[1]); + float def = (float)mp_obj_get_float(args[2]); + return mp_obj_new_float(ncnn_paramdict_get_float(pd, id, def)); +} +mp_obj_t mp_ncnn_paramdict_get_array(size_t n_args, const mp_obj_t* args) +{ + const ncnn_paramdict_t pd = (ncnn_paramdict_t)mp_obj_get_int(args[0]); + int id = mp_obj_get_int(args[1]); + const ncnn_mat_t def = (ncnn_mat_t)mp_obj_get_int(args[2]); + ncnn_mat_t mat = ncnn_paramdict_get_array(pd, id, def); + if (mat == NULL) + { + mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Failed to get NCNN Mat from paramdict")); + } + return mp_obj_new_int_from_uint((uintptr_t)mat); +} +mp_obj_t mp_ncnn_paramdict_set_int(size_t n_args, const mp_obj_t* args) +{ + ncnn_paramdict_t pd = (ncnn_paramdict_t)mp_obj_get_int(args[0]); + int id = mp_obj_get_int(args[1]); + int i = mp_obj_get_int(args[2]); + ncnn_paramdict_set_int(pd, id, i); + return mp_const_none; +} +mp_obj_t mp_ncnn_paramdict_set_float(size_t n_args, const mp_obj_t* args) +{ + ncnn_paramdict_t pd = (ncnn_paramdict_t)mp_obj_get_int(args[0]); + int id = mp_obj_get_int(args[1]); + float f = (float)mp_obj_get_float(args[2]); + ncnn_paramdict_set_float(pd, id, f); + return mp_const_none; +} +mp_obj_t mp_ncnn_paramdict_set_array(size_t n_args, const mp_obj_t* args) +{ + ncnn_paramdict_t pd = (ncnn_paramdict_t)mp_obj_get_int(args[0]); + int id = mp_obj_get_int(args[1]); + const ncnn_mat_t v = (ncnn_mat_t)mp_obj_get_int(args[2]); + ncnn_paramdict_set_array(pd, id, v); + return mp_const_none; +} + +/* datareader api */ +mp_obj_t mp_ncnn_datareader_create(void) +{ + ncnn_datareader_t dr = ncnn_datareader_create(); + return mp_obj_new_int_from_uint((uintptr_t)dr); +} +#if NCNN_STDIO +mp_obj_t mp_ncnn_datareader_create_from_stdio(mp_obj_t fp_obj) +{ + ncnn_datareader_t dr = ncnn_datareader_create_from_stdio((FILE*)mp_obj_get_int(fp_obj)); + return mp_obj_new_int_from_uint((uintptr_t)dr); +} +#endif /* NCNN_STDIO */ +mp_obj_t mp_ncnn_datareader_create_from_memory(mp_obj_t mem_obj) +{ + ncnn_datareader_t dr = ncnn_datareader_create_from_memory((const unsigned char**)mp_obj_get_int(mem_obj)); + return mp_obj_new_int_from_uint((uintptr_t)dr); +} +mp_obj_t mp_ncnn_datareader_destroy(mp_obj_t dr_obj) +{ + ncnn_datareader_destroy((ncnn_datareader_t)mp_obj_get_int(dr_obj)); + return mp_const_none; +} + +/* modelbin api */ +mp_obj_t mp_ncnn_modelbin_create_from_datareader(mp_obj_t dr_obj) +{ + ncnn_modelbin_t mb = ncnn_modelbin_create_from_datareader((ncnn_datareader_t)mp_obj_get_int(dr_obj)); + return mp_obj_new_int_from_uint((uintptr_t)mb); +} +mp_obj_t mp_ncnn_modelbin_create_from_mat_array(mp_obj_t weights_obj, mp_obj_t n_obj) +{ + const ncnn_mat_t* weights = (const ncnn_mat_t*)mp_obj_get_int(weights_obj); + int n = mp_obj_get_int(n_obj); + ncnn_modelbin_t mb = ncnn_modelbin_create_from_mat_array(weights, n); + return mp_obj_new_int_from_uint((uintptr_t)mb); +} +mp_obj_t mp_ncnn_modelbin_destroy(mp_obj_t mb_obj) +{ + ncnn_modelbin_destroy((ncnn_modelbin_t)mp_obj_get_int(mb_obj)); + return mp_const_none; +} + +/* layer api */ +mp_obj_t mp_ncnn_layer_create(void) +{ + return mp_obj_new_int_from_uint((uintptr_t)ncnn_layer_create()); +} +mp_obj_t mp_ncnn_layer_create_by_typeindex(mp_obj_t typeindex_obj) +{ + int typeindex = mp_obj_get_int(typeindex_obj); + return mp_obj_new_int_from_uint((uintptr_t)ncnn_layer_create_by_typeindex(typeindex)); +} +#if NCNN_STRING +mp_obj_t mp_ncnn_layer_create_by_type(mp_obj_t type_obj) +{ + const char* type = mp_obj_str_get_str(type_obj); + return mp_obj_new_int_from_uint((uintptr_t)ncnn_layer_create_by_type(type)); +} +mp_obj_t mp_ncnn_layer_type_to_index(mp_obj_t type_obj) +{ + const char* type = mp_obj_str_get_str(type_obj); + return mp_obj_new_int(ncnn_layer_type_to_index(type)); +} +#endif /* NCNN_STRING */ +mp_obj_t mp_ncnn_layer_destroy(mp_obj_t layer_obj) +{ + ncnn_layer_destroy((ncnn_layer_t)mp_obj_get_int(layer_obj)); + return mp_const_none; +} + +#if NCNN_STRING +mp_obj_t mp_ncnn_layer_get_name(mp_obj_t layer_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + const char* name = ncnn_layer_get_name(layer); + return mp_obj_new_str(name, strlen(name)); +} +#endif /* NCNN_STRING */ + +mp_obj_t mp_ncnn_layer_get_typeindex(mp_obj_t layer_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + return mp_obj_new_int(ncnn_layer_get_typeindex(layer)); +} +#if NCNN_STRING +mp_obj_t mp_ncnn_layer_get_type(mp_obj_t layer_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + const char* type = ncnn_layer_get_type(layer); + return mp_obj_new_str(type, strlen(type)); +} +#endif /* NCNN_STRING */ + +mp_obj_t mp_ncnn_layer_get_one_blob_only(mp_obj_t layer_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + return mp_obj_new_int(ncnn_layer_get_one_blob_only(layer)); +} +mp_obj_t mp_ncnn_layer_get_support_inplace(mp_obj_t layer_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + return mp_obj_new_int(ncnn_layer_get_support_inplace(layer)); +} +mp_obj_t mp_ncnn_layer_get_support_vulkan(mp_obj_t layer_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + return mp_obj_new_int(ncnn_layer_get_support_vulkan(layer)); +} +mp_obj_t mp_ncnn_layer_get_support_packing(mp_obj_t layer_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + return mp_obj_new_int(ncnn_layer_get_support_packing(layer)); +} +mp_obj_t mp_ncnn_layer_get_support_bf16_storage(mp_obj_t layer_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + return mp_obj_new_int(ncnn_layer_get_support_bf16_storage(layer)); +} +mp_obj_t mp_ncnn_layer_get_support_fp16_storage(mp_obj_t layer_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + return mp_obj_new_int(ncnn_layer_get_support_fp16_storage(layer)); +} + +mp_obj_t mp_ncnn_layer_set_one_blob_only(mp_obj_t layer_obj, mp_obj_t enable_obj) +{ + ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + int enable = mp_obj_get_int(enable_obj); + ncnn_layer_set_one_blob_only(layer, enable); + return mp_const_none; +} +mp_obj_t mp_ncnn_layer_set_support_inplace(mp_obj_t layer_obj, mp_obj_t enable_obj) +{ + ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + int enable = mp_obj_get_int(enable_obj); + ncnn_layer_set_support_inplace(layer, enable); + return mp_const_none; +} +mp_obj_t mp_ncnn_layer_set_support_vulkan(mp_obj_t layer_obj, mp_obj_t enable_obj) +{ + ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + int enable = mp_obj_get_int(enable_obj); + ncnn_layer_set_support_vulkan(layer, enable); + return mp_const_none; +} +mp_obj_t mp_ncnn_layer_set_support_packing(mp_obj_t layer_obj, mp_obj_t enable_obj) +{ + ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + int enable = mp_obj_get_int(enable_obj); + ncnn_layer_set_support_packing(layer, enable); + return mp_const_none; +} +mp_obj_t mp_ncnn_layer_set_support_bf16_storage(mp_obj_t layer_obj, mp_obj_t enable_obj) +{ + ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + int enable = mp_obj_get_int(enable_obj); + ncnn_layer_set_support_bf16_storage(layer, enable); + return mp_const_none; +} +mp_obj_t mp_ncnn_layer_set_support_fp16_storage(mp_obj_t layer_obj, mp_obj_t enable_obj) +{ + ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + int enable = mp_obj_get_int(enable_obj); + ncnn_layer_set_support_fp16_storage(layer, enable); + return mp_const_none; +} + +mp_obj_t mp_ncnn_layer_get_bottom_count(mp_obj_t layer_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + return mp_obj_new_int(ncnn_layer_get_bottom_count(layer)); +} +mp_obj_t mp_ncnn_layer_get_bottom(mp_obj_t layer_obj, mp_obj_t i_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + int i = mp_obj_get_int(i_obj); + return mp_obj_new_int(ncnn_layer_get_bottom(layer, i)); +} +mp_obj_t mp_ncnn_layer_get_top_count(mp_obj_t layer_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + return mp_obj_new_int(ncnn_layer_get_top_count(layer)); +} +mp_obj_t mp_ncnn_layer_get_top(mp_obj_t layer_obj, mp_obj_t i_obj) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(layer_obj); + int i = mp_obj_get_int(i_obj); + return mp_obj_new_int(ncnn_layer_get_top(layer, i)); +} + +mp_obj_t mp_ncnn_blob_get_bottom_shape(size_t n_args, const mp_obj_t* args) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(args[0]); + int i = mp_obj_get_int(args[1]); + int* dims = (int*)mp_obj_get_int(args[2]); + int* w = (int*)mp_obj_get_int(args[3]); + int* h = (int*)mp_obj_get_int(args[4]); + int* c = (int*)mp_obj_get_int(args[5]); + ncnn_blob_get_bottom_shape(layer, i, dims, w, h, c); + return mp_const_none; +} +mp_obj_t mp_ncnn_blob_get_top_shape(size_t n_args, const mp_obj_t* args) +{ + const ncnn_layer_t layer = (ncnn_layer_t)mp_obj_get_int(args[0]); + int i = mp_obj_get_int(args[1]); + int* dims = (int*)mp_obj_get_int(args[2]); + int* w = (int*)mp_obj_get_int(args[3]); + int* h = (int*)mp_obj_get_int(args[4]); + int* c = (int*)mp_obj_get_int(args[5]); + ncnn_blob_get_top_shape(layer, i, dims, w, h, c); + return mp_const_none; +} + +/* net api */ +mp_obj_t mp_ncnn_net_create(void) +{ + return mp_obj_new_int_from_uint((uintptr_t)ncnn_net_create()); +} +mp_obj_t mp_ncnn_net_destroy(mp_obj_t net_obj) +{ + ncnn_net_destroy((ncnn_net_t)mp_obj_get_int(net_obj)); + return mp_const_none; +} +mp_obj_t mp_ncnn_net_get_option(mp_obj_t net_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + ncnn_option_t opt = ncnn_net_get_option(net); + return mp_obj_new_int_from_uint((uintptr_t)opt); +} +mp_obj_t mp_ncnn_net_set_option(mp_obj_t net_obj, mp_obj_t opt_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(opt_obj); + ncnn_net_set_option(net, opt); + return mp_const_none; +} +#if NCNN_VULKAN +mp_obj_t mp_ncnn_net_set_vulkan_device(mp_obj_t net_obj, mp_obj_t device_index_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + int device_index = mp_obj_get_int(device_index_obj); + ncnn_net_set_vulkan_device(net, device_index); + return mp_const_none; +} +#endif +#if NCNN_STRING +mp_obj_t mp_ncnn_net_register_custom_layer_by_type(size_t n_args, const mp_obj_t* args) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(args[0]); + const char* type = mp_obj_str_get_str(args[1]); + ncnn_layer_creator_t creator = (ncnn_layer_creator_t)mp_obj_get_int(args[2]); + ncnn_layer_destroyer_t destroyer = (ncnn_layer_destroyer_t)mp_obj_get_int(args[3]); + void* userdata = (void*)mp_obj_get_int(args[4]); + ncnn_net_register_custom_layer_by_type(net, type, creator, destroyer, userdata); + return mp_const_none; +} +#endif +mp_obj_t mp_ncnn_net_register_custom_layer_by_typeindex(size_t n_args, const mp_obj_t* args) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(args[0]); + int typeindex = mp_obj_get_int(args[1]); + ncnn_layer_creator_t creator = (ncnn_layer_creator_t)mp_obj_get_int(args[2]); + ncnn_layer_destroyer_t destroyer = (ncnn_layer_destroyer_t)mp_obj_get_int(args[3]); + void* userdata = (void*)mp_obj_get_int(args[4]); + ncnn_net_register_custom_layer_by_typeindex(net, typeindex, creator, destroyer, userdata); + return mp_const_none; +} + +#if NCNN_STDIO +#if NCNN_STRING +mp_obj_t mp_ncnn_net_load_param(mp_obj_t net_obj, mp_obj_t path_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + const char* path = mp_obj_str_get_str(path_obj); + return mp_obj_new_int(ncnn_net_load_param(net, path)); +} +#endif /* NCNN_STRING */ +mp_obj_t mp_ncnn_net_load_param_bin(mp_obj_t net_obj, mp_obj_t path_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + const char* path = mp_obj_str_get_str(path_obj); + return mp_obj_new_int(ncnn_net_load_param_bin(net, path)); +} +mp_obj_t mp_ncnn_net_load_model(mp_obj_t net_obj, mp_obj_t path_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + const char* path = mp_obj_str_get_str(path_obj); + return mp_obj_new_int(ncnn_net_load_model(net, path)); +} +#endif /* NCNN_STDIO */ + +#if NCNN_STDIO +#if NCNN_STRING +mp_obj_t mp_ncnn_net_load_param_memory(mp_obj_t net_obj, mp_obj_t mem_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + const char* mem = (const char*)mp_obj_get_int(mem_obj); + return mp_obj_new_int(ncnn_net_load_param_memory(net, mem)); +} +#endif /* NCNN_STRING */ +#endif /* NCNN_STDIO */ +mp_obj_t mp_ncnn_net_load_param_bin_memory(mp_obj_t net_obj, mp_obj_t mem_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + const unsigned char* mem = (const unsigned char*)mp_obj_get_int(mem_obj); + return mp_obj_new_int(ncnn_net_load_param_bin_memory(net, mem)); +} +mp_obj_t mp_ncnn_net_load_model_memory(mp_obj_t net_obj, mp_obj_t mem_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + const unsigned char* mem = (const unsigned char*)mp_obj_get_int(mem_obj); + return mp_obj_new_int(ncnn_net_load_model_memory(net, mem)); +} + +#if NCNN_STRING +mp_obj_t mp_ncnn_net_load_param_datareader(mp_obj_t net_obj, mp_obj_t dr_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + const ncnn_datareader_t dr = (ncnn_datareader_t)mp_obj_get_int(dr_obj); + return mp_obj_new_int(ncnn_net_load_param_datareader(net, dr)); +} +#endif /* NCNN_STRING */ +mp_obj_t mp_ncnn_net_load_param_bin_datareader(mp_obj_t net_obj, mp_obj_t dr_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + const ncnn_datareader_t dr = (ncnn_datareader_t)mp_obj_get_int(dr_obj); + return mp_obj_new_int(ncnn_net_load_param_bin_datareader(net, dr)); +} +mp_obj_t mp_ncnn_net_load_model_datareader(mp_obj_t net_obj, mp_obj_t dr_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + const ncnn_datareader_t dr = (ncnn_datareader_t)mp_obj_get_int(dr_obj); + return mp_obj_new_int(ncnn_net_load_model_datareader(net, dr)); +} + +mp_obj_t mp_ncnn_net_clear(mp_obj_t net_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + ncnn_net_clear(net); + return mp_const_none; +} + +mp_obj_t mp_ncnn_net_get_input_count(mp_obj_t net_obj) +{ + const ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + return mp_obj_new_int(ncnn_net_get_input_count(net)); +} +mp_obj_t mp_ncnn_net_get_output_count(mp_obj_t net_obj) +{ + const ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + return mp_obj_new_int(ncnn_net_get_output_count(net)); +} +#if NCNN_STRING +mp_obj_t mp_ncnn_net_get_input_name(mp_obj_t net_obj, mp_obj_t i_obj) +{ + const ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + int i = mp_obj_get_int(i_obj); + const char* name = ncnn_net_get_input_name(net, i); + return mp_obj_new_str(name, strlen(name)); +} +mp_obj_t mp_ncnn_net_get_output_name(mp_obj_t net_obj, mp_obj_t i_obj) +{ + const ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + int i = mp_obj_get_int(i_obj); + const char* name = ncnn_net_get_output_name(net, i); + return mp_obj_new_str(name, strlen(name)); +} +#endif /* NCNN_STRING */ +mp_obj_t mp_ncnn_net_get_input_index(mp_obj_t net_obj, mp_obj_t i_obj) +{ + const ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + int i = mp_obj_get_int(i_obj); + return mp_obj_new_int(ncnn_net_get_input_index(net, i)); +} +mp_obj_t mp_ncnn_net_get_output_index(mp_obj_t net_obj, mp_obj_t i_obj) +{ + const ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + int i = mp_obj_get_int(i_obj); + return mp_obj_new_int(ncnn_net_get_output_index(net, i)); +} + +/* extractor api */ +mp_obj_t mp_ncnn_extractor_create(mp_obj_t net_obj) +{ + ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj); + return mp_obj_new_int_from_uint((uintptr_t)ncnn_extractor_create(net)); +} +mp_obj_t mp_ncnn_extractor_destroy(mp_obj_t ex_obj) +{ + ncnn_extractor_t ex = (ncnn_extractor_t)mp_obj_get_int(ex_obj); + ncnn_extractor_destroy(ex); + return mp_const_none; +} +mp_obj_t mp_ncnn_extractor_set_option(mp_obj_t ex_obj, mp_obj_t opt_obj) +{ + ncnn_extractor_t ex = (ncnn_extractor_t)mp_obj_get_int(ex_obj); + const ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(opt_obj); + ncnn_extractor_set_option(ex, opt); + return mp_const_none; +} +#if NCNN_STRING +mp_obj_t mp_ncnn_extractor_input(mp_obj_t ex_obj, mp_obj_t name_obj, mp_obj_t mat_obj) +{ + ncnn_extractor_t ex = (ncnn_extractor_t)mp_obj_get_int(ex_obj); + const char* name = mp_obj_str_get_str(name_obj); + const ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + return mp_obj_new_int(ncnn_extractor_input(ex, name, mat)); +} +mp_obj_t mp_ncnn_extractor_extract(mp_obj_t ex_obj, mp_obj_t name_obj, mp_obj_t mat_obj) +{ + ncnn_extractor_t ex = (ncnn_extractor_t)mp_obj_get_int(ex_obj); + const char* name = mp_obj_str_get_str(name_obj); + ncnn_mat_t* mat = (ncnn_mat_t*)mp_obj_get_int(mat_obj); + return mp_obj_new_int(ncnn_extractor_extract(ex, name, mat)); +} +#endif /* NCNN_STRING */ +mp_obj_t mp_ncnn_extractor_input_index(mp_obj_t ex_obj, mp_obj_t index_obj, mp_obj_t mat_obj) +{ + ncnn_extractor_t ex = (ncnn_extractor_t)mp_obj_get_int(ex_obj); + int index = mp_obj_get_int(index_obj); + const ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj); + return mp_obj_new_int(ncnn_extractor_input_index(ex, index, mat)); +} +mp_obj_t mp_ncnn_extractor_extract_index(mp_obj_t ex_obj, mp_obj_t index_obj, mp_obj_t mat_obj) +{ + ncnn_extractor_t ex = (ncnn_extractor_t)mp_obj_get_int(ex_obj); + int index = mp_obj_get_int(index_obj); + ncnn_mat_t* mat = (ncnn_mat_t*)mp_obj_get_int(mat_obj); + return mp_obj_new_int(ncnn_extractor_extract_index(ex, index, mat)); +} + +/* mat process api */ +mp_obj_t mp_ncnn_copy_make_border(size_t n_args, const mp_obj_t* args) +{ + const ncnn_mat_t src = (ncnn_mat_t)mp_obj_get_int(args[0]); + ncnn_mat_t dst = (ncnn_mat_t)mp_obj_get_int(args[1]); + int top = mp_obj_get_int(args[2]); + int bottom = mp_obj_get_int(args[3]); + int left = mp_obj_get_int(args[4]); + int right = mp_obj_get_int(args[5]); + int type = mp_obj_get_int(args[6]); + float v = (float)mp_obj_get_float(args[7]); + const ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(args[8]); + ncnn_copy_make_border(src, dst, top, bottom, left, right, type, v, opt); + return mp_const_none; +} +mp_obj_t mp_ncnn_copy_make_border_3d(size_t n_args, const mp_obj_t* args) +{ + const ncnn_mat_t src = (ncnn_mat_t)mp_obj_get_int(args[0]); + ncnn_mat_t dst = (ncnn_mat_t)mp_obj_get_int(args[1]); + int top = mp_obj_get_int(args[2]); + int bottom = mp_obj_get_int(args[3]); + int left = mp_obj_get_int(args[4]); + int right = mp_obj_get_int(args[5]); + int front = mp_obj_get_int(args[6]); + int behind = mp_obj_get_int(args[7]); + int type = mp_obj_get_int(args[8]); + float v = (float)mp_obj_get_float(args[9]); + const ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(args[10]); + ncnn_copy_make_border_3d(src, dst, top, bottom, left, right, front, behind, type, v, opt); + return mp_const_none; +} +mp_obj_t mp_ncnn_copy_cut_border(size_t n_args, const mp_obj_t* args) +{ + const ncnn_mat_t src = (ncnn_mat_t)mp_obj_get_int(args[0]); + ncnn_mat_t dst = (ncnn_mat_t)mp_obj_get_int(args[1]); + int top = mp_obj_get_int(args[2]); + int bottom = mp_obj_get_int(args[3]); + int left = mp_obj_get_int(args[4]); + int right = mp_obj_get_int(args[5]); + const ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(args[6]); + ncnn_copy_cut_border(src, dst, top, bottom, left, right, opt); + return mp_const_none; +} +mp_obj_t mp_ncnn_copy_cut_border_3d(size_t n_args, const mp_obj_t* args) +{ + const ncnn_mat_t src = (ncnn_mat_t)mp_obj_get_int(args[0]); + ncnn_mat_t dst = (ncnn_mat_t)mp_obj_get_int(args[1]); + int top = mp_obj_get_int(args[2]); + int bottom = mp_obj_get_int(args[3]); + int left = mp_obj_get_int(args[4]); + int right = mp_obj_get_int(args[5]); + int front = mp_obj_get_int(args[6]); + int behind = mp_obj_get_int(args[7]); + const ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(args[8]); + ncnn_copy_cut_border_3d(src, dst, top, bottom, left, right, front, behind, opt); + return mp_const_none; +} + +#if NCNN_PIXEL_DRAWING +/* mat pixel drawing api*/ +mp_obj_t mp_ncnn_draw_rectangle_c1(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int rx = mp_obj_get_int(args[3]); + int ry = mp_obj_get_int(args[4]); + int rw = mp_obj_get_int(args[5]); + int rh = mp_obj_get_int(args[6]); + int color = mp_obj_get_int(args[7]); + int thickness = mp_obj_get_int(args[8]); + ncnn_draw_rectangle_c1(pixels, w, h, rx, ry, rw, rh, color, thickness); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_rectangle_c2(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int rx = mp_obj_get_int(args[3]); + int ry = mp_obj_get_int(args[4]); + int rw = mp_obj_get_int(args[5]); + int rh = mp_obj_get_int(args[6]); + unsigned int color = mp_obj_get_int(args[7]); + int thickness = mp_obj_get_int(args[8]); + ncnn_draw_rectangle_c2(pixels, w, h, rx, ry, rw, rh, color, thickness); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_rectangle_c3(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int rx = mp_obj_get_int(args[3]); + int ry = mp_obj_get_int(args[4]); + int rw = mp_obj_get_int(args[5]); + int rh = mp_obj_get_int(args[6]); + unsigned int color = mp_obj_get_int(args[7]); + int thickness = mp_obj_get_int(args[8]); + ncnn_draw_rectangle_c3(pixels, w, h, rx, ry, rw, rh, color, thickness); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_rectangle_c4(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int rx = mp_obj_get_int(args[3]); + int ry = mp_obj_get_int(args[4]); + int rw = mp_obj_get_int(args[5]); + int rh = mp_obj_get_int(args[6]); + unsigned int color = mp_obj_get_int(args[7]); + int thickness = mp_obj_get_int(args[8]); + ncnn_draw_rectangle_c4(pixels, w, h, rx, ry, rw, rh, color, thickness); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_text_c1(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + const char* text = mp_obj_str_get_str(args[3]); + int x = mp_obj_get_int(args[4]); + int y = mp_obj_get_int(args[5]); + int fontpixelsize = mp_obj_get_int(args[6]); + unsigned int color = mp_obj_get_int(args[7]); + ncnn_draw_text_c1(pixels, w, h, text, x, y, fontpixelsize, color); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_text_c2(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + const char* text = mp_obj_str_get_str(args[3]); + int x = mp_obj_get_int(args[4]); + int y = mp_obj_get_int(args[5]); + int fontpixelsize = mp_obj_get_int(args[6]); + unsigned int color = mp_obj_get_int(args[7]); + ncnn_draw_text_c2(pixels, w, h, text, x, y, fontpixelsize, color); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_text_c3(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + const char* text = mp_obj_str_get_str(args[3]); + int x = mp_obj_get_int(args[4]); + int y = mp_obj_get_int(args[5]); + int fontpixelsize = mp_obj_get_int(args[6]); + unsigned int color = mp_obj_get_int(args[7]); + ncnn_draw_text_c3(pixels, w, h, text, x, y, fontpixelsize, color); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_text_c4(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + const char* text = mp_obj_str_get_str(args[3]); + int x = mp_obj_get_int(args[4]); + int y = mp_obj_get_int(args[5]); + int fontpixelsize = mp_obj_get_int(args[6]); + unsigned int color = mp_obj_get_int(args[7]); + ncnn_draw_text_c4(pixels, w, h, text, x, y, fontpixelsize, color); + return mp_const_none; +} + +mp_obj_t mp_ncnn_draw_circle_c1(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int cx = mp_obj_get_int(args[3]); + int cy = mp_obj_get_int(args[4]); + int radius = mp_obj_get_int(args[5]); + unsigned int color = mp_obj_get_int(args[6]); + int thickness = mp_obj_get_int(args[7]); + ncnn_draw_circle_c1(pixels, w, h, cx, cy, radius, color, thickness); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_circle_c2(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int cx = mp_obj_get_int(args[3]); + int cy = mp_obj_get_int(args[4]); + int radius = mp_obj_get_int(args[5]); + unsigned int color = mp_obj_get_int(args[6]); + int thickness = mp_obj_get_int(args[7]); + ncnn_draw_circle_c2(pixels, w, h, cx, cy, radius, color, thickness); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_circle_c3(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int cx = mp_obj_get_int(args[3]); + int cy = mp_obj_get_int(args[4]); + int radius = mp_obj_get_int(args[5]); + unsigned int color = mp_obj_get_int(args[6]); + int thickness = mp_obj_get_int(args[7]); + ncnn_draw_circle_c3(pixels, w, h, cx, cy, radius, color, thickness); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_circle_c4(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int cx = mp_obj_get_int(args[3]); + int cy = mp_obj_get_int(args[4]); + int radius = mp_obj_get_int(args[5]); + unsigned int color = mp_obj_get_int(args[6]); + int thickness = mp_obj_get_int(args[7]); + ncnn_draw_circle_c4(pixels, w, h, cx, cy, radius, color, thickness); + return mp_const_none; +} + +mp_obj_t mp_ncnn_draw_line_c1(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int x0 = mp_obj_get_int(args[3]); + int y0 = mp_obj_get_int(args[4]); + int x1 = mp_obj_get_int(args[5]); + int y1 = mp_obj_get_int(args[6]); + unsigned int color = mp_obj_get_int(args[7]); + int thickness = mp_obj_get_int(args[8]); + ncnn_draw_line_c1(pixels, w, h, x0, y0, x1, y1, color, thickness); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_line_c2(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int x0 = mp_obj_get_int(args[3]); + int y0 = mp_obj_get_int(args[4]); + int x1 = mp_obj_get_int(args[5]); + int y1 = mp_obj_get_int(args[6]); + unsigned int color = mp_obj_get_int(args[7]); + int thickness = mp_obj_get_int(args[8]); + ncnn_draw_line_c2(pixels, w, h, x0, y0, x1, y1, color, thickness); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_line_c3(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int x0 = mp_obj_get_int(args[3]); + int y0 = mp_obj_get_int(args[4]); + int x1 = mp_obj_get_int(args[5]); + int y1 = mp_obj_get_int(args[6]); + unsigned int color = mp_obj_get_int(args[7]); + int thickness = mp_obj_get_int(args[8]); + ncnn_draw_line_c3(pixels, w, h, x0, y0, x1, y1, color, thickness); + return mp_const_none; +} +mp_obj_t mp_ncnn_draw_line_c4(size_t n_args, const mp_obj_t* args) +{ + unsigned char* pixels = (unsigned char*)mp_obj_get_int(args[0]); + int w = mp_obj_get_int(args[1]); + int h = mp_obj_get_int(args[2]); + int x0 = mp_obj_get_int(args[3]); + int y0 = mp_obj_get_int(args[4]); + int x1 = mp_obj_get_int(args[5]); + int y1 = mp_obj_get_int(args[6]); + unsigned int color = mp_obj_get_int(args[7]); + int thickness = mp_obj_get_int(args[8]); + ncnn_draw_line_c4(pixels, w, h, x0, y0, x1, y1, color, thickness); + return mp_const_none; +} +#endif /* NCNN_PIXEL_DRAWING */ +} diff --git a/micropython/c_api/micropython.cmake b/micropython/c_api/micropython.cmake new file mode 100644 index 00000000000..97578dc5d33 --- /dev/null +++ b/micropython/c_api/micropython.cmake @@ -0,0 +1,31 @@ +add_library(usermod_ncnn INTERFACE) +find_package(OpenMP REQUIRED) + +find_library(NCNN_LIBRARY + NAMES ncnn + PATHS + ${CMAKE_CURRENT_SOURCE_DIR}/../../build_micropython/install/lib64 + ${CMAKE_CURRENT_SOURCE_DIR}/../../build_micropython/install/lib + NO_DEFAULT_PATH +) + +target_sources(usermod_ncnn INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/c_api.cpp + ${CMAKE_CURRENT_LIST_DIR}/ncnn_module.c +) + +target_include_directories(usermod_ncnn INTERFACE + ${CMAKE_CURRENT_LIST_DIR} + ${CMAKE_CURRENT_LIST_DIR}/../../build_micropython/install/include +) + +target_link_directories(usermod_ncnn INTERFACE + ${CMAKE_CURRENT_LIST_DIR}/../../build_micropython/install/lib + OpenMP::OpenMP_CXX +) + +target_link_libraries(usermod_ncnn INTERFACE + ncnn +) + +target_link_libraries(usermod INTERFACE usermod_ncnn) \ No newline at end of file diff --git a/micropython/c_api/micropython.mk b/micropython/c_api/micropython.mk new file mode 100644 index 00000000000..5eb9937e1be --- /dev/null +++ b/micropython/c_api/micropython.mk @@ -0,0 +1,12 @@ +NCNN_MOD_DIR := $(USERMOD_DIR) + +SRC_USERMOD += $(NCNN_MOD_DIR)/ncnn_module.c +SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/c_api.cpp + +CFLAGS_USERMOD += -I$(NCNN_MOD_DIR) -I$(NCNN_MOD_DIR)/../../build_micropython/install/include -fopenmp + +NCNN_LIB_PATH := $(shell find $(NCNN_MOD_DIR)/../../build_micropython/install -name "libncnn.*" -type f | head -1 | xargs dirname) + +LDFLAGS_USERMOD += -L$(NCNN_LIB_PATH) -lncnn -fopenmp -lstdc++ + +CXXFLAGS_USERMOD += -I$(NCNN_MOD_DIR) -I$(NCNN_MOD_DIR)/../../build_micropython/install/include -fopenmp \ No newline at end of file diff --git a/micropython/c_api/ncnn_module.c b/micropython/c_api/ncnn_module.c new file mode 100644 index 00000000000..cafa16b2356 --- /dev/null +++ b/micropython/c_api/ncnn_module.c @@ -0,0 +1,476 @@ +#include "ncnn_module.h" + +/* define function objects */ +static MP_DEFINE_CONST_FUN_OBJ_0(ncnn_version_obj, mp_ncnn_version); + +/* allocator api */ +static MP_DEFINE_CONST_FUN_OBJ_0(ncnn_allocator_create_pool_allocator_obj, mp_ncnn_allocator_create_pool_allocator); +static MP_DEFINE_CONST_FUN_OBJ_0(ncnn_allocator_create_unlocked_pool_allocator_obj, mp_ncnn_allocator_create_unlocked_pool_allocator); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_allocator_destroy_obj, mp_ncnn_allocator_destroy); + +/* option api */ +static MP_DEFINE_CONST_FUN_OBJ_0(ncnn_option_create_obj, mp_ncnn_option_create); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_option_destroy_obj, mp_ncnn_option_destroy); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_option_get_num_threads_obj, mp_ncnn_option_get_num_threads); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_option_set_num_threads_obj, mp_ncnn_option_set_num_threads); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_option_get_use_local_pool_allocator_obj, mp_ncnn_option_get_use_local_pool_allocator); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_option_set_use_local_pool_allocator_obj, mp_ncnn_option_set_use_local_pool_allocator); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_option_set_blob_allocator_obj, mp_ncnn_option_set_blob_allocator); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_option_set_workspace_allocator_obj, mp_ncnn_option_set_workspace_allocator); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_option_get_use_vulkan_compute_obj, mp_ncnn_option_get_use_vulkan_compute); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_option_set_use_vulkan_compute_obj, mp_ncnn_option_set_use_vulkan_compute); + +/* mat api */ +static MP_DEFINE_CONST_FUN_OBJ_0(ncnn_mat_create_obj, mp_ncnn_mat_create); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_mat_create_1d_obj, mp_ncnn_mat_create_1d); +static MP_DEFINE_CONST_FUN_OBJ_3(ncnn_mat_create_2d_obj, mp_ncnn_mat_create_2d); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_3d_obj, 4, 4, mp_ncnn_mat_create_3d); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_4d_obj, 5, 5, mp_ncnn_mat_create_4d); +static MP_DEFINE_CONST_FUN_OBJ_3(ncnn_mat_create_external_1d_obj, mp_ncnn_mat_create_external_1d); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_external_2d_obj, 4, 4, mp_ncnn_mat_create_external_2d); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_external_3d_obj, 5, 5, mp_ncnn_mat_create_external_3d); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_external_4d_obj, 6, 6, mp_ncnn_mat_create_external_4d); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_1d_elem_obj, 4, 4, mp_ncnn_mat_create_1d_elem); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_2d_elem_obj, 5, 5, mp_ncnn_mat_create_2d_elem); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_3d_elem_obj, 6, 6, mp_ncnn_mat_create_3d_elem); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_4d_elem_obj, 7, 7, mp_ncnn_mat_create_4d_elem); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_external_1d_elem_obj, 5, 5, mp_ncnn_mat_create_external_1d_elem); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_external_2d_elem_obj, 6, 6, mp_ncnn_mat_create_external_2d_elem); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_external_3d_elem_obj, 7, 7, mp_ncnn_mat_create_external_3d_elem); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_create_external_4d_elem_obj, 8, 8, mp_ncnn_mat_create_external_4d_elem); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_mat_destroy_obj, mp_ncnn_mat_destroy); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_mat_fill_float_obj, mp_ncnn_mat_fill_float); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_mat_clone_obj, mp_ncnn_mat_clone); +static MP_DEFINE_CONST_FUN_OBJ_3(ncnn_mat_reshape_1d_obj, mp_ncnn_mat_reshape_1d); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_reshape_2d_obj, 4, 4, mp_ncnn_mat_reshape_2d); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_reshape_3d_obj, 5, 5, mp_ncnn_mat_reshape_3d); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_reshape_4d_obj, 6, 6, mp_ncnn_mat_reshape_4d); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_mat_get_dims_obj, mp_ncnn_mat_get_dims); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_mat_get_w_obj, mp_ncnn_mat_get_w); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_mat_get_h_obj, mp_ncnn_mat_get_h); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_mat_get_d_obj, mp_ncnn_mat_get_d); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_mat_get_c_obj, mp_ncnn_mat_get_c); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_mat_get_elemsize_obj, mp_ncnn_mat_get_elemsize); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_mat_get_elempack_obj, mp_ncnn_mat_get_elempack); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_mat_get_cstep_obj, mp_ncnn_mat_get_cstep); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_mat_get_data_obj, mp_ncnn_mat_get_data); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_mat_get_channel_data_obj, mp_ncnn_mat_get_channel_data); + +/* mat pixel api */ +#if NCNN_PIXEL +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_from_pixels_obj, 6, 6, mp_ncnn_mat_from_pixels); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_from_pixels_resize_obj, 8, 8, mp_ncnn_mat_from_pixels_resize); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_from_pixels_resize_roi_obj, 10, 10, mp_ncnn_mat_from_pixels_roi); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_from_pixels_resize_roi_resize_obj, 12, 12, mp_ncnn_mat_from_pixels_roi_resize); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_to_pixels_obj, 4, 4, mp_ncnn_mat_to_pixels); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_to_pixels_resize_obj, 6, 6, mp_ncnn_mat_to_pixels_resize); +#endif + +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_mat_substract_mean_normalize_obj, 3, 3, mp_ncnn_mat_substract_mean_normalize); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_convert_packing_obj, 4, 4, mp_ncnn_convert_packing); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_flatten_obj, 3, 3, mp_ncnn_flatten); + +/* blob api */ +#if NCNN_STRING +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_blob_get_name_obj, mp_ncnn_blob_get_name); +#endif /* NCNN_STRING */ + +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_blob_get_producer_obj, mp_ncnn_blob_get_producer); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_blob_get_consumer_obj, mp_ncnn_blob_get_consumer); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_blob_get_shape_obj, 5, 5, mp_ncnn_blob_get_shape); + +/* paramdict api */ +static MP_DEFINE_CONST_FUN_OBJ_0(ncnn_paramdict_create_obj, mp_ncnn_paramdict_create); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_paramdict_destroy_obj, mp_ncnn_paramdict_destroy); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_paramdict_get_type_obj, mp_ncnn_paramdict_get_type); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_paramdict_get_int_obj, 3, 3, mp_ncnn_paramdict_get_int); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_paramdict_get_float_obj, 3, 3, mp_ncnn_paramdict_get_float); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_paramdict_get_array_obj, 3, 3, mp_ncnn_paramdict_get_array); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_paramdict_set_int_obj, 3, 3, mp_ncnn_paramdict_set_int); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_paramdict_set_float_obj, 3, 3, mp_ncnn_paramdict_set_float); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_paramdict_set_array_obj, 3, 3, mp_ncnn_paramdict_set_array); + +/* datareader api */ +static MP_DEFINE_CONST_FUN_OBJ_0(ncnn_datareader_create_obj, mp_ncnn_datareader_create); +#if NCNN_STDIO +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_datareader_create_from_stdio_obj, mp_ncnn_datareader_create_from_stdio); +#endif /* NCNN_STDIO */ +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_datareader_create_from_memory_obj, mp_ncnn_datareader_create_from_memory); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_datareader_destroy_obj, mp_ncnn_datareader_destroy); + +/* modelbin api */ +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_modelbin_create_from_datareader_obj, mp_ncnn_modelbin_create_from_datareader); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_modelbin_create_from_mat_array_obj, mp_ncnn_modelbin_create_from_mat_array); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_modelbin_destroy_obj, mp_ncnn_modelbin_destroy); + +/* layer api */ +static MP_DEFINE_CONST_FUN_OBJ_0(ncnn_layer_create_obj, mp_ncnn_layer_create); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_create_by_typeindex_obj, mp_ncnn_layer_create_by_typeindex); +#if NCNN_STRING +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_create_by_type_obj, mp_ncnn_layer_create_by_type); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_type_to_index_obj, mp_ncnn_layer_type_to_index); +#endif /* NCNN_STRING */ +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_destroy_obj, mp_ncnn_layer_destroy); + +#if NCNN_STRING +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_get_name_obj, mp_ncnn_layer_get_name); +#endif /* NCNN_STRING */ + +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_get_typeindex_obj, mp_ncnn_layer_get_typeindex); + +#if NCNN_STRING +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_get_type_obj, mp_ncnn_layer_get_type); +#endif /* NCNN_STRING */ + +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_get_one_blob_only_obj, mp_ncnn_layer_get_one_blob_only); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_get_support_inplace_obj, mp_ncnn_layer_get_support_inplace); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_get_support_vulkan_obj, mp_ncnn_layer_get_support_vulkan); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_get_support_packing_obj, mp_ncnn_layer_get_support_packing); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_get_support_bf16_storage_obj, mp_ncnn_layer_get_support_bf16_storage); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_get_support_fp16_storage_obj, mp_ncnn_layer_get_support_fp16_storage); + +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_layer_set_one_blob_only_obj, mp_ncnn_layer_set_one_blob_only); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_layer_set_support_inplace_obj, mp_ncnn_layer_set_support_inplace); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_layer_set_support_vulkan_obj, mp_ncnn_layer_set_support_vulkan); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_layer_set_support_packing_obj, mp_ncnn_layer_set_support_packing); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_layer_set_support_bf16_storage_obj, mp_ncnn_layer_set_support_bf16_storage); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_layer_set_support_fp16_storage_obj, mp_ncnn_layer_set_support_fp16_storage); + +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_get_bottom_count_obj, mp_ncnn_layer_get_bottom_count); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_layer_get_bottom_obj, mp_ncnn_layer_get_bottom); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_layer_get_top_count_obj, mp_ncnn_layer_get_top_count); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_layer_get_top_obj, mp_ncnn_layer_get_top); + +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_blob_get_bottom_shape_obj, 6, 6, mp_ncnn_blob_get_bottom_shape); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_blob_get_top_shape_obj, 6, 6, mp_ncnn_blob_get_top_shape); + +/* net api */ +static MP_DEFINE_CONST_FUN_OBJ_0(ncnn_net_create_obj, mp_ncnn_net_create); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_net_destroy_obj, mp_ncnn_net_destroy); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_net_get_option_obj, mp_ncnn_net_get_option); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_set_option_obj, mp_ncnn_net_set_option); + +#if NCNN_VULKAN +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_set_vulkan_device_obj, mp_ncnn_net_set_vulkan_device); +#endif + +#if NCNN_STRING +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_net_register_custom_layer_by_type_obj, 5, 5, mp_ncnn_net_register_custom_layer_by_type); +#endif /* NCNN_STRING */ +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_net_register_custom_layer_by_typeindex_obj, 5, 5, mp_ncnn_net_register_custom_layer_by_typeindex); + +#if NCNN_STDIO +#if NCNN_STRING +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_load_param_obj, mp_ncnn_net_load_param); +#endif /* NCNN_STRING */ +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_load_param_bin_obj, mp_ncnn_net_load_param_bin); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_load_model_obj, mp_ncnn_net_load_model); +#endif /* NCNN_STDIO */ + +#if NCNN_STDIO +#if NCNN_STRING +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_load_param_memory_obj, mp_ncnn_net_load_param_memory); +#endif /* NCNN_STRING */ +#endif /* NCNN_STDIO */ +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_load_param_bin_memory_obj, mp_ncnn_net_load_param_bin_memory); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_load_model_memory_obj, mp_ncnn_net_load_model_memory); + +#if NCNN_STRING +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_load_param_datareader_obj, mp_ncnn_net_load_param_datareader); +#endif /* NCNN_STRING */ +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_load_param_bin_datareader_obj, mp_ncnn_net_load_param_bin_datareader); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_load_model_datareader_obj, mp_ncnn_net_load_model_datareader); + +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_net_clear_obj, mp_ncnn_net_clear); + +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_net_get_input_count_obj, mp_ncnn_net_get_input_count); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_net_get_output_count_obj, mp_ncnn_net_get_output_count); + +#if NCNN_STRING +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_get_input_name_obj, mp_ncnn_net_get_input_name); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_get_output_name_obj, mp_ncnn_net_get_output_name); +#endif /* NCNN_STRING */ +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_get_input_index_obj, mp_ncnn_net_get_input_index); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_net_get_output_index_obj, mp_ncnn_net_get_output_index); + +/* extractor api */ +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_extractor_create_obj, mp_ncnn_extractor_create); +static MP_DEFINE_CONST_FUN_OBJ_1(ncnn_extractor_destroy_obj, mp_ncnn_extractor_destroy); +static MP_DEFINE_CONST_FUN_OBJ_2(ncnn_extractor_set_option_obj, mp_ncnn_extractor_set_option); + +#if NCNN_STRING +static MP_DEFINE_CONST_FUN_OBJ_3(ncnn_extractor_input_obj, mp_ncnn_extractor_input); +static MP_DEFINE_CONST_FUN_OBJ_3(ncnn_extractor_extract_obj, mp_ncnn_extractor_extract); +#endif /* NCNN_STRING */ +static MP_DEFINE_CONST_FUN_OBJ_3(ncnn_extractor_input_index_obj, mp_ncnn_extractor_input_index); +static MP_DEFINE_CONST_FUN_OBJ_3(ncnn_extractor_extract_index_obj, mp_ncnn_extractor_extract_index); + +/* mat process api */ +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_copy_make_border_obj, 9, 9, mp_ncnn_copy_make_border); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_copy_make_border_3d_obj, 11, 11, mp_ncnn_copy_make_border_3d); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_copy_cut_border_obj, 7, 7, mp_ncnn_copy_cut_border); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_copy_cut_border_3d_obj, 9, 9, mp_ncnn_copy_cut_border_3d); + +#if NCNN_PIXEL_DRAWING +/* mat pixel drawing api*/ +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_rectangle_c1_obj, 9, 9, mp_ncnn_draw_rectangle_c1); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_rectangle_c2_obj, 9, 9, mp_ncnn_draw_rectangle_c2); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_rectangle_c3_obj, 9, 9, mp_ncnn_draw_rectangle_c3); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_rectangle_c4_obj, 9, 9, mp_ncnn_draw_rectangle_c4); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_text_c1_obj, 8, 8, mp_ncnn_draw_text_c1); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_text_c2_obj, 8, 8, mp_ncnn_draw_text_c2); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_text_c3_obj, 8, 8, mp_ncnn_draw_text_c3); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_text_c4_obj, 8, 8, mp_ncnn_draw_text_c4); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_circle_c1_obj, 8, 8, mp_ncnn_draw_circle_c1); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_circle_c2_obj, 8, 8, mp_ncnn_draw_circle_c2); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_circle_c3_obj, 8, 8, mp_ncnn_draw_circle_c3); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_circle_c4_obj, 8, 8, mp_ncnn_draw_circle_c4); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_line_c1_obj, 9, 9, mp_ncnn_draw_line_c1); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_line_c2_obj, 9, 9, mp_ncnn_draw_line_c2); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_line_c3_obj, 9, 9, mp_ncnn_draw_line_c3); +static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(ncnn_draw_line_c4_obj, 9, 9, mp_ncnn_draw_line_c4); +#endif /* NCNN_PIXEL_DRAWING */ + +/* globals table */ +static const mp_rom_map_elem_t ncnn_module_globals_table[] = { + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ncnn)}, + {MP_ROM_QSTR(MP_QSTR_version), MP_ROM_PTR(&ncnn_version_obj)}, + + /* allocator API */ + {MP_ROM_QSTR(MP_QSTR_allocator_create_pool_allocator), MP_ROM_PTR(&ncnn_allocator_create_pool_allocator_obj)}, + {MP_ROM_QSTR(MP_QSTR_allocator_create_unlocked_pool_allocator), MP_ROM_PTR(&ncnn_allocator_create_unlocked_pool_allocator_obj)}, + {MP_ROM_QSTR(MP_QSTR_allocator_destroy), MP_ROM_PTR(&ncnn_allocator_destroy_obj)}, + + /* option API */ + {MP_ROM_QSTR(MP_QSTR_option_create), MP_ROM_PTR(&ncnn_option_create_obj)}, + {MP_ROM_QSTR(MP_QSTR_option_destroy), MP_ROM_PTR(&ncnn_option_destroy_obj)}, + {MP_ROM_QSTR(MP_QSTR_option_get_num_threads), MP_ROM_PTR(&ncnn_option_get_num_threads_obj)}, + {MP_ROM_QSTR(MP_QSTR_option_set_num_threads), MP_ROM_PTR(&ncnn_option_set_num_threads_obj)}, + {MP_ROM_QSTR(MP_QSTR_option_get_use_local_pool_allocator), MP_ROM_PTR(&ncnn_option_get_use_local_pool_allocator_obj)}, + {MP_ROM_QSTR(MP_QSTR_option_set_use_local_pool_allocator), MP_ROM_PTR(&ncnn_option_set_use_local_pool_allocator_obj)}, + {MP_ROM_QSTR(MP_QSTR_option_set_blob_allocator), MP_ROM_PTR(&ncnn_option_set_blob_allocator_obj)}, + {MP_ROM_QSTR(MP_QSTR_option_set_workspace_allocator), MP_ROM_PTR(&ncnn_option_set_workspace_allocator_obj)}, + {MP_ROM_QSTR(MP_QSTR_option_get_use_vulkan_compute), MP_ROM_PTR(&ncnn_option_get_use_vulkan_compute_obj)}, + {MP_ROM_QSTR(MP_QSTR_option_set_use_vulkan_compute), MP_ROM_PTR(&ncnn_option_set_use_vulkan_compute_obj)}, + + /* mat API */ + {MP_ROM_QSTR(MP_QSTR_mat_create), MP_ROM_PTR(&ncnn_mat_create_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_1d), MP_ROM_PTR(&ncnn_mat_create_1d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_2d), MP_ROM_PTR(&ncnn_mat_create_2d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_3d), MP_ROM_PTR(&ncnn_mat_create_3d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_4d), MP_ROM_PTR(&ncnn_mat_create_4d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_external_1d), MP_ROM_PTR(&ncnn_mat_create_external_1d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_external_2d), MP_ROM_PTR(&ncnn_mat_create_external_2d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_external_3d), MP_ROM_PTR(&ncnn_mat_create_external_3d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_external_4d), MP_ROM_PTR(&ncnn_mat_create_external_4d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_1d_elem), MP_ROM_PTR(&ncnn_mat_create_1d_elem_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_2d_elem), MP_ROM_PTR(&ncnn_mat_create_2d_elem_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_3d_elem), MP_ROM_PTR(&ncnn_mat_create_3d_elem_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_4d_elem), MP_ROM_PTR(&ncnn_mat_create_4d_elem_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_external_1d_elem), MP_ROM_PTR(&ncnn_mat_create_external_1d_elem_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_external_2d_elem), MP_ROM_PTR(&ncnn_mat_create_external_2d_elem_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_external_3d_elem), MP_ROM_PTR(&ncnn_mat_create_external_3d_elem_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_create_external_4d_elem), MP_ROM_PTR(&ncnn_mat_create_external_4d_elem_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_destroy), MP_ROM_PTR(&ncnn_mat_destroy_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_fill_float), MP_ROM_PTR(&ncnn_mat_fill_float_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_clone), MP_ROM_PTR(&ncnn_mat_clone_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_reshape_1d), MP_ROM_PTR(&ncnn_mat_reshape_1d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_reshape_2d), MP_ROM_PTR(&ncnn_mat_reshape_2d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_reshape_3d), MP_ROM_PTR(&ncnn_mat_reshape_3d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_reshape_4d), MP_ROM_PTR(&ncnn_mat_reshape_4d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_get_dims), MP_ROM_PTR(&ncnn_mat_get_dims_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_get_w), MP_ROM_PTR(&ncnn_mat_get_w_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_get_h), MP_ROM_PTR(&ncnn_mat_get_h_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_get_d), MP_ROM_PTR(&ncnn_mat_get_d_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_get_c), MP_ROM_PTR(&ncnn_mat_get_c_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_get_elemsize), MP_ROM_PTR(&ncnn_mat_get_elemsize_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_get_elempack), MP_ROM_PTR(&ncnn_mat_get_elempack_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_get_cstep), MP_ROM_PTR(&ncnn_mat_get_cstep_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_get_data), MP_ROM_PTR(&ncnn_mat_get_data_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_get_channel_data), MP_ROM_PTR(&ncnn_mat_get_channel_data_obj)}, + +#if NCNN_PIXEL + /* mat pixel api */ + {MP_ROM_QSTR(MP_QSTR_mat_from_pixels), MP_ROM_PTR(&ncnn_mat_from_pixels_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_from_pixels_resize), MP_ROM_PTR(&ncnn_mat_from_pixels_resize_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_from_pixels_resize_roi), MP_ROM_PTR(&ncnn_mat_from_pixels_resize_roi_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_from_pixels_resize_roi_resize), MP_ROM_PTR(&ncnn_mat_from_pixels_resize_roi_resize_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_to_pixels), MP_ROM_PTR(&ncnn_mat_to_pixels_obj)}, + {MP_ROM_QSTR(MP_QSTR_mat_to_pixels_resize), MP_ROM_PTR(&ncnn_mat_to_pixels_resize_obj)}, +#endif + + {MP_ROM_QSTR(MP_QSTR_mat_substract_mean_normalize), MP_ROM_PTR(&ncnn_mat_substract_mean_normalize_obj)}, + {MP_ROM_QSTR(MP_QSTR_convert_packing), MP_ROM_PTR(&ncnn_convert_packing_obj)}, + {MP_ROM_QSTR(MP_QSTR_flatten), MP_ROM_PTR(&ncnn_flatten_obj)}, + +/* blob api */ +#if NCNN_STRING + {MP_ROM_QSTR(MP_QSTR_blob_get_name), MP_ROM_PTR(&ncnn_blob_get_name_obj)}, +#endif + + {MP_ROM_QSTR(MP_QSTR_blob_get_producer), MP_ROM_PTR(&ncnn_blob_get_producer_obj)}, + {MP_ROM_QSTR(MP_QSTR_blob_get_consumer), MP_ROM_PTR(&ncnn_blob_get_consumer_obj)}, + + {MP_ROM_QSTR(MP_QSTR_blob_get_shape), MP_ROM_PTR(&ncnn_blob_get_shape_obj)}, + + /* paramdict api */ + {MP_ROM_QSTR(MP_QSTR_paramdict_create), MP_ROM_PTR(&ncnn_paramdict_create_obj)}, + {MP_ROM_QSTR(MP_QSTR_paramdict_destroy), MP_ROM_PTR(&ncnn_paramdict_destroy_obj)}, + {MP_ROM_QSTR(MP_QSTR_paramdict_get_type), MP_ROM_PTR(&ncnn_paramdict_get_type_obj)}, + {MP_ROM_QSTR(MP_QSTR_paramdict_get_int), MP_ROM_PTR(&ncnn_paramdict_get_int_obj)}, + {MP_ROM_QSTR(MP_QSTR_paramdict_get_float), MP_ROM_PTR(&ncnn_paramdict_get_float_obj)}, + {MP_ROM_QSTR(MP_QSTR_paramdict_get_array), MP_ROM_PTR(&ncnn_paramdict_get_array_obj)}, + {MP_ROM_QSTR(MP_QSTR_paramdict_set_int), MP_ROM_PTR(&ncnn_paramdict_set_int_obj)}, + {MP_ROM_QSTR(MP_QSTR_paramdict_set_float), MP_ROM_PTR(&ncnn_paramdict_set_float_obj)}, + {MP_ROM_QSTR(MP_QSTR_paramdict_set_array), MP_ROM_PTR(&ncnn_paramdict_set_array_obj)}, + + /* datareader api */ + {MP_ROM_QSTR(MP_QSTR_datareader_create), MP_ROM_PTR(&ncnn_datareader_create_obj)}, +#if NCNN_STDIO + {MP_ROM_QSTR(MP_QSTR_datareader_create_from_stdio), MP_ROM_PTR(&ncnn_datareader_create_from_stdio_obj)}, +#endif /* NCNN_STDIO */ + {MP_ROM_QSTR(MP_QSTR_datareader_create_from_memory), MP_ROM_PTR(&ncnn_datareader_create_from_memory_obj)}, + {MP_ROM_QSTR(MP_QSTR_datareader_destroy), MP_ROM_PTR(&ncnn_datareader_destroy_obj)}, + + /* modelbin api */ + {MP_ROM_QSTR(MP_QSTR_modelbin_create_from_datareader), MP_ROM_PTR(&ncnn_modelbin_create_from_datareader_obj)}, + {MP_ROM_QSTR(MP_QSTR_modelbin_create_from_mat_array), MP_ROM_PTR(&ncnn_modelbin_create_from_mat_array_obj)}, + {MP_ROM_QSTR(MP_QSTR_modelbin_destroy), MP_ROM_PTR(&ncnn_modelbin_destroy_obj)}, + + /* layer api */ + {MP_ROM_QSTR(MP_QSTR_layer_create), MP_ROM_PTR(&ncnn_layer_create_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_create_by_typeindex), MP_ROM_PTR(&ncnn_layer_create_by_typeindex_obj)}, +#if NCNN_STRING + {MP_ROM_QSTR(MP_QSTR_layer_create_by_type), MP_ROM_PTR(&ncnn_layer_create_by_type_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_type_to_index), MP_ROM_PTR(&ncnn_layer_type_to_index_obj)}, +#endif /* NCNN_STRING */ + {MP_ROM_QSTR(MP_QSTR_layer_destroy), MP_ROM_PTR(&ncnn_layer_destroy_obj)}, + +#if NCNN_STRING + {MP_ROM_QSTR(MP_QSTR_layer_get_name), MP_ROM_PTR(&ncnn_layer_get_name_obj)}, +#endif /* NCNN_STRING */ + + {MP_ROM_QSTR(MP_QSTR_layer_get_typeindex), MP_ROM_PTR(&ncnn_layer_get_typeindex_obj)}, +#if NCNN_STRING + {MP_ROM_QSTR(MP_QSTR_layer_get_type), MP_ROM_PTR(&ncnn_layer_get_type_obj)}, +#endif /* NCNN_STRING */ + + {MP_ROM_QSTR(MP_QSTR_layer_get_one_blob_only), MP_ROM_PTR(&ncnn_layer_get_one_blob_only_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_get_support_inplace), MP_ROM_PTR(&ncnn_layer_get_support_inplace_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_get_support_vulkan), MP_ROM_PTR(&ncnn_layer_get_support_vulkan_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_get_support_packing), MP_ROM_PTR(&ncnn_layer_get_support_packing_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_get_support_bf16_storage), MP_ROM_PTR(&ncnn_layer_get_support_bf16_storage_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_get_support_fp16_storage), MP_ROM_PTR(&ncnn_layer_get_support_fp16_storage_obj)}, + + {MP_ROM_QSTR(MP_QSTR_layer_set_one_blob_only), MP_ROM_PTR(&ncnn_layer_set_one_blob_only_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_set_support_inplace), MP_ROM_PTR(&ncnn_layer_set_support_inplace_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_set_support_vulkan), MP_ROM_PTR(&ncnn_layer_set_support_vulkan_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_set_support_packing), MP_ROM_PTR(&ncnn_layer_set_support_packing_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_set_support_bf16_storage), MP_ROM_PTR(&ncnn_layer_set_support_bf16_storage_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_set_support_fp16_storage), MP_ROM_PTR(&ncnn_layer_set_support_fp16_storage_obj)}, + + {MP_ROM_QSTR(MP_QSTR_layer_get_bottom_count), MP_ROM_PTR(&ncnn_layer_get_bottom_count_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_get_bottom), MP_ROM_PTR(&ncnn_layer_get_bottom_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_get_top_count), MP_ROM_PTR(&ncnn_layer_get_top_count_obj)}, + {MP_ROM_QSTR(MP_QSTR_layer_get_top), MP_ROM_PTR(&ncnn_layer_get_top_obj)}, + + {MP_ROM_QSTR(MP_QSTR_blob_get_bottom_shape), MP_ROM_PTR(&ncnn_blob_get_bottom_shape_obj)}, + {MP_ROM_QSTR(MP_QSTR_blob_get_top_shape), MP_ROM_PTR(&ncnn_blob_get_top_shape_obj)}, + + /* net api */ + {MP_ROM_QSTR(MP_QSTR_net_create), MP_ROM_PTR(&ncnn_net_create_obj)}, + {MP_ROM_QSTR(MP_QSTR_net_destroy), MP_ROM_PTR(&ncnn_net_destroy_obj)}, + {MP_ROM_QSTR(MP_QSTR_net_get_option), MP_ROM_PTR(&ncnn_net_get_option_obj)}, + {MP_ROM_QSTR(MP_QSTR_net_set_option), MP_ROM_PTR(&ncnn_net_set_option_obj)}, +#if NCNN_VULKAN + {MP_ROM_QSTR(MP_QSTR_net_set_vulkan_device), MP_ROM_PTR(&ncnn_net_set_vulkan_device_obj)}, +#endif + +#if NCNN_STRING + {MP_ROM_QSTR(MP_QSTR_net_register_custom_layer_by_type), MP_ROM_PTR(&ncnn_net_register_custom_layer_by_type_obj)}, +#endif /* NCNN_STRING */ + {MP_ROM_QSTR(MP_QSTR_net_register_custom_layer_by_typeindex), MP_ROM_PTR(&ncnn_net_register_custom_layer_by_typeindex_obj)}, + +#if NCNN_STDIO +#if NCNN_STRING + {MP_ROM_QSTR(MP_QSTR_net_load_param), MP_ROM_PTR(&ncnn_net_load_param_obj)}, +#endif /* NCNN_STRING */ + {MP_ROM_QSTR(MP_QSTR_net_load_param_bin), MP_ROM_PTR(&ncnn_net_load_param_bin_obj)}, + {MP_ROM_QSTR(MP_QSTR_net_load_model), MP_ROM_PTR(&ncnn_net_load_model_obj)}, +#endif /* NCNN_STDIO */ + +#if NCNN_STDIO +#if NCNN_STRING + {MP_ROM_QSTR(MP_QSTR_net_load_param_memory), MP_ROM_PTR(&ncnn_net_load_param_memory_obj)}, +#endif /* NCNN_STRING */ +#endif /* NCNN_STDIO */ + {MP_ROM_QSTR(MP_QSTR_net_load_param_bin_memory), MP_ROM_PTR(&ncnn_net_load_param_bin_memory_obj)}, + {MP_ROM_QSTR(MP_QSTR_net_load_model_memory), MP_ROM_PTR(&ncnn_net_load_model_memory_obj)}, + +#if NCNN_STRING + {MP_ROM_QSTR(MP_QSTR_net_load_param_datareader), MP_ROM_PTR(&ncnn_net_load_param_datareader_obj)}, +#endif /* NCNN_STRING */ + {MP_ROM_QSTR(MP_QSTR_net_load_param_bin_datareader), MP_ROM_PTR(&ncnn_net_load_param_bin_datareader_obj)}, + {MP_ROM_QSTR(MP_QSTR_net_load_model_datareader), MP_ROM_PTR(&ncnn_net_load_model_datareader_obj)}, + + {MP_ROM_QSTR(MP_QSTR_net_clear), MP_ROM_PTR(&ncnn_net_clear_obj)}, + + {MP_ROM_QSTR(MP_QSTR_net_get_input_count), MP_ROM_PTR(&ncnn_net_get_input_count_obj)}, + {MP_ROM_QSTR(MP_QSTR_net_get_output_count), MP_ROM_PTR(&ncnn_net_get_output_count_obj)}, +#if NCNN_STRING + {MP_ROM_QSTR(MP_QSTR_net_get_input_name), MP_ROM_PTR(&ncnn_net_get_input_name_obj)}, + {MP_ROM_QSTR(MP_QSTR_net_get_output_name), MP_ROM_PTR(&ncnn_net_get_output_name_obj)}, +#endif /* NCNN_STRING */ + {MP_ROM_QSTR(MP_QSTR_net_get_input_index), MP_ROM_PTR(&ncnn_net_get_input_index_obj)}, + {MP_ROM_QSTR(MP_QSTR_net_get_output_index), MP_ROM_PTR(&ncnn_net_get_output_index_obj)}, + + /* extractor api */ + {MP_ROM_QSTR(MP_QSTR_extractor_create), MP_ROM_PTR(&ncnn_extractor_create_obj)}, + {MP_ROM_QSTR(MP_QSTR_extractor_destroy), MP_ROM_PTR(&ncnn_extractor_destroy_obj)}, + {MP_ROM_QSTR(MP_QSTR_extractor_set_option), MP_ROM_PTR(&ncnn_extractor_set_option_obj)}, + +#if NCNN_STRING + {MP_ROM_QSTR(MP_QSTR_extractor_input), MP_ROM_PTR(&ncnn_extractor_input_obj)}, + {MP_ROM_QSTR(MP_QSTR_extractor_extract), MP_ROM_PTR(&ncnn_extractor_extract_obj)}, +#endif /* NCNN_STRING */ + {MP_ROM_QSTR(MP_QSTR_extractor_input_index), MP_ROM_PTR(&ncnn_extractor_input_index_obj)}, + {MP_ROM_QSTR(MP_QSTR_extractor_extract_index), MP_ROM_PTR(&ncnn_extractor_extract_index_obj)}, + + /* mat process api */ + {MP_ROM_QSTR(MP_QSTR_copy_make_border), MP_ROM_PTR(&ncnn_copy_make_border_obj)}, + {MP_ROM_QSTR(MP_QSTR_copy_make_border_3d), MP_ROM_PTR(&ncnn_copy_make_border_3d_obj)}, + {MP_ROM_QSTR(MP_QSTR_copy_cut_border), MP_ROM_PTR(&ncnn_copy_cut_border_obj)}, + {MP_ROM_QSTR(MP_QSTR_copy_cut_border_3d), MP_ROM_PTR(&ncnn_copy_cut_border_3d_obj)}, + +#if NCNN_PIXEL_DRAWING + /* mat pixel drawing api*/ + {MP_ROM_QSTR(MP_QSTR_draw_rectangle_c1), MP_ROM_PTR(&ncnn_draw_rectangle_c1_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_rectangle_c2), MP_ROM_PTR(&ncnn_draw_rectangle_c2_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_rectangle_c3), MP_ROM_PTR(&ncnn_draw_rectangle_c3_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_rectangle_c4), MP_ROM_PTR(&ncnn_draw_rectangle_c4_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_text_c1), MP_ROM_PTR(&ncnn_draw_text_c1_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_text_c2), MP_ROM_PTR(&ncnn_draw_text_c2_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_text_c3), MP_ROM_PTR(&ncnn_draw_text_c3_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_text_c4), MP_ROM_PTR(&ncnn_draw_text_c4_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_circle_c1), MP_ROM_PTR(&ncnn_draw_circle_c1_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_circle_c2), MP_ROM_PTR(&ncnn_draw_circle_c2_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_circle_c3), MP_ROM_PTR(&ncnn_draw_circle_c3_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_circle_c4), MP_ROM_PTR(&ncnn_draw_circle_c4_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_line_c1), MP_ROM_PTR(&ncnn_draw_line_c1_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_line_c2), MP_ROM_PTR(&ncnn_draw_line_c2_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_line_c3), MP_ROM_PTR(&ncnn_draw_line_c3_obj)}, + {MP_ROM_QSTR(MP_QSTR_draw_line_c4), MP_ROM_PTR(&ncnn_draw_line_c4_obj)}, +#endif /* NCNN_PIXEL_DRAWING */ +}; + +/* define the module globals dictionary */ +static MP_DEFINE_CONST_DICT(ncnn_module_globals, ncnn_module_globals_table); + +/* define the module object */ +const mp_obj_module_t ncnn_user_cmodule = { + .base = {&mp_type_module}, + .globals = (mp_obj_dict_t*)&ncnn_module_globals, +}; + +/* register module */ +MP_REGISTER_MODULE(MP_QSTR_ncnn, ncnn_user_cmodule); diff --git a/micropython/c_api/ncnn_module.h b/micropython/c_api/ncnn_module.h new file mode 100644 index 00000000000..c10a4a79e41 --- /dev/null +++ b/micropython/c_api/ncnn_module.h @@ -0,0 +1,192 @@ +#ifndef NCNN_MODULE_H +#define NCNN_MODULE_H + +#include "py/runtime.h" + +extern mp_obj_t mp_ncnn_version(void); + +/* allocator api */ +extern mp_obj_t mp_ncnn_allocator_create_pool_allocator(void); +extern mp_obj_t mp_ncnn_allocator_create_unlocked_pool_allocator(void); +extern mp_obj_t mp_ncnn_allocator_destroy(mp_obj_t ncnn_allocator_obj); + +/* option api */ +extern mp_obj_t mp_ncnn_option_create(void); +extern mp_obj_t mp_ncnn_option_destroy(mp_obj_t option_obj); +extern mp_obj_t mp_ncnn_option_get_num_threads(mp_obj_t option_obj); +extern mp_obj_t mp_ncnn_option_set_num_threads(mp_obj_t option_obj, mp_obj_t num_threads_obj); +extern mp_obj_t mp_ncnn_option_get_use_local_pool_allocator(mp_obj_t option_obj); +extern mp_obj_t mp_ncnn_option_set_use_local_pool_allocator(mp_obj_t option_obj, mp_obj_t use_local_pool_allocator_obj); +extern mp_obj_t mp_ncnn_option_set_blob_allocator(mp_obj_t option_obj, mp_obj_t allocator_obj); +extern mp_obj_t mp_ncnn_option_set_workspace_allocator(mp_obj_t option_obj, mp_obj_t allocator_obj); +extern mp_obj_t mp_ncnn_option_get_use_vulkan_compute(mp_obj_t option_obj); +extern mp_obj_t mp_ncnn_option_set_use_vulkan_compute(mp_obj_t option_obj, mp_obj_t use_vulkan_compute_obj); + +/* mat api */ +extern mp_obj_t mp_ncnn_mat_create(void); +extern mp_obj_t mp_ncnn_mat_create_1d(mp_obj_t w_obj, mp_obj_t allocator_obj); +extern mp_obj_t mp_ncnn_mat_create_2d(mp_obj_t w_obj, mp_obj_t h_obj, mp_obj_t allocator_obj); +extern mp_obj_t mp_ncnn_mat_create_3d(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_create_4d(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_create_external_1d(mp_obj_t w_obj, mp_obj_t data_obj, mp_obj_t allocator_obj); +extern mp_obj_t mp_ncnn_mat_create_external_2d(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_create_external_3d(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_create_external_4d(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_create_1d_elem(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_create_2d_elem(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_create_3d_elem(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_create_4d_elem(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_create_external_1d_elem(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_create_external_2d_elem(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_create_external_3d_elem(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_create_external_4d_elem(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_destroy(mp_obj_t mat_obj); + +extern mp_obj_t mp_ncnn_mat_fill_float(mp_obj_t mat_obj, mp_obj_t v_obj); + +extern mp_obj_t mp_ncnn_mat_clone(mp_obj_t mat_obj, mp_obj_t allocator_obj); +extern mp_obj_t mp_ncnn_mat_reshape_1d(mp_obj_t mat_obj, mp_obj_t w_obj, mp_obj_t allocator_obj); +extern mp_obj_t mp_ncnn_mat_reshape_2d(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_reshape_3d(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_reshape_4d(size_t n_args, const mp_obj_t* args); + +extern mp_obj_t mp_ncnn_mat_get_dims(mp_obj_t mat_obj); +extern mp_obj_t mp_ncnn_mat_get_w(mp_obj_t mat_obj); +extern mp_obj_t mp_ncnn_mat_get_h(mp_obj_t mat_obj); +extern mp_obj_t mp_ncnn_mat_get_d(mp_obj_t mat_obj); +extern mp_obj_t mp_ncnn_mat_get_c(mp_obj_t mat_obj); +extern mp_obj_t mp_ncnn_mat_get_elemsize(mp_obj_t mat_obj); +extern mp_obj_t mp_ncnn_mat_get_elempack(mp_obj_t mat_obj); +extern mp_obj_t mp_ncnn_mat_get_cstep(mp_obj_t mat_obj); +extern mp_obj_t mp_ncnn_mat_get_data(mp_obj_t mat_obj); + +extern mp_obj_t mp_ncnn_mat_get_channel_data(mp_obj_t mat_obj, mp_obj_t c_obj); + +/* pixel api */ +extern mp_obj_t mp_ncnn_mat_from_pixels(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_from_pixels_resize(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_from_pixels_roi(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_from_pixels_roi_resize(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_to_pixels(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_mat_to_pixels_resize(size_t n_args, const mp_obj_t* args); + +/* mat processing */ +extern mp_obj_t mp_ncnn_mat_substract_mean_normalize(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_convert_packing(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_flatten(size_t n_args, const mp_obj_t* args); + +/* blob api */ +extern mp_obj_t mp_ncnn_blob_get_name(mp_obj_t blob_obj); +extern mp_obj_t mp_ncnn_blob_get_producer(mp_obj_t blob_obj); +extern mp_obj_t mp_ncnn_blob_get_consumer(mp_obj_t blob_obj); +extern mp_obj_t mp_ncnn_blob_get_shape(size_t n_args, const mp_obj_t* args); + +/* paramdict api */ +extern mp_obj_t mp_ncnn_paramdict_create(void); +extern mp_obj_t mp_ncnn_paramdict_destroy(mp_obj_t pd_obj); +extern mp_obj_t mp_ncnn_paramdict_get_type(mp_obj_t pd_obj, mp_obj_t id_obj); +extern mp_obj_t mp_ncnn_paramdict_get_int(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_paramdict_get_float(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_paramdict_get_array(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_paramdict_set_int(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_paramdict_set_float(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_paramdict_set_array(size_t n_args, const mp_obj_t* args); + +/* datareader api */ +extern mp_obj_t mp_ncnn_datareader_create(void); +extern mp_obj_t mp_ncnn_datareader_create_from_stdio(mp_obj_t fp_obj); +extern mp_obj_t mp_ncnn_datareader_create_from_memory(mp_obj_t mem_obj); +extern mp_obj_t mp_ncnn_datareader_destroy(mp_obj_t dr_obj); + +/* modelbin api */ +extern mp_obj_t mp_ncnn_modelbin_create_from_datareader(mp_obj_t dr_obj); +extern mp_obj_t mp_ncnn_modelbin_create_from_mat_array(mp_obj_t weights_obj, mp_obj_t n_obj); +extern mp_obj_t mp_ncnn_modelbin_destroy(mp_obj_t mb_obj); + +/* layer api */ +extern mp_obj_t mp_ncnn_layer_create(void); +extern mp_obj_t mp_ncnn_layer_create_by_typeindex(mp_obj_t typeindex_obj); +extern mp_obj_t mp_ncnn_layer_create_by_type(mp_obj_t type_obj); +extern mp_obj_t mp_ncnn_layer_type_to_index(mp_obj_t type_obj); +extern mp_obj_t mp_ncnn_layer_destroy(mp_obj_t layer_obj); +extern mp_obj_t mp_ncnn_layer_get_name(mp_obj_t layer_obj); +extern mp_obj_t mp_ncnn_layer_get_typeindex(mp_obj_t layer_obj); +extern mp_obj_t mp_ncnn_layer_get_type(mp_obj_t layer_obj); +extern mp_obj_t mp_ncnn_layer_get_one_blob_only(mp_obj_t layer_obj); +extern mp_obj_t mp_ncnn_layer_get_support_inplace(mp_obj_t layer_obj); +extern mp_obj_t mp_ncnn_layer_get_support_vulkan(mp_obj_t layer_obj); +extern mp_obj_t mp_ncnn_layer_get_support_packing(mp_obj_t layer_obj); +extern mp_obj_t mp_ncnn_layer_get_support_bf16_storage(mp_obj_t layer_obj); +extern mp_obj_t mp_ncnn_layer_get_support_fp16_storage(mp_obj_t layer_obj); +extern mp_obj_t mp_ncnn_layer_set_one_blob_only(mp_obj_t layer_obj, mp_obj_t enable_obj); +extern mp_obj_t mp_ncnn_layer_set_support_inplace(mp_obj_t layer_obj, mp_obj_t enable_obj); +extern mp_obj_t mp_ncnn_layer_set_support_vulkan(mp_obj_t layer_obj, mp_obj_t enable_obj); +extern mp_obj_t mp_ncnn_layer_set_support_packing(mp_obj_t layer_obj, mp_obj_t enable_obj); +extern mp_obj_t mp_ncnn_layer_set_support_bf16_storage(mp_obj_t layer_obj, mp_obj_t enable_obj); +extern mp_obj_t mp_ncnn_layer_set_support_fp16_storage(mp_obj_t layer_obj, mp_obj_t enable_obj); +extern mp_obj_t mp_ncnn_layer_get_bottom_count(mp_obj_t layer_obj); +extern mp_obj_t mp_ncnn_layer_get_bottom(mp_obj_t layer_obj, mp_obj_t i_obj); +extern mp_obj_t mp_ncnn_layer_get_top_count(mp_obj_t layer_obj); +extern mp_obj_t mp_ncnn_layer_get_top(mp_obj_t layer_obj, mp_obj_t i_obj); +extern mp_obj_t mp_ncnn_blob_get_bottom_shape(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_blob_get_top_shape(size_t n_args, const mp_obj_t* args); + +/* net api */ +extern mp_obj_t mp_ncnn_net_create(void); +extern mp_obj_t mp_ncnn_net_destroy(mp_obj_t net_obj); +extern mp_obj_t mp_ncnn_net_get_option(mp_obj_t net_obj); +extern mp_obj_t mp_ncnn_net_set_option(mp_obj_t net_obj, mp_obj_t opt_obj); +extern mp_obj_t mp_ncnn_net_set_vulkan_device(mp_obj_t net_obj, mp_obj_t device_index_obj); +extern mp_obj_t mp_ncnn_net_register_custom_layer_by_type(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_net_register_custom_layer_by_typeindex(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_net_load_param(mp_obj_t net_obj, mp_obj_t path_obj); +extern mp_obj_t mp_ncnn_net_load_param_bin(mp_obj_t net_obj, mp_obj_t path_obj); +extern mp_obj_t mp_ncnn_net_load_model(mp_obj_t net_obj, mp_obj_t path_obj); +extern mp_obj_t mp_ncnn_net_load_param_memory(mp_obj_t net_obj, mp_obj_t mem_obj); +extern mp_obj_t mp_ncnn_net_load_param_bin_memory(mp_obj_t net_obj, mp_obj_t mem_obj); +extern mp_obj_t mp_ncnn_net_load_model_memory(mp_obj_t net_obj, mp_obj_t mem_obj); +extern mp_obj_t mp_ncnn_net_load_param_datareader(mp_obj_t net_obj, mp_obj_t dr_obj); +extern mp_obj_t mp_ncnn_net_load_param_bin_datareader(mp_obj_t net_obj, mp_obj_t dr_obj); +extern mp_obj_t mp_ncnn_net_load_model_datareader(mp_obj_t net_obj, mp_obj_t dr_obj); +extern mp_obj_t mp_ncnn_net_clear(mp_obj_t net_obj); +extern mp_obj_t mp_ncnn_net_get_input_count(mp_obj_t net_obj); +extern mp_obj_t mp_ncnn_net_get_output_count(mp_obj_t net_obj); +extern mp_obj_t mp_ncnn_net_get_input_name(mp_obj_t net_obj, mp_obj_t i_obj); +extern mp_obj_t mp_ncnn_net_get_output_name(mp_obj_t net_obj, mp_obj_t i_obj); +extern mp_obj_t mp_ncnn_net_get_input_index(mp_obj_t net_obj, mp_obj_t i_obj); +extern mp_obj_t mp_ncnn_net_get_output_index(mp_obj_t net_obj, mp_obj_t i_obj); + +/* extractor api */ +extern mp_obj_t mp_ncnn_extractor_create(mp_obj_t net_obj); +extern mp_obj_t mp_ncnn_extractor_destroy(mp_obj_t ex_obj); +extern mp_obj_t mp_ncnn_extractor_set_option(mp_obj_t ex_obj, mp_obj_t opt_obj); +extern mp_obj_t mp_ncnn_extractor_input(mp_obj_t ex_obj, mp_obj_t name_obj, mp_obj_t mat_obj); +extern mp_obj_t mp_ncnn_extractor_extract(mp_obj_t ex_obj, mp_obj_t name_obj, mp_obj_t mat_obj); +extern mp_obj_t mp_ncnn_extractor_input_index(mp_obj_t ex_obj, mp_obj_t index_obj, mp_obj_t mat_obj); +extern mp_obj_t mp_ncnn_extractor_extract_index(mp_obj_t ex_obj, mp_obj_t name_obj, mp_obj_t mat_obj); + +/* mat process api */ +extern mp_obj_t mp_ncnn_copy_make_border(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_copy_make_border_3d(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_copy_cut_border(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_copy_cut_border_3d(size_t n_args, const mp_obj_t* args); + +/* drawing api */ +extern mp_obj_t mp_ncnn_draw_rectangle_c1(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_rectangle_c2(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_rectangle_c3(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_rectangle_c4(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_text_c1(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_text_c2(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_text_c3(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_text_c4(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_circle_c1(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_circle_c2(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_circle_c3(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_circle_c4(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_line_c1(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_line_c2(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_line_c3(size_t n_args, const mp_obj_t* args); +extern mp_obj_t mp_ncnn_draw_line_c4(size_t n_args, const mp_obj_t* args); + +#endif \ No newline at end of file diff --git a/micropython/micropython.cmake b/micropython/micropython.cmake new file mode 100644 index 00000000000..917caeacbfb --- /dev/null +++ b/micropython/micropython.cmake @@ -0,0 +1 @@ +include(${CMAKE_CURRENT_LIST_DIR}/c_api/micropython.cmake) \ No newline at end of file