diff --git a/tensorflow/lite/core/api/profiler.h b/tensorflow/lite/core/api/profiler.h index aea70cd73f8..7bc296510d4 100644 --- a/tensorflow/lite/core/api/profiler.h +++ b/tensorflow/lite/core/api/profiler.h @@ -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)) diff --git a/tensorflow/lite/core/subgraph.cc b/tensorflow/lite/core/subgraph.cc index 38a9d24d782..e453ff2ff7e 100644 --- a/tensorflow/lite/core/subgraph.cc +++ b/tensorflow/lite/core/subgraph.cc @@ -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 diff --git a/tensorflow/lite/profiling/profile_summarizer.cc b/tensorflow/lite/profiling/profile_summarizer.cc index d69b0a697d7..b004bc2e361 100644 --- a/tensorflow/lite/profiling/profile_summarizer.cc +++ b/tensorflow/lite/profiling/profile_summarizer.cc @@ -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, diff --git a/tensorflow/lite/profiling/profile_summarizer_test.cc b/tensorflow/lite/profiling/profile_summarizer_test.cc index 6340921bc0e..0c4b9fcd88f 100644 --- a/tensorflow/lite/profiling/profile_summarizer_test.cc +++ b/tensorflow/lite/profiling/profile_summarizer_test.cc @@ -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; } diff --git a/tensorflow/lite/profiling/profiler.h b/tensorflow/lite/profiling/profiler.h index e75c90bf6b6..ff398698616 100644 --- a/tensorflow/lite/profiling/profiler.h +++ b/tensorflow/lite/profiling/profiler.h @@ -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_ diff --git a/tensorflow/lite/profiling/profiler_test.cc b/tensorflow/lite/profiling/profiler_test.cc index 57da951c8ce..cedb109697d 100644 --- a/tensorflow/lite/profiling/profiler_test.cc +++ b/tensorflow/lite/profiling/profiler_test.cc @@ -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());