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