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_GRAPPLER_COSTS_UTILS_H_ 17 #define TENSORFLOW_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 DeviceProperties of the device on which 'node' runs. 47 DeviceProperties GetDeviceInfo(const CostGraphDef::Node& node); 48 DeviceProperties GetDeviceInfo(const string& device_str); 49 50 // Return a string describing a node given a nodeinfo. 51 string GetOpDescription(const OpInfo& op_info); 52 53 // Builds the OpInfo for node without filling its device information, given all 54 // nodes in the graph and its input properties. 55 OpInfo BuildOpInfoWithoutDevice( 56 const NodeDef& node, 57 const std::unordered_map<string, const NodeDef*>& name_to_node, 58 const std::vector<OpInfo::TensorProperties>& inputs); 59 60 // Gather performance data from a cost graph. 61 OpPerformanceList CostGraphToOpPerformanceData(const CostGraphDef& cost_graph, 62 const GraphDef& graph); 63 64 // Simple histogram for profiling Tensor size; histogram uses logarithmic 65 // buckets. 66 class TensorSizeHistogram { 67 public: 68 TensorSizeHistogram() : buckets_(kMaxBuckets, 0) {} 69 70 void Add(const uint64 value); 71 void Merge(const TensorSizeHistogram& src); 72 double Average() const { 73 if (num_elem_ > 0) { 74 return static_cast<double>(sum_elem_) / num_elem_; 75 } else { 76 return 0.0; 77 } 78 } 79 uint64 Min() const { return min_; } 80 uint64 Max() const { return max_; } 81 uint64 NumElem() const { return num_elem_; } 82 uint64 SumElem() const { return sum_elem_; } 83 std::string ToString() const; 84 85 protected: 86 const int Index(const uint64 value) const; 87 const std::vector<uint64>& GetBuckets() const { return buckets_; } 88 89 private: 90 const int kMaxBuckets = 64; 91 uint64 num_elem_ = 0; 92 uint64 sum_elem_ = 0; 93 // min_ and max_ are initialized to a very large value and zero, respectively, 94 // so that any value added can replace the initial min_ and max_. 95 uint64 min_ = kuint64max; 96 uint64 max_ = 0; 97 // Buckets are logarithmic: 98 // 0B, 1B, 2-3B, 4-7B, 8-15B, ..., 2^N - 2^(N+1)-1B, ... 99 std::vector<uint64> buckets_; 100 }; 101 102 // Helper functions for aggregating per-device stats into per-device-class 103 // stats. 104 string GetDeviceClassForNonChannelDevice(const string& device_name); 105 string GetDeviceClass(const string& device_name); 106 107 // Get stats in string format from RunMetadata. 108 string GetStatsStringFromRunMetadata(const RunMetadata& run_metadata, 109 bool verbosity); 110 111 } // end namespace grappler 112 } // end namespace tensorflow 113 114 #endif // TENSORFLOW_GRAPPLER_COSTS_UTILS_H_ 115