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_LAYOUT_OPTIMIZER_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_LAYOUT_OPTIMIZER_H_ 18 19 #include "tensorflow/core/grappler/costs/graph_properties.h" 20 #include "tensorflow/core/grappler/costs/virtual_placer.h" 21 #include "tensorflow/core/grappler/optimizers/graph_optimizer.h" 22 23 namespace tensorflow { 24 namespace grappler { 25 // Convert the NHWC layout to NCHW for Conv-related ops on GPUs. 26 class LayoutOptimizer : public GraphOptimizer { 27 public: LayoutOptimizer()28 LayoutOptimizer() {} ~LayoutOptimizer()29 ~LayoutOptimizer() override {} 30 name()31 string name() const override { return "layout"; }; 32 33 struct TuningConfig { 34 // If true, do not use the NHWC GEMM implementation. When filter size is 35 // one or filter size is equal to input image size, 36 // the NHWC implementation of Conv2D, Conv2DBackpropInput, and 37 // Conv2DBackpropFilter will use a specialized GEMM implementation, which is 38 // usually faster than the NCHW implementation. The downside is that this 39 // might result in more non-cancellable layout conversion nodes (implemented 40 // by the Transpose op). 41 bool no_gemm; 42 }; 43 44 Status Optimize(Cluster* cluster, const GrapplerItem& item, 45 GraphDef* output) override; 46 47 void Feedback(Cluster* cluster, const GrapplerItem& item, 48 const GraphDef& optimize_output, double result) override; 49 50 private: 51 std::unique_ptr<VirtualPlacer> virtual_placer_; 52 std::unordered_set<string> nodes_to_preserve_; 53 Status Tune(const GrapplerItem& item, const GraphProperties& graph_properties, 54 const TuningConfig& config, GraphDef* output); 55 }; 56 57 } // end namespace grappler 58 } // end namespace tensorflow 59 60 #endif // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_LAYOUT_OPTIMIZER_H_ 61