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.

test_tensorflow_parser.cc 3.4 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include <gtest/gtest.h>
  17. #include "parser/common/op_parser_factory.h"
  18. #include "parser/tensorflow/tensorflow_parser.h"
  19. #include "graph/operator_reg.h"
  20. #include "register/op_registry.h"
  21. #include "parser/common/register_tbe.h"
  22. #include "external/parser/tensorflow_parser.h"
  23. #include "st/parser_st_utils.h"
  24. namespace ge {
  25. class STestTensorflowParser : public testing::Test {
  26. protected:
  27. void SetUp() {
  28. ParerSTestsUtils::ClearParserInnerCtx();
  29. }
  30. void TearDown() {}
  31. public:
  32. void RegisterCustomOp();
  33. };
  34. static Status ParseParams(const google::protobuf::Message* op_src, ge::Operator& op_dest) {
  35. return SUCCESS;
  36. }
  37. void STestTensorflowParser::RegisterCustomOp() {
  38. REGISTER_CUSTOM_OP("Add")
  39. .FrameworkType(domi::TENSORFLOW)
  40. .OriginOpType("Add")
  41. .ParseParamsFn(ParseParams);
  42. std::vector<OpRegistrationData> reg_datas = domi::OpRegistry::Instance()->registrationDatas;
  43. for (auto reg_data : reg_datas) {
  44. OpRegistrationTbe::Instance()->Finalize(reg_data);
  45. domi::OpRegistry::Instance()->Register(reg_data);
  46. }
  47. domi::OpRegistry::Instance()->registrationDatas.clear();
  48. }
  49. namespace {
  50. REG_OP(Data)
  51. .INPUT(x, TensorType::ALL())
  52. .OUTPUT(y, TensorType::ALL())
  53. .ATTR(index, Int, 0)
  54. .OP_END_FACTORY_REG(Data)
  55. REG_OP(Add)
  56. .INPUT(x1, TensorType({DT_FLOAT, DT_INT32, DT_INT64, DT_FLOAT16, DT_INT16,
  57. DT_INT8, DT_UINT8, DT_DOUBLE, DT_COMPLEX128,
  58. DT_COMPLEX64, DT_STRING}))
  59. .INPUT(x2, TensorType({DT_FLOAT, DT_INT32, DT_INT64, DT_FLOAT16, DT_INT16,
  60. DT_INT8, DT_UINT8, DT_DOUBLE, DT_COMPLEX128,
  61. DT_COMPLEX64, DT_STRING}))
  62. .OUTPUT(y, TensorType({DT_FLOAT, DT_INT32, DT_INT64, DT_FLOAT16, DT_INT16,
  63. DT_INT8, DT_UINT8, DT_DOUBLE, DT_COMPLEX128,
  64. DT_COMPLEX64, DT_STRING}))
  65. .OP_END_FACTORY_REG(Add)
  66. }
  67. TEST_F(STestTensorflowParser, tensorflow_parser_success) {
  68. RegisterCustomOp();
  69. std::string case_dir = __FILE__;
  70. case_dir = case_dir.substr(0, case_dir.find_last_of("/"));
  71. std::string model_file = case_dir + "/origin_models/tf_add.pb";
  72. std::map<ge::AscendString, ge::AscendString> parser_params;
  73. ge::Graph graph;
  74. auto ret = ge::aclgrphParseTensorFlow(model_file.c_str(), parser_params, graph);
  75. ASSERT_EQ(ret, SUCCESS);
  76. ge::ComputeGraphPtr compute_graph = ge::GraphUtils::GetComputeGraph(graph);
  77. auto output_nodes_info = compute_graph->GetGraphOutNodesInfo();
  78. ASSERT_EQ(output_nodes_info.size(), 1);
  79. EXPECT_EQ((output_nodes_info.at(0).first->GetName()), "add_test_1");
  80. EXPECT_EQ((output_nodes_info.at(0).second), 0);
  81. auto &net_out_name = ge::GetParserContext().net_out_nodes;
  82. ASSERT_EQ(net_out_name.size(), 1);
  83. EXPECT_EQ(net_out_name.at(0), "add_test_1:0");
  84. }
  85. } // namespace ge