• 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 #include "tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.h"
16 
17 #include <string>
18 #include <unordered_map>
19 
20 #include "tensorflow/core/platform/logging.h"
21 
22 namespace tensorflow {
23 namespace grappler {
24 
25 namespace {
26 typedef std::unordered_map<string, CustomGraphOptimizerRegistry::Creator>
27     RegistrationMap;
28 RegistrationMap* registered_optimizers = nullptr;
GetRegistrationMap()29 RegistrationMap* GetRegistrationMap() {
30   if (registered_optimizers == nullptr)
31     registered_optimizers = new RegistrationMap;
32   return registered_optimizers;
33 }
34 }  // namespace
35 
36 std::unique_ptr<CustomGraphOptimizer>
CreateByNameOrNull(const string & name)37 CustomGraphOptimizerRegistry::CreateByNameOrNull(const string& name) {
38   const auto it = GetRegistrationMap()->find(name);
39   if (it == GetRegistrationMap()->end()) return nullptr;
40   return std::unique_ptr<CustomGraphOptimizer>(it->second());
41 }
42 
GetRegisteredOptimizers()43 std::vector<string> CustomGraphOptimizerRegistry::GetRegisteredOptimizers() {
44   std::vector<string> optimizer_names;
45   optimizer_names.reserve(GetRegistrationMap()->size());
46   for (const auto& opt : *GetRegistrationMap())
47     optimizer_names.emplace_back(opt.first);
48   return optimizer_names;
49 }
50 
RegisterOptimizerOrDie(const Creator & optimizer_creator,const string & name)51 void CustomGraphOptimizerRegistry::RegisterOptimizerOrDie(
52     const Creator& optimizer_creator, const string& name) {
53   const auto it = GetRegistrationMap()->find(name);
54   if (it != GetRegistrationMap()->end()) {
55     LOG(FATAL) << "CustomGraphOptimizer is registered twice: " << name;
56   }
57   GetRegistrationMap()->insert({name, optimizer_creator});
58 }
59 
60 }  // end namespace grappler
61 }  // end namespace tensorflow
62