1 /* Copyright 2018 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_DATA_VECTORIZATION_VECTORIZER_REGISTRY_H_ 17 #define TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DATA_VECTORIZATION_VECTORIZER_REGISTRY_H_ 18 19 #include <functional> 20 #include <map> 21 22 #include "absl/container/flat_hash_map.h" 23 #include "tensorflow/core/grappler/optimizers/data/vectorization/vectorizer.h" 24 25 namespace tensorflow { 26 namespace grappler { 27 28 // A global VectorizerRegistry is used to hold all the vectorizers. 29 class VectorizerRegistry { 30 public: 31 // Returns a pointer to a global VectorizerRegistry object. 32 static VectorizerRegistry* Global(); 33 34 // Returns a pointer to a vectorizer that can vectorize an op for the op type. 35 Vectorizer* Get(const string& op_type); 36 37 // Registers a vectorizer that can vectorize an op for the given op type. 38 void Register(const string& op_type, std::unique_ptr<Vectorizer> vectorizer); 39 40 private: 41 absl::flat_hash_map<string, std::unique_ptr<Vectorizer>> vectorizers_; 42 }; 43 44 namespace vectorizer_registration { 45 46 class VectorizerRegistration { 47 public: VectorizerRegistration(const string & op_type,std::unique_ptr<Vectorizer> vectorizer)48 VectorizerRegistration(const string& op_type, 49 std::unique_ptr<Vectorizer> vectorizer) { 50 VectorizerRegistry::Global()->Register(op_type, std::move(vectorizer)); 51 } 52 }; 53 54 } // namespace vectorizer_registration 55 56 #define REGISTER_VECTORIZER(op_type, vectorizer) \ 57 REGISTER_VECTORIZER_UNIQ_HELPER(__COUNTER__, op_type, vectorizer) 58 59 #define REGISTER_VECTORIZER_UNIQ_HELPER(ctr, op_type, vectorizer) \ 60 REGISTER_VECTORIZER_UNIQ(ctr, op_type, vectorizer) 61 62 #define REGISTER_VECTORIZER_UNIQ(ctr, op_type, vectorizer) \ 63 static ::tensorflow::grappler::vectorizer_registration:: \ 64 VectorizerRegistration vectorizer_registration_##ctr( \ 65 op_type, ::std::unique_ptr<::tensorflow::grappler::Vectorizer>( \ 66 new vectorizer())) 67 68 } // namespace grappler 69 } // namespace tensorflow 70 71 #endif // TENSORFLOW_CORE_GRAPPLER_OPTIMIZERS_DATA_VECTORIZATION_VECTORIZER_REGISTRY_H_ 72