From 75850423270a9943e4320233af4f16a44100e735 Mon Sep 17 00:00:00 2001 From: Jiho Choi Date: Sat, 27 Jun 2020 10:13:09 -0700 Subject: [PATCH] Use the event name as part of the step name for the explicit root events. PiperOrigin-RevId: 318634056 Change-Id: I2860534f4ebe62e732306a39a6a8fd57f6366b16 --- .../profiler/convert/xplane_to_trace_events.cc | 3 +++ tensorflow/core/profiler/utils/group_events.cc | 17 ++++++++++++++++- .../core/profiler/utils/group_events_test.cc | 7 ++++--- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/tensorflow/core/profiler/convert/xplane_to_trace_events.cc b/tensorflow/core/profiler/convert/xplane_to_trace_events.cc index 882f50e6080..ceb3e003564 100644 --- a/tensorflow/core/profiler/convert/xplane_to_trace_events.cc +++ b/tensorflow/core/profiler/convert/xplane_to_trace_events.cc @@ -91,6 +91,9 @@ void ConvertXPlaneToTraceEvents(uint32 device_id, const XPlaneVisitor& xplane, xevent.ForEachStat([&](const XStatVisitor& stat) { if (stat.ValueCase() == XStat::VALUE_NOT_SET) return; if (IsInternalStat(stat.Type())) return; + if (stat.Type() == StatType::kStepName) { + event->set_name(stat.ToString()); + } args[std::string(stat.Name())] = stat.ToString(); }); }); diff --git a/tensorflow/core/profiler/utils/group_events.cc b/tensorflow/core/profiler/utils/group_events.cc index 0772cff7b97..926dfe65156 100644 --- a/tensorflow/core/profiler/utils/group_events.cc +++ b/tensorflow/core/profiler/utils/group_events.cc @@ -139,12 +139,25 @@ bool HasFunctionRun(EventNode* event_node) { return false; } +bool IsImplicitRootEvent(const XEventVisitor& event) { + static const auto* const kImplicitRootEvents = new absl::flat_hash_set{ + HostEventType::kFunctionRun, HostEventType::kSessionRun, + HostEventType::kRunGraph, HostEventType::kExecutorStateProcess}; + return event.Type().has_value() && + kImplicitRootEvents->contains(*event.Type()); +} + void ProcessRootEvent(int64 group_id, EventNode* root_event, EventGroupNameMap* event_group_name_map) { root_event->PropagateGroupId(group_id); std::string group_name = root_event->GetGroupName(); // TODO(jihochoi): change event name instead. - root_event->AddStepName(group_name); + if (!IsImplicitRootEvent(root_event->GetEventVisitor())) { + // Add the `step_name` stat for the user-defined root events only. When an + // XEvent is converted to a trace event, the trace event name is set to the + // `step_name` stat's value if present. + root_event->AddStepName(group_name); + } event_group_name_map->emplace(group_id, std::move(group_name)); } @@ -336,6 +349,8 @@ std::string EventNode::GetGroupName() const { if (absl::optional stat = GetContextStat(StatType::kGraphType)) { absl::StrAppend(&name, stat->StrOrRefValue(), " "); + } else if (!(IsImplicitRootEvent(visitor_))) { + absl::StrAppend(&name, GetEventVisitor().Name(), " "); } int64 step_num = group_id_.value_or(0); if (absl::optional stat = GetContextStat(StatType::kIterNum)) { diff --git a/tensorflow/core/profiler/utils/group_events_test.cc b/tensorflow/core/profiler/utils/group_events_test.cc index e9f5d58f8d5..231eec5ebe7 100644 --- a/tensorflow/core/profiler/utils/group_events_test.cc +++ b/tensorflow/core/profiler/utils/group_events_test.cc @@ -40,8 +40,9 @@ TEST(GroupEventsTest, GroupGpuTraceTest) { host_plane_builder.ReserveLines(2); auto main_thread = host_plane_builder.GetOrCreateLine(0); - CreateXEvent(&host_plane_builder, &main_thread, HostEventType::kTraceContext, - 0, 100, {{StatType::kStepNum, kStepNum}}); + CreateXEvent( + &host_plane_builder, &main_thread, HostEventType::kTraceContext, 0, 100, + {{StatType::kGraphType, "train"}, {StatType::kStepNum, kStepNum}}); CreateXEvent(&host_plane_builder, &main_thread, HostEventType::kFunctionRun, 10, 90, {{StatType::kStepId, kStepId}}); @@ -68,7 +69,7 @@ TEST(GroupEventsTest, GroupGpuTraceTest) { device_plane->lines(0).events(0).stats(1)), StatType::kGroupId); EXPECT_EQ(event_group_name_map.size(), 1); - EXPECT_EQ(event_group_name_map[0], "123"); + EXPECT_EQ(event_group_name_map[0], "train 123"); } TEST(GroupEventsTest, GroupTensorFlowLoopTest) {