• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_UTILS_H_
17 #define TENSORFLOW_CORE_GRAPPLER_COSTS_UTILS_H_
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "tensorflow/core/framework/cost_graph.pb.h"
24 #include "tensorflow/core/framework/graph.pb.h"
25 #include "tensorflow/core/framework/node_def.pb.h"
26 #include "tensorflow/core/framework/op_def.pb.h"
27 #include "tensorflow/core/graph/types.h"
28 #include "tensorflow/core/grappler/costs/cost_estimator.h"
29 #include "tensorflow/core/grappler/costs/op_performance_data.pb.h"
30 #include "tensorflow/core/platform/types.h"
31 #include "tensorflow/core/protobuf/config.pb.h"
32 #include "tensorflow/core/protobuf/device_properties.pb.h"
33 
34 namespace tensorflow {
35 namespace grappler {
36 
37 // Returns a vector of InputProperties for 'node'. The vector will contain one
38 // entry for each input of 'node'.
39 // For each node in the graph, the 'name_to_cost' map stores a pointer to the
40 // corresponding cost graph node indexed by node name. The 'name_to_node' maps a
41 // node name to its node definition.
42 std::vector<OpInfo::TensorProperties> FindInputFeatures(
43     const NodeDef& node,
44     const std::unordered_map<string, const CostGraphDef::Node*>& name_to_cost,
45     const std::unordered_map<string, const NodeDef*>& name_to_node);
46 
47 // Returns the size of tensor (unit: bytes). For tensor shape with unknown rank,
48 // it assumes the tensor to be scalar. For any unknown dimension, it assumes
49 // size one.
50 int64 CalculateTensorSize(const OpInfo::TensorProperties& prop);
51 
52 // Returns the size of output at port_num (unit: bytes). A special case is
53 // port_num -1, which is for control dependency and assumed to be 4 bytes.
54 int64 CalculateOutputSize(
55     const std::vector<OpInfo::TensorProperties>& output_properties,
56     int port_num);
57 
58 // Returns the DeviceProperties of the device on which 'node' runs.
59 DeviceProperties GetDeviceInfo(const CostGraphDef::Node& node);
60 DeviceProperties GetDeviceInfo(const string& device_str);
61 
62 // Return a string describing a node given a nodeinfo.
63 string GetOpDescription(const OpInfo& op_info);
64 
65 // Builds the OpInfo for node without filling its device information, given all
66 // nodes in the graph and its input properties.
67 OpInfo BuildOpInfoWithoutDevice(
68     const NodeDef& node,
69     const std::unordered_map<string, const NodeDef*>& name_to_node,
70     const std::vector<OpInfo::TensorProperties>& inputs);
71 
72 // Gather performance data from a cost graph.
73 OpPerformanceList CostGraphToOpPerformanceData(const CostGraphDef& cost_graph,
74                                                const GraphDef& graph);
75 
76 // Simple histogram for profiling Tensor size; histogram uses logarithmic
77 // buckets.
78 class TensorSizeHistogram {
79  public:
TensorSizeHistogram()80   TensorSizeHistogram() : buckets_(kMaxBuckets, 0) {}
81 
82   void Add(const uint64 value);
83   void Merge(const TensorSizeHistogram& src);
Average()84   double Average() const {
85     if (num_elem_ > 0) {
86       return static_cast<double>(sum_elem_) / num_elem_;
87     } else {
88       return 0.0;
89     }
90   }
Min()91   uint64 Min() const { return min_; }
Max()92   uint64 Max() const { return max_; }
NumElem()93   uint64 NumElem() const { return num_elem_; }
SumElem()94   uint64 SumElem() const { return sum_elem_; }
95   string ToString() const;
96 
97  protected:
98   const int Index(const uint64 value) const;
GetBuckets()99   const std::vector<uint64>& GetBuckets() const { return buckets_; }
100 
101  private:
102   const int kMaxBuckets = 64;
103   uint64 num_elem_ = 0;
104   uint64 sum_elem_ = 0;
105   // min_ and max_ are initialized to a very large value and zero, respectively,
106   // so that any value added can replace the initial min_ and max_.
107   uint64 min_ = kuint64max;
108   uint64 max_ = 0;
109   // Buckets are logarithmic:
110   // 0B, 1B, 2-3B, 4-7B, 8-15B, ..., 2^N - 2^(N+1)-1B, ...
111   std::vector<uint64> buckets_;
112 };
113 
114 // Helper functions for aggregating per-device stats into per-device-class
115 // stats.
116 string GetDeviceClassForNonChannelDevice(const string& device_name);
117 string GetDeviceClass(const string& device_name);
118 
119 // Get stats in string format from RunMetadata.
120 string GetStatsStringFromRunMetadata(const RunMetadata& run_metadata,
121                                      bool verbosity);
122 
123 // This method calculates the execution time depending on whether IO can
124 // overlap with computation. It assumes the memory and the compute times have
125 // already been calculated.
126 void CombineCostsAndUpdateExecutionTime(bool compute_memory_overlap,
127                                         Costs* costs);
128 
129 }  // end namespace grappler
130 }  // end namespace tensorflow
131 
132 #endif  // TENSORFLOW_CORE_GRAPPLER_COSTS_UTILS_H_
133