Skip to content

Microprofiler can now output logs for benchmarks in the csv format. #1290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tensorflow/lite/micro/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ cc_library(
],
copts = micro_copts(),
deps = [
":micro_compatibility",
":micro_error_reporter",
":micro_time",
"//tensorflow/lite/kernels/internal:compatibility",
Expand Down
61 changes: 61 additions & 0 deletions tensorflow/lite/micro/micro_profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,65 @@ void MicroProfiler::LogCsv() const {
#endif
}

void MicroProfiler::LogTicksPerTagCsv() {
#if !defined(TF_LITE_STRIP_ERROR_STRINGS)
MicroPrintf(
"\"Unique Tag\",\"Total ticks across all events with that tag.\"");
int total_ticks = 0;
for (int i = 0; i < num_events_; ++i) {
uint32_t ticks = end_ticks_[i] - start_ticks_[i];
int position = FindExistingOrNextPosition(tags_[i]);
total_ticks_per_tag[position].tag = tags_[i];
total_ticks_per_tag[position].ticks =
total_ticks_per_tag[position].ticks + ticks;
total_ticks += ticks;
}

for (int i = 0; i < num_events_; ++i) {
TicksPerTag each_tag_entry = total_ticks_per_tag[i];
if (each_tag_entry.tag == nullptr) {
break;
}
MicroPrintf("%s,%d", each_tag_entry.tag, each_tag_entry.ticks);
}
MicroPrintf("total,%d", total_ticks);
#endif
}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

super nit: let's use // for the comment style for consistency.

https://google.github.io/styleguide/cppguide.html#Comment_Style

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the suggestion.

* This method finds a particular array element in the total_ticks_per_tag array
* with the matching tag_name passed in the method. If it can find a
* matching array element that has the same tag_name, then it will return the
* position of the matching element. But if it unable to find a matching element
* with the given tag_name, it will return the next available empty postion
* from the array.
*/
int MicroProfiler::FindExistingOrNextPosition(const char* tag_name) {
int pos = 0;
for (; pos < num_events_; pos++) {
TicksPerTag each_tag_entry = total_ticks_per_tag[pos];
if (each_tag_entry.tag == nullptr) {
return pos;
} else {
const char* currentTagName_t = each_tag_entry.tag;
const char* newTagName_t = tag_name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

current_tag_name
new_tag_name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

bool matched = true;
while ((*currentTagName_t != '\0') && (*newTagName_t != '\0')) {
if (((*currentTagName_t) == (*newTagName_t))) {
++currentTagName_t;
++newTagName_t;
} else {
matched = false;
break;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use strcmp instead of this custom loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not aware of if strcmp is available in tflm Thank you for the suggestion.

}

if (matched) {
return pos;
}
}
}
return pos;
}

} // namespace tflite
16 changes: 16 additions & 0 deletions tensorflow/lite/micro/micro_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class MicroProfiler {
// Separated Value) form.
void LogCsv() const;

// Prints total ticks for each unique tag in CSV format.
// Output will have one row for each unique tag along with the
// total ticks summed across all events with that particular tag.
void LogTicksPerTagCsv();

private:
// Maximum number of events that this class can keep track of. If we call
// AddEvent more than kMaxEvents number of times, then the oldest event's
Expand All @@ -72,6 +77,17 @@ class MicroProfiler {
uint32_t end_ticks_[kMaxEvents];
int num_events_ = 0;

struct TicksPerTag {
const char* tag;
uint32_t ticks;
};
// In practice, the number of tags will be much lower than the number of
// events. But it is theoretically possible that each event to be unique and
// hence we allow total_ticks_per_tag to have kMaxEvents entries.
TicksPerTag total_ticks_per_tag[kMaxEvents];

int FindExistingOrNextPosition(const char* tagName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add a comment about what this function does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thank you for the suggestion!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tagName -> tag_name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Thank you for the suggestion!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Missed the header file last time.


TF_LITE_REMOVE_VIRTUAL_DELETE;
};

Expand Down