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 #ifndef TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_CUSTOM_GRAPH_OPTIMIZER_REGISTRY_H_ 16 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_CUSTOM_GRAPH_OPTIMIZER_REGISTRY_H_ 17 18 #include <functional> 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include "tensorflow/core/grappler/optimizers/custom_graph_optimizer.h" 24 25 namespace tensorflow { 26 namespace grappler { 27 28 // Contains plugin's configurations for each Grappler optimizer (on/off). 29 // See tensorflow/core/protobuf/rewriter_config.proto for optimizer description. 30 struct ConfigList { ConfigListConfigList31 ConfigList() {} ConfigListConfigList32 ConfigList(bool disable_model_pruning, 33 std::unordered_map<string, RewriterConfig_Toggle> config) 34 : disable_model_pruning(disable_model_pruning), 35 toggle_config(std::move(config)) {} 36 37 bool operator==(const ConfigList& other) const { 38 return (disable_model_pruning == other.disable_model_pruning) && 39 (toggle_config == other.toggle_config); 40 } 41 bool disable_model_pruning; // Don't remove unnecessary ops from the graph. 42 std::unordered_map<string, RewriterConfig_Toggle> toggle_config; 43 }; 44 45 class CustomGraphOptimizerRegistry { 46 public: 47 static std::unique_ptr<CustomGraphOptimizer> CreateByNameOrNull( 48 const string& name); 49 50 static std::vector<string> GetRegisteredOptimizers(); 51 52 typedef std::function<CustomGraphOptimizer*()> Creator; 53 // Register graph optimizer which can be called during program initialization. 54 // This class is not thread-safe. 55 static void RegisterOptimizerOrDie(const Creator& optimizer_creator, 56 const string& name); 57 }; 58 59 class CustomGraphOptimizerRegistrar { 60 public: CustomGraphOptimizerRegistrar(const CustomGraphOptimizerRegistry::Creator & creator,const string & name)61 explicit CustomGraphOptimizerRegistrar( 62 const CustomGraphOptimizerRegistry::Creator& creator, 63 const string& name) { 64 CustomGraphOptimizerRegistry::RegisterOptimizerOrDie(creator, name); 65 } 66 }; 67 68 #define REGISTER_GRAPH_OPTIMIZER_AS(MyCustomGraphOptimizerClass, name) \ 69 namespace { \ 70 static ::tensorflow::grappler::CustomGraphOptimizerRegistrar \ 71 MyCustomGraphOptimizerClass##_registrar( \ 72 []() { return new MyCustomGraphOptimizerClass; }, (name)); \ 73 } // namespace 74 75 #define REGISTER_GRAPH_OPTIMIZER(MyCustomGraphOptimizerClass) \ 76 REGISTER_GRAPH_OPTIMIZER_AS(MyCustomGraphOptimizerClass, \ 77 #MyCustomGraphOptimizerClass) 78 79 // A separate registry to register all plug-in CustomGraphOptimizers. 80 class PluginGraphOptimizerRegistry { 81 public: 82 // Constructs a list of plug-in CustomGraphOptimizers from the global map 83 // `registered_plugin_optimizers`. 84 static std::vector<std::unique_ptr<CustomGraphOptimizer>> CreateOptimizers( 85 const std::set<string>& device_types); 86 87 typedef std::function<CustomGraphOptimizer*()> Creator; 88 89 // Returns plugin's config. If any of the config is turned off, the returned 90 // config will be turned off. 91 static ConfigList GetPluginConfigs(bool use_plugin_optimizers, 92 const std::set<string>& device_types); 93 94 // Registers plugin graph optimizer which can be called during program 95 // initialization. Dies if multiple plugins with the same `device_type` are 96 // registered. This class is not thread-safe. 97 static void RegisterPluginOptimizerOrDie(const Creator& optimizer_creator, 98 const std::string& device_type, 99 ConfigList& configs); 100 101 // Prints plugin's configs if there are some conflicts. 102 static void PrintPluginConfigsIfConflict( 103 const std::set<string>& device_types); 104 105 // Returns true when `plugin_config` conflicts with `user_config`: 106 // - Plugin's `disable_model_pruning` is not equal to `user_config`'s, or 107 // - At least one of plugin's `toggle_config`s is on when it is set to off in 108 // `user_config`'s. 109 static bool IsConfigsConflict(ConfigList& user_config, 110 ConfigList& plugin_config); 111 }; 112 113 } // end namespace grappler 114 } // end namespace tensorflow 115 116 #endif // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_CUSTOM_GRAPH_OPTIMIZER_REGISTRY_H_ 117