• 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_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 
45   Status Optimize(Cluster* cluster, const GrapplerItem& item,
46                   GraphDef* pruned_graph) override;
47 
48   void Feedback(Cluster* cluster, const GrapplerItem& item,
49                 const GraphDef& pruned_graph, double result) override;
50 
51  private:
52   RewriterConfig::MemOptType optimization_level_;
53   string recomputation_targets_name_scope_;
54 };
55 
56 }  // end namespace grappler
57 }  // end namespace tensorflow
58 
59 #endif  // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_MEMORY_OPTIMIZER_H_
60