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.

ir_callback_test.cc 16 kB

4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /**
  2. * Copyright 2020-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 <list>
  18. #include "common/common.h"
  19. #include "minddata/dataset/callback/ds_callback.h"
  20. #include "minddata/dataset/core/client.h"
  21. #include "minddata/dataset/engine/datasetops/epoch_ctrl_op.h"
  22. #include "minddata/dataset/engine/datasetops/source/random_data_op.h"
  23. #include "minddata/dataset/engine/tree_adapter.h"
  24. #include "minddata/dataset/include/dataset/datasets.h"
  25. #include "minddata/dataset/include/dataset/transforms.h"
  26. #include "minddata/dataset/kernels/data/no_op.h"
  27. #include "utils/log_adapter.h"
  28. using namespace mindspore::dataset;
  29. using mindspore::LogStream;
  30. using mindspore::MsLogLevel::INFO;
  31. namespace mindspore {
  32. namespace dataset {
  33. namespace test {
  34. class TestCallback : public DSCallback {
  35. public:
  36. TestCallback(int32_t step_size)
  37. : DSCallback(step_size),
  38. begin_(true),
  39. epoch_begin_(true),
  40. step_begin_(true),
  41. end_(false),
  42. epoch_end_(true),
  43. step_end_(true) {
  44. all_names_.reserve(32);
  45. all_step_nums_.reserve(32);
  46. all_ep_nums_.reserve(32);
  47. }
  48. Status DSBegin(const CallbackParam &cb_param) override {
  49. all_names_.push_back("BGN");
  50. all_step_nums_.push_back(cb_param.cur_step_num_);
  51. all_ep_nums_.push_back(cb_param.cur_epoch_num_);
  52. return Status::OK();
  53. }
  54. Status DSEpochBegin(const CallbackParam &cb_param) override {
  55. all_names_.push_back("EPBGN");
  56. all_step_nums_.push_back(cb_param.cur_step_num_);
  57. all_ep_nums_.push_back(cb_param.cur_epoch_num_);
  58. return Status::OK();
  59. }
  60. Status DSNStepBegin(const CallbackParam &cb_param) override {
  61. all_names_.push_back("SPBGN");
  62. all_step_nums_.push_back(cb_param.cur_step_num_);
  63. all_ep_nums_.push_back(cb_param.cur_epoch_num_);
  64. return Status::OK();
  65. }
  66. Status DSEnd(const CallbackParam &cb_param) override {
  67. all_names_.push_back("END");
  68. all_step_nums_.push_back(cb_param.cur_step_num_);
  69. all_ep_nums_.push_back(cb_param.cur_epoch_num_);
  70. return Status::OK();
  71. }
  72. Status DSEpochEnd(const CallbackParam &cb_param) override {
  73. all_names_.push_back("EPEND");
  74. all_step_nums_.push_back(cb_param.cur_step_num_);
  75. all_ep_nums_.push_back(cb_param.cur_epoch_num_);
  76. return Status::OK();
  77. }
  78. Status DSNStepEnd(const CallbackParam &cb_param) override {
  79. all_names_.push_back("SPEND");
  80. all_step_nums_.push_back(cb_param.cur_step_num_);
  81. all_ep_nums_.push_back(cb_param.cur_epoch_num_);
  82. return Status::OK();
  83. }
  84. bool IsBeginNeeded() override { return begin_; }
  85. bool IsEpochBeginNeeded() override { return epoch_begin_; }
  86. bool IsNStepBeginNeeded() override { return step_begin_; }
  87. bool IsEndNeeded() override { return end_; }
  88. bool IsEpochEndNeeded() override { return epoch_end_; }
  89. bool IsNStepEndNeeded() override { return step_end_; }
  90. std::vector<std::string> all_names(size_t len) {
  91. std::vector<std::string> res(all_names_.begin(), all_names_.begin() + len);
  92. std::sort(res.begin(), res.end());
  93. return res;
  94. }
  95. std::vector<int64_t> all_step_nums(size_t len) {
  96. std::vector<int64_t> res(all_step_nums_.begin(), all_step_nums_.begin() + len);
  97. std::sort(res.begin(), res.end());
  98. return res;
  99. }
  100. std::vector<int64_t> all_ep_nums(size_t len) {
  101. std::vector<int64_t> res(all_ep_nums_.begin(), all_ep_nums_.begin() + len);
  102. std::sort(res.begin(), res.end());
  103. return res;
  104. }
  105. // flag for turning callback on and off
  106. bool begin_, epoch_begin_, step_begin_, end_, epoch_end_, step_end_;
  107. // name of the callback function in sequence, BGN, EPBGN, SPB, END, EPEND, SPEND
  108. std::vector<std::string> all_names_;
  109. std::vector<int64_t> all_step_nums_, all_ep_nums_;
  110. };
  111. } // namespace test
  112. } // namespace dataset
  113. } // namespace mindspore
  114. class MindDataTestCallback : public UT::DatasetOpTesting {
  115. public:
  116. void SetUp() override {
  117. DatasetOpTesting::SetUp();
  118. GlobalInit();
  119. }
  120. void TestBasicCallback(std::shared_ptr<ExecutionTree> tree, std::shared_ptr<DatasetOp> callback_node,
  121. int32_t step_size) {
  122. // config callback
  123. Status rc;
  124. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(step_size);
  125. std::shared_ptr<DSCallback> cb1 = tst_cb;
  126. std::vector<std::shared_ptr<DSCallback>> cbs = {};
  127. cbs.push_back(cb1);
  128. callback_node->AddCallbacks(std::move(cbs));
  129. ASSERT_OK(tree->Prepare());
  130. ASSERT_OK(tree->Launch());
  131. // Start the loop of reading tensors from our pipeline
  132. DatasetIterator di(tree);
  133. TensorMap tensor_map;
  134. rc = di.GetNextAsMap(&tensor_map);
  135. EXPECT_TRUE(rc.IsOk());
  136. while (!tensor_map.empty()) {
  137. rc = di.GetNextAsMap(&tensor_map);
  138. EXPECT_TRUE(rc.IsOk());
  139. }
  140. std::vector<std::string> callback_names = {"BGN", "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND"};
  141. std::sort(callback_names.begin(), callback_names.end());
  142. std::vector<int64_t> all_steps = {0, 0, 1, 1, 65, 65, 88};
  143. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 1, 1};
  144. // doing resize to make sure no unexpected epoch_end or extra epoch_begin is called
  145. size_t len = 7;
  146. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  147. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  148. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  149. }
  150. std::vector<std::shared_ptr<DatasetOp>> GenerateNodes() {
  151. // config leaf_op, use random_data to avoid I/O
  152. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  153. TensorShape shape({}); // empty shape is a 1-value scalar Tensor
  154. ColDescriptor col("label", DataType(DataType::DE_UINT32), TensorImpl::kFlexible, 0, &shape);
  155. EXPECT_OK(schema->AddColumn(col));
  156. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  157. int32_t op_connector_size = config_manager->op_connector_size();
  158. int32_t num_workers = config_manager->num_parallel_workers();
  159. int32_t num_rows = 44;
  160. std::shared_ptr<RandomDataOp> leaf =
  161. std::make_shared<RandomDataOp>(num_workers, op_connector_size, num_rows, std::move(schema));
  162. // config mapOp
  163. std::vector<std::string> input_columns = {"label"};
  164. std::vector<std::string> output_columns = {};
  165. std::vector<std::shared_ptr<TensorOp>> op_list;
  166. std::shared_ptr<TensorOp> my_no_op = std::make_shared<NoOp>();
  167. op_list.push_back(my_no_op);
  168. std::shared_ptr<MapOp> map_op =
  169. std::make_shared<MapOp>(input_columns, output_columns, std::move(op_list), num_workers, op_connector_size);
  170. PadInfo pad_map;
  171. std::shared_ptr<BatchOp> batch_op =
  172. std::make_shared<BatchOp>(1, false, false, op_connector_size, num_workers, std::vector<std::string>{}, pad_map);
  173. // config RepeatOp
  174. int32_t num_repeats = 2;
  175. std::shared_ptr<RepeatOp> repeat_op = std::make_shared<RepeatOp>(num_repeats);
  176. // start build then launch tree
  177. leaf->SetTotalRepeats(num_repeats);
  178. leaf->SetNumRepeatsPerEpoch(num_repeats);
  179. map_op->SetTotalRepeats(num_repeats);
  180. map_op->SetNumRepeatsPerEpoch(num_repeats);
  181. batch_op->SetTotalRepeats(num_repeats);
  182. batch_op->SetNumRepeatsPerEpoch(num_repeats);
  183. return {leaf, map_op, batch_op, repeat_op};
  184. }
  185. };
  186. /// Feature: Callback
  187. /// Description: Test callbacks with mappable dataset (RandomDataset)
  188. /// Expectation: number and order of callbacks generated are correct
  189. TEST_F(MindDataTestCallback, TestBasicCallback) {
  190. MS_LOG(INFO) << "Doing: MindDataTestCallback-TestBasicCallback";
  191. // Test Mapop
  192. auto nodes = GenerateNodes();
  193. auto tree = Build(nodes);
  194. TestBasicCallback(tree, nodes[1], 64);
  195. // Test LeafOp
  196. nodes = GenerateNodes();
  197. tree = Build(nodes);
  198. TestBasicCallback(tree, nodes[0], 64);
  199. // Test BatchOp
  200. nodes = GenerateNodes();
  201. tree = Build(nodes);
  202. TestBasicCallback(tree, nodes[2], 64);
  203. }
  204. TEST_F(MindDataTestCallback, TestMultiEpochCallback) {
  205. MS_LOG(INFO) << "Doing: MindDataTestCallback-TestMultiEpochCallback";
  206. // config callback
  207. Status rc;
  208. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(4);
  209. std::shared_ptr<DSCallback> cb1 = tst_cb;
  210. // config leaf_op, use random_data to avoid I/O
  211. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  212. int32_t op_connector_size = config_manager->op_connector_size();
  213. int32_t num_workers = config_manager->num_parallel_workers();
  214. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  215. TensorShape shape({}); // empty shape is a 1-value scalar Tensor
  216. ColDescriptor col("label", DataType(DataType::DE_UINT32), TensorImpl::kFlexible, 0, &shape);
  217. ASSERT_OK(schema->AddColumn(col));
  218. std::shared_ptr<RandomDataOp> leaf = std::make_shared<RandomDataOp>(4, op_connector_size, 4, std::move(schema));
  219. // config mapOp
  220. std::vector<std::string> input_columns = {"label"};
  221. std::vector<std::string> output_columns = {};
  222. std::vector<std::shared_ptr<TensorOp>> op_list;
  223. std::shared_ptr<TensorOp> my_no_op = std::make_shared<NoOp>();
  224. op_list.push_back(my_no_op);
  225. std::shared_ptr<MapOp> map_op =
  226. std::make_shared<MapOp>(input_columns, output_columns, std::move(op_list), num_workers, op_connector_size);
  227. std::vector<std::shared_ptr<DSCallback>> cbs = {};
  228. cbs.push_back(cb1);
  229. map_op->AddCallbacks(std::move(cbs));
  230. EXPECT_TRUE(rc.IsOk());
  231. int32_t num_repeats = 2;
  232. // config RepeatOp
  233. std::shared_ptr<RepeatOp> repeat_op = std::make_shared<RepeatOp>(num_repeats);
  234. // config EpochCtrlOp
  235. std::shared_ptr<EpochCtrlOp> epoch_ctrl_op = std::make_shared<EpochCtrlOp>(num_repeats);
  236. // start build then launch tree
  237. leaf->SetTotalRepeats(4);
  238. leaf->SetNumRepeatsPerEpoch(2);
  239. map_op->SetTotalRepeats(4);
  240. map_op->SetNumRepeatsPerEpoch(2);
  241. std::shared_ptr<ExecutionTree> tree = Build({leaf, map_op, repeat_op, epoch_ctrl_op});
  242. rc = tree->Prepare();
  243. EXPECT_TRUE(rc.IsOk());
  244. rc = tree->Launch();
  245. EXPECT_TRUE(rc.IsOk());
  246. // Start the loop of reading tensors from our pipeline
  247. DatasetIterator di(tree);
  248. TensorMap tensor_map;
  249. size_t num_epochs = 2;
  250. for (int ep_num = 0; ep_num < num_epochs; ++ep_num) {
  251. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  252. EXPECT_TRUE(rc.IsOk());
  253. while (tensor_map.size() != 0) {
  254. rc = di.GetNextAsMap(&tensor_map);
  255. EXPECT_TRUE(rc.IsOk());
  256. }
  257. }
  258. std::vector<std::string> callback_names = {"BGN", "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND",
  259. "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND"};
  260. std::sort(callback_names.begin(), callback_names.end());
  261. std::vector<int64_t> all_steps = {0, 0, 1, 1, 5, 5, 8, 8, 9, 9, 13, 13, 16};
  262. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2};
  263. size_t len = 13;
  264. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  265. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  266. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  267. }
  268. TEST_F(MindDataTestCallback, TestSelectedCallback) {
  269. MS_LOG(INFO) << "Doing: MindDataTestCallback-TestSelectedCallback";
  270. // config callback
  271. Status rc;
  272. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(4);
  273. std::shared_ptr<DSCallback> cb1 = tst_cb;
  274. // turn off the epochs
  275. tst_cb->epoch_begin_ = false;
  276. tst_cb->epoch_end_ = false;
  277. // config leaf_op, use random_data to avoid I/O
  278. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  279. int32_t op_connector_size = config_manager->op_connector_size();
  280. int32_t num_workers = config_manager->num_parallel_workers();
  281. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  282. TensorShape shape({}); // empty shape is a 1-value scalar Tensor
  283. ColDescriptor col("label", DataType(DataType::DE_UINT32), TensorImpl::kFlexible, 0, &shape);
  284. ASSERT_OK(schema->AddColumn(col));
  285. std::shared_ptr<RandomDataOp> leaf = std::make_shared<RandomDataOp>(4, op_connector_size, 4, std::move(schema));
  286. // config mapOp
  287. std::vector<std::string> input_columns = {"label"};
  288. std::vector<std::string> output_columns = {};
  289. std::vector<std::shared_ptr<TensorOp>> op_list;
  290. std::shared_ptr<TensorOp> my_no_op = std::make_shared<NoOp>();
  291. op_list.push_back(my_no_op);
  292. std::shared_ptr<MapOp> map_op =
  293. std::make_shared<MapOp>(input_columns, output_columns, std::move(op_list), num_workers, op_connector_size);
  294. map_op->AddCallbacks({cb1});
  295. // config RepeatOp
  296. std::shared_ptr<RepeatOp> repeat_op = std::make_shared<RepeatOp>(2);
  297. // config EpochCtrlOp
  298. std::shared_ptr<EpochCtrlOp> epoch_ctrl_op = std::make_shared<EpochCtrlOp>(2);
  299. // start build then launch tree
  300. leaf->SetTotalRepeats(4);
  301. leaf->SetNumRepeatsPerEpoch(2);
  302. map_op->SetTotalRepeats(4);
  303. map_op->SetNumRepeatsPerEpoch(2);
  304. std::shared_ptr<ExecutionTree> tree = Build({leaf, map_op, repeat_op, epoch_ctrl_op});
  305. rc = tree->Prepare();
  306. EXPECT_TRUE(rc.IsOk());
  307. rc = tree->Launch();
  308. EXPECT_TRUE(rc.IsOk());
  309. // Start the loop of reading tensors from our pipeline
  310. DatasetIterator di(tree);
  311. TensorMap tensor_map;
  312. size_t num_epochs = 2;
  313. for (int ep_num = 0; ep_num < num_epochs; ++ep_num) {
  314. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  315. EXPECT_TRUE(rc.IsOk());
  316. while (tensor_map.size() != 0) {
  317. rc = di.GetNextAsMap(&tensor_map);
  318. EXPECT_TRUE(rc.IsOk());
  319. }
  320. }
  321. std::vector<std::string> callback_names = {"BGN", "SPBGN", "SPEND", "SPBGN", "SPEND",
  322. "SPBGN", "SPEND", "SPBGN", "SPEND"};
  323. std::sort(callback_names.begin(), callback_names.end());
  324. std::vector<int64_t> all_steps = {0, 1, 1, 5, 5, 9, 9, 13, 13};
  325. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 2, 2, 2, 2};
  326. size_t len = 9;
  327. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  328. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  329. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  330. }
  331. TEST_F(MindDataTestCallback, TestCAPICallback) {
  332. MS_LOG(INFO) << "Doing: MindDataTestCallback-TestCAPICallback";
  333. // config callback
  334. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(64);
  335. std::shared_ptr<DSCallback> cb1 = tst_cb;
  336. // Create a RandomDataset. Use random_data to avoid I/O
  337. std::shared_ptr<SchemaObj> schema = Schema();
  338. ASSERT_OK(schema->add_column("label", mindspore::DataType::kNumberTypeUInt32, {}));
  339. std::shared_ptr<Dataset> ds = RandomData(44, schema);
  340. ASSERT_NE(ds, nullptr);
  341. ds = ds->Map({std::make_shared<transforms::TypeCast>(mindspore::DataType::kNumberTypeUInt64)}, {"label"}, {}, {},
  342. nullptr, {cb1});
  343. ASSERT_NE(ds, nullptr);
  344. ds = ds->Repeat(2);
  345. ASSERT_NE(ds, nullptr);
  346. auto tree_adapter = std::make_shared<TreeAdapter>();
  347. // Disable IR optimization pass
  348. tree_adapter->SetOptimize(false);
  349. // using tree_adapter to set num_epoch = 1
  350. ASSERT_OK(tree_adapter->Compile(ds->IRNode(), 1));
  351. TensorRow row;
  352. ASSERT_OK(tree_adapter->GetNext(&row));
  353. while (!row.empty()) {
  354. ASSERT_OK(tree_adapter->GetNext(&row));
  355. }
  356. std::vector<std::string> callback_names = {"BGN", "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND"};
  357. std::sort(callback_names.begin(), callback_names.end());
  358. std::vector<int64_t> all_steps = {0, 0, 1, 1, 65, 65, 88};
  359. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 1, 1};
  360. // doing resize to make sure no unexpected epoch_end or extra epoch_begin is called
  361. size_t len = 7;
  362. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  363. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  364. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  365. }