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.

parser_factory.h 5.2 kB

4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
  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 INC_FRAMEWORK_OMG_PARSER_PARSER_FACTORY_H_
  17. #define INC_FRAMEWORK_OMG_PARSER_PARSER_FACTORY_H_
  18. #include <map>
  19. #include <memory>
  20. #include <mutex>
  21. #include <string>
  22. #include "framework/omg/omg_inner_types.h"
  23. #include "framework/omg/parser/parser_types.h"
  24. #include "external/register/register.h"
  25. namespace domi {
  26. class WeightsParser;
  27. class ModelParser;
  28. using MODEL_PARSER_CREATOR_FUN = std::shared_ptr<ModelParser> (*)(void);
  29. // Create modelparser for different frameworks
  30. class GE_FUNC_VISIBILITY ModelParserFactory {
  31. public:
  32. static ModelParserFactory *Instance();
  33. /**
  34. * @ingroup domi_omg
  35. * @brief Create a modelparser based on the type entered
  36. * @param [in] type Framework type
  37. * @return Created modelparser
  38. */
  39. std::shared_ptr<ModelParser> CreateModelParser(const domi::FrameworkType type);
  40. /**
  41. * @ingroup domi_omg
  42. * @brief Register create function
  43. * @param [in] type Framework type
  44. * @param [in] fun ModelParser's create function
  45. */
  46. void RegisterCreator(const domi::FrameworkType type, MODEL_PARSER_CREATOR_FUN fun);
  47. protected:
  48. ModelParserFactory() {}
  49. ~ModelParserFactory();
  50. private:
  51. std::map<domi::FrameworkType, MODEL_PARSER_CREATOR_FUN> creator_map_;
  52. }; // end class ModelParserFactory
  53. class GE_FUNC_VISIBILITY ModelParserRegisterar {
  54. public:
  55. ModelParserRegisterar(const domi::FrameworkType type, MODEL_PARSER_CREATOR_FUN const fun) {
  56. ModelParserFactory::Instance()->RegisterCreator(type, fun);
  57. }
  58. ~ModelParserRegisterar() {}
  59. };
  60. // Registration macros for model parsers
  61. #define REGISTER_MODEL_PARSER_CREATOR(type, clazz) \
  62. std::shared_ptr<ModelParser> Creator_##type##_Model_Parser() { \
  63. std::shared_ptr<clazz> ptr = nullptr; \
  64. try { \
  65. ptr = make_shared<clazz>(); \
  66. } catch (...) { \
  67. ptr = nullptr; \
  68. } \
  69. return std::shared_ptr<ModelParser>(ptr); \
  70. } \
  71. ModelParserRegisterar g_##type##_Model_Parser_Creator(type, Creator_##type##_Model_Parser)
  72. using WEIGHTS_PARSER_CREATOR_FUN = std::shared_ptr<WeightsParser> (*)(void);
  73. // Create weightsparser for different frameworks
  74. class GE_FUNC_VISIBILITY WeightsParserFactory {
  75. public:
  76. static WeightsParserFactory *Instance();
  77. /**
  78. * @ingroup domi_omg
  79. * @brief Create weightsparser based on the type entered
  80. * @param [in] type Framework type
  81. * @return Created weightsparser
  82. */
  83. std::shared_ptr<WeightsParser> CreateWeightsParser(const domi::FrameworkType type);
  84. /**
  85. * @ingroup domi_omg
  86. * @brief Register create function
  87. * @param [in] type Framework type
  88. * @param [in] fun WeightsParser's create function
  89. */
  90. void RegisterCreator(const domi::FrameworkType type, WEIGHTS_PARSER_CREATOR_FUN fun);
  91. protected:
  92. WeightsParserFactory() {}
  93. ~WeightsParserFactory();
  94. private:
  95. std::map<domi::FrameworkType, WEIGHTS_PARSER_CREATOR_FUN> creator_map_;
  96. }; // end class WeightsParserFactory
  97. class GE_FUNC_VISIBILITY WeightsParserRegisterar {
  98. public:
  99. WeightsParserRegisterar(const domi::FrameworkType type, WEIGHTS_PARSER_CREATOR_FUN const fun) {
  100. WeightsParserFactory::Instance()->RegisterCreator(type, fun);
  101. }
  102. ~WeightsParserRegisterar() {}
  103. };
  104. // Register macro of weight resolver
  105. #define REGISTER_WEIGHTS_PARSER_CREATOR(type, clazz) \
  106. std::shared_ptr<WeightsParser> Creator_##type##_Weights_Parser() { \
  107. std::shared_ptr<clazz> ptr = nullptr; \
  108. try { \
  109. ptr = make_shared<clazz>(); \
  110. } catch (...) { \
  111. ptr = nullptr; \
  112. } \
  113. return std::shared_ptr<WeightsParser>(ptr); \
  114. } \
  115. WeightsParserRegisterar g_##type##_Weights_Parser_Creator(type, Creator_##type##_Weights_Parser)
  116. class GE_FUNC_VISIBILITY OpRegTbeParserFactory {
  117. public:
  118. static OpRegTbeParserFactory *Instance();
  119. void Finalize(const domi::OpRegistrationData &reg_data);
  120. };
  121. } // namespace domi
  122. #endif // INC_FRAMEWORK_OMG_PARSER_PARSER_FACTORY_H_

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