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

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