Use op_name as the tag when recording profiling events so that it's more specific than the original general "OpInvoke" tag.

As a result of this, simplify the existing interpretation of recorded profiling events in profiling summarizer.

PiperOrigin-RevId: 283452539
Change-Id: Ifd4a3d27c6975aec9d48a812a41165aa7bda6e81
This commit is contained in:
Chao Mei 2019-12-02 17:29:35 -08:00 committed by TensorFlower Gardener
parent bfe235d23f
commit 9bc6a80e2b
6 changed files with 22 additions and 25 deletions

View File

@ -93,9 +93,6 @@ class ScopedOperatorProfile : public ScopedProfile {
tflite::ScopedOperatorProfile TFLITE_VARNAME_UNIQ(_profile_, __COUNTER__)( \
(profiler), (tag), (node_index))
#define TFLITE_SCOPED_OPERATOR_PROFILE(profiler, node_index) \
TFLITE_SCOPED_TAGGED_OPERATOR_PROFILE((profiler), "OpInvoke", (node_index))
#define TFLITE_SCOPED_DELEGATE_OPERATOR_PROFILE(profiler, node_index) \
TFLITE_SCOPED_TAGGED_OPERATOR_PROFILE((profiler), "DelegateOpInvoke", \
(node_index))

View File

@ -758,7 +758,17 @@ TfLiteStatus Subgraph::Invoke() {
TfLiteNode& node = nodes_and_registration_[node_index].first;
const TfLiteRegistration& registration =
nodes_and_registration_[node_index].second;
TFLITE_SCOPED_OPERATOR_PROFILE(profiler_.get(), node_index);
const char* op_name = nullptr;
if (profiler_) {
if (registration.builtin_code == tflite::BuiltinOperator_CUSTOM) {
const char* const custom_name = registration.custom_name;
op_name = custom_name ? custom_name : "UnknownCustomOp";
} else {
op_name = tflite::EnumNamesBuiltinOperator()[registration.builtin_code];
}
}
TFLITE_SCOPED_TAGGED_OPERATOR_PROFILE(profiler_.get(), op_name, node_index);
// TODO(ycling): This is an extra loop through inputs to check if the data
// need to be copied from Delegate buffer to raw memory, which is often not

View File

@ -27,7 +27,7 @@ namespace {
struct OperatorDetails {
uint32_t subgraph_index;
uint32_t node_index;
std::string name;
std::string op_description;
std::vector<std::string> inputs;
std::vector<std::string> outputs;
};
@ -74,20 +74,11 @@ OperatorDetails GetOperatorDetails(const tflite::Interpreter& interpreter,
auto node_reg = subgraph->node_and_registration(node_index);
auto inputs = node_reg->first.inputs;
auto outputs = node_reg->first.outputs;
int code = node_reg->second.builtin_code;
const char* op_name = nullptr;
if (code == tflite::BuiltinOperator_CUSTOM) {
const char* custom_name = node_reg->second.custom_name;
op_name = custom_name ? custom_name : "UnknownCustomOp";
} else {
op_name = tflite::EnumNamesBuiltinOperator()[code];
}
const char* profiling_string =
interpreter.OpProfilingString(node_reg->second, &node_reg->first);
OperatorDetails details;
details.name = op_name;
if (profiling_string) {
details.name += ":" + std::string(profiling_string);
details.op_description = std::string(profiling_string);
}
details.inputs = GetTensorNames(interpreter, inputs);
details.outputs = GetTensorNames(interpreter, outputs);
@ -132,9 +123,6 @@ void ProfileSummarizer::ProcessProfiles(
int64_t base_start_us = events[0]->begin_timestamp_us;
int node_num = 0;
auto tag_string = [](const string& s, const string& t) {
return (t == "OpInvoke" || t == "DelegateOpInvoke") ? s : s + "/" + t;
};
// Total time will be accumulated per subgraph.
std::map<uint32_t, int64_t> total_us_per_subgraph_map;
@ -154,13 +142,16 @@ void ProfileSummarizer::ProcessProfiles(
const auto op_details =
GetOperatorDetails(interpreter, subgraph_index, node_index);
const auto type_in_stats = tag_string(op_details.name, event->tag);
std::string type_in_stats(event->tag);
if (!op_details.op_description.empty()) {
type_in_stats += "/" + op_details.op_description;
}
const auto node_name = ToString(op_details.outputs);
// Append node index to node name because 'stats_calculator' can not
// distinguish two nodes w/ the same 'node_name'.
const auto node_name_in_stats =
tag_string(node_name + ":" + std::to_string(node_index), event->tag);
node_name + ":" + std::to_string(node_index);
stats_calculator->AddNodeStats(node_name_in_stats, type_in_stats,
node_num, start_us, node_exec_time,

View File

@ -141,7 +141,7 @@ TEST(ProfileSummarizerTest, InterpreterPlusProfilingDetails) {
summarizer.ProcessProfiles(profiler.GetProfileEvents(), *interpreter);
auto output = summarizer.GetOutputString();
// TODO(shashishekhar): Add a better test here.
ASSERT_TRUE(output.find("SimpleOpEval:Profile") != std::string::npos)
ASSERT_TRUE(output.find("SimpleOpEval/Profile") != std::string::npos)
<< output;
}

View File

@ -32,6 +32,5 @@ using Profiler = NoopProfiler;
} // namespace tflite
#define SCOPED_TAGGED_OPERATOR_PROFILE TFLITE_SCOPED_TAGGED_OPERATOR_PROFILE
#define SCOPED_OPERATOR_PROFILE TFLITE_SCOPED_OPERATOR_PROFILE
#endif // TENSORFLOW_LITE_PROFILING_PROFILER_H_

View File

@ -97,13 +97,13 @@ TEST(ProfilingTest, ProfilesAreCollected) {
TEST(ProfilingTest, NullProfiler) {
Profiler* profiler = nullptr;
{ SCOPED_OPERATOR_PROFILE(profiler, 1); }
{ SCOPED_TAGGED_OPERATOR_PROFILE(profiler, "noop", 1); }
}
TEST(ProfilingTest, ScopedProfile) {
BufferedProfiler profiler(1024);
profiler.StartProfiling();
{ SCOPED_OPERATOR_PROFILE(&profiler, 1); }
{ SCOPED_TAGGED_OPERATOR_PROFILE(&profiler, "noop", 1); }
profiler.StopProfiling();
auto profile_events = profiler.GetProfileEvents();
EXPECT_EQ(1, profile_events.size());
@ -112,7 +112,7 @@ TEST(ProfilingTest, ScopedProfile) {
TEST(ProfilingTest, NoopProfiler) {
NoopProfiler profiler;
profiler.StartProfiling();
{ SCOPED_OPERATOR_PROFILE(&profiler, 1); }
{ SCOPED_TAGGED_OPERATOR_PROFILE(&profiler, "noop", 1); }
profiler.StopProfiling();
auto profile_events = profiler.GetProfileEvents();
EXPECT_EQ(0, profile_events.size());