xref: /aosp_15_r20/external/tensorflow/tensorflow/core/profiler/convert/op_stats_to_op_profile.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2022 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_op_profile.h"
17 
18 #include <string>
19 
20 #include "absl/strings/match.h"
21 #include "tensorflow/core/platform/logging.h"
22 #include "tensorflow/core/profiler/convert/op_profile_builder.h"
23 #include "tensorflow/core/profiler/protobuf/hardware_types.pb.h"
24 #include "tensorflow/core/profiler/protobuf/op_metrics.pb.h"
25 #include "tensorflow/core/profiler/protobuf/op_profile.pb.h"
26 #include "tensorflow/core/profiler/protobuf/op_stats.pb.h"
27 #include "tensorflow/core/profiler/utils/math_utils.h"
28 #include "tensorflow/core/profiler/utils/op_metrics_db_utils.h"
29 
30 namespace tensorflow {
31 namespace profiler {
32 namespace {
33 
34 using ::tensorflow::profiler::GigaToGibi;
35 using ::tensorflow::profiler::IsIdleOp;
36 using ::tensorflow::profiler::OpMetrics;
37 using ::tensorflow::profiler::OpProfileBuilder;
38 using ::tensorflow::profiler::OpProfileOptions;
39 using ::tensorflow::profiler::OpStats;
40 using ::tensorflow::profiler::TeraToGiga;
41 using ::tensorflow::profiler::TotalTimePs;
42 using ::tensorflow::profiler::op_profile::Node;
43 
BuildOpProfileNodeTree(const OpStats & op_stats,bool group_by_program,bool exclude_idle_ops,Node * root)44 void BuildOpProfileNodeTree(const OpStats& op_stats, bool group_by_program,
45                             bool exclude_idle_ops, Node* root) {
46   const auto& metrics_db = op_stats.device_op_metrics_db();
47   if (metrics_db.metrics_db().empty()) return;
48 
49   OpProfileOptions options = {group_by_program,
50                               /*group_by_deduplicated_name=*/true,
51                               /*children_per_node=*/100};
52   OpProfileBuilder builder(options, root, &op_stats.program_id_to_name_map());
53 
54   for (const OpMetrics& op_metrics : metrics_db.metrics_db()) {
55     DCHECK(!op_metrics.name().empty());
56     // Don't add ops that cannot be symbolized.
57     if (absl::StartsWith(op_metrics.name(), "region")) continue;
58     if (exclude_idle_ops && IsIdleOp(op_metrics)) continue;
59     builder.AddOp(op_metrics);
60   }
61 
62   const auto& perf_env = op_stats.perf_env();
63   double max_gigaflops_per_second_per_core =
64       TeraToGiga(perf_env.peak_tera_flops_per_second());
65   double max_gibibytes_per_second_per_core =
66       GigaToGibi(perf_env.peak_hbm_bw_giga_bytes_per_second());
67   builder.Finalize(max_gigaflops_per_second_per_core,
68                    max_gibibytes_per_second_per_core,
69                    TotalTimePs(metrics_db, exclude_idle_ops));
70 }
71 
72 }  // namespace
73 
ConvertOpStatsToOpProfile(const OpStats & op_stats,tensorflow::profiler::HardwareType hardware_type,tensorflow::profiler::op_profile::Profile & profile)74 void ConvertOpStatsToOpProfile(
75     const OpStats& op_stats, tensorflow::profiler::HardwareType hardware_type,
76     tensorflow::profiler::op_profile::Profile& profile) {
77   profile.set_device_type(HardwareType_Name(hardware_type));
78   BuildOpProfileNodeTree(op_stats,
79                          /*group_by_program=*/false,
80                          /*exclude_idle_ops=*/false,
81                          profile.mutable_by_category());
82 
83   BuildOpProfileNodeTree(op_stats,
84                          /*group_by_program=*/false,
85                          /*exclude_idle_ops=*/true,
86                          profile.mutable_by_category_exclude_idle());
87 
88   // Don't generate per program profile if there's only a single program.
89   if (op_stats.program_id_to_name_map_size() > 1) {
90     BuildOpProfileNodeTree(op_stats,
91                            /*group_by_program=*/true,
92                            /*exclude_idle_ops=*/false,
93                            profile.mutable_by_program());
94 
95     BuildOpProfileNodeTree(op_stats,
96                            /*group_by_program=*/true,
97                            /*exclude_idle_ops=*/true,
98                            profile.mutable_by_program_exclude_idle());
99   }
100 }
101 
102 }  // namespace profiler
103 }  // namespace tensorflow
104