emlearn / emlearn-micropython

Efficient Machine Learning engine for MicroPython
65 stars 13 forks source link

Modules exceptions or crashes after GC collect #12

Open jonnor opened 3 months ago

jonnor commented 3 months ago

On ESP32 and ESP32-S3 I am seeing various exceptions and crashes related to the native modules.

It can be exceptions like TypeError: 'slice' object isn't callable. Or array object is not callable. Or tuple is not callable. Or sometimes the entire interpreter crashes.

I am seeing this both with emltrees, emliir and emlfft. At first, I thought this was due to something the C code did wrong. But I reduced several C examples down to a minimum, and would still hit the issue. Even one example with a plain function (no allocated objects) - and that function itself seems to dissapear. And the behavior is influenced by unrelated things in MicroPython - doing other things that allocate/collect is needed to trigger the issue.

The same code also runs fine on x64 or STM32, or RP2040 - so it seems ESP32 port specific.

jonnor commented 3 months ago

I believe that this is due to this issue in upstream MicroPython: https://github.com/micropython/micropython/issues/6769

jonnor commented 1 week ago

Turns out the issue was not really port specific - could also happen on RP2040, NRF52 etc.

The problem has been fixed now in upstream MicroPython, scheduled for release in MicroPython 1.24. However, that fix needs re-built firmware images.

There was another fix available, which added additional code to the module. I have backported this now to the MicroPython branch we use. And I have been able to run the modules on ESP32 in some example programs. A bit more testing is needed however to fully conclude it has been fixed.

Probably I will keep this issue open until MicroPython 1.24 and we have rebased onto that for the proper fix.