Home | History | Annotate | Download | only in grappler
      1 /* Copyright 2017 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_GRAPPLER_COSTS_COST_ANALYZER_H_
     17 #define TENSORFLOW_CORE_GRAPPLER_COSTS_COST_ANALYZER_H_
     18 
     19 #include <iostream>
     20 #include "tensorflow/core/framework/cost_graph.pb.h"
     21 #include "tensorflow/core/framework/graph.pb.h"
     22 #include "tensorflow/core/grappler/clusters/cluster.h"
     23 #include "tensorflow/core/grappler/costs/analytical_cost_estimator.h"
     24 #include "tensorflow/core/grappler/costs/cost_estimator.h"
     25 #include "tensorflow/core/grappler/costs/measuring_cost_estimator.h"
     26 #include "tensorflow/core/grappler/costs/op_performance_data.pb.h"
     27 
     28 namespace tensorflow {
     29 class GraphDef;
     30 class CostGraphDef;
     31 
     32 namespace grappler {
     33 struct GrapplerItem;
     34 
     35 // Aggregated perf summary for ops of the same type in a graph.
     36 struct OpPerfSummary {
     37   string name;
     38   int64 count;
     39   int64 time;
     40   int64 compute_time;
     41   int64 memory_time;
     42   // Upper and lower bound for estimated time.
     43   int64 time_upper;
     44   int64 time_lower;
     45 };
     46 
     47 // Generate op-level performance insights on compute/memory
     48 // efficiency, as well as graph-level aggregated performance statistics.
     49 class CostAnalyzer {
     50  public:
     51   explicit CostAnalyzer(const GrapplerItem& item, Cluster* cluster,
     52                         const string& suffix);
     53   Status GenerateReport(std::ostream& os, bool per_node_report);
     54 
     55  private:
     56   void PredictCosts(CostEstimator* cost_estimator, CostGraphDef* cost_graph,
     57                     int64* total_time);
     58   void GatherCosts();
     59   void PreprocessCosts();
     60   void AnalyzeCosts();
     61   void SortOpsByTime(std::map<string, OpPerfSummary> ops);
     62   void PrintAnalysis(std::ostream& os, bool per_node_report) const;
     63 
     64   const GrapplerItem* item_;
     65   MeasuringCostEstimator measure_estimator_;
     66   AnalyticalCostEstimator analytical_estimator_;
     67   OpPerformanceList op_perf_;
     68   OpPerformanceList op_perf_analytical_;
     69   int64 total_time_measured_;
     70   int64 total_time_analytical_;
     71   std::vector<OpPerfSummary> ops_;
     72   int64 total_time_measured_serialized_;
     73   int64 total_time_analytical_upper_;
     74   int64 total_time_analytical_lower_;
     75   string suffix_;
     76 };
     77 
     78 }  // end namespace grappler
     79 }  // end namespace tensorflow
     80 
     81 #endif  // TENSORFLOW_CORE_GRAPPLER_COSTS_COST_ANALYZER_H_
     82