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.

caffe_data_parser.cc 7.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "parser/caffe/caffe_data_parser.h"
  17. #include <unordered_map>
  18. #include <utility>
  19. #include "common/debug/log.h"
  20. #include "common/types.h"
  21. #include "common/util.h"
  22. #include "common/util/error_manager/error_manager.h"
  23. #include "framework/common/debug/ge_log.h"
  24. #include "omg/omg_inner_types.h"
  25. #include "parser/common/op_parser_factory.h"
  26. namespace ge {
  27. Status CaffeDataParser::GetOutputDesc(const string &name, int dim_size, const std::vector<int64_t> &input_dims,
  28. ge::OpDescPtr &op) {
  29. GE_CHECK_NOTNULL(op);
  30. GELOGI("The input dim size is %zu in layer %s.", input_dims.size(), name.c_str());
  31. // Caffe default data type is float32
  32. GE_IF_BOOL_EXEC(!(ge::AttrUtils::SetInt(op, DATA_ATTR_NAME_DATA_TYPE, ge::DT_FLOAT)),
  33. GELOGW("SetInt failed for op %s.", op->GetName().c_str());); // no need to return
  34. // Initialize input and output description of OP according to input_dims information
  35. GE_RETURN_WITH_LOG_IF_ERROR(ParseShape(input_dims, op), "data layer %s ParseShape failed", name.c_str());
  36. return SUCCESS;
  37. }
  38. Status CaffeDataParser::ParseParams(const Message *op_src, ge::OpDescPtr &op) {
  39. GE_CHECK_NOTNULL(op_src);
  40. GE_CHECK_NOTNULL(op);
  41. const domi::caffe::LayerParameter *layer = DOMI_DYNAMIC_CAST<const domi::caffe::LayerParameter *>(op_src);
  42. GE_CHECK_NOTNULL(layer);
  43. GELOGD("Caffe layer name = %s, layer type= %s, parse params", layer->name().c_str(), layer->type().c_str());
  44. if (layer->type() == ge::INPUT_TYPE) {
  45. GE_CHK_STATUS_RET(ParseParamsForInput(layer, op), "Caffe layer name = %s, layer type= %s, parse params failed",
  46. layer->name().c_str(), layer->type().c_str());
  47. } else if(layer->type() == ge::DUMMY_DATA) {
  48. GE_CHK_STATUS_RET(ParseParamsForDummyData(layer, op), "Caffe layer name = %s, layer type= %s, parse params failed",
  49. layer->name().c_str(), layer->type().c_str());
  50. } else {
  51. ErrorManager::GetInstance().ATCReportErrMessage("E11030");
  52. GELOGE(PARAM_INVALID, "Caffe prototxt has no optype [Input]");
  53. return FAILED;
  54. }
  55. return SUCCESS;
  56. }
  57. Status CaffeDataParser::ParseParamsForInput(const domi::caffe::LayerParameter *layer, ge::OpDescPtr &op) {
  58. if (layer->has_input_param()) {
  59. const domi::caffe::InputParameter &input_param = layer->input_param();
  60. if (input_param.shape_size() == 0) {
  61. ErrorManager::GetInstance().ATCReportErrMessage(
  62. "E11027", {"layername", "layertype"}, {layer->name(), layer->type()});
  63. GELOGE(PARAM_INVALID,
  64. "input_param shape size is zero, caffe layer name [%s], layer type [%s].",
  65. layer->name().c_str(), layer->type().c_str());
  66. return FAILED;
  67. }
  68. for (int i = 0; i < input_param.shape_size(); i++) {
  69. const domi::caffe::BlobShape &blob_shape = input_param.shape(i);
  70. vector<int64_t> shape;
  71. unordered_map<string, vector<int64_t>> &shape_map = domi::GetContext().input_dims;
  72. std::vector<int64_t> model_dims;
  73. for (auto &blob_shape_dim_temp : blob_shape.dim()) {
  74. model_dims.push_back(blob_shape_dim_temp);
  75. }
  76. string name = layer->name();
  77. GE_IF_BOOL_EXEC(shape_map.count(name) != 0, model_dims = shape_map.at(name));
  78. GE_CHK_STATUS_RET(GetOutputDesc(name, model_dims.size(), model_dims, op), "Get output desc failed in layer %s",
  79. name.c_str());
  80. }
  81. } else {
  82. // Get from external input
  83. const ge::OmgContext &ctx = domi::GetContext();
  84. std::unordered_map<std::string, std::vector<int64_t>> input_dims = ctx.input_dims;
  85. string name = layer->name();
  86. auto search = input_dims.find(name);
  87. if (search == input_dims.end()) {
  88. ErrorManager::GetInstance().ATCReportErrMessage(
  89. "E11028", {"layername", "layertype"}, {layer->name(), layer->type()});
  90. GELOGE(PARAM_INVALID,
  91. "Caffe prototxt has no input_param or user should set --input_shape in atc parameter, "
  92. "caffe layer name [%s], layer type [%s].", layer->name().c_str(), layer->type().c_str());
  93. return FAILED;
  94. }
  95. std::vector<int64_t> dims = search->second;
  96. GE_CHK_STATUS_RET(GetOutputDesc(name, dims.size(), dims, op), "Get output desc failed in layer %s.",
  97. name.c_str());
  98. }
  99. return SUCCESS;
  100. }
  101. Status CaffeDataParser::ParseParamsForDummyData(const domi::caffe::LayerParameter *layer, ge::OpDescPtr &op) {
  102. if (layer->has_dummy_data_param()) {
  103. const domi::caffe::DummyDataParameter &dummy_data_param = layer->dummy_data_param();
  104. if (dummy_data_param.shape_size() == 0) {
  105. ErrorManager::GetInstance().ATCReportErrMessage(
  106. "E11027", {"layername", "layertype"}, {layer->name(), layer->type()});
  107. GELOGE(PARAM_INVALID,
  108. "input_param shape size is zero, caffe layer name [%s], layer type [%s].",
  109. layer->name().c_str(), layer->type().c_str());
  110. return FAILED;
  111. }
  112. for (int i = 0; i < dummy_data_param.shape_size(); i++) {
  113. const domi::caffe::BlobShape &blob_shape = dummy_data_param.shape(i);
  114. vector<int64_t> shape;
  115. unordered_map<string, vector<int64_t>> &shape_map = domi::GetContext().input_dims;
  116. std::vector<int64_t> model_dims;
  117. for (auto &blob_shape_dim_temp : blob_shape.dim()) {
  118. model_dims.push_back(blob_shape_dim_temp);
  119. }
  120. string name = layer->name();
  121. GE_IF_BOOL_EXEC(shape_map.count(name) != 0, model_dims = shape_map.at(name));
  122. GE_CHK_STATUS_RET(GetOutputDesc(name, model_dims.size(), model_dims, op), "Get output desc failed in layer %s",
  123. name.c_str());
  124. }
  125. } else {
  126. // Get from external input
  127. const ge::OmgContext &ctx = domi::GetContext();
  128. std::unordered_map<std::string, std::vector<int64_t>> input_dims = ctx.input_dims;
  129. string name = layer->name();
  130. auto search = input_dims.find(name);
  131. if (search == input_dims.end()) {
  132. ErrorManager::GetInstance().ATCReportErrMessage(
  133. "E11028", {"layername", "layertype"}, {layer->name(), layer->type()});
  134. GELOGE(PARAM_INVALID,
  135. "Caffe prototxt has no input_param or user should set --input_shape in atc parameter, "
  136. "caffe layer name [%s], layer type [%s].", layer->name().c_str(), layer->type().c_str());
  137. return FAILED;
  138. }
  139. std::vector<int64_t> dims = search->second;
  140. GE_CHK_STATUS_RET(GetOutputDesc(name, dims.size(), dims, op), "Get output desc failed in layer %s.",
  141. name.c_str());
  142. }
  143. return SUCCESS;
  144. }
  145. REGISTER_OP_PARSER_CREATOR(CAFFE, DATA, CaffeDataParser);
  146. } // namespace ge

Ascend CANN Parser(简称parser)配合TF_Adapter、 ATC工具、IR构图等使用,开发者通过以上工具,借助parser能方便地将第三方框架的算法表示转换成Ascend IR,充分利用昇腾AI处理器卓越的运算能力