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

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