modules/py_ml: Align model data allocated on GC.

Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
This commit is contained in:
iabdalkader 2024-12-05 15:39:32 +01:00
parent c8eb4754b2
commit b213417020
2 changed files with 16 additions and 1 deletions

View File

@ -49,6 +49,14 @@
#include "tflm_builtin_models.h"
#endif
#ifndef IMLIB_ML_MODEL_ALIGN
#ifndef __DCACHE_PRESENT
#define IMLIB_ML_MODEL_ALIGN (32 - 1)
#else
#define IMLIB_ML_MODEL_ALIGN (__SCB_DCACHE_LINE_SIZE - 1)
#endif
#endif
static size_t py_ml_tuple_sum(mp_obj_tuple_t *o) {
if (o->len < 1) {
mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("Unexpected tensor shape"));
@ -366,7 +374,13 @@ mp_obj_t py_ml_model_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
FIL fp;
file_open(&fp, path, false, FA_READ | FA_OPEN_EXISTING);
model->size = f_size(&fp);
model->data = model->fb_alloc ? fb_alloc(model->size, FB_ALLOC_PREFER_SIZE) : xalloc(model->size);
if (model->fb_alloc) {
model->data = fb_alloc(model->size, FB_ALLOC_PREFER_SPEED | FB_ALLOC_CACHE_ALIGN);
} else {
// Keeps a reference to the GC block.
model->_raw = xalloc(model->size + IMLIB_ML_MODEL_ALIGN);
model->data = (void *) (((uintptr_t) model->_raw + IMLIB_ML_MODEL_ALIGN) & ~IMLIB_ML_MODEL_ALIGN);
}
file_read(&fp, model->data, model->size);
file_close(&fp);
#else

View File

@ -36,6 +36,7 @@
typedef struct py_ml_model_obj {
mp_obj_base_t base;
unsigned int size;
unsigned char *_raw;
unsigned char *data;
size_t memory_size;
uint32_t memory_addr;