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.5 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Copyright 2020-2021 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 "common/common.h"
  19. #include "gtest/gtest.h"
  20. #include "minddata/dataset/core/client.h"
  21. #include "minddata/dataset/engine/ir/datasetops/dataset_node.h"
  22. #include "minddata/dataset/engine/ir/datasetops/map_node.h"
  23. #include "minddata/dataset/engine/opt/optional/tensor_op_fusion_pass.h"
  24. #include "minddata/dataset/engine/opt/post/auto_worker_pass.h"
  25. #include "minddata/dataset/include/transforms.h"
  26. #include "minddata/dataset/include/vision.h"
  27. #include "minddata/dataset/include/vision_lite.h"
  28. #include "minddata/dataset/kernels/ir/data/transforms_ir.h"
  29. #include "minddata/dataset/kernels/ir/vision/vision_ir.h"
  30. using namespace mindspore::dataset;
  31. using mindspore::LogStream;
  32. using mindspore::MsLogLevel::INFO;
  33. class MindDataTestOptimizationPass : public UT::DatasetOpTesting {};
  34. TEST_F(MindDataTestOptimizationPass, MindDataTestAutoWorkerPass) {
  35. MS_LOG(INFO) << "Doing MindDataTestOptimizationPass-MindDataTestAutoWorkerPass.";
  36. std::shared_ptr<SchemaObj> schema = std::make_shared<SchemaObj>();
  37. ASSERT_OK(schema->add_column("label", "uint32", {}));
  38. std::shared_ptr<Dataset> map_leaf = ImageFolder("dir")->SetNumWorkers(0);
  39. std::shared_ptr<Dataset> nonmap_leaf = RandomData(44, schema)->SetNumWorkers(0);
  40. std::shared_ptr<Dataset> batch = Zip({map_leaf, nonmap_leaf})->Batch(1)->SetNumWorkers(0);
  41. std::shared_ptr<Dataset> map = batch->Map({std::shared_ptr<TensorTransform>()})->SetNumWorkers(0);
  42. // {ImageFolder, RandomData} -> zip -> batch
  43. EXPECT_EQ(map_leaf->IRNode()->num_workers(), 0);
  44. EXPECT_EQ(nonmap_leaf->IRNode()->num_workers(), 0);
  45. EXPECT_EQ(batch->IRNode()->num_workers(), 0);
  46. EXPECT_EQ(map->IRNode()->num_workers(), 0);
  47. std::unique_ptr<IRPass> pass = std::make_unique<AutoWorkerPass>();
  48. bool m = false;
  49. ASSERT_OK(pass->Run(map->IRNode(), &m));
  50. // checking that after this pass, num_workers are set correctly (aka a positive number)
  51. // It is hard to test a exact value because num_threads are different for different machine
  52. // however, this will for sure succeed bc regardless of the total threads on cpu, this would always be >= 1
  53. EXPECT_NE(map_leaf->IRNode()->num_workers(), 0);
  54. EXPECT_NE(nonmap_leaf->IRNode()->num_workers(), 0);
  55. EXPECT_NE(batch->IRNode()->num_workers(), 0);
  56. EXPECT_NE(map->IRNode()->num_workers(), 0);
  57. MS_LOG(DEBUG) << map_leaf->IRNode()->Name() << ": num_worker=" << map_leaf->IRNode()->num_workers();
  58. MS_LOG(DEBUG) << nonmap_leaf->IRNode()->Name() << ": num_worker=" << nonmap_leaf->IRNode()->num_workers();
  59. MS_LOG(DEBUG) << batch->IRNode()->Name() << ": num_worker=" << batch->IRNode()->num_workers();
  60. MS_LOG(DEBUG) << map->IRNode()->Name() << ": num_worker=" << map->IRNode()->num_workers();
  61. }
  62. TEST_F(MindDataTestOptimizationPass, MindDataTestTensorFusionPass) {
  63. MS_LOG(INFO) << "Doing MindDataTestOptimizationPass-MindDataTestTensorFusionPass.";
  64. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  65. auto decode_op = vision::Decode();
  66. auto random_resized_crop_op = vision::RandomResizedCrop({100});
  67. std::shared_ptr<Dataset> root = ImageFolder(folder_path, false)->Map({decode_op, random_resized_crop_op}, {"image"});
  68. TensorOpFusionPass fusion_pass;
  69. bool modified = false;
  70. std::shared_ptr<MapNode> map_node = std::dynamic_pointer_cast<MapNode>(root->IRNode());
  71. // no deepcopy is performed because this doesn't go through tree_adapter
  72. fusion_pass.Run(root->IRNode(), &modified);
  73. EXPECT_EQ(modified, true);
  74. ASSERT_NE(map_node, nullptr);
  75. auto fused_ops = map_node->operations();
  76. ASSERT_EQ(fused_ops.size(), 1);
  77. ASSERT_EQ(fused_ops[0]->Name(), vision::kRandomCropDecodeResizeOperation);
  78. }
  79. TEST_F(MindDataTestOptimizationPass, MindDataTestTensorFusionPassPreBuiltTensorOperation) {
  80. MS_LOG(INFO) << "Doing MindDataTestOptimizationPass-MindDataTestTensorFusionPassPreBuiltTensorOperation.";
  81. std::string folder_path = datasets_root_path_ + "/testPK/data/";
  82. // make prebuilt tensor operation
  83. auto decode = std::make_shared<transforms::PreBuiltOperation>(vision::DecodeOperation(true).Build());
  84. auto resize = std::make_shared<transforms::PreBuiltOperation>(
  85. vision::RandomResizedCropOperation({100, 100}, {0.5, 1.0}, {0.1, 0.2}, InterpolationMode::kNearestNeighbour, 5).Build());
  86. std::vector<std::shared_ptr<TensorOperation>> op_list = {decode, resize};
  87. std::vector<std::string> op_name = {"image"};
  88. std::shared_ptr<DatasetNode> root = ImageFolder(folder_path, false)->IRNode();
  89. std::shared_ptr<MapNode> map_node = std::make_shared<MapNode>(root, op_list, op_name);
  90. TensorOpFusionPass fusion_pass;
  91. bool modified = false;
  92. // no deepcopy is performed because this doesn't go through tree_adapter
  93. fusion_pass.Run(map_node, &modified);
  94. EXPECT_EQ(modified, true);
  95. ASSERT_NE(map_node, nullptr);
  96. auto fused_ops = map_node->operations();
  97. ASSERT_EQ(fused_ops.size(), 1);
  98. ASSERT_EQ(fused_ops[0]->Name(), kRandomCropDecodeResizeOp);
  99. }