1 /* Copyright 2019 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_GENERIC_LAYOUT_OPTIMIZER_H_
17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_GENERIC_LAYOUT_OPTIMIZER_H_
18 
19 #include "tensorflow/core/grappler/optimizers/graph_optimizer.h"
20 #include "tensorflow/core/protobuf/rewriter_config.pb.h"
21 
22 namespace tensorflow {
23 namespace grappler {
24 
25 // Optimize the data layout for convolutional models.
26 class GenericLayoutOptimizer : public GraphOptimizer {
27  public:
GenericLayoutOptimizer()28   GenericLayoutOptimizer()
29       : GenericLayoutOptimizer(RewriterConfig::DEFAULT,
30                                RewriterConfig::NO_CONVERSION_ON_CPU) {}
GenericLayoutOptimizer(RewriterConfig::Toggle opt_level)31   explicit GenericLayoutOptimizer(RewriterConfig::Toggle opt_level)
32       : GenericLayoutOptimizer(opt_level,
33                                RewriterConfig::NO_CONVERSION_ON_CPU) {}
GenericLayoutOptimizer(RewriterConfig::Toggle opt_level,RewriterConfig::CpuLayout layout_conversion)34   explicit GenericLayoutOptimizer(RewriterConfig::Toggle opt_level,
35                                   RewriterConfig::CpuLayout layout_conversion)
36       : opt_level_(opt_level), cpu_layout_conversion_(layout_conversion) {}
37   ~GenericLayoutOptimizer() override = default;
38 
name()39   string name() const override { return "layout"; };
40 
UsesFunctionLibrary()41   bool UsesFunctionLibrary() const override { return false; }
42 
43   Status Optimize(Cluster* cluster, const GrapplerItem& item,
44                   GraphDef* output) override;
45 
46   void Feedback(Cluster* cluster, const GrapplerItem& item,
47                 const GraphDef& optimize_output, double result) override;
48 
49  private:
50   RewriterConfig::Toggle opt_level_;
51   RewriterConfig::CpuLayout cpu_layout_conversion_;
52 };
53 
54 }  // namespace grappler
55 }  // namespace tensorflow
56 
57 #endif  // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_GENERIC_LAYOUT_OPTIMIZER_H_
58