Skip to content

Commit ce16318

Browse files
committed
examples/natmod/deepcraft: Refactored dc enablement.
Signed-off-by: NikhitaR-IFX <nikhita.rajasekhar@infineon.com>
1 parent c4d4013 commit ce16318

3 files changed

Lines changed: 66 additions & 43 deletions

File tree

examples/natmod/deepcraft/Makefile

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,9 @@ ARCH = armv7emsp
1313
# Link with libm.a and libgcc.a from the toolchain
1414
LINK_RUNTIME = 1
1515

16-
CFLAGS += -Dsqrtf=__builtin_sqrtf -fno-math-errno -fno-trapping-math -fno-builtin-logf -ffreestanding
17-
CFLAGS += -Dlogf=logf
16+
override LIBGCC_PATH := gcc/lib/gcc/arm-none-eabi/11.3.1/thumb/v7e-m+fp/hard/libgcc.a
17+
override LIBM_PATH := gcc/arm-none-eabi/lib/thumb/v7e-m+fp/hard/libm.a
1818

19-
#CFLAGS += -Dlogf=__builtin_logf -ffreestanding -fno-math-errno -fno-trapping-math -ffast-math
20-
21-
# Include to get the rules for compiling and linking the module
2219
include $(MPY_DIR)/py/dynruntime.mk
2320

2421
# Custom clean target

examples/natmod/deepcraft/dc_mp_iface.c

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
#include "py/dynruntime.h"
22

3-
43
#if !defined(__linux__)
54
void *memcpy(void *dst, const void *src, size_t n) {
65
return mp_fun_table.memmove_(dst, src, n);
76
}
87
void *memset(void *s, int c, size_t n) {
98
return mp_fun_table.memset_(s, c, n);
109
}
11-
12-
__attribute__((weak))
13-
float logf(float x) {
14-
// Basic fallback or dummy version — avoids pulling in full libm
15-
// Replace with your approximation if needed
16-
return 0.0f; // Just to satisfy the linker
17-
}
18-
19-
/*float logf(float x) {
20-
// Optional: You can define a very basic approximation, or just return x
21-
// to make the code compile and defer real computation
22-
return x; // Placeholder – replace with approximation if needed
23-
}*/
24-
2510
#endif
2611

2712
int native_errno=0;
@@ -36,15 +21,10 @@ int *__errno (void)
3621

3722
#include "examples/natmod/deepcraft/mp_src.c"
3823

39-
40-
typedef struct _dc_obj_t {
41-
mp_obj_base_t base;
42-
} dc_obj_t;
43-
4424
// Forward declaration of type
4525
mp_obj_full_type_t dc_type;
4626

47-
mp_map_elem_t dc_locals_dict_table[3];
27+
mp_map_elem_t dc_locals_dict_table[5];
4828
static MP_DEFINE_CONST_DICT(dc_locals_dict, dc_locals_dict_table);
4929

5030
// Constructor
@@ -67,9 +47,10 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a
6747
dc_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_init), MP_OBJ_FROM_PTR(&init_obj) };
6848
dc_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_enqueue), MP_OBJ_FROM_PTR(&enqueue_obj) };
6949
dc_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_dequeue), MP_OBJ_FROM_PTR(&dequeue_obj) };
70-
50+
dc_locals_dict_table[3] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_get_model_input_dim), MP_OBJ_FROM_PTR(&get_model_input_dim_obj) };
51+
dc_locals_dict_table[4] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_get_model_output_dim), MP_OBJ_FROM_PTR(&get_model_output_dim_obj) };
7152

72-
MP_OBJ_TYPE_SET_SLOT(&dc_type, locals_dict, (void*)&dc_locals_dict, 2);
53+
MP_OBJ_TYPE_SET_SLOT(&dc_type, locals_dict, (void*)&dc_locals_dict, 5);
7354

7455
// Expose constructor as DEEPCRAFT
7556
mp_store_global(MP_QSTR_DEEPCRAFT, MP_OBJ_FROM_PTR(&dc_type));

examples/natmod/deepcraft/mp_src.c

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,68 @@
22
#include "model.h"
33
#include <string.h>
44

5-
mp_obj_t init(void){
6-
//assign();
5+
typedef struct _dc_obj_t {
6+
mp_obj_base_t base;
7+
bool model_state;
8+
uint8_t model_in_dim;
9+
uint8_t model_out_dim;
10+
} dc_obj_t;
11+
12+
mp_obj_t get_model_input_dim(mp_obj_t self_in){
13+
dc_obj_t *self = MP_OBJ_TO_PTR(self_in);
14+
return MP_OBJ_NEW_SMALL_INT(self->model_in_dim);
15+
}
16+
17+
mp_obj_t get_model_output_dim(mp_obj_t self_in){
18+
dc_obj_t *self = MP_OBJ_TO_PTR(self_in);
19+
return MP_OBJ_NEW_SMALL_INT(self->model_out_dim);
20+
}
21+
22+
mp_obj_t init(mp_obj_t self_in){
23+
dc_obj_t *self = MP_OBJ_TO_PTR(self_in);
24+
self->model_state = true;
25+
self->model_in_dim = IMAI_DATA_IN_COUNT;
26+
self->model_out_dim = IMAI_DATA_OUT_COUNT;
727
IMAI_init();
828
return mp_const_none;
929
}
1030

11-
mp_obj_t enqueue(const mp_obj_t data_in_obj){
12-
float data_in[IMAI_DATA_IN_COUNT];
31+
mp_obj_t enqueue(mp_obj_t self_in, const mp_obj_t data_in_obj){
32+
dc_obj_t *self = MP_OBJ_TO_PTR(self_in);
33+
34+
// Check if model is initialized
35+
if(!self->model_state){
36+
mp_raise_ValueError("Model should be initialized first.");
37+
}
38+
39+
float data_in[self->model_in_dim];
1340
mp_obj_t *data_in_items;
1441
size_t len;
1542
mp_obj_get_array(data_in_obj, &len, &data_in_items);
16-
if (len != IMAI_DATA_IN_COUNT) {
17-
mp_raise_ValueError("data_in must be a list of IMAI_DATA_IN_COUNT floats");
43+
44+
if (len != self->model_in_dim) {
45+
mp_raise_ValueError("data_in must be a list of floats with size matching to input dimensions to model. Check using get_model_input_dim().");
1846
}
19-
for (int i = 0; i < IMAI_DATA_IN_COUNT; i++) {
47+
48+
for (int i = 0; i < self->model_in_dim; i++) {
2049
data_in[i] = mp_obj_get_float(data_in_items[i]);
2150
}
2251
int result = IMAI_enqueue(data_in);
2352
return MP_OBJ_NEW_SMALL_INT(result);
24-
return mp_const_none;
2553
}
2654

27-
mp_obj_t dequeue(mp_obj_t data_out_obj) {
55+
mp_obj_t dequeue(mp_obj_t self_in, mp_obj_t data_out_obj) {
56+
dc_obj_t *self = MP_OBJ_TO_PTR(self_in);
57+
2858
mp_buffer_info_t buf_info;
2959
mp_get_buffer(data_out_obj, &buf_info, MP_BUFFER_WRITE);
3060
float *data_out = (float *)buf_info.buf;
61+
size_t len = buf_info.len / sizeof(float);
62+
63+
if (len != self->model_out_dim) {
64+
mp_raise_ValueError("data_out must be a list of floats with size matching to output dimensions of model. Check using get_model_output_dim().");
65+
}
66+
3167
int result = IMAI_dequeue(data_out);
3268
if (result == 0) {
3369
return MP_OBJ_NEW_SMALL_INT(result);
@@ -37,22 +73,31 @@ mp_obj_t dequeue(mp_obj_t data_out_obj) {
3773
mp_raise_ValueError(MP_ERROR_TEXT("Internal memory allocation error"));
3874
}
3975
return MP_OBJ_NEW_SMALL_INT(result);
40-
//return mp_const_none;
4176
}
4277

4378

4479
static const mp_obj_fun_builtin_fixed_t init_obj = {
45-
.base = { &mp_type_fun_builtin_0 },
46-
.fun._0 = (mp_fun_0_t)init,
80+
.base = { &mp_type_fun_builtin_1 },
81+
.fun._1 = (mp_fun_1_t)init,
4782
};
4883

4984

5085
static const mp_obj_fun_builtin_fixed_t enqueue_obj = {
51-
.base = { &mp_type_fun_builtin_1 },
52-
.fun._1 = (mp_fun_1_t)enqueue,
86+
.base = { &mp_type_fun_builtin_2 },
87+
.fun._2 = (mp_fun_2_t)enqueue,
5388
};
5489

5590
static const mp_obj_fun_builtin_fixed_t dequeue_obj = {
91+
.base = { &mp_type_fun_builtin_2 },
92+
.fun._2 = (mp_fun_2_t)dequeue,
93+
};
94+
95+
static const mp_obj_fun_builtin_fixed_t get_model_input_dim_obj = {
96+
.base = { &mp_type_fun_builtin_1 },
97+
.fun._1 = (mp_fun_1_t)get_model_input_dim,
98+
};
99+
100+
static const mp_obj_fun_builtin_fixed_t get_model_output_dim_obj = {
56101
.base = { &mp_type_fun_builtin_1 },
57-
.fun._1 = (mp_fun_1_t)dequeue,
102+
.fun._1 = (mp_fun_1_t)get_model_output_dim,
58103
};

0 commit comments

Comments
 (0)