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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * Copyright 2019-2021 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 <memory>
  17. #include <string>
  18. #include "minddata/dataset/core/client.h"
  19. #include "common/common.h"
  20. #include "gtest/gtest.h"
  21. #include "utils/log_adapter.h"
  22. #include "securec.h"
  23. #include "minddata/dataset/util/status.h"
  24. namespace common = mindspore::common;
  25. namespace de = mindspore::dataset;
  26. using namespace mindspore::dataset;
  27. using mindspore::LogStream;
  28. using mindspore::ExceptionType::NoExceptionType;
  29. using mindspore::MsLogLevel::ERROR;
  30. class MindDataTestBatchOp : public UT::DatasetOpTesting {
  31. protected:
  32. };
  33. std::shared_ptr<de::BatchOp> Batch(int32_t batch_size = 1, bool drop = false) {
  34. Status rc;
  35. std::shared_ptr<de::BatchOp> op;
  36. rc = de::BatchOp::Builder(batch_size).SetDrop(drop).Build(&op);
  37. EXPECT_TRUE(rc.IsOk());
  38. return op;
  39. }
  40. std::shared_ptr<de::RepeatOp> Repeat(int repeat_cnt = 1) {
  41. de::RepeatOp::Builder builder(repeat_cnt);
  42. std::shared_ptr<de::RepeatOp> op;
  43. Status rc = builder.Build(&op);
  44. EXPECT_TRUE(rc.IsOk());
  45. return op;
  46. }
  47. std::shared_ptr<de::TFReaderOp> TFReader(std::string schema, int num_works = 8) {
  48. std::shared_ptr<de::TFReaderOp> so;
  49. de::TFReaderOp::Builder builder;
  50. builder.SetDatasetFilesList({schema}).SetNumWorkers(num_works);
  51. Status rc = builder.Build(&so);
  52. return so;
  53. }
  54. std::shared_ptr<de::ExecutionTree> Build(std::vector<std::shared_ptr<de::DatasetOp>> ops) {
  55. std::shared_ptr<de::ExecutionTree> tree = std::make_shared<de::ExecutionTree>();
  56. for (int i = 0; i < ops.size(); i++) {
  57. tree->AssociateNode(ops[i]);
  58. if (i > 0) {
  59. ops[i]->AddChild(ops[i - 1]);
  60. }
  61. if (i == ops.size() - 1) {
  62. tree->AssignRoot(ops[i]);
  63. }
  64. }
  65. return tree;
  66. }
  67. TEST_F(MindDataTestBatchOp, TestSimpleBatch) {
  68. std::string schema_file = datasets_root_path_ + "/testBatchDataset/test.data";
  69. bool success = false;
  70. const std::shared_ptr<de::BatchOp> &op = Batch(12);
  71. EXPECT_EQ(op->Name(), "BatchOp");
  72. auto tree = Build({TFReader(schema_file), op});
  73. tree->Prepare();
  74. Status rc = tree->Launch();
  75. if (rc.IsError()) {
  76. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  77. } else {
  78. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  79. de::DatasetIterator di(tree);
  80. TensorMap tensor_map;
  81. rc = di.GetNextAsMap(&tensor_map);
  82. EXPECT_TRUE(rc.IsOk());
  83. std::shared_ptr<de::Tensor> t;
  84. rc = de::Tensor::CreateFromMemory(de::TensorShape({12, 1}), de::DataType(DataType::DE_INT64),
  85. (unsigned char *)payload, &t);
  86. EXPECT_TRUE(rc.IsOk());
  87. // verify the actual data in Tensor is correct
  88. EXPECT_EQ(*t == *tensor_map["col_sint64"], true);
  89. // change what's in Tensor and verify this time the data is incorrect1;
  90. EXPECT_EQ(*t == *tensor_map["col_sint16"], false);
  91. rc = di.GetNextAsMap(&tensor_map);
  92. EXPECT_TRUE(rc.IsOk());
  93. if (tensor_map.size() == 0) {
  94. success = true;
  95. }
  96. }
  97. EXPECT_EQ(success, true);
  98. }
  99. TEST_F(MindDataTestBatchOp, TestRepeatBatchDropTrue) {
  100. std::string schema_file = datasets_root_path_ + "/testBatchDataset/test.data";
  101. bool success = false;
  102. auto op1 = TFReader(schema_file);
  103. auto op2 = Repeat(2);
  104. auto op3 = Batch(7, true);
  105. op1->set_total_repeats(2);
  106. op1->set_num_repeats_per_epoch(2);
  107. auto tree = Build({op1, op2, op3});
  108. tree->Prepare();
  109. Status rc = tree->Launch();
  110. if (rc.IsError()) {
  111. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  112. } else {
  113. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807,
  114. -9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  115. de::DatasetIterator di(tree);
  116. std::shared_ptr<de::Tensor> t1, t2, t3;
  117. rc = de::Tensor::CreateFromMemory(de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  118. (unsigned char *)payload, &t1);
  119. EXPECT_TRUE(rc.IsOk());
  120. rc = de::Tensor::CreateFromMemory(de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  121. (unsigned char *)(payload + 7), &t2);
  122. EXPECT_TRUE(rc.IsOk());
  123. rc = de::Tensor::CreateFromMemory(de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  124. (unsigned char *)(payload + 2), &t3);
  125. EXPECT_TRUE(rc.IsOk());
  126. TensorMap tensor_map;
  127. rc = di.GetNextAsMap(&tensor_map);
  128. EXPECT_TRUE(rc.IsOk());
  129. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // first call to getNext()
  130. rc = di.GetNextAsMap(&tensor_map);
  131. EXPECT_TRUE(rc.IsOk());
  132. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // second call to getNext()
  133. rc = di.GetNextAsMap(&tensor_map);
  134. EXPECT_TRUE(rc.IsOk());
  135. EXPECT_EQ(*t3 == *(tensor_map["col_sint64"]), true); // third call to getNext()
  136. rc = di.GetNextAsMap(&tensor_map);
  137. EXPECT_TRUE(rc.IsOk());
  138. if (tensor_map.size() == 0) {
  139. success = true;
  140. }
  141. }
  142. EXPECT_EQ(success, true);
  143. }
  144. TEST_F(MindDataTestBatchOp, TestRepeatBatchDropFalse) {
  145. std::string schema_file = datasets_root_path_ + "/testBatchDataset/test.data";
  146. bool success = false;
  147. auto op1 = TFReader(schema_file);
  148. auto op2 = Repeat(2);
  149. auto op3 = Batch(7, false);
  150. op1->set_total_repeats(2);
  151. op1->set_num_repeats_per_epoch(2);
  152. auto tree = Build({op1, op2, op3});
  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::CreateFromMemory(de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  163. (unsigned char *)payload, &t1);
  164. EXPECT_TRUE(rc.IsOk());
  165. rc = de::Tensor::CreateFromMemory(de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  166. (unsigned char *)(payload + 7), &t2);
  167. EXPECT_TRUE(rc.IsOk());
  168. rc = de::Tensor::CreateFromMemory(de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  169. (unsigned char *)(payload + 2), &t3);
  170. EXPECT_TRUE(rc.IsOk());
  171. rc = de::Tensor::CreateFromMemory(de::TensorShape({3, 1}), de::DataType(DataType::DE_INT64),
  172. (unsigned char *)(payload + 9), &t4);
  173. EXPECT_TRUE(rc.IsOk());
  174. TensorMap tensor_map;
  175. rc = di.GetNextAsMap(&tensor_map);
  176. EXPECT_TRUE(rc.IsOk());
  177. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // first call to getNext()
  178. rc = di.GetNextAsMap(&tensor_map);
  179. EXPECT_TRUE(rc.IsOk());
  180. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // second call to getNext()
  181. rc = di.GetNextAsMap(&tensor_map);
  182. EXPECT_TRUE(rc.IsOk());
  183. EXPECT_EQ(*t3 == *(tensor_map["col_sint64"]), true); // third call to getNext()
  184. rc = di.GetNextAsMap(&tensor_map);
  185. EXPECT_TRUE(rc.IsOk());
  186. EXPECT_EQ(*t4 == *(tensor_map["col_sint64"]), true); // last call to getNext()
  187. rc = di.GetNextAsMap(&tensor_map);
  188. EXPECT_TRUE(rc.IsOk());
  189. if (tensor_map.size() == 0) {
  190. success = true;
  191. }
  192. }
  193. EXPECT_EQ(success, true);
  194. }
  195. TEST_F(MindDataTestBatchOp, TestBatchDropFalseRepeat) {
  196. std::string schema_file = datasets_root_path_ + "/testBatchDataset/test.data";
  197. bool success = false;
  198. auto op1 = TFReader(schema_file);
  199. auto op2 = Batch(7, false);
  200. auto op3 = Repeat(2);
  201. op1->set_total_repeats(2);
  202. op1->set_num_repeats_per_epoch(2);
  203. op2->set_total_repeats(2);
  204. op2->set_num_repeats_per_epoch(2);
  205. auto tree = Build({op1, op2, op3});
  206. tree->Prepare();
  207. Status rc = tree->Launch();
  208. if (rc.IsError()) {
  209. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  210. } else {
  211. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807,
  212. -9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  213. de::DatasetIterator di(tree);
  214. std::shared_ptr<de::Tensor> t1, t2;
  215. rc = de::Tensor::CreateFromMemory(de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  216. (unsigned char *)payload, &t1);
  217. EXPECT_TRUE(rc.IsOk());
  218. rc = de::Tensor::CreateFromMemory(de::TensorShape({5, 1}), de::DataType(DataType::DE_INT64),
  219. (unsigned char *)(payload + 7), &t2);
  220. EXPECT_TRUE(rc.IsOk());
  221. TensorMap tensor_map;
  222. rc = di.GetNextAsMap(&tensor_map);
  223. EXPECT_TRUE(rc.IsOk());
  224. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // first call to getNext()
  225. rc = di.GetNextAsMap(&tensor_map);
  226. EXPECT_TRUE(rc.IsOk());
  227. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // second call to getNext()
  228. rc = di.GetNextAsMap(&tensor_map);
  229. EXPECT_TRUE(rc.IsOk());
  230. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // third call to getNext()
  231. rc = di.GetNextAsMap(&tensor_map);
  232. EXPECT_TRUE(rc.IsOk());
  233. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // last call to getNext()
  234. rc = di.GetNextAsMap(&tensor_map);
  235. EXPECT_TRUE(rc.IsOk());
  236. if (tensor_map.size() == 0) {
  237. success = true;
  238. }
  239. }
  240. EXPECT_EQ(success, true);
  241. }
  242. TEST_F(MindDataTestBatchOp, TestBatchDropTrueRepeat) {
  243. std::string schema_file = datasets_root_path_ + "/testBatchDataset/test.data";
  244. bool success = false;
  245. auto op1 = TFReader(schema_file);
  246. auto op2 = Batch(5, true);
  247. auto op3 = Repeat(2);
  248. op1->set_total_repeats(2);
  249. op1->set_num_repeats_per_epoch(2);
  250. op2->set_total_repeats(2);
  251. op2->set_num_repeats_per_epoch(2);
  252. auto tree = Build({op1, op2, op3});
  253. tree->Prepare();
  254. Status rc = tree->Launch();
  255. if (rc.IsError()) {
  256. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  257. } else {
  258. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807,
  259. -9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  260. de::DatasetIterator di(tree);
  261. std::shared_ptr<de::Tensor> t1, t2;
  262. rc = de::Tensor::CreateFromMemory(de::TensorShape({5, 1}), de::DataType(DataType::DE_INT64),
  263. (unsigned char *)payload, &t1);
  264. EXPECT_TRUE(rc.IsOk());
  265. rc = de::Tensor::CreateFromMemory(de::TensorShape({5, 1}), de::DataType(DataType::DE_INT64),
  266. (unsigned char *)(payload + 5), &t2);
  267. EXPECT_TRUE(rc.IsOk());
  268. TensorMap tensor_map;
  269. rc = di.GetNextAsMap(&tensor_map);
  270. EXPECT_TRUE(rc.IsOk());
  271. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // first call to getNext()
  272. rc = di.GetNextAsMap(&tensor_map);
  273. EXPECT_TRUE(rc.IsOk());
  274. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // second call to getNext()
  275. rc = di.GetNextAsMap(&tensor_map);
  276. EXPECT_TRUE(rc.IsOk());
  277. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // third call to getNext()
  278. rc = di.GetNextAsMap(&tensor_map);
  279. EXPECT_TRUE(rc.IsOk());
  280. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // last call to getNext()
  281. rc = di.GetNextAsMap(&tensor_map);
  282. EXPECT_TRUE(rc.IsOk());
  283. if (tensor_map.size() == 0) {
  284. success = true;
  285. }
  286. }
  287. EXPECT_EQ(success, true);
  288. }
  289. TEST_F(MindDataTestBatchOp, TestSimpleBatchPadding) {
  290. std::string schema_file = datasets_root_path_ + "/testBatchDataset/test.data";
  291. std::shared_ptr<BatchOp> op;
  292. PadInfo m;
  293. std::shared_ptr<Tensor> pad_value;
  294. Tensor::CreateEmpty(TensorShape::CreateScalar(), DataType(DataType::DE_FLOAT32), &pad_value);
  295. pad_value->SetItemAt<float>({}, -1);
  296. m.insert({"col_1d", std::make_pair(TensorShape({4}), pad_value)});
  297. de::BatchOp::Builder(12).SetDrop(false).SetPaddingMap(m, true).Build(&op);
  298. auto tree = Build({TFReader(schema_file), op});
  299. tree->Prepare();
  300. Status rc = tree->Launch();
  301. if (rc.IsError()) {
  302. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  303. } else {
  304. int64_t payload[] = {-9223372036854775807 - 1,
  305. 1,
  306. -1,
  307. -1,
  308. 2,
  309. 3,
  310. -1,
  311. -1,
  312. 4,
  313. 5,
  314. -1,
  315. -1,
  316. 6,
  317. 7,
  318. -1,
  319. -1,
  320. 8,
  321. 9,
  322. -1,
  323. -1,
  324. 10,
  325. 11,
  326. -1,
  327. -1,
  328. 12,
  329. 13,
  330. -1,
  331. -1,
  332. 14,
  333. 15,
  334. -1,
  335. -1,
  336. 16,
  337. 17,
  338. -1,
  339. -1,
  340. 18,
  341. 19,
  342. -1,
  343. -1,
  344. 20,
  345. 21,
  346. -1,
  347. -1,
  348. 22,
  349. 23,
  350. -1,
  351. -1};
  352. std::shared_ptr<de::Tensor> t;
  353. rc = de::Tensor::CreateFromMemory(de::TensorShape({12, 4}), de::DataType(DataType::DE_INT64),
  354. (unsigned char *)payload, &t);
  355. de::DatasetIterator di(tree);
  356. TensorMap tensor_map;
  357. rc = di.GetNextAsMap(&tensor_map);
  358. EXPECT_TRUE((*t) == (*(tensor_map["col_1d"])));
  359. rc = di.GetNextAsMap(&tensor_map);
  360. EXPECT_TRUE(tensor_map.size() == 0);
  361. EXPECT_TRUE(rc.IsOk());
  362. }
  363. }