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 6.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. uint64_t labels[3] = {1, 1, 2};
  30. TensorShape shape({3});
  31. std::shared_ptr<Tensor> input =
  32. std::make_shared<Tensor>(shape, DataType(DataType::DE_UINT64), reinterpret_cast<unsigned char *>(labels));
  33. TensorShape fill_shape({});
  34. std::shared_ptr<Tensor> fill_tensor = std::make_shared<Tensor>(fill_shape, DataType(DataType::DE_UINT64));
  35. fill_tensor->SetItemAt<uint64_t>({}, 4);
  36. std::shared_ptr<Tensor> output;
  37. std::unique_ptr<FillOp> op(new FillOp(fill_tensor));
  38. Status s = op->Compute(input, &output);
  39. uint64_t out[3] = {4, 4, 4};
  40. std::shared_ptr<Tensor> expected =
  41. std::make_shared<Tensor>(TensorShape{3}, DataType(DataType::DE_UINT64), reinterpret_cast<unsigned char *>(out));
  42. EXPECT_TRUE(s.IsOk());
  43. ASSERT_TRUE(output->shape() == expected->shape());
  44. ASSERT_TRUE(output->type() == expected->type());
  45. MS_LOG(DEBUG) << *output << std::endl;
  46. MS_LOG(DEBUG) << *expected << std::endl;
  47. ASSERT_TRUE(*output == *expected);
  48. MS_LOG(INFO) << "MindDataTestFillOp-TestOp end.";
  49. }
  50. TEST_F(MindDataTestFillOp, TestCasting) {
  51. MS_LOG(INFO) << "Doing MindDataTestFillOp-TestCasting.";
  52. uint64_t labels[3] = {0, 1, 2};
  53. TensorShape shape({3});
  54. std::shared_ptr<Tensor> input =
  55. std::make_shared<Tensor>(shape, DataType(DataType::DE_UINT64), reinterpret_cast<unsigned char *>(labels));
  56. TensorShape fill_shape({});
  57. std::shared_ptr<Tensor> fill_tensor = std::make_shared<Tensor>(fill_shape, DataType(DataType::DE_FLOAT32));
  58. fill_tensor->SetItemAt<float>({}, 2.0);
  59. std::shared_ptr<Tensor> output;
  60. std::unique_ptr<FillOp> op(new FillOp(fill_tensor));
  61. Status s = op->Compute(input, &output);
  62. uint64_t out[3] = {2, 2, 2};
  63. std::shared_ptr<Tensor> expected =
  64. std::make_shared<Tensor>(TensorShape{3}, DataType(DataType::DE_UINT64), reinterpret_cast<unsigned char *>(out));
  65. ASSERT_TRUE(output->shape() == expected->shape());
  66. ASSERT_TRUE(output->type() == expected->type());
  67. EXPECT_TRUE(s.IsOk());
  68. MS_LOG(DEBUG) << *output << std::endl;
  69. MS_LOG(DEBUG) << *expected << std::endl;
  70. ASSERT_TRUE(*output == *expected);
  71. MS_LOG(INFO) << "MindDataTestFillOp-TestCasting end.";
  72. }
  73. TEST_F(MindDataTestFillOp, ScalarFill) {
  74. MS_LOG(INFO) << "Doing MindDataTestFillOp-ScalarFill.";
  75. uint64_t labels[3] = {0, 1, 2};
  76. TensorShape shape({3});
  77. std::shared_ptr<Tensor> input =
  78. std::make_shared<Tensor>(shape, DataType(DataType::DE_UINT64), reinterpret_cast<unsigned char *>(labels));
  79. TensorShape fill_shape({2});
  80. uint64_t fill_labels[3] = {0, 1};
  81. std::shared_ptr<Tensor> fill_tensor =
  82. std::make_shared<Tensor>(fill_shape, DataType(DataType::DE_UINT64), reinterpret_cast<unsigned char *>(fill_labels));
  83. std::shared_ptr<Tensor> output;
  84. std::unique_ptr<FillOp> op(new FillOp(fill_tensor));
  85. Status s = op->Compute(input, &output);
  86. EXPECT_TRUE(s.IsError());
  87. ASSERT_TRUE(s.get_code() == StatusCode::kUnexpectedError);
  88. MS_LOG(INFO) << "MindDataTestFillOp-ScalarFill end.";
  89. }
  90. TEST_F(MindDataTestFillOp, StringFill) {
  91. MS_LOG(INFO) << "Doing MindDataTestFillOp-StringFill.";
  92. std::vector<std::string> strings = {"xyzzy", "plugh", "abracadabra"};
  93. TensorShape shape({3});
  94. std::shared_ptr<Tensor> input = std::make_shared<Tensor>(strings, shape);
  95. TensorShape fill_shape({});
  96. std::string fill_string = "hello";
  97. std::shared_ptr<Tensor> fill_tensor = std::make_shared<Tensor>(fill_string);
  98. std::shared_ptr<Tensor> output;
  99. std::unique_ptr<FillOp> op(new FillOp(fill_tensor));
  100. Status s = op->Compute(input, &output);
  101. std::vector<std::string> expected_strings = {"hello", "hello", "hello"};
  102. TensorShape expected_shape({3});
  103. std::shared_ptr<Tensor> expected = std::make_shared<Tensor>(expected_strings, expected_shape);
  104. EXPECT_TRUE(s.IsOk());
  105. ASSERT_TRUE(output->shape() == expected->shape());
  106. ASSERT_TRUE(output->type() == expected->type());
  107. MS_LOG(DEBUG) << *output << std::endl;
  108. MS_LOG(DEBUG) << *expected << std::endl;
  109. ASSERT_TRUE(*output == *expected);
  110. MS_LOG(INFO) << "MindDataTestFillOp-StringFill end.";
  111. }
  112. TEST_F(MindDataTestFillOp, NumericToString) {
  113. MS_LOG(INFO) << "Doing MindDataTestFillOp-NumericToString.";
  114. std::vector<std::string> strings = {"xyzzy", "plugh", "abracadabra"};
  115. TensorShape shape({3});
  116. std::shared_ptr<Tensor> input = std::make_shared<Tensor>(strings, shape);
  117. TensorShape fill_shape({});
  118. std::shared_ptr<Tensor> fill_tensor = std::make_shared<Tensor>(fill_shape, DataType(DataType::DE_FLOAT32));
  119. fill_tensor->SetItemAt<float>({}, 2.0);
  120. std::shared_ptr<Tensor> output;
  121. std::unique_ptr<FillOp> op(new FillOp(fill_tensor));
  122. Status s = op->Compute(input, &output);
  123. EXPECT_TRUE(s.IsError());
  124. ASSERT_TRUE(s.get_code() == StatusCode::kUnexpectedError);
  125. MS_LOG(INFO) << "MindDataTestFillOp-NumericToString end.";
  126. }
  127. TEST_F(MindDataTestFillOp, StringToNumeric) {
  128. MS_LOG(INFO) << "Doing MindDataTestFillOp-StringToNumeric.";
  129. uint64_t labels[3] = {0, 1, 2};
  130. TensorShape shape({3});
  131. std::shared_ptr<Tensor> input =
  132. std::make_shared<Tensor>(shape, DataType(DataType::DE_UINT64), reinterpret_cast<unsigned char *>(labels));
  133. TensorShape fill_shape({});
  134. std::string fill_string = "hello";
  135. std::shared_ptr<Tensor> fill_tensor = std::make_shared<Tensor>(fill_string);
  136. std::shared_ptr<Tensor> output;
  137. std::unique_ptr<FillOp> op(new FillOp(fill_tensor));
  138. Status s = op->Compute(input, &output);
  139. EXPECT_TRUE(s.IsError());
  140. ASSERT_TRUE(s.get_code() == StatusCode::kUnexpectedError);
  141. MS_LOG(INFO) << "MindDataTestFillOp-StringToNumeric end.";
  142. }