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.

davinci_model_parser.cc 3.6 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Copyright 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. #include "graph/load/new_model_manager/davinci_model_parser.h"
  17. #include <fstream>
  18. #include <memory>
  19. #include <vector>
  20. #include "securec.h"
  21. #include "common/debug/log.h"
  22. #include "graph/load/new_model_manager/davinci_model.h"
  23. namespace ge {
  24. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY Status ModelInfoParser(const ModelData &model, ModelInfo &model_info) {
  25. GE_CHK_RT_RET(rtSetDevice(0));
  26. try {
  27. uint32_t model_len = 0;
  28. uint8_t *model_data = nullptr;
  29. Status ret = DavinciModelParser::ParseModelContent(model, model_data, model_len);
  30. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, GE_CHK_RT(rtDeviceReset(0)); return ret, "Parse model failed");
  31. auto *file_header = reinterpret_cast<ModelFileHeader *>(model.model_data);
  32. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(file_header == nullptr, GE_CHK_RT(rtDeviceReset(0));
  33. return PARAM_INVALID, "file_header is null.");
  34. model_info.version = file_header->version;
  35. model_info.is_encrypt = false;
  36. GE_IF_BOOL_EXEC(ENCRYPTED == file_header->is_encrypt, model_info.is_encrypt = true);
  37. std::shared_ptr<DavinciModel> davinci_model =
  38. std::shared_ptr<DavinciModel>(new (std::nothrow) DavinciModel(model.priority, nullptr));
  39. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(davinci_model == nullptr, GE_CHK_RT(rtDeviceReset(0));
  40. return PARAM_INVALID, "davinci_model is null.");
  41. GE_MAKE_GUARD(davinci_model, [&] { davinci_model = nullptr; });
  42. ModelHelper model_helper;
  43. ret = model_helper.LoadModel(model);
  44. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG((ret != SUCCESS), GE_CHK_RT(rtDeviceReset(0)); return FAILED, "load model failed");
  45. ret = davinci_model->Assign(model_helper.GetGeModel());
  46. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, GE_CHK_RT(rtDeviceReset(0));
  47. return ret, "Parse davinci model data failed");
  48. ret = davinci_model->Init();
  49. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, GE_CHK_RT(rtDeviceReset(0));
  50. return ret, "Davinci model init failed");
  51. vector<InputOutputDescInfo> input_list;
  52. vector<InputOutputDescInfo> output_list;
  53. ret = davinci_model->GetInputOutputDescInfo(input_list, output_list);
  54. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ret != SUCCESS, GE_CHK_RT(rtDeviceReset(0));
  55. return ret, "Davinci model GetInputOutputDescInfo failed");
  56. for (const auto &desc : input_list) {
  57. model_info.input_desc.push_back(desc.shape_info);
  58. }
  59. for (const auto &desc : output_list) {
  60. model_info.output_desc.push_back(desc.shape_info);
  61. }
  62. model_info.name = davinci_model->Name();
  63. } catch (...) {
  64. DOMI_LOGE("OM model parser failed, some exceptions occur !");
  65. GE_CHK_RT(rtDeviceReset(0));
  66. return FAILED;
  67. }
  68. GE_CHK_RT(rtDeviceReset(0));
  69. return SUCCESS;
  70. }
  71. DavinciModelParser::DavinciModelParser() {}
  72. DavinciModelParser::~DavinciModelParser() {}
  73. } // namespace ge

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