mocleiri / tensorflow-micropython-examples

A custom micropython firmware integrating tensorflow lite for microcontrollers and ulab to implement the tensorflow micro examples.
MIT License
170 stars 79 forks source link

Using a memory view for tensor area? #118

Closed MATTYGILO closed 1 year ago

MATTYGILO commented 1 year ago

I am currently trying to modify the code so that it's no longer area size, but can provide a memory view for the area. Just wondering whether there was a reason for using an integer value instead of providing a memory view. Surely using a mv would allow for greater control of memory usage. Here is my implementation so for:

STATIC mp_obj_t interpreter_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
    mp_arg_check_num(n_args, n_kw, 4, 4, false);

    // args:
    //  - model
    //  - size of the tensor area
    //  - input callback function
    //  - output callback function

    mp_obj_array_t *model = MP_OBJ_TO_PTR (args[0]);
    mp_obj_array_t *tensor_area_data = MP_OBJ_TO_PTR (args[1]); // <- New

    mp_obj_t input_callback_fn = args[2];
    mp_obj_t output_callback_fn = args[3];

    if (input_callback_fn != mp_const_none && !mp_obj_is_callable(input_callback_fn)) {
        mp_raise_ValueError(MP_ERROR_TEXT("Invalid Input Callback Handler"));
    }

    if (output_callback_fn != mp_const_none && !mp_obj_is_callable(output_callback_fn)) {
        mp_raise_ValueError(MP_ERROR_TEXT("Invalid Output Callback Handler"));
    }

    microlite_interpreter_obj_t *self = m_new_obj(microlite_interpreter_obj_t);

    self->input_callback = input_callback_fn;
    self->output_callback = output_callback_fn;

    self->inference_count = 0;

    self->base.type = &microlite_interpreter_type;

    // Set the model data
    self->model_data = model;
    self->tensor_area = tensor_area_data; // <- New

    mp_printf(MP_PYTHON_PRINTER, "interpreter_make_new: model size = %d, tensor area = %d\n", self->model_data->len, self->tensor_area->len);

    libtf_interpreter_init(self);

    return MP_OBJ_FROM_PTR(self);
}
MATTYGILO commented 1 year ago

However, when I go to run it I get this error:

decoded_backtrace:  0x00000bac: ?? ??:0
0x42070ab5: mp_call_function_n_kw at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/runtime.c:664
 (inlined by) mp_call_method_n_kw at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/runtime.c:680
0x40378885: mp_execute_bytecode at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/vm.c:1008
0x42068968: fun_bc_call at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/objfun.c:289
0x42070ab5: mp_call_function_n_kw at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/runtime.c:664
 (inlined by) mp_call_method_n_kw at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/runtime.c:680
0x40378885: mp_execute_bytecode at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/vm.c:1008
0x42068968: fun_bc_call at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/objfun.c:289
0x42070ab5: mp_call_function_n_kw at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/runtime.c:664
 (inlined by) mp_call_method_n_kw at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/runtime.c:680
0x40378885: mp_execute_bytecode at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/vm.c:1008
0x42068968: fun_bc_call at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/objfun.c:289
0x420709f5: mp_call_function_n_kw at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/runtime.c:664
0x403787f9: mp_execute_bytecode at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/vm.c:923
0x42068968: fun_bc_call at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/objfun.c:289
0x420709f5: mp_call_function_n_kw at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/runtime.c:664
0x403787f9: mp_execute_bytecode at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/vm.c:923
0x42068968: fun_bc_call at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/objfun.c:289
0x42070a1d: mp_call_function_n_kw at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/runtime.c:664
 (inlined by) mp_call_function_0 at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/py/runtime.c:638
0x420b1ce4: parse_compile_execute at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/shared/runtime/pyexec.c:116
0x420b1f34: pyexec_friendly_repl at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/shared/runtime/pyexec.c:662
0x4208c60c: mp_task at /home/runner/work/tensorflow-micropython-examples/tensorflow-micropython-examples/micropython/ports/esp32/main.c:170
MATTYGILO commented 1 year ago

Ignore me was still passing in an integer instead of the memory view, this code works for anyone who is interested