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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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::LogStream;
  32. using mindspore::ExceptionType::NoExceptionType;
  33. using mindspore::MsLogLevel::ERROR;
  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, TensorImpl::kFlexible, de::TensorShape({12, 1}), de::DataType(DataType::DE_INT64),
  87. (unsigned char *)payload);
  88. EXPECT_TRUE(rc.IsOk());
  89. // verify the actual data in Tensor is correct
  90. EXPECT_EQ(*t == *tensor_map["col_sint64"], true);
  91. // change what's in Tensor and verify this time the data is incorrect1;
  92. EXPECT_EQ(*t == *tensor_map["col_sint16"], false);
  93. rc = di.GetNextAsMap(&tensor_map);
  94. EXPECT_TRUE(rc.IsOk());
  95. if (tensor_map.size() == 0) {
  96. success = true;
  97. }
  98. }
  99. EXPECT_EQ(success, true);
  100. }
  101. TEST_F(MindDataTestBatchOp, TestRepeatBatchDropTrue) {
  102. std::string schema_file = datasets_root_path_ + "/testBatchDataset";
  103. bool success = false;
  104. auto tree = Build({Storage(schema_file), Repeat(2), Batch(7, true, 99)});
  105. tree->Prepare();
  106. Status rc = tree->Launch();
  107. if (rc.IsError()) {
  108. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  109. } else {
  110. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807,
  111. -9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  112. de::DatasetIterator di(tree);
  113. std::shared_ptr<de::Tensor> t1, t2, t3;
  114. rc = de::Tensor::CreateTensor(&t1, TensorImpl::kFlexible, de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  115. (unsigned char *)payload);
  116. EXPECT_TRUE(rc.IsOk());
  117. rc = de::Tensor::CreateTensor(&t2, TensorImpl::kFlexible, de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  118. (unsigned char *)(payload + 7));
  119. EXPECT_TRUE(rc.IsOk());
  120. rc = de::Tensor::CreateTensor(&t3, TensorImpl::kFlexible, de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  121. (unsigned char *)(payload + 2));
  122. EXPECT_TRUE(rc.IsOk());
  123. TensorMap tensor_map;
  124. rc = di.GetNextAsMap(&tensor_map);
  125. EXPECT_TRUE(rc.IsOk());
  126. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // first call to getNext()
  127. rc = di.GetNextAsMap(&tensor_map);
  128. EXPECT_TRUE(rc.IsOk());
  129. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // second call to getNext()
  130. rc = di.GetNextAsMap(&tensor_map);
  131. EXPECT_TRUE(rc.IsOk());
  132. EXPECT_EQ(*t3 == *(tensor_map["col_sint64"]), true); // third call to getNext()
  133. rc = di.GetNextAsMap(&tensor_map);
  134. EXPECT_TRUE(rc.IsOk());
  135. if (tensor_map.size() == 0) {
  136. success = true;
  137. }
  138. }
  139. EXPECT_EQ(success, true);
  140. }
  141. TEST_F(MindDataTestBatchOp, TestRepeatBatchDropFalse) {
  142. std::string schema_file = datasets_root_path_ + "/testBatchDataset";
  143. bool success = false;
  144. auto tree = Build({Storage(schema_file), Repeat(2), Batch(7, false, 99)});
  145. tree->Prepare();
  146. Status rc = tree->Launch();
  147. if (rc.IsError()) {
  148. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  149. } else {
  150. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807,
  151. -9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  152. de::DatasetIterator di(tree);
  153. std::shared_ptr<de::Tensor> t1, t2, t3, t4;
  154. rc = de::Tensor::CreateTensor(&t1, TensorImpl::kFlexible, de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  155. (unsigned char *)payload);
  156. EXPECT_TRUE(rc.IsOk());
  157. rc = de::Tensor::CreateTensor(&t2, TensorImpl::kFlexible, de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  158. (unsigned char *)(payload + 7));
  159. EXPECT_TRUE(rc.IsOk());
  160. rc = de::Tensor::CreateTensor(&t3, TensorImpl::kFlexible, de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  161. (unsigned char *)(payload + 2));
  162. EXPECT_TRUE(rc.IsOk());
  163. rc = de::Tensor::CreateTensor(&t4, TensorImpl::kFlexible, de::TensorShape({3, 1}), de::DataType(DataType::DE_INT64),
  164. (unsigned char *)(payload + 9));
  165. EXPECT_TRUE(rc.IsOk());
  166. TensorMap tensor_map;
  167. rc = di.GetNextAsMap(&tensor_map);
  168. EXPECT_TRUE(rc.IsOk());
  169. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // first call to getNext()
  170. rc = di.GetNextAsMap(&tensor_map);
  171. EXPECT_TRUE(rc.IsOk());
  172. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // second call to getNext()
  173. rc = di.GetNextAsMap(&tensor_map);
  174. EXPECT_TRUE(rc.IsOk());
  175. EXPECT_EQ(*t3 == *(tensor_map["col_sint64"]), true); // third call to getNext()
  176. rc = di.GetNextAsMap(&tensor_map);
  177. EXPECT_TRUE(rc.IsOk());
  178. EXPECT_EQ(*t4 == *(tensor_map["col_sint64"]), true); // last call to getNext()
  179. rc = di.GetNextAsMap(&tensor_map);
  180. EXPECT_TRUE(rc.IsOk());
  181. if (tensor_map.size() == 0) {
  182. success = true;
  183. }
  184. }
  185. EXPECT_EQ(success, true);
  186. }
  187. TEST_F(MindDataTestBatchOp, TestBatchDropFalseRepeat) {
  188. std::string schema_file = datasets_root_path_ + "/testBatchDataset";
  189. bool success = false;
  190. auto tree = Build({Storage(schema_file), Batch(7, false, 99), Repeat(2)});
  191. tree->Prepare();
  192. Status rc = tree->Launch();
  193. if (rc.IsError()) {
  194. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  195. } else {
  196. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807,
  197. -9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  198. de::DatasetIterator di(tree);
  199. std::shared_ptr<de::Tensor> t1, t2;
  200. rc = de::Tensor::CreateTensor(&t1, TensorImpl::kFlexible, de::TensorShape({7, 1}), de::DataType(DataType::DE_INT64),
  201. (unsigned char *)payload);
  202. EXPECT_TRUE(rc.IsOk());
  203. rc = de::Tensor::CreateTensor(&t2, TensorImpl::kFlexible, de::TensorShape({5, 1}), de::DataType(DataType::DE_INT64),
  204. (unsigned char *)(payload + 7));
  205. EXPECT_TRUE(rc.IsOk());
  206. TensorMap tensor_map;
  207. rc = di.GetNextAsMap(&tensor_map);
  208. EXPECT_TRUE(rc.IsOk());
  209. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // first call to getNext()
  210. rc = di.GetNextAsMap(&tensor_map);
  211. EXPECT_TRUE(rc.IsOk());
  212. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // second call to getNext()
  213. rc = di.GetNextAsMap(&tensor_map);
  214. EXPECT_TRUE(rc.IsOk());
  215. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // third call to getNext()
  216. rc = di.GetNextAsMap(&tensor_map);
  217. EXPECT_TRUE(rc.IsOk());
  218. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // last call to getNext()
  219. rc = di.GetNextAsMap(&tensor_map);
  220. EXPECT_TRUE(rc.IsOk());
  221. if (tensor_map.size() == 0) {
  222. success = true;
  223. }
  224. }
  225. EXPECT_EQ(success, true);
  226. }
  227. TEST_F(MindDataTestBatchOp, TestBatchDropTrueRepeat) {
  228. std::string schema_file = datasets_root_path_ + "/testBatchDataset";
  229. bool success = false;
  230. auto tree = Build({Storage(schema_file), Batch(5, true, 99), Repeat(2)});
  231. tree->Prepare();
  232. Status rc = tree->Launch();
  233. if (rc.IsError()) {
  234. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  235. } else {
  236. int64_t payload[] = {-9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807,
  237. -9223372036854775807 - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9223372036854775807};
  238. de::DatasetIterator di(tree);
  239. std::shared_ptr<de::Tensor> t1, t2;
  240. rc = de::Tensor::CreateTensor(&t1, TensorImpl::kFlexible, de::TensorShape({5, 1}), de::DataType(DataType::DE_INT64),
  241. (unsigned char *)payload);
  242. EXPECT_TRUE(rc.IsOk());
  243. rc = de::Tensor::CreateTensor(&t2, TensorImpl::kFlexible, de::TensorShape({5, 1}), de::DataType(DataType::DE_INT64),
  244. (unsigned char *)(payload + 5));
  245. EXPECT_TRUE(rc.IsOk());
  246. TensorMap tensor_map;
  247. rc = di.GetNextAsMap(&tensor_map);
  248. EXPECT_TRUE(rc.IsOk());
  249. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // first call to getNext()
  250. rc = di.GetNextAsMap(&tensor_map);
  251. EXPECT_TRUE(rc.IsOk());
  252. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // second call to getNext()
  253. rc = di.GetNextAsMap(&tensor_map);
  254. EXPECT_TRUE(rc.IsOk());
  255. EXPECT_EQ(*t1 == *(tensor_map["col_sint64"]), true); // third call to getNext()
  256. rc = di.GetNextAsMap(&tensor_map);
  257. EXPECT_TRUE(rc.IsOk());
  258. EXPECT_EQ(*t2 == *(tensor_map["col_sint64"]), true); // last call to getNext()
  259. rc = di.GetNextAsMap(&tensor_map);
  260. EXPECT_TRUE(rc.IsOk());
  261. if (tensor_map.size() == 0) {
  262. success = true;
  263. }
  264. }
  265. EXPECT_EQ(success, true);
  266. }
  267. TEST_F(MindDataTestBatchOp, TestSimpleBatchPadding) {
  268. std::string schema_file = datasets_root_path_ + "/testBatchDataset";
  269. std::shared_ptr<BatchOp> op;
  270. PadInfo m;
  271. std::shared_ptr<Tensor> pad_value;
  272. Tensor::CreateTensor(&pad_value, TensorImpl::kFlexible, TensorShape::CreateScalar(), DataType(DataType::DE_FLOAT32));
  273. pad_value->SetItemAt<float>({}, -1);
  274. m.insert({"col_1d", std::make_pair(TensorShape({4}), pad_value)});
  275. de::BatchOp::Builder(12).SetDrop(false).SetPaddingMap(m, true).Build(&op);
  276. auto tree = Build({Storage(schema_file), op});
  277. tree->Prepare();
  278. Status rc = tree->Launch();
  279. if (rc.IsError()) {
  280. MS_LOG(ERROR) << "Return code error detected during tree launch: " << rc.ToString() << ".";
  281. } else {
  282. int64_t payload[] = {-9223372036854775807 - 1,
  283. 1,
  284. -1,
  285. -1,
  286. 2,
  287. 3,
  288. -1,
  289. -1,
  290. 4,
  291. 5,
  292. -1,
  293. -1,
  294. 6,
  295. 7,
  296. -1,
  297. -1,
  298. 8,
  299. 9,
  300. -1,
  301. -1,
  302. 10,
  303. 11,
  304. -1,
  305. -1,
  306. 12,
  307. 13,
  308. -1,
  309. -1,
  310. 14,
  311. 15,
  312. -1,
  313. -1,
  314. 16,
  315. 17,
  316. -1,
  317. -1,
  318. 18,
  319. 19,
  320. -1,
  321. -1,
  322. 20,
  323. 21,
  324. -1,
  325. -1,
  326. 22,
  327. 23,
  328. -1,
  329. -1};
  330. std::shared_ptr<de::Tensor> t;
  331. rc = de::Tensor::CreateTensor(&t, TensorImpl::kFlexible, de::TensorShape({12, 4}), de::DataType(DataType::DE_INT64),
  332. (unsigned char *)payload);
  333. de::DatasetIterator di(tree);
  334. TensorMap tensor_map;
  335. rc = di.GetNextAsMap(&tensor_map);
  336. EXPECT_TRUE((*t) == (*(tensor_map["col_1d"])));
  337. rc = di.GetNextAsMap(&tensor_map);
  338. EXPECT_TRUE(tensor_map.size() == 0);
  339. EXPECT_TRUE(rc.IsOk());
  340. }
  341. }