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_OPTIMIZERS_MEMORY_OPTIMIZER_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_MEMORY_OPTIMIZER_H_ 18 19 #include <string> 20 #include "tensorflow/core/grappler/clusters/cluster.h" 21 #include "tensorflow/core/grappler/grappler_item.h" 22 #include "tensorflow/core/grappler/optimizers/graph_optimizer.h" 23 #include "tensorflow/core/protobuf/rewriter_config.pb.h" 24 25 namespace tensorflow { 26 namespace grappler { 27 28 // Swap tensors in and out of device memory. 29 class MemoryOptimizer : public GraphOptimizer { 30 public: 31 // optimization_level: Controls the level of autonomy for the memory 32 // optimizer. See RewriterConfig::memory_optimization. 33 // recomputation_targets_name_scope: Name scope for potential outputs of 34 // recomputations. See 35 // RewriterConfig::memory_optimizer_target_node_name_scope. 36 explicit MemoryOptimizer( 37 RewriterConfig::MemOptType optimization_level, 38 const string& recomputation_targets_name_scope = "gradients/") optimization_level_(optimization_level)39 : optimization_level_(optimization_level), 40 recomputation_targets_name_scope_(recomputation_targets_name_scope) {} ~MemoryOptimizer()41 ~MemoryOptimizer() override {} 42 name()43 string name() const override { return "memory_optimizer"; }; 44 UsesFunctionLibrary()45 bool UsesFunctionLibrary() const override { return false; } 46 47 Status Optimize(Cluster* cluster, const GrapplerItem& item, 48 GraphDef* pruned_graph) override; 49 50 private: 51 RewriterConfig::MemOptType optimization_level_; 52 string recomputation_targets_name_scope_; 53 }; 54 55 } // end namespace grappler 56 } // end namespace tensorflow 57 58 #endif // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_MEMORY_OPTIMIZER_H_ 59