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_AUTO_MIXED_PRECISION_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_AUTO_MIXED_PRECISION_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 enum class AutoMixedPrecisionMode { CUDA, MKL }; 26 27 // Convert data types to float16 or bfloat16 where appropriate to improve 28 // performance on GPUs or CPUs. 29 class AutoMixedPrecision : public GraphOptimizer { 30 public: 31 // If 'mode' is CUDA, converts nodes to float16 on Nvidia GPUs. If MKL, 32 // converts nodes to bfloat16 on CPUs in order to take advantage of MKL 33 // performance improvements with bfloat16. 34 explicit AutoMixedPrecision( 35 AutoMixedPrecisionMode mode = AutoMixedPrecisionMode::CUDA) mode_(mode)36 : mode_(mode) {} 37 ~AutoMixedPrecision()38 ~AutoMixedPrecision() override {} 39 name()40 string name() const override { 41 return mode_ == AutoMixedPrecisionMode::CUDA ? "auto_mixed_precision_cuda" 42 : "auto_mixed_precision_mkl"; 43 }; 44 UsesFunctionLibrary()45 bool UsesFunctionLibrary() const override { return false; } 46 47 Status Optimize(Cluster* cluster, const GrapplerItem& item, 48 GraphDef* output) override; 49 50 void Feedback(Cluster* cluster, const GrapplerItem& item, 51 const GraphDef& optimize_output, double result) override; 52 53 private: 54 const AutoMixedPrecisionMode mode_; 55 }; 56 57 } // end namespace grappler 58 } // end namespace tensorflow 59 60 #endif // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_AUTO_MIXED_PRECISION_H_ 61