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