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_tensor_add.cc 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <string>
  17. #include <vector>
  18. #include "common/common_test.h"
  19. #include "include/api/model.h"
  20. #include "include/api/serialization.h"
  21. #include "include/api/context.h"
  22. using namespace mindspore;
  23. static const char tensor_add_file[] = "/home/workspace/mindspore_dataset/mindir/add/add.mindir";
  24. static const std::vector<float> input_data_1 = {1, 2, 3, 4};
  25. static const std::vector<float> input_data_2 = {2, 3, 4, 5};
  26. class TestAdd : public ST::Common {
  27. public:
  28. TestAdd() {}
  29. };
  30. TEST_F(TestAdd, InferMindIR) {
  31. auto context = ContextAutoSet();
  32. Graph graph;
  33. ASSERT_TRUE(Serialization::Load(tensor_add_file, ModelType::kMindIR, &graph));
  34. Model tensor_add;
  35. ASSERT_TRUE(tensor_add.Build(GraphCell(graph), context) == kSuccess);
  36. // get model inputs
  37. std::vector<MSTensor> origin_inputs = tensor_add.GetInputs();
  38. ASSERT_EQ(origin_inputs.size(), 2);
  39. // prepare input
  40. std::vector<MSTensor> outputs;
  41. std::vector<MSTensor> inputs;
  42. inputs.emplace_back(origin_inputs[0].Name(), origin_inputs[0].DataType(), origin_inputs[0].Shape(),
  43. input_data_1.data(), sizeof(float) * input_data_1.size());
  44. inputs.emplace_back(origin_inputs[1].Name(), origin_inputs[1].DataType(), origin_inputs[1].Shape(),
  45. input_data_2.data(), sizeof(float) * input_data_2.size());
  46. // infer
  47. ASSERT_TRUE(tensor_add.Predict(inputs, &outputs) == kSuccess);
  48. // assert input
  49. inputs = tensor_add.GetInputs();
  50. ASSERT_EQ(inputs.size(), 2);
  51. auto after_input_data_1 = inputs[0].Data();
  52. auto after_input_data_2 = inputs[1].Data();
  53. const float *p = reinterpret_cast<const float *>(after_input_data_1.get());
  54. for (size_t i = 0; i < inputs[0].DataSize() / sizeof(float); ++i) {
  55. ASSERT_LE(std::abs(p[i] - input_data_1[i]), 1e-4);
  56. }
  57. p = reinterpret_cast<const float *>(after_input_data_2.get());
  58. for (size_t i = 0; i < inputs[0].DataSize() / sizeof(float); ++i) {
  59. ASSERT_LE(std::abs(p[i] - input_data_2[i]), 1e-4);
  60. }
  61. // assert output
  62. for (auto &buffer : outputs) {
  63. auto buffer_data = buffer.Data();
  64. p = reinterpret_cast<const float *>(buffer_data.get());
  65. for (size_t i = 0; i < buffer.DataSize() / sizeof(float); ++i) {
  66. ASSERT_LE(std::abs(p[i] - (input_data_1[i] + input_data_2[i])), 1e-4);
  67. }
  68. }
  69. }