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.

optimization_pass_test.cc 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 <string>
  18. #include "minddata/dataset/core/client.h"
  19. #include "common/common.h"
  20. #include "gtest/gtest.h"
  21. #include "minddata/dataset/engine/execution_tree.h"
  22. #include "minddata/dataset/engine/ir/datasetops/dataset_node.h"
  23. #include "minddata/dataset/engine/opt/pre/getter_pass.h"
  24. using namespace mindspore::dataset;
  25. using mindspore::LogStream;
  26. using mindspore::MsLogLevel::INFO;
  27. class MindDataTestOptimizationPass : public UT::DatasetOpTesting {
  28. public:
  29. MindDataTestOptimizationPass() = default;
  30. void SetUp() override { GlobalInit(); }
  31. // this recursive function helps build a ExecutionTree from a IR node, it is copied from TreeAdapter
  32. Status DFSBuild(std::shared_ptr<DatasetNode> ir, std::shared_ptr<DatasetOp> *op, ExecutionTree *tree) {
  33. std::vector<std::shared_ptr<DatasetOp>> ops = ir->Build();
  34. CHECK_FAIL_RETURN_UNEXPECTED(!ops.empty() && tree != nullptr && op != nullptr, "Fail To Build Tree.");
  35. (*op) = ops.front();
  36. RETURN_IF_NOT_OK(tree->AssociateNode(*op));
  37. for (size_t i = 1; i < ops.size(); i++) {
  38. RETURN_IF_NOT_OK(tree->AssociateNode(ops[i]));
  39. RETURN_IF_NOT_OK(ops[i - 1]->AddChild(ops[i]));
  40. }
  41. for (std::shared_ptr<DatasetNode> child_ir : ir->Children()) {
  42. std::shared_ptr<DatasetOp> child_op;
  43. RETURN_IF_NOT_OK(DFSBuild(child_ir, &child_op, tree));
  44. RETURN_IF_NOT_OK(ops.back()->AddChild(child_op)); // append children to the last of ops
  45. }
  46. return Status::OK();
  47. }
  48. // this function will build an execution_tree from a root ir node. nullptr will be returned if error occurs
  49. std::unique_ptr<ExecutionTree> BuildTree(std::shared_ptr<DatasetNode> ir) {
  50. std::unique_ptr<ExecutionTree> tree = std::make_unique<ExecutionTree>();
  51. std::shared_ptr<DatasetOp> root;
  52. if (DFSBuild(ir, &root, tree.get()).IsError()) return nullptr;
  53. if (tree->AssignRoot(root).IsError()) return nullptr;
  54. return tree;
  55. }
  56. };
  57. TEST_F(MindDataTestOptimizationPass, MindDataTestOutputShapeAndTypePass) {
  58. MS_LOG(INFO) << "Doing MindDataTestOptimizationPass-MindDataTestOutputShapeAndTypePass.";
  59. // config leaf_op, use random_data to avoid I/O
  60. std::shared_ptr<SchemaObj> schema = std::make_shared<SchemaObj>();
  61. ASSERT_TRUE(schema->add_column("label", "uint32", {}));
  62. std::shared_ptr<Dataset> ds = RandomData(44, schema)->Repeat(2)->Project({"label"})->Shuffle(10)->Batch(2);
  63. std::unique_ptr<ExecutionTree> exe_tree = BuildTree(ds->IRNode());
  64. ASSERT_NE(exe_tree, nullptr);
  65. // test the optimization pass
  66. // OptPass is supposed to remove concat, filter repeat, shuffle skip, take and set the callback of map to empty
  67. std::function<OptPass(OptPass)> pass = [](OptPass pre) {
  68. // return a new pass, this will override all the existing pre-pass es
  69. pre.clear();
  70. pre.push_back(std::make_unique<GetterPass>(GetterPass::kOutputShapeAndType));
  71. return pre;
  72. };
  73. exe_tree->SetPrePassOverride(pass);
  74. ASSERT_OK(exe_tree->PrepareTreePreAction());
  75. std::stringstream ss;
  76. // print the tree in std::string as a way to verify that nodes are indeed removed
  77. exe_tree->Print(ss);
  78. std::string ss_str = ss.str();
  79. // ss_str would look like this
  80. // +- ( 0) <BatchOp>: [workers: 4] [batch size: 2]
  81. // +- ( 2) <ProjectOp>: [workers: 0 (inlined)]
  82. // +- ( 4) <RandomDataOp>: [workers: 4] [total rows: 44]
  83. //
  84. // verify that Shuffle and RepeatOp are removed, but Batch and ProjectOp are not
  85. EXPECT_EQ(ss_str.find("ShuffleOp"), ss_str.npos);
  86. EXPECT_EQ(ss_str.find("RepeatOp"), ss_str.npos);
  87. EXPECT_NE(ss_str.find("ProjectOp"), ss_str.npos);
  88. EXPECT_NE(ss_str.find("BatchOp"), ss_str.npos);
  89. }
  90. TEST_F(MindDataTestOptimizationPass, MindDataTestDatasetSizePass) {
  91. MS_LOG(INFO) << "Doing MindDataTestOptimizationPass-MindDataTestDatasetSizePass.";
  92. // config leaf_op, use random_data to avoid I/O
  93. std::shared_ptr<SchemaObj> schema = std::make_shared<SchemaObj>();
  94. ASSERT_TRUE(schema->add_column("label", "uint32", {}));
  95. std::shared_ptr<Dataset> ds = RandomData(44, schema)->Repeat(2)->Project({"label"})->Shuffle(10)->Batch(2);
  96. std::unique_ptr<ExecutionTree> exe_tree = BuildTree(ds->IRNode());
  97. ASSERT_NE(exe_tree, nullptr);
  98. // test the optimization pass
  99. // OptPass is supposed to remove concat, filter repeat, shuffle skip, take and set the callback of map to empty
  100. std::function<OptPass(OptPass)> pass = [](OptPass pre) {
  101. // return a new pass, this will override all the existing pre-pass es
  102. pre.clear(); // remove all existing pre pass
  103. pre.push_back(std::make_unique<GetterPass>(GetterPass::kDatasetSize));
  104. return pre;
  105. };
  106. exe_tree->SetPrePassOverride(pass);
  107. ASSERT_OK(exe_tree->PrepareTreePreAction());
  108. std::stringstream ss;
  109. // print the tree in std::string as a way to verify that nodes are indeed removed
  110. exe_tree->Print(ss);
  111. std::string ss_str = ss.str();
  112. // verify that Shuffle and RepeatOp are removed, but Batch and ProjectOp are not
  113. EXPECT_EQ(ss_str.find("ShuffleOp"), ss_str.npos);
  114. EXPECT_NE(ss_str.find("RepeatOp"), ss_str.npos);
  115. EXPECT_EQ(ss_str.find("ProjectOp"), ss_str.npos);
  116. EXPECT_NE(ss_str.find("BatchOp"), ss_str.npos);
  117. }