Skip to content

Commit cfc2316

Browse files
committed
Optimize MicroPython module structure
- Reorganize source files into src/ and include/ directories - Update build configuration
1 parent 3b94196 commit cfc2316

20 files changed

+1538
-1441
lines changed
File renamed without changes.

micropython/c_api/c_api.cpp

Lines changed: 0 additions & 1426 deletions
This file was deleted.
File renamed without changes.
Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
add_library(usermod_ncnn INTERFACE)
2+
23
find_package(OpenMP REQUIRED)
34

5+
set(NCNN_INSTALL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../build_micropython/install)
6+
47
find_library(NCNN_LIBRARY
58
NAMES ncnn
69
PATHS
7-
${CMAKE_CURRENT_SOURCE_DIR}/../../build_micropython/install/lib64
8-
${CMAKE_CURRENT_SOURCE_DIR}/../../build_micropython/install/lib
10+
${NCNN_INSTALL_DIR}/lib64
11+
${NCNN_INSTALL_DIR}/lib
912
NO_DEFAULT_PATH
1013
)
1114

15+
if(NOT NCNN_LIBRARY)
16+
message(FATAL_ERROR "NCNN library not found in ${NCNN_INSTALL_DIR}")
17+
endif()
18+
19+
if(NOT EXISTS ${NCNN_INSTALL_DIR}/include)
20+
message(FATAL_ERROR "NCNN headers not found at ${NCNN_INSTALL_DIR}/include")
21+
endif()
22+
1223
target_compile_definitions(usermod_ncnn INTERFACE
1324
NCNN_STRING=1
1425
NCNN_STDIO=1
@@ -17,22 +28,40 @@ target_compile_definitions(usermod_ncnn INTERFACE
1728
)
1829

1930
target_sources(usermod_ncnn INTERFACE
20-
${CMAKE_CURRENT_LIST_DIR}/c_api.cpp
21-
${CMAKE_CURRENT_LIST_DIR}/ncnn_module.c
31+
${CMAKE_CURRENT_LIST_DIR}/src/core/ncnn_module.c
32+
${CMAKE_CURRENT_LIST_DIR}/src/api/version.cpp
33+
${CMAKE_CURRENT_LIST_DIR}/src/api/allocator.cpp
34+
${CMAKE_CURRENT_LIST_DIR}/src/api/option.cpp
35+
${CMAKE_CURRENT_LIST_DIR}/src/api/mat.cpp
36+
${CMAKE_CURRENT_LIST_DIR}/src/api/version.cpp
37+
${CMAKE_CURRENT_LIST_DIR}/src/api/allocator.cpp
38+
${CMAKE_CURRENT_LIST_DIR}/src/api/option.cpp
39+
${CMAKE_CURRENT_LIST_DIR}/src/api/mat.cpp
40+
${CMAKE_CURRENT_LIST_DIR}/src/api/mat_pixel.cpp
41+
${CMAKE_CURRENT_LIST_DIR}/src/api/mat_pixel_drawing.cpp
42+
${CMAKE_CURRENT_LIST_DIR}/src/api/mat_process.cpp
43+
${CMAKE_CURRENT_LIST_DIR}/src/api/extractor.cpp
44+
${CMAKE_CURRENT_LIST_DIR}/src/api/layer.cpp
45+
${CMAKE_CURRENT_LIST_DIR}/src/api/net.cpp
46+
${CMAKE_CURRENT_LIST_DIR}/src/api/datareader.cpp
47+
${CMAKE_CURRENT_LIST_DIR}/src/api/paramdict.cpp
48+
${CMAKE_CURRENT_LIST_DIR}/src/api/modelbin.cpp
49+
${CMAKE_CURRENT_LIST_DIR}/src/api/blob.cpp
2250
)
2351

2452
target_include_directories(usermod_ncnn INTERFACE
25-
${CMAKE_CURRENT_LIST_DIR}
26-
${CMAKE_CURRENT_LIST_DIR}/../../build_micropython/install/include
53+
${CMAKE_CURRENT_LIST_DIR}/include
54+
${NCNN_INSTALL_DIR}/include
2755
)
2856

2957
target_link_directories(usermod_ncnn INTERFACE
30-
${CMAKE_CURRENT_LIST_DIR}/../../build_micropython/install/lib
31-
OpenMP::OpenMP_CXX
58+
${NCNN_INSTALL_DIR}/lib64
59+
${NCNN_INSTALL_DIR}/lib
3260
)
3361

3462
target_link_libraries(usermod_ncnn INTERFACE
35-
ncnn
63+
${NCNN_LIBRARY}
64+
OpenMP::OpenMP_CXX
3665
)
3766

3867
target_link_libraries(usermod INTERFACE usermod_ncnn)

micropython/c_api/micropython.mk

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
NCNN_MOD_DIR := $(USERMOD_DIR)
22

3-
NCNN_CFLAGS = -DNCNN_STRING=1 -DNCNN_STDIO=1 -DNCNN_PIXEL=1 -DNCNN_PIXEL_DRAWING=1
3+
NCNN_INSTALL_DIR := $(NCNN_MOD_DIR)/../../build_micropython/install
4+
5+
NCNN_LIB_PATH := $(shell find $(NCNN_INSTALL_DIR) -name "libncnn.*" -type f | head -1 | xargs dirname)
6+
NCNN_INCLUDE_PATH := $(NCNN_INSTALL_DIR)/include
47

5-
SRC_USERMOD += $(NCNN_MOD_DIR)/ncnn_module.c
6-
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/c_api.cpp
8+
NCNN_CFLAGS = -DNCNN_STRING=1 -DNCNN_STDIO=1 -DNCNN_PIXEL=1 -DNCNN_PIXEL_DRAWING=1
79

8-
CFLAGS_USERMOD += -I$(NCNN_MOD_DIR) -I$(NCNN_MOD_DIR)/../../build_micropython/install/include -fopenmp $(NCNN_CFLAGS)
10+
SRC_USERMOD += $(NCNN_MOD_DIR)/src/core/ncnn_module.c
11+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/version.cpp
12+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/allocator.cpp
13+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/option.cpp
14+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/mat.cpp
15+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/mat_pixel.cpp
16+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/mat_pixel_drawing.cpp
17+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/mat_process.cpp
18+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/extractor.cpp
19+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/layer.cpp
20+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/net.cpp
21+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/datareader.cpp
22+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/paramdict.cpp
23+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/modelbin.cpp
24+
SRC_USERMOD_CXX += $(NCNN_MOD_DIR)/src/api/blob.cpp
925

10-
NCNN_LIB_PATH := $(shell find $(NCNN_MOD_DIR)/../../build_micropython/install -name "libncnn.*" -type f | head -1 | xargs dirname)
26+
CFLAGS_USERMOD += -I$(NCNN_MOD_DIR)/include -I$(NCNN_INCLUDE_PATH) -fopenmp $(NCNN_CFLAGS)
1127

1228
LDFLAGS_USERMOD += -L$(NCNN_LIB_PATH) -lncnn -fopenmp -lstdc++
1329

14-
CXXFLAGS_USERMOD += -I$(NCNN_MOD_DIR) -I$(NCNN_MOD_DIR)/../../build_micropython/install/include -fopenmp $(NCNN_CFLAGS)
30+
CXXFLAGS_USERMOD += -I$(NCNN_MOD_DIR)/include -I$(NCNN_INCLUDE_PATH) -fopenmp $(NCNN_CFLAGS)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "ncnn/c_api.h"
2+
3+
extern "C" {
4+
#include "py/runtime.h"
5+
6+
mp_obj_t mp_ncnn_allocator_create_pool_allocator(void)
7+
{
8+
ncnn_allocator_t allocator = ncnn_allocator_create_pool_allocator();
9+
return mp_obj_new_int_from_uint((uintptr_t)allocator);
10+
}
11+
mp_obj_t mp_ncnn_allocator_create_unlocked_pool_allocator(void)
12+
{
13+
ncnn_allocator_t allocator = ncnn_allocator_create_unlocked_pool_allocator();
14+
return mp_obj_new_int_from_uint((uintptr_t)allocator);
15+
}
16+
mp_obj_t mp_ncnn_allocator_destroy(mp_obj_t ncnn_allocator_obj)
17+
{
18+
ncnn_allocator_t allocator = (ncnn_allocator_t)mp_obj_get_int(ncnn_allocator_obj);
19+
ncnn_allocator_destroy(allocator);
20+
return mp_const_none;
21+
}
22+
}

micropython/c_api/src/api/blob.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include "ncnn/c_api.h"
2+
3+
extern "C" {
4+
#include "py/runtime.h"
5+
6+
#if NCNN_STRING
7+
mp_obj_t mp_ncnn_blob_get_name(mp_obj_t blob_obj)
8+
{
9+
const ncnn_blob_t blob = (ncnn_blob_t)mp_obj_get_int(blob_obj);
10+
return mp_obj_new_str(ncnn_blob_get_name(blob), strlen(ncnn_blob_get_name(blob)));
11+
}
12+
#endif
13+
14+
mp_obj_t mp_ncnn_blob_get_producer(mp_obj_t blob_obj)
15+
{
16+
const ncnn_blob_t blob = (ncnn_blob_t)mp_obj_get_int(blob_obj);
17+
return mp_obj_new_int(ncnn_blob_get_producer(blob));
18+
}
19+
mp_obj_t mp_ncnn_blob_get_consumer(mp_obj_t blob_obj)
20+
{
21+
const ncnn_blob_t blob = (ncnn_blob_t)mp_obj_get_int(blob_obj);
22+
return mp_obj_new_int(ncnn_blob_get_consumer(blob));
23+
}
24+
mp_obj_t mp_ncnn_blob_get_shape(size_t n_args, const mp_obj_t* args)
25+
{
26+
const ncnn_blob_t blob = (ncnn_blob_t)mp_obj_get_int(args[0]);
27+
int dims = mp_obj_get_int(args[1]);
28+
int w = mp_obj_get_int(args[2]);
29+
int h = mp_obj_get_int(args[3]);
30+
int c = mp_obj_get_int(args[4]);
31+
ncnn_blob_get_shape(blob, &dims, &w, &h, &c);
32+
return mp_const_none;
33+
}
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "ncnn/c_api.h"
2+
3+
extern "C" {
4+
#include "py/runtime.h"
5+
6+
mp_obj_t mp_ncnn_datareader_create(void)
7+
{
8+
ncnn_datareader_t dr = ncnn_datareader_create();
9+
return mp_obj_new_int_from_uint((uintptr_t)dr);
10+
}
11+
#if NCNN_STDIO
12+
mp_obj_t mp_ncnn_datareader_create_from_stdio(mp_obj_t fp_obj)
13+
{
14+
ncnn_datareader_t dr = ncnn_datareader_create_from_stdio((FILE*)mp_obj_get_int(fp_obj));
15+
return mp_obj_new_int_from_uint((uintptr_t)dr);
16+
}
17+
#endif /* NCNN_STDIO */
18+
mp_obj_t mp_ncnn_datareader_create_from_memory(mp_obj_t mem_obj)
19+
{
20+
mp_buffer_info_t bufinfo;
21+
mp_get_buffer_raise(mem_obj, &bufinfo, MP_BUFFER_READ);
22+
if (bufinfo.len == 0 || bufinfo.buf == NULL)
23+
{
24+
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("Memory buffer is empty or NULL"));
25+
}
26+
const unsigned char* mem_ptr = (const unsigned char*)bufinfo.buf;
27+
const unsigned char** mem = &mem_ptr;
28+
ncnn_datareader_t dr = ncnn_datareader_create_from_memory(mem);
29+
return mp_obj_new_int_from_uint((uintptr_t)dr);
30+
}
31+
mp_obj_t mp_ncnn_datareader_destroy(mp_obj_t dr_obj)
32+
{
33+
ncnn_datareader_destroy((ncnn_datareader_t)mp_obj_get_int(dr_obj));
34+
return mp_const_none;
35+
}
36+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include "ncnn/c_api.h"
2+
3+
extern "C" {
4+
#include "py/runtime.h"
5+
6+
mp_obj_t mp_ncnn_extractor_create(mp_obj_t net_obj)
7+
{
8+
ncnn_net_t net = (ncnn_net_t)mp_obj_get_int(net_obj);
9+
return mp_obj_new_int_from_uint((uintptr_t)ncnn_extractor_create(net));
10+
}
11+
mp_obj_t mp_ncnn_extractor_destroy(mp_obj_t ex_obj)
12+
{
13+
ncnn_extractor_t ex = (ncnn_extractor_t)mp_obj_get_int(ex_obj);
14+
ncnn_extractor_destroy(ex);
15+
return mp_const_none;
16+
}
17+
mp_obj_t mp_ncnn_extractor_set_option(mp_obj_t ex_obj, mp_obj_t opt_obj)
18+
{
19+
ncnn_extractor_t ex = (ncnn_extractor_t)mp_obj_get_int(ex_obj);
20+
const ncnn_option_t opt = (ncnn_option_t)mp_obj_get_int(opt_obj);
21+
ncnn_extractor_set_option(ex, opt);
22+
return mp_const_none;
23+
}
24+
#if NCNN_STRING
25+
mp_obj_t mp_ncnn_extractor_input(mp_obj_t ex_obj, mp_obj_t name_obj, mp_obj_t mat_obj)
26+
{
27+
ncnn_extractor_t ex = (ncnn_extractor_t)mp_obj_get_int(ex_obj);
28+
const char* name = mp_obj_str_get_str(name_obj);
29+
const ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj);
30+
return mp_obj_new_int(ncnn_extractor_input(ex, name, mat));
31+
}
32+
mp_obj_t mp_ncnn_extractor_extract(mp_obj_t ex_obj, mp_obj_t name_obj, mp_obj_t mat_obj)
33+
{
34+
ncnn_extractor_t ex = (ncnn_extractor_t)mp_obj_get_int(ex_obj);
35+
const char* name = mp_obj_str_get_str(name_obj);
36+
ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj);
37+
return mp_obj_new_int(ncnn_extractor_extract(ex, name, &mat));
38+
}
39+
#endif /* NCNN_STRING */
40+
mp_obj_t mp_ncnn_extractor_input_index(mp_obj_t ex_obj, mp_obj_t index_obj, mp_obj_t mat_obj)
41+
{
42+
ncnn_extractor_t ex = (ncnn_extractor_t)mp_obj_get_int(ex_obj);
43+
int index = mp_obj_get_int(index_obj);
44+
const ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj);
45+
return mp_obj_new_int(ncnn_extractor_input_index(ex, index, mat));
46+
}
47+
mp_obj_t mp_ncnn_extractor_extract_index(mp_obj_t ex_obj, mp_obj_t index_obj, mp_obj_t mat_obj)
48+
{
49+
ncnn_extractor_t ex = (ncnn_extractor_t)mp_obj_get_int(ex_obj);
50+
int index = mp_obj_get_int(index_obj);
51+
ncnn_mat_t mat = (ncnn_mat_t)mp_obj_get_int(mat_obj);
52+
return mp_obj_new_int(ncnn_extractor_extract_index(ex, index, &mat));
53+
}
54+
}

0 commit comments

Comments
 (0)