1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 #include "tensorflow/lite/profiling/profile_summarizer.h"
17
18 #include <sstream>
19
20 #include "tensorflow/lite/schema/schema_generated.h"
21
22 namespace tflite {
23 namespace profiling {
24 namespace {
25
26 struct OperatorDetails {
27 std::string name;
28 std::vector<std::string> inputs;
29 std::vector<std::string> outputs;
30 };
31
GetTensorName(const tflite::Interpreter & interpreter,int tensor_index)32 std::string GetTensorName(const tflite::Interpreter& interpreter,
33 int tensor_index) {
34 const auto tensor = interpreter.tensor(tensor_index);
35 if (tensor == nullptr || tensor->name == nullptr) {
36 return "Unknown";
37 }
38 return tensor->name;
39 }
GetTensorNames(const tflite::Interpreter & interpreter,const TfLiteIntArray * tensor_indices)40 std::vector<std::string> GetTensorNames(const tflite::Interpreter& interpreter,
41 const TfLiteIntArray* tensor_indices) {
42 std::vector<std::string> tensors;
43 tensors.reserve(tensor_indices->size);
44 for (int i = 0; i < tensor_indices->size; i++) {
45 tensors.push_back(GetTensorName(interpreter, tensor_indices->data[i]));
46 }
47 return tensors;
48 }
49
ToString(const std::vector<std::string> & str_vector)50 std::string ToString(const std::vector<std::string>& str_vector) {
51 std::stringstream stream;
52 stream << "[";
53 bool first = true;
54 for (const auto& s : str_vector) {
55 if (!first) {
56 stream << ", ";
57 } else {
58 first = false;
59 }
60 stream << s;
61 }
62 stream << "]";
63 return stream.str();
64 }
65
GetOperatorDetails(const tflite::Interpreter & interpreter,int node_index)66 OperatorDetails GetOperatorDetails(const tflite::Interpreter& interpreter,
67 int node_index) {
68 auto node_reg = interpreter.node_and_registration(node_index);
69 auto inputs = node_reg->first.inputs;
70 auto outputs = node_reg->first.outputs;
71 int code = node_reg->second.builtin_code;
72 const char* op_name = nullptr;
73 if (code == tflite::BuiltinOperator_CUSTOM) {
74 const char* custom_name = node_reg->second.custom_name;
75 op_name = custom_name ? custom_name : "UnknownCustomOp";
76 } else {
77 op_name = tflite::EnumNamesBuiltinOperator()[code];
78 }
79 const char* profiling_string =
80 interpreter.OpProfilingString(node_reg->second, &node_reg->first);
81 OperatorDetails details;
82 details.name = op_name;
83 if (profiling_string) {
84 details.name += ":" + std::string(profiling_string);
85 }
86 details.inputs = GetTensorNames(interpreter, inputs);
87 details.outputs = GetTensorNames(interpreter, outputs);
88 return details;
89 }
90
GetProfileSummarizerOptions()91 tensorflow::StatSummarizerOptions GetProfileSummarizerOptions() {
92 auto options = tensorflow::StatSummarizerOptions();
93 options.show_summary = true;
94 options.show_memory = false;
95 return options;
96 }
97
98 } // namespace
99
ProfileSummarizer()100 ProfileSummarizer::ProfileSummarizer()
101 : stats_calculator_(
102 new ::tensorflow::StatsCalculator(GetProfileSummarizerOptions())) {}
103
ProcessProfiles(const std::vector<const ProfileEvent * > & profile_stats,const tflite::Interpreter & interpreter)104 void ProfileSummarizer::ProcessProfiles(
105 const std::vector<const ProfileEvent*>& profile_stats,
106 const tflite::Interpreter& interpreter) {
107 std::vector<const ProfileEvent*> events;
108 std::copy_if(profile_stats.begin(), profile_stats.end(),
109 std::back_inserter(events), [](const ProfileEvent* e) {
110 return e->event_type ==
111 ProfileEvent::EventType::OPERATOR_INVOKE_EVENT &&
112 e->end_timestamp_us >= e->begin_timestamp_us;
113 });
114 // Sort with begin_time.
115 std::sort(events.begin(), events.end(),
116 [](const ProfileEvent* const& a, const ProfileEvent* const& b) {
117 return a->begin_timestamp_us < b->begin_timestamp_us;
118 });
119 if (events.empty()) {
120 return;
121 }
122
123 int64_t base_start_us = events[0]->begin_timestamp_us;
124 int node_num = 0;
125 int64_t curr_total_us = 0;
126 auto tag_string = [](const string& s, const string& t) {
127 return t == "OpInvoke" ? s : s + "/" + t;
128 };
129 for (auto event : events) {
130 auto op_details = GetOperatorDetails(interpreter, event->event_metadata);
131 auto node_name = ToString(op_details.outputs);
132 int64_t start_us = event->begin_timestamp_us - base_start_us;
133 int64_t node_exec_time =
134 event->end_timestamp_us - event->begin_timestamp_us;
135 stats_calculator_->AddNodeStats(tag_string(node_name, event->tag),
136 tag_string(op_details.name, event->tag),
137 node_num, start_us, node_exec_time,
138 0 /*memory */);
139
140 curr_total_us += node_exec_time;
141 ++node_num;
142 }
143 stats_calculator_->UpdateRunTotalUs(curr_total_us);
144 }
145 } // namespace profiling
146 } // namespace tflite
147