1 /** 2 * Copyright 2023 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_GRAPH_KERNEL_BUILDER_MANAGER_H_ 18 #define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_GRAPH_KERNEL_BUILDER_MANAGER_H_ 19 #include "kernel/graph_kernel/graph_kernel_builder.h" 20 #include <map> 21 #include <utility> 22 #include <memory> 23 #include <string> 24 #include "include/backend/visible.h" 25 26 namespace mindspore { 27 namespace kernel { 28 using GraphKernelBuildCreator = std::function<std::shared_ptr<GraphKernelBuilder>()>; 29 30 class BACKEND_EXPORT GraphKernelBuildManager { 31 public: 32 static GraphKernelBuildManager &Instance(); 33 void Register(const std::string &device_type, bool is_dynamic, GraphKernelBuildCreator &&creator); Clear()34 void Clear() { base_map_.clear(); } 35 std::shared_ptr<GraphKernelBuilder> GetGraphKernelBuilder(const std::string &device_type, bool is_dynamic); 36 37 private: 38 std::map<std::pair<std::string, bool>, GraphKernelBuildCreator> base_map_; 39 }; 40 41 class GraphKernelBuildRegister { 42 public: GraphKernelBuildRegister(const std::string & device_type,bool is_dynamic,GraphKernelBuildCreator && creator)43 GraphKernelBuildRegister(const std::string &device_type, bool is_dynamic, GraphKernelBuildCreator &&creator) { 44 GraphKernelBuildManager::Instance().Register(device_type, is_dynamic, std::move(creator)); 45 } 46 ~GraphKernelBuildRegister() = default; 47 }; 48 49 #define REG_GRAPH_KERNEL_BUILDER(DEVICE_TYPE, IS_DYNAMIC, BUILDER_CLASS) \ 50 static const GraphKernelBuildRegister g_graph_kernel_builder_##DEVICE_TYPE##_##IS_DYNAMIC##_reg( \ 51 DEVICE_TYPE, IS_DYNAMIC, []() { return std::make_shared<BUILDER_CLASS>(); }); 52 } // namespace kernel 53 } // namespace mindspore 54 55 #endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_AKG_AKG_KERNEL_BUILD_MANAGER_H_ 56