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

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