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

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