1 /* Copyright 2017 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/compiler/xla/service/hlo_profile_printer.h"
17
18 #include "absl/algorithm/container.h"
19 #include "absl/strings/str_cat.h"
20 #include "tensorflow/compiler/xla/service/human_readable_profile_builder.h"
21
22 namespace xla {
PrintHloProfile(const HloProfilePrinterData & hlo_profile_printer_data,const int64 * counters,double clock_rate_ghz)23 string PrintHloProfile(const HloProfilePrinterData& hlo_profile_printer_data,
24 const int64* counters, double clock_rate_ghz) {
25 using HloComputationInfo = HloProfilePrinterData::HloComputationInfo;
26 using HloInstructionInfo = HloProfilePrinterData::HloInstructionInfo;
27
28 string result;
29
30 for (const auto& item : hlo_profile_printer_data.extra_metrics()) {
31 absl::StrAppend(&result, "Extra metric ", item.first, ": ",
32 counters[item.second], "\n");
33 }
34
35 for (const HloComputationInfo& computation_info :
36 hlo_profile_printer_data.computation_infos()) {
37 const auto& instruction_infos = computation_info.instruction_infos();
38 bool any_instruction_profiled = absl::c_any_of(
39 instruction_infos, [&](const HloInstructionInfo& instruction_info) {
40 return counters[instruction_info.profile_index()] != 0;
41 });
42
43 if (!any_instruction_profiled) {
44 continue;
45 }
46
47 // Once we start using this in AOT for real, we will probably need a more
48 // minimal version of HumanReadableProfileBuilder.
49 HumanReadableProfileBuilder builder(
50 computation_info.name(),
51 hlo_profile_printer_data.entry_computation() == computation_info.name(),
52 counters[computation_info.profile_index()], clock_rate_ghz);
53
54 for (const auto& instruction_info : instruction_infos) {
55 builder.AddOp(
56 /*op_name=*/instruction_info.long_name(),
57 /*short_name=*/instruction_info.short_name(),
58 instruction_info.category(),
59 counters[instruction_info.profile_index()],
60 instruction_info.flop_count(),
61 instruction_info.transcendental_count(),
62 instruction_info.bytes_accessed(),
63 instruction_info.optimal_seconds());
64 }
65
66 result += builder.ToString();
67 }
68
69 return result;
70 }
71 } // namespace xla
72