[microTVM] Problems with arduino tutorial and generated code

Hello everyone!

I have been trying to get microTVM running on Arduino using the tutorial 5. Training Vision Models for microTVM on Arduino. Unfortunately I’ve run into a few issues, not all of which I was able to resolve.

I am using TVM v0.13.0. And have also gotten the same behavior with v0.14.0. I have not been able to get an older version (v0.10.0) running.

Error in the Tutorial

When working through the tutorial I identified some inconsistencies.

  1. In the tutorial the Arduino Nano 33 BLE is used. However in the python-code generating the project the target is set to (“zephyr”, “nrf5340dk_nrf5340_cpuapp”). This, in my experience, leads the Arduino IDE to fail compiling for the Nano 33 BLE. I think this should be set to (“arduino”, “nano33ble”) for the used board, which works for me. Please correct me, if there’s a reason for the different boards. I have created a pull request to update that.
  2. TVM Version Differences: There has been an update to the TVM-Methods. In the tutorial the arduino code uses TVMInitialize() which seems it has been renamed to TVMPlatformInitialize() from v0.11.0 to v0.12.0. Also the file src/model.c now seems to be src/platform.c (I assume that with v0.10.0 the tutorial might have worked properly, however I was not able to verify it). This means depending on the TVM version used, those mentions need to be updated for the code to work.

Issue of Faulty Code generation

Running the code with those manual fixes for error 2.) compiles just fine, but does not output the correct results. The output of the program running on the arduino is always just 0, 0 and I noticed the arduino probably running out of memory. It is not possible to upload new code without forcing the bootloader to by double pressing the reset button.

I’ve noticed some problems with the generated arduino code. Here is what I have found:

The arduino file project.ino calls the method TVMExecute(). This is defined in the file src/platform.c.

TVMExecute() calls tvmgen_default___tvm_main__(). This is defined in the file src/model/default_lib2.c. This is where the neural network functionality is implemented.

There is a method tvmgen_default_run(). This is defined in the file src/model/default_lib0.c. In this method “workspaces” are defined and populated with values. However since this method never is called, those values are not used.

I have tried fixing this generated code to use tvmgen_default_run(), which in turn calls tvmgen_default___tvm_main__(). With this I get useful results and the arduino does not run out of memory anymore, so uploads are always possible.

I am not sure if this fix is the intended use anyway. But it is working for me on short term. However I would appreciate not having to manually fix the generated code but the codegeneration be fixed. That’s why I have the following two main questions:

Q1.) What is the intended way to call the generated microTVM functions? It looks like the arduino adaptation of the microTVM workflow was not adapted accordingly. The workspace memory needs to be defined at a certain step in this process, but the initialization is not called in this case. In my case it was only possible by manually redirecting the functions. What is the intended way of microTVM to create this memory needed?

Q2.) I would appreciate any insight in the working of microTVMs Codegen, so that one does not have to manually fix the generated code but the codegeneration is fixed. There got to be a place where those faulty files are created? Is there a template where the structure is defined?

Any help with this is greatly appreciated. @mehrdadh @guberti

@areusch maybe you can help as well?