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_constant_parser.cc 4.6 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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/tensorflow/tensorflow_constant_parser.h"
  17. #include <map>
  18. #include <memory>
  19. #include <vector>
  20. #include "common/debug/log.h"
  21. #include "parser/common/acl_graph_parser_util.h"
  22. #include "common/op/ge_op_utils.h"
  23. #include "parser/common/op_def/constant_op.h"
  24. #include "parser/common/op_def/ir_pb_converter.h"
  25. #include "framework/common/debug/ge_log.h"
  26. #include "graph/ge_tensor.h"
  27. #include "graph/utils/attr_utils.h"
  28. #include "parser/common/op_parser_factory.h"
  29. #include "framework/omg/parser/parser_types.h"
  30. #include "register/tensor_assign.h"
  31. using domi::tensorflow::NodeDef;
  32. using domi::TENSORFLOW;
  33. using ge::parser::CONSTANTOP;
  34. namespace ge {
  35. Status TensorFlowConstantParser::ParseDType(const domi::tensorflow::NodeDef *node, ConstantOperator *op) {
  36. GE_CHECK_NOTNULL(node);
  37. GE_CHECK_NOTNULL(op);
  38. domi::tensorflow::AttrValue attr;
  39. CHECK_FALSE_EXEC(TensorFlowUtil::FindAttrValue(node, TENSORFLOW_ATTR_DTYPE, attr),
  40. op->DType(domi::TensorAssign::ConvertTensorflowDataType(domi::tensorflow::DT_FLOAT));
  41. return SUCCESS);
  42. GE_RETURN_WITH_LOG_IF_ERROR(TensorFlowUtil::CheckAttrHasType(attr, TENSORFLOW_ATTR_TYPE_TYPE),
  43. "check Attr dtype fail");
  44. domi::tensorflow::DataType tf_type = attr.type();
  45. ge::DataType type = domi::TensorAssign::ConvertTensorflowDataType(tf_type);
  46. op->DType(type);
  47. return SUCCESS;
  48. }
  49. Status TensorFlowConstantParser::ParseValue(const domi::tensorflow::NodeDef *node, const ge::OpDescPtr &opDesc) {
  50. GE_CHECK_NOTNULL(node);
  51. GE_CHECK_NOTNULL(opDesc);
  52. domi::tensorflow::AttrValue attr_value;
  53. // Check that the attribute value must exist and get the value of value
  54. GE_CHK_BOOL_RET_STATUS(TensorFlowUtil::FindAttrValue(node, TENSORFLOW_ATTR_VALUE, attr_value),
  55. domi::FAILED, "nodeDef %s Attr %s is not exist.", node->name().c_str(),
  56. TENSORFLOW_ATTR_VALUE.c_str());
  57. // Check that the value attribute must be tensor
  58. GE_RETURN_WITH_LOG_IF_ERROR(TensorFlowUtil::CheckAttrHasType(attr_value, TENSORFLOW_ATTR_TYPE_TENSOR),
  59. "check Attr %s failed", TENSORFLOW_ATTR_VALUE.c_str());
  60. const domi::tensorflow::TensorProto &tensor = attr_value.tensor();
  61. GeTensorPtr weight = ge::parser::MakeShared<ge::GeTensor>();
  62. GE_CHECK_NOTNULL(weight);
  63. int64_t dataType = 0;
  64. GE_CHK_BOOL_RET_STATUS(ge::AttrUtils::GetInt(opDesc, TENSORFLOW_ATTR_DTYPE, dataType), INTERNAL_ERROR,
  65. "get dtype fail");
  66. GE_CHK_STATUS_RET(domi::TensorAssign::SetGeTensorDataType(dataType, weight), "set ge tensor data type fail");
  67. GE_CHK_STATUS_RET(domi::TensorAssign::SetGeTensor(tensor, weight), "set ge tensor fail");
  68. GELOGI("TensorFlowConstantParser::ParseValue. TF op node name = %s", opDesc->GetName().c_str());
  69. GE_CHK_BOOL_RET_STATUS(ge::AttrUtils::SetTensor(opDesc, ATTR_NAME_WEIGHTS, weight), INTERNAL_ERROR,
  70. "set tensor fail");
  71. return domi::SUCCESS;
  72. }
  73. Status TensorFlowConstantParser::ParseParams(const Message *op_src, ge::OpDescPtr &op_dest) {
  74. GE_CHECK_NOTNULL(op_dest);
  75. const NodeDef *node = DOMI_DYNAMIC_CAST<const NodeDef *>(op_src);
  76. GE_CHECK_NOTNULL(node);
  77. GELOGD("TF op node name = %s, op type= %s, parse params", node->name().c_str(), node->op().c_str());
  78. ConstantOperator op;
  79. op.Name(node->name());
  80. GE_RETURN_WITH_LOG_IF_ERROR(ParseDType(node, &op), "Parse dtype for node %s failed.", node->name().c_str());
  81. GE_CHK_STATUS_RET(ConvertToOpDesc(op, op_dest), "ConvertToOpDesc ret fail");
  82. GE_CHK_STATUS_RET(ParseValue(node, op_dest), "ParseValue ret fail");
  83. for (const auto &output_desc : op_dest->GetAllOutputsDescPtr()) {
  84. // Fixed input ND
  85. output_desc->SetFormat(ge::Format::FORMAT_ND);
  86. output_desc->SetOriginFormat(ge::Format::FORMAT_ND);
  87. }
  88. return SUCCESS;
  89. }
  90. REGISTER_OP_PARSER_CREATOR(TENSORFLOW, CONSTANTOP, TensorFlowConstantParser);
  91. } // namespace ge