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