1 /**
2 * Copyright 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
17 #include <memory>
18 #include <string>
19 #include "common/common.h"
20 #include "minddata/dataset/engine/execution_tree.h"
21 #include "minddata/dataset/engine/ir/datasetops/dataset_node.h"
22 #include "minddata/dataset/engine/tree_adapter.h"
23 #include "minddata/dataset/include/dataset/datasets.h"
24 #include "minddata/dataset/include/dataset/transforms.h"
25 #include "minddata/dataset/include/dataset/vision.h"
26 #include "minddata/dataset/kernels/tensor_op.h"
27
28 using namespace mindspore::dataset;
29
30 class MindDataTestTensorOpFusionPass : public UT::DatasetOpTesting {
31 public:
32 MindDataTestTensorOpFusionPass() = default;
33 };
34
TEST_F(MindDataTestTensorOpFusionPass,RandomCropDecodeResizeDisabled)35 TEST_F(MindDataTestTensorOpFusionPass, RandomCropDecodeResizeDisabled) {
36 MS_LOG(INFO) << "Doing MindDataTestTensorOpFusionPass-RandomCropDecodeResizeDisabled";
37
38 std::string folder_path = datasets_root_path_ + "/testPK/data/";
39 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
40
41 // Create objects for the tensor ops
42 std::shared_ptr<TensorTransform> decode(new vision::Decode());
43 std::shared_ptr<TensorTransform> random_resized_crop(new vision::RandomResizedCrop({5}));
44 ds = ds->Map({decode, random_resized_crop}, {"image"});
45
46 std::shared_ptr<DatasetNode> node = ds->IRNode();
47 auto ir_tree = std::make_shared<TreeAdapter>();
48 // Disable IR optimization pass
49 ir_tree->SetOptimize(false);
50 Status rc;
51 rc = ir_tree->Compile(node);
52 EXPECT_TRUE(rc);
53 auto root_op = ir_tree->GetRoot();
54
55 auto tree = std::make_shared<ExecutionTree>();
56 auto it = tree->begin(static_cast<std::shared_ptr<DatasetOp>>(root_op));
57 ++it;
58 auto *map_op = &(*it);
59 auto tfuncs = static_cast<MapOp *>(map_op)->TFuncs();
60 auto func_it = tfuncs.begin();
61 EXPECT_EQ((*func_it)->Name(), kDecodeOp);
62 ++func_it;
63 EXPECT_EQ((*func_it)->Name(), kRandomCropAndResizeOp);
64 }
65
TEST_F(MindDataTestTensorOpFusionPass,RandomCropDecodeResizeEnabled)66 TEST_F(MindDataTestTensorOpFusionPass, RandomCropDecodeResizeEnabled) {
67 MS_LOG(INFO) << "Doing MindDataTestTensorOpFusionPass-RandomCropDecodeResizeEnabled";
68
69 std::string folder_path = datasets_root_path_ + "/testPK/data/";
70 std::shared_ptr<Dataset> ds = ImageFolder(folder_path, false, std::make_shared<SequentialSampler>(0, 11));
71
72 // Create objects for the tensor ops
73 std::shared_ptr<TensorTransform> decode(new vision::Decode());
74 std::shared_ptr<TensorTransform> random_resized_crop(new vision::RandomResizedCrop({5}));
75 ds = ds->Map({decode, random_resized_crop}, {"image"});
76
77 std::shared_ptr<DatasetNode> node = ds->IRNode();
78 auto ir_tree = std::make_shared<TreeAdapter>();
79 // Enable IR optimization pass
80 ir_tree->SetOptimize(true);
81 Status rc;
82 rc = ir_tree->Compile(node);
83 EXPECT_TRUE(rc);
84 auto root_op = ir_tree->GetRoot();
85
86 auto tree = std::make_shared<ExecutionTree>();
87 auto it = tree->begin(static_cast<std::shared_ptr<DatasetOp>>(root_op));
88 ++it;
89 auto *map_op = &(*it);
90 auto tfuncs = static_cast<MapOp *>(map_op)->TFuncs();
91 auto func_it = tfuncs.begin();
92 // FIXME: Currently the following 2 commented out verifications for this test will fail because this
93 // optimization is still in ExecutionTree code, and not yet in IR optimization pass
94 // However, use a bogus check for func_it, to avoid compile error for unused variable.
95 EXPECT_EQ(func_it, func_it);
96 // EXPECT_EQ((*func_it)->Name(), kRandomCropDecodeResizeOp);
97 // EXPECT_EQ(++func_it, tfuncs.end());
98 }
99
100