• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 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 // This checker checks the accelerator's utilization.
16 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_ADVISOR_ACCELERATOR_UTILIZATION_CHECKER_H_
17 #define TENSORFLOW_CORE_PROFILER_INTERNAL_ADVISOR_ACCELERATOR_UTILIZATION_CHECKER_H_
18 
19 #include "absl/strings/str_format.h"
20 #include "tensorflow/core/profiler/internal/advisor/checker.h"
21 
22 namespace tensorflow {
23 namespace tfprof {
24 
25 struct ExecStats {
26  public:
27   // Earliest start time of a step.
28   int64_t start_micros;
29   // Latest finish time of a step.
30   int64_t end_micros;
31   // The duration spent on running a kernel during a step.
32   int64_t exec_micros;
33 };
34 
35 class AcceleratorUtilizationChecker : public Checker {
36  public:
name()37   string name() const override { return kCheckers[0]; }
38 
39  private:
Check(const AdvisorOptionsProto::CheckerOption & options,const TFStats * stats)40   AdviceProto::Checker Check(const AdvisorOptionsProto::CheckerOption& options,
41                              const TFStats* stats) override {
42     if (!stats) {
43       absl::FPrintF(
44           stderr, "Missing profiles (e.g. graph, run_meta). Skip %s\n", name());
45       return reports_;
46     }
47     for (const auto& n : stats->nodes()) {
48       BuildExecStats(n.second.get());
49     }
50     return CheckInternal();
51   }
52 
CheckInternal()53   AdviceProto::Checker CheckInternal() {
54     for (const auto& s : accelerator_exec_stats_) {
55       const ExecStats& stat = s.second;
56       int64_t total_micros = stat.end_micros - stat.start_micros;
57       if (total_micros <= 0) continue;
58       double utilization = 1.0 * stat.exec_micros / total_micros;
59       if (utilization >= 0.5) {
60         reports_.add_reports(absl::StrFormat("device: %s utilization: %.2f",
61                                              s.first, utilization));
62       } else if (utilization < 0.5 && utilization > 0.2) {
63         reports_.add_reports(absl::StrFormat("device: %s low utilization: %.2f",
64                                              s.first, utilization));
65       } else if (utilization <= 0.2) {
66         reports_.add_reports(absl::StrFormat("device: %s low utilization: %.2f",
67                                              s.first, utilization));
68       }
69     }
70     return reports_;
71   }
72 
BuildExecStats(const TFGraphNode * node)73   void BuildExecStats(const TFGraphNode* node) {
74     const auto& execs = node->all_op_execs();
75     if (execs.empty()) {
76       return;
77     }
78     if (!IsPlacedOnAccelerator(node->canonical_device())) {
79       return;
80     }
81 
82     if (accelerator_exec_stats_.find(node->canonical_device()) ==
83         accelerator_exec_stats_.end()) {
84       accelerator_exec_stats_.insert(
85           std::pair<string, ExecStats>(node->canonical_device(), ExecStats()));
86     }
87     ExecStats& stats = accelerator_exec_stats_.at(node->canonical_device());
88 
89     // TODO(xpan): Use multiple steps?
90     const ExecStep& exec = execs.rbegin()->second;
91 
92     if (stats.start_micros == 0) {
93       stats.start_micros = exec.all_start_micros();
94     } else if (exec.all_start_micros() != 0) {
95       stats.start_micros =
96           std::min(stats.start_micros, exec.all_start_micros());
97     }
98     stats.end_micros = std::max(stats.end_micros, exec.latest_end_micros());
99     stats.exec_micros += exec.accelerator_exec_micros();
100   }
101 
102   std::map<string, ExecStats> accelerator_exec_stats_;
103   std::map<string, int64_t> ps_placement_;
104   AdviceProto::Checker reports_;
105 };
106 
107 }  // namespace tfprof
108 }  // namespace tensorflow
109 
110 #endif  // TENSORFLOW_CORE_PROFILER_INTERNAL_ADVISOR_ACCELERATOR_UTILIZATION_CHECKER_H_
111