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

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