You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

mds_kernel_factory.h 3.4 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Copyright 2021 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. #ifndef MAIN_GRAPHENGINE_GE_GRAPH_PASSES_MDS_KERNELS_MDS_KERNEL_FACTORY_H_
  17. #define MAIN_GRAPHENGINE_GE_GRAPH_PASSES_MDS_KERNELS_MDS_KERNEL_FACTORY_H_
  18. #include <functional>
  19. #include <map>
  20. #include <memory>
  21. #include <string>
  22. #include "common/ge/ge_util.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "graph/graph.h"
  25. using std::string;
  26. namespace ge {
  27. class DeploySchedulerKernel;
  28. ///
  29. /// @brief DeploySchedulerKernel create factory
  30. ///
  31. class KernelFactory {
  32. public:
  33. // KernelCreator(function), type definition
  34. using KERNEL_CREATOR_FUN = std::function<std::shared_ptr<DeploySchedulerKernel>(void)>;
  35. ///
  36. /// Get singleton instance
  37. ///
  38. static KernelFactory &Instance() {
  39. static KernelFactory instance;
  40. return instance;
  41. }
  42. ///
  43. /// create DeploySchedulerKernel
  44. /// @param [in] op_type operation type
  45. ///
  46. std::shared_ptr<DeploySchedulerKernel> Create(const std::string &op_type) {
  47. std::map<std::string, KERNEL_CREATOR_FUN>::iterator iter = creator_map_.find(op_type);
  48. if (iter != creator_map_.end()) {
  49. return iter->second();
  50. }
  51. return nullptr;
  52. }
  53. // DeploySchedulerKernel registration function to register different types of DeploySchedulerKernel to the factory
  54. class Register {
  55. public:
  56. ///
  57. /// @brief Constructor
  58. /// @param [in] type operation type
  59. /// @param [in| fun DeploySchedulerKernel function of the operation
  60. ///
  61. Register(const string &type, const KERNEL_CREATOR_FUN &fun) {
  62. KernelFactory::Instance().RegisterCreator(type, fun);
  63. }
  64. ~Register() = default;
  65. };
  66. protected:
  67. KernelFactory() = default;
  68. ~KernelFactory() = default;
  69. // register creator, this function will call in the constructor
  70. void RegisterCreator(const string &type, const KERNEL_CREATOR_FUN &fun) {
  71. std::map<std::string, KERNEL_CREATOR_FUN>::iterator iter = creator_map_.find(type);
  72. if (iter != creator_map_.end()) {
  73. GELOGW("KernelFactory::RegisterCreator: %s creator already exist", type.c_str());
  74. return;
  75. }
  76. creator_map_[type] = fun;
  77. }
  78. private:
  79. std::map<std::string, KERNEL_CREATOR_FUN> creator_map_{};
  80. };
  81. #define REGISTER_MDS_KERNEL(type, clazz) \
  82. std::shared_ptr<DeploySchedulerKernel> Creator_##type##_Kernel() { \
  83. std::shared_ptr<clazz> ptr = nullptr; \
  84. ptr = MakeShared<clazz>(); \
  85. return ptr; \
  86. } \
  87. KernelFactory::Register g_##type##_Kernel_Creator(type, Creator_##type##_Kernel)
  88. } // namespace ge
  89. #endif //MAIN_GRAPHENGINE_GE_GRAPH_PASSES_MDS_KERNELS_MDS_KERNEL_FACTORY_H_

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示