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.

task_factory.h 3.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Copyright 2019-2020 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 GE_GE_RUNTIME_TASK_TASK_FACTORY_H_
  17. #define GE_GE_RUNTIME_TASK_TASK_FACTORY_H_
  18. #include <functional>
  19. #include <map>
  20. #include <memory>
  21. #include <unordered_map>
  22. #include "framework/common/ge_inner_error_codes.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "framework/ge_runtime/task_info.h"
  25. namespace ge {
  26. namespace model_runner {
  27. class Task;
  28. class ModelContext;
  29. using TASK_CREATOR_FUN = std::function<std::shared_ptr<Task>(const ModelContext &, std::shared_ptr<TaskInfo>)>;
  30. class TaskFactory {
  31. private:
  32. TaskFactory() {}
  33. ~TaskFactory() {}
  34. void RegisterCreator(const TaskInfoType &type, const TASK_CREATOR_FUN &func) {
  35. if (creator_map_.find(type) != creator_map_.end()) {
  36. GELOGW("Creator type %d already exist", static_cast<int32_t>(type));
  37. }
  38. creator_map_[type] = func;
  39. }
  40. std::map<TaskInfoType, TASK_CREATOR_FUN> creator_map_;
  41. public:
  42. static TaskFactory &GetInstance() {
  43. static TaskFactory instance;
  44. return instance;
  45. }
  46. std::shared_ptr<Task> Create(const ModelContext &model_context, std::shared_ptr<TaskInfo> &task_info) const {
  47. if (task_info == nullptr) {
  48. GELOGE(FAILED, "task_info is null.");
  49. return nullptr;
  50. }
  51. auto iter = creator_map_.find(task_info->type());
  52. if (iter == creator_map_.end()) {
  53. GELOGE(FAILED, "Unknow task type %d", static_cast<int32_t>(task_info->type()));
  54. return nullptr;
  55. }
  56. return iter->second(model_context, task_info);
  57. }
  58. class Register {
  59. public:
  60. Register(const TaskInfoType &type, const TASK_CREATOR_FUN &func) {
  61. GELOGI("regist type %d", static_cast<int32_t>(type));
  62. TaskFactory::GetInstance().RegisterCreator(type, func);
  63. }
  64. ~Register() {}
  65. };
  66. };
  67. #define REGISTER_TASK(type, task_clazz, task_info_clazz) \
  68. TaskFactory::Register g_##task_clazz##_register( \
  69. type, [](const ModelContext &model_context, const std::shared_ptr<TaskInfo> &task_info) -> std::shared_ptr<Task> { \
  70. std::shared_ptr<task_info_clazz> concrete_task_info = std::static_pointer_cast<task_info_clazz>(task_info); \
  71. return std::make_shared<task_clazz>(model_context, concrete_task_info); \
  72. });
  73. } // namespace model_runner
  74. } // namespace ge
  75. #endif // GE_GE_RUNTIME_TASK_TASK_FACTORY_H_

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