1 /* Copyright 2019 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_GENERIC_LAYOUT_OPTIMIZER_TRANSPOSER_FACTORY_H_
17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_GENERIC_LAYOUT_OPTIMIZER_TRANSPOSER_FACTORY_H_
18 
19 #include <memory>
20 
21 #include "absl/container/flat_hash_map.h"
22 #include "tensorflow/core/grappler/optimizers/generic_layout_optimizer_transposer.h"
23 
24 namespace tensorflow {
25 namespace grappler {
26 
27 class TransposerFactory {
28  public:
TransposerFactory()29   explicit TransposerFactory() {}
30 
31   std::shared_ptr<Transposer> GetTransposer(const NodeDef& node);
32 
33  protected:
34   template <typename T>
GetOrCreateIfNotFound(const string & key)35   std::shared_ptr<Transposer> GetOrCreateIfNotFound(const string& key) {
36     auto& transposer = transposer_map_[key];
37     if (transposer == nullptr) {
38       transposer = std::make_shared<T>();
39     }
40     return transposer;
41   }
42 
43   absl::flat_hash_map<string, std::shared_ptr<Transposer>> transposer_map_;
44 };
45 
46 }  // end namespace grappler
47 }  // end namespace tensorflow
48 
49 #endif  // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_GENERIC_LAYOUT_OPTIMIZER_TRANSPOSER_FACTORY_H_
50