xref: /aosp_15_r20/external/tensorflow/tensorflow/compiler/xla/service/compilation_stats.cc (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 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/compilation_stats.h"
17 
18 #include <iostream>
19 #include <memory>
20 #include <string>
21 
22 #include "absl/container/flat_hash_map.h"
23 #include "absl/strings/str_format.h"
24 #include "tensorflow/compiler/xla/types.h"
25 #include "tensorflow/core/platform/env.h"
26 
27 namespace xla {
28 
29 class NoopStats : public CompilationStats {
30  public:
31   NoopStats() = default;
32 
StartPass(absl::string_view pass_name)33   void StartPass(absl::string_view pass_name) override {}
34 
EndPass(absl::string_view pass_name)35   void EndPass(absl::string_view pass_name) override {}
36 
CompilationReport()37   void CompilationReport() override {}
38 
GetPassesSize()39   int GetPassesSize() override { return 0; }
40 };
41 
42 class Stats : public CompilationStats {
43  public:
44   Stats() = default;
45 
46   void StartPass(absl::string_view pass_name) override;
47 
48   void EndPass(absl::string_view pass_name) override;
49 
50   void CompilationReport() override;
51 
52   int GetPassesSize() override;
53 
54  private:
55   struct PassInfo {
PassInfoxla::Stats::PassInfo56     PassInfo(absl::string_view name, double duration)
57         : name(name), duration_ms(duration) {}
58 
59     std::string name;
60     int num_runs = 1;
61     double duration_ms;
62   };
63 
64   // Info about the passes that have been run so far.
65   std::vector<PassInfo> passes_;
66   // Used to avoid nested calls to StartPass.
67   bool pass_running_ = false;
68   std::string current_pass_;
69   // The start time of the currently running pass.
70   uint64_t start_micros_;
71 };
72 
73 /* static */
MakeNoopStats()74 std::unique_ptr<CompilationStats> CompilationStats::MakeNoopStats() {
75   return std::make_unique<NoopStats>();
76 }
77 
78 /* static */
MakeStats()79 std::unique_ptr<CompilationStats> CompilationStats::MakeStats() {
80   return std::make_unique<Stats>();
81 }
82 
StartPass(absl::string_view pass_name)83 void Stats::StartPass(absl::string_view pass_name) {
84   CHECK(!pass_running_) << "Can't start " << pass_name << " while running "
85                         << current_pass_;
86   pass_running_ = true;
87   current_pass_ = std::string(pass_name);
88   start_micros_ = tensorflow::Env::Default()->NowMicros();
89 }
90 
EndPass(absl::string_view pass_name)91 void Stats::EndPass(absl::string_view pass_name) {
92   CHECK(pass_running_);
93   CHECK_EQ(current_pass_, std::string(pass_name));
94   pass_running_ = false;
95   uint64_t end_micros = tensorflow::Env::Default()->NowMicros();
96   double duration_ms = (end_micros - start_micros_) / 1000.0;
97   passes_.push_back(PassInfo(current_pass_, duration_ms));
98 }
99 
CompilationReport()100 void Stats::CompilationReport() {
101   CHECK(!pass_running_) << "EndPass never called for " << current_pass_;
102   absl::flat_hash_map<std::string, PassInfo> summary;
103   double total_duration = 0;
104 
105   for (auto& pass_run : passes_) {
106     auto pass_name = pass_run.name;
107     total_duration += pass_run.duration_ms;
108     auto it = summary.find(pass_name);
109     if (it == summary.end()) {
110       summary.insert(std::make_pair(pass_name, pass_run));
111     } else {
112       ++summary.at(pass_name).num_runs;
113       summary.at(pass_name).duration_ms += pass_run.duration_ms;
114     }
115   }
116 
117   std::vector<PassInfo> sorted_summary;
118   sorted_summary.reserve(summary.size());
119   for (auto& it : summary) {
120     sorted_summary.push_back(it.second);
121   }
122   absl::c_sort(sorted_summary, [](const PassInfo& a, const PassInfo& b) {
123     // Sort passes that take the longest first, break ties using pass names.
124     return std::make_pair(b.duration_ms, a.name) <
125            std::make_pair(a.duration_ms, b.name);
126   });
127   LOG(INFO) << "Total runtime (ms) of HLO passes: " << total_duration;
128   LOG(INFO) << "Pass name, num runs, time (ms)";
129   for (auto& pass_info : sorted_summary) {
130     LOG(INFO) << pass_info.name << ", " << pass_info.num_runs << ", "
131               << pass_info.duration_ms;
132   }
133 }
134 
GetPassesSize()135 int Stats::GetPassesSize() { return passes_.size(); }
136 
137 }  // namespace xla
138