• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 class CustomGraphOptimizerRegistry {
29  public:
30   static std::unique_ptr<CustomGraphOptimizer> CreateByNameOrNull(
31       const string& name);
32 
33   static std::vector<string> GetRegisteredOptimizers();
34 
35   typedef std::function<CustomGraphOptimizer*()> Creator;
36   // Register graph optimizer which can be called during program initialization.
37   // This class is not thread-safe.
38   static void RegisterOptimizerOrDie(const Creator& optimizer_creator,
39                                      const string& name);
40 };
41 
42 class CustomGraphOptimizerRegistrar {
43  public:
CustomGraphOptimizerRegistrar(const CustomGraphOptimizerRegistry::Creator & creator,const string & name)44   explicit CustomGraphOptimizerRegistrar(
45       const CustomGraphOptimizerRegistry::Creator& creator,
46       const string& name) {
47     CustomGraphOptimizerRegistry::RegisterOptimizerOrDie(creator, name);
48   }
49 };
50 
51 #define REGISTER_GRAPH_OPTIMIZER_AS(MyCustomGraphOptimizerClass, name) \
52   namespace {                                                          \
53   static ::tensorflow::grappler::CustomGraphOptimizerRegistrar         \
54       MyCustomGraphOptimizerClass##_registrar(                         \
55           []() { return new MyCustomGraphOptimizerClass; }, (name));   \
56   }  // namespace
57 
58 #define REGISTER_GRAPH_OPTIMIZER(MyCustomGraphOptimizerClass) \
59   REGISTER_GRAPH_OPTIMIZER_AS(MyCustomGraphOptimizerClass,    \
60                               #MyCustomGraphOptimizerClass)
61 
62 }  // end namespace grappler
63 }  // end namespace tensorflow
64 
65 #endif  // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_CUSTOM_GRAPH_OPTIMIZER_REGISTRY_H_
66