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

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. };
  121. TEST_F(MindDataTestCallback, TestBasicCallback) {
  122. MS_LOG(INFO) << "Doing: MindDataTestCallback-TestBasicCallback";
  123. // config callback
  124. Status rc;
  125. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(64);
  126. std::shared_ptr<DSCallback> cb1 = tst_cb;
  127. // config leaf_op, use random_data to avoid I/O
  128. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  129. TensorShape shape({}); // empty shape is a 1-value scalar Tensor
  130. ColDescriptor col("label", DataType(DataType::DE_UINT32), TensorImpl::kFlexible, 0, &shape);
  131. ASSERT_OK(schema->AddColumn(col));
  132. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  133. int32_t op_connector_size = config_manager->op_connector_size();
  134. int32_t num_workers = config_manager->num_parallel_workers();
  135. std::shared_ptr<RandomDataOp> leaf =
  136. std::make_shared<RandomDataOp>(num_workers, op_connector_size, 44, std::move(schema));
  137. // config mapOp
  138. std::vector<std::string> input_columns = {"label"};
  139. std::vector<std::string> output_columns = {};
  140. std::vector<std::shared_ptr<TensorOp>> op_list;
  141. std::shared_ptr<TensorOp> my_no_op = std::make_shared<NoOp>();
  142. op_list.push_back(my_no_op);
  143. std::shared_ptr<MapOp> map_op =
  144. std::make_shared<MapOp>(input_columns, output_columns, std::move(op_list), num_workers, op_connector_size);
  145. std::vector<std::shared_ptr<DSCallback>> cbs = {};
  146. cbs.push_back(cb1);
  147. map_op->AddCallbacks(std::move(cbs));
  148. // config RepeatOp
  149. std::shared_ptr<RepeatOp> repeat_op = std::make_shared<RepeatOp>(2);
  150. // start build then launch tree
  151. leaf->SetTotalRepeats(2);
  152. leaf->SetNumRepeatsPerEpoch(2);
  153. map_op->SetTotalRepeats(2);
  154. map_op->SetNumRepeatsPerEpoch(2);
  155. std::shared_ptr<ExecutionTree> tree = Build({leaf, map_op, repeat_op});
  156. rc = tree->Prepare();
  157. EXPECT_TRUE(rc.IsOk());
  158. rc = tree->Launch();
  159. EXPECT_TRUE(rc.IsOk());
  160. // Start the loop of reading tensors from our pipeline
  161. DatasetIterator di(tree);
  162. TensorMap tensor_map;
  163. rc = di.GetNextAsMap(&tensor_map);
  164. EXPECT_TRUE(rc.IsOk());
  165. while (!tensor_map.empty()) {
  166. rc = di.GetNextAsMap(&tensor_map);
  167. EXPECT_TRUE(rc.IsOk());
  168. }
  169. std::vector<std::string> callback_names = {"BGN", "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND"};
  170. std::sort(callback_names.begin(), callback_names.end());
  171. std::vector<int64_t> all_steps = {0, 0, 1, 1, 65, 65, 88};
  172. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 1, 1};
  173. // doing resize to make sure no unexpected epoch_end or extra epoch_begin is called
  174. size_t len = 7;
  175. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  176. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  177. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  178. }
  179. TEST_F(MindDataTestCallback, TestMultiEpochCallback) {
  180. MS_LOG(INFO) << "Doing: MindDataTestCallback-TestMultiEpochCallback";
  181. // config callback
  182. Status rc;
  183. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(4);
  184. std::shared_ptr<DSCallback> cb1 = tst_cb;
  185. // config leaf_op, use random_data to avoid I/O
  186. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  187. int32_t op_connector_size = config_manager->op_connector_size();
  188. int32_t num_workers = config_manager->num_parallel_workers();
  189. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  190. TensorShape shape({}); // empty shape is a 1-value scalar Tensor
  191. ColDescriptor col("label", DataType(DataType::DE_UINT32), TensorImpl::kFlexible, 0, &shape);
  192. ASSERT_OK(schema->AddColumn(col));
  193. std::shared_ptr<RandomDataOp> leaf = std::make_shared<RandomDataOp>(4, op_connector_size, 4, std::move(schema));
  194. // config mapOp
  195. std::vector<std::string> input_columns = {"label"};
  196. std::vector<std::string> output_columns = {};
  197. std::vector<std::shared_ptr<TensorOp>> op_list;
  198. std::shared_ptr<TensorOp> my_no_op = std::make_shared<NoOp>();
  199. op_list.push_back(my_no_op);
  200. std::shared_ptr<MapOp> map_op =
  201. std::make_shared<MapOp>(input_columns, output_columns, std::move(op_list), num_workers, op_connector_size);
  202. std::vector<std::shared_ptr<DSCallback>> cbs = {};
  203. cbs.push_back(cb1);
  204. map_op->AddCallbacks(std::move(cbs));
  205. EXPECT_TRUE(rc.IsOk());
  206. // config RepeatOp
  207. std::shared_ptr<RepeatOp> repeat_op = std::make_shared<RepeatOp>(2);
  208. // config EpochCtrlOp
  209. std::shared_ptr<EpochCtrlOp> epoch_ctrl_op = std::make_shared<EpochCtrlOp>(2);
  210. // start build then launch tree
  211. leaf->SetTotalRepeats(-2);
  212. leaf->SetNumRepeatsPerEpoch(2);
  213. map_op->SetTotalRepeats(-2);
  214. map_op->SetNumRepeatsPerEpoch(2);
  215. std::shared_ptr<ExecutionTree> tree = Build({leaf, map_op, repeat_op, epoch_ctrl_op});
  216. rc = tree->Prepare();
  217. EXPECT_TRUE(rc.IsOk());
  218. rc = tree->Launch();
  219. EXPECT_TRUE(rc.IsOk());
  220. // Start the loop of reading tensors from our pipeline
  221. DatasetIterator di(tree);
  222. TensorMap tensor_map;
  223. size_t num_epochs = 2;
  224. for (int ep_num = 0; ep_num < num_epochs; ++ep_num) {
  225. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  226. EXPECT_TRUE(rc.IsOk());
  227. while (tensor_map.size() != 0) {
  228. rc = di.GetNextAsMap(&tensor_map);
  229. EXPECT_TRUE(rc.IsOk());
  230. }
  231. }
  232. std::vector<std::string> callback_names = {"BGN", "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND",
  233. "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND"};
  234. std::sort(callback_names.begin(), callback_names.end());
  235. std::vector<int64_t> all_steps = {0, 0, 1, 1, 5, 5, 8, 8, 9, 9, 13, 13, 16};
  236. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2};
  237. size_t len = 13;
  238. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  239. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  240. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  241. }
  242. TEST_F(MindDataTestCallback, TestSelectedCallback) {
  243. MS_LOG(INFO) << "Doing: MindDataTestCallback-TestSelectedCallback";
  244. // config callback
  245. Status rc;
  246. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(4);
  247. std::shared_ptr<DSCallback> cb1 = tst_cb;
  248. // turn off the epochs
  249. tst_cb->epoch_begin_ = false;
  250. tst_cb->epoch_end_ = false;
  251. // config leaf_op, use random_data to avoid I/O
  252. std::shared_ptr<ConfigManager> config_manager = GlobalContext::config_manager();
  253. int32_t op_connector_size = config_manager->op_connector_size();
  254. int32_t num_workers = config_manager->num_parallel_workers();
  255. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  256. TensorShape shape({}); // empty shape is a 1-value scalar Tensor
  257. ColDescriptor col("label", DataType(DataType::DE_UINT32), TensorImpl::kFlexible, 0, &shape);
  258. ASSERT_OK(schema->AddColumn(col));
  259. std::shared_ptr<RandomDataOp> leaf = std::make_shared<RandomDataOp>(4, op_connector_size, 4, std::move(schema));
  260. // config mapOp
  261. std::vector<std::string> input_columns = {"label"};
  262. std::vector<std::string> output_columns = {};
  263. std::vector<std::shared_ptr<TensorOp>> op_list;
  264. std::shared_ptr<TensorOp> my_no_op = std::make_shared<NoOp>();
  265. op_list.push_back(my_no_op);
  266. std::shared_ptr<MapOp> map_op =
  267. std::make_shared<MapOp>(input_columns, output_columns, std::move(op_list), num_workers, op_connector_size);
  268. map_op->AddCallbacks({cb1});
  269. // config RepeatOp
  270. std::shared_ptr<RepeatOp> repeat_op = std::make_shared<RepeatOp>(2);
  271. // config EpochCtrlOp
  272. std::shared_ptr<EpochCtrlOp> epoch_ctrl_op = std::make_shared<EpochCtrlOp>(2);
  273. // start build then launch tree
  274. leaf->SetTotalRepeats(-2);
  275. leaf->SetNumRepeatsPerEpoch(2);
  276. map_op->SetTotalRepeats(-2);
  277. map_op->SetNumRepeatsPerEpoch(2);
  278. std::shared_ptr<ExecutionTree> tree = Build({leaf, map_op, repeat_op, epoch_ctrl_op});
  279. rc = tree->Prepare();
  280. EXPECT_TRUE(rc.IsOk());
  281. rc = tree->Launch();
  282. EXPECT_TRUE(rc.IsOk());
  283. // Start the loop of reading tensors from our pipeline
  284. DatasetIterator di(tree);
  285. TensorMap tensor_map;
  286. size_t num_epochs = 2;
  287. for (int ep_num = 0; ep_num < num_epochs; ++ep_num) {
  288. ASSERT_OK(di.GetNextAsMap(&tensor_map));
  289. EXPECT_TRUE(rc.IsOk());
  290. while (tensor_map.size() != 0) {
  291. rc = di.GetNextAsMap(&tensor_map);
  292. EXPECT_TRUE(rc.IsOk());
  293. }
  294. }
  295. std::vector<std::string> callback_names = {"BGN", "SPBGN", "SPEND", "SPBGN", "SPEND",
  296. "SPBGN", "SPEND", "SPBGN", "SPEND"};
  297. std::sort(callback_names.begin(), callback_names.end());
  298. std::vector<int64_t> all_steps = {0, 1, 1, 5, 5, 9, 9, 13, 13};
  299. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 2, 2, 2, 2};
  300. size_t len = 9;
  301. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  302. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  303. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  304. }
  305. TEST_F(MindDataTestCallback, TestCAPICallback) {
  306. MS_LOG(INFO) << "Doing: MindDataTestCallback-TestCAPICallback";
  307. // config callback
  308. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(64);
  309. std::shared_ptr<DSCallback> cb1 = tst_cb;
  310. // Create a RandomDataset. Use random_data to avoid I/O
  311. std::shared_ptr<SchemaObj> schema = Schema();
  312. ASSERT_OK(schema->add_column("label", mindspore::DataType::kNumberTypeUInt32, {}));
  313. std::shared_ptr<Dataset> ds = RandomData(44, schema);
  314. ASSERT_NE(ds, nullptr);
  315. ds = ds->Map({std::make_shared<transforms::TypeCast>(mindspore::DataType::kNumberTypeUInt64)}, {"label"}, {}, {},
  316. nullptr, {cb1});
  317. ASSERT_NE(ds, nullptr);
  318. ds = ds->Repeat(2);
  319. ASSERT_NE(ds, nullptr);
  320. auto tree_adapter = std::make_shared<TreeAdapter>();
  321. // Disable IR optimization pass
  322. tree_adapter->SetOptimize(false);
  323. // using tree_adapter to set num_epoch = 1
  324. ASSERT_OK(tree_adapter->Compile(ds->IRNode(), 1));
  325. TensorRow row;
  326. ASSERT_OK(tree_adapter->GetNext(&row));
  327. while (!row.empty()) {
  328. ASSERT_OK(tree_adapter->GetNext(&row));
  329. }
  330. std::vector<std::string> callback_names = {"BGN", "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND"};
  331. std::sort(callback_names.begin(), callback_names.end());
  332. std::vector<int64_t> all_steps = {0, 0, 1, 1, 65, 65, 88};
  333. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 1, 1};
  334. // doing resize to make sure no unexpected epoch_end or extra epoch_begin is called
  335. size_t len = 7;
  336. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  337. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  338. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  339. }