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 most expensive operations. 16 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_ADVISOR_EXPENSIVE_OPERATION_CHECKER_H_ 17 #define TENSORFLOW_CORE_PROFILER_INTERNAL_ADVISOR_EXPENSIVE_OPERATION_CHECKER_H_ 18 19 #include "tensorflow/core/profiler/internal/advisor/checker.h" 20 21 namespace tensorflow { 22 namespace tfprof { 23 24 class ExpensiveOperationChecker : public Checker { 25 public: name()26 string name() const override { return kCheckers[2]; } 27 28 private: Check(const AdvisorOptionsProto::CheckerOption & options,const TFStats * stats)29 AdviceProto::Checker Check(const AdvisorOptionsProto::CheckerOption& options, 30 const TFStats* stats) override { 31 if (!stats) { 32 fprintf(stderr, "Missing profiles (e.g. graph, run_meta). Skip %s\n", 33 name().c_str()); 34 return reports_; 35 } 36 if (stats->steps().empty()) { 37 fprintf(stderr, "Missing RunMetadata info. Skip %s\n", name().c_str()); 38 } 39 CheckOpView(stats); 40 CheckScopeView(stats); 41 CheckCodeView(stats); 42 return reports_; 43 } 44 CheckOpView(const TFStats * stats)45 void CheckOpView(const TFStats* stats) { 46 if (stats->steps().empty()) { 47 fprintf(stderr, "Missing run_meta for %s\n", name().c_str()); 48 return; 49 } 50 Options opts(3, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1, "micros", {".*"}, {".*"}, 51 {}, {".*"}, {}, false, {"micros", "occurrence"}, "none", {}); 52 const MultiGraphNodeProto root = stats->ShowMultiGraphNode("op", opts); 53 if (root.children_size() == 0) { 54 return; 55 } 56 const MultiGraphNodeProto* node = &root; 57 std::vector<string> outputs; 58 for (int i = 0; i < 3 && node->children_size() > 0; ++i) { 59 node = &node->children(0); 60 outputs.push_back(strings::Printf( 61 "top %d operation type: %s, " 62 "cpu: %s, accelerator: %s, total: %s (%.2f%%)", 63 i + 1, node->name().c_str(), 64 FormatTime(node->cpu_exec_micros()).c_str(), 65 FormatTime(node->accelerator_exec_micros()).c_str(), 66 FormatTime(node->exec_micros()).c_str(), 67 100.0 * node->exec_micros() / (root.total_exec_micros() + 1e-10))); 68 } 69 reports_.add_reports(str_util::Join(outputs, "\n")); 70 } 71 CheckCodeView(const TFStats * stats)72 void CheckCodeView(const TFStats* stats) { 73 if (!stats->has_code_traces()) { 74 fprintf(stderr, "Missing op_log (code traces) for %s\n", name().c_str()); 75 return; 76 } 77 Options opts(100, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1, "micros", {".*"}, 78 {".*"}, {}, {".*"}, {}, false, {"micros"}, "none", {}); 79 const MultiGraphNodeProto root = stats->ShowMultiGraphNode("code", opts); 80 const MultiGraphNodeProto* node = &root; 81 // A trick here is: Usually, codes in library file are usually referenced 82 // only once, while user's own code are referenced multiple times. 83 while (node->children_size() == 1) { 84 node = &node->children(0); 85 } 86 if (node->children_size() == 0) { 87 return; 88 } 89 90 std::vector<string> outputs; 91 CodeViewHelper(node, 0, &outputs); 92 reports_.add_reports(str_util::Join(outputs, "\n")); 93 } 94 CheckScopeView(const TFStats * stats)95 void CheckScopeView(const TFStats* stats) { 96 Options opts(100, 0, 0, 0, 0, 100, 0, 0, 0, 0, 0, -1, "micros", {".*"}, 97 {".*"}, {}, {".*"}, {}, false, {"micros"}, "none", {}); 98 const GraphNodeProto root = stats->ShowGraphNode("scope", opts); 99 if (root.children_size() == 0) { 100 return; 101 } 102 std::vector<string> outputs; 103 for (int i = 0; i < 3 && i < root.children_size(); ++i) { 104 const GraphNodeProto& node = root.children(i); 105 outputs.push_back(strings::Printf( 106 "top %d graph node: %s, cpu: %s, accelerator: %s, total: %s", i + 1, 107 node.name().c_str(), FormatTime(node.cpu_exec_micros()).c_str(), 108 FormatTime(node.accelerator_exec_micros()).c_str(), 109 FormatTime(node.exec_micros()).c_str())); 110 } 111 reports_.add_reports(str_util::Join(outputs, "\n")); 112 } 113 CodeViewHelper(const MultiGraphNodeProto * node,int depth,std::vector<string> * outputs)114 void CodeViewHelper(const MultiGraphNodeProto* node, int depth, 115 std::vector<string>* outputs) { 116 if (node->children_size() <= 1 || depth > 3) { 117 return; 118 } 119 for (int j = 0; j < 3 && j < node->children_size(); ++j) { 120 const MultiGraphNodeProto* c = &node->children(j); 121 if (c->total_exec_micros() < 1000) { 122 continue; 123 } 124 outputs->push_back(strings::Printf( 125 "%s%s, cpu: %s, accelerator: %s, total: %s", 126 string(depth * 2, ' ').c_str(), c->name().c_str(), 127 FormatTime(c->total_cpu_exec_micros()).c_str(), 128 FormatTime(c->total_accelerator_exec_micros()).c_str(), 129 FormatTime(c->total_exec_micros()).c_str())); 130 CodeViewHelper(c, depth + 1, outputs); 131 } 132 } 133 134 AdviceProto::Checker reports_; 135 }; 136 137 } // namespace tfprof 138 } // namespace tensorflow 139 140 #endif // TENSORFLOW_CORE_PROFILER_INTERNAL_ADVISOR_EXPENSIVE_OPERATION_CHECKER_H_ 141