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_viewer.h"
17 
18 #include "tensorflow/core/platform/logging.h"
19 #include "tensorflow/core/profiler/convert/op_stats_to_pod_stats.h"
20 #include "tensorflow/core/profiler/protobuf/pod_stats.pb.h"
21 #include "tensorflow/core/profiler/protobuf/steps_db.pb.h"
22 #include "tensorflow/core/profiler/utils/diagnostics.h"
23 
24 namespace tensorflow {
25 namespace profiler {
26 namespace {
27 
ConvertOpStatsToPodStatsSequence(const OpStats & op_stats,PodStatsDatabase pod_stats)28 PodStatsSequence ConvertOpStatsToPodStatsSequence(const OpStats& op_stats,
29                                                   PodStatsDatabase pod_stats) {
30   PodStatsSequence result_db;
31   // PodStatsDatabase is created using the same iteration order below.
32   // Thus, we just need to move one record at a time.
33   int i = 0;
34   for (const auto& step_sequence : op_stats.step_db().step_sequence()) {
35     PodStatsMap* pod_stats_map = result_db.add_pod_stats_map();
36     pod_stats_map->set_step_num(step_sequence.step_num());
37     for (const auto& entry : step_sequence.step_info_per_core()) {
38       PodStatsRecord& record =
39           (*pod_stats_map->mutable_pod_stats_per_core())[entry.first];
40       DCHECK_LE(i, pod_stats.pod_stats_record_size());
41       record = std::move(*pod_stats.mutable_pod_stats_record(i++));
42     }
43   }
44   return result_db;
45 }
46 
47 }  // namespace
48 
ConvertOpStatsToPodViewer(const OpStats & op_stats)49 PodViewerDatabase ConvertOpStatsToPodViewer(const OpStats& op_stats) {
50   PodViewerDatabase database;
51   database.set_device_type(op_stats.run_environment().device_type());
52   PodStatsDatabase pod_stats = ConvertOpStatsToPodStats(op_stats);
53   database.mutable_step_breakdown_events()->Swap(
54       pod_stats.mutable_step_breakdown_events());
55   *database.mutable_pod_stats_sequence() =
56       ConvertOpStatsToPodStatsSequence(op_stats, std::move(pod_stats));
57   PopulateStepDiagnostics(op_stats, database.mutable_diagnostics());
58   return database;
59 }
60 
61 }  // namespace profiler
62 }  // namespace tensorflow
63