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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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_(true),
  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. tst_cb->end_ = false; // don't do the end for now due to a timing issue
  134. // config leaf_op, use random_data to avoid I/O
  135. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  136. TensorShape shape({}); // empty shape is a 1-value scalar Tensor
  137. ColDescriptor col("label", DataType(DataType::DE_UINT32), TensorImpl::kFlexible, 0, &shape);
  138. schema->AddColumn(col);
  139. std::shared_ptr<RandomDataOp> leaf;
  140. rc = RandomDataOp::Builder().SetRowsPerBuffer(1).SetDataSchema(std::move(schema)).SetTotalRows(44).Build(&leaf);
  141. EXPECT_TRUE(rc.IsOk());
  142. // config mapOp
  143. std::shared_ptr<MapOp> map_op;
  144. auto map_b = MapOp::Builder();
  145. rc = map_b.SetInColNames({"label"}).SetTensorFuncs({std::make_shared<NoOp>()}).AddCallbacks({cb1}).Build(&map_op);
  146. EXPECT_TRUE(rc.IsOk());
  147. // config RepeatOp
  148. std::shared_ptr<RepeatOp> repeat_op;
  149. rc = RepeatOp::Builder(2).Build(&repeat_op);
  150. // start build then launch tree
  151. std::shared_ptr<ExecutionTree> tree = test::BuildTree({leaf, map_op, repeat_op});
  152. rc = tree->Prepare();
  153. EXPECT_TRUE(rc.IsOk());
  154. rc = tree->Launch();
  155. EXPECT_TRUE(rc.IsOk());
  156. // Start the loop of reading tensors from our pipeline
  157. DatasetIterator di(tree);
  158. TensorMap tensor_map;
  159. rc = di.GetNextAsMap(&tensor_map);
  160. EXPECT_TRUE(rc.IsOk());
  161. while (!tensor_map.empty()) {
  162. rc = di.GetNextAsMap(&tensor_map);
  163. EXPECT_TRUE(rc.IsOk());
  164. }
  165. std::vector<std::string> callback_names = {"BGN", "EPBGN", "SPBGN", "SPEND", "SPBGN", "SPEND", "EPEND"};
  166. std::vector<int64_t> all_steps = {0, 0, 1, 1, 65, 65, 88};
  167. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 1, 1};
  168. // doing resize to make sure no unexpected epoch_end or extra epoch_begin is called
  169. size_t len = 7;
  170. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  171. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  172. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  173. }
  174. TEST_F(MindDataTestCallback, TestMutiEpochCallback) {
  175. // config callback
  176. Status rc;
  177. std::shared_ptr<test::TestCallback> tst_cb = std::make_shared<test::TestCallback>(4);
  178. std::shared_ptr<DSCallback> cb1 = tst_cb;
  179. tst_cb->end_ = false; // don't do the end for now due to a timing issue
  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. 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. tst_cb->end_ = false;
  229. // turn off the epochs
  230. tst_cb->epoch_begin_ = false;
  231. tst_cb->epoch_end_ = false;
  232. // config leaf_op, use random_data to avoid I/O
  233. std::unique_ptr<DataSchema> schema = std::make_unique<DataSchema>();
  234. TensorShape shape({}); // empty shape is a 1-value scalar Tensor
  235. ColDescriptor col("label", DataType(DataType::DE_UINT32), TensorImpl::kFlexible, 0, &shape);
  236. schema->AddColumn(col);
  237. std::shared_ptr<RandomDataOp> leaf;
  238. rc = RandomDataOp::Builder().SetRowsPerBuffer(1).SetDataSchema(std::move(schema)).SetTotalRows(4).Build(&leaf);
  239. EXPECT_TRUE(rc.IsOk());
  240. // config mapOp
  241. std::shared_ptr<MapOp> map_op;
  242. auto map_b = MapOp::Builder();
  243. rc = map_b.SetInColNames({"label"}).SetTensorFuncs({std::make_shared<NoOp>()}).AddCallbacks({cb1}).Build(&map_op);
  244. EXPECT_TRUE(rc.IsOk());
  245. // config RepeatOp
  246. std::shared_ptr<RepeatOp> repeat_op;
  247. rc = RepeatOp::Builder(2).Build(&repeat_op);
  248. // start build then launch tree
  249. std::shared_ptr<ExecutionTree> tree = test::BuildTree({leaf, map_op, repeat_op});
  250. rc = tree->Prepare();
  251. EXPECT_TRUE(rc.IsOk());
  252. rc = tree->Launch();
  253. EXPECT_TRUE(rc.IsOk());
  254. // Start the loop of reading tensors from our pipeline
  255. DatasetIterator di(tree);
  256. TensorMap tensor_map;
  257. size_t num_epochs = 2;
  258. for (int ep_num = 0; ep_num < num_epochs; ++ep_num) {
  259. di.GetNextAsMap(&tensor_map);
  260. EXPECT_TRUE(rc.IsOk());
  261. while (tensor_map.size() != 0) {
  262. rc = di.GetNextAsMap(&tensor_map);
  263. EXPECT_TRUE(rc.IsOk());
  264. }
  265. }
  266. std::vector<std::string> callback_names = {"BGN", "SPBGN", "SPEND", "SPBGN", "SPEND",
  267. "SPBGN", "SPEND", "SPBGN", "SPEND"};
  268. std::vector<int64_t> all_steps = {0, 1, 1, 5, 5, 9, 9, 13, 13};
  269. std::vector<int64_t> all_epochs = {0, 1, 1, 1, 1, 2, 2, 2, 2};
  270. size_t len = 9;
  271. EXPECT_EQ(tst_cb->all_names(len), callback_names);
  272. EXPECT_EQ(tst_cb->all_ep_nums(len), all_epochs);
  273. EXPECT_EQ(tst_cb->all_step_nums(len), all_steps);
  274. }