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_GRAPH_MEMORY_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_COSTS_GRAPH_MEMORY_H_ 18 19 #include "tensorflow/core/framework/node_def.pb.h" 20 #include "tensorflow/core/grappler/clusters/cluster.h" 21 #include "tensorflow/core/grappler/costs/cost_estimator.h" 22 #include "tensorflow/core/grappler/costs/graph_properties.h" 23 #include "tensorflow/core/grappler/grappler_item.h" 24 25 namespace tensorflow { 26 namespace grappler { 27 28 // Infer the worst case memory usage for a given grappler item. 29 class GraphMemory { 30 public: 31 struct LiveTensor { 32 string node; 33 int output_id; 34 size_t memory_used; 35 Costs::Duration allocation_time; 36 Costs::Duration deallocation_time; 37 }; 38 struct MemoryUsage { 39 int64 used_memory; 40 std::vector<LiveTensor> live_tensors; 41 }; 42 GraphMemory(const GrapplerItem & item)43 explicit GraphMemory(const GrapplerItem& item) 44 : item_(item), unknown_usage_({-1, {}}) {} 45 46 Status InferStatically( 47 const std::unordered_map<string, DeviceProperties>& devices); 48 Status InferDynamically(Cluster* cluster); 49 50 // Worst case memory usage in bytes, or -1 if the usage is unknown. If there 51 // are multiple devices, returns the highest per device memory usage. 52 int64 GetWorstCaseMemoryUsage() const; 53 54 // Returns the peak memory usage for the specified device. GetPeakMemoryUsage(const string & device)55 const MemoryUsage& GetPeakMemoryUsage(const string& device) const { 56 auto it = peak_usage_.find(device); 57 if (it == peak_usage_.end()) { 58 return unknown_usage_; 59 } 60 return it->second; 61 } 62 63 private: 64 void InferMemUsageForNodes(const std::vector<const NodeDef*>& nodes, 65 GraphProperties* properties, int64* worst_case, 66 int64* best_case) const; 67 int64 InferMemUsageForNeighbors( 68 const std::vector<OpInfo::TensorProperties>& props) const; 69 70 void InferFromTrace(const StepStats& timeline); 71 72 const GrapplerItem& item_; 73 std::unordered_map<string, int64> worst_case_memory_usage_; 74 std::unordered_map<string, MemoryUsage> peak_usage_; 75 const MemoryUsage unknown_usage_; 76 }; 77 78 } // end namespace grappler 79 } // end namespace tensorflow 80 81 #endif // TENSORFLOW_CORE_GRAPPLER_COSTS_GRAPH_MEMORY_H_ 82