1 /* Copyright 2020 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/core/profiler/convert/op_stats_to_pod_stats.h"
17 
18 #include "google/protobuf/any.pb.h"
19 #include "absl/strings/string_view.h"
20 #include "tensorflow/core/lib/gtl/map_util.h"
21 #include "tensorflow/core/platform/logging.h"
22 #include "tensorflow/core/profiler/protobuf/steps_db.pb.h"
23 #include "tensorflow/core/profiler/utils/diagnostics.h"
24 #include "tensorflow/core/profiler/utils/event_span.h"
25 #include "tensorflow/core/profiler/utils/time_utils.h"
26 
27 namespace tensorflow {
28 namespace profiler {
29 
30 namespace {
31 
CreatePodStatsRecord(absl::string_view host_name,const StepInfoResult & step_info)32 PodStatsRecord CreatePodStatsRecord(absl::string_view host_name,
33                                     const StepInfoResult& step_info) {
34   PodStatsRecord record;
35   GenericStepBreakdown generic;
36   bool success = step_info.step_breakdown().UnpackTo(&generic);
37   DCHECK(success);
38   record.set_host_name(string(host_name));
39   record.set_step_num(step_info.step_num());
40   record.set_total_duration_us(PicosToMicros(step_info.duration_ps()));
41   auto& step_breakdown_map = *record.mutable_step_breakdown_us();
42   std::vector<std::pair<uint64, absl::string_view>> metrics;
43 
44   auto add_event = [&](GenericEventType type,
45                        std::initializer_list<EventType> event_list) {
46     uint64 ps = 0;
47     for (const auto& event_type : event_list) {
48       ps += gtl::FindWithDefault(generic.type_ps(), event_type, /*value=*/0);
49     }
50     step_breakdown_map[type] = PicosToMicros(ps);
51     metrics.emplace_back(ps, GetGenericEventTypeStr(type));
52   };
53 
54   add_event(kDeviceCompute, {DEVICE_COMPUTE_32, DEVICE_COMPUTE_16});
55   add_event(kDeviceToDevice, {DEVICE_TO_DEVICE, DEVICE_WAIT_DEVICE});
56   add_event(kDeviceCollectives, {DEVICE_COLLECTIVES});
57   add_event(kHostCompute, {HOST_COMPUTE});
58   add_event(kHostPrepare, {HOST_PREPARE});
59   add_event(kInput, {HOST_WAIT_INPUT, HOST_TO_DEVICE, DEVICE_WAIT_HOST});
60   add_event(kOutput, {DEVICE_TO_HOST});
61   add_event(kCompile, {HOST_COMPILE});
62   add_event(kAllOthers, {UNKNOWN_TIME});
63 
64   std::sort(metrics.begin(), metrics.end());
65   record.set_bottleneck(metrics.back().second.data(),
66                         metrics.back().second.size());
67   return record;
68 }
69 
70 }  // namespace
71 
ConvertOpStatsToPodStats(const OpStats & op_stats)72 PodStatsDatabase ConvertOpStatsToPodStats(const OpStats& op_stats) {
73   PodStatsDatabase pod_stats_db;
74   const auto& core_id_map = op_stats.core_id_to_details();
75   for (int i = GenericEventType::kFirstGenericEventType;
76        i <= GenericEventType::kLastGenericEventType; i++) {
77     auto& event = *pod_stats_db.add_step_breakdown_events();
78     event.set_id(i);
79     absl::string_view type_str =
80         GetGenericEventTypeStr(static_cast<GenericEventType>(i));
81     event.set_name(type_str.data(), type_str.size());
82   }
83 
84   for (const auto& step_sequence : op_stats.step_db().step_sequence()) {
85     for (const auto& entry : step_sequence.step_info_per_core()) {
86       const CoreDetails& details = core_id_map.at(entry.first);
87       *pod_stats_db.add_pod_stats_record() =
88           CreatePodStatsRecord(details.hostname(), entry.second);
89     }
90   }
91   PopulateStepDiagnostics(op_stats, pod_stats_db.mutable_diagnostics());
92   return pod_stats_db;
93 }
94 
95 }  // namespace profiler
96 }  // namespace tensorflow
97