Skip to content

Commit

Permalink
Convert mllib.c into mlrunner.cpp, add namespace as prep for pxt exte…
Browse files Browse the repository at this point in the history
…nsion.
  • Loading branch information
microbit-carlos committed May 17, 2024
1 parent a9c3d39 commit 3d9bfaf
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 20 deletions.
File renamed without changes.
14 changes: 7 additions & 7 deletions ml-module/micropython.mk
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
CPPEXAMPLE_MOD_DIR := $(USERMOD_DIR)

SRC_USERMOD += \
$(CPPEXAMPLE_MOD_DIR)/model-example/model_example.c \
$(CPPEXAMPLE_MOD_DIR)/mllib/ml4f/ml4f.c \
$(CPPEXAMPLE_MOD_DIR)/mllib/mllib.c \
$(CPPEXAMPLE_MOD_DIR)/ml-runner/model-example/model_example.c \
$(CPPEXAMPLE_MOD_DIR)/ml-runner/ml4f/ml4f.c \
$(CPPEXAMPLE_MOD_DIR)/src/mlmodule.c

CFLAGS_USERMOD += \
-I$(CPPEXAMPLE_MOD_DIR)/mllib \
-I$(CPPEXAMPLE_MOD_DIR)/model-example \
-I$(CPPEXAMPLE_MOD_DIR)/src
SRC_USERMOD_CXX += $(CPPEXAMPLE_MOD_DIR)/ml-runner/mlrunner.cpp

CFLAGS_USERMOD += -I$(CPPEXAMPLE_MOD_DIR)/ml-runner

CXXFLAGS_USERMOD += -I$(CPPEXAMPLE_MOD_DIR)/ml-runner -std=c++11
File renamed without changes.
File renamed without changes.
20 changes: 11 additions & 9 deletions ml-module/mllib/mllib.c → ml-module/ml-runner/mlrunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
* We call the "full model" the labels header + the ML4F model.
*/
#include <stdlib.h>
#include <model_example.h>
#include "model-example/model_example.h"
#include "ml4f/ml4f.h"
#include "mllib.h"
#include "mlrunner.h"

using namespace mlrunner;

// Linker symbols used to find the start of the model in flash.
extern uint8_t __etext, __data_start__, __data_end__;
Expand Down Expand Up @@ -90,24 +92,24 @@ static ml4f_header_t* get_ml4f_model() {
/*****************************************************************************/
/* Public API */
/*****************************************************************************/
void ml_useBuiltInModel(bool use) {
void mlrunner::ml_useBuiltInModel(bool use) {
USE_BUILT_IN = use;
}

bool ml_isModelPresent(void) {
bool mlrunner::ml_isModelPresent() {
ml_model_header_t *model_header = get_model_header();
return model_header != NULL;
}

size_t ml_getInputLen(void) {
size_t mlrunner::ml_getInputLen() {
ml4f_header_t *ml4f_model = get_ml4f_model();
if (ml4f_model == NULL) {
return 0;
}
return ml4f_shape_elements(ml4f_input_shape(ml4f_model));
}

ml_labels_t* ml_getLabels(void) {
ml_labels_t* mlrunner::ml_getLabels() {
static ml_labels_t labels = {
.num_labels = 0,
.labels = NULL
Expand Down Expand Up @@ -156,7 +158,7 @@ ml_labels_t* ml_getLabels(void) {
} else if (labels.num_labels != model_header->number_of_labels) {
set_labels = true;
} else {
for (int i = 0; i < labels.num_labels; i++) {
for (size_t i = 0; i < labels.num_labels; i++) {
if (labels.labels[i] != flash_labels[i]) {
set_labels = true;
break;
Expand All @@ -175,15 +177,15 @@ ml_labels_t* ml_getLabels(void) {
return NULL;
}
labels.num_labels = model_header->number_of_labels;
for (int i = 0; i < labels.num_labels; i++) {
for (size_t i = 0; i < labels.num_labels; i++) {
labels.labels[i] = flash_labels[i];
}
}

return &labels;
}

ml_prediction_t* ml_predict(const float *input) {
ml_prediction_t* mlrunner::ml_predict(const float *input) {
static ml_prediction_t predictions = {
.max_index = 0,
.num_labels = 0,
Expand Down
12 changes: 11 additions & 1 deletion ml-module/mllib/mllib.h → ml-module/ml-runner/mlrunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@
#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
namespace mlrunner {
extern "C" {
#endif

// ASCII for "MODL"
#define MODEL_HEADER_MAGIC0 0x4D4F444C
const uint32_t MODEL_HEADER_MAGIC0 = 0x4D4F444C;

typedef struct ml_model_header_t {
uint32_t magic0;
Expand Down Expand Up @@ -68,3 +73,8 @@ ml_labels_t* ml_getLabels(void);
* @return A pointer to a ml_prediction_t object containing the predictions.
*/
ml_prediction_t* ml_predict(const float *input);

#ifdef __cplusplus
} // extern "C"
} // namespace mlrunner
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#define ml4f_full_model_size (ml4f_model_example_header_len + ml4f_model_example_size)

// This is a struct representation of the header included at the beginning of model_example
/* const ml_model_header_t ml4f_model_example_header = {
/*
#include <mlrunner.h>
const ml_model_header_t ml4f_model_example_header = {
.magic0 = MODEL_HEADER_MAGIC0,
.header_size = 0x31, // 49
.model_offset = 0x34, // 52
Expand All @@ -18,7 +20,8 @@
"Standing\0"
"Walking\0\0\0"
}
}; */
};
*/

const unsigned int model_example[ml4f_full_model_size] = {
// Manually converted ml4f_model_example_header
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion ml-module/src/mlmodule.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <py/runtime.h>
#include <mllib.h>
#include <mlrunner.h>

mp_obj_t internal_model_func(mp_obj_t use_internal) {
bool use_internal_model = !!mp_obj_get_int(use_internal);
Expand Down

0 comments on commit 3d9bfaf

Please sign in to comment.