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.

batch_op_test.cc 13 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * Copyright 2019 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 <iostream>
  17. #include <memory>
  18. #include <string>
  19. #include "dataset/core/client.h"
  20. #include "common/common.h"
  21. #include "common/utils.h"
  22. #include "gtest/gtest.h"
  23. #include "dataset/core/global_context.h"
  24. #include "dataset/util/de_error.h"
  25. #include "utils/log_adapter.h"
  26. #include "securec.h"
  27. #include "dataset/util/status.h"
  28. namespace common = mindspore::common;
  29. namespace de = mindspore::dataset;
  30. using namespace mindspore::dataset;
  31. using mindspore::MsLogLevel::ERROR;
  32. using mindspore::ExceptionType::NoExceptionType;
  33. using mindspore::LogStream;
  34. class MindDataTestBatchOp : public UT::DatasetOpTesting {
  35. protected:
  36. };
  37. std::shared_ptr<de::BatchOp> Batch(int32_t batch_size = 1, bool drop = false, int rows_per_buf = 2) {
  38. Status rc;
  39. std::shared_ptr<de::BatchOp> op;
  40. rc = de::BatchOp::Builder(batch_size).SetDrop(drop).Build(&op);
  41. EXPECT_TRUE(rc.IsOk());
  42. return op;
  43. }
  44. std::shared_ptr<de::RepeatOp> Repeat(int repeat_cnt = 1) {
  45. de::RepeatOp::Builder builder(repeat_cnt);
  46. std::shared_ptr<de::RepeatOp> op;
  47. Status rc = builder.Build(&op);
  48. EXPECT_TRUE(rc.IsOk());
  49. return op;
  50. }
  51. std::shared_ptr<de::StorageOp> Storage(std::string schema, int rows_per_buf = 2, int num_works = 8) {
  52. std::shared_ptr<de::StorageOp> so;
  53. de::StorageOp::Builder builder;
  54. builder.SetDatasetFilesDir(schema).SetRowsPerBuffer(rows_per_buf).SetNumWorkers(num_works);
  55. Status rc = builder.Build(&so);
  56. return so;
  57. }
  58. std::shared_ptr<de::ExecutionTree> Build(std::vector<std::shared_ptr<de::DatasetOp>> ops) {
  59. std::shared_ptr<de::ExecutionTree> tree = std::make_shared<de::ExecutionTree>();
  60. for (int i = 0; i < ops.size(); i++) {
  61. tree->AssociateNode(ops[i]);
  62. if (i > 0) {
  63. ops[i]->AddChild(ops[i - 1]);
  64. }
  65. if (i == ops.size() - 1) {
  66. tree->AssignRoot(ops[i]);
  67. }
  68. }
  69. return tree;
  70. }
  71. TEST_F(MindDataTestBatchOp, TestSimpleBatch) {
  72. std::string schema_file = datasets_root_path_ + "/testBatchDataset";
  73. bool success = false;
  74. auto tree = Build({Storage(schema_file), Batch(12)});
  75. tree->Prepare();
  76. Status rc = tree->Launch();
  77. if (rc.IsError()) {
  78. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  79. } else {
  80. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  81. de::DatasetIterator di(tree);
  82. TensorMap tensor_map;
  83. rc = di.GetNextAsMap(&tensor_map);
  84. EXPECT_TRUE(rc.IsOk());
  85. std::shared_ptr<de::Tensor> t;
  86. rc = de::Tensor::CreateTensor(&t,
  87. TensorImpl::kFlexible, de::TensorShape({12, 1}),
  88. de::DataType(DataType::DE_INT64),
  89. (unsigned char *) payload);
  90. EXPECT_TRUE(rc.IsOk());
  91. // verify the actual data in Tensor is correct
  92. EXPECT_EQ(*t == *tensor_map["col_sint64"], true);
  93. // change what's in Tensor and verify this time the data is incorrect1;
  94. EXPECT_EQ(*t == *tensor_map["col_sint16"], false);
  95. rc = di.GetNextAsMap(&tensor_map);
  96. EXPECT_TRUE(rc.IsOk());
  97. if (tensor_map.size() == 0) {
  98. success = true;
  99. }
  100. }
  101. EXPECT_EQ(success, true);
  102. }
  103. TEST_F(MindDataTestBatchOp, TestRepeatBatchDropTrue) {
  104. std::string schema_file = datasets_root_path_ + "/testBatchDataset";
  105. bool success = false;
  106. auto tree = Build({Storage(schema_file), Repeat(2), Batch(7, true, 99)});
  107. tree->Prepare();
  108. Status rc = tree->Launch();
  109. if (rc.IsError()) {
  110. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  111. } else {
  112. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807,
  113. -9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  114. de::DatasetIterator di(tree);
  115. std::shared_ptr<de::Tensor> t1, t2, t3;
  116. rc = de::Tensor::CreateTensor(&t1,
  117. TensorImpl::kFlexible, de::TensorShape({7, 1}),
  118. de::DataType(DataType::DE_INT64),
  119. (unsigned char *) payload);
  120. EXPECT_TRUE(rc.IsOk());
  121. rc = de::Tensor::CreateTensor(&t2,
  122. TensorImpl::kFlexible, de::TensorShape({7, 1}),
  123. de::DataType(DataType::DE_INT64),
  124. (unsigned char *) (payload + 7));
  125. EXPECT_TRUE(rc.IsOk());
  126. rc = de::Tensor::CreateTensor(&t3,
  127. TensorImpl::kFlexible, de::TensorShape({7, 1}),
  128. de::DataType(DataType::DE_INT64),
  129. (unsigned char *) (payload + 2));
  130. EXPECT_TRUE(rc.IsOk());
  131. TensorMap tensor_map;
  132. rc = di.GetNextAsMap(&tensor_map);
  133. EXPECT_TRUE(rc.IsOk());
  134. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // first call to getNext()
  135. rc = di.GetNextAsMap(&tensor_map);
  136. EXPECT_TRUE(rc.IsOk());
  137. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // second call to getNext()
  138. rc = di.GetNextAsMap(&tensor_map);
  139. EXPECT_TRUE(rc.IsOk());
  140. EXPECT_EQ(*t3 == *(tensor_map["col_sint64"]), true); // third call to getNext()
  141. rc = di.GetNextAsMap(&tensor_map);
  142. EXPECT_TRUE(rc.IsOk());
  143. if (tensor_map.size() == 0) {
  144. success = true;
  145. }
  146. }
  147. EXPECT_EQ(success, true);
  148. }
  149. TEST_F(MindDataTestBatchOp, TestRepeatBatchDropFalse) {
  150. std::string schema_file = datasets_root_path_ + "/testBatchDataset";
  151. bool success = false;
  152. auto tree = Build({Storage(schema_file), Repeat(2), Batch(7, false, 99)});
  153. tree->Prepare();
  154. Status rc = tree->Launch();
  155. if (rc.IsError()) {
  156. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  157. } else {
  158. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807,
  159. -9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  160. de::DatasetIterator di(tree);
  161. std::shared_ptr<de::Tensor> t1, t2, t3, t4;
  162. rc = de::Tensor::CreateTensor(&t1,
  163. TensorImpl::kFlexible, de::TensorShape({7, 1}),
  164. de::DataType(DataType::DE_INT64),
  165. (unsigned char *) payload);
  166. EXPECT_TRUE(rc.IsOk());
  167. rc = de::Tensor::CreateTensor(&t2,
  168. TensorImpl::kFlexible, de::TensorShape({7, 1}),
  169. de::DataType(DataType::DE_INT64),
  170. (unsigned char *) (payload + 7));
  171. EXPECT_TRUE(rc.IsOk());
  172. rc = de::Tensor::CreateTensor(&t3,
  173. TensorImpl::kFlexible, de::TensorShape({7, 1}),
  174. de::DataType(DataType::DE_INT64),
  175. (unsigned char *) (payload + 2));
  176. EXPECT_TRUE(rc.IsOk());
  177. rc = de::Tensor::CreateTensor(&t4,
  178. TensorImpl::kFlexible, de::TensorShape({3, 1}),
  179. de::DataType(DataType::DE_INT64),
  180. (unsigned char *) (payload + 9));
  181. EXPECT_TRUE(rc.IsOk());
  182. TensorMap tensor_map;
  183. rc = di.GetNextAsMap(&tensor_map);
  184. EXPECT_TRUE(rc.IsOk());
  185. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // first call to getNext()
  186. rc = di.GetNextAsMap(&tensor_map);
  187. EXPECT_TRUE(rc.IsOk());
  188. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // second call to getNext()
  189. rc = di.GetNextAsMap(&tensor_map);
  190. EXPECT_TRUE(rc.IsOk());
  191. EXPECT_EQ(*t3 == *(tensor_map["col_sint64"]), true); // third call to getNext()
  192. rc = di.GetNextAsMap(&tensor_map);
  193. EXPECT_TRUE(rc.IsOk());
  194. EXPECT_EQ(*t4 == *(tensor_map["col_sint64"]), true); // last call to getNext()
  195. rc = di.GetNextAsMap(&tensor_map);
  196. EXPECT_TRUE(rc.IsOk());
  197. if (tensor_map.size() == 0) {
  198. success = true;
  199. }
  200. }
  201. EXPECT_EQ(success, true);
  202. }
  203. TEST_F(MindDataTestBatchOp, TestBatchDropFalseRepeat) {
  204. std::string schema_file = datasets_root_path_ + "/testBatchDataset";
  205. bool success = false;
  206. auto tree = Build({Storage(schema_file), Batch(7, false, 99), Repeat(2)});
  207. tree->Prepare();
  208. Status rc = tree->Launch();
  209. if (rc.IsError()) {
  210. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  211. } else {
  212. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807,
  213. -9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  214. de::DatasetIterator di(tree);
  215. std::shared_ptr<de::Tensor> t1, t2;
  216. rc = de::Tensor::CreateTensor(&t1,
  217. TensorImpl::kFlexible, de::TensorShape({7, 1}),
  218. de::DataType(DataType::DE_INT64),
  219. (unsigned char *) payload);
  220. EXPECT_TRUE(rc.IsOk());
  221. rc = de::Tensor::CreateTensor(&t2,
  222. TensorImpl::kFlexible, de::TensorShape({5, 1}),
  223. de::DataType(DataType::DE_INT64),
  224. (unsigned char *) (payload + 7));
  225. EXPECT_TRUE(rc.IsOk());
  226. TensorMap tensor_map;
  227. rc = di.GetNextAsMap(&tensor_map);
  228. EXPECT_TRUE(rc.IsOk());
  229. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // first call to getNext()
  230. rc = di.GetNextAsMap(&tensor_map);
  231. EXPECT_TRUE(rc.IsOk());
  232. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // second call to getNext()
  233. rc = di.GetNextAsMap(&tensor_map);
  234. EXPECT_TRUE(rc.IsOk());
  235. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // third call to getNext()
  236. rc = di.GetNextAsMap(&tensor_map);
  237. EXPECT_TRUE(rc.IsOk());
  238. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // last call to getNext()
  239. rc = di.GetNextAsMap(&tensor_map);
  240. EXPECT_TRUE(rc.IsOk());
  241. if (tensor_map.size() == 0) {
  242. success = true;
  243. }
  244. }
  245. EXPECT_EQ(success, true);
  246. }
  247. TEST_F(MindDataTestBatchOp, TestBatchDropTrueRepeat) {
  248. std::string schema_file = datasets_root_path_ + "/testBatchDataset";
  249. bool success = false;
  250. auto tree = Build({Storage(schema_file), Batch(5, true, 99), Repeat(2)});
  251. tree->Prepare();
  252. Status rc = tree->Launch();
  253. if (rc.IsError()) {
  254. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  255. } else {
  256. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807,
  257. -9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  258. de::DatasetIterator di(tree);
  259. std::shared_ptr<de::Tensor> t1, t2;
  260. rc = de::Tensor::CreateTensor(&t1,
  261. TensorImpl::kFlexible, de::TensorShape({5, 1}),
  262. de::DataType(DataType::DE_INT64),
  263. (unsigned char *) payload);
  264. EXPECT_TRUE(rc.IsOk());
  265. rc = de::Tensor::CreateTensor(&t2,
  266. TensorImpl::kFlexible, de::TensorShape({5, 1}),
  267. de::DataType(DataType::DE_INT64),
  268. (unsigned char *) (payload + 5));
  269. EXPECT_TRUE(rc.IsOk());
  270. TensorMap tensor_map;
  271. rc = di.GetNextAsMap(&tensor_map);
  272. EXPECT_TRUE(rc.IsOk());
  273. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // first call to getNext()
  274. rc = di.GetNextAsMap(&tensor_map);
  275. EXPECT_TRUE(rc.IsOk());
  276. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // second call to getNext()
  277. rc = di.GetNextAsMap(&tensor_map);
  278. EXPECT_TRUE(rc.IsOk());
  279. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // third call to getNext()
  280. rc = di.GetNextAsMap(&tensor_map);
  281. EXPECT_TRUE(rc.IsOk());
  282. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // last call to getNext()
  283. rc = di.GetNextAsMap(&tensor_map);
  284. EXPECT_TRUE(rc.IsOk());
  285. if (tensor_map.size() == 0) {
  286. success = true;
  287. }
  288. }
  289. EXPECT_EQ(success, true);
  290. }