• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 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_KERNEL_GRAPH_KERNEL_INFO_H_
18 #define MINDSPORE_CCSRC_KERNEL_GRAPH_KERNEL_INFO_H_
19 #include <iostream>
20 #include <vector>
21 #include <memory>
22 #include <map>
23 #include <string>
24 #include <utility>
25 #include "ir/dtype.h"
26 #include "ir/kernel_info_dev.h"
27 #include "kernel/kernel.h"
28 #include "include/backend/visible.h"
29 namespace mindspore {
30 class GraphKernelInfo {
31  public:
32   GraphKernelInfo() = default;
33   virtual ~GraphKernelInfo() = default;
SetKernelInfo(const CNodePtr &,KernelType)34   virtual void SetKernelInfo(const CNodePtr &, KernelType) {}
35 };
36 
37 using GraphKernelInfoCreator = std::function<std::shared_ptr<GraphKernelInfo>()>;
38 
39 class BACKEND_EXPORT GraphKernelInfoManager {
40  public:
Instance()41   static GraphKernelInfoManager &Instance() {
42     static GraphKernelInfoManager instance{};
43     return instance;
44   }
Register(const std::string & device_type,GraphKernelInfoCreator && creator)45   void Register(const std::string &device_type, GraphKernelInfoCreator &&creator) {
46     if (base_map_.find(device_type) == base_map_.end()) {
47       (void)base_map_.emplace(device_type, creator);
48     }
49   }
Clear()50   void Clear() { base_map_.clear(); }
GetGraphKernelInfo(const std::string & device_type)51   std::shared_ptr<GraphKernelInfo> GetGraphKernelInfo(const std::string &device_type) {
52     auto iter = base_map_.find(device_type);
53     if (base_map_.end() != iter) {
54       MS_EXCEPTION_IF_NULL(iter->second);
55       return (iter->second)();
56     }
57     MS_LOG(WARNING) << "Can not get a graph kernel info ptr on device: " << device_type;
58     return nullptr;
59   }
60 
61  private:
62   std::map<std::string, GraphKernelInfoCreator> base_map_;
63 };
64 
65 class GraphKernelInfoRegister {
66  public:
GraphKernelInfoRegister(const std::string & device_type,GraphKernelInfoCreator && creator)67   GraphKernelInfoRegister(const std::string &device_type, GraphKernelInfoCreator &&creator) {
68     GraphKernelInfoManager::Instance().Register(device_type, std::move(creator));
69   }
70   ~GraphKernelInfoRegister() = default;
71 };
72 
73 #define REG_GRAPH_KERNEL_INFO(DEVICE_TYPE, KERNEL_CLASS)                           \
74   static const GraphKernelInfoRegister g_graph_kernel_info_##DEVICE_TYPE##_##_reg( \
75     DEVICE_TYPE, []() { return std::make_shared<KERNEL_CLASS>(); });
76 }  // namespace mindspore
77 #endif  // MINDSPORE_CCSRC_KERNEL_GRAPH_KERNEL_INFO_H_
78