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 16 #ifndef TENSORFLOW_CORE_PROFILER_INTERNAL_ADVISOR_TFPROF_ADVISOR_H_ 17 #define TENSORFLOW_CORE_PROFILER_INTERNAL_ADVISOR_TFPROF_ADVISOR_H_ 18 19 #include "absl/strings/str_format.h" 20 #include "tensorflow/core/profiler/internal/advisor/accelerator_utilization_checker.h" 21 #include "tensorflow/core/profiler/internal/advisor/checker.h" 22 #include "tensorflow/core/profiler/internal/advisor/expensive_operation_checker.h" 23 #include "tensorflow/core/profiler/internal/advisor/internal_checker_runner.h" 24 #include "tensorflow/core/profiler/internal/advisor/operation_checker.h" 25 #include "tensorflow/core/profiler/tfprof_options.pb.h" 26 27 namespace tensorflow { 28 namespace tfprof { 29 30 // The Advisor runs a list of Checkers, each checks a specific area. 31 class Advisor { 32 public: Advisor(const TFStats * stats)33 Advisor(const TFStats* stats) : stats_(stats) {} 34 DefaultOptions()35 static AdvisorOptionsProto DefaultOptions() { 36 AdvisorOptionsProto options; 37 std::vector<string> checkers( 38 kCheckers, kCheckers + sizeof(kCheckers) / sizeof(*kCheckers)); 39 for (const string& checker : checkers) { 40 (*options.mutable_checkers())[checker]; 41 } 42 return options; 43 } 44 Advise(const AdvisorOptionsProto & options)45 AdviceProto Advise(const AdvisorOptionsProto& options) { 46 // Note: Release a checker's memory ASAP. 47 AdviceProto ret = RunInternalCheckers(options, stats_); 48 49 if (options.checkers().find(kCheckers[0]) != options.checkers().end()) { 50 AcceleratorUtilizationChecker au_checker; 51 (*ret.mutable_checkers())[kCheckers[0]].MergeFrom( 52 au_checker.Run(options.checkers().at(kCheckers[0]), stats_)); 53 } 54 if (options.checkers().find(kCheckers[1]) != options.checkers().end()) { 55 OperationChecker op_checker; 56 (*ret.mutable_checkers())[kCheckers[1]].MergeFrom( 57 op_checker.Run(options.checkers().at(kCheckers[1]), stats_)); 58 } 59 if (options.checkers().find(kCheckers[2]) != options.checkers().end()) { 60 ExpensiveOperationChecker expensive_op_checker; 61 (*ret.mutable_checkers())[kCheckers[2]].MergeFrom( 62 expensive_op_checker.Run(options.checkers().at(kCheckers[2]), 63 stats_)); 64 } 65 for (const auto& checker : ret.checkers()) { 66 absl::FPrintF(stdout, "\n%s:\n", checker.first); 67 for (const string& r : checker.second.reports()) { 68 absl::FPrintF(stdout, "%s\n", r); 69 } 70 } 71 fflush(stdout); 72 return ret; 73 } 74 75 private: 76 const TFStats* stats_; 77 }; 78 79 } // namespace tfprof 80 } // namespace tensorflow 81 82 #endif // TENSORFLOW_CORE_PROFILER_INTERNAL_ADVISOR_TFPROF_ADVISOR_H_ 83