Skip to content

Commit 55878ae

Browse files
committed
cnn: Fixup int8 when built as external C module
The TinyMaix symbols were defined as weak - so int8 config would call a function that had fp32 configuration. Would fail in tm_load
1 parent 99a1e95 commit 55878ae

8 files changed

Lines changed: 42 additions & 10 deletions

File tree

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
/*
22
* emlearn_cnn_fp32 wrapper
3-
* This file sets CONFIG_FP32 and includes the main mod_cnn.c
3+
* This file includes fp32/tm_port.h directly before including mod_cnn.c
4+
* We need to modify mod_cnn.c to not include tm_port.h when CONFIG is already defined
45
*/
6+
7+
/* Define CONFIG_FP32 first */
58
#define CONFIG_FP32
9+
10+
/* Include the fp32 tm_port.h directly */
11+
#include "../tinymaix_cnn/fp32/tm_port.h"
12+
13+
/* Now include mod_cnn.c - it will see CONFIG_FP32 is defined and use fp32/tm_port.h */
614
#include "../tinymaix_cnn/mod_cnn.c"

src/emlearn_cnn_fp32/micropython.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ CNN_SRC := $(USERMOD_DIR)/../tinymaix_cnn
66
# Add wrapper C file which defines CONFIG_FP32
77
SRC_USERMOD_C += $(USERMOD_DIR)/emlearn_cnn_fp32.c
88

9-
# Include paths - need to include tm_port.h location
9+
# Include paths - config directory first to ensure correct tm_port.h is found
1010
CFLAGS_USERMOD += -I$(CNN_SRC)/fp32
11+
CFLAGS_USERMOD += -I$(CNN_SRC)/int8
1112
CFLAGS_USERMOD += -I$(CNN_SRC)/../../dependencies/TinyMaix/include
1213
CFLAGS_USERMOD += -I$(CNN_SRC)/../../dependencies/TinyMaix/src
1314

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
/*
22
* emlearn_cnn_int8 wrapper
3-
* This file sets CONFIG_INT8 and includes the main mod_cnn.c
3+
* This file includes int8/tm_port.h directly before including mod_cnn.c
4+
* We need to modify mod_cnn.c to not include tm_port.h when CONFIG is already defined
45
*/
6+
7+
/* Define CONFIG_INT8 first */
58
#define CONFIG_INT8
9+
10+
/* Include the int8 tm_port.h directly */
11+
#include "../tinymaix_cnn/int8/tm_port.h"
12+
13+
/* Now include mod_cnn.c - it will see CONFIG_INT8 is defined and use int8/tm_port.h */
614
#include "../tinymaix_cnn/mod_cnn.c"

src/emlearn_cnn_int8/micropython.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ CNN_SRC := $(USERMOD_DIR)/../tinymaix_cnn
66
# Add wrapper C file which defines CONFIG_INT8
77
SRC_USERMOD_C += $(USERMOD_DIR)/emlearn_cnn_int8.c
88

9-
# Include paths - need to include tm_port.h location
9+
# Include paths - config directory first to ensure correct tm_port.h is found
1010
CFLAGS_USERMOD += -I$(CNN_SRC)/int8
11+
CFLAGS_USERMOD += -I$(CNN_SRC)/fp32
1112
CFLAGS_USERMOD += -I$(CNN_SRC)/../../dependencies/TinyMaix/include
1213
CFLAGS_USERMOD += -I$(CNN_SRC)/../../dependencies/TinyMaix/src
1314

src/tinymaix_cnn/fp32/tm_port.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ limitations under the License.
4141
#define TM_MAX_KCSIZE (3*3*256) //max kernel_size*channels //cost TM_MAX_KSIZE*sizeof(mtype_t) Byte
4242

4343
#define TM_INLINE __attribute__((always_inline)) static inline
44-
#define TM_WEAK __attribute__((weak))
44+
#define TM_WEAK static
4545

4646
// Disable "static" (non-const) globals, since they are not supported by MicroPython mpy_ld.py
4747
// But when building multiple variants, we need static to avoid duplicate definitions

src/tinymaix_cnn/int8/tm_port.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ limitations under the License.
4141
#define TM_MAX_KCSIZE (3*3*256) //max kernel_size*channels //cost TM_MAX_KSIZE*sizeof(mtype_t) Byte
4242

4343
#define TM_INLINE __attribute__((always_inline)) static inline
44-
#define TM_WEAK __attribute__((weak))
44+
#define TM_WEAK static
4545

4646
// Disable "static" (non-const) globals, since they are not supported by MicroPython mpy_ld.py
4747
// But when building multiple variants, we need static to avoid duplicate definitions

src/tinymaix_cnn/mod_cnn.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
#include "py/runtime.h"
66
#endif
77

8+
// Check that only one CONFIG is defined
9+
#if defined(CONFIG_FP32) && defined(CONFIG_INT8)
10+
#error "Only one of CONFIG_FP32 or CONFIG_INT8 should be defined"
11+
#endif
12+
813
// Define unique symbol names based on CONFIG
914
#ifdef CONFIG_FP32
1015
#define CNN_TYPE mod_cnn_fp32_type
@@ -23,8 +28,17 @@
2328
// Forward declaration for tm_port.h
2429
void CNN_FREE(void *ptr);
2530

26-
// TinyMaix config
27-
#include "./tm_port.h"
31+
// TinyMaix config - include from the config directory
32+
// Only include if not already included by wrapper
33+
#ifndef __TM_PORT_H
34+
#ifdef CONFIG_INT8
35+
#include "./int8/tm_port.h"
36+
#elif defined(CONFIG_FP32)
37+
#include "./fp32/tm_port.h"
38+
#else
39+
#include "./int8/tm_port.h" // default
40+
#endif
41+
#endif
2842

2943
#include <tinymaix.h>
3044

@@ -52,7 +66,7 @@ void *memset(void *s, int c, size_t n) {
5266

5367
// get model output shapes
5468
//mdl: model handle; in: input mat; out: output mat
55-
int TM_WEAK tm_get_outputs(tm_mdl_t* mdl, tm_mat_t* out, int out_length)
69+
static int tm_get_outputs(tm_mdl_t* mdl, tm_mat_t* out, int out_length)
5670
{
5771
// NOTE: based on tm_run, but without actually executing
5872
int out_idx = 0;

0 commit comments

Comments
 (0)