How to profile the cycles and timing of a inference in tflite-micro

The CPU cycle count is not abstracted away by the Arduino API or the TFLite API. They only care about microsecond or millisecond delays / timing. So you will have to create some abstraction layer or case work yourself.

#ifdef __arm__ 
/* some ARM CPU, assume we can use the ARM DWT */
#define CPU_RESET_CYCLECOUNTER    do { ARM_DEMCR |= ARM_DEMCR_TRCENA;          \
                                       ARM_DWT_CTRL |= ARM_DWT_CTRL_CYCCNTENA; \
                                       ARM_DWT_CYCCNT = 0; } while(0)
#define CPU_GET_CYCLECOUNTER()  ARM_DWT_CYCCNT
#elif defined(ESP32) // or ARDUINO_ARCH_ESP32 or __xtensa__ I guess
/* some ESP32 stuff, use their SDK */
#define CPU_RESET_CYCLECOUNTER    do { esp_cpu_set_cycle_count(0); } while(0)
#define CPU_GET_CYCLECOUNTER()  ((uint32_t) esp_cpu_get_cycle_count())
#else
#error "tf is this architecture"
#endif

// use CPU_RESET_CYCLECOUNTER and CPU_GET_CYCLECOUNTER in code

Similiarly, the ESP32 SDK has esp_cpu_get_cycle_count, providing the same functionality.

1 Like