Example for using model_profiler.cc

209 views
Skip to first unread message

Aakash Tyagi

unread,
Apr 12, 2022, 10:24:43 AM4/12/22
to SIG Micro
Hi Team, I am trying to use the model_profiler api for measuring inference time per layer of my model. Due to lack of documentation I am unable to figure out how to use that.
If anyone can provide a working example for hello_world it would be really helpful.

Thanks

Joe Antognini

unread,
Apr 12, 2022, 6:36:19 PM4/12/22
to Aakash Tyagi, SIG Micro
Hi Aakash,

I was able to get the profiler working a little while back so I can share how I did it.  The way to set up the profiler will be microprocessor dependent.  You will need to have access to a function that gives you the system time.  Basically, the profiler relies on two functions which have been stubbed out in micro_time.cc.  One is ticks_per_second and the other is GetCurrentTimeTicks.  You may need to create a version of `micro_time.cc` that implements these functions for your hardware.  Versions of `micro_time.cc` have already been implemented for a variety of different kinds of microprocessors in the different subdirectories in `tflite-micro/tensorflow/lite/micro/`.  If your microprocessor is already there, you just need to link to the correct version of `micro_time.cc` when you compile TFLM.

If your microprocessor is not there, you will need to add the appropriate `microe_time.cc` file yourself.  What I did was to implement this through some callbacks.  So in a file called `tflite-micro/tensorflow/lite/micro/<my_microprocessor>/micro_time.cc` I have:

static CurrentTimeCallback current_time_callback = nullptr;
static TicksPerSecondCallback ticks_per_second_callback = nullptr;

void RegisterCurrentTimeCallback(CurrentTimeCallback cb) {
  current_time_callback = cb;
}

void RegisterTicksPerSecondCallback(TicksPerSecondCallback cb) {
  ticks_per_second_callback = cb;
}

int32_t ticks_per_second() {
    return (int32_t)ticks_per_second_callback();
}

int32_t GetCurrentTimeTicks() {
    return (int32_t)current_time_callback();
}

Once you've done that, in your main function you can then do something like this:

#include "tensorflow/lite/micro/<my_microprocessor>/micro_time_callback.h"

// Register the callbacks with the appropriate timer calls on your system
// (May not be needed if model_time.cc can already find the right functions.)
tflite::RegisterCurrentTimeCallback(<my_microprocessors_timer_function>);
tflite::RegisterTicksPerSecondCallback(<my_microprocessors_clock_frequency_function>);

// Create a profiler
tflite::MicroProfiler profiler;

// Include it when you create your interpreter.
tflite::MicroInterpreter interpreter = MicroInterpreter(model, ..., &profiler);

// Invoke your model as normal.
interpreter.Invoke();

// Now you can just do this and it will print out the per-layer times.
profiler.Log();

Hope that helps!

Best,
Joe Antognini

--
You received this message because you are subscribed to the Google Groups "SIG Micro" group.
To unsubscribe from this group and stop receiving emails from it, send an email to micro+un...@tensorflow.org.
To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/micro/0766298f-2044-4763-8797-98bbfcfd6e29n%40tensorflow.org.
Reply all
Reply to author
Forward
0 new messages