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.0 kB

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

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