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.

callback_test.cc 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /**
  2. * Copyright 2020 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/source/random_data_op.h"
  22. #include "minddata/dataset/include/datasets.h"
  23. #include "minddata/dataset/include/transforms.h"
  24. #include "minddata/dataset/kernels/data/no_op.h"
  25. #include "utils/log_adapter.h"
  26. using namespace mindspore::dataset;
  27. using mindspore::LogStream;
  28. using mindspore::MsLogLevel::INFO;
  29. namespace mindspore {
  30. namespace dataset {
  31. namespace test {
  32. std::shared_ptr<ExecutionTree> BuildTree(std::vector<std::shared_ptr<DatasetOp>> ops) {
  33. std::shared_ptr<ExecutionTree> tree = std::make_shared<ExecutionTree>();
  34. Status rc;
  35. for (int i = 0; i < ops.size(); i++) {
  36. rc = tree->AssociateNode(ops[i]);
  37. EXPECT_TRUE(rc.IsOk());
  38. if (i > 0) {
  39. rc = ops[i]->AddChild(ops[i - 1]);
  40. EXPECT_TRUE(rc.IsOk());
  41. }
  42. if (i == ops.size() - 1) {
  43. rc = tree->AssignRoot(ops[i]);
  44. EXPECT_TRUE(rc.IsOk());
  45. }
  46. }
  47. return tree;
  48. }
  49. class TestCallback : public DSCallback {
  50. public:
  51. TestCallback(int32_t step_size)
  52. : DSCallback(step_size),
  53. begin_(true),
  54. epoch_begin_(true),
  55. step_begin_(true),
  56. end_(false),
  57. epoch_end_(true),
  58. step_end_(true) {
  59. all_names_.reserve(32);
  60. all_step_nums_.reserve(32);
  61. all_ep_nums_.reserve(32);
  62. }
  63. Status DSBegin(const CallbackParam &cb_param) override {
  64. all_names_.push_back("BGN");
  65. all_step_nums_.push_back(cb_param.cur_step_num_);
  66. all_ep_nums_.push_back(cb_param.cur_epoch_num_);
  67. return Status::OK();
  68. }
  69. Status DSEpochBegin(const CallbackParam &cb_param) override {
  70. all_names_.push_back("EPBGN");
  71. all_step_nums_.push_back(cb_param.cur_step_num_);
  72. all_ep_nums_.push_back(cb_param.cur_epoch_num_);
  73. return Status::OK();
  74. }
  75. Status DSNStepBegin(const CallbackParam &cb_param) override {
  76. all_names_.push_back("SPBGN");
  77. all_step_nums_.push_back(cb_param.cur_step_num_);
  78. all_ep_nums_.push_back(cb_param.cur_epoch_num_);
  79. return Status::OK();
  80. }
  81. Status DSEnd(const CallbackParam &cb_param) override {
  82. all_names_.push_back("END");
  83. all_step_nums_.push_back(cb_param.cur_step_num_);
  84. all_ep_nums_.push_back(cb_param.cur_epoch_num_);
  85. return Status::OK();
  86. }
  87. Status DSEpochEnd(const CallbackParam &cb_param) override {
  88. all_names_.push_back("EPEND");
  89. all_step_nums_.push_back(cb_param.cur_step_num_);
  90. all_ep_nums_.push_back(cb_param.cur_epoch_num_);
  91. return Status::OK();
  92. }
  93. Status DSNStepEnd(const CallbackParam &cb_param) override {
  94. all_names_.push_back("SPEND");
  95. all_step_nums_.push_back(cb_param.cur_step_num_);
  96. all_ep_nums_.push_back(cb_param.cur_epoch_num_);
  97. return Status::OK();
  98. }
  99. bool IsBeginNeeded() override { return begin_; }
  100. bool IsEpochBeginNeeded() override { return epoch_begin_; }
  101. bool IsNStepBeginNeeded() override { return step_begin_; }
  102. bool IsEndNeeded() override { return end_; }
  103. bool IsEpochEndNeeded() override { return epoch_end_; }
  104. bool IsNStepEndNeeded() override { return step_end_; }
  105. std::vector<std::string> all_names(size_t len) {
  106. return std::vector<std::string>(all_names_.begin(), all_names_.begin() + len);
  107. }
  108. std::vector<int64_t> all_step_nums(size_t len) {
  109. return std::vector<int64_t>(all_step_nums_.begin(), all_step_nums_.begin() + len);
  110. }
  111. std::vector<int64_t> all_ep_nums(size_t len) {
  112. return std::vector<int64_t>(all_ep_nums_.begin(), all_ep_nums_.begin() + len);
  113. }
  114. // flag for turning callback on and off
  115. bool begin_, epoch_begin_, step_begin_, end_, epoch_end_, step_end_;
  116. // name of the callback function in sequence, BGN, EPBGN, SPB, END, EPEND, SPEND
  117. std::vector<std::string> all_names_;
  118. std::vector<int64_t> all_step_nums_, all_ep_nums_;
  119. };
  120. } // namespace test
  121. } // namespace dataset
  122. } // namespace mindspore
  123. class MindDataTestCallback : public UT::DatasetOpTesting {
  124. public:
  125. void SetUp() override {
  126. DatasetOpTesting::SetUp();
  127. GlobalInit();
  128. }
  129. };
  130. TEST_F(MindDataTestCallback, TestBasicCallback) {
  131. // config callback
  132. Status rc;
  133. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(64);
  134. std::shared_ptr<DSCallback> cb1 = tst_cb;
  135. // config leaf_op, use random_data to avoid I/O
  136. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  137. TensorShape shape({}); // empty shape is a 1-value scalar Tensor
  138. ColDescriptor col("label", DataType(DataType::DE_UINT32), TensorImpl::kFlexible, 0, &shape);
  139. ASSERT_OK(schema->AddColumn(col));
  140. std::shared_ptr<RandomDataOp> leaf;
  141. rc = RandomDataOp::Builder().SetRowsPerBuffer(1).SetDataSchema(std::move(schema)).SetTotalRows(44).Build(&leaf);
  142. EXPECT_TRUE(rc.IsOk());
  143. // config mapOp
  144. std::shared_ptr<MapOp> map_op;
  145. auto map_b = MapOp::Builder();
  146. rc = map_b.SetInColNames({"label"}).SetTensorFuncs({std::make_shared<NoOp>()}).AddCallbacks({cb1}).Build(&map_op);
  147. EXPECT_TRUE(rc.IsOk());
  148. // config RepeatOp
  149. std::shared_ptr<RepeatOp> repeat_op;
  150. rc = RepeatOp::Builder(2).Build(&repeat_op);
  151. // start build then launch tree
  152. std::shared_ptr<ExecutionTree> tree = test::BuildTree({leaf, map_op, repeat_op});
  153. rc = tree->Prepare();
  154. EXPECT_TRUE(rc.IsOk());
  155. rc = tree->Launch();
  156. EXPECT_TRUE(rc.IsOk());
  157. // Start the loop of reading tensors from our pipeline
  158. DatasetIterator di(tree);
  159. TensorMap tensor_map;
  160. rc = di.GetNextAsMap(&tensor_map);
  161. EXPECT_TRUE(rc.IsOk());
  162. while (!tensor_map.empty()) {
  163. rc = di.GetNextAsMap(&tensor_map);
  164. EXPECT_TRUE(rc.IsOk());
  165. }
  166. std::vector<std::string> callback_names = {"BGN", "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND"};
  167. std::vector<int64_t> all_steps = {0, 0, 1, 1, 65, 65, 88};
  168. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 1, 1};
  169. // doing resize to make sure no unexpected epoch_end or extra epoch_begin is called
  170. size_t len = 7;
  171. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  172. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  173. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  174. }
  175. TEST_F(MindDataTestCallback, TestMutiEpochCallback) {
  176. // config callback
  177. Status rc;
  178. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(4);
  179. std::shared_ptr<DSCallback> cb1 = tst_cb;
  180. // config leaf_op, use random_data to avoid I/O
  181. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  182. TensorShape shape({}); // empty shape is a 1-value scalar Tensor
  183. ColDescriptor col("label", DataType(DataType::DE_UINT32), TensorImpl::kFlexible, 0, &shape);
  184. ASSERT_OK(schema->AddColumn(col));
  185. std::shared_ptr<RandomDataOp> leaf;
  186. rc = RandomDataOp::Builder().SetRowsPerBuffer(1).SetDataSchema(std::move(schema)).SetTotalRows(4).Build(&leaf);
  187. EXPECT_TRUE(rc.IsOk());
  188. // config mapOp
  189. std::shared_ptr<MapOp> map_op;
  190. auto map_b = MapOp::Builder();
  191. rc = map_b.SetInColNames({"label"}).SetTensorFuncs({std::make_shared<NoOp>()}).AddCallbacks({cb1}).Build(&map_op);
  192. EXPECT_TRUE(rc.IsOk());
  193. // config RepeatOp
  194. std::shared_ptr<RepeatOp> repeat_op;
  195. rc = RepeatOp::Builder(2).Build(&repeat_op);
  196. // start build then launch tree
  197. std::shared_ptr<ExecutionTree> tree = test::BuildTree({leaf, map_op, repeat_op});
  198. rc = tree->Prepare();
  199. EXPECT_TRUE(rc.IsOk());
  200. rc = tree->Launch();
  201. EXPECT_TRUE(rc.IsOk());
  202. // Start the loop of reading tensors from our pipeline
  203. DatasetIterator di(tree);
  204. TensorMap tensor_map;
  205. size_t num_epochs = 2;
  206. for (int ep_num = 0; ep_num < num_epochs; ++ep_num) {
  207. di.GetNextAsMap(&tensor_map);
  208. EXPECT_TRUE(rc.IsOk());
  209. while (tensor_map.size() != 0) {
  210. rc = di.GetNextAsMap(&tensor_map);
  211. EXPECT_TRUE(rc.IsOk());
  212. }
  213. }
  214. std::vector<std::string> callback_names = {"BGN", "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND",
  215. "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND"};
  216. std::vector<int64_t> all_steps = {0, 0, 1, 1, 5, 5, 8, 8, 9, 9, 13, 13, 16};
  217. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2};
  218. size_t len = 13;
  219. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  220. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  221. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  222. }
  223. TEST_F(MindDataTestCallback, TestSelectedCallback) {
  224. // config callback
  225. Status rc;
  226. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(4);
  227. std::shared_ptr<DSCallback> cb1 = tst_cb;
  228. // turn off the epochs
  229. tst_cb->epoch_begin_ = false;
  230. tst_cb->epoch_end_ = false;
  231. // config leaf_op, use random_data to avoid I/O
  232. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  233. TensorShape shape({}); // empty shape is a 1-value scalar Tensor
  234. ColDescriptor col("label", DataType(DataType::DE_UINT32), TensorImpl::kFlexible, 0, &shape);
  235. ASSERT_OK(schema->AddColumn(col));
  236. std::shared_ptr<RandomDataOp> leaf;
  237. rc = RandomDataOp::Builder().SetRowsPerBuffer(1).SetDataSchema(std::move(schema)).SetTotalRows(4).Build(&leaf);
  238. EXPECT_TRUE(rc.IsOk());
  239. // config mapOp
  240. std::shared_ptr<MapOp> map_op;
  241. auto map_b = MapOp::Builder();
  242. rc = map_b.SetInColNames({"label"}).SetTensorFuncs({std::make_shared<NoOp>()}).AddCallbacks({cb1}).Build(&map_op);
  243. EXPECT_TRUE(rc.IsOk());
  244. // config RepeatOp
  245. std::shared_ptr<RepeatOp> repeat_op;
  246. rc = RepeatOp::Builder(2).Build(&repeat_op);
  247. // start build then launch tree
  248. std::shared_ptr<ExecutionTree> tree = test::BuildTree({leaf, map_op, repeat_op});
  249. rc = tree->Prepare();
  250. EXPECT_TRUE(rc.IsOk());
  251. rc = tree->Launch();
  252. EXPECT_TRUE(rc.IsOk());
  253. // Start the loop of reading tensors from our pipeline
  254. DatasetIterator di(tree);
  255. TensorMap tensor_map;
  256. size_t num_epochs = 2;
  257. for (int ep_num = 0; ep_num < num_epochs; ++ep_num) {
  258. di.GetNextAsMap(&tensor_map);
  259. EXPECT_TRUE(rc.IsOk());
  260. while (tensor_map.size() != 0) {
  261. rc = di.GetNextAsMap(&tensor_map);
  262. EXPECT_TRUE(rc.IsOk());
  263. }
  264. }
  265. std::vector<std::string> callback_names = {"BGN", "SPBGN", "SPEND", "SPBGN", "SPEND",
  266. "SPBGN", "SPEND", "SPBGN", "SPEND"};
  267. std::vector<int64_t> all_steps = {0, 1, 1, 5, 5, 9, 9, 13, 13};
  268. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 2, 2, 2, 2};
  269. size_t len = 9;
  270. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  271. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  272. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  273. }
  274. TEST_F(MindDataTestCallback, TestCAPICallback) {
  275. MS_LOG(INFO) << "Doing: MindDataTestCallback-TestCAPICallback";
  276. // config callback
  277. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(64);
  278. std::shared_ptr<DSCallback> cb1 = tst_cb;
  279. // config leaf_op, use random_data to avoid I/O
  280. std::shared_ptr<SchemaObj> schema = std::make_shared<SchemaObj>();
  281. ASSERT_TRUE(schema->add_column("label", "uint32", {}));
  282. std::shared_ptr<Dataset> ds = RandomData(44, schema);
  283. ds = ds->Map({transforms::TypeCast("uint64")}, {"label"}, {}, {}, nullptr, {cb1});
  284. ds = ds->Repeat(2);
  285. TreeAdapter tree_adapter;
  286. // using tree_adapter to set num_epoch = 1
  287. ASSERT_OK(tree_adapter.Compile(ds->IRNode(), 1));
  288. TensorRow row;
  289. ASSERT_OK(tree_adapter.GetNext(&row));
  290. while (!row.empty()) {
  291. ASSERT_OK(tree_adapter.GetNext(&row));
  292. }
  293. std::vector<std::string> callback_names = {"BGN", "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND"};
  294. std::vector<int64_t> all_steps = {0, 0, 1, 1, 65, 65, 88};
  295. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 1, 1};
  296. // doing resize to make sure no unexpected epoch_end or extra epoch_begin is called
  297. size_t len = 7;
  298. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  299. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  300. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  301. }