1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_PYTHON_GRAPPLER_COST_ANALYZER_H_
17#define TENSORFLOW_PYTHON_GRAPPLER_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/framework/tensor_shape.pb.h"
23#include "tensorflow/core/grappler/clusters/cluster.h"
24#include "tensorflow/core/grappler/costs/analytical_cost_estimator.h"
25#include "tensorflow/core/grappler/costs/cost_estimator.h"
26#include "tensorflow/core/grappler/costs/measuring_cost_estimator.h"
27#include "tensorflow/core/grappler/costs/op_performance_data.pb.h"
28
29namespace tensorflow {
30class GraphDef;
31class CostGraphDef;
32
33namespace grappler {
34struct GrapplerItem;
35
36// Aggregated perf summary for ops of the same type in a graph.
37struct OpPerfSummary {
38 string name;
39 int64_t count;
40 int64_t time;
41 int64_t compute_time;
42 int64_t memory_time;
43 // Upper and lower bound for estimated time.
44 int64_t time_upper;
45 int64_t time_lower;
46};
47
48// Generate op-level performance insights on compute/memory
49// efficiency, as well as graph-level aggregated performance statistics.
50class CostAnalyzer {
51 public:
52 explicit CostAnalyzer(const GrapplerItem& item, Cluster* cluster,
53 const string& suffix);
54 Status GenerateReport(std::ostream& os, bool per_node_report, bool verbose);
55
56 private:
57 void PredictCosts(CostEstimator* cost_estimator, CostGraphDef* cost_graph,
58 int64_t* total_time);
59 void GatherCosts();
60 void PreprocessCosts();
61 void AnalyzeCosts();
62 void SortOpsByTime(std::map<string, OpPerfSummary> ops);
63 void PrintAnalysis(std::ostream& os, bool per_node_report,
64 bool verbose) const;
65
66 const GrapplerItem* item_;
67 MeasuringCostEstimator measure_estimator_;
68 AnalyticalCostEstimator analytical_estimator_;
69 OpPerformanceList op_perf_;
70 OpPerformanceList op_perf_analytical_;
71 int64_t total_time_measured_;
72 int64_t total_time_analytical_;
73 std::vector<OpPerfSummary> ops_;
74 int64_t total_time_measured_serialized_;
75 int64_t total_time_analytical_upper_;
76 int64_t total_time_analytical_lower_;
77 string suffix_;
78};
79
80} // end namespace grappler
81} // end namespace tensorflow
82
83#endif // TENSORFLOW_PYTHON_GRAPPLER_COST_ANALYZER_H_
84