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.

fill_op_test.cc 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "common/common.h"
  17. #include "minddata/dataset/kernels/data/fill_op.h"
  18. #include "utils/log_adapter.h"
  19. using namespace mindspore::dataset;
  20. using mindspore::LogStream;
  21. using mindspore::ExceptionType::NoExceptionType;
  22. using mindspore::MsLogLevel::INFO;
  23. class MindDataTestFillOp : public UT::Common {
  24. protected:
  25. MindDataTestFillOp() {}
  26. };
  27. TEST_F(MindDataTestFillOp, TestOp) {
  28. MS_LOG(INFO) << "Doing MindDataTestFillOp-TestOp.";
  29. std::vector<uint64_t> labels = {1, 1, 2};
  30. std::shared_ptr<Tensor> input;
  31. Tensor::CreateFromVector(labels, &input);
  32. std::shared_ptr<Tensor> fill_tensor;
  33. Tensor::CreateScalar<uint64_t>(4, &fill_tensor);
  34. std::shared_ptr<Tensor> output;
  35. std::unique_ptr<FillOp> op(new FillOp(fill_tensor));
  36. Status s = op->Compute(input, &output);
  37. std::vector<uint64_t> out = {4, 4, 4};
  38. std::shared_ptr<Tensor> expected;
  39. Tensor::CreateFromVector(out, &expected);
  40. EXPECT_TRUE(s.IsOk());
  41. ASSERT_TRUE(output->shape() == expected->shape());
  42. ASSERT_TRUE(output->type() == expected->type());
  43. MS_LOG(DEBUG) << *output << std::endl;
  44. MS_LOG(DEBUG) << *expected << std::endl;
  45. ASSERT_TRUE(*output == *expected);
  46. MS_LOG(INFO) << "MindDataTestFillOp-TestOp end.";
  47. }
  48. TEST_F(MindDataTestFillOp, TestCasting) {
  49. MS_LOG(INFO) << "Doing MindDataTestFillOp-TestCasting.";
  50. std::vector<uint64_t> labels = {0, 1, 2};
  51. std::shared_ptr<Tensor> input;
  52. Tensor::CreateFromVector(labels, &input);
  53. std::shared_ptr<Tensor> fill_tensor;
  54. Tensor::CreateScalar<float>(2.0, &fill_tensor);
  55. std::shared_ptr<Tensor> output;
  56. std::unique_ptr<FillOp> op(new FillOp(fill_tensor));
  57. Status s = op->Compute(input, &output);
  58. std::vector<uint64_t> out = {2, 2, 2};
  59. std::shared_ptr<Tensor> expected;
  60. Tensor::CreateFromVector(out, &expected);
  61. ASSERT_TRUE(output->shape() == expected->shape());
  62. ASSERT_TRUE(output->type() == expected->type());
  63. EXPECT_TRUE(s.IsOk());
  64. MS_LOG(DEBUG) << *output << std::endl;
  65. MS_LOG(DEBUG) << *expected << std::endl;
  66. ASSERT_TRUE(*output == *expected);
  67. MS_LOG(INFO) << "MindDataTestFillOp-TestCasting end.";
  68. }
  69. TEST_F(MindDataTestFillOp, ScalarFill) {
  70. MS_LOG(INFO) << "Doing MindDataTestFillOp-ScalarFill.";
  71. std::vector<uint64_t> labels = {0, 1, 2};
  72. std::shared_ptr<Tensor> input;
  73. Tensor::CreateFromVector(labels, &input);
  74. TensorShape fill_shape({2});
  75. std::vector<uint64_t> fill_labels = {0, 1};
  76. std::shared_ptr<Tensor> fill_tensor;
  77. Tensor::CreateFromVector(fill_labels, &fill_tensor);
  78. std::shared_ptr<Tensor> output;
  79. std::unique_ptr<FillOp> op(new FillOp(fill_tensor));
  80. Status s = op->Compute(input, &output);
  81. EXPECT_TRUE(s.IsError());
  82. ASSERT_TRUE(s.StatusCode() == StatusCode::kMDUnexpectedError);
  83. MS_LOG(INFO) << "MindDataTestFillOp-ScalarFill end.";
  84. }
  85. TEST_F(MindDataTestFillOp, StringFill) {
  86. MS_LOG(INFO) << "Doing MindDataTestFillOp-StringFill.";
  87. std::vector<std::string> strings = {"xyzzy", "plugh", "abracadabra"};
  88. std::shared_ptr<Tensor> input;
  89. Tensor::CreateFromVector(strings, &input);
  90. std::shared_ptr<Tensor> fill_tensor;
  91. Tensor::CreateScalar<std::string>("hello", &fill_tensor);
  92. std::shared_ptr<Tensor> output;
  93. std::unique_ptr<FillOp> op(new FillOp(fill_tensor));
  94. Status s = op->Compute(input, &output);
  95. std::vector<std::string> expected_strings = {"hello", "hello", "hello"};
  96. std::shared_ptr<Tensor> expected;
  97. Tensor::CreateFromVector(expected_strings, &expected);
  98. EXPECT_TRUE(s.IsOk());
  99. ASSERT_TRUE(output->shape() == expected->shape());
  100. ASSERT_TRUE(output->type() == expected->type());
  101. MS_LOG(DEBUG) << *output << std::endl;
  102. MS_LOG(DEBUG) << *expected << std::endl;
  103. ASSERT_TRUE(*output == *expected);
  104. MS_LOG(INFO) << "MindDataTestFillOp-StringFill end.";
  105. }
  106. TEST_F(MindDataTestFillOp, NumericToString) {
  107. MS_LOG(INFO) << "Doing MindDataTestFillOp-NumericToString.";
  108. std::vector<std::string> strings = {"xyzzy", "plugh", "abracadabra"};
  109. std::shared_ptr<Tensor> input;
  110. Tensor::CreateFromVector(strings, &input);
  111. std::shared_ptr<Tensor> fill_tensor;
  112. Tensor::CreateScalar<float>(2.0, &fill_tensor);
  113. std::shared_ptr<Tensor> output;
  114. std::unique_ptr<FillOp> op(new FillOp(fill_tensor));
  115. Status s = op->Compute(input, &output);
  116. EXPECT_TRUE(s.IsError());
  117. ASSERT_TRUE(s.StatusCode() == StatusCode::kMDUnexpectedError);
  118. MS_LOG(INFO) << "MindDataTestFillOp-NumericToString end.";
  119. }
  120. TEST_F(MindDataTestFillOp, StringToNumeric) {
  121. MS_LOG(INFO) << "Doing MindDataTestFillOp-StringToNumeric.";
  122. std::vector<uint64_t> labels = {0, 1, 2};
  123. std::shared_ptr<Tensor> input;
  124. Tensor::CreateFromVector(labels, &input);
  125. std::shared_ptr<Tensor> fill_tensor;
  126. Tensor::CreateScalar<std::string>("hello", &fill_tensor);
  127. std::shared_ptr<Tensor> output;
  128. std::unique_ptr<FillOp> op(new FillOp(fill_tensor));
  129. Status s = op->Compute(input, &output);
  130. EXPECT_TRUE(s.IsError());
  131. ASSERT_TRUE(s.StatusCode() == StatusCode::kMDUnexpectedError);
  132. MS_LOG(INFO) << "MindDataTestFillOp-StringToNumeric end.";
  133. }