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.

tensorflow_data_parser.cc 6.8 kB

5 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
3 years ago
5 years ago
5 years ago
5 years ago
3 years ago
5 years ago
3 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2022. 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. #include "parser/tensorflow/tensorflow_data_parser.h"
  17. #include <unordered_map>
  18. #include "common/util.h"
  19. #include "framework/common/debug/ge_log.h"
  20. #include "framework/omg/parser/parser_inner_ctx.h"
  21. #include "parser/common/op_parser_factory.h"
  22. #include "framework/omg/parser/parser_types.h"
  23. using domi::tensorflow::AttrValue;
  24. using domi::tensorflow::NodeDef;
  25. using domi::TENSORFLOW;
  26. using ge::parser::DATA;
  27. namespace ge {
  28. namespace {
  29. const int64_t kValidShapeMinValue = -2;
  30. } // namespace
  31. Status TensorFlowDataParser::ParseParams(const Message *op_src, ge::OpDescPtr &op_def) {
  32. GE_CHECK_NOTNULL(op_src);
  33. const NodeDef *node_src = DOMI_DYNAMIC_CAST<const NodeDef *>(op_src);
  34. GE_CHECK_NOTNULL(node_src);
  35. GELOGD("TF op node name = %s, op type= %s, parse params", node_src->name().c_str(), node_src->op().c_str());
  36. GE_CHECK_NOTNULL(op_def);
  37. GE_RETURN_WITH_LOG_IF_ERROR(ParseInputFromModel(op_src, op_def), "parse shape of data op %s from model failed",
  38. op_def->GetName().c_str());
  39. GE_RETURN_WITH_LOG_IF_ERROR(ParseInputFromUser(op_src, op_def), "parse shape of data op %s from user failed",
  40. op_def->GetName().c_str());
  41. GE_RETURN_WITH_LOG_IF_ERROR(CheckInputShape(op_def->GetName()),
  42. "input node %s :check user designated input shape not match input shape defined in model",
  43. op_def->GetName().c_str());
  44. // Parse data dimension values and add them to op_def
  45. GE_RETURN_WITH_LOG_IF_ERROR(ParseShape(user_input_dims_v, op_def), "TensorFlowDataParser::ParseShape failed");
  46. return SUCCESS;
  47. }
  48. Status TensorFlowDataParser::ParseInputFromModel(const Message *op_src, const ge::OpDescPtr &op_def) {
  49. GE_CHECK_NOTNULL(op_src);
  50. GE_CHECK_NOTNULL(op_def);
  51. const NodeDef *node = DOMI_DYNAMIC_CAST<const domi::tensorflow::NodeDef *>(op_src);
  52. GE_CHECK_NOTNULL(node);
  53. domi::tensorflow::AttrValue attr_value;
  54. if (TensorFlowUtil::FindAttrValue(node, TENSORFLOW_ATTR_DTYPE, attr_value)) {
  55. // Check dtype attribute must be type
  56. GE_RETURN_WITH_LOG_IF_ERROR(TensorFlowUtil::CheckAttrHasType(attr_value, TENSORFLOW_ATTR_TYPE_TYPE),
  57. "check Attr %s failed", TENSORFLOW_ATTR_DTYPE.c_str());
  58. domi::tensorflow::DataType tf_type = attr_value.type();
  59. ge::DataType type = domi::TensorAssign::ConvertTensorflowDataType(tf_type);
  60. CHECK_FALSE_EXEC(type != ge::DataType::DT_UNDEFINED,
  61. REPORT_CALL_ERROR("E19999", "Data type %s of node %s is not supported",
  62. DataType_Name(tf_type).c_str(),
  63. node->name().c_str());
  64. GELOGE(domi::PARAM_INVALID,
  65. "Data type %s of node %s is not supported.",
  66. DataType_Name(tf_type).c_str(),
  67. node->name().c_str());
  68. return domi::PARAM_INVALID);
  69. GE_CHK_BOOL_RET_STATUS(ge::AttrUtils::SetInt(op_def, DATA_ATTR_NAME_DATA_TYPE, static_cast<int64_t>(type)), FAILED,
  70. "SetAttr:%s to node:%s(%s) failed", DATA_ATTR_NAME_DATA_TYPE.c_str(),
  71. op_def->GetName().c_str(), op_def->GetType().c_str());
  72. }
  73. if (!TensorFlowUtil::FindAttrValue(node, TENSORFLOW_ATTR_SHAPE, attr_value)) {
  74. // in some case, data could be without shape and is updated by `input_shape` option in following process
  75. GELOGW("input data node %s do not find shape.", node->name().c_str());
  76. return SUCCESS;
  77. }
  78. // Check shape attribute must be shape
  79. GE_RETURN_WITH_LOG_IF_ERROR(TensorFlowUtil::CheckAttrHasType(attr_value, TENSORFLOW_ATTR_TYPE_SHAPE),
  80. "check Attr %s failed", TENSORFLOW_ATTR_SHAPE.c_str());
  81. const domi::tensorflow::TensorShapeProto &data_shape = attr_value.shape();
  82. for (auto i = 0; i < data_shape.dim_size(); i++) {
  83. model_input_dims_v.push_back(data_shape.dim(i).size());
  84. }
  85. return SUCCESS;
  86. }
  87. Status TensorFlowDataParser::ParseInputFromUser(const Message *op_src, const ge::OpDescPtr &op_def) {
  88. GE_CHECK_NOTNULL(op_def);
  89. (void)op_src;
  90. const ge::ParserContext &ctx = GetParserContext();
  91. std::map<std::string, std::vector<int64_t>> input_dims = ctx.input_dims;
  92. // User not designate the input_shape
  93. std::string name = op_def->GetName();
  94. if (input_dims.count(name) == 0) {
  95. GELOGI("input shape of node %s is not designated ,need parse from model", name.c_str());
  96. for (size_t i = 0; i < model_input_dims_v.size(); ++i) {
  97. user_input_dims_v.push_back(model_input_dims_v[i]);
  98. }
  99. return SUCCESS;
  100. }
  101. /* User designate the input_shape by passing '--input_shape=xxx:x,x,x,x' */
  102. // Two cases below both OK:
  103. // 1. the input_shape not defined in the model(dimension is 0).
  104. // 2. the input_shape defined in the model(dimension greater than 0), and the dimension matches with user
  105. // designate_dim.
  106. std::vector<int64_t> designated_dims = input_dims.at(name);
  107. size_t input_dim_size_ = designated_dims.size();
  108. GE_CHK_BOOL_RET_STATUS(model_input_dims_v.empty() || input_dim_size_ == model_input_dims_v.size(),
  109. domi::PARAM_INVALID,
  110. "user designated input_dim_num %zu does match input_dim_num %zu defined by model",
  111. input_dim_size_,
  112. model_input_dims_v.size());
  113. // replace with the user designated_dims
  114. user_input_dims_v.swap(designated_dims);
  115. return SUCCESS;
  116. }
  117. Status TensorFlowDataParser::CheckInputShape(const std::string &name) {
  118. for (size_t i = 0; i < user_input_dims_v.size(); ++i) {
  119. // if input_shape has some placeholders, user should designate them.
  120. // dim i = 0, means empty tensor.
  121. // dim i = -1 or -2, means unknown shape.
  122. GE_CHK_BOOL_RET_STATUS(user_input_dims_v[i] >= kValidShapeMinValue, domi::PARAM_INVALID,
  123. "parse data node %s: shape contains placeholder, but not designated by user", name.c_str());
  124. }
  125. return SUCCESS;
  126. }
  127. REGISTER_OP_PARSER_CREATOR(TENSORFLOW, DATA, TensorFlowDataParser);
  128. } // namespace ge