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.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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::api;
  23. static const char tensor_add_file[] = "/home/workspace/mindspore_dataset/mindir/tensor_add/tensor_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 TestTensorAdd : public ST::Common {
  27. public:
  28. TestTensorAdd() {}
  29. };
  30. TEST_F(TestTensorAdd, InferMindIR) {
  31. ContextAutoSet();
  32. auto graph = Serialization::LoadModel(tensor_add_file, ModelType::kMindIR);
  33. Model tensor_add((GraphCell(graph)));
  34. Status ret = tensor_add.Build({});
  35. ASSERT_TRUE(ret == SUCCESS);
  36. // prepare input
  37. std::vector<Buffer> outputs;
  38. std::vector<Buffer> inputs;
  39. inputs.emplace_back(Buffer(input_data_1.data(), sizeof(float) * input_data_1.size()));
  40. inputs.emplace_back(Buffer(input_data_2.data(), sizeof(float) * input_data_2.size()));
  41. // infer
  42. ret = tensor_add.Predict(inputs, &outputs);
  43. ASSERT_TRUE(ret == SUCCESS);
  44. // print
  45. for (auto &buffer : outputs) {
  46. const float *p = reinterpret_cast<const float *>(buffer.Data());
  47. for (size_t i = 0; i < buffer.DataSize() / sizeof(float); ++i) {
  48. ASSERT_LE(std::abs(p[i] - (input_data_1[i] + input_data_2[i])), 1e-4);
  49. }
  50. }
  51. }